package st; import java.util.ArrayList; import heat.TypecheckException; import misc.*; public class ClassInstance extends AbstractInstance { private ClassInstance ext; // the name of the extended class (null if none) private ArrayList attrs; // the list of class-fields private ArrayList mtds; // the list of methods public ClassInstance(String name) { super(name, TypeEnum.classname); this.attrs = new ArrayList<>(); this.mtds = new ArrayList<>(); } @Override public boolean equals(Object other) { ClassInstance o; return (other instanceof ClassInstance && ((o = (ClassInstance) other).getName() == this.getName())); } public boolean equalsOnExtend(Object other) { ClassInstance o; if (other instanceof ClassInstance) o = (ClassInstance) other; else return false; if (o.getName().equals(this.getName())) { MinimalLogger.info(String.format("Found extension end.")); if (o.getExtend() != null && o.getExtend().equals(this.getExtend())) throw new TypecheckException(String.format("Recursive extension on %s detected", this.toString())); return true; } else MinimalLogger.info(String.format("I (%s) do not have the same name as %s... Checking if I extend...", this.getName(), o.getName())); if (this.getExtend() == null) { MinimalLogger.info(String.format("I (%s) do not extend anything!", this.getName())); return false; } ClassInstance te = this.getExtend(); if (te.equalsOnExtend(o)) { MinimalLogger.info(String.format("I (%s) extend %s!", this.getName(), o.getName())); return true; } MinimalLogger.info(String.format("I (%s) do not extend %s! I extend %s instead.", this.getName(), o.getName(), te.getName())); return false; } public ClassInstance getExtend() { /** * Returns the parent class, or * `null' if unset. */ return this.ext; } public ArrayList getLocals() { return this.attrs; } public ArrayList getMethods() { return this.mtds; } public int getSize() { return 4 * (this.attrs.size() + this.mtds.size()); } protected void addLocal(TypeInstance attr) { this.attrs.add(attr); } protected void addMethod(MethodInstance mtd) { this.mtds.add(mtd); } protected void setExtend(ClassInstance ext) { if (this.ext != null) throw new RuntimeException("setExtend: Attempted to set extended class twice!"); this.ext = ext; } }