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

Side by Side Diff: pkg/compiler/lib/src/kernel/element_map_impl.dart

Issue 2964683003: Split implementation of KernelToElementMapImpl (Closed)
Patch Set: Updated cf. comments Created 3 years, 5 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
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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.kernel.element_map; 5 library dart2js.kernel.element_map;
6 6
7 import 'package:kernel/ast.dart' as ir; 7 import 'package:kernel/ast.dart' as ir;
8 import 'package:kernel/clone.dart';
9 import 'package:kernel/type_algebra.dart';
10 8
11 import '../common.dart'; 9 import '../common.dart';
12 import '../common/names.dart' show Identifiers; 10 import '../common/names.dart' show Identifiers;
13 import '../common/resolution.dart'; 11 import '../common/resolution.dart';
14 import '../compile_time_constants.dart'; 12 import '../compile_time_constants.dart';
15 import '../constants/constant_system.dart'; 13 import '../constants/constant_system.dart';
16 import '../constants/constructors.dart'; 14 import '../constants/constructors.dart';
17 import '../constants/evaluation.dart'; 15 import '../constants/evaluation.dart';
18 import '../constants/expressions.dart'; 16 import '../constants/expressions.dart';
19 import '../constants/values.dart'; 17 import '../constants/values.dart';
20 import '../common_elements.dart'; 18 import '../common_elements.dart';
21 import '../elements/elements.dart'; 19 import '../elements/elements.dart';
22 import '../elements/entities.dart'; 20 import '../elements/entities.dart';
23 import '../elements/names.dart'; 21 import '../elements/names.dart';
24 import '../elements/types.dart'; 22 import '../elements/types.dart';
25 import '../environment.dart'; 23 import '../environment.dart';
26 import '../frontend_strategy.dart'; 24 import '../frontend_strategy.dart';
27 import '../js_backend/backend_usage.dart'; 25 import '../js_backend/backend_usage.dart';
28 import '../js_backend/constant_system_javascript.dart'; 26 import '../js_backend/constant_system_javascript.dart';
29 import '../js_backend/interceptor_data.dart'; 27 import '../js_backend/interceptor_data.dart';
30 import '../js_backend/native_data.dart'; 28 import '../js_backend/native_data.dart';
31 import '../js_backend/no_such_method_registry.dart'; 29 import '../js_backend/no_such_method_registry.dart';
32 import '../js_model/elements.dart'; 30 import '../js_model/elements.dart';
33 import '../native/native.dart' as native; 31 import '../native/native.dart' as native;
34 import '../native/resolver.dart'; 32 import '../native/resolver.dart';
35 import '../ordered_typeset.dart'; 33 import '../ordered_typeset.dart';
36 import '../ssa/kernel_impact.dart';
37 import '../universe/class_set.dart'; 34 import '../universe/class_set.dart';
38 import '../universe/function_set.dart'; 35 import '../universe/function_set.dart';
39 import '../universe/selector.dart'; 36 import '../universe/selector.dart';
40 import '../universe/world_builder.dart'; 37 import '../universe/world_builder.dart';
41 import '../world.dart'; 38 import '../world.dart';
42 import '../util/util.dart' show Link, LinkBuilder; 39 import '../util/util.dart' show Link, LinkBuilder;
43 import 'element_map.dart'; 40 import 'element_map.dart';
44 import 'element_map_mixins.dart'; 41 import 'element_map_mixins.dart';
45 import 'elements.dart'; 42 import 'elements.dart';
43 import 'env.dart';
44 import 'kelements.dart';
46 45
47 part 'native_basic_data.dart'; 46 part 'native_basic_data.dart';
48 part 'no_such_method_resolver.dart'; 47 part 'no_such_method_resolver.dart';
49 part 'types.dart'; 48 part 'types.dart';
50 49
51 /// Interface for kernel queries needed to implement the [CodegenWorldBuilder]. 50 /// Interface for kernel queries needed to implement the [CodegenWorldBuilder].
52 abstract class KernelToWorldBuilder implements KernelToElementMapForBuilding { 51 abstract class KernelToWorldBuilder implements KernelToElementMapForBuilding {
53 /// Returns `true` if [field] has a constant initializer. 52 /// Returns `true` if [field] has a constant initializer.
54 bool hasConstantFieldInitializer(FieldEntity field); 53 bool hasConstantFieldInitializer(FieldEntity field);
55 54
56 /// Returns the constant initializer for [field]. 55 /// Returns the constant initializer for [field].
57 ConstantValue getConstantFieldInitializer(FieldEntity field); 56 ConstantValue getConstantFieldInitializer(FieldEntity field);
58 57
59 /// Calls [f] for each parameter of [function] providing the type and name of 58 /// Calls [f] for each parameter of [function] providing the type and name of
60 /// the parameter and the [defaultValue] if the parameter is optional. 59 /// the parameter and the [defaultValue] if the parameter is optional.
61 void forEachParameter(FunctionEntity function, 60 void forEachParameter(FunctionEntity function,
62 void f(DartType type, String name, ConstantValue defaultValue)); 61 void f(DartType type, String name, ConstantValue defaultValue));
63 } 62 }
64 63
65 class KernelToElementMapBase extends KernelToElementMapBaseMixin { 64 abstract class KernelToElementMapBase extends KernelToElementMapBaseMixin {
66 final DiagnosticReporter reporter; 65 final DiagnosticReporter reporter;
67 CommonElements _commonElements; 66 CommonElements _commonElements;
68 ElementEnvironment _elementEnvironment; 67 ElementEnvironment _elementEnvironment;
69 DartTypeConverter _typeConverter; 68 DartTypeConverter _typeConverter;
70 KernelConstantEnvironment _constantEnvironment; 69 KernelConstantEnvironment _constantEnvironment;
70 _KernelDartTypes _types;
71 71
72 /// Library environment. Used for fast lookup. 72 /// Library environment. Used for fast lookup.
73 _KEnv _env = new _KEnv(); 73 ProgramEnv _env = new ProgramEnv();
74 74
75 /// List of library environments by `KLibrary.libraryIndex`. This is used for 75 /// List of library environments by `IndexedLibrary.libraryIndex`. This is
76 /// fast lookup into library classes and members. 76 /// used for fast lookup into library classes and members.
77 List<_KLibraryEnv> _libraryEnvs = <_KLibraryEnv>[]; 77 List<LibraryEnv> _libraryEnvs = <LibraryEnv>[];
78 78
79 /// List of class environments by `KClass.classIndex`. This is used for 79 /// List of class environments by `IndexedClass.classIndex`. This is used for
80 /// fast lookup into class members. 80 /// fast lookup into class members.
81 List<_KClassEnv> _classEnvs = <_KClassEnv>[]; 81 List<ClassEnv> _classEnvs = <ClassEnv>[];
82 82
83 Map<ir.Library, KLibrary> _libraryMap = <ir.Library, KLibrary>{}; 83 /// List of member data by `IndexedMember.classIndex`. This is used for
84 Map<ir.Class, KClass> _classMap = <ir.Class, KClass>{}; 84 /// fast lookup into member properties.
85 Map<ir.TypeParameter, KTypeVariable> _typeVariableMap = 85 List<MemberData> _memberList = <MemberData>[];
86 <ir.TypeParameter, KTypeVariable>{};
87
88 List<_MemberData> _memberList = <_MemberData>[];
89
90 Map<ir.Member, KConstructor> _constructorMap = <ir.Member, KConstructor>{};
91 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{};
92 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{};
93
94 Map<ir.TreeNode, KLocalFunction> _localFunctionMap =
95 <ir.TreeNode, KLocalFunction>{};
96 86
97 KernelToElementMapBase(this.reporter, Environment environment) { 87 KernelToElementMapBase(this.reporter, Environment environment) {
98 _elementEnvironment = new KernelElementEnvironment(this); 88 _elementEnvironment = new KernelElementEnvironment(this);
99 _commonElements = new CommonElements(_elementEnvironment); 89 _commonElements = new CommonElements(_elementEnvironment);
100 _constantEnvironment = new KernelConstantEnvironment(this, environment); 90 _constantEnvironment = new KernelConstantEnvironment(this, environment);
101 _typeConverter = new DartTypeConverter(this); 91 _typeConverter = new DartTypeConverter(this);
92 _types = new _KernelDartTypes(this);
102 } 93 }
103 94
95 DartTypes get types => _types;
96
104 @override 97 @override
105 ElementEnvironment get elementEnvironment => _elementEnvironment; 98 ElementEnvironment get elementEnvironment => _elementEnvironment;
106 99
107 @override 100 @override
108 CommonElements get commonElements => _commonElements; 101 CommonElements get commonElements => _commonElements;
109 102
110 KMethod get _mainFunction { 103 FunctionEntity get _mainFunction {
111 return _env.mainMethod != null ? _getMethod(_env.mainMethod) : null; 104 return _env.mainMethod != null ? _getMethod(_env.mainMethod) : null;
112 } 105 }
113 106
114 KLibrary get _mainLibrary { 107 LibraryEntity get _mainLibrary {
115 return _env.mainMethod != null 108 return _env.mainMethod != null
116 ? _getLibrary(_env.mainMethod.enclosingLibrary) 109 ? _getLibrary(_env.mainMethod.enclosingLibrary)
117 : null; 110 : null;
118 } 111 }
119 112
120 Iterable<LibraryEntity> get _libraries { 113 Iterable<LibraryEntity> get _libraries;
121 if (_env.length != _libraryMap.length) {
122 // Create a [KLibrary] for each library.
123 _env.forEachLibrary((_KLibraryEnv env) {
124 _getLibrary(env.library, env);
125 });
126 }
127 return _libraryMap.values;
128 }
129 114
130 LibraryEntity lookupLibrary(Uri uri) { 115 LibraryEntity lookupLibrary(Uri uri) {
131 _KLibraryEnv libraryEnv = _env.lookupLibrary(uri); 116 LibraryEnv libraryEnv = _env.lookupLibrary(uri);
132 if (libraryEnv == null) return null; 117 if (libraryEnv == null) return null;
133 return _getLibrary(libraryEnv.library, libraryEnv); 118 return _getLibrary(libraryEnv.library, libraryEnv);
134 } 119 }
135 120
136 String _getLibraryName(KLibrary library) { 121 String _getLibraryName(KLibrary library) {
137 _KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex]; 122 LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
138 return libraryEnv.library.name ?? ''; 123 return libraryEnv.library.name ?? '';
139 } 124 }
140 125
141 MemberEntity lookupLibraryMember(KLibrary library, String name, 126 MemberEntity lookupLibraryMember(KLibrary library, String name,
142 {bool setter: false}) { 127 {bool setter: false}) {
143 _KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex]; 128 LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
144 ir.Member member = libraryEnv.lookupMember(name, setter: setter); 129 ir.Member member = libraryEnv.lookupMember(name, setter: setter);
145 return member != null ? getMember(member) : null; 130 return member != null ? getMember(member) : null;
146 } 131 }
147 132
148 void _forEachLibraryMember(KLibrary library, void f(MemberEntity member)) { 133 void _forEachLibraryMember(KLibrary library, void f(MemberEntity member)) {
149 _KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex]; 134 LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
150 libraryEnv.forEachMember((ir.Member node) { 135 libraryEnv.forEachMember((ir.Member node) {
151 f(getMember(node)); 136 f(getMember(node));
152 }); 137 });
153 } 138 }
154 139
155 ClassEntity lookupClass(KLibrary library, String name) { 140 ClassEntity lookupClass(KLibrary library, String name) {
156 _KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex]; 141 LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
157 _KClassEnv classEnv = libraryEnv.lookupClass(name); 142 ClassEnv classEnv = libraryEnv.lookupClass(name);
158 if (classEnv != null) { 143 if (classEnv != null) {
159 return _getClass(classEnv.cls, classEnv); 144 return _getClass(classEnv.cls, classEnv);
160 } 145 }
161 return null; 146 return null;
162 } 147 }
163 148
164 void _forEachClass(KLibrary library, void f(ClassEntity cls)) { 149 void _forEachClass(KLibrary library, void f(ClassEntity cls)) {
165 _KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex]; 150 LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
166 libraryEnv.forEachClass((_KClassEnv classEnv) { 151 libraryEnv.forEachClass((ClassEnv classEnv) {
167 if (!classEnv.isUnnamedMixinApplication) { 152 if (!classEnv.isUnnamedMixinApplication) {
168 f(_getClass(classEnv.cls, classEnv)); 153 f(_getClass(classEnv.cls, classEnv));
169 } 154 }
170 }); 155 });
171 } 156 }
172 157
173 MemberEntity lookupClassMember(KClass cls, String name, 158 MemberEntity lookupClassMember(IndexedClass cls, String name,
174 {bool setter: false}) { 159 {bool setter: false}) {
175 _KClassEnv classEnv = _classEnvs[cls.classIndex]; 160 ClassEnv classEnv = _classEnvs[cls.classIndex];
176 ir.Member member = classEnv.lookupMember(name, setter: setter); 161 ir.Member member = classEnv.lookupMember(name, setter: setter);
177 return member != null ? getMember(member) : null; 162 return member != null ? getMember(member) : null;
178 } 163 }
179 164
180 ConstructorEntity lookupConstructor(KClass cls, String name) { 165 ConstructorEntity lookupConstructor(IndexedClass cls, String name) {
181 _KClassEnv classEnv = _classEnvs[cls.classIndex]; 166 ClassEnv classEnv = _classEnvs[cls.classIndex];
182 ir.Member member = classEnv.lookupConstructor(name); 167 ir.Member member = classEnv.lookupConstructor(name);
183 return member != null ? getConstructor(member) : null; 168 return member != null ? getConstructor(member) : null;
184 } 169 }
185 170
186 @override 171 @override
187 InterfaceType createInterfaceType( 172 InterfaceType createInterfaceType(
188 ir.Class cls, List<ir.DartType> typeArguments) { 173 ir.Class cls, List<ir.DartType> typeArguments) {
189 return new InterfaceType(getClass(cls), getDartTypes(typeArguments)); 174 return new InterfaceType(getClass(cls), getDartTypes(typeArguments));
190 } 175 }
191 176
192 LibraryEntity getLibrary(ir.Library node) => _getLibrary(node); 177 LibraryEntity getLibrary(ir.Library node) => _getLibrary(node);
193 178
194 KLibrary _getLibrary(ir.Library node, [_KLibraryEnv libraryEnv]) { 179 LibraryEntity _getLibrary(ir.Library node, [LibraryEnv libraryEnv]);
195 return _libraryMap.putIfAbsent(node, () {
196 Uri canonicalUri = node.importUri;
197 _libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(canonicalUri));
198 String name = node.name;
199 if (name == null) {
200 // Use the file name as script name.
201 String path = canonicalUri.path;
202 name = path.substring(path.lastIndexOf('/') + 1);
203 }
204 return new KLibrary(_libraryMap.length, name, canonicalUri);
205 });
206 }
207 180
208 @override 181 @override
209 ClassEntity getClass(ir.Class node) => _getClass(node); 182 ClassEntity getClass(ir.Class node) => _getClass(node);
210 183
211 KClass _getClass(ir.Class node, [_KClassEnv classEnv]) { 184 ClassEntity _getClass(ir.Class node, [ClassEnv classEnv]);
212 return _classMap.putIfAbsent(node, () {
213 KLibrary library = _getLibrary(node.enclosingLibrary);
214 if (classEnv == null) {
215 classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name);
216 }
217 _classEnvs.add(classEnv);
218 return new KClass(library, _classMap.length, node.name,
219 isAbstract: node.isAbstract);
220 });
221 }
222 185
223 InterfaceType _getSuperType(KClass cls) { 186 InterfaceType _getSuperType(IndexedClass cls) {
224 _KClassEnv env = _classEnvs[cls.classIndex]; 187 ClassEnv env = _classEnvs[cls.classIndex];
225 _ensureSupertypes(cls, env); 188 _ensureSupertypes(cls, env);
226 return env.supertype; 189 return env.supertype;
227 } 190 }
228 191
229 void _ensureThisAndRawType(KClass cls, _KClassEnv env) { 192 void _ensureThisAndRawType(ClassEntity cls, ClassEnv env) {
230 if (env.thisType == null) { 193 if (env.thisType == null) {
231 ir.Class node = env.cls; 194 ir.Class node = env.cls;
232 // TODO(johnniwinther): Add the type argument to the list literal when we 195 // TODO(johnniwinther): Add the type argument to the list literal when we
233 // no longer use resolution types. 196 // no longer use resolution types.
234 if (node.typeParameters.isEmpty) { 197 if (node.typeParameters.isEmpty) {
235 env.thisType = 198 env.thisType =
236 env.rawType = new InterfaceType(cls, const/*<DartType>*/ []); 199 env.rawType = new InterfaceType(cls, const/*<DartType>*/ []);
237 } else { 200 } else {
238 env.thisType = new InterfaceType( 201 env.thisType = new InterfaceType(
239 cls, 202 cls,
240 new List/*<DartType>*/ .generate(node.typeParameters.length, 203 new List/*<DartType>*/ .generate(node.typeParameters.length,
241 (int index) { 204 (int index) {
242 return new TypeVariableType( 205 return new TypeVariableType(
243 _getTypeVariable(node.typeParameters[index])); 206 _getTypeVariable(node.typeParameters[index]));
244 })); 207 }));
245 env.rawType = new InterfaceType( 208 env.rawType = new InterfaceType(
246 cls, 209 cls,
247 new List/*<DartType>*/ .filled( 210 new List/*<DartType>*/ .filled(
248 node.typeParameters.length, const DynamicType())); 211 node.typeParameters.length, const DynamicType()));
249 } 212 }
250 } 213 }
251 } 214 }
252 215
253 TypeVariableEntity getTypeVariable(ir.TypeParameter node) => 216 TypeVariableEntity getTypeVariable(ir.TypeParameter node) =>
254 _getTypeVariable(node); 217 _getTypeVariable(node);
255 218
256 KTypeVariable _getTypeVariable(ir.TypeParameter node) { 219 TypeVariableEntity _getTypeVariable(ir.TypeParameter node);
257 return _typeVariableMap.putIfAbsent(node, () {
258 if (node.parent is ir.Class) {
259 ir.Class cls = node.parent;
260 int index = cls.typeParameters.indexOf(node);
261 return new KTypeVariable(_getClass(cls), node.name, index);
262 }
263 if (node.parent is ir.FunctionNode) {
264 ir.FunctionNode func = node.parent;
265 int index = func.typeParameters.indexOf(node);
266 if (func.parent is ir.Constructor) {
267 ir.Constructor constructor = func.parent;
268 ir.Class cls = constructor.enclosingClass;
269 return _getTypeVariable(cls.typeParameters[index]);
270 }
271 if (func.parent is ir.Procedure) {
272 ir.Procedure procedure = func.parent;
273 if (procedure.kind == ir.ProcedureKind.Factory) {
274 ir.Class cls = procedure.enclosingClass;
275 return _getTypeVariable(cls.typeParameters[index]);
276 } else {
277 return new KTypeVariable(_getMethod(procedure), node.name, index);
278 }
279 }
280 }
281 throw new UnsupportedError('Unsupported type parameter type node $node.');
282 });
283 }
284 220
285 void _ensureSupertypes(KClass cls, _KClassEnv env) { 221 void _ensureSupertypes(ClassEntity cls, ClassEnv env) {
286 if (env.orderedTypeSet == null) { 222 if (env.orderedTypeSet == null) {
287 _ensureThisAndRawType(cls, env); 223 _ensureThisAndRawType(cls, env);
288 224
289 ir.Class node = env.cls; 225 ir.Class node = env.cls;
290 226
291 if (node.supertype == null) { 227 if (node.supertype == null) {
292 env.orderedTypeSet = new OrderedTypeSet.singleton(env.thisType); 228 env.orderedTypeSet = new OrderedTypeSet.singleton(env.thisType);
293 env.isMixinApplication = false; 229 env.isMixinApplication = false;
294 env.interfaces = const <InterfaceType>[]; 230 env.interfaces = const <InterfaceType>[];
295 } else { 231 } else {
296 InterfaceType processSupertype(ir.Supertype node) { 232 InterfaceType processSupertype(ir.Supertype node) {
297 InterfaceType type = _typeConverter.visitSupertype(node); 233 InterfaceType type = _typeConverter.visitSupertype(node);
298 KClass superclass = type.element; 234 IndexedClass superclass = type.element;
299 _KClassEnv env = _classEnvs[superclass.classIndex]; 235 ClassEnv env = _classEnvs[superclass.classIndex];
300 _ensureSupertypes(superclass, env); 236 _ensureSupertypes(superclass, env);
301 return type; 237 return type;
302 } 238 }
303 239
304 env.supertype = processSupertype(node.supertype); 240 env.supertype = processSupertype(node.supertype);
305 LinkBuilder<InterfaceType> linkBuilder = 241 LinkBuilder<InterfaceType> linkBuilder =
306 new LinkBuilder<InterfaceType>(); 242 new LinkBuilder<InterfaceType>();
307 if (node.mixedInType != null) { 243 if (node.mixedInType != null) {
308 env.isMixinApplication = true; 244 env.isMixinApplication = true;
309 linkBuilder 245 linkBuilder
(...skipping 28 matching lines...) Expand all
338 } 274 }
339 } 275 }
340 throw new UnsupportedError("Unexpected member: $node"); 276 throw new UnsupportedError("Unexpected member: $node");
341 } 277 }
342 278
343 MemberEntity getSuperMember(ir.Member context, ir.Name name, ir.Member target, 279 MemberEntity getSuperMember(ir.Member context, ir.Name name, ir.Member target,
344 {bool setter: false}) { 280 {bool setter: false}) {
345 if (target != null) { 281 if (target != null) {
346 return getMember(target); 282 return getMember(target);
347 } 283 }
348 KClass cls = getMember(context).enclosingClass; 284 ClassEntity cls = getMember(context).enclosingClass;
349 KClass superclass = _getSuperType(cls)?.element; 285 IndexedClass superclass = _getSuperType(cls)?.element;
350 while (superclass != null) { 286 while (superclass != null) {
351 _KClassEnv env = _classEnvs[superclass.classIndex]; 287 ClassEnv env = _classEnvs[superclass.classIndex];
352 ir.Member superMember = env.lookupMember(name.name, setter: setter); 288 ir.Member superMember = env.lookupMember(name.name, setter: setter);
353 if (superMember != null) { 289 if (superMember != null) {
354 return getMember(superMember); 290 return getMember(superMember);
355 } 291 }
356 superclass = _getSuperType(superclass)?.element; 292 superclass = _getSuperType(superclass)?.element;
357 } 293 }
358 throw new SpannableAssertionFailure( 294 throw new SpannableAssertionFailure(
359 cls, "No super method member found for ${name} in $cls."); 295 cls, "No super method member found for ${name} in $cls.");
360 } 296 }
361 297
362 @override 298 @override
363 ConstructorEntity getConstructor(ir.Member node) => _getConstructor(node); 299 ConstructorEntity getConstructor(ir.Member node) => _getConstructor(node);
364 300
365 KConstructor _getConstructor(ir.Member node) { 301 ConstructorEntity _getConstructor(ir.Member node);
366 return _constructorMap.putIfAbsent(node, () {
367 int memberIndex = _memberList.length;
368 KConstructor constructor;
369 KClass enclosingClass = _getClass(node.enclosingClass);
370 Name name = getName(node.name);
371 bool isExternal = node.isExternal;
372
373 ir.FunctionNode functionNode;
374 if (node is ir.Constructor) {
375 functionNode = node.function;
376 constructor = new KGenerativeConstructor(memberIndex, enclosingClass,
377 name, _getParameterStructure(functionNode),
378 isExternal: isExternal, isConst: node.isConst);
379 } else if (node is ir.Procedure) {
380 functionNode = node.function;
381 bool isFromEnvironment = isExternal &&
382 name.text == 'fromEnvironment' &&
383 const ['int', 'bool', 'String'].contains(enclosingClass.name);
384 constructor = new KFactoryConstructor(memberIndex, enclosingClass, name,
385 _getParameterStructure(functionNode),
386 isExternal: isExternal,
387 isConst: node.isConst,
388 isFromEnvironmentConstructor: isFromEnvironment);
389 } else {
390 // TODO(johnniwinther): Convert `node.location` to a [SourceSpan].
391 throw new SpannableAssertionFailure(
392 NO_LOCATION_SPANNABLE, "Unexpected constructor node: ${node}.");
393 }
394 _memberList.add(new _ConstructorData(node, functionNode));
395 return constructor;
396 });
397 }
398 302
399 ConstructorEntity getSuperConstructor( 303 ConstructorEntity getSuperConstructor(
400 ir.Constructor sourceNode, ir.Member targetNode) { 304 ir.Constructor sourceNode, ir.Member targetNode) {
401 KConstructor source = getConstructor(sourceNode); 305 ConstructorEntity source = getConstructor(sourceNode);
402 KClass sourceClass = source.enclosingClass; 306 ClassEntity sourceClass = source.enclosingClass;
403 KConstructor target = getConstructor(targetNode); 307 ConstructorEntity target = getConstructor(targetNode);
404 KClass targetClass = target.enclosingClass; 308 ClassEntity targetClass = target.enclosingClass;
405 KClass superClass = _getSuperType(sourceClass)?.element; 309 IndexedClass superClass = _getSuperType(sourceClass)?.element;
406 if (superClass == targetClass) { 310 if (superClass == targetClass) {
407 return target; 311 return target;
408 } 312 }
409 _KClassEnv env = _classEnvs[superClass.classIndex]; 313 ClassEnv env = _classEnvs[superClass.classIndex];
410 ir.Member member = env.lookupConstructor(target.name); 314 ir.Member member = env.lookupConstructor(target.name);
411 if (member != null) { 315 if (member != null) {
412 return getConstructor(member); 316 return getConstructor(member);
413 } 317 }
414 throw new SpannableAssertionFailure( 318 throw new SpannableAssertionFailure(
415 source, "Super constructor for $source not found."); 319 source, "Super constructor for $source not found.");
416 } 320 }
417 321
418 @override 322 @override
419 FunctionEntity getMethod(ir.Procedure node) => _getMethod(node); 323 FunctionEntity getMethod(ir.Procedure node) => _getMethod(node);
420 324
421 KFunction _getMethod(ir.Procedure node) { 325 FunctionEntity _getMethod(ir.Procedure node);
422 return _methodMap.putIfAbsent(node, () {
423 int memberIndex = _memberList.length;
424 KLibrary library;
425 KClass enclosingClass;
426 if (node.enclosingClass != null) {
427 enclosingClass = _getClass(node.enclosingClass);
428 library = enclosingClass.library;
429 } else {
430 library = _getLibrary(node.enclosingLibrary);
431 }
432 Name name = getName(node.name);
433 bool isStatic = node.isStatic;
434 bool isExternal = node.isExternal;
435 bool isAbstract = node.isAbstract;
436 KFunction function;
437 AsyncMarker asyncMarker;
438 switch (node.function.asyncMarker) {
439 case ir.AsyncMarker.Async:
440 asyncMarker = AsyncMarker.ASYNC;
441 break;
442 case ir.AsyncMarker.AsyncStar:
443 asyncMarker = AsyncMarker.ASYNC_STAR;
444 break;
445 case ir.AsyncMarker.Sync:
446 asyncMarker = AsyncMarker.SYNC;
447 break;
448 case ir.AsyncMarker.SyncStar:
449 asyncMarker = AsyncMarker.SYNC_STAR;
450 break;
451 case ir.AsyncMarker.SyncYielding:
452 throw new UnsupportedError(
453 "Async marker ${node.function.asyncMarker} is not supported.");
454 }
455 switch (node.kind) {
456 case ir.ProcedureKind.Factory:
457 throw new UnsupportedError("Cannot create method from factory.");
458 case ir.ProcedureKind.Getter:
459 function = new KGetter(
460 memberIndex, library, enclosingClass, name, asyncMarker,
461 isStatic: isStatic,
462 isExternal: isExternal,
463 isAbstract: isAbstract);
464 break;
465 case ir.ProcedureKind.Method:
466 case ir.ProcedureKind.Operator:
467 function = new KMethod(memberIndex, library, enclosingClass, name,
468 _getParameterStructure(node.function), asyncMarker,
469 isStatic: isStatic,
470 isExternal: isExternal,
471 isAbstract: isAbstract);
472 break;
473 case ir.ProcedureKind.Setter:
474 assert(asyncMarker == AsyncMarker.SYNC);
475 function = new KSetter(
476 memberIndex, library, enclosingClass, getName(node.name).setter,
477 isStatic: isStatic,
478 isExternal: isExternal,
479 isAbstract: isAbstract);
480 break;
481 }
482 _memberList.add(new _FunctionData(node, node.function));
483 return function;
484 });
485 }
486 326
487 @override 327 @override
488 FieldEntity getField(ir.Field node) => _getField(node); 328 FieldEntity getField(ir.Field node) => _getField(node);
489 329
490 KField _getField(ir.Field node) { 330 FieldEntity _getField(ir.Field node);
491 return _fieldMap.putIfAbsent(node, () {
492 int memberIndex = _memberList.length;
493 KLibrary library;
494 KClass enclosingClass;
495 if (node.enclosingClass != null) {
496 enclosingClass = _getClass(node.enclosingClass);
497 library = enclosingClass.library;
498 } else {
499 library = _getLibrary(node.enclosingLibrary);
500 }
501 Name name = getName(node.name);
502 bool isStatic = node.isStatic;
503 _memberList.add(new _FieldData(node));
504 return new KField(memberIndex, library, enclosingClass, name,
505 isStatic: isStatic,
506 isAssignable: node.isMutable,
507 isConst: node.isConst);
508 });
509 }
510
511 ParameterStructure _getParameterStructure(ir.FunctionNode node) {
512 // TODO(johnniwinther): Cache the computed function type.
513 int requiredParameters = node.requiredParameterCount;
514 int positionalParameters = node.positionalParameters.length;
515 List<String> namedParameters =
516 node.namedParameters.map((p) => p.name).toList()..sort();
517 return new ParameterStructure(
518 requiredParameters, positionalParameters, namedParameters);
519 }
520 331
521 @override 332 @override
522 Local getLocalFunction(ir.TreeNode node) => _getLocal(node); 333 Local getLocalFunction(ir.TreeNode node) => _getLocalFunction(node);
523 334
524 KLocalFunction _getLocal(ir.TreeNode node) { 335 Local _getLocalFunction(ir.TreeNode node);
525 return _localFunctionMap.putIfAbsent(node, () {
526 MemberEntity memberContext;
527 Entity executableContext;
528 ir.TreeNode parent = node.parent;
529 while (parent != null) {
530 if (parent is ir.Member) {
531 executableContext = memberContext = getMember(parent);
532 break;
533 }
534 if (parent is ir.FunctionDeclaration ||
535 parent is ir.FunctionExpression) {
536 KLocalFunction localFunction = _getLocal(parent);
537 executableContext = localFunction;
538 memberContext = localFunction.memberContext;
539 break;
540 }
541 parent = parent.parent;
542 }
543 String name;
544 FunctionType functionType;
545 if (node is ir.FunctionDeclaration) {
546 name = node.variable.name;
547 functionType = getFunctionType(node.function);
548 } else if (node is ir.FunctionExpression) {
549 functionType = getFunctionType(node.function);
550 }
551 return new KLocalFunction(
552 name, memberContext, executableContext, functionType);
553 });
554 }
555 336
556 @override 337 @override
557 DartType getDartType(ir.DartType type) => _typeConverter.convert(type); 338 DartType getDartType(ir.DartType type) => _typeConverter.convert(type);
558 339
559 List<DartType> getDartTypes(List<ir.DartType> types) { 340 List<DartType> getDartTypes(List<ir.DartType> types) {
560 // TODO(johnniwinther): Add the type argument to the list literal when we 341 // TODO(johnniwinther): Add the type argument to the list literal when we
561 // no longer use resolution types. 342 // no longer use resolution types.
562 List<DartType> list = /*<DartType>*/ []; 343 List<DartType> list = /*<DartType>*/ [];
563 types.forEach((ir.DartType type) { 344 types.forEach((ir.DartType type) {
564 list.add(getDartType(type)); 345 list.add(getDartType(type));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 ConstantValue computeConstantValue(ConstantExpression constant, 379 ConstantValue computeConstantValue(ConstantExpression constant,
599 {bool requireConstant: true}) { 380 {bool requireConstant: true}) {
600 return _constantEnvironment.getConstantValue(constant); 381 return _constantEnvironment.getConstantValue(constant);
601 } 382 }
602 383
603 DartType _substByContext(DartType type, InterfaceType context) { 384 DartType _substByContext(DartType type, InterfaceType context) {
604 return type.subst( 385 return type.subst(
605 context.typeArguments, _getThisType(context.element).typeArguments); 386 context.typeArguments, _getThisType(context.element).typeArguments);
606 } 387 }
607 388
608 InterfaceType _getThisType(KClass cls) { 389 InterfaceType _getThisType(IndexedClass cls) {
609 _KClassEnv env = _classEnvs[cls.classIndex]; 390 ClassEnv env = _classEnvs[cls.classIndex];
610 _ensureThisAndRawType(cls, env); 391 _ensureThisAndRawType(cls, env);
611 return env.thisType; 392 return env.thisType;
612 } 393 }
613 394
614 InterfaceType _getRawType(KClass cls) { 395 InterfaceType _getRawType(IndexedClass cls) {
615 _KClassEnv env = _classEnvs[cls.classIndex]; 396 ClassEnv env = _classEnvs[cls.classIndex];
616 _ensureThisAndRawType(cls, env); 397 _ensureThisAndRawType(cls, env);
617 return env.rawType; 398 return env.rawType;
618 } 399 }
619 400
620 FunctionType _getFunctionType(KFunction function) { 401 FunctionType _getFunctionType(IndexedMember function) {
621 _FunctionData data = _memberList[function.memberIndex]; 402 FunctionData data = _memberList[function.memberIndex];
622 return data.getFunctionType(this); 403 return data.getFunctionType(this);
623 } 404 }
624 405
625 ClassEntity _getAppliedMixin(KClass cls) { 406 ClassEntity _getAppliedMixin(IndexedClass cls) {
626 _KClassEnv env = _classEnvs[cls.classIndex]; 407 ClassEnv env = _classEnvs[cls.classIndex];
627 _ensureSupertypes(cls, env); 408 _ensureSupertypes(cls, env);
628 return env.mixedInType?.element; 409 return env.mixedInType?.element;
629 } 410 }
630 411
631 bool _isMixinApplication(KClass cls) { 412 bool _isMixinApplication(IndexedClass cls) {
632 _KClassEnv env = _classEnvs[cls.classIndex]; 413 ClassEnv env = _classEnvs[cls.classIndex];
633 _ensureSupertypes(cls, env); 414 _ensureSupertypes(cls, env);
634 return env.isMixinApplication; 415 return env.isMixinApplication;
635 } 416 }
636 417
637 bool _isUnnamedMixinApplication(KClass cls) { 418 bool _isUnnamedMixinApplication(IndexedClass cls) {
638 _KClassEnv env = _classEnvs[cls.classIndex]; 419 ClassEnv env = _classEnvs[cls.classIndex];
639 _ensureSupertypes(cls, env); 420 _ensureSupertypes(cls, env);
640 return env.isUnnamedMixinApplication; 421 return env.isUnnamedMixinApplication;
641 } 422 }
642 423
643 void _forEachSupertype(KClass cls, void f(InterfaceType supertype)) { 424 void _forEachSupertype(IndexedClass cls, void f(InterfaceType supertype)) {
644 _KClassEnv env = _classEnvs[cls.classIndex]; 425 ClassEnv env = _classEnvs[cls.classIndex];
645 _ensureSupertypes(cls, env); 426 _ensureSupertypes(cls, env);
646 env.orderedTypeSet.supertypes.forEach(f); 427 env.orderedTypeSet.supertypes.forEach(f);
647 } 428 }
648 429
649 void _forEachMixin(KClass cls, void f(ClassEntity mixin)) { 430 void _forEachMixin(IndexedClass cls, void f(ClassEntity mixin)) {
650 while (cls != null) { 431 while (cls != null) {
651 _KClassEnv env = _classEnvs[cls.classIndex]; 432 ClassEnv env = _classEnvs[cls.classIndex];
652 _ensureSupertypes(cls, env); 433 _ensureSupertypes(cls, env);
653 if (env.mixedInType != null) { 434 if (env.mixedInType != null) {
654 f(env.mixedInType.element); 435 f(env.mixedInType.element);
655 } 436 }
656 cls = env.supertype?.element; 437 cls = env.supertype?.element;
657 } 438 }
658 } 439 }
659 440
660 void _forEachConstructor(KClass cls, void f(ConstructorEntity member)) { 441 void _forEachConstructor(IndexedClass cls, void f(ConstructorEntity member)) {
661 _KClassEnv env = _classEnvs[cls.classIndex]; 442 ClassEnv env = _classEnvs[cls.classIndex];
662 env.forEachConstructor((ir.Member member) { 443 env.forEachConstructor((ir.Member member) {
663 f(getConstructor(member)); 444 f(getConstructor(member));
664 }); 445 });
665 } 446 }
666 447
667 void _forEachClassMember( 448 void _forEachClassMember(
668 KClass cls, void f(ClassEntity cls, MemberEntity member)) { 449 IndexedClass cls, void f(ClassEntity cls, MemberEntity member)) {
669 _KClassEnv env = _classEnvs[cls.classIndex]; 450 ClassEnv env = _classEnvs[cls.classIndex];
670 env.forEachMember((ir.Member member) { 451 env.forEachMember((ir.Member member) {
671 f(cls, getMember(member)); 452 f(cls, getMember(member));
672 }); 453 });
673 _ensureSupertypes(cls, env); 454 _ensureSupertypes(cls, env);
674 if (env.supertype != null) { 455 if (env.supertype != null) {
675 _forEachClassMember(env.supertype.element, f); 456 _forEachClassMember(env.supertype.element, f);
676 } 457 }
677 } 458 }
678 459
679 ConstantConstructor _getConstructorConstant(KConstructor constructor) { 460 ConstantConstructor _getConstructorConstant(IndexedConstructor constructor) {
680 _ConstructorData data = _memberList[constructor.memberIndex]; 461 ConstructorData data = _memberList[constructor.memberIndex];
681 return data.getConstructorConstant(this, constructor); 462 return data.getConstructorConstant(this, constructor);
682 } 463 }
683 464
684 ConstantExpression _getFieldConstant(KField field) { 465 ConstantExpression _getFieldConstant(IndexedField field) {
685 _FieldData data = _memberList[field.memberIndex]; 466 FieldData data = _memberList[field.memberIndex];
686 return data.getFieldConstant(this, field); 467 return data.getFieldConstant(this, field);
687 } 468 }
469
470 InterfaceType _asInstanceOf(InterfaceType type, ClassEntity cls) {
471 OrderedTypeSet orderedTypeSet = _getOrderedTypeSet(type.element);
472 InterfaceType supertype =
473 orderedTypeSet.asInstanceOf(cls, _getHierarchyDepth(cls));
474 if (supertype != null) {
475 supertype = _substByContext(supertype, type);
476 }
477 return supertype;
478 }
479
480 OrderedTypeSet _getOrderedTypeSet(IndexedClass cls) {
481 ClassEnv env = _classEnvs[cls.classIndex];
482 _ensureSupertypes(cls, env);
483 return env.orderedTypeSet;
484 }
485
486 int _getHierarchyDepth(IndexedClass cls) {
487 ClassEnv env = _classEnvs[cls.classIndex];
488 _ensureSupertypes(cls, env);
489 return env.orderedTypeSet.maxDepth;
490 }
491
492 Iterable<InterfaceType> _getInterfaces(IndexedClass cls) {
493 ClassEnv env = _classEnvs[cls.classIndex];
494 _ensureSupertypes(cls, env);
495 return env.interfaces;
496 }
497
498 Spannable _getSpannable(MemberEntity member, ir.Node node) {
499 return member;
500 }
501
502 ir.Member _getMemberNode(covariant IndexedMember member) {
503 return _memberList[member.memberIndex].node;
504 }
505
506 ir.Class _getClassNode(covariant IndexedClass cls) {
507 return _classEnvs[cls.classIndex].cls;
508 }
688 } 509 }
689 510
690 class KernelToElementMapForImpactImpl extends KernelToElementMapBase 511 /// Mixin that implements the abstract methods in [KernelToElementMapBase] by
691 with KernelToElementMapForImpactMixin { 512 /// creating K-model elements.
513 abstract class KElementCreatorMixin {
514 ProgramEnv get _env;
515 List<LibraryEnv> get _libraryEnvs;
516 List<ClassEnv> get _classEnvs;
517 List<MemberData> get _memberList;
518
519 Map<ir.Library, KLibrary> _libraryMap = <ir.Library, KLibrary>{};
520 Map<ir.Class, KClass> _classMap = <ir.Class, KClass>{};
521 Map<ir.TypeParameter, KTypeVariable> _typeVariableMap =
522 <ir.TypeParameter, KTypeVariable>{};
523 Map<ir.Member, KConstructor> _constructorMap = <ir.Member, KConstructor>{};
524 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{};
525 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{};
526 Map<ir.TreeNode, KLocalFunction> _localFunctionMap =
527 <ir.TreeNode, KLocalFunction>{};
528
529 Name getName(ir.Name node);
530 FunctionType getFunctionType(ir.FunctionNode node);
531 MemberEntity getMember(ir.Member node);
532
533 Iterable<LibraryEntity> get _libraries {
534 if (_env.length != _libraryMap.length) {
535 // Create a [KLibrary] for each library.
536 _env.forEachLibrary((LibraryEnv env) {
537 _getLibrary(env.library, env);
538 });
539 }
540 return _libraryMap.values;
541 }
542
543 LibraryEntity _getLibrary(ir.Library node, [LibraryEnv libraryEnv]) {
544 return _libraryMap.putIfAbsent(node, () {
545 Uri canonicalUri = node.importUri;
546 _libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(canonicalUri));
547 String name = node.name;
548 if (name == null) {
549 // Use the file name as script name.
550 String path = canonicalUri.path;
551 name = path.substring(path.lastIndexOf('/') + 1);
552 }
553 return new KLibrary(_libraryMap.length, name, canonicalUri);
554 });
555 }
556
557 ClassEntity _getClass(ir.Class node, [ClassEnv classEnv]) {
558 return _classMap.putIfAbsent(node, () {
559 KLibrary library = _getLibrary(node.enclosingLibrary);
560 if (classEnv == null) {
561 classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name);
562 }
563 _classEnvs.add(classEnv);
564 return new KClass(library, _classMap.length, node.name,
565 isAbstract: node.isAbstract);
566 });
567 }
568
569 TypeVariableEntity _getTypeVariable(ir.TypeParameter node) {
570 return _typeVariableMap.putIfAbsent(node, () {
571 if (node.parent is ir.Class) {
572 ir.Class cls = node.parent;
573 int index = cls.typeParameters.indexOf(node);
574 return new KTypeVariable(_getClass(cls), node.name, index);
575 }
576 if (node.parent is ir.FunctionNode) {
577 ir.FunctionNode func = node.parent;
578 int index = func.typeParameters.indexOf(node);
579 if (func.parent is ir.Constructor) {
580 ir.Constructor constructor = func.parent;
581 ir.Class cls = constructor.enclosingClass;
582 return _getTypeVariable(cls.typeParameters[index]);
583 }
584 if (func.parent is ir.Procedure) {
585 ir.Procedure procedure = func.parent;
586 if (procedure.kind == ir.ProcedureKind.Factory) {
587 ir.Class cls = procedure.enclosingClass;
588 return _getTypeVariable(cls.typeParameters[index]);
589 } else {
590 return new KTypeVariable(_getMethod(procedure), node.name, index);
591 }
592 }
593 }
594 throw new UnsupportedError('Unsupported type parameter type node $node.');
595 });
596 }
597
598 ConstructorEntity _getConstructor(ir.Member node) {
599 return _constructorMap.putIfAbsent(node, () {
600 int memberIndex = _memberList.length;
601 KConstructor constructor;
602 KClass enclosingClass = _getClass(node.enclosingClass);
603 Name name = getName(node.name);
604 bool isExternal = node.isExternal;
605
606 ir.FunctionNode functionNode;
607 if (node is ir.Constructor) {
608 functionNode = node.function;
609 constructor = new KGenerativeConstructor(memberIndex, enclosingClass,
610 name, _getParameterStructure(functionNode),
611 isExternal: isExternal, isConst: node.isConst);
612 } else if (node is ir.Procedure) {
613 functionNode = node.function;
614 bool isFromEnvironment = isExternal &&
615 name.text == 'fromEnvironment' &&
616 const ['int', 'bool', 'String'].contains(enclosingClass.name);
617 constructor = new KFactoryConstructor(memberIndex, enclosingClass, name,
618 _getParameterStructure(functionNode),
619 isExternal: isExternal,
620 isConst: node.isConst,
621 isFromEnvironmentConstructor: isFromEnvironment);
622 } else {
623 // TODO(johnniwinther): Convert `node.location` to a [SourceSpan].
624 throw new SpannableAssertionFailure(
625 NO_LOCATION_SPANNABLE, "Unexpected constructor node: ${node}.");
626 }
627 _memberList.add(new ConstructorData(node, functionNode));
628 return constructor;
629 });
630 }
631
632 FunctionEntity _getMethod(ir.Procedure node) {
633 return _methodMap.putIfAbsent(node, () {
634 int memberIndex = _memberList.length;
635 LibraryEntity library;
636 ClassEntity enclosingClass;
637 if (node.enclosingClass != null) {
638 enclosingClass = _getClass(node.enclosingClass);
639 library = enclosingClass.library;
640 } else {
641 library = _getLibrary(node.enclosingLibrary);
642 }
643 Name name = getName(node.name);
644 bool isStatic = node.isStatic;
645 bool isExternal = node.isExternal;
646 bool isAbstract = node.isAbstract;
647 KFunction function;
648 AsyncMarker asyncMarker;
649 switch (node.function.asyncMarker) {
650 case ir.AsyncMarker.Async:
651 asyncMarker = AsyncMarker.ASYNC;
652 break;
653 case ir.AsyncMarker.AsyncStar:
654 asyncMarker = AsyncMarker.ASYNC_STAR;
655 break;
656 case ir.AsyncMarker.Sync:
657 asyncMarker = AsyncMarker.SYNC;
658 break;
659 case ir.AsyncMarker.SyncStar:
660 asyncMarker = AsyncMarker.SYNC_STAR;
661 break;
662 case ir.AsyncMarker.SyncYielding:
663 throw new UnsupportedError(
664 "Async marker ${node.function.asyncMarker} is not supported.");
665 }
666 switch (node.kind) {
667 case ir.ProcedureKind.Factory:
668 throw new UnsupportedError("Cannot create method from factory.");
669 case ir.ProcedureKind.Getter:
670 function = new KGetter(
671 memberIndex, library, enclosingClass, name, asyncMarker,
672 isStatic: isStatic,
673 isExternal: isExternal,
674 isAbstract: isAbstract);
675 break;
676 case ir.ProcedureKind.Method:
677 case ir.ProcedureKind.Operator:
678 function = new KMethod(memberIndex, library, enclosingClass, name,
679 _getParameterStructure(node.function), asyncMarker,
680 isStatic: isStatic,
681 isExternal: isExternal,
682 isAbstract: isAbstract);
683 break;
684 case ir.ProcedureKind.Setter:
685 assert(asyncMarker == AsyncMarker.SYNC);
686 function = new KSetter(
687 memberIndex, library, enclosingClass, getName(node.name).setter,
688 isStatic: isStatic,
689 isExternal: isExternal,
690 isAbstract: isAbstract);
691 break;
692 }
693 _memberList.add(new FunctionData(node, node.function));
694 return function;
695 });
696 }
697
698 FieldEntity _getField(ir.Field node) {
699 return _fieldMap.putIfAbsent(node, () {
700 int memberIndex = _memberList.length;
701 LibraryEntity library;
702 ClassEntity enclosingClass;
703 if (node.enclosingClass != null) {
704 enclosingClass = _getClass(node.enclosingClass);
705 library = enclosingClass.library;
706 } else {
707 library = _getLibrary(node.enclosingLibrary);
708 }
709 Name name = getName(node.name);
710 bool isStatic = node.isStatic;
711 _memberList.add(new FieldData(node));
712 return new KField(memberIndex, library, enclosingClass, name,
713 isStatic: isStatic,
714 isAssignable: node.isMutable,
715 isConst: node.isConst);
716 });
717 }
718
719 ParameterStructure _getParameterStructure(ir.FunctionNode node) {
720 // TODO(johnniwinther): Cache the computed function type.
721 int requiredParameters = node.requiredParameterCount;
722 int positionalParameters = node.positionalParameters.length;
723 List<String> namedParameters =
724 node.namedParameters.map((p) => p.name).toList()..sort();
725 return new ParameterStructure(
726 requiredParameters, positionalParameters, namedParameters);
727 }
728
729 Local _getLocalFunction(ir.TreeNode node) {
730 return _localFunctionMap.putIfAbsent(node, () {
731 MemberEntity memberContext;
732 Entity executableContext;
733 ir.TreeNode parent = node.parent;
734 while (parent != null) {
735 if (parent is ir.Member) {
736 executableContext = memberContext = getMember(parent);
737 break;
738 }
739 if (parent is ir.FunctionDeclaration ||
740 parent is ir.FunctionExpression) {
741 KLocalFunction localFunction = _getLocalFunction(parent);
742 executableContext = localFunction;
743 memberContext = localFunction.memberContext;
744 break;
745 }
746 parent = parent.parent;
747 }
748 String name;
749 FunctionType functionType;
750 if (node is ir.FunctionDeclaration) {
751 name = node.variable.name;
752 functionType = getFunctionType(node.function);
753 } else if (node is ir.FunctionExpression) {
754 functionType = getFunctionType(node.function);
755 }
756 return new KLocalFunction(
757 name, memberContext, executableContext, functionType);
758 });
759 }
760 }
761
762 /// Implementation of [KernelToElementMapForImpact] that only supports world
763 /// impact computation.
764 // TODO(johnniwinther): Merge this with [KernelToElementMapForImpactImpl] when
765 // [JsStrategy] is the default.
766 abstract class KernelToElementMapForImpactImpl
767 implements
768 KernelToElementMapBase,
769 KernelToElementMapForImpact,
770 KernelToElementMapForImpactMixin {
692 native.BehaviorBuilder _nativeBehaviorBuilder; 771 native.BehaviorBuilder _nativeBehaviorBuilder;
693 772
694 KernelToElementMapForImpactImpl(
695 DiagnosticReporter reporter, Environment environment)
696 : super(reporter, environment) {
697 _nativeBehaviorBuilder = new KernelBehaviorBuilder(_commonElements);
698 }
699
700 @override
701 native.BehaviorBuilder get nativeBehaviorBuilder => _nativeBehaviorBuilder;
702 }
703
704 /// Element builder used for creating elements and types corresponding to Kernel
705 /// IR nodes.
706 class KernelToElementMapImpl extends KernelToElementMapForImpactImpl
707 with KernelToElementMapForBuildingMixin
708 implements KernelToWorldBuilder {
709 _KernelDartTypes _types;
710
711 KernelToElementMapImpl(DiagnosticReporter reporter, Environment environment)
712 : super(reporter, environment) {
713 _types = new _KernelDartTypes(this);
714 }
715
716 /// Adds libraries in [program] to the set of libraries. 773 /// Adds libraries in [program] to the set of libraries.
717 /// 774 ///
718 /// The main method of the first program is used as the main method for the 775 /// The main method of the first program is used as the main method for the
719 /// compilation. 776 /// compilation.
720 void addProgram(ir.Program program) { 777 void addProgram(ir.Program program) {
721 _env.addProgram(program); 778 _env.addProgram(program);
722 } 779 }
723 780
724 ConstantEnvironment get constantEnvironment => _constantEnvironment; 781 @override
782 native.BehaviorBuilder get nativeBehaviorBuilder =>
783 _nativeBehaviorBuilder ??= new KernelBehaviorBuilder(commonElements);
725 784
726 DartTypes get types => _types; 785 ResolutionImpact computeWorldImpact(KMember member) {
727 786 return _memberList[member.memberIndex].getWorldImpact(this);
728 @override
729 ConstantValue getFieldConstantValue(ir.Field field) {
730 // TODO(johnniwinther): Cache the result in [_FieldData].
731 return getConstantValue(field.initializer,
732 requireConstant: field.isConst, implicitNull: !field.isConst);
733 }
734
735 Iterable<ConstantValue> _getClassMetadata(KClass cls) {
736 return _classEnvs[cls.classIndex].getMetadata(this);
737 } 787 }
738 788
739 /// Returns the kernel [ir.Procedure] node for the [method]. 789 /// Returns the kernel [ir.Procedure] node for the [method].
740 ir.Procedure _lookupProcedure(KFunction method) { 790 ir.Procedure _lookupProcedure(KFunction method) {
741 return _memberList[method.memberIndex].node; 791 return _memberList[method.memberIndex].node;
742 } 792 }
743 793
744 InterfaceType _asInstanceOf(InterfaceType type, KClass cls) { 794 Iterable<ConstantValue> _getClassMetadata(KClass cls) {
745 OrderedTypeSet orderedTypeSet = _getOrderedTypeSet(type.element); 795 return _classEnvs[cls.classIndex].getMetadata(this);
746 InterfaceType supertype =
747 orderedTypeSet.asInstanceOf(cls, _getHierarchyDepth(cls));
748 if (supertype != null) {
749 supertype = _substByContext(supertype, type);
750 }
751 return supertype;
752 } 796 }
797 }
753 798
754 OrderedTypeSet _getOrderedTypeSet(KClass cls) { 799 /// Implementation of [KernelToElementMapForImpact] that only supports world
755 _KClassEnv env = _classEnvs[cls.classIndex]; 800 /// impact computation.
756 _ensureSupertypes(cls, env); 801 // TODO(johnniwinther): Merge this with [KernelToElementMapForImpactImpl] when
757 return env.orderedTypeSet; 802 // [JsStrategy] is the default.
758 } 803 class KernelToElementMapForImpactImpl2 extends KernelToElementMapBase
804 with
805 KernelToElementMapForImpactMixin,
806 KernelToElementMapForImpactImpl,
807 KElementCreatorMixin {
808 KernelToElementMapForImpactImpl2(
809 DiagnosticReporter reporter, Environment environment)
810 : super(reporter, environment);
811 }
759 812
760 int _getHierarchyDepth(KClass cls) { 813 /// Element builder used for creating elements and types corresponding to Kernel
761 _KClassEnv env = _classEnvs[cls.classIndex]; 814 /// IR nodes.
762 _ensureSupertypes(cls, env); 815 // TODO(johnniwinther): Use this in the JsStrategy
763 return env.orderedTypeSet.maxDepth; 816 class KernelToElementMapForBuildingImpl extends KernelToElementMapBase
764 } 817 with KernelToElementMapForBuildingMixin, KElementCreatorMixin
818 implements KernelToWorldBuilder {
819 KernelToElementMapForBuildingImpl(
820 DiagnosticReporter reporter, Environment environment)
821 : super(reporter, environment);
765 822
766 Iterable<InterfaceType> _getInterfaces(KClass cls) { 823 ConstantEnvironment get constantEnvironment => _constantEnvironment;
767 _KClassEnv env = _classEnvs[cls.classIndex]; 824
768 _ensureSupertypes(cls, env); 825 @override
769 return env.interfaces; 826 ConstantValue getFieldConstantValue(ir.Field field) {
827 // TODO(johnniwinther): Cache the result in [FieldData].
828 return getConstantValue(field.initializer,
829 requireConstant: field.isConst, implicitNull: !field.isConst);
770 } 830 }
771 831
772 ir.Library getKernelLibrary(KLibrary entity) => 832 ir.Library getKernelLibrary(KLibrary entity) =>
773 _libraryEnvs[entity.libraryIndex].library; 833 _libraryEnvs[entity.libraryIndex].library;
774 834
775 ir.Class getKernelClass(KClass entity) => _classEnvs[entity.classIndex].cls; 835 ir.Class getKernelClass(KClass entity) => _classEnvs[entity.classIndex].cls;
776 836
777 bool hasConstantFieldInitializer(covariant KField field) { 837 bool hasConstantFieldInitializer(covariant KField field) {
778 _FieldData data = _memberList[field.memberIndex]; 838 FieldData data = _memberList[field.memberIndex];
779 return getFieldConstantValue(data.node) != null; 839 return getFieldConstantValue(data.node) != null;
780 } 840 }
781 841
782 ConstantValue getConstantFieldInitializer(covariant KField field) { 842 ConstantValue getConstantFieldInitializer(covariant KField field) {
783 _FieldData data = _memberList[field.memberIndex]; 843 FieldData data = _memberList[field.memberIndex];
784 ConstantValue value = getFieldConstantValue(data.node); 844 ConstantValue value = getFieldConstantValue(data.node);
785 assert(value != null, 845 assert(value != null,
786 failedAt(field, "Field $field doesn't have a constant initial value.")); 846 failedAt(field, "Field $field doesn't have a constant initial value."));
787 return value; 847 return value;
788 } 848 }
789 849
790 void forEachParameter(covariant KFunction function, 850 void forEachParameter(covariant KFunction function,
791 void f(DartType type, String name, ConstantValue defaultValue)) { 851 void f(DartType type, String name, ConstantValue defaultValue)) {
792 _FunctionData data = _memberList[function.memberIndex]; 852 FunctionData data = _memberList[function.memberIndex];
793 data.forEachParameter(this, f); 853 data.forEachParameter(this, f);
794 } 854 }
795 855
796 ResolutionImpact computeWorldImpact(KMember member) {
797 return _memberList[member.memberIndex].getWorldImpact(this);
798 }
799
800 @override 856 @override
801 Spannable getSpannable(MemberEntity member, ir.Node node) { 857 Spannable getSpannable(MemberEntity member, ir.Node node) {
802 return member; 858 return _getSpannable(member, node);
803 } 859 }
804 860
805 @override 861 @override
806 ir.Member getMemberNode(covariant KMember member) { 862 ir.Member getMemberNode(MemberEntity member) {
807 return _memberList[member.memberIndex].node; 863 return _getMemberNode(member);
808 } 864 }
809 865
810 /// Returns the kernel IR node that defines the [cls]. 866 @override
811 ir.Class getClassNode(KClass cls) { 867 ir.Class getClassNode(ClassEntity cls) {
812 return _classEnvs[cls.classIndex].cls; 868 return _getClassNode(cls);
813 } 869 }
814 } 870 }
815 871
816 /// Environment for fast lookup of program libraries. 872 /// [KernelToElementMap] implementation used for both world impact computation
817 class _KEnv { 873 /// and SSA building.
818 final Set<ir.Program> programs = new Set<ir.Program>(); 874 // TODO(johnniwinther): Remove this when [JsStrategy] is the default.
819 875 class KernelToElementMapImpl extends KernelToElementMapForBuildingImpl
820 Map<Uri, _KLibraryEnv> _libraryMap; 876 with
821 877 KernelToElementMapForImpactMixin,
822 /// TODO(johnniwinther): Handle arbitrary load order if needed. 878 KernelToElementMapForImpactImpl,
823 ir.Member get mainMethod => programs.first?.mainMethod; 879 KElementCreatorMixin
824 880 implements KernelToElementMapForImpactImpl2 {
825 void addProgram(ir.Program program) { 881 KernelToElementMapImpl(DiagnosticReporter reporter, Environment environment)
826 if (programs.add(program)) { 882 : super(reporter, environment);
827 if (_libraryMap != null) {
828 _addLibraries(program);
829 }
830 }
831 }
832
833 void _addLibraries(ir.Program program) {
834 for (ir.Library library in program.libraries) {
835 _libraryMap[library.importUri] = new _KLibraryEnv(library);
836 }
837 }
838
839 void _ensureLibraryMap() {
840 if (_libraryMap == null) {
841 _libraryMap = <Uri, _KLibraryEnv>{};
842 for (ir.Program program in programs) {
843 _addLibraries(program);
844 }
845 }
846 }
847
848 /// Return the [_KLibraryEnv] for the library with the canonical [uri].
849 _KLibraryEnv lookupLibrary(Uri uri) {
850 _ensureLibraryMap();
851 return _libraryMap[uri];
852 }
853
854 /// Calls [f] for each library in this environment.
855 void forEachLibrary(void f(_KLibraryEnv library)) {
856 _ensureLibraryMap();
857 _libraryMap.values.forEach(f);
858 }
859
860 /// Returns the number of libraries in this environment.
861 int get length {
862 _ensureLibraryMap();
863 return _libraryMap.length;
864 }
865 }
866
867 /// Environment for fast lookup of library classes and members.
868 class _KLibraryEnv {
869 final ir.Library library;
870
871 Map<String, _KClassEnv> _classMap;
872 Map<String, ir.Member> _memberMap;
873 Map<String, ir.Member> _setterMap;
874
875 _KLibraryEnv(this.library);
876
877 void _ensureClassMap() {
878 if (_classMap == null) {
879 _classMap = <String, _KClassEnv>{};
880 for (ir.Class cls in library.classes) {
881 _classMap[cls.name] = new _KClassEnv(cls);
882 }
883 }
884 }
885
886 /// Return the [_KClassEnv] for the class [name] in [library].
887 _KClassEnv lookupClass(String name) {
888 _ensureClassMap();
889 return _classMap[name];
890 }
891
892 /// Calls [f] for each class in this library.
893 void forEachClass(void f(_KClassEnv cls)) {
894 _ensureClassMap();
895 _classMap.values.forEach(f);
896 }
897
898 void _ensureMemberMaps() {
899 if (_memberMap == null) {
900 _memberMap = <String, ir.Member>{};
901 _setterMap = <String, ir.Member>{};
902 for (ir.Member member in library.members) {
903 if (member is ir.Procedure) {
904 if (member.kind == ir.ProcedureKind.Setter) {
905 _setterMap[member.name.name] = member;
906 } else {
907 _memberMap[member.name.name] = member;
908 }
909 } else if (member is ir.Field) {
910 _memberMap[member.name.name] = member;
911 if (member.isMutable) {
912 _setterMap[member.name.name] = member;
913 }
914 } else {
915 throw new SpannableAssertionFailure(
916 NO_LOCATION_SPANNABLE, "Unexpected library member node: $member");
917 }
918 }
919 }
920 }
921
922 /// Return the [ir.Member] for the member [name] in [library].
923 ir.Member lookupMember(String name, {bool setter: false}) {
924 _ensureMemberMaps();
925 return setter ? _setterMap[name] : _memberMap[name];
926 }
927
928 void forEachMember(void f(ir.Member member)) {
929 _ensureMemberMaps();
930 _memberMap.values.forEach(f);
931 for (ir.Member member in _setterMap.values) {
932 if (member is ir.Procedure) {
933 f(member);
934 } else {
935 // Skip fields; these are also in _memberMap.
936 }
937 }
938 }
939 }
940
941 /// Environment for fast lookup of class members.
942 class _KClassEnv {
943 final ir.Class cls;
944 bool isMixinApplication;
945 final bool isUnnamedMixinApplication;
946
947 InterfaceType thisType;
948 InterfaceType rawType;
949 InterfaceType supertype;
950 InterfaceType mixedInType;
951 List<InterfaceType> interfaces;
952 OrderedTypeSet orderedTypeSet;
953
954 Map<String, ir.Member> _constructorMap;
955 Map<String, ir.Member> _memberMap;
956 Map<String, ir.Member> _setterMap;
957
958 Iterable<ConstantValue> _metadata;
959
960 _KClassEnv(this.cls)
961 // TODO(johnniwinther): Change this to use a property on [cls] when such
962 // is added to kernel.
963 : isUnnamedMixinApplication =
964 cls.name.contains('+') || cls.name.contains('&');
965
966 /// Copied from 'package:kernel/transformations/mixin_full_resolution.dart'.
967 ir.Constructor _buildForwardingConstructor(
968 CloneVisitor cloner, ir.Constructor superclassConstructor) {
969 var superFunction = superclassConstructor.function;
970
971 // We keep types and default values for the parameters but always mark the
972 // parameters as final (since we just forward them to the super
973 // constructor).
974 ir.VariableDeclaration cloneVariable(ir.VariableDeclaration variable) {
975 ir.VariableDeclaration clone = cloner.clone(variable);
976 clone.isFinal = true;
977 return clone;
978 }
979
980 // Build a [FunctionNode] which has the same parameters as the one in the
981 // superclass constructor.
982 var positionalParameters =
983 superFunction.positionalParameters.map(cloneVariable).toList();
984 var namedParameters =
985 superFunction.namedParameters.map(cloneVariable).toList();
986 var function = new ir.FunctionNode(new ir.EmptyStatement(),
987 positionalParameters: positionalParameters,
988 namedParameters: namedParameters,
989 requiredParameterCount: superFunction.requiredParameterCount,
990 returnType: const ir.VoidType());
991
992 // Build a [SuperInitializer] which takes all positional/named parameters
993 // and forward them to the super class constructor.
994 var positionalArguments = <ir.Expression>[];
995 for (var variable in positionalParameters) {
996 positionalArguments.add(new ir.VariableGet(variable));
997 }
998 var namedArguments = <ir.NamedExpression>[];
999 for (var variable in namedParameters) {
1000 namedArguments.add(
1001 new ir.NamedExpression(variable.name, new ir.VariableGet(variable)));
1002 }
1003 var superInitializer = new ir.SuperInitializer(superclassConstructor,
1004 new ir.Arguments(positionalArguments, named: namedArguments));
1005
1006 // Assemble the constructor.
1007 return new ir.Constructor(function,
1008 name: superclassConstructor.name,
1009 initializers: <ir.Initializer>[superInitializer]);
1010 }
1011
1012 void _ensureMaps() {
1013 if (_memberMap == null) {
1014 _memberMap = <String, ir.Member>{};
1015 _setterMap = <String, ir.Member>{};
1016 _constructorMap = <String, ir.Member>{};
1017
1018 void addMembers(ir.Class c, {bool includeStatic}) {
1019 for (ir.Member member in c.members) {
1020 if (member is ir.Constructor ||
1021 member is ir.Procedure &&
1022 member.kind == ir.ProcedureKind.Factory) {
1023 if (!includeStatic) continue;
1024 _constructorMap[member.name.name] = member;
1025 } else if (member is ir.Procedure) {
1026 if (!includeStatic && member.isStatic) continue;
1027 if (member.kind == ir.ProcedureKind.Setter) {
1028 _setterMap[member.name.name] = member;
1029 } else {
1030 _memberMap[member.name.name] = member;
1031 }
1032 } else if (member is ir.Field) {
1033 if (!includeStatic && member.isStatic) continue;
1034 _memberMap[member.name.name] = member;
1035 if (member.isMutable) {
1036 _setterMap[member.name.name] = member;
1037 }
1038 _memberMap[member.name.name] = member;
1039 } else {
1040 throw new SpannableAssertionFailure(
1041 NO_LOCATION_SPANNABLE, "Unexpected class member node: $member");
1042 }
1043 }
1044 }
1045
1046 if (cls.mixedInClass != null) {
1047 addMembers(cls.mixedInClass, includeStatic: false);
1048 }
1049 addMembers(cls, includeStatic: true);
1050
1051 if (isUnnamedMixinApplication && _constructorMap.isEmpty) {
1052 // Unnamed mixin applications have no constructors when read from .dill.
1053 // For each generative constructor in the superclass we make a
1054 // corresponding forwarding constructor in the subclass.
1055 //
1056 // This code is copied from
1057 // 'package:kernel/transformations/mixin_full_resolution.dart'
1058 var superclassSubstitution = getSubstitutionMap(cls.supertype);
1059 var superclassCloner =
1060 new CloneVisitor(typeSubstitution: superclassSubstitution);
1061 for (var superclassConstructor in cls.superclass.constructors) {
1062 var forwardingConstructor = _buildForwardingConstructor(
1063 superclassCloner, superclassConstructor);
1064 cls.addMember(forwardingConstructor);
1065 _constructorMap[forwardingConstructor.name.name] =
1066 forwardingConstructor;
1067 }
1068 }
1069 }
1070 }
1071
1072 /// Return the [ir.Member] for the member [name] in [library].
1073 ir.Member lookupMember(String name, {bool setter: false}) {
1074 _ensureMaps();
1075 return setter ? _setterMap[name] : _memberMap[name];
1076 }
1077
1078 /// Return the [ir.Member] for the member [name] in [library].
1079 ir.Member lookupConstructor(String name) {
1080 _ensureMaps();
1081 return _constructorMap[name];
1082 }
1083
1084 void forEachMember(void f(ir.Member member)) {
1085 _ensureMaps();
1086 _memberMap.values.forEach(f);
1087 for (ir.Member member in _setterMap.values) {
1088 if (member is ir.Procedure) {
1089 f(member);
1090 } else {
1091 // Skip fields; these are also in _memberMap.
1092 }
1093 }
1094 }
1095
1096 void forEachConstructor(void f(ir.Member member)) {
1097 _ensureMaps();
1098 _constructorMap.values.forEach(f);
1099 }
1100
1101 Iterable<ConstantValue> getMetadata(KernelToElementMapImpl elementMap) {
1102 return _metadata ??= elementMap.getMetadata(cls.annotations);
1103 }
1104 }
1105
1106 class _MemberData {
1107 final ir.Member node;
1108 Iterable<ConstantValue> _metadata;
1109
1110 _MemberData(this.node);
1111
1112 ResolutionImpact getWorldImpact(KernelToElementMapImpl elementMap) {
1113 return buildKernelImpact(node, elementMap);
1114 }
1115
1116 Iterable<ConstantValue> getMetadata(KernelToElementMapImpl elementMap) {
1117 return _metadata ??= elementMap.getMetadata(node.annotations);
1118 }
1119 }
1120
1121 class _FunctionData extends _MemberData {
1122 final ir.FunctionNode functionNode;
1123 FunctionType _type;
1124
1125 _FunctionData(ir.Member node, this.functionNode) : super(node);
1126
1127 FunctionType getFunctionType(KernelToElementMapImpl elementMap) {
1128 return _type ??= elementMap.getFunctionType(functionNode);
1129 }
1130
1131 void forEachParameter(KernelToElementMapForBuilding elementMap,
1132 void f(DartType type, String name, ConstantValue defaultValue)) {
1133 void handleParameter(ir.VariableDeclaration node, {bool isOptional: true}) {
1134 DartType type = elementMap.getDartType(node.type);
1135 String name = node.name;
1136 ConstantValue defaultValue;
1137 if (isOptional) {
1138 if (node.initializer != null) {
1139 defaultValue = elementMap.getConstantValue(node.initializer);
1140 } else {
1141 defaultValue = new NullConstantValue();
1142 }
1143 }
1144 f(type, name, defaultValue);
1145 }
1146
1147 for (int i = 0; i < functionNode.positionalParameters.length; i++) {
1148 handleParameter(functionNode.positionalParameters[i],
1149 isOptional: i < functionNode.requiredParameterCount);
1150 }
1151 functionNode.namedParameters.toList()
1152 ..sort(namedOrdering)
1153 ..forEach(handleParameter);
1154 }
1155 }
1156
1157 class _ConstructorData extends _FunctionData {
1158 ConstantConstructor _constantConstructor;
1159
1160 _ConstructorData(ir.Member node, ir.FunctionNode functionNode)
1161 : super(node, functionNode);
1162
1163 ConstantConstructor getConstructorConstant(
1164 KernelToElementMapImpl elementMap, KConstructor constructor) {
1165 if (_constantConstructor == null) {
1166 if (node is ir.Constructor && constructor.isConst) {
1167 _constantConstructor =
1168 new Constantifier(elementMap).computeConstantConstructor(node);
1169 } else {
1170 throw new SpannableAssertionFailure(
1171 constructor,
1172 "Unexpected constructor $constructor in "
1173 "KernelWorldBuilder._getConstructorConstant");
1174 }
1175 }
1176 return _constantConstructor;
1177 }
1178 }
1179
1180 class _FieldData extends _MemberData {
1181 ConstantExpression _constant;
1182
1183 _FieldData(ir.Field node) : super(node);
1184
1185 ir.Field get node => super.node;
1186
1187 ConstantExpression getFieldConstant(
1188 KernelToElementMapImpl elementMap, KField field) {
1189 if (_constant == null) {
1190 if (node.isConst) {
1191 _constant = new Constantifier(elementMap).visit(node.initializer);
1192 } else {
1193 throw new SpannableAssertionFailure(
1194 field,
1195 "Unexpected field $field in "
1196 "KernelWorldBuilder._getConstructorConstant");
1197 }
1198 }
1199 return _constant;
1200 }
1201 } 883 }
1202 884
1203 class KernelElementEnvironment implements ElementEnvironment { 885 class KernelElementEnvironment implements ElementEnvironment {
1204 final KernelToElementMapBase elementMap; 886 final KernelToElementMapBase elementMap;
1205 887
1206 KernelElementEnvironment(this.elementMap); 888 KernelElementEnvironment(this.elementMap);
1207 889
1208 @override 890 @override
1209 DartType get dynamicType => const DynamicType(); 891 DartType get dynamicType => const DynamicType();
1210 892
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
1386 } 1068 }
1387 1069
1388 @override 1070 @override
1389 bool isDeferredLoadLibraryGetter(covariant KMember member) { 1071 bool isDeferredLoadLibraryGetter(covariant KMember member) {
1390 // TODO(redemption): Support these. 1072 // TODO(redemption): Support these.
1391 return false; 1073 return false;
1392 } 1074 }
1393 1075
1394 @override 1076 @override
1395 Iterable<ConstantValue> getMemberMetadata(covariant KMember member) { 1077 Iterable<ConstantValue> getMemberMetadata(covariant KMember member) {
1396 _MemberData memberData = elementMap._memberList[member.memberIndex]; 1078 MemberData memberData = elementMap._memberList[member.memberIndex];
1397 return memberData.getMetadata(elementMap); 1079 return memberData.getMetadata(elementMap);
1398 } 1080 }
1399 } 1081 }
1400 1082
1401 /// Visitor that converts kernel dart types into [DartType]. 1083 /// Visitor that converts kernel dart types into [DartType].
1402 class DartTypeConverter extends ir.DartTypeVisitor<DartType> { 1084 class DartTypeConverter extends ir.DartTypeVisitor<DartType> {
1403 final KernelToElementMapBase elementMap; 1085 final KernelToElementMapBase elementMap;
1404 bool topLevel = true; 1086 bool topLevel = true;
1405 1087
1406 DartTypeConverter(this.elementMap); 1088 DartTypeConverter(this.elementMap);
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
1559 throw new UnimplementedError("_EvaluationEnvironment.getLocalConstant"); 1241 throw new UnimplementedError("_EvaluationEnvironment.getLocalConstant");
1560 } 1242 }
1561 1243
1562 @override 1244 @override
1563 String readFromEnvironment(String name) { 1245 String readFromEnvironment(String name) {
1564 return _environment.valueOf(name); 1246 return _environment.valueOf(name);
1565 } 1247 }
1566 } 1248 }
1567 1249
1568 class KernelResolutionWorldBuilder extends KernelResolutionWorldBuilderBase { 1250 class KernelResolutionWorldBuilder extends KernelResolutionWorldBuilderBase {
1569 final KernelToElementMapImpl elementMap; 1251 final KernelToElementMapForImpactImpl elementMap;
1570 1252
1571 KernelResolutionWorldBuilder( 1253 KernelResolutionWorldBuilder(
1572 this.elementMap, 1254 this.elementMap,
1573 NativeBasicData nativeBasicData, 1255 NativeBasicData nativeBasicData,
1574 NativeDataBuilder nativeDataBuilder, 1256 NativeDataBuilder nativeDataBuilder,
1575 InterceptorDataBuilder interceptorDataBuilder, 1257 InterceptorDataBuilder interceptorDataBuilder,
1576 BackendUsageBuilder backendUsageBuilder, 1258 BackendUsageBuilder backendUsageBuilder,
1577 SelectorConstraintsStrategy selectorConstraintsStrategy) 1259 SelectorConstraintsStrategy selectorConstraintsStrategy)
1578 : super( 1260 : super(
1579 elementMap.elementEnvironment, 1261 elementMap.elementEnvironment,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1613 } 1295 }
1614 1296
1615 @override 1297 @override
1616 bool validateClass(ClassEntity cls) => true; 1298 bool validateClass(ClassEntity cls) => true;
1617 1299
1618 @override 1300 @override
1619 bool checkClass(ClassEntity cls) => true; 1301 bool checkClass(ClassEntity cls) => true;
1620 } 1302 }
1621 1303
1622 class KernelClosedWorld extends ClosedWorldBase { 1304 class KernelClosedWorld extends ClosedWorldBase {
1623 final KernelToElementMapImpl _elementMap; 1305 final KernelToElementMapForImpactImpl _elementMap;
1624 1306
1625 KernelClosedWorld(this._elementMap, 1307 KernelClosedWorld(this._elementMap,
1626 {ElementEnvironment elementEnvironment, 1308 {ElementEnvironment elementEnvironment,
1627 DartTypes dartTypes, 1309 DartTypes dartTypes,
1628 CommonElements commonElements, 1310 CommonElements commonElements,
1629 ConstantSystem constantSystem, 1311 ConstantSystem constantSystem,
1630 NativeData nativeData, 1312 NativeData nativeData,
1631 InterceptorData interceptorData, 1313 InterceptorData interceptorData,
1632 BackendUsage backendUsage, 1314 BackendUsage backendUsage,
1633 ResolutionWorldBuilder resolutionWorldBuilder, 1315 ResolutionWorldBuilder resolutionWorldBuilder,
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1717 return member == element; 1399 return member == element;
1718 } 1400 }
1719 cls = elementEnvironment.getSuperClass(cls); 1401 cls = elementEnvironment.getSuperClass(cls);
1720 } 1402 }
1721 return false; 1403 return false;
1722 } 1404 }
1723 } 1405 }
1724 1406
1725 // Interface for testing equivalence of Kernel-based entities. 1407 // Interface for testing equivalence of Kernel-based entities.
1726 class WorldDeconstructionForTesting { 1408 class WorldDeconstructionForTesting {
1727 final KernelToElementMapImpl elementMap; 1409 final KernelToElementMapBase elementMap;
1728 1410
1729 WorldDeconstructionForTesting(this.elementMap); 1411 WorldDeconstructionForTesting(this.elementMap);
1730 1412
1731 KClass getSuperclassForClass(KClass cls) { 1413 KClass getSuperclassForClass(KClass cls) {
1732 _KClassEnv env = elementMap._classEnvs[cls.classIndex]; 1414 ClassEnv env = elementMap._classEnvs[cls.classIndex];
1733 ir.Supertype supertype = env.cls.supertype; 1415 ir.Supertype supertype = env.cls.supertype;
1734 if (supertype == null) return null; 1416 if (supertype == null) return null;
1735 return elementMap.getClass(supertype.classNode); 1417 return elementMap.getClass(supertype.classNode);
1736 } 1418 }
1737 1419
1738 bool isUnnamedMixinApplication(KClass cls) { 1420 bool isUnnamedMixinApplication(KClass cls) {
1739 return elementMap._isUnnamedMixinApplication(cls); 1421 return elementMap._isUnnamedMixinApplication(cls);
1740 } 1422 }
1741 1423
1742 InterfaceType getMixinTypeForClass(KClass cls) { 1424 InterfaceType getMixinTypeForClass(KClass cls) {
1743 _KClassEnv env = elementMap._classEnvs[cls.classIndex]; 1425 ClassEnv env = elementMap._classEnvs[cls.classIndex];
1744 ir.Supertype mixedInType = env.cls.mixedInType; 1426 ir.Supertype mixedInType = env.cls.mixedInType;
1745 if (mixedInType == null) return null; 1427 if (mixedInType == null) return null;
1746 return elementMap.createInterfaceType( 1428 return elementMap.createInterfaceType(
1747 mixedInType.classNode, mixedInType.typeArguments); 1429 mixedInType.classNode, mixedInType.typeArguments);
1748 } 1430 }
1749 } 1431 }
1750 1432
1751 class KernelNativeMemberResolver extends NativeMemberResolverBase { 1433 class KernelNativeMemberResolver extends NativeMemberResolverBase {
1752 final KernelToElementMapImpl elementMap; 1434 final KernelToElementMapForImpactImpl elementMap;
1753 final NativeBasicData nativeBasicData; 1435 final NativeBasicData nativeBasicData;
1754 final NativeDataBuilder nativeDataBuilder; 1436 final NativeDataBuilder nativeDataBuilder;
1755 1437
1756 KernelNativeMemberResolver( 1438 KernelNativeMemberResolver(
1757 this.elementMap, this.nativeBasicData, this.nativeDataBuilder); 1439 this.elementMap, this.nativeBasicData, this.nativeDataBuilder);
1758 1440
1759 @override 1441 @override
1760 ElementEnvironment get elementEnvironment => elementMap.elementEnvironment; 1442 ElementEnvironment get elementEnvironment => elementMap.elementEnvironment;
1761 1443
1762 @override 1444 @override
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1800 return false; 1482 return false;
1801 } 1483 }
1802 } 1484 }
1803 1485
1804 class JsKernelToElementMap extends KernelToElementMapBase 1486 class JsKernelToElementMap extends KernelToElementMapBase
1805 with KernelToElementMapForBuildingMixin 1487 with KernelToElementMapForBuildingMixin
1806 implements KernelToWorldBuilder { 1488 implements KernelToWorldBuilder {
1807 final JsToFrontendMap _map; 1489 final JsToFrontendMap _map;
1808 final ElementEnvironment _elementEnvironment; 1490 final ElementEnvironment _elementEnvironment;
1809 final CommonElements _commonElements; 1491 final CommonElements _commonElements;
1810 final KernelToElementMapImpl _elementMap; 1492 final KernelToElementMapForImpactImpl _elementMap;
1811 1493
1812 JsKernelToElementMap( 1494 JsKernelToElementMap(
1813 DiagnosticReporter reporter, 1495 DiagnosticReporter reporter,
1814 Environment environment, 1496 Environment environment,
1815 this._map, 1497 this._map,
1816 this._elementEnvironment, 1498 this._elementEnvironment,
1817 this._commonElements, 1499 this._commonElements,
1818 this._elementMap) 1500 this._elementMap)
1819 : super(reporter, environment); 1501 : super(reporter, environment);
1820 1502
1821 @override 1503 @override
1822 Spannable getSpannable(MemberEntity member, ir.Node node) { 1504 Spannable getSpannable(MemberEntity member, ir.Node node) {
1823 return _elementMap.getSpannable(_map.toFrontendMember(member), node); 1505 return _elementMap._getSpannable(_map.toFrontendMember(member), node);
1506 }
1507
1508 Iterable<LibraryEntity> get _libraries {
1509 return _elementMap._libraries.map(_map.toBackendLibrary);
1824 } 1510 }
1825 1511
1826 @override 1512 @override
1827 LibraryEntity getLibrary(ir.Library node) { 1513 LibraryEntity _getLibrary(ir.Library node, [LibraryEnv env]) {
1828 return _map.toBackendLibrary(_elementMap.getLibrary(node)); 1514 return _map.toBackendLibrary(_elementMap.getLibrary(node));
1829 } 1515 }
1830 1516
1831 @override 1517 @override
1832 Local getLocalFunction(ir.TreeNode node) { 1518 Local _getLocalFunction(ir.TreeNode node) {
1833 throw new UnsupportedError("JsKernelToElementMap.getLocalFunction"); 1519 throw new UnsupportedError("JsKernelToElementMap.getLocalFunction");
1834 } 1520 }
1835 1521
1836 @override 1522 @override
1837 ClassEntity getClass(ir.Class node) { 1523 ClassEntity _getClass(ir.Class node, [ClassEnv env]) {
1838 return _map.toBackendClass(_elementMap.getClass(node)); 1524 return _map.toBackendClass(_elementMap.getClass(node));
1839 } 1525 }
1840 1526
1841 @override 1527 @override
1842 FieldEntity getField(ir.Field node) { 1528 TypeVariableEntity _getTypeVariable(ir.TypeParameter node) {
1529 throw new UnsupportedError("JsKernelToElementMap._getTypeVariable");
1530 }
1531
1532 @override
1533 FieldEntity _getField(ir.Field node) {
1843 return _map.toBackendMember(_elementMap.getField(node)); 1534 return _map.toBackendMember(_elementMap.getField(node));
1844 } 1535 }
1845 1536
1846 @override 1537 @override
1847 MemberEntity getSuperMember(ir.Member context, ir.Name name, ir.Member target, 1538 FunctionEntity _getMethod(ir.Procedure node) {
1848 {bool setter: false}) {
1849 return _map.toBackendMember(
1850 _elementMap.getSuperMember(context, name, target, setter: setter));
1851 }
1852
1853 @override
1854 FunctionEntity getMethod(ir.Procedure node) {
1855 return _map.toBackendMember(_elementMap.getMethod(node)); 1539 return _map.toBackendMember(_elementMap.getMethod(node));
1856 } 1540 }
1857 1541
1858 @override 1542 @override
1859 ir.Member getMemberNode(MemberEntity member) { 1543 ConstructorEntity _getConstructor(ir.Member node) {
1860 return _elementMap.getMemberNode(_map.toFrontendMember(member));
1861 }
1862
1863 @override
1864 MemberEntity getMember(ir.Member node) {
1865 return _map.toBackendMember(_elementMap.getMember(node));
1866 }
1867
1868 @override
1869 ConstructorEntity getConstructor(ir.Member node) {
1870 return _map.toBackendMember(_elementMap.getConstructor(node)); 1544 return _map.toBackendMember(_elementMap.getConstructor(node));
1871 } 1545 }
1872 1546
1873 @override 1547 @override
1548 ir.Member getMemberNode(MemberEntity member) {
1549 return _elementMap._getMemberNode(_map.toFrontendMember(member));
1550 }
1551
1552 @override
1553 ir.Class getClassNode(ClassEntity cls) {
1554 return _elementMap._getClassNode(_map.toFrontendClass(cls));
1555 }
1556
1557 @override
1874 InterfaceType createInterfaceType( 1558 InterfaceType createInterfaceType(
1875 ir.Class cls, List<ir.DartType> typeArguments) { 1559 ir.Class cls, List<ir.DartType> typeArguments) {
1876 return _map 1560 return _map
1877 .toBackendType(_elementMap.createInterfaceType(cls, typeArguments)); 1561 .toBackendType(_elementMap.createInterfaceType(cls, typeArguments));
1878 } 1562 }
1879 1563
1880 @override 1564 @override
1881 InterfaceType getInterfaceType(ir.InterfaceType type) { 1565 InterfaceType getInterfaceType(ir.InterfaceType type) {
1882 return _map.toBackendType(_elementMap.getInterfaceType(type)); 1566 return _map.toBackendType(_elementMap.getInterfaceType(type));
1883 } 1567 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1934 throw new UnsupportedError( 1618 throw new UnsupportedError(
1935 "JsKernelToElementMap.getConstantFieldInitializer"); 1619 "JsKernelToElementMap.getConstantFieldInitializer");
1936 } 1620 }
1937 1621
1938 @override 1622 @override
1939 bool hasConstantFieldInitializer(FieldEntity field) { 1623 bool hasConstantFieldInitializer(FieldEntity field) {
1940 throw new UnsupportedError( 1624 throw new UnsupportedError(
1941 "JsKernelToElementMap.hasConstantFieldInitializer"); 1625 "JsKernelToElementMap.hasConstantFieldInitializer");
1942 } 1626 }
1943 } 1627 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/kernel/element_map.dart ('k') | pkg/compiler/lib/src/kernel/element_map_mixins.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698