ready to generate code
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / SemanticsChecker.java
diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java b/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java
new file mode 100644 (file)
index 0000000..f053ba4
--- /dev/null
@@ -0,0 +1,84 @@
+package edu.uci.eecs.specCompiler.codeGenerator;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+
+import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
+import edu.uci.eecs.specCompiler.specExtraction.Construct;
+import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
+
+public class SemanticsChecker {
+       public final ArrayList<SpecConstruct> constructs;
+       public final HashMap<String, SpecConstruct> label2Construct;
+       public final HashMap<String, SpecConstruct> interfaceName2Construct;
+
+       public SemanticsChecker(ArrayList<SpecConstruct> constructs) {
+               this.constructs = constructs;
+               this.label2Construct = new HashMap<String, SpecConstruct>();
+               this.interfaceName2Construct = new HashMap<String, SpecConstruct>();
+       }
+
+       private void checkHBLabelConsistency(String interfaceName, String label)
+                       throws SemanticsCheckerException {
+               if (!interfaceName2Construct.containsKey(interfaceName)) {
+                       throw new SemanticsCheckerException(
+                                       "In global construct, no interface \""
+                                                       + interfaceName + "\"!");
+               } else {
+                       InterfaceConstruct iConstruct = (InterfaceConstruct) interfaceName2Construct
+                                       .get(interfaceName).construct;
+                       if (!iConstruct.hbConditions.containsKey(label)) {
+                               throw new SemanticsCheckerException("Interface "
+                                               + interfaceName
+                                               + " doesn't contain HB_codition: " + label
+                                               + "!");
+                       }
+               }
+       }
+
+       public void check() throws SemanticsCheckerException {
+               boolean hasGlobalConstruct = false;
+               // First grab the information from the interface
+               for (int i = 0; i < constructs.size(); i++) {
+                       Construct inst = constructs.get(i).construct;
+                       if (inst instanceof InterfaceConstruct) {
+                               InterfaceConstruct iConstruct = (InterfaceConstruct) inst;
+                               interfaceName2Construct.put(iConstruct.name, constructs.get(i));
+                       }
+               }
+
+               for (int i = 0; i < constructs.size(); i++) {
+                       SpecConstruct inst = constructs.get(i);
+                       Construct construct = inst.construct;
+                       if (construct instanceof GlobalConstruct) {
+                               GlobalConstruct theConstruct = (GlobalConstruct) construct;
+                               if (!hasGlobalConstruct)
+                                       hasGlobalConstruct = true;
+                               else {
+                                       throw new SemanticsCheckerException(
+                                                       "More than one global construct!");
+                               }
+
+                               HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbConditions = theConstruct.hbRelations;
+                               for (ConditionalInterface left : hbConditions.keySet()) {
+                                       HashSet<ConditionalInterface> set = hbConditions.get(left);
+                                       checkHBLabelConsistency(left.interfaceName, left.hbConditionLabel);
+                                       for (ConditionalInterface right : set) {
+                                               checkHBLabelConsistency(right.interfaceName, right.hbConditionLabel);
+                                       }
+                               }
+                       } else if (construct instanceof PotentialCPDefineConstruct) {
+                               PotentialCPDefineConstruct theConstruct = (PotentialCPDefineConstruct) construct;
+                               label2Construct.put(theConstruct.label, inst);
+                       } else if (construct instanceof CPDefineCheckConstruct) {
+                               CPDefineCheckConstruct theConstruct = (CPDefineCheckConstruct) construct;
+                               label2Construct.put(theConstruct.label, inst);
+                       }
+               }
+       }
+}