OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.constants.expressions; | 5 library dart2js.constants.expressions; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common.dart'; |
8 import '../constants/constant_system.dart'; | 8 import '../constants/constant_system.dart'; |
9 import '../core_types.dart'; | 9 import '../core_types.dart'; |
10 import '../elements/resolution_types.dart'; | 10 import '../elements/resolution_types.dart'; |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 229 |
230 @override | 230 @override |
231 int _computeHashCode() => 13 * primitiveValue.hashCode; | 231 int _computeHashCode() => 13 * primitiveValue.hashCode; |
232 | 232 |
233 @override | 233 @override |
234 bool _equals(BoolConstantExpression other) { | 234 bool _equals(BoolConstantExpression other) { |
235 return primitiveValue == other.primitiveValue; | 235 return primitiveValue == other.primitiveValue; |
236 } | 236 } |
237 | 237 |
238 @override | 238 @override |
239 ResolutionDartType getKnownType(CommonElements commonElements) => | 239 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
240 commonElements.boolType; | 240 commonElements.boolType; |
241 } | 241 } |
242 | 242 |
243 /// Integer literal constant. | 243 /// Integer literal constant. |
244 class IntConstantExpression extends PrimitiveConstantExpression { | 244 class IntConstantExpression extends PrimitiveConstantExpression { |
245 final int primitiveValue; | 245 final int primitiveValue; |
246 | 246 |
247 IntConstantExpression(this.primitiveValue); | 247 IntConstantExpression(this.primitiveValue); |
248 | 248 |
249 ConstantExpressionKind get kind => ConstantExpressionKind.INT; | 249 ConstantExpressionKind get kind => ConstantExpressionKind.INT; |
(...skipping 15 matching lines...) Expand all Loading... |
265 | 265 |
266 @override | 266 @override |
267 int _computeHashCode() => 17 * primitiveValue.hashCode; | 267 int _computeHashCode() => 17 * primitiveValue.hashCode; |
268 | 268 |
269 @override | 269 @override |
270 bool _equals(IntConstantExpression other) { | 270 bool _equals(IntConstantExpression other) { |
271 return primitiveValue == other.primitiveValue; | 271 return primitiveValue == other.primitiveValue; |
272 } | 272 } |
273 | 273 |
274 @override | 274 @override |
275 ResolutionDartType getKnownType(CommonElements commonElements) => | 275 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
276 commonElements.intType; | 276 commonElements.intType; |
277 } | 277 } |
278 | 278 |
279 /// Double literal constant. | 279 /// Double literal constant. |
280 class DoubleConstantExpression extends PrimitiveConstantExpression { | 280 class DoubleConstantExpression extends PrimitiveConstantExpression { |
281 final double primitiveValue; | 281 final double primitiveValue; |
282 | 282 |
283 DoubleConstantExpression(this.primitiveValue); | 283 DoubleConstantExpression(this.primitiveValue); |
284 | 284 |
285 ConstantExpressionKind get kind => ConstantExpressionKind.DOUBLE; | 285 ConstantExpressionKind get kind => ConstantExpressionKind.DOUBLE; |
(...skipping 15 matching lines...) Expand all Loading... |
301 | 301 |
302 @override | 302 @override |
303 int _computeHashCode() => 19 * primitiveValue.hashCode; | 303 int _computeHashCode() => 19 * primitiveValue.hashCode; |
304 | 304 |
305 @override | 305 @override |
306 bool _equals(DoubleConstantExpression other) { | 306 bool _equals(DoubleConstantExpression other) { |
307 return primitiveValue == other.primitiveValue; | 307 return primitiveValue == other.primitiveValue; |
308 } | 308 } |
309 | 309 |
310 @override | 310 @override |
311 ResolutionDartType getKnownType(CommonElements commonElements) => | 311 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
312 commonElements.doubleType; | 312 commonElements.doubleType; |
313 } | 313 } |
314 | 314 |
315 /// String literal constant. | 315 /// String literal constant. |
316 class StringConstantExpression extends PrimitiveConstantExpression { | 316 class StringConstantExpression extends PrimitiveConstantExpression { |
317 final String primitiveValue; | 317 final String primitiveValue; |
318 | 318 |
319 StringConstantExpression(this.primitiveValue); | 319 StringConstantExpression(this.primitiveValue); |
320 | 320 |
321 ConstantExpressionKind get kind => ConstantExpressionKind.STRING; | 321 ConstantExpressionKind get kind => ConstantExpressionKind.STRING; |
(...skipping 15 matching lines...) Expand all Loading... |
337 | 337 |
338 @override | 338 @override |
339 int _computeHashCode() => 23 * primitiveValue.hashCode; | 339 int _computeHashCode() => 23 * primitiveValue.hashCode; |
340 | 340 |
341 @override | 341 @override |
342 bool _equals(StringConstantExpression other) { | 342 bool _equals(StringConstantExpression other) { |
343 return primitiveValue == other.primitiveValue; | 343 return primitiveValue == other.primitiveValue; |
344 } | 344 } |
345 | 345 |
346 @override | 346 @override |
347 ResolutionDartType getKnownType(CommonElements commonElements) => | 347 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
348 commonElements.stringType; | 348 commonElements.stringType; |
349 } | 349 } |
350 | 350 |
351 /// Null literal constant. | 351 /// Null literal constant. |
352 class NullConstantExpression extends PrimitiveConstantExpression { | 352 class NullConstantExpression extends PrimitiveConstantExpression { |
353 NullConstantExpression(); | 353 NullConstantExpression(); |
354 | 354 |
355 ConstantExpressionKind get kind => ConstantExpressionKind.NULL; | 355 ConstantExpressionKind get kind => ConstantExpressionKind.NULL; |
356 | 356 |
357 accept(ConstantExpressionVisitor visitor, [context]) { | 357 accept(ConstantExpressionVisitor visitor, [context]) { |
(...skipping 13 matching lines...) Expand all Loading... |
371 | 371 |
372 get primitiveValue => null; | 372 get primitiveValue => null; |
373 | 373 |
374 @override | 374 @override |
375 int _computeHashCode() => 29; | 375 int _computeHashCode() => 29; |
376 | 376 |
377 @override | 377 @override |
378 bool _equals(NullConstantExpression other) => true; | 378 bool _equals(NullConstantExpression other) => true; |
379 | 379 |
380 @override | 380 @override |
381 ResolutionDartType getKnownType(CommonElements commonElements) => | 381 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
382 commonElements.nullType; | 382 commonElements.nullType; |
383 } | 383 } |
384 | 384 |
385 /// Literal list constant. | 385 /// Literal list constant. |
386 class ListConstantExpression extends ConstantExpression { | 386 class ListConstantExpression extends ConstantExpression { |
387 final ResolutionInterfaceType type; | 387 final ResolutionInterfaceType type; |
388 final List<ConstantExpression> values; | 388 final List<ConstantExpression> values; |
389 | 389 |
390 ListConstantExpression(this.type, this.values); | 390 ListConstantExpression(this.type, this.values); |
391 | 391 |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 @override | 688 @override |
689 bool _equals(ConcatenateConstantExpression other) { | 689 bool _equals(ConcatenateConstantExpression other) { |
690 if (expressions.length != other.expressions.length) return false; | 690 if (expressions.length != other.expressions.length) return false; |
691 for (int i = 0; i < expressions.length; i++) { | 691 for (int i = 0; i < expressions.length; i++) { |
692 if (expressions[i] != other.expressions[i]) return false; | 692 if (expressions[i] != other.expressions[i]) return false; |
693 } | 693 } |
694 return true; | 694 return true; |
695 } | 695 } |
696 | 696 |
697 @override | 697 @override |
698 ResolutionDartType getKnownType(CommonElements commonElements) => | 698 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
699 commonElements.stringType; | 699 commonElements.stringType; |
700 | 700 |
701 @override | 701 @override |
702 bool get isPotential { | 702 bool get isPotential { |
703 return expressions.any((e) => e.isPotential); | 703 return expressions.any((e) => e.isPotential); |
704 } | 704 } |
705 } | 705 } |
706 | 706 |
707 /// Symbol literal. | 707 /// Symbol literal. |
708 class SymbolConstantExpression extends ConstantExpression { | 708 class SymbolConstantExpression extends ConstantExpression { |
(...skipping 20 matching lines...) Expand all Loading... |
729 return name == other.name; | 729 return name == other.name; |
730 } | 730 } |
731 | 731 |
732 @override | 732 @override |
733 ConstantValue evaluate( | 733 ConstantValue evaluate( |
734 Environment environment, ConstantSystem constantSystem) { | 734 Environment environment, ConstantSystem constantSystem) { |
735 return constantSystem.createSymbol(environment.compiler, name); | 735 return constantSystem.createSymbol(environment.compiler, name); |
736 } | 736 } |
737 | 737 |
738 @override | 738 @override |
739 ResolutionDartType getKnownType(CommonElements commonElements) => | 739 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
740 commonElements.symbolType; | 740 commonElements.symbolType; |
741 } | 741 } |
742 | 742 |
743 /// Type literal. | 743 /// Type literal. |
744 class TypeConstantExpression extends ConstantExpression { | 744 class TypeConstantExpression extends ConstantExpression { |
745 /// Either [ResolutionDynamicType] or a raw [GenericType]. | 745 /// Either [ResolutionDynamicType] or a raw [GenericType]. |
746 final ResolutionDartType type; | 746 final ResolutionDartType type; |
747 | 747 |
748 TypeConstantExpression(this.type) { | 748 TypeConstantExpression(this.type) { |
749 assert(type is GenericType || type is ResolutionDynamicType); | 749 assert(type is GenericType || type is ResolutionDynamicType); |
(...skipping 18 matching lines...) Expand all Loading... |
768 | 768 |
769 @override | 769 @override |
770 int _computeHashCode() => 13 * type.hashCode; | 770 int _computeHashCode() => 13 * type.hashCode; |
771 | 771 |
772 @override | 772 @override |
773 bool _equals(TypeConstantExpression other) { | 773 bool _equals(TypeConstantExpression other) { |
774 return type == other.type; | 774 return type == other.type; |
775 } | 775 } |
776 | 776 |
777 @override | 777 @override |
778 ResolutionDartType getKnownType(CommonElements commonElements) => | 778 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
779 commonElements.typeType; | 779 commonElements.typeType; |
780 } | 780 } |
781 | 781 |
782 /// Reference to a constant local, top-level, or static variable. | 782 /// Reference to a constant local, top-level, or static variable. |
783 class VariableConstantExpression extends ConstantExpression { | 783 class VariableConstantExpression extends ConstantExpression { |
784 final VariableElement element; | 784 final VariableElement element; |
785 | 785 |
786 VariableConstantExpression(this.element); | 786 VariableConstantExpression(this.element); |
787 | 787 |
788 ConstantExpressionKind get kind => ConstantExpressionKind.VARIABLE; | 788 ConstantExpressionKind get kind => ConstantExpressionKind.VARIABLE; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 | 836 |
837 @override | 837 @override |
838 int _computeHashCode() => 13 * element.hashCode; | 838 int _computeHashCode() => 13 * element.hashCode; |
839 | 839 |
840 @override | 840 @override |
841 bool _equals(FunctionConstantExpression other) { | 841 bool _equals(FunctionConstantExpression other) { |
842 return element == other.element; | 842 return element == other.element; |
843 } | 843 } |
844 | 844 |
845 @override | 845 @override |
846 ResolutionDartType getKnownType(CommonElements commonElements) => | 846 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
847 commonElements.functionType; | 847 commonElements.functionType; |
848 } | 848 } |
849 | 849 |
850 /// A constant binary expression like `a * b`. | 850 /// A constant binary expression like `a * b`. |
851 class BinaryConstantExpression extends ConstantExpression { | 851 class BinaryConstantExpression extends ConstantExpression { |
852 final ConstantExpression left; | 852 final ConstantExpression left; |
853 final BinaryOperator operator; | 853 final BinaryOperator operator; |
854 final ConstantExpression right; | 854 final ConstantExpression right; |
855 | 855 |
856 BinaryConstantExpression(this.left, this.operator, this.right) { | 856 BinaryConstantExpression(this.left, this.operator, this.right) { |
(...skipping 30 matching lines...) Expand all Loading... |
887 .lookupBinary(operator) | 887 .lookupBinary(operator) |
888 .fold(leftValue, rightValue); | 888 .fold(leftValue, rightValue); |
889 } | 889 } |
890 } | 890 } |
891 | 891 |
892 ConstantExpression apply(NormalizedArguments arguments) { | 892 ConstantExpression apply(NormalizedArguments arguments) { |
893 return new BinaryConstantExpression( | 893 return new BinaryConstantExpression( |
894 left.apply(arguments), operator, right.apply(arguments)); | 894 left.apply(arguments), operator, right.apply(arguments)); |
895 } | 895 } |
896 | 896 |
897 ResolutionDartType getKnownType(CommonElements commonElements) { | 897 ResolutionInterfaceType getKnownType(CommonElements commonElements) { |
898 ResolutionDartType knownLeftType = left.getKnownType(commonElements); | 898 ResolutionDartType knownLeftType = left.getKnownType(commonElements); |
899 ResolutionDartType knownRightType = right.getKnownType(commonElements); | 899 ResolutionDartType knownRightType = right.getKnownType(commonElements); |
900 switch (operator.kind) { | 900 switch (operator.kind) { |
901 case BinaryOperatorKind.EQ: | 901 case BinaryOperatorKind.EQ: |
902 case BinaryOperatorKind.NOT_EQ: | 902 case BinaryOperatorKind.NOT_EQ: |
903 case BinaryOperatorKind.LOGICAL_AND: | 903 case BinaryOperatorKind.LOGICAL_AND: |
904 case BinaryOperatorKind.LOGICAL_OR: | 904 case BinaryOperatorKind.LOGICAL_OR: |
905 case BinaryOperatorKind.GT: | 905 case BinaryOperatorKind.GT: |
906 case BinaryOperatorKind.LT: | 906 case BinaryOperatorKind.LT: |
907 case BinaryOperatorKind.GTEQ: | 907 case BinaryOperatorKind.GTEQ: |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1029 int _computeHashCode() { | 1029 int _computeHashCode() { |
1030 return 17 * left.hashCode + 19 * right.hashCode; | 1030 return 17 * left.hashCode + 19 * right.hashCode; |
1031 } | 1031 } |
1032 | 1032 |
1033 @override | 1033 @override |
1034 bool _equals(IdenticalConstantExpression other) { | 1034 bool _equals(IdenticalConstantExpression other) { |
1035 return left == other.left && right == other.right; | 1035 return left == other.left && right == other.right; |
1036 } | 1036 } |
1037 | 1037 |
1038 @override | 1038 @override |
1039 ResolutionDartType getKnownType(CommonElements commonElements) => | 1039 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
1040 commonElements.boolType; | 1040 commonElements.boolType; |
1041 | 1041 |
1042 @override | 1042 @override |
1043 bool get isPotential { | 1043 bool get isPotential { |
1044 return left.isPotential || right.isPotential; | 1044 return left.isPotential || right.isPotential; |
1045 } | 1045 } |
1046 } | 1046 } |
1047 | 1047 |
1048 /// A unary constant expression like `-a`. | 1048 /// A unary constant expression like `-a`. |
1049 class UnaryConstantExpression extends ConstantExpression { | 1049 class UnaryConstantExpression extends ConstantExpression { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1148 int _computeHashCode() { | 1148 int _computeHashCode() { |
1149 return 23 * expression.hashCode; | 1149 return 23 * expression.hashCode; |
1150 } | 1150 } |
1151 | 1151 |
1152 @override | 1152 @override |
1153 bool _equals(StringLengthConstantExpression other) { | 1153 bool _equals(StringLengthConstantExpression other) { |
1154 return expression == other.expression; | 1154 return expression == other.expression; |
1155 } | 1155 } |
1156 | 1156 |
1157 @override | 1157 @override |
1158 ResolutionDartType getKnownType(CommonElements commonElements) => | 1158 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
1159 commonElements.intType; | 1159 commonElements.intType; |
1160 | 1160 |
1161 @override | 1161 @override |
1162 bool get isPotential { | 1162 bool get isPotential { |
1163 return expression.isPotential; | 1163 return expression.isPotential; |
1164 } | 1164 } |
1165 } | 1165 } |
1166 | 1166 |
1167 /// A constant conditional expression like `a ? b : c`. | 1167 /// A constant conditional expression like `a ? b : c`. |
1168 class ConditionalConstantExpression extends ConstantExpression { | 1168 class ConditionalConstantExpression extends ConstantExpression { |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1401 return defaultConstantValue; | 1401 return defaultConstantValue; |
1402 } | 1402 } |
1403 } | 1403 } |
1404 | 1404 |
1405 ConstantExpression apply(NormalizedArguments arguments) { | 1405 ConstantExpression apply(NormalizedArguments arguments) { |
1406 return new BoolFromEnvironmentConstantExpression(name.apply(arguments), | 1406 return new BoolFromEnvironmentConstantExpression(name.apply(arguments), |
1407 defaultValue != null ? defaultValue.apply(arguments) : null); | 1407 defaultValue != null ? defaultValue.apply(arguments) : null); |
1408 } | 1408 } |
1409 | 1409 |
1410 @override | 1410 @override |
1411 ResolutionDartType getKnownType(CommonElements commonElements) => | 1411 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
1412 commonElements.boolType; | 1412 commonElements.boolType; |
1413 } | 1413 } |
1414 | 1414 |
1415 /// A `const int.fromEnvironment` constant. | 1415 /// A `const int.fromEnvironment` constant. |
1416 class IntFromEnvironmentConstantExpression | 1416 class IntFromEnvironmentConstantExpression |
1417 extends FromEnvironmentConstantExpression { | 1417 extends FromEnvironmentConstantExpression { |
1418 IntFromEnvironmentConstantExpression( | 1418 IntFromEnvironmentConstantExpression( |
1419 ConstantExpression name, ConstantExpression defaultValue) | 1419 ConstantExpression name, ConstantExpression defaultValue) |
1420 : super(name, defaultValue); | 1420 : super(name, defaultValue); |
1421 | 1421 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1467 return constantSystem.createInt(value); | 1467 return constantSystem.createInt(value); |
1468 } | 1468 } |
1469 } | 1469 } |
1470 | 1470 |
1471 ConstantExpression apply(NormalizedArguments arguments) { | 1471 ConstantExpression apply(NormalizedArguments arguments) { |
1472 return new IntFromEnvironmentConstantExpression(name.apply(arguments), | 1472 return new IntFromEnvironmentConstantExpression(name.apply(arguments), |
1473 defaultValue != null ? defaultValue.apply(arguments) : null); | 1473 defaultValue != null ? defaultValue.apply(arguments) : null); |
1474 } | 1474 } |
1475 | 1475 |
1476 @override | 1476 @override |
1477 ResolutionDartType getKnownType(CommonElements commonElements) => | 1477 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
1478 commonElements.intType; | 1478 commonElements.intType; |
1479 } | 1479 } |
1480 | 1480 |
1481 /// A `const String.fromEnvironment` constant. | 1481 /// A `const String.fromEnvironment` constant. |
1482 class StringFromEnvironmentConstantExpression | 1482 class StringFromEnvironmentConstantExpression |
1483 extends FromEnvironmentConstantExpression { | 1483 extends FromEnvironmentConstantExpression { |
1484 StringFromEnvironmentConstantExpression( | 1484 StringFromEnvironmentConstantExpression( |
1485 ConstantExpression name, ConstantExpression defaultValue) | 1485 ConstantExpression name, ConstantExpression defaultValue) |
1486 : super(name, defaultValue); | 1486 : super(name, defaultValue); |
1487 | 1487 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1529 return constantSystem.createString(new DartString.literal(text)); | 1529 return constantSystem.createString(new DartString.literal(text)); |
1530 } | 1530 } |
1531 } | 1531 } |
1532 | 1532 |
1533 ConstantExpression apply(NormalizedArguments arguments) { | 1533 ConstantExpression apply(NormalizedArguments arguments) { |
1534 return new StringFromEnvironmentConstantExpression(name.apply(arguments), | 1534 return new StringFromEnvironmentConstantExpression(name.apply(arguments), |
1535 defaultValue != null ? defaultValue.apply(arguments) : null); | 1535 defaultValue != null ? defaultValue.apply(arguments) : null); |
1536 } | 1536 } |
1537 | 1537 |
1538 @override | 1538 @override |
1539 ResolutionDartType getKnownType(CommonElements commonElements) => | 1539 ResolutionInterfaceType getKnownType(CommonElements commonElements) => |
1540 commonElements.stringType; | 1540 commonElements.stringType; |
1541 } | 1541 } |
1542 | 1542 |
1543 /// A constant expression referenced with a deferred prefix. | 1543 /// A constant expression referenced with a deferred prefix. |
1544 /// For example `lib.C`. | 1544 /// For example `lib.C`. |
1545 class DeferredConstantExpression extends ConstantExpression { | 1545 class DeferredConstantExpression extends ConstantExpression { |
1546 final ConstantExpression expression; | 1546 final ConstantExpression expression; |
1547 final PrefixElement prefix; | 1547 final PrefixElement prefix; |
1548 | 1548 |
1549 DeferredConstantExpression(this.expression, this.prefix); | 1549 DeferredConstantExpression(this.expression, this.prefix); |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1888 visit(exp.name); | 1888 visit(exp.name); |
1889 if (exp.defaultValue != null) { | 1889 if (exp.defaultValue != null) { |
1890 sb.write(', defaultValue: '); | 1890 sb.write(', defaultValue: '); |
1891 visit(exp.defaultValue); | 1891 visit(exp.defaultValue); |
1892 } | 1892 } |
1893 sb.write(')'); | 1893 sb.write(')'); |
1894 } | 1894 } |
1895 | 1895 |
1896 String toString() => sb.toString(); | 1896 String toString() => sb.toString(); |
1897 } | 1897 } |
OLD | NEW |