public final Code printState;
public final ArrayList<CommutativityRule> commutativityRules;
+ // Whether the state declaration is empty
+ public final boolean emptyState;
+ // Whether we have auto-gen the state initialization code
+ public final boolean autoGenInitial;
+ // Whether we have auto-gen the state copying code
public final boolean autoGenCopy;
+ // Whether we have auto-gen the state printing code
+ public final boolean autoGenPrint;
public GlobalConstruct(File file, int beginLineNum,
ArrayList<String> annotations) throws WrongAnnotationException {
processAnnotations(annotations);
- if (copyState.isEmpty()) {
+ emptyState = declState.isEmpty();
+ if (emptyState) {
+ WrongAnnotationException.warning(file, beginLineNum,
+ "The state is empty. Make sure that's what you want!");
+ }
+
+ autoGenInitial = initState.isEmpty();
+ if (autoGenInitial) {
+ Code code = generateAutoInitalFunction();
+ initState.addLines(code);
+ }
+
+ autoGenCopy = copyState.isEmpty();
+ if (autoGenCopy) {
Code code = generateAutoCopyFunction();
copyState.addLines(code);
- autoGenCopy = true;
- } else {
- autoGenCopy = false;
+ }
+
+ autoGenPrint = printState.isEmpty();
+ if (autoGenPrint) {
+ Code code = generateAutoPrintFunction();
+ printState.addLines(code);
}
}
+ /**
+ * <p>
+ * This function will automatically generate the initial statements for
+ * supported types if the user has not defined the "@Initial" primitive
+ * </p>
+ *
+ * @return The auto-generated state intialization statements
+ * @throws WrongAnnotationException
+ */
+ private Code generateAutoInitalFunction() throws WrongAnnotationException {
+ Code code = new Code();
+ if (emptyState) // Empty state should have empty initial function
+ return code;
+ for (VariableDeclaration decl : declState) {
+ String type = decl.type;
+ String name = decl.name;
+ // Primitive types
+ if (type.equals("int") || type.equals("unsigned")
+ || type.equals("unsigned int")
+ || type.equals("int unsigned") || type.equals("double")
+ || type.equals("double") || type.equals("bool")) {
+ // x = 0;
+ code.addLine(name + " = 0;");
+ } else if (type.equals("IntList") || type.equals("IntSet")
+ || type.equals("IntMap")) {
+ // Supported types
+ // q = IntList();
+ code.addLine(name + " = " + type + "();");
+ } else if (type.equals("IntList *") || type.equals("IntSet *")
+ || type.equals("IntMap *")) {
+ // Supported pointer types
+ // q = new IntList;
+ String originalType = SpecUtils.trimSpace(type
+ .replace('*', ' '));
+ code.addLine(name + " = new " + originalType + "();");
+ } else {
+ WrongAnnotationException
+ .err(file,
+ beginLineNum,
+ "You have types in the state declaration that we do not support auto-gen initial function.");
+ }
+ }
+
+ return code;
+ }
+
/**
* <p>
* This function will automatically generate the copy statements for
* supported types if the user has not defined the "@Copy" primitive
* </p>
*
- * @return The auto-generated copy statements
+ * @return The auto-generated state copy statements
* @throws WrongAnnotationException
*/
private Code generateAutoCopyFunction() throws WrongAnnotationException {
Code code = new Code();
+ if (emptyState) // Empty state should have empty copy function
+ return code;
for (VariableDeclaration decl : declState) {
String type = decl.type;
String name = decl.name;
if (type.equals("int") || type.equals("unsigned")
|| type.equals("unsigned int")
|| type.equals("int unsigned") || type.equals("double")
- || type.equals("double")) {
+ || type.equals("double") || type.equals("bool")) {
// NEW->x = OLD->x;
code.addLine(SpecNaming.NewStateInst + "->" + name + " = "
+ SpecNaming.OldStateInst + "->" + name + ";");
return code;
}
+ /**
+ * <p>
+ * This function will automatically generate the printing statements for
+ * supported types if the user has not defined the "@Print" primitive
+ * </p>
+ *
+ * @return The auto-generated state printing statements
+ * @throws WrongAnnotationException
+ */
+ private Code generateAutoPrintFunction() throws WrongAnnotationException {
+ Code code = new Code();
+ if (emptyState) // Empty state should have empty printing function
+ return code;
+ for (VariableDeclaration decl : declState) {
+ String type = decl.type;
+ String name = decl.name;
+ code.addLines(SpecUtils.generatePrintStatement(type, name));
+ }
+
+ return code;
+ }
+
/**
* <p>
* Assert that the global state primitive is valid; if not, throws an