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

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

Issue 2666553002: Add KernelWorldBuilder and KElement model. (Closed)
Patch Set: Updated cf. comments. Created 3 years, 10 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/kernel/elements.dart ('k') | pkg/compiler/lib/src/ssa/kernel_impact.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'package:kernel/ast.dart' as ir;
6
7 import '../common.dart';
8 import '../common/names.dart';
9 import '../core_types.dart';
10 import '../elements/elements.dart';
11 import '../elements/entities.dart';
12 import '../elements/types.dart';
13 import '../native/native.dart' as native;
14 import 'element_adapter.dart';
15 import 'elements.dart';
16
17 /// World builder used for creating elements and types corresponding to Kernel
18 /// IR nodes.
19 // TODO(johnniwinther): Implement [ResolutionWorldBuilder].
20 class KernelWorldBuilder extends KernelElementAdapterMixin {
21 CommonElements _commonElements;
22 final DiagnosticReporter reporter;
23
24 DartTypeConverter _typeConverter;
25
26 /// Library environment. Used for fast lookup.
27 KEnv _env;
28
29 /// List of library environments by `KLibrary.libraryIndex`. This is used for
30 /// fast lookup into library classes and members.
31 List<KLibraryEnv> _libraryEnvs = <KLibraryEnv>[];
32
33 Map<ir.Library, KLibrary> _libraryMap = <ir.Library, KLibrary>{};
34 Map<ir.Class, KClass> _classMap = <ir.Class, KClass>{};
35 Map<ir.TypeParameter, KTypeVariable> _typeVariableMap =
36 <ir.TypeParameter, KTypeVariable>{};
37 Map<ir.Member, KConstructor> _constructorMap = <ir.Member, KConstructor>{};
38 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{};
39 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{};
40 Map<ir.TreeNode, KLocalFunction> _localFunctionMap =
41 <ir.TreeNode, KLocalFunction>{};
42
43 KernelWorldBuilder(this.reporter, ir.Program program)
44 : _env = new KEnv(program) {
45 _commonElements = new KernelCommonElements(this);
46 _typeConverter = new DartTypeConverter(this);
47 }
48
49 CommonElements get commonElements => _commonElements;
50
51 LibraryEntity lookupLibrary(Uri uri) {
52 KLibraryEnv libraryEnv = _env.lookupLibrary(uri);
53 return _getLibrary(libraryEnv.library, libraryEnv);
54 }
55
56 KLibrary _getLibrary(ir.Library node, [KLibraryEnv libraryEnv]) {
57 return _libraryMap.putIfAbsent(node, () {
58 _libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(node.importUri));
59 return new KLibrary(_libraryMap.length, node.name, node.fileUri);
60 });
61 }
62
63 ClassEntity lookupClass(KLibrary library, String name) {
64 KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
65 KClassEnv classEnv = libraryEnv.lookupClass(name);
66 return _getClass(classEnv.cls, classEnv);
67 }
68
69 KClass _getClass(ir.Class node, [KClassEnv classEnv]) {
70 return _classMap.putIfAbsent(node, () {
71 return new KClass(node.name);
72 });
73 }
74
75 KTypeVariable _getTypeVariable(ir.TypeParameter node) {
76 return _typeVariableMap.putIfAbsent(node, () {
77 if (node.parent is ir.Class) {
78 ir.Class cls = node.parent;
79 int index = cls.typeParameters.indexOf(node);
80 return new KTypeVariable(_getClass(cls), node.name, index);
81 }
82 if (node.parent is ir.FunctionNode) {
83 ir.FunctionNode func = node.parent;
84 int index = func.typeParameters.indexOf(node);
85 if (func.parent is ir.Constructor) {
86 ir.Constructor constructor = func.parent;
87 ir.Class cls = constructor.enclosingClass;
88 return _getTypeVariable(cls.typeParameters[index]);
89 }
90 if (func.parent is ir.Procedure) {
91 ir.Procedure procedure = func.parent;
92 if (procedure.kind == ir.ProcedureKind.Factory) {
93 ir.Class cls = procedure.enclosingClass;
94 return _getTypeVariable(cls.typeParameters[index]);
95 } else {
96 return new KTypeVariable(_getMethod(procedure), node.name, index);
97 }
98 }
99 }
100 throw new UnsupportedError('Unsupported type parameter type node $node.');
101 });
102 }
103
104 KConstructor _getConstructor(ir.Member node) {
105 return _constructorMap.putIfAbsent(node, () {
106 if (node is ir.Constructor) {
107 return new KGenerativeConstructor(
108 _getClass(node.enclosingClass), getName(node.name));
109 } else {
110 return new KFactoryConstructor(
111 _getClass(node.enclosingClass), getName(node.name));
112 }
113 });
114 }
115
116 KFunction _getMethod(ir.Procedure node) {
117 return _methodMap.putIfAbsent(node, () {
118 KClass enclosingClass =
119 node.enclosingClass != null ? _getClass(node.enclosingClass) : null;
120 Name name = getName(node.name);
121 bool isStatic = node.isStatic;
122 switch (node.kind) {
123 case ir.ProcedureKind.Factory:
124 throw new UnsupportedError("Cannot create method from factory.");
125 case ir.ProcedureKind.Getter:
126 return new KGetter(enclosingClass, name, isStatic: isStatic);
127 case ir.ProcedureKind.Method:
128 case ir.ProcedureKind.Operator:
129 return new KMethod(enclosingClass, name, isStatic: isStatic);
130 case ir.ProcedureKind.Setter:
131 return new KSetter(enclosingClass, getName(node.name).setter,
132 isStatic: isStatic);
133 }
134 });
135 }
136
137 KField _getField(ir.Field node) {
138 return _fieldMap.putIfAbsent(node, () {
139 KClass enclosingClass =
140 node.enclosingClass != null ? _getClass(node.enclosingClass) : null;
141 Name name = getName(node.name);
142 bool isStatic = node.isStatic;
143 return new KField(enclosingClass, name,
144 isStatic: isStatic, isAssignable: node.isMutable);
145 });
146 }
147
148 KLocalFunction _getLocal(ir.TreeNode node) {
149 return _localFunctionMap.putIfAbsent(node, () {
150 MemberEntity memberContext;
151 Entity executableContext;
152 ir.TreeNode parent = node.parent;
153 while (parent != null) {
154 if (parent is ir.Member) {
155 executableContext = memberContext = getMember(parent);
156 break;
157 }
158 if (parent is ir.FunctionDeclaration ||
159 parent is ir.FunctionExpression) {
160 KLocalFunction localFunction = _getLocal(parent);
161 executableContext = localFunction;
162 memberContext = localFunction.memberContext;
163 break;
164 }
165 parent = parent.parent;
166 }
167 String name;
168 if (node is ir.FunctionDeclaration) {
169 name = node.variable.name;
170 }
171 return new KLocalFunction(name, memberContext, executableContext);
172 });
173 }
174
175 @override
176 DartType getDartType(ir.DartType type) => _typeConverter.convert(type);
177
178 @override
179 InterfaceType createInterfaceType(
180 ir.Class cls, List<ir.DartType> typeArguments) {
181 return new InterfaceType(getClass(cls), getDartTypes(typeArguments));
182 }
183
184 @override
185 InterfaceType getInterfaceType(ir.InterfaceType type) =>
186 _typeConverter.convert(type);
187
188 @override
189 List<DartType> getDartTypes(List<ir.DartType> types) {
190 // TODO(johnniwinther): Add the type argument to the list literal when we
191 // no longer use resolution types.
192 List<DartType> list = /*<DartType>*/ [];
193 types.forEach((ir.DartType type) {
194 list.add(getDartType(type));
195 });
196 return list;
197 }
198
199 @override
200 InterfaceType getThisType(ClassEntity cls) {
201 throw new UnimplementedError('KernelWorldBuilder.getThisType');
202 }
203
204 @override
205 InterfaceType getRawType(ClassEntity cls) {
206 throw new UnimplementedError('KernelWorldBuilder.getRawType');
207 }
208
209 @override
210 InterfaceType getInterfaceTypeForJsInterceptorCall(ir.StaticInvocation node) {
211 throw new UnimplementedError('KernelWorldBuilder.getDartType');
212 }
213
214 @override
215 native.NativeBehavior getNativeBehaviorForJsEmbeddedGlobalCall(
216 ir.StaticInvocation node) {
217 throw new UnimplementedError('KernelWorldBuilder.getDartType');
218 }
219
220 @override
221 native.NativeBehavior getNativeBehaviorForJsBuiltinCall(
222 ir.StaticInvocation node) {
223 throw new UnimplementedError('KernelWorldBuilder.getDartType');
224 }
225
226 @override
227 native.NativeBehavior getNativeBehaviorForJsCall(ir.StaticInvocation node) {
228 throw new UnimplementedError('KernelWorldBuilder.getDartType');
229 }
230
231 @override
232 native.NativeBehavior getNativeBehaviorForMethod(ir.Procedure procedure) {
233 throw new UnimplementedError('KernelWorldBuilder.getDartType');
234 }
235
236 @override
237 native.NativeBehavior getNativeBehaviorForFieldStore(ir.Field field) {
238 throw new UnimplementedError('KernelWorldBuilder.getDartType');
239 }
240
241 @override
242 native.NativeBehavior getNativeBehaviorForFieldLoad(ir.Field field) {
243 throw new UnimplementedError('KernelWorldBuilder.getDartType');
244 }
245
246 LibraryEntity getLibrary(ir.Library node) => _getLibrary(node);
247
248 @override
249 Local getLocalFunction(ir.TreeNode node) => _getLocal(node);
250
251 @override
252 ClassEntity getClass(ir.Class node) => _getClass(node);
253
254 @override
255 FieldEntity getField(ir.Field node) => _getField(node);
256
257 TypeVariableEntity getTypeVariable(ir.TypeParameter node) =>
258 _getTypeVariable(node);
259
260 @override
261 FunctionEntity getMethod(ir.Procedure node) => _getMethod(node);
262
263 @override
264 MemberEntity getMember(ir.Member node) {
265 if (node is ir.Field) {
266 return _getField(node);
267 } else if (node is ir.Constructor) {
268 return _getConstructor(node);
269 } else if (node is ir.Procedure) {
270 if (node.kind == ir.ProcedureKind.Factory) {
271 return _getConstructor(node);
272 } else {
273 return _getMethod(node);
274 }
275 }
276 throw new UnsupportedError("Unexpected member: $node");
277 }
278
279 @override
280 FunctionEntity getConstructor(ir.Member node) => _getConstructor(node);
281 }
282
283 /// Environment for fast lookup of program libraries.
284 class KEnv {
285 final ir.Program program;
286
287 Map<Uri, KLibraryEnv> _libraryMap;
288
289 KEnv(this.program);
290
291 /// Return the [KLibraryEnv] for the library with the canonical [uri].
292 KLibraryEnv lookupLibrary(Uri uri) {
293 if (_libraryMap == null) {
294 _libraryMap = <Uri, KLibraryEnv>{};
295 for (ir.Library library in program.libraries) {
296 _libraryMap[library.importUri] = new KLibraryEnv(library);
297 }
298 }
299 return _libraryMap[uri];
300 }
301 }
302
303 /// Environment for fast lookup of library classes and members.
304 // TODO(johnniwinther): Add member lookup.
305 class KLibraryEnv {
306 final ir.Library library;
307
308 Map<String, KClassEnv> _classMap;
309
310 KLibraryEnv(this.library);
311
312 /// Return the [KClassEnv] for the class [name] in [library].
313 KClassEnv lookupClass(String name) {
314 if (_classMap == null) {
315 _classMap = <String, KClassEnv>{};
316 for (ir.Class cls in library.classes) {
317 _classMap[cls.name] = new KClassEnv(cls);
318 }
319 }
320 return _classMap[name];
321 }
322 }
323
324 /// Environment for fast lookup of class members.
325 // TODO(johnniwinther): Add member lookup.
326 class KClassEnv {
327 final ir.Class cls;
328
329 KClassEnv(this.cls);
330 }
331
332 /// [CommonElements] implementation based on [KernelWorldBuilder].
333 class KernelCommonElements extends CommonElementsMixin {
334 final KernelWorldBuilder worldBuilder;
335
336 KernelCommonElements(this.worldBuilder);
337
338 @override
339 LibraryEntity get coreLibrary {
340 return worldBuilder.lookupLibrary(Uris.dart_core);
341 }
342
343 @override
344 InterfaceType createInterfaceType(
345 ClassEntity cls, List<DartType> typeArguments) {
346 return new InterfaceType(cls, typeArguments);
347 }
348
349 @override
350 InterfaceType getRawType(ClassEntity cls) {
351 throw new UnimplementedError('KernelCommonElements.getRawType');
352 }
353
354 @override
355 FunctionEntity findConstructor(ClassEntity cls, String name,
356 {bool required: true}) {
357 throw new UnimplementedError('KernelCommonElements.findConstructor');
358 }
359
360 @override
361 MemberEntity findClassMember(ClassEntity cls, String name,
362 {bool required: true}) {
363 throw new UnimplementedError('KernelCommonElements.findClassMember');
364 }
365
366 @override
367 MemberEntity findLibraryMember(LibraryEntity library, String name,
368 {bool required: true}) {
369 throw new UnimplementedError('KernelCommonElements.findLibraryMember');
370 }
371
372 @override
373 ClassEntity findClass(LibraryEntity library, String name,
374 {bool required: true}) {
375 return worldBuilder.lookupClass(library, name);
376 }
377
378 @override
379 DynamicType get dynamicType => const DynamicType();
380
381 @override
382 ClassEntity get nativeAnnotationClass {
383 throw new UnimplementedError('KernelCommonElements.nativeAnnotationClass');
384 }
385
386 @override
387 ClassEntity get patchAnnotationClass {
388 throw new UnimplementedError('KernelCommonElements.patchAnnotationClass');
389 }
390
391 @override
392 LibraryEntity get typedDataLibrary {
393 throw new UnimplementedError('KernelCommonElements.typedDataLibrary');
394 }
395
396 @override
397 LibraryEntity get mirrorsLibrary {
398 throw new UnimplementedError('KernelCommonElements.mirrorsLibrary');
399 }
400
401 @override
402 LibraryEntity get asyncLibrary {
403 throw new UnimplementedError('KernelCommonElements.asyncLibrary');
404 }
405 }
406
407 /// Visitor that converts kernel dart types into [DartType].
408 class DartTypeConverter extends ir.DartTypeVisitor<DartType> {
409 final KernelWorldBuilder elementAdapter;
410 bool topLevel = true;
411
412 DartTypeConverter(this.elementAdapter);
413
414 DartType convert(ir.DartType type) {
415 topLevel = true;
416 return type.accept(this);
417 }
418
419 /// Visit a inner type.
420 DartType visitType(ir.DartType type) {
421 topLevel = false;
422 return type.accept(this);
423 }
424
425 List<DartType> visitTypes(List<ir.DartType> types) {
426 topLevel = false;
427 return new List.generate(
428 types.length, (int index) => types[index].accept(this));
429 }
430
431 @override
432 DartType visitTypeParameterType(ir.TypeParameterType node) {
433 return new TypeVariableType(elementAdapter.getTypeVariable(node.parameter));
434 }
435
436 @override
437 DartType visitFunctionType(ir.FunctionType node) {
438 return new FunctionType(
439 visitType(node.returnType),
440 visitTypes(node.positionalParameters
441 .take(node.requiredParameterCount)
442 .toList()),
443 visitTypes(node.positionalParameters
444 .skip(node.requiredParameterCount)
445 .toList()),
446 node.namedParameters.map((n) => n.name).toList(),
447 node.namedParameters.map((n) => visitType(n.type)).toList());
448 }
449
450 @override
451 DartType visitInterfaceType(ir.InterfaceType node) {
452 ClassEntity cls = elementAdapter.getClass(node.classNode);
453 return new InterfaceType(cls, visitTypes(node.typeArguments));
454 }
455
456 @override
457 DartType visitVoidType(ir.VoidType node) {
458 return const VoidType();
459 }
460
461 @override
462 DartType visitDynamicType(ir.DynamicType node) {
463 return const DynamicType();
464 }
465
466 @override
467 DartType visitInvalidType(ir.InvalidType node) {
468 if (topLevel) {
469 throw new UnimplementedError(
470 "Outermost invalid types not currently supported");
471 }
472 // Nested invalid types are treated as `dynamic`.
473 return const DynamicType();
474 }
475 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/kernel/elements.dart ('k') | pkg/compiler/lib/src/ssa/kernel_impact.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698