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