| 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 'ast.dart'; | 9 import 'ast.dart'; |
| 10 import 'constant.dart'; | 10 import 'constant.dart'; |
| (...skipping 1503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1514 @override | 1514 @override |
| 1515 Object visitBinaryExpression(BinaryExpression node) { | 1515 Object visitBinaryExpression(BinaryExpression node) { |
| 1516 sc.Token operator = node.operator; | 1516 sc.Token operator = node.operator; |
| 1517 bool isAmpAmp = operator.type == sc.TokenType.AMPERSAND_AMPERSAND; | 1517 bool isAmpAmp = operator.type == sc.TokenType.AMPERSAND_AMPERSAND; |
| 1518 bool isBarBar = operator.type == sc.TokenType.BAR_BAR; | 1518 bool isBarBar = operator.type == sc.TokenType.BAR_BAR; |
| 1519 if (isAmpAmp || isBarBar) { | 1519 if (isAmpAmp || isBarBar) { |
| 1520 Expression lhsCondition = node.leftOperand; | 1520 Expression lhsCondition = node.leftOperand; |
| 1521 if (!_isDebugConstant(lhsCondition)) { | 1521 if (!_isDebugConstant(lhsCondition)) { |
| 1522 EvaluationResultImpl lhsResult = _getConstantBooleanValue(lhsCondition); | 1522 EvaluationResultImpl lhsResult = _getConstantBooleanValue(lhsCondition); |
| 1523 if (lhsResult != null) { | 1523 if (lhsResult != null) { |
| 1524 if (lhsResult.value.isTrue && isBarBar) { | 1524 if (lhsResult.value.toBoolValue() == true && isBarBar) { |
| 1525 // report error on else block: true || !e! | 1525 // report error on else block: true || !e! |
| 1526 _errorReporter.reportErrorForNode( | 1526 _errorReporter.reportErrorForNode( |
| 1527 HintCode.DEAD_CODE, node.rightOperand); | 1527 HintCode.DEAD_CODE, node.rightOperand); |
| 1528 // only visit the LHS: | 1528 // only visit the LHS: |
| 1529 _safelyVisit(lhsCondition); | 1529 _safelyVisit(lhsCondition); |
| 1530 return null; | 1530 return null; |
| 1531 } else if (lhsResult.value.isFalse && isAmpAmp) { | 1531 } else if (lhsResult.value.toBoolValue() == false && isAmpAmp) { |
| 1532 // report error on if block: false && !e! | 1532 // report error on if block: false && !e! |
| 1533 _errorReporter.reportErrorForNode( | 1533 _errorReporter.reportErrorForNode( |
| 1534 HintCode.DEAD_CODE, node.rightOperand); | 1534 HintCode.DEAD_CODE, node.rightOperand); |
| 1535 // only visit the LHS: | 1535 // only visit the LHS: |
| 1536 _safelyVisit(lhsCondition); | 1536 _safelyVisit(lhsCondition); |
| 1537 return null; | 1537 return null; |
| 1538 } | 1538 } |
| 1539 } | 1539 } |
| 1540 } | 1540 } |
| 1541 // How do we want to handle the RHS? It isn't dead code, but "pointless" | 1541 // How do we want to handle the RHS? It isn't dead code, but "pointless" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1575 } | 1575 } |
| 1576 | 1576 |
| 1577 @override | 1577 @override |
| 1578 Object visitConditionalExpression(ConditionalExpression node) { | 1578 Object visitConditionalExpression(ConditionalExpression node) { |
| 1579 Expression conditionExpression = node.condition; | 1579 Expression conditionExpression = node.condition; |
| 1580 _safelyVisit(conditionExpression); | 1580 _safelyVisit(conditionExpression); |
| 1581 if (!_isDebugConstant(conditionExpression)) { | 1581 if (!_isDebugConstant(conditionExpression)) { |
| 1582 EvaluationResultImpl result = | 1582 EvaluationResultImpl result = |
| 1583 _getConstantBooleanValue(conditionExpression); | 1583 _getConstantBooleanValue(conditionExpression); |
| 1584 if (result != null) { | 1584 if (result != null) { |
| 1585 if (result.value.isTrue) { | 1585 if (result.value.toBoolValue() == true) { |
| 1586 // report error on else block: true ? 1 : !2! | 1586 // report error on else block: true ? 1 : !2! |
| 1587 _errorReporter.reportErrorForNode( | 1587 _errorReporter.reportErrorForNode( |
| 1588 HintCode.DEAD_CODE, node.elseExpression); | 1588 HintCode.DEAD_CODE, node.elseExpression); |
| 1589 _safelyVisit(node.thenExpression); | 1589 _safelyVisit(node.thenExpression); |
| 1590 return null; | 1590 return null; |
| 1591 } else { | 1591 } else { |
| 1592 // report error on if block: false ? !1! : 2 | 1592 // report error on if block: false ? !1! : 2 |
| 1593 _errorReporter.reportErrorForNode( | 1593 _errorReporter.reportErrorForNode( |
| 1594 HintCode.DEAD_CODE, node.thenExpression); | 1594 HintCode.DEAD_CODE, node.thenExpression); |
| 1595 _safelyVisit(node.elseExpression); | 1595 _safelyVisit(node.elseExpression); |
| 1596 return null; | 1596 return null; |
| 1597 } | 1597 } |
| 1598 } | 1598 } |
| 1599 } | 1599 } |
| 1600 return super.visitConditionalExpression(node); | 1600 return super.visitConditionalExpression(node); |
| 1601 } | 1601 } |
| 1602 | 1602 |
| 1603 @override | 1603 @override |
| 1604 Object visitIfStatement(IfStatement node) { | 1604 Object visitIfStatement(IfStatement node) { |
| 1605 Expression conditionExpression = node.condition; | 1605 Expression conditionExpression = node.condition; |
| 1606 _safelyVisit(conditionExpression); | 1606 _safelyVisit(conditionExpression); |
| 1607 if (!_isDebugConstant(conditionExpression)) { | 1607 if (!_isDebugConstant(conditionExpression)) { |
| 1608 EvaluationResultImpl result = | 1608 EvaluationResultImpl result = |
| 1609 _getConstantBooleanValue(conditionExpression); | 1609 _getConstantBooleanValue(conditionExpression); |
| 1610 if (result != null) { | 1610 if (result != null) { |
| 1611 if (result.value.isTrue) { | 1611 if (result.value.toBoolValue() == true) { |
| 1612 // report error on else block: if(true) {} else {!} | 1612 // report error on else block: if(true) {} else {!} |
| 1613 Statement elseStatement = node.elseStatement; | 1613 Statement elseStatement = node.elseStatement; |
| 1614 if (elseStatement != null) { | 1614 if (elseStatement != null) { |
| 1615 _errorReporter.reportErrorForNode( | 1615 _errorReporter.reportErrorForNode( |
| 1616 HintCode.DEAD_CODE, elseStatement); | 1616 HintCode.DEAD_CODE, elseStatement); |
| 1617 _safelyVisit(node.thenStatement); | 1617 _safelyVisit(node.thenStatement); |
| 1618 return null; | 1618 return null; |
| 1619 } | 1619 } |
| 1620 } else { | 1620 } else { |
| 1621 // report error on if block: if (false) {!} else {} | 1621 // report error on if block: if (false) {!} else {} |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1710 } | 1710 } |
| 1711 | 1711 |
| 1712 @override | 1712 @override |
| 1713 Object visitWhileStatement(WhileStatement node) { | 1713 Object visitWhileStatement(WhileStatement node) { |
| 1714 Expression conditionExpression = node.condition; | 1714 Expression conditionExpression = node.condition; |
| 1715 _safelyVisit(conditionExpression); | 1715 _safelyVisit(conditionExpression); |
| 1716 if (!_isDebugConstant(conditionExpression)) { | 1716 if (!_isDebugConstant(conditionExpression)) { |
| 1717 EvaluationResultImpl result = | 1717 EvaluationResultImpl result = |
| 1718 _getConstantBooleanValue(conditionExpression); | 1718 _getConstantBooleanValue(conditionExpression); |
| 1719 if (result != null) { | 1719 if (result != null) { |
| 1720 if (result.value.isFalse) { | 1720 if (result.value.toBoolValue() == false) { |
| 1721 // report error on if block: while (false) {!} | 1721 // report error on if block: while (false) {!} |
| 1722 _errorReporter.reportErrorForNode(HintCode.DEAD_CODE, node.body); | 1722 _errorReporter.reportErrorForNode(HintCode.DEAD_CODE, node.body); |
| 1723 return null; | 1723 return null; |
| 1724 } | 1724 } |
| 1725 } | 1725 } |
| 1726 } | 1726 } |
| 1727 _safelyVisit(node.body); | 1727 _safelyVisit(node.body); |
| 1728 return null; | 1728 return null; |
| 1729 } | 1729 } |
| 1730 | 1730 |
| (...skipping 1496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3227 variableName.staticElement = element; | 3227 variableName.staticElement = element; |
| 3228 } else { | 3228 } else { |
| 3229 SimpleIdentifier variableName = node.name; | 3229 SimpleIdentifier variableName = node.name; |
| 3230 TopLevelVariableElementImpl variable; | 3230 TopLevelVariableElementImpl variable; |
| 3231 if (isConst && hasInitializer) { | 3231 if (isConst && hasInitializer) { |
| 3232 variable = new ConstTopLevelVariableElementImpl(variableName); | 3232 variable = new ConstTopLevelVariableElementImpl(variableName); |
| 3233 } else { | 3233 } else { |
| 3234 variable = new TopLevelVariableElementImpl.forNode(variableName); | 3234 variable = new TopLevelVariableElementImpl.forNode(variableName); |
| 3235 } | 3235 } |
| 3236 element = variable; | 3236 element = variable; |
| 3237 if (node.parent.parent is TopLevelVariableDeclaration) { |
| 3238 _setDocRange(element, node.parent.parent); |
| 3239 } |
| 3237 if ((node.parent as VariableDeclarationList).type == null) { | 3240 if ((node.parent as VariableDeclarationList).type == null) { |
| 3238 variable.hasImplicitType = true; | 3241 variable.hasImplicitType = true; |
| 3239 } | 3242 } |
| 3240 _currentHolder.addTopLevelVariable(variable); | 3243 _currentHolder.addTopLevelVariable(variable); |
| 3241 variableName.staticElement = element; | 3244 variableName.staticElement = element; |
| 3242 } | 3245 } |
| 3243 element.const3 = isConst; | 3246 element.const3 = isConst; |
| 3244 element.final2 = isFinal; | 3247 element.final2 = isFinal; |
| 3245 if (hasInitializer) { | 3248 if (hasInitializer) { |
| 3246 ElementHolder holder = new ElementHolder(); | 3249 ElementHolder holder = new ElementHolder(); |
| (...skipping 11165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 14412 element.getAncestor((element) => element is ClassElement); | 14415 element.getAncestor((element) => element is ClassElement); |
| 14413 if (definingClass != null) { | 14416 if (definingClass != null) { |
| 14414 type.typeArguments = definingClass.type.typeArguments; | 14417 type.typeArguments = definingClass.type.typeArguments; |
| 14415 } | 14418 } |
| 14416 element.type = type; | 14419 element.type = type; |
| 14417 if (element is PropertyAccessorElement) { | 14420 if (element is PropertyAccessorElement) { |
| 14418 PropertyAccessorElement accessor = element as PropertyAccessorElement; | 14421 PropertyAccessorElement accessor = element as PropertyAccessorElement; |
| 14419 PropertyInducingElementImpl variable = | 14422 PropertyInducingElementImpl variable = |
| 14420 accessor.variable as PropertyInducingElementImpl; | 14423 accessor.variable as PropertyInducingElementImpl; |
| 14421 if (accessor.isGetter) { | 14424 if (accessor.isGetter) { |
| 14422 variable.type = type.returnType; | 14425 variable.type = type.baseReturnType; |
| 14423 } else if (variable.type == null) { | 14426 } else if (variable.type == null) { |
| 14424 List<DartType> parameterTypes = type.normalParameterTypes; | 14427 List<ParameterElement> parameters = type.baseParameters; |
| 14425 if (parameterTypes != null && parameterTypes.length > 0) { | 14428 if (parameters != null && parameters.length > 0) { |
| 14426 variable.type = parameterTypes[0]; | 14429 variable.type = parameters[0].type; |
| 14427 } | 14430 } |
| 14428 } | 14431 } |
| 14429 } | 14432 } |
| 14430 return null; | 14433 return null; |
| 14431 } | 14434 } |
| 14432 | 14435 |
| 14433 @override | 14436 @override |
| 14434 Object visitSimpleFormalParameter(SimpleFormalParameter node) { | 14437 Object visitSimpleFormalParameter(SimpleFormalParameter node) { |
| 14435 super.visitSimpleFormalParameter(node); | 14438 super.visitSimpleFormalParameter(node); |
| 14436 DartType declaredType; | 14439 DartType declaredType; |
| (...skipping 1277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 15714 _enclosingFunction = node.element; | 15717 _enclosingFunction = node.element; |
| 15715 return super.visitMethodDeclaration(node); | 15718 return super.visitMethodDeclaration(node); |
| 15716 } finally { | 15719 } finally { |
| 15717 _enclosingFunction = outerFunction; | 15720 _enclosingFunction = outerFunction; |
| 15718 } | 15721 } |
| 15719 } | 15722 } |
| 15720 | 15723 |
| 15721 @override | 15724 @override |
| 15722 Object visitSimpleIdentifier(SimpleIdentifier node) { | 15725 Object visitSimpleIdentifier(SimpleIdentifier node) { |
| 15723 // Ignore if already resolved - declaration or type. | 15726 // Ignore if already resolved - declaration or type. |
| 15724 if (node.staticElement != null) { | 15727 if (node.inDeclarationContext()) { |
| 15728 return null; |
| 15729 } |
| 15730 // Ignore if it cannot be a reference to a local variable. |
| 15731 AstNode parent = node.parent; |
| 15732 if (parent is FieldFormalParameter) { |
| 15733 return null; |
| 15734 } else if (parent is ConstructorDeclaration && parent.returnType == node) { |
| 15735 return null; |
| 15736 } else if (parent is ConstructorFieldInitializer && |
| 15737 parent.fieldName == node) { |
| 15725 return null; | 15738 return null; |
| 15726 } | 15739 } |
| 15727 // Ignore if qualified. | 15740 // Ignore if qualified. |
| 15728 AstNode parent = node.parent; | |
| 15729 if (parent is PrefixedIdentifier && identical(parent.identifier, node)) { | 15741 if (parent is PrefixedIdentifier && identical(parent.identifier, node)) { |
| 15730 return null; | 15742 return null; |
| 15731 } | 15743 } |
| 15732 if (parent is PropertyAccess && identical(parent.propertyName, node)) { | 15744 if (parent is PropertyAccess && identical(parent.propertyName, node)) { |
| 15733 return null; | 15745 return null; |
| 15734 } | 15746 } |
| 15735 if (parent is MethodInvocation && | 15747 if (parent is MethodInvocation && |
| 15736 identical(parent.methodName, node) && | 15748 identical(parent.methodName, node) && |
| 15737 parent.realTarget != null) { | 15749 parent.realTarget != null) { |
| 15738 return null; | 15750 return null; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 15768 // If we are in some closure, check if it is not the same as where | 15780 // If we are in some closure, check if it is not the same as where |
| 15769 // variable is declared. | 15781 // variable is declared. |
| 15770 if (_enclosingFunction != null && | 15782 if (_enclosingFunction != null && |
| 15771 (element.enclosingElement != _enclosingFunction)) { | 15783 (element.enclosingElement != _enclosingFunction)) { |
| 15772 parameterImpl.markPotentiallyMutatedInClosure(); | 15784 parameterImpl.markPotentiallyMutatedInClosure(); |
| 15773 } | 15785 } |
| 15774 } | 15786 } |
| 15775 } | 15787 } |
| 15776 return null; | 15788 return null; |
| 15777 } | 15789 } |
| 15790 |
| 15791 @override |
| 15792 Object visitTypeName(TypeName node) { |
| 15793 return null; |
| 15794 } |
| 15778 } | 15795 } |
| 15779 | 15796 |
| 15780 class _ConstantVerifier_validateInitializerExpression extends ConstantVisitor { | 15797 class _ConstantVerifier_validateInitializerExpression extends ConstantVisitor { |
| 15781 final ConstantVerifier verifier; | 15798 final ConstantVerifier verifier; |
| 15782 | 15799 |
| 15783 List<ParameterElement> parameterElements; | 15800 List<ParameterElement> parameterElements; |
| 15784 | 15801 |
| 15785 TypeSystem _typeSystem; | 15802 TypeSystem _typeSystem; |
| 15786 | 15803 |
| 15787 _ConstantVerifier_validateInitializerExpression( | 15804 _ConstantVerifier_validateInitializerExpression( |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 15946 nonFields.add(node); | 15963 nonFields.add(node); |
| 15947 return null; | 15964 return null; |
| 15948 } | 15965 } |
| 15949 | 15966 |
| 15950 @override | 15967 @override |
| 15951 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); | 15968 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); |
| 15952 | 15969 |
| 15953 @override | 15970 @override |
| 15954 Object visitWithClause(WithClause node) => null; | 15971 Object visitWithClause(WithClause node) => null; |
| 15955 } | 15972 } |
| OLD | NEW |