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