Search in sources :

Example 1 with DelegatingResolvedCall

use of org.jetbrains.kotlin.resolve.calls.model.DelegatingResolvedCall in project kotlin by JetBrains.

the class FunctionReferenceGenerationStrategy method doGenerateBody.

@Override
public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
    /*
         Here we need to put the arguments from our locals to the stack and invoke the referenced method. Since invocation
         of methods is highly dependent on expressions, we create a fake call expression. Then we create a new instance of
         ExpressionCodegen and, in order for it to generate code correctly, we save to its 'tempVariables' field every
         argument of our fake expression, pointing it to the corresponding index in our locals. This way generation of
         every argument boils down to calling LOAD with the corresponding index
         */
    KtCallExpression fakeExpression = CodegenUtil.constructFakeFunctionCall(state.getProject(), referencedFunction);
    final List<? extends ValueArgument> fakeArguments = fakeExpression.getValueArguments();
    final ReceiverValue dispatchReceiver = computeAndSaveReceiver(signature, codegen, referencedFunction.getDispatchReceiverParameter());
    final ReceiverValue extensionReceiver = computeAndSaveReceiver(signature, codegen, referencedFunction.getExtensionReceiverParameter());
    computeAndSaveArguments(fakeArguments, codegen);
    ResolvedCall<CallableDescriptor> fakeResolvedCall = new DelegatingResolvedCall<CallableDescriptor>(resolvedCall) {

        private final Map<ValueParameterDescriptor, ResolvedValueArgument> argumentMap;

        {
            argumentMap = new LinkedHashMap<ValueParameterDescriptor, ResolvedValueArgument>(fakeArguments.size());
            int index = 0;
            List<ValueParameterDescriptor> parameters = functionDescriptor.getValueParameters();
            for (ValueArgument argument : fakeArguments) {
                argumentMap.put(parameters.get(index), new ExpressionValueArgument(argument));
                index++;
            }
        }

        @Nullable
        @Override
        public ReceiverValue getExtensionReceiver() {
            return extensionReceiver;
        }

        @Nullable
        @Override
        public ReceiverValue getDispatchReceiver() {
            return dispatchReceiver;
        }

        @NotNull
        @Override
        public List<ResolvedValueArgument> getValueArgumentsByIndex() {
            return new ArrayList<ResolvedValueArgument>(argumentMap.values());
        }

        @NotNull
        @Override
        public Map<ValueParameterDescriptor, ResolvedValueArgument> getValueArguments() {
            return argumentMap;
        }
    };
    StackValue result;
    Type returnType = codegen.getReturnType();
    if (referencedFunction instanceof ConstructorDescriptor) {
        if (returnType.getSort() == Type.ARRAY) {
            //noinspection ConstantConditions
            result = codegen.generateNewArray(fakeExpression, referencedFunction.getReturnType(), fakeResolvedCall);
        } else {
            result = codegen.generateConstructorCall(fakeResolvedCall, returnType);
        }
    } else {
        Call call = CallMaker.makeCall(fakeExpression, null, null, fakeExpression, fakeArguments);
        result = codegen.invokeFunction(call, fakeResolvedCall, StackValue.none());
    }
    InstructionAdapter v = codegen.v;
    result.put(returnType, v);
    v.areturn(returnType);
}
Also used : ResolvedValueArgument(org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument) ExpressionValueArgument(org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument) ResolvedCall(org.jetbrains.kotlin.resolve.calls.model.ResolvedCall) DelegatingResolvedCall(org.jetbrains.kotlin.resolve.calls.model.DelegatingResolvedCall) Type(org.jetbrains.org.objectweb.asm.Type) DelegatingResolvedCall(org.jetbrains.kotlin.resolve.calls.model.DelegatingResolvedCall) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) ExpressionValueArgument(org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument) ResolvedValueArgument(org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument) ReceiverValue(org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue)

Aggregations

DelegatingResolvedCall (org.jetbrains.kotlin.resolve.calls.model.DelegatingResolvedCall)1 ExpressionValueArgument (org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument)1 ResolvedCall (org.jetbrains.kotlin.resolve.calls.model.ResolvedCall)1 ResolvedValueArgument (org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument)1 ReceiverValue (org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue)1 Type (org.jetbrains.org.objectweb.asm.Type)1 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)1