OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.constructors; | 5 library dart2js.constants.constructors; |
6 | 6 |
7 import '../dart_types.dart'; | 7 import '../dart_types.dart'; |
8 import '../elements/elements.dart' show | 8 import '../elements/elements.dart' show |
9 ConstructorElement, | 9 ConstructorElement, |
10 FieldElement; | 10 FieldElement; |
11 import '../universe/call_structure.dart' show | 11 import '../universe/call_structure.dart' show |
12 CallStructure; | 12 CallStructure; |
13 import '../util/util.dart'; | 13 import '../util/util.dart'; |
14 import 'evaluation.dart'; | 14 import 'evaluation.dart'; |
15 import 'expressions.dart'; | 15 import 'expressions.dart'; |
16 | 16 |
17 enum ConstantConstructorKind { | 17 enum ConstantConstructorKind { |
18 GENERATIVE, | 18 GENERATIVE, |
19 REDIRECTING_GENERATIVE, | 19 REDIRECTING_GENERATIVE, |
20 REDIRECTING_FACTORY, | 20 REDIRECTING_FACTORY, |
| 21 ERRONEOUS, |
21 } | 22 } |
22 | 23 |
23 /// Definition of a constant constructor. | 24 /// Definition of a constant constructor. |
24 abstract class ConstantConstructor { | 25 abstract class ConstantConstructor { |
25 ConstantConstructorKind get kind; | 26 ConstantConstructorKind get kind; |
26 | 27 |
27 /// Computes the type of the instance created in a const constructor | 28 /// Computes the type of the instance created in a const constructor |
28 /// invocation with type [newType]. | 29 /// invocation with type [newType]. |
29 InterfaceType computeInstanceType(InterfaceType newType); | 30 InterfaceType computeInstanceType(InterfaceType newType); |
30 | 31 |
31 /// Computes the constant expressions of the fields of the created instance | 32 /// Computes the constant expressions of the fields of the created instance |
32 /// in a const constructor invocation with [arguments]. | 33 /// in a const constructor invocation with [arguments]. |
33 Map<FieldElement, ConstantExpression> computeInstanceFields( | 34 Map<FieldElement, ConstantExpression> computeInstanceFields( |
34 List<ConstantExpression> arguments, | 35 List<ConstantExpression> arguments, |
35 CallStructure callStructure); | 36 CallStructure callStructure); |
36 | 37 |
| 38 ConstructorElement get parentConstructor; |
| 39 |
37 accept(ConstantConstructorVisitor visitor, arg); | 40 accept(ConstantConstructorVisitor visitor, arg); |
38 } | 41 } |
39 | 42 |
40 abstract class ConstantConstructorVisitor<R, A> { | 43 abstract class ConstantConstructorVisitor<R, A> { |
41 const ConstantConstructorVisitor(); | 44 const ConstantConstructorVisitor(); |
42 | 45 |
43 R visit(ConstantConstructor constantConstructor, A context) { | 46 R visit(ConstantConstructor constantConstructor, A context) { |
44 return constantConstructor.accept(this, context); | 47 return constantConstructor.accept(this, context); |
45 } | 48 } |
46 | 49 |
47 R visitGenerative(GenerativeConstantConstructor constructor, A arg); | 50 R visitGenerative(GenerativeConstantConstructor constructor, A arg); |
48 R visitRedirectingGenerative( | 51 R visitRedirectingGenerative( |
49 RedirectingGenerativeConstantConstructor constructor, A arg); | 52 RedirectingGenerativeConstantConstructor constructor, A arg); |
50 R visitRedirectingFactory( | 53 R visitRedirectingFactory( |
51 RedirectingFactoryConstantConstructor constructor, A arg); | 54 RedirectingFactoryConstantConstructor constructor, A arg); |
| 55 R visitErroneous(ErroneousConstantConstructor constructor, A arg); |
52 } | 56 } |
53 | 57 |
54 /// A generative constant constructor. | 58 /// A generative constant constructor. |
55 class GenerativeConstantConstructor implements ConstantConstructor{ | 59 class GenerativeConstantConstructor implements ConstantConstructor{ |
56 final InterfaceType type; | 60 final InterfaceType type; |
57 final Map<dynamic/*int|String*/, ConstantExpression> defaultValues; | 61 final Map<dynamic/*int|String*/, ConstantExpression> defaultValues; |
58 final Map<FieldElement, ConstantExpression> fieldMap; | 62 final Map<FieldElement, ConstantExpression> fieldMap; |
59 final ConstructedConstantExpression superConstructorInvocation; | 63 final ConstructedConstantExpression superConstructorInvocation; |
60 | 64 |
61 GenerativeConstantConstructor( | 65 GenerativeConstantConstructor( |
62 this.type, | 66 this.type, |
63 this.defaultValues, | 67 this.defaultValues, |
64 this.fieldMap, | 68 this.fieldMap, |
65 this.superConstructorInvocation); | 69 this.superConstructorInvocation); |
66 | 70 |
67 ConstantConstructorKind get kind => ConstantConstructorKind.GENERATIVE; | 71 ConstantConstructorKind get kind => ConstantConstructorKind.GENERATIVE; |
68 | 72 |
69 InterfaceType computeInstanceType(InterfaceType newType) { | 73 InterfaceType computeInstanceType(InterfaceType newType) { |
70 return type.substByContext(newType); | 74 return type.substByContext(newType); |
71 } | 75 } |
72 | 76 |
| 77 ConstructorElement get parentConstructor { |
| 78 if (superConstructorInvocation != null) { |
| 79 return superConstructorInvocation.target; |
| 80 } |
| 81 return null; |
| 82 } |
| 83 |
73 Map<FieldElement, ConstantExpression> computeInstanceFields( | 84 Map<FieldElement, ConstantExpression> computeInstanceFields( |
74 List<ConstantExpression> arguments, | 85 List<ConstantExpression> arguments, |
75 CallStructure callStructure) { | 86 CallStructure callStructure) { |
76 NormalizedArguments args = new NormalizedArguments( | 87 NormalizedArguments args = new NormalizedArguments( |
77 defaultValues, callStructure, arguments); | 88 defaultValues, callStructure, arguments); |
78 Map<FieldElement, ConstantExpression> appliedFieldMap = | 89 Map<FieldElement, ConstantExpression> appliedFieldMap = |
79 applyFields(args, superConstructorInvocation); | 90 applyFields(args, superConstructorInvocation); |
80 fieldMap.forEach((FieldElement field, ConstantExpression constant) { | 91 fieldMap.forEach((FieldElement field, ConstantExpression constant) { |
81 appliedFieldMap[field] = constant.apply(args); | 92 appliedFieldMap[field] = constant.apply(args); |
82 }); | 93 }); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 final ConstructedConstantExpression thisConstructorInvocation; | 166 final ConstructedConstantExpression thisConstructorInvocation; |
156 | 167 |
157 RedirectingGenerativeConstantConstructor( | 168 RedirectingGenerativeConstantConstructor( |
158 this.defaultValues, | 169 this.defaultValues, |
159 this.thisConstructorInvocation); | 170 this.thisConstructorInvocation); |
160 | 171 |
161 ConstantConstructorKind get kind { | 172 ConstantConstructorKind get kind { |
162 return ConstantConstructorKind.REDIRECTING_GENERATIVE; | 173 return ConstantConstructorKind.REDIRECTING_GENERATIVE; |
163 } | 174 } |
164 | 175 |
| 176 ConstructorElement get parentConstructor { |
| 177 return thisConstructorInvocation.target; |
| 178 } |
| 179 |
165 InterfaceType computeInstanceType(InterfaceType newType) { | 180 InterfaceType computeInstanceType(InterfaceType newType) { |
166 return thisConstructorInvocation.computeInstanceType() | 181 return thisConstructorInvocation.computeInstanceType() |
167 .substByContext(newType); | 182 .substByContext(newType); |
168 } | 183 } |
169 | 184 |
170 Map<FieldElement, ConstantExpression> computeInstanceFields( | 185 Map<FieldElement, ConstantExpression> computeInstanceFields( |
171 List<ConstantExpression> arguments, | 186 List<ConstantExpression> arguments, |
172 CallStructure callStructure) { | 187 CallStructure callStructure) { |
173 NormalizedArguments args = | 188 NormalizedArguments args = |
174 new NormalizedArguments(defaultValues, callStructure, arguments); | 189 new NormalizedArguments(defaultValues, callStructure, arguments); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 /// A redirecting factory constant constructor. | 226 /// A redirecting factory constant constructor. |
212 class RedirectingFactoryConstantConstructor implements ConstantConstructor { | 227 class RedirectingFactoryConstantConstructor implements ConstantConstructor { |
213 final ConstructedConstantExpression targetConstructorInvocation; | 228 final ConstructedConstantExpression targetConstructorInvocation; |
214 | 229 |
215 RedirectingFactoryConstantConstructor(this.targetConstructorInvocation); | 230 RedirectingFactoryConstantConstructor(this.targetConstructorInvocation); |
216 | 231 |
217 ConstantConstructorKind get kind { | 232 ConstantConstructorKind get kind { |
218 return ConstantConstructorKind.REDIRECTING_FACTORY; | 233 return ConstantConstructorKind.REDIRECTING_FACTORY; |
219 } | 234 } |
220 | 235 |
| 236 ConstructorElement get parentConstructor { |
| 237 return targetConstructorInvocation.target; |
| 238 } |
| 239 |
221 InterfaceType computeInstanceType(InterfaceType newType) { | 240 InterfaceType computeInstanceType(InterfaceType newType) { |
222 return targetConstructorInvocation.computeInstanceType() | 241 return targetConstructorInvocation.computeInstanceType() |
223 .substByContext(newType); | 242 .substByContext(newType); |
224 } | 243 } |
225 | 244 |
226 Map<FieldElement, ConstantExpression> computeInstanceFields( | 245 Map<FieldElement, ConstantExpression> computeInstanceFields( |
227 List<ConstantExpression> arguments, | 246 List<ConstantExpression> arguments, |
228 CallStructure callStructure) { | 247 CallStructure callStructure) { |
229 ConstantConstructor constantConstructor = | 248 ConstantConstructor constantConstructor = |
230 targetConstructorInvocation.target.constantConstructor; | 249 targetConstructorInvocation.target.constantConstructor; |
(...skipping 15 matching lines...) Expand all Loading... |
246 } | 265 } |
247 | 266 |
248 String toString() { | 267 String toString() { |
249 StringBuffer sb = new StringBuffer(); | 268 StringBuffer sb = new StringBuffer(); |
250 sb.write("{"); | 269 sb.write("{"); |
251 sb.write("'constructor': ${targetConstructorInvocation.getText()}"); | 270 sb.write("'constructor': ${targetConstructorInvocation.getText()}"); |
252 sb.write("}"); | 271 sb.write("}"); |
253 return sb.toString(); | 272 return sb.toString(); |
254 } | 273 } |
255 } | 274 } |
| 275 |
| 276 /// An erroneous constant constructor. |
| 277 class ErroneousConstantConstructor implements ConstantConstructor { |
| 278 const ErroneousConstantConstructor(); |
| 279 |
| 280 @override |
| 281 accept(ConstantConstructorVisitor visitor, arg) { |
| 282 return visitor.visitErroneous(this, arg); |
| 283 } |
| 284 |
| 285 @override |
| 286 Map<FieldElement, ConstantExpression> computeInstanceFields( |
| 287 List<ConstantExpression> arguments, CallStructure callStructure) { |
| 288 return null; |
| 289 } |
| 290 |
| 291 @override |
| 292 InterfaceType computeInstanceType(InterfaceType newType) { |
| 293 throw new UnsupportedError( |
| 294 'ErroneousConstantConstructor.computeInstanceType is unsupported.'); |
| 295 } |
| 296 |
| 297 @override |
| 298 ConstantConstructorKind get kind => ConstantConstructorKind.ERRONEOUS; |
| 299 |
| 300 @override |
| 301 ConstructorElement get parentConstructor => null; |
| 302 } |
OLD | NEW |