support for calls in pattern matching
This commit is contained in:
@@ -819,10 +819,14 @@ public class ExpressionCodegen extends JetVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void invokeMethodWithArguments(CallableMethod callableMethod, JetCallExpression expression) {
|
private void invokeMethodWithArguments(CallableMethod callableMethod, JetCallExpression expression) {
|
||||||
|
invokeMethodWithArguments(callableMethod, expression, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void invokeMethodWithArguments(CallableMethod callableMethod, JetCallExpression expression, final boolean haveReceiver) {
|
||||||
if (callableMethod.isOwnerFromCall()) {
|
if (callableMethod.isOwnerFromCall()) {
|
||||||
setOwnerFromCall(callableMethod, expression);
|
setOwnerFromCall(callableMethod, expression);
|
||||||
}
|
}
|
||||||
if (callableMethod.needsReceiverOnStack()) {
|
if (callableMethod.needsReceiverOnStack() && !haveReceiver) {
|
||||||
ensureReceiverOnStack(expression, callableMethod.getReceiverClass());
|
ensureReceiverOnStack(expression, callableMethod.getReceiverClass());
|
||||||
}
|
}
|
||||||
pushMethodArguments(expression, callableMethod.getValueParameterTypes());
|
pushMethodArguments(expression, callableMethod.getValueParameterTypes());
|
||||||
@@ -1793,6 +1797,23 @@ public class ExpressionCodegen extends JetVisitor {
|
|||||||
conditionValue = generatePatternMatch(pattern, patternCondition.isNegated(),
|
conditionValue = generatePatternMatch(pattern, patternCondition.isNegated(),
|
||||||
StackValue.local(subjectLocal, subjectType));
|
StackValue.local(subjectLocal, subjectType));
|
||||||
}
|
}
|
||||||
|
else if (condition instanceof JetWhenConditionCall) {
|
||||||
|
final JetExpression call = ((JetWhenConditionCall) condition).getCallSuffixExpression();
|
||||||
|
if (call instanceof JetCallExpression) {
|
||||||
|
v.load(subjectLocal, subjectType);
|
||||||
|
final DeclarationDescriptor declarationDescriptor = resolveCalleeDescriptor((JetCallExpression) call);
|
||||||
|
final PsiElement declaration = resolveCalleeToDeclaration(declarationDescriptor);
|
||||||
|
final CallableMethod callableMethod = typeMapper.mapToCallableMethod((PsiNamedElement) declaration);
|
||||||
|
if (callableMethod.getSignature().getReturnType() != Type.BOOLEAN_TYPE) {
|
||||||
|
throw new UnsupportedOperationException("calls in pattern matching must return boolean");
|
||||||
|
}
|
||||||
|
invokeMethodWithArguments(callableMethod, (JetCallExpression) call, true);
|
||||||
|
conditionValue = StackValue.onStack(Type.BOOLEAN_TYPE);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new UnsupportedOperationException("unsupported kind of call suffix");
|
||||||
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
throw new UnsupportedOperationException("unsupported kind of when condition");
|
throw new UnsupportedOperationException("unsupported kind of when condition");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,4 +69,11 @@ public class PatternMatchingTest extends CodegenTestCase {
|
|||||||
assertEquals("one,two", foo.invoke(null, new Tuple2<Integer, Integer>(1, 2)));
|
assertEquals("one,two", foo.invoke(null, new Tuple2<Integer, Integer>(1, 2)));
|
||||||
assertEquals("something", foo.invoke(null, new Tuple2<String, String>("not", "tuple")));
|
assertEquals("something", foo.invoke(null, new Tuple2<String, String>("not", "tuple")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testCall() throws Exception {
|
||||||
|
loadText("fun foo(s: String) = when(s) { .startsWith(\"J\") => \"JetBrains\"; else => \"something\" }");
|
||||||
|
Method foo = generateFunction();
|
||||||
|
assertEquals("JetBrains", foo.invoke(null, "Java"));
|
||||||
|
assertEquals("something", foo.invoke(null, "C#"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user