Explicit types in the generator.
[wrappi.git] / generator-macros / pa_wrap.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 (* For general information about camlp4, see:
20  * http://brion.inria.fr/gallium/index.php/Camlp4
21  *
22  * For information about quotations, see:
23  * http://brion.inria.fr/gallium/index.php/Quotation
24  *)
25
26 open Camlp4.PreCast
27 open Syntax
28 open Ast
29
30 open Wrappi_types
31
32 (* Convert a plain list into a list AST. *)
33 let rec expr_of_list _loc = function
34   | [] -> <:expr< [] >>
35   | h::t -> let t = expr_of_list _loc t in <:expr< $h$ :: $t$ >>
36
37 (* Convert an optional type into an option AST. *)
38 let expr_of_option _loc = function
39   | None -> <:expr< None >>
40   | Some x -> <:expr< Some $x$ >>
41
42 (* Convert a _loc to an AST. *)
43 let expr_of_loc _loc loc =
44   let file_name,
45     start_line, start_bol, start_off,
46     stop_line, stop_bol, stop_off,
47     ghost = Loc.to_tuple loc in
48   <:expr< Camlp4.PreCast.Loc.of_tuple
49     ($str:file_name$,
50      $`int:start_line$, $`int:start_bol$, $`int:start_off$,
51      $`int:stop_line$, $`int:stop_bol$, $`int:stop_off$,
52      $`bool:ghost$) >>
53
54 let add_entry_point _loc name parameters return_type code =
55   let parameters = List.map (
56     fun (name, t) -> <:expr< ($str:name$, $t$) >>
57   ) parameters in
58   let parameters = expr_of_list _loc parameters in
59
60   let code = expr_of_option _loc code in
61
62   let loc = expr_of_loc _loc _loc in
63
64   <:str_item<
65     let ep = { Wrappi_types.ep_loc = $loc$;
66                ep_name = $str:name$;
67                ep_params = $parameters$;
68                ep_return = $return_type$;
69                ep_code = $code$ } in
70     Wrappi_globals.add_entry_point ep
71   >>
72
73 let () =
74   (* Quotation expander for C code. *)
75   let c_quotation_expander _loc _ code =
76     (* XXX Expand %- or $- expressions in code. *)
77     (* XXX Escape >> in code. *)
78     ExStr (_loc, code)
79   in
80   Quotation.add "c" Quotation.DynAst.expr_tag c_quotation_expander;
81
82   (* Default quotation expander (<< .. >>) should be C code ("c"). *)
83   Quotation.default := "c"
84
85 ;;
86
87 (* Extend the regular OCaml grammar. *)
88 EXTEND Gram
89   GLOBAL: str_item;
90
91   (* A parameter or return type. *)
92   any_type: [
93     [ "fileperm" -> <:expr< Wrappi_types.TFilePerm >> ]
94   | [ "int32" -> <:expr< Wrappi_types.TInt32 >> ]
95   | [ "int64" -> <:expr< Wrappi_types.TInt64 >> ]
96   | [ "pathname" -> <:expr< Wrappi_types.TPathname >> ]
97   | [ "uint32" -> <:expr< Wrappi_types.TUInt32 >> ]
98   | [ "uint64" -> <:expr< Wrappi_types.TUInt64 >> ]
99   ];
100
101   (* A return type. *)
102   return_type: [
103     [ "err" -> <:expr< Wrappi_types.RErr >> ]
104   | [ t = any_type -> <:expr< Wrappi_types.Return $t$ >> ]
105   ];
106
107   (* A single function parameter. *)
108   parameter: [[ t = any_type; name = LIDENT -> (name, t) ]];
109
110   str_item: LEVEL "top" [
111     [ "entry_point";
112       return_type = return_type; name = LIDENT;
113       "("; parameters = LIST0 parameter SEP ","; ")";
114       code = OPT [ code = expr -> code ] ->
115       add_entry_point _loc name parameters return_type code
116     ]
117   ];
118
119 END