Lichen

Annotated translator.py

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