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