ready to generate code
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / SemanticsChecker.java
1 package edu.uci.eecs.specCompiler.codeGenerator;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
6
7 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
8 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
9 import edu.uci.eecs.specCompiler.specExtraction.Construct;
10 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
14
15 public class SemanticsChecker {
16         public final ArrayList<SpecConstruct> constructs;
17         public final HashMap<String, SpecConstruct> label2Construct;
18         public final HashMap<String, SpecConstruct> interfaceName2Construct;
19
20         public SemanticsChecker(ArrayList<SpecConstruct> constructs) {
21                 this.constructs = constructs;
22                 this.label2Construct = new HashMap<String, SpecConstruct>();
23                 this.interfaceName2Construct = new HashMap<String, SpecConstruct>();
24         }
25
26         private void checkHBLabelConsistency(String interfaceName, String label)
27                         throws SemanticsCheckerException {
28                 if (!interfaceName2Construct.containsKey(interfaceName)) {
29                         throw new SemanticsCheckerException(
30                                         "In global construct, no interface \""
31                                                         + interfaceName + "\"!");
32                 } else {
33                         InterfaceConstruct iConstruct = (InterfaceConstruct) interfaceName2Construct
34                                         .get(interfaceName).construct;
35                         if (!iConstruct.hbConditions.containsKey(label)) {
36                                 throw new SemanticsCheckerException("Interface "
37                                                 + interfaceName
38                                                 + " doesn't contain HB_codition: " + label
39                                                 + "!");
40                         }
41                 }
42         }
43
44         public void check() throws SemanticsCheckerException {
45                 boolean hasGlobalConstruct = false;
46                 // First grab the information from the interface
47                 for (int i = 0; i < constructs.size(); i++) {
48                         Construct inst = constructs.get(i).construct;
49                         if (inst instanceof InterfaceConstruct) {
50                                 InterfaceConstruct iConstruct = (InterfaceConstruct) inst;
51                                 interfaceName2Construct.put(iConstruct.name, constructs.get(i));
52                         }
53                 }
54
55                 for (int i = 0; i < constructs.size(); i++) {
56                         SpecConstruct inst = constructs.get(i);
57                         Construct construct = inst.construct;
58                         if (construct instanceof GlobalConstruct) {
59                                 GlobalConstruct theConstruct = (GlobalConstruct) construct;
60                                 if (!hasGlobalConstruct)
61                                         hasGlobalConstruct = true;
62                                 else {
63                                         throw new SemanticsCheckerException(
64                                                         "More than one global construct!");
65                                 }
66
67                                 HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbConditions = theConstruct.hbRelations;
68                                 for (ConditionalInterface left : hbConditions.keySet()) {
69                                         HashSet<ConditionalInterface> set = hbConditions.get(left);
70                                         checkHBLabelConsistency(left.interfaceName, left.hbConditionLabel);
71                                         for (ConditionalInterface right : set) {
72                                                 checkHBLabelConsistency(right.interfaceName, right.hbConditionLabel);
73                                         }
74                                 }
75                         } else if (construct instanceof PotentialCPDefineConstruct) {
76                                 PotentialCPDefineConstruct theConstruct = (PotentialCPDefineConstruct) construct;
77                                 label2Construct.put(theConstruct.label, inst);
78                         } else if (construct instanceof CPDefineCheckConstruct) {
79                                 CPDefineCheckConstruct theConstruct = (CPDefineCheckConstruct) construct;
80                                 label2Construct.put(theConstruct.label, inst);
81                         }
82                 }
83         }
84 }