Add library.
[wrappi.git] / generator-lib / wrappi_types.ml
1 (* wrappi
2  * Copyright (C) 2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  *)
18
19 open Camlp4.PreCast
20
21 open Wrappi_utils
22
23 open Printf
24
25 type ptype =
26   | TBool
27   | TBuffer
28   | TEnum of string
29   | TFile
30   | THash of ptype
31   | TInt
32   | TInt32
33   | TInt64
34   | TList of ptype
35   | TNullable of ptype
36   | TString
37   | TStruct of string
38   | TTypedef of string
39   | TUInt32
40   | TUInt64
41   | TUnion of string
42
43 type prec
44
45 type parameter = string * ptype * prec option
46
47 type rtype = RVoid | Return of ptype
48
49 type ftype = rtype * parameter list * parameter list
50
51 type c_code = string
52
53 type entry_point = {
54   ep_loc : Camlp4.PreCast.Loc.t;
55   ep_local : bool;
56   ep_name : string;
57   ep_ftype : ftype;
58   ep_code : c_code option;
59 }
60
61 type typedef = {
62   td_loc : Camlp4.PreCast.Loc.t;
63   td_name : string;
64   td_type : ptype;
65 }
66
67 type enum = {
68   en_loc : Camlp4.PreCast.Loc.t;
69   en_name : string;
70   en_identifiers : string array;
71 }
72
73 type struct_decl = {
74   sd_loc : Camlp4.PreCast.Loc.t;
75   sd_name : string;
76   sd_identifiers : string array;
77   sd_fields : ptype array;
78 }
79
80 type union = {
81   un_loc : Camlp4.PreCast.Loc.t;
82   un_name : string;
83   un_identifiers : string array;
84   un_fields : ptype array;
85 }
86
87 type api = {
88   api_typedefs : typedef StringMap.t;
89   api_enums : enum StringMap.t;
90   api_structs : struct_decl StringMap.t;
91   api_unions : union StringMap.t;
92   api_entry_points : entry_point StringMap.t;
93 }
94
95 let iter xs f =
96   let xs = StringMap.bindings xs in
97   let cmp (a, _) (b, _) = compare a b in
98   let xs = List.sort cmp xs in
99   List.iter (fun (_, x) -> f x) xs
100
101 let iter_typedefs { api_typedefs = tds } f = iter tds f
102 let iter_enums { api_enums = ens } f = iter ens f
103 let iter_structs { api_structs = sds } f = iter sds f
104 let iter_unions { api_unions = uns } f = iter uns f
105 let iter_entry_points { api_entry_points = eps } f = iter eps f
106
107 let rec string_of_ptype = function
108   | TBool -> "bool"
109   | TBuffer -> "buffer"
110   | TEnum name -> sprintf "enum %s" name
111   | TFile -> "file"
112   | THash t -> sprintf "hash(%s)" (string_of_ptype t)
113   | TInt -> "int"
114   | TInt32 -> "int32"
115   | TInt64 -> "int64"
116   | TList t -> sprintf "list(%s)" (string_of_ptype t)
117   | TNullable t -> sprintf "nullable(%s)" (string_of_ptype t)
118   | TString -> "string"
119   | TStruct name -> sprintf "struct %s" name
120   | TTypedef name -> name
121   | TUInt32 -> "uint32"
122   | TUInt64 -> "uint64"
123   | TUnion name -> sprintf "union %s" name
124 let string_of_rtype = function
125   | RVoid -> "void"
126   | Return t -> string_of_ptype t
127 let string_of_parameter (name, t, _) =
128   sprintf "%s %s" (string_of_ptype t) name
129 let string_of_parameters params =
130   sprintf "(%s)" (String.concat ", " (List.map string_of_parameter params))
131 let string_of_ftype (ret, req, opt) =
132   sprintf "%s %s %s"
133     (string_of_rtype ret) (string_of_parameters req) (string_of_parameters opt)
134 let string_of_c_code code = code
135
136 let string_of_typedef td =
137   sprintf "typedef %s %s" td.td_name (string_of_ptype td.td_type)
138
139 let string_of_enum en =
140   sprintf "enum %s {%s}" en.en_name
141     (String.concat ", " (Array.to_list en.en_identifiers))
142
143 let string_of_struct sd = assert false
144 let string_of_union un = assert false
145
146 let string_of_entry_point ep =
147   sprintf "entry_point%s %s %s <<%s>>"
148     (*(Loc.to_string ep.ep_loc)*)
149     (if ep.ep_local then " local" else "")
150     ep.ep_name
151     (string_of_ftype ep.ep_ftype)
152     (match ep.ep_code with
153     | None -> "/* implicit */"
154     | Some code -> string_of_c_code code)