summaryrefslogtreecommitdiff
path: root/st/AbstractInstance.java
blob: bddaccff982bf034ddab99fc5104fe2db8c762a2 (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
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 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() {
        return this.name;
    }

    public boolean equals(AbstractInstance other) {
        return this.name == other.getName() &&
            this.type == this.type;
    }

    public int hashCode() {
        return this.name.hashCode();
    }

    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() {
        return this.name;
    }

    public TypeEnum getType() {
        return this.type;
    }

    public int getSize() {
        return this.size;
    }

    public ArrayList<AbstractInstance> getScope() {
        return this.scope;
    }

}