*** empty log message ***
[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
7     String varname;
8     VarDescriptor vd = null;
9     boolean typechecked = false;
10
11     public Set freeVars() {
12         HashSet hs=new HashSet();
13         hs.add(vd);
14         return hs;
15     }
16
17     public VarExpr(String varname) {
18         this.varname = varname; 
19     }
20
21     public VarExpr(VarDescriptor vd) {
22         this.vd=vd;
23         varname=vd.getSymbol();
24         this.td = vd.getType(); 
25     }
26
27     public String name() {
28         return varname;
29     }
30
31     public boolean usesDescriptor(Descriptor d) {
32         if (d==vd)
33             return true;
34         return false;
35     }
36
37     public boolean isNonNull() {
38         return true;
39     }
40
41     public boolean equals(Map remap, Expr e) {
42         if (e==null||!(e instanceof VarExpr))
43             return false;
44         VarExpr ve=(VarExpr)e;
45         if (vd==null)
46             throw new Error("Uninitialized VarDescriptor");
47         if (ve.vd==null)
48             throw new Error("e has uninitialized VarDescriptor");
49         VarDescriptor nvd=vd;
50         if (remap!=null&&remap.containsKey(nvd))
51             nvd=(VarDescriptor)remap.get(nvd);
52         if (nvd!=ve.vd)
53             return false;
54         return true;
55     }
56
57     public Set getInversedRelations() {
58         return new HashSet();
59     }
60
61     public Set getRequiredDescriptors() {
62         return new HashSet();
63     }
64     
65     public VarDescriptor getVar() {
66         return vd;
67     }
68
69     public void generate(CodeWriter writer, VarDescriptor dest) {        
70
71         // #TBD#: bit of a hack, really should have been type checked properly 
72
73         vd = (VarDescriptor) writer.getSymbolTable().get(varname);        
74         assert vd != null;
75         assert vd.getType() != null;
76         this.td = vd.getType();
77
78         writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + 
79                           " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; //varexpr");
80     }
81
82     public void prettyPrint(PrettyPrinter pp) {
83         pp.output(varname);
84     }
85
86     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
87         typechecked = true;
88         vd = (VarDescriptor) sa.getSymbolTable().get(varname);
89
90         if (vd == null) {
91             System.out.println(varname);
92             sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
93             return null;
94         }
95         
96         assert vd.getType() != null;
97
98         this.td = vd.getType();
99         return this.td;
100     }
101     
102 }