Search in sources :

Example 1 with JvmMethodGenericSignature

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

the class KotlinTypeMapper method mapSignatureWithCustomParameters.

@NotNull
public JvmMethodGenericSignature mapSignatureWithCustomParameters(@NotNull FunctionDescriptor f, @NotNull OwnerKind kind, @NotNull List<ValueParameterDescriptor> valueParameters, boolean skipGenericSignature) {
    checkOwnerCompatibility(f);
    JvmSignatureWriter sw = skipGenericSignature || f instanceof AccessorForCallableDescriptor ? new JvmSignatureWriter() : new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
    if (f instanceof ClassConstructorDescriptor) {
        sw.writeParametersStart();
        writeAdditionalConstructorParameters((ClassConstructorDescriptor) f, sw);
        for (ValueParameterDescriptor parameter : valueParameters) {
            writeParameter(sw, parameter.getType(), f);
        }
        if (f instanceof AccessorForConstructorDescriptor) {
            writeParameter(sw, JvmMethodParameterKind.CONSTRUCTOR_MARKER, DEFAULT_CONSTRUCTOR_MARKER);
        }
        writeVoidReturn(sw);
    } else {
        CallableMemberDescriptor directMember = DescriptorUtils.getDirectMember(f);
        KotlinType thisIfNeeded = null;
        if (OwnerKind.DEFAULT_IMPLS == kind) {
            ReceiverTypeAndTypeParameters receiverTypeAndTypeParameters = TypeMapperUtilsKt.patchTypeParametersForDefaultImplMethod(directMember);
            writeFormalTypeParameters(CollectionsKt.plus(receiverTypeAndTypeParameters.getTypeParameters(), directMember.getTypeParameters()), sw);
            thisIfNeeded = receiverTypeAndTypeParameters.getReceiverType();
        } else {
            writeFormalTypeParameters(directMember.getTypeParameters(), sw);
            if (isAccessor(f) && f.getDispatchReceiverParameter() != null) {
                thisIfNeeded = ((ClassDescriptor) f.getContainingDeclaration()).getDefaultType();
            }
        }
        sw.writeParametersStart();
        if (thisIfNeeded != null) {
            writeParameter(sw, JvmMethodParameterKind.THIS, thisIfNeeded, f);
        }
        ReceiverParameterDescriptor receiverParameter = f.getExtensionReceiverParameter();
        if (receiverParameter != null) {
            writeParameter(sw, JvmMethodParameterKind.RECEIVER, receiverParameter.getType(), f);
        }
        for (ValueParameterDescriptor parameter : valueParameters) {
            boolean forceBoxing = MethodSignatureMappingKt.forceSingleValueParameterBoxing(f);
            writeParameter(sw, forceBoxing ? TypeUtils.makeNullable(parameter.getType()) : parameter.getType(), f);
        }
        sw.writeReturnType();
        mapReturnType(f, sw);
        sw.writeReturnTypeEnd();
    }
    JvmMethodGenericSignature signature = sw.makeJvmMethodSignature(mapFunctionName(f));
    if (kind != OwnerKind.DEFAULT_IMPLS) {
        SpecialSignatureInfo specialSignatureInfo = BuiltinMethodsWithSpecialGenericSignature.getSpecialSignatureInfo(f);
        if (specialSignatureInfo != null) {
            String newGenericSignature = CodegenUtilKt.replaceValueParametersIn(specialSignatureInfo, signature.getGenericsSignature());
            return new JvmMethodGenericSignature(signature.getAsmMethod(), signature.getValueParameters(), newGenericSignature);
        }
    }
    return signature;
}
Also used : BothSignatureWriter(org.jetbrains.kotlin.codegen.signature.BothSignatureWriter) JvmMethodGenericSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature) JavaCallableMemberDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor) DeserializedCallableMemberDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor) SpecialSignatureInfo(org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature.SpecialSignatureInfo) JvmSignatureWriter(org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with JvmMethodGenericSignature

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

the class FunctionCodegen method generateMethod.

public void generateMethod(@NotNull JvmDeclarationOrigin origin, @NotNull FunctionDescriptor functionDescriptor, @NotNull MethodContext methodContext, @NotNull FunctionGenerationStrategy strategy) {
    OwnerKind contextKind = methodContext.getContextKind();
    if (isInterface(functionDescriptor.getContainingDeclaration()) && functionDescriptor.getVisibility() == Visibilities.PRIVATE && !processInterfaceMember(functionDescriptor, contextKind, state)) {
        return;
    }
    JvmMethodGenericSignature jvmSignature = typeMapper.mapSignatureWithGeneric(functionDescriptor, contextKind);
    Method asmMethod = jvmSignature.getAsmMethod();
    int flags = getMethodAsmFlags(functionDescriptor, contextKind, state);
    if (origin.getOriginKind() == JvmDeclarationOriginKind.SAM_DELEGATION) {
        flags |= ACC_SYNTHETIC;
    }
    if (functionDescriptor.isExternal() && owner instanceof MultifileClassFacadeContext) {
        // Native methods are only defined in facades and do not need package part implementations
        return;
    }
    MethodVisitor mv = v.newMethod(origin, flags, asmMethod.getName(), asmMethod.getDescriptor(), jvmSignature.getGenericsSignature(), getThrownExceptions(functionDescriptor, typeMapper));
    if (CodegenContextUtil.isImplClassOwner(owner)) {
        v.getSerializationBindings().put(METHOD_FOR_FUNCTION, CodegenUtilKt.<FunctionDescriptor>unwrapFrontendVersion(functionDescriptor), asmMethod);
    }
    generateMethodAnnotations(functionDescriptor, asmMethod, mv);
    JvmMethodSignature signature = typeMapper.mapSignatureSkipGeneric(functionDescriptor);
    generateParameterAnnotations(functionDescriptor, mv, signature);
    GenerateJava8ParameterNamesKt.generateParameterNames(functionDescriptor, mv, signature, state, (flags & ACC_SYNTHETIC) != 0);
    generateBridges(functionDescriptor);
    if (isJvm8InterfaceWithDefaultsMember(functionDescriptor, state) && contextKind != OwnerKind.DEFAULT_IMPLS && state.getGenerateDefaultImplsForJvm8()) {
        generateDelegateForDefaultImpl(functionDescriptor, origin.getElement());
    }
    boolean staticInCompanionObject = CodegenUtilKt.isJvmStaticInCompanionObject(functionDescriptor);
    if (staticInCompanionObject) {
        ImplementationBodyCodegen parentBodyCodegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
        parentBodyCodegen.addAdditionalTask(new JvmStaticInCompanionObjectGenerator(functionDescriptor, origin, state, parentBodyCodegen));
    }
    if (!state.getClassBuilderMode().generateBodies || isAbstractMethod(functionDescriptor, contextKind, state)) {
        generateLocalVariableTable(mv, jvmSignature, functionDescriptor, getThisTypeForFunction(functionDescriptor, methodContext, typeMapper), new Label(), new Label(), contextKind, typeMapper, 0);
        mv.visitEnd();
        return;
    }
    if (!functionDescriptor.isExternal()) {
        generateMethodBody(mv, functionDescriptor, methodContext, jvmSignature, strategy, memberCodegen);
    } else if (staticInCompanionObject) {
        // native @JvmStatic foo() in companion object should delegate to the static native function moved to the outer class
        mv.visitCode();
        FunctionDescriptor staticFunctionDescriptor = JvmStaticInCompanionObjectGenerator.createStaticFunctionDescriptor(functionDescriptor);
        Method accessorMethod = typeMapper.mapAsmMethod(memberCodegen.getContext().accessibleDescriptor(staticFunctionDescriptor, null));
        Type owningType = typeMapper.mapClass((ClassifierDescriptor) staticFunctionDescriptor.getContainingDeclaration());
        generateDelegateToStaticMethodBody(false, mv, accessorMethod, owningType.getInternalName());
    }
    endVisit(mv, null, origin.getElement());
}
Also used : JvmMethodGenericSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature) Method(org.jetbrains.org.objectweb.asm.commons.Method) TraceMethodVisitor(org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor) JvmMethodSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 3 with JvmMethodGenericSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature 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 4 with JvmMethodGenericSignature

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

the class PropertyCodegen method generateConstructorPropertyAsMethodForAnnotationClass.

public void generateConstructorPropertyAsMethodForAnnotationClass(KtParameter p, PropertyDescriptor descriptor) {
    JvmMethodGenericSignature signature = typeMapper.mapAnnotationParameterSignature(descriptor);
    String name = p.getName();
    if (name == null)
        return;
    MethodVisitor mv = v.newMethod(JvmDeclarationOriginKt.OtherOrigin(p, descriptor), ACC_PUBLIC | ACC_ABSTRACT, name, signature.getAsmMethod().getDescriptor(), signature.getGenericsSignature(), null);
    KtExpression defaultValue = p.getDefaultValue();
    if (defaultValue != null) {
        ConstantValue<?> constant = ExpressionCodegen.getCompileTimeConstant(defaultValue, bindingContext, true, state.getShouldInlineConstVals());
        assert !state.getClassBuilderMode().generateBodies || constant != null : "Default value for annotation parameter should be compile time value: " + defaultValue.getText();
        if (constant != null) {
            AnnotationCodegen annotationCodegen = AnnotationCodegen.forAnnotationDefaultValue(mv, memberCodegen, typeMapper);
            annotationCodegen.generateAnnotationDefaultValue(constant, descriptor.getType());
        }
    }
    mv.visitEnd();
}
Also used : JvmMethodGenericSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Aggregations

JvmMethodGenericSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature)4 NotNull (org.jetbrains.annotations.NotNull)2 Method (org.jetbrains.org.objectweb.asm.commons.Method)2 ArrayList (java.util.ArrayList)1 BothSignatureWriter (org.jetbrains.kotlin.codegen.signature.BothSignatureWriter)1 JvmSignatureWriter (org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter)1 SpecialSignatureInfo (org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature.SpecialSignatureInfo)1 JavaCallableMemberDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor)1 JvmMethodParameterSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature)1 JvmMethodSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature)1 DeserializedCallableMemberDescriptor (org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)1 Type (org.jetbrains.org.objectweb.asm.Type)1 TraceMethodVisitor (org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor)1