| 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 library dart2js.compile_time_constant_evaluator; | 5 library dart2js.compile_time_constant_evaluator; |
| 6 | 6 |
| 7 import 'common.dart'; | 7 import 'common.dart'; |
| 8 import 'common/resolution.dart' show Resolution; | 8 import 'common/resolution.dart' show Resolution; |
| 9 import 'common/tasks.dart' show CompilerTask, Measurer; | 9 import 'common/tasks.dart' show CompilerTask, Measurer; |
| 10 import 'compiler.dart' show Compiler; | 10 import 'compiler.dart' show Compiler; |
| (...skipping 909 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 920 } | 920 } |
| 921 | 921 |
| 922 normalizedArguments = evaluateArgumentsToConstructor( | 922 normalizedArguments = evaluateArgumentsToConstructor( |
| 923 node, callStructure, arguments, implementation, | 923 node, callStructure, arguments, implementation, |
| 924 compileArgument: (node) => concreteArgumentMap[node]); | 924 compileArgument: (node) => concreteArgumentMap[node]); |
| 925 concreteArguments = concreteArgumentMap.values.toList(); | 925 concreteArguments = concreteArgumentMap.values.toList(); |
| 926 } else { | 926 } else { |
| 927 assert(normalizedArguments != null); | 927 assert(normalizedArguments != null); |
| 928 concreteArguments = normalizedArguments; | 928 concreteArguments = normalizedArguments; |
| 929 } | 929 } |
| 930 | 930 if (constructor.isFromEnvironmentConstructor) { |
| 931 if (constructor == compiler.intEnvironment || | |
| 932 constructor == compiler.boolEnvironment || | |
| 933 constructor == compiler.stringEnvironment) { | |
| 934 return createFromEnvironmentConstant(node, constructedType, constructor, | 931 return createFromEnvironmentConstant(node, constructedType, constructor, |
| 935 callStructure, normalizedArguments, concreteArguments); | 932 callStructure, normalizedArguments, concreteArguments); |
| 936 } else { | 933 } else { |
| 937 return makeConstructedConstant( | 934 return makeConstructedConstant( |
| 938 compiler, | 935 compiler, |
| 939 handler, | 936 handler, |
| 940 context, | 937 context, |
| 941 node, | 938 node, |
| 942 type, | 939 type, |
| 943 constructor, | 940 constructor, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 967 | 964 |
| 968 if (!firstArgument.isString) { | 965 if (!firstArgument.isString) { |
| 969 DartType type = defaultValue.getType(coreTypes); | 966 DartType type = defaultValue.getType(coreTypes); |
| 970 reporter.reportErrorMessage( | 967 reporter.reportErrorMessage( |
| 971 normalizedArguments[0].node, | 968 normalizedArguments[0].node, |
| 972 MessageKind.NOT_ASSIGNABLE, | 969 MessageKind.NOT_ASSIGNABLE, |
| 973 {'fromType': type, 'toType': coreTypes.stringType}); | 970 {'fromType': type, 'toType': coreTypes.stringType}); |
| 974 return null; | 971 return null; |
| 975 } | 972 } |
| 976 | 973 |
| 977 if (constructor == compiler.intEnvironment && | 974 if (constructor.isIntFromEnvironmentConstructor && |
| 978 !(defaultValue.isNull || defaultValue.isInt)) { | 975 !(defaultValue.isNull || defaultValue.isInt)) { |
| 979 DartType type = defaultValue.getType(coreTypes); | 976 DartType type = defaultValue.getType(coreTypes); |
| 980 reporter.reportErrorMessage( | 977 reporter.reportErrorMessage( |
| 981 normalizedArguments[1].node, | 978 normalizedArguments[1].node, |
| 982 MessageKind.NOT_ASSIGNABLE, | 979 MessageKind.NOT_ASSIGNABLE, |
| 983 {'fromType': type, 'toType': coreTypes.intType}); | 980 {'fromType': type, 'toType': coreTypes.intType}); |
| 984 return null; | 981 return null; |
| 985 } | 982 } |
| 986 | 983 |
| 987 if (constructor == compiler.boolEnvironment && | 984 if (constructor.isBoolFromEnvironmentConstructor && |
| 988 !(defaultValue.isNull || defaultValue.isBool)) { | 985 !(defaultValue.isNull || defaultValue.isBool)) { |
| 989 DartType type = defaultValue.getType(coreTypes); | 986 DartType type = defaultValue.getType(coreTypes); |
| 990 reporter.reportErrorMessage( | 987 reporter.reportErrorMessage( |
| 991 normalizedArguments[1].node, | 988 normalizedArguments[1].node, |
| 992 MessageKind.NOT_ASSIGNABLE, | 989 MessageKind.NOT_ASSIGNABLE, |
| 993 {'fromType': type, 'toType': coreTypes.boolType}); | 990 {'fromType': type, 'toType': coreTypes.boolType}); |
| 994 return null; | 991 return null; |
| 995 } | 992 } |
| 996 | 993 |
| 997 if (constructor == compiler.stringEnvironment && | 994 if (constructor.isStringFromEnvironmentConstructor && |
| 998 !(defaultValue.isNull || defaultValue.isString)) { | 995 !(defaultValue.isNull || defaultValue.isString)) { |
| 999 DartType type = defaultValue.getType(coreTypes); | 996 DartType type = defaultValue.getType(coreTypes); |
| 1000 reporter.reportErrorMessage( | 997 reporter.reportErrorMessage( |
| 1001 normalizedArguments[1].node, | 998 normalizedArguments[1].node, |
| 1002 MessageKind.NOT_ASSIGNABLE, | 999 MessageKind.NOT_ASSIGNABLE, |
| 1003 {'fromType': type, 'toType': coreTypes.stringType}); | 1000 {'fromType': type, 'toType': coreTypes.stringType}); |
| 1004 return null; | 1001 return null; |
| 1005 } | 1002 } |
| 1006 | 1003 |
| 1007 String name = firstArgument.primitiveValue.slowToString(); | 1004 String name = firstArgument.primitiveValue.slowToString(); |
| 1008 String value = compiler.fromEnvironment(name); | 1005 String value = compiler.fromEnvironment(name); |
| 1009 | 1006 |
| 1010 AstConstant createEvaluatedConstant(ConstantValue value) { | 1007 AstConstant createEvaluatedConstant(ConstantValue value) { |
| 1011 ConstantExpression expression; | 1008 ConstantExpression expression; |
| 1012 ConstantExpression name = concreteArguments[0].expression; | 1009 ConstantExpression name = concreteArguments[0].expression; |
| 1013 ConstantExpression defaultValue; | 1010 ConstantExpression defaultValue; |
| 1014 if (concreteArguments.length > 1) { | 1011 if (concreteArguments.length > 1) { |
| 1015 defaultValue = concreteArguments[1].expression; | 1012 defaultValue = concreteArguments[1].expression; |
| 1016 } | 1013 } |
| 1017 if (constructor == compiler.intEnvironment) { | 1014 if (constructor.isIntFromEnvironmentConstructor) { |
| 1018 expression = | 1015 expression = |
| 1019 new IntFromEnvironmentConstantExpression(name, defaultValue); | 1016 new IntFromEnvironmentConstantExpression(name, defaultValue); |
| 1020 } else if (constructor == compiler.boolEnvironment) { | 1017 } else if (constructor.isBoolFromEnvironmentConstructor) { |
| 1021 expression = | 1018 expression = |
| 1022 new BoolFromEnvironmentConstantExpression(name, defaultValue); | 1019 new BoolFromEnvironmentConstantExpression(name, defaultValue); |
| 1023 } else if (constructor == compiler.stringEnvironment) { | 1020 } else if (constructor.isStringFromEnvironmentConstructor) { |
| 1024 expression = | 1021 expression = |
| 1025 new StringFromEnvironmentConstantExpression(name, defaultValue); | 1022 new StringFromEnvironmentConstantExpression(name, defaultValue); |
| 1026 } | 1023 } |
| 1027 assert(expression != null); | 1024 assert(expression != null); |
| 1028 return new AstConstant(context, node, expression, value); | 1025 return new AstConstant(context, node, expression, value); |
| 1029 } | 1026 } |
| 1030 | 1027 |
| 1031 if (value == null) { | 1028 if (value == null) { |
| 1032 return createEvaluatedConstant(defaultValue); | 1029 return createEvaluatedConstant(defaultValue); |
| 1033 } else if (constructor == compiler.intEnvironment) { | 1030 } else if (constructor.isIntFromEnvironmentConstructor) { |
| 1034 int number = int.parse(value, onError: (_) => null); | 1031 int number = int.parse(value, onError: (_) => null); |
| 1035 return createEvaluatedConstant( | 1032 return createEvaluatedConstant( |
| 1036 (number == null) ? defaultValue : constantSystem.createInt(number)); | 1033 (number == null) ? defaultValue : constantSystem.createInt(number)); |
| 1037 } else if (constructor == compiler.boolEnvironment) { | 1034 } else if (constructor.isBoolFromEnvironmentConstructor) { |
| 1038 if (value == 'true') { | 1035 if (value == 'true') { |
| 1039 return createEvaluatedConstant(constantSystem.createBool(true)); | 1036 return createEvaluatedConstant(constantSystem.createBool(true)); |
| 1040 } else if (value == 'false') { | 1037 } else if (value == 'false') { |
| 1041 return createEvaluatedConstant(constantSystem.createBool(false)); | 1038 return createEvaluatedConstant(constantSystem.createBool(false)); |
| 1042 } else { | 1039 } else { |
| 1043 return createEvaluatedConstant(defaultValue); | 1040 return createEvaluatedConstant(defaultValue); |
| 1044 } | 1041 } |
| 1045 } else { | 1042 } else { |
| 1046 assert(constructor == compiler.stringEnvironment); | 1043 assert(constructor.isStringFromEnvironmentConstructor); |
| 1047 return createEvaluatedConstant( | 1044 return createEvaluatedConstant( |
| 1048 constantSystem.createString(new DartString.literal(value))); | 1045 constantSystem.createString(new DartString.literal(value))); |
| 1049 } | 1046 } |
| 1050 } | 1047 } |
| 1051 | 1048 |
| 1052 static AstConstant makeConstructedConstant( | 1049 static AstConstant makeConstructedConstant( |
| 1053 Compiler compiler, | 1050 Compiler compiler, |
| 1054 ConstantCompilerBase handler, | 1051 ConstantCompilerBase handler, |
| 1055 Element context, | 1052 Element context, |
| 1056 Node node, | 1053 Node node, |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1371 class _CompilerEnvironment implements Environment { | 1368 class _CompilerEnvironment implements Environment { |
| 1372 final Compiler compiler; | 1369 final Compiler compiler; |
| 1373 | 1370 |
| 1374 _CompilerEnvironment(this.compiler); | 1371 _CompilerEnvironment(this.compiler); |
| 1375 | 1372 |
| 1376 @override | 1373 @override |
| 1377 String readFromEnvironment(String name) { | 1374 String readFromEnvironment(String name) { |
| 1378 return compiler.fromEnvironment(name); | 1375 return compiler.fromEnvironment(name); |
| 1379 } | 1376 } |
| 1380 } | 1377 } |
| OLD | NEW |