Search in sources :

Example 6 with Method

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

the class LambdaInfo method addAllParameters.

@NotNull
public Parameters addAllParameters(@NotNull FieldRemapper remapper) {
    Method asmMethod = typeMapper.mapAsmMethod(getFunctionDescriptor());
    ParametersBuilder builder = ParametersBuilder.initializeBuilderFrom(AsmTypes.OBJECT_TYPE, asmMethod.getDescriptor(), this);
    for (CapturedParamDesc info : getCapturedVars()) {
        CapturedParamInfo field = remapper.findField(new FieldInsnNode(0, info.getContainingLambdaName(), info.getFieldName(), ""));
        assert field != null : "Captured field not found: " + info.getContainingLambdaName() + "." + info.getFieldName();
        builder.addCapturedParam(field, info.getFieldName());
    }
    return builder.buildParameters();
}
Also used : FieldInsnNode(org.jetbrains.org.objectweb.asm.tree.FieldInsnNode) Method(org.jetbrains.org.objectweb.asm.commons.Method) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with Method

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

the class MemberCodegen method generatePropertyMetadataArrayFieldIfNeeded.

protected void generatePropertyMetadataArrayFieldIfNeeded(@NotNull Type thisAsmType) {
    List<KtProperty> delegatedProperties = new ArrayList<KtProperty>();
    for (KtDeclaration declaration : ((KtDeclarationContainer) element).getDeclarations()) {
        if (declaration instanceof KtProperty) {
            KtProperty property = (KtProperty) declaration;
            if (property.hasDelegate()) {
                delegatedProperties.add(property);
            }
        }
    }
    if (delegatedProperties.isEmpty())
        return;
    v.newField(NO_ORIGIN, ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, JvmAbi.DELEGATED_PROPERTIES_ARRAY_NAME, "[" + K_PROPERTY_TYPE, null, null);
    if (!state.getClassBuilderMode().generateBodies)
        return;
    InstructionAdapter iv = createOrGetClInitCodegen().v;
    iv.iconst(delegatedProperties.size());
    iv.newarray(K_PROPERTY_TYPE);
    for (int i = 0, size = delegatedProperties.size(); i < size; i++) {
        PropertyDescriptor property = (PropertyDescriptor) BindingContextUtils.getNotNull(bindingContext, VARIABLE, delegatedProperties.get(i));
        iv.dup();
        iv.iconst(i);
        int receiverCount = (property.getDispatchReceiverParameter() != null ? 1 : 0) + (property.getExtensionReceiverParameter() != null ? 1 : 0);
        Type implType = property.isVar() ? MUTABLE_PROPERTY_REFERENCE_IMPL[receiverCount] : PROPERTY_REFERENCE_IMPL[receiverCount];
        iv.anew(implType);
        iv.dup();
        // TODO: generate the container once and save to a local field instead (KT-10495)
        ClosureCodegen.generateCallableReferenceDeclarationContainer(iv, property, state);
        iv.aconst(property.getName().asString());
        PropertyReferenceCodegen.generateCallableReferenceSignature(iv, property, state);
        iv.invokespecial(implType.getInternalName(), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, K_DECLARATION_CONTAINER_TYPE, JAVA_STRING_TYPE, JAVA_STRING_TYPE), false);
        Method wrapper = PropertyReferenceCodegen.getWrapperMethodForPropertyReference(property, receiverCount);
        iv.invokestatic(REFLECTION, wrapper.getName(), wrapper.getDescriptor(), false);
        StackValue.onStack(implType).put(K_PROPERTY_TYPE, iv);
        iv.astore(K_PROPERTY_TYPE);
    }
    iv.putstatic(thisAsmType.getInternalName(), JvmAbi.DELEGATED_PROPERTIES_ARRAY_NAME, "[" + K_PROPERTY_TYPE);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) Method(org.jetbrains.org.objectweb.asm.commons.Method)

Example 8 with Method

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

the class FunctionCodegen method generateDelegateForDefaultImpl.

private void generateDelegateForDefaultImpl(@NotNull final FunctionDescriptor functionDescriptor, @Nullable PsiElement element) {
    Method defaultImplMethod = typeMapper.mapAsmMethod(functionDescriptor, OwnerKind.DEFAULT_IMPLS);
    CodegenUtilKt.generateMethod(v, "Default Impl delegate in interface", Opcodes.ACC_SYNTHETIC | Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, new Method(defaultImplMethod.getName() + JvmAbi.DEFAULT_IMPLS_DELEGATE_SUFFIX, defaultImplMethod.getDescriptor()), element, JvmDeclarationOrigin.NO_ORIGIN, state, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter adapter) {
            Method interfaceMethod = typeMapper.mapAsmMethod(functionDescriptor, OwnerKind.IMPLEMENTATION);
            Type type = typeMapper.mapOwner(functionDescriptor);
            generateDelegateToMethodBody(-1, adapter, interfaceMethod, type.getInternalName(), Opcodes.INVOKESPECIAL, true);
            return null;
        }
    });
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) Method(org.jetbrains.org.objectweb.asm.commons.Method) Unit(kotlin.Unit)

Example 9 with Method

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

the class ImplementationBodyCodegen method genPropertyOnStack.

public Type genPropertyOnStack(InstructionAdapter iv, MethodContext context, @NotNull PropertyDescriptor propertyDescriptor, Type classAsmType, int index) {
    iv.load(index, classAsmType);
    if (couldUseDirectAccessToProperty(propertyDescriptor, /* forGetter = */
    true, /* isDelegated = */
    false, context, state.getShouldInlineConstVals())) {
        Type type = typeMapper.mapType(propertyDescriptor.getType());
        String fieldName = ((FieldOwnerContext) context.getParentContext()).getFieldName(propertyDescriptor, false);
        iv.getfield(classAsmType.getInternalName(), fieldName, type.getDescriptor());
        return type;
    } else {
        //noinspection ConstantConditions
        Method method = typeMapper.mapAsmMethod(propertyDescriptor.getGetter());
        iv.invokevirtual(classAsmType.getInternalName(), method.getName(), method.getDescriptor(), false);
        return method.getReturnType();
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) Method(org.jetbrains.org.objectweb.asm.commons.Method)

Example 10 with Method

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

the class FunctionCodegen method generateDefaultIfNeeded.

void generateDefaultIfNeeded(@NotNull MethodContext owner, @NotNull FunctionDescriptor functionDescriptor, @NotNull OwnerKind kind, @NotNull DefaultParameterValueLoader loadStrategy, @Nullable KtNamedFunction function) {
    DeclarationDescriptor contextClass = owner.getContextDescriptor().getContainingDeclaration();
    if (isInterface(contextClass) && !processInterface(contextClass, kind, state)) {
        return;
    }
    if (!isDefaultNeeded(functionDescriptor)) {
        return;
    }
    // $default methods are never private to be accessible from other class files (e.g. inner) without the need of synthetic accessors
    // $default methods are never protected to be accessible from subclass nested classes
    int visibilityFlag = Visibilities.isPrivate(functionDescriptor.getVisibility()) || isInlineOnlyOrReifiable(functionDescriptor) ? AsmUtil.NO_FLAG_PACKAGE_PRIVATE : Opcodes.ACC_PUBLIC;
    int flags = visibilityFlag | getDeprecatedAccessFlag(functionDescriptor) | ACC_SYNTHETIC;
    if (!(functionDescriptor instanceof ConstructorDescriptor)) {
        flags |= ACC_STATIC | ACC_BRIDGE;
    }
    Method defaultMethod = typeMapper.mapDefaultMethod(functionDescriptor, kind);
    MethodVisitor mv = v.newMethod(JvmDeclarationOriginKt.Synthetic(function, functionDescriptor), flags, defaultMethod.getName(), defaultMethod.getDescriptor(), null, getThrownExceptions(functionDescriptor, typeMapper));
    // Only method annotations are copied to the $default method. Parameter annotations are not copied until there are valid use cases;
    // enum constructors have two additional synthetic parameters which somewhat complicate this task
    AnnotationCodegen.forMethod(mv, memberCodegen, typeMapper).genAnnotations(functionDescriptor, defaultMethod.getReturnType());
    if (state.getClassBuilderMode().generateBodies) {
        if (this.owner instanceof MultifileClassFacadeContext) {
            mv.visitCode();
            generateFacadeDelegateMethodBody(mv, defaultMethod, (MultifileClassFacadeContext) this.owner);
            endVisit(mv, "default method delegation", getSourceFromDescriptor(functionDescriptor));
        } else {
            mv.visitCode();
            generateDefaultImplBody(owner, functionDescriptor, mv, loadStrategy, function, memberCodegen, defaultMethod);
            endVisit(mv, "default method", getSourceFromDescriptor(functionDescriptor));
        }
    }
}
Also used : Method(org.jetbrains.org.objectweb.asm.commons.Method) TraceMethodVisitor(org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor)

Aggregations

Method (org.jetbrains.org.objectweb.asm.commons.Method)25 KotlinType (org.jetbrains.kotlin.types.KotlinType)11 Type (org.jetbrains.org.objectweb.asm.Type)9 NotNull (org.jetbrains.annotations.NotNull)7 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)5 Unit (kotlin.Unit)3 JvmMethodGenericSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature)2 JvmMethodSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature)2 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)2 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)2 TraceMethodVisitor (org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor)2 InstrumentationClassFinder (com.intellij.compiler.instrumentation.InstrumentationClassFinder)1 PsiElement (com.intellij.psi.PsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 SupportCode (com.intellij.uiDesigner.core.SupportCode)1 FontDescriptor (com.intellij.uiDesigner.lw.FontDescriptor)1 LwComponent (com.intellij.uiDesigner.lw.LwComponent)1 StringDescriptor (com.intellij.uiDesigner.lw.StringDescriptor)1 ArrayList (java.util.ArrayList)1 Bridge (org.jetbrains.kotlin.backend.common.bridges.Bridge)1