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