public final ArrayList<VariableDeclaration> declState;
public final Code initState;
public final Code copyState;
+ public final Code clearState;
public final Code finalState;
public final Code printState;
public final ArrayList<CommutativityRule> commutativityRules;
public final boolean autoGenInitial;
// Whether we have auto-gen the state copying code
public final boolean autoGenCopy;
+ // Whether we have auto-gen the state clearing code
+ public final boolean autoGenClear;
// Whether we have auto-gen the state printing code
public final boolean autoGenPrint;
declState = new ArrayList<VariableDeclaration>();
initState = new Code();
copyState = new Code();
+ clearState = new Code();
finalState = new Code();
printState = new Code();
commutativityRules = new ArrayList<CommutativityRule>();
copyState.addLines(code);
}
+ autoGenClear = clearState.isEmpty();
+ if (autoGenClear) {
+ Code code = generateAutoClearFunction();
+ clearState.addLines(code);
+ }
+
autoGenPrint = printState.isEmpty();
if (autoGenPrint) {
Code code = generateAutoPrintFunction();
return code;
}
+
+ /**
+ * <p>
+ * This function will automatically generate the clear statements for
+ * supported types if the user has not defined the "@Clear" primitive
+ * </p>
+ *
+ * @return The auto-generated state copy statements
+ * @throws WrongAnnotationException
+ */
+ private Code generateAutoClearFunction() throws WrongAnnotationException {
+ Code code = new Code();
+ if (emptyState) // Empty state should have empty copy function
+ return code;
+
+ // FIXME: Just try our best to generate recycling statements
+ for (VariableDeclaration decl : declState) {
+ String type = decl.type;
+ String name = decl.name;
+ if (type.equals("IntList *") || type.equals("IntSet *")
+ || type.equals("IntMap *")) {
+ // Supported pointer types
+ // if (stack) delete stack;
+ code.addLine("if (" + name + ") delete " + name + ";");
+ }
+ }
+
+ return code;
+ }
/**
* <p>
if (!name.equals(SpecNaming.DeclareState)
&& !name.equals(SpecNaming.InitalState)
&& !name.equals(SpecNaming.CopyState)
+ && !name.equals(SpecNaming.ClearState)
&& !name.equals(SpecNaming.FinalState)
&& !name.equals(SpecNaming.Commutativity)
&& !name.equals(SpecNaming.PrintState)) {
initState.addLines(primitive.contents);
} else if (name.equals(SpecNaming.CopyState)) {
copyState.addLines(primitive.contents);
+ } else if (name.equals(SpecNaming.ClearState)) {
+ clearState.addLines(primitive.contents);
} else if (name.equals(SpecNaming.FinalState)) {
finalState.addLines(primitive.contents);
} else if (name.equals(SpecNaming.PrintState)) {