| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart2js; | 5 part of dart2js; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The [ConstantHandler] keeps track of compile-time constants, | 8 * The [ConstantHandler] keeps track of compile-time constants, |
| 9 * initializations of global and static fields, and default values of | 9 * initializations of global and static fields, and default values of |
| 10 * optional parameters. | 10 * optional parameters. |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 InterfaceType type = new InterfaceType(classElement, typeArgument); | 426 InterfaceType type = new InterfaceType(classElement, typeArgument); |
| 427 handler.registerInstantiatedType(type, elements); | 427 handler.registerInstantiatedType(type, elements); |
| 428 Constant constant = new MapConstant(type, keysList, values, protoValue); | 428 Constant constant = new MapConstant(type, keysList, values, protoValue); |
| 429 handler.registerCompileTimeConstant(constant, elements); | 429 handler.registerCompileTimeConstant(constant, elements); |
| 430 return constant; | 430 return constant; |
| 431 } | 431 } |
| 432 | 432 |
| 433 Constant visitLiteralNull(LiteralNull node) { | 433 Constant visitLiteralNull(LiteralNull node) { |
| 434 return constantSystem.createNull(); | 434 return constantSystem.createNull(); |
| 435 } | 435 } |
| 436 | 436 |
| 437 Constant visitLiteralString(LiteralString node) { | 437 Constant visitLiteralString(LiteralString node) { |
| 438 handler.registerStringInstance(elements); | 438 handler.registerStringInstance(elements); |
| 439 return constantSystem.createString(node.dartString, node); | 439 return constantSystem.createString(node.dartString, node); |
| 440 } | 440 } |
| 441 | 441 |
| 442 Constant visitStringJuxtaposition(StringJuxtaposition node) { | 442 Constant visitStringJuxtaposition(StringJuxtaposition node) { |
| 443 StringConstant left = evaluate(node.first); | 443 StringConstant left = evaluate(node.first); |
| 444 StringConstant right = evaluate(node.second); | 444 StringConstant right = evaluate(node.second); |
| 445 if (left == null || right == null) return null; | 445 if (left == null || right == null) return null; |
| 446 handler.registerStringInstance(elements); | 446 handler.registerStringInstance(elements); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 467 return signalNotCompileTimeConstant(part.expression); | 467 return signalNotCompileTimeConstant(part.expression); |
| 468 } | 468 } |
| 469 accumulator = new DartString.concat(accumulator, expressionString); | 469 accumulator = new DartString.concat(accumulator, expressionString); |
| 470 StringConstant partString = evaluate(part.string); | 470 StringConstant partString = evaluate(part.string); |
| 471 if (partString == null) return null; | 471 if (partString == null) return null; |
| 472 accumulator = new DartString.concat(accumulator, partString.value); | 472 accumulator = new DartString.concat(accumulator, partString.value); |
| 473 }; | 473 }; |
| 474 handler.registerStringInstance(elements); | 474 handler.registerStringInstance(elements); |
| 475 return constantSystem.createString(accumulator, node); | 475 return constantSystem.createString(accumulator, node); |
| 476 } | 476 } |
| 477 |
| 478 Constant visitSymbolLiteral(SymbolLiteral node) { |
| 479 InterfaceType type = compiler.symbolClass.computeType(compiler); |
| 480 List<Constant> createArguments(_) { |
| 481 return [constantSystem.createString( |
| 482 new DartString.literal(node.nameString), node)]; |
| 483 } |
| 484 return makeConstructedConstant( |
| 485 node, type, compiler.symbolConstructor, createArguments); |
| 486 } |
| 477 | 487 |
| 478 Constant makeTypeConstant(Element element) { | 488 Constant makeTypeConstant(Element element) { |
| 479 DartType elementType = element.computeType(compiler).asRaw(); | 489 DartType elementType = element.computeType(compiler).asRaw(); |
| 480 compiler.backend.registerTypeLiteral(element, elements); | 490 compiler.backend.registerTypeLiteral(element, elements); |
| 481 DartType constantType = | 491 DartType constantType = |
| 482 compiler.backend.typeImplementation.computeType(compiler); | 492 compiler.backend.typeImplementation.computeType(compiler); |
| 483 Constant constant = new TypeConstant(elementType, constantType); | 493 Constant constant = new TypeConstant(elementType, constantType); |
| 484 // If we use a type literal in a constant, the compile time | 494 // If we use a type literal in a constant, the compile time |
| 485 // constant emitter will generate a call to the createRuntimeType | 495 // constant emitter will generate a call to the createRuntimeType |
| 486 // helper so we register a use of that. | 496 // helper so we register a use of that. |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 } | 690 } |
| 681 | 691 |
| 682 Send send = node.send; | 692 Send send = node.send; |
| 683 FunctionElement constructor = elements[send]; | 693 FunctionElement constructor = elements[send]; |
| 684 // TODO(ahe): This is nasty: we must eagerly analyze the | 694 // TODO(ahe): This is nasty: we must eagerly analyze the |
| 685 // constructor to ensure the redirectionTarget has been computed | 695 // constructor to ensure the redirectionTarget has been computed |
| 686 // correctly. Find a way to avoid this. | 696 // correctly. Find a way to avoid this. |
| 687 compiler.analyzeElement(constructor.declaration); | 697 compiler.analyzeElement(constructor.declaration); |
| 688 | 698 |
| 689 InterfaceType type = elements.getType(node); | 699 InterfaceType type = elements.getType(node); |
| 700 List<Constant> evaluateArguments(FunctionElement constructor) { |
| 701 Selector selector = elements.getSelector(send); |
| 702 return evaluateArgumentsToConstructor( |
| 703 node, selector, send.arguments, constructor); |
| 704 } |
| 705 return makeConstructedConstant(node, type, constructor, evaluateArguments); |
| 706 } |
| 707 |
| 708 Constant makeConstructedConstant( |
| 709 Node node, InterfaceType type, FunctionElement constructor, |
| 710 List<Constant> getArguments(FunctionElement constructor)) { |
| 690 if (constructor.isRedirectingFactory) { | 711 if (constructor.isRedirectingFactory) { |
| 691 type = constructor.computeTargetType(compiler, type); | 712 type = constructor.computeTargetType(compiler, type); |
| 692 } | 713 } |
| 693 | 714 |
| 694 constructor = constructor.redirectionTarget; | 715 constructor = constructor.redirectionTarget; |
| 695 ClassElement classElement = constructor.getEnclosingClass(); | 716 ClassElement classElement = constructor.getEnclosingClass(); |
| 696 // The constructor must be an implementation to ensure that field | 717 // The constructor must be an implementation to ensure that field |
| 697 // initializers are handled correctly. | 718 // initializers are handled correctly. |
| 698 constructor = constructor.implementation; | 719 constructor = constructor.implementation; |
| 699 assert(invariant(node, constructor.isImplementation)); | 720 assert(invariant(node, constructor.isImplementation)); |
| 700 | 721 |
| 701 Selector selector = elements.getSelector(send); | 722 List<Constant> arguments = getArguments(constructor); |
| 702 List<Constant> arguments = evaluateArgumentsToConstructor( | |
| 703 node, selector, send.arguments, constructor); | |
| 704 ConstructorEvaluator evaluator = | 723 ConstructorEvaluator evaluator = |
| 705 new ConstructorEvaluator(constructor, handler, compiler); | 724 new ConstructorEvaluator(constructor, handler, compiler); |
| 706 evaluator.evaluateConstructorFieldValues(arguments); | 725 evaluator.evaluateConstructorFieldValues(arguments); |
| 707 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); | 726 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); |
| 708 | 727 |
| 709 handler.registerInstantiatedType(type, elements); | 728 handler.registerInstantiatedType(type, elements); |
| 710 Constant constant = new ConstructedConstant(type, jsNewArguments); | 729 Constant constant = new ConstructedConstant(type, jsNewArguments); |
| 711 handler.registerCompileTimeConstant(constant, elements); | 730 handler.registerCompileTimeConstant(constant, elements); |
| 712 return constant; | 731 return constant; |
| 713 } | 732 } |
| 714 | 733 |
| 715 Constant visitParenthesizedExpression(ParenthesizedExpression node) { | 734 Constant visitParenthesizedExpression(ParenthesizedExpression node) { |
| 716 return node.expression.accept(this); | 735 return node.expression.accept(this); |
| 717 } | 736 } |
| 718 | 737 |
| 719 error(Node node) { | 738 error(Node node) { |
| 720 // TODO(floitsch): get the list of constants that are currently compiled | 739 // TODO(floitsch): get the list of constants that are currently compiled |
| 721 // and present some kind of stack-trace. | 740 // and present some kind of stack-trace. |
| 722 compiler.reportFatalError( | 741 compiler.reportFatalError( |
| 723 node, MessageKind.NOT_A_COMPILE_TIME_CONSTANT); | 742 node, MessageKind.NOT_A_COMPILE_TIME_CONSTANT); |
| 724 } | 743 } |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 928 if (fieldValue == null) { | 947 if (fieldValue == null) { |
| 929 // Use the default value. | 948 // Use the default value. |
| 930 fieldValue = handler.compileConstant(field); | 949 fieldValue = handler.compileConstant(field); |
| 931 } | 950 } |
| 932 jsNewArguments.add(fieldValue); | 951 jsNewArguments.add(fieldValue); |
| 933 }, | 952 }, |
| 934 includeSuperAndInjectedMembers: true); | 953 includeSuperAndInjectedMembers: true); |
| 935 return jsNewArguments; | 954 return jsNewArguments; |
| 936 } | 955 } |
| 937 } | 956 } |
| OLD | NEW |