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

Side by Side Diff: pkg/compiler/lib/src/constants/expressions.dart

Issue 1170673002: Begin to compute constant expressions in resolution. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 6 months 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
« no previous file with comments | « pkg/compiler/lib/src/compiler.dart ('k') | pkg/compiler/lib/src/constants/values.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 '../constants/constant_system.dart'; 7 import '../constants/constant_system.dart';
8 import '../core_types.dart';
8 import '../dart2jslib.dart' show assertDebugMode, Compiler; 9 import '../dart2jslib.dart' show assertDebugMode, Compiler;
9 import '../dart_types.dart'; 10 import '../dart_types.dart';
10 import '../elements/elements.dart' show 11 import '../elements/elements.dart' show
11 ConstructorElement, 12 ConstructorElement,
12 Element, 13 Element,
13 FieldElement, 14 FieldElement,
14 FunctionElement, 15 FunctionElement,
15 PrefixElement, 16 PrefixElement,
16 VariableElement; 17 VariableElement;
17 import '../resolution/operators.dart'; 18 import '../resolution/operators.dart';
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 accept(ConstantExpressionVisitor visitor, [context]); 269 accept(ConstantExpressionVisitor visitor, [context]);
269 270
270 /// Substitute free variables using arguments. 271 /// Substitute free variables using arguments.
271 ConstantExpression apply(NormalizedArguments arguments) => this; 272 ConstantExpression apply(NormalizedArguments arguments) => this;
272 273
273 /// Compute the [ConstantValue] for this expression using the [environment] 274 /// Compute the [ConstantValue] for this expression using the [environment]
274 /// and the [constantSystem]. 275 /// and the [constantSystem].
275 ConstantValue evaluate(Environment environment, 276 ConstantValue evaluate(Environment environment,
276 ConstantSystem constantSystem); 277 ConstantSystem constantSystem);
277 278
279 /// Returns the type of this constant expression, if it is independent of the
280 /// environment values.
281 DartType getKnownType(CoreTypes coreTypes) => null;
282
278 String getText() { 283 String getText() {
279 ConstExpPrinter printer = new ConstExpPrinter(); 284 ConstExpPrinter printer = new ConstExpPrinter();
280 accept(printer); 285 accept(printer);
281 return printer.toString(); 286 return printer.toString();
282 } 287 }
283 288
284 int _computeHashCode(); 289 int _computeHashCode();
285 290
286 int get hashCode { 291 int get hashCode {
287 if (_hashCode == null) { 292 if (_hashCode == null) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 return constantSystem.createBool(primitiveValue); 358 return constantSystem.createBool(primitiveValue);
354 } 359 }
355 360
356 @override 361 @override
357 int _computeHashCode() => 13 * primitiveValue.hashCode; 362 int _computeHashCode() => 13 * primitiveValue.hashCode;
358 363
359 @override 364 @override
360 bool _equals(BoolConstantExpression other) { 365 bool _equals(BoolConstantExpression other) {
361 return primitiveValue == other.primitiveValue; 366 return primitiveValue == other.primitiveValue;
362 } 367 }
368
369 @override
370 DartType getKnownType(CoreTypes coreTypes) => coreTypes.boolType;
363 } 371 }
364 372
365 /// Integer literal constant. 373 /// Integer literal constant.
366 class IntConstantExpression extends PrimitiveConstantExpression { 374 class IntConstantExpression extends PrimitiveConstantExpression {
367 final int primitiveValue; 375 final int primitiveValue;
368 376
369 IntConstantExpression(this.primitiveValue); 377 IntConstantExpression(this.primitiveValue);
370 378
371 ConstantExpressionKind get kind => ConstantExpressionKind.INT; 379 ConstantExpressionKind get kind => ConstantExpressionKind.INT;
372 380
373 accept(ConstantExpressionVisitor visitor, [context]) { 381 accept(ConstantExpressionVisitor visitor, [context]) {
374 return visitor.visitInt(this, context); 382 return visitor.visitInt(this, context);
375 } 383 }
376 384
377 @override 385 @override
378 ConstantValue evaluate(Environment environment, 386 ConstantValue evaluate(Environment environment,
379 ConstantSystem constantSystem) { 387 ConstantSystem constantSystem) {
380 return constantSystem.createInt(primitiveValue); 388 return constantSystem.createInt(primitiveValue);
381 } 389 }
382 390
383 @override 391 @override
384 int _computeHashCode() => 17 * primitiveValue.hashCode; 392 int _computeHashCode() => 17 * primitiveValue.hashCode;
385 393
386 @override 394 @override
387 bool _equals(IntConstantExpression other) { 395 bool _equals(IntConstantExpression other) {
388 return primitiveValue == other.primitiveValue; 396 return primitiveValue == other.primitiveValue;
389 } 397 }
398
399 @override
400 DartType getKnownType(CoreTypes coreTypes) => coreTypes.intType;
390 } 401 }
391 402
392 /// Double literal constant. 403 /// Double literal constant.
393 class DoubleConstantExpression extends PrimitiveConstantExpression { 404 class DoubleConstantExpression extends PrimitiveConstantExpression {
394 final double primitiveValue; 405 final double primitiveValue;
395 406
396 DoubleConstantExpression(this.primitiveValue); 407 DoubleConstantExpression(this.primitiveValue);
397 408
398 ConstantExpressionKind get kind => ConstantExpressionKind.DOUBLE; 409 ConstantExpressionKind get kind => ConstantExpressionKind.DOUBLE;
399 410
400 accept(ConstantExpressionVisitor visitor, [context]) { 411 accept(ConstantExpressionVisitor visitor, [context]) {
401 return visitor.visitDouble(this, context); 412 return visitor.visitDouble(this, context);
402 } 413 }
403 414
404 @override 415 @override
405 ConstantValue evaluate(Environment environment, 416 ConstantValue evaluate(Environment environment,
406 ConstantSystem constantSystem) { 417 ConstantSystem constantSystem) {
407 return constantSystem.createDouble(primitiveValue); 418 return constantSystem.createDouble(primitiveValue);
408 } 419 }
409 420
410 @override 421 @override
411 int _computeHashCode() => 19 * primitiveValue.hashCode; 422 int _computeHashCode() => 19 * primitiveValue.hashCode;
412 423
413 @override 424 @override
414 bool _equals(DoubleConstantExpression other) { 425 bool _equals(DoubleConstantExpression other) {
415 return primitiveValue == other.primitiveValue; 426 return primitiveValue == other.primitiveValue;
416 } 427 }
428
429 @override
430 DartType getKnownType(CoreTypes coreTypes) => coreTypes.doubleType;
417 } 431 }
418 432
419 /// String literal constant. 433 /// String literal constant.
420 class StringConstantExpression extends PrimitiveConstantExpression { 434 class StringConstantExpression extends PrimitiveConstantExpression {
421 final String primitiveValue; 435 final String primitiveValue;
422 436
423 StringConstantExpression(this.primitiveValue); 437 StringConstantExpression(this.primitiveValue);
424 438
425 ConstantExpressionKind get kind => ConstantExpressionKind.STRING; 439 ConstantExpressionKind get kind => ConstantExpressionKind.STRING;
426 440
427 accept(ConstantExpressionVisitor visitor, [context]) { 441 accept(ConstantExpressionVisitor visitor, [context]) {
428 return visitor.visitString(this, context); 442 return visitor.visitString(this, context);
429 } 443 }
430 444
431 @override 445 @override
432 ConstantValue evaluate(Environment environment, 446 ConstantValue evaluate(Environment environment,
433 ConstantSystem constantSystem) { 447 ConstantSystem constantSystem) {
434 return constantSystem.createString(new DartString.literal(primitiveValue)); 448 return constantSystem.createString(new DartString.literal(primitiveValue));
435 } 449 }
436 450
437 @override 451 @override
438 int _computeHashCode() => 23 * primitiveValue.hashCode; 452 int _computeHashCode() => 23 * primitiveValue.hashCode;
439 453
440 @override 454 @override
441 bool _equals(StringConstantExpression other) { 455 bool _equals(StringConstantExpression other) {
442 return primitiveValue == other.primitiveValue; 456 return primitiveValue == other.primitiveValue;
443 } 457 }
458
459 @override
460 DartType getKnownType(CoreTypes coreTypes) => coreTypes.stringType;
444 } 461 }
445 462
446 /// Null literal constant. 463 /// Null literal constant.
447 class NullConstantExpression extends PrimitiveConstantExpression { 464 class NullConstantExpression extends PrimitiveConstantExpression {
448 NullConstantExpression(); 465 NullConstantExpression();
449 466
450 ConstantExpressionKind get kind => ConstantExpressionKind.NULL; 467 ConstantExpressionKind get kind => ConstantExpressionKind.NULL;
451 468
452 accept(ConstantExpressionVisitor visitor, [context]) { 469 accept(ConstantExpressionVisitor visitor, [context]) {
453 return visitor.visitNull(this, context); 470 return visitor.visitNull(this, context);
454 } 471 }
455 472
456 @override 473 @override
457 ConstantValue evaluate(Environment environment, 474 ConstantValue evaluate(Environment environment,
458 ConstantSystem constantSystem) { 475 ConstantSystem constantSystem) {
459 return constantSystem.createNull(); 476 return constantSystem.createNull();
460 } 477 }
461 478
462 get primitiveValue => null; 479 get primitiveValue => null;
463 480
464 @override 481 @override
465 int _computeHashCode() => 29; 482 int _computeHashCode() => 29;
466 483
467 @override 484 @override
468 bool _equals(NullConstantExpression other) => true; 485 bool _equals(NullConstantExpression other) => true;
486
487 @override
488 DartType getKnownType(CoreTypes coreTypes) => coreTypes.nullType;
469 } 489 }
470 490
471 /// Literal list constant. 491 /// Literal list constant.
472 class ListConstantExpression extends ConstantExpression { 492 class ListConstantExpression extends ConstantExpression {
473 final InterfaceType type; 493 final InterfaceType type;
474 final List<ConstantExpression> values; 494 final List<ConstantExpression> values;
475 495
476 ListConstantExpression(this.type, this.values); 496 ListConstantExpression(this.type, this.values);
477 497
478 ConstantExpressionKind get kind => ConstantExpressionKind.LIST; 498 ConstantExpressionKind get kind => ConstantExpressionKind.LIST;
(...skipping 25 matching lines...) Expand all
504 524
505 @override 525 @override
506 bool _equals(ListConstantExpression other) { 526 bool _equals(ListConstantExpression other) {
507 if (type != other.type) return false; 527 if (type != other.type) return false;
508 if (values.length != other.values.length) return false; 528 if (values.length != other.values.length) return false;
509 for (int i = 0; i < values.length; i++) { 529 for (int i = 0; i < values.length; i++) {
510 if (values[i] != other.values[i]) return false; 530 if (values[i] != other.values[i]) return false;
511 } 531 }
512 return true; 532 return true;
513 } 533 }
534
535 @override
536 DartType getKnownType(CoreTypes coreTypes) => type;
514 } 537 }
515 538
516 /// Literal map constant. 539 /// Literal map constant.
517 class MapConstantExpression extends ConstantExpression { 540 class MapConstantExpression extends ConstantExpression {
518 final InterfaceType type; 541 final InterfaceType type;
519 final List<ConstantExpression> keys; 542 final List<ConstantExpression> keys;
520 final List<ConstantExpression> values; 543 final List<ConstantExpression> values;
521 544
522 MapConstantExpression(this.type, this.keys, this.values); 545 MapConstantExpression(this.type, this.keys, this.values);
523 546
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 @override 578 @override
556 bool _equals(MapConstantExpression other) { 579 bool _equals(MapConstantExpression other) {
557 if (type != other.type) return false; 580 if (type != other.type) return false;
558 if (values.length != other.values.length) return false; 581 if (values.length != other.values.length) return false;
559 for (int i = 0; i < values.length; i++) { 582 for (int i = 0; i < values.length; i++) {
560 if (keys[i] != other.keys[i]) return false; 583 if (keys[i] != other.keys[i]) return false;
561 if (values[i] != other.values[i]) return false; 584 if (values[i] != other.values[i]) return false;
562 } 585 }
563 return true; 586 return true;
564 } 587 }
588
589 @override
590 DartType getKnownType(CoreTypes coreTypes) => type;
565 } 591 }
566 592
567 /// Invocation of a const constructor. 593 /// Invocation of a const constructor.
568 class ConstructedConstantExpression extends ConstantExpression { 594 class ConstructedConstantExpression extends ConstantExpression {
569 final InterfaceType type; 595 final InterfaceType type;
570 final ConstructorElement target; 596 final ConstructorElement target;
571 final CallStructure callStructure; 597 final CallStructure callStructure;
572 final List<ConstantExpression> arguments; 598 final List<ConstantExpression> arguments;
573 599
574 ConstructedConstantExpression( 600 ConstructedConstantExpression(
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 } 718 }
693 719
694 @override 720 @override
695 bool _equals(ConcatenateConstantExpression other) { 721 bool _equals(ConcatenateConstantExpression other) {
696 if (expressions.length != other.expressions.length) return false; 722 if (expressions.length != other.expressions.length) return false;
697 for (int i = 0; i < expressions.length; i++) { 723 for (int i = 0; i < expressions.length; i++) {
698 if (expressions[i] != other.expressions[i]) return false; 724 if (expressions[i] != other.expressions[i]) return false;
699 } 725 }
700 return true; 726 return true;
701 } 727 }
728
729 @override
730 DartType getKnownType(CoreTypes coreTypes) => coreTypes.stringType;
702 } 731 }
703 732
704 /// Symbol literal. 733 /// Symbol literal.
705 class SymbolConstantExpression extends ConstantExpression { 734 class SymbolConstantExpression extends ConstantExpression {
706 final String name; 735 final String name;
707 736
708 SymbolConstantExpression(this.name); 737 SymbolConstantExpression(this.name);
709 738
710 ConstantExpressionKind get kind => ConstantExpressionKind.SYMBOL; 739 ConstantExpressionKind get kind => ConstantExpressionKind.SYMBOL;
711 740
712 accept(ConstantExpressionVisitor visitor, [context]) { 741 accept(ConstantExpressionVisitor visitor, [context]) {
713 return visitor.visitSymbol(this, context); 742 return visitor.visitSymbol(this, context);
714 } 743 }
715 744
716 @override 745 @override
717 int _computeHashCode() => 13 * name.hashCode; 746 int _computeHashCode() => 13 * name.hashCode;
718 747
719 @override 748 @override
720 bool _equals(SymbolConstantExpression other) { 749 bool _equals(SymbolConstantExpression other) {
721 return name == other.name; 750 return name == other.name;
722 } 751 }
723 752
724 @override 753 @override
725 ConstantValue evaluate(Environment environment, 754 ConstantValue evaluate(Environment environment,
726 ConstantSystem constantSystem) { 755 ConstantSystem constantSystem) {
727 // TODO(johnniwinther): Implement this. 756 // TODO(johnniwinther): Implement this.
728 throw new UnsupportedError('SymbolConstantExpression.evaluate'); 757 throw new UnsupportedError('SymbolConstantExpression.evaluate');
729 } 758 }
759
760 @override
761 DartType getKnownType(CoreTypes coreTypes) => coreTypes.symbolType;
730 } 762 }
731 763
732 /// Type literal. 764 /// Type literal.
733 class TypeConstantExpression extends ConstantExpression { 765 class TypeConstantExpression extends ConstantExpression {
734 /// Either [DynamicType] or a raw [GenericType]. 766 /// Either [DynamicType] or a raw [GenericType].
735 final DartType type; 767 final DartType type;
736 768
737 TypeConstantExpression(this.type) { 769 TypeConstantExpression(this.type) {
738 assert(type is GenericType || type is DynamicType); 770 assert(type is GenericType || type is DynamicType);
739 } 771 }
(...skipping 10 matching lines...) Expand all
750 return constantSystem.createType(environment.compiler, type); 782 return constantSystem.createType(environment.compiler, type);
751 } 783 }
752 784
753 @override 785 @override
754 int _computeHashCode() => 13 * type.hashCode; 786 int _computeHashCode() => 13 * type.hashCode;
755 787
756 @override 788 @override
757 bool _equals(TypeConstantExpression other) { 789 bool _equals(TypeConstantExpression other) {
758 return type == other.type; 790 return type == other.type;
759 } 791 }
792
793 @override
794 DartType getKnownType(CoreTypes coreTypes) => coreTypes.typeType;
760 } 795 }
761 796
762 /// Reference to a constant local, top-level, or static variable. 797 /// Reference to a constant local, top-level, or static variable.
763 class VariableConstantExpression extends ConstantExpression { 798 class VariableConstantExpression extends ConstantExpression {
764 final VariableElement element; 799 final VariableElement element;
765 800
766 VariableConstantExpression(this.element); 801 VariableConstantExpression(this.element);
767 802
768 ConstantExpressionKind get kind => ConstantExpressionKind.VARIABLE; 803 ConstantExpressionKind get kind => ConstantExpressionKind.VARIABLE;
769 804
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 return new FunctionConstantValue(element); 839 return new FunctionConstantValue(element);
805 } 840 }
806 841
807 @override 842 @override
808 int _computeHashCode() => 13 * element.hashCode; 843 int _computeHashCode() => 13 * element.hashCode;
809 844
810 @override 845 @override
811 bool _equals(FunctionConstantExpression other) { 846 bool _equals(FunctionConstantExpression other) {
812 return element == other.element; 847 return element == other.element;
813 } 848 }
849
850 @override
851 DartType getKnownType(CoreTypes coreTypes) => coreTypes.functionType;
814 } 852 }
815 853
816 /// A constant binary expression like `a * b`. 854 /// A constant binary expression like `a * b`.
817 class BinaryConstantExpression extends ConstantExpression { 855 class BinaryConstantExpression extends ConstantExpression {
818 final ConstantExpression left; 856 final ConstantExpression left;
819 final BinaryOperator operator; 857 final BinaryOperator operator;
820 final ConstantExpression right; 858 final ConstantExpression right;
821 859
822 BinaryConstantExpression(this.left, this.operator, this.right) { 860 BinaryConstantExpression(this.left, this.operator, this.right) {
823 assert(PRECEDENCE_MAP[operator.kind] != null); 861 assert(PRECEDENCE_MAP[operator.kind] != null);
(...skipping 13 matching lines...) Expand all
837 right.evaluate(environment, constantSystem)); 875 right.evaluate(environment, constantSystem));
838 } 876 }
839 877
840 ConstantExpression apply(NormalizedArguments arguments) { 878 ConstantExpression apply(NormalizedArguments arguments) {
841 return new BinaryConstantExpression( 879 return new BinaryConstantExpression(
842 left.apply(arguments), 880 left.apply(arguments),
843 operator, 881 operator,
844 right.apply(arguments)); 882 right.apply(arguments));
845 } 883 }
846 884
885 DartType getKnownType(CoreTypes coreTypes) {
886 switch (operator.kind) {
887 case BinaryOperatorKind.EQ:
888 case BinaryOperatorKind.NOT_EQ:
889 case BinaryOperatorKind.LOGICAL_AND:
890 case BinaryOperatorKind.LOGICAL_OR:
891 return coreTypes.boolType;
892 case BinaryOperatorKind.XOR:
893 case BinaryOperatorKind.AND:
894 case BinaryOperatorKind.OR:
895 case BinaryOperatorKind.SHR:
896 case BinaryOperatorKind.SHL:
897 case BinaryOperatorKind.ADD:
898 case BinaryOperatorKind.SUB:
899 case BinaryOperatorKind.MUL:
900 case BinaryOperatorKind.DIV:
901 case BinaryOperatorKind.IDIV:
902 case BinaryOperatorKind.GT:
903 case BinaryOperatorKind.LT:
904 case BinaryOperatorKind.GTEQ:
905 case BinaryOperatorKind.LTEQ:
906 case BinaryOperatorKind.MOD:
karlklose 2015/06/08 09:25:24 This is not true - at least for MOD.
Johnni Winther 2015/06/08 12:18:13 Done.
907 // TODO(johnniwinther): Refine this when potentially invalid constant
908 // expressions are handled.
909 return left.getKnownType(coreTypes);
910 case BinaryOperatorKind.IF_NULL:
911 case BinaryOperatorKind.INDEX:
912 throw new UnsupportedError(
913 'Unexpected constant binary operator: $operator');
914 }
915 }
916
917
847 int get precedence => PRECEDENCE_MAP[operator.kind]; 918 int get precedence => PRECEDENCE_MAP[operator.kind];
848 919
849 @override 920 @override
850 int _computeHashCode() { 921 int _computeHashCode() {
851 return 13 * operator.hashCode + 922 return 13 * operator.hashCode +
852 17 * left.hashCode + 923 17 * left.hashCode +
853 19 * right.hashCode; 924 19 * right.hashCode;
854 } 925 }
855 926
856 @override 927 @override
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
916 int _computeHashCode() { 987 int _computeHashCode() {
917 return 17 * left.hashCode + 988 return 17 * left.hashCode +
918 19 * right.hashCode; 989 19 * right.hashCode;
919 } 990 }
920 991
921 @override 992 @override
922 bool _equals(IdenticalConstantExpression other) { 993 bool _equals(IdenticalConstantExpression other) {
923 return left == other.left && 994 return left == other.left &&
924 right == other.right; 995 right == other.right;
925 } 996 }
997
998 @override
999 DartType getKnownType(CoreTypes coreTypes) => coreTypes.boolType;
926 } 1000 }
927 1001
928 /// A unary constant expression like `-a`. 1002 /// A unary constant expression like `-a`.
929 class UnaryConstantExpression extends ConstantExpression { 1003 class UnaryConstantExpression extends ConstantExpression {
930 final UnaryOperator operator; 1004 final UnaryOperator operator;
931 final ConstantExpression expression; 1005 final ConstantExpression expression;
932 1006
933 UnaryConstantExpression(this.operator, this.expression) { 1007 UnaryConstantExpression(this.operator, this.expression) {
934 assert(PRECEDENCE_MAP[operator.kind] != null); 1008 assert(PRECEDENCE_MAP[operator.kind] != null);
935 } 1009 }
(...skipping 24 matching lines...) Expand all
960 return 13 * operator.hashCode + 1034 return 13 * operator.hashCode +
961 17 * expression.hashCode; 1035 17 * expression.hashCode;
962 } 1036 }
963 1037
964 @override 1038 @override
965 bool _equals(UnaryConstantExpression other) { 1039 bool _equals(UnaryConstantExpression other) {
966 return operator == other.operator && 1040 return operator == other.operator &&
967 expression == other.expression; 1041 expression == other.expression;
968 } 1042 }
969 1043
1044 @override
1045 DartType getKnownType(CoreTypes coreTypes) {
1046 return expression.getKnownType(coreTypes);
1047 }
1048
970 static const Map<UnaryOperatorKind, int> PRECEDENCE_MAP = const { 1049 static const Map<UnaryOperatorKind, int> PRECEDENCE_MAP = const {
971 UnaryOperatorKind.NOT: 14, 1050 UnaryOperatorKind.NOT: 14,
972 UnaryOperatorKind.COMPLEMENT: 14, 1051 UnaryOperatorKind.COMPLEMENT: 14,
973 UnaryOperatorKind.NEGATE: 14, 1052 UnaryOperatorKind.NEGATE: 14,
974 }; 1053 };
975 } 1054 }
976 1055
977 1056
978 /// A string length constant expression like `a.length`. 1057 /// A string length constant expression like `a.length`.
979 class StringLengthConstantExpression extends ConstantExpression { 1058 class StringLengthConstantExpression extends ConstantExpression {
(...skipping 26 matching lines...) Expand all
1006 1085
1007 @override 1086 @override
1008 int _computeHashCode() { 1087 int _computeHashCode() {
1009 return 23 * expression.hashCode; 1088 return 23 * expression.hashCode;
1010 } 1089 }
1011 1090
1012 @override 1091 @override
1013 bool _equals(StringLengthConstantExpression other) { 1092 bool _equals(StringLengthConstantExpression other) {
1014 return expression == other.expression; 1093 return expression == other.expression;
1015 } 1094 }
1095
1096 @override
1097 DartType getKnownType(CoreTypes coreTypes) => coreTypes.intType;
1016 } 1098 }
1017 1099
1018 /// A constant conditional expression like `a ? b : c`. 1100 /// A constant conditional expression like `a ? b : c`.
1019 class ConditionalConstantExpression extends ConstantExpression { 1101 class ConditionalConstantExpression extends ConstantExpression {
1020 final ConstantExpression condition; 1102 final ConstantExpression condition;
1021 final ConstantExpression trueExp; 1103 final ConstantExpression trueExp;
1022 final ConstantExpression falseExp; 1104 final ConstantExpression falseExp;
1023 1105
1024 ConditionalConstantExpression(this.condition, 1106 ConditionalConstantExpression(this.condition,
1025 this.trueExp, 1107 this.trueExp,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1064 ConstantValue falseValue = 1146 ConstantValue falseValue =
1065 falseExp.evaluate(environment, constantSystem); 1147 falseExp.evaluate(environment, constantSystem);
1066 if (conditionValue.isTrue) { 1148 if (conditionValue.isTrue) {
1067 return trueValue; 1149 return trueValue;
1068 } else if (conditionValue.isFalse) { 1150 } else if (conditionValue.isFalse) {
1069 return falseValue; 1151 return falseValue;
1070 } else { 1152 } else {
1071 return new NonConstantValue(); 1153 return new NonConstantValue();
1072 } 1154 }
1073 } 1155 }
1156
1157 @override
1158 DartType getKnownType(CoreTypes coreTypes) {
1159 DartType trueType = trueExp.getKnownType(coreTypes);
1160 DartType falseType = falseExp.getKnownType(coreTypes);
1161 if (trueType == falseType) {
1162 return trueType;
1163 }
1164 return null;
1165 }
1074 } 1166 }
1075 1167
1076 /// A reference to a position parameter. 1168 /// A reference to a position parameter.
1077 class PositionalArgumentReference extends ConstantExpression { 1169 class PositionalArgumentReference extends ConstantExpression {
1078 final int index; 1170 final int index;
1079 1171
1080 PositionalArgumentReference(this.index); 1172 PositionalArgumentReference(this.index);
1081 1173
1082 ConstantExpressionKind get kind { 1174 ConstantExpressionKind get kind {
1083 return ConstantExpressionKind.POSITIONAL_REFERENCE; 1175 return ConstantExpressionKind.POSITIONAL_REFERENCE;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
1196 } else { 1288 } else {
1197 return defaultConstantValue; 1289 return defaultConstantValue;
1198 } 1290 }
1199 } 1291 }
1200 1292
1201 ConstantExpression apply(NormalizedArguments arguments) { 1293 ConstantExpression apply(NormalizedArguments arguments) {
1202 return new BoolFromEnvironmentConstantExpression( 1294 return new BoolFromEnvironmentConstantExpression(
1203 name.apply(arguments), 1295 name.apply(arguments),
1204 defaultValue != null ? defaultValue.apply(arguments) : null); 1296 defaultValue != null ? defaultValue.apply(arguments) : null);
1205 } 1297 }
1298
1299 @override
1300 DartType getKnownType(CoreTypes coreTypes) => coreTypes.boolType;
1206 } 1301 }
1207 1302
1208 /// A `const int.fromEnvironment` constant. 1303 /// A `const int.fromEnvironment` constant.
1209 class IntFromEnvironmentConstantExpression 1304 class IntFromEnvironmentConstantExpression
1210 extends FromEnvironmentConstantExpression { 1305 extends FromEnvironmentConstantExpression {
1211 1306
1212 IntFromEnvironmentConstantExpression( 1307 IntFromEnvironmentConstantExpression(
1213 ConstantExpression name, 1308 ConstantExpression name,
1214 ConstantExpression defaultValue) 1309 ConstantExpression defaultValue)
1215 : super(name, defaultValue); 1310 : super(name, defaultValue);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1249 } else { 1344 } else {
1250 return constantSystem.createInt(value); 1345 return constantSystem.createInt(value);
1251 } 1346 }
1252 } 1347 }
1253 1348
1254 ConstantExpression apply(NormalizedArguments arguments) { 1349 ConstantExpression apply(NormalizedArguments arguments) {
1255 return new IntFromEnvironmentConstantExpression( 1350 return new IntFromEnvironmentConstantExpression(
1256 name.apply(arguments), 1351 name.apply(arguments),
1257 defaultValue != null ? defaultValue.apply(arguments) : null); 1352 defaultValue != null ? defaultValue.apply(arguments) : null);
1258 } 1353 }
1354
1355 @override
1356 DartType getKnownType(CoreTypes coreTypes) => coreTypes.intType;
1259 } 1357 }
1260 1358
1261 /// A `const String.fromEnvironment` constant. 1359 /// A `const String.fromEnvironment` constant.
1262 class StringFromEnvironmentConstantExpression 1360 class StringFromEnvironmentConstantExpression
1263 extends FromEnvironmentConstantExpression { 1361 extends FromEnvironmentConstantExpression {
1264 1362
1265 StringFromEnvironmentConstantExpression( 1363 StringFromEnvironmentConstantExpression(
1266 ConstantExpression name, 1364 ConstantExpression name,
1267 ConstantExpression defaultValue) 1365 ConstantExpression defaultValue)
1268 : super(name, defaultValue); 1366 : super(name, defaultValue);
(...skipping 29 matching lines...) Expand all
1298 } else { 1396 } else {
1299 return constantSystem.createString(new DartString.literal(text)); 1397 return constantSystem.createString(new DartString.literal(text));
1300 } 1398 }
1301 } 1399 }
1302 1400
1303 ConstantExpression apply(NormalizedArguments arguments) { 1401 ConstantExpression apply(NormalizedArguments arguments) {
1304 return new StringFromEnvironmentConstantExpression( 1402 return new StringFromEnvironmentConstantExpression(
1305 name.apply(arguments), 1403 name.apply(arguments),
1306 defaultValue != null ? defaultValue.apply(arguments) : null); 1404 defaultValue != null ? defaultValue.apply(arguments) : null);
1307 } 1405 }
1406
1407 @override
1408 DartType getKnownType(CoreTypes coreTypes) => coreTypes.stringType;
1308 } 1409 }
1309 1410
1310 /// A constant expression referenced with a deferred prefix. 1411 /// A constant expression referenced with a deferred prefix.
1311 /// For example `lib.C`. 1412 /// For example `lib.C`.
1312 class DeferredConstantExpression extends ConstantExpression { 1413 class DeferredConstantExpression extends ConstantExpression {
1313 final ConstantExpression expression; 1414 final ConstantExpression expression;
1314 final PrefixElement prefix; 1415 final PrefixElement prefix;
1315 1416
1316 DeferredConstantExpression(this.expression, this.prefix); 1417 DeferredConstantExpression(this.expression, this.prefix);
1317 1418
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
1654 visit(exp.name); 1755 visit(exp.name);
1655 if (exp.defaultValue != null) { 1756 if (exp.defaultValue != null) {
1656 sb.write(', defaultValue: '); 1757 sb.write(', defaultValue: ');
1657 visit(exp.defaultValue); 1758 visit(exp.defaultValue);
1658 } 1759 }
1659 sb.write(')'); 1760 sb.write(')');
1660 } 1761 }
1661 1762
1662 String toString() => sb.toString(); 1763 String toString() => sb.toString();
1663 } 1764 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/compiler.dart ('k') | pkg/compiler/lib/src/constants/values.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698