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