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@235 | 117 | self.functions = [] |
paul@0 | 118 | |
paul@0 | 119 | def parse(self, filename): |
paul@9 | 120 | |
paul@9 | 121 | "Parse the file having the given 'filename'." |
paul@9 | 122 | |
paul@0 | 123 | module = compiler.parseFile(filename) |
paul@0 | 124 | self.process(module) |
paul@0 | 125 | |
paul@0 | 126 | def process(self, module): |
paul@9 | 127 | |
paul@9 | 128 | "Process the given 'module'." |
paul@9 | 129 | |
paul@91 | 130 | self.astnode = self.module = module |
paul@236 | 131 | |
paul@260 | 132 | # Add __name__ to the namespace by adding an explicit assignment to the |
paul@260 | 133 | # module. |
paul@260 | 134 | |
paul@260 | 135 | module.node.nodes.insert(0, compiler.ast.Assign( |
paul@260 | 136 | [compiler.ast.AssName("__name__", 0)], |
paul@260 | 137 | compiler.ast.Const(self.full_name()) |
paul@260 | 138 | )) |
paul@260 | 139 | |
paul@236 | 140 | # First, visit module-level code, recording global names. |
paul@236 | 141 | |
paul@1 | 142 | processed = self.dispatch(module) |
paul@235 | 143 | |
paul@236 | 144 | # Then, for each function, detect and record globals declared in those |
paul@236 | 145 | # functions. |
paul@236 | 146 | |
paul@236 | 147 | for node, namespaces in self.functions: |
paul@236 | 148 | self.process_globals(node) |
paul@236 | 149 | |
paul@236 | 150 | # Then, visit each function, recording other names. |
paul@236 | 151 | |
paul@235 | 152 | for node, namespaces in self.functions: |
paul@235 | 153 | self._visitFunctionBody(node, namespaces) |
paul@235 | 154 | |
paul@236 | 155 | # Add references to other modules declared using the __all__ global. |
paul@236 | 156 | |
paul@13 | 157 | if self.has_key("__all__"): |
paul@12 | 158 | all = self["__all__"] |
paul@1 | 159 | if isinstance(all, compiler.ast.List): |
paul@1 | 160 | for n in all.nodes: |
paul@167 | 161 | self.store(n.value, self.importer.add_module(self.name + "." + n.value)) |
paul@235 | 162 | |
paul@1 | 163 | return processed |
paul@0 | 164 | |
paul@236 | 165 | def process_globals(self, node): |
paul@236 | 166 | |
paul@236 | 167 | """ |
paul@236 | 168 | Within the given 'node', process global declarations, adjusting the |
paul@236 | 169 | module namespace. |
paul@236 | 170 | """ |
paul@236 | 171 | |
paul@236 | 172 | for n in node.getChildNodes(): |
paul@236 | 173 | if isinstance(n, compiler.ast.Global): |
paul@236 | 174 | for name in n.names: |
paul@236 | 175 | if not self.has_key(name): |
paul@236 | 176 | self[name] = None |
paul@236 | 177 | else: |
paul@236 | 178 | self.process_globals(n) |
paul@236 | 179 | |
paul@3 | 180 | def vacuum(self): |
paul@9 | 181 | |
paul@171 | 182 | """ |
paul@171 | 183 | Vacuum the module namespace, removing unreferenced objects and unused |
paul@171 | 184 | names. |
paul@171 | 185 | """ |
paul@9 | 186 | |
paul@241 | 187 | if self.should_optimise_unused_objects(): |
paul@252 | 188 | self.vacuum_object(self) |
paul@0 | 189 | |
paul@167 | 190 | all_objects = list(self.all_objects) |
paul@167 | 191 | |
paul@167 | 192 | for obj in all_objects: |
paul@252 | 193 | if isinstance(obj, Class): |
paul@252 | 194 | self.vacuum_object(obj) |
paul@171 | 195 | |
paul@252 | 196 | def vacuum_object(self, obj, delete_all=0): |
paul@241 | 197 | |
paul@252 | 198 | "Vacuum the given object 'obj'." |
paul@171 | 199 | |
paul@252 | 200 | for name, attr in obj.items(): |
paul@270 | 201 | |
paul@270 | 202 | # Only consider deleting entire unused objects or things accessible |
paul@270 | 203 | # via names which are never used. |
paul@270 | 204 | |
paul@252 | 205 | if delete_all or not self.importer.uses_name(name): |
paul@252 | 206 | del obj[name] |
paul@270 | 207 | |
paul@270 | 208 | # Delete any unambiguous attribute value. Such values can only |
paul@270 | 209 | # have been defined within the object and therefore are not |
paul@270 | 210 | # redefined by other code regions. |
paul@270 | 211 | |
paul@252 | 212 | if attr.assignments == 1: |
paul@252 | 213 | value = attr.get_value() |
paul@171 | 214 | |
paul@252 | 215 | if value is not obj and value in self.all_objects: |
paul@252 | 216 | self.all_objects.remove(value) |
paul@171 | 217 | |
paul@252 | 218 | # Delete class contents. |
paul@241 | 219 | |
paul@252 | 220 | if isinstance(value, Class): |
paul@252 | 221 | self.vacuum_object(value, 1) |
paul@171 | 222 | |
paul@171 | 223 | def finalise(self): |
paul@171 | 224 | |
paul@171 | 225 | "Finalise the module." |
paul@171 | 226 | |
paul@171 | 227 | for obj in self.all_objects: |
paul@171 | 228 | if isinstance(obj, (Class, Function)): |
paul@171 | 229 | obj.finalise_attributes() |
paul@171 | 230 | |
paul@167 | 231 | def add_object(self, obj, any_scope=0): |
paul@167 | 232 | |
paul@167 | 233 | """ |
paul@167 | 234 | Record 'obj' if non-local or if the optional 'any_scope' is set to a |
paul@167 | 235 | true value. |
paul@167 | 236 | """ |
paul@167 | 237 | |
paul@167 | 238 | if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@167 | 239 | self.all_objects.add(obj) |
paul@167 | 240 | |
paul@167 | 241 | # Optimisation tests. |
paul@167 | 242 | |
paul@167 | 243 | def should_optimise_unused_objects(self): |
paul@167 | 244 | return "unused_objects" in self.optimisations |
paul@167 | 245 | |
paul@3 | 246 | # Namespace methods. |
paul@0 | 247 | |
paul@0 | 248 | def store(self, name, obj): |
paul@12 | 249 | |
paul@12 | 250 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 251 | |
paul@12 | 252 | if not self.namespaces: |
paul@34 | 253 | self.set(name, obj, not self.in_loop) |
paul@12 | 254 | else: |
paul@34 | 255 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 256 | |
paul@85 | 257 | def store_lambda(self, obj): |
paul@91 | 258 | |
paul@91 | 259 | "Store a lambda function 'obj'." |
paul@91 | 260 | |
paul@167 | 261 | self.add_object(obj) |
paul@85 | 262 | |
paul@94 | 263 | def store_module_attr(self, name, module): |
paul@94 | 264 | |
paul@94 | 265 | """ |
paul@94 | 266 | Record module attribute 'name' in the given 'module' using the current |
paul@94 | 267 | expression. |
paul@94 | 268 | """ |
paul@94 | 269 | |
paul@192 | 270 | module.set(name, self.expr, 0) |
paul@94 | 271 | |
paul@91 | 272 | def store_class_attr(self, name): |
paul@91 | 273 | |
paul@94 | 274 | """ |
paul@94 | 275 | Record class attribute 'name' in the current class using the current |
paul@94 | 276 | expression. |
paul@94 | 277 | """ |
paul@91 | 278 | |
paul@91 | 279 | if self.in_method and self.namespaces[-2].has_key(name): |
paul@192 | 280 | self.namespaces[-2].set(name, self.expr, 0) |
paul@91 | 281 | return 1 |
paul@91 | 282 | |
paul@91 | 283 | return 0 |
paul@91 | 284 | |
paul@34 | 285 | def store_instance_attr(self, name): |
paul@12 | 286 | |
paul@12 | 287 | "Record instance attribute 'name' in the current class." |
paul@12 | 288 | |
paul@91 | 289 | if self.in_method: |
paul@15 | 290 | |
paul@12 | 291 | # Current namespace is the function. |
paul@12 | 292 | # Previous namespace is the class. |
paul@15 | 293 | |
paul@12 | 294 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 295 | |
paul@272 | 296 | def get_namespace(self): |
paul@71 | 297 | |
paul@71 | 298 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 299 | |
paul@12 | 300 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 301 | |
paul@250 | 302 | def use_name(self, name): |
paul@250 | 303 | |
paul@250 | 304 | "Use the given 'name' within the current namespace/unit." |
paul@250 | 305 | |
paul@272 | 306 | unit = self.get_namespace() |
paul@250 | 307 | self.importer.use_name(name, unit.name) |
paul@250 | 308 | |
paul@279 | 309 | # Attribute usage methods. |
paul@279 | 310 | # These are convenience methods which refer to the specific namespace's |
paul@279 | 311 | # implementation of these operations. |
paul@279 | 312 | |
paul@272 | 313 | def new_branchpoint(self): |
paul@272 | 314 | self.get_namespace()._new_branchpoint() |
paul@272 | 315 | |
paul@272 | 316 | def new_branch(self): |
paul@272 | 317 | self.get_namespace()._new_branch() |
paul@272 | 318 | |
paul@272 | 319 | def shelve_branch(self): |
paul@272 | 320 | self.get_namespace()._shelve_branch() |
paul@272 | 321 | |
paul@272 | 322 | def merge_branches(self): |
paul@272 | 323 | self.get_namespace()._merge_branches() |
paul@272 | 324 | |
paul@278 | 325 | def define_attribute_user(self, node): |
paul@279 | 326 | |
paul@279 | 327 | """ |
paul@279 | 328 | Define 'node' as the user of attributes, indicating the point where the |
paul@279 | 329 | user is defined. |
paul@279 | 330 | """ |
paul@279 | 331 | |
paul@278 | 332 | self.get_namespace()._define_attribute_user(node) |
paul@272 | 333 | |
paul@275 | 334 | def reset_all_attributes(self): |
paul@275 | 335 | self.get_namespace()._reset_all_attributes() |
paul@275 | 336 | |
paul@272 | 337 | def use_attribute(self, attr, attrname): |
paul@275 | 338 | return self.get_namespace()._use_attribute(attr, attrname) |
paul@272 | 339 | |
paul@3 | 340 | # Visitor methods. |
paul@3 | 341 | |
paul@3 | 342 | def default(self, node, *args): |
paul@39 | 343 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 344 | |
paul@3 | 345 | def dispatch(self, node, *args): |
paul@3 | 346 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 347 | |
paul@0 | 348 | def NOP(self, node): |
paul@21 | 349 | for n in node.getChildNodes(): |
paul@21 | 350 | self.dispatch(n) |
paul@19 | 351 | return None |
paul@0 | 352 | |
paul@82 | 353 | def OP(self, node): |
paul@82 | 354 | for n in node.getChildNodes(): |
paul@82 | 355 | self.dispatch(n) |
paul@82 | 356 | return Instance() |
paul@1 | 357 | |
paul@172 | 358 | def _visitUnary(self, node): |
paul@172 | 359 | |
paul@172 | 360 | "Accounting method for the unary operator 'node'." |
paul@172 | 361 | |
paul@172 | 362 | method = unary_methods[node.__class__.__name__] |
paul@250 | 363 | self.use_name(method) |
paul@172 | 364 | return self.OP(node) |
paul@172 | 365 | |
paul@172 | 366 | def _visitBinary(self, node): |
paul@172 | 367 | |
paul@172 | 368 | "Accounting method for the binary operator 'node'." |
paul@172 | 369 | |
paul@172 | 370 | left_method, right_method = binary_methods[node.__class__.__name__] |
paul@250 | 371 | self.use_name(left_method) |
paul@250 | 372 | self.use_name(right_method) |
paul@172 | 373 | return self.OP(node) |
paul@172 | 374 | |
paul@85 | 375 | def _visitFunction(self, node, name): |
paul@85 | 376 | |
paul@85 | 377 | """ |
paul@85 | 378 | Return a function object for the function defined by 'node' with the |
paul@85 | 379 | given 'name'. If a lambda expression is being visited, 'name' should be |
paul@85 | 380 | None. |
paul@85 | 381 | """ |
paul@85 | 382 | |
paul@233 | 383 | # Define the function object. |
paul@233 | 384 | |
paul@85 | 385 | function = Function( |
paul@85 | 386 | name, |
paul@272 | 387 | self.get_namespace(), |
paul@85 | 388 | node.argnames, |
paul@85 | 389 | node.defaults, |
paul@85 | 390 | (node.flags & 4 != 0), |
paul@85 | 391 | (node.flags & 8 != 0), |
paul@85 | 392 | self, |
paul@85 | 393 | node |
paul@85 | 394 | ) |
paul@85 | 395 | |
paul@233 | 396 | self.add_object(function, any_scope=1) |
paul@233 | 397 | |
paul@85 | 398 | # Make a back reference from the node for code generation. |
paul@85 | 399 | |
paul@85 | 400 | node.unit = function |
paul@85 | 401 | |
paul@85 | 402 | # Process the defaults. |
paul@85 | 403 | |
paul@85 | 404 | for n in node.defaults: |
paul@85 | 405 | self.expr = self.dispatch(n) |
paul@192 | 406 | function.store_default(self.expr) |
paul@85 | 407 | |
paul@235 | 408 | self.functions.append((node, self.namespaces + [function])) |
paul@85 | 409 | |
paul@85 | 410 | if name is not None: |
paul@85 | 411 | self.store(name, function) |
paul@85 | 412 | else: |
paul@85 | 413 | self.store_lambda(function) |
paul@167 | 414 | |
paul@167 | 415 | # Where defaults exist, an instance needs creating. Thus, it makes |
paul@167 | 416 | # no sense to return a reference to the function here, since the |
paul@167 | 417 | # recipient will not be referencing the function itself. |
paul@167 | 418 | |
paul@109 | 419 | if node.defaults: |
paul@167 | 420 | return Instance() # indicates no known target |
paul@85 | 421 | |
paul@85 | 422 | return function |
paul@85 | 423 | |
paul@235 | 424 | def _visitFunctionBody(self, node, namespaces): |
paul@235 | 425 | |
paul@235 | 426 | "Enter the function." |
paul@235 | 427 | |
paul@235 | 428 | # Current namespace is the function. |
paul@235 | 429 | # Previous namespace is the class. |
paul@235 | 430 | |
paul@235 | 431 | if len(namespaces) > 1 and isinstance(namespaces[-2], Class): |
paul@235 | 432 | if namespaces[-1].name == "__init__": |
paul@235 | 433 | self.in_init = 1 |
paul@235 | 434 | self.in_method = 1 |
paul@235 | 435 | |
paul@235 | 436 | self.namespaces = namespaces |
paul@235 | 437 | self.dispatch(node.code) |
paul@235 | 438 | |
paul@235 | 439 | self.in_init = 0 |
paul@235 | 440 | self.in_method = 0 |
paul@235 | 441 | |
paul@172 | 442 | # Specific handler methods. |
paul@172 | 443 | |
paul@172 | 444 | visitAdd = _visitBinary |
paul@82 | 445 | |
paul@82 | 446 | visitAnd = OP |
paul@0 | 447 | |
paul@4 | 448 | visitAssert = NOP |
paul@4 | 449 | |
paul@0 | 450 | def visitAssign(self, node): |
paul@0 | 451 | self.expr = self.dispatch(node.expr) |
paul@0 | 452 | for n in node.nodes: |
paul@0 | 453 | self.dispatch(n) |
paul@0 | 454 | return None |
paul@0 | 455 | |
paul@0 | 456 | def visitAssAttr(self, node): |
paul@0 | 457 | expr = self.dispatch(node.expr) |
paul@272 | 458 | |
paul@272 | 459 | # Record the attribute on the presumed target. |
paul@272 | 460 | |
paul@94 | 461 | if isinstance(expr, Attr): |
paul@94 | 462 | if expr.name == "self": |
paul@94 | 463 | if not self.store_class_attr(node.attrname): |
paul@94 | 464 | self.store_instance_attr(node.attrname) |
paul@192 | 465 | elif isinstance(expr.get_value(), Module): |
paul@192 | 466 | self.store_module_attr(node.attrname, expr.get_value()) |
paul@192 | 467 | print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) |
paul@272 | 468 | |
paul@272 | 469 | # Note usage of the attribute. |
paul@272 | 470 | |
paul@272 | 471 | if expr.parent is self.get_namespace(): |
paul@275 | 472 | node._attrnames = self.use_attribute(expr, node.attrname) |
paul@272 | 473 | |
paul@0 | 474 | return None |
paul@0 | 475 | |
paul@0 | 476 | def visitAssList(self, node): |
paul@204 | 477 | |
paul@204 | 478 | # Declare names which will be used by generated code. |
paul@204 | 479 | |
paul@250 | 480 | self.use_name("__getitem__") |
paul@204 | 481 | |
paul@204 | 482 | # Process the assignment. |
paul@204 | 483 | |
paul@139 | 484 | for i, n in enumerate(node.nodes): |
paul@0 | 485 | self.dispatch(n) |
paul@168 | 486 | self.importer.make_constant(i) # for __getitem__(i) at run-time |
paul@0 | 487 | return None |
paul@0 | 488 | |
paul@0 | 489 | def visitAssName(self, node): |
paul@274 | 490 | if node.flags == "OP_DELETE": |
paul@274 | 491 | raise InspectError(self.full_name(), node, "Deletion of attribute %r is not supported." % node.name) |
paul@274 | 492 | |
paul@120 | 493 | self.store(node.name, self.expr) |
paul@278 | 494 | self.define_attribute_user(node) |
paul@252 | 495 | self.use_name(node.name) |
paul@0 | 496 | return None |
paul@0 | 497 | |
paul@0 | 498 | visitAssTuple = visitAssList |
paul@0 | 499 | |
paul@172 | 500 | def visitAugAssign(self, node): |
paul@172 | 501 | |
paul@172 | 502 | # Accounting. |
paul@172 | 503 | |
paul@172 | 504 | aug_method, (left_method, right_method) = augassign_methods[node.op] |
paul@250 | 505 | self.use_name(aug_method) |
paul@250 | 506 | self.use_name(left_method) |
paul@250 | 507 | self.use_name(right_method) |
paul@244 | 508 | |
paul@244 | 509 | # Process the assignment. |
paul@244 | 510 | |
paul@244 | 511 | self.expr = self.dispatch(node.expr) |
paul@244 | 512 | |
paul@244 | 513 | # NOTE: Similar to micropython.ast handler code. |
paul@244 | 514 | # NOTE: Slices and subscripts not supported. |
paul@244 | 515 | |
paul@244 | 516 | if isinstance(node.node, compiler.ast.Name): |
paul@244 | 517 | self.visitAssName(node.node) |
paul@244 | 518 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@244 | 519 | self.visitAssAttr(node.node) |
paul@244 | 520 | else: |
paul@244 | 521 | raise InspectError(self.full_name(), node, "AugAssign(Slice or Subscript)") |
paul@244 | 522 | |
paul@244 | 523 | return None |
paul@2 | 524 | |
paul@82 | 525 | visitBackquote = OP |
paul@4 | 526 | |
paul@172 | 527 | visitBitand = _visitBinary |
paul@2 | 528 | |
paul@172 | 529 | visitBitor = _visitBinary |
paul@2 | 530 | |
paul@172 | 531 | visitBitxor = _visitBinary |
paul@4 | 532 | |
paul@275 | 533 | def visitBreak(self, node): |
paul@275 | 534 | self.reset_all_attributes() |
paul@4 | 535 | |
paul@82 | 536 | visitCallFunc = OP |
paul@0 | 537 | |
paul@0 | 538 | def visitClass(self, node): |
paul@91 | 539 | |
paul@91 | 540 | """ |
paul@91 | 541 | Register the class at the given 'node' subject to the restrictions |
paul@91 | 542 | mentioned in the module docstring. |
paul@91 | 543 | """ |
paul@91 | 544 | |
paul@12 | 545 | if self.namespaces: |
paul@41 | 546 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@82 | 547 | return None |
paul@0 | 548 | else: |
paul@272 | 549 | cls = Class(node.name, self.get_namespace(), self, node) |
paul@37 | 550 | |
paul@37 | 551 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 552 | # definitions of classes. |
paul@37 | 553 | |
paul@0 | 554 | for base in node.bases: |
paul@36 | 555 | expr = self.dispatch(base) |
paul@36 | 556 | if isinstance(expr, Attr): |
paul@36 | 557 | if expr.assignments != 1: |
paul@39 | 558 | raise InspectError(self.full_name(), node, |
paul@40 | 559 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 560 | else: |
paul@192 | 561 | cls.add_base(expr.get_value()) |
paul@36 | 562 | else: # if expr is None: |
paul@39 | 563 | raise InspectError(self.full_name(), node, |
paul@40 | 564 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 565 | |
paul@59 | 566 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 567 | # If no bases exist, adopt the 'object' class. |
paul@59 | 568 | |
paul@61 | 569 | if not node.bases and not (self.name == "__builtins__" and node.name == "object") : |
paul@59 | 570 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@192 | 571 | cls.add_base(expr.get_value()) |
paul@59 | 572 | |
paul@21 | 573 | # Make a back reference from the node for code generation. |
paul@21 | 574 | |
paul@39 | 575 | node.unit = cls |
paul@21 | 576 | |
paul@0 | 577 | # Make an entry for the class. |
paul@0 | 578 | |
paul@0 | 579 | self.store(node.name, cls) |
paul@167 | 580 | self.add_object(cls) |
paul@0 | 581 | |
paul@59 | 582 | # Process the class body. |
paul@59 | 583 | |
paul@12 | 584 | self.namespaces.append(cls) |
paul@0 | 585 | self.dispatch(node.code) |
paul@12 | 586 | self.namespaces.pop() |
paul@0 | 587 | |
paul@82 | 588 | return cls |
paul@0 | 589 | |
paul@172 | 590 | def visitCompare(self, node): |
paul@172 | 591 | |
paul@172 | 592 | # Accounting. |
paul@172 | 593 | # NOTE: Replicates some code in micropython.ast.visitCompare. |
paul@172 | 594 | |
paul@172 | 595 | for op in node.ops: |
paul@172 | 596 | op_name, next_node = op |
paul@172 | 597 | methods = comparison_methods[op_name] |
paul@172 | 598 | if methods is not None: |
paul@250 | 599 | self.use_name(methods[0]) |
paul@250 | 600 | self.use_name(methods[1]) |
paul@172 | 601 | elif op_name.endswith("in"): |
paul@250 | 602 | self.use_name("__contains__") |
paul@172 | 603 | |
paul@172 | 604 | return self.OP(node) |
paul@4 | 605 | |
paul@21 | 606 | def visitConst(self, node): |
paul@177 | 607 | |
paul@177 | 608 | # Register the constant, if necessary, returning the resulting object. |
paul@177 | 609 | |
paul@252 | 610 | self.use_name(self.importer.get_constant_type_name(node.value)) |
paul@168 | 611 | return self.importer.make_constant(node.value) |
paul@0 | 612 | |
paul@275 | 613 | def visitContinue(self, node): |
paul@275 | 614 | self.reset_all_attributes() |
paul@4 | 615 | |
paul@4 | 616 | visitDecorators = NOP |
paul@4 | 617 | |
paul@82 | 618 | visitDict = OP |
paul@0 | 619 | |
paul@0 | 620 | visitDiscard = NOP |
paul@0 | 621 | |
paul@172 | 622 | visitDiv = _visitBinary |
paul@4 | 623 | |
paul@4 | 624 | visitEllipsis = NOP |
paul@4 | 625 | |
paul@4 | 626 | visitExec = NOP |
paul@4 | 627 | |
paul@82 | 628 | visitExpression = OP |
paul@4 | 629 | |
paul@172 | 630 | visitFloorDiv = _visitBinary |
paul@4 | 631 | |
paul@34 | 632 | def visitFor(self, node): |
paul@272 | 633 | self.new_branchpoint() |
paul@204 | 634 | |
paul@204 | 635 | # Declare names which will be used by generated code. |
paul@204 | 636 | |
paul@250 | 637 | self.use_name("__iter__") |
paul@250 | 638 | self.use_name("next") |
paul@204 | 639 | |
paul@272 | 640 | self.dispatch(node.assign) |
paul@272 | 641 | self.dispatch(node.list) |
paul@272 | 642 | |
paul@204 | 643 | # Enter the loop. |
paul@275 | 644 | # Propagate attribute usage to branches. |
paul@204 | 645 | |
paul@34 | 646 | self.in_loop = 1 |
paul@272 | 647 | self.new_branch() |
paul@272 | 648 | self.dispatch(node.body) |
paul@272 | 649 | self.shelve_branch() |
paul@34 | 650 | self.in_loop = 0 |
paul@272 | 651 | |
paul@275 | 652 | # Maintain a branch for the else clause or the current retained usage |
paul@275 | 653 | # where execution avoids the conditional clauses. |
paul@275 | 654 | |
paul@275 | 655 | self.new_branch() |
paul@272 | 656 | if node.else_ is not None: |
paul@272 | 657 | self.dispatch(node.else_) |
paul@275 | 658 | self.shelve_branch() |
paul@272 | 659 | |
paul@272 | 660 | self.merge_branches() |
paul@82 | 661 | return None |
paul@0 | 662 | |
paul@0 | 663 | def visitFrom(self, node): |
paul@0 | 664 | module = self.importer.load(node.modname, 1) |
paul@0 | 665 | |
paul@35 | 666 | #if module is None: |
paul@35 | 667 | # print "Warning:", node.modname, "not imported." |
paul@0 | 668 | |
paul@0 | 669 | for name, alias in node.names: |
paul@0 | 670 | if name != "*": |
paul@4 | 671 | if module is not None and module.namespace.has_key(name): |
paul@24 | 672 | attr = module[name] |
paul@192 | 673 | self.store(alias or name, attr) |
paul@192 | 674 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@192 | 675 | self.importer.load(attr.get_value().name) |
paul@24 | 676 | |
paul@24 | 677 | # Support the import of names from missing modules. |
paul@24 | 678 | |
paul@3 | 679 | else: |
paul@24 | 680 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 681 | else: |
paul@3 | 682 | if module is not None: |
paul@3 | 683 | for n in module.namespace.keys(): |
paul@24 | 684 | attr = module[n] |
paul@192 | 685 | self.store(n, attr) |
paul@192 | 686 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@192 | 687 | self.importer.load(attr.get_value().name) |
paul@0 | 688 | |
paul@0 | 689 | return None |
paul@0 | 690 | |
paul@0 | 691 | def visitFunction(self, node): |
paul@85 | 692 | return self._visitFunction(node, node.name) |
paul@0 | 693 | |
paul@82 | 694 | visitGenExpr = OP |
paul@4 | 695 | |
paul@4 | 696 | visitGenExprFor = NOP |
paul@4 | 697 | |
paul@4 | 698 | visitGenExprIf = NOP |
paul@4 | 699 | |
paul@4 | 700 | visitGenExprInner = NOP |
paul@4 | 701 | |
paul@0 | 702 | def visitGetattr(self, node): |
paul@0 | 703 | expr = self.dispatch(node.expr) |
paul@169 | 704 | attrname = node.attrname |
paul@169 | 705 | |
paul@272 | 706 | # Attempt to identify the nature of the attribute. |
paul@272 | 707 | |
paul@36 | 708 | if isinstance(expr, Attr): |
paul@192 | 709 | value = expr.get_value() |
paul@87 | 710 | if isinstance(value, (Class, Module)): |
paul@169 | 711 | attr = value.namespace.get(attrname) |
paul@36 | 712 | elif isinstance(value, UnresolvedName): |
paul@169 | 713 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@87 | 714 | else: |
paul@167 | 715 | attr = None |
paul@272 | 716 | |
paul@272 | 717 | # Note usage of the attribute. |
paul@272 | 718 | |
paul@272 | 719 | if expr.parent is self.get_namespace(): |
paul@275 | 720 | node._attrnames = self.use_attribute(expr, attrname) |
paul@272 | 721 | |
paul@167 | 722 | elif self.builtins is not None: |
paul@169 | 723 | attr = self.builtins.get(attrname) |
paul@61 | 724 | else: |
paul@169 | 725 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@169 | 726 | |
paul@169 | 727 | # Accounting. |
paul@167 | 728 | |
paul@250 | 729 | self.use_name(attrname) |
paul@169 | 730 | |
paul@167 | 731 | return attr |
paul@4 | 732 | |
paul@12 | 733 | def visitGlobal(self, node): |
paul@12 | 734 | if self.namespaces: |
paul@12 | 735 | for name in node.names: |
paul@178 | 736 | ns = self.namespaces[-1] |
paul@178 | 737 | if not ns.make_global(name): |
paul@178 | 738 | raise InspectError(ns.full_name(), node, "Name %r is global and local in %r" % (name, ns.full_name())) |
paul@37 | 739 | |
paul@236 | 740 | # The name is recorded in an earlier process. |
paul@0 | 741 | |
paul@0 | 742 | def visitIf(self, node): |
paul@272 | 743 | self.new_branchpoint() |
paul@272 | 744 | |
paul@275 | 745 | # Propagate attribute usage to branches. |
paul@275 | 746 | |
paul@0 | 747 | for test, body in node.tests: |
paul@202 | 748 | self.dispatch(test) |
paul@272 | 749 | |
paul@272 | 750 | self.new_branch() |
paul@0 | 751 | self.dispatch(body) |
paul@272 | 752 | self.shelve_branch() |
paul@272 | 753 | |
paul@275 | 754 | # Maintain a branch for the else clause or the current retained usage |
paul@275 | 755 | # where execution avoids the conditional clauses. |
paul@275 | 756 | |
paul@275 | 757 | self.new_branch() |
paul@0 | 758 | if node.else_ is not None: |
paul@0 | 759 | self.dispatch(node.else_) |
paul@275 | 760 | self.shelve_branch() |
paul@272 | 761 | |
paul@272 | 762 | self.merge_branches() |
paul@0 | 763 | return None |
paul@0 | 764 | |
paul@12 | 765 | visitIfExp = NOP |
paul@12 | 766 | |
paul@0 | 767 | def visitImport(self, node): |
paul@0 | 768 | for name, alias in node.names: |
paul@2 | 769 | if alias is not None: |
paul@167 | 770 | module = self.importer.load(name, 1) or UnresolvedName(None, name, self) |
paul@167 | 771 | self.store(alias, module) |
paul@2 | 772 | else: |
paul@167 | 773 | module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) |
paul@167 | 774 | self.store(name.split(".")[0], module) |
paul@0 | 775 | |
paul@0 | 776 | return None |
paul@0 | 777 | |
paul@172 | 778 | visitInvert = _visitUnary |
paul@4 | 779 | |
paul@59 | 780 | def visitKeyword(self, node): |
paul@59 | 781 | self.dispatch(node.expr) |
paul@168 | 782 | self.importer.make_constant(node.name) |
paul@71 | 783 | self.keyword_names.add(node.name) |
paul@59 | 784 | return None |
paul@4 | 785 | |
paul@85 | 786 | def visitLambda(self, node): |
paul@252 | 787 | self.use_name(None) # lambda functions have no names but are assumed to be invoked |
paul@85 | 788 | return self._visitFunction(node, None) |
paul@4 | 789 | |
paul@172 | 790 | visitLeftShift = _visitBinary |
paul@1 | 791 | |
paul@82 | 792 | visitList = OP |
paul@0 | 793 | |
paul@82 | 794 | visitListComp = OP |
paul@4 | 795 | |
paul@4 | 796 | visitListCompFor = NOP |
paul@4 | 797 | |
paul@4 | 798 | visitListCompIf = NOP |
paul@4 | 799 | |
paul@172 | 800 | visitMod = _visitBinary |
paul@4 | 801 | |
paul@0 | 802 | def visitModule(self, node): |
paul@143 | 803 | |
paul@143 | 804 | # Make a back reference from the node for code generation. |
paul@143 | 805 | |
paul@143 | 806 | node.unit = self |
paul@0 | 807 | return self.dispatch(node.node) |
paul@0 | 808 | |
paul@172 | 809 | visitMul = _visitBinary |
paul@2 | 810 | |
paul@0 | 811 | def visitName(self, node): |
paul@0 | 812 | name = node.name |
paul@169 | 813 | |
paul@168 | 814 | if self.importer.predefined_constants.has_key(name): |
paul@168 | 815 | attr = self.importer.get_predefined_constant(name) |
paul@82 | 816 | elif self.namespaces and self.namespaces[-1].has_key(name): |
paul@167 | 817 | attr = self.namespaces[-1][name] |
paul@13 | 818 | elif self.has_key(name): |
paul@167 | 819 | attr = self[name] |
paul@61 | 820 | elif self.builtins is not None and self.builtins.has_key(name): |
paul@167 | 821 | attr = self.builtins[name] |
paul@0 | 822 | else: |
paul@167 | 823 | attr = None |
paul@167 | 824 | |
paul@250 | 825 | self.use_name(name) |
paul@167 | 826 | return attr |
paul@0 | 827 | |
paul@82 | 828 | visitNot = OP |
paul@0 | 829 | |
paul@82 | 830 | visitOr = OP |
paul@0 | 831 | |
paul@0 | 832 | visitPass = NOP |
paul@0 | 833 | |
paul@172 | 834 | visitPower = _visitBinary |
paul@4 | 835 | |
paul@4 | 836 | visitPrint = NOP |
paul@4 | 837 | |
paul@4 | 838 | visitPrintnl = NOP |
paul@4 | 839 | |
paul@0 | 840 | visitRaise = NOP |
paul@0 | 841 | |
paul@4 | 842 | visitReturn = NOP |
paul@4 | 843 | |
paul@172 | 844 | visitRightShift = _visitBinary |
paul@1 | 845 | |
paul@82 | 846 | visitSlice = OP |
paul@4 | 847 | |
paul@82 | 848 | visitSliceobj = OP |
paul@23 | 849 | |
paul@0 | 850 | def visitStmt(self, node): |
paul@0 | 851 | for n in node.nodes: |
paul@0 | 852 | self.dispatch(n) |
paul@0 | 853 | return None |
paul@0 | 854 | |
paul@172 | 855 | visitSub = _visitBinary |
paul@4 | 856 | |
paul@216 | 857 | def visitSubscript(self, node): |
paul@250 | 858 | self.use_name("__getitem__") |
paul@216 | 859 | self.OP(node) |
paul@0 | 860 | |
paul@0 | 861 | def visitTryExcept(self, node): |
paul@0 | 862 | self.dispatch(node.body) |
paul@272 | 863 | |
paul@272 | 864 | self.new_branchpoint() |
paul@272 | 865 | |
paul@0 | 866 | for name, var, n in node.handlers: |
paul@272 | 867 | self.new_branch() |
paul@97 | 868 | |
paul@97 | 869 | # Establish the local for the handler. |
paul@97 | 870 | |
paul@99 | 871 | if var is not None: |
paul@99 | 872 | self.dispatch(var) |
paul@99 | 873 | if n is not None: |
paul@99 | 874 | self.dispatch(n) |
paul@272 | 875 | |
paul@272 | 876 | self.shelve_branch() |
paul@272 | 877 | |
paul@0 | 878 | if node.else_ is not None: |
paul@272 | 879 | self.new_branch() |
paul@0 | 880 | self.dispatch(node.else_) |
paul@272 | 881 | self.shelve_branch() |
paul@272 | 882 | |
paul@272 | 883 | self.merge_branches() |
paul@0 | 884 | return None |
paul@0 | 885 | |
paul@4 | 886 | visitTryFinally = NOP |
paul@4 | 887 | |
paul@82 | 888 | visitTuple = OP |
paul@1 | 889 | |
paul@172 | 890 | visitUnaryAdd = _visitUnary |
paul@4 | 891 | |
paul@172 | 892 | visitUnarySub = _visitUnary |
paul@4 | 893 | |
paul@34 | 894 | def visitWhile(self, node): |
paul@272 | 895 | self.new_branchpoint() |
paul@272 | 896 | |
paul@275 | 897 | # Propagate attribute usage to branches. |
paul@275 | 898 | |
paul@34 | 899 | self.in_loop = 1 |
paul@272 | 900 | self.dispatch(node.test) |
paul@272 | 901 | self.new_branch() |
paul@272 | 902 | self.dispatch(node.body) |
paul@272 | 903 | self.shelve_branch() |
paul@34 | 904 | self.in_loop = 0 |
paul@272 | 905 | |
paul@275 | 906 | # Maintain a branch for the else clause or the current retained usage |
paul@275 | 907 | # where execution avoids the conditional clauses. |
paul@275 | 908 | |
paul@275 | 909 | self.new_branch() |
paul@272 | 910 | if node.else_ is not None: |
paul@272 | 911 | self.dispatch(node.else_) |
paul@275 | 912 | self.shelve_branch() |
paul@272 | 913 | |
paul@272 | 914 | self.merge_branches() |
paul@82 | 915 | return None |
paul@4 | 916 | |
paul@4 | 917 | visitWith = NOP |
paul@4 | 918 | |
paul@4 | 919 | visitYield = NOP |
paul@4 | 920 | |
paul@0 | 921 | # vim: tabstop=4 expandtab shiftwidth=4 |