Lichen

Annotated translator.py

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