minor fix
[cdsspec-compiler.git] / src / edu / uci / eecs / specExtraction / GlobalConstruct.java
index f49911be2f51497a3268d79bc699fd6407d85974..b2e52a03d5b333626b5b014a8e433a91d4c22184 100644 (file)
@@ -25,7 +25,14 @@ public class GlobalConstruct extends Construct {
        public final Code printState;
        public final ArrayList<CommutativityRule> commutativityRules;
 
        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;
        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 {
 
        public GlobalConstruct(File file, int beginLineNum,
                        ArrayList<String> annotations) throws WrongAnnotationException {
@@ -39,13 +46,75 @@ public class GlobalConstruct extends Construct {
 
                processAnnotations(annotations);
 
 
                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);
                        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;
        }
 
        /**
        }
 
        /**
@@ -54,11 +123,13 @@ public class GlobalConstruct extends Construct {
         * supported types if the user has not defined the "@Copy" primitive
         * </p>
         * 
         * 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();
         * @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;
                for (VariableDeclaration decl : declState) {
                        String type = decl.type;
                        String name = decl.name;
@@ -97,6 +168,28 @@ public class GlobalConstruct extends Construct {
                return code;
        }
 
                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
        /**
         * <p>
         * Assert that the global state primitive is valid; if not, throws an