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