X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=generator%2Fwrappi_c.ml;h=3011ab10bc70c83a122b08196cc8eb270054afbe;hb=b5d8f76790194561f08fb500b3cc2cb10504199e;hp=62fb48d2e8b5e19bc8dcda6cb8eb39bba4886c62;hpb=68483fce4f959886866af28c1c3342c2a6fcb410;p=wrappi.git diff --git a/generator/wrappi_c.ml b/generator/wrappi_c.ml index 62fb48d..3011ab1 100644 --- a/generator/wrappi_c.ml +++ b/generator/wrappi_c.ml @@ -23,17 +23,49 @@ open Wrappi_boilerplate open Printf -let c_of_any_type = function - | TFilePerm -> "int" +let c_of_ptype ~param = function + | TBool -> "int" + | TBuffer -> assert false (* XXX not implemented *) + | TEnum name -> sprintf "enum wrap_%s" name + | TFile -> if param then "const char *" else "char *" + | THash t -> if param then "char * const *" else "char **" + | TInt -> "int" (* XXX not int, correct type depends on preconditions *) | TInt32 -> "int32_t" | TInt64 -> "int64_t" - | TPathname -> "const char *" + | TList t -> assert false (* XXX not implemented *) + | TNullable TString -> if param then "const char *" else "char *" + | TNullable _ -> assert false (* XXX may be implemented in future *) + | TString -> if param then "const char *" else "char *" + | TStruct name -> sprintf "struct wrap_%s" name + | TTypedef name -> assert false (* should never happen *) | TUInt32 -> "uint32_t" | TUInt64 -> "uint64_t" + | TUnion name -> sprintf "union wrap_%s" name -let c_of_return_type = function - | RErr -> "int" - | Return t -> c_of_any_type t +let c_of_rtype = function + | RVoid -> "void" + | Return t -> c_of_ptype ~param:false t + +(* Print the extern... declaration of a single entry point. *) +let pr_extern_decl ep = + let ret, req, opt = ep.ep_ftype in + pr "extern %s wrap_%s (wrap_h *w" (c_of_rtype ret) ep.ep_name; + + (* Required parameters. *) + List.iter ( + fun (name, t, _) -> + let t = c_of_ptype ~param:true t in + let sep = (* "const char *" - omit space after asterisk *) + let len = String.length t in + if isalnum t.[len-1] then " " else "" in + pr ", %s%s%s" t sep name + ) req; + + (* Optional parameters. *) + if opt <> [] then + pr ", ..."; + + pr ");\n" let generate_lib_wrappi_h api = generate_header CStyle LGPLv2plus; @@ -60,23 +92,19 @@ typedef struct wrap_h wrap_h; extern wrap_h *wrap_create (void); extern void wrap_close (wrap_h *w); -/* API entry points. */ "; - List.iter ( - fun ep -> - pr "extern %s wrap_%s (wrap_h *w, %s);\n" - (c_of_return_type ep.ep_return) - ep.ep_name - (String.concat ", " - (List.map ( - fun (name, t) -> - let t = c_of_any_type t in - let last_char = t.[String.length t - 1] in - let sep = if isalnum last_char then " " else "" in - sprintf "%s%s%s" t sep name - ) ep.ep_params)) - ) api.api_entry_points; + (* Separate the local and remote functions. *) + pr "\ +/* Handle functions. */ +"; + iter_entry_points api (fun ep -> if ep.ep_local then pr_extern_decl ep); + + pr "\ + +/* API entry points. */ +"; + iter_entry_points api (fun ep -> if not ep.ep_local then pr_extern_decl ep); pr "\