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