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

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

Issue 1410643003: Add a public API for the results of constant evaluation (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 2 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
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 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
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
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
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 13984 matching lines...) Expand 10 before | Expand all | Expand 10 after
15715 ExecutableElement outerFunction = _enclosingFunction; 15715 ExecutableElement outerFunction = _enclosingFunction;
15716 try { 15716 try {
15717 _enclosingFunction = node.element; 15717 _enclosingFunction = node.element;
15718 return super.visitMethodDeclaration(node); 15718 return super.visitMethodDeclaration(node);
15719 } finally { 15719 } finally {
15720 _enclosingFunction = outerFunction; 15720 _enclosingFunction = outerFunction;
15721 } 15721 }
15722 } 15722 }
15723 15723
15724 @override 15724 @override
15725 Object visitTypeName(TypeName node) {
15726 return null;
15727 }
15728
15729 @override
15730 Object visitSimpleIdentifier(SimpleIdentifier node) { 15725 Object visitSimpleIdentifier(SimpleIdentifier node) {
15731 // Ignore if already resolved - declaration or type. 15726 // Ignore if already resolved - declaration or type.
15732 if (node.inDeclarationContext()) { 15727 if (node.inDeclarationContext()) {
15733 return null; 15728 return null;
15734 } 15729 }
15735 // Ignore if it cannot be a reference to a local variable. 15730 // Ignore if it cannot be a reference to a local variable.
15736 AstNode parent = node.parent; 15731 AstNode parent = node.parent;
15737 if (parent is FieldFormalParameter) { 15732 if (parent is FieldFormalParameter) {
15738 return null; 15733 return null;
15739 } else if (parent is ConstructorDeclaration && parent.returnType == node) { 15734 } else if (parent is ConstructorDeclaration && parent.returnType == node) {
15740 return null; 15735 return null;
15741 } else if (parent is ConstructorFieldInitializer && parent.fieldName == node ) { 15736 } else if (parent is ConstructorFieldInitializer &&
15737 parent.fieldName == node) {
15742 return null; 15738 return null;
15743 } 15739 }
15744 // Ignore if qualified. 15740 // Ignore if qualified.
15745 if (parent is PrefixedIdentifier && identical(parent.identifier, node)) { 15741 if (parent is PrefixedIdentifier && identical(parent.identifier, node)) {
15746 return null; 15742 return null;
15747 } 15743 }
15748 if (parent is PropertyAccess && identical(parent.propertyName, node)) { 15744 if (parent is PropertyAccess && identical(parent.propertyName, node)) {
15749 return null; 15745 return null;
15750 } 15746 }
15751 if (parent is MethodInvocation && 15747 if (parent is MethodInvocation &&
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
15784 // 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
15785 // variable is declared. 15781 // variable is declared.
15786 if (_enclosingFunction != null && 15782 if (_enclosingFunction != null &&
15787 (element.enclosingElement != _enclosingFunction)) { 15783 (element.enclosingElement != _enclosingFunction)) {
15788 parameterImpl.markPotentiallyMutatedInClosure(); 15784 parameterImpl.markPotentiallyMutatedInClosure();
15789 } 15785 }
15790 } 15786 }
15791 } 15787 }
15792 return null; 15788 return null;
15793 } 15789 }
15790
15791 @override
15792 Object visitTypeName(TypeName node) {
15793 return null;
15794 }
15794 } 15795 }
15795 15796
15796 class _ConstantVerifier_validateInitializerExpression extends ConstantVisitor { 15797 class _ConstantVerifier_validateInitializerExpression extends ConstantVisitor {
15797 final ConstantVerifier verifier; 15798 final ConstantVerifier verifier;
15798 15799
15799 List<ParameterElement> parameterElements; 15800 List<ParameterElement> parameterElements;
15800 15801
15801 TypeSystem _typeSystem; 15802 TypeSystem _typeSystem;
15802 15803
15803 _ConstantVerifier_validateInitializerExpression( 15804 _ConstantVerifier_validateInitializerExpression(
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
15962 nonFields.add(node); 15963 nonFields.add(node);
15963 return null; 15964 return null;
15964 } 15965 }
15965 15966
15966 @override 15967 @override
15967 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); 15968 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this);
15968 15969
15969 @override 15970 @override
15970 Object visitWithClause(WithClause node) => null; 15971 Object visitWithClause(WithClause node) => null;
15971 } 15972 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/element_handle.dart ('k') | pkg/analyzer/test/generated/all_the_rest_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698