64 lines
1.9 KiB
Java
64 lines
1.9 KiB
Java
package abstractSyntaxTree.StatementExpression;
|
|
|
|
import TypeCheck.AbstractType;
|
|
import TypeCheck.TypeCheckResult;
|
|
import abstractSyntaxTree.Class.MethodDecl;
|
|
import abstractSyntaxTree.Class.RefType;
|
|
import abstractSyntaxTree.Expression.IExpression;
|
|
import abstractSyntaxTree.Statement.IStatement;
|
|
import org.objectweb.asm.MethodVisitor;
|
|
import org.objectweb.asm.Opcodes;
|
|
|
|
import java.util.List;
|
|
|
|
public class MethodCallStatementExpression extends AbstractType implements IExpression, IStatement {
|
|
String methodName;
|
|
List<IExpression> arguments;
|
|
RefType classThatHasTheMethodIfNotThis;
|
|
RefType thisClass;
|
|
|
|
public MethodCallStatementExpression(String methodName, List<IExpression> arguments) {
|
|
this.methodName = methodName;
|
|
this.arguments = arguments;
|
|
}
|
|
|
|
@Override
|
|
public TypeCheckResult typeCheck() throws Exception {
|
|
TypeCheckResult result = new TypeCheckResult();
|
|
|
|
RefType searchMethodHere;
|
|
if(classThatHasTheMethodIfNotThis == null){
|
|
searchMethodHere = thisClass;
|
|
} else {
|
|
searchMethodHere = classThatHasTheMethodIfNotThis;
|
|
}
|
|
|
|
List<MethodDecl> methods = searchMethodHere.methodDecls;
|
|
|
|
if(!methods.contains(methodName)){
|
|
throw new Exception("method not found");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
//Errors occur due to the change in parameter in the RefType class
|
|
@Override
|
|
public void CodeGen(MethodVisitor mv) throws Exception {
|
|
//Generate Bytecode for the receiver
|
|
if(classThatHasTheMethodIfNotThis != null){
|
|
classThatHasTheMethodIfNotThis.codeGen();
|
|
} else {
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
|
}
|
|
|
|
for (IExpression argument : arguments) {
|
|
argument.codeGen(mv);
|
|
}
|
|
|
|
//We need the class reference and the return type of the method
|
|
//mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, return type);
|
|
|
|
}
|
|
}
|