paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Inspect source files, obtaining details of classes and attributes. |
paul@0 | 5 | |
paul@299 | 6 | Copyright (C) 2007, 2008, 2009, 2010 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public License along with |
paul@0 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@91 | 20 | |
paul@91 | 21 | -------- |
paul@91 | 22 | |
paul@91 | 23 | The results of inspecting a module are as follows: |
paul@91 | 24 | |
paul@91 | 25 | Constants |
paul@91 | 26 | --------- |
paul@91 | 27 | |
paul@91 | 28 | All constants identified within the code shall be registered. |
paul@91 | 29 | |
paul@91 | 30 | Classes |
paul@91 | 31 | ------- |
paul@91 | 32 | |
paul@91 | 33 | All global classes shall be registered; local classes (within functions) or |
paul@91 | 34 | nested classes (within classes) are not currently registered. |
paul@91 | 35 | |
paul@91 | 36 | Base classes must be detected and constant. |
paul@91 | 37 | |
paul@91 | 38 | All classes without bases are made to inherit from __builtins__.object in order |
paul@91 | 39 | to support some standard methods. |
paul@91 | 40 | |
paul@91 | 41 | Functions |
paul@91 | 42 | --------- |
paul@91 | 43 | |
paul@91 | 44 | All functions and lambda definitions shall be registered. |
paul@91 | 45 | |
paul@91 | 46 | Namespaces |
paul@91 | 47 | ---------- |
paul@91 | 48 | |
paul@91 | 49 | Modules define their own "global" namespace, within which classes, functions |
paul@91 | 50 | and lambda definitions establish a hierarchy of namespaces. |
paul@91 | 51 | |
paul@91 | 52 | Only local, global and built-in namespaces are recognised; closures are not |
paul@91 | 53 | supported. |
paul@91 | 54 | |
paul@91 | 55 | Assignments |
paul@91 | 56 | ----------- |
paul@91 | 57 | |
paul@91 | 58 | Name assignment and attribute assignment involving modules and classes cause |
paul@91 | 59 | names to be associated with values within namespaces. |
paul@91 | 60 | |
paul@91 | 61 | Any assignments within loops are considered to cause the targets of such |
paul@91 | 62 | assignments to provide non-constant values. |
paul@91 | 63 | |
paul@91 | 64 | Assignments to names are only really considered to cause the targets of such |
paul@91 | 65 | assignments to provide constant values if the targets reside in the module |
paul@91 | 66 | namespace or in class namespaces, subject to the above conditions. |
paul@91 | 67 | |
paul@91 | 68 | Assignments to names within functions are not generally considered to cause the |
paul@91 | 69 | targets of such assignments to provide constant values since functions can be |
paul@91 | 70 | invoked many times with different inputs. However, there may be benefits in |
paul@91 | 71 | considering a local to be constant within a single invocation. |
paul@0 | 72 | """ |
paul@0 | 73 | |
paul@39 | 74 | from micropython.common import * |
paul@83 | 75 | from micropython.data import * |
paul@0 | 76 | import compiler.ast |
paul@0 | 77 | from compiler.visitor import ASTVisitor |
paul@15 | 78 | |
paul@21 | 79 | # Program visitors. |
paul@21 | 80 | |
paul@15 | 81 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 82 | |
paul@20 | 83 | """ |
paul@20 | 84 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 85 | capable of being used as an AST visitor. |
paul@20 | 86 | """ |
paul@15 | 87 | |
paul@71 | 88 | def __init__(self, name, importer): |
paul@71 | 89 | |
paul@71 | 90 | """ |
paul@71 | 91 | Initialise this visitor with a module 'name' and an 'importer' which is |
paul@71 | 92 | used to provide access to other modules when required. |
paul@71 | 93 | """ |
paul@71 | 94 | |
paul@15 | 95 | ASTVisitor.__init__(self) |
paul@15 | 96 | Module.__init__(self, name) |
paul@15 | 97 | self.visitor = self |
paul@15 | 98 | |
paul@91 | 99 | # Import machinery links. |
paul@91 | 100 | |
paul@15 | 101 | self.importer = importer |
paul@167 | 102 | self.optimisations = importer.optimisations |
paul@61 | 103 | self.builtins = self.importer.modules.get("__builtins__") |
paul@15 | 104 | self.loaded = 0 |
paul@15 | 105 | |
paul@1 | 106 | # Current expression state. |
paul@1 | 107 | |
paul@1 | 108 | self.expr = None |
paul@1 | 109 | |
paul@1 | 110 | # Namespace state. |
paul@1 | 111 | |
paul@34 | 112 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@91 | 113 | self.in_method = 0 # Find instance attributes in all methods. |
paul@34 | 114 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 115 | self.namespaces = [] |
paul@0 | 116 | self.module = None |
paul@235 | 117 | self.functions = [] |
paul@0 | 118 | |
paul@0 | 119 | def parse(self, filename): |
paul@9 | 120 | |
paul@9 | 121 | "Parse the file having the given 'filename'." |
paul@9 | 122 | |
paul@0 | 123 | module = compiler.parseFile(filename) |
paul@0 | 124 | self.process(module) |
paul@0 | 125 | |
paul@0 | 126 | def process(self, module): |
paul@9 | 127 | |
paul@9 | 128 | "Process the given 'module'." |
paul@9 | 129 | |
paul@91 | 130 | self.astnode = self.module = module |
paul@236 | 131 | |
paul@260 | 132 | # Add __name__ to the namespace by adding an explicit assignment to the |
paul@260 | 133 | # module. |
paul@260 | 134 | |
paul@260 | 135 | module.node.nodes.insert(0, compiler.ast.Assign( |
paul@260 | 136 | [compiler.ast.AssName("__name__", 0)], |
paul@260 | 137 | compiler.ast.Const(self.full_name()) |
paul@260 | 138 | )) |
paul@260 | 139 | |
paul@236 | 140 | # First, visit module-level code, recording global names. |
paul@236 | 141 | |
paul@1 | 142 | processed = self.dispatch(module) |
paul@235 | 143 | |
paul@236 | 144 | # Then, for each function, detect and record globals declared in those |
paul@236 | 145 | # functions. |
paul@236 | 146 | |
paul@236 | 147 | for node, namespaces in self.functions: |
paul@236 | 148 | self.process_globals(node) |
paul@236 | 149 | |
paul@284 | 150 | self.finalise_namespace(self) |
paul@284 | 151 | |
paul@236 | 152 | # Then, visit each function, recording other names. |
paul@236 | 153 | |
paul@235 | 154 | for node, namespaces in self.functions: |
paul@235 | 155 | self._visitFunctionBody(node, namespaces) |
paul@280 | 156 | self.finalise_namespace(namespaces[-1]) |
paul@280 | 157 | |
paul@236 | 158 | # Add references to other modules declared using the __all__ global. |
paul@236 | 159 | |
paul@13 | 160 | if self.has_key("__all__"): |
paul@12 | 161 | all = self["__all__"] |
paul@1 | 162 | if isinstance(all, compiler.ast.List): |
paul@1 | 163 | for n in all.nodes: |
paul@167 | 164 | self.store(n.value, self.importer.add_module(self.name + "." + n.value)) |
paul@235 | 165 | |
paul@1 | 166 | return processed |
paul@0 | 167 | |
paul@236 | 168 | def process_globals(self, node): |
paul@236 | 169 | |
paul@236 | 170 | """ |
paul@236 | 171 | Within the given 'node', process global declarations, adjusting the |
paul@236 | 172 | module namespace. |
paul@236 | 173 | """ |
paul@236 | 174 | |
paul@236 | 175 | for n in node.getChildNodes(): |
paul@236 | 176 | if isinstance(n, compiler.ast.Global): |
paul@236 | 177 | for name in n.names: |
paul@236 | 178 | if not self.has_key(name): |
paul@236 | 179 | self[name] = None |
paul@236 | 180 | else: |
paul@236 | 181 | self.process_globals(n) |
paul@236 | 182 | |
paul@280 | 183 | def finalise_namespace(self, namespace): |
paul@280 | 184 | |
paul@280 | 185 | "Finalise the given 'namespace'." |
paul@280 | 186 | |
paul@280 | 187 | for names in namespace.get_all_attribute_usage(): |
paul@280 | 188 | self.importer.use_names(names, namespace.full_name()) |
paul@280 | 189 | |
paul@3 | 190 | def vacuum(self): |
paul@9 | 191 | |
paul@171 | 192 | """ |
paul@171 | 193 | Vacuum the module namespace, removing unreferenced objects and unused |
paul@171 | 194 | names. |
paul@171 | 195 | """ |
paul@9 | 196 | |
paul@241 | 197 | if self.should_optimise_unused_objects(): |
paul@252 | 198 | self.vacuum_object(self) |
paul@0 | 199 | |
paul@167 | 200 | all_objects = list(self.all_objects) |
paul@167 | 201 | |
paul@167 | 202 | for obj in all_objects: |
paul@252 | 203 | if isinstance(obj, Class): |
paul@252 | 204 | self.vacuum_object(obj) |
paul@171 | 205 | |
paul@252 | 206 | def vacuum_object(self, obj, delete_all=0): |
paul@241 | 207 | |
paul@252 | 208 | "Vacuum the given object 'obj'." |
paul@171 | 209 | |
paul@252 | 210 | for name, attr in obj.items(): |
paul@270 | 211 | |
paul@270 | 212 | # Only consider deleting entire unused objects or things accessible |
paul@270 | 213 | # via names which are never used. |
paul@270 | 214 | |
paul@280 | 215 | if delete_all or not self.importer.uses_attribute(obj, name): |
paul@252 | 216 | del obj[name] |
paul@270 | 217 | |
paul@270 | 218 | # Delete any unambiguous attribute value. Such values can only |
paul@270 | 219 | # have been defined within the object and therefore are not |
paul@270 | 220 | # redefined by other code regions. |
paul@270 | 221 | |
paul@252 | 222 | if attr.assignments == 1: |
paul@252 | 223 | value = attr.get_value() |
paul@171 | 224 | |
paul@252 | 225 | if value is not obj and value in self.all_objects: |
paul@252 | 226 | self.all_objects.remove(value) |
paul@171 | 227 | |
paul@252 | 228 | # Delete class contents. |
paul@241 | 229 | |
paul@252 | 230 | if isinstance(value, Class): |
paul@252 | 231 | self.vacuum_object(value, 1) |
paul@171 | 232 | |
paul@287 | 233 | def unfinalise(self): |
paul@287 | 234 | |
paul@287 | 235 | "Reset finalised information for the module." |
paul@287 | 236 | |
paul@287 | 237 | for obj in self.all_objects: |
paul@287 | 238 | obj.unfinalise_attributes() |
paul@287 | 239 | |
paul@171 | 240 | def finalise(self): |
paul@171 | 241 | |
paul@171 | 242 | "Finalise the module." |
paul@171 | 243 | |
paul@171 | 244 | for obj in self.all_objects: |
paul@287 | 245 | obj.finalise_attributes() |
paul@171 | 246 | |
paul@167 | 247 | def add_object(self, obj, any_scope=0): |
paul@167 | 248 | |
paul@167 | 249 | """ |
paul@167 | 250 | Record 'obj' if non-local or if the optional 'any_scope' is set to a |
paul@167 | 251 | true value. |
paul@167 | 252 | """ |
paul@167 | 253 | |
paul@167 | 254 | if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@167 | 255 | self.all_objects.add(obj) |
paul@167 | 256 | |
paul@167 | 257 | # Optimisation tests. |
paul@167 | 258 | |
paul@167 | 259 | def should_optimise_unused_objects(self): |
paul@167 | 260 | return "unused_objects" in self.optimisations |
paul@167 | 261 | |
paul@3 | 262 | # Namespace methods. |
paul@0 | 263 | |
paul@0 | 264 | def store(self, name, obj): |
paul@12 | 265 | |
paul@12 | 266 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 267 | |
paul@12 | 268 | if not self.namespaces: |
paul@34 | 269 | self.set(name, obj, not self.in_loop) |
paul@12 | 270 | else: |
paul@34 | 271 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 272 | |
paul@85 | 273 | def store_lambda(self, obj): |
paul@91 | 274 | |
paul@91 | 275 | "Store a lambda function 'obj'." |
paul@91 | 276 | |
paul@167 | 277 | self.add_object(obj) |
paul@85 | 278 | |
paul@94 | 279 | def store_module_attr(self, name, module): |
paul@94 | 280 | |
paul@94 | 281 | """ |
paul@94 | 282 | Record module attribute 'name' in the given 'module' using the current |
paul@94 | 283 | expression. |
paul@94 | 284 | """ |
paul@94 | 285 | |
paul@192 | 286 | module.set(name, self.expr, 0) |
paul@94 | 287 | |
paul@91 | 288 | def store_class_attr(self, name): |
paul@91 | 289 | |
paul@94 | 290 | """ |
paul@94 | 291 | Record class attribute 'name' in the current class using the current |
paul@94 | 292 | expression. |
paul@94 | 293 | """ |
paul@91 | 294 | |
paul@91 | 295 | if self.in_method and self.namespaces[-2].has_key(name): |
paul@192 | 296 | self.namespaces[-2].set(name, self.expr, 0) |
paul@91 | 297 | return 1 |
paul@91 | 298 | |
paul@91 | 299 | return 0 |
paul@91 | 300 | |
paul@34 | 301 | def store_instance_attr(self, name): |
paul@12 | 302 | |
paul@12 | 303 | "Record instance attribute 'name' in the current class." |
paul@12 | 304 | |
paul@91 | 305 | if self.in_method: |
paul@15 | 306 | |
paul@12 | 307 | # Current namespace is the function. |
paul@12 | 308 | # Previous namespace is the class. |
paul@15 | 309 | |
paul@12 | 310 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 311 | |
paul@272 | 312 | def get_namespace(self): |
paul@71 | 313 | |
paul@71 | 314 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 315 | |
paul@12 | 316 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 317 | |
paul@250 | 318 | def use_name(self, name): |
paul@250 | 319 | |
paul@250 | 320 | "Use the given 'name' within the current namespace/unit." |
paul@250 | 321 | |
paul@272 | 322 | unit = self.get_namespace() |
paul@280 | 323 | self.importer.use_name(name, unit.full_name()) |
paul@250 | 324 | |
paul@279 | 325 | # Attribute usage methods. |
paul@279 | 326 | # These are convenience methods which refer to the specific namespace's |
paul@279 | 327 | # implementation of these operations. |
paul@279 | 328 | |
paul@272 | 329 | def new_branchpoint(self): |
paul@272 | 330 | self.get_namespace()._new_branchpoint() |
paul@272 | 331 | |
paul@272 | 332 | def new_branch(self): |
paul@272 | 333 | self.get_namespace()._new_branch() |
paul@272 | 334 | |
paul@299 | 335 | def abandon_branch(self): |
paul@299 | 336 | self.get_namespace()._abandon_branch() |
paul@299 | 337 | |
paul@272 | 338 | def shelve_branch(self): |
paul@272 | 339 | self.get_namespace()._shelve_branch() |
paul@272 | 340 | |
paul@272 | 341 | def merge_branches(self): |
paul@272 | 342 | self.get_namespace()._merge_branches() |
paul@272 | 343 | |
paul@278 | 344 | def define_attribute_user(self, node): |
paul@279 | 345 | |
paul@279 | 346 | """ |
paul@279 | 347 | Define 'node' as the user of attributes, indicating the point where the |
paul@279 | 348 | user is defined. |
paul@279 | 349 | """ |
paul@279 | 350 | |
paul@278 | 351 | self.get_namespace()._define_attribute_user(node) |
paul@272 | 352 | |
paul@280 | 353 | def use_attribute(self, name, attrname): |
paul@280 | 354 | return self.get_namespace()._use_attribute(name, attrname) |
paul@272 | 355 | |
paul@285 | 356 | # Specific attribute usage, nominating specific attributes which can be |
paul@285 | 357 | # resolved during inspection. |
paul@285 | 358 | |
paul@285 | 359 | def use_specific_attribute(self, objname, attrname): |
paul@285 | 360 | from_name = self.get_namespace().full_name() |
paul@285 | 361 | objname = objname or from_name |
paul@285 | 362 | self.importer.use_specific_name(objname, attrname, from_name) |
paul@285 | 363 | |
paul@3 | 364 | # Visitor methods. |
paul@3 | 365 | |
paul@3 | 366 | def default(self, node, *args): |
paul@39 | 367 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 368 | |
paul@3 | 369 | def dispatch(self, node, *args): |
paul@3 | 370 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 371 | |
paul@0 | 372 | def NOP(self, node): |
paul@21 | 373 | for n in node.getChildNodes(): |
paul@21 | 374 | self.dispatch(n) |
paul@19 | 375 | return None |
paul@0 | 376 | |
paul@299 | 377 | def NOP_ABANDON(self, node): |
paul@299 | 378 | self.abandon_branch() |
paul@299 | 379 | self.NOP(node) |
paul@299 | 380 | |
paul@82 | 381 | def OP(self, node): |
paul@82 | 382 | for n in node.getChildNodes(): |
paul@82 | 383 | self.dispatch(n) |
paul@82 | 384 | return Instance() |
paul@1 | 385 | |
paul@172 | 386 | def _visitUnary(self, node): |
paul@172 | 387 | |
paul@172 | 388 | "Accounting method for the unary operator 'node'." |
paul@172 | 389 | |
paul@172 | 390 | method = unary_methods[node.__class__.__name__] |
paul@250 | 391 | self.use_name(method) |
paul@172 | 392 | return self.OP(node) |
paul@172 | 393 | |
paul@172 | 394 | def _visitBinary(self, node): |
paul@172 | 395 | |
paul@172 | 396 | "Accounting method for the binary operator 'node'." |
paul@172 | 397 | |
paul@172 | 398 | left_method, right_method = binary_methods[node.__class__.__name__] |
paul@250 | 399 | self.use_name(left_method) |
paul@250 | 400 | self.use_name(right_method) |
paul@172 | 401 | return self.OP(node) |
paul@172 | 402 | |
paul@85 | 403 | def _visitFunction(self, node, name): |
paul@85 | 404 | |
paul@85 | 405 | """ |
paul@85 | 406 | Return a function object for the function defined by 'node' with the |
paul@85 | 407 | given 'name'. If a lambda expression is being visited, 'name' should be |
paul@85 | 408 | None. |
paul@85 | 409 | """ |
paul@85 | 410 | |
paul@233 | 411 | # Define the function object. |
paul@233 | 412 | |
paul@85 | 413 | function = Function( |
paul@85 | 414 | name, |
paul@272 | 415 | self.get_namespace(), |
paul@85 | 416 | node.argnames, |
paul@85 | 417 | node.defaults, |
paul@85 | 418 | (node.flags & 4 != 0), |
paul@85 | 419 | (node.flags & 8 != 0), |
paul@85 | 420 | self, |
paul@85 | 421 | node |
paul@85 | 422 | ) |
paul@85 | 423 | |
paul@233 | 424 | self.add_object(function, any_scope=1) |
paul@233 | 425 | |
paul@85 | 426 | # Make a back reference from the node for code generation. |
paul@85 | 427 | |
paul@85 | 428 | node.unit = function |
paul@85 | 429 | |
paul@85 | 430 | # Process the defaults. |
paul@85 | 431 | |
paul@85 | 432 | for n in node.defaults: |
paul@85 | 433 | self.expr = self.dispatch(n) |
paul@192 | 434 | function.store_default(self.expr) |
paul@85 | 435 | |
paul@235 | 436 | self.functions.append((node, self.namespaces + [function])) |
paul@85 | 437 | |
paul@85 | 438 | if name is not None: |
paul@85 | 439 | self.store(name, function) |
paul@85 | 440 | else: |
paul@85 | 441 | self.store_lambda(function) |
paul@167 | 442 | |
paul@167 | 443 | # Where defaults exist, an instance needs creating. Thus, it makes |
paul@167 | 444 | # no sense to return a reference to the function here, since the |
paul@167 | 445 | # recipient will not be referencing the function itself. |
paul@167 | 446 | |
paul@109 | 447 | if node.defaults: |
paul@167 | 448 | return Instance() # indicates no known target |
paul@85 | 449 | |
paul@85 | 450 | return function |
paul@85 | 451 | |
paul@235 | 452 | def _visitFunctionBody(self, node, namespaces): |
paul@235 | 453 | |
paul@235 | 454 | "Enter the function." |
paul@235 | 455 | |
paul@235 | 456 | # Current namespace is the function. |
paul@235 | 457 | # Previous namespace is the class. |
paul@235 | 458 | |
paul@235 | 459 | if len(namespaces) > 1 and isinstance(namespaces[-2], Class): |
paul@235 | 460 | if namespaces[-1].name == "__init__": |
paul@235 | 461 | self.in_init = 1 |
paul@235 | 462 | self.in_method = 1 |
paul@235 | 463 | |
paul@235 | 464 | self.namespaces = namespaces |
paul@235 | 465 | self.dispatch(node.code) |
paul@235 | 466 | |
paul@235 | 467 | self.in_init = 0 |
paul@235 | 468 | self.in_method = 0 |
paul@235 | 469 | |
paul@172 | 470 | # Specific handler methods. |
paul@172 | 471 | |
paul@172 | 472 | visitAdd = _visitBinary |
paul@82 | 473 | |
paul@82 | 474 | visitAnd = OP |
paul@0 | 475 | |
paul@4 | 476 | visitAssert = NOP |
paul@4 | 477 | |
paul@0 | 478 | def visitAssign(self, node): |
paul@0 | 479 | self.expr = self.dispatch(node.expr) |
paul@0 | 480 | for n in node.nodes: |
paul@0 | 481 | self.dispatch(n) |
paul@0 | 482 | return None |
paul@0 | 483 | |
paul@0 | 484 | def visitAssAttr(self, node): |
paul@0 | 485 | expr = self.dispatch(node.expr) |
paul@272 | 486 | |
paul@272 | 487 | # Record the attribute on the presumed target. |
paul@272 | 488 | |
paul@94 | 489 | if isinstance(expr, Attr): |
paul@94 | 490 | if expr.name == "self": |
paul@94 | 491 | if not self.store_class_attr(node.attrname): |
paul@94 | 492 | self.store_instance_attr(node.attrname) |
paul@192 | 493 | elif isinstance(expr.get_value(), Module): |
paul@192 | 494 | self.store_module_attr(node.attrname, expr.get_value()) |
paul@192 | 495 | print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) |
paul@272 | 496 | |
paul@285 | 497 | # Note usage of the attribute where a local is involved. |
paul@272 | 498 | |
paul@272 | 499 | if expr.parent is self.get_namespace(): |
paul@280 | 500 | node._attrnames = self.use_attribute(expr.name, node.attrname) |
paul@272 | 501 | |
paul@0 | 502 | return None |
paul@0 | 503 | |
paul@0 | 504 | def visitAssList(self, node): |
paul@204 | 505 | |
paul@204 | 506 | # Declare names which will be used by generated code. |
paul@204 | 507 | |
paul@250 | 508 | self.use_name("__getitem__") |
paul@204 | 509 | |
paul@204 | 510 | # Process the assignment. |
paul@204 | 511 | |
paul@139 | 512 | for i, n in enumerate(node.nodes): |
paul@0 | 513 | self.dispatch(n) |
paul@168 | 514 | self.importer.make_constant(i) # for __getitem__(i) at run-time |
paul@0 | 515 | return None |
paul@0 | 516 | |
paul@0 | 517 | def visitAssName(self, node): |
paul@289 | 518 | if hasattr(node, "flags") and node.flags == "OP_DELETE": |
paul@298 | 519 | print "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) |
paul@298 | 520 | #raise InspectError(self.full_name(), node, "Deletion of attribute %r is not supported." % node.name) |
paul@274 | 521 | |
paul@120 | 522 | self.store(node.name, self.expr) |
paul@278 | 523 | self.define_attribute_user(node) |
paul@285 | 524 | |
paul@285 | 525 | # Ensure the presence of the given name in this namespace. |
paul@285 | 526 | |
paul@285 | 527 | self.use_specific_attribute(None, node.name) |
paul@0 | 528 | return None |
paul@0 | 529 | |
paul@0 | 530 | visitAssTuple = visitAssList |
paul@0 | 531 | |
paul@172 | 532 | def visitAugAssign(self, node): |
paul@172 | 533 | |
paul@172 | 534 | # Accounting. |
paul@172 | 535 | |
paul@172 | 536 | aug_method, (left_method, right_method) = augassign_methods[node.op] |
paul@250 | 537 | self.use_name(aug_method) |
paul@250 | 538 | self.use_name(left_method) |
paul@250 | 539 | self.use_name(right_method) |
paul@244 | 540 | |
paul@244 | 541 | # Process the assignment. |
paul@244 | 542 | |
paul@244 | 543 | self.expr = self.dispatch(node.expr) |
paul@244 | 544 | |
paul@244 | 545 | # NOTE: Similar to micropython.ast handler code. |
paul@244 | 546 | # NOTE: Slices and subscripts not supported. |
paul@244 | 547 | |
paul@244 | 548 | if isinstance(node.node, compiler.ast.Name): |
paul@244 | 549 | self.visitAssName(node.node) |
paul@244 | 550 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@244 | 551 | self.visitAssAttr(node.node) |
paul@244 | 552 | else: |
paul@244 | 553 | raise InspectError(self.full_name(), node, "AugAssign(Slice or Subscript)") |
paul@244 | 554 | |
paul@244 | 555 | return None |
paul@2 | 556 | |
paul@82 | 557 | visitBackquote = OP |
paul@4 | 558 | |
paul@172 | 559 | visitBitand = _visitBinary |
paul@2 | 560 | |
paul@172 | 561 | visitBitor = _visitBinary |
paul@2 | 562 | |
paul@172 | 563 | visitBitxor = _visitBinary |
paul@4 | 564 | |
paul@299 | 565 | visitBreak = NOP_ABANDON |
paul@4 | 566 | |
paul@82 | 567 | visitCallFunc = OP |
paul@0 | 568 | |
paul@0 | 569 | def visitClass(self, node): |
paul@91 | 570 | |
paul@91 | 571 | """ |
paul@91 | 572 | Register the class at the given 'node' subject to the restrictions |
paul@91 | 573 | mentioned in the module docstring. |
paul@91 | 574 | """ |
paul@91 | 575 | |
paul@12 | 576 | if self.namespaces: |
paul@41 | 577 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@82 | 578 | return None |
paul@0 | 579 | else: |
paul@272 | 580 | cls = Class(node.name, self.get_namespace(), self, node) |
paul@37 | 581 | |
paul@37 | 582 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 583 | # definitions of classes. |
paul@37 | 584 | |
paul@0 | 585 | for base in node.bases: |
paul@36 | 586 | expr = self.dispatch(base) |
paul@36 | 587 | if isinstance(expr, Attr): |
paul@36 | 588 | if expr.assignments != 1: |
paul@39 | 589 | raise InspectError(self.full_name(), node, |
paul@40 | 590 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 591 | else: |
paul@192 | 592 | cls.add_base(expr.get_value()) |
paul@36 | 593 | else: # if expr is None: |
paul@39 | 594 | raise InspectError(self.full_name(), node, |
paul@40 | 595 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 596 | |
paul@59 | 597 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 598 | # If no bases exist, adopt the 'object' class. |
paul@59 | 599 | |
paul@61 | 600 | if not node.bases and not (self.name == "__builtins__" and node.name == "object") : |
paul@59 | 601 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@192 | 602 | cls.add_base(expr.get_value()) |
paul@59 | 603 | |
paul@21 | 604 | # Make a back reference from the node for code generation. |
paul@21 | 605 | |
paul@39 | 606 | node.unit = cls |
paul@21 | 607 | |
paul@0 | 608 | # Make an entry for the class. |
paul@0 | 609 | |
paul@0 | 610 | self.store(node.name, cls) |
paul@167 | 611 | self.add_object(cls) |
paul@0 | 612 | |
paul@59 | 613 | # Process the class body. |
paul@59 | 614 | |
paul@12 | 615 | self.namespaces.append(cls) |
paul@0 | 616 | self.dispatch(node.code) |
paul@284 | 617 | self.finalise_namespace(cls) |
paul@12 | 618 | self.namespaces.pop() |
paul@0 | 619 | |
paul@82 | 620 | return cls |
paul@0 | 621 | |
paul@172 | 622 | def visitCompare(self, node): |
paul@172 | 623 | |
paul@172 | 624 | # Accounting. |
paul@172 | 625 | # NOTE: Replicates some code in micropython.ast.visitCompare. |
paul@172 | 626 | |
paul@172 | 627 | for op in node.ops: |
paul@172 | 628 | op_name, next_node = op |
paul@172 | 629 | methods = comparison_methods[op_name] |
paul@172 | 630 | if methods is not None: |
paul@250 | 631 | self.use_name(methods[0]) |
paul@250 | 632 | self.use_name(methods[1]) |
paul@172 | 633 | elif op_name.endswith("in"): |
paul@250 | 634 | self.use_name("__contains__") |
paul@172 | 635 | |
paul@172 | 636 | return self.OP(node) |
paul@4 | 637 | |
paul@21 | 638 | def visitConst(self, node): |
paul@177 | 639 | |
paul@177 | 640 | # Register the constant, if necessary, returning the resulting object. |
paul@177 | 641 | |
paul@252 | 642 | self.use_name(self.importer.get_constant_type_name(node.value)) |
paul@168 | 643 | return self.importer.make_constant(node.value) |
paul@0 | 644 | |
paul@299 | 645 | visitContinue = NOP_ABANDON |
paul@4 | 646 | |
paul@4 | 647 | visitDecorators = NOP |
paul@4 | 648 | |
paul@82 | 649 | visitDict = OP |
paul@0 | 650 | |
paul@0 | 651 | visitDiscard = NOP |
paul@0 | 652 | |
paul@172 | 653 | visitDiv = _visitBinary |
paul@4 | 654 | |
paul@4 | 655 | visitEllipsis = NOP |
paul@4 | 656 | |
paul@4 | 657 | visitExec = NOP |
paul@4 | 658 | |
paul@82 | 659 | visitExpression = OP |
paul@4 | 660 | |
paul@172 | 661 | visitFloorDiv = _visitBinary |
paul@4 | 662 | |
paul@34 | 663 | def visitFor(self, node): |
paul@272 | 664 | self.new_branchpoint() |
paul@204 | 665 | |
paul@204 | 666 | # Declare names which will be used by generated code. |
paul@204 | 667 | |
paul@250 | 668 | self.use_name("__iter__") |
paul@250 | 669 | self.use_name("next") |
paul@204 | 670 | |
paul@289 | 671 | self.in_loop = 1 |
paul@272 | 672 | self.dispatch(node.assign) |
paul@272 | 673 | self.dispatch(node.list) |
paul@272 | 674 | |
paul@204 | 675 | # Enter the loop. |
paul@275 | 676 | # Propagate attribute usage to branches. |
paul@204 | 677 | |
paul@272 | 678 | self.new_branch() |
paul@272 | 679 | self.dispatch(node.body) |
paul@272 | 680 | self.shelve_branch() |
paul@34 | 681 | self.in_loop = 0 |
paul@272 | 682 | |
paul@275 | 683 | # Maintain a branch for the else clause or the current retained usage |
paul@275 | 684 | # where execution avoids the conditional clauses. |
paul@275 | 685 | |
paul@275 | 686 | self.new_branch() |
paul@272 | 687 | if node.else_ is not None: |
paul@272 | 688 | self.dispatch(node.else_) |
paul@275 | 689 | self.shelve_branch() |
paul@272 | 690 | |
paul@272 | 691 | self.merge_branches() |
paul@82 | 692 | return None |
paul@0 | 693 | |
paul@0 | 694 | def visitFrom(self, node): |
paul@0 | 695 | module = self.importer.load(node.modname, 1) |
paul@0 | 696 | |
paul@35 | 697 | #if module is None: |
paul@35 | 698 | # print "Warning:", node.modname, "not imported." |
paul@0 | 699 | |
paul@0 | 700 | for name, alias in node.names: |
paul@0 | 701 | if name != "*": |
paul@285 | 702 | if module is not None and module.has_key(name): |
paul@24 | 703 | attr = module[name] |
paul@192 | 704 | self.store(alias or name, attr) |
paul@192 | 705 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@192 | 706 | self.importer.load(attr.get_value().name) |
paul@24 | 707 | |
paul@24 | 708 | # Support the import of names from missing modules. |
paul@24 | 709 | |
paul@3 | 710 | else: |
paul@24 | 711 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 712 | else: |
paul@3 | 713 | if module is not None: |
paul@285 | 714 | for n in module.keys(): |
paul@24 | 715 | attr = module[n] |
paul@192 | 716 | self.store(n, attr) |
paul@192 | 717 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@192 | 718 | self.importer.load(attr.get_value().name) |
paul@0 | 719 | |
paul@0 | 720 | return None |
paul@0 | 721 | |
paul@0 | 722 | def visitFunction(self, node): |
paul@85 | 723 | return self._visitFunction(node, node.name) |
paul@0 | 724 | |
paul@82 | 725 | visitGenExpr = OP |
paul@4 | 726 | |
paul@4 | 727 | visitGenExprFor = NOP |
paul@4 | 728 | |
paul@4 | 729 | visitGenExprIf = NOP |
paul@4 | 730 | |
paul@4 | 731 | visitGenExprInner = NOP |
paul@4 | 732 | |
paul@0 | 733 | def visitGetattr(self, node): |
paul@0 | 734 | expr = self.dispatch(node.expr) |
paul@169 | 735 | attrname = node.attrname |
paul@169 | 736 | |
paul@272 | 737 | # Attempt to identify the nature of the attribute. |
paul@272 | 738 | |
paul@36 | 739 | if isinstance(expr, Attr): |
paul@192 | 740 | value = expr.get_value() |
paul@285 | 741 | |
paul@285 | 742 | # Get the attribute and record its usage. |
paul@285 | 743 | |
paul@87 | 744 | if isinstance(value, (Class, Module)): |
paul@285 | 745 | attr = value.get(attrname) |
paul@285 | 746 | self.use_specific_attribute(value.full_name(), attrname) |
paul@285 | 747 | |
paul@36 | 748 | elif isinstance(value, UnresolvedName): |
paul@169 | 749 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@285 | 750 | |
paul@87 | 751 | else: |
paul@167 | 752 | attr = None |
paul@272 | 753 | |
paul@285 | 754 | # Note usage of the attribute where a local is involved. |
paul@272 | 755 | |
paul@285 | 756 | if expr.parent is self.get_namespace(): |
paul@285 | 757 | node._attrnames = self.use_attribute(expr.name, attrname) |
paul@285 | 758 | else: |
paul@285 | 759 | self.use_name(attrname) |
paul@272 | 760 | |
paul@167 | 761 | elif self.builtins is not None: |
paul@169 | 762 | attr = self.builtins.get(attrname) |
paul@285 | 763 | self.use_specific_attribute(self.builtins.full_name(), attrname) |
paul@280 | 764 | |
paul@61 | 765 | else: |
paul@169 | 766 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@169 | 767 | |
paul@167 | 768 | return attr |
paul@4 | 769 | |
paul@12 | 770 | def visitGlobal(self, node): |
paul@12 | 771 | if self.namespaces: |
paul@12 | 772 | for name in node.names: |
paul@178 | 773 | ns = self.namespaces[-1] |
paul@178 | 774 | if not ns.make_global(name): |
paul@178 | 775 | raise InspectError(ns.full_name(), node, "Name %r is global and local in %r" % (name, ns.full_name())) |
paul@37 | 776 | |
paul@236 | 777 | # The name is recorded in an earlier process. |
paul@0 | 778 | |
paul@0 | 779 | def visitIf(self, node): |
paul@272 | 780 | self.new_branchpoint() |
paul@272 | 781 | |
paul@275 | 782 | # Propagate attribute usage to branches. |
paul@275 | 783 | |
paul@0 | 784 | for test, body in node.tests: |
paul@202 | 785 | self.dispatch(test) |
paul@272 | 786 | |
paul@272 | 787 | self.new_branch() |
paul@0 | 788 | self.dispatch(body) |
paul@272 | 789 | self.shelve_branch() |
paul@272 | 790 | |
paul@275 | 791 | # Maintain a branch for the else clause or the current retained usage |
paul@275 | 792 | # where execution avoids the conditional clauses. |
paul@275 | 793 | |
paul@275 | 794 | self.new_branch() |
paul@0 | 795 | if node.else_ is not None: |
paul@0 | 796 | self.dispatch(node.else_) |
paul@275 | 797 | self.shelve_branch() |
paul@272 | 798 | |
paul@272 | 799 | self.merge_branches() |
paul@0 | 800 | return None |
paul@0 | 801 | |
paul@12 | 802 | visitIfExp = NOP |
paul@12 | 803 | |
paul@0 | 804 | def visitImport(self, node): |
paul@0 | 805 | for name, alias in node.names: |
paul@2 | 806 | if alias is not None: |
paul@167 | 807 | module = self.importer.load(name, 1) or UnresolvedName(None, name, self) |
paul@167 | 808 | self.store(alias, module) |
paul@2 | 809 | else: |
paul@167 | 810 | module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) |
paul@167 | 811 | self.store(name.split(".")[0], module) |
paul@0 | 812 | |
paul@0 | 813 | return None |
paul@0 | 814 | |
paul@172 | 815 | visitInvert = _visitUnary |
paul@4 | 816 | |
paul@59 | 817 | def visitKeyword(self, node): |
paul@59 | 818 | self.dispatch(node.expr) |
paul@168 | 819 | self.importer.make_constant(node.name) |
paul@71 | 820 | self.keyword_names.add(node.name) |
paul@59 | 821 | return None |
paul@4 | 822 | |
paul@85 | 823 | def visitLambda(self, node): |
paul@252 | 824 | self.use_name(None) # lambda functions have no names but are assumed to be invoked |
paul@85 | 825 | return self._visitFunction(node, None) |
paul@4 | 826 | |
paul@172 | 827 | visitLeftShift = _visitBinary |
paul@1 | 828 | |
paul@82 | 829 | visitList = OP |
paul@0 | 830 | |
paul@82 | 831 | visitListComp = OP |
paul@4 | 832 | |
paul@4 | 833 | visitListCompFor = NOP |
paul@4 | 834 | |
paul@4 | 835 | visitListCompIf = NOP |
paul@4 | 836 | |
paul@172 | 837 | visitMod = _visitBinary |
paul@4 | 838 | |
paul@0 | 839 | def visitModule(self, node): |
paul@143 | 840 | |
paul@143 | 841 | # Make a back reference from the node for code generation. |
paul@143 | 842 | |
paul@143 | 843 | node.unit = self |
paul@0 | 844 | return self.dispatch(node.node) |
paul@0 | 845 | |
paul@172 | 846 | visitMul = _visitBinary |
paul@2 | 847 | |
paul@0 | 848 | def visitName(self, node): |
paul@0 | 849 | name = node.name |
paul@169 | 850 | |
paul@280 | 851 | # Constants. |
paul@280 | 852 | |
paul@168 | 853 | if self.importer.predefined_constants.has_key(name): |
paul@168 | 854 | attr = self.importer.get_predefined_constant(name) |
paul@280 | 855 | |
paul@280 | 856 | # Locals. |
paul@280 | 857 | |
paul@82 | 858 | elif self.namespaces and self.namespaces[-1].has_key(name): |
paul@167 | 859 | attr = self.namespaces[-1][name] |
paul@280 | 860 | |
paul@280 | 861 | # Note usage of the local (potentially a class attribute). |
paul@280 | 862 | |
paul@285 | 863 | self.use_specific_attribute(None, name) |
paul@280 | 864 | |
paul@280 | 865 | # Globals. |
paul@280 | 866 | |
paul@13 | 867 | elif self.has_key(name): |
paul@167 | 868 | attr = self[name] |
paul@280 | 869 | |
paul@280 | 870 | # Note usage of the module attribute. |
paul@280 | 871 | |
paul@285 | 872 | self.use_specific_attribute(self.full_name(), name) |
paul@280 | 873 | |
paul@280 | 874 | # Builtins. |
paul@280 | 875 | |
paul@61 | 876 | elif self.builtins is not None and self.builtins.has_key(name): |
paul@167 | 877 | attr = self.builtins[name] |
paul@285 | 878 | self.use_specific_attribute(self.builtins.full_name(), name) |
paul@280 | 879 | |
paul@280 | 880 | # Unknown. |
paul@280 | 881 | |
paul@0 | 882 | else: |
paul@167 | 883 | attr = None |
paul@280 | 884 | self.use_name(name) |
paul@167 | 885 | |
paul@167 | 886 | return attr |
paul@0 | 887 | |
paul@82 | 888 | visitNot = OP |
paul@0 | 889 | |
paul@82 | 890 | visitOr = OP |
paul@0 | 891 | |
paul@0 | 892 | visitPass = NOP |
paul@0 | 893 | |
paul@172 | 894 | visitPower = _visitBinary |
paul@4 | 895 | |
paul@4 | 896 | visitPrint = NOP |
paul@4 | 897 | |
paul@4 | 898 | visitPrintnl = NOP |
paul@4 | 899 | |
paul@299 | 900 | visitRaise = NOP_ABANDON |
paul@0 | 901 | |
paul@299 | 902 | visitReturn = NOP_ABANDON |
paul@4 | 903 | |
paul@172 | 904 | visitRightShift = _visitBinary |
paul@1 | 905 | |
paul@82 | 906 | visitSlice = OP |
paul@4 | 907 | |
paul@82 | 908 | visitSliceobj = OP |
paul@23 | 909 | |
paul@0 | 910 | def visitStmt(self, node): |
paul@0 | 911 | for n in node.nodes: |
paul@0 | 912 | self.dispatch(n) |
paul@0 | 913 | return None |
paul@0 | 914 | |
paul@172 | 915 | visitSub = _visitBinary |
paul@4 | 916 | |
paul@216 | 917 | def visitSubscript(self, node): |
paul@250 | 918 | self.use_name("__getitem__") |
paul@216 | 919 | self.OP(node) |
paul@0 | 920 | |
paul@0 | 921 | def visitTryExcept(self, node): |
paul@0 | 922 | self.dispatch(node.body) |
paul@272 | 923 | |
paul@272 | 924 | self.new_branchpoint() |
paul@272 | 925 | |
paul@0 | 926 | for name, var, n in node.handlers: |
paul@272 | 927 | self.new_branch() |
paul@97 | 928 | |
paul@97 | 929 | # Establish the local for the handler. |
paul@97 | 930 | |
paul@99 | 931 | if var is not None: |
paul@99 | 932 | self.dispatch(var) |
paul@99 | 933 | if n is not None: |
paul@99 | 934 | self.dispatch(n) |
paul@272 | 935 | |
paul@272 | 936 | self.shelve_branch() |
paul@272 | 937 | |
paul@0 | 938 | if node.else_ is not None: |
paul@272 | 939 | self.new_branch() |
paul@0 | 940 | self.dispatch(node.else_) |
paul@272 | 941 | self.shelve_branch() |
paul@272 | 942 | |
paul@272 | 943 | self.merge_branches() |
paul@0 | 944 | return None |
paul@0 | 945 | |
paul@4 | 946 | visitTryFinally = NOP |
paul@4 | 947 | |
paul@82 | 948 | visitTuple = OP |
paul@1 | 949 | |
paul@172 | 950 | visitUnaryAdd = _visitUnary |
paul@4 | 951 | |
paul@172 | 952 | visitUnarySub = _visitUnary |
paul@4 | 953 | |
paul@34 | 954 | def visitWhile(self, node): |
paul@272 | 955 | self.new_branchpoint() |
paul@272 | 956 | |
paul@275 | 957 | # Propagate attribute usage to branches. |
paul@275 | 958 | |
paul@34 | 959 | self.in_loop = 1 |
paul@272 | 960 | self.dispatch(node.test) |
paul@272 | 961 | self.new_branch() |
paul@272 | 962 | self.dispatch(node.body) |
paul@272 | 963 | self.shelve_branch() |
paul@34 | 964 | self.in_loop = 0 |
paul@272 | 965 | |
paul@275 | 966 | # Maintain a branch for the else clause or the current retained usage |
paul@275 | 967 | # where execution avoids the conditional clauses. |
paul@275 | 968 | |
paul@275 | 969 | self.new_branch() |
paul@272 | 970 | if node.else_ is not None: |
paul@272 | 971 | self.dispatch(node.else_) |
paul@275 | 972 | self.shelve_branch() |
paul@272 | 973 | |
paul@272 | 974 | self.merge_branches() |
paul@82 | 975 | return None |
paul@4 | 976 | |
paul@4 | 977 | visitWith = NOP |
paul@4 | 978 | |
paul@4 | 979 | visitYield = NOP |
paul@4 | 980 | |
paul@0 | 981 | # vim: tabstop=4 expandtab shiftwidth=4 |