Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: sdk/lib/_internal/compiler/implementation/compile_time_constants.dart

Issue 48323003: Implement fromEnvironment on bool, int, String in dart2js. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 return (value == 'true')
kasperl 2013/10/31 09:11:51 return constantSystem.createBool(value == 'true');
ngeoffray 2013/10/31 09:32:24 Done.
kasperl 2013/10/31 12:33:05 I just discussed this with Lars and we'd prefer to
ngeoffray 2013/10/31 12:53:18 Done.
694 ? constantSystem.createBool(true)
695 : constantSystem.createBool(false);
696 } else {
697 assert(constructor == compiler.stringEnvironment);
698 return constantSystem.createString(new DartString.literal(value), node);
699 }
700 } else {
701 return makeConstructedConstant(
702 node, type, constructor, evaluateArguments);
703 }
638 } 704 }
639 705
640 Constant makeConstructedConstant( 706 Constant makeConstructedConstant(
641 Spannable node, InterfaceType type, FunctionElement constructor, 707 Spannable node, InterfaceType type, FunctionElement constructor,
642 List<Constant> getArguments(FunctionElement constructor)) { 708 List<Constant> getArguments(FunctionElement constructor)) {
643 if (constructor.isRedirectingFactory) { 709 if (constructor.isRedirectingFactory) {
644 type = constructor.computeTargetType(compiler, type); 710 type = constructor.computeTargetType(compiler, type);
645 } 711 }
646 712
647 // The redirection chain of this element may not have been resolved through 713 // The redirection chain of this element may not have been resolved through
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 if (fieldValue == null) { 947 if (fieldValue == null) {
882 // Use the default value. 948 // Use the default value.
883 fieldValue = handler.compileConstant(field); 949 fieldValue = handler.compileConstant(field);
884 } 950 }
885 jsNewArguments.add(fieldValue); 951 jsNewArguments.add(fieldValue);
886 }, 952 },
887 includeSuperAndInjectedMembers: true); 953 includeSuperAndInjectedMembers: true);
888 return jsNewArguments; 954 return jsNewArguments;
889 } 955 }
890 } 956 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698