summaryrefslogtreecommitdiff
path: root/st/ClassInstance.java
blob: d96d3d59bb923d2c82f698b082001107bfa3eea4 (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
package st;

import java.util.ArrayList;

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 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;
    }

}