Search in sources :

Example 6 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateSuperCallImplicitArguments.

@NotNull
private ArgumentGenerator generateSuperCallImplicitArguments(@NotNull InstructionAdapter iv, @NotNull ExpressionCodegen codegen, @NotNull ConstructorDescriptor constructorDescriptor, @NotNull ConstructorDescriptor superConstructor, @NotNull CallableMethod superCallable, @NotNull List<JvmMethodParameterSignature> superParameters, @NotNull List<JvmMethodParameterSignature> parameters) {
    int offset = 1;
    int superIndex = 0;
    // them all onto the stack and update "offset" variable so that in the end it points to the slot of the first VALUE argument
    for (JvmMethodParameterSignature parameter : parameters) {
        if (superIndex >= superParameters.size())
            break;
        JvmMethodParameterKind superKind = superParameters.get(superIndex).getKind();
        JvmMethodParameterKind kind = parameter.getKind();
        Type type = parameter.getAsmType();
        if (superKind == JvmMethodParameterKind.VALUE && kind == JvmMethodParameterKind.SUPER_CALL_PARAM) {
            // Stop when we reach the actual value parameters present in the code; they will be generated via ResolvedCall below
            break;
        }
        if (superKind == JvmMethodParameterKind.OUTER) {
            assert kind == JvmMethodParameterKind.OUTER || kind == JvmMethodParameterKind.SUPER_CALL_PARAM : String.format("Non-outer parameter incorrectly mapped to outer for %s: %s vs %s", constructorDescriptor, parameters, superParameters);
            // Super constructor requires OUTER parameter, but our OUTER instance may be different from what is expected by the super
            // constructor. We need to traverse our outer classes from the bottom up, to find the needed class. See innerExtendsOuter.kt
            ClassDescriptor outerForSuper = (ClassDescriptor) superConstructor.getContainingDeclaration().getContainingDeclaration();
            StackValue outer = codegen.generateThisOrOuter(outerForSuper, true, true);
            outer.put(outer.type, codegen.v);
            superIndex++;
        } else if (kind == JvmMethodParameterKind.SUPER_CALL_PARAM || kind == JvmMethodParameterKind.ENUM_NAME_OR_ORDINAL) {
            iv.load(offset, type);
            superIndex++;
        }
        offset += type.getSize();
    }
    if (isAnonymousObject(descriptor)) {
        List<JvmMethodParameterSignature> superValues = superParameters.subList(superIndex, superParameters.size());
        return new ObjectSuperCallArgumentGenerator(superValues, iv, offset);
    } else {
        return new CallBasedArgumentGenerator(codegen, codegen.defaultCallGenerator, superConstructor.getValueParameters(), superCallable.getValueParameterTypes());
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature) JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) JvmMethodParameterKind(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind) NotNull(org.jetbrains.annotations.NotNull) BindingContextUtils.getNotNull(org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)

Example 7 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateThisCallImplicitArguments.

@NotNull
private static ArgumentGenerator generateThisCallImplicitArguments(@NotNull InstructionAdapter iv, @NotNull ExpressionCodegen codegen, @NotNull ConstructorDescriptor delegatingConstructor, @NotNull CallableMethod delegatingCallable, @NotNull List<JvmMethodParameterSignature> delegatingParameters, @NotNull List<JvmMethodParameterSignature> parameters) {
    int offset = 1;
    int index = 0;
    for (; index < delegatingParameters.size(); index++) {
        JvmMethodParameterKind delegatingKind = delegatingParameters.get(index).getKind();
        if (delegatingKind == JvmMethodParameterKind.VALUE) {
            assert index == parameters.size() || parameters.get(index).getKind() == JvmMethodParameterKind.VALUE : "Delegating constructor has not enough implicit parameters";
            break;
        }
        assert index < parameters.size() && parameters.get(index).getKind() == delegatingKind : "Constructors of the same class should have the same set of implicit arguments";
        JvmMethodParameterSignature parameter = parameters.get(index);
        iv.load(offset, parameter.getAsmType());
        offset += parameter.getAsmType().getSize();
    }
    assert index == parameters.size() || parameters.get(index).getKind() == JvmMethodParameterKind.VALUE : "Delegating constructor has not enough parameters";
    return new CallBasedArgumentGenerator(codegen, codegen.defaultCallGenerator, delegatingConstructor.getValueParameters(), delegatingCallable.getValueParameterTypes());
}
Also used : JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature) JvmMethodParameterKind(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind) NotNull(org.jetbrains.annotations.NotNull) BindingContextUtils.getNotNull(org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)

Example 8 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class FunctionCodegen method generateLocalVariablesForParameters.

private static void generateLocalVariablesForParameters(@NotNull MethodVisitor mv, @NotNull JvmMethodSignature jvmMethodSignature, @Nullable Type thisType, @NotNull Label methodBegin, @NotNull Label methodEnd, Collection<ValueParameterDescriptor> valueParameters, boolean isStatic, KotlinTypeMapper typeMapper, int shiftForDestructuringVariables) {
    Iterator<ValueParameterDescriptor> valueParameterIterator = valueParameters.iterator();
    List<JvmMethodParameterSignature> params = jvmMethodSignature.getValueParameters();
    int shift = 0;
    if (!isStatic) {
        //add this
        if (thisType != null) {
            mv.visitLocalVariable("this", thisType.getDescriptor(), null, methodBegin, methodEnd, shift);
        } else {
        //TODO: provide thisType for callable reference
        }
        shift++;
    }
    for (int i = 0; i < params.size(); i++) {
        JvmMethodParameterSignature param = params.get(i);
        JvmMethodParameterKind kind = param.getKind();
        String parameterName;
        if (kind == JvmMethodParameterKind.VALUE) {
            ValueParameterDescriptor parameter = valueParameterIterator.next();
            List<VariableDescriptor> destructuringVariables = ValueParameterDescriptorImpl.getDestructuringVariablesOrNull(parameter);
            parameterName = destructuringVariables == null ? computeParameterName(i, parameter) : "$" + joinParameterNames(destructuringVariables);
        } else {
            String lowercaseKind = kind.name().toLowerCase();
            parameterName = needIndexForVar(kind) ? "$" + lowercaseKind + "$" + i : "$" + lowercaseKind;
        }
        Type type = param.getAsmType();
        mv.visitLocalVariable(parameterName, type.getDescriptor(), null, methodBegin, methodEnd, shift);
        shift += type.getSize();
    }
    shift += shiftForDestructuringVariables;
    for (ValueParameterDescriptor parameter : valueParameters) {
        List<VariableDescriptor> destructuringVariables = ValueParameterDescriptorImpl.getDestructuringVariablesOrNull(parameter);
        if (destructuringVariables == null)
            continue;
        for (VariableDescriptor entry : CodegenUtilKt.filterOutDescriptorsWithSpecialNames(destructuringVariables)) {
            Type type = typeMapper.mapType(entry.getType());
            mv.visitLocalVariable(entry.getName().asString(), type.getDescriptor(), null, methodBegin, methodEnd, shift);
            shift += type.getSize();
        }
    }
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature) JvmMethodParameterKind(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind)

Example 9 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class JvmSignatureWriter method makeJvmMethodSignature.

@NotNull
public JvmMethodGenericSignature makeJvmMethodSignature(@NotNull String name) {
    List<Type> types = new ArrayList<Type>(kotlinParameterTypes.size());
    for (JvmMethodParameterSignature parameter : kotlinParameterTypes) {
        types.add(parameter.getAsmType());
    }
    Method asmMethod = new Method(name, jvmReturnType, types.toArray(new Type[types.size()]));
    return new JvmMethodGenericSignature(asmMethod, kotlinParameterTypes, makeJavaGenericSignature());
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature) ArrayList(java.util.ArrayList) JvmMethodGenericSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature) Method(org.jetbrains.org.objectweb.asm.commons.Method) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class JvmSignatureWriter method writeParameterTypeEnd.

public void writeParameterTypeEnd() {
    //noinspection ConstantConditions
    kotlinParameterTypes.add(new JvmMethodParameterSignature(getJvmCurrentType(), currentParameterKind));
    currentSignatureSize += getJvmCurrentType().getSize();
    currentParameterKind = null;
    clearCurrentType();
}
Also used : JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature)

Aggregations

JvmMethodParameterSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature)11 JvmMethodParameterKind (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind)5 KotlinType (org.jetbrains.kotlin.types.KotlinType)5 NotNull (org.jetbrains.annotations.NotNull)3 TypeAliasConstructorDescriptor (org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor)2 BindingContextUtils.getNotNull (org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)2 Type (org.jetbrains.org.objectweb.asm.Type)2 ArrayList (java.util.ArrayList)1 AnnotatedWithOnlyTargetedAnnotations (org.jetbrains.kotlin.codegen.annotation.AnnotatedWithOnlyTargetedAnnotations)1 GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)1 KotlinTypeMapper (org.jetbrains.kotlin.codegen.state.KotlinTypeMapper)1 Annotated (org.jetbrains.kotlin.descriptors.annotations.Annotated)1 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)1 SamConstructorDescriptor (org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor)1 DefaultValueArgument (org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument)1 ResolvedValueArgument (org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument)1 JvmMethodGenericSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature)1 JvmMethodSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature)1 Type.getObjectType (org.jetbrains.org.objectweb.asm.Type.getObjectType)1 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)1