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 '../constants/constant_system.dart'; | 7 import '../constants/constant_system.dart'; |
8 import '../core_types.dart'; | 8 import '../core_types.dart'; |
9 import '../dart2jslib.dart' show assertDebugMode, Compiler; | 9 import '../dart2jslib.dart' show assertDebugMode, Compiler; |
10 import '../dart_types.dart'; | 10 import '../dart_types.dart'; |
11 import '../elements/elements.dart' show | 11 import '../elements/elements.dart' show |
12 ConstructorElement, | 12 ConstructorElement, |
13 Element, | 13 Element, |
14 FieldElement, | 14 FieldElement, |
15 FunctionElement, | 15 FunctionElement, |
16 PrefixElement, | 16 PrefixElement, |
17 VariableElement; | 17 VariableElement; |
18 import '../resolution/operators.dart'; | 18 import '../resolution/operators.dart'; |
19 import '../tree/tree.dart' show DartString; | 19 import '../tree/tree.dart' show DartString; |
20 import '../universe/universe.dart' show CallStructure; | 20 import '../universe/universe.dart' show CallStructure; |
| 21 import '../util/util.dart'; |
21 import 'values.dart'; | 22 import 'values.dart'; |
22 | 23 |
23 enum ConstantExpressionKind { | 24 enum ConstantExpressionKind { |
24 BINARY, | 25 BINARY, |
25 BOOL, | 26 BOOL, |
26 BOOL_FROM_ENVIRONMENT, | 27 BOOL_FROM_ENVIRONMENT, |
27 CONCATENATE, | 28 CONCATENATE, |
28 CONDITIONAL, | 29 CONDITIONAL, |
29 CONSTRUCTED, | 30 CONSTRUCTED, |
30 DEFERRED, | 31 DEFERRED, |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 NormalizedArguments args = new NormalizedArguments( | 135 NormalizedArguments args = new NormalizedArguments( |
135 defaultValues, callStructure, arguments); | 136 defaultValues, callStructure, arguments); |
136 Map<FieldElement, ConstantExpression> appliedFieldMap = | 137 Map<FieldElement, ConstantExpression> appliedFieldMap = |
137 applyFields(args, superConstructorInvocation); | 138 applyFields(args, superConstructorInvocation); |
138 fieldMap.forEach((FieldElement field, ConstantExpression constant) { | 139 fieldMap.forEach((FieldElement field, ConstantExpression constant) { |
139 appliedFieldMap[field] = constant.apply(args); | 140 appliedFieldMap[field] = constant.apply(args); |
140 }); | 141 }); |
141 return appliedFieldMap; | 142 return appliedFieldMap; |
142 } | 143 } |
143 | 144 |
| 145 int get hashCode { |
| 146 int hash = Hashing.objectHash(type); |
| 147 hash = Hashing.mapHash(defaultValues, hash); |
| 148 hash = Hashing.mapHash(fieldMap, hash); |
| 149 return Hashing.objectHash(superConstructorInvocation, hash); |
| 150 } |
| 151 |
| 152 bool operator ==(other) { |
| 153 if (identical(this, other)) return true; |
| 154 if (other is! GenerativeConstantConstructor) return false; |
| 155 return |
| 156 type == other.type && |
| 157 superConstructorInvocation == other.superConstructorInvocation && |
| 158 mapEquals(defaultValues, other.defaultValues) && |
| 159 mapEquals(fieldMap, other.fieldMap); |
| 160 } |
| 161 |
144 String toString() { | 162 String toString() { |
145 StringBuffer sb = new StringBuffer(); | 163 StringBuffer sb = new StringBuffer(); |
146 sb.write("{'type': $type"); | 164 sb.write("{'type': $type"); |
147 defaultValues.forEach((key, ConstantExpression expression) { | 165 defaultValues.forEach((key, ConstantExpression expression) { |
148 sb.write(",\n 'default:${key}': ${expression.getText()}"); | 166 sb.write(",\n 'default:${key}': ${expression.getText()}"); |
149 }); | 167 }); |
150 fieldMap.forEach((FieldElement field, ConstantExpression expression) { | 168 fieldMap.forEach((FieldElement field, ConstantExpression expression) { |
151 sb.write(",\n 'field:${field}': ${expression.getText()}"); | 169 sb.write(",\n 'field:${field}': ${expression.getText()}"); |
152 }); | 170 }); |
153 if (superConstructorInvocation != null) { | 171 if (superConstructorInvocation != null) { |
154 sb.write(",\n 'constructor: ${superConstructorInvocation.getText()}"); | 172 sb.write(",\n 'constructor: ${superConstructorInvocation.getText()}"); |
155 } | 173 } |
156 sb.write("}"); | 174 sb.write("}"); |
157 return sb.toString(); | 175 return sb.toString(); |
158 } | 176 } |
159 | 177 |
| 178 static bool mapEquals(Map map1, Map map2) { |
| 179 if (map1.length != map1.length) return false; |
| 180 for (var key in map1.keys) { |
| 181 if (map1[key] != map2[key]) { |
| 182 return false; |
| 183 } |
| 184 } |
| 185 return true; |
| 186 } |
| 187 |
160 /// Creates the field-to-constant map from applying [args] to | 188 /// Creates the field-to-constant map from applying [args] to |
161 /// [constructorInvocation]. If [constructorInvocation] is `null`, an empty | 189 /// [constructorInvocation]. If [constructorInvocation] is `null`, an empty |
162 /// map is created. | 190 /// map is created. |
163 static Map<FieldElement, ConstantExpression> applyFields( | 191 static Map<FieldElement, ConstantExpression> applyFields( |
164 NormalizedArguments args, | 192 NormalizedArguments args, |
165 ConstructedConstantExpression constructorInvocation) { | 193 ConstructedConstantExpression constructorInvocation) { |
166 Map<FieldElement, ConstantExpression> appliedFieldMap = | 194 Map<FieldElement, ConstantExpression> appliedFieldMap = |
167 <FieldElement, ConstantExpression>{}; | 195 <FieldElement, ConstantExpression>{}; |
168 if (constructorInvocation != null) { | 196 if (constructorInvocation != null) { |
169 Map<FieldElement, ConstantExpression> fieldMap = | 197 Map<FieldElement, ConstantExpression> fieldMap = |
(...skipping 28 matching lines...) Expand all Loading... |
198 List<ConstantExpression> arguments, | 226 List<ConstantExpression> arguments, |
199 CallStructure callStructure) { | 227 CallStructure callStructure) { |
200 NormalizedArguments args = | 228 NormalizedArguments args = |
201 new NormalizedArguments(defaultValues, callStructure, arguments); | 229 new NormalizedArguments(defaultValues, callStructure, arguments); |
202 Map<FieldElement, ConstantExpression> appliedFieldMap = | 230 Map<FieldElement, ConstantExpression> appliedFieldMap = |
203 GenerativeConstantConstructor.applyFields( | 231 GenerativeConstantConstructor.applyFields( |
204 args, thisConstructorInvocation); | 232 args, thisConstructorInvocation); |
205 return appliedFieldMap; | 233 return appliedFieldMap; |
206 } | 234 } |
207 | 235 |
| 236 int get hashCode { |
| 237 int hash = Hashing.objectHash(thisConstructorInvocation); |
| 238 return Hashing.mapHash(defaultValues, hash); |
| 239 } |
| 240 |
| 241 bool operator ==(other) { |
| 242 if (identical(this, other)) return true; |
| 243 if (other is! RedirectingGenerativeConstantConstructor) return false; |
| 244 return |
| 245 thisConstructorInvocation == other.thisConstructorInvocation && |
| 246 GenerativeConstantConstructor.mapEquals( |
| 247 defaultValues, other.defaultValues); |
| 248 } |
| 249 |
208 String toString() { | 250 String toString() { |
209 StringBuffer sb = new StringBuffer(); | 251 StringBuffer sb = new StringBuffer(); |
210 sb.write("{'type': ${thisConstructorInvocation.type}"); | 252 sb.write("{'type': ${thisConstructorInvocation.type}"); |
211 defaultValues.forEach((key, ConstantExpression expression) { | 253 defaultValues.forEach((key, ConstantExpression expression) { |
212 sb.write(",\n 'default:${key}': ${expression.getText()}"); | 254 sb.write(",\n 'default:${key}': ${expression.getText()}"); |
213 }); | 255 }); |
214 sb.write(",\n 'constructor': ${thisConstructorInvocation.getText()}"); | 256 sb.write(",\n 'constructor': ${thisConstructorInvocation.getText()}"); |
215 sb.write("}"); | 257 sb.write("}"); |
216 return sb.toString(); | 258 return sb.toString(); |
217 } | 259 } |
(...skipping 15 matching lines...) Expand all Loading... |
233 } | 275 } |
234 | 276 |
235 Map<FieldElement, ConstantExpression> computeInstanceFields( | 277 Map<FieldElement, ConstantExpression> computeInstanceFields( |
236 List<ConstantExpression> arguments, | 278 List<ConstantExpression> arguments, |
237 CallStructure callStructure) { | 279 CallStructure callStructure) { |
238 ConstantConstructor constantConstructor = | 280 ConstantConstructor constantConstructor = |
239 targetConstructorInvocation.target.constantConstructor; | 281 targetConstructorInvocation.target.constantConstructor; |
240 return constantConstructor.computeInstanceFields(arguments, callStructure); | 282 return constantConstructor.computeInstanceFields(arguments, callStructure); |
241 } | 283 } |
242 | 284 |
| 285 int get hashCode { |
| 286 return Hashing.objectHash(targetConstructorInvocation); |
| 287 } |
| 288 |
| 289 bool operator ==(other) { |
| 290 if (identical(this, other)) return true; |
| 291 if (other is! RedirectingFactoryConstantConstructor) return false; |
| 292 return targetConstructorInvocation == other.targetConstructorInvocation; |
| 293 } |
| 294 |
243 String toString() { | 295 String toString() { |
244 StringBuffer sb = new StringBuffer(); | 296 StringBuffer sb = new StringBuffer(); |
245 sb.write("{"); | 297 sb.write("{"); |
246 sb.write("'constructor': ${targetConstructorInvocation.getText()}"); | 298 sb.write("'constructor': ${targetConstructorInvocation.getText()}"); |
247 sb.write("}"); | 299 sb.write("}"); |
248 return sb.toString(); | 300 return sb.toString(); |
249 } | 301 } |
250 } | 302 } |
251 | 303 |
252 /// An expression that is a compile-time constant. | 304 /// An expression that is a compile-time constant. |
(...skipping 1542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1795 visit(exp.name); | 1847 visit(exp.name); |
1796 if (exp.defaultValue != null) { | 1848 if (exp.defaultValue != null) { |
1797 sb.write(', defaultValue: '); | 1849 sb.write(', defaultValue: '); |
1798 visit(exp.defaultValue); | 1850 visit(exp.defaultValue); |
1799 } | 1851 } |
1800 sb.write(')'); | 1852 sb.write(')'); |
1801 } | 1853 } |
1802 | 1854 |
1803 String toString() => sb.toString(); | 1855 String toString() => sb.toString(); |
1804 } | 1856 } |
OLD | NEW |