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