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