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@31 | 6 | Copyright (C) 2007, 2008 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@91 | 70 | invoked many times with different inputs. However, there may be benefits in |
paul@91 | 71 | considering a local to be constant within a single invocation. |
paul@0 | 72 | """ |
paul@0 | 73 | |
paul@39 | 74 | from micropython.common import * |
paul@83 | 75 | from micropython.data import * |
paul@0 | 76 | import compiler.ast |
paul@0 | 77 | from compiler.visitor import ASTVisitor |
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@61 | 102 | self.builtins = self.importer.modules.get("__builtins__") |
paul@15 | 103 | self.loaded = 0 |
paul@15 | 104 | |
paul@82 | 105 | # Constant records. |
paul@82 | 106 | |
paul@82 | 107 | self.constant_values = importer.constant_values |
paul@82 | 108 | |
paul@1 | 109 | # Current expression state. |
paul@1 | 110 | |
paul@1 | 111 | self.expr = None |
paul@1 | 112 | |
paul@1 | 113 | # Namespace state. |
paul@1 | 114 | |
paul@34 | 115 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@91 | 116 | self.in_method = 0 # Find instance attributes in all methods. |
paul@34 | 117 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 118 | self.namespaces = [] |
paul@0 | 119 | self.module = None |
paul@0 | 120 | |
paul@0 | 121 | def parse(self, filename): |
paul@9 | 122 | |
paul@9 | 123 | "Parse the file having the given 'filename'." |
paul@9 | 124 | |
paul@0 | 125 | module = compiler.parseFile(filename) |
paul@0 | 126 | self.process(module) |
paul@0 | 127 | |
paul@0 | 128 | def process(self, module): |
paul@9 | 129 | |
paul@9 | 130 | "Process the given 'module'." |
paul@9 | 131 | |
paul@91 | 132 | self.astnode = self.module = module |
paul@1 | 133 | processed = self.dispatch(module) |
paul@13 | 134 | if self.has_key("__all__"): |
paul@12 | 135 | all = self["__all__"] |
paul@1 | 136 | if isinstance(all, compiler.ast.List): |
paul@1 | 137 | for n in all.nodes: |
paul@12 | 138 | self[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 139 | return processed |
paul@0 | 140 | |
paul@3 | 141 | def vacuum(self): |
paul@9 | 142 | |
paul@9 | 143 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 144 | |
paul@13 | 145 | for name, value in self.items(): |
paul@3 | 146 | if isinstance(value, Module) and not value.loaded: |
paul@12 | 147 | del self[name] |
paul@0 | 148 | |
paul@16 | 149 | # Complain about globals not initialised at the module level. |
paul@16 | 150 | |
paul@16 | 151 | if isinstance(value, Global): |
paul@16 | 152 | print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) |
paul@16 | 153 | |
paul@3 | 154 | # Namespace methods. |
paul@0 | 155 | |
paul@0 | 156 | def store(self, name, obj): |
paul@12 | 157 | |
paul@12 | 158 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 159 | |
paul@12 | 160 | if not self.namespaces: |
paul@34 | 161 | self.set(name, obj, not self.in_loop) |
paul@12 | 162 | else: |
paul@34 | 163 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 164 | |
paul@12 | 165 | # Record all non-local objects. |
paul@12 | 166 | |
paul@12 | 167 | if not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@33 | 168 | self.all_objects.add(obj) |
paul@0 | 169 | |
paul@85 | 170 | def store_lambda(self, obj): |
paul@91 | 171 | |
paul@91 | 172 | "Store a lambda function 'obj'." |
paul@91 | 173 | |
paul@85 | 174 | self.all_objects.add(obj) |
paul@85 | 175 | |
paul@94 | 176 | def store_module_attr(self, name, module): |
paul@94 | 177 | |
paul@94 | 178 | """ |
paul@94 | 179 | Record module attribute 'name' in the given 'module' using the current |
paul@94 | 180 | expression. |
paul@94 | 181 | """ |
paul@94 | 182 | |
paul@94 | 183 | if isinstance(self.expr, Attr): |
paul@94 | 184 | assigned_value = self.expr.value |
paul@94 | 185 | else: |
paul@94 | 186 | assigned_value = self.expr |
paul@94 | 187 | |
paul@94 | 188 | module.set(name, assigned_value, 0) |
paul@94 | 189 | |
paul@91 | 190 | def store_class_attr(self, name): |
paul@91 | 191 | |
paul@94 | 192 | """ |
paul@94 | 193 | Record class attribute 'name' in the current class using the current |
paul@94 | 194 | expression. |
paul@94 | 195 | """ |
paul@91 | 196 | |
paul@91 | 197 | if self.in_method and self.namespaces[-2].has_key(name): |
paul@91 | 198 | |
paul@91 | 199 | if isinstance(self.expr, Attr): |
paul@91 | 200 | assigned_value = self.expr.value |
paul@91 | 201 | else: |
paul@91 | 202 | assigned_value = self.expr |
paul@91 | 203 | |
paul@91 | 204 | self.namespaces[-2].set(name, assigned_value, 0) |
paul@91 | 205 | return 1 |
paul@91 | 206 | |
paul@91 | 207 | return 0 |
paul@91 | 208 | |
paul@34 | 209 | def store_instance_attr(self, name): |
paul@12 | 210 | |
paul@12 | 211 | "Record instance attribute 'name' in the current class." |
paul@12 | 212 | |
paul@91 | 213 | if self.in_method: |
paul@15 | 214 | |
paul@12 | 215 | # Current namespace is the function. |
paul@12 | 216 | # Previous namespace is the class. |
paul@15 | 217 | |
paul@12 | 218 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 219 | |
paul@7 | 220 | def get_parent(self): |
paul@71 | 221 | |
paul@71 | 222 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 223 | |
paul@12 | 224 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 225 | |
paul@3 | 226 | # Visitor methods. |
paul@3 | 227 | |
paul@3 | 228 | def default(self, node, *args): |
paul@39 | 229 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 230 | |
paul@3 | 231 | def dispatch(self, node, *args): |
paul@3 | 232 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 233 | |
paul@0 | 234 | def NOP(self, node): |
paul@21 | 235 | for n in node.getChildNodes(): |
paul@21 | 236 | self.dispatch(n) |
paul@19 | 237 | return None |
paul@0 | 238 | |
paul@82 | 239 | def OP(self, node): |
paul@82 | 240 | for n in node.getChildNodes(): |
paul@82 | 241 | self.dispatch(n) |
paul@82 | 242 | return Instance() |
paul@1 | 243 | |
paul@91 | 244 | def _make_constant(self, value): |
paul@91 | 245 | if not self.constant_values.has_key(value): |
paul@91 | 246 | const = Const(value) |
paul@91 | 247 | self.constant_values[value] = const |
paul@91 | 248 | return self.constant_values[value] |
paul@91 | 249 | |
paul@85 | 250 | def _visitFunction(self, node, name): |
paul@85 | 251 | |
paul@85 | 252 | """ |
paul@85 | 253 | Return a function object for the function defined by 'node' with the |
paul@85 | 254 | given 'name'. If a lambda expression is being visited, 'name' should be |
paul@85 | 255 | None. |
paul@85 | 256 | """ |
paul@85 | 257 | |
paul@85 | 258 | function = Function( |
paul@85 | 259 | name, |
paul@85 | 260 | self.get_parent(), |
paul@85 | 261 | node.argnames, |
paul@85 | 262 | node.defaults, |
paul@85 | 263 | (node.flags & 4 != 0), |
paul@85 | 264 | (node.flags & 8 != 0), |
paul@85 | 265 | self, |
paul@85 | 266 | node |
paul@85 | 267 | ) |
paul@85 | 268 | |
paul@85 | 269 | # Make a back reference from the node for code generation. |
paul@85 | 270 | |
paul@85 | 271 | node.unit = function |
paul@85 | 272 | |
paul@85 | 273 | # Process the defaults. |
paul@85 | 274 | |
paul@85 | 275 | for n in node.defaults: |
paul@85 | 276 | self.expr = self.dispatch(n) |
paul@85 | 277 | if isinstance(self.expr, Attr): |
paul@85 | 278 | function.store_default(self.expr.value) |
paul@85 | 279 | else: |
paul@85 | 280 | function.store_default(self.expr) |
paul@85 | 281 | |
paul@85 | 282 | # Enter the function. |
paul@85 | 283 | |
paul@85 | 284 | self.namespaces.append(function) |
paul@85 | 285 | |
paul@85 | 286 | # Current namespace is the function. |
paul@85 | 287 | # Previous namespace is the class. |
paul@85 | 288 | |
paul@91 | 289 | if len(self.namespaces) > 1 and isinstance(self.namespaces[-2], Class): |
paul@91 | 290 | if name == "__init__": |
paul@91 | 291 | self.in_init = 1 |
paul@91 | 292 | self.in_method = 1 |
paul@85 | 293 | |
paul@85 | 294 | self.dispatch(node.code) |
paul@85 | 295 | self.in_init = 0 |
paul@91 | 296 | self.in_method = 0 |
paul@85 | 297 | self.namespaces.pop() |
paul@85 | 298 | |
paul@85 | 299 | if name is not None: |
paul@85 | 300 | self.store(name, function) |
paul@85 | 301 | else: |
paul@85 | 302 | self.store_lambda(function) |
paul@109 | 303 | if node.defaults: |
paul@109 | 304 | return None # NOTE: See lambda code in the ast module. |
paul@85 | 305 | |
paul@85 | 306 | return function |
paul@85 | 307 | |
paul@82 | 308 | visitAdd = OP |
paul@82 | 309 | |
paul@82 | 310 | visitAnd = OP |
paul@0 | 311 | |
paul@4 | 312 | visitAssert = NOP |
paul@4 | 313 | |
paul@0 | 314 | def visitAssign(self, node): |
paul@0 | 315 | self.expr = self.dispatch(node.expr) |
paul@0 | 316 | for n in node.nodes: |
paul@0 | 317 | self.dispatch(n) |
paul@0 | 318 | return None |
paul@0 | 319 | |
paul@0 | 320 | def visitAssAttr(self, node): |
paul@0 | 321 | expr = self.dispatch(node.expr) |
paul@94 | 322 | if isinstance(expr, Attr): |
paul@94 | 323 | if expr.name == "self": |
paul@94 | 324 | if not self.store_class_attr(node.attrname): |
paul@94 | 325 | self.store_instance_attr(node.attrname) |
paul@94 | 326 | elif isinstance(expr.value, Module): |
paul@94 | 327 | self.store_module_attr(node.attrname, expr.value) |
paul@94 | 328 | print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.value.name) |
paul@0 | 329 | return None |
paul@0 | 330 | |
paul@0 | 331 | def visitAssList(self, node): |
paul@0 | 332 | for n in node.nodes: |
paul@0 | 333 | self.dispatch(n) |
paul@0 | 334 | return None |
paul@0 | 335 | |
paul@0 | 336 | def visitAssName(self, node): |
paul@36 | 337 | if isinstance(self.expr, Attr): |
paul@36 | 338 | self.store(node.name, self.expr.value) |
paul@36 | 339 | else: |
paul@36 | 340 | self.store(node.name, self.expr) |
paul@0 | 341 | return None |
paul@0 | 342 | |
paul@0 | 343 | visitAssTuple = visitAssList |
paul@0 | 344 | |
paul@82 | 345 | visitAugAssign = OP |
paul@2 | 346 | |
paul@82 | 347 | visitBackquote = OP |
paul@4 | 348 | |
paul@82 | 349 | visitBitand = OP |
paul@2 | 350 | |
paul@82 | 351 | visitBitor = OP |
paul@2 | 352 | |
paul@82 | 353 | visitBitxor = OP |
paul@4 | 354 | |
paul@4 | 355 | visitBreak = NOP |
paul@4 | 356 | |
paul@82 | 357 | visitCallFunc = OP |
paul@0 | 358 | |
paul@0 | 359 | def visitClass(self, node): |
paul@91 | 360 | |
paul@91 | 361 | """ |
paul@91 | 362 | Register the class at the given 'node' subject to the restrictions |
paul@91 | 363 | mentioned in the module docstring. |
paul@91 | 364 | """ |
paul@91 | 365 | |
paul@12 | 366 | if self.namespaces: |
paul@41 | 367 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@82 | 368 | return None |
paul@0 | 369 | else: |
paul@53 | 370 | cls = Class(node.name, self.get_parent(), self, node) |
paul@37 | 371 | |
paul@37 | 372 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 373 | # definitions of classes. |
paul@37 | 374 | |
paul@0 | 375 | for base in node.bases: |
paul@36 | 376 | expr = self.dispatch(base) |
paul@36 | 377 | if isinstance(expr, Attr): |
paul@36 | 378 | if expr.assignments != 1: |
paul@39 | 379 | raise InspectError(self.full_name(), node, |
paul@40 | 380 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 381 | else: |
paul@36 | 382 | cls.add_base(expr.value) |
paul@36 | 383 | else: # if expr is None: |
paul@39 | 384 | raise InspectError(self.full_name(), node, |
paul@40 | 385 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 386 | |
paul@59 | 387 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 388 | # If no bases exist, adopt the 'object' class. |
paul@59 | 389 | |
paul@61 | 390 | if not node.bases and not (self.name == "__builtins__" and node.name == "object") : |
paul@59 | 391 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@59 | 392 | cls.add_base(expr.value) |
paul@59 | 393 | |
paul@21 | 394 | # Make a back reference from the node for code generation. |
paul@21 | 395 | |
paul@39 | 396 | node.unit = cls |
paul@21 | 397 | |
paul@0 | 398 | # Make an entry for the class. |
paul@0 | 399 | |
paul@0 | 400 | self.store(node.name, cls) |
paul@0 | 401 | |
paul@59 | 402 | # Process the class body. |
paul@59 | 403 | |
paul@12 | 404 | self.namespaces.append(cls) |
paul@0 | 405 | self.dispatch(node.code) |
paul@12 | 406 | self.namespaces.pop() |
paul@0 | 407 | |
paul@82 | 408 | return cls |
paul@0 | 409 | |
paul@82 | 410 | visitCompare = OP |
paul@4 | 411 | |
paul@21 | 412 | def visitConst(self, node): |
paul@92 | 413 | return self._make_constant(node.value) |
paul@0 | 414 | |
paul@4 | 415 | visitContinue = NOP |
paul@4 | 416 | |
paul@4 | 417 | visitDecorators = NOP |
paul@4 | 418 | |
paul@82 | 419 | visitDict = OP |
paul@0 | 420 | |
paul@0 | 421 | visitDiscard = NOP |
paul@0 | 422 | |
paul@82 | 423 | visitDiv = OP |
paul@4 | 424 | |
paul@4 | 425 | visitEllipsis = NOP |
paul@4 | 426 | |
paul@4 | 427 | visitExec = NOP |
paul@4 | 428 | |
paul@82 | 429 | visitExpression = OP |
paul@4 | 430 | |
paul@82 | 431 | visitFloorDiv = OP |
paul@4 | 432 | |
paul@34 | 433 | def visitFor(self, node): |
paul@34 | 434 | self.in_loop = 1 |
paul@34 | 435 | self.NOP(node) |
paul@34 | 436 | self.in_loop = 0 |
paul@82 | 437 | return None |
paul@0 | 438 | |
paul@0 | 439 | def visitFrom(self, node): |
paul@0 | 440 | module = self.importer.load(node.modname, 1) |
paul@0 | 441 | |
paul@35 | 442 | #if module is None: |
paul@35 | 443 | # print "Warning:", node.modname, "not imported." |
paul@0 | 444 | |
paul@0 | 445 | for name, alias in node.names: |
paul@0 | 446 | if name != "*": |
paul@4 | 447 | if module is not None and module.namespace.has_key(name): |
paul@24 | 448 | attr = module[name] |
paul@36 | 449 | self.store(alias or name, attr.value) |
paul@2 | 450 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 451 | self.importer.load(attr.name) |
paul@24 | 452 | |
paul@24 | 453 | # Support the import of names from missing modules. |
paul@24 | 454 | |
paul@3 | 455 | else: |
paul@24 | 456 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 457 | else: |
paul@3 | 458 | if module is not None: |
paul@3 | 459 | for n in module.namespace.keys(): |
paul@24 | 460 | attr = module[n] |
paul@36 | 461 | self.store(n, attr.value) |
paul@3 | 462 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 463 | self.importer.load(attr.name) |
paul@0 | 464 | |
paul@0 | 465 | return None |
paul@0 | 466 | |
paul@0 | 467 | def visitFunction(self, node): |
paul@85 | 468 | return self._visitFunction(node, node.name) |
paul@0 | 469 | |
paul@82 | 470 | visitGenExpr = OP |
paul@4 | 471 | |
paul@4 | 472 | visitGenExprFor = NOP |
paul@4 | 473 | |
paul@4 | 474 | visitGenExprIf = NOP |
paul@4 | 475 | |
paul@4 | 476 | visitGenExprInner = NOP |
paul@4 | 477 | |
paul@0 | 478 | def visitGetattr(self, node): |
paul@0 | 479 | expr = self.dispatch(node.expr) |
paul@36 | 480 | if isinstance(expr, Attr): |
paul@36 | 481 | value = expr.value |
paul@87 | 482 | if isinstance(value, (Class, Module)): |
paul@36 | 483 | return value.namespace.get(node.attrname) |
paul@36 | 484 | elif isinstance(value, UnresolvedName): |
paul@36 | 485 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@87 | 486 | else: |
paul@87 | 487 | return None |
paul@61 | 488 | if self.builtins is not None: |
paul@61 | 489 | return self.builtins.get(node.attrname) |
paul@61 | 490 | else: |
paul@61 | 491 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@4 | 492 | |
paul@12 | 493 | def visitGlobal(self, node): |
paul@12 | 494 | if self.namespaces: |
paul@12 | 495 | for name in node.names: |
paul@12 | 496 | self.namespaces[-1].make_global(name) |
paul@37 | 497 | |
paul@37 | 498 | # Record a global entry for the name in the module. |
paul@37 | 499 | |
paul@15 | 500 | if not self.has_key(name): |
paul@16 | 501 | self[name] = Global() |
paul@0 | 502 | |
paul@0 | 503 | def visitIf(self, node): |
paul@0 | 504 | for test, body in node.tests: |
paul@0 | 505 | self.dispatch(body) |
paul@0 | 506 | if node.else_ is not None: |
paul@0 | 507 | self.dispatch(node.else_) |
paul@0 | 508 | return None |
paul@0 | 509 | |
paul@12 | 510 | visitIfExp = NOP |
paul@12 | 511 | |
paul@0 | 512 | def visitImport(self, node): |
paul@0 | 513 | for name, alias in node.names: |
paul@2 | 514 | if alias is not None: |
paul@24 | 515 | self.store(alias, self.importer.load(name, 1) or UnresolvedName(None, name, self)) |
paul@2 | 516 | else: |
paul@24 | 517 | self.store(name.split(".")[0], self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self)) |
paul@0 | 518 | |
paul@0 | 519 | return None |
paul@0 | 520 | |
paul@82 | 521 | visitInvert = OP |
paul@4 | 522 | |
paul@59 | 523 | def visitKeyword(self, node): |
paul@59 | 524 | self.dispatch(node.expr) |
paul@92 | 525 | self._make_constant(node.name) |
paul@71 | 526 | self.keyword_names.add(node.name) |
paul@59 | 527 | return None |
paul@4 | 528 | |
paul@85 | 529 | def visitLambda(self, node): |
paul@85 | 530 | return self._visitFunction(node, None) |
paul@4 | 531 | |
paul@82 | 532 | visitLeftShift = OP |
paul@1 | 533 | |
paul@82 | 534 | visitList = OP |
paul@0 | 535 | |
paul@82 | 536 | visitListComp = OP |
paul@4 | 537 | |
paul@4 | 538 | visitListCompFor = NOP |
paul@4 | 539 | |
paul@4 | 540 | visitListCompIf = NOP |
paul@4 | 541 | |
paul@82 | 542 | visitMod = OP |
paul@4 | 543 | |
paul@0 | 544 | def visitModule(self, node): |
paul@0 | 545 | return self.dispatch(node.node) |
paul@0 | 546 | |
paul@82 | 547 | visitMul = OP |
paul@2 | 548 | |
paul@0 | 549 | def visitName(self, node): |
paul@0 | 550 | name = node.name |
paul@82 | 551 | if name == "None": |
paul@91 | 552 | return self._make_constant(None) |
paul@82 | 553 | elif self.namespaces and self.namespaces[-1].has_key(name): |
paul@35 | 554 | return self.namespaces[-1][name] |
paul@13 | 555 | elif self.has_key(name): |
paul@12 | 556 | return self[name] |
paul@61 | 557 | elif self.builtins is not None and self.builtins.has_key(name): |
paul@61 | 558 | return self.builtins[name] |
paul@0 | 559 | else: |
paul@0 | 560 | return None |
paul@0 | 561 | |
paul@82 | 562 | visitNot = OP |
paul@0 | 563 | |
paul@82 | 564 | visitOr = OP |
paul@0 | 565 | |
paul@0 | 566 | visitPass = NOP |
paul@0 | 567 | |
paul@82 | 568 | visitPower = OP |
paul@4 | 569 | |
paul@4 | 570 | visitPrint = NOP |
paul@4 | 571 | |
paul@4 | 572 | visitPrintnl = NOP |
paul@4 | 573 | |
paul@0 | 574 | visitRaise = NOP |
paul@0 | 575 | |
paul@4 | 576 | visitReturn = NOP |
paul@4 | 577 | |
paul@82 | 578 | visitRightShift = OP |
paul@1 | 579 | |
paul@82 | 580 | visitSlice = OP |
paul@4 | 581 | |
paul@82 | 582 | visitSliceobj = OP |
paul@23 | 583 | |
paul@0 | 584 | def visitStmt(self, node): |
paul@0 | 585 | for n in node.nodes: |
paul@0 | 586 | self.dispatch(n) |
paul@0 | 587 | return None |
paul@0 | 588 | |
paul@82 | 589 | visitSub = OP |
paul@4 | 590 | |
paul@82 | 591 | visitSubscript = OP |
paul@0 | 592 | |
paul@0 | 593 | def visitTryExcept(self, node): |
paul@0 | 594 | self.dispatch(node.body) |
paul@0 | 595 | for name, var, n in node.handlers: |
paul@97 | 596 | |
paul@97 | 597 | # Establish the local for the handler. |
paul@97 | 598 | |
paul@99 | 599 | if var is not None: |
paul@99 | 600 | self.dispatch(var) |
paul@99 | 601 | if n is not None: |
paul@99 | 602 | self.dispatch(n) |
paul@0 | 603 | if node.else_ is not None: |
paul@0 | 604 | self.dispatch(node.else_) |
paul@0 | 605 | return None |
paul@0 | 606 | |
paul@4 | 607 | visitTryFinally = NOP |
paul@4 | 608 | |
paul@82 | 609 | visitTuple = OP |
paul@1 | 610 | |
paul@82 | 611 | visitUnaryAdd = OP |
paul@4 | 612 | |
paul@82 | 613 | visitUnarySub = OP |
paul@4 | 614 | |
paul@34 | 615 | def visitWhile(self, node): |
paul@34 | 616 | self.in_loop = 1 |
paul@34 | 617 | self.NOP(node) |
paul@34 | 618 | self.in_loop = 0 |
paul@82 | 619 | return None |
paul@4 | 620 | |
paul@4 | 621 | visitWith = NOP |
paul@4 | 622 | |
paul@4 | 623 | visitYield = NOP |
paul@4 | 624 | |
paul@16 | 625 | class Global: |
paul@16 | 626 | |
paul@16 | 627 | """ |
paul@16 | 628 | A reference to an object assigned to a global from outside the module |
paul@16 | 629 | top-level. |
paul@16 | 630 | """ |
paul@16 | 631 | |
paul@16 | 632 | pass |
paul@16 | 633 | |
paul@0 | 634 | # vim: tabstop=4 expandtab shiftwidth=4 |