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