Search in sources :

Example 1 with ValueParameterDescriptor

use of org.jetbrains.kotlin.descriptors.ValueParameterDescriptor in project kotlin by JetBrains.

the class ResolvedCallImpl method setResultingSubstitutor.

@Override
public void setResultingSubstitutor(@NotNull TypeSubstitutor substitutor) {
    resultingDescriptor = (D) candidateDescriptor.substitute(substitutor);
    assert resultingDescriptor != null : candidateDescriptor;
    for (TypeParameterDescriptor typeParameter : candidateDescriptor.getTypeParameters()) {
        TypeProjection typeArgumentProjection = substitutor.getSubstitution().get(typeParameter.getDefaultType());
        if (typeArgumentProjection != null) {
            typeArguments.put(typeParameter, typeArgumentProjection.getType());
        }
    }
    if (candidateDescriptor.getValueParameters().isEmpty())
        return;
    List<ValueParameterDescriptor> substitutedParameters = resultingDescriptor.getValueParameters();
    Collection<Map.Entry<ValueParameterDescriptor, ResolvedValueArgument>> valueArgumentsBeforeSubstitution = new SmartList<Map.Entry<ValueParameterDescriptor, ResolvedValueArgument>>(valueArguments.entrySet());
    valueArguments.clear();
    for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> entry : valueArgumentsBeforeSubstitution) {
        ValueParameterDescriptor substitutedVersion = substitutedParameters.get(entry.getKey().getIndex());
        assert substitutedVersion != null : entry.getKey();
        valueArguments.put(substitutedVersion, entry.getValue());
    }
    Collection<Map.Entry<ValueArgument, ArgumentMatchImpl>> unsubstitutedArgumentMappings = new SmartList<Map.Entry<ValueArgument, ArgumentMatchImpl>>(argumentToParameterMap.entrySet());
    argumentToParameterMap.clear();
    for (Map.Entry<ValueArgument, ArgumentMatchImpl> entry : unsubstitutedArgumentMappings) {
        ArgumentMatchImpl argumentMatch = entry.getValue();
        ValueParameterDescriptor valueParameterDescriptor = argumentMatch.getValueParameter();
        ValueParameterDescriptor substitutedVersion = substitutedParameters.get(valueParameterDescriptor.getIndex());
        assert substitutedVersion != null : valueParameterDescriptor;
        argumentToParameterMap.put(entry.getKey(), argumentMatch.replaceValueParameter(substitutedVersion));
    }
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) TypeProjection(org.jetbrains.kotlin.types.TypeProjection) ValueArgument(org.jetbrains.kotlin.psi.ValueArgument) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor) SmartList(com.intellij.util.SmartList)

Example 2 with ValueParameterDescriptor

use of org.jetbrains.kotlin.descriptors.ValueParameterDescriptor in project kotlin by JetBrains.

the class TailRecursionCodegen method assignParameterValues.

private void assignParameterValues(CallableDescriptor fd, CallableMethod callableMethod, List<ResolvedValueArgument> valueArguments) {
    List<Type> types = callableMethod.getValueParameterTypes();
    for (ValueParameterDescriptor parameterDescriptor : Lists.reverse(fd.getValueParameters())) {
        ResolvedValueArgument arg = valueArguments.get(parameterDescriptor.getIndex());
        Type type = types.get(parameterDescriptor.getIndex());
        if (arg instanceof ExpressionValueArgument) {
            ExpressionValueArgument ev = (ExpressionValueArgument) arg;
            ValueArgument argument = ev.getValueArgument();
            KtExpression argumentExpression = argument == null ? null : argument.getArgumentExpression();
            if (argumentExpression instanceof KtSimpleNameExpression) {
                ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCall(argumentExpression, state.getBindingContext());
                if (resolvedCall != null && resolvedCall.getResultingDescriptor().equals(parameterDescriptor.getOriginal())) {
                    // do nothing: we shouldn't store argument to itself again
                    AsmUtil.pop(v, type);
                    continue;
                }
            }
        //assign the parameter below
        } else if (arg instanceof DefaultValueArgument) {
            AsmUtil.pop(v, type);
            DefaultParameterValueLoader.DEFAULT.genValue(parameterDescriptor, codegen).put(type, v);
        } else if (arg instanceof VarargValueArgument) {
        // assign the parameter below
        } else {
            throw new UnsupportedOperationException("Unknown argument type: " + arg + " in " + fd);
        }
        store(parameterDescriptor, type);
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ValueArgument(org.jetbrains.kotlin.psi.ValueArgument) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtSimpleNameExpression(org.jetbrains.kotlin.psi.KtSimpleNameExpression) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)

Example 3 with ValueParameterDescriptor

use of org.jetbrains.kotlin.descriptors.ValueParameterDescriptor in project kotlin by JetBrains.

the class CallBasedArgumentGenerator method generateVararg.

@Override
protected void generateVararg(int i, @NotNull VarargValueArgument argument) {
    ValueParameterDescriptor parameter = valueParameters.get(i);
    Type type = valueParameterTypes.get(i);
    // Upper bound for type of vararg parameter should always have a form of 'Array<out T>',
    // while its lower bound may be Nothing-typed after approximation
    codegen.genVarargs(argument, FlexibleTypesKt.upperIfFlexible(parameter.getType()));
    callGenerator.afterParameterPut(type, null, i);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)

Example 4 with ValueParameterDescriptor

use of org.jetbrains.kotlin.descriptors.ValueParameterDescriptor in project kotlin by JetBrains.

the class ScriptCodegen method genConstructor.

private void genConstructor(@NotNull ScriptDescriptor scriptDescriptor, @NotNull ClassBuilder classBuilder, @NotNull MethodContext methodContext) {
    JvmMethodSignature jvmSignature = typeMapper.mapScriptSignature(scriptDescriptor, context.getEarlierScripts());
    if (state.getReplSpecific().getShouldGenerateScriptResultValue()) {
        FieldInfo resultFieldInfo = context.getResultFieldInfo();
        classBuilder.newField(JvmDeclarationOrigin.NO_ORIGIN, ACC_PUBLIC | ACC_FINAL, resultFieldInfo.getFieldName(), resultFieldInfo.getFieldType().getDescriptor(), null, null);
    }
    MethodVisitor mv = classBuilder.newMethod(JvmDeclarationOriginKt.OtherOrigin(scriptDeclaration, scriptDescriptor.getUnsubstitutedPrimaryConstructor()), ACC_PUBLIC, jvmSignature.getAsmMethod().getName(), jvmSignature.getAsmMethod().getDescriptor(), null, null);
    if (state.getClassBuilderMode().generateBodies) {
        mv.visitCode();
        InstructionAdapter iv = new InstructionAdapter(mv);
        Type classType = typeMapper.mapType(scriptDescriptor);
        ClassDescriptor superclass = DescriptorUtilsKt.getSuperClassNotAny(scriptDescriptor);
        if (superclass == null) {
            iv.load(0, classType);
            iv.invokespecial("java/lang/Object", "<init>", "()V", false);
        } else {
            ConstructorDescriptor ctorDesc = superclass.getUnsubstitutedPrimaryConstructor();
            if (ctorDesc == null)
                throw new RuntimeException("Primary constructor not found for script template " + superclass.toString());
            iv.load(0, classType);
            int valueParamStart = context.getEarlierScripts().size() + 1;
            List<ValueParameterDescriptor> valueParameters = scriptDescriptor.getUnsubstitutedPrimaryConstructor().getValueParameters();
            for (ValueParameterDescriptor superclassParam : ctorDesc.getValueParameters()) {
                ValueParameterDescriptor valueParam = null;
                for (ValueParameterDescriptor vpd : valueParameters) {
                    if (vpd.getName().equals(superclassParam.getName())) {
                        valueParam = vpd;
                        break;
                    }
                }
                assert valueParam != null;
                iv.load(valueParam.getIndex() + valueParamStart, typeMapper.mapType(valueParam.getType()));
            }
            CallableMethod ctorMethod = typeMapper.mapToCallableMethod(ctorDesc, false);
            String sig = ctorMethod.getAsmMethod().getDescriptor();
            iv.invokespecial(typeMapper.mapSupertype(superclass.getDefaultType(), null).getInternalName(), "<init>", sig, false);
        }
        iv.load(0, classType);
        FrameMap frameMap = new FrameMap();
        frameMap.enterTemp(OBJECT_TYPE);
        for (ScriptDescriptor importedScript : context.getEarlierScripts()) {
            frameMap.enter(importedScript, OBJECT_TYPE);
        }
        int offset = 1;
        for (ScriptDescriptor earlierScript : context.getEarlierScripts()) {
            Type earlierClassType = typeMapper.mapClass(earlierScript);
            iv.load(0, classType);
            iv.load(offset, earlierClassType);
            offset += earlierClassType.getSize();
            iv.putfield(classType.getInternalName(), context.getScriptFieldName(earlierScript), earlierClassType.getDescriptor());
        }
        final ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, Type.VOID_TYPE, methodContext, state, this);
        generateInitializers(new Function0<ExpressionCodegen>() {

            @Override
            public ExpressionCodegen invoke() {
                return codegen;
            }
        });
        iv.areturn(Type.VOID_TYPE);
    }
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ConstructorDescriptor(org.jetbrains.kotlin.descriptors.ConstructorDescriptor) ScriptDescriptor(org.jetbrains.kotlin.descriptors.ScriptDescriptor) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) JvmMethodSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature) Type(org.jetbrains.org.objectweb.asm.Type) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)

Example 5 with ValueParameterDescriptor

use of org.jetbrains.kotlin.descriptors.ValueParameterDescriptor in project kotlin by JetBrains.

the class ResolvedCallImpl method getValueArgumentsByIndex.

@Nullable
@Override
public List<ResolvedValueArgument> getValueArgumentsByIndex() {
    List<ResolvedValueArgument> arguments = new ArrayList<ResolvedValueArgument>(candidateDescriptor.getValueParameters().size());
    for (int i = 0; i < candidateDescriptor.getValueParameters().size(); ++i) {
        arguments.add(null);
    }
    for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> entry : valueArguments.entrySet()) {
        ValueParameterDescriptor parameterDescriptor = entry.getKey();
        ResolvedValueArgument value = entry.getValue();
        ResolvedValueArgument oldValue = arguments.set(parameterDescriptor.getIndex(), value);
        if (oldValue != null) {
            return null;
        }
    }
    for (int i = 0; i < arguments.size(); i++) {
        Object o = arguments.get(i);
        if (o == null) {
            return null;
        }
    }
    return arguments;
}
Also used : ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)8 Type (org.jetbrains.org.objectweb.asm.Type)4 KtExpression (org.jetbrains.kotlin.psi.KtExpression)3 ValueArgument (org.jetbrains.kotlin.psi.ValueArgument)3 SmartList (com.intellij.util.SmartList)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)1 ConstructorDescriptor (org.jetbrains.kotlin.descriptors.ConstructorDescriptor)1 ScriptDescriptor (org.jetbrains.kotlin.descriptors.ScriptDescriptor)1 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)1 KtSimpleNameExpression (org.jetbrains.kotlin.psi.KtSimpleNameExpression)1 JvmMethodSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 TypeProjection (org.jetbrains.kotlin.types.TypeProjection)1 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)1 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)1