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

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

Issue 1933763002: Use null-aware operators to clean up the code (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Additional 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
« no previous file with comments | « pkg/analyzer/lib/src/generated/parser.dart ('k') | pkg/analyzer/lib/src/generated/source.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) 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 83
84 /** 84 /**
85 * Create a new instance of the [BestPracticesVerifier]. 85 * Create a new instance of the [BestPracticesVerifier].
86 * 86 *
87 * @param errorReporter the error reporter 87 * @param errorReporter the error reporter
88 */ 88 */
89 BestPracticesVerifier( 89 BestPracticesVerifier(
90 this._errorReporter, TypeProvider typeProvider, this._currentLibrary, 90 this._errorReporter, TypeProvider typeProvider, this._currentLibrary,
91 {TypeSystem typeSystem}) 91 {TypeSystem typeSystem})
92 : _futureNullType = typeProvider.futureNullType, 92 : _futureNullType = typeProvider.futureNullType,
93 _typeSystem = (typeSystem != null) ? typeSystem : new TypeSystemImpl(); 93 _typeSystem = typeSystem ?? new TypeSystemImpl();
94 94
95 @override 95 @override
96 Object visitArgumentList(ArgumentList node) { 96 Object visitArgumentList(ArgumentList node) {
97 _checkForArgumentTypesNotAssignableInList(node); 97 _checkForArgumentTypesNotAssignableInList(node);
98 return super.visitArgumentList(node); 98 return super.visitArgumentList(node);
99 } 99 }
100 100
101 @override 101 @override
102 Object visitAsExpression(AsExpression node) { 102 Object visitAsExpression(AsExpression node) {
103 _checkForUnnecessaryCast(node); 103 _checkForUnnecessaryCast(node);
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 @override 204 @override
205 Object visitIfStatement(IfStatement node) { 205 Object visitIfStatement(IfStatement node) {
206 _checkForPossibleNullCondition(node.condition); 206 _checkForPossibleNullCondition(node.condition);
207 return super.visitIfStatement(node); 207 return super.visitIfStatement(node);
208 } 208 }
209 209
210 @override 210 @override
211 Object visitImportDirective(ImportDirective node) { 211 Object visitImportDirective(ImportDirective node) {
212 _checkForDeprecatedMemberUse(node.uriElement, node); 212 _checkForDeprecatedMemberUse(node.uriElement, node);
213 ImportElement importElement = node.element; 213 ImportElement importElement = node.element;
214 if (importElement != null) { 214 if (importElement != null && importElement.isDeferred) {
215 if (importElement.isDeferred) { 215 _checkForLoadLibraryFunction(node, importElement);
216 _checkForLoadLibraryFunction(node, importElement);
217 }
218 } 216 }
219 return super.visitImportDirective(node); 217 return super.visitImportDirective(node);
220 } 218 }
221 219
222 @override 220 @override
223 Object visitIndexExpression(IndexExpression node) { 221 Object visitIndexExpression(IndexExpression node) {
224 _checkForDeprecatedMemberUse(node.bestElement, node); 222 _checkForDeprecatedMemberUse(node.bestElement, node);
225 return super.visitIndexExpression(node); 223 return super.visitIndexExpression(node);
226 } 224 }
227 225
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 _errorReporter.reportErrorForNode( 343 _errorReporter.reportErrorForNode(
346 HintCode.UNNECESSARY_TYPE_CHECK_TRUE, node); 344 HintCode.UNNECESSARY_TYPE_CHECK_TRUE, node);
347 } else { 345 } else {
348 // the is not case 346 // the is not case
349 _errorReporter.reportErrorForNode( 347 _errorReporter.reportErrorForNode(
350 HintCode.UNNECESSARY_TYPE_CHECK_FALSE, node); 348 HintCode.UNNECESSARY_TYPE_CHECK_FALSE, node);
351 } 349 }
352 return true; 350 return true;
353 } 351 }
354 Element rhsElement = rhsType.element; 352 Element rhsElement = rhsType.element;
355 LibraryElement libraryElement = 353 LibraryElement libraryElement = rhsElement?.library;
356 rhsElement != null ? rhsElement.library : null;
357 if (libraryElement != null && libraryElement.isDartCore) { 354 if (libraryElement != null && libraryElement.isDartCore) {
358 // if x is Object or null is Null 355 // if x is Object or null is Null
359 if (rhsType.isObject || 356 if (rhsType.isObject ||
360 (expression is NullLiteral && rhsNameStr == _NULL_TYPE_NAME)) { 357 (expression is NullLiteral && rhsNameStr == _NULL_TYPE_NAME)) {
361 if (node.notOperator == null) { 358 if (node.notOperator == null) {
362 // the is case 359 // the is case
363 _errorReporter.reportErrorForNode( 360 _errorReporter.reportErrorForNode(
364 HintCode.UNNECESSARY_TYPE_CHECK_TRUE, node); 361 HintCode.UNNECESSARY_TYPE_CHECK_TRUE, node);
365 } else { 362 } else {
366 // the is not case 363 // the is not case
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 if (!_typeSystem.isAssignableTo(actualStaticType, expectedStaticType)) { 411 if (!_typeSystem.isAssignableTo(actualStaticType, expectedStaticType)) {
415 // A warning was created in the ErrorVerifier, return false, don't 412 // A warning was created in the ErrorVerifier, return false, don't
416 // create a hint when a warning has already been created. 413 // create a hint when a warning has already been created.
417 return false; 414 return false;
418 } 415 }
419 } 416 }
420 // 417 //
421 // Hint case: test propagated type information 418 // Hint case: test propagated type information
422 // 419 //
423 // Compute the best types to use. 420 // Compute the best types to use.
424 DartType expectedBestType = expectedPropagatedType != null 421 DartType expectedBestType = expectedPropagatedType ?? expectedStaticType;
425 ? expectedPropagatedType 422 DartType actualBestType = actualPropagatedType ?? actualStaticType;
426 : expectedStaticType;
427 DartType actualBestType =
428 actualPropagatedType != null ? actualPropagatedType : actualStaticType;
429 if (actualBestType != null && expectedBestType != null) { 423 if (actualBestType != null && expectedBestType != null) {
430 if (!_typeSystem.isAssignableTo(actualBestType, expectedBestType)) { 424 if (!_typeSystem.isAssignableTo(actualBestType, expectedBestType)) {
431 _errorReporter.reportTypeErrorForNode( 425 _errorReporter.reportTypeErrorForNode(
432 hintCode, expression, [actualBestType, expectedBestType]); 426 hintCode, expression, [actualBestType, expectedBestType]);
433 return true; 427 return true;
434 } 428 }
435 } 429 }
436 return false; 430 return false;
437 } 431 }
438 432
439 /** 433 /**
440 * This verifies that the passed argument can be assigned to its corresponding parameter. 434 * This verifies that the passed argument can be assigned to its corresponding parameter.
441 * 435 *
442 * This method corresponds to ErrorCode.checkForArgumentTypeNotAssignableForAr gument. 436 * This method corresponds to ErrorCode.checkForArgumentTypeNotAssignableForAr gument.
443 * 437 *
444 * @param argument the argument to evaluate 438 * @param argument the argument to evaluate
445 * @return `true` if and only if an hint code is generated on the passed node 439 * @return `true` if and only if an hint code is generated on the passed node
446 * See [HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]. 440 * See [HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE].
447 */ 441 */
448 bool _checkForArgumentTypeNotAssignableForArgument(Expression argument) { 442 bool _checkForArgumentTypeNotAssignableForArgument(Expression argument) {
449 if (argument == null) { 443 if (argument == null) {
450 return false; 444 return false;
451 } 445 }
452 ParameterElement staticParameterElement = argument.staticParameterElement; 446 ParameterElement staticParameterElement = argument.staticParameterElement;
453 DartType staticParameterType = 447 DartType staticParameterType = staticParameterElement?.type;
454 staticParameterElement == null ? null : staticParameterElement.type;
455 ParameterElement propagatedParameterElement = 448 ParameterElement propagatedParameterElement =
456 argument.propagatedParameterElement; 449 argument.propagatedParameterElement;
457 DartType propagatedParameterType = propagatedParameterElement == null 450 DartType propagatedParameterType = propagatedParameterElement?.type;
458 ? null
459 : propagatedParameterElement.type;
460 return _checkForArgumentTypeNotAssignableWithExpectedTypes( 451 return _checkForArgumentTypeNotAssignableWithExpectedTypes(
461 argument, 452 argument,
462 staticParameterType, 453 staticParameterType,
463 propagatedParameterType, 454 propagatedParameterType,
464 HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE); 455 HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE);
465 } 456 }
466 457
467 /** 458 /**
468 * This verifies that the passed expression can be assigned to its correspondi ng parameters. 459 * This verifies that the passed expression can be assigned to its correspondi ng parameters.
469 * 460 *
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 _checkForPossibleNullConditionInSimpleExpression(condition); 816 _checkForPossibleNullConditionInSimpleExpression(condition);
826 } 817 }
827 } 818 }
828 819
829 /** 820 /**
830 * Produce a hint if any of the parts of the given binary [condition] could 821 * Produce a hint if any of the parts of the given binary [condition] could
831 * have a value of `null`. 822 * have a value of `null`.
832 */ 823 */
833 void _checkForPossibleNullConditionInBinaryExpression( 824 void _checkForPossibleNullConditionInBinaryExpression(
834 BinaryExpression condition) { 825 BinaryExpression condition) {
835 Token operator = condition.operator; 826 TokenType type = condition.operator?.type;
836 if (operator != null && 827 if (type == TokenType.AMPERSAND_AMPERSAND || type == TokenType.BAR_BAR) {
837 (operator.type == TokenType.AMPERSAND_AMPERSAND ||
838 operator.type == TokenType.BAR_BAR)) {
839 _checkForPossibleNullCondition(condition.leftOperand); 828 _checkForPossibleNullCondition(condition.leftOperand);
840 _checkForPossibleNullCondition(condition.rightOperand); 829 _checkForPossibleNullCondition(condition.rightOperand);
841 } 830 }
842 } 831 }
843 832
844 /** 833 /**
845 * Produce a hint if the operand of the given prefix [condition] could 834 * Produce a hint if the operand of the given prefix [condition] could
846 * have a value of `null`. 835 * have a value of `null`.
847 */ 836 */
848 void _checkForPossibleNullConditionInPrefixExpression( 837 void _checkForPossibleNullConditionInPrefixExpression(
849 PrefixExpression condition) { 838 PrefixExpression condition) {
850 if (condition.operator?.type == TokenType.BANG) { 839 if (condition.operator?.type == TokenType.BANG) {
851 _checkForPossibleNullCondition(condition.operand); 840 _checkForPossibleNullCondition(condition.operand);
852 } 841 }
853 } 842 }
854 843
855 /** 844 /**
856 * Produce a hint if the given [condition] could have a value of `null`. 845 * Produce a hint if the given [condition] could have a value of `null`.
857 */ 846 */
858 void _checkForPossibleNullConditionInSimpleExpression(Expression condition) { 847 void _checkForPossibleNullConditionInSimpleExpression(Expression condition) {
859 if (condition is MethodInvocation) { 848 if (condition is MethodInvocation) {
860 Token operator = condition.operator; 849 if (condition.operator?.type == TokenType.QUESTION_PERIOD) {
861 if (operator != null && operator.type == TokenType.QUESTION_PERIOD) {
862 _errorReporter.reportErrorForNode( 850 _errorReporter.reportErrorForNode(
863 HintCode.NULL_AWARE_IN_CONDITION, condition); 851 HintCode.NULL_AWARE_IN_CONDITION, condition);
864 } 852 }
865 } else if (condition is PropertyAccess) { 853 } else if (condition is PropertyAccess) {
866 Token operator = condition.operator; 854 if (condition.operator?.type == TokenType.QUESTION_PERIOD) {
867 if (operator != null && operator.type == TokenType.QUESTION_PERIOD) {
868 _errorReporter.reportErrorForNode( 855 _errorReporter.reportErrorForNode(
869 HintCode.NULL_AWARE_IN_CONDITION, condition); 856 HintCode.NULL_AWARE_IN_CONDITION, condition);
870 } 857 }
871 } 858 }
872 } 859 }
873 860
874 /** 861 /**
875 * Check for the passed as expression for the [HintCode.UNNECESSARY_CAST] hint code. 862 * Check for the passed as expression for the [HintCode.UNNECESSARY_CAST] hint code.
876 * 863 *
877 * @param node the as expression to check 864 * @param node the as expression to check
(...skipping 859 matching lines...) Expand 10 before | Expand all | Expand 10 after
1737 * @param node the is expression to check 1724 * @param node the is expression to check
1738 * @return `true` if and only if a hint code is generated on the passed node 1725 * @return `true` if and only if a hint code is generated on the passed node
1739 * See [HintCode.IS_DOUBLE], 1726 * See [HintCode.IS_DOUBLE],
1740 * [HintCode.IS_INT], 1727 * [HintCode.IS_INT],
1741 * [HintCode.IS_NOT_DOUBLE], and 1728 * [HintCode.IS_NOT_DOUBLE], and
1742 * [HintCode.IS_NOT_INT]. 1729 * [HintCode.IS_NOT_INT].
1743 */ 1730 */
1744 bool _checkForIsDoubleHints(IsExpression node) { 1731 bool _checkForIsDoubleHints(IsExpression node) {
1745 TypeName typeName = node.type; 1732 TypeName typeName = node.type;
1746 DartType type = typeName.type; 1733 DartType type = typeName.type;
1747 if (type != null && type.element != null) { 1734 Element element = type?.element;
1748 Element element = type.element; 1735 if (element != null) {
1749 String typeNameStr = element.name; 1736 String typeNameStr = element.name;
1750 LibraryElement libraryElement = element.library; 1737 LibraryElement libraryElement = element.library;
1751 // if (typeNameStr.equals(INT_TYPE_NAME) && libraryElement != null 1738 // if (typeNameStr.equals(INT_TYPE_NAME) && libraryElement != null
1752 // && libraryElement.isDartCore()) { 1739 // && libraryElement.isDartCore()) {
1753 // if (node.getNotOperator() == null) { 1740 // if (node.getNotOperator() == null) {
1754 // errorReporter.reportError(HintCode.IS_INT, node); 1741 // errorReporter.reportError(HintCode.IS_INT, node);
1755 // } else { 1742 // } else {
1756 // errorReporter.reportError(HintCode.IS_NOT_INT, node); 1743 // errorReporter.reportError(HintCode.IS_NOT_INT, node);
1757 // } 1744 // }
1758 // return true; 1745 // return true;
(...skipping 27 matching lines...) Expand all
1786 * The type system for this visitor 1773 * The type system for this visitor
1787 */ 1774 */
1788 final TypeSystem _typeSystem; 1775 final TypeSystem _typeSystem;
1789 1776
1790 /** 1777 /**
1791 * Create a new instance of the [DeadCodeVerifier]. 1778 * Create a new instance of the [DeadCodeVerifier].
1792 * 1779 *
1793 * @param errorReporter the error reporter 1780 * @param errorReporter the error reporter
1794 */ 1781 */
1795 DeadCodeVerifier(this._errorReporter, {TypeSystem typeSystem}) 1782 DeadCodeVerifier(this._errorReporter, {TypeSystem typeSystem})
1796 : this._typeSystem = 1783 : this._typeSystem = typeSystem ?? new TypeSystemImpl();
1797 (typeSystem != null) ? typeSystem : new TypeSystemImpl();
1798 1784
1799 @override 1785 @override
1800 Object visitBinaryExpression(BinaryExpression node) { 1786 Object visitBinaryExpression(BinaryExpression node) {
1801 Token operator = node.operator; 1787 Token operator = node.operator;
1802 bool isAmpAmp = operator.type == TokenType.AMPERSAND_AMPERSAND; 1788 bool isAmpAmp = operator.type == TokenType.AMPERSAND_AMPERSAND;
1803 bool isBarBar = operator.type == TokenType.BAR_BAR; 1789 bool isBarBar = operator.type == TokenType.BAR_BAR;
1804 if (isAmpAmp || isBarBar) { 1790 if (isAmpAmp || isBarBar) {
1805 Expression lhsCondition = node.leftOperand; 1791 Expression lhsCondition = node.leftOperand;
1806 if (!_isDebugConstant(lhsCondition)) { 1792 if (!_isDebugConstant(lhsCondition)) {
1807 EvaluationResultImpl lhsResult = _getConstantBooleanValue(lhsCondition); 1793 EvaluationResultImpl lhsResult = _getConstantBooleanValue(lhsCondition);
1808 if (lhsResult != null) { 1794 if (lhsResult != null) {
1809 if (lhsResult.value.toBoolValue() == true && isBarBar) { 1795 bool value = lhsResult.value.toBoolValue();
1796 if (value == true && isBarBar) {
1810 // report error on else block: true || !e! 1797 // report error on else block: true || !e!
1811 _errorReporter.reportErrorForNode( 1798 _errorReporter.reportErrorForNode(
1812 HintCode.DEAD_CODE, node.rightOperand); 1799 HintCode.DEAD_CODE, node.rightOperand);
1813 // only visit the LHS: 1800 // only visit the LHS:
1814 lhsCondition?.accept(this); 1801 lhsCondition?.accept(this);
1815 return null; 1802 return null;
1816 } else if (lhsResult.value.toBoolValue() == false && isAmpAmp) { 1803 } else if (value == false && isAmpAmp) {
1817 // report error on if block: false && !e! 1804 // report error on if block: false && !e!
1818 _errorReporter.reportErrorForNode( 1805 _errorReporter.reportErrorForNode(
1819 HintCode.DEAD_CODE, node.rightOperand); 1806 HintCode.DEAD_CODE, node.rightOperand);
1820 // only visit the LHS: 1807 // only visit the LHS:
1821 lhsCondition?.accept(this); 1808 lhsCondition?.accept(this);
1822 return null; 1809 return null;
1823 } 1810 }
1824 } 1811 }
1825 } 1812 }
1826 // How do we want to handle the RHS? It isn't dead code, but "pointless" 1813 // How do we want to handle the RHS? It isn't dead code, but "pointless"
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1961 node.body?.accept(this); 1948 node.body?.accept(this);
1962 node.finallyBlock?.accept(this); 1949 node.finallyBlock?.accept(this);
1963 NodeList<CatchClause> catchClauses = node.catchClauses; 1950 NodeList<CatchClause> catchClauses = node.catchClauses;
1964 int numOfCatchClauses = catchClauses.length; 1951 int numOfCatchClauses = catchClauses.length;
1965 List<DartType> visitedTypes = new List<DartType>(); 1952 List<DartType> visitedTypes = new List<DartType>();
1966 for (int i = 0; i < numOfCatchClauses; i++) { 1953 for (int i = 0; i < numOfCatchClauses; i++) {
1967 CatchClause catchClause = catchClauses[i]; 1954 CatchClause catchClause = catchClauses[i];
1968 if (catchClause.onKeyword != null) { 1955 if (catchClause.onKeyword != null) {
1969 // on-catch clause found, verify that the exception type is not a 1956 // on-catch clause found, verify that the exception type is not a
1970 // subtype of a previous on-catch exception type 1957 // subtype of a previous on-catch exception type
1971 TypeName typeName = catchClause.exceptionType; 1958 DartType currentType = catchClause.exceptionType?.type;
1972 if (typeName != null && typeName.type != null) { 1959 if (currentType != null) {
1973 DartType currentType = typeName.type;
1974 if (currentType.isObject) { 1960 if (currentType.isObject) {
1975 // Found catch clause clause that has Object as an exception type, 1961 // Found catch clause clause that has Object as an exception type,
1976 // this is equivalent to having a catch clause that doesn't have an 1962 // this is equivalent to having a catch clause that doesn't have an
1977 // exception type, visit the block, but generate an error on any 1963 // exception type, visit the block, but generate an error on any
1978 // following catch clauses (and don't visit them). 1964 // following catch clauses (and don't visit them).
1979 catchClause?.accept(this); 1965 catchClause?.accept(this);
1980 if (i + 1 != numOfCatchClauses) { 1966 if (i + 1 != numOfCatchClauses) {
1981 // this catch clause is not the last in the try statement 1967 // this catch clause is not the last in the try statement
1982 CatchClause nextCatchClause = catchClauses[i + 1]; 1968 CatchClause nextCatchClause = catchClauses[i + 1];
1983 CatchClause lastCatchClause = catchClauses[numOfCatchClauses - 1]; 1969 CatchClause lastCatchClause = catchClauses[numOfCatchClauses - 1];
(...skipping 1794 matching lines...) Expand 10 before | Expand all | Expand 10 after
3778 } 3764 }
3779 // For switch members with no statements, don't visit the children, 3765 // For switch members with no statements, don't visit the children,
3780 // otherwise, return false if no return is found in the children 3766 // otherwise, return false if no return is found in the children
3781 // statements. 3767 // statements.
3782 if (!switchMember.statements.isEmpty && !switchMember.accept(this)) { 3768 if (!switchMember.statements.isEmpty && !switchMember.accept(this)) {
3783 return false; 3769 return false;
3784 } 3770 }
3785 } 3771 }
3786 // All of the members exit, determine whether there are possible cases 3772 // All of the members exit, determine whether there are possible cases
3787 // that are not caught by the members. 3773 // that are not caught by the members.
3788 DartType type = node.expression == null ? null : node.expression.bestType; 3774 DartType type = node.expression?.bestType;
3789 if (type is InterfaceType) { 3775 if (type is InterfaceType) {
3790 ClassElement element = type.element; 3776 ClassElement element = type.element;
3791 if (element != null && element.isEnum) { 3777 if (element != null && element.isEnum) {
3792 // If some of the enum values are not covered, then a warning will 3778 // If some of the enum values are not covered, then a warning will
3793 // have already been generated, so there's no point in generating a 3779 // have already been generated, so there's no point in generating a
3794 // hint. 3780 // hint.
3795 return true; 3781 return true;
3796 } 3782 }
3797 } 3783 }
3798 return hasDefault; 3784 return hasDefault;
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after
4507 if (importsLibrary.length == 1) { 4493 if (importsLibrary.length == 1) {
4508 ImportDirective usedImportDirective = importsLibrary[0]; 4494 ImportDirective usedImportDirective = importsLibrary[0];
4509 _unusedImports.remove(usedImportDirective); 4495 _unusedImports.remove(usedImportDirective);
4510 _removeFromUnusedShownNamesMap(element, usedImportDirective); 4496 _removeFromUnusedShownNamesMap(element, usedImportDirective);
4511 continue; 4497 continue;
4512 } 4498 }
4513 // Otherwise, find import directives using namespaces. 4499 // Otherwise, find import directives using namespaces.
4514 String name = element.displayName; 4500 String name = element.displayName;
4515 for (ImportDirective importDirective in importsLibrary) { 4501 for (ImportDirective importDirective in importsLibrary) {
4516 Namespace namespace = _computeNamespace(importDirective); 4502 Namespace namespace = _computeNamespace(importDirective);
4517 if (namespace != null && namespace.get(name) != null) { 4503 if (namespace?.get(name) != null) {
4518 _unusedImports.remove(importDirective); 4504 _unusedImports.remove(importDirective);
4519 _removeFromUnusedShownNamesMap(element, importDirective); 4505 _removeFromUnusedShownNamesMap(element, importDirective);
4520 } 4506 }
4521 } 4507 }
4522 } 4508 }
4523 } 4509 }
4524 4510
4525 /** 4511 /**
4526 * Recursively add any exported library elements into the [libraryMap]. 4512 * Recursively add any exported library elements into the [libraryMap].
4527 */ 4513 */
(...skipping 1253 matching lines...) Expand 10 before | Expand all | Expand 10 after
5781 } 5767 }
5782 return null; 5768 return null;
5783 } 5769 }
5784 5770
5785 /** 5771 /**
5786 * A client is about to resolve a member in the given class declaration. 5772 * A client is about to resolve a member in the given class declaration.
5787 */ 5773 */
5788 void prepareToResolveMembersInClass(ClassDeclaration node) { 5774 void prepareToResolveMembersInClass(ClassDeclaration node) {
5789 _enclosingClassDeclaration = node; 5775 _enclosingClassDeclaration = node;
5790 enclosingClass = node.element; 5776 enclosingClass = node.element;
5791 typeAnalyzer.thisType = enclosingClass == null ? null : enclosingClass.type; 5777 typeAnalyzer.thisType = enclosingClass?.type;
5792 } 5778 }
5793 5779
5794 /** 5780 /**
5795 * If the given [type] is valid, strongly more specific than the 5781 * If the given [type] is valid, strongly more specific than the
5796 * existing static type of the given [expression], record it as a propagated 5782 * existing static type of the given [expression], record it as a propagated
5797 * type of the given [expression]. Otherwise, reset it to `null`. 5783 * type of the given [expression]. Otherwise, reset it to `null`.
5798 * 5784 *
5799 * If [hasOldPropagatedType] is `true` then the existing propagated type 5785 * If [hasOldPropagatedType] is `true` then the existing propagated type
5800 * should also is checked. 5786 * should also is checked.
5801 */ 5787 */
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
6034 Object visitCascadeExpression(CascadeExpression node) { 6020 Object visitCascadeExpression(CascadeExpression node) {
6035 InferenceContext.setTypeFromNode(node.target, node); 6021 InferenceContext.setTypeFromNode(node.target, node);
6036 return super.visitCascadeExpression(node); 6022 return super.visitCascadeExpression(node);
6037 } 6023 }
6038 6024
6039 @override 6025 @override
6040 Object visitClassDeclaration(ClassDeclaration node) { 6026 Object visitClassDeclaration(ClassDeclaration node) {
6041 // 6027 //
6042 // Resolve the metadata in the library scope. 6028 // Resolve the metadata in the library scope.
6043 // 6029 //
6044 if (node.metadata != null) { 6030 node.metadata?.accept(this);
6045 node.metadata.accept(this);
6046 }
6047 _enclosingClassDeclaration = node; 6031 _enclosingClassDeclaration = node;
6048 // 6032 //
6049 // Continue the class resolution. 6033 // Continue the class resolution.
6050 // 6034 //
6051 ClassElement outerType = enclosingClass; 6035 ClassElement outerType = enclosingClass;
6052 try { 6036 try {
6053 enclosingClass = node.element; 6037 enclosingClass = node.element;
6054 typeAnalyzer.thisType = 6038 typeAnalyzer.thisType = enclosingClass?.type;
6055 enclosingClass == null ? null : enclosingClass.type;
6056 super.visitClassDeclaration(node); 6039 super.visitClassDeclaration(node);
6057 node.accept(elementResolver); 6040 node.accept(elementResolver);
6058 node.accept(typeAnalyzer); 6041 node.accept(typeAnalyzer);
6059 } finally { 6042 } finally {
6060 typeAnalyzer.thisType = outerType == null ? null : outerType.type; 6043 typeAnalyzer.thisType = outerType?.type;
6061 enclosingClass = outerType; 6044 enclosingClass = outerType;
6062 _enclosingClassDeclaration = null; 6045 _enclosingClassDeclaration = null;
6063 } 6046 }
6064 return null; 6047 return null;
6065 } 6048 }
6066 6049
6067 /** 6050 /**
6068 * Implementation of this method should be synchronized with 6051 * Implementation of this method should be synchronized with
6069 * [visitClassDeclaration]. 6052 * [visitClassDeclaration].
6070 */ 6053 */
6071 visitClassDeclarationIncrementally(ClassDeclaration node) { 6054 visitClassDeclarationIncrementally(ClassDeclaration node) {
6072 // 6055 //
6073 // Resolve the metadata in the library scope. 6056 // Resolve the metadata in the library scope.
6074 // 6057 //
6075 if (node.metadata != null) { 6058 node.metadata?.accept(this);
6076 node.metadata.accept(this);
6077 }
6078 _enclosingClassDeclaration = node; 6059 _enclosingClassDeclaration = node;
6079 // 6060 //
6080 // Continue the class resolution. 6061 // Continue the class resolution.
6081 // 6062 //
6082 enclosingClass = node.element; 6063 enclosingClass = node.element;
6083 typeAnalyzer.thisType = enclosingClass == null ? null : enclosingClass.type; 6064 typeAnalyzer.thisType = enclosingClass?.type;
6084 node.accept(elementResolver); 6065 node.accept(elementResolver);
6085 node.accept(typeAnalyzer); 6066 node.accept(typeAnalyzer);
6086 } 6067 }
6087 6068
6088 @override 6069 @override
6089 Object visitComment(Comment node) { 6070 Object visitComment(Comment node) {
6090 AstNode parent = node.parent; 6071 AstNode parent = node.parent;
6091 if (parent is FunctionDeclaration || 6072 if (parent is FunctionDeclaration ||
6092 parent is FunctionTypeAlias || 6073 parent is FunctionTypeAlias ||
6093 parent is ConstructorDeclaration || 6074 parent is ConstructorDeclaration ||
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
6291 if (node.metadata != null) { 6272 if (node.metadata != null) {
6292 node.metadata.accept(this); 6273 node.metadata.accept(this);
6293 ElementResolver.resolveMetadata(node); 6274 ElementResolver.resolveMetadata(node);
6294 } 6275 }
6295 // 6276 //
6296 // Continue the enum resolution. 6277 // Continue the enum resolution.
6297 // 6278 //
6298 ClassElement outerType = enclosingClass; 6279 ClassElement outerType = enclosingClass;
6299 try { 6280 try {
6300 enclosingClass = node.element; 6281 enclosingClass = node.element;
6301 typeAnalyzer.thisType = 6282 typeAnalyzer.thisType = enclosingClass?.type;
6302 enclosingClass == null ? null : enclosingClass.type;
6303 super.visitEnumDeclaration(node); 6283 super.visitEnumDeclaration(node);
6304 node.accept(elementResolver); 6284 node.accept(elementResolver);
6305 node.accept(typeAnalyzer); 6285 node.accept(typeAnalyzer);
6306 } finally { 6286 } finally {
6307 typeAnalyzer.thisType = outerType == null ? null : outerType.type; 6287 typeAnalyzer.thisType = outerType?.type;
6308 enclosingClass = outerType; 6288 enclosingClass = outerType;
6309 _enclosingClassDeclaration = null; 6289 _enclosingClassDeclaration = null;
6310 } 6290 }
6311 return null; 6291 return null;
6312 } 6292 }
6313 6293
6314 @override 6294 @override
6315 Object visitExpressionFunctionBody(ExpressionFunctionBody node) { 6295 Object visitExpressionFunctionBody(ExpressionFunctionBody node) {
6316 if (resolveOnlyCommentInFunctionBody) { 6296 if (resolveOnlyCommentInFunctionBody) {
6317 return null; 6297 return null;
(...skipping 792 matching lines...) Expand 10 before | Expand all | Expand 10 after
7110 return; 7090 return;
7111 } 7091 }
7112 FunctionExpression closure = mayBeClosure as FunctionExpression; 7092 FunctionExpression closure = mayBeClosure as FunctionExpression;
7113 // prepare expected closure type 7093 // prepare expected closure type
7114 if (mayByFunctionType is! FunctionType) { 7094 if (mayByFunctionType is! FunctionType) {
7115 return; 7095 return;
7116 } 7096 }
7117 FunctionType expectedClosureType = mayByFunctionType as FunctionType; 7097 FunctionType expectedClosureType = mayByFunctionType as FunctionType;
7118 // If the expectedClosureType is not more specific than the static type, 7098 // If the expectedClosureType is not more specific than the static type,
7119 // return. 7099 // return.
7120 DartType staticClosureType = 7100 DartType staticClosureType = closure.element?.type;
7121 closure.element != null ? closure.element.type : null;
7122 if (staticClosureType != null && 7101 if (staticClosureType != null &&
7123 !expectedClosureType.isMoreSpecificThan(staticClosureType)) { 7102 !expectedClosureType.isMoreSpecificThan(staticClosureType)) {
7124 return; 7103 return;
7125 } 7104 }
7126 // set propagated type for the closure 7105 // set propagated type for the closure
7127 closure.propagatedType = expectedClosureType; 7106 closure.propagatedType = expectedClosureType;
7128 // set inferred types for parameters 7107 // set inferred types for parameters
7129 NodeList<FormalParameter> parameters = closure.parameters.parameters; 7108 NodeList<FormalParameter> parameters = closure.parameters.parameters;
7130 List<ParameterElement> expectedParameters = expectedClosureType.parameters; 7109 List<ParameterElement> expectedParameters = expectedClosureType.parameters;
7131 for (int i = 0; 7110 for (int i = 0;
(...skipping 1255 matching lines...) Expand 10 before | Expand all | Expand 10 after
8387 TypeArgumentList argumentList = node.typeArguments; 8366 TypeArgumentList argumentList = node.typeArguments;
8388 Element element = nameScope.lookup(typeName, definingLibrary); 8367 Element element = nameScope.lookup(typeName, definingLibrary);
8389 if (element == null) { 8368 if (element == null) {
8390 // 8369 //
8391 // Check to see whether the type name is either 'dynamic' or 'void', 8370 // Check to see whether the type name is either 'dynamic' or 'void',
8392 // neither of which are in the name scope and hence will not be found by 8371 // neither of which are in the name scope and hence will not be found by
8393 // normal means. 8372 // normal means.
8394 // 8373 //
8395 if (typeName.name == dynamicType.name) { 8374 if (typeName.name == dynamicType.name) {
8396 _setElement(typeName, dynamicType.element); 8375 _setElement(typeName, dynamicType.element);
8397 if (argumentList != null) { 8376 // if (argumentList != null) {
8398 // TODO(brianwilkerson) Report this error 8377 // // TODO(brianwilkerson) Report this error
8399 // reporter.reportError(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGU MENTS, node, dynamicType.getName(), 0, argumentList.getArguments().size()); 8378 // reporter.reportError(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGU MENTS, node, dynamicType.getName(), 0, argumentList.getArguments().size());
8400 } 8379 // }
8401 typeName.staticType = dynamicType; 8380 typeName.staticType = dynamicType;
8402 node.type = dynamicType; 8381 node.type = dynamicType;
8403 return; 8382 return;
8404 } 8383 }
8405 VoidTypeImpl voidType = VoidTypeImpl.instance; 8384 VoidTypeImpl voidType = VoidTypeImpl.instance;
8406 if (typeName.name == voidType.name) { 8385 if (typeName.name == voidType.name) {
8407 // There is no element for 'void'. 8386 // There is no element for 'void'.
8408 if (argumentList != null) { 8387 // if (argumentList != null) {
8409 // TODO(brianwilkerson) Report this error 8388 // // TODO(brianwilkerson) Report this error
8410 // reporter.reportError(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGU MENTS, node, voidType.getName(), 0, argumentList.getArguments().size()); 8389 // reporter.reportError(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGU MENTS, node, voidType.getName(), 0, argumentList.getArguments().size());
8411 } 8390 // }
8412 typeName.staticType = voidType; 8391 typeName.staticType = voidType;
8413 node.type = voidType; 8392 node.type = voidType;
8414 return; 8393 return;
8415 } 8394 }
8416 // 8395 //
8417 // If not, the look to see whether we might have created the wrong AST 8396 // If not, the look to see whether we might have created the wrong AST
8418 // structure for a constructor name. If so, fix the AST structure and then 8397 // structure for a constructor name. If so, fix the AST structure and then
8419 // proceed. 8398 // proceed.
8420 // 8399 //
8421 AstNode parent = node.parent; 8400 AstNode parent = node.parent;
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
8551 DartType type = null; 8530 DartType type = null;
8552 if (element is ClassElement) { 8531 if (element is ClassElement) {
8553 _setElement(typeName, element); 8532 _setElement(typeName, element);
8554 type = element.type; 8533 type = element.type;
8555 } else if (element is FunctionTypeAliasElement) { 8534 } else if (element is FunctionTypeAliasElement) {
8556 _setElement(typeName, element); 8535 _setElement(typeName, element);
8557 type = element.type; 8536 type = element.type;
8558 } else if (element is TypeParameterElement) { 8537 } else if (element is TypeParameterElement) {
8559 _setElement(typeName, element); 8538 _setElement(typeName, element);
8560 type = element.type; 8539 type = element.type;
8561 if (argumentList != null) { 8540 // if (argumentList != null) {
8562 // Type parameters cannot have type arguments. 8541 // // Type parameters cannot have type arguments.
8563 // TODO(brianwilkerson) Report this error. 8542 // // TODO(brianwilkerson) Report this error.
8564 // resolver.reportError(ResolverErrorCode.?, keyType); 8543 // // resolver.reportError(ResolverErrorCode.?, keyType);
8565 } 8544 // }
8566 } else if (element is MultiplyDefinedElement) { 8545 } else if (element is MultiplyDefinedElement) {
8567 List<Element> elements = element.conflictingElements; 8546 List<Element> elements = element.conflictingElements;
8568 type = _getTypeWhenMultiplyDefined(elements); 8547 type = _getTypeWhenMultiplyDefined(elements);
8569 if (type != null) { 8548 if (type != null) {
8570 node.type = type; 8549 node.type = type;
8571 } 8550 }
8572 } else { 8551 } else {
8573 // The name does not represent a type. 8552 // The name does not represent a type.
8574 RedirectingConstructorKind redirectingConstructorKind; 8553 RedirectingConstructorKind redirectingConstructorKind;
8575 if (_isTypeNameInCatchClause(node)) { 8554 if (_isTypeNameInCatchClause(node)) {
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
8923 8902
8924 /** 8903 /**
8925 * Return the best type information available for the given element. If the ty pe of the element 8904 * Return the best type information available for the given element. If the ty pe of the element
8926 * has been overridden, then return the overriding type. Otherwise, return the static type. 8905 * has been overridden, then return the overriding type. Otherwise, return the static type.
8927 * 8906 *
8928 * @param element the element for which type information is to be returned 8907 * @param element the element for which type information is to be returned
8929 * @return the best type information available for the given element 8908 * @return the best type information available for the given element
8930 */ 8909 */
8931 DartType getBestType(VariableElement element) { 8910 DartType getBestType(VariableElement element) {
8932 DartType bestType = getType(element); 8911 DartType bestType = getType(element);
8933 return bestType == null ? element.type : bestType; 8912 return bestType ?? element.type;
8934 } 8913 }
8935 8914
8936 /** 8915 /**
8937 * Return the overridden type of the given element, or `null` if the type of t he element has 8916 * Return the overridden type of the given element, or `null` if the type of t he element has
8938 * not been overridden. 8917 * not been overridden.
8939 * 8918 *
8940 * @param element the element whose type might have been overridden 8919 * @param element the element whose type might have been overridden
8941 * @return the overridden type of the given element 8920 * @return the overridden type of the given element
8942 */ 8921 */
8943 DartType getType(Element element) { 8922 DartType getType(Element element) {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
9051 * @param element the element whose type might have been overridden 9030 * @param element the element whose type might have been overridden
9052 * @return the overridden type of the given element 9031 * @return the overridden type of the given element
9053 */ 9032 */
9054 DartType getType(Element element) { 9033 DartType getType(Element element) {
9055 Element nonAccessor = 9034 Element nonAccessor =
9056 element is PropertyAccessorElement ? element.variable : element; 9035 element is PropertyAccessorElement ? element.variable : element;
9057 DartType type = _overridenTypes[nonAccessor]; 9036 DartType type = _overridenTypes[nonAccessor];
9058 if (_overridenTypes.containsKey(nonAccessor)) { 9037 if (_overridenTypes.containsKey(nonAccessor)) {
9059 return type; 9038 return type;
9060 } 9039 }
9061 if (type != null) { 9040 return type ?? _outerScope?.getType(element);
9062 return type;
9063 } else if (_outerScope != null) {
9064 return _outerScope.getType(nonAccessor);
9065 }
9066 return null;
9067 } 9041 }
9068 9042
9069 /** 9043 /**
9070 * Clears the overridden type of the given [element]. 9044 * Clears the overridden type of the given [element].
9071 */ 9045 */
9072 void resetType(VariableElement element) { 9046 void resetType(VariableElement element) {
9073 _overridenTypes[element] = null; 9047 _overridenTypes[element] = null;
9074 } 9048 }
9075 9049
9076 /** 9050 /**
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
9110 * Exit the current promotion scope. 9084 * Exit the current promotion scope.
9111 */ 9085 */
9112 void exitScope() { 9086 void exitScope() {
9113 if (currentScope == null) { 9087 if (currentScope == null) {
9114 throw new IllegalStateException("No scope to exit"); 9088 throw new IllegalStateException("No scope to exit");
9115 } 9089 }
9116 currentScope = currentScope._outerScope; 9090 currentScope = currentScope._outerScope;
9117 } 9091 }
9118 9092
9119 /** 9093 /**
9120 * Returns static type of the given variable - declared or promoted. 9094 * Return the static type of the given [variable] - declared or promoted.
9121 *
9122 * @return the static type of the given variable - declared or promoted
9123 */ 9095 */
9124 DartType getStaticType(VariableElement variable) { 9096 DartType getStaticType(VariableElement variable) =>
9125 DartType staticType = getType(variable); 9097 getType(variable) ?? variable.type;
9126 if (staticType == null) {
9127 staticType = variable.type;
9128 }
9129 return staticType;
9130 }
9131 9098
9132 /** 9099 /**
9133 * Return the promoted type of the given element, or `null` if the type of the element has 9100 * Return the promoted type of the given [element], or `null` if the type of
9134 * not been promoted. 9101 * the element has not been promoted.
9135 *
9136 * @param element the element whose type might have been promoted
9137 * @return the promoted type of the given element
9138 */ 9102 */
9139 DartType getType(Element element) { 9103 DartType getType(Element element) => currentScope?.getType(element);
9140 if (currentScope == null) {
9141 return null;
9142 }
9143 return currentScope.getType(element);
9144 }
9145 9104
9146 /** 9105 /**
9147 * Set the promoted type of the given element to the given type. 9106 * Set the promoted type of the given element to the given type.
9148 * 9107 *
9149 * @param element the element whose type might have been promoted 9108 * @param element the element whose type might have been promoted
9150 * @param type the promoted type of the given element 9109 * @param type the promoted type of the given element
9151 */ 9110 */
9152 void setType(Element element, DartType type) { 9111 void setType(Element element, DartType type) {
9153 if (currentScope == null) { 9112 if (currentScope == null) {
9154 throw new IllegalStateException("Cannot promote without a scope"); 9113 throw new IllegalStateException("Cannot promote without a scope");
(...skipping 1593 matching lines...) Expand 10 before | Expand all | Expand 10 after
10748 10707
10749 TypeSystem _typeSystem; 10708 TypeSystem _typeSystem;
10750 10709
10751 _ConstantVerifier_validateInitializerExpression( 10710 _ConstantVerifier_validateInitializerExpression(
10752 TypeProvider typeProvider, 10711 TypeProvider typeProvider,
10753 ErrorReporter errorReporter, 10712 ErrorReporter errorReporter,
10754 this.verifier, 10713 this.verifier,
10755 this.parameterElements, 10714 this.parameterElements,
10756 DeclaredVariables declaredVariables, 10715 DeclaredVariables declaredVariables,
10757 {TypeSystem typeSystem}) 10716 {TypeSystem typeSystem})
10758 : _typeSystem = (typeSystem != null) ? typeSystem : new TypeSystemImpl(), 10717 : _typeSystem = typeSystem ?? new TypeSystemImpl(),
10759 super( 10718 super(
10760 new ConstantEvaluationEngine(typeProvider, declaredVariables, 10719 new ConstantEvaluationEngine(typeProvider, declaredVariables,
10761 typeSystem: typeSystem), 10720 typeSystem: typeSystem),
10762 errorReporter); 10721 errorReporter);
10763 10722
10764 @override 10723 @override
10765 DartObjectImpl visitSimpleIdentifier(SimpleIdentifier node) { 10724 DartObjectImpl visitSimpleIdentifier(SimpleIdentifier node) {
10766 Element element = node.staticElement; 10725 Element element = node.staticElement;
10767 for (ParameterElement parameterElement in parameterElements) { 10726 for (ParameterElement parameterElement in parameterElements) {
10768 if (identical(parameterElement, element) && parameterElement != null) { 10727 if (identical(parameterElement, element) && parameterElement != null) {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
10852 return null; 10811 return null;
10853 } 10812 }
10854 if (identical(node.staticElement, variable)) { 10813 if (identical(node.staticElement, variable)) {
10855 if (node.inSetterContext()) { 10814 if (node.inSetterContext()) {
10856 result = true; 10815 result = true;
10857 } 10816 }
10858 } 10817 }
10859 return null; 10818 return null;
10860 } 10819 }
10861 } 10820 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/parser.dart ('k') | pkg/analyzer/lib/src/generated/source.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698