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/element_map_impl.dart

Issue 2981423003: Move .getLocalFunction from KernelToElementMap to KernelToLocalsMap (Closed)
Patch Set: Updated cf. comments Created 3 years, 4 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 8
9 import '../common.dart'; 9 import '../common.dart';
10 import '../common/names.dart' show Identifiers; 10 import '../common/names.dart' show Identifiers;
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 FunctionEntity getMethod(ir.Procedure node) => _getMethod(node); 391 FunctionEntity getMethod(ir.Procedure node) => _getMethod(node);
392 392
393 FunctionEntity _getMethod(ir.Procedure node); 393 FunctionEntity _getMethod(ir.Procedure node);
394 394
395 @override 395 @override
396 FieldEntity getField(ir.Field node) => _getField(node); 396 FieldEntity getField(ir.Field node) => _getField(node);
397 397
398 FieldEntity _getField(ir.Field node); 398 FieldEntity _getField(ir.Field node);
399 399
400 @override 400 @override
401 Local getLocalFunction(ir.TreeNode node) => _getLocalFunction(node);
402
403 Local _getLocalFunction(ir.TreeNode node);
404
405 @override
406 DartType getDartType(ir.DartType type) => _typeConverter.convert(type); 401 DartType getDartType(ir.DartType type) => _typeConverter.convert(type);
407 402
408 List<DartType> getDartTypes(List<ir.DartType> types) { 403 List<DartType> getDartTypes(List<ir.DartType> types) {
409 // TODO(johnniwinther): Add the type argument to the list literal when we 404 // TODO(johnniwinther): Add the type argument to the list literal when we
410 // no longer use resolution types. 405 // no longer use resolution types.
411 List<DartType> list = /*<DartType>*/ []; 406 List<DartType> list = /*<DartType>*/ [];
412 types.forEach((ir.DartType type) { 407 types.forEach((ir.DartType type) {
413 list.add(getDartType(type)); 408 list.add(getDartType(type));
414 }); 409 });
415 return list; 410 return list;
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 ParameterStructure _getParameterStructure(ir.FunctionNode node) { 883 ParameterStructure _getParameterStructure(ir.FunctionNode node) {
889 // TODO(johnniwinther): Cache the computed function type. 884 // TODO(johnniwinther): Cache the computed function type.
890 int requiredParameters = node.requiredParameterCount; 885 int requiredParameters = node.requiredParameterCount;
891 int positionalParameters = node.positionalParameters.length; 886 int positionalParameters = node.positionalParameters.length;
892 List<String> namedParameters = 887 List<String> namedParameters =
893 node.namedParameters.map((p) => p.name).toList()..sort(); 888 node.namedParameters.map((p) => p.name).toList()..sort();
894 return new ParameterStructure( 889 return new ParameterStructure(
895 requiredParameters, positionalParameters, namedParameters); 890 requiredParameters, positionalParameters, namedParameters);
896 } 891 }
897 892
898 Local _getLocalFunction(ir.TreeNode node) {
899 assert(
900 node is ir.FunctionDeclaration || node is ir.FunctionExpression,
901 failedAt(
902 CURRENT_ELEMENT_SPANNABLE, 'Invalid local function node: $node'));
903 return _localFunctionMap.putIfAbsent(node, () {
904 MemberEntity memberContext;
905 Entity executableContext;
906 ir.TreeNode parent = node.parent;
907 while (parent != null) {
908 if (parent is ir.Member) {
909 executableContext = memberContext = getMember(parent);
910 break;
911 }
912 if (parent is ir.FunctionDeclaration ||
913 parent is ir.FunctionExpression) {
914 Local localFunction = _getLocalFunction(parent);
915 executableContext = localFunction;
916 memberContext = localFunction.memberContext;
917 break;
918 }
919 parent = parent.parent;
920 }
921 String name;
922 FunctionType functionType;
923 if (node is ir.FunctionDeclaration) {
924 name = node.variable.name;
925 functionType = getFunctionType(node.function);
926 } else if (node is ir.FunctionExpression) {
927 functionType = getFunctionType(node.function);
928 }
929 return createLocalFunction(
930 name, memberContext, executableContext, functionType);
931 });
932 }
933
934 IndexedLibrary createLibrary(int libraryIndex, String name, Uri canonicalUri); 893 IndexedLibrary createLibrary(int libraryIndex, String name, Uri canonicalUri);
935 894
936 IndexedClass createClass(LibraryEntity library, int classIndex, String name, 895 IndexedClass createClass(LibraryEntity library, int classIndex, String name,
937 {bool isAbstract}); 896 {bool isAbstract});
938 897
939 TypeVariableEntity createTypeVariable( 898 TypeVariableEntity createTypeVariable(
940 int typeVariableIndex, Entity typeDeclaration, String name, int index); 899 int typeVariableIndex, Entity typeDeclaration, String name, int index);
941 900
942 IndexedConstructor createGenerativeConstructor( 901 IndexedConstructor createGenerativeConstructor(
943 int memberIndex, 902 int memberIndex,
(...skipping 27 matching lines...) Expand all
971 bool isExternal, 930 bool isExternal,
972 bool isAbstract}); 931 bool isAbstract});
973 932
974 IndexedFunction createSetter(int memberIndex, LibraryEntity library, 933 IndexedFunction createSetter(int memberIndex, LibraryEntity library,
975 ClassEntity enclosingClass, Name name, 934 ClassEntity enclosingClass, Name name,
976 {bool isStatic, bool isExternal, bool isAbstract}); 935 {bool isStatic, bool isExternal, bool isAbstract});
977 936
978 IndexedField createField(int memberIndex, LibraryEntity library, 937 IndexedField createField(int memberIndex, LibraryEntity library,
979 ClassEntity enclosingClass, Name name, 938 ClassEntity enclosingClass, Name name,
980 {bool isStatic, bool isAssignable, bool isConst}); 939 {bool isStatic, bool isAssignable, bool isConst});
981
982 Local createLocalFunction(String name, MemberEntity memberContext,
983 Entity executableContext, FunctionType functionType);
984 } 940 }
985 941
986 /// Completes the [ElementCreatorMixin] by creating K-model elements. 942 /// Completes the [ElementCreatorMixin] by creating K-model elements.
987 abstract class KElementCreatorMixin implements ElementCreatorMixin { 943 abstract class KElementCreatorMixin implements ElementCreatorMixin {
988 IndexedLibrary createLibrary( 944 IndexedLibrary createLibrary(
989 int libraryIndex, String name, Uri canonicalUri) { 945 int libraryIndex, String name, Uri canonicalUri) {
990 return new KLibrary(libraryIndex, name, canonicalUri); 946 return new KLibrary(libraryIndex, name, canonicalUri);
991 } 947 }
992 948
993 IndexedClass createClass(LibraryEntity library, int classIndex, String name, 949 IndexedClass createClass(LibraryEntity library, int classIndex, String name,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 return new KSetter(memberIndex, library, enclosingClass, name, 1011 return new KSetter(memberIndex, library, enclosingClass, name,
1056 isStatic: isStatic, isExternal: isExternal, isAbstract: isAbstract); 1012 isStatic: isStatic, isExternal: isExternal, isAbstract: isAbstract);
1057 } 1013 }
1058 1014
1059 IndexedField createField(int memberIndex, LibraryEntity library, 1015 IndexedField createField(int memberIndex, LibraryEntity library,
1060 ClassEntity enclosingClass, Name name, 1016 ClassEntity enclosingClass, Name name,
1061 {bool isStatic, bool isAssignable, bool isConst}) { 1017 {bool isStatic, bool isAssignable, bool isConst}) {
1062 return new KField(memberIndex, library, enclosingClass, name, 1018 return new KField(memberIndex, library, enclosingClass, name,
1063 isStatic: isStatic, isAssignable: isAssignable, isConst: isConst); 1019 isStatic: isStatic, isAssignable: isAssignable, isConst: isConst);
1064 } 1020 }
1065
1066 Local createLocalFunction(String name, MemberEntity memberContext,
1067 Entity executableContext, FunctionType functionType) {
1068 return new KLocalFunction(
1069 name, memberContext, executableContext, functionType);
1070 }
1071 } 1021 }
1072 1022
1073 /// Implementation of [KernelToElementMapForImpact] that only supports world 1023 /// Implementation of [KernelToElementMapForImpact] that only supports world
1074 /// impact computation. 1024 /// impact computation.
1075 class KernelToElementMapForImpactImpl extends KernelToElementMapBase 1025 class KernelToElementMapForImpactImpl extends KernelToElementMapBase
1076 with 1026 with
1077 KernelToElementMapForImpactMixin, 1027 KernelToElementMapForImpactMixin,
1078 ElementCreatorMixin, 1028 ElementCreatorMixin,
1079 KElementCreatorMixin { 1029 KElementCreatorMixin {
1080 native.BehaviorBuilder _nativeBehaviorBuilder; 1030 native.BehaviorBuilder _nativeBehaviorBuilder;
(...skipping 28 matching lines...) Expand all
1109 } 1059 }
1110 1060
1111 /// Returns the kernel [ir.Procedure] node for the [method]. 1061 /// Returns the kernel [ir.Procedure] node for the [method].
1112 ir.Procedure _lookupProcedure(KFunction method) { 1062 ir.Procedure _lookupProcedure(KFunction method) {
1113 return _memberData[method.memberIndex].node; 1063 return _memberData[method.memberIndex].node;
1114 } 1064 }
1115 1065
1116 Iterable<ConstantValue> _getClassMetadata(KClass cls) { 1066 Iterable<ConstantValue> _getClassMetadata(KClass cls) {
1117 return _classData[cls.classIndex].getMetadata(this); 1067 return _classData[cls.classIndex].getMetadata(this);
1118 } 1068 }
1069
1070 @override
1071 Local getLocalFunction(ir.TreeNode node) {
1072 assert(
1073 node is ir.FunctionDeclaration || node is ir.FunctionExpression,
1074 failedAt(
1075 CURRENT_ELEMENT_SPANNABLE, 'Invalid local function node: $node'));
1076 return _localFunctionMap.putIfAbsent(node, () {
1077 MemberEntity memberContext;
1078 Entity executableContext;
1079 ir.TreeNode parent = node.parent;
1080 while (parent != null) {
1081 if (parent is ir.Member) {
1082 executableContext = memberContext = getMember(parent);
1083 break;
1084 }
1085 if (parent is ir.FunctionDeclaration ||
1086 parent is ir.FunctionExpression) {
1087 Local localFunction = getLocalFunction(parent);
1088 executableContext = localFunction;
1089 memberContext = localFunction.memberContext;
1090 break;
1091 }
1092 parent = parent.parent;
1093 }
1094 String name;
1095 FunctionType functionType;
1096 if (node is ir.FunctionDeclaration) {
1097 name = node.variable.name;
1098 functionType = getFunctionType(node.function);
1099 } else if (node is ir.FunctionExpression) {
1100 functionType = getFunctionType(node.function);
1101 }
1102 return new KLocalFunction(
1103 name, memberContext, executableContext, functionType);
1104 });
1105 }
1119 } 1106 }
1120 1107
1121 class KernelElementEnvironment implements ElementEnvironment { 1108 class KernelElementEnvironment implements ElementEnvironment {
1122 final KernelToElementMapBase elementMap; 1109 final KernelToElementMapBase elementMap;
1123 1110
1124 KernelElementEnvironment(this.elementMap); 1111 KernelElementEnvironment(this.elementMap);
1125 1112
1126 @override 1113 @override
1127 DartType get dynamicType => const DynamicType(); 1114 DartType get dynamicType => const DynamicType();
1128 1115
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after
1679 allTypedefs, 1666 allTypedefs,
1680 mixinUses, 1667 mixinUses,
1681 typesImplementedBySubclasses, 1668 typesImplementedBySubclasses,
1682 classHierarchyNodes, 1669 classHierarchyNodes,
1683 classSets) { 1670 classSets) {
1684 computeRtiNeed(resolutionWorldBuilder, rtiNeedBuilder, 1671 computeRtiNeed(resolutionWorldBuilder, rtiNeedBuilder,
1685 enableTypeAssertions: options.enableTypeAssertions); 1672 enableTypeAssertions: options.enableTypeAssertions);
1686 } 1673 }
1687 1674
1688 @override 1675 @override
1689 void registerClosureClass(ClassEntity cls, bool fromInstanceMember) { 1676 void registerClosureClass(ClassEntity cls) {
1690 // Tell the hierarchy that this is the super class. then we can use 1677 throw new UnsupportedError('KernelClosedWorld.registerClosureClass');
1691 // .getSupertypes(class)
1692 IndexedClass superclass = fromInstanceMember
1693 ? commonElements.boundClosureClass
1694 : commonElements.closureClass;
1695 ClassHierarchyNode parentNode = getClassHierarchyNode(superclass);
1696 ClassHierarchyNode node = new ClassHierarchyNode(
1697 parentNode, cls, getHierarchyDepth(superclass) + 1);
1698 addClassHierarchyNode(cls, node);
1699 for (InterfaceType type in getOrderedTypeSet(superclass).types) {
1700 // TODO(efortuna): assert that the FunctionClass is in this ordered set.
1701 // If not, we need to explicitly add node as a subtype of FunctionClass.
1702 ClassSet subtypeSet = getClassSet(type.element);
1703 subtypeSet.addSubtype(node);
1704 }
1705 addClassSet(cls, new ClassSet(node));
1706
1707 // Ensure that the supertype's hierarchy is completely set up.
1708 var supertype = new InterfaceType(superclass, const []);
1709 ClassData superdata = elementMap._classData[superclass.classIndex];
1710 elementMap._ensureSupertypes(superclass, superdata);
1711 elementMap._ensureThisAndRawType(superclass, superdata);
1712
1713 elementMap.addClosureClass(cls, supertype);
1714 node.isDirectlyInstantiated = true;
1715 } 1678 }
1716 } 1679 }
1717 1680
1718 // Interface for testing equivalence of Kernel-based entities. 1681 // Interface for testing equivalence of Kernel-based entities.
1719 class WorldDeconstructionForTesting { 1682 class WorldDeconstructionForTesting {
1720 final KernelToElementMapBase elementMap; 1683 final KernelToElementMapBase elementMap;
1721 1684
1722 WorldDeconstructionForTesting(this.elementMap); 1685 WorldDeconstructionForTesting(this.elementMap);
1723 1686
1724 IndexedClass getSuperclassForClass(IndexedClass cls) { 1687 IndexedClass getSuperclassForClass(IndexedClass cls) {
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
2031 if (data.constructorBody != null) { 1994 if (data.constructorBody != null) {
2032 f(data.constructorBody); 1995 f(data.constructorBody);
2033 } 1996 }
2034 }); 1997 });
2035 } 1998 }
2036 1999
2037 String getDeferredUri(ir.LibraryDependency node) { 2000 String getDeferredUri(ir.LibraryDependency node) {
2038 throw new UnimplementedError('JsKernelToElementMap.getDeferredUri'); 2001 throw new UnimplementedError('JsKernelToElementMap.getDeferredUri');
2039 } 2002 }
2040 } 2003 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/kernel/element_map.dart ('k') | pkg/compiler/lib/src/ssa/builder_kernel.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698