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@299 | 6 | Copyright (C) 2007, 2008, 2009, 2010 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@91 | 65 | assignments to provide constant values if the targets reside in the module |
paul@91 | 66 | namespace or in class namespaces, subject to the above conditions. |
paul@91 | 67 | |
paul@91 | 68 | Assignments to names within functions are not generally considered to cause the |
paul@91 | 69 | targets of such assignments to provide constant values since functions can be |
paul@331 | 70 | invoked many times with different inputs. This affects particularly the |
paul@331 | 71 | definition of functions or lambdas within functions. However, there may be |
paul@331 | 72 | benefits in considering a local to be constant within a single invocation. |
paul@0 | 73 | """ |
paul@0 | 74 | |
paul@39 | 75 | from micropython.common import * |
paul@83 | 76 | from micropython.data import * |
paul@0 | 77 | import compiler.ast |
paul@15 | 78 | |
paul@21 | 79 | # Program visitors. |
paul@21 | 80 | |
paul@15 | 81 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 82 | |
paul@20 | 83 | """ |
paul@20 | 84 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 85 | capable of being used as an AST visitor. |
paul@20 | 86 | """ |
paul@15 | 87 | |
paul@71 | 88 | def __init__(self, name, importer): |
paul@71 | 89 | |
paul@71 | 90 | """ |
paul@71 | 91 | Initialise this visitor with a module 'name' and an 'importer' which is |
paul@71 | 92 | used to provide access to other modules when required. |
paul@71 | 93 | """ |
paul@71 | 94 | |
paul@15 | 95 | ASTVisitor.__init__(self) |
paul@343 | 96 | Module.__init__(self, name, importer) |
paul@15 | 97 | self.visitor = self |
paul@15 | 98 | |
paul@91 | 99 | # Import machinery links. |
paul@91 | 100 | |
paul@343 | 101 | self.optimisations = self.importer.optimisations |
paul@61 | 102 | self.builtins = self.importer.modules.get("__builtins__") |
paul@15 | 103 | self.loaded = 0 |
paul@15 | 104 | |
paul@1 | 105 | # Current expression state. |
paul@1 | 106 | |
paul@1 | 107 | self.expr = None |
paul@1 | 108 | |
paul@1 | 109 | # Namespace state. |
paul@1 | 110 | |
paul@91 | 111 | self.in_method = 0 # Find instance attributes in all methods. |
paul@331 | 112 | self.in_function = 0 # Note function presence, affecting definitions. |
paul@34 | 113 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 114 | self.namespaces = [] |
paul@235 | 115 | self.functions = [] |
paul@0 | 116 | |
paul@0 | 117 | def parse(self, filename): |
paul@9 | 118 | |
paul@9 | 119 | "Parse the file having the given 'filename'." |
paul@9 | 120 | |
paul@0 | 121 | module = compiler.parseFile(filename) |
paul@0 | 122 | self.process(module) |
paul@0 | 123 | |
paul@0 | 124 | def process(self, module): |
paul@9 | 125 | |
paul@9 | 126 | "Process the given 'module'." |
paul@9 | 127 | |
paul@343 | 128 | self.astnode = module |
paul@236 | 129 | |
paul@260 | 130 | # Add __name__ to the namespace by adding an explicit assignment to the |
paul@260 | 131 | # module. |
paul@260 | 132 | |
paul@260 | 133 | module.node.nodes.insert(0, compiler.ast.Assign( |
paul@260 | 134 | [compiler.ast.AssName("__name__", 0)], |
paul@260 | 135 | compiler.ast.Const(self.full_name()) |
paul@260 | 136 | )) |
paul@260 | 137 | |
paul@236 | 138 | # First, visit module-level code, recording global names. |
paul@236 | 139 | |
paul@1 | 140 | processed = self.dispatch(module) |
paul@235 | 141 | |
paul@236 | 142 | # Then, for each function, detect and record globals declared in those |
paul@236 | 143 | # functions. |
paul@236 | 144 | |
paul@236 | 145 | for node, namespaces in self.functions: |
paul@236 | 146 | self.process_globals(node) |
paul@236 | 147 | |
paul@344 | 148 | self.finalise_attribute_usage() |
paul@284 | 149 | |
paul@236 | 150 | # Then, visit each function, recording other names. |
paul@236 | 151 | |
paul@235 | 152 | for node, namespaces in self.functions: |
paul@235 | 153 | self._visitFunctionBody(node, namespaces) |
paul@344 | 154 | namespaces[-1].finalise_attribute_usage() |
paul@280 | 155 | |
paul@236 | 156 | # Add references to other modules declared using the __all__ global. |
paul@236 | 157 | |
paul@13 | 158 | if self.has_key("__all__"): |
paul@12 | 159 | all = self["__all__"] |
paul@1 | 160 | if isinstance(all, compiler.ast.List): |
paul@1 | 161 | for n in all.nodes: |
paul@167 | 162 | self.store(n.value, self.importer.add_module(self.name + "." + n.value)) |
paul@235 | 163 | |
paul@1 | 164 | return processed |
paul@0 | 165 | |
paul@236 | 166 | def process_globals(self, node): |
paul@236 | 167 | |
paul@236 | 168 | """ |
paul@236 | 169 | Within the given 'node', process global declarations, adjusting the |
paul@236 | 170 | module namespace. |
paul@236 | 171 | """ |
paul@236 | 172 | |
paul@236 | 173 | for n in node.getChildNodes(): |
paul@236 | 174 | if isinstance(n, compiler.ast.Global): |
paul@236 | 175 | for name in n.names: |
paul@236 | 176 | if not self.has_key(name): |
paul@236 | 177 | self[name] = None |
paul@236 | 178 | else: |
paul@236 | 179 | self.process_globals(n) |
paul@236 | 180 | |
paul@3 | 181 | def vacuum(self): |
paul@9 | 182 | |
paul@171 | 183 | """ |
paul@171 | 184 | Vacuum the module namespace, removing unreferenced objects and unused |
paul@171 | 185 | names. |
paul@171 | 186 | """ |
paul@9 | 187 | |
paul@241 | 188 | if self.should_optimise_unused_objects(): |
paul@252 | 189 | self.vacuum_object(self) |
paul@0 | 190 | |
paul@167 | 191 | all_objects = list(self.all_objects) |
paul@167 | 192 | |
paul@167 | 193 | for obj in all_objects: |
paul@351 | 194 | self.vacuum_object(obj) |
paul@171 | 195 | |
paul@252 | 196 | def vacuum_object(self, obj, delete_all=0): |
paul@241 | 197 | |
paul@252 | 198 | "Vacuum the given object 'obj'." |
paul@171 | 199 | |
paul@351 | 200 | for name, attr in obj.items_for_vacuum(): |
paul@270 | 201 | |
paul@270 | 202 | # Only consider deleting entire unused objects or things accessible |
paul@270 | 203 | # via names which are never used. |
paul@270 | 204 | |
paul@344 | 205 | if delete_all or not self.importer.uses_attribute(obj.full_name(), name): |
paul@351 | 206 | obj.vacuum_item(name) |
paul@270 | 207 | |
paul@270 | 208 | # Delete any unambiguous attribute value. Such values can only |
paul@270 | 209 | # have been defined within the object and therefore are not |
paul@270 | 210 | # redefined by other code regions. |
paul@270 | 211 | |
paul@252 | 212 | if attr.assignments == 1: |
paul@252 | 213 | value = attr.get_value() |
paul@171 | 214 | |
paul@252 | 215 | if value is not obj and value in self.all_objects: |
paul@252 | 216 | self.all_objects.remove(value) |
paul@171 | 217 | |
paul@351 | 218 | # Delete class contents and lambdas from functions. |
paul@241 | 219 | |
paul@351 | 220 | self.vacuum_object(value, 1) |
paul@171 | 221 | |
paul@287 | 222 | def unfinalise(self): |
paul@287 | 223 | |
paul@287 | 224 | "Reset finalised information for the module." |
paul@287 | 225 | |
paul@287 | 226 | for obj in self.all_objects: |
paul@287 | 227 | obj.unfinalise_attributes() |
paul@287 | 228 | |
paul@171 | 229 | def finalise(self): |
paul@171 | 230 | |
paul@171 | 231 | "Finalise the module." |
paul@171 | 232 | |
paul@171 | 233 | for obj in self.all_objects: |
paul@287 | 234 | obj.finalise_attributes() |
paul@171 | 235 | |
paul@167 | 236 | def add_object(self, obj, any_scope=0): |
paul@167 | 237 | |
paul@167 | 238 | """ |
paul@167 | 239 | Record 'obj' if non-local or if the optional 'any_scope' is set to a |
paul@167 | 240 | true value. |
paul@167 | 241 | """ |
paul@167 | 242 | |
paul@167 | 243 | if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@167 | 244 | self.all_objects.add(obj) |
paul@167 | 245 | |
paul@167 | 246 | # Optimisation tests. |
paul@167 | 247 | |
paul@167 | 248 | def should_optimise_unused_objects(self): |
paul@167 | 249 | return "unused_objects" in self.optimisations |
paul@167 | 250 | |
paul@3 | 251 | # Namespace methods. |
paul@0 | 252 | |
paul@330 | 253 | def in_class(self, namespaces=None): |
paul@330 | 254 | namespaces = namespaces or self.namespaces |
paul@330 | 255 | return len(namespaces) > 1 and isinstance(namespaces[-2], Class) |
paul@330 | 256 | |
paul@0 | 257 | def store(self, name, obj): |
paul@12 | 258 | |
paul@12 | 259 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 260 | |
paul@339 | 261 | # Store in the module. |
paul@339 | 262 | |
paul@12 | 263 | if not self.namespaces: |
paul@342 | 264 | if self.in_loop and self.used_in_scope(name, "builtins"): |
paul@339 | 265 | raise InspectError("Name %r already used as a built-in." % name) |
paul@339 | 266 | else: |
paul@339 | 267 | self.set(name, obj, not self.in_loop) |
paul@339 | 268 | |
paul@339 | 269 | # Or store locally. |
paul@339 | 270 | |
paul@12 | 271 | else: |
paul@341 | 272 | locals = self.namespaces[-1] |
paul@341 | 273 | |
paul@342 | 274 | if self.in_loop and locals.used_in_scope(name, "global") and not name in locals.globals: |
paul@341 | 275 | raise InspectError("Name %r already used as global." % name) |
paul@342 | 276 | elif self.in_loop and locals.used_in_scope(name, "builtins"): |
paul@341 | 277 | raise InspectError("Name %r already used as a built-in." % name) |
paul@341 | 278 | else: |
paul@341 | 279 | locals.set(name, obj, not self.in_loop) |
paul@12 | 280 | |
paul@85 | 281 | def store_lambda(self, obj): |
paul@91 | 282 | |
paul@91 | 283 | "Store a lambda function 'obj'." |
paul@91 | 284 | |
paul@167 | 285 | self.add_object(obj) |
paul@351 | 286 | self.get_namespace().add_lambda(obj) |
paul@85 | 287 | |
paul@94 | 288 | def store_module_attr(self, name, module): |
paul@94 | 289 | |
paul@94 | 290 | """ |
paul@94 | 291 | Record module attribute 'name' in the given 'module' using the current |
paul@94 | 292 | expression. |
paul@94 | 293 | """ |
paul@94 | 294 | |
paul@192 | 295 | module.set(name, self.expr, 0) |
paul@94 | 296 | |
paul@91 | 297 | def store_class_attr(self, name): |
paul@91 | 298 | |
paul@94 | 299 | """ |
paul@94 | 300 | Record class attribute 'name' in the current class using the current |
paul@94 | 301 | expression. |
paul@94 | 302 | """ |
paul@91 | 303 | |
paul@91 | 304 | if self.in_method and self.namespaces[-2].has_key(name): |
paul@192 | 305 | self.namespaces[-2].set(name, self.expr, 0) |
paul@91 | 306 | return 1 |
paul@91 | 307 | |
paul@91 | 308 | return 0 |
paul@91 | 309 | |
paul@34 | 310 | def store_instance_attr(self, name): |
paul@12 | 311 | |
paul@12 | 312 | "Record instance attribute 'name' in the current class." |
paul@12 | 313 | |
paul@91 | 314 | if self.in_method: |
paul@15 | 315 | |
paul@12 | 316 | # Current namespace is the function. |
paul@12 | 317 | # Previous namespace is the class. |
paul@15 | 318 | |
paul@12 | 319 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 320 | |
paul@272 | 321 | def get_namespace(self): |
paul@71 | 322 | |
paul@71 | 323 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 324 | |
paul@12 | 325 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 326 | |
paul@323 | 327 | def use_name(self, name, node=None): |
paul@323 | 328 | |
paul@323 | 329 | """ |
paul@323 | 330 | Use the given 'name' within the current namespace/unit, either in |
paul@323 | 331 | conjunction with a particular object (if 'node' is specified and not |
paul@323 | 332 | None) or unconditionally. |
paul@323 | 333 | """ |
paul@250 | 334 | |
paul@323 | 335 | if node is not None and isinstance(node, compiler.ast.Name): |
paul@323 | 336 | self.use_attribute(node.name, name) |
paul@250 | 337 | |
paul@323 | 338 | # For general name usage, declare usage of the given name from this |
paul@323 | 339 | # particular unit. |
paul@323 | 340 | |
paul@323 | 341 | else: |
paul@323 | 342 | unit = self.get_namespace() |
paul@323 | 343 | self.importer.use_name(name, unit.full_name()) |
paul@250 | 344 | |
paul@279 | 345 | # Attribute usage methods. |
paul@279 | 346 | # These are convenience methods which refer to the specific namespace's |
paul@279 | 347 | # implementation of these operations. |
paul@279 | 348 | |
paul@272 | 349 | def new_branchpoint(self): |
paul@272 | 350 | self.get_namespace()._new_branchpoint() |
paul@272 | 351 | |
paul@311 | 352 | def new_branch(self, loop_node=None): |
paul@311 | 353 | self.get_namespace()._new_branch(loop_node) |
paul@272 | 354 | |
paul@299 | 355 | def abandon_branch(self): |
paul@299 | 356 | self.get_namespace()._abandon_branch() |
paul@299 | 357 | |
paul@272 | 358 | def shelve_branch(self): |
paul@272 | 359 | self.get_namespace()._shelve_branch() |
paul@272 | 360 | |
paul@272 | 361 | def merge_branches(self): |
paul@272 | 362 | self.get_namespace()._merge_branches() |
paul@272 | 363 | |
paul@278 | 364 | def define_attribute_user(self, node): |
paul@279 | 365 | |
paul@279 | 366 | """ |
paul@279 | 367 | Define 'node' as the user of attributes, indicating the point where the |
paul@279 | 368 | user is defined. |
paul@279 | 369 | """ |
paul@279 | 370 | |
paul@278 | 371 | self.get_namespace()._define_attribute_user(node) |
paul@272 | 372 | |
paul@280 | 373 | def use_attribute(self, name, attrname): |
paul@323 | 374 | |
paul@323 | 375 | "Note usage on the attribute user 'name' of the attribute 'attrname'." |
paul@323 | 376 | |
paul@280 | 377 | return self.get_namespace()._use_attribute(name, attrname) |
paul@272 | 378 | |
paul@347 | 379 | def use_specific_attribute(self, objname, attrname): |
paul@347 | 380 | |
paul@347 | 381 | """ |
paul@347 | 382 | Note usage on the object having the given 'objname' of the attribute |
paul@351 | 383 | 'attrname'. If 'objname' is None, the current namespace is chosen as the |
paul@351 | 384 | object providing the attribute. |
paul@347 | 385 | """ |
paul@347 | 386 | |
paul@347 | 387 | return self.get_namespace()._use_specific_attribute(objname, attrname) |
paul@347 | 388 | |
paul@3 | 389 | # Visitor methods. |
paul@3 | 390 | |
paul@3 | 391 | def default(self, node, *args): |
paul@339 | 392 | raise InspectError("Node class %r is not supported." % node.__class__) |
paul@3 | 393 | |
paul@0 | 394 | def NOP(self, node): |
paul@21 | 395 | for n in node.getChildNodes(): |
paul@21 | 396 | self.dispatch(n) |
paul@19 | 397 | return None |
paul@0 | 398 | |
paul@299 | 399 | def NOP_ABANDON(self, node): |
paul@302 | 400 | self.NOP(node) |
paul@299 | 401 | self.abandon_branch() |
paul@299 | 402 | |
paul@82 | 403 | def OP(self, node): |
paul@82 | 404 | for n in node.getChildNodes(): |
paul@82 | 405 | self.dispatch(n) |
paul@82 | 406 | return Instance() |
paul@1 | 407 | |
paul@314 | 408 | # Generic support for classes of operations. |
paul@314 | 409 | |
paul@354 | 410 | def _ensureOperators(self): |
paul@358 | 411 | attr, scope, namespace = self._get_with_scope("$operator") |
paul@358 | 412 | if attr is None: |
paul@354 | 413 | module = self.importer.load("operator") |
paul@354 | 414 | self["$operator"] = module |
paul@358 | 415 | else: |
paul@358 | 416 | module = attr.get_value() |
paul@358 | 417 | return module |
paul@354 | 418 | |
paul@172 | 419 | def _visitUnary(self, node): |
paul@172 | 420 | |
paul@358 | 421 | "Accounting method for the operator 'node'." |
paul@172 | 422 | |
paul@354 | 423 | operator_module = self._ensureOperators() |
paul@354 | 424 | operator_fn = operator_functions[node.__class__.__name__] |
paul@354 | 425 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@172 | 426 | return self.OP(node) |
paul@172 | 427 | |
paul@358 | 428 | _visitBinary = _visitUnary |
paul@358 | 429 | |
paul@85 | 430 | def _visitFunction(self, node, name): |
paul@85 | 431 | |
paul@85 | 432 | """ |
paul@85 | 433 | Return a function object for the function defined by 'node' with the |
paul@85 | 434 | given 'name'. If a lambda expression is being visited, 'name' should be |
paul@85 | 435 | None. |
paul@85 | 436 | """ |
paul@85 | 437 | |
paul@233 | 438 | # Define the function object. |
paul@233 | 439 | |
paul@85 | 440 | function = Function( |
paul@85 | 441 | name, |
paul@272 | 442 | self.get_namespace(), |
paul@85 | 443 | node.argnames, |
paul@85 | 444 | node.defaults, |
paul@85 | 445 | (node.flags & 4 != 0), |
paul@85 | 446 | (node.flags & 8 != 0), |
paul@331 | 447 | self.in_loop or self.in_function, |
paul@85 | 448 | self, |
paul@85 | 449 | node |
paul@85 | 450 | ) |
paul@85 | 451 | |
paul@233 | 452 | self.add_object(function, any_scope=1) |
paul@233 | 453 | |
paul@85 | 454 | # Make a back reference from the node for code generation. |
paul@85 | 455 | |
paul@85 | 456 | node.unit = function |
paul@85 | 457 | |
paul@85 | 458 | # Process the defaults. |
paul@85 | 459 | |
paul@85 | 460 | for n in node.defaults: |
paul@85 | 461 | self.expr = self.dispatch(n) |
paul@192 | 462 | function.store_default(self.expr) |
paul@85 | 463 | |
paul@368 | 464 | # Note attribute usage where tuple parameters are involved. |
paul@368 | 465 | |
paul@368 | 466 | if function.tuple_parameters(): |
paul@368 | 467 | self.use_name("__getitem__", node) |
paul@368 | 468 | |
paul@368 | 469 | # Record the namespace context of the function for later processing. |
paul@368 | 470 | |
paul@235 | 471 | self.functions.append((node, self.namespaces + [function])) |
paul@85 | 472 | |
paul@330 | 473 | # Store the function. |
paul@330 | 474 | |
paul@85 | 475 | if name is not None: |
paul@85 | 476 | self.store(name, function) |
paul@85 | 477 | else: |
paul@85 | 478 | self.store_lambda(function) |
paul@167 | 479 | |
paul@330 | 480 | # Test the defaults and assess whether an dynamic object will result. |
paul@330 | 481 | |
paul@331 | 482 | function.make_dynamic() |
paul@331 | 483 | return function |
paul@85 | 484 | |
paul@235 | 485 | def _visitFunctionBody(self, node, namespaces): |
paul@235 | 486 | |
paul@235 | 487 | "Enter the function." |
paul@235 | 488 | |
paul@235 | 489 | # Current namespace is the function. |
paul@235 | 490 | # Previous namespace is the class. |
paul@235 | 491 | |
paul@330 | 492 | if self.in_class(namespaces): |
paul@235 | 493 | self.in_method = 1 |
paul@235 | 494 | |
paul@331 | 495 | in_function = self.in_function |
paul@331 | 496 | in_loop = self.in_loop |
paul@331 | 497 | self.in_function = 1 |
paul@331 | 498 | self.in_loop = 0 |
paul@331 | 499 | |
paul@235 | 500 | self.namespaces = namespaces |
paul@235 | 501 | self.dispatch(node.code) |
paul@235 | 502 | |
paul@331 | 503 | self.in_loop = in_loop |
paul@331 | 504 | self.in_function = in_function |
paul@235 | 505 | self.in_method = 0 |
paul@235 | 506 | |
paul@172 | 507 | # Specific handler methods. |
paul@172 | 508 | |
paul@172 | 509 | visitAdd = _visitBinary |
paul@82 | 510 | |
paul@82 | 511 | visitAnd = OP |
paul@0 | 512 | |
paul@4 | 513 | visitAssert = NOP |
paul@4 | 514 | |
paul@0 | 515 | def visitAssign(self, node): |
paul@0 | 516 | self.expr = self.dispatch(node.expr) |
paul@0 | 517 | for n in node.nodes: |
paul@0 | 518 | self.dispatch(n) |
paul@0 | 519 | return None |
paul@0 | 520 | |
paul@0 | 521 | def visitAssAttr(self, node): |
paul@0 | 522 | expr = self.dispatch(node.expr) |
paul@272 | 523 | |
paul@272 | 524 | # Record the attribute on the presumed target. |
paul@272 | 525 | |
paul@94 | 526 | if isinstance(expr, Attr): |
paul@94 | 527 | if expr.name == "self": |
paul@94 | 528 | if not self.store_class_attr(node.attrname): |
paul@94 | 529 | self.store_instance_attr(node.attrname) |
paul@192 | 530 | elif isinstance(expr.get_value(), Module): |
paul@192 | 531 | self.store_module_attr(node.attrname, expr.get_value()) |
paul@192 | 532 | print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) |
paul@272 | 533 | |
paul@285 | 534 | # Note usage of the attribute where a local is involved. |
paul@272 | 535 | |
paul@272 | 536 | if expr.parent is self.get_namespace(): |
paul@309 | 537 | node._attrusers = self.use_attribute(expr.name, node.attrname) |
paul@309 | 538 | node._username = expr.name |
paul@272 | 539 | |
paul@0 | 540 | return None |
paul@0 | 541 | |
paul@0 | 542 | def visitAssList(self, node): |
paul@204 | 543 | |
paul@204 | 544 | # Declare names which will be used by generated code. |
paul@204 | 545 | |
paul@323 | 546 | self.use_name("__getitem__", node) |
paul@204 | 547 | |
paul@204 | 548 | # Process the assignment. |
paul@204 | 549 | |
paul@139 | 550 | for i, n in enumerate(node.nodes): |
paul@0 | 551 | self.dispatch(n) |
paul@168 | 552 | self.importer.make_constant(i) # for __getitem__(i) at run-time |
paul@0 | 553 | return None |
paul@0 | 554 | |
paul@0 | 555 | def visitAssName(self, node): |
paul@289 | 556 | if hasattr(node, "flags") and node.flags == "OP_DELETE": |
paul@298 | 557 | print "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) |
paul@339 | 558 | #raise InspectError("Deletion of attribute %r is not supported." % node.name) |
paul@274 | 559 | |
paul@120 | 560 | self.store(node.name, self.expr) |
paul@278 | 561 | self.define_attribute_user(node) |
paul@285 | 562 | |
paul@285 | 563 | # Ensure the presence of the given name in this namespace. |
paul@285 | 564 | |
paul@285 | 565 | self.use_specific_attribute(None, node.name) |
paul@0 | 566 | return None |
paul@0 | 567 | |
paul@0 | 568 | visitAssTuple = visitAssList |
paul@0 | 569 | |
paul@172 | 570 | def visitAugAssign(self, node): |
paul@172 | 571 | |
paul@172 | 572 | # Accounting. |
paul@172 | 573 | |
paul@354 | 574 | operator_fn = operator_functions.get(node.op) |
paul@354 | 575 | operator_module = self._ensureOperators() |
paul@354 | 576 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@244 | 577 | |
paul@244 | 578 | # Process the assignment. |
paul@244 | 579 | |
paul@244 | 580 | self.expr = self.dispatch(node.expr) |
paul@244 | 581 | |
paul@244 | 582 | # NOTE: Similar to micropython.ast handler code. |
paul@363 | 583 | # NOTE: Slices and subscripts are supported by __setitem__(slice) and |
paul@363 | 584 | # NOTE: not __setslice__. |
paul@244 | 585 | |
paul@244 | 586 | if isinstance(node.node, compiler.ast.Name): |
paul@244 | 587 | self.visitAssName(node.node) |
paul@244 | 588 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@244 | 589 | self.visitAssAttr(node.node) |
paul@244 | 590 | else: |
paul@363 | 591 | self.use_specific_attribute("__builtins__", "slice") |
paul@363 | 592 | self.use_name("__setitem__", node) |
paul@244 | 593 | |
paul@244 | 594 | return None |
paul@2 | 595 | |
paul@82 | 596 | visitBackquote = OP |
paul@4 | 597 | |
paul@172 | 598 | visitBitand = _visitBinary |
paul@2 | 599 | |
paul@172 | 600 | visitBitor = _visitBinary |
paul@2 | 601 | |
paul@172 | 602 | visitBitxor = _visitBinary |
paul@4 | 603 | |
paul@299 | 604 | visitBreak = NOP_ABANDON |
paul@4 | 605 | |
paul@82 | 606 | visitCallFunc = OP |
paul@0 | 607 | |
paul@0 | 608 | def visitClass(self, node): |
paul@91 | 609 | |
paul@91 | 610 | """ |
paul@91 | 611 | Register the class at the given 'node' subject to the restrictions |
paul@91 | 612 | mentioned in the module docstring. |
paul@91 | 613 | """ |
paul@91 | 614 | |
paul@12 | 615 | if self.namespaces: |
paul@363 | 616 | print "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@82 | 617 | return None |
paul@0 | 618 | else: |
paul@331 | 619 | if self.in_loop: |
paul@331 | 620 | print "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) |
paul@331 | 621 | |
paul@272 | 622 | cls = Class(node.name, self.get_namespace(), self, node) |
paul@37 | 623 | |
paul@349 | 624 | # Make a back reference from the node for code generation. |
paul@349 | 625 | |
paul@349 | 626 | node.unit = cls |
paul@349 | 627 | |
paul@349 | 628 | # Process base classes in the context of the class's namespace. |
paul@349 | 629 | # This confines references to such classes to the class instead of |
paul@349 | 630 | # the namespace in which it is defined. |
paul@349 | 631 | |
paul@349 | 632 | self.namespaces.append(cls) |
paul@349 | 633 | |
paul@37 | 634 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 635 | # definitions of classes. |
paul@37 | 636 | |
paul@0 | 637 | for base in node.bases: |
paul@36 | 638 | expr = self.dispatch(base) |
paul@349 | 639 | |
paul@349 | 640 | # Each base class must be constant and known at compile-time. |
paul@349 | 641 | |
paul@36 | 642 | if isinstance(expr, Attr): |
paul@36 | 643 | if expr.assignments != 1: |
paul@339 | 644 | raise InspectError("Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 645 | else: |
paul@192 | 646 | cls.add_base(expr.get_value()) |
paul@349 | 647 | |
paul@349 | 648 | # Where no expression value is available, the base class is |
paul@349 | 649 | # not identifiable. |
paul@349 | 650 | |
paul@349 | 651 | else: |
paul@339 | 652 | raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 653 | |
paul@59 | 654 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 655 | # If no bases exist, adopt the 'object' class. |
paul@59 | 656 | |
paul@61 | 657 | if not node.bases and not (self.name == "__builtins__" and node.name == "object") : |
paul@59 | 658 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@192 | 659 | cls.add_base(expr.get_value()) |
paul@59 | 660 | |
paul@349 | 661 | # Make an entry for the class in the parent namespace. |
paul@21 | 662 | |
paul@349 | 663 | self.namespaces.pop() |
paul@0 | 664 | self.store(node.name, cls) |
paul@167 | 665 | self.add_object(cls) |
paul@0 | 666 | |
paul@349 | 667 | # Process the class body in its own namespace. |
paul@59 | 668 | |
paul@12 | 669 | self.namespaces.append(cls) |
paul@0 | 670 | self.dispatch(node.code) |
paul@12 | 671 | self.namespaces.pop() |
paul@0 | 672 | |
paul@349 | 673 | cls.finalise_attribute_usage() |
paul@82 | 674 | return cls |
paul@0 | 675 | |
paul@172 | 676 | def visitCompare(self, node): |
paul@172 | 677 | |
paul@172 | 678 | # Accounting. |
paul@172 | 679 | # NOTE: Replicates some code in micropython.ast.visitCompare. |
paul@172 | 680 | |
paul@323 | 681 | this_node = node |
paul@323 | 682 | |
paul@172 | 683 | for op in node.ops: |
paul@172 | 684 | op_name, next_node = op |
paul@323 | 685 | |
paul@354 | 686 | # Define name/attribute usage. |
paul@354 | 687 | # Get the applicable operation. |
paul@323 | 688 | |
paul@354 | 689 | operator_fn = operator_functions.get(op_name) |
paul@354 | 690 | |
paul@354 | 691 | # For operators, reference the specific function involved. |
paul@323 | 692 | |
paul@354 | 693 | if operator_fn is not None: |
paul@354 | 694 | operator_module = self._ensureOperators() |
paul@354 | 695 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@323 | 696 | |
paul@354 | 697 | # Define __contains__ usage on the next node. |
paul@354 | 698 | |
paul@172 | 699 | elif op_name.endswith("in"): |
paul@323 | 700 | self.use_name("__contains__", next_node) |
paul@323 | 701 | |
paul@323 | 702 | this_node = next_node |
paul@172 | 703 | |
paul@172 | 704 | return self.OP(node) |
paul@4 | 705 | |
paul@21 | 706 | def visitConst(self, node): |
paul@177 | 707 | |
paul@177 | 708 | # Register the constant, if necessary, returning the resulting object. |
paul@177 | 709 | |
paul@323 | 710 | self.use_name(self.importer.get_constant_type_name(node.value), node) |
paul@168 | 711 | return self.importer.make_constant(node.value) |
paul@0 | 712 | |
paul@299 | 713 | visitContinue = NOP_ABANDON |
paul@4 | 714 | |
paul@4 | 715 | visitDecorators = NOP |
paul@4 | 716 | |
paul@82 | 717 | visitDict = OP |
paul@0 | 718 | |
paul@0 | 719 | visitDiscard = NOP |
paul@0 | 720 | |
paul@172 | 721 | visitDiv = _visitBinary |
paul@4 | 722 | |
paul@4 | 723 | visitEllipsis = NOP |
paul@4 | 724 | |
paul@4 | 725 | visitExec = NOP |
paul@4 | 726 | |
paul@82 | 727 | visitExpression = OP |
paul@4 | 728 | |
paul@172 | 729 | visitFloorDiv = _visitBinary |
paul@4 | 730 | |
paul@34 | 731 | def visitFor(self, node): |
paul@272 | 732 | self.new_branchpoint() |
paul@204 | 733 | |
paul@204 | 734 | # Declare names which will be used by generated code. |
paul@204 | 735 | |
paul@323 | 736 | self.use_name("__iter__", node.list) |
paul@250 | 737 | self.use_name("next") |
paul@345 | 738 | self.use_name("StopIteration") |
paul@204 | 739 | |
paul@331 | 740 | in_loop = self.in_loop |
paul@289 | 741 | self.in_loop = 1 |
paul@341 | 742 | self.dispatch(node.list) |
paul@272 | 743 | self.dispatch(node.assign) |
paul@272 | 744 | |
paul@204 | 745 | # Enter the loop. |
paul@275 | 746 | # Propagate attribute usage to branches. |
paul@204 | 747 | |
paul@366 | 748 | self.new_branch(node) |
paul@272 | 749 | self.dispatch(node.body) |
paul@272 | 750 | self.shelve_branch() |
paul@362 | 751 | |
paul@331 | 752 | self.in_loop = in_loop |
paul@272 | 753 | |
paul@275 | 754 | # Maintain a branch for the else clause or the current retained usage |
paul@275 | 755 | # where execution avoids the conditional clauses. |
paul@275 | 756 | |
paul@275 | 757 | self.new_branch() |
paul@272 | 758 | if node.else_ is not None: |
paul@272 | 759 | self.dispatch(node.else_) |
paul@275 | 760 | self.shelve_branch() |
paul@272 | 761 | |
paul@272 | 762 | self.merge_branches() |
paul@82 | 763 | return None |
paul@0 | 764 | |
paul@0 | 765 | def visitFrom(self, node): |
paul@0 | 766 | module = self.importer.load(node.modname, 1) |
paul@0 | 767 | |
paul@35 | 768 | #if module is None: |
paul@35 | 769 | # print "Warning:", node.modname, "not imported." |
paul@0 | 770 | |
paul@0 | 771 | for name, alias in node.names: |
paul@0 | 772 | if name != "*": |
paul@285 | 773 | if module is not None and module.has_key(name): |
paul@24 | 774 | attr = module[name] |
paul@192 | 775 | self.store(alias or name, attr) |
paul@364 | 776 | self.use_specific_attribute(module.full_name(), name) |
paul@192 | 777 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@192 | 778 | self.importer.load(attr.get_value().name) |
paul@24 | 779 | |
paul@24 | 780 | # Support the import of names from missing modules. |
paul@24 | 781 | |
paul@3 | 782 | else: |
paul@24 | 783 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 784 | else: |
paul@3 | 785 | if module is not None: |
paul@285 | 786 | for n in module.keys(): |
paul@24 | 787 | attr = module[n] |
paul@192 | 788 | self.store(n, attr) |
paul@364 | 789 | self.use_specific_attribute(module.full_name(), n) |
paul@192 | 790 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@192 | 791 | self.importer.load(attr.get_value().name) |
paul@0 | 792 | |
paul@0 | 793 | return None |
paul@0 | 794 | |
paul@0 | 795 | def visitFunction(self, node): |
paul@85 | 796 | return self._visitFunction(node, node.name) |
paul@0 | 797 | |
paul@82 | 798 | visitGenExpr = OP |
paul@4 | 799 | |
paul@4 | 800 | visitGenExprFor = NOP |
paul@4 | 801 | |
paul@4 | 802 | visitGenExprIf = NOP |
paul@4 | 803 | |
paul@4 | 804 | visitGenExprInner = NOP |
paul@4 | 805 | |
paul@0 | 806 | def visitGetattr(self, node): |
paul@0 | 807 | expr = self.dispatch(node.expr) |
paul@169 | 808 | attrname = node.attrname |
paul@169 | 809 | |
paul@272 | 810 | # Attempt to identify the nature of the attribute. |
paul@272 | 811 | |
paul@36 | 812 | if isinstance(expr, Attr): |
paul@192 | 813 | value = expr.get_value() |
paul@285 | 814 | |
paul@285 | 815 | # Get the attribute and record its usage. |
paul@285 | 816 | |
paul@87 | 817 | if isinstance(value, (Class, Module)): |
paul@285 | 818 | attr = value.get(attrname) |
paul@285 | 819 | self.use_specific_attribute(value.full_name(), attrname) |
paul@285 | 820 | |
paul@36 | 821 | elif isinstance(value, UnresolvedName): |
paul@169 | 822 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@285 | 823 | |
paul@87 | 824 | else: |
paul@167 | 825 | attr = None |
paul@272 | 826 | |
paul@285 | 827 | # Note usage of the attribute where a local is involved. |
paul@272 | 828 | |
paul@285 | 829 | if expr.parent is self.get_namespace(): |
paul@309 | 830 | node._attrusers = self.use_attribute(expr.name, attrname) |
paul@309 | 831 | node._username = expr.name |
paul@285 | 832 | else: |
paul@323 | 833 | self.use_name(attrname, node.expr) |
paul@272 | 834 | |
paul@167 | 835 | elif self.builtins is not None: |
paul@169 | 836 | attr = self.builtins.get(attrname) |
paul@285 | 837 | self.use_specific_attribute(self.builtins.full_name(), attrname) |
paul@280 | 838 | |
paul@61 | 839 | else: |
paul@169 | 840 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@169 | 841 | |
paul@167 | 842 | return attr |
paul@4 | 843 | |
paul@12 | 844 | def visitGlobal(self, node): |
paul@12 | 845 | if self.namespaces: |
paul@12 | 846 | for name in node.names: |
paul@178 | 847 | ns = self.namespaces[-1] |
paul@178 | 848 | if not ns.make_global(name): |
paul@339 | 849 | raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) |
paul@37 | 850 | |
paul@236 | 851 | # The name is recorded in an earlier process. |
paul@0 | 852 | |
paul@0 | 853 | def visitIf(self, node): |
paul@272 | 854 | self.new_branchpoint() |
paul@272 | 855 | |
paul@275 | 856 | # Propagate attribute usage to branches. |
paul@275 | 857 | |
paul@0 | 858 | for test, body in node.tests: |
paul@202 | 859 | self.dispatch(test) |
paul@272 | 860 | |
paul@272 | 861 | self.new_branch() |
paul@0 | 862 | self.dispatch(body) |
paul@272 | 863 | self.shelve_branch() |
paul@272 | 864 | |
paul@275 | 865 | # Maintain a branch for the else clause or the current retained usage |
paul@275 | 866 | # where execution avoids the conditional clauses. |
paul@275 | 867 | |
paul@275 | 868 | self.new_branch() |
paul@0 | 869 | if node.else_ is not None: |
paul@0 | 870 | self.dispatch(node.else_) |
paul@275 | 871 | self.shelve_branch() |
paul@272 | 872 | |
paul@272 | 873 | self.merge_branches() |
paul@0 | 874 | return None |
paul@0 | 875 | |
paul@12 | 876 | visitIfExp = NOP |
paul@12 | 877 | |
paul@0 | 878 | def visitImport(self, node): |
paul@0 | 879 | for name, alias in node.names: |
paul@2 | 880 | if alias is not None: |
paul@167 | 881 | module = self.importer.load(name, 1) or UnresolvedName(None, name, self) |
paul@167 | 882 | self.store(alias, module) |
paul@2 | 883 | else: |
paul@167 | 884 | module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) |
paul@167 | 885 | self.store(name.split(".")[0], module) |
paul@0 | 886 | |
paul@0 | 887 | return None |
paul@0 | 888 | |
paul@172 | 889 | visitInvert = _visitUnary |
paul@4 | 890 | |
paul@59 | 891 | def visitKeyword(self, node): |
paul@59 | 892 | self.dispatch(node.expr) |
paul@168 | 893 | self.importer.make_constant(node.name) |
paul@71 | 894 | self.keyword_names.add(node.name) |
paul@59 | 895 | return None |
paul@4 | 896 | |
paul@85 | 897 | def visitLambda(self, node): |
paul@351 | 898 | fn = self._visitFunction(node, None) |
paul@351 | 899 | self.use_specific_attribute(None, fn.name) |
paul@351 | 900 | return fn |
paul@4 | 901 | |
paul@172 | 902 | visitLeftShift = _visitBinary |
paul@1 | 903 | |
paul@346 | 904 | def visitList(self, node): |
paul@347 | 905 | self.use_specific_attribute("__builtins__", "list") |
paul@346 | 906 | self.OP(node) |
paul@0 | 907 | |
paul@341 | 908 | def visitListComp(self, node): |
paul@341 | 909 | for qual in node.quals: |
paul@341 | 910 | self.dispatch(qual) |
paul@341 | 911 | self.dispatch(node.expr) |
paul@341 | 912 | |
paul@341 | 913 | def visitListCompFor(self, node): |
paul@341 | 914 | self.new_branchpoint() |
paul@341 | 915 | |
paul@341 | 916 | # Declare names which will be used by generated code. |
paul@341 | 917 | |
paul@341 | 918 | self.use_name("__iter__", node.list) |
paul@341 | 919 | self.use_name("next") |
paul@4 | 920 | |
paul@341 | 921 | in_loop = self.in_loop |
paul@341 | 922 | self.in_loop = 1 |
paul@341 | 923 | self.dispatch(node.list) |
paul@341 | 924 | self.dispatch(node.assign) |
paul@341 | 925 | |
paul@341 | 926 | # Enter the loop. |
paul@341 | 927 | # Propagate attribute usage to branches. |
paul@341 | 928 | |
paul@366 | 929 | self.new_branch(node) |
paul@341 | 930 | |
paul@341 | 931 | for if_ in node.ifs: |
paul@341 | 932 | self.dispatch(if_) |
paul@341 | 933 | |
paul@341 | 934 | self.shelve_branch() |
paul@341 | 935 | self.in_loop = in_loop |
paul@341 | 936 | |
paul@341 | 937 | self.merge_branches() |
paul@341 | 938 | return None |
paul@4 | 939 | |
paul@4 | 940 | visitListCompIf = NOP |
paul@4 | 941 | |
paul@172 | 942 | visitMod = _visitBinary |
paul@4 | 943 | |
paul@0 | 944 | def visitModule(self, node): |
paul@143 | 945 | |
paul@143 | 946 | # Make a back reference from the node for code generation. |
paul@143 | 947 | |
paul@143 | 948 | node.unit = self |
paul@0 | 949 | return self.dispatch(node.node) |
paul@0 | 950 | |
paul@172 | 951 | visitMul = _visitBinary |
paul@2 | 952 | |
paul@0 | 953 | def visitName(self, node): |
paul@343 | 954 | return self.get_namespace().get_using_node(node.name, node) |
paul@0 | 955 | |
paul@82 | 956 | visitNot = OP |
paul@0 | 957 | |
paul@82 | 958 | visitOr = OP |
paul@0 | 959 | |
paul@0 | 960 | visitPass = NOP |
paul@0 | 961 | |
paul@172 | 962 | visitPower = _visitBinary |
paul@4 | 963 | |
paul@4 | 964 | visitPrint = NOP |
paul@4 | 965 | |
paul@4 | 966 | visitPrintnl = NOP |
paul@4 | 967 | |
paul@299 | 968 | visitRaise = NOP_ABANDON |
paul@0 | 969 | |
paul@299 | 970 | visitReturn = NOP_ABANDON |
paul@4 | 971 | |
paul@172 | 972 | visitRightShift = _visitBinary |
paul@1 | 973 | |
paul@342 | 974 | def visitSlice(self, node): |
paul@347 | 975 | self.use_specific_attribute("__builtins__", "slice") |
paul@342 | 976 | self.use_name("__getitem__", node) |
paul@342 | 977 | self.OP(node) |
paul@4 | 978 | |
paul@82 | 979 | visitSliceobj = OP |
paul@23 | 980 | |
paul@0 | 981 | def visitStmt(self, node): |
paul@0 | 982 | for n in node.nodes: |
paul@0 | 983 | self.dispatch(n) |
paul@0 | 984 | return None |
paul@0 | 985 | |
paul@172 | 986 | visitSub = _visitBinary |
paul@4 | 987 | |
paul@216 | 988 | def visitSubscript(self, node): |
paul@323 | 989 | self.use_name("__getitem__", node) |
paul@216 | 990 | self.OP(node) |
paul@0 | 991 | |
paul@0 | 992 | def visitTryExcept(self, node): |
paul@0 | 993 | self.dispatch(node.body) |
paul@272 | 994 | |
paul@272 | 995 | self.new_branchpoint() |
paul@272 | 996 | |
paul@0 | 997 | for name, var, n in node.handlers: |
paul@272 | 998 | self.new_branch() |
paul@97 | 999 | |
paul@97 | 1000 | # Establish the local for the handler. |
paul@97 | 1001 | |
paul@99 | 1002 | if var is not None: |
paul@99 | 1003 | self.dispatch(var) |
paul@99 | 1004 | if n is not None: |
paul@99 | 1005 | self.dispatch(n) |
paul@272 | 1006 | |
paul@272 | 1007 | self.shelve_branch() |
paul@272 | 1008 | |
paul@362 | 1009 | self.new_branch() |
paul@0 | 1010 | if node.else_ is not None: |
paul@0 | 1011 | self.dispatch(node.else_) |
paul@362 | 1012 | self.shelve_branch() |
paul@272 | 1013 | |
paul@272 | 1014 | self.merge_branches() |
paul@0 | 1015 | return None |
paul@0 | 1016 | |
paul@4 | 1017 | visitTryFinally = NOP |
paul@4 | 1018 | |
paul@82 | 1019 | visitTuple = OP |
paul@1 | 1020 | |
paul@172 | 1021 | visitUnaryAdd = _visitUnary |
paul@4 | 1022 | |
paul@172 | 1023 | visitUnarySub = _visitUnary |
paul@4 | 1024 | |
paul@34 | 1025 | def visitWhile(self, node): |
paul@272 | 1026 | self.new_branchpoint() |
paul@272 | 1027 | |
paul@275 | 1028 | # Propagate attribute usage to branches. |
paul@275 | 1029 | |
paul@331 | 1030 | in_loop = self.in_loop |
paul@34 | 1031 | self.in_loop = 1 |
paul@272 | 1032 | self.dispatch(node.test) |
paul@311 | 1033 | self.new_branch(node) |
paul@272 | 1034 | self.dispatch(node.body) |
paul@272 | 1035 | self.shelve_branch() |
paul@331 | 1036 | self.in_loop = in_loop |
paul@272 | 1037 | |
paul@275 | 1038 | # Maintain a branch for the else clause or the current retained usage |
paul@275 | 1039 | # where execution avoids the conditional clauses. |
paul@311 | 1040 | # NOTE: Consider merging here before the else clause. |
paul@275 | 1041 | |
paul@275 | 1042 | self.new_branch() |
paul@272 | 1043 | if node.else_ is not None: |
paul@272 | 1044 | self.dispatch(node.else_) |
paul@275 | 1045 | self.shelve_branch() |
paul@272 | 1046 | |
paul@272 | 1047 | self.merge_branches() |
paul@82 | 1048 | return None |
paul@4 | 1049 | |
paul@4 | 1050 | visitWith = NOP |
paul@4 | 1051 | |
paul@4 | 1052 | visitYield = NOP |
paul@4 | 1053 | |
paul@0 | 1054 | # vim: tabstop=4 expandtab shiftwidth=4 |