| OLD | NEW |
| 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.world_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 '../frontend_strategy.dart'; |
| 21 import '../js_backend/constant_system_javascript.dart'; | 22 import '../js_backend/constant_system_javascript.dart'; |
| 22 import '../js_backend/native_data.dart'; | 23 import '../js_backend/native_data.dart'; |
| 23 import '../js_backend/no_such_method_registry.dart'; | 24 import '../js_backend/no_such_method_registry.dart'; |
| 24 import '../native/native.dart' as native; | 25 import '../native/native.dart' as native; |
| 25 import '../native/resolver.dart'; | 26 import '../native/resolver.dart'; |
| 26 import '../ordered_typeset.dart'; | 27 import '../ordered_typeset.dart'; |
| 27 import '../ssa/kernel_impact.dart'; | 28 import '../ssa/kernel_impact.dart'; |
| 28 import '../universe/call_structure.dart'; | 29 import '../universe/call_structure.dart'; |
| 29 import '../universe/world_builder.dart'; | 30 import '../universe/world_builder.dart'; |
| 30 import '../util/util.dart' show Link, LinkBuilder; | 31 import '../util/util.dart' show Link, LinkBuilder; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 42 class KernelWorldBuilder extends KernelElementAdapterMixin { | 43 class KernelWorldBuilder extends KernelElementAdapterMixin { |
| 43 CommonElements _commonElements; | 44 CommonElements _commonElements; |
| 44 native.BehaviorBuilder _nativeBehaviorBuilder; | 45 native.BehaviorBuilder _nativeBehaviorBuilder; |
| 45 final DiagnosticReporter reporter; | 46 final DiagnosticReporter reporter; |
| 46 ElementEnvironment _elementEnvironment; | 47 ElementEnvironment _elementEnvironment; |
| 47 DartTypeConverter _typeConverter; | 48 DartTypeConverter _typeConverter; |
| 48 KernelConstantEnvironment _constantEnvironment; | 49 KernelConstantEnvironment _constantEnvironment; |
| 49 _KernelDartTypes _types; | 50 _KernelDartTypes _types; |
| 50 | 51 |
| 51 /// Library environment. Used for fast lookup. | 52 /// Library environment. Used for fast lookup. |
| 52 KEnv _env; | 53 KEnv _env = new KEnv(); |
| 53 | 54 |
| 54 /// List of library environments by `KLibrary.libraryIndex`. This is used for | 55 /// List of library environments by `KLibrary.libraryIndex`. This is used for |
| 55 /// fast lookup into library classes and members. | 56 /// fast lookup into library classes and members. |
| 56 List<KLibraryEnv> _libraryEnvs = <KLibraryEnv>[]; | 57 List<KLibraryEnv> _libraryEnvs = <KLibraryEnv>[]; |
| 57 | 58 |
| 58 /// List of class environments by `KClass.classIndex`. This is used for | 59 /// List of class environments by `KClass.classIndex`. This is used for |
| 59 /// fast lookup into class members. | 60 /// fast lookup into class members. |
| 60 List<KClassEnv> _classEnvs = <KClassEnv>[]; | 61 List<KClassEnv> _classEnvs = <KClassEnv>[]; |
| 61 | 62 |
| 62 Map<ir.Library, KLibrary> _libraryMap = <ir.Library, KLibrary>{}; | 63 Map<ir.Library, KLibrary> _libraryMap = <ir.Library, KLibrary>{}; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 74 | 75 |
| 75 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{}; | 76 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{}; |
| 76 | 77 |
| 77 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{}; | 78 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{}; |
| 78 Map<KField, ConstantExpression> _fieldConstantMap = | 79 Map<KField, ConstantExpression> _fieldConstantMap = |
| 79 <KField, ConstantExpression>{}; | 80 <KField, ConstantExpression>{}; |
| 80 | 81 |
| 81 Map<ir.TreeNode, KLocalFunction> _localFunctionMap = | 82 Map<ir.TreeNode, KLocalFunction> _localFunctionMap = |
| 82 <ir.TreeNode, KLocalFunction>{}; | 83 <ir.TreeNode, KLocalFunction>{}; |
| 83 | 84 |
| 84 KernelWorldBuilder(this.reporter, ir.Program program) | 85 KernelWorldBuilder(this.reporter) { |
| 85 : _env = new KEnv(program) { | |
| 86 _elementEnvironment = new KernelElementEnvironment(this); | 86 _elementEnvironment = new KernelElementEnvironment(this); |
| 87 _commonElements = new CommonElements(_elementEnvironment); | 87 _commonElements = new CommonElements(_elementEnvironment); |
| 88 _constantEnvironment = new KernelConstantEnvironment(this); | 88 _constantEnvironment = new KernelConstantEnvironment(this); |
| 89 _nativeBehaviorBuilder = | 89 _nativeBehaviorBuilder = |
| 90 new KernelBehaviorBuilder(_commonElements, _constantEnvironment); | 90 new KernelBehaviorBuilder(_commonElements, _constantEnvironment); |
| 91 _types = new _KernelDartTypes(this); | 91 _types = new _KernelDartTypes(this); |
| 92 _typeConverter = new DartTypeConverter(this); | 92 _typeConverter = new DartTypeConverter(this); |
| 93 } | 93 } |
| 94 | 94 |
| 95 /// Adds libraries in [program] to the set of libraries. |
| 96 /// |
| 97 /// The main method of the first program is used as the main method for the |
| 98 /// compilation. |
| 99 void addProgram(ir.Program program) { |
| 100 _env.addProgram(program); |
| 101 } |
| 102 |
| 95 KMethod get _mainFunction { | 103 KMethod get _mainFunction { |
| 96 return _env.program.mainMethod != null | 104 return _env.mainMethod != null ? _getMethod(_env.mainMethod) : null; |
| 97 ? _getMethod(_env.program.mainMethod) | 105 } |
| 106 |
| 107 KLibrary get _mainLibrary { |
| 108 return _env.mainMethod != null |
| 109 ? _getLibrary(_env.mainMethod.enclosingLibrary) |
| 98 : null; | 110 : null; |
| 99 } | 111 } |
| 100 | 112 |
| 101 KLibrary get _mainLibrary { | |
| 102 return _env.program.mainMethod != null | |
| 103 ? _getLibrary(_env.program.mainMethod.enclosingLibrary) | |
| 104 : null; | |
| 105 } | |
| 106 | |
| 107 Iterable<LibraryEntity> get _libraries { | 113 Iterable<LibraryEntity> get _libraries { |
| 108 if (_env.length != _libraryMap.length) { | 114 if (_env.length != _libraryMap.length) { |
| 109 // Create a [KLibrary] for each library. | 115 // Create a [KLibrary] for each library. |
| 110 _env.forEachLibrary((KLibraryEnv env) { | 116 _env.forEachLibrary((KLibraryEnv env) { |
| 111 _getLibrary(env.library, env); | 117 _getLibrary(env.library, env); |
| 112 }); | 118 }); |
| 113 } | 119 } |
| 114 return _libraryMap.values; | 120 return _libraryMap.values; |
| 115 } | 121 } |
| 116 | 122 |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 } | 596 } |
| 591 | 597 |
| 592 ResolutionImpact computeWorldImpact(KMember member) { | 598 ResolutionImpact computeWorldImpact(KMember member) { |
| 593 ir.Member node = _memberList[member.memberIndex]; | 599 ir.Member node = _memberList[member.memberIndex]; |
| 594 return buildKernelImpact(node, this); | 600 return buildKernelImpact(node, this); |
| 595 } | 601 } |
| 596 } | 602 } |
| 597 | 603 |
| 598 /// Environment for fast lookup of program libraries. | 604 /// Environment for fast lookup of program libraries. |
| 599 class KEnv { | 605 class KEnv { |
| 600 final ir.Program program; | 606 final Set<ir.Program> programs = new Set<ir.Program>(); |
| 601 | 607 |
| 602 Map<Uri, KLibraryEnv> _libraryMap; | 608 Map<Uri, KLibraryEnv> _libraryMap; |
| 603 | 609 |
| 604 KEnv(this.program); | 610 /// TODO(johnniwinther): Handle arbitrary load order if needed. |
| 611 ir.Member get mainMethod => programs.first?.mainMethod; |
| 612 |
| 613 void addProgram(ir.Program program) { |
| 614 if (programs.add(program)) { |
| 615 if (_libraryMap != null) { |
| 616 _addLibraries(program); |
| 617 } |
| 618 } |
| 619 } |
| 620 |
| 621 void _addLibraries(ir.Program program) { |
| 622 for (ir.Library library in program.libraries) { |
| 623 _libraryMap[library.importUri] = new KLibraryEnv(library); |
| 624 } |
| 625 } |
| 605 | 626 |
| 606 void _ensureLibraryMap() { | 627 void _ensureLibraryMap() { |
| 607 if (_libraryMap == null) { | 628 if (_libraryMap == null) { |
| 608 _libraryMap = <Uri, KLibraryEnv>{}; | 629 _libraryMap = <Uri, KLibraryEnv>{}; |
| 609 for (ir.Library library in program.libraries) { | 630 for (ir.Program program in programs) { |
| 610 _libraryMap[library.importUri] = new KLibraryEnv(library); | 631 _addLibraries(program); |
| 611 } | 632 } |
| 612 } | 633 } |
| 613 } | 634 } |
| 614 | 635 |
| 615 /// Return the [KLibraryEnv] for the library with the canonical [uri]. | 636 /// Return the [KLibraryEnv] for the library with the canonical [uri]. |
| 616 KLibraryEnv lookupLibrary(Uri uri) { | 637 KLibraryEnv lookupLibrary(Uri uri) { |
| 617 _ensureLibraryMap(); | 638 _ensureLibraryMap(); |
| 618 return _libraryMap[uri]; | 639 return _libraryMap[uri]; |
| 619 } | 640 } |
| 620 | 641 |
| (...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1189 } | 1210 } |
| 1190 | 1211 |
| 1191 InterfaceType getMixinTypeForClass(KClass cls) { | 1212 InterfaceType getMixinTypeForClass(KClass cls) { |
| 1192 KClassEnv env = builder._classEnvs[cls.classIndex]; | 1213 KClassEnv env = builder._classEnvs[cls.classIndex]; |
| 1193 ir.Supertype mixedInType = env.cls.mixedInType; | 1214 ir.Supertype mixedInType = env.cls.mixedInType; |
| 1194 if (mixedInType == null) return null; | 1215 if (mixedInType == null) return null; |
| 1195 return builder.createInterfaceType( | 1216 return builder.createInterfaceType( |
| 1196 mixedInType.classNode, mixedInType.typeArguments); | 1217 mixedInType.classNode, mixedInType.typeArguments); |
| 1197 } | 1218 } |
| 1198 } | 1219 } |
| OLD | NEW |