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);
}
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);
}
}
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);
}
Aggregations