Search in sources :

Example 1 with NullValue

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

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

the class Translation method translateConstant.

@Nullable
public static JsExpression translateConstant(@NotNull CompileTimeConstant compileTimeValue, @NotNull KtExpression expression, @NotNull TranslationContext context) {
    KotlinType expectedType = context.bindingContext().getType(expression);
    ConstantValue<?> constant = compileTimeValue.toConstantValue(expectedType != null ? expectedType : TypeUtils.NO_EXPECTED_TYPE);
    if (constant instanceof NullValue) {
        return JsLiteral.NULL;
    }
    Object value = constant.getValue();
    if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
        return context.program().getNumberLiteral(((Number) value).intValue());
    } else if (value instanceof Long) {
        return JsAstUtils.newLong((Long) value, context);
    } else if (value instanceof Float) {
        float floatValue = (Float) value;
        double doubleValue;
        if (Float.isInfinite(floatValue) || Float.isNaN(floatValue)) {
            doubleValue = floatValue;
        } else {
            doubleValue = Double.parseDouble(Float.toString(floatValue));
        }
        return context.program().getNumberLiteral(doubleValue);
    } else if (value instanceof Number) {
        return context.program().getNumberLiteral(((Number) value).doubleValue());
    } else if (value instanceof Boolean) {
        return JsLiteral.getBoolean((Boolean) value);
    }
    //TODO: test
    if (value instanceof String) {
        return context.program().getStringLiteral((String) value);
    }
    if (value instanceof Character) {
        return context.program().getNumberLiteral(((Character) value).charValue());
    }
    return null;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) NullValue(org.jetbrains.kotlin.resolve.constants.NullValue) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with NullValue

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

the class SwitchCodegen method prepareConfiguration.

/**
     * Sets up transitionsTable and maybe something else needed in a special case
     * Behaviour may be changed by overriding processConstant
     */
private void prepareConfiguration() {
    for (KtWhenEntry entry : expression.getEntries()) {
        Label entryLabel = new Label();
        for (ConstantValue<?> constant : SwitchCodegenUtil.getConstantsFromEntry(entry, bindingContext, codegen.getState().getShouldInlineConstVals())) {
            if (constant instanceof NullValue)
                continue;
            processConstant(constant, entryLabel);
        }
        if (entry.isElse()) {
            elseLabel = entryLabel;
        }
        entryLabels.add(entryLabel);
    }
}
Also used : KtWhenEntry(org.jetbrains.kotlin.psi.KtWhenEntry) NullValue(org.jetbrains.kotlin.resolve.constants.NullValue) Label(org.jetbrains.org.objectweb.asm.Label)

Aggregations

NullValue (org.jetbrains.kotlin.resolve.constants.NullValue)3 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 Nullable (org.jetbrains.annotations.Nullable)1 WhenByEnumsMapping (org.jetbrains.kotlin.codegen.when.WhenByEnumsMapping)1 KtWhenEntry (org.jetbrains.kotlin.psi.KtWhenEntry)1 EnumValue (org.jetbrains.kotlin.resolve.constants.EnumValue)1 Label (org.jetbrains.org.objectweb.asm.Label)1