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@506 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 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@557 | 65 | assignments to provide constant values if the targets reside in class |
paul@557 | 66 | 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@331 | 70 | invoked many times with different inputs. This affects particularly the |
paul@331 | 71 | definition of functions or lambdas within functions. However, there may be |
paul@331 | 72 | benefits in considering a local to be constant within a single invocation. |
paul@0 | 73 | """ |
paul@0 | 74 | |
paul@601 | 75 | from micropython.common import ASTVisitor, operator_functions |
paul@83 | 76 | from micropython.data import * |
paul@555 | 77 | from micropython.errors import * |
paul@0 | 78 | import compiler.ast |
paul@507 | 79 | import sys |
paul@15 | 80 | |
paul@21 | 81 | # Program visitors. |
paul@21 | 82 | |
paul@15 | 83 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 84 | |
paul@20 | 85 | """ |
paul@20 | 86 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 87 | capable of being used as an AST visitor. |
paul@575 | 88 | |
paul@575 | 89 | A module can be inspected through the invocation of the following methods in |
paul@575 | 90 | order: |
paul@575 | 91 | |
paul@575 | 92 | 1. parse |
paul@581 | 93 | 2. process |
paul@581 | 94 | 3. vacuum |
paul@581 | 95 | 4. finalise |
paul@575 | 96 | |
paul@575 | 97 | A module importer can be expected to perform these invocations. |
paul@20 | 98 | """ |
paul@15 | 99 | |
paul@71 | 100 | def __init__(self, name, importer): |
paul@71 | 101 | |
paul@71 | 102 | """ |
paul@71 | 103 | Initialise this visitor with a module 'name' and an 'importer' which is |
paul@71 | 104 | used to provide access to other modules when required. |
paul@71 | 105 | """ |
paul@71 | 106 | |
paul@343 | 107 | Module.__init__(self, name, importer) |
paul@15 | 108 | self.visitor = self |
paul@15 | 109 | |
paul@91 | 110 | # Import machinery links. |
paul@91 | 111 | |
paul@343 | 112 | self.optimisations = self.importer.optimisations |
paul@61 | 113 | self.builtins = self.importer.modules.get("__builtins__") |
paul@591 | 114 | self.loaded = False |
paul@591 | 115 | self.completed = False |
paul@15 | 116 | |
paul@1 | 117 | # Current expression state. |
paul@1 | 118 | |
paul@1 | 119 | self.expr = None |
paul@591 | 120 | self.in_assignment = False # For slice and subscript handling. |
paul@1 | 121 | |
paul@1 | 122 | # Namespace state. |
paul@1 | 123 | |
paul@591 | 124 | self.in_method = False # Find instance attributes in all methods. |
paul@591 | 125 | self.in_function = False # Note function presence, affecting definitions. |
paul@591 | 126 | self.in_loop = False # Note loop "membership", affecting assignments. |
paul@12 | 127 | self.namespaces = [] |
paul@235 | 128 | self.functions = [] |
paul@0 | 129 | |
paul@0 | 130 | def parse(self, filename): |
paul@9 | 131 | |
paul@9 | 132 | "Parse the file having the given 'filename'." |
paul@9 | 133 | |
paul@581 | 134 | self.astnode = module = compiler.parseFile(filename) |
paul@581 | 135 | |
paul@581 | 136 | # Detect and record imports and globals declared in the module. |
paul@581 | 137 | |
paul@581 | 138 | self.process_structure(module) |
paul@0 | 139 | |
paul@581 | 140 | def complete(self): |
paul@581 | 141 | if not self.completed: |
paul@591 | 142 | self.completed = True |
paul@581 | 143 | self.process() |
paul@581 | 144 | if self.importer.verbose: |
paul@581 | 145 | print >>sys.stderr, "Completed import of", self.full_name() |
paul@581 | 146 | |
paul@581 | 147 | def process(self): |
paul@581 | 148 | return self.process_module(self.astnode) |
paul@581 | 149 | |
paul@581 | 150 | def process_module(self, module): |
paul@9 | 151 | |
paul@546 | 152 | """ |
paul@581 | 153 | Process the given 'module', visiting module-level code and function |
paul@581 | 154 | code. |
paul@546 | 155 | """ |
paul@9 | 156 | |
paul@439 | 157 | # Add __name__ to the namespace. |
paul@260 | 158 | |
paul@439 | 159 | self.store("__name__", self._visitConst(self.full_name())) |
paul@260 | 160 | |
paul@575 | 161 | # Visit module-level code, also recording global names. |
paul@236 | 162 | |
paul@1 | 163 | processed = self.dispatch(module) |
paul@235 | 164 | |
paul@344 | 165 | self.finalise_attribute_usage() |
paul@284 | 166 | |
paul@578 | 167 | # Visit functions. |
paul@578 | 168 | |
paul@578 | 169 | self.process_functions() |
paul@578 | 170 | |
paul@236 | 171 | # Add references to other modules declared using the __all__ global. |
paul@236 | 172 | |
paul@13 | 173 | if self.has_key("__all__"): |
paul@12 | 174 | all = self["__all__"] |
paul@1 | 175 | if isinstance(all, compiler.ast.List): |
paul@1 | 176 | for n in all.nodes: |
paul@167 | 177 | self.store(n.value, self.importer.add_module(self.name + "." + n.value)) |
paul@235 | 178 | |
paul@1 | 179 | return processed |
paul@0 | 180 | |
paul@546 | 181 | def process_functions(self): |
paul@546 | 182 | |
paul@546 | 183 | """ |
paul@546 | 184 | Process all function bodies. Deferred imports may occur during this |
paul@546 | 185 | process. |
paul@546 | 186 | """ |
paul@546 | 187 | |
paul@547 | 188 | # Then, visit each function, recording other names. This happens to |
paul@547 | 189 | # work for lambda definitions inside functions since they are added to |
paul@547 | 190 | # the end of self.functions and are thus visited as the iteration |
paul@547 | 191 | # reaches the end of the original list. |
paul@546 | 192 | |
paul@546 | 193 | for node, namespaces in self.functions: |
paul@546 | 194 | self._visitFunctionBody(node, namespaces) |
paul@546 | 195 | namespaces[-1].finalise_attribute_usage() |
paul@546 | 196 | |
paul@581 | 197 | def process_structure(self, node): |
paul@236 | 198 | |
paul@236 | 199 | """ |
paul@236 | 200 | Within the given 'node', process global declarations, adjusting the |
paul@581 | 201 | module namespace, and import statements, building a module dependency |
paul@581 | 202 | hierarchy. |
paul@236 | 203 | """ |
paul@236 | 204 | |
paul@236 | 205 | for n in node.getChildNodes(): |
paul@581 | 206 | |
paul@581 | 207 | # Module global detection. |
paul@581 | 208 | |
paul@236 | 209 | if isinstance(n, compiler.ast.Global): |
paul@236 | 210 | for name in n.names: |
paul@575 | 211 | |
paul@575 | 212 | # Each name may potentially be assigned many times. |
paul@578 | 213 | # We don't try and find out the specifics at this point and |
paul@578 | 214 | # just indicate that the name cannot be relied upon for |
paul@578 | 215 | # various observations. |
paul@575 | 216 | |
paul@581 | 217 | self.modify_name(name) |
paul@581 | 218 | |
paul@581 | 219 | # Module import declarations. |
paul@581 | 220 | |
paul@581 | 221 | elif isinstance(n, compiler.ast.From): |
paul@581 | 222 | |
paul@581 | 223 | # Load the mentioned module. |
paul@581 | 224 | |
paul@581 | 225 | self.record_import(n.modname, n) |
paul@581 | 226 | |
paul@581 | 227 | # Speculatively load modules for names beneath the module. |
paul@581 | 228 | |
paul@581 | 229 | for name, alias in n.names: |
paul@581 | 230 | modname = n.modname + "." + name |
paul@581 | 231 | self.record_import(modname, n) |
paul@581 | 232 | |
paul@581 | 233 | elif isinstance(n, compiler.ast.Import): |
paul@581 | 234 | |
paul@581 | 235 | # Load the mentioned module. |
paul@581 | 236 | |
paul@581 | 237 | for name, alias in n.names: |
paul@581 | 238 | self.record_import(name, n) |
paul@581 | 239 | |
paul@581 | 240 | # Nodes using operator module functions. |
paul@581 | 241 | |
paul@581 | 242 | elif operator_functions.has_key(n.__class__.__name__) or \ |
paul@581 | 243 | isinstance(n, (compiler.ast.AugAssign, compiler.ast.Compare)): |
paul@581 | 244 | |
paul@581 | 245 | n._module = self.importer.load("operator") |
paul@576 | 246 | |
paul@236 | 247 | else: |
paul@581 | 248 | self.process_structure(n) |
paul@581 | 249 | |
paul@581 | 250 | def get_module_paths(self, name): |
paul@581 | 251 | |
paul@581 | 252 | """ |
paul@581 | 253 | Return the paths of modules leading to the module having the given |
paul@581 | 254 | 'name'. |
paul@581 | 255 | """ |
paul@581 | 256 | |
paul@581 | 257 | names = [] |
paul@581 | 258 | parts = [] |
paul@581 | 259 | for part in name.split("."): |
paul@581 | 260 | parts.append(part) |
paul@581 | 261 | names.append(".".join(parts)) |
paul@581 | 262 | return names |
paul@581 | 263 | |
paul@581 | 264 | def record_import(self, name, node): |
paul@581 | 265 | |
paul@581 | 266 | """ |
paul@581 | 267 | Record an import of a module with the given 'name' occurring at the |
paul@581 | 268 | given 'node'. |
paul@581 | 269 | """ |
paul@581 | 270 | |
paul@581 | 271 | module = self.importer.load(name, 1, importer=node) |
paul@581 | 272 | if module and not module.loaded: |
paul@581 | 273 | self.importer.circular_imports.add(module) |
paul@581 | 274 | |
paul@581 | 275 | def complete_import(self, name, return_leaf): |
paul@581 | 276 | |
paul@581 | 277 | """ |
paul@581 | 278 | Complete the import of the module with the given 'name', returning the |
paul@581 | 279 | module itself if 'return_leaf' is a true value, or returning the root of |
paul@581 | 280 | the module hierarchy if 'return_leaf' is a false value. |
paul@581 | 281 | """ |
paul@581 | 282 | |
paul@581 | 283 | top = module = None |
paul@581 | 284 | |
paul@581 | 285 | for modname in self.get_module_paths(name): |
paul@581 | 286 | |
paul@581 | 287 | # Attempt to get the module, returning None for non-existent |
paul@581 | 288 | # modules. |
paul@581 | 289 | |
paul@581 | 290 | try: |
paul@581 | 291 | module = self.importer.get_module(modname) |
paul@581 | 292 | except KeyError: |
paul@581 | 293 | return None |
paul@581 | 294 | |
paul@581 | 295 | if module: |
paul@581 | 296 | module.complete() |
paul@581 | 297 | |
paul@581 | 298 | if top is None: |
paul@581 | 299 | top = module |
paul@581 | 300 | |
paul@581 | 301 | if return_leaf: |
paul@581 | 302 | return module |
paul@581 | 303 | else: |
paul@581 | 304 | return top |
paul@236 | 305 | |
paul@3 | 306 | def vacuum(self): |
paul@9 | 307 | |
paul@171 | 308 | """ |
paul@171 | 309 | Vacuum the module namespace, removing unreferenced objects and unused |
paul@171 | 310 | names. |
paul@171 | 311 | """ |
paul@9 | 312 | |
paul@241 | 313 | if self.should_optimise_unused_objects(): |
paul@252 | 314 | self.vacuum_object(self) |
paul@0 | 315 | |
paul@167 | 316 | all_objects = list(self.all_objects) |
paul@167 | 317 | |
paul@167 | 318 | for obj in all_objects: |
paul@351 | 319 | self.vacuum_object(obj) |
paul@171 | 320 | |
paul@252 | 321 | def vacuum_object(self, obj, delete_all=0): |
paul@241 | 322 | |
paul@252 | 323 | "Vacuum the given object 'obj'." |
paul@171 | 324 | |
paul@468 | 325 | # Get all constant objects in apparent use. |
paul@468 | 326 | |
paul@468 | 327 | if delete_all: |
paul@468 | 328 | obj_objects = set() |
paul@468 | 329 | else: |
paul@468 | 330 | obj_objects = [] |
paul@468 | 331 | for name, attr in obj.items_for_vacuum(): |
paul@468 | 332 | |
paul@468 | 333 | # Get constant objects for attributes in use. |
paul@468 | 334 | |
paul@468 | 335 | if self.importer.uses_attribute(obj.full_name(), name) and \ |
paul@468 | 336 | attr is not None and attr.is_constant(): |
paul@468 | 337 | |
paul@468 | 338 | value = attr.get_value() |
paul@468 | 339 | obj_objects.append(value) |
paul@468 | 340 | |
paul@468 | 341 | # Now vacuum unused attributes and objects not in use. |
paul@468 | 342 | |
paul@351 | 343 | for name, attr in obj.items_for_vacuum(): |
paul@270 | 344 | |
paul@270 | 345 | # Only consider deleting entire unused objects or things accessible |
paul@270 | 346 | # via names which are never used. |
paul@270 | 347 | |
paul@344 | 348 | if delete_all or not self.importer.uses_attribute(obj.full_name(), name): |
paul@351 | 349 | obj.vacuum_item(name) |
paul@270 | 350 | |
paul@270 | 351 | # Delete any unambiguous attribute value. Such values can only |
paul@270 | 352 | # have been defined within the object and therefore are not |
paul@270 | 353 | # redefined by other code regions. |
paul@270 | 354 | |
paul@468 | 355 | if attr is not None and attr.is_constant(): |
paul@252 | 356 | value = attr.get_value() |
paul@171 | 357 | |
paul@394 | 358 | # The value must have this object as a parent. |
paul@468 | 359 | # However, it must not be shared by several names. |
paul@394 | 360 | |
paul@468 | 361 | if value is not obj and value.parent is obj and \ |
paul@468 | 362 | value in self.all_objects and value not in obj_objects: |
paul@468 | 363 | |
paul@252 | 364 | self.all_objects.remove(value) |
paul@171 | 365 | |
paul@351 | 366 | # Delete class contents and lambdas from functions. |
paul@241 | 367 | |
paul@351 | 368 | self.vacuum_object(value, 1) |
paul@171 | 369 | |
paul@287 | 370 | def unfinalise(self): |
paul@287 | 371 | |
paul@287 | 372 | "Reset finalised information for the module." |
paul@287 | 373 | |
paul@287 | 374 | for obj in self.all_objects: |
paul@287 | 375 | obj.unfinalise_attributes() |
paul@287 | 376 | |
paul@437 | 377 | def finalise(self, objtable): |
paul@171 | 378 | |
paul@171 | 379 | "Finalise the module." |
paul@171 | 380 | |
paul@171 | 381 | for obj in self.all_objects: |
paul@437 | 382 | obj.finalise(objtable) |
paul@171 | 383 | |
paul@478 | 384 | self.finalise_users(objtable) |
paul@478 | 385 | |
paul@167 | 386 | def add_object(self, obj, any_scope=0): |
paul@167 | 387 | |
paul@167 | 388 | """ |
paul@167 | 389 | Record 'obj' if non-local or if the optional 'any_scope' is set to a |
paul@167 | 390 | true value. |
paul@167 | 391 | """ |
paul@167 | 392 | |
paul@167 | 393 | if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@167 | 394 | self.all_objects.add(obj) |
paul@167 | 395 | |
paul@167 | 396 | # Optimisation tests. |
paul@167 | 397 | |
paul@167 | 398 | def should_optimise_unused_objects(self): |
paul@167 | 399 | return "unused_objects" in self.optimisations |
paul@167 | 400 | |
paul@3 | 401 | # Namespace methods. |
paul@0 | 402 | |
paul@330 | 403 | def in_class(self, namespaces=None): |
paul@330 | 404 | namespaces = namespaces or self.namespaces |
paul@330 | 405 | return len(namespaces) > 1 and isinstance(namespaces[-2], Class) |
paul@330 | 406 | |
paul@0 | 407 | def store(self, name, obj): |
paul@12 | 408 | |
paul@12 | 409 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 410 | |
paul@339 | 411 | # Store in the module. |
paul@339 | 412 | |
paul@12 | 413 | if not self.namespaces: |
paul@342 | 414 | if self.in_loop and self.used_in_scope(name, "builtins"): |
paul@339 | 415 | raise InspectError("Name %r already used as a built-in." % name) |
paul@339 | 416 | else: |
paul@339 | 417 | self.set(name, obj, not self.in_loop) |
paul@339 | 418 | |
paul@339 | 419 | # Or store locally. |
paul@339 | 420 | |
paul@12 | 421 | else: |
paul@341 | 422 | locals = self.namespaces[-1] |
paul@341 | 423 | |
paul@342 | 424 | if self.in_loop and locals.used_in_scope(name, "global") and not name in locals.globals: |
paul@341 | 425 | raise InspectError("Name %r already used as global." % name) |
paul@342 | 426 | elif self.in_loop and locals.used_in_scope(name, "builtins"): |
paul@341 | 427 | raise InspectError("Name %r already used as a built-in." % name) |
paul@341 | 428 | else: |
paul@341 | 429 | locals.set(name, obj, not self.in_loop) |
paul@12 | 430 | |
paul@85 | 431 | def store_lambda(self, obj): |
paul@91 | 432 | |
paul@91 | 433 | "Store a lambda function 'obj'." |
paul@91 | 434 | |
paul@167 | 435 | self.add_object(obj) |
paul@351 | 436 | self.get_namespace().add_lambda(obj) |
paul@85 | 437 | |
paul@94 | 438 | def store_module_attr(self, name, module): |
paul@94 | 439 | |
paul@94 | 440 | """ |
paul@94 | 441 | Record module attribute 'name' in the given 'module' using the current |
paul@94 | 442 | expression. |
paul@94 | 443 | """ |
paul@94 | 444 | |
paul@192 | 445 | module.set(name, self.expr, 0) |
paul@405 | 446 | self.use_specific_attribute(module.full_name(), name) |
paul@94 | 447 | |
paul@404 | 448 | def store_class_attr(self, name, cls): |
paul@91 | 449 | |
paul@94 | 450 | """ |
paul@404 | 451 | Record class attribute 'name' in the given class 'cls' using the current |
paul@94 | 452 | expression. |
paul@94 | 453 | """ |
paul@91 | 454 | |
paul@404 | 455 | cls.set(name, self.expr, 0) |
paul@405 | 456 | self.use_specific_attribute(cls.full_name(), name) |
paul@91 | 457 | |
paul@527 | 458 | def store_instance_attr(self, name, tentative=False): |
paul@12 | 459 | |
paul@527 | 460 | """ |
paul@527 | 461 | Record instance attribute 'name' in the current class. If 'tentative' is |
paul@527 | 462 | set to a true value, the instance attribute will be discarded if a class |
paul@527 | 463 | attribute is observed. |
paul@527 | 464 | """ |
paul@12 | 465 | |
paul@91 | 466 | if self.in_method: |
paul@15 | 467 | |
paul@12 | 468 | # Current namespace is the function. |
paul@12 | 469 | # Previous namespace is the class. |
paul@15 | 470 | |
paul@405 | 471 | cls = self.namespaces[-2] |
paul@527 | 472 | cls.add_instance_attribute(name, tentative) |
paul@406 | 473 | |
paul@406 | 474 | # NOTE: The instance attribute, although defined in a specific |
paul@406 | 475 | # NOTE: class, obviously appears in all descendant classes. |
paul@406 | 476 | |
paul@405 | 477 | self.use_specific_attribute(cls.full_name(), name) |
paul@0 | 478 | |
paul@272 | 479 | def get_namespace(self): |
paul@71 | 480 | |
paul@71 | 481 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 482 | |
paul@12 | 483 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 484 | |
paul@557 | 485 | def use_name(self, name, node=None, value=None, ns=None): |
paul@323 | 486 | |
paul@323 | 487 | """ |
paul@323 | 488 | Use the given 'name' within the current namespace/unit, either in |
paul@323 | 489 | conjunction with a particular object (if 'node' is specified and not |
paul@323 | 490 | None) or unconditionally. |
paul@323 | 491 | """ |
paul@250 | 492 | |
paul@557 | 493 | unit = self.get_namespace() |
paul@557 | 494 | |
paul@557 | 495 | # Handle attribute usage situations within the current unit. |
paul@557 | 496 | |
paul@557 | 497 | if node is not None and isinstance(node, compiler.ast.Name) and ns is unit: |
paul@404 | 498 | self.use_attribute(node.name, name, value) |
paul@250 | 499 | |
paul@323 | 500 | # For general name usage, declare usage of the given name from this |
paul@323 | 501 | # particular unit. |
paul@323 | 502 | |
paul@323 | 503 | else: |
paul@404 | 504 | self.importer.use_name(name, unit.full_name(), value) |
paul@250 | 505 | |
paul@440 | 506 | def use_constant(self, const): |
paul@440 | 507 | |
paul@440 | 508 | "Use the given 'const' within the current namespace/unit." |
paul@440 | 509 | |
paul@440 | 510 | unit = self.get_namespace() |
paul@440 | 511 | self.importer.use_constant(const, unit.full_name()) |
paul@440 | 512 | |
paul@279 | 513 | # Attribute usage methods. |
paul@279 | 514 | # These are convenience methods which refer to the specific namespace's |
paul@279 | 515 | # implementation of these operations. |
paul@279 | 516 | |
paul@388 | 517 | def new_branchpoint(self, loop_node=None): |
paul@388 | 518 | self.get_namespace()._new_branchpoint(loop_node) |
paul@272 | 519 | |
paul@379 | 520 | def new_branch(self, node): |
paul@379 | 521 | self.get_namespace()._new_branch(node) |
paul@272 | 522 | |
paul@299 | 523 | def abandon_branch(self): |
paul@299 | 524 | self.get_namespace()._abandon_branch() |
paul@299 | 525 | |
paul@388 | 526 | def suspend_broken_branch(self): |
paul@388 | 527 | self.get_namespace()._suspend_broken_branch() |
paul@388 | 528 | |
paul@388 | 529 | def suspend_continuing_branch(self): |
paul@388 | 530 | self.get_namespace()._suspend_continuing_branch() |
paul@387 | 531 | |
paul@272 | 532 | def shelve_branch(self): |
paul@272 | 533 | self.get_namespace()._shelve_branch() |
paul@272 | 534 | |
paul@379 | 535 | def merge_branches(self): |
paul@379 | 536 | self.get_namespace()._merge_branches() |
paul@272 | 537 | |
paul@388 | 538 | def resume_broken_branches(self): |
paul@388 | 539 | self.get_namespace()._resume_broken_branches() |
paul@388 | 540 | |
paul@388 | 541 | def resume_continuing_branches(self): |
paul@388 | 542 | self.get_namespace()._resume_continuing_branches() |
paul@387 | 543 | |
paul@540 | 544 | def resume_abandoned_branches(self): |
paul@540 | 545 | self.get_namespace()._resume_abandoned_branches() |
paul@540 | 546 | |
paul@278 | 547 | def define_attribute_user(self, node): |
paul@279 | 548 | |
paul@279 | 549 | """ |
paul@279 | 550 | Define 'node' as the user of attributes, indicating the point where the |
paul@279 | 551 | user is defined. |
paul@279 | 552 | """ |
paul@279 | 553 | |
paul@278 | 554 | self.get_namespace()._define_attribute_user(node) |
paul@272 | 555 | |
paul@404 | 556 | def use_attribute(self, name, attrname, value=None): |
paul@323 | 557 | |
paul@404 | 558 | """ |
paul@404 | 559 | Note usage on the attribute user 'name' of the attribute 'attrname', |
paul@404 | 560 | noting an assignment if 'value' is specified. |
paul@404 | 561 | """ |
paul@323 | 562 | |
paul@404 | 563 | return self.get_namespace()._use_attribute(name, attrname, value) |
paul@272 | 564 | |
paul@434 | 565 | def use_specific_attribute(self, objname, attrname, from_name=None): |
paul@347 | 566 | |
paul@347 | 567 | """ |
paul@347 | 568 | Note usage on the object having the given 'objname' of the attribute |
paul@351 | 569 | 'attrname'. If 'objname' is None, the current namespace is chosen as the |
paul@351 | 570 | object providing the attribute. |
paul@347 | 571 | """ |
paul@347 | 572 | |
paul@434 | 573 | return self.get_namespace()._use_specific_attribute(objname, attrname, from_name) |
paul@347 | 574 | |
paul@556 | 575 | def define_attribute_accessor(self, name, attrname, node, value=None): |
paul@556 | 576 | |
paul@556 | 577 | """ |
paul@556 | 578 | Note applicable attribute users providing the given 'name' when |
paul@556 | 579 | accessing the given 'attrname' on the specified 'node', with the |
paul@556 | 580 | optional 'value' indicating an assignment. |
paul@556 | 581 | """ |
paul@556 | 582 | |
paul@556 | 583 | self.get_namespace()._define_attribute_accessor(name, attrname, node, value) |
paul@556 | 584 | |
paul@3 | 585 | # Visitor methods. |
paul@3 | 586 | |
paul@3 | 587 | def default(self, node, *args): |
paul@339 | 588 | raise InspectError("Node class %r is not supported." % node.__class__) |
paul@3 | 589 | |
paul@0 | 590 | def NOP(self, node): |
paul@21 | 591 | for n in node.getChildNodes(): |
paul@21 | 592 | self.dispatch(n) |
paul@0 | 593 | |
paul@299 | 594 | def NOP_ABANDON(self, node): |
paul@302 | 595 | self.NOP(node) |
paul@299 | 596 | self.abandon_branch() |
paul@299 | 597 | |
paul@462 | 598 | def TEST_NOP(self, node): |
paul@462 | 599 | self.use_name("__bool__", node) |
paul@462 | 600 | self.NOP(node) |
paul@462 | 601 | |
paul@82 | 602 | def OP(self, node): |
paul@82 | 603 | for n in node.getChildNodes(): |
paul@82 | 604 | self.dispatch(n) |
paul@448 | 605 | return make_instance() |
paul@1 | 606 | |
paul@462 | 607 | def TEST_OP(self, node): |
paul@462 | 608 | self.use_name("__bool__", node) |
paul@531 | 609 | self.new_branchpoint() |
paul@531 | 610 | |
paul@531 | 611 | # Propagate attribute usage to branches. |
paul@531 | 612 | # Each node starts a new conditional region, effectively making a deeply |
paul@531 | 613 | # nested collection of if-like statements. |
paul@531 | 614 | |
paul@531 | 615 | for n in node.nodes: |
paul@531 | 616 | self.new_branch(n) |
paul@531 | 617 | self.dispatch(n) |
paul@531 | 618 | |
paul@531 | 619 | # The nested regions must be terminated. |
paul@531 | 620 | |
paul@531 | 621 | for n in node.nodes: |
paul@531 | 622 | self.shelve_branch() |
paul@531 | 623 | |
paul@531 | 624 | self.merge_branches() |
paul@531 | 625 | return make_instance() |
paul@462 | 626 | |
paul@314 | 627 | # Generic support for classes of operations. |
paul@314 | 628 | |
paul@581 | 629 | def _ensureOperators(self, node): |
paul@358 | 630 | attr, scope, namespace = self._get_with_scope("$operator") |
paul@358 | 631 | if attr is None: |
paul@581 | 632 | module = node._module |
paul@581 | 633 | module.complete() |
paul@354 | 634 | self["$operator"] = module |
paul@358 | 635 | else: |
paul@358 | 636 | module = attr.get_value() |
paul@358 | 637 | return module |
paul@354 | 638 | |
paul@374 | 639 | def _visitOperator(self, node, operator_name=None): |
paul@172 | 640 | |
paul@358 | 641 | "Accounting method for the operator 'node'." |
paul@172 | 642 | |
paul@581 | 643 | operator_module = self._ensureOperators(node) |
paul@374 | 644 | operator_fn = operator_functions[operator_name or node.__class__.__name__] |
paul@354 | 645 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@172 | 646 | return self.OP(node) |
paul@172 | 647 | |
paul@372 | 648 | def _visitAttr(self, expr, attrname, node): |
paul@372 | 649 | |
paul@468 | 650 | """ |
paul@468 | 651 | Process the attribute provided by the given 'expr' with the given |
paul@468 | 652 | 'attrname' and involving the given 'node'. |
paul@468 | 653 | """ |
paul@468 | 654 | |
paul@372 | 655 | # Attempt to identify the nature of the attribute. |
paul@372 | 656 | |
paul@372 | 657 | if isinstance(expr, Attr): |
paul@372 | 658 | value = expr.get_value() |
paul@372 | 659 | |
paul@372 | 660 | # Get the attribute and record its usage. |
paul@559 | 661 | # NOTE: Need to provide concrete values for things like base classes |
paul@559 | 662 | # NOTE: while also handling module attribute modification. |
paul@372 | 663 | |
paul@599 | 664 | # Only specific class attributes are detected here since class |
paul@599 | 665 | # attribute finalisation has not yet occurred. |
paul@599 | 666 | |
paul@559 | 667 | if isinstance(value, (Class, Module)): |
paul@401 | 668 | |
paul@401 | 669 | # Check for class.__class__. |
paul@401 | 670 | |
paul@401 | 671 | if attrname == "__class__" and isinstance(value, Class): |
paul@401 | 672 | attr = type_class |
paul@401 | 673 | else: |
paul@448 | 674 | attr = value.get(attrname) or make_instance() |
paul@372 | 675 | self.use_specific_attribute(value.full_name(), attrname) |
paul@372 | 676 | |
paul@372 | 677 | elif isinstance(value, UnresolvedName): |
paul@372 | 678 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@372 | 679 | |
paul@467 | 680 | # The actual attribute is not readily identifiable and is assumed |
paul@467 | 681 | # to be an instance. |
paul@467 | 682 | |
paul@372 | 683 | else: |
paul@527 | 684 | |
paul@527 | 685 | # Record any instance attributes. |
paul@527 | 686 | |
paul@527 | 687 | if expr.name == "self": |
paul@527 | 688 | self.store_instance_attr(attrname, tentative=True) |
paul@527 | 689 | |
paul@448 | 690 | attr = make_instance() |
paul@372 | 691 | |
paul@372 | 692 | # Note usage of the attribute where a local is involved. |
paul@372 | 693 | |
paul@404 | 694 | self._visitAttrUser(expr, attrname, node) |
paul@372 | 695 | |
paul@467 | 696 | # No particular attribute has been identified, thus a general instance |
paul@467 | 697 | # is assumed. |
paul@467 | 698 | |
paul@372 | 699 | else: |
paul@448 | 700 | attr = make_instance() |
paul@372 | 701 | self.use_name(attrname, node) |
paul@372 | 702 | |
paul@372 | 703 | return attr |
paul@358 | 704 | |
paul@404 | 705 | def _visitAttrUser(self, expr, attrname, node, value=None): |
paul@404 | 706 | |
paul@404 | 707 | """ |
paul@404 | 708 | Note usage of the attribute provided by 'expr' with the given 'attrname' |
paul@404 | 709 | where a local is involved, annotating the given 'node'. If the optional |
paul@404 | 710 | 'value' is given, note an assignment for future effects on attributes |
paul@404 | 711 | where such attributes are inferred from the usage. |
paul@404 | 712 | """ |
paul@404 | 713 | |
paul@571 | 714 | # Access to attributes via a local in functions, classes or modules. |
paul@571 | 715 | # Since module-level locals are globals that can be modified |
paul@571 | 716 | # independently of the namespace, any attribute usage observations made |
paul@571 | 717 | # here may be revoked later if such modification is thought to occur. |
paul@467 | 718 | |
paul@571 | 719 | if expr.parent is self.get_namespace(): |
paul@556 | 720 | self.define_attribute_accessor(expr.name, attrname, node, value) |
paul@404 | 721 | else: |
paul@557 | 722 | self.use_name(attrname, node.expr, value, ns=expr.parent) |
paul@404 | 723 | |
paul@439 | 724 | def _visitConst(self, value): |
paul@439 | 725 | |
paul@439 | 726 | """ |
paul@439 | 727 | Register the constant given by 'value', if necessary, returning the |
paul@439 | 728 | resulting object. The type name is noted as being used, thus preserving |
paul@439 | 729 | the class in any generated program. |
paul@439 | 730 | """ |
paul@439 | 731 | |
paul@439 | 732 | self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value)) |
paul@440 | 733 | const = self.importer.make_constant(value) |
paul@440 | 734 | self.use_constant(const) |
paul@440 | 735 | return const |
paul@439 | 736 | |
paul@85 | 737 | def _visitFunction(self, node, name): |
paul@85 | 738 | |
paul@85 | 739 | """ |
paul@85 | 740 | Return a function object for the function defined by 'node' with the |
paul@85 | 741 | given 'name'. If a lambda expression is being visited, 'name' should be |
paul@85 | 742 | None. |
paul@85 | 743 | """ |
paul@85 | 744 | |
paul@233 | 745 | # Define the function object. |
paul@233 | 746 | |
paul@85 | 747 | function = Function( |
paul@85 | 748 | name, |
paul@272 | 749 | self.get_namespace(), |
paul@85 | 750 | node.argnames, |
paul@85 | 751 | node.defaults, |
paul@85 | 752 | (node.flags & 4 != 0), |
paul@85 | 753 | (node.flags & 8 != 0), |
paul@331 | 754 | self.in_loop or self.in_function, |
paul@85 | 755 | self, |
paul@85 | 756 | node |
paul@85 | 757 | ) |
paul@85 | 758 | |
paul@233 | 759 | self.add_object(function, any_scope=1) |
paul@233 | 760 | |
paul@85 | 761 | # Make a back reference from the node for code generation. |
paul@85 | 762 | |
paul@85 | 763 | node.unit = function |
paul@85 | 764 | |
paul@85 | 765 | # Process the defaults. |
paul@85 | 766 | |
paul@85 | 767 | for n in node.defaults: |
paul@85 | 768 | self.expr = self.dispatch(n) |
paul@192 | 769 | function.store_default(self.expr) |
paul@85 | 770 | |
paul@368 | 771 | # Note attribute usage where tuple parameters are involved. |
paul@368 | 772 | |
paul@368 | 773 | if function.tuple_parameters(): |
paul@368 | 774 | self.use_name("__getitem__", node) |
paul@368 | 775 | |
paul@368 | 776 | # Record the namespace context of the function for later processing. |
paul@368 | 777 | |
paul@235 | 778 | self.functions.append((node, self.namespaces + [function])) |
paul@85 | 779 | |
paul@330 | 780 | # Store the function. |
paul@330 | 781 | |
paul@85 | 782 | if name is not None: |
paul@85 | 783 | self.store(name, function) |
paul@85 | 784 | else: |
paul@85 | 785 | self.store_lambda(function) |
paul@167 | 786 | |
paul@330 | 787 | # Test the defaults and assess whether an dynamic object will result. |
paul@330 | 788 | |
paul@331 | 789 | function.make_dynamic() |
paul@331 | 790 | return function |
paul@85 | 791 | |
paul@235 | 792 | def _visitFunctionBody(self, node, namespaces): |
paul@235 | 793 | |
paul@235 | 794 | "Enter the function." |
paul@235 | 795 | |
paul@235 | 796 | # Current namespace is the function. |
paul@235 | 797 | # Previous namespace is the class. |
paul@235 | 798 | |
paul@330 | 799 | if self.in_class(namespaces): |
paul@591 | 800 | self.in_method = True |
paul@235 | 801 | |
paul@331 | 802 | in_function = self.in_function |
paul@331 | 803 | in_loop = self.in_loop |
paul@591 | 804 | self.in_function = True |
paul@591 | 805 | self.in_loop = False |
paul@331 | 806 | |
paul@235 | 807 | self.namespaces = namespaces |
paul@235 | 808 | self.dispatch(node.code) |
paul@235 | 809 | |
paul@331 | 810 | self.in_loop = in_loop |
paul@331 | 811 | self.in_function = in_function |
paul@591 | 812 | self.in_method = False |
paul@235 | 813 | |
paul@172 | 814 | # Specific handler methods. |
paul@172 | 815 | |
paul@580 | 816 | visitAdd = _visitOperator |
paul@82 | 817 | |
paul@462 | 818 | visitAnd = TEST_OP |
paul@0 | 819 | |
paul@4 | 820 | visitAssert = NOP |
paul@4 | 821 | |
paul@0 | 822 | def visitAssign(self, node): |
paul@0 | 823 | self.expr = self.dispatch(node.expr) |
paul@591 | 824 | self.in_assignment = True |
paul@0 | 825 | for n in node.nodes: |
paul@0 | 826 | self.dispatch(n) |
paul@591 | 827 | self.in_assignment = False |
paul@0 | 828 | |
paul@0 | 829 | def visitAssAttr(self, node): |
paul@0 | 830 | expr = self.dispatch(node.expr) |
paul@404 | 831 | attrname = node.attrname |
paul@272 | 832 | |
paul@272 | 833 | # Record the attribute on the presumed target. |
paul@272 | 834 | |
paul@94 | 835 | if isinstance(expr, Attr): |
paul@404 | 836 | value = expr.get_value() |
paul@404 | 837 | |
paul@94 | 838 | if expr.name == "self": |
paul@404 | 839 | self.store_instance_attr(attrname) |
paul@556 | 840 | self.use_attribute(expr.name, attrname, value) |
paul@488 | 841 | self._visitAttrUser(expr, attrname, node, self.expr) |
paul@404 | 842 | |
paul@602 | 843 | # No definite attribute can be identified, since the instance |
paul@602 | 844 | # being accessed may be a subclass of the method's class. |
paul@602 | 845 | |
paul@602 | 846 | attr = make_instance() |
paul@602 | 847 | |
paul@404 | 848 | elif isinstance(value, Module): |
paul@404 | 849 | self.store_module_attr(attrname, value) |
paul@602 | 850 | attr = value.get(attrname) |
paul@272 | 851 | |
paul@404 | 852 | elif isinstance(value, Class): |
paul@404 | 853 | self.store_class_attr(attrname, value) |
paul@602 | 854 | attr = value.get(attrname) |
paul@404 | 855 | |
paul@285 | 856 | # Note usage of the attribute where a local is involved. |
paul@272 | 857 | |
paul@404 | 858 | else: |
paul@404 | 859 | self._visitAttrUser(expr, attrname, node, self.expr) |
paul@602 | 860 | attr = make_instance() |
paul@381 | 861 | |
paul@404 | 862 | else: |
paul@404 | 863 | self.use_name(attrname, node) |
paul@602 | 864 | attr = make_instance() |
paul@602 | 865 | |
paul@602 | 866 | node._attr = attr |
paul@272 | 867 | |
paul@0 | 868 | def visitAssList(self, node): |
paul@204 | 869 | |
paul@204 | 870 | # Declare names which will be used by generated code. |
paul@204 | 871 | |
paul@323 | 872 | self.use_name("__getitem__", node) |
paul@204 | 873 | |
paul@204 | 874 | # Process the assignment. |
paul@204 | 875 | |
paul@139 | 876 | for i, n in enumerate(node.nodes): |
paul@0 | 877 | self.dispatch(n) |
paul@439 | 878 | self._visitConst(i) # for __getitem__(i) at run-time |
paul@0 | 879 | |
paul@0 | 880 | def visitAssName(self, node): |
paul@599 | 881 | if node.flags == "OP_DELETE": |
paul@507 | 882 | print >>sys.stderr, "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) |
paul@339 | 883 | #raise InspectError("Deletion of attribute %r is not supported." % node.name) |
paul@599 | 884 | self._visitAssName(node) |
paul@274 | 885 | |
paul@599 | 886 | def _visitAssName(self, node): |
paul@120 | 887 | self.store(node.name, self.expr) |
paul@278 | 888 | self.define_attribute_user(node) |
paul@285 | 889 | |
paul@285 | 890 | # Ensure the presence of the given name in this namespace. |
paul@431 | 891 | # NOTE: Consider not registering assignments involving methods, since |
paul@431 | 892 | # NOTE: this is merely creating aliases for such methods. |
paul@285 | 893 | |
paul@434 | 894 | if isinstance(self.get_namespace(), (Class, Module)): |
paul@434 | 895 | if not isinstance(self.expr, Attr) or not isinstance(self.expr.get_value(), Function): |
paul@434 | 896 | self.use_specific_attribute(None, node.name) |
paul@434 | 897 | else: |
paul@434 | 898 | fn = self.expr.get_value() |
paul@434 | 899 | ns = self.get_namespace().full_name() |
paul@434 | 900 | self.use_specific_attribute(fn.parent.full_name(), fn.name, "%s.%s" % (ns, node.name)) |
paul@434 | 901 | |
paul@0 | 902 | visitAssTuple = visitAssList |
paul@0 | 903 | |
paul@172 | 904 | def visitAugAssign(self, node): |
paul@172 | 905 | |
paul@172 | 906 | # Accounting. |
paul@172 | 907 | |
paul@354 | 908 | operator_fn = operator_functions.get(node.op) |
paul@581 | 909 | operator_module = self._ensureOperators(node) |
paul@354 | 910 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@244 | 911 | |
paul@244 | 912 | # Process the assignment. |
paul@244 | 913 | |
paul@244 | 914 | self.expr = self.dispatch(node.expr) |
paul@244 | 915 | |
paul@244 | 916 | # NOTE: Similar to micropython.ast handler code. |
paul@363 | 917 | # NOTE: Slices and subscripts are supported by __setitem__(slice) and |
paul@363 | 918 | # NOTE: not __setslice__. |
paul@244 | 919 | |
paul@244 | 920 | if isinstance(node.node, compiler.ast.Name): |
paul@599 | 921 | self._visitAssName(node.node) |
paul@244 | 922 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@244 | 923 | self.visitAssAttr(node.node) |
paul@244 | 924 | else: |
paul@594 | 925 | self.dispatch(node.node) |
paul@363 | 926 | self.use_specific_attribute("__builtins__", "slice") |
paul@363 | 927 | self.use_name("__setitem__", node) |
paul@244 | 928 | |
paul@82 | 929 | visitBackquote = OP |
paul@4 | 930 | |
paul@580 | 931 | visitBitand = _visitOperator |
paul@2 | 932 | |
paul@580 | 933 | visitBitor = _visitOperator |
paul@2 | 934 | |
paul@580 | 935 | visitBitxor = _visitOperator |
paul@4 | 936 | |
paul@388 | 937 | def visitBreak(self, node): |
paul@388 | 938 | self.NOP(node) |
paul@388 | 939 | self.suspend_broken_branch() |
paul@4 | 940 | |
paul@82 | 941 | visitCallFunc = OP |
paul@0 | 942 | |
paul@0 | 943 | def visitClass(self, node): |
paul@91 | 944 | |
paul@91 | 945 | """ |
paul@91 | 946 | Register the class at the given 'node' subject to the restrictions |
paul@91 | 947 | mentioned in the module docstring. |
paul@91 | 948 | """ |
paul@91 | 949 | |
paul@12 | 950 | if self.namespaces: |
paul@507 | 951 | print >>sys.stderr, "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@442 | 952 | return |
paul@0 | 953 | else: |
paul@331 | 954 | if self.in_loop: |
paul@507 | 955 | print >>sys.stderr, "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) |
paul@331 | 956 | |
paul@394 | 957 | cls = get_class(node.name, self.get_namespace(), self, node) |
paul@37 | 958 | |
paul@349 | 959 | # Make a back reference from the node for code generation. |
paul@349 | 960 | |
paul@349 | 961 | node.unit = cls |
paul@349 | 962 | |
paul@349 | 963 | # Process base classes in the context of the class's namespace. |
paul@349 | 964 | # This confines references to such classes to the class instead of |
paul@349 | 965 | # the namespace in which it is defined. |
paul@349 | 966 | |
paul@349 | 967 | self.namespaces.append(cls) |
paul@349 | 968 | |
paul@37 | 969 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 970 | # definitions of classes. |
paul@37 | 971 | |
paul@0 | 972 | for base in node.bases: |
paul@36 | 973 | expr = self.dispatch(base) |
paul@349 | 974 | |
paul@349 | 975 | # Each base class must be constant and known at compile-time. |
paul@349 | 976 | |
paul@36 | 977 | if isinstance(expr, Attr): |
paul@36 | 978 | if expr.assignments != 1: |
paul@442 | 979 | raise InspectError("Base class %r for %r is not constant: %r" % (base, cls.full_name(), expr)) |
paul@541 | 980 | elif not isinstance(expr.get_value(), Class): |
paul@541 | 981 | raise InspectError("Base class %r for %r is not a class: %r" % (base, cls.full_name(), expr.get_value())) |
paul@36 | 982 | else: |
paul@192 | 983 | cls.add_base(expr.get_value()) |
paul@349 | 984 | |
paul@349 | 985 | # Where no expression value is available, the base class is |
paul@349 | 986 | # not identifiable. |
paul@349 | 987 | |
paul@349 | 988 | else: |
paul@339 | 989 | raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 990 | |
paul@59 | 991 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 992 | # If no bases exist, adopt the 'object' class. |
paul@59 | 993 | |
paul@580 | 994 | if not node.bases and not (self.name == "__builtins__" and node.name == "object"): |
paul@59 | 995 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@192 | 996 | cls.add_base(expr.get_value()) |
paul@59 | 997 | |
paul@349 | 998 | # Make an entry for the class in the parent namespace. |
paul@21 | 999 | |
paul@349 | 1000 | self.namespaces.pop() |
paul@0 | 1001 | self.store(node.name, cls) |
paul@167 | 1002 | self.add_object(cls) |
paul@0 | 1003 | |
paul@349 | 1004 | # Process the class body in its own namespace. |
paul@439 | 1005 | # Add __name__ to the namespace. |
paul@59 | 1006 | |
paul@12 | 1007 | self.namespaces.append(cls) |
paul@439 | 1008 | self.store("__name__", self._visitConst(node.name)) |
paul@0 | 1009 | self.dispatch(node.code) |
paul@12 | 1010 | self.namespaces.pop() |
paul@0 | 1011 | |
paul@349 | 1012 | cls.finalise_attribute_usage() |
paul@82 | 1013 | return cls |
paul@0 | 1014 | |
paul@172 | 1015 | def visitCompare(self, node): |
paul@172 | 1016 | |
paul@172 | 1017 | # Accounting. |
paul@172 | 1018 | # NOTE: Replicates some code in micropython.ast.visitCompare. |
paul@172 | 1019 | |
paul@462 | 1020 | self.use_name("__bool__", node) |
paul@462 | 1021 | |
paul@323 | 1022 | this_node = node |
paul@323 | 1023 | |
paul@172 | 1024 | for op in node.ops: |
paul@172 | 1025 | op_name, next_node = op |
paul@323 | 1026 | |
paul@354 | 1027 | # Define name/attribute usage. |
paul@354 | 1028 | # Get the applicable operation. |
paul@323 | 1029 | |
paul@354 | 1030 | operator_fn = operator_functions.get(op_name) |
paul@354 | 1031 | |
paul@354 | 1032 | # For operators, reference the specific function involved. |
paul@323 | 1033 | |
paul@354 | 1034 | if operator_fn is not None: |
paul@581 | 1035 | operator_module = self._ensureOperators(node) |
paul@354 | 1036 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@323 | 1037 | |
paul@354 | 1038 | # Define __contains__ usage on the next node. |
paul@354 | 1039 | |
paul@172 | 1040 | elif op_name.endswith("in"): |
paul@323 | 1041 | self.use_name("__contains__", next_node) |
paul@323 | 1042 | |
paul@323 | 1043 | this_node = next_node |
paul@172 | 1044 | |
paul@172 | 1045 | return self.OP(node) |
paul@4 | 1046 | |
paul@21 | 1047 | def visitConst(self, node): |
paul@439 | 1048 | return self._visitConst(node.value) |
paul@0 | 1049 | |
paul@388 | 1050 | def visitContinue(self, node): |
paul@388 | 1051 | self.NOP(node) |
paul@388 | 1052 | self.suspend_continuing_branch() |
paul@4 | 1053 | |
paul@4 | 1054 | visitDecorators = NOP |
paul@4 | 1055 | |
paul@82 | 1056 | visitDict = OP |
paul@0 | 1057 | |
paul@0 | 1058 | visitDiscard = NOP |
paul@0 | 1059 | |
paul@580 | 1060 | visitDiv = _visitOperator |
paul@4 | 1061 | |
paul@4 | 1062 | visitEllipsis = NOP |
paul@4 | 1063 | |
paul@4 | 1064 | visitExec = NOP |
paul@4 | 1065 | |
paul@82 | 1066 | visitExpression = OP |
paul@4 | 1067 | |
paul@580 | 1068 | visitFloorDiv = _visitOperator |
paul@4 | 1069 | |
paul@34 | 1070 | def visitFor(self, node): |
paul@388 | 1071 | self.new_branchpoint(node) |
paul@204 | 1072 | |
paul@204 | 1073 | # Declare names which will be used by generated code. |
paul@204 | 1074 | |
paul@323 | 1075 | self.use_name("__iter__", node.list) |
paul@250 | 1076 | self.use_name("next") |
paul@345 | 1077 | self.use_name("StopIteration") |
paul@204 | 1078 | |
paul@331 | 1079 | in_loop = self.in_loop |
paul@591 | 1080 | self.in_loop = True |
paul@341 | 1081 | self.dispatch(node.list) |
paul@442 | 1082 | |
paul@442 | 1083 | # NOTE: Could generate AST nodes for the actual operations instead of |
paul@442 | 1084 | # NOTE: manually generating code in micropython.ast. |
paul@442 | 1085 | |
paul@448 | 1086 | self.expr = make_instance() # each element is a result of a function call |
paul@272 | 1087 | self.dispatch(node.assign) |
paul@272 | 1088 | |
paul@204 | 1089 | # Enter the loop. |
paul@275 | 1090 | # Propagate attribute usage to branches. |
paul@204 | 1091 | |
paul@379 | 1092 | self.new_branch(node) |
paul@272 | 1093 | self.dispatch(node.body) |
paul@385 | 1094 | |
paul@388 | 1095 | self.resume_continuing_branches() |
paul@385 | 1096 | |
paul@272 | 1097 | self.shelve_branch() |
paul@362 | 1098 | |
paul@331 | 1099 | self.in_loop = in_loop |
paul@272 | 1100 | |
paul@386 | 1101 | # A null branch is used to record a path around the loop. |
paul@275 | 1102 | |
paul@379 | 1103 | self.new_branch(node.else_ or NullBranch()) |
paul@379 | 1104 | self.shelve_branch() |
paul@272 | 1105 | |
paul@379 | 1106 | self.merge_branches() |
paul@386 | 1107 | |
paul@386 | 1108 | # The else clause is evaluated outside any branch. |
paul@386 | 1109 | |
paul@386 | 1110 | if node.else_ is not None: |
paul@386 | 1111 | self.dispatch(node.else_) |
paul@386 | 1112 | |
paul@387 | 1113 | # Any suspended branches from the loop can now be resumed. |
paul@387 | 1114 | |
paul@388 | 1115 | self.resume_broken_branches() |
paul@387 | 1116 | |
paul@0 | 1117 | def visitFrom(self, node): |
paul@581 | 1118 | module = self.complete_import(node.modname, True) |
paul@0 | 1119 | |
paul@0 | 1120 | for name, alias in node.names: |
paul@563 | 1121 | |
paul@563 | 1122 | # For specific names, obtain and store referenced objects using |
paul@563 | 1123 | # the name or any alias provided in the current namespace. |
paul@563 | 1124 | |
paul@0 | 1125 | if name != "*": |
paul@545 | 1126 | if module: |
paul@501 | 1127 | |
paul@501 | 1128 | # Missing names may refer to submodules. |
paul@501 | 1129 | |
paul@581 | 1130 | submodule = self.complete_import(node.modname + "." + name, True) |
paul@581 | 1131 | if submodule: |
paul@581 | 1132 | if not module.has_key(name): |
paul@501 | 1133 | module.store(name, submodule) |
paul@501 | 1134 | |
paul@501 | 1135 | # Complete the import if the name was found. |
paul@501 | 1136 | |
paul@501 | 1137 | if module.has_key(name): |
paul@501 | 1138 | attr = module[name] |
paul@501 | 1139 | self.store(alias or name, attr) |
paul@501 | 1140 | self.use_specific_attribute(module.full_name(), name) |
paul@501 | 1141 | continue |
paul@24 | 1142 | |
paul@24 | 1143 | # Support the import of names from missing modules. |
paul@24 | 1144 | |
paul@501 | 1145 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@501 | 1146 | |
paul@563 | 1147 | # For wildcards, obtain and store all objects from a module in the |
paul@563 | 1148 | # current namespace. |
paul@563 | 1149 | |
paul@3 | 1150 | else: |
paul@545 | 1151 | if module: |
paul@285 | 1152 | for n in module.keys(): |
paul@24 | 1153 | attr = module[n] |
paul@192 | 1154 | self.store(n, attr) |
paul@364 | 1155 | self.use_specific_attribute(module.full_name(), n) |
paul@0 | 1156 | |
paul@0 | 1157 | def visitFunction(self, node): |
paul@85 | 1158 | return self._visitFunction(node, node.name) |
paul@0 | 1159 | |
paul@82 | 1160 | visitGenExpr = OP |
paul@4 | 1161 | |
paul@4 | 1162 | visitGenExprFor = NOP |
paul@4 | 1163 | |
paul@4 | 1164 | visitGenExprIf = NOP |
paul@4 | 1165 | |
paul@4 | 1166 | visitGenExprInner = NOP |
paul@4 | 1167 | |
paul@0 | 1168 | def visitGetattr(self, node): |
paul@602 | 1169 | node._expr = self.dispatch(node.expr) |
paul@602 | 1170 | node._attr = self._visitAttr(node._expr, node.attrname, node) |
paul@556 | 1171 | return node._attr |
paul@4 | 1172 | |
paul@12 | 1173 | def visitGlobal(self, node): |
paul@12 | 1174 | if self.namespaces: |
paul@12 | 1175 | for name in node.names: |
paul@178 | 1176 | ns = self.namespaces[-1] |
paul@178 | 1177 | if not ns.make_global(name): |
paul@339 | 1178 | raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) |
paul@37 | 1179 | |
paul@236 | 1180 | # The name is recorded in an earlier process. |
paul@0 | 1181 | |
paul@0 | 1182 | def visitIf(self, node): |
paul@462 | 1183 | self.use_name("__bool__", node) |
paul@272 | 1184 | self.new_branchpoint() |
paul@272 | 1185 | |
paul@275 | 1186 | # Propagate attribute usage to branches. |
paul@275 | 1187 | |
paul@0 | 1188 | for test, body in node.tests: |
paul@202 | 1189 | self.dispatch(test) |
paul@272 | 1190 | |
paul@377 | 1191 | self.new_branch(body) |
paul@0 | 1192 | self.dispatch(body) |
paul@272 | 1193 | self.shelve_branch() |
paul@272 | 1194 | |
paul@377 | 1195 | # Maintain a branch for the else clause. |
paul@275 | 1196 | |
paul@379 | 1197 | self.new_branch(node.else_ or NullBranch()) |
paul@0 | 1198 | if node.else_ is not None: |
paul@0 | 1199 | self.dispatch(node.else_) |
paul@379 | 1200 | self.shelve_branch() |
paul@272 | 1201 | |
paul@272 | 1202 | self.merge_branches() |
paul@442 | 1203 | |
paul@442 | 1204 | def visitIfExp(self, node): |
paul@462 | 1205 | self.use_name("__bool__", node) |
paul@442 | 1206 | self.new_branchpoint() |
paul@442 | 1207 | |
paul@442 | 1208 | # Propagate attribute usage to branches. |
paul@442 | 1209 | |
paul@442 | 1210 | self.dispatch(node.test) |
paul@0 | 1211 | |
paul@442 | 1212 | self.new_branch(node.then) |
paul@442 | 1213 | self.dispatch(node.then) |
paul@442 | 1214 | self.shelve_branch() |
paul@442 | 1215 | |
paul@442 | 1216 | self.new_branch(node.else_) |
paul@442 | 1217 | self.dispatch(node.else_) |
paul@442 | 1218 | self.shelve_branch() |
paul@442 | 1219 | |
paul@442 | 1220 | self.merge_branches() |
paul@448 | 1221 | return make_instance() # either outcome is possible |
paul@12 | 1222 | |
paul@0 | 1223 | def visitImport(self, node): |
paul@0 | 1224 | for name, alias in node.names: |
paul@581 | 1225 | module = self.complete_import(name, alias) |
paul@2 | 1226 | if alias is not None: |
paul@581 | 1227 | self.store(alias, module or UnresolvedName(None, name, self)) |
paul@2 | 1228 | else: |
paul@581 | 1229 | name_used = name.split(".")[0] |
paul@581 | 1230 | self.store(name_used, module or UnresolvedName(None, name_used, self)) |
paul@578 | 1231 | |
paul@580 | 1232 | visitInvert = _visitOperator |
paul@4 | 1233 | |
paul@59 | 1234 | def visitKeyword(self, node): |
paul@59 | 1235 | self.dispatch(node.expr) |
paul@439 | 1236 | self._visitConst(node.name) |
paul@71 | 1237 | self.keyword_names.add(node.name) |
paul@4 | 1238 | |
paul@85 | 1239 | def visitLambda(self, node): |
paul@351 | 1240 | fn = self._visitFunction(node, None) |
paul@351 | 1241 | self.use_specific_attribute(None, fn.name) |
paul@351 | 1242 | return fn |
paul@4 | 1243 | |
paul@580 | 1244 | visitLeftShift = _visitOperator |
paul@1 | 1245 | |
paul@346 | 1246 | def visitList(self, node): |
paul@347 | 1247 | self.use_specific_attribute("__builtins__", "list") |
paul@442 | 1248 | return self.OP(node) |
paul@0 | 1249 | |
paul@341 | 1250 | def visitListComp(self, node): |
paul@506 | 1251 | |
paul@506 | 1252 | # Note that explicit dispatch is performed. |
paul@506 | 1253 | |
paul@506 | 1254 | if node.quals: |
paul@506 | 1255 | self.visitListCompFor(node.quals[0], node.quals[1:], node.expr) |
paul@448 | 1256 | return make_instance() |
paul@341 | 1257 | |
paul@506 | 1258 | def visitListCompFor(self, node, following_quals, expr): |
paul@341 | 1259 | self.new_branchpoint() |
paul@341 | 1260 | |
paul@341 | 1261 | # Declare names which will be used by generated code. |
paul@341 | 1262 | |
paul@341 | 1263 | self.use_name("__iter__", node.list) |
paul@341 | 1264 | self.use_name("next") |
paul@4 | 1265 | |
paul@341 | 1266 | in_loop = self.in_loop |
paul@591 | 1267 | self.in_loop = True |
paul@341 | 1268 | self.dispatch(node.list) |
paul@442 | 1269 | |
paul@442 | 1270 | # NOTE: Could generate AST nodes for the actual operations instead of |
paul@442 | 1271 | # NOTE: manually generating code in micropython.ast. |
paul@442 | 1272 | |
paul@448 | 1273 | self.expr = make_instance() # each element is a result of a function call |
paul@341 | 1274 | self.dispatch(node.assign) |
paul@341 | 1275 | |
paul@341 | 1276 | # Enter the loop. |
paul@341 | 1277 | # Propagate attribute usage to branches. |
paul@341 | 1278 | |
paul@379 | 1279 | self.new_branch(node) |
paul@341 | 1280 | |
paul@506 | 1281 | # Note that explicit dispatch is performed. |
paul@506 | 1282 | |
paul@506 | 1283 | if node.ifs: |
paul@506 | 1284 | self.visitListCompIf(node.ifs[0], node.ifs[1:], following_quals, expr) |
paul@506 | 1285 | elif following_quals: |
paul@506 | 1286 | self.visitListCompFor(following_quals[0], following_quals[1:], expr) |
paul@506 | 1287 | else: |
paul@506 | 1288 | self.dispatch(expr) |
paul@341 | 1289 | |
paul@341 | 1290 | self.shelve_branch() |
paul@341 | 1291 | self.in_loop = in_loop |
paul@341 | 1292 | |
paul@379 | 1293 | self.merge_branches() |
paul@4 | 1294 | |
paul@506 | 1295 | def visitListCompIf(self, node, following_ifs, following_quals, expr): |
paul@506 | 1296 | self.use_name("__bool__", node) |
paul@506 | 1297 | self.new_branchpoint() |
paul@506 | 1298 | |
paul@506 | 1299 | # Propagate attribute usage to branches. |
paul@506 | 1300 | |
paul@506 | 1301 | self.dispatch(node.test) |
paul@506 | 1302 | |
paul@506 | 1303 | # Note that explicit dispatch is performed. |
paul@506 | 1304 | |
paul@506 | 1305 | if following_ifs: |
paul@506 | 1306 | self.visitListCompIf(following_ifs[0], following_ifs[1:], following_quals, expr) |
paul@506 | 1307 | elif following_quals: |
paul@506 | 1308 | self.visitListCompFor(following_quals[0], following_quals[1:], expr) |
paul@506 | 1309 | else: |
paul@506 | 1310 | self.new_branch(expr) |
paul@506 | 1311 | self.dispatch(expr) |
paul@506 | 1312 | self.shelve_branch() |
paul@506 | 1313 | |
paul@506 | 1314 | # Maintain a branch for the else clause. |
paul@506 | 1315 | |
paul@506 | 1316 | self.new_branch(NullBranch()) |
paul@506 | 1317 | self.shelve_branch() |
paul@506 | 1318 | |
paul@506 | 1319 | self.merge_branches() |
paul@4 | 1320 | |
paul@580 | 1321 | visitMod = _visitOperator |
paul@4 | 1322 | |
paul@0 | 1323 | def visitModule(self, node): |
paul@143 | 1324 | |
paul@143 | 1325 | # Make a back reference from the node for code generation. |
paul@143 | 1326 | |
paul@143 | 1327 | node.unit = self |
paul@0 | 1328 | return self.dispatch(node.node) |
paul@0 | 1329 | |
paul@580 | 1330 | visitMul = _visitOperator |
paul@2 | 1331 | |
paul@0 | 1332 | def visitName(self, node): |
paul@583 | 1333 | attr = self.get_namespace().get_using_node(node.name, node) or make_instance() |
paul@583 | 1334 | node._attr = attr |
paul@583 | 1335 | return attr |
paul@0 | 1336 | |
paul@531 | 1337 | def visitNot(self, node): |
paul@531 | 1338 | self.use_name("__bool__", node) |
paul@531 | 1339 | self.dispatch(node.expr) |
paul@531 | 1340 | return make_instance() |
paul@0 | 1341 | |
paul@462 | 1342 | visitOr = TEST_OP |
paul@0 | 1343 | |
paul@0 | 1344 | visitPass = NOP |
paul@0 | 1345 | |
paul@580 | 1346 | visitPower = _visitOperator |
paul@4 | 1347 | |
paul@393 | 1348 | def _visitPrint(self, node, function_name): |
paul@393 | 1349 | self.NOP(node) |
paul@393 | 1350 | self.use_specific_attribute("__builtins__", function_name) |
paul@4 | 1351 | |
paul@393 | 1352 | def visitPrint(self, node): |
paul@393 | 1353 | self._visitPrint(node, "_print") |
paul@393 | 1354 | |
paul@393 | 1355 | def visitPrintnl(self, node): |
paul@393 | 1356 | self._visitPrint(node, "_printnl") |
paul@4 | 1357 | |
paul@299 | 1358 | visitRaise = NOP_ABANDON |
paul@0 | 1359 | |
paul@299 | 1360 | visitReturn = NOP_ABANDON |
paul@4 | 1361 | |
paul@580 | 1362 | visitRightShift = _visitOperator |
paul@1 | 1363 | |
paul@342 | 1364 | def visitSlice(self, node): |
paul@442 | 1365 | return self._visitOperator(node, self.in_assignment and "AssSlice" or "Slice") |
paul@4 | 1366 | |
paul@82 | 1367 | visitSliceobj = OP |
paul@23 | 1368 | |
paul@0 | 1369 | def visitStmt(self, node): |
paul@0 | 1370 | for n in node.nodes: |
paul@0 | 1371 | self.dispatch(n) |
paul@0 | 1372 | |
paul@580 | 1373 | visitSub = _visitOperator |
paul@4 | 1374 | |
paul@216 | 1375 | def visitSubscript(self, node): |
paul@442 | 1376 | return self._visitOperator(node, self.in_assignment and "AssSubscript" or "Subscript") |
paul@0 | 1377 | |
paul@0 | 1378 | def visitTryExcept(self, node): |
paul@540 | 1379 | self.new_branchpoint() |
paul@0 | 1380 | self.dispatch(node.body) |
paul@272 | 1381 | |
paul@0 | 1382 | for name, var, n in node.handlers: |
paul@572 | 1383 | if name is not None: |
paul@572 | 1384 | self.dispatch(name) |
paul@572 | 1385 | |
paul@537 | 1386 | self.new_branch(n) |
paul@97 | 1387 | |
paul@540 | 1388 | # Any abandoned branches from the body can now be resumed. |
paul@540 | 1389 | |
paul@540 | 1390 | self.resume_abandoned_branches() |
paul@540 | 1391 | |
paul@97 | 1392 | # Establish the local for the handler. |
paul@97 | 1393 | |
paul@99 | 1394 | if var is not None: |
paul@99 | 1395 | self.dispatch(var) |
paul@99 | 1396 | if n is not None: |
paul@99 | 1397 | self.dispatch(n) |
paul@272 | 1398 | |
paul@272 | 1399 | self.shelve_branch() |
paul@272 | 1400 | |
paul@540 | 1401 | # The else clause maintains the usage from the body but without the |
paul@540 | 1402 | # abandoned branches since they would never lead to the else clause |
paul@540 | 1403 | # being executed. |
paul@540 | 1404 | |
paul@379 | 1405 | self.new_branch(node.else_ or NullBranch()) |
paul@0 | 1406 | if node.else_ is not None: |
paul@0 | 1407 | self.dispatch(node.else_) |
paul@379 | 1408 | self.shelve_branch() |
paul@272 | 1409 | |
paul@272 | 1410 | self.merge_branches() |
paul@0 | 1411 | |
paul@4 | 1412 | visitTryFinally = NOP |
paul@4 | 1413 | |
paul@82 | 1414 | visitTuple = OP |
paul@1 | 1415 | |
paul@580 | 1416 | visitUnaryAdd = _visitOperator |
paul@4 | 1417 | |
paul@580 | 1418 | visitUnarySub = _visitOperator |
paul@4 | 1419 | |
paul@34 | 1420 | def visitWhile(self, node): |
paul@462 | 1421 | self.use_name("__bool__", node) |
paul@388 | 1422 | self.new_branchpoint(node) |
paul@272 | 1423 | |
paul@275 | 1424 | # Propagate attribute usage to branches. |
paul@275 | 1425 | |
paul@331 | 1426 | in_loop = self.in_loop |
paul@591 | 1427 | self.in_loop = True |
paul@381 | 1428 | |
paul@386 | 1429 | # The test is evaluated initially and again in the loop. |
paul@381 | 1430 | |
paul@272 | 1431 | self.dispatch(node.test) |
paul@381 | 1432 | |
paul@379 | 1433 | self.new_branch(node) |
paul@272 | 1434 | self.dispatch(node.body) |
paul@388 | 1435 | |
paul@388 | 1436 | self.resume_continuing_branches() |
paul@388 | 1437 | |
paul@386 | 1438 | self.dispatch(node.test) |
paul@386 | 1439 | self.shelve_branch() |
paul@381 | 1440 | |
paul@386 | 1441 | self.in_loop = in_loop |
paul@386 | 1442 | |
paul@386 | 1443 | # A null branch is used to record a path around the loop. |
paul@381 | 1444 | |
paul@386 | 1445 | self.new_branch(node.else_ or NullBranch()) |
paul@386 | 1446 | self.shelve_branch() |
paul@385 | 1447 | |
paul@386 | 1448 | self.merge_branches() |
paul@386 | 1449 | |
paul@386 | 1450 | # The else clause is evaluated outside any branch. |
paul@385 | 1451 | |
paul@385 | 1452 | if node.else_ is not None: |
paul@385 | 1453 | self.dispatch(node.else_) |
paul@385 | 1454 | |
paul@387 | 1455 | # Any suspended branches from the loop can now be resumed. |
paul@387 | 1456 | |
paul@388 | 1457 | self.resume_broken_branches() |
paul@387 | 1458 | |
paul@4 | 1459 | visitWith = NOP |
paul@4 | 1460 | |
paul@4 | 1461 | visitYield = NOP |
paul@4 | 1462 | |
paul@0 | 1463 | # vim: tabstop=4 expandtab shiftwidth=4 |