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