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