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@506 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 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@331 | 70 | invoked many times with different inputs. This affects particularly the |
paul@331 | 71 | definition of functions or lambdas within functions. However, there may be |
paul@331 | 72 | benefits in considering a local to be constant within a single invocation. |
paul@0 | 73 | """ |
paul@0 | 74 | |
paul@39 | 75 | from micropython.common import * |
paul@83 | 76 | from micropython.data import * |
paul@0 | 77 | import compiler.ast |
paul@507 | 78 | import sys |
paul@15 | 79 | |
paul@21 | 80 | # Program visitors. |
paul@21 | 81 | |
paul@15 | 82 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 83 | |
paul@20 | 84 | """ |
paul@20 | 85 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 86 | capable of being used as an AST visitor. |
paul@20 | 87 | """ |
paul@15 | 88 | |
paul@71 | 89 | def __init__(self, name, importer): |
paul@71 | 90 | |
paul@71 | 91 | """ |
paul@71 | 92 | Initialise this visitor with a module 'name' and an 'importer' which is |
paul@71 | 93 | used to provide access to other modules when required. |
paul@71 | 94 | """ |
paul@71 | 95 | |
paul@343 | 96 | Module.__init__(self, name, importer) |
paul@15 | 97 | self.visitor = self |
paul@15 | 98 | |
paul@91 | 99 | # Import machinery links. |
paul@91 | 100 | |
paul@343 | 101 | self.optimisations = self.importer.optimisations |
paul@61 | 102 | self.builtins = self.importer.modules.get("__builtins__") |
paul@15 | 103 | self.loaded = 0 |
paul@15 | 104 | |
paul@1 | 105 | # Current expression state. |
paul@1 | 106 | |
paul@1 | 107 | self.expr = None |
paul@374 | 108 | self.in_assignment = 0 # For slice and subscript handling. |
paul@1 | 109 | |
paul@1 | 110 | # Namespace state. |
paul@1 | 111 | |
paul@91 | 112 | self.in_method = 0 # Find instance attributes in all methods. |
paul@331 | 113 | self.in_function = 0 # Note function presence, affecting definitions. |
paul@34 | 114 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 115 | self.namespaces = [] |
paul@235 | 116 | self.functions = [] |
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@343 | 129 | self.astnode = module |
paul@236 | 130 | |
paul@439 | 131 | # Add __name__ to the namespace. |
paul@260 | 132 | |
paul@439 | 133 | self.store("__name__", self._visitConst(self.full_name())) |
paul@260 | 134 | |
paul@236 | 135 | # First, visit module-level code, recording global names. |
paul@236 | 136 | |
paul@1 | 137 | processed = self.dispatch(module) |
paul@235 | 138 | |
paul@236 | 139 | # Then, for each function, detect and record globals declared in those |
paul@236 | 140 | # functions. |
paul@236 | 141 | |
paul@236 | 142 | for node, namespaces in self.functions: |
paul@236 | 143 | self.process_globals(node) |
paul@236 | 144 | |
paul@344 | 145 | self.finalise_attribute_usage() |
paul@284 | 146 | |
paul@236 | 147 | # Then, visit each function, recording other names. |
paul@236 | 148 | |
paul@235 | 149 | for node, namespaces in self.functions: |
paul@235 | 150 | self._visitFunctionBody(node, namespaces) |
paul@344 | 151 | namespaces[-1].finalise_attribute_usage() |
paul@280 | 152 | |
paul@236 | 153 | # Add references to other modules declared using the __all__ global. |
paul@236 | 154 | |
paul@13 | 155 | if self.has_key("__all__"): |
paul@12 | 156 | all = self["__all__"] |
paul@1 | 157 | if isinstance(all, compiler.ast.List): |
paul@1 | 158 | for n in all.nodes: |
paul@167 | 159 | self.store(n.value, self.importer.add_module(self.name + "." + n.value)) |
paul@235 | 160 | |
paul@1 | 161 | return processed |
paul@0 | 162 | |
paul@236 | 163 | def process_globals(self, node): |
paul@236 | 164 | |
paul@236 | 165 | """ |
paul@236 | 166 | Within the given 'node', process global declarations, adjusting the |
paul@236 | 167 | module namespace. |
paul@236 | 168 | """ |
paul@236 | 169 | |
paul@236 | 170 | for n in node.getChildNodes(): |
paul@236 | 171 | if isinstance(n, compiler.ast.Global): |
paul@236 | 172 | for name in n.names: |
paul@236 | 173 | if not self.has_key(name): |
paul@448 | 174 | self[name] = make_instance() |
paul@236 | 175 | else: |
paul@236 | 176 | self.process_globals(n) |
paul@236 | 177 | |
paul@3 | 178 | def vacuum(self): |
paul@9 | 179 | |
paul@171 | 180 | """ |
paul@171 | 181 | Vacuum the module namespace, removing unreferenced objects and unused |
paul@171 | 182 | names. |
paul@171 | 183 | """ |
paul@9 | 184 | |
paul@241 | 185 | if self.should_optimise_unused_objects(): |
paul@252 | 186 | self.vacuum_object(self) |
paul@0 | 187 | |
paul@167 | 188 | all_objects = list(self.all_objects) |
paul@167 | 189 | |
paul@167 | 190 | for obj in all_objects: |
paul@351 | 191 | self.vacuum_object(obj) |
paul@171 | 192 | |
paul@252 | 193 | def vacuum_object(self, obj, delete_all=0): |
paul@241 | 194 | |
paul@252 | 195 | "Vacuum the given object 'obj'." |
paul@171 | 196 | |
paul@468 | 197 | # Get all constant objects in apparent use. |
paul@468 | 198 | |
paul@468 | 199 | if delete_all: |
paul@468 | 200 | obj_objects = set() |
paul@468 | 201 | else: |
paul@468 | 202 | obj_objects = [] |
paul@468 | 203 | for name, attr in obj.items_for_vacuum(): |
paul@468 | 204 | |
paul@468 | 205 | # Get constant objects for attributes in use. |
paul@468 | 206 | |
paul@468 | 207 | if self.importer.uses_attribute(obj.full_name(), name) and \ |
paul@468 | 208 | attr is not None and attr.is_constant(): |
paul@468 | 209 | |
paul@468 | 210 | value = attr.get_value() |
paul@468 | 211 | obj_objects.append(value) |
paul@468 | 212 | |
paul@468 | 213 | # Now vacuum unused attributes and objects not in use. |
paul@468 | 214 | |
paul@351 | 215 | for name, attr in obj.items_for_vacuum(): |
paul@270 | 216 | |
paul@270 | 217 | # Only consider deleting entire unused objects or things accessible |
paul@270 | 218 | # via names which are never used. |
paul@270 | 219 | |
paul@344 | 220 | if delete_all or not self.importer.uses_attribute(obj.full_name(), name): |
paul@351 | 221 | obj.vacuum_item(name) |
paul@270 | 222 | |
paul@270 | 223 | # Delete any unambiguous attribute value. Such values can only |
paul@270 | 224 | # have been defined within the object and therefore are not |
paul@270 | 225 | # redefined by other code regions. |
paul@270 | 226 | |
paul@468 | 227 | if attr is not None and attr.is_constant(): |
paul@252 | 228 | value = attr.get_value() |
paul@171 | 229 | |
paul@394 | 230 | # The value must have this object as a parent. |
paul@468 | 231 | # However, it must not be shared by several names. |
paul@394 | 232 | |
paul@468 | 233 | if value is not obj and value.parent is obj and \ |
paul@468 | 234 | value in self.all_objects and value not in obj_objects: |
paul@468 | 235 | |
paul@252 | 236 | self.all_objects.remove(value) |
paul@171 | 237 | |
paul@351 | 238 | # Delete class contents and lambdas from functions. |
paul@241 | 239 | |
paul@351 | 240 | self.vacuum_object(value, 1) |
paul@171 | 241 | |
paul@287 | 242 | def unfinalise(self): |
paul@287 | 243 | |
paul@287 | 244 | "Reset finalised information for the module." |
paul@287 | 245 | |
paul@287 | 246 | for obj in self.all_objects: |
paul@287 | 247 | obj.unfinalise_attributes() |
paul@287 | 248 | |
paul@437 | 249 | def finalise(self, objtable): |
paul@171 | 250 | |
paul@171 | 251 | "Finalise the module." |
paul@171 | 252 | |
paul@171 | 253 | for obj in self.all_objects: |
paul@437 | 254 | obj.finalise(objtable) |
paul@171 | 255 | |
paul@478 | 256 | self.finalise_users(objtable) |
paul@478 | 257 | |
paul@167 | 258 | def add_object(self, obj, any_scope=0): |
paul@167 | 259 | |
paul@167 | 260 | """ |
paul@167 | 261 | Record 'obj' if non-local or if the optional 'any_scope' is set to a |
paul@167 | 262 | true value. |
paul@167 | 263 | """ |
paul@167 | 264 | |
paul@167 | 265 | if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@167 | 266 | self.all_objects.add(obj) |
paul@167 | 267 | |
paul@167 | 268 | # Optimisation tests. |
paul@167 | 269 | |
paul@167 | 270 | def should_optimise_unused_objects(self): |
paul@167 | 271 | return "unused_objects" in self.optimisations |
paul@167 | 272 | |
paul@3 | 273 | # Namespace methods. |
paul@0 | 274 | |
paul@330 | 275 | def in_class(self, namespaces=None): |
paul@330 | 276 | namespaces = namespaces or self.namespaces |
paul@330 | 277 | return len(namespaces) > 1 and isinstance(namespaces[-2], Class) |
paul@330 | 278 | |
paul@0 | 279 | def store(self, name, obj): |
paul@12 | 280 | |
paul@12 | 281 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 282 | |
paul@339 | 283 | # Store in the module. |
paul@339 | 284 | |
paul@12 | 285 | if not self.namespaces: |
paul@342 | 286 | if self.in_loop and self.used_in_scope(name, "builtins"): |
paul@339 | 287 | raise InspectError("Name %r already used as a built-in." % name) |
paul@339 | 288 | else: |
paul@339 | 289 | self.set(name, obj, not self.in_loop) |
paul@339 | 290 | |
paul@339 | 291 | # Or store locally. |
paul@339 | 292 | |
paul@12 | 293 | else: |
paul@341 | 294 | locals = self.namespaces[-1] |
paul@341 | 295 | |
paul@342 | 296 | if self.in_loop and locals.used_in_scope(name, "global") and not name in locals.globals: |
paul@341 | 297 | raise InspectError("Name %r already used as global." % name) |
paul@342 | 298 | elif self.in_loop and locals.used_in_scope(name, "builtins"): |
paul@341 | 299 | raise InspectError("Name %r already used as a built-in." % name) |
paul@341 | 300 | else: |
paul@341 | 301 | locals.set(name, obj, not self.in_loop) |
paul@12 | 302 | |
paul@85 | 303 | def store_lambda(self, obj): |
paul@91 | 304 | |
paul@91 | 305 | "Store a lambda function 'obj'." |
paul@91 | 306 | |
paul@167 | 307 | self.add_object(obj) |
paul@351 | 308 | self.get_namespace().add_lambda(obj) |
paul@85 | 309 | |
paul@94 | 310 | def store_module_attr(self, name, module): |
paul@94 | 311 | |
paul@94 | 312 | """ |
paul@94 | 313 | Record module attribute 'name' in the given 'module' using the current |
paul@94 | 314 | expression. |
paul@94 | 315 | """ |
paul@94 | 316 | |
paul@192 | 317 | module.set(name, self.expr, 0) |
paul@405 | 318 | self.use_specific_attribute(module.full_name(), name) |
paul@94 | 319 | |
paul@404 | 320 | def store_class_attr(self, name, cls): |
paul@91 | 321 | |
paul@94 | 322 | """ |
paul@404 | 323 | Record class attribute 'name' in the given class 'cls' using the current |
paul@94 | 324 | expression. |
paul@94 | 325 | """ |
paul@91 | 326 | |
paul@404 | 327 | cls.set(name, self.expr, 0) |
paul@405 | 328 | self.use_specific_attribute(cls.full_name(), name) |
paul@91 | 329 | |
paul@527 | 330 | def store_instance_attr(self, name, tentative=False): |
paul@12 | 331 | |
paul@527 | 332 | """ |
paul@527 | 333 | Record instance attribute 'name' in the current class. If 'tentative' is |
paul@527 | 334 | set to a true value, the instance attribute will be discarded if a class |
paul@527 | 335 | attribute is observed. |
paul@527 | 336 | """ |
paul@12 | 337 | |
paul@91 | 338 | if self.in_method: |
paul@15 | 339 | |
paul@12 | 340 | # Current namespace is the function. |
paul@12 | 341 | # Previous namespace is the class. |
paul@15 | 342 | |
paul@405 | 343 | cls = self.namespaces[-2] |
paul@527 | 344 | cls.add_instance_attribute(name, tentative) |
paul@406 | 345 | |
paul@406 | 346 | # NOTE: The instance attribute, although defined in a specific |
paul@406 | 347 | # NOTE: class, obviously appears in all descendant classes. |
paul@406 | 348 | |
paul@405 | 349 | self.use_specific_attribute(cls.full_name(), name) |
paul@0 | 350 | |
paul@272 | 351 | def get_namespace(self): |
paul@71 | 352 | |
paul@71 | 353 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 354 | |
paul@12 | 355 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 356 | |
paul@404 | 357 | def use_name(self, name, node=None, value=None): |
paul@323 | 358 | |
paul@323 | 359 | """ |
paul@323 | 360 | Use the given 'name' within the current namespace/unit, either in |
paul@323 | 361 | conjunction with a particular object (if 'node' is specified and not |
paul@323 | 362 | None) or unconditionally. |
paul@323 | 363 | """ |
paul@250 | 364 | |
paul@323 | 365 | if node is not None and isinstance(node, compiler.ast.Name): |
paul@404 | 366 | self.use_attribute(node.name, name, value) |
paul@250 | 367 | |
paul@323 | 368 | # For general name usage, declare usage of the given name from this |
paul@323 | 369 | # particular unit. |
paul@323 | 370 | |
paul@323 | 371 | else: |
paul@323 | 372 | unit = self.get_namespace() |
paul@404 | 373 | self.importer.use_name(name, unit.full_name(), value) |
paul@250 | 374 | |
paul@440 | 375 | def use_constant(self, const): |
paul@440 | 376 | |
paul@440 | 377 | "Use the given 'const' within the current namespace/unit." |
paul@440 | 378 | |
paul@440 | 379 | unit = self.get_namespace() |
paul@440 | 380 | self.importer.use_constant(const, unit.full_name()) |
paul@440 | 381 | |
paul@279 | 382 | # Attribute usage methods. |
paul@279 | 383 | # These are convenience methods which refer to the specific namespace's |
paul@279 | 384 | # implementation of these operations. |
paul@279 | 385 | |
paul@388 | 386 | def new_branchpoint(self, loop_node=None): |
paul@388 | 387 | self.get_namespace()._new_branchpoint(loop_node) |
paul@272 | 388 | |
paul@379 | 389 | def new_branch(self, node): |
paul@379 | 390 | self.get_namespace()._new_branch(node) |
paul@272 | 391 | |
paul@299 | 392 | def abandon_branch(self): |
paul@299 | 393 | self.get_namespace()._abandon_branch() |
paul@299 | 394 | |
paul@388 | 395 | def suspend_broken_branch(self): |
paul@388 | 396 | self.get_namespace()._suspend_broken_branch() |
paul@388 | 397 | |
paul@388 | 398 | def suspend_continuing_branch(self): |
paul@388 | 399 | self.get_namespace()._suspend_continuing_branch() |
paul@387 | 400 | |
paul@272 | 401 | def shelve_branch(self): |
paul@272 | 402 | self.get_namespace()._shelve_branch() |
paul@272 | 403 | |
paul@379 | 404 | def merge_branches(self): |
paul@379 | 405 | self.get_namespace()._merge_branches() |
paul@272 | 406 | |
paul@388 | 407 | def resume_broken_branches(self): |
paul@388 | 408 | self.get_namespace()._resume_broken_branches() |
paul@388 | 409 | |
paul@388 | 410 | def resume_continuing_branches(self): |
paul@388 | 411 | self.get_namespace()._resume_continuing_branches() |
paul@387 | 412 | |
paul@540 | 413 | def resume_abandoned_branches(self): |
paul@540 | 414 | self.get_namespace()._resume_abandoned_branches() |
paul@540 | 415 | |
paul@278 | 416 | def define_attribute_user(self, node): |
paul@279 | 417 | |
paul@279 | 418 | """ |
paul@279 | 419 | Define 'node' as the user of attributes, indicating the point where the |
paul@279 | 420 | user is defined. |
paul@279 | 421 | """ |
paul@279 | 422 | |
paul@278 | 423 | self.get_namespace()._define_attribute_user(node) |
paul@272 | 424 | |
paul@404 | 425 | def use_attribute(self, name, attrname, value=None): |
paul@323 | 426 | |
paul@404 | 427 | """ |
paul@404 | 428 | Note usage on the attribute user 'name' of the attribute 'attrname', |
paul@404 | 429 | noting an assignment if 'value' is specified. |
paul@404 | 430 | """ |
paul@323 | 431 | |
paul@404 | 432 | return self.get_namespace()._use_attribute(name, attrname, value) |
paul@272 | 433 | |
paul@434 | 434 | def use_specific_attribute(self, objname, attrname, from_name=None): |
paul@347 | 435 | |
paul@347 | 436 | """ |
paul@347 | 437 | Note usage on the object having the given 'objname' of the attribute |
paul@351 | 438 | 'attrname'. If 'objname' is None, the current namespace is chosen as the |
paul@351 | 439 | object providing the attribute. |
paul@347 | 440 | """ |
paul@347 | 441 | |
paul@434 | 442 | return self.get_namespace()._use_specific_attribute(objname, attrname, from_name) |
paul@347 | 443 | |
paul@3 | 444 | # Visitor methods. |
paul@3 | 445 | |
paul@3 | 446 | def default(self, node, *args): |
paul@339 | 447 | raise InspectError("Node class %r is not supported." % node.__class__) |
paul@3 | 448 | |
paul@0 | 449 | def NOP(self, node): |
paul@21 | 450 | for n in node.getChildNodes(): |
paul@21 | 451 | self.dispatch(n) |
paul@0 | 452 | |
paul@299 | 453 | def NOP_ABANDON(self, node): |
paul@302 | 454 | self.NOP(node) |
paul@299 | 455 | self.abandon_branch() |
paul@299 | 456 | |
paul@462 | 457 | def TEST_NOP(self, node): |
paul@462 | 458 | self.use_name("__bool__", node) |
paul@462 | 459 | self.NOP(node) |
paul@462 | 460 | |
paul@82 | 461 | def OP(self, node): |
paul@82 | 462 | for n in node.getChildNodes(): |
paul@82 | 463 | self.dispatch(n) |
paul@448 | 464 | return make_instance() |
paul@1 | 465 | |
paul@462 | 466 | def TEST_OP(self, node): |
paul@462 | 467 | self.use_name("__bool__", node) |
paul@531 | 468 | self.new_branchpoint() |
paul@531 | 469 | |
paul@531 | 470 | # Propagate attribute usage to branches. |
paul@531 | 471 | # Each node starts a new conditional region, effectively making a deeply |
paul@531 | 472 | # nested collection of if-like statements. |
paul@531 | 473 | |
paul@531 | 474 | for n in node.nodes: |
paul@531 | 475 | self.new_branch(n) |
paul@531 | 476 | self.dispatch(n) |
paul@531 | 477 | |
paul@531 | 478 | # The nested regions must be terminated. |
paul@531 | 479 | |
paul@531 | 480 | for n in node.nodes: |
paul@531 | 481 | self.shelve_branch() |
paul@531 | 482 | |
paul@531 | 483 | self.merge_branches() |
paul@531 | 484 | return make_instance() |
paul@462 | 485 | |
paul@314 | 486 | # Generic support for classes of operations. |
paul@314 | 487 | |
paul@354 | 488 | def _ensureOperators(self): |
paul@358 | 489 | attr, scope, namespace = self._get_with_scope("$operator") |
paul@358 | 490 | if attr is None: |
paul@354 | 491 | module = self.importer.load("operator") |
paul@354 | 492 | self["$operator"] = module |
paul@358 | 493 | else: |
paul@358 | 494 | module = attr.get_value() |
paul@358 | 495 | return module |
paul@354 | 496 | |
paul@374 | 497 | def _visitOperator(self, node, operator_name=None): |
paul@172 | 498 | |
paul@358 | 499 | "Accounting method for the operator 'node'." |
paul@172 | 500 | |
paul@354 | 501 | operator_module = self._ensureOperators() |
paul@374 | 502 | operator_fn = operator_functions[operator_name or node.__class__.__name__] |
paul@354 | 503 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@172 | 504 | return self.OP(node) |
paul@172 | 505 | |
paul@372 | 506 | _visitBinary = _visitOperator |
paul@372 | 507 | _visitUnary = _visitOperator |
paul@372 | 508 | |
paul@372 | 509 | def _visitAttr(self, expr, attrname, node): |
paul@372 | 510 | |
paul@468 | 511 | """ |
paul@468 | 512 | Process the attribute provided by the given 'expr' with the given |
paul@468 | 513 | 'attrname' and involving the given 'node'. |
paul@468 | 514 | """ |
paul@468 | 515 | |
paul@372 | 516 | # Attempt to identify the nature of the attribute. |
paul@372 | 517 | |
paul@372 | 518 | if isinstance(expr, Attr): |
paul@372 | 519 | value = expr.get_value() |
paul@372 | 520 | |
paul@372 | 521 | # Get the attribute and record its usage. |
paul@372 | 522 | |
paul@372 | 523 | if isinstance(value, (Class, Module)): |
paul@401 | 524 | |
paul@401 | 525 | # Check for class.__class__. |
paul@401 | 526 | |
paul@401 | 527 | if attrname == "__class__" and isinstance(value, Class): |
paul@401 | 528 | attr = type_class |
paul@401 | 529 | else: |
paul@448 | 530 | attr = value.get(attrname) or make_instance() |
paul@372 | 531 | self.use_specific_attribute(value.full_name(), attrname) |
paul@372 | 532 | |
paul@372 | 533 | elif isinstance(value, UnresolvedName): |
paul@372 | 534 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@372 | 535 | |
paul@467 | 536 | # The actual attribute is not readily identifiable and is assumed |
paul@467 | 537 | # to be an instance. |
paul@467 | 538 | |
paul@372 | 539 | else: |
paul@527 | 540 | |
paul@527 | 541 | # Record any instance attributes. |
paul@527 | 542 | |
paul@527 | 543 | if expr.name == "self": |
paul@527 | 544 | self.store_instance_attr(attrname, tentative=True) |
paul@527 | 545 | |
paul@448 | 546 | attr = make_instance() |
paul@372 | 547 | |
paul@372 | 548 | # Note usage of the attribute where a local is involved. |
paul@372 | 549 | |
paul@404 | 550 | self._visitAttrUser(expr, attrname, node) |
paul@372 | 551 | |
paul@467 | 552 | # No particular attribute has been identified, thus a general instance |
paul@467 | 553 | # is assumed. |
paul@467 | 554 | |
paul@372 | 555 | else: |
paul@448 | 556 | attr = make_instance() |
paul@372 | 557 | self.use_name(attrname, node) |
paul@372 | 558 | |
paul@372 | 559 | return attr |
paul@358 | 560 | |
paul@404 | 561 | def _visitAttrUser(self, expr, attrname, node, value=None): |
paul@404 | 562 | |
paul@404 | 563 | """ |
paul@404 | 564 | Note usage of the attribute provided by 'expr' with the given 'attrname' |
paul@404 | 565 | where a local is involved, annotating the given 'node'. If the optional |
paul@404 | 566 | 'value' is given, note an assignment for future effects on attributes |
paul@404 | 567 | where such attributes are inferred from the usage. |
paul@404 | 568 | """ |
paul@404 | 569 | |
paul@467 | 570 | # Access to attribute via a local. |
paul@467 | 571 | |
paul@404 | 572 | if expr.parent is self.get_namespace(): |
paul@404 | 573 | |
paul@404 | 574 | # NOTE: Revisiting of nodes may occur for loops. |
paul@404 | 575 | |
paul@404 | 576 | if not hasattr(node, "_attrusers"): |
paul@404 | 577 | node._attrusers = set() |
paul@404 | 578 | |
paul@404 | 579 | node._attrusers.update(self.use_attribute(expr.name, attrname, value)) |
paul@404 | 580 | node._username = expr.name |
paul@404 | 581 | else: |
paul@404 | 582 | self.use_name(attrname, node.expr, value) |
paul@404 | 583 | |
paul@439 | 584 | def _visitConst(self, value): |
paul@439 | 585 | |
paul@439 | 586 | """ |
paul@439 | 587 | Register the constant given by 'value', if necessary, returning the |
paul@439 | 588 | resulting object. The type name is noted as being used, thus preserving |
paul@439 | 589 | the class in any generated program. |
paul@439 | 590 | """ |
paul@439 | 591 | |
paul@439 | 592 | self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value)) |
paul@440 | 593 | const = self.importer.make_constant(value) |
paul@440 | 594 | self.use_constant(const) |
paul@440 | 595 | return const |
paul@439 | 596 | |
paul@85 | 597 | def _visitFunction(self, node, name): |
paul@85 | 598 | |
paul@85 | 599 | """ |
paul@85 | 600 | Return a function object for the function defined by 'node' with the |
paul@85 | 601 | given 'name'. If a lambda expression is being visited, 'name' should be |
paul@85 | 602 | None. |
paul@85 | 603 | """ |
paul@85 | 604 | |
paul@233 | 605 | # Define the function object. |
paul@233 | 606 | |
paul@85 | 607 | function = Function( |
paul@85 | 608 | name, |
paul@272 | 609 | self.get_namespace(), |
paul@85 | 610 | node.argnames, |
paul@85 | 611 | node.defaults, |
paul@85 | 612 | (node.flags & 4 != 0), |
paul@85 | 613 | (node.flags & 8 != 0), |
paul@331 | 614 | self.in_loop or self.in_function, |
paul@85 | 615 | self, |
paul@85 | 616 | node |
paul@85 | 617 | ) |
paul@85 | 618 | |
paul@233 | 619 | self.add_object(function, any_scope=1) |
paul@233 | 620 | |
paul@85 | 621 | # Make a back reference from the node for code generation. |
paul@85 | 622 | |
paul@85 | 623 | node.unit = function |
paul@85 | 624 | |
paul@85 | 625 | # Process the defaults. |
paul@85 | 626 | |
paul@85 | 627 | for n in node.defaults: |
paul@85 | 628 | self.expr = self.dispatch(n) |
paul@192 | 629 | function.store_default(self.expr) |
paul@85 | 630 | |
paul@368 | 631 | # Note attribute usage where tuple parameters are involved. |
paul@368 | 632 | |
paul@368 | 633 | if function.tuple_parameters(): |
paul@368 | 634 | self.use_name("__getitem__", node) |
paul@368 | 635 | |
paul@368 | 636 | # Record the namespace context of the function for later processing. |
paul@368 | 637 | |
paul@235 | 638 | self.functions.append((node, self.namespaces + [function])) |
paul@85 | 639 | |
paul@330 | 640 | # Store the function. |
paul@330 | 641 | |
paul@85 | 642 | if name is not None: |
paul@85 | 643 | self.store(name, function) |
paul@85 | 644 | else: |
paul@85 | 645 | self.store_lambda(function) |
paul@167 | 646 | |
paul@330 | 647 | # Test the defaults and assess whether an dynamic object will result. |
paul@330 | 648 | |
paul@331 | 649 | function.make_dynamic() |
paul@331 | 650 | return function |
paul@85 | 651 | |
paul@235 | 652 | def _visitFunctionBody(self, node, namespaces): |
paul@235 | 653 | |
paul@235 | 654 | "Enter the function." |
paul@235 | 655 | |
paul@235 | 656 | # Current namespace is the function. |
paul@235 | 657 | # Previous namespace is the class. |
paul@235 | 658 | |
paul@330 | 659 | if self.in_class(namespaces): |
paul@235 | 660 | self.in_method = 1 |
paul@235 | 661 | |
paul@331 | 662 | in_function = self.in_function |
paul@331 | 663 | in_loop = self.in_loop |
paul@331 | 664 | self.in_function = 1 |
paul@331 | 665 | self.in_loop = 0 |
paul@331 | 666 | |
paul@235 | 667 | self.namespaces = namespaces |
paul@235 | 668 | self.dispatch(node.code) |
paul@235 | 669 | |
paul@331 | 670 | self.in_loop = in_loop |
paul@331 | 671 | self.in_function = in_function |
paul@235 | 672 | self.in_method = 0 |
paul@235 | 673 | |
paul@172 | 674 | # Specific handler methods. |
paul@172 | 675 | |
paul@172 | 676 | visitAdd = _visitBinary |
paul@82 | 677 | |
paul@462 | 678 | visitAnd = TEST_OP |
paul@0 | 679 | |
paul@4 | 680 | visitAssert = NOP |
paul@4 | 681 | |
paul@0 | 682 | def visitAssign(self, node): |
paul@0 | 683 | self.expr = self.dispatch(node.expr) |
paul@374 | 684 | self.in_assignment = 1 |
paul@0 | 685 | for n in node.nodes: |
paul@0 | 686 | self.dispatch(n) |
paul@374 | 687 | self.in_assignment = 0 |
paul@0 | 688 | |
paul@0 | 689 | def visitAssAttr(self, node): |
paul@0 | 690 | expr = self.dispatch(node.expr) |
paul@404 | 691 | attrname = node.attrname |
paul@272 | 692 | |
paul@272 | 693 | # Record the attribute on the presumed target. |
paul@272 | 694 | |
paul@94 | 695 | if isinstance(expr, Attr): |
paul@404 | 696 | value = expr.get_value() |
paul@404 | 697 | |
paul@94 | 698 | if expr.name == "self": |
paul@404 | 699 | self.store_instance_attr(attrname) |
paul@485 | 700 | self.use_attribute(expr.name, attrname, value) # NOTE: Impose constraints on the type given the hierarchy. |
paul@488 | 701 | self._visitAttrUser(expr, attrname, node, self.expr) |
paul@404 | 702 | |
paul@404 | 703 | elif isinstance(value, Module): |
paul@404 | 704 | self.store_module_attr(attrname, value) |
paul@507 | 705 | print >>sys.stderr, "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) |
paul@272 | 706 | |
paul@404 | 707 | elif isinstance(value, Class): |
paul@404 | 708 | self.store_class_attr(attrname, value) |
paul@404 | 709 | |
paul@285 | 710 | # Note usage of the attribute where a local is involved. |
paul@272 | 711 | |
paul@404 | 712 | else: |
paul@404 | 713 | self._visitAttrUser(expr, attrname, node, self.expr) |
paul@381 | 714 | |
paul@404 | 715 | else: |
paul@404 | 716 | self.use_name(attrname, node) |
paul@272 | 717 | |
paul@0 | 718 | def visitAssList(self, node): |
paul@204 | 719 | |
paul@204 | 720 | # Declare names which will be used by generated code. |
paul@204 | 721 | |
paul@323 | 722 | self.use_name("__getitem__", node) |
paul@204 | 723 | |
paul@204 | 724 | # Process the assignment. |
paul@204 | 725 | |
paul@139 | 726 | for i, n in enumerate(node.nodes): |
paul@0 | 727 | self.dispatch(n) |
paul@439 | 728 | self._visitConst(i) # for __getitem__(i) at run-time |
paul@0 | 729 | |
paul@0 | 730 | def visitAssName(self, node): |
paul@289 | 731 | if hasattr(node, "flags") and node.flags == "OP_DELETE": |
paul@507 | 732 | print >>sys.stderr, "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) |
paul@339 | 733 | #raise InspectError("Deletion of attribute %r is not supported." % node.name) |
paul@274 | 734 | |
paul@120 | 735 | self.store(node.name, self.expr) |
paul@278 | 736 | self.define_attribute_user(node) |
paul@285 | 737 | |
paul@285 | 738 | # Ensure the presence of the given name in this namespace. |
paul@431 | 739 | # NOTE: Consider not registering assignments involving methods, since |
paul@431 | 740 | # NOTE: this is merely creating aliases for such methods. |
paul@285 | 741 | |
paul@434 | 742 | if isinstance(self.get_namespace(), (Class, Module)): |
paul@434 | 743 | if not isinstance(self.expr, Attr) or not isinstance(self.expr.get_value(), Function): |
paul@434 | 744 | self.use_specific_attribute(None, node.name) |
paul@434 | 745 | else: |
paul@434 | 746 | fn = self.expr.get_value() |
paul@434 | 747 | ns = self.get_namespace().full_name() |
paul@434 | 748 | self.use_specific_attribute(fn.parent.full_name(), fn.name, "%s.%s" % (ns, node.name)) |
paul@434 | 749 | |
paul@0 | 750 | visitAssTuple = visitAssList |
paul@0 | 751 | |
paul@172 | 752 | def visitAugAssign(self, node): |
paul@172 | 753 | |
paul@172 | 754 | # Accounting. |
paul@172 | 755 | |
paul@354 | 756 | operator_fn = operator_functions.get(node.op) |
paul@354 | 757 | operator_module = self._ensureOperators() |
paul@354 | 758 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@244 | 759 | |
paul@244 | 760 | # Process the assignment. |
paul@244 | 761 | |
paul@244 | 762 | self.expr = self.dispatch(node.expr) |
paul@244 | 763 | |
paul@244 | 764 | # NOTE: Similar to micropython.ast handler code. |
paul@363 | 765 | # NOTE: Slices and subscripts are supported by __setitem__(slice) and |
paul@363 | 766 | # NOTE: not __setslice__. |
paul@244 | 767 | |
paul@244 | 768 | if isinstance(node.node, compiler.ast.Name): |
paul@244 | 769 | self.visitAssName(node.node) |
paul@244 | 770 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@244 | 771 | self.visitAssAttr(node.node) |
paul@244 | 772 | else: |
paul@363 | 773 | self.use_specific_attribute("__builtins__", "slice") |
paul@363 | 774 | self.use_name("__setitem__", node) |
paul@244 | 775 | |
paul@82 | 776 | visitBackquote = OP |
paul@4 | 777 | |
paul@172 | 778 | visitBitand = _visitBinary |
paul@2 | 779 | |
paul@172 | 780 | visitBitor = _visitBinary |
paul@2 | 781 | |
paul@172 | 782 | visitBitxor = _visitBinary |
paul@4 | 783 | |
paul@388 | 784 | def visitBreak(self, node): |
paul@388 | 785 | self.NOP(node) |
paul@388 | 786 | self.suspend_broken_branch() |
paul@4 | 787 | |
paul@82 | 788 | visitCallFunc = OP |
paul@0 | 789 | |
paul@0 | 790 | def visitClass(self, node): |
paul@91 | 791 | |
paul@91 | 792 | """ |
paul@91 | 793 | Register the class at the given 'node' subject to the restrictions |
paul@91 | 794 | mentioned in the module docstring. |
paul@91 | 795 | """ |
paul@91 | 796 | |
paul@12 | 797 | if self.namespaces: |
paul@507 | 798 | print >>sys.stderr, "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@442 | 799 | return |
paul@0 | 800 | else: |
paul@331 | 801 | if self.in_loop: |
paul@507 | 802 | print >>sys.stderr, "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) |
paul@331 | 803 | |
paul@394 | 804 | cls = get_class(node.name, self.get_namespace(), self, node) |
paul@37 | 805 | |
paul@349 | 806 | # Make a back reference from the node for code generation. |
paul@349 | 807 | |
paul@349 | 808 | node.unit = cls |
paul@349 | 809 | |
paul@349 | 810 | # Process base classes in the context of the class's namespace. |
paul@349 | 811 | # This confines references to such classes to the class instead of |
paul@349 | 812 | # the namespace in which it is defined. |
paul@349 | 813 | |
paul@349 | 814 | self.namespaces.append(cls) |
paul@349 | 815 | |
paul@37 | 816 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 817 | # definitions of classes. |
paul@37 | 818 | |
paul@0 | 819 | for base in node.bases: |
paul@36 | 820 | expr = self.dispatch(base) |
paul@349 | 821 | |
paul@349 | 822 | # Each base class must be constant and known at compile-time. |
paul@349 | 823 | |
paul@36 | 824 | if isinstance(expr, Attr): |
paul@36 | 825 | if expr.assignments != 1: |
paul@442 | 826 | raise InspectError("Base class %r for %r is not constant: %r" % (base, cls.full_name(), expr)) |
paul@36 | 827 | else: |
paul@192 | 828 | cls.add_base(expr.get_value()) |
paul@349 | 829 | |
paul@349 | 830 | # Where no expression value is available, the base class is |
paul@349 | 831 | # not identifiable. |
paul@349 | 832 | |
paul@349 | 833 | else: |
paul@339 | 834 | raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 835 | |
paul@59 | 836 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 837 | # If no bases exist, adopt the 'object' class. |
paul@59 | 838 | |
paul@61 | 839 | if not node.bases and not (self.name == "__builtins__" and node.name == "object") : |
paul@59 | 840 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@192 | 841 | cls.add_base(expr.get_value()) |
paul@59 | 842 | |
paul@349 | 843 | # Make an entry for the class in the parent namespace. |
paul@21 | 844 | |
paul@349 | 845 | self.namespaces.pop() |
paul@0 | 846 | self.store(node.name, cls) |
paul@167 | 847 | self.add_object(cls) |
paul@0 | 848 | |
paul@349 | 849 | # Process the class body in its own namespace. |
paul@439 | 850 | # Add __name__ to the namespace. |
paul@59 | 851 | |
paul@12 | 852 | self.namespaces.append(cls) |
paul@439 | 853 | self.store("__name__", self._visitConst(node.name)) |
paul@0 | 854 | self.dispatch(node.code) |
paul@12 | 855 | self.namespaces.pop() |
paul@0 | 856 | |
paul@349 | 857 | cls.finalise_attribute_usage() |
paul@82 | 858 | return cls |
paul@0 | 859 | |
paul@172 | 860 | def visitCompare(self, node): |
paul@172 | 861 | |
paul@172 | 862 | # Accounting. |
paul@172 | 863 | # NOTE: Replicates some code in micropython.ast.visitCompare. |
paul@172 | 864 | |
paul@462 | 865 | self.use_name("__bool__", node) |
paul@462 | 866 | |
paul@323 | 867 | this_node = node |
paul@323 | 868 | |
paul@172 | 869 | for op in node.ops: |
paul@172 | 870 | op_name, next_node = op |
paul@323 | 871 | |
paul@354 | 872 | # Define name/attribute usage. |
paul@354 | 873 | # Get the applicable operation. |
paul@323 | 874 | |
paul@354 | 875 | operator_fn = operator_functions.get(op_name) |
paul@354 | 876 | |
paul@354 | 877 | # For operators, reference the specific function involved. |
paul@323 | 878 | |
paul@354 | 879 | if operator_fn is not None: |
paul@354 | 880 | operator_module = self._ensureOperators() |
paul@354 | 881 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@323 | 882 | |
paul@354 | 883 | # Define __contains__ usage on the next node. |
paul@354 | 884 | |
paul@172 | 885 | elif op_name.endswith("in"): |
paul@323 | 886 | self.use_name("__contains__", next_node) |
paul@323 | 887 | |
paul@323 | 888 | this_node = next_node |
paul@172 | 889 | |
paul@172 | 890 | return self.OP(node) |
paul@4 | 891 | |
paul@21 | 892 | def visitConst(self, node): |
paul@439 | 893 | return self._visitConst(node.value) |
paul@0 | 894 | |
paul@388 | 895 | def visitContinue(self, node): |
paul@388 | 896 | self.NOP(node) |
paul@388 | 897 | self.suspend_continuing_branch() |
paul@4 | 898 | |
paul@4 | 899 | visitDecorators = NOP |
paul@4 | 900 | |
paul@82 | 901 | visitDict = OP |
paul@0 | 902 | |
paul@0 | 903 | visitDiscard = NOP |
paul@0 | 904 | |
paul@172 | 905 | visitDiv = _visitBinary |
paul@4 | 906 | |
paul@4 | 907 | visitEllipsis = NOP |
paul@4 | 908 | |
paul@4 | 909 | visitExec = NOP |
paul@4 | 910 | |
paul@82 | 911 | visitExpression = OP |
paul@4 | 912 | |
paul@172 | 913 | visitFloorDiv = _visitBinary |
paul@4 | 914 | |
paul@34 | 915 | def visitFor(self, node): |
paul@388 | 916 | self.new_branchpoint(node) |
paul@204 | 917 | |
paul@204 | 918 | # Declare names which will be used by generated code. |
paul@204 | 919 | |
paul@323 | 920 | self.use_name("__iter__", node.list) |
paul@250 | 921 | self.use_name("next") |
paul@345 | 922 | self.use_name("StopIteration") |
paul@204 | 923 | |
paul@331 | 924 | in_loop = self.in_loop |
paul@289 | 925 | self.in_loop = 1 |
paul@341 | 926 | self.dispatch(node.list) |
paul@442 | 927 | |
paul@442 | 928 | # NOTE: Could generate AST nodes for the actual operations instead of |
paul@442 | 929 | # NOTE: manually generating code in micropython.ast. |
paul@442 | 930 | |
paul@448 | 931 | self.expr = make_instance() # each element is a result of a function call |
paul@272 | 932 | self.dispatch(node.assign) |
paul@272 | 933 | |
paul@204 | 934 | # Enter the loop. |
paul@275 | 935 | # Propagate attribute usage to branches. |
paul@204 | 936 | |
paul@379 | 937 | self.new_branch(node) |
paul@272 | 938 | self.dispatch(node.body) |
paul@385 | 939 | |
paul@388 | 940 | self.resume_continuing_branches() |
paul@385 | 941 | |
paul@272 | 942 | self.shelve_branch() |
paul@362 | 943 | |
paul@331 | 944 | self.in_loop = in_loop |
paul@272 | 945 | |
paul@386 | 946 | # A null branch is used to record a path around the loop. |
paul@275 | 947 | |
paul@379 | 948 | self.new_branch(node.else_ or NullBranch()) |
paul@379 | 949 | self.shelve_branch() |
paul@272 | 950 | |
paul@379 | 951 | self.merge_branches() |
paul@386 | 952 | |
paul@386 | 953 | # The else clause is evaluated outside any branch. |
paul@386 | 954 | |
paul@386 | 955 | if node.else_ is not None: |
paul@386 | 956 | self.dispatch(node.else_) |
paul@386 | 957 | |
paul@387 | 958 | # Any suspended branches from the loop can now be resumed. |
paul@387 | 959 | |
paul@388 | 960 | self.resume_broken_branches() |
paul@387 | 961 | |
paul@0 | 962 | def visitFrom(self, node): |
paul@0 | 963 | module = self.importer.load(node.modname, 1) |
paul@0 | 964 | |
paul@35 | 965 | #if module is None: |
paul@507 | 966 | # print >>sys.stderr, "Warning:", node.modname, "not imported." |
paul@0 | 967 | |
paul@0 | 968 | for name, alias in node.names: |
paul@0 | 969 | if name != "*": |
paul@501 | 970 | if module is not None: |
paul@501 | 971 | |
paul@501 | 972 | # Missing names may refer to submodules. |
paul@501 | 973 | |
paul@501 | 974 | if not module.has_key(name): |
paul@501 | 975 | submodule = self.importer.load(node.modname + "." + name, 1) |
paul@501 | 976 | if submodule is not None: |
paul@501 | 977 | module.store(name, submodule) |
paul@501 | 978 | |
paul@501 | 979 | # Complete the import if the name was found. |
paul@501 | 980 | |
paul@501 | 981 | if module.has_key(name): |
paul@501 | 982 | attr = module[name] |
paul@501 | 983 | self.store(alias or name, attr) |
paul@501 | 984 | self.use_specific_attribute(module.full_name(), name) |
paul@501 | 985 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@501 | 986 | self.importer.load(attr.get_value().name) |
paul@501 | 987 | continue |
paul@24 | 988 | |
paul@24 | 989 | # Support the import of names from missing modules. |
paul@24 | 990 | |
paul@501 | 991 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@501 | 992 | |
paul@3 | 993 | else: |
paul@3 | 994 | if module is not None: |
paul@285 | 995 | for n in module.keys(): |
paul@24 | 996 | attr = module[n] |
paul@192 | 997 | self.store(n, attr) |
paul@364 | 998 | self.use_specific_attribute(module.full_name(), n) |
paul@192 | 999 | if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: |
paul@192 | 1000 | self.importer.load(attr.get_value().name) |
paul@0 | 1001 | |
paul@0 | 1002 | def visitFunction(self, node): |
paul@85 | 1003 | return self._visitFunction(node, node.name) |
paul@0 | 1004 | |
paul@82 | 1005 | visitGenExpr = OP |
paul@4 | 1006 | |
paul@4 | 1007 | visitGenExprFor = NOP |
paul@4 | 1008 | |
paul@4 | 1009 | visitGenExprIf = NOP |
paul@4 | 1010 | |
paul@4 | 1011 | visitGenExprInner = NOP |
paul@4 | 1012 | |
paul@0 | 1013 | def visitGetattr(self, node): |
paul@0 | 1014 | expr = self.dispatch(node.expr) |
paul@169 | 1015 | attrname = node.attrname |
paul@372 | 1016 | return self._visitAttr(expr, attrname, node) |
paul@4 | 1017 | |
paul@12 | 1018 | def visitGlobal(self, node): |
paul@12 | 1019 | if self.namespaces: |
paul@12 | 1020 | for name in node.names: |
paul@178 | 1021 | ns = self.namespaces[-1] |
paul@178 | 1022 | if not ns.make_global(name): |
paul@339 | 1023 | raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) |
paul@37 | 1024 | |
paul@236 | 1025 | # The name is recorded in an earlier process. |
paul@0 | 1026 | |
paul@0 | 1027 | def visitIf(self, node): |
paul@462 | 1028 | self.use_name("__bool__", node) |
paul@272 | 1029 | self.new_branchpoint() |
paul@272 | 1030 | |
paul@275 | 1031 | # Propagate attribute usage to branches. |
paul@275 | 1032 | |
paul@0 | 1033 | for test, body in node.tests: |
paul@202 | 1034 | self.dispatch(test) |
paul@272 | 1035 | |
paul@377 | 1036 | self.new_branch(body) |
paul@0 | 1037 | self.dispatch(body) |
paul@272 | 1038 | self.shelve_branch() |
paul@272 | 1039 | |
paul@377 | 1040 | # Maintain a branch for the else clause. |
paul@275 | 1041 | |
paul@379 | 1042 | self.new_branch(node.else_ or NullBranch()) |
paul@0 | 1043 | if node.else_ is not None: |
paul@0 | 1044 | self.dispatch(node.else_) |
paul@379 | 1045 | self.shelve_branch() |
paul@272 | 1046 | |
paul@272 | 1047 | self.merge_branches() |
paul@442 | 1048 | |
paul@442 | 1049 | def visitIfExp(self, node): |
paul@462 | 1050 | self.use_name("__bool__", node) |
paul@442 | 1051 | self.new_branchpoint() |
paul@442 | 1052 | |
paul@442 | 1053 | # Propagate attribute usage to branches. |
paul@442 | 1054 | |
paul@442 | 1055 | self.dispatch(node.test) |
paul@0 | 1056 | |
paul@442 | 1057 | self.new_branch(node.then) |
paul@442 | 1058 | self.dispatch(node.then) |
paul@442 | 1059 | self.shelve_branch() |
paul@442 | 1060 | |
paul@442 | 1061 | self.new_branch(node.else_) |
paul@442 | 1062 | self.dispatch(node.else_) |
paul@442 | 1063 | self.shelve_branch() |
paul@442 | 1064 | |
paul@442 | 1065 | self.merge_branches() |
paul@448 | 1066 | return make_instance() # either outcome is possible |
paul@12 | 1067 | |
paul@0 | 1068 | def visitImport(self, node): |
paul@0 | 1069 | for name, alias in node.names: |
paul@2 | 1070 | if alias is not None: |
paul@167 | 1071 | module = self.importer.load(name, 1) or UnresolvedName(None, name, self) |
paul@167 | 1072 | self.store(alias, module) |
paul@2 | 1073 | else: |
paul@167 | 1074 | module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) |
paul@167 | 1075 | self.store(name.split(".")[0], module) |
paul@0 | 1076 | |
paul@172 | 1077 | visitInvert = _visitUnary |
paul@4 | 1078 | |
paul@59 | 1079 | def visitKeyword(self, node): |
paul@59 | 1080 | self.dispatch(node.expr) |
paul@439 | 1081 | self._visitConst(node.name) |
paul@71 | 1082 | self.keyword_names.add(node.name) |
paul@4 | 1083 | |
paul@85 | 1084 | def visitLambda(self, node): |
paul@351 | 1085 | fn = self._visitFunction(node, None) |
paul@351 | 1086 | self.use_specific_attribute(None, fn.name) |
paul@351 | 1087 | return fn |
paul@4 | 1088 | |
paul@172 | 1089 | visitLeftShift = _visitBinary |
paul@1 | 1090 | |
paul@346 | 1091 | def visitList(self, node): |
paul@347 | 1092 | self.use_specific_attribute("__builtins__", "list") |
paul@442 | 1093 | return self.OP(node) |
paul@0 | 1094 | |
paul@341 | 1095 | def visitListComp(self, node): |
paul@506 | 1096 | |
paul@506 | 1097 | # Note that explicit dispatch is performed. |
paul@506 | 1098 | |
paul@506 | 1099 | if node.quals: |
paul@506 | 1100 | self.visitListCompFor(node.quals[0], node.quals[1:], node.expr) |
paul@448 | 1101 | return make_instance() |
paul@341 | 1102 | |
paul@506 | 1103 | def visitListCompFor(self, node, following_quals, expr): |
paul@341 | 1104 | self.new_branchpoint() |
paul@341 | 1105 | |
paul@341 | 1106 | # Declare names which will be used by generated code. |
paul@341 | 1107 | |
paul@341 | 1108 | self.use_name("__iter__", node.list) |
paul@341 | 1109 | self.use_name("next") |
paul@4 | 1110 | |
paul@341 | 1111 | in_loop = self.in_loop |
paul@341 | 1112 | self.in_loop = 1 |
paul@341 | 1113 | self.dispatch(node.list) |
paul@442 | 1114 | |
paul@442 | 1115 | # NOTE: Could generate AST nodes for the actual operations instead of |
paul@442 | 1116 | # NOTE: manually generating code in micropython.ast. |
paul@442 | 1117 | |
paul@448 | 1118 | self.expr = make_instance() # each element is a result of a function call |
paul@341 | 1119 | self.dispatch(node.assign) |
paul@341 | 1120 | |
paul@341 | 1121 | # Enter the loop. |
paul@341 | 1122 | # Propagate attribute usage to branches. |
paul@341 | 1123 | |
paul@379 | 1124 | self.new_branch(node) |
paul@341 | 1125 | |
paul@506 | 1126 | # Note that explicit dispatch is performed. |
paul@506 | 1127 | |
paul@506 | 1128 | if node.ifs: |
paul@506 | 1129 | self.visitListCompIf(node.ifs[0], node.ifs[1:], following_quals, expr) |
paul@506 | 1130 | elif following_quals: |
paul@506 | 1131 | self.visitListCompFor(following_quals[0], following_quals[1:], expr) |
paul@506 | 1132 | else: |
paul@506 | 1133 | self.dispatch(expr) |
paul@341 | 1134 | |
paul@341 | 1135 | self.shelve_branch() |
paul@341 | 1136 | self.in_loop = in_loop |
paul@341 | 1137 | |
paul@379 | 1138 | self.merge_branches() |
paul@4 | 1139 | |
paul@506 | 1140 | def visitListCompIf(self, node, following_ifs, following_quals, expr): |
paul@506 | 1141 | self.use_name("__bool__", node) |
paul@506 | 1142 | self.new_branchpoint() |
paul@506 | 1143 | |
paul@506 | 1144 | # Propagate attribute usage to branches. |
paul@506 | 1145 | |
paul@506 | 1146 | self.dispatch(node.test) |
paul@506 | 1147 | |
paul@506 | 1148 | # Note that explicit dispatch is performed. |
paul@506 | 1149 | |
paul@506 | 1150 | if following_ifs: |
paul@506 | 1151 | self.visitListCompIf(following_ifs[0], following_ifs[1:], following_quals, expr) |
paul@506 | 1152 | elif following_quals: |
paul@506 | 1153 | self.visitListCompFor(following_quals[0], following_quals[1:], expr) |
paul@506 | 1154 | else: |
paul@506 | 1155 | self.new_branch(expr) |
paul@506 | 1156 | self.dispatch(expr) |
paul@506 | 1157 | self.shelve_branch() |
paul@506 | 1158 | |
paul@506 | 1159 | # Maintain a branch for the else clause. |
paul@506 | 1160 | |
paul@506 | 1161 | self.new_branch(NullBranch()) |
paul@506 | 1162 | self.shelve_branch() |
paul@506 | 1163 | |
paul@506 | 1164 | self.merge_branches() |
paul@4 | 1165 | |
paul@172 | 1166 | visitMod = _visitBinary |
paul@4 | 1167 | |
paul@0 | 1168 | def visitModule(self, node): |
paul@143 | 1169 | |
paul@143 | 1170 | # Make a back reference from the node for code generation. |
paul@143 | 1171 | |
paul@143 | 1172 | node.unit = self |
paul@0 | 1173 | return self.dispatch(node.node) |
paul@0 | 1174 | |
paul@172 | 1175 | visitMul = _visitBinary |
paul@2 | 1176 | |
paul@0 | 1177 | def visitName(self, node): |
paul@448 | 1178 | return self.get_namespace().get_using_node(node.name, node) or make_instance() |
paul@0 | 1179 | |
paul@531 | 1180 | def visitNot(self, node): |
paul@531 | 1181 | self.use_name("__bool__", node) |
paul@531 | 1182 | self.dispatch(node.expr) |
paul@531 | 1183 | return make_instance() |
paul@0 | 1184 | |
paul@462 | 1185 | visitOr = TEST_OP |
paul@0 | 1186 | |
paul@0 | 1187 | visitPass = NOP |
paul@0 | 1188 | |
paul@172 | 1189 | visitPower = _visitBinary |
paul@4 | 1190 | |
paul@393 | 1191 | def _visitPrint(self, node, function_name): |
paul@393 | 1192 | self.NOP(node) |
paul@393 | 1193 | self.use_specific_attribute("__builtins__", function_name) |
paul@4 | 1194 | |
paul@393 | 1195 | def visitPrint(self, node): |
paul@393 | 1196 | self._visitPrint(node, "_print") |
paul@393 | 1197 | |
paul@393 | 1198 | def visitPrintnl(self, node): |
paul@393 | 1199 | self._visitPrint(node, "_printnl") |
paul@4 | 1200 | |
paul@299 | 1201 | visitRaise = NOP_ABANDON |
paul@0 | 1202 | |
paul@299 | 1203 | visitReturn = NOP_ABANDON |
paul@4 | 1204 | |
paul@172 | 1205 | visitRightShift = _visitBinary |
paul@1 | 1206 | |
paul@342 | 1207 | def visitSlice(self, node): |
paul@442 | 1208 | return self._visitOperator(node, self.in_assignment and "AssSlice" or "Slice") |
paul@4 | 1209 | |
paul@82 | 1210 | visitSliceobj = OP |
paul@23 | 1211 | |
paul@0 | 1212 | def visitStmt(self, node): |
paul@0 | 1213 | for n in node.nodes: |
paul@0 | 1214 | self.dispatch(n) |
paul@0 | 1215 | |
paul@172 | 1216 | visitSub = _visitBinary |
paul@4 | 1217 | |
paul@216 | 1218 | def visitSubscript(self, node): |
paul@442 | 1219 | return self._visitOperator(node, self.in_assignment and "AssSubscript" or "Subscript") |
paul@0 | 1220 | |
paul@0 | 1221 | def visitTryExcept(self, node): |
paul@540 | 1222 | self.new_branchpoint() |
paul@0 | 1223 | self.dispatch(node.body) |
paul@272 | 1224 | |
paul@0 | 1225 | for name, var, n in node.handlers: |
paul@537 | 1226 | self.new_branch(n) |
paul@97 | 1227 | |
paul@540 | 1228 | # Any abandoned branches from the body can now be resumed. |
paul@540 | 1229 | |
paul@540 | 1230 | self.resume_abandoned_branches() |
paul@540 | 1231 | |
paul@97 | 1232 | # Establish the local for the handler. |
paul@97 | 1233 | |
paul@99 | 1234 | if var is not None: |
paul@99 | 1235 | self.dispatch(var) |
paul@99 | 1236 | if n is not None: |
paul@99 | 1237 | self.dispatch(n) |
paul@272 | 1238 | |
paul@272 | 1239 | self.shelve_branch() |
paul@272 | 1240 | |
paul@540 | 1241 | # The else clause maintains the usage from the body but without the |
paul@540 | 1242 | # abandoned branches since they would never lead to the else clause |
paul@540 | 1243 | # being executed. |
paul@540 | 1244 | |
paul@379 | 1245 | self.new_branch(node.else_ or NullBranch()) |
paul@0 | 1246 | if node.else_ is not None: |
paul@0 | 1247 | self.dispatch(node.else_) |
paul@379 | 1248 | self.shelve_branch() |
paul@272 | 1249 | |
paul@272 | 1250 | self.merge_branches() |
paul@0 | 1251 | |
paul@4 | 1252 | visitTryFinally = NOP |
paul@4 | 1253 | |
paul@82 | 1254 | visitTuple = OP |
paul@1 | 1255 | |
paul@172 | 1256 | visitUnaryAdd = _visitUnary |
paul@4 | 1257 | |
paul@172 | 1258 | visitUnarySub = _visitUnary |
paul@4 | 1259 | |
paul@34 | 1260 | def visitWhile(self, node): |
paul@462 | 1261 | self.use_name("__bool__", node) |
paul@388 | 1262 | self.new_branchpoint(node) |
paul@272 | 1263 | |
paul@275 | 1264 | # Propagate attribute usage to branches. |
paul@275 | 1265 | |
paul@331 | 1266 | in_loop = self.in_loop |
paul@34 | 1267 | self.in_loop = 1 |
paul@381 | 1268 | |
paul@386 | 1269 | # The test is evaluated initially and again in the loop. |
paul@381 | 1270 | |
paul@272 | 1271 | self.dispatch(node.test) |
paul@381 | 1272 | |
paul@379 | 1273 | self.new_branch(node) |
paul@272 | 1274 | self.dispatch(node.body) |
paul@388 | 1275 | |
paul@388 | 1276 | self.resume_continuing_branches() |
paul@388 | 1277 | |
paul@386 | 1278 | self.dispatch(node.test) |
paul@386 | 1279 | self.shelve_branch() |
paul@381 | 1280 | |
paul@386 | 1281 | self.in_loop = in_loop |
paul@386 | 1282 | |
paul@386 | 1283 | # A null branch is used to record a path around the loop. |
paul@381 | 1284 | |
paul@386 | 1285 | self.new_branch(node.else_ or NullBranch()) |
paul@386 | 1286 | self.shelve_branch() |
paul@385 | 1287 | |
paul@386 | 1288 | self.merge_branches() |
paul@386 | 1289 | |
paul@386 | 1290 | # The else clause is evaluated outside any branch. |
paul@385 | 1291 | |
paul@385 | 1292 | if node.else_ is not None: |
paul@385 | 1293 | self.dispatch(node.else_) |
paul@385 | 1294 | |
paul@387 | 1295 | # Any suspended branches from the loop can now be resumed. |
paul@387 | 1296 | |
paul@388 | 1297 | self.resume_broken_branches() |
paul@387 | 1298 | |
paul@4 | 1299 | visitWith = NOP |
paul@4 | 1300 | |
paul@4 | 1301 | visitYield = NOP |
paul@4 | 1302 | |
paul@0 | 1303 | # vim: tabstop=4 expandtab shiftwidth=4 |