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