paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@83 | 3 | """ |
paul@83 | 4 | Data classes. |
paul@83 | 5 | |
paul@178 | 6 | Copyright (C) 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk> |
paul@83 | 7 | |
paul@83 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@83 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@83 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@83 | 11 | version. |
paul@83 | 12 | |
paul@83 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@83 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@83 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@83 | 16 | details. |
paul@83 | 17 | |
paul@83 | 18 | You should have received a copy of the GNU General Public License along with |
paul@83 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@83 | 20 | |
paul@83 | 21 | -------- |
paul@83 | 22 | |
paul@190 | 23 | The central data structure classes in this module are the following: |
paul@83 | 24 | |
paul@83 | 25 | * Class |
paul@83 | 26 | * Function |
paul@83 | 27 | * Module |
paul@83 | 28 | |
paul@83 | 29 | All of the above support the Naming interface either explicitly or through |
paul@83 | 30 | general conformance, meaning that all can be asked to provide their 'full_name' |
paul@83 | 31 | using the method of that name. |
paul@83 | 32 | |
paul@83 | 33 | Additionally, all of the above also support a dictionary interface in order to |
paul@83 | 34 | access names within their defined scopes. Specific methods also exist in order |
paul@83 | 35 | to distinguish between certain kinds of attributes: |
paul@83 | 36 | |
paul@190 | 37 | * Class: class_attributes, all_class_attributes, instance_attributes, all_attributes |
paul@83 | 38 | * Function: parameters, locals, all_locals |
paul@83 | 39 | * Module: module_attributes |
paul@83 | 40 | |
paul@83 | 41 | These specific methods are useful in certain situations. |
paul@83 | 42 | |
paul@92 | 43 | The above classes also provide an 'astnode' attribute, indicating the AST node |
paul@92 | 44 | where each such object is defined. |
paul@83 | 45 | """ |
paul@83 | 46 | |
paul@264 | 47 | from micropython.program import DataObject, DataValue, ReplaceableContext, PlaceholderContext |
paul@83 | 48 | |
paul@87 | 49 | def shortrepr(obj): |
paul@87 | 50 | if obj is None: |
paul@87 | 51 | return repr(None) |
paul@87 | 52 | else: |
paul@87 | 53 | return obj.__shortrepr__() |
paul@87 | 54 | |
paul@201 | 55 | # Special representations. |
paul@201 | 56 | |
paul@201 | 57 | class AtLeast: |
paul@201 | 58 | |
paul@201 | 59 | "A special representation for numbers of a given value or greater." |
paul@201 | 60 | |
paul@201 | 61 | def __init__(self, count): |
paul@201 | 62 | self.count = count |
paul@201 | 63 | |
paul@201 | 64 | def __eq__(self, other): |
paul@201 | 65 | return 0 |
paul@201 | 66 | |
paul@201 | 67 | __lt__ = __le__ = __eq__ |
paul@201 | 68 | |
paul@201 | 69 | def __ne__(self, other): |
paul@201 | 70 | return 1 |
paul@201 | 71 | |
paul@201 | 72 | def __gt__(self, other): |
paul@201 | 73 | if isinstance(other, AtLeast): |
paul@201 | 74 | return 0 |
paul@201 | 75 | else: |
paul@201 | 76 | return self.count > other |
paul@201 | 77 | |
paul@201 | 78 | def __ge__(self, other): |
paul@201 | 79 | if isinstance(other, AtLeast): |
paul@201 | 80 | return 0 |
paul@201 | 81 | else: |
paul@201 | 82 | return self.count >= other |
paul@201 | 83 | |
paul@201 | 84 | def __iadd__(self, other): |
paul@201 | 85 | if isinstance(other, AtLeast): |
paul@201 | 86 | self.count += other.count |
paul@201 | 87 | else: |
paul@201 | 88 | self.count += other |
paul@201 | 89 | return self |
paul@201 | 90 | |
paul@201 | 91 | def __radd__(self, other): |
paul@201 | 92 | if isinstance(other, AtLeast): |
paul@201 | 93 | return AtLeast(self.count + other.count) |
paul@201 | 94 | else: |
paul@201 | 95 | return AtLeast(self.count + other) |
paul@201 | 96 | |
paul@201 | 97 | def __repr__(self): |
paul@201 | 98 | return "AtLeast(%r)" % self.count |
paul@201 | 99 | |
paul@83 | 100 | # Mix-ins and abstract classes. |
paul@83 | 101 | |
paul@201 | 102 | class Naming: |
paul@201 | 103 | |
paul@201 | 104 | "A mix-in providing naming conveniences." |
paul@201 | 105 | |
paul@201 | 106 | def full_name(self): |
paul@201 | 107 | if self.name is not None: |
paul@201 | 108 | return self.parent.full_name() + "." + self.name |
paul@201 | 109 | else: |
paul@201 | 110 | return self.parent.full_name() |
paul@201 | 111 | |
paul@83 | 112 | class NamespaceDict: |
paul@83 | 113 | |
paul@83 | 114 | "A mix-in providing dictionary methods." |
paul@83 | 115 | |
paul@180 | 116 | def __init__(self, module=None): |
paul@83 | 117 | self.namespace = {} |
paul@83 | 118 | self.globals = set() |
paul@180 | 119 | self.module = module |
paul@92 | 120 | self.finalised = 0 |
paul@83 | 121 | |
paul@272 | 122 | # Attributes accessed on objects, potentially narrowing their types. |
paul@272 | 123 | # Specific namespaces should define known names during initialisation. |
paul@280 | 124 | # For example, functions can define names belonging to parameters. |
paul@272 | 125 | |
paul@272 | 126 | self.attributes_used = [{}] # stack of usage |
paul@272 | 127 | self.attribute_shelves = [] # stack of unmerged definitions |
paul@278 | 128 | self.attribute_users = [{}] # stack of assignments |
paul@280 | 129 | self.all_attributes_used = [] |
paul@272 | 130 | |
paul@272 | 131 | # Attribute/name definition and access. |
paul@272 | 132 | |
paul@167 | 133 | def __delitem__(self, name): |
paul@167 | 134 | del self.namespace[name] |
paul@167 | 135 | |
paul@167 | 136 | def has_key(self, name): |
paul@167 | 137 | return self.namespace.has_key(name) |
paul@167 | 138 | |
paul@167 | 139 | def keys(self): |
paul@167 | 140 | return self.namespace.keys() |
paul@167 | 141 | |
paul@167 | 142 | def values(self): |
paul@167 | 143 | return self.namespace.values() |
paul@167 | 144 | |
paul@167 | 145 | def items(self): |
paul@167 | 146 | return self.namespace.items() |
paul@167 | 147 | |
paul@83 | 148 | def __getitem__(self, name): |
paul@83 | 149 | return self.namespace[name] |
paul@83 | 150 | |
paul@83 | 151 | def get(self, name, default=None): |
paul@83 | 152 | return self.namespace.get(name, default) |
paul@83 | 153 | |
paul@83 | 154 | def __setitem__(self, name, value): |
paul@83 | 155 | self.set(name, value) |
paul@83 | 156 | |
paul@83 | 157 | def set(self, name, value, single_assignment=1): |
paul@83 | 158 | |
paul@83 | 159 | """ |
paul@83 | 160 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@83 | 161 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@83 | 162 | this operation (or whether the operation covers potentially many |
paul@83 | 163 | assignments in the lifetime of a program). |
paul@83 | 164 | """ |
paul@83 | 165 | |
paul@83 | 166 | if name in self.globals: |
paul@180 | 167 | self.module.set(name, value, 0) |
paul@83 | 168 | else: |
paul@192 | 169 | self._set(name, value, single_assignment) |
paul@83 | 170 | |
paul@83 | 171 | def set_module(self, name, value): |
paul@83 | 172 | |
paul@83 | 173 | """ |
paul@83 | 174 | A specialised set operation, making 'name' refer to 'value' in the |
paul@83 | 175 | context of making a module reference available in association with |
paul@83 | 176 | 'name' as part of the import of that module or a submodule of that |
paul@83 | 177 | module. |
paul@83 | 178 | """ |
paul@83 | 179 | |
paul@192 | 180 | self._set(name, value, 1) |
paul@83 | 181 | |
paul@192 | 182 | def _set(self, name, attr_or_value, single_assignment=1): |
paul@83 | 183 | |
paul@187 | 184 | """ |
paul@192 | 185 | The underlying set operation associating 'name' with the given |
paul@192 | 186 | 'attr_or_value'. |
paul@187 | 187 | See: docs/assignment.txt |
paul@187 | 188 | """ |
paul@83 | 189 | |
paul@192 | 190 | # Add and/or obtain the namespace entry. |
paul@87 | 191 | |
paul@192 | 192 | if not self.namespace.has_key(name): |
paul@192 | 193 | self.namespace[name] = Attr(None, self, name) |
paul@120 | 194 | |
paul@192 | 195 | attr = self.namespace[name] |
paul@187 | 196 | |
paul@192 | 197 | # Handle attribute assignment as well as assignment of basic objects. |
paul@237 | 198 | # Attempt to fix the context if not explicitly defined. |
paul@192 | 199 | |
paul@192 | 200 | if isinstance(attr_or_value, Attr): |
paul@237 | 201 | context_values = self.get_updated_context_values(attr_or_value.context_values) |
paul@237 | 202 | else: |
paul@237 | 203 | context_values = self.get_updated_context_values([self.get_context_and_value(attr_or_value)]) |
paul@187 | 204 | |
paul@237 | 205 | attr.update(context_values, single_assignment) |
paul@237 | 206 | |
paul@237 | 207 | def get_context_and_value(self, value): |
paul@237 | 208 | |
paul@237 | 209 | "Return a context, value tuple for the given 'value'." |
paul@237 | 210 | |
paul@237 | 211 | # Functions have a replaceable context. |
paul@120 | 212 | |
paul@237 | 213 | if isinstance(value, Function): |
paul@237 | 214 | return (ReplaceableContext, value) |
paul@237 | 215 | |
paul@237 | 216 | # Classes use placeholder contexts which cannot be replaced but which |
paul@237 | 217 | # do not communicate useful contextual information. |
paul@237 | 218 | |
paul@237 | 219 | elif isinstance(value, Class): |
paul@237 | 220 | return (PlaceholderContext, value) |
paul@237 | 221 | |
paul@237 | 222 | # Other values employ themselves as the context. |
paul@87 | 223 | |
paul@192 | 224 | else: |
paul@237 | 225 | return (value, value) |
paul@83 | 226 | |
paul@192 | 227 | def get_updated_context_values(self, context_values): |
paul@87 | 228 | |
paul@119 | 229 | """ |
paul@192 | 230 | Adapt the contexts found in the given 'context_values', returning a new |
paul@192 | 231 | set. |
paul@187 | 232 | See: docs/assignment.txt |
paul@119 | 233 | """ |
paul@87 | 234 | |
paul@192 | 235 | results = set() |
paul@192 | 236 | |
paul@192 | 237 | for context, value in context_values: |
paul@192 | 238 | |
paul@192 | 239 | # Set the context of instances to themselves. |
paul@187 | 240 | |
paul@192 | 241 | if isinstance(value, Instance): |
paul@192 | 242 | results.add((value, value)) |
paul@192 | 243 | else: |
paul@192 | 244 | results.add((context, value)) |
paul@192 | 245 | |
paul@192 | 246 | return results |
paul@87 | 247 | |
paul@83 | 248 | def make_global(self, name): |
paul@83 | 249 | if not self.namespace.has_key(name): |
paul@83 | 250 | self.globals.add(name) |
paul@178 | 251 | return 1 |
paul@83 | 252 | else: |
paul@178 | 253 | return 0 |
paul@83 | 254 | |
paul@272 | 255 | # Attribute positioning. |
paul@272 | 256 | |
paul@83 | 257 | def attributes_as_list(self): |
paul@92 | 258 | |
paul@92 | 259 | "Return the attributes in a list." |
paul@92 | 260 | |
paul@83 | 261 | self.finalise_attributes() |
paul@83 | 262 | l = [None] * len(self.keys()) |
paul@83 | 263 | for attr in self.values(): |
paul@83 | 264 | l[attr.position] = attr |
paul@83 | 265 | return l |
paul@83 | 266 | |
paul@282 | 267 | def finalise_attributes(self, reset=0): |
paul@83 | 268 | |
paul@83 | 269 | "Make sure all attributes are fully defined." |
paul@83 | 270 | |
paul@282 | 271 | if reset: |
paul@282 | 272 | self.unfinalise_attributes() |
paul@282 | 273 | |
paul@92 | 274 | if self.finalised: |
paul@92 | 275 | return |
paul@92 | 276 | |
paul@83 | 277 | # The default action is to assign attribute positions sequentially. |
paul@83 | 278 | |
paul@83 | 279 | for i, attr in enumerate(self.values()): |
paul@83 | 280 | attr.position = i |
paul@83 | 281 | |
paul@92 | 282 | self.finalised = 1 |
paul@92 | 283 | |
paul@282 | 284 | def unfinalise_attributes(self): |
paul@282 | 285 | |
paul@282 | 286 | "Open attribute definitions to editing and subsequent finalisation." |
paul@282 | 287 | |
paul@282 | 288 | self.finalised = 0 |
paul@282 | 289 | |
paul@272 | 290 | # Attribute usage methods. |
paul@272 | 291 | |
paul@280 | 292 | def get_active_attributes(self): |
paul@280 | 293 | |
paul@280 | 294 | """ |
paul@280 | 295 | Return attributes on this object that are actually used. |
paul@280 | 296 | """ |
paul@280 | 297 | |
paul@280 | 298 | return self.attributes_used[-1].get(None, []) |
paul@280 | 299 | |
paul@280 | 300 | def get_all_attribute_usage(self): |
paul@280 | 301 | |
paul@280 | 302 | """ |
paul@280 | 303 | Return a set of all usage tuples for attributes used with the different |
paul@280 | 304 | local names. |
paul@280 | 305 | """ |
paul@280 | 306 | |
paul@280 | 307 | usage = set() |
paul@280 | 308 | for names in self.all_attributes_used: |
paul@280 | 309 | usage.add(tuple(names)) |
paul@280 | 310 | return usage |
paul@280 | 311 | |
paul@280 | 312 | # These shadow various methods in the InspectedModule class, and provide |
paul@280 | 313 | # implementations generally. |
paul@280 | 314 | |
paul@280 | 315 | def _use_attribute(self, name, attrname): |
paul@280 | 316 | |
paul@280 | 317 | """ |
paul@280 | 318 | Indicate the use on the given 'name' in this namespace of an attribute |
paul@280 | 319 | with the given 'attrname'. |
paul@280 | 320 | """ |
paul@280 | 321 | |
paul@272 | 322 | defs = self.attributes_used[-1] |
paul@278 | 323 | users = self.attribute_users[-1] |
paul@280 | 324 | |
paul@280 | 325 | # Permit the unforeseen definition of names since some names are likely |
paul@280 | 326 | # to be defined by things (such as class definitions) which will not |
paul@280 | 327 | # require access optimisations, but which still need to be monitored. |
paul@280 | 328 | |
paul@280 | 329 | if not defs.has_key(name): |
paul@280 | 330 | defs[name] = set() |
paul@280 | 331 | |
paul@272 | 332 | defs[name].add(attrname) |
paul@275 | 333 | return defs[name] |
paul@272 | 334 | |
paul@278 | 335 | def _define_attribute_user(self, node): |
paul@279 | 336 | |
paul@279 | 337 | """ |
paul@279 | 338 | Define 'node' as the user of attributes, indicating the point where the |
paul@279 | 339 | user is defined. |
paul@279 | 340 | """ |
paul@279 | 341 | |
paul@278 | 342 | name = node.name |
paul@279 | 343 | self._define_attribute_user_for_name(node, name) |
paul@279 | 344 | |
paul@279 | 345 | def _define_attribute_user_for_name(self, node, name): |
paul@280 | 346 | |
paul@280 | 347 | "Define 'node' as the user of attributes for the given 'name'." |
paul@280 | 348 | |
paul@272 | 349 | defs = self.attributes_used[-1] |
paul@278 | 350 | users = self.attribute_users[-1] |
paul@278 | 351 | users[name] = node |
paul@279 | 352 | defs[name] = set() |
paul@280 | 353 | |
paul@280 | 354 | # Record the attribute combinations for the name. |
paul@280 | 355 | |
paul@279 | 356 | if not hasattr(node, "_attrnames"): |
paul@279 | 357 | node._attrnames = {} |
paul@279 | 358 | node._attrnames[name] = defs[name] |
paul@272 | 359 | |
paul@280 | 360 | # Remember all attribute combinations. |
paul@280 | 361 | |
paul@280 | 362 | self.all_attributes_used.append(defs[name]) |
paul@280 | 363 | |
paul@275 | 364 | def _reset_all_attributes(self): |
paul@275 | 365 | self.attributes_used[-1] = {} |
paul@278 | 366 | self.attribute_users[-1] = {} |
paul@275 | 367 | |
paul@272 | 368 | def _new_branchpoint(self): |
paul@272 | 369 | self.attribute_shelves.append([]) |
paul@272 | 370 | |
paul@272 | 371 | def _new_branch(self): |
paul@272 | 372 | d = {} |
paul@272 | 373 | for name, attrnames in self.attributes_used[-1].items(): |
paul@272 | 374 | d[name] = set(attrnames) |
paul@272 | 375 | self.attributes_used.append(d) |
paul@278 | 376 | self.attribute_users.append({}) |
paul@272 | 377 | |
paul@272 | 378 | def _shelve_branch(self): |
paul@272 | 379 | self.attribute_shelves[-1].append(self.attributes_used.pop()) |
paul@278 | 380 | self.attribute_users.pop() |
paul@272 | 381 | |
paul@272 | 382 | def _merge_branches(self): |
paul@272 | 383 | active = self.attributes_used[-1] |
paul@272 | 384 | |
paul@272 | 385 | shelved_defs = self.attribute_shelves.pop() |
paul@272 | 386 | defs = shelved_defs[0] |
paul@272 | 387 | |
paul@272 | 388 | for next_defs in shelved_defs[1:]: |
paul@272 | 389 | for name, attrnames in next_defs.items(): |
paul@272 | 390 | if defs.has_key(name): |
paul@275 | 391 | defs[name] = defs[name].intersection(attrnames) |
paul@272 | 392 | |
paul@272 | 393 | for name, attrnames in defs.items(): |
paul@272 | 394 | if active.has_key(name): |
paul@278 | 395 | active[name].intersection_update(attrnames) |
paul@272 | 396 | else: |
paul@272 | 397 | active[name] = attrnames |
paul@272 | 398 | |
paul@85 | 399 | # Program data structures. There are two separate kinds of structures: those |
paul@85 | 400 | # with context, which are the values manipulated by programs, and those without |
paul@85 | 401 | # context, which are typically constant things which are stored alongside the |
paul@85 | 402 | # program but which are wrapped in context-dependent structures in the running |
paul@85 | 403 | # program. |
paul@83 | 404 | |
paul@83 | 405 | class Attr: |
paul@83 | 406 | |
paul@87 | 407 | "An attribute entry having a context." |
paul@83 | 408 | |
paul@192 | 409 | def __init__(self, position, parent, name): |
paul@119 | 410 | |
paul@119 | 411 | """ |
paul@119 | 412 | Initialise the attribute with the given 'position' within the collection |
paul@192 | 413 | of attributes of its 'parent', indicating its 'name'. |
paul@119 | 414 | """ |
paul@119 | 415 | |
paul@83 | 416 | self.position = position |
paul@83 | 417 | self.parent = parent |
paul@83 | 418 | self.name = name |
paul@192 | 419 | |
paul@272 | 420 | # Possible values. |
paul@272 | 421 | |
paul@192 | 422 | self.context_values = set() |
paul@83 | 423 | |
paul@83 | 424 | # Number of assignments per name. |
paul@83 | 425 | |
paul@192 | 426 | self.assignments = None |
paul@192 | 427 | |
paul@268 | 428 | # Value-related methods. |
paul@268 | 429 | |
paul@192 | 430 | def get_contexts(self): |
paul@192 | 431 | return [c for (c, v) in self.context_values] |
paul@167 | 432 | |
paul@192 | 433 | def get_values(self): |
paul@192 | 434 | return [v for (c, v) in self.context_values] |
paul@192 | 435 | |
paul@192 | 436 | def get_context(self): |
paul@237 | 437 | if len(self.context_values) == 1: |
paul@237 | 438 | return self.get_contexts()[0] |
paul@237 | 439 | else: |
paul@237 | 440 | return None |
paul@192 | 441 | |
paul@192 | 442 | def get_value(self): |
paul@237 | 443 | if len(self.context_values) == 1: |
paul@237 | 444 | return self.get_values()[0] |
paul@237 | 445 | else: |
paul@237 | 446 | return None |
paul@192 | 447 | |
paul@192 | 448 | def update(self, context_values, single_assignment): |
paul@83 | 449 | |
paul@83 | 450 | """ |
paul@192 | 451 | Update the attribute, adding the 'context_values' provided to the |
paul@192 | 452 | known details associated with the attribute, changing the number of |
paul@192 | 453 | assignments according to the 'single_assignment' status of the |
paul@192 | 454 | operation, where a true value indicates that only one assignment is |
paul@192 | 455 | associated with the update, and a false value indicates that potentially |
paul@192 | 456 | many assignments may be involved. |
paul@83 | 457 | """ |
paul@83 | 458 | |
paul@83 | 459 | if self.assignments is None: |
paul@83 | 460 | if single_assignment: |
paul@83 | 461 | self.assignments = 1 |
paul@83 | 462 | else: |
paul@83 | 463 | self.assignments = AtLeast(1) |
paul@83 | 464 | else: |
paul@83 | 465 | if single_assignment: |
paul@83 | 466 | self.assignments += 1 |
paul@83 | 467 | else: |
paul@83 | 468 | self.assignments += AtLeast(1) |
paul@104 | 469 | |
paul@192 | 470 | self.context_values.update(context_values) |
paul@87 | 471 | |
paul@242 | 472 | def is_static_attribute(self): |
paul@242 | 473 | |
paul@242 | 474 | """ |
paul@242 | 475 | Return whether this attribute is defined on a fixed/static object such |
paul@242 | 476 | as a class or a module. |
paul@242 | 477 | """ |
paul@242 | 478 | |
paul@242 | 479 | return isinstance(self.parent, (Class, Module)) |
paul@127 | 480 | |
paul@243 | 481 | def defines_ambiguous_class(self): |
paul@243 | 482 | |
paul@243 | 483 | "Return whether this attribute defines more than one class." |
paul@243 | 484 | |
paul@243 | 485 | if self.assignments > 1: |
paul@243 | 486 | have_class = 0 |
paul@243 | 487 | for obj in self.get_values(): |
paul@243 | 488 | if isinstance(obj, Class): |
paul@243 | 489 | if have_class: |
paul@243 | 490 | return 1 |
paul@243 | 491 | have_class = 1 |
paul@243 | 492 | |
paul@243 | 493 | return 0 |
paul@243 | 494 | |
paul@119 | 495 | def defined_within_hierarchy(self): |
paul@119 | 496 | |
paul@119 | 497 | """ |
paul@119 | 498 | Return whether the parent and context of the attribute belong to the |
paul@119 | 499 | same class hierarchy. |
paul@119 | 500 | """ |
paul@119 | 501 | |
paul@192 | 502 | # Must be defined within a class. |
paul@192 | 503 | |
paul@192 | 504 | if isinstance(self.parent, Class): |
paul@192 | 505 | |
paul@192 | 506 | # To be sure, all contexts must be classes and be the same as the |
paul@192 | 507 | # parent, or be a superclass of the parent, or be a subclass of the |
paul@192 | 508 | # parent. |
paul@192 | 509 | |
paul@192 | 510 | for context in self.get_contexts(): |
paul@192 | 511 | if not ( |
paul@192 | 512 | isinstance(context, Class) and ( |
paul@192 | 513 | context is self.parent or |
paul@192 | 514 | context.has_subclass(self.parent) or |
paul@192 | 515 | self.parent.has_subclass(context)) |
paul@192 | 516 | ): |
paul@192 | 517 | return 0 |
paul@192 | 518 | |
paul@192 | 519 | return 1 |
paul@192 | 520 | |
paul@192 | 521 | # Instance attributes are not defined within a hierarchy. |
paul@192 | 522 | |
paul@192 | 523 | else: |
paul@192 | 524 | return 0 |
paul@119 | 525 | |
paul@194 | 526 | def defined_outside_hierarchy(self): |
paul@194 | 527 | |
paul@194 | 528 | """ |
paul@194 | 529 | Return whether the parent and context of the attribute never belong to |
paul@194 | 530 | the same class hierarchy. |
paul@194 | 531 | """ |
paul@194 | 532 | |
paul@194 | 533 | # Must be defined within a class. |
paul@194 | 534 | |
paul@194 | 535 | if isinstance(self.parent, Class): |
paul@194 | 536 | |
paul@194 | 537 | # To be sure, all contexts must be classes and be the same as the |
paul@194 | 538 | # parent, or be a superclass of the parent, or be a subclass of the |
paul@194 | 539 | # parent. |
paul@194 | 540 | |
paul@194 | 541 | for context in self.get_contexts(): |
paul@194 | 542 | if not ( |
paul@194 | 543 | isinstance(context, Class) and not ( |
paul@194 | 544 | context is self.parent or |
paul@194 | 545 | context.has_subclass(self.parent) or |
paul@194 | 546 | self.parent.has_subclass(context)) |
paul@194 | 547 | ): |
paul@194 | 548 | return 0 |
paul@194 | 549 | |
paul@194 | 550 | return 1 |
paul@194 | 551 | |
paul@194 | 552 | # Instance attributes are not defined within a hierarchy. |
paul@194 | 553 | |
paul@194 | 554 | else: |
paul@194 | 555 | return 0 |
paul@194 | 556 | |
paul@83 | 557 | def __repr__(self): |
paul@192 | 558 | return "Attr(%r, %s, %r) # [%s], %r" % ( |
paul@192 | 559 | self.position, shortrepr(self.parent), self.name, |
paul@192 | 560 | self._context_values_str(), self.assignments |
paul@87 | 561 | ) |
paul@83 | 562 | |
paul@192 | 563 | def _context_values_str(self): |
paul@192 | 564 | l = [] |
paul@192 | 565 | for (c, v) in self.context_values: |
paul@192 | 566 | l.append("(c=%s, v=%s)" % (shortrepr(c), shortrepr(v))) |
paul@192 | 567 | return ", ".join(l) |
paul@192 | 568 | |
paul@86 | 569 | # Instances are special in that they need to be wrapped together with context in |
paul@86 | 570 | # a running program, but they are not generally constant. |
paul@86 | 571 | |
paul@86 | 572 | class Instance: |
paul@86 | 573 | |
paul@86 | 574 | "A placeholder indicating the involvement of an instance." |
paul@86 | 575 | |
paul@87 | 576 | def __init__(self): |
paul@87 | 577 | self.parent = None |
paul@87 | 578 | |
paul@119 | 579 | # Image generation details. |
paul@119 | 580 | |
paul@119 | 581 | self.location = None |
paul@119 | 582 | |
paul@86 | 583 | def __repr__(self): |
paul@86 | 584 | return "Instance()" |
paul@86 | 585 | |
paul@87 | 586 | __shortrepr__ = __repr__ |
paul@87 | 587 | |
paul@85 | 588 | class Constant: |
paul@85 | 589 | |
paul@85 | 590 | "A superclass for all constant or context-free structures." |
paul@85 | 591 | |
paul@85 | 592 | pass |
paul@85 | 593 | |
paul@167 | 594 | # Data objects appearing in programs before run-time. |
paul@167 | 595 | |
paul@86 | 596 | class Const(Constant, Instance): |
paul@83 | 597 | |
paul@83 | 598 | "A constant object with no context." |
paul@83 | 599 | |
paul@83 | 600 | def __init__(self, value): |
paul@167 | 601 | Instance.__init__(self) |
paul@83 | 602 | self.value = value |
paul@83 | 603 | |
paul@192 | 604 | def get_value(self): |
paul@265 | 605 | return self.value |
paul@192 | 606 | |
paul@83 | 607 | def __repr__(self): |
paul@83 | 608 | if self.location is not None: |
paul@83 | 609 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@83 | 610 | else: |
paul@83 | 611 | return "Const(%r)" % self.value |
paul@83 | 612 | |
paul@87 | 613 | __shortrepr__ = __repr__ |
paul@87 | 614 | |
paul@83 | 615 | # Support constants as dictionary keys in order to build constant tables. |
paul@83 | 616 | |
paul@83 | 617 | def __eq__(self, other): |
paul@192 | 618 | return other is not None and self.value == other.value and self.value.__class__ is other.value.__class__ |
paul@83 | 619 | |
paul@83 | 620 | def __hash__(self): |
paul@83 | 621 | return hash(self.value) |
paul@83 | 622 | |
paul@83 | 623 | def value_type_name(self): |
paul@83 | 624 | return "__builtins__." + self.value.__class__.__name__ |
paul@83 | 625 | |
paul@85 | 626 | class Class(NamespaceDict, Naming, Constant): |
paul@83 | 627 | |
paul@83 | 628 | "An inspected class." |
paul@83 | 629 | |
paul@180 | 630 | def __init__(self, name, parent, module=None, node=None): |
paul@83 | 631 | |
paul@83 | 632 | """ |
paul@83 | 633 | Initialise the class with the given 'name', 'parent' object, optional |
paul@180 | 634 | 'module' and optional AST 'node'. |
paul@83 | 635 | """ |
paul@83 | 636 | |
paul@180 | 637 | NamespaceDict.__init__(self, module) |
paul@83 | 638 | self.name = name |
paul@83 | 639 | self.parent = parent |
paul@91 | 640 | self.astnode = node |
paul@83 | 641 | |
paul@83 | 642 | # Superclasses, descendants and attributes. |
paul@83 | 643 | |
paul@83 | 644 | self.bases = [] |
paul@83 | 645 | self.descendants = set() |
paul@83 | 646 | self.instattr = set() # instance attributes |
paul@83 | 647 | self.relocated = set() # attributes which do not have the same |
paul@83 | 648 | # position as those of the same name in |
paul@83 | 649 | # some superclasses |
paul@83 | 650 | |
paul@83 | 651 | # Caches. |
paul@83 | 652 | |
paul@282 | 653 | self.reset_caches() |
paul@83 | 654 | |
paul@83 | 655 | # Image generation details. |
paul@83 | 656 | |
paul@83 | 657 | self.location = None |
paul@83 | 658 | self.code_location = None |
paul@240 | 659 | self.code_body_location = None # corresponds to the instantiator |
paul@240 | 660 | |
paul@83 | 661 | self.instantiator = None |
paul@184 | 662 | self.instance_template_location = None # for creating instances at run-time |
paul@83 | 663 | |
paul@83 | 664 | # Program-related details. |
paul@83 | 665 | |
paul@173 | 666 | self.blocks = None |
paul@116 | 667 | self.temp_usage = 0 |
paul@116 | 668 | self.local_usage = 0 |
paul@133 | 669 | self.all_local_usage = 0 |
paul@83 | 670 | |
paul@192 | 671 | # Add this class to its attributes. |
paul@192 | 672 | |
paul@192 | 673 | self.set("__class__", self) |
paul@192 | 674 | |
paul@282 | 675 | def reset_caches(self): |
paul@282 | 676 | |
paul@282 | 677 | "Reset the caches." |
paul@282 | 678 | |
paul@282 | 679 | self.all_instattr = None # cache for instance_attributes |
paul@282 | 680 | self.all_instattr_names = None # from all_instattr |
paul@282 | 681 | self.all_classattr = None # cache for all_class_attributes |
paul@282 | 682 | self.all_classattr_names = None # from all_classattr |
paul@282 | 683 | self.allattr = None # cache for all_attributes |
paul@282 | 684 | self.allattr_names = None # from allattr |
paul@282 | 685 | |
paul@83 | 686 | def __repr__(self): |
paul@83 | 687 | if self.location is not None: |
paul@87 | 688 | return "Class(%r, %s, location=%r)" % (self.name, shortrepr(self.parent), self.location) |
paul@83 | 689 | else: |
paul@87 | 690 | return "Class(%r, %s)" % (self.name, shortrepr(self.parent)) |
paul@87 | 691 | |
paul@87 | 692 | def __shortrepr__(self): |
paul@87 | 693 | return "Class(%r, %s)" % (self.name, shortrepr(self.parent)) |
paul@87 | 694 | |
paul@227 | 695 | def get_body_block(self): |
paul@227 | 696 | return self.get_instantiator().blocks[0] |
paul@215 | 697 | |
paul@189 | 698 | # Namespace-related methods. |
paul@189 | 699 | |
paul@192 | 700 | def get_updated_context_values(self, context_values): |
paul@87 | 701 | |
paul@119 | 702 | """ |
paul@192 | 703 | Adapt the contexts found in the given 'context_values', returning a new |
paul@192 | 704 | set. |
paul@187 | 705 | See: docs/assignment.txt |
paul@119 | 706 | """ |
paul@87 | 707 | |
paul@192 | 708 | results = set() |
paul@192 | 709 | |
paul@192 | 710 | for context, value in context_values: |
paul@187 | 711 | |
paul@187 | 712 | # Change the ownership of functions. |
paul@187 | 713 | |
paul@237 | 714 | if context is ReplaceableContext and value is not None and isinstance(value, Function): |
paul@192 | 715 | results.add((self, value)) |
paul@192 | 716 | else: |
paul@192 | 717 | results.add((context, value)) |
paul@187 | 718 | |
paul@192 | 719 | return NamespaceDict.get_updated_context_values(self, results) |
paul@83 | 720 | |
paul@282 | 721 | def finalise_attributes(self, reset=0): |
paul@83 | 722 | |
paul@83 | 723 | "Make sure that all attributes are fully defined." |
paul@83 | 724 | |
paul@282 | 725 | if reset: |
paul@282 | 726 | self.unfinalise_attributes() |
paul@282 | 727 | |
paul@92 | 728 | if self.finalised: |
paul@92 | 729 | return |
paul@92 | 730 | |
paul@83 | 731 | self.finalise_class_attributes() |
paul@83 | 732 | self.finalise_instance_attributes() |
paul@92 | 733 | self.finalised = 1 |
paul@83 | 734 | |
paul@282 | 735 | def unfinalise_attributes(self): |
paul@282 | 736 | |
paul@282 | 737 | "Open attribute definitions to editing and subsequent finalisation." |
paul@282 | 738 | |
paul@282 | 739 | self.reset_caches() |
paul@282 | 740 | self.finalised = 0 |
paul@282 | 741 | |
paul@184 | 742 | # Convenience methods for accessing functions and methods. |
paul@184 | 743 | |
paul@83 | 744 | def get_instantiator(self): |
paul@83 | 745 | |
paul@83 | 746 | "Return a function which can be used to instantiate the class." |
paul@83 | 747 | |
paul@83 | 748 | if self.instantiator is None: |
paul@212 | 749 | self.instantiator = self.get_init_method().as_instantiator() |
paul@83 | 750 | return self.instantiator |
paul@83 | 751 | |
paul@137 | 752 | def get_init_method(self): |
paul@192 | 753 | return self.all_class_attributes()["__init__"].get_value() |
paul@137 | 754 | |
paul@83 | 755 | # Class-specific methods. |
paul@83 | 756 | |
paul@83 | 757 | def add_base(self, base): |
paul@83 | 758 | self.bases.append(base) |
paul@83 | 759 | base.add_descendant(self) |
paul@83 | 760 | |
paul@83 | 761 | def add_instance_attribute(self, name): |
paul@83 | 762 | self.instattr.add(name) |
paul@83 | 763 | |
paul@83 | 764 | def add_descendant(self, cls): |
paul@83 | 765 | self.descendants.add(cls) |
paul@83 | 766 | for base in self.bases: |
paul@83 | 767 | base.add_descendant(cls) |
paul@83 | 768 | |
paul@122 | 769 | def has_subclass(self, other): |
paul@122 | 770 | return other in self.descendants |
paul@122 | 771 | |
paul@135 | 772 | def all_descendants(self): |
paul@135 | 773 | d = {} |
paul@135 | 774 | for cls in self.descendants: |
paul@135 | 775 | d[cls.full_name()] = cls |
paul@135 | 776 | return d |
paul@135 | 777 | |
paul@83 | 778 | "Return the attribute names provided by this class only." |
paul@83 | 779 | |
paul@83 | 780 | class_attribute_names = NamespaceDict.keys |
paul@83 | 781 | |
paul@83 | 782 | def class_attributes(self): |
paul@83 | 783 | |
paul@83 | 784 | "Return class attributes provided by this class only." |
paul@83 | 785 | |
paul@83 | 786 | return dict(self) |
paul@83 | 787 | |
paul@83 | 788 | def all_class_attribute_names(self): |
paul@83 | 789 | |
paul@83 | 790 | "Return the attribute names provided by classes in this hierarchy." |
paul@83 | 791 | |
paul@83 | 792 | if self.all_classattr_names is None: |
paul@83 | 793 | self.all_class_attributes() |
paul@282 | 794 | self.all_classattr_names = self.all_classattr.keys() |
paul@83 | 795 | return self.all_classattr_names |
paul@83 | 796 | |
paul@83 | 797 | def all_class_attributes(self): |
paul@83 | 798 | |
paul@83 | 799 | "Return all class attributes, indicating the class which provides them." |
paul@83 | 800 | |
paul@83 | 801 | self.finalise_class_attributes() |
paul@83 | 802 | return self.all_classattr |
paul@83 | 803 | |
paul@83 | 804 | def finalise_class_attributes(self): |
paul@83 | 805 | |
paul@83 | 806 | "Make sure that the class attributes are fully defined." |
paul@83 | 807 | |
paul@83 | 808 | if self.all_classattr is None: |
paul@83 | 809 | self.all_classattr = {} |
paul@83 | 810 | clsattr = {} |
paul@83 | 811 | |
paul@83 | 812 | # Record provisional position information for attributes of this |
paul@83 | 813 | # class. |
paul@83 | 814 | |
paul@83 | 815 | for name in self.class_attributes().keys(): |
paul@83 | 816 | clsattr[name] = set() # position not yet defined |
paul@83 | 817 | |
paul@83 | 818 | reversed_bases = self.bases[:] |
paul@83 | 819 | reversed_bases.reverse() |
paul@83 | 820 | |
paul@83 | 821 | # For the bases in reverse order, acquire class attribute details. |
paul@83 | 822 | |
paul@83 | 823 | for cls in reversed_bases: |
paul@83 | 824 | for name, attr in cls.all_class_attributes().items(): |
paul@83 | 825 | self.all_classattr[name] = attr |
paul@83 | 826 | |
paul@83 | 827 | # Record previous attribute information. |
paul@83 | 828 | |
paul@83 | 829 | if clsattr.has_key(name): |
paul@83 | 830 | clsattr[name].add(attr.position) |
paul@83 | 831 | |
paul@83 | 832 | # Record class attributes provided by this class and its bases, |
paul@83 | 833 | # along with their positions. |
paul@83 | 834 | |
paul@83 | 835 | self.all_classattr.update(self.class_attributes()) |
paul@83 | 836 | |
paul@83 | 837 | if clsattr: |
paul@83 | 838 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@83 | 839 | self.all_classattr[name].position = i |
paul@83 | 840 | |
paul@83 | 841 | return self.all_classattr |
paul@83 | 842 | |
paul@83 | 843 | def instance_attribute_names(self): |
paul@83 | 844 | |
paul@83 | 845 | "Return the instance attribute names provided by the class." |
paul@83 | 846 | |
paul@83 | 847 | if self.all_instattr_names is None: |
paul@83 | 848 | self.instance_attributes() |
paul@83 | 849 | return self.all_instattr_names |
paul@83 | 850 | |
paul@83 | 851 | def instance_attributes(self): |
paul@83 | 852 | |
paul@83 | 853 | "Return instance-only attributes for instances of this class." |
paul@83 | 854 | |
paul@83 | 855 | self.finalise_instance_attributes() |
paul@83 | 856 | return self.all_instattr |
paul@83 | 857 | |
paul@83 | 858 | def finalise_instance_attributes(self): |
paul@83 | 859 | |
paul@83 | 860 | "Make sure that the instance attributes are fully defined." |
paul@83 | 861 | |
paul@210 | 862 | # Cache the attributes by converting the positioned attributes into a |
paul@210 | 863 | # dictionary. |
paul@83 | 864 | |
paul@210 | 865 | if self.all_instattr is None: |
paul@210 | 866 | self.all_instattr = self._get_attributes() |
paul@83 | 867 | self.all_instattr_names = self.all_instattr.keys() |
paul@83 | 868 | |
paul@83 | 869 | return self.all_instattr |
paul@83 | 870 | |
paul@210 | 871 | def _get_attributes(self): |
paul@210 | 872 | |
paul@210 | 873 | """ |
paul@210 | 874 | Return a dictionary mapping names to Attr instances incorporating |
paul@210 | 875 | information about their positions in the final instance structure. |
paul@210 | 876 | """ |
paul@210 | 877 | |
paul@210 | 878 | instattr = {} |
paul@210 | 879 | |
paul@210 | 880 | # Record provisional position information for attributes of this |
paul@210 | 881 | # instance. |
paul@210 | 882 | |
paul@210 | 883 | for name in self.instattr: |
paul@210 | 884 | instattr[name] = set() # position not yet defined |
paul@210 | 885 | |
paul@210 | 886 | reversed_bases = self.bases[:] |
paul@210 | 887 | reversed_bases.reverse() |
paul@210 | 888 | |
paul@210 | 889 | # For the bases in reverse order, acquire instance attribute |
paul@210 | 890 | # details. |
paul@210 | 891 | |
paul@210 | 892 | for cls in reversed_bases: |
paul@210 | 893 | for name, attr in cls.instance_attributes().items(): |
paul@210 | 894 | |
paul@210 | 895 | # Record previous attribute information. |
paul@210 | 896 | |
paul@210 | 897 | if instattr.has_key(name): |
paul@210 | 898 | instattr[name].add(attr.position) |
paul@235 | 899 | else: |
paul@235 | 900 | instattr[name] = set([attr.position]) |
paul@210 | 901 | |
paul@210 | 902 | # Build the dictionary of attributes using the existing positions known |
paul@210 | 903 | # for each name. |
paul@210 | 904 | |
paul@210 | 905 | d = {} |
paul@210 | 906 | for i, name in enumerate(self._get_position_list(instattr)): |
paul@210 | 907 | d[name] = Attr(i, Instance(), name) |
paul@210 | 908 | return d |
paul@210 | 909 | |
paul@83 | 910 | def _get_position_list(self, positions): |
paul@83 | 911 | |
paul@83 | 912 | """ |
paul@83 | 913 | Return a list of attribute names for the given 'positions' mapping from |
paul@83 | 914 | names to positions, indicating the positions of the attributes in the |
paul@83 | 915 | final instance structure. |
paul@83 | 916 | """ |
paul@83 | 917 | |
paul@83 | 918 | position_items = positions.items() |
paul@83 | 919 | namearray = [None] * len(position_items) |
paul@83 | 920 | |
paul@83 | 921 | # Get the positions in ascending order of list size, with lists |
paul@83 | 922 | # of the same size ordered according to their smallest position |
paul@83 | 923 | # value. |
paul@83 | 924 | |
paul@83 | 925 | position_items.sort(self._cmp_positions) |
paul@83 | 926 | |
paul@83 | 927 | # Get the names in position order. |
paul@83 | 928 | |
paul@83 | 929 | held = [] |
paul@83 | 930 | |
paul@83 | 931 | for name, pos in position_items: |
paul@83 | 932 | pos = list(pos) |
paul@83 | 933 | pos.sort() |
paul@83 | 934 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@83 | 935 | namearray[pos[0]] = name |
paul@83 | 936 | else: |
paul@83 | 937 | if pos: |
paul@83 | 938 | self.relocated.add(name) |
paul@83 | 939 | held.append((name, pos)) |
paul@83 | 940 | |
paul@83 | 941 | for i, attr in enumerate(namearray): |
paul@83 | 942 | if attr is None: |
paul@83 | 943 | name, pos = held.pop() |
paul@83 | 944 | namearray[i] = name |
paul@83 | 945 | |
paul@83 | 946 | return namearray |
paul@83 | 947 | |
paul@83 | 948 | def _cmp_positions(self, a, b): |
paul@83 | 949 | |
paul@83 | 950 | "Compare name plus position list operands 'a' and 'b'." |
paul@83 | 951 | |
paul@83 | 952 | name_a, list_a = a |
paul@83 | 953 | name_b, list_b = b |
paul@83 | 954 | if len(list_a) < len(list_b): |
paul@83 | 955 | return -1 |
paul@83 | 956 | elif len(list_a) > len(list_b): |
paul@83 | 957 | return 1 |
paul@83 | 958 | elif not list_a: |
paul@83 | 959 | return 0 |
paul@83 | 960 | else: |
paul@83 | 961 | return cmp(min(list_a), min(list_b)) |
paul@83 | 962 | |
paul@83 | 963 | def all_attribute_names(self): |
paul@83 | 964 | |
paul@83 | 965 | """ |
paul@83 | 966 | Return the names of all attributes provided by instances of this class. |
paul@83 | 967 | """ |
paul@83 | 968 | |
paul@83 | 969 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@83 | 970 | return self.allattr_names |
paul@83 | 971 | |
paul@83 | 972 | def all_attributes(self): |
paul@83 | 973 | |
paul@83 | 974 | """ |
paul@83 | 975 | Return all attributes for an instance, indicating either the class which |
paul@83 | 976 | provides them or that the instance itself provides them. |
paul@83 | 977 | """ |
paul@83 | 978 | |
paul@83 | 979 | if self.allattr is None: |
paul@83 | 980 | self.allattr = {} |
paul@83 | 981 | self.allattr.update(self.all_class_attributes()) |
paul@83 | 982 | for name, attr in self.instance_attributes().items(): |
paul@83 | 983 | if self.allattr.has_key(name): |
paul@83 | 984 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@83 | 985 | self.allattr[name] = attr |
paul@83 | 986 | return self.allattr |
paul@83 | 987 | |
paul@85 | 988 | class Function(NamespaceDict, Naming, Constant): |
paul@83 | 989 | |
paul@83 | 990 | "An inspected function." |
paul@83 | 991 | |
paul@180 | 992 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, module=None, node=None): |
paul@83 | 993 | |
paul@83 | 994 | """ |
paul@83 | 995 | Initialise the function with the given 'name', 'parent', list of |
paul@83 | 996 | 'argnames', list of 'defaults', the 'has_star' flag (indicating the |
paul@83 | 997 | presence of a * parameter), the 'has_dstar' flag (indicating the |
paul@180 | 998 | presence of a ** parameter), optional 'module', and optional AST 'node'. |
paul@83 | 999 | """ |
paul@83 | 1000 | |
paul@180 | 1001 | NamespaceDict.__init__(self, module) |
paul@83 | 1002 | self.name = name |
paul@83 | 1003 | self.parent = parent |
paul@83 | 1004 | self.argnames = argnames |
paul@83 | 1005 | self.defaults = defaults |
paul@83 | 1006 | self.has_star = has_star |
paul@83 | 1007 | self.has_dstar = has_dstar |
paul@91 | 1008 | self.astnode = node |
paul@83 | 1009 | |
paul@233 | 1010 | # For lambda functions with defaults, add a context argument. |
paul@233 | 1011 | |
paul@233 | 1012 | if name is None and defaults: |
paul@233 | 1013 | self.argnames.insert(0, "<context>") |
paul@233 | 1014 | |
paul@83 | 1015 | # Initialise the positional names. |
paul@83 | 1016 | |
paul@83 | 1017 | self.positional_names = self.argnames[:] |
paul@83 | 1018 | if has_dstar: |
paul@83 | 1019 | self.dstar_name = self.positional_names[-1] |
paul@83 | 1020 | del self.positional_names[-1] |
paul@83 | 1021 | if has_star: |
paul@83 | 1022 | self.star_name = self.positional_names[-1] |
paul@83 | 1023 | del self.positional_names[-1] |
paul@83 | 1024 | |
paul@83 | 1025 | # Initialise default storage. |
paul@83 | 1026 | # NOTE: This must be initialised separately due to the reliance on node |
paul@83 | 1027 | # NOTE: visiting. |
paul@83 | 1028 | |
paul@83 | 1029 | self.default_attrs = [] |
paul@83 | 1030 | |
paul@272 | 1031 | # Initialise attribute usage. |
paul@272 | 1032 | |
paul@272 | 1033 | for arg in argnames: |
paul@279 | 1034 | |
paul@279 | 1035 | # Define attribute users. |
paul@279 | 1036 | |
paul@279 | 1037 | if node is not None: |
paul@279 | 1038 | self._define_attribute_user_for_name(node, arg) |
paul@279 | 1039 | |
paul@279 | 1040 | # Or just record the usage. |
paul@279 | 1041 | |
paul@279 | 1042 | else: |
paul@279 | 1043 | self.attributes_used[-1][arg] = set() |
paul@272 | 1044 | |
paul@83 | 1045 | # Caches. |
paul@83 | 1046 | |
paul@83 | 1047 | self.localnames = None # cache for locals |
paul@83 | 1048 | |
paul@83 | 1049 | # Add parameters to the namespace. |
paul@83 | 1050 | |
paul@83 | 1051 | self._add_parameters(argnames) |
paul@83 | 1052 | |
paul@83 | 1053 | # Image generation details. |
paul@83 | 1054 | |
paul@83 | 1055 | self.location = None |
paul@83 | 1056 | self.code_location = None |
paul@240 | 1057 | self.code_body_location = None |
paul@83 | 1058 | |
paul@83 | 1059 | # Program-related details. |
paul@83 | 1060 | |
paul@173 | 1061 | self.blocks = None |
paul@225 | 1062 | self.body_block = None |
paul@225 | 1063 | |
paul@116 | 1064 | self.temp_usage = 0 |
paul@116 | 1065 | self.local_usage = 0 |
paul@133 | 1066 | self.all_local_usage = 0 |
paul@83 | 1067 | |
paul@83 | 1068 | def _add_parameters(self, argnames): |
paul@83 | 1069 | for name in argnames: |
paul@83 | 1070 | if isinstance(name, tuple): |
paul@83 | 1071 | self._add_parameters(name) |
paul@83 | 1072 | else: |
paul@83 | 1073 | self.set(name, None) |
paul@83 | 1074 | |
paul@83 | 1075 | def __repr__(self): |
paul@83 | 1076 | if self.location is not None: |
paul@225 | 1077 | return "Function(%r, %s, %r, location=%r, code_location=%r)" % ( |
paul@225 | 1078 | self.name, shortrepr(self.parent), self.argnames, self.location, self.code_location |
paul@83 | 1079 | ) |
paul@83 | 1080 | else: |
paul@98 | 1081 | return "Function(%r, %s, %r)" % ( |
paul@98 | 1082 | self.name, shortrepr(self.parent), self.argnames |
paul@83 | 1083 | ) |
paul@83 | 1084 | |
paul@87 | 1085 | def __shortrepr__(self): |
paul@87 | 1086 | return "Function(%r, %s)" % ( |
paul@87 | 1087 | self.name, shortrepr(self.parent) |
paul@87 | 1088 | ) |
paul@87 | 1089 | |
paul@227 | 1090 | def get_body_block(self): |
paul@227 | 1091 | return self.body_block |
paul@215 | 1092 | |
paul@189 | 1093 | # Namespace-related methods. |
paul@189 | 1094 | |
paul@83 | 1095 | def store_default(self, value): |
paul@192 | 1096 | attr = Attr(None, self, None) |
paul@237 | 1097 | attr.update([self.get_context_and_value(value)], 1) |
paul@83 | 1098 | self.default_attrs.append(attr) |
paul@83 | 1099 | |
paul@83 | 1100 | def make_global(self, name): |
paul@83 | 1101 | if name not in self.argnames and not self.has_key(name): |
paul@83 | 1102 | self.globals.add(name) |
paul@178 | 1103 | return 1 |
paul@83 | 1104 | else: |
paul@178 | 1105 | return 0 |
paul@83 | 1106 | |
paul@83 | 1107 | def parameters(self): |
paul@83 | 1108 | |
paul@83 | 1109 | """ |
paul@83 | 1110 | Return a dictionary mapping parameter names to their position in the |
paul@83 | 1111 | parameter list. |
paul@83 | 1112 | """ |
paul@83 | 1113 | |
paul@83 | 1114 | parameters = {} |
paul@83 | 1115 | for i, name in enumerate(self.argnames): |
paul@83 | 1116 | parameters[name] = i |
paul@83 | 1117 | return parameters |
paul@83 | 1118 | |
paul@83 | 1119 | def all_locals(self): |
paul@83 | 1120 | |
paul@83 | 1121 | "Return a dictionary mapping names to local and parameter details." |
paul@83 | 1122 | |
paul@83 | 1123 | return dict(self) |
paul@83 | 1124 | |
paul@83 | 1125 | def locals(self): |
paul@83 | 1126 | |
paul@83 | 1127 | "Return a dictionary mapping names to local details." |
paul@83 | 1128 | |
paul@83 | 1129 | if self.localnames is None: |
paul@83 | 1130 | self.localnames = {} |
paul@83 | 1131 | self.localnames.update(self.all_locals()) |
paul@83 | 1132 | for name in self.argnames: |
paul@83 | 1133 | del self.localnames[name] |
paul@83 | 1134 | return self.localnames |
paul@83 | 1135 | |
paul@83 | 1136 | def is_method(self): |
paul@83 | 1137 | |
paul@241 | 1138 | """ |
paul@241 | 1139 | Return whether this function is a method explicitly defined in a class. |
paul@241 | 1140 | """ |
paul@83 | 1141 | |
paul@83 | 1142 | return isinstance(self.parent, Class) |
paul@83 | 1143 | |
paul@83 | 1144 | def is_relocated(self, name): |
paul@83 | 1145 | |
paul@83 | 1146 | """ |
paul@83 | 1147 | Determine whether the given attribute 'name' is relocated for instances |
paul@83 | 1148 | having this function as a method. |
paul@83 | 1149 | """ |
paul@83 | 1150 | |
paul@83 | 1151 | for cls in self.parent.descendants: |
paul@83 | 1152 | if name in cls.relocated: |
paul@83 | 1153 | return 1 |
paul@83 | 1154 | return 0 |
paul@83 | 1155 | |
paul@282 | 1156 | def finalise_attributes(self, reset=0): |
paul@83 | 1157 | |
paul@83 | 1158 | """ |
paul@83 | 1159 | Make sure all attributes (locals) are fully defined. Note that locals |
paul@83 | 1160 | are not attributes in the sense of class, module or instance attributes. |
paul@83 | 1161 | Defaults are also finalised by this method. |
paul@83 | 1162 | """ |
paul@83 | 1163 | |
paul@282 | 1164 | if reset: |
paul@282 | 1165 | self.unfinalise_attributes() |
paul@282 | 1166 | |
paul@210 | 1167 | if self.finalised: |
paul@210 | 1168 | return |
paul@210 | 1169 | |
paul@210 | 1170 | # Defaults. |
paul@210 | 1171 | |
paul@83 | 1172 | for i, default in enumerate(self.default_attrs): |
paul@83 | 1173 | default.position = i |
paul@83 | 1174 | |
paul@210 | 1175 | # Locals. |
paul@210 | 1176 | |
paul@83 | 1177 | i = None |
paul@83 | 1178 | for i, name in enumerate(self.argnames): |
paul@83 | 1179 | self[name].position = i |
paul@83 | 1180 | |
paul@83 | 1181 | if i is not None: |
paul@144 | 1182 | nparams = i + 1 |
paul@83 | 1183 | else: |
paul@144 | 1184 | nparams = 0 |
paul@144 | 1185 | |
paul@144 | 1186 | i = None |
paul@144 | 1187 | for i, attr in enumerate(self.locals().values()): |
paul@144 | 1188 | attr.position = i + nparams |
paul@83 | 1189 | |
paul@144 | 1190 | if i is not None: |
paul@144 | 1191 | nothers = i + 1 |
paul@144 | 1192 | else: |
paul@144 | 1193 | nothers = 0 |
paul@83 | 1194 | |
paul@144 | 1195 | self.local_usage = nothers |
paul@144 | 1196 | self.all_local_usage = nparams + nothers |
paul@92 | 1197 | self.finalised = 1 |
paul@83 | 1198 | |
paul@212 | 1199 | def as_instantiator(self): |
paul@83 | 1200 | |
paul@230 | 1201 | "Make an instantiator function from a method, keeping all arguments." |
paul@83 | 1202 | |
paul@230 | 1203 | function = Function(self.parent.name, self.parent.parent, self.argnames, self.defaults, |
paul@180 | 1204 | self.has_star, self.has_dstar, self.module, self.astnode) |
paul@83 | 1205 | function.default_attrs = self.default_attrs |
paul@83 | 1206 | return function |
paul@83 | 1207 | |
paul@85 | 1208 | class UnresolvedName(NamespaceDict, Constant): |
paul@83 | 1209 | |
paul@83 | 1210 | "A module, class or function which was mentioned but could not be imported." |
paul@83 | 1211 | |
paul@180 | 1212 | def __init__(self, name, parent_name, module=None): |
paul@180 | 1213 | NamespaceDict.__init__(self, module) |
paul@83 | 1214 | self.name = name |
paul@83 | 1215 | self.parent_name = parent_name |
paul@88 | 1216 | self.parent = None |
paul@167 | 1217 | |
paul@167 | 1218 | self.descendants = set() |
paul@167 | 1219 | |
paul@167 | 1220 | def add_descendant(self, cls): |
paul@167 | 1221 | self.descendants.add(cls) |
paul@83 | 1222 | |
paul@83 | 1223 | def all_class_attributes(self): |
paul@83 | 1224 | return {} |
paul@83 | 1225 | |
paul@83 | 1226 | def instance_attributes(self): |
paul@83 | 1227 | return {} |
paul@83 | 1228 | |
paul@83 | 1229 | def __repr__(self): |
paul@83 | 1230 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@83 | 1231 | |
paul@87 | 1232 | __shortrepr__ = __repr__ |
paul@87 | 1233 | |
paul@83 | 1234 | def full_name(self): |
paul@83 | 1235 | if self.name is not None: |
paul@83 | 1236 | return self.parent_name + "." + self.name |
paul@83 | 1237 | else: |
paul@83 | 1238 | return self.parent_name |
paul@83 | 1239 | |
paul@85 | 1240 | class Module(NamespaceDict, Constant): |
paul@83 | 1241 | |
paul@83 | 1242 | "An inspected module's core details." |
paul@83 | 1243 | |
paul@83 | 1244 | def __init__(self, name): |
paul@83 | 1245 | NamespaceDict.__init__(self, self) |
paul@83 | 1246 | self.name = name |
paul@88 | 1247 | self.parent = None |
paul@83 | 1248 | |
paul@83 | 1249 | # Original location details. |
paul@83 | 1250 | |
paul@91 | 1251 | self.astnode = None |
paul@83 | 1252 | |
paul@83 | 1253 | # Complete lists of classes and functions. |
paul@83 | 1254 | |
paul@83 | 1255 | self.all_objects = set() |
paul@83 | 1256 | |
paul@83 | 1257 | # Keyword records. |
paul@83 | 1258 | |
paul@83 | 1259 | self.keyword_names = set() |
paul@83 | 1260 | |
paul@83 | 1261 | # Image generation details. |
paul@83 | 1262 | |
paul@83 | 1263 | self.location = None |
paul@83 | 1264 | self.code_location = None |
paul@83 | 1265 | |
paul@83 | 1266 | # Program-related details. |
paul@83 | 1267 | |
paul@173 | 1268 | self.blocks = None |
paul@116 | 1269 | self.temp_usage = 0 |
paul@116 | 1270 | self.local_usage = 0 |
paul@133 | 1271 | self.all_local_usage = 0 |
paul@83 | 1272 | |
paul@83 | 1273 | def full_name(self): |
paul@83 | 1274 | return self.name |
paul@83 | 1275 | |
paul@83 | 1276 | def __repr__(self): |
paul@83 | 1277 | if self.location is not None: |
paul@83 | 1278 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@83 | 1279 | else: |
paul@83 | 1280 | return "Module(%r)" % self.name |
paul@83 | 1281 | |
paul@87 | 1282 | def __shortrepr__(self): |
paul@87 | 1283 | return "Module(%r)" % self.name |
paul@87 | 1284 | |
paul@83 | 1285 | # Attribute methods. |
paul@83 | 1286 | |
paul@83 | 1287 | "Return the module attribute names provided by the module." |
paul@83 | 1288 | |
paul@83 | 1289 | module_attribute_names = NamespaceDict.keys |
paul@83 | 1290 | |
paul@83 | 1291 | def module_attributes(self): |
paul@83 | 1292 | |
paul@83 | 1293 | "Return a dictionary mapping names to module attributes." |
paul@83 | 1294 | |
paul@83 | 1295 | return dict(self) |
paul@83 | 1296 | |
paul@83 | 1297 | # vim: tabstop=4 expandtab shiftwidth=4 |