X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=src%2Fedu%2Fuci%2Feecs%2FspecExtraction%2FGlobalConstruct.java;h=b2e52a03d5b333626b5b014a8e433a91d4c22184;hb=579524b6cd8994427d50cf604ee130493f59b4d3;hp=f49911be2f51497a3268d79bc699fd6407d85974;hpb=83eae09be64a115aad7e6822d03cc87e603a60ea;p=cdsspec-compiler.git diff --git a/src/edu/uci/eecs/specExtraction/GlobalConstruct.java b/src/edu/uci/eecs/specExtraction/GlobalConstruct.java index f49911b..b2e52a0 100644 --- a/src/edu/uci/eecs/specExtraction/GlobalConstruct.java +++ b/src/edu/uci/eecs/specExtraction/GlobalConstruct.java @@ -25,7 +25,14 @@ public class GlobalConstruct extends Construct { public final Code printState; public final ArrayList 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 annotations) throws WrongAnnotationException { @@ -39,13 +46,75 @@ public class GlobalConstruct extends Construct { 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); + } + } + + /** + *

+ * This function will automatically generate the initial statements for + * supported types if the user has not defined the "@Initial" primitive + *

+ * + * @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; } /** @@ -54,11 +123,13 @@ public class GlobalConstruct extends Construct { * supported types if the user has not defined the "@Copy" primitive *

* - * @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; @@ -97,6 +168,28 @@ public class GlobalConstruct extends Construct { return code; } + /** + *

+ * This function will automatically generate the printing statements for + * supported types if the user has not defined the "@Print" primitive + *

+ * + * @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; + } + /** *

* Assert that the global state primitive is valid; if not, throws an