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