1 #!/usr/bin/env python 2 3 """ 4 Fix instances, removing those which are not part of the distinct set for a given 5 class. 6 7 Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 22 -------- 23 24 To use this module, the easiest approach is to use the fix and fix_structures 25 functions: 26 27 fix_structures(module) # to fix the structures 28 fix(module) # to fix references to the structures 29 30 The more complicated approach involves instantiating a Fixer object: 31 32 fixer = Fixer() 33 34 Then, applying the fixer to an existing module: 35 36 fixer.process_structures(module) 37 fixer.process(module) 38 """ 39 40 from simplify.simplified import * 41 42 # Fixing of instance information. 43 44 class Fixer(Visitor): 45 46 """ 47 The name fixer which traverses the program nodes in a module, typically 48 depth-first, and eliminates references to superfluous instances, replacing 49 them with those from each class's distinct list, if necessary. 50 51 See the simplify.fixnames.Fixer class for a description of the mechanisms 52 used to deal with subprograms. 53 """ 54 55 def __init__(self): 56 57 "Initialise the name fixer." 58 59 Visitor.__init__(self) 60 61 # Satisfy visitor issues. 62 63 self.visitor = self 64 65 def process(self, module): 66 67 "Process the given 'module'." 68 69 # The fixer maintains a list of transformed subprograms (added for each 70 # of the processing "roots" and also for each invoked internal 71 # subprogram), along with a list of current subprograms (used to avoid 72 # recursion issues) and a list of current namespaces (used to recall 73 # namespaces upon invoking internal subprograms). 74 75 self.current_subprograms = [] 76 77 self.module = module 78 79 # Process all functions and methods. 80 81 for subprogram in self.module.simplifier.subprograms: 82 83 # Internal subprograms are skipped here and processed specially via 84 # Invoke nodes. 85 86 if not getattr(subprogram, "internal", 0): 87 for specialised in subprogram.active(): 88 self.process_node(specialised) 89 90 self.process_node(module) 91 92 # Fix the simplifier's subprograms list itself. 93 94 #subprograms = set() 95 #for subprogram in self.module.simplifier.subprograms: 96 # subprograms.add(self._get_replacement(subprogram)) 97 #self.module.simplifier.subprograms = subprograms 98 99 def process_structures(self, module): 100 101 "Process the structures of the given 'module'." 102 103 self.module = module 104 105 # Visit structures and instances. 106 107 for structure in self.module.simplifier.structures: 108 for instance in structure.get_instances(): 109 for name, attrs in instance.namespace.items(): 110 instance.namespace[name] = self._replace(attrs) 111 112 def process_node(self, node): 113 114 """ 115 Process a subprogram or module 'node', discovering from attributes on 116 'node' any initial locals. Return a modified subprogram or module. 117 """ 118 119 # Do not process subprograms already being processed. 120 121 if node in self.current_subprograms: 122 return None 123 124 # Record the current subprogram. 125 126 self.current_subprograms.append(node) 127 128 # Dispatch to the code itself. 129 130 result = self.dispatch(node) 131 132 # Restore the previous subprogram and namespace. 133 134 self.current_subprograms.pop() 135 136 return node 137 138 # Visitor methods. 139 140 def default(self, node): 141 142 """ 143 Process the given 'node', given that it does not have a specific 144 handler. 145 """ 146 147 # Process annotations. 148 149 for name in ("non_accesses", "non_writes", "raises", "returns", "types"): 150 if hasattr(node, name): 151 attrs = getattr(node, name) 152 setattr(node, name, self._replace(attrs)) 153 for name in ("accesses", "writes", "paramtypes"): 154 if hasattr(node, name): 155 d = getattr(node, name) 156 new_d = {} 157 for expr, attrs in d.items(): 158 new_d[self._get_replacement(expr)] = self._replace(attrs, name) 159 setattr(node, name, new_d) 160 161 # Visit program nodes. 162 163 for attr in ("pos_args",): 164 if hasattr(node, attr): 165 self.dispatches(getattr(node, attr)) 166 for attr in ("kw_args",): 167 if hasattr(node, attr): 168 self.dispatch_dict(getattr(node, attr)) 169 for attr in ("expr", "lvalue", "test", "star", "dstar"): 170 if hasattr(node, attr): 171 self.dispatch(getattr(node, attr)) 172 for attr in ("body", "else_", "handler", "finally_", "code", "choices", "nodes"): 173 if hasattr(node, attr): 174 self.dispatches(getattr(node, attr)) 175 if hasattr(node, "params"): 176 for param, default in node.params: 177 self.dispatch(default) 178 for attr in ("star", "dstar"): 179 if getattr(node, attr, None): 180 param, default = getattr(node, attr) 181 self.dispatch(default) 182 183 return node 184 185 def _replace(self, items, name=None): 186 187 """ 188 Produce a new list or set for the given 'items', acquired from the 189 annotation having the given 'name'. 190 """ 191 192 if name == "accesses": 193 new_items = [] 194 else: 195 new_items = set() 196 197 for item in list(items): 198 if name == "accesses": 199 attr, accessor = item 200 value = attr.type 201 new_items.append((Attribute(self._get_replacement(attr.context), self._get_replacement(value)), self._get_replacement(accessor))) 202 else: 203 attr = item 204 value = attr.type 205 new_items.add(Attribute(self._get_replacement(attr.context), self._get_replacement(value))) 206 207 return new_items 208 209 def _get_replacement(self, value): 210 211 "Get a replacement for the given 'value'." 212 213 # Find the distinct instance for any given instance. 214 215 if isinstance(value, Instance): 216 distinct_instances = value.get_class().get_distinct_instances() 217 return distinct_instances[value] 218 219 # For subprograms, find the distinct instance's copy for the owner 220 # instance and assert that the signatures are the same; otherwise, 221 # return the original subprogram. 222 # NOTE: This needs to be verified in a somewhat more formal fashion. 223 224 elif isinstance(value, Subprogram): 225 if hasattr(value, "copy_of") and hasattr(value, "instance"): 226 cls = value.instance.get_class() 227 distinct = cls.get_distinct_instances() 228 instance = distinct[value.instance] 229 if value.copy_of.copies.has_key(instance): 230 subprogram = value.copy_of.copies[instance] 231 if subprogram.paramtypes == value.paramtypes: 232 return subprogram 233 234 return value 235 236 # Return all other values as they are. 237 238 else: 239 return value 240 241 def visitInvokeFunction(self, invoke): 242 243 "Transform the 'invoke' node, performing processing on subprograms." 244 245 return self.default(invoke) 246 247 def visitInvokeRef(self, invoke): 248 249 "Transform the 'invoke' node, performing processing on subprograms." 250 251 # The special case of internal subprogram invocation is addressed by 252 # propagating namespace information to the subprogram and processing it. 253 254 self.process_node(invoke.ref) 255 return invoke 256 257 # Convenience functions. 258 259 def fix_structures(module): 260 261 "Fix the structures in the given 'module'." 262 263 fixer = Fixer() 264 fixer.process_structures(module) 265 266 def fix(module): 267 268 "Fix the structure references in the given 'module'." 269 270 fixer = Fixer() 271 fixer.process(module) 272 273 # vim: tabstop=4 expandtab shiftwidth=4