2 * Copyright (C) 2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
45 type parameter = string * ptype * prec option
47 type rtype = RErr | Return of ptype
49 type ftype = rtype * parameter list * parameter list
54 ep_loc : Camlp4.PreCast.Loc.t;
57 ep_code : c_code option;
61 td_loc : Camlp4.PreCast.Loc.t;
67 en_loc : Camlp4.PreCast.Loc.t;
69 en_identifiers : string array;
73 sd_loc : Camlp4.PreCast.Loc.t;
75 sd_identifiers : string array;
76 sd_fields : ptype array;
80 un_loc : Camlp4.PreCast.Loc.t;
82 un_identifiers : string array;
83 un_fields : ptype array;
87 api_typedefs : typedef StringMap.t;
88 api_enums : enum StringMap.t;
89 api_structs : struct_decl StringMap.t;
90 api_unions : union StringMap.t;
91 api_entry_points : entry_point StringMap.t;
95 let xs = StringMap.bindings xs in
96 let cmp (a, _) (b, _) = compare a b in
97 let xs = List.sort cmp xs in
98 List.iter (fun (_, x) -> f x) xs
100 let iter_typedefs { api_typedefs = tds } f = iter tds f
101 let iter_enums { api_enums = ens } f = iter ens f
102 let iter_structs { api_structs = sds } f = iter sds f
103 let iter_unions { api_unions = uns } f = iter uns f
104 let iter_entry_points { api_entry_points = eps } f = iter eps f
106 let rec string_of_ptype = function
108 | TBuffer -> "buffer"
109 | TEnum name -> sprintf "enum %s" name
111 | THash t -> sprintf "hash(%s)" (string_of_ptype t)
115 | TList t -> sprintf "list(%s)" (string_of_ptype t)
116 | TNullable t -> sprintf "nullable(%s)" (string_of_ptype t)
117 | TString -> "string"
118 | TStruct name -> sprintf "struct %s" name
119 | TTypedef name -> name
120 | TUInt32 -> "uint32"
121 | TUInt64 -> "uint64"
122 | TUnion name -> sprintf "union %s" name
123 let string_of_rtype = function
125 | Return t -> string_of_ptype t
126 let string_of_parameter (name, t, _) =
127 sprintf "%s %s" (string_of_ptype t) name
128 let string_of_parameters params =
129 sprintf "(%s)" (String.concat ", " (List.map string_of_parameter params))
130 let string_of_ftype (ret, req, opt) =
132 (string_of_rtype ret) (string_of_parameters req) (string_of_parameters opt)
133 let string_of_c_code code = code
135 let string_of_typedef td =
136 sprintf "typedef %s %s" td.td_name (string_of_ptype td.td_type)
138 let string_of_enum en =
139 sprintf "enum %s {%s}" en.en_name
140 (String.concat ", " (Array.to_list en.en_identifiers))
142 let string_of_struct sd = assert false
143 let string_of_union un = assert false
145 let string_of_entry_point ep =
146 sprintf "entry_point %s %s <<%s>>"
147 (*(Loc.to_string ep.ep_loc)*)
149 (string_of_ftype ep.ep_ftype)
150 (match ep.ep_code with
151 | None -> "/* implicit */"
152 | Some code -> string_of_c_code code)