1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>ForeignFunctionInterfaceSyntax</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">
<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>ForeignFunctionInterfaceSyntax</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>MLton extends the syntax of SML with expressions that enable a
<a href="ForeignFunctionInterface">ForeignFunctionInterface</a> to C. The following description of the
syntax uses some abbreviations.</p></div>
<table class="tableblock frame-all grid-all"
style="
width:100%;
">
<col style="width:33%;">
<col style="width:33%;">
<col style="width:33%;">
<thead>
<tr>
<th class="tableblock halign-left valign-top" > C base type </th>
<th class="tableblock halign-left valign-top" > <em>cBaseTy</em> </th>
<th class="tableblock halign-left valign-top" > <a href="ForeignFunctionInterfaceTypes"> Foreign Function Interface types</a></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top" ><p class="tableblock">C argument type</p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><em>cArgTy</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><em>cBaseTy</em><sub>1</sub> <span class="monospaced">*</span> … <span class="monospaced">*</span> <em>cBaseTy</em><sub>n</sub> or <span class="monospaced">unit</span></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top" ><p class="tableblock">C return type</p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><em>cRetTy</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><em>cBaseTy</em> or <span class="monospaced">unit</span></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top" ><p class="tableblock">C function type</p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><em>cFuncTy</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><em>cArgTy</em> <span class="monospaced">-></span> <em>cRetTy</em></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top" ><p class="tableblock">C pointer type</p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><em>cPtrTy</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">MLton.Pointer.t</span></p></td>
</tr>
</tbody>
</table>
<div class="paragraph"><p>The type annotation and the semicolon are not optional in the syntax
of <a href="ForeignFunctionInterface">ForeignFunctionInterface</a> expressions. However, the type is
lexed, parsed, and elaborated as an SML type, so any type (including
type abbreviations) may be used, so long as it elaborates to a type of
the correct form.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_address">Address</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content monospaced">
<pre>_address "CFunctionOrVariableName" attr... : cPtrTy;</pre>
</div></div>
<div class="paragraph"><p>Denotes the address of the C function or variable.</p></div>
<div class="paragraph"><p><span class="monospaced">attr...</span> denotes a (possibly empty) sequence of attributes. The following attributes are recognized:</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">external</span> : import with external symbol scope (see <a href="LibrarySupport">LibrarySupport</a>) (default).
</p>
</li>
<li>
<p>
<span class="monospaced">private</span> : import with private symbol scope (see <a href="LibrarySupport">LibrarySupport</a>).
</p>
</li>
<li>
<p>
<span class="monospaced">public</span> : import with public symbol scope (see <a href="LibrarySupport">LibrarySupport</a>).
</p>
</li>
</ul></div>
<div class="paragraph"><p>See <a href="MLtonPointer"> MLtonPointer</a> for functions that manipulate C pointers.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_symbol">Symbol</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content monospaced">
<pre>_symbol "CVariableName" attr... : (unit -> cBaseTy) * (cBaseTy -> unit);</pre>
</div></div>
<div class="paragraph"><p>Denotes the <em>getter</em> and <em>setter</em> for a C variable. The <em>cBaseTy</em>s
must be identical.</p></div>
<div class="paragraph"><p><span class="monospaced">attr...</span> denotes a (possibly empty) sequence of attributes. The following attributes are recognized:</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">alloc</span> : allocate storage (and export a symbol) for the C variable.
</p>
</li>
<li>
<p>
<span class="monospaced">external</span> : import or export with external symbol scope (see <a href="LibrarySupport">LibrarySupport</a>) (default if not <span class="monospaced">alloc</span>).
</p>
</li>
<li>
<p>
<span class="monospaced">private</span> : import or export with private symbol scope (see <a href="LibrarySupport">LibrarySupport</a>).
</p>
</li>
<li>
<p>
<span class="monospaced">public</span> : import or export with public symbol scope (see <a href="LibrarySupport">LibrarySupport</a>) (default if <span class="monospaced">alloc</span>).
</p>
</li>
</ul></div>
<div class="listingblock">
<div class="content monospaced">
<pre>_symbol * : cPtrTy -> (unit -> cBaseTy) * (cBaseTy -> unit);</pre>
</div></div>
<div class="paragraph"><p>Denotes the <em>getter</em> and <em>setter</em> for a C pointer to a variable.
The <em>cBaseTy</em>s must be identical.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_import">Import</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content monospaced">
<pre>_import "CFunctionName" attr... : cFuncTy;</pre>
</div></div>
<div class="paragraph"><p>Denotes an SML function whose behavior is implemented by calling the C
function. See <a href="CallingFromSMLToC"> Calling from SML to C</a> for more
details.</p></div>
<div class="paragraph"><p><span class="monospaced">attr...</span> denotes a (possibly empty) sequence of attributes. The following attributes are recognized:</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">cdecl</span> : call with the <span class="monospaced">cdecl</span> calling convention (default).
</p>
</li>
<li>
<p>
<span class="monospaced">external</span> : import with external symbol scope (see <a href="LibrarySupport">LibrarySupport</a>) (default).
</p>
</li>
<li>
<p>
<span class="monospaced">private</span> : import with private symbol scope (see <a href="LibrarySupport">LibrarySupport</a>).
</p>
</li>
<li>
<p>
<span class="monospaced">public</span> : import with public symbol scope (see <a href="LibrarySupport">LibrarySupport</a>).
</p>
</li>
<li>
<p>
<span class="monospaced">stdcall</span> : call with the <span class="monospaced">stdcall</span> calling convention (ignored except on Cygwin and MinGW).
</p>
</li>
</ul></div>
<div class="listingblock">
<div class="content monospaced">
<pre>_import * attr... : cPtrTy -> cFuncTy;</pre>
</div></div>
<div class="paragraph"><p>Denotes an SML function whose behavior is implemented by calling a C
function through a C function pointer.</p></div>
<div class="paragraph"><p><span class="monospaced">attr...</span> denotes a (possibly empty) sequence of attributes. The following attributes are recognized:</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">cdecl</span> : call with the <span class="monospaced">cdecl</span> calling convention (default).
</p>
</li>
<li>
<p>
<span class="monospaced">stdcall</span> : call with the <span class="monospaced">stdcall</span> calling convention (ignored except on Cygwin and MinGW).
</p>
</li>
</ul></div>
<div class="paragraph"><p>See
<a href="CallingFromSMLToCFunctionPointer"> Calling from SML to C function pointer</a>
for more details.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_export">Export</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content monospaced">
<pre>_export "CFunctionName" attr... : cFuncTy -> unit;</pre>
</div></div>
<div class="paragraph"><p>Exports a C function with the name <span class="monospaced">CFunctionName</span> that can be used to
call an SML function of the type <em>cFuncTy</em>. When the function denoted
by the export expression is applied to an SML function <span class="monospaced">f</span>, subsequent
C calls to <span class="monospaced">CFunctionName</span> will call <span class="monospaced">f</span>. It is an error to call
<span class="monospaced">CFunctionName</span> before the export has been applied. The export may be
applied more than once, with each application replacing any previous
definition of <span class="monospaced">CFunctionName</span>.</p></div>
<div class="paragraph"><p><span class="monospaced">attr...</span> denotes a (possibly empty) sequence of attributes. The following attributes are recognized:</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">cdecl</span> : call with the <span class="monospaced">cdecl</span> calling convention (default).
</p>
</li>
<li>
<p>
<span class="monospaced">private</span> : export with private symbol scope (see <a href="LibrarySupport">LibrarySupport</a>).
</p>
</li>
<li>
<p>
<span class="monospaced">public</span> : export with public symbol scope (see <a href="LibrarySupport">LibrarySupport</a>) (default).
</p>
</li>
<li>
<p>
<span class="monospaced">stdcall</span> : call with the <span class="monospaced">stdcall</span> calling convention (ignored except on Cygwin and MinGW).
</p>
</li>
</ul></div>
<div class="paragraph"><p>See <a href="CallingFromCToSML"> Calling from C to SML</a> for more details.</p></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
|