| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 5 |
<meta name="generator" content="hevea 2.35"> |
| 6 |
<style type="text/css"> |
| 7 |
.equationcontainer{position:relative;} |
| 8 |
.equationnumber{float:right;left:auto;position:absolute;right:0} |
| 9 |
.equationnumber-valign{float:right;left:auto;position:absolute;right:0;margin-top:-0.5em;} |
| 10 |
.floatrule{background-color: black; border: none; height: 1px; width: 80%} |
| 11 |
.phantom{display: inline-block; visibility: hidden} |
| 12 |
.hphantom{display: inline-block; height: 0; visibility: hidden} |
| 13 |
.vphantom{display: inline-block; visibility: hidden; width: 0} |
| 14 |
.smash{display: inline-block; height: 0; line-height: 0} |
| 15 |
.li-itemize{margin:1ex 0ex;} |
| 16 |
.li-enumerate{margin:1ex 0ex;} |
| 17 |
.dd-description{margin:0ex 0ex 1ex 4ex;} |
| 18 |
.dt-description{margin:0ex;} |
| 19 |
.toc{list-style:none;} |
| 20 |
.footnotetext{margin:0ex; padding:0ex;} |
| 21 |
div.footnotetext P{margin:0px; text-indent:1em;} |
| 22 |
.thefootnotes{text-align:left;margin:0ex;} |
| 23 |
.dt-thefootnotes{margin:0em;} |
| 24 |
.dd-thefootnotes{margin:0em 0em 0em 2em;} |
| 25 |
.footnoterule{background-color: black; border: none; height: 1px; margin: 1em auto 1em 0px; width: 40%} |
| 26 |
.caption{padding-left:2ex; padding-right:2ex; margin-left:auto; margin-right:auto} |
| 27 |
.title{margin:2ex auto;text-align:center} |
| 28 |
.titlemain{margin:1ex 2ex 2ex 1ex;} |
| 29 |
.titlerest{margin:0ex 2ex;} |
| 30 |
.center{text-align:center;margin-left:auto;margin-right:auto;} |
| 31 |
.flushleft{text-align:left;margin-left:0ex;margin-right:auto;} |
| 32 |
.flushright{text-align:right;margin-left:auto;margin-right:0ex;} |
| 33 |
div table{margin-left:inherit;margin-right:inherit;margin-bottom:2px;margin-top:2px} |
| 34 |
td table{margin:auto;} |
| 35 |
table{border-collapse:collapse;} |
| 36 |
td{padding:0;} |
| 37 |
.cellpadding0 tr td{padding:0;} |
| 38 |
.cellpadding1 tr td{padding:1px;} |
| 39 |
pre{text-align:left;margin-left:0ex;margin-right:auto;} |
| 40 |
blockquote{margin-left:4ex;margin-right:4ex;text-align:left;} |
| 41 |
td p{margin:0px;} |
| 42 |
.quote{margin-left:3em;margin-right:3em;text-align:inherit;text-indent:0pt} |
| 43 |
.quotation{margin-left:3em;margin-right:3em;text-align:inherit;text-indent:1.5em} |
| 44 |
.verse{margin-left:3em;margin-right:3em;text-indent:1.5em hanging each-line} |
| 45 |
.parbox{box-sizing: border-box; |
| 46 |
display: inline-block; |
| 47 |
text-indent: 0; |
| 48 |
} |
| 49 |
.rule-rect{fill: black;} |
| 50 |
.lrbox{box-sizing:border-box;display:inline-block;overflow:visible;white-space:nowrap;} |
| 51 |
.center-lrbox{display:inline-block;margin-left:50%;transform:translateX(-50%);} |
| 52 |
.makebox{} |
| 53 |
.framebox{border:1px solid black;padding:0.25em;} |
| 54 |
.vertical-rule{border:none;width:2px;background-color:black;} |
| 55 |
.horizontal-rule{border:none;background-color:black;} |
| 56 |
.hrule{border:none;height:2px;width:100%;background-color:black;} |
| 57 |
.hfill{border:none;height:1px;width:200%;background-color:black;} |
| 58 |
.vdisplay{border-collapse:separate;border-spacing:2px;line-height:1.1;width:auto; empty-cells:show; border:2px solid red;} |
| 59 |
.vdcell{white-space:nowrap;padding:0px; border:2px solid green;} |
| 60 |
.display{border-collapse:separate;border-spacing:2px;line-height:1.1;width:auto; border:none;} |
| 61 |
.dcell{white-space:nowrap;padding:0px; border:none;} |
| 62 |
.dcenter{margin:0ex auto;} |
| 63 |
.vdcenter{border:solid #FF8000 2px; margin:0ex auto;} |
| 64 |
.minipage{text-align:left; margin-left:0em; margin-right:auto;} |
| 65 |
.marginpar{border:solid thin black; margin-bottom:1ex; width:20%; text-align:left;} |
| 66 |
.marginparleft{float:left; clear:left; margin-left:0ex; margin-right:1ex;} |
| 67 |
.marginparright{float:right; clear:right; margin-left:1ex; margin-right:0ex;} |
| 68 |
.theorem{text-align:left;margin:1ex auto 1ex 0ex;} |
| 69 |
.part{margin:2ex auto;text-align:center} |
| 70 |
.syntax-token{color:blue;font-family:monospace} |
| 71 |
a.syntax:link{color:maroon;text-decoration:underline} |
| 72 |
a.syntax:visited{color:maroon;text-decoration:underline} |
| 73 |
a.syntax:hover{color:black;text-decoration:none;background-color:#FF6060} |
| 74 |
.nonterminal{color:maroon;font-style:oblique} |
| 75 |
a.section-anchor::after{content:"🔗"; |
| 76 |
font-size:smaller; |
| 77 |
margin-left:-1.5em; |
| 78 |
padding-right:0.5em; |
| 79 |
} |
| 80 |
a.section-anchor{visibility:hidden; |
| 81 |
color:grey !important; |
| 82 |
text-decoration:none !important; |
| 83 |
} |
| 84 |
*:hover>a.section-anchor{visibility:visible; |
| 85 |
} |
| 86 |
a:link{color:#4286f4;text-decoration:underline;} |
| 87 |
a:visited{color:#0d46a3;text-decoration:underline;} |
| 88 |
a:hover{color:black;text-decoration:underline;} |
| 89 |
@media all{@font-face { |
| 90 |
/* fira-sans-regular - latin */ |
| 91 |
font-family: 'Fira Sans'; |
| 92 |
font-style: normal; |
| 93 |
font-weight: 400; |
| 94 |
src: url('fonts/fira-sans-v8-latin-regular.eot'); /* IE9 Compat Modes */ |
| 95 |
src: local('Fira Sans Regular'), local('FiraSans-Regular'), |
| 96 |
url('fonts/fira-sans-v8-latin-regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ |
| 97 |
url('fonts/fira-sans-v8-latin-regular.woff2') format('woff2'), /* Super Modern Browsers */ |
| 98 |
url('fonts/fira-sans-v8-latin-regular.woff') format('woff'), /* Modern Browsers */ |
| 99 |
url('fonts/fira-sans-v8-latin-regular.ttf') format('truetype'), /* Safari, Android, iOS */ |
| 100 |
url('fonts/fira-sans-v8-latin-regular.svg#FiraSans') format('svg'); /* Legacy iOS */ |
| 101 |
}} |
| 102 |
body{max-width:750px; |
| 103 |
width: 85%; |
| 104 |
margin: auto; |
| 105 |
background: #f7f7f7; |
| 106 |
margin-top: 80px; |
| 107 |
font-size: 1rem; |
| 108 |
} |
| 109 |
.maintitle{font-family: "Fira Sans", sans-serif; |
| 110 |
text-align: center; |
| 111 |
} |
| 112 |
h1, h2, h3{font-family: "Fira Sans", sans-serif; |
| 113 |
font-weight: normal; |
| 114 |
border-bottom: 1px solid black; |
| 115 |
} |
| 116 |
div.ocaml{margin:2ex 0px; |
| 117 |
font-size: 1rem; |
| 118 |
background: beige; |
| 119 |
border: 1px solid grey; |
| 120 |
padding: 10px; |
| 121 |
overflow-y:auto; |
| 122 |
display:flex; |
| 123 |
flex-direction: column; |
| 124 |
flex-wrap: nowrap; |
| 125 |
} |
| 126 |
div.ocaml .pre{white-space: pre; |
| 127 |
font-family: monospace; |
| 128 |
} |
| 129 |
.ocamlkeyword{font-weight:bold; |
| 130 |
} |
| 131 |
.ocamlhighlight{font-weight:bold; |
| 132 |
text-decoration:underline; |
| 133 |
} |
| 134 |
.ocamlerror{font-weight:bold; |
| 135 |
color:red; |
| 136 |
} |
| 137 |
.ocamlwarning{font-weight:bold; |
| 138 |
color:purple; |
| 139 |
} |
| 140 |
.ocamlcomment{color:grey; |
| 141 |
} |
| 142 |
.ocamlstring{opacity:0.75; |
| 143 |
} |
| 144 |
#cc_license_logo{float:left; |
| 145 |
margin-right: 1em; |
| 146 |
} |
| 147 |
p,ul{line-height:1.3em} |
| 148 |
.cellpadding1 tr td{padding:1px 4px} |
| 149 |
div.caml-output{color:maroon;} |
| 150 |
div.caml-example.toplevel div.caml-input{color:#006000;} |
| 151 |
.ocamlprompt{color:black;} |
| 152 |
.osvariant{font-family:sans-serif} |
| 153 |
.tableau, .syntax, .syntaxleft{/* same width as body */ |
| 154 |
max-width: 750px; |
| 155 |
overflow-y: auto; |
| 156 |
} |
| 157 |
.th{font-size:small;} |
| 158 |
.li-links{margin:0ex 0ex;} |
| 159 |
.font-tt{font-family:monospace;} |
| 160 |
.font-it{font-style:italic;} |
| 161 |
.font-bold{font-weight:bold;} |
| 162 |
.font-sl{font-style:oblique;} |
| 163 |
</style> |
| 164 |
<title>Camlidl user's manual |
| 165 |
</title> |
| 166 |
</head> |
| 167 |
<body > |
| 168 |
|
| 169 |
<table class="title"><tr><td style="padding:1ex"><h1 class="titlemain">Camlidl user’s manual</h1><h3 class="titlerest">Xavier Leroy <br> |
| 170 |
Collège de France and Inria</h3></td></tr> |
| 171 |
</table> |
| 172 |
|
| 173 |
<h2 id="sec1" class="section">1 Overview</h-><p>Camlidl generates stub code for interfacing Caml with C |
| 174 |
(as described in chapter “Interfacing with C” of the |
| 175 |
<a href="http://caml.inria.fr/ocaml/htmlman/index.html">Objective Caml |
| 176 |
reference manual</a>) from an IDL description of the C functions to be |
| 177 |
made available in Caml. |
| 178 |
Thus, Camlidl automates the most tedious task in interfacing C |
| 179 |
libraries with Caml programs. It can also be used to interface Caml |
| 180 |
programs with other languages, as long as those languages have a |
| 181 |
well-defined C interface.</p><p>In addition, Camlidl provides basic support for COM interfaces and |
| 182 |
components. It supports both using COM components (usually written in |
| 183 |
C++ or C) from Caml programs, and packaging Caml objects as |
| 184 |
COM components that can then be used from C++ or C.</p> |
| 185 |
|
| 186 |
<h3 id="sec2" class="subsection">1.1 What is IDL?</h-><p>IDL stands for Interface Description Language. This is a generic term |
| 187 |
for a family of small languages that have been developed to provide |
| 188 |
type specifications for libraries written in C and C++. Those |
| 189 |
languages resembles C declarations (as found in C header files), with |
| 190 |
extra annotations to provide more precise types for the arguments and |
| 191 |
results of the functions.</p><p>The particular IDL used by Camlidl is inspired by Microsoft’s IDL, |
| 192 |
which itself is an extension of the IDL found in DCE (The Open Group’s |
| 193 |
Distributed Common Environment). The initial motivation for those IDLs |
| 194 |
was to automate the generation of stub code for remote procedure calls |
| 195 |
and network objects, where the arguments to the function are marshaled |
| 196 |
at the calling site, then sent across the network or through |
| 197 |
interprocess communications to a server process, which unmarshals the |
| 198 |
arguments, compute the function application, marshal the results, |
| 199 |
sends them back to the calling site, where they are unmarshaled and |
| 200 |
returned to the caller. IDLs were also found to be very useful for |
| 201 |
inter-language communications, since the same type information that |
| 202 |
guides the generation of marshaling stubs can be used to generate |
| 203 |
stubs to convert between the data representations of several |
| 204 |
languages.</p> |
| 205 |
|
| 206 |
<h3 id="sec3" class="subsection">1.2 What is COM?</h-><p>COM is Microsoft’s Common Object Model. It provides a set of |
| 207 |
programming conventions as well as system support for packaging |
| 208 |
C++ objects as executable components that can be used in other |
| 209 |
programs, either by dynamic linking of the component inside the |
| 210 |
program, or through interprocess or internetwork communications |
| 211 |
between the program and a remote server. COM components implement one |
| 212 |
or several interfaces, (similar to Caml object types or Java |
| 213 |
interfaces) identified by unique 128-bit interface identifiers (IIDs). |
| 214 |
COM specifies a standard protocol for reference counting of |
| 215 |
components, and for asking a component which interfaces it |
| 216 |
implements.</p><p>While the full range of COM services and third-party components is |
| 217 |
available only on Microsoft’s Windows operating systems, the basic COM |
| 218 |
conventions can also be used on Unix and other operating systems |
| 219 |
to exchange objects between Caml and C or C++. Of particular |
| 220 |
interest is the encapsulation of Caml objects as COM components, which |
| 221 |
can then be used inside larger C or C++ applications; those |
| 222 |
applications do not need to know anything about Caml: they just call |
| 223 |
the component methods as if they were C++ methods or C |
| 224 |
functions, without knowing that they are actually implemented in Caml.</p><p>For more information about COM, see for instance <em>Inside COM</em> |
| 225 |
by Dale Rogerson (Microsoft Press), or the |
| 226 |
<a href="http://msdn.microsoft.com">Microsoft developer Web site</a>.</p> |
| 227 |
|
| 228 |
<h2 id="sec4" class="section">2 IDL syntax</h-><p>This section describes the syntax of IDL files. IDL syntax is very |
| 229 |
close to that of C declarations, with extra attributes between |
| 230 |
brackets adding information to the C types. The following example |
| 231 |
should give the flavor of the syntax: |
| 232 |
</p><pre> int f([in,string] char * msg); |
| 233 |
</pre><p>This reads: “<span class=machine><span class=font-tt>f</span></span> is a function taking a character string as input and |
| 234 |
returning an <span class=machine><span class=font-tt>int</span></span>”.</p> |
| 235 |
|
| 236 |
<h3 id="sec5" class="subsection">2.1 Lexical conventions</h-> |
| 237 |
|
| 238 |
<h4 id="sec6" class="paragraph">Blanks.</h4><p> Space, newline, horizontal tabulation, carriage |
| 239 |
return, line feed and form feed are considered as blanks. Blanks are |
| 240 |
ignored, but they separate adjacent tokens.</p> |
| 241 |
|
| 242 |
<h4 id="sec7" class="paragraph">Comments.</h4><p> Both C-style comments <span class=machine><span class=font-tt>/* ... */</span></span> and |
| 243 |
Java-style comments <span class=machine><span class=font-tt>// ...</span></span> are supported. C-style comments are |
| 244 |
introduced by <span class=machine><span class=font-tt>/*</span></span> and terminated by <span class=machine><span class=font-tt>*/</span></span>. Java-style comments are |
| 245 |
introduced by <span class=machine><span class=font-tt>//</span></span> and extend to the end of the line. Comments are |
| 246 |
treated as blank characters. Comments do not occur inside string or |
| 247 |
character literals. Nested C-style comments are not supported.</p> |
| 248 |
|
| 249 |
<h4 id="sec8" class="paragraph">Identifiers.</h4><p> Identifiers have the same syntax as in C. |
| 250 |
</p><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="ident" class="syntax"><span class=nonterminal>ident</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > (<span class=syntax-token>A</span> … <span class=syntax-token>Z</span> ∣ <span class=syntax-token>a</span> … <span class=syntax-token>z</span> ∣ <span class=syntax-token>_</span>) |
| 251 |
{<span class=syntax-token>A</span> … <span class=syntax-token>Z</span> ∣ <span class=syntax-token>a</span> … <span class=syntax-token>z</span> ∣ <span class=syntax-token>0</span> … <span class=syntax-token>9</span> ∣ <span class=syntax-token>_</span>}</td></tr> |
| 252 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 253 |
</table></td></tr> |
| 254 |
</table></div> |
| 255 |
|
| 256 |
<h4 id="sec9" class="paragraph">Literals.</h4><p> Integer literals, character literals and string |
| 257 |
literals have the same syntax as in C. |
| 258 |
</p><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="integer" class="syntax"><span class=nonterminal>integer</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > [<span class=syntax-token>-</span>] {<span class=syntax-token>0</span> … <span class=syntax-token>9</span>} |
| 259 |
[<span class=syntax-token>-</span>] <span class=syntax-token>0x</span> {<span class=syntax-token>0</span> … <span class=syntax-token>9</span> ∣ <span class=syntax-token>a</span> … <span class=syntax-token>f</span> ∣ <span class=syntax-token>A</span> … <span class=syntax-token>F</span>} |
| 260 |
[<span class=syntax-token>-</span>] <span class=syntax-token>0</span> {<span class=syntax-token>0</span> … <span class=syntax-token>7</span>}</td></tr> |
| 261 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 262 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 263 |
<a id="character" class="syntax"><span class=nonterminal>character</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > (<span class=nonterminal>regular-char</span> ∣ <a href="#escape-char" class="syntax"><span class=nonterminal>escape-char</span></a>) </td></tr> |
| 264 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 265 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 266 |
<a id="string" class="syntax"><span class=nonterminal>string</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>"</span> {<span class=nonterminal>regular-char</span> ∣ <a href="#escape-char" class="syntax"><span class=nonterminal>escape-char</span></a>} <span class=syntax-token>"</span></td></tr> |
| 267 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 268 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 269 |
<a id="escape-char" class="syntax"><span class=nonterminal>escape-char</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>\</span> (<span class=syntax-token>b</span>∣<span class=syntax-token>n</span>∣<span class=syntax-token>r</span>∣<span class=syntax-token>t</span>) |
| 270 |
</td></tr> |
| 271 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>\</span> (<span class=syntax-token>0</span>…<span class=syntax-token>7</span>) [<span class=syntax-token>0</span>…<span class=syntax-token>7</span>] [<span class=syntax-token>0</span>…<span class=syntax-token>7</span>]</td></tr> |
| 272 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 273 |
</table></td></tr> |
| 274 |
</table></div> |
| 275 |
|
| 276 |
<h4 id="sec10" class="paragraph">UUID.</h4><p> Unique identifiers are composed of 16 hexadecimal |
| 277 |
digits, in groups of 8, 4, 4, 4 and 12, separated by dashes. |
| 278 |
</p><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="uuid" class="syntax"><span class=nonterminal>uuid</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > <a href="#hex" class="syntax"><span class=nonterminal>hex</span></a><sup>8</sup> <span class=syntax-token>-</span> <a href="#hex" class="syntax"><span class=nonterminal>hex</span></a><sup>4</sup> <span class=syntax-token>-</span> <a href="#hex" class="syntax"><span class=nonterminal>hex</span></a><sup>4</sup> <span class=syntax-token>-</span> <a href="#hex" class="syntax"><span class=nonterminal>hex</span></a><sup>4</sup> <span class=syntax-token>-</span> <a href="#hex" class="syntax"><span class=nonterminal>hex</span></a><sup>4</sup> <a href="#hex" class="syntax"><span class=nonterminal>hex</span></a><sup>4</sup> <a href="#hex" class="syntax"><span class=nonterminal>hex</span></a><sup>4</sup></td></tr> |
| 279 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 280 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 281 |
<a id="hex" class="syntax"><span class=nonterminal>hex</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>0</span> … <span class=syntax-token>9</span> ∣ <span class=syntax-token>a</span> … <span class=syntax-token>f</span> ∣ <span class=syntax-token>A</span> … <span class=syntax-token>F</span></td></tr> |
| 282 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 283 |
</table></td></tr> |
| 284 |
</table></div> |
| 285 |
|
| 286 |
<h3 id="sec11" class="subsection">2.2 Limited expressions</h-><p>Limited expressions are similar to C expressions, with the omission of |
| 287 |
assignment operators (<span class=machine><span class=font-tt>=</span></span>, <span class=machine><span class=font-tt>+=</span></span>, etc), and the addition of the |
| 288 |
unsigned (logical) right shift operator <span class=machine><span class=font-tt>>>></span></span>. Operators have the same |
| 289 |
precedences and associativities as in C. They are listed below in |
| 290 |
decreasing priority order. |
| 291 |
</p><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="lexpr" class="syntax"><span class=nonterminal>lexpr</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 292 |
<a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> |
| 293 |
</td></tr> |
| 294 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#integer" class="syntax"><span class=nonterminal>integer</span></a> |
| 295 |
</td></tr> |
| 296 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#character" class="syntax"><span class=nonterminal>character</span></a> |
| 297 |
</td></tr> |
| 298 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>true</span> |
| 299 |
</td></tr> |
| 300 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>false</span> |
| 301 |
</td></tr> |
| 302 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#string" class="syntax"><span class=nonterminal>string</span></a> |
| 303 |
</td></tr> |
| 304 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=nonterminal>sizeof</span> <span class=syntax-token>(</span> <span class=nonterminal>type-expr</span> <span class=syntax-token>)</span> |
| 305 |
</td></tr> |
| 306 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>(</span> <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> <span class=syntax-token>)</span> |
| 307 |
</td></tr> |
| 308 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> (<span class=syntax-token>.</span> ∣ <span class=syntax-token>-></span>) <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> |
| 309 |
</td></tr> |
| 310 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>(</span> <span class=nonterminal>type-expr</span> <span class=syntax-token>)</span> <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> |
| 311 |
</td></tr> |
| 312 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > (<span class=syntax-token>&</span>∣<span class=syntax-token>*</span>∣<span class=syntax-token>!</span>∣<span class=syntax-token>~</span>∣<span class=syntax-token>-</span>∣<span class=syntax-token>+</span>) <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> |
| 313 |
</td></tr> |
| 314 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> (<span class=syntax-token>*</span>∣<span class=syntax-token>/</span>∣<span class=syntax-token>%</span>) <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> |
| 315 |
</td></tr> |
| 316 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> (<span class=syntax-token>+</span>∣<span class=syntax-token>-</span>) <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> |
| 317 |
</td></tr> |
| 318 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> (<span class=syntax-token><<</span>∣<span class=syntax-token>>></span></td></tr> |
| 319 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" ><span class=syntax-token>>>></span>) <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> |
| 320 |
</td></tr> |
| 321 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> (<span class=syntax-token>==</span>∣<span class=syntax-token>!=</span>∣<span class=syntax-token>>=</span>∣<span class=syntax-token><=</span>∣<span class=syntax-token>></span>∣<span class=syntax-token><</span>) |
| 322 |
</td></tr> |
| 323 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> (<span class=syntax-token>&</span>∣<span class=syntax-token>^</span>∣<span class=syntax-token>|</span>) <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> |
| 324 |
</td></tr> |
| 325 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> (<span class=syntax-token>&&</span>∣<span class=syntax-token>||</span>) <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> |
| 326 |
</td></tr> |
| 327 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> <span class=syntax-token>?</span> <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> <span class=syntax-token>:</span> <a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a> |
| 328 |
</td></tr> |
| 329 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 330 |
</table></td></tr> |
| 331 |
</table></div><p> |
| 332 |
Constant limited expressions, written <span class=nonterminal>const-lexpr</span> below, can only |
| 333 |
reference identifiers that are bound by the IDL <span class=machine><span class=font-tt>const</span></span> declaration.</p> |
| 334 |
|
| 335 |
<h3 id="sec12" class="subsection">2.3 Attributes</h-><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="attributes" class="syntax"><span class=nonterminal>attributes</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 336 |
<span class=syntax-token>[</span> <a href="#attribute" class="syntax"><span class=nonterminal>attribute</span></a> { <span class=syntax-token>,</span> <a href="#attribute" class="syntax"><span class=nonterminal>attribute</span></a> } <span class=syntax-token>]</span> </td></tr> |
| 337 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 338 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 339 |
<a id="attribute" class="syntax"><span class=nonterminal>attribute</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 340 |
<a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> |
| 341 |
</td></tr> |
| 342 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>(</span> [<a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a>] { <span class=syntax-token>,</span> [<a href="#lexpr" class="syntax"><span class=nonterminal>lexpr</span></a>] } <span class=syntax-token>)</span> |
| 343 |
</td></tr> |
| 344 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>(</span> <a href="#uuid" class="syntax"><span class=nonterminal>uuid</span></a> <span class=syntax-token>)</span> |
| 345 |
</td></tr> |
| 346 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#attribute" class="syntax"><span class=nonterminal>attribute</span></a> <span class=syntax-token>*</span> |
| 347 |
</td></tr> |
| 348 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>*</span> <a href="#attribute" class="syntax"><span class=nonterminal>attribute</span></a> |
| 349 |
</td></tr> |
| 350 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 351 |
</table></td></tr> |
| 352 |
</table></div><p> |
| 353 |
Attribute lists are written in brackets <span class=machine><span class=font-tt>[...]</span></span>, and are always |
| 354 |
optional. Each attribute is identified by a name, and may carry |
| 355 |
optional arguments. Starred attributes apply to the element type of a |
| 356 |
pointer or array type, rather than to the pointer or array type itself. |
| 357 |
The following table summarizes the recognized attributes and their |
| 358 |
arguments.</p><div class="tableau"> |
| 359 |
<div class="center"><table border=1 style="border-spacing:0;width:80%" class="cellpadding1"><tr><td style="text-align:center;border:solid 1px;white-space:nowrap" ><span class=font-bold>Attribute</span></td><td style="text-align:center;border:solid 1px;white-space:nowrap" ><span class=font-bold>Context where it can appear</span> </td></tr> |
| 360 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" > |
| 361 |
<span class=syntax-token>abstract</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>typedef</span></span> </td></tr> |
| 362 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>bigarray</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >array type </td></tr> |
| 363 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>blocking</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >function declaration </td></tr> |
| 364 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>camlint</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span> or <span class=machine><span class=font-tt>long</span></span> integer type </td></tr> |
| 365 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>compare</span><span class=syntax-token>(</span><span class=nonterminal>fun-name</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>typedef</span></span> </td></tr> |
| 366 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>c2ml</span><span class=syntax-token>(</span><span class=nonterminal>fun-name</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>typedef</span></span> </td></tr> |
| 367 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>errorcheck</span><span class=syntax-token>(</span><span class=nonterminal>fun-name</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>typedef</span></span> </td></tr> |
| 368 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>errorcode</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>typedef</span></span> </td></tr> |
| 369 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>finalize</span><span class=syntax-token>(</span><span class=nonterminal>fun-name</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>typedef</span></span> </td></tr> |
| 370 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>fortran</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >array type with <span class=machine><span class=font-tt>bigarray</span></span> attribute </td></tr> |
| 371 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>hash</span><span class=syntax-token>(</span><span class=nonterminal>fun-name</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>typedef</span></span> </td></tr> |
| 372 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>ignore</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >any pointer type </td></tr> |
| 373 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>in</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >function parameter </td></tr> |
| 374 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>int_default</span><span class=syntax-token>(</span> <span class=syntax-token>camlint</span>∣<span class=syntax-token>nativeint</span>∣<span class=syntax-token>int32</span>∣<span class=syntax-token>int64</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >interface </td></tr> |
| 375 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>int32</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span> or <span class=machine><span class=font-tt>long</span></span> integer type </td></tr> |
| 376 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>int64</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span> or <span class=machine><span class=font-tt>long</span></span> integer type </td></tr> |
| 377 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>length_is</span><span class=syntax-token>(</span><span class=nonterminal>le</span><sub>1</sub><span class=syntax-token>,</span><span class=nonterminal>le</span><sub>2</sub><span class=syntax-token>,</span>…<span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >array type </td></tr> |
| 378 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>long_default</span><span class=syntax-token>(</span> <span class=syntax-token>camlint</span>∣<span class=syntax-token>nativeint</span>∣<span class=syntax-token>int32</span>∣<span class=syntax-token>int64</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >interface </td></tr> |
| 379 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>managed</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >array type with <span class=machine><span class=font-tt>bigarray</span></span> attribute </td></tr> |
| 380 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>ml2c</span><span class=syntax-token>(</span><span class=nonterminal>fun-name</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>typedef</span></span> </td></tr> |
| 381 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>mlname(</span> <span class=nonterminal>fun-or-field-name</span> <span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >function declaration, <span class=machine><span class=font-tt>struct</span></span> field </td></tr> |
| 382 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>mltype("</span> <span class=nonterminal>caml-type-expr</span> <span class=syntax-token>")</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>typedef</span></span> </td></tr> |
| 383 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>nativeint</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span> or <span class=machine><span class=font-tt>long</span></span> integer type </td></tr> |
| 384 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>null_terminated</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >array of pointers </td></tr> |
| 385 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>object</span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" >interface </td></tr> |
| 386 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>out</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >function parameter </td></tr> |
| 387 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>pointer_default</span><span class=syntax-token>(</span> <span class=syntax-token>ref</span>∣<span class=syntax-token>unique</span>∣<span class=syntax-token>ptr</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >interface </td></tr> |
| 388 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>propget</span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" >function declaration </td></tr> |
| 389 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>propput</span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" >function declaration </td></tr> |
| 390 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>propputref</span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" >function declaration </td></tr> |
| 391 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>ptr</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >any pointer type </td></tr> |
| 392 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>ref</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >any pointer type </td></tr> |
| 393 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>set</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >enum type </td></tr> |
| 394 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>size_is</span><span class=syntax-token>(</span><span class=nonterminal>le</span><sub>1</sub><span class=syntax-token>,</span><span class=nonterminal>le</span><sub>2</sub><span class=syntax-token>,</span>…<span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >array type </td></tr> |
| 395 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>string</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >character array or pointer </td></tr> |
| 396 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>switch_is</span><span class=syntax-token>(</span><span class=nonterminal>le</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >union type or pointer to union </td></tr> |
| 397 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>switch_type</span><span class=syntax-token>(</span><span class=nonterminal>ty</span><span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >union or pointer to union </td></tr> |
| 398 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>unique</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >any pointer, array, or bigarray type </td></tr> |
| 399 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=syntax-token>uuid</span><span class=syntax-token>(</span> <a href="#uuid" class="syntax"><span class=nonterminal>uuid</span></a> <span class=syntax-token>)</span> </td><td style="text-align:left;border:solid 1px;white-space:nowrap" >interface </td></tr> |
| 400 |
</table></div></div> |
| 401 |
|
| 402 |
<h3 id="sec13" class="subsection">2.4 Types and declarators</h-><p>The declaration of an identifier along with its type is as in C: |
| 403 |
a type specification comes first, followed by the identifier possibly |
| 404 |
decorated with <span class=machine><span class=font-tt>*</span></span> and <span class=machine><span class=font-tt>[...]</span></span> to denote pointers and array types. |
| 405 |
For instance, <span class=machine><span class=font-tt>int x</span></span> declares an identifier <span class=machine><span class=font-tt>x</span></span> of type <span class=machine><span class=font-tt>int</span></span>, |
| 406 |
while <span class=machine><span class=font-tt>int (*x)[]</span></span> declares an identifier <span class=machine><span class=font-tt>x</span></span> that is a pointer to an |
| 407 |
array of integers. |
| 408 |
</p><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="type-spec" class="syntax"><span class=nonterminal>type-spec</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 409 |
[<span class=syntax-token>unsigned</span>∣<span class=syntax-token>signed</span>] |
| 410 |
(<span class=syntax-token>int</span>∣<span class=syntax-token>short</span>∣<span class=syntax-token>long</span>∣<span class=syntax-token>char</span>∣<span class=syntax-token>hyper</span>∣<span class=syntax-token>long</span> <span class=syntax-token>long</span>∣<span class=syntax-token>__int64</span>) |
| 411 |
</td></tr> |
| 412 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>byte</span> |
| 413 |
</td></tr> |
| 414 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>float</span> |
| 415 |
</td></tr> |
| 416 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>double</span> |
| 417 |
</td></tr> |
| 418 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>boolean</span> |
| 419 |
</td></tr> |
| 420 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>void</span> |
| 421 |
</td></tr> |
| 422 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> |
| 423 |
</td></tr> |
| 424 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>wchar_t</span> |
| 425 |
</td></tr> |
| 426 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>handle_t</span> |
| 427 |
</td></tr> |
| 428 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>struct</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> |
| 429 |
</td></tr> |
| 430 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>union</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> |
| 431 |
</td></tr> |
| 432 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>enum</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> |
| 433 |
</td></tr> |
| 434 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#struct-decl" class="syntax"><span class=nonterminal>struct-decl</span></a> |
| 435 |
</td></tr> |
| 436 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#union-decl" class="syntax"><span class=nonterminal>union-decl</span></a> |
| 437 |
</td></tr> |
| 438 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#enum-decl" class="syntax"><span class=nonterminal>enum-decl</span></a> |
| 439 |
</td></tr> |
| 440 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 441 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 442 |
<a id="declarator" class="syntax"><span class=nonterminal>declarator</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 443 |
{<span class=syntax-token>*</span>} <a href="#direct-declarator" class="syntax"><span class=nonterminal>direct-declarator</span></a> |
| 444 |
</td></tr> |
| 445 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 446 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 447 |
<a id="direct-declarator" class="syntax"><span class=nonterminal>direct-declarator</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 448 |
<a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> |
| 449 |
</td></tr> |
| 450 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>(</span> <a href="#declarator" class="syntax"><span class=nonterminal>declarator</span></a> <span class=syntax-token>)</span> |
| 451 |
</td></tr> |
| 452 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#direct-declarator" class="syntax"><span class=nonterminal>direct-declarator</span></a> <span class=syntax-token>[</span> [<span class=nonterminal>const-lexpr</span>] <span class=syntax-token>]</span> |
| 453 |
</td></tr> |
| 454 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 455 |
</table></td></tr> |
| 456 |
</table></div> |
| 457 |
|
| 458 |
<h3 id="sec14" class="subsection">2.5 Structures, unions and enumerations</h-><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="struct-decl" class="syntax"><span class=nonterminal>struct-decl</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 459 |
<span class=syntax-token>struct</span> [<a href="#ident" class="syntax"><span class=nonterminal>ident</span></a>] <span class=syntax-token>{</span> {<a href="#field-decl" class="syntax"><span class=nonterminal>field-decl</span></a>} <span class=syntax-token>}</span> |
| 460 |
</td></tr> |
| 461 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 462 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 463 |
<a id="field-decl" class="syntax"><span class=nonterminal>field-decl</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 464 |
<a href="#attributes" class="syntax"><span class=nonterminal>attributes</span></a> <a href="#type-spec" class="syntax"><span class=nonterminal>type-spec</span></a> <a href="#declarator" class="syntax"><span class=nonterminal>declarator</span></a> { <span class=syntax-token>,</span> <a href="#declarator" class="syntax"><span class=nonterminal>declarator</span></a> } <span class=syntax-token>;</span> |
| 465 |
</td></tr> |
| 466 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 467 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 468 |
<a id="union-decl" class="syntax"><span class=nonterminal>union-decl</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 469 |
<span class=syntax-token>union</span> [<a href="#ident" class="syntax"><span class=nonterminal>ident</span></a>] <span class=syntax-token>{</span> {<a href="#union-case" class="syntax"><span class=nonterminal>union-case</span></a>} <span class=syntax-token>}</span> |
| 470 |
</td></tr> |
| 471 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>union</span> [<a href="#ident" class="syntax"><span class=nonterminal>ident</span></a>] <span class=syntax-token>switch</span> <span class=syntax-token>(</span> <a href="#type-spec" class="syntax"><span class=nonterminal>type-spec</span></a> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>)</span> <span class=syntax-token>{</span> {<a href="#union-case" class="syntax"><span class=nonterminal>union-case</span></a>} <span class=syntax-token>}</span> |
| 472 |
</td></tr> |
| 473 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 474 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 475 |
<a id="union-case" class="syntax"><span class=nonterminal>union-case</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 476 |
{<span class=syntax-token>case</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>:</span>}<sup>+</sup> [<a href="#field-decl" class="syntax"><span class=nonterminal>field-decl</span></a>] <span class=syntax-token>;</span> |
| 477 |
<span class=syntax-token>default</span> <span class=syntax-token>:</span> [<a href="#field-decl" class="syntax"><span class=nonterminal>field-decl</span></a>] <span class=syntax-token>;</span> |
| 478 |
</td></tr> |
| 479 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 480 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 481 |
<a id="enum-decl" class="syntax"><span class=nonterminal>enum-decl</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 482 |
<span class=syntax-token>enum</span> [<a href="#ident" class="syntax"><span class=nonterminal>ident</span></a>] <span class=syntax-token>{</span> <a href="#enum-case" class="syntax"><span class=nonterminal>enum-case</span></a> {<span class=syntax-token>,</span> <a href="#enum-case" class="syntax"><span class=nonterminal>enum-case</span></a>} [<span class=syntax-token>,</span>] <span class=syntax-token>}</span> |
| 483 |
</td></tr> |
| 484 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 485 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 486 |
<a id="enum-case" class="syntax"><span class=nonterminal>enum-case</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 487 |
<a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> [<span class=syntax-token>=</span> <span class=nonterminal>const-lexpr</span>] |
| 488 |
</td></tr> |
| 489 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 490 |
</table></td></tr> |
| 491 |
</table></div><p>IDL <span class=machine><span class=font-tt>struct</span></span> declarations are like those of C, with the addition of |
| 492 |
optional attributes on each field. <span class=machine><span class=font-tt>union</span></span> declarations are also as |
| 493 |
in C, except that each case of an union must be labeled by one or |
| 494 |
several <span class=syntax-token>case</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>:</span>. The first form of union declaration |
| 495 |
assumes that the discriminant of the union is provided separately |
| 496 |
via a <span class=machine><span class=font-tt>switch</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span> annotation on the union type, while the second form |
| 497 |
encapsulates the discriminant along with the union itself |
| 498 |
(like in Pascal’s <span class=machine><span class=font-tt>record case of</span></span> construct).</p> |
| 499 |
|
| 500 |
<h3 id="sec15" class="subsection">2.6 Function declarations</h-><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="function-decl" class="syntax"><span class=nonterminal>function-decl</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 501 |
<a href="#attributes" class="syntax"><span class=nonterminal>attributes</span></a> <a href="#type-spec" class="syntax"><span class=nonterminal>type-spec</span></a> {<span class=syntax-token>*</span>} <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>(</span> <a href="#params" class="syntax"><span class=nonterminal>params</span></a> <span class=syntax-token>)</span> {<span class=syntax-token>quote</span><span class=syntax-token>(</span><a href="#ident" class="syntax"><span class=nonterminal>ident</span></a><span class=syntax-token>,</span><a href="#string" class="syntax"><span class=nonterminal>string</span></a><span class=syntax-token>)</span>} |
| 502 |
</td></tr> |
| 503 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 504 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 505 |
<a id="params" class="syntax"><span class=nonterminal>params</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 506 |
є |
| 507 |
</td></tr> |
| 508 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>void</span> |
| 509 |
</td></tr> |
| 510 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#param" class="syntax"><span class=nonterminal>param</span></a> { <span class=syntax-token>,</span> <a href="#param" class="syntax"><span class=nonterminal>param</span></a> } |
| 511 |
</td></tr> |
| 512 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 513 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 514 |
<a id="param" class="syntax"><span class=nonterminal>param</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 515 |
<a href="#attributes" class="syntax"><span class=nonterminal>attributes</span></a> <a href="#type-spec" class="syntax"><span class=nonterminal>type-spec</span></a> <a href="#declarator" class="syntax"><span class=nonterminal>declarator</span></a> |
| 516 |
</td></tr> |
| 517 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 518 |
</table></td></tr> |
| 519 |
</table></div><p> |
| 520 |
Function declarations are like in ANSI C, with the addition of |
| 521 |
attributes on each parameter and on the function itself. |
| 522 |
Parameters must be named. The optional <span class=nonterminal>quote</span> statements following the |
| 523 |
declaration are user-provided calling sequences and deallocation |
| 524 |
sequences that replaces the default sequences in the |
| 525 |
<span class=machine><span class=font-tt>camlidl</span></span>-generated stub code for the function.</p> |
| 526 |
|
| 527 |
<h3 id="sec16" class="subsection">2.7 Constant definitions</h-><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="constant-decl" class="syntax"><span class=nonterminal>constant-decl</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>const</span> <a href="#attributes" class="syntax"><span class=nonterminal>attributes</span></a> <a href="#type-spec" class="syntax"><span class=nonterminal>type-spec</span></a> {<span class=syntax-token>*</span>} <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>=</span> <span class=nonterminal>const-lexpr</span> <span class=syntax-token>;</span> |
| 528 |
</td></tr> |
| 529 |
</table></td></tr> |
| 530 |
</table></div><p> |
| 531 |
A constant declaration associates a name to a limited expression. |
| 532 |
The limited expression can refer to constant names declared earlier, |
| 533 |
but cannot refer to other kinds of identifiers. The optional |
| 534 |
attributes influence the interpretation of the type specification, |
| 535 |
e.g. <span class=machine><span class=font-tt>const int x = 3</span></span> defines <span class=machine><span class=font-tt>x</span></span> with Caml type <span class=machine><span class=font-tt>int</span></span>, but |
| 536 |
<span class=machine><span class=font-tt>const [int64] long x = 5</span></span> defines <span class=machine><span class=font-tt>x</span></span> with Caml type <span class=machine><span class=font-tt>int64</span></span>.</p> |
| 537 |
|
| 538 |
<h3 id="sec17" class="subsection">2.8 IDL files</h-><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><a id="file" class="syntax"><span class=nonterminal>file</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > {<a href="#decl" class="syntax"><span class=nonterminal>decl</span></a>} </td></tr> |
| 539 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 540 |
<tr><td style="text-align:right;white-space:nowrap" > |
| 541 |
<a id="decl" class="syntax"><span class=nonterminal>decl</span></a></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 542 |
<a href="#function-decl" class="syntax"><span class=nonterminal>function-decl</span></a> <span class=syntax-token>;</span> |
| 543 |
</td></tr> |
| 544 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#constant-decl" class="syntax"><span class=nonterminal>constant-decl</span></a> <span class=syntax-token>;</span> |
| 545 |
</td></tr> |
| 546 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#struct-decl" class="syntax"><span class=nonterminal>struct-decl</span></a> <span class=syntax-token>;</span> |
| 547 |
</td></tr> |
| 548 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#union-decl" class="syntax"><span class=nonterminal>union-decl</span></a> <span class=syntax-token>;</span> |
| 549 |
</td></tr> |
| 550 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#enum-decl" class="syntax"><span class=nonterminal>enum-decl</span></a> <span class=syntax-token>;</span> |
| 551 |
</td></tr> |
| 552 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>typedef</span> <a href="#attributes" class="syntax"><span class=nonterminal>attributes</span></a> <a href="#type-spec" class="syntax"><span class=nonterminal>type-spec</span></a> <a href="#declarator" class="syntax"><span class=nonterminal>declarator</span></a> { <span class=syntax-token>,</span> <a href="#declarator" class="syntax"><span class=nonterminal>declarator</span></a> } <span class=syntax-token>;</span> |
| 553 |
</td></tr> |
| 554 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#attributes" class="syntax"><span class=nonterminal>attributes</span></a> <span class=syntax-token>interface</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> [ <span class=syntax-token>:</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> ] <span class=syntax-token>{</span> {<a href="#decl" class="syntax"><span class=nonterminal>decl</span></a>} <span class=syntax-token>}</span> |
| 555 |
</td></tr> |
| 556 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>struct</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>;</span> |
| 557 |
</td></tr> |
| 558 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>union</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>;</span> |
| 559 |
</td></tr> |
| 560 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>union</span> <span class=syntax-token>switch</span> <span class=syntax-token>(</span> <a href="#type-spec" class="syntax"><span class=nonterminal>type-spec</span></a> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>)</span> <span class=syntax-token>;</span> |
| 561 |
</td></tr> |
| 562 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <a href="#attributes" class="syntax"><span class=nonterminal>attributes</span></a> <span class=syntax-token>interface</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>;</span> |
| 563 |
</td></tr> |
| 564 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>import</span> <a href="#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>;</span> |
| 565 |
</td></tr> |
| 566 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>quote</span> <span class=syntax-token>(</span> [<a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>,</span>] <a href="#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>)</span> |
| 567 |
</td></tr> |
| 568 |
<tr><td style="text-align:right;white-space:nowrap" > </td><td style="text-align:center;white-space:nowrap" >∣</td><td style="text-align:left;white-space:nowrap" > <span class=syntax-token>cpp_quote</span> <span class=syntax-token>(</span> <a href="#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>)</span> |
| 569 |
</td></tr> |
| 570 |
</table></td></tr> |
| 571 |
</table></div><p> |
| 572 |
An IDL file is a sequence of IDL declarations. Declarations include |
| 573 |
function declarations, constant declarations, type declarations |
| 574 |
(structs, unions, enums, as well as a C-style <span class=machine><span class=font-tt>typedef</span></span> declaration to |
| 575 |
name a type expression), and interfaces.</p><p>An interface declaration gives a name and attributes to a collection |
| 576 |
of declarations. For interfaces with the <span class=machine><span class=font-tt>object</span></span> attribute, |
| 577 |
an optional super-interface can be provided, as in |
| 578 |
<span class=syntax-token>interface</span> <span class=nonterminal>intf</span> <span class=syntax-token>:</span> <span class=nonterminal>super-intf</span>. The name of the interface can be |
| 579 |
used as a type name in the remainder of the file.</p><p>Forward declarations of structs, unions and interfaces are supported |
| 580 |
in the usual C manner, by just giving the name of the struct, union or |
| 581 |
interface, but not its actual contents.</p><p>The <span class=machine><span class=font-tt>import</span></span> statement reads another IDL file and makes available its |
| 582 |
type and constant declarations in the remainder of the file. |
| 583 |
No code is generated for the functions and interfaces declared in the |
| 584 |
imported file. The same file can be imported several times, but is |
| 585 |
read in only the first time.</p><p>The <span class=syntax-token>quote</span> <span class=syntax-token>(</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>,</span> <span class=nonterminal>str</span> <span class=syntax-token>)</span> diversion copies the string <span class=nonterminal>str</span> |
| 586 |
verbatim to one of the files generated by the <span class=machine><span class=font-tt>camlidl</span></span> compiler. |
| 587 |
The <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> determines the file where <span class=nonterminal>str</span> is copied: |
| 588 |
it can be <span class=machine><span class=font-tt>ml</span></span> for the Caml implementation file (<span class=machine><span class=font-tt>.ml</span></span>), |
| 589 |
<span class=machine><span class=font-tt>mli</span></span> for the Caml interface file (<span class=machine><span class=font-tt>.mli</span></span>), |
| 590 |
<span class=machine><span class=font-tt>mlmli</span></span> for both Caml files, |
| 591 |
<span class=machine><span class=font-tt>h</span></span> for the C header file (<span class=machine><span class=font-tt>.h</span></span>), |
| 592 |
and <span class=machine><span class=font-tt>c</span></span> for the C source file containing the generated stub code (<span class=machine><span class=font-tt>.c</span></span> |
| 593 |
file). For backward compatibility, <span class=syntax-token>cpp_quote</span> <span class=syntax-token>(</span> <span class=nonterminal>str</span> <span class=syntax-token>)</span> is |
| 594 |
recognized as synonymous for <span class=syntax-token>quote</span> <span class=syntax-token>(</span> <span class=syntax-token>h</span> <span class=syntax-token>,</span> <span class=nonterminal>str</span> <span class=syntax-token>)</span>.</p> |
| 595 |
|
| 596 |
<h2 id="sec18" class="section">3 The Caml-IDL mapping</h-><p>This section describes how IDL types, function declarations, and |
| 597 |
interfaces are mapped to Caml types, functions and classes.</p> |
| 598 |
|
| 599 |
<h3 id="sec19" class="subsection">3.1 Base types</h-><div class="tableau"> |
| 600 |
<div class="center"><table border=1 style="border-spacing:0;width:80%" class="cellpadding1"><tr><td style="text-align:center;border:solid 1px;white-space:nowrap" ><span class=font-bold>IDL type </span><span class=font-bold><span class=font-it>ty</span></span></td><td style="text-align:center;border:solid 1px;white-space:nowrap" ><span class=font-bold>Caml type </span>[[<span class=font-it>ty</span>]] </td></tr> |
| 601 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" > |
| 602 |
<span class=machine><span class=font-tt>byte</span></span>, <span class=machine><span class=font-tt>short</span></span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span> </td></tr> |
| 603 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span>, <span class=machine><span class=font-tt>long</span></span> with <span class=machine><span class=font-tt>[camlint]</span></span> attribute</td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span> </td></tr> |
| 604 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span>, <span class=machine><span class=font-tt>long</span></span> with <span class=machine><span class=font-tt>[nativeint]</span></span> attribute</td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>nativeint</span></span> </td></tr> |
| 605 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span>, <span class=machine><span class=font-tt>long</span></span> with <span class=machine><span class=font-tt>[int32]</span></span> attribute</td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int32</span></span> </td></tr> |
| 606 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int</span></span>, <span class=machine><span class=font-tt>long</span></span> with <span class=machine><span class=font-tt>[int64]</span></span> attribute</td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int64</span></span> </td></tr> |
| 607 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>hyper</span></span>, <span class=machine><span class=font-tt>long long</span></span>, <span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>int64</span></span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>int64</span></span> </td></tr> |
| 608 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>char</span></span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>char</span></span> </td></tr> |
| 609 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>float</span></span>, <span class=machine><span class=font-tt>double</span></span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>float</span></span> </td></tr> |
| 610 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>boolean</span></span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>bool</span></span> </td></tr> |
| 611 |
</table></div></div><p> |
| 612 |
(For integer types, <span class=machine><span class=font-tt>signed</span></span> and <span class=machine><span class=font-tt>unsigned</span></span> variants of the same IDL |
| 613 |
integer type translate to the same Caml type.)</p><p>Depending on the attributes, the <span class=machine><span class=font-tt>int</span></span> and <span class=machine><span class=font-tt>long</span></span> integer types are |
| 614 |
converted to one of the Caml integer types <span class=machine><span class=font-tt>int</span></span>, <span class=machine><span class=font-tt>nativeint</span></span>, |
| 615 |
<span class=machine><span class=font-tt>int32</span></span>, or <span class=machine><span class=font-tt>int64</span></span>. Values of Caml type <span class=machine><span class=font-tt>int32</span></span> are exactly 32-bit wide |
| 616 |
and values of type <span class=machine><span class=font-tt>int64</span></span> are exactly 64-bit wide on all platforms. |
| 617 |
Values of type <span class=machine><span class=font-tt>nativeint</span></span> have the natural word size of the platform, |
| 618 |
and are large enough to accommodate any C <span class=machine><span class=font-tt>int</span></span> or <span class=machine><span class=font-tt>long int</span></span> without |
| 619 |
loss of precision. Values of Caml type <span class=machine><span class=font-tt>int</span></span> have the natural word |
| 620 |
size of the platform minus one bit of tag, hence the conversion from IDL |
| 621 |
types <span class=machine><span class=font-tt>int</span></span> and <span class=machine><span class=font-tt>long</span></span> loses the most significant bit on 32-bit |
| 622 |
platforms. On 64-bit platforms, the conversion from <span class=machine><span class=font-tt>int</span></span> is exact, |
| 623 |
but the conversion from <span class=machine><span class=font-tt>long</span></span> loses the most significant bit.</p><p>If no explicit integer attribute is given for an <span class=machine><span class=font-tt>int</span></span> or <span class=machine><span class=font-tt>long</span></span> type, |
| 624 |
the <span class=machine><span class=font-tt>int</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>default</span></span> or <span class=machine><span class=font-tt>long</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>default</span></span> attribute of the enclosing |
| 625 |
interface, if any, determines the kind of the integer. |
| 626 |
If no <span class=machine><span class=font-tt>int</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>default</span></span> or <span class=machine><span class=font-tt>long</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>default</span></span> attribute is in scope, the kind |
| 627 |
<span class=machine><span class=font-tt>camlint</span></span> is assumed, which maps IDL <span class=machine><span class=font-tt>int</span></span> and <span class=machine><span class=font-tt>long</span></span> types to the |
| 628 |
Caml <span class=machine><span class=font-tt>int</span></span> type.</p> |
| 629 |
|
| 630 |
<h3 id="sec20" class="subsection">3.2 Pointers</h-><p>The mapping of IDL pointer types depends on their kinds. Writing |
| 631 |
[[<span class=font-it>ty</span>]] for the Caml type corresponding to the IDL type |
| 632 |
<span class=font-it>ty</span>, we have: |
| 633 |
</p><pre> |
| 634 |
[ref] <span class=font-it>ty</span> * ⇒ [[<span class=font-it>ty</span>]] |
| 635 |
[unique] <span class=font-it>ty</span> * ⇒ [[<span class=font-it>ty</span>]] option |
| 636 |
[ptr] <span class=font-it>ty</span> * ⇒ [[<span class=font-it>ty</span>]] Com.opaque |
| 637 |
</pre><p> |
| 638 |
In other terms, IDL pointers of kind <span class=machine><span class=font-tt>ref</span></span> are ignored during the mapping: |
| 639 |
<span class=machine><span class=font-tt>[ref] </span></span><span class=font-it>ty</span><span class=machine><span class=font-tt> *</span></span> is mapped to the same Caml type as <span class=font-it>ty</span>. |
| 640 |
A pointer <span class=font-it>p</span> to a C value <span class=font-it>c</span><span class=machine><span class=font-tt> = *</span></span><span class=font-it>p</span> is translated to |
| 641 |
the Caml value corresponding to <span class=font-it>c</span>.</p><p>IDL pointers of kind <span class=machine><span class=font-tt>unique</span></span> are mapped to an <span class=machine><span class=font-tt>option</span></span> type. The |
| 642 |
option value is <span class=machine><span class=font-tt>None</span></span> for a null pointer, and <span class=machine><span class=font-tt>Some(</span></span><span class=font-it>v</span><span class=machine><span class=font-tt>)</span></span> |
| 643 |
for a non-null pointer to a C value <span class=font-it>c</span> that translates to the ML |
| 644 |
value <span class=font-it>v</span>.</p><p>IDL pointers of kind <span class=machine><span class=font-tt>ptr</span></span> are mapped to a <span class=machine><span class=font-tt>Com.opaque</span></span> type. |
| 645 |
This is an abstract type that encapsulates the C pointer without |
| 646 |
attempting to convert it to an ML data structure.</p><p>IDL pointers of kind <span class=machine><span class=font-tt>ignore</span></span> denote struct fields and function |
| 647 |
parameters that need not be exposed in the Caml code. Those pointers |
| 648 |
are simply set to null when converting from Caml to C, and ignored |
| 649 |
when converting from C to Caml. They cannot occur elsewhere.</p><p>If no explicit pointer kind is given, the <span class=machine><span class=font-tt>pointer</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>default</span></span> attribute |
| 650 |
of the enclosing interface, if any, determines the kind of the pointer. |
| 651 |
If no <span class=machine><span class=font-tt>pointer</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>default</span></span> attribute is in scope, the kind <span class=machine><span class=font-tt>unique</span></span> is |
| 652 |
assumed.</p> |
| 653 |
|
| 654 |
<h3 id="sec21" class="subsection">3.3 Arrays</h-><p>IDL arrays of characters that carry the <span class=machine><span class=font-tt>[string]</span></span> attribute are mapped |
| 655 |
to the Caml <span class=machine><span class=font-tt>string</span></span> type: |
| 656 |
</p><div class="tableau"> |
| 657 |
<div class="center"><table border=1 style="border-spacing:0;width:80%" class="cellpadding1"><tr><td style="text-align:center;border:solid 1px;white-space:nowrap" ><span class=font-bold>IDL type </span><span class=font-bold><span class=font-it>ty</span></span></td><td style="text-align:center;border:solid 1px;white-space:nowrap" ><span class=font-bold>Caml type </span>[[<span class=font-it>ty</span>]] </td></tr> |
| 658 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" > |
| 659 |
<span class=machine><span class=font-tt>[string] char []</span></span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>string</span></span> </td></tr> |
| 660 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>[string] unsigned char []</span></span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>string</span></span> </td></tr> |
| 661 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>[string] signed char []</span></span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>string</span></span> </td></tr> |
| 662 |
<tr><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>[string] byte []</span></span></td><td style="text-align:left;border:solid 1px;white-space:nowrap" ><span class=machine><span class=font-tt>string</span></span> </td></tr> |
| 663 |
</table></div></div><p> |
| 664 |
Caml string values are translated to standard null-terminated C strings. |
| 665 |
Be careful about embedded null characters in the Caml string, which |
| 666 |
will be recognized as end of string by C functions.</p><p>IDL arrays of characters that carry the <span class=machine><span class=font-tt>[byte]</span></span> attribute are mapped |
| 667 |
to the Caml <span class=machine><span class=font-tt>bytes</span></span> type of byte arrays.</p><p>IDL arrays carrying the <span class=machine><span class=font-tt>[bigarray]</span></span> attribute are translated to Caml |
| 668 |
“big arrays”, as described in the next section.</p><p>All other IDL arrays are translated to ML arrays: |
| 669 |
</p><pre> |
| 670 |
<span class=font-it>ty</span> [] ⇒ [[<span class=font-it>ty</span>]] array |
| 671 |
</pre><p> |
| 672 |
For instance, <span class=machine><span class=font-tt>double []</span></span> becomes <span class=machine><span class=font-tt>float array</span></span>. |
| 673 |
Consequently, multi-dimensional arrays are translated to Caml arrays |
| 674 |
of arrays. For instance, <span class=machine><span class=font-tt>int [][]</span></span> becomes <span class=machine><span class=font-tt>int array array</span></span>.</p><p>If the <span class=machine><span class=font-tt>unique</span></span> attribute is given, the IDL array is translated to an |
| 675 |
ML option type: |
| 676 |
</p><pre> |
| 677 |
[string,unique] char [] ⇒ string option |
| 678 |
[unique] <span class=font-it>ty</span> [] ⇒ [[<span class=font-it>ty</span>]] array option |
| 679 |
</pre><p> |
| 680 |
As in the case of pointers of kind <span class=machine><span class=font-tt>unique</span></span>, the option value is |
| 681 |
<span class=machine><span class=font-tt>None</span></span> for a null C pointer, and <span class=machine><span class=font-tt>Some(</span></span><span class=font-it>v</span><span class=machine><span class=font-tt>)</span></span> for a non-null |
| 682 |
C pointer to a C array that translates to the ML string or array <span class=font-it>v</span>.</p><p>Conversion between a C array and an ML array proceed element by |
| 683 |
element. For the conversion from C to ML, the number of elements of |
| 684 |
the ML array is determined as follows (in the order presented): |
| 685 |
</p><ul class="itemize"><li class="li-itemize"> |
| 686 |
By the <span class=machine><span class=font-tt>length</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span> attribute, if present. |
| 687 |
</li><li class="li-itemize">By the <span class=machine><span class=font-tt>size</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span> attribute, if present. |
| 688 |
</li><li class="li-itemize">By the bound written in the array type, if any. |
| 689 |
</li><li class="li-itemize">By searching the first null element of the C array, if the |
| 690 |
<span class=machine><span class=font-tt>null</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>terminated</span></span> attribute is present. |
| 691 |
</li></ul><p> |
| 692 |
For instance, C values of IDL type <span class=machine><span class=font-tt>[length</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is(n)] double[]</span></span> are |
| 693 |
mapped to Caml <span class=machine><span class=font-tt>float array</span></span> of <span class=machine><span class=font-tt>n</span></span> elements. C values of IDL type |
| 694 |
<span class=machine><span class=font-tt>double[10]</span></span> are mapped to Caml <span class=machine><span class=font-tt>float array</span></span> of 10 elements.</p><p>The <span class=machine><span class=font-tt>length</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span> and <span class=machine><span class=font-tt>size</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span> attributes take as argument one or |
| 695 |
several limited expressions. Each expression applies to one dimension |
| 696 |
of the array. For instance, <span class=machine><span class=font-tt>[size</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is(*dimx, *dimy)] double d[][]</span></span> |
| 697 |
specifies a matrix of <span class=machine><span class=font-tt>double</span></span> whose first dimension has size |
| 698 |
<span class=machine><span class=font-tt>*dimx</span></span> and the second has size <span class=machine><span class=font-tt>*dimy</span></span>.</p> |
| 699 |
|
| 700 |
<h3 id="sec22" class="subsection">3.4 Big arrays</h-><p>IDL arrays of integers or floats that carry the <span class=machine><span class=font-tt>[bigarray]</span></span> attribute |
| 701 |
are mapped to one of the Caml <span class=machine><span class=font-tt>Bigarray</span></span> types: <span class=machine><span class=font-tt>Array1.t</span></span> for |
| 702 |
one-dimensional arrays, <span class=machine><span class=font-tt>Array2.t</span></span> for 2-dimensional arrays, |
| 703 |
<span class=machine><span class=font-tt>Array3.t</span></span> for 3-dimensional arrays, and <span class=machine><span class=font-tt>Genarray.t</span></span> for arrays of 4 |
| 704 |
dimensions or more.</p><p>If the <span class=machine><span class=font-tt>[fortran]</span></span> attribute is given, the big array is accessed |
| 705 |
from Caml using the Fortran conventions (array indices start at 1; |
| 706 |
column-major memory layout). By default, the big array is accessed |
| 707 |
from Caml using the C conventions (array indices start at 0; row-major |
| 708 |
memory layout).</p><p>If the <span class=machine><span class=font-tt>[managed]</span></span> attribute is given on a big array type that is |
| 709 |
result type or out parameter type of a function, Caml assumes that the |
| 710 |
corresponding C array was allocated using <span class=machine><span class=font-tt>malloc()</span></span>, and is not |
| 711 |
referenced anywhere else; then, the Caml garbage collector will free |
| 712 |
the C array when the corresponding Caml big array becomes unreachable. |
| 713 |
By default, Caml assumes that result or out C arrays are statically or |
| 714 |
permanently allocated, and keeps a pointer to them during conversion |
| 715 |
to Caml big arrays, and does not free them when the Caml bigarrays |
| 716 |
become unreachable.</p><p>Finally, the <span class=machine><span class=font-tt>[unique]</span></span> attribute applies to bigarrays as to arrays, |
| 717 |
that is, it maps a null C pointer to <span class=machine><span class=font-tt>None</span></span>, and a non-null C pointer |
| 718 |
<span class=font-it>p</span> to <span class=machine><span class=font-tt>Some(</span></span><span class=font-it>v</span><span class=machine><span class=font-tt>)</span></span> where <span class=font-it>v</span> is the ML bigarray |
| 719 |
resulting from the translation of <span class=font-it>p</span>.</p> |
| 720 |
|
| 721 |
<h3 id="sec23" class="subsection">3.5 Structs</h-><p>IDL structs are mapped to Caml record types. The names and types of |
| 722 |
the IDL struct fields determine the names and types of the Caml record |
| 723 |
type: |
| 724 |
</p><pre> |
| 725 |
struct <span class=font-it>s</span> { ... ; <span class=font-it>ty</span><sub><span class=font-it>i</span></sub> <span class=font-it>id</span><sub><span class=font-it>i</span></sub> ; ... } becomes type <span class=font-it>s</span> = { ... ; <span class=font-it>id</span><sub><span class=font-it>i</span></sub> : [[<span class=font-it>ty</span><sub><span class=font-it>i</span></sub>]] ; ... } |
| 726 |
</pre><p> |
| 727 |
Example: <span class=machine><span class=font-tt>struct s </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt> int n; double d[4]; </span></span><span class=machine><span class=font-tt>}</span></span> becomes |
| 728 |
<span class=machine><span class=font-tt>type s = </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt>n: int; d: float array</span></span><span class=machine><span class=font-tt>}</span></span>.</p><p>Exceptions to this rule are as follows: |
| 729 |
</p><ul class="itemize"><li class="li-itemize"> |
| 730 |
Fields of the IDL struct that are pointers with the <span class=machine><span class=font-tt>[ignore]</span></span> |
| 731 |
attribute do not appear in the Caml record type. |
| 732 |
Example: <span class=machine><span class=font-tt>struct s </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt> double x,y; [ignore] void * data; </span></span><span class=machine><span class=font-tt>}</span></span> |
| 733 |
becomes <span class=machine><span class=font-tt>type struct</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>s = </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt>x : float; y: float</span></span><span class=machine><span class=font-tt>}</span></span>. |
| 734 |
Those ignored pointer fields are set to <span class=machine><span class=font-tt>NULL</span></span> when converting from a |
| 735 |
Caml record to a C struct.</li><li class="li-itemize">Integer fields of the IDL struct that appear in a <span class=machine><span class=font-tt>length</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span>, |
| 736 |
<span class=machine><span class=font-tt>size</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span> or <span class=machine><span class=font-tt>switch</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span> attribute of another field also do not appear |
| 737 |
in the Caml record type. (We call those fields <em>dependent</em> fields.) |
| 738 |
Example: <span class=machine><span class=font-tt>struct s </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt> int idx; int len; [size</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is(len)] double d[]; </span></span><span class=machine><span class=font-tt>}</span></span> |
| 739 |
is translated to the Caml record type |
| 740 |
<span class=machine><span class=font-tt>type struct</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>s = </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt>idx: int; d: float array</span></span><span class=machine><span class=font-tt>}</span></span>. |
| 741 |
The value of <span class=machine><span class=font-tt>len</span></span> is recovered from the size of the Caml array <span class=machine><span class=font-tt>d</span></span>, |
| 742 |
and thus doesn’t need to be represented explicitly in the Caml record.</li><li class="li-itemize">If, after elimination of ignored pointer fields and dependent |
| 743 |
fields as described above, the IDL struct has only one field |
| 744 |
<span class=font-it>ty</span> <span class=font-it>id</span>, we avoid creating a one-field Caml record type |
| 745 |
and translate the IDL struct type directly to the Caml type |
| 746 |
[[<span class=font-it>ty</span>]]. |
| 747 |
Example: <span class=machine><span class=font-tt>struct s </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt> int len; [size</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is(len)] double d[]; </span></span><span class=machine><span class=font-tt>}</span></span> |
| 748 |
is translated to the Caml type abbreviation <span class=machine><span class=font-tt>type struct</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>s = double array</span></span>.</li><li class="li-itemize">The names of labels in the Caml record type can be changed by |
| 749 |
using the <span class=syntax-token>mlname</span> attribute on struct field declarations. For instance, |
| 750 |
<pre> |
| 751 |
struct s { int n; [mlname(p)] int q; } |
| 752 |
becomes type s = { n : int; p : int } |
| 753 |
</pre></li><li class="li-itemize">The Caml type system makes it difficult to use two record types |
| 754 |
defined in the same module and having some label names in common. |
| 755 |
Thus, if CamlIDL encounters two or more structs having |
| 756 |
identically-named fields, it prefixes the Caml label names by the names |
| 757 |
of the structs in order to distinguish them. For instance: |
| 758 |
<pre> |
| 759 |
struct s1 { int x; int y; } |
| 760 |
struct s2 { double x; double t; } |
| 761 |
struct s3 { int z; } |
| 762 |
becomes type s1 = { s1_x: int; s1_y: int } |
| 763 |
and s2 = { s2_x: float; s2_t: float } |
| 764 |
and s3 = { z: int } |
| 765 |
</pre> |
| 766 |
The labels for <span class=machine><span class=font-tt>s1</span></span> and <span class=machine><span class=font-tt>s2</span></span> have been prefixed by <span class=machine><span class=font-tt>s1</span></span><span class=machine><span class=font-tt>_</span></span> and |
| 767 |
<span class=machine><span class=font-tt>s2</span></span><span class=machine><span class=font-tt>_</span></span> respectively, to avoid ambiguity on the <span class=machine><span class=font-tt>x</span></span> label. However, the |
| 768 |
label <span class=machine><span class=font-tt>z</span></span> for <span class=machine><span class=font-tt>s3</span></span> is not prefixed, since it is not used elsewhere.<p>The prefix added in front of multiply-defined labels is taken from the |
| 769 |
struct name, if any, and otherwise from the name of the nearest |
| 770 |
enclosing struct, union or typedef. For instance: |
| 771 |
</p><pre> |
| 772 |
typedef struct { int x; } t; |
| 773 |
struct s4 { struct { int x; } z; }; |
| 774 |
becomes type t = { t_x: int } |
| 775 |
and s4 = { z: struct_1 } |
| 776 |
and struct_1 = { s4_x: int } |
| 777 |
</pre><p>The “minimal prefixing” strategy described above is the default |
| 778 |
behavior of <span class=machine><span class=font-tt>camlidl</span></span>. If the <span class=machine><span class=font-tt>-prefix-all-labels</span></span> option is given, |
| 779 |
all record labels are prefixed, whether they occur several times or |
| 780 |
not. If the <span class=machine><span class=font-tt>-keep-labels</span></span> option is given, no automatic prefixing |
| 781 |
takes place; the naming of record labels is left entirely under the |
| 782 |
user’s control, via <span class=syntax-token>mlname</span> annotations.</p></li></ul> |
| 783 |
|
| 784 |
<h3 id="sec24" class="subsection">3.6 Unions</h-><p>IDL discriminated unions are translated to Caml sum types. Each case |
| 785 |
of the union corresponds to a constructor of the sum type. The |
| 786 |
constructor is constant if the union case has no associated field, |
| 787 |
otherwise has one argument corresponding to the union case field. If |
| 788 |
the union has a <span class=machine><span class=font-tt>default</span></span> case, an extra constructor |
| 789 |
<span class=machine><span class=font-tt>Default</span></span><span class=machine><span class=font-tt>_</span></span><span class=font-it>unionname</span> is added to the Caml sum type, carrying an |
| 790 |
<span class=machine><span class=font-tt>int</span></span> argument (the value of the discriminating field), |
| 791 |
and possibly another argument corresponding to the default field. |
| 792 |
Examples: |
| 793 |
</p><pre> |
| 794 |
union u1 { case A: int x; case B: case C: double d; case D: ; } |
| 795 |
becomes type u1 = A of int | B of float | C of float | D |
| 796 |
union u2 { case A: int x; case B: double d; default: ; } |
| 797 |
becomes type u2 = A of int | B of float | Default_u of int |
| 798 |
union u3 { case A: int x; default: double d; } |
| 799 |
becomes type u3 = A of int | Default_v of int * double |
| 800 |
</pre><p> |
| 801 |
All IDL unions must be discriminated, either via the special syntax |
| 802 |
<span class=machine><span class=font-tt>union </span></span><span class=font-it>name</span><span class=machine><span class=font-tt> switch(int </span></span><span class=font-it>discr</span><span class=machine><span class=font-tt>)</span></span>…, or via the |
| 803 |
attribute <span class=machine><span class=font-tt>switch</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is(</span></span><span class=font-it>discr</span><span class=machine><span class=font-tt>)</span></span>, where <span class=font-it>discr</span> is a C l-value |
| 804 |
built from other parameters of the current function, or other fields |
| 805 |
of the current <span class=machine><span class=font-tt>struct</span></span>. Both the discriminant and the |
| 806 |
case labels must be of an integer type. Unless a <span class=machine><span class=font-tt>default</span></span> case is |
| 807 |
given, the value of the discriminant must be one of the cases of the |
| 808 |
union.</p> |
| 809 |
|
| 810 |
<h3 id="sec25" class="subsection">3.7 Enums</h-><p>IDL enums are translated to Caml enumerated types (sum types with only |
| 811 |
constant constructors). The names of the constructors are determined |
| 812 |
by the names of the enum labels. The values attached to the enum |
| 813 |
labels are ignored. |
| 814 |
Example: |
| 815 |
<span class=machine><span class=font-tt>enum e </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt> A, B = 2, C = 4 </span></span><span class=machine><span class=font-tt>}</span></span> becomes <span class=machine><span class=font-tt>type enum</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>e = A | B | C</span></span>.</p><p>The <span class=syntax-token>set</span> attribute can be applied to a named enum to denote a |
| 816 |
bitfield obtained by logical “or” of zero, one or several labels of |
| 817 |
the enum. The corresponding ML value is a list of zero, one or |
| 818 |
several constructors of the Caml enumerated type. Consider for |
| 819 |
instance: |
| 820 |
</p><pre>enum e { A = 1, B = 2, C = 4 }; |
| 821 |
typedef [set] enum e eset; |
| 822 |
</pre><p>The Caml type <span class=machine><span class=font-tt>eset</span></span> is equal to <span class=machine><span class=font-tt>enum</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>e list</span></span>. |
| 823 |
The C integer 6 (= <span class=machine><span class=font-tt>B | C</span></span>) is translated to the ML list <span class=machine><span class=font-tt>[B; C]</span></span>. |
| 824 |
The ML list <span class=machine><span class=font-tt>[A; C]</span></span> is translated to the C integer <span class=machine><span class=font-tt>A | C</span></span>, that is <span class=machine><span class=font-tt>5</span></span>.</p> |
| 825 |
|
| 826 |
<h3 id="sec26" class="subsection">3.8 Type definitions</h-><p>An IDL <span class=machine><span class=font-tt>typedef</span></span> statement is normally translated |
| 827 |
to a Caml type abbreviation. For instance, |
| 828 |
<span class=machine><span class=font-tt>typedef [string] char * str</span></span> becomes <span class=machine><span class=font-tt>type str = string</span></span>.</p><p>If the <span class=syntax-token>abstract</span> attribute is given, a Caml abstract type is |
| 829 |
generated instead of a type abbreviation, thus hinding from Caml the |
| 830 |
representation of the type in question. For instance, |
| 831 |
<span class=machine><span class=font-tt>typedef [abstract] void * handle</span></span> becomes <span class=machine><span class=font-tt>type handle</span></span>. |
| 832 |
In this case, the IDL type in the <span class=machine><span class=font-tt>typedef</span></span> is ignored.</p><p>If the <span class=syntax-token>mltype</span> <span class=syntax-token>(</span> <span class=syntax-token>"</span> <span class=nonterminal>caml-type-expr</span> <span class=syntax-token>"</span> <span class=syntax-token>)</span> attribute is given, |
| 833 |
the Caml type is made equal to <span class=nonterminal>caml-type-expr</span>. This is often used |
| 834 |
in conjunction with the <span class=syntax-token>ml2c</span> and <span class=syntax-token>c2ml</span> attributes to implement |
| 835 |
custom translation of data structures between C and ML. For instance, |
| 836 |
<span class=machine><span class=font-tt>typedef [mltype("int list")] struct mylist</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>struct * mylist</span></span> |
| 837 |
becomes <span class=machine><span class=font-tt>type mylist = int list</span></span>.</p><p>If the <span class=syntax-token>c2ml(</span><span class=nonterminal>funct-name</span><span class=syntax-token>)</span> <span class=nonterminal>and</span> <span class=machine><span class=font-tt>ml2c(</span></span>funct-name<span class=machine><span class=font-tt>)</span></span> attributes are |
| 838 |
given, the user-provided C functions given as attributes will be |
| 839 |
called to perform Caml to C and C to Caml conversions for values of |
| 840 |
the typedef-ed type, instead of using the <span class=machine><span class=font-tt>camlidl</span></span>-generated |
| 841 |
conversion functions. This allows user-controlled translation of data |
| 842 |
structures. The prototypes of the conversion functions must be |
| 843 |
</p><pre> |
| 844 |
value c2ml(<span class=font-it>ty</span> * input); |
| 845 |
void ml2c(value input, <span class=font-it>ty</span> * output); |
| 846 |
</pre><p> |
| 847 |
where <span class=font-it>ty</span> is the name of the type defined by <span class=machine><span class=font-tt>typedef</span></span>. In other |
| 848 |
terms, the <span class=machine><span class=font-tt>c2ml</span></span> function is passed a reference to a <span class=font-it>ty</span> and |
| 849 |
returns the corresponding Caml value, while the <span class=machine><span class=font-tt>ml2c</span></span> function is |
| 850 |
passed a Caml value as first argument and stores the corresponding C |
| 851 |
value in the <span class=font-it>ty</span> reference passed as second argument.</p><p>If the <span class=syntax-token>finalize(</span><span class=nonterminal>final-fn</span><span class=syntax-token>)</span> attribute is given in combination with the |
| 852 |
<span class=syntax-token>abstract</span> attribute, the function <span class=nonterminal>final-fn</span> is called when |
| 853 |
the Caml block representing a value of this typedef becomes |
| 854 |
unreachable from Caml and is reclaimed by the Caml garbage collector. |
| 855 |
Similarly, <span class=syntax-token>compare(</span><span class=nonterminal>compare-fn</span><span class=syntax-token>)</span> and <span class=syntax-token>hash(</span><span class=nonterminal>hash-fn</span><span class=syntax-token>)</span> attach a |
| 856 |
comparison function and a hashing function (respectively) to Caml |
| 857 |
values for this typedef. The comparison function is called when two |
| 858 |
Caml values of this typedef are compared using the generic comparisons |
| 859 |
<span class=machine><span class=font-tt>compare</span></span>, <span class=machine><span class=font-tt>=</span></span>, <span class=machine><span class=font-tt><</span></span>, etc. The hashing function is called when |
| 860 |
<span class=machine><span class=font-tt>Hashtbl.hash</span></span> is applied to a Caml value of this typedef. |
| 861 |
The prototype of the finalization, comparison and hashing functions are: |
| 862 |
</p><pre> |
| 863 |
value <span class=font-it>final-fn</span>(<span class=font-it>ty</span> * x); |
| 864 |
int <span class=font-it>compare-fn</span>(<span class=font-it>ty</span> * x, <span class=font-it>ty</span> * y); |
| 865 |
long <span class=font-it>hash-fn</span>(<span class=font-it>ty</span> * x); |
| 866 |
</pre><p> |
| 867 |
That is, their arguments are passed by reference. The comparison |
| 868 |
function must return an integer that is negative, zero, or positive |
| 869 |
depending on whether its first argument is smaller, equal or greater |
| 870 |
than its second argument. The hashing function returns a suitable |
| 871 |
hash value for its argument.</p><p>If the <span class=syntax-token>errorcheck(</span><span class=nonterminal>fn</span><span class=syntax-token>)</span> attribute is provided for the <span class=machine><span class=font-tt>typedef</span></span> <span class=font-it>ty</span>, |
| 872 |
the error checking function <span class=nonterminal>fn</span> is called each time a function result |
| 873 |
of type <span class=font-it>ty</span> is converted from C to Caml. The function can then check |
| 874 |
the <span class=font-it>ty</span> value for values indicating an error condition, and raise the |
| 875 |
appropriate exception. If in addition the <span class=syntax-token>errorcode</span> attribute is |
| 876 |
provided, the conversion from C to Caml is suppressed: values of type |
| 877 |
<span class=font-it>ty</span> are only passed to <span class=nonterminal>fn</span> for error checking, then discarded.</p> |
| 878 |
|
| 879 |
<h3 id="sec27" class="subsection">3.9 Functions</h-><p>IDL function declarations are translated to Caml functions. |
| 880 |
The parameters and results of the Caml function are determined from |
| 881 |
those of the IDL function according to the following rules: |
| 882 |
</p><ul class="itemize"><li class="li-itemize"> |
| 883 |
First, dependent parameters (parameters that are <span class=machine><span class=font-tt>size</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span>, |
| 884 |
<span class=machine><span class=font-tt>length</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span> or <span class=machine><span class=font-tt>switch</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>is</span></span> of other parameters) as well as parameters |
| 885 |
that are ignored pointers are removed.</li><li class="li-itemize">The remaining parameters are split into Caml function inputs and |
| 886 |
Caml function outputs. Parameters with the <span class=machine><span class=font-tt>[in]</span></span> attribute are added |
| 887 |
to the inputs of the function. Parameters with the <span class=machine><span class=font-tt>[out]</span></span> |
| 888 |
attribute are added to the outputs of the function. Parameters with |
| 889 |
the <span class=machine><span class=font-tt>[in,out]</span></span> attribute are added both to the inputs and to the |
| 890 |
outputs of the function, unless they are of type <span class=machine><span class=font-tt>bytes</span></span> or big array, |
| 891 |
in which case they are added to the inputs of the function only. |
| 892 |
(The reason for this exception is that byte arrays and big arrays are |
| 893 |
shared between Caml and C, thus allowing true <span class=machine><span class=font-tt>in,out</span></span> behavior on the |
| 894 |
Caml function parameter, while other data types are copied during |
| 895 |
Caml/C conversion, thus turning a C <span class=machine><span class=font-tt>in,out</span></span> parameter into a Caml |
| 896 |
<span class=machine><span class=font-tt>copy in, copy out</span></span> parameter, that is, one parameter and one result.)</li><li class="li-itemize">The return value of the IDL function is added to the outputs of |
| 897 |
the Caml function (in first position), unless it is of type <span class=machine><span class=font-tt>void</span></span> or |
| 898 |
of a type name that carries the <span class=machine><span class=font-tt>errorcode</span></span> attribute. In the latter |
| 899 |
two cases, the return value of the IDL function is not transmitted to |
| 900 |
Caml. |
| 901 |
</li><li class="li-itemize">The Caml function is then given type |
| 902 |
<span class=nonterminal>in</span><sub>1</sub> <span class=syntax-token>-></span> … <span class=syntax-token>-></span> <span class=nonterminal>in</span><sub><span class=font-it>p</span></sub> <span class=syntax-token>-></span> <span class=nonterminal>out</span><sub>1</sub> <span class=syntax-token>*</span> … <span class=syntax-token>*</span> <span class=nonterminal>out</span><sub><span class=font-it>q</span></sub> |
| 903 |
where <span class=nonterminal>in</span><sub>1</sub> … <span class=nonterminal>in</span><sub><span class=font-it>p</span></sub> are the types of its inputs |
| 904 |
and <span class=nonterminal>out</span><sub>1</sub> … <span class=nonterminal>out</span><sub><span class=font-it>q</span></sub> are the types of its outputs. |
| 905 |
If there are no inputs, a <span class=machine><span class=font-tt>unit</span></span> parameter is added. |
| 906 |
If there are no outputs, a <span class=machine><span class=font-tt>unit</span></span> result is added. |
| 907 |
</li></ul><p> |
| 908 |
Examples: |
| 909 |
</p><pre> |
| 910 |
int f([in] double x, [in] double y) f : float -> float -> int |
| 911 |
</pre><blockquote class="quote"> Two <span class=machine><span class=font-tt>double</span></span> input, one <span class=machine><span class=font-tt>int</span></span> output </blockquote><pre> |
| 912 |
void g([in] int x) g : int -> unit |
| 913 |
</pre><blockquote class="quote"> One <span class=machine><span class=font-tt>int</span></span> input, no output </blockquote><pre> |
| 914 |
int h() h : unit -> int |
| 915 |
</pre><blockquote class="quote"> No input, one <span class=machine><span class=font-tt>int</span></span> result </blockquote><pre> |
| 916 |
void i([in] int x, [out] double * y) i : int -> double |
| 917 |
</pre><blockquote class="quote"> One <span class=machine><span class=font-tt>int</span></span> input, one <span class=machine><span class=font-tt>double</span></span> output (as an <span class=machine><span class=font-tt>out</span></span> |
| 918 |
parameter) </blockquote><pre> |
| 919 |
int j([in] int x, [out] double * y) j : int -> int * double |
| 920 |
</pre><blockquote class="quote"> One <span class=machine><span class=font-tt>int</span></span> input, one <span class=machine><span class=font-tt>int</span></span> output (in the result), one |
| 921 |
<span class=machine><span class=font-tt>double</span></span> output (as an <span class=machine><span class=font-tt>out</span></span> parameter) </blockquote><pre> |
| 922 |
void k([in,out,ref] int * x) k : int -> int |
| 923 |
</pre><blockquote class="quote"> The <span class=machine><span class=font-tt>in,out</span></span> parameter is both one <span class=machine><span class=font-tt>int</span></span> input and one |
| 924 |
<span class=machine><span class=font-tt>int</span></span> output. </blockquote><pre> |
| 925 |
HRESULT l([in] int x, [out] int * res1, [out] int * res2) |
| 926 |
l : int -> int * int |
| 927 |
</pre><blockquote class="quote"> <span class=machine><span class=font-tt>HRESULT</span></span> is a predefined type with the <span class=machine><span class=font-tt>errorcode</span></span> |
| 928 |
attribute, hence it is ignored. It remains one <span class=machine><span class=font-tt>int</span></span> input and |
| 929 |
two <span class=machine><span class=font-tt>int</span></span> outputs (<span class=machine><span class=font-tt>out</span></span> parameters) </blockquote><pre> |
| 930 |
void m([in] int len, [in,size_is(len)] double d[]) |
| 931 |
m : float array -> int |
| 932 |
</pre><blockquote class="quote"> <span class=machine><span class=font-tt>len</span></span> is a dependent parameter, hence is ignored. The |
| 933 |
only input is the <span class=machine><span class=font-tt>double</span></span> array </blockquote><pre> |
| 934 |
void n([in] int inputlen, [out] int * outputlen, |
| 935 |
[in,out,size_is(inputlen),length_is(*outputlen)] double d[]) |
| 936 |
n : float array -> float array |
| 937 |
</pre><blockquote class="quote"> The two parameters <span class=machine><span class=font-tt>inputlen</span></span> and <span class=machine><span class=font-tt>outputlen</span></span> are |
| 938 |
dependent, hence ignored. The <span class=machine><span class=font-tt>double</span></span> array is both an input |
| 939 |
and an output. </blockquote><pre> |
| 940 |
void p([in] int dimx, [in] int dimy, |
| 941 |
[in,out,bigarray,size_is(dimx,dimy)] double d[][]) |
| 942 |
p : (float, Bigarray.float64_elt, Bigarray.c_layout) Bigarray.Array2.t -> unit |
| 943 |
</pre><blockquote class="quote"> The two parameters <span class=machine><span class=font-tt>dimx</span></span> and <span class=machine><span class=font-tt>dimy</span></span> are dependent |
| 944 |
(determined from the dimensions of the big array argument), |
| 945 |
hence ignored. The two-dimensional array <span class=machine><span class=font-tt>d</span></span>, although marked <span class=machine><span class=font-tt>[in,out]</span></span>, |
| 946 |
is a big array, hence passed as an input that will be modified in |
| 947 |
place by the C function <span class=machine><span class=font-tt>p</span></span>. The Caml function has no outputs. |
| 948 |
</blockquote> |
| 949 |
|
| 950 |
<h4 id="sec28" class="paragraph">Error checking:</h4><p> |
| 951 |
For every output that is of a named type with the <span class=syntax-token>errorcheck(</span><span class=nonterminal>fn</span><span class=syntax-token>)</span> |
| 952 |
attribute, the error checking function <span class=nonterminal>fn</span> is called after the C |
| 953 |
function returns. That function is assumed to raise a Caml exception |
| 954 |
if it finds an output denoting an error.</p> |
| 955 |
|
| 956 |
<h4 id="sec29" class="paragraph">Custom calling and deallocation sequences:</h4><p> |
| 957 |
The IDL declaration for a function can optionally specify a custom |
| 958 |
calling sequence and/or a custom deallocation sequence, via <span class=nonterminal>quote</span> |
| 959 |
clauses following the function declaration: |
| 960 |
</p><div class="syntax"><table class="display dcenter"><tr style="vertical-align:middle"><td class="dcell"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="text-align:right;white-space:nowrap" ><span class=nonterminal>function-decl</span></td><td style="text-align:center;white-space:nowrap" >::=</td><td style="text-align:left;white-space:nowrap" > |
| 961 |
<a href="#attributes" class="syntax"><span class=nonterminal>attributes</span></a> <a href="#type-spec" class="syntax"><span class=nonterminal>type-spec</span></a> {<span class=syntax-token>*</span>} <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>(</span> <a href="#params" class="syntax"><span class=nonterminal>params</span></a> <span class=syntax-token>)</span> |
| 962 |
{ <span class=syntax-token>quote</span><span class=syntax-token>(</span> <a href="#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>,</span> <a href="#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>)</span> } </td></tr> |
| 963 |
<tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 964 |
</table></td></tr> |
| 965 |
</table></div><p> |
| 966 |
The general shape of a <span class=machine><span class=font-tt>camlidl</span></span>-generated stub function is as |
| 967 |
follows: |
| 968 |
</p><pre> |
| 969 |
value caml_wrapper(value camlparam1, ..., value camlparamK) |
| 970 |
|
| 971 |
/* Convert the function parameters from Caml to C */ |
| 972 |
param1 = ...; |
| 973 |
... |
| 974 |
paramN = ...; |
| 975 |
/* Call the C function ’ident’ */ |
| 976 |
_res = ident(param1, ..., paramN); |
| 977 |
/* Convert the function result and out parameters to Caml values */ |
| 978 |
camlres = ...; |
| 979 |
/* Return result to Caml */ |
| 980 |
return camlres; |
| 981 |
|
| 982 |
</pre><p> |
| 983 |
A <span class=syntax-token>quote(call,</span> <a href="#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>)</span> clause causes the C statements in |
| 984 |
<a href="#string" class="syntax"><span class=nonterminal>string</span></a> to be inserted in the generated stub code |
| 985 |
instead of the default calling sequence <span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>res = ident(param1, ..., paramN)</span></span>. |
| 986 |
Thus, the statements in <a href="#string" class="syntax"><span class=nonterminal>string</span></a> find the converted parameters in |
| 987 |
local variables that have the same names as the parameters in the IDL |
| 988 |
declaration, and should leave the result of the function, if any, in |
| 989 |
the local variable named <span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>res</span></span>.</p><p>A <span class=syntax-token>quote(dealloc,</span> <a href="#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>)</span> clause causes the C statements in |
| 990 |
<a href="#string" class="syntax"><span class=nonterminal>string</span></a> to be inserted in the generated stub code just before the |
| 991 |
stub function returns, hence after the conversion of the C function |
| 992 |
results to Caml values. Again, the statements in <a href="#string" class="syntax"><span class=nonterminal>string</span></a> have access |
| 993 |
to the function result in the local variable named <span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>res</span></span>, and to out |
| 994 |
parameters in local variables having the same names as the |
| 995 |
parameters. Since the function results and out parameters have |
| 996 |
already been converted to Caml values, the code in <a href="#string" class="syntax"><span class=nonterminal>string</span></a> can safely |
| 997 |
deallocate the data structures they point to.</p><p>Custom calling sequences are typically used to rearrange or combine |
| 998 |
function parameters, and to perform extra error checks on the |
| 999 |
arguments and results. For instance, the Unix <span class=machine><span class=font-tt>write</span></span> system call can |
| 1000 |
be specified in IDL as follows: |
| 1001 |
</p><pre> int write([in] int fd, |
| 1002 |
[in,string,length_is(len)] char * data, |
| 1003 |
[in] int len, |
| 1004 |
[in] int ofs, |
| 1005 |
[in] int towrite) |
| 1006 |
quote(call, |
| 1007 |
" /* Validate the arguments */ |
| 1008 |
if (ofs < 0 || ofs + towrite >= len) failwith(\"write\"); |
| 1009 |
/* Perform the write */ |
| 1010 |
_res = write(fd, data + ofs, towrite); |
| 1011 |
/* Validate the result */ |
| 1012 |
if (_res == -1) failwith(\"write\"); "); |
| 1013 |
</pre><p>Custom deallocation sequences are useful to free data structures |
| 1014 |
dynamically allocated and returned by the C function. For instance, |
| 1015 |
a C function <span class=machine><span class=font-tt>f</span></span> that returns a <span class=machine><span class=font-tt>malloc</span></span>-ed string can be specified in |
| 1016 |
IDL as follows: |
| 1017 |
</p><pre> [string] char * f([in] int x) |
| 1018 |
quote(dealloc, "free(_res); "); |
| 1019 |
</pre><p>If the string is returned as an <span class=machine><span class=font-tt>out</span></span> parameter instead, we would write: |
| 1020 |
</p><pre> void f ([in] int x, [out, string*] char ** str) |
| 1021 |
quote(dealloc, "free(*str); "); |
| 1022 |
</pre> |
| 1023 |
|
| 1024 |
<h4 id="sec30" class="paragraph">Blocking functions:</h4><p> |
| 1025 |
A function can be given the attribute <span class=machine><span class=font-tt>blocking</span></span> to indicate that it |
| 1026 |
may block on an input/output operation. The generated code will then |
| 1027 |
allow other Caml threads to execute concurrently with the operation.</p> |
| 1028 |
|
| 1029 |
<h3 id="sec31" class="subsection">3.10 Interfaces</h-><p>IDL interfaces that do not have the <span class=syntax-token>object</span> attribute are |
| 1030 |
essentially ignored. That is, the declarations contained in the |
| 1031 |
interface are processed as if they occurred at the top-level of the |
| 1032 |
IDL file. The <span class=syntax-token>pointer_default</span>, <span class=syntax-token>int_default</span> and |
| 1033 |
<span class=syntax-token>long_default</span> attributes to the interface can be |
| 1034 |
used to specify the default pointer kind and integer mappings |
| 1035 |
for the declarations contained in the interface. Other attributes, as |
| 1036 |
well as the name of the super-interface if any, are ignored.</p><p>IDL interfaces having the <span class=syntax-token>object</span> attribute specify COM-style object |
| 1037 |
interfaces. The function declarations contained in the interface |
| 1038 |
specify the methods of the COM interface. Other kinds of declarations |
| 1039 |
(type declarations, <span class=syntax-token>import</span> statements, etc) are treated as if they |
| 1040 |
occurred at the top-level of the IDL file. An optional |
| 1041 |
super-interface can be given, in which case the COM interface |
| 1042 |
implements the methods of the super-interface in addition to those |
| 1043 |
specified in the IDL interface. |
| 1044 |
Example: |
| 1045 |
</p><pre>[object, uuid(...)] interface IA { typedef int t; int f(int x); } |
| 1046 |
[object] interface IB : IA { import "foo.idl"; void g([string] char * s); } |
| 1047 |
</pre><p>This defines a type <span class=machine><span class=font-tt>t</span></span> and imports the file <span class=machine><span class=font-tt>foo.idl</span></span> as usual. In |
| 1048 |
addition, two interfaces are declared: <span class=machine><span class=font-tt>IA</span></span>, containing one |
| 1049 |
method <span class=machine><span class=font-tt>f</span></span> from <span class=machine><span class=font-tt>int</span></span> to <span class=machine><span class=font-tt>int</span></span>, and <span class=machine><span class=font-tt>IB</span></span>, containing |
| 1050 |
two methods, <span class=machine><span class=font-tt>f</span></span> from <span class=machine><span class=font-tt>int</span></span> to <span class=machine><span class=font-tt>int</span></span> and <span class=machine><span class=font-tt>g</span></span> from <span class=machine><span class=font-tt>string</span></span> to <span class=machine><span class=font-tt>unit</span></span>.</p><p>The definition of an object interface <span class=font-it>i</span> generates the following |
| 1051 |
Caml definitions: |
| 1052 |
</p><ul class="itemize"><li class="li-itemize"> |
| 1053 |
An abstract type <span class=font-it>i</span> identifying the interface. |
| 1054 |
COM interfaces of type <span class=font-it>i</span> are represented in Caml |
| 1055 |
with type <span class=font-it>i</span> <span class=machine><span class=font-tt> Com.interface</span></span>. |
| 1056 |
</li><li class="li-itemize">If a super-interface <span class=font-it>s</span> is given, a conversion function |
| 1057 |
<span class=font-it>s</span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>of</span></span><span class=machine><span class=font-tt>_</span></span><span class=font-it>i</span> of type |
| 1058 |
<span class=font-it>i</span> <span class=machine><span class=font-tt> Com.interface -> </span></span> <span class=font-it>s</span> <span class=machine><span class=font-tt> Com.interface</span></span>. |
| 1059 |
</li><li class="li-itemize">If the <span class=machine><span class=font-tt>uuid(</span></span><span class=font-it>iid</span><span class=machine><span class=font-tt>)</span></span> attribute is given, a value |
| 1060 |
<span class=machine><span class=font-tt>iid</span></span><span class=machine><span class=font-tt>_</span></span><span class=font-it>i</span> of type <span class=font-it>i</span><span class=machine><span class=font-tt> Com.iid</span></span> holding the given interface |
| 1061 |
identifier. |
| 1062 |
</li><li class="li-itemize">A Caml class <span class=font-it>i</span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>class</span></span>, with the same methods as the COM |
| 1063 |
interface. |
| 1064 |
</li><li class="li-itemize">A function <span class=machine><span class=font-tt>use</span></span><span class=machine><span class=font-tt>_</span></span><span class=font-it>i</span> of type <span class=font-it>i</span> <span class=machine><span class=font-tt> Com.interface -> </span></span><span class=font-it>i</span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>class</span></span>, to transform a COM object into a Caml object. This |
| 1065 |
allows the methods of the COM object to be invoked from Caml. |
| 1066 |
</li><li class="li-itemize">A function <span class=machine><span class=font-tt>make</span></span><span class=machine><span class=font-tt>_</span></span><span class=font-it>i</span> of type <span class=machine><span class=font-tt>#</span></span><span class=font-it>i</span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>class -> </span></span><span class=font-it>i</span> |
| 1067 |
<span class=machine><span class=font-tt> Com.interface</span></span>, to transform a Caml object into a COM object with |
| 1068 |
interface <span class=font-it>i</span>. This allows the methods of the Caml object to be |
| 1069 |
invoked from any COM client. |
| 1070 |
</li></ul><p>Example: in the <span class=machine><span class=font-tt>IA</span></span> and <span class=machine><span class=font-tt>IB</span></span> example above, the following Caml |
| 1071 |
definitions are generated for <span class=machine><span class=font-tt>IA</span></span>: |
| 1072 |
</p><pre>type iA |
| 1073 |
val iid_iA : iA Com.iid |
| 1074 |
class iA_class : iA Com.interface -> object method f : int -> int end |
| 1075 |
val use_iA : iA Com.interface -> iA_class |
| 1076 |
val make_iA : #iA_class -> iA Com.interface |
| 1077 |
</pre><p>For <span class=machine><span class=font-tt>IB</span></span>, we get: |
| 1078 |
</p><pre>type iB |
| 1079 |
val iA_of_iB : iB Com.interface -> iA Com.interface |
| 1080 |
class iB_class : |
| 1081 |
iB Com.interface -> object inherit iA_class method g : string -> unit end |
| 1082 |
val use_iB : iB Com.interface -> iB_class |
| 1083 |
val make_iB : #iB_class -> iB Com.interface |
| 1084 |
</pre> |
| 1085 |
|
| 1086 |
<h4 id="sec32" class="paragraph">Error handling in interfaces:</h4><p> Conventionally, methods of |
| 1087 |
COM interfaces always return a result of type <span class=machine><span class=font-tt>HRESULT</span></span> that says |
| 1088 |
whether the method succeeded or failed, and in the latter case returns |
| 1089 |
an error code to its caller.</p><p>When calling an interface method from Caml, if the method returns an |
| 1090 |
<span class=machine><span class=font-tt>HRESULT</span></span> denoting failure, the exception <span class=machine><span class=font-tt>Com.Error</span></span> is raised with a |
| 1091 |
message describing the error. Successful <span class=machine><span class=font-tt>HRESULT</span></span> return values are |
| 1092 |
ignored. To make them available to Caml, <span class=machine><span class=font-tt>camlidl</span></span> defines the types |
| 1093 |
<span class=machine><span class=font-tt>HRESULT</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>bool</span></span> and <span class=machine><span class=font-tt>HRESULT</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>int</span></span>. If those types are used as return |
| 1094 |
types instead of <span class=machine><span class=font-tt>HRESULT</span></span>, failure results are mapped to |
| 1095 |
<span class=machine><span class=font-tt>Com.Error</span></span> exceptions as before, but successful results are mapped to |
| 1096 |
the Caml types <span class=machine><span class=font-tt>bool</span></span> and <span class=machine><span class=font-tt>int</span></span> respectively. (For <span class=machine><span class=font-tt>HRESULT</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>bool</span></span>, |
| 1097 |
the <span class=machine><span class=font-tt>S</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>OK</span></span> result is mapped to <span class=machine><span class=font-tt>true</span></span> and other successful results are |
| 1098 |
mapped to <span class=machine><span class=font-tt>false</span></span>. For <span class=machine><span class=font-tt>HRESULT</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>int</span></span>, the low 16 bits of the result |
| 1099 |
code are returned as a Caml <span class=machine><span class=font-tt>int</span></span>.)</p><p>When calling a Caml method from a COM client, any exception that |
| 1100 |
escapes the Caml method is mapped back to a failure <span class=machine><span class=font-tt>HRESULT</span></span>. A |
| 1101 |
textual description of the uncaught exception is saved using |
| 1102 |
<span class=machine><span class=font-tt>SetLastError</span></span>, and can be consulted by the COM client using |
| 1103 |
<span class=machine><span class=font-tt>GetLastError</span></span> (this is the standard convention for passing extended |
| 1104 |
error information in COM).</p><p>If the IDL return type of the method is not one of the <span class=machine><span class=font-tt>HRESULT</span></span> |
| 1105 |
types, any exception escaping the Caml method aborts the whole program |
| 1106 |
after printing a description of the exception. Hence, programmers of |
| 1107 |
Caml components should either use <span class=machine><span class=font-tt>HRESULT</span></span> as result type, or make |
| 1108 |
very sure that all exceptions are properly caught by the method.</p> |
| 1109 |
|
| 1110 |
<h2 id="sec33" class="section">4 Using <span class=machine><span class=font-tt>camlidl</span></span></h-> |
| 1111 |
|
| 1112 |
<h3 id="sec34" class="subsection">4.1 Overview</h-><p>The <span class=machine><span class=font-tt>camlidl</span></span> stub generator is invoked as follows: |
| 1113 |
</p><pre> |
| 1114 |
camlidl <span class=font-it>options</span> <span class=font-it>file1</span>.idl <span class=font-it>file2</span>.idl ... |
| 1115 |
</pre><p> |
| 1116 |
For each file <span class=font-it>f</span><span class=machine><span class=font-tt>.idl</span></span> given on the command line, <span class=machine><span class=font-tt>camlidl</span></span> |
| 1117 |
generates the following files: |
| 1118 |
</p><ul class="itemize"><li class="li-itemize"> |
| 1119 |
A Caml interface file <span class=font-it>f</span><span class=machine><span class=font-tt>.mli</span></span> that defines the Caml view |
| 1120 |
of the IDL file. It contains Caml definitions for the types declared |
| 1121 |
in the IDL file, as well as declarations for the functions and the |
| 1122 |
interfaces. |
| 1123 |
</li><li class="li-itemize">A Caml implementation file <span class=font-it>f</span><span class=machine><span class=font-tt>.ml</span></span> that implements the |
| 1124 |
<span class=font-it>f</span><span class=machine><span class=font-tt>.mli</span></span> file. |
| 1125 |
</li><li class="li-itemize">A C source file <span class=font-it>f</span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>stubs.c</span></span> that contains the stub functions for |
| 1126 |
converting between C and Caml data representations. |
| 1127 |
</li><li class="li-itemize">If the <span class=machine><span class=font-tt>-header</span></span> option is given, a C header file <span class=font-it>f</span><span class=machine><span class=font-tt>.h</span></span> |
| 1128 |
containing C declarations for the types declared in the IDL file. |
| 1129 |
</li></ul><p> |
| 1130 |
The generated <span class=machine><span class=font-tt>.ml</span></span> and <span class=machine><span class=font-tt>.c</span></span> files must be compiled and linked with |
| 1131 |
the remainder of the Caml program.</p> |
| 1132 |
|
| 1133 |
<h3 id="sec35" class="subsection">4.2 Options</h-><p>The following command-line options are recognized by <span class=machine><span class=font-tt>camlidl</span></span>.</p><dl class="description"><dt class="dt-description"><span class=font-bold><span class=machine><span class=font-tt>-cpp</span></span></span></dt><dd class="dd-description"> |
| 1134 |
Pre-process the source IDL files with the C preprocessor. This option |
| 1135 |
is set by default.</dd><dt class="dt-description"><span class=font-bold><span class=machine><span class=font-tt>-D </span></span></span><span class=font-bold> </span><span class=font-bold><span class=font-it>symbol</span></span><span class=font-bold><span class=machine><span class=font-tt>=</span></span></span><span class=font-bold><span class=font-it>value</span></span></dt><dd class="dd-description"> |
| 1136 |
Define a preprocessor symbol. The option <span class=machine><span class=font-tt>-D</span></span><span class=font-it>symbol</span><span class=machine><span class=font-tt>=</span></span><span class=font-it>value</span> |
| 1137 |
is passed to the C preprocessor. The <span class=font-it>value</span> can be omitted, |
| 1138 |
as in <span class=machine><span class=font-tt>-D</span></span> <span class=font-it>symbol</span>, and defaults to <span class=machine><span class=font-tt>1</span></span>.</dd><dt class="dt-description"><span class=font-bold><span class=machine><span class=font-tt>-header</span></span></span></dt><dd class="dd-description"> |
| 1139 |
Generate a C header file <span class=font-it>f</span><span class=machine><span class=font-tt>.h</span></span> containing C declarations for the |
| 1140 |
types and functions declared in the IDL file <span class=font-it>f</span><span class=machine><span class=font-tt>.c</span></span>.</dd><dt class="dt-description"><span class=font-bold><span class=machine><span class=font-tt>-I </span></span></span><span class=font-bold> </span><span class=font-bold><span class=font-it>dir</span></span></dt><dd class="dd-description"> |
| 1141 |
Add the directory <span class=font-it>dir</span> to the list of directories searched for |
| 1142 |
<span class=machine><span class=font-tt>.idl</span></span> files, as given on the command line or recursively loaded |
| 1143 |
by <span class=syntax-token>import</span> statements.</dd><dt class="dt-description"><span class=font-bold><span class=machine><span class=font-tt>-keep-labels</span></span></span></dt><dd class="dd-description"> |
| 1144 |
Keep the Caml names of record labels as specified in the IDL file. |
| 1145 |
Do not prefix them with the name of the enclosing struct, even if they |
| 1146 |
appear in several struct definitions.</dd><dt class="dt-description"><span class=font-bold><span class=machine><span class=font-tt>-nocpp</span></span></span></dt><dd class="dd-description"> |
| 1147 |
Suppresses the pre-processing of source IDL files.</dd><dt class="dt-description"><span class=font-bold><span class=machine><span class=font-tt>-no-include</span></span></span></dt><dd class="dd-description"> |
| 1148 |
By default, <span class=machine><span class=font-tt>camlidl</span></span> emits a <span class=machine><span class=font-tt>#include "</span></span><span class=font-it>f</span><span class=machine><span class=font-tt>.h"</span></span> statement in |
| 1149 |
the file <span class=font-it>f</span><span class=machine><span class=font-tt>.c</span></span> containing the generated C code. |
| 1150 |
The <span class=font-it>f</span><span class=machine><span class=font-tt>.h</span></span> header file being included is |
| 1151 |
either the one generated by <span class=machine><span class=font-tt>camlidl -header</span></span>, or generated by another |
| 1152 |
tool (such as Microsoft’s <span class=machine><span class=font-tt>midl</span></span> compiler) from the IDL file, or |
| 1153 |
hand-written. The <span class=font-it>f</span><span class=machine><span class=font-tt>.h</span></span> file is assumed to provide all C type |
| 1154 |
declarations needed for compiling the stub code.<p>The <span class=machine><span class=font-tt>-no-include</span></span> option suppresses the automatic inclusion of the |
| 1155 |
<span class=font-it>f</span><span class=machine><span class=font-tt>.h</span></span> file. The IDL file should then include the right header |
| 1156 |
files and provide the right type declarations via <span class=syntax-token>quote</span> statements.</p></dd><dt class="dt-description"><span class=font-bold><span class=machine><span class=font-tt>-prefix-all-labels</span></span></span></dt><dd class="dd-description"> |
| 1157 |
Prefix all Caml names of record labels with the name of the enclosing |
| 1158 |
struct. The default is to prefix only those labels that could cause |
| 1159 |
ambiguity because they appear in several struct definitions.</dd><dt class="dt-description"><span class=font-bold><span class=machine><span class=font-tt>-prepro</span></span></span><span class=font-bold> </span><span class=font-bold><span class=font-it>preprocessing-command</span></span></dt><dd class="dd-description"> |
| 1160 |
Set the command that is executed to pre-process the source IDL files. |
| 1161 |
The default is the C preprocessor.</dd></dl> |
| 1162 |
|
| 1163 |
<h3 id="sec36" class="subsection">4.3 The <span class=machine><span class=font-tt>camlidldll</span></span> script</h-><p>Under Windows, a <span class=machine><span class=font-tt>bash</span></span> script called <span class=machine><span class=font-tt>camlidldll</span></span> is provided to |
| 1164 |
automate the construction of a DLL containing a COM component written |
| 1165 |
in Caml.</p><p>The script <span class=machine><span class=font-tt>camlidldll</span></span> accepts essentially the same command-line |
| 1166 |
arguments and options as the <span class=machine><span class=font-tt>ocamlc</span></span> compiler. (It also accepts |
| 1167 |
<span class=machine><span class=font-tt>.tlb</span></span> type library files on the command-line; see |
| 1168 |
section <a href="#s-dispatch">5.3</a>, “Dispatch interfaces”, for more |
| 1169 |
information on type libraries.) |
| 1170 |
It produces a DLL file that encapsulates the Caml and C object files |
| 1171 |
given on the command line.</p><p>Use <span class=machine><span class=font-tt>regsvr32 /s </span></span><span class=font-it>file</span><span class=machine><span class=font-tt>.dll</span></span> to record the components in the |
| 1172 |
system registry once it is compiled to a DLL.</p> |
| 1173 |
|
| 1174 |
<h2 id="sec37" class="section">5 Hints on writing IDL files</h-> |
| 1175 |
|
| 1176 |
<h3 id="sec38" class="subsection">5.1 Writing an IDL file for a C library</h-><p>When writing an IDL file for a C library that doesn’t have an IDL interface |
| 1177 |
already, the include files for that library are a good starting point: |
| 1178 |
just copy the relevant type and functin declarations to the IDL file, |
| 1179 |
then annotate them with IDL attributes to describe more precisely |
| 1180 |
their actual behavior. The documentation of the library must be read |
| 1181 |
carefully to determine the mode of function parameters (<span class=machine><span class=font-tt>in</span></span>, <span class=machine><span class=font-tt>out</span></span>, |
| 1182 |
<span class=machine><span class=font-tt>inout</span></span>), the actual sizes of arrays, etc.</p><p>The type definitions in the IDL file need not correspond exactly with |
| 1183 |
those in the include files. Often, a cleaner Caml interface can be |
| 1184 |
obtained by omitting irrelevant struct fields, or changing their types. |
| 1185 |
For instance, the Unix library functions for reading library entries |
| 1186 |
may use the following structure: |
| 1187 |
</p><pre> struct dirent { |
| 1188 |
long int d_ino; |
| 1189 |
__off_t d_off; |
| 1190 |
unsigned short int d_reclen; |
| 1191 |
unsigned char d_type; |
| 1192 |
char d_name[256]; |
| 1193 |
}; |
| 1194 |
</pre><p>Of those fields, only <span class=machine><span class=font-tt>d</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>name</span></span> and <span class=machine><span class=font-tt>d</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>ino</span></span> are of interest to the |
| 1195 |
user; the other fields are internal information for the library |
| 1196 |
functions, are not specified in the POSIX specs, and therefore must |
| 1197 |
not be used. Thus, in the IDL file, you should declare: |
| 1198 |
</p><pre> struct dirent { |
| 1199 |
long int d_ino; |
| 1200 |
char d_name[256]; |
| 1201 |
}; |
| 1202 |
</pre><p>Thus, the Caml code will have |
| 1203 |
<span class=machine><span class=font-tt>type struct</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>dirent = </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt>d</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>ino: int; d</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>name: string</span></span><span class=machine><span class=font-tt>}</span></span> |
| 1204 |
as desired. However, the generated stub code, being |
| 1205 |
compiled against the “true” definition of <span class=machine><span class=font-tt>struct dirent</span></span>, will find |
| 1206 |
those two fields at the correct offsets in the actual struct.</p><p>Special attention must be paid to integer fields or variables. |
| 1207 |
By default, integer IDL types are mapped to the Caml type <span class=machine><span class=font-tt>int</span></span>, |
| 1208 |
which is convenient to use in Caml code, but loses one bit |
| 1209 |
when converting from a C <span class=machine><span class=font-tt>long</span></span> integer, and may lose one bit (on |
| 1210 |
32-bit platforms) when converting from a C <span class=machine><span class=font-tt>int</span></span> integer. When the |
| 1211 |
range of values represented by the C integer is small enough, this |
| 1212 |
loss is acceptable. Otherwise, you should use the attributes |
| 1213 |
<span class=machine><span class=font-tt>nativeint</span></span>, <span class=machine><span class=font-tt>int32</span></span> or <span class=machine><span class=font-tt>int64</span></span> so that integer IDL types are mapped |
| 1214 |
to one of the Caml boxed integer types. (We recommend that you use |
| 1215 |
<span class=machine><span class=font-tt>int32</span></span> or <span class=machine><span class=font-tt>int64</span></span> for integers that are specified as being exactly 32 |
| 1216 |
bit wide or 64 bit wide, and <span class=machine><span class=font-tt>nativeint</span></span> for unspecified <span class=machine><span class=font-tt>int</span></span> or |
| 1217 |
<span class=machine><span class=font-tt>long</span></span> integers.)</p><p>Yet another possibility is to declare certain integer fields or variables |
| 1218 |
as <span class=machine><span class=font-tt>double</span></span> in the IDL file, so that they are represented by <span class=machine><span class=font-tt>float</span></span> |
| 1219 |
in Caml, and all 32 bits of the integer are preserved in Caml. For |
| 1220 |
instance, the Unix function to get the current type is declared as |
| 1221 |
</p><pre> time_t time(time_t * t); |
| 1222 |
</pre><p>where <span class=machine><span class=font-tt>time</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>t</span></span> is usually defined as <span class=machine><span class=font-tt>long</span></span>. We can nonetheless |
| 1223 |
pretend (in the IDL file) that <span class=machine><span class=font-tt>time</span></span> returns a double: |
| 1224 |
</p><pre> double time() quote(" _res = time(NULL); "); |
| 1225 |
</pre><p>This way, <span class=machine><span class=font-tt>time</span></span> will have the Caml type <span class=machine><span class=font-tt>unit -> float</span></span>. |
| 1226 |
Again, the stub code “knows” that <span class=machine><span class=font-tt>time</span></span> actually returns an integer, |
| 1227 |
and therefore will insert the right integer-float coercions.</p> |
| 1228 |
|
| 1229 |
<h3 id="sec39" class="subsection">5.2 Sharing IDL files between MIDL and CamlIDL</h-><p>The Microsoft software development kit provides a number of IDL files |
| 1230 |
describing various libraries and components. In its current state, |
| 1231 |
<span class=machine><span class=font-tt>camlidl</span></span> cannot exploit those files directly: they use many |
| 1232 |
(often poorly documented) Microsoft IDL features that are not |
| 1233 |
implemented yet in <span class=machine><span class=font-tt>camlidl</span></span>; symmetrically, <span class=machine><span class=font-tt>camlidl</span></span> introduces |
| 1234 |
several new annotations that are not recognized by Microsoft’s <span class=machine><span class=font-tt>midl</span></span> |
| 1235 |
compiler. So, significant editing work on the IDL files is required.</p><p>The C preprocessor can be used to alleviate the <span class=machine><span class=font-tt>camlidl</span></span>-<span class=machine><span class=font-tt>midl</span></span> |
| 1236 |
incompatibilities: <span class=machine><span class=font-tt>camlidl</span></span> defines the preprocessor symbol <span class=machine><span class=font-tt>CAMLIDL</span></span> |
| 1237 |
when preprocessing its input files, while <span class=machine><span class=font-tt>midl</span></span> does not. Hence, |
| 1238 |
one can bracket incompatible definitions in |
| 1239 |
<span class=machine><span class=font-tt>#ifdef CAMLIDL ... #else ... #endif</span></span>. Along these lines, a C |
| 1240 |
preprocessor header file, <span class=machine><span class=font-tt>camlidlcompat.h</span></span>, is provided: it uses |
| 1241 |
<span class=machine><span class=font-tt>#define</span></span> to remove <span class=machine><span class=font-tt>camlidl</span></span>-specific attributes when compiling with |
| 1242 |
<span class=machine><span class=font-tt>midl</span></span>, and to remove <span class=machine><span class=font-tt>midl</span></span>-specific attributes when compiling with |
| 1243 |
<span class=machine><span class=font-tt>camlidl</span></span>. Thus, an IDL file compatible with both <span class=machine><span class=font-tt>midl</span></span> and |
| 1244 |
<span class=machine><span class=font-tt>camlidl</span></span> would look like this: |
| 1245 |
</p><pre> #include <camlidlcompat.h> |
| 1246 |
|
| 1247 |
#ifndef CAMLIDL |
| 1248 |
import "unknwn.idl"; // imports specific to MIDL |
| 1249 |
import "oaidl.idl"; |
| 1250 |
#endif |
| 1251 |
import "mymodule.idl"; // imports common to MIDL and CamlIDL |
| 1252 |
|
| 1253 |
typedef [abstract,marshal_as(int)] void * ptr; |
| 1254 |
|
| 1255 |
... |
| 1256 |
|
| 1257 |
#ifndef CAMLIDL |
| 1258 |
[...] library MyTypeLib { |
| 1259 |
importlib("stdole32.tlb"); |
| 1260 |
[...] coclass MyComponent { [default] interface IX; } |
| 1261 |
} |
| 1262 |
#endif |
| 1263 |
</pre><p>Notice that since <span class=machine><span class=font-tt>camlidl</span></span> doesn’t handle type libraries, the type |
| 1264 |
library part of an <span class=machine><span class=font-tt>midl</span></span> file must be enclosed in <span class=machine><span class=font-tt>#ifndef CAMLIDL</span></span>.</p> |
| 1265 |
|
| 1266 |
<h3 id="sec40" class="subsection">5.3 Dispatch interfaces and type libraries</h-><p> <a id="s-dispatch"></a></p><p>A dispatch interface, in COM lingo, is an interface that supports |
| 1267 |
dynamic, interpreted dispatch of method interfaces. This form of |
| 1268 |
interpreted dispatch is used by Visual Basic and other scripting |
| 1269 |
languages to perform calls to methods of COM components.</p><p>CamlIDL provides minimal support for dispatch interfaces. To equip a |
| 1270 |
Caml component with a dispatch interface (thus making it callable from |
| 1271 |
Visual Basic), you need to do the following: |
| 1272 |
</p><ol class="enumerate" type=1><li class="li-enumerate"> |
| 1273 |
Use <span class=machine><span class=font-tt>IDispatch</span></span> instead of <span class=machine><span class=font-tt>IUnknown</span></span> as the super-interface of |
| 1274 |
the component’s interfaces. |
| 1275 |
</li><li class="li-enumerate">Write a type library for your component and compile it using |
| 1276 |
<span class=machine><span class=font-tt>midl</span></span>. A type library is a run-time representation of the interfaces |
| 1277 |
supported by an object. The <span class=machine><span class=font-tt>midl</span></span> compiler can generate a type |
| 1278 |
library from the IDL description of the component, enriched with some |
| 1279 |
special-purpose declarations (the <span class=machine><span class=font-tt>library</span></span> and <span class=machine><span class=font-tt>coclass</span></span> |
| 1280 |
statements). Refer to the documentation of <span class=machine><span class=font-tt>midl</span></span> for more |
| 1281 |
information. |
| 1282 |
</li><li class="li-enumerate">Pass the type library files (<span class=machine><span class=font-tt>.tlb</span></span> files) generated by <span class=machine><span class=font-tt>midl</span></span> |
| 1283 |
as extra arguments to <span class=machine><span class=font-tt>camlidldll</span></span> when generating the DLL for your |
| 1284 |
Caml component. |
| 1285 |
</li></ol> |
| 1286 |
|
| 1287 |
<h2 id="sec41" class="section">6 Release notes</h-><p>Here are some caveats and open issues that apply to the current |
| 1288 |
release.</p> |
| 1289 |
|
| 1290 |
<h4 id="sec42" class="paragraph">Deallocation of function results and <span class=machine><span class=font-tt>out</span></span> parameters:</h4><p> |
| 1291 |
If a C function dynamically allocates some of its outputs (either |
| 1292 |
returned or stored in <span class=machine><span class=font-tt>out</span></span> parameters), its IDL declaration must |
| 1293 |
contain a <span class=syntax-token>quote(dealloc,</span> <a href="#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>)</span> clause to properly free the |
| 1294 |
space occupied by those outputs after they have been converted to |
| 1295 |
Caml. Otherwise, memory leaks will occur. (The only exception is |
| 1296 |
results and output parameters of type <span class=machine><span class=font-tt>[bigarray,managed] </span></span><span class=font-it>ty</span><span class=machine><span class=font-tt>[]</span></span>, |
| 1297 |
where the Caml garbage collector takes care of deallocation.)</p><p>This does not conform to the MIDL and COM specifications, which say |
| 1298 |
that space for <span class=machine><span class=font-tt>out</span></span> data structures must be allocated |
| 1299 |
with <span class=machine><span class=font-tt>CoTaskMemAlloc</span></span> by the callee, and automatically freed |
| 1300 |
using <span class=machine><span class=font-tt>CoTaskMemFree</span></span> by the generated stub code. (The specs don’t |
| 1301 |
say what happens with the return value of the function.) |
| 1302 |
However, there are many functions in Win32 (not to mention the |
| 1303 |
Unix world) that do not follow this convention, and |
| 1304 |
return data structures (e.g. strings) that are statically |
| 1305 |
allocated, or require special deallocation functions. Hence, |
| 1306 |
<span class=machine><span class=font-tt>camlidl</span></span> leaves deallocation of outputs entirely under user control.</p> |
| 1307 |
|
| 1308 |
<h4 id="sec43" class="paragraph">Allocation and deallocation of <span class=machine><span class=font-tt>in,out</span></span> parameters:</h4><p> |
| 1309 |
For <span class=machine><span class=font-tt>in,out</span></span> parameters, the MIDL/COM rules are that the caller (the |
| 1310 |
stub code) should allocate the inputs, the callee should free them |
| 1311 |
and allocate again its outputs, and the caller should free the outputs. |
| 1312 |
As explained above, <span class=machine><span class=font-tt>camlidl</span></span>-generated stubs don’t automatically free |
| 1313 |
the outputs. Worse, the inputs passed to the functions are allocated |
| 1314 |
partially on the stack and partially in the heap |
| 1315 |
(using <span class=machine><span class=font-tt>CoTaskMemAlloc</span></span>), so the callee may perform an incorrect |
| 1316 |
free on a stack-allocated argument. The best thing to do is avoid |
| 1317 |
<span class=machine><span class=font-tt>in,out</span></span> parameters entirely, and split them into one <span class=machine><span class=font-tt>in</span></span> and one |
| 1318 |
<span class=machine><span class=font-tt>out</span></span> parameter.</p> |
| 1319 |
|
| 1320 |
<h4 id="sec44" class="paragraph">Reference-counting of COM interfaces:</h4><p> |
| 1321 |
Caml finalized objects are used to call <span class=machine><span class=font-tt>Release</span></span> automatically on COM |
| 1322 |
interfaces that become unreachable. The reference counting of |
| 1323 |
interfaces passed as <span class=machine><span class=font-tt>in</span></span> and <span class=machine><span class=font-tt>out</span></span> parameters is correctly |
| 1324 |
implemented. However, <span class=machine><span class=font-tt>in,out</span></span> parameters that are interfaces are not |
| 1325 |
correctly handled. Again, avoid <span class=machine><span class=font-tt>in,out</span></span> parameters.</p> |
| 1326 |
|
| 1327 |
<h4 id="sec45" class="paragraph">COM support:</h4><p> |
| 1328 |
The support for COM is currently quite small. COM components |
| 1329 |
registered in the system registry can be imported via |
| 1330 |
<span class=machine><span class=font-tt>Com.create</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>instance</span></span>. Components written in Caml can be exported as |
| 1331 |
DLLs, but not yet as standalone servers. Preliminary support for |
| 1332 |
dispatch interfaces is available, however many of the data types used |
| 1333 |
in the Automation framework are not supported yet (e.g. <span class=machine><span class=font-tt>SAFEARRAY</span></span>). |
| 1334 |
</p> |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
<hr class="horizontal-rule" style="height:2px"><blockquote class="quote"><em>This document was translated from L<sup>A</sup>T<sub>E</sub>X by |
| 1339 |
</em><a href="http://hevea.inria.fr/index.html"><em>H</em><em><span style="font-size:small"><sup>E</sup></span></em><em>V</em><em><span style="font-size:small"><sup>E</sup></span></em><em>A</em></a><em>.</em></blockquote></body> |
| 1340 |
</html> |