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