Debian Codegraph @

source_2020/interface.mss

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