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