| 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/resolution.dart' show Resolution; | 7 import 'common/resolution.dart' show Resolution; |
| 8 import 'common/tasks.dart' show CompilerTask, Measurer; | 8 import 'common/tasks.dart' show CompilerTask, Measurer; |
| 9 import 'common.dart'; | 9 import 'common.dart'; |
| 10 import 'compiler.dart' show Compiler; | 10 import 'compiler.dart' show Compiler; |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 constantSystem.createMap( | 508 constantSystem.createMap( |
| 509 compiler.commonElements, type, keyValues, map.values.toList())); | 509 compiler.commonElements, type, keyValues, map.values.toList())); |
| 510 } | 510 } |
| 511 | 511 |
| 512 AstConstant visitLiteralNull(LiteralNull node) { | 512 AstConstant visitLiteralNull(LiteralNull node) { |
| 513 return new AstConstant(context, node, new NullConstantExpression(), | 513 return new AstConstant(context, node, new NullConstantExpression(), |
| 514 constantSystem.createNull()); | 514 constantSystem.createNull()); |
| 515 } | 515 } |
| 516 | 516 |
| 517 AstConstant visitLiteralString(LiteralString node) { | 517 AstConstant visitLiteralString(LiteralString node) { |
| 518 return new AstConstant( | 518 String text = node.dartString.slowToString(); |
| 519 context, | 519 return new AstConstant(context, node, new StringConstantExpression(text), |
| 520 node, | 520 constantSystem.createString(text)); |
| 521 new StringConstantExpression(node.dartString.slowToString()), | |
| 522 constantSystem.createString(node.dartString)); | |
| 523 } | 521 } |
| 524 | 522 |
| 525 AstConstant visitStringJuxtaposition(StringJuxtaposition node) { | 523 AstConstant visitStringJuxtaposition(StringJuxtaposition node) { |
| 526 AstConstant left = evaluate(node.first); | 524 AstConstant left = evaluate(node.first); |
| 527 AstConstant right = evaluate(node.second); | 525 AstConstant right = evaluate(node.second); |
| 528 if (left == null || left.isError) { | 526 if (left == null || left.isError) { |
| 529 return left; | 527 return left; |
| 530 } | 528 } |
| 531 if (right == null || right.isError) { | 529 if (right == null || right.isError) { |
| 532 return right; | 530 return right; |
| 533 } | 531 } |
| 534 StringConstantValue leftValue = left.value; | 532 StringConstantValue leftValue = left.value; |
| 535 StringConstantValue rightValue = right.value; | 533 StringConstantValue rightValue = right.value; |
| 536 return new AstConstant( | 534 return new AstConstant( |
| 537 context, | 535 context, |
| 538 node, | 536 node, |
| 539 new ConcatenateConstantExpression([left.expression, right.expression]), | 537 new ConcatenateConstantExpression([left.expression, right.expression]), |
| 540 constantSystem.createString(new DartString.concat( | 538 constantSystem.createString( |
| 541 leftValue.primitiveValue, rightValue.primitiveValue))); | 539 leftValue.primitiveValue + rightValue.primitiveValue)); |
| 542 } | 540 } |
| 543 | 541 |
| 544 AstConstant visitStringInterpolation(StringInterpolation node) { | 542 AstConstant visitStringInterpolation(StringInterpolation node) { |
| 545 List<ConstantExpression> subexpressions = <ConstantExpression>[]; | 543 List<ConstantExpression> subexpressions = <ConstantExpression>[]; |
| 546 AstConstant initialString = evaluate(node.string); | 544 AstConstant initialString = evaluate(node.string); |
| 547 if (initialString == null || initialString.isError) { | 545 if (initialString == null || initialString.isError) { |
| 548 return initialString; | 546 return initialString; |
| 549 } | 547 } |
| 550 subexpressions.add(initialString.expression); | 548 subexpressions.add(initialString.expression); |
| 549 StringBuffer sb = new StringBuffer(); |
| 551 StringConstantValue initialStringValue = initialString.value; | 550 StringConstantValue initialStringValue = initialString.value; |
| 552 DartString accumulator = initialStringValue.primitiveValue; | 551 sb.write(initialStringValue.primitiveValue); |
| 553 for (StringInterpolationPart part in node.parts) { | 552 for (StringInterpolationPart part in node.parts) { |
| 554 AstConstant subexpression = evaluate(part.expression); | 553 AstConstant subexpression = evaluate(part.expression); |
| 555 if (subexpression == null || subexpression.isError) { | 554 if (subexpression == null || subexpression.isError) { |
| 556 return subexpression; | 555 return subexpression; |
| 557 } | 556 } |
| 558 subexpressions.add(subexpression.expression); | 557 subexpressions.add(subexpression.expression); |
| 559 ConstantValue expression = subexpression.value; | 558 ConstantValue expression = subexpression.value; |
| 560 DartString expressionString; | 559 if (expression.isPrimitive) { |
| 561 if (expression.isNum || expression.isBool || expression.isNull) { | |
| 562 PrimitiveConstantValue primitive = expression; | 560 PrimitiveConstantValue primitive = expression; |
| 563 expressionString = | 561 sb.write(primitive.primitiveValue); |
| 564 new DartString.literal(primitive.primitiveValue.toString()); | |
| 565 } else if (expression.isString) { | |
| 566 PrimitiveConstantValue primitive = expression; | |
| 567 expressionString = primitive.primitiveValue; | |
| 568 } else { | 562 } else { |
| 569 // TODO(johnniwinther): Specialize message to indicated that the problem | 563 // TODO(johnniwinther): Specialize message to indicated that the problem |
| 570 // is not constness but the types of the const expressions. | 564 // is not constness but the types of the const expressions. |
| 571 return signalNotCompileTimeConstant(part.expression); | 565 return signalNotCompileTimeConstant(part.expression); |
| 572 } | 566 } |
| 573 accumulator = new DartString.concat(accumulator, expressionString); | |
| 574 AstConstant partString = evaluate(part.string); | 567 AstConstant partString = evaluate(part.string); |
| 575 if (partString == null) return null; | 568 if (partString == null) return null; |
| 576 subexpressions.add(partString.expression); | 569 subexpressions.add(partString.expression); |
| 577 StringConstantValue partStringValue = partString.value; | 570 StringConstantValue partStringValue = partString.value; |
| 578 accumulator = | 571 sb.write(partStringValue.primitiveValue); |
| 579 new DartString.concat(accumulator, partStringValue.primitiveValue); | |
| 580 } | 572 } |
| 581 return new AstConstant( | 573 return new AstConstant( |
| 582 context, | 574 context, |
| 583 node, | 575 node, |
| 584 new ConcatenateConstantExpression(subexpressions), | 576 new ConcatenateConstantExpression(subexpressions), |
| 585 constantSystem.createString(accumulator)); | 577 constantSystem.createString(sb.toString())); |
| 586 } | 578 } |
| 587 | 579 |
| 588 AstConstant visitLiteralSymbol(LiteralSymbol node) { | 580 AstConstant visitLiteralSymbol(LiteralSymbol node) { |
| 589 ResolutionInterfaceType type = commonElements.symbolImplementationType; | 581 ResolutionInterfaceType type = commonElements.symbolImplementationType; |
| 590 String text = node.slowNameString; | 582 String text = node.slowNameString; |
| 591 List<AstConstant> arguments = <AstConstant>[ | 583 List<AstConstant> arguments = <AstConstant>[ |
| 592 new AstConstant(context, node, new StringConstantExpression(text), | 584 new AstConstant(context, node, new StringConstantExpression(text), |
| 593 constantSystem.createString(new LiteralDartString(text))) | 585 constantSystem.createString(text)) |
| 594 ]; | 586 ]; |
| 595 ConstructorElement constructor = | 587 ConstructorElement constructor = |
| 596 compiler.commonElements.symbolConstructorTarget; | 588 compiler.commonElements.symbolConstructorTarget; |
| 597 AstConstant constant = createConstructorInvocation( | 589 AstConstant constant = createConstructorInvocation( |
| 598 node, type, constructor, CallStructure.ONE_ARG, | 590 node, type, constructor, CallStructure.ONE_ARG, |
| 599 normalizedArguments: arguments); | 591 normalizedArguments: arguments); |
| 600 return new AstConstant( | 592 return new AstConstant( |
| 601 context, node, new SymbolConstantExpression(text), constant.value); | 593 context, node, new SymbolConstantExpression(text), constant.value); |
| 602 } | 594 } |
| 603 | 595 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 result = new AstConstant( | 653 result = new AstConstant( |
| 662 context, | 654 context, |
| 663 send, | 655 send, |
| 664 new TypeConstantExpression(elementType, element.name), | 656 new TypeConstantExpression(elementType, element.name), |
| 665 makeTypeConstant(elementType)); | 657 makeTypeConstant(elementType)); |
| 666 } else if (send.receiver != null) { | 658 } else if (send.receiver != null) { |
| 667 if (send.selector.asIdentifier().source == "length") { | 659 if (send.selector.asIdentifier().source == "length") { |
| 668 AstConstant left = evaluate(send.receiver); | 660 AstConstant left = evaluate(send.receiver); |
| 669 if (left != null && left.value.isString) { | 661 if (left != null && left.value.isString) { |
| 670 StringConstantValue stringConstantValue = left.value; | 662 StringConstantValue stringConstantValue = left.value; |
| 671 DartString string = stringConstantValue.primitiveValue; | 663 String string = stringConstantValue.primitiveValue; |
| 672 IntConstantValue length = constantSystem.createInt(string.length); | 664 IntConstantValue length = constantSystem.createInt(string.length); |
| 673 result = new AstConstant(context, send, | 665 result = new AstConstant(context, send, |
| 674 new StringLengthConstantExpression(left.expression), length); | 666 new StringLengthConstantExpression(left.expression), length); |
| 675 } | 667 } |
| 676 } | 668 } |
| 677 // Fall through to error handling. | 669 // Fall through to error handling. |
| 678 } else if (!Elements.isUnresolved(element) && | 670 } else if (!Elements.isUnresolved(element) && |
| 679 element.isVariable && | 671 element.isVariable && |
| 680 element.isConst) { | 672 element.isConst) { |
| 681 LocalVariableElement local = element; | 673 LocalVariableElement local = element; |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1036 | 1028 |
| 1037 if (constructor.isStringFromEnvironmentConstructor && | 1029 if (constructor.isStringFromEnvironmentConstructor && |
| 1038 !(defaultValue.isNull || defaultValue.isString)) { | 1030 !(defaultValue.isNull || defaultValue.isString)) { |
| 1039 ResolutionDartType type = defaultValue.getType(commonElements); | 1031 ResolutionDartType type = defaultValue.getType(commonElements); |
| 1040 return reportNotCompileTimeConstant( | 1032 return reportNotCompileTimeConstant( |
| 1041 normalizedArguments[1].node, | 1033 normalizedArguments[1].node, |
| 1042 MessageKind.NOT_ASSIGNABLE, | 1034 MessageKind.NOT_ASSIGNABLE, |
| 1043 {'fromType': type, 'toType': commonElements.stringType}); | 1035 {'fromType': type, 'toType': commonElements.stringType}); |
| 1044 } | 1036 } |
| 1045 | 1037 |
| 1046 String name = firstArgument.primitiveValue.slowToString(); | 1038 String name = firstArgument.primitiveValue; |
| 1047 String value = compiler.fromEnvironment(name); | 1039 String value = compiler.fromEnvironment(name); |
| 1048 | 1040 |
| 1049 AstConstant createEvaluatedConstant(ConstantValue value) { | 1041 AstConstant createEvaluatedConstant(ConstantValue value) { |
| 1050 ConstantExpression expression; | 1042 ConstantExpression expression; |
| 1051 ConstantExpression name = concreteArguments[0].expression; | 1043 ConstantExpression name = concreteArguments[0].expression; |
| 1052 ConstantExpression defaultValue; | 1044 ConstantExpression defaultValue; |
| 1053 if (concreteArguments.length > 1) { | 1045 if (concreteArguments.length > 1) { |
| 1054 defaultValue = concreteArguments[1].expression; | 1046 defaultValue = concreteArguments[1].expression; |
| 1055 } | 1047 } |
| 1056 if (constructor.isIntFromEnvironmentConstructor) { | 1048 if (constructor.isIntFromEnvironmentConstructor) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1076 } else if (constructor.isBoolFromEnvironmentConstructor) { | 1068 } else if (constructor.isBoolFromEnvironmentConstructor) { |
| 1077 if (value == 'true') { | 1069 if (value == 'true') { |
| 1078 return createEvaluatedConstant(constantSystem.createBool(true)); | 1070 return createEvaluatedConstant(constantSystem.createBool(true)); |
| 1079 } else if (value == 'false') { | 1071 } else if (value == 'false') { |
| 1080 return createEvaluatedConstant(constantSystem.createBool(false)); | 1072 return createEvaluatedConstant(constantSystem.createBool(false)); |
| 1081 } else { | 1073 } else { |
| 1082 return createEvaluatedConstant(defaultValue); | 1074 return createEvaluatedConstant(defaultValue); |
| 1083 } | 1075 } |
| 1084 } else { | 1076 } else { |
| 1085 assert(constructor.isStringFromEnvironmentConstructor); | 1077 assert(constructor.isStringFromEnvironmentConstructor); |
| 1086 return createEvaluatedConstant( | 1078 return createEvaluatedConstant(constantSystem.createString(value)); |
| 1087 constantSystem.createString(new DartString.literal(value))); | |
| 1088 } | 1079 } |
| 1089 } | 1080 } |
| 1090 | 1081 |
| 1091 static AstConstant makeConstructedConstant( | 1082 static AstConstant makeConstructedConstant( |
| 1092 Compiler compiler, | 1083 Compiler compiler, |
| 1093 ConstantCompilerBase handler, | 1084 ConstantCompilerBase handler, |
| 1094 Element context, | 1085 Element context, |
| 1095 Node node, | 1086 Node node, |
| 1096 ResolutionInterfaceType type, | 1087 ResolutionInterfaceType type, |
| 1097 ConstructorElement constructor, | 1088 ConstructorElement constructor, |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1469 @override | 1460 @override |
| 1470 ConstantExpression getFieldConstant(FieldElement field) { | 1461 ConstantExpression getFieldConstant(FieldElement field) { |
| 1471 return field.constant; | 1462 return field.constant; |
| 1472 } | 1463 } |
| 1473 | 1464 |
| 1474 @override | 1465 @override |
| 1475 ConstantExpression getLocalConstant(LocalVariableElement local) { | 1466 ConstantExpression getLocalConstant(LocalVariableElement local) { |
| 1476 return local.constant; | 1467 return local.constant; |
| 1477 } | 1468 } |
| 1478 } | 1469 } |
| OLD | NEW |