Search in sources :

Example 16 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project kotlin by JetBrains.

the class InlineCodegen method doCreateMethodNodeFromSource.

@NotNull
private static SMAPAndMethodNode doCreateMethodNodeFromSource(@NotNull FunctionDescriptor callableDescriptor, @NotNull JvmMethodSignature jvmSignature, @NotNull ExpressionCodegen codegen, @NotNull CodegenContext context, boolean callDefault, @NotNull GenerationState state, @NotNull Method asmMethod) {
    PsiElement element = DescriptorToSourceUtils.descriptorToDeclaration(callableDescriptor);
    if (!(element instanceof KtNamedFunction || element instanceof KtPropertyAccessor)) {
        throw new IllegalStateException("Couldn't find declaration for function " + callableDescriptor);
    }
    KtDeclarationWithBody inliningFunction = (KtDeclarationWithBody) element;
    MethodNode node = new MethodNode(InlineCodegenUtil.API, getMethodAsmFlags(callableDescriptor, context.getContextKind(), state) | (callDefault ? Opcodes.ACC_STATIC : 0), asmMethod.getName(), asmMethod.getDescriptor(), null, null);
    //for maxLocals calculation
    MethodVisitor maxCalcAdapter = InlineCodegenUtil.wrapWithMaxLocalCalc(node);
    CodegenContext parentContext = context.getParentContext();
    assert parentContext != null : "Context has no parent: " + context;
    MethodContext methodContext = parentContext.intoFunction(callableDescriptor);
    SMAP smap;
    if (callDefault) {
        Type implementationOwner = state.getTypeMapper().mapImplementationOwner(callableDescriptor);
        FakeMemberCodegen parentCodegen = new FakeMemberCodegen(codegen.getParentCodegen(), inliningFunction, (FieldOwnerContext) methodContext.getParentContext(), implementationOwner.getInternalName());
        if (!(element instanceof KtNamedFunction)) {
            throw new IllegalStateException("Propertiy accessors with default parameters not supported " + callableDescriptor);
        }
        FunctionCodegen.generateDefaultImplBody(methodContext, callableDescriptor, maxCalcAdapter, DefaultParameterValueLoader.DEFAULT, (KtNamedFunction) inliningFunction, parentCodegen, asmMethod);
        smap = createSMAPWithDefaultMapping(inliningFunction, parentCodegen.getOrCreateSourceMapper().getResultMappings());
    } else {
        smap = generateMethodBody(maxCalcAdapter, callableDescriptor, methodContext, inliningFunction, jvmSignature, codegen, null);
    }
    maxCalcAdapter.visitMaxs(-1, -1);
    maxCalcAdapter.visitEnd();
    return new SMAPAndMethodNode(node, smap);
}
Also used : MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor 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 18 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project kotlin by JetBrains.

the class MappingClassesForWhenByEnumCodegen method generateInitialization.

private void generateInitialization(@NotNull ClassBuilder cb, @NotNull List<WhenByEnumsMapping> mappings) {
    MethodVisitor mv = cb.newMethod(JvmDeclarationOrigin.NO_ORIGIN, ACC_STATIC | ACC_SYNTHETIC, "<clinit>", "()V", null, ArrayUtil.EMPTY_STRING_ARRAY);
    mv.visitCode();
    InstructionAdapter v = new InstructionAdapter(mv);
    for (WhenByEnumsMapping mapping : mappings) {
        generateInitializationForMapping(cb, v, mapping);
    }
    v.areturn(Type.VOID_TYPE);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
Also used : InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 19 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project kotlin by JetBrains.

the class ClosureCodegen method generateFunctionReferenceMethods.

// TODO: ImplementationBodyCodegen.markLineNumberForSyntheticFunction?
private void generateFunctionReferenceMethods(@NotNull FunctionDescriptor descriptor) {
    int flags = ACC_PUBLIC | ACC_FINAL;
    boolean generateBody = state.getClassBuilderMode().generateBodies;
    {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, flags, "getOwner", Type.getMethodDescriptor(K_DECLARATION_CONTAINER_TYPE), null, null);
        if (generateBody) {
            mv.visitCode();
            InstructionAdapter iv = new InstructionAdapter(mv);
            generateCallableReferenceDeclarationContainer(iv, descriptor, state);
            iv.areturn(K_DECLARATION_CONTAINER_TYPE);
            FunctionCodegen.endVisit(iv, "function reference getOwner", element);
        }
    }
    {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, flags, "getName", Type.getMethodDescriptor(JAVA_STRING_TYPE), null, null);
        if (generateBody) {
            mv.visitCode();
            InstructionAdapter iv = new InstructionAdapter(mv);
            iv.aconst(descriptor.getName().asString());
            iv.areturn(JAVA_STRING_TYPE);
            FunctionCodegen.endVisit(iv, "function reference getName", element);
        }
    }
    {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, flags, "getSignature", Type.getMethodDescriptor(JAVA_STRING_TYPE), null, null);
        if (generateBody) {
            mv.visitCode();
            InstructionAdapter iv = new InstructionAdapter(mv);
            PropertyReferenceCodegen.generateCallableReferenceSignature(iv, descriptor, state);
            iv.areturn(JAVA_STRING_TYPE);
            FunctionCodegen.endVisit(iv, "function reference getSignature", element);
        }
    }
}
Also used : InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 20 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor 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

MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)23 NotNull (org.jetbrains.annotations.NotNull)7 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)7 Type (org.jetbrains.org.objectweb.asm.Type)5 KotlinType (org.jetbrains.kotlin.types.KotlinType)4 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)3 Label (org.jetbrains.org.objectweb.asm.Label)3 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)3 SourcePosition (com.intellij.debugger.SourcePosition)2 DebuggerUtilsEx (com.intellij.debugger.impl.DebuggerUtilsEx)2 MethodBytecodeUtil (com.intellij.debugger.jdi.MethodBytecodeUtil)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Document (com.intellij.openapi.editor.Document)2 com.intellij.psi (com.intellij.psi)2 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)2 Opcodes (org.jetbrains.org.objectweb.asm.Opcodes)2