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 boolean isValue() {
76 public void generate(CodeWriter writer, VarDescriptor dest) {
78 // #TBD#: bit of a hack, really should have been type checked properly
80 vd = (VarDescriptor) writer.getSymbolTable().get(varname);
82 assert vd.getType() != null;
83 this.td = vd.getType();
86 writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() +
87 " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; //varexpr");
88 if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
89 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
90 writer.outputline("if ("+dest.getSafeSymbol()+")");
93 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
95 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
96 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
98 writer.outputline(dest.getSafeSymbol()+"=0;");
100 writer.outputline(vd.getSafeSymbol()+"=0;");
106 public void prettyPrint(PrettyPrinter pp) {
110 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
112 vd = (VarDescriptor) sa.getSymbolTable().get(varname);
115 //System.out.println(varname);
116 sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
120 assert vd.getType() != null;
122 this.td = vd.getType();