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

Side by Side Diff: pkg/compiler/lib/src/js_emitter/constant_ordering.dart

Issue 1491413008: Canonical output ordering for constants. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library dart2js.js_emitter.constant_ordering;
6
7 import '../constants/values.dart';
8
9 import '../common.dart';
10 //import '../core_types.dart';
11 import '../dart_types.dart';
12 import '../elements/elements.dart'
13 show Element,
14 Elements,
15 FieldElement;
16
17 /// A canonical but arbrary ordering of constants. The ordering is 'stable'
18 /// under perturbation of the source.
19 int deepCompareConstants(ConstantValue a, ConstantValue b) {
20 return _CompareVisitor.compareValues(a, b);
21 }
22
23 class _CompareVisitor implements ConstantValueVisitor<int, ConstantValue> {
24 const _CompareVisitor();
25
26 static int compareValues(ConstantValue a, ConstantValue b) {
27 if (identical(a, b)) return 0;
28 int r = _KindVisitor.kind(a).compareTo(_KindVisitor.kind(b));
29 if (r != 0) return r;
30 r = a.accept(const _CompareVisitor(), b);
31 assert(r != 0); // Constants are canonicalized.
32 return r;
33 }
34
35 static int compareNullable(int compare(a, b), a, b) {
36 if (a == null && b == null) return 0;
37 if (a == null) return -1;
38 if (b == null) return 1;
39 return compare(a, b);
40 }
41
42 static int compareLists(int compare(a, b), List a, List b) {
Siggi Cherem (dart-lang) 2015/12/09 00:36:23 nit/idea: maybe make `compare` optional and defaul
sra1 2015/12/09 23:15:24 But then it has to go last, and if we need to make
43 int r = a.length.compareTo(b.length);
44 if (r != 0) return r;
45 for (int i = 0; i < a.length; i++) {
46 r = compare(a[i], b[i]);
47 if (r != 0) return r;
48 }
49 return 0;
50 }
51
52 static int compareElements(Element a, Element b) {
53 int r = a.name.compareTo(b.name);
54 if (r != 0) return r;
55 return Elements.compareByPosition(a, b);
56 }
57
58 static int compareDartTypes(DartType a, DartType b) {
59 if (a == b) return 0;
60 int r = a.kind.index.compareTo(b.kind.index);
61 if (r != 0) return r;
62 //int r = a.name.compareTo(b.name);
Siggi Cherem (dart-lang) 2015/12/09 00:36:23 delete commented code
sra1 2015/12/09 23:15:24 Done.
63 //if (r != 0) return r;
64 r = compareNullable(compareElements, a.element, b.element);
65 if (r != 0) return r;
66
67 if (a is GenericType) {
68 GenericType aGeneric = a;
69 GenericType bGeneric = b;
70 r = compareLists(compareDartTypes,
71 aGeneric.typeArguments, bGeneric.typeArguments);
72 if (r != 0) return r;
73 }
74 throw 'compareDartTypes $a $b';
75 }
76
77 int visitFunction(FunctionConstantValue a, FunctionConstantValue b) {
78 return compareElements(a.element, b.element);
79 }
80
81 int visitNull(NullConstantValue a, NullConstantValue b) {
82 throw 'visitNull\n $a\n $b';
83 return 0;
84 }
85
86 int visitInt(IntConstantValue a, IntConstantValue b) {
87 return a.primitiveValue.compareTo(b.primitiveValue);
88 }
89
90 int visitDouble(DoubleConstantValue a, DoubleConstantValue b) {
91 return a.primitiveValue.compareTo(b.primitiveValue);
92 }
93
94 int visitBool(BoolConstantValue a, BoolConstantValue b) {
95 int aInt = a.primitiveValue ? 1 : 0;
96 int bInt = b.primitiveValue ? 1 : 0;
97 return aInt.compareTo(bInt);
Siggi Cherem (dart-lang) 2015/12/09 00:36:23 can't believe `bool` is not comparable! :(
sra1 2015/12/09 23:15:24 Acknowledged.
98 }
99
100 int visitString(StringConstantValue a, StringConstantValue b) {
101 DartString aString = a.primitiveValue;
102 DartString bString = b.primitiveValue;
103 int r = aString.length.compareTo(bString.length);
104 if (r != 0) return r;
105 return aString.slowToString().compareTo(bString.slowToString());
106 }
107
108 int visitList(ListConstantValue a, ListConstantValue b) {
109 int r = compareLists(compare, a.entries, b.entries);
Siggi Cherem (dart-lang) 2015/12/09 00:36:23 I couldn't find the definition of `compare`, shoul
sra1 2015/12/09 23:15:23 Thanks, a missed last-minute rename not exercised
110 if (r != 0) return r;
111 return compareDartTypes(a.type, b.type);
112 }
113
114 int visitMap(MapConstantValue a, MapConstantValue b) {
115 int r = compareLists(compare, a.keys, b.keys);
116 if (r != 0) return r;
117 r = compareLists(compare, a.values, b.values);
118 if (r != 0) return r;
119 return compareDartTypes(a.type, b.type);
120 }
121
122 int visitConstructed(ConstructedConstantValue a, ConstructedConstantValue b) {
123 int r = compareDartTypes(a.type, b.type);
124 if (r != 0) return r;
125
126 List<FieldElement> aFields = a.fields.keys.toList()..sort(compareElements);
127 List<FieldElement> bFields = b.fields.keys.toList()..sort(compareElements);
128
129 r = compareLists(compareElements, aFields, bFields);
130 if (r != 0) return r;
131
132 return compareLists(compareValues,
133 aFields.map((field) => a.fields[field]).toList(),
134 aFields.map((field) => b.fields[field]).toList());
135 }
136
137 int visitType(TypeConstantValue a, TypeConstantValue b) {
138 int r = compareDartTypes(a.representedType, b.representedType);
139 if (r != 0) return r;
140 return compareDartTypes(a.type, b.type);
141 }
142
143 int visitInterceptor(InterceptorConstantValue a, InterceptorConstantValue b) {
144 return compareDartTypes(a.dispatchedType, b.dispatchedType);
145 }
146
147 int visitSynthetic(SyntheticConstantValue a, SyntheticConstantValue b) {
Siggi Cherem (dart-lang) 2015/12/09 00:36:23 seems like there are multiple possible values here
sra1 2015/12/09 23:15:24 The payload is completely abstract. Lucky for us t
148 return 0;
149 }
150
151 int visitDeferred(DeferredConstantValue a, DeferredConstantValue b) {
152 int r = compareValues(a.referenced, b.referenced);
153 if (r != 0) return r;
154 return compareElements(a.prefix, b.prefix);
155 }
156 }
157
158 class _KindVisitor implements ConstantValueVisitor<int, Null> {
159 const _KindVisitor();
160
161 static const int FUNCTION = 1;
162 static const int NULL = 2;
163 static const int INT = 3;
164 static const int DOUBLE = 4;
165 static const int BOOL = 5;
166 static const int STRING = 6;
167 static const int LIST = 7;
168 static const int MAP = 8;
169 static const int CONSTRUCTED = 9;
170 static const int TYPE = 10;
171 static const int INTERCEPTOR = 11;
172 static const int SYNTHETIC = 12;
173 static const int DEFERRED = 13;
174
175 static int kind(ConstantValue constant) =>
176 constant.accept(const _KindVisitor(), null);
177
178 int visitFunction(FunctionConstantValue a, _) => FUNCTION;
179 int visitNull(NullConstantValue a, _) => NULL;
180 int visitInt(IntConstantValue a, _) => INT;
181 int visitDouble(DoubleConstantValue a, _) => DOUBLE;
182 int visitBool(BoolConstantValue a, _) => BOOL;
183 int visitString(StringConstantValue a, _) => STRING;
184 int visitList(ListConstantValue a, _) => LIST;
185 int visitMap(MapConstantValue a, _) => MAP;
186 int visitConstructed(ConstructedConstantValue a, _) => CONSTRUCTED;
187 int visitType(TypeConstantValue a, _) => TYPE;
188 int visitInterceptor(InterceptorConstantValue a, _) => INTERCEPTOR;
189 int visitSynthetic(SyntheticConstantValue a, _) => SYNTHETIC;
190 int visitDeferred(DeferredConstantValue a, _) => DEFERRED;
191 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/js_backend/namer.dart ('k') | pkg/compiler/lib/src/js_emitter/full_emitter/emitter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698