5 public class VarExpr extends Expr {
6 static boolean DOMEMCHECKS=false;
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 Expr getLower() {
24 public Expr getUpper() {
28 public SetDescriptor getSet() {
32 public VarExpr(String varname) {
33 this.varname = varname;
36 public VarExpr(VarDescriptor vd) {
38 varname=vd.getSymbol();
39 this.td = vd.getType();
42 public String name() {
46 public boolean usesDescriptor(Descriptor d) {
52 public Set useDescriptor(Descriptor d) {
53 HashSet newset=new HashSet();
59 public boolean isNonNull() {
63 public boolean equals(Map remap, Expr e) {
64 if (e==null||!(e instanceof VarExpr))
66 VarExpr ve=(VarExpr)e;
68 throw new Error("Uninitialized VarDescriptor");
70 throw new Error("e has uninitialized VarDescriptor");
72 if (remap!=null&&remap.containsKey(nvd))
73 nvd=(VarDescriptor)remap.get(nvd);
79 public Set getInversedRelations() {
83 public Set getRequiredDescriptors() {
87 public VarDescriptor getVar() {
91 public boolean isValue() {
95 public void generate(CodeWriter writer, VarDescriptor dest) {
96 // #TBD#: bit of a hack, really should have been type checked properly
98 assert vd.getType() != null;
99 this.td = vd.getType();
102 writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() +
103 " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; //varexpr");
104 if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
105 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
106 writer.outputline("if ("+dest.getSafeSymbol()+")");
109 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
111 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
112 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
114 writer.outputline(dest.getSafeSymbol()+"=0;");
116 writer.outputline(vd.getSafeSymbol()+"=0;");
122 public void prettyPrint(PrettyPrinter pp) {
126 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
128 vd = (VarDescriptor) sa.getSymbolTable().get(varname);
131 //System.out.println(varname);
132 sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
136 assert vd.getType() != null;
138 this.td = vd.getType();