From 58fa180a069cef9692f002cb5b5eb401d6c90d0b Mon Sep 17 00:00:00 2001 From: bd-912 Date: Fri, 10 May 2024 13:03:12 -0600 Subject: Fixed many issues with overloading in extensions --- heat/HeatVisitor.java | 20 +++++++---- output/negative/Overwrite-error1.java | 52 ---------------------------- output/negative/TrickyOverload-error.java | 54 +++++++++++++++++++++++++++++ st/ClassInstance.java | 26 ++++++++++++-- st/SymbolTable.java | 57 +++++++++++++++++++++---------- 5 files changed, 130 insertions(+), 79 deletions(-) delete mode 100644 output/negative/Overwrite-error1.java create mode 100644 output/negative/TrickyOverload-error.java 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> // 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> 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> 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 actual = new ArrayList(); ArrayList expected = new ArrayList(); n.f4.accept(this, actual); diff --git a/output/negative/Overwrite-error1.java b/output/negative/Overwrite-error1.java deleted file mode 100644 index 89b11a2..0000000 --- a/output/negative/Overwrite-error1.java +++ /dev/null @@ -1,52 +0,0 @@ -class MoreThan4{ - public static void main(String[] a){ - System.out.println(new MT4().Start(1,2,3,4,5,6)); - } -} - -class MT4 { - public int Start(int p1, int p2, int p3 , int p4, int p5, int p6){ - int aux ; - System.out.println(p1); - System.out.println(p2); - System.out.println(p3); - System.out.println(p4); - System.out.println(p5); - System.out.println(p6); - aux = this.Change(p6,p5,p4,p3,p2,p1); - return aux ; - } - - public int Change(int p1, int p2, int p3 , int p4, int p5, int p6){ - System.out.println(p1); - System.out.println(p2); - System.out.println(p3); - System.out.println(p4); - System.out.println(p5); - System.out.println(p6); - return 0 ; - } -} - -class MT42 extends MT4 { - public int Start(int p1, int p2, int p3 , int p4, int p5){ - int aux ; - System.out.println(p1); - System.out.println(p2); - System.out.println(p3); - System.out.println(p4); - System.out.println(p5); - aux = this.Change(p5,p4,p3,p2,p1); - return aux ; - } - - public int Change(int p1, int p2, int p3 , int p4, int p5){ - System.out.println(p1); - System.out.println(p2); - System.out.println(p3); - System.out.println(p4); - System.out.println(p5); - return 0 ; - } - -} diff --git a/output/negative/TrickyOverload-error.java b/output/negative/TrickyOverload-error.java new file mode 100644 index 0000000..d6b7d96 --- /dev/null +++ b/output/negative/TrickyOverload-error.java @@ -0,0 +1,54 @@ +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); + System.out.println(p2); + System.out.println(p3); + System.out.println(p4); + System.out.println(p5); + System.out.println(p6); + aux = this.Change(p6,p5,p4,p3,p2,p1); + return aux ; + } + + public int Change(int p1, int p2, int p3 , int p4, int p5, int p6){ + System.out.println(p1); + System.out.println(p2); + System.out.println(p3); + System.out.println(p4); + System.out.println(p5); + System.out.println(p6); + return 0 ; + } +} + +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); + System.out.println(p2); + System.out.println(p3); + System.out.println(p4); + System.out.println(p5); + aux = this.Change(p5,p4,p3,p2,p1); + return aux ; + } + + public int Change(int p1, int p2, int p3 , int p4, int p5){ + System.out.println(p1); + System.out.println(p2); + System.out.println(p3); + System.out.println(p4); + System.out.println(p5); + return 0 ; + } + +} 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 expected = exist.getArguments(); + ArrayList 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())); + } } } -- cgit v1.2.3