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

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

Issue 1124433007: Fix constant evaluation logic for String.length. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/all_the_rest_test.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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library engine.constant; 8 library engine.constant;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
(...skipping 1296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1307 new DartObjectComputer(_errorReporter, _typeProvider); 1307 new DartObjectComputer(_errorReporter, _typeProvider);
1308 } 1308 }
1309 1309
1310 /** 1310 /**
1311 * This method is called just before retrieving an evaluation result from an 1311 * This method is called just before retrieving an evaluation result from an
1312 * AST node. Unit tests will override it to introduce additional error 1312 * AST node. Unit tests will override it to introduce additional error
1313 * checking. 1313 * checking.
1314 */ 1314 */
1315 void beforeGetEvaluationResult(AstNode node) {} 1315 void beforeGetEvaluationResult(AstNode node) {}
1316 1316
1317 /**
1318 * Return `true` if the given [element] represents the `length` getter in
1319 * class 'String'.
1320 */
1321 bool isStringLength(Element element) {
1322 if (element is PropertyAccessorElement) {
1323 if (element.isGetter && element.name == 'length') {
1324 return element.enclosingElement == _typeProvider.stringType.element;
1325 }
1326 }
1327 return false;
1328 }
1329
1330 @override 1317 @override
1331 DartObjectImpl visitAdjacentStrings(AdjacentStrings node) { 1318 DartObjectImpl visitAdjacentStrings(AdjacentStrings node) {
1332 DartObjectImpl result = null; 1319 DartObjectImpl result = null;
1333 for (StringLiteral string in node.strings) { 1320 for (StringLiteral string in node.strings) {
1334 if (result == null) { 1321 if (result == null) {
1335 result = string.accept(this); 1322 result = string.accept(this);
1336 } else { 1323 } else {
1337 result = 1324 result =
1338 _dartObjectComputer.concatenate(node, result, string.accept(this)); 1325 _dartObjectComputer.concatenate(node, result, string.accept(this));
1339 } 1326 }
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
1593 1580
1594 @override 1581 @override
1595 DartObjectImpl visitNullLiteral(NullLiteral node) => _typeProvider.nullObject; 1582 DartObjectImpl visitNullLiteral(NullLiteral node) => _typeProvider.nullObject;
1596 1583
1597 @override 1584 @override
1598 DartObjectImpl visitParenthesizedExpression(ParenthesizedExpression node) => 1585 DartObjectImpl visitParenthesizedExpression(ParenthesizedExpression node) =>
1599 node.expression.accept(this); 1586 node.expression.accept(this);
1600 1587
1601 @override 1588 @override
1602 DartObjectImpl visitPrefixedIdentifier(PrefixedIdentifier node) { 1589 DartObjectImpl visitPrefixedIdentifier(PrefixedIdentifier node) {
1590 SimpleIdentifier prefixNode = node.prefix;
1591 Element prefixElement = prefixNode.staticElement;
1603 // String.length 1592 // String.length
1604 { 1593 if (prefixElement is! PrefixElement && prefixElement is! ClassElement) {
1605 Element element = node.staticElement; 1594 DartObjectImpl prefixResult = node.prefix.accept(this);
1606 if (isStringLength(element)) { 1595 if (_isStringLength(prefixResult, node.identifier)) {
1607 DartObjectImpl prefixResult = node.prefix.accept(this);
1608 return prefixResult.stringLength(_typeProvider); 1596 return prefixResult.stringLength(_typeProvider);
1609 } 1597 }
1610 } 1598 }
1611 // importPrefix.CONST 1599 // importPrefix.CONST
1612 SimpleIdentifier prefixNode = node.prefix;
1613 Element prefixElement = prefixNode.staticElement;
1614 if (prefixElement is! PrefixElement) { 1600 if (prefixElement is! PrefixElement) {
1615 DartObjectImpl prefixResult = prefixNode.accept(this); 1601 DartObjectImpl prefixResult = prefixNode.accept(this);
1616 if (prefixResult == null) { 1602 if (prefixResult == null) {
1617 // The error has already been reported. 1603 // The error has already been reported.
1618 return null; 1604 return null;
1619 } 1605 }
1620 } 1606 }
1621 // validate prefixed identifier 1607 // validate prefixed identifier
1622 return _getConstantValue(node, node.staticElement); 1608 return _getConstantValue(node, node.staticElement);
1623 } 1609 }
(...skipping 16 matching lines...) Expand all
1640 // TODO(brianwilkerson) Figure out which error to report. 1626 // TODO(brianwilkerson) Figure out which error to report.
1641 _error(node, null); 1627 _error(node, null);
1642 return null; 1628 return null;
1643 } 1629 }
1644 break; 1630 break;
1645 } 1631 }
1646 } 1632 }
1647 1633
1648 @override 1634 @override
1649 DartObjectImpl visitPropertyAccess(PropertyAccess node) { 1635 DartObjectImpl visitPropertyAccess(PropertyAccess node) {
1650 Element element = node.propertyName.staticElement; 1636 if (node.target != null) {
1651 if (isStringLength(element)) { 1637 DartObjectImpl prefixResult = node.target.accept(this);
1652 DartObjectImpl prefixResult = node.realTarget.accept(this); 1638 if (_isStringLength(prefixResult, node.propertyName)) {
1653 return prefixResult.stringLength(_typeProvider); 1639 return prefixResult.stringLength(_typeProvider);
1640 }
1654 } 1641 }
1655 return _getConstantValue(node, element); 1642 return _getConstantValue(node, node.propertyName.staticElement);
1656 } 1643 }
1657 1644
1658 @override 1645 @override
1659 DartObjectImpl visitSimpleIdentifier(SimpleIdentifier node) { 1646 DartObjectImpl visitSimpleIdentifier(SimpleIdentifier node) {
1660 if (_lexicalEnvironment != null && 1647 if (_lexicalEnvironment != null &&
1661 _lexicalEnvironment.containsKey(node.name)) { 1648 _lexicalEnvironment.containsKey(node.name)) {
1662 return _lexicalEnvironment[node.name]; 1649 return _lexicalEnvironment[node.name];
1663 } 1650 }
1664 return _getConstantValue(node, node.staticElement); 1651 return _getConstantValue(node, node.staticElement);
1665 } 1652 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1736 element is FunctionTypeAliasElement || 1723 element is FunctionTypeAliasElement ||
1737 element is DynamicElementImpl) { 1724 element is DynamicElementImpl) {
1738 return new DartObjectImpl(_typeProvider.typeType, new TypeState(element)); 1725 return new DartObjectImpl(_typeProvider.typeType, new TypeState(element));
1739 } 1726 }
1740 // TODO(brianwilkerson) Figure out which error to report. 1727 // TODO(brianwilkerson) Figure out which error to report.
1741 _error(node, null); 1728 _error(node, null);
1742 return null; 1729 return null;
1743 } 1730 }
1744 1731
1745 /** 1732 /**
1733 * Return `true` if the given [targetResult] represents a string and the
1734 * [identifier] is "length".
1735 */
1736 bool _isStringLength(
1737 DartObjectImpl targetResult, SimpleIdentifier identifier) {
1738 if (targetResult == null || targetResult.type != _typeProvider.stringType) {
1739 return false;
1740 }
1741 return identifier.name == 'length';
1742 }
1743
1744 /**
1746 * Return the value of the given [expression], or a representation of 'null' 1745 * Return the value of the given [expression], or a representation of 'null'
1747 * if the expression cannot be evaluated. 1746 * if the expression cannot be evaluated.
1748 */ 1747 */
1749 DartObjectImpl _valueOf(Expression expression) { 1748 DartObjectImpl _valueOf(Expression expression) {
1750 DartObjectImpl expressionValue = expression.accept(this); 1749 DartObjectImpl expressionValue = expression.accept(this);
1751 if (expressionValue != null) { 1750 if (expressionValue != null) {
1752 return expressionValue; 1751 return expressionValue;
1753 } 1752 }
1754 return _typeProvider.nullObject; 1753 return _typeProvider.nullObject;
1755 } 1754 }
(...skipping 3413 matching lines...) Expand 10 before | Expand all | Expand 10 after
5169 return BoolState.from(_element == rightElement); 5168 return BoolState.from(_element == rightElement);
5170 } else if (rightOperand is DynamicState) { 5169 } else if (rightOperand is DynamicState) {
5171 return BoolState.UNKNOWN_VALUE; 5170 return BoolState.UNKNOWN_VALUE;
5172 } 5171 }
5173 return BoolState.FALSE_STATE; 5172 return BoolState.FALSE_STATE;
5174 } 5173 }
5175 5174
5176 @override 5175 @override
5177 String toString() => _element == null ? "-unknown-" : _element.name; 5176 String toString() => _element == null ? "-unknown-" : _element.name;
5178 } 5177 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/all_the_rest_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698