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() {
89 public VarDescriptor getVar() {
93 public boolean isValue() {
97 public boolean isInvariant(Set vars) {
98 return vd.isGlobal()||vars.contains(vd);
101 public Set findInvariants(Set vars) {
102 if (isInvariant(vars)) {
107 return new HashSet();
110 public void generate(CodeWriter writer, VarDescriptor dest) {
111 // #TBD#: bit of a hack, really should have been type checked properly
113 assert vd.getType() != null;
114 this.td = vd.getType();
116 if (writer.getInvariantValue()!=null&&
117 writer.getInvariantValue().isInvariant(this)) {
118 writer.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol());
119 writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
120 writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
124 writer.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol());
125 writer.outputline(dest.getSafeSymbol() +
126 " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; /*varexpr*/");
127 if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
128 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
129 writer.outputline("if ("+dest.getSafeSymbol()+")");
132 writer.addDeclaration("bool",typevar.getSafeSymbol());
133 writer.outputline(typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
135 writer.addDeclaration("bool",typevar.getSafeSymbol());
136 writer.outputline(typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
138 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
140 writer.outputline(dest.getSafeSymbol()+"=0;");
142 writer.outputline(vd.getSafeSymbol()+"=0;");
148 public void prettyPrint(PrettyPrinter pp) {
152 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
154 vd = (VarDescriptor) sa.getSymbolTable().get(varname);
156 //System.out.println(varname);
157 sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
160 assert vd.getType() != null;
161 this.td = vd.getType();