Explicit types in the generator.
[wrappi.git] / generator-macros / pa_wrap.ml
index c8e8af8..a73484a 100644 (file)
@@ -18,6 +18,7 @@
 
 (* For general information about camlp4, see:
  * http://brion.inria.fr/gallium/index.php/Camlp4
+ *
  * For information about quotations, see:
  * http://brion.inria.fr/gallium/index.php/Quotation
  *)
@@ -28,14 +29,52 @@ open Ast
 
 open Wrappi_types
 
+(* Convert a plain list into a list AST. *)
+let rec expr_of_list _loc = function
+  | [] -> <:expr< [] >>
+  | h::t -> let t = expr_of_list _loc t in <:expr< $h$ :: $t$ >>
+
+(* Convert an optional type into an option AST. *)
+let expr_of_option _loc = function
+  | None -> <:expr< None >>
+  | Some x -> <:expr< Some $x$ >>
+
+(* Convert a _loc to an AST. *)
+let expr_of_loc _loc loc =
+  let file_name,
+    start_line, start_bol, start_off,
+    stop_line, stop_bol, stop_off,
+    ghost = Loc.to_tuple loc in
+  <:expr< Camlp4.PreCast.Loc.of_tuple
+    ($str:file_name$,
+     $`int:start_line$, $`int:start_bol$, $`int:start_off$,
+     $`int:stop_line$, $`int:stop_bol$, $`int:stop_off$,
+     $`bool:ghost$) >>
+
 let add_entry_point _loc name parameters return_type code =
-  (* XXX *)
-  <:str_item< >>
+  let parameters = List.map (
+    fun (name, t) -> <:expr< ($str:name$, $t$) >>
+  ) parameters in
+  let parameters = expr_of_list _loc parameters in
+
+  let code = expr_of_option _loc code in
+
+  let loc = expr_of_loc _loc _loc in
+
+  <:str_item<
+    let ep = { Wrappi_types.ep_loc = $loc$;
+               ep_name = $str:name$;
+               ep_params = $parameters$;
+               ep_return = $return_type$;
+               ep_code = $code$ } in
+    Wrappi_globals.add_entry_point ep
+  >>
 
 let () =
   (* Quotation expander for C code. *)
   let c_quotation_expander _loc _ code =
     (* XXX Expand %- or $- expressions in code. *)
+    (* XXX Escape >> in code. *)
     ExStr (_loc, code)
   in
   Quotation.add "c" Quotation.DynAst.expr_tag c_quotation_expander;
@@ -51,19 +90,22 @@ EXTEND Gram
 
   (* A parameter or return type. *)
   any_type: [
-    [ "int32" -> TInt32 ]
-  | [ "int64" -> TInt64 ]
-  | [ t = LIDENT -> Type t ]
+    [ "fileperm" -> <:expr< Wrappi_types.TFilePerm >> ]
+  | [ "int32" -> <:expr< Wrappi_types.TInt32 >> ]
+  | [ "int64" -> <:expr< Wrappi_types.TInt64 >> ]
+  | [ "pathname" -> <:expr< Wrappi_types.TPathname >> ]
+  | [ "uint32" -> <:expr< Wrappi_types.TUInt32 >> ]
+  | [ "uint64" -> <:expr< Wrappi_types.TUInt64 >> ]
   ];
 
   (* A return type. *)
   return_type: [
-    [ "err" -> RErr ]
-  | [ t = any_type -> Return t ]
+    [ "err" -> <:expr< Wrappi_types.RErr >> ]
+  | [ t = any_type -> <:expr< Wrappi_types.Return $t$ >> ]
   ];
 
   (* A single function parameter. *)
-  parameter: [[ t = any_type; name = LIDENT -> (t, name) ]];
+  parameter: [[ t = any_type; name = LIDENT -> (name, t) ]];
 
   str_item: LEVEL "top" [
     [ "entry_point";