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() {
34 public VarExpr(String varname) {
35 this.varname = varname;
38 public VarExpr(VarDescriptor vd) {
40 varname=vd.getSymbol();
41 this.td = vd.getType();
44 public String name() {
48 public boolean usesDescriptor(Descriptor d) {
54 public Set useDescriptor(Descriptor d) {
55 HashSet newset=new HashSet();
61 public boolean isNonNull() {
65 public boolean equals(Map remap, Expr e) {
66 if (e==null||!(e instanceof VarExpr))
68 VarExpr ve=(VarExpr)e;
70 throw new Error("Uninitialized VarDescriptor");
72 throw new Error("e has uninitialized VarDescriptor");
74 if (remap!=null&&remap.containsKey(nvd))
75 nvd=(VarDescriptor)remap.get(nvd);
81 public Set getInversedRelations() {
85 public Set getRequiredDescriptors() {
92 public VarDescriptor getVar() {
96 public boolean isValue() {
100 public boolean isInvariant(Set vars) {
101 return vd.isGlobal()||vars.contains(vd);
104 public Set findInvariants(Set vars) {
105 if (isInvariant(vars)) {
110 return new HashSet();
113 public void generate(CodeWriter writer, VarDescriptor dest) {
114 // #TBD#: bit of a hack, really should have been type checked properly
116 assert vd.getType() != null;
117 this.td = vd.getType();
119 if (writer.getInvariantValue()!=null&&
120 writer.getInvariantValue().isInvariant(this)) {
121 writer.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol());
122 writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
123 writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
127 writer.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol());
128 writer.outputline(dest.getSafeSymbol() +
129 " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; /*varexpr*/");
130 if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
131 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
132 writer.outputline("if ("+dest.getSafeSymbol()+")");
135 writer.addDeclaration("bool",typevar.getSafeSymbol());
136 writer.outputline(typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
138 writer.addDeclaration("bool",typevar.getSafeSymbol());
139 writer.outputline(typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
141 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
143 writer.outputline(dest.getSafeSymbol()+"=0;");
145 writer.outputline(vd.getSafeSymbol()+"=0;");
151 public void prettyPrint(PrettyPrinter pp) {
155 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
157 vd = (VarDescriptor) sa.getSymbolTable().get(varname);
159 //System.out.println(varname);
160 sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
163 assert vd.getType() != null;
164 this.td = vd.getType();