Do direct call to local functions, inplace closures, and callable references (not via bridge method)

This commit is contained in:
Mikhael Bogdanov
2013-09-18 15:41:12 +04:00
parent ebecc7b8ac
commit 59c04bd4cf
5 changed files with 46 additions and 31 deletions
@@ -2095,22 +2095,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
}
private boolean isCallAsFunctionObject(FunctionDescriptor fd) {
if (fd.getContainingDeclaration() instanceof ScriptDescriptor) {
JetNamedFunction psi = (JetNamedFunction) descriptorToDeclaration(bindingContext, fd);
assert psi != null;
return !JetPsiUtil.isScriptDeclaration(psi);
}
else if (fd instanceof ExpressionAsFunctionDescriptor) {
return true;
}
else if (fd instanceof SimpleFunctionDescriptor &&
(fd.getContainingDeclaration() instanceof FunctionDescriptor ||
fd.getContainingDeclaration() instanceof ScriptDescriptor)) {
return true;
}
else {
return false;
if (fd instanceof ExpressionAsFunctionDescriptor) {
JetExpression deparenthesize = JetPsiUtil.deparenthesize(((ExpressionAsFunctionDescriptor) fd).getExpression());
return !(deparenthesize instanceof JetCallableReferenceExpression || deparenthesize instanceof JetFunctionLiteralExpression);
}
return false;
}
@@ -2134,9 +2123,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
if (!(resolvedCall.getResultingDescriptor() instanceof ConstructorDescriptor)) { // otherwise already
receiver = StackValue.receiver(resolvedCall, receiver, this, callableMethod);
receiver.put(receiver.type, v);
if (calleeType != null) {
StackValue.onStack(receiver.type).put(boxType(receiver.type), v);
}
}
pushArgumentsAndInvoke(resolvedCall, callableMethod);
@@ -3037,7 +3023,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
private StackValue invokeOperation(JetOperationExpression expression, FunctionDescriptor op, CallableMethod callable) {
int functionLocalIndex = lookupLocalIndex(op);
if (functionLocalIndex >= 0) {
stackValueForLocal(op, functionLocalIndex).put(getFunctionImplClassName(op).getAsmType(), v);
stackValueForLocal(op, functionLocalIndex).put(callable.getOwner().getAsmType(), v);
}
ResolvedCall<? extends CallableDescriptor> resolvedCall =
bindingContext.get(BindingContext.RESOLVED_CALL, expression.getOperationReference());
@@ -31,13 +31,12 @@ import org.jetbrains.jet.codegen.signature.JvmMethodParameterKind;
import org.jetbrains.jet.codegen.signature.JvmMethodParameterSignature;
import org.jetbrains.jet.codegen.signature.JvmMethodSignature;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.JetDelegatorToSuperCall;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetFile;
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.BindingTrace;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.calls.util.ExpressionAsFunctionDescriptor;
import org.jetbrains.jet.lang.resolve.java.*;
import org.jetbrains.jet.lang.resolve.java.mapping.KotlinToJavaTypesMap;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -55,6 +54,7 @@ import static org.jetbrains.jet.codegen.AsmUtil.getTraitImplThisParameterType;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.codegen.FunctionTypesUtil.getFunctionTraitClassName;
import static org.jetbrains.jet.codegen.binding.CodegenBinding.*;
import static org.jetbrains.jet.codegen.binding.CodegenBinding.classNameForAnonymousClass;
public class JetTypeMapper extends BindingTraceAware {
@@ -487,7 +487,25 @@ public class JetTypeMapper extends BindingTraceAware {
JvmClassName ownerForDefaultParam;
int invokeOpcode;
JvmClassName thisClass;
if (functionParent instanceof NamespaceDescriptor) {
Type calleeType = null;
if (isLocalNamedFun(functionDescriptor) || functionDescriptor instanceof ExpressionAsFunctionDescriptor) {
if (functionDescriptor instanceof ExpressionAsFunctionDescriptor) {
JetExpression expression = JetPsiUtil.deparenthesize(((ExpressionAsFunctionDescriptor) functionDescriptor).getExpression());
if (expression instanceof JetFunctionLiteralExpression) {
expression = ((JetFunctionLiteralExpression) expression).getFunctionLiteral();
}
functionDescriptor = bindingContext.get(BindingContext.FUNCTION, expression);
}
functionDescriptor = functionDescriptor.getOriginal();
owner = classNameForAnonymousClass(bindingContext, functionDescriptor);
ownerForDefaultImpl = ownerForDefaultParam = thisClass = owner;
invokeOpcode = INVOKEVIRTUAL;
descriptor = mapSignature("invoke", functionDescriptor, true, kind);
calleeType = owner.getAsmType();
}
else if (functionParent instanceof NamespaceDescriptor) {
assert !superCall;
owner = jvmClassNameForNamespace((NamespaceDescriptor) functionParent, functionDescriptor, isInsideModule);
ownerForDefaultImpl = ownerForDefaultParam = owner;
@@ -571,7 +589,7 @@ public class JetTypeMapper extends BindingTraceAware {
}
return new CallableMethod(
owner, ownerForDefaultImpl, ownerForDefaultParam, descriptor, invokeOpcode,
thisClass, receiverParameterType, null);
thisClass, receiverParameterType, calleeType);
}
public static boolean isAccessor(@NotNull CallableMemberDescriptor descriptor) {