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