diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-07 12:26:47 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-07 12:26:47 -0600 |
commit | 1ec847c7222b8adb9a70264c98a44dc9911d65d3 (patch) | |
tree | ed8d2601bc14079bb39c7af0f0c6533875b48b2c /st/AbstractInstance.java | |
parent | 9cd3df6ff58e3999a049f97a0acaaf997a01fec8 (diff) |
More bugfixes in ST, changes to scoping
Diffstat (limited to 'st/AbstractInstance.java')
-rw-r--r-- | st/AbstractInstance.java | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java index a6c2ca1..bddaccf 100644 --- a/st/AbstractInstance.java +++ b/st/AbstractInstance.java @@ -1,14 +1,17 @@ package st; +import java.util.ArrayList; + public abstract class AbstractInstance { - protected String name; // the literal name of the declaration - protected TypeEnum type; // the type of the declaration - protected int size; // the size in memory - protected AbstractInstance scope; // the surrounding method or class + protected String name; // the literal name of the declaration + protected TypeEnum type; // the type of the declaration + protected int size; // the size in memory + protected ArrayList<AbstractInstance> scope; // the scope where the instance is valid public AbstractInstance(String name, TypeEnum type) { this.type = type; this.name = name; + this.scope = new ArrayList<>(); } public String toString() { @@ -24,11 +27,18 @@ public abstract class AbstractInstance { return this.name.hashCode(); } - public void setScope(AbstractInstance cls) { - if (this.scope != null) - throw new RuntimeException("setScope: Attempted to set scope twice!"); - - this.scope = cls; + public void setScope(AbstractInstance ins) { + /** + * If the scope is a MethodInstance, add the single method. + * If the scope is a ClassInstance, add the classes' methods, + * and the class itself. + */ + if (ins instanceof MethodInstance) + this.scope.add(ins); + else if (ins instanceof ClassInstance) { + for (MethodInstance mtd : ((ClassInstance) ins).getMethods()) + this.scope.add(mtd); + } } public String getName() { @@ -43,11 +53,7 @@ public abstract class AbstractInstance { return this.size; } - public AbstractInstance getScope() { - /** - * Returns the scope of the method, or - * `null' if unset. - */ + public ArrayList<AbstractInstance> getScope() { return this.scope; } |