Commit 9b68a48c by Tianqi Yang

feat(overload): add errors caused by overloads

Add errors: - AmbiguousNewDeclarationError: Ambiguous new declarations with the same parameter list but different return values - ConflictMainError: Multiple main functions - MethodCallAmbiguousError: Ambiguous method matches the method call - NoMatchMethodError: No method matches the call - RedefinitionError: Redefine the same function - VarOverrideMethodError: Try to override the method with a variable Add CommonUtils: - copyList: shallow copy a List
parent df2bb8d4
package decaf.error;
import java.util.Iterator;
import java.util.List;
import decaf.Location;
import decaf.symbol.Function;
import decaf.type.Type;
import decaf.utils.FuncUtils;
public class AmbiguousNewDeclarationError extends DecafError {
Function func;
Function prev;
public AmbiguousNewDeclarationError (Function func, Function prev, Location location) {
super(location);
this.func = func;
this.prev = prev;
}
@Override
protected String getErrMsg() {
String msg = "ambiguous new declaration of '" + func.getReturnType() + " " + FuncUtils.getArgListString(func.getName(), func.getType().getArgList(), func.isStatik()) + "' :\n";
msg += " *** note : previous declaration at " + prev.getLocation() + " : " + prev.getReturnType() + " " + FuncUtils.getArgListString(prev.getName(), prev.getType().getArgList(), prev.isStatik()) + "\n";
return msg.trim();
}
}
package decaf.error;
import java.util.List;
import decaf.Location;
import decaf.symbol.Function;
import decaf.utils.CommonUtils;
import decaf.utils.FuncUtils;
public class ConflictMainError extends DecafError {
Function func;
List<Function> prevList;
public ConflictMainError (Function func, List<Function> prevList, Location location) {
super(location);
this.func = func;
this.prevList = CommonUtils.copyList(prevList);
}
@Override
protected String getErrMsg() {
String msg = "conflicting declaration of main function '" + func.getReturnType() + " " + FuncUtils.getArgListString(func.getName(), func.getType().getArgList(), func.isStatik()) + "' :\n";
for (Function prev : prevList) {
msg += " *** note : previous declaration at " + prev.getLocation() + " : " + prev.getReturnType() + " " + FuncUtils.getArgListString(prev.getName(), prev.getType().getArgList(), prev.isStatik()) + "\n";
}
return msg.trim();
}
}
package decaf.error;
import java.util.List;
import decaf.Location;
import decaf.symbol.Function;
import decaf.type.Type;
import decaf.utils.CommonUtils;
import decaf.utils.FuncUtils;
public class MethodCallAmbiguousError extends DecafError {
String name;
List<Type> argList;
List<Function> functions;
public MethodCallAmbiguousError (String name, List<Type> argList, List<Function> functions, Location location) {
super(location);
this.name = name;
this.argList = CommonUtils.copyList(argList);
this.functions = CommonUtils.copyList(functions);
}
@Override
protected String getErrMsg() {
String msg = "call of overloaded " + FuncUtils.getArgListString(name, argList, true) + " is ambiguous, candidates are :\n";
for (Function func : functions) {
msg += " *** note : candidate at " + func.getLocation() + " : " + func.getReturnType() + " " + FuncUtils.getArgListString(func.getName(), func.getType().getArgList(), func.isStatik()) + "\n";
}
return msg.trim();
}
}
package decaf.error;
import java.util.Iterator;
import java.util.List;
import decaf.Location;
import decaf.symbol.Function;
import decaf.type.Type;
import decaf.utils.CommonUtils;
import decaf.utils.FuncUtils;
public class NoMatchMethodError extends DecafError {
String name;
List<Type> argList;
List<Function> functions;
List<DecafError> errors;
public NoMatchMethodError(String name, List<Type> argList, List<Function> functions, List<DecafError> errors, Location location) {
super(location);
this.name = name;
this.argList = CommonUtils.copyList(argList);
this.functions = CommonUtils.copyList(functions);
this.errors = CommonUtils.copyList(errors);
}
@Override
protected String getErrMsg() {
String msg = "no match function for call to " + FuncUtils.getArgListString(name, argList, true) + ", candidates are :\n";
Iterator<DecafError> iter = errors.iterator();
for (Function func : functions) {
DecafError error = iter.next();
msg += " *** note : candidate at " + func.getLocation() + " : " + func.getReturnType() + " " + FuncUtils.getArgListString(func.getName(), func.getType().getArgList(), func.isStatik()) + "\n";
msg += " error : " + error.getErrMsg() + "\n";
}
return msg.trim();
}
}
package decaf.error;
import decaf.Location;
import decaf.symbol.Function;
import decaf.utils.FuncUtils;
public class RedefinitionError extends DecafError {
Function func;
Function prev;
public RedefinitionError (Function func, Function prev, Location location) {
super(location);
this.func = func;
this.prev = prev;
}
@Override
protected String getErrMsg() {
String msg = "redefinition of '" + func.getReturnType() + " " + FuncUtils.getArgListString(func.getName(), func.getType().getArgList(), func.isStatik()) + "' :\n";
msg += " *** note : previous declaration at " + prev.getLocation() + " : " + prev.getReturnType() + " " + FuncUtils.getArgListString(prev.getName(), prev.getType().getArgList(), prev.isStatik()) + "\n";
return msg.trim();
}
}
package decaf.error;
import java.util.List;
import decaf.Location;
import decaf.symbol.Function;
import decaf.type.Type;
import decaf.utils.CommonUtils;
import decaf.utils.FuncUtils;
public class VarOverrideMethodError extends DecafError {
String name;
List<Function> functions;
public VarOverrideMethodError (String name, List<Function> functions, Location location) {
super(location);
this.name = name;
this.functions = CommonUtils.copyList(functions);
}
@Override
protected String getErrMsg() {
String msg = "declaration of '" + name
+ "' here conflicts with earlier declarations : \n";
for (Function func : functions) {
msg += " *** note : previously declared at " + func.getLocation() + " : " + func.getReturnType() + " " + FuncUtils.getArgListString(func.getName(), func.getType().getArgList(), func.isStatik()) + "\n";
}
return msg.trim();
}
}
package decaf.utils;
import java.util.ArrayList;
import java.util.List;
public final class CommonUtils
{
public static final <T> List<T> copyList (List<T> list)
{
List<T> result = new ArrayList<>();
result.addAll(list);
return result;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment