Added c ast for constant

Also print only short names for struct fields
This commit is contained in:
Cédric Pasteur 2011-09-06 14:20:57 +02:00
parent e7e81f2637
commit 4cd506f3db
2 changed files with 20 additions and 5 deletions

View File

@ -112,6 +112,8 @@ type cdecl =
| Cdecl_struct of string * (string * cty) list
(** C function declaration. *)
| Cdecl_function of string * cty * (string * cty) list
(** C constant declaration (alias, name)*)
| Cdecl_constant of string * cty * cexpr
(** C function definitions *)
type cfundef = {
@ -170,6 +172,9 @@ let cname_of_qn qn =
let pp_qualname fmt q =
pp_string fmt (cname_of_qn q)
let pp_shortname fmt q =
pp_string fmt q.name
let rec pp_cty fmt cty = match cty with
| Cty_int -> fprintf fmt "int"
| Cty_float -> fprintf fmt "float"
@ -245,15 +250,22 @@ and pp_cexpr fmt ce = match ce with
| Cconst c -> pp_cconst fmt c
| Cvar s -> pp_string fmt s
| Cderef e -> fprintf fmt "*%a" pp_cexpr e
| Cfield (Cderef e, f) -> fprintf fmt "%a->%a" pp_cexpr e pp_qualname f
| Cfield (e, f) -> fprintf fmt "%a.%a" pp_cexpr e pp_qualname f
| Cfield (Cderef e, f) -> fprintf fmt "%a->%a" pp_cexpr e pp_shortname f
| Cfield (e, f) -> fprintf fmt "%a.%a" pp_cexpr e pp_shortname f
| Carray (e1, e2) -> fprintf fmt "%a[%a]" pp_cexpr e1 pp_cexpr e2
and pp_cconst_expr fmt ce = match ce with
| Cstructlit (_, el) ->
fprintf fmt "{@[%a@]}" (pp_list1 pp_cconst_expr ",") el
| Carraylit el ->
fprintf fmt "{@[%a@]}" (pp_list1 pp_cconst_expr ",") el
| _ -> pp_cexpr fmt ce
and pp_clhs fmt clhs = match clhs with
| CLvar s -> pp_string fmt s
| CLderef lhs' -> fprintf fmt "*%a" pp_clhs lhs'
| CLfield (CLderef lhs, f) -> fprintf fmt "%a->%a" pp_clhs lhs pp_qualname f
| CLfield (lhs, f) -> fprintf fmt "%a.%a" pp_clhs lhs pp_qualname f
| CLfield (CLderef lhs, f) -> fprintf fmt "%a->%a" pp_clhs lhs pp_shortname f
| CLfield (lhs, f) -> fprintf fmt "%a.%a" pp_clhs lhs pp_shortname f
| CLarray (lhs, e) ->
fprintf fmt "%a[%a]"
pp_clhs lhs
@ -281,6 +293,9 @@ let pp_cdecl fmt cdecl = match cdecl with
| Cdecl_function (n, retty, args) ->
fprintf fmt "@[<v>%a %a(@[<hov>%a@]);@ @]@\n"
pp_cty retty pp_string n pp_param_list args
| Cdecl_constant (n, cty, ce) ->
fprintf fmt "@[<v>const %a %a = %a;@ @]@\n"
pp_cty cty pp_string n pp_cconst_expr ce
let pp_cdef fmt cdef = match cdef with
| Cfundef cfd ->

View File

@ -771,7 +771,7 @@ let cdefs_and_cdecls_of_type_decl otd =
cdecl_of_cfundef of_string_fun;
cdecl_of_cfundef to_string_fun])
| Type_struct fl ->
let decls = List.map (fun f -> cname_of_qn f.Signature.f_name,
let decls = List.map (fun f -> cname_of_name f.Signature.f_name.name,
ctype_of_otype f.Signature.f_type) fl in
let decl = Cdecl_struct (name, decls) in
([], [decl])