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