JS backend: drop CallType

This commit is contained in:
Erokhin Stanislav
2014-01-30 23:11:23 +04:00
parent e9b1ae7cb1
commit 7f8c17d6af
7 changed files with 39 additions and 115 deletions
@@ -26,7 +26,8 @@ import org.jetbrains.k2js.translate.context.Namer
import com.google.dart.compiler.backend.js.ast.JsNameRef
import org.jetbrains.k2js.translate.utils.JsAstUtils
import org.jetbrains.jet.lang.resolve.calls.tasks.ExplicitReceiverKind.*
import org.jetbrains.k2js.translate.reference.CallType
import com.google.dart.compiler.backend.js.ast.JsLiteral
import org.jetbrains.k2js.translate.utils.TranslationUtils
val CallInfo.callableDescriptor: CallableDescriptor
@@ -51,11 +52,9 @@ fun CallInfo.constructSafeCallIsNeeded(result: JsExpression): JsExpression {
BOTH_RECEIVERS, RECEIVER_ARGUMENT -> receiverObject
else -> thisObject
}
return CallType.SAFE.constructCall(nullableReceiverForSafeCall, object : CallType.CallConstructor {
override fun construct(receiver: JsExpression?): JsExpression {
return result
}
}, context)
val expression = TranslationUtils.notNullConditional(nullableReceiverForSafeCall!!, JsLiteral.NULL, context)
expression.setThenExpression(result)
return expression
}
val VariableAccessInfo.variableDescriptor: VariableDescriptor
@@ -27,10 +27,7 @@ import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.constants.IntegerValueTypeConstant;
import org.jetbrains.jet.lang.resolve.constants.NullValue;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.k2js.translate.context.TemporaryVariable;
import org.jetbrains.k2js.translate.context.TranslationContext;
@@ -181,7 +178,7 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
@NotNull
public JsNode visitCallExpression(@NotNull JetCallExpression expression,
@NotNull TranslationContext context) {
return CallExpressionTranslator.translate(expression, null, CallType.NORMAL, context).source(expression);
return CallExpressionTranslator.translate(expression, null, context).source(expression);
}
@Override
@@ -35,17 +35,16 @@ public abstract class AbstractCallExpressionTranslator extends AbstractTranslato
protected final ResolvedCall<? extends FunctionDescriptor> resolvedCall;
@Nullable
protected final JsExpression receiver;
@NotNull
protected final CallType callType;
protected AbstractCallExpressionTranslator(@NotNull JetCallExpression expression,
protected AbstractCallExpressionTranslator(
@NotNull JetCallExpression expression,
@Nullable JsExpression receiver,
@NotNull CallType type, @NotNull TranslationContext context) {
@NotNull TranslationContext context
) {
super(context);
this.expression = expression;
this.resolvedCall = getResolvedCallForCallExpression(bindingContext(), expression);
this.receiver = receiver;
this.callType = type;
}
}
@@ -26,20 +26,23 @@ import org.jetbrains.k2js.translate.context.TranslationContext;
public final class CallExpressionTranslator extends AbstractCallExpressionTranslator {
@NotNull
public static JsExpression translate(@NotNull JetCallExpression expression,
public static JsExpression translate(
@NotNull JetCallExpression expression,
@Nullable JsExpression receiver,
@NotNull CallType callType,
@NotNull TranslationContext context) {
@NotNull TranslationContext context
) {
if (InlinedCallExpressionTranslator.shouldBeInlined(expression, context)) {
return InlinedCallExpressionTranslator.translate(expression, receiver, callType, context);
return InlinedCallExpressionTranslator.translate(expression, receiver, context);
}
return (new CallExpressionTranslator(expression, receiver, callType, context)).translate();
return (new CallExpressionTranslator(expression, receiver, context)).translate();
}
private CallExpressionTranslator(@NotNull JetCallExpression expression,
private CallExpressionTranslator(
@NotNull JetCallExpression expression,
@Nullable JsExpression receiver,
@NotNull CallType callType, @NotNull TranslationContext context) {
super(expression, receiver, callType, context);
@NotNull TranslationContext context
) {
super(expression, receiver, context);
}
@NotNull
@@ -1,76 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.translate.reference;
import com.google.dart.compiler.backend.js.ast.JsConditional;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsLiteral;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression;
import org.jetbrains.jet.lang.psi.JetQualifiedExpression;
import org.jetbrains.jet.lang.psi.JetSafeQualifiedExpression;
import org.jetbrains.k2js.translate.context.TranslationContext;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.notNullConditional;
public enum CallType {
SAFE {
@NotNull
@Override
public JsExpression constructCall(
@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
@NotNull TranslationContext context
) {
assert receiver != null;
JsConditional expression = notNullConditional(receiver, JsLiteral.NULL, context);
expression.setThenExpression(constructor.construct(expression.getThenExpression()));
return expression;
}
},
//TODO: bang qualifier is not implemented in frontend for now
// BANG,
NORMAL {
@NotNull
@Override
public JsExpression constructCall(
@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
@NotNull TranslationContext context
) {
return constructor.construct(receiver);
}
};
@NotNull
public abstract JsExpression constructCall(@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
@NotNull TranslationContext context);
@NotNull
public static CallType getCallTypeForQualifiedExpression(@NotNull JetQualifiedExpression expression) {
if (expression instanceof JetSafeQualifiedExpression) {
return SAFE;
}
assert expression instanceof JetDotQualifiedExpression;
return NORMAL;
}
public interface CallConstructor {
@NotNull
JsExpression construct(@Nullable JsExpression receiver);
}
}
@@ -62,16 +62,19 @@ public final class InlinedCallExpressionTranslator extends AbstractCallExpressio
}
@NotNull
public static JsExpression translate(@NotNull JetCallExpression expression,
@Nullable JsExpression receiver,
@NotNull CallType callType,
@NotNull TranslationContext context) {
return (new InlinedCallExpressionTranslator(expression, receiver, callType, context)).translate();
public static JsExpression translate(
@NotNull JetCallExpression expression,
@Nullable JsExpression receiver,
@NotNull TranslationContext context
) {
return (new InlinedCallExpressionTranslator(expression, receiver, context)).translate();
}
private InlinedCallExpressionTranslator(@NotNull JetCallExpression expression, @Nullable JsExpression receiver,
@NotNull CallType callType, @NotNull TranslationContext context) {
super(expression, receiver, callType, context);
private InlinedCallExpressionTranslator(
@NotNull JetCallExpression expression, @Nullable JsExpression receiver,
@NotNull TranslationContext context
) {
super(expression, receiver, context);
}
@NotNull
@@ -48,15 +48,13 @@ public final class QualifiedExpressionTranslator {
@NotNull TranslationContext context) {
JsExpression receiver = translateReceiver(expression, context);
JetExpression selector = getSelector(expression);
CallType callType = CallType.getCallTypeForQualifiedExpression(expression);
return dispatchToCorrectTranslator(receiver, selector, callType, context);
return dispatchToCorrectTranslator(receiver, selector, context);
}
@NotNull
private static JsExpression dispatchToCorrectTranslator(
@Nullable JsExpression receiver,
@NotNull JetExpression selector,
@NotNull CallType callType,
@NotNull TranslationContext context
) {
if (ReferenceTranslator.canBePropertyAccess(selector, context)) {
@@ -64,7 +62,7 @@ public final class QualifiedExpressionTranslator {
return VariableAccessTranslator.newInstance(context, (JetSimpleNameExpression)selector, receiver).translateAsGet();
}
if (selector instanceof JetCallExpression) {
return invokeCallExpressionTranslator(receiver, selector, callType, context);
return invokeCallExpressionTranslator(receiver, selector, context);
}
//TODO: never get there
if (selector instanceof JetSimpleNameExpression) {
@@ -78,12 +76,13 @@ public final class QualifiedExpressionTranslator {
}
@NotNull
private static JsExpression invokeCallExpressionTranslator(@Nullable JsExpression receiver,
private static JsExpression invokeCallExpressionTranslator(
@Nullable JsExpression receiver,
@NotNull JetExpression selector,
@NotNull CallType callType,
@NotNull TranslationContext context) {
@NotNull TranslationContext context
) {
try {
return CallExpressionTranslator.translate((JetCallExpression) selector, receiver, callType, context);
return CallExpressionTranslator.translate((JetCallExpression) selector, receiver, context);
} catch (RuntimeException e) {
throw ErrorReportingUtils.reportErrorWithLocation(selector, e);
}