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