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