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.subprograms = set() 76 self.current_subprograms = [] 77 78 self.module = module 79 self.process_node(module) 80 81 # Then, process all functions and methods. 82 83 for subprogram in self.module.simplifier.subprograms: 84 85 # Internal subprograms are skipped here and processed specially via 86 # Invoke nodes. 87 88 if not getattr(subprogram, "internal", 0): 89 for specialised in subprogram.active(): 90 self.subprograms.add(self.process_node(specialised)) 91 92 def process_structures(self, module): 93 94 "Process the structures of the given 'module'." 95 96 self.module = module 97 98 # Visit structures and instances. 99 100 for structure in self.module.simplifier.structures: 101 for instance in structure.get_instances(): 102 for name, attrs in instance.namespace.items(): 103 instance.namespace[name] = self._replace(attrs) 104 105 def process_node(self, node): 106 107 """ 108 Process a subprogram or module 'node', discovering from attributes on 109 'node' any initial locals. Return a modified subprogram or module. 110 """ 111 112 # Do not process subprograms already being processed. 113 114 if node in self.current_subprograms: 115 return None 116 117 # Record the current subprogram. 118 119 self.current_subprograms.append(node) 120 121 # Dispatch to the code itself. 122 123 result = self.dispatch(node) 124 125 # Restore the previous subprogram and namespace. 126 127 self.current_subprograms.pop() 128 129 return node 130 131 # Visitor methods. 132 133 def default(self, node): 134 135 """ 136 Process the given 'node', given that it does not have a specific 137 handler. 138 """ 139 140 # Process annotations. 141 142 for name in ("non_accesses", "non_writes", "raises", "returns", "types"): 143 if hasattr(node, name): 144 attrs = getattr(node, name) 145 setattr(node, name, self._replace(attrs)) 146 for name in ("accesses", "writes", "paramtypes"): 147 if hasattr(node, name): 148 d = getattr(node, name) 149 new_d = {} 150 for expr, attrs in d.items(): 151 new_d[self._get_replacement(expr)] = self._replace(attrs, name) 152 setattr(node, name, new_d) 153 154 # Visit program nodes. 155 156 for attr in ("pos_args",): 157 if hasattr(node, attr): 158 self.dispatches(getattr(node, attr)) 159 for attr in ("kw_args",): 160 if hasattr(node, attr): 161 self.dispatch_dict(getattr(node, attr)) 162 for attr in ("expr", "lvalue", "test", "star", "dstar"): 163 if hasattr(node, attr): 164 self.dispatch(getattr(node, attr)) 165 for attr in ("body", "else_", "handler", "finally_", "code", "choices", "nodes"): 166 if hasattr(node, attr): 167 self.dispatches(getattr(node, attr)) 168 if hasattr(node, "params"): 169 for param, default in node.params: 170 self.dispatch(default) 171 for attr in ("star", "dstar"): 172 if getattr(node, attr, None): 173 param, default = getattr(node, attr) 174 self.dispatch(default) 175 176 return node 177 178 def _replace(self, items, name=None): 179 180 """ 181 Produce a new list or set for the given 'items', acquired from the 182 annotation having the given 'name'. 183 """ 184 185 if name == "accesses": 186 new_items = [] 187 else: 188 new_items = set() 189 190 for item in list(items): 191 if name == "accesses": 192 attr, accessor = item 193 value = attr.type 194 new_items.append((Attribute(self._get_replacement(attr.context), self._get_replacement(value)), self._get_replacement(accessor))) 195 else: 196 attr = item 197 value = attr.type 198 new_items.add(Attribute(self._get_replacement(attr.context), self._get_replacement(value))) 199 200 return new_items 201 202 def _get_replacement(self, value): 203 204 "Get a replacement for the given 'value'." 205 206 # Find the distinct instance for any given instance. 207 208 if isinstance(value, Instance): 209 distinct_instances = value.get_class().get_distinct_instances() 210 return distinct_instances[value] 211 212 # For subprograms, find the distinct instance's copy for the owner 213 # instance and assert that the signatures are the same; otherwise, 214 # return the original subprogram. 215 # NOTE: This needs to be verified in a somewhat more formal fashion. 216 217 elif isinstance(value, Subprogram): 218 if hasattr(value, "copy_of") and hasattr(value, "instance"): 219 cls = value.instance.get_class() 220 distinct = cls.get_distinct_instances() 221 instance = distinct[value.instance] 222 if value.copy_of.copies.has_key(instance): 223 subprogram = value.copy_of.copies[instance] 224 if subprogram.paramtypes == value.paramtypes: 225 return subprogram 226 227 return value 228 229 # Return all other values as they are. 230 231 else: 232 return value 233 234 def dispatch(self, node, *args): 235 return Visitor.dispatch(self, node, *args) 236 237 def visitInvokeFunction(self, invoke): 238 239 "Transform the 'invoke' node, performing processing on subprograms." 240 241 return self.default(invoke) 242 243 def visitInvokeRef(self, invoke): 244 245 "Transform the 'invoke' node, performing processing on subprograms." 246 247 # The special case of internal subprogram invocation is addressed by 248 # propagating namespace information to the subprogram and processing it. 249 250 subprogram = self.process_node(invoke.ref) 251 252 if subprogram is not None: 253 self.subprograms.add(subprogram) 254 return invoke 255 256 # Convenience functions. 257 258 def fix_structures(module): 259 260 "Fix the structures in the given 'module'." 261 262 fixer = Fixer() 263 fixer.process_structures(module) 264 265 def fix(module): 266 267 "Fix the structure references in the given 'module'." 268 269 fixer = Fixer() 270 fixer.process(module) 271 272 # vim: tabstop=4 expandtab shiftwidth=4