Debian Codegraph @

source_2012/interface.mss

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