| 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 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 // constructor to ensure the redirectionTarget has been computed | 627 // constructor to ensure the redirectionTarget has been computed |
| 628 // correctly. Find a way to avoid this. | 628 // correctly. Find a way to avoid this. |
| 629 compiler.analyzeElement(constructor.declaration); | 629 compiler.analyzeElement(constructor.declaration); |
| 630 | 630 |
| 631 InterfaceType type = elements.getType(node); | 631 InterfaceType type = elements.getType(node); |
| 632 List<Constant> evaluateArguments(FunctionElement constructor) { | 632 List<Constant> evaluateArguments(FunctionElement constructor) { |
| 633 Selector selector = elements.getSelector(send); | 633 Selector selector = elements.getSelector(send); |
| 634 return evaluateArgumentsToConstructor( | 634 return evaluateArgumentsToConstructor( |
| 635 node, selector, send.arguments, constructor); | 635 node, selector, send.arguments, constructor); |
| 636 } | 636 } |
| 637 return makeConstructedConstant(node, type, constructor, evaluateArguments); | 637 |
| 638 if (constructor == compiler.intEnvironment |
| 639 || constructor == compiler.boolEnvironment |
| 640 || constructor == compiler.stringEnvironment) { |
| 641 List<Constant> arguments = evaluateArguments(constructor); |
| 642 var firstArgument = arguments[0]; |
| 643 Constant defaultValue = arguments[1]; |
| 644 |
| 645 if (firstArgument is NullConstant) { |
| 646 compiler.reportFatalError( |
| 647 send.arguments.head, MessageKind.NULL_NOT_ALLOWED); |
| 648 } |
| 649 |
| 650 if (firstArgument is! StringConstant) { |
| 651 DartType type = defaultValue.computeType(compiler); |
| 652 compiler.reportFatalError( |
| 653 send.arguments.head, MessageKind.NOT_ASSIGNABLE.error, |
| 654 {'fromType': type, 'toType': compiler.stringClass.rawType}); |
| 655 } |
| 656 |
| 657 if (constructor == compiler.intEnvironment |
| 658 && !(defaultValue is NullConstant || defaultValue is IntConstant)) { |
| 659 DartType type = defaultValue.computeType(compiler); |
| 660 compiler.reportFatalError( |
| 661 send.arguments.tail.head, MessageKind.NOT_ASSIGNABLE.error, |
| 662 {'fromType': type, 'toType': compiler.intClass.rawType}); |
| 663 } |
| 664 |
| 665 if (constructor == compiler.boolEnvironment |
| 666 && !(defaultValue is NullConstant || defaultValue is BoolConstant)) { |
| 667 DartType type = defaultValue.computeType(compiler); |
| 668 compiler.reportFatalError( |
| 669 send.arguments.tail.head, MessageKind.NOT_ASSIGNABLE.error, |
| 670 {'fromType': type, 'toType': compiler.boolClass.rawType}); |
| 671 } |
| 672 |
| 673 if (constructor == compiler.stringEnvironment |
| 674 && !(defaultValue is NullConstant |
| 675 || defaultValue is StringConstant)) { |
| 676 DartType type = defaultValue.computeType(compiler); |
| 677 compiler.reportFatalError( |
| 678 send.arguments.tail.head, MessageKind.NOT_ASSIGNABLE.error, |
| 679 {'fromType': type, 'toType': compiler.stringClass.rawType}); |
| 680 } |
| 681 |
| 682 String value = |
| 683 compiler.fromEnvironment(firstArgument.value.slowToString()); |
| 684 |
| 685 if (value == null) { |
| 686 return defaultValue; |
| 687 } else if (constructor == compiler.intEnvironment) { |
| 688 int number = int.parse(value, onError: (_) => null); |
| 689 return (number == null) |
| 690 ? defaultValue |
| 691 : constantSystem.createInt(number); |
| 692 } else if (constructor == compiler.boolEnvironment) { |
| 693 if (value == 'true') { |
| 694 return constantSystem.createBool(true); |
| 695 } else if (value == 'false') { |
| 696 return constantSystem.createBool(false); |
| 697 } else { |
| 698 return defaultValue; |
| 699 } |
| 700 } else { |
| 701 assert(constructor == compiler.stringEnvironment); |
| 702 return constantSystem.createString(new DartString.literal(value), node); |
| 703 } |
| 704 } else { |
| 705 return makeConstructedConstant( |
| 706 node, type, constructor, evaluateArguments); |
| 707 } |
| 638 } | 708 } |
| 639 | 709 |
| 640 Constant makeConstructedConstant( | 710 Constant makeConstructedConstant( |
| 641 Spannable node, InterfaceType type, FunctionElement constructor, | 711 Spannable node, InterfaceType type, FunctionElement constructor, |
| 642 List<Constant> getArguments(FunctionElement constructor)) { | 712 List<Constant> getArguments(FunctionElement constructor)) { |
| 643 if (constructor.isRedirectingFactory) { | 713 if (constructor.isRedirectingFactory) { |
| 644 type = constructor.computeTargetType(compiler, type); | 714 type = constructor.computeTargetType(compiler, type); |
| 645 } | 715 } |
| 646 | 716 |
| 647 // The redirection chain of this element may not have been resolved through | 717 // The redirection chain of this element may not have been resolved through |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 881 if (fieldValue == null) { | 951 if (fieldValue == null) { |
| 882 // Use the default value. | 952 // Use the default value. |
| 883 fieldValue = handler.compileConstant(field); | 953 fieldValue = handler.compileConstant(field); |
| 884 } | 954 } |
| 885 jsNewArguments.add(fieldValue); | 955 jsNewArguments.add(fieldValue); |
| 886 }, | 956 }, |
| 887 includeSuperAndInjectedMembers: true); | 957 includeSuperAndInjectedMembers: true); |
| 888 return jsNewArguments; | 958 return jsNewArguments; |
| 889 } | 959 } |
| 890 } | 960 } |
| OLD | NEW |