Using new ast instead of utilities.

Adapted from https://github.com/develar/kotlin/commit/a9e0a42fb1347fa8e21c86b5a073ef8a7c873da0.
This commit is contained in:
Pavel V. Talanov
2012-08-10 21:19:37 +04:00
parent d6a6bd7cac
commit 86b85177b7
18 changed files with 46 additions and 91 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.k2js.translate.expression;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.JsBlock;
import com.google.dart.compiler.backend.js.ast.JsCatch;
import com.google.dart.compiler.backend.js.ast.JsName;
@@ -27,7 +28,6 @@ import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.general.AbstractTranslator;
import org.jetbrains.k2js.translate.general.Translation;
import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.convertToBlock;
@@ -55,11 +55,7 @@ public final class TryTranslator extends AbstractTranslator {
}
private JsTry translate() {
JsTry result = new JsTry();
result.setTryBlock(translateTryBlock());
result.setFinallyBlock(translateFinallyBlock());
result.getCatches().addAll(translateCatches());
return result;
return new JsTry(translateTryBlock(), translateCatches(), translateFinallyBlock());
}
@Nullable
@@ -75,10 +71,9 @@ public final class TryTranslator extends AbstractTranslator {
return convertToBlock(Translation.translateAsStatement(expression.getTryBlock(), context()));
}
@NotNull
private List<JsCatch> translateCatches() {
List<JsCatch> result = new ArrayList<JsCatch>();
List<JsCatch> result = Lists.newArrayList();
for (JetCatchClause catchClause : expression.getCatchClauses()) {
result.add(translateCatchClause(catchClause));
}
@@ -67,7 +67,7 @@ public final class WhenTranslator extends AbstractTranslator {
@NotNull
JsNode translate() {
JsFor resultingFor = generateDummyFor();
resultingFor.setBody(newBlock(translateEntries()));
resultingFor.setBody(new JsBlock(translateEntries()));
context().addStatementToCurrentBlock(resultingFor);
return result.reference();
}
@@ -82,7 +82,7 @@ public final class ArrayForTranslator extends ForTranslator {
List<JsStatement> blockStatements = Lists.newArrayList();
blockStatements.add(temporariesInitialization(loopRange, end).makeStmt());
blockStatements.add(generateForExpression(getInitExpression(), getCondition(), getIncrementExpression(), getBody()));
return newBlock(blockStatements);
return new JsBlock(blockStatements);
}
@@ -86,7 +86,7 @@ public final class RangeLiteralForTranslator extends ForTranslator {
getCondition(),
getIncrExpression(),
translateOriginalBodyExpression()));
return newBlock(blockStatements);
return new JsBlock(blockStatements);
}
@NotNull
@@ -101,6 +101,6 @@ public final class RangeLiteralForTranslator extends ForTranslator {
@NotNull
private JsExpression getIncrExpression() {
return new JsPrefixOperation(JsUnaryOperator.INC, parameterName.makeRef());
return new JsPostfixOperation(JsUnaryOperator.INC, parameterName.makeRef());
}
}
@@ -34,7 +34,8 @@ import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.k2js.translate.utils.BindingUtils.*;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.convertToStatement;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.setParameters;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getPrimaryConstructorParameters;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.translateArgumentList;
@@ -90,7 +91,7 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
}
else {
JsName superMethodName = context().scope().declareName(Namer.superMethodName());
initializerStatements.add(convertToStatement(newInvocation(thisQualifiedReference(superMethodName), arguments)));
initializerStatements.add(convertToStatement(new JsInvocation(new JsNameRef(superMethodName, JsLiteral.THIS), arguments)));
}
}
@@ -17,17 +17,14 @@
package org.jetbrains.k2js.translate.intrinsic.functions.basic;
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.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import java.util.List;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.newInvocation;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.setQualifier;
/**
* @author Pavel Talanov
*/
@@ -45,8 +42,6 @@ public final class BuiltInFunctionIntrinsic extends FunctionIntrinsic {
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
@NotNull TranslationContext context) {
assert receiver != null;
JsNameRef functionReference = AstUtil.newQualifiedNameRef(functionName);
setQualifier(functionReference, receiver);
return newInvocation(functionReference, arguments);
return new JsInvocation(new JsNameRef(functionName, receiver), arguments);
}
}
@@ -2,15 +2,12 @@ package org.jetbrains.k2js.translate.intrinsic.functions.basic;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import java.util.List;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.setQualifier;
/**
* @author Pavel Talanov
*/
@@ -30,8 +27,6 @@ public final class BuiltInPropertyIntrinsic extends FunctionIntrinsic {
@NotNull TranslationContext context) {
assert receiver != null;
assert arguments.isEmpty() : "Properties can't have arguments.";
JsNameRef propertyReference = AstUtil.newQualifiedNameRef(propertyName);
setQualifier(propertyReference, receiver);
return propertyReference;
return new JsNameRef(propertyName, receiver);
}
}
@@ -18,8 +18,8 @@ package org.jetbrains.k2js.translate.intrinsic.functions.basic;
import com.google.common.collect.Lists;
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.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.translate.context.TranslationContext;
@@ -27,20 +27,18 @@ import org.jetbrains.k2js.translate.context.TranslationContext;
import java.util.List;
import static org.jetbrains.k2js.translate.utils.ErrorReportingUtils.atLocation;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.newInvocation;
/**
* @author Pavel Talanov
*/
public final class CallStandardMethodIntrinsic extends FunctionIntrinsic {
@NotNull
private final String methodName;
private final JsNameRef methodName;
private final boolean receiverShouldBeNotNull;
private final int expectedParamsNumber;
public CallStandardMethodIntrinsic(@NotNull String methodName, boolean receiverShouldBeNotNull, int expectedParamsNumber) {
public CallStandardMethodIntrinsic(@NotNull JsNameRef methodName, boolean receiverShouldBeNotNull, int expectedParamsNumber) {
this.methodName = methodName;
this.receiverShouldBeNotNull = receiverShouldBeNotNull;
this.expectedParamsNumber = expectedParamsNumber;
@@ -53,14 +51,13 @@ public final class CallStandardMethodIntrinsic extends FunctionIntrinsic {
@NotNull TranslationContext context) {
assert (receiver != null == receiverShouldBeNotNull);
assert arguments.size() == expectedParamsNumber : errorMessage(receiver, arguments);
JsNameRef iteratorFunName = AstUtil.newQualifiedNameRef(methodName);
return newInvocation(iteratorFunName, composeArguments(receiver, arguments));
return new JsInvocation(methodName, composeArguments(receiver, arguments));
}
@NotNull
private String errorMessage(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments) {
return "Incorrect number of arguments " + arguments.size() + " when expected " + expectedParamsNumber + " on method " + methodName + " " +
atLocation(receiver, arguments);
atLocation(receiver, arguments);
}
@NotNull
@@ -75,4 +72,4 @@ public final class CallStandardMethodIntrinsic extends FunctionIntrinsic {
return arguments;
}
}
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.k2js.translate.intrinsic.functions.factories;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.JsArrayAccess;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -91,8 +92,8 @@ public final class ArrayFIF extends CompositeFIF {
add(pattern(ARRAYS, "get"), GET_INTRINSIC);
add(pattern(ARRAYS, "set"), SET_INTRINSIC);
add(pattern(ARRAYS, "<get-size>"), ARRAY_LENGTH_INTRINSIC);
add(pattern(ARRAYS, "<get-indices>"), new CallStandardMethodIntrinsic("Kotlin.arrayIndices", true, 0));
add(pattern(ARRAYS, "iterator"), new CallStandardMethodIntrinsic("Kotlin.arrayIterator", true, 0));
add(pattern(ARRAYS, "<init>"), new CallStandardMethodIntrinsic("Kotlin.arrayFromFun", false, 2));
add(pattern(ARRAYS, "<get-indices>"), new CallStandardMethodIntrinsic(new JsNameRef("arrayIndices", "Kotlin"), true, 0));
add(pattern(ARRAYS, "iterator"), new CallStandardMethodIntrinsic(new JsNameRef("arrayIterator", "Kotlin"), true, 0));
add(pattern(ARRAYS, "<init>"), new CallStandardMethodIntrinsic(new JsNameRef("arrayFromFun", "Kotlin"), false, 2));
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.k2js.translate.intrinsic.functions.factories;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethodIntrinsic;
@@ -30,7 +31,7 @@ public final class RangesFIF extends CompositeFIF {
public static final FunctionIntrinsicFactory INSTANCE = new RangesFIF();
private RangesFIF() {
add(pattern("Int.upto"), new CallStandardMethodIntrinsic("Kotlin.intUpto", true, 1));
add(pattern("Int.downto"), new CallStandardMethodIntrinsic("Kotlin.intDownto", true, 1));
add(pattern("Int.upto"), new CallStandardMethodIntrinsic(new JsNameRef("intUpto", "Kotlin"), true, 1));
add(pattern("Int.downto"), new CallStandardMethodIntrinsic(new JsNameRef("intDownto", "Kotlin"), true, 1));
}
}
@@ -17,6 +17,7 @@
package org.jetbrains.k2js.translate.intrinsic.functions.factories;
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.util.AstUtil;
import org.jetbrains.annotations.NotNull;
@@ -29,7 +30,6 @@ import org.jetbrains.k2js.translate.intrinsic.functions.patterns.DescriptorPredi
import java.util.List;
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.newInvocation;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.setQualifier;
/**
@@ -52,7 +52,7 @@ public final class StringOperationFIF extends CompositeFIF {
//TODO: provide better way
JsNameRef charAtReference = AstUtil.newQualifiedNameRef("charAt");
setQualifier(charAtReference, receiver);
return newInvocation(charAtReference, arguments);
return new JsInvocation(charAtReference, arguments);
}
};
@@ -16,6 +16,7 @@
package org.jetbrains.k2js.translate.intrinsic.functions.factories;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.BuiltInFunctionIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethodIntrinsic;
@@ -28,7 +29,7 @@ import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternB
*/
public final class TopLevelFIF extends CompositeFIF {
@NotNull
public static final CallStandardMethodIntrinsic EQUALS = new CallStandardMethodIntrinsic("Kotlin.equals", true, 1);
public static final CallStandardMethodIntrinsic EQUALS = new CallStandardMethodIntrinsic(new JsNameRef("equals", "Kotlin"), true, 1);
@NotNull
public static final FunctionIntrinsicFactory INSTANCE = new TopLevelFIF();
@@ -37,6 +38,6 @@ public final class TopLevelFIF extends CompositeFIF {
add(pattern("equals"), EQUALS);
add(pattern(NamePredicate.PRIMITIVE_NUMBERS, "equals"), EQUALS);
add(pattern("String|Boolean|Char|Number.equals"), EQUALS);
add(pattern("arrayOfNulls"), new CallStandardMethodIntrinsic("Kotlin.nullArray", false, 1));
add(pattern("arrayOfNulls"), new CallStandardMethodIntrinsic(new JsNameRef("nullArray", "Kotlin"), false, 1));
}
}
@@ -17,6 +17,8 @@
package org.jetbrains.k2js.translate.operation;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
@@ -89,7 +91,7 @@ public abstract class IncrementTranslator extends AbstractTranslator {
JsExpression getExpression = accessTranslator.translateAsGet();
JsExpression reassignment = variableReassignment(getExpression);
JsExpression getNewValue = accessTranslator.translateAsGet();
return AstUtil.newSequence(reassignment, getNewValue);
return new JsBinaryOperation(JsBinaryOperator.COMMA, reassignment, getNewValue);
}
//TODO: decide if this expression can be optimised in case of direct access (not property)
@@ -96,8 +96,6 @@ public abstract class AbstractCallExpressionTranslator extends AbstractTranslato
@NotNull
private static List<JsExpression> wrapInArrayLiteral(@NotNull List<JsExpression> translatedArgs) {
JsArrayLiteral argsWrappedInArray = new JsArrayLiteral();
argsWrappedInArray.getExpressions().addAll(translatedArgs);
return Arrays.<JsExpression>asList(argsWrappedInArray);
return Collections.<JsExpression>singletonList(new JsArrayLiteral(translatedArgs));
}
}
@@ -196,9 +196,7 @@ public final class CallTranslator extends AbstractTranslator {
@NotNull
private JsExpression constructExtensionLiteralCall(@NotNull JsExpression realReceiver) {
List<JsExpression> callArguments = generateExtensionCallArgumentList(realReceiver);
JsInvocation callMethodInvocation = generateCallMethodInvocation();
setArguments(callMethodInvocation, callArguments);
return callMethodInvocation;
return new JsInvocation(new JsNameRef("call", callParameters.getFunctionReference()), callArguments);
}
@NotNull
@@ -233,7 +231,7 @@ public final class CallTranslator extends AbstractTranslator {
List<JsExpression> argumentList = generateExtensionCallArgumentList(receiver);
JsExpression functionReference = callParameters.getFunctionReference();
setQualifier(functionReference, getThisObjectOrQualifier());
return newInvocation(functionReference, argumentList);
return new JsInvocation(functionReference, argumentList);
}
@NotNull
@@ -256,7 +254,7 @@ public final class CallTranslator extends AbstractTranslator {
return ecma5PropertyAccess(qualifiedCallee);
}
return newInvocation(qualifiedCallee, arguments);
return new JsInvocation(qualifiedCallee, arguments);
}
}, context());
}
@@ -18,6 +18,7 @@ package org.jetbrains.k2js.translate.reference;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsName;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
@@ -25,7 +26,6 @@ import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.k2js.translate.context.TranslationContext;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.assignment;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.qualified;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getExpectedThisDescriptor;
/**
@@ -71,7 +71,7 @@ public final class NativePropertyAccessTranslator extends PropertyAccessTranslat
private JsExpression doTranslateAsGet(JsExpression receiver) {
JsName nativePropertyName = context().getNameForDescriptor(propertyDescriptor);
if (receiver != null) {
return qualified(nativePropertyName, receiver);
return new JsNameRef(nativePropertyName, receiver);
}
else {
return nativePropertyName.makeRef();
@@ -18,13 +18,13 @@ package org.jetbrains.k2js.translate.reference;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsName;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.k2js.translate.context.TranslationContext;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.qualified;
import static org.jetbrains.k2js.translate.utils.PsiUtils.isBackingFieldReference;
/**
@@ -49,7 +49,7 @@ public final class ReferenceTranslator {
return translateAsLocalNameReference(referencedDescriptor, context);
}
JsName referencedName = context.getNameForDescriptor(referencedDescriptor);
return qualified(referencedName, qualifier);
return new JsNameRef(referencedName, qualifier);
}
@NotNull
@@ -102,7 +102,7 @@ public final class TranslationUtils {
public static JsNameRef backingFieldReference(@NotNull TranslationContext context,
@NotNull PropertyDescriptor descriptor) {
JsName backingFieldName = context.getNameForDescriptor(descriptor);
return qualified(backingFieldName, JsLiteral.THIS);
return new JsNameRef(backingFieldName, JsLiteral.THIS);
}
@NotNull
@@ -110,7 +110,7 @@ public final class TranslationUtils {
@NotNull PropertyDescriptor descriptor,
@NotNull JsExpression assignTo) {
JsNameRef backingFieldReference = backingFieldReference(context, descriptor);
return JsAstUtils.assignment(backingFieldReference, assignTo);
return assignment(backingFieldReference, assignTo);
}
@Nullable
@@ -125,37 +125,8 @@ public final class TranslationUtils {
}
@NotNull
public static JsNameRef getQualifiedReference(@NotNull TranslationContext context,
@NotNull DeclarationDescriptor descriptor) {
JsName name = context.getNameForDescriptor(descriptor);
JsNameRef reference = name.makeRef();
JsNameRef qualifier = context.getQualifierForDescriptor(descriptor);
if (qualifier != null) {
setQualifier(reference, qualifier);
}
return reference;
}
//TODO: refactor
@NotNull
public static JsExpression getThisObject(@NotNull TranslationContext context,
@NotNull DeclarationDescriptor correspondingDeclaration) {
if (correspondingDeclaration instanceof ClassDescriptor) {
JsName alias = context.aliasingContext().getAliasForThis(correspondingDeclaration);
if (alias != null) {
return alias.makeRef();
}
}
if (correspondingDeclaration instanceof CallableDescriptor) {
DeclarationDescriptor receiverDescriptor =
getExpectedReceiverDescriptor((CallableDescriptor) correspondingDeclaration);
assert receiverDescriptor != null;
JsName alias = context.aliasingContext().getAliasForThis(receiverDescriptor);
if (alias != null) {
return alias.makeRef();
}
}
return JsLiteral.THIS;
public static JsNameRef getQualifiedReference(@NotNull TranslationContext context, @NotNull DeclarationDescriptor descriptor) {
return new JsNameRef(context.getNameForDescriptor(descriptor), context.getQualifierForDescriptor(descriptor));
}
@NotNull