Rollback commented out code.

Implement a hacky solution for the new "invoke" concept.
Fix most of the tests. (apart from 3)
This commit is contained in:
pTalanov
2012-05-04 15:37:21 +04:00
parent 646cdefc51
commit 5ea8e0f697
15 changed files with 122 additions and 128 deletions
@@ -50,6 +50,6 @@ public abstract class TestWithEnvironment extends UsefulTestCase {
} }
protected void createEnvironmentWithMockJdkAndIdeaAnnotations() { protected void createEnvironmentWithMockJdkAndIdeaAnnotations() {
myEnvironment = JetTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable()); myEnvironment = JetCoreEnvironment.getCoreEnvironmentForJS(getTestRootDisposable());
} }
} }
@@ -28,6 +28,6 @@ public final class ClosureTest extends SingleFileTranslationTest {
} }
public void testIteratingCallbacks() throws Exception { public void testIteratingCallbacks() throws Exception {
//checkFooBoxIsTrue("iteratingCallbacks.kt"); checkFooBoxIsTrue("iteratingCallbacks.kt");
} }
} }
@@ -16,16 +16,12 @@
package org.jetbrains.k2js.test.semantics; package org.jetbrains.k2js.test.semantics;
import closurecompiler.internal.com.google.common.collect.Sets;
import junit.framework.Test; import junit.framework.Test;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.test.BasicTest; import org.jetbrains.k2js.test.BasicTest;
import org.jetbrains.k2js.test.SingleFileTranslationTest; import org.jetbrains.k2js.test.SingleFileTranslationTest;
import org.jetbrains.k2js.translate.context.Namer; import org.jetbrains.k2js.translate.context.Namer;
import java.io.File;
import java.io.FilenameFilter;
@SuppressWarnings("JUnitTestCaseWithNoTests") @SuppressWarnings("JUnitTestCaseWithNoTests")
public final class ExamplesTest extends SingleFileTranslationTest { public final class ExamplesTest extends SingleFileTranslationTest {
@@ -44,13 +40,7 @@ public final class ExamplesTest extends SingleFileTranslationTest {
} }
public static Test suite() throws Exception { public static Test suite() throws Exception {
return TranslatorTestCaseBuilder.suiteForDirectory(BasicTest.pathToTestFilesRoot() + "examples/cases/", true, new FilenameFilter() { return TranslatorTestCaseBuilder.suiteForDirectory(BasicTest.pathToTestFilesRoot() + "examples/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() {
@Override
public boolean accept(File dir, String name) {
return !Sets.newHashSet("closureWithParameter.jet", "closureWithParameterAndBoxing.jet", "doubleEnclosedLocalVariable.jet", "enclosingLocalVariable.jet", "extensionClosure.jet",
"simplestClosure.jet", "simplestClosureAndBoxing.jet").contains(name);
}
}, new TranslatorTestCaseBuilder.NamedTestFactory() {
@NotNull @NotNull
@Override @Override
public Test createTest(@NotNull String filename) { public Test createTest(@NotNull String filename) {
@@ -52,15 +52,15 @@ public final class ExtensionFunctionTest extends SingleFileTranslationTest {
} }
public void testExtensionLiteralPassedToFunction() throws Exception { public void testExtensionLiteralPassedToFunction() throws Exception {
//checkFooBoxIsTrue("extensionLiteralPassedToFunction.kt"); checkFooBoxIsTrue("extensionLiteralPassedToFunction.kt");
} }
public void testExtensionInsideFunctionLiteral() throws Exception { public void testExtensionInsideFunctionLiteral() throws Exception {
//checkFooBoxIsTrue("extensionInsideFunctionLiteral.kt"); checkFooBoxIsTrue("extensionInsideFunctionLiteral.kt");
} }
public void testGenericExtension() throws Exception { public void testGenericExtension() throws Exception {
//checkFooBoxIsOk("generic.kt"); checkFooBoxIsOk("generic.kt");
} }
public void testExtensionFunctionCalledFromExtensionFunction() throws Exception { public void testExtensionFunctionCalledFromExtensionFunction() throws Exception {
@@ -36,36 +36,36 @@ public class FunctionTest extends AbstractExpressionTest {
} }
public void testFunctionLiteral() throws Exception { public void testFunctionLiteral() throws Exception {
//checkFooBoxIsTrue("functionLiteral.kt"); checkFooBoxIsTrue("functionLiteral.kt");
} }
public void testAdderClosure() throws Exception { public void testAdderClosure() throws Exception {
//checkFooBoxIsTrue("adderClosure.kt"); checkFooBoxIsTrue("adderClosure.kt");
} }
public void testLoopClosure() throws Exception { public void testLoopClosure() throws Exception {
//checkFooBoxIsTrue("loopClosure.kt"); checkFooBoxIsTrue("loopClosure.kt");
} }
public void testFunctionLiteralAsParameter() throws Exception { public void testFunctionLiteralAsParameter() throws Exception {
//checkFooBoxIsTrue("functionLiteralAsParameter.kt"); checkFooBoxIsTrue("functionLiteralAsParameter.kt");
} }
public void testClosureWithParameter() throws Exception { public void testClosureWithParameter() throws Exception {
//checkFooBoxIsOk("closureWithParameter.kt"); checkFooBoxIsOk("closureWithParameter.kt");
} }
public void testClosureWithParameterAndBoxing() throws Exception { public void testClosureWithParameterAndBoxing() throws Exception {
//checkFooBoxIsOk("closureWithParameterAndBoxing.jet"); checkFooBoxIsOk("closureWithParameterAndBoxing.jet");
} }
public void testEnclosingThis() throws Exception { public void testEnclosingThis() throws Exception {
//runFunctionOutputTest("enclosingThis.kt", Namer.getRootNamespaceName(), "box", "OK"); runFunctionOutputTest("enclosingThis.kt", Namer.getRootNamespaceName(), "box", "OK");
} }
public void testImplicitItParameter() throws Exception { public void testImplicitItParameter() throws Exception {
//checkFooBoxIsTrue("implicitItParameter.kt"); checkFooBoxIsTrue("implicitItParameter.kt");
} }
@@ -75,7 +75,7 @@ public class FunctionTest extends AbstractExpressionTest {
public void testFunctionLiteralAsLastParameter() throws Exception { public void testFunctionLiteralAsLastParameter() throws Exception {
//checkFooBoxIsTrue("functionLiteralAsLastParameter.kt"); checkFooBoxIsTrue("functionLiteralAsLastParameter.kt");
} }
@@ -92,23 +92,23 @@ public final class MiscTest extends AbstractExpressionTest {
} }
public void testFunInConstructor() throws Exception { public void testFunInConstructor() throws Exception {
//checkFooBoxIsTrue("funInConstructor.kt"); checkFooBoxIsTrue("funInConstructor.kt");
} }
public void testFunInConstructorBlock() throws Exception { public void testFunInConstructorBlock() throws Exception {
//checkFooBoxIsTrue("funInConstructorBlock.kt"); checkFooBoxIsTrue("funInConstructorBlock.kt");
} }
public void testPropertyAsFunCalledOnConstructor() throws Exception { public void testPropertyAsFunCalledOnConstructor() throws Exception {
//checkFooBoxIsTrue("propertyAsFunCalledOnConstructor.kt"); checkFooBoxIsTrue("propertyAsFunCalledOnConstructor.kt");
} }
public void testNamespacePropertyCalledAsFun() throws Exception { public void testNamespacePropertyCalledAsFun() throws Exception {
//checkFooBoxIsTrue("namespacePropertyCalledAsFun.kt"); checkFooBoxIsTrue("namespacePropertyCalledAsFun.kt");
} }
public void testExtensionLiteralCreatedAtNamespaceLevel() throws Exception { public void testExtensionLiteralCreatedAtNamespaceLevel() throws Exception {
//checkFooBoxIsTrue("extensionLiteralCreatedAtNamespaceLevel.kt"); checkFooBoxIsTrue("extensionLiteralCreatedAtNamespaceLevel.kt");
} }
public void testTemporaryVariableCreatedInNamespaceInitializer() throws Exception { public void testTemporaryVariableCreatedInNamespaceInitializer() throws Exception {
@@ -70,7 +70,7 @@ public final class PropertyAccessTest extends SingleFileTranslationTest {
} }
public void testExtensionLiteralSafeCall() throws Exception { public void testExtensionLiteralSafeCall() throws Exception {
//checkFooBoxIsTrue("extensionLiteralSafeCall.kt"); checkFooBoxIsTrue("extensionLiteralSafeCall.kt");
} }
} }
@@ -33,11 +33,11 @@ public final class WebDemoExamples2Test extends SingleFileTranslationTest {
} }
public void testLife() throws Exception { public void testLife() throws Exception {
//performTestWithMain("life", "", "2"); performTestWithMain("life", "", "2");
} }
public void testBuilder() throws Exception { public void testBuilder() throws Exception {
//performTestWithMain("builder", ""); performTestWithMain("builder", "");
//performTestWithMain("builder", "1", "over9000"); performTestWithMain("builder", "1", "over9000");
} }
} }
@@ -412,15 +412,6 @@ public final class StaticContext {
return true; return true;
} }
}; };
Rule<Boolean> variableAsFunctionsHaveNoQualifiers = new Rule<Boolean>() {
@Override
public Boolean apply(@NotNull DeclarationDescriptor descriptor) {
if (!isVariableAsFunction(descriptor)) {
return null;
}
return true;
}
};
//TODO: hack! //TODO: hack!
Rule<Boolean> nativeObjectsHaveNoQualifiers = new Rule<Boolean>() { Rule<Boolean> nativeObjectsHaveNoQualifiers = new Rule<Boolean>() {
@Override @Override
@@ -445,7 +436,6 @@ public final class StaticContext {
}; };
addRule(topLevelNamespaceHaveNoQualifier); addRule(topLevelNamespaceHaveNoQualifier);
addRule(propertiesHaveNoQualifiers); addRule(propertiesHaveNoQualifiers);
addRule(variableAsFunctionsHaveNoQualifiers);
addRule(nativeObjectsHaveNoQualifiers); addRule(nativeObjectsHaveNoQualifiers);
} }
} }
@@ -22,14 +22,14 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.psi.JetCallExpression; import org.jetbrains.jet.lang.psi.JetCallExpression;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.jet.lang.resolve.calls.ExpressionAsFunctionDescriptor; import org.jetbrains.jet.lang.resolve.calls.ExpressionAsFunctionDescriptor;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.calls.ResolvedValueArgument; import org.jetbrains.jet.lang.resolve.calls.ResolvedValueArgument;
import org.jetbrains.jet.lang.resolve.calls.VariableAsFunctionResolvedCall; import org.jetbrains.jet.lang.resolve.calls.VariableAsFunctionResolvedCall;
import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.general.Translation; import org.jetbrains.k2js.translate.general.Translation;
import org.jetbrains.k2js.translate.utils.AnnotationsUtils; import org.jetbrains.k2js.translate.utils.AnnotationsUtils;
import org.jetbrains.k2js.translate.utils.PsiUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -43,9 +43,9 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
@NotNull @NotNull
public static JsExpression translate(@NotNull JetCallExpression expression, public static JsExpression translate(@NotNull JetCallExpression expression,
@Nullable JsExpression receiver, @Nullable JsExpression receiver,
@NotNull CallType callType, @NotNull CallType callType,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
if (InlinedCallExpressionTranslator.shouldBeInlined(expression, context)) { if (InlinedCallExpressionTranslator.shouldBeInlined(expression, context)) {
return InlinedCallExpressionTranslator.translate(expression, receiver, callType, context); return InlinedCallExpressionTranslator.translate(expression, receiver, callType, context);
} }
@@ -55,8 +55,8 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
private final boolean isNativeFunctionCall; private final boolean isNativeFunctionCall;
private CallExpressionTranslator(@NotNull JetCallExpression expression, private CallExpressionTranslator(@NotNull JetCallExpression expression,
@Nullable JsExpression receiver, @Nullable JsExpression receiver,
@NotNull CallType callType, @NotNull TranslationContext context) { @NotNull CallType callType, @NotNull TranslationContext context) {
super(expression, receiver, callType, context); super(expression, receiver, callType, context);
this.isNativeFunctionCall = AnnotationsUtils.isNativeObject(resolvedCall.getCandidateDescriptor()); this.isNativeFunctionCall = AnnotationsUtils.isNativeObject(resolvedCall.getCandidateDescriptor());
} }
@@ -64,12 +64,28 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
@NotNull @NotNull
private JsExpression translate() { private JsExpression translate() {
return CallBuilder.build(context()) return CallBuilder.build(context())
.receiver(receiver) .receiver(getReceiver())
.callee(getCalleeExpression()) .callee(getCalleeExpression())
.args(translateArguments()) .args(translateArguments())
.resolvedCall(resolvedCall) .resolvedCall(getResolvedCall())
.type(callType) .type(callType)
.translate(); .translate();
}
@NotNull
private ResolvedCall<?> getResolvedCall() {
if (resolvedCall instanceof VariableAsFunctionResolvedCall) {
return ((VariableAsFunctionResolvedCall) resolvedCall).getFunctionCall();
}
return resolvedCall;
}
@Nullable
private JsExpression getReceiver() {
if (receiver != null) {
return receiver;
}
return null;
} }
@Nullable @Nullable
@@ -78,19 +94,12 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
if (candidateDescriptor instanceof ExpressionAsFunctionDescriptor) { if (candidateDescriptor instanceof ExpressionAsFunctionDescriptor) {
return translateExpressionAsFunction(); return translateExpressionAsFunction();
} }
//if (candidateDescriptor instanceof VariableAsFunctionDescriptor) { if (resolvedCall instanceof VariableAsFunctionResolvedCall) {
// return translateVariableAsFunction(); return Translation.translateAsExpression(PsiUtils.getCallee(expression), context());
//} }
return null; return null;
} }
@NotNull
private JsExpression translateVariableAsFunction() {
JetExpression callee = getCallee(expression);
assert callee instanceof JetSimpleNameExpression;
return ReferenceTranslator.getAccessTranslator((JetSimpleNameExpression)callee, receiver, context()).translateAsGet();
}
@NotNull @NotNull
private JsExpression translateExpressionAsFunction() { private JsExpression translateExpressionAsFunction() {
return Translation.translateAsExpression(getCallee(expression), context()); return Translation.translateAsExpression(getCallee(expression), context());
@@ -21,6 +21,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall; import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCallWithTrace;
import org.jetbrains.jet.lang.resolve.calls.VariableAsFunctionResolvedCall;
import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.utils.TranslationUtils; import org.jetbrains.k2js.translate.utils.TranslationUtils;
@@ -78,22 +80,11 @@ public final class CallParametersResolver {
if (callee != null) { if (callee != null) {
return callee; return callee;
} }
if (!isVariableAsFunction(descriptor)) { if (!(resolvedCall instanceof VariableAsFunctionResolvedCall)) {
return ReferenceTranslator.translateAsLocalNameReference(descriptor, context); return ReferenceTranslator.translateAsLocalNameReference(descriptor, context);
} }
//VariableDescriptor variableDescriptor = ResolvedCallWithTrace<FunctionDescriptor> call = ((VariableAsFunctionResolvedCall) resolvedCall).getFunctionCall();
// getVariableDescriptorForVariableAsFunction((VariableAsFunctionDescriptor)descriptor); return CallBuilder.build(context).resolvedCall(call).translate();
//if (variableDescriptor instanceof PropertyDescriptor) {
// return getterCall((PropertyDescriptor)variableDescriptor);
//}
return ReferenceTranslator.translateAsLocalNameReference(descriptor, context);
}
//TODO: inspect
@NotNull
private JsExpression getterCall(@NotNull PropertyDescriptor variableDescriptor) {
//TODO: call type?
return PropertyAccessTranslator.translateAsPropertyGetterCall(variableDescriptor, resolvedCall, context);
} }
@Nullable @Nullable
@@ -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.JsExpression; 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.JsInvocation;
import com.google.dart.compiler.backend.js.ast.JsNameRef; import com.google.dart.compiler.backend.js.ast.JsNameRef;
@@ -28,6 +29,7 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.resolve.calls.ExpressionAsFunctionDescriptor; import org.jetbrains.jet.lang.resolve.calls.ExpressionAsFunctionDescriptor;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall; import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.calls.VariableAsFunctionResolvedCall;
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.intrinsic.Intrinsic; import org.jetbrains.k2js.translate.intrinsic.Intrinsic;
@@ -37,8 +39,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.jetbrains.k2js.translate.reference.CallParametersResolver.resolveCallParameters; import static org.jetbrains.k2js.translate.reference.CallParametersResolver.resolveCallParameters;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.isConstructorDescriptor;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.*; import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.isConstructorDescriptor;
/** /**
* @author Pavel Talanov * @author Pavel Talanov
@@ -57,11 +59,11 @@ public final class CallTranslator extends AbstractTranslator {
private final CallParameters callParameters; private final CallParameters callParameters;
/*package*/ CallTranslator(@Nullable JsExpression receiver, @Nullable JsExpression callee, /*package*/ CallTranslator(@Nullable JsExpression receiver, @Nullable JsExpression callee,
@NotNull List<JsExpression> arguments, @NotNull List<JsExpression> arguments,
@NotNull ResolvedCall<? extends CallableDescriptor> resolvedCall, @NotNull ResolvedCall<? extends CallableDescriptor> resolvedCall,
@NotNull CallableDescriptor descriptorToCall, @NotNull CallableDescriptor descriptorToCall,
@NotNull CallType callType, @NotNull CallType callType,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
super(context); super(context);
this.arguments = arguments; this.arguments = arguments;
this.resolvedCall = resolvedCall; this.resolvedCall = resolvedCall;
@@ -90,12 +92,28 @@ public final class CallTranslator extends AbstractTranslator {
if (isExpressionAsFunction()) { if (isExpressionAsFunction()) {
return expressionAsFunctionCall(); return expressionAsFunctionCall();
} }
if (isInvoke()) {
return invokeCall();
}
return methodCall(getThisObjectOrQualifier()); return methodCall(getThisObjectOrQualifier());
} }
//TODO:
private boolean isInvoke() {
return descriptor.getName().equals("invoke");
}
private JsExpression invokeCall() {
JsInvocation callMethodInvocation = generateCallMethodInvocation();
List<JsExpression> parameters = Lists.<JsExpression>newArrayList(context().program().getNullLiteral());
parameters.addAll(arguments);
setArguments(callMethodInvocation, parameters);
return callMethodInvocation;
}
private boolean isExpressionAsFunction() { private boolean isExpressionAsFunction() {
return descriptor instanceof ExpressionAsFunctionDescriptor;// || return descriptor instanceof ExpressionAsFunctionDescriptor ||
//descriptor instanceof VariableAsFunctionDescriptor; resolvedCall instanceof VariableAsFunctionResolvedCall;
} }
@NotNull @NotNull
@@ -110,7 +128,7 @@ public final class CallTranslator extends AbstractTranslator {
@NotNull @NotNull
private JsExpression intrinsicInvocation() { private JsExpression intrinsicInvocation() {
assert descriptor instanceof FunctionDescriptor; assert descriptor instanceof FunctionDescriptor;
Intrinsic intrinsic = context().intrinsics().getFunctionIntrinsic((FunctionDescriptor)descriptor); Intrinsic intrinsic = context().intrinsics().getFunctionIntrinsic((FunctionDescriptor) descriptor);
return intrinsic.apply(callParameters.getThisObject(), arguments, context()); return intrinsic.apply(callParameters.getThisObject(), arguments, context());
} }
@@ -141,8 +159,8 @@ public final class CallTranslator extends AbstractTranslator {
} }
private boolean isExtensionFunctionLiteral() { private boolean isExtensionFunctionLiteral() {
boolean isLiteral = //descriptor instanceof VariableAsFunctionDescriptor || boolean isLiteral = isInvoke()
descriptor instanceof ExpressionAsFunctionDescriptor; || descriptor instanceof ExpressionAsFunctionDescriptor;
return isExtensionFunction() && isLiteral; return isExtensionFunction() && isLiteral;
} }
@@ -30,7 +30,7 @@ import org.jetbrains.k2js.translate.general.AbstractTranslator;
import static org.jetbrains.k2js.translate.utils.AnnotationsUtils.isNativeObject; import static org.jetbrains.k2js.translate.utils.AnnotationsUtils.isNativeObject;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getDescriptorForReferenceExpression; import static org.jetbrains.k2js.translate.utils.BindingUtils.getDescriptorForReferenceExpression;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getResolvedCall; import static org.jetbrains.k2js.translate.utils.BindingUtils.getResolvedCallForProperty;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getSelectorAsSimpleName; import static org.jetbrains.k2js.translate.utils.PsiUtils.getSelectorAsSimpleName;
import static org.jetbrains.k2js.translate.utils.PsiUtils.isBackingFieldReference; import static org.jetbrains.k2js.translate.utils.PsiUtils.isBackingFieldReference;
@@ -42,8 +42,8 @@ public abstract class PropertyAccessTranslator extends AbstractTranslator implem
//TODO: is it really called? //TODO: is it really called?
@NotNull @NotNull
private static PropertyAccessTranslator newInstance(@NotNull PropertyDescriptor descriptor, private static PropertyAccessTranslator newInstance(@NotNull PropertyDescriptor descriptor,
@NotNull ResolvedCall<?> resolvedCall, @NotNull ResolvedCall<?> resolvedCall,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
if (isNativeObject(descriptor)) { if (isNativeObject(descriptor)) {
return new NativePropertyAccessTranslator(descriptor, /*qualifier = */ null, context); return new NativePropertyAccessTranslator(descriptor, /*qualifier = */ null, context);
} }
@@ -54,16 +54,16 @@ public abstract class PropertyAccessTranslator extends AbstractTranslator implem
@NotNull @NotNull
public static PropertyAccessTranslator newInstance(@NotNull JetSimpleNameExpression expression, public static PropertyAccessTranslator newInstance(@NotNull JetSimpleNameExpression expression,
@Nullable JsExpression qualifier, @Nullable JsExpression qualifier,
@NotNull CallType callType, @NotNull CallType callType,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
PropertyAccessTranslator result; PropertyAccessTranslator result;
PropertyDescriptor propertyDescriptor = getPropertyDescriptor(expression, context); PropertyDescriptor propertyDescriptor = getPropertyDescriptor(expression, context);
if (isNativeObject(propertyDescriptor) || isBackingFieldReference(expression)) { if (isNativeObject(propertyDescriptor) || isBackingFieldReference(expression)) {
result = new NativePropertyAccessTranslator(propertyDescriptor, qualifier, context); result = new NativePropertyAccessTranslator(propertyDescriptor, qualifier, context);
} }
else { else {
ResolvedCall<?> resolvedCall = getResolvedCall(context.bindingContext(), expression); ResolvedCall<?> resolvedCall = getResolvedCallForProperty(context.bindingContext(), expression);
result = new KotlinPropertyAccessTranslator(propertyDescriptor, qualifier, resolvedCall, context); result = new KotlinPropertyAccessTranslator(propertyDescriptor, qualifier, resolvedCall, context);
} }
result.setCallType(callType); result.setCallType(callType);
@@ -72,7 +72,7 @@ public abstract class PropertyAccessTranslator extends AbstractTranslator implem
@NotNull @NotNull
private static PropertyDescriptor getPropertyDescriptor(@NotNull JetSimpleNameExpression expression, private static PropertyDescriptor getPropertyDescriptor(@NotNull JetSimpleNameExpression expression,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
DeclarationDescriptor descriptor = DeclarationDescriptor descriptor =
getDescriptorForReferenceExpression(context.bindingContext(), expression); getDescriptorForReferenceExpression(context.bindingContext(), expression);
assert descriptor instanceof PropertyDescriptor : "Must be a property descriptor."; assert descriptor instanceof PropertyDescriptor : "Must be a property descriptor.";
@@ -83,37 +83,37 @@ public abstract class PropertyAccessTranslator extends AbstractTranslator implem
@NotNull @NotNull
/*package*/ /*package*/
static JsExpression translateAsPropertyGetterCall(@NotNull PropertyDescriptor descriptor, static JsExpression translateAsPropertyGetterCall(@NotNull PropertyDescriptor descriptor,
@NotNull ResolvedCall<?> resolvedCall, @NotNull ResolvedCall<?> resolvedCall,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
return (newInstance(descriptor, resolvedCall, context)) return (newInstance(descriptor, resolvedCall, context))
.translateAsGet(); .translateAsGet();
} }
@NotNull @NotNull
public static JsExpression translateAsPropertyGetterCall(@NotNull JetSimpleNameExpression expression, public static JsExpression translateAsPropertyGetterCall(@NotNull JetSimpleNameExpression expression,
@Nullable JsExpression qualifier, @Nullable JsExpression qualifier,
@NotNull CallType callType, @NotNull CallType callType,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
return (newInstance(expression, qualifier, callType, context)) return (newInstance(expression, qualifier, callType, context))
.translateAsGet(); .translateAsGet();
} }
private static boolean canBePropertyGetterCall(@NotNull JetQualifiedExpression expression, private static boolean canBePropertyGetterCall(@NotNull JetQualifiedExpression expression,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
JetSimpleNameExpression selector = getSelectorAsSimpleName(expression); JetSimpleNameExpression selector = getSelectorAsSimpleName(expression);
assert selector != null : "Only names are allowed after the dot"; assert selector != null : "Only names are allowed after the dot";
return canBePropertyGetterCall(selector, context); return canBePropertyGetterCall(selector, context);
} }
public static boolean canBePropertyGetterCall(@NotNull JetSimpleNameExpression expression, public static boolean canBePropertyGetterCall(@NotNull JetSimpleNameExpression expression,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
return (getDescriptorForReferenceExpression return (getDescriptorForReferenceExpression
(context.bindingContext(), expression) instanceof PropertyDescriptor); (context.bindingContext(), expression) instanceof PropertyDescriptor);
} }
public static boolean canBePropertyGetterCall(@NotNull JetExpression expression, public static boolean canBePropertyGetterCall(@NotNull JetExpression expression,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
if (expression instanceof JetQualifiedExpression) { if (expression instanceof JetQualifiedExpression) {
return canBePropertyGetterCall((JetQualifiedExpression) expression, context); return canBePropertyGetterCall((JetQualifiedExpression) expression, context);
} }
@@ -124,7 +124,7 @@ public abstract class PropertyAccessTranslator extends AbstractTranslator implem
} }
public static boolean canBePropertyAccess(@NotNull JetExpression expression, public static boolean canBePropertyAccess(@NotNull JetExpression expression,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
return canBePropertyGetterCall(expression, context); return canBePropertyGetterCall(expression, context);
} }
@@ -25,6 +25,7 @@ import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall; import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.calls.VariableAsFunctionResolvedCall;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant; import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses; import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
@@ -190,15 +191,11 @@ public final class BindingUtils {
return referencedDescriptor; return referencedDescriptor;
} }
//TODO: remove?
@Nullable @Nullable
public static DeclarationDescriptor getNullableDescriptorForReferenceExpression(@NotNull BindingContext context, public static DeclarationDescriptor getNullableDescriptorForReferenceExpression(@NotNull BindingContext context,
@NotNull JetReferenceExpression reference) { @NotNull JetReferenceExpression reference) {
DeclarationDescriptor referencedDescriptor = context.get(BindingContext.REFERENCE_TARGET, reference); return context.get(BindingContext.REFERENCE_TARGET, reference);
//if (isVariableAsFunction(referencedDescriptor)) {
// assert referencedDescriptor != null;
// return getVariableDescriptorForVariableAsFunction((VariableAsFunctionDescriptor)referencedDescriptor);
//}
return referencedDescriptor;
} }
public static boolean isNotAny(@NotNull DeclarationDescriptor superClassDescriptor) { public static boolean isNotAny(@NotNull DeclarationDescriptor superClassDescriptor) {
@@ -213,6 +210,17 @@ public final class BindingUtils {
return resolvedCall; return resolvedCall;
} }
@NotNull
public static ResolvedCall<?> getResolvedCallForProperty(@NotNull BindingContext context,
@NotNull JetExpression expression) {
ResolvedCall<? extends CallableDescriptor> resolvedCall = context.get(BindingContext.RESOLVED_CALL, expression);
assert resolvedCall != null : "Must resolve to a call.";
if (resolvedCall instanceof VariableAsFunctionResolvedCall) {
return ((VariableAsFunctionResolvedCall)resolvedCall).getVariableCall();
}
return resolvedCall;
}
@NotNull @NotNull
public static ResolvedCall<?> getResolvedCallForCallExpression(@NotNull BindingContext context, public static ResolvedCall<?> getResolvedCallForCallExpression(@NotNull BindingContext context,
@NotNull JetCallExpression expression) { @NotNull JetCallExpression expression) {
@@ -126,18 +126,6 @@ public final class JsDescriptorUtils {
return (ClassDescriptor)superClassDescriptor; return (ClassDescriptor)superClassDescriptor;
} }
//@NotNull
//public static VariableDescriptor getVariableDescriptorForVariableAsFunction
// (@NotNull VariableAsFunctionDescriptor descriptor) {
// VariableDescriptor functionVariable = descriptor.getVariableDescriptor();
// assert functionVariable != null;
// return functionVariable;
//}
public static boolean isVariableAsFunction(@Nullable DeclarationDescriptor referencedDescriptor) {
return false;//referencedDescriptor instanceof VariableAsFunctionDescriptor;
}
@NotNull @NotNull
public static DeclarationDescriptor getContainingDeclaration(@NotNull DeclarationDescriptor descriptor) { public static DeclarationDescriptor getContainingDeclaration(@NotNull DeclarationDescriptor descriptor) {