fix generation of graph files
[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.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol());
119             writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
120             writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
121             return;
122         }
123
124         writer.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol());
125         writer.outputline(dest.getSafeSymbol() +
126                           " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; /*varexpr*/");
127         if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
128             VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
129             writer.outputline("if ("+dest.getSafeSymbol()+")");
130             writer.startblock();
131             if (DOTYPECHECKS) {
132                 writer.addDeclaration("bool",typevar.getSafeSymbol());
133                 writer.outputline(typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
134             } else {
135                 writer.addDeclaration("bool",typevar.getSafeSymbol());
136                 writer.outputline(typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
137             }
138             writer.outputline("if (!"+typevar.getSafeSymbol()+")");
139             writer.startblock();
140             writer.outputline(dest.getSafeSymbol()+"=0;");
141             if (DONULL)
142                 writer.outputline(vd.getSafeSymbol()+"=0;");
143             writer.endblock();
144             writer.endblock();
145         }
146     }
147
148     public void prettyPrint(PrettyPrinter pp) {
149         pp.output(varname);
150     }
151
152     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
153         typechecked = true;
154         vd = (VarDescriptor) sa.getSymbolTable().get(varname);
155         if (vd == null) {
156             //System.out.println(varname);
157             sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
158             return null;
159         }
160         assert vd.getType() != null;
161         this.td = vd.getType();
162         return this.td;
163     }
164 }