Minor: extract get*CompileTimeConstant
Get rid of code duplication.
This commit is contained in:
@@ -750,11 +750,21 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StackValue visitConstantExpression(@NotNull KtConstantExpression expression, StackValue receiver) {
|
public StackValue visitConstantExpression(@NotNull KtConstantExpression expression, StackValue receiver) {
|
||||||
ConstantValue<?> compileTimeValue = getPrimitiveOrStringCompileTimeConstant(expression, bindingContext, state.getShouldInlineConstVals());
|
ConstantValue<?> compileTimeValue = getPrimitiveOrStringCompileTimeConstant(expression);
|
||||||
assert compileTimeValue != null;
|
assert compileTimeValue != null;
|
||||||
return StackValue.constant(compileTimeValue.getValue(), expressionType(expression));
|
return StackValue.constant(compileTimeValue.getValue(), expressionType(expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public ConstantValue<?> getCompileTimeConstant(@NotNull KtExpression expression) {
|
||||||
|
return getCompileTimeConstant(expression, bindingContext, state.getShouldInlineConstVals());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public ConstantValue<?> getPrimitiveOrStringCompileTimeConstant(@NotNull KtExpression expression) {
|
||||||
|
return getPrimitiveOrStringCompileTimeConstant(expression, bindingContext, state.getShouldInlineConstVals());
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static ConstantValue<?> getPrimitiveOrStringCompileTimeConstant(
|
public static ConstantValue<?> getPrimitiveOrStringCompileTimeConstant(
|
||||||
@NotNull KtExpression expression,
|
@NotNull KtExpression expression,
|
||||||
@@ -910,8 +920,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
KtExpression entryExpression = entry.getExpression();
|
KtExpression entryExpression = entry.getExpression();
|
||||||
if (entryExpression == null) throw new AssertionError("No expression in " + entry);
|
if (entryExpression == null) throw new AssertionError("No expression in " + entry);
|
||||||
|
|
||||||
ConstantValue<?> compileTimeConstant =
|
ConstantValue<?> compileTimeConstant = getPrimitiveOrStringCompileTimeConstant(entryExpression);
|
||||||
getPrimitiveOrStringCompileTimeConstant(entryExpression, bindingContext, state.getShouldInlineConstVals());
|
|
||||||
|
|
||||||
if (compileTimeConstant != null && isConstantValueInlinableInStringTemplate(compileTimeConstant)) {
|
if (compileTimeConstant != null && isConstantValueInlinableInStringTemplate(compileTimeConstant)) {
|
||||||
constantValue.append(String.valueOf(compileTimeConstant.getValue()));
|
constantValue.append(String.valueOf(compileTimeConstant.getValue()));
|
||||||
@@ -2837,7 +2846,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
expression.getRight(), reference);
|
expression.getRight(), reference);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ConstantValue<?> compileTimeConstant = getPrimitiveOrStringCompileTimeConstant(expression, bindingContext, state.getShouldInlineConstVals());
|
ConstantValue<?> compileTimeConstant = getPrimitiveOrStringCompileTimeConstant(expression);
|
||||||
if (compileTimeConstant != null) {
|
if (compileTimeConstant != null) {
|
||||||
return StackValue.constant(compileTimeConstant.getValue(), expressionType(expression));
|
return StackValue.constant(compileTimeConstant.getValue(), expressionType(expression));
|
||||||
}
|
}
|
||||||
@@ -3120,7 +3129,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isIntZero(KtExpression expr, Type exprType) {
|
private boolean isIntZero(KtExpression expr, Type exprType) {
|
||||||
ConstantValue<?> exprValue = getPrimitiveOrStringCompileTimeConstant(expr, bindingContext, state.getShouldInlineConstVals());
|
ConstantValue<?> exprValue = getPrimitiveOrStringCompileTimeConstant(expr);
|
||||||
return isIntPrimitive(exprType) && exprValue != null && Integer.valueOf(0).equals(exprValue.getValue());
|
return isIntPrimitive(exprType) && exprValue != null && Integer.valueOf(0).equals(exprValue.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3249,7 +3258,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
public void invokeAppend(InstructionAdapter v, KtExpression expr) {
|
public void invokeAppend(InstructionAdapter v, KtExpression expr) {
|
||||||
expr = KtPsiUtil.safeDeparenthesize(expr);
|
expr = KtPsiUtil.safeDeparenthesize(expr);
|
||||||
|
|
||||||
ConstantValue<?> compileTimeConstant = getPrimitiveOrStringCompileTimeConstant(expr, bindingContext, state.getShouldInlineConstVals());
|
ConstantValue<?> compileTimeConstant = getPrimitiveOrStringCompileTimeConstant(expr);
|
||||||
|
|
||||||
if (compileTimeConstant == null) {
|
if (compileTimeConstant == null) {
|
||||||
if (expr instanceof KtBinaryExpression) {
|
if (expr instanceof KtBinaryExpression) {
|
||||||
@@ -3299,7 +3308,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StackValue visitPrefixExpression(@NotNull KtPrefixExpression expression, @NotNull StackValue receiver) {
|
public StackValue visitPrefixExpression(@NotNull KtPrefixExpression expression, @NotNull StackValue receiver) {
|
||||||
ConstantValue<?> compileTimeConstant = getPrimitiveOrStringCompileTimeConstant(expression, bindingContext, state.getShouldInlineConstVals());
|
ConstantValue<?> compileTimeConstant = getPrimitiveOrStringCompileTimeConstant(expression);
|
||||||
if (compileTimeConstant != null) {
|
if (compileTimeConstant != null) {
|
||||||
return StackValue.constant(compileTimeConstant.getValue(), expressionType(expression));
|
return StackValue.constant(compileTimeConstant.getValue(), expressionType(expression));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user