Search in sources :

Example 6 with JvmMethodSignature

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

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature 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)

Aggregations

JvmMethodSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature)7 NotNull (org.jetbrains.annotations.NotNull)3 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)2 Type (org.jetbrains.org.objectweb.asm.Type)2 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)2 Method (org.jetbrains.org.objectweb.asm.commons.Method)2 FunctionClassDescriptor (org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor)1 GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)1 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)1 ConstructorDescriptor (org.jetbrains.kotlin.descriptors.ConstructorDescriptor)1 ScriptDescriptor (org.jetbrains.kotlin.descriptors.ScriptDescriptor)1 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)1 LocalVariableAccessorDescriptor (org.jetbrains.kotlin.descriptors.impl.LocalVariableAccessorDescriptor)1 TypeAliasConstructorDescriptor (org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor)1 IrBuiltinsPackageFragmentDescriptor (org.jetbrains.kotlin.ir.descriptors.IrBuiltinsPackageFragmentDescriptor)1 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)1 JvmDeclarationOrigin (org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin)1 JvmMethodGenericSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature)1 JvmMethodParameterSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature)1