@@ -262,3 +262,120 @@ let function_ ?(attrs=[]) ?(variadic=false) ?(return=`Void) args : t =
262262 }
263263
264264let is_function : t -> Bool.t = function `Function _ -> true | _ -> false
265+
266+
267+ let pp_comma ppf () = Format. fprintf ppf " , "
268+
269+ let pp_attr ppf = function
270+ | {Attr. name; args =[] } -> Format. fprintf ppf " %s" name
271+ | {Attr. name; args} ->
272+ Format. fprintf ppf " %s(%a)" name
273+ (Format. pp_print_list ~pp_sep: pp_comma
274+ Format. pp_print_string) args
275+
276+ let pp_attr_list ppf xs =
277+ Format. pp_print_list ~pp_sep: pp_comma pp_attr ppf xs
278+
279+ let pp_attrs ppf = function
280+ | [] -> ()
281+ | attrs ->
282+ Format. fprintf ppf " __attribute__((%a)) "
283+ pp_attr_list attrs
284+
285+ let pp_qualifier name ppf = function
286+ | false -> ()
287+ | true -> Format. fprintf ppf " %s " name
288+
289+ let pp_cv ppf {Qualifier. const;volatile} =
290+ Format. fprintf ppf " %a%a"
291+ (pp_qualifier " const" ) const
292+ (pp_qualifier " volatile" ) volatile
293+
294+ let pp_cvr ppf {Qualifier. const;volatile;restrict} =
295+ Format. fprintf ppf " %a%a%a"
296+ (pp_qualifier " const" ) const
297+ (pp_qualifier " volatile" ) volatile
298+ (pp_qualifier " restrict" ) restrict
299+
300+ let pp_size ppf = function
301+ | None -> ()
302+ | Some size -> Format. fprintf ppf " %d" size
303+
304+ let pp_enum_value ppf = function
305+ | None -> Format. fprintf ppf " ,"
306+ | Some v -> Format. fprintf ppf " = %Ld," v
307+
308+ let pp_enum_field ppf (name ,value ) =
309+ Format. fprintf ppf " @,%s%a," name pp_enum_value value
310+
311+ let rec pp_enum_fields ppf =
312+ List. iter ~f: (pp_enum_field ppf)
313+
314+ let string_of_basic t = match (t : basic ) with
315+ | `schar -> " signed char"
316+ | `cdouble -> " double complex"
317+ | `long_double -> " long double"
318+ | `cfloat -> " float complex"
319+ | `float -> " float"
320+ | `clong_double -> " long double complex"
321+ | `ulong_long -> " unsigned long long"
322+ | `uint -> " unsigned"
323+ | `slong -> " signed long"
324+ | `bool -> " _Bool"
325+ | `double -> " double"
326+ | `slong_long -> " signed long long"
327+ | `sshort -> " signed short"
328+ | `ushort -> " unsigned short"
329+ | `char -> " char"
330+ | `sint -> " signed"
331+ | `ulong -> " unsigned long"
332+ | `uchar -> " unsigned char"
333+ | `enum fields ->
334+ Format. asprintf " @[<v>@[<v4>enum {%a@]@,}@;" pp_enum_fields fields
335+
336+ let pp_variadic ppf = function
337+ | true -> Format. fprintf ppf " , ..."
338+ | false -> ()
339+
340+ let pp_basic ppf t =
341+ Format. pp_print_string ppf (string_of_basic t)
342+
343+ let rec pp ppf t = match (t : t ) with
344+ | `Void ->
345+ Format. fprintf ppf " void"
346+ | `Array { qualifier; t ={ element; size } ; attrs } ->
347+ Format. fprintf ppf " @[<h>%a%a%a[%a]@]"
348+ pp_attrs attrs pp_cvr qualifier pp_incomplete element pp_size size
349+ | `Basic { qualifier; t; attrs } ->
350+ Format. fprintf ppf " @[<h>%a%a%a@]"
351+ pp_attrs attrs pp_cv qualifier pp_basic t
352+ | `Function { t =proto ; attrs } ->
353+ Format. fprintf ppf " @[<h>%a%a;@]"
354+ pp_proto proto pp_attrs attrs
355+ | `Pointer { qualifier; t; attrs } ->
356+ Format. fprintf ppf " @[<h>%a%a%a*@]"
357+ pp_attrs attrs pp_incomplete t pp_cvr qualifier
358+ | `Union { t= {name;fields}; attrs }
359+ | `Structure { t ={name;fields} ; attrs } as t ->
360+ let kind = if is_structure t then " struct" else " union" in
361+ Format. fprintf ppf " @[<v>@[<v4>%s %s {@,%a@]@,}@]%a;"
362+ kind name pp_fields fields pp_attrs attrs
363+ and pp_proto ppf { return; args; variadic } =
364+ Format. fprintf ppf " %a (*)(%a%a)"
365+ pp_incomplete return pp_args args pp_variadic variadic
366+ and pp_args ppf = function
367+ | [] -> Format. fprintf ppf " void"
368+ | args ->
369+ Format. pp_print_list ~pp_sep: pp_comma pp_arg ppf args
370+ and pp_arg ppf (name ,t ) =
371+ Format. fprintf ppf " %a %s" pp_incomplete t name
372+ and pp_fields ppf fields =
373+ Format. pp_print_list pp_field ppf fields
374+ and pp_field ppf (name ,t ) =
375+ Format. fprintf ppf " %a %s;" pp_incomplete t name
376+ and pp_incomplete ppf t = match (t : t ) with
377+ | `Union { t= {name} }
378+ | `Structure { t ={name} } as t ->
379+ let kind = if is_structure t then " struct" else " union" in
380+ Format. fprintf ppf " %s %s" kind name
381+ | t -> pp ppf t
0 commit comments