OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 analyzer.src.generated.static_type_analyzer; | 5 library analyzer.src.generated.static_type_analyzer; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/ast/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
(...skipping 1887 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1898 | 1898 |
1899 /** | 1899 /** |
1900 * Given a possibly generic invocation like `o.m(args)` or `(f)(args)` try to | 1900 * Given a possibly generic invocation like `o.m(args)` or `(f)(args)` try to |
1901 * infer the instantiated generic function type. | 1901 * infer the instantiated generic function type. |
1902 * | 1902 * |
1903 * This takes into account both the context type, as well as information from | 1903 * This takes into account both the context type, as well as information from |
1904 * the argument types. | 1904 * the argument types. |
1905 */ | 1905 */ |
1906 void _inferGenericInvocationExpression(InvocationExpression node) { | 1906 void _inferGenericInvocationExpression(InvocationExpression node) { |
1907 ArgumentList arguments = node.argumentList; | 1907 ArgumentList arguments = node.argumentList; |
1908 FunctionType inferred = _inferGenericInvoke( | 1908 FunctionType inferred = _inferGenericInvoke(node, node.function.staticType, |
1909 node, node.function.staticType, node.typeArguments, arguments); | 1909 node.typeArguments, arguments, node.function); |
1910 if (inferred != null && inferred != node.staticInvokeType) { | 1910 if (inferred != null && inferred != node.staticInvokeType) { |
1911 // Fix up the parameter elements based on inferred method. | 1911 // Fix up the parameter elements based on inferred method. |
1912 arguments.correspondingStaticParameters = ResolverVisitor | 1912 arguments.correspondingStaticParameters = ResolverVisitor |
1913 .resolveArgumentsToParameters(arguments, inferred.parameters, null); | 1913 .resolveArgumentsToParameters(arguments, inferred.parameters, null); |
1914 node.staticInvokeType = inferred; | 1914 node.staticInvokeType = inferred; |
1915 } | 1915 } |
1916 } | 1916 } |
1917 | 1917 |
1918 /** | 1918 /** |
1919 * Given a possibly generic invocation or instance creation, such as | 1919 * Given a possibly generic invocation or instance creation, such as |
1920 * `o.m(args)` or `(f)(args)` or `new T(args)` try to infer the instantiated | 1920 * `o.m(args)` or `(f)(args)` or `new T(args)` try to infer the instantiated |
1921 * generic function type. | 1921 * generic function type. |
1922 * | 1922 * |
1923 * This takes into account both the context type, as well as information from | 1923 * This takes into account both the context type, as well as information from |
1924 * the argument types. | 1924 * the argument types. |
1925 */ | 1925 */ |
1926 FunctionType _inferGenericInvoke(Expression node, DartType fnType, | 1926 FunctionType _inferGenericInvoke( |
1927 TypeArgumentList typeArguments, ArgumentList argumentList) { | 1927 Expression node, |
| 1928 DartType fnType, |
| 1929 TypeArgumentList typeArguments, |
| 1930 ArgumentList argumentList, |
| 1931 AstNode errorNode) { |
1928 TypeSystem ts = _typeSystem; | 1932 TypeSystem ts = _typeSystem; |
1929 if (typeArguments == null && | 1933 if (typeArguments == null && |
1930 fnType is FunctionType && | 1934 fnType is FunctionType && |
1931 fnType.typeFormals.isNotEmpty && | 1935 fnType.typeFormals.isNotEmpty && |
1932 ts is StrongTypeSystemImpl) { | 1936 ts is StrongTypeSystemImpl) { |
1933 // Get the parameters that correspond to the uninstantiated generic. | 1937 // Get the parameters that correspond to the uninstantiated generic. |
1934 List<ParameterElement> rawParameters = ResolverVisitor | 1938 List<ParameterElement> rawParameters = ResolverVisitor |
1935 .resolveArgumentsToParameters(argumentList, fnType.parameters, null); | 1939 .resolveArgumentsToParameters(argumentList, fnType.parameters, null); |
1936 | 1940 |
1937 List<DartType> paramTypes = <DartType>[]; | 1941 List<DartType> paramTypes = <DartType>[]; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1975 ..synthetic = true | 1979 ..synthetic = true |
1976 ..shareParameters(firstParamType.parameters) | 1980 ..shareParameters(firstParamType.parameters) |
1977 ..returnType = paramReturnType; | 1981 ..returnType = paramReturnType; |
1978 function.type = new FunctionTypeImpl(function); | 1982 function.type = new FunctionTypeImpl(function); |
1979 // Use this as the expected 1st parameter type. | 1983 // Use this as the expected 1st parameter type. |
1980 paramTypes[0] = function.type; | 1984 paramTypes[0] = function.type; |
1981 } | 1985 } |
1982 } | 1986 } |
1983 } | 1987 } |
1984 return ts.inferGenericFunctionCall(_typeProvider, fnType, paramTypes, | 1988 return ts.inferGenericFunctionCall(_typeProvider, fnType, paramTypes, |
1985 argTypes, InferenceContext.getContext(node)); | 1989 argTypes, InferenceContext.getContext(node), |
| 1990 errorReporter: _resolver.errorReporter, errorNode: errorNode); |
1986 } | 1991 } |
1987 return null; | 1992 return null; |
1988 } | 1993 } |
1989 | 1994 |
1990 /** | 1995 /** |
1991 * Given an instance creation of a possibly generic type, infer the type | 1996 * Given an instance creation of a possibly generic type, infer the type |
1992 * arguments using the current context type as well as the argument types. | 1997 * arguments using the current context type as well as the argument types. |
1993 */ | 1998 */ |
1994 void _inferInstanceCreationExpression(InstanceCreationExpression node) { | 1999 void _inferInstanceCreationExpression(InstanceCreationExpression node) { |
1995 ConstructorName constructor = node.constructorName; | 2000 ConstructorName constructor = node.constructorName; |
(...skipping 17 matching lines...) Expand all Loading... |
2013 | 2018 |
2014 // Get back to the uninstantiated generic constructor. | 2019 // Get back to the uninstantiated generic constructor. |
2015 // TODO(jmesserly): should we store this earlier in resolution? | 2020 // TODO(jmesserly): should we store this earlier in resolution? |
2016 // Or look it up, instead of jumping backwards through the Member? | 2021 // Or look it up, instead of jumping backwards through the Member? |
2017 var rawElement = (originalElement as ConstructorMember).baseElement; | 2022 var rawElement = (originalElement as ConstructorMember).baseElement; |
2018 | 2023 |
2019 FunctionType constructorType = | 2024 FunctionType constructorType = |
2020 _constructorToGenericFunctionType(rawElement); | 2025 _constructorToGenericFunctionType(rawElement); |
2021 | 2026 |
2022 ArgumentList arguments = node.argumentList; | 2027 ArgumentList arguments = node.argumentList; |
2023 FunctionType inferred = _inferGenericInvoke( | 2028 FunctionType inferred = _inferGenericInvoke(node, constructorType, |
2024 node, constructorType, constructor.type.typeArguments, arguments); | 2029 constructor.type.typeArguments, arguments, node.constructorName); |
2025 | 2030 |
2026 if (inferred != null && inferred != originalElement.type) { | 2031 if (inferred != null && inferred != originalElement.type) { |
2027 // Fix up the parameter elements based on inferred method. | 2032 // Fix up the parameter elements based on inferred method. |
2028 arguments.correspondingStaticParameters = ResolverVisitor | 2033 arguments.correspondingStaticParameters = ResolverVisitor |
2029 .resolveArgumentsToParameters(arguments, inferred.parameters, null); | 2034 .resolveArgumentsToParameters(arguments, inferred.parameters, null); |
2030 inferConstructorName(constructor, inferred.returnType); | 2035 inferConstructorName(constructor, inferred.returnType); |
2031 // Update the static element as well. This is used in some cases, such as | 2036 // Update the static element as well. This is used in some cases, such as |
2032 // computing constant values. It is stored in two places. | 2037 // computing constant values. It is stored in two places. |
2033 constructor.staticElement = | 2038 constructor.staticElement = |
2034 ConstructorMember.from(rawElement, inferred.returnType); | 2039 ConstructorMember.from(rawElement, inferred.returnType); |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2402 } | 2407 } |
2403 // merge types | 2408 // merge types |
2404 if (result == null) { | 2409 if (result == null) { |
2405 result = type; | 2410 result = type; |
2406 } else { | 2411 } else { |
2407 result = _typeSystem.getLeastUpperBound(_typeProvider, result, type); | 2412 result = _typeSystem.getLeastUpperBound(_typeProvider, result, type); |
2408 } | 2413 } |
2409 return null; | 2414 return null; |
2410 } | 2415 } |
2411 } | 2416 } |
OLD | NEW |