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

Unified Diff: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/resolver/StaticTypeAnalyzer.java

Issue 15901004: Issue 1015. Make 'double' type contagious in some binary expressions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/resolver/StaticTypeAnalyzerTest.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/resolver/StaticTypeAnalyzer.java
diff --git a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/resolver/StaticTypeAnalyzer.java b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/resolver/StaticTypeAnalyzer.java
index 6cbe31773b80ec2778b2e7e804b8bc43d0e0c99b..eae7b2c6cc55d4aae2f9cfa2276a7c0972ac0604 100644
--- a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/resolver/StaticTypeAnalyzer.java
+++ b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/resolver/StaticTypeAnalyzer.java
@@ -13,6 +13,7 @@
*/
package com.google.dart.engine.internal.resolver;
+import com.google.common.annotations.VisibleForTesting;
import com.google.dart.engine.ast.ASTNode;
import com.google.dart.engine.ast.AdjacentStrings;
import com.google.dart.engine.ast.ArgumentDefinitionTest;
@@ -215,6 +216,17 @@ public class StaticTypeAnalyzer extends SimpleASTVisitor<Void> {
}
/**
+ * Return a table mapping nodes in the AST to the element produced based on static type
+ * information.
+ *
+ * @return the static element map
+ */
+ @VisibleForTesting
+ public HashMap<ASTNode, ExecutableElement> getStaticElementMap() {
+ return staticElementMap;
+ }
+
+ /**
* Set the type of the class being analyzed to the given type.
*
* @param thisType the type representing the class containing the nodes being analyzed
@@ -374,25 +386,7 @@ public class StaticTypeAnalyzer extends SimpleASTVisitor<Void> {
public Void visitBinaryExpression(BinaryExpression node) {
ExecutableElement staticMethodElement = staticElementMap.get(node);
Type staticType = computeReturnType(staticMethodElement);
- TokenType operator = node.getOperator().getType();
- if (operator == TokenType.AMPERSAND_AMPERSAND || operator == TokenType.BAR_BAR
- || operator == TokenType.EQ_EQ || operator == TokenType.BANG_EQ) {
- staticType = typeProvider.getBoolType();
- } else if (operator == TokenType.MINUS || operator == TokenType.PERCENT
- || operator == TokenType.PLUS || operator == TokenType.STAR
- || operator == TokenType.TILDE_SLASH) {
- Type intType = typeProvider.getIntType();
- if (getStaticType(node.getLeftOperand()) == intType
- && getStaticType(node.getRightOperand()) == intType) {
- staticType = intType;
- }
- } else if (operator == TokenType.SLASH) {
- Type doubleType = typeProvider.getDoubleType();
- if (getStaticType(node.getLeftOperand()) == doubleType
- || getStaticType(node.getRightOperand()) == doubleType) {
- staticType = doubleType;
- }
- }
+ staticType = refineBinaryExpressionType(node, staticType);
recordStaticType(node, staticType);
MethodElement propagatedMethodElement = node.getElement();
@@ -1627,6 +1621,42 @@ public class StaticTypeAnalyzer extends SimpleASTVisitor<Void> {
}
/**
+ * Attempts to make a better guess for the static type of the given binary expression.
+ *
+ * @param node the binary expression to analyze
+ * @param staticType the static type of the expression as resolved
+ * @return the better type guess, or the same static type as given
+ */
+ private Type refineBinaryExpressionType(BinaryExpression node, Type staticType) {
+ TokenType operator = node.getOperator().getType();
+ // bool
+ if (operator == TokenType.AMPERSAND_AMPERSAND || operator == TokenType.BAR_BAR
+ || operator == TokenType.EQ_EQ || operator == TokenType.BANG_EQ) {
+ return typeProvider.getBoolType();
+ }
+ // int op double
+ if (operator == TokenType.MINUS || operator == TokenType.PERCENT || operator == TokenType.PLUS
+ || operator == TokenType.STAR) {
+ Type doubleType = typeProvider.getDoubleType();
+ if (getStaticType(node.getLeftOperand()) == doubleType
+ || getStaticType(node.getRightOperand()) == doubleType) {
+ return doubleType;
+ }
+ }
+ // int op int
+ if (operator == TokenType.MINUS || operator == TokenType.PERCENT || operator == TokenType.PLUS
+ || operator == TokenType.STAR || operator == TokenType.TILDE_SLASH) {
+ Type intType = typeProvider.getIntType();
+ if (getStaticType(node.getLeftOperand()) == intType
+ && getStaticType(node.getRightOperand()) == intType) {
+ staticType = intType;
+ }
+ }
+ // default
+ return staticType;
+ }
+
+ /**
* Set the return type and parameter type information for the given function type based on the
* given return type and parameter elements.
*
« no previous file with comments | « no previous file | editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/resolver/StaticTypeAnalyzerTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698