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