Debian Codegraph @

source_2020/07.mss

raw
1 @Part(07, Root="ada.mss")
2
3 @Comment{$Date: 2020/06/03 00:09:00 $}
4 @LabeledSection{Packages}
5
6 @Comment{$Source: e:\\cvsroot/ARM/Source/07.mss,v $}
7 @Comment{$Revision: 1.151 $}
8
9 @begin{Intro}
10 @redundant[@ToGlossaryAlso{Term=<Package>,
11 Text=<Packages are program units that allow the specification of groups of
12 logically related entities.
13 Typically, a package contains the declaration of a type
14 (often a private type or private extension) along with
15 the declarations of primitive subprograms of the type,
16 which can be called from outside the package,
17 while their inner workings remain hidden from outside users.>}
18 @IndexSee{Term=[information hiding],See=(package)}
19 @IndexSee{Term=[encapsulation],See=(package)}
20 @IndexSee{Term=[module],See=(package)}
21 @IndexSeeAlso{Term=[class],See=(package)}]
22 @end{Intro}
23
24 @LabeledClause{Package Specifications and Declarations}
25
26 @begin{Intro}
27 @redundant[A package is generally provided in two parts: a
28 @nt{package_specification} and a @nt{package_body}.
29 Every package has a @nt{package_specification}, but not all packages
30 have a @nt{package_body}.]
31 @end{Intro}
32
33 @begin{Syntax}
34 @Syn{lhs=<package_declaration>,rhs="@Syn2{package_specification};"}
35
36
37 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0183-1]}
38 @Syn{lhs=<package_specification>,rhs="
39 @key{package} @Syn2{defining_program_unit_name}@Chg{Version=[3],New=<
40 [@Syn2{aspect_specification}]>,Old=[]} @key{is}
41 {@Syn2{basic_declarative_item}}
42 [@key{private}
43 {@Syn2{basic_declarative_item}}]
44 @key{end} [[@Syn2{parent_unit_name}.]@Syn2{identifier}]"}
45
46 @begin{SyntaxText}
47 If an @nt{identifier} or @nt{parent_unit_name}.@nt{identifier}
48 appears at the end of a @nt{package_specification},
49 then this sequence of lexical elements shall repeat the
50 @nt{defining_program_unit_name}.
51 @end{SyntaxText}
52 @end{Syntax}
53
54 @begin{Legality}
55 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00434-01]}
56 @PDefn2{Term=[requires a completion], Sec=(@nt{package_declaration})}
57 @PDefn2{Term=[requires a completion], Sec=(@nt{generic_package_declaration})}
58 A @nt{package_declaration} or @nt{generic_package_declaration}
59 requires a completion @Redundant[(a body)]
60 if it contains any @Chg{Version=[2],New=[@nt{basic_declarative_item}],
61 Old=[@nt<declarative_item>]} that requires a completion,
62 but whose completion is not in its @nt{package_specification}.
63 @begin(Honest)
64 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1]}
65 If an implementation supports it,@Chg{Version=[3],New=[],Old=[ a
66 @nt{pragma} Import may substitute
67 for]} the body of a package or generic package@Chg{Version=[3],New=[
68 may be imported (using aspect Import, see @RefSecNum{Interfacing Aspects}),
69 in which case no explicit body is allowed],Old=[]}.
70 @end(Honest)
71 @end{Legality}
72
73 @begin{StaticSem}
74 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00420-01],ARef=[AI95-00434-01]}
75 @PDefn2{Term=[visible part], Sec=<of a package
76 (other than a generic formal package)>}
77 The first list of @Chg{Version=[2],New=[@nt{basic_declarative_item}s],
78 Old=[@nt{declarative_item}s]} of a
79 @nt{package_specification} of a package other than a generic formal package
80 is called the @i{visible part} of the package.
81 @Redundant[@PDefn2{Term=[private part], Sec=(of a package)}
82 The optional list of @Chg{Version=[2],New=[@nt{basic_declarative_item}s],
83 Old=[@nt{declarative_item}s]} after the reserved word
84 @key{private} (of any @nt{package_specification}) is called the
85 @i{private part} of the package.
86 If the reserved
87 word @key{private} does not appear, the package has an implicit empty
88 private part.]@Chg{Version=[2],New=[ Each list of @nt{basic_declarative_item}s
89 of a @nt{package_specification} forms a @i{declaration list} of the
90 package.@PDefn2{Term=[declaration list],Sec=(package_specification)}],Old=[]}
91
92 @begin{Ramification}
93 This definition of visible part does not apply to generic formal
94 packages @em @RefSecNum{Formal Packages} defines
95 the visible part of a generic formal package.
96
97 The implicit empty private part is important because certain
98 implicit declarations occur there if the package is a child package,
99 and it defines types in its visible part that are derived from,
100 or contain as components, private types declared within the
101 parent package. These implicit declarations are visible
102 in children of the child package.
103 See @RefSecNum(Compilation Units - Library Units).
104 @end{Ramification}
105
106 @redundant[An entity declared in the private part of a package is visible
107 only within the declarative region of the package itself
108 (including any child units @em
109 see @RefSecNum{Compilation Units - Library Units}).
110 In contrast, expanded names denoting
111 entities declared in the visible part can be used even outside the
112 package; furthermore, direct visibility of such entities can be
113 achieved by means of @nt{use_clause}s
114 (see @RefSecNum{Selected Components} and @RefSecNum{Use Clauses}).]
115 @end{StaticSem}
116
117 @begin{RunTime}
118 @PDefn2{Term=[elaboration], Sec=(package_declaration)}
119 The elaboration of a @nt{package_declaration} consists of the elaboration of
120 its @nt{basic_declarative_item}s in the given order.
121 @end{RunTime}
122
123 @begin{Notes}
124 The visible part of a package contains all the information that
125 another program unit is able to know about the package.
126
127 If a declaration occurs immediately within the specification
128 of a package, and the declaration has a corresponding completion that
129 is a body,
130 then that body has to occur immediately within the body of
131 the package.
132 @begin{TheProof}
133 This follows from the fact that the declaration and completion are
134 required to occur
135 immediately within the same declarative region,
136 and the fact that @ntf{bodies} are disallowed (by the @SyntaxName@;s)
137 in @nt{package_specification}s.
138 This does not apply to instances of generic units,
139 whose bodies can occur in @nt{package_specification}s.
140 @end{TheProof}
141 @end{Notes}
142
143 @begin{Examples}
144 @leading@keepnext@i{Example of a package declaration:}
145 @begin{Example}
146 @key[package] Rational_Numbers @key[is]
147
148 @key[type] Rational @key[is]
149 @key[record]
150 Numerator : Integer;
151 Denominator : Positive;
152 @key[end] @key[record];
153
154 @key[function] "="(X,Y : Rational) @key[return] Boolean;
155
156 @key[function] "/" (X,Y : Integer) @key[return] Rational; --@RI{ to construct a rational number}
157
158 @key[function] "+" (X,Y : Rational) @key[return] Rational;
159 @key[function] "-" (X,Y : Rational) @key[return] Rational;
160 @key[function] "*" (X,Y : Rational) @key[return] Rational;
161 @key[function] "/" (X,Y : Rational) @key[return] Rational;
162 @key[end] Rational_Numbers;
163 @end{Example}
164
165 There are also many examples of package declarations in the predefined
166 language environment
167 (see @RefSecNum{Predefined Language Environment}).
168 @end{Examples}
169
170 @begin{Incompatible83}
171 @Defn{incompatibilities with Ada 83}
172 In Ada 83, a library package is allowed to have a body even if
173 it doesn't need one.
174 In Ada 95, a library package body is either
175 required or forbidden @em never optional.
176 The workaround is to add @key[pragma] Elaborate_Body,
177 or something else requiring a body,
178 to each library package that has a body that isn't otherwise
179 required.
180 @end{Incompatible83}
181
182 @begin{DiffWord83}
183 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
184 We have moved the syntax into this @Chg{Version=[3],New=[subclause],Old=[clause]}
185 and the next @Chg{Version=[3],New=[subclause],Old=[clause]} from
186 RM83-7.1, @lquotes@;Package Structure@rquotes@;, which we have removed.
187
188 RM83 was unclear on the rules about when a package requires a body.
189 For example, RM83-7.1(4) and RM83-7.1(8) clearly forgot about the
190 case of an incomplete type declared in a @nt{package_declaration} but
191 completed in the body.
192 In addition,
193 RM83 forgot to make this rule apply to a generic package.
194 We have corrected these rules.
195 Finally, since we now allow a @nt{pragma} Import for any explicit
196 declaration, the completion rules need to take this into account as
197 well.
198 @end{DiffWord83}
199
200 @begin{DiffWord95}
201 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00420-01]}
202 @ChgAdded{Version=[2],Text=[Defined @lquotes@;declaration list@rquotes
203 to avoid ambiguity in other rules as to whether packages are included.]}
204 @end{DiffWord95}
205
206 @begin{Extend2005}
207 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
208 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
209 An optional @nt{aspect_specification} can be used in a @nt{package_specification}.
210 This is described in @RefSecNum{Aspect Specifications}.]}
211 @end{Extend2005}
212
213
214 @LabeledClause{Package Bodies}
215
216 @begin{Intro}
217 @redundant[In contrast to the entities declared in the visible part of a
218 package, the entities declared in the @nt{package_body} are
219 visible only
220 within the @nt{package_body} itself.
221 As a consequence, a
222 package with a @nt{package_body} can be used for the construction of
223 a group of related subprograms in
224 which the logical operations available to clients are clearly
225 isolated from the internal entities.]
226 @end{Intro}
227
228 @begin{Syntax}
229
230 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0267-1]}
231 @Syn{lhs=<package_body>,rhs="
232 @key{package} @key{body} @Syn2{defining_program_unit_name}@Chg{Version=[3],New=<
233 [@Syn2{aspect_specification}]>,Old=[]} @key{is}
234 @Syn2{declarative_part}
235 [@key{begin}
236 @Syn2{handled_sequence_of_statements}]
237 @key{end} [[@Syn2{parent_unit_name}.]@Syn2{identifier}];"}
238
239 @begin{SyntaxText}
240 If an @nt{identifier} or @nt{parent_unit_name}.@nt{identifier}
241 appears at the end of a @nt{package_body},
242 then this sequence of lexical elements shall repeat the
243 @nt{defining_program_unit_name}.
244 @end{SyntaxText}
245 @end{Syntax}
246
247 @begin{Legality}
248 A @nt{package_body} shall be the completion of a previous
249 @nt{package_@!declaration} or @nt{generic_@!package_@!declaration}.
250 A library @nt{package_@!declaration} or
251 library @nt{generic_@!package_@!declaration}
252 shall not have a body
253 unless it requires a body@Redundant[;
254 @key<pragma> Elaborate_Body can be used to require
255 a @nt<library_@!unit_@!declaration> to have a body
256 (see @RefSecNum{Elaboration Control})
257 if it would not otherwise require one].
258 @begin{Ramification}
259 The first part of the rule forbids a @nt{package_body} from
260 standing alone @em it has to belong to some previous
261 @nt{package_declaration} or @nt{generic_package_declaration}.
262
263 A nonlibrary @nt{package_declaration} or
264 nonlibrary @nt<generic_package_declaration>
265 that does not require a completion may
266 have a corresponding body anyway.
267 @end{Ramification}
268 @end{Legality}
269
270 @begin{StaticSem}
271 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
272 In any @nt{package_body} without @nt{statement}s
273 there is an implicit @nt{null_@!statement}.
274 For any @nt{package_@!declaration} without an explicit completion,
275 there is an implicit @nt{package_@!body} containing a single
276 @nt{null_statement}.
277 For a noninstance, nonlibrary package,
278 this body occurs at the end of the
279 @nt{declarative_@!part} of the innermost enclosing program unit or
280 @nt{block_@!statement};
281 if there are several such packages,
282 the order of the implicit @ntf{package_@!bodies} is unspecified.
283 @PDefn{unspecified}
284 @Redundant[(For an instance, the implicit @nt{package_@!body}
285 occurs at the place of the instantiation
286 (see @RefSecNum{Generic Instantiation}).
287 For a library package, the place is partially determined by the
288 elaboration dependences (see @Chg{Version=[3],New=[Clause],Old=[Section]}
289 @RefSecNum{Program Structure and Compilation Issues}).)]
290 @begin{Discussion}
291 Thus, for example, we can refer to something happening just
292 after the @key{begin} of a @nt{package_body},
293 and we can refer to the @nt{handled_sequence_of_statements}
294 of a @nt{package_body},
295 without worrying about all the optional pieces.
296 The place of the implicit body makes a difference for tasks
297 activated by the package.
298 See also RM83-9.3(5).
299
300 The implicit body would be illegal if explicit in the case of a library
301 package that does not require (and therefore does not allow)
302 a body.
303 This is a bit strange, but not harmful.
304 @end{Discussion}
305 @end{StaticSem}
306
307 @begin{RunTime}
308 @PDefn2{Term=[elaboration], Sec=(nongeneric package_body)}
309 For the elaboration of a nongeneric @nt{package_body},
310 its @nt{declarative_@!part} is first
311 elaborated, and its @nt{handled_@!sequence_of_@!statements} is then executed.
312 @end{RunTime}
313
314 @begin{Notes}
315 A variable declared in the body of a package is only visible
316 within this body and, consequently, its value can only be
317 changed within the @nt{package_body}. In the absence of local tasks,
318 the value of such a variable remains unchanged between calls issued
319 from outside the package to subprograms declared in the visible part.
320 The properties of such a variable are similar to those of a @lquotes@;static@rquotes@;
321 variable of C.
322
323 The elaboration of the body of a subprogram explicitly declared
324 in the visible part of a package is caused by the elaboration of the
325 body of the package. Hence a call of such a subprogram by an
326 outside program unit raises the exception Program_Error if the call
327 takes place before the elaboration of the
328 @nt{package_body} (see @RefSecNum{Declarative Parts}).
329 @end{Notes}
330
331 @begin{Examples}
332 @leading@keepnext@i{Example of a package body
333 (see @RefSecNum{Package Specifications and Declarations}):}
334 @begin{Example}
335 @key[package] @key[body] Rational_Numbers @key[is]
336
337 @key[procedure] Same_Denominator (X,Y : @key[in] @key[out] Rational) @key[is]
338 @key[begin]
339 --@RI{ reduces X and Y to the same denominator:}
340 ...
341 @key[end] Same_Denominator;
342
343 @key[function] "="(X,Y : Rational) @key[return] Boolean @key[is]
344 U : Rational := X;
345 V : Rational := Y;
346 @key[begin]
347 Same_Denominator (U,V);
348 @key[return] U.Numerator = V.Numerator;
349 @key[end] "=";
350
351 @key[function] "/" (X,Y : Integer) @key[return] Rational @key[is]
352 @key[begin]
353 @key[if] Y > 0 @key[then]
354 @key[return] (Numerator => X, Denominator => Y);
355 @key[else]
356 @key[return] (Numerator => -X, Denominator => -Y);
357 @key[end] @key[if];
358 @key[end] "/";
359
360 @key[function] "+" (X,Y : Rational) @key[return] Rational @key[is] ... @key[end] "+";
361 @key[function] "-" (X,Y : Rational) @key[return] Rational @key[is] ... @key[end] "-";
362 @key[function] "*" (X,Y : Rational) @key[return] Rational @key[is] ... @key[end] "*";
363 @key[function] "/" (X,Y : Rational) @key[return] Rational @key[is] ... @key[end] "/";
364
365 @key[end] Rational_Numbers;
366 @end{Example}
367 @end{Examples}
368
369 @begin{DiffWord83}
370 The syntax rule for @nt{package_body} now uses the syntactic category
371 @nt{handled_sequence_of_statements}.
372
373 The @nt{declarative_part} of a @nt{package_body} is now required;
374 that doesn't make any real difference,
375 since a @nt{declarative_part} can be empty.
376
377 RM83 seems to have forgotten to say that a @nt{package_body} can't
378 stand alone, without a previous declaration.
379 We state that rule here.
380
381 RM83 forgot to restrict the definition of elaboration of
382 @ntf{package_bodies} to nongeneric ones.
383 We have corrected that omission.
384
385 The rule about implicit bodies (from RM83-9.3(5))
386 is moved here, since it is more generally applicable.
387 @end{DiffWord83}
388
389 @begin{Extend2005}
390 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0267-1]}
391 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
392 An optional @nt{aspect_specification} can be used in a @nt{package_body}.
393 This is described in @RefSecNum{Aspect Specifications}.]}
394 @end{Extend2005}
395
396 @LabeledClause{Private Types and Private Extensions}
397
398 @begin{Intro}
399 @redundant[The declaration (in the visible part of a
400 package) of a type as a private type or private extension
401 serves to separate the characteristics that can be used
402 directly by outside program units (that is, the logical properties)
403 from other characteristics whose direct use is confined to the
404 package (the details of the definition of the type itself).
405 See @RefSecNum(Type Extensions) for an overview of type extensions.
406 @Defn{private types and private extensions}
407 @IndexSee{Term=[information hiding],See=(private types and private extensions)}
408 @IndexSee{Term=[opaque type],See=(private types and private extensions)}
409 @IndexSee{Term=[abstract data type (ADT)],See=(private types and private extensions)}
410 @IndexSee{Term=[ADT (abstract data type)],See=(private types and private extensions)}]
411 @end{Intro}
412
413 @begin{MetaRules}
414 A private (untagged) type can be thought of as a record type
415 with the type of its single (hidden) component being the full view.
416
417 A private tagged type can be thought of as a private extension
418 of an anonymous parent with no components. The only
419 dispatching operation of the parent is equality
420 (although the Size attribute, and, if nonlimited, assignment are allowed,
421 and those will presumably be implemented in terms of dispatching).
422 @end{MetaRules}
423
424 @begin{Syntax}
425 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0183-1]}
426 @Syn{lhs=<private_type_declaration>,rhs="
427 @key{type} @Syn2{defining_identifier} [@Syn2{discriminant_part}] @key{is} [[@key{abstract}] @key{tagged}] [@key{limited}] @key{private}@Chg{Version=[3],New=<
428 [@Syn2{aspect_specification}]>,Old=[]};"}
429
430 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
431 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0183-1]}
432 @Syn{lhs=<private_extension_declaration>,rhs="
433 @key{type} @Syn2{defining_identifier} [@Syn2{discriminant_part}] @key{is}
434 [@key{abstract}] @Chg{Version=[2],New=<[@key{limited} | @key{synchronized}]>,Old=[]} @key{new} @SynI(ancestor_)@Syn2{subtype_indication}@Chg{Version=[2],New=<
435 [@key{and} @Syn2[interface_list]]>,Old=<>} @key{with private}@Chg{Version=[3],New=<
436 [@Syn2{aspect_specification}]>,Old=[]};"}
437 @end{Syntax}
438
439 @begin{Legality}
440 @ChgNote{We don't mark this as a change, since it only involves the AARM}
441 @Defn2{Term=[partial view], Sec=(of a type)}
442 @PDefn2{Term=[requires a completion], Sec=(declaration of a partial view)}
443 A @nt<private_type_declaration>
444 or @nt<private_extension_declaration>
445 declares a @i{partial view} of the type;
446 such a declaration is allowed only as a
447 @nt{declarative_item} of the visible part of a package,
448 and it requires a completion,
449 which shall be a @nt{full_type_declaration} that occurs as
450 a @nt{declarative_item} of the private part of the package.
451 @Chg{Version=[2],New=[],Old=[@Defn2{Term=[full view], Sec=(of a type)}]}
452 @Redundant[@ChgNote{This really should be a change, but that's too hard.}
453 The view of the type declared by the @nt<full_type_declaration>
454 is called the @i(full view).]
455 A generic formal private type or a
456 generic formal private extension is also a partial view.
457 @begin(Honest)
458 A private type can also be@Chg{Version=[3],New=[
459 imported (using aspect Import, see @RefSecNum{Interfacing Aspects}),
460 in which case no completion is allowed],Old=[completed by a @nt{pragma}
461 Import]}, if supported by an implementation.
462 @end(Honest)
463 @begin{Reason}
464 We originally used the term @lquotes@;private view,@rquotes@; but this was
465 easily confused with the view provided @i(from) the private part, namely the
466 full view.
467 @end{Reason}
468 @begin{TheProof}
469 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00326-01]}
470 @ChgAdded{Version=[2],Text=[Full view is now defined in
471 @RefSec{Type Declarations}, as all types now have them.]}
472 @end{TheProof}
473
474 @Redundant[A type shall be completely defined before it is frozen
475 (see @RefSecNum{Completions of Declarations} and
476 @RefSecNum{Freezing Rules}).
477 Thus, neither the declaration
478 of a variable of a partial view of a type, nor the creation by an
479 @nt{allocator} of an object of the partial view are allowed before
480 the full declaration of the type.
481 Similarly, before the full declaration, the name of the partial view
482 cannot be used in a @nt{generic_instantiation} or in a
483 representation item.]
484 @begin{TheProof}
485 This rule is stated officially in
486 @RefSec{Completions of Declarations}.
487 @end{TheProof}
488
489
490
491 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
492 @Redundant[A private type is limited if its declaration includes
493 the reserved word @key[limited];
494 a private extension is limited if its ancestor type is @Chg{Version=[2],
495 New=[a limited type that is not an interface type, or if the reserved word
496 @key{limited} or @key{synchronized} appears in its
497 definition],Old=[limited]}.]
498 If the partial view is nonlimited, then
499 the full view shall be nonlimited.
500 If a tagged partial view is limited,
501 then the full view shall be limited.
502 @Redundant[On the other hand,
503 if an untagged partial view is limited,
504 the full view may be limited or nonlimited.]
505
506 If the partial view is tagged,
507 then the full view shall be tagged.
508 @Redundant[On the other hand, if the partial view is untagged,
509 then the full view may be tagged or untagged.]
510 In the case where the partial view is untagged and the full view is
511 tagged,
512 no derivatives of the partial view are allowed within the immediate
513 scope of the partial view;
514 @Redundant[derivatives of the full view are allowed.]
515 @begin{Ramification}
516 Note that deriving from a partial view within its immediate scope
517 can only occur in a package that is a child of the one where the partial
518 view is declared.
519 The rule implies that in the visible part of a public child package,
520 it is impossible to derive from an untagged private type declared in the
521 visible part of the parent package in the case where the full
522 view of the parent type turns out to be tagged.
523 We considered a model in which the derived type was implicitly
524 redeclared at the earliest place within its immediate scope where
525 characteristics needed to be added.
526 However, we rejected that model, because (1) it would imply that (for an
527 untagged type) subprograms explicitly declared after the derived type
528 could be inherited, and (2) to make this model work for composite types
529 as well, several implicit redeclarations would be
530 needed, since new characteristics can become visible one by one;
531 that seemed like too much mechanism.
532 @end{Ramification}
533 @begin{Discussion}
534 The rule for tagged partial views
535 is redundant for partial views that are private extensions,
536 since all extensions of a given ancestor tagged type are tagged,
537 and limited if the ancestor is limited.
538 We phrase this rule partially redundantly to keep its structure parallel
539 with the other rules.
540 @end{Discussion}
541 @begin{Honest}
542 This rule is checked in a generic unit,
543 rather than using the @lquotes@;assume the best@rquotes@; or @lquotes@;assume the worst@rquotes@;
544 method.
545 @end{Honest}
546 @begin{Reason}
547 @leading@;@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}Tagged
548 limited private types have certain capabilities that are
549 incompatible with having assignment for the full view of the type.
550 In particular, tagged limited private types can be extended
551 with @Chg{Version=[2],New=[],Old=[access discriminants and ]}components
552 of a limited type, which works only because assignment is not allowed.
553 Consider the following example:
554 @begin{Example}
555 @key[package] P1 @key[is]
556 @key[type] T1 @key[is] @key[tagged] @key[limited] @key[private];
557 @key[procedure] Foo(X : @key[in] T1'Class);
558 @key[private]
559 @key[type] T1 @key[is] @key[tagged] @key[null] @key[record]; --@RI{ Illegal!}
560 --@RI{ This should say @lquotes@;@key[tagged limited null record]@rquotes@;.}
561 @key[end] P1;
562
563 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{Presentation AI-00114}
564 @key[package] @key[body] P1 @key[is]
565 @key[type] A @key[is] @key[access] T1'Class;
566 Global : A;
567 @key[procedure] Foo(X : @key[in] T1'Class) @key[is]
568 @key[begin]
569 Global := @key[new] T1'Class'(X);
570 --@RI{ This would be illegal if the full view of}
571 --@RI{ T1 were limited, like it's supposed to be.}
572 @key[end] @Chg{New=[Foo],Old=[A]};
573 @key[end] P1;
574
575 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}@key[with] P1;
576 @key[package] P2 @key[is]
577 @key[type] T2(D : @key[access] Integer)@Chg{Version=[2],New=[],Old=[ --@RI{ Trouble!}]}
578 @key[is] @key[new] P1.T1 @key[with]
579 @key[record]
580 My_Task : Some_Task_Type; --@RI{ @Chg{Version=[2],New=[Trouble],Old=[More trouble]}!}
581 @key[end] @key[record];
582 @key[end] P2;
583
584 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{Presentation AI-00114}
585 @key[with] P1;
586 @key[with] P2;
587 @key[procedure] Main @key[is]
588 Local : @key[aliased] Integer;
589 Y : P2.T2(@Chg{New=[D],Old=[A]} => Local'Access);
590 @key[begin]
591 P1.Foo(Y);
592 @key[end] Main;
593 @end{Example}
594
595 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
596 If the above example were legal,
597 we would have succeeded in @Chg{Version=[2],New=[],Old=[making an access
598 value that points to Main.Local after Main has been left,
599 and we would also have succeeded in ]}doing an assignment of a task
600 object, @Chg{Version=[2],New=[],Old=[both of ]}which @Chg{Version=[2],New=[is],
601 Old=[are]} supposed to be @Chg{Version=[2],New=[a no-no],Old=[no-no's]}.
602 @ChgNote{A runtime check prevents the first from being a problem in Ada 2005.}
603
604 This rule is not needed for private extensions,
605 because they inherit their limitedness from their ancestor,
606 and there is a separate rule forbidding limited components of the
607 corresponding record extension if the parent is nonlimited.
608 @end{Reason}
609 @begin{Ramification}
610 @leading@;A type derived from an untagged private type is untagged,
611 even if the full view of the parent is tagged,
612 and even at places that can see the parent:
613 @begin{Example}
614 @key[package] P @key[is]
615 @key[type] Parent @key[is] @key[private];
616 @key[private]
617 @key[type] Parent @key[is] @key[tagged]
618 @key[record]
619 X: Integer;
620 @key[end] @key[record];
621 @key[end] P;
622
623 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{Presentation AI-00114}
624 @Chg{New=[@key[with] P;
625 ],Old=[]}@key[package] Q @key[is]
626 @key[type] T @key[is] @key[new] @Chg{New=[P.],Old=[]}Parent;
627 @key[end] Q;
628
629 @key[with] Q; @key[use] Q;
630 @key[package] @key[body] P @key[is]
631 ... T'Class ... --@RI{ Illegal!}
632 Object: T;
633 ... Object.X ... --@RI{ Illegal!}
634 ... Parent(Object).X ... --@RI{ OK.}
635 @key[end] P;
636 @end{Example}
637
638 The declaration of T declares an untagged view.
639 This view is always untagged, so T'Class is illegal,
640 it would be illegal to extend T, and so forth.
641 The component name X is never visible for this view,
642 although the component is still there @em one
643 can get one's hands on it via a @nt{type_conversion}.
644
645 @end{Ramification}
646
647 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00396-01]}
648 @ChgAdded{Version=[2],Type=[Leading],Text=[If a full type has a partial view
649 that is tagged, then:]}
650 @begin{Itemize}
651 @ChgRef{Version=[2],Kind=[Added]}
652 @ChgAdded{Version=[2],Text=[the partial view shall be a synchronized tagged
653 type (see @RefSecNum{Interface Types}) if and only if the full type is a
654 synchronized tagged type;]}
655
656 @begin{Reason}
657 @ChgRef{Version=[2],Kind=[Added]}
658 @ChgAdded{Version=[2],Text=[ Since we do not allow record extensions of
659 synchronized tagged types, this property has to be visible in the partial
660 view to avoid privacy breaking. Generic formals do not need a similar rule as
661 any extensions are rechecked for legality in the specification, and
662 extensions of tagged formals are always illegal in a generic body.]}
663 @end{Reason}
664
665 @ChgRef{Version=[2],Kind=[Added]}
666 @ChgAdded{Version=[2],Text=[the partial view shall be a descendant of an
667 interface type (see 3.9.4) if and only if the full type is a descendant of the
668 interface type.]}
669
670 @begin{Reason}
671 @ChgRef{Version=[2],Kind=[AddedNormal]}
672 @ChgAdded{Version=[2],KeepNext=[T],Type=[Leading],Text=[Consider the following example:]}
673 @begin{Example}
674 @ChgRef{Version=[2],Kind=[AddedNormal]}
675 @ChgAdded{Version=[2],Text=[@key{package} P @key{is}
676 @key{package} Pkg @key{is}
677 @key{type} Ifc @key{is interface};
678 @key{procedure} Foo (X : Ifc) @key{is abstract};
679 @key{end} Pkg;]}
680
681 @ChgRef{Version=[2],Kind=[AddedNormal]}
682 @ChgAdded{Version=[2],Text=[ @key{type} Parent_1 @key{is tagged null record};]}
683
684 @ChgRef{Version=[2],Kind=[AddedNormal]}
685 @ChgAdded{Version=[2],Text=[ @key{type} T1 @key{is new} Parent_1 @key{with private};
686 @key{private}
687 @key{type} Parent_2 @key{is new} Parent_1 @key{and} Pkg.Ifc @key{with null record};
688 @key{procedure} Foo (X : Parent_2); -- @RI[Foo #1]]}
689
690 @ChgRef{Version=[2],Kind=[AddedNormal]}
691 @ChgAdded{Version=[2],Text=[ @key{type} T1 @key{is new} Parent_2 @key{with null record}; -- @RI[Illegal.]
692 @key{end} P;]}
693
694 @ChgRef{Version=[2],Kind=[AddedNormal]}
695 @ChgAdded{Version=[2],Text=[@key{with} P;
696 @key{package} P_Client @key{is}
697 @key{type} T2 @key{is new} P.T1 @key{and} P.Pkg.Ifc @key{with null record};
698 @key{procedure} Foo (X : T2); -- @RI[Foo #2]
699 X : T2;
700 @key{end} P_Client;]}
701
702 @ChgRef{Version=[2],Kind=[AddedNormal]}
703 @ChgAdded{Version=[2],Text=[@key{with} P_Client;
704 @key{package body} P @key{is}
705 ...]}
706
707 @ChgRef{Version=[2],Kind=[AddedNormal]}
708 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0005-1]}
709 @ChgAdded{Version=[2],Text=[ @key{procedure} Bar (X : T1'Class) @key{is}
710 @key{begin}
711 Pkg.Foo (@Chg{Version=[5],New=[Pkg.Ifc'Class (X)],Old=[X]}); -- @RI[should call Foo #1 or an override thereof]
712 @key{end};]}
713
714 @ChgRef{Version=[2],Kind=[AddedNormal]}
715 @ChgAdded{Version=[2],Text=[@key{begin}
716 Pkg.Foo (Pkg.Ifc'Class (P_Client.X)); -- @RI[should call Foo #2]
717 Bar (T1'Class (P_Client.X));
718 @key{end} P;]}
719 @end{Example}
720 @ChgRef{Version=[2],Kind=[AddedNormal]}
721 @ChgAdded{Version=[2],Text=[This example is illegal because the completion
722 of T1 is descended from an interface that the partial view is not descended
723 from. If it were legal, T2 would implement Ifc twice, once in the visible
724 part of P, and once in the visible part of P_Client. We would need to
725 decide how Foo #1 and Foo #2 relate to each other. There are two options:
726 either Foo #2 overrides Foo #1, or it doesn't.]}
727
728 @ChgRef{Version=[2],Kind=[AddedNormal]}
729 @ChgAdded{Version=[2],Text=[If Foo #2 overrides Foo #1,
730 we have a problem because the client redefines a behavior that it doesn't
731 know about, and we try to avoid this at all costs, as it would lead to a
732 breakdown of whatever abstraction was implemented. If the abstraction
733 didn't expose that it implements Ifc, there must be a reason, and it should
734 be able to depend on the fact that no overriding takes place in clients.
735 Also, during maintenance, things may change and the full view might
736 implement a different set of interfaces. Furthermore, the situation is even
737 worse if the full type implements another interface Ifc2 that happens to
738 have a conforming Foo (otherwise unrelated, except for its name and
739 profile).]}
740
741 @ChgRef{Version=[2],Kind=[AddedNormal]}
742 @ChgAdded{Version=[2],Text=[If Foo #2 doesn't override Foo #1,
743 there is some similarity with the case of normal tagged private types,
744 where a client can declare an operation that happens to conform to some
745 private operation, and that's OK, it gets a different slot in the type
746 descriptor. The problem here is that T2 would implement Ifc in two
747 different ways, and through conversions to Ifc'Class we could end up with
748 visibility on both of these two different implementations. This is the
749 @lquotes@;diamond inheritance@rquotes problem of C++ all over again, and we
750 would need some kind of a preference rule to pick one implementation. We
751 don't want to go there (if we did, we might as well provide full-fledged
752 multiple inheritance).]}
753
754 @ChgRef{Version=[2],Kind=[AddedNormal]}
755 @ChgAdded{Version=[2],Text=[Note that there wouldn't be any difficulty to
756 implement the first option, so the restriction is essentially
757 methodological. The second option might be harder to implement, depending
758 on the language rules that we would choose.]}
759 @end{Reason}
760
761 @begin{Ramification}
762 @ChgRef{Version=[2],Kind=[AddedNormal]}
763 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
764 @ChgAdded{Version=[2],Text=[This rule also prevents completing a private type
765 with an interface. @Chg{Version=[3],New=[An],Old=[A]} interface, like all
766 types, is a descendant of itself,
767 and thus this rule is triggered. One reason this is necessary is that
768 a client of a private extension should be able to inherit limitedness
769 without having to look in the private part to see if the type is an
770 interface (remember that limitedness of interfaces is never inherited, while
771 it is inherited from other types).]}
772 @end{Ramification}
773
774 @end{Itemize}
775
776 @Defn2{Term=[ancestor subtype], Sec=(of a @nt<private_extension_declaration>)}
777 The @i(ancestor subtype) of a @nt<private_extension_declaration>
778 is the subtype defined by the @i(ancestor_)@!@nt<subtype_@!indication>;
779 the ancestor type shall be a specific tagged type.
780 The full view of a private extension shall be derived
781 (directly or indirectly) from the ancestor type.
782 In addition to the places where @LegalityTitle normally apply
783 (see @RefSecNum{Generic Instantiation}),
784 the requirement that the ancestor be specific applies also in the
785 private part of an instance of a generic unit.@PDefn{generic contract issue}
786 @begin{Reason}
787 This rule allows the full view to be defined
788 through several intermediate derivations,
789 possibly from a series of types produced by
790 @nt{generic_instantiation}s.
791 @end{Reason}
792
793 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
794 @ChgAdded{Version=[2],Text=[If the reserved word @key{limited} appears in a
795 @nt{private_extension_declaration}, the ancestor type shall be a limited type.
796 If the reserved word @key{synchronized} appears in a
797 @nt{private_extension_declaration}, the ancestor type shall be a limited
798 interface.]}
799
800 If the declaration of a partial view includes
801 a @nt{known_discriminant_part}, then
802 the @nt{full_type_declaration} shall have a fully conforming
803 @Redundant[(explicit)]
804 @nt{known_discriminant_part}
805 @Redundant[(see @RefSec(Conformance Rules))].
806 @Defn2{Term=[full conformance],Sec=(required)}
807 @Redundant[The ancestor subtype may be unconstrained;
808 the parent subtype of the full view is required to be constrained
809 (see @RefSecNum{Discriminants}).]
810 @begin{Discussion}
811 If the ancestor subtype has discriminants,
812 then it is usually best to make it unconstrained.
813 @end{Discussion}
814
815 @begin{Ramification}
816 If the partial view has a @nt<known_discriminant_part>,
817 then the full view has to be a composite, non-array type,
818 since only such types may have known discriminants.
819 Also, the full view cannot inherit the discriminants in this case;
820 the @nt{known_discriminant_part} has to be explicit.
821
822 @leading@keepnext@;That is, the following is illegal:
823 @begin{Example}
824 @key[package] P @key[is]
825 @key[type] T(D : Integer) @key[is] @key[private];
826 @key[private]
827 @key[type] T @key[is] @key[new] Some_Other_Type; --@RI{ Illegal!}
828 @key[end] P;
829 @end{Example}
830 even if Some_Other_Type has an integer discriminant called D.
831
832 It is a ramification of this and other rules that in order for
833 a tagged type to privately inherit unconstrained discriminants,
834 the private type declaration has to have an
835 @nt{unknown_discriminant_part}.
836 @end{Ramification}
837
838
839 If a private extension inherits known discriminants from the ancestor subtype,
840 then the full view shall also inherit its discriminants from the
841 ancestor subtype,
842 and the parent subtype of the full view shall be constrained
843 if and only if the ancestor subtype is constrained.
844 @begin{Reason}
845 The first part ensures that the full view has the same discriminants
846 as the partial view.
847 The second part ensures that if the partial view is unconstrained,
848 then the full view is also unconstrained;
849 otherwise, a client might constrain the partial view in a way that
850 conflicts with the constraint on the full view.
851 @end{Reason}
852
853 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01]}
854 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0004-1]}
855 @ChgAdded{Version=[2],Text=[If the @nt{full_type_declaration} for a private
856 extension @Chg{Version=[3],New=[includes],Old=[is]} a
857 @Chg{Version=[3],New=[@nt{derived_type_definition}],Old=[@ntf{derived_type_declaration}]},
858 then the reserved word
859 @key{limited} shall appear in the @nt{full_type_declaration} if and only if it
860 also appears in the @nt{private_extension_declaration}.]}
861 @begin{Reason}
862 @ChgRef{Version=[2],Kind=[AddedNormal]}
863 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0004-1]}
864 @ChgAdded{Version=[2],Text=[The word @key{limited} is optional (unless the
865 ancestor is an interface), but it should be used consistently. Otherwise
866 things would be too confusing for the reader. Of course, we only require
867 that if the full type @Chg{Version=[3],New=[includes],Old=[is defined by]}
868 a @nt{derived_type_definition}, as we
869 want to allow task and protected types to complete extensions of synchronized
870 interfaces.]}
871 @end{Reason}
872
873 @Redundant[If a partial view has unknown discriminants,
874 then the @nt{full_type_declaration} may define
875 a definite or an indefinite subtype, with or without discriminants.]
876
877 If a partial view has neither known nor unknown discriminants,
878 then the @nt{full_type_declaration} shall define a definite subtype.
879
880 If the ancestor subtype of a private extension has constrained
881 discriminants,
882 then the parent subtype of the full view shall impose a statically
883 matching constraint on those discriminants.
884 @PDefn2{Term=[statically matching],Sec=(required)}
885 @begin{Ramification}
886 If the parent type of the full view is not the ancestor type,
887 but is rather some descendant thereof, the constraint on
888 the discriminants of the parent type might come from
889 the declaration of some intermediate type in the derivation
890 chain between the ancestor type and the parent type.
891 @end{Ramification}
892 @begin{Reason}
893 @leading@keepnext@;This prevents the following:
894 @begin{Example}
895 @key[package] P @key[is]
896 @key[type] T2 @key[is] @key[new] T1(Discrim => 3) @key[with] @key[private];
897 @key[private]
898 @key[type] T2 @key[is] @key[new] T1(Discrim => 999) --@RI{ Illegal!}
899 @key[with] @key[record] ...;
900 @key[end] P;
901 @end{Example}
902
903 The constraints in this example do not statically match.
904
905 @leading@;If the constraint on the parent subtype of the full view depends on
906 discriminants of the full view, then the ancestor subtype has to be
907 unconstrained:
908 @begin{Example}
909 @key[type] One_Discrim(A: Integer) @key[is] @key[tagged] ...;
910 ...
911 @key[package] P @key[is]
912 @key[type] Two_Discrims(B: Boolean; C: Integer) @key[is] @key[new] One_Discrim @key[with] @key[private];
913 @key[private]
914 @key[type] Two_Discrims(B: Boolean; C: Integer) @key[is] @key[new] One_Discrim(A => C) @key[with]
915 @key[record]
916 ...
917 @key[end] @key[record];
918 @key[end] P;
919 @end{Example}
920
921 The above example would be illegal if the private extension said
922 @lquotes@;is new One_Discrim(A => C);@rquotes@;,
923 because then the constraints would not statically match.
924 (Constraints that depend on discriminants are not static.)
925
926 @end{Reason}
927 @end{Legality}
928
929 @begin{StaticSem}
930 @PDefn{private type}
931 A @nt{private_type_declaration} declares a private type
932 and its first subtype.
933 @PDefn{private extension}
934 Similarly, a @nt{private_@!extension_@!declaration} declares a private
935 extension and its first subtype.
936 @begin{Discussion}
937 @Defn{package-private type}
938 A @i(package-private type) is one
939 declared by a @nt<private_type_declaration>;
940 that is,
941 a private type other than a generic formal private type.
942 @Defn{package-private extension}
943 Similarly, a @i(package-private extension) is one
944 declared by a @nt<private_extension_declaration>.
945 These terms are not used in the RM95 version of this document.
946 @end{Discussion}
947
948 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0269-1]}
949 A declaration of a partial view and the corresponding
950 @nt{full_type_declaration} define two views of a single type.
951 The declaration of a partial view
952 together with the visible part define the operations that are
953 available to outside program units;
954 the declaration of the full view together with
955 the private part define other operations whose direct use is
956 possible only within the declarative region of the package itself.
957 @Chg{Version=[3],New=[],Old=[@PDefn{characteristics}]}Moreover,
958 within the scope of the declaration of the full view, the
959 @Chg{Version=[3],New=[characteristics (see
960 @RefSecNum{Derived Types and Classes})],Old=[@i{characteristics}]}
961 of the type are determined by the full view;
962 in particular, within its scope, the full view determines
963 the classes that include the type,
964 which components, entries, and protected subprograms are visible,
965 what attributes and other predefined operations are allowed,
966 and whether the first subtype is static.
967 See @RefSecNum{Private Operations}.
968
969 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00401-01]}
970 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0110-1]}
971 @Chg{Version=[3],New=[For a],Old=[A]}
972 private extension@Chg{Version=[3],New=[, the characteristics],
973 Old=[inherits components]} (including
974 @Chg{Version=[3],New=[components, but excluding ],Old=[]}discriminants
975 @Chg{Version=[3],New=[if],Old=[unless]} there is a new @nt<discriminant_part>
976 specified)@Chg{Version=[3],New=[, predefined operators,],Old=[]} and
977 @Chg{Version=[3],New=[inherited ],Old=[]}user-defined primitive
978 subprograms @Chg{Version=[3],New=[are determined
979 by],Old=[from]} its ancestor type@Chg{Version=[2],New=[ and its progenitor
980 types (if any)],Old=[]}, in the same way that@Chg{Version=[3],New=[ those
981 of],Old=[]} a record extension @Chg{Version=[3],New=[are determined
982 by those of],Old=[inherits components and user-defined primitive
983 subprograms from]} its parent type@Chg{Version=[2],New=[ and its progenitor
984 types],Old=[]}
985 (see @RefSecNum{Derived Types and Classes}@Chg{Version=[3],New=[ and
986 @RefSecNum{Private Operations}],Old=[]}).
987 @begin{Honest}
988 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0110-1]}
989 If an operation of the @Chg{Version=[3],New=[ancestor or],Old=[]} parent type
990 is abstract, then the abstractness of the inherited operation
991 is different for nonabstract record extensions
992 than for nonabstract private extensions
993 (see @RefSecNum{Abstract Types and Subprograms}).
994 @end{Honest}
995 @end{StaticSem}
996
997 @begin{RunTime}
998 @PDefn2{Term=[elaboration], Sec=(private_type_declaration)}
999 The elaboration of a @nt{private_type_declaration} creates a partial
1000 view of a type.
1001 @PDefn2{Term=[elaboration], Sec=(private_extension_declaration)}
1002 The elaboration of a @nt{private_extension_declaration} elaborates
1003 the @i(ancestor_)@nt<subtype_indication>, and creates a
1004 partial view of a type.
1005 @end{RunTime}
1006
1007 @begin{Notes}
1008 The partial view of a type as declared by a @nt<private_type_declaration>
1009 is defined to be a composite view (in @RefSecNum{Types and Subtypes}).
1010 The full view of the type might or might not be composite.
1011 A private extension is also composite,
1012 as is its full view.
1013
1014 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
1015 Declaring a private type with an @nt{unknown_discriminant_part} is a
1016 way of preventing clients from creating uninitialized objects of the
1017 type; they are then forced to initialize each object by calling some
1018 operation declared in the visible part of the package.
1019 @Chg{Version=[2],New=[],Old=[If such a type is also limited, then no objects
1020 of the type can be declared outside the scope of the @nt{full_type_declaration},
1021 restricting all object creation to the package defining the type. This
1022 allows complete control over all storage allocation for the type.
1023 Objects of such a type can still be passed as parameters, however.]}
1024 @begin{Discussion}
1025 @Defn{generic contract/private type contract analogy}
1026 Packages with private types are analogous to generic packages with
1027 formal private types,
1028 as follows:
1029 The declaration of a package-private type is like the declaration of
1030 a formal private type.
1031 The visible part of the package is like the generic formal part;
1032 these both specify a contract (that is, a set of operations and other
1033 things available for the private type).
1034 The private part of the package is like an instantiation of the generic;
1035 they both give a @nt{full_type_declaration} that specifies implementation
1036 details of the private type.
1037 The clients of the package are like the body of the generic;
1038 usage of the private type in these places is restricted to the
1039 operations defined by the contract.
1040
1041 In other words, being inside the package is like being outside the
1042 generic, and being outside the package is like being inside the
1043 generic;
1044 a generic is like an @lquotes@;inside-out@rquotes@; package.
1045
1046 This analogy also works for private extensions
1047 in the same inside-out way.
1048
1049 Many of the legality rules are defined with this analogy in mind.
1050 See, for example, the rules relating to operations of [formal]
1051 derived types.
1052
1053 The completion rules for a private
1054 type are intentionally quite similar to the matching rules for a
1055 generic formal private type.
1056
1057 This analogy breaks down in one respect:
1058 a generic actual subtype is a subtype,
1059 whereas the full view for a private type is always a new type.
1060 (We considered allowing the completion of a @nt{private_type_declaration}
1061 to be a @nt{subtype_declaration},
1062 but the semantics just won't work.)
1063 This difference is behind the fact that a generic actual type can be
1064 class-wide, whereas the completion of a private type always declares
1065 a specific type.
1066 @end{Discussion}
1067
1068 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00401]}
1069 The ancestor type specified in a @nt<private_extension_declaration>
1070 and the parent type specified in the corresponding declaration
1071 of a record extension given in the private part need not be the
1072 same@Chg{Version=[2],New=[. If the ancestor
1073 type is not an interface type,],Old=[ @em]} the parent type of the full view
1074 can be any descendant of the ancestor type.
1075 In this case, for a primitive subprogram that is inherited from the
1076 ancestor type and
1077 not overridden, the formal parameter names and default expressions (if any)
1078 come from the corresponding primitive subprogram of the specified ancestor
1079 type, while the body comes from the corresponding primitive subprogram
1080 of the parent type of the full view.
1081 See @RefSecNum{Dispatching Operations of Tagged Types}.
1082
1083 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00401]}
1084 @ChgAdded{Version=[2],Text=[If the ancestor type specified in a
1085 @nt{private_extension_declaration} is an
1086 interface type, the parent type can be any type so long as the full view is a
1087 descendant of the ancestor type. The progenitor types specified in a
1088 @nt{private_extension_declaration} and the progenitor types specified in the
1089 corresponding declaration of a record extension given in the private part need
1090 not be the same @em the only requirement is that the private extension and the
1091 record extension be descended from the same set of interfaces.]}
1092 @end{Notes}
1093
1094 @begin{Examples}
1095 @leading@keepnext@i{Examples of private type declarations:}
1096 @begin{Example}
1097 @key[type] Key @key[is] @key[private];
1098 @key[type] File_Name @key[is] @key[limited] @key[private];
1099 @end{Example}
1100
1101 @begin{WideAbove}
1102 @leading@keepnext@i{Example of a private extension declaration:}
1103 @end{WideAbove}
1104 @begin{Example}
1105 @key[type] List @key[is] @key[new] Ada.Finalization.Controlled @key[with] @key[private];
1106 @end{Example}
1107 @end{Examples}
1108
1109 @begin{Extend83}
1110 @Defn{extensions to Ada 83}
1111 The syntax for a @nt{private_type_declaration} is augmented to
1112 allow the reserved word @key{tagged}.
1113
1114 In Ada 83, a private type without discriminants cannot be completed
1115 with a type with discriminants.
1116 Ada 95 allows the full view to have discriminants,
1117 so long as they have defaults
1118 (that is, so long as the first subtype is definite).
1119 This change is made for uniformity with generics,
1120 and because the rule as stated is simpler and easier to remember
1121 than the Ada 83 rule.
1122 In the original version of Ada 83, the same restriction applied
1123 to generic formal private types.
1124 However, the restriction was removed by the ARG for generics.
1125 In order to maintain the @lquotes@;generic contract/private type contract analogy@rquotes@;
1126 discussed above, we have to apply the same rule to
1127 package-private types.
1128 Note that a private untagged type without discriminants can be
1129 completed with a tagged type with discriminants only if the
1130 full view is constrained, because discriminants of tagged types
1131 cannot have defaults.
1132 @end{Extend83}
1133
1134 @begin{DiffWord83}
1135 RM83-7.4.1(4),
1136 @lquotes@;Within the specification of the package that declares a private type
1137 and before the end of the corresponding full type declaration, a
1138 restriction applies....@rquotes@;,
1139 is subsumed (and corrected) by the rule that
1140 a type shall be completely defined before it is frozen,
1141 and the rule that the parent type of a derived type declaration shall be
1142 completely defined, unless the derived type is a private extension.
1143 @end{DiffWord83}
1144
1145 @begin{Extend95}
1146 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01],ARef=[AI95-00396-01],ARef=[AI95-00401-01]}
1147 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1148 Added @nt{interface_list} to private extensions to
1149 support interfaces and multiple inheritance
1150 (see @RefSecNum{Interface Types}).]}
1151
1152 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
1153 @ChgAdded{Version=[2],Text=[A private extension may specify that it is a
1154 limited type. This is required for interface ancestors (from which
1155 limitedness is not inherited), but it is generally useful as documentation of
1156 limitedness.]}
1157
1158 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00443-01]}
1159 @ChgAdded{Version=[2],Text=[A private extension may specify that it is a
1160 synchronized type. This is required in order so that a regular limited
1161 interface can be used as the ancestor of a synchronized type (we do not
1162 allow hiding of synchronization).]}
1163 @end{Extend95}
1164
1165 @begin{Extend2005}
1166 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
1167 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
1168 An optional @nt{aspect_specification} can be used in a @nt{private_type_declaration} and
1169 a @nt{private_extension_declaration}.
1170 This is described in @RefSecNum{Aspect Specifications}.]}
1171 @end{Extend2005}
1172
1173 @begin{DiffWord2005}
1174 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0110-1]}
1175 @ChgAdded{Version=[3],Text=[@b<Correction:> The description of how a
1176 private extension inherits characteristics was made consistent with the
1177 way formal derived types inherit characteristics (see
1178 @RefSecNum{Formal Private and Derived Types}).]}
1179 @end{DiffWord2005}
1180
1181
1182
1183 @LabeledSubClause{Private Operations}
1184
1185 @begin{Intro}
1186 @Redundant[For a type declared in the visible part of a package
1187 or generic package, certain operations on the type
1188 do not become visible until later in the package @em
1189 either in the private part or the body.
1190 @Defn{private operations}
1191 Such @i{private operations} are available only inside the declarative
1192 region of the package or generic package.]
1193 @end{Intro}
1194
1195 @begin{StaticSem}
1196 The predefined operators that exist for a given type are determined by
1197 the classes to which the type belongs.
1198 For example, an integer type has a predefined "+" operator.
1199 In most cases, the predefined operators of a type are declared
1200 immediately after the definition of the type;
1201 the exceptions are explained below.
1202 Inherited subprograms are also implicitly declared
1203 immediately after the definition of the type,
1204 except as stated below.
1205
1206 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1207 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0029-1]}
1208 For a composite type, the characteristics
1209 (see @RefSecNum{Private Types and Private Extensions})
1210 of the type are determined in part by the
1211 characteristics of its component types.
1212 At the place where the composite type is declared,
1213 the only characteristics of component types used are those
1214 characteristics visible at that place.
1215 If later @Chg{New=[immediately within the declarative region in which the
1216 composite type is declared],
1217 Old=[within the immediate scope of the composite type]} additional
1218 characteristics become visible for a component type,
1219 then any corresponding characteristics become visible for the composite
1220 type.
1221 Any additional predefined operators are implicitly declared at that
1222 place.@Chg{Version=[3],New=[ If there is no such place, then additional
1223 predefined operators are not declared at all, but they still exist.],Old=[]}
1224
1225 @begin{Reason}
1226 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0029-1]}
1227 @ChgAdded{Version=[3],Text=[We say that the predefined operators exist
1228 because they can emerge in some unusual generic instantiations. See
1229 @RefSecNum{Formal Types}.]}
1230 @end{Reason}
1231 @begin{Discussion}
1232 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0029-1]}
1233 @ChgAdded{Version=[3],Text=[The predefined operators for the underlying class
1234 of a type always exist, even if there is no visibility on that underlying
1235 class. This rule is simply about where (if ever) those operators are
1236 declared (and thus become usable). The @ldquote@;additional predefined
1237 operators@rdquote defined by this rule are any that are not declared at the
1238 point of the original type declaration. For instance, a type derived from a
1239 private type whose full type is type String always will have a ">"
1240 operator, but where that operator is declared (and thus whether it is
1241 visible) will depend on the visibility of the full type of the parent type.]}
1242 @end{Discussion}
1243
1244 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1245 The corresponding rule applies to a type defined by a
1246 @nt{derived_type_definition},
1247 if there is a place @Chg{New=[immediately within the declarative region in which
1248 the type is declared],
1249 Old=[within its immediate scope]} where additional
1250 characteristics of its parent type become visible.
1251
1252 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1253 @Defn{become nonlimited}
1254 @Defn2{Term=[nonlimited type],Sec=(becoming nonlimited)}
1255 @Defn2{Term=[limited type],Sec=(becoming nonlimited)}
1256 @Redundant[For example, an array type whose component type is limited
1257 private becomes nonlimited if the full view of the component type is
1258 nonlimited and visible at some later place @Chg{New=[immediately within the declarative region in which
1259 the array type is declared.],
1260 Old=[within the immediate scope of the array type.]}
1261 In such a case, the predefined "=" operator is implicitly declared at
1262 that place, and assignment is allowed after that place.]
1263
1264 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0140-1]}
1265 @ChgAdded{Version=[5],Text=[The characteristics and constraints of the
1266 designated subtype of an access type follow a somewhat different rule. The view
1267 of the designated subtype of (a view of) an access type at a given place is
1268 determined by the view of the designated subtype that is visible at that place,
1269 rather than the view at the place where the access type is declared.]}
1270
1271 @begin{Ramification}
1272 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0140-1]}
1273 @ChgAdded{Version=[5],Text=[Whether or not the designated subtype is
1274 considered incomplete is determined by rules in
1275 @RefSecNum{Incomplete Type Declarations};
1276 this rule has no effect on that property.]}
1277 @end{Ramification}
1278
1279 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0115-1],ARef=[AI05-0269-1]}
1280 @ChgRef{Version=[5],Kind=[RevisedAdded],ARef=[AI12-0140-1]}@Comment{Only because the paragraph number changes}
1281 @ChgAdded{Version=[3],Text=[A type is a @i<descendant>@Defn2{Term=[descendant],
1282 Sec=[of the full view of a type]} of the full view of some ancestor
1283 of its parent type only if the current view it has of its parent is a
1284 descendant of the full view of that ancestor. More generally, at any given
1285 place, a type is descended from the same view of an ancestor as that from which
1286 the current view of its parent is descended. This view determines what
1287 characteristics are inherited from the ancestor@Redundant[, and, for example,
1288 whether the type is considered to be a descendant of a record type, or a
1289 descendant only through record extensions of a more distant ancestor].]}
1290
1291 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0115-1]}
1292 @ChgRef{Version=[4],Kind=[RevisedAdded],ARef=[AI12-0065-1]}
1293 @ChgRef{Version=[5],Kind=[RevisedAdded],ARef=[AI12-0140-1]}@Comment{Only because the paragraph number changes}
1294 @ChgAdded{Version=[3],Text=[@Redundant[@Chg{Version=[4],New=[Furthermore,
1295 it],Old=[It]} is possible for there to be places
1296 where a derived type is @Chg{Version=[4],New=[known to be derived
1297 indirectly from],Old=[visibly a descendant of]} an ancestor type, but
1298 @Chg{Version=[4],New=[is ],Old=[]}not a
1299 descendant of even a partial view of the ancestor type, because the parent
1300 of the derived type is not visibly a descendant of the ancestor. In
1301 this case, the derived type inherits no characteristics from that
1302 ancestor, but nevertheless is within the derivation class of the
1303 ancestor for the purposes of type conversion, the "covers"
1304 relationship, and matching against a formal derived type. In this
1305 case the derived type is @Chg{Version=[4],New=[effectively],Old=[considered
1306 to be]} a @i<descendant> of an incomplete view of the ancestor.@Chg{Version=[4],New=[],Old=[@Defn2{Term=[descendant],
1307 Sec=[of an incomplete view]}]}]]}
1308
1309 @begin{Discussion}
1310 @ChgRef{Version=[3],Kind=[AddedNormal]}
1311 @ChgAdded{Version=[3],Type=[Leading],Text=[Here is an example of this situation:]}
1312 @begin{Example}
1313 @ChgRef{Version=[3],Kind=[AddedNormal]}
1314 @ChgAdded{Version=[3],Text=[@key[package] P @key[is]
1315 @key[type] T @key[is] @key[private];
1316 C : @key[constant] T;
1317 @key[private]
1318 @key[type] T @key[is new] Integer;
1319 C : @key[constant] T := 42;
1320 @key[end] P;]}
1321
1322 @ChgRef{Version=[3],Kind=[AddedNormal]}
1323 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0065-1]}
1324 @ChgAdded{Version=[3],Text=[@key[with] P;
1325 @key[package] Q @key[is]
1326 @key[type] T2 @key[is new] P.T;@Chg{Version=[4],New=[ -- @Examcom{T2 is @b<not> a descendant of Integer}],Old=[]}
1327 @key[end] Q;]}
1328
1329 @ChgRef{Version=[3],Kind=[AddedNormal]}
1330 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0065-1]}
1331 @ChgAdded{Version=[3],Text=[@key[with] Q;
1332 @key[package] P.Child @key[is]
1333 @key[type] T3 @key[is new] Q.T2;
1334 @key[private]
1335 @Chg{Version=[4],New=[-- @Examcom{Here T3 is known to be indirectly derived from Integer, but inherits}
1336 -- @Examcom{@b<no> characteristics from Integer, since T2 inherits no characteristics}
1337 -- @Examcom{from Integer.}
1338 -- @Examcom{However, we allow an explicit conversion of T3 to/from Integer.}
1339 -- @Examcom{Hence, T3 is effectively a descendant of an "incomplete" view of Integer.}
1340 ],Old=[]}Int : Integer := 52;
1341 V : T3 := T3(P.C); -- @Examcom{Legal: conversion allowed}
1342 W : T3 := T3(Int); -- @Examcom{Legal: conversion allowed}
1343 X : T3 := T3(42); -- @Examcom{Error: T3 is not a numeric type}
1344 Y : T3 := X + 1; -- @Examcom{Error: no visible "+" operator}
1345 Z : T3 := T3(Integer(W) + 1); -- @Examcom{Legal: convert to Integer first}
1346 @key[end] P.Child;]}
1347 @end{Example}
1348 @end{Discussion}
1349
1350 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1351 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0029-1]}
1352 Inherited primitive subprograms follow a different rule.
1353 For a @nt{derived_type_definition},
1354 each inherited primitive subprogram
1355 is implicitly declared at the earliest place, if any,
1356 @Chg{New=[immediately within the declarative region in which],
1357 Old=[within the immediate scope of]} the @nt{type_declaration}@Chg{New=[ occurs],Old=[]},
1358 but after the @nt{type_declaration},
1359 where the corresponding declaration from the parent is
1360 visible.
1361 If there is no such place, then the inherited subprogram
1362 is not declared at all@Chg{Version=[3],New=[, but it still exists],Old=[]}.
1363 @Redundant[@Chg{Version=[3],New=[For a tagged type, it is possible to
1364 dispatch to an],Old=[An]} inherited subprogram that is not declared at
1365 all@Chg{Version=[3],New=[],Old=[cannot be named in a call and cannot be
1366 overridden, but for a tagged type, it is possible to dispatch to it]}.]
1367
1368 For a @nt{private_extension_declaration}, each inherited subprogram is
1369 declared immediately after the @nt{private_extension_declaration}
1370 if the corresponding declaration from the ancestor is visible at that
1371 place.
1372 Otherwise, the inherited subprogram is not declared for the private
1373 extension, @Redundant[though it might be for the full type].
1374 @begin{Reason}
1375 @ChgRef{Version=[1],Kind=[Revised]}
1376 There is no need for the @lquotes@;earliest place
1377 @Chg{New=[immediately within the declarative region],
1378 Old=[within the immediate scope]}@rquotes@;
1379 business here, because a @nt{private_extension_declaration} will be
1380 completed with a @nt{full_type_declaration}, so we can hang the
1381 necessary private implicit declarations on the @nt{full_type_declaration}.
1382 @end{Reason}
1383 @begin{Discussion}
1384 The above rules matter only when the component type (or parent type) is
1385 declared in the visible part of a package, and the composite type (or
1386 derived type) is declared within the declarative region of that package
1387 (possibly in a nested package or a child package).
1388
1389 @leading@keepnext@;Consider:
1390 @begin{Example}
1391 @key[package] Parent @key[is]
1392 @key[type] Root @key[is] @key[tagged] @key[null] @key[record];
1393 @key[procedure] Op1(X : Root);
1394
1395 @key[type] My_Int @key[is] @key[range] 1..10;
1396 @key[private]
1397 @key[procedure] Op2(X : Root);
1398
1399 @key[type] Another_Int @key[is] @key[new] My_Int;
1400 @key[procedure] Int_Op(X : My_Int);
1401 @key[end] Parent;
1402
1403 @key[with] Parent; @key[use] Parent;
1404 @key[package] Unrelated @key[is]
1405 @key[type] T2 @key[is] @key[new] Root @key[with] @key[null] @key[record];
1406 @key[procedure] Op2(X : T2);
1407 @key[end] Unrelated;
1408
1409 @key[package] Parent.Child @key[is]
1410 @key[type] T3 @key[is] @key[new] Root @key[with] @key[null] @key[record];
1411 --@RI{ Op1(T3) implicitly declared here.}
1412
1413 @key[package] Nested @key[is]
1414 @key[type] T4 @key[is] @key[new] Root @key[with] @key[null] @key[record];
1415 @key[private]
1416 ...
1417 @key[end] Nested;
1418 @key[private]
1419 --@RI{ Op2(T3) implicitly declared here.}
1420 ...
1421 @key[end] Parent.Child;
1422
1423 @key[with] Unrelated; @key[use] Unrelated;
1424 @key[package] @key[body] Parent.Child @key[is]
1425 @key[package] @key[body] Nested @key[is]
1426 --@RI{ Op2(T4) implicitly declared here.}
1427 @key[end] Nested;
1428
1429 @key[type] T5 @key[is] @key[new] T2 @key[with] @key[null] @key[record];
1430 @key[end] Parent.Child;
1431 @end{Example}
1432
1433 Another_Int does not inherit Int_Op,
1434 because Int_Op does not @lquotes@;exist@rquotes@; at the place
1435 where Another_Int is declared.
1436
1437 @ChgRef{Version=[1],Kind=[Revised]}
1438 Type T2 inherits Op1 and Op2 from Root.
1439 However, the inherited Op2 is never declared,
1440 because Parent.Op2 is never visible @Chg{New=[immediately within the declarative region],
1441 Old=[within the immediate scope]} of T2.
1442 T2 explicitly declares its own Op2,
1443 but this is unrelated to the inherited one @em it
1444 does not override the inherited one,
1445 and occupies a different slot in the type descriptor.
1446
1447 T3 inherits both Op1 and Op2. Op1 is implicitly declared immediately
1448 after the type declaration,
1449 whereas Op2 is declared at the beginning of the private part.
1450 Note that if Child were a private child of Parent,
1451 then Op1 and Op2 would both be implicitly declared immediately after the
1452 type declaration.
1453
1454 @ChgRef{Version=[1],Kind=[Revised]}
1455 T4 is similar to T3, except that the earliest place
1456 @Chg{New=[immediately within the declarative region containing T4],
1457 Old=[within T4's immediate scope]}
1458 where Root's Op2 is visible is in the body of Nested.
1459
1460 If T3 or T4 were to declare a type-conformant Op2,
1461 this would override the one inherited from Root.
1462 This is different from the situation with T2.
1463
1464
1465 T5 inherits Op1 and two Op2's from T2.
1466 Op1 is implicitly declared immediately after the declaration of T5,
1467 as is the Op2 that came from Unrelated.Op2.
1468 However, the Op2 that originally came from Parent.Op2 is never
1469 implicitly declared for T5,
1470 since T2's version of that Op2 is never visible (anywhere @em it never
1471 got declared either).
1472
1473 For all of these rules, implicit private parts and bodies are assumed as
1474 needed.
1475
1476 @leading@keepnext@;It is possible for characteristics of a type to be revealed
1477 in more than one place:
1478
1479 @begin{Example}
1480 @key[package] P @key[is]
1481 @key[type] Comp1 @key[is] @key[private];
1482 @key[private]
1483 @key[type] Comp1 @key[is] @key[new] Boolean;
1484 @key[end] P;
1485
1486 @key[package] P.Q @key[is]
1487 @key[package] R @key[is]
1488 @key[type] Comp2 @key[is] @key[limited] @key[private];
1489 @key[type] A @key[is] @key[array](Integer @key[range] <>) @key[of] Comp2;
1490 @key[private]
1491 @key[type] Comp2 @key[is] @key[new] Comp1;
1492 --@RI{ A becomes nonlimited here.}
1493 --@RI{ "="(A, A) return Boolean is implicitly declared here.}
1494 ...
1495 @key[end] R;
1496 @key[private]
1497 --@RI{ Now we find out what Comp1 really is, which reveals}
1498 --@RI{ more information about Comp2, but we're not within}
1499 --@RI{ the immediate scope of Comp2, so we don't do anything}
1500 --@RI{ about it yet.}
1501 @key[end] P.Q;
1502
1503 @key[package] @key[body] P.Q @key[is]
1504 @key[package] @key[body] R @key[is]
1505 --@RI{ Things like "@key[xor]"(A,A) return A are implicitly}
1506 --@RI{ declared here.}
1507 @key[end] R;
1508 @key[end] P.Q;
1509 @end{Example}
1510
1511 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0019],ARef=[AI95-00033-01]}
1512 @ChgAdded{Version=[1],Type=[Leading],Text=[We say @i<immediately> within
1513 the declarative region in order that
1514 types do not gain operations within a nested scope. Consider:]}
1515 @begin{Example}
1516 @ChgRef{Version=[1],Kind=[Added]}
1517 @Chg{New=[@Key[package] Outer @key[is]
1518 @key[package] Inner @key[is]
1519 @key[type] Inner_Type @key[is] @key[private];
1520 @key[private]
1521 @key[type] Inner_Type @key[is] @key[new] Boolean;
1522 @key[end] Inner;
1523 @key[type] Outer_Type @key[is] @key[array](Natural @key[range] <>) @key[of] Inner.Inner_Type;
1524 @key[end] Outer;],Old=[]}
1525
1526 @ChgRef{Version=[1],Kind=[Added]}
1527 @Chg{New=[@key[package] @key[body] Outer @key[is]
1528 @key[package] @key[body] Inner @key[is]
1529 -- At this point, we can see that Inner_Type is a Boolean type.
1530 -- But we don't want Outer_Type to gain an "and" operator here.
1531 @key[end] Inner;
1532 @key[end] Outer;],Old=[]}
1533 @end{Example}
1534 @end{Discussion}
1535
1536 @leading@Redundant[The Class attribute is defined for tagged subtypes in
1537 @RefSecNum{Tagged Types and Type Extensions}.
1538 In addition,] for
1539 @PrefixType{every subtype S of an untagged private type
1540 whose full view is tagged},
1541 the following attribute is defined:
1542 @begin(description)
1543 @Attribute{Prefix=<S>, AttrName=<Class>,
1544 Text=<Denotes the class-wide subtype corresponding to the full
1545 view of S.
1546 This attribute is allowed only from the beginning of the private part
1547 in which the full view is declared, until the declaration of the full
1548 view.
1549 @Redundant[After the full view,
1550 the Class attribute of the full view can be used.]>}
1551 @end(description)
1552 @EndPrefixType{}
1553 @end{StaticSem}
1554
1555 @begin{Notes}
1556 Because a partial view and a full view
1557 are two different views of one and the same type,
1558 outside of the defining package the characteristics of the type are
1559 those defined by the visible part.
1560 Within these outside program units the type is just a private type
1561 or private extension,
1562 and any language rule that applies only to another class of types
1563 does not apply. The fact that the full declaration might implement
1564 a private type with a type of a particular class (for example, as
1565 an array type) is relevant only
1566 within the declarative region of the package itself
1567 including any child units.
1568
1569 @NoPrefix@;The consequences of this actual implementation are, however, valid
1570 everywhere. For example: any default initialization of components
1571 takes place; the attribute Size provides the size of the full view;
1572 finalization is still done for controlled components of the full view;
1573 task dependence rules still apply to components that are task
1574 objects.
1575
1576 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
1577 Partial views provide @Chg{Version=[2],New=[initialization],
1578 Old=[assignment (unless the view is limited)]},
1579 membership tests, selected components for the selection of
1580 discriminants and inherited components, qualification,
1581 and explicit conversion.@Chg{Version=[2],New=[ Nonlimited partial views
1582 also allow use of @nt{assignment_statement}s.],Old=[]}
1583
1584 For a subtype S of a partial view, S'Size is defined
1585 (see @RefSecNum{Operational and Representation Attributes}).
1586 For an object A of a partial view,
1587 the attributes A'Size and A'Address are defined
1588 (see @RefSecNum{Operational and Representation Attributes}).
1589 The Position, First_Bit, and Last_Bit attributes
1590 are also defined for discriminants and inherited components.
1591 @end{Notes}
1592
1593 @begin{Examples}
1594 @leading@keepnext@i{Example of a type with private operations:}
1595 @begin{Example}
1596 @key[package] Key_Manager @key[is]
1597 @key[type] Key @key[is] @key[private];
1598 Null_Key : @key[constant] Key; --@RI{ a deferred constant declaration (see @RefSecNum{Deferred Constants})}
1599 @key[procedure] Get_Key(K : @key[out] Key);
1600 @key[function] "<" (X, Y : Key) @key[return] Boolean;
1601 @key[private]
1602 @key[type] Key @key[is] @key[new] Natural;
1603 Null_Key : @key[constant] Key := Key'First;
1604 @key[end] Key_Manager;
1605
1606 @NotISORMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
1607 @key[package] @key[body] Key_Manager @key[is]
1608 Last_Key : Key := Null_Key;
1609 @key[procedure] Get_Key(K : @key[out] Key) @key[is]
1610 @key[begin]
1611 Last_Key := Last_Key + 1;
1612 K := Last_Key;
1613 @key[end] Get_Key;
1614
1615 @key[function] "<" (X, Y : Key) @key[return] Boolean @key[is]
1616 @key[begin]
1617 @key[return] Natural(X) < Natural(Y);
1618 @key[end] "<";
1619 @key[end] Key_Manager;
1620 @end{Example}
1621 @end{Examples}
1622
1623 @begin{Notes}
1624 @i{Notes on the example:}
1625 Outside of the package Key_Manager, the operations available for
1626 objects of type Key include assignment, the comparison for equality
1627 or inequality, the procedure Get_Key and the operator "<"; they do
1628 not include other relational operators such as ">=", or arithmetic
1629 operators.
1630
1631 @NoPrefix@;The explicitly declared operator "<" hides the predefined operator
1632 "<" implicitly declared by the @nt{full_type_declaration}. Within the
1633 body of the function, an explicit conversion of X and Y to the
1634 subtype Natural is necessary to invoke the "<" operator of the parent
1635 type.
1636 Alternatively, the result of the function could be written as not (X
1637 >= Y), since the operator ">=" is not redefined.
1638
1639 @NoPrefix@;The value of the variable Last_Key, declared in the package body,
1640 remains unchanged between calls of the procedure Get_Key. (See also
1641 the NOTES of @RefSecNum{Package Bodies}.)
1642 @end{Notes}
1643
1644 @begin{DiffWord83}
1645 The phrase in RM83-7.4.2(7), @lquotes@;...after the full type declaration@rquotes@;,
1646 doesn't work in the presence of child units, so we define that rule in
1647 terms of visibility.
1648
1649 The definition of the Constrained attribute for private types
1650 has been moved to @lquotes@;Obsolescent Features.@rquotes@;
1651 (The Constrained attribute of an object has not been moved there.)
1652 @end{DiffWord83}
1653
1654 @begin{DiffWord95}
1655 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0018],ARef=[AI95-00033-01]}
1656 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified when additional
1657 operations are declared.]}
1658
1659 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
1660 @ChgAdded{Version=[2],Text=[Revised the note on operations of partial views
1661 to reflect that limited types do have an assignment operation, but not
1662 @nt{assignment_statement}s.]}
1663 @end{DiffWord95}
1664
1665 @begin{DiffWord2005}
1666 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0029-1]}
1667 @ChgAdded{Version=[3],Text=[@b<Correction:> Revised the wording to say
1668 that predefined operations still exist even if they are never declared,
1669 because it is possible to reference them in a generic unit.]}
1670
1671 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0115-1]}
1672 @ChgAdded{Version=[3],Text=[@b<Correction:> Clarified that the characteristics
1673 of a descendant of a private type depend on the visibility of the full
1674 view of the direct ancestor. This has to be the case (so that privacy is not
1675 violated), but it wasn't spelled out in earlier versions of Ada.]}
1676 @end{DiffWord2005}
1677
1678 @begin{DiffWord2012}
1679 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0065-1]}
1680 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Clarified the clarification added
1681 by AI05-0115-1, as it turned out to not be that clear. Hopefully this version
1682 is better.]}
1683
1684 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0140-1]}
1685 @ChgAdded{Version=[5],Text=[@b<Correction:> Clarified the constraints and
1686 properties that apply to a designated subtype. This additional wording
1687 does not mean to change existing practice.]}
1688 @end{DiffWord2012}
1689
1690
1691 @LabeledAddedSubClause{Version=[3],Name=[Type Invariants]}
1692
1693 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1694 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0041-1]}
1695 @ChgAdded{Version=[3],Type=[Leading],Text=[For a private type@Chg{Version=[4],New=[,],Old=[ or]} private
1696 extension@Chg{Version=[4],New=[, or interface],Old=[]}, the following
1697 language-defined aspects may be specified with an
1698 @nt{aspect_specification} (see @RefSecNum{Aspect Specifications}):]}
1699
1700 @begin{Description}
1701 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0250-1]}
1702 @ChgAdded{Version=[3],Text=[Type_Invariant@\This aspect
1703 shall be specified by an @nt{expression}, called an @i<invariant
1704 expression>.@Defn{invariant expression}
1705 Type_Invariant may be specified on a @nt{private_@!type_@!declaration}, on a
1706 @nt{private_@!extension_@!declaration}, or on a @nt{full_@!type_@!declaration} that
1707 declares the completion of a private type or private
1708 extension.@AspectDefn{Type_Invariant}]}
1709
1710 @ChgAspectDesc{Version=[3],Kind=[AddedNormal],Aspect=[Type_Invariant],
1711 Text=[@ChgAdded{Version=[3],Text=[A condition that must hold true for all
1712 objects of a type.]}]}
1713
1714 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1715 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0041-1],ARef=[AI12-0150-1]}
1716 @ChgAdded{Version=[3],Text=[Type_Invariant'Class@\This aspect
1717 shall be specified by an @nt{expression}, called an @i<invariant
1718 expression>.
1719 Type_Invariant'Class may be specified on a
1720 @nt{private_@!type_@!declaration}@Chg{Version=[4],New=[,],Old=[ or]} a
1721 @nt{private_@!extension_@!declaration}@Chg{Version=[4],New=[, or a
1722 @nt{full_@!type_@!declaration} for an
1723 interface type],Old=[]}.@AspectDefn{Type_Invariant'Class}@Chg{Version=[4],New=[
1724 Type_Invariant'Class determines a @i{class-wide type invariant}
1725 for a tagged type.@Defn{class-wide type invariant}@Defn2{Term=[type invariant],
1726 Sec=[class-wide]}@Defn2{Term=[invariant],Sec=[class-wide]}],Old=[]}]}
1727
1728 @begin{Reason}
1729 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0254-1]}
1730 @ChgAdded{Version=[3],Text=[A class-wide type invariant cannot be hidden
1731 in the private part, as the creator of an extension needs to know about it
1732 in order to conform to it in any new or overriding operations. On the other
1733 hand, a specific type invariant is not inherited, so that no operation
1734 outside of the original package needs to conform to it; thus there is no
1735 need for it to be visible.]}
1736 @end{Reason}
1737
1738 @ChgAspectDesc{Version=[3],Kind=[AddedNormal],Aspect=[Type_Invariant'Class],
1739 Text=[@ChgAdded{Version=[3],Text=[A condition that must hold true for all
1740 objects in a class of types.]}]}
1741
1742 @end{Description}
1743
1744 @begin{Resolution}
1745 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1746 @ChgAdded{Version=[3],Text=[The expected type for an invariant expression
1747 is any boolean type.@PDefn2{Term=[expected type],
1748 Sec=(invariant expression)}]}
1749
1750 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1751 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0150-1],ARef=[AI12-0159-1]}
1752 @ChgAdded{Version=[3],Text=[@Redundant[Within an invariant expression, the
1753 identifier of the first subtype of the associated type denotes the current
1754 instance of the type.] Within an invariant expression
1755 @Chg{Version=[4],New=[for the Type_Invariant aspect of a],Old=[associated
1756 with]} type @i<T>, the type of @Chg{Version=[4],New=[this],Old=[the]}
1757 current instance is @i<T>@Chg{Version=[4],New=[. Within an invariant
1758 expression],Old=[for the Type_Invariant aspect and @i<T>'Class]}
1759 for the Type_Invariant'Class aspect@Chg{Version=[4],New=[ of a
1760 type @i<T>, the type of this current instance is interpreted as though it
1761 had a (notional) @Chg{Version=[5],New=[nonabstract ],Old=[]}type
1762 @i<NT> that is a visible formal derived type whose
1763 ancestor type is @i<T>.@Redundant[ The effect of this
1764 interpretation is that the only operations that can be applied to this
1765 current instance are those defined for such a formal derived type.]],Old=[.]}]}
1766
1767 @begin{TheProof}
1768 @ChgRef{Version=[3],Kind=[AddedNormal]}
1769 @ChgAdded{Version=[3],Text=[The first sentence is given formally in
1770 @RefSecNum{Aspect Specifications}.]}
1771 @end{TheProof}
1772
1773 @begin{Reason}
1774 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0159-1]}
1775 @ChgAdded{Version=[4],Text=[The rules for Type_Invariant'Class ensure
1776 that the invariant expression is well-defined for any type descended
1777 from @i<T>.]}
1778 @end{Reason}
1779
1780 @end{Resolution}
1781
1782 @begin{Legality}
1783
1784 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1785 @ChgAdded{Version=[3],Text=[@Redundant[The Type_Invariant'Class aspect shall not be
1786 specified for an untagged type.] The Type_Invariant aspect shall not be specified
1787 for an abstract type.]}
1788
1789 @begin{TheProof}
1790 @ChgRef{Version=[3],Kind=[AddedNormal]}
1791 @ChgAdded{Version=[3],Text=[The first sentence is given formally in
1792 @RefSecNum{Aspect Specifications}.]}
1793 @end{TheProof}
1794
1795 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0042-1]}
1796 @ChgAdded{Version=[4],Text=[If a type extension occurs at a point where a
1797 private operation of some ancestor is visible and inherited, and a
1798 Type_Invariant'Class expression applies to that ancestor, then the inherited
1799 operation shall be abstract or shall be overridden.]}
1800 @end{Legality}
1801
1802 @begin{StaticSem}
1803
1804 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0250-1]}
1805 @ChgAdded{Version=[3],Text=[@Redundant[If the Type_Invariant aspect is
1806 specified for a type @i<T>, then the invariant expression applies to @i<T>.]]}
1807
1808 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1809 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0199-1]}
1810 @ChgAdded{Version=[5],Type=[Leading],Text=[]}@Comment{Conditional leading}
1811 @ChgAdded{Version=[3],Text=[If the Type_Invariant'Class aspect is
1812 specified for a tagged type @i<T>, then @Chg{Version=[5],New=[a
1813 @i<corresponding expression> also applies to each nonabstract descendant
1814 @i{T1} of @i{T} @Redundant[(including @i{T} itself if it is nonabstract)]. The
1815 corresponding expression is constructed from the associated expression
1816 as follows:@Defn2{Term=[corresponding expression],Sec=[class-wide type invariant]}],
1817 Old=[the invariant expression applies to all descendants of @i<T>.]}]}
1818
1819 @begin{Itemize}
1820 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0199-1]}
1821 @ChgAdded{Version=[5],Text=[References to nondiscriminant components of @i{T}
1822 (or to @i{T} itself) are replaced with references to the corresponding
1823 components of @i{T1} (or to @i{T1} as a whole).]}
1824
1825 @begin{Ramification}
1826 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0199-1]}
1827 @ChgAdded{Version=[5],Text=[The only nondiscriminant components visible at
1828 the point of such an aspect specification are necessarily inherited from
1829 some nonprivate ancestor.]}
1830 @end{Ramification}
1831
1832 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0199-1]}
1833 @ChgAdded{Version=[5],Text=[References to discriminants of @i{T} are replaced
1834 with references to the corresponding discriminant of @i{T1}, or to the
1835 specified value for the discriminant, if the discriminant is specified by the
1836 @nt{derived_type_definition} for some type that is an ancestor of @i{T1} and
1837 a descendant of @i{T} (see @RefSecNum{Discriminants}).]}
1838 @end{Itemize}
1839
1840 @begin{TheProof}
1841 @ChgRef{Version=[3],Kind=[AddedNormal]}
1842 @ChgRef{Version=[5],Kind=[Deleted]}
1843 @ChgAdded{Version=[3],Text=[@Chg{Version=[5],New=[],Old=["Applies" is
1844 formally defined in
1845 @RefSecNum{Aspect Specifications}.]}]}
1846 @end{TheProof}
1847
1848 @begin{Discussion}
1849 @ChgRef{Version=[5],Kind=[AddedNormal]}
1850 @ChgAdded{Version=[5],Text=[The associated expression from which the
1851 corresponding expression is constructed is the one that applies to the
1852 descendant type; "applies" is formally defined in
1853 @RefSecNum{Aspect Specifications}.]}
1854 @end{Discussion}
1855
1856
1857 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0075-1],ARef=[AI12-0191-1]}
1858 @ChgAdded{Version=[5],Type=[Leading],Text=[For a nonabstract type @i<T>, a
1859 callable entity is said to be a @i<boundary entity>@Defn{boundary entity}
1860 for @i<T> if it is declared within the immediate scope of @i<T> (or by an
1861 instance of a generic unit, and the generic is declared within the immediate
1862 scope of type @i<T>), or is the Read or Input stream-oriented attribute of type
1863 @i<T>, and either:]}
1864
1865 @begin{Itemize}
1866
1867 @ChgRef{Version=[5],Kind=[Added]}
1868 @ChgAdded{Version=[5],Text=[@i<T> is a private type or a private extension and
1869 the callable entity is visible outside the immediate scope of type T or
1870 overrides an inherited operation that is visible outside the immediate scope
1871 of @i<T>; or]}
1872
1873 @ChgRef{Version=[5],Kind=[Added]}
1874 @ChgAdded{Version=[5],Text=[@i<T> is a record extension, and the callable
1875 entity is a primitive operation visible outside the immediate scope of type
1876 @i<T> or overrides an inherited operation that is visible outside the
1877 immediate scope of @i<T>.]}
1878
1879 @end{Itemize}
1880
1881 @begin{Reason}
1882 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0191-1]}
1883 @ChgAdded{Version=[5],Text=[A boundary entity for type @i<T> is one that might
1884 require an invariant check for @i<T>. It includes subprograms that don't
1885 (visibly) involve T; since we don't want to break privacy, we can't
1886 statically know if some private type has some part of @i<T>. We'll reduce
1887 the set when we describe the actual checks.]}
1888 @end{Reason}
1889
1890 @end{StaticSem}
1891
1892 @begin{Runtime}
1893
1894 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0247-1],ARef=[AI05-0290-1]}
1895 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0150-1]}
1896 @ChgAdded{Version=[3],Type=[Leading],Text=[If one or more invariant expressions
1897 apply to a @Chg{Version=[4],New=[nonabstract ],Old=[]}type @i<T>, then an
1898 invariant check is performed at the following places,
1899 on the specified object(s):@Defn{invariant check}]}
1900
1901 @begin{Itemize}
1902 @ChgRef{Version=[3],Kind=[AddedNormal]}
1903 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0133-1]}
1904 @ChgAdded{Version=[3],Text=[After successful
1905 @Chg{Version=[4],New=[],Old=[default ]}initialization of an
1906 object of type @i<T>@Chg{Version=[4],New=[ by default (see
1907 @RefSecNum{Object Declarations})],Old=[]}, the check is performed on the
1908 new object@Chg{Version=[4],New=[ unless the partial
1909 view of @i<T> has unknown discriminants],Old=[]};]}
1910
1911 @begin{Reason}
1912 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0133-1]}
1913 @ChgAdded{Version=[4],Text=[The check applies everywhere, even in the
1914 package body, because default initialization has to work the same for
1915 clients as it does within the package. As such, checks within the package
1916 are either harmless or will uncover a bug that could also happen to a
1917 client. However, if the partial view of the type has unknown discriminants,
1918 no client of the package can declare a default-initialized object.
1919 Therefore, no invariant check is needed, as all default initialized objects
1920 are necessarily inside the package.]}
1921 @end{Reason}
1922
1923 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0049-1]}
1924 @ChgRef{Version=[5],Kind=[RevisedAdded],ARef=[AI12-0191-1]}
1925 @ChgAdded{Version=[4],Text=[After successful explicit initialization of the
1926 completion of a deferred constant
1927 @Chg{Version=[5],New=[whose nominal type has],Old=[with]} a part of type @i<T>, if the completion
1928 is inside the immediate scope of the full view of @i<T>, and the deferred
1929 constant is visible outside the immediate scope of @i<T>, the check is
1930 performed on the part(s) of type @i<T>;]}
1931
1932 @ChgRef{Version=[3],Kind=[AddedNormal]}
1933 @ChgAdded{Version=[3],Text=[After successful conversion to type @i<T>, the
1934 check is performed on the result of the conversion;]}
1935
1936 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0269-1]}
1937 @ChgAdded{Version=[3],Text=[For a view conversion, outside the immediate scope
1938 of @i<T>, that converts from a descendant of @i<T> (including @i<T> itself) to
1939 an ancestor of type @i<T> (other than @i<T> itself), a check is performed on
1940 the part of the object that is of type @i<T>:]}
1941
1942 @begin{InnerItemize}
1943 @ChgRef{Version=[3],Kind=[AddedNormal]}
1944 @ChgAdded{Version=[3],Text=[after assigning to the view conversion; and]}
1945
1946 @ChgRef{Version=[3],Kind=[AddedNormal]}
1947 @ChgAdded{Version=[3],Text=[after successful return from a call that passes the view conversion
1948 as an @key[in out] or @key[out] parameter.]}
1949 @end{InnerItemize}
1950
1951 @begin{Ramification}
1952 @ChgRef{Version=[3],Kind=[AddedNormal]}
1953 @ChgAdded{Version=[3],Text=[For a single view conversion that converts
1954 between distantly related types, this rule could be triggered for
1955 multiple types and thus multiple invariant checks may be needed.]}
1956 @end{Ramification}
1957 @begin{ImplNote}
1958 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0299-1]}
1959 @ChgAdded{Version=[3],Text=[For calls to inherited subprograms (including
1960 dispatching calls), the implied view conversions mean that a wrapper is
1961 probably needed. (See the Note at the bottom of this subclause for more
1962 on the model of checks for inherited subprograms.)]}
1963
1964 @ChgRef{Version=[3],Kind=[AddedNormal]}
1965 @ChgAdded{Version=[3],Text=[For view conversions involving class-wide
1966 types, the exact checks needed may not be known at compile-time. One
1967 way to deal with this is to have an implicit dispatching operation
1968 that is given the object to check and the tag of the target of the
1969 conversion, and which first checks if the passed tag is not for itself,
1970 and if not, checks the its invariant on the object and then calls
1971 the operation of its parent type. If the tag is for itself, the
1972 operation is complete.]}
1973 @end{ImplNote}
1974
1975 @ChgRef{Version=[3],Kind=[AddedNormal]}
1976 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0146-1]}
1977 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0075-1],ARef=[AI12-0191-1],ARef=[AI12-0193-1]}
1978 @ChgAdded{Version=[5],Type=[Leading],Text=[]}@ChgNote{To get conditional Leading}
1979 @ChgAdded{Version=[3],Text=[@Chg{Version=[5],New=[Upon],Old=[After a]}
1980 successful @Chg{Version=[5],New=[return from a ],Old=[]}call on
1981 @Chg{Version=[5],New=[any callable entity which is a boundary entity
1982 for @i<T>, an invariant],Old=[the Read or Input
1983 @Chg{Version=[4],New=[stream-oriented],Old=[stream]} attribute of the
1984 type @i<T>, the]} check is performed on
1985 @Chg{Version=[5],New=[each object which is subject to an
1986 invariant check for @i<T>. In the case of a call to a protected operation,
1987 the check is performed before the end of the protected action. In the case
1988 of a call to a task entry, the check is performed before the end of the
1989 rendezvous],Old=[the object
1990 initialized by the @Chg{Version=[4],New=[],Old=[stream ]}attribute]}@Chg{Version=[5],New=[.
1991 The following objects of a callable entity are subject to an invariant
1992 check for @i<T>:],Old=[;]}]}
1993
1994 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0269-1]}
1995 @ChgRef{Version=[5],Kind=[Deleted],ARef=[AI12-0075-1]}
1996 @ChgAdded{Version=[3],Type=[Leading],Text=[@Chg{Version=[5],New=[],Old=[An
1997 invariant is checked upon successful return from a call
1998 on any subprogram or entry that:]}]}
1999 @begin{Itemize}
2000 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0269-1]}
2001 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0042-1]}
2002 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0075-1],ARef=[AI12-0191-1]}
2003 @ChgAdded{Version=[3],Text=[@Chg{Version=[5],New=[a result with a nominal type
2004 that has a part],Old=[is declared within the immediate
2005 scope]} of type @i<T>@Chg{Version=[5],New=[;],Old=[ (or by an instance of a generic unit, and the generic
2006 is declared within the immediate scope of type @i<T>),@Chg{Version=[4],New=[],Old=[ and]}]}]}
2007
2008 @ChgRef{Version=[3],Kind=[AddedNormal]}
2009 @ChgRef{Version=[4],Kind=[Deleted],ARef=[AI12-0042-1]}
2010 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0191-1]}
2011 @ChgAdded{Version=[3],Text=[@Chg{Version=[5],New=[an @key[out] or
2012 @key[in out] parameter whose nominal type has a
2013 part of type @i<T>;],Old=[@Chg{Version=[4],New=[],Old=[is
2014 visible outside the
2015 immediate scope of type @i<T> or overrides an operation that is visible outside
2016 the immediate scope of @i<T>, and]}]}]}
2017
2018 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0075-1],ARef=[AI12-0191-1]}
2019 @ChgAdded{Version=[5],Text=[an access-to-object parameter or result
2020 whose designated nominal type has a part of type @i<T>; or]}
2021
2022 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0289-1]}
2023 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0042-1],ARef=[AI12-0044-1]}
2024 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0075-1],ARef=[AI12-0191-1]}
2025 @Comment{@ChgAdded{Version=[4],Type=[Leading],Text=[]} - Only this version is "Leading"}
2026 @ChgAdded{Version=[3],Text=[@Chg{Version=[5],New=[for a procedure or
2027 entry, an @b<in> parameter whose nominal type
2028 has a part of type @i<T>.],Old=[@Chg{Version=[4],New=[and
2029 either:], Old=[has a result with a part of type @i<T>, or one or more
2030 parameters with a part of type @i<T>, or an access to variable
2031 parameter whose designated type has a part of type @i<T>.]}]}]}
2032
2033 @begin{Ramification}
2034 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0167-1],ARef=[AI12-0191-1]}
2035 @ChgAdded{Version=[5],Text=[This is a @RuntimeTitle rule, so we ignore
2036 privacy when determining if a check is needed. We do, however, use the
2037 nominal type of objects to determine if a part of type @i<T> is present;
2038 therefore, parts that aren't known at compile-time (after ignoring privacy)
2039 are never subject to an invariant check. This is
2040 preferable, as we don't want overhead associated with the possibility
2041 that there @i<might> exist an extension of a tagged type that has a part
2042 of type @i<T>. (See the "leaks" note below for avoidance advice.)]}
2043 @end{Ramification}
2044
2045 @begin{Itemize}
2046 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0044-1]}
2047 @ChgRef{Version=[5],Kind=[DeletedAddedNoDelMsg],ARef=[AI12-0075-1]}
2048 @ChgAdded{Version=[4],Text=[@Chg{Version=[5],New=[],Old=[has a result with a
2049 part of type @i<T>, or]}]}
2050
2051 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0044-1]}
2052 @ChgRef{Version=[5],Kind=[DeletedAddedNoDelMsg],ARef=[AI12-0075-1]}
2053 @ChgAdded{Version=[4],Text=[@Chg{Version=[5],New=[],Old=[has one or more
2054 @key[out] or @key[in out] parameters with a part of type @i<T>, or]}]}
2055
2056 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0044-1],ARef=[AI12-0149-1]}
2057 @ChgRef{Version=[5],Kind=[DeletedAddedNoDelMsg],ARef=[AI12-0075-1]}
2058 @ChgAdded{Version=[4],Text=[@Chg{Version=[5],New=[],Old=[has an
2059 access-to-object parameter or result
2060 whose designated type has a part of type @i<T>, or]}]}
2061
2062 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0042-1],ARef=[AI12-0044-1]}
2063 @ChgRef{Version=[5],Kind=[DeletedAddedNoDelMsg],ARef=[AI12-0075-1]}
2064 @ChgAdded{Version=[4],Text=[@Chg{Version=[5],New=[],Old=[is a procedure
2065 or entry that has an @key[in] parameter with a part of type @i<T>,]}]}
2066
2067 @begin{Discussion}
2068 @ChgRef{Version=[4],Kind=[AddedNormal]}
2069 @ChgAdded{Version=[4],Text=[We don't check @key[in] parameters for
2070 functions to avoid infinite recursion for calls to public functions
2071 appearing in invariant expressions. Such function calls are unavoidable
2072 for class-wide invariants and likely for other invariants. This is the
2073 simplest rule that avoids trouble, and functions are much more likely to
2074 be queries that don't modify their parameters than other callable
2075 entities.]}
2076 @end{Discussion}
2077 @end{Itemize}
2078
2079 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0042-1]}
2080 @ChgRef{Version=[5],Kind=[DeletedAddedNoDelMsg],ARef=[AI12-0075-1]}
2081 @ChgAdded{Version=[4],Type=[Leading],Text=[@Chg{Version=[5],New=[],Old=[and either:]}]}
2082
2083 @begin{Itemize}
2084 @ChgRef{Version=[4],Kind=[Added]}
2085 @ChgRef{Version=[5],Kind=[DeletedAddedNoDelMsg],ARef=[AI12-0075-1]}
2086 @ChgAdded{Version=[4],Text=[@Chg{Version=[5],New=[],Old=[@i<T> is a
2087 private type or a private extension
2088 and the subprogram or entry is visible outside the immediate scope of
2089 type @i<T> or overrides an inherited operation that is visible outside
2090 the immediate scope of @i<T>, or]}]}
2091
2092 @ChgRef{Version=[4],Kind=[Added]}
2093 @ChgRef{Version=[5],Kind=[DeletedAddedNoDelMsg],ARef=[AI12-0075-1]}
2094 @ChgAdded{Version=[4],Text=[@Chg{Version=[5],New=[],Old=[@i<T> is a
2095 record extension, and the
2096 subprogram or entry is a primitive operation visible outside the
2097 immediate scope of type @i<T> or overrides an inherited operation that
2098 is visible outside the immediate scope of @i<T>.]}]}
2099 @end{Itemize}
2100 @end{Itemize}
2101 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0269-1]}
2102 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0075-1],ARef=[AI12-0338-1]}
2103 @ChgAdded{Version=[3],NoPrefix=[T],Text=[@Chg{Version=[5],New=[If the nominal
2104 type of a formal parameter (or the designated nominal type of an
2105 access-to-object parameter or result) is incomplete at the point of the
2106 declaration of the callable entity, and if the completion of that incomplete
2107 type does not occur in the same declaration list as the incomplete
2108 declaration, then for purposes of the preceding rules the nominal type is
2109 considered to have no parts],Old=[The check is performed on
2110 each such part]} of type @i<T>.]}
2111
2112 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0042-1]}
2113 @ChgAdded{Version=[4],Text=[For a view conversion to a class-wide type
2114 occurring within the immediate scope of @i<T>, from a specific type that is
2115 a descendant of @i<T> (including @i<T> itself), a check is performed
2116 on the part of the object that is of type @i<T>.]}
2117
2118 @begin{Reason}
2119 @ChgRef{Version=[4],Kind=[AddedNormal]}
2120 @ChgAdded{Version=[4],Text=[Class-wide objects are treated as though they
2121 exist outside the scope of every type, and may be passed across package
2122 "boundaries" freely without further invariant checks.]}
2123
2124 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0167-1]}
2125 @ChgAdded{Version=[5],Text=[Despite this model, if an object of type @i<T>
2126 that is a component of a class-wide object is modified within the scope of
2127 the full view of type @i<T>, then there is no invariant check for @i<T>
2128 at that point.]}
2129 @end{Reason}
2130
2131 @end{Itemize}
2132
2133 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0290-1]}
2134 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0080-1],ARef=[AI12-0159-1]}
2135 @ChgAdded{Version=[3],Text=[If performing checks is required by the
2136 @Chg{Version=[4],New=[Type_Invariant],Old=[Invariant]} or
2137 @Chg{Version=[4],New=[Type_Invariant'Class],Old=[Invariant'Class]} assertion
2138 policies (see
2139 @RefSecNum{Pragmas Assert and Assertion_Policy}) in effect at the point of
2140 @Chg{Version=[4],New=[the ],Old=[]}corresponding aspect specification
2141 applicable to a given type, then the respective invariant expression is
2142 considered @i(enabled).@Defn2{Term=[enabled],Sec=[invariant expression]}]}
2143
2144 @begin{Ramification}
2145 @ChgRef{Version=[3],Kind=[AddedNormal]}
2146 @ChgAdded{Version=[3],Text=[If a class-wide invariant expression is enabled
2147 for a type, it remains enabled when inherited by descendants of that type,
2148 even if the policy in effect is Ignore for the inheriting type.]}
2149 @end{Ramification}
2150
2151 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0250-1],ARef=[AI05-0289-1],ARef=[AI05-0290-1]}
2152 @ChgAdded{Version=[3],Text=[The invariant check consists of the evaluation of
2153 each enabled invariant expression that applies to @i<T>, on each of the objects
2154 specified above. If any of these evaluate to False,
2155 Assertions.Assertion_Error is raised at the point of the object
2156 initialization, conversion, or call.@Defn2{Term=(Assertion_Error),
2157 Sec=(raised by failure of runtime check)}@Defn2{Term=[check, language-defined],
2158 Sec=[controlled by assertion policy]}]} If a given call requires more than one
2159 evaluation of an invariant expression, either for multiple objects of a single
2160 type or for multiple types with invariants, the evaluations are performed in
2161 an arbitrary order, and if one of them evaluates to False, it is not specified whether
2162 the others are evaluated. Any invariant check is performed prior to copying back
2163 any by-copy @key[in out] or @key[out] parameters. Invariant checks,
2164 any postcondition check, and any
2165 constraint or predicate checks associated with @key[in out] or @key[out]
2166 parameters are performed in an arbitrary order.]}
2167
2168 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0150-1],ARef=[AI12-0159-1]}
2169 @ChgAdded{Version=[4],Text=[For an invariant check on a value of type @i<T1>
2170 based on a class-wide invariant expression inherited from an ancestor type
2171 @i<T>, any operations within the invariant expression that were resolved as
2172 primitive operations of the (notional) formal derived type @i<NT> are
2173 bound to the corresponding operations of type @i<T1> in the
2174 evaluation of the invariant expression for the check on @i<T1>.]}
2175
2176 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0247-1],ARef=[AI05-0250-1]}
2177 @ChgAdded{Version=[3],Text=[The invariant checks performed on a call are
2178 determined by the subprogram or entry actually invoked, whether directly, as
2179 part of a dispatching call, or as part of a call through an access-to-subprogram
2180 value.]}
2181
2182 @begin{Ramification}
2183 @ChgRef{Version=[3],Kind=[AddedNormal]}
2184 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0149-1]}
2185 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0167-1],ARef=[AI12-0210-1]}
2186 @ChgAdded{Version=[3],Text=[@Chg{Version=[5],New=[Type invariant checks are
2187 intended to prevent invariant-violating values from inadvertently "leaking
2188 out"; that is, code which cannot see the completion of the private type should
2189 not be able to reference invariant-violating values of the type (assuming the
2190 type invariant condition itself is well behaved - for example, no uses of
2191 global variables during evaluation of the invariant expression). This goal is
2192 not completely achieved; such leaking is possible but, importantly, it
2193 requires assistance (deliberate or not) of some form from the package that
2194 declares the invariant-bearing private type (or a child unit thereof); a
2195 client of a well-crafted package cannot use these holes to obtain an
2196 invariant-violating value without help.],Old=[Invariant checks on
2197 subprogram return are not
2198 performed on objects that are accessible only through access
2199 values@Chg{Version=[4],New=[ that are subcomponents of some other object],Old=[]}.
2200 It is also possible to call through an access-to-subprogram value and reach a
2201 subprogram body that has visibility on the full declaration of a type, from
2202 outside the immediate scope of the type. No invariant checks will be performed
2203 if the designated subprogram is not itself externally visible. These cases
2204 represent "holes" in the protection provided by invariant checks; but note
2205 that these holes cannot be caused by clients of the type @i<T> with the
2206 invariant@Chg{Version=[4],New=[. The designer of the package has to declare
2207 a visible type with an access-to-@i<T> subcomponent and use it as a parameter
2208 or result to subprograms in the package, or pass the client an
2209 access-to-subprogram value representing a private operation of the package.
2210 In the absence of such things, all values that the client can see will be
2211 checked for a private type or extension],Old=[ without help for the
2212 designer of the package containing @i<T>]}.]}]}
2213
2214 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0210-1]}
2215 @ChgAdded{Version=[5],Type=[Leading],Text=[The list of known techniques
2216 whereby this kind of leak can occur (ignoring things like erroneous execution
2217 and the various forms of unchecked type conversion) consists of:]}
2218
2219 @begin{Itemize}
2220
2221 @ChgRef{Version=[5],Kind=[AddedNormal]}
2222 @ChgAdded{Version=[5],Text=[A boundary entity might assign an
2223 invariant-violating value to a global variable that is visible to client
2224 code.]}
2225
2226 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0149-1]}
2227 @ChgAdded{Version=[5],Text=[Invariant checks on subprogram return are not
2228 performed on objects that are accessible only through access-valued
2229 components of other objects. This can only cause a leak if there is a type
2230 with access-valued components that is used as a parameter or result type of
2231 a boundary entity. For a type @i<T> that has a type invariant, avoiding the
2232 declaration of types with access-valued components designating objects with
2233 parts of @i<T> in the package that contains @i<T> is sufficient to prevent
2234 this leak.]}
2235
2236 @ChgRef{Version=[5],Kind=[AddedNormal]}
2237 @ChgAdded{Version=[5],Text=[A client could call through an
2238 access-to-subprogram value and reach a subprogram body that has visibility
2239 on the full declaration of a type; no invariant checks will be performed if
2240 the designated subprogram is not itself a boundary subprogram. This leak can
2241 only happen if an access-to-subprogram value of a subprogram that is not
2242 visible to clients is passed out to clients.]}
2243
2244 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0167-1],ARef=[AI12-0191-1]}
2245 @ChgAdded{Version=[5],Text=[Invariant checks are only performed for parts of
2246 the nominal type for tagged parameters and function results. This means that
2247 components of extensions are not checked (these would be very expensive to
2248 check as any tagged type @i<might> have such an extension in the future,
2249 even though that would be very unlikely). For this leak to occur for a type
2250 @i<T> that has a type invariant, the body of a boundary entity of @i<T>
2251 needs to have visibility on a type extension that has components of @i<T> or
2252 access-to-@i<T> and also has an ancestor type (or class) as a parameter or
2253 result of the subprogram.]}
2254
2255 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0338-1]}
2256 @ChgAdded{Version=[5],Text=[Invariant checks are not performed for parts of
2257 incomplete types when the completion is not available. For this leak to
2258 occur for a type @i<T> that has a type invariant and is declared in a
2259 package @i<P>, one has to use a @key{limited with} on a package that has
2260 @i<P> in its semantic closure, and then use a type from that package as a
2261 parameter or result of a boundary subprogram for @i<T> (or as the designated
2262 type of a parameter or result of such a subprogram).]}
2263
2264 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0210-1]}
2265 @ChgAdded{Version=[5],Text=[Consider a package @i<P> which declares an
2266 invariant-bearing private type @i<T> and a generic package @i<P.G1>, which
2267 in turn declares another generic package @i<P.G1.G2>. Outside of package
2268 @i<P>, there are declarations of an instantiation @i<I1> of @i<P.G1> and an
2269 instantiation @i<I2> of @i<I1.G2>. @i<I2> can declare visible subprograms
2270 whose bodies see the full view of @i<T> and yet these subprograms are not
2271 boundary subprograms (because the generic @i<I1.G2> is not declared within
2272 the immediate scope of @i<T> - @i<G1.G2> is, but that's irrelevant). So a
2273 call to one of these subprograms from outside of @i<P> could yield an
2274 invariant-violating value. So long as a nested generic of a nested generic
2275 unit of @i<P> is not declared, no such leaks are possible.]}
2276
2277 @end{Itemize}
2278
2279 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0210-1]}
2280 @ChgAdded{Version=[5],Text=[All of these leaks require cooperation of some
2281 form (as detailed above) from within the immediate scope of the
2282 invariant-bearing type.]}
2283
2284 @end{Ramification}
2285
2286 @begin{ImplNote}
2287 @ChgRef{Version=[3],Kind=[AddedNormal]}
2288 @ChgRef{Version=[5],Kind=[Revised]}@Comment{Paragraph number changed by above}
2289 @ChgAdded{Version=[3],Text=[The implementation might want to produce a warning
2290 if a private extension has an ancestor type that is a visible extension, and
2291 an invariant expression depends on the value of one of the components from a
2292 visible extension part.]}
2293 @end{ImplNote}
2294
2295 @end{Runtime}
2296
2297 @begin{Notes}
2298 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0250-1],ARef=[AI05-0269-1]}
2299 @ChgAdded{Version=[3],Text=[For a call of a primitive subprogram of type
2300 @i<NT> that is inherited from type @i<T>, the specified checks of the specific
2301 invariants of both the types @i<NT> and @i<T> are performed. For a call of a
2302 primitive subprogram of type @i<NT> that is overridden for type @i<NT>, the
2303 specified checks of the specific invariants of only type @i<NT> are
2304 performed.]}
2305
2306 @begin{TheProof}
2307 @ChgRef{Version=[3],Kind=[AddedNormal]}
2308 @ChgAdded{Version=[3],Text=[This follows from the definition of a call on
2309 an inherited subprogram as view conversions of the parameters of the type
2310 and a call to the original subprogram
2311 (see @RefSecNum{Derived Types and Classes}), along with the normal invariant
2312 checking rules. In particular, the call to the original subprogram takes
2313 care of any checks needed on type @i<T>, and the checks required
2314 on view conversions take care of any checks needed on type @i<NT>,
2315 specifically on @key[in out] and @key[out] parameters. We require this in
2316 order that the semantics of an explicitly defined wrapper that does nothing
2317 but call the original subprogram is the same as that of an inherited
2318 subprogram.]}
2319 @end{TheProof}
2320
2321 @end{Notes}
2322
2323 @begin{Examples}
2324 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0312-1]}
2325 @ChgAdded{Version=[5],Text=[@i{A work scheduler where only urgent work can be
2326 scheduled for weekends:}]}
2327
2328 @begin{Example}
2329 @ChgRef{Version=[5],Kind=[AddedNormal]}
2330 @ChgAdded{Version=[5],Text=[@key[package] Work_Orders @key[is]]}
2331
2332 @ChgRef{Version=[5],Kind=[AddedNormal]}
2333 @ChgAdded{Version=[5],Text=[ --@Examcom{ See 3.5.1 for type declarations of Level, Day, and Weekday}]}
2334
2335 @ChgRef{Version=[5],Kind=[AddedNormal]}
2336 @ChgAdded{Version=[5],Text=[ @key[type] Work_Order @key[is private with]
2337 Type_Invariant => Day_Scheduled (Work_Order) @key[in] Weekday
2338 @key[or else] Priority (Work_Order) = Urgent;]}
2339
2340 @ChgRef{Version=[5],Kind=[AddedNormal]}
2341 @ChgAdded{Version=[5],Text=[ @key[function] Schedule_Work (Urgency : @key[in] Level;
2342 To_Occur : @key[in] Day) @key[return] Work_Order
2343 @key[with] Pre => Urgency = Urgent @key[or else] To_Occur @key[in] Weekday;]}
2344
2345 @ChgRef{Version=[5],Kind=[AddedNormal]}
2346 @ChgAdded{Version=[5],Text=[ @key[function] Day_Scheduled (Order : @key[in] Work_Order) @key[return] Day;]}
2347
2348 @ChgRef{Version=[5],Kind=[AddedNormal]}
2349 @ChgAdded{Version=[5],Text=[ @key[function] Priority (Order : @key[in] Work_Order) @key[return] Level;]}
2350
2351 @ChgRef{Version=[5],Kind=[AddedNormal]}
2352 @ChgAdded{Version=[5],Text=[ @key[procedure] Change_Priority (Order : @key[in out] Work_Order;
2353 New_Priority : @key[in] Level;
2354 Changed : @key[out] Boolean)
2355 @key[with] Post => Changed = (Day_Scheduled(Order) @key[in] Weekday
2356 @key[or else] Priority(Order) = Urgent);]}
2357
2358 @ChgRef{Version=[5],Kind=[AddedNormal]}
2359 @ChgAdded{Version=[5],Text=[@key[private]]}
2360
2361 @ChgRef{Version=[5],Kind=[AddedNormal]}
2362 @ChgAdded{Version=[5],Text=[ @key[type] Work_Order @key[is record]
2363 Scheduled : Day;
2364 Urgency : Level;
2365 @key[end record];]}
2366
2367 @ChgRef{Version=[5],Kind=[AddedNormal]}
2368 @ChgAdded{Version=[5],Text=[@key[end] Work_Orders;]}
2369
2370 @ChgRef{Version=[5],Kind=[AddedNormal]}
2371 @ChgAdded{Version=[5],Text=[@key[package body] Work_Orders @key[is]]}
2372
2373 @ChgRef{Version=[5],Kind=[AddedNormal]}
2374 @ChgAdded{Version=[5],Text=[ @key[function] Schedule_Work (Urgency : @key[in] Level;
2375 To_Occur : @key[in] Day) @key[return] Work_Order @key[is]
2376 (Scheduled => To_Occur, Urgency => Urgency);]}
2377
2378 @ChgRef{Version=[5],Kind=[AddedNormal]}
2379 @ChgAdded{Version=[5],Text=[ @key[function] Day_Scheduled (Order : @key[in] Work_Order) @key[return] Day @key[is] (Order.Scheduled);]}
2380
2381 @ChgRef{Version=[5],Kind=[AddedNormal]}
2382 @ChgAdded{Version=[5],Text=[ @key[function] Priority (Order : @key[in] Work_Order) @key[return] Level @key[is] (Order.Urgency);]}
2383
2384 @ChgRef{Version=[5],Kind=[AddedNormal]}
2385 @ChgAdded{Version=[5],Text=[ @key[procedure] Change_Priority (Order : @key[in out] Work_Order;
2386 New_Priority : @key[in] Level;
2387 Changed : @key[out] Boolean) @key[is]
2388 @key[begin]
2389 --@Examcom{ Ensure type invariant is not violated}
2390 @key[if] Order.Urgency = Urgent @key[or else] (Order.Scheduled @key[in] Weekday) @key[then]
2391 Changed := True;
2392 Order.Urgency := New_Priority;
2393 @key[else]
2394 Changed := False;
2395 @key[end if];
2396 @key[end] Change_Priority;]}
2397
2398 @ChgRef{Version=[5],Kind=[AddedNormal]}
2399 @ChgAdded{Version=[5],Text=[@key[end] Work_Orders;]}
2400 @end{Example}
2401 @end{Examples}
2402
2403
2404 @begin{Extend2005}
2405 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0247-1],ARef=[AI05-0250-1],ARef=[AI05-0289-1]}
2406 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
2407 Type_Invariant aspects are new.]}
2408 @end{Extend2005}
2409
2410 @begin{Inconsistent2012}
2411 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0042-1]}
2412 @ChgAdded{Version=[4],Text=[@Defn{inconsistencies with Ada 2012}
2413 @b<Corrigendum:> Clarified the definition of when invariant checks occur
2414 for inherited subprograms. This might cause checks to be added or removed
2415 in some cases. These are all rare cases involving class-wide type
2416 invariants and either record extensions or multiple levels of derivation.
2417 Additionally, implementations probably make the checks as the intent seems
2418 clear, even though the formal language did not include them. So we do not
2419 expect this to be a problem in practice.]}
2420
2421 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0042-1]}
2422 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added invariant checks for
2423 conversions to class-wide types. This might cause an invariant check to
2424 fail in some cases where they would not be made in the original definition
2425 of Ada 2012. Such cases represent a hole where a value that fails an
2426 invariant could "leak out" of a package, and as such will detect far more
2427 bugs than it causes.]}
2428
2429 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0044-1]}
2430 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Removed the invariant check
2431 for @key[in] parameters of
2432 functions, so that typical invariants don't cause infinite recursion.
2433 This is strictly inconsistent, as the Ada 2012 definition has this check;
2434 therefore, programs could depend on Assertion_Error being raised upon the
2435 return from some call on a public function. However, as the intent of
2436 assertion checking is to uncover bugs, a program that depends on a bug
2437 occurring seems very unlikely.]}
2438
2439 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0049-1],ARef=[AI12-0149-1]}
2440 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added an invariant check for
2441 deferred constants and for access values returned from functions, so they
2442 cannot be used to @ldquote@;leak@rdquote values that violate the invariant
2443 from a package. This is strictly inconsistent, as the Ada 2012 definition
2444 is missing these checks; therefore, programs could depend on using values
2445 that violate an invariant outside of the package of definition. These will
2446 not raise Assertion_Error in Ada 2012 as defined in the Ada 2012 Standard,
2447 but ought to do so (as noted by this change). As these are a violation of
2448 the intent of invariants, we think that this change will mainly reveal bugs
2449 rather than cause them.]}
2450
2451 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0150-1],ARef=[AI12-0159-1]}
2452 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Eliminated unintentional
2453 redispatching from class-wide type invariants. This means that a different
2454 body might be evaluated for a type invariant check where the value
2455 has a different tag than that of the type. The change means that the behavior
2456 of Type_Invariant and Type_Invariant'Class will be the same for a particular
2457 subprogram, and that the known behavior of the operations can be assumed.
2458 We expect that this change will primarily fix bugs, as it will make
2459 class-wide type invariants work more like expected. In the case where
2460 redispatching is desired, an explicit conversion to a class-wide type can be
2461 used.]}
2462
2463 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0199-1]}
2464 @ChgAdded{Version=[5],Text=[@b<Correction:> Class-wide type invariants are
2465 no longer checked for abstract types. Thus, a program that previously
2466 raised Assertion_Error because of a call to a concrete subprogram of an
2467 abstract type will no longer do so. However, programs that depend on assertion
2468 failure are likely to be very rare, some explicit conversion to
2469 the abstract type is needed to get static binding, and additionally many such
2470 checks would call abstract functions (likely causing some compiler failure).
2471 As such, this incompatibility most likely will never be seen in practice.]}
2472 @end{Inconsistent2012}
2473
2474 @begin{Incompatible2012}
2475 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0042-1]}
2476 @ChgAdded{Version=[4],Text=[@Defn{incompatiblities with Ada 2012}
2477 @b<Corrigendum:> A private operation that is inherited in the visible
2478 part of a package to which a class-wide invariant applies now requires
2479 overriding. This is a very unlikely situation, and will prevent problems
2480 with invariant checks being added to routines that assume that they don't
2481 have them.]}
2482 @end{Incompatible2012}
2483
2484 @begin{Extend2012}
2485 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0041-1]}
2486 @ChgAdded{Version=[4],Text=[@Defn{extensions to Ada 2012}
2487 @b<Corrigendum:> Class-wide type invariants can now be specified on
2488 interfaces as well as private types.]}
2489 @end{Extend2012}
2490
2491 @begin{DiffWord2012}
2492 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0133-1]}
2493 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Clarified that all objects
2494 that are initialized by default should have an invariant check, and
2495 added an exception for types with unknown discriminants, as in that
2496 case the client cannot declare a default-initialized object. This
2497 exception to the check is formally inconsistent, but since it is only
2498 removing an assertion failure that occurs where no assertion should be
2499 checked anyway (meaning it's more likely to fix a bug than cause one),
2500 and programs depending on assertion failure should be very rare outside of
2501 test cases, we don't document this as inconsistent.]}
2502
2503 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0075-1],ARef=[AI12-0191-1]}
2504 @ChgAdded{Version=[5],Text=[Defined the term @ldquote@;boundary entity@rdquote
2505 to separate the static rules from the dynamic rules, and allow rules in other
2506 subclauses to reference these rules. No semantic change is intended.]}
2507
2508 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0191-1]}
2509 @ChgAdded{Version=[5],Text=[Clarified that invariant checks only apply
2510 to parts of the nominal type of objects.]}
2511
2512 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0193-1]}
2513 @ChgAdded{Version=[5],Text=[@b<Correction:> Clarified when type invariant
2514 checks happen for protected actions and entry calls.]}
2515
2516 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0338-1]}
2517 @ChgAdded{Version=[5],Text=[@b<Correction:> Clarified that type invariant
2518 checks do not occur for parts of incomplete types unless the completion
2519 is available.]}
2520 @end{DiffWord2012}
2521
2522
2523 @LabeledAddedSubClause{Version=[5],Name=[Default Initial Conditions]}
2524
2525
2526 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0265-1],ARef=[AI12-0272-1]}
2527 @ChgAdded{Version=[5],Type=[Leading],Text=[For a private type or private
2528 extension (including a generic formal type), the following
2529 language-defined aspect may be specified with an @nt{aspect_specification}
2530 (see @RefSecNum{Aspect Specifications}):]}
2531
2532 @begin{Description}
2533 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0265-1]}
2534 @ChgAdded{Version=[5],Text=[Default_Initial_Condition@\This aspect shall be
2535 specified by an @nt{expression}, called a @i<default initial condition
2536 expression>. Default_Initial_Condition may be specified on a
2537 @nt{private_type_declaration}, a @nt{private_extension_declaration},
2538 a @nt{formal_private_type_definition}, or a @nt{formal_derived_type_definition}.
2539 @Defn{default initial condition expression}
2540 @AspectDefn{Default_Initial_Condition}]}
2541
2542 @ChgAspectDesc{Version=[5],Kind=[AddedNormal],Aspect=[Default_Initial_Condition],
2543 Text=[@ChgAdded{Version=[5],Text=[A condition that must hold true after
2544 the default initialization of an object.]}]}
2545 @end{Description}
2546
2547 @ChgToGlossary{Version=[5],Kind=[Added],Term=<Default Initial Condition>,
2548 Text=<@ChgAdded{Version=[5],Text=[A property that holds for every
2549 default-initialized object of a given type.]}>}
2550
2551
2552 @begin{Resolution}
2553
2554 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0265-1]}
2555 @ChgAdded{Version=[5],Text=[The expected type for a default initial condition
2556 expression is any boolean type.]}
2557
2558 @end{Resolution}
2559
2560 @begin{Legality}
2561
2562 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0265-1]}
2563 @ChgAdded{Version=[5],Text=[The Default_Initial_Condition aspect shall not be
2564 specified for a type whose partial view has unknown
2565 discriminants@Redundant[, whether explicitly declared or inherited].]}
2566
2567 @end{Legality}
2568
2569 @begin{StaticSem}
2570
2571 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0265-1]}
2572 @ChgAdded{Version=[5],Text=[If the Default_Initial_Condition aspect is specified for
2573 a type T, then the default initial condition expression applies to T and to all
2574 descendants of T.]}
2575
2576 @end{StaticSem}
2577
2578 @begin{Runtime}
2579
2580 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0265-1]}
2581 @ChgAdded{Version=[5],Text=[If one or more default initial condition expressions
2582 apply to a type T, then a default initial condition check is performed after
2583 successful initialization of an object of type T by default (see
2584 @RefSecNum{Object Declarations}).
2585 In the case of a controlled type, the check is performed after the call to the
2586 type's Initialize procedure (see @RefSecNum{Assignment and Finalization}).]}
2587
2588 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0265-1]}
2589 @ChgAdded{Version=[5],Text=[If performing checks is required by the Default_Initial_Condition
2590 assertion policy (see @RefSecNum{Pragmas Assert and Assertion_Policy}) in
2591 effect at the point of the
2592 corresponding @nt{aspect_specification} applicable to a given type, then
2593 the respective default initial condition expression is
2594 considered enabled.]}
2595
2596 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0265-1]}
2597 @ChgAdded{Version=[5],Text=[The default initial condition check consists of the
2598 evaluation of each enabled default initial condition expression that applies to
2599 T. These evaluations, if there are more than one, are performed in an arbitrary
2600 order. If any of these evaluate to False, Assertions.Assertion_Error is raised
2601 at the point of the object initialization.@Defn{default initial condition check}@Defn2{Term=[check, language-defined],
2602 Sec=[controlled by assertion policy]}@Defn2{Term=(Assertion_Error),
2603 Sec=(raised by failure of runtime check)}]}
2604
2605 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0272-1]}
2606 @ChgAdded{Version=[5],Text=[@Redundant[For a generic formal type T, default
2607 initial condition checks performed are as determined by the actual type, along
2608 with any default initial condition of the formal type itself.]]}
2609
2610 @begin{TheProof}
2611 @ChgRef{Version=[5],Kind=[AddedNormal]}
2612 @ChgAdded{Version=[5],Text=[This follows from the general dynamic semantics
2613 rules given above, but we mention it explicitly so that there can be no doubt
2614 that it is intended.]}
2615 @end{TheProof}
2616
2617 @end{Runtime}
2618
2619 @begin{ImplPerm}
2620
2621 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0332-1]}
2622 @ChgAdded{Version=[5],Text=[Implementations may extend the syntax or semantics
2623 of the Default_Initial_Condition aspect in an implementation-defined manner.]}
2624
2625 @ChgImplDef{Version=[5],Kind=[AddedNormal],InitialVersion=[5],
2626 Text=[@ChgAdded{Version=[5],Text=[Any extensions of the
2627 Default_Initial_Condition aspect.]}]}
2628
2629 @begin{Reason}
2630 @ChgRef{Version=[5],Kind=[AddedNormal]}
2631 @ChgAdded{Version=[5],Text=[This is intended to allow preexisting usages
2632 from SPARK 2014 to remain acceptable in conforming implementations, as well
2633 as to provide future flexibility. Note the word @ldquote@;extend@rdquote
2634 in this permission; we expect that any aspect usage
2635 that conforms with the (other) rules of this clause will be accepted by
2636 any Ada implementation, regardless of any implementation-defined extensions.]}
2637 @end{Reason}
2638 @end{ImplPerm}
2639
2640 @begin{Notes}
2641 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0312-1]}
2642 @ChgAdded{Version=[5],Text=[For an example of the use of this aspect, see the
2643 Vector container definition in @RefSecNum{The Generic Package Containers.Vectors}.]}
2644 @end{Notes}
2645
2646 @begin{Extend2012}
2647 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0265-1],ARef=[AI12-0272-1]}
2648 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}
2649 Aspect Default_Initial_Condition is new.]}
2650 @end{Extend2012}
2651
2652
2653 @LabeledAddedSubClause{Version=[5],Name=[Stable Properties of a Type]}
2654
2655 @begin{Intro}
2656 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0324-1]}
2657 @ChgAdded{Version=[5],Text=[Certain characteristics of an object of a given
2658 type are unchanged by most of the primitive operations of the type. Such
2659 characteristics are called @i{stable properties} of the
2660 type.@Defn{stable property}@Defn2{Term=[property],Sec=[stable]}]}
2661
2662 @ChgToGlossary{Version=[5],Kind=[Added],Term=<Stable Property>,
2663 Text=<@ChgAdded{Version=[5],Text=[A characteristic associated with objects of a
2664 given type that is preserved by many of the primitive operations of the
2665 type.]}>}
2666 @end{Intro}
2667
2668 @begin{StaticSem}
2669
2670 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1]}
2671 @ChgAdded{Version=[5],Text=[A @i{property function} of type @i<T> is a function
2672 with a single parameter of type @i<T> or of a class-wide type that covers
2673 @i<T>.@Defn{property function}@Defn2{Term=[function],Sec=[property]}]}
2674
2675 @begin{Reason}
2676 @ChgRef{Version=[5],Kind=[AddedNormal]}
2677 @ChgAdded{Version=[5],Text=[This provides restrictions on name resolution so
2678 overloaded functions can be used as a stable property function.]}
2679 @end{Reason}
2680
2681 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0285-1],ARef=[AI12-0324-1]}
2682 @ChgAdded{Version=[5],Text=[A @i<type property aspect definition> is
2683 @Defn{type property aspect definition}a list of @nt{name}s written in the syntax
2684 of a @nt{positional_array_aggregate}. A @i<subprogram property aspect
2685 definition>@Defn{subprogram property aspect definition} is a list of @nt{name}s
2686 preceded by an optional @key[not], also written in the syntax of a
2687 @nt{positional_array_aggregate}.]}
2688
2689 @begin{Honest}
2690 @ChgRef{Version=[5],Kind=[AddedNormal]}
2691 @ChgAdded{Version=[5],Text=[A single @nt{name} would technically be a
2692 parenthesized @nt{expression} rather than an @nt{aggregate}; we mean to
2693 include that here. We say "syntax of a @nt{positional_array_aggregate}"
2694 to hopefully clarify that the specification
2695 is in no other way an actual @nt{aggregate}.]}
2696 @end{Honest}
2697
2698
2699 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0272-1]}
2700 @ChgAdded{Version=[5],Type=[Leading],Text=[For a nonformal private type,
2701 nonformal private extension, or full type that does not have a partial view, the
2702 following language-defined aspects may be specified with an
2703 @nt{aspect_specification} (see @RefSecNum{Aspect Specifications}):]}
2704
2705 @begin{Description}
2706 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0285-1]}
2707 @ChgAdded{Version=[5],Text=[Stable_Properties@\This aspect shall be specified by
2708 a type property aspect definition; each @nt{name} shall statically denote
2709 a single property function of the type. This aspect defines the
2710 @i<stable property functions> of the associated
2711 type.@Defn2{Term=[stable property function],Sec=[of a type]}@Defn2{Term=[function],Sec={stable property}}
2712 @Defn2{Term=[type],Sec=[stable property function of]}@AspectDefn{Stable_Properties}]}
2713
2714 @begin{Discussion}
2715 @ChgRef{Version=[5],Kind=[AddedNormal]}
2716 @ChgAdded{Version=[5],Text=[We do not allow this aspect on generic formal
2717 types, as it is only meaningful for primitive subprograms and generic formal
2718 types have no such subprograms.]}
2719 @end{Discussion}
2720
2721 @begin{Ramification}
2722 @ChgRef{Version=[5],Kind=[AddedNormal]}
2723 @ChgAdded{Version=[5],Text=[Class-wide aspects are only allowed on tagged types
2724 (see @RefSecNum{Aspect Specifications}), so we don't say that here.]}
2725 @end{Ramification}
2726
2727 @ChgAspectDesc{Version=[5],Kind=[AddedNormal],Aspect=[Stable_Properties],
2728 Text=[@ChgAdded{Version=[5],Text=[A list of functions
2729 describing characteristics that usually are unchanged by primitive
2730 operations of the type or an individual primitive subprogram.]}]}
2731
2732 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0285-1]}
2733 @ChgAdded{Version=[5],Text=[Stable_Properties'Class@\This aspect shall be
2734 specified by a type property aspect definition; each
2735 @nt{name} shall statically denote a single property function of the type. This
2736 aspect defines the @i{class-wide stable property functions} of the associated
2737 type. @Redundant[Unlike most class-wide aspects, Stable_Properties'Class is not
2738 inherited by descendant types and subprograms, but the enhanced class-wide
2739 postconditions are inherited in the normal
2740 manner.]@Defn{class-wide stable property function}@Defn2{Term=[stable property function],Sec=[class-wide]}@AspectDefn{Stable_Properties'Class}]}
2741
2742 @begin{TheProof}
2743 @ChgRef{Version=[5],Kind=[AddedNormal]}
2744 @ChgAdded{Version=[5],Text=[Class-wide inheritance has to be explicitly
2745 defined. Here we are not making such a definition, so there is no inheritance.
2746 @RefSecNum{Preconditions and Postconditions} defines the inheritance of
2747 class-wide postconditions.]}
2748 @end{TheProof}
2749
2750 @begin{Discussion}
2751 @ChgRef{Version=[5],Kind=[AddedNormal]}
2752 @ChgAdded{Version=[5],Text=[Since class-wide postconditions are inherited by
2753 descendants, we don't need the stable property functions to be inherited; if
2754 they were inherited, we'd be duplicating the checks, which we don't want.]}
2755 @end{Discussion}
2756
2757 @begin{Ramification}
2758 @ChgRef{Version=[5],Kind=[AddedNormal]}
2759 @ChgAdded{Version=[5],Text=[Class-wide aspects are only allowed on primitive
2760 subprograms of tagged types (see @RefSecNum{Aspect Specifications}), so we
2761 don't say that here.]}
2762 @end{Ramification}
2763
2764 @ChgAspectDesc{Version=[5],Kind=[AddedNormal],Aspect=[Stable_Properties'Class],
2765 Text=[@ChgAdded{Version=[5],Text=[A list of functions
2766 describing characteristics that usually are unchanged by primitive
2767 operations of a class of types or a primitive subprogram for such a class.]}]}
2768
2769 @end{Description}
2770
2771
2772 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1]}
2773 @ChgAdded{Version=[5],Type=[Leading],Text=[For a primitive subprogram, the
2774 following language-defined aspects may be specified with an
2775 @nt{aspect_specification} (see @RefSecNum{Aspect Specifications}):]}
2776
2777 @begin{Description}
2778 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0285-1]}
2779 @ChgAdded{Version=[5],Text=[Stable_Properties@\This aspect shall be specified by
2780 a subprogram property aspect definition; each @nt{name} shall statically
2781 denote a single property function of a type for which the associated subprogram
2782 is primitive.]}
2783
2784 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0285-1]}
2785 @ChgAdded{Version=[5],Text=[Stable_Properties'Class@\This aspect shall be
2786 specified by a subprogram property aspect definition; each @nt{name} shall
2787 statically denote a single property function of a tagged type for which the
2788 associated subprogram is primitive. @Redundant[Unlike most class-wide aspects,
2789 Stable_Properties'Class is not inherited by descendant subprograms, but the
2790 enhanced class-wide postconditions are inherited in the normal manner.]]}
2791
2792 @begin{Reason}
2793 @ChgRef{Version=[5],Kind=[AddedNormal]}
2794 @ChgAdded{Version=[5],Text=[The subprogram versions of Stable_Properties are
2795 provided to allow overriding the stable properties of a type for an individual
2796 primitive subprogram. While they can be used even if the type has no stable
2797 properties, that is not an intended use (as simply modifying the postcondition
2798 directly makes more sense for something that only happens in one place).]}
2799 @end{Reason}
2800
2801 @end{Description}
2802
2803 @end{StaticSem}
2804
2805 @begin{Legality}
2806
2807 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1]}
2808 @ChgAdded{Version=[5],Type=[Leading],Text=[A stable property function of a type
2809 @i<T> (including a class-wide stable property function) shall have a nonlimited
2810 return type and shall be:]}
2811
2812 @begin{Itemize}
2813 @ChgRef{Version=[5],Kind=[AddedNormal]}
2814 @ChgAdded{Version=[5],Text=[a primitive function with a single parameter of
2815 mode @key[in] of type @i<T>; or]}
2816
2817 @ChgRef{Version=[5],Kind=[AddedNormal]}
2818 @ChgAdded{Version=[5],Text=[a function that is declared immediately within the
2819 declarative region in which an ancestor type of @i<T> is declared and has a
2820 single parameter of mode @key[in] of a class-wide type that covers @i<T>.]}
2821 @end{Itemize}
2822
2823 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0285-1]}
2824 @ChgAdded{Version=[5],Type=[Leading],Text=[In a
2825 subprogram property aspect definition for a subprogram @i<S>:]}
2826
2827 @begin{Itemize}
2828
2829 @ChgRef{Version=[5],Kind=[AddedNormal]}
2830 @ChgAdded{Version=[5],Text=[all or none of the items shall be preceded by
2831 @key[not];]}
2832
2833 @begin{Ramification}
2834 @ChgRef{Version=[5],Kind=[AddedNormal]}
2835 @ChgAdded{Version=[5],Text=[Mixing @key[not] functions with
2836 regular functions is not allowed.]}
2837 @end{Ramification}
2838
2839 @ChgRef{Version=[5],Kind=[AddedNormal]}
2840 @ChgAdded{Version=[5],Text=[any property functions mentioned after @key[not]
2841 shall be a stable property function of a type for which @i<S> is primitive.]}
2842 @end{Itemize}
2843
2844 @end{Legality}
2845
2846 @begin{StaticSem}
2847
2848 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1]}
2849 @ChgAdded{Version=[5],Type=[Leading],Text=[For a primitive subprogram @i<S> of
2850 a type @i<T>, the stable property functions of @i<S> for type @i<T> are:]}
2851
2852 @begin{Itemize}
2853
2854 @ChgRef{Version=[5],Kind=[AddedNormal]}
2855 @ChgAdded{Version=[5],Text=[if @i<S> has an aspect Stable_Properties specified
2856 that does not include @key[not], those functions denoted in the aspect
2857 Stable_Properties for @i<S> that have a parameter of @i<T> or @i<T>'Class;]}
2858
2859 @ChgRef{Version=[5],Kind=[AddedNormal]}
2860 @ChgAdded{Version=[5],Text=[if @i<S> has an aspect Stable_Properties specified
2861 that includes @key[not], those functions denoted in the aspect
2862 Stable_Properties for @i<T>, excluding those denoted in the aspect
2863 Stable_Properties for @i<S>;]}
2864
2865 @ChgRef{Version=[5],Kind=[AddedNormal]}
2866 @ChgAdded{Version=[5],Text=[if @i<S> does not have an aspect
2867 Stable_Properties, those functions denoted in the aspect Stable_Properties
2868 for @i<T>, if any.]}
2869
2870 @end{Itemize}
2871
2872 @begin{Discussion}
2873 @ChgRef{Version=[5],Kind=[AddedNormal]}
2874 @ChgAdded{Version=[5],Text=[A primitive subprogram can be primitive for
2875 more than one type, and thus there can be more than one such set of stable
2876 properties for a subprogram. Thus we say @ldquote@;stable property
2877 functions for subprogram @i<S> for type @i<T>@rdquote.]}
2878 @end{Discussion}
2879
2880 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1]}
2881 @ChgAdded{Version=[5],Text=[A similar definition applies for class-wide stable
2882 property functions by replacing aspect Stable_Properties with aspect
2883 Stable_Properties'Class in the above definition.]}
2884
2885 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1]}
2886 @ChgAdded{Version=[5],Text=[The @i<explicit> specific postcondition expression
2887 for a subprogram @i<S> is the @nt{expression} directly specified for @i<S> with
2888 the Post aspect. Similarly, the @i<explicit> class-wide postcondition expression
2889 for a subprogram @i<S> is the @nt{expression} directly specified for @i<S> with
2890 the Post'Class aspect.@Defn{explicit specific postcondition expression}@Defn{explicit class-wide postcondition expression}@Defn2{Term=[postcondition expression],Sec=(explicit)}]}
2891
2892 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0324-1]}
2893 @ChgAdded{Version=[5],Text=[For every primitive subprogram @i<S> of a type @i<T>
2894 that is not a stable property function of @i<T>, the specific postcondition
2895 expression of @i<S> is modified to include expressions of the
2896 form @exam{@i<F>(@i<P>) = @i<F>(@i<P>)'Old}, all @key[and]ed with each other
2897 and any explicit specific postcondition expression, with one such equality
2898 included for each stable property function @i<F> of @i<S> for type @i<T> that
2899 does not occur in the explicit specific postcondition expression of @i<S>, and
2900 @i<P> is each parameter of @i<S> that has type @i<T>. The resulting specific
2901 postcondition expression of @i<S> is used in place of the explicit specific
2902 postcondition expression of @i<S> @Redundant[when interpreting the meaning of
2903 the postcondition as defined in @RefSecNum{Preconditions and Postconditions}].]}
2904
2905 @begin{Ramification}
2906 @ChgRef{Version=[5],Kind=[AddedNormal]}
2907 @ChgAdded{Version=[5],Text=[There is one @exam{F(P) = F(P)'Old}
2908 subexpression for every combination of a stable expression function of
2909 type @i<T> and a parameter of type @i<T>. For instance, if there are three
2910 stable property functions for type @i<T> and two parameters of type @i<T>,
2911 then there are six such subexpressions appended to the postcondition.]}
2912
2913 @ChgRef{Version=[5],Kind=[AddedNormal]}
2914 @ChgAdded{Version=[5],Text=[The resulting specific postcondition is
2915 evaluated as described in @RefSecNum{Preconditions and Postconditions}. One
2916 hopes that compilers can be smart enough to prove that many of these added
2917 postcondition subexpressions cannot fail, but that is not required here.]}
2918 @end{Ramification}
2919
2920 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0324-1]}
2921 @ChgAdded{Version=[5],Text=[For every primitive subprogram @i<S> of a type @i<T>
2922 that is not a stable property function of @i<T>, the class-wide postcondition
2923 expression of @i<S> is modified to include expressions of the form
2924 @exam{@i<F>(@i<P>) = @i<F>(@i<P>)'Old}, all @key[and]ed with each other and any
2925 explicit class-wide postcondition expression, with one such equality included
2926 for each class-wide stable property function @i<F> of @i<S> for type @i<T> that
2927 does not occur in any class-wide postcondition expression that applies to @i<S>,
2928 and @i<P> is each parameter of @i<S> that has type @i<T>. The resulting
2929 class-wide postcondition expression of @i<S> is used in place of the explicit
2930 class-wide postcondition expression of @i<S> @Redundant[when interpreting the
2931 meaning of the postcondition as defined in
2932 @RefSecNum{Preconditions and Postconditions}].]}
2933
2934 @begin{Reason}
2935 @ChgRef{Version=[5],Kind=[AddedNormal]}
2936 @ChgAdded{Version=[5],Text=[We suppress stable property expressions if the
2937 property function appears in the explicit class-wide postcondition, or in
2938 any inherited class-wide postconditions. If we didn't do that, we could have
2939 conflicting requirements in an inherited postcondition and the current one.
2940 We also avoid redundant property checks.]}
2941 @end{Reason}
2942
2943 @begin{Ramification}
2944 @ChgRef{Version=[5],Kind=[AddedNormal]}
2945 @ChgAdded{Version=[5],Text=[The resulting class-wide postcondition is
2946 evaluated as described in @RefSecNum{Preconditions and Postconditions}. In
2947 particular, the enhanced class-wide postcondition @i<is> the class-wide
2948 postcondition for @i<S>, and therefore inherited postconditions include any
2949 stable property expressions for @i<S>.]}
2950 @end{Ramification}
2951 @end{StaticSem}
2952
2953 @begin{Notes}
2954 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0112-1]}
2955 @ChgAdded{Version=[5],Text=[For an example of the use of these aspects,
2956 see the Vector container definition in
2957 @RefSecNum{The Generic Package Containers.Vectors}.]}
2958 @end{Notes}
2959
2960 @begin{Extend2012}
2961 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0187-1],ARef=[AI12-0285-1]}
2962 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}
2963 These aspects are new.]}
2964 @end{Extend2012}
2965
2966
2967 @NotISORMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
2968 @LabeledClause{Deferred Constants}
2969
2970 @begin{Intro}
2971 @redundant[Deferred constant declarations may be used to declare constants
2972 in the visible part of a package,
2973 but with the value of the constant given in the private part.
2974 They may also be used to declare constants imported from other
2975 languages (see @RefSecNum{Interface to Other Languages}).]
2976 @end{Intro}
2977
2978 @begin{Legality}
2979 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1],ARef=[AI05-0269-1]}
2980 @Redundant[@Defn{deferred constant declaration}
2981 A @i(deferred constant declaration) is an @nt<object_declaration>
2982 with the reserved word @key(constant) but no initialization expression.]
2983 @Defn{deferred constant}
2984 The constant declared by a deferred constant declaration is called
2985 a @i{deferred constant}.
2986 @PDefn2{Term=[requires a completion], Sec=(deferred constant declaration)}
2987 @Chg{Version=[3],New=[@Redundant[Unless the Import aspect
2988 (see @RefSecNum{Interfacing Aspects}) is True for a deferred constant
2989 declaration, the ]],Old=[A]} deferred constant declaration requires a completion,
2990 which shall be a full constant declaration
2991 (called the @i{full declaration} of the deferred
2992 constant)@Chg{Version=[3],New=[],Old=[,
2993 or a @nt{pragma} Import (see @RefSecNum(Interface to Other Languages))]}.
2994 @Defn{full declaration}
2995 @begin{TheProof}
2996 The first sentence is redundant, as it is stated officially in
2997 @RefSecNum(Object Declarations).
2998
2999 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0229-1],ARef=[AI05-0269-1]}
3000 @ChgAdded{Version=[3],Text=[The first part of the last sentence is redundant,
3001 as no imported entity may have a completion, as stated in
3002 @RefSecNum{Interfacing Aspects}.]}
3003 @end{TheProof}
3004
3005 @leading@;A deferred constant declaration that is completed
3006 by a full constant declaration shall occur immediately
3007 within the visible part of a @nt<package_specification>.
3008 For this case, the following additional rules apply to the
3009 corresponding full declaration:
3010 @begin(itemize)
3011 The full declaration shall occur immediately
3012 within the private part of the same package;
3013
3014 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00385-01]}
3015 The deferred and full constants shall have the same type@Chg{Version=[2],
3016 New=[, or shall have statically matching anonymous access subtypes],Old=[]};
3017 @begin{Ramification}
3018 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00385-01]}
3019 This implies that
3020 both the deferred declaration and the full declaration
3021 have to have a @nt<subtype_indication>@Chg{Version=[2],New=[ or
3022 @nt{access_definition}],Old=[]} rather than an
3023 @nt<array_type_definition>, because each @nt{array_type_definition}
3024 would define a new type.
3025 @end{Ramification}
3026
3027 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00385-01]}
3028 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0062-1],ARef=[AI05-0262-1]}
3029 If the @Chg{Version=[2],New=[deferred constant declaration includes
3030 a],Old=[subtype defined by the]} @nt<subtype_indication>
3031 @Chg{Version=[3],New=[@i<S> ],Old=[]}@Chg{Version=[2],
3032 New=[that defines a],Old=[in the deferred declaration is]}
3033 constrained@Chg{Version=[2],New=[ subtype],Old=[]}, then the
3034 @Chg{Version=[3],New=[constraint],Old=[subtype]} defined
3035 by the @nt<subtype_indication> in the full declaration shall match
3036 @Chg{Version=[3],New=[the constraint defined by @i<S>],Old=[it]}
3037 statically.@Redundant[ On the other hand,
3038 if the subtype of the deferred constant is unconstrained,
3039 then the full declaration is still allowed to impose a constraint.
3040 The constant itself will be constrained, like all constants;]
3041
3042 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01]}
3043 If the deferred constant declaration
3044 includes the reserved word @key(aliased), then the
3045 full declaration shall also@Chg{Version=[2],New=[;],Old=[.]}
3046 @begin{Ramification}
3047 On the other hand, the full constant can be aliased
3048 even if the deferred constant is not.
3049 @end{Ramification}
3050
3051 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00231-01]}
3052 @ChgAdded{Version=[2],Text=[If the subtype of the deferred constant
3053 declaration excludes null, the subtype of the full declaration shall also
3054 exclude null.]}
3055 @begin{Ramification}
3056 @ChgRef{Version=[2],Kind=[Added]}
3057 @ChgAdded{Version=[2],Text=[On the other hand, the full constant can
3058 exclude null even if the deferred constant does not. But that can only
3059 happen for a @nt{subtype_indication}, as anonymous access types are
3060 required to statically match (which includes any @nt{null_exclusion}).]}
3061 @end{Ramification}
3062 @end(itemize)
3063
3064 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1]}
3065 @Redundant[A deferred constant declaration
3066 @Chg{Version=[3],New=[for which the],Old=[that is completed by
3067 a @nt{pragma}]} Import @Chg{Version=[3],New=[aspect is True ],Old=[]}need
3068 not appear in the visible part of a @nt{package_specification},
3069 and has no full constant declaration.]
3070
3071
3072 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
3073 The completion of a deferred constant declaration shall occur
3074 before the constant is frozen
3075 (see @Chg{Version=[2],New=[@RefSecNum{Freezing Rules}],
3076 Old=[@RefSecNum{Deferred Constants}]}).
3077
3078 @end{Legality}
3079
3080 @begin{RunTime}
3081 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0004-1]}
3082 @PDefn2{Term=[elaboration], Sec=(deferred constant declaration)}
3083 The elaboration of a deferred constant declaration
3084 elaborates the @nt<subtype_indication>@Chg{Version=[3],New=[,
3085 @nt<access_definition>,],Old=[]} or (only allowed in the case of an
3086 imported constant) the @nt<array_type_definition>.
3087 @begin{Ramification}
3088 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0004-1]}
3089 @ChgAdded{Version=[3],Text=[For nonimported constants, these elaborations
3090 cannot require any code or checks for a legal program, because the given
3091 @nt<subtype_indication> has to be indefinite or statically match that of
3092 the full constant, meaning that either it is a @nt<subtype_mark> or it has
3093 static constraints. If the deferred constant instead has an
3094 @nt<access_definition>, the designated subtype must be a @nt<subtype_mark>.
3095 We still say that these are elaborated, however, because part of elaboration
3096 is creating the type, which is clearly needed for @nt<access_definition>s.
3097 (A deferred constant and its full constant have different types when
3098 they are specified by an @nt<access_definition>, although there is no
3099 visible effect of these types being different as neither can be named.)]}
3100 @end{Ramification}
3101 @end{RunTime}
3102
3103 @begin{Notes}
3104 The full constant declaration for a deferred constant that is of a given
3105 private type or private extension is not allowed before the corresponding
3106 @nt{full_type_declaration}. This is a consequence of the freezing
3107 rules for types
3108 (see @RefSecNum{Freezing Rules}).
3109 @begin{Ramification}
3110 Multiple or single declarations are allowed for the
3111 deferred and the full declarations, provided that the
3112 equivalent single declarations would be allowed.
3113
3114 Deferred constant declarations are useful for declaring constants of
3115 private views, and types with components of private views.
3116 They are also useful for declaring
3117 access-to-constant objects that designate
3118 variables declared in the private part of a package.
3119 @end{Ramification}
3120 @end{Notes}
3121
3122 @begin{Examples}
3123 @leading@keepnext@i{Examples of deferred constant declarations:}
3124 @begin{Example}
3125 Null_Key : @key[constant] Key; --@RI[ see @RefSecNum{Private Operations}]
3126
3127 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1]}
3128 CPU_Identifier : @key[constant] String(1..8)@Chg{Version=[3],New=[],Old=[;]}
3129 @Chg{Version=[3],New=[ @key[with]],Old=[@key[pragma]]} Import@Chg{Version=[3],New=[ => True, Convention => ],Old=[(]}Assembler, @Chg{Version=[3],New=[],Old=[CPU_Identifier, ]}Link_Name => "CPU_ID"@Chg{Version=[3],New=[],Old=[)]};
3130 --@RI[ see @RefSecNum{Interfacing Aspects}]
3131 @end{Example}
3132 @end{Examples}
3133
3134 @begin{Extend83}
3135 @Defn{extensions to Ada 83}
3136 In Ada 83, a deferred constant is required to be of a private type
3137 declared in the same visible part.
3138 This restriction is removed for Ada 95;
3139 deferred constants can be of any type.
3140
3141 In Ada 83, a deferred constant declaration was not permitted to
3142 include a constraint, nor the reserved word @key(aliased).
3143
3144 In Ada 83, the rules required conformance of type marks; here
3145 we require static matching of subtypes if the deferred constant
3146 is constrained.
3147
3148 A deferred constant declaration can be completed with a @nt{pragma}
3149 Import. Such a deferred constant declaration need not be within a
3150 @nt{package_specification}.
3151
3152 The rules for too-early uses of deferred constants are modified in
3153 Ada 95 to allow more cases, and catch all errors at compile time.
3154 This change is necessary in order to allow deferred constants of a
3155 tagged type without violating the principle that for a dispatching call,
3156 there is always an implementation to dispatch to.
3157 It has the beneficial side effect of catching some Ada-83-erroneous
3158 programs at compile time.
3159 The new rule fits in well with the new freezing-point rules.
3160 Furthermore, we are trying to convert undefined-value problems into
3161 bounded errors, and we were having trouble for the case of deferred
3162 constants.
3163 Furthermore, uninitialized deferred constants cause trouble for the
3164 shared variable / tasking rules, since they are really variable, even
3165 though they purport to be constant.
3166 In Ada 95, they cannot be touched until they become constant.
3167
3168 Note that we do not consider this change to be an upward
3169 incompatibility, because it merely changes an erroneous execution in Ada
3170 83 into a compile-time error.
3171
3172 The Ada 83 semantics are unclear in the case where
3173 the full view turns out to be an access type.
3174 It is a goal of the language design to prevent uninitialized access
3175 objects.
3176 One wonders if the implementation is required to initialize the
3177 deferred constant to null, and then initialize it (again!) to its
3178 real value.
3179 In Ada 95, the problem goes away.
3180 @end{Extend83}
3181
3182 @begin{DiffWord83}
3183 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
3184 Since deferred constants can now be of a nonprivate type,
3185 we have made this a stand-alone @Chg{Version=[3],New=[subclause],Old=[clause]},
3186 rather than a subclause of @RefSec{Private Types and Private Extensions}.
3187
3188 Deferred constant declarations used to have their own syntax, but now
3189 they are simply a special case of @nt<object_declaration>s.
3190 @end{DiffWord83}
3191
3192 @begin{Extend95}
3193 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00385-01]}
3194 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
3195 Deferred constants were enhanced to allow
3196 the use of anonymous access types in them.]}
3197 @end{Extend95}
3198
3199 @begin{DiffWord95}
3200 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01]}
3201 @ChgAdded{Version=[2],Text=[Added matching rules for subtypes that
3202 exclude null.]}
3203 @end{DiffWord95}
3204
3205 @begin{DiffWord2005}
3206 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0062-1]}
3207 @ChgAdded{Version=[3],Text=[@b<Correction:> Corrected rules so
3208 that the intent that a full constant may have a null exclusion even
3209 if the deferred constant does not is actually met.]}
3210 @end{DiffWord2005}
3211
3212
3213 @LabeledClause{Limited Types}
3214
3215 @begin{Intro}
3216 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
3217 @redundant[A limited type is (a view of) a type for which
3218 @Chg{Version=[2],New=[copying (such as for an @nt{assignment_statement})],
3219 Old=[the assignment operation]} is not allowed.
3220 A nonlimited type is a (view of a) type for which
3221 @Chg{Version=[2],New=[copying], Old=[the assignment operation]} is allowed.]
3222 @begin{Discussion}
3223 The concept of the @i(value) of a limited type is difficult
3224 to define, since the abstract value of a limited type often
3225 extends beyond its physical representation. In some
3226 sense, values of a limited type cannot be divorced from
3227 their object. The value @i(is) the object.
3228
3229 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
3230 In Ada 83, in the two places where limited types were defined
3231 by the language, namely tasks and files, an implicit
3232 level of indirection was implied by the semantics to
3233 avoid the separation of the value from an associated
3234 object.
3235 In Ada 95, most limited types are passed by reference,
3236 and even return-ed by reference.@Chg{Version=[2],New=[ In Ada 2005,
3237 most limited types are built-in-place upon return, rather than returned
3238 by reference. Thus the object @lquotes@;identity@rquotes is part of the
3239 logical value of most limited types.],Old=[]}
3240 @end{Discussion}
3241 @begin{Honest}
3242 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01],ARef=[AI95-00419-01]}
3243 For a limited partial view whose full view is nonlimited,
3244 @Chg{Version=[2],New=[copying],Old=[assignment]} is possible on parameter
3245 passing and function return. To prevent any copying whatsoever, one should
3246 make both the partial @i{and} full views limited.
3247 @end{Honest}
3248 @Comment{The below was moved from a ChgToGlossaryAlso.}
3249 @ChgToGlossary{Version=[2],Kind=[Revised],Term=<Limited type>,
3250 Text=<A limited type is @Chg{Version=[2],New=[],Old=[(a view of) ]}a type
3251 for which
3252 @Chg{Version=[2],New=[copying (such as in an @nt{assignment_statement})],
3253 Old=[the assignment operation]} is not allowed.
3254 A nonlimited type is a @Chg{Version=[2],New=[],Old=[(view of a) ]}type for
3255 which
3256 @Chg{Version=[2],New=[copying], Old=[the assignment operation]} is allowed.>}
3257 @end{Intro}
3258
3259 @begin{Legality}
3260
3261 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
3262 If a tagged record type has any limited components,
3263 then the reserved word @key[limited] shall
3264 appear in its @nt<record_type_definition>.@Chg{Version=[2],New=[ @Redundant[If
3265 the reserved word @key[limited] appears in the definition of a
3266 @nt{derived_type_definition}, its parent type and any progenitor interfaces
3267 shall be limited.]],Old=[]}
3268 @begin{TheProof}
3269 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01]}
3270 @ChgAdded{Version=[2],Text=[The rule about the parent type being required
3271 to be limited can be found in @RefSecNum{Derived Types and Classes}. Rules
3272 about progenitor interfaces can be found in
3273 @RefSecNum{Interface Types}, specifically, a nonlimited interface can appear
3274 only on a nonlimited type. We repeat these rules here to gather these
3275 scattered rules in one obvious place.]}
3276 @end{TheProof}
3277 @begin{Reason}
3278 @leading@;This prevents tagged limited types from becoming nonlimited.
3279 Otherwise, the following could happen:
3280 @begin{Example}
3281 @key[package] P @key[is]
3282 @key[type] T @key[is] @key[limited] @key[private];
3283 @key[type] R @key[is] @key[tagged]
3284 @key[record] --@RI{ Illegal!}
3285 --@RI{ This should say @lquotes@;@key[limited record]@rquotes@;.}
3286 X : T;
3287 @key[end] @key[record];
3288 @key[private]
3289 @key[type] T @key[is] @key[new] Integer; --@RI{ R becomes nonlimited here.}
3290 @key[end] P;
3291
3292 @ChgRef{Version=[2],Kind=[Revised]}
3293 @key[package] Q @key[is]
3294 @key[type] R2@Chg{Version=[2],New=[],Old=[(Access_Discrim : @key[access] ...)]} @key[is] @key[new] R @key[with]
3295 @key[record]
3296 Y : Some_Task_Type;
3297 @key[end] @key[record];
3298 @key[end] Q;
3299 @end{Example}
3300
3301 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
3302 If the above were legal,
3303 then assignment would be defined for R'Class in the body of P,
3304 which is bad news, given @Chg{Version=[2],New=[],Old=[the access discriminant
3305 and ]}the task.
3306 @end{Reason}
3307
3308 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
3309 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0147-1]}
3310 @ChgRef{Version=[5],Kind=[RevisedAdded],ARef=[AI12-0172-1],ARef=[AI12-0236-1],ARef=[AI12-0317-1]}
3311 @ChgAdded{Version=[2],Type=[Leading],Text=[In the following contexts,
3312 an @nt{expression} of a limited
3313 type is @Chg{Version=[5],New=[],Old=[not ]}permitted
3314 @Chg{Version=[5],New=[only if each of its operative constituents is newly constructed
3315 (see @RefSecNum{Expressions})],Old=[unless it is an @nt{aggregate}, a @nt{function_call},
3316 @Chg{Version=[3],New=[],Old=[or ]}a parenthesized @nt{expression} or
3317 @nt{qualified_expression} whose operand
3318 is permitted by this rule@Chg{Version=[3],New=[,
3319 or a @nt{conditional_expression}
3320 all of whose @Syni{dependent_}@nt{expression}s are permitted by this
3321 rule],Old=[]}]}:]}
3322 @begin{Itemize}
3323
3324 @ChgRef{Version=[2],Kind=[Added]}
3325 @Chg{Version=[2],New=[the initialization @nt{expression} of an
3326 @nt{object_declaration} (see @RefSecNum{Object Declarations})],Old=[]}
3327
3328 @ChgRef{Version=[2],Kind=[Added]}
3329 @Chg{Version=[2],New=[the @nt{default_expression} of a
3330 @nt{component_declaration} (see @RefSecNum{Record Types})],Old=[]}
3331
3332 @ChgRef{Version=[2],Kind=[Added]}
3333 @Chg{Version=[2],New=[the @nt{expression} of a @nt{record_component_association} (see @RefSecNum{Record Aggregates})],Old=[]}
3334
3335 @ChgRef{Version=[2],Kind=[Added]}
3336 @Chg{Version=[2],New=[the @nt{expression} for an @nt{ancestor_part} of an
3337 @nt{extension_aggregate} (see @RefSecNum{Extension Aggregates})],Old=[]}
3338
3339 @ChgRef{Version=[2],Kind=[Added]}
3340 @Chg{Version=[2],New=[an @nt{expression} of a @nt{positional_array_aggregate}
3341 or the @nt{expression} of an @nt{array_component_association} (see
3342 @RefSecNum{Array Aggregates})],Old=[]}
3343
3344 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0127-1]}
3345 @ChgAdded{Version=[5],Text=[the @Syni{base_}@nt{expression} of a @nt{record_delta_aggregate}
3346 (see @RefSecNum{Delta Aggregates})]}
3347
3348 @ChgRef{Version=[2],Kind=[Added]}
3349 @ChgRef{Version=[5],Kind=[RevisedAdded]}@ChgNote{Just a paragraph number change}
3350 @ChgAdded{Version=[2],Text=[the @nt{qualified_expression} of an initialized allocator
3351 (see @RefSecNum{Allocators})]}
3352
3353 @ChgRef{Version=[2],Kind=[Added]}
3354 @ChgRef{Version=[5],Kind=[RevisedAdded]}@ChgNote{Just a paragraph number change}
3355 @ChgAdded{Version=[2],Text=[the @nt{expression} of a return statement (see
3356 @RefSecNum{Return Statements})]}
3357
3358 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0177-1]}
3359 @ChgRef{Version=[4],Kind=[RevisedAdded],ARef=[AI12-0157-1]}
3360 @ChgRef{Version=[5],Kind=[RevisedAdded]}@ChgNote{Just a paragraph number change}
3361 @ChgAdded{Version=[3],Text=[the
3362 @Chg{Version=[4],New=[return expression],Old=[@nt{expression}]} of an
3363 @Chg{Version=[4],New=[expression function],Old=[@nt{expression_function_declaration}]}
3364 (see @RefSecNum{Expression Functions})]}
3365
3366 @ChgRef{Version=[2],Kind=[Added]}
3367 @ChgRef{Version=[3],Kind=[RevisedAdded]}@ChgNote{Only because the paragraph number has changed}
3368 @ChgRef{Version=[5],Kind=[RevisedAdded]}@ChgNote{Just a paragraph number change}
3369 @ChgAdded{Version=[2],Text=[the @nt{default_expression} or actual parameter for a
3370 formal object of mode @b{in} (see @RefSecNum{Formal Objects})]}
3371
3372 @begin{Discussion}
3373 @ChgRef{Version=[2],Kind=[AddedNormal]}
3374 @ChgAdded{Version=[2],Text=[All of these contexts normally require copying; by
3375 restricting the uses as above, we can require the new object to be
3376 built-in-place.]}
3377 @end{Discussion}
3378 @end{Itemize}
3379
3380
3381 @end{Legality}
3382
3383 @begin{StaticSem}
3384 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
3385 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0178-1]}
3386 @leading@keepnext@Defn{limited type}
3387 A @Chg{Version=[3],New=[view of a ],Old=[]}type is @i{limited} if it
3388 is @Chg{Version=[2],New=[],Old=[a descendant of ]}one of the following:
3389 @begin(itemize)
3390 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00411-01],ARef=[AI95-00419-01]}
3391 a type with the reserved word @key(limited)@Chg{Version=[2],New=[,
3392 @key(synchronized), @key(task), or @key(protected) ],Old=[]}
3393 in its definition;
3394 @begin{Ramification}
3395 Note that there is always a @lquotes@;definition,@rquotes@; conceptually,
3396 even if there is no syntactic category called @lquotes@;..._definition@rquotes@;.
3397
3398 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
3399 @ChgAdded{Version=[2],Text=[This includes interfaces of the above kinds,
3400 derived types with the reserved word @key{limited}, as well as task and
3401 protected types.]}
3402 @end{Ramification}
3403
3404 @ChgNote{This should really be deleted in Version 2, but we want to
3405 reuse the paragraph number, and that is hard, so we add the message manually.}
3406 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
3407 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0087-1]}
3408 @Chg{Version=[3],New=[a class-wide type whose specific type is limited;],
3409 Old=[@Chg{Version=[2],New=[@Shrink{@i<This paragraph was deleted.>}],
3410 Old=[a task or protected type;]}]}
3411
3412 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
3413 a composite type with a limited component@Chg{Version=[2],New=[;],Old=[.]}
3414
3415 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0178-1]}
3416 @ChgAdded{Version=[3],Text=[an incomplete view;]}
3417
3418 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01]}
3419 @ChgAdded{Version=[2],Text=[a derived type whose parent is limited and is not an
3420 interface.]}
3421
3422 @begin{Ramification}
3423 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
3424 @ChgAdded{Version=[2],Text=[Limitedness is not inherited from interfaces;
3425 it must be explicitly specified when the parent is an interface.]}
3426 @end{Ramification}
3427
3428 @end(itemize)
3429
3430 @begin{Honest}
3431 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
3432 @ChgAdded{Version=[2],Text=[A derived type can become nonlimited if
3433 @key{limited} does not appear and the derivation
3434 takes place in the visible part of a child package,
3435 and the parent type is nonlimited as viewed from the
3436 private part or body of the child package.]}
3437 @end{Honest}
3438 @begin{Reason}
3439 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
3440 @ChgAdded{Version=[2],Text=[We considered a rule where limitedness was always
3441 inherited from the parent for derived types, but in the case of a type whose
3442 parent is an interface, this meant that the first interface is treated
3443 differently than other interfaces. It also would have forced users to declare
3444 dummy nonlimited interfaces just to get the limitedness right. We also
3445 considered a syntax like @key{not limited} to specify nonlimitedness when the
3446 parent was limited, but that was unsavory. The rule given is more uniform and
3447 simpler to understand.]}
3448
3449 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
3450 @ChgAdded{Version=[2],Text=[The rules for interfaces are asymmetrical, but
3451 the language is not: if the parent interface is limited, the presence of the
3452 word @key{limited} determines the limitedness, and nonlimited progenitors are
3453 illegal by the rules in @RefSecNum{Interface Types} if @key{limited} is
3454 present. If the parent interface
3455 is nonlimited, the word @key{limited} is illegal by the rules in
3456 @RefSecNum{Derived Types and Classes}. The net effect is that the order of
3457 the interfaces doesn't matter.]}
3458 @end{Reason}
3459
3460
3461 @Defn{nonlimited type}
3462 Otherwise, the type is nonlimited.
3463
3464 @Redundant[There are no predefined equality operators for
3465 a limited type.]
3466
3467 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0052-1]}
3468 @ChgAdded{Version=[3],Type=[Leading],Keepnext=[T],Text=[A type is
3469 @i{immutably limited} if it is one of the
3470 following:@Defn{immutably limited}@Defn2{Term=[limited type],Sec=[immutably]}]}
3471
3472 @begin{Itemize}
3473 @ChgRef{Version=[3],Kind=[Added]}
3474 @ChgAdded{Version=[3],Text=[An explicitly limited record type;]}
3475
3476 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0217-1]}
3477 @ChgAdded{Version=[3],Text=[A record extension with the reserved word
3478 @key[limited];]}
3479
3480 @ChgRef{Version=[3],Kind=[Added]}
3481 @ChgAdded{Version=[3],Text=[A nonformal limited private type that is
3482 tagged or has at least one access discriminant with a @nt{default_expression};]}
3483
3484 @begin{Reason}
3485 @ChgRef{Version=[3],Kind=[AddedNormal]}
3486 @ChgAdded{Version=[3],Text=[The full type in both of these cases must
3487 necessarily be immutably limited. We need to include private types
3488 as much as possible so that we aren't unintentionally discouraging the
3489 use of private types.]}
3490 @end{Reason}
3491
3492 @ChgRef{Version=[3],Kind=[Added]}
3493 @ChgAdded{Version=[3],Text=[A task type, a protected type, or a
3494 synchronized interface;]}
3495
3496 @ChgRef{Version=[3],Kind=[Added]}
3497 @ChgAdded{Version=[3],Text=[A type derived from an immutably limited type.]}
3498
3499 @end{Itemize}
3500
3501 @begin{Discussion}
3502 @ChgRef{Version=[3],Kind=[AddedNormal]}
3503 @ChgAdded{Version=[3],Text=[An immutably limited type is a type that cannot
3504 become nonlimited subsequently in a private part or in a child unit.
3505 If a view of the type makes it immutably limited, then no copying (assignment)
3506 operations are ever available for objects of the type. This allows other
3507 properties; for instance, it is safe for such objects to have
3508 access discriminants that have defaults or designate other limited objects.]}
3509 @end{Discussion}
3510
3511 @begin{Ramification}
3512 @ChgRef{Version=[3],Kind=[AddedNormal]}
3513 @ChgAdded{Version=[3],Text=[A nonsynchronized limited interface type is
3514 not immutably limited; a type derived from it can be nonlimited.]}
3515 @end{Ramification}
3516
3517 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0052-1]}
3518 @ChgAdded{Version=[3],Text=[A descendant of a generic formal
3519 limited private type is
3520 presumed to be immutably limited except within the body
3521 of a generic unit or a body declared within the declarative
3522 region of a generic unit, if the formal type is declared
3523 within the formal part of the generic unit.]}
3524
3525 @begin{Ramification}
3526 @ChgRef{Version=[3],Kind=[AddedNormal]}
3527 @ChgAdded{Version=[3],Text=[In an instance, a type is descended from
3528 the actual type corresponding to the formal, and all rules are rechecked
3529 in the specification. Bodies are excepted so that we assume the worst there;
3530 the complex wording is required to handle children of generics and
3531 unrelated bodies properly.]}
3532 @end{Ramification}
3533
3534 @end{StaticSem}
3535
3536 @begin{ImplReq}
3537 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
3538 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg],ARef=[AI05-0067-1]}
3539 @ChgDeleted{Version=[3],Text=[@Chg{Version=[2],New=[
3540 For an @nt{aggregate} of a limited type used to initialize an object as allowed
3541 above, the implementation shall not create a separate anonymous object for the
3542 @nt{aggregate}. For a @nt{function_call} of a type with a part that is of a
3543 task, protected, or explicitly limited record type that is used to initialize
3544 an object as allowed above, the implementation shall not create a separate
3545 return object (see 6.5) for the @nt{function_call}. The @nt{aggregate} or
3546 @nt{function_call} shall be constructed directly in the new object.],Old=[]}]}
3547 @begin{Discussion}
3548 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
3549 @ChgRef{Version=[3],Kind=[DeletedNoDelMsg],ARef=[AI05-0067-1]}
3550 @ChgDeleted{Version=[3],Text=[@Chg{Version=[2],New=[For a
3551 @nt{function_call}, we only require @i{build-in-place}@PDefn{build-in-place}
3552 for a limited type that would have
3553 been a return-by-reference type in Ada 95. We do this because
3554 we want to minimize disruption to Ada 95 implementations and users.],Old=[]}]}
3555 @end{Discussion}
3556 @end{ImplReq}
3557
3558 @begin{Notes}
3559 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
3560 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
3561 @Chg{Version=[2],New=[While it is allowed to write initializations of limited
3562 objects, such initializations never copy a limited object. The source of such an
3563 assignment operation must be an @nt<aggregate> or @nt<function_call>, and such
3564 @nt<aggregate>s and @nt<function_call>s must be built directly in the target
3565 object@Chg{Version=[3],New=[ (see @RefSecNum{Assignment and Finalization})],Old=[]}.],
3566 Old=[@leading@keepnext@;The following are consequences of the rules for limited types:]}
3567 @begin{Honest}
3568 @ChgRef{Version=[2],Kind=[AddedNormal]}
3569 @ChgAdded{Version=[2],Text=[This isn't quite true if the type can become
3570 nonlimited (see below); @nt{function_call}s only are required to be
3571 build-in-place for @lquotes@;really@rquotes@; limited types.]}
3572 @end{Honest}
3573
3574 @begin{NotIso}
3575 @ChgAdded{Version=[2],Noprefix=[T],Noparanum=[T],Text=[@Shrink{@i<Paragraphs 10
3576 through 15 were deleted.>}]}@Comment{This message should be deleted if the
3577 paragraphs are ever renumbered.}
3578 @end{NotIso}
3579
3580 @begin{Itemize}
3581 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
3582 @ChgDeleted{Version=[2],Text=[An initialization expression is not allowed in an
3583 @nt{object_declaration} if the type of the object is limited.]}
3584
3585 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
3586 @ChgDeleted{Version=[2],Text=[A default expression is not allowed in a
3587 @nt{component_declaration} if the type of the record component is limited.]}
3588
3589 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
3590 @ChgDeleted{Version=[2],Text=[An initialized allocator is not allowed if the
3591 designated type is limited.]}
3592
3593 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
3594 @ChgDeleted{Version=[2],Text=[A generic formal parameter of mode @key[in] must
3595 not be of a limited type.]}
3596 @end{Itemize}
3597
3598 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
3599 @ChgDeleted{Version=[2],Text=[@nt{Aggregate}s are not available for a limited
3600 composite type. Concatenation is not available for a limited array type.]}
3601
3602 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
3603 @ChgDeleted{Version=[2],Text=[The rules do not exclude a @nt{default_expression}
3604 for a formal parameter of a limited type; they do not exclude a deferred
3605 constant of a limited type if the full declaration of the constant is of a
3606 nonlimited type.]}
3607
3608 @Defn{become nonlimited}
3609 @Defn2{Term=[nonlimited type],Sec=(becoming nonlimited)}
3610 @Defn2{Term=[limited type],Sec=(becoming nonlimited)}
3611 As illustrated in @RefSecNum{Private Operations},
3612 an untagged limited type can become nonlimited under certain
3613 circumstances.
3614 @begin{Ramification}
3615 Limited private types do not become nonlimited;
3616 instead, their full view can be nonlimited,
3617 which has a similar effect.
3618
3619 It is important to remember
3620 that a single nonprivate type can be both limited and nonlimited
3621 in different parts of its scope. In other words, @lquotes@;limited@rquotes@; is a property
3622 that depends on where you are in the scope of the type.
3623 We don't call this a @lquotes@;view property@rquotes@; because there is no particular
3624 declaration to declare the nonlimited view.
3625
3626 Tagged types never become nonlimited.
3627 @end{Ramification}
3628 @end{Notes}
3629
3630 @begin{Examples}
3631 @leading@keepnext@i{Example of a package with a limited type:}
3632 @begin{Example}
3633 @key[package] IO_Package @key[is]
3634 @key[type] File_Name @key[is] @key[limited] @key[private];
3635
3636 @key[procedure] Open (F : @key[in] @key[out] File_Name);
3637 @key[procedure] Close(F : @key[in] @key[out] File_Name);
3638 @key[procedure] Read (F : @key[in] File_Name; Item : @key[out] Integer);
3639 @key[procedure] Write(F : @key[in] File_Name; Item : @key[in] Integer);
3640 @key[private]
3641 @key[type] File_Name @key[is]
3642 @key[limited] @key[record]
3643 Internal_Name : Integer := 0;
3644 @key[end] @key[record];
3645 @key[end] IO_Package;
3646
3647
3648 @key[package] @key[body] IO_Package @key[is]
3649 Limit : @key[constant] := 200;
3650 @key[type] File_Descriptor @key[is] @key[record] ... @key[end] @key[record];
3651 Directory : @key[array] (1 .. Limit) @key[of] File_Descriptor;
3652 ...
3653 @key[procedure] Open (F : @key[in] @key[out] File_Name) @key[is] ... @key[end];
3654 @key[procedure] Close(F : @key[in] @key[out] File_Name) @key[is] ... @key[end];
3655 @key[procedure] Read (F : @key[in] File_Name; Item : @key[out] Integer) @key[is] ... @key[end];
3656 @key[procedure] Write(F : @key[in] File_Name; Item : @key[in] Integer) @key[is] ... @key[end];
3657 @key[begin]
3658 ...
3659 @key[end] IO_Package;
3660 @end{Example}
3661 @end{Examples}
3662
3663 @begin{Notes}
3664 @i{Notes on the example:}
3665 In the example above, an outside subprogram making use of IO_Package
3666 may obtain a file name by calling Open and later use it in calls to
3667 Read and Write. Thus, outside the package, a file name obtained from
3668 Open acts as a kind of password; its internal properties (such as
3669 containing a numeric value) are not known and no other operations
3670 (such as addition or comparison of internal names) can be performed
3671 on a file name.
3672 Most importantly, clients of the package cannot make copies
3673 of objects of type File_Name.
3674
3675 @NoPrefix@;This example is characteristic of any case where complete control
3676 over the operations of a type is desired. Such packages serve a dual
3677 purpose. They prevent a user from making use of the internal
3678 structure of the type. They also implement the notion of an
3679 encapsulated data type where the only operations on the type are
3680 those given in the package specification.
3681
3682 @NoPrefix@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
3683 The fact that the full view of File_Name is explicitly declared
3684 @key[limited] means that parameter passing @Chg{Version=[2],New=[],
3685 Old=[and function return ]}will always be by reference@Chg{Version=[2],New=[
3686 and function results will always be built directly in the result object],Old=[]}
3687 (see @RefSecNum{Formal Parameter Modes} and @RefSecNum{Return Statements}).
3688
3689 @end{Notes}
3690
3691 @begin{Extend83}
3692 @Defn{extensions to Ada 83}
3693 The restrictions in RM83-7.4.4(4),
3694 which disallowed @key[out] parameters of limited types in certain
3695 cases, are removed.
3696 @end{Extend83}
3697
3698 @begin{DiffWord83}
3699 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
3700 Since limitedness and privateness are orthogonal in Ada 95 (and
3701 to some extent in Ada 83), this is now its own @Chg{Version=[3],New=[subclause],Old=[clause]}
3702 rather than being a subclause of
3703 @RefSec{Private Types and Private Extensions}.
3704 @end{DiffWord83}
3705
3706 @begin{Extend95}
3707 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
3708 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
3709 Limited types now have an assignment operation, but its use is restricted
3710 such that all uses are build-in-place. This is
3711 accomplished by restricting uses to @nt{aggregate}s and @nt{function_call}s.
3712 @nt{Aggregate}s were not allowed to have a limited type in Ada 95, which
3713 causes a compatibility issue discussed in @RefSec{Aggregates}.
3714 Compatibility issues with return statements for limited
3715 @nt{function_call}s are discussed
3716 in @RefSec{Return Statements}.]}
3717 @end{Extend95}
3718
3719 @begin{DiffWord95}
3720 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00411-01],ARef=[AI95-00419-01]}
3721 @ChgAdded{Version=[2],Text=[Rewrote the definition of limited to ensure that
3722 interfaces are covered, but that limitedness is not inherited from interfaces.
3723 Derived types that explicitly include @key{limited} are now also covered.]}
3724 @end{DiffWord95}
3725
3726 @begin{DiffWord2005}
3727 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0052-1],ARef=[AI05-0217-1]}
3728 @ChgAdded{Version=[3],Text=[@b<Correction:> Added a definition for
3729 immutably limited types, so that the fairly complex definition does
3730 not need to be repeated in rules elsewhere in the Standard.]}
3731
3732 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1],ARef=[AI05-0299-1]}
3733 @ChgAdded{Version=[3],Text=[@b<Correction:> The built-in-place rules
3734 are consolidated in @RefSecNum{Assignment and Finalization}, and thus
3735 they are removed from this subclause.]}
3736
3737 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0087-1]}
3738 @ChgAdded{Version=[3],Text=[@b<Correction:> Fixed an oversight: class-wide
3739 types were never defined to be limited, even if their associated specific
3740 type is. It is thought that this oversight was never implemented incorrectly
3741 by any compiler, thus we have not classified it as an incompatibility.]}
3742
3743 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0147-1]}
3744 @ChgAdded{Version=[3],Text=[Allowed @nt{conditional_expression}s in limited
3745 constructor contexts @em we want to treat these as closely to parentheses as
3746 possible.]}
3747
3748 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0177-1]}
3749 @ChgAdded{Version=[3],Text=[Added wording so that expression functions can
3750 return limited entities.]}
3751
3752 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0178-1]}
3753 @ChgAdded{Version=[3],Text=[@b<Correction:> Added incomplete views
3754 to the list of reasons for a view of a type to be limited. This is not
3755 a change as the definition already was in
3756 @RefSecNum{Incomplete Type Declarations}. But it is much better to have
3757 all of the reasons for limitedness together.]}
3758 @end{DiffWord2005}
3759
3760 @begin{Extend2012}
3761 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0172-1]}
3762 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}
3763 @b{Correction:} A @nt{raise_expression} can be used in an expression
3764 used in a limited context. This is harmless (no object will be created
3765 if an exception is raised instead), useful, and thus appears to have been
3766 an omission when @nt{raise_expression}s were added to the language.]}
3767 @end{Extend2012}
3768
3769 @begin{DiffWord2012}
3770 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0127-1]}
3771 @ChgAdded{Version=[5],Text=[Added the @Syni{base_}@nt{expression} of a
3772 @nt{delta_aggregate} as a limited context.]}
3773 @end{DiffWord2012}
3774
3775
3776 @RMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
3777 @LabeledRevisedClause{Version=[3],New=[Assignment and Finalization],Old=[User-Defined Assignment and Finalization]}
3778
3779 @begin{Intro}
3780 @redundant[@Defn{user-defined assignment}
3781 @Defn2{Term=[assignment], Sec=(user-defined)}
3782 Three kinds of actions are
3783 fundamental to the manipulation of objects:
3784 initialization, finalization, and assignment.
3785 Every object is initialized, either explicitly
3786 or by default, after being created
3787 (for example, by an @nt{object_declaration} or @nt{allocator}).
3788 Every object is finalized before being destroyed
3789 (for example, by leaving a @nt{subprogram_body} containing an
3790 @nt{object_declaration}, or by a call to an instance of
3791 Unchecked_Deallocation).
3792 An assignment operation is used as part of @nt{assignment_statement}s,
3793 explicit initialization, parameter passing, and other operations.
3794 @IndexSee{Term=[constructor],See=[initialization]}
3795 @IndexSee{Term=[constructor],See=[Initialize]}
3796 @IndexSee{Term=[destructor],See=[finalization]}
3797
3798 Default definitions for these three fundamental
3799 operations are provided by the
3800 language, but
3801 @Defn{controlled type}
3802 a @i{controlled} type gives the user additional
3803 control over parts of these operations.
3804 @Defn{Initialize}@Defn{Finalize}@Defn{Adjust}
3805 In particular, the user can define, for a controlled type,
3806 an Initialize procedure which is invoked immediately after
3807 the normal default initialization of a controlled object, a Finalize procedure
3808 which is invoked immediately before finalization of any of the
3809 components of a controlled object, and an Adjust
3810 procedure which is invoked as the last step of an assignment to
3811 a (nonlimited) controlled object.]
3812 @ToGlossary{Term=<Controlled type>,
3813 Text=<A controlled type supports user-defined assignment and
3814 finalization.
3815 Objects are always finalized before being destroyed.>}
3816 @begin{Ramification}
3817 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01],ARef=[AI95-00287-01]}
3818 Here's the basic idea of initialization, value adjustment, and finalization,
3819 whether or not user defined:
3820 When an object is created,
3821 if it is explicitly assigned an initial value,
3822 @Chg{Version=[2],New=[the object is either built-in-place from an @nt{aggregate}
3823 or function call (in which case neither Adjust nor Initialize is applied), or ],Old=[]}
3824 the assignment copies and
3825 adjusts the initial value. Otherwise, Initialize is applied to it
3826 (except in the case of an @nt{aggregate} as a whole).
3827 An @nt{assignment_statement} finalizes the target before
3828 copying in and adjusting the new value.
3829 Whenever an object goes away, it is finalized.
3830 Calls on Initialize and Adjust happen bottom-up; that is,
3831 components first, followed by the containing object.
3832 Calls on Finalize @Chg{Version=[2],New=[happen],Old=[happens]} top-down; that is,
3833 first the containing object, and then its components.
3834 These ordering rules ensure that any components will be in a
3835 well-defined state when Initialize, Adjust,
3836 or Finalize is applied to the containing object.
3837 @end{Ramification}
3838 @end{Intro}
3839
3840 @begin{StaticSem}
3841 @leading@keepnext@;The following language-defined library package exists:
3842 @begin{Example}@ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0020],ARef=[AI95-00126-01]}
3843 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0212-1]}
3844 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0241-1]}
3845 @key[package] Ada.Finalization@Chg{Version=[5],New=[],Old=[ @key[is]]}@ChildUnit{Parent=[Ada],Child=[Finalization]}
3846 @Chg{Version=[5],New=[@key[with] Pure, Nonblocking => False @key[is]],Old=[@key[pragma] @Chg{Version=[3],New=[Pure],Old=[Preelaborate]}(Finalization);@Chg{Version=[3],New=[],Old=[@Chg{New=[
3847 @key[pragma] Remote_Types(Finalization);],Old=[]}]}]}
3848
3849 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00161-01]}
3850 @key[type] @AdaTypeDefn{Controlled} @key[is abstract tagged private];@Chg{Version=[2],New=[
3851 @key{pragma} Preelaborable_Initialization(Controlled);],Old=[]}
3852
3853 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
3854 @key(procedure) @AdaSubDefn{Initialize} (Object : @key(in out) Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3855 @key(procedure) @AdaSubDefn{Adjust} (Object : @key(in out) Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3856 @key(procedure) @AdaSubDefn{Finalize} (Object : @key(in out) Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3857
3858 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00161-01]}
3859 @key[type] @AdaTypeDefn{Limited_Controlled} @key[is abstract tagged limited private];@Chg{Version=[2],New=[
3860 @key{pragma} Preelaborable_Initialization(Limited_Controlled);],Old=[]}
3861
3862 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
3863 @key(procedure) @AdaSubDefn{Initialize} (Object : @key(in out) Limited_Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3864 @key(procedure) @AdaSubDefn{Finalize} (Object : @key(in out) Limited_Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3865 @key(private)
3866 ... -- @RI{not specified by the language}
3867 @key[end] Ada.Finalization;
3868 @end{Example}
3869
3870 @begin{Reason}
3871 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0241-1]}
3872 @ChgAdded{Version=[5],Text=[This package must allow blocking
3873 (Nonblocking => False) for compatibility. The purpose of this package
3874 is to provide a template for overriding user-defined routines; and
3875 such routines can only allow blocking if the root type does so.
3876 Users can still declare their overridding routines nonblocking if they
3877 wish.]}
3878 @end{Reason}
3879
3880 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
3881 @Defn{controlled type}
3882 A controlled type is a descendant of Controlled or Limited_Controlled.
3883 @Chg{Version=[2],New=[],Old=[The (default) implementations of
3884 Initialize, Adjust, and Finalize have no effect. ]}The predefined
3885 "=" operator of type Controlled always returns True,
3886 @Redundant[since this operator is incorporated into
3887 the implementation of the predefined equality operator of types derived
3888 from Controlled, as explained in
3889 @RefSecNum(Relational Operators and Membership Tests).]
3890 The type Limited_Controlled is like Controlled, except
3891 that it is limited and it lacks the primitive subprogram Adjust.
3892 @begin{Discussion}
3893 We say @lquotes@;nonlimited controlled type@rquotes@ (rather than just
3894 @lquotes@;controlled type@rquotes@;;) when we want to talk about descendants
3895 of Controlled only.
3896 @end{Discussion}
3897 @begin{Reason}
3898 We considered making Adjust and Finalize abstract.
3899 However, a reasonable coding convention is e.g. for Finalize to
3900 always call the parent's Finalize after doing whatever work is needed
3901 for the extension part.
3902 (Unlike CLOS, we have no way to do that automatically in Ada 95.)
3903 For this to work, Finalize cannot be abstract.
3904 In a generic unit, for a generic formal abstract derived type whose
3905 ancestor is Controlled or Limited_Controlled, calling the ancestor's
3906 Finalize would be illegal if it were abstract, even though the actual
3907 type might have a concrete version.
3908
3909 Types Controlled and Limited_Controlled are abstract, even though
3910 they have no abstract primitive subprograms.
3911 It is not clear that they need to be abstract, but there seems to be
3912 no harm in it, and it might make an implementation's life easier to
3913 know that there are no objects of these types @em in case the
3914 implementation wishes to make them @lquotes@;magic@rquotes@; in some way.
3915
3916 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01]}
3917 @ChgAdded{Version=[2],Text=[For Ada 2005, we considered making these types
3918 interfaces. That would have the advantage of allowing them to be added
3919 to existing trees. But that was rejected both because it would cause
3920 massive disruptions to existing implementations, and because it would be
3921 very incompatible due to the "no hidden interfaces" rule. The latter rule
3922 would prevent a tagged private type from being completed with a derivation
3923 from Controlled or Limited_Controlled @em a very common idiom.]}
3924 @end{Reason}
3925
3926 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00360-01]}
3927 @ChgAdded{Version=[2],Type=[Leading],Text=[A type is said to
3928 @i{need finalization} if:@Defn{needs finalization}@Defn2{Term=[type],Sec=[needs finalization]}]}
3929
3930 @begin{Itemize}
3931 @ChgRef{Version=[2],Kind=[Added]}
3932 @ChgAdded{Version=[2],Text=[it is a controlled type, a task type or
3933 a protected type; or]}
3934
3935 @ChgRef{Version=[2],Kind=[Added]}
3936 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0092-1]}
3937 @ChgAdded{Version=[2],Text=[it has a component
3938 @Chg{Version=[3],New=[whose type ],Old=[that ]} needs finalization; or]}
3939
3940 @ChgRef{Version=[2],Kind=[Added]}
3941 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0013-1]}
3942 @Chg{Version=[3],New=[it is a class-wide type; or],
3943 Old=[@Chg{Version=[2],New=[it is a limited type that
3944 has an access discriminant whose designated type needs finalization; or],Old=[]}]}
3945
3946 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0026-1]}
3947 @ChgAdded{Version=[3],Text=[it is a partial view whose full view needs finalization; or]}
3948
3949 @ChgRef{Version=[2],Kind=[Added]}
3950 @ChgAdded{Version=[2],Text=[it is one of a number of language-defined types
3951 that are explicitly defined to need finalization.]}
3952
3953 @begin{Ramification}
3954 @ChgRef{Version=[2],Kind=[AddedNormal]}
3955 @ChgAdded{Version=[2],Text=[The fact that a type needs finalization
3956 does not require it to be implemented with a controlled type. It just has to
3957 be recognized by the No_Nested_Finalization restriction.]}
3958
3959 @ChgRef{Version=[2],Kind=[AddedNormal]}
3960 @ChgAdded{Version=[2],Text=[This property is defined for the type, not
3961 for a particular view. That's necessary as restrictions look in private parts
3962 to enforce their restrictions; the point is to eliminate all controlled
3963 parts, not just ones that are visible.]}
3964 @end{Ramification}
3965
3966 @end{Itemize}
3967 @end{StaticSem}
3968
3969 @begin{RunTime}
3970 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
3971 @Chg{Version=[2],New=[],Old=[@PDefn2{Term=[elaboration],
3972 Sec=(object_declaration)}]}During
3973 the elaboration @Chg{Version=[2],New=[or evaluation of a construct that
3974 causes an object to be initialized by default],Old=[of
3975 an @nt{object_declaration}]},
3976 for every controlled subcomponent of
3977 the object that is not assigned an initial value
3978 (as defined in @RefSecNum{Object Declarations}),
3979 Initialize is called on that subcomponent.
3980 Similarly, if the object @Chg{Version=[2],New=[that is initialized by
3981 default ],Old=[]}as a whole is controlled@Chg{Version=[2],New=[],Old=[ and
3982 is not assigned an initial value]}, Initialize is called on the object.@Chg{Version=[2],
3983 New=[],Old=[ The same applies to the evaluation of an @nt{allocator},
3984 as explained in @RefSecNum{Allocators}.]}
3985
3986 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0021],ARef=[AI95-00182-01]}
3987 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
3988 For an @nt{extension_aggregate} whose @nt{ancestor_part} is a
3989 @nt{subtype_mark}@Chg{Version=[2],New=[ denoting a],
3990 Old=[, @Chg{New=[for each controlled subcomponent of the ancestor part, either
3991 Initialize is called, or its initial value is assigned, as appropriate],
3992 Old=[Initialize is called on all controlled subcomponents of the
3993 ancestor part]}; if the type of the ancestor part is itself]}
3994 controlled@Chg{Version=[2],New=[ subtype],Old=[]},
3995 the Initialize procedure of the ancestor type is called,
3996 unless that Initialize procedure is abstract.
3997 @begin{Discussion}
3998 @leading@keepnext@;Example:
3999 @begin{Example}
4000 @key[type] T1 @key[is] @key[new] Controlled @key[with]
4001 @key[record]
4002 ... --@RI{ some components might have defaults}
4003 @key[end] @key[record];
4004
4005 @key[type] T2 @key[is] @key[new] Controlled @key[with]
4006 @key[record]
4007 X : T1; --@RI{ no default}
4008 Y : T1 := ...; --@RI{ default}
4009 @key[end] @key[record];
4010
4011 A : T2;
4012 B : T2 := ...;
4013 @end{Example}
4014
4015 As part of the elaboration of A's declaration, A.Y is assigned a
4016 value; therefore Initialize is not applied to A.Y.
4017 Instead, Adjust is applied to A.Y as part of the assignment operation.
4018 Initialize is applied to A.X and to A, since those objects are not
4019 assigned an initial value.
4020 The assignment to A.Y is not considered an assignment to A.
4021
4022 For the elaboration of B's declaration, Initialize is not called at
4023 all. Instead the assignment adjusts B's value;
4024 that is, it applies Adjust to B.X, B.Y, and B.
4025
4026 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0021],ARef=[AI95-00182-01]}
4027 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00373-01]}
4028 @ChgAdded{Version=[1],Text=[The @nt{ancestor_part} of an
4029 @nt{extension_aggregate}@Chg{Version=[2],New=[, <> in aggregates, and the
4030 return object of an @nt{extended_return_statement} are],Old=[ is]} handled
4031 similarly.]}
4032 @end{Discussion}
4033
4034 Initialize and other initialization
4035 operations are done in an arbitrary order,
4036 except as follows.@PDefn2{Term=[arbitrary order],Sec=[allowed]}
4037 Initialize is applied to an object after initialization
4038 of its subcomponents, if any
4039 @Redundant[(including both implicit initialization and Initialize calls)].
4040 If an object has a component with an access discriminant
4041 constrained by a per-object expression,
4042 Initialize is applied to this component after any components that do not
4043 have such discriminants.
4044 For an object with several components with such a discriminant,
4045 Initialize is applied to them in order
4046 of their @nt{component_declaration}s.
4047 For an @nt<allocator>,
4048 any task activations follow all calls
4049 on Initialize.
4050 @begin{Reason}
4051 The fact that Initialize is done for subcomponents
4052 first allows Initialize for a composite
4053 object to refer to its subcomponents knowing
4054 they have been properly initialized.
4055
4056 The fact that Initialize is done for components with access
4057 discriminants after other components
4058 allows the Initialize operation for a component
4059 with a self-referential access discriminant to assume
4060 that other components of the enclosing object have already been
4061 properly initialized.
4062 For multiple such components, it allows some predictability.
4063 @end{Reason}
4064
4065 @leading@keepnext@Defn{assignment operation}
4066 When a target object with any controlled parts is assigned a value,
4067 @Redundant[either when created or in a subsequent
4068 @nt{assignment_statement},]
4069 the @i{assignment operation} proceeds as follows:
4070 @begin(itemize)
4071 The value of the target becomes the assigned value.
4072
4073 @Defn{adjusting the value of an object}
4074 @Defn{adjustment}
4075 The value of the target is @i{adjusted.}
4076 @end(itemize)
4077 @begin{Ramification}
4078 If any parts of the object are controlled,
4079 abort is deferred during the assignment operation.
4080 @end{Ramification}
4081
4082 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}@Comment{May need future changes.}
4083 @Defn{adjusting the value of an object}
4084 @Defn{adjustment}
4085 To adjust the value of a
4086 @Chg{Version=[3],New=[],Old=[@Redundant[(nonlimited)] ]}composite object,
4087 the values of the components of the object are first
4088 adjusted in an arbitrary order,
4089 and then, if the object is
4090 @Chg{Version=[3],New=[nonlimited ],Old=[]}controlled,
4091 Adjust is called.@PDefn2{Term=[arbitrary order],Sec=[allowed]}
4092 Adjusting the value of an elementary object has no effect@Redundant[,
4093 nor does adjusting the value of a composite object with no
4094 controlled parts.]
4095 @begin{Ramification}
4096 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
4097 Adjustment is never @Chg{Version=[3],New=[actually ],Old=[]}performed for
4098 values of @Chg{Version=[3],New=[an immutably],Old=[a by-reference]} limited type,
4099 since @Chg{Version=[3],New=[all
4100 assignment operations for such types are required to be built-in-place.
4101 Even so, we still define adjustment for all types in order that the
4102 canonical semantics is well-defined],Old=[these
4103 types do not support copying]}.
4104 @end{Ramification}
4105 @begin{Reason}
4106 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
4107 The verbiage in the Initialize rule about access discriminants
4108 constrained by per-object expressions is not necessary here,
4109 since such types are
4110 @Chg{Version=[3],New=[either ],Old=[]}limited@Chg{Version=[3],New=[ or
4111 do not have defaults, so the discriminant can only be changed by
4112 an assignment to an outer object. Such an assignment could
4113 happen only before any adjustments or (if part of an outer Adjust)
4114 only after any inner (component) adjustments have completed.],
4115 Old=[, and therefore are never adjusted.]}
4116 @end{Reason}
4117
4118 @PDefn2{Term=[execution], Sec=(assignment_statement)}
4119 For an @nt{assignment_statement},
4120 @Redundant[ after the @nt{name} and
4121 @nt{expression} have been evaluated,
4122 and any conversion (including constraint checking) has been done,]
4123 an anonymous object is created, and
4124 the value is assigned into it;
4125 @Redundant[that is, the assignment operation is applied].
4126 @Redundant[(Assignment includes value adjustment.)]
4127 The target of the @nt{assignment_statement} is then finalized.
4128 The value of the anonymous object is then assigned into the target of
4129 the @nt{assignment_statement}.
4130 Finally, the anonymous object is finalized.
4131 @Redundant[As explained below,
4132 the implementation may eliminate the intermediate anonymous object,
4133 so this description subsumes the one given in
4134 @RefSec{Assignment Statements}.]
4135 @begin{Reason}
4136 @leading@;An alternative design for user-defined assignment might involve an
4137 Assign operation instead of Adjust:
4138 @begin{Example}
4139 @key[procedure] Assign(Target : @key[in] @key[out] Controlled; Source : @key[in] @key[out] Controlled);
4140 @end{Example}
4141
4142 @leading@keepnext@;Or perhaps even a syntax like this:
4143 @begin{Example}
4144 @key[procedure] ":="(Target : @key[in] @key[out] Controlled; Source : @key[in] @key[out] Controlled);
4145 @end{Example}
4146
4147 @leading@;Assign (or ":=") would have the responsibility of doing the copy,
4148 as well as whatever else is necessary.
4149 This would have the advantage that the Assign operation knows about both
4150 the target and the source at the same time @em it would be possible to
4151 do things like reuse storage belonging to the target, for example,
4152 which Adjust cannot do.
4153 However, this sort of design would not work in the case of unconstrained
4154 discriminated variables, because there is no way to change the
4155 discriminants individually.
4156 For example:
4157 @begin{Example}
4158 @key[type] Mutable(D : Integer := 0) @key[is]
4159 @key[record]
4160 X : Array_Of_Controlled_Things(1..D);
4161 @key[case] D @key[is]
4162 @key[when] 17 => Y : Controlled_Thing;
4163 @key[when] @key[others] => @key[null];
4164 @key[end] D;
4165 @key[end] @key[record];
4166 @end{Example}
4167
4168 An assignment to an unconstrained variable of type Mutable can cause
4169 some of the components of X, and the component Y, to appear and/or
4170 disappear.
4171 There is no way to write the Assign operation to handle this sort of
4172 case.
4173
4174 Forbidding such cases is not an option @em it would cause generic
4175 contract model violations.
4176 @end{Reason}
4177
4178
4179 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0067-1]}
4180 @ChgAdded{Version=[3],Type=[Leading],Text=[When a function call
4181 or @nt{aggregate} is used to initialize an object, the
4182 result of the function call or @nt{aggregate} is an anonymous object, which
4183 is assigned into the newly-created object. For such an assignment,
4184 the anonymous object might be @i<built in place>,@Defn{built in place}@Seeother{Primary=[build-in-place],Other=[built in place]}
4185 in which case the assignment does not involve any copying.
4186 Under certain circumstances, the anonymous object is required to be built in
4187 place. In particular:]}
4188
4189 @begin{Discussion}
4190 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
4191 @ChgAdded{Version=[3],Text=[We say assignment to built-in-place objects does not
4192 involve copying, which matches the intended implementation (see below). Of
4193 course, the implementation can do any copying it likes, if it can make such
4194 copying semantically invisible (by patching up access values to point to the
4195 copy, and so forth).]}
4196 @end{Discussion}
4197
4198 @begin{Itemize}
4199 @ChgRef{Version=[3],Kind=[Added]}
4200 @ChgAdded{Version=[3],Text=[If the full type of any part of the object is
4201 immutably limited, the anonymous object is built in place.]}
4202
4203 @begin{Reason}
4204 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
4205 @ChgAdded{Version=[3],Text=[We talk about the full types being immutably
4206 limited, as this is independent of the view of a type (in the same way that it
4207 is for determining the technique of parameter passing). That is, privacy is
4208 ignored for this purpose.]}
4209
4210 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0005-1],ARef=[AI05-0067-1]}
4211 @ChgAdded{Version=[3],Text=[For function calls, we only require building in
4212 place for immutably limited types. These are the types that would have been
4213 return-by-reference types in Ada 95. We limited the requirement because
4214 we want to minimize disruption to Ada 95 implementations and users.]}
4215 @end{Reason}
4216
4217 @begin{Honest}
4218 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0232-1]}
4219 @ChgAdded{Version=[3],Text=[This is a dynamic property and is determined by
4220 the specific type of the parts of the actual object. In particular, if a part
4221 has a class-wide type, the tag of the object might need to be examined in
4222 order to determine if build-in-place is required. However, we expect that most
4223 Ada implementations will determine this property at compile-time using some
4224 assume-the-worst algorithm in order to chose the appropriate method to
4225 implement a given call or aggregate. In addition, there is no attribute or
4226 other method for a program to determine if a particular object has this
4227 property (or not), so there is no value to a more careful description of this
4228 rule.]}
4229 @end{Honest}
4230
4231 @ChgRef{Version=[3],Kind=[Added]}
4232 @ChgAdded{Version=[3],Text=[In the case of an @nt{aggregate}, if the full type
4233 of any part of the newly-created object is controlled, the anonymous object is
4234 built in place.]}
4235
4236 @begin{Reason}
4237 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
4238 @ChgAdded{Version=[3],Type=[Leading],Text=[This is necessary to prevent
4239 elaboration problems with deferred constants of controlled types. Consider:]}
4240 @begin{Example}
4241 @ChgRef{Version=[3],Kind=[Added]}
4242 @ChgAdded{Version=[3],Text=[@key[package] P @key[is]
4243 @key[type] Dyn_String @key[is private];
4244 Null_String : @key[constant] Dyn_String;
4245 ...
4246 @key[private]
4247 @key[type] Dyn_String @key[is new] Ada.Finalization.Controlled @key[with] ...
4248 @key[procedure] Finalize(X : @key[in out] Dyn_String);
4249 @key[procedure] Adjust(X : @key[in out] Dyn_String);
4250 @comment{Blank Line}
4251 Null_String : @key[constant] Dyn_String :=
4252 (Ada.Finalization.Controlled @key[with] ...);
4253 ...
4254 @key[end] P;]}
4255 @end{Example}
4256 @ChgRef{Version=[3],Kind=[Added]}
4257 @ChgAdded{Version=[3],Text=[When Null_String is elaborated, the bodies of Finalize and Adjust
4258 clearly have not been elaborated. Without this rule, this declaration would
4259 necessarily raise Program_Error (unless the permissions given below are
4260 used by the implementation).]}
4261 @end{Reason}
4262
4263 @begin{Ramification}
4264 @ChgRef{Version=[3],Kind=[AddedNormal]}
4265 @ChgAdded{Version=[3],Text=[An @nt{aggregate} with a controlled part
4266 used in the return expression
4267 of a @nt{simple_@!return_@!statement} has to be built in place in the anonymous
4268 return object, as this is similar to an object declaration. (This is a change
4269 from Ada 95, but it is not an inconsistency as it only serves to restrict
4270 implementation choices.) But this only covers the @nt{aggregate}; a separate
4271 anonymous return object can still be used unless it too is required to be
4272 built in place.]}
4273
4274 @ChgRef{Version=[3],Kind=[AddedNormal]}
4275 @ChgAdded{Version=[3],Text=[Similarly, an @nt{aggregate} that has
4276 a controlled part but is not itself controlled and that is used to
4277 initialize an object also has to be built in place. This is also a change
4278 from Ada 95, but it is not an inconsistency as it only serves to restrict
4279 implementation choices. This avoids problems if a type like Dyn_String
4280 (in the example above) is used as a component in a type used as a
4281 deferred constant in package P.]}
4282 @end{Ramification}
4283
4284 @ChgRef{Version=[3],Kind=[Added]}
4285 @ChgAdded{Version=[3],Text=[In other cases, it is unspecified whether the
4286 anonymous object is built in place.@PDefn{unspecified}]}
4287
4288 @begin{Reason}
4289 @ChgRef{Version=[3],Kind=[AddedNormal]}
4290 @ChgAdded{Version=[3],Text=[This is left unspecified so the implementation
4291 can use any appropriate criteria for determining when to build in place.
4292 That includes making the decision on a call-by-call basis. Reasonable
4293 programs will not care what decision is made here anyway.]}
4294 @end{Reason}
4295 @end{Itemize}
4296
4297 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0067-1]}
4298 @ChgAdded{Version=[3],Type=[Leading],Text=[Notwithstanding@Defn{notwithstanding}
4299 what this International Standard says elsewhere, if an object is
4300 built in place:]}
4301
4302 @begin{Itemize}
4303 @ChgRef{Version=[3],Kind=[Added]}
4304 @ChgAdded{Version=[3],Text=[Upon successful completion of the return
4305 statement or @nt{aggregate}, the anonymous object @i<mutates into> the
4306 newly-created object; that is, the anonymous object ceases to exist, and the
4307 newly-created object appears in its place.@Defn{mutates}]}
4308
4309 @ChgRef{Version=[3],Kind=[Added]}
4310 @ChgAdded{Version=[3],Text=[Finalization is not performed on the anonymous object.]}
4311
4312 @ChgRef{Version=[3],Kind=[Added]}
4313 @ChgAdded{Version=[3],Text=[Adjustment is not performed on the newly-created
4314 object.]}
4315
4316 @ChgRef{Version=[3],Kind=[Added]}
4317 @ChgAdded{Version=[3],Text=[All access values that designate parts of the
4318 anonymous object now designate the corresponding parts of the newly-created
4319 object.]}
4320
4321 @ChgRef{Version=[3],Kind=[Added]}
4322 @ChgAdded{Version=[3],Text=[All renamings of parts of the anonymous
4323 object now denote views of the corresponding parts of the newly-created
4324 object.]}
4325
4326 @ChgRef{Version=[3],Kind=[Added]}
4327 @ChgAdded{Version=[3],Text=[Coextensions of the anonymous object become
4328 coextensions of the newly-created object.]}
4329 @end{Itemize}
4330
4331 @begin{Honest}
4332 @ChgRef{Version=[3],Kind=[AddedNormal]}
4333 @ChgAdded{Version=[3],Text=[This @ldquote@;mutating@rdquote does not
4334 necessarily happen atomically with
4335 respect to abort and other tasks. For example, if a function call is used as
4336 the parent part of an @nt{extension_aggregate}, then the tag of the anonymous
4337 object (the function result) will be different from the tag of the
4338 newly-created object (the parent part of the @nt{extension_aggregate}). In
4339 implementation terms, this involves modifying the tag field. If the current
4340 task is aborted during this modification, the object might become abnormal.
4341 Likewise, if some other task accesses the tag field during this modification,
4342 it constitutes improper use of shared variables, and is erroneous.]}
4343 @end{Honest}
4344
4345 @begin{ImplNote}
4346 @ChgRef{Version=[3],Kind=[AddedNormal]}
4347 @ChgAdded{Version=[3],Text=[The intended implementation is that the
4348 anonymous object is allocated at the
4349 same address as the newly-created object. Thus, no run-time action is
4350 required to cause all the access values and renamings to point to the right
4351 place. They just point to the newly-created object, which is what the return
4352 object has magically @ldquote@;mutated into@rdquote.]}
4353
4354 @ChgRef{Version=[3],Kind=[AddedNormal]}
4355 @ChgAdded{Version=[3],Text=[There is no requirement that 'Address of the
4356 return object is equal to 'Address of the newly-created object, but that will
4357 be true in the intended implementation.]}
4358
4359 @ChgRef{Version=[3],Kind=[AddedNormal]}
4360 @ChgAdded{Version=[3],Text=[For a function call, if the size of the
4361 newly-created object is known at the call site, the object is allocated there,
4362 and the address is implicitly passed to the function; the return object is
4363 created at that address. Otherwise, a storage pool is implicitly passed to the
4364 function; the size is determined at the point of the return statement, and
4365 passed to the Allocate procedure. The address returned by the storage pool is
4366 returned from the function, and the newly-created object uses that same
4367 address. If the return statement is left without returning (via an exception
4368 or a goto, for example), then Deallocate is called. The storage pool might be
4369 a dummy pool that represents @ldquote@;allocate on the stack@rdquote.]}
4370
4371 @ChgRef{Version=[3],Kind=[AddedNormal]}
4372 @ChgAdded{Version=[3],Text=[The Tag of the newly-created object may be
4373 different from that of the result object. Likewise, the master and
4374 accessibility level may be different.]}
4375
4376 @ChgRef{Version=[3],Kind=[AddedNormal]}
4377 @ChgAdded{Version=[3],Text=[An alternative implementation model might allow
4378 objects to move around to different addresses. In this case, access values and
4379 renamings would need to be modified at run time. It seems that this model
4380 requires the full power of tracing garbage collection.]}
4381 @end{ImplNote}
4382 @end{RunTime}
4383
4384 @begin{ImplReq}
4385 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0022],ARef=[AI95-00083-01]}
4386 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00318-02]}
4387 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg],ARef=[AI05-0067-1]}
4388 @ChgDeleted{Version=[3],Text=[@Chg{Version=[1],New=[For an @nt{aggregate} of a
4389 controlled type whose value is assigned,
4390 other than by an @nt{assignment_statement}@Chg{Version=[2],New=[],Old=[ or
4391 a @nt{return_statement}]}, the
4392 implementation shall not create a separate anonymous object for the
4393 @nt{aggregate}. The @nt{aggregate} value shall be constructed directly in the target
4394 of the assignment operation and Adjust is not called on the target object.],Old=[]}]}
4395 @begin{Reason}
4396 @ChgRef{Version=[1],Kind=[Added]}
4397 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00318-02]}
4398 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg],ARef=[AI05-0067-1]}
4399 @ChgDeleted{Version=[3],Type=[Leading],Text=[@Chg{Version=[1],New=[@Chg{Version=[2],
4400 New=[@PDefn{build-in-place}],Old=[]}This@Chg{Version=[2],
4401 New=[ @i<build-in-place> requirement],Old=[]} is necessary to prevent
4402 elaboration problems with deferred constants of controlled types. Consider:],Old=[]}]}
4403 @begin{Example}
4404 @ChgRef{Version=[1],Kind=[Added]}
4405 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg]}
4406 @ChgDeleted{Version=[3],Text=[@Chg{New=[@key[package] P @key[is]
4407 @key[type] Dyn_String @key[is private];
4408 Null_String : @key[constant] Dyn_String;
4409 ...
4410 @key[private]
4411 @key[type] Dyn_String @key[is new] Ada.Finalization.Controlled @key[with] ...
4412 @key[procedure] Finalize(X : @key[in out] Dyn_String);
4413 @key[procedure] Adjust(X : @key[in out] Dyn_String);
4414 @comment{Blank Line}
4415 Null_String : @key[constant] Dyn_String :=
4416 (Ada.Finalization.Controlled @key[with] ...);
4417 ...
4418 @key[end] P;],Old=[]}]}
4419 @end{Example}
4420 @ChgRef{Version=[1],Kind=[Added]}
4421 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg]}
4422 @ChgDeleted{Version=[3],Text=[@Chg{New=[When Null_String is elaborated, the bodies of Finalize and Adjust
4423 clearly have not been elaborated. Without this rule, this declaration would
4424 necessarily raise Program_Error (unless the permissions given below are
4425 used by the implementation).],Old=[]}]}
4426 @end{Reason}
4427 @begin{Ramification}
4428 @ChgRef{Version=[2],Kind=[AddedNormal]}
4429 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg]}
4430 @ChgDeleted{Version=[3],Text=[@Chg{Version=[2],New=[An @nt{aggregate} of a controlled type
4431 used in the return expression
4432 of a @nt{simple_@!return_@!statement} has to be built-in-place in the anonymous
4433 return object, as this is similar to an object declaration. (This is a change
4434 from Ada 95, but it is not an inconsistency as it only serves to restrict
4435 implementation choices.) But this only covers the @nt{aggregate}; a separate
4436 anonymous return object can still be used unless it too is required to be
4437 built-in-place (see @RefSecNum{Limited Types}).],Old=[]}]}
4438 @end{Ramification}
4439 @end{ImplReq}
4440
4441 @begin{ImplPerm}
4442 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
4443 An implementation is allowed to relax the above rules
4444 @Chg{Version=[3],New=[for @nt{assignment_statement}s],
4445 Old=[@Redundant[(for nonlimited controlled types)]]}
4446 in the following ways:
4447 @begin{TheProof}
4448 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0067-1]}
4449 @ChgDeleted{Version=[3],Text=[The phrase @lquotes@;for nonlimited controlled
4450 types@rquotes@; follows from the fact
4451 that all of the following permissions apply to cases involving
4452 assignment.
4453 It is important because the programmer can count on a stricter semantics
4454 for limited controlled types.]}
4455 @end{TheProof}
4456 @begin{Ramification}
4457 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
4458 @ChgAdded{Version=[3],Text=[The relaxations apply only to nonlimited types,
4459 as @nt{assignment_statement}s
4460 are not allowed for limited types. This is important so that the programmer
4461 can count on a stricter semantics for limited controlled types.]}
4462 @end{Ramification}
4463 @begin{Itemize}
4464 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
4465 @Chg{Version=[3],New=[If],Old=[For an @nt{assignment_statement} that assigns to]}
4466 an object @Chg{Version=[3],New=[is assigned ],Old=[]}the value of that same
4467 object, the implementation need not do anything.
4468 @begin{Ramification}
4469 In other words, even if an object is controlled and a combination
4470 of Finalize and Adjust on the object might have a net
4471 side effect, they need not be performed.
4472 @end{Ramification}
4473
4474 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
4475 For @Chg{Version=[3],New=[assignment of],Old=[an @nt{assignment_statement} for]}
4476 a noncontrolled type,
4477 the implementation may finalize and assign each
4478 component of the variable separately (rather than finalizing the entire
4479 variable and assigning the entire new value)
4480 unless a discriminant of the variable is changed by the assignment.
4481 @begin{Reason}
4482 For example, in a slice assignment, an anonymous object is not necessary
4483 if the slice is copied component-by-component in the right direction,
4484 since array types are not controlled (although their components may be).
4485 Note that the direction, and even the fact that it's a slice assignment,
4486 can in general be determined only at run time.
4487 @end{Reason}
4488 @begin{Ramification}
4489 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0005-1]}
4490 @ChgAdded{Version=[3],Text=[This potentially breaks a single assignment
4491 operation into many, and thus abort deferral (see
4492 @RefSecNum{Abort of a Task - Abort of a Sequence of Statements}) needs to
4493 last only across an individual component assignment when the component
4494 has a controlled part. It is only important that the copy step is
4495 not separated (by an abort) from the adjust step, so aborts between
4496 component assignments is not harmful.]}
4497 @end{Ramification}
4498
4499 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00147-01]}
4500 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
4501 @Chg{Version=[3],New=[The],Old=[For an @nt{aggregate} or function call whose
4502 value is assigned into a target object,
4503 the implementation need not create a separate anonymous object if
4504 it can safely create the value of
4505 the @nt{aggregate} or function call
4506 directly in the target object.
4507 Similarly, for an @nt{assignment_@!statement}, the]}
4508 implementation need not create an anonymous object if
4509 the value being assigned is the result of evaluating a @nt{name}
4510 denoting an object (the source object) whose storage cannot overlap
4511 with the target. If the source object might overlap with the
4512 target object, then the implementation can avoid the need for
4513 an intermediary anonymous object by exercising one of the
4514 above permissions and perform the assignment one component
4515 at a time (for an overlapping array assignment), or not at all
4516 (for an assignment where the target and the source of the assignment are
4517 the same object).@Chg{Version=[2],New=[],Old=[ Even if an anonymous object is
4518 created, the implementation may move its value to the target object as part of
4519 the assignment without re-adjusting so long as the
4520 anonymous object has no aliased subcomponents.]}
4521 @begin{Ramification}
4522 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
4523 @Chg{Version=[3],New=[If the anonymous object is eliminated by this permission, ],
4524 Old=[In the @nt{aggregate} case, only one
4525 value adjustment is necessary, and]} there is no anonymous object to be
4526 finalized@Chg{Version=[3],New=[ and thus the Finalize call on it is
4527 eliminated],Old=[]}.
4528
4529 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00147-01]}
4530 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
4531 @Chg{Version=[2],New=[@Chg{Version=[3],New=[Note that if the anonymous
4532 object is eliminated but the new value is not built in place in the
4533 target object],Old=[Similarly, in the function call case, the
4534 anonymous object can be eliminated. Note, however]}, that Adjust
4535 must be called],Old=[In the @nt{assignment_statement} case as well,
4536 no finalization of the anonymous object is needed.
4537 On the other hand, if the target has aliased subcomponents,
4538 then an adjustment takes place]} directly on the target object
4539 as the last step of the assignment, since some of the
4540 subcomponents may be self-referential or otherwise
4541 position-dependent.@Chg{Version=[2],New=[ This Adjust can be eliminated only
4542 by using one of the following permissions.],Old=[]}
4543 @end{Ramification}
4544 @end{Itemize}
4545
4546 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00147-01]}
4547 @ChgAdded{Version=[2],Type=[Leading],Text=[Furthermore, an implementation is
4548 permitted to
4549 omit implicit Initialize, Adjust, and Finalize calls and associated assignment
4550 operations on an object of a nonlimited controlled type provided that:]}
4551
4552 @begin{Itemize}
4553 @ChgRef{Version=[2],Kind=[AddedNormal]}
4554 @ChgAdded{Version=[2],Text=[any omitted Initialize call is not a call on a
4555 user-defined Initialize procedure, and]}
4556 @begin{Honest}
4557 @ChgRef{Version=[2],Kind=[AddedNormal]}
4558 @ChgAdded{Version=[2],Text=[This does not apply to any calls to a user-defined
4559 Initialize routine that happen to occur in an Adjust or Finalize routine. It
4560 is intended that it is never necessary to look inside of an Adjust or Finalize
4561 routine to determine if the call can be omitted.]}
4562 @end{Honest}
4563 @begin{Reason}
4564 @ChgRef{Version=[2],Kind=[AddedNormal]}
4565 @ChgAdded{Version=[2],Text=[We don't want to eliminate objects for which the
4566 Initialize might have side effects (such as locking a resource).]}
4567 @end{Reason}
4568
4569 @ChgRef{Version=[2],Kind=[AddedNormal]}
4570 @ChgAdded{Version=[2],Text=[any usage of the value of the object after the implicit
4571 Initialize or Adjust call and before any subsequent Finalize call on the object
4572 does not change the external effect of the program, and]}
4573
4574 @ChgRef{Version=[2],Kind=[AddedNormal]}
4575 @ChgAdded{Version=[2],Text=[after the omission of such calls and operations, any
4576 execution of the program that executes an Initialize or Adjust call on an
4577 object or initializes an object by an @nt{aggregate} will also later execute a
4578 Finalize call on the object and will always do so prior to assigning a new
4579 value to the object, and]}
4580
4581 @ChgRef{Version=[2],Kind=[AddedNormal]}
4582 @ChgAdded{Version=[2],Text=[the assignment operations associated with omitted Adjust
4583 calls are also omitted.]}
4584 @end{Itemize}
4585
4586 @ChgRef{Version=[2],Kind=[AddedNormal]}
4587 @ChgAdded{Version=[2],Text=[This permission applies to Adjust and Finalize calls even
4588 if the implicit calls have additional external effects.]}
4589 @begin{Reason}
4590 @ChgRef{Version=[2],Kind=[AddedNormal]}
4591 @ChgAdded{Version=[2],Text=[The goal of the above permissions is to allow
4592 typical dead assignment and dead variable removal algorithms to work for
4593 nonlimited controlled types. We require that @lquotes@;pairs@rquotes@; of
4594 Initialize/Adjust/Finalize operations are removed. (These aren't always pairs,
4595 which is why we talk about @lquotes@;any execution of the program@rquotes@;.)]}
4596 @end{Reason}
4597
4598 @end{ImplPerm}
4599
4600 @begin{Extend83}
4601 @Defn{extensions to Ada 83}
4602 Controlled types and user-defined finalization are new to Ada 95.
4603 (Ada 83 had finalization semantics only for masters of tasks.)
4604 @end{Extend83}
4605
4606 @begin{Extend95}
4607 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00161-01]}
4608 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
4609 @b[Amendment Correction:] Types Controlled and Limited_Controlled now have
4610 Preelaborable_Initialization, so that objects of types derived from these
4611 types can be used in preelaborated packages.]}
4612 @end{Extend95}
4613
4614 @begin{DiffWord95}
4615 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0020],ARef=[AI95-00126-01]}
4616 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified that Ada.Finalization
4617 is a remote types package.]}
4618
4619 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0021],ARef=[AI95-00182-01]}
4620 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Added wording to clarify that
4621 the default initialization (whatever it is) of an ancestor part is used.]}
4622
4623 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0022],ARef=[AI95-00083-01]}
4624 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified that Adjust is never
4625 called on an @nt{aggregate} used for the initialization of an object or
4626 subaggregate, or passed as a parameter.]}
4627
4628 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00147-01]}
4629 @ChgAdded{Version=[2],Text=[Additional optimizations are allowed for
4630 nonlimited controlled types. These allow traditional dead variable
4631 elimination to be applied to such types.]}
4632
4633 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
4634 @ChgAdded{Version=[2],Text=[Corrected the build-in-place requirement
4635 for controlled @nt{aggregate}s to be consistent with the requirements
4636 for limited types.]}
4637
4638 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00348-01]}
4639 @ChgAdded{Version=[2],Text=[The operations of types Controlled and
4640 Limited_Controlled are now declared as null procedures (see
4641 @RefSecNum{Null Procedures}) to make the semantics clear (and to provide
4642 a good example of what null procedures can be used for).]}
4643
4644 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00360-01]}
4645 @ChgAdded{Version=[2],Text=[Types that need finalization are defined; this is
4646 used by the No_Nested_Finalization restriction
4647 (see @RefSec{Tasking Restrictions}).]}
4648
4649 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00373-01]}
4650 @ChgAdded{Version=[2],Text=[Generalized the description of objects that
4651 have Initialize called for them to say that it is done for all objects that
4652 are initialized by default. This is needed so that all of the new cases
4653 are covered.]}
4654 @end{DiffWord95}
4655
4656 @begin{Extend2005}
4657 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0212-1]}
4658 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
4659 Package Ada.Finalization now has Pure categorization, so it can be mentioned
4660 for any package. Note that this does not change the preelaborability of
4661 objects descended from Controlled and Limited_Controlled.]}
4662 @end{Extend2005}
4663
4664 @begin{DiffWord2005}
4665 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0013-1]}
4666 @ChgAdded{Version=[3],Text=[@b<Correction:> Eliminated coextensions
4667 from the @ldquote@;needs finalization@rdquote rules, as this cannot be
4668 determined in general in the compilation unit that declares the type.
4669 (The designated type of the coextension may have been imported as a
4670 limited view.) Uses of @ldquote@;needs finalization@rdquote need to
4671 ensure that coextensions are handled by other means (such as
4672 in No_Nested_Finalization @en see @RefSecNum{Tasking Restrictions})
4673 or that coextensions cannot happen.]}
4674
4675 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0013-1]}
4676 @ChgAdded{Version=[3],Text=[@b<Correction:> Corrected the
4677 @ldquote@;needs finalization@rdquote rules to include class-wide types,
4678 as a future extension can include a part that needs finalization.]}
4679
4680 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0026-1]}
4681 @ChgAdded{Version=[3],Text=[@b<Correction:> Corrected the
4682 @ldquote@;needs finalization@rdquote rules to clearly say that they
4683 ignore privacy.]}
4684
4685 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
4686 @ChgAdded{Version=[3],Text=[@b<Correction:> Changed @ldquote@;built in place@rdquote
4687 to @RuntimeTitle and centralized the rules here. This eliminates the
4688 fiction that built in place is just a combination of a permission and
4689 a requirement; it clearly has noticeable semantic effects. This wording
4690 change is not intended to change the semantics of any correct Ada program.]}
4691 @end{DiffWord2005}
4692
4693
4694 @LabeledSubClause{Completion and Finalization}
4695
4696 @begin{Intro}
4697 @redundant[This subclause defines @i{completion} and @i{leaving} of the execution
4698 of constructs and entities.
4699 A @i{master} is the execution of a construct that
4700 includes finalization of local objects after it is complete
4701 (and after waiting for any local tasks
4702 @em see @RefSecNum(Task Dependence - Termination of Tasks)),
4703 but before leaving.
4704 Other constructs and entities are left immediately upon completion.
4705 @IndexSee{Term=[cleanup],See=(finalization)}
4706 @IndexSee{Term=[destructor],See=(finalization)}]
4707 @end{Intro}
4708
4709 @begin{RunTime}
4710 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
4711 @Defn{completion and leaving (completed and left)}
4712 @Defn2{Term=[completion], Sec=(run-time concept)}
4713 The execution of a construct or entity
4714 is @i{complete} when the end of that execution has been reached,
4715 or when a transfer of control
4716 (see @RefSecNum{Simple and Compound Statements - Sequences of Statements})
4717 causes it to be abandoned.
4718 @Defn{normal completion}
4719 @Defn2{Term=[completion], Sec=(normal)}
4720 @Defn{abnormal completion}
4721 @Defn2{Term=[completion], Sec=(abnormal)}
4722 Completion due to reaching the end of execution,
4723 or due to the transfer of control of an @Chg{Version=[2],
4724 New=[@nt{exit_statement}, return statement, @nt{goto_statement}],
4725 Old=[@ntf{exit_}, @ntf{return_}, @ntf{goto_}]},
4726 or @nt{requeue_statement} or of the
4727 selection of a @nt{terminate_alternative}
4728 is @i{normal completion}.
4729 Completion is @i{abnormal} otherwise @Redundant[@em
4730 when control is transferred out of a construct
4731 due to abort or the raising of an exception].
4732 @begin{Discussion}
4733 Don't confuse the run-time concept of completion with
4734 the compile-time concept of completion
4735 defined in @RefSecNum{Completions of Declarations}.
4736 @end{Discussion}
4737
4738 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01],ARef=[AI95-00416-01]}
4739 @Defn{leaving}
4740 @Defn{left}
4741 After execution of a construct or entity is complete,
4742 it is @i{left},
4743 meaning that execution continues with the next action,
4744 as defined for the execution that is taking place.
4745 @Defn{master}
4746 Leaving an execution happens immediately after its completion,
4747 except in the case of a @i{master}:
4748 the execution of
4749 a @Chg{Version=[2],New=[body other than a @nt{package_body};
4750 the execution of a @nt{statement}; or the evaluation of an
4751 @nt{expression}, @nt{function_call}, or @nt{range} that
4752 is not part of an enclosing @nt{expression}, @nt{function_call}, @nt{range}, or
4753 @nt{simple_@!statement} other than a @nt{simple_@!return_@!statement}],
4754 Old=[@nt{task_body}, a @nt{block_@!statement},
4755 a @nt{subprogram_body}, an @nt{entry_body}, or an @nt{accept_@!statement}]}.
4756 A master is finalized after it is
4757 complete, and before it is left.
4758
4759 @begin{Reason}
4760 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01],ARef=[AI95-00416-01]}
4761 @Chg{Version=[2],New=[@nt{Expression}s and @nt{statement}s
4762 are masters so that objects created by subprogram calls (in @nt{aggregate}s,
4763 @nt{allocator}s for anonymous access-to-object types, and so on) are
4764 finalized and have their tasks awaited before the @nt{expression}s or
4765 @nt{statement}s are left. Note that @nt{expression}s like the @nt{condition}
4766 of an @nt{if_statement} are masters, because they are not enclosed by a
4767 @nt{simple_statement}. Similarly, a @nt{function_call} which is renamed
4768 is a master, as it is not in a @nt{simple_@!statement}.],
4769 Old=[Note that although an @nt{accept_statement} has no
4770 @nt{declarative_part}, it can call functions and evaluate @nt{aggregate}s,
4771 possibly causing anonymous controlled objects to be created,
4772 and we don't want those objects to escape outside the rendezvous.]}
4773
4774 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
4775 @ChgAdded{Version=[2],Text=[We have to include @nt{function_call}s in the
4776 contexts that do not cause masters to occur so that @nt{expression}s
4777 contained in a @nt{function_call} (that is not part of an @nt{expression} or
4778 @nt{simple_statement}) do not individually become masters. We certainly do
4779 not want the parameter @nt{expression}s of a @nt{function_call} to be
4780 separate masters, as they would then be finalized before the function is
4781 called.]}
4782 @end{Reason}
4783 @begin{Ramification}
4784 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
4785 @ChgAdded{Version=[2],Text=[The fact that a @nt{function_call} is a master
4786 does not change the accessibility of the return object denoted by the
4787 @nt{function_call}; that depends on the use of the @nt{function_call}.
4788 The @nt{function_call} is the master
4789 of any short-lived entities (such as @nt{aggregate}s used as parameters of
4790 types with task or controlled parts).]}
4791 @end{Ramification}
4792
4793 @ChgToGlossary{Version=[5],Kind=[Added],Term=<Master>,
4794 Text=<@ChgAdded{Version=[5],Text=[The execution of a construct that includes
4795 waiting for tasks and finalization of objects associated with the master, prior
4796 to leaving the construct.]}>}
4797
4798 @Defn2{Term=[finalization], Sec=(of a master)}
4799 For the @i{finalization} of a master,
4800 dependent tasks are first awaited,
4801 as explained in @RefSecNum{Task Dependence - Termination of Tasks}.
4802 Then each object whose accessibility level is
4803 the same as that of the master is finalized
4804 if the object was successfully initialized and still exists.
4805 @Redundant[These actions are performed whether the master is left
4806 by reaching the last statement or via a transfer of control.]
4807 When a transfer of control causes completion of an execution, each
4808 included master is finalized in order, from innermost outward.
4809 @begin{Ramification}
4810 As explained in @RefSecNum{Operations of Access Types},
4811 the set of objects with the same accessibility level
4812 as that of the master
4813 includes objects declared immediately within the master,
4814 objects declared in nested packages,
4815 objects created by @nt{allocator}s
4816 (if the ultimate ancestor access type is declared in one of those places)
4817 and subcomponents of all of these things.
4818 If an object was already finalized by Unchecked_Deallocation,
4819 then it is not finalized again when the master is left.
4820
4821 Note that any object whose accessibility level is deeper than that of
4822 the master would no longer exist;
4823 those objects would have been finalized by some inner master.
4824 Thus, after leaving a master, the only objects yet to be
4825 finalized are those whose accessibility level is less deep than that
4826 of the master.
4827
4828 @end{Ramification}
4829 @begin{Honest}
4830 Subcomponents of objects due to be finalized are not finalized
4831 by the finalization of the master;
4832 they are finalized by the finalization of the containing object.
4833 @end{Honest}
4834 @begin{Reason}
4835 We need to finalize subcomponents of objects even if the containing
4836 object is not going to get finalized because it was not fully
4837 initialized.
4838 But if the containing object is finalized, we don't want to require
4839 repeated finalization of the subcomponents,
4840 as might normally be implied by the recursion in finalization of a
4841 master and the recursion in finalization of an object.
4842 @end{Reason}
4843 @begin{Honest}
4844 Formally, completion and leaving refer to executions of constructs or
4845 entities.
4846 However, the standard sometimes (informally) refers
4847 to the constructs or entities whose executions are being completed.
4848 Thus, for example,
4849 @lquotes@;the subprogram call or task is complete@rquotes@;
4850 really means
4851 @lquotes@;@i{the execution of} the subprogram call or task is complete.@rquotes@;
4852 @end{Honest}
4853
4854 @leading@keepnext@RootDefn2{Term=[finalization], Sec=(of an object)}
4855 For the @i{finalization} of an object:
4856 @begin{Itemize}
4857 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0099-1]}
4858 If @Chg{Version=[3],New=[the full type of ],Old=[]}the object is
4859 @Chg{Version=[3],New=[],Old=[of ]}an elementary type, finalization
4860 has no effect;
4861 @begin{Reason}
4862 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0099-1]}
4863 @ChgAdded{Version=[3],Text=[We say @ldquote@;full type@rdquote in this
4864 and the following bullets as privacy is ignored for the purpose of
4865 determining the finalization actions of an object; that is as expected
4866 for @RunTimeTitle rules.]}
4867 @end{Reason}
4868
4869 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0099-1]}
4870 If @Chg{Version=[3],New=[the full type of ],Old=[]}the object is
4871 @Chg{Version=[3],New=[a tagged type, and the tag of the object
4872 identifies],Old=[of]} a controlled type,
4873 the Finalize procedure @Chg{Version=[3],New=[of that controlled
4874 type ],Old=[]}is called;
4875
4876 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0099-1]}
4877 If @Chg{Version=[3],New=[the full type of ],Old=[]}the object is
4878 @Chg{Version=[3],New=[],Old=[of ]}a protected type,
4879 @Chg{Version=[3],New=[or if the full type of the object is a tagged type
4880 and the tag of the object identifies a protected type, ],Old=[]}the
4881 actions defined in @RefSecNum{Protected Units and Protected Objects} are performed;
4882
4883 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00416-01]}
4884 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0099-1]}
4885 If @Chg{Version=[3],New=[the full type of ],Old=[]}the object is
4886 @Chg{Version=[3],New=[],Old=[of ]}a composite type, then after performing
4887 the above actions, if any, every component of the object
4888 is finalized in an arbitrary order, except as follows:@PDefn2{Term=[arbitrary order],Sec=[allowed]}
4889 if the object has
4890 a component with an access discriminant constrained by a per-object
4891 expression, this component is finalized before any components that
4892 do not have such discriminants; for an object with several components
4893 with such a discriminant, they are finalized in the
4894 reverse of the order of their
4895 @nt<component_declaration>s@Chg{Version=[2],New=[;],Old=[.]}
4896 @begin{Reason}
4897 This allows the finalization of a component with an access discriminant
4898 to refer to other components of the enclosing object prior to
4899 their being finalized.
4900 @end{Reason}
4901 @begin{Honest}
4902 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0099-1]}
4903 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0005-1]}
4904 @ChgAdded{Version=[3],Text=[The components discussed here are all of the
4905 components that the object actually has, not just those components that are
4906 statically identified by the type of the object. These can be different if
4907 the object has a @Chg{Version=[4],New=[class-wide],Old=[classwide]} type.]}
4908 @end{Honest}
4909
4910 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00416-01]}
4911 @ChgAdded{Version=[2],Text=[If the object has coextensions (see
4912 @RefSecNum{Operations of Access Types}), each coextension is
4913 finalized after the object whose access discriminant designates it.]}
4914
4915 @begin{Ramification}
4916 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0066-1]}
4917 @ChgAdded{Version=[3],Text=[In the case of an @nt{aggregate} or function call that is used
4918 (in its entirety) to directly initialize a part of an object, the
4919 coextensions of the result of evaluating the @nt{aggregate} or function
4920 call are transfered to become coextensions of the object being
4921 initialized and are not finalized until the object being initialized
4922 is ultimately finalized, even if an anonymous object is created
4923 as part of the operation.]}
4924 @end{Ramification}
4925
4926 @end{Itemize}
4927
4928 @PDefn2{Term=[execution], Sec=(instance of Unchecked_Deallocation)}
4929 Immediately before an instance of Unchecked_Deallocation
4930 reclaims the storage of an object,
4931 the object is finalized.
4932 @Redundant[If an instance of Unchecked_Deallocation is never applied to
4933 an object created by an @nt{allocator},
4934 the object will still exist when the corresponding master completes,
4935 and it will be finalized then.]
4936
4937 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00280-01]}
4938 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0051-1],ARef=[AI05-0190-1]}
4939 The @Chg{Version=[3],New=[],Old=[order in which the ]}finalization of a master
4940 performs finalization of objects @Chg{Version=[3],New=[],Old=[is as follows:
4941 Objects ]}created by declarations in the master
4942 @Chg{Version=[3],New=[],Old=[are finalized ]}in the reverse order of their
4943 creation.@Chg{Version=[3],New=[],Old=[ For objects that were created by
4944 @nt{allocator}s for an access type whose
4945 ultimate ancestor is declared in the master,
4946 this rule is applied as though each
4947 such object that still exists had been created
4948 in an arbitrary order
4949 at the first freezing point
4950 (see @RefSecNum{Freezing Rules})@PDefn2{Term=[arbitrary order],Sec=[allowed]}
4951 of the ultimate ancestor type@Chg{Version=[2],New=[; the finalization of
4952 these objects is called the @i<finalization of the
4953 collection>@Defn{finalization of the collection}@Defn2{Term=[collection],
4954 Sec=[finalization of]}],Old=[]}.@Chg{Version=[3],New=[ Objects created by
4955 allocators for an anonymous access type that are not coextensions of some other
4956 object, are finalized in an arbitrary order during the finalization of their
4957 associated master.@PDefn2{Term=[arbitrary order],Sec=[allowed]}],Old=[]}]}@Chg{Version=[2],
4958 New=[ After the finalization of a master is complete, the objects finalized
4959 as part of its finalization cease to @i<exist>, as do any types and subtypes
4960 defined and created within the master.@PDefn2{Term=[exist],Sec=[cease to]}
4961 @PDefn2{Term=[cease to exist],Sec=[object]}
4962 @Defn2{Term=[cease to exist],Sec=[type]}],Old=[]}
4963 @begin{Reason}
4964 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4965 @ChgDeleted{Version=[3],Text=[Note that we talk about the type of the
4966 @nt{allocator} here. There may be access values of a (general) access type
4967 pointing at objects created by @nt{allocator}s for some other type; these are
4968 not finalized at this point.]}
4969
4970 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4971 @ChgDeleted{Version=[3],Text=[The freezing point of the ultimate ancestor access type is chosen
4972 because before that point, pool elements cannot be created,
4973 and after that point, access values designating (parts of)
4974 the pool elements can be created.
4975 This is also the point after which the pool object cannot have been
4976 declared.
4977 We don't want to finalize the pool elements until after anything
4978 finalizing objects that contain access values designating them.
4979 Nor do we want to finalize pool elements after finalizing the pool
4980 object itself.]}
4981 @end{Reason}
4982 @begin{Ramification}
4983 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4984 @ChgDeleted{Version=[3],Text=[Finalization of allocated objects is done according
4985 to the (ultimate ancestor) @nt{allocator} type, not according to the storage pool
4986 in which they are allocated.
4987 Pool finalization might reclaim storage (see @RefSec{Storage Management}),
4988 but has nothing (directly) to do with finalization of the
4989 pool elements.]}
4990
4991 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4992 @ChgDeleted{Version=[3],Text=[Note that finalization is done only for objects
4993 that still exist; if an instance of Unchecked_Deallocation has already gotten
4994 rid of a given pool element, that pool element will not be finalized when
4995 the master is left.]}
4996
4997 Note that a deferred constant declaration does not create the
4998 constant; the full constant declaration creates it.
4999 Therefore, the order of finalization depends on where the full
5000 constant declaration occurs,
5001 not the deferred constant declaration.
5002
5003 An imported object is not created by its declaration.
5004 It is neither initialized nor finalized.
5005 @end{Ramification}
5006 @begin{ImplNote}
5007 An implementation has to ensure that the storage for an object is not
5008 reclaimed when references to the object are still possible
5009 (unless, of course, the user explicitly requests reclamation via an
5010 instance of Unchecked_Deallocation).
5011 This implies, in general, that objects cannot be deallocated one by one
5012 as they are finalized;
5013 a subsequent finalization might reference an object that has been
5014 finalized, and that object had better be in its (well-defined)
5015 finalized state.
5016 @end{ImplNote}
5017
5018 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0190-1]}
5019 @ChgAdded{Version=[3],Type=[Leading],Text=[Each nonderived access type @i<T> has
5020 an associated @i<collection>,@Defn2{Term=[collection],Sec=[of an access type]}
5021 which is the set of objects created by @nt{allocator}s of @i<T>,
5022 or of types derived from @i<T>. Unchecked_Deallocation removes an object from
5023 its collection. Finalization of a collection consists of finalization of each
5024 object in the collection, in an arbitrary order. The collection of an access
5025 type is an object implicitly declared at the following place:@PDefn2{Term=[arbitrary order],Sec=[allowed]}]}
5026
5027 @begin{Ramification}
5028 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}
5029 @ChgAdded{Version=[3],Text=[The place of the implicit declaration determines
5030 when allocated objects are finalized. For multiple collections declared at the
5031 same place, we do not define the order of their implicit declarations.]}
5032
5033 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}@ChgNote{Moved down}
5034 @ChgAdded{Version=[3],Text=[Finalization of allocated objects is done according
5035 to the (ultimate ancestor) @nt{allocator} type, not according to the storage
5036 pool in which they are allocated. Pool finalization might reclaim storage (see
5037 @RefSec{Storage Management}), but has nothing (directly) to do with
5038 finalization of the pool elements.]}
5039
5040 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}@ChgNote{Moved down}
5041 @ChgAdded{Version=[3],Text=[Note that finalization is done only for objects that
5042 still exist; if an instance of Unchecked_Deallocation has already gotten rid
5043 of a given pool element, that pool element will not be finalized when the
5044 master is left.]}
5045 @end{Ramification}
5046 @begin{Reason}
5047 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}@ChgNote{Moved down}
5048 @ChgAdded{Version=[3],Text=[Note that we talk about the type of the
5049 @nt{allocator} here. There may be access values of a (general) access type
5050 pointing at objects created by @nt{allocator}s for some other type; these are
5051 not (necessarily) finalized at this point.]}
5052 @end{Reason}
5053
5054 @begin{Itemize}
5055 @ChgRef{Version=[3],Kind=[Added]}
5056 @ChgAdded{Version=[3],Text=[For a named access type, the first freezing point (see
5057 @RefSecNum{Freezing Rules}) of the type.]}
5058
5059 @begin{Reason}
5060 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}@ChgNote{Moved down}
5061 @ChgAdded{Version=[3],Text=[The freezing point of the ultimate ancestor access
5062 type is chosen because before that point, pool elements cannot be created,
5063 and after that point, access values designating (parts of)
5064 the pool elements can be created.
5065 This is also the point after which the pool object cannot have been declared.
5066 We don't want to finalize the pool elements until after anything
5067 finalizing objects that contain access values designating them.
5068 Nor do we want to finalize pool elements after finalizing the pool
5069 object itself.]}
5070 @end{Reason}
5071
5072 @ChgRef{Version=[3],Kind=[Added]}
5073 @ChgAdded{Version=[3],Text=[For the type of an access parameter, the call that
5074 contains the @nt{allocator}.]}
5075
5076 @ChgRef{Version=[3],Kind=[Added]}
5077 @ChgAdded{Version=[3],Text=[For the type of an access result, within the master of
5078 the call (see @RefSecNum{Operations of Access Types}).]}
5079 @begin{Honest}
5080 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0005-1],ARef=[AI05-0190-1]}
5081 @ChgAdded{Version=[3],Text=[We mean at a place within the master consistent
5082 with the execution of the call within the master. We don't say that
5083 normatively, as it is difficult to explain that when the master of the call
5084 need not be the master that immediately includes the call (such as when an
5085 anonymous result is converted to a named access type).]}
5086 @end{Honest}
5087
5088 @ChgRef{Version=[3],Kind=[Added]}
5089 @ChgAdded{Version=[3],Text=[For any other anonymous access type, the first
5090 freezing point of the innermost enclosing declaration.]}
5091 @end{Itemize}
5092
5093
5094 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
5095 @PDefn2{Term=[execution], Sec=(assignment_statement)}
5096 The target of an @Chg{Version=[2],New=[@nt{assignment_statement}],
5097 Old=[assignment statement]} is finalized before copying in the
5098 new value, as explained
5099 in @RefSecNum{Assignment and Finalization}.
5100
5101 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0021],ARef=[AI95-00182-01]}
5102 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01]}
5103 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0066-1],ARef=[AI05-0142-4],ARef=[AI05-0269-1]}
5104 @Chg{Version=[2],New=[The master of an object is the master enclosing its
5105 creation whose accessibility level (see @RefSecNum{Operations of Access Types})
5106 is equal to that of the object@Chg{Version=[3],New=[, except
5107 in the case of an anonymous object representing the result of
5108 an @nt{aggregate} or function call. If such an anonymous object
5109 is part of the result of evaluating the actual parameter expression for
5110 an explicitly aliased
5111 parameter of a function call, the master of the object is the innermost
5112 master enclosing the evaluation of the @nt{aggregate} or function call, excluding
5113 the @nt{aggregate} or function call itself. Otherwise, the master of such
5114 an anonymous object is the innermost master enclosing the evaluation of the
5115 @nt{aggregate} or function call, which may be the @nt{aggregate} or function
5116 call itself],Old=[]}.],
5117 Old=[@Chg{New=[If the @i{object_}@nt{name} in an @nt{object_renaming_declaration}, or
5118 the actual parameter for a generic formal @key[in out] parameter in a
5119 @nt{generic_instantiation}, denotes any part of an anonymous object created by
5120 a function call, the anonymous object is not finalized until after it is no
5121 longer accessible via any name. Otherwise, an],
5122 Old=[The]} anonymous object@Chg{New=[],Old=[s]} created by
5123 @Chg{New=[a ],Old=[]}function @Chg{New=[call or],Old=[calls and]} by
5124 @Chg{New=[an ],Old=[]}@nt{aggregate}@Chg{New=[ is],Old=[s are]}
5125 finalized no later than the end of the innermost enclosing
5126 @nt{declarative_item} or @nt{statement};
5127 if that is a @nt{compound_statement},
5128 @Chg{New=[the object is],Old=[they are]} finalized before starting the
5129 execution of any @nt{statement} within the @nt{compound_statement}.]}
5130 @begin{Honest}
5131 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00162-01]}@ChgNote{Should be ChgDeleted}
5132 @ChgDeleted{Version=[2],Text=[@leading@;This is not to be construed as permission to call Finalize
5133 asynchronously with respect to normal user code.
5134 For example,]}
5135 @begin{Example}
5136 @ChgRef{Version=[2],Kind=[Deleted]}
5137 @ChgDeleted{Version=[2],Text=[@key[declare]
5138 X : Some_Controlled_Type := F(G(...));
5139 --@RI{ The anonymous objects created for F and G are finalized}
5140 --@RI{ no later than this point.}
5141 Y : ...
5142 @key[begin]
5143 ...
5144 @key[end];]}
5145 @end{Example}
5146
5147 @ChgRef{Version=[2],Kind=[Deleted]}
5148 @ChgDeleted{Version=[2],Text=[
5149 The anonymous object for G should not be finalized at some random
5150 point in the middle of the body of F,
5151 because F might manipulate the same data structures as
5152 the Finalize operation, resulting in erroneous access
5153 to shared variables.]}
5154 @end{Honest}
5155 @begin{Reason}
5156 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01]}
5157 @Chg{Version=[2],New=[This effectively imports all of the special rules for
5158 the accessibility level of renames, @nt{allocator}s, and so on, and applies
5159 them to determine where objects created in them are finalized. For instance,
5160 the master of a rename of a subprogram is that of the renamed subprogram.],
5161 Old=[It might be quite inconvenient for the implementation to defer
5162 finalization of the anonymous object for G until after copying the
5163 value of F into X, especially if the size of the result
5164 is not known at the call site.]}
5165
5166 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0066-1]}
5167 @ChgAdded{Version=[3],Text=[In @RefSecNum{Operations of Access Types}
5168 we assign an accessibility level to the result of an
5169 @nt{aggregate} or function call that is used to directly initialize a
5170 part of an object based on the object being initialized. This is
5171 important to ensure that any access discriminants denote objects
5172 that live at least as long as the object being initialized.
5173 However, if the result of the @nt{aggregate} or function call is not
5174 built directly in the target object, but instead is built in an
5175 anonymous object that is then assigned to the target, the anonymous
5176 object needs to be finalized after the assignment rather than
5177 persisting until the target object is finalized (but not its
5178 coextensions). (Note than an implementation is never required to
5179 create such an anonymous object, and in some cases is required to
5180 @i{not} have such a separate object, but rather to build the result
5181 directly in the target.)]}
5182
5183 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0142-4]}
5184 @ChgAdded{Version=[3],Text=[The special case for explicitly aliased parameters
5185 of functions is needed for the same reason, as access discriminants of the
5186 returned object may designate one of these parameters. In that case, we want to
5187 lengthen the lifetime of the anonymous objects as long as the possible lifetime
5188 of the result.]}
5189
5190 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0142-4]}
5191 @ChgAdded{Version=[3],Text=[We don't do a similar change for other kinds of
5192 calls, because the extended lifetime of the parameters adds no value, but could
5193 constitute a storage leak. For instance, such an anonymous object created by a
5194 procedure call in the elaboration part of a package body would have to live
5195 until the end of the program, even though it could not be used after the
5196 procedure returns (other than via Unchecked_Access).]}
5197 @end{Reason}
5198
5199 @begin{Ramification}
5200 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0142-4]}
5201 @ChgAdded{Version=[3],Text=[Note that the lifetime of the master given to
5202 anonymous objects in explicitly aliased parameters of functions is not
5203 necessarily as long as the lifetime of the master of the object being
5204 initialized (if the function call is used to initialize an @nt{allocator}, for
5205 instance). In that case, the accessibility check on explicitly aliased
5206 parameters will necessarily fail if any such anonymous objects exist. This is
5207 necessary to avoid requiring the objects to live as long as the access type or
5208 having the implementation complexity of an implicit coextension.]}
5209 @end{Ramification}
5210
5211
5212 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
5213 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00162-01]}
5214 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0066-1],ARef=[AI05-0262-1]}
5215 @Chg{Version=[2],New=[In the case of an @nt{expression} that is a master,
5216 finalization of any (anonymous) objects occurs @Chg{Version=[3],New=[after
5217 completing],Old=[as the final part of]}
5218 evaluation of the @nt{expression}@Chg{Version=[3],New=[ and all use of the
5219 objects, prior to starting the execution of any subsequent construct],Old=[]}.],
5220 Old=[@Chg{New=[If a transfer of control or raising of an exception occurs prior to
5221 performing a finalization of an anonymous object, the anonymous object is
5222 finalized as part of the finalizations due to be performed for the object's
5223 innermost enclosing master.],Old=[]}]}
5224
5225 @end{RunTime}
5226
5227 @begin{Bounded}
5228 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0023],ARef=[AI95-00169-01]}
5229 @leading@PDefn2{Term=(bounded error),Sec=(cause)}
5230 It is a bounded error for a call on Finalize or Adjust @Chg{New=[that occurs as
5231 part of object finalization or assignment ], Old=[]}to propagate an exception.
5232 The possible consequences depend on what action invoked the Finalize or
5233 Adjust operation:
5234 @begin{Ramification}
5235 It is not a bounded error for Initialize to propagate an
5236 exception. If Initialize propagates an exception,
5237 then no further calls on Initialize are performed,
5238 and those components that have already been initialized
5239 (either explicitly or by default)
5240 are finalized in the usual way.
5241
5242 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
5243 @Chg{New=[It also is not a bounded error for an explicit call to Finalize or
5244 Adjust to propagate an exception. We do not want implementations to have to
5245 treat explicit calls to these routines specially.],Old=[]}
5246 @end{Ramification}
5247 @begin{Itemize}
5248 @Defn2{Term=[Program_Error],Sec=(raised by failed finalization)}
5249 For a Finalize invoked as part of an @nt<assignment_statement>,
5250 Program_Error is raised at that point.
5251
5252 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0024],ARef=[AI95-00193-01]}
5253 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
5254 @Chg{New=[For an Adjust invoked as part of @Chg{Version=[2],New=[assignment
5255 operations other than those invoked as part of an @nt{assignment_statement}],
5256 Old=[the initialization of a controlled object]}, other adjustments due to be
5257 performed might or might not be performed, and then Program_Error is raised.
5258 During its propagation, finalization might or
5259 might not be applied to objects whose Adjust failed.],Old=[]}
5260 @Defn2{Term=[Program_Error],Sec=(raised by failed finalization)}
5261 For an Adjust invoked as part of an @Chg{Version=[2],New=[@nt{assignment_statement}],
5262 Old=[assignment @Chg{New=[statement],Old=[operation]}]}, any other adjustments
5263 due to be performed are performed, and then Program_Error is raised.
5264 @begin{Reason}
5265 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0024],ARef=[AI95-00193-01]}
5266 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
5267 @ChgAdded{Version=[1],Text=[In the case of assignments that are part of
5268 initialization, there is
5269 no need to complete all adjustments if one propagates an exception, as the
5270 object will immediately be finalized. So long as a subcomponent is not going
5271 to be finalized, it need not be adjusted, even if it is initialized as part of
5272 an enclosing composite assignment operation for which some adjustments are
5273 performed. However, there is no harm in an implementation making additional
5274 Adjust calls (as long as any additional components that are adjusted are also
5275 finalized), so we allow the implementation flexibility here.
5276 On the other hand, for an @Chg{Version=[2],New=[@nt{assignment_statement}],
5277 Old=[assignment statement]}, it is important that all
5278 adjustments be performed, even if one fails, because all controlled
5279 subcomponents are going to be finalized.@Chg{Version=[2],New=[ Other kinds of
5280 assignment are more like initialization than @nt{assignment_statement}s, so we
5281 include them as well in the permission.],Old=[]}]}
5282 @end{Reason}
5283 @begin{Ramification}
5284 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0024],ARef=[AI95-00193-01]}
5285 @ChgAdded{Version=[1],Text=[Even if an Adjust invoked as part of the
5286 initialization of a
5287 controlled object propagates an exception, objects whose initialization
5288 (including any Adjust or Initialize calls) successfully completed will be
5289 finalized. The permission above only applies to objects whose Adjust failed.
5290 Objects for which Adjust was never even invoked must not be finalized.]}
5291 @end{Ramification}
5292
5293 @Defn2{Term=[Program_Error],Sec=(raised by failed finalization)}
5294 For a Finalize invoked as part of a call on an instance of
5295 Unchecked_Deallocation, any other finalizations due to
5296 be performed are performed, and then Program_Error is raised.
5297 @begin{Discussion}
5298 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0104],ARef=[AI95-00179-01]}
5299 @ChgAdded{Version=[1],Text=[The standard does not specify if storage is
5300 recovered in this case.
5301 If storage is not recovered (and the object continues to exist), Finalize
5302 may be called on the object again (when the @nt<allocator>'s master is
5303 finalized).]}
5304 @end{Discussion}
5305
5306 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
5307 @ChgRef{Version=[3],Kind=[DeletedAdded],ARef=[AI05-0064-1]}
5308 @ChgDeleted{Version=[3],Text=[@Chg{New=[@Defn2{Term=[Program_Error],Sec=(raised by failed finalization)}
5309 For a Finalize invoked as part of the finalization of the anonymous
5310 object created by a function call or @nt{aggregate}, any other finalizations
5311 due to be performed are performed, and then Program_Error is raised.],Old=[]}]}
5312
5313 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
5314 @Chg{New=[@Defn2{Term=[Program_Error],Sec=(raised by failed finalization)}
5315 For a Finalize invoked due to reaching the end of the execution of a
5316 master, any other finalizations associated with the master are performed, and
5317 Program_Error is raised immediately after leaving the master.],Old=[]}
5318
5319 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0064-1]}
5320 @begin{Discussion}
5321 @ChgAdded{Version=[3],Text=[This rule covers both ordinary objects created
5322 by a declaration, and anonymous objects created as part of evaluating an
5323 @nt{expression}. All contexts that create objects that need finalization
5324 are defined to be masters.]}
5325 @end{Discussion}
5326
5327 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
5328 @Defn2{Term=[Program_Error],Sec=(raised by failed finalization)}
5329 For a Finalize invoked by the transfer of control of
5330 an @Chg{Version=[2],
5331 New=[@nt{exit_statement}, return statement, @nt{goto_statement}],
5332 Old=[@ntf{exit_}, @ntf{return_}, @ntf{goto_}]},
5333 or @nt{requeue_@!statement},
5334 Program_Error is raised no earlier than after the finalization of the
5335 master being finalized when the exception occurred,
5336 and no later than the point where normal execution would have continued.
5337 Any other finalizations due to be performed up to that point are
5338 performed before raising Program_Error.
5339 @begin{Ramification}
5340 For example, upon leaving a @nt{block_statement} due to a
5341 @nt{goto_statement}, the Program_Error would be raised at the point of the
5342 target statement denoted by the label,
5343 or else in some more dynamically nested place,
5344 but not so nested as to allow an @nt{exception_handler} that has
5345 visibility upon the finalized object to handle it.
5346 For example,
5347 @begin{Example}
5348 @key[procedure] Main @key[is]
5349 @key[begin]
5350 <<The_Label>>
5351 Outer_Block_Statement : @key[declare]
5352 X : Some_Controlled_Type;
5353 @key[begin]
5354 Inner_Block_Statement : @key[declare]
5355 Y : Some_Controlled_Type;
5356 Z : Some_Controlled_Type;
5357 @key[begin]
5358 @key[goto] The_Label;
5359 @key[exception]
5360 @key[when] Program_Error => ... --@RI{ Handler number 1.}
5361 @key[end];
5362 @key[exception]
5363 @key[when] Program_Error => ... --@RI{ Handler number 2.}
5364 @key[end];
5365 @key[exception]
5366 @key[when] Program_Error => ... --@RI{ Handler number 3.}
5367 @key[end] Main;
5368 @end{Example}
5369
5370 The @nt{goto_statement} will first cause
5371 Finalize(Y) to be called.
5372 Suppose that Finalize(Y) propagates an exception.
5373 Program_Error will be raised after leaving Inner_Block_Statement,
5374 but before leaving Main.
5375 Thus, handler number 1 cannot handle this Program_Error;
5376 it will be handled either by handler number 2 or handler number 3.
5377 If it is handled by handler number 2,
5378 then Finalize(Z) will be done before executing the handler.
5379 If it is handled by handler number 3,
5380 then Finalize(Z) and Finalize(X) will both
5381 be done before executing the handler.
5382 @end{Ramification}
5383
5384 For a Finalize invoked by a transfer of control
5385 that is due to raising an exception,
5386 any other finalizations due to be performed for the same master are
5387 performed; Program_Error is raised immediately after leaving the master.
5388 @begin{Ramification}
5389 If, in the above example, the @nt{goto_statement} were replaced by a
5390 @nt{raise_statement}, then the Program_Error would be handled by
5391 handler number 2, and
5392 Finalize(Z) would be done before executing the handler.
5393 @end{Ramification}
5394 @begin{Reason}
5395 We considered treating this case in the same way as the others,
5396 but that would render certain @nt{exception_handler}s useless.
5397 For example, suppose the only @nt{exception_handler} is
5398 one for @key{others} in the main subprogram.
5399 If some deeply nested call raises an exception,
5400 causing some Finalize operation to be called,
5401 which then raises an exception,
5402 then normal execution @lquotes@;would have continued@rquotes@;
5403 at the beginning of the @nt{exception_handler}.
5404 Raising Program_Error at that point would cause that
5405 handler's code to be skipped.
5406 One would need two nested @nt{exception_handler}s
5407 to be sure of catching such cases!
5408
5409 On the other hand, the @nt{exception_handler} for a given master
5410 should not be allowed to handle exceptions raised during finalization
5411 of that master.
5412 @end{Reason}
5413
5414 For a Finalize invoked by a transfer of control due to
5415 an abort or selection of a terminate alternative,
5416 the exception is ignored;
5417 any other finalizations due to be performed are performed.
5418 @begin{Ramification}
5419 This case includes an asynchronous transfer of control.
5420 @end{Ramification}
5421 @begin{Honest}
5422 @Defn2{Term=[Program_Error],Sec=(raised by failure of runtime check)}
5423 This violates the general principle that it is always possible for
5424 a bounded error to raise Program_Error
5425 (see @RefSec{Classification of Errors}).
5426 @end{Honest}
5427 @end{Itemize}
5428 @end{Bounded}
5429
5430 @begin{ImplPerm}
5431 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0107-1]}
5432 @ChgAdded{Version=[3],Text=[If the execution of an @nt{allocator} propagates an
5433 exception, any parts of the allocated object that were successfully initialized
5434 may be finalized as part of the finalization of the innermost master enclosing
5435 the @nt{allocator}.]}
5436
5437 @begin{Reason}
5438 @ChgRef{Version=[3],Kind=[AddedNormal]}
5439 @ChgAdded{Version=[3],Text=[This allows deallocating the memory for the
5440 allocated object at the innermost master, preventing a storage leak.
5441 Otherwise, the object would have to stay around until the finalization of the
5442 collection that it belongs to, which could be the entire life of the program
5443 if the associated access type is library level.]}
5444 @end{Reason}
5445
5446 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0111-3],ARef=[AI05-0262-1]}
5447 @ChgAdded{Version=[3],Text=[The implementation may finalize objects created by
5448 @nt{allocator}s for an access type whose storage pool supports subpools (see
5449 @RefSecNum{Storage Subpools}) as if the objects were created (in an arbitrary
5450 order) at the point where the storage pool was elaborated instead of at the first
5451 freezing point of the access type.@PDefn2{Term=[arbitrary order],Sec=[allowed]}]}
5452
5453 @begin{Ramification}
5454 @ChgRef{Version=[3],Kind=[AddedNormal]}
5455 @ChgAdded{Version=[3],Text=[This allows the finalization of such objects to
5456 occur later than they otherwise would, but still as part of the finalization
5457 of the same master. Accessibility rules in @RefSecNum{Storage Subpools} ensure
5458 that it is the same master (usually that of the environment task).]}
5459 @end{Ramification}
5460
5461 @begin{ImplNote}
5462 @ChgRef{Version=[3],Kind=[AddedNormal]}
5463 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0005-1]}
5464 @ChgAdded{Version=[3],Text=[This permission is intended to allow the allocated
5465 objects to "belong" to the subpool objects and to allow those objects to be
5466 finalized at the time that the storage pool is finalized (if they are not
5467 finalized earlier). This is expected to ease implementation, as the
5468 @Chg{Version=[5],New=[remaining yet-to-be deallocated ],Old=[]}objects
5469 will only need to @Chg{Version=[5],New=[be accessible at run time from
5470 the subpool header and not also from the overall access type collection
5471 header. That is, they only need to belong to a single list,
5472 rather two],Old=[belong to the subpool and not also to the collection]}.]}
5473 @end{ImplNote}
5474
5475 @end{ImplPerm}
5476
5477
5478 @begin{Notes}
5479 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
5480 The rules of @Chg{Version=[3],New=[Clause],Old=[Section]} 10 imply that
5481 immediately prior to partition termination, Finalize operations
5482 are applied to library-level controlled objects (including those
5483 created by @nt{allocator}s of library-level access types, except those
5484 already finalized).
5485 This occurs after waiting for library-level
5486 tasks to terminate.
5487 @begin{Discussion}
5488 We considered defining a pragma that would apply to a controlled
5489 type that would suppress Finalize operations for library-level objects
5490 of the type upon partition termination.
5491 This would be useful for types whose finalization actions
5492 consist of simply reclaiming global heap storage, when this is already
5493 provided automatically by the environment upon program termination.
5494 @end{Discussion}
5495
5496 A constant is only constant between its initialization and
5497 finalization.
5498 Both initialization and finalization are allowed to
5499 change the value of a constant.
5500
5501 Abort is deferred during certain operations related to controlled types,
5502 as explained in
5503 @RefSecNum{Abort of a Task - Abort of a Sequence of Statements}.
5504 Those rules prevent an abort from causing a controlled object
5505 to be left in an ill-defined state.
5506
5507 The Finalize procedure is called upon finalization of
5508 a controlled object, even if Finalize was called earlier,
5509 either explicitly or as part of an assignment; hence,
5510 if a controlled type is visibly controlled (implying that its Finalize
5511 primitive is directly callable), or is nonlimited (implying that
5512 assignment is allowed), its Finalize procedure should be
5513 designed to have no ill effect if it is applied a second time
5514 to the same object.
5515 @begin{Discussion}
5516 Or equivalently, a Finalize procedure
5517 should be @lquotes@;idempotent@rquotes@;;
5518 applying it twice to the same object should be equivalent to
5519 applying it once.
5520 @end{Discussion}
5521 @begin{Reason}
5522 A user-written Finalize procedure should be idempotent since it
5523 can be called explicitly
5524 by a client (at least if the type is "visibly" controlled).
5525 Also, Finalize is used implicitly as part of the
5526 @nt<assignment_statement> if the
5527 type is nonlimited, and an abort is permitted to disrupt an
5528 @nt<assignment_statement> between finalizing the left-hand side
5529 and assigning the new value to it (an abort is not permitted to
5530 disrupt an assignment operation between copying in the new value
5531 and adjusting it).
5532 @end{Reason}
5533 @begin{Discussion}
5534 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
5535 Either Initialize or Adjust,
5536 but not both, is applied to (almost) every controlled object
5537 when it is created:
5538 Initialize is done when no initial value is assigned to the object,
5539 whereas Adjust is done as part of assigning the initial value.
5540 The one exception is the @Chg{Version=[2],New=[],Old=[anonymous ]}object
5541 @Chg{Version=[2],New=[initialized],Old=[created]} by an
5542 @nt{aggregate}@Chg{Version=[2],New=[ (both the anonymous object created for
5543 an aggregate, or an object initialized by an @nt{aggregate} that is
5544 built-in-place)],Old=[]}; Initialize is not applied to the @nt{aggregate}
5545 as a whole, nor is the value of the @nt<aggregate> @Chg{Version=[2],
5546 New=[or object ],Old=[]}adjusted.
5547
5548 @leading@Defn2{Term=[assignment operation], Sec=(list of uses)}
5549 All of the following use the assignment operation,
5550 and thus perform value adjustment:
5551 @begin{Itemize}
5552 the @nt{assignment_statement} (see @RefSecNum{Assignment Statements});
5553
5554 explicit initialization of a stand-alone object
5555 (see @RefSecNum{Object Declarations})
5556 or of a pool element (see @RefSecNum{Allocators});
5557
5558 default initialization of a component of a stand-alone object
5559 or pool element
5560 (in this case, the value of each component is assigned, and therefore adjusted,
5561 but the value of the object as a whole is not adjusted);
5562
5563 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
5564 function return, when the result @Chg{Version=[2],New=[is not built-in-place],
5565 Old=[type is not a return-by-reference
5566 type (see @RefSecNum{Return Statements});]}
5567 (adjustment of the result happens before finalization of
5568 the function@Chg{Version=[2],New=[],Old=[;
5569 values of return-by-reference types are not
5570 adjusted]});
5571
5572 predefined operators (although the only one that matters is
5573 concatenation; see @RefSecNum{Binary Adding Operators});
5574
5575 generic formal objects of mode @key{in}
5576 (see @RefSecNum{Formal Objects});
5577 these are defined in terms of constant declarations; and
5578
5579 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
5580 @nt{aggregate}s (see @RefSecNum{Aggregates})@Chg{Version=[2],New=[, when
5581 the result is not built-in-place],Old=[]}
5582 (in this case, the value of each component, and the parent part, for an
5583 @nt{extension_aggregate}, is assigned, and therefore adjusted,
5584 but the value of the @nt{aggregate} as a whole is not adjusted;
5585 neither is Initialize called);
5586 @end{Itemize}
5587
5588 @leading@;The following also use the assignment operation,
5589 but adjustment never does anything interesting in these cases:
5590 @begin{Itemize}
5591 By-copy parameter passing uses the assignment operation
5592 (see @RefSecNum{Parameter Associations}),
5593 but controlled objects are always passed by reference,
5594 so the assignment operation never does anything interesting in
5595 this case.
5596 If we were to allow by-copy parameter passing for controlled objects,
5597 we would need to make sure that the actual is finalized before doing
5598 the copy back for [@key{in}] @key{out} parameters.
5599 The finalization of the parameter itself needs to happen
5600 after the copy back (if any),
5601 similar to the finalization of an anonymous function return
5602 object or @nt{aggregate} object.
5603
5604 @key{For} loops use the assignment operation
5605 (see @RefSecNum{Loop Statements}), but since the type
5606 of the loop parameter is never controlled,
5607 nothing interesting happens there, either.
5608
5609 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00318-02]}
5610 @ChgAdded{Version=[2],Text=[Objects initialized by function results and
5611 @nt{aggregate}s that are built-in-place. In this case, the assignment
5612 operation is never executed, and no adjustment takes place. While
5613 built-in-place
5614 is always allowed, it is required for some types @em see
5615 @RefSecNum{Limited Types} and
5616 @RefSecNum{Assignment and Finalization} @em and that's
5617 important since limited types have no Adjust to call.]}
5618 @end{Itemize}
5619
5620 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00287-01]}
5621 @ChgDeleted{Version=[2],Text=[Because Controlled and Limited_Controlled
5622 are library-level tagged types,
5623 all controlled types will be library-level types,
5624 because of the accessibility rules
5625 (see @RefSecNum{Operations of Access Types} and @RefSecNum{Type Extensions}).
5626 This ensures that the Finalize operations may be applied without
5627 providing any @lquotes@;display@rquotes@; or @lquotes@;static-link.@rquotes@;
5628 This simplifies finalization as a result of garbage collection,
5629 abort, and asynchronous transfer of control.]}
5630
5631 Finalization of the parts of a protected object are not done as
5632 protected actions.
5633 It is possible (in pathological cases)
5634 to create tasks during finalization that
5635 access these parts in parallel with the finalization itself.
5636 This is an erroneous use of shared variables.
5637 @end{Discussion}
5638 @begin{ImplNote}
5639 One implementation technique for finalization is to chain the
5640 controlled objects together on a per-task list.
5641 When leaving a master, the list can be walked up to a marked place.
5642 The links needed to implement the list can be declared (privately)
5643 in types Controlled and Limited_Controlled,
5644 so they will be inherited by all controlled types.
5645
5646 Another implementation technique, which we refer to as the @lquotes@;PC-map@rquotes@;
5647 approach essentially implies inserting exception handlers at various
5648 places, and finalizing objects based on where the exception was
5649 raised.
5650
5651 @Defn{PC-map approach to finalization}
5652 @Defn{program-counter-map approach to finalization}
5653 The PC-map approach is for the compiler/linker to create a map of
5654 code addresses; when an exception is raised, or abort occurs,
5655 the map can be consulted to see where the task was executing,
5656 and what finalization needs to be performed.
5657 This approach was given in the Ada 83 Rationale as a
5658 possible implementation strategy for exception handling @em the
5659 map is consulted to determine which exception handler applies.
5660
5661 If the PC-map approach is used, the implementation must take care in the
5662 case of arrays. The generated code will generally contain a loop to
5663 initialize an array. If an exception is raised part way through the
5664 array, the components that have been initialized must be finalized,
5665 and the others must not be finalized.
5666
5667 It is our intention that both of these implementation methods should
5668 be possible.
5669 @end{ImplNote}
5670 @end{Notes}
5671
5672 @begin{DiffWord83}
5673 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
5674 Finalization depends on the concepts of completion and leaving,
5675 and on the concept of a master.
5676 Therefore, we have moved the definitions of these concepts here,
5677 from where they used to be in
5678 @Chg{Version=[3],New=[Clause],Old=[Section]} @RefSecNum{Tasks and Synchronization}.
5679 These concepts also needed to be generalized somewhat.
5680 Task waiting is closely related to user-defined finalization;
5681 the rules here refer to the task-waiting rules of
5682 @Chg{Version=[3],New=[Clause],Old=[Section]} @RefSecNum{Tasks and Synchronization}.
5683 @end{DiffWord83}
5684
5685 @begin{Inconsistent95}
5686 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0066-1]}
5687 @ChgAdded{Version=[3],Text=[@Defn{inconsistencies with Ada 95}@b<Ada 2012 Correction:>
5688 Changed the definition
5689 of the master of an anonymous object used to directly initialize an
5690 object, so it can be finalized immediately rather than having to hang
5691 around as long as the object. In this case, the Ada 2005 definition was
5692 inconsistent with Ada 95, and Ada 2012 changes it back. It is unlikely
5693 that many compilers implemented the rule as written in Amendment 1,
5694 so an inconsistency is unlikely to arise in practice.]}
5695 @end{Inconsistent95}
5696
5697 @begin{DiffWord95}
5698 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0021],ARef=[AI95-00182-01]}
5699 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Fixed the wording to say that
5700 anonymous objects aren't finalized until the object can't be used anymore.]}
5701
5702 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0023],ARef=[AI95-00169-01]}
5703 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Added wording to clarify what
5704 happens when Adjust or Finalize raises an exception; some cases had been
5705 omitted.]}
5706
5707 @ChgRef{Version=[1],Kind=[AddedNormal],Ref=[8652/0024],ARef=[AI95-00193-01]}
5708 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
5709 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Stated that if Adjust raises an
5710 exception during initialization, nothing further is required. This is
5711 corrected in Ada 2005 to include all kinds of assignment other than
5712 @nt{assignment_statement}s.]}
5713
5714 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00162-01],ARef=[AI95-00416-01]}
5715 @ChgAdded{Version=[2],Text=[Revised the definition of master to include
5716 @nt{expression}s and @nt{statement}s, in order to cleanly define what
5717 happens for tasks and controlled objects created as part of a subprogram call.
5718 Having done that, all of the special wording to cover those cases is
5719 eliminated (at least until the Ada comments start rolling in).]}
5720
5721 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00280-01]}
5722 @ChgAdded{Version=[2],Text=[We define @i{finalization of the collection}
5723 here, so as to be able to conveniently refer to it in other rules (especially
5724 in @RefSec{Allocators}).]}
5725
5726 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
5727 @ChgAdded{Version=[2],Text=[Clarified that a coextension is finalized at
5728 the same time as the outer object. (This was intended for Ada 95, but since
5729 the concept did not have a name, it was overlooked.)]}
5730 @end{DiffWord95}
5731
5732 @begin{Inconsistent2005}
5733 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0051-1],ARef=[AI05-0190-1]}
5734 @ChgAdded{Version=[3],Text=[@Defn{inconsistencies with Ada 2005}@b<Correction:>
5735 Better defined when objects allocated from anonymous access types are
5736 finalized. This could be inconsistent if objects are finalized in
5737 a different order than in an Ada 2005 implementation and that order caused
5738 different program behavior; however programs that depend on the order of
5739 finalization within a single master are already fragile and hopefully
5740 are rare.]}
5741 @end{Inconsistent2005}
5742
5743 @begin{DiffWord2005}
5744 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0064-1]}
5745 @ChgAdded{Version=[3],Text=[@b<Correction:> Removed a redundant rule,
5746 which is now covered by the additional places where masters are defined.]}
5747
5748 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0099-1]}
5749 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0005-1]}
5750 @ChgAdded{Version=[3],Text=[@b<Correction:> Clarified the finalization
5751 rules so that there is no doubt that privacy is ignored, and to ensure
5752 that objects of @Chg{Version=[4],New=[class-wide],Old=[classwide]} interface
5753 types are finalized based on their specific concrete type.]}
5754
5755 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0107-1]}
5756 @ChgAdded{Version=[3],Text=[@b<Correction:> Allowed premature finalization
5757 of parts of failed @nt{allocator}s. This could be an inconsistency, but the
5758 previous behavior is still allowed and there is no requirement that
5759 implementations take advantage of the permission.]}
5760
5761 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0111-3]}
5762 @ChgAdded{Version=[3],Text=[Added a permission to finalize an object allocated
5763 from a subpool later than usual.]}
5764
5765 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0142-4]}
5766 @ChgAdded{Version=[3],Text=[Added text to specially define the master of
5767 anonymous objects which are passed as explicitly aliased parameters (see
5768 @RefSecNum{Subprogram Declarations}) of functions. The model for these
5769 parameters is explained in detail in @RefSecNum{Parameter Associations}.]}
5770 @end{DiffWord2005}
5771