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

Move TYPE from cstubs to ctypes #544

Merged
merged 2 commits into from
Sep 25, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Makefile.tests
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ tests/test-higher_order/generated_bindings.ml: $(BUILDDIR)/test-higher_order-stu

test-enums-struct-stubs.dir = tests/test-enums/struct-stubs
test-enums-struct-stubs.threads = yes
test-enums-struct-stubs.subproject_deps = ctypes cstubs \
test-enums-struct-stubs.subproject_deps = ctypes \
ctypes-foreign-base ctypes-foreign-threaded tests-common
test-enums-struct-stubs: PROJECT=test-enums-struct-stubs
test-enums-struct-stubs: $$(LIB_TARGETS)

test-enums-stubs.dir = tests/test-enums/stubs
test-enums-stubs.threads = yes
test-enums-stubs.extra_mls = generated_struct_bindings.ml
test-enums-stubs.subproject_deps = ctypes cstubs \
test-enums-stubs.subproject_deps = ctypes \
test-enums-struct-stubs \
test-enums-struct-stubs-generator \
ctypes-foreign-base ctypes-foreign-threaded tests-common
Expand Down Expand Up @@ -347,7 +347,7 @@ $(BUILDDIR)/tests/test-structs/generated_struct_stubs.c: $(BUILDDIR)/test-struct

test-constants-stubs.dir = tests/test-constants/stubs
test-constants-stubs.threads = yes
test-constants-stubs.subproject_deps = ctypes cstubs \
test-constants-stubs.subproject_deps = ctypes \
ctypes-foreign-base ctypes-foreign-threaded tests-common
test-constants-stubs: PROJECT=test-constants-stubs
test-constants-stubs: $$(LIB_TARGETS)
Expand Down Expand Up @@ -962,7 +962,7 @@ tests/test-passing-ocaml-values/generated_bindings.ml: $(BUILDDIR)/test-passing-

test-lwt-jobs-stubs.dir = tests/test-lwt-jobs/stubs
test-lwt-jobs-stubs.threads = yes
test-lwt-jobs-stubs.subproject_deps = ctypes cstubs \
test-lwt-jobs-stubs.subproject_deps = ctypes \
ctypes-foreign-base ctypes-foreign-threaded tests-common
test-lwt-jobs-stubs: PROJECT=test-lwt-jobs-stubs
test-lwt-jobs-stubs: $$(LIB_TARGETS)
Expand Down Expand Up @@ -1005,7 +1005,7 @@ $(BUILDDIR)/tests/test-lwt-jobs/generated_struct_stubs.c: $(BUILDDIR)/test-lwt-j

test-lwt-preemptive-stubs.dir = tests/test-lwt-preemptive/stubs
test-lwt-preemptive-stubs.threads = yes
test-lwt-preemptive-stubs.subproject_deps = ctypes cstubs \
test-lwt-preemptive-stubs.subproject_deps = ctypes \
ctypes-foreign-base ctypes-foreign-threaded tests-common
test-lwt-preemptive-stubs: PROJECT=test-lwt-preemptive-stubs
test-lwt-preemptive-stubs: $$(LIB_TARGETS)
Expand Down Expand Up @@ -1048,7 +1048,7 @@ $(BUILDDIR)/tests/test-lwt-preemptive/generated_struct_stubs.c: $(BUILDDIR)/test

test-returning-errno-lwt-jobs-stubs.dir = tests/test-returning-errno-lwt-jobs/stubs
test-returning-errno-lwt-jobs-stubs.threads = yes
test-returning-errno-lwt-jobs-stubs.subproject_deps = ctypes cstubs \
test-returning-errno-lwt-jobs-stubs.subproject_deps = ctypes \
ctypes-foreign-base ctypes-foreign-threaded tests-common
test-returning-errno-lwt-jobs-stubs: PROJECT=test-returning-errno-lwt-jobs-stubs
test-returning-errno-lwt-jobs-stubs: $$(LIB_TARGETS)
Expand Down Expand Up @@ -1090,7 +1090,7 @@ $(BUILDDIR)/tests/test-returning-errno-lwt-jobs/generated_struct_stubs.c: $(BUIL

test-returning-errno-lwt-preemptive-stubs.dir = tests/test-returning-errno-lwt-preemptive/stubs
test-returning-errno-lwt-preemptive-stubs.threads = yes
test-returning-errno-lwt-preemptive-stubs.subproject_deps = ctypes cstubs \
test-returning-errno-lwt-preemptive-stubs.subproject_deps = ctypes \
ctypes-foreign-base ctypes-foreign-threaded tests-common
test-returning-errno-lwt-preemptive-stubs: PROJECT=test-returning-errno-lwt-preemptive-stubs
test-returning-errno-lwt-preemptive-stubs: $$(LIB_TARGETS)
Expand Down Expand Up @@ -1132,7 +1132,7 @@ $(BUILDDIR)/tests/test-returning-errno-lwt-preemptive/generated_struct_stubs.c:

test-returning-errno-stubs.dir = tests/test-returning-errno/stubs
test-returning-errno-stubs.threads = yes
test-returning-errno-stubs.subproject_deps = ctypes cstubs \
test-returning-errno-stubs.subproject_deps = ctypes \
ctypes-foreign-base ctypes-foreign-threaded tests-common
test-returning-errno-stubs: PROJECT=test-returning-errno-stubs
test-returning-errno-stubs: $$(LIB_TARGETS)
Expand Down
69 changes: 1 addition & 68 deletions src/cstubs/cstubs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,74 +9,7 @@

module Types :
sig
module type TYPE =
sig
include Ctypes_types.TYPE

type 'a const
val constant : string -> 'a typ -> 'a const
(** [constant name typ] retrieves the value of the compile-time constant
[name] of type [typ]. It can be used to retrieve enum constants,
#defined values and other integer constant expressions.

The type [typ] must be either an integer type such as [bool], [char],
[int], [uint8], etc., or a view (or perhaps multiple views) where the
underlying type is an integer type.

When the value of the constant cannot be represented in the type there
will typically be a diagnostic from either the C compiler or the OCaml
compiler. For example, gcc will say

warning: overflow in implicit constant conversion *)

val enum : string -> ?typedef:bool ->
?unexpected:(int64 -> 'a) -> ('a * int64 const) list -> 'a typ
(** [enum name ?unexpected alist] builds a type representation for the
enum named [name]. The size and alignment are retrieved so that the
resulting type can be used everywhere an integer type can be used: as
an array element or struct member, as an argument or return value,
etc.

The value [alist] is an association list of OCaml values and values
retrieved by the [constant] function. For example, to expose the enum

enum letters \{ A, B, C = 10, D \};

you might first retrieve the values of the enumeration constants:

{[
let a = constant "A" int64_t
and b = constant "B" int64_t
and c = constant "C" int64_t
and d = constant "D" int64_t
]}

and then build the enumeration type

{[
let letters = enum "letters" [
`A, a;
`B, b;
`C, c;
`D, d;
] ~unexpected:(fun i -> `E i)
]}

The [unexpected] function specifies the value to return in the case
that some unexpected value is encountered -- for example, if a
function with the return type 'enum letters' actually returns the
value [-1].

The optional flag [typedef] specifies whether the first argument,
[name], indicates an tag or an alias. If [typedef] is [false] (the
default) then [name] is treated as an enumeration tag:

[enum letters { ... }]

If [typedef] is [true] then [name] is instead treated as an alias:

[typedef enum { ... } letters] *)
end
module type TYPE = Ctypes.TYPE

module type BINDINGS = functor (F : TYPE) -> sig end

Expand Down
10 changes: 10 additions & 0 deletions src/ctypes/ctypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ sig
val foreign : string -> ('a -> 'b) fn -> ('a -> 'b) result
val foreign_value : string -> 'a typ -> 'a ptr result
end

module type TYPE =
sig
include Ctypes_types.TYPE

type 'a const
val constant : string -> 'a typ -> 'a const
val enum : string -> ?typedef:bool ->
?unexpected:(int64 -> 'a) -> ('a * int64 const) list -> 'a typ
end
78 changes: 76 additions & 2 deletions src/ctypes/ctypes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,12 @@ val coerce_fn : 'a fn -> 'b fn -> 'a -> 'b
coercions. *)


(** {2 Foreign function binding interface}.
(** {2 binding interfaces}.
*)

(** Foreign function binding interface.

The {!Foreign} and {!Cstubs} modules provide concrete implementations. *)
The {!Foreign} and {!Cstubs} modules provide concrete implementations. *)
module type FOREIGN =
sig
type 'a fn
Expand All @@ -487,6 +490,77 @@ sig
val foreign_value : string -> 'a typ -> 'a ptr result
end

(** Foreign types binding interface.

The {!Cstubs} module builds concrete implementations. *)
module type TYPE =
sig
include Ctypes_types.TYPE

type 'a const
val constant : string -> 'a typ -> 'a const
(** [constant name typ] retrieves the value of the compile-time constant
[name] of type [typ]. It can be used to retrieve enum constants,
#defined values and other integer constant expressions.

The type [typ] must be either an integer type such as [bool], [char],
[int], [uint8], etc., or a view (or perhaps multiple views) where the
underlying type is an integer type.

When the value of the constant cannot be represented in the type there
will typically be a diagnostic from either the C compiler or the OCaml
compiler. For example, gcc will say

warning: overflow in implicit constant conversion *)

val enum : string -> ?typedef:bool ->
?unexpected:(int64 -> 'a) -> ('a * int64 const) list -> 'a typ
(** [enum name ?unexpected alist] builds a type representation for the
enum named [name]. The size and alignment are retrieved so that the
resulting type can be used everywhere an integer type can be used: as
an array element or struct member, as an argument or return value,
etc.

The value [alist] is an association list of OCaml values and values
retrieved by the [constant] function. For example, to expose the enum

enum letters \{ A, B, C = 10, D \};

you might first retrieve the values of the enumeration constants:

{[
let a = constant "A" int64_t
and b = constant "B" int64_t
and c = constant "C" int64_t
and d = constant "D" int64_t
]}

and then build the enumeration type

{[
let letters = enum "letters" [
`A, a;
`B, b;
`C, c;
`D, d;
] ~unexpected:(fun i -> `E i)
]}

The [unexpected] function specifies the value to return in the case
that some unexpected value is encountered -- for example, if a
function with the return type 'enum letters' actually returns the
value [-1].

The optional flag [typedef] specifies whether the first argument,
[name], indicates an tag or an alias. If [typedef] is [false] (the
default) then [name] is treated as an enumeration tag:

[enum letters { ... }]

If [typedef] is [true] then [name] is instead treated as an alias:

[typedef enum { ... } letters] *)
end

(** {2:roots Registration of OCaml values as roots} *)
module Root :
Expand Down
2 changes: 1 addition & 1 deletion tests/test-constants/stubs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

open Ctypes

module Struct_stubs(S : Cstubs.Types.TYPE) =
module Struct_stubs(S : Ctypes.TYPE) =
struct
open S

Expand Down
2 changes: 1 addition & 1 deletion tests/test-enums/struct-stubs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open Ctypes

type fruit = Orange | Apple | Banana | Pear

module Struct_stubs(S : Cstubs.Types.TYPE) =
module Struct_stubs(S : Ctypes.TYPE) =
struct
open S

Expand Down
2 changes: 1 addition & 1 deletion tests/test-lwt-jobs/stubs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
open Ctypes
open PosixTypes

module Struct_stubs(S : Cstubs.Types.TYPE) =
module Struct_stubs(S : Ctypes.TYPE) =
struct
open S

Expand Down
2 changes: 1 addition & 1 deletion tests/test-lwt-preemptive/stubs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
open Ctypes
open PosixTypes

module Struct_stubs(S : Cstubs.Types.TYPE) =
module Struct_stubs(S : Ctypes.TYPE) =
struct
open S

Expand Down
2 changes: 1 addition & 1 deletion tests/test-returning-errno-lwt-jobs/stubs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
open Ctypes
open PosixTypes

module Struct_stubs(S : Cstubs.Types.TYPE) =
module Struct_stubs(S : Ctypes.TYPE) =
struct
open S

Expand Down
2 changes: 1 addition & 1 deletion tests/test-returning-errno-lwt-preemptive/stubs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
open Ctypes
open PosixTypes

module Struct_stubs(S : Cstubs.Types.TYPE) =
module Struct_stubs(S : Ctypes.TYPE) =
struct
open S

Expand Down
2 changes: 1 addition & 1 deletion tests/test-returning-errno/stubs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
open Ctypes
open PosixTypes

module Struct_stubs(S : Cstubs.Types.TYPE) =
module Struct_stubs(S : Ctypes.TYPE) =
struct
open S

Expand Down
2 changes: 1 addition & 1 deletion tests/test-structs/stubs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

open Ctypes

module Struct_stubs(S : Ctypes_types.TYPE) =
module Struct_stubs(S : Ctypes.TYPE) =
struct
open S

Expand Down
2 changes: 1 addition & 1 deletion tests/test-structs/test_structs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ module Stub_tests = Build_stub_tests(Generated_bindings)


module Build_struct_stub_tests
(S : Ctypes_types.TYPE
(S : Ctypes.TYPE
with type 'a typ = 'a Ctypes.typ
and type ('a, 's) field = ('a, 's) Ctypes.field) =
struct
Expand Down
2 changes: 1 addition & 1 deletion tests/test-unions/stubs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

open Ctypes

module Struct_stubs(S : Ctypes_types.TYPE) =
module Struct_stubs(S : Ctypes.TYPE) =
struct
open S

Expand Down
2 changes: 1 addition & 1 deletion tests/test-unions/test_unions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ end


module Build_struct_stub_tests
(S : Ctypes_types.TYPE
(S : Ctypes.TYPE
with type 'a typ = 'a Ctypes.typ
and type ('a, 's) field = ('a, 's) Ctypes.field) =
struct
Expand Down