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