1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>AARM95 - Interfacing with C</TITLE>
<META NAME="Author" CONTENT="JTC1/SC22/WG9/ARG, by Randall Brukardt, ARG Editor">
<META NAME="GENERATOR" CONTENT="Arm_Form.Exe, Ada Reference Manual generator">
<STYLE type="text/css">
DIV.paranum {position: absolute; font-family: Arial, Helvetica, sans-serif; left: 0.5 em; top: auto}
TT {font-family: "Courier New", monospace}
DT {display: compact}
DIV.Normal {font-family: "Times New Roman", Times, serif; margin-bottom: 0.6em}
DIV.Wide {font-family: "Times New Roman", Times, serif; margin-top: 0.6em; margin-bottom: 0.6em}
DIV.Annotations {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-bottom: 0.6em}
DIV.WideAnnotations {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0.6em; margin-bottom: 0.6em}
DIV.Index {font-family: "Times New Roman", Times, serif}
DIV.SyntaxSummary {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.4em}
DIV.Notes {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.6em}
DIV.NotesHeader {font-family: "Times New Roman", Times, serif; margin-left: 2.0em}
DIV.SyntaxIndented {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.4em}
DIV.Indented {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-bottom: 0.6em}
DIV.CodeIndented {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-bottom: 0.6em}
DIV.SmallIndented {font-family: "Times New Roman", Times, serif; margin-left: 10.0em; margin-bottom: 0.6em}
DIV.SmallCodeIndented {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-bottom: 0.6em}
DIV.Examples {font-family: "Courier New", monospace; margin-left: 2.0em; margin-bottom: 0.6em}
DIV.SmallExamples {font-family: "Courier New", monospace; font-size: 80%; margin-left: 7.5em; margin-bottom: 0.6em}
DIV.IndentedExamples {font-family: "Courier New", monospace; margin-left: 8.0em; margin-bottom: 0.6em}
DIV.SmallIndentedExamples {font-family: "Courier New", monospace; font-size: 80%; margin-left: 15.0em; margin-bottom: 0.6em}
UL.Bulleted {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SmallBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SmallNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.IndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.CodeIndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.CodeIndentedNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SyntaxIndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NotesBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NotesNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
DL.Hanging {font-family: "Times New Roman", Times, serif; margin-top: 0em; margin-bottom: 0.6em}
DD.Hanging {margin-left: 6.0em}
DL.IndentedHanging {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.IndentedHanging {margin-left: 2.0em}
DL.HangingInBulleted {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.HangingInBulleted {margin-left: 4.0em}
DL.SmallHanging {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.SmallHanging {margin-left: 7.5em}
DL.SmallIndentedHanging {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.SmallIndentedHanging {margin-left: 2.0em}
DL.SmallHangingInBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.SmallHangingInBulleted {margin-left: 5.0em}
DL.Enumerated {font-family: "Times New Roman", Times, serif; margin-right: 0.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.Enumerated {margin-left: 2.0em}
DL.SmallEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.SmallEnumerated {margin-left: 2.5em}
DL.NestedEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
DL.SmallNestedEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
</STYLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFF0" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
<P><A HREF="AA-TOC.html">Contents</A> <A HREF="AA-0-29.html">Index</A> <A HREF="AA-B-2.html">Previous</A> <A HREF="AA-B-3-1.html">Next</A></P>
<HR>
<H1> B.3 Interfacing with C</H1>
<DIV Class="Paranum"><FONT SIZE=-2>1/1</FONT></DIV>
<DIV Class="Normal"> {<I><A HREF="defect1.html#8652/0059">8652/0059</A></I>}
<A NAME="I6348"></A><A NAME="I6349"></A>The facilities relevant to interfacing
with the C language are the package Interfaces.C and its children; <S>and
</S>support for the Import, Export, and Convention pragmas with <I>convention</I>_<FONT FACE="Arial, Helvetica">identifier</FONT>
C<U>; and support for the Convention pragma with <I>convention</I>_<FONT FACE="Arial, Helvetica">identifier</FONT>
C_Pass_By_Copy</U>.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>2</FONT></DIV>
<DIV Class="Normal"> The package Interfaces.C contains the basic types,
constants and subprograms that allow an Ada program to pass scalars and
strings to C functions. </DIV>
<H4 ALIGN=CENTER>Static Semantics</H4>
<DIV Class="Paranum"><FONT SIZE=-2>3</FONT></DIV>
<DIV Class="Normal" Style="margin-bottom: 0.4em"> The library package
Interfaces.C has the following declaration: </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4</FONT></DIV>
<DIV Class="Examples"><TT><B>package</B> Interfaces.C <B>is</B><A NAME="I6350"></A><A NAME="I6351"></A><A NAME="I6352"></A><BR>
<B>pragma</B> Pure(C);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>5</FONT></DIV>
<DIV Class="Examples"><TT> <I>-- Declarations based on C's <limits.h></I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>6</FONT></DIV>
<DIV Class="Examples"><TT> <A NAME="I6353"></A>CHAR_BIT : <B>constant</B> := <I>implementation-defined</I>; <I>-- typically 8</I><BR>
<A NAME="I6354"></A>SCHAR_MIN : <B>constant</B> := <I>implementation-defined</I>; <I>-- typically -128</I><BR>
<A NAME="I6355"></A>SCHAR_MAX : <B>constant</B> := <I>implementation-defined</I>; <I>-- typically 127</I><BR>
<A NAME="I6356"></A>UCHAR_MAX : <B>constant</B> := <I>implementation-defined</I>; <I>-- typically 255</I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>7</FONT></DIV>
<DIV Class="Examples"><TT> <I>-- Signed and Unsigned Integers</I><BR>
<B>type</B> <A NAME="I6357"></A><A NAME="I6358"></A>int <B>is</B> <B>range</B> <I>implementation-defined</I>;<BR>
<B>type</B> <A NAME="I6359"></A><A NAME="I6360"></A>short <B>is</B> <B>range</B> <I>implementation-defined</I>;<BR>
<B>type</B> <A NAME="I6361"></A><A NAME="I6362"></A>long <B>is</B> <B>range</B> <I>implementation-defined</I>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>8</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6363"></A><A NAME="I6364"></A>signed_char <B>is</B> <B>range</B> SCHAR_MIN .. SCHAR_MAX;<BR>
<B>for</B> signed_char'Size <B>use</B> CHAR_BIT;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>9</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6365"></A><A NAME="I6366"></A>unsigned <B>is</B> <B>mod</B> <I>implementation-defined</I>;<BR>
<B>type</B> <A NAME="I6367"></A><A NAME="I6368"></A>unsigned_short <B>is</B> <B>mod</B> <I>implementation-defined</I>;<BR>
<B>type</B> <A NAME="I6369"></A><A NAME="I6370"></A>unsigned_long <B>is</B> <B>mod</B> <I>implementation-defined</I>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>10</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6371"></A><A NAME="I6372"></A>unsigned_char <B>is</B> <B>mod</B> (UCHAR_MAX+1);<BR>
<B>for</B> unsigned_char'Size <B>use</B> CHAR_BIT;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11</FONT></DIV>
<DIV Class="Examples"><TT> <B>subtype</B> <A NAME="I6373"></A><A NAME="I6374"></A>plain_char <B>is</B> <I>implementation-defined</I>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>12</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6375"></A><A NAME="I6376"></A>ptrdiff_t <B>is</B> <B>range</B> <I>implementation-defined</I>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>13</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6377"></A><A NAME="I6378"></A>size_t <B>is</B> <B>mod</B> <I>implementation-defined</I>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>14</FONT></DIV>
<DIV Class="Examples"><TT> <I>-- Floating Point</I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>15</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6379"></A><A NAME="I6380"></A>C_float <B>is</B> <B>digits</B> <I>implementation-defined</I>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>16</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6381"></A><A NAME="I6382"></A>double <B>is</B> <B>digits</B> <I>implementation-defined</I>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6383"></A><A NAME="I6384"></A>long_double <B>is</B> <B>digits</B> <I>implementation-defined</I>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>18</FONT></DIV>
<DIV Class="Examples"><TT> <I>-- Characters and Strings </I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>19</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6385"></A><A NAME="I6386"></A>char <B>is</B> <I><implementation-defined character type></I>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>20/1</FONT></DIV>
<DIV Class="Examples"><TT>{<I><A HREF="defect1.html#8652/0060">8652/0060</A></I>}
<BR>
<A NAME="I6387"></A>nul : <B>constant</B> char := <U><I>implementation-defined</I></U><S>char'First</S>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>21</FONT></DIV>
<DIV Class="Examples"><TT> <B>function</B> <A NAME="I6388"></A><A NAME="I6389"></A>To_C (Item : <B>in</B> Character) <B>return</B> char;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>22</FONT></DIV>
<DIV Class="Examples"><TT> <B>function</B> <A NAME="I6390"></A><A NAME="I6391"></A>To_Ada (Item : <B>in</B> char) <B>return</B> Character;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>23</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6392"></A><A NAME="I6393"></A>char_array <B>is</B> <B>array</B> (size_t <B>range</B> <>) <B>of</B> <B>aliased</B> char;<BR>
<B>pragma</B> Pack(char_array);<BR>
<B>for</B> char_array'Component_Size <B>use</B> CHAR_BIT;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>24</FONT></DIV>
<DIV Class="Examples"><TT> <B>function</B> <A NAME="I6394"></A><A NAME="I6395"></A>Is_Nul_Terminated (Item : <B>in</B> char_array) <B>return</B> Boolean;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>25</FONT></DIV>
<DIV Class="Examples"><TT> <B>function</B> <A NAME="I6396"></A><A NAME="I6397"></A>To_C (Item : <B>in</B> String;<BR>
Append_Nul : <B>in</B> Boolean := True)<BR>
<B>return</B> char_array;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>26</FONT></DIV>
<DIV Class="Examples"><TT> <B>function</B> <A NAME="I6398"></A><A NAME="I6399"></A>To_Ada (Item : <B>in</B> char_array;<BR>
Trim_Nul : <B>in</B> Boolean := True)<BR>
<B>return</B> String;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>27</FONT></DIV>
<DIV Class="Examples"><TT> <B>procedure</B> <A NAME="I6400"></A><A NAME="I6401"></A>To_C (Item : <B>in</B> String;<BR>
Target : <B>out</B> char_array;<BR>
Count : <B>out</B> size_t;<BR>
Append_Nul : <B>in</B> Boolean := True);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>28</FONT></DIV>
<DIV Class="Examples"><TT> <B>procedure</B> <A NAME="I6402"></A><A NAME="I6403"></A>To_Ada (Item : <B>in</B> char_array;<BR>
Target : <B>out</B> String;<BR>
Count : <B>out</B> Natural;<BR>
Trim_Nul : <B>in</B> Boolean := True);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>29</FONT></DIV>
<DIV Class="Examples"><TT> <I>-- Wide Character and Wide String</I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>30/1</FONT></DIV>
<DIV Class="Examples"><TT>{<I><A HREF="defect1.html#8652/0060">8652/0060</A></I>}
<BR>
<B>type</B> <A NAME="I6404"></A><A NAME="I6405"></A>wchar_t <B>is</B> <U><I><implementation-defined character type></I></U><S><I>implementation-defined</I></S>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>31/1</FONT></DIV>
<DIV Class="Examples"><TT>{<I><A HREF="defect1.html#8652/0060">8652/0060</A></I>}
<BR>
<A NAME="I6406"></A>wide_nul : <B>constant</B> wchar_t := <U><I>implementation-defined</I></U><S>wchar_t'First</S>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>32</FONT></DIV>
<DIV Class="Examples"><TT> <B>function</B> <A NAME="I6407"></A><A NAME="I6408"></A>To_C (Item : <B>in</B> Wide_Character) <B>return</B> wchar_t;<BR>
<B>function</B> <A NAME="I6409"></A><A NAME="I6410"></A>To_Ada (Item : <B>in</B> wchar_t ) <B>return</B> Wide_Character;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>33</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I6411"></A><A NAME="I6412"></A>wchar_array <B>is</B> <B>array</B> (size_t <B>range</B> <>) <B>of</B> <B>aliased</B> wchar_t;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>34</FONT></DIV>
<DIV Class="Examples"><TT> <B>pragma</B> Pack(wchar_array);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>35</FONT></DIV>
<DIV Class="Examples"><TT> <B>function</B> <A NAME="I6413"></A><A NAME="I6414"></A>Is_Nul_Terminated (Item : <B>in</B> wchar_array) <B>return</B> Boolean;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>36</FONT></DIV>
<DIV Class="Examples"><TT> <B>function</B> <A NAME="I6415"></A><A NAME="I6416"></A>To_C (Item : <B>in</B> Wide_String;<BR>
Append_Nul : <B>in</B> Boolean := True)<BR>
<B>return</B> wchar_array;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>37</FONT></DIV>
<DIV Class="Examples"><TT> <B>function</B> <A NAME="I6417"></A><A NAME="I6418"></A>To_Ada (Item : <B>in</B> wchar_array;<BR>
Trim_Nul : <B>in</B> Boolean := True)<BR>
<B>return</B> Wide_String;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>38</FONT></DIV>
<DIV Class="Examples"><TT> <B>procedure</B> <A NAME="I6419"></A><A NAME="I6420"></A>To_C (Item : <B>in</B> Wide_String;<BR>
Target : <B>out</B> wchar_array;<BR>
Count : <B>out</B> size_t;<BR>
Append_Nul : <B>in</B> Boolean := True);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>39</FONT></DIV>
<DIV Class="Examples"><TT> <B>procedure</B> <A NAME="I6421"></A><A NAME="I6422"></A>To_Ada (Item : <B>in</B> wchar_array;<BR>
Target : <B>out</B> Wide_String;<BR>
Count : <B>out</B> Natural;<BR>
Trim_Nul : <B>in</B> Boolean := True);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>40</FONT></DIV>
<DIV Class="Examples"><TT> <A NAME="I6423"></A>Terminator_Error : <B>exception</B>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>41</FONT></DIV>
<DIV Class="Examples"><TT><B>end</B> Interfaces.C;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>41.a.1/1</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Implementation defined: </B><U>The
definitions of types and constants in Interfaces.C.</U></FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>42</FONT></DIV>
<DIV Class="Normal"> Each of the types declared in Interfaces.C is
C-compatible.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>43</FONT></DIV>
<DIV Class="Normal"> The types int, short, long, unsigned, ptrdiff_t,
size_t, double, char, and wchar_t correspond respectively to the C types
having the same names. The types signed_char, unsigned_short, unsigned_long,
unsigned_char, C_float, and long_double correspond respectively to the
C types signed char, unsigned short, unsigned long, unsigned char, float,
and long double.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>44</FONT></DIV>
<DIV Class="Normal" Style="margin-bottom: 0.9em"> The type of the
subtype plain_char is either signed_char or unsigned_char, depending
on the C implementation. </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>45</FONT></DIV>
<DIV Class="Examples"><TT><B>function</B> To_C (Item : <B>in</B> Character) <B>return</B> char;<BR>
<B>function</B> To_Ada (Item : <B>in</B> char ) <B>return</B> Character;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>46</FONT></DIV>
<DIV Class="CodeIndented" Style="margin-bottom: 0.9em">The functions
To_C and To_Ada map between the Ada type Character and the C type char.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>46.a.1/1</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Implementation Note: </B>{<I><A HREF="defect2.html#8652/0114">8652/0114</A></I>}
<U>The To_C and To_Ada functions map between corresponding characters,
not necessarily between characters with the same internal representation.
Corresponding characters are characters defined by the same enumeration
literal, if such exist; otherwise, the correspondence is unspecified.<A NAME="I6424"></A></U></FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>46.a.2/1</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><U>The following definition is
equivalent to the above summary:</U></FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>46.a.3/1</FONT></DIV>
<DIV Class="Annotations" Style="margin-bottom: 0.4em"><FONT SIZE=-1><U><TT>To_C
(Latin_1_Char) = char'Value(Character'Image(Latin_1_Char))</TT><BR>
provided that char'Value does not raise an exception; otherwise the result
is unspecified.</U></FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>46.a.4/1</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><U><TT>To_Ada (Native_C_Char)
= Character'Value(char'Image(Native_C_Char))</TT><BR>
provided that Character'Value does not raise an exception; otherwise
the result is unspecified.</U> </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>47</FONT></DIV>
<DIV Class="Examples"><TT><B>function</B> Is_Nul_Terminated (Item : <B>in</B> char_array) <B>return</B> Boolean;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>48</FONT></DIV>
<DIV Class="CodeIndented" Style="margin-bottom: 0.9em">The result of
Is_Nul_Terminated is True if Item contains nul, and is False otherwise.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>49</FONT></DIV>
<DIV Class="Examples"><TT><B>function</B> To_C (Item : <B>in</B> String; Append_Nul : <B>in</B> Boolean := True)<BR>
<B>return</B> char_array;<BR>
<BR>
<B>function</B> To_Ada (Item : <B>in</B> char_array; Trim_Nul : <B>in</B> Boolean := True)<BR>
<B>return</B> String;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>50</FONT></DIV>
<DIV Class="CodeIndented">The result of To_C is a char_array value of
length Item'Length (if Append_Nul is False) or Item'Length+1 (if Append_Nul
is True). The lower bound is 0. For each component Item(I), the corresponding
component in the result is To_C applied to Item(I). The value nul is
appended if Append_Nul is True.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>51</FONT></DIV>
<DIV Class="CodeIndented" Style="margin-bottom: 0.9em">The result of
To_Ada is a String whose length is Item'Length (if Trim_Nul is False)
or the length of the slice of Item preceding the first nul (if Trim_Nul
is True). The lower bound of the result is 1. If Trim_Nul is False, then
for each component Item(I) the corresponding component in the result
is To_Ada applied to Item(I). If Trim_Nul is True, then for each component
Item(I) before the first nul the corresponding component in the result
is To_Ada applied to Item(I). The function propagates Terminator_Error
if Trim_Nul is True and Item does not contain nul.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>52</FONT></DIV>
<DIV Class="Examples"><TT><B>procedure</B> To_C (Item : <B>in</B> String;<BR>
Target : <B>out</B> char_array;<BR>
Count : <B>out</B> size_t;<BR>
Append_Nul : <B>in</B> Boolean := True);<BR>
<BR>
<B>procedure</B> To_Ada (Item : <B>in</B> char_array;<BR>
Target : <B>out</B> String;<BR>
Count : <B>out</B> Natural;<BR>
Trim_Nul : <B>in</B> Boolean := True);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>53</FONT></DIV>
<DIV Class="CodeIndented">For procedure To_C, each element of Item is
converted (via the To_C function) to a char, which is assigned to the
corresponding element of Target. If Append_Nul is True, nul is then assigned
to the next element of Target. In either case, Count is set to the number
of Target elements assigned. <A NAME="I6425"></A>If Target is not long
enough, Constraint_Error is propagated.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>54</FONT></DIV>
<DIV Class="CodeIndented" Style="margin-bottom: 0.9em">For procedure
To_Ada, each element of Item (if Trim_Nul is False) or each element of
Item preceding the first nul (if Trim_Nul is True) is converted (via
the To_Ada function) to a Character, which is assigned to the corresponding
element of Target. Count is set to the number of Target elements assigned.
<A NAME="I6426"></A>If Target is not long enough, Constraint_Error is
propagated. If Trim_Nul is True and Item does not contain nul, then Terminator_Error
is propagated.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>55</FONT></DIV>
<DIV Class="Examples"><TT><B>function</B> Is_Nul_Terminated (Item : <B>in</B> wchar_array) <B>return</B> Boolean;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>56</FONT></DIV>
<DIV Class="CodeIndented" Style="margin-bottom: 0.9em">The result of
Is_Nul_Terminated is True if Item contains wide_nul, and is False otherwise.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>57</FONT></DIV>
<DIV Class="Examples"><TT><B>function</B> To_C (Item : <B>in</B> Wide_Character) <B>return</B> wchar_t;<BR>
<B>function</B> To_Ada (Item : <B>in</B> wchar_t ) <B>return</B> Wide_Character;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>58</FONT></DIV>
<DIV Class="CodeIndented" Style="margin-bottom: 0.9em">To_C and To_Ada
provide the mappings between the Ada and C wide character types.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>59</FONT></DIV>
<DIV Class="Examples"><TT><B>function</B> To_C (Item : <B>in</B> Wide_String;<BR>
Append_Nul : <B>in</B> Boolean := True)<BR>
<B>return</B> wchar_array;<BR>
<BR>
<B>function</B> To_Ada (Item : <B>in</B> wchar_array;<BR>
Trim_Nul : <B>in</B> Boolean := True)<BR>
<B>return</B> Wide_String;<BR>
<BR>
<B>procedure</B> To_C (Item : <B>in</B> Wide_String;<BR>
Target : <B>out</B> wchar_array;<BR>
Count : <B>out</B> size_t;<BR>
Append_Nul : <B>in</B> Boolean := True);<BR>
<BR>
<B>procedure</B> To_Ada (Item : <B>in</B> wchar_array;<BR>
Target : <B>out</B> Wide_String;<BR>
Count : <B>out</B> Natural;<BR>
Trim_Nul : <B>in</B> Boolean := True);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60</FONT></DIV>
<DIV Class="CodeIndented">The To_C and To_Ada subprograms that convert
between Wide_String and wchar_array have analogous effects to the To_C
and To_Ada subprograms that convert between String and char_array, except
that wide_nul is used instead of nul. </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Discussion: </B>The Interfaces.C
package provides an implementation-defined character type, char, designed
to model the C run-time character set, and mappings between the types
char and Character.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.b</FONT></DIV>
<DIV Class="Annotations" Style="margin-bottom: 0.4em"><FONT SIZE=-1>One
application of the C interface package is to compose a C string and pass
it to a C function. One way to do this is for the programmer to declare
an object that will hold the C array, and then pass this array to the
C function. This is realized via the type char_array: </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.c</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>type</B> char_array <B>is</B> <B>array</B> (size_t <B>range</B> <>) of Char;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.d</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>The programmer can declare an
Ada String, convert it to a char_array, and pass the char_array as actual
parameter to the C function that is expecting a char *.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.e</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>An alternative approach is for
the programmer to obtain a C char pointer from an Ada String (or from
a char_array) by invoking an allocation function. The package Interfaces.C.Strings
(see below) supplies the needed facilities, including a private type
chars_ptr that corresponds to C's char *, and two allocation functions.
To avoid storage leakage, a Free procedure releases the storage that
was allocated by one of these allocate functions.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.f</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>It is typical for a C function
that deals with strings to adopt the convention that the string is delimited
by a nul char. The C interface packages support this convention. A constant
nul of type Char is declared, and the function Value(Chars_Ptr) in Interfaces.C.Strings
returns a char_array up to and including the first nul in the array that
the chars_ptr points to. The Allocate_Chars function allocates an array
that is nul terminated.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.g</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>Some C functions that deal with
strings take an explicit length as a parameter, thus allowing strings
to be passed that contain nul as a data element. Other C functions take
an explicit length that is an upper bound: the prefix of the string up
to the char before nul, or the prefix of the given length, is used by
the function, whichever is shorter. The C Interface packages support
calling such functions. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.1/1</FONT></DIV>
<DIV Class="Normal"> {<I><A HREF="defect1.html#8652/0059">8652/0059</A></I>}
<U>A Convention pragma with <I>convention</I>_<FONT FACE="Arial, Helvetica">identifier</FONT>
C_Pass_By_Copy shall only be applied to a type.</U></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.2/1</FONT></DIV>
<DIV Class="Normal"> {<I><A HREF="defect1.html#8652/0059">8652/0059</A></I>}
<U>The eligibility rules in <A HREF="AA-B-1.html">B.1</A> do not apply
to convention C_Pass_By_Copy. Instead, a type T is eligible for convention
C_Pass_By_Copy if T is a record type that has no discriminants and that
only has components with statically constrained subtypes, and each component
is C-compatible.</U></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>60.3/1</FONT></DIV>
<DIV Class="Normal"> {<I><A HREF="defect1.html#8652/0059">8652/0059</A></I>}
<U>If a type is C_Pass_By_Copy-compatible then it is also C-compatible.</U></DIV>
<H4 ALIGN=CENTER>Implementation Requirements</H4>
<DIV Class="Paranum"><FONT SIZE=-2>61/1</FONT></DIV>
<DIV Class="Normal"> {<I><A HREF="defect1.html#8652/0059">8652/0059</A></I>}
An implementation shall support pragma Convention with a C <I>convention</I>_<FONT FACE="Arial, Helvetica">identifier</FONT>
for a C-eligible type (see <A HREF="AA-B-1.html">B.1</A>)<U>. An implementation
shall support pragma Convention with a C_Pass_By_Copy <I>convention</I>_<FONT FACE="Arial, Helvetica">identifier</FONT>
for a C_Pass_By_Copy-eligible type.</U> </DIV>
<H4 ALIGN=CENTER>Implementation Permissions</H4>
<DIV Class="Paranum"><FONT SIZE=-2>62</FONT></DIV>
<DIV Class="Normal"> An implementation may provide additional declarations
in the C interface packages. </DIV>
<H4 ALIGN=CENTER>Implementation Advice</H4>
<DIV Class="Paranum"><FONT SIZE=-2>62.1/1</FONT></DIV>
<DIV Class="Normal"> {<I><A HREF="defect1.html#8652/0060">8652/0060</A></I>}
<U>The constants nul and wide_nul should have a representation of zero.</U></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>63</FONT></DIV>
<DIV Class="Normal"> An implementation should support the following
interface correspondences between Ada and C. </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>64</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>An Ada procedure corresponds to a void-returning C function.
</LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>64.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Discussion: </B>The programmer
can also choose an Ada procedure when the C function returns an int that
is to be discarded.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>65</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>An Ada function corresponds to a non-void C function.</LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>66</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>An Ada <B>in</B> scalar parameter is passed as a scalar
argument to a C function.</LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>67</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>An Ada <B>in</B> parameter of an access-to-object type
with designated type T is passed as a t* argument to a C function, where
t is the C type corresponding to the Ada type T.</LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>68</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>An Ada <B>access</B> T parameter, or an Ada <B>out</B>
or <B>in out</B> parameter of an elementary type T, is passed as a t*
argument to a C function, where t is the C type corresponding to the
Ada type T. In the case of an elementary <B>out</B> or <B>in out</B>
parameter, a pointer to a temporary copy is used to preserve by-copy
semantics.</LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>68.1/1</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>{<I><A HREF="defect1.html#8652/0059">8652/0059</A></I>}
<U>An Ada parameter of a C_Pass_By_Copy-compatible (record) type T, of
mode <B>in</B>, is passed as a t argument to a C function, where t is
the C struct corresponding to the Ada type T.</U></LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>69/1</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>{<I><A HREF="defect1.html#8652/0059">8652/0059</A></I>}
An Ada parameter of a record type T, of any mode, <U>other than an <B>in</B>
parameter of a C_Pass_By_Copy-compatible type,</U> is passed as a t*
argument to a C function, where t is the C struct corresponding to the
Ada type T.</LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>70</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>An Ada parameter of an array type with component type T,
of any mode, is passed as a t* argument to a C function, where t is the
C type corresponding to the Ada type T.</LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>71</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>An Ada parameter of an access-to-subprogram type is passed
as a pointer to a C function whose prototype corresponds to the designated
subprogram's specification. </LI></UL>
<DIV Class="NotesHeader"><FONT SIZE=-1>NOTES</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>72</FONT></DIV>
<DIV Class="Notes"><FONT SIZE=-1>9 Values of type char_array
are not implicitly terminated with nul. If a char_array is to be passed
as a parameter to an imported C function requiring nul termination, it
is the programmer's responsibility to obtain this effect.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>73</FONT></DIV>
<DIV Class="Notes"><FONT SIZE=-1>10 To obtain the effect of
C's sizeof(item_type), where Item_Type is the corresponding Ada type,
evaluate the expression: size_t(Item_Type'Size/CHAR_BIT).</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>74</FONT></DIV>
<DIV Class="Notes"><FONT SIZE=-1>11 There is no explicit support
for C's union types. Unchecked conversions can be used to obtain the
effect of C unions.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>75</FONT></DIV>
<DIV Class="Notes"><FONT SIZE=-1>12 A C function that takes
a variable number of arguments can correspond to several Ada subprograms,
taking various specific numbers and types of parameters. </FONT></DIV>
<H4 ALIGN=CENTER>Examples</H4>
<DIV Class="Paranum"><FONT SIZE=-2>76</FONT></DIV>
<DIV Class="Normal" Style="margin-bottom: 0.4em"> <I>Example of using
the Interfaces.C package:</I> </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>77</FONT></DIV>
<DIV Class="Examples"><TT><I>--Calling the C Library Function strcpy</I><BR>
<B>with</B> Interfaces.C;<BR>
<B>procedure</B> Test <B>is</B><BR>
<B>package</B> C <B>renames</B> Interfaces.C;<BR>
<B>use</B> <B>type</B> C.char_array;<BR>
<I>-- Call <string.h>strcpy:</I><BR>
<I>-- C definition of strcpy: char *strcpy(char *s1, const char *s2);</I><BR>
<I>-- This function copies the string pointed to by s2 (including the terminating null character)</I><BR>
<I>-- into the array pointed to by s1. If copying takes place between objects that overlap, </I><BR>
<I>-- the behavior is undefined. The strcpy function returns the value of s1.</I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>78</FONT></DIV>
<DIV Class="Examples"><TT> <I>-- Note: since the C function's return value is of no interest, the Ada interface is a procedure</I><BR>
<B>procedure</B> Strcpy (Target : <B>out</B> C.char_array;<BR>
Source : <B>in</B> C.char_array);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>79</FONT></DIV>
<DIV Class="Examples"><TT> <B>pragma</B> Import(C, Strcpy, "strcpy");</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>80</FONT></DIV>
<DIV Class="Examples"><TT> Chars1 : C.char_array(1..20);<BR>
Chars2 : C.char_array(1..20);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>81</FONT></DIV>
<DIV Class="Examples"><TT><B>begin</B><BR>
Chars2(1..6) := "qwert" & C.nul;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>82</FONT></DIV>
<DIV Class="Examples"><TT> Strcpy(Chars1, Chars2);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>83</FONT></DIV>
<DIV Class="Examples"><TT><I>-- Now Chars1(1..6) = "qwert" & C.Nul</I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>84</FONT></DIV>
<DIV Class="Examples"><TT><B>end</B> Test;</TT></DIV>
<HR>
<P><A HREF="AA-TOC.html">Contents</A> <A HREF="AA-0-29.html">Index</A> <A HREF="AA-B-2.html">Previous</A> <A HREF="AA-B-3-1.html">Next</A> <A HREF="AA-TTL.html">Legal</A></P>
</BODY>
</HTML>
|