Debian Codegraph @

doc/guide/src/ForeignFunctionInterfaceSyntax.adoc

raw
1 = ForeignFunctionInterfaceSyntax
2
3 MLton extends the syntax of SML with expressions that enable a
4 <<ForeignFunctionInterface#>> to C. The following description of the
5 syntax uses some abbreviations.
6
7 [options="header"]
8 |===
9 | C base type | _cBaseTy_ | <<ForeignFunctionInterfaceTypes#,Foreign Function Interface types>>
10 | C argument type | _cArgTy_ | `_cBaseTy_~1~ * ... * _cBaseTy_~n~` or `unit`
11 | C return type | _cRetTy_ | `_cBaseTy_` or `unit`
12 | C function type | _cFuncTy_ | `_cArgTy_ \-> _cRetTy_`
13 | C pointer type | _cPtrTy_ | `MLton.Pointer.t`
14 |===
15
16 The type annotation and the semicolon are not optional in the syntax
17 of <<ForeignFunctionInterface#>> expressions. However, the type is
18 lexed, parsed, and elaborated as an SML type, so any type (including
19 type abbreviations) may be used, so long as it elaborates to a type of
20 the correct form.
21
22
23 == Address
24
25 ----
26 _address "CFunctionOrVariableName" attr... : cPtrTy;
27 ----
28
29 Denotes the address of the C function or variable.
30
31 `attr...` denotes a (possibly empty) sequence of attributes. The following attributes are recognized:
32
33 * `external` : import with external symbol scope (see <<LibrarySupport#>>) (default).
34 * `private` : import with private symbol scope (see <<LibrarySupport#>>).
35 * `public` : import with public symbol scope (see <<LibrarySupport#>>).
36
37 See <<MLtonPointer#,MLtonPointer>> for functions that manipulate C pointers.
38
39
40 == Symbol
41
42 ----
43 _symbol "CVariableName" attr... : (unit -> cBaseTy) * (cBaseTy -> unit);
44 ----
45
46 Denotes the _getter_ and _setter_ for a C variable. The __cBaseTy__s
47 must be identical.
48
49 `attr...` denotes a (possibly empty) sequence of attributes. The following attributes are recognized:
50
51 * `alloc` : allocate storage (and export a symbol) for the C variable.
52 * `external` : import or export with external symbol scope (see <<LibrarySupport#>>) (default if not `alloc`).
53 * `private` : import or export with private symbol scope (see <<LibrarySupport#>>).
54 * `public` : import or export with public symbol scope (see <<LibrarySupport#>>) (default if `alloc`).
55
56
57 ----
58 _symbol * : cPtrTy -> (unit -> cBaseTy) * (cBaseTy -> unit);
59 ----
60
61 Denotes the _getter_ and _setter_ for a C pointer to a variable.
62 The __cBaseTy__s must be identical.
63
64
65 == Import
66
67 ----
68 _import "CFunctionName" attr... : cFuncTy;
69 ----
70
71 Denotes an SML function whose behavior is implemented by calling the C
72 function. See <<CallingFromSMLToC#,Calling from SML to C>> for more
73 details.
74
75 `attr...` denotes a (possibly empty) sequence of attributes. The following attributes are recognized:
76
77 * `cdecl` : call with the `cdecl` calling convention (default).
78 * `external` : import with external symbol scope (see <<LibrarySupport#>>) (default).
79 * `impure`: assert that the function depends upon state and/or performs side effects (default).
80 * `private` : import with private symbol scope (see <<LibrarySupport#>>).
81 * `public` : import with public symbol scope (see <<LibrarySupport#>>).
82 * `pure`: assert that the function does not depend upon state or perform any side effects; such functions are subject to various optimizations (e.g., <<CommonSubexp#>>, <<RemoveUnused#>>)
83 * `reentrant`: assert that the function (directly or indirectly) calls an `_export`-ed SML function.
84 * `stdcall` : call with the `stdcall` calling convention (ignored except on Cygwin and MinGW).
85
86
87 ----
88 _import * attr... : cPtrTy -> cFuncTy;
89 ----
90
91 Denotes an SML function whose behavior is implemented by calling a C
92 function through a C function pointer.
93
94 `attr...` denotes a (possibly empty) sequence of attributes. The following attributes are recognized:
95
96 * `cdecl` : call with the `cdecl` calling convention (default).
97 * `impure`: assert that the function depends upon state and/or performs side effects (default).
98 * `pure`: assert that the function does not depend upon state or perform any side effects; such functions are subject to various optimizations (e.g., <<CommonSubexp#>>, <<RemoveUnused#>>)
99 * `reentrant`: assert that the function (directly or indirectly) calls an `_export`-ed SML function.
100 * `stdcall` : call with the `stdcall` calling convention (ignored except on Cygwin and MinGW).
101
102 See
103 <<CallingFromSMLToCFunctionPointer#,Calling from SML to C function pointer>>
104 for more details.
105
106
107 == Export
108
109 ----
110 _export "CFunctionName" attr... : cFuncTy -> unit;
111 ----
112
113 Exports a C function with the name `CFunctionName` that can be used to
114 call an SML function of the type _cFuncTy_. When the function denoted
115 by the export expression is applied to an SML function `f`, subsequent
116 C calls to `CFunctionName` will call `f`. It is an error to call
117 `CFunctionName` before the export has been applied. The export may be
118 applied more than once, with each application replacing any previous
119 definition of `CFunctionName`.
120
121 `attr...` denotes a (possibly empty) sequence of attributes. The following attributes are recognized:
122
123 * `cdecl` : call with the `cdecl` calling convention (default).
124 * `private` : export with private symbol scope (see <<LibrarySupport#>>).
125 * `public` : export with public symbol scope (see <<LibrarySupport#>>) (default).
126 * `stdcall` : call with the `stdcall` calling convention (ignored except on Cygwin and MinGW).
127
128 See <<CallingFromCToSML#,Calling from C to SML>> for more details.