paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Inspect source files, obtaining details of classes and attributes. |
paul@0 | 5 | |
paul@31 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public License along with |
paul@0 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@91 | 20 | |
paul@91 | 21 | -------- |
paul@91 | 22 | |
paul@91 | 23 | The results of inspecting a module are as follows: |
paul@91 | 24 | |
paul@91 | 25 | Constants |
paul@91 | 26 | --------- |
paul@91 | 27 | |
paul@91 | 28 | All constants identified within the code shall be registered. |
paul@91 | 29 | |
paul@91 | 30 | Classes |
paul@91 | 31 | ------- |
paul@91 | 32 | |
paul@91 | 33 | All global classes shall be registered; local classes (within functions) or |
paul@91 | 34 | nested classes (within classes) are not currently registered. |
paul@91 | 35 | |
paul@91 | 36 | Base classes must be detected and constant. |
paul@91 | 37 | |
paul@91 | 38 | All classes without bases are made to inherit from __builtins__.object in order |
paul@91 | 39 | to support some standard methods. |
paul@91 | 40 | |
paul@91 | 41 | Functions |
paul@91 | 42 | --------- |
paul@91 | 43 | |
paul@91 | 44 | All functions and lambda definitions shall be registered. |
paul@91 | 45 | |
paul@91 | 46 | Namespaces |
paul@91 | 47 | ---------- |
paul@91 | 48 | |
paul@91 | 49 | Modules define their own "global" namespace, within which classes, functions |
paul@91 | 50 | and lambda definitions establish a hierarchy of namespaces. |
paul@91 | 51 | |
paul@91 | 52 | Only local, global and built-in namespaces are recognised; closures are not |
paul@91 | 53 | supported. |
paul@91 | 54 | |
paul@91 | 55 | Assignments |
paul@91 | 56 | ----------- |
paul@91 | 57 | |
paul@91 | 58 | Name assignment and attribute assignment involving modules and classes cause |
paul@91 | 59 | names to be associated with values within namespaces. |
paul@91 | 60 | |
paul@91 | 61 | Any assignments within loops are considered to cause the targets of such |
paul@91 | 62 | assignments to provide non-constant values. |
paul@91 | 63 | |
paul@91 | 64 | Assignments to names are only really considered to cause the targets of such |
paul@91 | 65 | assignments to provide constant values if the targets reside in the module |
paul@91 | 66 | namespace or in class namespaces, subject to the above conditions. |
paul@91 | 67 | |
paul@91 | 68 | Assignments to names within functions are not generally considered to cause the |
paul@91 | 69 | targets of such assignments to provide constant values since functions can be |
paul@91 | 70 | invoked many times with different inputs. However, there may be benefits in |
paul@91 | 71 | considering a local to be constant within a single invocation. |
paul@0 | 72 | """ |
paul@0 | 73 | |
paul@39 | 74 | from micropython.common import * |
paul@83 | 75 | from micropython.data import * |
paul@0 | 76 | import compiler.ast |
paul@0 | 77 | from compiler.visitor import ASTVisitor |
paul@15 | 78 | |
paul@21 | 79 | # Program visitors. |
paul@21 | 80 | |
paul@15 | 81 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 82 | |
paul@20 | 83 | """ |
paul@20 | 84 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 85 | capable of being used as an AST visitor. |
paul@20 | 86 | """ |
paul@15 | 87 | |
paul@71 | 88 | def __init__(self, name, importer): |
paul@71 | 89 | |
paul@71 | 90 | """ |
paul@71 | 91 | Initialise this visitor with a module 'name' and an 'importer' which is |
paul@71 | 92 | used to provide access to other modules when required. |
paul@71 | 93 | """ |
paul@71 | 94 | |
paul@15 | 95 | ASTVisitor.__init__(self) |
paul@15 | 96 | Module.__init__(self, name) |
paul@15 | 97 | self.visitor = self |
paul@15 | 98 | |
paul@91 | 99 | # Import machinery links. |
paul@91 | 100 | |
paul@15 | 101 | self.importer = importer |
paul@167 | 102 | self.optimisations = importer.optimisations |
paul@61 | 103 | self.builtins = self.importer.modules.get("__builtins__") |
paul@15 | 104 | self.loaded = 0 |
paul@15 | 105 | |
paul@1 | 106 | # Current expression state. |
paul@1 | 107 | |
paul@1 | 108 | self.expr = None |
paul@1 | 109 | |
paul@1 | 110 | # Namespace state. |
paul@1 | 111 | |
paul@34 | 112 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@91 | 113 | self.in_method = 0 # Find instance attributes in all methods. |
paul@34 | 114 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 115 | self.namespaces = [] |
paul@0 | 116 | self.module = None |
paul@0 | 117 | |
paul@0 | 118 | def parse(self, filename): |
paul@9 | 119 | |
paul@9 | 120 | "Parse the file having the given 'filename'." |
paul@9 | 121 | |
paul@0 | 122 | module = compiler.parseFile(filename) |
paul@0 | 123 | self.process(module) |
paul@0 | 124 | |
paul@0 | 125 | def process(self, module): |
paul@9 | 126 | |
paul@9 | 127 | "Process the given 'module'." |
paul@9 | 128 | |
paul@91 | 129 | self.astnode = self.module = module |
paul@1 | 130 | processed = self.dispatch(module) |
paul@13 | 131 | if self.has_key("__all__"): |
paul@12 | 132 | all = self["__all__"] |
paul@1 | 133 | if isinstance(all, compiler.ast.List): |
paul@1 | 134 | for n in all.nodes: |
paul@167 | 135 | self.store(n.value, self.importer.add_module(self.name + "." + n.value)) |
paul@1 | 136 | return processed |
paul@0 | 137 | |
paul@3 | 138 | def vacuum(self): |
paul@9 | 139 | |
paul@171 | 140 | """ |
paul@171 | 141 | Vacuum the module namespace, removing unreferenced objects and unused |
paul@171 | 142 | names. |
paul@171 | 143 | """ |
paul@9 | 144 | |
paul@13 | 145 | for name, value in self.items(): |
paul@167 | 146 | |
paul@171 | 147 | if self.should_optimise_unused_objects(): |
paul@167 | 148 | |
paul@171 | 149 | # Remove unused names. |
paul@167 | 150 | |
paul@171 | 151 | if name not in self.importer.names_used: |
paul@167 | 152 | del self[name] |
paul@167 | 153 | |
paul@171 | 154 | # Remove entries for unreferenced objects. |
paul@169 | 155 | # This, due to the nature of the referenced attribute, assumes |
paul@169 | 156 | # that only explicitly mentioned classes and functions are |
paul@169 | 157 | # employed in the final program. |
paul@167 | 158 | |
paul@171 | 159 | elif isinstance(value, Attr): |
paul@171 | 160 | attr_value = value.value |
paul@167 | 161 | |
paul@167 | 162 | # Only remove entries for classes and functions, not methods. |
paul@167 | 163 | |
paul@167 | 164 | for attr_value in value.assignment_values: |
paul@167 | 165 | if (isinstance(attr_value, Function) and not attr_value.is_method() or |
paul@167 | 166 | isinstance(attr_value, Class)) and not attr_value.referenced: |
paul@167 | 167 | pass |
paul@167 | 168 | else: |
paul@167 | 169 | break |
paul@167 | 170 | else: |
paul@167 | 171 | del self[name] |
paul@0 | 172 | |
paul@16 | 173 | # Complain about globals not initialised at the module level. |
paul@16 | 174 | |
paul@171 | 175 | if isinstance(value, Global): |
paul@16 | 176 | print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) |
paul@16 | 177 | |
paul@167 | 178 | # Remove unreferenced objects. |
paul@167 | 179 | |
paul@167 | 180 | if self.should_optimise_unused_objects(): |
paul@167 | 181 | |
paul@167 | 182 | all_objects = list(self.all_objects) |
paul@167 | 183 | |
paul@167 | 184 | for obj in all_objects: |
paul@171 | 185 | |
paul@171 | 186 | # Only remove entries for classes and functions, not methods. |
paul@171 | 187 | |
paul@170 | 188 | if (isinstance(obj, Function) and not obj.is_method() or |
paul@170 | 189 | isinstance(obj, Class)) and not obj.referenced: |
paul@171 | 190 | |
paul@167 | 191 | self.all_objects.remove(obj) |
paul@167 | 192 | |
paul@171 | 193 | # Remove unused entries from classes plus associated methods. |
paul@171 | 194 | |
paul@171 | 195 | if isinstance(obj, Class): |
paul@171 | 196 | for name, attr in obj.class_attributes().items(): |
paul@171 | 197 | |
paul@171 | 198 | if name not in self.importer.names_used: |
paul@171 | 199 | del obj[name] |
paul@171 | 200 | |
paul@171 | 201 | # Methods can only be deleted if they are the only |
paul@171 | 202 | # assigned object to the class and are unreferenced. |
paul@171 | 203 | |
paul@171 | 204 | if attr.assignments == 1 and isinstance(attr.value, Function) and \ |
paul@171 | 205 | attr.value.is_method() and not attr.value.referenced: |
paul@171 | 206 | |
paul@171 | 207 | self.all_objects.remove(attr.value) |
paul@171 | 208 | |
paul@171 | 209 | def finalise(self): |
paul@171 | 210 | |
paul@171 | 211 | "Finalise the module." |
paul@171 | 212 | |
paul@171 | 213 | for obj in self.all_objects: |
paul@171 | 214 | if isinstance(obj, (Class, Function)): |
paul@171 | 215 | obj.finalise_attributes() |
paul@171 | 216 | |
paul@167 | 217 | def add_object(self, obj, any_scope=0): |
paul@167 | 218 | |
paul@167 | 219 | """ |
paul@167 | 220 | Record 'obj' if non-local or if the optional 'any_scope' is set to a |
paul@167 | 221 | true value. |
paul@167 | 222 | """ |
paul@167 | 223 | |
paul@167 | 224 | if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@167 | 225 | self.all_objects.add(obj) |
paul@167 | 226 | |
paul@167 | 227 | # Optimisation tests. |
paul@167 | 228 | |
paul@167 | 229 | def should_optimise_unused_objects(self): |
paul@167 | 230 | return "unused_objects" in self.optimisations |
paul@167 | 231 | |
paul@3 | 232 | # Namespace methods. |
paul@0 | 233 | |
paul@0 | 234 | def store(self, name, obj): |
paul@12 | 235 | |
paul@12 | 236 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 237 | |
paul@12 | 238 | if not self.namespaces: |
paul@34 | 239 | self.set(name, obj, not self.in_loop) |
paul@12 | 240 | else: |
paul@34 | 241 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 242 | |
paul@85 | 243 | def store_lambda(self, obj): |
paul@91 | 244 | |
paul@91 | 245 | "Store a lambda function 'obj'." |
paul@91 | 246 | |
paul@167 | 247 | self.add_object(obj) |
paul@85 | 248 | |
paul@94 | 249 | def store_module_attr(self, name, module): |
paul@94 | 250 | |
paul@94 | 251 | """ |
paul@94 | 252 | Record module attribute 'name' in the given 'module' using the current |
paul@94 | 253 | expression. |
paul@94 | 254 | """ |
paul@94 | 255 | |
paul@94 | 256 | if isinstance(self.expr, Attr): |
paul@94 | 257 | assigned_value = self.expr.value |
paul@94 | 258 | else: |
paul@94 | 259 | assigned_value = self.expr |
paul@94 | 260 | |
paul@94 | 261 | module.set(name, assigned_value, 0) |
paul@94 | 262 | |
paul@91 | 263 | def store_class_attr(self, name): |
paul@91 | 264 | |
paul@94 | 265 | """ |
paul@94 | 266 | Record class attribute 'name' in the current class using the current |
paul@94 | 267 | expression. |
paul@94 | 268 | """ |
paul@91 | 269 | |
paul@91 | 270 | if self.in_method and self.namespaces[-2].has_key(name): |
paul@91 | 271 | |
paul@91 | 272 | if isinstance(self.expr, Attr): |
paul@91 | 273 | assigned_value = self.expr.value |
paul@91 | 274 | else: |
paul@91 | 275 | assigned_value = self.expr |
paul@91 | 276 | |
paul@91 | 277 | self.namespaces[-2].set(name, assigned_value, 0) |
paul@91 | 278 | return 1 |
paul@91 | 279 | |
paul@91 | 280 | return 0 |
paul@91 | 281 | |
paul@34 | 282 | def store_instance_attr(self, name): |
paul@12 | 283 | |
paul@12 | 284 | "Record instance attribute 'name' in the current class." |
paul@12 | 285 | |
paul@91 | 286 | if self.in_method: |
paul@15 | 287 | |
paul@12 | 288 | # Current namespace is the function. |
paul@12 | 289 | # Previous namespace is the class. |
paul@15 | 290 | |
paul@12 | 291 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 292 | |
paul@7 | 293 | def get_parent(self): |
paul@71 | 294 | |
paul@71 | 295 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 296 | |
paul@12 | 297 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 298 | |
paul@3 | 299 | # Visitor methods. |
paul@3 | 300 | |
paul@3 | 301 | def default(self, node, *args): |
paul@39 | 302 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 303 | |
paul@3 | 304 | def dispatch(self, node, *args): |
paul@3 | 305 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 306 | |
paul@0 | 307 | def NOP(self, node): |
paul@21 | 308 | for n in node.getChildNodes(): |
paul@21 | 309 | self.dispatch(n) |
paul@19 | 310 | return None |
paul@0 | 311 | |
paul@82 | 312 | def OP(self, node): |
paul@82 | 313 | for n in node.getChildNodes(): |
paul@82 | 314 | self.dispatch(n) |
paul@82 | 315 | return Instance() |
paul@1 | 316 | |
paul@172 | 317 | def _visitUnary(self, node): |
paul@172 | 318 | |
paul@172 | 319 | "Accounting method for the unary operator 'node'." |
paul@172 | 320 | |
paul@172 | 321 | method = unary_methods[node.__class__.__name__] |
paul@172 | 322 | self.importer.use_name(method) |
paul@172 | 323 | return self.OP(node) |
paul@172 | 324 | |
paul@172 | 325 | def _visitBinary(self, node): |
paul@172 | 326 | |
paul@172 | 327 | "Accounting method for the binary operator 'node'." |
paul@172 | 328 | |
paul@172 | 329 | left_method, right_method = binary_methods[node.__class__.__name__] |
paul@172 | 330 | self.importer.use_name(left_method) |
paul@172 | 331 | self.importer.use_name(right_method) |
paul@172 | 332 | return self.OP(node) |
paul@172 | 333 | |
paul@85 | 334 | def _visitFunction(self, node, name): |
paul@85 | 335 | |
paul@85 | 336 | """ |
paul@85 | 337 | Return a function object for the function defined by 'node' with the |
paul@85 | 338 | given 'name'. If a lambda expression is being visited, 'name' should be |
paul@85 | 339 | None. |
paul@85 | 340 | """ |
paul@85 | 341 | |
paul@85 | 342 | function = Function( |
paul@85 | 343 | name, |
paul@85 | 344 | self.get_parent(), |
paul@85 | 345 | node.argnames, |
paul@85 | 346 | node.defaults, |
paul@85 | 347 | (node.flags & 4 != 0), |
paul@85 | 348 | (node.flags & 8 != 0), |
paul@85 | 349 | self, |
paul@85 | 350 | node |
paul@85 | 351 | ) |
paul@85 | 352 | |
paul@85 | 353 | # Make a back reference from the node for code generation. |
paul@85 | 354 | |
paul@85 | 355 | node.unit = function |
paul@85 | 356 | |
paul@85 | 357 | # Process the defaults. |
paul@85 | 358 | |
paul@85 | 359 | for n in node.defaults: |
paul@85 | 360 | self.expr = self.dispatch(n) |
paul@85 | 361 | if isinstance(self.expr, Attr): |
paul@85 | 362 | function.store_default(self.expr.value) |
paul@85 | 363 | else: |
paul@85 | 364 | function.store_default(self.expr) |
paul@85 | 365 | |
paul@85 | 366 | # Enter the function. |
paul@85 | 367 | |
paul@85 | 368 | self.namespaces.append(function) |
paul@85 | 369 | |
paul@85 | 370 | # Current namespace is the function. |
paul@85 | 371 | # Previous namespace is the class. |
paul@85 | 372 | |
paul@91 | 373 | if len(self.namespaces) > 1 and isinstance(self.namespaces[-2], Class): |
paul@91 | 374 | if name == "__init__": |
paul@91 | 375 | self.in_init = 1 |
paul@91 | 376 | self.in_method = 1 |
paul@85 | 377 | |
paul@85 | 378 | self.dispatch(node.code) |
paul@85 | 379 | self.in_init = 0 |
paul@91 | 380 | self.in_method = 0 |
paul@85 | 381 | self.namespaces.pop() |
paul@85 | 382 | |
paul@85 | 383 | if name is not None: |
paul@85 | 384 | self.store(name, function) |
paul@85 | 385 | else: |
paul@85 | 386 | self.store_lambda(function) |
paul@167 | 387 | |
paul@167 | 388 | # Lambda functions are always assumed to be referenced. This is |
paul@167 | 389 | # because other means of discovering the referencing of objects rely |
paul@167 | 390 | # on the original names inherent in the definition of those objects. |
paul@167 | 391 | |
paul@167 | 392 | function.set_referenced() |
paul@167 | 393 | |
paul@167 | 394 | # Where defaults exist, an instance needs creating. Thus, it makes |
paul@167 | 395 | # no sense to return a reference to the function here, since the |
paul@167 | 396 | # recipient will not be referencing the function itself. |
paul@167 | 397 | |
paul@109 | 398 | if node.defaults: |
paul@167 | 399 | return Instance() # indicates no known target |
paul@85 | 400 | |
paul@167 | 401 | self.add_object(function, any_scope=1) |
paul@85 | 402 | return function |
paul@85 | 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@94 | 424 | elif isinstance(expr.value, Module): |
paul@94 | 425 | self.store_module_attr(node.attrname, expr.value) |
paul@94 | 426 | print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.value.name) |
paul@0 | 427 | return None |
paul@0 | 428 | |
paul@0 | 429 | def visitAssList(self, node): |
paul@139 | 430 | for i, n in enumerate(node.nodes): |
paul@0 | 431 | self.dispatch(n) |
paul@168 | 432 | self.importer.make_constant(i) # for __getitem__(i) at run-time |
paul@0 | 433 | return None |
paul@0 | 434 | |
paul@0 | 435 | def visitAssName(self, node): |
paul@120 | 436 | self.store(node.name, self.expr) |
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@172 | 446 | self.importer.use_name(aug_method) |
paul@172 | 447 | self.importer.use_name(left_method) |
paul@172 | 448 | self.importer.use_name(right_method) |
paul@172 | 449 | return self.OP(node) |
paul@2 | 450 | |
paul@82 | 451 | visitBackquote = OP |
paul@4 | 452 | |
paul@172 | 453 | visitBitand = _visitBinary |
paul@2 | 454 | |
paul@172 | 455 | visitBitor = _visitBinary |
paul@2 | 456 | |
paul@172 | 457 | visitBitxor = _visitBinary |
paul@4 | 458 | |
paul@4 | 459 | visitBreak = NOP |
paul@4 | 460 | |
paul@82 | 461 | visitCallFunc = OP |
paul@0 | 462 | |
paul@0 | 463 | def visitClass(self, node): |
paul@91 | 464 | |
paul@91 | 465 | """ |
paul@91 | 466 | Register the class at the given 'node' subject to the restrictions |
paul@91 | 467 | mentioned in the module docstring. |
paul@91 | 468 | """ |
paul@91 | 469 | |
paul@12 | 470 | if self.namespaces: |
paul@41 | 471 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@82 | 472 | return None |
paul@0 | 473 | else: |
paul@53 | 474 | cls = Class(node.name, self.get_parent(), self, node) |
paul@37 | 475 | |
paul@37 | 476 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 477 | # definitions of classes. |
paul@37 | 478 | |
paul@0 | 479 | for base in node.bases: |
paul@36 | 480 | expr = self.dispatch(base) |
paul@36 | 481 | if isinstance(expr, Attr): |
paul@36 | 482 | if expr.assignments != 1: |
paul@39 | 483 | raise InspectError(self.full_name(), node, |
paul@40 | 484 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 485 | else: |
paul@36 | 486 | cls.add_base(expr.value) |
paul@36 | 487 | else: # if expr is None: |
paul@39 | 488 | raise InspectError(self.full_name(), node, |
paul@40 | 489 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 490 | |
paul@59 | 491 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 492 | # If no bases exist, adopt the 'object' class. |
paul@59 | 493 | |
paul@61 | 494 | if not node.bases and not (self.name == "__builtins__" and node.name == "object") : |
paul@59 | 495 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@59 | 496 | cls.add_base(expr.value) |
paul@59 | 497 | |
paul@21 | 498 | # Make a back reference from the node for code generation. |
paul@21 | 499 | |
paul@39 | 500 | node.unit = cls |
paul@21 | 501 | |
paul@0 | 502 | # Make an entry for the class. |
paul@0 | 503 | |
paul@0 | 504 | self.store(node.name, cls) |
paul@167 | 505 | self.add_object(cls) |
paul@0 | 506 | |
paul@59 | 507 | # Process the class body. |
paul@59 | 508 | |
paul@12 | 509 | self.namespaces.append(cls) |
paul@0 | 510 | self.dispatch(node.code) |
paul@12 | 511 | self.namespaces.pop() |
paul@0 | 512 | |
paul@82 | 513 | return cls |
paul@0 | 514 | |
paul@172 | 515 | def visitCompare(self, node): |
paul@172 | 516 | |
paul@172 | 517 | # Accounting. |
paul@172 | 518 | # NOTE: Replicates some code in micropython.ast.visitCompare. |
paul@172 | 519 | |
paul@172 | 520 | for op in node.ops: |
paul@172 | 521 | op_name, next_node = op |
paul@172 | 522 | methods = comparison_methods[op_name] |
paul@172 | 523 | if methods is not None: |
paul@172 | 524 | self.importer.use_name(methods[0]) |
paul@172 | 525 | self.importer.use_name(methods[1]) |
paul@172 | 526 | elif op_name.endswith("in"): |
paul@172 | 527 | self.importer.use_name("__contains__") |
paul@172 | 528 | |
paul@172 | 529 | return self.OP(node) |
paul@4 | 530 | |
paul@21 | 531 | def visitConst(self, node): |
paul@168 | 532 | return self.importer.make_constant(node.value) |
paul@0 | 533 | |
paul@4 | 534 | visitContinue = NOP |
paul@4 | 535 | |
paul@4 | 536 | visitDecorators = NOP |
paul@4 | 537 | |
paul@82 | 538 | visitDict = OP |
paul@0 | 539 | |
paul@0 | 540 | visitDiscard = NOP |
paul@0 | 541 | |
paul@172 | 542 | visitDiv = _visitBinary |
paul@4 | 543 | |
paul@4 | 544 | visitEllipsis = NOP |
paul@4 | 545 | |
paul@4 | 546 | visitExec = NOP |
paul@4 | 547 | |
paul@82 | 548 | visitExpression = OP |
paul@4 | 549 | |
paul@172 | 550 | visitFloorDiv = _visitBinary |
paul@4 | 551 | |
paul@34 | 552 | def visitFor(self, node): |
paul@34 | 553 | self.in_loop = 1 |
paul@34 | 554 | self.NOP(node) |
paul@34 | 555 | self.in_loop = 0 |
paul@82 | 556 | return None |
paul@0 | 557 | |
paul@0 | 558 | def visitFrom(self, node): |
paul@0 | 559 | module = self.importer.load(node.modname, 1) |
paul@0 | 560 | |
paul@167 | 561 | if module is not None: |
paul@167 | 562 | module.set_referenced() |
paul@167 | 563 | |
paul@35 | 564 | #if module is None: |
paul@35 | 565 | # print "Warning:", node.modname, "not imported." |
paul@0 | 566 | |
paul@0 | 567 | for name, alias in node.names: |
paul@0 | 568 | if name != "*": |
paul@4 | 569 | if module is not None and module.namespace.has_key(name): |
paul@24 | 570 | attr = module[name] |
paul@36 | 571 | self.store(alias or name, attr.value) |
paul@167 | 572 | if isinstance(attr.value, Module) and not attr.value.loaded: |
paul@167 | 573 | self.importer.load(attr.value.name) |
paul@24 | 574 | |
paul@24 | 575 | # Support the import of names from missing modules. |
paul@24 | 576 | |
paul@3 | 577 | else: |
paul@24 | 578 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 579 | else: |
paul@3 | 580 | if module is not None: |
paul@3 | 581 | for n in module.namespace.keys(): |
paul@24 | 582 | attr = module[n] |
paul@36 | 583 | self.store(n, attr.value) |
paul@167 | 584 | if isinstance(attr.value, Module) and not attr.value.loaded: |
paul@167 | 585 | self.importer.load(attr.value.name) |
paul@0 | 586 | |
paul@0 | 587 | return None |
paul@0 | 588 | |
paul@0 | 589 | def visitFunction(self, node): |
paul@85 | 590 | return self._visitFunction(node, node.name) |
paul@0 | 591 | |
paul@82 | 592 | visitGenExpr = OP |
paul@4 | 593 | |
paul@4 | 594 | visitGenExprFor = NOP |
paul@4 | 595 | |
paul@4 | 596 | visitGenExprIf = NOP |
paul@4 | 597 | |
paul@4 | 598 | visitGenExprInner = NOP |
paul@4 | 599 | |
paul@0 | 600 | def visitGetattr(self, node): |
paul@0 | 601 | expr = self.dispatch(node.expr) |
paul@169 | 602 | attrname = node.attrname |
paul@169 | 603 | |
paul@36 | 604 | if isinstance(expr, Attr): |
paul@36 | 605 | value = expr.value |
paul@87 | 606 | if isinstance(value, (Class, Module)): |
paul@169 | 607 | attr = value.namespace.get(attrname) |
paul@36 | 608 | elif isinstance(value, UnresolvedName): |
paul@169 | 609 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@87 | 610 | else: |
paul@167 | 611 | attr = None |
paul@167 | 612 | elif self.builtins is not None: |
paul@169 | 613 | attr = self.builtins.get(attrname) |
paul@61 | 614 | else: |
paul@169 | 615 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@169 | 616 | |
paul@169 | 617 | # Accounting. |
paul@167 | 618 | |
paul@167 | 619 | if attr is not None: |
paul@167 | 620 | attr.set_referenced() |
paul@167 | 621 | |
paul@169 | 622 | self.importer.use_name(attrname) |
paul@169 | 623 | |
paul@167 | 624 | return attr |
paul@4 | 625 | |
paul@12 | 626 | def visitGlobal(self, node): |
paul@12 | 627 | if self.namespaces: |
paul@12 | 628 | for name in node.names: |
paul@12 | 629 | self.namespaces[-1].make_global(name) |
paul@37 | 630 | |
paul@37 | 631 | # Record a global entry for the name in the module. |
paul@37 | 632 | |
paul@15 | 633 | if not self.has_key(name): |
paul@16 | 634 | self[name] = Global() |
paul@0 | 635 | |
paul@0 | 636 | def visitIf(self, node): |
paul@0 | 637 | for test, body in node.tests: |
paul@0 | 638 | self.dispatch(body) |
paul@0 | 639 | if node.else_ is not None: |
paul@0 | 640 | self.dispatch(node.else_) |
paul@0 | 641 | return None |
paul@0 | 642 | |
paul@12 | 643 | visitIfExp = NOP |
paul@12 | 644 | |
paul@0 | 645 | def visitImport(self, node): |
paul@0 | 646 | for name, alias in node.names: |
paul@2 | 647 | if alias is not None: |
paul@167 | 648 | module = self.importer.load(name, 1) or UnresolvedName(None, name, self) |
paul@167 | 649 | self.store(alias, module) |
paul@2 | 650 | else: |
paul@167 | 651 | module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) |
paul@167 | 652 | self.store(name.split(".")[0], module) |
paul@167 | 653 | module.set_referenced() |
paul@0 | 654 | |
paul@0 | 655 | return None |
paul@0 | 656 | |
paul@172 | 657 | visitInvert = _visitUnary |
paul@4 | 658 | |
paul@59 | 659 | def visitKeyword(self, node): |
paul@59 | 660 | self.dispatch(node.expr) |
paul@168 | 661 | self.importer.make_constant(node.name) |
paul@71 | 662 | self.keyword_names.add(node.name) |
paul@59 | 663 | return None |
paul@4 | 664 | |
paul@85 | 665 | def visitLambda(self, node): |
paul@85 | 666 | return self._visitFunction(node, None) |
paul@4 | 667 | |
paul@172 | 668 | visitLeftShift = _visitBinary |
paul@1 | 669 | |
paul@82 | 670 | visitList = OP |
paul@0 | 671 | |
paul@82 | 672 | visitListComp = OP |
paul@4 | 673 | |
paul@4 | 674 | visitListCompFor = NOP |
paul@4 | 675 | |
paul@4 | 676 | visitListCompIf = NOP |
paul@4 | 677 | |
paul@172 | 678 | visitMod = _visitBinary |
paul@4 | 679 | |
paul@0 | 680 | def visitModule(self, node): |
paul@143 | 681 | |
paul@143 | 682 | # Make a back reference from the node for code generation. |
paul@143 | 683 | |
paul@143 | 684 | node.unit = self |
paul@0 | 685 | return self.dispatch(node.node) |
paul@0 | 686 | |
paul@172 | 687 | visitMul = _visitBinary |
paul@2 | 688 | |
paul@0 | 689 | def visitName(self, node): |
paul@0 | 690 | name = node.name |
paul@169 | 691 | |
paul@168 | 692 | if self.importer.predefined_constants.has_key(name): |
paul@168 | 693 | attr = self.importer.get_predefined_constant(name) |
paul@82 | 694 | elif self.namespaces and self.namespaces[-1].has_key(name): |
paul@167 | 695 | attr = self.namespaces[-1][name] |
paul@13 | 696 | elif self.has_key(name): |
paul@167 | 697 | attr = self[name] |
paul@61 | 698 | elif self.builtins is not None and self.builtins.has_key(name): |
paul@167 | 699 | attr = self.builtins[name] |
paul@0 | 700 | else: |
paul@167 | 701 | attr = None |
paul@167 | 702 | |
paul@169 | 703 | # Accounting. |
paul@169 | 704 | |
paul@167 | 705 | if attr is not None: |
paul@167 | 706 | attr.set_referenced() |
paul@167 | 707 | |
paul@169 | 708 | self.importer.use_name(name) |
paul@169 | 709 | |
paul@167 | 710 | return attr |
paul@0 | 711 | |
paul@82 | 712 | visitNot = OP |
paul@0 | 713 | |
paul@82 | 714 | visitOr = OP |
paul@0 | 715 | |
paul@0 | 716 | visitPass = NOP |
paul@0 | 717 | |
paul@172 | 718 | visitPower = _visitBinary |
paul@4 | 719 | |
paul@4 | 720 | visitPrint = NOP |
paul@4 | 721 | |
paul@4 | 722 | visitPrintnl = NOP |
paul@4 | 723 | |
paul@0 | 724 | visitRaise = NOP |
paul@0 | 725 | |
paul@4 | 726 | visitReturn = NOP |
paul@4 | 727 | |
paul@172 | 728 | visitRightShift = _visitBinary |
paul@1 | 729 | |
paul@82 | 730 | visitSlice = OP |
paul@4 | 731 | |
paul@82 | 732 | visitSliceobj = OP |
paul@23 | 733 | |
paul@0 | 734 | def visitStmt(self, node): |
paul@0 | 735 | for n in node.nodes: |
paul@0 | 736 | self.dispatch(n) |
paul@0 | 737 | return None |
paul@0 | 738 | |
paul@172 | 739 | visitSub = _visitBinary |
paul@4 | 740 | |
paul@82 | 741 | visitSubscript = OP |
paul@0 | 742 | |
paul@0 | 743 | def visitTryExcept(self, node): |
paul@0 | 744 | self.dispatch(node.body) |
paul@0 | 745 | for name, var, n in node.handlers: |
paul@97 | 746 | |
paul@97 | 747 | # Establish the local for the handler. |
paul@97 | 748 | |
paul@99 | 749 | if var is not None: |
paul@99 | 750 | self.dispatch(var) |
paul@99 | 751 | if n is not None: |
paul@99 | 752 | self.dispatch(n) |
paul@0 | 753 | if node.else_ is not None: |
paul@0 | 754 | self.dispatch(node.else_) |
paul@0 | 755 | return None |
paul@0 | 756 | |
paul@4 | 757 | visitTryFinally = NOP |
paul@4 | 758 | |
paul@82 | 759 | visitTuple = OP |
paul@1 | 760 | |
paul@172 | 761 | visitUnaryAdd = _visitUnary |
paul@4 | 762 | |
paul@172 | 763 | visitUnarySub = _visitUnary |
paul@4 | 764 | |
paul@34 | 765 | def visitWhile(self, node): |
paul@34 | 766 | self.in_loop = 1 |
paul@34 | 767 | self.NOP(node) |
paul@34 | 768 | self.in_loop = 0 |
paul@82 | 769 | return None |
paul@4 | 770 | |
paul@4 | 771 | visitWith = NOP |
paul@4 | 772 | |
paul@4 | 773 | visitYield = NOP |
paul@4 | 774 | |
paul@16 | 775 | class Global: |
paul@16 | 776 | |
paul@16 | 777 | """ |
paul@16 | 778 | A reference to an object assigned to a global from outside the module |
paul@16 | 779 | top-level. |
paul@16 | 780 | """ |
paul@16 | 781 | |
paul@16 | 782 | pass |
paul@16 | 783 | |
paul@0 | 784 | # vim: tabstop=4 expandtab shiftwidth=4 |