1 #!/usr/bin/env python 2 3 """ 4 Translate programs. 5 6 Copyright (C) 2015, 2016 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 * 23 from encoders import * 24 from os.path import exists, join 25 from os import makedirs 26 from referencing import Reference 27 import compiler 28 import results 29 30 class Translator(CommonOutput): 31 32 "A program translator." 33 34 def __init__(self, importer, deducer, optimiser, output): 35 self.importer = importer 36 self.deducer = deducer 37 self.optimiser = optimiser 38 self.output = output 39 40 def to_output(self): 41 output = join(self.output, "src") 42 43 if not exists(output): 44 makedirs(output) 45 46 self.check_output() 47 48 for module in self.importer.modules.values(): 49 if module.name != "native": 50 tm = TranslatedModule(module.name, self.importer, self.deducer, self.optimiser) 51 tm.translate(module.filename, join(output, "%s.c" % module.name)) 52 53 # Classes representing intermediate translation results. 54 55 class TranslationResult: 56 57 "An abstract translation result mix-in." 58 59 def get_accessor_kinds(self): 60 return None 61 62 class ReturnRef(TranslationResult): 63 64 "Indicates usage of a return statement." 65 66 pass 67 68 class Expression(results.Result, TranslationResult): 69 70 "A general expression." 71 72 def __init__(self, s): 73 self.s = s 74 def __str__(self): 75 return self.s 76 def __repr__(self): 77 return "Expression(%r)" % self.s 78 79 class TrResolvedNameRef(results.ResolvedNameRef, TranslationResult): 80 81 "A reference to a name in the translation." 82 83 def __init__(self, name, ref, expr=None, parameter=None): 84 results.ResolvedNameRef.__init__(self, name, ref, expr) 85 self.parameter = parameter 86 87 def __str__(self): 88 89 "Return an output representation of the referenced name." 90 91 # For sources, any identified static origin will be constant and thus 92 # usable directly. For targets, no constant should be assigned and thus 93 # the alias (or any plain name) will be used. 94 95 ref = self.static() 96 origin = ref and self.get_origin() 97 static_name = origin and encode_path(origin) 98 99 # Determine whether a qualified name is involved. 100 101 t = (self.get_name() or self.name).rsplit(".", 1) 102 parent = len(t) > 1 and t[0] or None 103 attrname = encode_path(t[-1]) 104 105 # Assignments. 106 107 if self.expr: 108 109 # Eliminate assignments between constants. 110 111 if ref and isinstance(self.expr, results.ResolvedNameRef) and self.expr.static(): 112 return "" 113 114 # Qualified names must be converted into parent-relative assignments. 115 116 elif parent: 117 return "__store_via_object(&%s, %s, %s)" % ( 118 encode_path(parent), encode_symbol("pos", attrname), self.expr) 119 120 # All other assignments involve the names as they were given. 121 122 else: 123 return "(%s%s) = %s" % (self.parameter and "*" or "", attrname, self.expr) 124 125 # Expressions. 126 127 elif static_name: 128 parent = ref.parent() 129 context = ref.has_kind("<function>") and encode_path(parent) or None 130 return "((__attr) {%s, &%s})" % (context and "&%s" % context or "0", static_name) 131 132 # Qualified names must be converted into parent-relative accesses. 133 134 elif parent: 135 return "__load_via_object(&%s, %s)" % ( 136 encode_path(parent), encode_symbol("pos", attrname)) 137 138 # All other accesses involve the names as they were given. 139 140 else: 141 return "(%s%s)" % (self.parameter and "*" or "", attrname) 142 143 class TrConstantValueRef(results.ConstantValueRef, TranslationResult): 144 145 "A constant value reference in the translation." 146 147 def __str__(self): 148 return encode_literal_constant(self.number) 149 150 class TrLiteralSequenceRef(results.LiteralSequenceRef, TranslationResult): 151 152 "A reference representing a sequence of values." 153 154 def __str__(self): 155 return str(self.node) 156 157 class TrInstanceRef(results.InstanceRef, TranslationResult): 158 159 "A reference representing instantiation of a class." 160 161 def __init__(self, ref, expr): 162 163 """ 164 Initialise the reference with 'ref' indicating the nature of the 165 reference and 'expr' being an expression used to create the instance. 166 """ 167 168 results.InstanceRef.__init__(self, ref) 169 self.expr = expr 170 171 def __str__(self): 172 return self.expr 173 174 def __repr__(self): 175 return "TrResolvedInstanceRef(%r, %r)" % (self.ref, self.expr) 176 177 class AttrResult(Expression, TranslationResult): 178 179 "A translation result for an attribute access." 180 181 def __init__(self, s, refs, accessor_kinds): 182 Expression.__init__(self, s) 183 self.refs = refs 184 self.accessor_kinds = accessor_kinds 185 186 def get_origin(self): 187 return self.refs and len(self.refs) == 1 and first(self.refs).get_origin() 188 189 def has_kind(self, kinds): 190 if not self.refs: 191 return False 192 for ref in self.refs: 193 if ref.has_kind(kinds): 194 return True 195 return False 196 197 def get_accessor_kinds(self): 198 return self.accessor_kinds 199 200 def __repr__(self): 201 return "AttrResult(%r, %r)" % (self.s, self.get_origin()) 202 203 class PredefinedConstantRef(AttrResult): 204 205 "A predefined constant reference." 206 207 def __init__(self, value): 208 self.value = value 209 210 def __str__(self): 211 if self.value in ("False", "True"): 212 return encode_path("__builtins__.boolean.%s" % self.value) 213 elif self.value == "None": 214 return encode_path("__builtins__.none.%s" % self.value) 215 elif self.value == "NotImplemented": 216 return encode_path("__builtins__.notimplemented.%s" % self.value) 217 else: 218 return self.value 219 220 def __repr__(self): 221 return "PredefinedConstantRef(%r)" % self.value 222 223 class BooleanResult(Expression, TranslationResult): 224 225 "A expression producing a boolean result." 226 227 def __str__(self): 228 return "__builtins___bool_bool(%s)" % self.s 229 230 def __repr__(self): 231 return "BooleanResult(%r)" % self.s 232 233 def make_expression(expr): 234 235 "Make a new expression from the existing 'expr'." 236 237 if isinstance(expr, results.Result): 238 return expr 239 else: 240 return Expression(str(expr)) 241 242 # The actual translation process itself. 243 244 class TranslatedModule(CommonModule): 245 246 "A module translator." 247 248 def __init__(self, name, importer, deducer, optimiser): 249 CommonModule.__init__(self, name, importer) 250 self.deducer = deducer 251 self.optimiser = optimiser 252 253 # Output stream. 254 255 self.out = None 256 self.indent = 0 257 self.tabstop = " " 258 259 # Recorded namespaces. 260 261 self.namespaces = [] 262 self.in_conditional = False 263 264 # Exception raising adjustments. 265 266 self.in_try_finally = False 267 self.in_try_except = False 268 269 # Attribute access and accessor counting. 270 271 self.attr_accesses = {} 272 self.attr_accessors = {} 273 274 def __repr__(self): 275 return "TranslatedModule(%r, %r)" % (self.name, self.importer) 276 277 def translate(self, filename, output_filename): 278 279 """ 280 Parse the file having the given 'filename', writing the translation to 281 the given 'output_filename'. 282 """ 283 284 self.parse_file(filename) 285 286 # Collect function namespaces for separate processing. 287 288 self.record_namespaces(self.astnode) 289 290 # Reset the lambda naming (in order to obtain the same names again) and 291 # translate the program. 292 293 self.reset_lambdas() 294 295 self.out = open(output_filename, "w") 296 try: 297 self.start_output() 298 299 # Process namespaces, writing the translation. 300 301 for path, node in self.namespaces: 302 self.process_namespace(path, node) 303 304 # Process the module namespace including class namespaces. 305 306 self.process_namespace([], self.astnode) 307 308 finally: 309 self.out.close() 310 311 def have_object(self): 312 313 "Return whether a namespace is a recorded object." 314 315 return self.importer.objects.get(self.get_namespace_path()) 316 317 def get_builtin_class(self, name): 318 319 "Return a reference to the actual object providing 'name'." 320 321 # NOTE: This makes assumptions about the __builtins__ structure. 322 323 return self.importer.get_object("__builtins__.%s.%s" % (name, name)) 324 325 def is_method(self, path): 326 327 "Return whether 'path' is a method." 328 329 class_name, method_name = path.rsplit(".", 1) 330 return self.importer.classes.has_key(class_name) and class_name or None 331 332 def in_method(self): 333 334 "Return whether the current namespace provides a method." 335 336 return self.in_function and self.is_method(self.get_namespace_path()) 337 338 # Namespace recording. 339 340 def record_namespaces(self, node): 341 342 "Process the program structure 'node', recording namespaces." 343 344 for n in node.getChildNodes(): 345 self.record_namespaces_in_node(n) 346 347 def record_namespaces_in_node(self, node): 348 349 "Process the program structure 'node', recording namespaces." 350 351 # Function namespaces within modules, classes and other functions. 352 # Functions appearing within conditional statements are given arbitrary 353 # names. 354 355 if isinstance(node, compiler.ast.Function): 356 self.record_function_node(node, (self.in_conditional or self.in_function) and self.get_lambda_name() or node.name) 357 358 elif isinstance(node, compiler.ast.Lambda): 359 self.record_function_node(node, self.get_lambda_name()) 360 361 # Classes are visited, but may be ignored if inside functions. 362 363 elif isinstance(node, compiler.ast.Class): 364 self.enter_namespace(node.name) 365 if self.have_object(): 366 self.record_namespaces(node) 367 self.exit_namespace() 368 369 # Conditional nodes are tracked so that function definitions may be 370 # handled. Since "for" loops are converted to "while" loops, they are 371 # included here. 372 373 elif isinstance(node, (compiler.ast.For, compiler.ast.If, compiler.ast.While)): 374 in_conditional = self.in_conditional 375 self.in_conditional = True 376 self.record_namespaces(node) 377 self.in_conditional = in_conditional 378 379 # All other nodes are processed depth-first. 380 381 else: 382 self.record_namespaces(node) 383 384 def record_function_node(self, n, name): 385 386 """ 387 Record the given function, lambda, if expression or list comprehension 388 node 'n' with the given 'name'. 389 """ 390 391 self.in_function = True 392 self.enter_namespace(name) 393 394 if self.have_object(): 395 396 # Record the namespace path and the node itself. 397 398 self.namespaces.append((self.namespace_path[:], n)) 399 self.record_namespaces_in_node(n.code) 400 401 self.exit_namespace() 402 self.in_function = False 403 404 # Constant referencing. 405 406 def get_literal_instance(self, n, name): 407 408 """ 409 For node 'n', return a reference for the type of the given 'name'. 410 """ 411 412 ref = self.get_builtin_class(name) 413 414 if name in ("dict", "list", "tuple"): 415 return self.process_literal_sequence_node(n, name, ref, TrLiteralSequenceRef) 416 else: 417 path = self.get_namespace_path() 418 local_number = self.importer.all_constants[path][n.value] 419 constant_name = "$c%d" % local_number 420 objpath = self.get_object_path(constant_name) 421 number = self.optimiser.constant_numbers[objpath] 422 return TrConstantValueRef(constant_name, ref.instance_of(), n.value, number) 423 424 # Namespace translation. 425 426 def process_namespace(self, path, node): 427 428 """ 429 Process the namespace for the given 'path' defined by the given 'node'. 430 """ 431 432 self.namespace_path = path 433 434 if isinstance(node, (compiler.ast.Function, compiler.ast.Lambda)): 435 self.in_function = True 436 self.process_function_body_node(node) 437 else: 438 self.in_function = False 439 self.function_target = 0 440 self.start_module() 441 self.process_structure(node) 442 self.end_module() 443 444 def process_structure(self, node): 445 446 "Process the given 'node' or result." 447 448 # Handle processing requests on results. 449 450 if isinstance(node, results.Result): 451 return node 452 453 # Handle processing requests on nodes. 454 455 else: 456 l = CommonModule.process_structure(self, node) 457 458 # Return indications of return statement usage. 459 460 if l and isinstance(l[-1], ReturnRef): 461 return l[-1] 462 else: 463 return None 464 465 def process_structure_node(self, n): 466 467 "Process the individual node 'n'." 468 469 # Plain statements emit their expressions. 470 471 if isinstance(n, compiler.ast.Discard): 472 expr = self.process_structure_node(n.expr) 473 self.statement(expr) 474 475 # Module import declarations. 476 477 elif isinstance(n, compiler.ast.From): 478 self.process_from_node(n) 479 480 # Nodes using operator module functions. 481 482 elif isinstance(n, compiler.ast.Operator): 483 return self.process_operator_node(n) 484 485 elif isinstance(n, compiler.ast.AugAssign): 486 self.process_augassign_node(n) 487 488 elif isinstance(n, compiler.ast.Compare): 489 return self.process_compare_node(n) 490 491 elif isinstance(n, compiler.ast.Slice): 492 return self.process_slice_node(n) 493 494 elif isinstance(n, compiler.ast.Sliceobj): 495 return self.process_sliceobj_node(n) 496 497 elif isinstance(n, compiler.ast.Subscript): 498 return self.process_subscript_node(n) 499 500 # Classes are visited, but may be ignored if inside functions. 501 502 elif isinstance(n, compiler.ast.Class): 503 self.process_class_node(n) 504 505 # Functions within namespaces have any dynamic defaults initialised. 506 507 elif isinstance(n, compiler.ast.Function): 508 self.process_function_node(n) 509 510 # Lambdas are replaced with references to separately-generated 511 # functions. 512 513 elif isinstance(n, compiler.ast.Lambda): 514 return self.process_lambda_node(n) 515 516 # Assignments. 517 518 elif isinstance(n, compiler.ast.Assign): 519 520 # Handle each assignment node. 521 522 for node in n.nodes: 523 self.process_assignment_node(node, n.expr) 524 525 # Accesses. 526 527 elif isinstance(n, compiler.ast.Getattr): 528 return self.process_attribute_access(n) 529 530 # Names. 531 532 elif isinstance(n, compiler.ast.Name): 533 return self.process_name_node(n) 534 535 # Loops and conditionals. 536 537 elif isinstance(n, compiler.ast.For): 538 self.process_for_node(n) 539 540 elif isinstance(n, compiler.ast.While): 541 self.process_while_node(n) 542 543 elif isinstance(n, compiler.ast.If): 544 self.process_if_node(n) 545 546 elif isinstance(n, (compiler.ast.And, compiler.ast.Or)): 547 return self.process_logical_node(n) 548 549 elif isinstance(n, compiler.ast.Not): 550 return self.process_not_node(n) 551 552 # Exception control-flow tracking. 553 554 elif isinstance(n, compiler.ast.TryExcept): 555 self.process_try_node(n) 556 557 elif isinstance(n, compiler.ast.TryFinally): 558 self.process_try_finally_node(n) 559 560 # Control-flow modification statements. 561 562 elif isinstance(n, compiler.ast.Break): 563 self.writestmt("break;") 564 565 elif isinstance(n, compiler.ast.Continue): 566 self.writestmt("continue;") 567 568 elif isinstance(n, compiler.ast.Raise): 569 self.process_raise_node(n) 570 571 elif isinstance(n, compiler.ast.Return): 572 return self.process_return_node(n) 573 574 # Print statements. 575 576 elif isinstance(n, (compiler.ast.Print, compiler.ast.Printnl)): 577 self.statement(self.process_print_node(n)) 578 579 # Invocations. 580 581 elif isinstance(n, compiler.ast.CallFunc): 582 return self.process_invocation_node(n) 583 584 elif isinstance(n, compiler.ast.Keyword): 585 return self.process_structure_node(n.expr) 586 587 # Constant usage. 588 589 elif isinstance(n, compiler.ast.Const): 590 return self.get_literal_instance(n, n.value.__class__.__name__) 591 592 elif isinstance(n, compiler.ast.Dict): 593 return self.get_literal_instance(n, "dict") 594 595 elif isinstance(n, compiler.ast.List): 596 return self.get_literal_instance(n, "list") 597 598 elif isinstance(n, compiler.ast.Tuple): 599 return self.get_literal_instance(n, "tuple") 600 601 # All other nodes are processed depth-first. 602 603 else: 604 return self.process_structure(n) 605 606 def process_assignment_node(self, n, expr): 607 608 "Process the individual node 'n' to be assigned the contents of 'expr'." 609 610 # Names and attributes are assigned the entire expression. 611 612 if isinstance(n, compiler.ast.AssName): 613 name_ref = self.process_name_node(n, self.process_structure_node(expr)) 614 self.statement(name_ref) 615 616 # Employ guards after assignments if required. 617 618 if expr and name_ref.is_name(): 619 self.generate_guard(name_ref.name) 620 621 elif isinstance(n, compiler.ast.AssAttr): 622 in_assignment = self.in_assignment 623 self.in_assignment = self.process_structure_node(expr) 624 self.statement(self.process_attribute_access(n)) 625 self.in_assignment = in_assignment 626 627 # Lists and tuples are matched against the expression and their 628 # items assigned to expression items. 629 630 elif isinstance(n, (compiler.ast.AssList, compiler.ast.AssTuple)): 631 self.process_assignment_node_items(n, expr) 632 633 # Slices and subscripts are permitted within assignment nodes. 634 635 elif isinstance(n, compiler.ast.Slice): 636 self.statement(self.process_slice_node(n, expr)) 637 638 elif isinstance(n, compiler.ast.Subscript): 639 self.statement(self.process_subscript_node(n, expr)) 640 641 def process_attribute_access(self, n): 642 643 """ 644 Process the given attribute access node 'n'. 645 646 Where a name is provided, a single access should be recorded 647 involving potentially many attributes, thus providing a path to an 648 object. The remaining attributes are then accessed dynamically. 649 The remaining accesses could be deduced and computed, but they would 650 also need to be tested. 651 652 Where no name is provided, potentially many accesses should be 653 recorded, one per attribute name. These could be used to provide 654 computed accesses, but the accessors would need to be tested in each 655 case. 656 """ 657 658 # Obtain any completed chain and return the reference to it. 659 660 attr_expr = self.process_attribute_chain(n) 661 if self.have_access_expression(n): 662 return attr_expr 663 664 # Where the start of the chain of attributes has been reached, process 665 # the complete access. 666 667 name_ref = attr_expr and attr_expr.is_name() and attr_expr 668 name = name_ref and self.get_name_for_tracking(name_ref.name, name_ref and name_ref.final()) or None 669 670 location = self.get_access_location(name) 671 refs = self.get_referenced_attributes(location) 672 673 # Generate access instructions. 674 675 subs = { 676 "<expr>" : str(attr_expr), 677 "<assexpr>" : str(self.in_assignment), 678 "<context>" : "__tmp_context", 679 "<accessor>" : "__tmp_value", 680 } 681 682 output = [] 683 684 for instruction in self.optimiser.access_instructions[location]: 685 output.append(encode_access_instruction(instruction, subs)) 686 687 if len(output) == 1: 688 out = output[0] 689 else: 690 out = "(\n%s\n)" % ",\n".join(output) 691 692 del self.attrs[0] 693 return AttrResult(out, refs, self.get_accessor_kinds(location)) 694 695 def get_referenced_attributes(self, location): 696 697 """ 698 Convert 'location' to the form used by the deducer and retrieve any 699 identified attribute. 700 """ 701 702 access_location = self.deducer.const_accesses.get(location) 703 refs = [] 704 for attrtype, objpath, attr in self.deducer.referenced_attrs[access_location or location]: 705 refs.append(attr) 706 return refs 707 708 def get_accessor_kinds(self, location): 709 710 "Return the accessor kinds for 'location'." 711 712 return self.optimiser.accessor_kinds[location] 713 714 def get_access_location(self, name): 715 716 """ 717 Using the current namespace and the given 'name', return the access 718 location. 719 """ 720 721 path = self.get_path_for_access() 722 723 # Get the location used by the deducer and optimiser and find any 724 # recorded access. 725 726 attrnames = ".".join(self.attrs) 727 access_number = self.get_access_number(path, name, attrnames) 728 self.update_access_number(path, name, attrnames) 729 return (path, name, attrnames, access_number) 730 731 def get_access_number(self, path, name, attrnames): 732 access = name, attrnames 733 if self.attr_accesses.has_key(path) and self.attr_accesses[path].has_key(access): 734 return self.attr_accesses[path][access] 735 else: 736 return 0 737 738 def update_access_number(self, path, name, attrnames): 739 access = name, attrnames 740 if name: 741 init_item(self.attr_accesses, path, dict) 742 init_item(self.attr_accesses[path], access, lambda: 0) 743 self.attr_accesses[path][access] += 1 744 745 def get_accessor_location(self, name): 746 747 """ 748 Using the current namespace and the given 'name', return the accessor 749 location. 750 """ 751 752 path = self.get_path_for_access() 753 754 # Get the location used by the deducer and optimiser and find any 755 # recorded accessor. 756 757 access_number = self.get_accessor_number(path, name) 758 self.update_accessor_number(path, name) 759 return (path, name, None, access_number) 760 761 def get_accessor_number(self, path, name): 762 if self.attr_accessors.has_key(path) and self.attr_accessors[path].has_key(name): 763 return self.attr_accessors[path][name] 764 else: 765 return 0 766 767 def update_accessor_number(self, path, name): 768 if name: 769 init_item(self.attr_accessors, path, dict) 770 init_item(self.attr_accessors[path], name, lambda: 0) 771 self.attr_accessors[path][name] += 1 772 773 def process_class_node(self, n): 774 775 "Process the given class node 'n'." 776 777 self.enter_namespace(n.name) 778 779 if self.have_object(): 780 class_name = self.get_namespace_path() 781 self.write_comment("Class: %s" % class_name) 782 783 self.initialise_inherited_members(class_name) 784 785 self.process_structure(n) 786 self.write_comment("End class: %s" % class_name) 787 788 self.exit_namespace() 789 790 def initialise_inherited_members(self, class_name): 791 792 "Initialise members of 'class_name' inherited from its ancestors." 793 794 for name, path in self.importer.all_class_attrs[class_name].items(): 795 target = "%s.%s" % (class_name, name) 796 797 # Ignore attributes with definitions. 798 799 ref = self.importer.identify(target) 800 if ref: 801 continue 802 803 # Reference inherited attributes. 804 805 ref = self.importer.identify(path) 806 if ref and not ref.static(): 807 parent, attrname = path.rsplit(".", 1) 808 809 self.writestmt("__store_via_object(&%s, %s, __load_via_object(&%s, %s));" % ( 810 encode_path(class_name), encode_symbol("pos", name), 811 encode_path(parent), encode_symbol("pos", attrname) 812 )) 813 814 def process_from_node(self, n): 815 816 "Process the given node 'n', importing from another module." 817 818 path = self.get_namespace_path() 819 820 # Attempt to obtain the referenced objects. 821 822 for name, alias in n.names: 823 if name == "*": 824 raise InspectError("Only explicitly specified names can be imported from modules.", path, n) 825 826 # Obtain the path of the assigned name. 827 828 objpath = self.get_object_path(alias or name) 829 830 # Obtain the identity of the name. 831 832 ref = self.importer.identify(objpath) 833 834 # Where the name is not static, assign the value. 835 836 if ref and not ref.static() and ref.get_name(): 837 self.writestmt("%s;" % 838 TrResolvedNameRef(alias or name, Reference("<var>", None, objpath), 839 expr=TrResolvedNameRef(name, ref))) 840 841 def process_function_body_node(self, n): 842 843 """ 844 Process the given function, lambda, if expression or list comprehension 845 node 'n', generating the body. 846 """ 847 848 function_name = self.get_namespace_path() 849 self.start_function(function_name) 850 851 # Process the function body. 852 853 in_conditional = self.in_conditional 854 self.in_conditional = False 855 self.function_target = 0 856 857 # Process any guards defined for the parameters. 858 859 for name in self.importer.function_parameters.get(function_name): 860 self.generate_guard(name) 861 862 # Produce the body and any additional return statement. 863 864 expr = self.process_structure_node(n.code) or PredefinedConstantRef("None") 865 if not isinstance(expr, ReturnRef): 866 self.writestmt("return %s;" % expr) 867 868 self.in_conditional = in_conditional 869 870 self.end_function(function_name) 871 872 def generate_guard(self, name): 873 874 """ 875 Get the accessor details for 'name', found in the current namespace, and 876 generate any guards defined for it. 877 """ 878 879 # Obtain the location, keeping track of assignment versions. 880 881 location = self.get_accessor_location(name) 882 test = self.deducer.accessor_guard_tests.get(location) 883 884 # Generate any guard from the deduced information. 885 886 if test: 887 guard, guard_type = test 888 889 if guard == "specific": 890 ref = first(self.deducer.accessor_all_types[location]) 891 argstr = "&%s" % encode_path(ref.get_origin()) 892 elif guard == "common": 893 ref = first(self.deducer.accessor_all_general_types[location]) 894 typeattr = encode_type_attribute(ref.get_origin()) 895 argstr = "%s, %s" % (encode_symbol("pos", typeattr), encode_symbol("code", typeattr)) 896 else: 897 return 898 899 # Produce an appropriate access to an attribute's value. 900 901 parameters = self.importer.function_parameters.get(self.get_namespace_path()) 902 if parameters and name in parameters: 903 name_to_value = "%s->value" % name 904 else: 905 name_to_value = "%s.value" % name 906 907 # Write a test that raises a TypeError upon failure. 908 909 self.writestmt("if (!__test_%s_%s(%s, %s)) __raise_type_error();" % ( 910 guard, guard_type, name_to_value, argstr)) 911 912 def process_function_node(self, n): 913 914 """ 915 Process the given function, lambda, if expression or list comprehension 916 node 'n', generating any initialisation statements. 917 """ 918 919 # Where a function is declared conditionally, use a separate name for 920 # the definition, and assign the definition to the stated name. 921 922 original_name = n.name 923 924 if self.in_conditional or self.in_function: 925 name = self.get_lambda_name() 926 else: 927 name = n.name 928 929 objpath = self.get_object_path(name) 930 931 # Obtain details of the defaults. 932 933 defaults = self.process_function_defaults(n, name, objpath) 934 if defaults: 935 for default in defaults: 936 self.writeline("%s;" % default) 937 938 # Where a function is set conditionally or where the name may refer to 939 # different values, assign the name. 940 941 ref = self.importer.identify(objpath) 942 943 if self.in_conditional or self.in_function: 944 self.process_assignment_for_function(original_name, compiler.ast.Name(name)) 945 elif not ref.static(): 946 context = self.is_method(objpath) 947 948 self.process_assignment_for_function(original_name, 949 make_expression("((__attr) {%s, &%s})" % ( 950 context and "&%s" % encode_path(context) or "0", 951 encode_path(objpath)))) 952 953 def process_function_defaults(self, n, name, objpath, instance_name=None): 954 955 """ 956 Process the given function or lambda node 'n', initialising defaults 957 that are dynamically set. The given 'name' indicates the name of the 958 function. The given 'objpath' indicates the origin of the function. 959 The given 'instance_name' indicates the name of any separate instance 960 of the function created to hold the defaults. 961 962 Return a list of operations setting defaults on a function instance. 963 """ 964 965 function_name = self.get_object_path(name) 966 function_defaults = self.importer.function_defaults.get(function_name) 967 if not function_defaults: 968 return None 969 970 # Determine whether any unidentified defaults are involved. 971 972 for argname, default in function_defaults: 973 if not default.static(): 974 break 975 else: 976 return None 977 978 # Handle bound methods. 979 980 if not instance_name: 981 if self.is_method(objpath): 982 instance_name = "&%s" % encode_bound_reference(objpath) 983 else: 984 instance_name = "&%s" % encode_path(objpath) 985 986 # Where defaults are involved but cannot be identified, obtain a new 987 # instance of the lambda and populate the defaults. 988 989 defaults = [] 990 991 # Join the original defaults with the inspected defaults. 992 993 original_defaults = [(argname, default) for (argname, default) in compiler.ast.get_defaults(n) if default] 994 995 for i, (original, inspected) in enumerate(map(None, original_defaults, function_defaults)): 996 997 # Obtain any reference for the default. 998 999 if original: 1000 argname, default = original 1001 name_ref = self.process_structure_node(default) 1002 elif inspected: 1003 argname, default = inspected 1004 name_ref = TrResolvedNameRef(argname, default) 1005 else: 1006 continue 1007 1008 if name_ref: 1009 defaults.append("__SETDEFAULT(%s, %s, %s)" % (instance_name, i, name_ref)) 1010 1011 return defaults 1012 1013 def process_if_node(self, n): 1014 1015 """ 1016 Process the given "if" node 'n'. 1017 """ 1018 1019 first = True 1020 for test, body in n.tests: 1021 test_ref = self.process_structure_node(test) 1022 self.start_if(first, test_ref) 1023 1024 in_conditional = self.in_conditional 1025 self.in_conditional = True 1026 self.process_structure_node(body) 1027 self.in_conditional = in_conditional 1028 1029 self.end_if() 1030 first = False 1031 1032 if n.else_: 1033 self.start_else() 1034 self.process_structure_node(n.else_) 1035 self.end_else() 1036 1037 def process_invocation_node(self, n): 1038 1039 "Process the given invocation node 'n'." 1040 1041 expr = self.process_structure_node(n.node) 1042 objpath = expr.get_origin() 1043 target = None 1044 function = None 1045 instantiation = False 1046 literal_instantiation = False 1047 context_required = True 1048 1049 # Obtain details of the callable. 1050 1051 # Literals may be instantiated specially. 1052 1053 if expr.is_name() and expr.name.startswith("$L") and objpath: 1054 instantiation = literal_instantiation = objpath 1055 parameters = None 1056 target = encode_literal_instantiator(objpath) 1057 context_required = False 1058 1059 # Identified targets employ function pointers directly. 1060 1061 elif objpath: 1062 parameters = self.importer.function_parameters.get(objpath) 1063 1064 # Class invocation involves instantiators. 1065 1066 if expr.has_kind("<class>"): 1067 instantiation = objpath 1068 target = encode_instantiator_pointer(objpath) 1069 target_structure = "&%s" % encode_bound_reference("%s.__init__" % objpath) 1070 context_required = False 1071 1072 # Only plain functions and bound methods employ function pointers. 1073 1074 elif expr.has_kind("<function>"): 1075 function = objpath 1076 1077 # Test for functions and methods. 1078 1079 method_class = self.is_method(objpath) 1080 accessor_kinds = expr.get_accessor_kinds() 1081 instance_accessor = accessor_kinds and \ 1082 len(accessor_kinds) == 1 and \ 1083 first(accessor_kinds) == "<instance>" 1084 1085 if not method_class or instance_accessor: 1086 target = encode_function_pointer(objpath) 1087 target_structure = self.is_method(objpath) and \ 1088 encode_bound_reference(objpath) or \ 1089 "&%s" % encode_path(objpath) 1090 1091 if not method_class: 1092 context_required = False 1093 1094 # Other targets are retrieved at run-time. 1095 1096 else: 1097 parameters = None 1098 1099 # Arguments are presented in a temporary frame array with any context 1100 # always being the first argument. Where it would be unused, it may be 1101 # set to null. 1102 1103 if context_required: 1104 args = ["__CONTEXT_AS_VALUE(__tmp_targets[%d])" % self.function_target] 1105 else: 1106 args = ["(__attr) {0, 0}"] 1107 1108 args += [None] * (not parameters and len(n.args) or parameters and len(parameters) or 0) 1109 kwcodes = [] 1110 kwargs = [] 1111 1112 # Any invocations in the arguments will store target details in a 1113 # different location. 1114 1115 self.function_target += 1 1116 1117 for i, arg in enumerate(n.args): 1118 argexpr = self.process_structure_node(arg) 1119 1120 # Store a keyword argument, either in the argument list or 1121 # in a separate keyword argument list for subsequent lookup. 1122 1123 if isinstance(arg, compiler.ast.Keyword): 1124 1125 # With knowledge of the target, store the keyword 1126 # argument directly. 1127 1128 if parameters: 1129 argnum = parameters.index(arg.name) 1130 args[argnum+1] = str(argexpr) 1131 1132 # Otherwise, store the details in a separate collection. 1133 1134 else: 1135 kwargs.append(str(argexpr)) 1136 kwcodes.append("{%s, %s}" % ( 1137 encode_symbol("ppos", arg.name), 1138 encode_symbol("pcode", arg.name))) 1139 1140 # Store non-keyword arguments in the argument list, rejecting 1141 # superfluous arguments. 1142 1143 else: 1144 try: 1145 args[i+1] = str(argexpr) 1146 except IndexError: 1147 raise TranslateError("Too many arguments specified.", 1148 self.get_namespace_path(), n) 1149 1150 # Reference the current target again. 1151 1152 self.function_target -= 1 1153 1154 # Defaults are added to the frame where arguments are missing. 1155 1156 if parameters: 1157 function_defaults = self.importer.function_defaults.get(objpath) 1158 if function_defaults: 1159 1160 # Visit each default and set any missing arguments. 1161 # Use the target structure to obtain defaults, as opposed to the 1162 # actual function involved. 1163 1164 for i, (argname, default) in enumerate(function_defaults): 1165 argnum = parameters.index(argname) 1166 if not args[argnum+1]: 1167 args[argnum+1] = "__GETDEFAULT(%s, %d)" % (target_structure, i) 1168 1169 # Test for missing arguments. 1170 1171 if None in args: 1172 raise TranslateError("Not all arguments supplied.", 1173 self.get_namespace_path(), n) 1174 1175 # Encode the arguments. 1176 1177 argstr = "__ARGS(%s)" % ", ".join(args) 1178 kwargstr = kwargs and ("__ARGS(%s)" % ", ".join(kwargs)) or "0" 1179 kwcodestr = kwcodes and ("__KWARGS(%s)" % ", ".join(kwcodes)) or "0" 1180 1181 # Where literal instantiation is occurring, add an argument indicating 1182 # the number of values. 1183 1184 if literal_instantiation: 1185 argstr += ", %d" % (len(args) - 1) 1186 1187 # First, the invocation expression is presented. 1188 1189 stages = [] 1190 1191 # Without a known specific callable, the expression provides the target. 1192 1193 if not target or context_required: 1194 stages.append("__tmp_targets[%d] = %s" % (self.function_target, expr)) 1195 1196 # Any specific callable is then obtained. 1197 1198 if target: 1199 stages.append(target) 1200 elif function: 1201 stages.append("__load_via_object(__tmp_targets[%d].value, %s).fn" % ( 1202 self.function_target, encode_symbol("pos", "__fn__"))) 1203 1204 # With a known target, the function is obtained directly and called. 1205 1206 if target or function: 1207 output = "(\n%s\n)(%s)" % (",\n".join(stages), argstr) 1208 1209 # With unknown targets, the generic invocation function is applied to 1210 # the callable and argument collections. 1211 1212 else: 1213 output = "(%s, __invoke(\n__tmp_targets[%d],\n%d, %d, %s, %s,\n%d, %s\n))" % ( 1214 ",\n".join(stages), 1215 self.function_target, 1216 self.always_callable and 1 or 0, 1217 len(kwargs), kwcodestr, kwargstr, 1218 len(args), argstr) 1219 1220 if instantiation: 1221 return TrInstanceRef(instantiation, output) 1222 else: 1223 return make_expression(output) 1224 1225 def always_callable(self, refs): 1226 1227 "Determine whether all 'refs' are callable." 1228 1229 for ref in refs: 1230 if not ref.static(): 1231 return False 1232 else: 1233 origin = ref.final() 1234 if not self.importer.get_attribute(origin, "__fn__"): 1235 return False 1236 return True 1237 1238 def need_default_arguments(self, objpath, nargs): 1239 1240 """ 1241 Return whether any default arguments are needed when invoking the object 1242 given by 'objpath'. 1243 """ 1244 1245 parameters = self.importer.function_parameters.get(objpath) 1246 return nargs < len(parameters) 1247 1248 def process_lambda_node(self, n): 1249 1250 "Process the given lambda node 'n'." 1251 1252 name = self.get_lambda_name() 1253 function_name = self.get_object_path(name) 1254 1255 defaults = self.process_function_defaults(n, name, function_name, "__tmp_value") 1256 1257 # Without defaults, produce an attribute referring to the function. 1258 1259 if not defaults: 1260 return make_expression("((__attr) {0, &%s})" % encode_path(function_name)) 1261 1262 # With defaults, copy the function structure and set the defaults on the 1263 # copy. 1264 1265 else: 1266 return make_expression("(__tmp_value = __COPY(&%s, sizeof(%s)), %s, (__attr) {0, __tmp_value})" % ( 1267 encode_path(function_name), 1268 encode_symbol("obj", function_name), 1269 ", ".join(defaults))) 1270 1271 def process_logical_node(self, n): 1272 1273 """ 1274 Process the given operator node 'n'. 1275 1276 Convert ... to ... 1277 1278 <a> and <b> 1279 (__tmp_result = <a>, !__BOOL(__tmp_result)) ? __tmp_result : <b> 1280 1281 <a> or <b> 1282 (__tmp_result = <a>, __BOOL(__tmp_result)) ? __tmp_result : <b> 1283 """ 1284 1285 if isinstance(n, compiler.ast.And): 1286 op = "!" 1287 else: 1288 op = "" 1289 1290 results = [] 1291 1292 for node in n.nodes[:-1]: 1293 expr = self.process_structure_node(node) 1294 results.append("(__tmp_result = %s, %s__BOOL(__tmp_result)) ? __tmp_result : " % (expr, op)) 1295 1296 expr = self.process_structure_node(n.nodes[-1]) 1297 results.append(str(expr)) 1298 1299 return make_expression("(%s)" % "".join(results)) 1300 1301 def process_name_node(self, n, expr=None): 1302 1303 "Process the given name node 'n' with the optional assignment 'expr'." 1304 1305 # Determine whether the name refers to a static external entity. 1306 1307 if n.name in predefined_constants: 1308 return PredefinedConstantRef(n.name) 1309 1310 # Convert literal references, operator function names, and print 1311 # function names to references. 1312 1313 elif n.name.startswith("$L") or n.name.startswith("$op") or \ 1314 n.name.startswith("$print"): 1315 ref = self.importer.get_module(self.name).special.get(n.name) 1316 return TrResolvedNameRef(n.name, ref) 1317 1318 # Get the appropriate name for the name reference, using the same method 1319 # as in the inspector. 1320 1321 path = self.get_namespace_path() 1322 objpath = self.get_object_path(n.name) 1323 1324 # Determine any assigned globals. 1325 1326 globals = self.importer.get_module(self.name).scope_globals.get(path) 1327 if globals and n.name in globals: 1328 objpath = self.get_global_path(n.name) 1329 1330 # Get the static identity of the name. 1331 1332 ref = self.importer.identify(objpath) 1333 if ref and not ref.get_name(): 1334 ref = ref.alias(objpath) 1335 1336 # Obtain any resolved names for non-assignment names. 1337 1338 if not expr and not ref and self.in_function: 1339 locals = self.importer.function_locals.get(path) 1340 ref = locals and locals.get(n.name) 1341 1342 # Determine whether the name refers to a parameter. The generation of 1343 # parameter references is different from other names. 1344 1345 parameters = self.importer.function_parameters.get(path) 1346 parameter = n.name == "self" and self.in_method() or \ 1347 parameters and n.name in parameters 1348 1349 # Qualified names are used for resolved static references or for 1350 # static namespace members. The reference should be configured to return 1351 # such names. 1352 1353 return TrResolvedNameRef(n.name, ref, expr=expr, parameter=parameter) 1354 1355 def process_not_node(self, n): 1356 1357 "Process the given operator node 'n'." 1358 1359 return make_expression("(__BOOL(%s) ? %s : %s)" % 1360 (self.process_structure_node(n.expr), PredefinedConstantRef("False"), 1361 PredefinedConstantRef("True"))) 1362 1363 def process_raise_node(self, n): 1364 1365 "Process the given raise node 'n'." 1366 1367 # NOTE: Determine which raise statement variants should be permitted. 1368 1369 if n.expr1: 1370 exc = self.process_structure_node(n.expr1) 1371 1372 # Raise instances, testing the kind at run-time if necessary and 1373 # instantiating any non-instance. 1374 1375 if isinstance(exc, TrInstanceRef): 1376 self.writestmt("__Raise(%s);" % exc) 1377 else: 1378 self.writestmt("__Raise(__ensure_instance(%s));" % exc) 1379 else: 1380 self.writestmt("__Complete;") 1381 1382 def process_return_node(self, n): 1383 1384 "Process the given return node 'n'." 1385 1386 expr = self.process_structure_node(n.value) or PredefinedConstantRef("None") 1387 if self.in_try_finally or self.in_try_except: 1388 self.writestmt("__Return(%s);" % expr) 1389 else: 1390 self.writestmt("return %s;" % expr) 1391 1392 return ReturnRef() 1393 1394 def process_try_node(self, n): 1395 1396 """ 1397 Process the given "try...except" node 'n'. 1398 """ 1399 1400 in_try_except = self.in_try_except 1401 self.in_try_except = True 1402 1403 # Use macros to implement exception handling. 1404 1405 self.writestmt("__Try") 1406 self.writeline("{") 1407 self.indent += 1 1408 self.process_structure_node(n.body) 1409 1410 # Put the else statement in another try block that handles any raised 1411 # exceptions and converts them to exceptions that will not be handled by 1412 # the main handling block. 1413 1414 if n.else_: 1415 self.writestmt("__Try") 1416 self.writeline("{") 1417 self.indent += 1 1418 self.process_structure_node(n.else_) 1419 self.indent -= 1 1420 self.writeline("}") 1421 self.writeline("__Catch (__tmp_exc)") 1422 self.writeline("{") 1423 self.indent += 1 1424 self.writeline("if (__tmp_exc.raising) __RaiseElse(__tmp_exc.arg);") 1425 self.writeline("else if (__tmp_exc.completing) __Throw(__tmp_exc);") 1426 self.indent -= 1 1427 self.writeline("}") 1428 1429 # Complete the try block and enter the finally block, if appropriate. 1430 1431 if self.in_try_finally: 1432 self.writestmt("__Complete;") 1433 1434 self.indent -= 1 1435 self.writeline("}") 1436 1437 self.in_try_except = in_try_except 1438 1439 # Handlers are tests within a common handler block. 1440 1441 self.writeline("__Catch (__tmp_exc)") 1442 self.writeline("{") 1443 self.indent += 1 1444 1445 # Introduce an if statement to handle the completion of a try block. 1446 1447 self.process_try_completion() 1448 1449 # Handle exceptions in else blocks converted to __RaiseElse, converting 1450 # them back to normal exceptions. 1451 1452 if n.else_: 1453 self.writeline("else if (__tmp_exc.raising_else) __Raise(__tmp_exc.arg);") 1454 1455 # Exception handling. 1456 1457 for name, var, handler in n.handlers: 1458 1459 # Test for specific exceptions. 1460 1461 if name is not None: 1462 name_ref = self.process_structure_node(name) 1463 self.writeline("else if (__BOOL(__fn_native__isinstance((__attr[]) {{0, 0}, __tmp_exc.arg, %s})))" % name_ref) 1464 else: 1465 self.writeline("else if (1)") 1466 1467 self.writeline("{") 1468 self.indent += 1 1469 1470 # Establish the local for the handler. 1471 1472 if var is not None: 1473 self.writestmt("%s;" % self.process_name_node(var, make_expression("__tmp_exc.arg"))) 1474 1475 if handler is not None: 1476 self.process_structure_node(handler) 1477 1478 self.indent -= 1 1479 self.writeline("}") 1480 1481 # Re-raise unhandled exceptions. 1482 1483 self.writeline("else __Throw(__tmp_exc);") 1484 1485 # End the handler block. 1486 1487 self.indent -= 1 1488 self.writeline("}") 1489 1490 def process_try_finally_node(self, n): 1491 1492 """ 1493 Process the given "try...finally" node 'n'. 1494 """ 1495 1496 in_try_finally = self.in_try_finally 1497 self.in_try_finally = True 1498 1499 # Use macros to implement exception handling. 1500 1501 self.writestmt("__Try") 1502 self.writeline("{") 1503 self.indent += 1 1504 self.process_structure_node(n.body) 1505 self.indent -= 1 1506 self.writeline("}") 1507 1508 self.in_try_finally = in_try_finally 1509 1510 # Finally clauses handle special exceptions. 1511 1512 self.writeline("__Catch (__tmp_exc)") 1513 self.writeline("{") 1514 self.indent += 1 1515 self.process_structure_node(n.final) 1516 1517 # Introduce an if statement to handle the completion of a try block. 1518 1519 self.process_try_completion() 1520 self.writeline("else __Throw(__tmp_exc);") 1521 1522 self.indent -= 1 1523 self.writeline("}") 1524 1525 def process_try_completion(self): 1526 1527 "Generate a test for the completion of a try block." 1528 1529 self.writestmt("if (__tmp_exc.completing)") 1530 self.writeline("{") 1531 self.indent += 1 1532 1533 # Do not return anything at the module level. 1534 1535 if self.get_namespace_path() != self.name: 1536 1537 # Only use the normal return statement if no surrounding try blocks 1538 # apply. 1539 1540 if not self.in_try_finally and not self.in_try_except: 1541 self.writeline("if (!__ISNULL(__tmp_exc.arg)) return __tmp_exc.arg;") 1542 else: 1543 self.writeline("if (!__ISNULL(__tmp_exc.arg)) __Throw(__tmp_exc);") 1544 1545 self.indent -= 1 1546 self.writeline("}") 1547 1548 def process_while_node(self, n): 1549 1550 "Process the given while node 'n'." 1551 1552 self.writeline("while (1)") 1553 self.writeline("{") 1554 self.indent += 1 1555 test = self.process_structure_node(n.test) 1556 1557 # Emit the loop termination condition unless "while <true value>" is 1558 # indicated. 1559 1560 if not (isinstance(test, PredefinedConstantRef) and test.value): 1561 1562 # NOTE: This needs to evaluate whether the operand is true or false 1563 # NOTE: according to Python rules. 1564 1565 self.writeline("if (!__BOOL(%s))" % test) 1566 self.writeline("{") 1567 self.indent += 1 1568 if n.else_: 1569 self.process_structure_node(n.else_) 1570 self.writestmt("break;") 1571 self.indent -= 1 1572 self.writeline("}") 1573 1574 in_conditional = self.in_conditional 1575 self.in_conditional = True 1576 self.process_structure_node(n.body) 1577 self.in_conditional = in_conditional 1578 1579 self.indent -= 1 1580 self.writeline("}") 1581 1582 # Output generation. 1583 1584 def start_output(self): 1585 1586 "Write the declarations at the top of each source file." 1587 1588 print >>self.out, """\ 1589 #include "types.h" 1590 #include "exceptions.h" 1591 #include "ops.h" 1592 #include "progconsts.h" 1593 #include "progops.h" 1594 #include "progtypes.h" 1595 #include "main.h" 1596 """ 1597 1598 def start_module(self): 1599 1600 "Write the start of each module's main function." 1601 1602 print >>self.out, "void __main_%s()" % encode_path(self.name) 1603 print >>self.out, "{" 1604 self.indent += 1 1605 self.write_temporaries(self.importer.function_targets.get(self.name)) 1606 1607 def end_module(self): 1608 1609 "End each module by closing its main function." 1610 1611 self.indent -= 1 1612 print >>self.out, "}" 1613 1614 def start_function(self, name): 1615 1616 "Start the function having the given 'name'." 1617 1618 print >>self.out, "__attr %s(__attr __args[])" % encode_function_pointer(name) 1619 print >>self.out, "{" 1620 self.indent += 1 1621 self.write_temporaries(self.importer.function_targets.get(name)) 1622 1623 # Obtain local names from parameters. 1624 1625 parameters = self.importer.function_parameters[name] 1626 locals = self.importer.function_locals[name].keys() 1627 names = [] 1628 1629 for n in locals: 1630 1631 # Filter out special names and parameters. Note that self is a local 1632 # regardless of whether it originally appeared in the parameters or 1633 # not. 1634 1635 if n.startswith("$l") or n in parameters or n == "self": 1636 continue 1637 names.append(encode_path(n)) 1638 1639 # Emit required local names. 1640 1641 if names: 1642 names.sort() 1643 self.writeline("__attr %s;" % ", ".join(names)) 1644 1645 self.write_parameters(name) 1646 1647 def end_function(self, name): 1648 1649 "End the function having the given 'name'." 1650 1651 self.indent -= 1 1652 print >>self.out, "}" 1653 print >>self.out 1654 1655 def write_temporaries(self, targets): 1656 1657 """ 1658 Write temporary storage employed by functions, providing space for the 1659 given number of 'targets'. 1660 """ 1661 1662 targets = targets is not None and "__tmp_targets[%d], " % targets or "" 1663 1664 self.writeline("__ref __tmp_context, __tmp_value;") 1665 self.writeline("__attr %s__tmp_result;" % targets) 1666 self.writeline("__exc __tmp_exc;") 1667 1668 def write_parameters(self, name): 1669 1670 """ 1671 For the function having the given 'name', write definitions of 1672 parameters found in the arguments array. 1673 """ 1674 1675 parameters = self.importer.function_parameters[name] 1676 1677 # Generate any self reference. 1678 1679 if self.is_method(name): 1680 self.writeline("__attr * const self = &__args[0];") 1681 1682 # Generate aliases for the parameters. 1683 1684 for i, parameter in enumerate(parameters): 1685 self.writeline("__attr * const %s = &__args[%d];" % (encode_path(parameter), i+1)) 1686 1687 def start_if(self, first, test_ref): 1688 self.writestmt("%sif (__BOOL(%s))" % (not first and "else " or "", test_ref)) 1689 self.writeline("{") 1690 self.indent += 1 1691 1692 def end_if(self): 1693 self.indent -= 1 1694 self.writeline("}") 1695 1696 def start_else(self): 1697 self.writeline("else") 1698 self.writeline("{") 1699 self.indent += 1 1700 1701 def end_else(self): 1702 self.indent -= 1 1703 self.writeline("}") 1704 1705 def statement(self, expr): 1706 # NOTE: Should never be None. 1707 if not expr: 1708 self.writestmt("...;") 1709 s = str(expr) 1710 if s: 1711 self.writestmt("%s;" % s) 1712 1713 def statements(self, results): 1714 for result in results: 1715 self.statement(result) 1716 1717 def writeline(self, s): 1718 print >>self.out, "%s%s" % (self.pad(), self.indenttext(s, self.indent + 1)) 1719 1720 def writestmt(self, s): 1721 print >>self.out 1722 self.writeline(s) 1723 1724 def write_comment(self, s): 1725 self.writestmt("/* %s */" % s) 1726 1727 def pad(self, extra=0): 1728 return (self.indent + extra) * self.tabstop 1729 1730 def indenttext(self, s, levels): 1731 lines = s.split("\n") 1732 out = [lines[0]] 1733 for line in lines[1:]: 1734 out.append(levels * self.tabstop + line) 1735 if line.endswith("("): 1736 levels += 1 1737 elif line.startswith(")"): 1738 levels -= 1 1739 return "\n".join(out) 1740 1741 # vim: tabstop=4 expandtab shiftwidth=4