[Libguestfs] [PATCH libnbd 6/9] generator: Add non-optional Flags type.

Richard W.M. Jones
2019-08-10T13:02:45Z

This works just like OFlags but is a non-optional argument.
---
 generator/generator | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/generator/generator b/generator/generator
index 2b37cea..96d1148 100755
--- a/generator/generator
+++ b/generator/generator
@@ -856,6 +856,7 @@ and arg  | BytesPersistOut of string * string
 | Closure of closure       (* function pointer + void *opaque *)
 | Enum of string * enum    (* enum/union type, int in C *)
+| Flags of string * flags  (* flags, uint32_t in C *)
 | Int of string            (* small int *)
 | Int64 of string          (* 64 bit signed int *)
 | Path of string           (* filename or path *)
@@ -3324,6 +3325,7 @@ let rec name_of_arg = function
 | Closure { cbname } ->
    [ sprintf "%s_callback" cbname; sprintf "%s_user_data"
cbname ]
 | Enum (n, _) -> [n]
+| Flags (n, _) -> [n]
 | Int n -> [n]
 | Int64 n -> [n]
 | Path n -> [n]
@@ -3371,6 +3373,9 @@ let rec print_arg_list ?(handle = false) ?(types = true)
args optargs        | Enum (n, _) ->
          if types then pr "int ";
          pr "%s" n
+      | Flags (n, _) ->
+         if types then pr "uint32_t ";
+         pr "%s" n
       | Int n ->
          if types then pr "int ";
          pr "%s" n
@@ -3748,6 +3753,7 @@ let generate_lib_api_c ()        | BytesPersistOut (n,
count) -> pr " %s=<buf> %s=%%zu" n count
       | Closure { cbname } -> pr " %s=<fun>" cbname
       | Enum (n, _) -> pr " %s=%%d" n
+      | Flags (n, _) -> pr " %s=0x%%x" n
       | Int n -> pr " %s=%%d" n
       | Int64 n -> pr " %s=%%\" PRIi64 \"" n
       | SockAddrAndLen (n, len) -> pr " %s=<sockaddr>
%s=%%d" n len
@@ -3772,6 +3778,7 @@ let generate_lib_api_c ()        | BytesPersistOut (_,
count) -> pr ", %s" count
       | Closure { cbname } -> ()
       | Enum (n, _) -> pr ", %s" n
+      | Flags (n, _) -> pr ", %s" n
       | Int n -> pr ", %s" n
       | Int64 n -> pr ", %s" n
       | SockAddrAndLen (_, len) -> pr ", (int) %s" len
@@ -4255,6 +4262,9 @@ let print_python_binding name { args; optargs; ret;
may_set_error }      | Closure { cbname } ->
        pr "  PyObject *%s_user_data;\n" cbname
     | Enum (n, _) -> pr "  int %s;\n" n
+    | Flags (n, _) ->
+       pr "  uint32_t %s_u32;\n" n;
+       pr "  unsigned int %s; /* really uint32_t */\n" n
     | Int n -> pr "  int %s;\n" n
     | Int64 n ->
        pr "  int64_t %s_i64;\n" n;
@@ -4298,6 +4308,7 @@ let print_python_binding name { args; optargs; ret;
may_set_error }      | BytesPersistOut (_, count) -> pr "
\"O\""
     | Closure _ -> pr " \"O\""
     | Enum _ -> pr " \"i\""
+    | Flags _ -> pr " \"I\""
     | Int n -> pr " \"i\""
     | Int64 n -> pr " \"L\""
     | Path n -> pr " \"O&\""
@@ -4323,6 +4334,7 @@ let print_python_binding name { args; optargs; ret;
may_set_error }      | BytesOut (_, count) -> pr ", &%s" count
     | Closure { cbname } -> pr ", &%s_user_data" cbname
     | Enum (n, _) -> pr ", &%s" n
+    | Flags (n, _) -> pr ", &%s" n
     | Int n -> pr ", &%s" n
     | Int64 n -> pr ", &%s" n
     | Path n -> pr ", PyUnicode_FSConverter, &py_%s" n
@@ -4358,6 +4370,7 @@ let print_python_binding name { args; optargs; ret;
may_set_error }             pr "    return NULL;\n";
            pr "  }\n"
     | Enum _ -> ()
+    | Flags (n, _) -> pr "  %s_u32 = %s;\n" n n
     | Int _ -> ()
     | Int64 n -> pr "  %s_i64 = %s;\n" n n
     | Path n ->
@@ -4391,6 +4404,7 @@ let print_python_binding name { args; optargs; ret;
may_set_error }         pr ", %s_%s_wrapper" name cbname;
        pr ", %s_user_data" cbname
     | Enum (n, _) -> pr ", %s" n
+    | Flags (n, _) -> pr ", %s_u32" n
     | Int n -> pr ", %s" n
     | Int64 n -> pr ", %s_i64" n
     | Path n -> pr ", %s" n
@@ -4427,6 +4441,7 @@ let print_python_binding name { args; optargs; ret;
may_set_error }      | BytesPersistIn _ | BytesPersistOut _
     | Closure _
     | Enum _
+    | Flags _
     | Int _
     | Int64 _
     | Path _
@@ -4468,6 +4483,7 @@ let print_python_binding name { args; optargs; ret;
may_set_error }      | BytesPersistIn _ | BytesOut _ | BytesPersistOut _ ->
()
     | Closure _ -> ()
     | Enum _ -> ()
+    | Flags _ -> ()
     | Int _ -> ()
     | Int64 _ -> ()
     | Path n ->
@@ -4630,6 +4646,7 @@ class NBD (object):
           | BytesOut (_, count) -> count
           | Closure { cbname } -> cbname
           | Enum (n, _) -> n
+          | Flags (n, _) -> n
           | Int n -> n
           | Int64 n -> n
           | Path n -> n
@@ -4706,6 +4723,7 @@ and ocaml_arg_to_string = function
   | Closure { cbargs } ->
      sprintf "(%s)" (ocaml_closuredecl_to_string cbargs)
   | Enum (_, { enum_prefix }) -> sprintf "%s.t" enum_prefix
+  | Flags (_, { flag_prefix }) -> sprintf "%s.t" flag_prefix
   | Int _ -> "int"
   | Int64 _ -> "int64"
   | Path _ -> "string"
@@ -4753,6 +4771,7 @@ let ocaml_name_of_arg = function
   | BytesPersistOut (n, len) -> n
   | Closure { cbname } -> cbname
   | Enum (n, _) -> n
+  | Flags (n, _) -> n
   | Int n -> n
   | Int64 n -> n
   | Path n -> n
@@ -5212,6 +5231,8 @@ let print_ocaml_binding (name, { args; optargs; ret })
pr "  const void *%s_callback = %s_%s_wrapper;\n" cbname name cbname
     | Enum (n, { enum_prefix }) ->
        pr "  int %s = %s_val (%sv);\n" n enum_prefix n
+    | Flags (n, { flag_prefix }) ->
+       pr "  uint32_t %s = %s_val (%sv);\n" n flag_prefix n
     | Int n ->
        pr "  int %s = Int_val (%sv);\n" n n
     | Int64 n ->
@@ -5270,6 +5291,7 @@ let print_ocaml_binding (name, { args; optargs; ret })
| BytesPersistOut _
     | Closure _
     | Enum _
+    | Flags _
     | Int _
     | Int64 _
     | Path _
--
2.22.0