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

Side by Side Diff: pkg/analyzer/lib/src/summary/link.dart

Issue 1899643003: Link invocation of method references, with inference. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/resynthesize_ast_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 /** 5 /**
6 * This library is capable of producing linked summaries from unlinked 6 * This library is capable of producing linked summaries from unlinked
7 * ones (or prelinked ones). It functions by building a miniature 7 * ones (or prelinked ones). It functions by building a miniature
8 * element model to represent the contents of the summaries, and then 8 * element model to represent the contents of the summaries, and then
9 * scanning the element model to gather linked information and adding 9 * scanning the element model to gather linked information and adding
10 * it to the summary data structures. 10 * it to the summary data structures.
(...skipping 2024 matching lines...) Expand 10 before | Expand all | Expand 10 after
2035 stack.add(DynamicTypeImpl.instance); 2035 stack.add(DynamicTypeImpl.instance);
2036 } 2036 }
2037 } 2037 }
2038 2038
2039 void _doInvokeMethod() { 2039 void _doInvokeMethod() {
2040 int numNamed = unlinkedConst.ints[intPtr++]; 2040 int numNamed = unlinkedConst.ints[intPtr++];
2041 int numPositional = unlinkedConst.ints[intPtr++]; 2041 int numPositional = unlinkedConst.ints[intPtr++];
2042 List<String> namedArgNames = _getNextStrings(numNamed); 2042 List<String> namedArgNames = _getNextStrings(numNamed);
2043 List<DartType> namedArgTypeList = _popList(numNamed); 2043 List<DartType> namedArgTypeList = _popList(numNamed);
2044 List<DartType> positionalArgTypes = _popList(numPositional); 2044 List<DartType> positionalArgTypes = _popList(numPositional);
2045 // TODO(scheglov) if we pushed target and method name first, we might be
2046 // able to move work with arguments in _inferExecutableType()
2045 String methodName = _getNextString(); 2047 String methodName = _getNextString();
2046 DartType target = stack.removeLast(); 2048 DartType target = stack.removeLast();
2047 stack.add(() { 2049 stack.add(() {
2048 if (target is InterfaceType) { 2050 if (target is InterfaceType) {
2049 MethodElement method = target.lookUpMethod(methodName, library); 2051 MethodElement method = target.lookUpMethod(methodName, library);
2050 DartType rawMethodType = method?.type; 2052 FunctionType rawType = method?.type;
2051 TypeSystem ts = linker.typeSystem; 2053 FunctionType inferredType = _inferExecutableType(rawType, numNamed,
2052 if (rawMethodType is FunctionType) { 2054 numPositional, namedArgNames, namedArgTypeList, positionalArgTypes);
2053 if (rawMethodType.typeFormals.isNotEmpty && 2055 if (inferredType != null) {
2054 ts is StrongTypeSystemImpl) { 2056 return inferredType.returnType;
2055 List<DartType> paramTypes = <DartType>[];
2056 List<DartType> argTypes = <DartType>[];
2057 // Add positional parameter and argument types.
2058 for (int i = 0; i < numPositional; i++) {
2059 ParameterElement parameter = rawMethodType.parameters[i];
2060 if (parameter != null) {
2061 paramTypes.add(parameter.type);
2062 argTypes.add(positionalArgTypes[i]);
2063 }
2064 }
2065 // Prepare named argument types map.
2066 Map<String, DartType> namedArgTypes = <String, DartType>{};
2067 for (int i = 0; i < numNamed; i++) {
2068 String name = namedArgNames[i];
2069 DartType type = namedArgTypeList[i];
2070 namedArgTypes[name] = type;
2071 }
2072 // Add named parameter and argument types.
2073 Map<String, DartType> namedParameterTypes =
2074 rawMethodType.namedParameterTypes;
2075 namedArgTypes.forEach((String name, DartType argType) {
2076 DartType parameterType = namedParameterTypes[name];
2077 if (parameterType != null) {
2078 paramTypes.add(parameterType);
2079 argTypes.add(argType);
2080 }
2081 });
2082 // Perform inference.
2083 FunctionType inferred = ts.inferGenericFunctionCall(
2084 typeProvider, rawMethodType, paramTypes, argTypes, null);
2085 return inferred.returnType;
2086 }
2087 // Not a generic method, use the raw return type.
2088 return rawMethodType.returnType;
2089 } 2057 }
2090 } 2058 }
2091 return DynamicTypeImpl.instance; 2059 return DynamicTypeImpl.instance;
2092 }()); 2060 }());
2093 } 2061 }
2094 2062
2095 void _doInvokeMethodRef() { 2063 void _doInvokeMethodRef() {
2096 int numNamed = _getNextInt(); 2064 int numNamed = _getNextInt();
2097 int numPositional = _getNextInt(); 2065 int numPositional = _getNextInt();
2098 // TODO(paulberry): don't just pop the args; use their types 2066 List<String> namedArgNames = _getNextStrings(numNamed);
2099 // to infer the type of type arguments. 2067 List<DartType> namedArgTypeList = _popList(numNamed);
2100 stack.length -= numNamed + numPositional; 2068 List<DartType> positionalArgTypes = _popList(numPositional);
2101 strPtr += numNamed; 2069 EntityRef ref = unlinkedConst.references[refPtr++];
2102 refPtr++; 2070 ReferenceableElementForLink element = unit._resolveRef(ref.reference);
2103 // TODO(paulberry): implement. 2071 stack.add(() {
2104 stack.add(DynamicTypeImpl.instance); 2072 DartType rawType = element.asStaticType;
2073 if (rawType is FunctionType) {
2074 FunctionType inferredType = _inferExecutableType(rawType, numNamed,
2075 numPositional, namedArgNames, namedArgTypeList, positionalArgTypes);
2076 if (inferredType != null) {
2077 return inferredType.returnType;
2078 }
2079 }
2080 return DynamicTypeImpl.instance;
2081 }());
2105 } 2082 }
2106 2083
2107 void _doMakeTypedList() { 2084 void _doMakeTypedList() {
2108 DartType itemType = _getNextTypeRef(); 2085 DartType itemType = _getNextTypeRef();
2109 stack.length -= _getNextInt(); 2086 stack.length -= _getNextInt();
2110 stack.add(typeProvider.listType.instantiate(<DartType>[itemType])); 2087 stack.add(typeProvider.listType.instantiate(<DartType>[itemType]));
2111 } 2088 }
2112 2089
2113 void _doMakeTypeMap() { 2090 void _doMakeTypeMap() {
2114 DartType keyType = _getNextTypeRef(); 2091 DartType keyType = _getNextTypeRef();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
2187 * Return the type of the property with the given [propertyName] in the 2164 * Return the type of the property with the given [propertyName] in the
2188 * given [targetType]. May return `dynamic` if the property cannot be 2165 * given [targetType]. May return `dynamic` if the property cannot be
2189 * resolved. 2166 * resolved.
2190 */ 2167 */
2191 DartType _getPropertyType(DartType targetType, String propertyName) { 2168 DartType _getPropertyType(DartType targetType, String propertyName) {
2192 return targetType is InterfaceType 2169 return targetType is InterfaceType
2193 ? targetType.lookUpGetter(propertyName, library)?.returnType 2170 ? targetType.lookUpGetter(propertyName, library)?.returnType
2194 : DynamicTypeImpl.instance; 2171 : DynamicTypeImpl.instance;
2195 } 2172 }
2196 2173
2174 FunctionType _inferExecutableType(
2175 FunctionType rawMethodType,
2176 int numNamed,
2177 int numPositional,
2178 List<String> namedArgNames,
2179 List<DartType> namedArgTypeList,
2180 List<DartType> positionalArgTypes) {
2181 TypeSystem ts = linker.typeSystem;
2182 if (rawMethodType != null) {
2183 if (rawMethodType.typeFormals.isNotEmpty && ts is StrongTypeSystemImpl) {
2184 List<DartType> paramTypes = <DartType>[];
2185 List<DartType> argTypes = <DartType>[];
2186 // Add positional parameter and argument types.
2187 for (int i = 0; i < numPositional; i++) {
2188 ParameterElement parameter = rawMethodType.parameters[i];
2189 if (parameter != null) {
2190 paramTypes.add(parameter.type);
2191 argTypes.add(positionalArgTypes[i]);
2192 }
2193 }
2194 // Prepare named argument types map.
2195 Map<String, DartType> namedArgTypes = <String, DartType>{};
2196 for (int i = 0; i < numNamed; i++) {
2197 String name = namedArgNames[i];
2198 DartType type = namedArgTypeList[i];
2199 namedArgTypes[name] = type;
2200 }
2201 // Add named parameter and argument types.
2202 Map<String, DartType> namedParameterTypes =
2203 rawMethodType.namedParameterTypes;
2204 namedArgTypes.forEach((String name, DartType argType) {
2205 DartType parameterType = namedParameterTypes[name];
2206 if (parameterType != null) {
2207 paramTypes.add(parameterType);
2208 argTypes.add(argType);
2209 }
2210 });
2211 // Perform inference.
2212 FunctionType inferred = ts.inferGenericFunctionCall(
2213 typeProvider, rawMethodType, paramTypes, argTypes, null);
2214 return inferred;
2215 }
2216 }
2217 // Not a generic function type, use the raw type.
2218 return rawMethodType;
2219 }
2220
2197 DartType _leastUpperBound(DartType s, DartType t) { 2221 DartType _leastUpperBound(DartType s, DartType t) {
2198 return linker.typeSystem.getLeastUpperBound(typeProvider, s, t); 2222 return linker.typeSystem.getLeastUpperBound(typeProvider, s, t);
2199 } 2223 }
2200 2224
2201 List<DartType> _popList(int n) { 2225 List<DartType> _popList(int n) {
2202 List<DartType> result = stack.sublist(stack.length - n, stack.length); 2226 List<DartType> result = stack.sublist(stack.length - n, stack.length);
2203 stack.length -= n; 2227 stack.length -= n;
2204 return result; 2228 return result;
2205 } 2229 }
2206 2230
(...skipping 1970 matching lines...) Expand 10 before | Expand all | Expand 10 after
4177 if (type is InterfaceType) { 4201 if (type is InterfaceType) {
4178 Element result = type.lookUpGetter(name, compilationUnit.library); 4202 Element result = type.lookUpGetter(name, compilationUnit.library);
4179 result ??= type.lookUpMethod(name, compilationUnit.library); 4203 result ??= type.lookUpMethod(name, compilationUnit.library);
4180 return result; 4204 return result;
4181 } 4205 }
4182 } 4206 }
4183 // TODO(scheglov): implement for propagated types 4207 // TODO(scheglov): implement for propagated types
4184 return null; 4208 return null;
4185 } 4209 }
4186 } 4210 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/resynthesize_ast_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698