提交 d58e988c 编写于 作者: A Alex Tkachman

incorporating Sveta's patch on VariableAsFunctionResolvedCall

上级 6626086a
......@@ -79,7 +79,7 @@ public class CallableMethod implements Callable {
}
public void setNeedsReceiver(@Nullable CallableDescriptor receiverClass) {
this.receiverFunction = receiverClass;
this.receiverFunction = receiverClass.getOriginal();
}
public JetType getThisType() {
......
......@@ -964,13 +964,17 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
descriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, expression);
}
else {
if(resolvedCall instanceof VariableAsFunctionResolvedCall) {
VariableAsFunctionResolvedCall call = (VariableAsFunctionResolvedCall) resolvedCall;
resolvedCall = call.getVariableCall();
}
receiver = StackValue.receiver(resolvedCall, receiver, this, null, state);
descriptor = resolvedCall.getResultingDescriptor();
}
if (descriptor instanceof VariableAsFunctionDescriptor) {
descriptor = ((VariableAsFunctionDescriptor) descriptor).getVariableDescriptor();
}
//if (descriptor instanceof VariableAsFunctionDescriptor) {
// descriptor = ((VariableAsFunctionDescriptor) descriptor).getVariableDescriptor();
//}
final IntrinsicMethod intrinsic = state.getInjector().getIntrinsics().getIntrinsic(descriptor);
if (intrinsic != null) {
......@@ -1255,14 +1259,24 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
}
else if (funDescriptor instanceof FunctionDescriptor) {
final FunctionDescriptor fd = (FunctionDescriptor) funDescriptor;
return invokeFunction(expression, fd, receiver);
if(resolvedCall instanceof VariableAsFunctionResolvedCall) {
VariableAsFunctionResolvedCall call = (VariableAsFunctionResolvedCall) resolvedCall;
ResolvedCallWithTrace<FunctionDescriptor> functionCall = call.getFunctionCall();
return invokeFunction(expression, functionCall.getResultingDescriptor(), receiver, functionCall);
}
else {
return invokeFunction(expression, fd, receiver, resolvedCall);
}
}
else {
throw new UnsupportedOperationException("unknown type of callee descriptor: " + funDescriptor);
}
}
private StackValue invokeFunction(JetCallExpression expression, DeclarationDescriptor fd, StackValue receiver) {
private StackValue invokeFunction(JetCallExpression expression,
DeclarationDescriptor fd,
StackValue receiver,
ResolvedCall<? extends CallableDescriptor> resolvedCall) {
boolean superCall = false;
if (expression.getParent() instanceof JetQualifiedExpression) {
final JetExpression receiverExpression = ((JetQualifiedExpression) expression.getParent()).getReceiverExpression();
......@@ -1295,8 +1309,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
return returnValueAsStackValue((FunctionDescriptor) fd, callReturnType);
}
else {
ResolvedCall<? extends CallableDescriptor> resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, expression.getCalleeExpression());
assert resolvedCall != null;
receiver = StackValue.receiver(resolvedCall, receiver, this, null, state);
IntrinsicMethod intrinsic = (IntrinsicMethod) callable;
......@@ -1324,11 +1336,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
}
CallableMethod callableMethod;
if (fd instanceof VariableAsFunctionDescriptor) {
assert !superCall;
callableMethod = ClosureCodegen.asCallableMethod((FunctionDescriptor) fd);
}
else if (fd instanceof ExpressionAsFunctionDescriptor || (fd instanceof SimpleFunctionDescriptor && fd.getContainingDeclaration() instanceof FunctionDescriptor)) {
//if (fd instanceof VariableAsFunctionDescriptor) {
// assert !superCall;
// callableMethod = ClosureCodegen.asCallableMethod((FunctionDescriptor) fd);
//}
if (fd instanceof ExpressionAsFunctionDescriptor || (fd instanceof SimpleFunctionDescriptor && fd.getContainingDeclaration() instanceof FunctionDescriptor)) {
SimpleFunctionDescriptor invoke = CodegenUtil.createInvoke((FunctionDescriptor) fd);
callableMethod = ClosureCodegen.asCallableMethod(invoke);
}
......@@ -1351,6 +1363,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
ResolvedCall<? extends CallableDescriptor> resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, expression.getCalleeExpression());
assert resolvedCall != null;
if(resolvedCall instanceof VariableAsFunctionResolvedCall) {
resolvedCall = ((VariableAsFunctionResolvedCall)resolvedCall).getFunctionCall();
}
if(!(resolvedCall.getResultingDescriptor() instanceof ConstructorDescriptor)) { // otherwise already
receiver = StackValue.receiver(resolvedCall, receiver, this, callableMethod, state);
receiver.put(receiver.type, v);
......@@ -1397,7 +1413,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
else if(descriptor instanceof ExpressionReceiver) {
ExpressionReceiver expressionReceiver = (ExpressionReceiver) descriptor;
JetExpression expr = expressionReceiver.getExpression();
Type exprType = expressionType(expr);
Type exprType = asmType(expressionReceiver.getType());
gen(expr, exprType);
if(type != null)
StackValue.onStack(exprType).put(type, v);
......
/*
* Copyright 2010-2012 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.jet.lang.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import java.util.Collections;
/**
* @author abreslav
*/
public class VariableAsFunctionDescriptor extends FunctionDescriptorImpl {
public static VariableAsFunctionDescriptor create(@NotNull VariableDescriptor variableDescriptor) {
JetType outType = variableDescriptor.getType();
assert outType != null;
VariableAsFunctionDescriptor result = new VariableAsFunctionDescriptor(variableDescriptor);
FunctionDescriptorUtil.initializeFromFunctionType(result, outType, variableDescriptor.getExpectedThisObject());
return result;
}
private final VariableDescriptor variableDescriptor;
private VariableAsFunctionDescriptor(VariableDescriptor variableDescriptor) {
super(variableDescriptor.getContainingDeclaration(), Collections.<AnnotationDescriptor>emptyList(), variableDescriptor.getName(), Kind.DECLARATION);
this.variableDescriptor = variableDescriptor;
}
public VariableDescriptor getVariableDescriptor() {
return variableDescriptor;
}
@NotNull
@Override
public VariableAsFunctionDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract, Kind kind, boolean copyOverrides) {
throw new UnsupportedOperationException("Should not be copied for overriding");
}
@Override
protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal, Kind kind) {
throw new IllegalStateException();
}
}
......@@ -49,12 +49,12 @@ public class BindingContextUtils {
throw new IllegalStateException("non-declaration descriptors should be filtered out earler: " + callable);
}
}
if (declarationDescriptor instanceof VariableAsFunctionDescriptor) {
VariableAsFunctionDescriptor descriptor = (VariableAsFunctionDescriptor) declarationDescriptor;
if (descriptor.getOriginal() != descriptor) {
throw new IllegalStateException("original should be resolved earlier: " + descriptor);
}
}
//if (declarationDescriptor instanceof VariableAsFunctionDescriptor) {
// VariableAsFunctionDescriptor descriptor = (VariableAsFunctionDescriptor) declarationDescriptor;
// if (descriptor.getOriginal() != descriptor) {
// throw new IllegalStateException("original should be resolved earlier: " + descriptor);
// }
//}
return declarationDescriptor.getOriginal();
}
};
......@@ -118,9 +118,6 @@ public class BindingContextUtils {
if (descriptor instanceof VariableDescriptor) {
return (VariableDescriptor) descriptor;
}
if (descriptor instanceof VariableAsFunctionDescriptor) {
return ((VariableAsFunctionDescriptor) descriptor).getVariableDescriptor();
}
return null;
}
......
......@@ -99,7 +99,11 @@ public class ResolutionTask<D extends CallableDescriptor, F extends D> extends R
public final TracingStrategy tracing = new TracingStrategy() {
@Override
public <D extends CallableDescriptor> void bindResolvedCall(@NotNull BindingTrace trace, @NotNull ResolvedCallWithTrace<D> resolvedCall) {
trace.record(REFERENCE_TARGET, reference, resolvedCall.getResultingDescriptor());
CallableDescriptor descriptor = resolvedCall.getResultingDescriptor();
if (resolvedCall instanceof VariableAsFunctionResolvedCall) {
descriptor = ((VariableAsFunctionResolvedCall) resolvedCall).getVariableCall().getResultingDescriptor();
}
trace.record(REFERENCE_TARGET, reference, descriptor);
trace.record(RESOLVED_CALL, call.getCalleeExpression(), resolvedCall);
}
......
......@@ -17,11 +17,13 @@
package org.jetbrains.jet.lang.resolve.calls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.resolve.TemporaryBindingTrace;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import java.util.List;
import java.util.Map;
......@@ -32,18 +34,11 @@ import java.util.Map;
public class VariableAsFunctionResolvedCall implements ResolvedCallWithTrace<FunctionDescriptor> {
private final ResolvedCallWithTrace<FunctionDescriptor> functionCall;
private final ResolvedCallWithTrace<VariableDescriptor> variableCall;
private final VariableAsFunctionDescriptor variableAsFunctionDescriptor;
public VariableAsFunctionResolvedCall(@NotNull ResolvedCallWithTrace<FunctionDescriptor> functionCall,
@NotNull ResolvedCallWithTrace<VariableDescriptor> variableCall) {
this.functionCall = functionCall;
this.variableCall = variableCall;
if (JetStandardClasses.isFunctionType(variableCall.getResultingDescriptor().getType())) {
variableAsFunctionDescriptor = VariableAsFunctionDescriptor.create(variableCall.getResultingDescriptor());
}
else {
variableAsFunctionDescriptor = null;
}
}
public ResolvedCallWithTrace<FunctionDescriptor> getFunctionCall() {
......@@ -57,20 +52,19 @@ public class VariableAsFunctionResolvedCall implements ResolvedCallWithTrace<Fun
@NotNull
@Override
public FunctionDescriptor getCandidateDescriptor() {
return variableAsFunctionDescriptor != null ? variableAsFunctionDescriptor : functionCall.getResultingDescriptor();
return functionCall.getResultingDescriptor();
}
@NotNull
@Override
public FunctionDescriptor getResultingDescriptor() {
return variableAsFunctionDescriptor != null ? variableAsFunctionDescriptor : functionCall.getResultingDescriptor();
return functionCall.getResultingDescriptor();
}
@NotNull
@Override
public ReceiverDescriptor getReceiverArgument() {
ReceiverDescriptor receiverArgument = variableCall.getReceiverArgument();
return receiverArgument.exists() ? receiverArgument : functionCall.getReceiverArgument();
return variableCall.getReceiverArgument();
}
@NotNull
......
fun box() : String {
val y = 12
val op = { (x:Int) -> (x + y).toString() }
val op2 : Int.(Int) -> String = { op(this + it) }
return if("27" == 5.op2(10)) "OK" else "fail"
}
......@@ -71,4 +71,9 @@ public class ExtensionFunctionsTest extends CodegenTestCase {
createEnvironmentWithFullJdk();
blackBoxFile("regressions/kt865.jet");
}
public void testKtNested2() throws Exception {
createEnvironmentWithMockJdk(CompilerSpecialMode.BUILTINS);
blackBoxFile("extensionFunctions/nested2.kt");
}
}
......@@ -20,6 +20,7 @@ import com.intellij.openapi.vfs.local.CoreLocalFileSystem;
import gnu.trove.THashSet;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestResult;
import junit.framework.TestSuite;
import org.jetbrains.jet.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.jet.cli.jvm.compiler.K2JVMCompileEnvironmentConfiguration;
......@@ -126,6 +127,19 @@ public class TestlibTest extends CodegenTestCase {
suite.addTestSuite(aClass);
}
}
catch (final VerifyError e) {
suite.addTest(new TestCase(aClass.getName()) {
@Override
public int countTestCases() {
return 1;
}
@Override
public void run(TestResult result) {
result.addError(this, new RuntimeException(e));
}
});
}
catch (NoSuchMethodException e) {
}
}
......@@ -141,7 +155,6 @@ public class TestlibTest extends CodegenTestCase {
}
return suite;
} catch (RuntimeException e) {
throw e;
} catch (Throwable e) {
......
......@@ -279,10 +279,6 @@ public abstract class ExpectedResolveData {
DeclarationDescriptor actualDescriptor = bindingContext.get(REFERENCE_TARGET, reference);
if (actualDescriptor instanceof VariableAsFunctionDescriptor) {
VariableAsFunctionDescriptor descriptor = (VariableAsFunctionDescriptor) actualDescriptor;
actualDescriptor = descriptor.getVariableDescriptor();
}
assertEquals(
"Reference `" + name + "`" + renderReferenceInContext(reference) + " is resolved into " + actualName + ".",
......
......@@ -72,7 +72,7 @@ public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisit
if (calleeDescriptor instanceof ConstructorDescriptor) {
JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.CONSTRUCTOR_CALL);
}
else if (calleeDescriptor instanceof FunctionDescriptor && !(calleeDescriptor instanceof VariableAsFunctionDescriptor)) {
else if (calleeDescriptor instanceof FunctionDescriptor) {
FunctionDescriptor fun = (FunctionDescriptor)calleeDescriptor;
JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.FUNCTION_CALL);
if (fun.getContainingDeclaration() instanceof NamespaceDescriptor) {
......
......@@ -36,9 +36,6 @@ class PropertiesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
return;
}
DeclarationDescriptor target = bindingContext.get(BindingContext.REFERENCE_TARGET, expression);
if (target instanceof VariableAsFunctionDescriptor) {
target = ((VariableAsFunctionDescriptor)target).getVariableDescriptor();
}
if (!(target instanceof PropertyDescriptor)) {
return;
}
......
......@@ -86,11 +86,6 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
}
private void highlightVariable(@NotNull PsiElement elementToHighlight, @NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof VariableAsFunctionDescriptor) {
descriptor = ((VariableAsFunctionDescriptor)descriptor).getVariableDescriptor();
//noinspection ConstantConditions
if (descriptor == null) return;
}
if (descriptor instanceof VariableDescriptor) {
VariableDescriptor variableDescriptor = (VariableDescriptor) descriptor;
if (variableDescriptor.isVar()) {
......
......@@ -21,12 +21,12 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableAsFunctionDescriptor;
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.ResolvedValueArgument;
import org.jetbrains.jet.lang.resolve.calls.VariableAsFunctionResolvedCall;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.general.Translation;
import org.jetbrains.k2js.translate.utils.AnnotationsUtils;
......@@ -78,9 +78,9 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
if (candidateDescriptor instanceof ExpressionAsFunctionDescriptor) {
return translateExpressionAsFunction();
}
if (candidateDescriptor instanceof VariableAsFunctionDescriptor) {
return translateVariableAsFunction();
}
//if (candidateDescriptor instanceof VariableAsFunctionDescriptor) {
// return translateVariableAsFunction();
//}
return null;
}
......
......@@ -81,12 +81,12 @@ public final class CallParametersResolver {
if (!isVariableAsFunction(descriptor)) {
return ReferenceTranslator.translateAsLocalNameReference(descriptor, context);
}
VariableDescriptor variableDescriptor =
getVariableDescriptorForVariableAsFunction((VariableAsFunctionDescriptor)descriptor);
if (variableDescriptor instanceof PropertyDescriptor) {
return getterCall((PropertyDescriptor)variableDescriptor);
}
return ReferenceTranslator.translateAsLocalNameReference(variableDescriptor, context);
//VariableDescriptor variableDescriptor =
// getVariableDescriptorForVariableAsFunction((VariableAsFunctionDescriptor)descriptor);
//if (variableDescriptor instanceof PropertyDescriptor) {
// return getterCall((PropertyDescriptor)variableDescriptor);
//}
return ReferenceTranslator.translateAsLocalNameReference(descriptor, context);
}
//TODO: inspect
......
......@@ -26,7 +26,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableAsFunctionDescriptor;
import org.jetbrains.jet.lang.resolve.calls.ExpressionAsFunctionDescriptor;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.k2js.translate.context.TranslationContext;
......@@ -95,8 +94,8 @@ public final class CallTranslator extends AbstractTranslator {
}
private boolean isExpressionAsFunction() {
return descriptor instanceof ExpressionAsFunctionDescriptor ||
descriptor instanceof VariableAsFunctionDescriptor;
return descriptor instanceof ExpressionAsFunctionDescriptor;// ||
//descriptor instanceof VariableAsFunctionDescriptor;
}
@NotNull
......@@ -142,8 +141,8 @@ public final class CallTranslator extends AbstractTranslator {
}
private boolean isExtensionFunctionLiteral() {
boolean isLiteral = descriptor instanceof VariableAsFunctionDescriptor
|| descriptor instanceof ExpressionAsFunctionDescriptor;
boolean isLiteral = //descriptor instanceof VariableAsFunctionDescriptor ||
descriptor instanceof ExpressionAsFunctionDescriptor;
return isExtensionFunction() && isLiteral;
}
......
......@@ -194,10 +194,10 @@ public final class BindingUtils {
public static DeclarationDescriptor getNullableDescriptorForReferenceExpression(@NotNull BindingContext context,
@NotNull JetReferenceExpression reference) {
DeclarationDescriptor referencedDescriptor = context.get(BindingContext.REFERENCE_TARGET, reference);
if (isVariableAsFunction(referencedDescriptor)) {
assert referencedDescriptor != null;
return getVariableDescriptorForVariableAsFunction((VariableAsFunctionDescriptor)referencedDescriptor);
}
//if (isVariableAsFunction(referencedDescriptor)) {
// assert referencedDescriptor != null;
// return getVariableDescriptorForVariableAsFunction((VariableAsFunctionDescriptor)referencedDescriptor);
//}
return referencedDescriptor;
}
......
......@@ -126,17 +126,17 @@ public final class JsDescriptorUtils {
return (ClassDescriptor)superClassDescriptor;
}
@NotNull
public static VariableDescriptor getVariableDescriptorForVariableAsFunction
(@NotNull VariableAsFunctionDescriptor descriptor) {
VariableDescriptor functionVariable = descriptor.getVariableDescriptor();
assert functionVariable != null;
return functionVariable;
}
//@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 referencedDescriptor instanceof VariableAsFunctionDescriptor;
return false;//referencedDescriptor instanceof VariableAsFunctionDescriptor;
}
@NotNull
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册