2 bug fixes to getRequiredConstraints
[repair.git] / Repair / RepairCompiler / MCC / IR / VarExpr.java
index ddc0e8eb9b7de1c0c246de76ea57365502638614..e94f6fb8cdf87b006a2b418e0ff3ec3a96e3cbbc 100755 (executable)
@@ -3,7 +3,7 @@ package MCC.IR;
 import java.util.*;
 
 public class VarExpr extends Expr {
-    static boolean DOMEMCHECKS=true;
+    static boolean DOMEMCHECKS=false;
     static boolean DOTYPECHECKS=false;
     static boolean DONULL=false;
 
@@ -17,7 +17,17 @@ public class VarExpr extends Expr {
        return hs;
     }
 
+    public Expr getLower() {
+       return vd.getLower();
+    }
+
+    public Expr getUpper() {
+       return vd.getUpper();
+    }
+
     public SetDescriptor getSet() {
+       if (vd==null)
+           return null;
        return vd.getSet();
     }
 
@@ -28,7 +38,7 @@ public class VarExpr extends Expr {
     public VarExpr(VarDescriptor vd) {
        this.vd=vd;
        varname=vd.getSymbol();
-        this.td = vd.getType();        
+        this.td = vd.getType();
     }
 
     public String name() {
@@ -73,9 +83,12 @@ public class VarExpr extends Expr {
     }
 
     public Set getRequiredDescriptors() {
-        return new HashSet();
+        Set s=new HashSet();
+        s.add(vd);
+        return s;
+
     }
-    
+
     public VarDescriptor getVar() {
         return vd;
     }
@@ -84,23 +97,47 @@ public class VarExpr extends Expr {
        return vd.isGlobal();
     }
 
-    public void generate(CodeWriter writer, VarDescriptor dest) {        
-        // #TBD#: bit of a hack, really should have been type checked properly 
+    public boolean isInvariant(Set vars) {
+       return vd.isGlobal()||vars.contains(vd);
+    }
+
+    public Set findInvariants(Set vars) {
+       if (isInvariant(vars)) {
+           Set s=new HashSet();
+           s.add(this);
+           return s;
+       } else
+           return new HashSet();
+    }
+
+    public void generate(CodeWriter writer, VarDescriptor dest) {
+        // #TBD#: bit of a hack, really should have been type checked properly
         assert vd != null;
         assert vd.getType() != null;
        this.td = vd.getType();
 
+       if (writer.getInvariantValue()!=null&&
+           writer.getInvariantValue().isInvariant(this)) {
+           writer.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol());
+           writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
+           writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
+           return;
+       }
 
-        writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + 
-                          " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; //varexpr");
+        writer.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol());
+        writer.outputline(dest.getSafeSymbol() +
+                          " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; /*varexpr*/");
        if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) {
            VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
            writer.outputline("if ("+dest.getSafeSymbol()+")");
            writer.startblock();
-           if (DOTYPECHECKS)
-               writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); 
-           else
-               writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); 
+           if (DOTYPECHECKS) {
+               writer.addDeclaration("bool",typevar.getSafeSymbol());
+               writer.outputline(typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
+           } else {
+               writer.addDeclaration("bool",typevar.getSafeSymbol());
+               writer.outputline(typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");");
+           }
            writer.outputline("if (!"+typevar.getSafeSymbol()+")");
            writer.startblock();
            writer.outputline(dest.getSafeSymbol()+"=0;");
@@ -118,17 +155,13 @@ public class VarExpr extends Expr {
     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
         typechecked = true;
         vd = (VarDescriptor) sa.getSymbolTable().get(varname);
-
         if (vd == null) {
             //System.out.println(varname);
             sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
             return null;
         }
-        
         assert vd.getType() != null;
-
         this.td = vd.getType();
         return this.td;
     }
-    
 }