Should do array index calculations...Wonder if it works correctly?
[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         assert vd != null;
83         assert vd.getType() != null;
84         this.td = vd.getType();
85
86
87         writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + 
88                           " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; //varexpr");
89         if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
90             VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
91             writer.outputline("if ("+dest.getSafeSymbol()+")");
92             writer.startblock();
93             if (DOTYPECHECKS)
94                 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); 
95             else
96                 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); 
97             writer.outputline("if (!"+typevar.getSafeSymbol()+")");
98             writer.startblock();
99             writer.outputline(dest.getSafeSymbol()+"=0;");
100             if (DONULL)
101                 writer.outputline(vd.getSafeSymbol()+"=0;");
102             writer.endblock();
103             writer.endblock();
104         }
105     }
106
107     public void prettyPrint(PrettyPrinter pp) {
108         pp.output(varname);
109     }
110
111     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
112         typechecked = true;
113         vd = (VarDescriptor) sa.getSymbolTable().get(varname);
114
115         if (vd == null) {
116             //System.out.println(varname);
117             sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
118             return null;
119         }
120         
121         assert vd.getType() != null;
122
123         this.td = vd.getType();
124         return this.td;
125     }
126     
127 }