Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typedef support (Closes #135) #238

Merged
merged 1 commit into from
Dec 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/cstubs/cstubs_generate_c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ struct
(e, from) >>= fun x ->
`Cast (into, x)

let rec prj : type a. a typ -> cexp -> ccomp option =
fun ty x -> match ty with
let rec prj : type a b. a typ -> orig: b typ -> cexp -> ccomp option =
fun ty ~orig x -> match ty with
| Void -> None
| Primitive p ->
let { fn = Fn fn } as prj = prim_prj p in
Expand All @@ -175,18 +175,20 @@ struct
| Pointer _ -> Some (of_fatptr x)
| Struct s ->
Some ((of_fatptr x, ptr void) >>= fun y ->
`Deref (`Cast (Ty (ptr ty), y)))
`Deref (`Cast (Ty (ptr orig), y)))
| Union u ->
Some ((of_fatptr x, ptr void) >>= fun y ->
`Deref (`Cast (Ty (ptr ty), y)))
`Deref (`Cast (Ty (ptr orig), y)))
| Abstract _ -> report_unpassable "values of abstract type"
| View { ty } -> prj ty x
| View { ty } -> prj ty ~orig x
| Array _ -> report_unpassable "arrays"
| Bigarray _ -> report_unpassable "bigarrays"
| OCaml String -> Some (string_to_ptr x)
| OCaml Bytes -> Some (string_to_ptr x)
| OCaml FloatArray -> Some (float_array_to_ptr x)

let prj ty x = prj ty ~orig:ty x

let rec inj : type a. a typ -> cexp -> ceff =
fun ty x -> match ty with
| Void -> val_unit
Expand Down
8 changes: 8 additions & 0 deletions src/ctypes/ctypes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ val view : ?format_typ:((Format.formatter -> unit) -> Format.formatter -> unit)

*)

val typedef : 'a typ -> string -> 'a typ
(** [typedef t name] creates a C type representation [t'] which
is equivalent to [t] except its name is printed as [name].

This is useful when generating C stubs involving "anonymous" types, for
example: [typedef struct { int f } typedef_name;]
*)

(** {3 Abstract types} *)

type 'a abstract
Expand Down
4 changes: 4 additions & 0 deletions src/ctypes/static.ml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ let abstract ~name ~size ~alignment =
Abstract { aname = name; asize = size; aalignment = alignment }
let view ?format_typ ?format ~read ~write ty =
View { read; write; format_typ; format; ty }
let id v = v
let typedef old name =
view ~format_typ:(fun k fmt -> Format.fprintf fmt "%s%t" name k)
~read:id ~write:id old
let bigarray : type a b c d e.
< element: a;
dims: b;
Expand Down
1 change: 1 addition & 0 deletions src/ctypes/static.mli
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ val view : ?format_typ:((Format.formatter -> unit) ->
Format.formatter -> unit) ->
?format: (Format.formatter -> 'b -> unit) ->
read:('a -> 'b) -> write:('b -> 'a) -> 'a typ -> 'b typ
val typedef: 'a typ -> string -> 'a typ
val bigarray : < ba_repr : 'c;
bigarray : 'd;
carray : 'e;
Expand Down