diff options
-rw-r--r-- | heat/HeatVisitor.java | 20 | ||||
-rw-r--r-- | output/negative/TrickyOverload-error.java (renamed from output/negative/Overwrite-error1.java) | 4 | ||||
-rw-r--r-- | st/ClassInstance.java | 26 | ||||
-rw-r--r-- | st/SymbolTable.java | 57 |
4 files changed, 79 insertions, 28 deletions
diff --git a/heat/HeatVisitor.java b/heat/HeatVisitor.java index 9886fdc..2e830c5 100644 --- a/heat/HeatVisitor.java +++ b/heat/HeatVisitor.java @@ -249,10 +249,9 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,ArrayList<TypeBundle>> // NOT // if it is a class and the classes are equal // or it is not a class and the types are equal - if (!((tb.getInstance() != null && tb.getInstance().equals(_ret.getInstance())) || - (tb.getInstance() == null && tb.getType() == _ret.getType()))) - throw new TypecheckException(String.format("%s returns the wrong type!", - id)); + if (!tb.equals(_ret)) + throw new TypecheckException(String.format("%s in %s returns the wrong type!", + id, symt.getActive(TypeEnum.classname))); /////////////////////////////////////////////////////////////// this.symt.removeActive(TypeEnum.method); MinimalLogger.info(String.format("<- %s (%s)", @@ -327,9 +326,14 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,ArrayList<TypeBundle>> MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// - if (n.f0.which == 3) - _ret = new TypeBundle(TypeEnum.classname, this.symt.getClass(((Identifier) n.f0.choice).f0.tokenImage)); - else + if (n.f0.which == 3) { + String id = ((Identifier) n.f0.choice).f0.tokenImage; + ClassInstance c = this.symt.getClass(id); + if (c == null) + throw new TypecheckException(String.format("%s was never declared!", + id)); + _ret = new TypeBundle(TypeEnum.classname, c); + } else _ret = n.f0.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", @@ -820,6 +824,8 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,ArrayList<TypeBundle>> n.getClass().getSimpleName(), t.getName())); + MinimalLogger.severe(String.format("Class instance was: %s", t.getClassInstance())); + MinimalLogger.severe(String.format("M was: %s with args %s", m, m.getParentClass())); ArrayList<TypeBundle> actual = new ArrayList<TypeBundle>(); ArrayList<TypeBundle> expected = new ArrayList<TypeBundle>(); n.f4.accept(this, actual); diff --git a/output/negative/Overwrite-error1.java b/output/negative/TrickyOverload-error.java index 89b11a2..d6b7d96 100644 --- a/output/negative/Overwrite-error1.java +++ b/output/negative/TrickyOverload-error.java @@ -1,10 +1,11 @@ -class MoreThan4{ +class TrickyOverload{ public static void main(String[] a){ System.out.println(new MT4().Start(1,2,3,4,5,6)); } } class MT4 { + int x ; public int Start(int p1, int p2, int p3 , int p4, int p5, int p6){ int aux ; System.out.println(p1); @@ -29,6 +30,7 @@ class MT4 { } class MT42 extends MT4 { + int[] x ; public int Start(int p1, int p2, int p3 , int p4, int p5){ int aux ; System.out.println(p1); diff --git a/st/ClassInstance.java b/st/ClassInstance.java index d96d3d5..a794f96 100644 --- a/st/ClassInstance.java +++ b/st/ClassInstance.java @@ -1,6 +1,7 @@ package st; import java.util.ArrayList; +import misc.*; public class ClassInstance extends AbstractInstance { private ClassInstance ext; // the name of the extended class (null if none) @@ -15,8 +16,29 @@ public class ClassInstance extends AbstractInstance { @Override public boolean equals(Object other) { ClassInstance o; - return (other instanceof ClassInstance && - ((o = (ClassInstance) other).getName() == this.getName())); + if (other instanceof ClassInstance) + o = (ClassInstance) other; + else + return false; + + if (o.getName().equals(this.getName())) + 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.equals(o)) { + MinimalLogger.info(String.format("I (%s) extends %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() { diff --git a/st/SymbolTable.java b/st/SymbolTable.java index fa6bdf2..ca38295 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -56,27 +56,48 @@ public class SymbolTable { cls.getName(), ext.getName())); cls.setExtend(ext); + TokenKey k; for (TypeInstance t : ext.getLocals()) { - MinimalLogger.info(String.format("Added %s (%s) as a local var of %s (%s)", - t.getName(), t.getType(), - cls.getName(), cls.getType())); - cls.addLocal(t); - this.symt.put(new TokenKey(t.getName(), - TypeEnum.integer, - cls, - null), - t); + k = new TokenKey(t.getName(), TypeEnum.integer, + cls, null); + if (this.symt.get(k) == null) { + MinimalLogger.info(String.format("Added %s (%s) as a local var of %s (%s)", + t.getName(), t.getType(), + cls.getName(), cls.getType())); + cls.addLocal(t); + this.symt.put(k, t); + } else { MinimalLogger.info(String.format("%s found to be overridden in %s.", + t.getName(), cls.getName())); } } for (MethodInstance m : ext.getMethods()) { - MinimalLogger.info(String.format("Added %s (%s) as a method of %s (%s)", - m.getName(), m.getType(), - cls.getName(), cls.getType())); - this.symt.put(new TokenKey(m.getName(), - TypeEnum.method, - cls, - null), - m); - cls.addMethod(m); + k = new TokenKey(m.getName(), TypeEnum.method, + cls, null); + if (this.symt.get(k) == null) { + MinimalLogger.info(String.format("Added %s (%s) as a method of %s (%s)", + m.getName(), m.getType(), + cls.getName(), cls.getType())); + this.symt.put(k, m); + cls.addMethod(m); + } else { + MethodInstance exist = (MethodInstance) this.symt.get(k); + ArrayList<TypeInstance> expected = exist.getArguments(); + ArrayList<TypeInstance> actual = m.getArguments(); + if (expected.size() != actual.size() || + !exist.getReturn().equals(m.getReturn())) + throw new TypecheckException(String.format("SymbolTable found that %s is overwritten in %s!", + m.getName(), + cls.getName())); + for (int i = 0; i < actual.size(); ++i) { + if (expected.get(i).getClassInstance() != actual.get(i).getClassInstance()) { + throw new TypecheckException(String.format("SymbolTable found that %s is overwritten in %s!", + m.getName(), + cls.getName())); + } + } + + MinimalLogger.info(String.format("%s found to be overridden in %s.", + m.getName(), cls.getName())); + } } } |