Search in sources :

Example 1 with EnumValue

use of org.jetbrains.kotlin.resolve.constants.EnumValue in project kotlin by JetBrains.

the class CodegenAnnotatingVisitor method visitWhenExpression.

@Override
public void visitWhenExpression(@NotNull KtWhenExpression expression) {
    super.visitWhenExpression(expression);
    if (!isWhenWithEnums(expression))
        return;
    String currentClassName = getCurrentTopLevelClassOrPackagePartInternalName(expression.getContainingKtFile());
    if (bindingContext.get(MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, currentClassName) == null) {
        bindingTrace.record(MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, currentClassName, new ArrayList<WhenByEnumsMapping>(1));
    }
    List<WhenByEnumsMapping> mappings = bindingContext.get(MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, currentClassName);
    assert mappings != null : "guaranteed by contract";
    int fieldNumber = mappings.size();
    assert expression.getSubjectExpression() != null : "subject expression should be not null in a valid when by enums";
    KotlinType type = WhenChecker.whenSubjectType(expression, bindingContext);
    assert type != null : "should not be null in a valid when by enums";
    ClassDescriptor classDescriptor = (ClassDescriptor) type.getConstructor().getDeclarationDescriptor();
    assert classDescriptor != null : "because it's enum";
    WhenByEnumsMapping mapping = new WhenByEnumsMapping(classDescriptor, currentClassName, fieldNumber);
    for (ConstantValue<?> constant : SwitchCodegenUtil.getAllConstants(expression, bindingContext, shouldInlineConstVals)) {
        if (constant instanceof NullValue)
            continue;
        assert constant instanceof EnumValue : "expression in when should be EnumValue";
        mapping.putFirstTime((EnumValue) constant, mapping.size() + 1);
    }
    mappings.add(mapping);
    bindingTrace.record(MAPPING_FOR_WHEN_BY_ENUM, expression, mapping);
}
Also used : WhenByEnumsMapping(org.jetbrains.kotlin.codegen.when.WhenByEnumsMapping) NullValue(org.jetbrains.kotlin.resolve.constants.NullValue) EnumValue(org.jetbrains.kotlin.resolve.constants.EnumValue) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 2 with EnumValue

use of org.jetbrains.kotlin.resolve.constants.EnumValue 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)

Aggregations

EnumValue (org.jetbrains.kotlin.resolve.constants.EnumValue)2 Map (java.util.Map)1 WhenByEnumsMapping (org.jetbrains.kotlin.codegen.when.WhenByEnumsMapping)1 NullValue (org.jetbrains.kotlin.resolve.constants.NullValue)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 Type (org.jetbrains.org.objectweb.asm.Type)1