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

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

Issue 2829033002: Rename KernelWorldBuilder to KernelElementBuilder (Closed)
Patch Set: Created 3 years, 8 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 | « no previous file | pkg/compiler/lib/src/kernel/native_class_resolver.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 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.world_builder; 5 library dart2js.kernel.element_builder;
6 6
7 import 'package:kernel/ast.dart' as ir; 7 import 'package:kernel/ast.dart' as ir;
8 8
9 import '../common.dart'; 9 import '../common.dart';
10 import '../common/resolution.dart'; 10 import '../common/resolution.dart';
11 import '../compile_time_constants.dart'; 11 import '../compile_time_constants.dart';
12 import '../constants/constant_system.dart'; 12 import '../constants/constant_system.dart';
13 import '../constants/constructors.dart'; 13 import '../constants/constructors.dart';
14 import '../constants/evaluation.dart'; 14 import '../constants/evaluation.dart';
15 import '../constants/expressions.dart'; 15 import '../constants/expressions.dart';
16 import '../constants/values.dart'; 16 import '../constants/values.dart';
17 import '../common_elements.dart'; 17 import '../common_elements.dart';
18 import '../elements/elements.dart'; 18 import '../elements/elements.dart';
19 import '../elements/entities.dart'; 19 import '../elements/entities.dart';
20 import '../elements/types.dart'; 20 import '../elements/types.dart';
21 import '../js_backend/constant_system_javascript.dart'; 21 import '../js_backend/constant_system_javascript.dart';
22 import '../js_backend/native_data.dart' show NativeData; 22 import '../js_backend/native_data.dart' show NativeData;
23 import '../js_backend/no_such_method_registry.dart'; 23 import '../js_backend/no_such_method_registry.dart';
24 import '../native/native.dart' as native; 24 import '../native/native.dart' as native;
25 import '../native/resolver.dart'; 25 import '../native/resolver.dart';
26 import '../ssa/kernel_impact.dart'; 26 import '../ssa/kernel_impact.dart';
27 import '../universe/call_structure.dart'; 27 import '../universe/call_structure.dart';
28 import 'element_adapter.dart'; 28 import 'element_adapter.dart';
29 import 'elements.dart'; 29 import 'elements.dart';
30 30
31 part 'native_class_resolver.dart'; 31 part 'native_class_resolver.dart';
32 part 'no_such_method_resolver.dart'; 32 part 'no_such_method_resolver.dart';
33 33
34 /// World builder used for creating elements and types corresponding to Kernel 34 /// Element builder used for creating elements and types corresponding to Kernel
35 /// IR nodes. 35 /// IR nodes.
36 // TODO(johnniwinther): Implement [ResolutionWorldBuilder]. 36 class KernelElementBuilder extends KernelElementAdapterMixin {
Siggi Cherem (dart-lang) 2017/04/20 22:39:39 as we discuss in the meeting, I think we can drop
Johnni Winther 2017/04/24 15:24:01 Changed to KernelToElementMap
37 class KernelWorldBuilder extends KernelElementAdapterMixin {
38 CommonElements _commonElements; 37 CommonElements _commonElements;
39 native.BehaviorBuilder _nativeBehaviorBuilder; 38 native.BehaviorBuilder _nativeBehaviorBuilder;
40 final DiagnosticReporter reporter; 39 final DiagnosticReporter reporter;
41 ElementEnvironment _elementEnvironment; 40 ElementEnvironment _elementEnvironment;
42 DartTypeConverter _typeConverter; 41 DartTypeConverter _typeConverter;
43 42
44 /// Library environment. Used for fast lookup. 43 /// Library environment. Used for fast lookup.
45 KEnv _env; 44 _KEnv _env;
46 45
47 /// List of library environments by `KLibrary.libraryIndex`. This is used for 46 /// List of library environments by `KLibrary.libraryIndex`. This is used for
48 /// fast lookup into library classes and members. 47 /// fast lookup into library classes and members.
49 List<KLibraryEnv> _libraryEnvs = <KLibraryEnv>[]; 48 List<_KLibraryEnv> _libraryEnvs = <_KLibraryEnv>[];
50 49
51 /// List of class environments by `KClass.classIndex`. This is used for 50 /// List of class environments by `KClass.classIndex`. This is used for
52 /// fast lookup into class members. 51 /// fast lookup into class members.
53 List<KClassEnv> _classEnvs = <KClassEnv>[]; 52 List<_KClassEnv> _classEnvs = <_KClassEnv>[];
54 53
55 Map<ir.Library, KLibrary> _libraryMap = <ir.Library, KLibrary>{}; 54 Map<ir.Library, KLibrary> _libraryMap = <ir.Library, KLibrary>{};
56 Map<ir.Class, KClass> _classMap = <ir.Class, KClass>{}; 55 Map<ir.Class, KClass> _classMap = <ir.Class, KClass>{};
57 Map<ir.TypeParameter, KTypeVariable> _typeVariableMap = 56 Map<ir.TypeParameter, KTypeVariable> _typeVariableMap =
58 <ir.TypeParameter, KTypeVariable>{}; 57 <ir.TypeParameter, KTypeVariable>{};
59 58
60 // TODO(johnniwinther): Change this to a list of 'KMemberData' class if we 59 // TODO(johnniwinther): Change this to a list of 'KMemberData' class if we
61 // need more data for members. 60 // need more data for members.
62 List<ir.Member> _memberList = <ir.Member>[]; 61 List<ir.Member> _memberList = <ir.Member>[];
63 62
64 Map<ir.Member, KConstructor> _constructorMap = <ir.Member, KConstructor>{}; 63 Map<ir.Member, KConstructor> _constructorMap = <ir.Member, KConstructor>{};
65 Map<KConstructor, ConstantConstructor> _constructorConstantMap = 64 Map<KConstructor, ConstantConstructor> _constructorConstantMap =
66 <KConstructor, ConstantConstructor>{}; 65 <KConstructor, ConstantConstructor>{};
67 66
68 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{}; 67 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{};
69 68
70 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{}; 69 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{};
71 Map<KField, ConstantExpression> _fieldConstantMap = 70 Map<KField, ConstantExpression> _fieldConstantMap =
72 <KField, ConstantExpression>{}; 71 <KField, ConstantExpression>{};
73 72
74 Map<ir.TreeNode, KLocalFunction> _localFunctionMap = 73 Map<ir.TreeNode, KLocalFunction> _localFunctionMap =
75 <ir.TreeNode, KLocalFunction>{}; 74 <ir.TreeNode, KLocalFunction>{};
76 75
77 KernelWorldBuilder(this.reporter, ir.Program program) 76 KernelElementBuilder(this.reporter, ir.Program program)
78 : _env = new KEnv(program) { 77 : _env = new _KEnv(program) {
79 _elementEnvironment = new KernelElementEnvironment(this); 78 _elementEnvironment = new KernelElementEnvironment(this);
80 _commonElements = new CommonElements(_elementEnvironment); 79 _commonElements = new CommonElements(_elementEnvironment);
81 ConstantEnvironment constants = new KernelConstantEnvironment(this); 80 ConstantEnvironment constants = new KernelConstantEnvironment(this);
82 _nativeBehaviorBuilder = 81 _nativeBehaviorBuilder =
83 new KernelBehaviorBuilder(_commonElements, constants); 82 new KernelBehaviorBuilder(_commonElements, constants);
84 _typeConverter = new DartTypeConverter(this); 83 _typeConverter = new DartTypeConverter(this);
85 } 84 }
86 85
87 KMethod get _mainFunction { 86 KMethod get _mainFunction {
88 return _env.program.mainMethod != null 87 return _env.program.mainMethod != null
(...skipping 10 matching lines...) Expand all
99 @override 98 @override
100 CommonElements get commonElements => _commonElements; 99 CommonElements get commonElements => _commonElements;
101 100
102 @override 101 @override
103 ElementEnvironment get elementEnvironment => _elementEnvironment; 102 ElementEnvironment get elementEnvironment => _elementEnvironment;
104 103
105 @override 104 @override
106 native.BehaviorBuilder get nativeBehaviorBuilder => _nativeBehaviorBuilder; 105 native.BehaviorBuilder get nativeBehaviorBuilder => _nativeBehaviorBuilder;
107 106
108 LibraryEntity lookupLibrary(Uri uri) { 107 LibraryEntity lookupLibrary(Uri uri) {
109 KLibraryEnv libraryEnv = _env.lookupLibrary(uri); 108 _KLibraryEnv libraryEnv = _env.lookupLibrary(uri);
110 return _getLibrary(libraryEnv.library, libraryEnv); 109 return _getLibrary(libraryEnv.library, libraryEnv);
111 } 110 }
112 111
113 KLibrary _getLibrary(ir.Library node, [KLibraryEnv libraryEnv]) { 112 KLibrary _getLibrary(ir.Library node, [_KLibraryEnv libraryEnv]) {
114 return _libraryMap.putIfAbsent(node, () { 113 return _libraryMap.putIfAbsent(node, () {
115 Uri canonicalUri = node.importUri; 114 Uri canonicalUri = node.importUri;
116 _libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(canonicalUri)); 115 _libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(canonicalUri));
117 String name = node.name; 116 String name = node.name;
118 if (name == null) { 117 if (name == null) {
119 // Use the file name as script name. 118 // Use the file name as script name.
120 String path = canonicalUri.path; 119 String path = canonicalUri.path;
121 name = path.substring(path.lastIndexOf('/') + 1); 120 name = path.substring(path.lastIndexOf('/') + 1);
122 } 121 }
123 return new KLibrary(_libraryMap.length, name, canonicalUri); 122 return new KLibrary(_libraryMap.length, name, canonicalUri);
124 }); 123 });
125 } 124 }
126 125
127 MemberEntity lookupLibraryMember(KLibrary library, String name, 126 MemberEntity lookupLibraryMember(KLibrary library, String name,
128 {bool setter: false}) { 127 {bool setter: false}) {
129 KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex]; 128 _KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
130 ir.Member member = libraryEnv.lookupMember(name, setter: setter); 129 ir.Member member = libraryEnv.lookupMember(name, setter: setter);
131 return member != null ? getMember(member) : null; 130 return member != null ? getMember(member) : null;
132 } 131 }
133 132
134 ClassEntity lookupClass(KLibrary library, String name) { 133 ClassEntity lookupClass(KLibrary library, String name) {
135 KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex]; 134 _KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
136 KClassEnv classEnv = libraryEnv.lookupClass(name); 135 _KClassEnv classEnv = libraryEnv.lookupClass(name);
137 if (classEnv != null) { 136 if (classEnv != null) {
138 return _getClass(classEnv.cls, classEnv); 137 return _getClass(classEnv.cls, classEnv);
139 } 138 }
140 return null; 139 return null;
141 } 140 }
142 141
143 MemberEntity lookupClassMember(KClass cls, String name, 142 MemberEntity lookupClassMember(KClass cls, String name,
144 {bool setter: false}) { 143 {bool setter: false}) {
145 KClassEnv classEnv = _classEnvs[cls.classIndex]; 144 _KClassEnv classEnv = _classEnvs[cls.classIndex];
146 ir.Member member = classEnv.lookupMember(name, setter: setter); 145 ir.Member member = classEnv.lookupMember(name, setter: setter);
147 return member != null ? getMember(member) : null; 146 return member != null ? getMember(member) : null;
148 } 147 }
149 148
150 ConstructorEntity lookupConstructor(KClass cls, String name) { 149 ConstructorEntity lookupConstructor(KClass cls, String name) {
151 KClassEnv classEnv = _classEnvs[cls.classIndex]; 150 _KClassEnv classEnv = _classEnvs[cls.classIndex];
152 ir.Member member = classEnv.lookupConstructor(name); 151 ir.Member member = classEnv.lookupConstructor(name);
153 return member != null ? getConstructor(member) : null; 152 return member != null ? getConstructor(member) : null;
154 } 153 }
155 154
156 KClass _getClass(ir.Class node, [KClassEnv classEnv]) { 155 KClass _getClass(ir.Class node, [_KClassEnv classEnv]) {
157 return _classMap.putIfAbsent(node, () { 156 return _classMap.putIfAbsent(node, () {
158 KLibrary library = _getLibrary(node.enclosingLibrary); 157 KLibrary library = _getLibrary(node.enclosingLibrary);
159 if (classEnv == null) { 158 if (classEnv == null) {
160 classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name); 159 classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name);
161 } 160 }
162 _classEnvs.add(classEnv); 161 _classEnvs.add(classEnv);
163 return new KClass(library, _classMap.length, node.name, 162 return new KClass(library, _classMap.length, node.name,
164 isAbstract: node.isAbstract); 163 isAbstract: node.isAbstract);
165 }); 164 });
166 } 165 }
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 List<DartType> getDartTypes(List<ir.DartType> types) { 345 List<DartType> getDartTypes(List<ir.DartType> types) {
347 // TODO(johnniwinther): Add the type argument to the list literal when we 346 // TODO(johnniwinther): Add the type argument to the list literal when we
348 // no longer use resolution types. 347 // no longer use resolution types.
349 List<DartType> list = /*<DartType>*/ []; 348 List<DartType> list = /*<DartType>*/ [];
350 types.forEach((ir.DartType type) { 349 types.forEach((ir.DartType type) {
351 list.add(getDartType(type)); 350 list.add(getDartType(type));
352 }); 351 });
353 return list; 352 return list;
354 } 353 }
355 354
356 void _ensureThisAndRawType(KClass cls, KClassEnv env) { 355 void _ensureThisAndRawType(KClass cls, _KClassEnv env) {
357 if (env.thisType == null) { 356 if (env.thisType == null) {
358 ir.Class node = env.cls; 357 ir.Class node = env.cls;
359 // TODO(johnniwinther): Add the type argument to the list literal when we 358 // TODO(johnniwinther): Add the type argument to the list literal when we
360 // no longer use resolution types. 359 // no longer use resolution types.
361 if (node.typeParameters.isEmpty) { 360 if (node.typeParameters.isEmpty) {
362 env.thisType = 361 env.thisType =
363 env.rawType = new InterfaceType(cls, const/*<DartType>*/ []); 362 env.rawType = new InterfaceType(cls, const/*<DartType>*/ []);
364 } else { 363 } else {
365 env.thisType = new InterfaceType( 364 env.thisType = new InterfaceType(
366 cls, 365 cls,
367 new List/*<DartType>*/ .generate(node.typeParameters.length, 366 new List/*<DartType>*/ .generate(node.typeParameters.length,
368 (int index) { 367 (int index) {
369 return new TypeVariableType( 368 return new TypeVariableType(
370 _getTypeVariable(node.typeParameters[index])); 369 _getTypeVariable(node.typeParameters[index]));
371 })); 370 }));
372 env.rawType = new InterfaceType( 371 env.rawType = new InterfaceType(
373 cls, 372 cls,
374 new List/*<DartType>*/ .filled( 373 new List/*<DartType>*/ .filled(
375 node.typeParameters.length, const DynamicType())); 374 node.typeParameters.length, const DynamicType()));
376 } 375 }
377 } 376 }
378 } 377 }
379 378
380 InterfaceType _getThisType(KClass cls) { 379 InterfaceType _getThisType(KClass cls) {
381 KClassEnv env = _classEnvs[cls.classIndex]; 380 _KClassEnv env = _classEnvs[cls.classIndex];
382 _ensureThisAndRawType(cls, env); 381 _ensureThisAndRawType(cls, env);
383 return env.thisType; 382 return env.thisType;
384 } 383 }
385 384
386 InterfaceType _getRawType(KClass cls) { 385 InterfaceType _getRawType(KClass cls) {
387 KClassEnv env = _classEnvs[cls.classIndex]; 386 _KClassEnv env = _classEnvs[cls.classIndex];
388 _ensureThisAndRawType(cls, env); 387 _ensureThisAndRawType(cls, env);
389 return env.rawType; 388 return env.rawType;
390 } 389 }
391 390
392 void _ensureSupertypes(KClass cls, KClassEnv env) { 391 void _ensureSupertypes(KClass cls, _KClassEnv env) {
393 if (env.supertypes == null) { 392 if (env.supertypes == null) {
394 _ensureThisAndRawType(cls, env); 393 _ensureThisAndRawType(cls, env);
395 394
396 ir.Class node = env.cls; 395 ir.Class node = env.cls;
397 396
398 Set<InterfaceType> supertypes = new Set<InterfaceType>(); 397 Set<InterfaceType> supertypes = new Set<InterfaceType>();
399 398
400 InterfaceType addSupertype(ir.Supertype node) { 399 InterfaceType addSupertype(ir.Supertype node) {
401 if (node == null) return null; 400 if (node == null) return null;
402 InterfaceType type = _typeConverter.visitSupertype(node); 401 InterfaceType type = _typeConverter.visitSupertype(node);
403 KClass superclass = type.element; 402 KClass superclass = type.element;
404 KClassEnv env = _classEnvs[superclass.classIndex]; 403 _KClassEnv env = _classEnvs[superclass.classIndex];
405 _ensureSupertypes(superclass, env); 404 _ensureSupertypes(superclass, env);
406 for (InterfaceType supertype in env.supertypes) { 405 for (InterfaceType supertype in env.supertypes) {
407 supertypes.add( 406 supertypes.add(
408 supertype.subst(type.typeArguments, env.thisType.typeArguments)); 407 supertype.subst(type.typeArguments, env.thisType.typeArguments));
409 } 408 }
410 return type; 409 return type;
411 } 410 }
412 411
413 env.supertype = addSupertype(node.supertype); 412 env.supertype = addSupertype(node.supertype);
414 addSupertype(node.mixedInType); 413 addSupertype(node.mixedInType);
415 node.implementedTypes.forEach(addSupertype); 414 node.implementedTypes.forEach(addSupertype);
416 415
417 env.supertypes = supertypes.toList(); 416 env.supertypes = supertypes.toList();
418 } 417 }
419 } 418 }
420 419
421 InterfaceType _getSuperType(KClass cls) { 420 InterfaceType _getSuperType(KClass cls) {
422 KClassEnv env = _classEnvs[cls.classIndex]; 421 _KClassEnv env = _classEnvs[cls.classIndex];
423 _ensureSupertypes(cls, env); 422 _ensureSupertypes(cls, env);
424 return env.supertype; 423 return env.supertype;
425 } 424 }
426 425
427 void _forEachSupertype(KClass cls, void f(InterfaceType supertype)) { 426 void _forEachSupertype(KClass cls, void f(InterfaceType supertype)) {
428 KClassEnv env = _classEnvs[cls.classIndex]; 427 _KClassEnv env = _classEnvs[cls.classIndex];
429 _ensureSupertypes(cls, env); 428 _ensureSupertypes(cls, env);
430 env.supertypes.forEach(f); 429 env.supertypes.forEach(f);
431 } 430 }
432 431
433 @override 432 @override
434 FunctionType getFunctionType(ir.FunctionNode node) { 433 FunctionType getFunctionType(ir.FunctionNode node) {
435 DartType returnType = getDartType(node.returnType); 434 DartType returnType = getDartType(node.returnType);
436 List<DartType> parameterTypes = /*<DartType>*/ []; 435 List<DartType> parameterTypes = /*<DartType>*/ [];
437 List<DartType> optionalParameterTypes = /*<DartType>*/ []; 436 List<DartType> optionalParameterTypes = /*<DartType>*/ [];
438 for (ir.VariableDeclaration variable in node.positionalParameters) { 437 for (ir.VariableDeclaration variable in node.positionalParameters) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 }); 520 });
522 } 521 }
523 522
524 ResolutionImpact computeWorldImpact(KMember member) { 523 ResolutionImpact computeWorldImpact(KMember member) {
525 ir.Member node = _memberList[member.memberIndex]; 524 ir.Member node = _memberList[member.memberIndex];
526 return buildKernelImpact(node, this); 525 return buildKernelImpact(node, this);
527 } 526 }
528 } 527 }
529 528
530 /// Environment for fast lookup of program libraries. 529 /// Environment for fast lookup of program libraries.
531 class KEnv { 530 class _KEnv {
532 final ir.Program program; 531 final ir.Program program;
533 532
534 Map<Uri, KLibraryEnv> _libraryMap; 533 Map<Uri, _KLibraryEnv> _libraryMap;
535 534
536 KEnv(this.program); 535 _KEnv(this.program);
537 536
538 /// Return the [KLibraryEnv] for the library with the canonical [uri]. 537 /// Return the [_KLibraryEnv] for the library with the canonical [uri].
539 KLibraryEnv lookupLibrary(Uri uri) { 538 _KLibraryEnv lookupLibrary(Uri uri) {
540 if (_libraryMap == null) { 539 if (_libraryMap == null) {
541 _libraryMap = <Uri, KLibraryEnv>{}; 540 _libraryMap = <Uri, _KLibraryEnv>{};
542 for (ir.Library library in program.libraries) { 541 for (ir.Library library in program.libraries) {
543 _libraryMap[library.importUri] = new KLibraryEnv(library); 542 _libraryMap[library.importUri] = new _KLibraryEnv(library);
544 } 543 }
545 } 544 }
546 return _libraryMap[uri]; 545 return _libraryMap[uri];
547 } 546 }
548 } 547 }
549 548
550 /// Environment for fast lookup of library classes and members. 549 /// Environment for fast lookup of library classes and members.
551 // TODO(johnniwinther): Add member lookup. 550 class _KLibraryEnv {
552 class KLibraryEnv {
553 final ir.Library library; 551 final ir.Library library;
554 552
555 Map<String, KClassEnv> _classMap; 553 Map<String, _KClassEnv> _classMap;
556 Map<String, ir.Member> _memberMap; 554 Map<String, ir.Member> _memberMap;
557 555
558 KLibraryEnv(this.library); 556 _KLibraryEnv(this.library);
559 557
560 /// Return the [KClassEnv] for the class [name] in [library]. 558 /// Return the [_KClassEnv] for the class [name] in [library].
561 KClassEnv lookupClass(String name) { 559 _KClassEnv lookupClass(String name) {
562 if (_classMap == null) { 560 if (_classMap == null) {
563 _classMap = <String, KClassEnv>{}; 561 _classMap = <String, _KClassEnv>{};
564 for (ir.Class cls in library.classes) { 562 for (ir.Class cls in library.classes) {
565 _classMap[cls.name] = new KClassEnv(cls); 563 _classMap[cls.name] = new _KClassEnv(cls);
566 } 564 }
567 } 565 }
568 return _classMap[name]; 566 return _classMap[name];
569 } 567 }
570 568
571 /// Return the [ir.Member] for the member [name] in [library]. 569 /// Return the [ir.Member] for the member [name] in [library].
572 ir.Member lookupMember(String name, {bool setter: false}) { 570 ir.Member lookupMember(String name, {bool setter: false}) {
573 if (_memberMap == null) { 571 if (_memberMap == null) {
574 _memberMap = <String, ir.Member>{}; 572 _memberMap = <String, ir.Member>{};
575 for (ir.Member member in library.members) { 573 for (ir.Member member in library.members) {
576 // TODO(johnniwinther): Support setter vs. getter. 574 // TODO(johnniwinther): Support setter vs. getter.
577 _memberMap[member.name.name] = member; 575 _memberMap[member.name.name] = member;
578 } 576 }
579 } 577 }
580 return _memberMap[name]; 578 return _memberMap[name];
581 } 579 }
582 } 580 }
583 581
584 /// Environment for fast lookup of class members. 582 /// Environment for fast lookup of class members.
585 // TODO(johnniwinther): Add member lookup. 583 class _KClassEnv {
586 class KClassEnv {
587 final ir.Class cls; 584 final ir.Class cls;
588 585
589 InterfaceType thisType; 586 InterfaceType thisType;
590 InterfaceType rawType; 587 InterfaceType rawType;
591 InterfaceType supertype; 588 InterfaceType supertype;
592 List<InterfaceType> supertypes; 589 List<InterfaceType> supertypes;
593 590
594 Map<String, ir.Member> _constructorMap; 591 Map<String, ir.Member> _constructorMap;
595 Map<String, ir.Member> _memberMap; 592 Map<String, ir.Member> _memberMap;
596 593
597 KClassEnv(this.cls); 594 _KClassEnv(this.cls);
598 595
599 void _ensureMaps() { 596 void _ensureMaps() {
600 if (_memberMap == null) { 597 if (_memberMap == null) {
601 _memberMap = <String, ir.Member>{}; 598 _memberMap = <String, ir.Member>{};
602 _constructorMap = <String, ir.Member>{}; 599 _constructorMap = <String, ir.Member>{};
603 for (ir.Member member in cls.members) { 600 for (ir.Member member in cls.members) {
604 if (member is ir.Procedure && member.kind == ir.ProcedureKind.Factory) { 601 if (member is ir.Procedure && member.kind == ir.ProcedureKind.Factory) {
605 _constructorMap[member.name.name] = member; 602 _constructorMap[member.name.name] = member;
606 } else { 603 } else {
607 // TODO(johnniwinther): Support setter vs. getter. 604 // TODO(johnniwinther): Support setter vs. getter.
(...skipping 13 matching lines...) Expand all
621 } 618 }
622 619
623 /// Return the [ir.Member] for the member [name] in [library]. 620 /// Return the [ir.Member] for the member [name] in [library].
624 ir.Member lookupConstructor(String name, {bool setter: false}) { 621 ir.Member lookupConstructor(String name, {bool setter: false}) {
625 _ensureMaps(); 622 _ensureMaps();
626 return _constructorMap[name]; 623 return _constructorMap[name];
627 } 624 }
628 } 625 }
629 626
630 class KernelElementEnvironment implements ElementEnvironment { 627 class KernelElementEnvironment implements ElementEnvironment {
631 final KernelWorldBuilder worldBuilder; 628 final KernelElementBuilder elementBuilder;
632 629
633 KernelElementEnvironment(this.worldBuilder); 630 KernelElementEnvironment(this.elementBuilder);
634 631
635 @override 632 @override
636 DartType get dynamicType => const DynamicType(); 633 DartType get dynamicType => const DynamicType();
637 634
638 @override 635 @override
639 LibraryEntity get mainLibrary => worldBuilder._mainLibrary; 636 LibraryEntity get mainLibrary => elementBuilder._mainLibrary;
640 637
641 @override 638 @override
642 FunctionEntity get mainFunction => worldBuilder._mainFunction; 639 FunctionEntity get mainFunction => elementBuilder._mainFunction;
643 640
644 @override 641 @override
645 Iterable<LibraryEntity> get libraries => worldBuilder._libraryMap.values; 642 Iterable<LibraryEntity> get libraries => elementBuilder._libraryMap.values;
646 643
647 @override 644 @override
648 InterfaceType getThisType(ClassEntity cls) { 645 InterfaceType getThisType(ClassEntity cls) {
649 return worldBuilder._getThisType(cls); 646 return elementBuilder._getThisType(cls);
650 } 647 }
651 648
652 @override 649 @override
653 InterfaceType getRawType(ClassEntity cls) { 650 InterfaceType getRawType(ClassEntity cls) {
654 return worldBuilder._getRawType(cls); 651 return elementBuilder._getRawType(cls);
655 } 652 }
656 653
657 @override 654 @override
658 DartType getTypeVariableBound(TypeVariableEntity typeVariable) { 655 DartType getTypeVariableBound(TypeVariableEntity typeVariable) {
659 throw new UnimplementedError( 656 throw new UnimplementedError(
660 'KernelElementEnvironment.getTypeVariableBound'); 657 'KernelElementEnvironment.getTypeVariableBound');
661 } 658 }
662 659
663 @override 660 @override
664 InterfaceType createInterfaceType( 661 InterfaceType createInterfaceType(
(...skipping 16 matching lines...) Expand all
681 FunctionType getLocalFunctionType(KLocalFunction function) { 678 FunctionType getLocalFunctionType(KLocalFunction function) {
682 return function.functionType; 679 return function.functionType;
683 } 680 }
684 681
685 @override 682 @override
686 DartType getUnaliasedType(DartType type) => type; 683 DartType getUnaliasedType(DartType type) => type;
687 684
688 @override 685 @override
689 ConstructorEntity lookupConstructor(ClassEntity cls, String name, 686 ConstructorEntity lookupConstructor(ClassEntity cls, String name,
690 {bool required: false}) { 687 {bool required: false}) {
691 ConstructorEntity constructor = worldBuilder.lookupConstructor(cls, name); 688 ConstructorEntity constructor = elementBuilder.lookupConstructor(cls, name);
692 if (constructor == null && required) { 689 if (constructor == null && required) {
693 throw new SpannableAssertionFailure( 690 throw new SpannableAssertionFailure(
694 CURRENT_ELEMENT_SPANNABLE, 691 CURRENT_ELEMENT_SPANNABLE,
695 "The constructor '$name' was not found in class '${cls.name}' " 692 "The constructor '$name' was not found in class '${cls.name}' "
696 "in library ${cls.library.canonicalUri}."); 693 "in library ${cls.library.canonicalUri}.");
697 } 694 }
698 return constructor; 695 return constructor;
699 } 696 }
700 697
701 @override 698 @override
702 MemberEntity lookupClassMember(ClassEntity cls, String name, 699 MemberEntity lookupClassMember(ClassEntity cls, String name,
703 {bool setter: false, bool required: false}) { 700 {bool setter: false, bool required: false}) {
704 MemberEntity member = 701 MemberEntity member =
705 worldBuilder.lookupClassMember(cls, name, setter: setter); 702 elementBuilder.lookupClassMember(cls, name, setter: setter);
706 if (member == null && required) { 703 if (member == null && required) {
707 throw new SpannableAssertionFailure(CURRENT_ELEMENT_SPANNABLE, 704 throw new SpannableAssertionFailure(CURRENT_ELEMENT_SPANNABLE,
708 "The member '$name' was not found in ${cls.name}."); 705 "The member '$name' was not found in ${cls.name}.");
709 } 706 }
710 return member; 707 return member;
711 } 708 }
712 709
713 @override 710 @override
714 ClassEntity getSuperClass(ClassEntity cls) { 711 ClassEntity getSuperClass(ClassEntity cls) {
715 return worldBuilder._getSuperType(cls)?.element; 712 return elementBuilder._getSuperType(cls)?.element;
716 } 713 }
717 714
718 @override 715 @override
719 void forEachSupertype(ClassEntity cls, void f(InterfaceType supertype)) { 716 void forEachSupertype(ClassEntity cls, void f(InterfaceType supertype)) {
720 worldBuilder._forEachSupertype(cls, f); 717 elementBuilder._forEachSupertype(cls, f);
721 } 718 }
722 719
723 @override 720 @override
724 void forEachMixin(ClassEntity cls, void f(ClassEntity mixin)) { 721 void forEachMixin(ClassEntity cls, void f(ClassEntity mixin)) {
725 throw new UnimplementedError('KernelElementEnvironment.forEachMixin'); 722 throw new UnimplementedError('KernelElementEnvironment.forEachMixin');
726 } 723 }
727 724
728 @override 725 @override
729 void forEachClassMember( 726 void forEachClassMember(
730 ClassEntity cls, void f(ClassEntity declarer, MemberEntity member)) { 727 ClassEntity cls, void f(ClassEntity declarer, MemberEntity member)) {
731 // TODO(johnniwinther): Implement this. 728 // TODO(johnniwinther): Implement this.
732 } 729 }
733 730
734 @override 731 @override
735 MemberEntity lookupLibraryMember(LibraryEntity library, String name, 732 MemberEntity lookupLibraryMember(LibraryEntity library, String name,
736 {bool setter: false, bool required: false}) { 733 {bool setter: false, bool required: false}) {
737 MemberEntity member = 734 MemberEntity member =
738 worldBuilder.lookupLibraryMember(library, name, setter: setter); 735 elementBuilder.lookupLibraryMember(library, name, setter: setter);
739 if (member == null && required) { 736 if (member == null && required) {
740 throw new SpannableAssertionFailure(CURRENT_ELEMENT_SPANNABLE, 737 throw new SpannableAssertionFailure(CURRENT_ELEMENT_SPANNABLE,
741 "The member '${name}' was not found in library '${library.name}'."); 738 "The member '${name}' was not found in library '${library.name}'.");
742 } 739 }
743 return member; 740 return member;
744 } 741 }
745 742
746 @override 743 @override
747 ClassEntity lookupClass(LibraryEntity library, String name, 744 ClassEntity lookupClass(LibraryEntity library, String name,
748 {bool required: false}) { 745 {bool required: false}) {
749 ClassEntity cls = worldBuilder.lookupClass(library, name); 746 ClassEntity cls = elementBuilder.lookupClass(library, name);
750 if (cls == null && required) { 747 if (cls == null && required) {
751 throw new SpannableAssertionFailure(CURRENT_ELEMENT_SPANNABLE, 748 throw new SpannableAssertionFailure(CURRENT_ELEMENT_SPANNABLE,
752 "The class '$name' was not found in library '${library.name}'."); 749 "The class '$name' was not found in library '${library.name}'.");
753 } 750 }
754 return cls; 751 return cls;
755 } 752 }
756 753
757 @override 754 @override
758 LibraryEntity lookupLibrary(Uri uri, {bool required: false}) { 755 LibraryEntity lookupLibrary(Uri uri, {bool required: false}) {
759 LibraryEntity library = worldBuilder.lookupLibrary(uri); 756 LibraryEntity library = elementBuilder.lookupLibrary(uri);
760 if (library == null && required) { 757 if (library == null && required) {
761 throw new SpannableAssertionFailure( 758 throw new SpannableAssertionFailure(
762 CURRENT_ELEMENT_SPANNABLE, "The library '$uri' was not found."); 759 CURRENT_ELEMENT_SPANNABLE, "The library '$uri' was not found.");
763 } 760 }
764 return library; 761 return library;
765 } 762 }
766 763
767 @override 764 @override
768 CallStructure getCallStructure(KFunction function) { 765 CallStructure getCallStructure(KFunction function) {
769 ir.Member member = worldBuilder._memberList[function.memberIndex]; 766 ir.Member member = elementBuilder._memberList[function.memberIndex];
770 ir.FunctionNode functionNode; 767 ir.FunctionNode functionNode;
771 if (member is ir.Procedure) { 768 if (member is ir.Procedure) {
772 functionNode = member.function; 769 functionNode = member.function;
773 } else if (member is ir.Constructor) { 770 } else if (member is ir.Constructor) {
774 functionNode = member.function; 771 functionNode = member.function;
775 } else { 772 } else {
776 throw new SpannableAssertionFailure( 773 throw new SpannableAssertionFailure(
777 function, "Unexpected function node ${member} for $function."); 774 function, "Unexpected function node ${member} for $function.");
778 } 775 }
779 return new CallStructure( 776 return new CallStructure(
780 functionNode.positionalParameters.length + 777 functionNode.positionalParameters.length +
781 functionNode.namedParameters.length, 778 functionNode.namedParameters.length,
782 functionNode.namedParameters.map((d) => d.name).toList()); 779 functionNode.namedParameters.map((d) => d.name).toList());
783 } 780 }
784 781
785 @override 782 @override
786 bool isDeferredLoadLibraryGetter(KMember member) { 783 bool isDeferredLoadLibraryGetter(KMember member) {
787 // TODO(johnniwinther): Support these. 784 // TODO(johnniwinther): Support these.
788 return false; 785 return false;
789 } 786 }
790 } 787 }
791 788
792 /// Visitor that converts kernel dart types into [DartType]. 789 /// Visitor that converts kernel dart types into [DartType].
793 class DartTypeConverter extends ir.DartTypeVisitor<DartType> { 790 class DartTypeConverter extends ir.DartTypeVisitor<DartType> {
794 final KernelWorldBuilder elementAdapter; 791 final KernelElementBuilder elementAdapter;
795 bool topLevel = true; 792 bool topLevel = true;
796 793
797 DartTypeConverter(this.elementAdapter); 794 DartTypeConverter(this.elementAdapter);
798 795
799 DartType convert(ir.DartType type) { 796 DartType convert(ir.DartType type) {
800 topLevel = true; 797 topLevel = true;
801 return type.accept(this); 798 return type.accept(this);
802 } 799 }
803 800
804 /// Visit a inner type. 801 /// Visit a inner type.
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
887 884
888 @override 885 @override
889 NativeData get nativeData { 886 NativeData get nativeData {
890 throw new UnimplementedError("KernelNativeBehaviorComputer.nativeData"); 887 throw new UnimplementedError("KernelNativeBehaviorComputer.nativeData");
891 } 888 }
892 } 889 }
893 890
894 /// Constant environment mapping [ConstantExpression]s to [ConstantValue]s using 891 /// Constant environment mapping [ConstantExpression]s to [ConstantValue]s using
895 /// [_EvaluationEnvironment] for the evaluation. 892 /// [_EvaluationEnvironment] for the evaluation.
896 class KernelConstantEnvironment implements ConstantEnvironment { 893 class KernelConstantEnvironment implements ConstantEnvironment {
897 KernelWorldBuilder _worldBuilder; 894 KernelElementBuilder _worldBuilder;
898 Map<ConstantExpression, ConstantValue> _valueMap = 895 Map<ConstantExpression, ConstantValue> _valueMap =
899 <ConstantExpression, ConstantValue>{}; 896 <ConstantExpression, ConstantValue>{};
900 897
901 KernelConstantEnvironment(this._worldBuilder); 898 KernelConstantEnvironment(this._worldBuilder);
902 899
903 @override 900 @override
904 ConstantSystem get constantSystem => const JavaScriptConstantSystem(); 901 ConstantSystem get constantSystem => const JavaScriptConstantSystem();
905 902
906 @override 903 @override
907 ConstantValue getConstantValueForVariable(VariableElement element) { 904 ConstantValue getConstantValueForVariable(VariableElement element) {
(...skipping 11 matching lines...) Expand all
919 916
920 @override 917 @override
921 bool hasConstantValue(ConstantExpression expression) { 918 bool hasConstantValue(ConstantExpression expression) {
922 throw new UnimplementedError("KernelConstantEnvironment.hasConstantValue"); 919 throw new UnimplementedError("KernelConstantEnvironment.hasConstantValue");
923 } 920 }
924 } 921 }
925 922
926 /// Evaluation environment used for computing [ConstantValue]s for 923 /// Evaluation environment used for computing [ConstantValue]s for
927 /// kernel based [ConstantExpression]s. 924 /// kernel based [ConstantExpression]s.
928 class _EvaluationEnvironment implements Environment { 925 class _EvaluationEnvironment implements Environment {
929 final KernelWorldBuilder _worldBuilder; 926 final KernelElementBuilder _worldBuilder;
930 927
931 _EvaluationEnvironment(this._worldBuilder); 928 _EvaluationEnvironment(this._worldBuilder);
932 929
933 @override 930 @override
934 CommonElements get commonElements { 931 CommonElements get commonElements {
935 throw new UnimplementedError("_EvaluationEnvironment.commonElements"); 932 throw new UnimplementedError("_EvaluationEnvironment.commonElements");
936 } 933 }
937 934
938 @override 935 @override
939 InterfaceType substByContext(InterfaceType base, InterfaceType target) { 936 InterfaceType substByContext(InterfaceType base, InterfaceType target) {
(...skipping 19 matching lines...) Expand all
959 } 956 }
960 957
961 @override 958 @override
962 String readFromEnvironment(String name) { 959 String readFromEnvironment(String name) {
963 throw new UnimplementedError("_EvaluationEnvironment.readFromEnvironment"); 960 throw new UnimplementedError("_EvaluationEnvironment.readFromEnvironment");
964 } 961 }
965 } 962 }
966 963
967 // Interface for testing equivalence of Kernel-based entities. 964 // Interface for testing equivalence of Kernel-based entities.
968 class WorldDeconstructionForTesting { 965 class WorldDeconstructionForTesting {
969 final KernelWorldBuilder builder; 966 final KernelElementBuilder builder;
970 967
971 WorldDeconstructionForTesting(this.builder); 968 WorldDeconstructionForTesting(this.builder);
972 969
973 Uri getLibraryUri(KLibrary library) { 970 Uri getLibraryUri(KLibrary library) {
974 return builder._libraryEnvs[library.libraryIndex].library.importUri; 971 return builder._libraryEnvs[library.libraryIndex].library.importUri;
975 } 972 }
976 973
977 KLibrary getLibraryForClass(KClass cls) { 974 KLibrary getLibraryForClass(KClass cls) {
978 KClassEnv env = builder._classEnvs[cls.classIndex]; 975 _KClassEnv env = builder._classEnvs[cls.classIndex];
979 return builder.getLibrary(env.cls.enclosingLibrary); 976 return builder.getLibrary(env.cls.enclosingLibrary);
980 } 977 }
981 978
982 KLibrary _getLibrary<E>(E member, Map<ir.Member, E> map) { 979 KLibrary _getLibrary<E>(E member, Map<ir.Member, E> map) {
983 ir.Library library; 980 ir.Library library;
984 map.forEach((ir.Member node, E other) { 981 map.forEach((ir.Member node, E other) {
985 if (library == null && member == other) { 982 if (library == null && member == other) {
986 library = node.enclosingLibrary; 983 library = node.enclosingLibrary;
987 } 984 }
988 }); 985 });
989 if (library == null) { 986 if (library == null) {
990 throw new ArgumentError("No library found for $member"); 987 throw new ArgumentError("No library found for $member");
991 } 988 }
992 return builder._getLibrary(library); 989 return builder._getLibrary(library);
993 } 990 }
994 991
995 KLibrary getLibraryForFunction(KFunction function) => 992 KLibrary getLibraryForFunction(KFunction function) =>
996 _getLibrary(function, builder._methodMap); 993 _getLibrary(function, builder._methodMap);
997 994
998 KLibrary getLibraryForField(KField field) => 995 KLibrary getLibraryForField(KField field) =>
999 _getLibrary(field, builder._fieldMap); 996 _getLibrary(field, builder._fieldMap);
1000 997
1001 KClass getSuperclassForClass(KClass cls) { 998 KClass getSuperclassForClass(KClass cls) {
1002 KClassEnv env = builder._classEnvs[cls.classIndex]; 999 _KClassEnv env = builder._classEnvs[cls.classIndex];
1003 ir.Supertype supertype = env.cls.supertype; 1000 ir.Supertype supertype = env.cls.supertype;
1004 if (supertype == null) return null; 1001 if (supertype == null) return null;
1005 return builder.getClass(supertype.classNode); 1002 return builder.getClass(supertype.classNode);
1006 } 1003 }
1007 1004
1008 InterfaceType getMixinTypeForClass(KClass cls) { 1005 InterfaceType getMixinTypeForClass(KClass cls) {
1009 KClassEnv env = builder._classEnvs[cls.classIndex]; 1006 _KClassEnv env = builder._classEnvs[cls.classIndex];
1010 ir.Supertype mixedInType = env.cls.mixedInType; 1007 ir.Supertype mixedInType = env.cls.mixedInType;
1011 if (mixedInType == null) return null; 1008 if (mixedInType == null) return null;
1012 return builder.createInterfaceType( 1009 return builder.createInterfaceType(
1013 mixedInType.classNode, mixedInType.typeArguments); 1010 mixedInType.classNode, mixedInType.typeArguments);
1014 } 1011 }
1015 } 1012 }
OLDNEW
« no previous file with comments | « no previous file | pkg/compiler/lib/src/kernel/native_class_resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698