paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Inspect and obtain module structure. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, |
paul@0 | 7 | 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 8 | |
paul@0 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 12 | version. |
paul@0 | 13 | |
paul@0 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 17 | details. |
paul@0 | 18 | |
paul@0 | 19 | You should have received a copy of the GNU General Public License along with |
paul@0 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 21 | """ |
paul@0 | 22 | |
paul@0 | 23 | from branching import BranchTracker |
paul@366 | 24 | from common import CommonModule, get_argnames, get_builtin_type, init_item, \ |
paul@366 | 25 | predefined_constants |
paul@26 | 26 | from modules import BasicModule, CacheWritingModule, InspectionNaming |
paul@3 | 27 | from errors import InspectError |
paul@0 | 28 | from referencing import Reference |
paul@12 | 29 | from resolving import NameResolving |
paul@12 | 30 | from results import AccessRef, InstanceRef, InvocationRef, LiteralSequenceRef, \ |
paul@226 | 31 | LocalNameRef, NameRef, ResolvedNameRef, VariableRef |
paul@0 | 32 | import compiler |
paul@0 | 33 | import sys |
paul@0 | 34 | |
paul@26 | 35 | class InspectedModule(BasicModule, CacheWritingModule, NameResolving, InspectionNaming): |
paul@0 | 36 | |
paul@0 | 37 | "A module inspector." |
paul@0 | 38 | |
paul@0 | 39 | def __init__(self, name, importer): |
paul@13 | 40 | |
paul@13 | 41 | "Initialise the module with basic details." |
paul@13 | 42 | |
paul@0 | 43 | BasicModule.__init__(self, name, importer) |
paul@12 | 44 | |
paul@0 | 45 | self.in_class = False |
paul@0 | 46 | self.in_conditional = False |
paul@110 | 47 | |
paul@110 | 48 | # Accesses to global attributes. |
paul@110 | 49 | |
paul@0 | 50 | self.global_attr_accesses = {} |
paul@0 | 51 | |
paul@0 | 52 | # Usage tracking. |
paul@0 | 53 | |
paul@0 | 54 | self.trackers = [] |
paul@0 | 55 | self.attr_accessor_branches = {} |
paul@0 | 56 | |
paul@0 | 57 | def __repr__(self): |
paul@0 | 58 | return "InspectedModule(%r, %r)" % (self.name, self.importer) |
paul@0 | 59 | |
paul@27 | 60 | # Principal methods. |
paul@27 | 61 | |
paul@0 | 62 | def parse(self, filename): |
paul@0 | 63 | |
paul@0 | 64 | "Parse the file having the given 'filename'." |
paul@0 | 65 | |
paul@0 | 66 | self.parse_file(filename) |
paul@0 | 67 | |
paul@0 | 68 | # Inspect the module. |
paul@0 | 69 | |
paul@0 | 70 | self.start_tracking_in_module() |
paul@0 | 71 | |
paul@0 | 72 | # Detect and record imports and globals declared in the module. |
paul@0 | 73 | |
paul@0 | 74 | self.process_structure(self.astnode) |
paul@0 | 75 | |
paul@0 | 76 | # Set the class of the module after the definition has occurred. |
paul@0 | 77 | |
paul@269 | 78 | ref = self.get_builtin("module") |
paul@0 | 79 | self.set_name("__class__", ref) |
paul@271 | 80 | self.set_name("__mname__", self.get_constant("string", self.name).reference()) |
paul@271 | 81 | self.set_name("__file__", self.get_constant("string", filename).reference()) |
paul@0 | 82 | |
paul@406 | 83 | # Reserve a constant for the encoding. |
paul@406 | 84 | |
paul@406 | 85 | if self.encoding: |
paul@406 | 86 | self.get_constant("string", self.encoding) |
paul@406 | 87 | |
paul@0 | 88 | # Get module-level attribute usage details. |
paul@0 | 89 | |
paul@0 | 90 | self.stop_tracking_in_module() |
paul@0 | 91 | |
paul@27 | 92 | # Collect external name references. |
paul@0 | 93 | |
paul@27 | 94 | self.collect_names() |
paul@0 | 95 | |
paul@12 | 96 | def complete(self): |
paul@0 | 97 | |
paul@12 | 98 | "Complete the module inspection." |
paul@0 | 99 | |
paul@12 | 100 | # Resolve names not definitively mapped to objects. |
paul@0 | 101 | |
paul@12 | 102 | self.resolve() |
paul@0 | 103 | |
paul@12 | 104 | # Define the invocation requirements in each namespace. |
paul@0 | 105 | |
paul@12 | 106 | self.set_invocation_usage() |
paul@0 | 107 | |
paul@12 | 108 | # Propagate to the importer information needed in subsequent activities. |
paul@0 | 109 | |
paul@12 | 110 | self.propagate() |
paul@0 | 111 | |
paul@27 | 112 | # Accessory methods. |
paul@0 | 113 | |
paul@27 | 114 | def collect_names(self): |
paul@0 | 115 | |
paul@27 | 116 | "Collect the names used by each scope." |
paul@0 | 117 | |
paul@0 | 118 | for path in self.names_used.keys(): |
paul@27 | 119 | self.collect_names_for_path(path) |
paul@27 | 120 | |
paul@27 | 121 | def collect_names_for_path(self, path): |
paul@0 | 122 | |
paul@33 | 123 | """ |
paul@33 | 124 | Collect the names used by the given 'path'. These are propagated to the |
paul@33 | 125 | importer in advance of any dependency resolution. |
paul@33 | 126 | """ |
paul@0 | 127 | |
paul@0 | 128 | names = self.names_used.get(path) |
paul@0 | 129 | if not names: |
paul@0 | 130 | return |
paul@0 | 131 | |
paul@0 | 132 | in_function = self.function_locals.has_key(path) |
paul@0 | 133 | |
paul@0 | 134 | for name in names: |
paul@135 | 135 | if in_function and name in self.function_locals[path]: |
paul@135 | 136 | continue |
paul@135 | 137 | |
paul@135 | 138 | key = "%s.%s" % (path, name) |
paul@135 | 139 | |
paul@35 | 140 | # Find local definitions (within dynamic namespaces). |
paul@0 | 141 | |
paul@27 | 142 | ref = self.get_resolved_object(key) |
paul@0 | 143 | if ref: |
paul@40 | 144 | self.set_name_reference(key, ref) |
paul@0 | 145 | continue |
paul@0 | 146 | |
paul@142 | 147 | # Find global. |
paul@0 | 148 | |
paul@142 | 149 | ref = self.get_global(name) |
paul@27 | 150 | if ref: |
paul@40 | 151 | self.set_name_reference(key, ref) |
paul@0 | 152 | continue |
paul@0 | 153 | |
paul@40 | 154 | # Find presumed built-in definitions. |
paul@0 | 155 | |
paul@40 | 156 | ref = self.get_builtin(name) |
paul@40 | 157 | self.set_name_reference(key, ref) |
paul@0 | 158 | |
paul@40 | 159 | def set_name_reference(self, path, ref): |
paul@0 | 160 | |
paul@40 | 161 | "Map the given name 'path' to 'ref'." |
paul@0 | 162 | |
paul@40 | 163 | self.importer.all_name_references[path] = self.name_references[path] = ref |
paul@0 | 164 | |
paul@0 | 165 | # Module structure traversal. |
paul@0 | 166 | |
paul@0 | 167 | def process_structure_node(self, n): |
paul@0 | 168 | |
paul@0 | 169 | "Process the individual node 'n'." |
paul@0 | 170 | |
paul@205 | 171 | path = self.get_namespace_path() |
paul@205 | 172 | |
paul@0 | 173 | # Module global detection. |
paul@0 | 174 | |
paul@0 | 175 | if isinstance(n, compiler.ast.Global): |
paul@0 | 176 | self.process_global_node(n) |
paul@0 | 177 | |
paul@0 | 178 | # Module import declarations. |
paul@0 | 179 | |
paul@0 | 180 | elif isinstance(n, compiler.ast.From): |
paul@0 | 181 | self.process_from_node(n) |
paul@0 | 182 | |
paul@0 | 183 | elif isinstance(n, compiler.ast.Import): |
paul@0 | 184 | self.process_import_node(n) |
paul@0 | 185 | |
paul@0 | 186 | # Nodes using operator module functions. |
paul@0 | 187 | |
paul@0 | 188 | elif isinstance(n, compiler.ast.Operator): |
paul@0 | 189 | return self.process_operator_node(n) |
paul@0 | 190 | |
paul@0 | 191 | elif isinstance(n, compiler.ast.AugAssign): |
paul@0 | 192 | self.process_augassign_node(n) |
paul@0 | 193 | |
paul@0 | 194 | elif isinstance(n, compiler.ast.Compare): |
paul@0 | 195 | return self.process_compare_node(n) |
paul@0 | 196 | |
paul@0 | 197 | elif isinstance(n, compiler.ast.Slice): |
paul@0 | 198 | return self.process_slice_node(n) |
paul@0 | 199 | |
paul@0 | 200 | elif isinstance(n, compiler.ast.Sliceobj): |
paul@0 | 201 | return self.process_sliceobj_node(n) |
paul@0 | 202 | |
paul@0 | 203 | elif isinstance(n, compiler.ast.Subscript): |
paul@0 | 204 | return self.process_subscript_node(n) |
paul@0 | 205 | |
paul@0 | 206 | # Namespaces within modules. |
paul@0 | 207 | |
paul@0 | 208 | elif isinstance(n, compiler.ast.Class): |
paul@0 | 209 | self.process_class_node(n) |
paul@0 | 210 | |
paul@0 | 211 | elif isinstance(n, compiler.ast.Function): |
paul@0 | 212 | self.process_function_node(n, n.name) |
paul@0 | 213 | |
paul@0 | 214 | elif isinstance(n, compiler.ast.Lambda): |
paul@0 | 215 | return self.process_lambda_node(n) |
paul@0 | 216 | |
paul@0 | 217 | # Assignments. |
paul@0 | 218 | |
paul@0 | 219 | elif isinstance(n, compiler.ast.Assign): |
paul@0 | 220 | |
paul@0 | 221 | # Handle each assignment node. |
paul@0 | 222 | |
paul@0 | 223 | for node in n.nodes: |
paul@0 | 224 | self.process_assignment_node(node, n.expr) |
paul@0 | 225 | |
paul@0 | 226 | # Assignments within non-Assign nodes. |
paul@0 | 227 | |
paul@0 | 228 | elif isinstance(n, compiler.ast.AssName): |
paul@205 | 229 | raise InspectError("Name assignment appearing outside assignment statement.", path, n) |
paul@0 | 230 | |
paul@0 | 231 | elif isinstance(n, compiler.ast.AssAttr): |
paul@205 | 232 | raise InspectError("Attribute assignment appearing outside assignment statement.", path, n) |
paul@0 | 233 | |
paul@0 | 234 | # Accesses. |
paul@0 | 235 | |
paul@0 | 236 | elif isinstance(n, compiler.ast.Getattr): |
paul@0 | 237 | return self.process_attribute_access(n) |
paul@0 | 238 | |
paul@0 | 239 | # Name recording for later testing. |
paul@0 | 240 | |
paul@0 | 241 | elif isinstance(n, compiler.ast.Name): |
paul@0 | 242 | return self.process_name_node(n) |
paul@0 | 243 | |
paul@0 | 244 | # Conditional statement tracking. |
paul@0 | 245 | |
paul@0 | 246 | elif isinstance(n, compiler.ast.For): |
paul@0 | 247 | self.process_for_node(n) |
paul@0 | 248 | |
paul@0 | 249 | elif isinstance(n, compiler.ast.While): |
paul@0 | 250 | self.process_while_node(n) |
paul@0 | 251 | |
paul@0 | 252 | elif isinstance(n, compiler.ast.If): |
paul@0 | 253 | self.process_if_node(n) |
paul@0 | 254 | |
paul@0 | 255 | elif isinstance(n, (compiler.ast.And, compiler.ast.Or)): |
paul@0 | 256 | return self.process_logical_node(n) |
paul@0 | 257 | |
paul@0 | 258 | # Exception control-flow tracking. |
paul@0 | 259 | |
paul@0 | 260 | elif isinstance(n, compiler.ast.TryExcept): |
paul@0 | 261 | self.process_try_node(n) |
paul@0 | 262 | |
paul@0 | 263 | elif isinstance(n, compiler.ast.TryFinally): |
paul@0 | 264 | self.process_try_finally_node(n) |
paul@0 | 265 | |
paul@0 | 266 | # Control-flow modification statements. |
paul@0 | 267 | |
paul@0 | 268 | elif isinstance(n, compiler.ast.Break): |
paul@0 | 269 | self.trackers[-1].suspend_broken_branch() |
paul@0 | 270 | |
paul@0 | 271 | elif isinstance(n, compiler.ast.Continue): |
paul@0 | 272 | self.trackers[-1].suspend_continuing_branch() |
paul@0 | 273 | |
paul@0 | 274 | elif isinstance(n, compiler.ast.Raise): |
paul@0 | 275 | self.process_structure(n) |
paul@0 | 276 | self.trackers[-1].abandon_branch() |
paul@0 | 277 | |
paul@0 | 278 | elif isinstance(n, compiler.ast.Return): |
paul@0 | 279 | self.process_structure(n) |
paul@0 | 280 | self.trackers[-1].abandon_returning_branch() |
paul@0 | 281 | |
paul@173 | 282 | # Print statements. |
paul@173 | 283 | |
paul@173 | 284 | elif isinstance(n, (compiler.ast.Print, compiler.ast.Printnl)): |
paul@173 | 285 | self.process_print_node(n) |
paul@173 | 286 | |
paul@0 | 287 | # Invocations. |
paul@0 | 288 | |
paul@0 | 289 | elif isinstance(n, compiler.ast.CallFunc): |
paul@0 | 290 | return self.process_invocation_node(n) |
paul@0 | 291 | |
paul@0 | 292 | # Constant usage. |
paul@0 | 293 | |
paul@0 | 294 | elif isinstance(n, compiler.ast.Const): |
paul@405 | 295 | return self.get_literal_instance(n) |
paul@0 | 296 | |
paul@0 | 297 | elif isinstance(n, compiler.ast.Dict): |
paul@0 | 298 | return self.get_literal_instance(n, "dict") |
paul@0 | 299 | |
paul@0 | 300 | elif isinstance(n, compiler.ast.List): |
paul@0 | 301 | return self.get_literal_instance(n, "list") |
paul@0 | 302 | |
paul@0 | 303 | elif isinstance(n, compiler.ast.Tuple): |
paul@0 | 304 | return self.get_literal_instance(n, "tuple") |
paul@0 | 305 | |
paul@3 | 306 | # Unsupported nodes. |
paul@3 | 307 | |
paul@3 | 308 | elif isinstance(n, compiler.ast.GenExpr): |
paul@205 | 309 | raise InspectError("Generator expressions are not supported.", path, n) |
paul@3 | 310 | |
paul@3 | 311 | elif isinstance(n, compiler.ast.IfExp): |
paul@205 | 312 | raise InspectError("If-else expressions are not supported.", path, n) |
paul@0 | 313 | |
paul@0 | 314 | elif isinstance(n, compiler.ast.ListComp): |
paul@205 | 315 | raise InspectError("List comprehensions are not supported.", path, n) |
paul@0 | 316 | |
paul@0 | 317 | # All other nodes are processed depth-first. |
paul@0 | 318 | |
paul@0 | 319 | else: |
paul@0 | 320 | self.process_structure(n) |
paul@0 | 321 | |
paul@0 | 322 | # By default, no expression details are returned. |
paul@0 | 323 | |
paul@0 | 324 | return None |
paul@0 | 325 | |
paul@0 | 326 | # Specific node handling. |
paul@0 | 327 | |
paul@0 | 328 | def process_assignment_node(self, n, expr): |
paul@0 | 329 | |
paul@0 | 330 | "Process the individual node 'n' to be assigned the contents of 'expr'." |
paul@0 | 331 | |
paul@0 | 332 | # Names and attributes are assigned the entire expression. |
paul@0 | 333 | |
paul@0 | 334 | if isinstance(n, compiler.ast.AssName): |
paul@61 | 335 | if n.name == "self": |
paul@61 | 336 | raise InspectError("Redefinition of self is not allowed.", self.get_namespace_path(), n) |
paul@0 | 337 | |
paul@0 | 338 | name_ref = expr and self.process_structure_node(expr) |
paul@0 | 339 | |
paul@0 | 340 | # Name assignments populate either function namespaces or the |
paul@0 | 341 | # general namespace hierarchy. |
paul@0 | 342 | |
paul@0 | 343 | self.assign_general_local(n.name, name_ref) |
paul@0 | 344 | |
paul@0 | 345 | # Record usage of the name. |
paul@0 | 346 | |
paul@0 | 347 | self.record_name(n.name) |
paul@0 | 348 | |
paul@0 | 349 | elif isinstance(n, compiler.ast.AssAttr): |
paul@124 | 350 | if expr: |
paul@124 | 351 | expr = self.process_structure_node(expr) |
paul@107 | 352 | |
paul@107 | 353 | in_assignment = self.in_assignment |
paul@389 | 354 | self.in_assignment = True |
paul@0 | 355 | self.process_attribute_access(n) |
paul@107 | 356 | self.in_assignment = in_assignment |
paul@0 | 357 | |
paul@0 | 358 | # Lists and tuples are matched against the expression and their |
paul@0 | 359 | # items assigned to expression items. |
paul@0 | 360 | |
paul@0 | 361 | elif isinstance(n, (compiler.ast.AssList, compiler.ast.AssTuple)): |
paul@0 | 362 | self.process_assignment_node_items(n, expr) |
paul@0 | 363 | |
paul@0 | 364 | # Slices and subscripts are permitted within assignment nodes. |
paul@0 | 365 | |
paul@0 | 366 | elif isinstance(n, compiler.ast.Slice): |
paul@0 | 367 | self.process_slice_node(n, expr) |
paul@0 | 368 | |
paul@0 | 369 | elif isinstance(n, compiler.ast.Subscript): |
paul@0 | 370 | self.process_subscript_node(n, expr) |
paul@0 | 371 | |
paul@0 | 372 | def process_attribute_access(self, n): |
paul@0 | 373 | |
paul@0 | 374 | "Process the given attribute access node 'n'." |
paul@0 | 375 | |
paul@107 | 376 | # Obtain any completed chain and return the reference to it. |
paul@107 | 377 | |
paul@0 | 378 | name_ref = self.process_attribute_chain(n) |
paul@107 | 379 | |
paul@0 | 380 | if self.have_access_expression(n): |
paul@0 | 381 | return name_ref |
paul@0 | 382 | |
paul@0 | 383 | # Where the start of the chain of attributes has been reached, determine |
paul@0 | 384 | # the complete access. |
paul@0 | 385 | |
paul@0 | 386 | # Given a non-access node, this chain can be handled in its entirety, |
paul@0 | 387 | # either being name-based and thus an access rooted on a name, or being |
paul@0 | 388 | # based on some other node and thus an anonymous access of some kind. |
paul@0 | 389 | |
paul@0 | 390 | path = self.get_namespace_path() |
paul@0 | 391 | |
paul@0 | 392 | # Start with the the full attribute chain. |
paul@0 | 393 | |
paul@0 | 394 | remaining = self.attrs |
paul@0 | 395 | attrnames = ".".join(remaining) |
paul@0 | 396 | |
paul@0 | 397 | # If the accessor cannot be identified, or where attributes |
paul@0 | 398 | # remain in an attribute chain, record the anonymous accesses. |
paul@0 | 399 | |
paul@0 | 400 | if not isinstance(name_ref, NameRef): # includes ResolvedNameRef |
paul@0 | 401 | |
paul@0 | 402 | init_item(self.attr_accesses, path, set) |
paul@0 | 403 | self.attr_accesses[path].add(attrnames) |
paul@0 | 404 | |
paul@117 | 405 | self.record_access_details(None, attrnames, self.in_assignment, |
paul@117 | 406 | self.in_invocation) |
paul@0 | 407 | del self.attrs[0] |
paul@0 | 408 | return |
paul@0 | 409 | |
paul@0 | 410 | # Name-based accesses will handle the first attribute in a |
paul@0 | 411 | # chain. |
paul@0 | 412 | |
paul@0 | 413 | else: |
paul@0 | 414 | attrname = remaining[0] |
paul@0 | 415 | |
paul@0 | 416 | # Attribute assignments are used to identify instance attributes. |
paul@0 | 417 | |
paul@0 | 418 | if isinstance(n, compiler.ast.AssAttr) and \ |
paul@0 | 419 | self.in_class and self.in_function and n.expr.name == "self": |
paul@0 | 420 | |
paul@0 | 421 | self.set_instance_attr(attrname) |
paul@0 | 422 | |
paul@0 | 423 | # Record attribute usage using any name local to this namespace, |
paul@0 | 424 | # if assigned in the namespace, or using an external name |
paul@0 | 425 | # (presently just globals within classes). |
paul@0 | 426 | |
paul@0 | 427 | name = self.get_name_for_tracking(name_ref.name, name_ref.final()) |
paul@0 | 428 | tracker = self.trackers[-1] |
paul@0 | 429 | |
paul@0 | 430 | immediate_access = len(self.attrs) == 1 |
paul@0 | 431 | assignment = immediate_access and isinstance(n, compiler.ast.AssAttr) |
paul@0 | 432 | |
paul@0 | 433 | # Record global-based chains for subsequent resolution. |
paul@0 | 434 | |
paul@0 | 435 | is_global = self.in_function and not self.function_locals[path].has_key(name) or \ |
paul@0 | 436 | not self.in_function |
paul@0 | 437 | |
paul@0 | 438 | if is_global: |
paul@0 | 439 | self.record_global_access_details(name, attrnames) |
paul@0 | 440 | |
paul@0 | 441 | # Make sure the name is being tracked: global names will not |
paul@0 | 442 | # already be initialised in a branch and must be added |
paul@0 | 443 | # explicitly. |
paul@0 | 444 | |
paul@0 | 445 | if not tracker.have_name(name): |
paul@0 | 446 | tracker.assign_names([name]) |
paul@0 | 447 | if self.in_function: |
paul@0 | 448 | self.scope_globals[path].add(name) |
paul@0 | 449 | |
paul@0 | 450 | # Record attribute usage in the tracker, and record the branch |
paul@0 | 451 | # information for the access. |
paul@0 | 452 | |
paul@110 | 453 | branches = tracker.use_attribute(name, attrname, self.in_invocation, assignment) |
paul@0 | 454 | |
paul@0 | 455 | if not branches: |
paul@84 | 456 | raise InspectError("Name %s is accessed using %s before an assignment." % ( |
paul@84 | 457 | name, attrname), path, n) |
paul@0 | 458 | |
paul@0 | 459 | self.record_branches_for_access(branches, name, attrnames) |
paul@117 | 460 | access_number = self.record_access_details(name, attrnames, |
paul@117 | 461 | self.in_assignment, self.in_invocation) |
paul@107 | 462 | |
paul@107 | 463 | del self.attrs[0] |
paul@0 | 464 | return AccessRef(name, attrnames, access_number) |
paul@0 | 465 | |
paul@0 | 466 | def process_class_node(self, n): |
paul@0 | 467 | |
paul@0 | 468 | "Process the given class node 'n'." |
paul@0 | 469 | |
paul@0 | 470 | path = self.get_namespace_path() |
paul@0 | 471 | |
paul@0 | 472 | # To avoid notions of class "versions" where the same definition |
paul@0 | 473 | # might be parameterised with different state and be referenced |
paul@0 | 474 | # elsewhere (as base classes, for example), classes in functions or |
paul@0 | 475 | # conditions are forbidden. |
paul@0 | 476 | |
paul@0 | 477 | if self.in_function or self.in_conditional: |
paul@0 | 478 | print >>sys.stderr, "In %s, class %s in function or conditional statement ignored." % ( |
paul@0 | 479 | path, n.name) |
paul@0 | 480 | return |
paul@0 | 481 | |
paul@0 | 482 | # Resolve base classes. |
paul@0 | 483 | |
paul@0 | 484 | bases = [] |
paul@0 | 485 | |
paul@0 | 486 | for base in n.bases: |
paul@0 | 487 | base_class = self.get_class(base) |
paul@0 | 488 | |
paul@0 | 489 | if not base_class: |
paul@12 | 490 | print >>sys.stderr, "In %s, class %s has unidentifiable base class: %s" % ( |
paul@12 | 491 | path, n.name, base) |
paul@0 | 492 | return |
paul@0 | 493 | else: |
paul@0 | 494 | bases.append(base_class) |
paul@0 | 495 | |
paul@348 | 496 | # Detect conflicting definitions. Such definitions cause conflicts in |
paul@348 | 497 | # the storage of namespace-related information. |
paul@348 | 498 | |
paul@348 | 499 | class_name = self.get_object_path(n.name) |
paul@422 | 500 | ref = self.get_object(class_name, defer=False) |
paul@348 | 501 | |
paul@422 | 502 | if ref and ref.static(): |
paul@348 | 503 | raise InspectError("Multiple definitions for the same name are not permitted.", class_name, n) |
paul@348 | 504 | |
paul@0 | 505 | # Record bases for the class and retain the class name. |
paul@107 | 506 | # Note that the function class does not inherit from the object class. |
paul@0 | 507 | |
paul@107 | 508 | if not bases and class_name != "__builtins__.core.object" and \ |
paul@107 | 509 | class_name != "__builtins__.core.function": |
paul@107 | 510 | |
paul@0 | 511 | ref = self.get_object("__builtins__.object") |
paul@0 | 512 | bases.append(ref) |
paul@0 | 513 | |
paul@0 | 514 | self.importer.classes[class_name] = self.classes[class_name] = bases |
paul@0 | 515 | self.importer.subclasses[class_name] = set() |
paul@0 | 516 | self.scope_globals[class_name] = set() |
paul@0 | 517 | |
paul@0 | 518 | # Set the definition before entering the namespace rather than |
paul@0 | 519 | # afterwards because methods may reference it. In normal Python, |
paul@0 | 520 | # a class is not accessible until the definition is complete, but |
paul@0 | 521 | # methods can generally reference it since upon being called the |
paul@0 | 522 | # class will already exist. |
paul@0 | 523 | |
paul@0 | 524 | self.set_definition(n.name, "<class>") |
paul@0 | 525 | |
paul@0 | 526 | in_class = self.in_class |
paul@0 | 527 | self.in_class = class_name |
paul@0 | 528 | self.set_instance_attr("__class__", Reference("<class>", class_name)) |
paul@0 | 529 | self.enter_namespace(n.name) |
paul@107 | 530 | |
paul@107 | 531 | # Do not provide the special instantiator attributes on the function |
paul@107 | 532 | # class. Function instances provide these attributes. |
paul@107 | 533 | |
paul@107 | 534 | if class_name != "__builtins__.core.function": |
paul@107 | 535 | self.set_name("__fn__") # special instantiator attribute |
paul@107 | 536 | self.set_name("__args__") # special instantiator attribute |
paul@107 | 537 | |
paul@274 | 538 | self.set_name("__name__", self.get_constant("string", class_name).reference()) |
paul@274 | 539 | |
paul@0 | 540 | self.process_structure_node(n.code) |
paul@0 | 541 | self.exit_namespace() |
paul@0 | 542 | self.in_class = in_class |
paul@0 | 543 | |
paul@0 | 544 | def process_from_node(self, n): |
paul@0 | 545 | |
paul@0 | 546 | "Process the given node 'n', importing from another module." |
paul@0 | 547 | |
paul@0 | 548 | path = self.get_namespace_path() |
paul@0 | 549 | |
paul@12 | 550 | module_name, names = self.get_module_name(n) |
paul@12 | 551 | if module_name == self.name: |
paul@12 | 552 | raise InspectError("Cannot import from the current module.", path, n) |
paul@0 | 553 | |
paul@18 | 554 | self.queue_module(module_name) |
paul@0 | 555 | |
paul@0 | 556 | # Attempt to obtain the referenced objects. |
paul@0 | 557 | |
paul@0 | 558 | for name, alias in n.names: |
paul@0 | 559 | if name == "*": |
paul@12 | 560 | raise InspectError("Only explicitly specified names can be imported from modules.", path, n) |
paul@0 | 561 | |
paul@0 | 562 | # Explicit names. |
paul@0 | 563 | |
paul@12 | 564 | ref = self.import_name_from_module(name, module_name) |
paul@0 | 565 | value = ResolvedNameRef(alias or name, ref) |
paul@0 | 566 | self.set_general_local(alias or name, value) |
paul@0 | 567 | |
paul@0 | 568 | def process_function_node(self, n, name): |
paul@0 | 569 | |
paul@0 | 570 | """ |
paul@0 | 571 | Process the given function or lambda node 'n' with the given 'name'. |
paul@0 | 572 | """ |
paul@0 | 573 | |
paul@0 | 574 | is_lambda = isinstance(n, compiler.ast.Lambda) |
paul@0 | 575 | |
paul@0 | 576 | # Where a function is declared conditionally, use a separate name for |
paul@0 | 577 | # the definition, and assign the definition to the stated name. |
paul@0 | 578 | |
paul@0 | 579 | if (self.in_conditional or self.in_function) and not is_lambda: |
paul@0 | 580 | original_name = name |
paul@0 | 581 | name = self.get_lambda_name() |
paul@0 | 582 | else: |
paul@0 | 583 | original_name = None |
paul@0 | 584 | |
paul@348 | 585 | # Detect conflicting definitions. Such definitions cause conflicts in |
paul@348 | 586 | # the storage of namespace-related information. |
paul@348 | 587 | |
paul@348 | 588 | function_name = self.get_object_path(name) |
paul@422 | 589 | ref = self.get_object(function_name, defer=False) |
paul@348 | 590 | |
paul@422 | 591 | if ref and ref.static(): |
paul@348 | 592 | raise InspectError("Multiple definitions for the same name are not permitted.", function_name, n) |
paul@348 | 593 | |
paul@0 | 594 | # Initialise argument and local records. |
paul@0 | 595 | |
paul@46 | 596 | argnames = get_argnames(n.argnames) |
paul@48 | 597 | is_method = self.in_class and not self.in_function |
paul@0 | 598 | |
paul@48 | 599 | # Remove explicit "self" from method parameters. |
paul@46 | 600 | |
paul@48 | 601 | if is_method and argnames and argnames[0] == "self": |
paul@48 | 602 | del argnames[0] |
paul@48 | 603 | |
paul@48 | 604 | # Copy and propagate the parameters. |
paul@46 | 605 | |
paul@46 | 606 | self.importer.function_parameters[function_name] = \ |
paul@109 | 607 | self.function_parameters[function_name] = argnames[:] |
paul@46 | 608 | |
paul@46 | 609 | # Define all arguments/parameters in the local namespace. |
paul@46 | 610 | |
paul@109 | 611 | locals = \ |
paul@109 | 612 | self.importer.function_locals[function_name] = \ |
paul@109 | 613 | self.function_locals[function_name] = {} |
paul@0 | 614 | |
paul@48 | 615 | # Insert "self" into method locals. |
paul@48 | 616 | |
paul@48 | 617 | if is_method: |
paul@48 | 618 | argnames.insert(0, "self") |
paul@48 | 619 | |
paul@47 | 620 | # Define "self" in terms of the class if in a method. |
paul@47 | 621 | # This does not diminish the need for type-narrowing in the deducer. |
paul@47 | 622 | |
paul@47 | 623 | if argnames: |
paul@48 | 624 | if self.in_class and not self.in_function and argnames[0] == "self": |
paul@47 | 625 | locals[argnames[0]] = Reference("<instance>", self.in_class) |
paul@47 | 626 | else: |
paul@47 | 627 | locals[argnames[0]] = Reference("<var>") |
paul@47 | 628 | |
paul@47 | 629 | for argname in argnames[1:]: |
paul@0 | 630 | locals[argname] = Reference("<var>") |
paul@0 | 631 | |
paul@0 | 632 | globals = self.scope_globals[function_name] = set() |
paul@0 | 633 | |
paul@0 | 634 | # Process the defaults. |
paul@0 | 635 | |
paul@0 | 636 | defaults = self.importer.function_defaults[function_name] = \ |
paul@0 | 637 | self.function_defaults[function_name] = [] |
paul@0 | 638 | |
paul@0 | 639 | for argname, default in compiler.ast.get_defaults(n): |
paul@0 | 640 | if default: |
paul@0 | 641 | |
paul@0 | 642 | # Obtain any reference for the default. |
paul@0 | 643 | |
paul@0 | 644 | name_ref = self.process_structure_node(default) |
paul@0 | 645 | defaults.append((argname, name_ref.is_name() and name_ref.reference() or Reference("<var>"))) |
paul@0 | 646 | |
paul@0 | 647 | # Reset conditional tracking to focus on the function contents. |
paul@0 | 648 | |
paul@0 | 649 | in_conditional = self.in_conditional |
paul@0 | 650 | self.in_conditional = False |
paul@0 | 651 | |
paul@0 | 652 | in_function = self.in_function |
paul@0 | 653 | self.in_function = function_name |
paul@0 | 654 | |
paul@0 | 655 | self.enter_namespace(name) |
paul@0 | 656 | |
paul@251 | 657 | # Define a name attribute value for the function instance. |
paul@251 | 658 | |
paul@251 | 659 | ref = self.get_builtin_class("string") |
paul@251 | 660 | self.reserve_constant(function_name, function_name, ref.get_origin()) |
paul@251 | 661 | |
paul@0 | 662 | # Track attribute usage within the namespace. |
paul@0 | 663 | |
paul@0 | 664 | path = self.get_namespace_path() |
paul@0 | 665 | |
paul@0 | 666 | self.start_tracking(locals) |
paul@0 | 667 | self.process_structure_node(n.code) |
paul@0 | 668 | self.stop_tracking() |
paul@0 | 669 | |
paul@1 | 670 | # Exit to the parent. |
paul@0 | 671 | |
paul@0 | 672 | self.exit_namespace() |
paul@0 | 673 | |
paul@0 | 674 | # Update flags. |
paul@0 | 675 | |
paul@0 | 676 | self.in_function = in_function |
paul@0 | 677 | self.in_conditional = in_conditional |
paul@0 | 678 | |
paul@0 | 679 | # Define the function using the appropriate name. |
paul@0 | 680 | |
paul@0 | 681 | self.set_definition(name, "<function>") |
paul@0 | 682 | |
paul@0 | 683 | # Where a function is set conditionally, assign the name. |
paul@0 | 684 | |
paul@0 | 685 | if original_name: |
paul@322 | 686 | self.process_assignment_for_object(original_name, compiler.ast.Name(name)) |
paul@0 | 687 | |
paul@0 | 688 | def process_global_node(self, n): |
paul@0 | 689 | |
paul@0 | 690 | """ |
paul@0 | 691 | Process the given "global" node 'n'. |
paul@0 | 692 | """ |
paul@0 | 693 | |
paul@0 | 694 | path = self.get_namespace_path() |
paul@0 | 695 | |
paul@0 | 696 | if path != self.name: |
paul@0 | 697 | self.scope_globals[path].update(n.names) |
paul@0 | 698 | |
paul@0 | 699 | def process_if_node(self, n): |
paul@0 | 700 | |
paul@0 | 701 | """ |
paul@0 | 702 | Process the given "if" node 'n'. |
paul@0 | 703 | """ |
paul@0 | 704 | |
paul@0 | 705 | tracker = self.trackers[-1] |
paul@0 | 706 | tracker.new_branchpoint() |
paul@0 | 707 | |
paul@0 | 708 | for test, body in n.tests: |
paul@0 | 709 | self.process_structure_node(test) |
paul@0 | 710 | |
paul@0 | 711 | tracker.new_branch() |
paul@0 | 712 | |
paul@0 | 713 | in_conditional = self.in_conditional |
paul@0 | 714 | self.in_conditional = True |
paul@0 | 715 | self.process_structure_node(body) |
paul@0 | 716 | self.in_conditional = in_conditional |
paul@0 | 717 | |
paul@0 | 718 | tracker.shelve_branch() |
paul@0 | 719 | |
paul@0 | 720 | # Maintain a branch for the else clause. |
paul@0 | 721 | |
paul@0 | 722 | tracker.new_branch() |
paul@0 | 723 | if n.else_: |
paul@0 | 724 | self.process_structure_node(n.else_) |
paul@0 | 725 | tracker.shelve_branch() |
paul@0 | 726 | |
paul@0 | 727 | tracker.merge_branches() |
paul@0 | 728 | |
paul@0 | 729 | def process_import_node(self, n): |
paul@0 | 730 | |
paul@0 | 731 | "Process the given import node 'n'." |
paul@0 | 732 | |
paul@0 | 733 | path = self.get_namespace_path() |
paul@0 | 734 | |
paul@0 | 735 | # Load the mentioned module. |
paul@0 | 736 | |
paul@0 | 737 | for name, alias in n.names: |
paul@12 | 738 | if name == self.name: |
paul@12 | 739 | raise InspectError("Cannot import the current module.", path, n) |
paul@0 | 740 | |
paul@13 | 741 | self.set_module(alias or name.split(".")[-1], name) |
paul@18 | 742 | self.queue_module(name, True) |
paul@0 | 743 | |
paul@0 | 744 | def process_invocation_node(self, n): |
paul@0 | 745 | |
paul@0 | 746 | "Process the given invocation node 'n'." |
paul@0 | 747 | |
paul@0 | 748 | path = self.get_namespace_path() |
paul@0 | 749 | |
paul@0 | 750 | self.allocate_arguments(path, n.args) |
paul@0 | 751 | |
paul@0 | 752 | try: |
paul@107 | 753 | # Communicate to the invocation target expression that it forms the |
paul@107 | 754 | # target of an invocation, potentially affecting attribute accesses. |
paul@0 | 755 | |
paul@88 | 756 | in_invocation = self.in_invocation |
paul@88 | 757 | self.in_invocation = True |
paul@107 | 758 | |
paul@107 | 759 | # Process the expression, obtaining any identified reference. |
paul@107 | 760 | |
paul@0 | 761 | name_ref = self.process_structure_node(n.node) |
paul@223 | 762 | self.in_invocation = False |
paul@0 | 763 | |
paul@0 | 764 | # Process the arguments. |
paul@0 | 765 | |
paul@0 | 766 | for arg in n.args: |
paul@0 | 767 | self.process_structure_node(arg) |
paul@0 | 768 | |
paul@223 | 769 | self.in_invocation = in_invocation |
paul@223 | 770 | |
paul@0 | 771 | # Detect class invocations. |
paul@0 | 772 | |
paul@0 | 773 | if isinstance(name_ref, ResolvedNameRef) and name_ref.has_kind("<class>"): |
paul@0 | 774 | return InstanceRef(name_ref.reference().instance_of()) |
paul@0 | 775 | |
paul@0 | 776 | elif isinstance(name_ref, NameRef): |
paul@0 | 777 | return InvocationRef(name_ref) |
paul@0 | 778 | |
paul@226 | 779 | # Provide a general reference to indicate that something is produced |
paul@226 | 780 | # by the invocation, useful for retaining assignment expression |
paul@226 | 781 | # details. |
paul@226 | 782 | |
paul@226 | 783 | return VariableRef() |
paul@0 | 784 | |
paul@0 | 785 | finally: |
paul@0 | 786 | self.deallocate_arguments(path, n.args) |
paul@0 | 787 | |
paul@0 | 788 | def process_lambda_node(self, n): |
paul@0 | 789 | |
paul@0 | 790 | "Process the given lambda node 'n'." |
paul@0 | 791 | |
paul@0 | 792 | name = self.get_lambda_name() |
paul@0 | 793 | self.process_function_node(n, name) |
paul@0 | 794 | |
paul@0 | 795 | origin = self.get_object_path(name) |
paul@210 | 796 | |
paul@210 | 797 | if self.function_defaults.get(origin): |
paul@210 | 798 | return None |
paul@210 | 799 | else: |
paul@210 | 800 | return ResolvedNameRef(name, Reference("<function>", origin)) |
paul@0 | 801 | |
paul@0 | 802 | def process_logical_node(self, n): |
paul@0 | 803 | |
paul@0 | 804 | "Process the given operator node 'n'." |
paul@0 | 805 | |
paul@0 | 806 | self.process_operator_chain(n.nodes, self.process_structure_node) |
paul@0 | 807 | |
paul@0 | 808 | def process_name_node(self, n): |
paul@0 | 809 | |
paul@0 | 810 | "Process the given name node 'n'." |
paul@0 | 811 | |
paul@0 | 812 | path = self.get_namespace_path() |
paul@0 | 813 | |
paul@420 | 814 | # Find predefined constant names before anything else. |
paul@420 | 815 | |
paul@420 | 816 | if n.name in predefined_constants: |
paul@420 | 817 | ref = self.get_builtin(n.name) |
paul@420 | 818 | value = ResolvedNameRef(n.name, ref) |
paul@420 | 819 | return value |
paul@420 | 820 | |
paul@173 | 821 | # Special names that have already been identified. |
paul@0 | 822 | |
paul@0 | 823 | if n.name.startswith("$"): |
paul@0 | 824 | value = self.get_special(n.name) |
paul@0 | 825 | if value: |
paul@0 | 826 | return value |
paul@0 | 827 | |
paul@0 | 828 | # Special case for operator functions introduced through code |
paul@0 | 829 | # transformations. |
paul@0 | 830 | |
paul@0 | 831 | if n.name.startswith("$op"): |
paul@0 | 832 | |
paul@0 | 833 | # Obtain the location of the actual function defined in the operator |
paul@0 | 834 | # package. |
paul@0 | 835 | |
paul@0 | 836 | op = n.name[len("$op"):] |
paul@0 | 837 | |
paul@0 | 838 | # Attempt to get a reference. |
paul@0 | 839 | |
paul@12 | 840 | ref = self.import_name_from_module(op, "operator") |
paul@0 | 841 | |
paul@0 | 842 | # Record the imported name and provide the resolved name reference. |
paul@0 | 843 | |
paul@0 | 844 | value = ResolvedNameRef(n.name, ref) |
paul@0 | 845 | self.set_special(n.name, value) |
paul@0 | 846 | return value |
paul@0 | 847 | |
paul@173 | 848 | # Special case for print operations. |
paul@173 | 849 | |
paul@173 | 850 | elif n.name.startswith("$print"): |
paul@173 | 851 | |
paul@173 | 852 | # Attempt to get a reference. |
paul@173 | 853 | |
paul@173 | 854 | ref = self.get_builtin("print_") |
paul@173 | 855 | |
paul@173 | 856 | # Record the imported name and provide the resolved name reference. |
paul@173 | 857 | |
paul@173 | 858 | value = ResolvedNameRef(n.name, ref) |
paul@173 | 859 | self.set_special(n.name, value) |
paul@173 | 860 | return value |
paul@173 | 861 | |
paul@60 | 862 | # Test for self usage, which is only allowed in methods. |
paul@60 | 863 | |
paul@60 | 864 | if n.name == "self" and not (self.in_function and self.in_class): |
paul@60 | 865 | raise InspectError("Use of self is only allowed in methods.", path, n) |
paul@60 | 866 | |
paul@0 | 867 | # Record usage of the name. |
paul@0 | 868 | |
paul@0 | 869 | self.record_name(n.name) |
paul@0 | 870 | |
paul@0 | 871 | # Search for unknown names in non-function scopes immediately. |
paul@0 | 872 | # External names in functions are resolved later. |
paul@0 | 873 | |
paul@0 | 874 | ref = self.find_name(n.name) |
paul@0 | 875 | if ref: |
paul@0 | 876 | return ResolvedNameRef(n.name, ref) |
paul@0 | 877 | |
paul@40 | 878 | # Explicitly-declared global names. |
paul@0 | 879 | |
paul@0 | 880 | elif self.in_function and n.name in self.scope_globals[path]: |
paul@0 | 881 | return NameRef(n.name) |
paul@0 | 882 | |
paul@0 | 883 | # Examine other names. |
paul@0 | 884 | |
paul@0 | 885 | else: |
paul@0 | 886 | tracker = self.trackers[-1] |
paul@0 | 887 | |
paul@0 | 888 | # Check local names. |
paul@0 | 889 | |
paul@0 | 890 | branches = tracker.tracking_name(n.name) |
paul@0 | 891 | |
paul@1 | 892 | # Local name. |
paul@0 | 893 | |
paul@0 | 894 | if branches: |
paul@0 | 895 | self.record_branches_for_access(branches, n.name, None) |
paul@117 | 896 | access_number = self.record_access_details(n.name, None, False, False) |
paul@0 | 897 | return LocalNameRef(n.name, access_number) |
paul@0 | 898 | |
paul@40 | 899 | # Possible global or built-in name. |
paul@0 | 900 | |
paul@0 | 901 | else: |
paul@0 | 902 | return NameRef(n.name) |
paul@0 | 903 | |
paul@0 | 904 | def process_operator_chain(self, nodes, fn): |
paul@0 | 905 | |
paul@0 | 906 | """ |
paul@0 | 907 | Process the given chain of 'nodes', applying 'fn' to each node or item. |
paul@0 | 908 | Each node starts a new conditional region, effectively making a deeply- |
paul@0 | 909 | nested collection of if-like statements. |
paul@0 | 910 | """ |
paul@0 | 911 | |
paul@0 | 912 | tracker = self.trackers[-1] |
paul@0 | 913 | |
paul@0 | 914 | for item in nodes: |
paul@0 | 915 | tracker.new_branchpoint() |
paul@0 | 916 | tracker.new_branch() |
paul@0 | 917 | fn(item) |
paul@0 | 918 | |
paul@0 | 919 | for item in nodes[:-1]: |
paul@0 | 920 | tracker.shelve_branch() |
paul@0 | 921 | tracker.new_branch() |
paul@0 | 922 | tracker.shelve_branch() |
paul@0 | 923 | tracker.merge_branches() |
paul@0 | 924 | |
paul@0 | 925 | tracker.shelve_branch() |
paul@0 | 926 | tracker.merge_branches() |
paul@0 | 927 | |
paul@0 | 928 | def process_try_node(self, n): |
paul@0 | 929 | |
paul@0 | 930 | """ |
paul@0 | 931 | Process the given "try...except" node 'n'. |
paul@0 | 932 | """ |
paul@0 | 933 | |
paul@0 | 934 | tracker = self.trackers[-1] |
paul@0 | 935 | tracker.new_branchpoint() |
paul@0 | 936 | |
paul@0 | 937 | self.process_structure_node(n.body) |
paul@0 | 938 | |
paul@0 | 939 | for name, var, handler in n.handlers: |
paul@0 | 940 | if name is not None: |
paul@0 | 941 | self.process_structure_node(name) |
paul@0 | 942 | |
paul@0 | 943 | # Any abandoned branches from the body can now be resumed in a new |
paul@0 | 944 | # branch. |
paul@0 | 945 | |
paul@0 | 946 | tracker.resume_abandoned_branches() |
paul@0 | 947 | |
paul@0 | 948 | # Establish the local for the handler. |
paul@0 | 949 | |
paul@0 | 950 | if var is not None: |
paul@261 | 951 | self.process_assignment_node(var, None) |
paul@0 | 952 | if handler is not None: |
paul@0 | 953 | self.process_structure_node(handler) |
paul@0 | 954 | |
paul@0 | 955 | tracker.shelve_branch() |
paul@0 | 956 | |
paul@0 | 957 | # The else clause maintains the usage from the body but without the |
paul@0 | 958 | # abandoned branches since they would never lead to the else clause |
paul@0 | 959 | # being executed. |
paul@0 | 960 | |
paul@0 | 961 | if n.else_: |
paul@0 | 962 | tracker.new_branch() |
paul@0 | 963 | self.process_structure_node(n.else_) |
paul@0 | 964 | tracker.shelve_branch() |
paul@0 | 965 | |
paul@0 | 966 | # Without an else clause, a null branch propagates the successful |
paul@0 | 967 | # outcome. |
paul@0 | 968 | |
paul@0 | 969 | else: |
paul@0 | 970 | tracker.new_branch() |
paul@0 | 971 | tracker.shelve_branch() |
paul@0 | 972 | |
paul@0 | 973 | tracker.merge_branches() |
paul@0 | 974 | |
paul@0 | 975 | def process_try_finally_node(self, n): |
paul@0 | 976 | |
paul@0 | 977 | """ |
paul@0 | 978 | Process the given "try...finally" node 'n'. |
paul@0 | 979 | """ |
paul@0 | 980 | |
paul@0 | 981 | tracker = self.trackers[-1] |
paul@0 | 982 | self.process_structure_node(n.body) |
paul@0 | 983 | |
paul@0 | 984 | # Any abandoned branches from the body can now be resumed. |
paul@0 | 985 | |
paul@0 | 986 | branches = tracker.resume_all_abandoned_branches() |
paul@0 | 987 | self.process_structure_node(n.final) |
paul@0 | 988 | |
paul@0 | 989 | # At the end of the finally clause, abandoned branches are discarded. |
paul@0 | 990 | |
paul@0 | 991 | tracker.restore_active_branches(branches) |
paul@0 | 992 | |
paul@0 | 993 | def process_while_node(self, n): |
paul@0 | 994 | |
paul@0 | 995 | "Process the given while node 'n'." |
paul@0 | 996 | |
paul@0 | 997 | tracker = self.trackers[-1] |
paul@0 | 998 | tracker.new_branchpoint(loop_node=True) |
paul@0 | 999 | |
paul@0 | 1000 | # Evaluate any test or iterator outside the loop. |
paul@0 | 1001 | |
paul@0 | 1002 | self.process_structure_node(n.test) |
paul@0 | 1003 | |
paul@0 | 1004 | # Propagate attribute usage to branches. |
paul@0 | 1005 | |
paul@0 | 1006 | tracker.new_branch(loop_node=True) |
paul@0 | 1007 | |
paul@0 | 1008 | # Enter the loop. |
paul@0 | 1009 | |
paul@0 | 1010 | in_conditional = self.in_conditional |
paul@0 | 1011 | self.in_conditional = True |
paul@0 | 1012 | self.process_structure_node(n.body) |
paul@0 | 1013 | self.in_conditional = in_conditional |
paul@0 | 1014 | |
paul@0 | 1015 | # Continuing branches are resumed before any test. |
paul@0 | 1016 | |
paul@0 | 1017 | tracker.resume_continuing_branches() |
paul@0 | 1018 | |
paul@0 | 1019 | # Evaluate any continuation test within the body. |
paul@0 | 1020 | |
paul@0 | 1021 | self.process_structure_node(n.test) |
paul@0 | 1022 | |
paul@0 | 1023 | tracker.shelve_branch(loop_node=True) |
paul@0 | 1024 | |
paul@0 | 1025 | # Support the non-looping condition. |
paul@0 | 1026 | |
paul@0 | 1027 | tracker.new_branch() |
paul@0 | 1028 | tracker.shelve_branch() |
paul@0 | 1029 | |
paul@0 | 1030 | tracker.merge_branches() |
paul@0 | 1031 | |
paul@0 | 1032 | # Evaluate any else clause outside branches. |
paul@0 | 1033 | |
paul@0 | 1034 | if n.else_: |
paul@0 | 1035 | self.process_structure_node(n.else_) |
paul@0 | 1036 | |
paul@0 | 1037 | # Connect broken branches to the code after any loop. |
paul@0 | 1038 | |
paul@0 | 1039 | tracker.resume_broken_branches() |
paul@0 | 1040 | |
paul@0 | 1041 | # Branch tracking methods. |
paul@0 | 1042 | |
paul@0 | 1043 | def start_tracking(self, names): |
paul@0 | 1044 | |
paul@0 | 1045 | """ |
paul@0 | 1046 | Start tracking attribute usage for names in the current namespace, |
paul@0 | 1047 | immediately registering the given 'names'. |
paul@0 | 1048 | """ |
paul@0 | 1049 | |
paul@0 | 1050 | path = self.get_namespace_path() |
paul@0 | 1051 | parent = self.trackers[-1] |
paul@0 | 1052 | tracker = BranchTracker() |
paul@0 | 1053 | self.trackers.append(tracker) |
paul@0 | 1054 | |
paul@0 | 1055 | # Record the given names established as new branches. |
paul@0 | 1056 | |
paul@0 | 1057 | tracker.assign_names(names) |
paul@0 | 1058 | |
paul@0 | 1059 | def assign_name(self, name, name_ref): |
paul@0 | 1060 | |
paul@0 | 1061 | "Assign to 'name' the given 'name_ref' in the current namespace." |
paul@0 | 1062 | |
paul@0 | 1063 | name = self.get_name_for_tracking(name) |
paul@0 | 1064 | self.trackers[-1].assign_names([name], [name_ref]) |
paul@0 | 1065 | |
paul@0 | 1066 | def stop_tracking(self): |
paul@0 | 1067 | |
paul@0 | 1068 | """ |
paul@0 | 1069 | Stop tracking attribute usage, recording computed usage for the current |
paul@0 | 1070 | namespace. |
paul@0 | 1071 | """ |
paul@0 | 1072 | |
paul@0 | 1073 | path = self.get_namespace_path() |
paul@0 | 1074 | tracker = self.trackers.pop() |
paul@0 | 1075 | self.record_assignments_for_access(tracker) |
paul@0 | 1076 | |
paul@0 | 1077 | self.attr_usage[path] = tracker.get_all_usage() |
paul@0 | 1078 | self.name_initialisers[path] = tracker.get_all_values() |
paul@0 | 1079 | |
paul@0 | 1080 | def start_tracking_in_module(self): |
paul@0 | 1081 | |
paul@0 | 1082 | "Start tracking attribute usage in the module." |
paul@0 | 1083 | |
paul@0 | 1084 | tracker = BranchTracker() |
paul@0 | 1085 | self.trackers.append(tracker) |
paul@0 | 1086 | |
paul@0 | 1087 | def stop_tracking_in_module(self): |
paul@0 | 1088 | |
paul@0 | 1089 | "Stop tracking attribute usage in the module." |
paul@0 | 1090 | |
paul@0 | 1091 | tracker = self.trackers[0] |
paul@0 | 1092 | self.record_assignments_for_access(tracker) |
paul@0 | 1093 | self.attr_usage[self.name] = tracker.get_all_usage() |
paul@0 | 1094 | self.name_initialisers[self.name] = tracker.get_all_values() |
paul@0 | 1095 | |
paul@0 | 1096 | def record_assignments_for_access(self, tracker): |
paul@0 | 1097 | |
paul@0 | 1098 | """ |
paul@0 | 1099 | For the current path, use the given 'tracker' to record assignment |
paul@0 | 1100 | version information for attribute accesses. |
paul@0 | 1101 | """ |
paul@0 | 1102 | |
paul@0 | 1103 | path = self.get_path_for_access() |
paul@0 | 1104 | |
paul@0 | 1105 | if not self.attr_accessor_branches.has_key(path): |
paul@0 | 1106 | return |
paul@0 | 1107 | |
paul@0 | 1108 | init_item(self.attr_accessors, path, dict) |
paul@0 | 1109 | attr_accessors = self.attr_accessors[path] |
paul@0 | 1110 | |
paul@0 | 1111 | # Obtain the branches applying during each access. |
paul@0 | 1112 | |
paul@0 | 1113 | for access, all_branches in self.attr_accessor_branches[path].items(): |
paul@0 | 1114 | name, attrnames = access |
paul@0 | 1115 | init_item(attr_accessors, access, list) |
paul@0 | 1116 | |
paul@0 | 1117 | # Obtain the assignments applying to each branch. |
paul@0 | 1118 | |
paul@0 | 1119 | for branches in all_branches: |
paul@0 | 1120 | positions = tracker.get_assignment_positions_for_branches(name, branches) |
paul@0 | 1121 | |
paul@0 | 1122 | # Detect missing name information. |
paul@0 | 1123 | |
paul@0 | 1124 | if None in positions: |
paul@0 | 1125 | globals = self.global_attr_accesses.get(path) |
paul@0 | 1126 | accesses = globals and globals.get(name) |
paul@0 | 1127 | if not accesses: |
paul@0 | 1128 | print >>sys.stderr, "In %s, %s may not be defined when used." % ( |
paul@0 | 1129 | self.get_namespace_path(), name) |
paul@0 | 1130 | positions.remove(None) |
paul@0 | 1131 | |
paul@0 | 1132 | attr_accessors[access].append(positions) |
paul@0 | 1133 | |
paul@0 | 1134 | def record_branches_for_access(self, branches, name, attrnames): |
paul@0 | 1135 | |
paul@0 | 1136 | """ |
paul@0 | 1137 | Record the given 'branches' for an access involving the given 'name' and |
paul@0 | 1138 | 'attrnames'. |
paul@0 | 1139 | """ |
paul@0 | 1140 | |
paul@0 | 1141 | access = name, attrnames |
paul@0 | 1142 | path = self.get_path_for_access() |
paul@0 | 1143 | |
paul@0 | 1144 | init_item(self.attr_accessor_branches, path, dict) |
paul@0 | 1145 | attr_accessor_branches = self.attr_accessor_branches[path] |
paul@0 | 1146 | |
paul@0 | 1147 | init_item(attr_accessor_branches, access, list) |
paul@0 | 1148 | attr_accessor_branches[access].append(branches) |
paul@0 | 1149 | |
paul@117 | 1150 | def record_access_details(self, name, attrnames, assignment, invocation): |
paul@0 | 1151 | |
paul@0 | 1152 | """ |
paul@0 | 1153 | For the given 'name' and 'attrnames', record an access indicating |
paul@0 | 1154 | whether 'assignment' is occurring. |
paul@0 | 1155 | |
paul@0 | 1156 | These details correspond to accesses otherwise recorded by the attribute |
paul@0 | 1157 | accessor and attribute access dictionaries. |
paul@0 | 1158 | """ |
paul@0 | 1159 | |
paul@0 | 1160 | access = name, attrnames |
paul@0 | 1161 | path = self.get_path_for_access() |
paul@0 | 1162 | |
paul@0 | 1163 | init_item(self.attr_access_modifiers, path, dict) |
paul@0 | 1164 | init_item(self.attr_access_modifiers[path], access, list) |
paul@0 | 1165 | |
paul@0 | 1166 | access_number = len(self.attr_access_modifiers[path][access]) |
paul@117 | 1167 | self.attr_access_modifiers[path][access].append((assignment, invocation)) |
paul@0 | 1168 | return access_number |
paul@0 | 1169 | |
paul@0 | 1170 | def record_global_access_details(self, name, attrnames): |
paul@0 | 1171 | |
paul@0 | 1172 | """ |
paul@0 | 1173 | Record details of a global access via the given 'name' involving the |
paul@0 | 1174 | indicated 'attrnames'. |
paul@0 | 1175 | """ |
paul@0 | 1176 | |
paul@0 | 1177 | path = self.get_namespace_path() |
paul@0 | 1178 | |
paul@0 | 1179 | init_item(self.global_attr_accesses, path, dict) |
paul@0 | 1180 | init_item(self.global_attr_accesses[path], name, set) |
paul@0 | 1181 | self.global_attr_accesses[path][name].add(attrnames) |
paul@0 | 1182 | |
paul@0 | 1183 | # Namespace modification. |
paul@0 | 1184 | |
paul@0 | 1185 | def record_name(self, name): |
paul@0 | 1186 | |
paul@0 | 1187 | "Record the use of 'name' in a namespace." |
paul@0 | 1188 | |
paul@0 | 1189 | path = self.get_namespace_path() |
paul@0 | 1190 | init_item(self.names_used, path, set) |
paul@0 | 1191 | self.names_used[path].add(name) |
paul@0 | 1192 | |
paul@12 | 1193 | def set_module(self, name, module_name): |
paul@0 | 1194 | |
paul@0 | 1195 | """ |
paul@12 | 1196 | Set a module in the current namespace using the given 'name' associated |
paul@12 | 1197 | with the corresponding 'module_name'. |
paul@0 | 1198 | """ |
paul@0 | 1199 | |
paul@0 | 1200 | if name: |
paul@12 | 1201 | self.set_general_local(name, Reference("<module>", module_name)) |
paul@0 | 1202 | |
paul@0 | 1203 | def set_definition(self, name, kind): |
paul@0 | 1204 | |
paul@0 | 1205 | """ |
paul@0 | 1206 | Set the definition having the given 'name' and 'kind'. |
paul@0 | 1207 | |
paul@0 | 1208 | Definitions are set in the static namespace hierarchy, but they can also |
paul@0 | 1209 | be recorded for function locals. |
paul@0 | 1210 | """ |
paul@0 | 1211 | |
paul@0 | 1212 | if self.is_global(name): |
paul@0 | 1213 | print >>sys.stderr, "In %s, %s is defined as being global." % ( |
paul@0 | 1214 | self.get_namespace_path(), name) |
paul@0 | 1215 | |
paul@0 | 1216 | path = self.get_object_path(name) |
paul@0 | 1217 | self.set_object(path, kind) |
paul@0 | 1218 | |
paul@0 | 1219 | ref = self.get_object(path) |
paul@0 | 1220 | if ref.get_kind() == "<var>": |
paul@0 | 1221 | print >>sys.stderr, "In %s, %s is defined more than once." % ( |
paul@0 | 1222 | self.get_namespace_path(), name) |
paul@0 | 1223 | |
paul@0 | 1224 | if not self.is_global(name) and self.in_function: |
paul@0 | 1225 | self.set_function_local(name, ref) |
paul@0 | 1226 | |
paul@0 | 1227 | def set_function_local(self, name, ref=None): |
paul@0 | 1228 | |
paul@0 | 1229 | "Set the local with the given 'name' and optional 'ref'." |
paul@0 | 1230 | |
paul@0 | 1231 | locals = self.function_locals[self.get_namespace_path()] |
paul@0 | 1232 | multiple = not ref or locals.has_key(name) and locals[name] != ref |
paul@0 | 1233 | locals[name] = multiple and Reference("<var>") or ref |
paul@0 | 1234 | |
paul@0 | 1235 | def assign_general_local(self, name, name_ref): |
paul@0 | 1236 | |
paul@0 | 1237 | """ |
paul@0 | 1238 | Set for 'name' the given 'name_ref', recording the name for attribute |
paul@0 | 1239 | usage tracking. |
paul@0 | 1240 | """ |
paul@0 | 1241 | |
paul@0 | 1242 | self.set_general_local(name, name_ref) |
paul@0 | 1243 | self.assign_name(name, name_ref) |
paul@0 | 1244 | |
paul@0 | 1245 | def set_general_local(self, name, value=None): |
paul@0 | 1246 | |
paul@0 | 1247 | """ |
paul@0 | 1248 | Set the 'name' with optional 'value' in any kind of local namespace, |
paul@0 | 1249 | where the 'value' should be a reference if specified. |
paul@0 | 1250 | """ |
paul@0 | 1251 | |
paul@0 | 1252 | init_value = self.get_initialising_value(value) |
paul@0 | 1253 | |
paul@0 | 1254 | # Module global names. |
paul@0 | 1255 | |
paul@0 | 1256 | if self.is_global(name): |
paul@0 | 1257 | path = self.get_global_path(name) |
paul@0 | 1258 | self.set_object(path, init_value) |
paul@0 | 1259 | |
paul@0 | 1260 | # Function local names. |
paul@0 | 1261 | |
paul@0 | 1262 | elif self.in_function: |
paul@0 | 1263 | path = self.get_object_path(name) |
paul@0 | 1264 | self.set_function_local(name, init_value) |
paul@0 | 1265 | |
paul@0 | 1266 | # Other namespaces (classes). |
paul@0 | 1267 | |
paul@0 | 1268 | else: |
paul@0 | 1269 | path = self.get_object_path(name) |
paul@0 | 1270 | self.set_name(name, init_value) |
paul@0 | 1271 | |
paul@0 | 1272 | def set_name(self, name, ref=None): |
paul@0 | 1273 | |
paul@0 | 1274 | "Attach the 'name' with optional 'ref' to the current namespace." |
paul@0 | 1275 | |
paul@0 | 1276 | self.set_object(self.get_object_path(name), ref) |
paul@0 | 1277 | |
paul@0 | 1278 | def set_instance_attr(self, name, ref=None): |
paul@0 | 1279 | |
paul@0 | 1280 | """ |
paul@0 | 1281 | Add an instance attribute of the given 'name' to the current class, |
paul@0 | 1282 | using the optional 'ref'. |
paul@0 | 1283 | """ |
paul@0 | 1284 | |
paul@251 | 1285 | self._set_instance_attr(self.in_class, name, ref) |
paul@251 | 1286 | |
paul@251 | 1287 | def _set_instance_attr(self, path, name, ref=None): |
paul@251 | 1288 | |
paul@251 | 1289 | init_item(self.instance_attrs, path, set) |
paul@251 | 1290 | self.instance_attrs[path].add(name) |
paul@0 | 1291 | |
paul@0 | 1292 | if ref: |
paul@251 | 1293 | init_item(self.instance_attr_constants, path, dict) |
paul@251 | 1294 | self.instance_attr_constants[path][name] = ref |
paul@0 | 1295 | |
paul@0 | 1296 | def get_initialising_value(self, value): |
paul@0 | 1297 | |
paul@0 | 1298 | "Return a suitable initialiser reference for 'value'." |
paul@0 | 1299 | |
paul@25 | 1300 | # Includes LiteralSequenceRef, ResolvedNameRef... |
paul@25 | 1301 | |
paul@25 | 1302 | if isinstance(value, (NameRef, AccessRef, InstanceRef)): |
paul@0 | 1303 | return value.reference() |
paul@0 | 1304 | |
paul@0 | 1305 | # In general, invocations do not produce known results. However, the |
paul@0 | 1306 | # name initialisers are resolved once a module has been inspected. |
paul@0 | 1307 | |
paul@0 | 1308 | elif isinstance(value, InvocationRef): |
paul@27 | 1309 | return value.reference() |
paul@0 | 1310 | |
paul@229 | 1311 | # Variable references are unknown results. |
paul@229 | 1312 | |
paul@229 | 1313 | elif isinstance(value, VariableRef): |
paul@229 | 1314 | return value.reference() |
paul@229 | 1315 | |
paul@0 | 1316 | else: |
paul@0 | 1317 | return value |
paul@0 | 1318 | |
paul@0 | 1319 | # Static, program-relative naming. |
paul@0 | 1320 | |
paul@0 | 1321 | def find_name(self, name): |
paul@0 | 1322 | |
paul@0 | 1323 | """ |
paul@0 | 1324 | Return the qualified name for the given 'name' used in the current |
paul@0 | 1325 | non-function namespace. |
paul@0 | 1326 | """ |
paul@0 | 1327 | |
paul@0 | 1328 | path = self.get_namespace_path() |
paul@0 | 1329 | ref = None |
paul@0 | 1330 | |
paul@0 | 1331 | if not self.in_function and name not in predefined_constants: |
paul@0 | 1332 | if self.in_class: |
paul@152 | 1333 | ref = self.get_object(self.get_object_path(name), False) |
paul@0 | 1334 | if not ref: |
paul@0 | 1335 | ref = self.get_global_or_builtin(name) |
paul@0 | 1336 | |
paul@0 | 1337 | return ref |
paul@0 | 1338 | |
paul@0 | 1339 | def get_class(self, node): |
paul@0 | 1340 | |
paul@0 | 1341 | """ |
paul@0 | 1342 | Use the given 'node' to obtain the identity of a class. Return a |
paul@0 | 1343 | reference for the class. Unresolved dependencies are permitted and must |
paul@0 | 1344 | be resolved later. |
paul@0 | 1345 | """ |
paul@0 | 1346 | |
paul@0 | 1347 | ref = self._get_class(node) |
paul@0 | 1348 | return ref.has_kind(["<class>", "<depends>"]) and ref or None |
paul@0 | 1349 | |
paul@0 | 1350 | def _get_class(self, node): |
paul@0 | 1351 | |
paul@0 | 1352 | """ |
paul@0 | 1353 | Use the given 'node' to find a class definition. Return a reference to |
paul@0 | 1354 | the class. |
paul@0 | 1355 | """ |
paul@0 | 1356 | |
paul@0 | 1357 | if isinstance(node, compiler.ast.Getattr): |
paul@0 | 1358 | |
paul@0 | 1359 | # Obtain the identity of the access target. |
paul@0 | 1360 | |
paul@0 | 1361 | ref = self._get_class(node.expr) |
paul@0 | 1362 | |
paul@0 | 1363 | # Where the target is a class or module, obtain the identity of the |
paul@0 | 1364 | # attribute. |
paul@0 | 1365 | |
paul@0 | 1366 | if ref.has_kind(["<function>", "<var>"]): |
paul@0 | 1367 | return None |
paul@0 | 1368 | else: |
paul@0 | 1369 | attrname = "%s.%s" % (ref.get_origin(), node.attrname) |
paul@0 | 1370 | return self.get_object(attrname) |
paul@0 | 1371 | |
paul@0 | 1372 | # Names can be module-level or built-in. |
paul@0 | 1373 | |
paul@0 | 1374 | elif isinstance(node, compiler.ast.Name): |
paul@0 | 1375 | |
paul@0 | 1376 | # Record usage of the name and attempt to identify it. |
paul@0 | 1377 | |
paul@0 | 1378 | self.record_name(node.name) |
paul@73 | 1379 | return self.find_name(node.name) |
paul@0 | 1380 | else: |
paul@0 | 1381 | return None |
paul@0 | 1382 | |
paul@0 | 1383 | def get_constant(self, name, value): |
paul@0 | 1384 | |
paul@0 | 1385 | "Return a constant reference for the given type 'name' and 'value'." |
paul@0 | 1386 | |
paul@12 | 1387 | ref = self.get_builtin_class(name) |
paul@0 | 1388 | return self.get_constant_reference(ref, value) |
paul@0 | 1389 | |
paul@405 | 1390 | def get_literal_instance(self, n, name=None): |
paul@0 | 1391 | |
paul@405 | 1392 | """ |
paul@405 | 1393 | For node 'n', return a reference to an instance of 'name', or if 'name' |
paul@405 | 1394 | is not specified, deduce the type from the value. |
paul@405 | 1395 | """ |
paul@0 | 1396 | |
paul@366 | 1397 | # Handle stray None constants (Sliceobj seems to produce them). |
paul@366 | 1398 | |
paul@366 | 1399 | if name == "NoneType": |
paul@366 | 1400 | return self.process_name_node(compiler.ast.Name("None")) |
paul@366 | 1401 | |
paul@0 | 1402 | # Obtain the details of the literal itself. |
paul@0 | 1403 | # An alias to the type is generated for sequences. |
paul@0 | 1404 | |
paul@0 | 1405 | if name in ("dict", "list", "tuple"): |
paul@405 | 1406 | ref = self.get_builtin_class(name) |
paul@0 | 1407 | self.set_special_literal(name, ref) |
paul@0 | 1408 | return self.process_literal_sequence_node(n, name, ref, LiteralSequenceRef) |
paul@0 | 1409 | |
paul@0 | 1410 | # Constant values are independently recorded. |
paul@0 | 1411 | |
paul@0 | 1412 | else: |
paul@406 | 1413 | value, typename, encoding = self.get_constant_value(n.value, n.literal) |
paul@405 | 1414 | name = get_builtin_type(typename) |
paul@405 | 1415 | ref = self.get_builtin_class(name) |
paul@406 | 1416 | return self.get_constant_reference(ref, value, encoding) |
paul@0 | 1417 | |
paul@17 | 1418 | # Special names. |
paul@0 | 1419 | |
paul@17 | 1420 | def get_special(self, name): |
paul@0 | 1421 | |
paul@17 | 1422 | "Return any stored value for the given special 'name'." |
paul@0 | 1423 | |
paul@423 | 1424 | value = self.special.get(name) |
paul@423 | 1425 | if value: |
paul@423 | 1426 | ref, paths = value |
paul@423 | 1427 | else: |
paul@423 | 1428 | ref = None |
paul@423 | 1429 | return ref |
paul@17 | 1430 | |
paul@17 | 1431 | def set_special(self, name, value): |
paul@0 | 1432 | |
paul@17 | 1433 | """ |
paul@17 | 1434 | Set a special 'name' that merely tracks the use of an implicit object |
paul@17 | 1435 | 'value'. |
paul@17 | 1436 | """ |
paul@0 | 1437 | |
paul@423 | 1438 | if not self.special.has_key(name): |
paul@423 | 1439 | paths = set() |
paul@423 | 1440 | self.special[name] = value, paths |
paul@423 | 1441 | else: |
paul@423 | 1442 | _ref, paths = self.special[name] |
paul@423 | 1443 | |
paul@423 | 1444 | paths.add(self.get_namespace_path()) |
paul@17 | 1445 | |
paul@17 | 1446 | def set_special_literal(self, name, ref): |
paul@0 | 1447 | |
paul@17 | 1448 | """ |
paul@17 | 1449 | Set a special name for the literal type 'name' having type 'ref'. Such |
paul@17 | 1450 | special names provide a way of referring to literal object types. |
paul@17 | 1451 | """ |
paul@0 | 1452 | |
paul@17 | 1453 | literal_name = "$L%s" % name |
paul@17 | 1454 | value = ResolvedNameRef(literal_name, ref) |
paul@17 | 1455 | self.set_special(literal_name, value) |
paul@0 | 1456 | |
paul@0 | 1457 | # Functions and invocations. |
paul@0 | 1458 | |
paul@36 | 1459 | def set_invocation_usage(self): |
paul@36 | 1460 | |
paul@36 | 1461 | """ |
paul@36 | 1462 | Discard the current invocation storage figures, retaining the maximum |
paul@36 | 1463 | values. |
paul@36 | 1464 | """ |
paul@36 | 1465 | |
paul@36 | 1466 | for path, (current, maximum) in self.function_targets.items(): |
paul@36 | 1467 | self.importer.function_targets[path] = self.function_targets[path] = maximum |
paul@36 | 1468 | |
paul@36 | 1469 | for path, (current, maximum) in self.function_arguments.items(): |
paul@36 | 1470 | self.importer.function_arguments[path] = self.function_arguments[path] = maximum |
paul@36 | 1471 | |
paul@0 | 1472 | def allocate_arguments(self, path, args): |
paul@0 | 1473 | |
paul@0 | 1474 | """ |
paul@0 | 1475 | Allocate temporary argument storage using current and maximum |
paul@0 | 1476 | requirements for the given 'path' and 'args'. |
paul@0 | 1477 | """ |
paul@0 | 1478 | |
paul@192 | 1479 | # Class and module initialisation is ultimately combined. |
paul@192 | 1480 | |
paul@192 | 1481 | if not self.in_function: |
paul@192 | 1482 | path = self.name |
paul@192 | 1483 | |
paul@0 | 1484 | init_item(self.function_targets, path, lambda: [0, 0]) |
paul@0 | 1485 | t = self.function_targets[path] |
paul@0 | 1486 | t[0] += 1 |
paul@0 | 1487 | t[1] = max(t[0], t[1]) |
paul@0 | 1488 | |
paul@0 | 1489 | init_item(self.function_arguments, path, lambda: [0, 0]) |
paul@0 | 1490 | t = self.function_arguments[path] |
paul@0 | 1491 | t[0] += len(args) + 1 |
paul@0 | 1492 | t[1] = max(t[0], t[1]) |
paul@0 | 1493 | |
paul@0 | 1494 | def deallocate_arguments(self, path, args): |
paul@0 | 1495 | |
paul@0 | 1496 | "Deallocate temporary argument storage for the given 'path' and 'args'." |
paul@0 | 1497 | |
paul@192 | 1498 | # Class and module initialisation is ultimately combined. |
paul@192 | 1499 | |
paul@192 | 1500 | if not self.in_function: |
paul@192 | 1501 | path = self.name |
paul@192 | 1502 | |
paul@0 | 1503 | self.function_targets[path][0] -= 1 |
paul@0 | 1504 | self.function_arguments[path][0] -= len(args) + 1 |
paul@0 | 1505 | |
paul@0 | 1506 | # vim: tabstop=4 expandtab shiftwidth=4 |