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