From be1d7c979b1acb80d240b90c5a42a4a3ec49c7a0 Mon Sep 17 00:00:00 2001 From: Pavel Talanov Date: Thu, 24 Nov 2011 23:10:43 +0400 Subject: [PATCH] Binary operation overloading for most cases (except contains, compareTo). --- .idea/workspace.xml | 517 ++++++++++-------- .../translate/BinaryOperationTranslator.java | 44 +- .../k2js/translate/OperationTranslator.java | 19 +- .../translate/UnaryOperationTranslator.java | 3 +- .../operatorOverloading/cases/compareTo.kt | 0 5 files changed, 329 insertions(+), 254 deletions(-) create mode 100644 translator/testFiles/operatorOverloading/cases/compareTo.kt diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 23395ef3d72..2ff0f5bb8d5 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -8,16 +8,11 @@ - - - + - - - @@ -48,6 +43,47 @@ + + + + + + + + - + - + - - - - + + - + - + @@ -117,25 +151,16 @@ - + - - + + - - - - - - - - - - + @@ -144,7 +169,16 @@ - + + + + + + + + + + @@ -153,52 +187,52 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - + @@ -225,7 +259,6 @@ + + + @@ -283,6 +320,20 @@ + + + + + + + + + + @@ -390,6 +441,32 @@ + + + + + + + + + + + @@ -549,20 +626,6 @@ - - - - - - - - - - @@ -598,54 +661,6 @@ - - - - - - - - - - - - - - - - - - - - - - @@ -738,6 +753,40 @@ + + + + + + + + + + + + + + + + - - - - - + + + + + localhost @@ -872,29 +921,49 @@ + + + + + + + + + + + + + + + + + + + + - + - - + - + - - + + - + - - + + + @@ -936,7 +1005,7 @@ @@ -983,48 +1052,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1032,68 +1059,98 @@ - + - - - - - - - - - - - - - - - - - + - - - + - + - - - - - - - - - - - - - - - - - - - - - - + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/translator/src/org/jetbrains/k2js/translate/BinaryOperationTranslator.java b/translator/src/org/jetbrains/k2js/translate/BinaryOperationTranslator.java index 921d4bd4777..738d12aa0d1 100644 --- a/translator/src/org/jetbrains/k2js/translate/BinaryOperationTranslator.java +++ b/translator/src/org/jetbrains/k2js/translate/BinaryOperationTranslator.java @@ -1,14 +1,12 @@ package org.jetbrains.k2js.translate; -import com.google.dart.compiler.backend.js.ast.JsBinaryOperation; -import com.google.dart.compiler.backend.js.ast.JsExpression; -import com.google.dart.compiler.backend.js.ast.JsInvocation; -import com.google.dart.compiler.backend.js.ast.JsNameRef; +import com.google.dart.compiler.backend.js.ast.*; import com.google.dart.compiler.util.AstUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.psi.JetBinaryExpression; import org.jetbrains.jet.lang.psi.JetExpression; +import org.jetbrains.jet.lang.types.expressions.OperatorConventions; import org.jetbrains.jet.lexer.JetToken; /** @@ -39,7 +37,7 @@ public final class BinaryOperationTranslator extends OperationTranslator { this.expression = expression; this.isPropertyOnTheLeft = isPropertyAccess(expression.getLeft()); this.isVariableReassignment = isVariableReassignment(expression); - this.operationReference = getOverloadedOperationReference(expression); + this.operationReference = getOverloadedOperationReference(expression.getOperationReference()); this.right = translateRightExpression(); //TODO: decide whether it is harmful to possibly translate left expression more than once this.left = translateLeftExpression(); @@ -47,20 +45,53 @@ public final class BinaryOperationTranslator extends OperationTranslator { @NotNull JsExpression translate() { - if (operationReference != null) { + if (isCompareTo()) { + return asCompareToOverload(); + } + if (isOverloadedCall()) { return asOverloadedMethodCall(); } return asBinaryOperation(); } + @NotNull + private JsExpression asCompareToOverload() { + JetToken operationToken = getOperationToken(); + assert (OperatorConventions.COMPARISON_OPERATIONS.contains(operationToken)); + JsNumberLiteral zetoLiteral = program().getNumberLiteral(0); + JsBinaryOperator correspondingOperator = OperatorTable.getBinaryOperator(operationToken); + return new JsBinaryOperation(correspondingOperator, overloadedMethodInvocation(), zetoLiteral); + } + + private boolean isOverloadedCall() { + return operationReference != null; + } + + private boolean isCompareTo() { + if (operationReference == null) { + return false; + } + String nameForOperationSymbol = OperatorConventions.getNameForOperationSymbol(getOperationToken()); + assert nameForOperationSymbol != null : "Must have a name for overloaded operator"; + return (nameForOperationSymbol.equals("compareTo")); + } + @NotNull private JsExpression asOverloadedMethodCall() { if (isPropertyOnTheLeft) { return overloadOnProperty(); } + if (isVariableReassignment) { + return nonPropertyReassignment(); + } return overloadedMethodInvocation(); } + private JsExpression nonPropertyReassignment() { + assert left instanceof JsNameRef : "Reassignment should be called on l-value."; + return AstUtil.newAssignment((JsNameRef) left, overloadedMethodInvocation()); + } + @NotNull private JsExpression overloadOnProperty() { if (isVariableReassignment) { @@ -117,5 +148,4 @@ public final class BinaryOperationTranslator extends OperationTranslator { return setterCall; } - } diff --git a/translator/src/org/jetbrains/k2js/translate/OperationTranslator.java b/translator/src/org/jetbrains/k2js/translate/OperationTranslator.java index b66ede09aab..040bf12c914 100644 --- a/translator/src/org/jetbrains/k2js/translate/OperationTranslator.java +++ b/translator/src/org/jetbrains/k2js/translate/OperationTranslator.java @@ -7,10 +7,8 @@ import com.google.dart.compiler.util.AstUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.psi.JetBinaryExpression; import org.jetbrains.jet.lang.psi.JetExpression; import org.jetbrains.jet.lang.psi.JetSimpleNameExpression; -import org.jetbrains.jet.lang.psi.JetUnaryExpression; /** * @author Talanov Pavel @@ -22,8 +20,8 @@ public class OperationTranslator extends AbstractTranslator { } @Nullable - protected JsNameRef getOverloadedOperationReference(@NotNull JetExpression expression) { - DeclarationDescriptor operationDescriptor = getOperationDescriptor(expression); + protected JsNameRef getOverloadedOperationReference(@NotNull JetSimpleNameExpression operationExpression) { + DeclarationDescriptor operationDescriptor = getOperationDescriptor(operationExpression); if (operationDescriptor == null) { return null; } @@ -34,20 +32,11 @@ public class OperationTranslator extends AbstractTranslator { } @Nullable - private DeclarationDescriptor getOperationDescriptor(@NotNull JetExpression expression) { - JetSimpleNameExpression operationReference = null; - if (expression instanceof JetBinaryExpression) { - operationReference = ((JetBinaryExpression) expression).getOperationReference(); - } - if (expression instanceof JetUnaryExpression) { - operationReference = ((JetUnaryExpression) expression).getOperationSign(); - } - assert operationReference != null : "Should be applied only to unary or binary operations"; + private DeclarationDescriptor getOperationDescriptor(@NotNull JetSimpleNameExpression expression) { return BindingUtils.getDescriptorForReferenceExpression - (translationContext().bindingContext(), operationReference); + (translationContext().bindingContext(), expression); } - protected boolean isPropertyAccess(@NotNull JetExpression expression) { return Translation.propertyAccessTranslator(translationContext()).canBePropertyAccess(expression); diff --git a/translator/src/org/jetbrains/k2js/translate/UnaryOperationTranslator.java b/translator/src/org/jetbrains/k2js/translate/UnaryOperationTranslator.java index 72d924d32a8..aff3c5ddaef 100644 --- a/translator/src/org/jetbrains/k2js/translate/UnaryOperationTranslator.java +++ b/translator/src/org/jetbrains/k2js/translate/UnaryOperationTranslator.java @@ -48,7 +48,7 @@ public final class UnaryOperationTranslator extends OperationTranslator { this.isStatement = BindingUtils.isStatement(translationContext().bindingContext(), expression); this.baseExpression = translateBaseExpression(); this.isPropertyAccess = isPropertyAccess(getBaseExpression()); - this.operationReference = getOverloadedOperationReference(expression); + this.operationReference = getOverloadedOperationReference(expression.getOperationSign()); } @NotNull @@ -103,7 +103,6 @@ public final class UnaryOperationTranslator extends OperationTranslator { return AstUtil.newSequence(t1.assignmentExpression(), t2.assignmentExpression(), methodCall, returnedValue); } - //TODO: should modify this for properties @NotNull private JsExpression variableReassignment(@NotNull JsExpression toCallMethodUpon) { diff --git a/translator/testFiles/operatorOverloading/cases/compareTo.kt b/translator/testFiles/operatorOverloading/cases/compareTo.kt new file mode 100644 index 00000000000..e69de29bb2d