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@31 | 6 | Copyright (C) 2007, 2008 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@20 | 20 | |
paul@20 | 21 | -------- |
paul@20 | 22 | |
paul@20 | 23 | The central classes in this module are the following: |
paul@20 | 24 | |
paul@20 | 25 | * Class |
paul@20 | 26 | * Function |
paul@20 | 27 | * Module |
paul@20 | 28 | * InspectedModule (derived from Module) |
paul@20 | 29 | |
paul@20 | 30 | All of the above support the Naming interface either explicitly or through |
paul@20 | 31 | general conformance, meaning that all can be asked to provide their 'full_name' |
paul@20 | 32 | using the method of that name. |
paul@20 | 33 | |
paul@20 | 34 | Additionally, all of the above also support a dictionary interface in order to |
paul@20 | 35 | access names within their defined scopes. Specific methods also exist in order |
paul@20 | 36 | to distinguish between certain kinds of attributes: |
paul@20 | 37 | |
paul@20 | 38 | * Class: (class|all_class|instance|all)_attributes |
paul@31 | 39 | * Function: parameters, locals, all_locals |
paul@20 | 40 | * Module: module_attributes |
paul@20 | 41 | |
paul@20 | 42 | These specific methods are useful in certain situations. |
paul@20 | 43 | |
paul@20 | 44 | The above classes also provide a 'node' attribute, indicating the AST node where |
paul@20 | 45 | each such object is defined. |
paul@0 | 46 | """ |
paul@0 | 47 | |
paul@39 | 48 | from micropython.common import * |
paul@0 | 49 | import compiler.ast |
paul@0 | 50 | from compiler.visitor import ASTVisitor |
paul@0 | 51 | try: |
paul@0 | 52 | set |
paul@0 | 53 | except NameError: |
paul@0 | 54 | from sets import Set as set |
paul@0 | 55 | |
paul@39 | 56 | class InspectError(ProcessingError): pass |
paul@0 | 57 | |
paul@31 | 58 | class AtLeast: |
paul@31 | 59 | |
paul@31 | 60 | "A special representation for numbers of a given value or greater." |
paul@31 | 61 | |
paul@31 | 62 | def __init__(self, count): |
paul@31 | 63 | self.count = count |
paul@31 | 64 | |
paul@31 | 65 | def __eq__(self, other): |
paul@31 | 66 | return 0 |
paul@31 | 67 | |
paul@31 | 68 | __lt__ = __le__ = __eq__ |
paul@31 | 69 | |
paul@31 | 70 | def __ne__(self, other): |
paul@31 | 71 | return 1 |
paul@31 | 72 | |
paul@31 | 73 | def __gt__(self, other): |
paul@31 | 74 | if isinstance(other, AtLeast): |
paul@31 | 75 | return 0 |
paul@31 | 76 | else: |
paul@31 | 77 | return self.count > other |
paul@31 | 78 | |
paul@31 | 79 | def __ge__(self, other): |
paul@31 | 80 | if isinstance(other, AtLeast): |
paul@31 | 81 | return 0 |
paul@31 | 82 | else: |
paul@31 | 83 | return self.count >= other |
paul@31 | 84 | |
paul@31 | 85 | def __iadd__(self, other): |
paul@31 | 86 | if isinstance(other, AtLeast): |
paul@31 | 87 | self.count += other.count |
paul@31 | 88 | else: |
paul@31 | 89 | self.count += other |
paul@31 | 90 | return self |
paul@31 | 91 | |
paul@31 | 92 | def __radd__(self, other): |
paul@31 | 93 | if isinstance(other, AtLeast): |
paul@31 | 94 | return AtLeast(self.count + other.count) |
paul@31 | 95 | else: |
paul@31 | 96 | return AtLeast(self.count + other) |
paul@31 | 97 | |
paul@31 | 98 | def __repr__(self): |
paul@31 | 99 | return "AtLeast(%r)" % self.count |
paul@31 | 100 | |
paul@12 | 101 | # Mix-ins and abstract classes. |
paul@12 | 102 | |
paul@3 | 103 | class NamespaceDict: |
paul@3 | 104 | |
paul@3 | 105 | "A mix-in providing dictionary methods." |
paul@3 | 106 | |
paul@24 | 107 | def __init__(self, global_namespace=None): |
paul@12 | 108 | self.namespace = {} |
paul@12 | 109 | self.globals = set() |
paul@24 | 110 | self.global_namespace = global_namespace |
paul@23 | 111 | |
paul@3 | 112 | def __getitem__(self, name): |
paul@3 | 113 | return self.namespace[name] |
paul@3 | 114 | |
paul@15 | 115 | def get(self, name, default=None): |
paul@15 | 116 | return self.namespace.get(name, default) |
paul@15 | 117 | |
paul@12 | 118 | def __setitem__(self, name, value): |
paul@37 | 119 | self.set(name, value) |
paul@31 | 120 | |
paul@34 | 121 | def set(self, name, value, single_assignment=1): |
paul@47 | 122 | |
paul@47 | 123 | """ |
paul@47 | 124 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@47 | 125 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@47 | 126 | this operation (or whether the operation covers potentially many |
paul@47 | 127 | assignments in the lifetime of a program). |
paul@47 | 128 | """ |
paul@47 | 129 | |
paul@37 | 130 | if name in self.globals: |
paul@37 | 131 | self.global_namespace.set(name, value, 0) |
paul@37 | 132 | else: |
paul@37 | 133 | attr = self._set(name, value) |
paul@12 | 134 | |
paul@37 | 135 | # NOTE: Insist on assignments with known values. |
paul@23 | 136 | |
paul@37 | 137 | if value is not None: |
paul@37 | 138 | attr.update(value, single_assignment) |
paul@32 | 139 | |
paul@32 | 140 | def set_module(self, name, value): |
paul@47 | 141 | |
paul@47 | 142 | """ |
paul@47 | 143 | A specialised set operation, making 'name' refer to 'value' in the |
paul@47 | 144 | context of making a module reference available in association with |
paul@47 | 145 | 'name' as part of the import of that module or a submodule of that |
paul@47 | 146 | module. |
paul@47 | 147 | """ |
paul@47 | 148 | |
paul@36 | 149 | attr = self._set(name, value) |
paul@36 | 150 | if attr.assignments is None: |
paul@36 | 151 | attr.assignments = 1 |
paul@36 | 152 | attr.assignment_values.add(value) |
paul@36 | 153 | |
paul@36 | 154 | def _set(self, name, value): |
paul@47 | 155 | |
paul@47 | 156 | "The underlying set operation associating 'name' with 'value'." |
paul@47 | 157 | |
paul@36 | 158 | if not self.namespace.has_key(name): |
paul@49 | 159 | self.namespace[name] = Attr(None, self, name, value) |
paul@36 | 160 | return self.namespace[name] |
paul@23 | 161 | |
paul@12 | 162 | def __delitem__(self, name): |
paul@12 | 163 | del self.namespace[name] |
paul@12 | 164 | |
paul@13 | 165 | def has_key(self, name): |
paul@13 | 166 | return self.namespace.has_key(name) |
paul@13 | 167 | |
paul@3 | 168 | def keys(self): |
paul@3 | 169 | return self.namespace.keys() |
paul@3 | 170 | |
paul@7 | 171 | def values(self): |
paul@7 | 172 | return self.namespace.values() |
paul@7 | 173 | |
paul@7 | 174 | def items(self): |
paul@7 | 175 | return self.namespace.items() |
paul@7 | 176 | |
paul@12 | 177 | def make_global(self, name): |
paul@12 | 178 | if not self.namespace.has_key(name): |
paul@12 | 179 | self.globals.add(name) |
paul@12 | 180 | else: |
paul@43 | 181 | raise InspectError(self.full_name(), self.node, "Name %r is both global and local in %r" % (name, self.full_name())) |
paul@12 | 182 | |
paul@32 | 183 | def get_assignments(self, name): |
paul@32 | 184 | if self.assignments.has_key(name): |
paul@32 | 185 | return max(self.assignments[name], len(self.assignment_values[name])) |
paul@32 | 186 | else: |
paul@32 | 187 | return None |
paul@32 | 188 | |
paul@49 | 189 | def attributes_as_list(self): |
paul@49 | 190 | self.finalise_attributes() |
paul@49 | 191 | l = [None] * len(self.keys()) |
paul@49 | 192 | for attr in self.values(): |
paul@37 | 193 | l[attr.position] = attr |
paul@37 | 194 | return l |
paul@37 | 195 | |
paul@49 | 196 | def finalise_attributes(self): |
paul@49 | 197 | |
paul@49 | 198 | "Make sure all attributes are fully defined." |
paul@49 | 199 | |
paul@49 | 200 | # The default action is to assign attribute positions sequentially. |
paul@49 | 201 | |
paul@49 | 202 | for i, attr in enumerate(self.values()): |
paul@49 | 203 | attr.position = i |
paul@49 | 204 | |
paul@4 | 205 | class Naming: |
paul@4 | 206 | |
paul@4 | 207 | "A mix-in providing naming conveniences." |
paul@4 | 208 | |
paul@4 | 209 | def full_name(self): |
paul@4 | 210 | if self.name is not None: |
paul@53 | 211 | return self.parent.full_name() + "." + self.name |
paul@4 | 212 | else: |
paul@53 | 213 | return self.parent.full_name() |
paul@4 | 214 | |
paul@12 | 215 | # Program data structures. |
paul@12 | 216 | |
paul@10 | 217 | class Attr: |
paul@10 | 218 | |
paul@54 | 219 | "An attribute entry having a parent as context." |
paul@10 | 220 | |
paul@36 | 221 | def __init__(self, position, parent, name, value=None, assignments=None): |
paul@10 | 222 | self.position = position |
paul@10 | 223 | self.parent = parent |
paul@36 | 224 | self.name = name |
paul@10 | 225 | self.value = value |
paul@10 | 226 | |
paul@36 | 227 | # Number of assignments per name. |
paul@36 | 228 | |
paul@36 | 229 | self.assignments = assignments |
paul@36 | 230 | self.assignment_values = set() |
paul@36 | 231 | |
paul@36 | 232 | def update(self, value, single_assignment): |
paul@47 | 233 | |
paul@47 | 234 | """ |
paul@47 | 235 | Update the attribute, adding the 'value' provided to the known values |
paul@47 | 236 | associated with the attribute, changing the number of assignments |
paul@47 | 237 | according to the 'single_assignment' status of the operation, where |
paul@47 | 238 | a true value indicates that only one assignment is associated with the |
paul@47 | 239 | update, and a false value indicates that potentially many assignments |
paul@47 | 240 | may be involved. |
paul@47 | 241 | """ |
paul@47 | 242 | |
paul@36 | 243 | if self.assignments is None: |
paul@36 | 244 | if single_assignment: |
paul@36 | 245 | self.assignments = 1 |
paul@36 | 246 | else: |
paul@36 | 247 | self.assignments = AtLeast(1) |
paul@36 | 248 | else: |
paul@36 | 249 | if single_assignment: |
paul@36 | 250 | self.assignments += 1 |
paul@36 | 251 | else: |
paul@36 | 252 | self.assignments += AtLeast(1) |
paul@36 | 253 | self.assignment_values.add(value) |
paul@36 | 254 | |
paul@10 | 255 | def __repr__(self): |
paul@36 | 256 | return "Attr(%r, %r, %r, %r, %r)" % (self.position, self.parent, self.name, self.value, self.assignments) |
paul@10 | 257 | |
paul@21 | 258 | class Const: |
paul@21 | 259 | |
paul@55 | 260 | "A constant object with no context." |
paul@21 | 261 | |
paul@21 | 262 | def __init__(self, value): |
paul@21 | 263 | self.value = value |
paul@21 | 264 | |
paul@21 | 265 | # Image generation details. |
paul@21 | 266 | |
paul@21 | 267 | self.location = None |
paul@21 | 268 | |
paul@21 | 269 | def __repr__(self): |
paul@37 | 270 | if self.location is not None: |
paul@37 | 271 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@37 | 272 | else: |
paul@37 | 273 | return "Const(%r)" % self.value |
paul@21 | 274 | |
paul@21 | 275 | def __eq__(self, other): |
paul@21 | 276 | return self.value == other.value |
paul@21 | 277 | |
paul@21 | 278 | def __hash__(self): |
paul@21 | 279 | return hash(self.value) |
paul@21 | 280 | |
paul@4 | 281 | class Class(NamespaceDict, Naming): |
paul@0 | 282 | |
paul@0 | 283 | "An inspected class." |
paul@0 | 284 | |
paul@53 | 285 | def __init__(self, name, parent, global_namespace=None, node=None): |
paul@54 | 286 | |
paul@54 | 287 | """ |
paul@54 | 288 | Initialise the class with the given 'name', 'parent' object, optional |
paul@54 | 289 | 'global_namespace' and optional AST 'node'. |
paul@54 | 290 | """ |
paul@54 | 291 | |
paul@24 | 292 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 293 | self.name = name |
paul@53 | 294 | self.parent = parent |
paul@17 | 295 | self.node = node |
paul@12 | 296 | |
paul@53 | 297 | # Superclasses, descendants and attributes. |
paul@47 | 298 | |
paul@0 | 299 | self.bases = [] |
paul@53 | 300 | self.descendants = set() |
paul@46 | 301 | self.instattr = set() # instance attributes |
paul@49 | 302 | self.relocated = set() # attributes which do not have the same |
paul@49 | 303 | # position as those of the same name in |
paul@49 | 304 | # some superclasses |
paul@12 | 305 | |
paul@47 | 306 | # Caches. |
paul@47 | 307 | |
paul@46 | 308 | self.all_instattr = None # cache for instance_attributes |
paul@46 | 309 | self.all_instattr_names = None # from all_instattr |
paul@46 | 310 | self.all_classattr = None # cache for all_class_attributes |
paul@12 | 311 | self.all_classattr_names = None # from all_classattr |
paul@46 | 312 | self.allattr = None # cache for all_attributes |
paul@46 | 313 | self.allattr_names = None # from allattr |
paul@9 | 314 | |
paul@9 | 315 | # Image generation details. |
paul@9 | 316 | |
paul@9 | 317 | self.location = None |
paul@19 | 318 | self.code_location = None |
paul@61 | 319 | self.instantiator = None |
paul@0 | 320 | |
paul@12 | 321 | def __repr__(self): |
paul@37 | 322 | if self.location is not None: |
paul@53 | 323 | return "Class(%r, %r, location=%r)" % (self.name, self.parent, self.location) |
paul@37 | 324 | else: |
paul@53 | 325 | return "Class(%r, %r)" % (self.name, self.parent) |
paul@12 | 326 | |
paul@49 | 327 | def finalise_attributes(self): |
paul@49 | 328 | |
paul@49 | 329 | "Make sure that all attributes are fully defined." |
paul@49 | 330 | |
paul@49 | 331 | self.finalise_class_attributes() |
paul@49 | 332 | self.finalise_instance_attributes() |
paul@49 | 333 | |
paul@61 | 334 | def get_instantiator(self): |
paul@61 | 335 | |
paul@61 | 336 | "Return a function which can be used to instantiate the class." |
paul@61 | 337 | |
paul@61 | 338 | if self.instantiator is None: |
paul@61 | 339 | init_method = self.all_class_attributes()["__init__"].value |
paul@61 | 340 | self.instantiator = init_method.function_from_method() |
paul@61 | 341 | return self.instantiator |
paul@61 | 342 | |
paul@49 | 343 | # Class-specific methods. |
paul@49 | 344 | |
paul@0 | 345 | def add_base(self, base): |
paul@0 | 346 | self.bases.append(base) |
paul@53 | 347 | base.add_descendant(self) |
paul@0 | 348 | |
paul@12 | 349 | def add_instance_attribute(self, name): |
paul@12 | 350 | self.instattr.add(name) |
paul@9 | 351 | |
paul@53 | 352 | def add_descendant(self, cls): |
paul@53 | 353 | self.descendants.add(cls) |
paul@53 | 354 | for base in self.bases: |
paul@53 | 355 | base.add_descendant(cls) |
paul@53 | 356 | |
paul@36 | 357 | "Return the attribute names provided by this class only." |
paul@9 | 358 | |
paul@36 | 359 | class_attribute_names = NamespaceDict.keys |
paul@4 | 360 | |
paul@4 | 361 | def class_attributes(self): |
paul@4 | 362 | |
paul@12 | 363 | "Return class attributes provided by this class only." |
paul@4 | 364 | |
paul@49 | 365 | self.finalise_class_attributes() |
paul@53 | 366 | return dict(self) |
paul@12 | 367 | |
paul@12 | 368 | def all_class_attribute_names(self): |
paul@12 | 369 | |
paul@12 | 370 | "Return the attribute names provided by classes in this hierarchy." |
paul@12 | 371 | |
paul@12 | 372 | if self.all_classattr_names is None: |
paul@12 | 373 | self.all_class_attributes() |
paul@12 | 374 | return self.all_classattr_names |
paul@12 | 375 | |
paul@12 | 376 | def all_class_attributes(self): |
paul@12 | 377 | |
paul@12 | 378 | "Return all class attributes, indicating the class which provides them." |
paul@12 | 379 | |
paul@49 | 380 | self.finalise_class_attributes() |
paul@49 | 381 | return self.all_classattr |
paul@49 | 382 | |
paul@49 | 383 | def finalise_class_attributes(self): |
paul@49 | 384 | |
paul@49 | 385 | "Make sure that the class attributes are fully defined." |
paul@49 | 386 | |
paul@12 | 387 | if self.all_classattr is None: |
paul@12 | 388 | self.all_classattr = {} |
paul@49 | 389 | clsattr = {} |
paul@49 | 390 | |
paul@49 | 391 | # Record provisional position information for attributes of this |
paul@49 | 392 | # class. |
paul@49 | 393 | |
paul@49 | 394 | for name in self.class_attributes().keys(): |
paul@49 | 395 | clsattr[name] = set() # position not yet defined |
paul@12 | 396 | |
paul@9 | 397 | reversed_bases = self.bases[:] |
paul@9 | 398 | reversed_bases.reverse() |
paul@49 | 399 | |
paul@49 | 400 | # For the bases in reverse order, acquire class attribute details. |
paul@49 | 401 | |
paul@9 | 402 | for cls in reversed_bases: |
paul@49 | 403 | for name, attr in cls.all_class_attributes().items(): |
paul@49 | 404 | self.all_classattr[name] = attr |
paul@10 | 405 | |
paul@49 | 406 | # Record previous attribute information. |
paul@49 | 407 | |
paul@49 | 408 | if clsattr.has_key(name): |
paul@49 | 409 | clsattr[name].add(attr.position) |
paul@49 | 410 | |
paul@49 | 411 | # Record class attributes provided by this class and its bases, |
paul@49 | 412 | # along with their positions. |
paul@10 | 413 | |
paul@12 | 414 | self.all_classattr.update(self.class_attributes()) |
paul@9 | 415 | |
paul@49 | 416 | if clsattr: |
paul@49 | 417 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@49 | 418 | self.all_classattr[name].position = i |
paul@49 | 419 | |
paul@12 | 420 | return self.all_classattr |
paul@4 | 421 | |
paul@7 | 422 | def instance_attribute_names(self): |
paul@7 | 423 | |
paul@7 | 424 | "Return the instance attribute names provided by the class." |
paul@7 | 425 | |
paul@12 | 426 | if self.all_instattr_names is None: |
paul@12 | 427 | self.instance_attributes() |
paul@12 | 428 | return self.all_instattr_names |
paul@7 | 429 | |
paul@4 | 430 | def instance_attributes(self): |
paul@4 | 431 | |
paul@9 | 432 | "Return instance-only attributes for instances of this class." |
paul@9 | 433 | |
paul@49 | 434 | self.finalise_instance_attributes() |
paul@49 | 435 | return self.all_instattr |
paul@49 | 436 | |
paul@49 | 437 | def finalise_instance_attributes(self): |
paul@49 | 438 | |
paul@49 | 439 | "Make sure that the instance attributes are fully defined." |
paul@49 | 440 | |
paul@12 | 441 | if self.all_instattr is None: |
paul@12 | 442 | self.all_instattr = {} |
paul@46 | 443 | instattr = {} |
paul@12 | 444 | |
paul@49 | 445 | # Record provisional position information for attributes of this |
paul@49 | 446 | # instance. |
paul@49 | 447 | |
paul@49 | 448 | for name in self.instattr: |
paul@49 | 449 | instattr[name] = set() # position not yet defined |
paul@49 | 450 | |
paul@12 | 451 | reversed_bases = self.bases[:] |
paul@12 | 452 | reversed_bases.reverse() |
paul@46 | 453 | |
paul@46 | 454 | # For the bases in reverse order, acquire instance attribute |
paul@46 | 455 | # details. |
paul@46 | 456 | |
paul@12 | 457 | for cls in reversed_bases: |
paul@46 | 458 | for name, attr in cls.instance_attributes().items(): |
paul@49 | 459 | |
paul@49 | 460 | # Record previous attribute information. |
paul@12 | 461 | |
paul@49 | 462 | if instattr.has_key(name): |
paul@49 | 463 | instattr[name].add(attr.position) |
paul@46 | 464 | |
paul@48 | 465 | # Cache the attributes by converting the positioned attributes into |
paul@48 | 466 | # a dictionary. |
paul@48 | 467 | |
paul@46 | 468 | if not instattr: |
paul@46 | 469 | self.all_instattr = {} |
paul@46 | 470 | else: |
paul@49 | 471 | self.all_instattr = self._get_attributes(instattr) |
paul@12 | 472 | |
paul@12 | 473 | self.all_instattr_names = self.all_instattr.keys() |
paul@12 | 474 | |
paul@12 | 475 | return self.all_instattr |
paul@9 | 476 | |
paul@49 | 477 | def _get_position_list(self, positions): |
paul@48 | 478 | |
paul@48 | 479 | """ |
paul@49 | 480 | Return a list of attribute names for the given 'positions' mapping from |
paul@49 | 481 | names to positions, indicating the positions of the attributes in the |
paul@49 | 482 | final instance structure. |
paul@48 | 483 | """ |
paul@48 | 484 | |
paul@49 | 485 | position_items = positions.items() |
paul@49 | 486 | namearray = [None] * len(position_items) |
paul@48 | 487 | |
paul@48 | 488 | # Get the positions in ascending order of list size, with lists |
paul@48 | 489 | # of the same size ordered according to their smallest position |
paul@48 | 490 | # value. |
paul@48 | 491 | |
paul@49 | 492 | position_items.sort(self._cmp_positions) |
paul@48 | 493 | |
paul@48 | 494 | # Get the names in position order. |
paul@48 | 495 | |
paul@48 | 496 | held = [] |
paul@48 | 497 | |
paul@49 | 498 | for name, pos in position_items: |
paul@48 | 499 | pos = list(pos) |
paul@51 | 500 | pos.sort() |
paul@51 | 501 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@49 | 502 | namearray[pos[0]] = name |
paul@48 | 503 | else: |
paul@49 | 504 | if pos: |
paul@49 | 505 | self.relocated.add(name) |
paul@48 | 506 | held.append((name, pos)) |
paul@48 | 507 | |
paul@49 | 508 | for i, attr in enumerate(namearray): |
paul@48 | 509 | if attr is None: |
paul@48 | 510 | name, pos = held.pop() |
paul@49 | 511 | namearray[i] = name |
paul@49 | 512 | |
paul@49 | 513 | #print self.name, positions |
paul@49 | 514 | #print "->", namearray |
paul@49 | 515 | return namearray |
paul@49 | 516 | |
paul@49 | 517 | def _get_attributes(self, positions): |
paul@48 | 518 | |
paul@49 | 519 | """ |
paul@49 | 520 | For the given 'positions' mapping from names to positions, return a |
paul@49 | 521 | dictionary mapping names to Attr instances incorporating information |
paul@49 | 522 | about their positions in the final instance structure. |
paul@49 | 523 | """ |
paul@49 | 524 | |
paul@49 | 525 | d = {} |
paul@49 | 526 | for i, name in enumerate(self._get_position_list(positions)): |
paul@54 | 527 | d[name] = Attr(i, Instance(), name, None) |
paul@49 | 528 | return d |
paul@48 | 529 | |
paul@46 | 530 | def _cmp_positions(self, a, b): |
paul@48 | 531 | |
paul@48 | 532 | "Compare name plus position list operands 'a' and 'b'." |
paul@48 | 533 | |
paul@46 | 534 | name_a, list_a = a |
paul@46 | 535 | name_b, list_b = b |
paul@46 | 536 | if len(list_a) < len(list_b): |
paul@46 | 537 | return -1 |
paul@46 | 538 | elif len(list_a) > len(list_b): |
paul@46 | 539 | return 1 |
paul@49 | 540 | elif not list_a: |
paul@49 | 541 | return 0 |
paul@46 | 542 | else: |
paul@46 | 543 | return cmp(min(list_a), min(list_b)) |
paul@46 | 544 | |
paul@9 | 545 | def all_attribute_names(self): |
paul@9 | 546 | |
paul@9 | 547 | """ |
paul@9 | 548 | Return the names of all attributes provided by instances of this class. |
paul@9 | 549 | """ |
paul@9 | 550 | |
paul@9 | 551 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@9 | 552 | return self.allattr_names |
paul@9 | 553 | |
paul@9 | 554 | def all_attributes(self): |
paul@9 | 555 | |
paul@4 | 556 | """ |
paul@4 | 557 | Return all attributes for an instance, indicating either the class which |
paul@4 | 558 | provides them or that the instance itself provides them. |
paul@4 | 559 | """ |
paul@4 | 560 | |
paul@9 | 561 | if self.allattr is None: |
paul@9 | 562 | self.allattr = {} |
paul@12 | 563 | self.allattr.update(self.all_class_attributes()) |
paul@47 | 564 | for name, attr in self.instance_attributes().items(): |
paul@9 | 565 | if self.allattr.has_key(name): |
paul@9 | 566 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@47 | 567 | self.allattr[name] = attr |
paul@9 | 568 | return self.allattr |
paul@4 | 569 | |
paul@12 | 570 | class Function(NamespaceDict, Naming): |
paul@0 | 571 | |
paul@0 | 572 | "An inspected function." |
paul@0 | 573 | |
paul@62 | 574 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, global_namespace=None, node=None): |
paul@24 | 575 | NamespaceDict.__init__(self, global_namespace) |
paul@3 | 576 | self.name = name |
paul@53 | 577 | self.parent = parent |
paul@3 | 578 | self.argnames = argnames |
paul@62 | 579 | self.defaults = defaults |
paul@59 | 580 | self.positional_names = self.argnames[:] |
paul@59 | 581 | if has_dstar: |
paul@59 | 582 | self.dstar_name = self.positional_names[-1] |
paul@59 | 583 | del self.positional_names[-1] |
paul@59 | 584 | if has_star: |
paul@59 | 585 | self.star_name = self.positional_names[-1] |
paul@59 | 586 | del self.positional_names[-1] |
paul@3 | 587 | self.has_star = has_star |
paul@3 | 588 | self.has_dstar = has_dstar |
paul@17 | 589 | self.node = node |
paul@3 | 590 | |
paul@36 | 591 | # Caches. |
paul@36 | 592 | |
paul@12 | 593 | self.localnames = None # cache for locals |
paul@12 | 594 | |
paul@17 | 595 | # Add parameters to the namespace. |
paul@17 | 596 | |
paul@25 | 597 | self._add_parameters(argnames) |
paul@17 | 598 | |
paul@9 | 599 | # Image generation details. |
paul@9 | 600 | |
paul@9 | 601 | self.location = None |
paul@19 | 602 | self.code_location = None |
paul@9 | 603 | |
paul@25 | 604 | def _add_parameters(self, argnames): |
paul@25 | 605 | for name in argnames: |
paul@25 | 606 | if isinstance(name, tuple): |
paul@25 | 607 | self._add_parameters(name) |
paul@25 | 608 | else: |
paul@54 | 609 | self.set(name, None) |
paul@25 | 610 | |
paul@4 | 611 | def __repr__(self): |
paul@37 | 612 | if self.location is not None: |
paul@62 | 613 | return "Function(%r, %r, %r, %r, %r, %r, location=%r)" % ( |
paul@62 | 614 | self.name, self.parent, self.argnames, self.defaults, self.has_star, self.has_dstar, self.location |
paul@37 | 615 | ) |
paul@37 | 616 | else: |
paul@62 | 617 | return "Function(%r, %r, %r, %r, %r, %r)" % ( |
paul@62 | 618 | self.name, self.parent, self.argnames, self.defaults, self.has_star, self.has_dstar |
paul@37 | 619 | ) |
paul@4 | 620 | |
paul@12 | 621 | def make_global(self, name): |
paul@13 | 622 | if name not in self.argnames and not self.has_key(name): |
paul@12 | 623 | self.globals.add(name) |
paul@12 | 624 | else: |
paul@40 | 625 | raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@12 | 626 | |
paul@7 | 627 | def parameters(self): |
paul@7 | 628 | |
paul@7 | 629 | """ |
paul@7 | 630 | Return a dictionary mapping parameter names to their position in the |
paul@7 | 631 | parameter list. |
paul@7 | 632 | """ |
paul@7 | 633 | |
paul@7 | 634 | parameters = {} |
paul@7 | 635 | for i, name in enumerate(self.argnames): |
paul@7 | 636 | parameters[name] = i |
paul@7 | 637 | return parameters |
paul@7 | 638 | |
paul@36 | 639 | def all_locals(self): |
paul@36 | 640 | |
paul@36 | 641 | "Return a dictionary mapping names to local and parameter details." |
paul@36 | 642 | |
paul@53 | 643 | return dict(self) |
paul@36 | 644 | |
paul@12 | 645 | def locals(self): |
paul@12 | 646 | |
paul@12 | 647 | "Return a dictionary mapping names to local details." |
paul@12 | 648 | |
paul@12 | 649 | if self.localnames is None: |
paul@12 | 650 | self.localnames = {} |
paul@36 | 651 | self.localnames.update(self.all_locals()) |
paul@36 | 652 | for name in self.argnames: |
paul@36 | 653 | del self.localnames[name] |
paul@12 | 654 | return self.localnames |
paul@12 | 655 | |
paul@54 | 656 | def is_relocated(self, name): |
paul@54 | 657 | |
paul@54 | 658 | """ |
paul@54 | 659 | Determine whether the given attribute 'name' is relocated for instances |
paul@54 | 660 | having this function as a method. |
paul@54 | 661 | """ |
paul@54 | 662 | |
paul@54 | 663 | for cls in self.parent.descendants: |
paul@54 | 664 | if name in cls.relocated: |
paul@54 | 665 | return 1 |
paul@54 | 666 | return 0 |
paul@54 | 667 | |
paul@54 | 668 | def finalise_attributes(self): |
paul@54 | 669 | |
paul@54 | 670 | "Make sure all attributes are fully defined." |
paul@54 | 671 | |
paul@54 | 672 | for i, name in enumerate(self.argnames): |
paul@54 | 673 | self[name].position = i |
paul@54 | 674 | |
paul@54 | 675 | j = i |
paul@55 | 676 | for i, attr in enumerate(self.locals().values()): |
paul@54 | 677 | attr.position = i + j |
paul@54 | 678 | |
paul@61 | 679 | def function_from_method(self): |
paul@61 | 680 | |
paul@61 | 681 | "Make a function from a method." |
paul@61 | 682 | |
paul@62 | 683 | return Function(self.name, self.parent, self.argnames[1:], self.defaults, |
paul@62 | 684 | self.has_star, self.has_dstar, self.global_namespace, self.node) |
paul@61 | 685 | |
paul@53 | 686 | class UnresolvedName(NamespaceDict): |
paul@3 | 687 | |
paul@3 | 688 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 689 | |
paul@24 | 690 | def __init__(self, name, parent_name, global_namespace=None): |
paul@24 | 691 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 692 | self.name = name |
paul@7 | 693 | self.parent_name = parent_name |
paul@0 | 694 | |
paul@12 | 695 | def all_class_attributes(self): |
paul@7 | 696 | return {} |
paul@7 | 697 | |
paul@13 | 698 | def instance_attributes(self): |
paul@13 | 699 | return {} |
paul@13 | 700 | |
paul@4 | 701 | def __repr__(self): |
paul@7 | 702 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@4 | 703 | |
paul@53 | 704 | def full_name(self): |
paul@53 | 705 | if self.name is not None: |
paul@53 | 706 | return self.parent_name + "." + self.name |
paul@53 | 707 | else: |
paul@53 | 708 | return self.parent_name |
paul@53 | 709 | |
paul@54 | 710 | class Instance: |
paul@54 | 711 | |
paul@54 | 712 | "A placeholder indicating the involvement of an instance." |
paul@54 | 713 | |
paul@54 | 714 | def __repr__(self): |
paul@54 | 715 | return "Instance()" |
paul@54 | 716 | |
paul@15 | 717 | class Module(NamespaceDict): |
paul@0 | 718 | |
paul@20 | 719 | "An inspected module's core details." |
paul@20 | 720 | |
paul@15 | 721 | def __init__(self, name): |
paul@24 | 722 | NamespaceDict.__init__(self, self) |
paul@1 | 723 | self.name = name |
paul@0 | 724 | |
paul@7 | 725 | # Complete lists of classes and functions. |
paul@7 | 726 | |
paul@33 | 727 | self.all_objects = set() |
paul@0 | 728 | |
paul@22 | 729 | # Constant records. |
paul@22 | 730 | |
paul@22 | 731 | self.constant_values = {} |
paul@22 | 732 | self.constant_list = None # cache for constants |
paul@22 | 733 | |
paul@15 | 734 | # Image generation details. |
paul@15 | 735 | |
paul@15 | 736 | self.location = None |
paul@19 | 737 | self.code_location = None |
paul@15 | 738 | |
paul@20 | 739 | # Original location details. |
paul@20 | 740 | |
paul@20 | 741 | self.node = None |
paul@20 | 742 | |
paul@15 | 743 | def full_name(self): |
paul@15 | 744 | return self.name |
paul@15 | 745 | |
paul@15 | 746 | def __repr__(self): |
paul@37 | 747 | if self.location is not None: |
paul@37 | 748 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@37 | 749 | else: |
paul@37 | 750 | return "Module(%r)" % self.name |
paul@15 | 751 | |
paul@15 | 752 | # Attribute methods. |
paul@15 | 753 | |
paul@36 | 754 | "Return the module attribute names provided by the module." |
paul@15 | 755 | |
paul@36 | 756 | module_attribute_names = NamespaceDict.keys |
paul@15 | 757 | |
paul@15 | 758 | def module_attributes(self): |
paul@15 | 759 | |
paul@15 | 760 | "Return a dictionary mapping names to module attributes." |
paul@15 | 761 | |
paul@53 | 762 | return dict(self) |
paul@15 | 763 | |
paul@22 | 764 | def constants(self): |
paul@22 | 765 | |
paul@22 | 766 | "Return a list of constants." |
paul@22 | 767 | |
paul@22 | 768 | if self.constant_list is None: |
paul@22 | 769 | self.constant_list = list(self.constant_values.values()) |
paul@22 | 770 | |
paul@22 | 771 | return self.constant_list |
paul@22 | 772 | |
paul@21 | 773 | # Program visitors. |
paul@21 | 774 | |
paul@15 | 775 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 776 | |
paul@20 | 777 | """ |
paul@20 | 778 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 779 | capable of being used as an AST visitor. |
paul@20 | 780 | """ |
paul@15 | 781 | |
paul@15 | 782 | def __init__(self, name, importer=None): |
paul@15 | 783 | ASTVisitor.__init__(self) |
paul@15 | 784 | Module.__init__(self, name) |
paul@15 | 785 | self.visitor = self |
paul@15 | 786 | |
paul@15 | 787 | self.importer = importer |
paul@61 | 788 | self.builtins = self.importer.modules.get("__builtins__") |
paul@15 | 789 | self.loaded = 0 |
paul@15 | 790 | |
paul@1 | 791 | # Current expression state. |
paul@1 | 792 | |
paul@1 | 793 | self.expr = None |
paul@1 | 794 | |
paul@1 | 795 | # Namespace state. |
paul@1 | 796 | |
paul@34 | 797 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@34 | 798 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 799 | self.namespaces = [] |
paul@0 | 800 | self.module = None |
paul@0 | 801 | |
paul@0 | 802 | def parse(self, filename): |
paul@9 | 803 | |
paul@9 | 804 | "Parse the file having the given 'filename'." |
paul@9 | 805 | |
paul@0 | 806 | module = compiler.parseFile(filename) |
paul@0 | 807 | self.process(module) |
paul@0 | 808 | |
paul@0 | 809 | def process(self, module): |
paul@9 | 810 | |
paul@9 | 811 | "Process the given 'module'." |
paul@9 | 812 | |
paul@20 | 813 | self.node = self.module = module |
paul@1 | 814 | processed = self.dispatch(module) |
paul@13 | 815 | if self.has_key("__all__"): |
paul@12 | 816 | all = self["__all__"] |
paul@1 | 817 | if isinstance(all, compiler.ast.List): |
paul@1 | 818 | for n in all.nodes: |
paul@12 | 819 | self[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 820 | return processed |
paul@0 | 821 | |
paul@3 | 822 | def vacuum(self): |
paul@9 | 823 | |
paul@9 | 824 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 825 | |
paul@13 | 826 | for name, value in self.items(): |
paul@3 | 827 | if isinstance(value, Module) and not value.loaded: |
paul@12 | 828 | del self[name] |
paul@0 | 829 | |
paul@16 | 830 | # Complain about globals not initialised at the module level. |
paul@16 | 831 | |
paul@16 | 832 | if isinstance(value, Global): |
paul@16 | 833 | print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) |
paul@16 | 834 | |
paul@3 | 835 | # Namespace methods. |
paul@0 | 836 | |
paul@0 | 837 | def store(self, name, obj): |
paul@12 | 838 | |
paul@12 | 839 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 840 | |
paul@12 | 841 | if not self.namespaces: |
paul@34 | 842 | self.set(name, obj, not self.in_loop) |
paul@12 | 843 | else: |
paul@34 | 844 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 845 | |
paul@12 | 846 | # Record all non-local objects. |
paul@12 | 847 | |
paul@12 | 848 | if not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@33 | 849 | self.all_objects.add(obj) |
paul@0 | 850 | |
paul@34 | 851 | def store_instance_attr(self, name): |
paul@12 | 852 | |
paul@12 | 853 | "Record instance attribute 'name' in the current class." |
paul@12 | 854 | |
paul@0 | 855 | if self.in_init: |
paul@15 | 856 | |
paul@12 | 857 | # Current namespace is the function. |
paul@12 | 858 | # Previous namespace is the class. |
paul@15 | 859 | |
paul@12 | 860 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 861 | |
paul@7 | 862 | def get_parent(self): |
paul@12 | 863 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 864 | |
paul@3 | 865 | # Visitor methods. |
paul@3 | 866 | |
paul@3 | 867 | def default(self, node, *args): |
paul@39 | 868 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 869 | |
paul@3 | 870 | def dispatch(self, node, *args): |
paul@3 | 871 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 872 | |
paul@0 | 873 | def NOP(self, node): |
paul@21 | 874 | for n in node.getChildNodes(): |
paul@21 | 875 | self.dispatch(n) |
paul@19 | 876 | return None |
paul@0 | 877 | |
paul@1 | 878 | visitAdd = NOP |
paul@1 | 879 | |
paul@0 | 880 | visitAnd = NOP |
paul@0 | 881 | |
paul@4 | 882 | visitAssert = NOP |
paul@4 | 883 | |
paul@0 | 884 | def visitAssign(self, node): |
paul@0 | 885 | self.expr = self.dispatch(node.expr) |
paul@0 | 886 | for n in node.nodes: |
paul@0 | 887 | self.dispatch(n) |
paul@0 | 888 | return None |
paul@0 | 889 | |
paul@0 | 890 | def visitAssAttr(self, node): |
paul@0 | 891 | expr = self.dispatch(node.expr) |
paul@36 | 892 | if isinstance(expr, Attr) and expr.name == "self": |
paul@34 | 893 | self.store_instance_attr(node.attrname) |
paul@0 | 894 | return None |
paul@0 | 895 | |
paul@0 | 896 | def visitAssList(self, node): |
paul@0 | 897 | for n in node.nodes: |
paul@0 | 898 | self.dispatch(n) |
paul@0 | 899 | return None |
paul@0 | 900 | |
paul@0 | 901 | def visitAssName(self, node): |
paul@36 | 902 | if isinstance(self.expr, Attr): |
paul@36 | 903 | self.store(node.name, self.expr.value) |
paul@36 | 904 | else: |
paul@36 | 905 | self.store(node.name, self.expr) |
paul@0 | 906 | return None |
paul@0 | 907 | |
paul@0 | 908 | visitAssTuple = visitAssList |
paul@0 | 909 | |
paul@2 | 910 | visitAugAssign = NOP |
paul@2 | 911 | |
paul@4 | 912 | visitBackquote = NOP |
paul@4 | 913 | |
paul@2 | 914 | visitBitand = NOP |
paul@2 | 915 | |
paul@2 | 916 | visitBitor = NOP |
paul@2 | 917 | |
paul@4 | 918 | visitBitxor = NOP |
paul@4 | 919 | |
paul@4 | 920 | visitBreak = NOP |
paul@4 | 921 | |
paul@0 | 922 | visitCallFunc = NOP |
paul@0 | 923 | |
paul@0 | 924 | def visitClass(self, node): |
paul@12 | 925 | if self.namespaces: |
paul@41 | 926 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@0 | 927 | else: |
paul@53 | 928 | cls = Class(node.name, self.get_parent(), self, node) |
paul@37 | 929 | |
paul@37 | 930 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 931 | # definitions of classes. |
paul@37 | 932 | |
paul@0 | 933 | for base in node.bases: |
paul@36 | 934 | expr = self.dispatch(base) |
paul@36 | 935 | if isinstance(expr, Attr): |
paul@36 | 936 | if expr.assignments != 1: |
paul@39 | 937 | raise InspectError(self.full_name(), node, |
paul@40 | 938 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 939 | else: |
paul@36 | 940 | cls.add_base(expr.value) |
paul@36 | 941 | else: # if expr is None: |
paul@39 | 942 | raise InspectError(self.full_name(), node, |
paul@40 | 943 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 944 | |
paul@59 | 945 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 946 | # If no bases exist, adopt the 'object' class. |
paul@59 | 947 | |
paul@61 | 948 | if not node.bases and not (self.name == "__builtins__" and node.name == "object") : |
paul@59 | 949 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@59 | 950 | cls.add_base(expr.value) |
paul@59 | 951 | |
paul@21 | 952 | # Make a back reference from the node for code generation. |
paul@21 | 953 | |
paul@39 | 954 | node.unit = cls |
paul@21 | 955 | |
paul@0 | 956 | # Make an entry for the class. |
paul@0 | 957 | |
paul@0 | 958 | self.store(node.name, cls) |
paul@0 | 959 | |
paul@59 | 960 | # Process the class body. |
paul@59 | 961 | |
paul@12 | 962 | self.namespaces.append(cls) |
paul@0 | 963 | self.dispatch(node.code) |
paul@12 | 964 | self.namespaces.pop() |
paul@0 | 965 | |
paul@19 | 966 | return None |
paul@0 | 967 | |
paul@4 | 968 | visitCompare = NOP |
paul@4 | 969 | |
paul@21 | 970 | def visitConst(self, node): |
paul@21 | 971 | const = Const(node.value) |
paul@22 | 972 | self.constant_values[node.value] = const |
paul@24 | 973 | return const |
paul@0 | 974 | |
paul@4 | 975 | visitContinue = NOP |
paul@4 | 976 | |
paul@4 | 977 | visitDecorators = NOP |
paul@4 | 978 | |
paul@0 | 979 | visitDict = NOP |
paul@0 | 980 | |
paul@0 | 981 | visitDiscard = NOP |
paul@0 | 982 | |
paul@4 | 983 | visitDiv = NOP |
paul@4 | 984 | |
paul@4 | 985 | visitEllipsis = NOP |
paul@4 | 986 | |
paul@4 | 987 | visitExec = NOP |
paul@4 | 988 | |
paul@4 | 989 | visitExpression = NOP |
paul@4 | 990 | |
paul@4 | 991 | visitFloorDiv = NOP |
paul@4 | 992 | |
paul@34 | 993 | def visitFor(self, node): |
paul@34 | 994 | self.in_loop = 1 |
paul@34 | 995 | self.NOP(node) |
paul@34 | 996 | self.in_loop = 0 |
paul@0 | 997 | |
paul@0 | 998 | def visitFrom(self, node): |
paul@0 | 999 | if self.importer is None: |
paul@39 | 1000 | raise InspectError(self.full_name(), node, |
paul@39 | 1001 | "Please use the micropython.Importer class for code which uses the 'from' statement.") |
paul@0 | 1002 | |
paul@0 | 1003 | module = self.importer.load(node.modname, 1) |
paul@0 | 1004 | |
paul@35 | 1005 | #if module is None: |
paul@35 | 1006 | # print "Warning:", node.modname, "not imported." |
paul@0 | 1007 | |
paul@0 | 1008 | for name, alias in node.names: |
paul@0 | 1009 | if name != "*": |
paul@4 | 1010 | if module is not None and module.namespace.has_key(name): |
paul@24 | 1011 | attr = module[name] |
paul@36 | 1012 | self.store(alias or name, attr.value) |
paul@2 | 1013 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 1014 | self.importer.load(attr.name) |
paul@24 | 1015 | |
paul@24 | 1016 | # Support the import of names from missing modules. |
paul@24 | 1017 | |
paul@3 | 1018 | else: |
paul@24 | 1019 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 1020 | else: |
paul@3 | 1021 | if module is not None: |
paul@3 | 1022 | for n in module.namespace.keys(): |
paul@24 | 1023 | attr = module[n] |
paul@36 | 1024 | self.store(n, attr.value) |
paul@3 | 1025 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 1026 | self.importer.load(attr.name) |
paul@0 | 1027 | |
paul@0 | 1028 | return None |
paul@0 | 1029 | |
paul@0 | 1030 | def visitFunction(self, node): |
paul@3 | 1031 | function = Function( |
paul@3 | 1032 | node.name, |
paul@53 | 1033 | self.get_parent(), |
paul@3 | 1034 | node.argnames, |
paul@62 | 1035 | node.defaults, |
paul@17 | 1036 | (node.flags & 4 != 0), |
paul@17 | 1037 | (node.flags & 8 != 0), |
paul@24 | 1038 | self, |
paul@17 | 1039 | node |
paul@3 | 1040 | ) |
paul@0 | 1041 | |
paul@21 | 1042 | # Make a back reference from the node for code generation. |
paul@21 | 1043 | |
paul@39 | 1044 | node.unit = function |
paul@21 | 1045 | |
paul@12 | 1046 | self.namespaces.append(function) |
paul@15 | 1047 | |
paul@12 | 1048 | # Current namespace is the function. |
paul@12 | 1049 | # Previous namespace is the class. |
paul@15 | 1050 | |
paul@12 | 1051 | if node.name == "__init__" and isinstance(self.namespaces[-2], Class): |
paul@0 | 1052 | self.in_init = 1 |
paul@15 | 1053 | |
paul@12 | 1054 | self.dispatch(node.code) |
paul@12 | 1055 | self.in_init = 0 |
paul@12 | 1056 | self.namespaces.pop() |
paul@3 | 1057 | |
paul@3 | 1058 | self.store(node.name, function) |
paul@0 | 1059 | return None |
paul@0 | 1060 | |
paul@4 | 1061 | visitGenExpr = NOP |
paul@4 | 1062 | |
paul@4 | 1063 | visitGenExprFor = NOP |
paul@4 | 1064 | |
paul@4 | 1065 | visitGenExprIf = NOP |
paul@4 | 1066 | |
paul@4 | 1067 | visitGenExprInner = NOP |
paul@4 | 1068 | |
paul@0 | 1069 | def visitGetattr(self, node): |
paul@0 | 1070 | expr = self.dispatch(node.expr) |
paul@36 | 1071 | if isinstance(expr, Attr): |
paul@36 | 1072 | value = expr.value |
paul@36 | 1073 | if isinstance(value, Module): |
paul@36 | 1074 | return value.namespace.get(node.attrname) |
paul@36 | 1075 | elif isinstance(value, UnresolvedName): |
paul@36 | 1076 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@61 | 1077 | if self.builtins is not None: |
paul@61 | 1078 | return self.builtins.get(node.attrname) |
paul@61 | 1079 | else: |
paul@61 | 1080 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@4 | 1081 | |
paul@12 | 1082 | def visitGlobal(self, node): |
paul@12 | 1083 | if self.namespaces: |
paul@12 | 1084 | for name in node.names: |
paul@12 | 1085 | self.namespaces[-1].make_global(name) |
paul@37 | 1086 | |
paul@37 | 1087 | # Record a global entry for the name in the module. |
paul@37 | 1088 | |
paul@15 | 1089 | if not self.has_key(name): |
paul@16 | 1090 | self[name] = Global() |
paul@0 | 1091 | |
paul@0 | 1092 | def visitIf(self, node): |
paul@0 | 1093 | for test, body in node.tests: |
paul@0 | 1094 | self.dispatch(body) |
paul@0 | 1095 | if node.else_ is not None: |
paul@0 | 1096 | self.dispatch(node.else_) |
paul@0 | 1097 | return None |
paul@0 | 1098 | |
paul@12 | 1099 | visitIfExp = NOP |
paul@12 | 1100 | |
paul@0 | 1101 | def visitImport(self, node): |
paul@0 | 1102 | if self.importer is None: |
paul@39 | 1103 | raise InspectError(self.full_name(), node, |
paul@39 | 1104 | "Please use the micropython.Importer class for code which uses the 'import' statement.") |
paul@0 | 1105 | |
paul@0 | 1106 | for name, alias in node.names: |
paul@2 | 1107 | if alias is not None: |
paul@24 | 1108 | self.store(alias, self.importer.load(name, 1) or UnresolvedName(None, name, self)) |
paul@2 | 1109 | else: |
paul@24 | 1110 | self.store(name.split(".")[0], self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self)) |
paul@0 | 1111 | |
paul@0 | 1112 | return None |
paul@0 | 1113 | |
paul@4 | 1114 | visitInvert = NOP |
paul@4 | 1115 | |
paul@59 | 1116 | def visitKeyword(self, node): |
paul@59 | 1117 | self.dispatch(node.expr) |
paul@59 | 1118 | const = Const(node.name) |
paul@59 | 1119 | self.constant_values[node.name] = const |
paul@59 | 1120 | return None |
paul@4 | 1121 | |
paul@4 | 1122 | visitLambda = NOP |
paul@4 | 1123 | |
paul@1 | 1124 | visitLeftShift = NOP |
paul@1 | 1125 | |
paul@0 | 1126 | visitList = NOP |
paul@0 | 1127 | |
paul@4 | 1128 | visitListComp = NOP |
paul@4 | 1129 | |
paul@4 | 1130 | visitListCompFor = NOP |
paul@4 | 1131 | |
paul@4 | 1132 | visitListCompIf = NOP |
paul@4 | 1133 | |
paul@4 | 1134 | visitMod = NOP |
paul@4 | 1135 | |
paul@0 | 1136 | def visitModule(self, node): |
paul@0 | 1137 | return self.dispatch(node.node) |
paul@0 | 1138 | |
paul@2 | 1139 | visitMul = NOP |
paul@2 | 1140 | |
paul@0 | 1141 | def visitName(self, node): |
paul@0 | 1142 | name = node.name |
paul@36 | 1143 | if self.namespaces and self.namespaces[-1].has_key(name): |
paul@35 | 1144 | return self.namespaces[-1][name] |
paul@13 | 1145 | elif self.has_key(name): |
paul@12 | 1146 | return self[name] |
paul@61 | 1147 | elif self.builtins is not None and self.builtins.has_key(name): |
paul@61 | 1148 | return self.builtins[name] |
paul@0 | 1149 | else: |
paul@0 | 1150 | return None |
paul@0 | 1151 | |
paul@0 | 1152 | visitNot = NOP |
paul@0 | 1153 | |
paul@0 | 1154 | visitOr = NOP |
paul@0 | 1155 | |
paul@0 | 1156 | visitPass = NOP |
paul@0 | 1157 | |
paul@4 | 1158 | visitPower = NOP |
paul@4 | 1159 | |
paul@4 | 1160 | visitPrint = NOP |
paul@4 | 1161 | |
paul@4 | 1162 | visitPrintnl = NOP |
paul@4 | 1163 | |
paul@0 | 1164 | visitRaise = NOP |
paul@0 | 1165 | |
paul@4 | 1166 | visitReturn = NOP |
paul@4 | 1167 | |
paul@1 | 1168 | visitRightShift = NOP |
paul@1 | 1169 | |
paul@4 | 1170 | visitSlice = NOP |
paul@4 | 1171 | |
paul@23 | 1172 | visitSliceobj = NOP |
paul@23 | 1173 | |
paul@0 | 1174 | def visitStmt(self, node): |
paul@0 | 1175 | for n in node.nodes: |
paul@0 | 1176 | self.dispatch(n) |
paul@0 | 1177 | return None |
paul@0 | 1178 | |
paul@4 | 1179 | visitSub = NOP |
paul@4 | 1180 | |
paul@0 | 1181 | visitSubscript = NOP |
paul@0 | 1182 | |
paul@0 | 1183 | def visitTryExcept(self, node): |
paul@0 | 1184 | self.dispatch(node.body) |
paul@0 | 1185 | for name, var, n in node.handlers: |
paul@0 | 1186 | self.dispatch(n) |
paul@0 | 1187 | if node.else_ is not None: |
paul@0 | 1188 | self.dispatch(node.else_) |
paul@0 | 1189 | return None |
paul@0 | 1190 | |
paul@4 | 1191 | visitTryFinally = NOP |
paul@4 | 1192 | |
paul@1 | 1193 | visitTuple = NOP |
paul@1 | 1194 | |
paul@4 | 1195 | visitUnaryAdd = NOP |
paul@4 | 1196 | |
paul@4 | 1197 | visitUnarySub = NOP |
paul@4 | 1198 | |
paul@34 | 1199 | def visitWhile(self, node): |
paul@34 | 1200 | self.in_loop = 1 |
paul@34 | 1201 | self.NOP(node) |
paul@34 | 1202 | self.in_loop = 0 |
paul@4 | 1203 | |
paul@4 | 1204 | visitWith = NOP |
paul@4 | 1205 | |
paul@4 | 1206 | visitYield = NOP |
paul@4 | 1207 | |
paul@16 | 1208 | class Global: |
paul@16 | 1209 | |
paul@16 | 1210 | """ |
paul@16 | 1211 | A reference to an object assigned to a global from outside the module |
paul@16 | 1212 | top-level. |
paul@16 | 1213 | """ |
paul@16 | 1214 | |
paul@16 | 1215 | pass |
paul@16 | 1216 | |
paul@0 | 1217 | # vim: tabstop=4 expandtab shiftwidth=4 |