Lichen

Annotated translator.py

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