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 boolean isInvariant(Set vars) {
96 return vd.isGlobal()||vars.contains(vd);
99 public Set findInvariants(Set vars) {
100 if (isInvariant(vars)) {
105 return new HashSet();
108 public void generate(CodeWriter writer, VarDescriptor dest) {
109 // #TBD#: bit of a hack, really should have been type checked properly
111 assert vd.getType() != null;
112 this.td = vd.getType();
114 if (writer.getInvariantValue()!=null&&
115 writer.getInvariantValue().isInvariant(this)) {
116 writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
117 writer.outputline(vd.getType().getGenerateType().getSafeSymbol()+
118 " "+dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
122 writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() +
123 " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; //varexpr");
124 if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
125 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
126 writer.outputline("if ("+dest.getSafeSymbol()+")");
129 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
131 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
132 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
134 writer.outputline(dest.getSafeSymbol()+"=0;");
136 writer.outputline(vd.getSafeSymbol()+"=0;");
142 public void prettyPrint(PrettyPrinter pp) {
146 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
148 vd = (VarDescriptor) sa.getSymbolTable().get(varname);
151 //System.out.println(varname);
152 sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
156 assert vd.getType() != null;
158 this.td = vd.getType();