| 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.resolver; | 5 library analyzer.src.generated.resolver; |
| 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 9511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9522 TypeName exceptionTypeName = node.exceptionType; | 9522 TypeName exceptionTypeName = node.exceptionType; |
| 9523 DartType exceptionType; | 9523 DartType exceptionType; |
| 9524 if (exceptionTypeName == null) { | 9524 if (exceptionTypeName == null) { |
| 9525 exceptionType = typeProvider.dynamicType; | 9525 exceptionType = typeProvider.dynamicType; |
| 9526 } else { | 9526 } else { |
| 9527 exceptionType = _typeNameResolver._getType(exceptionTypeName); | 9527 exceptionType = _typeNameResolver._getType(exceptionTypeName); |
| 9528 } | 9528 } |
| 9529 _recordType(exception, exceptionType); | 9529 _recordType(exception, exceptionType); |
| 9530 Element element = exception.staticElement; | 9530 Element element = exception.staticElement; |
| 9531 if (element is VariableElementImpl) { | 9531 if (element is VariableElementImpl) { |
| 9532 element.type = exceptionType; | 9532 element.declaredType = exceptionType; |
| 9533 } else { | 9533 } else { |
| 9534 // TODO(brianwilkerson) Report the internal error | 9534 // TODO(brianwilkerson) Report the internal error |
| 9535 } | 9535 } |
| 9536 } | 9536 } |
| 9537 SimpleIdentifier stackTrace = node.stackTraceParameter; | 9537 SimpleIdentifier stackTrace = node.stackTraceParameter; |
| 9538 if (stackTrace != null) { | 9538 if (stackTrace != null) { |
| 9539 _recordType(stackTrace, typeProvider.stackTraceType); | 9539 _recordType(stackTrace, typeProvider.stackTraceType); |
| 9540 Element element = stackTrace.staticElement; | 9540 Element element = stackTrace.staticElement; |
| 9541 if (element is VariableElementImpl) { | 9541 if (element is VariableElementImpl) { |
| 9542 element.type = typeProvider.stackTraceType; | 9542 element.declaredType = typeProvider.stackTraceType; |
| 9543 } else { | 9543 } else { |
| 9544 // TODO(brianwilkerson) Report the internal error | 9544 // TODO(brianwilkerson) Report the internal error |
| 9545 } | 9545 } |
| 9546 } | 9546 } |
| 9547 return null; | 9547 return null; |
| 9548 } | 9548 } |
| 9549 | 9549 |
| 9550 @override | 9550 @override |
| 9551 Object visitClassDeclaration(ClassDeclaration node) { | 9551 Object visitClassDeclaration(ClassDeclaration node) { |
| 9552 _hasReferenceToSuper = false; | 9552 _hasReferenceToSuper = false; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9658 Object visitDeclaredIdentifier(DeclaredIdentifier node) { | 9658 Object visitDeclaredIdentifier(DeclaredIdentifier node) { |
| 9659 super.visitDeclaredIdentifier(node); | 9659 super.visitDeclaredIdentifier(node); |
| 9660 DartType declaredType; | 9660 DartType declaredType; |
| 9661 TypeName typeName = node.type; | 9661 TypeName typeName = node.type; |
| 9662 if (typeName == null) { | 9662 if (typeName == null) { |
| 9663 declaredType = _dynamicType; | 9663 declaredType = _dynamicType; |
| 9664 } else { | 9664 } else { |
| 9665 declaredType = _typeNameResolver._getType(typeName); | 9665 declaredType = _typeNameResolver._getType(typeName); |
| 9666 } | 9666 } |
| 9667 LocalVariableElementImpl element = node.element as LocalVariableElementImpl; | 9667 LocalVariableElementImpl element = node.element as LocalVariableElementImpl; |
| 9668 element.type = declaredType; | 9668 element.declaredType = declaredType; |
| 9669 return null; | 9669 return null; |
| 9670 } | 9670 } |
| 9671 | 9671 |
| 9672 @override | 9672 @override |
| 9673 Object visitFieldFormalParameter(FieldFormalParameter node) { | 9673 Object visitFieldFormalParameter(FieldFormalParameter node) { |
| 9674 super.visitFieldFormalParameter(node); | 9674 super.visitFieldFormalParameter(node); |
| 9675 Element element = node.identifier.staticElement; | 9675 Element element = node.identifier.staticElement; |
| 9676 if (element is ParameterElementImpl) { | 9676 if (element is ParameterElementImpl) { |
| 9677 FormalParameterList parameterList = node.parameters; | 9677 FormalParameterList parameterList = node.parameters; |
| 9678 if (parameterList == null) { | 9678 if (parameterList == null) { |
| 9679 DartType type; | 9679 DartType type; |
| 9680 TypeName typeName = node.type; | 9680 TypeName typeName = node.type; |
| 9681 if (typeName == null) { | 9681 if (typeName == null) { |
| 9682 element.hasImplicitType = true; | 9682 element.hasImplicitType = true; |
| 9683 if (element is FieldFormalParameterElement) { | 9683 if (element is FieldFormalParameterElement) { |
| 9684 FieldElement fieldElement = | 9684 FieldElement fieldElement = |
| 9685 (element as FieldFormalParameterElement).field; | 9685 (element as FieldFormalParameterElement).field; |
| 9686 type = fieldElement?.type; | 9686 type = fieldElement?.type; |
| 9687 } | 9687 } |
| 9688 } else { | 9688 } else { |
| 9689 type = _typeNameResolver._getType(typeName); | 9689 type = _typeNameResolver._getType(typeName); |
| 9690 } | 9690 } |
| 9691 element.type = type ?? _dynamicType; | 9691 element.declaredType = type ?? _dynamicType; |
| 9692 } else { | 9692 } else { |
| 9693 _setFunctionTypedParameterType(element, node.type, node.parameters); | 9693 _setFunctionTypedParameterType(element, node.type, node.parameters); |
| 9694 } | 9694 } |
| 9695 } else { | 9695 } else { |
| 9696 // TODO(brianwilkerson) Report this internal error | 9696 // TODO(brianwilkerson) Report this internal error |
| 9697 } | 9697 } |
| 9698 return null; | 9698 return null; |
| 9699 } | 9699 } |
| 9700 | 9700 |
| 9701 @override | 9701 @override |
| 9702 Object visitFunctionDeclaration(FunctionDeclaration node) { | 9702 Object visitFunctionDeclaration(FunctionDeclaration node) { |
| 9703 super.visitFunctionDeclaration(node); | 9703 super.visitFunctionDeclaration(node); |
| 9704 ExecutableElementImpl element = node.element as ExecutableElementImpl; | 9704 ExecutableElementImpl element = node.element as ExecutableElementImpl; |
| 9705 if (element == null) { | 9705 if (element == null) { |
| 9706 StringBuffer buffer = new StringBuffer(); | 9706 StringBuffer buffer = new StringBuffer(); |
| 9707 buffer.write("The element for the top-level function "); | 9707 buffer.write("The element for the top-level function "); |
| 9708 buffer.write(node.name); | 9708 buffer.write(node.name); |
| 9709 buffer.write(" in "); | 9709 buffer.write(" in "); |
| 9710 buffer.write(source.fullName); | 9710 buffer.write(source.fullName); |
| 9711 buffer.write(" was not set while trying to resolve types."); | 9711 buffer.write(" was not set while trying to resolve types."); |
| 9712 AnalysisEngine.instance.logger.logError(buffer.toString(), | 9712 AnalysisEngine.instance.logger.logError(buffer.toString(), |
| 9713 new CaughtException(new AnalysisException(), null)); | 9713 new CaughtException(new AnalysisException(), null)); |
| 9714 } | 9714 } |
| 9715 element.returnType = _computeReturnType(node.returnType); | 9715 element.declaredReturnType = _computeReturnType(node.returnType); |
| 9716 element.type = new FunctionTypeImpl(element); | 9716 element.type = new FunctionTypeImpl(element); |
| 9717 _inferSetterReturnType(element); | 9717 _inferSetterReturnType(element); |
| 9718 return null; | 9718 return null; |
| 9719 } | 9719 } |
| 9720 | 9720 |
| 9721 @override | 9721 @override |
| 9722 Object visitFunctionTypeAlias(FunctionTypeAlias node) { | 9722 Object visitFunctionTypeAlias(FunctionTypeAlias node) { |
| 9723 FunctionTypeAliasElementImpl element = | 9723 FunctionTypeAliasElementImpl element = |
| 9724 node.element as FunctionTypeAliasElementImpl; | 9724 node.element as FunctionTypeAliasElementImpl; |
| 9725 super.visitFunctionTypeAlias(node); | 9725 super.visitFunctionTypeAlias(node); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 9754 buffer.write("<unknown class>"); | 9754 buffer.write("<unknown class>"); |
| 9755 } else { | 9755 } else { |
| 9756 buffer.write(classNode.name.name); | 9756 buffer.write(classNode.name.name); |
| 9757 } | 9757 } |
| 9758 buffer.write(" in "); | 9758 buffer.write(" in "); |
| 9759 buffer.write(source.fullName); | 9759 buffer.write(source.fullName); |
| 9760 buffer.write(" was not set while trying to resolve types."); | 9760 buffer.write(" was not set while trying to resolve types."); |
| 9761 AnalysisEngine.instance.logger.logError(buffer.toString(), | 9761 AnalysisEngine.instance.logger.logError(buffer.toString(), |
| 9762 new CaughtException(new AnalysisException(), null)); | 9762 new CaughtException(new AnalysisException(), null)); |
| 9763 } | 9763 } |
| 9764 element.returnType = _computeReturnType(node.returnType); | 9764 element.declaredReturnType = _computeReturnType(node.returnType); |
| 9765 element.type = new FunctionTypeImpl(element); | 9765 element.type = new FunctionTypeImpl(element); |
| 9766 _inferSetterReturnType(element); | 9766 _inferSetterReturnType(element); |
| 9767 if (element is PropertyAccessorElement) { | 9767 if (element is PropertyAccessorElement) { |
| 9768 PropertyAccessorElement accessor = element as PropertyAccessorElement; | 9768 PropertyAccessorElement accessor = element as PropertyAccessorElement; |
| 9769 PropertyInducingElementImpl variable = | 9769 PropertyInducingElementImpl variable = |
| 9770 accessor.variable as PropertyInducingElementImpl; | 9770 accessor.variable as PropertyInducingElementImpl; |
| 9771 if (accessor.isGetter) { | 9771 if (accessor.isGetter) { |
| 9772 variable.type = element.returnType; | 9772 variable.declaredType = element.returnType; |
| 9773 } else if (variable.type == null) { | 9773 } else if (variable.type == null) { |
| 9774 List<ParameterElement> parameters = element.parameters; | 9774 List<ParameterElement> parameters = element.parameters; |
| 9775 if (parameters != null && parameters.length > 0) { | 9775 if (parameters != null && parameters.length > 0) { |
| 9776 variable.type = parameters[0].type; | 9776 variable.declaredType = parameters[0].type; |
| 9777 } | 9777 } |
| 9778 } | 9778 } |
| 9779 } | 9779 } |
| 9780 return null; | 9780 return null; |
| 9781 } | 9781 } |
| 9782 | 9782 |
| 9783 @override | 9783 @override |
| 9784 Object visitNode(AstNode node) { | 9784 Object visitNode(AstNode node) { |
| 9785 // In API mode we need to skip: | 9785 // In API mode we need to skip: |
| 9786 // - function bodies; | 9786 // - function bodies; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9891 super.visitSimpleFormalParameter(node); | 9891 super.visitSimpleFormalParameter(node); |
| 9892 DartType declaredType; | 9892 DartType declaredType; |
| 9893 TypeName typeName = node.type; | 9893 TypeName typeName = node.type; |
| 9894 if (typeName == null) { | 9894 if (typeName == null) { |
| 9895 declaredType = _dynamicType; | 9895 declaredType = _dynamicType; |
| 9896 } else { | 9896 } else { |
| 9897 declaredType = _typeNameResolver._getType(typeName); | 9897 declaredType = _typeNameResolver._getType(typeName); |
| 9898 } | 9898 } |
| 9899 Element element = node.identifier.staticElement; | 9899 Element element = node.identifier.staticElement; |
| 9900 if (element is ParameterElementImpl) { | 9900 if (element is ParameterElementImpl) { |
| 9901 element.type = declaredType; | 9901 element.declaredType = declaredType; |
| 9902 } else { | 9902 } else { |
| 9903 // TODO(brianwilkerson) Report the internal error. | 9903 // TODO(brianwilkerson) Report the internal error. |
| 9904 } | 9904 } |
| 9905 return null; | 9905 return null; |
| 9906 } | 9906 } |
| 9907 | 9907 |
| 9908 @override | 9908 @override |
| 9909 Object visitSuperExpression(SuperExpression node) { | 9909 Object visitSuperExpression(SuperExpression node) { |
| 9910 _hasReferenceToSuper = true; | 9910 _hasReferenceToSuper = true; |
| 9911 return super.visitSuperExpression(node); | 9911 return super.visitSuperExpression(node); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9956 // Resolve the type. | 9956 // Resolve the type. |
| 9957 DartType declaredType; | 9957 DartType declaredType; |
| 9958 TypeName typeName = variableList.type; | 9958 TypeName typeName = variableList.type; |
| 9959 if (typeName == null) { | 9959 if (typeName == null) { |
| 9960 declaredType = _dynamicType; | 9960 declaredType = _dynamicType; |
| 9961 } else { | 9961 } else { |
| 9962 declaredType = _typeNameResolver._getType(typeName); | 9962 declaredType = _typeNameResolver._getType(typeName); |
| 9963 } | 9963 } |
| 9964 Element element = node.name.staticElement; | 9964 Element element = node.name.staticElement; |
| 9965 if (element is VariableElementImpl) { | 9965 if (element is VariableElementImpl) { |
| 9966 element.type = declaredType; | 9966 element.declaredType = declaredType; |
| 9967 } | 9967 } |
| 9968 return null; | 9968 return null; |
| 9969 } | 9969 } |
| 9970 | 9970 |
| 9971 /** | 9971 /** |
| 9972 * Given a type name representing the return type of a function, compute the r
eturn type of the | 9972 * Given a type name representing the return type of a function, compute the r
eturn type of the |
| 9973 * function. | 9973 * function. |
| 9974 * | 9974 * |
| 9975 * @param returnType the type name representing the return type of the functio
n | 9975 * @param returnType the type name representing the return type of the functio
n |
| 9976 * @return the return type that was computed | 9976 * @return the return type that was computed |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10030 /** | 10030 /** |
| 10031 * In strong mode we infer "void" as the setter return type (as void is the | 10031 * In strong mode we infer "void" as the setter return type (as void is the |
| 10032 * only legal return type for a setter). This allows us to give better | 10032 * only legal return type for a setter). This allows us to give better |
| 10033 * errors later if an invalid type is returned. | 10033 * errors later if an invalid type is returned. |
| 10034 */ | 10034 */ |
| 10035 void _inferSetterReturnType(ExecutableElementImpl element) { | 10035 void _inferSetterReturnType(ExecutableElementImpl element) { |
| 10036 if (_strongMode && | 10036 if (_strongMode && |
| 10037 element is PropertyAccessorElementImpl && | 10037 element is PropertyAccessorElementImpl && |
| 10038 element.isSetter && | 10038 element.isSetter && |
| 10039 element.hasImplicitReturnType) { | 10039 element.hasImplicitReturnType) { |
| 10040 element.returnType = VoidTypeImpl.instance; | 10040 element.declaredReturnType = VoidTypeImpl.instance; |
| 10041 } | 10041 } |
| 10042 } | 10042 } |
| 10043 | 10043 |
| 10044 /** | 10044 /** |
| 10045 * Record that the static type of the given node is the given type. | 10045 * Record that the static type of the given node is the given type. |
| 10046 * | 10046 * |
| 10047 * @param expression the node whose type is to be recorded | 10047 * @param expression the node whose type is to be recorded |
| 10048 * @param type the static type of the node | 10048 * @param type the static type of the node |
| 10049 */ | 10049 */ |
| 10050 Object _recordType(Expression expression, DartType type) { | 10050 Object _recordType(Expression expression, DartType type) { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10178 * @param element the parameter element whose type is to be set | 10178 * @param element the parameter element whose type is to be set |
| 10179 * @param returnType the (possibly `null`) return type of the function | 10179 * @param returnType the (possibly `null`) return type of the function |
| 10180 * @param parameterList the list of parameters to the function | 10180 * @param parameterList the list of parameters to the function |
| 10181 */ | 10181 */ |
| 10182 void _setFunctionTypedParameterType(ParameterElementImpl element, | 10182 void _setFunctionTypedParameterType(ParameterElementImpl element, |
| 10183 TypeName returnType, FormalParameterList parameterList) { | 10183 TypeName returnType, FormalParameterList parameterList) { |
| 10184 List<ParameterElement> parameters = _getElements(parameterList); | 10184 List<ParameterElement> parameters = _getElements(parameterList); |
| 10185 FunctionElementImpl functionElement = new FunctionElementImpl.forNode(null); | 10185 FunctionElementImpl functionElement = new FunctionElementImpl.forNode(null); |
| 10186 functionElement.synthetic = true; | 10186 functionElement.synthetic = true; |
| 10187 functionElement.shareParameters(parameters); | 10187 functionElement.shareParameters(parameters); |
| 10188 functionElement.returnType = _computeReturnType(returnType); | 10188 functionElement.declaredReturnType = _computeReturnType(returnType); |
| 10189 functionElement.enclosingElement = element; | 10189 functionElement.enclosingElement = element; |
| 10190 functionElement.shareTypeParameters(element.typeParameters); | 10190 functionElement.shareTypeParameters(element.typeParameters); |
| 10191 element.type = new FunctionTypeImpl(functionElement); | 10191 element.type = new FunctionTypeImpl(functionElement); |
| 10192 functionElement.type = element.type; | 10192 functionElement.type = element.type; |
| 10193 } | 10193 } |
| 10194 } | 10194 } |
| 10195 | 10195 |
| 10196 /** | 10196 /** |
| 10197 * Instances of the class [UnusedLocalElementsVerifier] traverse an element | 10197 * Instances of the class [UnusedLocalElementsVerifier] traverse an element |
| 10198 * structure looking for cases of [HintCode.UNUSED_ELEMENT], | 10198 * structure looking for cases of [HintCode.UNUSED_ELEMENT], |
| (...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10731 return null; | 10731 return null; |
| 10732 } | 10732 } |
| 10733 if (identical(node.staticElement, variable)) { | 10733 if (identical(node.staticElement, variable)) { |
| 10734 if (node.inSetterContext()) { | 10734 if (node.inSetterContext()) { |
| 10735 result = true; | 10735 result = true; |
| 10736 } | 10736 } |
| 10737 } | 10737 } |
| 10738 return null; | 10738 return null; |
| 10739 } | 10739 } |
| 10740 } | 10740 } |
| OLD | NEW |