refactor visitCallExpression() in preparation for supporting calls in pattern matching

This commit is contained in:
Dmitry Jemerov
2011-07-06 16:02:25 +02:00
parent 7adb2d1381
commit d824997dcf
@@ -742,13 +742,8 @@ public class ExpressionCodegen extends JetVisitor {
@Override @Override
public void visitCallExpression(JetCallExpression expression) { public void visitCallExpression(JetCallExpression expression) {
JetExpression callee = expression.getCalleeExpression(); final JetExpression callee = expression.getCalleeExpression();
DeclarationDescriptor funDescriptor = resolveCalleeDescriptor(expression);
if (callee instanceof JetSimpleNameExpression) {
DeclarationDescriptor funDescriptor = bindingContext.resolveReferenceExpression((JetSimpleNameExpression) callee);
if (funDescriptor == null) {
throw new CompilationException("Cannot resolve: " + callee.getText());
}
if (funDescriptor instanceof ConstructorDescriptor) { if (funDescriptor instanceof ConstructorDescriptor) {
generateConstructorCall(expression, (JetSimpleNameExpression) callee); generateConstructorCall(expression, (JetSimpleNameExpression) callee);
@@ -765,25 +760,8 @@ public class ExpressionCodegen extends JetVisitor {
} }
} }
PsiElement declarationPsiElement = bindingContext.getDeclarationPsiElement(funDescriptor);
final FunctionDescriptor fd = (FunctionDescriptor) funDescriptor; final FunctionDescriptor fd = (FunctionDescriptor) funDescriptor;
if (declarationPsiElement == null && isClass(functionParent, "String")) { PsiElement declarationPsiElement = resolveCalleeToDeclaration(funDescriptor);
final Project project = expression.getProject();
PsiClass jlString = JavaPsiFacade.getInstance(project).findClass("java.lang.String",
ProjectScope.getAllScope(project));
// TODO better overload mapping
final PsiMethod[] methods = jlString.findMethodsByName(funDescriptor.getName(), false);
final int arity = fd.getValueParameters().size();
for (PsiMethod method : methods) {
if (method.getParameterList().getParametersCount() == arity) {
declarationPsiElement = method;
}
}
}
if (declarationPsiElement == null) {
throw new UnsupportedOperationException("couldn't find declaration for " + funDescriptor);
}
CallableMethod callableMethod; CallableMethod callableMethod;
if (declarationPsiElement instanceof PsiMethod) { if (declarationPsiElement instanceof PsiMethod) {
@@ -827,13 +805,42 @@ public class ExpressionCodegen extends JetVisitor {
} }
} }
else { else {
throw new CompilationException(); throw new UnsupportedOperationException("unknown type of callee descriptor: " + funDescriptor);
} }
} }
else {
throw new UnsupportedOperationException("Don't know how to generate a call"); private PsiElement resolveCalleeToDeclaration(DeclarationDescriptor funDescriptor) {
PsiElement declarationPsiElement = bindingContext.getDeclarationPsiElement(funDescriptor);
if (declarationPsiElement == null && isClass(funDescriptor.getContainingDeclaration(), "String")) {
final Project project = state.getProject();
PsiClass jlString = JavaPsiFacade.getInstance(project).findClass("java.lang.String",
ProjectScope.getAllScope(project));
// TODO better overload mapping
final PsiMethod[] methods = jlString.findMethodsByName(funDescriptor.getName(), false);
final int arity = ((FunctionDescriptor) funDescriptor).getValueParameters().size();
for (PsiMethod method : methods) {
if (method.getParameterList().getParametersCount() == arity) {
declarationPsiElement = method;
} }
} }
}
if (declarationPsiElement == null) {
throw new UnsupportedOperationException("couldn't find declaration for " + funDescriptor);
}
return declarationPsiElement;
}
private DeclarationDescriptor resolveCalleeDescriptor(JetCallExpression call) {
JetExpression callee = call.getCalleeExpression();
if (!(callee instanceof JetSimpleNameExpression)) {
throw new UnsupportedOperationException("Don't know how to generate a call to " + callee);
}
DeclarationDescriptor funDescriptor = bindingContext.resolveReferenceExpression((JetSimpleNameExpression) callee);
if (funDescriptor == null) {
throw new CompilationException("Cannot resolve: " + callee.getText());
}
return funDescriptor;
}
private void invokeMethodWithArguments(CallableMethod callableMethod, JetCallExpression expression) { private void invokeMethodWithArguments(CallableMethod callableMethod, JetCallExpression expression) {
pushMethodArguments(expression, callableMethod.getValueParameterTypes()); pushMethodArguments(expression, callableMethod.getValueParameterTypes());