context().bindingContext() -> bindingContext() (same for program() and aliaser())
refactored CallExpressionTranslator, also worked on vararg problem
This commit is contained in:
+6
-6
@@ -117,8 +117,8 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
|
|||||||
|
|
||||||
private void removeAliases() {
|
private void removeAliases() {
|
||||||
for (JetClass jetClass : getClassDeclarations()) {
|
for (JetClass jetClass : getClassDeclarations()) {
|
||||||
ClassDescriptor descriptor = BindingUtils.getClassDescriptor(context().bindingContext(), jetClass);
|
ClassDescriptor descriptor = BindingUtils.getClassDescriptor(bindingContext(), jetClass);
|
||||||
context().aliaser().removeAliasForDescriptor(descriptor);
|
aliaser().removeAliasForDescriptor(descriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,9 +126,9 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
|
|||||||
private List<JetClass> getClassDeclarations() {
|
private List<JetClass> getClassDeclarations() {
|
||||||
List<JetClass> classes = new ArrayList<JetClass>();
|
List<JetClass> classes = new ArrayList<JetClass>();
|
||||||
for (ClassDescriptor classDescriptor : descriptors) {
|
for (ClassDescriptor classDescriptor : descriptors) {
|
||||||
classes.add(BindingUtils.getClassForDescriptor(context().bindingContext(), classDescriptor));
|
classes.add(BindingUtils.getClassForDescriptor(bindingContext(), classDescriptor));
|
||||||
}
|
}
|
||||||
return ClassSorter.sortUsingInheritanceOrder(classes, context().bindingContext());
|
return ClassSorter.sortUsingInheritanceOrder(classes, bindingContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -144,8 +144,8 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
|
|||||||
JsName globalClassName = context().getNameForElement(declaration);
|
JsName globalClassName = context().getNameForElement(declaration);
|
||||||
JsName localAlias = dummyFunctionScope.declareTemporary();
|
JsName localAlias = dummyFunctionScope.declareTemporary();
|
||||||
localToGlobalClassName.put(localAlias, globalClassName);
|
localToGlobalClassName.put(localAlias, globalClassName);
|
||||||
ClassDescriptor descriptor = BindingUtils.getClassDescriptor(context().bindingContext(), declaration);
|
ClassDescriptor descriptor = BindingUtils.getClassDescriptor(bindingContext(), declaration);
|
||||||
context().aliaser().setAliasForDescriptor(descriptor, localAlias);
|
aliaser().setAliasForDescriptor(descriptor, localAlias);
|
||||||
return localAlias;
|
return localAlias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -142,8 +142,8 @@ public final class ClassTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression getClassReference(@NotNull ClassDescriptor superClassDescriptor) {
|
private JsExpression getClassReference(@NotNull ClassDescriptor superClassDescriptor) {
|
||||||
//NOTE: aliasing here is needed for the declaration generation step
|
//NOTE: aliasing here is needed for the declaration generation step
|
||||||
if (context().aliaser().hasAliasForDeclaration(superClassDescriptor)) {
|
if (aliaser().hasAliasForDeclaration(superClassDescriptor)) {
|
||||||
return context().aliaser().getAliasForDeclaration(superClassDescriptor).makeRef();
|
return aliaser().getAliasForDeclaration(superClassDescriptor).makeRef();
|
||||||
}
|
}
|
||||||
return context().getNameForDescriptor(superClassDescriptor).makeRef();
|
return context().getNameForDescriptor(superClassDescriptor).makeRef();
|
||||||
}
|
}
|
||||||
@@ -171,7 +171,7 @@ public final class ClassTranslator extends AbstractTranslator {
|
|||||||
List<JsPropertyInitializer> result = new ArrayList<JsPropertyInitializer>();
|
List<JsPropertyInitializer> result = new ArrayList<JsPropertyInitializer>();
|
||||||
for (JetParameter parameter : getPrimaryConstructorParameters(classDeclaration)) {
|
for (JetParameter parameter : getPrimaryConstructorParameters(classDeclaration)) {
|
||||||
PropertyDescriptor descriptor =
|
PropertyDescriptor descriptor =
|
||||||
getPropertyDescriptorForConstructorParameter(context().bindingContext(), parameter);
|
getPropertyDescriptorForConstructorParameter(bindingContext(), parameter);
|
||||||
if (descriptor != null) {
|
if (descriptor != null) {
|
||||||
result.addAll(PropertyTranslator.translateAccessors(descriptor, context()));
|
result.addAll(PropertyTranslator.translateAccessors(descriptor, context()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public final class NamespaceTranslator extends AbstractTranslator {
|
|||||||
//TODO: at the moment this check is very ineffective, possible solution is to cash the result of getDFN
|
//TODO: at the moment this check is very ineffective, possible solution is to cash the result of getDFN
|
||||||
// other solution is to determine it's not affecting performance :D
|
// other solution is to determine it's not affecting performance :D
|
||||||
public boolean isNamespaceEmpty() {
|
public boolean isNamespaceEmpty() {
|
||||||
return BindingUtils.getDeclarationsForNamespace(context().bindingContext(), descriptor).isEmpty();
|
return BindingUtils.getDeclarationsForNamespace(bindingContext(), descriptor).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ public final class PropertyTranslator extends AbstractTranslator {
|
|||||||
private PropertyTranslator(@NotNull PropertyDescriptor property, @NotNull TranslationContext context) {
|
private PropertyTranslator(@NotNull PropertyDescriptor property, @NotNull TranslationContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
this.property = property;
|
this.property = property;
|
||||||
this.declaration = BindingUtils.getPropertyForDescriptor(context().bindingContext(), property);
|
this.declaration = BindingUtils.getPropertyForDescriptor(bindingContext(), property);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ public final class FunctionTranslator extends AbstractTranslator {
|
|||||||
JsName receiver = functionBodyContext.jsScope().declareName(Namer.getReceiverParameterName());
|
JsName receiver = functionBodyContext.jsScope().declareName(Namer.getReceiverParameterName());
|
||||||
DeclarationDescriptor expectedReceiverDescriptor = getExpectedReceiverDescriptor(descriptor);
|
DeclarationDescriptor expectedReceiverDescriptor = getExpectedReceiverDescriptor(descriptor);
|
||||||
assert expectedReceiverDescriptor != null;
|
assert expectedReceiverDescriptor != null;
|
||||||
context().aliaser().setAliasForThis(expectedReceiverDescriptor, receiver);
|
aliaser().setAliasForThis(expectedReceiverDescriptor, receiver);
|
||||||
jsParameters.add(new JsParameter(receiver));
|
jsParameters.add(new JsParameter(receiver));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ public final class PatternTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNullable(JetTypePattern pattern) {
|
private boolean isNullable(JetTypePattern pattern) {
|
||||||
return BindingUtils.getTypeByReference(context().bindingContext(),
|
return BindingUtils.getTypeByReference(bindingContext(),
|
||||||
getTypeReference(pattern)).isNullable();
|
getTypeReference(pattern)).isNullable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ public final class PatternTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private JsNameRef getClassNameReferenceForTypeReference(@NotNull JetTypeReference typeReference) {
|
private JsNameRef getClassNameReferenceForTypeReference(@NotNull JetTypeReference typeReference) {
|
||||||
ClassDescriptor referencedClass = BindingUtils.getClassDescriptorForTypeReference
|
ClassDescriptor referencedClass = BindingUtils.getClassDescriptorForTypeReference
|
||||||
(context().bindingContext(), typeReference);
|
(bindingContext(), typeReference);
|
||||||
return TranslationUtils.getQualifiedReference(context(), referencedClass);
|
return TranslationUtils.getQualifiedReference(context(), referencedClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -82,13 +82,13 @@ public final class StringTemplateTranslator extends AbstractTranslator {
|
|||||||
//TODO: duplication
|
//TODO: duplication
|
||||||
@Override
|
@Override
|
||||||
public void visitLiteralStringTemplateEntry(@NotNull JetLiteralStringTemplateEntry entry) {
|
public void visitLiteralStringTemplateEntry(@NotNull JetLiteralStringTemplateEntry entry) {
|
||||||
JsStringLiteral stringConstant = context().program().getStringLiteral(entry.getText());
|
JsStringLiteral stringConstant = program().getStringLiteral(entry.getText());
|
||||||
append(stringConstant);
|
append(stringConstant);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitEscapeStringTemplateEntry(@NotNull JetEscapeStringTemplateEntry entry) {
|
public void visitEscapeStringTemplateEntry(@NotNull JetEscapeStringTemplateEntry entry) {
|
||||||
JsStringLiteral escapedValue = context().program().getStringLiteral(entry.getUnescapedValue());
|
JsStringLiteral escapedValue = program().getStringLiteral(entry.getUnescapedValue());
|
||||||
append(escapedValue);
|
append(escapedValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class WhenTranslator extends AbstractTranslator {
|
|||||||
super(context);
|
super(context);
|
||||||
this.whenExpression = expression;
|
this.whenExpression = expression;
|
||||||
this.expressionToMatch = translateExpressionToMatch(whenExpression);
|
this.expressionToMatch = translateExpressionToMatch(whenExpression);
|
||||||
this.dummyCounter = context.declareTemporary(context().program().getNumberLiteral(0));
|
this.dummyCounter = context.declareTemporary(program().getNumberLiteral(0));
|
||||||
this.result = context.declareTemporary(program().getNullLiteral());
|
this.result = context.declareTemporary(program().getNullLiteral());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +80,7 @@ public class WhenTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private JsStatement surroundWithDummyIf(@NotNull JsStatement entryStatement) {
|
private JsStatement surroundWithDummyIf(@NotNull JsStatement entryStatement) {
|
||||||
JsExpression stepNumberEqualsCurrentEntryNumber = new JsBinaryOperation(JsBinaryOperator.EQ,
|
JsExpression stepNumberEqualsCurrentEntryNumber = new JsBinaryOperation(JsBinaryOperator.EQ,
|
||||||
dummyCounter.reference(), context().program().getNumberLiteral(currentEntryNumber));
|
dummyCounter.reference(), program().getNumberLiteral(currentEntryNumber));
|
||||||
currentEntryNumber++;
|
currentEntryNumber++;
|
||||||
return new JsIf(stepNumberEqualsCurrentEntryNumber, entryStatement, null);
|
return new JsIf(stepNumberEqualsCurrentEntryNumber, entryStatement, null);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -71,13 +71,13 @@ public final class IteratorForTranslator extends ForTranslator {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression nextMethodInvocation() {
|
private JsExpression nextMethodInvocation() {
|
||||||
FunctionDescriptor nextFunction = getNextFunction(context().bindingContext(), getLoopRange(expression));
|
FunctionDescriptor nextFunction = getNextFunction(bindingContext(), getLoopRange(expression));
|
||||||
return translateMethodInvocation(iterator.reference(), nextFunction);
|
return translateMethodInvocation(iterator.reference(), nextFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression hasNextMethodInvocation() {
|
private JsExpression hasNextMethodInvocation() {
|
||||||
CallableDescriptor hasNextFunction = getHasNextCallable(context().bindingContext(), getLoopRange(expression));
|
CallableDescriptor hasNextFunction = getHasNextCallable(bindingContext(), getLoopRange(expression));
|
||||||
return translateMethodInvocation(iterator.reference(), hasNextFunction);
|
return translateMethodInvocation(iterator.reference(), hasNextFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ public final class IteratorForTranslator extends ForTranslator {
|
|||||||
private JsExpression iteratorMethodInvocation() {
|
private JsExpression iteratorMethodInvocation() {
|
||||||
JetExpression rangeExpression = getLoopRange(expression);
|
JetExpression rangeExpression = getLoopRange(expression);
|
||||||
JsExpression range = Translation.translateAsExpression(rangeExpression, context());
|
JsExpression range = Translation.translateAsExpression(rangeExpression, context());
|
||||||
FunctionDescriptor iteratorFunction = getIteratorFunction(context().bindingContext(), rangeExpression);
|
FunctionDescriptor iteratorFunction = getIteratorFunction(bindingContext(), rangeExpression);
|
||||||
return translateMethodInvocation(range, iteratorFunction);
|
return translateMethodInvocation(range, iteratorFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,13 +18,13 @@ package org.jetbrains.k2js.translate.general;
|
|||||||
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsProgram;
|
import com.google.dart.compiler.backend.js.ast.JsProgram;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
import org.jetbrains.k2js.translate.context.Aliaser;
|
import org.jetbrains.k2js.translate.context.Aliaser;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
//TODO: provide helper methods for different parts of context
|
|
||||||
public abstract class AbstractTranslator {
|
public abstract class AbstractTranslator {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -48,4 +48,9 @@ public abstract class AbstractTranslator {
|
|||||||
protected Aliaser aliaser() {
|
protected Aliaser aliaser() {
|
||||||
return context.aliaser();
|
return context.aliaser();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
protected BindingContext bindingContext() {
|
||||||
|
return context.bindingContext();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -73,7 +73,7 @@ public final class ClassInitializerTranslator extends AbstractInitializerTransla
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void mayBeAddCallToSuperMethod() {
|
private void mayBeAddCallToSuperMethod() {
|
||||||
if (hasAncestorClass(context().bindingContext(), classDeclaration)) {
|
if (hasAncestorClass(bindingContext(), classDeclaration)) {
|
||||||
JetDelegatorToSuperCall superCall = getSuperCall();
|
JetDelegatorToSuperCall superCall = getSuperCall();
|
||||||
if (superCall == null) return;
|
if (superCall == null) return;
|
||||||
addCallToSuperMethod(superCall);
|
addCallToSuperMethod(superCall);
|
||||||
@@ -119,7 +119,7 @@ public final class ClassInitializerTranslator extends AbstractInitializerTransla
|
|||||||
@NotNull
|
@NotNull
|
||||||
private JsParameter translateParameter(@NotNull JetParameter jetParameter) {
|
private JsParameter translateParameter(@NotNull JetParameter jetParameter) {
|
||||||
DeclarationDescriptor parameterDescriptor =
|
DeclarationDescriptor parameterDescriptor =
|
||||||
getDescriptorForElement(context().bindingContext(), jetParameter);
|
getDescriptorForElement(bindingContext(), jetParameter);
|
||||||
JsName parameterName = context().getNameForDescriptor(parameterDescriptor);
|
JsName parameterName = context().getNameForDescriptor(parameterDescriptor);
|
||||||
JsParameter jsParameter = new JsParameter(parameterName);
|
JsParameter jsParameter = new JsParameter(parameterName);
|
||||||
mayBeAddInitializerStatementForProperty(jsParameter, jetParameter);
|
mayBeAddInitializerStatementForProperty(jsParameter, jetParameter);
|
||||||
@@ -129,7 +129,7 @@ public final class ClassInitializerTranslator extends AbstractInitializerTransla
|
|||||||
private void mayBeAddInitializerStatementForProperty(@NotNull JsParameter jsParameter,
|
private void mayBeAddInitializerStatementForProperty(@NotNull JsParameter jsParameter,
|
||||||
@NotNull JetParameter jetParameter) {
|
@NotNull JetParameter jetParameter) {
|
||||||
PropertyDescriptor propertyDescriptor =
|
PropertyDescriptor propertyDescriptor =
|
||||||
getPropertyDescriptorForConstructorParameter(context().bindingContext(), jetParameter);
|
getPropertyDescriptorForConstructorParameter(bindingContext(), jetParameter);
|
||||||
if (propertyDescriptor != null) {
|
if (propertyDescriptor != null) {
|
||||||
JsStatement assignmentToBackingFieldExpression = assignmentToBackingField
|
JsStatement assignmentToBackingFieldExpression = assignmentToBackingField
|
||||||
(context(), propertyDescriptor, jsParameter.getName().makeRef());
|
(context(), propertyDescriptor, jsParameter.getName().makeRef());
|
||||||
|
|||||||
+2
-2
@@ -74,7 +74,7 @@ public final class BinaryOperationTranslator extends AbstractTranslator {
|
|||||||
super(context);
|
super(context);
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
this.operationDescriptor =
|
this.operationDescriptor =
|
||||||
getFunctionDescriptorForOperationExpression(context().bindingContext(), expression);
|
getFunctionDescriptorForOperationExpression(bindingContext(), expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -125,7 +125,7 @@ public final class BinaryOperationTranslator extends AbstractTranslator {
|
|||||||
private JsExpression translateAsOverloadedBinaryOperation() {
|
private JsExpression translateAsOverloadedBinaryOperation() {
|
||||||
CallBuilder callBuilder = setReceiverAndArguments();
|
CallBuilder callBuilder = setReceiverAndArguments();
|
||||||
ResolvedCall<?> resolvedCall1 =
|
ResolvedCall<?> resolvedCall1 =
|
||||||
getResolvedCall(context().bindingContext(), expression.getOperationReference());
|
getResolvedCall(bindingContext(), expression.getOperationReference());
|
||||||
JsExpression result = callBuilder.resolvedCall(resolvedCall1)
|
JsExpression result = callBuilder.resolvedCall(resolvedCall1)
|
||||||
.type(CallType.NORMAL).translate();
|
.type(CallType.NORMAL).translate();
|
||||||
return mayBeWrapWithNegation(result);
|
return mayBeWrapWithNegation(result);
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ public abstract class IncrementTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean returnValueIgnored() {
|
private boolean returnValueIgnored() {
|
||||||
return isStatement(context().bindingContext(), expression);
|
return isStatement(bindingContext(), expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
+1
-1
@@ -76,7 +76,7 @@ public final class IntrinsicIncrementTranslator extends IncrementTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public JsBinaryOperation unaryAsBinary(@NotNull JsExpression leftExpression) {
|
public JsBinaryOperation unaryAsBinary(@NotNull JsExpression leftExpression) {
|
||||||
JsNumberLiteral oneLiteral = context().program().getNumberLiteral(1);
|
JsNumberLiteral oneLiteral = program().getNumberLiteral(1);
|
||||||
JetToken token = getOperationToken(expression);
|
JetToken token = getOperationToken(expression);
|
||||||
if (token.equals(JetTokens.PLUSPLUS)) {
|
if (token.equals(JetTokens.PLUSPLUS)) {
|
||||||
return new JsBinaryOperation(JsBinaryOperator.ADD, leftExpression, oneLiteral);
|
return new JsBinaryOperation(JsBinaryOperator.ADD, leftExpression, oneLiteral);
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ public final class ArrayAccessTranslator extends AccessTranslator {
|
|||||||
return CallBuilder.build(context())
|
return CallBuilder.build(context())
|
||||||
.receiver(translateArrayExpression())
|
.receiver(translateArrayExpression())
|
||||||
.args(arguments)
|
.args(arguments)
|
||||||
.resolvedCall(BindingUtils.getResolvedCall(context().bindingContext(), expression))
|
.resolvedCall(BindingUtils.getResolvedCall(bindingContext(), expression))
|
||||||
.descriptor(methodDescriptor)
|
.descriptor(methodDescriptor)
|
||||||
.translate();
|
.translate();
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-14
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.k2js.translate.reference;
|
package org.jetbrains.k2js.translate.reference;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import com.google.dart.compiler.backend.js.ast.JsArrayLiteral;
|
import com.google.dart.compiler.backend.js.ast.JsArrayLiteral;
|
||||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -27,8 +28,10 @@ import org.jetbrains.jet.lang.resolve.calls.*;
|
|||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||||
import org.jetbrains.k2js.translate.general.Translation;
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
|
import org.jetbrains.k2js.translate.utils.AnnotationsUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getDefaultArgument;
|
import static org.jetbrains.k2js.translate.utils.BindingUtils.getDefaultArgument;
|
||||||
@@ -50,20 +53,24 @@ public final class CallExpressionTranslator extends AbstractTranslator {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final JetCallExpression expression;
|
private final JetCallExpression expression;
|
||||||
|
@NotNull
|
||||||
|
private final ResolvedCall<?> resolvedCall;
|
||||||
|
private final boolean isNativeFunctionCall;
|
||||||
|
|
||||||
private CallExpressionTranslator(@NotNull JetCallExpression expression,
|
private CallExpressionTranslator(@NotNull JetCallExpression expression,
|
||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
|
this.resolvedCall = getResolvedCallForCallExpression(bindingContext(), expression);
|
||||||
|
this.isNativeFunctionCall = AnnotationsUtils.isNativeObject(resolvedCall.getCandidateDescriptor());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translate(@Nullable JsExpression receiver,
|
private JsExpression translate(@Nullable JsExpression receiver,
|
||||||
@NotNull CallType callType) {
|
@NotNull CallType callType) {
|
||||||
ResolvedCall<?> resolvedCall = getResolvedCallForCallExpression(context().bindingContext(), expression);
|
|
||||||
return CallBuilder.build(context())
|
return CallBuilder.build(context())
|
||||||
.receiver(receiver)
|
.receiver(receiver)
|
||||||
.callee(getCalleeExpression(resolvedCall))
|
.callee(getCalleeExpression())
|
||||||
.args(translateArguments())
|
.args(translateArguments())
|
||||||
.resolvedCall(resolvedCall)
|
.resolvedCall(resolvedCall)
|
||||||
.type(callType)
|
.type(callType)
|
||||||
@@ -71,7 +78,7 @@ public final class CallExpressionTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private JsExpression getCalleeExpression(@NotNull ResolvedCall<?> resolvedCall) {
|
private JsExpression getCalleeExpression() {
|
||||||
if (resolvedCall.getCandidateDescriptor() instanceof ExpressionAsFunctionDescriptor) {
|
if (resolvedCall.getCandidateDescriptor() instanceof ExpressionAsFunctionDescriptor) {
|
||||||
return Translation.translateAsExpression(getCallee(expression), context());
|
return Translation.translateAsExpression(getCallee(expression), context());
|
||||||
}
|
}
|
||||||
@@ -81,36 +88,46 @@ public final class CallExpressionTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private List<JsExpression> translateArguments() {
|
private List<JsExpression> translateArguments() {
|
||||||
List<JsExpression> result = new ArrayList<JsExpression>();
|
List<JsExpression> result = new ArrayList<JsExpression>();
|
||||||
ResolvedCall<?> resolvedCall = getResolvedCallForCallExpression(context().bindingContext(), expression);
|
ResolvedCall<?> resolvedCall = getResolvedCallForCallExpression(bindingContext(), expression);
|
||||||
for (ValueParameterDescriptor parameterDescriptor : resolvedCall.getResultingDescriptor().getValueParameters()) {
|
for (ValueParameterDescriptor parameterDescriptor : resolvedCall.getResultingDescriptor().getValueParameters()) {
|
||||||
ResolvedValueArgument actualArgument = resolvedCall.getValueArgumentsByIndex().get(parameterDescriptor.getIndex());
|
ResolvedValueArgument actualArgument = resolvedCall.getValueArgumentsByIndex().get(parameterDescriptor.getIndex());
|
||||||
result.add(translateSingleArgument(actualArgument, parameterDescriptor));
|
result.addAll(translateSingleArgument(actualArgument, parameterDescriptor));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translateSingleArgument(@NotNull ResolvedValueArgument actualArgument,
|
private List<JsExpression> translateSingleArgument(@NotNull ResolvedValueArgument actualArgument,
|
||||||
@NotNull ValueParameterDescriptor parameterDescriptor) {
|
@NotNull ValueParameterDescriptor parameterDescriptor) {
|
||||||
List<JetExpression> argumentExpressions = actualArgument.getArgumentExpressions();
|
List<JetExpression> argumentExpressions = actualArgument.getArgumentExpressions();
|
||||||
if (actualArgument instanceof VarargValueArgument) {
|
if (actualArgument instanceof VarargValueArgument) {
|
||||||
return translateVarargArgument(argumentExpressions);
|
return translateVarargArgument(argumentExpressions);
|
||||||
}
|
}
|
||||||
if (actualArgument instanceof DefaultValueArgument) {
|
if (actualArgument instanceof DefaultValueArgument) {
|
||||||
JetExpression defaultArgument = getDefaultArgument(context().bindingContext(), parameterDescriptor);
|
JetExpression defaultArgument = getDefaultArgument(bindingContext(), parameterDescriptor);
|
||||||
return Translation.translateAsExpression(defaultArgument, context());
|
return Arrays.asList(Translation.translateAsExpression(defaultArgument, context()));
|
||||||
}
|
}
|
||||||
assert actualArgument instanceof ExpressionValueArgument;
|
assert actualArgument instanceof ExpressionValueArgument;
|
||||||
assert argumentExpressions.size() == 1;
|
assert argumentExpressions.size() == 1;
|
||||||
return Translation.translateAsExpression(argumentExpressions.get(0), context());
|
return Arrays.asList(Translation.translateAsExpression(argumentExpressions.get(0), context()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translateVarargArgument(@NotNull List<JetExpression> arguments) {
|
private List<JsExpression> translateVarargArgument(@NotNull List<JetExpression> arguments) {
|
||||||
JsArrayLiteral varargArgument = new JsArrayLiteral();
|
List<JsExpression> translatedArgs = Lists.newArrayList();
|
||||||
for (JetExpression argument : arguments) {
|
for (JetExpression argument : arguments) {
|
||||||
varargArgument.getExpressions().add(Translation.translateAsExpression(argument, context()));
|
translatedArgs.add(Translation.translateAsExpression(argument, context()));
|
||||||
}
|
}
|
||||||
return varargArgument;
|
if (isNativeFunctionCall) {
|
||||||
|
return translatedArgs;
|
||||||
|
}
|
||||||
|
return wrapInArrayLiteral(translatedArgs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private List<JsExpression> wrapInArrayLiteral(@NotNull List<JsExpression> translatedArgs) {
|
||||||
|
JsArrayLiteral argsWrappedInArray = new JsArrayLiteral();
|
||||||
|
argsWrappedInArray.getExpressions().addAll(translatedArgs);
|
||||||
|
return Arrays.<JsExpression>asList(argsWrappedInArray);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user