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