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

Side by Side Diff: pkg/analyzer/lib/src/dart/resolver/inheritance_manager.dart

Issue 1918923003: Remove unnecessary casts and general code clean-up (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: clean-up Created 4 years, 7 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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'dart:collection'; 5 import 'dart:collection';
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/token.dart'; 8 import 'package:analyzer/dart/ast/token.dart';
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/dart/element/type.dart'; 10 import 'package:analyzer/dart/element/type.dart';
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 new HashMap<ClassElement, Set<AnalysisError>>(); 46 new HashMap<ClassElement, Set<AnalysisError>>();
47 47
48 /** 48 /**
49 * Initialize a newly created inheritance manager. 49 * Initialize a newly created inheritance manager.
50 * 50 *
51 * @param library the library element context that the inheritance mappings ar e being generated 51 * @param library the library element context that the inheritance mappings ar e being generated
52 */ 52 */
53 InheritanceManager(LibraryElement library) { 53 InheritanceManager(LibraryElement library) {
54 this._library = library; 54 this._library = library;
55 _classLookup = new HashMap<ClassElement, Map<String, ExecutableElement>>(); 55 _classLookup = new HashMap<ClassElement, Map<String, ExecutableElement>>();
56 _interfaceLookup = new HashMap<ClassElement, Map<String, ExecutableElement>> (); 56 _interfaceLookup =
57 new HashMap<ClassElement, Map<String, ExecutableElement>>();
57 } 58 }
58 59
59 /** 60 /**
60 * Set the new library element context. 61 * Set the new library element context.
61 * 62 *
62 * @param library the new library element 63 * @param library the new library element
63 */ 64 */
64 void set libraryElement(LibraryElement library) { 65 void set libraryElement(LibraryElement library) {
65 this._library = library; 66 this._library = library;
66 } 67 }
(...skipping 12 matching lines...) Expand all
79 /** 80 /**
80 * Get and return a mapping between the set of all string names of the members inherited from the 81 * Get and return a mapping between the set of all string names of the members inherited from the
81 * passed [ClassElement] superclass hierarchy, and the associated [ExecutableE lement]. 82 * passed [ClassElement] superclass hierarchy, and the associated [ExecutableE lement].
82 * 83 *
83 * @param classElt the class element to query 84 * @param classElt the class element to query
84 * @return a mapping between the set of all members inherited from the passed [ClassElement] 85 * @return a mapping between the set of all members inherited from the passed [ClassElement]
85 * superclass hierarchy, and the associated [ExecutableElement] 86 * superclass hierarchy, and the associated [ExecutableElement]
86 */ 87 */
87 @deprecated 88 @deprecated
88 MemberMap getMapOfMembersInheritedFromClasses(ClassElement classElt) => 89 MemberMap getMapOfMembersInheritedFromClasses(ClassElement classElt) =>
89 new MemberMap.fromMap(_computeClassChainLookupMap(classElt, new HashSet<Cl assElement>())); 90 new MemberMap.fromMap(
91 _computeClassChainLookupMap(classElt, new HashSet<ClassElement>()));
90 92
91 /** 93 /**
92 * Get and return a mapping between the set of all string names of the members inherited from the 94 * Get and return a mapping between the set of all string names of the members inherited from the
93 * passed [ClassElement] interface hierarchy, and the associated [ExecutableEl ement]. 95 * passed [ClassElement] interface hierarchy, and the associated [ExecutableEl ement].
94 * 96 *
95 * @param classElt the class element to query 97 * @param classElt the class element to query
96 * @return a mapping between the set of all string names of the members inheri ted from the passed 98 * @return a mapping between the set of all string names of the members inheri ted from the passed
97 * [ClassElement] interface hierarchy, and the associated [ExecutableE lement]. 99 * [ClassElement] interface hierarchy, and the associated [ExecutableE lement].
98 */ 100 */
99 @deprecated 101 @deprecated
100 MemberMap getMapOfMembersInheritedFromInterfaces(ClassElement classElt) => 102 MemberMap getMapOfMembersInheritedFromInterfaces(ClassElement classElt) =>
101 new MemberMap.fromMap(_computeInterfaceLookupMap(classElt, new HashSet<Cla ssElement>())); 103 new MemberMap.fromMap(
104 _computeInterfaceLookupMap(classElt, new HashSet<ClassElement>()));
102 105
103 /** 106 /**
104 * Return a table mapping the string names of the members inherited from the 107 * Return a table mapping the string names of the members inherited from the
105 * passed [ClassElement]'s superclass hierarchy, and the associated executable 108 * passed [ClassElement]'s superclass hierarchy, and the associated executable
106 * element. 109 * element.
107 */ 110 */
108 Map<String, ExecutableElement> getMembersInheritedFromClasses(ClassElement cla ssElt) => 111 Map<String, ExecutableElement> getMembersInheritedFromClasses(
112 ClassElement classElt) =>
109 _computeClassChainLookupMap(classElt, new HashSet<ClassElement>()); 113 _computeClassChainLookupMap(classElt, new HashSet<ClassElement>());
110 114
111 /** 115 /**
112 * Return a table mapping the string names of the members inherited from the 116 * Return a table mapping the string names of the members inherited from the
113 * passed [ClassElement]'s interface hierarchy, and the associated executable 117 * passed [ClassElement]'s interface hierarchy, and the associated executable
114 * element. 118 * element.
115 */ 119 */
116 Map<String, ExecutableElement> getMembersInheritedFromInterfaces(ClassElement classElt) => 120 Map<String, ExecutableElement> getMembersInheritedFromInterfaces(
121 ClassElement classElt) =>
117 _computeInterfaceLookupMap(classElt, new HashSet<ClassElement>()); 122 _computeInterfaceLookupMap(classElt, new HashSet<ClassElement>());
118 123
119 /** 124 /**
120 * Given some [ClassElement] and some member name, this returns the 125 * Given some [ClassElement] and some member name, this returns the
121 * [ExecutableElement] that the class inherits from the mixins, 126 * [ExecutableElement] that the class inherits from the mixins,
122 * superclasses or interfaces, that has the member name, if no member is inher ited `null` is 127 * superclasses or interfaces, that has the member name, if no member is inher ited `null` is
123 * returned. 128 * returned.
124 * 129 *
125 * @param classElt the class element to query 130 * @param classElt the class element to query
126 * @param memberName the name of the executable element to find and return 131 * @param memberName the name of the executable element to find and return
127 * @return the inherited executable element with the member name, or `null` if no such 132 * @return the inherited executable element with the member name, or `null` if no such
128 * member exists 133 * member exists
129 */ 134 */
130 ExecutableElement lookupInheritance( 135 ExecutableElement lookupInheritance(
131 ClassElement classElt, String memberName) { 136 ClassElement classElt, String memberName) {
132 if (memberName == null || memberName.isEmpty) { 137 if (memberName == null || memberName.isEmpty) {
133 return null; 138 return null;
134 } 139 }
135 ExecutableElement executable = 140 ExecutableElement executable = _computeClassChainLookupMap(
136 _computeClassChainLookupMap(classElt, new HashSet<ClassElement>()) 141 classElt, new HashSet<ClassElement>())[memberName];
137 [memberName];
138 if (executable == null) { 142 if (executable == null) {
139 return _computeInterfaceLookupMap(classElt, new HashSet<ClassElement>()) 143 return _computeInterfaceLookupMap(classElt, new HashSet<ClassElement>())[
140 [memberName]; 144 memberName];
141 } 145 }
142 return executable; 146 return executable;
143 } 147 }
144 148
145 /** 149 /**
146 * Given some [ClassElement] and some member name, this returns the 150 * Given some [ClassElement] and some member name, this returns the
147 * [ExecutableElement] that the class either declares itself, or 151 * [ExecutableElement] that the class either declares itself, or
148 * inherits, that has the member name, if no member is inherited `null` is ret urned. 152 * inherits, that has the member name, if no member is inherited `null` is ret urned.
149 * 153 *
150 * @param classElt the class element to query 154 * @param classElt the class element to query
(...skipping 25 matching lines...) Expand all
176 if (memberName == null || memberName.isEmpty) { 180 if (memberName == null || memberName.isEmpty) {
177 return result; 181 return result;
178 } 182 }
179 List<Map<String, ExecutableElement>> interfaceMaps = 183 List<Map<String, ExecutableElement>> interfaceMaps =
180 _gatherInterfaceLookupMaps(classElt, new HashSet<ClassElement>()); 184 _gatherInterfaceLookupMaps(classElt, new HashSet<ClassElement>());
181 if (interfaceMaps != null) { 185 if (interfaceMaps != null) {
182 for (Map<String, ExecutableElement> interfaceMap in interfaceMaps) { 186 for (Map<String, ExecutableElement> interfaceMap in interfaceMaps) {
183 ExecutableElement overriddenElement = interfaceMap[memberName]; 187 ExecutableElement overriddenElement = interfaceMap[memberName];
184 if (overriddenElement != null) { 188 if (overriddenElement != null) {
185 if (overriddenElement is MultiplyInheritedExecutableElement) { 189 if (overriddenElement is MultiplyInheritedExecutableElement) {
186 MultiplyInheritedExecutableElement multiplyInheritedElement =
187 overriddenElement;
188 for (ExecutableElement element 190 for (ExecutableElement element
189 in multiplyInheritedElement.inheritedElements) { 191 in overriddenElement.inheritedElements) {
190 result.add(element); 192 result.add(element);
191 } 193 }
192 } else { 194 } else {
193 result.add(overriddenElement); 195 result.add(overriddenElement);
194 } 196 }
195 } 197 }
196 } 198 }
197 } 199 }
198 return result; 200 return result;
199 } 201 }
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 // multiple mixins, visit them in the order listed so that methods in later 300 // multiple mixins, visit them in the order listed so that methods in later
299 // mixins will overwrite identically-named methods in earlier mixins. 301 // mixins will overwrite identically-named methods in earlier mixins.
300 // 302 //
301 List<InterfaceType> mixins = classElt.mixins; 303 List<InterfaceType> mixins = classElt.mixins;
302 for (InterfaceType mixin in mixins) { 304 for (InterfaceType mixin in mixins) {
303 ClassElement mixinElement = mixin.element; 305 ClassElement mixinElement = mixin.element;
304 if (mixinElement != null) { 306 if (mixinElement != null) {
305 if (!visitedClasses.contains(mixinElement)) { 307 if (!visitedClasses.contains(mixinElement)) {
306 visitedClasses.add(mixinElement); 308 visitedClasses.add(mixinElement);
307 try { 309 try {
308 Map<String, ExecutableElement> map = new Map<String, ExecutableEleme nt>.from( 310 Map<String, ExecutableElement> map =
309 _computeClassChainLookupMap(mixinElement, visitedClasses)); 311 new Map<String, ExecutableElement>.from(
312 _computeClassChainLookupMap(mixinElement, visitedClasses));
310 // 313 //
311 // Substitute the super types down the hierarchy. 314 // Substitute the super types down the hierarchy.
312 // 315 //
313 _substituteTypeParametersDownHierarchy(mixin, map); 316 _substituteTypeParametersDownHierarchy(mixin, map);
314 // 317 //
315 // Include the members from the superclass in the resultMap. 318 // Include the members from the superclass in the resultMap.
316 // 319 //
317 _recordMapWithClassMembers(map, mixin, false); 320 _recordMapWithClassMembers(map, mixin, false);
318 // 321 //
319 // Add the members from map into result map. 322 // Add the members from map into result map.
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 * lookup maps. 458 * lookup maps.
456 */ 459 */
457 List<Map<String, ExecutableElement>> _gatherInterfaceLookupMaps( 460 List<Map<String, ExecutableElement>> _gatherInterfaceLookupMaps(
458 ClassElement classElt, HashSet<ClassElement> visitedInterfaces) { 461 ClassElement classElt, HashSet<ClassElement> visitedInterfaces) {
459 InterfaceType supertype = classElt.supertype; 462 InterfaceType supertype = classElt.supertype;
460 ClassElement superclassElement = 463 ClassElement superclassElement =
461 supertype != null ? supertype.element : null; 464 supertype != null ? supertype.element : null;
462 List<InterfaceType> mixins = classElt.mixins; 465 List<InterfaceType> mixins = classElt.mixins;
463 List<InterfaceType> interfaces = classElt.interfaces; 466 List<InterfaceType> interfaces = classElt.interfaces;
464 // Recursively collect the list of mappings from all of the interface types 467 // Recursively collect the list of mappings from all of the interface types
465 List<Map<String, ExecutableElement>> lookupMaps = new List<Map<String, Execu tableElement>>(); 468 List<Map<String, ExecutableElement>> lookupMaps =
469 new List<Map<String, ExecutableElement>>();
466 // 470 //
467 // Superclass element 471 // Superclass element
468 // 472 //
469 if (superclassElement != null) { 473 if (superclassElement != null) {
470 if (!visitedInterfaces.contains(superclassElement)) { 474 if (!visitedInterfaces.contains(superclassElement)) {
471 try { 475 try {
472 visitedInterfaces.add(superclassElement); 476 visitedInterfaces.add(superclassElement);
473 // 477 //
474 // Recursively compute the map for the super type. 478 // Recursively compute the map for the super type.
475 // 479 //
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 599
596 /** 600 /**
597 * Record the passed map with the set of all members (methods, getters and set ters) in the type 601 * Record the passed map with the set of all members (methods, getters and set ters) in the type
598 * into the passed map. 602 * into the passed map.
599 * 603 *
600 * @param map some non-`null` map to put the methods and accessors from the pa ssed 604 * @param map some non-`null` map to put the methods and accessors from the pa ssed
601 * [ClassElement] into 605 * [ClassElement] into
602 * @param type the type that will be recorded into the passed map 606 * @param type the type that will be recorded into the passed map
603 * @param doIncludeAbstract `true` if abstract members will be put into the ma p 607 * @param doIncludeAbstract `true` if abstract members will be put into the ma p
604 */ 608 */
605 void _recordMapWithClassMembers( 609 void _recordMapWithClassMembers(Map<String, ExecutableElement> map,
606 Map<String, ExecutableElement> map, InterfaceType type, bool doIncludeAbst ract) { 610 InterfaceType type, bool doIncludeAbstract) {
607 List<MethodElement> methods = type.methods; 611 List<MethodElement> methods = type.methods;
608 for (MethodElement method in methods) { 612 for (MethodElement method in methods) {
609 if (method.isAccessibleIn(_library) && 613 if (method.isAccessibleIn(_library) &&
610 !method.isStatic && 614 !method.isStatic &&
611 (doIncludeAbstract || !method.isAbstract)) { 615 (doIncludeAbstract || !method.isAbstract)) {
612 map[method.name] = method; 616 map[method.name] = method;
613 } 617 }
614 } 618 }
615 List<PropertyAccessorElement> accessors = type.accessors; 619 List<PropertyAccessorElement> accessors = type.accessors;
616 for (PropertyAccessorElement accessor in accessors) { 620 for (PropertyAccessorElement accessor in accessors) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 * apply the appropriate inheritance rules to determine those methods inherite d by or overridden 653 * apply the appropriate inheritance rules to determine those methods inherite d by or overridden
650 * by [classElt]. Also report static warnings 654 * by [classElt]. Also report static warnings
651 * [StaticTypeWarningCode.INCONSISTENT_METHOD_INHERITANCE] and 655 * [StaticTypeWarningCode.INCONSISTENT_METHOD_INHERITANCE] and
652 * [StaticWarningCode.INCONSISTENT_METHOD_INHERITANCE_GETTER_AND_METHOD] if ap propriate. 656 * [StaticWarningCode.INCONSISTENT_METHOD_INHERITANCE_GETTER_AND_METHOD] if ap propriate.
653 * 657 *
654 * @param classElt the class element to query. 658 * @param classElt the class element to query.
655 * @param unionMap a mapping from method name to the set of unique (in terms o f signature) methods 659 * @param unionMap a mapping from method name to the set of unique (in terms o f signature) methods
656 * defined in superclasses of [classElt]. 660 * defined in superclasses of [classElt].
657 * @return the inheritance lookup map for [classElt]. 661 * @return the inheritance lookup map for [classElt].
658 */ 662 */
659 Map<String, ExecutableElement> _resolveInheritanceLookup(ClassElement classElt , 663 Map<String, ExecutableElement> _resolveInheritanceLookup(
660 Map<String, List<ExecutableElement>> unionMap) { 664 ClassElement classElt, Map<String, List<ExecutableElement>> unionMap) {
661 Map<String, ExecutableElement> resultMap = new Map<String, ExecutableElement >(); 665 Map<String, ExecutableElement> resultMap =
666 new Map<String, ExecutableElement>();
662 unionMap.forEach((String key, List<ExecutableElement> list) { 667 unionMap.forEach((String key, List<ExecutableElement> list) {
663 int numOfEltsWithMatchingNames = list.length; 668 int numOfEltsWithMatchingNames = list.length;
664 if (numOfEltsWithMatchingNames == 1) { 669 if (numOfEltsWithMatchingNames == 1) {
665 // 670 //
666 // Example: class A inherits only 1 method named 'm'. 671 // Example: class A inherits only 1 method named 'm'.
667 // Since it is the only such method, it is inherited. 672 // Since it is the only such method, it is inherited.
668 // Another example: class A inherits 2 methods named 'm' from 2 673 // Another example: class A inherits 2 methods named 'm' from 2
669 // different interfaces, but they both have the same signature, so it is 674 // different interfaces, but they both have the same signature, so it is
670 // the method inherited. 675 // the method inherited.
671 // 676 //
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 static ExecutableElement _createSyntheticExecutableElement( 943 static ExecutableElement _createSyntheticExecutableElement(
939 List<ExecutableElement> elementArrayToMerge, 944 List<ExecutableElement> elementArrayToMerge,
940 String name, 945 String name,
941 int numOfRequiredParameters, 946 int numOfRequiredParameters,
942 int numOfPositionalParameters, 947 int numOfPositionalParameters,
943 List<String> namedParameters) { 948 List<String> namedParameters) {
944 DynamicTypeImpl dynamicType = DynamicTypeImpl.instance; 949 DynamicTypeImpl dynamicType = DynamicTypeImpl.instance;
945 SimpleIdentifier nameIdentifier = 950 SimpleIdentifier nameIdentifier =
946 new SimpleIdentifier(new StringToken(TokenType.IDENTIFIER, name, 0)); 951 new SimpleIdentifier(new StringToken(TokenType.IDENTIFIER, name, 0));
947 ExecutableElementImpl executable; 952 ExecutableElementImpl executable;
948 if (elementArrayToMerge[0] is MethodElement) { 953 ExecutableElement elementToMerge = elementArrayToMerge[0];
954 if (elementToMerge is MethodElement) {
949 MultiplyInheritedMethodElementImpl unionedMethod = 955 MultiplyInheritedMethodElementImpl unionedMethod =
950 new MultiplyInheritedMethodElementImpl(nameIdentifier); 956 new MultiplyInheritedMethodElementImpl(nameIdentifier);
951 unionedMethod.inheritedElements = elementArrayToMerge; 957 unionedMethod.inheritedElements = elementArrayToMerge;
952 executable = unionedMethod; 958 executable = unionedMethod;
953 } else { 959 } else {
954 MultiplyInheritedPropertyAccessorElementImpl unionedPropertyAccessor = 960 MultiplyInheritedPropertyAccessorElementImpl unionedPropertyAccessor =
955 new MultiplyInheritedPropertyAccessorElementImpl(nameIdentifier); 961 new MultiplyInheritedPropertyAccessorElementImpl(nameIdentifier);
956 unionedPropertyAccessor.getter = 962 unionedPropertyAccessor.getter =
957 (elementArrayToMerge[0] as PropertyAccessorElement).isGetter; 963 (elementToMerge as PropertyAccessorElement).isGetter;
958 unionedPropertyAccessor.setter = 964 unionedPropertyAccessor.setter =
959 (elementArrayToMerge[0] as PropertyAccessorElement).isSetter; 965 (elementToMerge as PropertyAccessorElement).isSetter;
960 unionedPropertyAccessor.inheritedElements = elementArrayToMerge; 966 unionedPropertyAccessor.inheritedElements = elementArrayToMerge;
961 executable = unionedPropertyAccessor; 967 executable = unionedPropertyAccessor;
962 } 968 }
963 int numOfParameters = numOfRequiredParameters + 969 int numOfParameters = numOfRequiredParameters +
964 numOfPositionalParameters + 970 numOfPositionalParameters +
965 namedParameters.length; 971 namedParameters.length;
966 List<ParameterElement> parameters = 972 List<ParameterElement> parameters =
967 new List<ParameterElement>(numOfParameters); 973 new List<ParameterElement>(numOfParameters);
968 int i = 0; 974 int i = 0;
969 for (int j = 0; j < numOfRequiredParameters; j++, i++) { 975 for (int j = 0; j < numOfRequiredParameters; j++, i++) {
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
1225 } 1231 }
1226 1232
1227 /** 1233 /**
1228 * Initializes [keys] and [values]. 1234 * Initializes [keys] and [values].
1229 */ 1235 */
1230 void _initArrays(int initialCapacity) { 1236 void _initArrays(int initialCapacity) {
1231 _keys = new List<String>(initialCapacity); 1237 _keys = new List<String>(initialCapacity);
1232 _values = new List<ExecutableElement>(initialCapacity); 1238 _values = new List<ExecutableElement>(initialCapacity);
1233 } 1239 }
1234 } 1240 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/element.dart ('k') | pkg/analyzer/lib/src/dart/resolver/scope.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698