| 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 | <link rel="stylesheet" type="text/css" href="main.css"> |
| 7 | <title>The Caml-IDL mapping</title> |
| 8 | </head> |
| 9 | <body > |
| 10 | <a href="main002.html"><img src="previous_motif.svg" alt="Previous"></a> |
| 11 | <a href="index.html"><img src="contents_motif.svg" alt="Up"></a> |
| 12 | <a href="main004.html"><img src="next_motif.svg" alt="Next"></a> |
| 13 | <hr> |
| 14 | <h2 id="sec18" class="section">3 The Caml-IDL mapping</h2> |
| 15 | <p>This section describes how IDL types, function declarations, and |
| 16 | interfaces are mapped to Caml types, functions and classes.</p> |
| 17 | <h3 id="sec19" class="subsection">3.1 Base types</h3> |
| 18 | <div class="tableau"> |
| 19 | <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> |
| 20 | <tr><td style="text-align:left;border:solid 1px;white-space:nowrap" > |
| 21 | <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> |
| 22 | <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> |
| 23 | <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> |
| 24 | <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> |
| 25 | <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> |
| 26 | <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> |
| 27 | <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> |
| 28 | <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> |
| 29 | <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> |
| 30 | </table></div></div><p> |
| 31 | (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 |
| 32 | 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 |
| 33 | 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>, |
| 34 | <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 |
| 35 | and values of type <span class=machine><span class=font-tt>int64</span></span> are exactly 64-bit wide on all platforms. |
| 36 | Values of type <span class=machine><span class=font-tt>nativeint</span></span> have the natural word size of the platform, |
| 37 | 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 |
| 38 | loss of precision. Values of Caml type <span class=machine><span class=font-tt>int</span></span> have the natural word |
| 39 | size of the platform minus one bit of tag, hence the conversion from IDL |
| 40 | 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 |
| 41 | platforms. On 64-bit platforms, the conversion from <span class=machine><span class=font-tt>int</span></span> is exact, |
| 42 | 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, |
| 43 | 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 |
| 44 | interface, if any, determines the kind of the integer. |
| 45 | 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 |
| 46 | <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 |
| 47 | Caml <span class=machine><span class=font-tt>int</span></span> type.</p> |
| 48 | <h3 id="sec20" class="subsection">3.2 Pointers</h3> |
| 49 | <p>The mapping of IDL pointer types depends on their kinds. Writing |
| 50 | [[<span class=font-it>ty</span>]] for the Caml type corresponding to the IDL type |
| 51 | <span class=font-it>ty</span>, we have: |
| 52 | </p><pre> |
| 53 | [ref] <span class=font-it>ty</span> * ⇒ [[<span class=font-it>ty</span>]] |
| 54 | [unique] <span class=font-it>ty</span> * ⇒ [[<span class=font-it>ty</span>]] option |
| 55 | [ptr] <span class=font-it>ty</span> * ⇒ [[<span class=font-it>ty</span>]] Com.opaque |
| 56 | </pre><p> |
| 57 | In other terms, IDL pointers of kind <span class=machine><span class=font-tt>ref</span></span> are ignored during the mapping: |
| 58 | <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>. |
| 59 | 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 |
| 60 | 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 |
| 61 | 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> |
| 62 | for a non-null pointer to a C value <span class=font-it>c</span> that translates to the ML |
| 63 | 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. |
| 64 | This is an abstract type that encapsulates the C pointer without |
| 65 | 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 |
| 66 | parameters that need not be exposed in the Caml code. Those pointers |
| 67 | are simply set to null when converting from Caml to C, and ignored |
| 68 | 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 |
| 69 | of the enclosing interface, if any, determines the kind of the pointer. |
| 70 | 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 |
| 71 | assumed.</p> |
| 72 | <h3 id="sec21" class="subsection">3.3 Arrays</h3> |
| 73 | <p>IDL arrays of characters that carry the <span class=machine><span class=font-tt>[string]</span></span> attribute are mapped |
| 74 | to the Caml <span class=machine><span class=font-tt>string</span></span> type: |
| 75 | </p><div class="tableau"> |
| 76 | <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> |
| 77 | <tr><td style="text-align:left;border:solid 1px;white-space:nowrap" > |
| 78 | <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> |
| 79 | <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> |
| 80 | <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> |
| 81 | <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> |
| 82 | </table></div></div><p> |
| 83 | Caml string values are translated to standard null-terminated C strings. |
| 84 | Be careful about embedded null characters in the Caml string, which |
| 85 | 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 |
| 86 | 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 |
| 87 | “big arrays”, as described in the next section.</p><p>All other IDL arrays are translated to ML arrays: |
| 88 | </p><pre> |
| 89 | <span class=font-it>ty</span> [] ⇒ [[<span class=font-it>ty</span>]] array |
| 90 | </pre><p> |
| 91 | For instance, <span class=machine><span class=font-tt>double []</span></span> becomes <span class=machine><span class=font-tt>float array</span></span>. |
| 92 | Consequently, multi-dimensional arrays are translated to Caml arrays |
| 93 | 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 |
| 94 | ML option type: |
| 95 | </p><pre> |
| 96 | [string,unique] char [] ⇒ string option |
| 97 | [unique] <span class=font-it>ty</span> [] ⇒ [[<span class=font-it>ty</span>]] array option |
| 98 | </pre><p> |
| 99 | As in the case of pointers of kind <span class=machine><span class=font-tt>unique</span></span>, the option value is |
| 100 | <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 |
| 101 | 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 |
| 102 | element. For the conversion from C to ML, the number of elements of |
| 103 | the ML array is determined as follows (in the order presented): |
| 104 | </p><ul class="itemize"><li class="li-itemize"> |
| 105 | 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. |
| 106 | </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. |
| 107 | </li><li class="li-itemize">By the bound written in the array type, if any. |
| 108 | </li><li class="li-itemize">By searching the first null element of the C array, if the |
| 109 | <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. |
| 110 | </li></ul><p> |
| 111 | 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 |
| 112 | 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 |
| 113 | <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 |
| 114 | several limited expressions. Each expression applies to one dimension |
| 115 | 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> |
| 116 | specifies a matrix of <span class=machine><span class=font-tt>double</span></span> whose first dimension has size |
| 117 | <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> |
| 118 | <h3 id="sec22" class="subsection">3.4 Big arrays</h3> |
| 119 | <p>IDL arrays of integers or floats that carry the <span class=machine><span class=font-tt>[bigarray]</span></span> attribute |
| 120 | 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 |
| 121 | one-dimensional arrays, <span class=machine><span class=font-tt>Array2.t</span></span> for 2-dimensional arrays, |
| 122 | <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 |
| 123 | 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 |
| 124 | from Caml using the Fortran conventions (array indices start at 1; |
| 125 | column-major memory layout). By default, the big array is accessed |
| 126 | from Caml using the C conventions (array indices start at 0; row-major |
| 127 | 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 |
| 128 | result type or out parameter type of a function, Caml assumes that the |
| 129 | corresponding C array was allocated using <span class=machine><span class=font-tt>malloc()</span></span>, and is not |
| 130 | referenced anywhere else; then, the Caml garbage collector will free |
| 131 | the C array when the corresponding Caml big array becomes unreachable. |
| 132 | By default, Caml assumes that result or out C arrays are statically or |
| 133 | permanently allocated, and keeps a pointer to them during conversion |
| 134 | to Caml big arrays, and does not free them when the Caml bigarrays |
| 135 | become unreachable.</p><p>Finally, the <span class=machine><span class=font-tt>[unique]</span></span> attribute applies to bigarrays as to arrays, |
| 136 | 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 |
| 137 | <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 |
| 138 | resulting from the translation of <span class=font-it>p</span>.</p> |
| 139 | <h3 id="sec23" class="subsection">3.5 Structs</h3> |
| 140 | <p>IDL structs are mapped to Caml record types. The names and types of |
| 141 | the IDL struct fields determine the names and types of the Caml record |
| 142 | type: |
| 143 | </p><pre> |
| 144 | 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>]] ; ... } |
| 145 | </pre><p> |
| 146 | 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 |
| 147 | <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: |
| 148 | </p><ul class="itemize"><li class="li-itemize"> |
| 149 | Fields of the IDL struct that are pointers with the <span class=machine><span class=font-tt>[ignore]</span></span> |
| 150 | attribute do not appear in the Caml record type. |
| 151 | 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> |
| 152 | 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>. |
| 153 | Those ignored pointer fields are set to <span class=machine><span class=font-tt>NULL</span></span> when converting from a |
| 154 | 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>, |
| 155 | <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 |
| 156 | in the Caml record type. (We call those fields <em>dependent</em> fields.) |
| 157 | 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> |
| 158 | is translated to the Caml record type |
| 159 | <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>. |
| 160 | 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>, |
| 161 | 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 |
| 162 | fields as described above, the IDL struct has only one field |
| 163 | <span class=font-it>ty</span> <span class=font-it>id</span>, we avoid creating a one-field Caml record type |
| 164 | and translate the IDL struct type directly to the Caml type |
| 165 | [[<span class=font-it>ty</span>]]. |
| 166 | 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> |
| 167 | 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 |
| 168 | using the <span class=syntax-token>mlname</span> attribute on struct field declarations. For instance, |
| 169 | <pre> |
| 170 | struct s { int n; [mlname(p)] int q; } |
| 171 | becomes type s = { n : int; p : int } |
| 172 | </pre></li><li class="li-itemize">The Caml type system makes it difficult to use two record types |
| 173 | defined in the same module and having some label names in common. |
| 174 | Thus, if CamlIDL encounters two or more structs having |
| 175 | identically-named fields, it prefixes the Caml label names by the names |
| 176 | of the structs in order to distinguish them. For instance: |
| 177 | <pre> |
| 178 | struct s1 { int x; int y; } |
| 179 | struct s2 { double x; double t; } |
| 180 | struct s3 { int z; } |
| 181 | becomes type s1 = { s1_x: int; s1_y: int } |
| 182 | and s2 = { s2_x: float; s2_t: float } |
| 183 | and s3 = { z: int } |
| 184 | </pre> |
| 185 | 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 |
| 186 | <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 |
| 187 | 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 |
| 188 | struct name, if any, and otherwise from the name of the nearest |
| 189 | enclosing struct, union or typedef. For instance: |
| 190 | </p><pre> |
| 191 | typedef struct { int x; } t; |
| 192 | struct s4 { struct { int x; } z; }; |
| 193 | becomes type t = { t_x: int } |
| 194 | and s4 = { z: struct_1 } |
| 195 | and struct_1 = { s4_x: int } |
| 196 | </pre><p>The “minimal prefixing” strategy described above is the default |
| 197 | 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, |
| 198 | all record labels are prefixed, whether they occur several times or |
| 199 | not. If the <span class=machine><span class=font-tt>-keep-labels</span></span> option is given, no automatic prefixing |
| 200 | takes place; the naming of record labels is left entirely under the |
| 201 | user’s control, via <span class=syntax-token>mlname</span> annotations.</p></li></ul> |
| 202 | <h3 id="sec24" class="subsection">3.6 Unions</h3> |
| 203 | <p>IDL discriminated unions are translated to Caml sum types. Each case |
| 204 | of the union corresponds to a constructor of the sum type. The |
| 205 | constructor is constant if the union case has no associated field, |
| 206 | otherwise has one argument corresponding to the union case field. If |
| 207 | the union has a <span class=machine><span class=font-tt>default</span></span> case, an extra constructor |
| 208 | <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 |
| 209 | <span class=machine><span class=font-tt>int</span></span> argument (the value of the discriminating field), |
| 210 | and possibly another argument corresponding to the default field. |
| 211 | Examples: |
| 212 | </p><pre> |
| 213 | union u1 { case A: int x; case B: case C: double d; case D: ; } |
| 214 | becomes type u1 = A of int | B of float | C of float | D |
| 215 | union u2 { case A: int x; case B: double d; default: ; } |
| 216 | becomes type u2 = A of int | B of float | Default_u of int |
| 217 | union u3 { case A: int x; default: double d; } |
| 218 | becomes type u3 = A of int | Default_v of int * double |
| 219 | </pre><p> |
| 220 | All IDL unions must be discriminated, either via the special syntax |
| 221 | <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 |
| 222 | 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 |
| 223 | built from other parameters of the current function, or other fields |
| 224 | of the current <span class=machine><span class=font-tt>struct</span></span>. Both the discriminant and the |
| 225 | case labels must be of an integer type. Unless a <span class=machine><span class=font-tt>default</span></span> case is |
| 226 | given, the value of the discriminant must be one of the cases of the |
| 227 | union.</p> |
| 228 | <h3 id="sec25" class="subsection">3.7 Enums</h3> |
| 229 | <p>IDL enums are translated to Caml enumerated types (sum types with only |
| 230 | constant constructors). The names of the constructors are determined |
| 231 | by the names of the enum labels. The values attached to the enum |
| 232 | labels are ignored. |
| 233 | Example: |
| 234 | <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 |
| 235 | bitfield obtained by logical “or” of zero, one or several labels of |
| 236 | the enum. The corresponding ML value is a list of zero, one or |
| 237 | several constructors of the Caml enumerated type. Consider for |
| 238 | instance: |
| 239 | </p><pre>enum e { A = 1, B = 2, C = 4 }; |
| 240 | typedef [set] enum e eset; |
| 241 | </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>. |
| 242 | 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>. |
| 243 | 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> |
| 244 | <h3 id="sec26" class="subsection">3.8 Type definitions</h3> |
| 245 | <p>An IDL <span class=machine><span class=font-tt>typedef</span></span> statement is normally translated |
| 246 | to a Caml type abbreviation. For instance, |
| 247 | <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 |
| 248 | generated instead of a type abbreviation, thus hinding from Caml the |
| 249 | representation of the type in question. For instance, |
| 250 | <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>. |
| 251 | 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, |
| 252 | the Caml type is made equal to <span class=nonterminal>caml-type-expr</span>. This is often used |
| 253 | in conjunction with the <span class=syntax-token>ml2c</span> and <span class=syntax-token>c2ml</span> attributes to implement |
| 254 | custom translation of data structures between C and ML. For instance, |
| 255 | <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> |
| 256 | 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 |
| 257 | given, the user-provided C functions given as attributes will be |
| 258 | called to perform Caml to C and C to Caml conversions for values of |
| 259 | the typedef-ed type, instead of using the <span class=machine><span class=font-tt>camlidl</span></span>-generated |
| 260 | conversion functions. This allows user-controlled translation of data |
| 261 | structures. The prototypes of the conversion functions must be |
| 262 | </p><pre> |
| 263 | value c2ml(<span class=font-it>ty</span> * input); |
| 264 | void ml2c(value input, <span class=font-it>ty</span> * output); |
| 265 | </pre><p> |
| 266 | 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 |
| 267 | 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 |
| 268 | returns the corresponding Caml value, while the <span class=machine><span class=font-tt>ml2c</span></span> function is |
| 269 | passed a Caml value as first argument and stores the corresponding C |
| 270 | 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 |
| 271 | <span class=syntax-token>abstract</span> attribute, the function <span class=nonterminal>final-fn</span> is called when |
| 272 | the Caml block representing a value of this typedef becomes |
| 273 | unreachable from Caml and is reclaimed by the Caml garbage collector. |
| 274 | 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 |
| 275 | comparison function and a hashing function (respectively) to Caml |
| 276 | values for this typedef. The comparison function is called when two |
| 277 | Caml values of this typedef are compared using the generic comparisons |
| 278 | <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 |
| 279 | <span class=machine><span class=font-tt>Hashtbl.hash</span></span> is applied to a Caml value of this typedef. |
| 280 | The prototype of the finalization, comparison and hashing functions are: |
| 281 | </p><pre> |
| 282 | value <span class=font-it>final-fn</span>(<span class=font-it>ty</span> * x); |
| 283 | int <span class=font-it>compare-fn</span>(<span class=font-it>ty</span> * x, <span class=font-it>ty</span> * y); |
| 284 | long <span class=font-it>hash-fn</span>(<span class=font-it>ty</span> * x); |
| 285 | </pre><p> |
| 286 | That is, their arguments are passed by reference. The comparison |
| 287 | function must return an integer that is negative, zero, or positive |
| 288 | depending on whether its first argument is smaller, equal or greater |
| 289 | than its second argument. The hashing function returns a suitable |
| 290 | 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>, |
| 291 | the error checking function <span class=nonterminal>fn</span> is called each time a function result |
| 292 | of type <span class=font-it>ty</span> is converted from C to Caml. The function can then check |
| 293 | the <span class=font-it>ty</span> value for values indicating an error condition, and raise the |
| 294 | appropriate exception. If in addition the <span class=syntax-token>errorcode</span> attribute is |
| 295 | provided, the conversion from C to Caml is suppressed: values of type |
| 296 | <span class=font-it>ty</span> are only passed to <span class=nonterminal>fn</span> for error checking, then discarded.</p> |
| 297 | <h3 id="sec27" class="subsection">3.9 Functions</h3> |
| 298 | <p>IDL function declarations are translated to Caml functions. |
| 299 | The parameters and results of the Caml function are determined from |
| 300 | those of the IDL function according to the following rules: |
| 301 | </p><ul class="itemize"><li class="li-itemize"> |
| 302 | 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>, |
| 303 | <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 |
| 304 | that are ignored pointers are removed.</li><li class="li-itemize">The remaining parameters are split into Caml function inputs and |
| 305 | Caml function outputs. Parameters with the <span class=machine><span class=font-tt>[in]</span></span> attribute are added |
| 306 | to the inputs of the function. Parameters with the <span class=machine><span class=font-tt>[out]</span></span> |
| 307 | attribute are added to the outputs of the function. Parameters with |
| 308 | the <span class=machine><span class=font-tt>[in,out]</span></span> attribute are added both to the inputs and to the |
| 309 | outputs of the function, unless they are of type <span class=machine><span class=font-tt>bytes</span></span> or big array, |
| 310 | in which case they are added to the inputs of the function only. |
| 311 | (The reason for this exception is that byte arrays and big arrays are |
| 312 | shared between Caml and C, thus allowing true <span class=machine><span class=font-tt>in,out</span></span> behavior on the |
| 313 | Caml function parameter, while other data types are copied during |
| 314 | Caml/C conversion, thus turning a C <span class=machine><span class=font-tt>in,out</span></span> parameter into a Caml |
| 315 | <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 |
| 316 | the Caml function (in first position), unless it is of type <span class=machine><span class=font-tt>void</span></span> or |
| 317 | of a type name that carries the <span class=machine><span class=font-tt>errorcode</span></span> attribute. In the latter |
| 318 | two cases, the return value of the IDL function is not transmitted to |
| 319 | Caml. |
| 320 | </li><li class="li-itemize">The Caml function is then given type |
| 321 | <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> |
| 322 | 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 |
| 323 | 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. |
| 324 | If there are no inputs, a <span class=machine><span class=font-tt>unit</span></span> parameter is added. |
| 325 | If there are no outputs, a <span class=machine><span class=font-tt>unit</span></span> result is added. |
| 326 | </li></ul><p> |
| 327 | Examples: |
| 328 | </p><pre> |
| 329 | int f([in] double x, [in] double y) f : float -> float -> int |
| 330 | </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> |
| 331 | void g([in] int x) g : int -> unit |
| 332 | </pre><blockquote class="quote"> One <span class=machine><span class=font-tt>int</span></span> input, no output </blockquote><pre> |
| 333 | int h() h : unit -> int |
| 334 | </pre><blockquote class="quote"> No input, one <span class=machine><span class=font-tt>int</span></span> result </blockquote><pre> |
| 335 | void i([in] int x, [out] double * y) i : int -> double |
| 336 | </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> |
| 337 | parameter) </blockquote><pre> |
| 338 | int j([in] int x, [out] double * y) j : int -> int * double |
| 339 | </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 |
| 340 | <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> |
| 341 | void k([in,out,ref] int * x) k : int -> int |
| 342 | </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 |
| 343 | <span class=machine><span class=font-tt>int</span></span> output. </blockquote><pre> |
| 344 | HRESULT l([in] int x, [out] int * res1, [out] int * res2) |
| 345 | l : int -> int * int |
| 346 | </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> |
| 347 | attribute, hence it is ignored. It remains one <span class=machine><span class=font-tt>int</span></span> input and |
| 348 | 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> |
| 349 | void m([in] int len, [in,size_is(len)] double d[]) |
| 350 | m : float array -> int |
| 351 | </pre><blockquote class="quote"> <span class=machine><span class=font-tt>len</span></span> is a dependent parameter, hence is ignored. The |
| 352 | only input is the <span class=machine><span class=font-tt>double</span></span> array </blockquote><pre> |
| 353 | void n([in] int inputlen, [out] int * outputlen, |
| 354 | [in,out,size_is(inputlen),length_is(*outputlen)] double d[]) |
| 355 | n : float array -> float array |
| 356 | </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 |
| 357 | dependent, hence ignored. The <span class=machine><span class=font-tt>double</span></span> array is both an input |
| 358 | and an output. </blockquote><pre> |
| 359 | void p([in] int dimx, [in] int dimy, |
| 360 | [in,out,bigarray,size_is(dimx,dimy)] double d[][]) |
| 361 | p : (float, Bigarray.float64_elt, Bigarray.c_layout) Bigarray.Array2.t -> unit |
| 362 | </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 |
| 363 | (determined from the dimensions of the big array argument), |
| 364 | 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>, |
| 365 | is a big array, hence passed as an input that will be modified in |
| 366 | place by the C function <span class=machine><span class=font-tt>p</span></span>. The Caml function has no outputs. |
| 367 | </blockquote> |
| 368 | <h4 id="sec28" class="paragraph">Error checking:</h4> |
| 369 | <p> |
| 370 | 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> |
| 371 | attribute, the error checking function <span class=nonterminal>fn</span> is called after the C |
| 372 | function returns. That function is assumed to raise a Caml exception |
| 373 | if it finds an output denoting an error.</p> |
| 374 | <h4 id="sec29" class="paragraph">Custom calling and deallocation sequences:</h4> |
| 375 | <p> |
| 376 | The IDL declaration for a function can optionally specify a custom |
| 377 | calling sequence and/or a custom deallocation sequence, via <span class=nonterminal>quote</span> |
| 378 | clauses following the function declaration: |
| 379 | </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" > |
| 380 | <a href="main002.html#attributes" class="syntax"><span class=nonterminal>attributes</span></a> <a href="main002.html#type-spec" class="syntax"><span class=nonterminal>type-spec</span></a> {<span class=syntax-token>*</span>} <a href="main002.html#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>(</span> <a href="main002.html#params" class="syntax"><span class=nonterminal>params</span></a> <span class=syntax-token>)</span> |
| 381 | { <span class=syntax-token>quote</span><span class=syntax-token>(</span> <a href="main002.html#ident" class="syntax"><span class=nonterminal>ident</span></a> <span class=syntax-token>,</span> <a href="main002.html#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>)</span> } </td></tr> |
| 382 | <tr><td style="text-align:right;white-space:nowrap" > </td></tr> |
| 383 | </table></td></tr> |
| 384 | </table></div><p> |
| 385 | The general shape of a <span class=machine><span class=font-tt>camlidl</span></span>-generated stub function is as |
| 386 | follows: |
| 387 | </p><pre> |
| 388 | value caml_wrapper(value camlparam1, ..., value camlparamK) |
| 389 | |
| 390 | /* Convert the function parameters from Caml to C */ |
| 391 | param1 = ...; |
| 392 | ... |
| 393 | paramN = ...; |
| 394 | /* Call the C function ’ident’ */ |
| 395 | _res = ident(param1, ..., paramN); |
| 396 | /* Convert the function result and out parameters to Caml values */ |
| 397 | camlres = ...; |
| 398 | /* Return result to Caml */ |
| 399 | return camlres; |
| 400 | |
| 401 | </pre><p> |
| 402 | A <span class=syntax-token>quote(call,</span> <a href="main002.html#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>)</span> clause causes the C statements in |
| 403 | <a href="main002.html#string" class="syntax"><span class=nonterminal>string</span></a> to be inserted in the generated stub code |
| 404 | 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>. |
| 405 | Thus, the statements in <a href="main002.html#string" class="syntax"><span class=nonterminal>string</span></a> find the converted parameters in |
| 406 | local variables that have the same names as the parameters in the IDL |
| 407 | declaration, and should leave the result of the function, if any, in |
| 408 | 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="main002.html#string" class="syntax"><span class=nonterminal>string</span></a> <span class=syntax-token>)</span> clause causes the C statements in |
| 409 | <a href="main002.html#string" class="syntax"><span class=nonterminal>string</span></a> to be inserted in the generated stub code just before the |
| 410 | stub function returns, hence after the conversion of the C function |
| 411 | results to Caml values. Again, the statements in <a href="main002.html#string" class="syntax"><span class=nonterminal>string</span></a> have access |
| 412 | 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 |
| 413 | parameters in local variables having the same names as the |
| 414 | parameters. Since the function results and out parameters have |
| 415 | already been converted to Caml values, the code in <a href="main002.html#string" class="syntax"><span class=nonterminal>string</span></a> can safely |
| 416 | deallocate the data structures they point to.</p><p>Custom calling sequences are typically used to rearrange or combine |
| 417 | function parameters, and to perform extra error checks on the |
| 418 | arguments and results. For instance, the Unix <span class=machine><span class=font-tt>write</span></span> system call can |
| 419 | be specified in IDL as follows: |
| 420 | </p><pre> int write([in] int fd, |
| 421 | [in,string,length_is(len)] char * data, |
| 422 | [in] int len, |
| 423 | [in] int ofs, |
| 424 | [in] int towrite) |
| 425 | quote(call, |
| 426 | " /* Validate the arguments */ |
| 427 | if (ofs < 0 || ofs + towrite >= len) failwith(\"write\"); |
| 428 | /* Perform the write */ |
| 429 | _res = write(fd, data + ofs, towrite); |
| 430 | /* Validate the result */ |
| 431 | if (_res == -1) failwith(\"write\"); "); |
| 432 | </pre><p>Custom deallocation sequences are useful to free data structures |
| 433 | dynamically allocated and returned by the C function. For instance, |
| 434 | 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 |
| 435 | IDL as follows: |
| 436 | </p><pre> [string] char * f([in] int x) |
| 437 | quote(dealloc, "free(_res); "); |
| 438 | </pre><p>If the string is returned as an <span class=machine><span class=font-tt>out</span></span> parameter instead, we would write: |
| 439 | </p><pre> void f ([in] int x, [out, string*] char ** str) |
| 440 | quote(dealloc, "free(*str); "); |
| 441 | </pre> |
| 442 | <h4 id="sec30" class="paragraph">Blocking functions:</h4> |
| 443 | <p> |
| 444 | A function can be given the attribute <span class=machine><span class=font-tt>blocking</span></span> to indicate that it |
| 445 | may block on an input/output operation. The generated code will then |
| 446 | allow other Caml threads to execute concurrently with the operation.</p> |
| 447 | <h3 id="sec31" class="subsection">3.10 Interfaces</h3> |
| 448 | <p>IDL interfaces that do not have the <span class=syntax-token>object</span> attribute are |
| 449 | essentially ignored. That is, the declarations contained in the |
| 450 | interface are processed as if they occurred at the top-level of the |
| 451 | IDL file. The <span class=syntax-token>pointer_default</span>, <span class=syntax-token>int_default</span> and |
| 452 | <span class=syntax-token>long_default</span> attributes to the interface can be |
| 453 | used to specify the default pointer kind and integer mappings |
| 454 | for the declarations contained in the interface. Other attributes, as |
| 455 | 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 |
| 456 | interfaces. The function declarations contained in the interface |
| 457 | specify the methods of the COM interface. Other kinds of declarations |
| 458 | (type declarations, <span class=syntax-token>import</span> statements, etc) are treated as if they |
| 459 | occurred at the top-level of the IDL file. An optional |
| 460 | super-interface can be given, in which case the COM interface |
| 461 | implements the methods of the super-interface in addition to those |
| 462 | specified in the IDL interface. |
| 463 | Example: |
| 464 | </p><pre>[object, uuid(...)] interface IA { typedef int t; int f(int x); } |
| 465 | [object] interface IB : IA { import "foo.idl"; void g([string] char * s); } |
| 466 | </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 |
| 467 | addition, two interfaces are declared: <span class=machine><span class=font-tt>IA</span></span>, containing one |
| 468 | 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 |
| 469 | 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 |
| 470 | Caml definitions: |
| 471 | </p><ul class="itemize"><li class="li-itemize"> |
| 472 | An abstract type <span class=font-it>i</span> identifying the interface. |
| 473 | COM interfaces of type <span class=font-it>i</span> are represented in Caml |
| 474 | with type <span class=font-it>i</span> <span class=machine><span class=font-tt> Com.interface</span></span>. |
| 475 | </li><li class="li-itemize">If a super-interface <span class=font-it>s</span> is given, a conversion function |
| 476 | <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 |
| 477 | <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>. |
| 478 | </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 |
| 479 | <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 |
| 480 | identifier. |
| 481 | </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 |
| 482 | interface. |
| 483 | </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 |
| 484 | allows the methods of the COM object to be invoked from Caml. |
| 485 | </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> |
| 486 | <span class=machine><span class=font-tt> Com.interface</span></span>, to transform a Caml object into a COM object with |
| 487 | interface <span class=font-it>i</span>. This allows the methods of the Caml object to be |
| 488 | invoked from any COM client. |
| 489 | </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 |
| 490 | definitions are generated for <span class=machine><span class=font-tt>IA</span></span>: |
| 491 | </p><pre>type iA |
| 492 | val iid_iA : iA Com.iid |
| 493 | class iA_class : iA Com.interface -> object method f : int -> int end |
| 494 | val use_iA : iA Com.interface -> iA_class |
| 495 | val make_iA : #iA_class -> iA Com.interface |
| 496 | </pre><p>For <span class=machine><span class=font-tt>IB</span></span>, we get: |
| 497 | </p><pre>type iB |
| 498 | val iA_of_iB : iB Com.interface -> iA Com.interface |
| 499 | class iB_class : |
| 500 | iB Com.interface -> object inherit iA_class method g : string -> unit end |
| 501 | val use_iB : iB Com.interface -> iB_class |
| 502 | val make_iB : #iB_class -> iB Com.interface |
| 503 | </pre> |
| 504 | <h4 id="sec32" class="paragraph">Error handling in interfaces:</h4> |
| 505 | <p> Conventionally, methods of |
| 506 | COM interfaces always return a result of type <span class=machine><span class=font-tt>HRESULT</span></span> that says |
| 507 | whether the method succeeded or failed, and in the latter case returns |
| 508 | an error code to its caller.</p><p>When calling an interface method from Caml, if the method returns an |
| 509 | <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 |
| 510 | message describing the error. Successful <span class=machine><span class=font-tt>HRESULT</span></span> return values are |
| 511 | ignored. To make them available to Caml, <span class=machine><span class=font-tt>camlidl</span></span> defines the types |
| 512 | <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 |
| 513 | types instead of <span class=machine><span class=font-tt>HRESULT</span></span>, failure results are mapped to |
| 514 | <span class=machine><span class=font-tt>Com.Error</span></span> exceptions as before, but successful results are mapped to |
| 515 | 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>, |
| 516 | 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 |
| 517 | 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 |
| 518 | 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 |
| 519 | escapes the Caml method is mapped back to a failure <span class=machine><span class=font-tt>HRESULT</span></span>. A |
| 520 | textual description of the uncaught exception is saved using |
| 521 | <span class=machine><span class=font-tt>SetLastError</span></span>, and can be consulted by the COM client using |
| 522 | <span class=machine><span class=font-tt>GetLastError</span></span> (this is the standard convention for passing extended |
| 523 | 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> |
| 524 | types, any exception escaping the Caml method aborts the whole program |
| 525 | after printing a description of the exception. Hence, programmers of |
| 526 | Caml components should either use <span class=machine><span class=font-tt>HRESULT</span></span> as result type, or make |
| 527 | very sure that all exceptions are properly caught by the method.</p> |
| 528 | <hr> |
| 529 | <a href="main002.html"><img src="previous_motif.svg" alt="Previous"></a> |
| 530 | <a href="index.html"><img src="contents_motif.svg" alt="Up"></a> |
| 531 | <a href="main004.html"><img src="next_motif.svg" alt="Next"></a> |
| 532 | </body> |
| 533 | </html> |