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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
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()))
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 void recursiveExtendFilter() {
ClassInstance ce = this.getExtend();
if (ce != null)
this.findMyselfHelper(ce);
}
public boolean findMyselfHelper(ClassInstance c) {
/**
* Returns true if there is a path through extensions that reaches
* the original class. Such a case is a type error.
*/
if (this.getName().equals(c.getName())) {
MinimalLogger.info(String.format("%s is part of an extension loop!",
this.getName()));
throw new TypecheckException(String.format("Recursive extension detected!"));
}
if (c.getExtend() == null) {
MinimalLogger.info(String.format("Search ended at %s.",
c.getName()));
return false;
}
ClassInstance ce = c.getExtend();
return this.findMyselfHelper(ce);
}
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;
}
}
|