1 #!/usr/bin/env python 2 3 """ 4 Translate programs. 5 6 Copyright (C) 2015, 2016, 2017, 2018, 2023 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from common import AccessLocation, CommonModule, CommonOutput, Location, \ 23 first, get_builtin_class, init_item, is_newer, \ 24 predefined_constants 25 from encoders import encode_access_instruction, encode_access_instruction_arg, \ 26 encode_function_pointer, encode_literal_instantiator, \ 27 encode_instantiator_pointer, encode_path, encode_symbol, \ 28 encode_type_attribute, is_type_attribute, \ 29 type_ops, typename_ops 30 from errors import InspectError, TranslateError 31 from os.path import exists, join 32 from os import makedirs 33 from referencing import Reference, combine_types 34 from results import Result 35 from transresults import TrConstantValueRef, TrInstanceRef, \ 36 TrLiteralSequenceRef, TrResolvedNameRef, \ 37 AliasResult, AttrResult, Expression, InstantiationResult, \ 38 InvocationResult, LogicalOperationResult, \ 39 LogicalResult, NegationResult, PredefinedConstantRef, \ 40 ReturnRef 41 from StringIO import StringIO 42 import compiler 43 import sys 44 45 class Translator(CommonOutput): 46 47 "A program translator." 48 49 def __init__(self, importer, deducer, optimiser, output): 50 self.importer = importer 51 self.deducer = deducer 52 self.optimiser = optimiser 53 self.output = output 54 55 def to_output(self, reset=False, debug=False, gc_sections=False): 56 57 "Write a program to the configured output directory." 58 59 # Make a directory for the final sources. 60 61 output = join(self.output, "src") 62 63 if not exists(output): 64 makedirs(output) 65 66 # Clean the output directory of irrelevant data. 67 68 self.check_output("debug=%r gc_sections=%r" % (debug, gc_sections)) 69 70 for module in self.importer.modules.values(): 71 output_filename = join(output, "%s.c" % module.name) 72 73 # Do not generate modules in the native package. They are provided 74 # by native functionality source files. 75 76 parts = module.name.split(".") 77 78 if parts[0] != "native" and \ 79 (reset or is_newer(module.filename, output_filename)): 80 81 tm = TranslatedModule(module.name, self.importer, self.deducer, self.optimiser) 82 tm.translate(module.filename, output_filename) 83 84 85 86 def make_expression(expr): 87 88 "Make a new expression from the existing 'expr'." 89 90 if isinstance(expr, Result): 91 return expr 92 else: 93 return Expression(str(expr)) 94 95 96 97 # The actual translation process itself. 98 99 class TranslatedModule(CommonModule): 100 101 "A module translator." 102 103 def __init__(self, name, importer, deducer, optimiser): 104 CommonModule.__init__(self, name, importer) 105 self.deducer = deducer 106 self.optimiser = optimiser 107 108 # Output stream. 109 110 self.out_toplevel = self.out = None 111 self.indent = 0 112 self.tabstop = " " 113 114 # Recorded namespaces. 115 116 self.namespaces = [] 117 self.in_conditional = False 118 self.in_parameter_list = False 119 120 # Exception raising adjustments. 121 122 self.in_try_finally = False 123 self.in_try_except = False 124 125 # Attribute access and accessor counting. 126 127 self.attr_accesses = {} 128 self.attr_accessors = {} 129 130 # Special variable usage. 131 132 self.temp_usage = {} 133 134 # Initialise some data used for attribute access generation. 135 136 self.init_substitutions() 137 138 def __repr__(self): 139 return "TranslatedModule(%r, %r)" % (self.name, self.importer) 140 141 def translate(self, filename, output_filename): 142 143 """ 144 Parse the file having the given 'filename', writing the translation to 145 the given 'output_filename'. 146 """ 147 148 self.parse_file(filename) 149 150 # Collect function namespaces for separate processing. 151 152 self.record_namespaces(self.astnode) 153 154 # Reset the lambda naming (in order to obtain the same names again) and 155 # translate the program. 156 157 self.reset_lambdas() 158 159 self.out_toplevel = self.out = open(output_filename, "w") 160 try: 161 self.start_output() 162 163 # Process namespaces, writing the translation. 164 165 for path, node in self.namespaces: 166 self.process_namespace(path, node) 167 168 # Process the module namespace including class namespaces. 169 170 self.process_namespace([], self.astnode) 171 172 finally: 173 self.out.close() 174 175 def have_object(self): 176 177 "Return whether a namespace is a recorded object." 178 179 return self.importer.objects.get(self.get_namespace_path()) 180 181 def get_builtin_class(self, name): 182 183 "Return a reference to the actual object providing 'name'." 184 185 return self.importer.get_object(get_builtin_class(name)) 186 187 def is_method(self, path): 188 189 "Return whether 'path' is a method." 190 191 class_name, method_name = path.rsplit(".", 1) 192 return self.importer.classes.has_key(class_name) and class_name or None 193 194 def in_method(self): 195 196 "Return whether the current namespace provides a method." 197 198 return self.in_function and self.is_method(self.get_namespace_path()) 199 200 # Namespace recording. 201 202 def record_namespaces(self, node): 203 204 "Process the program structure 'node', recording namespaces." 205 206 for n in node.getChildNodes(): 207 self.record_namespaces_in_node(n) 208 209 def record_namespaces_in_node(self, node): 210 211 "Process the program structure 'node', recording namespaces." 212 213 # Function namespaces within modules, classes and other functions. 214 # Functions appearing within conditional statements are given arbitrary 215 # names. 216 217 if isinstance(node, compiler.ast.Function): 218 self.record_function_node(node, (self.in_conditional or self.in_function) and self.get_lambda_name() or node.name) 219 220 elif isinstance(node, compiler.ast.Lambda): 221 self.record_function_node(node, self.get_lambda_name()) 222 223 # Classes are visited, but may be ignored if inside functions. 224 225 elif isinstance(node, compiler.ast.Class): 226 self.enter_namespace(node.name) 227 if self.have_object(): 228 self.record_namespaces(node) 229 self.exit_namespace() 230 231 # Conditional nodes are tracked so that function definitions may be 232 # handled. Since "for" loops are converted to "while" loops, they are 233 # included here. 234 235 elif isinstance(node, (compiler.ast.For, compiler.ast.If, compiler.ast.While)): 236 in_conditional = self.in_conditional 237 self.in_conditional = True 238 self.record_namespaces(node) 239 self.in_conditional = in_conditional 240 241 # All other nodes are processed depth-first. 242 243 else: 244 self.record_namespaces(node) 245 246 def record_function_node(self, n, name): 247 248 """ 249 Record the given function, lambda, if expression or list comprehension 250 node 'n' with the given 'name'. 251 """ 252 253 self.in_function = True 254 self.enter_namespace(name) 255 256 if self.have_object(): 257 258 # Record the namespace path and the node itself. 259 260 self.namespaces.append((self.namespace_path[:], n)) 261 self.record_namespaces_in_node(n.code) 262 263 self.exit_namespace() 264 self.in_function = False 265 266 # Constant referencing. 267 268 def get_literal_instance(self, n, name=None): 269 270 """ 271 For node 'n', return a reference for the type of the given 'name', or if 272 'name' is not specified, deduce the type from the value. 273 """ 274 275 # Handle stray None constants (Sliceobj seems to produce them). 276 277 if name is None and n.value is None: 278 return self.process_name_node(compiler.ast.Name("None")) 279 280 if name in ("dict", "list", "tuple"): 281 ref = self.get_builtin_class(name) 282 return self.process_literal_sequence_node(n, name, ref, TrLiteralSequenceRef) 283 else: 284 value, typename, encoding = self.get_constant_value(n.value, n.literals) 285 ref = self.get_builtin_class(typename) 286 value_type = ref.get_origin() 287 288 path = self.get_namespace_path() 289 290 # Obtain the local numbering of the constant and thus the 291 # locally-qualified name. 292 293 local_number = self.importer.all_constants[path][(value, value_type, encoding)] 294 constant_name = "$c%d" % local_number 295 objpath = self.get_object_path(constant_name) 296 297 # Obtain the unique identifier for the constant. 298 299 number = self.optimiser.constant_numbers[objpath] 300 return TrConstantValueRef(constant_name, ref.instance_of(), value, number) 301 302 # Namespace translation. 303 304 def process_namespace(self, path, node): 305 306 """ 307 Process the namespace for the given 'path' defined by the given 'node'. 308 """ 309 310 self.namespace_path = path 311 312 if isinstance(node, (compiler.ast.Function, compiler.ast.Lambda)): 313 self.in_function = True 314 self.process_function_body_node(node) 315 else: 316 self.in_function = False 317 self.reset_temp_limits() 318 319 self.start_module() 320 self.process_structure(node) 321 self.end_module() 322 323 def process_structure(self, node): 324 325 "Process the given 'node' or result." 326 327 # Handle processing requests on results. 328 329 if isinstance(node, Result): 330 return node 331 332 # Handle processing requests on nodes. 333 334 else: 335 l = CommonModule.process_structure(self, node) 336 337 # Return indications of return statement usage. 338 339 if l and isinstance(l[-1], ReturnRef): 340 return l[-1] 341 else: 342 return None 343 344 def process_statement_node(self, node, is_lambda=False): 345 346 "Process the given statement 'node'." 347 348 self.reset_temp_counters() 349 350 if is_lambda: 351 self.result_target_name = "__result" 352 353 return CommonModule.process_statement_node(self, node) 354 355 def process_structure_node(self, n): 356 357 "Process the individual node 'n'." 358 359 # Plain statements emit their expressions. 360 361 if isinstance(n, compiler.ast.Discard): 362 expr = self.process_structure_node(n.expr) 363 self.statement(expr) 364 365 # Module import declarations. 366 367 elif isinstance(n, compiler.ast.From): 368 self.process_from_node(n) 369 370 # Nodes using operator module functions. 371 372 elif isinstance(n, compiler.ast.Operator): 373 return self.process_operator_node(n) 374 375 elif isinstance(n, compiler.ast.AugAssign): 376 self.process_augassign_node(n) 377 378 elif isinstance(n, compiler.ast.Compare): 379 return self.process_compare_node(n) 380 381 elif isinstance(n, compiler.ast.Slice): 382 return self.process_slice_node(n) 383 384 elif isinstance(n, compiler.ast.Sliceobj): 385 return self.process_sliceobj_node(n) 386 387 elif isinstance(n, compiler.ast.Subscript): 388 return self.process_subscript_node(n) 389 390 # Classes are visited, but may be ignored if inside functions. 391 392 elif isinstance(n, compiler.ast.Class): 393 self.process_class_node(n) 394 395 # Functions within namespaces have any dynamic defaults initialised. 396 397 elif isinstance(n, compiler.ast.Function): 398 self.process_function_node(n) 399 400 # Lambdas are replaced with references to separately-generated 401 # functions. 402 403 elif isinstance(n, compiler.ast.Lambda): 404 return self.process_lambda_node(n) 405 406 # Assignments. 407 408 elif isinstance(n, compiler.ast.Assign): 409 410 # Handle each assignment node. 411 412 for node in n.nodes: 413 self.process_assignment_node(node, n.expr) 414 415 # Accesses. 416 417 elif isinstance(n, compiler.ast.Getattr): 418 return self.process_attribute_access(n) 419 420 # Names. 421 422 elif isinstance(n, compiler.ast.Name): 423 return self.process_name_node(n) 424 425 # Loops and conditionals. 426 427 elif isinstance(n, compiler.ast.For): 428 self.process_for_node(n) 429 430 elif isinstance(n, compiler.ast.While): 431 self.process_while_node(n) 432 433 elif isinstance(n, compiler.ast.If): 434 self.process_if_node(n) 435 436 elif isinstance(n, (compiler.ast.And, compiler.ast.Or)): 437 return self.process_logical_node(n) 438 439 elif isinstance(n, compiler.ast.Not): 440 return self.process_not_node(n) 441 442 # Exception control-flow tracking. 443 444 elif isinstance(n, compiler.ast.TryExcept): 445 self.process_try_node(n) 446 447 elif isinstance(n, compiler.ast.TryFinally): 448 self.process_try_finally_node(n) 449 450 # Control-flow modification statements. 451 452 elif isinstance(n, compiler.ast.Break): 453 self.writestmt("break;") 454 455 elif isinstance(n, compiler.ast.Continue): 456 self.writestmt("continue;") 457 458 elif isinstance(n, compiler.ast.Raise): 459 self.process_raise_node(n) 460 461 elif isinstance(n, compiler.ast.Return): 462 return self.process_return_node(n) 463 464 # Print statements. 465 466 elif isinstance(n, (compiler.ast.Print, compiler.ast.Printnl)): 467 self.statement(self.process_print_node(n)) 468 469 # Invocations. 470 471 elif isinstance(n, compiler.ast.CallFunc): 472 return self.process_invocation_node(n) 473 474 elif isinstance(n, compiler.ast.Keyword): 475 return self.process_structure_node(n.expr) 476 477 # Constant usage. 478 479 elif isinstance(n, compiler.ast.Const): 480 return self.get_literal_instance(n) 481 482 elif isinstance(n, compiler.ast.Dict): 483 return self.get_literal_instance(n, "dict") 484 485 elif isinstance(n, compiler.ast.List): 486 return self.get_literal_instance(n, "list") 487 488 elif isinstance(n, compiler.ast.Tuple): 489 return self.get_literal_instance(n, "tuple") 490 491 # All other nodes are processed depth-first. 492 493 else: 494 return self.process_structure(n) 495 496 def process_assignment_node(self, n, expr): 497 498 "Process the individual node 'n' to be assigned the contents of 'expr'." 499 500 # Names and attributes are assigned the entire expression. 501 502 if isinstance(n, compiler.ast.AssName): 503 name_ref = self.process_name_node(n, expr, True) 504 self.statement(name_ref) 505 506 # Employ guards after assignments if required. 507 508 if expr and name_ref.is_name(): 509 self.generate_guard(name_ref.name) 510 511 elif isinstance(n, compiler.ast.AssAttr): 512 in_assignment = self.in_assignment 513 self.result_target_name = "*__get_attr_ref(%d)" % self.attribute_ref_index 514 self.in_assignment = self.process_structure_node(expr) 515 self.statement(self.process_attribute_access(n)) 516 self.in_assignment = in_assignment 517 518 # Lists and tuples are matched against the expression and their 519 # items assigned to expression items. 520 521 elif isinstance(n, (compiler.ast.AssList, compiler.ast.AssTuple)): 522 self.process_assignment_node_items(n, expr) 523 524 # Slices and subscripts are permitted within assignment nodes. 525 526 elif isinstance(n, compiler.ast.Slice): 527 self.statement(self.process_slice_node(n, expr)) 528 529 elif isinstance(n, compiler.ast.Subscript): 530 self.statement(self.process_subscript_node(n, expr)) 531 532 def process_attribute_access(self, n): 533 534 "Process the given attribute access node 'n'." 535 536 # Obtain any completed chain and return the reference to it. 537 538 attr_expr = self.process_attribute_chain(n) 539 if self.have_access_expression(n): 540 return attr_expr 541 542 # Where the start of the chain of attributes has been reached, process 543 # the complete access. 544 545 name_ref = attr_expr and attr_expr.is_name() and attr_expr 546 name = name_ref and self.get_name_for_tracking(name_ref.name, name_ref) or None 547 548 location = self.get_access_location(name, self.attrs) 549 refs = self.get_referenced_attributes(location) 550 551 # Generate access instructions. 552 553 subs = { 554 "<expr>" : attr_expr, 555 "<name>" : attr_expr, 556 "<assexpr>" : self.in_assignment, 557 "<new_context>" : self.result_target_name or "__NULL", 558 } 559 560 subs.update(self.temp_subs) 561 subs.update(self.op_subs) 562 563 output = [] 564 substituted = set() 565 566 # The context set or retrieved will be that used by any enclosing 567 # invocation. 568 569 accessor_index = self.accessor_index 570 attribute_ref_index = self.attribute_ref_index 571 context_index = self.context_index 572 context_identity = None 573 context_identity_verified = False 574 final_identity = None 575 accessor_test = False 576 accessor_stored = False 577 attribute_ref_stored = False 578 579 # Obtain encoded versions of each instruction, accumulating temporary 580 # variables. 581 582 for instruction in self.deducer.access_instructions[location]: 583 584 # Intercept a special instruction identifying the context. 585 586 if instruction[0] in ("<context_identity>", "<context_identity_verified>"): 587 context_identity, _substituted = \ 588 encode_access_instruction_arg(instruction[1], subs, instruction[0], 589 accessor_index, context_index, 590 attribute_ref_index) 591 context_identity_verified = instruction[0] == "<context_identity_verified>" 592 continue 593 594 # Intercept a special instruction identifying the target. The value 595 # is not encoded since it is used internally. 596 597 elif instruction[0] == "<final_identity>": 598 final_identity = instruction[1] 599 continue 600 601 # Modify test instructions. 602 603 elif instruction[0] in typename_ops or instruction[0] in type_ops: 604 instruction = ("__to_error", instruction) 605 accessor_test = True 606 607 # Detect accessor storage. 608 609 elif instruction[0] == "<set_accessor>": 610 accessor_stored = True 611 612 # Detect attribute reference storage. 613 614 elif instruction[0] == "<set_attr_ref>": 615 attribute_ref_stored = True 616 self.next_attribute_ref() 617 618 # Collect the encoded instruction, noting any temporary variables 619 # required by it. 620 621 encoded, _substituted = encode_access_instruction(instruction, subs, 622 accessor_index, context_index, 623 attribute_ref_index) 624 output.append(encoded) 625 substituted.update(_substituted) 626 627 # Record temporary name usage. 628 629 for sub in substituted: 630 if self.temp_subs.has_key(sub): 631 self.record_temp(self.temp_subs[sub]) 632 633 # Get full final identity details. 634 635 if final_identity and not refs: 636 refs = set([self.importer.identify(final_identity)]) 637 638 del self.attrs[0] 639 return AttrResult(output, refs, location, 640 context_identity, context_identity_verified, 641 accessor_test, accessor_stored, attribute_ref_stored) 642 643 def init_substitutions(self): 644 645 """ 646 Initialise substitutions, defining temporary variable mappings, some of 647 which are also used as substitutions, together with operation mappings 648 used as substitutions in instructions defined by the optimiser. 649 """ 650 651 self.temp_subs = { 652 653 # Substitutions used by instructions. 654 655 "<private_context>" : "__tmp_private_context", 656 "<target_accessor>" : "__tmp_target_value", 657 658 # Mappings to be replaced by those given below. 659 660 "<accessor>" : "__tmp_values", 661 "<attr_ref>" : "__tmp_attr_refs", 662 "<context>" : "__tmp_contexts", 663 "<test_context_revert>" : "__tmp_contexts", 664 "<test_context_static>" : "__tmp_contexts", 665 "<set_context>" : "__tmp_contexts", 666 "<set_private_context>" : "__tmp_private_context", 667 "<set_accessor>" : "__tmp_values", 668 "<set_attr_ref>" : "__tmp_attr_refs", 669 "<set_target_accessor>" : "__tmp_target_value", 670 } 671 672 self.op_subs = { 673 "<accessor>" : "__get_accessor", 674 "<attr_ref>" : "__get_attr_ref", 675 "<context>" : "__get_context", 676 "<test_context_revert>" : "__test_context_revert", 677 "<test_context_static>" : "__test_context_static", 678 "<set_context>" : "__set_context", 679 "<set_private_context>" : "__set_private_context", 680 "<set_accessor>" : "__set_accessor", 681 "<set_attr_ref>" : "__set_attr_ref", 682 "<set_target_accessor>" : "__set_target_accessor", 683 } 684 685 def get_referenced_attributes(self, location): 686 687 """ 688 Convert 'location' to the form used by the deducer and retrieve any 689 identified attributes. 690 """ 691 692 # Determine whether any deduced references refer to the accessed 693 # attribute. 694 695 attrnames = location.attrnames 696 attrnames = attrnames and attrnames.split(".") 697 remaining = attrnames and len(attrnames) > 1 698 699 access_location = self.deducer.const_accesses.get(location) 700 701 if remaining and not access_location: 702 return set() 703 704 return self.deducer.get_references_for_access(access_location or location) 705 706 def get_referenced_attribute_invocations(self, location): 707 708 """ 709 Convert 'location' to the form used by the deducer and retrieve any 710 identified attribute invocation details. 711 """ 712 713 access_location = self.deducer.const_accesses.get(location) 714 return self.deducer.reference_invocations_unsuitable.get(access_location or location) 715 716 def get_accessor_kinds(self, location): 717 718 "Return the accessor kinds for 'location'." 719 720 return self.deducer.accessor_kinds.get(location) 721 722 def get_access_location(self, name, attrnames=None): 723 724 """ 725 Using the current namespace, the given 'name', and the 'attrnames' 726 employed in an access, return the access location. 727 """ 728 729 path = self.get_path_for_access() 730 731 # Get the location used by the deducer and optimiser and find any 732 # recorded access. 733 734 attrnames = attrnames and ".".join(self.attrs) 735 access_number = self.get_access_number(path, name, attrnames) 736 self.update_access_number(path, name, attrnames) 737 return AccessLocation(path, name, attrnames, access_number) 738 739 def get_access_number(self, path, name, attrnames): 740 access = name, attrnames 741 if self.attr_accesses.has_key(path) and self.attr_accesses[path].has_key(access): 742 return self.attr_accesses[path][access] 743 else: 744 return 0 745 746 def update_access_number(self, path, name, attrnames): 747 access = name, attrnames 748 if name: 749 init_item(self.attr_accesses, path, dict) 750 init_item(self.attr_accesses[path], access, lambda: 0) 751 self.attr_accesses[path][access] += 1 752 753 def get_accessor_location(self, name): 754 755 """ 756 Using the current namespace and the given 'name', return the accessor 757 location. 758 """ 759 760 path = self.get_path_for_access() 761 762 # Get the location used by the deducer and optimiser and find any 763 # recorded accessor. 764 765 version = self.get_accessor_number(path, name) 766 self.update_accessor_number(path, name) 767 return Location(path, name, None, version) 768 769 def get_accessor_number(self, path, name): 770 if self.attr_accessors.has_key(path) and self.attr_accessors[path].has_key(name): 771 return self.attr_accessors[path][name] 772 else: 773 return 0 774 775 def update_accessor_number(self, path, name): 776 if name: 777 init_item(self.attr_accessors, path, dict) 778 init_item(self.attr_accessors[path], name, lambda: 0) 779 self.attr_accessors[path][name] += 1 780 781 def process_class_node(self, n): 782 783 "Process the given class node 'n'." 784 785 class_name = self.get_object_path(n.name) 786 787 # Where a class is set conditionally or where the name may refer to 788 # different values, assign the name. 789 790 ref = self.importer.identify(class_name) 791 792 if not ref.static(): 793 self.process_assignment_for_object(n.name, 794 make_expression("__ATTRVALUE(&%s)" % encode_path(class_name))) 795 796 self.enter_namespace(n.name) 797 798 if self.have_object(): 799 self.write_comment("Class: %s" % class_name) 800 801 self.initialise_inherited_members(class_name) 802 803 self.process_structure(n) 804 self.write_comment("End class: %s" % class_name) 805 806 self.exit_namespace() 807 808 def initialise_inherited_members(self, class_name): 809 810 "Initialise members of 'class_name' inherited from its ancestors." 811 812 for name, path in self.importer.all_class_attrs[class_name].items(): 813 target = "%s.%s" % (class_name, name) 814 815 # Ignore attributes with definitions. 816 817 ref = self.importer.identify(target) 818 if ref: 819 continue 820 821 # Ignore special type attributes. 822 823 if is_type_attribute(name): 824 continue 825 826 # Reference inherited attributes. 827 828 ref = self.importer.identify(path) 829 if ref and not ref.static(): 830 parent, attrname = path.rsplit(".", 1) 831 832 self.writestmt("__store_via_attr_ref(__get_object_attr_ref(&%s, %s), __load_via_object(&%s, %s));" % ( 833 encode_path(class_name), name, 834 encode_path(parent), attrname 835 )) 836 837 def process_from_node(self, n): 838 839 "Process the given node 'n', importing from another module." 840 841 path = self.get_namespace_path() 842 843 # Attempt to obtain the referenced objects. 844 845 for name, alias in n.names: 846 if name == "*": 847 raise InspectError("Only explicitly specified names can be imported from modules.", path, n) 848 849 # Obtain the path of the assigned name. 850 851 objpath = self.get_object_path(alias or name) 852 853 # Obtain the identity of the name. 854 855 ref = self.importer.identify(objpath) 856 857 # Where the name is not static, assign the value. 858 859 if ref and not ref.static() and ref.get_name(): 860 self.writestmt("%s;" % 861 TrResolvedNameRef(alias or name, Reference("<var>", None, objpath), 862 expr=TrResolvedNameRef(name, ref))) 863 864 def process_function_body_node(self, n): 865 866 """ 867 Process the given function, lambda, if expression or list comprehension 868 node 'n', generating the body. 869 """ 870 871 function_name = self.get_namespace_path() 872 self.start_function(function_name) 873 874 # Process the function body. 875 876 in_conditional = self.in_conditional 877 self.in_conditional = False 878 879 # Reset temporary storage counters and limits. 880 881 self.reset_temp_limits() 882 883 # Reset result target storage details. To be effective, storage 884 # locations are not reused between statements. 885 886 self.max_result_target = 0 887 self.result_target = 0 888 889 # Volatile locals for exception handling. 890 891 self.volatile_locals = set() 892 893 # Process any guards defined for the parameters. 894 895 for name in self.importer.function_parameters.get(function_name): 896 self.generate_guard(name) 897 898 # Also support self in methods, since some mix-in methods may only work 899 # with certain descendant classes. 900 901 if self.in_method(): 902 self.generate_guard("self") 903 904 # Make assignments for .name entries in the parameters, provided this is 905 # a method. 906 907 if self.in_method(): 908 for name in self.importer.function_attr_initialisers.get(function_name) or []: 909 910 # Treat each assignment as a separate statement. 911 912 self.reset_temp_counters() 913 914 self.process_assignment_node( 915 compiler.ast.AssAttr(compiler.ast.Name("self"), name, "OP_ASSIGN"), 916 compiler.ast.Name(name)) 917 918 # Produce the body and any additional return statement. 919 920 is_lambda = isinstance(n, compiler.ast.Lambda) 921 922 expr = self.process_statement_node(n.code, is_lambda) or \ 923 self.in_method() and \ 924 function_name.rsplit(".", 1)[-1] == "__init__" and \ 925 TrResolvedNameRef("self", self.importer.function_locals[function_name]["self"]) or \ 926 PredefinedConstantRef("None") 927 928 if not isinstance(expr, ReturnRef): 929 self.writestmt("return %s;" % expr) 930 931 self.in_conditional = in_conditional 932 933 self.end_function(function_name) 934 935 def generate_guard(self, name): 936 937 """ 938 Get the accessor details for 'name', found in the current namespace, and 939 generate any guards defined for it. 940 """ 941 942 # Obtain the location, keeping track of assignment versions. 943 944 location = self.get_accessor_location(name) 945 test = self.deducer.accessor_guard_tests.get(location) 946 947 # Generate any guard from the deduced information. 948 949 if test: 950 guard, guard_type = test 951 952 if guard == "specific": 953 ref = first(self.deducer.accessor_all_types[location]) 954 argstr = "&%s" % encode_path(ref.get_origin()) 955 elif guard == "common": 956 ref = first(self.deducer.accessor_all_general_types[location]) 957 argstr = encode_path(encode_type_attribute(ref.get_origin())) 958 else: 959 return 960 961 # Write a test that raises a TypeError upon failure. 962 963 self.writestmt("if (!__test_%s_%s(__VALUE(%s), %s)) __raise_type_error();" % ( 964 guard, guard_type, encode_path(name), argstr)) 965 966 def process_function_node(self, n): 967 968 """ 969 Process the given function, lambda, if expression or list comprehension 970 node 'n', generating any initialisation statements. 971 """ 972 973 # Where a function is declared conditionally, use a separate name for 974 # the definition, and assign the definition to the stated name. 975 976 original_name = n.name 977 978 if self.in_conditional or self.in_function: 979 name = self.get_lambda_name() 980 else: 981 name = n.name 982 983 objpath = self.get_object_path(name) 984 985 # Obtain details of the defaults. 986 987 defaults = self.process_function_defaults(n, name, objpath) 988 if defaults: 989 for default in defaults: 990 self.writeline("%s;" % default) 991 992 # Where a function is set conditionally or where the name may refer to 993 # different values, assign the name. 994 995 ref = self.importer.identify(objpath) 996 997 if self.in_conditional or self.in_function: 998 self.process_assignment_for_object(original_name, compiler.ast.Name(name)) 999 elif not ref.static(): 1000 context = self.is_method(objpath) 1001 1002 self.process_assignment_for_object(original_name, 1003 make_expression("__ATTRVALUE(&%s)" % encode_path(objpath))) 1004 1005 def process_function_defaults(self, n, name, objpath, instance_name=None): 1006 1007 """ 1008 Process the given function or lambda node 'n', initialising defaults 1009 that are dynamically set. The given 'name' indicates the name of the 1010 function. The given 'objpath' indicates the origin of the function. 1011 The given 'instance_name' indicates the name of any separate instance 1012 of the function created to hold the defaults. 1013 1014 Return a list of operations setting defaults on a function instance. 1015 """ 1016 1017 function_name = self.get_object_path(name) 1018 function_defaults = self.importer.function_defaults.get(function_name) 1019 if not function_defaults: 1020 return None 1021 1022 # Determine whether any unidentified defaults are involved. 1023 1024 for argname, default in function_defaults: 1025 if not default.static(): 1026 break 1027 else: 1028 return None 1029 1030 # Handle bound methods. 1031 1032 if not instance_name: 1033 instance_name = "&%s" % encode_path(objpath) 1034 else: 1035 instance_name = "__VALUE(%s)" % instance_name 1036 1037 # Where defaults are involved but cannot be identified, obtain a new 1038 # instance of the lambda and populate the defaults. 1039 1040 defaults = [] 1041 1042 # Join the original defaults with the inspected defaults. 1043 1044 original_defaults = [(argname, default) for (argname, default) in compiler.ast.get_defaults(n) if default] 1045 1046 for i, (original, inspected) in enumerate(map(None, original_defaults, function_defaults)): 1047 1048 # Obtain any reference for the default. 1049 1050 if original: 1051 argname, default = original 1052 name_ref = self.process_structure_node(default) 1053 elif inspected: 1054 argname, default = inspected 1055 name_ref = TrResolvedNameRef(argname, default) 1056 else: 1057 continue 1058 1059 # Generate default initialisers except when constants are employed. 1060 # Constants should be used when populating the function structures. 1061 1062 if name_ref and not isinstance(name_ref, TrConstantValueRef): 1063 defaults.append("__SETDEFAULT(%s, %s, %s)" % (instance_name, i, name_ref)) 1064 1065 return defaults 1066 1067 def process_if_node(self, n): 1068 1069 """ 1070 Process the given "if" node 'n'. 1071 """ 1072 1073 first = True 1074 for test, body in n.tests: 1075 test_ref = self.process_statement_node(test) 1076 self.start_if(first, test_ref) 1077 1078 in_conditional = self.in_conditional 1079 self.in_conditional = True 1080 self.process_statement_node(body) 1081 self.in_conditional = in_conditional 1082 1083 self.end_if() 1084 first = False 1085 1086 if n.else_: 1087 self.start_else() 1088 self.process_statement_node(n.else_) 1089 self.end_else() 1090 1091 print >>self.out 1092 1093 def process_invocation_node(self, n): 1094 1095 "Process the given invocation node 'n'." 1096 1097 # Process the expression. 1098 1099 expr = self.process_structure_node(n.node) 1100 1101 # Obtain details of the invocation expression. 1102 1103 objpath = expr.get_origin() 1104 location = expr.access_location() 1105 refs = expr.references() 1106 1107 # Identified target details. 1108 1109 target = None 1110 target_structure = None 1111 1112 # Specific function target information. 1113 1114 function = None 1115 1116 # Instantiation involvement. 1117 1118 instantiation = False 1119 literal_instantiation = False 1120 1121 # Invocation requirements. 1122 1123 context_required = True 1124 have_access_context = isinstance(expr, AttrResult) 1125 1126 # The context identity is merely the thing providing the context. 1127 # A verified context is one that does not need further testing for 1128 # suitability. 1129 1130 context_identity = have_access_context and expr.context() 1131 context_verified = have_access_context and expr.context_verified() 1132 1133 # The presence of any test operations in the accessor expression. 1134 # With such operations present, the expression cannot be eliminated. 1135 1136 tests_accessor = have_access_context and expr.tests_accessor() 1137 stores_accessor = have_access_context and expr.stores_accessor() 1138 1139 # Parameter details and parameter list dimensions. 1140 1141 parameters = None 1142 num_parameters = None 1143 num_defaults = None 1144 1145 # Obtain details of the callable and of its parameters. 1146 1147 # Literals may be instantiated specially. 1148 1149 if expr.is_name() and expr.name.startswith("$L") and objpath: 1150 instantiation = literal_instantiation = objpath 1151 target = encode_literal_instantiator(objpath) 1152 context_required = False 1153 1154 # Identified targets employ function pointers directly. 1155 1156 elif objpath: 1157 parameters = self.importer.function_parameters.get(objpath) 1158 function_defaults = self.importer.function_defaults.get(objpath) 1159 num_parameters = parameters and len(parameters) or 0 1160 num_defaults = function_defaults and len(function_defaults) or 0 1161 1162 # Class invocation involves instantiators. 1163 1164 if expr.has_kind("<class>"): 1165 instantiation = objpath 1166 target = encode_instantiator_pointer(objpath) 1167 init_ref = self.importer.all_class_attrs[objpath]["__init__"] 1168 target_structure = "&%s" % encode_path(init_ref) 1169 context_required = False 1170 1171 # Only plain functions and bound methods employ function pointers. 1172 1173 elif expr.has_kind("<function>"): 1174 function = objpath 1175 1176 # Test for functions and methods. 1177 1178 context_required = self.is_method(objpath) 1179 1180 accessor_kinds = location and self.get_accessor_kinds(location) 1181 1182 instance_accessor = accessor_kinds and \ 1183 len(accessor_kinds) == 1 and \ 1184 first(accessor_kinds) == "<instance>" 1185 1186 # Only identify certain bound methods or functions. 1187 1188 if not context_required or instance_accessor: 1189 target = encode_function_pointer(objpath) 1190 1191 # Access bound method defaults even if it is not clear whether 1192 # the accessor is appropriate. 1193 1194 target_structure = "&%s" % encode_path(objpath) 1195 1196 # Other targets are retrieved at run-time. 1197 1198 else: 1199 if location: 1200 attrnames = location.attrnames 1201 attrname = attrnames and attrnames.rsplit(".", 1)[-1] 1202 1203 # Determine common aspects of any identifiable targets. 1204 1205 if attrname or refs: 1206 all_params = set() 1207 all_defaults = set() 1208 min_params = set() 1209 max_params = set() 1210 1211 # Employ references from the expression or find all 1212 # possible attributes for the given attribute name. 1213 1214 refs = refs or self.get_attributes_for_attrname(attrname) 1215 1216 # Obtain parameters and defaults for each possible target. 1217 1218 for ref in refs: 1219 origin = ref.get_origin() 1220 params = self.importer.function_parameters.get(origin) 1221 1222 defaults = self.importer.function_defaults.get(origin) 1223 if defaults is not None: 1224 all_defaults.add(tuple(defaults)) 1225 1226 if params is not None: 1227 all_params.add(tuple(params)) 1228 min_params.add(len(params) - (defaults and len(defaults) or 0)) 1229 max_params.add(len(params)) 1230 else: 1231 refs = set() 1232 break 1233 1234 # Where the parameters and defaults are always the same, 1235 # permit populating them in advance. 1236 1237 if refs: 1238 if self.uses_keyword_arguments(n): 1239 if len(all_params) == 1 and (not all_defaults or len(all_defaults) == 1): 1240 parameters = first(all_params) 1241 function_defaults = all_defaults and first(all_defaults) or [] 1242 num_parameters = parameters and len(parameters) or 0 1243 num_defaults = function_defaults and len(function_defaults) or 0 1244 else: 1245 if len(min_params) == 1 and len(max_params) == 1: 1246 num_parameters = first(max_params) 1247 num_defaults = first(max_params) - first(min_params) 1248 1249 # Some information about the target may be available and be used to 1250 # provide warnings about argument compatibility. 1251 1252 if self.importer.give_warning("args"): 1253 unsuitable = self.get_referenced_attribute_invocations(location) 1254 1255 if unsuitable: 1256 for ref in unsuitable: 1257 _objpath = ref.get_origin() 1258 print >>sys.stderr, \ 1259 "In %s, at line %d, inappropriate number of " \ 1260 "arguments given. Need %d arguments to call %s." % ( 1261 self.get_namespace_path(), n.lineno, 1262 len(self.importer.function_parameters[_objpath]), 1263 _objpath) 1264 1265 # Logical statement about available parameter information. 1266 1267 known_parameters = num_parameters is not None 1268 1269 # The source of context information: target or temporary. 1270 1271 need_context_target = context_required and not have_access_context 1272 1273 need_context_stored = context_required and context_identity and \ 1274 context_identity.startswith("__get_context") 1275 1276 # Determine any readily-accessible target identity. 1277 1278 target_named = expr.is_name() and str(expr) or None 1279 target_identity = target or target_named 1280 1281 # Use of target information to populate defaults. 1282 1283 defaults_target_var = not (parameters and function_defaults is not None) and \ 1284 known_parameters and len(n.args) < num_parameters 1285 1286 # Use of a temporary target variable in these situations: 1287 # 1288 # A target provided by an expression needed for defaults. 1289 # 1290 # A target providing the context but not using a name to do so. 1291 # 1292 # A target expression involving the definition of a context which may 1293 # then be evaluated and stored to ensure that the context is available 1294 # during argument evaluation. 1295 # 1296 # An expression featuring an accessor test. 1297 1298 need_target_stored = defaults_target_var and not target_identity or \ 1299 need_context_target and not target_identity or \ 1300 need_context_stored or \ 1301 tests_accessor and not target 1302 1303 # Define stored target details. 1304 1305 target_stored = "__tmp_targets[%d]" % self.function_target 1306 target_var = need_target_stored and target_stored or target_identity 1307 1308 if need_target_stored: 1309 self.record_temp("__tmp_targets") 1310 1311 if need_context_stored: 1312 self.record_temp("__tmp_contexts") 1313 1314 if stores_accessor: 1315 self.record_temp("__tmp_values") 1316 1317 # Employ result targets only in functions. 1318 1319 if self.in_function: 1320 if self.result_target_name: 1321 result_target = self.result_target_name 1322 self.result_target_name = None 1323 else: 1324 result_target = "__tmp_results[%d]" % self.result_target 1325 self.record_temp("__tmp_results") 1326 self.next_result() 1327 else: 1328 result_target = None 1329 1330 # Arguments are presented in a temporary frame array with any context 1331 # always being the first argument. Where it would be unused, it may be 1332 # set to null. 1333 1334 if context_required: 1335 if have_access_context: 1336 context_arg = context_identity 1337 else: 1338 context_arg = "__CONTEXT_AS_VALUE(%s)" % target_var 1339 else: 1340 context_arg = "__NULL" 1341 1342 # Start with result target and context arguments for each invocation. 1343 1344 args = [result_target or "__NULL", context_arg] 1345 reserved_args = 2 1346 1347 # Complete the array with null values, permitting tests for a complete 1348 # set of arguments. 1349 1350 args += [None] * (num_parameters is None and len(n.args) or num_parameters is not None and num_parameters or 0) 1351 kwcodes = [] 1352 kwargs = [] 1353 1354 # Any invocations in the arguments will store target details in a 1355 # different location. 1356 1357 function_target = self.function_target 1358 context_index = self.context_index 1359 accessor_index = self.accessor_index 1360 1361 if need_target_stored: 1362 self.next_target() 1363 1364 if need_context_stored: 1365 self.next_context() 1366 1367 if stores_accessor: 1368 self.next_accessor() 1369 1370 in_parameter_list = self.in_parameter_list 1371 self.in_parameter_list = True 1372 1373 for i, arg in enumerate(n.args): 1374 argexpr = self.process_structure_node(arg) 1375 1376 # Convert any attributes indicating value replacement. 1377 1378 if isinstance(argexpr, InvocationResult) and argexpr.result_target: 1379 argexprstr = "__set_attr(&%s, %s)" % (argexpr.result_target, argexpr) 1380 else: 1381 argexprstr = str(argexpr) 1382 1383 # Store a keyword argument, either in the argument list or 1384 # in a separate keyword argument list for subsequent lookup. 1385 1386 if isinstance(arg, compiler.ast.Keyword): 1387 1388 # With knowledge of the target, store the keyword 1389 # argument directly. 1390 1391 if parameters: 1392 try: 1393 argnum = parameters.index(arg.name) 1394 except ValueError: 1395 raise TranslateError("Argument %s is not recognised." % arg.name, 1396 self.get_namespace_path(), n) 1397 args[argnum + reserved_args] = argexprstr 1398 1399 # Otherwise, store the details in a separate collection. 1400 1401 else: 1402 kwargs.append(argexprstr) 1403 kwcodes.append("{%s, %s}" % ( 1404 encode_ppos(arg.name), encode_pcode(arg.name))) 1405 1406 # Store non-keyword arguments in the argument list, rejecting 1407 # superfluous arguments. 1408 1409 else: 1410 try: 1411 args[i + reserved_args] = argexprstr 1412 except IndexError: 1413 raise TranslateError("Too many arguments specified.", 1414 self.get_namespace_path(), n) 1415 1416 # Reference the current target again. 1417 1418 self.in_parameter_list = in_parameter_list 1419 1420 if not self.in_parameter_list: 1421 self.function_target = function_target 1422 self.context_index = context_index 1423 self.accessor_index = accessor_index 1424 1425 # Defaults are added to the frame where arguments are missing. 1426 1427 if parameters and function_defaults is not None: 1428 1429 # Visit each default and set any missing arguments. Where keyword 1430 # arguments have been used, the defaults must be inspected and, if 1431 # necessary, inserted into gaps in the argument list. 1432 1433 for i, (argname, default) in enumerate(function_defaults): 1434 argnum = parameters.index(argname) 1435 if not args[argnum + reserved_args]: 1436 args[argnum + reserved_args] = "__GETDEFAULT(%s, %d)" % (target_structure, i) 1437 1438 elif known_parameters: 1439 1440 # No specific parameter details are provided, but no keyword 1441 # arguments are used. Thus, defaults can be supplied using position 1442 # information only. 1443 1444 i = len(n.args) 1445 pos = i - (num_parameters - num_defaults) 1446 while i < num_parameters: 1447 args[i + reserved_args] = "__GETDEFAULT(%s.value, %d)" % (target_var, pos) 1448 i += 1 1449 pos += 1 1450 1451 # Test for missing arguments. 1452 1453 if None in args: 1454 raise TranslateError("Not all arguments supplied.", 1455 self.get_namespace_path(), n) 1456 1457 # Encode the arguments. 1458 1459 # Where literal instantiation is occurring, add an argument indicating 1460 # the number of values. The result target and context are excluded. 1461 1462 if literal_instantiation: 1463 argstr = "%d, %s" % (len(args) - reserved_args, ", ".join(args[reserved_args:])) 1464 else: 1465 argstr = ", ".join(args) 1466 1467 kwargstr = kwargs and ("__ARGS(%s)" % ", ".join(kwargs)) or "0" 1468 kwcodestr = kwcodes and ("__KWARGS(%s)" % ", ".join(kwcodes)) or "0" 1469 1470 # First, the invocation expression is presented. 1471 1472 stages = [] 1473 emit = stages.append 1474 1475 # Assign and yield any stored target. 1476 # The context may be set in the expression. 1477 1478 if need_target_stored: 1479 emit("%s = %s" % (target_var, expr)) 1480 target_expr = target_var 1481 1482 # Otherwise, retain the expression for later use. 1483 1484 else: 1485 target_expr = str(expr) 1486 1487 # Any specific callable is then obtained for invocation. 1488 1489 if target: 1490 1491 # An expression involving a test of the accessor providing the target. 1492 # This must be emitted in order to perform the test. 1493 1494 if tests_accessor: 1495 emit(str(expr)) 1496 1497 emit(target) 1498 1499 # Methods accessed via unidentified accessors are obtained for 1500 # invocation. 1501 1502 elif function: 1503 if context_required: 1504 1505 # Avoid further context testing if appropriate. 1506 1507 if have_access_context and context_verified: 1508 emit("__get_function_member(%s)" % target_expr) 1509 1510 # Otherwise, test the context for the function/method. 1511 1512 else: 1513 emit("__get_function(%s, %s)" % (context_arg, target_expr)) 1514 else: 1515 emit("_get_function_member(%s)" % target_expr) 1516 1517 # With known parameters, the target can be tested. 1518 1519 elif known_parameters: 1520 if self.always_callable(refs): 1521 if context_verified: 1522 emit("__get_function_member(%s)" % target_expr) 1523 else: 1524 emit("__get_function(%s, %s)" % (context_arg, target_expr)) 1525 else: 1526 emit("__check_and_get_function(%s, %s)" % (context_arg, target_expr)) 1527 1528 # With a known target, the function is obtained directly and called. 1529 # By putting the invocation at the end of the final element in the 1530 # instruction sequence (the stages), the result becomes the result of 1531 # the sequence. Moreover, the parameters become part of the sequence 1532 # and thereby participate in a guaranteed evaluation order. 1533 1534 if target or function or known_parameters: 1535 stages[-1] += "(%s)" % argstr 1536 if instantiation: 1537 return InstantiationResult(instantiation, stages) 1538 else: 1539 return InvocationResult(result_target, stages) 1540 1541 # With unknown targets, the generic invocation function is applied to 1542 # the callable and argument collections. 1543 1544 else: 1545 emit("__invoke(\n%s,\n%d, %d, %s, %s,\n%d, %s\n)" % ( 1546 target_expr, 1547 self.always_callable(refs) and 1 or 0, 1548 len(kwargs), kwcodestr, kwargstr, 1549 len(args), "__ARGS(%s)" % argstr)) 1550 return InvocationResult(result_target, stages) 1551 1552 def reset_temp_counters(self): 1553 1554 "Reset the target counters." 1555 1556 self.function_target = 0 1557 self.context_index = 0 1558 self.accessor_index = 0 1559 self.attribute_ref_index = 0 1560 self.result_target_name = None 1561 1562 def reset_temp_limits(self): 1563 1564 "Reset the target counter limits." 1565 1566 self.max_function_target = 0 1567 self.max_context_index = 0 1568 self.max_accessor_index = 0 1569 self.max_attribute_ref_index = 0 1570 1571 def next_result(self): 1572 1573 "Allocate the next result target storage." 1574 1575 self.result_target += 1 1576 self.max_result_target = max(self.result_target, self.max_result_target) 1577 1578 def next_target(self): 1579 1580 "Allocate the next function target storage." 1581 1582 self.function_target += 1 1583 self.max_function_target = max(self.function_target, self.max_function_target) 1584 1585 def next_context(self): 1586 1587 "Allocate the next context value storage." 1588 1589 self.context_index += 1 1590 self.max_context_index = max(self.context_index, self.max_context_index) 1591 1592 def next_accessor(self): 1593 1594 "Allocate the next accessor value storage." 1595 1596 self.accessor_index += 1 1597 self.max_accessor_index = max(self.accessor_index, self.max_accessor_index) 1598 1599 def next_attribute_ref(self): 1600 1601 "Allocate the next attribute reference value storage." 1602 1603 self.attribute_ref_index += 1 1604 self.max_attribute_ref_index = max(self.attribute_ref_index, self.max_attribute_ref_index) 1605 1606 def always_callable(self, refs): 1607 1608 "Determine whether all 'refs' are callable." 1609 1610 if not refs: 1611 return False 1612 1613 for ref in refs: 1614 if not ref.has_kind("<function>") and not self.importer.get_attributes(ref, "__fn__"): 1615 return False 1616 1617 return True 1618 1619 def need_default_arguments(self, objpath, nargs): 1620 1621 """ 1622 Return whether any default arguments are needed when invoking the object 1623 given by 'objpath'. 1624 """ 1625 1626 parameters = self.importer.function_parameters.get(objpath) 1627 return nargs < len(parameters) 1628 1629 def uses_keyword_arguments(self, n): 1630 1631 "Return whether invocation node 'n' uses keyword arguments." 1632 1633 for arg in enumerate(n.args): 1634 if isinstance(arg, compiler.ast.Keyword): 1635 return True 1636 1637 return False 1638 1639 def get_attributes_for_attrname(self, attrname): 1640 1641 "Return a set of all attributes exposed by 'attrname'." 1642 1643 usage = [(attrname, True, False)] 1644 class_types = self.deducer.get_class_types_for_usage(usage) 1645 instance_types = self.deducer.get_instance_types_for_usage(usage) 1646 module_types = self.deducer.get_module_types_for_usage(usage) 1647 attrs = set() 1648 1649 for ref in combine_types(class_types, instance_types, module_types): 1650 attrs.update(self.importer.get_attributes(ref, attrname)) 1651 1652 return attrs 1653 1654 def process_lambda_node(self, n): 1655 1656 "Process the given lambda node 'n'." 1657 1658 name = self.get_lambda_name() 1659 function_name = self.get_object_path(name) 1660 instance_name = "__get_accessor(%d)" % self.accessor_index 1661 1662 defaults = self.process_function_defaults(n, name, function_name, instance_name) 1663 1664 # Without defaults, produce an attribute referring to the function. 1665 1666 if not defaults: 1667 return make_expression("__ATTRVALUE(&%s)" % encode_path(function_name)) 1668 1669 # With defaults, copy the function structure and set the defaults on the 1670 # copy. 1671 1672 else: 1673 self.record_temp("__tmp_values") 1674 return make_expression("""\ 1675 (__set_accessor(%d, __ATTRVALUE(__COPY(&%s, sizeof(%s)))), 1676 %s, 1677 __get_accessor(%d))""" % ( 1678 self.accessor_index, 1679 encode_path(function_name), 1680 encode_symbol("obj", function_name), 1681 ", ".join(defaults), 1682 self.accessor_index)) 1683 1684 def process_logical_node(self, n): 1685 1686 "Process the given operator node 'n'." 1687 1688 self.record_temp("__tmp_result") 1689 1690 conjunction = isinstance(n, compiler.ast.And) 1691 results = [] 1692 1693 for node in n.nodes: 1694 results.append(self.process_structure_node(node)) 1695 1696 return LogicalOperationResult(results, conjunction) 1697 1698 def process_name_node(self, n, expr=None, process_expr=False): 1699 1700 "Process the given name node 'n' with the optional assignment 'expr'." 1701 1702 # Determine whether the name refers to a static external entity. 1703 1704 if n.name in predefined_constants: 1705 return PredefinedConstantRef(n.name, expr) 1706 1707 # Convert literal references, operator function names, and print 1708 # function names to references. 1709 1710 elif n.name.startswith("$L") or n.name.startswith("$op") or \ 1711 n.name.startswith("$seq") or n.name.startswith("$print"): 1712 1713 ref, paths = self.importer.get_module(self.name).special[n.name] 1714 return TrResolvedNameRef(n.name, ref) 1715 1716 # Get the appropriate name for the name reference, using the same method 1717 # as in the inspector. 1718 1719 path = self.get_namespace_path() 1720 objpath = self.get_object_path(n.name) 1721 1722 # Determine any assigned globals. 1723 1724 globals = self.importer.get_module(self.name).scope_globals.get(path) 1725 1726 # Explicitly declared globals. 1727 1728 if globals and n.name in globals: 1729 objpath = self.get_global_path(n.name) 1730 is_global = True 1731 1732 # Implicitly referenced globals in functions. 1733 1734 elif self.in_function: 1735 is_global = n.name not in self.importer.function_locals[path] 1736 1737 # Implicitly referenced globals elsewhere. 1738 1739 else: 1740 namespace = self.importer.identify(path) 1741 is_global = not self.importer.get_attributes(namespace, n.name) 1742 1743 # Get the static identity of the name. 1744 1745 ref = self.importer.identify(objpath) 1746 if ref and not ref.get_name(): 1747 ref = ref.alias(objpath) 1748 1749 # Obtain any resolved names for non-assignment names. 1750 1751 if not expr and not ref and self.in_function: 1752 locals = self.importer.function_locals.get(path) 1753 ref = locals and locals.get(n.name) 1754 1755 # Find any invocation or alias details. 1756 1757 name = self.get_name_for_tracking(n.name, is_global=is_global) 1758 location = not expr and self.get_access_location(name) or None 1759 1760 # Mark any local assignments as volatile in exception blocks. 1761 1762 if expr and self.in_function and not is_global and self.in_try_except: 1763 self.make_volatile(n.name) 1764 1765 # Set the replacement target. Note that this will not apply to all 1766 # objects, with only floats supporting replaceable values. 1767 1768 if expr: 1769 # Prevent parameters from becoming result targets. Otherwise, they 1770 # may inadvertently cause the modification of the supplied object. 1771 1772 parameters = self.importer.function_parameters.get(path) 1773 1774 if not parameters or n.name not in parameters: 1775 target_ref = TrResolvedNameRef(n.name, ref, is_global=is_global, 1776 location=location) 1777 self.result_target_name = str(target_ref) 1778 else: 1779 self.result_target_name = None 1780 1781 # Expression processing is deferred until after any result target has 1782 # been set. 1783 1784 if process_expr: 1785 expr = self.process_structure_node(expr) 1786 1787 # Qualified names are used for resolved static references or for 1788 # static namespace members. The reference should be configured to return 1789 # such names. 1790 1791 name_ref = TrResolvedNameRef(n.name, ref, expr=expr, is_global=is_global, 1792 location=location) 1793 1794 return not expr and self.get_aliases(name_ref) or name_ref 1795 1796 def get_aliases(self, name_ref): 1797 1798 "Return alias references for the given 'name_ref'." 1799 1800 location = name_ref.access_location() 1801 accessor_locations = self.deducer.access_index.get(location) 1802 1803 if not accessor_locations: 1804 return None 1805 1806 refs = set() 1807 1808 for accessor_location in accessor_locations: 1809 alias_refs = self.deducer.referenced_objects.get(accessor_location) 1810 if alias_refs: 1811 refs.update(alias_refs) 1812 1813 if refs: 1814 return AliasResult(name_ref, refs, location) 1815 else: 1816 return None 1817 1818 def make_volatile(self, name): 1819 1820 "Record 'name' as volatile in the current namespace." 1821 1822 self.volatile_locals.add(name) 1823 1824 def process_not_node(self, n): 1825 1826 "Process the given operator node 'n'." 1827 1828 return self.make_negation(self.process_structure_node(n.expr)) 1829 1830 def process_raise_node(self, n): 1831 1832 "Process the given raise node 'n'." 1833 1834 # NOTE: Determine which raise statement variants should be permitted. 1835 1836 if n.expr1: 1837 1838 # Names with accompanying arguments are treated like invocations. 1839 1840 if n.expr2: 1841 call = compiler.ast.CallFunc(n.expr1, [n.expr2]) 1842 exc = self.process_structure_node(call) 1843 self.writestmt("__Raise(%s);" % exc) 1844 1845 # Raise instances, testing the kind at run-time if necessary and 1846 # instantiating any non-instance. 1847 1848 else: 1849 exc = self.process_structure_node(n.expr1) 1850 1851 if isinstance(exc, TrInstanceRef) or exc.is_well_defined_instance(): 1852 self.writestmt("__Raise(%s);" % exc) 1853 else: 1854 self.writestmt("__Raise(__ensure_instance(%s));" % exc) 1855 else: 1856 self.writestmt("__Throw(__tmp_exc);") 1857 1858 def process_return_node(self, n): 1859 1860 "Process the given return node 'n'." 1861 1862 if self.in_function: 1863 self.result_target_name = "__result" 1864 1865 expr = self.process_structure_node(n.value) or PredefinedConstantRef("None") 1866 1867 if self.in_try_finally or self.in_try_except: 1868 self.writestmt("__Return(%s);" % expr) 1869 else: 1870 self.writestmt("return %s;" % expr) 1871 1872 return ReturnRef() 1873 1874 def process_try_node(self, n): 1875 1876 """ 1877 Process the given "try...except" node 'n'. 1878 """ 1879 1880 in_try_except = self.in_try_except 1881 self.in_try_except = True 1882 1883 # Use macros to implement exception handling. 1884 1885 self.writestmt("__Try") 1886 self.writeline("{") 1887 self.indent += 1 1888 self.process_statement_node(n.body) 1889 1890 # Put the else statement in another try block that handles any raised 1891 # exceptions and converts them to exceptions that will not be handled by 1892 # the main handling block. 1893 1894 if n.else_: 1895 self.writestmt("__Try") 1896 self.writeline("{") 1897 self.indent += 1 1898 self.process_statement_node(n.else_) 1899 self.indent -= 1 1900 self.writeline("}") 1901 self.writeline("__Catch (__tmp_exc)") 1902 self.writeline("{") 1903 self.indent += 1 1904 self.writeline("if (__tmp_exc.raising) __RaiseElse(__tmp_exc.arg);") 1905 self.writeline("else if (__tmp_exc.completing) __Throw(__tmp_exc);") 1906 self.indent -= 1 1907 self.writeline("}") 1908 1909 # Complete the try block and enter the finally block, if appropriate. 1910 1911 if self.in_try_finally: 1912 self.writestmt("__Complete;") 1913 1914 self.indent -= 1 1915 self.writeline("}") 1916 1917 self.in_try_except = in_try_except 1918 1919 # Handlers are tests within a common handler block. 1920 1921 self.writeline("__Catch (__tmp_exc)") 1922 self.writeline("{") 1923 self.indent += 1 1924 1925 # Introduce an if statement to handle the completion of a try block. 1926 1927 self.process_try_completion() 1928 1929 # Handle exceptions in else blocks converted to __RaiseElse, converting 1930 # them back to normal exceptions. 1931 1932 if n.else_: 1933 self.writeline("else if (__tmp_exc.raising_else) __Raise(__tmp_exc.arg);") 1934 1935 # Exception handling. 1936 1937 for name, var, handler in n.handlers: 1938 1939 # Test for specific exceptions. 1940 1941 if name is not None: 1942 name_ref = self.process_statement_node(name) 1943 self.writeline("else if (__ISINSTANCE(__tmp_exc.arg, %s))" % name_ref) 1944 else: 1945 self.writeline("else if (1)") 1946 1947 self.writeline("{") 1948 self.indent += 1 1949 1950 # Establish the local for the handler. 1951 1952 if var is not None: 1953 self.writestmt("%s;" % self.process_name_node(var, make_expression("__tmp_exc.arg"))) 1954 1955 if handler is not None: 1956 self.process_statement_node(handler) 1957 1958 self.indent -= 1 1959 self.writeline("}") 1960 1961 # Re-raise unhandled exceptions. 1962 1963 self.writeline("else __Throw(__tmp_exc);") 1964 1965 # End the handler block. 1966 1967 self.indent -= 1 1968 self.writeline("}") 1969 print >>self.out 1970 1971 def process_try_finally_node(self, n): 1972 1973 """ 1974 Process the given "try...finally" node 'n'. 1975 """ 1976 1977 in_try_finally = self.in_try_finally 1978 self.in_try_finally = True 1979 1980 # Use macros to implement exception handling. 1981 1982 self.writestmt("__Try") 1983 self.writeline("{") 1984 self.indent += 1 1985 self.process_statement_node(n.body) 1986 self.indent -= 1 1987 self.writeline("}") 1988 1989 self.in_try_finally = in_try_finally 1990 1991 # Finally clauses handle special exceptions. 1992 1993 self.writeline("__Catch (__tmp_exc)") 1994 self.writeline("{") 1995 self.indent += 1 1996 self.process_statement_node(n.final) 1997 1998 # Introduce an if statement to handle the completion of a try block. 1999 2000 self.process_try_completion() 2001 self.writeline("else __Throw(__tmp_exc);") 2002 2003 self.indent -= 1 2004 self.writeline("}") 2005 print >>self.out 2006 2007 def process_try_completion(self): 2008 2009 "Generate a test for the completion of a try block." 2010 2011 self.writestmt("if (__tmp_exc.completing)") 2012 self.writeline("{") 2013 self.indent += 1 2014 2015 # Do not return anything at the module level. 2016 2017 if self.get_namespace_path() != self.name: 2018 2019 # Only use the normal return statement if no surrounding try blocks 2020 # apply. 2021 2022 if not self.in_try_finally and not self.in_try_except: 2023 self.writeline("if (!__ISNULL(__tmp_exc.arg)) return __tmp_exc.arg;") 2024 else: 2025 self.writeline("if (!__ISNULL(__tmp_exc.arg)) __Throw(__tmp_exc);") 2026 2027 self.indent -= 1 2028 self.writeline("}") 2029 2030 def process_while_node(self, n): 2031 2032 "Process the given while node 'n'." 2033 2034 self.writeline("while (1)") 2035 self.writeline("{") 2036 self.indent += 1 2037 test = self.process_statement_node(n.test) 2038 2039 # Emit the loop termination condition unless "while <true value>" is 2040 # indicated. 2041 2042 if not (isinstance(test, PredefinedConstantRef) and test.value): 2043 2044 # Emit a negated test of the continuation condition. 2045 2046 self.start_if(True, self.make_negation(test)) 2047 if n.else_: 2048 self.process_statement_node(n.else_) 2049 self.writestmt("break;") 2050 self.end_if() 2051 2052 in_conditional = self.in_conditional 2053 self.in_conditional = True 2054 self.process_statement_node(n.body) 2055 self.in_conditional = in_conditional 2056 2057 self.indent -= 1 2058 self.writeline("}") 2059 print >>self.out 2060 2061 # Special variable usage. 2062 2063 def get_temp_path(self): 2064 2065 """ 2066 Return the appropriate namespace path for temporary names in the current 2067 namespace. 2068 """ 2069 2070 if self.in_function: 2071 return self.get_namespace_path() 2072 else: 2073 return self.name 2074 2075 def record_temp(self, name): 2076 2077 """ 2078 Record the use of the temporary 'name' in the current namespace. At the 2079 class or module level, the temporary name is associated with the module, 2080 since the variable will then be allocated in the module's own main 2081 program. 2082 """ 2083 2084 path = self.get_temp_path() 2085 2086 init_item(self.temp_usage, path, list) 2087 self.temp_usage[path].append(name) 2088 2089 def remove_temps(self, names): 2090 2091 """ 2092 Remove 'names' from temporary storage allocations, each instance 2093 removing each request for storage. 2094 """ 2095 2096 path = self.get_temp_path() 2097 2098 for name in names: 2099 if self.uses_temp(path, name): 2100 self.temp_usage[path].remove(name) 2101 2102 def uses_temp(self, path, name): 2103 2104 """ 2105 Return whether the given namespace 'path' employs a temporary variable 2106 with the given 'name'. Note that 'path' should only be a module or a 2107 function or method, not a class. 2108 """ 2109 2110 return self.temp_usage.has_key(path) and name in self.temp_usage[path] 2111 2112 def make_negation(self, expr): 2113 2114 "Return a negated form of 'expr'." 2115 2116 result = NegationResult(expr) 2117 2118 # Negation discards the temporary results of its operand. 2119 2120 temps = expr.discards_temporary() 2121 if temps: 2122 self.remove_temps(temps) 2123 2124 return result 2125 2126 # Output generation. 2127 2128 def start_output(self): 2129 2130 "Write the declarations at the top of each source file." 2131 2132 print >>self.out, """\ 2133 #include "types.h" 2134 #include "exceptions.h" 2135 #include "ops.h" 2136 #include "progconsts.h" 2137 #include "progops.h" 2138 #include "progtypes.h" 2139 #include "main.h" 2140 """ 2141 2142 def start_unit(self): 2143 2144 "Record output within a generated function for later use." 2145 2146 self.out = StringIO() 2147 2148 def end_unit(self): 2149 2150 "Restore the output stream." 2151 2152 out = self.out 2153 self.out = self.out_toplevel 2154 return out 2155 2156 def flush_unit(self, name, out): 2157 2158 "Add declarations and generated code." 2159 2160 self.write_temporaries(name) 2161 print >>self.out 2162 out.seek(0) 2163 self.out.write(out.read()) 2164 2165 def start_module(self): 2166 2167 "Write the start of each module's main function." 2168 2169 print >>self.out, "void __main_%s()" % encode_path(self.name) 2170 print >>self.out, "{" 2171 self.indent += 1 2172 2173 # Define temporary variables, excluded from the module structure itself. 2174 2175 tempnames = [] 2176 2177 for n in self.importer.all_module_attrs[self.name]: 2178 if n.startswith("$t"): 2179 tempnames.append(encode_path(n)) 2180 2181 if tempnames: 2182 tempnames.sort() 2183 self.writeline("__attr %s;" % ", ".join(tempnames)) 2184 2185 self.start_unit() 2186 2187 def end_module(self): 2188 2189 "End each module by closing its main function." 2190 2191 out = self.end_unit() 2192 self.flush_unit(self.name, out) 2193 2194 self.indent -= 1 2195 print >>self.out, "}" 2196 2197 def start_function(self, name): 2198 2199 "Start the function having the given 'name'." 2200 2201 self.indent += 1 2202 2203 self.start_unit() 2204 2205 def end_function(self, name): 2206 2207 "End the function having the given 'name'." 2208 2209 out = self.end_unit() 2210 2211 # Write the signature at the top indentation level. 2212 2213 self.indent -= 1 2214 self.write_parameters(name) 2215 print >>self.out, "{" 2216 2217 # Obtain local names from parameters. 2218 2219 parameters = self.importer.function_parameters[name] 2220 locals = self.importer.function_locals[name].keys() 2221 names = [] 2222 volatile_names = [] 2223 2224 for n in locals: 2225 2226 # Filter out special names and parameters. Note that self is a local 2227 # regardless of whether it originally appeared in the parameters or 2228 # not. 2229 2230 if n.startswith("$l") or n in parameters or n == "self": 2231 continue 2232 if n in self.volatile_locals: 2233 volatile_names.append("%s = __NULL" % encode_path(n)) 2234 else: 2235 names.append("%s = __NULL" % encode_path(n)) 2236 2237 # Emit required local names at the function indentation level. 2238 2239 self.indent += 1 2240 2241 if names: 2242 names.sort() 2243 self.writeline("__attr %s;" % ", ".join(names)) 2244 2245 if volatile_names: 2246 volatile_names.sort() 2247 self.writeline("volatile __attr %s;" % ", ".join(volatile_names)) 2248 2249 self.flush_unit(name, out) 2250 2251 self.indent -= 1 2252 print >>self.out, "}" 2253 print >>self.out 2254 2255 def write_parameters(self, name): 2256 2257 """ 2258 For the function having the given 'name', write definitions of 2259 parameters found in the arguments array. 2260 """ 2261 2262 # Generate any self reference. 2263 2264 l = [] 2265 l.append("__attr __result") 2266 2267 if self.is_method(name): 2268 l.append("__attr self") 2269 else: 2270 l.append("__attr __self") 2271 2272 # Generate aliases for the parameters. 2273 2274 for parameter in self.importer.function_parameters[name]: 2275 l.append("%s__attr %s" % ( 2276 parameter in self.volatile_locals and "volatile " or "", 2277 encode_path(parameter))) 2278 2279 self.writeline("__attr %s(%s)" % ( 2280 encode_function_pointer(name), ", ".join(l))) 2281 2282 def write_temporaries(self, name): 2283 2284 "Write temporary storage employed by 'name'." 2285 2286 # Provide space for the recorded number of temporary variables. 2287 2288 if self.uses_temp(name, "__tmp_targets"): 2289 self.writeline("__attr __tmp_targets[%d];" % self.max_function_target) 2290 2291 if self.uses_temp(name, "__tmp_contexts"): 2292 self.writeline("__attr __tmp_contexts[%d];" % self.max_context_index) 2293 2294 if self.uses_temp(name, "__tmp_values"): 2295 self.writeline("__attr __tmp_values[%d];" % self.max_accessor_index) 2296 2297 if self.uses_temp(name, "__tmp_attr_refs"): 2298 self.writeline("__attr *__tmp_attr_refs[%d];" % self.max_attribute_ref_index) 2299 2300 if self.uses_temp(name, "__tmp_results"): 2301 self.writeline("__attr __tmp_results[%d] = {0};" % self.max_result_target) 2302 2303 if self.uses_temp(name, "__tmp_private_context"): 2304 self.writeline("__attr __tmp_private_context;") 2305 if self.uses_temp(name, "__tmp_target_value"): 2306 self.writeline("__attr __tmp_target_value;") 2307 if self.uses_temp(name, "__tmp_result"): 2308 self.writeline("__attr __tmp_result;") 2309 2310 module = self.importer.get_module(self.name) 2311 2312 if name in module.exception_namespaces: 2313 self.writeline("__exc __tmp_exc;") 2314 2315 def start_if(self, first, test_ref): 2316 statement = "%sif" % (not first and "else " or "") 2317 2318 # Consume logical results directly. 2319 2320 if isinstance(test_ref, LogicalResult): 2321 self.writeline("%s %s" % (statement, test_ref.apply_test())) 2322 temps = test_ref.discards_temporary() 2323 if temps: 2324 self.remove_temps(temps) 2325 else: 2326 self.writeline("%s (__BOOL(%s))" % (statement, test_ref)) 2327 2328 self.writeline("{") 2329 self.indent += 1 2330 2331 def end_if(self): 2332 self.indent -= 1 2333 self.writeline("}") 2334 2335 def start_else(self): 2336 self.writeline("else") 2337 self.writeline("{") 2338 self.indent += 1 2339 2340 def end_else(self): 2341 self.indent -= 1 2342 self.writeline("}") 2343 2344 def statement(self, expr): 2345 s = str(expr) 2346 if s: 2347 self.writestmt("%s;" % s) 2348 2349 def statements(self, results): 2350 for result in results: 2351 self.statement(result) 2352 2353 def writeline(self, s): 2354 print >>self.out, "%s%s" % (self.pad(), self.indenttext(s, self.indent + 1)) 2355 2356 def writestmt(self, s): 2357 self.writeline(s) 2358 2359 def write_comment(self, s): 2360 self.writestmt("/* %s */" % s) 2361 2362 def pad(self, extra=0): 2363 return (self.indent + extra) * self.tabstop 2364 2365 def indenttext(self, s, levels): 2366 lines = s.split("\n") 2367 out = [lines[0]] 2368 for line in lines[1:]: 2369 out.append(levels * self.tabstop + line) 2370 if line.endswith("("): 2371 levels += 1 2372 elif line.startswith(")"): 2373 levels -= 1 2374 return "\n".join(out) 2375 2376 # vim: tabstop=4 expandtab shiftwidth=4