1 package edu.uci.eecs.specCompiler.codeGenerator;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.HashSet;
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;
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;
34 public final HashMap<String, Integer> interface2Num;
35 public final HashMap<String, Integer> hbLabel2Num;
36 public final HashMap<String, Integer> commitPointLabel2Num;
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;
44 private String templateStr;
45 private String templateFullStr;
46 private String className;
48 private int _interfaceNum;
49 private int _hbLabelNum;
50 private int _commitPointNum;
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;
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);
69 this.commitPointLabel2Num = new HashMap<String, Integer>();
76 templateFullStr = null;
80 public ClassBeginConstruct getClassBeginConstruct() {
81 return this.classBeginConstruct;
84 public ClassEndConstruct getClassEndConstruct() {
85 return this.classEndConstruct;
88 public String getTemplateFullStr() {
89 return this.templateFullStr;
92 public String getTemplateStr() {
93 return this.templateStr;
96 public String getClassName() {
97 return this.className;
100 public HashMap<ConditionalInterface, HashSet<ConditionalInterface>> getHBConditions() {
101 return this.hbConditions;
105 * Check if the conditional interface is in the HB checking list
106 * @param condInterface
109 public boolean containsConditionalInterface(ConditionalInterface condInterface) {
110 if (hbConditions.containsKey(condInterface))
112 for (ConditionalInterface key : hbConditions.keySet()) {
113 if (hbConditions.get(key).contains(condInterface))
119 public String getOption(String key) {
120 return options.get(key);
123 private void checkHBLabelConsistency(ConditionalInterface inst)
124 throws SemanticsCheckerException {
125 String interfaceName = inst.interfaceName, label = inst.hbConditionLabel;
126 if (!interfaceName2Construct.containsKey(interfaceName)) {
127 throw new SemanticsCheckerException(
128 "In global construct, no interface \"" + interfaceName
130 } else if (!label.equals("")) {
131 InterfaceConstruct iConstruct = (InterfaceConstruct) interfaceName2Construct
133 if (!iConstruct.hbConditions.containsKey(label)) {
134 throw new SemanticsCheckerException("Interface "
135 + interfaceName + " doesn't contain HB_codition: "
139 // Number the HB-condition label
140 hbLabel2Num.put(label, _hbLabelNum++);
144 private void checkLabelDuplication(Construct construct, String label)
145 throws SemanticsCheckerException {
146 if (potentialCPLabel2Construct.containsKey(label)
147 || CPLabel2Construct.containsKey(label))
148 throw new SemanticsCheckerException("In construct: " + construct
149 + "\"" + label + "\" has duplication.");
152 private void checkOptions() throws SemanticsCheckerException {
153 // FIXME: We don't have any check here
156 private void postCheck() throws SemanticsCheckerException {
157 // C++ data structure with Class must provide the beginning and ending
158 // of its declaration
159 if (getOption("Class") != null) {
160 if (classBeginConstruct == null || classEndConstruct == null) {
161 throw new SemanticsCheckerException(
162 "Class must provide the boundary explicitly!");
165 // It must provide the entry point
166 if (entryPointConstructs.size() == 0) {
167 throw new SemanticsCheckerException(
168 "The program must have at least one entry point!");
171 // Check if interface define construct labels are correct
172 for (String name : interfaceName2DefineConstruct.keySet()) {
173 if (!interfaceName2Construct.containsKey(name)) {
174 throw new SemanticsCheckerException("Label \"" + name
175 + "\" does not have interface declaration!");
180 public void check() throws SemanticsCheckerException {
181 boolean hasGlobalConstruct = false;
182 // First grab the information from the interface
183 for (int i = 0; i < constructs.size(); i++) {
184 Construct inst = constructs.get(i);
185 if (inst instanceof InterfaceConstruct) {
186 InterfaceConstruct iConstruct = (InterfaceConstruct) inst;
187 if (interfaceName2Construct.containsKey(iConstruct.name)) {
188 throw new SemanticsCheckerException("Interface name: "
189 + iConstruct.name + " duplicates!");
191 // Number the interface label
192 interface2Num.put(iConstruct.name, _interfaceNum++);
194 interfaceName2Construct.put(iConstruct.name,
195 (InterfaceConstruct) constructs.get(i));
197 for (int j = 0; j < iConstruct.commitPointSet.size(); j++) {
198 String label = iConstruct.commitPointSet.get(j);
199 if (!CPLabel2InterfaceConstruct.containsKey(label)) {
200 CPLabel2InterfaceConstruct.put(label,
201 new ArrayList<InterfaceConstruct>());
203 CPLabel2InterfaceConstruct.get(label).add(iConstruct);
209 for (int i = 0; i < constructs.size(); i++) {
210 Construct construct = constructs.get(i);
211 if (construct instanceof GlobalConstruct) {
212 GlobalConstruct theConstruct = (GlobalConstruct) construct;
213 if (!hasGlobalConstruct)
214 hasGlobalConstruct = true;
216 throw new SemanticsCheckerException(
217 "More than one global construct!");
219 // Record the options and check them
220 options = theConstruct.options;
222 // Record the HB conditions and check it
223 hbConditions = theConstruct.hbRelations;
224 for (ConditionalInterface left : hbConditions.keySet()) {
225 HashSet<ConditionalInterface> set = hbConditions.get(left);
226 checkHBLabelConsistency(left);
228 for (ConditionalInterface right : set) {
229 checkHBLabelConsistency(right);
232 } else if (construct instanceof PotentialCPDefineConstruct) {
233 PotentialCPDefineConstruct theConstruct = (PotentialCPDefineConstruct) construct;
234 label = theConstruct.label;
235 checkLabelDuplication(construct, label);
236 // Number the commit_point label
237 commitPointLabel2Num.put(label, _commitPointNum++);
239 potentialCPLabel2Construct.put(label,
240 (PotentialCPDefineConstruct) construct);
241 } else if (construct instanceof CPDefineCheckConstruct) {
242 CPDefineCheckConstruct theConstruct = (CPDefineCheckConstruct) construct;
243 label = theConstruct.label;
244 checkLabelDuplication(construct, label);
245 // Number the commit_point label
246 commitPointLabel2Num.put(label, _commitPointNum++);
248 CPLabel2Construct.put(label, construct);
249 } else if (construct instanceof CPDefineConstruct) {
250 CPDefineConstruct theConstruct = (CPDefineConstruct) construct;
251 label = theConstruct.label;
252 checkLabelDuplication(construct, label);
253 // Number the commit_point label
254 commitPointLabel2Num.put(label, _commitPointNum++);
256 CPLabel2Construct.put(label, construct);
257 } else if (construct instanceof EntryPointConstruct) {
258 entryPointConstructs.add((EntryPointConstruct) construct);
259 } else if (construct instanceof InterfaceDefineConstruct) {
260 InterfaceDefineConstruct theConstruct = (InterfaceDefineConstruct) construct;
261 String name = theConstruct.name;
262 if (interfaceName2DefineConstruct.containsKey(name)) {
263 throw new SemanticsCheckerException(
264 "Interface define label duplicates!");
266 interfaceName2DefineConstruct.put(name, theConstruct);
267 } else if (construct instanceof ClassBeginConstruct) {
268 classBeginConstruct = (ClassBeginConstruct) construct;
269 ArrayList<String> content = srcFilesInfo.get(classBeginConstruct.file).content;
270 String firstLine = content.get(classBeginConstruct.beginLineNum), secondLine;
271 if (firstLine.startsWith("template")) {
272 secondLine = content.get(classBeginConstruct.beginLineNum + 1);
273 templateFullStr = firstLine;
274 templateStr = ParserUtils.getTemplateStr(firstLine);
275 className = ParserUtils.getClassName(secondLine);
277 className = ParserUtils.getClassName(firstLine);
280 } else if (construct instanceof ClassEndConstruct) {
281 classEndConstruct = (ClassEndConstruct) construct;
286 public String toString() {
287 StringBuilder sb = new StringBuilder();
289 sb.append("Interface name 2 Construct:\n");
290 for (String interfaceName : interfaceName2Construct.keySet()) {
291 sb.append(interfaceName + "\t"
292 + interfaceName2Construct.get(interfaceName) + "\n");
295 sb.append("Interface name 2 define construct:\n");
296 for (String interfaceName : interfaceName2DefineConstruct.keySet()) {
297 sb.append(interfaceName + "\t"
298 + interfaceName2DefineConstruct.get(interfaceName) + "\n");
301 sb.append("Potential commit point label 2 Construct:\n");
302 for (String label : potentialCPLabel2Construct.keySet()) {
303 sb.append(label + "\t" + potentialCPLabel2Construct.get(label)
307 sb.append("Commit point label 2 Construct:\n");
308 for (String label : CPLabel2Construct.keySet()) {
309 sb.append(label + "\t" + CPLabel2Construct.get(label) + "\n");
311 return sb.toString();