Search in sources :

Example 1 with WhenByEnumsMapping

use of org.jetbrains.kotlin.codegen.when.WhenByEnumsMapping 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)

Aggregations

WhenByEnumsMapping (org.jetbrains.kotlin.codegen.when.WhenByEnumsMapping)1 EnumValue (org.jetbrains.kotlin.resolve.constants.EnumValue)1 NullValue (org.jetbrains.kotlin.resolve.constants.NullValue)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1