| 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.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 | 8 |
| 9 import '../common.dart'; | 9 import '../common.dart'; |
| 10 import '../common/names.dart' show Identifiers; | 10 import '../common/names.dart' show Identifiers; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{}; | 70 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{}; |
| 71 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{}; | 71 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{}; |
| 72 | 72 |
| 73 Map<ir.TreeNode, KLocalFunction> _localFunctionMap = | 73 Map<ir.TreeNode, KLocalFunction> _localFunctionMap = |
| 74 <ir.TreeNode, KLocalFunction>{}; | 74 <ir.TreeNode, KLocalFunction>{}; |
| 75 | 75 |
| 76 KernelToElementMap(this.reporter) { | 76 KernelToElementMap(this.reporter) { |
| 77 _elementEnvironment = new KernelElementEnvironment(this); | 77 _elementEnvironment = new KernelElementEnvironment(this); |
| 78 _commonElements = new CommonElements(_elementEnvironment); | 78 _commonElements = new CommonElements(_elementEnvironment); |
| 79 _constantEnvironment = new KernelConstantEnvironment(this); | 79 _constantEnvironment = new KernelConstantEnvironment(this); |
| 80 _nativeBehaviorBuilder = | 80 _nativeBehaviorBuilder = new KernelBehaviorBuilder(_commonElements); |
| 81 new KernelBehaviorBuilder(_commonElements, _constantEnvironment); | |
| 82 _types = new _KernelDartTypes(this); | 81 _types = new _KernelDartTypes(this); |
| 83 _typeConverter = new DartTypeConverter(this); | 82 _typeConverter = new DartTypeConverter(this); |
| 84 } | 83 } |
| 85 | 84 |
| 86 /// Adds libraries in [program] to the set of libraries. | 85 /// Adds libraries in [program] to the set of libraries. |
| 87 /// | 86 /// |
| 88 /// The main method of the first program is used as the main method for the | 87 /// The main method of the first program is used as the main method for the |
| 89 /// compilation. | 88 /// compilation. |
| 90 void addProgram(ir.Program program) { | 89 void addProgram(ir.Program program) { |
| 91 _env.addProgram(program); | 90 _env.addProgram(program); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 117 @override | 116 @override |
| 118 ElementEnvironment get elementEnvironment => _elementEnvironment; | 117 ElementEnvironment get elementEnvironment => _elementEnvironment; |
| 119 | 118 |
| 120 ConstantEnvironment get constantEnvironment => _constantEnvironment; | 119 ConstantEnvironment get constantEnvironment => _constantEnvironment; |
| 121 | 120 |
| 122 DartTypes get types => _types; | 121 DartTypes get types => _types; |
| 123 | 122 |
| 124 @override | 123 @override |
| 125 native.BehaviorBuilder get nativeBehaviorBuilder => _nativeBehaviorBuilder; | 124 native.BehaviorBuilder get nativeBehaviorBuilder => _nativeBehaviorBuilder; |
| 126 | 125 |
| 126 @override |
| 127 ConstantValue computeConstantValue(ConstantExpression constant) { |
| 128 return _constantEnvironment.getConstantValue(constant); |
| 129 } |
| 130 |
| 127 LibraryEntity lookupLibrary(Uri uri) { | 131 LibraryEntity lookupLibrary(Uri uri) { |
| 128 _KLibraryEnv libraryEnv = _env.lookupLibrary(uri); | 132 _KLibraryEnv libraryEnv = _env.lookupLibrary(uri); |
| 129 if (libraryEnv == null) return null; | 133 if (libraryEnv == null) return null; |
| 130 return _getLibrary(libraryEnv.library, libraryEnv); | 134 return _getLibrary(libraryEnv.library, libraryEnv); |
| 131 } | 135 } |
| 132 | 136 |
| 133 KLibrary _getLibrary(ir.Library node, [_KLibraryEnv libraryEnv]) { | 137 KLibrary _getLibrary(ir.Library node, [_KLibraryEnv libraryEnv]) { |
| 134 return _libraryMap.putIfAbsent(node, () { | 138 return _libraryMap.putIfAbsent(node, () { |
| 135 Uri canonicalUri = node.importUri; | 139 Uri canonicalUri = node.importUri; |
| 136 _libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(canonicalUri)); | 140 _libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(canonicalUri)); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 KLibrary library = _getLibrary(node.enclosingLibrary); | 191 KLibrary library = _getLibrary(node.enclosingLibrary); |
| 188 if (classEnv == null) { | 192 if (classEnv == null) { |
| 189 classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name); | 193 classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name); |
| 190 } | 194 } |
| 191 _classEnvs.add(classEnv); | 195 _classEnvs.add(classEnv); |
| 192 return new KClass(library, _classMap.length, node.name, | 196 return new KClass(library, _classMap.length, node.name, |
| 193 isAbstract: node.isAbstract); | 197 isAbstract: node.isAbstract); |
| 194 }); | 198 }); |
| 195 } | 199 } |
| 196 | 200 |
| 197 Iterable<ConstantExpression> _getClassMetadata(KClass cls) { | 201 Iterable<ConstantValue> _getClassMetadata(KClass cls) { |
| 198 return _classEnvs[cls.classIndex].getMetadata(this); | 202 return _classEnvs[cls.classIndex].getMetadata(this); |
| 199 } | 203 } |
| 200 | 204 |
| 201 KTypeVariable _getTypeVariable(ir.TypeParameter node) { | 205 KTypeVariable _getTypeVariable(ir.TypeParameter node) { |
| 202 return _typeVariableMap.putIfAbsent(node, () { | 206 return _typeVariableMap.putIfAbsent(node, () { |
| 203 if (node.parent is ir.Class) { | 207 if (node.parent is ir.Class) { |
| 204 ir.Class cls = node.parent; | 208 ir.Class cls = node.parent; |
| 205 int index = cls.typeParameters.indexOf(node); | 209 int index = cls.typeParameters.indexOf(node); |
| 206 return new KTypeVariable(_getClass(cls), node.name, index); | 210 return new KTypeVariable(_getClass(cls), node.name, index); |
| 207 } | 211 } |
| (...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 InterfaceType thisType; | 729 InterfaceType thisType; |
| 726 InterfaceType rawType; | 730 InterfaceType rawType; |
| 727 InterfaceType supertype; | 731 InterfaceType supertype; |
| 728 InterfaceType mixedInType; | 732 InterfaceType mixedInType; |
| 729 OrderedTypeSet orderedTypeSet; | 733 OrderedTypeSet orderedTypeSet; |
| 730 | 734 |
| 731 Map<String, ir.Member> _constructorMap; | 735 Map<String, ir.Member> _constructorMap; |
| 732 Map<String, ir.Member> _memberMap; | 736 Map<String, ir.Member> _memberMap; |
| 733 Map<String, ir.Member> _setterMap; | 737 Map<String, ir.Member> _setterMap; |
| 734 | 738 |
| 735 Iterable<ConstantExpression> _metadata; | 739 Iterable<ConstantValue> _metadata; |
| 736 | 740 |
| 737 _KClassEnv(this.cls) | 741 _KClassEnv(this.cls) |
| 738 // TODO(johnniwinther): Change this to use a property on [cls] when such | 742 // TODO(johnniwinther): Change this to use a property on [cls] when such |
| 739 // is added to kernel. | 743 // is added to kernel. |
| 740 : isUnnamedMixinApplication = cls.name.contains('+'); | 744 : isUnnamedMixinApplication = cls.name.contains('+'); |
| 741 | 745 |
| 742 void _ensureMaps() { | 746 void _ensureMaps() { |
| 743 if (_memberMap == null) { | 747 if (_memberMap == null) { |
| 744 _memberMap = <String, ir.Member>{}; | 748 _memberMap = <String, ir.Member>{}; |
| 745 _setterMap = <String, ir.Member>{}; | 749 _setterMap = <String, ir.Member>{}; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 _memberMap.values.forEach(f); | 798 _memberMap.values.forEach(f); |
| 795 for (ir.Member member in _setterMap.values) { | 799 for (ir.Member member in _setterMap.values) { |
| 796 if (member is ir.Procedure) { | 800 if (member is ir.Procedure) { |
| 797 f(member); | 801 f(member); |
| 798 } else { | 802 } else { |
| 799 // Skip fields; these are also in _memberMap. | 803 // Skip fields; these are also in _memberMap. |
| 800 } | 804 } |
| 801 } | 805 } |
| 802 } | 806 } |
| 803 | 807 |
| 804 Iterable<ConstantExpression> getMetadata(KernelToElementMap elementMap) { | 808 Iterable<ConstantValue> getMetadata(KernelToElementMap elementMap) { |
| 805 if (_metadata == null) { | 809 if (_metadata == null) { |
| 806 _metadata = elementMap.getMetadata(cls.annotations); | 810 _metadata = elementMap.getMetadata(cls.annotations); |
| 807 } | 811 } |
| 808 return _metadata; | 812 return _metadata; |
| 809 } | 813 } |
| 810 } | 814 } |
| 811 | 815 |
| 812 class _MemberData { | 816 class _MemberData { |
| 813 final ir.Member node; | 817 final ir.Member node; |
| 814 Iterable<ConstantExpression> _metadata; | 818 Iterable<ConstantValue> _metadata; |
| 815 | 819 |
| 816 _MemberData(this.node); | 820 _MemberData(this.node); |
| 817 | 821 |
| 818 ResolutionImpact getWorldImpact(KernelToElementMap elementMap) { | 822 ResolutionImpact getWorldImpact(KernelToElementMap elementMap) { |
| 819 return buildKernelImpact(node, elementMap); | 823 return buildKernelImpact(node, elementMap); |
| 820 } | 824 } |
| 821 | 825 |
| 822 Iterable<ConstantExpression> getMetadata(KernelToElementMap elementMap) { | 826 Iterable<ConstantValue> getMetadata(KernelToElementMap elementMap) { |
| 823 return _metadata ??= elementMap.getMetadata(node.annotations); | 827 return _metadata ??= elementMap.getMetadata(node.annotations); |
| 824 } | 828 } |
| 825 } | 829 } |
| 826 | 830 |
| 827 class _FunctionData extends _MemberData { | 831 class _FunctionData extends _MemberData { |
| 828 final ir.FunctionNode functionNode; | 832 final ir.FunctionNode functionNode; |
| 829 FunctionType _type; | 833 FunctionType _type; |
| 830 CallStructure _callStructure; | 834 CallStructure _callStructure; |
| 831 | 835 |
| 832 _FunctionData(ir.Member node, this.functionNode) : super(node); | 836 _FunctionData(ir.Member node, this.functionNode) : super(node); |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1042 } | 1046 } |
| 1043 | 1047 |
| 1044 @override | 1048 @override |
| 1045 bool isDeferredLoadLibraryGetter(KMember member) { | 1049 bool isDeferredLoadLibraryGetter(KMember member) { |
| 1046 // TODO(johnniwinther): Support these. | 1050 // TODO(johnniwinther): Support these. |
| 1047 return false; | 1051 return false; |
| 1048 } | 1052 } |
| 1049 | 1053 |
| 1050 @override | 1054 @override |
| 1051 Iterable<ConstantValue> getMemberMetadata(KMember member) { | 1055 Iterable<ConstantValue> getMemberMetadata(KMember member) { |
| 1052 List<ConstantValue> values = <ConstantValue>[]; | |
| 1053 _MemberData memberData = elementMap._memberList[member.memberIndex]; | 1056 _MemberData memberData = elementMap._memberList[member.memberIndex]; |
| 1054 for (ConstantExpression constant in memberData.getMetadata(elementMap)) { | 1057 return memberData.getMetadata(elementMap); |
| 1055 values.add(elementMap.constantEnvironment.getConstantValue(constant)); | |
| 1056 } | |
| 1057 return values; | |
| 1058 } | 1058 } |
| 1059 } | 1059 } |
| 1060 | 1060 |
| 1061 /// Visitor that converts kernel dart types into [DartType]. | 1061 /// Visitor that converts kernel dart types into [DartType]. |
| 1062 class DartTypeConverter extends ir.DartTypeVisitor<DartType> { | 1062 class DartTypeConverter extends ir.DartTypeVisitor<DartType> { |
| 1063 final KernelToElementMap elementAdapter; | 1063 final KernelToElementMap elementAdapter; |
| 1064 bool topLevel = true; | 1064 bool topLevel = true; |
| 1065 | 1065 |
| 1066 DartTypeConverter(this.elementAdapter); | 1066 DartTypeConverter(this.elementAdapter); |
| 1067 | 1067 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1129 "Outermost invalid types not currently supported"); | 1129 "Outermost invalid types not currently supported"); |
| 1130 } | 1130 } |
| 1131 // Nested invalid types are treated as `dynamic`. | 1131 // Nested invalid types are treated as `dynamic`. |
| 1132 return const DynamicType(); | 1132 return const DynamicType(); |
| 1133 } | 1133 } |
| 1134 } | 1134 } |
| 1135 | 1135 |
| 1136 /// [native.BehaviorBuilder] for kernel based elements. | 1136 /// [native.BehaviorBuilder] for kernel based elements. |
| 1137 class KernelBehaviorBuilder extends native.BehaviorBuilder { | 1137 class KernelBehaviorBuilder extends native.BehaviorBuilder { |
| 1138 final CommonElements commonElements; | 1138 final CommonElements commonElements; |
| 1139 final ConstantEnvironment constants; | |
| 1140 | 1139 |
| 1141 KernelBehaviorBuilder( | 1140 KernelBehaviorBuilder(this.commonElements); |
| 1142 this.commonElements, | |
| 1143 this.constants, | |
| 1144 ); | |
| 1145 | 1141 |
| 1146 @override | 1142 @override |
| 1147 bool get trustJSInteropTypeAnnotations { | 1143 bool get trustJSInteropTypeAnnotations { |
| 1148 throw new UnimplementedError( | 1144 throw new UnimplementedError( |
| 1149 "KernelNativeBehaviorComputer.trustJSInteropTypeAnnotations"); | 1145 "KernelNativeBehaviorComputer.trustJSInteropTypeAnnotations"); |
| 1150 } | 1146 } |
| 1151 | 1147 |
| 1152 @override | 1148 @override |
| 1153 DiagnosticReporter get reporter { | 1149 DiagnosticReporter get reporter { |
| 1154 throw new UnimplementedError("KernelNativeBehaviorComputer.reporter"); | 1150 throw new UnimplementedError("KernelNativeBehaviorComputer.reporter"); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1294 } | 1290 } |
| 1295 | 1291 |
| 1296 InterfaceType getMixinTypeForClass(KClass cls) { | 1292 InterfaceType getMixinTypeForClass(KClass cls) { |
| 1297 _KClassEnv env = builder._classEnvs[cls.classIndex]; | 1293 _KClassEnv env = builder._classEnvs[cls.classIndex]; |
| 1298 ir.Supertype mixedInType = env.cls.mixedInType; | 1294 ir.Supertype mixedInType = env.cls.mixedInType; |
| 1299 if (mixedInType == null) return null; | 1295 if (mixedInType == null) return null; |
| 1300 return builder.createInterfaceType( | 1296 return builder.createInterfaceType( |
| 1301 mixedInType.classNode, mixedInType.typeArguments); | 1297 mixedInType.classNode, mixedInType.typeArguments); |
| 1302 } | 1298 } |
| 1303 } | 1299 } |
| OLD | NEW |