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