oops...freeVars can return null.
[repair.git] / Repair / RepairCompiler / MCC / IR / SemanticChecker.java
index fb3f020602e834211436a7883ee96187cf6f2e0b..23e83f4b785377173e563a645e09735e3da66ac4 100755 (executable)
@@ -290,7 +290,7 @@ public class SemanticChecker {
                 } else {
                     rule.addQuantifier(quantifier);
                 }
-            }             
+            }
         }
         
         /* get guard expr */
@@ -379,7 +379,7 @@ public class SemanticChecker {
 
         boolean ok = true;
         ParseNodeVector constraints = pn.getChildren();
-        
+
         for (int i = 0; i < constraints.size(); i++) {
             ParseNode constraint = constraints.elementAt(i);
             assert constraint.getLabel().equals("constraint");
@@ -390,6 +390,28 @@ public class SemanticChecker {
 
         /* do any post checks... (type constraints, etc?) */
 
+        Iterator consiterator = state.vConstraints.iterator();
+
+        while (consiterator.hasNext()) {
+            Constraint cons = (Constraint) consiterator.next();
+
+            final SymbolTable consst = cons.getSymbolTable();
+            SemanticAnalyzer sa = new SemanticAnalyzer() {
+                    public IRErrorReporter getErrorReporter() { return er; }
+                    public SymbolTable getSymbolTable() { return consst; }
+                };
+
+            TypeDescriptor constype = cons.getLogicStatement().typecheck(sa);
+
+            if (constype == null) {
+               System.out.println("Failed attempting to type constraint");
+                ok = false;
+            } else if (constype != ReservedTypeDescriptor.INT) {
+                er.report(null, "Type of guard must be 'int' not '" + constype.getSymbol() + "'");
+                ok = false;
+            }
+       }
+
        return ok;
     }
 
@@ -418,17 +440,19 @@ public class SemanticChecker {
                 assert qn.getLabel().equals("quantifier");
                 Quantifier quantifier = parse_quantifier(qn);
                 if (quantifier == null) {
+                   System.out.println("Failed parsing quantifier");
                     ok = false;
                 } else {
                     constraint.addQuantifier(quantifier);
                 }
             }
         }
-                                                      
+
         /* get body */
         LogicStatement logicexpr = parse_body(pn.getChild("body"));
 
         if (logicexpr == null) {
+           System.out.println("Failed parsing logical expression");
             ok = false;
         } else {
             constraint.setLogicStatement(logicexpr);
@@ -442,7 +466,7 @@ public class SemanticChecker {
         assert sts.empty();
 
         /* add to vConstraints */
-        vConstraints.addElement(constraint);           
+        vConstraints.addElement(constraint);
 
         return ok;
     }
@@ -468,6 +492,7 @@ public class SemanticChecker {
             SetDescriptor set = parse_set(pn.getChild("set"));
             assert set != null;
             sq.setSet(set);
+           vd.setSet(set);
 
             vd.setType(set.getType());
 
@@ -498,18 +523,21 @@ public class SemanticChecker {
             
             rq.setRelation(rd);
             vd1.setType(rd.getDomain().getType());
+           vd1.setSet(rd.getDomain());
             vd2.setType(rd.getRange().getType());
+           vd2.setSet(rd.getRange());
             return rq;
         } else if (pn.getChild("for") != null) { /* for j = x to y */
             ForQuantifier fq = new ForQuantifier();
             
             /* grab var */
             VarDescriptor vd = reserveName(pn.getChild("var"));
-
+           
             if (vd == null) {
                 return null;
             }
 
+           vd.setSet(lookupSet("int", false));
             vd.setType(ReservedTypeDescriptor.INT);
             fq.setVar(vd);
 
@@ -517,10 +545,11 @@ public class SemanticChecker {
             Expr lower = parse_expr(pn.getChild("lower").getChild("expr"));
             Expr upper = parse_expr(pn.getChild("upper").getChild("expr"));
 
+
             if ((lower == null) || (upper == null)) {
                 return null;
             }
-
+           vd.setBounds(lower,upper);
             fq.setBounds(lower, upper);
 
             return fq;
@@ -785,13 +814,17 @@ public class SemanticChecker {
         String rangesetname = pn.getChild("range").getChild("type").getTerminal();
         assert rangesetname != null;
 
-        /* get domain multiplicity */
-        String domainmult = pn.getChild("domain").getChild("mult").getTerminal();
-        assert domainmult != null;
+        /* get domain multiplicity */  
+       String domainmult;
+       if (pn.getChild("domain").getChild("domainmult") != null)
+           domainmult = pn.getChild("domain").getChild("domainmult").getChild("mult").getTerminal();
+        //assert domainmult != null;
 
         /* get range multiplicity */
-        String rangemult = pn.getChild("range").getChild("mult").getTerminal();
-        assert rangemult != null;
+       String rangemult;
+       if (pn.getChild("range").getChild("domainrange") != null)
+           rangemult = pn.getChild("range").getChild("domainrange").getChild("mult").getTerminal();
+        //assert rangemult != null;
 
         /* NOTE: it is assumed that the sets have been parsed already so that the 
            set namespace is fully populated. any missing setdescriptors for the set
@@ -888,13 +921,17 @@ public class SemanticChecker {
                         if (newtype == null) {
                             // type never defined
                             // #TBD#: replace new ParseNode with original parsenode
-                            er.report(null, "Undefined type '" + fieldtype.getSymbol() + "'");
-                            ok = false;
+                           
+                           if (!field.getPtr()) { 
+                               /* Pointer types don't have to be defined */
+                               er.report(null, "Undefined type '" + fieldtype.getSymbol() + "'");
+                               ok = false;
+                           }
                         } else {
                             assert newtype != null;
                             field.setType(newtype);
                         }
-                    }                        
+                    }
                 }
 
                 Iterator labels = type.getLabels();
@@ -980,11 +1017,9 @@ public class SemanticChecker {
                         }
                     }
                 }
-                
-            } else {
+           } else {
                 throw new IRException("shouldn't be any other typedescriptor classes");
             }
-
         }
 
         // #TBD#: need to make sure that no cycles exist in any of the declarations or subtypes
@@ -1039,6 +1074,19 @@ public class SemanticChecker {
 
             /* lookup the type to get the type descriptor */
             type.setSuperType(lookupType(subtype, CREATE_MISSING));
+        } else if (pn.getChild("subclass") != null) {
+            // has a subtype, lets try to resolve it
+            String subclass = pn.getChild("subclass").getTerminal();
+           
+            if (subclass.equals(typename)) {
+                // Semantic Error: cyclic inheritance
+                er.report(pn, "Cyclic inheritance prohibited");
+                ok = false;
+            }
+
+            /* lookup the type to get the type descriptor */
+            type.setSuperType(lookupType(subclass, CREATE_MISSING));
+            type.setSubClass(true);
         }
 
         // set the current type so that the recursive parses on the labels
@@ -1407,6 +1455,16 @@ public class SemanticChecker {
             RelationDescriptor relation = lookupRelation(pn.getChild("dotinv").getChild("relation").getTerminal());
             relation.addUsage(RelationDescriptor.INVIMAGE);
             return new ImageSetExpr(ImageSetExpr.INVERSE, vd, relation);
+        } else if (pn.getChild("dotset") != null) {
+            ImageSetExpr ise = (ImageSetExpr) parse_setexpr(pn.getChild("dotset").getChild("setexpr"));
+            RelationDescriptor relation = lookupRelation(pn.getChild("dotset").getChild("relation").getTerminal());
+            relation.addUsage(RelationDescriptor.IMAGE);
+            return new ImageSetExpr(ise, relation);
+        } else if (pn.getChild("dotinvset") != null) {
+            ImageSetExpr ise = (ImageSetExpr) parse_setexpr(pn.getChild("dotinvset").getChild("setexpr"));
+            RelationDescriptor relation = lookupRelation(pn.getChild("dotinvset").getChild("relation").getTerminal());
+            relation.addUsage(RelationDescriptor.INVIMAGE);
+            return new ImageSetExpr(ImageSetExpr.INVERSE, ise, relation);
         } else {
             throw new IRException();
         }
@@ -1432,7 +1490,14 @@ public class SemanticChecker {
         /* do semantic check and if valid, add it to symbol table
            and add it to the quantifier as well */
         if (sts.peek().contains(varname)) {
-            return new VarDescriptor(varname);
+           VarDescriptor vdold=(VarDescriptor)sts.peek().get(varname);
+           return vdold;
+           /*     Dan was creating a new VarDescriptor...This seems
+                  like the wrong thing to do.  We'll just lookup the
+                  other one.
+                  --------------------------------------------------
+                  VarDescriptor vd=new VarDescriptor(varname);
+                  vd.setSet(vdold.getSet()); return vd;*/
         } else {
             /* Semantic Error: undefined variable */
             er.report(pn, "Undefined variable '" + varname + "'");