Search in sources :

Example 6 with Type

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

the class AsmTypes method getType.

@NotNull
public static Type getType(@NotNull Class<?> javaClass) {
    Type type = TYPES_MAP.get(javaClass);
    if (type == null) {
        type = Type.getType(javaClass);
        TYPES_MAP.put(javaClass, type);
    }
    return type;
}
Also used : PrimitiveType(org.jetbrains.kotlin.builtins.PrimitiveType) Type(org.jetbrains.org.objectweb.asm.Type) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with Type

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

the class MappingClassesForWhenByEnumCodegen method generateInitializationForMapping.

private void generateInitializationForMapping(@NotNull ClassBuilder cb, @NotNull InstructionAdapter v, @NotNull WhenByEnumsMapping mapping) {
    Type enumType = state.getTypeMapper().mapClass(mapping.getEnumClassDescriptor());
    v.invokestatic(enumType.getInternalName(), "values", Type.getMethodDescriptor(Type.getType("[" + enumType.getDescriptor())), false);
    v.arraylength();
    v.newarray(Type.INT_TYPE);
    v.putstatic(cb.getThisName(), mapping.getFieldName(), MAPPINGS_FIELD_DESCRIPTOR);
    for (Map.Entry<EnumValue, Integer> item : mapping.enumValuesToIntMapping()) {
        EnumValue enumEntry = item.getKey();
        int mappedValue = item.getValue();
        v.getstatic(cb.getThisName(), mapping.getFieldName(), MAPPINGS_FIELD_DESCRIPTOR);
        v.getstatic(enumType.getInternalName(), enumEntry.getValue().getName().asString(), enumType.getDescriptor());
        v.invokevirtual(enumType.getInternalName(), "ordinal", Type.getMethodDescriptor(Type.INT_TYPE), false);
        v.iconst(mappedValue);
        v.astore(Type.INT_TYPE);
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) EnumValue(org.jetbrains.kotlin.resolve.constants.EnumValue) Map(java.util.Map)

Example 8 with Type

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

the class MappingsClassesForWhenByEnum method generateMappingsClassForExpression.

public void generateMappingsClassForExpression(@NotNull KtWhenExpression expression) {
    WhenByEnumsMapping mapping = state.getBindingContext().get(CodegenBinding.MAPPING_FOR_WHEN_BY_ENUM, expression);
    assert mapping != null : "mapping class should not be requested for non enum when";
    if (!generatedMappingClasses.contains(mapping.getMappingsClassInternalName())) {
        List<WhenByEnumsMapping> mappings = state.getBindingContext().get(CodegenBinding.MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, mapping.getOuterClassInternalNameForExpression());
        assert mappings != null : "guaranteed by usage contract of EnumSwitchCodegen";
        Type mappingsClassType = Type.getObjectType(mapping.getMappingsClassInternalName());
        mappingsCodegen.generate(mappings, mappingsClassType, expression.getContainingKtFile());
        generatedMappingClasses.add(mapping.getMappingsClassInternalName());
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type)

Example 9 with Type

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

the class SwitchCodegenUtil method buildAppropriateSwitchCodegenIfPossible.

@Nullable
public static SwitchCodegen buildAppropriateSwitchCodegenIfPossible(@NotNull KtWhenExpression expression, boolean isStatement, boolean isExhaustive, @NotNull ExpressionCodegen codegen) {
    BindingContext bindingContext = codegen.getBindingContext();
    boolean shouldInlineConstVals = codegen.getState().getShouldInlineConstVals();
    if (!isThereConstantEntriesButNulls(expression, bindingContext, shouldInlineConstVals)) {
        return null;
    }
    Type subjectType = codegen.expressionType(expression.getSubjectExpression());
    WhenByEnumsMapping mapping = codegen.getBindingContext().get(CodegenBinding.MAPPING_FOR_WHEN_BY_ENUM, expression);
    if (mapping != null) {
        return new EnumSwitchCodegen(expression, isStatement, isExhaustive, codegen, mapping);
    }
    if (isIntegralConstantsSwitch(expression, subjectType, bindingContext, shouldInlineConstVals)) {
        return new IntegralConstantsSwitchCodegen(expression, isStatement, isExhaustive, codegen);
    }
    if (isStringConstantsSwitch(expression, subjectType, bindingContext, shouldInlineConstVals)) {
        return new StringSwitchCodegen(expression, isStatement, isExhaustive, codegen);
    }
    return null;
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with Type

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

the class ExpressionCodegen method generateInPrimitiveRange.

/*
     * Translates x in a..b to a <= x && x <= b
     * and x !in a..b to a > x || x > b for any primitive type
     */
private void generateInPrimitiveRange(StackValue argument, KtBinaryExpression rangeExpression, boolean isInverted) {
    Type rangeType = argument.type;
    int localVarIndex = myFrameMap.enterTemp(rangeType);
    // Load left bound
    gen(rangeExpression.getLeft(), rangeType);
    // Load x into local variable to avoid StackValue#put side-effects
    argument.put(rangeType, v);
    v.store(localVarIndex, rangeType);
    v.load(localVarIndex, rangeType);
    // If (x < left) goto L1
    Label l1 = new Label();
    emitGreaterThan(rangeType, l1);
    // If (x > right) goto L1
    v.load(localVarIndex, rangeType);
    gen(rangeExpression.getRight(), rangeType);
    emitGreaterThan(rangeType, l1);
    Label l2 = new Label();
    v.iconst(isInverted ? 0 : 1);
    v.goTo(l2);
    v.mark(l1);
    v.iconst(isInverted ? 1 : 0);
    v.mark(l2);
    myFrameMap.leaveTemp(rangeType);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Label(org.jetbrains.org.objectweb.asm.Label)

Aggregations

Type (org.jetbrains.org.objectweb.asm.Type)104 KotlinType (org.jetbrains.kotlin.types.KotlinType)66 IElementType (com.intellij.psi.tree.IElementType)45 NotNull (org.jetbrains.annotations.NotNull)23 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)16 Label (org.jetbrains.org.objectweb.asm.Label)12 Type.getObjectType (org.jetbrains.org.objectweb.asm.Type.getObjectType)10 Method (org.jetbrains.org.objectweb.asm.commons.Method)9 Unit (kotlin.Unit)8 LocalVariableDescriptor (org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)7 ArrayList (java.util.ArrayList)5 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)5 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)5 PrimitiveType (org.jetbrains.kotlin.builtins.PrimitiveType)4 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)4 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 ScriptDescriptor (org.jetbrains.kotlin.descriptors.ScriptDescriptor)3 InOut (com.intellij.codeInspection.bytecodeAnalysis.Direction.InOut)2 FunctionClassDescriptor (org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor)2