|
|
|
@ -69,11 +69,18 @@ type cty =
|
|
|
|
|
| Cty_arr of int * cty (** A static array of the specified size. *)
|
|
|
|
|
| Cty_void (** Well, [void] is not really a C type. *)
|
|
|
|
|
|
|
|
|
|
type cvardecl = {
|
|
|
|
|
vd_name : string;
|
|
|
|
|
vd_ty : cty;
|
|
|
|
|
vd_static : bool;
|
|
|
|
|
vd_value : cexpr option
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(** A C block: declarations and statements. In source code form, it begins with
|
|
|
|
|
variable declarations before a list of semicolon-separated statements, the
|
|
|
|
|
whole thing being enclosed in curly braces. *)
|
|
|
|
|
type cblock = {
|
|
|
|
|
var_decls : (string * cty) list;
|
|
|
|
|
and cblock = {
|
|
|
|
|
var_decls : cvardecl list;
|
|
|
|
|
(** Variable declarations, where each declaration consists of a variable
|
|
|
|
|
name and the associated C type. *)
|
|
|
|
|
block_body : cstm list;
|
|
|
|
@ -169,6 +176,15 @@ and cfile_desc =
|
|
|
|
|
|
|
|
|
|
(******************************)
|
|
|
|
|
|
|
|
|
|
let mk_vardecl ?(static = false) ?(value = None) name ty =
|
|
|
|
|
{ vd_name = name; vd_ty = ty; vd_static = static; vd_value = value }
|
|
|
|
|
|
|
|
|
|
let mk_vardecl_val ?(static = false) name ty value =
|
|
|
|
|
mk_vardecl ~static:static ~value:(Some value) name ty
|
|
|
|
|
|
|
|
|
|
let vardecl_of_cvars cvars =
|
|
|
|
|
List.map (fun (name, ty) -> mk_vardecl name ty) cvars
|
|
|
|
|
|
|
|
|
|
(** {3 Pretty-printing of the C ast.} *)
|
|
|
|
|
|
|
|
|
|
(** [pp_list1 f sep fmt l] pretty-prints into the Format.formatter [fmt]
|
|
|
|
@ -235,7 +251,7 @@ and pp_param_list fmt l = pp_list1 pp_vardecl "," fmt l
|
|
|
|
|
and pp_var_list fmt l = pp_list pp_vardecl ";" fmt l
|
|
|
|
|
|
|
|
|
|
let rec pp_cblock fmt cb =
|
|
|
|
|
let pp_varlist = pp_list pp_vardecl ";" in
|
|
|
|
|
let pp_varlist = pp_list pp_cvardecl ";" in
|
|
|
|
|
fprintf fmt "%a%a" pp_varlist cb.var_decls pp_cstm_list cb.block_body
|
|
|
|
|
and pp_cstm_list fmt stml = pp_list pp_cstm ";" fmt stml
|
|
|
|
|
and pp_cstm fmt stm = match stm with
|
|
|
|
@ -308,6 +324,16 @@ and pp_cconst fmt cconst = match cconst with
|
|
|
|
|
| Ctag t -> pp_string fmt t
|
|
|
|
|
| Cstrlit t -> fprintf fmt "\"%s\"" (String.escaped t)
|
|
|
|
|
|
|
|
|
|
and pp_cvardecl fmt vd =
|
|
|
|
|
if vd.vd_static then
|
|
|
|
|
fprintf fmt "static ";
|
|
|
|
|
fprintf fmt "%a" pp_vardecl (vd.vd_name, vd.vd_ty);
|
|
|
|
|
match vd.vd_value with
|
|
|
|
|
| Some e -> fprintf fmt " = %a" pp_cexpr e
|
|
|
|
|
| None -> ()
|
|
|
|
|
|
|
|
|
|
and pp_cvardecl_list fmt l = pp_list1 pp_vardecl "," fmt l
|
|
|
|
|
|
|
|
|
|
let pp_cdecl fmt cdecl = match cdecl with
|
|
|
|
|
| Cdecl_enum (s, sl) ->
|
|
|
|
|
fprintf fmt "@[<v>@[<v 2>typedef enum {@ %a@]@ } %a;@ @]@\n"
|
|
|
|
|