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 Set useDescriptor(Descriptor d) {
45 HashSet newset=new HashSet();
51 public boolean isNonNull() {
55 public boolean equals(Map remap, Expr e) {
56 if (e==null||!(e instanceof VarExpr))
58 VarExpr ve=(VarExpr)e;
60 throw new Error("Uninitialized VarDescriptor");
62 throw new Error("e has uninitialized VarDescriptor");
64 if (remap!=null&&remap.containsKey(nvd))
65 nvd=(VarDescriptor)remap.get(nvd);
71 public Set getInversedRelations() {
75 public Set getRequiredDescriptors() {
79 public VarDescriptor getVar() {
83 public boolean isValue() {
87 public void generate(CodeWriter writer, VarDescriptor dest) {
88 // #TBD#: bit of a hack, really should have been type checked properly
90 assert vd.getType() != null;
91 this.td = vd.getType();
94 writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() +
95 " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; //varexpr");
96 if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
97 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
98 writer.outputline("if ("+dest.getSafeSymbol()+")");
101 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
103 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
104 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
106 writer.outputline(dest.getSafeSymbol()+"=0;");
108 writer.outputline(vd.getSafeSymbol()+"=0;");
114 public void prettyPrint(PrettyPrinter pp) {
118 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
120 vd = (VarDescriptor) sa.getSymbolTable().get(varname);
123 //System.out.println(varname);
124 sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
128 assert vd.getType() != null;
130 this.td = vd.getType();