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

Side by Side Diff: frog/leg/compile_time_constants.dart

Issue 9866027: Error out on compile-time constants if equality is tested on bad types. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | frog/leg/operations.dart » ('j') | frog/leg/operations.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 class Constant implements Hashable { 5 class Constant implements Hashable {
6 const Constant(); 6 const Constant();
7 7
8 bool isNull() => false; 8 bool isNull() => false;
9 bool isBool() => false; 9 bool isBool() => false;
10 bool isTrue() => false; 10 bool isTrue() => false;
11 bool isFalse() => false; 11 bool isFalse() => false;
12 bool isInt() => false; 12 bool isInt() => false;
13 bool isDouble() => false; 13 bool isDouble() => false;
14 bool isNum() => false; 14 bool isNum() => false;
15 bool isString() => false; 15 bool isString() => false;
16 bool isList() => false; 16 bool isList() => false;
17 bool isMap() => false; 17 bool isMap() => false;
18 bool isConstructedObject() => false; 18 bool isConstructedObject() => false;
19 /** Returns true if the constant is null, a bool, a number or a string. */
20 bool isPrimitive() => false;
19 /** Returns true if the constant is a list, a map or a constructed object. */ 21 /** Returns true if the constant is a list, a map or a constructed object. */
20 bool isObject() => false; 22 bool isObject() => false;
21 23
22 abstract void writeJsCode(StringBuffer buffer, ConstantHandler handler); 24 abstract void writeJsCode(StringBuffer buffer, ConstantHandler handler);
23 abstract List<Constant> getDependencies(); 25 abstract List<Constant> getDependencies();
24 } 26 }
25 27
26 class PrimitiveConstant extends Constant { 28 class PrimitiveConstant extends Constant {
27 abstract get value(); 29 abstract get value();
28 const PrimitiveConstant(); 30 const PrimitiveConstant();
31 bool isPrimitive() => true;
29 32
30 bool operator ==(var other) { 33 bool operator ==(var other) {
31 if (other is !PrimitiveConstant) return false; 34 if (other is !PrimitiveConstant) return false;
32 PrimitiveConstant otherPrimitive = other; 35 PrimitiveConstant otherPrimitive = other;
33 // We use == instead of === so that DartStrings compare correctly. 36 // We use == instead of === so that DartStrings compare correctly.
34 return value == otherPrimitive.value; 37 return value == otherPrimitive.value;
35 } 38 }
36 39
37 String toString() => value.toString(); 40 String toString() => value.toString();
38 // Primitive constants don't have dependencies. 41 // Primitive constants don't have dependencies.
(...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 case "<=": 893 case "<=":
891 folded = const LessEqualOperation().fold(left, right); 894 folded = const LessEqualOperation().fold(left, right);
892 break; 895 break;
893 case ">": 896 case ">":
894 folded = const GreaterOperation().fold(left, right); 897 folded = const GreaterOperation().fold(left, right);
895 break; 898 break;
896 case ">=": 899 case ">=":
897 folded = const GreaterEqualOperation().fold(left, right); 900 folded = const GreaterEqualOperation().fold(left, right);
898 break; 901 break;
899 case "==": 902 case "==":
900 folded = const EqualsOperation().fold(left, right); 903 if (left.isPrimitive() && right.isPrimitive()) {
Lasse Reichstein Nielsen 2012/03/27 08:12:12 Is "null" a numeric, string or boolean value? It s
floitsch 2012/03/28 00:25:21 Filed already a bug, and Gilad confirmed that null
904 folded = const EqualsOperation().fold(left, right);
905 }
901 break; 906 break;
902 case "===": 907 case "===":
903 folded = const IdentityOperation().fold(left, right); 908 if (left.isPrimitive() && right.isPrimitive()) {
909 folded = const IdentityOperation().fold(left, right);
910 }
904 break; 911 break;
905 case "!=": 912 case "!=":
906 BoolConstant areEquals = const EqualsOperation().fold(left, right); 913 if (left.isPrimitive() && right.isPrimitive()) {
907 if (areEquals === null) { 914 BoolConstant areEquals = const EqualsOperation().fold(left, right);
908 folded = null; 915 if (areEquals === null) {
909 } else { 916 folded = null;
910 folded = areEquals.negate(); 917 } else {
918 folded = areEquals.negate();
919 }
911 } 920 }
912 break; 921 break;
913 case "!==": 922 case "!==":
914 BoolConstant areIdentical = 923 if (left.isPrimitive() && right.isPrimitive()) {
915 const IdentityOperation().fold(left, right); 924 BoolConstant areIdentical =
916 if (areIdentical === null) { 925 const IdentityOperation().fold(left, right);
917 folded = null; 926 if (areIdentical === null) {
Lasse Reichstein Nielsen 2012/03/27 08:12:12 Have you considered having a dedicated FoldingFail
floitsch 2012/03/28 00:25:21 I have considered it, but the problem is, that you
918 } else { 927 folded = null;
919 folded = areIdentical.negate(); 928 } else {
929 folded = areIdentical.negate();
930 }
920 } 931 }
921 break; 932 break;
922 default: 933 default:
923 compiler.internalError("Unexpected operator.", node: op); 934 compiler.internalError("Unexpected operator.", node: op);
924 break; 935 break;
925 } 936 }
926 if (folded === null) error(send); 937 if (folded === null) error(send);
927 return folded; 938 return folded;
928 } 939 }
929 return super.visitSend(send); 940 return super.visitSend(send);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1060 return node.expression.accept(this); 1071 return node.expression.accept(this);
1061 } 1072 }
1062 1073
1063 error(Node node) { 1074 error(Node node) {
1064 // TODO(floitsch): get the list of constants that are currently compiled 1075 // TODO(floitsch): get the list of constants that are currently compiled
1065 // and present some kind of stack-trace. 1076 // and present some kind of stack-trace.
1066 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; 1077 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT;
1067 compiler.reportError(node, new CompileTimeConstantError(kind, const [])); 1078 compiler.reportError(node, new CompileTimeConstantError(kind, const []));
1068 } 1079 }
1069 } 1080 }
OLDNEW
« no previous file with comments | « no previous file | frog/leg/operations.dart » ('j') | frog/leg/operations.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698