5 public class VarExpr extends Expr {
6 static boolean DOMEMCHECKS=true;
7 static boolean DOTYPECHECKS=false;
8 static boolean DONULL=false;
11 VarDescriptor vd = null;
12 boolean typechecked = false;
14 public Set freeVars() {
15 HashSet hs=new HashSet();
20 public VarExpr(String varname) {
21 this.varname = varname;
24 public VarExpr(VarDescriptor vd) {
26 varname=vd.getSymbol();
27 this.td = vd.getType();
30 public String name() {
34 public boolean usesDescriptor(Descriptor d) {
40 public boolean isNonNull() {
44 public boolean equals(Map remap, Expr e) {
45 if (e==null||!(e instanceof VarExpr))
47 VarExpr ve=(VarExpr)e;
49 throw new Error("Uninitialized VarDescriptor");
51 throw new Error("e has uninitialized VarDescriptor");
53 if (remap!=null&&remap.containsKey(nvd))
54 nvd=(VarDescriptor)remap.get(nvd);
60 public Set getInversedRelations() {
64 public Set getRequiredDescriptors() {
68 public VarDescriptor getVar() {
72 public void generate(CodeWriter writer, VarDescriptor dest) {
74 // #TBD#: bit of a hack, really should have been type checked properly
76 vd = (VarDescriptor) writer.getSymbolTable().get(varname);
78 assert vd.getType() != null;
79 this.td = vd.getType();
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()+")");
89 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
91 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
92 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
94 writer.outputline(dest.getSafeSymbol()+"=0;");
96 writer.outputline(vd.getSafeSymbol()+"=0;");
102 public void prettyPrint(PrettyPrinter pp) {
106 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
108 vd = (VarDescriptor) sa.getSymbolTable().get(varname);
111 System.out.println(varname);
112 sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
116 assert vd.getType() != null;
118 this.td = vd.getType();