one more step of CallableMethod refactoring
This commit is contained in:
@@ -10,7 +10,7 @@ import java.util.List;
|
|||||||
* @author yole
|
* @author yole
|
||||||
*/
|
*/
|
||||||
public class CallableMethod {
|
public class CallableMethod {
|
||||||
private final String owner;
|
private String owner;
|
||||||
private final Method signature;
|
private final Method signature;
|
||||||
private final int invokeOpcode;
|
private final int invokeOpcode;
|
||||||
private final List<Type> valueParameterTypes;
|
private final List<Type> valueParameterTypes;
|
||||||
@@ -26,6 +26,10 @@ public class CallableMethod {
|
|||||||
return owner;
|
return owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setOwner(String owner) {
|
||||||
|
this.owner = owner;
|
||||||
|
}
|
||||||
|
|
||||||
public Method getSignature() {
|
public Method getSignature() {
|
||||||
return signature;
|
return signature;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -782,7 +782,15 @@ public class ExpressionCodegen extends JetVisitor {
|
|||||||
}
|
}
|
||||||
Method methodDescriptor;
|
Method methodDescriptor;
|
||||||
if (declarationPsiElement instanceof PsiMethod) {
|
if (declarationPsiElement instanceof PsiMethod) {
|
||||||
methodDescriptor = generateJavaMethodCall(expression, (PsiMethod) declarationPsiElement);
|
final PsiMethod psiMethod = (PsiMethod) declarationPsiElement;
|
||||||
|
CallableMethod callableMethod = JetTypeMapper.mapToCallableMethod(psiMethod);
|
||||||
|
if (!psiMethod.hasModifierProperty(PsiModifier.STATIC)) {
|
||||||
|
ensureReceiverOnStack(expression, null);
|
||||||
|
setOwnerFromCall(callableMethod, expression);
|
||||||
|
}
|
||||||
|
pushMethodArguments(expression, callableMethod.getValueParameterTypes());
|
||||||
|
callableMethod.invoke(v);
|
||||||
|
methodDescriptor = callableMethod.getSignature();
|
||||||
}
|
}
|
||||||
else if (declarationPsiElement instanceof JetNamedFunction) {
|
else if (declarationPsiElement instanceof JetNamedFunction) {
|
||||||
final JetNamedFunction jetFunction = (JetNamedFunction) declarationPsiElement;
|
final JetNamedFunction jetFunction = (JetNamedFunction) declarationPsiElement;
|
||||||
@@ -832,33 +840,16 @@ public class ExpressionCodegen extends JetVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Method generateJavaMethodCall(JetCallExpression expression, PsiMethod psiMethod) {
|
private void setOwnerFromCall(CallableMethod callableMethod, JetCallExpression expression) {
|
||||||
final PsiClass containingClass = psiMethod.getContainingClass();
|
if (expression.getParent() instanceof JetQualifiedExpression) {
|
||||||
String owner = JetTypeMapper.jvmName(containingClass);
|
final JetExpression receiver = ((JetQualifiedExpression) expression.getParent()).getReceiverExpression();
|
||||||
Method methodDescriptor = JetTypeMapper.getMethodDescriptor(psiMethod);
|
JetType expressionType = bindingContext.getExpressionType(receiver);
|
||||||
final boolean isStatic = psiMethod.hasModifierProperty(PsiModifier.STATIC);
|
DeclarationDescriptor declarationDescriptor = expressionType.getConstructor().getDeclarationDescriptor();
|
||||||
|
PsiElement ownerDeclaration = bindingContext.getDeclarationPsiElement(declarationDescriptor);
|
||||||
if (!isStatic) {
|
if (ownerDeclaration instanceof PsiClass) {
|
||||||
ensureReceiverOnStack(expression, null);
|
callableMethod.setOwner(typeMapper.mapType(expressionType).getInternalName());
|
||||||
if (expression.getParent() instanceof JetQualifiedExpression) {
|
|
||||||
final JetExpression receiver = ((JetQualifiedExpression) expression.getParent()).getReceiverExpression();
|
|
||||||
JetType expressionType = bindingContext.getExpressionType(receiver);
|
|
||||||
DeclarationDescriptor declarationDescriptor = expressionType.getConstructor().getDeclarationDescriptor();
|
|
||||||
PsiElement ownerDeclaration = bindingContext.getDeclarationPsiElement(declarationDescriptor);
|
|
||||||
if (ownerDeclaration instanceof PsiClass) {
|
|
||||||
owner = typeMapper.mapType(expressionType).getInternalName();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pushMethodArguments(expression, methodDescriptor);
|
|
||||||
|
|
||||||
v.visitMethodInsn(isStatic
|
|
||||||
? Opcodes.INVOKESTATIC
|
|
||||||
: (containingClass.isInterface() ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL),
|
|
||||||
owner,
|
|
||||||
methodDescriptor.getName(),
|
|
||||||
methodDescriptor.getDescriptor());
|
|
||||||
return methodDescriptor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JetExpression getReceiverForSelector(JetElement expression) {
|
private JetExpression getReceiverForSelector(JetElement expression) {
|
||||||
|
|||||||
@@ -103,7 +103,8 @@ public class JetTypeMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static CallableMethod mapToCallableMethod(PsiMethod method) {
|
static CallableMethod mapToCallableMethod(PsiMethod method) {
|
||||||
String owner = jvmName(method.getContainingClass());
|
final PsiClass containingClass = method.getContainingClass();
|
||||||
|
String owner = jvmName(containingClass);
|
||||||
Method signature = getMethodDescriptor(method);
|
Method signature = getMethodDescriptor(method);
|
||||||
List<Type> valueParameterTypes = new ArrayList<Type>();
|
List<Type> valueParameterTypes = new ArrayList<Type>();
|
||||||
Collections.addAll(valueParameterTypes, signature.getArgumentTypes());
|
Collections.addAll(valueParameterTypes, signature.getArgumentTypes());
|
||||||
@@ -111,8 +112,14 @@ public class JetTypeMapper {
|
|||||||
if (method.isConstructor()) {
|
if (method.isConstructor()) {
|
||||||
opcode = Opcodes.INVOKESPECIAL;
|
opcode = Opcodes.INVOKESPECIAL;
|
||||||
}
|
}
|
||||||
|
else if (method.hasModifierProperty(PsiModifier.STATIC)) {
|
||||||
|
opcode = Opcodes.INVOKESTATIC;
|
||||||
|
}
|
||||||
|
else if (containingClass.isInterface()) {
|
||||||
|
opcode = Opcodes.INVOKEINTERFACE;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
throw new UnsupportedOperationException("TODO");
|
opcode = Opcodes.INVOKEVIRTUAL;
|
||||||
}
|
}
|
||||||
return new CallableMethod(owner, signature, opcode, valueParameterTypes);
|
return new CallableMethod(owner, signature, opcode, valueParameterTypes);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user