Combine generator subdirectories into one.
[wrappi.git] / generator / wrappi_c_xdr.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 open Wrappi_types
23 open Wrappi_boilerplate
24 open Wrappi_pr
25
26 open Printf
27
28 let inputs = ["wrappi_c_xdr.ml"]
29
30 (* Traditionally 'rpcgen' is used to generate this code.  However the
31  * glibc maintainers have shown no interest (in fact, malign influence)
32  * in doing a proper job maintaining this, resulting in bitrot and poor
33  * code being generated.  In any case the code that rpcgen makes is
34  * relatively simple.
35  *)
36
37 let pr_xdr_of_ptype n = function
38   | TBool ->
39     pr "  if (!xdr_bool (xdrs, &%s)) return FALSE;\n" n
40   | TBuffer -> assert false (* XXX not implemented *)
41   | TEnum _ ->
42     pr "  if (!xdr_int (xdrs, &%s)) return FALSE;\n" n
43   | TFile -> assert false (* XXX not implemented *)
44   | THash t -> assert false (* XXX not implemented *)
45   | TInt -> (* XXX not int, correct type depends on preconditions *)
46     pr "  if (!xdr_int (xdrs, &%s)) return FALSE;\n" n
47   | TInt32 ->
48     pr "  if (!xdr_int32_t (xdrs, &%s)) return FALSE;\n" n
49   | TInt64 ->
50     pr "  if (!xdr_int64_t (xdrs, &%s)) return FALSE;\n" n
51   | TList t -> assert false (* XXX not implemented *)
52   | TNullable TString ->
53     pr "  if (!xdr_pointer (xdrs, &%s, sizeof (unlimited_string), (xdrproc_t) xdr_unlimited_string)) return FALSE;\n" n
54   | TNullable _ -> assert false (* XXX may be implemented in future *)
55   | TString ->
56     pr "  if (!xdr_unlimited_string (xdrs, (void *) &%s)) return FALSE;\n" n
57   | TStruct sname ->
58     pr "  if (!xdr_struct_%s (xdrs, %s)) return FALSE;\n" sname n
59   | TTypedef name -> assert false (* should never happen *)
60   | TUInt32 ->
61     pr "  if (!xdr_uint32_t (xdrs, &%s)) return FALSE;\n" n
62   | TUInt64 ->
63     pr "  if (!xdr_uint64_t (xdrs, &%s)) return FALSE;\n" n
64   | TUnion uname ->
65     pr "  if (!xdr_union_%s (xdrs, %s)) return FALSE;\n" uname n
66
67 let generate_lib_xdr_c api =
68   generate_header inputs CStyle LGPLv2plus;
69
70   pr "\
71 #include <config.h>
72
73 #include <stdlib.h>
74 #include <rpc/xdr.h>
75
76 #include \"wrappi.h\"
77 #include \"internal.h\"
78
79 /* An unlimited length string. */
80 typedef char *unlimited_string;
81
82 static bool_t
83 xdr_unlimited_string (XDR *xdrs, unlimited_string *objp)
84 {
85   if (!xdr_string (xdrs, objp, ~0)) return FALSE;
86   return TRUE;
87 }
88 ";
89
90   iter_structs api (
91     fun sd ->
92       let name = sd.sd_name in
93
94       pr "\n";
95       pr "static bool_t\n";
96       pr "xdr_struct_%s (XDR *xdrs, struct wrap_%s *objp)\n" name name;
97       pr "{\n";
98
99       Array.iter (
100         fun (n, t) ->
101           pr_xdr_of_ptype (sprintf "objp->%s" n) t
102       ) sd.sd_fields;
103
104       pr "  return TRUE;\n";
105       pr "}\n";
106   );
107
108   iter_entry_points api (
109     fun ep ->
110       let name = ep.ep_name in
111       let ret, req, opt = ep.ep_ftype in
112
113       pr "\n";
114       pr "bool_t\n";
115       pr "wrap_int_xdr_%s_args (XDR *xdrs, struct wrap_%s_args *args)\n"
116         name name;
117       pr "{\n";
118
119       List.iter (
120         fun (n, t, _) ->
121           pr_xdr_of_ptype (sprintf "args->%s" n) t
122       ) req;
123
124       if opt <> [] then assert false; (* XXX not implemented *)
125
126       pr "  return TRUE;\n";
127       pr "}\n";
128
129       pr "\n";
130       pr "bool_t\n";
131       pr "wrap_int_xdr_%s_ret (XDR *xdrs, struct wrap_%s_ret *ret)\n"
132         name name;
133       pr "{\n";
134
135       (match ret with
136       | RVoid -> ()
137       | RStaticString ->
138         pr_xdr_of_ptype "ret->r" TString (* XXX is this right? *)
139       | Return t ->
140         pr_xdr_of_ptype "ret->r" t
141       );
142
143       pr "  return TRUE;\n";
144       pr "}\n"
145   )
146
147 let generate api =
148   output_to "lib/xdr.c" generate_lib_xdr_c api