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