Updates to allow repairing backpointers.
[repair.git] / Repair / RepairCompiler / MCC / IR / VarExpr.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class VarExpr extends Expr {
6     static boolean DOMEMCHECKS=false;
7     static boolean DOTYPECHECKS=false;
8     static boolean DONULL=false;
9
10     String varname;
11     VarDescriptor vd = null;
12     boolean typechecked = false;
13
14     public Set freeVars() {
15         HashSet hs=new HashSet();
16         hs.add(vd);
17         return hs;
18     }
19
20     public Expr getLower() {
21         return vd.getLower();
22     }
23
24     public Expr getUpper() {
25         return vd.getUpper();
26     }
27
28     public SetDescriptor getSet() {
29         if (vd==null)
30             return null;
31         return vd.getSet();
32     }
33
34     public VarExpr(String varname) {
35         this.varname = varname;
36     }
37
38     public VarExpr(VarDescriptor vd) {
39         this.vd=vd;
40         varname=vd.getSymbol();
41         this.td = vd.getType(); 
42     }
43
44     public String name() {
45         return varname;
46     }
47
48     public boolean usesDescriptor(Descriptor d) {
49         if (d==vd)
50             return true;
51         return false;
52     }
53
54     public Set useDescriptor(Descriptor d) {
55         HashSet newset=new HashSet();
56         if (d==vd)
57             newset.add(this);
58         return newset;
59     }
60
61     public boolean isNonNull() {
62         return true;
63     }
64
65     public boolean equals(Map remap, Expr e) {
66         if (e==null||!(e instanceof VarExpr))
67             return false;
68         VarExpr ve=(VarExpr)e;
69         if (vd==null)
70             throw new Error("Uninitialized VarDescriptor");
71         if (ve.vd==null)
72             throw new Error("e has uninitialized VarDescriptor");
73         VarDescriptor nvd=vd;
74         if (remap!=null&&remap.containsKey(nvd))
75             nvd=(VarDescriptor)remap.get(nvd);
76         if (nvd!=ve.vd)
77             return false;
78         return true;
79     }
80
81     public Set getInversedRelations() {
82         return new HashSet();
83     }
84
85     public Set getRequiredDescriptors() {
86         return new HashSet();
87     }
88     
89     public VarDescriptor getVar() {
90         return vd;
91     }
92
93     public boolean isValue() {
94         return vd.isGlobal();
95     }
96
97     public boolean isInvariant(Set vars) {
98         return vd.isGlobal()||vars.contains(vd);
99     }
100
101     public Set findInvariants(Set vars) {
102         if (isInvariant(vars)) {
103             Set s=new HashSet();
104             s.add(this);
105             return s;
106         } else 
107             return new HashSet();
108     }
109
110     public void generate(CodeWriter writer, VarDescriptor dest) {
111         // #TBD#: bit of a hack, really should have been type checked properly 
112         assert vd != null;
113         assert vd.getType() != null;
114         this.td = vd.getType();
115
116         if (writer.getInvariantValue()!=null&&
117             writer.getInvariantValue().isInvariant(this)) {
118             writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
119             writer.outputline(vd.getType().getGenerateType().getSafeSymbol()+
120                               " "+dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
121             return;
122         }
123
124         writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + 
125                           " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; //varexpr");
126         if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
127             VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
128             writer.outputline("if ("+dest.getSafeSymbol()+")");
129             writer.startblock();
130             if (DOTYPECHECKS)
131                 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); 
132             else
133                 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); 
134             writer.outputline("if (!"+typevar.getSafeSymbol()+")");
135             writer.startblock();
136             writer.outputline(dest.getSafeSymbol()+"=0;");
137             if (DONULL)
138                 writer.outputline(vd.getSafeSymbol()+"=0;");
139             writer.endblock();
140             writer.endblock();
141         }
142     }
143
144     public void prettyPrint(PrettyPrinter pp) {
145         pp.output(varname);
146     }
147
148     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
149         typechecked = true;
150         vd = (VarDescriptor) sa.getSymbolTable().get(varname);
151
152         if (vd == null) {
153             //System.out.println(varname);
154             sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
155             return null;
156         }
157         
158         assert vd.getType() != null;
159
160         this.td = vd.getType();
161         return this.td;
162     }
163     
164 }