Adding the -cplusplus option. This option writes virual table pointers into allocate...
[repair.git] / Repair / RepairCompiler / MCC / IR / VarExpr.java
index ff52366fac5a2dcc6dcb91037d83292a82c632c8..12a47f364ac74001bac92ae06c00aaf4fdd5b56b 100755 (executable)
@@ -3,6 +3,9 @@ package MCC.IR;
 import java.util.*;
 
 public class VarExpr extends Expr {
+    static boolean DOMEMCHECKS=false;
+    static boolean DOTYPECHECKS=false;
+    static boolean DONULL=false;
 
     String varname;
     VarDescriptor vd = null;
@@ -14,8 +17,22 @@ 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();
+    }
+
     public VarExpr(String varname) {
-        this.varname = varname; 
+        this.varname = varname;
     }
 
     public VarExpr(VarDescriptor vd) {
@@ -34,6 +51,13 @@ public class VarExpr extends Expr {
        return false;
     }
 
+    public Set useDescriptor(Descriptor d) {
+       HashSet newset=new HashSet();
+       if (d==vd)
+           newset.add(this);
+       return newset;
+    }
+
     public boolean isNonNull() {
        return true;
     }
@@ -66,17 +90,55 @@ public class VarExpr extends Expr {
         return vd;
     }
 
-    public void generate(CodeWriter writer, VarDescriptor dest) {        
+    public boolean isValue() {
+       return vd.isGlobal();
+    }
 
-        // #TBD#: bit of a hack, really should have been type checked properly 
+    public boolean isInvariant(Set vars) {
+       return vd.isGlobal()||vars.contains(vd);
+    }
 
-        vd = (VarDescriptor) writer.getSymbolTable().get(varname);        
+    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();
+       this.td = vd.getType();
+
+       if (writer.getInvariantValue()!=null&&
+           writer.getInvariantValue().isInvariant(this)) {
+           writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
+           writer.outputline(vd.getType().getGenerateType().getSafeSymbol()+
+                             " "+dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
+           return;
+       }
 
         writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + 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() + ");"); 
+           writer.outputline("if (!"+typevar.getSafeSymbol()+")");
+           writer.startblock();
+           writer.outputline(dest.getSafeSymbol()+"=0;");
+           if (DONULL)
+               writer.outputline(vd.getSafeSymbol()+"=0;");
+           writer.endblock();
+           writer.endblock();
+       }
     }
 
     public void prettyPrint(PrettyPrinter pp) {
@@ -88,7 +150,7 @@ public class VarExpr extends Expr {
         vd = (VarDescriptor) sa.getSymbolTable().get(varname);
 
         if (vd == null) {
-            System.out.println(varname);
+            //System.out.println(varname);
             sa.getErrorReporter().report(null, "Undefined variable '" + varname + "'");
             return null;
         }