add destructors
[cdsspec-compiler.git] / src / edu / uci / eecs / specExtraction / GlobalConstruct.java
index b2e52a03d5b333626b5b014a8e433a91d4c22184..d8f6dd42491bfaaa62b17b9e3c06efd8840a4e6e 100644 (file)
@@ -21,6 +21,7 @@ public class GlobalConstruct extends Construct {
        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;
@@ -31,6 +32,8 @@ public class GlobalConstruct extends Construct {
        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;
 
@@ -40,6 +43,7 @@ public class GlobalConstruct extends Construct {
                declState = new ArrayList<VariableDeclaration>();
                initState = new Code();
                copyState = new Code();
+               clearState = new Code();
                finalState = new Code();
                printState = new Code();
                commutativityRules = new ArrayList<CommutativityRule>();
@@ -64,6 +68,12 @@ public class GlobalConstruct extends Construct {
                        copyState.addLines(code);
                }
 
+               autoGenClear = clearState.isEmpty();
+               if (autoGenClear) {
+                       Code code = generateAutoClearFunction();
+                       clearState.addLines(code);
+               }
+
                autoGenPrint = printState.isEmpty();
                if (autoGenPrint) {
                        Code code = generateAutoPrintFunction();
@@ -167,6 +177,35 @@ public class GlobalConstruct extends Construct {
 
                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>
@@ -209,6 +248,7 @@ public class GlobalConstruct extends Construct {
                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)) {
@@ -292,6 +332,8 @@ public class GlobalConstruct extends Construct {
                                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)) {