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

Side by Side Diff: pkg/compiler/lib/src/constants/constructors.dart

Issue 2857373002: Handle more constants in Constantifier (Closed)
Patch Set: Updated cf. comments. Created 3 years, 7 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
« no previous file with comments | « pkg/compiler/lib/src/compiler.dart ('k') | pkg/compiler/lib/src/constants/evaluation.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/entities.dart' show FieldEntity; 7 import '../elements/entities.dart' show FieldEntity;
8 import '../elements/types.dart'; 8 import '../elements/types.dart';
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( 26 InterfaceType computeInstanceType(
27 Environment environment, InterfaceType newType); 27 EvaluationEnvironment environment, InterfaceType newType);
28 28
29 /// Computes the constant expressions of the fields of the created instance 29 /// Computes the constant expressions of the fields of the created instance
30 /// in a const constructor invocation with [arguments]. 30 /// in a const constructor invocation with [arguments].
31 Map<FieldEntity, ConstantExpression> computeInstanceFields( 31 Map<FieldEntity, ConstantExpression> computeInstanceFields(
32 Environment environment, 32 EvaluationEnvironment environment,
33 List<ConstantExpression> arguments, 33 List<ConstantExpression> arguments,
34 CallStructure callStructure); 34 CallStructure callStructure);
35 35
36 accept(ConstantConstructorVisitor visitor, arg); 36 accept(ConstantConstructorVisitor visitor, arg);
37 } 37 }
38 38
39 abstract class ConstantConstructorVisitor<R, A> { 39 abstract class ConstantConstructorVisitor<R, A> {
40 const ConstantConstructorVisitor(); 40 const ConstantConstructorVisitor();
41 41
42 R visit(ConstantConstructor constantConstructor, A context) { 42 R visit(ConstantConstructor constantConstructor, A context) {
(...skipping 13 matching lines...) Expand all
56 final Map<dynamic /*int|String*/, ConstantExpression> defaultValues; 56 final Map<dynamic /*int|String*/, ConstantExpression> defaultValues;
57 final Map<FieldEntity, ConstantExpression> fieldMap; 57 final Map<FieldEntity, ConstantExpression> fieldMap;
58 final ConstructedConstantExpression superConstructorInvocation; 58 final ConstructedConstantExpression superConstructorInvocation;
59 59
60 GenerativeConstantConstructor(this.type, this.defaultValues, this.fieldMap, 60 GenerativeConstantConstructor(this.type, this.defaultValues, this.fieldMap,
61 this.superConstructorInvocation); 61 this.superConstructorInvocation);
62 62
63 ConstantConstructorKind get kind => ConstantConstructorKind.GENERATIVE; 63 ConstantConstructorKind get kind => ConstantConstructorKind.GENERATIVE;
64 64
65 InterfaceType computeInstanceType( 65 InterfaceType computeInstanceType(
66 Environment environment, InterfaceType newType) { 66 EvaluationEnvironment environment, InterfaceType newType) {
67 return environment.substByContext(type, newType); 67 return environment.substByContext(type, newType);
68 } 68 }
69 69
70 Map<FieldEntity, ConstantExpression> computeInstanceFields( 70 Map<FieldEntity, ConstantExpression> computeInstanceFields(
71 Environment environment, 71 EvaluationEnvironment environment,
72 List<ConstantExpression> arguments, 72 List<ConstantExpression> arguments,
73 CallStructure callStructure) { 73 CallStructure callStructure) {
74 NormalizedArguments args = 74 NormalizedArguments args =
75 new NormalizedArguments(defaultValues, callStructure, arguments); 75 new NormalizedArguments(defaultValues, callStructure, arguments);
76 Map<FieldEntity, ConstantExpression> appliedFieldMap = 76 Map<FieldEntity, ConstantExpression> appliedFieldMap =
77 applyFields(environment, args, superConstructorInvocation); 77 applyFields(environment, args, superConstructorInvocation);
78 fieldMap.forEach((FieldEntity field, ConstantExpression constant) { 78 fieldMap.forEach((FieldEntity field, ConstantExpression constant) {
79 appliedFieldMap[field] = constant.apply(args); 79 appliedFieldMap[field] = constant.apply(args);
80 }); 80 });
81 return appliedFieldMap; 81 return appliedFieldMap;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 return false; 124 return false;
125 } 125 }
126 } 126 }
127 return true; 127 return true;
128 } 128 }
129 129
130 /// Creates the field-to-constant map from applying [args] to 130 /// Creates the field-to-constant map from applying [args] to
131 /// [constructorInvocation]. If [constructorInvocation] is `null`, an empty 131 /// [constructorInvocation]. If [constructorInvocation] is `null`, an empty
132 /// map is created. 132 /// map is created.
133 static Map<FieldEntity, ConstantExpression> applyFields( 133 static Map<FieldEntity, ConstantExpression> applyFields(
134 Environment environment, 134 EvaluationEnvironment environment,
135 NormalizedArguments args, 135 NormalizedArguments args,
136 ConstructedConstantExpression constructorInvocation) { 136 ConstructedConstantExpression constructorInvocation) {
137 Map<FieldEntity, ConstantExpression> appliedFieldMap = 137 Map<FieldEntity, ConstantExpression> appliedFieldMap =
138 <FieldEntity, ConstantExpression>{}; 138 <FieldEntity, ConstantExpression>{};
139 if (constructorInvocation != null) { 139 if (constructorInvocation != null) {
140 Map<FieldEntity, ConstantExpression> fieldMap = 140 Map<FieldEntity, ConstantExpression> fieldMap =
141 constructorInvocation.computeInstanceFields(environment); 141 constructorInvocation.computeInstanceFields(environment);
142 fieldMap.forEach((FieldEntity field, ConstantExpression constant) { 142 fieldMap.forEach((FieldEntity field, ConstantExpression constant) {
143 appliedFieldMap[field] = constant.apply(args); 143 appliedFieldMap[field] = constant.apply(args);
144 }); 144 });
145 } 145 }
146 return appliedFieldMap; 146 return appliedFieldMap;
147 } 147 }
148 } 148 }
149 149
150 /// A redirecting generative constant constructor. 150 /// A redirecting generative constant constructor.
151 class RedirectingGenerativeConstantConstructor implements ConstantConstructor { 151 class RedirectingGenerativeConstantConstructor implements ConstantConstructor {
152 final Map<dynamic /*int|String*/, ConstantExpression> defaultValues; 152 final Map<dynamic /*int|String*/, ConstantExpression> defaultValues;
153 final ConstructedConstantExpression thisConstructorInvocation; 153 final ConstructedConstantExpression thisConstructorInvocation;
154 154
155 RedirectingGenerativeConstantConstructor( 155 RedirectingGenerativeConstantConstructor(
156 this.defaultValues, this.thisConstructorInvocation); 156 this.defaultValues, this.thisConstructorInvocation);
157 157
158 ConstantConstructorKind get kind { 158 ConstantConstructorKind get kind {
159 return ConstantConstructorKind.REDIRECTING_GENERATIVE; 159 return ConstantConstructorKind.REDIRECTING_GENERATIVE;
160 } 160 }
161 161
162 InterfaceType computeInstanceType( 162 InterfaceType computeInstanceType(
163 Environment environment, InterfaceType newType) { 163 EvaluationEnvironment environment, InterfaceType newType) {
164 return environment.substByContext( 164 return environment.substByContext(
165 thisConstructorInvocation.computeInstanceType(environment), newType); 165 thisConstructorInvocation.computeInstanceType(environment), newType);
166 } 166 }
167 167
168 Map<FieldEntity, ConstantExpression> computeInstanceFields( 168 Map<FieldEntity, ConstantExpression> computeInstanceFields(
169 Environment environment, 169 EvaluationEnvironment environment,
170 List<ConstantExpression> arguments, 170 List<ConstantExpression> arguments,
171 CallStructure callStructure) { 171 CallStructure callStructure) {
172 NormalizedArguments args = 172 NormalizedArguments args =
173 new NormalizedArguments(defaultValues, callStructure, arguments); 173 new NormalizedArguments(defaultValues, callStructure, arguments);
174 Map<FieldEntity, ConstantExpression> appliedFieldMap = 174 Map<FieldEntity, ConstantExpression> appliedFieldMap =
175 GenerativeConstantConstructor.applyFields( 175 GenerativeConstantConstructor.applyFields(
176 environment, args, thisConstructorInvocation); 176 environment, args, thisConstructorInvocation);
177 return appliedFieldMap; 177 return appliedFieldMap;
178 } 178 }
179 179
(...skipping 30 matching lines...) Expand all
210 class RedirectingFactoryConstantConstructor implements ConstantConstructor { 210 class RedirectingFactoryConstantConstructor implements ConstantConstructor {
211 final ConstructedConstantExpression targetConstructorInvocation; 211 final ConstructedConstantExpression targetConstructorInvocation;
212 212
213 RedirectingFactoryConstantConstructor(this.targetConstructorInvocation); 213 RedirectingFactoryConstantConstructor(this.targetConstructorInvocation);
214 214
215 ConstantConstructorKind get kind { 215 ConstantConstructorKind get kind {
216 return ConstantConstructorKind.REDIRECTING_FACTORY; 216 return ConstantConstructorKind.REDIRECTING_FACTORY;
217 } 217 }
218 218
219 InterfaceType computeInstanceType( 219 InterfaceType computeInstanceType(
220 Environment environment, InterfaceType newType) { 220 EvaluationEnvironment environment, InterfaceType newType) {
221 return environment.substByContext( 221 return environment.substByContext(
222 targetConstructorInvocation.computeInstanceType(environment), newType); 222 targetConstructorInvocation.computeInstanceType(environment), newType);
223 } 223 }
224 224
225 Map<FieldEntity, ConstantExpression> computeInstanceFields( 225 Map<FieldEntity, ConstantExpression> computeInstanceFields(
226 Environment environment, 226 EvaluationEnvironment environment,
227 List<ConstantExpression> arguments, 227 List<ConstantExpression> arguments,
228 CallStructure callStructure) { 228 CallStructure callStructure) {
229 ConstantConstructor constantConstructor = 229 ConstantConstructor constantConstructor =
230 environment.getConstructorConstant(targetConstructorInvocation.target); 230 environment.getConstructorConstant(targetConstructorInvocation.target);
231 return constantConstructor.computeInstanceFields( 231 return constantConstructor.computeInstanceFields(
232 environment, arguments, callStructure); 232 environment, arguments, callStructure);
233 } 233 }
234 234
235 accept(ConstantConstructorVisitor visitor, arg) { 235 accept(ConstantConstructorVisitor visitor, arg) {
236 return visitor.visitRedirectingFactory(this, arg); 236 return visitor.visitRedirectingFactory(this, arg);
(...skipping 10 matching lines...) Expand all
247 } 247 }
248 248
249 String toString() { 249 String toString() {
250 StringBuffer sb = new StringBuffer(); 250 StringBuffer sb = new StringBuffer();
251 sb.write("{"); 251 sb.write("{");
252 sb.write("'constructor': ${targetConstructorInvocation.toDartText()}"); 252 sb.write("'constructor': ${targetConstructorInvocation.toDartText()}");
253 sb.write("}"); 253 sb.write("}");
254 return sb.toString(); 254 return sb.toString();
255 } 255 }
256 } 256 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/compiler.dart ('k') | pkg/compiler/lib/src/constants/evaluation.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698