Debian Codegraph @

source_2005/interface.mss

raw
1 @comment{ $Source: e:\\cvsroot/ARM/Source/interface.mss,v $ }
2 @comment{ $Revision: 1.48 $ $Date: 2006/10/18 00:25:28 $ $Author: Randy $ }
3 @Part(interface, Root="ada.mss")
4
5 @Comment{$Date: 2006/10/18 00:25:28 $}
6 @LabeledNormativeAnnex{Interface to Other Languages}
7
8 @begin{Intro}
9 @Defn{interface to other languages}
10 @Defn2{Term=[language], Sec=(interface to non-Ada)}
11 @Defn{mixed-language programs}
12 This Annex describes features for writing mixed-language programs.
13 General interface support is presented first;
14 then specific support for C, COBOL, and Fortran is defined,
15 in terms of language interface packages for each of
16 these languages.
17 @begin{Ramification}
18 This Annex is not a @lquotes@;Specialized Needs@rquotes@; annex.
19 Every implementation must support all non-optional features defined here
20 (mainly the package Interfaces).
21 @end{Ramification}
22 @end{Intro}
23
24 @begin{MetaRules}
25 Ada should have strong support for mixed-language programming.
26 @end{MetaRules}
27
28 @begin{Extend83}
29 @Defn{extensions to Ada 83}
30 Much of the functionality in this Annex is new to Ada 95.
31 @end{Extend83}
32
33 @begin{DiffWord83}
34 This Annex contains what used to be RM83-13.8.
35 @end{DiffWord83}
36
37
38 @LabeledClause{Interfacing Pragmas}
39
40 @begin{Intro}
41 A @nt{pragma} Import is used to import an entity defined in a foreign
42 language into an Ada program,
43 thus allowing
44 a foreign-language subprogram to be called from Ada,
45 or a foreign-language variable to be accessed from Ada.
46 In contrast,
47 a @nt{pragma} Export is used to export an Ada entity
48 to a foreign language, thus allowing
49 an Ada subprogram to be called from a foreign language,
50 or an Ada object
51 to be accessed from a foreign language.
52 The @nt[pragma]s
53 Import and Export
54 are intended primarily for objects and
55 subprograms, although implementations are allowed to support other
56 entities.
57
58 A @nt{pragma} Convention is used to specify that an Ada entity should use
59 the conventions of another language.
60 It is intended primarily for types and @lquotes@;callback@rquotes@; subprograms.
61 For example,
62 @lquotes@;@key{pragma} Convention(Fortran, Matrix);@rquotes@;
63 implies that Matrix should be represented according to the
64 conventions of the supported Fortran implementation, namely
65 column-major order.
66
67 A @nt{pragma} Linker_Options is used to specify the system linker
68 parameters needed when a given compilation unit is included in a
69 partition.
70
71 @end{Intro}
72
73 @begin{Syntax}
74 @begin{SyntaxText}
75 @Leading@RootDefn{interfacing pragma}
76 @PDefn2{Term=[interfacing pragma], Sec=(Import)}
77 @PDefn2{Term=[pragma, interfacing], Sec=(Import)}
78 @PDefn2{Term=[interfacing pragma], Sec=(Export)}
79 @PDefn2{Term=[pragma, interfacing], Sec=(Export)}
80 @PDefn2{Term=[interfacing pragma], Sec=(Convention)}
81 @PDefn2{Term=[pragma, interfacing], Sec=(Convention)}
82 @PDefn2{Term=[pragma, interfacing], Sec=(Linker_Options)}
83 An @i{interfacing pragma} is a representation
84 @nt[pragma] that is
85 one of the @nt{pragma}s Import, Export,
86 or Convention.
87 Their forms, together with that of the related
88 @nt[pragma] Linker_Options, are as follows:
89 @end{SyntaxText}
90 @PragmaSyn`@key{pragma} @prag(Import)(@*
91 @ @ @ @ @ [Convention =>] @SynI{convention_}@Syn2{identifier}, [Entity =>] @Syn2{local_name}@*
92 @ @ [, [External_Name =>] @SynI{string_}@Syn2{expression}] [, [Link_Name =>] @SynI{string_}@Syn2{expression}]);'
93
94 @PragmaSyn`@key{pragma} @prag(Export)(@*
95 @ @ @ @ @ [Convention =>] @SynI{convention_}@Syn2{identifier}, [Entity =>] @Syn2{local_name}@*
96 @ @ [, [External_Name =>] @SynI{string_}@Syn2{expression}] [, [Link_Name =>] @SynI{string_}@Syn2{expression}]);'
97
98 @PragmaSyn`@key{pragma} @prag(Convention)([Convention =>] @SynI{convention_}@Syn2{identifier},[Entity =>] @Syn2{local_name});'
99
100 @PragmaSyn`@key{pragma} @prag(Linker_Options)(@SynI{string_}@Syn2{expression});'
101
102 @begin{SyntaxText}
103 A @nt[pragma] Linker_Options is allowed only at the place of a
104 @nt[declarative_item].
105
106 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0058],ARef=[AI95-00036-01]}
107 @ChgAdded{Version=[1],Text=[For @nt{pragma}s Import and Export, the argument
108 for Link_Name shall not be given without the
109 @i{pragma_@!argument_}@!@nt{identifier}
110 unless the argument for External_Name is given.]}
111 @end{SyntaxText}
112 @end{Syntax}
113
114 @begin{Resolution}
115 @PDefn2{Term=[expected type],
116 Sec=(link name)}
117 The expected type for a @SynI{string_}@Syn2{expression}
118 in an interfacing pragma or in pragma Linker_Options is String.
119 @begin{Ramification}
120 There is no language-defined support for
121 external or link names of type
122 Wide_String, or of other string types.
123 Implementations may, of course, have additional pragmas for that
124 purpose.
125 Note that allowing both String and Wide_String in the same @nt{pragma} would
126 cause ambiguities.
127 @end{Ramification}
128 @end{Resolution}
129
130 @begin{Legality}
131 @Defn{convention}
132 The @SynI{convention_}@nt{identifier} of an interfacing pragma
133 shall be the name of a @i{convention}.
134 The convention names are implementation defined,
135 except for certain language-defined ones,
136 such as Ada and Intrinsic,
137 as explained in @RefSec{Conformance Rules}.
138 @Redundant[Additional convention names generally represent
139 the calling conventions of foreign languages,
140 language implementations, or specific run-time models.]
141 @Defn{calling convention}
142 The convention of a callable entity is its @i{calling convention}.
143 @ImplDef{Implementation-defined convention names.}
144 @begin{Discussion}
145 We considered representing the convention names using an enumeration
146 type declared in System.
147 Then, @SynI{convention_}@nt{identifier} would be changed to
148 @SynI{convention_}@nt{name},
149 and we would make its expected type be the enumeration type.
150 We didn't do this because it seems to introduce extra complexity,
151 and because the list of available languages is better represented as
152 the list of children of package Interfaces @em a more open-ended sort
153 of list.
154 @end{Discussion}
155
156 @Leading@Defn2{Term=[compatible],Sec=[a type, with a convention]}
157 If @i[L] is a @i[convention_]@nt[identifier] for a language, then a
158 type T is said to be @i{compatible with convention L}, (alternatively,
159 is said to be an @i[L-compatible type]) if any of the following conditions
160 are met:
161 @begin[itemize]
162 T is declared in a language interface package
163 corresponding to @i[L] and is defined to be
164 @i[L]-compatible
165 (see @refsecnum(Interfacing with C and C++),
166 @refsecnum(The Package Interfaces.C.Strings),
167 @refsecnum(The Generic Package Interfaces.C.Pointers),
168 @refsecnum(Interfacing with COBOL),
169 @refsecnum(Interfacing with Fortran)),
170
171 @Leading@Defn2{Term=[eligible],Sec=[a type, for a convention]}
172 Convention @i[L] has been specified for T in
173 a @nt[pragma] Convention, and T is @i{eligible for
174 convention @i[L]}; that is:
175 @begin[inneritemize]
176 T is an array type with either an
177 unconstrained or statically-constrained first subtype, and
178 its component type is @i[L]-compatible,
179
180 T is a record type that has no discriminants and that only has
181 components with statically-constrained subtypes, and
182 each component type is @i[L]-compatible,
183
184 T is an access-to-object type, and its designated type is @i[L]-compatible,
185
186 T is an access-to-subprogram type,
187 and its designated profile's parameter and result types are all @i[L]-compatible.
188 @end[inneritemize]
189
190 T is derived from an @i[L]-compatible type,
191
192 The implementation permits T as an @i[L]-compatible type.
193 @begin{discussion}
194
195 For example, an implementation might permit Integer as a C-compatible
196 type, though the C type to which it corresponds might be different
197 in different environments.@end{discussion}
198 @end[itemize]
199
200 If @nt[pragma] Convention applies to a type,
201 then the type shall either be
202 compatible with or eligible for
203 the convention specified in the pragma.
204 @begin[ramification]
205 If a type is derived from an @i[L]-compatible type, the derived type
206 is by default @i[L]-compatible, but it is also permitted to specify
207 pragma Convention for the derived type.
208
209 It is permitted to specify pragma Convention for an incomplete type,
210 but in the complete declaration each component must be
211 @i[L]-compatible.
212
213 If each component of a record type is @i[L]-compatible, then
214 the record type itself is only @i[L]-compatible if it has a pragma
215 Convention.
216 @end[Ramification]
217
218 A @nt{pragma} Import shall be the completion of a declaration.
219 @Defn{notwithstanding}
220 Notwithstanding any rule to the contrary,
221 a @nt{pragma} Import may serve as the completion of any
222 kind of (explicit) declaration if supported by an implementation for
223 that kind of declaration.
224 If a completion is a @nt{pragma} Import,
225 then it shall appear in the same
226 @nt{declarative_part}, @nt{package_specification}, @nt{task_definition}
227 or @nt{protected_definition}
228 as the declaration.
229 For a library unit, it shall appear in the same @nt{compilation},
230 before any subsequent @nt{compilation_unit}s other than @nt{pragma}s.
231 If the @nt{local_name} denotes more than one entity,
232 then the @nt{pragma} Import is the completion of all of them.
233 @begin(Discussion)
234 For declarations of deferred constants and subprograms, we mention
235 pragma Import
236 explicitly as a possible completion. For other declarations that
237 require completions, we ignore the possibility of pragma Import.
238 Nevertheless, if an implementation chooses to allow a @nt{pragma} Import
239 to complete the declaration of a task, protected type, incomplete
240 type, private type, etc., it may do so, and the normal completion
241 is then not allowed for that declaration.
242 @end(Discussion)
243
244 @Defn{imported entity} @Defn{exported entity}
245 An entity specified as the Entity argument to a @nt[pragma] Import
246 (or @nt[pragma] Export)
247 is said to be @i{imported} (respectively, @i{exported}).
248
249 The declaration of an imported object shall not include an explicit
250 initialization expression.
251 @Redundant[Default initializations are not performed.]
252 @begin{TheProof}
253 This follows from the @lquotes@;Notwithstanding ...@rquotes@;
254 wording in the Dynamics Semantics paragraphs below.
255 @end{TheProof}
256
257 The type of an imported or exported object shall be compatible with the
258 convention specified in the corresponding @nt[pragma].
259 @begin[Ramification]
260 This implies, for example, that importing an Integer object might be illegal,
261 whereas importing an object of type Interfaces.C.int would be permitted.
262 @end[Ramification]
263
264 For an imported or exported subprogram, the result and parameter types
265 shall each be compatible with the convention specified in the
266 corresponding pragma.
267
268 The external name and link name @i{string}_@nt[expression]s
269 of a @nt{pragma} Import or Export, and the @i[string]_@nt[expression]
270 of a @nt[pragma] Linker_Options, shall be
271 static.
272 @end{Legality}
273
274 @begin{StaticSem}
275 @PDefn2{Term=[representation pragma], Sec=(Import)}
276 @PDefn2{Term=[pragma, representation], Sec=(Import)}
277 @PDefn2{Term=[representation pragma], Sec=(Export)}
278 @PDefn2{Term=[pragma, representation], Sec=(Export)}
279 @PDefn2{Term=[representation pragma], Sec=(Convention)}
280 @PDefn2{Term=[pragma, representation], Sec=(Convention)}
281 @PDefn2{Term=[aspect of representation], Sec=(convention, calling convention)}
282 @Defn2{Term=[convention], Sec=(aspect of representation)}
283 Import, Export, and Convention @nt{pragma}s are representation pragmas
284 that specify the @i{convention} aspect of representation.
285 @PDefn2{Term=[aspect of representation], Sec=(imported)}
286 @Defn2{Term=[imported], Sec=(aspect of representation)}
287 @PDefn2{Term=[aspect of representation], Sec=(exported)}
288 @Defn2{Term=[exported], Sec=(aspect of representation)}
289 In addition, Import and Export @nt{pragma}s specify the
290 @i{imported} and @i{exported} aspects of representation,
291 respectively.
292
293 @PDefn2{Term=[program unit pragma], Sec=(Import)}
294 @PDefn2{Term=[pragma, program unit], Sec=(Import)}
295 @PDefn2{Term=[program unit pragma], Sec=(Export)}
296 @PDefn2{Term=[pragma, program unit], Sec=(Export)}
297 @PDefn2{Term=[program unit pragma], Sec=(Convention)}
298 @PDefn2{Term=[pragma, program unit], Sec=(Convention)}
299 An interfacing pragma is a program unit pragma
300 when applied to a program unit
301 (see @RefSecNum{Pragmas and Program Units}).
302
303 @Leading@;An interfacing pragma defines the convention
304 of the entity denoted by the @nt{local_name}.
305 The convention represents the calling convention or representation
306 convention of the entity.
307 For an access-to-subprogram type, it represents the calling
308 convention of designated subprograms.
309 In addition:
310 @begin{Itemize}
311 A @nt{pragma} Import specifies that the entity is defined externally (that is,
312 outside the Ada program).
313
314 A @nt{pragma} Export specifies that the entity is used externally.
315
316 A @nt{pragma} Import or Export optionally specifies an
317 entity's external name, link name, or both.
318 @end{Itemize}
319
320 @Defn{external name}
321 An @i{external name} is a string value for the
322 name used by a foreign language program either for an
323 entity that an Ada program imports, or for referring to
324 an entity that an Ada program exports.
325
326 @Defn{link name}
327 A @i[link name] is a string value for the name of an exported
328 or imported entity, based on the conventions of the
329 foreign language's compiler in interfacing with the
330 system's linker tool.
331
332 The meaning of link names is implementation defined.
333 If neither a link name nor
334 the Address attribute of an imported or exported entity is specified,
335 then a link name is chosen in an implementation-defined manner,
336 based on the external name if one is specified.
337 @ImplDef{The meaning of link names.}
338 @begin{Ramification}
339 For example,
340 an implementation might always prepend "_",
341 and then pass it to the system linker.
342 @end{Ramification}
343 @ImplDef{The manner of choosing link names when neither the link name
344 nor the address of an imported or exported entity is specified.}
345 @begin{Ramification}
346 Normally, this will be the entity's defining name,
347 or some simple transformation thereof.
348 @end{Ramification}
349
350 Pragma Linker_Options has the
351 effect of passing its string argument as a parameter to
352 the system linker (if one exists), if the immediately
353 enclosing compilation unit is included in the partition
354 being linked. The interpretation of the string argument, and the
355 way in which the string arguments from
356 multiple Linker_Options pragmas are combined, is implementation
357 defined.
358 @ImplDef(The effect of pragma Linker_Options.)
359 @end{StaticSem}
360
361 @begin{RunTime}
362 @PDefn2{Term=[elaboration], Sec=(declaration named by a @nt{pragma} Import)}
363 @Defn{notwithstanding}
364 Notwithstanding what this International Standard says elsewhere,
365 the elaboration of a declaration denoted by the
366 @nt{local_name} of
367 a @nt{pragma} Import does not create the entity.
368 Such an elaboration has no other effect than to allow the defining name
369 to denote the external entity.
370 @begin{Ramification}
371 This implies that default initializations are skipped.
372 (Explicit initializations are illegal.)
373 For example, an imported access object is @i{not}
374 initialized to @key[null].
375
376 Note that the @nt{local_name}
377 in a @nt{pragma} Import might denote
378 more than one declaration; in that case, the entity of all of those
379 declarations will be the external entity.
380 @end{Ramification}
381 @begin{Discussion}
382 This @lquotes@;notwithstanding@rquotes@; wording is better than saying
383 @lquotes@;unless named by a @nt{pragma} Import@rquotes@; on every definition of
384 elaboration.
385 It says we recognize the contradiction, and this rule takes
386 precedence.
387 @end{Discussion}
388 @end{RunTime}
389
390 @begin{Erron}
391 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00320-01]}
392 @ChgAdded{Version=[2],Text=[@PDefn2{Term=(erroneous execution),Sec=(cause)}
393 It is the programmer's responsibility to ensure that the use of interfacing
394 pragmas does not violate Ada semantics; otherwise, program execution is
395 erroneous.]}
396 @end{Erron}
397
398
399 @begin{ImplAdvice}
400 If an implementation supports pragma Export
401 to a given language, then it should also
402 allow the main subprogram to be written in that language.
403 It should support some mechanism for invoking the elaboration of the
404 Ada library units included in the system, and for invoking the
405 finalization of the environment task.
406 On typical systems, the recommended mechanism is to provide two
407 subprograms whose link names are "adainit" and "adafinal".
408 Adainit should contain the elaboration code for library units.
409 Adafinal should contain the finalization code.
410 These subprograms should have no effect the second and subsequent
411 time they are called.
412 @Chg{New=[@Defn{adainit}@Defn{adafinal}@Defn2{Term=[Elaboration],
413 Sec=[of library units for a foreign language main subprogram]}
414 @Defn2{Term=[Finalization],
415 Sec=[of environment task for a foreign language main subprogram]}],
416 Old=[]}@ChgNote{Presentation AI-00052. Index entries only; no
417 real change, so no Chgref}
418 @ChgImplAdvice{Version=[2],Kind=[Added],Text=[@ChgAdded{Version=[2],
419 Text=[If @nt{pragma} Export is supported for a language, the main program should
420 be able to be written in that language. Subprograms named "adainit" and
421 "adafinal"
422 should be provided for elaboration and finalization of the environment task.]}]}
423 @begin{ramification}
424 For example, if the main subprogram is written in C,
425 it can call adainit before the first call to an Ada subprogram,
426 and adafinal after the last.@end{ramification}
427
428 Automatic elaboration of preelaborated packages should be provided
429 when @nt[pragma] Export is supported.
430 @ChgImplAdvice{Version=[2],Kind=[Added],Text=[@ChgAdded{Version=[2],
431 Text=[Automatic elaboration of preelaborated packages should be provided
432 when @nt[pragma] Export is supported.]}]}
433
434 For each supported convention @i[L] other than Intrinsic,
435 an implementation should support Import and Export @nt{pragma}s for
436 objects of @i[L]-compatible types and for
437 subprograms, and @nt(pragma) Convention for @i[L]-eligible
438 types and for subprograms,
439 presuming the other language has corresponding features.
440 @nt(Pragma) Convention need not be supported for scalar types.
441 @ChgImplAdvice{Version=[2],Kind=[Added],Text=[@ChgAdded{Version=[2],
442 Text=[For each supported convention @i[L] other than Intrinsic,
443 @nt{pragma}s Import and Export should be supported for
444 objects of @i[L]-compatible types and for
445 subprograms, and @nt(pragma) Convention should be supported for @i[L]-eligible
446 types and for subprograms.]}]}
447 @begin{reason}
448 Pragma Convention is not necessary for scalar types,
449 since the language interface packages declare scalar types corresponding
450 to those provided by the respective foreign languages.
451 @end[reason]
452 @begin{ImplNote}
453 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
454 If an implementation supports interfacing to @Chg{Version=[2],New=[the ],
455 Old=[]}C++@Chg{Version=[2],New=[ entities not supported by
456 @RefSecNum{Interfacing with C and C++}],Old=[]},
457 it should do so via the convention identifier C_Plus_Plus
458 (in additional to any C++-implementation-specific ones).
459 @end{ImplNote}
460 @begin{Reason}
461 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
462 The reason for giving the advice about C++ is to encourage
463 uniformity among implementations, given that the name of the language is
464 not syntactically legal as an @nt{identifier}.@Chg{Version=[2],New=[],Old=[
465 We place this advice in the AARM, rather than the RM95 proper,
466 because (as of this writing) C++ is not an international standard,
467 and we don't want to refer to a such a language from an international
468 standard.]}
469 @end{Reason}
470 @end{ImplAdvice}
471
472 @begin{Notes}
473 Implementations may place restrictions on interfacing
474 pragmas;
475 for example, requiring each exported entity to be declared
476 at the library level.
477 @begin{TheProof}
478 Arbitrary restrictions are allowed by
479 @RefSecNum{Operational and Representation Items}.
480 @end{TheProof}
481 @begin{Ramification}
482 Such a restriction might be to disallow them altogether.
483 Alternatively, the implementation might allow them only for certain
484 kinds of entities,
485 or only for certain conventions.
486 @end{Ramification}
487
488 A @nt{pragma} Import specifies the conventions for accessing external
489 entities. It is possible that the actual entity is written in assembly
490 language, but reflects the conventions of a particular
491 language. For example, @key{pragma} Import(Ada, ...) can be used to
492 interface to an assembly language routine that obeys the
493 Ada compiler's calling conventions.
494
495 To obtain @lquotes@;call-back@rquotes@; to an Ada subprogram from a foreign language
496 environment, @key(pragma) Convention should be specified both for the
497 access-to-subprogram type and the specific subprogram(s) to which 'Access
498 is applied.
499
500 It is illegal to specify more than one of
501 Import, Export, or Convention for a given entity.
502
503 The @nt{local_name}
504 in an interfacing pragma can denote more than one
505 entity in the case of overloading.
506 Such a @nt{pragma} applies to all of the denoted entities.
507
508 See also @RefSec{Machine Code Insertions}.
509 @begin{Ramification}
510 The Intrinsic convention (see @refsecnum(Conformance Rules))
511 implies that the entity is somehow @lquotes@;built
512 in@rquotes@; to the implementation.
513 Thus, it generally does not make sense for users to specify Intrinsic
514 in a @nt{pragma} Import.
515 The intention is that only implementations will specify
516 Intrinsic in a @nt{pragma} Import.
517 The language also defines certain subprograms to be Intrinsic.
518 @end{Ramification}
519 @begin{Discussion}
520 There are many imaginable interfacing pragmas that don't make any
521 sense.
522 For example, setting the Convention of a protected procedure to Ada
523 is probably wrong.
524 Rather than enumerating all such cases, however,
525 we leave it up to implementations to decide what
526 is sensible.
527 @end{Discussion}
528
529 If both External_Name and Link_Name are specified for an Import
530 or Export pragma,
531 then the External_Name is ignored.
532
533 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00320-01]}
534 @ChgDeleted{Version=[2],Text=[An interfacing pragma might result in an effect
535 that violates Ada semantics.]}
536 @end{Notes}
537
538 @begin{Examples}
539 @leading@keepnext@i{Example of interfacing pragmas:}
540 @begin{Example}
541 @key[package] Fortran_Library @key[is]
542 @key[function] Sqrt (X : Float) @key[return] Float;
543 @key[function] Exp (X : Float) @key[return] Float;
544 @key[private]
545 @key[pragma] Import(Fortran, Sqrt);
546 @key[pragma] Import(Fortran, Exp);
547 @key[end] Fortran_Library;
548 @end{Example}
549 @end{Examples}
550
551 @begin{Extend83}
552 @Defn{extensions to Ada 83}
553 Interfacing pragmas are new to Ada 95.
554 Pragma Import replaces Ada 83's pragma Interface.
555 Existing implementations can continue to support pragma Interface for
556 upward compatibility.
557 @end{Extend83}
558
559 @begin{DiffWord95}
560 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0058],ARef=[AI95-00036-01]}
561 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified that @nt{pragma}s
562 Import and Export work like a subprogram call; parameters cannot be
563 omitted unless named notation is used. (Reordering is still not permitted,
564 however.)]}
565
566 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00320-01]}
567 @ChgAdded{Version=[2],Text=[Added wording to say all bets are off if
568 foreign code doesn't follow the semantics promised by the Ada
569 specifications.]}
570 @end{DiffWord95}
571
572
573 @LabeledClause{The Package Interfaces}
574 @begin{Intro}
575 Package Interfaces is the parent of several library
576 packages that declare types and other entities useful for
577 interfacing to foreign languages.
578 It also contains some implementation-defined
579 types that are useful across more than one language
580 (in particular for interfacing to assembly
581 language).
582 @ImplDef{The contents of the visible part of package Interfaces
583 and its language-defined descendants.}
584 @end{Intro}
585
586 @begin{StaticSem}
587 @Leading@Keepnext@;The library package Interfaces has the following skeletal declaration:
588 @begin{Example}
589 @RootLibUnit{Interfaces}
590 @key[package] Interfaces @key[is]
591 @key[pragma] Pure(Interfaces);
592
593 @key[type] Integer_@RI{n} @key[is] @key[range] -2**(@RI{n}-1) .. 2**(@RI{n}-1) - 1; --@RI{2's complement}
594
595 @key[type] Unsigned_@RI{n} @key[is] @key[mod] 2**@RI{n};
596
597 @key[function] Shift_Left (Value : Unsigned_@RI{n}; Amount : Natural)
598 @key[return] Unsigned_@RI{n};
599 @key[function] Shift_Right (Value : Unsigned_@RI{n}; Amount : Natural)
600 @key[return] Unsigned_@RI{n};
601 @key[function] Shift_Right_Arithmetic (Value : Unsigned_@RI{n}; Amount : Natural)
602 @key[return] Unsigned_@RI{n};
603 @key[function] Rotate_Left (Value : Unsigned_@RI{n}; Amount : Natural)
604 @key[return] Unsigned_@RI{n};
605 @key[function] Rotate_Right (Value : Unsigned_@RI{n}; Amount : Natural)
606 @key[return] Unsigned_@RI{n};
607 ...
608 @key[end] Interfaces;
609 @end{Example}
610 @end{StaticSem}
611
612 @begin{ImplReq}
613 @Leading@;An implementation shall provide the following declarations in the
614 visible part of package Interfaces:
615 @begin{Itemize}
616 Signed and modular integer types of @i{n} bits,
617 if supported by the target architecture,
618 for each @i{n} that is at least the size
619 of a storage element and that is a factor of the word size.
620 The names of these types are of the form Integer_@i{n} for the
621 signed types, and Unsigned_@i{n} for the modular types;
622 @begin{Ramification}
623 For example, for a typical 32-bit machine the corresponding
624 types might be Integer_8, Unsigned_8,
625 Integer_16, Unsigned_16,
626 Integer_32, and Unsigned_32.
627
628 The wording above implies, for example, that Integer_16'Size =
629 Unsigned_16'Size = 16.
630 Unchecked conversions between same-Sized types will work as
631 expected.
632 @end{Ramification}
633
634 @Defn{shift}@Defn{rotate}
635 For each such modular type in Interfaces,
636 shifting and rotating subprograms as specified in the declaration of
637 Interfaces above.
638 These subprograms are Intrinsic.
639 They operate on a bit-by-bit basis,
640 using the binary representation of the
641 value of the operands
642 to yield a binary representation for the result.
643 The Amount parameter gives the number of bits by which to shift
644 or rotate.
645 For shifting, zero bits are shifted in, except in the case of
646 Shift_Right_Arithmetic, where one bits are shifted in if Value is
647 at least half the modulus.
648 @begin{Reason}
649 We considered making shifting and rotating be primitive operations of
650 all modular types.
651 However, it is a design principle of Ada that all predefined operations
652 should be operators (not functions named by identifiers).
653 (Note that an early version of Ada had "@key[abs]" as an identifier,
654 but it was changed to a reserved word operator before standardization of
655 Ada 83.)
656 This is important because the implicit declarations would hide
657 non-overloadable declarations with the same name,
658 whereas operators are always overloadable.
659 Therefore, we would have had to make shift and rotate
660 into reserved words,
661 which would have been upward incompatible,
662 or else invent new operator symbols,
663 which seemed like too much mechanism.
664 @end{Reason}
665
666 Floating point types corresponding to each floating point format
667 fully supported by the hardware.
668 @begin{ImplNote}
669
670 The names for these floating point types are not specified.
671 @Defn{IEEE floating point arithmetic}
672 However, if IEEE arithmetic is supported, then the names
673 should be IEEE_Float_32 and IEEE_Float_64 for single and double
674 precision, respectively.
675
676 @end{ImplNote}
677 @end{Itemize}
678
679 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00204-01]}
680 @ChgAdded{Version=[2],Text=[Support for interfacing to any foreign language is
681 optional. However, an implementation shall not provide any attribute, library
682 unit, or pragma having the same name as an attribute, library unit, or pragma
683 (respectively) specified in the following clauses of this Annex unless the
684 provided construct is either as specified in those clauses or is more limited
685 in capability than that required by those clauses. A program that attempts to
686 use an unsupported capability of this Annex shall either be identified by the
687 implementation before run time or shall raise an exception at run time.]}
688 @begin{Discussion}
689 @ChgRef{Version=[2],Kind=[AddedNormal]}
690 @ChgAdded{Version=[2],Text=[The intent is that the same rules apply for
691 language interfacing as apply for Specialized Needs Annexes. See
692 @RefSecNum{Conformity of an Implementation with the Standard} for a
693 discussion of the purpose of these rules.]}
694 @end{Discussion}
695 @end{ImplReq}
696
697 @begin{ImplPerm}
698 An implementation may provide implementation-defined library units
699 that are children of Interfaces,
700 and may add declarations to the visible part of Interfaces
701 in addition to the ones defined above.
702 @ChgImplDef{Version=[2],Kind=[Revised],Text=[Implementation-defined
703 children of package Interfaces.@Chg{Version=[2],New=[],Old=[ The
704 contents of the visible part of package Interfaces.]}]}
705 @ChgNote{The latter sentence is given previously!}
706
707 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00204-01]}
708 @ChgAdded{Version=[2],Text=[A child package of package Interfaces with the name
709 of a convention may be provided independently of whether the convention is
710 supported by the pragma Convention and vice versa. Such a child package should
711 contain any declarations that would be useful for interfacing to the language
712 (implementation) represented by the convention. Any declarations useful for
713 interfacing to any language on the given hardware architecture should be
714 provided directly in Interfaces.]}
715
716 @begin{Ramification}
717 @ChgRef{Version=[2],Kind=[AddedNormal]}@ChgNote{Moved from below}
718 @ChgAdded{Version=[2],Text=[For example, package Interfaces.XYZ_Pascal might
719 contain declarations of types that match the data types provided by the XYZ
720 implementation of Pascal, so that it will be more convenient to pass
721 parameters to a subprogram whose convention is XYZ_Pascal.]}
722 @end{Ramification}
723
724 @end{ImplPerm}
725
726 @begin{ImplAdvice}
727 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00204-01]}
728 @ChgDeleted{Version=[2],Text=[For each implementation-defined convention
729 identifier, there should be a child package of
730 package Interfaces with the corresponding name.
731 This package should contain any declarations that would be useful for
732 interfacing to the language (implementation) represented by the convention.
733 Any declarations useful for interfacing to any language on the
734 given hardware architecture should be provided directly in Interfaces.]}
735 @begin{Ramification}
736 @ChgRef{Version=[2],Kind=[Deleted]}
737 @ChgDeleted{Version=[2],Text=[For example, package Interfaces.XYZ_Pascal might contain
738 declarations of types that match the data types provided by the
739 XYZ implementation of Pascal,
740 so that it will be more convenient to pass parameters to a subprogram
741 whose convention is XYZ_Pascal.]}
742 @end{Ramification}
743
744 An implementation supporting an interface to C, COBOL, or Fortran
745 should provide the corresponding
746 package or packages described in the following clauses.
747 @ChgImplAdvice{Version=[2],Kind=[Added],Text=[@ChgAdded{Version=[2],
748 Text=[If an interface to C, COBOL, or Fortran is provided, the corresponding
749 package or packages described in @RefSec{Interface to Other Languages}
750 should also be provided.]}]}
751 @begin{ImplNote}
752 @Leading@;The intention is that an implementation might support several
753 implementations of the foreign language: Interfaces.This_Fortran and
754 Interfaces.That_Fortran might both exist.
755 The @lquotes@;default@rquotes@; implementation, overridable by the user,
756 should be declared as a renaming:
757 @begin{Example}
758 @key[package] Interfaces.Fortran @key[renames] Interfaces.This_Fortran;
759 @end{Example}
760 @end{ImplNote}
761 @end{ImplAdvice}
762
763 @begin{DiffWord95}
764 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00204-01]}
765 @ChgAdded{Version=[2],Text=[Clarified that interfacing to foreign languages
766 is optional and has the same restrictions as a Specialized Needs Annex.]}
767 @end{DiffWord95}
768
769
770 @LabeledRevisedClause{Version=[2],New=[Interfacing with C and C++],
771 Old=[Interfacing with C]}
772 @begin{Intro}
773 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0059],ARef=[AI95-00131-01]}
774 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00376-01]}
775 @Defn{interface to C}
776 @Defn{C interface}
777 The facilities relevant to interfacing with
778 the C language @Chg{Version=[2],New=[and the corresponding subset of
779 the C++ language ],Old=[]}are the package Interfaces.C and its children;
780 @Chg{New=[],Old=[and ]}support for the Import, Export, and
781 Convention pragmas with @i{convention}_@nt{identifier}
782 C@Chg{New=[; and support for the Convention pragma with
783 @i{convention}_@nt{identifier} C_Pass_By_Copy],Old=[]}.
784
785 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00376-01]}
786 The package Interfaces.C contains the basic types, constants and
787 subprograms that allow an Ada program to pass scalars and strings to C
788 @Chg{Version=[2],New=[and C++ ],Old=[]}functions.@Chg{Version=[2],New=[ When
789 this clause mentions a C entity,
790 the reference also applies to the corresponding entity in C++.],Old=[]}
791 @end{Intro}
792
793 @begin{StaticSem}
794 @Leading@Keepnext@;The library package Interfaces.C has the following declaration:
795 @begin{Example}
796 @key(package) Interfaces.C @key(is)@ChildUnit{Parent=[Interfaces],Child=[C]}
797 @key(pragma) Pure(C);
798
799 @RI{-- Declarations based on C's <limits.h>}
800
801 @AdaObjDefn{CHAR_BIT} : @key(constant) := @RI{implementation-defined}; @RI{-- typically 8}
802 @AdaObjDefn{SCHAR_MIN} : @key(constant) := @RI{implementation-defined}; @RI{-- typically @en@;128}
803 @AdaObjDefn{SCHAR_MAX} : @key(constant) := @RI{implementation-defined}; @RI{-- typically 127}
804 @AdaObjDefn{UCHAR_MAX} : @key(constant) := @RI{implementation-defined}; @RI{-- typically 255}
805
806 @RI{-- Signed and Unsigned Integers}
807 @key(type) @AdaTypeDefn{int} @key(is) @key(range) @RI{implementation-defined};
808 @key(type) @AdaTypeDefn{short} @key(is) @key(range) @RI{implementation-defined};
809 @key(type) @AdaTypeDefn{long} @key(is) @key(range) @RI{implementation-defined};
810
811 @key(type) @AdaTypeDefn{signed_char} @key(is) @key(range) SCHAR_MIN .. SCHAR_MAX;
812 @key(for) signed_char'Size @key(use) CHAR_BIT;
813
814 @key(type) @AdaTypeDefn{unsigned} @key(is) @key(mod) @RI{implementation-defined};
815 @key(type) @AdaTypeDefn{unsigned_short} @key(is) @key(mod) @RI{implementation-defined};
816 @key(type) @AdaTypeDefn{unsigned_long} @key(is) @key(mod) @RI{implementation-defined};
817
818 @key(type) @AdaTypeDefn{unsigned_char} @key(is) @key(mod) (UCHAR_MAX+1);
819 @key(for) unsigned_char'Size @key(use) CHAR_BIT;
820
821 @key(subtype) @AdaTypeDefn{plain_char} @key(is) @RI{implementation-defined};
822
823 @key(type) @AdaTypeDefn{ptrdiff_t} @key(is) @key(range) @RI{implementation-defined};
824
825 @key(type) @AdaTypeDefn{size_t} @key(is) @key(mod) @RI{implementation-defined};
826
827 @RI{-- Floating Point}
828
829 @key(type) @AdaTypeDefn{C_float} @key(is) @key(digits) @RI{implementation-defined};
830
831 @key(type) @AdaTypeDefn{double} @key(is) @key(digits) @RI{implementation-defined};
832
833 @key(type) @AdaTypeDefn{long_double} @key(is) @key(digits) @RI{implementation-defined};
834
835
836 @RI{-- Characters and Strings }
837
838 @key(type) @AdaTypeDefn{char} @key(is) @RI{<implementation-defined character type>};
839
840 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0060],ARef=[AI95-00037-01]}
841 @AdaObjDefn{nul} : @key(constant) char := @Chg{New=[@RI{implementation-defined}],Old=[char'First]};
842
843 @key[function] @AdaSubDefn{To_C} (Item : @key[in] Character) @key[return] char;
844
845 @key[function] @AdaSubDefn{To_Ada} (Item : @key[in] char) @key[return] Character;
846
847 @key(type) @AdaTypeDefn{char_array} @key(is) @key(array) (size_t @key(range) <>) @key(of) @key[aliased] char;
848 @key[pragma] Pack(char_array);
849 @key(for) char_array'Component_Size @key(use) CHAR_BIT;
850
851 @key(function) @AdaSubDefn{Is_Nul_Terminated} (Item : @key(in) char_array) @key(return) Boolean;
852
853 @key(function) @AdaSubDefn{To_C} (Item : @key(in) String;
854 Append_Nul : @key(in) Boolean := True)
855 @key(return) char_array;
856
857 @key(function) @AdaSubDefn{To_Ada} (Item : @key(in) char_array;
858 Trim_Nul : @key(in) Boolean := True)
859 @key(return) String;
860
861 @key(procedure) @AdaSubDefn{To_C} (Item : @key(in) String;
862 Target : @key(out) char_array;
863 Count : @key(out) size_t;
864 Append_Nul : @key(in) Boolean := True);
865
866 @key(procedure) @AdaSubDefn{To_Ada} (Item : @key(in) char_array;
867 Target : @key(out) String;
868 Count : @key(out) Natural;
869 Trim_Nul : @key(in) Boolean := True);
870
871 @RI{-- Wide Character and Wide String}
872
873 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0060],ARef=[AI95-00037-01]}
874 @key(type) @AdaTypeDefn{wchar_t} @key(is) @Chg{New=[@RI{<implementation-defined character type>}],
875 Old=[@RI{implementation-defined}]};
876
877 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0060],ARef=[AI95-00037-01]}
878 @AdaObjDefn{wide_nul} : @key(constant) wchar_t := @Chg{New=[@RI{implementation-defined}],Old=[wchar_t'First]};
879
880 @key(function) @AdaSubDefn{To_C} (Item : @key(in) Wide_Character) @key(return) wchar_t;
881 @key(function) @AdaSubDefn{To_Ada} (Item : @key(in) wchar_t ) @key(return) Wide_Character;
882
883 @key(type) @AdaTypeDefn{wchar_array} @key(is) @key(array) (size_t @key(range) <>) @key(of) @key(aliased) wchar_t;
884
885 @key(pragma) Pack(wchar_array);
886
887 @key(function) @AdaSubDefn{Is_Nul_Terminated} (Item : @key(in) wchar_array) @key(return) Boolean;
888
889 @key(function) @AdaSubDefn{To_C} (Item : @key(in) Wide_String;
890 Append_Nul : @key(in) Boolean := True)
891 @key(return) wchar_array;
892
893 @key(function) @AdaSubDefn{To_Ada} (Item : @key(in) wchar_array;
894 Trim_Nul : @key(in) Boolean := True)
895 @key(return) Wide_String;
896
897 @key(procedure) @AdaSubDefn{To_C} (Item : @key(in) Wide_String;
898 Target : @key(out) wchar_array;
899 Count : @key(out) size_t;
900 Append_Nul : @key(in) Boolean := True);
901
902 @key(procedure) @AdaSubDefn{To_Ada} (Item : @key(in) wchar_array;
903 Target : @key(out) Wide_String;
904 Count : @key(out) Natural;
905 Trim_Nul : @key(in) Boolean := True);
906
907 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00285-01]}
908 @ChgAdded{Version=[2],Text=[ -- @RI[ISO/IEC 10646:2003 compatible types defined by ISO/IEC TR 19769:2004.]]}
909
910 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00285-01]}
911 @ChgAdded{Version=[2],Text=[ @key<type> @AdaTypeDefn{char16_t} @key<is> @RI{<implementation-defined character type>};]}
912
913 @ChgRef{Version=[2],Kind=[Added]}
914 @ChgAdded{Version=[2],Text=[ @AdaObjDefn{char16_nul} : @key<constant> char16_t := @RI{implementation-defined};]}
915
916 @ChgRef{Version=[2],Kind=[Added]}
917 @ChgAdded{Version=[2],Text=[ @key<function> @AdaSubDefn{To_C} (Item : @key<in> Wide_Character) @key<return> char16_t;
918 @key<function> @AdaSubDefn{To_Ada} (Item : @key<in> char16_t) @key<return> Wide_Character;]}
919
920 @ChgRef{Version=[2],Kind=[Added]}
921 @ChgAdded{Version=[2],Text=[ @key<type> @AdaTypeDefn{char16_array} @key<is array> (size_t @key<range> <>) @key<of aliased> char16_t;]}
922
923 @ChgRef{Version=[2],Kind=[Added]}
924 @ChgAdded{Version=[2],Text=[ @key<pragma> Pack(char16_array);]}
925
926 @ChgRef{Version=[2],Kind=[Added]}
927 @ChgAdded{Version=[2],Text=[ @key<function> @AdaSubDefn{Is_Nul_Terminated} (Item : @key<in> char16_array) @key<return> Boolean;
928 @key<function> @AdaSubDefn{To_C} (Item : @key<in> Wide_String;
929 Append_Nul : @key<in> Boolean := True)
930 @key<return> char16_array;]}
931
932 @ChgRef{Version=[2],Kind=[Added]}
933 @ChgAdded{Version=[2],Text=[ @key<function> @AdaSubDefn{To_Ada} (Item : @key<in> char16_array;
934 Trim_Nul : @key<in> Boolean := True)
935 @key<return> Wide_String;]}
936
937 @ChgRef{Version=[2],Kind=[Added]}
938 @ChgAdded{Version=[2],Text=[ @key<procedure> @AdaSubDefn{To_C} (Item : @key<in> Wide_String;
939 Target : @key<out> char16_array;
940 Count : @key<out> size_t;
941 Append_Nul : @key<in> Boolean := True);]}
942
943 @ChgRef{Version=[2],Kind=[Added]}
944 @ChgAdded{Version=[2],Text=[ @key<procedure> @AdaSubDefn{To_Ada} (Item : @key<in> char16_array;
945 Target : @key<out> Wide_String;
946 Count : @key<out> Natural;
947 Trim_Nul : @key<in> Boolean := True);]}
948
949 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00285-01]}
950 @ChgAdded{Version=[2],Text=[ @key<type> @AdaTypeDefn{char32_t} @key<is> @RI{<implementation-defined character type>};]}
951
952 @ChgRef{Version=[2],Kind=[Added]}
953 @ChgAdded{Version=[2],Text=[ @AdaObjDefn{char32_nul} : @key<constant> char32_t := @RI{implementation-defined};]}
954
955 @ChgRef{Version=[2],Kind=[Added]}
956 @ChgAdded{Version=[2],Text=[ @key<function> @AdaSubDefn{To_C} (Item : @key<in> Wide_Wide_Character) @key<return> char32_t;
957 @key<function> @AdaSubDefn{To_Ada} (Item : @key<in> char32_t) @key<return> Wide_Wide_Character;]}
958
959 @ChgRef{Version=[2],Kind=[Added]}
960 @ChgAdded{Version=[2],Text=[ @key<type> @AdaTypeDefn{char32_array} @key<is array> (size_t @key<range> <>) @key<of aliased> char32_t;]}
961
962 @ChgRef{Version=[2],Kind=[Added]}
963 @ChgAdded{Version=[2],Text=[ @key<pragma> Pack(char32_array);]}
964
965 @ChgRef{Version=[2],Kind=[Added]}
966 @ChgAdded{Version=[2],Text=[ @key<function> @AdaSubDefn{Is_Nul_Terminated} (Item : @key<in> char32_array) @key<return> Boolean;
967 @key<function> @AdaSubDefn{To_C} (Item : @key<in> Wide_Wide_String;
968 Append_Nul : @key<in> Boolean := True)
969 @key<return> char32_array;]}
970
971 @ChgRef{Version=[2],Kind=[Added]}
972 @ChgAdded{Version=[2],Text=[ @key<function> @AdaSubDefn{To_Ada} (Item : @key<in> char32_array;
973 Trim_Nul : @key<in> Boolean := True)
974 @key<return> Wide_Wide_String;]}
975
976 @ChgRef{Version=[2],Kind=[Added]}
977 @ChgAdded{Version=[2],Text=[ @key<procedure> @AdaSubDefn{To_C} (Item : @key<in> Wide_Wide_String;
978 Target : @key<out> char32_array;
979 Count : @key<out> size_t;
980 Append_Nul : @key<in> Boolean := True);]}
981
982 @ChgRef{Version=[2],Kind=[Added]}
983 @ChgAdded{Version=[2],Text=[ @key<procedure> @AdaSubDefn{To_Ada} (Item : @key<in> char32_array;
984 Target : @key<out> Wide_Wide_String;
985 Count : @key<out> Natural;
986 Trim_Nul : @key<in> Boolean := True);]}
987
988 @AdaExcDefn{Terminator_Error} : @key(exception);
989
990 @key(end) Interfaces.C;
991 @end{Example}
992 @ChgImplDef{Version=[2],Kind=[Added],Text=[@Chg{New=[The definitions of
993 @Chg{Version=[2],New=[certain ],Old=[]}types and constants in
994 Interfaces.C.],Old=[]}]}
995
996 Each of the types declared in Interfaces.C is C-compatible.
997
998 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00285-01]}
999 The types int, short, long, unsigned,
1000 ptrdiff_t, size_t, double,
1001 char, @Chg{Version=[2],New=[],Old=[and ]}wchar_t@Chg{Version=[2],New=[,
1002 char16_t, and char32_t],Old=[]}
1003 correspond respectively to the C types having the same names.
1004 The types signed_char, unsigned_@!short, unsigned_@!long, unsigned_@!char,
1005 C_float, and long_@!double correspond respectively
1006 to the C types signed char,
1007 unsigned short, unsigned long, unsigned char, float, and long double.
1008
1009 @begin{Discussion}
1010 @ChgRef{Version=[2],Kind=[AddedNormal]}
1011 @ChgAdded{Version=[2],Text=[The C types wchar_t and char16_t seem to be the
1012 same. However, wchar_t has an implementation-defined size, whereas
1013 char16_t is guaranteed to be an unsigned type of at least 16
1014 bits. Also, char16_t and char32_t are encouraged to have UTF-16 and UTF-32
1015 representations; that means that they are not directly the same as the Ada
1016 types, which most likely don't use any UTF encoding.]}
1017 @end{Discussion}
1018
1019 @Trailing@;The type of the subtype plain_char is either signed_char or
1020 unsigned_char, depending on the C implementation.
1021 @begin{DescribeCode}
1022 @begin{Example}@Keepnext
1023 @key(function) To_C (Item : @key(in) Character) @key(return) char;
1024 @key(function) To_Ada (Item : @key(in) char ) @key(return) Character;
1025 @end{Example}
1026 @Trailing@;The functions To_C and To_Ada map between the Ada type Character
1027 and the C type char.
1028
1029 @begin{ImplNote}
1030 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0114],ARef=[AI95-00038-01]}
1031 @ChgAdded{Version=[1],Text=[The To_C and To_Ada functions map between
1032 corresponding characters, not necessarily between characters with the same
1033 internal representation. Corresponding characters are characters defined by the
1034 same enumeration literal, if such exist; otherwise, the correspondence
1035 is unspecified.@PDefn{Unspecified}]}
1036
1037 @ChgRef{Version=[1],Kind=[Added]}
1038 @ChgAdded{Version=[1],Type=[Leading],Text=[The following definition is
1039 equivalent to the above summary:]}
1040
1041 @ChgRef{Version=[1],Kind=[Added]}
1042 @ChgAdded{Version=[1],Text=[@f{To_C (Latin_1_Char) = char'Value(Character'Image(Latin_1_Char))}@*
1043 provided that char'Value does not raise an exception; otherwise the result
1044 is unspecified.]}
1045
1046 @ChgRef{Version=[1],Kind=[Added]}
1047 @ChgAdded{Version=[1],Text=[@f{To_Ada (Native_C_Char) = Character'Value(char'Image(Native_C_Char))}@*
1048 provided that Character'Value does not raise an exception;
1049 otherwise the result is unspecified.]}
1050 @end{ImplNote}
1051
1052 @begin{Example}@Keepnext
1053 @key(function) Is_Nul_Terminated (Item : @key(in) char_array) @key(return) Boolean;
1054 @end{Example}
1055 @Trailing@;The result of Is_Nul_Terminated is True if Item contains nul, and is
1056 False otherwise.
1057
1058 @begin{Example}@Keepnext
1059 @key(function) To_C (Item : @key(in) String; Append_Nul : @key(in) Boolean := True)
1060 @key(return) char_array;
1061 @Comment{Blank line}
1062 @key(function) To_Ada (Item : @key(in) char_array; Trim_Nul : @key(in) Boolean := True)
1063 @key(return) String;
1064 @end{Example}
1065
1066 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00258-01]}
1067 The result of To_C is a char_array value of length Item'Length (if
1068 Append_Nul is False) or Item'Length+1 (if Append_Nul is True).
1069 The lower bound is 0.
1070 For each component Item(I), the corresponding component in the result
1071 is To_C applied to Item(I).
1072 The value nul is appended if Append_Nul is True.@Chg{Version=[2],New=[ If
1073 Append_Nul is False and Item'Length is 0, then To_C propagates
1074 Constraint_Error.],Old=[]}
1075
1076 @Trailing@;The result of To_Ada is a String whose length is Item'Length (if Trim_Nul is
1077 False) or the length of the slice of Item preceding the first
1078 nul (if Trim_Nul is True). The lower bound of the result is 1.
1079 If Trim_Nul is False, then for each component Item(I)
1080 the corresponding component in the result
1081 is To_Ada applied to Item(I).
1082 If Trim_Nul is True, then for each component Item(I) before
1083 the first nul the corresponding component in the result
1084 is To_Ada applied to Item(I).
1085 The function propagates Terminator_Error if Trim_Nul is True and
1086 Item does not contain nul.
1087
1088 @begin{Example}@Keepnext
1089 @key(procedure) To_C (Item : @key(in) String;
1090 Target : @key(out) char_array;
1091 Count : @key(out) size_t;
1092 Append_Nul : @key(in) Boolean := True);
1093 @Comment{Blank line}
1094 @key(procedure) To_Ada (Item : @key(in) char_array;
1095 Target : @key(out) String;
1096 Count : @key(out) Natural;
1097 Trim_Nul : @key(in) Boolean := True);
1098 @end{Example}
1099 For procedure To_C, each element of Item is converted (via the To_C function)
1100 to a char, which is assigned to the corresponding element
1101 of Target. If Append_Nul is True, nul
1102 is then assigned to the next
1103 element of Target. In either case, Count is set to the
1104 number of Target elements assigned.
1105 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
1106 If Target is not long enough, Constraint_Error is propagated.
1107
1108 @Trailing@;For procedure To_Ada, each element of Item (if Trim_Nul is False) or
1109 each element of Item preceding the first nul (if Trim_Nul is True) is
1110 converted (via the To_Ada function) to a Character, which is
1111 assigned to the corresponding element of Target. Count
1112 is set to the number of Target elements assigned.
1113 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
1114 If Target is not long enough, Constraint_Error is propagated.
1115 If Trim_Nul is True and Item does not contain nul,
1116 then Terminator_Error is propagated.
1117
1118 @begin{Example}@Keepnext
1119 @key(function) Is_Nul_Terminated (Item : @key(in) wchar_array) @key(return) Boolean;
1120 @end{Example}
1121 @Trailing@;The result of Is_Nul_Terminated is True if Item contains wide_nul,
1122 and is False otherwise.
1123
1124 @begin{Example}@Keepnext
1125 @key(function) To_C (Item : @key(in) Wide_Character) @key(return) wchar_t;
1126 @key(function) To_Ada (Item : @key(in) wchar_t ) @key(return) Wide_Character;
1127 @end{Example}
1128 @Trailing@;To_C and To_Ada provide the mappings between the Ada and C wide
1129 character types.
1130
1131 @begin{Example}
1132 @key(function) To_C (Item : @key(in) Wide_String;
1133 Append_Nul : @key(in) Boolean := True)
1134 @key(return) wchar_array;
1135 @Comment{Blank line}
1136 @key(function) To_Ada (Item : @key(in) wchar_array;
1137 Trim_Nul : @key(in) Boolean := True)
1138 @key(return) Wide_String;
1139 @Comment{Blank line}
1140 @key(procedure) To_C (Item : @key(in) Wide_String;
1141 Target : @key(out) wchar_array;
1142 Count : @key(out) size_t;
1143 Append_Nul : @key(in) Boolean := True);
1144 @Comment{Blank line}
1145 @key(procedure) To_Ada (Item : @key(in) wchar_array;
1146 Target : @key(out) Wide_String;
1147 Count : @key(out) Natural;
1148 Trim_Nul : @key(in) Boolean := True);
1149 @end{Example}
1150 The To_C and To_Ada subprograms that convert between Wide_String and
1151 wchar_array have analogous effects to the To_C and To_Ada
1152 subprograms that convert between String and char_array, except that
1153 wide_nul is used instead of nul.
1154
1155 @begin{Example}
1156 @ChgRef{Version=[2],Kind=[Added]}
1157 @ChgAdded{Version=[2],KeepNext=[T],Text=[@key<function> Is_Nul_Terminated (Item : @key<in> char16_array) @key<return> Boolean;]}
1158 @end{Example}
1159
1160 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00285-01]}
1161 @ChgAdded{Version=[2],Type=[Trailing],Text=[The result of Is_Nul_Terminated is True if Item contains char16_nul,
1162 and is False otherwise.]}
1163
1164 @begin{Example}
1165 @ChgRef{Version=[2],Kind=[Added]}
1166 @ChgAdded{Version=[2],KeepNext=[T],Text=[@key<function> To_C (Item : @key<in> Wide_Character) @key<return> char16_t;
1167 @key<function> To_Ada (Item : @key<in> char16_t ) @key<return> Wide_Character;]}
1168 @end{Example}
1169
1170 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00285-01]}
1171 @ChgAdded{Version=[2],Type=[Trailing],Text=[To_C and To_Ada provide mappings
1172 between the Ada and C 16-bit character types.]}
1173
1174 @begin{Example}
1175 @ChgRef{Version=[2],Kind=[Added]}
1176 @ChgAdded{Version=[2],Text=[@key<function> To_C (Item : @key<in> Wide_String;
1177 Append_Nul : @key<in> Boolean := True)
1178 @key<return> char16_array;
1179 @Comment{Blank line}
1180 @key<function> To_Ada (Item : @key<in> char16_array;
1181 Trim_Nul : @key<in> Boolean := True)
1182 @key<return> Wide_String;
1183 @Comment{Blank line}
1184 @key<procedure> To_C (Item : @key<in> Wide_String;
1185 Target : @key<out> char16_array;
1186 Count : @key<out> size_t;
1187 Append_Nul : @key<in> Boolean := True);
1188 @Comment{Blank line}
1189 @key<procedure> To_Ada (Item : @key<in> char16_array;
1190 Target : @key<out> Wide_String;
1191 Count : @key<out> Natural;
1192 Trim_Nul : @key<in> Boolean := True);]}
1193 @end{Example}
1194
1195 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00285-01]}
1196 @ChgAdded{Version=[2],Type=[Trailing],Text=[The To_C and To_Ada subprograms that
1197 convert between Wide_String and char16_array have analogous effects to the To_C
1198 and To_Ada subprograms that convert between String and char_array, except that
1199 char16_nul is used instead of nul.]}
1200
1201 @begin{Example}
1202 @ChgRef{Version=[2],Kind=[Added]}
1203 @ChgAdded{Version=[2],KeepNext=[T],Text=[@key<function> Is_Nul_Terminated (Item : @key<in> char32_array) @key<return> Boolean;]}
1204 @end{Example}
1205
1206 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00285-01]}
1207 @ChgAdded{Version=[2],Type=[Trailing],Text=[The result of Is_Nul_Terminated is
1208 True if Item contains char16_nul, and is False otherwise.]}
1209
1210 @begin{Example}
1211 @ChgRef{Version=[2],Kind=[Added]}
1212 @ChgAdded{Version=[2],KeepNext=[T],Text=[@key<function> To_C (Item : @key<in> Wide_Wide_Character) @key<return> char32_t;
1213 @key<function> To_Ada (Item : @key<in> char32_t ) @key<return> Wide_Wide_Character;]}
1214 @end{Example}
1215
1216 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00285-01]}
1217 @ChgAdded{Version=[2],Type=[Trailing],Text=[To_C and To_Ada provide mappings
1218 between the Ada and C 32-bit character types.]}
1219
1220 @begin{Example}
1221 @ChgRef{Version=[2],Kind=[Added]}
1222 @ChgAdded{Version=[2],Text=[@key<function> To_C (Item : @key<in> Wide_Wide_String;
1223 Append_Nul : @key<in> Boolean := True)
1224 @key<return> char32_array;
1225 @Comment{Blank line}
1226 @key<function> To_Ada (Item : @key<in> char32_array;
1227 Trim_Nul : @key<in> Boolean := True)
1228 @key<return> Wide_Wide_String;
1229 @Comment{Blank line}
1230 @key<procedure> To_C (Item : @key<in> Wide_Wide_String;
1231 Target : @key<out> char32_array;
1232 Count : @key<out> size_t;
1233 Append_Nul : @key<in> Boolean := True);
1234 @Comment{Blank line}
1235 @key<procedure> To_Ada (Item : @key<in> char32_array;
1236 Target : @key<out> Wide_Wide_String;
1237 Count : @key<out> Natural;
1238 Trim_Nul : @key<in> Boolean := True);]}
1239 @end{Example}
1240
1241 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00285-01]}
1242 @ChgAdded{Version=[2],Type=[Trailing],Text=[The To_C and To_Ada subprograms
1243 that convert between Wide_Wide_String and char32_array have analogous effects
1244 to the To_C and To_Ada subprograms that convert between String and char_array,
1245 except that char32_nul is used instead of nul.]}
1246
1247 @begin{Discussion}
1248 The Interfaces.C package provides an implementation-defined character type,
1249 char, designed to model the C run-time character set, and mappings
1250 between the types char and Character.
1251
1252 @Leading@;One application of the C interface package is
1253 to compose a C string and pass it to a C function.
1254 One way to do this is for the programmer to declare an
1255 object that will hold the C array, and then pass this array to the C
1256 function. This is realized via the type char_array:
1257 @begin{Example}
1258 @key(type) char_array @key(is) @key(array) (size_t @key(range) <>) of Char;
1259 @end{Example}
1260
1261 The programmer can declare an Ada String, convert it to a char_array, and
1262 pass the char_array as actual parameter to the C function that is expecting
1263 a char *.
1264
1265 An alternative approach is for the programmer to obtain a C char pointer
1266 from an Ada String (or from a char_array) by invoking an allocation
1267 function. The package Interfaces.C.Strings (see below) supplies
1268 the needed facilities, including a
1269 private type chars_ptr that corresponds to C's
1270 char *, and two allocation functions. To avoid storage
1271 leakage, a Free procedure releases the storage that was
1272 allocated by one of these allocate functions.
1273
1274 It is typical for a C function that deals with strings to adopt the
1275 convention that the string is delimited by a nul char. The C interface
1276 packages support this convention. A constant nul of type Char is declared,
1277 and the function Value(Chars_Ptr) in Interfaces.C.Strings
1278 returns a char_array up to and including
1279 the first nul in the array that the chars_ptr points to. The Allocate_Chars
1280 function allocates an array that is nul terminated.
1281
1282 Some C functions that deal with strings take an explicit length as a
1283 parameter, thus allowing strings to be passed that contain nul as
1284 a data element. Other C functions take an explicit length that is
1285 an upper bound: the prefix of the string up to the char before nul,
1286 or the prefix of the given length, is used by the
1287 function, whichever is shorter.
1288 The C Interface packages support calling such functions.
1289 @end{Discussion}
1290 @end{DescribeCode}
1291
1292 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0059],ARef=[AI95-00131-01]}
1293 @ChgAdded{Version=[1],Text=[A Convention pragma with @i{convention}_@nt{identifier}
1294 C_Pass_By_Copy shall only be applied to a type.]}
1295
1296 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0059],ARef=[AI95-00131-01]}
1297 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00216-01]}
1298 @ChgAdded{Version=[1],Text=[The eligibility rules in @RefSecNum(Interfacing Pragmas) do not apply
1299 to convention C_Pass_By_Copy. Instead, a type T is eligible for convention
1300 C_Pass_By_Copy @Chg{Version=[2],New=[if T is an unchecked union type or ],
1301 Old=[]}if T is a record type that has no discriminants and that only
1302 has components with statically constrained subtypes, and each component is
1303 C-compatible.]}
1304
1305 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0059],ARef=[AI95-00131-01]}
1306 @ChgAdded{Version=[1],Text=[If a type is C_Pass_By_Copy-compatible then it is
1307 also C-compatible.]}
1308
1309 @end{StaticSem}
1310
1311 @begin{ImplReq}
1312 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0059],ARef=[AI95-00131-01]}
1313 An implementation shall support pragma Convention
1314 with a C @i{convention}_@nt{identifier} for a
1315 C-eligible type (see @refsecnum(Interfacing Pragmas))@Chg{New=[. An
1316 implementation shall support pragma Convention with a C_Pass_By_Copy
1317 @i{convention}_@nt{identifier} for a C_Pass_By_Copy-eligible type.],Old=[]}
1318 @end{ImplReq}
1319
1320 @begin{ImplPerm}
1321 An implementation may provide additional declarations in the C
1322 interface packages.
1323 @end{ImplPerm}
1324
1325 @begin{ImplAdvice}
1326 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0060],ARef=[AI95-00037-01]}
1327 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00285-01]}
1328 @ChgAdded{Version=[1],Text=[The constants nul@Chg{Version=[2],New=[,],Old=[ and]}
1329 wide_nul@Chg{Version=[2],New=[, char16_nul, and char32_nul],Old=[]} should
1330 have a representation of zero.]}
1331 @ChgImplAdvice{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],
1332 Text=[The constants nul, wide_nul, char16_nul, and char32_nul in package
1333 Interfaces.C should have a representation of zero.]}]}
1334
1335 An implementation should support the following interface
1336 correspondences between Ada and C.
1337 @begin[itemize]
1338 An Ada procedure corresponds to
1339 a void-returning C function.
1340 @begin{discussion}
1341 The programmer can also choose an Ada procedure when
1342 the C function returns an int that is to be discarded.@end{discussion}
1343
1344 An Ada function corresponds to a non-void C function.
1345
1346 An Ada @key[in] scalar parameter is passed as a scalar argument to a C function.
1347
1348 An Ada @key[in] parameter of an access-to-object type with designated
1349 type T is passed as a t* argument to a C function, where t is the C type
1350 corresponding to the Ada type T.
1351
1352 An Ada @key[access] T parameter,
1353 or an Ada @key[out] or @key[in out] parameter of an elementary type T,
1354 is passed as a t* argument
1355 to a C function, where t is the C type corresponding to the
1356 Ada type T. In the case of an elementary @key[out] or @key[in out]
1357 parameter, a pointer to a temporary copy is used to preserve
1358 by-copy semantics.
1359
1360 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0059],ARef=[AI95-00131-01]}
1361 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00343-01]}
1362 @ChgAdded{Version=[1],Text=[An Ada parameter of a @Chg{Version=[2],
1363 New=[(record) type T of convention ],Old=[]}C_Pass_By_Copy@Chg{Version=[2],
1364 New=[],Old=[-compatible (record) type T]}, of
1365 mode @key{in}, is passed as a t argument to a C function, where t is the
1366 C struct corresponding to the Ada type T.]}
1367
1368 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0059],ARef=[AI95-00131-01]}
1369 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00343-01]}
1370 An Ada parameter of a record type T, of any mode,
1371 @Chg{New=[other than an @key{in} parameter of a @Chg{Version=[2],
1372 New=[type of convention ],Old=[]}C_Pass_By_Copy@Chg{Version=[2],
1373 New=[],Old=[-compatible type]},],Old=[]}
1374 is passed as a t* argument to a C function, where t is the
1375 C struct corresponding to the Ada type T.
1376
1377 An Ada parameter of an array type with component type
1378 T, of any mode, is passed as a t* argument to a
1379 C function, where t is the C type corresponding to the
1380 Ada type T.
1381
1382 An Ada parameter of an access-to-subprogram type
1383 is passed as a pointer to a
1384 C function whose prototype corresponds to the designated subprogram's
1385 specification.
1386 @end[itemize]
1387
1388 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00337-01]}
1389 @ChgAdded{Version=[2],Text=[An Ada parameter of a private type is passed
1390 as specified for the full view of the type.]}
1391
1392 @ChgImplAdvice{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],
1393 Text=[If C interfacing is supported, the interface correspondences between Ada
1394 and C should be supported.]}]}
1395 @end{ImplAdvice}
1396
1397 @begin{Notes}
1398 Values of type char_array are not implicitly terminated with nul.
1399 If a char_array is to be passed as a parameter to an imported
1400 C function requiring nul termination, it is the programmer's
1401 responsibility to obtain this effect.
1402
1403 To obtain the effect of C's sizeof(item_type),
1404 where Item_Type is the corresponding Ada type,
1405 evaluate the expression: size_t(Item_Type'Size/CHAR_BIT).
1406
1407 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00216-01]}
1408 @ChgDeleted{Version=[2],Text=[There is no explicit support for C's union types.
1409 Unchecked conversions can be used to obtain
1410 the effect of C unions.]}
1411
1412 A C function that takes a variable number of arguments
1413 can correspond to several Ada subprograms, taking various
1414 specific numbers and types of parameters.
1415 @end{Notes}
1416
1417 @begin{Examples}
1418 @Leading@Keepnext@i{Example of using the Interfaces.C package:}
1419 @begin{Example}
1420 @RI{--Calling the C Library Function strcpy}
1421 @key(with) Interfaces.C;
1422 @key(procedure) Test @key(is)
1423 @key(package) C @key(renames) Interfaces.C;
1424 @key(use) @key(type) C.char_array;
1425 @RI{-- Call <string.h>strcpy:}
1426 @RI{-- C definition of strcpy: char *strcpy(char *s1, const char *s2);}
1427 @RI{-- This function copies the string pointed to by s2 (including the terminating null character)}
1428 @RI{-- into the array pointed to by s1. If copying takes place between objects that overlap, }
1429 @RI{-- the behavior is undefined. The strcpy function returns the value of s1.}
1430
1431 @RI{-- Note: since the C function's return value is of no interest, the Ada interface is a procedure}
1432 @key(procedure) Strcpy (Target : @key(out) C.char_array;
1433 Source : @key(in) C.char_array);
1434
1435 @key(pragma) Import(C, Strcpy, "strcpy");
1436
1437 Chars1 : C.char_array(1..20);
1438 Chars2 : C.char_array(1..20);
1439
1440 @key(begin)
1441 Chars2(1..6) := "qwert" & C.nul;
1442
1443 Strcpy(Chars1, Chars2);
1444
1445 @RI{-- Now Chars1(1..6) = "qwert" & C.Nul}
1446
1447 @key(end) Test;
1448 @end{Example}
1449 @end{Examples}
1450
1451 @begin{Incompatible95}
1452 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00285-01]}
1453 @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95}
1454 Types char16_t and char32_t and their related
1455 types and operations are newly added to Interfaces.C. If Interfaces.C is
1456 referenced in a @nt{use_clause}, and an entity @i<E> with the same
1457 @nt{defining_identifier} as a new entity in Interfaces.C is defined in a
1458 package that is also referenced in a @nt{use_clause}, the entity @i<E> may no
1459 longer be use-visible, resulting in errors. This should be rare and is easily
1460 fixed if it does occur.]}
1461 @end{Incompatible95}
1462
1463 @begin{Extend95}
1464 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0059],ARef=[AI95-00131-01]}
1465 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1466 @b<Corrigendum:> Convention C_Pass_By_Copy is new.]}
1467 @end{Extend95}
1468
1469 @begin{DiffWord95}
1470 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0060],ARef=[AI95-00037-01]}
1471 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified the intent for
1472 Nul and Wide_Nul.]}
1473
1474 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
1475 @ChgAdded{Version=[2],Text=[Specified that an unchecked union type (see
1476 @RefSecNum{Pragma Unchecked_Union}) is eligible for convention
1477 C_Pass_By_Copy.]}
1478
1479 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00258-01]}
1480 @ChgAdded{Version=[2],Text=[Specified what happens if the To_C function
1481 tries to return a null string.]}
1482
1483 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00337-01]}
1484 @ChgAdded{Version=[2],Text=[Clarified that the interface correspondences
1485 also apply to private types whose full types have the specified
1486 characteristics.]}
1487
1488 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00343-01]}
1489 @ChgAdded{Version=[2],Text=[Clarified that a type must have convention
1490 C_Pass_By_Copy in order to be passed by copy (not just a type that could
1491 have that convention).]}
1492
1493 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00376-01]}
1494 @ChgAdded{Version=[2],Text=[Added wording to make it clear that these
1495 facilities can also be used with C++.]}
1496 @end{DiffWord95}
1497
1498
1499
1500 @LabeledSubClause{The Package Interfaces.C.Strings}
1501
1502 @begin{Intro}
1503 The package Interfaces.C.Strings declares types and subprograms
1504 allowing an Ada program to allocate, reference, update, and free C-style
1505 strings.
1506 In particular, the private type chars_ptr
1507 corresponds to a common
1508 use of @lquotes@;char *@rquotes@; in C programs, and an object of this type can be
1509 passed to a subprogram to which @nt(pragma) Import(C,...) has been applied,
1510 and for which @lquotes@;char *@rquotes@;
1511 is the type of the argument of the C function.
1512 @end{Intro}
1513
1514 @begin{StaticSem}
1515 @Leading@;The library package Interfaces.C.Strings has the following
1516 declaration:
1517 @begin{example}
1518 @key(package) Interfaces.C.Strings @key(is)@ChildUnit{Parent=[Interfaces.C],Child=[Strings]}
1519 @key[pragma] Preelaborate(Strings);
1520
1521 @key(type) @AdaTypeDefn{char_array_access} @key(is) @key(access) @key(all) char_array;
1522
1523 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00161-01]}
1524 @key(type) @AdaTypeDefn{chars_ptr} @key(is) @key(private);@Chg{Version=[2],New=[
1525 @key(pragma) Preelaborable_Initialization(chars_ptr);],Old=[]}
1526
1527 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00276-01]}
1528 @key(type) @AdaTypeDefn{chars_ptr_array} @key(is) @key(array) (size_t @key(range) <>) @key(of) @Chg{Version=[2],New=[@key(aliased) ],Old=[]}chars_ptr;
1529
1530 @AdaObjDefn{Null_Ptr} : @key(constant) chars_ptr;
1531
1532 @key(function) @AdaSubDefn{To_Chars_Ptr} (Item : @key(in) char_array_access;
1533 Nul_Check : @key(in) Boolean := False)
1534 @key(return) chars_ptr;
1535
1536 @key(function) @AdaSubDefn{New_Char_Array} (Chars : @key(in) char_array) @key(return) chars_ptr;
1537
1538 @key(function) @AdaSubDefn{New_String} (Str : @key(in) String) @key(return) chars_ptr;
1539
1540 @key(procedure) @AdaSubDefn{Free} (Item : @key(in) @key(out) chars_ptr);
1541
1542 @AdaSubDefn{Dereference_Error} : @key(exception);
1543
1544
1545 @key(function) @AdaSubDefn{Value} (Item : @key(in) chars_ptr) @key(return) char_array;
1546
1547 @key(function) @AdaSubDefn{Value} (Item : @key(in) chars_ptr; Length : @key(in) size_t)
1548 @key(return) char_array;
1549
1550 @key(function) @AdaSubDefn{Value} (Item : @key(in) chars_ptr) @key(return) String;
1551
1552 @key(function) @AdaSubDefn{Value} (Item : @key(in) chars_ptr; Length : @key(in) size_t)
1553 @key(return) String;
1554
1555 @key(function) @AdaSubDefn{Strlen} (Item : @key(in) chars_ptr) @key(return) size_t;
1556
1557 @key(procedure) @AdaSubDefn{Update} (Item : @key(in) chars_ptr;
1558 Offset : @key(in) size_t;
1559 Chars : @key(in) char_array;
1560 Check : @key(in) Boolean := True);
1561
1562 @key(procedure) @AdaSubDefn{Update} (Item : @key(in) chars_ptr;
1563 Offset : @key(in) size_t;
1564 Str : @key(in) String;
1565 Check : @key(in) Boolean := True);
1566
1567 @AdaSubDefn{Update_Error} : @key(exception);
1568
1569
1570 @key(private)
1571 ... -- @RI{not specified by the language}
1572 @key(end) Interfaces.C.Strings;
1573 @end{Example}
1574 @begin{discussion}
1575 The string manipulation types and subprograms appear in a
1576 child of Interfaces.C versus being there directly, since it is
1577 useful to have Interfaces.C specified as @nt(pragma) Pure.
1578
1579 Differently named functions New_String and New_Char_Array
1580 are declared, since if there were a single overloaded function
1581 a call with a string literal as actual parameter would be
1582 ambiguous.
1583 @end{discussion}
1584
1585 The type chars_ptr is C-compatible and
1586 corresponds to the use of C's @lquotes@;char *@rquotes@; for
1587 a pointer to the first char in a char array terminated by nul.
1588 When an object of type chars_ptr is declared, its value is
1589 by default set to Null_Ptr, unless the object is imported
1590 (see @RefSecNum(Interfacing Pragmas)).
1591 @begin{discussion}
1592
1593 The type char_array_access is not necessarily C-compatible, since
1594 an object of this type may carry @lquotes@;dope@rquotes@; information.
1595 The programmer should convert from char_array_access to chars_ptr
1596 for objects imported from, exported to, or passed to C.@end{discussion}
1597 @begin{DescribeCode}
1598 @begin{Example}@Keepnext
1599 @key(function) To_Chars_Ptr (Item : @key(in) char_array_access;
1600 Nul_Check : @key(in) Boolean := False)
1601 @key(return) chars_ptr;
1602 @end{Example}
1603 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0061],ARef=[AI95-00140-01]}
1604 @Trailing@;If Item is @key(null), then To_Chars_Ptr returns Null_Ptr.
1605 @Chg{New=[If Item is not @key(null),], Old=[Otherwise, if]} Nul_Check is
1606 True@Chg{New=[,],Old=[]} and Item.@key(all) does not contain nul, then
1607 the function propagates Terminator_Error;
1608 @Chg{New=[otherwise],
1609 Old=[if Nul_Check is True and Item.@key(all) does contain nul,]}
1610 To_Chars_Ptr performs a pointer conversion with no allocation of memory.
1611
1612 @begin{Example}@Keepnext
1613 @key(function) New_Char_Array (Chars : @key(in) char_array) @key(return) chars_ptr;
1614 @end{Example}
1615 This function returns a pointer to an allocated object initialized to
1616 Chars(Chars'First .. Index) & nul, where
1617 @begin{itemize}
1618 Index = Chars'Last if Chars does not contain nul, or
1619
1620 Index is the smallest size_t value I such that Chars(I+1) = nul.
1621 @end{itemize}
1622
1623 @ChgNote{The following paragraph is missing a number in the original version.
1624 To give it a number in the new version, it is marked as an insertion.}
1625 @ChgRef{Version=[0],Kind=[Added]}@Trailing
1626 @Chg{New=[],Old=[@Noparanum@;]}Storage_Error is propagated if the allocation
1627 fails.
1628
1629 @begin{Example}@Keepnext
1630 @key(function) New_String (Str : @key(in) String) @key(return) chars_ptr;
1631 @end{Example}
1632 @Trailing@;This function is equivalent to New_Char_Array(To_C(Str)).
1633
1634 @begin{Example}@Keepnext
1635 @key(procedure) Free (Item : @key(in) @key(out) chars_ptr);
1636 @end{Example}
1637 @Trailing@;If Item is Null_Ptr, then Free has no effect.
1638 Otherwise, Free releases the storage occupied by Value(Item),
1639 and resets Item to Null_Ptr.
1640
1641 @begin{Example}@Keepnext
1642 @key(function) Value (Item : @key(in) chars_ptr) @key(return) char_array;
1643 @end{Example}
1644 @Trailing@;If Item = Null_Ptr then Value propagates Dereference_Error.
1645 Otherwise Value returns the prefix of the array
1646 of chars pointed to by Item, up to and including the
1647 first nul.
1648 The lower bound of the result is 0.
1649 If Item does not point to a nul-terminated string, then
1650 execution of Value is erroneous.
1651
1652 @begin{Example}@Keepnext
1653 @key(function) Value (Item : @key(in) chars_ptr; Length : @key(in) size_t)
1654 @key(return) char_array;
1655 @end{Example}
1656 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0062],ARef=[AI95-00139-01]}
1657 @Trailing@;If Item = Null_Ptr then Value@Chg{New=[],Old=[(Item)]} propagates
1658 Dereference_Error.
1659 Otherwise Value returns the shorter of two arrays@Chg{New=[, either],Old=[:]}
1660 the first Length chars pointed to by Item, @Chg{New=[or],Old=[and]}
1661 Value(Item). The lower bound of the result is 0.
1662 @Chg{New=[If Length is 0, then Value propagates Constraint_Error.],Old=[]}
1663 @begin{Ramification}
1664 Value(New_Char_Array(Chars)) = Chars if Chars does not contain
1665 nul; else Value(New_Char_Array( Chars)) is the prefix of Chars
1666 up to and including the first nul.
1667 @end{Ramification}
1668
1669 @begin{Example}@Keepnext
1670 @key(function) Value (Item : @key(in) chars_ptr) @key(return) String;
1671 @end{Example}
1672 @Trailing@;Equivalent to To_Ada(Value(Item), Trim_Nul=>True).
1673
1674 @begin{Example}@Keepnext
1675 @key(function) Value (Item : @key(in) chars_ptr; Length : @key(in) size_t)
1676 @key(return) String;
1677 @end{Example}
1678 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0063],ARef=[AI95-00177-01]}
1679 @Trailing@;Equivalent to To_Ada(Value(Item, Length)@Chg{New=[ & nul],Old=[]}, Trim_Nul=>True).
1680
1681 @begin{Example}@Keepnext
1682 @key(function) Strlen (Item : @key(in) chars_ptr) @key(return) size_t;
1683 @end{Example}
1684 @Trailing@;Returns @i[Val]'Length@en@;1 where @i[Val] = Value(Item);
1685 propagates Dereference_Error if Item = Null_Ptr.
1686 @begin{ramification}
1687 Strlen returns the number of chars in the array pointed to by Item, up to
1688 and including the char immediately before the first nul.
1689
1690 Strlen has the same possibility for erroneous execution
1691 as Value, in cases where the string has not been nul-terminated.
1692
1693 Strlen has the effect of C's strlen function.
1694 @end{Ramification}
1695
1696 @begin{Example}@Keepnext
1697 @key(procedure) Update (Item : @key(in) chars_ptr;
1698 Offset : @key(in) size_t;
1699 Chars : @key(in) char_array;
1700 Check : Boolean := True);
1701 @end{Example}
1702 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0064],ARef=[AI95-00039-01]}
1703 @Leading@;@Chg{New=[If Item = Null_Ptr, then Update propagates
1704 Dereference_Error. Otherwise, t],Old=[T]}his procedure updates the value
1705 pointed to by Item, starting at position Offset, using Chars as the data to be
1706 copied into the array. Overwriting the nul terminator,
1707 and skipping with the Offset past the nul terminator,
1708 are both prevented if Check is True, as follows:
1709 @begin[itemize]
1710 Let N = Strlen(Item).
1711 If Check is True, then:
1712 @begin{inneritemize}
1713 If Offset+Chars'Length>N, propagate Update_Error.
1714
1715 Otherwise, overwrite the data in the array pointed to by Item,
1716 starting at the char at position Offset, with the data in Chars.
1717 @end{inneritemize}
1718
1719 @Trailing@;If Check is False, then
1720 processing is as above, but with no check that Offset+Chars'Length>N.
1721 @begin{Ramification}
1722 If Chars contains nul, Update's effect may be
1723 to @lquotes@;shorten@rquotes@; the pointed-to char array.@end{ramification}
1724 @end[itemize]
1725
1726 @begin{Example}@Keepnext
1727 @key(procedure) Update (Item : @key(in) chars_ptr;
1728 Offset : @key(in) size_t;
1729 Str : @key(in) String;
1730 Check : @key(in) Boolean := True);
1731 @end{Example}
1732 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00242-01]}
1733 Equivalent to Update(Item, Offset, To_C(Str@Chg{Version=[2],
1734 New=[, Append_Nul => False],Old=[]}), Check).
1735 @begin{Discussion}
1736 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00242-01]}
1737 @ChgAdded{Version=[2],Text=[To truncate the Item to the length of Str, use
1738 Update(Item, Offset, To_C(Str), Check) instead of Update(Item, Offset, Str, Check).
1739 Note that when truncating Item, Item must be longer than Str.]}
1740 @end{Discussion}
1741 @end{DescribeCode}
1742 @end{StaticSem}
1743
1744 @begin{erron}
1745 @PDefn2{Term=(erroneous execution),Sec=(cause)}
1746 Execution of any of the following is erroneous if the Item
1747 parameter is not null_ptr and Item
1748 does not point to a nul-terminated array of chars.
1749 @begin[itemize]
1750 a Value function not taking a Length parameter,
1751
1752 the Free procedure,
1753
1754 the Strlen function.
1755 @end[itemize]
1756
1757 @PDefn2{Term=(erroneous execution),Sec=(cause)}
1758 Execution of Free(X) is also erroneous if the chars_ptr X was not returned
1759 by New_Char_Array or New_String.
1760
1761 @PDefn2{Term=(erroneous execution),Sec=(cause)}
1762 Reading or updating a freed char_array is erroneous.
1763
1764 @PDefn2{Term=(erroneous execution),Sec=(cause)}
1765 Execution of Update is erroneous if Check is False and a call with
1766 Check equal to True would have propagated Update_Error.
1767 @end{erron}
1768
1769 @begin{Notes}
1770 New_Char_Array and New_String might be
1771 implemented either through
1772 the allocation function from the C environment (@lquotes@;malloc@rquotes@;) or through
1773 Ada dynamic memory allocation (@lquotes@;new@rquotes@;). The key points are
1774 @begin{itemize}
1775 the returned value (a chars_ptr) is
1776 represented as a C @lquotes@;char *@rquotes@; so
1777 that it may be passed to C functions;
1778
1779 the allocated object should be freed by the programmer via a call of
1780 Free, not by a called C function.
1781 @end{itemize}
1782 @end{Notes}
1783
1784 @begin{Inconsistent95}
1785 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00242-01]}
1786 @ChgAdded{Version=[2],Text=[@Defn{inconsistencies with Ada 95}
1787 @b[Amendment Correction:] Update for a String parameter is now defined to not
1788 add a nul character. It did add a nul in Ada 95. This means that programs
1789 that used this behavior of Update to truncate a string will no longer work
1790 (the string will not be truncated). This change makes Update for a string
1791 consistent with Update for a char_array (no implicit nul is added to the end
1792 of a char_array).]}
1793 @end{Inconsistent95}
1794
1795 @begin{Extend95}
1796 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00161-01]}
1797 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1798 @b[Amendment Correction:] Added @nt{pragma} Preelaborable_Initialization to
1799 type chars_ptr, so that it can be used in preelaborated units.]}
1800
1801 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00276-01]}
1802 @ChgAdded{Version=[2],Text=[@b[Amendment Correction:] The components of
1803 chars_ptr_array are aliased so that it can be used to instantiate
1804 Interfaces.C.Pointers (that is its intended purpose, which is otherwise
1805 mysterious as it has no operations).]}
1806 @end{Extend95}
1807
1808 @begin{DiffWord95}
1809 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0061],ARef=[AI95-00140-01]}
1810 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Fixed the missing semantics
1811 of To_Char_Ptr when Nul_Check is False.]}
1812
1813 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0062],ARef=[AI95-00139-01]}
1814 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Fixed the missing semantics
1815 of Value when the Length is 0.]}
1816
1817 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0063],ARef=[AI95-00177-01]}
1818 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Corrected the definition of
1819 Value to avoid raising Terminator_Error.]}
1820
1821 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0064],ARef=[AI95-00039-01]}
1822 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Fixed the missing semantics
1823 of Update when Item is Null_Ptr.]}
1824 @end{DiffWord95}
1825
1826
1827 @LabeledSubClause{The Generic Package Interfaces.C.Pointers}
1828 @begin{Intro}
1829 The generic package Interfaces.C.Pointers allows the Ada programmer to
1830 perform C-style operations on pointers. It includes an access type
1831 Pointer, Value functions that dereference a Pointer and deliver the
1832 designated array, several pointer arithmetic operations, and @lquotes@;copy@rquotes@;
1833 procedures that copy the contents of a source pointer into the array
1834 designated by a destination pointer. As in C, it treats an object Ptr of
1835 type Pointer as a pointer to the first element of an array, so that for
1836 example, adding 1 to Ptr yields a pointer to the
1837 second element of the array.
1838
1839 The generic allows two styles of usage: one in which the array is
1840 terminated by a special terminator element; and another in which the
1841 programmer needs to keep track of the length.
1842 @end{Intro}
1843
1844 @begin{StaticSem}
1845 @Leading@;The generic library package Interfaces.C.Pointers has the
1846 following declaration:
1847 @begin{Example}
1848 @key(generic)
1849 @key(type) Index @key(is) (<>);
1850 @key(type) Element @key(is) @key(private);
1851 @key(type) Element_Array @key(is) @key(array) (Index @key(range) <>) @key(of) @key(aliased) Element;
1852 Default_Terminator : Element;
1853 @key(package) Interfaces.C.Pointers @key(is)@ChildUnit{Parent=[Interfaces.C],Child=[Pointers]}
1854 @key[pragma] Preelaborate(Pointers);
1855
1856 @key(type) @AdaTypeDefn{Pointer} @key(is) @key(access) @key(all) Element;
1857
1858 @key(function) @AdaSubDefn{Value}(Ref : @key(in) Pointer;
1859 Terminator : @key(in) Element := Default_Terminator)
1860 @key(return) Element_Array;
1861
1862 @key(function) @AdaSubDefn{Value}(Ref : @key(in) Pointer;
1863 Length : @key(in) ptrdiff_t)
1864 @key(return) Element_Array;
1865
1866
1867 @AdaExcDefn{Pointer_Error} : @key(exception);
1868
1869 @RI{-- C-style Pointer arithmetic}
1870
1871 @key(function) "+" (Left : @key(in) Pointer; Right : @key(in) ptrdiff_t) @key(return) Pointer;
1872 @key(function) "+" (Left : @key(in) ptrdiff_t; Right : @key(in) Pointer) @key(return) Pointer;
1873 @key(function) "-" (Left : @key(in) Pointer; Right : @key(in) ptrdiff_t) @key(return) Pointer;
1874 @key(function) "-" (Left : @key(in) Pointer; Right : @key(in) Pointer) @key(return) ptrdiff_t;
1875
1876 @key(procedure) @AdaSubDefn{Increment} (Ref : @key(in) @key(out) Pointer);
1877 @key(procedure) @AdaSubDefn{Decrement} (Ref : @key(in) @key(out) Pointer);
1878
1879 @key(pragma) Convention (Intrinsic, "+");
1880 @key(pragma) Convention (Intrinsic, "-");
1881 @key(pragma) Convention (Intrinsic, Increment);
1882 @key(pragma) Convention (Intrinsic, Decrement);
1883
1884 @key(function) @AdaSubDefn{Virtual_Length} (Ref : @key(in) Pointer;
1885 Terminator : @key(in) Element := Default_Terminator)
1886 @key(return) ptrdiff_t;
1887
1888 @key(procedure) @AdaSubDefn{Copy_Terminated_Array}
1889 (Source : @key(in) Pointer;
1890 Target : @key(in) Pointer;
1891 Limit : @key(in) ptrdiff_t := ptrdiff_t'Last;
1892 Terminator : @key(in) Element := Default_Terminator);
1893
1894 @key(procedure) @AdaSubDefn{Copy_Array} (Source : @key(in) Pointer;
1895 Target : @key(in) Pointer;
1896 Length : @key(in) ptrdiff_t);
1897
1898 @key(end) Interfaces.C.Pointers;
1899 @end{Example}
1900
1901 @Leading@;The type Pointer is C-compatible and
1902 corresponds to one use of C's @lquotes@;Element *@rquotes@;.
1903 An object of type Pointer is interpreted as a pointer to the
1904 initial Element in an Element_Array.
1905 Two styles are supported:
1906 @begin{Itemize}
1907 Explicit termination of an array value with
1908 Default_Terminator (a special terminator value);
1909
1910 @Trailing@;Programmer-managed length, with
1911 Default_Terminator treated simply as a data element.
1912 @end{Itemize}
1913 @begin{DescribeCode}
1914 @begin{Example}@Keepnext
1915 @key(function) Value(Ref : @key(in) Pointer;
1916 Terminator : @key(in) Element := Default_Terminator)
1917 @key(return) Element_Array;
1918 @end{Example}
1919 @Trailing@;This function returns an Element_Array whose value is the array
1920 pointed to by Ref, up to and including the first Terminator; the lower bound
1921 of the array is Index'First. Interfaces.C.Strings.Dereference_Error is
1922 propagated if Ref is @key(null).
1923
1924 @begin{Example}@Keepnext
1925 @key(function) Value(Ref : @key(in) Pointer;
1926 Length : @key(in) ptrdiff_t)
1927 @key(return) Element_Array;
1928 @end{Example}
1929 @Trailing@;This function returns an Element_Array comprising the first Length
1930 elements pointed to by Ref. The exception
1931 Interfaces.C.Strings.Dereference_Error is propagated if Ref is @key(null).
1932 @end{DescribeCode}
1933
1934 @Trailing@;The "+" and "@en@;" functions perform arithmetic on Pointer values,
1935 based on the Size of the array elements. In each of these functions,
1936 Pointer_Error is propagated if a Pointer parameter is @key(null).
1937 @begin{DescribeCode}
1938 @begin{Example}@Keepnext
1939 @key(procedure) Increment (Ref : @key(in) @key(out) Pointer);
1940 @end{Example}
1941 @Trailing@;Equivalent to Ref := Ref+1.
1942
1943 @begin{Example}@Keepnext
1944 @key(procedure) Decrement (Ref : @key(in) @key(out) Pointer);
1945 @end{Example}
1946 @Trailing@;Equivalent to Ref := Ref@en@;1.
1947
1948 @begin{Example}@Keepnext
1949 @key(function) Virtual_Length (Ref : @key(in) Pointer;
1950 Terminator : @key(in) Element := Default_Terminator)
1951 @key(return) ptrdiff_t;
1952 @end{Example}
1953 @Trailing@;Returns the number of Elements, up to the one just before the first
1954 Terminator, in Value(Ref, Terminator).
1955
1956 @begin{Example}@Keepnext
1957 @key(procedure) Copy_Terminated_Array
1958 (Source : @key(in) Pointer;
1959 Target : @key(in) Pointer;
1960 Limit : @key(in) ptrdiff_t := ptrdiff_t'Last;
1961 Terminator : @key(in) Element := Default_Terminator);
1962 @end{Example}
1963 @Trailing@;This procedure copies Value(Source, Terminator) into the array
1964 pointed to by Target; it stops either after Terminator has been copied, or the
1965 number of elements copied is Limit, whichever occurs first.
1966 Dereference_Error is propagated if either Source or Target is @key(null).
1967 @begin{ramification}
1968 It is the programmer's responsibility to ensure that
1969 elements are not copied beyond the logical length of the target array.
1970 @end{ramification}
1971 @begin{ImplNote}
1972 The implementation has to take care to check the Limit first.
1973 @end{ImplNote}
1974
1975 @begin{Example}@Keepnext
1976 @key(procedure) Copy_Array (Source : @key(in) Pointer;
1977 Target : @key(in) Pointer;
1978 Length : @key(in) ptrdiff_t);
1979 @end{Example}
1980 This procedure copies the first Length elements from the array pointed
1981 to by Source, into the array pointed to by Target.
1982 Dereference_Error is propagated if either
1983 Source or Target is @key(null).
1984 @end{DescribeCode}
1985 @end{StaticSem}
1986
1987 @begin{erron}
1988 @PDefn2{Term=(erroneous execution),Sec=(cause)}
1989 It is erroneous to dereference a Pointer that does not designate
1990 an aliased Element.
1991 @begin{Discussion}
1992 Such a Pointer could arise via "+", "@en@;", Increment, or
1993 Decrement.@end{discussion}
1994
1995 @PDefn2{Term=(erroneous execution),Sec=(cause)}
1996 Execution of Value(Ref, Terminator) is erroneous if
1997 Ref does not designate an aliased Element in an Element_Array
1998 terminated by Terminator.
1999
2000 @PDefn2{Term=(erroneous execution),Sec=(cause)}
2001 Execution of Value(Ref, Length) is erroneous if
2002 Ref does not designate an aliased Element in an Element_Array
2003 containing at least Length Elements between the designated Element and
2004 the end of the array, inclusive.
2005
2006 @PDefn2{Term=(erroneous execution),Sec=(cause)}
2007 Execution of Virtual_Length(Ref, Terminator) is erroneous if
2008 Ref does not designate an aliased Element in an Element_Array
2009 terminated by Terminator.
2010
2011 @Leading@PDefn2{Term=(erroneous execution),Sec=(cause)}
2012 Execution of Copy_Terminated_Array(Source, Target, Limit, Terminator)
2013 is erroneous in either of the following situations:
2014 @begin[itemize]
2015 Execution of both Value(Source, Terminator) and
2016 Value(Source, Limit) are erroneous, or
2017
2018 Copying writes past the end of the array containing the Element
2019 designated by Target.
2020 @end[Itemize]
2021
2022 @PDefn2{Term=(erroneous execution),Sec=(cause)}
2023 Execution of Copy_Array(Source, Target, Length) is erroneous if either
2024 Value(Source, Length) is erroneous, or copying writes past the end of
2025 the array containing the Element designated by Target.
2026 @end{erron}
2027
2028 @begin{Notes}
2029 @Leading@;To compose a Pointer from an Element_Array, use 'Access on
2030 the first element. For example (assuming appropriate instantiations):
2031 @begin{example}
2032 Some_Array : Element_Array(0..5) ;
2033 Some_Pointer : Pointer := Some_Array(0)'Access;
2034 @end{example}
2035 @end{Notes}
2036
2037 @begin{Examples}
2038 @Leading@Keepnext@i{Example of Interfaces.C.Pointers:}
2039 @begin{Example}
2040 @key(with) Interfaces.C.Pointers;
2041 @key(with) Interfaces.C.Strings;
2042 @key(procedure) Test_Pointers @key(is)
2043 @key(package) C @key(renames) Interfaces.C;
2044 @key(package) Char_Ptrs @key(is)
2045 @key(new) C.Pointers (Index => C.size_t,
2046 Element => C.char,
2047 Element_Array => C.char_array,
2048 Default_Terminator => C.nul);
2049
2050 @key(use) @key(type) Char_Ptrs.Pointer;
2051 @key(subtype) Char_Star @key(is) Char_Ptrs.Pointer;
2052
2053 @key(procedure) Strcpy (Target_Ptr, Source_Ptr : Char_Star) @key(is)
2054 Target_Temp_Ptr : Char_Star := Target_Ptr;
2055 Source_Temp_Ptr : Char_Star := Source_Ptr;
2056 Element : C.char;
2057 @key(begin)
2058 @key(if) Target_Temp_Ptr = @key(null) @key(or) Source_Temp_Ptr = @key(null) @key(then)
2059 @key(raise) C.Strings.Dereference_Error;
2060 @key(end if);
2061
2062 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0065],ARef=[AI95-00142-01]}
2063 @key(loop)
2064 Element := Source_Temp_Ptr.@key(all);
2065 Target_Temp_Ptr.@key(all) := Element;
2066 @key(exit) @key(when) @Chg{New=[C."="(Element, C.nul)],Old=[Element = C.nul]};
2067 Char_Ptrs.Increment(Target_Temp_Ptr);
2068 Char_Ptrs.Increment(Source_Temp_Ptr);
2069 @key(end) @key(loop);
2070 @key(end) Strcpy;
2071 @key(begin)
2072 ...
2073 @key(end) Test_Pointers;
2074 @end{Example}
2075 @end{Examples}
2076
2077
2078
2079 @LabeledAddedSubClause{Version=[2],Name=[Pragma Unchecked_Union]}
2080
2081 @begin{Intro}
2082 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2083 @ChgAdded{Version=[2],Text=[@Defn2{Term=[union],Sec=[C]}
2084 @Redundant[A pragma Unchecked_Union specifies an interface correspondence
2085 between a given discriminated type and some C union. The pragma
2086 specifies that the associated type shall be given a representation
2087 that leaves no space for its discriminant(s).]]}
2088 @end{Intro}
2089
2090 @begin{Syntax}
2091
2092 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2093 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],
2094 Text=[The form of a pragma Unchecked_Union is as follows:]}
2095
2096 @ChgRef{Version=[2],Kind=[AddedNormal]}
2097 @ChgAdded{Version=[2],Text=`@AddedPragmaSyn`Version=[2],@key{pragma} @prag{Unchecked_Union} (@Syni<first_subtype_>@Syn2<local_name>);''}
2098
2099 @end{Syntax}
2100
2101 @begin{Legality}
2102
2103 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2104 @ChgAdded{Version=[2],Text=[Unchecked_Union is a representation pragma,
2105 specifying the unchecked union aspect of representation.]}
2106
2107 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2108 @ChgAdded{Version=[2],Text=[The @SynI{first_subtype_}@nt{local_name} of a
2109 @nt{pragma} Unchecked_Union shall denote an unconstrained discriminated record
2110 subtype having a @nt{variant_part}.]}
2111
2112 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2113 @ChgAdded{Version=[2],Text=[@Defn{unchecked union type}
2114 @Defn{unchecked union subtype}
2115 @Defn{unchecked union object}
2116 A type to which a pragma Unchecked_Union applies is called an
2117 @i<unchecked union type>. A subtype of an
2118 unchecked union type is defined to be an @i<unchecked union subtype>.
2119 An object of an unchecked union type is defined to be an @i<unchecked union
2120 object>.]}
2121
2122 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2123 @ChgAdded{Version=[2],Text=[All component subtypes of an unchecked union type
2124 shall be C-compatible.]}
2125
2126 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2127 @ChgAdded{Version=[2],Text=[If a component subtype of an unchecked union type
2128 is subject to a per-object constraint, then the component subtype shall be an
2129 unchecked union subtype.]}
2130
2131 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2132 @ChgAdded{Version=[2],Text=[Any name that denotes a discriminant of an object
2133 of an unchecked union type shall occur within the declarative region of the
2134 type.]}
2135
2136 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2137 @ChgAdded{Version=[2],Text=[A component declared in a @nt{variant_part} of an
2138 unchecked union type shall not have a controlled, protected, or task part.]}
2139
2140 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2141 @ChgAdded{Version=[2],Text=[The completion of an incomplete or private type
2142 declaration having a @nt{known_discriminant_part} shall not be an unchecked
2143 union type.]}
2144
2145 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2146 @ChgAdded{Version=[2],Text=[An unchecked union subtype shall only be passed as
2147 a generic actual parameter if the corresponding formal type has no known
2148 discriminants or is an unchecked union type.]}
2149
2150 @begin{Ramification}
2151 @ChgRef{Version=[2],Kind=[AddedNormal]}
2152 @ChgAdded{Version=[2],Text=[This includes formal private types without a
2153 @nt{known_discriminant_part}, formal derived types that do not inherit any
2154 discriminants (formal derived types do not have @nt{known_discriminant_part}s),
2155 and formal derived types that are unchecked union types.]}
2156 @end{Ramification}
2157
2158 @end{Legality}
2159
2160 @begin{StaticSem}
2161
2162 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2163 @ChgAdded{Version=[2],Text=[An unchecked union type is eligible for convention
2164 C.]}
2165
2166 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2167 @ChgAdded{Version=[2],Text=[All objects of an unchecked union type have the
2168 same size.]}
2169
2170 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2171 @ChgAdded{Version=[2],Text=[Discriminants of objects of an unchecked union type
2172 are of size zero.]}
2173
2174 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2175 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],
2176 Text=[Any check which would require reading a discriminant
2177 of an unchecked union object is suppressed (see @RefSecNum{Suppressing Checks}).
2178 These checks include:]}
2179
2180 @begin{Itemize}
2181 @ChgRef{Version=[2],Kind=[AddedNormal]}
2182 @ChgAdded{Version=[2],Text=[The check performed when addressing a variant
2183 component (i.e., a component that was declared in a variant part) of an
2184 unchecked union object that the object has this component (see
2185 @RefSecNum{Selected Components}).]}
2186
2187 @ChgRef{Version=[2],Kind=[AddedNormal]}
2188 @ChgAdded{Version=[2],Text=[Any checks associated with a type or subtype
2189 conversion of a value of an unchecked union type (see
2190 @RefSecNum{Type Conversions}). This includes, for example, the check
2191 associated with the implicit subtype conversion of an assignment statement.]}
2192
2193 @ChgRef{Version=[2],Kind=[AddedNormal]}
2194 @ChgAdded{Version=[2],Text=[The subtype membership check associated with the
2195 evaluation of a qualified expression (see @RefSecNum{Qualified Expressions})
2196 or an uninitialized allocator (see @RefSecNum{Allocators}).]}
2197 @end{Itemize}
2198
2199 @begin{Discussion}
2200 @ChgRef{Version=[2],Kind=[AddedNormal]}
2201 @ChgAdded{Version=[2],Text=[If a suppressed check would have failed,
2202 execution is erroneous (see @RefSecNum{Suppressing Checks}). An
2203 implementation is always allowed to make a suppressed check if it can
2204 somehow determine the discriminant value.]}
2205 @end{Discussion}
2206
2207 @end{StaticSem}
2208
2209 @begin{RunTime}
2210
2211 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2212 @ChgAdded{Version=[2],Text=[A view of an unchecked union object (including a
2213 type conversion or function call) has @i<inferable discriminants> if it has a
2214 constrained nominal subtype, unless the object is a component of an enclosing
2215 unchecked union object that is subject to a per-object constraint and the
2216 enclosing object lacks inferable discriminants.@Defn{inferable discriminants}]}
2217
2218 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2219 @ChgAdded{Version=[2],Text=[An expression of an unchecked union type has
2220 inferable discriminants if it is either a name of an object with inferable
2221 discriminants or a qualified expression whose @nt{subtype_mark} denotes a
2222 constrained subtype.]}
2223
2224 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2225 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],
2226 Text=[Program_Error is raised in the following
2227 cases:@Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}]}
2228
2229 @begin{Itemize}
2230 @ChgRef{Version=[2],Kind=[AddedNormal]}
2231 @ChgAdded{Version=[2],Text=[Evaluation of the predefined equality operator
2232 for an unchecked union type if either of the operands lacks inferable
2233 discriminants.]}
2234
2235 @ChgRef{Version=[2],Kind=[AddedNormal]}
2236 @ChgAdded{Version=[2],Text=[Evaluation of the predefined equality operator
2237 for a type which has a subcomponent of an unchecked union type whose nominal
2238 subtype is unconstrained.]}
2239
2240 @ChgRef{Version=[2],Kind=[AddedNormal]}
2241 @ChgAdded{Version=[2],Text=[Evaluation of a membership test if the
2242 @nt{subtype_mark} denotes a constrained unchecked union subtype and the
2243 expression lacks inferable discriminants.]}
2244
2245 @ChgRef{Version=[2],Kind=[AddedNormal]}
2246 @ChgAdded{Version=[2],Text=[Conversion from a derived unchecked union type to
2247 an unconstrained non-unchecked-union type if the operand of the conversion
2248 lacks inferable discriminants.]}
2249
2250 @ChgRef{Version=[2],Kind=[AddedNormal]}
2251 @ChgAdded{Version=[2],Text=[Execution of the default implementation of the
2252 Write or Read attribute of an unchecked union type.]}
2253
2254 @ChgRef{Version=[2],Kind=[AddedNormal]}
2255 @ChgAdded{Version=[2],Text=[Execution of the default implementation of the
2256 Output or Input attribute of an unchecked union type if the type lacks default
2257 discriminant values.]}
2258 @end{Itemize}
2259 @end{RunTime}
2260
2261 @begin{ImplPerm}
2262
2263 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2264 @ChgAdded{Version=[2],Text=[An implementation may require that @nt{pragma}
2265 Controlled be specified for the type of an access subcomponent of an unchecked
2266 union type.]}
2267
2268 @end{ImplPerm}
2269
2270 @begin{Notes}
2271
2272 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2273 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],
2274 Text=[The use of an unchecked union to obtain the effect of an
2275 unchecked conversion results in erroneous execution (see @RefSecNum{Suppressing Checks}).
2276 Execution of the following example is erroneous even if
2277 Float'Size = Integer'Size:]}
2278
2279 @begin{Example}
2280 @ChgRef{Version=[2],Kind=[AddedNormal]}
2281 @ChgAdded{Version=[2],Text=[@key<type> T (Flag : Boolean := False) @key<is>
2282 @key<record>
2283 @key<case> Flag @key<is>
2284 @key<when> False =>
2285 F1 : Float := 0.0;
2286 @key<when> True =>
2287 F2 : Integer := 0;
2288 @key<end case>;
2289 @key<end record>;
2290 @key<pragma> Unchecked_Union (T);]}
2291
2292 @ChgRef{Version=[2],Kind=[AddedNormal]}
2293 @ChgAdded{Version=[2],Text=[X : T;
2294 Y : Integer := X.F2; -- @RI[erroneous]]}
2295 @end{Example}
2296 @end{Notes}
2297
2298 @begin{Extend95}
2299 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00216-01]}
2300 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2301 @nt{Pragma} Unchecked_Union is new.]}
2302 @end{Extend95}
2303
2304
2305
2306 @RMNewPage@Comment{For printed RM Ada 2005}
2307 @LabeledClause{Interfacing with COBOL}
2308 @begin{Intro}
2309 @Defn{interface to COBOL}
2310 @Defn{COBOL interface}
2311 The facilities relevant to interfacing with
2312 the COBOL language
2313 are the package
2314 Interfaces.COBOL
2315 and support for the Import, Export and
2316 Convention pragmas with @i{convention}_@nt{identifier} COBOL.
2317
2318 @Leading@;The COBOL interface package supplies several sets of facilities:
2319 @begin{itemize}
2320 A set of types corresponding to the native
2321 COBOL types of the supported COBOL implementation
2322 (so-called @lquotes@;internal COBOL representations@rquotes@;),
2323 allowing Ada data to be passed as parameters to COBOL programs
2324
2325 A set of types and constants reflecting external data representations
2326 such as might be found in files or databases, allowing COBOL-generated
2327 data to be read by an Ada program, and Ada-generated data to be read
2328 by COBOL programs
2329
2330 A generic package for converting between an Ada decimal type value and
2331 either an internal or external COBOL representation
2332 @end{itemize}
2333 @end{Intro}
2334
2335 @begin{StaticSem}
2336 @Leading@Keepnext@;The library package Interfaces.COBOL has the following declaration:
2337 @begin{Example}
2338 @key(package) Interfaces.COBOL @key(is)@ChildUnit{Parent=[Interfaces],Child=[COBOL]}
2339 @key[pragma] Preelaborate(COBOL);
2340
2341 @RI{-- Types and operations for internal data representations}
2342
2343 @key(type) @AdaTypeDefn{Floating} @key(is) @key(digits) @RI{implementation-defined};
2344 @key(type) @AdaTypeDefn{Long_Floating} @key(is) @key(digits) @RI{implementation-defined};
2345
2346 @key(type) @AdaTypeDefn{Binary} @key(is) @key(range) @RI{implementation-defined};
2347 @key(type) @AdaTypeDefn{Long_Binary} @key(is) @key(range) @RI{implementation-defined};
2348
2349 @AdaObjDefn{Max_Digits_Binary} : @key(constant) := @RI{implementation-defined};
2350 @AdaObjDefn{Max_Digits_Long_Binary} : @key(constant) := @RI{implementation-defined};
2351
2352 @key(type) @AdaTypeDefn{Decimal_Element} @key(is) @key(mod) @RI{implementation-defined};
2353 @key(type) @AdaTypeDefn{Packed_Decimal} @key(is) @key(array) (Positive @key(range) <>) @key(of) Decimal_Element;
2354 @key(pragma) Pack(Packed_Decimal);
2355
2356
2357 @key(type) @AdaTypeDefn{COBOL_Character} @key(is) @RI{implementation-defined character type};
2358
2359 @AdaObjDefn{Ada_To_COBOL} : @key(array) (Character) @key(of) COBOL_Character := @RI{implementation-defined};
2360
2361 @AdaObjDefn{COBOL_To_Ada} : @key(array) (COBOL_Character) @key(of) Character := @RI{implementation-defined};
2362
2363 @key(type) @AdaTypeDefn{Alphanumeric} @key(is) @key(array) (Positive range <>) @key(of) COBOL_Character;
2364 @key(pragma) Pack(Alphanumeric);
2365
2366 @key(function) @AdaSubDefn{To_COBOL} (Item : @key(in) String) @key(return) Alphanumeric;
2367 @key(function) @AdaSubDefn{To_Ada} (Item : @key(in) Alphanumeric) @key(return) String;
2368
2369 @key(procedure) @AdaSubDefn{To_COBOL} (Item : @key(in) String;
2370 Target : @key(out) Alphanumeric;
2371 Last : @key(out) Natural);
2372
2373 @key(procedure) @AdaSubDefn{To_Ada} (Item : @key(in) Alphanumeric;
2374 Target : @key(out) String;
2375 Last : @key(out) Natural);
2376
2377 @key(type) @AdaTypeDefn{Numeric} @key(is) @key(array) (Positive @key[range] <>) @key(of) COBOL_Character;
2378 @key(pragma) Pack(Numeric);
2379
2380 @RI{-- Formats for COBOL data representations}
2381
2382 @key(type) @AdaTypeDefn{Display_Format} @key(is) @key(private);
2383
2384 @AdaObjDefn{Unsigned} : @key(constant) Display_Format;
2385 @AdaObjDefn{Leading_Separate} : @key(constant) Display_Format;
2386 @AdaObjDefn{Trailing_Separate} : @key(constant) Display_Format;
2387 @AdaObjDefn{Leading_Nonseparate} : @key(constant) Display_Format;
2388 @AdaObjDefn{Trailing_Nonseparate} : @key(constant) Display_Format;
2389
2390 @key(type) @AdaTypeDefn{Binary_Format} @key(is) @key(private);
2391
2392 @AdaObjDefn{High_Order_First} : @key(constant) Binary_Format;
2393 @AdaObjDefn{Low_Order_First} : @key(constant) Binary_Format;
2394 @AdaObjDefn{Native_Binary} : @key(constant) Binary_Format;
2395
2396 @key(type) @AdaTypeDefn{Packed_Format} @key(is) @key(private);
2397
2398 @AdaObjDefn{Packed_Unsigned} : @key(constant) Packed_Format;
2399 @AdaObjDefn{Packed_Signed} : @key(constant) Packed_Format;
2400
2401
2402 @RI{-- Types for external representation of COBOL binary data}
2403
2404 @key(type) @AdaTypeDefn{Byte} @key(is) @key(mod) 2**COBOL_Character'Size;
2405 @key(type) @AdaTypeDefn{Byte_Array} @key(is) @key(array) (Positive @key(range) <>) @key(of) Byte;
2406 @key(pragma) Pack (Byte_Array);
2407
2408 @AdaExcDefn{Conversion_Error} : @key(exception);
2409
2410 @key(generic)
2411 @key(type) Num @key(is) @key(delta) <> @key(digits) <>;
2412 @key(package) @AdaPackDefn{Decimal_Conversions} @key(is)
2413
2414 @RI{-- Display Formats: data values are represented as Numeric}
2415
2416 @key(function) @AdaSubDefn{Valid} (Item : @key(in) Numeric;
2417 Format : @key(in) Display_Format) @key(return) Boolean;
2418
2419 @key(function) @AdaSubDefn{Length} (Format : @key(in) Display_Format) @key(return) Natural;
2420
2421
2422 @key(function) @AdaSubDefn{To_Decimal} (Item : @key(in) Numeric;
2423 Format : @key(in) Display_Format) @key(return) Num;
2424
2425 @key(function) @AdaSubDefn{To_Display} (Item : @key(in) Num;
2426 Format : @key(in) Display_Format) @key(return) Numeric;
2427
2428
2429 @RI{-- Packed Formats: data values are represented as Packed_Decimal}
2430
2431 @key(function) @AdaSubDefn{Valid} (Item : @key(in) Packed_Decimal;
2432 Format : @key(in) Packed_Format) @key(return) Boolean;
2433
2434 @key(function) @AdaSubDefn{Length} (Format : @key(in) Packed_Format) @key(return) Natural;
2435
2436 @key(function) @AdaSubDefn{To_Decimal} (Item : @key(in) Packed_Decimal;
2437 Format : @key(in) Packed_Format) @key(return) Num;
2438
2439 @key(function) @AdaSubDefn{To_Packed} (Item : @key(in) Num;
2440 Format : @key(in) Packed_Format) @key(return) Packed_Decimal;
2441
2442
2443 @RI{-- Binary Formats: external data values are represented as Byte_Array}
2444
2445 @key(function) @AdaSubDefn{Valid} (Item : @key(in) Byte_Array;
2446 Format : @key(in) Binary_Format) @key(return) Boolean;
2447
2448 @key(function) @AdaSubDefn{Length} (Format : @key(in) Binary_Format) @key(return) Natural;
2449 @key(function) @AdaSubDefn{To_Decimal} (Item : @key(in) Byte_Array;
2450 Format : @key(in) Binary_Format) @key(return) Num;
2451
2452 @key(function) @AdaSubDefn{To_Binary} (Item : @key(in) Num;
2453 Format : @key(in) Binary_Format) @key(return) Byte_Array;
2454
2455 @RI{-- Internal Binary formats: data values are of type Binary or Long_Binary}
2456
2457 @key(function) @AdaSubDefn{To_Decimal} (Item : @key(in) Binary) @key(return) Num;
2458 @key(function) @AdaSubDefn{To_Decimal} (Item : @key(in) Long_Binary) @key(return) Num;
2459
2460 @key(function) @AdaSubDefn{To_Binary} (Item : @key(in) Num) @key(return) Binary;
2461 @key(function) @AdaSubDefn{To_Long_Binary} (Item : @key(in) Num) @key(return) Long_Binary;
2462
2463 @key(end) Decimal_Conversions;
2464
2465 @key(private)
2466 ... -- @RI{not specified by the language}
2467 @key(end) Interfaces.COBOL;
2468 @end{Example}
2469 @ChgImplDef{Version=[1],Kind=[Revised],Text=[The types Floating, Long_Floating, Binary, Long_Binary,
2470 Decimal_Element, and COBOL_Character; and the initializations
2471 of the variables Ada_To_COBOL and COBOL_To_Ada, in Interfaces.COBOL@Chg{New=[.],Old=[]}]}
2472
2473 Each of the types in Interfaces.COBOL is COBOL-compatible.
2474
2475 The types Floating and Long_Floating correspond to the
2476 native types in COBOL for data items with computational usage
2477 implemented by floating point.
2478 The types Binary and Long_Binary correspond to the
2479 native types in COBOL for data items with binary usage,
2480 or with computational usage implemented by binary.
2481
2482 Max_Digits_Binary is the largest number of decimal digits in a
2483 numeric value that is represented as Binary.
2484 Max_Digits_Long_Binary is the largest number of decimal digits in a
2485 numeric value that is represented as Long_Binary.
2486
2487 The type Packed_Decimal corresponds to COBOL's packed-decimal
2488 usage.
2489
2490 The type COBOL_Character defines the run-time character set used in the
2491 COBOL implementation.
2492 Ada_To_COBOL and COBOL_To_Ada are the mappings between the Ada and
2493 COBOL run-time character sets.
2494 @begin{reason}
2495 The character mappings are visible variables, since the
2496 user needs the ability to modify them at run time.
2497 @end{reason}
2498
2499 Type Alphanumeric corresponds to COBOL's alphanumeric data category.
2500
2501 Each of the functions To_COBOL and To_Ada converts its parameter
2502 based on the mappings Ada_To_COBOL and COBOL_To_Ada, respectively.
2503 The length of the result for each is the length of the parameter,
2504 and the lower bound of the result is 1. Each component of the result
2505 is obtained by applying the relevant mapping to the corresponding
2506 component of the parameter.
2507
2508 Each of the procedures To_COBOL and To_Ada copies converted elements
2509 from Item to Target, using the appropriate mapping (Ada_To_COBOL or
2510 COBOL_To_Ada, respectively). The index in Target of the last element
2511 assigned is returned in Last (0 if Item is a null array).
2512 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
2513 If Item'Length exceeds Target'Length, Constraint_Error is propagated.
2514
2515 Type Numeric corresponds to COBOL's numeric data category with
2516 display usage.
2517
2518 @Trailing@;The types Display_Format, Binary_Format, and Packed_Format
2519 are used in conversions between Ada decimal type values and
2520 COBOL internal or external data representations. The value of the
2521 constant Native_Binary is either High_Order_First or Low_Order_First,
2522 depending on the implementation.
2523 @begin{DescribeCode}
2524 @begin{Example}@Keepnext
2525 @key(function) Valid (Item : @key(in) Numeric;
2526 Format : @key(in) Display_Format) @key(return) Boolean;
2527 @end{Example}
2528 @Leading@;The function Valid checks that the
2529 Item parameter has a value consistent with the value of Format.
2530 If the value of Format is other than
2531 Unsigned, Leading_Separate, and Trailing_Separate,
2532 the effect is implementation defined. If Format does have one
2533 of these values, the following rules apply:
2534 @begin{itemize}
2535 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0066],ARef=[AI95-00071-01]}
2536 Format=Unsigned: if Item comprises @Chg{New=[],Old=[zero or more leading
2537 space characters followed by ]}one or more decimal digit
2538 characters then Valid returns True, else it returns False.
2539
2540 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0066],ARef=[AI95-00071-01]}
2541 Format=Leading_Separate: if Item comprises
2542 @Chg{New=[],Old=[zero or more leading space characters, followed by ]}a
2543 single occurrence of the plus or minus sign character, and then one or more
2544 decimal digit characters, then Valid returns True, else it returns False.
2545
2546 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0066],ARef=[AI95-00071-01]}
2547 @Trailing@;Format=Trailing_Separate: if Item comprises
2548 @Chg{New=[],Old=[zero or more leading space characters, followed by ]}one or
2549 more decimal digit characters and finally a plus or minus sign character,
2550 then Valid returns True, else it returns False.
2551 @end{itemize}
2552
2553 @begin{Example}@Keepnext
2554 @key(function) Length (Format : @key(in) Display_Format) @key(return) Natural;
2555 @end{Example}
2556 @Trailing@;The Length function returns the minimal length of a Numeric value
2557 sufficient to hold any value of type Num when represented as Format.
2558
2559 @begin{Example}@Keepnext
2560 @key(function) To_Decimal (Item : @key(in) Numeric;
2561 Format : @key(in) Display_Format) @key(return) Num;
2562 @end{Example}
2563 @Trailing@;Produces a value of type Num corresponding to Item as represented by
2564 Format.
2565 The number of digits after the assumed
2566 radix point in Item is Num'Scale.
2567 Conversion_Error is propagated if the value
2568 represented by Item is outside the range of Num.
2569 @begin{discussion}
2570 There is no issue of truncation versus rounding, since
2571 the number of decimal places is established by Num'Scale.@end{discussion}
2572
2573 @begin{Example}@Keepnext
2574 @key(function) To_Display (Item : @key(in) Num;
2575 Format : @key(in) Display_Format) @key(return) Numeric;
2576 @end{Example}
2577 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0067],ARef=[AI95-00072-01]}
2578 @Trailing@;This function returns the Numeric value for Item, represented in
2579 accordance with Format.
2580 @Chg{New=[The length of the returned value is Length(Format), and the
2581 lower bound is 1. ],Old=[]}Conversion_Error is propagated if Num is negative
2582 and Format is Unsigned.
2583
2584 @begin{Example}@Keepnext
2585 @key(function) Valid (Item : @key(in) Packed_Decimal;
2586 Format : @key(in) Packed_Format) @key(return) Boolean;
2587 @end{Example}
2588 @Trailing@;This function returns True if Item has a value consistent with Format,
2589 and False otherwise. The rules for the formation of Packed_Decimal
2590 values are implementation defined.
2591
2592 @begin{Example}@Keepnext
2593 @key(function) Length (Format : @key(in) Packed_Format) @key(return) Natural;
2594 @end{Example}
2595 @Trailing@;This function returns the minimal length of a Packed_Decimal value
2596 sufficient to hold any value of type Num when represented as Format.
2597
2598 @begin{Example}@Keepnext
2599 @key(function) To_Decimal (Item : @key(in) Packed_Decimal;
2600 Format : @key(in) Packed_Format) @key(return) Num;
2601 @end{Example}
2602 @Trailing@;Produces a value of type Num corresponding to Item as represented by
2603 Format. Num'Scale is the number of digits after the assumed radix point
2604 in Item. Conversion_Error is propagated if the value represented by Item is
2605 outside the range of Num.
2606
2607 @begin{Example}@Keepnext
2608 @key(function) To_Packed (Item : @key(in) Num;
2609 Format : @key(in) Packed_Format) @key(return) Packed_Decimal;
2610 @end{Example}
2611 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0067],ARef=[AI95-00072-01]}
2612 @Trailing@;This function returns the Packed_Decimal value for Item, represented in
2613 accordance with Format.
2614 @Chg{New=[The length of the returned value is Length(Format), and the
2615 lower bound is 1. ],Old=[]}Conversion_Error is propagated if Num is negative
2616 and Format is Packed_Unsigned.
2617
2618 @begin{Example}@Keepnext
2619 @key(function) Valid (Item : @key(in) Byte_Array;
2620 Format : @key(in) Binary_Format) @key(return) Boolean;
2621 @end{Example}
2622 @Trailing@;This function returns True if Item has a value consistent with Format,
2623 and False otherwise.
2624 @begin{ramification}
2625 This function returns False only when the represented
2626 value is outside the range of Num.@end{ramification}
2627
2628 @begin{Example}@Keepnext
2629 @key(function) Length (Format : @key(in) Binary_Format) @key(return) Natural;
2630 @end{Example}
2631 @Trailing@;This function returns the minimal length of a Byte_Array value
2632 sufficient to hold any value of type Num when represented as Format.
2633
2634 @begin{Example}@Keepnext
2635 @key(function) To_Decimal (Item : @key(in) Byte_Array;
2636 Format : @key(in) Binary_Format) @key(return) Num;
2637 @end{Example}
2638 @Trailing@;Produces a value of type Num corresponding to Item as represented by
2639 Format. Num'Scale is the number of digits after the assumed radix point
2640 in Item. Conversion_Error is propagated if the value represented by Item is
2641 outside the range of Num.
2642
2643 @begin{Example}@Keepnext
2644 @key(function) To_Binary (Item : @key(in) Num;
2645 Format : @key(in) Binary_Format) @key(return) Byte_Array;
2646 @end{Example}
2647 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0067],ARef=[AI95-00072-01]}
2648 @Trailing@;This function returns the Byte_Array value for Item, represented in
2649 accordance with Format.
2650 @Chg{New=[The length of the returned value is Length(Format), and the
2651 lower bound is 1.],Old=[]}
2652
2653 @begin{Example}@Keepnext
2654 @key(function) To_Decimal (Item : @key(in) Binary) @key(return) Num;
2655 @comment{Blank line}
2656 @key(function) To_Decimal (Item : @key(in) Long_Binary) @key(return) Num;
2657 @end{Example}
2658 @Trailing@;These functions convert from COBOL binary format to a corresponding
2659 value of the decimal type Num. Conversion_Error is propagated if Item is
2660 too large for Num.
2661 @begin{Ramification}
2662 There is no rescaling performed on the conversion. That
2663 is, the returned value in each case is a @lquotes@;bit copy@rquotes@; if Num has a
2664 binary radix. The programmer is responsible for maintaining the correct
2665 scale.
2666 @end{ramification}
2667
2668 @begin{Example}@Keepnext
2669 @key(function) To_Binary (Item : @key(in) Num) @key(return) Binary;
2670 @comment{Blank line}
2671 @key(function) To_Long_Binary (Item : @key(in) Num) @key(return) Long_Binary;
2672 @end{Example}
2673
2674 These functions convert from Ada decimal to COBOL binary format.
2675 Conversion_Error is propagated if the value of Item is too large to be
2676 represented in the result type.
2677 @begin{discussion}
2678 One style of interface supported for COBOL, similar to
2679 what is provided for C, is the ability to call and pass parameters to an
2680 existing COBOL program. Thus the interface package supplies types
2681 that can be used in an Ada program as parameters to subprograms whose
2682 bodies will be in COBOL. These types map to COBOL's alphanumeric and
2683 numeric data categories.
2684
2685 Several types are provided for support of alphanumeric data.
2686 Since COBOL's run-time character
2687 set is not necessarily the same as Ada's, Interfaces.COBOL declares
2688 an implementation-defined character type
2689 COBOL_Character, and mappings
2690 between Character and COBOL_Character.
2691 These mappings are visible variables (rather than, say,
2692 functions or constant arrays),
2693 since in the situation where
2694 COBOL_Character is EBCDIC, the
2695 flexibility of dynamically modifying the mappings is needed.
2696 Corresponding to COBOL's alphanumeric data is the string
2697 type Alphanumeric.
2698
2699 Numeric data may have either a @lquotes@;display@rquotes@; or @lquotes@;computational@rquotes@; representation
2700 in COBOL. On the Ada side, the data is of a decimal fixed point type.
2701 Passing an Ada decimal data item to
2702 a COBOL program requires conversion from the Ada decimal type to some type
2703 that reflects the representation expected on the COBOL side.
2704 @begin{Itemize}
2705 Computational Representation
2706
2707 @NoPrefix@;Floating point representation is modeled by Ada floating point types,
2708 Floating and Long_Floating. Conversion between these types and Ada decimal
2709 types is obtained directly, since the type name serves as a conversion
2710 function.
2711
2712 @NoPrefix@;Binary representation is modeled by an Ada integer type, Binary, and
2713 possibly other types such as Long_Binary. Conversion between, say, Binary
2714 and a decimal type is through functions from an instantiation of the
2715 generic package Decimal_Conversions.
2716
2717 @NoPrefix@;Packed decimal representation is modeled by the Ada array type Packed_Decimal.
2718 Conversion between packed decimal and a decimal type is through functions
2719 from an instantiation of the generic package Decimal_Conversions.
2720
2721 Display Representation
2722
2723 @NoPrefix@;Display representation for numeric data
2724 is modeled by the array type Numeric.
2725 Conversion between display representation and a decimal type is through
2726 functions from an instantiation of the generic package Decimal_Conversions.
2727 A parameter to the conversion function indicates the desired interpretation
2728 of the data (e.g., signed leading separate, etc.)
2729 @end{Itemize}
2730
2731 Pragma Convention(COBOL, T) may be applied to a record type T to direct the
2732 compiler to choose a COBOL-compatible representation for objects of the
2733 type.
2734
2735 The package Interfaces.COBOL allows the
2736 Ada programmer to deal with data from files (or databases) created by a
2737 COBOL program. For data that is alphanumeric, or in display
2738 or packed decimal format, the
2739 approach is the same as for passing parameters (instantiate
2740 Decimal_Conversions to obtain the needed conversion functions). For binary
2741 data, the external representation is treated as a Byte array, and an
2742 instantiation of Decimal_IO produces a package that declares the needed
2743 conversion functions. A parameter to the conversion function indicates the
2744 desired interpretation of the data (e.g., high- versus low-order byte
2745 first).
2746 @end{discussion}
2747 @end{DescribeCode}
2748 @end{StaticSem}
2749
2750 @begin{ImplReq}
2751 An implementation shall support pragma Convention with
2752 a COBOL @i[convention]_@nt[identifier] for a COBOL-eligible type
2753 (see @RefSecNum(Interfacing Pragmas)).
2754 @begin{ramification}
2755 An implementation supporting this package shall ensure that if the bounds of
2756 a Packed_Decimal, Alphanumeric, or Numeric variable are static,
2757 then the representation of the
2758 object comprises solely the array components (that is, there is no implicit
2759 run-time @lquotes@;descriptor@rquotes@;
2760 that is part of the object).
2761 @end{ramification}
2762 @end{ImplReq}
2763
2764 @begin{ImplPerm}
2765 An implementation may
2766 provide additional constants of the private types
2767 Display_Format, Binary_Format, or Packed_Format.
2768 @begin{Reason}
2769 This is to allow exploitation of other external formats that may
2770 be available in the COBOL implementation.@end{reason}
2771
2772 An implementation may
2773 provide further floating point and integer types
2774 in Interfaces.COBOL to match additional native COBOL types,
2775 and may also supply corresponding conversion functions in the
2776 generic package Decimal_Conversions.
2777 @end{ImplPerm}
2778
2779 @begin{ImplAdvice}
2780 An Ada implementation should support the following interface
2781 correspondences between Ada and COBOL.
2782 @begin[itemize]
2783 An Ada @key[access] T parameter
2784 is passed as a
2785 @lquotes@;BY REFERENCE@rquotes@; data item of the COBOL type corresponding
2786 to T.
2787
2788 An Ada @key[in] scalar parameter is passed as a @lquotes@;BY CONTENT@rquotes@; data item
2789 of the corresponding COBOL type.
2790
2791 Any other Ada parameter is passed as a
2792 @lquotes@;BY REFERENCE@rquotes@; data item of the COBOL type corresponding
2793 to the Ada parameter type; for scalars, a local copy is
2794 used if necessary to ensure by-copy semantics.
2795 @end[itemize]
2796 @ChgImplAdvice{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],
2797 Text=[If COBOL interfacing is supported, the interface correspondences between
2798 Ada and COBOL should be supported.]}]}
2799 @end{ImplAdvice}
2800
2801 @begin[Notes]
2802 An implementation is not required to support pragma Convention
2803 for access types, nor is it required to support pragma Import, Export
2804 or Convention for functions.
2805 @begin{reason}
2806 COBOL does not have a pointer facility, and a COBOL program
2807 does not return a value.@end{reason}
2808
2809 If an Ada subprogram is exported to COBOL, then a call from COBOL
2810 call may specify
2811 either @lquotes@;BY CONTENT@rquotes@; or @lquotes@;BY REFERENCE@rquotes@;.
2812 @end[Notes]
2813
2814 @begin{Examples}
2815 @Leading@Keepnext@i{Examples of Interfaces.COBOL:}
2816 @begin{Example}
2817 @key(with) Interfaces.COBOL;
2818 @key(procedure) Test_Call @key(is)
2819
2820 @RI{-- Calling a foreign COBOL program}
2821 @RI{-- Assume that a COBOL program PROG has the following declaration}
2822 @RI{-- in its LINKAGE section:}
2823 @RI{-- 01 Parameter-Area}
2824 @RI{-- 05 NAME PIC X(20).}
2825 @RI{-- 05 SSN PIC X(9).}
2826 @RI{-- 05 SALARY PIC 99999V99 USAGE COMP.}
2827 @RI{-- The effect of PROG is to update SALARY based on some algorithm}
2828
2829 @key(package) COBOL @key(renames) Interfaces.COBOL;
2830
2831 @key(type) Salary_Type @key(is) @key(delta) 0.01 @key(digits) 7;
2832
2833 @key(type) COBOL_Record @key(is)
2834 @key(record)
2835 Name : COBOL.Numeric(1..20);
2836 SSN : COBOL.Numeric(1..9);
2837 Salary : COBOL.Binary; @RI{-- Assume Binary = 32 bits}
2838 @key(end) @key(record);
2839 @key(pragma) Convention (COBOL, COBOL_Record);
2840
2841 @key(procedure) Prog (Item : @key(in) @key(out) COBOL_Record);
2842 @key(pragma) Import (COBOL, Prog, "PROG");
2843
2844 @key(package) Salary_Conversions @key(is)
2845 @key(new) COBOL.Decimal_Conversions(Salary_Type);
2846
2847 Some_Salary : Salary_Type := 12_345.67;
2848 Some_Record : COBOL_Record :=
2849 (Name => "Johnson, John ",
2850 SSN => "111223333",
2851 Salary => Salary_Conversions.To_Binary(Some_Salary));
2852
2853 @key(begin)
2854 Prog (Some_Record);
2855 ...
2856 @key(end) Test_Call;
2857 @end{Example}
2858
2859 @begin{Example}
2860 @key(with) Interfaces.COBOL;
2861 @key(with) COBOL_Sequential_IO; @RI{-- Assumed to be supplied by implementation}
2862 @key(procedure) Test_External_Formats @key(is)
2863
2864 @RI{-- Using data created by a COBOL program}
2865 @RI{-- Assume that a COBOL program has created a sequential file with}
2866 @RI{-- the following record structure, and that we need to}
2867 @RI{-- process the records in an Ada program}
2868 @RI{-- 01 EMPLOYEE-RECORD}
2869 @RI{-- 05 NAME PIC X(20).}
2870 @RI{-- 05 SSN PIC X(9).}
2871 @RI{-- 05 SALARY PIC 99999V99 USAGE COMP.}
2872 @RI{-- 05 ADJUST PIC S999V999 SIGN LEADING SEPARATE.}
2873 @RI{-- The COMP data is binary (32 bits), high-order byte first}
2874
2875 @key(package) COBOL @key(renames) Interfaces.COBOL;
2876
2877 @key(type) Salary_Type @key(is) @key(delta) 0.01 @key(digits) 7;
2878 @key(type) Adjustments_Type @key(is) @key(delta) 0.001 @key(digits) 6;
2879
2880 @key(type) COBOL_Employee_Record_Type @key(is) @RI{-- External representation}
2881 @key(record)
2882 Name : COBOL.Alphanumeric(1..20);
2883 SSN : COBOL.Alphanumeric(1..9);
2884 Salary : COBOL.Byte_Array(1..4);
2885 Adjust : COBOL.Numeric(1..7); @RI{-- Sign and 6 digits}
2886 @key(end) @key(record);
2887 @key(pragma) Convention (COBOL, COBOL_Employee_Record_Type);
2888
2889 @key(package) COBOL_Employee_IO @key(is)
2890 @key(new) COBOL_Sequential_IO(COBOL_Employee_Record_Type);
2891 @key(use) COBOL_Employee_IO;
2892
2893 COBOL_File : File_Type;
2894
2895 @key(type) Ada_Employee_Record_Type @key(is) @RI{-- Internal representation}
2896 @key(record)
2897 Name : String(1..20);
2898 SSN : String(1..9);
2899 Salary : Salary_Type;
2900 Adjust : Adjustments_Type;
2901 @key(end) @key(record);
2902
2903 COBOL_Record : COBOL_Employee_Record_Type;
2904 Ada_Record : Ada_Employee_Record_Type;
2905
2906 @key(package) Salary_Conversions @key(is)
2907 @key(new) COBOL.Decimal_Conversions(Salary_Type);
2908 @key(use) Salary_Conversions;
2909
2910 @key(package) Adjustments_Conversions @key(is)
2911 @key(new) COBOL.Decimal_Conversions(Adjustments_Type);
2912 @key(use) Adjustments_Conversions;
2913
2914 @key(begin)
2915 Open (COBOL_File, Name => "Some_File");
2916
2917 @key(loop)
2918 Read (COBOL_File, COBOL_Record);
2919
2920 Ada_Record.Name := To_Ada(COBOL_Record.Name);
2921 Ada_Record.SSN := To_Ada(COBOL_Record.SSN);
2922 Ada_Record.Salary :=
2923 To_Decimal(COBOL_Record.Salary, COBOL.High_Order_First);
2924 Ada_Record.Adjust :=
2925 To_Decimal(COBOL_Record.Adjust, COBOL.Leading_Separate);
2926 ... @RI{-- Process Ada_Record}
2927 @key(end) @key(loop);
2928 @key(exception)
2929 @key[when] End_Error => ...
2930 @key(end) Test_External_Formats;
2931 @end{Example}
2932 @end{Examples}
2933
2934 @begin{DiffWord95}
2935 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0066],ARef=[AI95-00071-01]}
2936 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Corrected the definition of
2937 Valid to match COBOL.]}
2938
2939 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0067],ARef=[AI95-00072-01]}
2940 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Specified the bounds of the
2941 results of To_Display, To_Packed, and To_Binary.]}
2942 @end{DiffWord95}
2943
2944 @LabeledClause{Interfacing with Fortran}
2945
2946 @begin{Intro}
2947 @Defn{interface to Fortran}
2948 @Defn{Fortran interface}
2949 The facilities relevant to interfacing with the Fortran language
2950 are the package Interfaces.Fortran and support for the
2951 Import, Export and Convention pragmas with
2952 @i{convention}_@nt{identifier} Fortran.
2953
2954 The package Interfaces.Fortran defines Ada types whose representations are
2955 identical to the default representations of the Fortran intrinsic types
2956 Integer, Real, Double Precision, Complex, Logical, and Character in a
2957 supported Fortran implementation. These Ada types can therefore be used to
2958 pass objects between Ada and Fortran programs.
2959 @end{Intro}
2960
2961 @begin{StaticSem}
2962 @Leading@Keepnext@;The library package Interfaces.Fortran has the following
2963 declaration:
2964 @begin{Example}
2965 @key[with] Ada.Numerics.Generic_Complex_Types; @RI{-- see @RefSecNum{Complex Types}}
2966 @key[pragma] Elaborate_All(Ada.Numerics.Generic_Complex_Types);
2967 @key[package] Interfaces.Fortran @key[is]@ChildUnit{Parent=[Interfaces],Child=[Fortran]}
2968 @key[pragma] Pure(Fortran);
2969
2970 @key[type] @AdaTypeDefn{Fortran_Integer} @key[is] @key[range] @RI{implementation-defined};
2971
2972 @key[type] @AdaTypeDefn{Real} @key[is] @key[digits] @RI{implementation-defined};
2973 @key[type] @AdaTypeDefn{Double_Precision} @key[is] @key[digits] @RI{implementation-defined};
2974
2975 @key[type] @AdaTypeDefn{Logical} @key[is] @key[new] Boolean;
2976
2977 @key[package] @AdaPackDefn{Single_Precision_Complex_Types} @key[is]
2978 @key[new] Ada.Numerics.Generic_Complex_Types (Real);
2979
2980 @key[type] @AdaTypeDefn{Complex} @key[is] @key[new] Single_Precision_Complex_Types.Complex;
2981
2982 @key[subtype] @AdaSubtypeDefn{Name=[Imaginary],Of=[Imaginary]} @key[is] Single_Precision_Complex_Types.Imaginary;
2983 @AdaObjDefn{i} : Imaginary @key[renames] Single_Precision_Complex_Types.i;
2984 @AdaObjDefn{j} : Imaginary @key[renames] Single_Precision_Complex_Types.j;
2985
2986 @key[type] @AdaTypeDefn{Character_Set} @key[is] @RI{implementation-defined character type};
2987
2988 @key[type] @AdaTypeDefn{Fortran_Character} @key[is] @key[array] (Positive @key[range] <>) @key[of] Character_Set;
2989 @key[pragma] Pack (Fortran_Character);
2990
2991 @key[function] @AdaSubDefn{To_Fortran} (Item : @key[in] Character) @key[return] Character_Set;
2992 @key[function] @AdaSubDefn{To_Ada} (Item : @key[in] Character_Set) @key[return] Character;
2993
2994 @key(function) @AdaSubDefn{To_Fortran} (Item : @key(in) String) @key(return) Fortran_Character;
2995 @key(function) @AdaSubDefn{To_Ada} (Item : @key(in) Fortran_Character) @key(return) String;
2996
2997 @key(procedure) @AdaSubDefn{To_Fortran} (Item : @key(in) String;
2998 Target : @key(out) Fortran_Character;
2999 Last : @key(out) Natural);
3000
3001 @key(procedure) @AdaSubDefn{To_Ada} (Item : @key(in) Fortran_Character;
3002 Target : @key(out) String;
3003 Last : @key(out) Natural);
3004
3005 @key[end] Interfaces.Fortran;
3006 @end{Example}
3007 @ChgImplDef{Version=[1],Kind=[Added],Text=[@Chg{New=[The types Fortran_Integer,
3008 Real, Double_Precision, and Character_Set in Interfaces.Fortran.],Old=[]}]}
3009 @begin{Ramification}
3010 The means by which the Complex type is provided in Interfaces.Fortran
3011 creates a dependence of Interfaces.Fortran on Numerics.Generic_Complex_Types
3012 (see @RefSecNum{Complex Types}).
3013 This dependence is intentional and unavoidable,
3014 if the Fortran-compatible Complex type is to be useful in Ada code
3015 without duplicating facilities defined elsewhere.
3016 @end{Ramification}
3017
3018 The types Fortran_Integer, Real, Double_Precision, Logical,
3019 Complex, and Fortran_Character are Fortran-compatible.
3020
3021 The To_Fortran and To_Ada functions map between the
3022 Ada type Character and the Fortran type Character_Set,
3023 and also between the Ada type String and the Fortran type
3024 Fortran_Character.
3025 The To_Fortran and To_Ada procedures
3026 have analogous effects to the string conversion subprograms
3027 found in Interfaces.COBOL.
3028 @end{StaticSem}
3029
3030 @begin[ImplReq]
3031 An implementation shall support @nt[pragma] Convention
3032 with a Fortran @i[convention]_@nt[identifier] for a Fortran-eligible
3033 type (see @RefSecNum(Interfacing Pragmas)).
3034 @end[ImplReq]
3035
3036 @begin{ImplPerm}
3037 An implementation may add additional declarations to the Fortran interface
3038 packages. For example, the Fortran interface package for an implementation of
3039 Fortran 77 (ANSI X3.9-1978) that defines types like Integer*@i{n}, Real*@i{n},
3040 Logical*@i{n}, and Complex*@i{n} may contain the declarations of types named
3041 Integer_@!Star_@i{n}, Real_@!Star_@i{n}, Logical_@!Star_@i{n}, and
3042 Complex_@!Star_@i{n}. (This convention should not apply to Character*@i{n}, for
3043 which the Ada analog is the constrained array subtype Fortran_Character
3044 (1..@i{n}).) Similarly, the Fortran interface package for an implementation of
3045 Fortran 90 that provides multiple @i{kinds} of intrinsic types, e.g. Integer
3046 (Kind=@i{n}), Real (Kind=@i{n}), Logical (Kind=@i{n}), Complex (Kind=@i{n}),
3047 and Character (Kind=@i{n}), may contain the declarations of types
3048 with the recommended names
3049 Integer_Kind_@i{n}, Real_Kind_@i{n}, Logical_Kind_@i{n}, Complex_Kind_@i{n},
3050 and Character_Kind_@i{n}.
3051 @begin[discussion]
3052 Implementations may add auxiliary declarations as needed to assist in the
3053 declarations of additional Fortran-compatible types. For example, if a double
3054 precision complex type is defined, then Numerics.@!Generic_@!Complex_@!Types may be
3055 instantiated for the double precision type. Similarly, if a wide character
3056 type is defined to match a Fortran 90 wide character type (accessible in
3057 Fortran 90 with the Kind modifier), then an auxiliary character set may be
3058 declared to serve as its component type.
3059 @end[discussion]
3060 @end{ImplPerm}
3061
3062 @begin{ImplAdvice}
3063 @Leading@;An Ada implementation should support the following interface
3064 correspondences between Ada and Fortran:
3065 @begin[itemize]
3066 An Ada procedure corresponds to
3067 a Fortran subroutine.
3068
3069 An Ada function corresponds to a Fortran function.
3070
3071 An Ada parameter of an elementary, array, or
3072 record type T is passed as a T@-(F) argument to a Fortran procedure,
3073 where T@-(F) is the Fortran type corresponding to the
3074 Ada type T, and where the INTENT attribute of the corresponding
3075 dummy argument matches the Ada formal parameter mode;
3076 the Fortran
3077 implementation's parameter passing conventions are used.
3078 For elementary types, a local copy is used if necessary to ensure
3079 by-copy semantics.
3080
3081 An Ada parameter of an access-to-subprogram type
3082 is passed as a reference to a Fortran
3083 procedure whose interface corresponds to the designated subprogram's
3084 specification.
3085 @end[itemize]
3086 @ChgImplAdvice{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],
3087 Text=[If Fortran interfacing is supported, the interface correspondences
3088 between Ada and Fortran should be supported.]}]}
3089 @end{ImplAdvice}
3090
3091 @begin[Notes]
3092 An object of a Fortran-compatible record type,
3093 declared in a library package or subprogram,
3094 can correspond to a Fortran common
3095 block; the type also corresponds to
3096 a Fortran @lquotes@;derived type@rquotes@;.
3097 @end[Notes]
3098 @begin{Examples}
3099 @Leading@Keepnext@i{Example of Interfaces.Fortran:}
3100 @begin{Example}
3101 @key[with] Interfaces.Fortran;
3102 @key[use] Interfaces.Fortran;
3103 @key[procedure] Ada_Application @key[is]
3104
3105 @key[type] Fortran_Matrix @key[is] @key[array] (Integer @key[range] <>,
3106 Integer @key[range] <>) @key[of] Double_Precision;
3107 @key[pragma] Convention (Fortran, Fortran_Matrix); @RI{-- stored in Fortran's}
3108 @RI{-- column-major order}
3109 @key[procedure] Invert (Rank : @key[in] Fortran_Integer; X : @key[in] @key[out] Fortran_Matrix);
3110 @key[pragma] Import (Fortran, Invert); @RI{-- a Fortran subroutine}
3111
3112 Rank : @key[constant] Fortran_Integer := 100;
3113 My_Matrix : Fortran_Matrix (1 .. Rank, 1 .. Rank);
3114
3115
3116 @key[begin]
3117
3118 ...
3119 My_Matrix := ...;
3120 ...
3121 Invert (Rank, My_Matrix);
3122 ...
3123
3124 @key[end] Ada_Application;
3125 @end{Example}
3126 @end{Examples}