cb5792a892eb0a37d7444503fc3419e0d79d222c
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / SemanticsChecker.java
1 package edu.uci.eecs.specCompiler.codeGenerator;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.HashSet;
7
8 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
9 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
10 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
14 import edu.uci.eecs.specCompiler.specExtraction.Construct;
15 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
16 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.ParserUtils;
20 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
22 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
23
24 public class SemanticsChecker {
25         public final HashMap<File, SourceFileInfo> srcFilesInfo;
26         public final ArrayList<Construct> constructs;
27         public final HashMap<String, Construct> CPLabel2Construct;
28         public final HashMap<String, PotentialCPDefineConstruct> potentialCPLabel2Construct;
29         public final HashMap<String, InterfaceConstruct> interfaceName2Construct;
30         public final HashMap<String, InterfaceDefineConstruct> interfaceName2DefineConstruct;
31         public final HashMap<String, ArrayList<InterfaceConstruct>> CPLabel2InterfaceConstruct;
32
33         public final HashMap<String, Integer> interface2Num;
34         public final HashMap<Integer, String> num2Interface;
35         public final HashMap<String, Integer> hbLabel2Num;
36         public final HashMap<Integer, String> num2HBLabel;
37         public final HashMap<String, Integer> commitPointLabel2Num;
38         public final HashMap<Integer, String> num2CommitPointLabel;
39
40         private HashMap<String, String> options;
41         private HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbConditions;
42         private ArrayList<EntryPointConstruct> entryPointConstructs;
43         private ClassBeginConstruct classBeginConstruct;
44         private ClassEndConstruct classEndConstruct;
45         private GlobalConstruct globalConstruct;
46
47         private String templateStr;
48         private String templateFullStr;
49         private String className;
50
51         private int _interfaceNum;
52         private int _hbLabelNum;
53         private int _commitPointNum;
54
55         public SemanticsChecker(SpecExtractor extractor) {
56                 this.srcFilesInfo = extractor.srcFilesInfo;
57                 this.constructs = extractor.getConstructs();
58                 this.CPLabel2Construct = new HashMap<String, Construct>();
59                 this.potentialCPLabel2Construct = new HashMap<String, PotentialCPDefineConstruct>();
60                 this.interfaceName2Construct = new HashMap<String, InterfaceConstruct>();
61                 this.interfaceName2DefineConstruct = new HashMap<String, InterfaceDefineConstruct>();
62                 this.CPLabel2InterfaceConstruct = new HashMap<String, ArrayList<InterfaceConstruct>>();
63                 this.entryPointConstructs = new ArrayList<EntryPointConstruct>();
64                 this.classBeginConstruct = null;
65                 this.classEndConstruct = null;
66
67                 this.interface2Num = new HashMap<String, Integer>();
68                 this.num2Interface = new HashMap<Integer, String>();
69                 this.hbLabel2Num = new HashMap<String, Integer>();
70                 this.num2HBLabel = new HashMap<Integer, String>();
71                 // Immediately init the true HB-condition to be 0
72                 hbLabel2Num.put("", 0);
73                 num2HBLabel.put(0, "");
74
75                 this.commitPointLabel2Num = new HashMap<String, Integer>();
76                 this.num2CommitPointLabel = new HashMap<Integer, String>();
77
78                 _interfaceNum = 0;
79                 _hbLabelNum = 0;
80                 _commitPointNum = 0;
81
82                 templateStr = null;
83                 templateFullStr = null;
84                 className = null;
85         }
86
87         public ClassBeginConstruct getClassBeginConstruct() {
88                 return this.classBeginConstruct;
89         }
90
91         public ClassEndConstruct getClassEndConstruct() {
92                 return this.classEndConstruct;
93         }
94
95         public String getTemplateFullStr() {
96                 return this.templateFullStr;
97         }
98
99         public String getTemplateStr() {
100                 return this.templateStr;
101         }
102
103         public String getClassName() {
104                 return this.className;
105         }
106
107         public GlobalConstruct getGlobalConstruct() {
108                 return this.globalConstruct;
109         }
110
111         public HashMap<ConditionalInterface, HashSet<ConditionalInterface>> getHBConditions() {
112                 return this.hbConditions;
113         }
114
115         /**
116          * Check if the conditional interface is in the HB checking list
117          * 
118          * @param condInterface
119          * @return
120          */
121         public boolean containsConditionalInterface(
122                         ConditionalInterface condInterface) {
123                 if (hbConditions.containsKey(condInterface))
124                         return true;
125                 for (ConditionalInterface key : hbConditions.keySet()) {
126                         if (hbConditions.get(key).contains(condInterface))
127                                 return true;
128                 }
129                 return false;
130         }
131
132         public String getOption(String key) {
133                 return options.get(key);
134         }
135
136         private void checkHBLabelConsistency(ConditionalInterface inst)
137                         throws SemanticsCheckerException {
138                 String interfaceName = inst.interfaceName, label = inst.hbConditionLabel;
139                 if (!interfaceName2Construct.containsKey(interfaceName)) {
140                         throw new SemanticsCheckerException(
141                                         "In global construct, no interface \"" + interfaceName
142                                                         + "\"!");
143                 } else if (!label.equals("")) {
144                         InterfaceConstruct iConstruct = (InterfaceConstruct) interfaceName2Construct
145                                         .get(interfaceName);
146                         if (!iConstruct.hbConditions.containsKey(label)) {
147                                 throw new SemanticsCheckerException("Interface "
148                                                 + interfaceName + " doesn't contain HB_codition: "
149                                                 + label + "!");
150                         }
151
152                         // Number the HB-condition label
153                         hbLabel2Num.put(label, _hbLabelNum++);
154                         num2HBLabel.put(_hbLabelNum, label);
155                 }
156         }
157
158         private void checkLabelDuplication(Construct construct, String label)
159                         throws SemanticsCheckerException {
160                 if (potentialCPLabel2Construct.containsKey(label)
161                                 || CPLabel2Construct.containsKey(label))
162                         throw new SemanticsCheckerException("In construct: " + construct
163                                         + "\"" + label + "\" has duplication.");
164         }
165
166         private void checkOptions() throws SemanticsCheckerException {
167                 // FIXME: We don't have any check here
168         }
169
170         private void postCheck() throws SemanticsCheckerException {
171                 // C++ data structure with Class must provide the beginning and ending
172                 // of its declaration
173                 if (getOption("Class") != null) {
174                         if (classBeginConstruct == null || classEndConstruct == null) {
175                                 throw new SemanticsCheckerException(
176                                                 "Class must provide the boundary explicitly!");
177                         }
178                 }
179                 // It must provide the entry point
180                 if (entryPointConstructs.size() == 0) {
181                         throw new SemanticsCheckerException(
182                                         "The program must have at least one entry point!");
183                 }
184
185                 // Check if interface define construct labels are correct
186                 for (String name : interfaceName2DefineConstruct.keySet()) {
187                         if (!interfaceName2Construct.containsKey(name)) {
188                                 throw new SemanticsCheckerException("Label \"" + name
189                                                 + "\" does not have interface declaration!");
190                         }
191                 }
192         }
193
194         public void check() throws SemanticsCheckerException {
195                 boolean hasGlobalConstruct = false;
196                 // First grab the information from the interface
197                 for (int i = 0; i < constructs.size(); i++) {
198                         Construct inst = constructs.get(i);
199                         if (inst instanceof InterfaceConstruct) {
200                                 InterfaceConstruct iConstruct = (InterfaceConstruct) inst;
201                                 if (interfaceName2Construct.containsKey(iConstruct.name)) {
202                                         throw new SemanticsCheckerException("Interface name: "
203                                                         + iConstruct.name + " duplicates!");
204                                 }
205                                 // Number the interface label
206                                 interface2Num.put(iConstruct.name, _interfaceNum++);
207                                 num2Interface.put(_interfaceNum, iConstruct.name);
208
209                                 interfaceName2Construct.put(iConstruct.name,
210                                                 (InterfaceConstruct) constructs.get(i));
211
212                                 for (int j = 0; j < iConstruct.commitPointSet.size(); j++) {
213                                         String label = iConstruct.commitPointSet.get(j);
214                                         if (!CPLabel2InterfaceConstruct.containsKey(label)) {
215                                                 CPLabel2InterfaceConstruct.put(label,
216                                                                 new ArrayList<InterfaceConstruct>());
217                                         }
218                                         CPLabel2InterfaceConstruct.get(label).add(iConstruct);
219                                 }
220                         }
221                 }
222
223                 String label;
224                 for (int i = 0; i < constructs.size(); i++) {
225                         Construct construct = constructs.get(i);
226                         if (construct instanceof GlobalConstruct) {
227                                 GlobalConstruct theConstruct = (GlobalConstruct) construct;
228                                 globalConstruct = theConstruct;
229                                 if (!hasGlobalConstruct)
230                                         hasGlobalConstruct = true;
231                                 else {
232                                         throw new SemanticsCheckerException(
233                                                         "More than one global construct!");
234                                 }
235                                 // Record the options and check them
236                                 options = theConstruct.options;
237
238                                 // Record the HB conditions and check it
239                                 hbConditions = theConstruct.hbRelations;
240                                 for (ConditionalInterface left : hbConditions.keySet()) {
241                                         HashSet<ConditionalInterface> set = hbConditions.get(left);
242                                         checkHBLabelConsistency(left);
243
244                                         for (ConditionalInterface right : set) {
245                                                 checkHBLabelConsistency(right);
246                                         }
247                                 }
248                         } else if (construct instanceof PotentialCPDefineConstruct) {
249                                 PotentialCPDefineConstruct theConstruct = (PotentialCPDefineConstruct) construct;
250                                 label = theConstruct.label;
251                                 checkLabelDuplication(construct, label);
252                                 // Number the commit point potential commit point label
253                                 commitPointLabel2Num.put(label, _commitPointNum++);
254                                 num2CommitPointLabel.put(_commitPointNum, label);
255
256                                 potentialCPLabel2Construct.put(label,
257                                                 (PotentialCPDefineConstruct) construct);
258                         } else if (construct instanceof CPDefineCheckConstruct) {
259                                 CPDefineCheckConstruct theConstruct = (CPDefineCheckConstruct) construct;
260                                 label = theConstruct.label;
261                                 checkLabelDuplication(construct, label);
262                                 // Number the commit point define check label
263                                 commitPointLabel2Num.put(label, _commitPointNum++);
264                                 num2CommitPointLabel.put(_commitPointNum, label);
265
266                                 CPLabel2Construct.put(label, construct);
267                         } else if (construct instanceof CPDefineConstruct) {
268                                 CPDefineConstruct theConstruct = (CPDefineConstruct) construct;
269                                 label = theConstruct.label;
270                                 checkLabelDuplication(construct, label);
271                                 // Number the commit point define label
272                                 commitPointLabel2Num.put(label, _commitPointNum++);
273                                 num2CommitPointLabel.put(_commitPointNum, label);
274
275                                 CPLabel2Construct.put(label, construct);
276                         } else if (construct instanceof EntryPointConstruct) {
277                                 entryPointConstructs.add((EntryPointConstruct) construct);
278                         } else if (construct instanceof InterfaceDefineConstruct) {
279                                 InterfaceDefineConstruct theConstruct = (InterfaceDefineConstruct) construct;
280                                 String name = theConstruct.name;
281                                 if (interfaceName2DefineConstruct.containsKey(name)) {
282                                         throw new SemanticsCheckerException(
283                                                         "Interface define label duplicates!");
284                                 }
285                                 interfaceName2DefineConstruct.put(name, theConstruct);
286                         } else if (construct instanceof ClassBeginConstruct) {
287                                 classBeginConstruct = (ClassBeginConstruct) construct;
288                                 ArrayList<String> content = srcFilesInfo
289                                                 .get(classBeginConstruct.file).content;
290                                 String firstLine = content
291                                                 .get(classBeginConstruct.beginLineNum + 1), secondLine;
292                                 if (firstLine.startsWith("template")) {
293                                         secondLine = content
294                                                         .get(classBeginConstruct.beginLineNum + 1);
295                                         templateFullStr = firstLine;
296                                         templateStr = ParserUtils.getTemplateStr(firstLine);
297                                         className = ParserUtils.getClassName(secondLine);
298                                 } else {
299                                         className = ParserUtils.getClassName(firstLine);
300                                 }
301
302                         } else if (construct instanceof ClassEndConstruct) {
303                                 classEndConstruct = (ClassEndConstruct) construct;
304                                 className = getOption("CLASS");
305                         }
306                 }
307         }
308
309         public String toString() {
310                 StringBuilder sb = new StringBuilder();
311
312                 sb.append("Interface name 2 Construct:\n");
313                 for (String interfaceName : interfaceName2Construct.keySet()) {
314                         sb.append(interfaceName + "\t"
315                                         + interfaceName2Construct.get(interfaceName) + "\n");
316                 }
317
318                 sb.append("Interface name 2 define construct:\n");
319                 for (String interfaceName : interfaceName2DefineConstruct.keySet()) {
320                         sb.append(interfaceName + "\t"
321                                         + interfaceName2DefineConstruct.get(interfaceName) + "\n");
322                 }
323
324                 sb.append("Potential commit point label 2 Construct:\n");
325                 for (String label : potentialCPLabel2Construct.keySet()) {
326                         sb.append(label + "\t" + potentialCPLabel2Construct.get(label)
327                                         + "\n");
328                 }
329
330                 sb.append("Commit point label 2 Construct:\n");
331                 for (String label : CPLabel2Construct.keySet()) {
332                         sb.append(label + "\t" + CPLabel2Construct.get(label) + "\n");
333                 }
334                 return sb.toString();
335         }
336 }