blob: d51446f3d14f885630995f4dc95f7b147ce91450 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
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<TypeInstance> attrs; // the list of class-fields
private ArrayList<MethodInstance> 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<TypeInstance> getLocals() {
return this.attrs;
}
public ArrayList<MethodInstance> 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;
}
}
|