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