Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / ForeignFunctionInterfaceTypes.adoc
CommitLineData
7f918cf1
CE
1ForeignFunctionInterfaceTypes
2=============================
3
4MLton's <:ForeignFunctionInterface:> only allows values of certain SML
5types to be passed between SML and C. The following types are
6allowed: `bool`, `char`, `int`, `real`, `word`. All of the different
7sizes of (fixed-sized) integers, reals, and words are supported as
8well: `Int8.int`, `Int16.int`, `Int32.int`, `Int64.int`,
9`Real32.real`, `Real64.real`, `Word8.word`, `Word16.word`,
10`Word32.word`, `Word64.word`. There is a special type,
11`MLton.Pointer.t`, for passing C pointers -- see <:MLtonPointer:> for
12details.
13
14Arrays, refs, and vectors of the above types are also allowed.
15Because in MLton monomorphic arrays and vectors are exactly the same
16as their polymorphic counterpart, these are also allowed. Hence,
17`string`, `char vector`, and `CharVector.vector` are also allowed.
18Strings are not null terminated, unless you manually do so from the
19SML side.
20
21Unfortunately, passing tuples or datatypes is not allowed because that
22would interfere with representation optimizations.
23
24The C header file that `-export-header` generates includes
25++typedef++s for the C types corresponding to the SML types. Here is
26the mapping between SML types and C types.
27
28[options="header"]
29|====
30| SML type | C typedef | C type | Note
31| `array` | `Pointer` | `unsigned char *` |
32| `bool` | `Bool` | `int32_t` |
33| `char` | `Char8` | `uint8_t` |
34| `Int8.int` | `Int8` | `int8_t` |
35| `Int16.int` | `Int16` | `int16_t` |
36| `Int32.int` | `Int32` | `int32_t` |
37| `Int64.int` | `Int64` | `int64_t` |
38| `int` | `Int32` | `int32_t` | <:#Default:(default)>
39| `MLton.Pointer.t` | `Pointer` | `unsigned char *` |
40| `Real32.real` | `Real32` | `float` |
41| `Real64.real` | `Real64` | `double` |
42| `real` | `Real64` | `double` | <:#Default:(default)>
43| `ref` | `Pointer` | `unsigned char *` |
44| `string` | `Pointer` | `unsigned char *` | <:#ReadOnly:(read only)>
45| `vector` | `Pointer` | `unsigned char *` | <:#ReadOnly:(read only)>
46| `Word8.word` | `Word8` | `uint8_t` |
47| `Word16.word` | `Word16` | `uint16_t` |
48| `Word32.word` | `Word32` | `uint32_t` |
49| `Word64.word` | `Word64` | `uint64_t` |
50| `word` | `Word32` | `uint32_t` | <:#Default:(default)>
51|====
52
53<!Anchor(Default)>Note (default): The default `int`, `real`, and
54`word` types may be set by the ++-default-type __type__++
55<:CompileTimeOptions: compiler option>. The given C typedef and C
56types correspond to the default behavior.
57
58<!Anchor(ReadOnly)>Note (read only): Because MLton assumes that
59vectors and strings are read-only (and will perform optimizations
60that, for instance, cause them to share space), you must not modify
61the data pointed to by the `unsigned char *` in C code.
62
63Although the C type of an array, ref, or vector is always `Pointer`,
64in reality, the object has the natural C representation. Your C code
65should cast to the appropriate C type if you want to keep the C
66compiler from complaining.
67
68When calling an <:CallingFromSMLToC: imported C function from SML>
69that returns an array, ref, or vector result or when calling an
70<:CallingFromCToSML: exported SML function from C> that takes an
71array, ref, or string argument, then the object must be an ML object
72allocated on the ML heap. (Although an array, ref, or vector object
73has the natural C representation, the object also has an additional
74header used by the SML runtime system.)
75
76In addition, there is an <:MLBasis:> file, `$(SML_LIB)/basis/c-types.mlb`,
77which provides structure aliases for various C types:
78
79|====
80| C type | Structure | Signature
81| `char` | `C_Char` | `INTEGER`
82| `signed char` | `C_SChar` | `INTEGER`
83| `unsigned char` | `C_UChar` | `WORD`
84| `short` | `C_Short` | `INTEGER`
85| `signed short` | `C_SShort` | `INTEGER`
86| `unsigned short` | `C_UShort` | `WORD`
87| `int` | `C_Int` | `INTEGER`
88| `signed int` | `C_SInt` | `INTEGER`
89| `unsigned int` | `C_UInt` | `WORD`
90| `long` | `C_Long` | `INTEGER`
91| `signed long` | `C_SLong` | `INTEGER`
92| `unsigned long` | `C_ULong` | `WORD`
93| `long long` | `C_LongLong` | `INTEGER`
94| `signed long long` | `C_SLongLong` | `INTEGER`
95| `unsigned long long` | `C_ULongLong` | `WORD`
96| `float` | `C_Float` | `REAL`
97| `double` | `C_Double` | `REAL`
98| `size_t` | `C_Size` | `WORD`
99| `ptrdiff_t` | `C_Ptrdiff` | `INTEGER`
100| `intmax_t` | `C_Intmax` | `INTEGER`
101| `uintmax_t` | `C_UIntmax` | `WORD`
102| `intptr_t` | `C_Intptr` | `INTEGER`
103| `uintptr_t` | `C_UIntptr` | `WORD`
104| `void *` | `C_Pointer` | `WORD`
105|====
106
107These aliases depend on the configuration of the C compiler for the
108target architecture, and are independent of the configuration of MLton
109(including the ++-default-type __type__++
110<:CompileTimeOptions: compiler option>).