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@372 | 552 | def _visitAttr(self, expr, attrname, node): |
paul@372 | 553 | |
paul@468 | 554 | """ |
paul@468 | 555 | Process the attribute provided by the given 'expr' with the given |
paul@468 | 556 | 'attrname' and involving the given 'node'. |
paul@468 | 557 | """ |
paul@468 | 558 | |
paul@372 | 559 | # Attempt to identify the nature of the attribute. |
paul@372 | 560 | |
paul@372 | 561 | if isinstance(expr, Attr): |
paul@372 | 562 | value = expr.get_value() |
paul@372 | 563 | |
paul@372 | 564 | # Get the attribute and record its usage. |
paul@559 | 565 | # NOTE: Need to provide concrete values for things like base classes |
paul@559 | 566 | # NOTE: while also handling module attribute modification. |
paul@372 | 567 | |
paul@559 | 568 | if isinstance(value, (Class, Module)): |
paul@401 | 569 | |
paul@401 | 570 | # Check for class.__class__. |
paul@401 | 571 | |
paul@401 | 572 | if attrname == "__class__" and isinstance(value, Class): |
paul@401 | 573 | attr = type_class |
paul@401 | 574 | else: |
paul@448 | 575 | attr = value.get(attrname) or make_instance() |
paul@372 | 576 | self.use_specific_attribute(value.full_name(), attrname) |
paul@372 | 577 | |
paul@372 | 578 | elif isinstance(value, UnresolvedName): |
paul@372 | 579 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@372 | 580 | |
paul@467 | 581 | # The actual attribute is not readily identifiable and is assumed |
paul@467 | 582 | # to be an instance. |
paul@467 | 583 | |
paul@372 | 584 | else: |
paul@527 | 585 | |
paul@527 | 586 | # Record any instance attributes. |
paul@527 | 587 | |
paul@527 | 588 | if expr.name == "self": |
paul@527 | 589 | self.store_instance_attr(attrname, tentative=True) |
paul@527 | 590 | |
paul@448 | 591 | attr = make_instance() |
paul@372 | 592 | |
paul@372 | 593 | # Note usage of the attribute where a local is involved. |
paul@372 | 594 | |
paul@404 | 595 | self._visitAttrUser(expr, attrname, node) |
paul@372 | 596 | |
paul@467 | 597 | # No particular attribute has been identified, thus a general instance |
paul@467 | 598 | # is assumed. |
paul@467 | 599 | |
paul@372 | 600 | else: |
paul@448 | 601 | attr = make_instance() |
paul@372 | 602 | self.use_name(attrname, node) |
paul@372 | 603 | |
paul@372 | 604 | return attr |
paul@358 | 605 | |
paul@404 | 606 | def _visitAttrUser(self, expr, attrname, node, value=None): |
paul@404 | 607 | |
paul@404 | 608 | """ |
paul@404 | 609 | Note usage of the attribute provided by 'expr' with the given 'attrname' |
paul@404 | 610 | where a local is involved, annotating the given 'node'. If the optional |
paul@404 | 611 | 'value' is given, note an assignment for future effects on attributes |
paul@404 | 612 | where such attributes are inferred from the usage. |
paul@404 | 613 | """ |
paul@404 | 614 | |
paul@571 | 615 | # Access to attributes via a local in functions, classes or modules. |
paul@571 | 616 | # Since module-level locals are globals that can be modified |
paul@571 | 617 | # independently of the namespace, any attribute usage observations made |
paul@571 | 618 | # here may be revoked later if such modification is thought to occur. |
paul@467 | 619 | |
paul@571 | 620 | if expr.parent is self.get_namespace(): |
paul@556 | 621 | self.define_attribute_accessor(expr.name, attrname, node, value) |
paul@404 | 622 | else: |
paul@557 | 623 | self.use_name(attrname, node.expr, value, ns=expr.parent) |
paul@404 | 624 | |
paul@439 | 625 | def _visitConst(self, value): |
paul@439 | 626 | |
paul@439 | 627 | """ |
paul@439 | 628 | Register the constant given by 'value', if necessary, returning the |
paul@439 | 629 | resulting object. The type name is noted as being used, thus preserving |
paul@439 | 630 | the class in any generated program. |
paul@439 | 631 | """ |
paul@439 | 632 | |
paul@439 | 633 | self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value)) |
paul@440 | 634 | const = self.importer.make_constant(value) |
paul@440 | 635 | self.use_constant(const) |
paul@440 | 636 | return const |
paul@439 | 637 | |
paul@85 | 638 | def _visitFunction(self, node, name): |
paul@85 | 639 | |
paul@85 | 640 | """ |
paul@85 | 641 | Return a function object for the function defined by 'node' with the |
paul@85 | 642 | given 'name'. If a lambda expression is being visited, 'name' should be |
paul@85 | 643 | None. |
paul@85 | 644 | """ |
paul@85 | 645 | |
paul@233 | 646 | # Define the function object. |
paul@233 | 647 | |
paul@85 | 648 | function = Function( |
paul@85 | 649 | name, |
paul@272 | 650 | self.get_namespace(), |
paul@85 | 651 | node.argnames, |
paul@85 | 652 | node.defaults, |
paul@85 | 653 | (node.flags & 4 != 0), |
paul@85 | 654 | (node.flags & 8 != 0), |
paul@331 | 655 | self.in_loop or self.in_function, |
paul@85 | 656 | self, |
paul@85 | 657 | node |
paul@85 | 658 | ) |
paul@85 | 659 | |
paul@233 | 660 | self.add_object(function, any_scope=1) |
paul@233 | 661 | |
paul@85 | 662 | # Make a back reference from the node for code generation. |
paul@85 | 663 | |
paul@85 | 664 | node.unit = function |
paul@85 | 665 | |
paul@85 | 666 | # Process the defaults. |
paul@85 | 667 | |
paul@85 | 668 | for n in node.defaults: |
paul@85 | 669 | self.expr = self.dispatch(n) |
paul@192 | 670 | function.store_default(self.expr) |
paul@85 | 671 | |
paul@368 | 672 | # Note attribute usage where tuple parameters are involved. |
paul@368 | 673 | |
paul@368 | 674 | if function.tuple_parameters(): |
paul@368 | 675 | self.use_name("__getitem__", node) |
paul@368 | 676 | |
paul@368 | 677 | # Record the namespace context of the function for later processing. |
paul@368 | 678 | |
paul@235 | 679 | self.functions.append((node, self.namespaces + [function])) |
paul@85 | 680 | |
paul@330 | 681 | # Store the function. |
paul@330 | 682 | |
paul@85 | 683 | if name is not None: |
paul@85 | 684 | self.store(name, function) |
paul@85 | 685 | else: |
paul@85 | 686 | self.store_lambda(function) |
paul@167 | 687 | |
paul@330 | 688 | # Test the defaults and assess whether an dynamic object will result. |
paul@330 | 689 | |
paul@331 | 690 | function.make_dynamic() |
paul@331 | 691 | return function |
paul@85 | 692 | |
paul@235 | 693 | def _visitFunctionBody(self, node, namespaces): |
paul@235 | 694 | |
paul@235 | 695 | "Enter the function." |
paul@235 | 696 | |
paul@235 | 697 | # Current namespace is the function. |
paul@235 | 698 | # Previous namespace is the class. |
paul@235 | 699 | |
paul@330 | 700 | if self.in_class(namespaces): |
paul@235 | 701 | self.in_method = 1 |
paul@235 | 702 | |
paul@331 | 703 | in_function = self.in_function |
paul@331 | 704 | in_loop = self.in_loop |
paul@331 | 705 | self.in_function = 1 |
paul@331 | 706 | self.in_loop = 0 |
paul@331 | 707 | |
paul@235 | 708 | self.namespaces = namespaces |
paul@235 | 709 | self.dispatch(node.code) |
paul@235 | 710 | |
paul@331 | 711 | self.in_loop = in_loop |
paul@331 | 712 | self.in_function = in_function |
paul@235 | 713 | self.in_method = 0 |
paul@235 | 714 | |
paul@172 | 715 | # Specific handler methods. |
paul@172 | 716 | |
paul@580 | 717 | visitAdd = _visitOperator |
paul@82 | 718 | |
paul@462 | 719 | visitAnd = TEST_OP |
paul@0 | 720 | |
paul@4 | 721 | visitAssert = NOP |
paul@4 | 722 | |
paul@0 | 723 | def visitAssign(self, node): |
paul@0 | 724 | self.expr = self.dispatch(node.expr) |
paul@374 | 725 | self.in_assignment = 1 |
paul@0 | 726 | for n in node.nodes: |
paul@0 | 727 | self.dispatch(n) |
paul@374 | 728 | self.in_assignment = 0 |
paul@0 | 729 | |
paul@0 | 730 | def visitAssAttr(self, node): |
paul@0 | 731 | expr = self.dispatch(node.expr) |
paul@404 | 732 | attrname = node.attrname |
paul@272 | 733 | |
paul@272 | 734 | # Record the attribute on the presumed target. |
paul@272 | 735 | |
paul@94 | 736 | if isinstance(expr, Attr): |
paul@404 | 737 | value = expr.get_value() |
paul@404 | 738 | |
paul@94 | 739 | if expr.name == "self": |
paul@404 | 740 | self.store_instance_attr(attrname) |
paul@556 | 741 | self.use_attribute(expr.name, attrname, value) |
paul@488 | 742 | self._visitAttrUser(expr, attrname, node, self.expr) |
paul@404 | 743 | |
paul@404 | 744 | elif isinstance(value, Module): |
paul@404 | 745 | self.store_module_attr(attrname, value) |
paul@507 | 746 | print >>sys.stderr, "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) |
paul@272 | 747 | |
paul@404 | 748 | elif isinstance(value, Class): |
paul@404 | 749 | self.store_class_attr(attrname, value) |
paul@404 | 750 | |
paul@285 | 751 | # Note usage of the attribute where a local is involved. |
paul@272 | 752 | |
paul@404 | 753 | else: |
paul@404 | 754 | self._visitAttrUser(expr, attrname, node, self.expr) |
paul@381 | 755 | |
paul@404 | 756 | else: |
paul@404 | 757 | self.use_name(attrname, node) |
paul@272 | 758 | |
paul@0 | 759 | def visitAssList(self, node): |
paul@204 | 760 | |
paul@204 | 761 | # Declare names which will be used by generated code. |
paul@204 | 762 | |
paul@323 | 763 | self.use_name("__getitem__", node) |
paul@204 | 764 | |
paul@204 | 765 | # Process the assignment. |
paul@204 | 766 | |
paul@139 | 767 | for i, n in enumerate(node.nodes): |
paul@0 | 768 | self.dispatch(n) |
paul@439 | 769 | self._visitConst(i) # for __getitem__(i) at run-time |
paul@0 | 770 | |
paul@0 | 771 | def visitAssName(self, node): |
paul@289 | 772 | if hasattr(node, "flags") and node.flags == "OP_DELETE": |
paul@507 | 773 | print >>sys.stderr, "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) |
paul@339 | 774 | #raise InspectError("Deletion of attribute %r is not supported." % node.name) |
paul@274 | 775 | |
paul@120 | 776 | self.store(node.name, self.expr) |
paul@278 | 777 | self.define_attribute_user(node) |
paul@285 | 778 | |
paul@285 | 779 | # Ensure the presence of the given name in this namespace. |
paul@431 | 780 | # NOTE: Consider not registering assignments involving methods, since |
paul@431 | 781 | # NOTE: this is merely creating aliases for such methods. |
paul@285 | 782 | |
paul@434 | 783 | if isinstance(self.get_namespace(), (Class, Module)): |
paul@434 | 784 | if not isinstance(self.expr, Attr) or not isinstance(self.expr.get_value(), Function): |
paul@434 | 785 | self.use_specific_attribute(None, node.name) |
paul@434 | 786 | else: |
paul@434 | 787 | fn = self.expr.get_value() |
paul@434 | 788 | ns = self.get_namespace().full_name() |
paul@434 | 789 | self.use_specific_attribute(fn.parent.full_name(), fn.name, "%s.%s" % (ns, node.name)) |
paul@434 | 790 | |
paul@0 | 791 | visitAssTuple = visitAssList |
paul@0 | 792 | |
paul@172 | 793 | def visitAugAssign(self, node): |
paul@172 | 794 | |
paul@172 | 795 | # Accounting. |
paul@172 | 796 | |
paul@354 | 797 | operator_fn = operator_functions.get(node.op) |
paul@354 | 798 | operator_module = self._ensureOperators() |
paul@354 | 799 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@244 | 800 | |
paul@244 | 801 | # Process the assignment. |
paul@244 | 802 | |
paul@244 | 803 | self.expr = self.dispatch(node.expr) |
paul@244 | 804 | |
paul@244 | 805 | # NOTE: Similar to micropython.ast handler code. |
paul@363 | 806 | # NOTE: Slices and subscripts are supported by __setitem__(slice) and |
paul@363 | 807 | # NOTE: not __setslice__. |
paul@244 | 808 | |
paul@244 | 809 | if isinstance(node.node, compiler.ast.Name): |
paul@244 | 810 | self.visitAssName(node.node) |
paul@244 | 811 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@244 | 812 | self.visitAssAttr(node.node) |
paul@244 | 813 | else: |
paul@363 | 814 | self.use_specific_attribute("__builtins__", "slice") |
paul@363 | 815 | self.use_name("__setitem__", node) |
paul@244 | 816 | |
paul@82 | 817 | visitBackquote = OP |
paul@4 | 818 | |
paul@580 | 819 | visitBitand = _visitOperator |
paul@2 | 820 | |
paul@580 | 821 | visitBitor = _visitOperator |
paul@2 | 822 | |
paul@580 | 823 | visitBitxor = _visitOperator |
paul@4 | 824 | |
paul@388 | 825 | def visitBreak(self, node): |
paul@388 | 826 | self.NOP(node) |
paul@388 | 827 | self.suspend_broken_branch() |
paul@4 | 828 | |
paul@82 | 829 | visitCallFunc = OP |
paul@0 | 830 | |
paul@0 | 831 | def visitClass(self, node): |
paul@91 | 832 | |
paul@91 | 833 | """ |
paul@91 | 834 | Register the class at the given 'node' subject to the restrictions |
paul@91 | 835 | mentioned in the module docstring. |
paul@91 | 836 | """ |
paul@91 | 837 | |
paul@12 | 838 | if self.namespaces: |
paul@507 | 839 | print >>sys.stderr, "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@442 | 840 | return |
paul@0 | 841 | else: |
paul@331 | 842 | if self.in_loop: |
paul@507 | 843 | print >>sys.stderr, "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) |
paul@331 | 844 | |
paul@394 | 845 | cls = get_class(node.name, self.get_namespace(), self, node) |
paul@37 | 846 | |
paul@349 | 847 | # Make a back reference from the node for code generation. |
paul@349 | 848 | |
paul@349 | 849 | node.unit = cls |
paul@349 | 850 | |
paul@349 | 851 | # Process base classes in the context of the class's namespace. |
paul@349 | 852 | # This confines references to such classes to the class instead of |
paul@349 | 853 | # the namespace in which it is defined. |
paul@349 | 854 | |
paul@349 | 855 | self.namespaces.append(cls) |
paul@349 | 856 | |
paul@37 | 857 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 858 | # definitions of classes. |
paul@37 | 859 | |
paul@0 | 860 | for base in node.bases: |
paul@36 | 861 | expr = self.dispatch(base) |
paul@349 | 862 | |
paul@349 | 863 | # Each base class must be constant and known at compile-time. |
paul@349 | 864 | |
paul@36 | 865 | if isinstance(expr, Attr): |
paul@36 | 866 | if expr.assignments != 1: |
paul@442 | 867 | raise InspectError("Base class %r for %r is not constant: %r" % (base, cls.full_name(), expr)) |
paul@541 | 868 | elif not isinstance(expr.get_value(), Class): |
paul@541 | 869 | raise InspectError("Base class %r for %r is not a class: %r" % (base, cls.full_name(), expr.get_value())) |
paul@36 | 870 | else: |
paul@192 | 871 | cls.add_base(expr.get_value()) |
paul@349 | 872 | |
paul@349 | 873 | # Where no expression value is available, the base class is |
paul@349 | 874 | # not identifiable. |
paul@349 | 875 | |
paul@349 | 876 | else: |
paul@339 | 877 | raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 878 | |
paul@59 | 879 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 880 | # If no bases exist, adopt the 'object' class. |
paul@59 | 881 | |
paul@580 | 882 | if not node.bases and not (self.name == "__builtins__" and node.name == "object"): |
paul@59 | 883 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@192 | 884 | cls.add_base(expr.get_value()) |
paul@59 | 885 | |
paul@349 | 886 | # Make an entry for the class in the parent namespace. |
paul@21 | 887 | |
paul@349 | 888 | self.namespaces.pop() |
paul@0 | 889 | self.store(node.name, cls) |
paul@167 | 890 | self.add_object(cls) |
paul@0 | 891 | |
paul@349 | 892 | # Process the class body in its own namespace. |
paul@439 | 893 | # Add __name__ to the namespace. |
paul@59 | 894 | |
paul@12 | 895 | self.namespaces.append(cls) |
paul@439 | 896 | self.store("__name__", self._visitConst(node.name)) |
paul@0 | 897 | self.dispatch(node.code) |
paul@12 | 898 | self.namespaces.pop() |
paul@0 | 899 | |
paul@349 | 900 | cls.finalise_attribute_usage() |
paul@82 | 901 | return cls |
paul@0 | 902 | |
paul@172 | 903 | def visitCompare(self, node): |
paul@172 | 904 | |
paul@172 | 905 | # Accounting. |
paul@172 | 906 | # NOTE: Replicates some code in micropython.ast.visitCompare. |
paul@172 | 907 | |
paul@462 | 908 | self.use_name("__bool__", node) |
paul@462 | 909 | |
paul@323 | 910 | this_node = node |
paul@323 | 911 | |
paul@172 | 912 | for op in node.ops: |
paul@172 | 913 | op_name, next_node = op |
paul@323 | 914 | |
paul@354 | 915 | # Define name/attribute usage. |
paul@354 | 916 | # Get the applicable operation. |
paul@323 | 917 | |
paul@354 | 918 | operator_fn = operator_functions.get(op_name) |
paul@354 | 919 | |
paul@354 | 920 | # For operators, reference the specific function involved. |
paul@323 | 921 | |
paul@354 | 922 | if operator_fn is not None: |
paul@354 | 923 | operator_module = self._ensureOperators() |
paul@354 | 924 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@323 | 925 | |
paul@354 | 926 | # Define __contains__ usage on the next node. |
paul@354 | 927 | |
paul@172 | 928 | elif op_name.endswith("in"): |
paul@323 | 929 | self.use_name("__contains__", next_node) |
paul@323 | 930 | |
paul@323 | 931 | this_node = next_node |
paul@172 | 932 | |
paul@172 | 933 | return self.OP(node) |
paul@4 | 934 | |
paul@21 | 935 | def visitConst(self, node): |
paul@439 | 936 | return self._visitConst(node.value) |
paul@0 | 937 | |
paul@388 | 938 | def visitContinue(self, node): |
paul@388 | 939 | self.NOP(node) |
paul@388 | 940 | self.suspend_continuing_branch() |
paul@4 | 941 | |
paul@4 | 942 | visitDecorators = NOP |
paul@4 | 943 | |
paul@82 | 944 | visitDict = OP |
paul@0 | 945 | |
paul@0 | 946 | visitDiscard = NOP |
paul@0 | 947 | |
paul@580 | 948 | visitDiv = _visitOperator |
paul@4 | 949 | |
paul@4 | 950 | visitEllipsis = NOP |
paul@4 | 951 | |
paul@4 | 952 | visitExec = NOP |
paul@4 | 953 | |
paul@82 | 954 | visitExpression = OP |
paul@4 | 955 | |
paul@580 | 956 | visitFloorDiv = _visitOperator |
paul@4 | 957 | |
paul@34 | 958 | def visitFor(self, node): |
paul@388 | 959 | self.new_branchpoint(node) |
paul@204 | 960 | |
paul@204 | 961 | # Declare names which will be used by generated code. |
paul@204 | 962 | |
paul@323 | 963 | self.use_name("__iter__", node.list) |
paul@250 | 964 | self.use_name("next") |
paul@345 | 965 | self.use_name("StopIteration") |
paul@204 | 966 | |
paul@331 | 967 | in_loop = self.in_loop |
paul@289 | 968 | self.in_loop = 1 |
paul@341 | 969 | self.dispatch(node.list) |
paul@442 | 970 | |
paul@442 | 971 | # NOTE: Could generate AST nodes for the actual operations instead of |
paul@442 | 972 | # NOTE: manually generating code in micropython.ast. |
paul@442 | 973 | |
paul@448 | 974 | self.expr = make_instance() # each element is a result of a function call |
paul@272 | 975 | self.dispatch(node.assign) |
paul@272 | 976 | |
paul@204 | 977 | # Enter the loop. |
paul@275 | 978 | # Propagate attribute usage to branches. |
paul@204 | 979 | |
paul@379 | 980 | self.new_branch(node) |
paul@272 | 981 | self.dispatch(node.body) |
paul@385 | 982 | |
paul@388 | 983 | self.resume_continuing_branches() |
paul@385 | 984 | |
paul@272 | 985 | self.shelve_branch() |
paul@362 | 986 | |
paul@331 | 987 | self.in_loop = in_loop |
paul@272 | 988 | |
paul@386 | 989 | # A null branch is used to record a path around the loop. |
paul@275 | 990 | |
paul@379 | 991 | self.new_branch(node.else_ or NullBranch()) |
paul@379 | 992 | self.shelve_branch() |
paul@272 | 993 | |
paul@379 | 994 | self.merge_branches() |
paul@386 | 995 | |
paul@386 | 996 | # The else clause is evaluated outside any branch. |
paul@386 | 997 | |
paul@386 | 998 | if node.else_ is not None: |
paul@386 | 999 | self.dispatch(node.else_) |
paul@386 | 1000 | |
paul@387 | 1001 | # Any suspended branches from the loop can now be resumed. |
paul@387 | 1002 | |
paul@388 | 1003 | self.resume_broken_branches() |
paul@387 | 1004 | |
paul@0 | 1005 | def visitFrom(self, node): |
paul@562 | 1006 | module = self.importer.load(node.modname, 1, importer=node) |
paul@545 | 1007 | if module and not module.loaded: |
paul@545 | 1008 | print >>sys.stderr, "Warning: a circular import of %s is being attempted in %s" % (node.modname, self.full_name()) |
paul@0 | 1009 | |
paul@578 | 1010 | if module is None and self.importer.verbose: |
paul@578 | 1011 | print >>sys.stderr, "Warning:", node.modname, "not imported." |
paul@0 | 1012 | |
paul@0 | 1013 | for name, alias in node.names: |
paul@563 | 1014 | |
paul@563 | 1015 | # For specific names, obtain and store referenced objects using |
paul@563 | 1016 | # the name or any alias provided in the current namespace. |
paul@563 | 1017 | |
paul@0 | 1018 | if name != "*": |
paul@545 | 1019 | if module: |
paul@501 | 1020 | |
paul@501 | 1021 | # Missing names may refer to submodules. |
paul@501 | 1022 | |
paul@501 | 1023 | if not module.has_key(name): |
paul@562 | 1024 | submodule = self.importer.load(node.modname + "." + name, 1, importer=node) |
paul@545 | 1025 | if submodule: |
paul@545 | 1026 | if not submodule.loaded: |
paul@545 | 1027 | print >>sys.stderr, "Warning: a circular import of %s.%s is being attempted in %s" % ( |
paul@545 | 1028 | node.modname, name, self.full_name()) |
paul@545 | 1029 | |
paul@501 | 1030 | module.store(name, submodule) |
paul@501 | 1031 | |
paul@501 | 1032 | # Complete the import if the name was found. |
paul@501 | 1033 | |
paul@501 | 1034 | if module.has_key(name): |
paul@501 | 1035 | attr = module[name] |
paul@501 | 1036 | self.store(alias or name, attr) |
paul@501 | 1037 | self.use_specific_attribute(module.full_name(), name) |
paul@501 | 1038 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@578 | 1039 | submodule = self.importer.load(attr.get_value().name, importer=node) |
paul@578 | 1040 | |
paul@578 | 1041 | # For circular imports, invalidate attribute usage for |
paul@578 | 1042 | # all exported names of submodules whose names are |
paul@578 | 1043 | # specified in the from statement. |
paul@578 | 1044 | |
paul@578 | 1045 | if submodule and not submodule.loaded: |
paul@578 | 1046 | for n in submodule.keys(): |
paul@578 | 1047 | submodule.modify_name(n) |
paul@578 | 1048 | |
paul@501 | 1049 | continue |
paul@24 | 1050 | |
paul@24 | 1051 | # Support the import of names from missing modules. |
paul@24 | 1052 | |
paul@501 | 1053 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@501 | 1054 | |
paul@563 | 1055 | # For wildcards, obtain and store all objects from a module in the |
paul@563 | 1056 | # current namespace. |
paul@563 | 1057 | |
paul@3 | 1058 | else: |
paul@545 | 1059 | if module: |
paul@285 | 1060 | for n in module.keys(): |
paul@24 | 1061 | attr = module[n] |
paul@192 | 1062 | self.store(n, attr) |
paul@364 | 1063 | self.use_specific_attribute(module.full_name(), n) |
paul@192 | 1064 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@578 | 1065 | submodule = self.importer.load(attr.get_value().name, importer=node) |
paul@578 | 1066 | |
paul@578 | 1067 | # For circular imports, invalidate attribute usage |
paul@578 | 1068 | # for all exported names of submodules provided by |
paul@578 | 1069 | # the module. |
paul@578 | 1070 | |
paul@578 | 1071 | if submodule and not submodule.loaded: |
paul@578 | 1072 | for subn in submodule.keys(): |
paul@578 | 1073 | submodule.modify_name(subn) |
paul@0 | 1074 | |
paul@0 | 1075 | def visitFunction(self, node): |
paul@85 | 1076 | return self._visitFunction(node, node.name) |
paul@0 | 1077 | |
paul@82 | 1078 | visitGenExpr = OP |
paul@4 | 1079 | |
paul@4 | 1080 | visitGenExprFor = NOP |
paul@4 | 1081 | |
paul@4 | 1082 | visitGenExprIf = NOP |
paul@4 | 1083 | |
paul@4 | 1084 | visitGenExprInner = NOP |
paul@4 | 1085 | |
paul@0 | 1086 | def visitGetattr(self, node): |
paul@0 | 1087 | expr = self.dispatch(node.expr) |
paul@169 | 1088 | attrname = node.attrname |
paul@556 | 1089 | node._attr = self._visitAttr(expr, attrname, node) |
paul@556 | 1090 | return node._attr |
paul@4 | 1091 | |
paul@12 | 1092 | def visitGlobal(self, node): |
paul@12 | 1093 | if self.namespaces: |
paul@12 | 1094 | for name in node.names: |
paul@178 | 1095 | ns = self.namespaces[-1] |
paul@178 | 1096 | if not ns.make_global(name): |
paul@339 | 1097 | raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) |
paul@37 | 1098 | |
paul@236 | 1099 | # The name is recorded in an earlier process. |
paul@0 | 1100 | |
paul@0 | 1101 | def visitIf(self, node): |
paul@462 | 1102 | self.use_name("__bool__", node) |
paul@272 | 1103 | self.new_branchpoint() |
paul@272 | 1104 | |
paul@275 | 1105 | # Propagate attribute usage to branches. |
paul@275 | 1106 | |
paul@0 | 1107 | for test, body in node.tests: |
paul@202 | 1108 | self.dispatch(test) |
paul@272 | 1109 | |
paul@377 | 1110 | self.new_branch(body) |
paul@0 | 1111 | self.dispatch(body) |
paul@272 | 1112 | self.shelve_branch() |
paul@272 | 1113 | |
paul@377 | 1114 | # Maintain a branch for the else clause. |
paul@275 | 1115 | |
paul@379 | 1116 | self.new_branch(node.else_ or NullBranch()) |
paul@0 | 1117 | if node.else_ is not None: |
paul@0 | 1118 | self.dispatch(node.else_) |
paul@379 | 1119 | self.shelve_branch() |
paul@272 | 1120 | |
paul@272 | 1121 | self.merge_branches() |
paul@442 | 1122 | |
paul@442 | 1123 | def visitIfExp(self, node): |
paul@462 | 1124 | self.use_name("__bool__", node) |
paul@442 | 1125 | self.new_branchpoint() |
paul@442 | 1126 | |
paul@442 | 1127 | # Propagate attribute usage to branches. |
paul@442 | 1128 | |
paul@442 | 1129 | self.dispatch(node.test) |
paul@0 | 1130 | |
paul@442 | 1131 | self.new_branch(node.then) |
paul@442 | 1132 | self.dispatch(node.then) |
paul@442 | 1133 | self.shelve_branch() |
paul@442 | 1134 | |
paul@442 | 1135 | self.new_branch(node.else_) |
paul@442 | 1136 | self.dispatch(node.else_) |
paul@442 | 1137 | self.shelve_branch() |
paul@442 | 1138 | |
paul@442 | 1139 | self.merge_branches() |
paul@448 | 1140 | return make_instance() # either outcome is possible |
paul@12 | 1141 | |
paul@0 | 1142 | def visitImport(self, node): |
paul@0 | 1143 | for name, alias in node.names: |
paul@2 | 1144 | if alias is not None: |
paul@562 | 1145 | module = self.importer.load(name, 1, importer=node) or UnresolvedName(None, name, self) |
paul@167 | 1146 | self.store(alias, module) |
paul@2 | 1147 | else: |
paul@562 | 1148 | module = self.importer.load(name, importer=node) or UnresolvedName(None, name.split(".")[0], self) |
paul@167 | 1149 | self.store(name.split(".")[0], module) |
paul@0 | 1150 | |
paul@578 | 1151 | circular = module and not module.loaded |
paul@578 | 1152 | |
paul@578 | 1153 | # For circular imports, invalidate attribute usage for all exported |
paul@578 | 1154 | # names of modules. |
paul@578 | 1155 | |
paul@578 | 1156 | if module and not module.loaded: |
paul@578 | 1157 | for n in module.keys(): |
paul@578 | 1158 | module.modify_name(n) |
paul@578 | 1159 | |
paul@580 | 1160 | visitInvert = _visitOperator |
paul@4 | 1161 | |
paul@59 | 1162 | def visitKeyword(self, node): |
paul@59 | 1163 | self.dispatch(node.expr) |
paul@439 | 1164 | self._visitConst(node.name) |
paul@71 | 1165 | self.keyword_names.add(node.name) |
paul@4 | 1166 | |
paul@85 | 1167 | def visitLambda(self, node): |
paul@351 | 1168 | fn = self._visitFunction(node, None) |
paul@351 | 1169 | self.use_specific_attribute(None, fn.name) |
paul@351 | 1170 | return fn |
paul@4 | 1171 | |
paul@580 | 1172 | visitLeftShift = _visitOperator |
paul@1 | 1173 | |
paul@346 | 1174 | def visitList(self, node): |
paul@347 | 1175 | self.use_specific_attribute("__builtins__", "list") |
paul@442 | 1176 | return self.OP(node) |
paul@0 | 1177 | |
paul@341 | 1178 | def visitListComp(self, node): |
paul@506 | 1179 | |
paul@506 | 1180 | # Note that explicit dispatch is performed. |
paul@506 | 1181 | |
paul@506 | 1182 | if node.quals: |
paul@506 | 1183 | self.visitListCompFor(node.quals[0], node.quals[1:], node.expr) |
paul@448 | 1184 | return make_instance() |
paul@341 | 1185 | |
paul@506 | 1186 | def visitListCompFor(self, node, following_quals, expr): |
paul@341 | 1187 | self.new_branchpoint() |
paul@341 | 1188 | |
paul@341 | 1189 | # Declare names which will be used by generated code. |
paul@341 | 1190 | |
paul@341 | 1191 | self.use_name("__iter__", node.list) |
paul@341 | 1192 | self.use_name("next") |
paul@4 | 1193 | |
paul@341 | 1194 | in_loop = self.in_loop |
paul@341 | 1195 | self.in_loop = 1 |
paul@341 | 1196 | self.dispatch(node.list) |
paul@442 | 1197 | |
paul@442 | 1198 | # NOTE: Could generate AST nodes for the actual operations instead of |
paul@442 | 1199 | # NOTE: manually generating code in micropython.ast. |
paul@442 | 1200 | |
paul@448 | 1201 | self.expr = make_instance() # each element is a result of a function call |
paul@341 | 1202 | self.dispatch(node.assign) |
paul@341 | 1203 | |
paul@341 | 1204 | # Enter the loop. |
paul@341 | 1205 | # Propagate attribute usage to branches. |
paul@341 | 1206 | |
paul@379 | 1207 | self.new_branch(node) |
paul@341 | 1208 | |
paul@506 | 1209 | # Note that explicit dispatch is performed. |
paul@506 | 1210 | |
paul@506 | 1211 | if node.ifs: |
paul@506 | 1212 | self.visitListCompIf(node.ifs[0], node.ifs[1:], following_quals, expr) |
paul@506 | 1213 | elif following_quals: |
paul@506 | 1214 | self.visitListCompFor(following_quals[0], following_quals[1:], expr) |
paul@506 | 1215 | else: |
paul@506 | 1216 | self.dispatch(expr) |
paul@341 | 1217 | |
paul@341 | 1218 | self.shelve_branch() |
paul@341 | 1219 | self.in_loop = in_loop |
paul@341 | 1220 | |
paul@379 | 1221 | self.merge_branches() |
paul@4 | 1222 | |
paul@506 | 1223 | def visitListCompIf(self, node, following_ifs, following_quals, expr): |
paul@506 | 1224 | self.use_name("__bool__", node) |
paul@506 | 1225 | self.new_branchpoint() |
paul@506 | 1226 | |
paul@506 | 1227 | # Propagate attribute usage to branches. |
paul@506 | 1228 | |
paul@506 | 1229 | self.dispatch(node.test) |
paul@506 | 1230 | |
paul@506 | 1231 | # Note that explicit dispatch is performed. |
paul@506 | 1232 | |
paul@506 | 1233 | if following_ifs: |
paul@506 | 1234 | self.visitListCompIf(following_ifs[0], following_ifs[1:], following_quals, expr) |
paul@506 | 1235 | elif following_quals: |
paul@506 | 1236 | self.visitListCompFor(following_quals[0], following_quals[1:], expr) |
paul@506 | 1237 | else: |
paul@506 | 1238 | self.new_branch(expr) |
paul@506 | 1239 | self.dispatch(expr) |
paul@506 | 1240 | self.shelve_branch() |
paul@506 | 1241 | |
paul@506 | 1242 | # Maintain a branch for the else clause. |
paul@506 | 1243 | |
paul@506 | 1244 | self.new_branch(NullBranch()) |
paul@506 | 1245 | self.shelve_branch() |
paul@506 | 1246 | |
paul@506 | 1247 | self.merge_branches() |
paul@4 | 1248 | |
paul@580 | 1249 | visitMod = _visitOperator |
paul@4 | 1250 | |
paul@0 | 1251 | def visitModule(self, node): |
paul@143 | 1252 | |
paul@143 | 1253 | # Make a back reference from the node for code generation. |
paul@143 | 1254 | |
paul@143 | 1255 | node.unit = self |
paul@0 | 1256 | return self.dispatch(node.node) |
paul@0 | 1257 | |
paul@580 | 1258 | visitMul = _visitOperator |
paul@2 | 1259 | |
paul@0 | 1260 | def visitName(self, node): |
paul@448 | 1261 | return self.get_namespace().get_using_node(node.name, node) or make_instance() |
paul@0 | 1262 | |
paul@531 | 1263 | def visitNot(self, node): |
paul@531 | 1264 | self.use_name("__bool__", node) |
paul@531 | 1265 | self.dispatch(node.expr) |
paul@531 | 1266 | return make_instance() |
paul@0 | 1267 | |
paul@462 | 1268 | visitOr = TEST_OP |
paul@0 | 1269 | |
paul@0 | 1270 | visitPass = NOP |
paul@0 | 1271 | |
paul@580 | 1272 | visitPower = _visitOperator |
paul@4 | 1273 | |
paul@393 | 1274 | def _visitPrint(self, node, function_name): |
paul@393 | 1275 | self.NOP(node) |
paul@393 | 1276 | self.use_specific_attribute("__builtins__", function_name) |
paul@4 | 1277 | |
paul@393 | 1278 | def visitPrint(self, node): |
paul@393 | 1279 | self._visitPrint(node, "_print") |
paul@393 | 1280 | |
paul@393 | 1281 | def visitPrintnl(self, node): |
paul@393 | 1282 | self._visitPrint(node, "_printnl") |
paul@4 | 1283 | |
paul@299 | 1284 | visitRaise = NOP_ABANDON |
paul@0 | 1285 | |
paul@299 | 1286 | visitReturn = NOP_ABANDON |
paul@4 | 1287 | |
paul@580 | 1288 | visitRightShift = _visitOperator |
paul@1 | 1289 | |
paul@342 | 1290 | def visitSlice(self, node): |
paul@442 | 1291 | return self._visitOperator(node, self.in_assignment and "AssSlice" or "Slice") |
paul@4 | 1292 | |
paul@82 | 1293 | visitSliceobj = OP |
paul@23 | 1294 | |
paul@0 | 1295 | def visitStmt(self, node): |
paul@0 | 1296 | for n in node.nodes: |
paul@0 | 1297 | self.dispatch(n) |
paul@0 | 1298 | |
paul@580 | 1299 | visitSub = _visitOperator |
paul@4 | 1300 | |
paul@216 | 1301 | def visitSubscript(self, node): |
paul@442 | 1302 | return self._visitOperator(node, self.in_assignment and "AssSubscript" or "Subscript") |
paul@0 | 1303 | |
paul@0 | 1304 | def visitTryExcept(self, node): |
paul@540 | 1305 | self.new_branchpoint() |
paul@0 | 1306 | self.dispatch(node.body) |
paul@272 | 1307 | |
paul@0 | 1308 | for name, var, n in node.handlers: |
paul@572 | 1309 | if name is not None: |
paul@572 | 1310 | self.dispatch(name) |
paul@572 | 1311 | |
paul@537 | 1312 | self.new_branch(n) |
paul@97 | 1313 | |
paul@540 | 1314 | # Any abandoned branches from the body can now be resumed. |
paul@540 | 1315 | |
paul@540 | 1316 | self.resume_abandoned_branches() |
paul@540 | 1317 | |
paul@97 | 1318 | # Establish the local for the handler. |
paul@97 | 1319 | |
paul@99 | 1320 | if var is not None: |
paul@99 | 1321 | self.dispatch(var) |
paul@99 | 1322 | if n is not None: |
paul@99 | 1323 | self.dispatch(n) |
paul@272 | 1324 | |
paul@272 | 1325 | self.shelve_branch() |
paul@272 | 1326 | |
paul@540 | 1327 | # The else clause maintains the usage from the body but without the |
paul@540 | 1328 | # abandoned branches since they would never lead to the else clause |
paul@540 | 1329 | # being executed. |
paul@540 | 1330 | |
paul@379 | 1331 | self.new_branch(node.else_ or NullBranch()) |
paul@0 | 1332 | if node.else_ is not None: |
paul@0 | 1333 | self.dispatch(node.else_) |
paul@379 | 1334 | self.shelve_branch() |
paul@272 | 1335 | |
paul@272 | 1336 | self.merge_branches() |
paul@0 | 1337 | |
paul@4 | 1338 | visitTryFinally = NOP |
paul@4 | 1339 | |
paul@82 | 1340 | visitTuple = OP |
paul@1 | 1341 | |
paul@580 | 1342 | visitUnaryAdd = _visitOperator |
paul@4 | 1343 | |
paul@580 | 1344 | visitUnarySub = _visitOperator |
paul@4 | 1345 | |
paul@34 | 1346 | def visitWhile(self, node): |
paul@462 | 1347 | self.use_name("__bool__", node) |
paul@388 | 1348 | self.new_branchpoint(node) |
paul@272 | 1349 | |
paul@275 | 1350 | # Propagate attribute usage to branches. |
paul@275 | 1351 | |
paul@331 | 1352 | in_loop = self.in_loop |
paul@34 | 1353 | self.in_loop = 1 |
paul@381 | 1354 | |
paul@386 | 1355 | # The test is evaluated initially and again in the loop. |
paul@381 | 1356 | |
paul@272 | 1357 | self.dispatch(node.test) |
paul@381 | 1358 | |
paul@379 | 1359 | self.new_branch(node) |
paul@272 | 1360 | self.dispatch(node.body) |
paul@388 | 1361 | |
paul@388 | 1362 | self.resume_continuing_branches() |
paul@388 | 1363 | |
paul@386 | 1364 | self.dispatch(node.test) |
paul@386 | 1365 | self.shelve_branch() |
paul@381 | 1366 | |
paul@386 | 1367 | self.in_loop = in_loop |
paul@386 | 1368 | |
paul@386 | 1369 | # A null branch is used to record a path around the loop. |
paul@381 | 1370 | |
paul@386 | 1371 | self.new_branch(node.else_ or NullBranch()) |
paul@386 | 1372 | self.shelve_branch() |
paul@385 | 1373 | |
paul@386 | 1374 | self.merge_branches() |
paul@386 | 1375 | |
paul@386 | 1376 | # The else clause is evaluated outside any branch. |
paul@385 | 1377 | |
paul@385 | 1378 | if node.else_ is not None: |
paul@385 | 1379 | self.dispatch(node.else_) |
paul@385 | 1380 | |
paul@387 | 1381 | # Any suspended branches from the loop can now be resumed. |
paul@387 | 1382 | |
paul@388 | 1383 | self.resume_broken_branches() |
paul@387 | 1384 | |
paul@4 | 1385 | visitWith = NOP |
paul@4 | 1386 | |
paul@4 | 1387 | visitYield = NOP |
paul@4 | 1388 | |
paul@0 | 1389 | # vim: tabstop=4 expandtab shiftwidth=4 |