Refactor codegen of "in" expression

Use generateIn() twice, for binary expressions and for when clauses
This commit is contained in:
Alexander Udalov
2014-01-29 18:12:40 +04:00
parent 1832973491
commit 9b01f01e17
2 changed files with 14 additions and 37 deletions
@@ -2656,7 +2656,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
return generateElvis(expression); return generateElvis(expression);
} }
else if (opToken == JetTokens.IN_KEYWORD || opToken == JetTokens.NOT_IN) { else if (opToken == JetTokens.IN_KEYWORD || opToken == JetTokens.NOT_IN) {
return generateIn(expression); return generateIn(StackValue.expression(Type.INT_TYPE, expression.getLeft(), this), expression.getRight(), reference);
} }
else { else {
ResolvedCall<? extends CallableDescriptor> resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, reference); ResolvedCall<? extends CallableDescriptor> resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, reference);
@@ -2678,23 +2678,21 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
} }
} }
private StackValue generateIn(JetBinaryExpression expression) { private StackValue generateIn(StackValue leftValue, JetExpression rangeExpression, JetSimpleNameExpression operationReference) {
boolean inverted = expression.getOperationReference().getReferencedNameElementType() == JetTokens.NOT_IN; JetExpression deparenthesized = JetPsiUtil.deparenthesize(rangeExpression);
if (isIntRangeExpr(expression.getRight())) { if (isIntRangeExpr(deparenthesized)) {
StackValue leftValue = StackValue.expression(Type.INT_TYPE, expression.getLeft(), this); genInIntRange(leftValue, (JetBinaryExpression) deparenthesized);
JetBinaryExpression rangeExpression = (JetBinaryExpression) expression.getRight();
getInIntRange(leftValue, rangeExpression, inverted);
} }
else { else {
invokeFunctionByReference(expression.getOperationReference()); invokeFunctionByReference(operationReference);
if (inverted) { }
genInvertBoolean(v); if (operationReference.getReferencedNameElementType() == JetTokens.NOT_IN) {
} genInvertBoolean(v);
} }
return StackValue.onStack(Type.BOOLEAN_TYPE); return StackValue.onStack(Type.BOOLEAN_TYPE);
} }
private void getInIntRange(StackValue leftValue, JetBinaryExpression rangeExpression, boolean inverted) { private void genInIntRange(StackValue leftValue, JetBinaryExpression rangeExpression) {
v.iconst(1); v.iconst(1);
// 1 // 1
leftValue.put(Type.INT_TYPE, v); leftValue.put(Type.INT_TYPE, v);
@@ -2728,9 +2726,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
// c c // c c
v.and(Type.INT_TYPE); v.and(Type.INT_TYPE);
if (inverted) {
genInvertBoolean(v);
}
} }
private StackValue generateBooleanAnd(JetBinaryExpression expression) { private StackValue generateBooleanAnd(JetBinaryExpression expression) {
@@ -3746,27 +3741,9 @@ The "returned" value of try expression with no finally is either the last expres
private StackValue generateWhenCondition(Type subjectType, int subjectLocal, JetWhenCondition condition) { private StackValue generateWhenCondition(Type subjectType, int subjectLocal, JetWhenCondition condition) {
if (condition instanceof JetWhenConditionInRange) { if (condition instanceof JetWhenConditionInRange) {
JetWhenConditionInRange conditionInRange = (JetWhenConditionInRange) condition; JetWhenConditionInRange conditionInRange = (JetWhenConditionInRange) condition;
JetExpression rangeExpression = conditionInRange.getRangeExpression(); return generateIn(StackValue.local(subjectLocal, subjectType),
while (rangeExpression instanceof JetParenthesizedExpression) { conditionInRange.getRangeExpression(),
rangeExpression = ((JetParenthesizedExpression) rangeExpression).getExpression(); conditionInRange.getOperationReference());
}
JetSimpleNameExpression operationReference = conditionInRange.getOperationReference();
boolean inverted = operationReference.getReferencedNameElementType() == JetTokens.NOT_IN;
if (isIntRangeExpr(rangeExpression)) {
getInIntRange(new StackValue.Local(subjectLocal, subjectType), (JetBinaryExpression) rangeExpression, inverted);
}
else {
//FunctionDescriptor op =
// (FunctionDescriptor) bindingContext.get(BindingContext.REFERENCE_TARGET, conditionInRange.getOperationReference());
//genToJVMStack(rangeExpression);
//new StackValue.Local(subjectLocal, subjectType).put(OBJECT_TYPE, v);
//invokeFunctionNoParams(op, Type.BOOLEAN_TYPE, v);
invokeFunctionByReference(operationReference);
if (inverted) {
genInvertBoolean(v);
}
}
return StackValue.onStack(Type.BOOLEAN_TYPE);
} }
StackValue.Local match = subjectLocal == -1 ? null : StackValue.local(subjectLocal, subjectType); StackValue.Local match = subjectLocal == -1 ? null : StackValue.local(subjectLocal, subjectType);
if (condition instanceof JetWhenConditionIsPattern) { if (condition instanceof JetWhenConditionIsPattern) {
@@ -366,7 +366,7 @@ public abstract class StackValue {
public static class Local extends StackValue { public static class Local extends StackValue {
final int index; final int index;
public Local(int index, Type type) { private Local(int index, Type type) {
super(type); super(type);
this.index = index; this.index = index;