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