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