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