| 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 '../elements/resolution_types.dart'; | 7 import '../elements/resolution_types.dart'; |
| 8 import '../elements/elements.dart' show FieldElement; | 8 import '../elements/elements.dart' show FieldElement; |
| 9 import '../universe/call_structure.dart' show CallStructure; | 9 import '../universe/call_structure.dart' show CallStructure; |
| 10 import '../util/util.dart'; | 10 import '../util/util.dart'; |
| 11 import 'evaluation.dart'; | 11 import 'evaluation.dart'; |
| 12 import 'expressions.dart'; | 12 import 'expressions.dart'; |
| 13 | 13 |
| 14 enum ConstantConstructorKind { | 14 enum ConstantConstructorKind { |
| 15 GENERATIVE, | 15 GENERATIVE, |
| 16 REDIRECTING_GENERATIVE, | 16 REDIRECTING_GENERATIVE, |
| 17 REDIRECTING_FACTORY, | 17 REDIRECTING_FACTORY, |
| 18 } | 18 } |
| 19 | 19 |
| 20 /// Definition of a constant constructor. | 20 /// Definition of a constant constructor. |
| 21 abstract class ConstantConstructor { | 21 abstract class ConstantConstructor { |
| 22 ConstantConstructorKind get kind; | 22 ConstantConstructorKind get kind; |
| 23 | 23 |
| 24 /// Computes the type of the instance created in a const constructor | 24 /// Computes the type of the instance created in a const constructor |
| 25 /// invocation with type [newType]. | 25 /// invocation with type [newType]. |
| 26 InterfaceType computeInstanceType(InterfaceType newType); | 26 ResolutionInterfaceType computeInstanceType(ResolutionInterfaceType newType); |
| 27 | 27 |
| 28 /// Computes the constant expressions of the fields of the created instance | 28 /// Computes the constant expressions of the fields of the created instance |
| 29 /// in a const constructor invocation with [arguments]. | 29 /// in a const constructor invocation with [arguments]. |
| 30 Map<FieldElement, ConstantExpression> computeInstanceFields( | 30 Map<FieldElement, ConstantExpression> computeInstanceFields( |
| 31 List<ConstantExpression> arguments, CallStructure callStructure); | 31 List<ConstantExpression> arguments, CallStructure callStructure); |
| 32 | 32 |
| 33 accept(ConstantConstructorVisitor visitor, arg); | 33 accept(ConstantConstructorVisitor visitor, arg); |
| 34 } | 34 } |
| 35 | 35 |
| 36 abstract class ConstantConstructorVisitor<R, A> { | 36 abstract class ConstantConstructorVisitor<R, A> { |
| 37 const ConstantConstructorVisitor(); | 37 const ConstantConstructorVisitor(); |
| 38 | 38 |
| 39 R visit(ConstantConstructor constantConstructor, A context) { | 39 R visit(ConstantConstructor constantConstructor, A context) { |
| 40 return constantConstructor.accept(this, context); | 40 return constantConstructor.accept(this, context); |
| 41 } | 41 } |
| 42 | 42 |
| 43 R visitGenerative(GenerativeConstantConstructor constructor, A arg); | 43 R visitGenerative(GenerativeConstantConstructor constructor, A arg); |
| 44 R visitRedirectingGenerative( | 44 R visitRedirectingGenerative( |
| 45 RedirectingGenerativeConstantConstructor constructor, A arg); | 45 RedirectingGenerativeConstantConstructor constructor, A arg); |
| 46 R visitRedirectingFactory( | 46 R visitRedirectingFactory( |
| 47 RedirectingFactoryConstantConstructor constructor, A arg); | 47 RedirectingFactoryConstantConstructor constructor, A arg); |
| 48 } | 48 } |
| 49 | 49 |
| 50 /// A generative constant constructor. | 50 /// A generative constant constructor. |
| 51 class GenerativeConstantConstructor implements ConstantConstructor { | 51 class GenerativeConstantConstructor implements ConstantConstructor { |
| 52 final InterfaceType type; | 52 final ResolutionInterfaceType type; |
| 53 final Map<dynamic /*int|String*/, ConstantExpression> defaultValues; | 53 final Map<dynamic /*int|String*/, ConstantExpression> defaultValues; |
| 54 final Map<FieldElement, ConstantExpression> fieldMap; | 54 final Map<FieldElement, ConstantExpression> fieldMap; |
| 55 final ConstructedConstantExpression superConstructorInvocation; | 55 final ConstructedConstantExpression superConstructorInvocation; |
| 56 | 56 |
| 57 GenerativeConstantConstructor(this.type, this.defaultValues, this.fieldMap, | 57 GenerativeConstantConstructor(this.type, this.defaultValues, this.fieldMap, |
| 58 this.superConstructorInvocation); | 58 this.superConstructorInvocation); |
| 59 | 59 |
| 60 ConstantConstructorKind get kind => ConstantConstructorKind.GENERATIVE; | 60 ConstantConstructorKind get kind => ConstantConstructorKind.GENERATIVE; |
| 61 | 61 |
| 62 InterfaceType computeInstanceType(InterfaceType newType) { | 62 ResolutionInterfaceType computeInstanceType(ResolutionInterfaceType newType) { |
| 63 return type.substByContext(newType); | 63 return type.substByContext(newType); |
| 64 } | 64 } |
| 65 | 65 |
| 66 Map<FieldElement, ConstantExpression> computeInstanceFields( | 66 Map<FieldElement, ConstantExpression> computeInstanceFields( |
| 67 List<ConstantExpression> arguments, CallStructure callStructure) { | 67 List<ConstantExpression> arguments, CallStructure callStructure) { |
| 68 NormalizedArguments args = | 68 NormalizedArguments args = |
| 69 new NormalizedArguments(defaultValues, callStructure, arguments); | 69 new NormalizedArguments(defaultValues, callStructure, arguments); |
| 70 Map<FieldElement, ConstantExpression> appliedFieldMap = | 70 Map<FieldElement, ConstantExpression> appliedFieldMap = |
| 71 applyFields(args, superConstructorInvocation); | 71 applyFields(args, superConstructorInvocation); |
| 72 fieldMap.forEach((FieldElement field, ConstantExpression constant) { | 72 fieldMap.forEach((FieldElement field, ConstantExpression constant) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 final Map<dynamic /*int|String*/, ConstantExpression> defaultValues; | 145 final Map<dynamic /*int|String*/, ConstantExpression> defaultValues; |
| 146 final ConstructedConstantExpression thisConstructorInvocation; | 146 final ConstructedConstantExpression thisConstructorInvocation; |
| 147 | 147 |
| 148 RedirectingGenerativeConstantConstructor( | 148 RedirectingGenerativeConstantConstructor( |
| 149 this.defaultValues, this.thisConstructorInvocation); | 149 this.defaultValues, this.thisConstructorInvocation); |
| 150 | 150 |
| 151 ConstantConstructorKind get kind { | 151 ConstantConstructorKind get kind { |
| 152 return ConstantConstructorKind.REDIRECTING_GENERATIVE; | 152 return ConstantConstructorKind.REDIRECTING_GENERATIVE; |
| 153 } | 153 } |
| 154 | 154 |
| 155 InterfaceType computeInstanceType(InterfaceType newType) { | 155 ResolutionInterfaceType computeInstanceType(ResolutionInterfaceType newType) { |
| 156 return thisConstructorInvocation | 156 return thisConstructorInvocation |
| 157 .computeInstanceType() | 157 .computeInstanceType() |
| 158 .substByContext(newType); | 158 .substByContext(newType); |
| 159 } | 159 } |
| 160 | 160 |
| 161 Map<FieldElement, ConstantExpression> computeInstanceFields( | 161 Map<FieldElement, ConstantExpression> computeInstanceFields( |
| 162 List<ConstantExpression> arguments, CallStructure callStructure) { | 162 List<ConstantExpression> arguments, CallStructure callStructure) { |
| 163 NormalizedArguments args = | 163 NormalizedArguments args = |
| 164 new NormalizedArguments(defaultValues, callStructure, arguments); | 164 new NormalizedArguments(defaultValues, callStructure, arguments); |
| 165 Map<FieldElement, ConstantExpression> appliedFieldMap = | 165 Map<FieldElement, ConstantExpression> appliedFieldMap = |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 /// A redirecting factory constant constructor. | 200 /// A redirecting factory constant constructor. |
| 201 class RedirectingFactoryConstantConstructor implements ConstantConstructor { | 201 class RedirectingFactoryConstantConstructor implements ConstantConstructor { |
| 202 final ConstructedConstantExpression targetConstructorInvocation; | 202 final ConstructedConstantExpression targetConstructorInvocation; |
| 203 | 203 |
| 204 RedirectingFactoryConstantConstructor(this.targetConstructorInvocation); | 204 RedirectingFactoryConstantConstructor(this.targetConstructorInvocation); |
| 205 | 205 |
| 206 ConstantConstructorKind get kind { | 206 ConstantConstructorKind get kind { |
| 207 return ConstantConstructorKind.REDIRECTING_FACTORY; | 207 return ConstantConstructorKind.REDIRECTING_FACTORY; |
| 208 } | 208 } |
| 209 | 209 |
| 210 InterfaceType computeInstanceType(InterfaceType newType) { | 210 ResolutionInterfaceType computeInstanceType(ResolutionInterfaceType newType) { |
| 211 return targetConstructorInvocation | 211 return targetConstructorInvocation |
| 212 .computeInstanceType() | 212 .computeInstanceType() |
| 213 .substByContext(newType); | 213 .substByContext(newType); |
| 214 } | 214 } |
| 215 | 215 |
| 216 Map<FieldElement, ConstantExpression> computeInstanceFields( | 216 Map<FieldElement, ConstantExpression> computeInstanceFields( |
| 217 List<ConstantExpression> arguments, CallStructure callStructure) { | 217 List<ConstantExpression> arguments, CallStructure callStructure) { |
| 218 ConstantConstructor constantConstructor = | 218 ConstantConstructor constantConstructor = |
| 219 targetConstructorInvocation.target.constantConstructor; | 219 targetConstructorInvocation.target.constantConstructor; |
| 220 return constantConstructor.computeInstanceFields(arguments, callStructure); | 220 return constantConstructor.computeInstanceFields(arguments, callStructure); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 235 } | 235 } |
| 236 | 236 |
| 237 String toString() { | 237 String toString() { |
| 238 StringBuffer sb = new StringBuffer(); | 238 StringBuffer sb = new StringBuffer(); |
| 239 sb.write("{"); | 239 sb.write("{"); |
| 240 sb.write("'constructor': ${targetConstructorInvocation.toDartText()}"); | 240 sb.write("'constructor': ${targetConstructorInvocation.toDartText()}"); |
| 241 sb.write("}"); | 241 sb.write("}"); |
| 242 return sb.toString(); | 242 return sb.toString(); |
| 243 } | 243 } |
| 244 } | 244 } |
| OLD | NEW |