Search in sources :

Example 1 with ConstantValue

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

the class CompileTimeConstantUtils method canBeReducedToBooleanConstant.

public static boolean canBeReducedToBooleanConstant(@Nullable KtExpression expression, @NotNull BindingContext context, @Nullable Boolean expectedValue) {
    KtExpression effectiveExpression = KtPsiUtil.deparenthesize(expression);
    if (effectiveExpression == null)
        return false;
    CompileTimeConstant<?> compileTimeConstant = ConstantExpressionEvaluator.getConstant(effectiveExpression, context);
    if (!(compileTimeConstant instanceof TypedCompileTimeConstant) || compileTimeConstant.getUsesVariableAsConstant())
        return false;
    ConstantValue constantValue = ((TypedCompileTimeConstant) compileTimeConstant).getConstantValue();
    if (!(constantValue instanceof BooleanValue))
        return false;
    Boolean value = ((BooleanValue) constantValue).getValue();
    return expectedValue == null || expectedValue.equals(value);
}
Also used : BooleanValue(org.jetbrains.kotlin.resolve.constants.BooleanValue) KtExpression(org.jetbrains.kotlin.psi.KtExpression) TypedCompileTimeConstant(org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant) ConstantValue(org.jetbrains.kotlin.resolve.constants.ConstantValue)

Example 2 with ConstantValue

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

the class ExpressionCodegen method visitBinaryExpression.

@Override
public StackValue visitBinaryExpression(@NotNull KtBinaryExpression expression, @NotNull StackValue receiver) {
    KtSimpleNameExpression reference = expression.getOperationReference();
    IElementType opToken = reference.getReferencedNameElementType();
    if (opToken == KtTokens.EQ) {
        return generateAssignmentExpression(expression);
    } else if (KtTokens.AUGMENTED_ASSIGNMENTS.contains(opToken)) {
        return generateAugmentedAssignment(expression);
    } else if (opToken == KtTokens.ANDAND) {
        return generateBooleanAnd(expression);
    } else if (opToken == KtTokens.OROR) {
        return generateBooleanOr(expression);
    } else if (opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ || opToken == KtTokens.EQEQEQ || opToken == KtTokens.EXCLEQEQEQ) {
        return generateEquals(expression.getLeft(), expression.getRight(), opToken);
    } else if (opToken == KtTokens.LT || opToken == KtTokens.LTEQ || opToken == KtTokens.GT || opToken == KtTokens.GTEQ) {
        return generateComparison(expression, receiver);
    } else if (opToken == KtTokens.ELVIS) {
        return generateElvis(expression);
    } else if (opToken == KtTokens.IN_KEYWORD || opToken == KtTokens.NOT_IN) {
        return generateIn(StackValue.expression(expressionType(expression.getLeft()), expression.getLeft(), this), expression.getRight(), reference);
    } else {
        ConstantValue<?> compileTimeConstant = getPrimitiveOrStringCompileTimeConstant(expression, bindingContext, state.getShouldInlineConstVals());
        if (compileTimeConstant != null) {
            return StackValue.constant(compileTimeConstant.getValue(), expressionType(expression));
        }
        ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCallWithAssert(expression, bindingContext);
        FunctionDescriptor descriptor = (FunctionDescriptor) resolvedCall.getResultingDescriptor();
        if (descriptor instanceof ConstructorDescriptor) {
            return generateConstructorCall(resolvedCall, expressionType(expression));
        }
        return invokeFunction(resolvedCall, receiver);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) SamConstructorDescriptor(org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor) TypeAliasConstructorDescriptor(org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor) ConstantValue(org.jetbrains.kotlin.resolve.constants.ConstantValue)

Example 3 with ConstantValue

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

the class FunctionCodegen method getThrownExceptions.

@NotNull
public static String[] getThrownExceptions(@NotNull FunctionDescriptor function, @NotNull final KotlinTypeMapper mapper) {
    AnnotationDescriptor annotation = function.getAnnotations().findAnnotation(new FqName("kotlin.throws"));
    if (annotation == null) {
        annotation = function.getAnnotations().findAnnotation(new FqName("kotlin.jvm.Throws"));
    }
    if (annotation == null)
        return ArrayUtil.EMPTY_STRING_ARRAY;
    Collection<ConstantValue<?>> values = annotation.getAllValueArguments().values();
    if (values.isEmpty())
        return ArrayUtil.EMPTY_STRING_ARRAY;
    Object value = values.iterator().next();
    if (!(value instanceof ArrayValue))
        return ArrayUtil.EMPTY_STRING_ARRAY;
    ArrayValue arrayValue = (ArrayValue) value;
    List<String> strings = ContainerUtil.mapNotNull(arrayValue.getValue(), new Function<ConstantValue<?>, String>() {

        @Override
        public String fun(ConstantValue<?> constant) {
            if (constant instanceof KClassValue) {
                KClassValue classValue = (KClassValue) constant;
                ClassDescriptor classDescriptor = DescriptorUtils.getClassDescriptorForType(classValue.getValue());
                return mapper.mapClass(classDescriptor).getInternalName();
            }
            return null;
        }
    });
    return ArrayUtil.toStringArray(strings);
}
Also used : AnnotationDescriptor(org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor) JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) FqName(org.jetbrains.kotlin.name.FqName) KClassValue(org.jetbrains.kotlin.resolve.constants.KClassValue) ConstantValue(org.jetbrains.kotlin.resolve.constants.ConstantValue) ArrayValue(org.jetbrains.kotlin.resolve.constants.ArrayValue) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ConstantValue (org.jetbrains.kotlin.resolve.constants.ConstantValue)3 IElementType (com.intellij.psi.tree.IElementType)1 NotNull (org.jetbrains.annotations.NotNull)1 AnnotationDescriptor (org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor)1 TypeAliasConstructorDescriptor (org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor)1 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)1 SamConstructorDescriptor (org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor)1 FqName (org.jetbrains.kotlin.name.FqName)1 KtExpression (org.jetbrains.kotlin.psi.KtExpression)1 ArrayValue (org.jetbrains.kotlin.resolve.constants.ArrayValue)1 BooleanValue (org.jetbrains.kotlin.resolve.constants.BooleanValue)1 KClassValue (org.jetbrains.kotlin.resolve.constants.KClassValue)1 TypedCompileTimeConstant (org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant)1