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 'constant_system_dart.dart'; | 7 import 'constant_system_dart.dart'; |
8 import 'constants/constant_system.dart'; | 8 import 'constants/constant_system.dart'; |
9 import 'constants/expressions.dart'; | 9 import 'constants/expressions.dart'; |
10 import 'constants/values.dart'; | 10 import 'constants/values.dart'; |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 } else if (Elements.isClass(element) || Elements.isTypedef(element)) { | 528 } else if (Elements.isClass(element) || Elements.isTypedef(element)) { |
529 assert(elements.isTypeLiteral(send)); | 529 assert(elements.isTypeLiteral(send)); |
530 result = makeTypeConstant(elements.getTypeLiteralType(send)); | 530 result = makeTypeConstant(elements.getTypeLiteralType(send)); |
531 } else if (send.receiver != null) { | 531 } else if (send.receiver != null) { |
532 if (send.selector.asIdentifier().source == "length") { | 532 if (send.selector.asIdentifier().source == "length") { |
533 AstConstant left = evaluate(send.receiver); | 533 AstConstant left = evaluate(send.receiver); |
534 if (left != null && left.value.isString) { | 534 if (left != null && left.value.isString) { |
535 StringConstantValue stringConstantValue = left.value; | 535 StringConstantValue stringConstantValue = left.value; |
536 DartString string = stringConstantValue.primitiveValue; | 536 DartString string = stringConstantValue.primitiveValue; |
537 IntConstantValue length = constantSystem.createInt(string.length); | 537 IntConstantValue length = constantSystem.createInt(string.length); |
538 result = new VariableConstantExpression(length, element); | 538 result = |
| 539 new StringLengthConstantExpression(length, left.expression); |
539 } | 540 } |
540 } | 541 } |
541 // Fall through to error handling. | 542 // Fall through to error handling. |
542 } else if (!Elements.isUnresolved(element) | 543 } else if (!Elements.isUnresolved(element) |
543 && element.isVariable | 544 && element.isVariable |
544 && element.isConst) { | 545 && element.isConst) { |
545 ConstantExpression variableExpression = | 546 ConstantExpression variableExpression = |
546 handler.compileConstant(element); | 547 handler.compileConstant(element); |
547 if (variableExpression != null) { | 548 if (variableExpression != null) { |
548 result = new VariableConstantExpression(variableExpression.value, | 549 result = new VariableConstantExpression(variableExpression.value, |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
873 } | 874 } |
874 | 875 |
875 String name = | 876 String name = |
876 firstArgument.primitiveValue.slowToString(); | 877 firstArgument.primitiveValue.slowToString(); |
877 String value = | 878 String value = |
878 compiler.fromEnvironment(name); | 879 compiler.fromEnvironment(name); |
879 | 880 |
880 AstConstant createEvaluatedConstant(ConstantValue value) { | 881 AstConstant createEvaluatedConstant(ConstantValue value) { |
881 | 882 |
882 ConstantExpression expression; | 883 ConstantExpression expression; |
| 884 ConstantExpression name = concreteArguments[0].expression; |
| 885 ConstantExpression defaultValue; |
| 886 if (concreteArguments.length > 1) { |
| 887 defaultValue = concreteArguments[1].expression; |
| 888 } |
883 if (constructor == compiler.intEnvironment) { | 889 if (constructor == compiler.intEnvironment) { |
884 expression = new IntFromEnvironmentConstantExpression( | 890 expression = new IntFromEnvironmentConstantExpression( |
885 value, name, normalizedArguments[1].expression); | 891 value, name, defaultValue); |
886 } else if (constructor == compiler.boolEnvironment) { | 892 } else if (constructor == compiler.boolEnvironment) { |
887 expression = new BoolFromEnvironmentConstantExpression( | 893 expression = new BoolFromEnvironmentConstantExpression( |
888 value, name, normalizedArguments[1].expression); | 894 value, name, defaultValue); |
889 } else if (constructor == compiler.stringEnvironment) { | 895 } else if (constructor == compiler.stringEnvironment) { |
890 expression = new StringFromEnvironmentConstantExpression( | 896 expression = new StringFromEnvironmentConstantExpression( |
891 value, name, normalizedArguments[1].expression); | 897 value, name, defaultValue); |
892 } | 898 } |
893 return new AstConstant(context, node, expression); | 899 return new AstConstant(context, node, expression); |
894 } | 900 } |
895 | 901 |
896 if (value == null) { | 902 if (value == null) { |
897 return createEvaluatedConstant(defaultValue); | 903 return createEvaluatedConstant(defaultValue); |
898 } else if (constructor == compiler.intEnvironment) { | 904 } else if (constructor == compiler.intEnvironment) { |
899 int number = int.parse(value, onError: (_) => null); | 905 int number = int.parse(value, onError: (_) => null); |
900 return createEvaluatedConstant( | 906 return createEvaluatedConstant( |
901 (number == null) | 907 (number == null) |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1227 ErroneousAstConstant(Element element, Node node) | 1233 ErroneousAstConstant(Element element, Node node) |
1228 : super(element, node, new ErroneousConstantExpression()); | 1234 : super(element, node, new ErroneousConstantExpression()); |
1229 } | 1235 } |
1230 | 1236 |
1231 // TODO(johnniwinther): Avoid the need for this hack. | 1237 // TODO(johnniwinther): Avoid the need for this hack. |
1232 TreeElements _analyzeElementEagerly(Compiler compiler, AstElement element) { | 1238 TreeElements _analyzeElementEagerly(Compiler compiler, AstElement element) { |
1233 WorldImpact worldImpact = compiler.analyzeElement(element.declaration); | 1239 WorldImpact worldImpact = compiler.analyzeElement(element.declaration); |
1234 compiler.enqueuer.resolution.applyImpact(element.declaration, worldImpact); | 1240 compiler.enqueuer.resolution.applyImpact(element.declaration, worldImpact); |
1235 return element.resolvedAst.elements; | 1241 return element.resolvedAst.elements; |
1236 } | 1242 } |
OLD | NEW |