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