Debian Codegraph @

source_2012/07.mss

raw
1 @Part(07, Root="ada.mss")
2
3 @Comment{$Date: 2015/04/03 04:12:41 $}
4 @LabeledSection{Packages}
5
6 @Comment{$Source: e:\\cvsroot/ARM/Source/07.mss,v $}
7 @Comment{$Revision: 1.138 $}
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 @ChgAdded{Version=[2],Text=[ @key{procedure} Bar (X : T1'Class) @key{is}
709 @key{begin}
710 Pkg.Foo (X); -- @RI[should call Foo #1 or an override thereof]
711 @key{end};]}
712
713 @ChgRef{Version=[2],Kind=[AddedNormal]}
714 @ChgAdded{Version=[2],Text=[@key{begin}
715 Pkg.Foo (Pkg.Ifc'Class (P_Client.X)); -- @RI[should call Foo #2]
716 Bar (T1'Class (P_Client.X));
717 @key{end} P;]}
718 @end{Example}
719 @ChgRef{Version=[2],Kind=[AddedNormal]}
720 @ChgAdded{Version=[2],Text=[This example is illegal because the completion
721 of T1 is descended from an interface that the partial view is not descended
722 from. If it were legal, T2 would implement Ifc twice, once in the visible
723 part of P, and once in the visible part of P_Client. We would need to
724 decide how Foo #1 and Foo #2 relate to each other. There are two options:
725 either Foo #2 overrides Foo #1, or it doesn't.]}
726
727 @ChgRef{Version=[2],Kind=[AddedNormal]}
728 @ChgAdded{Version=[2],Text=[If Foo #2 overrides Foo #1,
729 we have a problem because the client redefines a behavior that it doesn't
730 know about, and we try to avoid this at all costs, as it would lead to a
731 breakdown of whatever abstraction was implemented. If the abstraction
732 didn't expose that it implements Ifc, there must be a reason, and it should
733 be able to depend on the fact that no overriding takes place in clients.
734 Also, during maintenance, things may change and the full view might
735 implement a different set of interfaces. Furthermore, the situation is even
736 worse if the full type implements another interface Ifc2 that happens to
737 have a conforming Foo (otherwise unrelated, except for its name and
738 profile).]}
739
740 @ChgRef{Version=[2],Kind=[AddedNormal]}
741 @ChgAdded{Version=[2],Text=[If Foo #2 doesn't override Foo #1,
742 there is some similarity with the case of normal tagged private types,
743 where a client can declare an operation that happens to conform to some
744 private operation, and that's OK, it gets a different slot in the type
745 descriptor. The problem here is that T2 would implement Ifc in two
746 different ways, and through conversions to Ifc'Class we could end up with
747 visibility on both of these two different implementations. This is the
748 @lquotes@;diamond inheritance@rquotes problem of C++ all over again, and we
749 would need some kind of a preference rule to pick one implementation. We
750 don't want to go there (if we did, we might as well provide full-fledged
751 multiple inheritance).]}
752
753 @ChgRef{Version=[2],Kind=[AddedNormal]}
754 @ChgAdded{Version=[2],Text=[Note that there wouldn't be any difficulty to
755 implement the first option, so the restriction is essentially
756 methodological. The second option might be harder to implement, depending
757 on the language rules that we would choose.]}
758 @end{Reason}
759
760 @begin{Ramification}
761 @ChgRef{Version=[2],Kind=[AddedNormal]}
762 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
763 @ChgAdded{Version=[2],Text=[This rule also prevents completing a private type
764 with an interface. @Chg{Version=[3],New=[An],Old=[A]} interface, like all
765 types, is a descendant of itself,
766 and thus this rule is triggered. One reason this is necessary is that
767 a client of a private extension should be able to inherit limitedness
768 without having to look in the private part to see if the type is an
769 interface (remember that limitedness of interfaces is never inherited, while
770 it is inherited from other types).]}
771 @end{Ramification}
772
773 @end{Itemize}
774
775 @Defn2{Term=[ancestor subtype], Sec=(of a @nt<private_extension_declaration>)}
776 The @i(ancestor subtype) of a @nt<private_extension_declaration>
777 is the subtype defined by the @i(ancestor_)@!@nt<subtype_@!indication>;
778 the ancestor type shall be a specific tagged type.
779 The full view of a private extension shall be derived
780 (directly or indirectly) from the ancestor type.
781 In addition to the places where @LegalityTitle normally apply
782 (see @RefSecNum{Generic Instantiation}),
783 the requirement that the ancestor be specific applies also in the
784 private part of an instance of a generic unit.@PDefn{generic contract issue}
785 @begin{Reason}
786 This rule allows the full view to be defined
787 through several intermediate derivations,
788 possibly from a series of types produced by
789 @nt{generic_instantiation}s.
790 @end{Reason}
791
792 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
793 @ChgAdded{Version=[2],Text=[If the reserved word @key{limited} appears in a
794 @nt{private_extension_declaration}, the ancestor type shall be a limited type.
795 If the reserved word @key{synchronized} appears in a
796 @nt{private_extension_declaration}, the ancestor type shall be a limited
797 interface.]}
798
799 If the declaration of a partial view includes
800 a @nt{known_discriminant_part}, then
801 the @nt{full_type_declaration} shall have a fully conforming
802 @Redundant[(explicit)]
803 @nt{known_discriminant_part}
804 @Redundant[(see @RefSec(Conformance Rules))].
805 @Defn2{Term=[full conformance],Sec=(required)}
806 @Redundant[The ancestor subtype may be unconstrained;
807 the parent subtype of the full view is required to be constrained
808 (see @RefSecNum{Discriminants}).]
809 @begin{Discussion}
810 If the ancestor subtype has discriminants,
811 then it is usually best to make it unconstrained.
812 @end{Discussion}
813
814 @begin{Ramification}
815 If the partial view has a @nt<known_discriminant_part>,
816 then the full view has to be a composite, non-array type,
817 since only such types may have known discriminants.
818 Also, the full view cannot inherit the discriminants in this case;
819 the @nt{known_discriminant_part} has to be explicit.
820
821 @leading@keepnext@;That is, the following is illegal:
822 @begin{Example}
823 @key[package] P @key[is]
824 @key[type] T(D : Integer) @key[is] @key[private];
825 @key[private]
826 @key[type] T @key[is] @key[new] Some_Other_Type; --@RI{ Illegal!}
827 @key[end] P;
828 @end{Example}
829 even if Some_Other_Type has an integer discriminant called D.
830
831 It is a ramification of this and other rules that in order for
832 a tagged type to privately inherit unconstrained discriminants,
833 the private type declaration has to have an
834 @nt{unknown_discriminant_part}.
835 @end{Ramification}
836
837
838 If a private extension inherits known discriminants from the ancestor subtype,
839 then the full view shall also inherit its discriminants from the
840 ancestor subtype,
841 and the parent subtype of the full view shall be constrained
842 if and only if the ancestor subtype is constrained.
843 @begin{Reason}
844 The first part ensures that the full view has the same discriminants
845 as the partial view.
846 The second part ensures that if the partial view is unconstrained,
847 then the full view is also unconstrained;
848 otherwise, a client might constrain the partial view in a way that
849 conflicts with the constraint on the full view.
850 @end{Reason}
851
852 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01]}
853 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0004-1]}
854 @ChgAdded{Version=[2],Text=[If the @nt{full_type_declaration} for a private
855 extension @Chg{Version=[3],New=[includes],Old=[is]} a
856 @Chg{Version=[3],New=[@nt{derived_type_definition}],Old=[@ntf{derived_type_declaration}]},
857 then the reserved word
858 @key{limited} shall appear in the @nt{full_type_declaration} if and only if it
859 also appears in the @nt{private_extension_declaration}.]}
860 @begin{Reason}
861 @ChgRef{Version=[2],Kind=[AddedNormal]}
862 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0004-1]}
863 @ChgAdded{Version=[2],Text=[The word @key{limited} is optional (unless the
864 ancestor is an interface), but it should be used consistently. Otherwise
865 things would be too confusing for the reader. Of course, we only require
866 that if the full type @Chg{Version=[3],New=[includes],Old=[is defined by]}
867 a @nt{derived_type_definition}, as we
868 want to allow task and protected types to complete extensions of synchronized
869 interfaces.]}
870 @end{Reason}
871
872 @Redundant[If a partial view has unknown discriminants,
873 then the @nt{full_type_declaration} may define
874 a definite or an indefinite subtype, with or without discriminants.]
875
876 If a partial view has neither known nor unknown discriminants,
877 then the @nt{full_type_declaration} shall define a definite subtype.
878
879 If the ancestor subtype of a private extension has constrained
880 discriminants,
881 then the parent subtype of the full view shall impose a statically
882 matching constraint on those discriminants.
883 @PDefn2{Term=[statically matching],Sec=(required)}
884 @begin{Ramification}
885 If the parent type of the full view is not the ancestor type,
886 but is rather some descendant thereof, the constraint on
887 the discriminants of the parent type might come from
888 the declaration of some intermediate type in the derivation
889 chain between the ancestor type and the parent type.
890 @end{Ramification}
891 @begin{Reason}
892 @leading@keepnext@;This prevents the following:
893 @begin{Example}
894 @key[package] P @key[is]
895 @key[type] T2 @key[is] @key[new] T1(Discrim => 3) @key[with] @key[private];
896 @key[private]
897 @key[type] T2 @key[is] @key[new] T1(Discrim => 999) --@RI{ Illegal!}
898 @key[with] @key[record] ...;
899 @key[end] P;
900 @end{Example}
901
902 The constraints in this example do not statically match.
903
904 @leading@;If the constraint on the parent subtype of the full view depends on
905 discriminants of the full view, then the ancestor subtype has to be
906 unconstrained:
907 @begin{Example}
908 @key[type] One_Discrim(A: Integer) @key[is] @key[tagged] ...;
909 ...
910 @key[package] P @key[is]
911 @key[type] Two_Discrims(B: Boolean; C: Integer) @key[is] @key[new] One_Discrim @key[with] @key[private];
912 @key[private]
913 @key[type] Two_Discrims(B: Boolean; C: Integer) @key[is] @key[new] One_Discrim(A => C) @key[with]
914 @key[record]
915 ...
916 @key[end] @key[record];
917 @key[end] P;
918 @end{Example}
919
920 The above example would be illegal if the private extension said
921 @lquotes@;is new One_Discrim(A => C);@rquotes@;,
922 because then the constraints would not statically match.
923 (Constraints that depend on discriminants are not static.)
924
925 @end{Reason}
926 @end{Legality}
927
928 @begin{StaticSem}
929 @PDefn{private type}
930 A @nt{private_type_declaration} declares a private type
931 and its first subtype.
932 @PDefn{private extension}
933 Similarly, a @nt{private_@!extension_@!declaration} declares a private
934 extension and its first subtype.
935 @begin{Discussion}
936 @Defn{package-private type}
937 A @i(package-private type) is one
938 declared by a @nt<private_type_declaration>;
939 that is,
940 a private type other than a generic formal private type.
941 @Defn{package-private extension}
942 Similarly, a @i(package-private extension) is one
943 declared by a @nt<private_extension_declaration>.
944 These terms are not used in the RM95 version of this document.
945 @end{Discussion}
946
947 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0269-1]}
948 A declaration of a partial view and the corresponding
949 @nt{full_type_declaration} define two views of a single type.
950 The declaration of a partial view
951 together with the visible part define the operations that are
952 available to outside program units;
953 the declaration of the full view together with
954 the private part define other operations whose direct use is
955 possible only within the declarative region of the package itself.
956 @Chg{Version=[3],New=[],Old=[@PDefn{characteristics}]}Moreover,
957 within the scope of the declaration of the full view, the
958 @Chg{Version=[3],New=[characteristics (see
959 @RefSecNum{Derived Types and Classes})],Old=[@i{characteristics}]}
960 of the type are determined by the full view;
961 in particular, within its scope, the full view determines
962 the classes that include the type,
963 which components, entries, and protected subprograms are visible,
964 what attributes and other predefined operations are allowed,
965 and whether the first subtype is static.
966 See @RefSecNum{Private Operations}.
967
968 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00401-01]}
969 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0110-1]}
970 @Chg{Version=[3],New=[For a],Old=[A]}
971 private extension@Chg{Version=[3],New=[, the characteristics],
972 Old=[inherits components]} (including
973 @Chg{Version=[3],New=[components, but excluding ],Old=[]}discriminants
974 @Chg{Version=[3],New=[if],Old=[unless]} there is a new @nt<discriminant_part>
975 specified)@Chg{Version=[3],New=[, predefined operators,],Old=[]} and
976 @Chg{Version=[3],New=[inherited ],Old=[]}user-defined primitive
977 subprograms @Chg{Version=[3],New=[are determined
978 by],Old=[from]} its ancestor type@Chg{Version=[2],New=[ and its progenitor
979 types (if any)],Old=[]}, in the same way that@Chg{Version=[3],New=[ those
980 of],Old=[]} a record extension @Chg{Version=[3],New=[are determined
981 by those of],Old=[inherits components and user-defined primitive
982 subprograms from]} its parent type@Chg{Version=[2],New=[ and its progenitor
983 types],Old=[]}
984 (see @RefSecNum{Derived Types and Classes}@Chg{Version=[3],New=[ and
985 @RefSecNum{Private Operations}],Old=[]}).
986 @begin{Honest}
987 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0110-1]}
988 If an operation of the @Chg{Version=[3],New=[ancestor or],Old=[]} parent type
989 is abstract, then the abstractness of the inherited operation
990 is different for nonabstract record extensions
991 than for nonabstract private extensions
992 (see @RefSecNum{Abstract Types and Subprograms}).
993 @end{Honest}
994 @end{StaticSem}
995
996 @begin{RunTime}
997 @PDefn2{Term=[elaboration], Sec=(private_type_declaration)}
998 The elaboration of a @nt{private_type_declaration} creates a partial
999 view of a type.
1000 @PDefn2{Term=[elaboration], Sec=(private_extension_declaration)}
1001 The elaboration of a @nt{private_extension_declaration} elaborates
1002 the @i(ancestor_)@nt<subtype_indication>, and creates a
1003 partial view of a type.
1004 @end{RunTime}
1005
1006 @begin{Notes}
1007 The partial view of a type as declared by a @nt<private_type_declaration>
1008 is defined to be a composite view (in @RefSecNum{Types and Subtypes}).
1009 The full view of the type might or might not be composite.
1010 A private extension is also composite,
1011 as is its full view.
1012
1013 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
1014 Declaring a private type with an @nt{unknown_discriminant_part} is a
1015 way of preventing clients from creating uninitialized objects of the
1016 type; they are then forced to initialize each object by calling some
1017 operation declared in the visible part of the package.
1018 @Chg{Version=[2],New=[],Old=[If such a type is also limited, then no objects
1019 of the type can be declared outside the scope of the @nt{full_type_declaration},
1020 restricting all object creation to the package defining the type. This
1021 allows complete control over all storage allocation for the type.
1022 Objects of such a type can still be passed as parameters, however.]}
1023 @begin{Discussion}
1024 @Defn{generic contract/private type contract analogy}
1025 Packages with private types are analogous to generic packages with
1026 formal private types,
1027 as follows:
1028 The declaration of a package-private type is like the declaration of
1029 a formal private type.
1030 The visible part of the package is like the generic formal part;
1031 these both specify a contract (that is, a set of operations and other
1032 things available for the private type).
1033 The private part of the package is like an instantiation of the generic;
1034 they both give a @nt{full_type_declaration} that specifies implementation
1035 details of the private type.
1036 The clients of the package are like the body of the generic;
1037 usage of the private type in these places is restricted to the
1038 operations defined by the contract.
1039
1040 In other words, being inside the package is like being outside the
1041 generic, and being outside the package is like being inside the
1042 generic;
1043 a generic is like an @lquotes@;inside-out@rquotes@; package.
1044
1045 This analogy also works for private extensions
1046 in the same inside-out way.
1047
1048 Many of the legality rules are defined with this analogy in mind.
1049 See, for example, the rules relating to operations of [formal]
1050 derived types.
1051
1052 The completion rules for a private
1053 type are intentionally quite similar to the matching rules for a
1054 generic formal private type.
1055
1056 This analogy breaks down in one respect:
1057 a generic actual subtype is a subtype,
1058 whereas the full view for a private type is always a new type.
1059 (We considered allowing the completion of a @nt{private_type_declaration}
1060 to be a @nt{subtype_declaration},
1061 but the semantics just won't work.)
1062 This difference is behind the fact that a generic actual type can be
1063 class-wide, whereas the completion of a private type always declares
1064 a specific type.
1065 @end{Discussion}
1066
1067 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00401]}
1068 The ancestor type specified in a @nt<private_extension_declaration>
1069 and the parent type specified in the corresponding declaration
1070 of a record extension given in the private part need not be the
1071 same@Chg{Version=[2],New=[. If the ancestor
1072 type is not an interface type,],Old=[ @em]} the parent type of the full view
1073 can be any descendant of the ancestor type.
1074 In this case, for a primitive subprogram that is inherited from the
1075 ancestor type and
1076 not overridden, the formal parameter names and default expressions (if any)
1077 come from the corresponding primitive subprogram of the specified ancestor
1078 type, while the body comes from the corresponding primitive subprogram
1079 of the parent type of the full view.
1080 See @RefSecNum{Dispatching Operations of Tagged Types}.
1081
1082 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00401]}
1083 @ChgAdded{Version=[2],Text=[If the ancestor type specified in a
1084 @nt{private_extension_declaration} is an
1085 interface type, the parent type can be any type so long as the full view is a
1086 descendant of the ancestor type. The progenitor types specified in a
1087 @nt{private_extension_declaration} and the progenitor types specified in the
1088 corresponding declaration of a record extension given in the private part need
1089 not be the same @em the only requirement is that the private extension and the
1090 record extension be descended from the same set of interfaces.]}
1091 @end{Notes}
1092
1093 @begin{Examples}
1094 @leading@keepnext@i{Examples of private type declarations:}
1095 @begin{Example}
1096 @key[type] Key @key[is] @key[private];
1097 @key[type] File_Name @key[is] @key[limited] @key[private];
1098 @end{Example}
1099
1100 @begin{WideAbove}
1101 @leading@keepnext@i{Example of a private extension declaration:}
1102 @end{WideAbove}
1103 @begin{Example}
1104 @key[type] List @key[is] @key[new] Ada.Finalization.Controlled @key[with] @key[private];
1105 @end{Example}
1106 @end{Examples}
1107
1108 @begin{Extend83}
1109 @Defn{extensions to Ada 83}
1110 The syntax for a @nt{private_type_declaration} is augmented to
1111 allow the reserved word @key{tagged}.
1112
1113 In Ada 83, a private type without discriminants cannot be completed
1114 with a type with discriminants.
1115 Ada 95 allows the full view to have discriminants,
1116 so long as they have defaults
1117 (that is, so long as the first subtype is definite).
1118 This change is made for uniformity with generics,
1119 and because the rule as stated is simpler and easier to remember
1120 than the Ada 83 rule.
1121 In the original version of Ada 83, the same restriction applied
1122 to generic formal private types.
1123 However, the restriction was removed by the ARG for generics.
1124 In order to maintain the @lquotes@;generic contract/private type contract analogy@rquotes@;
1125 discussed above, we have to apply the same rule to
1126 package-private types.
1127 Note that a private untagged type without discriminants can be
1128 completed with a tagged type with discriminants only if the
1129 full view is constrained, because discriminants of tagged types
1130 cannot have defaults.
1131 @end{Extend83}
1132
1133 @begin{DiffWord83}
1134 RM83-7.4.1(4),
1135 @lquotes@;Within the specification of the package that declares a private type
1136 and before the end of the corresponding full type declaration, a
1137 restriction applies....@rquotes@;,
1138 is subsumed (and corrected) by the rule that
1139 a type shall be completely defined before it is frozen,
1140 and the rule that the parent type of a derived type declaration shall be
1141 completely defined, unless the derived type is a private extension.
1142 @end{DiffWord83}
1143
1144 @begin{Extend95}
1145 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01],ARef=[AI95-00396-01],ARef=[AI95-00401-01]}
1146 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1147 Added @nt{interface_list} to private extensions to
1148 support interfaces and multiple inheritance
1149 (see @RefSecNum{Interface Types}).]}
1150
1151 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
1152 @ChgAdded{Version=[2],Text=[A private extension may specify that it is a
1153 limited type. This is required for interface ancestors (from which
1154 limitedness is not inherited), but it is generally useful as documentation of
1155 limitedness.]}
1156
1157 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00443-01]}
1158 @ChgAdded{Version=[2],Text=[A private extension may specify that it is a
1159 synchronized type. This is required in order so that a regular limited
1160 interface can be used as the ancestor of a synchronized type (we do not
1161 allow hiding of synchronization).]}
1162 @end{Extend95}
1163
1164 @begin{Extend2005}
1165 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
1166 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
1167 An optional @nt{aspect_specification} can be used in a @nt{private_type_declaration} and
1168 a @nt{private_extension_declaration}.
1169 This is described in @RefSecNum{Aspect Specifications}.]}
1170 @end{Extend2005}
1171
1172 @begin{DiffWord2005}
1173 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0110-1]}
1174 @ChgAdded{Version=[3],Text=[@b<Correction:> The description of how a
1175 private extension inherits characteristics was made consistent with the
1176 way formal derived types inherit characteristics (see
1177 @RefSecNum{Formal Private and Derived Types}).]}
1178 @end{DiffWord2005}
1179
1180
1181
1182 @LabeledSubClause{Private Operations}
1183
1184 @begin{Intro}
1185 @Redundant[For a type declared in the visible part of a package
1186 or generic package, certain operations on the type
1187 do not become visible until later in the package @em
1188 either in the private part or the body.
1189 @Defn{private operations}
1190 Such @i{private operations} are available only inside the declarative
1191 region of the package or generic package.]
1192 @end{Intro}
1193
1194 @begin{StaticSem}
1195 The predefined operators that exist for a given type are determined by
1196 the classes to which the type belongs.
1197 For example, an integer type has a predefined "+" operator.
1198 In most cases, the predefined operators of a type are declared
1199 immediately after the definition of the type;
1200 the exceptions are explained below.
1201 Inherited subprograms are also implicitly declared
1202 immediately after the definition of the type,
1203 except as stated below.
1204
1205 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1206 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0029-1]}
1207 For a composite type, the characteristics
1208 (see @RefSecNum{Private Types and Private Extensions})
1209 of the type are determined in part by the
1210 characteristics of its component types.
1211 At the place where the composite type is declared,
1212 the only characteristics of component types used are those
1213 characteristics visible at that place.
1214 If later @Chg{New=[immediately within the declarative region in which the
1215 composite type is declared],
1216 Old=[within the immediate scope of the composite type]} additional
1217 characteristics become visible for a component type,
1218 then any corresponding characteristics become visible for the composite
1219 type.
1220 Any additional predefined operators are implicitly declared at that
1221 place.@Chg{Version=[3],New=[ If there is no such place, then additional
1222 predefined operators are not declared at all, but they still exist.],Old=[]}
1223
1224 @begin{Reason}
1225 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0029-1]}
1226 @ChgAdded{Version=[3],Text=[We say that the predefined operators exist
1227 because they can emerge in some unusual generic instantiations. See
1228 @RefSecNum{Formal Types}.]}
1229 @end{Reason}
1230 @begin{Discussion}
1231 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0029-1]}
1232 @ChgAdded{Version=[3],Text=[The predefined operators for the underlying class
1233 of a type always exist, even if there is no visibility on that underlying
1234 class. This rule is simply about where (if ever) those operators are
1235 declared (and thus become usable). The @ldquote@;additional predefined
1236 operators@rdquote defined by this rule are any that are not declared at the
1237 point of the original type declaration. For instance, a type derived from a
1238 private type whose full type is type String always will have a ">"
1239 operator, but where that operator is declared (and thus whether it is
1240 visible) will depend on the visibility of the full type of the parent type.]}
1241 @end{Discussion}
1242
1243 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1244 The corresponding rule applies to a type defined by a
1245 @nt{derived_type_definition},
1246 if there is a place @Chg{New=[immediately within the declarative region in which
1247 the type is declared],
1248 Old=[within its immediate scope]} where additional
1249 characteristics of its parent type become visible.
1250
1251 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1252 @Defn{become nonlimited}
1253 @Defn2{Term=[nonlimited type],Sec=(becoming nonlimited)}
1254 @Defn2{Term=[limited type],Sec=(becoming nonlimited)}
1255 @Redundant[For example, an array type whose component type is limited
1256 private becomes nonlimited if the full view of the component type is
1257 nonlimited and visible at some later place @Chg{New=[immediately within the declarative region in which
1258 the array type is declared.],
1259 Old=[within the immediate scope of the array type.]}
1260 In such a case, the predefined "=" operator is implicitly declared at
1261 that place, and assignment is allowed after that place.]
1262
1263 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0115-1],ARef=[AI05-0269-1]}
1264 @ChgAdded{Version=[3],Text=[A type is a @i<descendant>@Defn2{Term=[descendant],
1265 Sec=[of the full view of a type]} of the full view of some ancestor
1266 of its parent type only if the current view it has of its parent is a
1267 descendant of the full view of that ancestor. More generally, at any given
1268 place, a type is descended from the same view of an ancestor as that from which
1269 the current view of its parent is descended. This view determines what
1270 characteristics are inherited from the ancestor@Redundant[, and, for example,
1271 whether the type is considered to be a descendant of a record type, or a
1272 descendant only through record extensions of a more distant ancestor].]}
1273
1274 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0115-1]}
1275 @ChgRef{Version=[4],Kind=[RevisedAdded],ARef=[AI12-0065-1]}
1276 @ChgAdded{Version=[3],Text=[@Redundant[@Chg{Version=[4],New=[Furthermore,
1277 it],Old=[It]} is possible for there to be places
1278 where a derived type is @Chg{Version=[4],New=[known to be derived
1279 indirectly from],Old=[visibly a descendant of]} an ancestor type, but
1280 @Chg{Version=[4],New=[is ],Old=[]}not a
1281 descendant of even a partial view of the ancestor type, because the parent
1282 of the derived type is not visibly a descendant of the ancestor. In
1283 this case, the derived type inherits no characteristics from that
1284 ancestor, but nevertheless is within the derivation class of the
1285 ancestor for the purposes of type conversion, the "covers"
1286 relationship, and matching against a formal derived type. In this
1287 case the derived type is @Chg{Version=[4],New=[effectively],Old=[considered
1288 to be]} a @i<descendant> of an incomplete view of the ancestor.@Chg{Version=[4],New=[],Old=[@Defn2{Term=[descendant],
1289 Sec=[of an incomplete view]}]}]]}
1290
1291 @begin{Discussion}
1292 @ChgRef{Version=[3],Kind=[AddedNormal]}
1293 @ChgAdded{Version=[3],Type=[Leading],Text=[Here is an example of this situation:]}
1294 @begin{Example}
1295 @ChgRef{Version=[3],Kind=[AddedNormal]}
1296 @ChgAdded{Version=[3],Text=[@key[package] P @key[is]
1297 @key[type] T @key[is] @key[private];
1298 C : @key[constant] T;
1299 @key[private]
1300 @key[type] T @key[is new] Integer;
1301 C : @key[constant] T := 42;
1302 @key[end] P;]}
1303
1304 @ChgRef{Version=[3],Kind=[AddedNormal]}
1305 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0065-1]}
1306 @ChgAdded{Version=[3],Text=[@key[with] P;
1307 @key[package] Q @key[is]
1308 @key[type] T2 @key[is new] P.T;@Chg{Version=[4],New=[ -- @Examcom{T2 is @b<not> a descendant of Integer}],Old=[]}
1309 @key[end] Q;]}
1310
1311 @ChgRef{Version=[3],Kind=[AddedNormal]}
1312 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0065-1]}
1313 @ChgAdded{Version=[3],Text=[@key[with] Q;
1314 @key[package] P.Child @key[is]
1315 @key[type] T3 @key[is new] Q.T2;
1316 @key[private]
1317 @Chg{Version=[4],New=[-- @Examcom{Here T3 is known to be indirectly derived from Integer, but inherits}
1318 -- @Examcom{@b<no> characteristics from Integer, since T2 inherits no characteristics}
1319 -- @Examcom{from Integer.}
1320 -- @Examcom{However, we allow an explicit conversion of T3 to/from Integer.}
1321 -- @Examcom{Hence, T3 is effectively a descendant of an "incomplete" view of Integer.}
1322 ],Old=[]}Int : Integer := 52;
1323 V : T3 := T3(P.C); -- @Examcom{Legal: conversion allowed}
1324 W : T3 := T3(Int); -- @Examcom{Legal: conversion allowed}
1325 X : T3 := T3(42); -- @Examcom{Error: T3 is not a numeric type}
1326 Y : T3 := X + 1; -- @Examcom{Error: no visible "+" operator}
1327 Z : T3 := T3(Integer(W) + 1); -- @Examcom{Legal: convert to Integer first}
1328 @key[end] P.Child;]}
1329 @end{Example}
1330 @end{Discussion}
1331
1332 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1333 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0029-1]}
1334 Inherited primitive subprograms follow a different rule.
1335 For a @nt{derived_type_definition},
1336 each inherited primitive subprogram
1337 is implicitly declared at the earliest place, if any,
1338 @Chg{New=[immediately within the declarative region in which],
1339 Old=[within the immediate scope of]} the @nt{type_declaration}@Chg{New=[ occurs],Old=[]},
1340 but after the @nt{type_declaration},
1341 where the corresponding declaration from the parent is
1342 visible.
1343 If there is no such place, then the inherited subprogram
1344 is not declared at all@Chg{Version=[3],New=[, but it still exists],Old=[]}.
1345 @Redundant[@Chg{Version=[3],New=[For a tagged type, it is possible to
1346 dispatch to an],Old=[An]} inherited subprogram that is not declared at
1347 all@Chg{Version=[3],New=[],Old=[cannot be named in a call and cannot be
1348 overridden, but for a tagged type, it is possible to dispatch to it]}.]
1349
1350 For a @nt{private_extension_declaration}, each inherited subprogram is
1351 declared immediately after the @nt{private_extension_declaration}
1352 if the corresponding declaration from the ancestor is visible at that
1353 place.
1354 Otherwise, the inherited subprogram is not declared for the private
1355 extension, @Redundant[though it might be for the full type].
1356 @begin{Reason}
1357 @ChgRef{Version=[1],Kind=[Revised]}
1358 There is no need for the @lquotes@;earliest place
1359 @Chg{New=[immediately within the declarative region],
1360 Old=[within the immediate scope]}@rquotes@;
1361 business here, because a @nt{private_extension_declaration} will be
1362 completed with a @nt{full_type_declaration}, so we can hang the
1363 necessary private implicit declarations on the @nt{full_type_declaration}.
1364 @end{Reason}
1365 @begin{Discussion}
1366 The above rules matter only when the component type (or parent type) is
1367 declared in the visible part of a package, and the composite type (or
1368 derived type) is declared within the declarative region of that package
1369 (possibly in a nested package or a child package).
1370
1371 @leading@keepnext@;Consider:
1372 @begin{Example}
1373 @key[package] Parent @key[is]
1374 @key[type] Root @key[is] @key[tagged] @key[null] @key[record];
1375 @key[procedure] Op1(X : Root);
1376
1377 @key[type] My_Int @key[is] @key[range] 1..10;
1378 @key[private]
1379 @key[procedure] Op2(X : Root);
1380
1381 @key[type] Another_Int @key[is] @key[new] My_Int;
1382 @key[procedure] Int_Op(X : My_Int);
1383 @key[end] Parent;
1384
1385 @key[with] Parent; @key[use] Parent;
1386 @key[package] Unrelated @key[is]
1387 @key[type] T2 @key[is] @key[new] Root @key[with] @key[null] @key[record];
1388 @key[procedure] Op2(X : T2);
1389 @key[end] Unrelated;
1390
1391 @key[package] Parent.Child @key[is]
1392 @key[type] T3 @key[is] @key[new] Root @key[with] @key[null] @key[record];
1393 --@RI{ Op1(T3) implicitly declared here.}
1394
1395 @key[package] Nested @key[is]
1396 @key[type] T4 @key[is] @key[new] Root @key[with] @key[null] @key[record];
1397 @key[private]
1398 ...
1399 @key[end] Nested;
1400 @key[private]
1401 --@RI{ Op2(T3) implicitly declared here.}
1402 ...
1403 @key[end] Parent.Child;
1404
1405 @key[with] Unrelated; @key[use] Unrelated;
1406 @key[package] @key[body] Parent.Child @key[is]
1407 @key[package] @key[body] Nested @key[is]
1408 --@RI{ Op2(T4) implicitly declared here.}
1409 @key[end] Nested;
1410
1411 @key[type] T5 @key[is] @key[new] T2 @key[with] @key[null] @key[record];
1412 @key[end] Parent.Child;
1413 @end{Example}
1414
1415 Another_Int does not inherit Int_Op,
1416 because Int_Op does not @lquotes@;exist@rquotes@; at the place
1417 where Another_Int is declared.
1418
1419 @ChgRef{Version=[1],Kind=[Revised]}
1420 Type T2 inherits Op1 and Op2 from Root.
1421 However, the inherited Op2 is never declared,
1422 because Parent.Op2 is never visible @Chg{New=[immediately within the declarative region],
1423 Old=[within the immediate scope]} of T2.
1424 T2 explicitly declares its own Op2,
1425 but this is unrelated to the inherited one @em it
1426 does not override the inherited one,
1427 and occupies a different slot in the type descriptor.
1428
1429 T3 inherits both Op1 and Op2. Op1 is implicitly declared immediately
1430 after the type declaration,
1431 whereas Op2 is declared at the beginning of the private part.
1432 Note that if Child were a private child of Parent,
1433 then Op1 and Op2 would both be implicitly declared immediately after the
1434 type declaration.
1435
1436 @ChgRef{Version=[1],Kind=[Revised]}
1437 T4 is similar to T3, except that the earliest place
1438 @Chg{New=[immediately within the declarative region containing T4],
1439 Old=[within T4's immediate scope]}
1440 where Root's Op2 is visible is in the body of Nested.
1441
1442 If T3 or T4 were to declare a type-conformant Op2,
1443 this would override the one inherited from Root.
1444 This is different from the situation with T2.
1445
1446
1447 T5 inherits Op1 and two Op2's from T2.
1448 Op1 is implicitly declared immediately after the declaration of T5,
1449 as is the Op2 that came from Unrelated.Op2.
1450 However, the Op2 that originally came from Parent.Op2 is never
1451 implicitly declared for T5,
1452 since T2's version of that Op2 is never visible (anywhere @em it never
1453 got declared either).
1454
1455 For all of these rules, implicit private parts and bodies are assumed as
1456 needed.
1457
1458 @leading@keepnext@;It is possible for characteristics of a type to be revealed
1459 in more than one place:
1460
1461 @begin{Example}
1462 @key[package] P @key[is]
1463 @key[type] Comp1 @key[is] @key[private];
1464 @key[private]
1465 @key[type] Comp1 @key[is] @key[new] Boolean;
1466 @key[end] P;
1467
1468 @key[package] P.Q @key[is]
1469 @key[package] R @key[is]
1470 @key[type] Comp2 @key[is] @key[limited] @key[private];
1471 @key[type] A @key[is] @key[array](Integer @key[range] <>) @key[of] Comp2;
1472 @key[private]
1473 @key[type] Comp2 @key[is] @key[new] Comp1;
1474 --@RI{ A becomes nonlimited here.}
1475 --@RI{ "="(A, A) return Boolean is implicitly declared here.}
1476 ...
1477 @key[end] R;
1478 @key[private]
1479 --@RI{ Now we find out what Comp1 really is, which reveals}
1480 --@RI{ more information about Comp2, but we're not within}
1481 --@RI{ the immediate scope of Comp2, so we don't do anything}
1482 --@RI{ about it yet.}
1483 @key[end] P.Q;
1484
1485 @key[package] @key[body] P.Q @key[is]
1486 @key[package] @key[body] R @key[is]
1487 --@RI{ Things like "@key[xor]"(A,A) return A are implicitly}
1488 --@RI{ declared here.}
1489 @key[end] R;
1490 @key[end] P.Q;
1491 @end{Example}
1492
1493 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0019],ARef=[AI95-00033-01]}
1494 @ChgAdded{Version=[1],Type=[Leading],Text=[We say @i<immediately> within
1495 the declarative region in order that
1496 types do not gain operations within a nested scope. Consider:]}
1497 @begin{Example}
1498 @ChgRef{Version=[1],Kind=[Added]}
1499 @Chg{New=[@Key[package] Outer @key[is]
1500 @key[package] Inner @key[is]
1501 @key[type] Inner_Type @key[is] @key[private];
1502 @key[private]
1503 @key[type] Inner_Type @key[is] @key[new] Boolean;
1504 @key[end] Inner;
1505 @key[type] Outer_Type @key[is] @key[array](Natural @key[range] <>) @key[of] Inner.Inner_Type;
1506 @key[end] Outer;],Old=[]}
1507
1508 @ChgRef{Version=[1],Kind=[Added]}
1509 @Chg{New=[@key[package] @key[body] Outer @key[is]
1510 @key[package] @key[body] Inner @key[is]
1511 -- At this point, we can see that Inner_Type is a Boolean type.
1512 -- But we don't want Outer_Type to gain an "and" operator here.
1513 @key[end] Inner;
1514 @key[end] Outer;],Old=[]}
1515 @end{Example}
1516 @end{Discussion}
1517
1518 @leading@Redundant[The Class attribute is defined for tagged subtypes in
1519 @RefSecNum{Tagged Types and Type Extensions}.
1520 In addition,] for
1521 @PrefixType{every subtype S of an untagged private type
1522 whose full view is tagged},
1523 the following attribute is defined:
1524 @begin(description)
1525 @Attribute{Prefix=<S>, AttrName=<Class>,
1526 Text=<Denotes the class-wide subtype corresponding to the full
1527 view of S.
1528 This attribute is allowed only from the beginning of the private part
1529 in which the full view is declared, until the declaration of the full
1530 view.
1531 @Redundant[After the full view,
1532 the Class attribute of the full view can be used.]>}
1533 @end(description)
1534 @EndPrefixType{}
1535 @end{StaticSem}
1536
1537 @begin{Notes}
1538 Because a partial view and a full view
1539 are two different views of one and the same type,
1540 outside of the defining package the characteristics of the type are
1541 those defined by the visible part.
1542 Within these outside program units the type is just a private type
1543 or private extension,
1544 and any language rule that applies only to another class of types
1545 does not apply. The fact that the full declaration might implement
1546 a private type with a type of a particular class (for example, as
1547 an array type) is relevant only
1548 within the declarative region of the package itself
1549 including any child units.
1550
1551 @NoPrefix@;The consequences of this actual implementation are, however, valid
1552 everywhere. For example: any default initialization of components
1553 takes place; the attribute Size provides the size of the full view;
1554 finalization is still done for controlled components of the full view;
1555 task dependence rules still apply to components that are task
1556 objects.
1557
1558 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
1559 Partial views provide @Chg{Version=[2],New=[initialization],
1560 Old=[assignment (unless the view is limited)]},
1561 membership tests, selected components for the selection of
1562 discriminants and inherited components, qualification,
1563 and explicit conversion.@Chg{Version=[2],New=[ Nonlimited partial views
1564 also allow use of @nt{assignment_statement}s.],Old=[]}
1565
1566 For a subtype S of a partial view, S'Size is defined
1567 (see @RefSecNum{Operational and Representation Attributes}).
1568 For an object A of a partial view,
1569 the attributes A'Size and A'Address are defined
1570 (see @RefSecNum{Operational and Representation Attributes}).
1571 The Position, First_Bit, and Last_Bit attributes
1572 are also defined for discriminants and inherited components.
1573 @end{Notes}
1574
1575 @begin{Examples}
1576 @leading@keepnext@i{Example of a type with private operations:}
1577 @begin{Example}
1578 @key[package] Key_Manager @key[is]
1579 @key[type] Key @key[is] @key[private];
1580 Null_Key : @key[constant] Key; --@RI{ a deferred constant declaration (see @RefSecNum{Deferred Constants})}
1581 @key[procedure] Get_Key(K : @key[out] Key);
1582 @key[function] "<" (X, Y : Key) @key[return] Boolean;
1583 @key[private]
1584 @key[type] Key @key[is] @key[new] Natural;
1585 Null_Key : @key[constant] Key := Key'First;
1586 @key[end] Key_Manager;
1587
1588 @NotISORMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
1589 @key[package] @key[body] Key_Manager @key[is]
1590 Last_Key : Key := Null_Key;
1591 @key[procedure] Get_Key(K : @key[out] Key) @key[is]
1592 @key[begin]
1593 Last_Key := Last_Key + 1;
1594 K := Last_Key;
1595 @key[end] Get_Key;
1596
1597 @key[function] "<" (X, Y : Key) @key[return] Boolean @key[is]
1598 @key[begin]
1599 @key[return] Natural(X) < Natural(Y);
1600 @key[end] "<";
1601 @key[end] Key_Manager;
1602 @end{Example}
1603 @end{Examples}
1604
1605 @begin{Notes}
1606 @i{Notes on the example:}
1607 Outside of the package Key_Manager, the operations available for
1608 objects of type Key include assignment, the comparison for equality
1609 or inequality, the procedure Get_Key and the operator "<"; they do
1610 not include other relational operators such as ">=", or arithmetic
1611 operators.
1612
1613 @NoPrefix@;The explicitly declared operator "<" hides the predefined operator
1614 "<" implicitly declared by the @nt{full_type_declaration}. Within the
1615 body of the function, an explicit conversion of X and Y to the
1616 subtype Natural is necessary to invoke the "<" operator of the parent
1617 type.
1618 Alternatively, the result of the function could be written as not (X
1619 >= Y), since the operator ">=" is not redefined.
1620
1621 @NoPrefix@;The value of the variable Last_Key, declared in the package body,
1622 remains unchanged between calls of the procedure Get_Key. (See also
1623 the NOTES of @RefSecNum{Package Bodies}.)
1624 @end{Notes}
1625
1626 @begin{DiffWord83}
1627 The phrase in RM83-7.4.2(7), @lquotes@;...after the full type declaration@rquotes@;,
1628 doesn't work in the presence of child units, so we define that rule in
1629 terms of visibility.
1630
1631 The definition of the Constrained attribute for private types
1632 has been moved to @lquotes@;Obsolescent Features.@rquotes@;
1633 (The Constrained attribute of an object has not been moved there.)
1634 @end{DiffWord83}
1635
1636 @begin{DiffWord95}
1637 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0018],ARef=[AI95-00033-01]}
1638 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified when additional
1639 operations are declared.]}
1640
1641 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
1642 @ChgAdded{Version=[2],Text=[Revised the note on operations of partial views
1643 to reflect that limited types do have an assignment operation, but not
1644 @nt{assignment_statement}s.]}
1645 @end{DiffWord95}
1646
1647 @begin{DiffWord2005}
1648 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0029-1]}
1649 @ChgAdded{Version=[3],Text=[@b<Correction:> Revised the wording to say
1650 that predefined operations still exist even if they are never declared,
1651 because it is possible to reference them in a generic unit.]}
1652
1653 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0115-1]}
1654 @ChgAdded{Version=[3],Text=[@b<Correction:> Clarified that the characteristics
1655 of a descendant of a private type depend on the visibility of the full
1656 view of the direct ancestor. This has to be the case (so that privacy is not
1657 violated), but it wasn't spelled out in earlier versions of Ada.]}
1658 @end{DiffWord2005}
1659
1660 @begin{DiffWord2012}
1661 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0065-1]}
1662 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Clarified the clarification added
1663 by AI05-0115-1, as it turned out to not be that clear. Hopefully this version
1664 is better.]}
1665 @end{DiffWord2012}
1666
1667
1668 @LabeledAddedSubClause{Version=[3],Name=[Type Invariants]}
1669
1670 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1671 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0041-1]}
1672 @ChgAdded{Version=[3],Type=[Leading],Text=[For a private type@Chg{Version=[4],New=[,],Old=[ or]} private
1673 extension@Chg{Version=[4],New=[, or interface],Old=[]}, the following
1674 language-defined aspects may be specified with an
1675 @nt{aspect_specification} (see @RefSecNum{Aspect Specifications}):]}
1676
1677 @begin{Description}
1678 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0250-1]}
1679 @ChgAdded{Version=[3],Text=[Type_Invariant@\This aspect
1680 shall be specified by an @nt{expression}, called an @i<invariant
1681 expression>.@Defn{invariant expression}
1682 Type_Invariant may be specified on a @nt{private_@!type_@!declaration}, on a
1683 @nt{private_@!extension_@!declaration}, or on a @nt{full_@!type_@!declaration} that
1684 declares the completion of a private type or private
1685 extension.@AspectDefn{Type_Invariant}]}
1686
1687 @ChgAspectDesc{Version=[3],Kind=[AddedNormal],Aspect=[Type_Invariant],
1688 Text=[@ChgAdded{Version=[3],Text=[A condition that must hold true for all
1689 objects of a type.]}]}
1690
1691 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1692 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0041-1],ARef=[AI12-0150-1]}
1693 @ChgAdded{Version=[3],Text=[Type_Invariant'Class@\This aspect
1694 shall be specified by an @nt{expression}, called an @i<invariant
1695 expression>.
1696 Type_Invariant'Class may be specified on a
1697 @nt{private_@!type_@!declaration}@Chg{Version=[4],New=[,],Old=[ or]} a
1698 @nt{private_@!extension_@!declaration}@Chg{Version=[4],New=[, or a
1699 @nt{full_@!type_@!declaration} for an
1700 interface type],Old=[]}.@AspectDefn{Type_Invariant'Class}@Chg{Version=[4],New=[
1701 Type_Invariant'Class determines a @i{class-wide type invariant}
1702 for a tagged type.@Defn{class-wide type invariant}@Defn2{Term=[type invariant],
1703 Sec=[class-wide]}@Defn2{Term=[invariant],Sec=[class-wide]}],Old=[]}]}
1704
1705 @begin{Reason}
1706 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0254-1]}
1707 @ChgAdded{Version=[3],Text=[A class-wide type invariant cannot be hidden
1708 in the private part, as the creator of an extension needs to know about it
1709 in order to conform to it in any new or overriding operations. On the other
1710 hand, a specific type invariant is not inherited, so that no operation
1711 outside of the original package needs to conform to it; thus there is no
1712 need for it to be visible.]}
1713 @end{Reason}
1714
1715 @ChgAspectDesc{Version=[3],Kind=[AddedNormal],Aspect=[Type_Invariant'Class],
1716 Text=[@ChgAdded{Version=[3],Text=[A condition that must hold true for all
1717 objects in a class of types.]}]}
1718
1719 @end{Description}
1720
1721 @begin{Resolution}
1722 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1723 @ChgAdded{Version=[3],Text=[The expected type for an invariant expression
1724 is any boolean type.@PDefn2{Term=[expected type],
1725 Sec=(invariant expression)}]}
1726
1727 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1728 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0150-1],ARef=[AI12-0159-1]}
1729 @ChgAdded{Version=[3],Text=[@Redundant[Within an invariant expression, the
1730 identifier of the first subtype of the associated type denotes the current
1731 instance of the type.] Within an invariant expression
1732 @Chg{Version=[4],New=[for the Type_Invariant aspect of a],Old=[associated
1733 with]} type @i<T>, the type of @Chg{Version=[4],New=[this],Old=[the]}
1734 current instance is @i<T>@Chg{Version=[4],New=[. Within an invariant
1735 expression],Old=[for the Type_Invariant aspect and @i<T>'Class]}
1736 for the Type_Invariant'Class aspect@Chg{Version=[4],New=[ of a
1737 type @i<T>, the type of this current instance is interpreted as though it
1738 had a (notional) type @i<NT> that is a visible formal derived type whose
1739 ancestor type is @i<T>.@Redundant[ The effect of this
1740 interpretation is that the only operations that can be applied to this
1741 current instance are those defined for such a formal derived type.]],Old=[.]}]}
1742
1743 @begin{TheProof}
1744 @ChgRef{Version=[3],Kind=[AddedNormal]}
1745 @ChgAdded{Version=[3],Text=[The first sentence is given formally in
1746 @RefSecNum{Aspect Specifications}.]}
1747 @end{TheProof}
1748
1749 @begin{Reason}
1750 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0159-1]}
1751 @ChgAdded{Version=[4],Text=[The rules for Type_Invariant'Class ensure
1752 that the invariant expression is well-defined for any type descended
1753 from @i<T>.]}
1754 @end{Reason}
1755
1756 @end{Resolution}
1757
1758 @begin{Legality}
1759
1760 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1761 @ChgAdded{Version=[3],Text=[@Redundant[The Type_Invariant'Class aspect shall not be
1762 specified for an untagged type.] The Type_Invariant aspect shall not be specified
1763 for an abstract type.]}
1764
1765 @begin{TheProof}
1766 @ChgRef{Version=[3],Kind=[AddedNormal]}
1767 @ChgAdded{Version=[3],Text=[The first sentence is given formally in
1768 @RefSecNum{Aspect Specifications}.]}
1769 @end{TheProof}
1770
1771 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0042-1]}
1772 @ChgAdded{Version=[4],Text=[If a type extension occurs at a point where a
1773 private operation of some ancestor is visible and inherited, and a
1774 Type_Invariant'Class expression applies to that ancestor, then the inherited
1775 operation shall be abstract or shall be overridden.]}
1776 @end{Legality}
1777
1778 @begin{StaticSem}
1779
1780 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0250-1]}
1781 @ChgAdded{Version=[3],Text=[@Redundant[If the Type_Invariant aspect is
1782 specified for a type @i<T>, then the invariant expression applies to @i<T>.]]}
1783
1784 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1]}
1785 @ChgAdded{Version=[3],Text=[@Redundant[If the Type_Invariant'Class aspect is
1786 specified for a tagged type @i<T>, then the invariant expression applies to all
1787 descendants of @i<T>.]]}
1788
1789 @begin{TheProof}
1790 @ChgRef{Version=[3],Kind=[AddedNormal]}
1791 @ChgAdded{Version=[3],Text=["Applies" is formally defined in
1792 @RefSecNum{Aspect Specifications}.]}
1793 @end{TheProof}
1794 @end{StaticSem}
1795 @begin{Runtime}
1796
1797 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0247-1],ARef=[AI05-0290-1]}
1798 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0150-1]}
1799 @ChgAdded{Version=[3],Type=[Leading],Text=[If one or more invariant expressions
1800 apply to a @Chg{Version=[4],New=[nonabstract ],Old=[]}type @i<T>, then an
1801 invariant check is performed at the following places,
1802 on the specified object(s):@Defn{invariant check}@Defn2{Term=[check, language-defined],
1803 Sec=[controlled by assertion policy]}]}
1804
1805 @begin{Itemize}
1806 @ChgRef{Version=[3],Kind=[AddedNormal]}
1807 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0133-1]}
1808 @ChgAdded{Version=[3],Text=[After successful
1809 @Chg{Version=[4],New=[],Old=[default ]}initialization of an
1810 object of type @i<T>@Chg{Version=[4],New=[ by default (see
1811 @RefSecNum{Object Declarations})],Old=[]}, the check is performed on the
1812 new object@Chg{Version=[4],New=[ unless the partial
1813 view of @i<T> has unknown discriminants],Old=[]};]}
1814
1815 @begin{Reason}
1816 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0133-1]}
1817 @ChgAdded{Version=[4],Text=[The check applies everywhere, even in the
1818 package body, because default initialization has to work the same for
1819 clients as it does within the package. As such, checks within the package
1820 are either harmless or will uncover a bug that could also happen to a
1821 client. However, if the partial view of the type has unknown discriminants,
1822 no client of the package can declare a default-initialized object.
1823 Therefore, no invariant check is needed, as all default initialized objects
1824 are necessarily inside the package.]}
1825 @end{Reason}
1826
1827 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0049-1]}
1828 @ChgAdded{Version=[4],Text=[After successful explicit initialization of the
1829 completion of a deferred constant with a part of type @i<T>, if the completion
1830 is inside the immediate scope of the full view of @i<T>, and the deferred
1831 constant is visible outside the immediate scope of @i<T>, the check is
1832 performed on the part(s) of type @i<T>;]}
1833
1834 @ChgRef{Version=[3],Kind=[AddedNormal]}
1835 @ChgAdded{Version=[3],Text=[After successful conversion to type @i<T>, the
1836 check is performed on the result of the conversion;]}
1837
1838 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0269-1]}
1839 @ChgAdded{Version=[3],Text=[For a view conversion, outside the immediate scope
1840 of @i<T>, that converts from a descendant of @i<T> (including @i<T> itself) to
1841 an ancestor of type @i<T> (other than @i<T> itself), a check is performed on
1842 the part of the object that is of type @i<T>:]}
1843
1844 @begin{InnerItemize}
1845 @ChgRef{Version=[3],Kind=[AddedNormal]}
1846 @ChgAdded{Version=[3],Text=[after assigning to the view conversion; and]}
1847
1848 @ChgRef{Version=[3],Kind=[AddedNormal]}
1849 @ChgAdded{Version=[3],Text=[after successful return from a call that passes the view conversion
1850 as an @key[in out] or @key[out] parameter.]}
1851 @end{InnerItemize}
1852
1853 @begin{Ramification}
1854 @ChgRef{Version=[3],Kind=[AddedNormal]}
1855 @ChgAdded{Version=[3],Text=[For a single view conversion that converts
1856 between distantly related types, this rule could be triggered for
1857 multiple types and thus multiple invariant checks may be needed.]}
1858 @end{Ramification}
1859 @begin{ImplNote}
1860 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0299-1]}
1861 @ChgAdded{Version=[3],Text=[For calls to inherited subprograms (including
1862 dispatching calls), the implied view conversions mean that a wrapper is
1863 probably needed. (See the Note at the bottom of this subclause for more
1864 on the model of checks for inherited subprograms.)]}
1865
1866 @ChgRef{Version=[3],Kind=[AddedNormal]}
1867 @ChgAdded{Version=[3],Text=[For view conversions involving class-wide
1868 types, the exact checks needed may not be known at compile-time. One
1869 way to deal with this is to have an implicit dispatching operation
1870 that is given the object to check and the tag of the target of the
1871 conversion, and which first checks if the passed tag is not for itself,
1872 and if not, checks the its invariant on the object and then calls
1873 the operation of its parent type. If the tag is for itself, the
1874 operation is complete.]}
1875 @end{ImplNote}
1876
1877 @ChgRef{Version=[3],Kind=[AddedNormal]}
1878 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0146-1]}
1879 @ChgAdded{Version=[3],Text=[After a successful call on the Read or Input
1880 @Chg{Version=[4],New=[stream-oriented],Old=[stream]} attribute of the
1881 type @i<T>, the check is performed on the object
1882 initialized by the @Chg{Version=[4],New=[],Old=[stream ]}attribute;]}
1883
1884 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0269-1]}
1885 @ChgAdded{Version=[3],Type=[Leading],Text=[An invariant is checked upon successful return from a call
1886 on any subprogram or entry that:]}
1887 @begin{Itemize}
1888 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0269-1]}
1889 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0042-1]}
1890 @ChgAdded{Version=[3],Text=[is declared within the immediate
1891 scope of type @i<T> (or by an instance of a generic unit, and the generic
1892 is declared within the immediate scope of type @i<T>),@Chg{Version=[4],New=[],Old=[ and]}]}
1893
1894 @ChgRef{Version=[3],Kind=[AddedNormal]}
1895 @ChgRef{Version=[4],Kind=[Deleted],ARef=[AI12-0042-1]}
1896 @ChgAdded{Version=[3],Text=[@Chg{Version=[4],New=[],Old=[is visible outside the
1897 immediate scope of type @i<T> or overrides an operation that is visible outside
1898 the immediate scope of @i<T>, and]}]}
1899
1900 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0289-1]}
1901 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0042-1],ARef=[AI12-0044-1]}
1902 @ChgAdded{Version=[4],Type=[Leading],Text=[]}@ChgNote{To get conditional Leading}
1903 @Chg{Version=[3],New=[@Chg{Version=[4],New=[and either:],
1904 Old=[has a result with a part of type @i<T>, or one or more parameters
1905 with a part of type @i<T>, or an access to variable
1906 parameter whose designated type has a part of type @i<T>.]}],Old=[]}
1907
1908 @begin{Itemize}
1909 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0044-1]}
1910 @ChgAdded{Version=[4],Text=[has a result with a part of type @i<T>, or]}
1911
1912 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0044-1]}
1913 @ChgAdded{Version=[4],Text=[has one or more @key[out] or @key[in out]
1914 parameters with a part of type @i<T>, or]}
1915
1916 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0044-1],ARef=[AI12-0149-1]}
1917 @ChgAdded{Version=[4],Text=[has an access-to-object parameter or result
1918 whose designated type has a part of type @i<T>, or]}
1919
1920 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0042-1],ARef=[AI12-0044-1]}
1921 @ChgAdded{Version=[4],Text=[is a procedure or entry that has an @key[in]
1922 parameter with a part of type @i<T>,]}
1923
1924 @begin{Discussion}
1925 @ChgRef{Version=[4],Kind=[AddedNormal]}
1926 @ChgAdded{Version=[4],Text=[We don't check @key[in] parameters for
1927 functions to avoid infinite recursion for calls to public functions
1928 appearing in invariant expressions. Such function calls are unavoidable
1929 for class-wide invariants and likely for other invariants. This is the
1930 simplest rule that avoids trouble, and functions are much more likely to
1931 be queries that don't modify their parameters than other callable
1932 entities.]}
1933 @end{Discussion}
1934 @end{Itemize}
1935
1936 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0042-1]}
1937 @ChgAdded{Version=[4],Type=[Leading],Text=[and either:]}
1938
1939 @begin{Itemize}
1940 @ChgRef{Version=[4],Kind=[Added]}
1941 @ChgAdded{Version=[4],Text=[@i<T> is a private type or a private extension
1942 and the subprogram or entry is visible outside the immediate scope of
1943 type @i<T> or overrides an inherited operation that is visible outside
1944 the immediate scope of @i<T>, or]}
1945
1946 @ChgRef{Version=[4],Kind=[Added]}
1947 @ChgAdded{Version=[4],Text=[@i<T> is a record extension, and the
1948 subprogram or entry is a primitive operation visible outside the
1949 immediate scope of type @i<T> or overrides an inherited operation that
1950 is visible outside the immediate scope of @i<T>.]}
1951 @end{Itemize}
1952 @end{Itemize}
1953 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0269-1]}
1954 @ChgAdded{Version=[3],NoPrefix=[T],Text=[The check is performed on each such
1955 part of type @i<T>.]}
1956
1957 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0042-1]}
1958 @ChgAdded{Version=[4],Text=[For a view conversion to a class-wide type
1959 occurring within the immediate scope of @i<T>, from a specific type that is
1960 a descendant of @i<T> (including @i<T> itself), a check is performed
1961 on the part of the object that is of type @i<T>.]}
1962
1963 @begin{Reason}
1964 @ChgRef{Version=[4],Kind=[AddedNormal]}
1965 @ChgAdded{Version=[4],Text=[Class-wide objects are treated as though they
1966 exist outside the scope of every type, and may be passed across package
1967 "boundaries" freely without further invariant checks.]}
1968 @end{Reason}
1969
1970 @end{Itemize}
1971
1972 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0290-1]}
1973 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0080-1],ARef=[AI12-0159-1]}
1974 @ChgAdded{Version=[3],Text=[If performing checks is required by the
1975 @Chg{Version=[4],New=[Type_Invariant],Old=[Invariant]} or
1976 @Chg{Version=[4],New=[Type_Invariant'Class],Old=[Invariant'Class]} assertion
1977 policies (see
1978 @RefSecNum{Pragmas Assert and Assertion_Policy}) in effect at the point of
1979 @Chg{Version=[4],New=[the ],Old=[]}corresponding aspect specification
1980 applicable to a given type, then the respective invariant expression is
1981 considered @i(enabled).@Defn2{Term=[enabled],Sec=[invariant expression]}]}
1982
1983 @begin{Ramification}
1984 @ChgRef{Version=[3],Kind=[AddedNormal]}
1985 @ChgAdded{Version=[3],Text=[If a class-wide invariant expression is enabled
1986 for a type, it remains enabled when inherited by descendants of that type,
1987 even if the policy in effect is Ignore for the inheriting type.]}
1988 @end{Ramification}
1989
1990 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0250-1],ARef=[AI05-0289-1],ARef=[AI05-0290-1]}
1991 @ChgAdded{Version=[3],Text=[The invariant check consists of the evaluation of
1992 each enabled invariant expression that applies to @i<T>, on each of the objects
1993 specified above. If any of these evaluate to False,
1994 Assertions.Assertion_Error is raised at the point of the object
1995 initialization, conversion, or call.@Defn2{Term=(Assertion_Error),
1996 Sec=(raised by failure of run-time check)} If a given call requires more than one
1997 evaluation of an invariant expression, either for multiple objects of a single
1998 type or for multiple types with invariants, the evaluations are performed in
1999 an arbitrary order, and if one of them evaluates to False, it is not specified whether
2000 the others are evaluated. Any invariant check is performed prior to copying back
2001 any by-copy @key[in out] or @key[out] parameters. Invariant checks,
2002 any postcondition check, and any
2003 constraint or predicate checks associated with @key[in out] or @key[out]
2004 parameters are performed in an arbitrary order.]}
2005
2006 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0150-1],ARef=[AI12-0159-1]}
2007 @ChgAdded{Version=[4],Text=[For an invariant check on a value of type @i<T1>
2008 based on a class-wide invariant expression inherited from an ancestor type
2009 @i<T>, any operations within the invariant expression that were resolved as
2010 primitive operations of the (notional) formal derived type @i<NT> are
2011 bound to the corresponding operations of type @i<T1> in the
2012 evaluation of the invariant expression for the check on @i<T1>.]}
2013
2014 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0247-1],ARef=[AI05-0250-1]}
2015 @ChgAdded{Version=[3],Text=[The invariant checks performed on a call are
2016 determined by the subprogram or entry actually invoked, whether directly, as
2017 part of a dispatching call, or as part of a call through an access-to-subprogram
2018 value.]}
2019
2020 @begin{Ramification}
2021 @ChgRef{Version=[3],Kind=[AddedNormal]}
2022 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0149-1]}
2023 @ChgAdded{Version=[3],Text=[Invariant checks on subprogram return are not
2024 performed on objects that are accessible only through access
2025 values@Chg{Version=[4],New=[ that are subcomponents of some other object],Old=[]}.
2026 It is also possible to call through an access-to-subprogram value and reach a
2027 subprogram body that has visibility on the full declaration of a type, from
2028 outside the immediate scope of the type. No invariant checks will be performed
2029 if the designated subprogram is not itself externally visible. These cases
2030 represent "holes" in the protection provided by invariant checks; but note
2031 that these holes cannot be caused by clients of the type @i<T> with the
2032 invariant@Chg{Version=[4],New=[. The designer of the package has to declare
2033 a visible type with an access-to-@i<T> subcomponent and use it as a parameter
2034 or result to subprograms in the package, or pass the client an
2035 access-to-subprogram value representing a private operation of the package.
2036 In the absence of such things, all values that the client can see will be
2037 checked for a private type or extension],Old=[ without help for the
2038 designer of the package containing @i<T>]}.]}
2039 @end{Ramification}
2040
2041 @begin{ImplNote}
2042 @ChgRef{Version=[3],Kind=[AddedNormal]}
2043 @ChgAdded{Version=[3],Text=[The implementation might want to produce a warning
2044 if a private extension has an ancestor type that is a visible extension, and
2045 an invariant expression depends on the value of one of the components from a
2046 visible extension part.]}
2047 @end{ImplNote}
2048
2049 @end{Runtime}
2050
2051 @begin{Notes}
2052 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0250-1],ARef=[AI05-0269-1]}
2053 @ChgAdded{Version=[3],Text=[For a call of a primitive subprogram of type
2054 @i<NT> that is inherited from type @i<T>, the specified checks of the specific
2055 invariants of both the types @i<NT> and @i<T> are performed. For a call of a
2056 primitive subprogram of type @i<NT> that is overridden for type @i<NT>, the
2057 specified checks of the specific invariants of only type @i<NT> are
2058 performed.]}
2059
2060 @begin{TheProof}
2061 @ChgRef{Version=[3],Kind=[AddedNormal]}
2062 @ChgAdded{Version=[3],Text=[This follows from the definition of a call on
2063 an inherited subprogram as view conversions of the parameters of the type
2064 and a call to the original subprogram
2065 (see @RefSecNum{Derived Types and Classes}), along with the normal invariant
2066 checking rules. In particular, the call to the original subprogram takes
2067 care of any checks needed on type @i<T>, and the checks required
2068 on view conversions take care of any checks needed on type @i<NT>,
2069 specifically on @key[in out] and @key[out] parameters. We require this in
2070 order that the semantics of an explicitly defined wrapper that does nothing
2071 but call the original subprogram is the same as that of an inherited
2072 subprogram.]}
2073 @end{TheProof}
2074
2075 @end{Notes}
2076
2077 @begin{Extend2005}
2078 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0146-1],ARef=[AI05-0247-1],ARef=[AI05-0250-1],ARef=[AI05-0289-1]}
2079 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
2080 Type_Invariant aspects are new.]}
2081 @end{Extend2005}
2082
2083 @begin{Inconsistent2012}
2084 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0042-1]}
2085 @ChgAdded{Version=[4],Text=[@Defn{inconsistencies with Ada 2012}
2086 @b<Corrigendum:> Clarified the definition of when invariant checks occur
2087 for inherited subprograms. This might cause checks to be added or removed
2088 in some cases. These are all rare cases involving class-wide type
2089 invariants and either record extensions or multiple levels of derivation.
2090 Additionally, implementations probably make the checks as the intent seems
2091 clear, even though the formal language did not include them. So we do not
2092 expect this to be a problem in practice.]}
2093
2094 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0042-1]}
2095 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added invariant checks for
2096 conversions to class-wide types. This might cause an invariant check to
2097 fail in some cases where they would not be made in the original definition
2098 of Ada 2012. Such cases represent a hole where a value that fails an
2099 invariant could "leak out" of a package, and as such will detect far more
2100 bugs than it causes.]}
2101
2102 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0044-1]}
2103 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Removed the invariant check
2104 for @key[in] parameters of
2105 functions, so that typical invariants don't cause infinite recursion.
2106 This is strictly inconsistent, as the Ada 2012 definition has this check;
2107 therefore, programs could depend on Assertion_Error being raised upon the
2108 return from some call on a public function. However, as the intent of
2109 assertion checking is to uncover bugs, a program that depends on a bug
2110 occurring seems very unlikely.]}
2111
2112 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0049-1],ARef=[AI12-0149-1]}
2113 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added an invariant check for
2114 deferred constants and for access values returned from functions, so they
2115 cannot be used to @ldquote@;leak@rdquote values that violate the invariant
2116 from a package. This is strictly inconsistent, as the Ada 2012 definition
2117 is missing these checks; therefore, programs could depend on using values
2118 that violate an invariant outside of the package of definition. These will
2119 not raise Assertion_Error in Ada 2012 as defined in the Ada 2012 Standard,
2120 but ought to do so (as noted by this change). As these are a violation of
2121 the intent of invariants, we think that this change will mainly reveal bugs
2122 rather than cause them.]}
2123
2124 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0150-1],ARef=[AI12-0159-1]}
2125 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Eliminated unintentional
2126 redispatching from class-wide type invariants. This means that a different
2127 body might be evaluated for a type invariant check where the value
2128 has a different tag than that of the type. The change means that the behavior
2129 of Type_Invariant and Type_Invariant'Class will be the same for a particular
2130 subprogram, and that the known behavior of the operations can be assumed.
2131 We expect that this change will primarily fix bugs, as it will make
2132 class-wide type invariants work more like expected. In the case where
2133 redispatching is desired, an explicit conversion to a class-wide type can be
2134 used.]}
2135 @end{Inconsistent2012}
2136
2137 @begin{Incompatible2012}
2138 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0042-1]}
2139 @ChgAdded{Version=[4],Text=[@Defn{incompatiblities with Ada 2012}
2140 @b<Corrigendum:> A private operation that is inherited in the visible
2141 part of a package to which a class-wide invariant applies now requires
2142 overriding. This is a very unlikely situation, and will prevent problems
2143 with invariant checks being added to routines that assume that they don't
2144 need them.]}
2145 @end{Incompatible2012}
2146
2147 @begin{Extend2012}
2148 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0041-1]}
2149 @ChgAdded{Version=[4],Text=[@Defn{extensions to Ada 2012}
2150 @b<Corrigendum:> Class-wide type invariants can now be specified on
2151 interfaces as well as private types.]}
2152 @end{Extend2012}
2153
2154 @begin{DiffWord2012}
2155 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0133-1]}
2156 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Clarified that all objects
2157 that are initialized by default should have an invariant check, and
2158 added an exception for types with unknown discriminants, as in that
2159 case the client cannot declare a default-initialized object. This
2160 exception to the check is formally inconsistent, but since it is only
2161 removing an assertion failure that occurs where no assertion should be
2162 checked anyway (meaning it's more likely to fix a bug than cause one),
2163 and programs depending on assertion failure should be very rare outside of
2164 test cases, we don't document this as inconsistent.]}
2165 @end{DiffWord2012}
2166
2167
2168 @NotISORMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
2169 @LabeledClause{Deferred Constants}
2170
2171 @begin{Intro}
2172 @redundant[Deferred constant declarations may be used to declare constants
2173 in the visible part of a package,
2174 but with the value of the constant given in the private part.
2175 They may also be used to declare constants imported from other
2176 languages (see @RefSecNum{Interface to Other Languages}).]
2177 @end{Intro}
2178
2179 @begin{Legality}
2180 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1],ARef=[AI05-0269-1]}
2181 @Redundant[@Defn{deferred constant declaration}
2182 A @i(deferred constant declaration) is an @nt<object_declaration>
2183 with the reserved word @key(constant) but no initialization expression.]
2184 @Defn{deferred constant}
2185 The constant declared by a deferred constant declaration is called
2186 a @i{deferred constant}.
2187 @PDefn2{Term=[requires a completion], Sec=(deferred constant declaration)}
2188 @Chg{Version=[3],New=[@Redundant[Unless the Import aspect
2189 (see @RefSecNum{Interfacing Aspects}) is True for a deferred constant
2190 declaration, the ]],Old=[A]} deferred constant declaration requires a completion,
2191 which shall be a full constant declaration
2192 (called the @i{full declaration} of the deferred
2193 constant)@Chg{Version=[3],New=[],Old=[,
2194 or a @nt{pragma} Import (see @RefSecNum(Interface to Other Languages))]}.
2195 @Defn{full declaration}
2196 @begin{TheProof}
2197 The first sentence is redundant, as it is stated officially in
2198 @RefSecNum(Object Declarations).
2199
2200 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0229-1],ARef=[AI05-0269-1]}
2201 @ChgAdded{Version=[3],Text=[The first part of the last sentence is redundant,
2202 as no imported entity may have a completion, as stated in
2203 @RefSecNum{Interfacing Aspects}.]}
2204 @end{TheProof}
2205
2206 @leading@;A deferred constant declaration that is completed
2207 by a full constant declaration shall occur immediately
2208 within the visible part of a @nt<package_specification>.
2209 For this case, the following additional rules apply to the
2210 corresponding full declaration:
2211 @begin(itemize)
2212 The full declaration shall occur immediately
2213 within the private part of the same package;
2214
2215 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00385-01]}
2216 The deferred and full constants shall have the same type@Chg{Version=[2],
2217 New=[, or shall have statically matching anonymous access subtypes],Old=[]};
2218 @begin{Ramification}
2219 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00385-01]}
2220 This implies that
2221 both the deferred declaration and the full declaration
2222 have to have a @nt<subtype_indication>@Chg{Version=[2],New=[ or
2223 @nt{access_definition}],Old=[]} rather than an
2224 @nt<array_type_definition>, because each @nt{array_type_definition}
2225 would define a new type.
2226 @end{Ramification}
2227
2228 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00385-01]}
2229 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0062-1],ARef=[AI05-0262-1]}
2230 If the @Chg{Version=[2],New=[deferred constant declaration includes
2231 a],Old=[subtype defined by the]} @nt<subtype_indication>
2232 @Chg{Version=[3],New=[@i<S> ],Old=[]}@Chg{Version=[2],
2233 New=[that defines a],Old=[in the deferred declaration is]}
2234 constrained@Chg{Version=[2],New=[ subtype],Old=[]}, then the
2235 @Chg{Version=[3],New=[constraint],Old=[subtype]} defined
2236 by the @nt<subtype_indication> in the full declaration shall match
2237 @Chg{Version=[3],New=[the constraint defined by @i<S>],Old=[it]}
2238 statically.@Redundant[ On the other hand,
2239 if the subtype of the deferred constant is unconstrained,
2240 then the full declaration is still allowed to impose a constraint.
2241 The constant itself will be constrained, like all constants;]
2242
2243 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01]}
2244 If the deferred constant declaration
2245 includes the reserved word @key(aliased), then the
2246 full declaration shall also@Chg{Version=[2],New=[;],Old=[.]}
2247 @begin{Ramification}
2248 On the other hand, the full constant can be aliased
2249 even if the deferred constant is not.
2250 @end{Ramification}
2251
2252 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00231-01]}
2253 @ChgAdded{Version=[2],Text=[If the subtype of the deferred constant
2254 declaration excludes null, the subtype of the full declaration shall also
2255 exclude null.]}
2256 @begin{Ramification}
2257 @ChgRef{Version=[2],Kind=[Added]}
2258 @ChgAdded{Version=[2],Text=[On the other hand, the full constant can
2259 exclude null even if the deferred constant does not. But that can only
2260 happen for a @nt{subtype_indication}, as anonymous access types are
2261 required to statically match (which includes any @nt{null_exclusion}).]}
2262 @end{Ramification}
2263 @end(itemize)
2264
2265 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1]}
2266 @Redundant[A deferred constant declaration
2267 @Chg{Version=[3],New=[for which the],Old=[that is completed by
2268 a @nt{pragma}]} Import @Chg{Version=[3],New=[aspect is True ],Old=[]}need
2269 not appear in the visible part of a @nt{package_specification},
2270 and has no full constant declaration.]
2271
2272
2273 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
2274 The completion of a deferred constant declaration shall occur
2275 before the constant is frozen
2276 (see @Chg{Version=[2],New=[@RefSecNum{Freezing Rules}],
2277 Old=[@RefSecNum{Deferred Constants}]}).
2278
2279 @end{Legality}
2280
2281 @begin{RunTime}
2282 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0004-1]}
2283 @PDefn2{Term=[elaboration], Sec=(deferred constant declaration)}
2284 The elaboration of a deferred constant declaration
2285 elaborates the @nt<subtype_indication>@Chg{Version=[3],New=[,
2286 @nt<access_definition>,],Old=[]} or (only allowed in the case of an
2287 imported constant) the @nt<array_type_definition>.
2288 @begin{Ramification}
2289 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0004-1]}
2290 @ChgAdded{Version=[3],Text=[For nonimported constants, these elaborations
2291 cannot require any code or checks for a legal program, because the given
2292 @nt<subtype_indication> has to be indefinite or statically match that of
2293 the full constant, meaning that either it is a @nt<subtype_mark> or it has
2294 static constraints. If the deferred constant instead has an
2295 @nt<access_definition>, the designated subtype must be a @nt<subtype_mark>.
2296 We still say that these are elaborated, however, because part of elaboration
2297 is creating the type, which is clearly needed for @nt<access_definition>s.
2298 (A deferred constant and its full constant have different types when
2299 they are specified by an @nt<access_definition>, although there is no
2300 visible effect of these types being different as neither can be named.)]}
2301 @end{Ramification}
2302 @end{RunTime}
2303
2304 @begin{Notes}
2305 The full constant declaration for a deferred constant that is of a given
2306 private type or private extension is not allowed before the corresponding
2307 @nt{full_type_declaration}. This is a consequence of the freezing
2308 rules for types
2309 (see @RefSecNum{Freezing Rules}).
2310 @begin{Ramification}
2311 Multiple or single declarations are allowed for the
2312 deferred and the full declarations, provided that the
2313 equivalent single declarations would be allowed.
2314
2315 Deferred constant declarations are useful for declaring constants of
2316 private views, and types with components of private views.
2317 They are also useful for declaring
2318 access-to-constant objects that designate
2319 variables declared in the private part of a package.
2320 @end{Ramification}
2321 @end{Notes}
2322
2323 @begin{Examples}
2324 @leading@keepnext@i{Examples of deferred constant declarations:}
2325 @begin{Example}
2326 Null_Key : @key[constant] Key; --@RI[ see @RefSecNum{Private Operations}]
2327
2328 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1]}
2329 CPU_Identifier : @key[constant] String(1..8)@Chg{Version=[3],New=[],Old=[;]}
2330 @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=[)]};
2331 --@RI[ see @RefSecNum{Interfacing Aspects}]
2332 @end{Example}
2333 @end{Examples}
2334
2335 @begin{Extend83}
2336 @Defn{extensions to Ada 83}
2337 In Ada 83, a deferred constant is required to be of a private type
2338 declared in the same visible part.
2339 This restriction is removed for Ada 95;
2340 deferred constants can be of any type.
2341
2342 In Ada 83, a deferred constant declaration was not permitted to
2343 include a constraint, nor the reserved word @key(aliased).
2344
2345 In Ada 83, the rules required conformance of type marks; here
2346 we require static matching of subtypes if the deferred constant
2347 is constrained.
2348
2349 A deferred constant declaration can be completed with a @nt{pragma}
2350 Import. Such a deferred constant declaration need not be within a
2351 @nt{package_specification}.
2352
2353 The rules for too-early uses of deferred constants are modified in
2354 Ada 95 to allow more cases, and catch all errors at compile time.
2355 This change is necessary in order to allow deferred constants of a
2356 tagged type without violating the principle that for a dispatching call,
2357 there is always an implementation to dispatch to.
2358 It has the beneficial side effect of catching some Ada-83-erroneous
2359 programs at compile time.
2360 The new rule fits in well with the new freezing-point rules.
2361 Furthermore, we are trying to convert undefined-value problems into
2362 bounded errors, and we were having trouble for the case of deferred
2363 constants.
2364 Furthermore, uninitialized deferred constants cause trouble for the
2365 shared variable / tasking rules, since they are really variable, even
2366 though they purport to be constant.
2367 In Ada 95, they cannot be touched until they become constant.
2368
2369 Note that we do not consider this change to be an upward
2370 incompatibility, because it merely changes an erroneous execution in Ada
2371 83 into a compile-time error.
2372
2373 The Ada 83 semantics are unclear in the case where
2374 the full view turns out to be an access type.
2375 It is a goal of the language design to prevent uninitialized access
2376 objects.
2377 One wonders if the implementation is required to initialize the
2378 deferred constant to null, and then initialize it (again!) to its
2379 real value.
2380 In Ada 95, the problem goes away.
2381 @end{Extend83}
2382
2383 @begin{DiffWord83}
2384 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2385 Since deferred constants can now be of a nonprivate type,
2386 we have made this a stand-alone @Chg{Version=[3],New=[subclause],Old=[clause]},
2387 rather than a subclause of @RefSec{Private Types and Private Extensions}.
2388
2389 Deferred constant declarations used to have their own syntax, but now
2390 they are simply a special case of @nt<object_declaration>s.
2391 @end{DiffWord83}
2392
2393 @begin{Extend95}
2394 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00385-01]}
2395 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2396 Deferred constants were enhanced to allow
2397 the use of anonymous access types in them.]}
2398 @end{Extend95}
2399
2400 @begin{DiffWord95}
2401 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01]}
2402 @ChgAdded{Version=[2],Text=[Added matching rules for subtypes that
2403 exclude null.]}
2404 @end{DiffWord95}
2405
2406 @begin{DiffWord2005}
2407 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0062-1]}
2408 @ChgAdded{Version=[3],Text=[@b<Correction:> Corrected rules so
2409 that the intent that a full constant may have a null exclusion even
2410 if the deferred constant does not is actually met.]}
2411 @end{DiffWord2005}
2412
2413
2414 @LabeledClause{Limited Types}
2415
2416 @begin{Intro}
2417 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
2418 @redundant[A limited type is (a view of) a type for which
2419 @Chg{Version=[2],New=[copying (such as for an @nt{assignment_statement})],
2420 Old=[the assignment operation]} is not allowed.
2421 A nonlimited type is a (view of a) type for which
2422 @Chg{Version=[2],New=[copying], Old=[the assignment operation]} is allowed.]
2423 @begin{Discussion}
2424 The concept of the @i(value) of a limited type is difficult
2425 to define, since the abstract value of a limited type often
2426 extends beyond its physical representation. In some
2427 sense, values of a limited type cannot be divorced from
2428 their object. The value @i(is) the object.
2429
2430 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
2431 In Ada 83, in the two places where limited types were defined
2432 by the language, namely tasks and files, an implicit
2433 level of indirection was implied by the semantics to
2434 avoid the separation of the value from an associated
2435 object.
2436 In Ada 95, most limited types are passed by reference,
2437 and even return-ed by reference.@Chg{Version=[2],New=[ In Ada 2005,
2438 most limited types are built-in-place upon return, rather than returned
2439 by reference. Thus the object @lquotes@;identity@rquotes is part of the
2440 logical value of most limited types.],Old=[]}
2441 @end{Discussion}
2442 @begin{Honest}
2443 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01],ARef=[AI95-00419-01]}
2444 For a limited partial view whose full view is nonlimited,
2445 @Chg{Version=[2],New=[copying],Old=[assignment]} is possible on parameter
2446 passing and function return. To prevent any copying whatsoever, one should
2447 make both the partial @i{and} full views limited.
2448 @end{Honest}
2449 @Comment{The below was moved from a ChgToGlossaryAlso.}
2450 @ChgToGlossary{Version=[2],Kind=[Revised],Term=<Limited type>,
2451 Text=<A limited type is @Chg{Version=[2],New=[],Old=[(a view of) ]}a type
2452 for which
2453 @Chg{Version=[2],New=[copying (such as in an @nt{assignment_statement})],
2454 Old=[the assignment operation]} is not allowed.
2455 A nonlimited type is a @Chg{Version=[2],New=[],Old=[(view of a) ]}type for
2456 which
2457 @Chg{Version=[2],New=[copying], Old=[the assignment operation]} is allowed.>}
2458 @end{Intro}
2459
2460 @begin{Legality}
2461
2462 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
2463 If a tagged record type has any limited components,
2464 then the reserved word @key[limited] shall
2465 appear in its @nt<record_type_definition>.@Chg{Version=[2],New=[ @Redundant[If
2466 the reserved word @key[limited] appears in the definition of a
2467 @nt{derived_type_definition}, its parent type and any progenitor interfaces
2468 shall be limited.]],Old=[]}
2469 @begin{TheProof}
2470 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01]}
2471 @ChgAdded{Version=[2],Text=[The rule about the parent type being required
2472 to be limited can be found in @RefSecNum{Derived Types and Classes}. Rules
2473 about progenitor interfaces can be found in
2474 @RefSecNum{Interface Types}, specifically, a nonlimited interface can appear
2475 only on a nonlimited type. We repeat these rules here to gather these
2476 scattered rules in one obvious place.]}
2477 @end{TheProof}
2478 @begin{Reason}
2479 @leading@;This prevents tagged limited types from becoming nonlimited.
2480 Otherwise, the following could happen:
2481 @begin{Example}
2482 @key[package] P @key[is]
2483 @key[type] T @key[is] @key[limited] @key[private];
2484 @key[type] R @key[is] @key[tagged]
2485 @key[record] --@RI{ Illegal!}
2486 --@RI{ This should say @lquotes@;@key[limited record]@rquotes@;.}
2487 X : T;
2488 @key[end] @key[record];
2489 @key[private]
2490 @key[type] T @key[is] @key[new] Integer; --@RI{ R becomes nonlimited here.}
2491 @key[end] P;
2492
2493 @ChgRef{Version=[2],Kind=[Revised]}
2494 @key[package] Q @key[is]
2495 @key[type] R2@Chg{Version=[2],New=[],Old=[(Access_Discrim : @key[access] ...)]} @key[is] @key[new] R @key[with]
2496 @key[record]
2497 Y : Some_Task_Type;
2498 @key[end] @key[record];
2499 @key[end] Q;
2500 @end{Example}
2501
2502 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
2503 If the above were legal,
2504 then assignment would be defined for R'Class in the body of P,
2505 which is bad news, given @Chg{Version=[2],New=[],Old=[the access discriminant
2506 and ]}the task.
2507 @end{Reason}
2508
2509 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
2510 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0147-1]}
2511 @ChgAdded{Version=[2],Type=[Leading],Text=[In the following contexts,
2512 an @nt{expression} of a limited
2513 type is not permitted unless it is an @nt{aggregate}, a @nt{function_call},
2514 @Chg{Version=[3],New=[],Old=[or ]}a parenthesized @nt{expression} or
2515 @nt{qualified_expression} whose operand
2516 is permitted by this rule@Chg{Version=[3],New=[, or a @nt{conditional_expression}
2517 all of whose @Syni{dependent_}@nt{expression}s are permitted by this
2518 rule],Old=[]}:]}
2519 @begin{Itemize}
2520
2521 @ChgRef{Version=[2],Kind=[Added]}
2522 @Chg{Version=[2],New=[the initialization @nt{expression} of an
2523 @nt{object_declaration} (see @RefSecNum{Object Declarations})],Old=[]}
2524
2525 @ChgRef{Version=[2],Kind=[Added]}
2526 @Chg{Version=[2],New=[the @nt{default_expression} of a
2527 @nt{component_declaration} (see @RefSecNum{Record Types})],Old=[]}
2528
2529 @ChgRef{Version=[2],Kind=[Added]}
2530 @Chg{Version=[2],New=[the @nt{expression} of a @nt{record_component_association} (see @RefSecNum{Record Aggregates})],Old=[]}
2531
2532 @ChgRef{Version=[2],Kind=[Added]}
2533 @Chg{Version=[2],New=[the @nt{expression} for an @nt{ancestor_part} of an
2534 @nt{extension_aggregate} (see @RefSecNum{Extension Aggregates})],Old=[]}
2535
2536 @ChgRef{Version=[2],Kind=[Added]}
2537 @Chg{Version=[2],New=[an @nt{expression} of a @nt{positional_array_aggregate}
2538 or the @nt{expression} of an @nt{array_component_association} (see
2539 @RefSecNum{Array Aggregates})],Old=[]}
2540
2541 @ChgRef{Version=[2],Kind=[Added]}
2542 @Chg{Version=[2],New=[the @nt{qualified_expression} of an initialized allocator
2543 (see @RefSecNum{Allocators})],Old=[]}
2544
2545 @ChgRef{Version=[2],Kind=[Added]}
2546 @Chg{Version=[2],New=[the @nt{expression} of a return statement (see
2547 @RefSecNum{Return Statements})],Old=[]}
2548
2549 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0177-1]}
2550 @ChgRef{Version=[4],Kind=[RevisedAdded],ARef=[AI12-0157-1]}
2551 @Chg{Version=[3],New=[the
2552 @Chg{Version=[4],New=[return expression],Old=[@nt{expression}]} of an
2553 @Chg{Version=[4],New=[expression function],Old=[@nt{expression_function_declaration}]}
2554 (see @RefSecNum{Expression Functions})],Old=[]}
2555
2556 @ChgRef{Version=[2],Kind=[Added]}
2557 @ChgRef{Version=[3],Kind=[RevisedAdded]}@ChgNote{Only because the paragraph number has changed}
2558 @Chg{Version=[2],New=[the @nt{default_expression} or actual parameter for a
2559 formal object of mode @b{in} (see @RefSecNum{Formal Objects})],Old=[]}
2560
2561 @begin{Discussion}
2562 @ChgRef{Version=[2],Kind=[AddedNormal]}
2563 @ChgAdded{Version=[2],Text=[All of these contexts normally require copying; by
2564 restricting the uses as above, we can require the new object to be
2565 built-in-place.]}
2566 @end{Discussion}
2567 @end{Itemize}
2568
2569
2570 @end{Legality}
2571
2572 @begin{StaticSem}
2573 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
2574 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0178-1]}
2575 @leading@keepnext@Defn{limited type}
2576 A @Chg{Version=[3],New=[view of a ],Old=[]}type is @i{limited} if it
2577 is @Chg{Version=[2],New=[],Old=[a descendant of ]}one of the following:
2578 @begin(itemize)
2579 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00411-01],ARef=[AI95-00419-01]}
2580 a type with the reserved word @key(limited)@Chg{Version=[2],New=[,
2581 @key(synchronized), @key(task), or @key(protected) ],Old=[]}
2582 in its definition;
2583 @begin{Ramification}
2584 Note that there is always a @lquotes@;definition,@rquotes@; conceptually,
2585 even if there is no syntactic category called @lquotes@;..._definition@rquotes@;.
2586
2587 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
2588 @ChgAdded{Version=[2],Text=[This includes interfaces of the above kinds,
2589 derived types with the reserved word @key{limited}, as well as task and
2590 protected types.]}
2591 @end{Ramification}
2592
2593 @ChgNote{This should really be deleted in Version 2, but we want to
2594 reuse the paragraph number, and that is hard, so we add the message manually.}
2595 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
2596 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0087-1]}
2597 @Chg{Version=[3],New=[a class-wide type whose specific type is limited;],
2598 Old=[@Chg{Version=[2],New=[@Shrink{@i<This paragraph was deleted.>}],
2599 Old=[a task or protected type;]}]}
2600
2601 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
2602 a composite type with a limited component@Chg{Version=[2],New=[;],Old=[.]}
2603
2604 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0178-1]}
2605 @ChgAdded{Version=[3],Text=[an incomplete view;]}
2606
2607 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01]}
2608 @ChgAdded{Version=[2],Text=[a derived type whose parent is limited and is not an
2609 interface.]}
2610
2611 @begin{Ramification}
2612 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
2613 @ChgAdded{Version=[2],Text=[Limitedness is not inherited from interfaces;
2614 it must be explicitly specified when the parent is an interface.]}
2615 @end{Ramification}
2616
2617 @end(itemize)
2618
2619 @begin{Honest}
2620 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
2621 @ChgAdded{Version=[2],Text=[A derived type can become nonlimited if
2622 @key{limited} does not appear and the derivation
2623 takes place in the visible part of a child package,
2624 and the parent type is nonlimited as viewed from the
2625 private part or body of the child package.]}
2626 @end{Honest}
2627 @begin{Reason}
2628 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
2629 @ChgAdded{Version=[2],Text=[We considered a rule where limitedness was always
2630 inherited from the parent for derived types, but in the case of a type whose
2631 parent is an interface, this meant that the first interface is treated
2632 differently than other interfaces. It also would have forced users to declare
2633 dummy nonlimited interfaces just to get the limitedness right. We also
2634 considered a syntax like @key{not limited} to specify nonlimitedness when the
2635 parent was limited, but that was unsavory. The rule given is more uniform and
2636 simpler to understand.]}
2637
2638 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
2639 @ChgAdded{Version=[2],Text=[The rules for interfaces are asymmetrical, but
2640 the language is not: if the parent interface is limited, the presence of the
2641 word @key{limited} determines the limitedness, and nonlimited progenitors are
2642 illegal by the rules in @RefSecNum{Interface Types} if @key{limited} is
2643 present. If the parent interface
2644 is nonlimited, the word @key{limited} is illegal by the rules in
2645 @RefSecNum{Derived Types and Classes}. The net effect is that the order of
2646 the interfaces doesn't matter.]}
2647 @end{Reason}
2648
2649
2650 @Defn{nonlimited type}
2651 Otherwise, the type is nonlimited.
2652
2653 @Redundant[There are no predefined equality operators for
2654 a limited type.]
2655
2656 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0052-1]}
2657 @ChgAdded{Version=[3],Type=[Leading],Keepnext=[T],Text=[A type is
2658 @i{immutably limited} if it is one of the
2659 following:@Defn{immutably limited}@Defn2{Term=[limited type],Sec=[immutably]}]}
2660
2661 @begin{Itemize}
2662 @ChgRef{Version=[3],Kind=[Added]}
2663 @ChgAdded{Version=[3],Text=[An explicitly limited record type;]}
2664
2665 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0217-1]}
2666 @ChgAdded{Version=[3],Text=[A record extension with the reserved word
2667 @key[limited];]}
2668
2669 @ChgRef{Version=[3],Kind=[Added]}
2670 @ChgAdded{Version=[3],Text=[A nonformal limited private type that is
2671 tagged or has at least one access discriminant with a @nt{default_expression};]}
2672
2673 @begin{Reason}
2674 @ChgRef{Version=[3],Kind=[AddedNormal]}
2675 @ChgAdded{Version=[3],Text=[The full type in both of these cases must
2676 necessarily be immutably limited. We need to include private types
2677 as much as possible so that we aren't unintentionally discouraging the
2678 use of private types.]}
2679 @end{Reason}
2680
2681 @ChgRef{Version=[3],Kind=[Added]}
2682 @ChgAdded{Version=[3],Text=[A task type, a protected type, or a
2683 synchronized interface;]}
2684
2685 @ChgRef{Version=[3],Kind=[Added]}
2686 @ChgAdded{Version=[3],Text=[A type derived from an immutably limited type.]}
2687
2688 @end{Itemize}
2689
2690 @begin{Discussion}
2691 @ChgRef{Version=[3],Kind=[AddedNormal]}
2692 @ChgAdded{Version=[3],Text=[An immutably limited type is a type that cannot
2693 become nonlimited subsequently in a private part or in a child unit.
2694 If a view of the type makes it immutably limited, then no copying (assignment)
2695 operations are ever available for objects of the type. This allows other
2696 properties; for instance, it is safe for such objects to have
2697 access discriminants that have defaults or designate other limited objects.]}
2698 @end{Discussion}
2699
2700 @begin{Ramification}
2701 @ChgRef{Version=[3],Kind=[AddedNormal]}
2702 @ChgAdded{Version=[3],Text=[A nonsynchronized limited interface type is
2703 not immutably limited; a type derived from it can be nonlimited.]}
2704 @end{Ramification}
2705
2706 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0052-1]}
2707 @ChgAdded{Version=[3],Text=[A descendant of a generic formal
2708 limited private type is
2709 presumed to be immutably limited except within the body
2710 of a generic unit or a body declared within the declarative
2711 region of a generic unit, if the formal type is declared
2712 within the formal part of the generic unit.]}
2713
2714 @begin{Ramification}
2715 @ChgRef{Version=[3],Kind=[AddedNormal]}
2716 @ChgAdded{Version=[3],Text=[In an instance, a type is descended from
2717 the actual type corresponding to the formal, and all rules are rechecked
2718 in the specification. Bodies are excepted so that we assume the worst there;
2719 the complex wording is required to handle children of generics and
2720 unrelated bodies properly.]}
2721 @end{Ramification}
2722
2723 @end{StaticSem}
2724
2725 @begin{ImplReq}
2726 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
2727 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg],ARef=[AI05-0067-1]}
2728 @ChgDeleted{Version=[3],Text=[@Chg{Version=[2],New=[
2729 For an @nt{aggregate} of a limited type used to initialize an object as allowed
2730 above, the implementation shall not create a separate anonymous object for the
2731 @nt{aggregate}. For a @nt{function_call} of a type with a part that is of a
2732 task, protected, or explicitly limited record type that is used to initialize
2733 an object as allowed above, the implementation shall not create a separate
2734 return object (see 6.5) for the @nt{function_call}. The @nt{aggregate} or
2735 @nt{function_call} shall be constructed directly in the new object.],Old=[]}]}
2736 @begin{Discussion}
2737 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
2738 @ChgRef{Version=[3],Kind=[DeletedNoDelMsg],ARef=[AI05-0067-1]}
2739 @ChgDeleted{Version=[3],Text=[@Chg{Version=[2],New=[For a
2740 @nt{function_call}, we only require @i{build-in-place}@PDefn{build-in-place}
2741 for a limited type that would have
2742 been a return-by-reference type in Ada 95. We do this because
2743 we want to minimize disruption to Ada 95 implementations and users.],Old=[]}]}
2744 @end{Discussion}
2745 @end{ImplReq}
2746
2747 @begin{Notes}
2748 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
2749 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
2750 @Chg{Version=[2],New=[While it is allowed to write initializations of limited
2751 objects, such initializations never copy a limited object. The source of such an
2752 assignment operation must be an @nt<aggregate> or @nt<function_call>, and such
2753 @nt<aggregate>s and @nt<function_call>s must be built directly in the target
2754 object@Chg{Version=[3],New=[ (see @RefSecNum{Assignment and Finalization})],Old=[]}.],
2755 Old=[@leading@keepnext@;The following are consequences of the rules for limited types:]}
2756 @begin{Honest}
2757 @ChgRef{Version=[2],Kind=[AddedNormal]}
2758 @ChgAdded{Version=[2],Text=[This isn't quite true if the type can become
2759 nonlimited (see below); @nt{function_call}s only are required to be
2760 build-in-place for @lquotes@;really@rquotes@; limited types.]}
2761 @end{Honest}
2762
2763 @begin{NotIso}
2764 @ChgAdded{Version=[2],Noprefix=[T],Noparanum=[T],Text=[@Shrink{@i<Paragraphs 10
2765 through 15 were deleted.>}]}@Comment{This message should be deleted if the
2766 paragraphs are ever renumbered.}
2767 @end{NotIso}
2768
2769 @begin{Itemize}
2770 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
2771 @ChgDeleted{Version=[2],Text=[An initialization expression is not allowed in an
2772 @nt{object_declaration} if the type of the object is limited.]}
2773
2774 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
2775 @ChgDeleted{Version=[2],Text=[A default expression is not allowed in a
2776 @nt{component_declaration} if the type of the record component is limited.]}
2777
2778 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
2779 @ChgDeleted{Version=[2],Text=[An initialized allocator is not allowed if the
2780 designated type is limited.]}
2781
2782 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
2783 @ChgDeleted{Version=[2],Text=[A generic formal parameter of mode @key[in] must
2784 not be of a limited type.]}
2785 @end{Itemize}
2786
2787 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
2788 @ChgDeleted{Version=[2],Text=[@nt{Aggregate}s are not available for a limited
2789 composite type. Concatenation is not available for a limited array type.]}
2790
2791 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
2792 @ChgDeleted{Version=[2],Text=[The rules do not exclude a @nt{default_expression}
2793 for a formal parameter of a limited type; they do not exclude a deferred
2794 constant of a limited type if the full declaration of the constant is of a
2795 nonlimited type.]}
2796
2797 @Defn{become nonlimited}
2798 @Defn2{Term=[nonlimited type],Sec=(becoming nonlimited)}
2799 @Defn2{Term=[limited type],Sec=(becoming nonlimited)}
2800 As illustrated in @RefSecNum{Private Operations},
2801 an untagged limited type can become nonlimited under certain
2802 circumstances.
2803 @begin{Ramification}
2804 Limited private types do not become nonlimited;
2805 instead, their full view can be nonlimited,
2806 which has a similar effect.
2807
2808 It is important to remember
2809 that a single nonprivate type can be both limited and nonlimited
2810 in different parts of its scope. In other words, @lquotes@;limited@rquotes@; is a property
2811 that depends on where you are in the scope of the type.
2812 We don't call this a @lquotes@;view property@rquotes@; because there is no particular
2813 declaration to declare the nonlimited view.
2814
2815 Tagged types never become nonlimited.
2816 @end{Ramification}
2817 @end{Notes}
2818
2819 @begin{Examples}
2820 @leading@keepnext@i{Example of a package with a limited type:}
2821 @begin{Example}
2822 @key[package] IO_Package @key[is]
2823 @key[type] File_Name @key[is] @key[limited] @key[private];
2824
2825 @key[procedure] Open (F : @key[in] @key[out] File_Name);
2826 @key[procedure] Close(F : @key[in] @key[out] File_Name);
2827 @key[procedure] Read (F : @key[in] File_Name; Item : @key[out] Integer);
2828 @key[procedure] Write(F : @key[in] File_Name; Item : @key[in] Integer);
2829 @key[private]
2830 @key[type] File_Name @key[is]
2831 @key[limited] @key[record]
2832 Internal_Name : Integer := 0;
2833 @key[end] @key[record];
2834 @key[end] IO_Package;
2835
2836
2837 @key[package] @key[body] IO_Package @key[is]
2838 Limit : @key[constant] := 200;
2839 @key[type] File_Descriptor @key[is] @key[record] ... @key[end] @key[record];
2840 Directory : @key[array] (1 .. Limit) @key[of] File_Descriptor;
2841 ...
2842 @key[procedure] Open (F : @key[in] @key[out] File_Name) @key[is] ... @key[end];
2843 @key[procedure] Close(F : @key[in] @key[out] File_Name) @key[is] ... @key[end];
2844 @key[procedure] Read (F : @key[in] File_Name; Item : @key[out] Integer) @key[is] ... @key[end];
2845 @key[procedure] Write(F : @key[in] File_Name; Item : @key[in] Integer) @key[is] ... @key[end];
2846 @key[begin]
2847 ...
2848 @key[end] IO_Package;
2849 @end{Example}
2850 @end{Examples}
2851
2852 @begin{Notes}
2853 @i{Notes on the example:}
2854 In the example above, an outside subprogram making use of IO_Package
2855 may obtain a file name by calling Open and later use it in calls to
2856 Read and Write. Thus, outside the package, a file name obtained from
2857 Open acts as a kind of password; its internal properties (such as
2858 containing a numeric value) are not known and no other operations
2859 (such as addition or comparison of internal names) can be performed
2860 on a file name.
2861 Most importantly, clients of the package cannot make copies
2862 of objects of type File_Name.
2863
2864 @NoPrefix@;This example is characteristic of any case where complete control
2865 over the operations of a type is desired. Such packages serve a dual
2866 purpose. They prevent a user from making use of the internal
2867 structure of the type. They also implement the notion of an
2868 encapsulated data type where the only operations on the type are
2869 those given in the package specification.
2870
2871 @NoPrefix@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
2872 The fact that the full view of File_Name is explicitly declared
2873 @key[limited] means that parameter passing @Chg{Version=[2],New=[],
2874 Old=[and function return ]}will always be by reference@Chg{Version=[2],New=[
2875 and function results will always be built directly in the result object],Old=[]}
2876 (see @RefSecNum{Formal Parameter Modes} and @RefSecNum{Return Statements}).
2877
2878 @end{Notes}
2879
2880 @begin{Extend83}
2881 @Defn{extensions to Ada 83}
2882 The restrictions in RM83-7.4.4(4),
2883 which disallowed @key[out] parameters of limited types in certain
2884 cases, are removed.
2885 @end{Extend83}
2886
2887 @begin{DiffWord83}
2888 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2889 Since limitedness and privateness are orthogonal in Ada 95 (and
2890 to some extent in Ada 83), this is now its own @Chg{Version=[3],New=[subclause],Old=[clause]}
2891 rather than being a subclause of
2892 @RefSec{Private Types and Private Extensions}.
2893 @end{DiffWord83}
2894
2895 @begin{Extend95}
2896 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
2897 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2898 Limited types now have an assignment operation, but its use is restricted
2899 such that all uses are build-in-place. This is
2900 accomplished by restricting uses to @nt{aggregate}s and @nt{function_call}s.
2901 @nt{Aggregate}s were not allowed to have a limited type in Ada 95, which
2902 causes a compatibility issue discussed in @RefSec{Aggregates}.
2903 Compatibility issues with return statements for limited
2904 @nt{function_call}s are discussed
2905 in @RefSec{Return Statements}.]}
2906 @end{Extend95}
2907
2908 @begin{DiffWord95}
2909 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00411-01],ARef=[AI95-00419-01]}
2910 @ChgAdded{Version=[2],Text=[Rewrote the definition of limited to ensure that
2911 interfaces are covered, but that limitedness is not inherited from interfaces.
2912 Derived types that explicitly include @key{limited} are now also covered.]}
2913 @end{DiffWord95}
2914
2915 @begin{DiffWord2005}
2916 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0052-1],ARef=[AI05-0217-1]}
2917 @ChgAdded{Version=[3],Text=[@b<Correction:> Added a definition for
2918 immutably limited types, so that the fairly complex definition does
2919 not need to be repeated in rules elsewhere in the Standard.]}
2920
2921 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1],ARef=[AI05-0299-1]}
2922 @ChgAdded{Version=[3],Text=[@b<Correction:> The built-in-place rules
2923 are consolidated in @RefSecNum{Assignment and Finalization}, and thus
2924 they are removed from this subclause.]}
2925
2926 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0087-1]}
2927 @ChgAdded{Version=[3],Text=[@b<Correction:> Fixed an oversight: class-wide
2928 types were never defined to be limited, even if their associated specific
2929 type is. It is thought that this oversight was never implemented incorrectly
2930 by any compiler, thus we have not classified it as an incompatibility.]}
2931
2932 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0147-1]}
2933 @ChgAdded{Version=[3],Text=[Allowed @nt{conditional_expression}s in limited
2934 constructor contexts @em we want to treat these as closely to parentheses as
2935 possible.]}
2936
2937 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0177-1]}
2938 @ChgAdded{Version=[3],Text=[Added wording so that expression functions can
2939 return limited entities.]}
2940
2941 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0178-1]}
2942 @ChgAdded{Version=[3],Text=[@b<Correction:> Added incomplete views
2943 to the list of reasons for a view of a type to be limited. This is not
2944 a change as the definition already was in
2945 @RefSecNum{Incomplete Type Declarations}. But it is much better to have
2946 all of the reasons for limitedness together.]}
2947 @end{DiffWord2005}
2948
2949
2950 @RMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
2951 @LabeledRevisedClause{Version=[3],New=[Assignment and Finalization],Old=[User-Defined Assignment and Finalization]}
2952
2953 @begin{Intro}
2954 @redundant[@Defn{user-defined assignment}
2955 @Defn2{Term=[assignment], Sec=(user-defined)}
2956 Three kinds of actions are
2957 fundamental to the manipulation of objects:
2958 initialization, finalization, and assignment.
2959 Every object is initialized, either explicitly
2960 or by default, after being created
2961 (for example, by an @nt{object_declaration} or @nt{allocator}).
2962 Every object is finalized before being destroyed
2963 (for example, by leaving a @nt{subprogram_body} containing an
2964 @nt{object_declaration}, or by a call to an instance of
2965 Unchecked_Deallocation).
2966 An assignment operation is used as part of @nt{assignment_statement}s,
2967 explicit initialization, parameter passing, and other operations.
2968 @IndexSee{Term=[constructor],See=[initialization]}
2969 @IndexSee{Term=[constructor],See=[Initialize]}
2970 @IndexSee{Term=[destructor],See=[finalization]}
2971
2972 Default definitions for these three fundamental
2973 operations are provided by the
2974 language, but
2975 @Defn{controlled type}
2976 a @i{controlled} type gives the user additional
2977 control over parts of these operations.
2978 @Defn{Initialize}@Defn{Finalize}@Defn{Adjust}
2979 In particular, the user can define, for a controlled type,
2980 an Initialize procedure which is invoked immediately after
2981 the normal default initialization of a controlled object, a Finalize procedure
2982 which is invoked immediately before finalization of any of the
2983 components of a controlled object, and an Adjust
2984 procedure which is invoked as the last step of an assignment to
2985 a (nonlimited) controlled object.]
2986 @ToGlossary{Term=<Controlled type>,
2987 Text=<A controlled type supports user-defined assignment and
2988 finalization.
2989 Objects are always finalized before being destroyed.>}
2990 @begin{Ramification}
2991 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01],ARef=[AI95-00287-01]}
2992 Here's the basic idea of initialization, value adjustment, and finalization,
2993 whether or not user defined:
2994 When an object is created,
2995 if it is explicitly assigned an initial value,
2996 @Chg{Version=[2],New=[the object is either built-in-place from an @nt{aggregate}
2997 or function call (in which case neither Adjust nor Initialize is applied), or ],Old=[]}
2998 the assignment copies and
2999 adjusts the initial value. Otherwise, Initialize is applied to it
3000 (except in the case of an @nt{aggregate} as a whole).
3001 An @nt{assignment_statement} finalizes the target before
3002 copying in and adjusting the new value.
3003 Whenever an object goes away, it is finalized.
3004 Calls on Initialize and Adjust happen bottom-up; that is,
3005 components first, followed by the containing object.
3006 Calls on Finalize @Chg{Version=[2],New=[happen],Old=[happens]} top-down; that is,
3007 first the containing object, and then its components.
3008 These ordering rules ensure that any components will be in a
3009 well-defined state when Initialize, Adjust,
3010 or Finalize is applied to the containing object.
3011 @end{Ramification}
3012 @end{Intro}
3013
3014 @begin{StaticSem}
3015 @leading@keepnext@;The following language-defined library package exists:
3016 @begin{Example}@ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0020],ARef=[AI95-00126-01]}
3017 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0212-1]}
3018 @key[package] Ada.Finalization @key[is]@ChildUnit{Parent=[Ada],Child=[Finalization]}
3019 @key[pragma] @Chg{Version=[3],New=[Pure],Old=[Preelaborate]}(Finalization);@Chg{Version=[3],New=[],Old=[@Chg{New=[
3020 @key[pragma] Remote_Types(Finalization);],Old=[]}]}
3021
3022 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00161-01]}
3023 @key[type] @AdaTypeDefn{Controlled} @key[is abstract tagged private];@Chg{Version=[2],New=[
3024 @key{pragma} Preelaborable_Initialization(Controlled);],Old=[]}
3025
3026 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
3027 @key(procedure) @AdaSubDefn{Initialize} (Object : @key(in out) Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3028 @key(procedure) @AdaSubDefn{Adjust} (Object : @key(in out) Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3029 @key(procedure) @AdaSubDefn{Finalize} (Object : @key(in out) Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3030
3031 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00161-01]}
3032 @key[type] @AdaTypeDefn{Limited_Controlled} @key[is abstract tagged limited private];@Chg{Version=[2],New=[
3033 @key{pragma} Preelaborable_Initialization(Limited_Controlled);],Old=[]}
3034
3035 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
3036 @key(procedure) @AdaSubDefn{Initialize} (Object : @key(in out) Limited_Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3037 @key(procedure) @AdaSubDefn{Finalize} (Object : @key(in out) Limited_Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
3038 @key(private)
3039 ... -- @RI{not specified by the language}
3040 @key[end] Ada.Finalization;
3041 @end{Example}
3042
3043 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
3044 @Defn{controlled type}
3045 A controlled type is a descendant of Controlled or Limited_Controlled.
3046 @Chg{Version=[2],New=[],Old=[The (default) implementations of
3047 Initialize, Adjust, and Finalize have no effect. ]}The predefined
3048 "=" operator of type Controlled always returns True,
3049 @Redundant[since this operator is incorporated into
3050 the implementation of the predefined equality operator of types derived
3051 from Controlled, as explained in
3052 @RefSecNum(Relational Operators and Membership Tests).]
3053 The type Limited_Controlled is like Controlled, except
3054 that it is limited and it lacks the primitive subprogram Adjust.
3055 @begin{Discussion}
3056 We say @lquotes@;nonlimited controlled type@rquotes@ (rather than just
3057 @lquotes@;controlled type@rquotes@;;) when we want to talk about descendants
3058 of Controlled only.
3059 @end{Discussion}
3060 @begin{Reason}
3061 We considered making Adjust and Finalize abstract.
3062 However, a reasonable coding convention is e.g. for Finalize to
3063 always call the parent's Finalize after doing whatever work is needed
3064 for the extension part.
3065 (Unlike CLOS, we have no way to do that automatically in Ada 95.)
3066 For this to work, Finalize cannot be abstract.
3067 In a generic unit, for a generic formal abstract derived type whose
3068 ancestor is Controlled or Limited_Controlled, calling the ancestor's
3069 Finalize would be illegal if it were abstract, even though the actual
3070 type might have a concrete version.
3071
3072 Types Controlled and Limited_Controlled are abstract, even though
3073 they have no abstract primitive subprograms.
3074 It is not clear that they need to be abstract, but there seems to be
3075 no harm in it, and it might make an implementation's life easier to
3076 know that there are no objects of these types @em in case the
3077 implementation wishes to make them @lquotes@;magic@rquotes@; in some way.
3078
3079 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01]}
3080 @ChgAdded{Version=[2],Text=[For Ada 2005, we considered making these types
3081 interfaces. That would have the advantage of allowing them to be added
3082 to existing trees. But that was rejected both because it would cause
3083 massive disruptions to existing implementations, and because it would be
3084 very incompatible due to the "no hidden interfaces" rule. The latter rule
3085 would prevent a tagged private type from being completed with a derivation
3086 from Controlled or Limited_Controlled @em a very common idiom.]}
3087 @end{Reason}
3088
3089 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00360-01]}
3090 @ChgAdded{Version=[2],Type=[Leading],Text=[A type is said to
3091 @i{need finalization} if:@Defn{needs finalization}@Defn2{Term=[type],Sec=[needs finalization]}]}
3092
3093 @begin{Itemize}
3094 @ChgRef{Version=[2],Kind=[Added]}
3095 @ChgAdded{Version=[2],Text=[it is a controlled type, a task type or
3096 a protected type; or]}
3097
3098 @ChgRef{Version=[2],Kind=[Added]}
3099 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0092-1]}
3100 @ChgAdded{Version=[2],Text=[it has a component
3101 @Chg{Version=[3],New=[whose type ],Old=[that ]} needs finalization; or]}
3102
3103 @ChgRef{Version=[2],Kind=[Added]}
3104 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0013-1]}
3105 @Chg{Version=[3],New=[it is a class-wide type; or],
3106 Old=[@Chg{Version=[2],New=[it is a limited type that
3107 has an access discriminant whose designated type needs finalization; or],Old=[]}]}
3108
3109 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0026-1]}
3110 @ChgAdded{Version=[3],Text=[it is a partial view whose full view needs finalization; or]}
3111
3112 @ChgRef{Version=[2],Kind=[Added]}
3113 @ChgAdded{Version=[2],Text=[it is one of a number of language-defined types
3114 that are explicitly defined to need finalization.]}
3115
3116 @begin{Ramification}
3117 @ChgRef{Version=[2],Kind=[AddedNormal]}
3118 @ChgAdded{Version=[2],Text=[The fact that a type needs finalization
3119 does not require it to be implemented with a controlled type. It just has to
3120 be recognized by the No_Nested_Finalization restriction.]}
3121
3122 @ChgRef{Version=[2],Kind=[AddedNormal]}
3123 @ChgAdded{Version=[2],Text=[This property is defined for the type, not
3124 for a particular view. That's necessary as restrictions look in private parts
3125 to enforce their restrictions; the point is to eliminate all controlled
3126 parts, not just ones that are visible.]}
3127 @end{Ramification}
3128
3129 @end{Itemize}
3130 @end{StaticSem}
3131
3132 @begin{RunTime}
3133 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
3134 @Chg{Version=[2],New=[],Old=[@PDefn2{Term=[elaboration],
3135 Sec=(object_declaration)}]}During
3136 the elaboration @Chg{Version=[2],New=[or evaluation of a construct that
3137 causes an object to be initialized by default],Old=[of
3138 an @nt{object_declaration}]},
3139 for every controlled subcomponent of
3140 the object that is not assigned an initial value
3141 (as defined in @RefSecNum{Object Declarations}),
3142 Initialize is called on that subcomponent.
3143 Similarly, if the object @Chg{Version=[2],New=[that is initialized by
3144 default ],Old=[]}as a whole is controlled@Chg{Version=[2],New=[],Old=[ and
3145 is not assigned an initial value]}, Initialize is called on the object.@Chg{Version=[2],
3146 New=[],Old=[ The same applies to the evaluation of an @nt{allocator},
3147 as explained in @RefSecNum{Allocators}.]}
3148
3149 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0021],ARef=[AI95-00182-01]}
3150 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
3151 For an @nt{extension_aggregate} whose @nt{ancestor_part} is a
3152 @nt{subtype_mark}@Chg{Version=[2],New=[ denoting a],
3153 Old=[, @Chg{New=[for each controlled subcomponent of the ancestor part, either
3154 Initialize is called, or its initial value is assigned, as appropriate],
3155 Old=[Initialize is called on all controlled subcomponents of the
3156 ancestor part]}; if the type of the ancestor part is itself]}
3157 controlled@Chg{Version=[2],New=[ subtype],Old=[]},
3158 the Initialize procedure of the ancestor type is called,
3159 unless that Initialize procedure is abstract.
3160 @begin{Discussion}
3161 @leading@keepnext@;Example:
3162 @begin{Example}
3163 @key[type] T1 @key[is] @key[new] Controlled @key[with]
3164 @key[record]
3165 ... --@RI{ some components might have defaults}
3166 @key[end] @key[record];
3167
3168 @key[type] T2 @key[is] @key[new] Controlled @key[with]
3169 @key[record]
3170 X : T1; --@RI{ no default}
3171 Y : T1 := ...; --@RI{ default}
3172 @key[end] @key[record];
3173
3174 A : T2;
3175 B : T2 := ...;
3176 @end{Example}
3177
3178 As part of the elaboration of A's declaration, A.Y is assigned a
3179 value; therefore Initialize is not applied to A.Y.
3180 Instead, Adjust is applied to A.Y as part of the assignment operation.
3181 Initialize is applied to A.X and to A, since those objects are not
3182 assigned an initial value.
3183 The assignment to A.Y is not considered an assignment to A.
3184
3185 For the elaboration of B's declaration, Initialize is not called at
3186 all. Instead the assignment adjusts B's value;
3187 that is, it applies Adjust to B.X, B.Y, and B.
3188
3189 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0021],ARef=[AI95-00182-01]}
3190 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00373-01]}
3191 @ChgAdded{Version=[1],Text=[The @nt{ancestor_part} of an
3192 @nt{extension_aggregate}@Chg{Version=[2],New=[, <> in aggregates, and the
3193 return object of an @nt{extended_return_statement} are],Old=[ is]} handled
3194 similarly.]}
3195 @end{Discussion}
3196
3197 Initialize and other initialization
3198 operations are done in an arbitrary order,
3199 except as follows.@PDefn2{Term=[arbitrary order],Sec=[allowed]}
3200 Initialize is applied to an object after initialization
3201 of its subcomponents, if any
3202 @Redundant[(including both implicit initialization and Initialize calls)].
3203 If an object has a component with an access discriminant
3204 constrained by a per-object expression,
3205 Initialize is applied to this component after any components that do not
3206 have such discriminants.
3207 For an object with several components with such a discriminant,
3208 Initialize is applied to them in order
3209 of their @nt{component_declaration}s.
3210 For an @nt<allocator>,
3211 any task activations follow all calls
3212 on Initialize.
3213 @begin{Reason}
3214 The fact that Initialize is done for subcomponents
3215 first allows Initialize for a composite
3216 object to refer to its subcomponents knowing
3217 they have been properly initialized.
3218
3219 The fact that Initialize is done for components with access
3220 discriminants after other components
3221 allows the Initialize operation for a component
3222 with a self-referential access discriminant to assume
3223 that other components of the enclosing object have already been
3224 properly initialized.
3225 For multiple such components, it allows some predictability.
3226 @end{Reason}
3227
3228 @leading@keepnext@Defn{assignment operation}
3229 When a target object with any controlled parts is assigned a value,
3230 @Redundant[either when created or in a subsequent
3231 @nt{assignment_statement},]
3232 the @i{assignment operation} proceeds as follows:
3233 @begin(itemize)
3234 The value of the target becomes the assigned value.
3235
3236 @Defn{adjusting the value of an object}
3237 @Defn{adjustment}
3238 The value of the target is @i{adjusted.}
3239 @end(itemize)
3240 @begin{Ramification}
3241 If any parts of the object are controlled,
3242 abort is deferred during the assignment operation.
3243 @end{Ramification}
3244
3245 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}@Comment{May need future changes.}
3246 @Defn{adjusting the value of an object}
3247 @Defn{adjustment}
3248 To adjust the value of a
3249 @Chg{Version=[3],New=[],Old=[@Redundant[(nonlimited)] ]}composite object,
3250 the values of the components of the object are first
3251 adjusted in an arbitrary order,
3252 and then, if the object is
3253 @Chg{Version=[3],New=[nonlimited ],Old=[]}controlled,
3254 Adjust is called.@PDefn2{Term=[arbitrary order],Sec=[allowed]}
3255 Adjusting the value of an elementary object has no effect@Redundant[,
3256 nor does adjusting the value of a composite object with no
3257 controlled parts.]
3258 @begin{Ramification}
3259 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
3260 Adjustment is never @Chg{Version=[3],New=[actually ],Old=[]}performed for
3261 values of @Chg{Version=[3],New=[an immutably],Old=[a by-reference]} limited type,
3262 since @Chg{Version=[3],New=[all
3263 assignment operations for such types are required to be built-in-place.
3264 Even so, we still define adjustment for all types in order that the
3265 canonical semantics is well-defined],Old=[these
3266 types do not support copying]}.
3267 @end{Ramification}
3268 @begin{Reason}
3269 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
3270 The verbiage in the Initialize rule about access discriminants
3271 constrained by per-object expressions is not necessary here,
3272 since such types are
3273 @Chg{Version=[3],New=[either ],Old=[]}limited@Chg{Version=[3],New=[ or
3274 do not have defaults, so the discriminant can only be changed by
3275 an assignment to an outer object. Such an assignment could
3276 happen only before any adjustments or (if part of an outer Adjust)
3277 only after any inner (component) adjustments have completed.],
3278 Old=[, and therefore are never adjusted.]}
3279 @end{Reason}
3280
3281 @PDefn2{Term=[execution], Sec=(assignment_statement)}
3282 For an @nt{assignment_statement},
3283 @Redundant[ after the @nt{name} and
3284 @nt{expression} have been evaluated,
3285 and any conversion (including constraint checking) has been done,]
3286 an anonymous object is created, and
3287 the value is assigned into it;
3288 @Redundant[that is, the assignment operation is applied].
3289 @Redundant[(Assignment includes value adjustment.)]
3290 The target of the @nt{assignment_statement} is then finalized.
3291 The value of the anonymous object is then assigned into the target of
3292 the @nt{assignment_statement}.
3293 Finally, the anonymous object is finalized.
3294 @Redundant[As explained below,
3295 the implementation may eliminate the intermediate anonymous object,
3296 so this description subsumes the one given in
3297 @RefSec{Assignment Statements}.]
3298 @begin{Reason}
3299 @leading@;An alternative design for user-defined assignment might involve an
3300 Assign operation instead of Adjust:
3301 @begin{Example}
3302 @key[procedure] Assign(Target : @key[in] @key[out] Controlled; Source : @key[in] @key[out] Controlled);
3303 @end{Example}
3304
3305 @leading@keepnext@;Or perhaps even a syntax like this:
3306 @begin{Example}
3307 @key[procedure] ":="(Target : @key[in] @key[out] Controlled; Source : @key[in] @key[out] Controlled);
3308 @end{Example}
3309
3310 @leading@;Assign (or ":=") would have the responsibility of doing the copy,
3311 as well as whatever else is necessary.
3312 This would have the advantage that the Assign operation knows about both
3313 the target and the source at the same time @em it would be possible to
3314 do things like reuse storage belonging to the target, for example,
3315 which Adjust cannot do.
3316 However, this sort of design would not work in the case of unconstrained
3317 discriminated variables, because there is no way to change the
3318 discriminants individually.
3319 For example:
3320 @begin{Example}
3321 @key[type] Mutable(D : Integer := 0) @key[is]
3322 @key[record]
3323 X : Array_Of_Controlled_Things(1..D);
3324 @key[case] D @key[is]
3325 @key[when] 17 => Y : Controlled_Thing;
3326 @key[when] @key[others] => @key[null];
3327 @key[end] D;
3328 @key[end] @key[record];
3329 @end{Example}
3330
3331 An assignment to an unconstrained variable of type Mutable can cause
3332 some of the components of X, and the component Y, to appear and/or
3333 disappear.
3334 There is no way to write the Assign operation to handle this sort of
3335 case.
3336
3337 Forbidding such cases is not an option @em it would cause generic
3338 contract model violations.
3339 @end{Reason}
3340
3341
3342 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0067-1]}
3343 @ChgAdded{Version=[3],Type=[Leading],Text=[When a function call
3344 or @nt{aggregate} is used to initialize an object, the
3345 result of the function call or @nt{aggregate} is an anonymous object, which
3346 is assigned into the newly-created object. For such an assignment,
3347 the anonymous object might be @i<built in place>,@Defn{built in place}@Seeother{Primary=[build-in-place],Other=[built in place]}
3348 in which case the assignment does not involve any copying.
3349 Under certain circumstances, the anonymous object is required to be built in
3350 place. In particular:]}
3351
3352 @begin{Discussion}
3353 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
3354 @ChgAdded{Version=[3],Text=[We say assignment to built-in-place objects does not
3355 involve copying, which matches the intended implementation (see below). Of
3356 course, the implementation can do any copying it likes, if it can make such
3357 copying semantically invisible (by patching up access values to point to the
3358 copy, and so forth).]}
3359 @end{Discussion}
3360
3361 @begin{Itemize}
3362 @ChgRef{Version=[3],Kind=[Added]}
3363 @ChgAdded{Version=[3],Text=[If the full type of any part of the object is
3364 immutably limited, the anonymous object is built in place.]}
3365
3366 @begin{Reason}
3367 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
3368 @ChgAdded{Version=[3],Text=[We talk about the full types being immutably
3369 limited, as this is independent of the view of a type (in the same way that it
3370 is for determining the technique of parameter passing). That is, privacy is
3371 ignored for this purpose.]}
3372
3373 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0005-1],ARef=[AI05-0067-1]}
3374 @ChgAdded{Version=[3],Text=[For function calls, we only require building in
3375 place for immutably limited types. These are the types that would have been
3376 return-by-reference types in Ada 95. We limited the requirement because
3377 we want to minimize disruption to Ada 95 implementations and users.]}
3378 @end{Reason}
3379
3380 @begin{Honest}
3381 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0232-1]}
3382 @ChgAdded{Version=[3],Text=[This is a dynamic property and is determined by
3383 the specific type of the parts of the actual object. In particular, if a part
3384 has a class-wide type, the tag of the object might need to be examined in
3385 order to determine if build-in-place is required. However, we expect that most
3386 Ada implementations will determine this property at compile-time using some
3387 assume-the-worst algorithm in order to chose the appropriate method to
3388 implement a given call or aggregate. In addition, there is no attribute or
3389 other method for a program to determine if a particular object has this
3390 property (or not), so there is no value to a more careful description of this
3391 rule.]}
3392 @end{Honest}
3393
3394 @ChgRef{Version=[3],Kind=[Added]}
3395 @ChgAdded{Version=[3],Text=[In the case of an @nt{aggregate}, if the full type
3396 of any part of the newly-created object is controlled, the anonymous object is
3397 built in place.]}
3398
3399 @begin{Reason}
3400 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
3401 @ChgAdded{Version=[3],Type=[Leading],Text=[This is necessary to prevent
3402 elaboration problems with deferred constants of controlled types. Consider:]}
3403 @begin{Example}
3404 @ChgRef{Version=[3],Kind=[Added]}
3405 @ChgAdded{Version=[3],Text=[@key[package] P @key[is]
3406 @key[type] Dyn_String @key[is private];
3407 Null_String : @key[constant] Dyn_String;
3408 ...
3409 @key[private]
3410 @key[type] Dyn_String @key[is new] Ada.Finalization.Controlled @key[with] ...
3411 @key[procedure] Finalize(X : @key[in out] Dyn_String);
3412 @key[procedure] Adjust(X : @key[in out] Dyn_String);
3413 @comment{Blank Line}
3414 Null_String : @key[constant] Dyn_String :=
3415 (Ada.Finalization.Controlled @key[with] ...);
3416 ...
3417 @key[end] P;]}
3418 @end{Example}
3419 @ChgRef{Version=[3],Kind=[Added]}
3420 @ChgAdded{Version=[3],Text=[When Null_String is elaborated, the bodies of Finalize and Adjust
3421 clearly have not been elaborated. Without this rule, this declaration would
3422 necessarily raise Program_Error (unless the permissions given below are
3423 used by the implementation).]}
3424 @end{Reason}
3425
3426 @begin{Ramification}
3427 @ChgRef{Version=[3],Kind=[AddedNormal]}
3428 @ChgAdded{Version=[3],Text=[An @nt{aggregate} with a controlled part
3429 used in the return expression
3430 of a @nt{simple_@!return_@!statement} has to be built in place in the anonymous
3431 return object, as this is similar to an object declaration. (This is a change
3432 from Ada 95, but it is not an inconsistency as it only serves to restrict
3433 implementation choices.) But this only covers the @nt{aggregate}; a separate
3434 anonymous return object can still be used unless it too is required to be
3435 built in place.]}
3436
3437 @ChgRef{Version=[3],Kind=[AddedNormal]}
3438 @ChgAdded{Version=[3],Text=[Similarly, an @nt{aggregate} that has
3439 a controlled part but is not itself controlled and that is used to
3440 initialize an object also has to be built in place. This is also a change
3441 from Ada 95, but it is not an inconsistency as it only serves to restrict
3442 implementation choices. This avoids problems if a type like Dyn_String
3443 (in the example above) is used as a component in a type used as a
3444 deferred constant in package P.]}
3445 @end{Ramification}
3446
3447 @ChgRef{Version=[3],Kind=[Added]}
3448 @ChgAdded{Version=[3],Text=[In other cases, it is unspecified whether the
3449 anonymous object is built in place.@PDefn{unspecified}]}
3450
3451 @begin{Reason}
3452 @ChgRef{Version=[3],Kind=[AddedNormal]}
3453 @ChgAdded{Version=[3],Text=[This is left unspecified so the implementation
3454 can use any appropriate criteria for determining when to build in place.
3455 That includes making the decision on a call-by-call basis. Reasonable
3456 programs will not care what decision is made here anyway.]}
3457 @end{Reason}
3458 @end{Itemize}
3459
3460 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0067-1]}
3461 @ChgAdded{Version=[3],Type=[Leading],Text=[Notwithstanding@Defn{notwithstanding}
3462 what this International Standard says elsewhere, if an object is
3463 built in place:]}
3464
3465 @begin{Itemize}
3466 @ChgRef{Version=[3],Kind=[Added]}
3467 @ChgAdded{Version=[3],Text=[Upon successful completion of the return
3468 statement or @nt{aggregate}, the anonymous object @i<mutates into> the
3469 newly-created object; that is, the anonymous object ceases to exist, and the
3470 newly-created object appears in its place.@Defn{mutates}]}
3471
3472 @ChgRef{Version=[3],Kind=[Added]}
3473 @ChgAdded{Version=[3],Text=[Finalization is not performed on the anonymous object.]}
3474
3475 @ChgRef{Version=[3],Kind=[Added]}
3476 @ChgAdded{Version=[3],Text=[Adjustment is not performed on the newly-created
3477 object.]}
3478
3479 @ChgRef{Version=[3],Kind=[Added]}
3480 @ChgAdded{Version=[3],Text=[All access values that designate parts of the
3481 anonymous object now designate the corresponding parts of the newly-created
3482 object.]}
3483
3484 @ChgRef{Version=[3],Kind=[Added]}
3485 @ChgAdded{Version=[3],Text=[All renamings of parts of the anonymous
3486 object now denote views of the corresponding parts of the newly-created
3487 object.]}
3488
3489 @ChgRef{Version=[3],Kind=[Added]}
3490 @ChgAdded{Version=[3],Text=[Coextensions of the anonymous object become
3491 coextensions of the newly-created object.]}
3492 @end{Itemize}
3493
3494 @begin{Honest}
3495 @ChgRef{Version=[3],Kind=[AddedNormal]}
3496 @ChgAdded{Version=[3],Text=[This @ldquote@;mutating@rdquote does not
3497 necessarily happen atomically with
3498 respect to abort and other tasks. For example, if a function call is used as
3499 the parent part of an @nt{extension_aggregate}, then the tag of the anonymous
3500 object (the function result) will be different from the tag of the
3501 newly-created object (the parent part of the @nt{extension_aggregate}). In
3502 implementation terms, this involves modifying the tag field. If the current
3503 task is aborted during this modification, the object might become abnormal.
3504 Likewise, if some other task accesses the tag field during this modification,
3505 it constitutes improper use of shared variables, and is erroneous.]}
3506 @end{Honest}
3507
3508 @begin{ImplNote}
3509 @ChgRef{Version=[3],Kind=[AddedNormal]}
3510 @ChgAdded{Version=[3],Text=[The intended implementation is that the
3511 anonymous object is allocated at the
3512 same address as the newly-created object. Thus, no run-time action is
3513 required to cause all the access values and renamings to point to the right
3514 place. They just point to the newly-created object, which is what the return
3515 object has magically @ldquote@;mutated into@rdquote.]}
3516
3517 @ChgRef{Version=[3],Kind=[AddedNormal]}
3518 @ChgAdded{Version=[3],Text=[There is no requirement that 'Address of the
3519 return object is equal to 'Address of the newly-created object, but that will
3520 be true in the intended implementation.]}
3521
3522 @ChgRef{Version=[3],Kind=[AddedNormal]}
3523 @ChgAdded{Version=[3],Text=[For a function call, if the size of the
3524 newly-created object is known at the call site, the object is allocated there,
3525 and the address is implicitly passed to the function; the return object is
3526 created at that address. Otherwise, a storage pool is implicitly passed to the
3527 function; the size is determined at the point of the return statement, and
3528 passed to the Allocate procedure. The address returned by the storage pool is
3529 returned from the function, and the newly-created object uses that same
3530 address. If the return statement is left without returning (via an exception
3531 or a goto, for example), then Deallocate is called. The storage pool might be
3532 a dummy pool that represents @ldquote@;allocate on the stack@rdquote.]}
3533
3534 @ChgRef{Version=[3],Kind=[AddedNormal]}
3535 @ChgAdded{Version=[3],Text=[The Tag of the newly-created object may be
3536 different from that of the result object. Likewise, the master and
3537 accessibility level may be different.]}
3538
3539 @ChgRef{Version=[3],Kind=[AddedNormal]}
3540 @ChgAdded{Version=[3],Text=[An alternative implementation model might allow
3541 objects to move around to different addresses. In this case, access values and
3542 renamings would need to be modified at run time. It seems that this model
3543 requires the full power of tracing garbage collection.]}
3544 @end{ImplNote}
3545 @end{RunTime}
3546
3547 @begin{ImplReq}
3548 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0022],ARef=[AI95-00083-01]}
3549 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00318-02]}
3550 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg],ARef=[AI05-0067-1]}
3551 @ChgDeleted{Version=[3],Text=[@Chg{Version=[1],New=[For an @nt{aggregate} of a
3552 controlled type whose value is assigned,
3553 other than by an @nt{assignment_statement}@Chg{Version=[2],New=[],Old=[ or
3554 a @nt{return_statement}]}, the
3555 implementation shall not create a separate anonymous object for the
3556 @nt{aggregate}. The @nt{aggregate} value shall be constructed directly in the target
3557 of the assignment operation and Adjust is not called on the target object.],Old=[]}]}
3558 @begin{Reason}
3559 @ChgRef{Version=[1],Kind=[Added]}
3560 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00318-02]}
3561 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg],ARef=[AI05-0067-1]}
3562 @ChgDeleted{Version=[3],Type=[Leading],Text=[@Chg{Version=[1],New=[@Chg{Version=[2],
3563 New=[@PDefn{build-in-place}],Old=[]}This@Chg{Version=[2],
3564 New=[ @i<build-in-place> requirement],Old=[]} is necessary to prevent
3565 elaboration problems with deferred constants of controlled types. Consider:],Old=[]}]}
3566 @begin{Example}
3567 @ChgRef{Version=[1],Kind=[Added]}
3568 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg]}
3569 @ChgDeleted{Version=[3],Text=[@Chg{New=[@key[package] P @key[is]
3570 @key[type] Dyn_String @key[is private];
3571 Null_String : @key[constant] Dyn_String;
3572 ...
3573 @key[private]
3574 @key[type] Dyn_String @key[is new] Ada.Finalization.Controlled @key[with] ...
3575 @key[procedure] Finalize(X : @key[in out] Dyn_String);
3576 @key[procedure] Adjust(X : @key[in out] Dyn_String);
3577 @comment{Blank Line}
3578 Null_String : @key[constant] Dyn_String :=
3579 (Ada.Finalization.Controlled @key[with] ...);
3580 ...
3581 @key[end] P;],Old=[]}]}
3582 @end{Example}
3583 @ChgRef{Version=[1],Kind=[Added]}
3584 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg]}
3585 @ChgDeleted{Version=[3],Text=[@Chg{New=[When Null_String is elaborated, the bodies of Finalize and Adjust
3586 clearly have not been elaborated. Without this rule, this declaration would
3587 necessarily raise Program_Error (unless the permissions given below are
3588 used by the implementation).],Old=[]}]}
3589 @end{Reason}
3590 @begin{Ramification}
3591 @ChgRef{Version=[2],Kind=[AddedNormal]}
3592 @ChgRef{Version=[3],Kind=[DeletedAddedNoDelMsg]}
3593 @ChgDeleted{Version=[3],Text=[@Chg{Version=[2],New=[An @nt{aggregate} of a controlled type
3594 used in the return expression
3595 of a @nt{simple_@!return_@!statement} has to be built-in-place in the anonymous
3596 return object, as this is similar to an object declaration. (This is a change
3597 from Ada 95, but it is not an inconsistency as it only serves to restrict
3598 implementation choices.) But this only covers the @nt{aggregate}; a separate
3599 anonymous return object can still be used unless it too is required to be
3600 built-in-place (see @RefSecNum{Limited Types}).],Old=[]}]}
3601 @end{Ramification}
3602 @end{ImplReq}
3603
3604 @begin{ImplPerm}
3605 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
3606 An implementation is allowed to relax the above rules
3607 @Chg{Version=[3],New=[for @nt{assignment_statement}s],
3608 Old=[@Redundant[(for nonlimited controlled types)]]}
3609 in the following ways:
3610 @begin{TheProof}
3611 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0067-1]}
3612 @ChgDeleted{Version=[3],Text=[The phrase @lquotes@;for nonlimited controlled
3613 types@rquotes@; follows from the fact
3614 that all of the following permissions apply to cases involving
3615 assignment.
3616 It is important because the programmer can count on a stricter semantics
3617 for limited controlled types.]}
3618 @end{TheProof}
3619 @begin{Ramification}
3620 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
3621 @ChgAdded{Version=[3],Text=[The relaxations apply only to nonlimited types,
3622 as @nt{assignment_statement}s
3623 are not allowed for limited types. This is important so that the programmer
3624 can count on a stricter semantics for limited controlled types.]}
3625 @end{Ramification}
3626 @begin{Itemize}
3627 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
3628 @Chg{Version=[3],New=[If],Old=[For an @nt{assignment_statement} that assigns to]}
3629 an object @Chg{Version=[3],New=[is assigned ],Old=[]}the value of that same
3630 object, the implementation need not do anything.
3631 @begin{Ramification}
3632 In other words, even if an object is controlled and a combination
3633 of Finalize and Adjust on the object might have a net
3634 side effect, they need not be performed.
3635 @end{Ramification}
3636
3637 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
3638 For @Chg{Version=[3],New=[assignment of],Old=[an @nt{assignment_statement} for]}
3639 a noncontrolled type,
3640 the implementation may finalize and assign each
3641 component of the variable separately (rather than finalizing the entire
3642 variable and assigning the entire new value)
3643 unless a discriminant of the variable is changed by the assignment.
3644 @begin{Reason}
3645 For example, in a slice assignment, an anonymous object is not necessary
3646 if the slice is copied component-by-component in the right direction,
3647 since array types are not controlled (although their components may be).
3648 Note that the direction, and even the fact that it's a slice assignment,
3649 can in general be determined only at run time.
3650 @end{Reason}
3651 @begin{Ramification}
3652 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0005-1]}
3653 @ChgAdded{Version=[3],Text=[This potentially breaks a single assignment
3654 operation into many, and thus abort deferral (see
3655 @RefSecNum{Abort of a Task - Abort of a Sequence of Statements}) needs to
3656 last only across an individual component assignment when the component
3657 has a controlled part. It is only important that the copy step is
3658 not separated (by an abort) from the adjust step, so aborts between
3659 component assignments is not harmful.]}
3660 @end{Ramification}
3661
3662 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00147-01]}
3663 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0067-1]}
3664 @Chg{Version=[3],New=[The],Old=[For an @nt{aggregate} or function call whose
3665 value is assigned into a target object,
3666 the implementation need not create a separate anonymous object if
3667 it can safely create the value of
3668 the @nt{aggregate} or function call
3669 directly in the target object.
3670 Similarly, for an @nt{assignment_@!statement}, the]}
3671 implementation need not create an anonymous object if
3672 the value being assigned is the result of evaluating a @nt{name}
3673 denoting an object (the source object) whose storage cannot overlap
3674 with the target. If the source object might overlap with the
3675 target object, then the implementation can avoid the need for
3676 an intermediary anonymous object by exercising one of the
3677 above permissions and perform the assignment one component
3678 at a time (for an overlapping array assignment), or not at all
3679 (for an assignment where the target and the source of the assignment are
3680 the same object).@Chg{Version=[2],New=[],Old=[ Even if an anonymous object is
3681 created, the implementation may move its value to the target object as part of
3682 the assignment without re-adjusting so long as the
3683 anonymous object has no aliased subcomponents.]}
3684 @begin{Ramification}
3685 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
3686 @Chg{Version=[3],New=[If the anonymous object is eliminated by this permission, ],
3687 Old=[In the @nt{aggregate} case, only one
3688 value adjustment is necessary, and]} there is no anonymous object to be
3689 finalized@Chg{Version=[3],New=[ and thus the Finalize call on it is
3690 eliminated],Old=[]}.
3691
3692 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00147-01]}
3693 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
3694 @Chg{Version=[2],New=[@Chg{Version=[3],New=[Note that if the anonymous
3695 object is eliminated but the new value is not built in place in the
3696 target object],Old=[Similarly, in the function call case, the
3697 anonymous object can be eliminated. Note, however]}, that Adjust
3698 must be called],Old=[In the @nt{assignment_statement} case as well,
3699 no finalization of the anonymous object is needed.
3700 On the other hand, if the target has aliased subcomponents,
3701 then an adjustment takes place]} directly on the target object
3702 as the last step of the assignment, since some of the
3703 subcomponents may be self-referential or otherwise
3704 position-dependent.@Chg{Version=[2],New=[ This Adjust can be eliminated only
3705 by using one of the following permissions.],Old=[]}
3706 @end{Ramification}
3707 @end{Itemize}
3708
3709 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00147-01]}
3710 @ChgAdded{Version=[2],Type=[Leading],Text=[Furthermore, an implementation is
3711 permitted to
3712 omit implicit Initialize, Adjust, and Finalize calls and associated assignment
3713 operations on an object of a nonlimited controlled type provided that:]}
3714
3715 @begin{Itemize}
3716 @ChgRef{Version=[2],Kind=[AddedNormal]}
3717 @ChgAdded{Version=[2],Text=[any omitted Initialize call is not a call on a
3718 user-defined Initialize procedure, and]}
3719 @begin{Honest}
3720 @ChgRef{Version=[2],Kind=[AddedNormal]}
3721 @ChgAdded{Version=[2],Text=[This does not apply to any calls to a user-defined
3722 Initialize routine that happen to occur in an Adjust or Finalize routine. It
3723 is intended that it is never necessary to look inside of an Adjust or Finalize
3724 routine to determine if the call can be omitted.]}
3725 @end{Honest}
3726 @begin{Reason}
3727 @ChgRef{Version=[2],Kind=[AddedNormal]}
3728 @ChgAdded{Version=[2],Text=[We don't want to eliminate objects for which the
3729 Initialize might have side effects (such as locking a resource).]}
3730 @end{Reason}
3731
3732 @ChgRef{Version=[2],Kind=[AddedNormal]}
3733 @ChgAdded{Version=[2],Text=[any usage of the value of the object after the implicit
3734 Initialize or Adjust call and before any subsequent Finalize call on the object
3735 does not change the external effect of the program, and]}
3736
3737 @ChgRef{Version=[2],Kind=[AddedNormal]}
3738 @ChgAdded{Version=[2],Text=[after the omission of such calls and operations, any
3739 execution of the program that executes an Initialize or Adjust call on an
3740 object or initializes an object by an @nt{aggregate} will also later execute a
3741 Finalize call on the object and will always do so prior to assigning a new
3742 value to the object, and]}
3743
3744 @ChgRef{Version=[2],Kind=[AddedNormal]}
3745 @ChgAdded{Version=[2],Text=[the assignment operations associated with omitted Adjust
3746 calls are also omitted.]}
3747 @end{Itemize}
3748
3749 @ChgRef{Version=[2],Kind=[AddedNormal]}
3750 @ChgAdded{Version=[2],Text=[This permission applies to Adjust and Finalize calls even
3751 if the implicit calls have additional external effects.]}
3752 @begin{Reason}
3753 @ChgRef{Version=[2],Kind=[AddedNormal]}
3754 @ChgAdded{Version=[2],Text=[The goal of the above permissions is to allow
3755 typical dead assignment and dead variable removal algorithms to work for
3756 nonlimited controlled types. We require that @lquotes@;pairs@rquotes@; of
3757 Initialize/Adjust/Finalize operations are removed. (These aren't always pairs,
3758 which is why we talk about @lquotes@;any execution of the program@rquotes@;.)]}
3759 @end{Reason}
3760
3761 @end{ImplPerm}
3762
3763 @begin{Extend83}
3764 @Defn{extensions to Ada 83}
3765 Controlled types and user-defined finalization are new to Ada 95.
3766 (Ada 83 had finalization semantics only for masters of tasks.)
3767 @end{Extend83}
3768
3769 @begin{Extend95}
3770 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00161-01]}
3771 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
3772 @b[Amendment Correction:] Types Controlled and Limited_Controlled now have
3773 Preelaborable_Initialization, so that objects of types derived from these
3774 types can be used in preelaborated packages.]}
3775 @end{Extend95}
3776
3777 @begin{DiffWord95}
3778 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0020],ARef=[AI95-00126-01]}
3779 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified that Ada.Finalization
3780 is a remote types package.]}
3781
3782 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0021],ARef=[AI95-00182-01]}
3783 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Added wording to clarify that
3784 the default initialization (whatever it is) of an ancestor part is used.]}
3785
3786 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0022],ARef=[AI95-00083-01]}
3787 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified that Adjust is never
3788 called on an @nt{aggregate} used for the initialization of an object or
3789 subaggregate, or passed as a parameter.]}
3790
3791 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00147-01]}
3792 @ChgAdded{Version=[2],Text=[Additional optimizations are allowed for
3793 nonlimited controlled types. These allow traditional dead variable
3794 elimination to be applied to such types.]}
3795
3796 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
3797 @ChgAdded{Version=[2],Text=[Corrected the build-in-place requirement
3798 for controlled @nt{aggregate}s to be consistent with the requirements
3799 for limited types.]}
3800
3801 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00348-01]}
3802 @ChgAdded{Version=[2],Text=[The operations of types Controlled and
3803 Limited_Controlled are now declared as null procedures (see
3804 @RefSecNum{Null Procedures}) to make the semantics clear (and to provide
3805 a good example of what null procedures can be used for).]}
3806
3807 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00360-01]}
3808 @ChgAdded{Version=[2],Text=[Types that need finalization are defined; this is
3809 used by the No_Nested_Finalization restriction
3810 (see @RefSec{Tasking Restrictions}).]}
3811
3812 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00373-01]}
3813 @ChgAdded{Version=[2],Text=[Generalized the description of objects that
3814 have Initialize called for them to say that it is done for all objects that
3815 are initialized by default. This is needed so that all of the new cases
3816 are covered.]}
3817 @end{DiffWord95}
3818
3819 @begin{Extend2005}
3820 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0212-1]}
3821 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
3822 Package Ada.Finalization now has Pure categorization, so it can be mentioned
3823 for any package. Note that this does not change the preelaborability of
3824 objects descended from Controlled and Limited_Controlled.]}
3825 @end{Extend2005}
3826
3827 @begin{DiffWord2005}
3828 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0013-1]}
3829 @ChgAdded{Version=[3],Text=[@b<Correction:> Eliminated coextensions
3830 from the @ldquote@;needs finalization@rdquote rules, as this cannot be
3831 determined in general in the compilation unit that declares the type.
3832 (The designated type of the coextension may have been imported as a
3833 limited view.) Uses of @ldquote@;needs finalization@rdquote need to
3834 ensure that coextensions are handled by other means (such as
3835 in No_Nested_Finalization @en see @RefSecNum{Tasking Restrictions})
3836 or that coextensions cannot happen.]}
3837
3838 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0013-1]}
3839 @ChgAdded{Version=[3],Text=[@b<Correction:> Corrected the
3840 @ldquote@;needs finalization@rdquote rules to include class-wide types,
3841 as a future extension can include a part that needs finalization.]}
3842
3843 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0026-1]}
3844 @ChgAdded{Version=[3],Text=[@b<Correction:> Corrected the
3845 @ldquote@;needs finalization@rdquote rules to clearly say that they
3846 ignore privacy.]}
3847
3848 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0067-1]}
3849 @ChgAdded{Version=[3],Text=[@b<Correction:> Changed @ldquote@;built in place@rdquote
3850 to @RuntimeTitle and centralized the rules here. This eliminates the
3851 fiction that built in place is just a combination of a permission and
3852 a requirement; it clearly has noticeable semantic effects. This wording
3853 change is not intended to change the semantics of any correct Ada program.]}
3854 @end{DiffWord2005}
3855
3856
3857 @LabeledSubClause{Completion and Finalization}
3858
3859 @begin{Intro}
3860 @redundant[This subclause defines @i{completion} and @i{leaving} of the execution
3861 of constructs and entities.
3862 A @i{master} is the execution of a construct that
3863 includes finalization of local objects after it is complete
3864 (and after waiting for any local tasks
3865 @em see @RefSecNum(Task Dependence - Termination of Tasks)),
3866 but before leaving.
3867 Other constructs and entities are left immediately upon completion.
3868 @IndexSee{Term=[cleanup],See=(finalization)}
3869 @IndexSee{Term=[destructor],See=(finalization)}]
3870 @end{Intro}
3871
3872 @begin{RunTime}
3873 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
3874 @Defn{completion and leaving (completed and left)}
3875 @Defn2{Term=[completion], Sec=(run-time concept)}
3876 The execution of a construct or entity
3877 is @i{complete} when the end of that execution has been reached,
3878 or when a transfer of control
3879 (see @RefSecNum{Simple and Compound Statements - Sequences of Statements})
3880 causes it to be abandoned.
3881 @Defn{normal completion}
3882 @Defn2{Term=[completion], Sec=(normal)}
3883 @Defn{abnormal completion}
3884 @Defn2{Term=[completion], Sec=(abnormal)}
3885 Completion due to reaching the end of execution,
3886 or due to the transfer of control of an @Chg{Version=[2],
3887 New=[@nt{exit_statement}, return statement, @nt{goto_statement}],
3888 Old=[@ntf{exit_}, @ntf{return_}, @ntf{goto_}]},
3889 or @nt{requeue_statement} or of the
3890 selection of a @nt{terminate_alternative}
3891 is @i{normal completion}.
3892 Completion is @i{abnormal} otherwise @Redundant[@em
3893 when control is transferred out of a construct
3894 due to abort or the raising of an exception].
3895 @begin{Discussion}
3896 Don't confuse the run-time concept of completion with
3897 the compile-time concept of completion
3898 defined in @RefSecNum{Completions of Declarations}.
3899 @end{Discussion}
3900
3901 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01],ARef=[AI95-00416-01]}
3902 @Defn{leaving}
3903 @Defn{left}
3904 After execution of a construct or entity is complete,
3905 it is @i{left},
3906 meaning that execution continues with the next action,
3907 as defined for the execution that is taking place.
3908 @Defn{master}
3909 Leaving an execution happens immediately after its completion,
3910 except in the case of a @i{master}:
3911 the execution of
3912 a @Chg{Version=[2],New=[body other than a @nt{package_body};
3913 the execution of a @nt{statement}; or the evaluation of an
3914 @nt{expression}, @nt{function_call}, or @nt{range} that
3915 is not part of an enclosing @nt{expression}, @nt{function_call}, @nt{range}, or
3916 @nt{simple_@!statement} other than a @nt{simple_@!return_@!statement}],
3917 Old=[@nt{task_body}, a @nt{block_@!statement},
3918 a @nt{subprogram_body}, an @nt{entry_body}, or an @nt{accept_@!statement}]}.
3919 A master is finalized after it is
3920 complete, and before it is left.
3921
3922 @begin{Reason}
3923 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01],ARef=[AI95-00416-01]}
3924 @Chg{Version=[2],New=[@nt{Expression}s and @nt{statement}s
3925 are masters so that objects created by subprogram calls (in @nt{aggregate}s,
3926 @nt{allocator}s for anonymous access-to-object types, and so on) are
3927 finalized and have their tasks awaited before the @nt{expression}s or
3928 @nt{statement}s are left. Note that @nt{expression}s like the @nt{condition}
3929 of an @nt{if_statement} are masters, because they are not enclosed by a
3930 @nt{simple_statement}. Similarly, a @nt{function_call} which is renamed
3931 is a master, as it is not in a @nt{simple_@!statement}.],
3932 Old=[Note that although an @nt{accept_statement} has no
3933 @nt{declarative_part}, it can call functions and evaluate @nt{aggregate}s,
3934 possibly causing anonymous controlled objects to be created,
3935 and we don't want those objects to escape outside the rendezvous.]}
3936
3937 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
3938 @ChgAdded{Version=[2],Text=[We have to include @nt{function_call}s in the
3939 contexts that do not cause masters to occur so that @nt{expression}s
3940 contained in a @nt{function_call} (that is not part of an @nt{expression} or
3941 @nt{simple_statement}) do not individually become masters. We certainly do
3942 not want the parameter @nt{expression}s of a @nt{function_call} to be
3943 separate masters, as they would then be finalized before the function is
3944 called.]}
3945 @end{Reason}
3946 @begin{Ramification}
3947 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
3948 @ChgAdded{Version=[2],Text=[The fact that a @nt{function_call} is a master
3949 does not change the accessibility of the return object denoted by the
3950 @nt{function_call}; that depends on the use of the @nt{function_call}.
3951 The @nt{function_call} is the master
3952 of any short-lived entities (such as @nt{aggregate}s used as parameters of
3953 types with task or controlled parts).]}
3954 @end{Ramification}
3955
3956 @Defn2{Term=[finalization], Sec=(of a master)}
3957 For the @i{finalization} of a master,
3958 dependent tasks are first awaited,
3959 as explained in @RefSecNum{Task Dependence - Termination of Tasks}.
3960 Then each object whose accessibility level is
3961 the same as that of the master is finalized
3962 if the object was successfully initialized and still exists.
3963 @Redundant[These actions are performed whether the master is left
3964 by reaching the last statement or via a transfer of control.]
3965 When a transfer of control causes completion of an execution, each
3966 included master is finalized in order, from innermost outward.
3967 @begin{Ramification}
3968 As explained in @RefSecNum{Operations of Access Types},
3969 the set of objects with the same accessibility level
3970 as that of the master
3971 includes objects declared immediately within the master,
3972 objects declared in nested packages,
3973 objects created by @nt{allocator}s
3974 (if the ultimate ancestor access type is declared in one of those places)
3975 and subcomponents of all of these things.
3976 If an object was already finalized by Unchecked_Deallocation,
3977 then it is not finalized again when the master is left.
3978
3979 Note that any object whose accessibility level is deeper than that of
3980 the master would no longer exist;
3981 those objects would have been finalized by some inner master.
3982 Thus, after leaving a master, the only objects yet to be
3983 finalized are those whose accessibility level is less deep than that
3984 of the master.
3985
3986 @end{Ramification}
3987 @begin{Honest}
3988 Subcomponents of objects due to be finalized are not finalized
3989 by the finalization of the master;
3990 they are finalized by the finalization of the containing object.
3991 @end{Honest}
3992 @begin{Reason}
3993 We need to finalize subcomponents of objects even if the containing
3994 object is not going to get finalized because it was not fully
3995 initialized.
3996 But if the containing object is finalized, we don't want to require
3997 repeated finalization of the subcomponents,
3998 as might normally be implied by the recursion in finalization of a
3999 master and the recursion in finalization of an object.
4000 @end{Reason}
4001 @begin{Honest}
4002 Formally, completion and leaving refer to executions of constructs or
4003 entities.
4004 However, the standard sometimes (informally) refers
4005 to the constructs or entities whose executions are being completed.
4006 Thus, for example,
4007 @lquotes@;the subprogram call or task is complete@rquotes@;
4008 really means
4009 @lquotes@;@i{the execution of} the subprogram call or task is complete.@rquotes@;
4010 @end{Honest}
4011
4012 @leading@keepnext@RootDefn2{Term=[finalization], Sec=(of an object)}
4013 For the @i{finalization} of an object:
4014 @begin{Itemize}
4015 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0099-1]}
4016 If @Chg{Version=[3],New=[the full type of ],Old=[]}the object is
4017 @Chg{Version=[3],New=[],Old=[of ]}an elementary type, finalization
4018 has no effect;
4019 @begin{Reason}
4020 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0099-1]}
4021 @ChgAdded{Version=[3],Text=[We say @ldquote@;full type@rdquote in this
4022 and the following bullets as privacy is ignored for the purpose of
4023 determining the finalization actions of an object; that is as expected
4024 for @RunTimeTitle rules.]}
4025 @end{Reason}
4026
4027 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0099-1]}
4028 If @Chg{Version=[3],New=[the full type of ],Old=[]}the object is
4029 @Chg{Version=[3],New=[a tagged type, and the tag of the object
4030 identifies],Old=[of]} a controlled type,
4031 the Finalize procedure @Chg{Version=[3],New=[of that controlled
4032 type ],Old=[]}is called;
4033
4034 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0099-1]}
4035 If @Chg{Version=[3],New=[the full type of ],Old=[]}the object is
4036 @Chg{Version=[3],New=[],Old=[of ]}a protected type,
4037 @Chg{Version=[3],New=[or if the full type of the object is a tagged type
4038 and the tag of the object identifies a protected type, ],Old=[]}the
4039 actions defined in @RefSecNum{Protected Units and Protected Objects} are performed;
4040
4041 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00416-01]}
4042 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0099-1]}
4043 If @Chg{Version=[3],New=[the full type of ],Old=[]}the object is
4044 @Chg{Version=[3],New=[],Old=[of ]}a composite type, then after performing
4045 the above actions, if any, every component of the object
4046 is finalized in an arbitrary order, except as follows:@PDefn2{Term=[arbitrary order],Sec=[allowed]}
4047 if the object has
4048 a component with an access discriminant constrained by a per-object
4049 expression, this component is finalized before any components that
4050 do not have such discriminants; for an object with several components
4051 with such a discriminant, they are finalized in the
4052 reverse of the order of their
4053 @nt<component_declaration>s@Chg{Version=[2],New=[;],Old=[.]}
4054 @begin{Reason}
4055 This allows the finalization of a component with an access discriminant
4056 to refer to other components of the enclosing object prior to
4057 their being finalized.
4058 @end{Reason}
4059 @begin{Honest}
4060 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0099-1]}
4061 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0005-1]}
4062 @ChgAdded{Version=[3],Text=[The components discussed here are all of the
4063 components that the object actually has, not just those components that are
4064 statically identified by the type of the object. These can be different if
4065 the object has a @Chg{Version=[4],New=[class-wide],Old=[classwide]} type.]}
4066 @end{Honest}
4067
4068 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00416-01]}
4069 @ChgAdded{Version=[2],Text=[If the object has coextensions (see
4070 @RefSecNum{Operations of Access Types}), each coextension is
4071 finalized after the object whose access discriminant designates it.]}
4072
4073 @begin{Ramification}
4074 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0066-1]}
4075 @ChgAdded{Version=[3],Text=[In the case of an @nt{aggregate} or function call that is used
4076 (in its entirety) to directly initialize a part of an object, the
4077 coextensions of the result of evaluating the @nt{aggregate} or function
4078 call are transfered to become coextensions of the object being
4079 initialized and are not finalized until the object being initialized
4080 is ultimately finalized, even if an anonymous object is created
4081 as part of the operation.]}
4082 @end{Ramification}
4083
4084 @end{Itemize}
4085
4086 @PDefn2{Term=[execution], Sec=(instance of Unchecked_Deallocation)}
4087 Immediately before an instance of Unchecked_Deallocation
4088 reclaims the storage of an object,
4089 the object is finalized.
4090 @Redundant[If an instance of Unchecked_Deallocation is never applied to
4091 an object created by an @nt{allocator},
4092 the object will still exist when the corresponding master completes,
4093 and it will be finalized then.]
4094
4095 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00280-01]}
4096 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0051-1],ARef=[AI05-0190-1]}
4097 The @Chg{Version=[3],New=[],Old=[order in which the ]}finalization of a master
4098 performs finalization of objects @Chg{Version=[3],New=[],Old=[is as follows:
4099 Objects ]}created by declarations in the master
4100 @Chg{Version=[3],New=[],Old=[are finalized ]}in the reverse order of their
4101 creation.@Chg{Version=[3],New=[],Old=[ For objects that were created by
4102 @nt{allocator}s for an access type whose
4103 ultimate ancestor is declared in the master,
4104 this rule is applied as though each
4105 such object that still exists had been created
4106 in an arbitrary order
4107 at the first freezing point
4108 (see @RefSecNum{Freezing Rules})@PDefn2{Term=[arbitrary order],Sec=[allowed]}
4109 of the ultimate ancestor type@Chg{Version=[2],New=[; the finalization of
4110 these objects is called the @i<finalization of the
4111 collection>@Defn{finalization of the collection}@Defn2{Term=[collection],
4112 Sec=[finalization of]}],Old=[]}.@Chg{Version=[3],New=[ Objects created by
4113 allocators for an anonymous access type that are not coextensions of some other
4114 object, are finalized in an arbitrary order during the finalization of their
4115 associated master.@PDefn2{Term=[arbitrary order],Sec=[allowed]}],Old=[]}]}@Chg{Version=[2],
4116 New=[ After the finalization of a master is complete, the objects finalized
4117 as part of its finalization cease to @i<exist>, as do any types and subtypes
4118 defined and created within the master.@PDefn2{Term=[exist],Sec=[cease to]}
4119 @PDefn2{Term=[cease to exist],Sec=[object]}
4120 @Defn2{Term=[cease to exist],Sec=[type]}],Old=[]}
4121 @begin{Reason}
4122 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4123 @ChgDeleted{Version=[3],Text=[Note that we talk about the type of the
4124 @nt{allocator} here. There may be access values of a (general) access type
4125 pointing at objects created by @nt{allocator}s for some other type; these are
4126 not finalized at this point.]}
4127
4128 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4129 @ChgDeleted{Version=[3],Text=[The freezing point of the ultimate ancestor access type is chosen
4130 because before that point, pool elements cannot be created,
4131 and after that point, access values designating (parts of)
4132 the pool elements can be created.
4133 This is also the point after which the pool object cannot have been
4134 declared.
4135 We don't want to finalize the pool elements until after anything
4136 finalizing objects that contain access values designating them.
4137 Nor do we want to finalize pool elements after finalizing the pool
4138 object itself.]}
4139 @end{Reason}
4140 @begin{Ramification}
4141 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4142 @ChgDeleted{Version=[3],Text=[Finalization of allocated objects is done according
4143 to the (ultimate ancestor) @nt{allocator} type, not according to the storage pool
4144 in which they are allocated.
4145 Pool finalization might reclaim storage (see @RefSec{Storage Management}),
4146 but has nothing (directly) to do with finalization of the
4147 pool elements.]}
4148
4149 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4150 @ChgDeleted{Version=[3],Text=[Note that finalization is done only for objects
4151 that still exist; if an instance of Unchecked_Deallocation has already gotten
4152 rid of a given pool element, that pool element will not be finalized when
4153 the master is left.]}
4154
4155 Note that a deferred constant declaration does not create the
4156 constant; the full constant declaration creates it.
4157 Therefore, the order of finalization depends on where the full
4158 constant declaration occurs,
4159 not the deferred constant declaration.
4160
4161 An imported object is not created by its declaration.
4162 It is neither initialized nor finalized.
4163 @end{Ramification}
4164 @begin{ImplNote}
4165 An implementation has to ensure that the storage for an object is not
4166 reclaimed when references to the object are still possible
4167 (unless, of course, the user explicitly requests reclamation via an
4168 instance of Unchecked_Deallocation).
4169 This implies, in general, that objects cannot be deallocated one by one
4170 as they are finalized;
4171 a subsequent finalization might reference an object that has been
4172 finalized, and that object had better be in its (well-defined)
4173 finalized state.
4174 @end{ImplNote}
4175
4176 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0190-1]}
4177 @ChgAdded{Version=[3],Type=[Leading],Text=[Each nonderived access type @i<T> has
4178 an associated @i<collection>,@Defn2{Term=[collection],Sec=[of an access type]}
4179 which is the set of objects created by @nt{allocator}s of @i<T>,
4180 or of types derived from @i<T>. Unchecked_Deallocation removes an object from
4181 its collection. Finalization of a collection consists of finalization of each
4182 object in the collection, in an arbitrary order. The collection of an access
4183 type is an object implicitly declared at the following place:@PDefn2{Term=[arbitrary order],Sec=[allowed]}]}
4184
4185 @begin{Ramification}
4186 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}
4187 @ChgAdded{Version=[3],Text=[The place of the implicit declaration determines
4188 when allocated objects are finalized. For multiple collections declared at the
4189 same place, we do not define the order of their implicit declarations.]}
4190
4191 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4192 @ChgAdded{Version=[3],Text=[Finalization of allocated objects is done according
4193 to the (ultimate ancestor) @nt{allocator} type, not according to the storage
4194 pool in which they are allocated. Pool finalization might reclaim storage (see
4195 @RefSec{Storage Management}), but has nothing (directly) to do with
4196 finalization of the pool elements.]}
4197
4198 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4199 @ChgAdded{Version=[3],Text=[Note that finalization is done only for objects that
4200 still exist; if an instance of Unchecked_Deallocation has already gotten rid
4201 of a given pool element, that pool element will not be finalized when the
4202 master is left.]}
4203 @end{Ramification}
4204 @begin{Reason}
4205 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4206 @ChgAdded{Version=[3],Text=[Note that we talk about the type of the
4207 @nt{allocator} here. There may be access values of a (general) access type
4208 pointing at objects created by @nt{allocator}s for some other type; these are
4209 not (necessarily) finalized at this point.]}
4210 @end{Reason}
4211
4212 @begin{Itemize}
4213 @ChgRef{Version=[3],Kind=[Added]}
4214 @ChgAdded{Version=[3],Text=[For a named access type, the first freezing point (see
4215 @RefSecNum{Freezing Rules}) of the type.]}
4216
4217 @begin{Reason}
4218 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0190-1]}@ChgNote{Moved down}
4219 @ChgAdded{Version=[3],Text=[The freezing point of the ultimate ancestor access
4220 type is chosen because before that point, pool elements cannot be created,
4221 and after that point, access values designating (parts of)
4222 the pool elements can be created.
4223 This is also the point after which the pool object cannot have been declared.
4224 We don't want to finalize the pool elements until after anything
4225 finalizing objects that contain access values designating them.
4226 Nor do we want to finalize pool elements after finalizing the pool
4227 object itself.]}
4228 @end{Reason}
4229
4230 @ChgRef{Version=[3],Kind=[Added]}
4231 @ChgAdded{Version=[3],Text=[For the type of an access parameter, the call that
4232 contains the @nt{allocator}.]}
4233
4234 @ChgRef{Version=[3],Kind=[Added]}
4235 @ChgAdded{Version=[3],Text=[For the type of an access result, within the master of
4236 the call (see @RefSecNum{Operations of Access Types}).]}
4237 @begin{Honest}
4238 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0005-1],ARef=[AI05-0190-1]}
4239 @ChgAdded{Version=[3],Text=[We mean at a place within the master consistent
4240 with the execution of the call within the master. We don't say that
4241 normatively, as it is difficult to explain that when the master of the call
4242 need not be the master that immediately includes the call (such as when an
4243 anonymous result is converted to a named access type).]}
4244 @end{Honest}
4245
4246 @ChgRef{Version=[3],Kind=[Added]}
4247 @ChgAdded{Version=[3],Text=[For any other anonymous access type, the first
4248 freezing point of the innermost enclosing declaration.]}
4249 @end{Itemize}
4250
4251
4252 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
4253 @PDefn2{Term=[execution], Sec=(assignment_statement)}
4254 The target of an @Chg{Version=[2],New=[@nt{assignment_statement}],
4255 Old=[assignment statement]} is finalized before copying in the
4256 new value, as explained
4257 in @RefSecNum{Assignment and Finalization}.
4258
4259 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0021],ARef=[AI95-00182-01]}
4260 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01]}
4261 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0066-1],ARef=[AI05-0142-4],ARef=[AI05-0269-1]}
4262 @Chg{Version=[2],New=[The master of an object is the master enclosing its
4263 creation whose accessibility level (see @RefSecNum{Operations of Access Types})
4264 is equal to that of the object@Chg{Version=[3],New=[, except
4265 in the case of an anonymous object representing the result of
4266 an @nt{aggregate} or function call. If such an anonymous object
4267 is part of the result of evaluating the actual parameter expression for
4268 an explicitly aliased
4269 parameter of a function call, the master of the object is the innermost
4270 master enclosing the evaluation of the @nt{aggregate} or function call, excluding
4271 the @nt{aggregate} or function call itself. Otherwise, the master of such
4272 an anonymous object is the innermost master enclosing the evaluation of the
4273 @nt{aggregate} or function call, which may be the @nt{aggregate} or function
4274 call itself],Old=[]}.],
4275 Old=[@Chg{New=[If the @i{object_}@nt{name} in an @nt{object_renaming_declaration}, or
4276 the actual parameter for a generic formal @key[in out] parameter in a
4277 @nt{generic_instantiation}, denotes any part of an anonymous object created by
4278 a function call, the anonymous object is not finalized until after it is no
4279 longer accessible via any name. Otherwise, an],
4280 Old=[The]} anonymous object@Chg{New=[],Old=[s]} created by
4281 @Chg{New=[a ],Old=[]}function @Chg{New=[call or],Old=[calls and]} by
4282 @Chg{New=[an ],Old=[]}@nt{aggregate}@Chg{New=[ is],Old=[s are]}
4283 finalized no later than the end of the innermost enclosing
4284 @nt{declarative_item} or @nt{statement};
4285 if that is a @nt{compound_statement},
4286 @Chg{New=[the object is],Old=[they are]} finalized before starting the
4287 execution of any @nt{statement} within the @nt{compound_statement}.]}
4288 @begin{Honest}
4289 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00162-01]}@ChgNote{Should be ChgDeleted}
4290 @ChgDeleted{Version=[2],Text=[@leading@;This is not to be construed as permission to call Finalize
4291 asynchronously with respect to normal user code.
4292 For example,]}
4293 @begin{Example}
4294 @ChgRef{Version=[2],Kind=[Deleted]}
4295 @ChgDeleted{Version=[2],Text=[@key[declare]
4296 X : Some_Controlled_Type := F(G(...));
4297 --@RI{ The anonymous objects created for F and G are finalized}
4298 --@RI{ no later than this point.}
4299 Y : ...
4300 @key[begin]
4301 ...
4302 @key[end];]}
4303 @end{Example}
4304
4305 @ChgRef{Version=[2],Kind=[Deleted]}
4306 @ChgDeleted{Version=[2],Text=[
4307 The anonymous object for G should not be finalized at some random
4308 point in the middle of the body of F,
4309 because F might manipulate the same data structures as
4310 the Finalize operation, resulting in erroneous access
4311 to shared variables.]}
4312 @end{Honest}
4313 @begin{Reason}
4314 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01]}
4315 @Chg{Version=[2],New=[This effectively imports all of the special rules for
4316 the accessibility level of renames, @nt{allocator}s, and so on, and applies
4317 them to determine where objects created in them are finalized. For instance,
4318 the master of a rename of a subprogram is that of the renamed subprogram.],
4319 Old=[It might be quite inconvenient for the implementation to defer
4320 finalization of the anonymous object for G until after copying the
4321 value of F into X, especially if the size of the result
4322 is not known at the call site.]}
4323
4324 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0066-1]}
4325 @ChgAdded{Version=[3],Text=[In @RefSecNum{Operations of Access Types}
4326 we assign an accessibility level to the result of an
4327 @nt{aggregate} or function call that is used to directly initialize a
4328 part of an object based on the object being initialized. This is
4329 important to ensure that any access discriminants denote objects
4330 that live at least as long as the object being initialized.
4331 However, if the result of the @nt{aggregate} or function call is not
4332 built directly in the target object, but instead is built in an
4333 anonymous object that is then assigned to the target, the anonymous
4334 object needs to be finalized after the assignment rather than
4335 persisting until the target object is finalized (but not its
4336 coextensions). (Note than an implementation is never required to
4337 create such an anonymous object, and in some cases is required to
4338 @i{not} have such a separate object, but rather to build the result
4339 directly in the target.)]}
4340
4341 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0142-4]}
4342 @ChgAdded{Version=[3],Text=[The special case for explicitly aliased parameters
4343 of functions is needed for the same reason, as access discriminants of the
4344 returned object may designate one of these parameters. In that case, we want to
4345 lengthen the lifetime of the anonymous objects as long as the possible lifetime
4346 of the result.]}
4347
4348 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0142-4]}
4349 @ChgAdded{Version=[3],Text=[We don't do a similar change for other kinds of
4350 calls, because the extended lifetime of the parameters adds no value, but could
4351 constitute a storage leak. For instance, such an anonymous object created by a
4352 procedure call in the elaboration part of a package body would have to live
4353 until the end of the program, even though it could not be used after the
4354 procedure returns (other than via Unchecked_Access).]}
4355 @end{Reason}
4356
4357 @begin{Ramification}
4358 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0142-4]}
4359 @ChgAdded{Version=[3],Text=[Note that the lifetime of the master given to
4360 anonymous objects in explicitly aliased parameters of functions is not
4361 necessarily as long as the lifetime of the master of the object being
4362 initialized (if the function call is used to initialize an @nt{allocator}, for
4363 instance). In that case, the accessibility check on explicitly aliased
4364 parameters will necessarily fail if any such anonymous objects exist. This is
4365 necessary to avoid requiring the objects to live as long as the access type or
4366 having the implementation complexity of an implicit coextension.]}
4367 @end{Ramification}
4368
4369
4370 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
4371 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00162-01]}
4372 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0066-1],ARef=[AI05-0262-1]}
4373 @Chg{Version=[2],New=[In the case of an @nt{expression} that is a master,
4374 finalization of any (anonymous) objects occurs @Chg{Version=[3],New=[after
4375 completing],Old=[as the final part of]}
4376 evaluation of the @nt{expression}@Chg{Version=[3],New=[ and all use of the
4377 objects, prior to starting the execution of any subsequent construct],Old=[]}.],
4378 Old=[@Chg{New=[If a transfer of control or raising of an exception occurs prior to
4379 performing a finalization of an anonymous object, the anonymous object is
4380 finalized as part of the finalizations due to be performed for the object's
4381 innermost enclosing master.],Old=[]}]}
4382
4383 @end{RunTime}
4384
4385 @begin{Bounded}
4386 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0023],ARef=[AI95-00169-01]}
4387 @leading@PDefn2{Term=(bounded error),Sec=(cause)}
4388 It is a bounded error for a call on Finalize or Adjust @Chg{New=[that occurs as
4389 part of object finalization or assignment ], Old=[]}to propagate an exception.
4390 The possible consequences depend on what action invoked the Finalize or
4391 Adjust operation:
4392 @begin{Ramification}
4393 It is not a bounded error for Initialize to propagate an
4394 exception. If Initialize propagates an exception,
4395 then no further calls on Initialize are performed,
4396 and those components that have already been initialized
4397 (either explicitly or by default)
4398 are finalized in the usual way.
4399
4400 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
4401 @Chg{New=[It also is not a bounded error for an explicit call to Finalize or
4402 Adjust to propagate an exception. We do not want implementations to have to
4403 treat explicit calls to these routines specially.],Old=[]}
4404 @end{Ramification}
4405 @begin{Itemize}
4406 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
4407 For a Finalize invoked as part of an @nt<assignment_statement>,
4408 Program_Error is raised at that point.
4409
4410 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0024],ARef=[AI95-00193-01]}
4411 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
4412 @Chg{New=[For an Adjust invoked as part of @Chg{Version=[2],New=[assignment
4413 operations other than those invoked as part of an @nt{assignment_statement}],
4414 Old=[the initialization of a controlled object]}, other adjustments due to be
4415 performed might or might not be performed, and then Program_Error is raised.
4416 During its propagation, finalization might or
4417 might not be applied to objects whose Adjust failed.],Old=[]}
4418 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
4419 For an Adjust invoked as part of an @Chg{Version=[2],New=[@nt{assignment_statement}],
4420 Old=[assignment @Chg{New=[statement],Old=[operation]}]}, any other adjustments
4421 due to be performed are performed, and then Program_Error is raised.
4422 @begin{Reason}
4423 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0024],ARef=[AI95-00193-01]}
4424 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
4425 @ChgAdded{Version=[1],Text=[In the case of assignments that are part of
4426 initialization, there is
4427 no need to complete all adjustments if one propagates an exception, as the
4428 object will immediately be finalized. So long as a subcomponent is not going
4429 to be finalized, it need not be adjusted, even if it is initialized as part of
4430 an enclosing composite assignment operation for which some adjustments are
4431 performed. However, there is no harm in an implementation making additional
4432 Adjust calls (as long as any additional components that are adjusted are also
4433 finalized), so we allow the implementation flexibility here.
4434 On the other hand, for an @Chg{Version=[2],New=[@nt{assignment_statement}],
4435 Old=[assignment statement]}, it is important that all
4436 adjustments be performed, even if one fails, because all controlled
4437 subcomponents are going to be finalized.@Chg{Version=[2],New=[ Other kinds of
4438 assignment are more like initialization than @nt{assignment_statement}s, so we
4439 include them as well in the permission.],Old=[]}]}
4440 @end{Reason}
4441 @begin{Ramification}
4442 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0024],ARef=[AI95-00193-01]}
4443 @ChgAdded{Version=[1],Text=[Even if an Adjust invoked as part of the
4444 initialization of a
4445 controlled object propagates an exception, objects whose initialization
4446 (including any Adjust or Initialize calls) successfully completed will be
4447 finalized. The permission above only applies to objects whose Adjust failed.
4448 Objects for which Adjust was never even invoked must not be finalized.]}
4449 @end{Ramification}
4450
4451 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
4452 For a Finalize invoked as part of a call on an instance of
4453 Unchecked_Deallocation, any other finalizations due to
4454 be performed are performed, and then Program_Error is raised.
4455 @begin{Discussion}
4456 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0104],ARef=[AI95-00179-01]}
4457 @ChgAdded{Version=[1],Text=[The standard does not specify if storage is
4458 recovered in this case.
4459 If storage is not recovered (and the object continues to exist), Finalize
4460 may be called on the object again (when the @nt<allocator>'s master is
4461 finalized).]}
4462 @end{Discussion}
4463
4464 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
4465 @ChgRef{Version=[3],Kind=[DeletedAdded],ARef=[AI05-0064-1]}
4466 @ChgDeleted{Version=[3],Text=[@Chg{New=[@Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
4467 For a Finalize invoked as part of the finalization of the anonymous
4468 object created by a function call or @nt{aggregate}, any other finalizations
4469 due to be performed are performed, and then Program_Error is raised.],Old=[]}]}
4470
4471 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
4472 @Chg{New=[@Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
4473 For a Finalize invoked due to reaching the end of the execution of a
4474 master, any other finalizations associated with the master are performed, and
4475 Program_Error is raised immediately after leaving the master.],Old=[]}
4476
4477 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0064-1]}
4478 @begin{Discussion}
4479 @ChgAdded{Version=[3],Text=[This rule covers both ordinary objects created
4480 by a declaration, and anonymous objects created as part of evaluating an
4481 @nt{expression}. All contexts that create objects that need finalization
4482 are defined to be masters.]}
4483 @end{Discussion}
4484
4485 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
4486 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
4487 For a Finalize invoked by the transfer of control of
4488 an @Chg{Version=[2],
4489 New=[@nt{exit_statement}, return statement, @nt{goto_statement}],
4490 Old=[@ntf{exit_}, @ntf{return_}, @ntf{goto_}]},
4491 or @nt{requeue_@!statement},
4492 Program_Error is raised no earlier than after the finalization of the
4493 master being finalized when the exception occurred,
4494 and no later than the point where normal execution would have continued.
4495 Any other finalizations due to be performed up to that point are
4496 performed before raising Program_Error.
4497 @begin{Ramification}
4498 For example, upon leaving a @nt{block_statement} due to a
4499 @nt{goto_statement}, the Program_Error would be raised at the point of the
4500 target statement denoted by the label,
4501 or else in some more dynamically nested place,
4502 but not so nested as to allow an @nt{exception_handler} that has
4503 visibility upon the finalized object to handle it.
4504 For example,
4505 @begin{Example}
4506 @key[procedure] Main @key[is]
4507 @key[begin]
4508 <<The_Label>>
4509 Outer_Block_Statement : @key[declare]
4510 X : Some_Controlled_Type;
4511 @key[begin]
4512 Inner_Block_Statement : @key[declare]
4513 Y : Some_Controlled_Type;
4514 Z : Some_Controlled_Type;
4515 @key[begin]
4516 @key[goto] The_Label;
4517 @key[exception]
4518 @key[when] Program_Error => ... --@RI{ Handler number 1.}
4519 @key[end];
4520 @key[exception]
4521 @key[when] Program_Error => ... --@RI{ Handler number 2.}
4522 @key[end];
4523 @key[exception]
4524 @key[when] Program_Error => ... --@RI{ Handler number 3.}
4525 @key[end] Main;
4526 @end{Example}
4527
4528 The @nt{goto_statement} will first cause
4529 Finalize(Y) to be called.
4530 Suppose that Finalize(Y) propagates an exception.
4531 Program_Error will be raised after leaving Inner_Block_Statement,
4532 but before leaving Main.
4533 Thus, handler number 1 cannot handle this Program_Error;
4534 it will be handled either by handler number 2 or handler number 3.
4535 If it is handled by handler number 2,
4536 then Finalize(Z) will be done before executing the handler.
4537 If it is handled by handler number 3,
4538 then Finalize(Z) and Finalize(X) will both
4539 be done before executing the handler.
4540 @end{Ramification}
4541
4542 For a Finalize invoked by a transfer of control
4543 that is due to raising an exception,
4544 any other finalizations due to be performed for the same master are
4545 performed; Program_Error is raised immediately after leaving the master.
4546 @begin{Ramification}
4547 If, in the above example, the @nt{goto_statement} were replaced by a
4548 @nt{raise_statement}, then the Program_Error would be handled by
4549 handler number 2, and
4550 Finalize(Z) would be done before executing the handler.
4551 @end{Ramification}
4552 @begin{Reason}
4553 We considered treating this case in the same way as the others,
4554 but that would render certain @nt{exception_handler}s useless.
4555 For example, suppose the only @nt{exception_handler} is
4556 one for @key{others} in the main subprogram.
4557 If some deeply nested call raises an exception,
4558 causing some Finalize operation to be called,
4559 which then raises an exception,
4560 then normal execution @lquotes@;would have continued@rquotes@;
4561 at the beginning of the @nt{exception_handler}.
4562 Raising Program_Error at that point would cause that
4563 handler's code to be skipped.
4564 One would need two nested @nt{exception_handler}s
4565 to be sure of catching such cases!
4566
4567 On the other hand, the @nt{exception_handler} for a given master
4568 should not be allowed to handle exceptions raised during finalization
4569 of that master.
4570 @end{Reason}
4571
4572 For a Finalize invoked by a transfer of control due to
4573 an abort or selection of a terminate alternative,
4574 the exception is ignored;
4575 any other finalizations due to be performed are performed.
4576 @begin{Ramification}
4577 This case includes an asynchronous transfer of control.
4578 @end{Ramification}
4579 @begin{Honest}
4580 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
4581 This violates the general principle that it is always possible for
4582 a bounded error to raise Program_Error
4583 (see @RefSec{Classification of Errors}).
4584 @end{Honest}
4585 @end{Itemize}
4586 @end{Bounded}
4587
4588 @begin{ImplPerm}
4589 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0107-1]}
4590 @ChgAdded{Version=[3],Text=[If the execution of an @nt{allocator} propagates an
4591 exception, any parts of the allocated object that were successfully initialized
4592 may be finalized as part of the finalization of the innermost master enclosing
4593 the @nt{allocator}.]}
4594
4595 @begin{Reason}
4596 @ChgRef{Version=[3],Kind=[AddedNormal]}
4597 @ChgAdded{Version=[3],Text=[This allows deallocating the memory for the
4598 allocated object at the innermost master, preventing a storage leak.
4599 Otherwise, the object would have to stay around until the finalization of the
4600 collection that it belongs to, which could be the entire life of the program
4601 if the associated access type is library level.]}
4602 @end{Reason}
4603
4604 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0111-3],ARef=[AI05-0262-1]}
4605 @ChgAdded{Version=[3],Text=[The implementation may finalize objects created by
4606 @nt{allocator}s for an access type whose storage pool supports subpools (see
4607 @RefSecNum{Storage Subpools}) as if the objects were created (in an arbitrary
4608 order) at the point where the storage pool was elaborated instead of at the first
4609 freezing point of the access type.@PDefn2{Term=[arbitrary order],Sec=[allowed]}]}
4610
4611 @begin{Ramification}
4612 @ChgRef{Version=[3],Kind=[AddedNormal]}
4613 @ChgAdded{Version=[3],Text=[This allows the finalization of such objects to
4614 occur later than they otherwise would, but still as part of the finalization
4615 of the same master. Accessibility rules in @RefSecNum{Storage Subpools} ensure
4616 that it is the same master (usually that of the environment task).]}
4617 @end{Ramification}
4618
4619 @begin{ImplNote}
4620 @ChgRef{Version=[3],Kind=[AddedNormal]}
4621 @ChgAdded{Version=[3],Text=[This permission is intended to allow the allocated
4622 objects to "belong" to the subpool objects and to allow those objects to be
4623 finalized at the time that the storage pool is finalized (if they are not
4624 finalized earlier). This is expected to ease implementation, as the objects
4625 will only need to belong to the subpool and not also to the collection.]}
4626 @end{ImplNote}
4627
4628 @end{ImplPerm}
4629
4630
4631 @begin{Notes}
4632 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
4633 The rules of @Chg{Version=[3],New=[Clause],Old=[Section]} 10 imply that
4634 immediately prior to partition termination, Finalize operations
4635 are applied to library-level controlled objects (including those
4636 created by @nt{allocator}s of library-level access types, except those
4637 already finalized).
4638 This occurs after waiting for library-level
4639 tasks to terminate.
4640 @begin{Discussion}
4641 We considered defining a pragma that would apply to a controlled
4642 type that would suppress Finalize operations for library-level objects
4643 of the type upon partition termination.
4644 This would be useful for types whose finalization actions
4645 consist of simply reclaiming global heap storage, when this is already
4646 provided automatically by the environment upon program termination.
4647 @end{Discussion}
4648
4649 A constant is only constant between its initialization and
4650 finalization.
4651 Both initialization and finalization are allowed to
4652 change the value of a constant.
4653
4654 Abort is deferred during certain operations related to controlled types,
4655 as explained in
4656 @RefSecNum{Abort of a Task - Abort of a Sequence of Statements}.
4657 Those rules prevent an abort from causing a controlled object
4658 to be left in an ill-defined state.
4659
4660 The Finalize procedure is called upon finalization of
4661 a controlled object, even if Finalize was called earlier,
4662 either explicitly or as part of an assignment; hence,
4663 if a controlled type is visibly controlled (implying that its Finalize
4664 primitive is directly callable), or is nonlimited (implying that
4665 assignment is allowed), its Finalize procedure should be
4666 designed to have no ill effect if it is applied a second time
4667 to the same object.
4668 @begin{Discussion}
4669 Or equivalently, a Finalize procedure
4670 should be @lquotes@;idempotent@rquotes@;;
4671 applying it twice to the same object should be equivalent to
4672 applying it once.
4673 @end{Discussion}
4674 @begin{Reason}
4675 A user-written Finalize procedure should be idempotent since it
4676 can be called explicitly
4677 by a client (at least if the type is "visibly" controlled).
4678 Also, Finalize is used implicitly as part of the
4679 @nt<assignment_statement> if the
4680 type is nonlimited, and an abort is permitted to disrupt an
4681 @nt<assignment_statement> between finalizing the left-hand side
4682 and assigning the new value to it (an abort is not permitted to
4683 disrupt an assignment operation between copying in the new value
4684 and adjusting it).
4685 @end{Reason}
4686 @begin{Discussion}
4687 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
4688 Either Initialize or Adjust,
4689 but not both, is applied to (almost) every controlled object
4690 when it is created:
4691 Initialize is done when no initial value is assigned to the object,
4692 whereas Adjust is done as part of assigning the initial value.
4693 The one exception is the @Chg{Version=[2],New=[],Old=[anonymous ]}object
4694 @Chg{Version=[2],New=[initialized],Old=[created]} by an
4695 @nt{aggregate}@Chg{Version=[2],New=[ (both the anonymous object created for
4696 an aggregate, or an object initialized by an @nt{aggregate} that is
4697 built-in-place)],Old=[]}; Initialize is not applied to the @nt{aggregate}
4698 as a whole, nor is the value of the @nt<aggregate> @Chg{Version=[2],
4699 New=[or object ],Old=[]}adjusted.
4700
4701 @leading@Defn2{Term=[assignment operation], Sec=(list of uses)}
4702 All of the following use the assignment operation,
4703 and thus perform value adjustment:
4704 @begin{Itemize}
4705 the @nt{assignment_statement} (see @RefSecNum{Assignment Statements});
4706
4707 explicit initialization of a stand-alone object
4708 (see @RefSecNum{Object Declarations})
4709 or of a pool element (see @RefSecNum{Allocators});
4710
4711 default initialization of a component of a stand-alone object
4712 or pool element
4713 (in this case, the value of each component is assigned, and therefore adjusted,
4714 but the value of the object as a whole is not adjusted);
4715
4716 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
4717 function return, when the result @Chg{Version=[2],New=[is not built-in-place],
4718 Old=[type is not a return-by-reference
4719 type (see @RefSecNum{Return Statements});]}
4720 (adjustment of the result happens before finalization of
4721 the function@Chg{Version=[2],New=[],Old=[;
4722 values of return-by-reference types are not
4723 adjusted]});
4724
4725 predefined operators (although the only one that matters is
4726 concatenation; see @RefSecNum{Binary Adding Operators});
4727
4728 generic formal objects of mode @key{in}
4729 (see @RefSecNum{Formal Objects});
4730 these are defined in terms of constant declarations; and
4731
4732 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
4733 @nt{aggregate}s (see @RefSecNum{Aggregates})@Chg{Version=[2],New=[, when
4734 the result is not built-in-place],Old=[]}
4735 (in this case, the value of each component, and the parent part, for an
4736 @nt{extension_aggregate}, is assigned, and therefore adjusted,
4737 but the value of the @nt{aggregate} as a whole is not adjusted;
4738 neither is Initialize called);
4739 @end{Itemize}
4740
4741 @leading@;The following also use the assignment operation,
4742 but adjustment never does anything interesting in these cases:
4743 @begin{Itemize}
4744 By-copy parameter passing uses the assignment operation
4745 (see @RefSecNum{Parameter Associations}),
4746 but controlled objects are always passed by reference,
4747 so the assignment operation never does anything interesting in
4748 this case.
4749 If we were to allow by-copy parameter passing for controlled objects,
4750 we would need to make sure that the actual is finalized before doing
4751 the copy back for [@key{in}] @key{out} parameters.
4752 The finalization of the parameter itself needs to happen
4753 after the copy back (if any),
4754 similar to the finalization of an anonymous function return
4755 object or @nt{aggregate} object.
4756
4757 @key{For} loops use the assignment operation
4758 (see @RefSecNum{Loop Statements}), but since the type
4759 of the loop parameter is never controlled,
4760 nothing interesting happens there, either.
4761
4762 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00318-02]}
4763 @ChgAdded{Version=[2],Text=[Objects initialized by function results and
4764 @nt{aggregate}s that are built-in-place. In this case, the assignment
4765 operation is never executed, and no adjustment takes place. While
4766 built-in-place
4767 is always allowed, it is required for some types @em see
4768 @RefSecNum{Limited Types} and
4769 @RefSecNum{Assignment and Finalization} @em and that's
4770 important since limited types have no Adjust to call.]}
4771 @end{Itemize}
4772
4773 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00287-01]}
4774 @ChgDeleted{Version=[2],Text=[Because Controlled and Limited_Controlled
4775 are library-level tagged types,
4776 all controlled types will be library-level types,
4777 because of the accessibility rules
4778 (see @RefSecNum{Operations of Access Types} and @RefSecNum{Type Extensions}).
4779 This ensures that the Finalize operations may be applied without
4780 providing any @lquotes@;display@rquotes@; or @lquotes@;static-link.@rquotes@;
4781 This simplifies finalization as a result of garbage collection,
4782 abort, and asynchronous transfer of control.]}
4783
4784 Finalization of the parts of a protected object are not done as
4785 protected actions.
4786 It is possible (in pathological cases)
4787 to create tasks during finalization that
4788 access these parts in parallel with the finalization itself.
4789 This is an erroneous use of shared variables.
4790 @end{Discussion}
4791 @begin{ImplNote}
4792 One implementation technique for finalization is to chain the
4793 controlled objects together on a per-task list.
4794 When leaving a master, the list can be walked up to a marked place.
4795 The links needed to implement the list can be declared (privately)
4796 in types Controlled and Limited_Controlled,
4797 so they will be inherited by all controlled types.
4798
4799 Another implementation technique, which we refer to as the @lquotes@;PC-map@rquotes@;
4800 approach essentially implies inserting exception handlers at various
4801 places, and finalizing objects based on where the exception was
4802 raised.
4803
4804 @Defn{PC-map approach to finalization}
4805 @Defn{program-counter-map approach to finalization}
4806 The PC-map approach is for the compiler/linker to create a map of
4807 code addresses; when an exception is raised, or abort occurs,
4808 the map can be consulted to see where the task was executing,
4809 and what finalization needs to be performed.
4810 This approach was given in the Ada 83 Rationale as a
4811 possible implementation strategy for exception handling @em the
4812 map is consulted to determine which exception handler applies.
4813
4814 If the PC-map approach is used, the implementation must take care in the
4815 case of arrays. The generated code will generally contain a loop to
4816 initialize an array. If an exception is raised part way through the
4817 array, the components that have been initialized must be finalized,
4818 and the others must not be finalized.
4819
4820 It is our intention that both of these implementation methods should
4821 be possible.
4822 @end{ImplNote}
4823 @end{Notes}
4824
4825 @begin{DiffWord83}
4826 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
4827 Finalization depends on the concepts of completion and leaving,
4828 and on the concept of a master.
4829 Therefore, we have moved the definitions of these concepts here,
4830 from where they used to be in
4831 @Chg{Version=[3],New=[Clause],Old=[Section]} @RefSecNum{Tasks and Synchronization}.
4832 These concepts also needed to be generalized somewhat.
4833 Task waiting is closely related to user-defined finalization;
4834 the rules here refer to the task-waiting rules of
4835 @Chg{Version=[3],New=[Clause],Old=[Section]} @RefSecNum{Tasks and Synchronization}.
4836 @end{DiffWord83}
4837
4838 @begin{Inconsistent95}
4839 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0066-1]}
4840 @ChgAdded{Version=[3],Text=[@Defn{inconsistencies with Ada 95}@b<Ada 2012 Correction:>
4841 Changed the definition
4842 of the master of an anonymous object used to directly initialize an
4843 object, so it can be finalized immediately rather than having to hang
4844 around as long as the object. In this case, the Ada 2005 definition was
4845 inconsistent with Ada 95, and Ada 2012 changes it back. It is unlikely
4846 that many compilers implemented the rule as written in Amendment 1,
4847 so an inconsistency is unlikely to arise in practice.]}
4848 @end{Inconsistent95}
4849
4850 @begin{DiffWord95}
4851 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0021],ARef=[AI95-00182-01]}
4852 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Fixed the wording to say that
4853 anonymous objects aren't finalized until the object can't be used anymore.]}
4854
4855 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0023],ARef=[AI95-00169-01]}
4856 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Added wording to clarify what
4857 happens when Adjust or Finalize raises an exception; some cases had been
4858 omitted.]}
4859
4860 @ChgRef{Version=[1],Kind=[AddedNormal],Ref=[8652/0024],ARef=[AI95-00193-01]}
4861 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
4862 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Stated that if Adjust raises an
4863 exception during initialization, nothing further is required. This is
4864 corrected in Ada 2005 to include all kinds of assignment other than
4865 @nt{assignment_statement}s.]}
4866
4867 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00162-01],ARef=[AI95-00416-01]}
4868 @ChgAdded{Version=[2],Text=[Revised the definition of master to include
4869 @nt{expression}s and @nt{statement}s, in order to cleanly define what
4870 happens for tasks and controlled objects created as part of a subprogram call.
4871 Having done that, all of the special wording to cover those cases is
4872 eliminated (at least until the Ada comments start rolling in).]}
4873
4874 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00280-01]}
4875 @ChgAdded{Version=[2],Text=[We define @i{finalization of the collection}
4876 here, so as to be able to conveniently refer to it in other rules (especially
4877 in @RefSec{Allocators}).]}
4878
4879 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
4880 @ChgAdded{Version=[2],Text=[Clarified that a coextension is finalized at
4881 the same time as the outer object. (This was intended for Ada 95, but since
4882 the concept did not have a name, it was overlooked.)]}
4883 @end{DiffWord95}
4884
4885 @begin{Inconsistent2005}
4886 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0051-1],ARef=[AI05-0190-1]}
4887 @ChgAdded{Version=[3],Text=[@Defn{inconsistencies with Ada 2005}@b<Correction:>
4888 Better defined when objects allocated from anonymous access types are
4889 finalized. This could be inconsistent if objects are finalized in
4890 a different order than in an Ada 2005 implementation and that order caused
4891 different program behavior; however programs that depend on the order of
4892 finalization within a single master are already fragile and hopefully
4893 are rare.]}
4894 @end{Inconsistent2005}
4895
4896 @begin{DiffWord2005}
4897 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0064-1]}
4898 @ChgAdded{Version=[3],Text=[@b<Correction:> Removed a redundant rule,
4899 which is now covered by the additional places where masters are defined.]}
4900
4901 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0099-1]}
4902 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0005-1]}
4903 @ChgAdded{Version=[3],Text=[@b<Correction:> Clarified the finalization
4904 rules so that there is no doubt that privacy is ignored, and to ensure
4905 that objects of @Chg{Version=[4],New=[class-wide],Old=[classwide]} interface
4906 types are finalized based on their specific concrete type.]}
4907
4908 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0107-1]}
4909 @ChgAdded{Version=[3],Text=[@b<Correction:> Allowed premature finalization
4910 of parts of failed @nt{allocator}s. This could be an inconsistency, but the
4911 previous behavior is still allowed and there is no requirement that
4912 implementations take advantage of the permission.]}
4913
4914 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0111-3]}
4915 @ChgAdded{Version=[3],Text=[Added a permission to finalize an object allocated
4916 from a subpool later than usual.]}
4917
4918 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0142-4]}
4919 @ChgAdded{Version=[3],Text=[Added text to specially define the master of
4920 anonymous objects which are passed as explicitly aliased parameters (see
4921 @RefSecNum{Subprogram Declarations}) of functions. The model for these
4922 parameters is explained in detail in @RefSecNum{Parameter Associations}.]}
4923 @end{DiffWord2005}
4924