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@36 | 111 | self.attr_position = 0 |
paul@23 | 112 | |
paul@3 | 113 | def __getitem__(self, name): |
paul@3 | 114 | return self.namespace[name] |
paul@3 | 115 | |
paul@15 | 116 | def get(self, name, default=None): |
paul@15 | 117 | return self.namespace.get(name, default) |
paul@15 | 118 | |
paul@12 | 119 | def __setitem__(self, name, value): |
paul@37 | 120 | self.set(name, value) |
paul@31 | 121 | |
paul@34 | 122 | def set(self, name, value, single_assignment=1): |
paul@37 | 123 | if name in self.globals: |
paul@37 | 124 | self.global_namespace.set(name, value, 0) |
paul@37 | 125 | else: |
paul@37 | 126 | attr = self._set(name, value) |
paul@12 | 127 | |
paul@37 | 128 | # NOTE: Insist on assignments with known values. |
paul@23 | 129 | |
paul@37 | 130 | if value is not None: |
paul@37 | 131 | attr.update(value, single_assignment) |
paul@32 | 132 | |
paul@32 | 133 | def set_module(self, name, value): |
paul@36 | 134 | attr = self._set(name, value) |
paul@36 | 135 | if attr.assignments is None: |
paul@36 | 136 | attr.assignments = 1 |
paul@36 | 137 | attr.assignment_values.add(value) |
paul@36 | 138 | |
paul@36 | 139 | def _set(self, name, value): |
paul@36 | 140 | if not self.namespace.has_key(name): |
paul@36 | 141 | self.namespace[name] = Attr(self.attr_position, self, name, value) |
paul@36 | 142 | self.attr_position += 1 |
paul@36 | 143 | return self.namespace[name] |
paul@23 | 144 | |
paul@12 | 145 | def __delitem__(self, name): |
paul@12 | 146 | del self.namespace[name] |
paul@12 | 147 | |
paul@13 | 148 | def has_key(self, name): |
paul@13 | 149 | return self.namespace.has_key(name) |
paul@13 | 150 | |
paul@3 | 151 | def keys(self): |
paul@3 | 152 | return self.namespace.keys() |
paul@3 | 153 | |
paul@7 | 154 | def values(self): |
paul@7 | 155 | return self.namespace.values() |
paul@7 | 156 | |
paul@7 | 157 | def items(self): |
paul@7 | 158 | return self.namespace.items() |
paul@7 | 159 | |
paul@12 | 160 | def make_global(self, name): |
paul@12 | 161 | if not self.namespace.has_key(name): |
paul@12 | 162 | self.globals.add(name) |
paul@12 | 163 | else: |
paul@40 | 164 | raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@12 | 165 | |
paul@32 | 166 | def get_assignments(self, name): |
paul@32 | 167 | if self.assignments.has_key(name): |
paul@32 | 168 | return max(self.assignments[name], len(self.assignment_values[name])) |
paul@32 | 169 | else: |
paul@32 | 170 | return None |
paul@32 | 171 | |
paul@37 | 172 | def to_list(self, d): |
paul@37 | 173 | l = [None] * len(d.keys()) |
paul@37 | 174 | for attr in d.values(): |
paul@37 | 175 | l[attr.position] = attr |
paul@37 | 176 | return l |
paul@37 | 177 | |
paul@4 | 178 | class Naming: |
paul@4 | 179 | |
paul@4 | 180 | "A mix-in providing naming conveniences." |
paul@4 | 181 | |
paul@4 | 182 | def full_name(self): |
paul@4 | 183 | if self.name is not None: |
paul@7 | 184 | return self.parent_name + "." + self.name |
paul@4 | 185 | else: |
paul@7 | 186 | return self.parent_name |
paul@4 | 187 | |
paul@12 | 188 | # Program data structures. |
paul@12 | 189 | |
paul@10 | 190 | class Attr: |
paul@10 | 191 | |
paul@10 | 192 | "An attribute entry." |
paul@10 | 193 | |
paul@36 | 194 | def __init__(self, position, parent, name, value=None, assignments=None): |
paul@10 | 195 | self.position = position |
paul@10 | 196 | self.parent = parent |
paul@36 | 197 | self.name = name |
paul@10 | 198 | self.value = value |
paul@10 | 199 | |
paul@36 | 200 | # Number of assignments per name. |
paul@36 | 201 | |
paul@36 | 202 | self.assignments = assignments |
paul@36 | 203 | self.assignment_values = set() |
paul@36 | 204 | |
paul@36 | 205 | def update(self, value, single_assignment): |
paul@36 | 206 | if self.assignments is None: |
paul@36 | 207 | if single_assignment: |
paul@36 | 208 | self.assignments = 1 |
paul@36 | 209 | else: |
paul@36 | 210 | self.assignments = AtLeast(1) |
paul@36 | 211 | else: |
paul@36 | 212 | if single_assignment: |
paul@36 | 213 | self.assignments += 1 |
paul@36 | 214 | else: |
paul@36 | 215 | self.assignments += AtLeast(1) |
paul@36 | 216 | self.assignment_values.add(value) |
paul@36 | 217 | |
paul@10 | 218 | def __repr__(self): |
paul@36 | 219 | return "Attr(%r, %r, %r, %r, %r)" % (self.position, self.parent, self.name, self.value, self.assignments) |
paul@10 | 220 | |
paul@21 | 221 | class Const: |
paul@21 | 222 | |
paul@21 | 223 | "A constant object." |
paul@21 | 224 | |
paul@21 | 225 | def __init__(self, value): |
paul@21 | 226 | self.value = value |
paul@21 | 227 | |
paul@21 | 228 | # Image generation details. |
paul@21 | 229 | |
paul@21 | 230 | self.location = None |
paul@21 | 231 | |
paul@21 | 232 | def __repr__(self): |
paul@37 | 233 | if self.location is not None: |
paul@37 | 234 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@37 | 235 | else: |
paul@37 | 236 | return "Const(%r)" % self.value |
paul@21 | 237 | |
paul@21 | 238 | def __eq__(self, other): |
paul@21 | 239 | return self.value == other.value |
paul@21 | 240 | |
paul@21 | 241 | def __hash__(self): |
paul@21 | 242 | return hash(self.value) |
paul@21 | 243 | |
paul@4 | 244 | class Class(NamespaceDict, Naming): |
paul@0 | 245 | |
paul@0 | 246 | "An inspected class." |
paul@0 | 247 | |
paul@24 | 248 | def __init__(self, name, parent_name, global_namespace=None, node=None): |
paul@24 | 249 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 250 | self.name = name |
paul@7 | 251 | self.parent_name = parent_name |
paul@17 | 252 | self.node = node |
paul@12 | 253 | |
paul@0 | 254 | self.bases = [] |
paul@9 | 255 | self.instattr = set() # instance attributes |
paul@12 | 256 | |
paul@12 | 257 | self.all_instattr = None # cache for instance_attributes |
paul@13 | 258 | self.all_instattr_names = None # from all_instattr |
paul@12 | 259 | self.all_classattr = None # cache for all_class_attributes |
paul@12 | 260 | self.all_classattr_names = None # from all_classattr |
paul@9 | 261 | self.allattr = None # cache for all_attributes |
paul@10 | 262 | self.allattr_names = None # from allattr |
paul@9 | 263 | |
paul@9 | 264 | # Image generation details. |
paul@9 | 265 | |
paul@9 | 266 | self.location = None |
paul@19 | 267 | self.code_location = None |
paul@0 | 268 | |
paul@12 | 269 | def __repr__(self): |
paul@37 | 270 | if self.location is not None: |
paul@37 | 271 | return "Class(%r, %r, location=%r)" % (self.name, self.parent_name, self.location) |
paul@37 | 272 | else: |
paul@37 | 273 | return "Class(%r, %r)" % (self.name, self.parent_name) |
paul@12 | 274 | |
paul@0 | 275 | def add_base(self, base): |
paul@0 | 276 | self.bases.append(base) |
paul@0 | 277 | |
paul@12 | 278 | def add_instance_attribute(self, name): |
paul@12 | 279 | self.instattr.add(name) |
paul@9 | 280 | |
paul@36 | 281 | "Return the attribute names provided by this class only." |
paul@9 | 282 | |
paul@36 | 283 | class_attribute_names = NamespaceDict.keys |
paul@4 | 284 | |
paul@4 | 285 | def class_attributes(self): |
paul@4 | 286 | |
paul@12 | 287 | "Return class attributes provided by this class only." |
paul@4 | 288 | |
paul@36 | 289 | return self |
paul@12 | 290 | |
paul@12 | 291 | def all_class_attribute_names(self): |
paul@12 | 292 | |
paul@12 | 293 | "Return the attribute names provided by classes in this hierarchy." |
paul@12 | 294 | |
paul@12 | 295 | if self.all_classattr_names is None: |
paul@12 | 296 | self.all_class_attributes() |
paul@12 | 297 | return self.all_classattr_names |
paul@12 | 298 | |
paul@12 | 299 | def all_class_attributes(self): |
paul@12 | 300 | |
paul@12 | 301 | "Return all class attributes, indicating the class which provides them." |
paul@12 | 302 | |
paul@12 | 303 | if self.all_classattr is None: |
paul@12 | 304 | self.all_classattr = {} |
paul@12 | 305 | |
paul@9 | 306 | reversed_bases = self.bases[:] |
paul@9 | 307 | reversed_bases.reverse() |
paul@9 | 308 | for cls in reversed_bases: |
paul@12 | 309 | self.all_classattr.update(cls.all_class_attributes()) |
paul@10 | 310 | |
paul@10 | 311 | # Record attributes provided by this class, along with their |
paul@10 | 312 | # positions. |
paul@10 | 313 | |
paul@12 | 314 | self.all_classattr.update(self.class_attributes()) |
paul@9 | 315 | |
paul@12 | 316 | return self.all_classattr |
paul@4 | 317 | |
paul@7 | 318 | def instance_attribute_names(self): |
paul@7 | 319 | |
paul@7 | 320 | "Return the instance attribute names provided by the class." |
paul@7 | 321 | |
paul@12 | 322 | if self.all_instattr_names is None: |
paul@12 | 323 | self.instance_attributes() |
paul@12 | 324 | return self.all_instattr_names |
paul@7 | 325 | |
paul@4 | 326 | def instance_attributes(self): |
paul@4 | 327 | |
paul@9 | 328 | "Return instance-only attributes for instances of this class." |
paul@9 | 329 | |
paul@12 | 330 | if self.all_instattr is None: |
paul@12 | 331 | self.all_instattr = {} |
paul@12 | 332 | instattr = set() |
paul@12 | 333 | |
paul@12 | 334 | reversed_bases = self.bases[:] |
paul@12 | 335 | reversed_bases.reverse() |
paul@12 | 336 | for cls in reversed_bases: |
paul@12 | 337 | instattr.update(cls.instance_attributes().keys()) |
paul@12 | 338 | |
paul@12 | 339 | # Record instance attributes provided by this class and its bases, |
paul@12 | 340 | # along with their positions. |
paul@12 | 341 | |
paul@12 | 342 | instattr.update(self.instattr) |
paul@12 | 343 | |
paul@12 | 344 | for i, name in enumerate(instattr): |
paul@36 | 345 | self.all_instattr[name] = Attr(i, None, name) |
paul@12 | 346 | |
paul@12 | 347 | self.all_instattr_names = self.all_instattr.keys() |
paul@12 | 348 | |
paul@12 | 349 | return self.all_instattr |
paul@9 | 350 | |
paul@9 | 351 | def all_attribute_names(self): |
paul@9 | 352 | |
paul@9 | 353 | """ |
paul@9 | 354 | Return the names of all attributes provided by instances of this class. |
paul@9 | 355 | """ |
paul@9 | 356 | |
paul@9 | 357 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@9 | 358 | return self.allattr_names |
paul@9 | 359 | |
paul@9 | 360 | def all_attributes(self): |
paul@9 | 361 | |
paul@4 | 362 | """ |
paul@4 | 363 | Return all attributes for an instance, indicating either the class which |
paul@4 | 364 | provides them or that the instance itself provides them. |
paul@4 | 365 | """ |
paul@4 | 366 | |
paul@9 | 367 | if self.allattr is None: |
paul@9 | 368 | self.allattr = {} |
paul@12 | 369 | self.allattr.update(self.all_class_attributes()) |
paul@9 | 370 | for i, name in enumerate(self.instance_attribute_names()): |
paul@9 | 371 | if self.allattr.has_key(name): |
paul@9 | 372 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@36 | 373 | self.allattr[name] = Attr(i, None, name) |
paul@9 | 374 | return self.allattr |
paul@4 | 375 | |
paul@12 | 376 | class Function(NamespaceDict, Naming): |
paul@0 | 377 | |
paul@0 | 378 | "An inspected function." |
paul@0 | 379 | |
paul@24 | 380 | def __init__(self, name, parent_name, argnames, has_star, has_dstar, global_namespace=None, node=None): |
paul@24 | 381 | NamespaceDict.__init__(self, global_namespace) |
paul@3 | 382 | self.name = name |
paul@7 | 383 | self.parent_name = parent_name |
paul@3 | 384 | self.argnames = argnames |
paul@3 | 385 | self.has_star = has_star |
paul@3 | 386 | self.has_dstar = has_dstar |
paul@17 | 387 | self.node = node |
paul@3 | 388 | |
paul@36 | 389 | # Caches. |
paul@36 | 390 | |
paul@12 | 391 | self.localnames = None # cache for locals |
paul@12 | 392 | |
paul@17 | 393 | # Add parameters to the namespace. |
paul@17 | 394 | |
paul@25 | 395 | self._add_parameters(argnames) |
paul@17 | 396 | |
paul@9 | 397 | # Image generation details. |
paul@9 | 398 | |
paul@9 | 399 | self.location = None |
paul@19 | 400 | self.code_location = None |
paul@9 | 401 | |
paul@25 | 402 | def _add_parameters(self, argnames): |
paul@25 | 403 | for name in argnames: |
paul@25 | 404 | if isinstance(name, tuple): |
paul@25 | 405 | self._add_parameters(name) |
paul@25 | 406 | else: |
paul@25 | 407 | self[name] = None |
paul@25 | 408 | |
paul@4 | 409 | def __repr__(self): |
paul@37 | 410 | if self.location is not None: |
paul@37 | 411 | return "Function(%r, %r, %r, %r, %r, location=%r)" % ( |
paul@37 | 412 | self.name, self.parent_name, self.argnames, self.has_star, self.has_dstar, self.location |
paul@37 | 413 | ) |
paul@37 | 414 | else: |
paul@37 | 415 | return "Function(%r, %r, %r, %r, %r)" % ( |
paul@37 | 416 | self.name, self.parent_name, self.argnames, self.has_star, self.has_dstar |
paul@37 | 417 | ) |
paul@4 | 418 | |
paul@12 | 419 | def make_global(self, name): |
paul@13 | 420 | if name not in self.argnames and not self.has_key(name): |
paul@12 | 421 | self.globals.add(name) |
paul@12 | 422 | else: |
paul@40 | 423 | raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@12 | 424 | |
paul@7 | 425 | def parameters(self): |
paul@7 | 426 | |
paul@7 | 427 | """ |
paul@7 | 428 | Return a dictionary mapping parameter names to their position in the |
paul@7 | 429 | parameter list. |
paul@7 | 430 | """ |
paul@7 | 431 | |
paul@7 | 432 | parameters = {} |
paul@7 | 433 | for i, name in enumerate(self.argnames): |
paul@7 | 434 | parameters[name] = i |
paul@7 | 435 | return parameters |
paul@7 | 436 | |
paul@36 | 437 | def all_locals(self): |
paul@36 | 438 | |
paul@36 | 439 | "Return a dictionary mapping names to local and parameter details." |
paul@36 | 440 | |
paul@36 | 441 | return self |
paul@36 | 442 | |
paul@12 | 443 | def locals(self): |
paul@12 | 444 | |
paul@12 | 445 | "Return a dictionary mapping names to local details." |
paul@12 | 446 | |
paul@12 | 447 | if self.localnames is None: |
paul@12 | 448 | self.localnames = {} |
paul@36 | 449 | self.localnames.update(self.all_locals()) |
paul@36 | 450 | for name in self.argnames: |
paul@36 | 451 | del self.localnames[name] |
paul@12 | 452 | return self.localnames |
paul@12 | 453 | |
paul@4 | 454 | class UnresolvedName(NamespaceDict, Naming): |
paul@3 | 455 | |
paul@3 | 456 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 457 | |
paul@24 | 458 | def __init__(self, name, parent_name, global_namespace=None): |
paul@24 | 459 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 460 | self.name = name |
paul@7 | 461 | self.parent_name = parent_name |
paul@0 | 462 | |
paul@12 | 463 | def all_class_attributes(self): |
paul@7 | 464 | return {} |
paul@7 | 465 | |
paul@13 | 466 | def instance_attributes(self): |
paul@13 | 467 | return {} |
paul@13 | 468 | |
paul@4 | 469 | def __repr__(self): |
paul@7 | 470 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@4 | 471 | |
paul@15 | 472 | class Module(NamespaceDict): |
paul@0 | 473 | |
paul@20 | 474 | "An inspected module's core details." |
paul@20 | 475 | |
paul@15 | 476 | def __init__(self, name): |
paul@24 | 477 | NamespaceDict.__init__(self, self) |
paul@1 | 478 | self.name = name |
paul@0 | 479 | |
paul@7 | 480 | # Complete lists of classes and functions. |
paul@7 | 481 | |
paul@33 | 482 | self.all_objects = set() |
paul@0 | 483 | |
paul@22 | 484 | # Constant records. |
paul@22 | 485 | |
paul@22 | 486 | self.constant_values = {} |
paul@22 | 487 | self.constant_list = None # cache for constants |
paul@22 | 488 | |
paul@15 | 489 | # Image generation details. |
paul@15 | 490 | |
paul@15 | 491 | self.location = None |
paul@19 | 492 | self.code_location = None |
paul@15 | 493 | |
paul@20 | 494 | # Original location details. |
paul@20 | 495 | |
paul@20 | 496 | self.node = None |
paul@20 | 497 | |
paul@15 | 498 | def full_name(self): |
paul@15 | 499 | return self.name |
paul@15 | 500 | |
paul@15 | 501 | def __repr__(self): |
paul@37 | 502 | if self.location is not None: |
paul@37 | 503 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@37 | 504 | else: |
paul@37 | 505 | return "Module(%r)" % self.name |
paul@15 | 506 | |
paul@15 | 507 | # Attribute methods. |
paul@15 | 508 | |
paul@36 | 509 | "Return the module attribute names provided by the module." |
paul@15 | 510 | |
paul@36 | 511 | module_attribute_names = NamespaceDict.keys |
paul@15 | 512 | |
paul@15 | 513 | def module_attributes(self): |
paul@15 | 514 | |
paul@15 | 515 | "Return a dictionary mapping names to module attributes." |
paul@15 | 516 | |
paul@36 | 517 | return self |
paul@15 | 518 | |
paul@22 | 519 | def constants(self): |
paul@22 | 520 | |
paul@22 | 521 | "Return a list of constants." |
paul@22 | 522 | |
paul@22 | 523 | if self.constant_list is None: |
paul@22 | 524 | self.constant_list = list(self.constant_values.values()) |
paul@22 | 525 | |
paul@22 | 526 | return self.constant_list |
paul@22 | 527 | |
paul@21 | 528 | # Program visitors. |
paul@21 | 529 | |
paul@15 | 530 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 531 | |
paul@20 | 532 | """ |
paul@20 | 533 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 534 | capable of being used as an AST visitor. |
paul@20 | 535 | """ |
paul@15 | 536 | |
paul@15 | 537 | def __init__(self, name, importer=None): |
paul@15 | 538 | ASTVisitor.__init__(self) |
paul@15 | 539 | Module.__init__(self, name) |
paul@15 | 540 | self.visitor = self |
paul@15 | 541 | |
paul@15 | 542 | self.importer = importer |
paul@15 | 543 | self.loaded = 0 |
paul@15 | 544 | |
paul@1 | 545 | # Current expression state. |
paul@1 | 546 | |
paul@1 | 547 | self.expr = None |
paul@1 | 548 | |
paul@1 | 549 | # Namespace state. |
paul@1 | 550 | |
paul@34 | 551 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@34 | 552 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 553 | self.namespaces = [] |
paul@0 | 554 | self.module = None |
paul@0 | 555 | |
paul@0 | 556 | def parse(self, filename): |
paul@9 | 557 | |
paul@9 | 558 | "Parse the file having the given 'filename'." |
paul@9 | 559 | |
paul@0 | 560 | module = compiler.parseFile(filename) |
paul@0 | 561 | self.process(module) |
paul@0 | 562 | |
paul@0 | 563 | def process(self, module): |
paul@9 | 564 | |
paul@9 | 565 | "Process the given 'module'." |
paul@9 | 566 | |
paul@20 | 567 | self.node = self.module = module |
paul@1 | 568 | processed = self.dispatch(module) |
paul@13 | 569 | if self.has_key("__all__"): |
paul@12 | 570 | all = self["__all__"] |
paul@1 | 571 | if isinstance(all, compiler.ast.List): |
paul@1 | 572 | for n in all.nodes: |
paul@12 | 573 | self[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 574 | return processed |
paul@0 | 575 | |
paul@3 | 576 | def vacuum(self): |
paul@9 | 577 | |
paul@9 | 578 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 579 | |
paul@13 | 580 | for name, value in self.items(): |
paul@3 | 581 | if isinstance(value, Module) and not value.loaded: |
paul@12 | 582 | del self[name] |
paul@0 | 583 | |
paul@16 | 584 | # Complain about globals not initialised at the module level. |
paul@16 | 585 | |
paul@16 | 586 | if isinstance(value, Global): |
paul@16 | 587 | print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) |
paul@16 | 588 | |
paul@3 | 589 | # Namespace methods. |
paul@0 | 590 | |
paul@0 | 591 | def store(self, name, obj): |
paul@12 | 592 | |
paul@12 | 593 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 594 | |
paul@12 | 595 | if not self.namespaces: |
paul@34 | 596 | self.set(name, obj, not self.in_loop) |
paul@12 | 597 | else: |
paul@34 | 598 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 599 | |
paul@12 | 600 | # Record all non-local objects. |
paul@12 | 601 | |
paul@12 | 602 | if not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@33 | 603 | self.all_objects.add(obj) |
paul@0 | 604 | |
paul@34 | 605 | def store_instance_attr(self, name): |
paul@12 | 606 | |
paul@12 | 607 | "Record instance attribute 'name' in the current class." |
paul@12 | 608 | |
paul@0 | 609 | if self.in_init: |
paul@15 | 610 | |
paul@12 | 611 | # Current namespace is the function. |
paul@12 | 612 | # Previous namespace is the class. |
paul@15 | 613 | |
paul@12 | 614 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 615 | |
paul@7 | 616 | def get_parent(self): |
paul@12 | 617 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 618 | |
paul@3 | 619 | # Visitor methods. |
paul@3 | 620 | |
paul@3 | 621 | def default(self, node, *args): |
paul@39 | 622 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 623 | |
paul@3 | 624 | def dispatch(self, node, *args): |
paul@3 | 625 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 626 | |
paul@0 | 627 | def NOP(self, node): |
paul@21 | 628 | for n in node.getChildNodes(): |
paul@21 | 629 | self.dispatch(n) |
paul@19 | 630 | return None |
paul@0 | 631 | |
paul@1 | 632 | visitAdd = NOP |
paul@1 | 633 | |
paul@0 | 634 | visitAnd = NOP |
paul@0 | 635 | |
paul@4 | 636 | visitAssert = NOP |
paul@4 | 637 | |
paul@0 | 638 | def visitAssign(self, node): |
paul@0 | 639 | self.expr = self.dispatch(node.expr) |
paul@0 | 640 | for n in node.nodes: |
paul@0 | 641 | self.dispatch(n) |
paul@0 | 642 | return None |
paul@0 | 643 | |
paul@0 | 644 | def visitAssAttr(self, node): |
paul@0 | 645 | expr = self.dispatch(node.expr) |
paul@36 | 646 | if isinstance(expr, Attr) and expr.name == "self": |
paul@34 | 647 | self.store_instance_attr(node.attrname) |
paul@0 | 648 | return None |
paul@0 | 649 | |
paul@0 | 650 | def visitAssList(self, node): |
paul@0 | 651 | for n in node.nodes: |
paul@0 | 652 | self.dispatch(n) |
paul@0 | 653 | return None |
paul@0 | 654 | |
paul@0 | 655 | def visitAssName(self, node): |
paul@36 | 656 | if isinstance(self.expr, Attr): |
paul@36 | 657 | self.store(node.name, self.expr.value) |
paul@36 | 658 | else: |
paul@36 | 659 | self.store(node.name, self.expr) |
paul@0 | 660 | return None |
paul@0 | 661 | |
paul@0 | 662 | visitAssTuple = visitAssList |
paul@0 | 663 | |
paul@2 | 664 | visitAugAssign = NOP |
paul@2 | 665 | |
paul@4 | 666 | visitBackquote = NOP |
paul@4 | 667 | |
paul@2 | 668 | visitBitand = NOP |
paul@2 | 669 | |
paul@2 | 670 | visitBitor = NOP |
paul@2 | 671 | |
paul@4 | 672 | visitBitxor = NOP |
paul@4 | 673 | |
paul@4 | 674 | visitBreak = NOP |
paul@4 | 675 | |
paul@0 | 676 | visitCallFunc = NOP |
paul@0 | 677 | |
paul@0 | 678 | def visitClass(self, node): |
paul@12 | 679 | if self.namespaces: |
paul@41 | 680 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@0 | 681 | else: |
paul@24 | 682 | cls = Class(node.name, self.get_parent().full_name(), self, node) |
paul@37 | 683 | |
paul@37 | 684 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 685 | # definitions of classes. |
paul@37 | 686 | |
paul@0 | 687 | for base in node.bases: |
paul@36 | 688 | expr = self.dispatch(base) |
paul@36 | 689 | if isinstance(expr, Attr): |
paul@36 | 690 | if expr.assignments != 1: |
paul@39 | 691 | raise InspectError(self.full_name(), node, |
paul@40 | 692 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 693 | else: |
paul@36 | 694 | cls.add_base(expr.value) |
paul@36 | 695 | else: # if expr is None: |
paul@39 | 696 | raise InspectError(self.full_name(), node, |
paul@40 | 697 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 698 | |
paul@21 | 699 | # Make a back reference from the node for code generation. |
paul@21 | 700 | |
paul@39 | 701 | node.unit = cls |
paul@21 | 702 | |
paul@0 | 703 | # Make an entry for the class. |
paul@0 | 704 | |
paul@0 | 705 | self.store(node.name, cls) |
paul@0 | 706 | |
paul@12 | 707 | self.namespaces.append(cls) |
paul@0 | 708 | self.dispatch(node.code) |
paul@12 | 709 | self.namespaces.pop() |
paul@0 | 710 | |
paul@19 | 711 | return None |
paul@0 | 712 | |
paul@4 | 713 | visitCompare = NOP |
paul@4 | 714 | |
paul@21 | 715 | def visitConst(self, node): |
paul@21 | 716 | const = Const(node.value) |
paul@22 | 717 | self.constant_values[node.value] = const |
paul@24 | 718 | return const |
paul@0 | 719 | |
paul@4 | 720 | visitContinue = NOP |
paul@4 | 721 | |
paul@4 | 722 | visitDecorators = NOP |
paul@4 | 723 | |
paul@0 | 724 | visitDict = NOP |
paul@0 | 725 | |
paul@0 | 726 | visitDiscard = NOP |
paul@0 | 727 | |
paul@4 | 728 | visitDiv = NOP |
paul@4 | 729 | |
paul@4 | 730 | visitEllipsis = NOP |
paul@4 | 731 | |
paul@4 | 732 | visitExec = NOP |
paul@4 | 733 | |
paul@4 | 734 | visitExpression = NOP |
paul@4 | 735 | |
paul@4 | 736 | visitFloorDiv = NOP |
paul@4 | 737 | |
paul@34 | 738 | def visitFor(self, node): |
paul@34 | 739 | self.in_loop = 1 |
paul@34 | 740 | self.NOP(node) |
paul@34 | 741 | self.in_loop = 0 |
paul@0 | 742 | |
paul@0 | 743 | def visitFrom(self, node): |
paul@0 | 744 | if self.importer is None: |
paul@39 | 745 | raise InspectError(self.full_name(), node, |
paul@39 | 746 | "Please use the micropython.Importer class for code which uses the 'from' statement.") |
paul@0 | 747 | |
paul@0 | 748 | module = self.importer.load(node.modname, 1) |
paul@0 | 749 | |
paul@35 | 750 | #if module is None: |
paul@35 | 751 | # print "Warning:", node.modname, "not imported." |
paul@0 | 752 | |
paul@0 | 753 | for name, alias in node.names: |
paul@0 | 754 | if name != "*": |
paul@4 | 755 | if module is not None and module.namespace.has_key(name): |
paul@24 | 756 | attr = module[name] |
paul@36 | 757 | self.store(alias or name, attr.value) |
paul@2 | 758 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 759 | self.importer.load(attr.name) |
paul@24 | 760 | |
paul@24 | 761 | # Support the import of names from missing modules. |
paul@24 | 762 | |
paul@3 | 763 | else: |
paul@24 | 764 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 765 | else: |
paul@3 | 766 | if module is not None: |
paul@3 | 767 | for n in module.namespace.keys(): |
paul@24 | 768 | attr = module[n] |
paul@36 | 769 | self.store(n, attr.value) |
paul@3 | 770 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 771 | self.importer.load(attr.name) |
paul@0 | 772 | |
paul@0 | 773 | return None |
paul@0 | 774 | |
paul@0 | 775 | def visitFunction(self, node): |
paul@3 | 776 | function = Function( |
paul@3 | 777 | node.name, |
paul@7 | 778 | self.get_parent().full_name(), |
paul@3 | 779 | node.argnames, |
paul@17 | 780 | (node.flags & 4 != 0), |
paul@17 | 781 | (node.flags & 8 != 0), |
paul@24 | 782 | self, |
paul@17 | 783 | node |
paul@3 | 784 | ) |
paul@0 | 785 | |
paul@21 | 786 | # Make a back reference from the node for code generation. |
paul@21 | 787 | |
paul@39 | 788 | node.unit = function |
paul@21 | 789 | |
paul@12 | 790 | self.namespaces.append(function) |
paul@15 | 791 | |
paul@12 | 792 | # Current namespace is the function. |
paul@12 | 793 | # Previous namespace is the class. |
paul@15 | 794 | |
paul@12 | 795 | if node.name == "__init__" and isinstance(self.namespaces[-2], Class): |
paul@0 | 796 | self.in_init = 1 |
paul@15 | 797 | |
paul@12 | 798 | self.dispatch(node.code) |
paul@12 | 799 | self.in_init = 0 |
paul@12 | 800 | self.namespaces.pop() |
paul@3 | 801 | |
paul@3 | 802 | self.store(node.name, function) |
paul@0 | 803 | return None |
paul@0 | 804 | |
paul@4 | 805 | visitGenExpr = NOP |
paul@4 | 806 | |
paul@4 | 807 | visitGenExprFor = NOP |
paul@4 | 808 | |
paul@4 | 809 | visitGenExprIf = NOP |
paul@4 | 810 | |
paul@4 | 811 | visitGenExprInner = NOP |
paul@4 | 812 | |
paul@0 | 813 | def visitGetattr(self, node): |
paul@0 | 814 | expr = self.dispatch(node.expr) |
paul@36 | 815 | if isinstance(expr, Attr): |
paul@36 | 816 | value = expr.value |
paul@36 | 817 | if isinstance(value, Module): |
paul@36 | 818 | return value.namespace.get(node.attrname) |
paul@36 | 819 | elif isinstance(value, UnresolvedName): |
paul@36 | 820 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@15 | 821 | return builtins.get(node.attrname) |
paul@4 | 822 | |
paul@12 | 823 | def visitGlobal(self, node): |
paul@12 | 824 | if self.namespaces: |
paul@12 | 825 | for name in node.names: |
paul@12 | 826 | self.namespaces[-1].make_global(name) |
paul@37 | 827 | |
paul@37 | 828 | # Record a global entry for the name in the module. |
paul@37 | 829 | |
paul@15 | 830 | if not self.has_key(name): |
paul@16 | 831 | self[name] = Global() |
paul@0 | 832 | |
paul@0 | 833 | def visitIf(self, node): |
paul@0 | 834 | for test, body in node.tests: |
paul@0 | 835 | self.dispatch(body) |
paul@0 | 836 | if node.else_ is not None: |
paul@0 | 837 | self.dispatch(node.else_) |
paul@0 | 838 | return None |
paul@0 | 839 | |
paul@12 | 840 | visitIfExp = NOP |
paul@12 | 841 | |
paul@0 | 842 | def visitImport(self, node): |
paul@0 | 843 | if self.importer is None: |
paul@39 | 844 | raise InspectError(self.full_name(), node, |
paul@39 | 845 | "Please use the micropython.Importer class for code which uses the 'import' statement.") |
paul@0 | 846 | |
paul@0 | 847 | for name, alias in node.names: |
paul@2 | 848 | if alias is not None: |
paul@24 | 849 | self.store(alias, self.importer.load(name, 1) or UnresolvedName(None, name, self)) |
paul@2 | 850 | else: |
paul@24 | 851 | self.store(name.split(".")[0], self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self)) |
paul@0 | 852 | |
paul@0 | 853 | return None |
paul@0 | 854 | |
paul@4 | 855 | visitInvert = NOP |
paul@4 | 856 | |
paul@4 | 857 | visitKeyword = NOP |
paul@4 | 858 | |
paul@4 | 859 | visitLambda = NOP |
paul@4 | 860 | |
paul@1 | 861 | visitLeftShift = NOP |
paul@1 | 862 | |
paul@0 | 863 | visitList = NOP |
paul@0 | 864 | |
paul@4 | 865 | visitListComp = NOP |
paul@4 | 866 | |
paul@4 | 867 | visitListCompFor = NOP |
paul@4 | 868 | |
paul@4 | 869 | visitListCompIf = NOP |
paul@4 | 870 | |
paul@4 | 871 | visitMod = NOP |
paul@4 | 872 | |
paul@0 | 873 | def visitModule(self, node): |
paul@0 | 874 | return self.dispatch(node.node) |
paul@0 | 875 | |
paul@2 | 876 | visitMul = NOP |
paul@2 | 877 | |
paul@0 | 878 | def visitName(self, node): |
paul@0 | 879 | name = node.name |
paul@36 | 880 | if self.namespaces and self.namespaces[-1].has_key(name): |
paul@35 | 881 | return self.namespaces[-1][name] |
paul@13 | 882 | elif self.has_key(name): |
paul@12 | 883 | return self[name] |
paul@15 | 884 | elif builtins.has_key(name): |
paul@15 | 885 | return builtins[name] |
paul@0 | 886 | else: |
paul@0 | 887 | return None |
paul@0 | 888 | |
paul@0 | 889 | visitNot = NOP |
paul@0 | 890 | |
paul@0 | 891 | visitOr = NOP |
paul@0 | 892 | |
paul@0 | 893 | visitPass = NOP |
paul@0 | 894 | |
paul@4 | 895 | visitPower = NOP |
paul@4 | 896 | |
paul@4 | 897 | visitPrint = NOP |
paul@4 | 898 | |
paul@4 | 899 | visitPrintnl = NOP |
paul@4 | 900 | |
paul@0 | 901 | visitRaise = NOP |
paul@0 | 902 | |
paul@4 | 903 | visitReturn = NOP |
paul@4 | 904 | |
paul@1 | 905 | visitRightShift = NOP |
paul@1 | 906 | |
paul@4 | 907 | visitSlice = NOP |
paul@4 | 908 | |
paul@23 | 909 | visitSliceobj = NOP |
paul@23 | 910 | |
paul@0 | 911 | def visitStmt(self, node): |
paul@0 | 912 | for n in node.nodes: |
paul@0 | 913 | self.dispatch(n) |
paul@0 | 914 | return None |
paul@0 | 915 | |
paul@4 | 916 | visitSub = NOP |
paul@4 | 917 | |
paul@0 | 918 | visitSubscript = NOP |
paul@0 | 919 | |
paul@0 | 920 | def visitTryExcept(self, node): |
paul@0 | 921 | self.dispatch(node.body) |
paul@0 | 922 | for name, var, n in node.handlers: |
paul@0 | 923 | self.dispatch(n) |
paul@0 | 924 | if node.else_ is not None: |
paul@0 | 925 | self.dispatch(node.else_) |
paul@0 | 926 | return None |
paul@0 | 927 | |
paul@4 | 928 | visitTryFinally = NOP |
paul@4 | 929 | |
paul@1 | 930 | visitTuple = NOP |
paul@1 | 931 | |
paul@4 | 932 | visitUnaryAdd = NOP |
paul@4 | 933 | |
paul@4 | 934 | visitUnarySub = NOP |
paul@4 | 935 | |
paul@34 | 936 | def visitWhile(self, node): |
paul@34 | 937 | self.in_loop = 1 |
paul@34 | 938 | self.NOP(node) |
paul@34 | 939 | self.in_loop = 0 |
paul@4 | 940 | |
paul@4 | 941 | visitWith = NOP |
paul@4 | 942 | |
paul@4 | 943 | visitYield = NOP |
paul@4 | 944 | |
paul@16 | 945 | class Global: |
paul@16 | 946 | |
paul@16 | 947 | """ |
paul@16 | 948 | A reference to an object assigned to a global from outside the module |
paul@16 | 949 | top-level. |
paul@16 | 950 | """ |
paul@16 | 951 | |
paul@16 | 952 | pass |
paul@16 | 953 | |
paul@4 | 954 | # Built-in types initialisation. |
paul@4 | 955 | |
paul@15 | 956 | class Builtins(Module): |
paul@15 | 957 | |
paul@15 | 958 | "The special built-in types module." |
paul@15 | 959 | |
paul@15 | 960 | def __init__(self): |
paul@15 | 961 | Module.__init__(self, "__builtins__") |
paul@15 | 962 | |
paul@15 | 963 | for key in ['ArithmeticError', 'AssertionError', 'AttributeError', |
paul@15 | 964 | 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', |
paul@15 | 965 | 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', |
paul@15 | 966 | 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
paul@15 | 967 | 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
paul@15 | 968 | 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
paul@15 | 969 | 'None', 'NotImplemented', 'NotImplementedError', 'OSError', |
paul@15 | 970 | 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', |
paul@15 | 971 | 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', |
paul@15 | 972 | 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
paul@15 | 973 | 'TabError', 'True', 'TypeError', 'UnboundLocalError', |
paul@15 | 974 | 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', |
paul@15 | 975 | 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
paul@15 | 976 | 'ValueError', 'Warning', 'ZeroDivisionError', |
paul@25 | 977 | 'basestring', 'bool', 'buffer', 'complex', 'dict', 'file', 'float', |
paul@25 | 978 | 'frozenset', 'int', 'list', 'long', 'object', 'set', 'slice', 'str', |
paul@25 | 979 | 'tuple', 'type', 'unicode', 'xrange']: |
paul@24 | 980 | self[key] = Class(key, self.full_name(), self) |
paul@15 | 981 | |
paul@25 | 982 | # NOTE: Incomplete: some functions have more than one parameter. |
paul@25 | 983 | |
paul@25 | 984 | for key in ['__import__', 'abs', 'all', 'any', 'callable', 'chr', |
paul@25 | 985 | 'classmethod', 'cmp', 'compile', 'delattr', 'dir', 'divmod', |
paul@25 | 986 | 'enumerate', 'eval', 'execfile', 'filter', 'getattr', 'globals', |
paul@25 | 987 | 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'isinstance', |
paul@25 | 988 | 'issubclass', 'iter', 'len', 'locals', 'map', 'max', 'min', 'oct', |
paul@25 | 989 | 'open', 'ord', 'pow', 'property', 'range', 'raw_input', 'reduce', |
paul@25 | 990 | 'reload', 'repr', 'reversed', 'round', 'setattr', 'sorted', |
paul@25 | 991 | 'staticmethod', 'sum', 'super', 'unichr', 'vars', 'zip']: |
paul@24 | 992 | self[key] = Function(key, self.full_name(), ['arg'], 0, 0, self) |
paul@16 | 993 | |
paul@15 | 994 | builtins = Builtins() |
paul@0 | 995 | |
paul@0 | 996 | # vim: tabstop=4 expandtab shiftwidth=4 |