Added:
[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             if (DOTYPECHECKS)
87                 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); 
88             else
89                 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); 
90             writer.outputline("if (!"+typevar.getSafeSymbol()+")");
91             writer.startblock();
92             writer.outputline(dest.getSafeSymbol()+"=0;");
93             if (DONULL)
94                 writer.outputline(vd.getSafeSymbol()+"=0;");
95             writer.endblock();
96         }
97     }
98
99     public void prettyPrint(PrettyPrinter pp) {
100         pp.output(varname);
101     }
102
103     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
104         typechecked = true;
105         vd = (VarDescriptor) sa.getSymbolTable().get(varname);
106
107         if (vd == null) {
108             System.out.println(varname);
109             sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
110             return null;
111         }
112         
113         assert vd.getType() != null;
114
115         this.td = vd.getType();
116         return this.td;
117     }
118     
119 }