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 SetDescriptor getSet() {
24 public VarExpr(String varname) {
25 this.varname = varname;
28 public VarExpr(VarDescriptor vd) {
30 varname=vd.getSymbol();
31 this.td = vd.getType();
34 public String name() {
38 public boolean usesDescriptor(Descriptor d) {
44 public boolean isNonNull() {
48 public boolean equals(Map remap, Expr e) {
49 if (e==null||!(e instanceof VarExpr))
51 VarExpr ve=(VarExpr)e;
53 throw new Error("Uninitialized VarDescriptor");
55 throw new Error("e has uninitialized VarDescriptor");
57 if (remap!=null&&remap.containsKey(nvd))
58 nvd=(VarDescriptor)remap.get(nvd);
64 public Set getInversedRelations() {
68 public Set getRequiredDescriptors() {
72 public VarDescriptor getVar() {
76 public boolean isValue() {
80 public void generate(CodeWriter writer, VarDescriptor dest) {
81 // #TBD#: bit of a hack, really should have been type checked properly
83 assert vd.getType() != null;
84 this.td = vd.getType();
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()+")");
94 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
96 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
97 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
99 writer.outputline(dest.getSafeSymbol()+"=0;");
101 writer.outputline(vd.getSafeSymbol()+"=0;");
107 public void prettyPrint(PrettyPrinter pp) {
111 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
113 vd = (VarDescriptor) sa.getSymbolTable().get(varname);
116 //System.out.println(varname);
117 sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
121 assert vd.getType() != null;
123 this.td = vd.getType();