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

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

Issue 2981423003: Move .getLocalFunction from KernelToElementMap to KernelToLocalsMap (Closed)
Patch Set: Created 3 years, 5 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) => _getLocalFunction(node);
1072
1073 Local _getLocalFunction(ir.TreeNode node) {
Emily Fortuna 2017/07/21 22:21:43 I know you've done this elsewhere, but why the pub
Johnni Winther 2017/07/28 12:08:19 Not needed here (now remove). I other places it wa
1074 assert(
1075 node is ir.FunctionDeclaration || node is ir.FunctionExpression,
1076 failedAt(
1077 CURRENT_ELEMENT_SPANNABLE, 'Invalid local function node: $node'));
1078 return _localFunctionMap.putIfAbsent(node, () {
1079 MemberEntity memberContext;
1080 Entity executableContext;
1081 ir.TreeNode parent = node.parent;
1082 while (parent != null) {
1083 if (parent is ir.Member) {
1084 executableContext = memberContext = getMember(parent);
1085 break;
1086 }
1087 if (parent is ir.FunctionDeclaration ||
1088 parent is ir.FunctionExpression) {
1089 Local localFunction = _getLocalFunction(parent);
1090 executableContext = localFunction;
1091 memberContext = localFunction.memberContext;
1092 break;
1093 }
1094 parent = parent.parent;
1095 }
1096 String name;
1097 FunctionType functionType;
1098 if (node is ir.FunctionDeclaration) {
1099 name = node.variable.name;
1100 functionType = getFunctionType(node.function);
1101 } else if (node is ir.FunctionExpression) {
1102 functionType = getFunctionType(node.function);
1103 }
1104 return new KLocalFunction(
1105 name, memberContext, executableContext, functionType);
1106 });
1107 }
1119 } 1108 }
1120 1109
1121 class KernelElementEnvironment implements ElementEnvironment { 1110 class KernelElementEnvironment implements ElementEnvironment {
1122 final KernelToElementMapBase elementMap; 1111 final KernelToElementMapBase elementMap;
1123 1112
1124 KernelElementEnvironment(this.elementMap); 1113 KernelElementEnvironment(this.elementMap);
1125 1114
1126 @override 1115 @override
1127 DartType get dynamicType => const DynamicType(); 1116 DartType get dynamicType => const DynamicType();
1128 1117
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after
1679 allTypedefs, 1668 allTypedefs,
1680 mixinUses, 1669 mixinUses,
1681 typesImplementedBySubclasses, 1670 typesImplementedBySubclasses,
1682 classHierarchyNodes, 1671 classHierarchyNodes,
1683 classSets) { 1672 classSets) {
1684 computeRtiNeed(resolutionWorldBuilder, rtiNeedBuilder, 1673 computeRtiNeed(resolutionWorldBuilder, rtiNeedBuilder,
1685 enableTypeAssertions: options.enableTypeAssertions); 1674 enableTypeAssertions: options.enableTypeAssertions);
1686 } 1675 }
1687 1676
1688 @override 1677 @override
1689 void registerClosureClass(ClassEntity cls, bool fromInstanceMember) { 1678 void registerClosureClass(ClassEntity cls) {
1690 // Tell the hierarchy that this is the super class. then we can use 1679 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 } 1680 }
1716 } 1681 }
1717 1682
1718 // Interface for testing equivalence of Kernel-based entities. 1683 // Interface for testing equivalence of Kernel-based entities.
1719 class WorldDeconstructionForTesting { 1684 class WorldDeconstructionForTesting {
1720 final KernelToElementMapBase elementMap; 1685 final KernelToElementMapBase elementMap;
1721 1686
1722 WorldDeconstructionForTesting(this.elementMap); 1687 WorldDeconstructionForTesting(this.elementMap);
1723 1688
1724 IndexedClass getSuperclassForClass(IndexedClass cls) { 1689 IndexedClass getSuperclassForClass(IndexedClass cls) {
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
2031 if (data.constructorBody != null) { 1996 if (data.constructorBody != null) {
2032 f(data.constructorBody); 1997 f(data.constructorBody);
2033 } 1998 }
2034 }); 1999 });
2035 } 2000 }
2036 2001
2037 String getDeferredUri(ir.LibraryDependency node) { 2002 String getDeferredUri(ir.LibraryDependency node) {
2038 throw new UnimplementedError('JsKernelToElementMap.getDeferredUri'); 2003 throw new UnimplementedError('JsKernelToElementMap.getDeferredUri');
2039 } 2004 }
2040 } 2005 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698