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.ClassWriter; 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 arguments; RefType classThatHasTheMethodIfNotThis; RefType thisClass; public MethodCallStatementExpression(String methodName, List 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 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(new ClassWriter(ClassWriter.COMPUTE_FRAMES)); } 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); } }