Anonymous avatar Anonymous committed 186cace

enum: ?generate_always

Comments (0)

Files changed (3)

 for each next element.  So, here A = 0 (start from 0), B = 3 (explicitely
 specified with ~v:3), C = 4 (not specified, so "previous value + 1").
 This behaviour matches C enum definitions.
+  Only enums that are used in cstructs are codegenerated.  Optional boolean
+argument "generate_always" passed to "enum" function changes this behaviour.
+
 
   Then -- structures:
 

lib/cstruct_codegen.ml

 (************************************************************)
 
 
+value generate_always_queue = Queue.create ()
+;
+
+
 module Enum
  =
   struct
     ;
 
     value enum
+      ?(generate_always=False)
       name (enumtype : enum_type 'a) (elts : list (enum_elt 'a))
      : string -> cfielddef
      =
-      let ftype = Fenum
+      let fe =
         { enum_repr = enumtype.et_ftype_int
         ; enum_name = String.uncapitalize name
         ; enum_elts =
               elts
         }
       in
+      let () =
+        if generate_always
+        then Queue.push fe generate_always_queue
+        else ()
+      in
+      let ftype = Fenum fe
+      in
         fun fieldname -> { fname = fieldname ; ftype = ftype }
     ;
 
 
 value codegen_enums outch =
   let generated = Hashtbl.create 7 in
+  let maybe_gen fe =
+    if not (Hashtbl.mem generated fe.enum_name)
+    then
+      ( codegen_enum outch fe
+      ; Hashtbl.add generated fe.enum_name ()
+      )
+    else
+      ()
+  in
+  let () = Queue.iter maybe_gen generate_always_queue in
   Queue.iter
     (fun (_csname, csfields) ->
        List.iter
          (fun fld ->
             match fld.ftype with
             [ Fint _ -> ()
-            | Fenum fe ->
-                if not (Hashtbl.mem generated fe.enum_name)
-                then
-                  ( codegen_enum outch fe
-                  ; Hashtbl.add generated fe.enum_name ()
-                  )
-                else
-                  ()
+            | Fenum fe -> maybe_gen fe
             ]
          )
          csfields

lib/cstruct_codegen.mli

     type enum_elt 'a;
     value elt : ?v : 'a -> string -> enum_elt 'a;
 
-    value enum : string -> enum_type 'a -> list (enum_elt 'a) ->
+    value enum : ?generate_always:bool ->
+                 string -> enum_type 'a -> list (enum_elt 'a) ->
                  (string -> cfielddef)
     ;
   end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.