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