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 engine.resolver; | 5 library engine.resolver; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/src/generated/scanner.dart'; | 9 import 'package:analyzer/src/generated/scanner.dart'; |
10 | 10 |
(...skipping 5834 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5845 */ | 5845 */ |
5846 ExecutableElement lookupMember(ClassElement classElt, String memberName) { | 5846 ExecutableElement lookupMember(ClassElement classElt, String memberName) { |
5847 ExecutableElement element = _lookupMemberInClass(classElt, memberName); | 5847 ExecutableElement element = _lookupMemberInClass(classElt, memberName); |
5848 if (element != null) { | 5848 if (element != null) { |
5849 return element; | 5849 return element; |
5850 } | 5850 } |
5851 return lookupInheritance(classElt, memberName); | 5851 return lookupInheritance(classElt, memberName); |
5852 } | 5852 } |
5853 | 5853 |
5854 /** | 5854 /** |
5855 * Given some [InterfaceType] and some member name, this returns the | |
5856 * [FunctionType] of the [ExecutableElement] that the | |
5857 * class either declares itself, or inherits, that has the member name, if no
member is inherited | |
5858 * `null` is returned. The returned [FunctionType] has all type | |
5859 * parameters substituted with corresponding type arguments from the given [In
terfaceType]. | |
5860 * | |
5861 * @param interfaceType the interface type to query | |
5862 * @param memberName the name of the executable element to find and return | |
5863 * @return the member's function type, or `null` if no such member exists | |
5864 */ | |
5865 FunctionType lookupMemberType( | |
5866 InterfaceType interfaceType, String memberName) { | |
5867 ExecutableElement iteratorMember = | |
5868 lookupMember(interfaceType.element, memberName); | |
5869 if (iteratorMember == null) { | |
5870 return null; | |
5871 } | |
5872 return substituteTypeArgumentsInMemberFromInheritance( | |
5873 iteratorMember.type, memberName, interfaceType); | |
5874 } | |
5875 | |
5876 /** | |
5877 * Determine the set of methods which is overridden by the given class member.
If no member is | 5855 * Determine the set of methods which is overridden by the given class member.
If no member is |
5878 * inherited, an empty list is returned. If one of the inherited members is a | 5856 * inherited, an empty list is returned. If one of the inherited members is a |
5879 * [MultiplyInheritedExecutableElement], then it is expanded into its constitu
ent inherited | 5857 * [MultiplyInheritedExecutableElement], then it is expanded into its constitu
ent inherited |
5880 * elements. | 5858 * elements. |
5881 * | 5859 * |
5882 * @param classElt the class to query | 5860 * @param classElt the class to query |
5883 * @param memberName the name of the class member to query | 5861 * @param memberName the name of the class member to query |
5884 * @return a list of overridden methods | 5862 * @return a list of overridden methods |
5885 */ | 5863 */ |
5886 List<ExecutableElement> lookupOverrides( | 5864 List<ExecutableElement> lookupOverrides( |
(...skipping 27 matching lines...) Expand all Loading... |
5914 /** | 5892 /** |
5915 * This method takes some inherited [FunctionType], and resolves all the param
eterized types | 5893 * This method takes some inherited [FunctionType], and resolves all the param
eterized types |
5916 * in the function type, dependent on the class in which it is being overridde
n. | 5894 * in the function type, dependent on the class in which it is being overridde
n. |
5917 * | 5895 * |
5918 * @param baseFunctionType the function type that is being overridden | 5896 * @param baseFunctionType the function type that is being overridden |
5919 * @param memberName the name of the member, this is used to lookup the inheri
tance path of the | 5897 * @param memberName the name of the member, this is used to lookup the inheri
tance path of the |
5920 * override | 5898 * override |
5921 * @param definingType the type that is overriding the member | 5899 * @param definingType the type that is overriding the member |
5922 * @return the passed function type with any parameterized types substituted | 5900 * @return the passed function type with any parameterized types substituted |
5923 */ | 5901 */ |
| 5902 // TODO(jmesserly): investigate why this is needed in ErrorVerifier's override |
| 5903 // checking. There seems to be some rare cases where we get partially |
| 5904 // substituted type arguments, and the function types don't compare equally. |
5924 FunctionType substituteTypeArgumentsInMemberFromInheritance( | 5905 FunctionType substituteTypeArgumentsInMemberFromInheritance( |
5925 FunctionType baseFunctionType, | 5906 FunctionType baseFunctionType, |
5926 String memberName, | 5907 String memberName, |
5927 InterfaceType definingType) { | 5908 InterfaceType definingType) { |
5928 // if the baseFunctionType is null, or does not have any parameters, | 5909 // if the baseFunctionType is null, or does not have any parameters, |
5929 // return it. | 5910 // return it. |
5930 if (baseFunctionType == null || | 5911 if (baseFunctionType == null || |
5931 baseFunctionType.typeArguments.length == 0) { | 5912 baseFunctionType.typeArguments.length == 0) { |
5932 return baseFunctionType; | 5913 return baseFunctionType; |
5933 } | 5914 } |
(...skipping 5651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11585 * The given expression is the expression used to compute the iterator for a | 11566 * The given expression is the expression used to compute the iterator for a |
11586 * for-each statement. Attempt to compute the type of objects that will be | 11567 * for-each statement. Attempt to compute the type of objects that will be |
11587 * assigned to the loop variable and return that type. Return `null` if the | 11568 * assigned to the loop variable and return that type. Return `null` if the |
11588 * type could not be determined. The [iteratorExpression] is the expression | 11569 * type could not be determined. The [iteratorExpression] is the expression |
11589 * that will return the Iterable being iterated over. | 11570 * that will return the Iterable being iterated over. |
11590 */ | 11571 */ |
11591 DartType _getIteratorElementType(Expression iteratorExpression) { | 11572 DartType _getIteratorElementType(Expression iteratorExpression) { |
11592 DartType expressionType = iteratorExpression.bestType; | 11573 DartType expressionType = iteratorExpression.bestType; |
11593 if (expressionType is InterfaceType) { | 11574 if (expressionType is InterfaceType) { |
11594 InterfaceType interfaceType = expressionType; | 11575 InterfaceType interfaceType = expressionType; |
11595 FunctionType iteratorFunction = | 11576 PropertyAccessorElement iteratorFunction = |
11596 _inheritanceManager.lookupMemberType(interfaceType, "iterator"); | 11577 interfaceType.lookUpInheritedGetter("iterator"); |
11597 if (iteratorFunction == null) { | 11578 if (iteratorFunction == null) { |
11598 // TODO(brianwilkerson) Should we report this error? | 11579 // TODO(brianwilkerson) Should we report this error? |
11599 return null; | 11580 return null; |
11600 } | 11581 } |
11601 DartType iteratorType = iteratorFunction.returnType; | 11582 DartType iteratorType = iteratorFunction.returnType; |
11602 if (iteratorType is InterfaceType) { | 11583 if (iteratorType is InterfaceType) { |
11603 InterfaceType iteratorInterfaceType = iteratorType; | 11584 InterfaceType iteratorInterfaceType = iteratorType; |
11604 FunctionType currentFunction = _inheritanceManager.lookupMemberType( | 11585 PropertyAccessorElement currentFunction = |
11605 iteratorInterfaceType, "current"); | 11586 iteratorInterfaceType.lookUpInheritedGetter("current"); |
11606 if (currentFunction == null) { | 11587 if (currentFunction == null) { |
11607 // TODO(brianwilkerson) Should we report this error? | 11588 // TODO(brianwilkerson) Should we report this error? |
11608 return null; | 11589 return null; |
11609 } | 11590 } |
11610 return currentFunction.returnType; | 11591 return currentFunction.returnType; |
11611 } | 11592 } |
11612 } | 11593 } |
11613 return null; | 11594 return null; |
11614 } | 11595 } |
11615 | 11596 |
11616 /** | 11597 /** |
11617 * The given expression is the expression used to compute the stream for an | 11598 * The given expression is the expression used to compute the stream for an |
11618 * asyncronous for-each statement. Attempt to compute the type of objects that | 11599 * asynchronous for-each statement. Attempt to compute the type of objects |
11619 * will be assigned to the loop variable and return that type. Return `null` | 11600 * that will be assigned to the loop variable and return that type. |
11620 * if the type could not be determined. The [streamExpression] is the | 11601 * Return `null` if the type could not be determined. The [streamExpression] |
11621 * expression that will return the stream being iterated over. | 11602 * is the expression that will return the stream being iterated over. |
11622 */ | 11603 */ |
11623 DartType _getStreamElementType(Expression streamExpression) { | 11604 DartType _getStreamElementType(Expression streamExpression) { |
11624 DartType streamType = streamExpression.bestType; | 11605 DartType streamType = streamExpression.bestType; |
11625 if (streamType is InterfaceType) { | 11606 if (streamType is InterfaceType) { |
11626 FunctionType listenFunction = | 11607 MethodElement listenFunction = streamType.lookUpInheritedMethod("listen"); |
11627 _inheritanceManager.lookupMemberType(streamType, "listen"); | |
11628 if (listenFunction == null) { | 11608 if (listenFunction == null) { |
11629 return null; | 11609 return null; |
11630 } | 11610 } |
11631 List<ParameterElement> listenParameters = listenFunction.parameters; | 11611 List<ParameterElement> listenParameters = listenFunction.parameters; |
11632 if (listenParameters == null || listenParameters.length < 1) { | 11612 if (listenParameters == null || listenParameters.length < 1) { |
11633 return null; | 11613 return null; |
11634 } | 11614 } |
11635 DartType onDataType = listenParameters[0].type; | 11615 DartType onDataType = listenParameters[0].type; |
11636 if (onDataType is FunctionType) { | 11616 if (onDataType is FunctionType) { |
11637 List<ParameterElement> onDataParameters = onDataType.parameters; | 11617 List<ParameterElement> onDataParameters = onDataType.parameters; |
11638 if (onDataParameters == null || onDataParameters.length < 1) { | 11618 if (onDataParameters == null || onDataParameters.isEmpty) { |
11639 return null; | 11619 return null; |
11640 } | 11620 } |
11641 DartType eventType = onDataParameters[0].type; | 11621 return onDataParameters[0].type; |
11642 // TODO(paulberry): checking that typeParameters.isNotEmpty is a | |
11643 // band-aid fix for dartbug.com/24191. Figure out what the correct | |
11644 // logic should be. | |
11645 if (streamType.typeParameters.isNotEmpty && | |
11646 eventType.element == streamType.typeParameters[0]) { | |
11647 return streamType.typeArguments[0]; | |
11648 } | |
11649 } | 11622 } |
11650 } | 11623 } |
11651 return null; | 11624 return null; |
11652 } | 11625 } |
11653 | 11626 |
11654 /** | 11627 /** |
11655 * If given "mayBeClosure" is [FunctionExpression] without explicit parameters
types and its | 11628 * If given "mayBeClosure" is [FunctionExpression] without explicit parameters
types and its |
11656 * required type is [FunctionType], then infer parameters types from [Function
Type]. | 11629 * required type is [FunctionType], then infer parameters types from [Function
Type]. |
11657 */ | 11630 */ |
11658 void _inferFunctionExpressionParametersTypes( | 11631 void _inferFunctionExpressionParametersTypes( |
(...skipping 1215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12874 return false; | 12847 return false; |
12875 } | 12848 } |
12876 | 12849 |
12877 @override | 12850 @override |
12878 bool isSubtypeOf(DartType leftType, DartType rightType) { | 12851 bool isSubtypeOf(DartType leftType, DartType rightType) { |
12879 return _isSubtypeOf(leftType, rightType, null); | 12852 return _isSubtypeOf(leftType, rightType, null); |
12880 } | 12853 } |
12881 | 12854 |
12882 FunctionType _getCallMethodType(DartType t) { | 12855 FunctionType _getCallMethodType(DartType t) { |
12883 if (t is InterfaceType) { | 12856 if (t is InterfaceType) { |
12884 ClassElement element = t.element; | 12857 return t.lookUpInheritedMethod("call")?.type; |
12885 InheritanceManager manager = new InheritanceManager(element.library); | |
12886 FunctionType callType = manager.lookupMemberType(t, "call"); | |
12887 return callType; | |
12888 } | 12858 } |
12889 return null; | 12859 return null; |
12890 } | 12860 } |
12891 | 12861 |
12892 // Given a type t, if t is an interface type with a call method | 12862 // Given a type t, if t is an interface type with a call method |
12893 // defined, return the function type for the call method, otherwise | 12863 // defined, return the function type for the call method, otherwise |
12894 // return null. | 12864 // return null. |
12895 _GuardedSubtypeChecker<DartType> _guard( | 12865 _GuardedSubtypeChecker<DartType> _guard( |
12896 _GuardedSubtypeChecker<DartType> check) { | 12866 _GuardedSubtypeChecker<DartType> check) { |
12897 return (DartType t1, DartType t2, Set<Element> visited) { | 12867 return (DartType t1, DartType t2, Set<Element> visited) { |
(...skipping 3166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
16064 nonFields.add(node); | 16034 nonFields.add(node); |
16065 return null; | 16035 return null; |
16066 } | 16036 } |
16067 | 16037 |
16068 @override | 16038 @override |
16069 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); | 16039 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); |
16070 | 16040 |
16071 @override | 16041 @override |
16072 Object visitWithClause(WithClause node) => null; | 16042 Object visitWithClause(WithClause node) => null; |
16073 } | 16043 } |
OLD | NEW |