Debian Codegraph @

source_2005/07.mss

raw
1 @Part(07, Root="ada.mss")
2
3 @Comment{$Date: 2006/10/18 00:25:24 $}
4 @LabeledSection{Packages}
5
6 @Comment{$Source: e:\\cvsroot/ARM/Source/07.mss,v $}
7 @Comment{$Revision: 1.83 $}
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 @Syn{lhs=<package_specification>,rhs="
38 @key{package} @Syn2{defining_program_unit_name} @key{is}
39 {@Syn2{basic_declarative_item}}
40 [@key{private}
41 {@Syn2{basic_declarative_item}}]
42 @key{end} [[@Syn2{parent_unit_name}.]@Syn2{identifier}]"}
43
44 @begin{SyntaxText}
45 If an @nt{identifier} or @nt{parent_unit_name}.@nt{identifier}
46 appears at the end of a @nt{package_specification},
47 then this sequence of lexical elements shall repeat the
48 @nt{defining_program_unit_name}.
49 @end{SyntaxText}
50 @end{Syntax}
51
52 @begin{Legality}
53 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00434-01]}
54 @PDefn2{Term=[requires a completion], Sec=(@nt{package_declaration})}
55 @PDefn2{Term=[requires a completion], Sec=(@nt{generic_package_declaration})}
56 A @nt{package_declaration} or @nt{generic_package_declaration}
57 requires a completion @Redundant[(a body)]
58 if it contains any @Chg{Version=[2],New=[@nt{basic_declarative_item}],
59 Old=[@nt<declarative_item>]} that requires a completion,
60 but whose completion is not in its @nt{package_specification}.
61 @begin(Honest)
62 If an implementation supports it, a @nt{pragma} Import may substitute
63 for the body of a package or generic package.
64 @end(Honest)
65 @end{Legality}
66
67 @begin{StaticSem}
68 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00420-01],ARef=[AI95-00434-01]}
69 @PDefn2{Term=[visible part], Sec=<of a package
70 (other than a generic formal package)>}
71 The first list of @Chg{Version=[2],New=[@nt{basic_declarative_item}s],
72 Old=[@nt{declarative_item}s]} of a
73 @nt{package_specification} of a package other than a generic formal package
74 is called the @i{visible part} of the package.
75 @Redundant[@PDefn2{Term=[private part], Sec=(of a package)}
76 The optional list of @Chg{Version=[2],New=[@nt{basic_declarative_item}s],
77 Old=[@nt{declarative_item}s]} after the reserved word
78 @key{private} (of any @nt{package_specification}) is called the
79 @i{private part} of the package.
80 If the reserved
81 word @key{private} does not appear, the package has an implicit empty
82 private part.]@Chg{Version=[2],New=[ Each list of @nt{basic_declarative_item}s
83 of a @nt{package_specification} forms a @i{declaration list} of the
84 package.@PDefn2{Term=[declaration list],Sec=(package_specification)}],Old=[]}
85
86 @begin{Ramification}
87 This definition of visible part does not apply to generic formal
88 packages @em @RefSecNum{Formal Packages} defines
89 the visible part of a generic formal package.
90
91 The implicit empty private part is important because certain
92 implicit declarations occur there if the package is a child package,
93 and it defines types in its visible part that are derived from,
94 or contain as components, private types declared within the
95 parent package. These implicit declarations are visible
96 in children of the child package.
97 See @RefSecNum(Compilation Units - Library Units).
98 @end{Ramification}
99
100 @redundant[An entity declared in the private part of a package is visible
101 only within the declarative region of the package itself
102 (including any child units @em
103 see @RefSecNum{Compilation Units - Library Units}).
104 In contrast, expanded names denoting
105 entities declared in the visible part can be used even outside the
106 package; furthermore, direct visibility of such entities can be
107 achieved by means of @nt{use_clause}s
108 (see @RefSecNum{Selected Components} and @RefSecNum{Use Clauses}).]
109 @end{StaticSem}
110
111 @begin{RunTime}
112 @PDefn2{Term=[elaboration], Sec=(package_declaration)}
113 The elaboration of a @nt{package_declaration} consists of the elaboration of
114 its @nt{basic_declarative_item}s in the given order.
115 @end{RunTime}
116
117 @begin{Notes}
118 The visible part of a package contains all the information that
119 another program unit is able to know about the package.
120
121 If a declaration occurs immediately within the specification
122 of a package, and the declaration has a corresponding completion that
123 is a body,
124 then that body has to occur immediately within the body of
125 the package.
126 @begin{TheProof}
127 This follows from the fact that the declaration and completion are
128 required to occur
129 immediately within the same declarative region,
130 and the fact that @ntf{bodies} are disallowed (by the @SyntaxName@;s)
131 in @nt{package_specification}s.
132 This does not apply to instances of generic units,
133 whose bodies can occur in @nt{package_specification}s.
134 @end{TheProof}
135 @end{Notes}
136
137 @begin{Examples}
138 @leading@keepnext@i{Example of a package declaration:}
139 @begin{Example}
140 @key[package] Rational_Numbers @key[is]
141
142 @key[type] Rational @key[is]
143 @key[record]
144 Numerator : Integer;
145 Denominator : Positive;
146 @key[end] @key[record];
147
148 @key[function] "="(X,Y : Rational) @key[return] Boolean;
149
150 @key[function] "/" (X,Y : Integer) @key[return] Rational; --@RI{ to construct a rational number}
151
152 @key[function] "+" (X,Y : Rational) @key[return] Rational;
153 @key[function] "-" (X,Y : Rational) @key[return] Rational;
154 @key[function] "*" (X,Y : Rational) @key[return] Rational;
155 @key[function] "/" (X,Y : Rational) @key[return] Rational;
156 @key[end] Rational_Numbers;
157 @end{Example}
158
159 There are also many examples of package declarations in the predefined
160 language environment
161 (see @RefSecNum{Predefined Language Environment}).
162 @end{Examples}
163
164 @begin{Incompatible83}
165 @Defn{incompatibilities with Ada 83}
166 In Ada 83, a library package is allowed to have a body even if
167 it doesn't need one.
168 In Ada 95, a library package body is either
169 required or forbidden @em never optional.
170 The workaround is to add @key[pragma] Elaborate_Body,
171 or something else requiring a body,
172 to each library package that has a body that isn't otherwise
173 required.
174 @end{Incompatible83}
175
176 @begin{DiffWord83}
177 We have moved the syntax into this clause and the next clause from
178 RM83-7.1, @lquotes@;Package Structure@rquotes@;, which we have removed.
179
180 RM83 was unclear on the rules about when a package requires a body.
181 For example, RM83-7.1(4) and RM83-7.1(8) clearly forgot about the
182 case of an incomplete type declared in a @nt{package_declaration} but
183 completed in the body.
184 In addition,
185 RM83 forgot to make this rule apply to a generic package.
186 We have corrected these rules.
187 Finally, since we now allow a @nt{pragma} Import for any explicit
188 declaration, the completion rules need to take this into account as
189 well.
190 @end{DiffWord83}
191
192 @begin{DiffWord95}
193 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00420-01]}
194 @ChgAdded{Version=[2],Text=[Defined @lquotes@;declaration list@rquotes
195 to avoid ambiguity in other rules as to whether packages are included.]}
196 @end{DiffWord95}
197
198 @LabeledClause{Package Bodies}
199
200 @begin{Intro}
201 @redundant[In contrast to the entities declared in the visible part of a
202 package, the entities declared in the @nt{package_body} are
203 visible only
204 within the @nt{package_body} itself.
205 As a consequence, a
206 package with a @nt{package_body} can be used for the construction of
207 a group of related subprograms in
208 which the logical operations available to clients are clearly
209 isolated from the internal entities.]
210 @end{Intro}
211
212 @begin{Syntax}
213 @Syn{lhs=<package_body>,rhs="
214 @key{package} @key{body} @Syn2{defining_program_unit_name} @key{is}
215 @Syn2{declarative_part}
216 [@key{begin}
217 @Syn2{handled_sequence_of_statements}]
218 @key{end} [[@Syn2{parent_unit_name}.]@Syn2{identifier}];"}
219
220 @begin{SyntaxText}
221 If an @nt{identifier} or @nt{parent_unit_name}.@nt{identifier}
222 appears at the end of a @nt{package_body},
223 then this sequence of lexical elements shall repeat the
224 @nt{defining_program_unit_name}.
225 @end{SyntaxText}
226 @end{Syntax}
227
228 @begin{Legality}
229 A @nt{package_body} shall be the completion of a previous
230 @nt{package_@!declaration} or @nt{generic_@!package_@!declaration}.
231 A library @nt{package_@!declaration} or
232 library @nt{generic_@!package_@!declaration}
233 shall not have a body
234 unless it requires a body@Redundant[;
235 @key<pragma> Elaborate_Body can be used to require
236 a @nt<library_@!unit_@!declaration> to have a body
237 (see @RefSecNum{Elaboration Control})
238 if it would not otherwise require one].
239 @begin{Ramification}
240 The first part of the rule forbids a @nt{package_body} from
241 standing alone @em it has to belong to some previous
242 @nt{package_declaration} or @nt{generic_package_declaration}.
243
244 A nonlibrary @nt{package_declaration} or
245 nonlibrary @nt<generic_package_declaration>
246 that does not require a completion may
247 have a corresponding body anyway.
248 @end{Ramification}
249 @end{Legality}
250
251 @begin{StaticSem}
252 In any @nt{package_body} without @nt{statement}s
253 there is an implicit @nt{null_@!statement}.
254 For any @nt{package_@!declaration} without an explicit completion,
255 there is an implicit @nt{package_@!body} containing a single
256 @nt{null_statement}.
257 For a noninstance, nonlibrary package,
258 this body occurs at the end of the
259 @nt{declarative_@!part} of the innermost enclosing program unit or
260 @nt{block_@!statement};
261 if there are several such packages,
262 the order of the implicit @ntf{package_@!bodies} is unspecified.
263 @PDefn{unspecified}
264 @Redundant[(For an instance, the implicit @nt{package_@!body}
265 occurs at the place of the instantiation
266 (see @RefSecNum{Generic Instantiation}).
267 For a library package, the place is partially determined by the
268 elaboration dependences (see Section 10).)]
269 @begin{Discussion}
270 Thus, for example, we can refer to something happening just
271 after the @key{begin} of a @nt{package_body},
272 and we can refer to the @nt{handled_sequence_of_statements}
273 of a @nt{package_body},
274 without worrying about all the optional pieces.
275 The place of the implicit body makes a difference for tasks
276 activated by the package.
277 See also RM83-9.3(5).
278
279 The implicit body would be illegal if explicit in the case of a library
280 package that does not require (and therefore does not allow)
281 a body.
282 This is a bit strange, but not harmful.
283 @end{Discussion}
284 @end{StaticSem}
285
286 @begin{RunTime}
287 @PDefn2{Term=[elaboration], Sec=(nongeneric package_body)}
288 For the elaboration of a nongeneric @nt{package_body},
289 its @nt{declarative_@!part} is first
290 elaborated, and its @nt{handled_@!sequence_of_@!statements} is then executed.
291 @end{RunTime}
292
293 @begin{Notes}
294 A variable declared in the body of a package is only visible
295 within this body and, consequently, its value can only be
296 changed within the @nt{package_body}. In the absence of local tasks,
297 the value of such a variable remains unchanged between calls issued
298 from outside the package to subprograms declared in the visible part.
299 The properties of such a variable are similar to those of a @lquotes@;static@rquotes@;
300 variable of C.
301
302 The elaboration of the body of a subprogram explicitly declared
303 in the visible part of a package is caused by the elaboration of the
304 body of the package. Hence a call of such a subprogram by an
305 outside program unit raises the exception Program_Error if the call
306 takes place before the elaboration of the
307 @nt{package_body} (see @RefSecNum{Declarative Parts}).
308 @end{Notes}
309
310 @begin{Examples}
311 @leading@keepnext@i{Example of a package body
312 (see @RefSecNum{Package Specifications and Declarations}):}
313 @begin{Example}
314 @key[package] @key[body] Rational_Numbers @key[is]
315
316 @key[procedure] Same_Denominator (X,Y : @key[in] @key[out] Rational) @key[is]
317 @key[begin]
318 --@RI{ reduces X and Y to the same denominator:}
319 ...
320 @key[end] Same_Denominator;
321
322 @key[function] "="(X,Y : Rational) @key[return] Boolean @key[is]
323 U : Rational := X;
324 V : Rational := Y;
325 @key[begin]
326 Same_Denominator (U,V);
327 @key[return] U.Numerator = V.Numerator;
328 @key[end] "=";
329
330 @key[function] "/" (X,Y : Integer) @key[return] Rational @key[is]
331 @key[begin]
332 @key[if] Y > 0 @key[then]
333 @key[return] (Numerator => X, Denominator => Y);
334 @key[else]
335 @key[return] (Numerator => -X, Denominator => -Y);
336 @key[end] @key[if];
337 @key[end] "/";
338
339 @key[function] "+" (X,Y : Rational) @key[return] Rational @key[is] ... @key[end] "+";
340 @key[function] "-" (X,Y : Rational) @key[return] Rational @key[is] ... @key[end] "-";
341 @key[function] "*" (X,Y : Rational) @key[return] Rational @key[is] ... @key[end] "*";
342 @key[function] "/" (X,Y : Rational) @key[return] Rational @key[is] ... @key[end] "/";
343
344 @key[end] Rational_Numbers;
345 @end{Example}
346 @end{Examples}
347
348 @begin{DiffWord83}
349 The syntax rule for @nt{package_body} now uses the syntactic category
350 @nt{handled_sequence_of_statements}.
351
352 The @nt{declarative_part} of a @nt{package_body} is now required;
353 that doesn't make any real difference,
354 since a @nt{declarative_part} can be empty.
355
356 RM83 seems to have forgotten to say that a @nt{package_body} can't
357 stand alone, without a previous declaration.
358 We state that rule here.
359
360 RM83 forgot to restrict the definition of elaboration of
361 @ntf{package_bodies} to nongeneric ones.
362 We have corrected that omission.
363
364 The rule about implicit bodies (from RM83-9.3(5))
365 is moved here, since it is more generally applicable.
366 @end{DiffWord83}
367
368 @LabeledClause{Private Types and Private Extensions}
369
370 @begin{Intro}
371 @redundant[The declaration (in the visible part of a
372 package) of a type as a private type or private extension
373 serves to separate the characteristics that can be used
374 directly by outside program units (that is, the logical properties)
375 from other characteristics whose direct use is confined to the
376 package (the details of the definition of the type itself).
377 See @RefSecNum(Type Extensions) for an overview of type extensions.
378 @Defn{private types and private extensions}
379 @IndexSee{Term=[information hiding],See=(private types and private extensions)}
380 @IndexSee{Term=[opaque type],See=(private types and private extensions)}
381 @IndexSee{Term=[abstract data type (ADT)],See=(private types and private extensions)}
382 @IndexSee{Term=[ADT (abstract data type)],See=(private types and private extensions)}]
383 @end{Intro}
384
385 @begin{MetaRules}
386 A private (untagged) type can be thought of as a record type
387 with the type of its single (hidden) component being the full view.
388
389 A private tagged type can be thought of as a private extension
390 of an anonymous parent with no components. The only
391 dispatching operation of the parent is equality
392 (although the Size attribute, and, if nonlimited, assignment are allowed,
393 and those will presumably be implemented in terms of dispatching).
394 @end{MetaRules}
395
396 @begin{Syntax}
397 @Syn{lhs=<private_type_declaration>,rhs="
398 @key{type} @Syn2{defining_identifier} [@Syn2{discriminant_part}] @key{is} [[@key{abstract}] @key{tagged}] [@key{limited}] @key{private};"}
399
400 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
401 @Syn{lhs=<private_extension_declaration>,rhs="
402 @key{type} @Syn2{defining_identifier} [@Syn2{discriminant_part}] @key{is}
403 [@key{abstract}] @Chg{Version=[2],New=<[@key{limited} | @key{synchronized}]>,Old=[]} @key{new} @SynI(ancestor_)@Syn2{subtype_indication}@Chg{Version=[2],New=<
404 [@key{and} @Syn2[interface_list]]>,Old=<>} @key{with private};"}
405 @end{Syntax}
406
407 @begin{Legality}
408 @ChgNote{We don't mark this as a change, since it only involves the AARM}
409 @Defn2{Term=[partial view], Sec=(of a type)}
410 @PDefn2{Term=[requires a completion], Sec=(declaration of a partial view)}
411 A @nt<private_type_declaration>
412 or @nt<private_extension_declaration>
413 declares a @i{partial view} of the type;
414 such a declaration is allowed only as a
415 @nt{declarative_item} of the visible part of a package,
416 and it requires a completion,
417 which shall be a @nt{full_type_declaration} that occurs as
418 a @nt{declarative_item} of the private part of the package.
419 @Chg{Version=[2],New=[],Old=[@Defn2{Term=[full view], Sec=(of a type)}]}
420 @Redundant[@ChgNote{This really should be a change, but that's too hard.}
421 The view of the type declared by the @nt<full_type_declaration>
422 is called the @i(full view).]
423 A generic formal private type or a
424 generic formal private extension is also a partial view.
425 @begin(Honest)
426 A private type can also be completed by a @nt{pragma}
427 Import, if supported by an implementation.
428 @end(Honest)
429 @begin{Reason}
430 We originally used the term @lquotes@;private view,@rquotes@; but this was easily
431 confused with the view provided @i(from) the private part, namely the
432 full view.
433 @end{Reason}
434 @begin{TheProof}
435 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00326-01]}
436 @ChgAdded{Version=[2],Text=[Full view is now defined in
437 @RefSec{Type Declarations}, as all types now have them.]}
438 @end{TheProof}
439
440 @Redundant[A type shall be completely defined before it is frozen
441 (see @RefSecNum{Completions of Declarations} and
442 @RefSecNum{Freezing Rules}).
443 Thus, neither the declaration
444 of a variable of a partial view of a type, nor the creation by an
445 @nt{allocator} of an object of the partial view are allowed before
446 the full declaration of the type.
447 Similarly, before the full declaration, the name of the partial view
448 cannot be used in a @nt{generic_instantiation} or in a
449 representation item.]
450 @begin{TheProof}
451 This rule is stated officially in
452 @RefSec{Completions of Declarations}.
453 @end{TheProof}
454
455
456
457 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
458 @Redundant[A private type is limited if its declaration includes
459 the reserved word @key[limited];
460 a private extension is limited if its ancestor type is @Chg{Version=[2],
461 New=[a limited type that is not an interface type, or if the reserved word
462 @key{limited} or @key{synchronized} appears in its
463 definition],Old=[limited]}.]
464 If the partial view is nonlimited, then
465 the full view shall be nonlimited.
466 If a tagged partial view is limited,
467 then the full view shall be limited.
468 @Redundant[On the other hand,
469 if an untagged partial view is limited,
470 the full view may be limited or nonlimited.]
471
472 If the partial view is tagged,
473 then the full view shall be tagged.
474 @Redundant[On the other hand, if the partial view is untagged,
475 then the full view may be tagged or untagged.]
476 In the case where the partial view is untagged and the full view is
477 tagged,
478 no derivatives of the partial view are allowed within the immediate
479 scope of the partial view;
480 @Redundant[derivatives of the full view are allowed.]
481 @begin{Ramification}
482 Note that deriving from a partial view within its immediate scope
483 can only occur in a package that is a child of the one where the partial
484 view is declared.
485 The rule implies that in the visible part of a public child package,
486 it is impossible to derive from an untagged private type declared in the
487 visible part of the parent package in the case where the full
488 view of the parent type turns out to be tagged.
489 We considered a model in which the derived type was implicitly
490 redeclared at the earliest place within its immediate scope where
491 characteristics needed to be added.
492 However, we rejected that model, because (1) it would imply that (for an
493 untagged type) subprograms explicitly declared after the derived type
494 could be inherited, and (2) to make this model work for composite types
495 as well, several implicit redeclarations would be
496 needed, since new characteristics can become visible one by one;
497 that seemed like too much mechanism.
498 @end{Ramification}
499 @begin{Discussion}
500 The rule for tagged partial views
501 is redundant for partial views that are private extensions,
502 since all extensions of a given ancestor tagged type are tagged,
503 and limited if the ancestor is limited.
504 We phrase this rule partially redundantly to keep its structure parallel
505 with the other rules.
506 @end{Discussion}
507 @begin{Honest}
508 This rule is checked in a generic unit,
509 rather than using the @lquotes@;assume the best@rquotes@; or @lquotes@;assume the worst@rquotes@;
510 method.
511 @end{Honest}
512 @begin{Reason}
513 @leading@;@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}Tagged
514 limited private types have certain capabilities that are
515 incompatible with having assignment for the full view of the type.
516 In particular, tagged limited private types can be extended
517 with @Chg{Version=[2],New=[],Old=[access discriminants and ]}components
518 of a limited type, which works only because assignment is not allowed.
519 Consider the following example:
520 @begin{Example}
521 @key[package] P1 @key[is]
522 @key[type] T1 @key[is] @key[tagged] @key[limited] @key[private];
523 @key[procedure] Foo(X : @key[in] T1'Class);
524 @key[private]
525 @key[type] T1 @key[is] @key[tagged] @key[null] @key[record]; --@RI{ Illegal!}
526 --@RI{ This should say @lquotes@;@key[tagged limited null record]@rquotes@;.}
527 @key[end] P1;
528
529 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{Presentation AI-00114}
530 @key[package] @key[body] P1 @key[is]
531 @key[type] A @key[is] @key[access] T1'Class;
532 Global : A;
533 @key[procedure] Foo(X : @key[in] T1'Class) @key[is]
534 @key[begin]
535 Global := @key[new] T1'Class'(X);
536 --@RI{ This would be illegal if the full view of}
537 --@RI{ T1 were limited, like it's supposed to be.}
538 @key[end] @Chg{New=[Foo],Old=[A]};
539 @key[end] P1;
540
541 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}@key[with] P1;
542 @key[package] P2 @key[is]
543 @key[type] T2(D : @key[access] Integer)@Chg{Version=[2],New=[],Old=[ --@RI{ Trouble!}]}
544 @key[is] @key[new] P1.T1 @key[with]
545 @key[record]
546 My_Task : Some_Task_Type; --@RI{ @Chg{Version=[2],New=[Trouble],Old=[More trouble]}!}
547 @key[end] @key[record];
548 @key[end] P2;
549
550 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{Presentation AI-00114}
551 @key[with] P1;
552 @key[with] P2;
553 @key[procedure] Main @key[is]
554 Local : @key[aliased] Integer;
555 Y : P2.T2(@Chg{New=[D],Old=[A]} => Local'Access);
556 @key[begin]
557 P1.Foo(Y);
558 @key[end] Main;
559 @end{Example}
560
561 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
562 If the above example were legal,
563 we would have succeeded in @Chg{Version=[2],New=[],Old=[making an access
564 value that points to Main.Local after Main has been left,
565 and we would also have succeeded in ]}doing an assignment of a task
566 object, @Chg{Version=[2],New=[],Old=[both of ]}which @Chg{Version=[2],New=[is],
567 Old=[are]} supposed to be @Chg{Version=[2],New=[a no-no],Old=[no-no's]}.
568 @ChgNote{A runtime check prevents the first from being a problem in Ada 2005.}
569
570 This rule is not needed for private extensions,
571 because they inherit their limitedness from their ancestor,
572 and there is a separate rule forbidding limited components of the
573 corresponding record extension if the parent is nonlimited.
574 @end{Reason}
575 @begin{Ramification}
576 @leading@;A type derived from an untagged private type is untagged,
577 even if the full view of the parent is tagged,
578 and even at places that can see the parent:
579 @begin{Example}
580 @key[package] P @key[is]
581 @key[type] Parent @key[is] @key[private];
582 @key[private]
583 @key[type] Parent @key[is] @key[tagged]
584 @key[record]
585 X: Integer;
586 @key[end] @key[record];
587 @key[end] P;
588
589 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{Presentation AI-00114}
590 @Chg{New=[@key[with] P;
591 ],Old=[]}@key[package] Q @key[is]
592 @key[type] T @key[is] @key[new] @Chg{New=[P.],Old=[]}Parent;
593 @key[end] Q;
594
595 @key[with] Q; @key[use] Q;
596 @key[package] @key[body] P @key[is]
597 ... T'Class ... --@RI{ Illegal!}
598 Object: T;
599 ... Object.X ... --@RI{ Illegal!}
600 ... Parent(Object).X ... --@RI{ OK.}
601 @key[end] P;
602 @end{Example}
603
604 The declaration of T declares an untagged view.
605 This view is always untagged, so T'Class is illegal,
606 it would be illegal to extend T, and so forth.
607 The component name X is never visible for this view,
608 although the component is still there @em one
609 can get one's hands on it via a @nt{type_conversion}.
610
611 @end{Ramification}
612
613 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00396-01]}
614 @ChgAdded{Version=[2],Type=[Leading],Text=[If a full type has a partial view
615 that is tagged, then:]}
616 @begin{Itemize}
617 @ChgRef{Version=[2],Kind=[Added]}
618 @ChgAdded{Version=[2],Text=[the partial view shall be a synchronized tagged
619 type (see @RefSecNum{Interface Types}) if and only if the full type is a
620 synchronized tagged type;]}
621
622 @begin{Reason}
623 @ChgRef{Version=[2],Kind=[Added]}
624 @ChgAdded{Version=[2],Text=[ Since we do not allow record extensions of
625 synchronized tagged types, this property has to be visible in the partial
626 view to avoid privacy breaking. Generic formals do not need a similar rule as
627 any extensions are rechecked for legality in the specification, and
628 extensions of tagged formals are always illegal in a generic body.]}
629 @end{Reason}
630
631 @ChgRef{Version=[2],Kind=[Added]}
632 @ChgAdded{Version=[2],Text=[the partial view shall be a descendant of an
633 interface type (see 3.9.4) if and only if the full type is a descendant of the
634 interface type.]}
635
636 @begin{Reason}
637 @ChgRef{Version=[2],Kind=[AddedNormal]}
638 @ChgAdded{Version=[2],KeepNext=[T],Type=[Leading],Text=[Consider the following example:]}
639 @begin{Example}
640 @ChgRef{Version=[2],Kind=[AddedNormal]}
641 @ChgAdded{Version=[2],Text=[@key{package} P @key{is}
642 @key{package} Pkg @key{is}
643 @key{type} Ifc @key{is interface};
644 @key{procedure} Foo (X : Ifc) @key{is abstract};
645 @key{end} Pkg;]}
646
647 @ChgRef{Version=[2],Kind=[AddedNormal]}
648 @ChgAdded{Version=[2],Text=[ @key{type} Parent_1 @key{is tagged null record};]}
649
650 @ChgRef{Version=[2],Kind=[AddedNormal]}
651 @ChgAdded{Version=[2],Text=[ @key{type} T1 @key{is new} Parent_1 @key{with private};
652 @key{private}
653 @key{type} Parent_2 @key{is new} Parent_1 @key{and} Pkg.Ifc @key{with null record};
654 @key{procedure} Foo (X : Parent_2); -- @RI[Foo #1]]}
655
656 @ChgRef{Version=[2],Kind=[AddedNormal]}
657 @ChgAdded{Version=[2],Text=[ @key{type} T1 @key{is new} Parent_2 @key{with null record}; -- @RI[Illegal.]
658 @key{end} P;]}
659
660 @ChgRef{Version=[2],Kind=[AddedNormal]}
661 @ChgAdded{Version=[2],Text=[@key{with} P;
662 @key{package} P_Client @key{is}
663 @key{type} T2 @key{is new} P.T1 @key{and} P.Pkg.Ifc @key{with null record};
664 @key{procedure} Foo (X : T2); -- @RI[Foo #2]
665 X : T2;
666 @key{end} P_Client;]}
667
668 @ChgRef{Version=[2],Kind=[AddedNormal]}
669 @ChgAdded{Version=[2],Text=[@key{with} P_Client;
670 @key{package body} P @key{is}
671 ...]}
672
673 @ChgRef{Version=[2],Kind=[AddedNormal]}
674 @ChgAdded{Version=[2],Text=[ @key{procedure} Bar (X : T1'Class) @key{is}
675 @key{begin}
676 Pkg.Foo (X); -- @RI[should call Foo #1 or an override thereof]
677 @key{end};]}
678
679 @ChgRef{Version=[2],Kind=[AddedNormal]}
680 @ChgAdded{Version=[2],Text=[@key{begin}
681 Pkg.Foo (Pkg.Ifc'Class (P_Client.X)); -- @RI[should call Foo #2]
682 Bar (T1'Class (P_Client.X));
683 @key{end} P;]}
684 @end{Example}
685 @ChgRef{Version=[2],Kind=[AddedNormal]}
686 @ChgAdded{Version=[2],Text=[This example is illegal because the completion
687 of T1 is descended from an interface that the partial view is not descended
688 from. If it were legal, T2 would implement Ifc twice, once in the visible
689 part of P, and once in the visible part of P_Client. We would need to
690 decide how Foo #1 and Foo #2 relate to each other. There are two options:
691 either Foo #2 overrides Foo #1, or it doesn't.]}
692
693 @ChgRef{Version=[2],Kind=[AddedNormal]}
694 @ChgAdded{Version=[2],Text=[If Foo #2 overrides Foo #1,
695 we have a problem because the client redefines a behavior that it doesn't
696 know about, and we try to avoid this at all costs, as it would lead to a
697 breakdown of whatever abstraction was implemented. If the abstraction
698 didn't expose that it implements Ifc, there must be a reason, and it should
699 be able to depend on the fact that no overriding takes place in clients.
700 Also, during maintenance, things may change and the full view might
701 implement a different set of interfaces. Furthermore, the situation is even
702 worse if the full type implements another interface Ifc2 that happens to
703 have a conforming Foo (otherwise unrelated, except for its name and
704 profile).]}
705
706 @ChgRef{Version=[2],Kind=[AddedNormal]}
707 @ChgAdded{Version=[2],Text=[If Foo #2 doesn't override Foo #1,
708 there is some similarity with the case of normal tagged private types,
709 where a client can declare an operation that happens to conform to some
710 private operation, and that's OK, it gets a different slot in the type
711 descriptor. The problem here is that T2 would implement Ifc in two
712 different ways, and through conversions to Ifc'Class we could end up with
713 visibility on both of these two different implementations. This is the
714 @lquotes@;diamond inheritance@rquotes problem of C++ all over again, and we
715 would need some kind of a preference rule to pick one implementation. We
716 don't want to go there (if we did, we might as well provide full-fledged
717 multiple inheritance).]}
718
719 @ChgRef{Version=[2],Kind=[AddedNormal]}
720 @ChgAdded{Version=[2],Text=[Note that there wouldn't be any difficulty to
721 implement the first option, so the restriction is essentially
722 methodological. The second option might be harder to implement, depending
723 on the language rules that we would choose.]}
724 @end{Reason}
725
726 @begin{Ramification}
727 @ChgRef{Version=[2],Kind=[AddedNormal]}
728 @ChgAdded{Version=[2],Text=[This rule also prevents completing a private type
729 with an interface. A interface, like all types, is a descendant of itself,
730 and thus this rule is triggered. One reason this is necessary is that
731 a client of a private extension should be able to inherit limitedness
732 without having to look in the private part to see if the type is an
733 interface (remember that limitedness of interfaces is never inherited, while
734 it is inherited from other types).]}
735 @end{Ramification}
736
737 @end{Itemize}
738
739 @Defn2{Term=[ancestor subtype], Sec=(of a @nt<private_extension_declaration>)}
740 The @i(ancestor subtype) of a @nt<private_extension_declaration>
741 is the subtype defined by the @i(ancestor_)@!@nt<subtype_@!indication>;
742 the ancestor type shall be a specific tagged type.
743 The full view of a private extension shall be derived
744 (directly or indirectly) from the ancestor type.
745 In addition to the places where @LegalityTitle normally apply
746 (see @RefSecNum{Generic Instantiation}),
747 the requirement that the ancestor be specific applies also in the
748 private part of an instance of a generic unit.
749 @begin{Reason}
750 This rule allows the full view to be defined
751 through several intermediate derivations,
752 possibly from a series of types produced by
753 @nt{generic_instantiation}s.
754 @end{Reason}
755
756 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
757 @ChgAdded{Version=[2],Text=[If the reserved word @key{limited} appears in a
758 @nt{private_extension_declaration}, the ancestor type shall be a limited type.
759 If the reserved word @key{synchronized} appears in a
760 @nt{private_extension_declaration}, the ancestor type shall be a limited
761 interface.]}
762
763 If the declaration of a partial view includes
764 a @nt{known_discriminant_part}, then
765 the @nt{full_type_declaration} shall have a fully conforming
766 @Redundant[(explicit)]
767 @nt{known_discriminant_part}
768 @Redundant[(see @RefSec(Conformance Rules))].
769 @Defn2{Term=[full conformance],Sec=(required)}
770 @Redundant[The ancestor subtype may be unconstrained;
771 the parent subtype of the full view is required to be constrained
772 (see @RefSecNum{Discriminants}).]
773 @begin{Discussion}
774 If the ancestor subtype has discriminants,
775 then it is usually best to make it unconstrained.
776 @end{Discussion}
777
778 @begin{Ramification}
779 If the partial view has a @nt<known_discriminant_part>,
780 then the full view has to be a composite, non-array type,
781 since only such types may have known discriminants.
782 Also, the full view cannot inherit the discriminants in this case;
783 the @nt{known_discriminant_part} has to be explicit.
784
785 @leading@keepnext@;That is, the following is illegal:
786 @begin{Example}
787 @key[package] P @key[is]
788 @key[type] T(D : Integer) @key[is] @key[private];
789 @key[private]
790 @key[type] T @key[is] @key[new] Some_Other_Type; --@RI{ Illegal!}
791 @key[end] P;
792 @end{Example}
793 even if Some_Other_Type has an integer discriminant called D.
794
795 It is a ramification of this and other rules that in order for
796 a tagged type to privately inherit unconstrained discriminants,
797 the private type declaration has to have an
798 @nt{unknown_discriminant_part}.
799 @end{Ramification}
800
801
802 If a private extension inherits known discriminants from the ancestor subtype,
803 then the full view shall also inherit its discriminants from the
804 ancestor subtype,
805 and the parent subtype of the full view shall be constrained
806 if and only if the ancestor subtype is constrained.
807 @begin{Reason}
808 The first part ensures that the full view has the same discriminants
809 as the partial view.
810 The second part ensures that if the partial view is unconstrained,
811 then the full view is also unconstrained;
812 otherwise, a client might constrain the partial view in a way that
813 conflicts with the constraint on the full view.
814 @end{Reason}
815
816 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01]}
817 @ChgAdded{Version=[2],Text=[If the @nt{full_type_declaration} for a private
818 extension is defined by a @nt{derived_type_definition}, then the reserved word
819 @key{limited} shall appear in the @nt{full_type_declaration} if and only if it
820 also appears in the @nt{private_extension_declaration}.]}
821 @begin{Reason}
822 @ChgRef{Version=[2],Kind=[AddedNormal]}
823 @ChgAdded{Version=[2],Text=[The word @key{limited} is optional (unless the
824 ancestor is an interface), but it should be used consistently. Otherwise
825 things would be too confusing for the reader. Of course, we only require
826 that if the full type is defined by a @nt{derived_type_definition}, as we
827 want to allow task and protected types to complete extensions of synchronized
828 interfaces.]}
829 @end{Reason}
830
831 @Redundant[If a partial view has unknown discriminants,
832 then the @nt{full_type_declaration} may define
833 a definite or an indefinite subtype, with or without discriminants.]
834
835 If a partial view has neither known nor unknown discriminants,
836 then the @nt{full_type_declaration} shall define a definite subtype.
837
838 If the ancestor subtype of a private extension has constrained
839 discriminants,
840 then the parent subtype of the full view shall impose a statically
841 matching constraint on those discriminants.
842 @PDefn2{Term=[statically matching],Sec=(required)}
843 @begin{Ramification}
844 If the parent type of the full view is not the ancestor type,
845 but is rather some descendant thereof, the constraint on
846 the discriminants of the parent type might come from
847 the declaration of some intermediate type in the derivation
848 chain between the ancestor type and the parent type.
849 @end{Ramification}
850 @begin{Reason}
851 @leading@keepnext@;This prevents the following:
852 @begin{Example}
853 @key[package] P @key[is]
854 @key[type] T2 @key[is] @key[new] T1(Discrim => 3) @key[with] @key[private];
855 @key[private]
856 @key[type] T2 @key[is] @key[new] T1(Discrim => 999) --@RI{ Illegal!}
857 @key[with] @key[record] ...;
858 @key[end] P;
859 @end{Example}
860
861 The constraints in this example do not statically match.
862
863 @leading@;If the constraint on the parent subtype of the full view depends on
864 discriminants of the full view, then the ancestor subtype has to be
865 unconstrained:
866 @begin{Example}
867 @key[type] One_Discrim(A: Integer) @key[is] @key[tagged] ...;
868 ...
869 @key[package] P @key[is]
870 @key[type] Two_Discrims(B: Boolean; C: Integer) @key[is] @key[new] One_Discrim @key[with] @key[private];
871 @key[private]
872 @key[type] Two_Discrims(B: Boolean; C: Integer) @key[is] @key[new] One_Discrim(A => C) @key[with]
873 @key[record]
874 ...
875 @key[end] @key[record];
876 @key[end] P;
877 @end{Example}
878
879 The above example would be illegal if the private extension said
880 @lquotes@;is new One_Discrim(A => C);@rquotes@;,
881 because then the constraints would not statically match.
882 (Constraints that depend on discriminants are not static.)
883
884 @end{Reason}
885 @end{Legality}
886
887 @begin{StaticSem}
888 @PDefn{private type}
889 A @nt{private_type_declaration} declares a private type
890 and its first subtype.
891 @PDefn{private extension}
892 Similarly, a @nt{private_@!extension_@!declaration} declares a private
893 extension and its first subtype.
894 @begin{Discussion}
895 @Defn{package-private type}
896 A @i(package-private type) is one
897 declared by a @nt<private_type_declaration>;
898 that is,
899 a private type other than a generic formal private type.
900 @Defn{package-private extension}
901 Similarly, a @i(package-private extension) is one
902 declared by a @nt<private_extension_declaration>.
903 These terms are not used in the RM95 version of this document.
904 @end{Discussion}
905
906 A declaration of a partial view and the corresponding
907 @nt{full_type_declaration} define two views of a single type.
908 The declaration of a partial view
909 together with the visible part define the operations that are
910 available to outside program units;
911 the declaration of the full view together with
912 the private part define other operations whose direct use is
913 possible only within the declarative region of the package itself.
914 @Defn{characteristics}
915 Moreover, within the scope of the declaration of the full view, the
916 @i{characteristics} of the type are determined by the full view;
917 in particular, within its scope, the full view determines
918 the classes that include the type,
919 which components, entries, and protected subprograms are visible,
920 what attributes and other predefined operations are allowed,
921 and whether the first subtype is static.
922 See @RefSecNum{Private Operations}.
923
924 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00401]}
925 A private extension inherits components (including
926 discriminants unless there is a new @nt<discriminant_part>
927 specified) and user-defined primitive
928 subprograms from its ancestor type@Chg{Version=[2],New=[ and its progenitor
929 types (if any)],Old=[]}, in the same way that
930 a record extension inherits components and user-defined primitive
931 subprograms from its parent type@Chg{Version=[2],New=[ and its progenitor
932 types],Old=[]}
933 (see @RefSecNum{Derived Types and Classes}).
934 @begin{Honest}
935 If an operation of the parent type is abstract,
936 then the abstractness of the inherited operation
937 is different for nonabstract record extensions
938 than for nonabstract private extensions
939 (see @RefSecNum{Abstract Types and Subprograms}).
940 @end{Honest}
941 @end{StaticSem}
942
943 @begin{RunTime}
944 @PDefn2{Term=[elaboration], Sec=(private_type_declaration)}
945 The elaboration of a @nt{private_type_declaration} creates a partial
946 view of a type.
947 @PDefn2{Term=[elaboration], Sec=(private_extension_declaration)}
948 The elaboration of a @nt{private_extension_declaration} elaborates
949 the @i(ancestor_)@nt<subtype_indication>, and creates a
950 partial view of a type.
951 @end{RunTime}
952
953 @begin{Notes}
954 The partial view of a type as declared by a @nt<private_type_declaration>
955 is defined to be a composite view (in @RefSecNum{Types and Subtypes}).
956 The full view of the type might or might not be composite.
957 A private extension is also composite,
958 as is its full view.
959
960 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
961 Declaring a private type with an @nt{unknown_discriminant_part} is a
962 way of preventing clients from creating uninitialized objects of the
963 type; they are then forced to initialize each object by calling some
964 operation declared in the visible part of the package.
965 @Chg{Version=[2],New=[],Old=[If such a type is also limited, then no objects
966 of the type can be declared outside the scope of the @nt{full_type_declaration},
967 restricting all object creation to the package defining the type. This
968 allows complete control over all storage allocation for the type.
969 Objects of such a type can still be passed as parameters, however.]}
970 @begin{Discussion}
971 @Defn{generic contract/private type contract analogy}
972 Packages with private types are analogous to generic packages with
973 formal private types,
974 as follows:
975 The declaration of a package-private type is like the declaration of
976 a formal private type.
977 The visible part of the package is like the generic formal part;
978 these both specify a contract (that is, a set of operations and other
979 things available for the private type).
980 The private part of the package is like an instantiation of the generic;
981 they both give a @nt{full_type_declaration} that specifies implementation
982 details of the private type.
983 The clients of the package are like the body of the generic;
984 usage of the private type in these places is restricted to the
985 operations defined by the contract.
986
987 In other words, being inside the package is like being outside the
988 generic, and being outside the package is like being inside the
989 generic;
990 a generic is like an @lquotes@;inside-out@rquotes@; package.
991
992 This analogy also works for private extensions
993 in the same inside-out way.
994
995 Many of the legality rules are defined with this analogy in mind.
996 See, for example, the rules relating to operations of [formal]
997 derived types.
998
999 The completion rules for a private
1000 type are intentionally quite similar to the matching rules for a
1001 generic formal private type.
1002
1003 This analogy breaks down in one respect:
1004 a generic actual subtype is a subtype,
1005 whereas the full view for a private type is always a new type.
1006 (We considered allowing the completion of a @nt{private_type_declaration}
1007 to be a @nt{subtype_declaration},
1008 but the semantics just won't work.)
1009 This difference is behind the fact that a generic actual type can be
1010 class-wide, whereas the completion of a private type always declares
1011 a specific type.
1012 @end{Discussion}
1013
1014 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00401]}
1015 The ancestor type specified in a @nt<private_extension_declaration>
1016 and the parent type specified in the corresponding declaration
1017 of a record extension given in the private part need not be the
1018 same@Chg{Version=[2],New=[. If the ancestor
1019 type is not an interface type,],Old=[ @em]} the parent type of the full view
1020 can be any descendant of the ancestor type.
1021 In this case, for a primitive subprogram that is inherited from the
1022 ancestor type and
1023 not overridden, the formal parameter names and default expressions (if any)
1024 come from the corresponding primitive subprogram of the specified ancestor
1025 type, while the body comes from the corresponding primitive subprogram
1026 of the parent type of the full view.
1027 See @RefSecNum{Dispatching Operations of Tagged Types}.
1028
1029 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00401]}
1030 @ChgAdded{Version=[2],Text=[If the ancestor type specified in a
1031 @nt{private_extension_declaration} is an
1032 interface type, the parent type can be any type so long as the full view is a
1033 descendant of the ancestor type. The progenitor types specified in a
1034 @nt{private_extension_declaration} and the progenitor types specified in the
1035 corresponding declaration of a record extension given in the private part need
1036 not be the same @em the only requirement is that the private extension and the
1037 record extension be descended from the same set of interfaces.]}
1038 @end{Notes}
1039
1040 @begin{Examples}
1041 @leading@keepnext@i{Examples of private type declarations:}
1042 @begin{Example}
1043 @key[type] Key @key[is] @key[private];
1044 @key[type] File_Name @key[is] @key[limited] @key[private];
1045 @end{Example}
1046
1047 @begin{Wide}
1048 @leading@keepnext@i{Example of a private extension declaration:}
1049 @end{Wide}
1050 @begin{Example}
1051 @key[type] List @key[is] @key[new] Ada.Finalization.Controlled @key[with] @key[private];
1052 @end{Example}
1053 @end{Examples}
1054
1055 @begin{Extend83}
1056 @Defn{extensions to Ada 83}
1057 The syntax for a @nt{private_type_declaration} is augmented to
1058 allow the reserved word @key{tagged}.
1059
1060 In Ada 83, a private type without discriminants cannot be completed
1061 with a type with discriminants.
1062 Ada 95 allows the full view to have discriminants,
1063 so long as they have defaults
1064 (that is, so long as the first subtype is definite).
1065 This change is made for uniformity with generics,
1066 and because the rule as stated is simpler and easier to remember
1067 than the Ada 83 rule.
1068 In the original version of Ada 83, the same restriction applied
1069 to generic formal private types.
1070 However, the restriction was removed by the ARG for generics.
1071 In order to maintain the @lquotes@;generic contract/private type contract analogy@rquotes@;
1072 discussed above, we have to apply the same rule to
1073 package-private types.
1074 Note that a private untagged type without discriminants can be
1075 completed with a tagged type with discriminants only if the
1076 full view is constrained, because discriminants of tagged types
1077 cannot have defaults.
1078 @end{Extend83}
1079
1080 @begin{DiffWord83}
1081 RM83-7.4.1(4),
1082 @lquotes@;Within the specification of the package that declares a private type
1083 and before the end of the corresponding full type declaration, a
1084 restriction applies....@rquotes@;,
1085 is subsumed (and corrected) by the rule that
1086 a type shall be completely defined before it is frozen,
1087 and the rule that the parent type of a derived type declaration shall be
1088 completely defined, unless the derived type is a private extension.
1089 @end{DiffWord83}
1090
1091 @begin{Extend95}
1092 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01],ARef=[AI95-00396-01],ARef=[AI95-00401-01]}
1093 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1094 Added @nt{interface_list} to private extensions to
1095 support interfaces and multiple inheritance
1096 (see @RefSecNum{Interface Types}).]}
1097
1098 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
1099 @ChgAdded{Version=[2],Text=[A private extension may specify that it is a
1100 limited type. This is required for interface ancestors (from which
1101 limitedness is not inherited), but it is generally useful as documentation of
1102 limitedness.]}
1103
1104 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00443-01]}
1105 @ChgAdded{Version=[2],Text=[A private extension may specify that it is a
1106 synchronized type. This is required in order so that a regular limited
1107 interface can be used as the ancestor of a synchronized type (we do not
1108 allow hiding of synchronization).]}
1109 @end{Extend95}
1110
1111
1112
1113 @LabeledSubClause{Private Operations}
1114
1115 @begin{Intro}
1116 @Redundant[For a type declared in the visible part of a package
1117 or generic package, certain operations on the type
1118 do not become visible until later in the package @em
1119 either in the private part or the body.
1120 @Defn{private operations}
1121 Such @i{private operations} are available only inside the declarative
1122 region of the package or generic package.]
1123 @end{Intro}
1124
1125 @begin{StaticSem}
1126 The predefined operators that exist for a given type are determined by
1127 the classes to which the type belongs.
1128 For example, an integer type has a predefined "+" operator.
1129 In most cases, the predefined operators of a type are declared
1130 immediately after the definition of the type;
1131 the exceptions are explained below.
1132 Inherited subprograms are also implicitly declared
1133 immediately after the definition of the type,
1134 except as stated below.
1135
1136 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1137 For a composite type, the characteristics
1138 (see @RefSecNum{Private Types and Private Extensions})
1139 of the type are determined in part by the
1140 characteristics of its component types.
1141 At the place where the composite type is declared,
1142 the only characteristics of component types used are those
1143 characteristics visible at that place.
1144 If later @Chg{New=[immediately within the declarative region in which the
1145 composite type is declared],
1146 Old=[within the immediate scope of the composite type]} additional
1147 characteristics become visible for a component type,
1148 then any corresponding characteristics become visible for the composite
1149 type.
1150 Any additional predefined operators are implicitly declared at that place.
1151
1152 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1153 The corresponding rule applies to a type defined by a
1154 @nt{derived_type_definition},
1155 if there is a place @Chg{New=[immediately within the declarative region in which
1156 the type is declared],
1157 Old=[within its immediate scope]} where additional
1158 characteristics of its parent type become visible.
1159
1160 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1161 @Defn{become nonlimited}
1162 @Defn2{Term=[nonlimited type],Sec=(becoming nonlimited)}
1163 @Defn2{Term=[limited type],Sec=(becoming nonlimited)}
1164 @Redundant[For example, an array type whose component type is limited
1165 private becomes nonlimited if the full view of the component type is
1166 nonlimited and visible at some later place @Chg{New=[immediately within the declarative region in which
1167 the array type is declared.],
1168 Old=[within the immediate scope of the array type.]}
1169 In such a case, the predefined "=" operator is implicitly declared at
1170 that place, and assignment is allowed after that place.]
1171
1172 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0019],ARef=[AI95-00033-01]}
1173 Inherited primitive subprograms follow a different rule.
1174 For a @nt{derived_type_definition},
1175 each inherited primitive subprogram
1176 is implicitly declared at the earliest place, if any,
1177 @Chg{New=[immediately within the declarative region in which],
1178 Old=[within the immediate scope of]} the @nt{type_declaration}@Chg{New=[ occurs],Old=[]},
1179 but after the @nt{type_declaration},
1180 where the corresponding declaration from the parent is
1181 visible.
1182 If there is no such place, then the inherited subprogram
1183 is not declared at all.
1184 @Redundant[An inherited subprogram that is not declared at all
1185 cannot be named in a call and cannot be overridden,
1186 but for a tagged type, it is possible to dispatch to it.]
1187
1188 For a @nt{private_extension_declaration}, each inherited subprogram is
1189 declared immediately after the @nt{private_extension_declaration}
1190 if the corresponding declaration from the ancestor is visible at that
1191 place.
1192 Otherwise, the inherited subprogram is not declared for the private
1193 extension, @Redundant[though it might be for the full type].
1194 @begin{Reason}
1195 @ChgRef{Version=[1],Kind=[Revised]}
1196 There is no need for the @lquotes@;earliest place
1197 @Chg{New=[immediately within the declarative region],
1198 Old=[within the immediate scope]}@rquotes@;
1199 business here, because a @nt{private_extension_declaration} will be
1200 completed with a @nt{full_type_declaration}, so we can hang the
1201 necessary private implicit declarations on the @nt{full_type_declaration}.
1202 @end{Reason}
1203 @begin{Discussion}
1204 The above rules matter only when the component type (or parent type) is
1205 declared in the visible part of a package, and the composite type (or
1206 derived type) is declared within the declarative region of that package
1207 (possibly in a nested package or a child package).
1208
1209 @leading@keepnext@;Consider:
1210 @begin{Example}
1211 @key[package] Parent @key[is]
1212 @key[type] Root @key[is] @key[tagged] @key[null] @key[record];
1213 @key[procedure] Op1(X : Root);
1214
1215 @key[type] My_Int @key[is] @key[range] 1..10;
1216 @key[private]
1217 @key[procedure] Op2(X : Root);
1218
1219 @key[type] Another_Int @key[is] @key[new] My_Int;
1220 @key[procedure] Int_Op(X : My_Int);
1221 @key[end] Parent;
1222
1223 @key[with] Parent; @key[use] Parent;
1224 @key[package] Unrelated @key[is]
1225 @key[type] T2 @key[is] @key[new] Root @key[with] @key[null] @key[record];
1226 @key[procedure] Op2(X : T2);
1227 @key[end] Unrelated;
1228
1229 @key[package] Parent.Child @key[is]
1230 @key[type] T3 @key[is] @key[new] Root @key[with] @key[null] @key[record];
1231 --@RI{ Op1(T3) implicitly declared here.}
1232
1233 @key[package] Nested @key[is]
1234 @key[type] T4 @key[is] @key[new] Root @key[with] @key[null] @key[record];
1235 @key[private]
1236 ...
1237 @key[end] Nested;
1238 @key[private]
1239 --@RI{ Op2(T3) implicitly declared here.}
1240 ...
1241 @key[end] Parent.Child;
1242
1243 @key[with] Unrelated; @key[use] Unrelated;
1244 @key[package] @key[body] Parent.Child @key[is]
1245 @key[package] @key[body] Nested @key[is]
1246 --@RI{ Op2(T4) implicitly declared here.}
1247 @key[end] Nested;
1248
1249 @key[type] T5 @key[is] @key[new] T2 @key[with] @key[null] @key[record];
1250 @key[end] Parent.Child;
1251 @end{Example}
1252
1253 Another_Int does not inherit Int_Op,
1254 because Int_Op does not @lquotes@;exist@rquotes@; at the place
1255 where Another_Int is declared.
1256
1257 @ChgRef{Version=[1],Kind=[Revised]}
1258 Type T2 inherits Op1 and Op2 from Root.
1259 However, the inherited Op2 is never declared,
1260 because Parent.Op2 is never visible @Chg{New=[immediately within the declarative region],
1261 Old=[within the immediate scope]} of T2.
1262 T2 explicitly declares its own Op2,
1263 but this is unrelated to the inherited one @em it
1264 does not override the inherited one,
1265 and occupies a different slot in the type descriptor.
1266
1267 T3 inherits both Op1 and Op2. Op1 is implicitly declared immediately
1268 after the type declaration,
1269 whereas Op2 is declared at the beginning of the private part.
1270 Note that if Child were a private child of Parent,
1271 then Op1 and Op2 would both be implicitly declared immediately after the
1272 type declaration.
1273
1274 @ChgRef{Version=[1],Kind=[Revised]}
1275 T4 is similar to T3, except that the earliest place
1276 @Chg{New=[immediately within the declarative region containing T4],
1277 Old=[within T4's immediate scope]}
1278 where Root's Op2 is visible is in the body of Nested.
1279
1280 If T3 or T4 were to declare a type-conformant Op2,
1281 this would override the one inherited from Root.
1282 This is different from the situation with T2.
1283
1284
1285 T5 inherits Op1 and two Op2's from T2.
1286 Op1 is implicitly declared immediately after the declaration of T5,
1287 as is the Op2 that came from Unrelated.Op2.
1288 However, the Op2 that originally came from Parent.Op2 is never
1289 implicitly declared for T5,
1290 since T2's version of that Op2 is never visible (anywhere @em it never
1291 got declared either).
1292
1293 For all of these rules, implicit private parts and bodies are assumed as
1294 needed.
1295
1296 @leading@keepnext@;It is possible for characteristics of a type to be revealed
1297 in more than one place:
1298
1299 @begin{Example}
1300 @key[package] P @key[is]
1301 @key[type] Comp1 @key[is] @key[private];
1302 @key[private]
1303 @key[type] Comp1 @key[is] @key[new] Boolean;
1304 @key[end] P;
1305
1306 @key[package] P.Q @key[is]
1307 @key[package] R @key[is]
1308 @key[type] Comp2 @key[is] @key[limited] @key[private];
1309 @key[type] A @key[is] @key[array](Integer @key[range] <>) @key[of] Comp2;
1310 @key[private]
1311 @key[type] Comp2 @key[is] @key[new] Comp1;
1312 --@RI{ A becomes nonlimited here.}
1313 --@RI{ "="(A, A) return Boolean is implicitly declared here.}
1314 ...
1315 @key[end] R;
1316 @key[private]
1317 --@RI{ Now we find out what Comp1 really is, which reveals}
1318 --@RI{ more information about Comp2, but we're not within}
1319 --@RI{ the immediate scope of Comp2, so we don't do anything}
1320 --@RI{ about it yet.}
1321 @key[end] P.Q;
1322
1323 @key[package] @key[body] P.Q @key[is]
1324 @key[package] @key[body] R @key[is]
1325 --@RI{ Things like "@key[xor]"(A,A) return A are implicitly}
1326 --@RI{ declared here.}
1327 @key[end] R;
1328 @key[end] P.Q;
1329 @end{Example}
1330
1331 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0019],ARef=[AI95-00033-01]}
1332 @ChgAdded{Version=[1],Type=[Leading],Text=[We say @i<immediately> within
1333 the declarative region in order that
1334 types do not gain operations within a nested scope. Consider:]}
1335 @begin{Example}
1336 @ChgRef{Version=[1],Kind=[Added]}
1337 @Chg{New=[@Key[package] Outer @key[is]
1338 @key[package] Inner @key[is]
1339 @key[type] Inner_Type @key[is] @key[private];
1340 @key[private]
1341 @key[type] Inner_Type @key[is] @key[new] Boolean;
1342 @key[end] Inner;
1343 @key[type] Outer_Type @key[is] @key[array](Natural @key[range] <>) @key[of] Inner.Inner_Type;
1344 @key[end] Outer;],Old=[]}
1345
1346 @ChgRef{Version=[1],Kind=[Added]}
1347 @Chg{New=[@key[package] @key[body] Outer @key[is]
1348 @key[package] @key[body] Inner @key[is]
1349 -- At this point, we can see that Inner_Type is a Boolean type.
1350 -- But we don't want Outer_Type to gain an "and" operator here.
1351 @key[end] Inner;
1352 @key[end] Outer;],Old=[]}
1353 @end{Example}
1354 @end{Discussion}
1355
1356 @leading@Redundant[The Class attribute is defined for tagged subtypes in
1357 @RefSecNum{Tagged Types and Type Extensions}.
1358 In addition,] for
1359 @PrefixType{every subtype S of an untagged private type
1360 whose full view is tagged},
1361 the following attribute is defined:
1362 @begin(description)
1363 @Attribute{Prefix=<S>, AttrName=<Class>,
1364 Text=<Denotes the class-wide subtype corresponding to the full
1365 view of S.
1366 This attribute is allowed only from the beginning of the private part
1367 in which the full view is declared, until the declaration of the full
1368 view.
1369 @Redundant[After the full view,
1370 the Class attribute of the full view can be used.]>}
1371 @end(description)
1372 @EndPrefixType{}
1373 @end{StaticSem}
1374
1375 @begin{Notes}
1376 Because a partial view and a full view
1377 are two different views of one and the same type,
1378 outside of the defining package the characteristics of the type are
1379 those defined by the visible part.
1380 Within these outside program units the type is just a private type
1381 or private extension,
1382 and any language rule that applies only to another class of types
1383 does not apply. The fact that the full declaration might implement
1384 a private type with a type of a particular class (for example, as
1385 an array type) is relevant only
1386 within the declarative region of the package itself
1387 including any child units.
1388
1389 @NoPrefix@;The consequences of this actual implementation are, however, valid
1390 everywhere. For example: any default initialization of components
1391 takes place; the attribute Size provides the size of the full view;
1392 finalization is still done for controlled components of the full view;
1393 task dependence rules still apply to components that are task
1394 objects.
1395
1396 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
1397 Partial views provide @Chg{Version=[2],New=[initialization],
1398 Old=[assignment (unless the view is limited)]},
1399 membership tests, selected components for the selection of
1400 discriminants and inherited components, qualification,
1401 and explicit conversion.@Chg{Version=[2],New=[ Nonlimited partial views
1402 also allow use of @nt{assignment_statement}s.],Old=[]}
1403
1404 For a subtype S of a partial view, S'Size is defined
1405 (see @RefSecNum{Operational and Representation Attributes}).
1406 For an object A of a partial view,
1407 the attributes A'Size and A'Address are defined
1408 (see @RefSecNum{Operational and Representation Attributes}).
1409 The Position, First_Bit, and Last_Bit attributes
1410 are also defined for discriminants and inherited components.
1411 @end{Notes}
1412
1413 @begin{Examples}
1414 @leading@keepnext@i{Example of a type with private operations:}
1415 @begin{Example}
1416 @key[package] Key_Manager @key[is]
1417 @key[type] Key @key[is] @key[private];
1418 Null_Key : @key[constant] Key; --@RI{ a deferred constant declaration (see @RefSecNum{Deferred Constants})}
1419 @key[procedure] Get_Key(K : @key[out] Key);
1420 @key[function] "<" (X, Y : Key) @key[return] Boolean;
1421 @key[private]
1422 @key[type] Key @key[is] @key[new] Natural;
1423 Null_Key : @key[constant] Key := Key'First;
1424 @key[end] Key_Manager;
1425
1426
1427 @key[package] @key[body] Key_Manager @key[is]
1428 Last_Key : Key := Null_Key;
1429 @key[procedure] Get_Key(K : @key[out] Key) @key[is]
1430 @key[begin]
1431 Last_Key := Last_Key + 1;
1432 K := Last_Key;
1433 @key[end] Get_Key;
1434
1435 @key[function] "<" (X, Y : Key) @key[return] Boolean @key[is]
1436 @key[begin]
1437 @key[return] Natural(X) < Natural(Y);
1438 @key[end] "<";
1439 @key[end] Key_Manager;
1440 @end{Example}
1441 @end{Examples}
1442
1443 @begin{Notes}
1444 @i{Notes on the example:}
1445 Outside of the package Key_Manager, the operations available for
1446 objects of type Key include assignment, the comparison for equality
1447 or inequality, the procedure Get_Key and the operator "<"; they do
1448 not include other relational operators such as ">=", or arithmetic
1449 operators.
1450
1451 @NoPrefix@;The explicitly declared operator "<" hides the predefined operator
1452 "<" implicitly declared by the @nt{full_type_declaration}. Within the
1453 body of the function, an explicit conversion of X and Y to the
1454 subtype Natural is necessary to invoke the "<" operator of the parent
1455 type.
1456 Alternatively, the result of the function could be written as not (X
1457 >= Y), since the operator ">=" is not redefined.
1458
1459 @NoPrefix@;The value of the variable Last_Key, declared in the package body,
1460 remains unchanged between calls of the procedure Get_Key. (See also
1461 the NOTES of @RefSecNum{Package Bodies}.)
1462 @end{Notes}
1463
1464 @begin{DiffWord83}
1465 The phrase in RM83-7.4.2(7), @lquotes@;...after the full type declaration@rquotes@;,
1466 doesn't work in the presence of child units, so we define that rule in
1467 terms of visibility.
1468
1469 The definition of the Constrained attribute for private types
1470 has been moved to @lquotes@;Obsolescent Features.@rquotes@;
1471 (The Constrained attribute of an object has not been moved there.)
1472 @end{DiffWord83}
1473
1474 @begin{DiffWord95}
1475 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0018],ARef=[AI95-00033-01]}
1476 @Chg{Version=[2],New=[@b<Corrigendum:> Clarified when additional operations
1477 are declared.],Old=[]}
1478
1479 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
1480 @Chg{Version=[2],New=[Revised the note on operations of partial views to
1481 reflect that limited types do have an assignment operation, but not
1482 @nt{assignment_statement}s.],Old=[]}
1483 @end{DiffWord95}
1484
1485
1486 @LabeledClause{Deferred Constants}
1487
1488 @begin{Intro}
1489 @redundant[Deferred constant declarations may be used to declare constants
1490 in the visible part of a package,
1491 but with the value of the constant given in the private part.
1492 They may also be used to declare constants imported from other
1493 languages (see @RefSecNum{Interface to Other Languages}).]
1494 @end{Intro}
1495
1496 @begin{Legality}
1497 @Redundant[@Defn{deferred constant declaration}
1498 A @i(deferred constant declaration) is an @nt<object_declaration>
1499 with the reserved word @key(constant) but no initialization expression.]
1500 @Defn{deferred constant}
1501 The constant declared by a deferred constant declaration is called
1502 a @i{deferred constant}.
1503 @PDefn2{Term=[requires a completion], Sec=(deferred constant declaration)}
1504 A deferred constant declaration requires a completion,
1505 which shall be a full constant declaration
1506 (called the @i{full declaration} of the deferred constant),
1507 or a @nt{pragma} Import (see @RefSecNum(Interface to Other Languages)).
1508 @Defn{full declaration}
1509 @begin{TheProof}
1510 The first sentence is redundant, as it is stated officially in
1511 @RefSecNum(Object Declarations).
1512 @end{TheProof}
1513
1514 @leading@;A deferred constant declaration that is completed
1515 by a full constant declaration shall occur immediately
1516 within the visible part of a @nt<package_specification>.
1517 For this case, the following additional rules apply to the
1518 corresponding full declaration:
1519 @begin(itemize)
1520 The full declaration shall occur immediately
1521 within the private part of the same package;
1522
1523 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00385-01]}
1524 The deferred and full constants shall have the same type@Chg{Version=[2],
1525 New=[, or shall have statically matching anonymous access subtypes],Old=[]};
1526 @begin{Ramification}
1527 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00385-01]}
1528 This implies that
1529 both the deferred declaration and the full declaration
1530 have to have a @nt<subtype_indication>@Chg{Version=[2],New=[ or
1531 @nt{access_definition}],Old=[]} rather than an
1532 @nt<array_type_definition>, because each @nt{array_type_definition}
1533 would define a new type.
1534 @end{Ramification}
1535
1536 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00385-01]}
1537 If the @Chg{Version=[2],New=[deferred constant declaration includes
1538 a],Old=[subtype defined by the]} @nt<subtype_indication> @Chg{Version=[2],
1539 New=[that defines a],Old=[in the deferred declaration is]}
1540 constrained@Chg{Version=[2],New=[ subtype],Old=[]}, then the subtype defined
1541 by the @nt<subtype_indication> in the full declaration shall match it
1542 statically.@Redundant[ On the other hand,
1543 if the subtype of the deferred constant is unconstrained,
1544 then the full declaration is still allowed to impose a constraint.
1545 The constant itself will be constrained, like all constants;]
1546
1547 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01]}
1548 If the deferred constant declaration
1549 includes the reserved word @key(aliased), then the
1550 full declaration shall also@Chg{Version=[2],New=[;],Old=[.]}
1551 @begin{Ramification}
1552 On the other hand, the full constant can be aliased
1553 even if the deferred constant is not.
1554 @end{Ramification}
1555
1556 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00231-01]}
1557 @ChgAdded{Version=[2],Text=[If the subtype of the deferred constant
1558 declaration excludes null, the subtype of the full declaration shall also
1559 exclude null.]}
1560 @begin{Ramification}
1561 @ChgRef{Version=[2],Kind=[Added]}
1562 @ChgAdded{Version=[2],Text=[On the other hand, the full constant can
1563 exclude null even if the deferred constant does not. But that can only
1564 happen for a @nt{subtype_indication}, as anonymous access types are
1565 required to statically match (which includes any @nt{null_exclusion}).]}
1566 @end{Ramification}
1567 @end(itemize)
1568
1569 @Redundant[A deferred constant declaration
1570 that is completed by a @nt{pragma} Import need not appear in
1571 the visible part of a @nt{package_specification},
1572 and has no full constant declaration.]
1573
1574
1575 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
1576 The completion of a deferred constant declaration shall occur
1577 before the constant is frozen
1578 (see @Chg{Version=[2],New=[@RefSecNum{Freezing Rules}],
1579 Old=[@RefSecNum{Deferred Constants}]}).
1580
1581
1582 @end{Legality}
1583
1584 @begin{RunTime}
1585 @PDefn2{Term=[elaboration], Sec=(deferred constant declaration)}
1586 The elaboration of a deferred constant declaration
1587 elaborates the @nt<subtype_indication> or (only allowed in the case of an
1588 imported constant) the @nt<array_type_definition>.
1589 @end{RunTime}
1590
1591 @begin{Notes}
1592 The full constant declaration for a deferred constant that is of a given
1593 private type or private extension is not allowed before the corresponding
1594 @nt{full_type_declaration}. This is a consequence of the freezing
1595 rules for types
1596 (see @RefSecNum{Freezing Rules}).
1597 @begin{Ramification}
1598 Multiple or single declarations are allowed for the
1599 deferred and the full declarations, provided that the
1600 equivalent single declarations would be allowed.
1601
1602 Deferred constant declarations are useful for declaring constants of
1603 private views, and types with components of private views.
1604 They are also useful for declaring
1605 access-to-constant objects that designate
1606 variables declared in the private part of a package.
1607 @end{Ramification}
1608 @end{Notes}
1609
1610 @begin{Examples}
1611 @leading@keepnext@i{Examples of deferred constant declarations:}
1612 @begin{Example}
1613 Null_Key : @key[constant] Key; --@RI[ see @RefSecNum{Private Operations}]
1614
1615 CPU_Identifier : @key[constant] String(1..8);
1616 @key[pragma] Import(Assembler, CPU_Identifier, Link_Name => "CPU_ID");
1617 --@RI[ see @RefSecNum{Interfacing Pragmas}]
1618 @end{Example}
1619 @end{Examples}
1620
1621 @begin{Extend83}
1622 @Defn{extensions to Ada 83}
1623 In Ada 83, a deferred constant is required to be of a private type
1624 declared in the same visible part.
1625 This restriction is removed for Ada 95;
1626 deferred constants can be of any type.
1627
1628 In Ada 83, a deferred constant declaration was not permitted to
1629 include a constraint, nor the reserved word @key(aliased).
1630
1631 In Ada 83, the rules required conformance of type marks; here
1632 we require static matching of subtypes if the deferred constant
1633 is constrained.
1634
1635 A deferred constant declaration can be completed with a @nt{pragma}
1636 Import. Such a deferred constant declaration need not be within a
1637 @nt{package_specification}.
1638
1639 The rules for too-early uses of deferred constants are modified in
1640 Ada 95 to allow more cases, and catch all errors at compile time.
1641 This change is necessary in order to allow deferred constants of a
1642 tagged type without violating the principle that for a dispatching call,
1643 there is always an implementation to dispatch to.
1644 It has the beneficial side-effect of catching some Ada-83-erroneous
1645 programs at compile time.
1646 The new rule fits in well with the new freezing-point rules.
1647 Furthermore, we are trying to convert undefined-value problems into
1648 bounded errors, and we were having trouble for the case of deferred
1649 constants.
1650 Furthermore, uninitialized deferred constants cause trouble for the
1651 shared variable / tasking rules, since they are really variable, even
1652 though they purport to be constant.
1653 In Ada 95, they cannot be touched until they become constant.
1654
1655 Note that we do not consider this change to be an upward
1656 incompatibility, because it merely changes an erroneous execution in Ada
1657 83 into a compile-time error.
1658
1659 The Ada 83 semantics are unclear in the case where
1660 the full view turns out to be an access type.
1661 It is a goal of the language design to prevent uninitialized access
1662 objects.
1663 One wonders if the implementation is required to initialize the
1664 deferred constant to null, and then initialize it (again!) to its
1665 real value.
1666 In Ada 95, the problem goes away.
1667 @end{Extend83}
1668
1669 @begin{DiffWord83}
1670 Since deferred constants can now be of a nonprivate type,
1671 we have made this a stand-alone clause, rather than
1672 a subclause of @RefSec{Private Types and Private Extensions}.
1673
1674 Deferred constant declarations used to have their own syntax, but now
1675 they are simply a special case of @nt<object_declaration>s.
1676 @end{DiffWord83}
1677
1678 @begin{Extend95}
1679 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00385-01]}
1680 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1681 Deferred constants were enhanced to allow
1682 the use of anonymous access types in them.]}
1683 @end{Extend95}
1684
1685 @begin{DiffWord95}
1686 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01]}
1687 @ChgAdded{Version=[2],Text=[Added matching rules for subtypes that
1688 exclude null.]}
1689 @end{DiffWord95}
1690
1691
1692 @LabeledClause{Limited Types}
1693
1694 @begin{Intro}
1695 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
1696 @redundant[A limited type is (a view of) a type for which
1697 @Chg{Version=[2],New=[copying (such as for an @nt{assignment_statement})],
1698 Old=[the assignment operation]} is not allowed.
1699 A nonlimited type is a (view of a) type for which
1700 @Chg{Version=[2],New=[copying], Old=[the assignment operation]} is allowed.]
1701 @begin{Discussion}
1702 The concept of the @i(value) of a limited type is difficult
1703 to define, since the abstract value of a limited type often
1704 extends beyond its physical representation. In some
1705 sense, values of a limited type cannot be divorced from
1706 their object. The value @i(is) the object.
1707
1708 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
1709 In Ada 83, in the two places where limited types were defined
1710 by the language, namely tasks and files, an implicit
1711 level of indirection was implied by the semantics to
1712 avoid the separation of the value from an associated
1713 object.
1714 In Ada 95, most limited types are passed by reference,
1715 and even return-ed by reference.@Chg{Version=[2],New=[ In Ada 2005,
1716 most limited types are built-in-place upon return, rather than returned
1717 by reference. Thus the object @lquotes@;identity@rquotes is part of the
1718 logical value of most limited types.],Old=[]}
1719 @end{Discussion}
1720 @begin{Honest}
1721 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01],ARef=[AI95-00419-01]}
1722 For a limited partial view whose full view is nonlimited,
1723 @Chg{Version=[2],New=[copying],Old=[assignment]} is possible on parameter
1724 passing and function return. To prevent any copying whatsoever, one should
1725 make both the partial @i{and} full views limited.
1726 @end{Honest}
1727 @Comment{The below was moved from a ChgToGlossaryAlso.}
1728 @ChgToGlossary{Version=[2],Kind=[Revised],Term=<Limited type>,
1729 Text=<A limited type is @Chg{Version=[2],New=[],Old=[(a view of) ]}a type
1730 for which
1731 @Chg{Version=[2],New=[copying (such as in an @nt{assignment_statement})],
1732 Old=[the assignment operation]} is not allowed.
1733 A nonlimited type is a @Chg{Version=[2],New=[],Old=[(view of a) ]}type for
1734 which
1735 @Chg{Version=[2],New=[copying], Old=[the assignment operation]} is allowed.>}
1736 @end{Intro}
1737
1738 @begin{Legality}
1739
1740 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
1741 If a tagged record type has any limited components,
1742 then the reserved word @key[limited] shall
1743 appear in its @nt<record_type_definition>.@Chg{Version=[2],New=[ @Redundant[If
1744 the reserved word @key[limited] appears in the definition of a
1745 @nt{derived_type_definition}, its parent type and any progenitor interfaces
1746 shall be limited.]],Old=[]}
1747 @begin{TheProof}
1748 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01]}
1749 @ChgAdded{Version=[2],Text=[The rule about the parent type being required
1750 to be limited can be found in @RefSecNum{Derived Types and Classes}. Rules
1751 about progenitor interfaces can be found in
1752 @RefSecNum{Interface Types}, specifically, a nonlimited interface can appear
1753 only on a nonlimited type. We repeat these rules here to gather these
1754 scattered rules in one obvious place.]}
1755 @end{TheProof}
1756 @begin{Reason}
1757 @leading@;This prevents tagged limited types from becoming nonlimited.
1758 Otherwise, the following could happen:
1759 @begin{Example}
1760 @key[package] P @key[is]
1761 @key[type] T @key[is] @key[limited] @key[private];
1762 @key[type] R @key[is] @key[tagged]
1763 @key[record] --@RI{ Illegal!}
1764 --@RI{ This should say @lquotes@;@key[limited record]@rquotes@;.}
1765 X : T;
1766 @key[end] @key[record];
1767 @key[private]
1768 @key[type] T @key[is] @key[new] Integer; --@RI{ R becomes nonlimited here.}
1769 @key[end] P;
1770
1771 @ChgRef{Version=[2],Kind=[Revised]}
1772 @key[package] Q @key[is]
1773 @key[type] R2@Chg{Version=[2],New=[],Old=[(Access_Discrim : @key[access] ...)]} @key[is] @key[new] R @key[with]
1774 @key[record]
1775 Y : Some_Task_Type;
1776 @key[end] @key[record];
1777 @key[end] Q;
1778 @end{Example}
1779
1780 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
1781 If the above were legal,
1782 then assignment would be defined for R'Class in the body of P,
1783 which is bad news, given @Chg{Version=[2],New=[],Old=[the access discriminant
1784 and ]}the task.
1785 @end{Reason}
1786
1787 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
1788 @ChgAdded{Version=[2],Type=[Leading],Text=[In the following contexts,
1789 an @nt{expression} of a limited
1790 type is not permitted unless it is an @nt{aggregate}, a @nt{function_call},
1791 or a parenthesized @nt{expression} or @nt{qualified_expression} whose operand
1792 is permitted by this rule:]}
1793 @begin{Itemize}
1794
1795 @ChgRef{Version=[2],Kind=[Added]}
1796 @Chg{Version=[2],New=[the initialization @nt{expression} of an
1797 @nt{object_declaration} (see @RefSecNum{Object Declarations})],Old=[]}
1798
1799 @ChgRef{Version=[2],Kind=[Added]}
1800 @Chg{Version=[2],New=[the @nt{default_expression} of a
1801 @nt{component_declaration} (see @RefSecNum{Record Types})],Old=[]}
1802
1803 @ChgRef{Version=[2],Kind=[Added]}
1804 @Chg{Version=[2],New=[the @nt{expression} of a @nt{record_component_association} (see @RefSecNum{Record Aggregates})],Old=[]}
1805
1806 @ChgRef{Version=[2],Kind=[Added]}
1807 @Chg{Version=[2],New=[the @nt{expression} for an @nt{ancestor_part} of an
1808 @nt{extension_aggregate} (see @RefSecNum{Extension Aggregates})],Old=[]}
1809
1810 @ChgRef{Version=[2],Kind=[Added]}
1811 @Chg{Version=[2],New=[an @nt{expression} of a @nt{positional_array_aggregate}
1812 or the @nt{expression} of an @nt{array_component_association} (see
1813 @RefSecNum{Array Aggregates})],Old=[]}
1814
1815 @ChgRef{Version=[2],Kind=[Added]}
1816 @Chg{Version=[2],New=[the @nt{qualified_expression} of an initialized allocator
1817 (see @RefSecNum{Allocators})],Old=[]}
1818
1819 @ChgRef{Version=[2],Kind=[Added]}
1820 @Chg{Version=[2],New=[the @nt{expression} of a return statement (see
1821 @RefSecNum{Return Statements})],Old=[]}
1822
1823 @ChgRef{Version=[2],Kind=[Added]}
1824 @Chg{Version=[2],New=[the @nt{default_expression} or actual parameter for a
1825 formal object of mode @b{in} (see @RefSecNum{Formal Objects})],Old=[]}
1826
1827 @begin{Discussion}
1828 @ChgRef{Version=[2],Kind=[AddedNormal]}
1829 @ChgAdded{Version=[2],Text=[All of these contexts normally require copying; by
1830 restricting the uses as above, we can require the new object to be
1831 built-in-place.]}
1832 @end{Discussion}
1833 @end{Itemize}
1834
1835
1836 @end{Legality}
1837
1838 @begin{StaticSem}
1839 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
1840 @leading@keepnext@Defn{limited type}
1841 A type is @i{limited} if it
1842 is @Chg{Version=[2],New=[],Old=[a descendant of ]}one of the following:
1843 @begin(itemize)
1844 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00411-01],ARef=[AI95-00419-01]}
1845 a type with the reserved word @key(limited)@Chg{Version=[2],New=[,
1846 @key(synchronized), @key(task), or @key(protected) ],Old=[]}
1847 in its definition;
1848 @begin{Ramification}
1849 Note that there is always a @lquotes@;definition,@rquotes@; conceptually,
1850 even if there is no syntactic category called @lquotes@;..._definition@rquotes@;.
1851
1852 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
1853 @ChgAdded{Version=[2],Text=[This includes interfaces of the above kinds,
1854 derived types with the reserved word @key{limited}, as well as task and
1855 protected types.]}
1856 @end{Ramification}
1857
1858 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00419-01]}
1859 @ChgDeleted{Version=[2],Text=[a task or protected type;]}
1860
1861 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
1862 a composite type with a limited component@Chg{Version=[2],New=[;],Old=[.]}
1863
1864 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00419-01]}
1865 @ChgAdded{Version=[2],Text=[a derived type whose parent is limited and is not an
1866 interface.]}
1867
1868 @begin{Ramification}
1869 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00419-01]}
1870 @ChgAdded{Version=[2],Text=[Limitedness is not inherited from interfaces;
1871 it must be explicitly specified when the parent is an interface.]}
1872 @end{Ramification}
1873
1874 @end(itemize)
1875
1876 @begin{Honest}
1877 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
1878 @ChgAdded{Version=[2],Text=[A derived type can become nonlimited if
1879 @key{limited} does not appear and the derivation
1880 takes place in the visible part of a child package,
1881 and the parent type is nonlimited as viewed from the
1882 private part or body of the child package.]}
1883 @end{Honest}
1884 @begin{Reason}
1885 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
1886 @ChgAdded{Version=[2],Text=[We considered a rule where limitedness was always
1887 inherited from the parent for derived types, but in the case of a type whose
1888 parent is an interface, this meant that the first interface is treated
1889 differently than other interfaces. It also would have forced users to declare
1890 dummy nonlimited interfaces just to get the limitedness right. We also
1891 considered a syntax like @key{not limited} to specify nonlimitedness when the
1892 parent was limited, but that was unsavory. The rule given is more uniform and
1893 simpler to understand.]}
1894
1895 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
1896 @ChgAdded{Version=[2],Text=[The rules for interfaces are asymmetrical, but
1897 the language is not: if the parent interface is limited, the presence of the
1898 word @key{limited} determines the limitedness, and nonlimited progenitors are
1899 illegal by the rules in @RefSecNum{Interface Types}. If the parent interface
1900 is nonlimited, the word @key{limited} is illegal by the rules in
1901 @RefSecNum{Derived Types and Classes}. The net effect is that the order of
1902 the interfaces doesn't matter.]}
1903 @end{Reason}
1904
1905
1906 @Defn{nonlimited type}
1907 Otherwise, the type is nonlimited.
1908
1909 @Redundant[There are no predefined equality operators for
1910 a limited type.]
1911 @end{StaticSem}
1912
1913 @begin{ImplReq}
1914 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
1915 @ChgAdded{Version=[2],Text=[
1916 For an @nt{aggregate} of a limited type used to initialize an object as allowed
1917 above, the implementation shall not create a separate anonymous object for the
1918 @nt{aggregate}. For a @nt{function_call} of a type with a part that is of a
1919 task, protected, or explicitly limited record type that is used to initialize
1920 an object as allowed above, the implementation shall not create a separate
1921 return object (see 6.5) for the @nt{function_call}. The @nt{aggregate} or
1922 @nt{function_call} shall be constructed directly in the new object.]}
1923 @begin{Discussion}
1924 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
1925 @ChgAdded{Version=[2],Text=[For a @nt{function_call}, we only require
1926 @i{build-in-place}@PDefn{build-in-place}
1927 for a limited type that would have
1928 been a return-by-reference type in Ada 95. We do this because
1929 we want to minimize disruption to Ada 95 implementations and users.]}
1930 @end{Discussion}
1931 @end{ImplReq}
1932
1933 @begin{Notes}
1934 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
1935 @Chg{Version=[2],New=[While it is allowed to write initializations of limited
1936 objects, such initializations never copy a limited object. The source of such an
1937 assignment operation must be an @nt<aggregate> or @nt<function_call>, and such
1938 @nt<aggregate>s and @nt<function_call>s must be built directly in the target
1939 object.],
1940 Old=[@leading@keepnext@;The following are consequences of the rules for limited types:]}
1941 @begin{Honest}
1942 @ChgRef{Version=[2],Kind=[AddedNormal]}
1943 @ChgAdded{Version=[2],Text=[This isn't quite true if the type can become
1944 nonlimited (see below); @nt{function_call}s only are required to be
1945 build-in-place for @lquotes@;really@rquotes@; limited types.]}
1946 @end{Honest}
1947
1948 @begin{NotIso}
1949 @ChgAdded{Version=[2],Noprefix=[T],Noparanum=[T],Text=[@Shrink{@i<Paragraphs 10
1950 through 15 were deleted.>}]}@Comment{This message should be deleted if the
1951 paragraphs are ever renumbered.}
1952 @end{NotIso}
1953
1954 @begin{Itemize}
1955 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
1956 @ChgDeleted{Version=[2],Text=[An initialization expression is not allowed in an
1957 @nt{object_declaration} if the type of the object is limited.]}
1958
1959 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
1960 @ChgDeleted{Version=[2],Text=[A default expression is not allowed in a
1961 @nt{component_declaration} if the type of the record component is limited.]}
1962
1963 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
1964 @ChgDeleted{Version=[2],Text=[An initialized allocator is not allowed if the
1965 designated type is limited.]}
1966
1967 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
1968 @ChgDeleted{Version=[2],Text=[A generic formal parameter of mode @key[in] must
1969 not be of a limited type.]}
1970 @end{Itemize}
1971
1972 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
1973 @ChgDeleted{Version=[2],Text=[@nt{Aggregate}s are not available for a limited
1974 composite type. Concatenation is not available for a limited array type.]}
1975
1976 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00287-01]}
1977 @ChgDeleted{Version=[2],Text=[The rules do not exclude a @nt{default_expression}
1978 for a formal parameter of a limited type; they do not exclude a deferred
1979 constant of a limited type if the full declaration of the constant is of a
1980 nonlimited type.]}
1981
1982 @Defn{become nonlimited}
1983 @Defn2{Term=[nonlimited type],Sec=(becoming nonlimited)}
1984 @Defn2{Term=[limited type],Sec=(becoming nonlimited)}
1985 As illustrated in @RefSecNum{Private Operations},
1986 an untagged limited type can become nonlimited under certain
1987 circumstances.
1988 @begin{Ramification}
1989 Limited private types do not become nonlimited;
1990 instead, their full view can be nonlimited,
1991 which has a similar effect.
1992
1993 It is important to remember
1994 that a single nonprivate type can be both limited and nonlimited
1995 in different parts of its scope. In other words, @lquotes@;limited@rquotes@; is a property
1996 that depends on where you are in the scope of the type.
1997 We don't call this a @lquotes@;view property@rquotes@; because there is no particular
1998 declaration to declare the nonlimited view.
1999
2000 Tagged types never become nonlimited.
2001 @end{Ramification}
2002 @end{Notes}
2003
2004 @begin{Examples}
2005 @leading@keepnext@i{Example of a package with a limited type:}
2006 @begin{Example}
2007 @key[package] IO_Package @key[is]
2008 @key[type] File_Name @key[is] @key[limited] @key[private];
2009
2010 @key[procedure] Open (F : @key[in] @key[out] File_Name);
2011 @key[procedure] Close(F : @key[in] @key[out] File_Name);
2012 @key[procedure] Read (F : @key[in] File_Name; Item : @key[out] Integer);
2013 @key[procedure] Write(F : @key[in] File_Name; Item : @key[in] Integer);
2014 @key[private]
2015 @key[type] File_Name @key[is]
2016 @key[limited] @key[record]
2017 Internal_Name : Integer := 0;
2018 @key[end] @key[record];
2019 @key[end] IO_Package;
2020
2021
2022 @key[package] @key[body] IO_Package @key[is]
2023 Limit : @key[constant] := 200;
2024 @key[type] File_Descriptor @key[is] @key[record] ... @key[end] @key[record];
2025 Directory : @key[array] (1 .. Limit) @key[of] File_Descriptor;
2026 ...
2027 @key[procedure] Open (F : @key[in] @key[out] File_Name) @key[is] ... @key[end];
2028 @key[procedure] Close(F : @key[in] @key[out] File_Name) @key[is] ... @key[end];
2029 @key[procedure] Read (F : @key[in] File_Name; Item : @key[out] Integer) @key[is] ... @key[end];
2030 @key[procedure] Write(F : @key[in] File_Name; Item : @key[in] Integer) @key[is] ... @key[end];
2031 @key[begin]
2032 ...
2033 @key[end] IO_Package;
2034 @end{Example}
2035 @end{Examples}
2036
2037 @begin{Notes}
2038 @i{Notes on the example:}
2039 In the example above, an outside subprogram making use of IO_Package
2040 may obtain a file name by calling Open and later use it in calls to
2041 Read and Write. Thus, outside the package, a file name obtained from
2042 Open acts as a kind of password; its internal properties (such as
2043 containing a numeric value) are not known and no other operations
2044 (such as addition or comparison of internal names) can be performed
2045 on a file name.
2046 Most importantly, clients of the package cannot make copies
2047 of objects of type File_Name.
2048
2049 @NoPrefix@;This example is characteristic of any case where complete control
2050 over the operations of a type is desired. Such packages serve a dual
2051 purpose. They prevent a user from making use of the internal
2052 structure of the type. They also implement the notion of an
2053 encapsulated data type where the only operations on the type are
2054 those given in the package specification.
2055
2056 @NoPrefix@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
2057 The fact that the full view of File_Name is explicitly declared
2058 @key[limited] means that parameter passing @Chg{Version=[2],New=[],
2059 Old=[and function return ]}will always be by reference@Chg{Version=[2],New=[
2060 and function results will always be built directly in the result object],Old=[]}
2061 (see @RefSecNum{Formal Parameter Modes} and @RefSecNum{Return Statements}).
2062
2063 @end{Notes}
2064
2065 @begin{Extend83}
2066 @Defn{extensions to Ada 83}
2067 The restrictions in RM83-7.4.4(4),
2068 which disallowed @key[out] parameters of limited types in certain
2069 cases, are removed.
2070 @end{Extend83}
2071
2072 @begin{DiffWord83}
2073 Since limitedness and privateness are orthogonal in Ada 95 (and
2074 to some extent in Ada 83), this is now its own clause rather
2075 than being a subclause of
2076 @RefSec{Private Types and Private Extensions}.
2077 @end{DiffWord83}
2078
2079 @begin{Extend95}
2080 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01],ARef=[AI95-00318-02]}
2081 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2082 Limited types now have an assignment operation, but its use is restricted
2083 such that all uses are build-in-place. This is
2084 accomplished by restricting uses to @nt{aggregate}s and @nt{function_call}s.
2085 @nt{Aggregate}s were not allowed to have a limited type in Ada 95, which
2086 causes a compatibility issue discussed in @RefSec{Aggregates}.
2087 Compatibility issues with return statements for limited
2088 @nt{function_call}s are discussed
2089 in @RefSec{Return Statements}.]}
2090 @end{Extend95}
2091
2092 @begin{DiffWord95}
2093 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00411-01],ARef=[AI95-00419-01]}
2094 @ChgAdded{Version=[2],Text=[Rewrote the definition of limited to ensure that
2095 interfaces are covered, but that limitedness is not inherited from interfaces.
2096 Derived types that explicitly include @key{limited} are now also covered.]}
2097 @end{DiffWord95}
2098
2099 @LabeledClause{User-Defined Assignment and Finalization}
2100
2101 @begin{Intro}
2102 @redundant[@Defn{user-defined assignment}
2103 @Defn2{Term=[assignment], Sec=(user-defined)}
2104 Three kinds of actions are
2105 fundamental to the manipulation of objects:
2106 initialization, finalization, and assignment.
2107 Every object is initialized, either explicitly
2108 or by default, after being created
2109 (for example, by an @nt{object_declaration} or @nt{allocator}).
2110 Every object is finalized before being destroyed
2111 (for example, by leaving a @nt{subprogram_body} containing an
2112 @nt{object_declaration}, or by a call to an instance of
2113 Unchecked_Deallocation).
2114 An assignment operation is used as part of @nt{assignment_statement}s,
2115 explicit initialization, parameter passing, and other operations.
2116 @IndexSee{Term=[constructor],See=[initialization]}
2117 @IndexSee{Term=[constructor],See=[Initialize]}
2118 @IndexSee{Term=[destructor],See=[finalization]}
2119
2120 Default definitions for these three fundamental
2121 operations are provided by the
2122 language, but
2123 @Defn{controlled type}
2124 a @i{controlled} type gives the user additional
2125 control over parts of these operations.
2126 @Defn{Initialize}@Defn{Finalize}@Defn{Adjust}
2127 In particular, the user can define, for a controlled type,
2128 an Initialize procedure which is invoked immediately after
2129 the normal default initialization of a controlled object, a Finalize procedure
2130 which is invoked immediately before finalization of any of the
2131 components of a controlled object, and an Adjust
2132 procedure which is invoked as the last step of an assignment to
2133 a (nonlimited) controlled object.]
2134 @ToGlossary{Term=<Controlled type>,
2135 Text=<A controlled type supports user-defined assignment and
2136 finalization.
2137 Objects are always finalized before being destroyed.>}
2138 @begin{Ramification}
2139 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01],ARef=[AI95-00287-01]}
2140 Here's the basic idea of initialization, value adjustment, and finalization,
2141 whether or not user defined:
2142 When an object is created,
2143 if it is explicitly assigned an initial value,
2144 @Chg{Version=[2],New=[the object is either built-in-place from an @nt{aggregate}
2145 or function call (in which case neither Adjust nor Initialize is applied), or ],Old=[]}
2146 the assignment copies and
2147 adjusts the initial value. Otherwise, Initialize is applied to it
2148 (except in the case of an @nt{aggregate} as a whole).
2149 An @nt{assignment_statement} finalizes the target before
2150 copying in and adjusting the new value.
2151 Whenever an object goes away, it is finalized.
2152 Calls on Initialize and Adjust happen bottom-up; that is,
2153 components first, followed by the containing object.
2154 Calls on Finalize @Chg{Version=[2],New=[happen],Old=[happens]} top-down; that is,
2155 first the containing object, and then its components.
2156 These ordering rules ensure that any components will be in a
2157 well-defined state when Initialize, Adjust,
2158 or Finalize is applied to the containing object.
2159 @end{Ramification}
2160 @end{Intro}
2161
2162 @begin{StaticSem}
2163 @leading@keepnext@;The following language-defined library package exists:
2164 @begin{Example}@ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0020],ARef=[AI95-00126-01]}
2165 @key[package] Ada.Finalization @key[is]@ChildUnit{Parent=[Ada],Child=[Finalization]}
2166 @key[pragma] Preelaborate(Finalization);@Chg{New=[
2167 @key[pragma] Remote_Types(Finalization);],Old=[]}
2168
2169 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00161-01]}
2170 @key[type] @AdaTypeDefn{Controlled} @key[is abstract tagged private];@Chg{Version=[2],New=[
2171 @key{pragma} Preelaborable_Initialization(Controlled);],Old=[]}
2172
2173 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
2174 @key(procedure) @AdaSubDefn{Initialize} (Object : @key(in out) Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
2175 @key(procedure) @AdaSubDefn{Adjust} (Object : @key(in out) Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
2176 @key(procedure) @AdaSubDefn{Finalize} (Object : @key(in out) Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
2177
2178 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00161-01]}
2179 @key[type] @AdaTypeDefn{Limited_Controlled} @key[is abstract tagged limited private];@Chg{Version=[2],New=[
2180 @key{pragma} Preelaborable_Initialization(Limited_Controlled);],Old=[]}
2181
2182 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
2183 @key(procedure) @AdaSubDefn{Initialize} (Object : @key(in out) Limited_Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
2184 @key(procedure) @AdaSubDefn{Finalize} (Object : @key(in out) Limited_Controlled)@Chg{Version=[2],New=[ @key{is null}],Old=[]};
2185 @key(private)
2186 ... -- @RI{not specified by the language}
2187 @key[end] Ada.Finalization;
2188 @end{Example}
2189
2190 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
2191 @Defn{controlled type}
2192 A controlled type is a descendant of Controlled or Limited_Controlled.
2193 @Chg{Version=[2],New=[],Old=[The (default) implementations of
2194 Initialize, Adjust, and Finalize have no effect. ]}The predefined
2195 "=" operator of type Controlled always returns True,
2196 @Redundant[since this operator is incorporated into
2197 the implementation of the predefined equality operator of types derived
2198 from Controlled, as explained in
2199 @RefSecNum(Relational Operators and Membership Tests).]
2200 The type Limited_Controlled is like Controlled, except
2201 that it is limited and it lacks the primitive subprogram Adjust.
2202 @begin{Discussion}
2203 We say @lquotes@;nonlimited controlled type@rquotes@ (rather than just
2204 @lquotes@;controlled type@rquotes@;; when we want to talk about descendants
2205 of Controlled only.
2206 @end{Discussion}
2207 @begin{Reason}
2208 We considered making Adjust and Finalize abstract.
2209 However, a reasonable coding convention is e.g. for Finalize to
2210 always call the parent's Finalize after doing whatever work is needed
2211 for the extension part.
2212 (Unlike CLOS, we have no way to do that automatically in Ada 95.)
2213 For this to work, Finalize cannot be abstract.
2214 In a generic unit, for a generic formal abstract derived type whose
2215 ancestor is Controlled or Limited_Controlled, calling the ancestor's
2216 Finalize would be illegal if it were abstract, even though the actual
2217 type might have a concrete version.
2218
2219 Types Controlled and Limited_Controlled are abstract, even though
2220 they have no abstract primitive subprograms.
2221 It is not clear that they need to be abstract, but there seems to be
2222 no harm in it, and it might make an implementation's life easier to
2223 know that there are no objects of these types @em in case the
2224 implementation wishes to make them @lquotes@;magic@rquotes@; in some way.
2225
2226 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01]}
2227 @ChgAdded{Version=[2],Text=[For Ada 2005, we considered making these types
2228 interfaces. That would have the advantage of allowing them to be added
2229 to existing trees. But that was rejected both because it would cause
2230 massive disruption to existing implementations, and because it would be
2231 very incompatible due to the "no hidden interfaces" rule. The latter rule
2232 would prevent a tagged private type from being completed with a derivation
2233 from Controlled or Limited_Controlled @em a very common idiom.]}
2234 @end{Reason}
2235
2236 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00360-01]}
2237 @ChgAdded{Version=[2],Type=[Leading],Text=[A type is said to
2238 @i{need finalization} if:@Defn{needs finalization}@Defn2{Term=[type],Sec=[needs finalization]}]}
2239
2240 @begin{Itemize}
2241 @ChgRef{Version=[2],Kind=[Added]}
2242 @ChgAdded{Version=[2],Text=[it is a controlled type, a task type or
2243 a protected type; or]}
2244
2245 @ChgRef{Version=[2],Kind=[Added]}
2246 @ChgAdded{Version=[2],Text=[it has a component that needs finalization; or]}
2247
2248 @ChgRef{Version=[2],Kind=[Added]}
2249 @ChgAdded{Version=[2],Text=[it is a limited type that has an access discriminant
2250 whose designated type needs finalization; or]}
2251
2252 @ChgRef{Version=[2],Kind=[Added]}
2253 @ChgAdded{Version=[2],Text=[it is one of a number of language-defined types
2254 that are explicitly defined to need finalization.]}
2255
2256 @begin{Ramification}
2257 @ChgRef{Version=[2],Kind=[AddedNormal]}
2258 @ChgAdded{Version=[2],Text=[The fact that a type needs finalization
2259 does not require it to be implemented with a controlled type. It just has to
2260 be recognized by the No_Nested_Finalization restriction.]}
2261
2262 @ChgRef{Version=[2],Kind=[AddedNormal]}
2263 @ChgAdded{Version=[2],Text=[This property is defined for the type, not
2264 for a particular view. That's necessary as restrictions look in private parts
2265 to enforce their restrictions; the point is to eliminate all controlled
2266 parts, not just ones that are visible.]}
2267 @end{Ramification}
2268
2269 @end{Itemize}
2270 @end{StaticSem}
2271
2272 @begin{RunTime}
2273 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
2274 @Chg{Version=[2],New=[],Old=[@PDefn2{Term=[elaboration],
2275 Sec=(object_declaration)}]}During
2276 the elaboration @Chg{Version=[2],New=[or evaluation of a construct that
2277 causes an object to be initialized by default],Old=[of
2278 an @nt{object_declaration}]},
2279 for every controlled subcomponent of
2280 the object that is not assigned an initial value
2281 (as defined in @RefSecNum{Object Declarations}),
2282 Initialize is called on that subcomponent.
2283 Similarly, if the object @Chg{Version=[2],New=[that is initialized by
2284 default ],Old=[]}as a whole is controlled@Chg{Version=[2],New=[],Old=[ and
2285 is not assigned an initial value]}, Initialize is called on the object.@Chg{Version=[2],
2286 New=[],Old=[ The same applies to the evaluation of an @nt{allocator},
2287 as explained in @RefSecNum{Allocators}.]}
2288
2289 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0021],ARef=[AI95-00182-01]}
2290 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
2291 For an @nt{extension_aggregate} whose @nt{ancestor_part} is a
2292 @nt{subtype_mark}@Chg{Version=[2],New=[ denoting a],
2293 Old=[, @Chg{New=[for each controlled subcomponent of the ancestor part, either
2294 Initialize is called, or its initial value is assigned, as appropriate],
2295 Old=[Initialize is called on all controlled subcomponents of the
2296 ancestor part]}; if the type of the ancestor part is itself]}
2297 controlled@Chg{Version=[2],New=[ subtype],Old=[]},
2298 the Initialize procedure of the ancestor type is called,
2299 unless that Initialize procedure is abstract.
2300 @begin{Discussion}
2301 @leading@keepnext@;Example:
2302 @begin{Example}
2303 @key[type] T1 @key[is] @key[new] Controlled @key[with]
2304 @key[record]
2305 ... --@RI{ some components might have defaults}
2306 @key[end] @key[record];
2307
2308 @key[type] T2 @key[is] @key[new] Controlled @key[with]
2309 @key[record]
2310 X : T1; --@RI{ no default}
2311 Y : T1 := ...; --@RI{ default}
2312 @key[end] @key[record];
2313
2314 A : T2;
2315 B : T2 := ...;
2316 @end{Example}
2317
2318 As part of the elaboration of A's declaration, A.Y is assigned a
2319 value; therefore Initialize is not applied to A.Y.
2320 Instead, Adjust is applied to A.Y as part of the assignment operation.
2321 Initialize is applied to A.X and to A, since those objects are not
2322 assigned an initial value.
2323 The assignment to A.Y is not considered an assignment to A.
2324
2325 For the elaboration of B's declaration, Initialize is not called at
2326 all. Instead the assignment adjusts B's value;
2327 that is, it applies Adjust to B.X, B.Y, and B.
2328
2329 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0021],ARef=[AI95-00182-01]}
2330 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00373-01]}
2331 @ChgAdded{Version=[1],Text=[The @nt{ancestor_part} of an
2332 @nt{extension_aggregate}@Chg{Version=[2],New=[, <> in aggregates, and the
2333 return object of an @nt{extended_return_statement} are],Old=[ is]} handled
2334 similarly.]}
2335 @end{Discussion}
2336
2337 Initialize and other initialization
2338 operations are done in an arbitrary order,
2339 except as follows.
2340 Initialize is applied to an object after initialization
2341 of its subcomponents, if any
2342 @Redundant[(including both implicit initialization and Initialize calls)].
2343 If an object has a component with an access discriminant
2344 constrained by a per-object expression,
2345 Initialize is applied to this component after any components that do not
2346 have such discriminants.
2347 For an object with several components with such a discriminant,
2348 Initialize is applied to them in order
2349 of their @nt{component_declaration}s.
2350 For an @nt<allocator>,
2351 any task activations follow all calls
2352 on Initialize.
2353 @begin{Reason}
2354 The fact that Initialize is done for subcomponents
2355 first allows Initialize for a composite
2356 object to refer to its subcomponents knowing
2357 they have been properly initialized.
2358
2359 The fact that Initialize is done for components with access
2360 discriminants after other components
2361 allows the Initialize operation for a component
2362 with a self-referential access discriminant to assume
2363 that other components of the enclosing object have already been
2364 properly initialized.
2365 For multiple such components, it allows some predictability.
2366 @end{Reason}
2367
2368 @leading@keepnext@Defn{assignment operation}
2369 When a target object with any controlled parts is assigned a value,
2370 @Redundant[either when created or in a subsequent
2371 @nt{assignment_statement},]
2372 the @i{assignment operation} proceeds as follows:
2373 @begin(itemize)
2374 The value of the target becomes the assigned value.
2375
2376 @Defn{adjusting the value of an object}
2377 @Defn{adjustment}
2378 The value of the target is @i{adjusted.}
2379 @end(itemize)
2380 @begin{Ramification}
2381 If any parts of the object are controlled,
2382 abort is deferred during the assignment operation.
2383 @end{Ramification}
2384
2385 @Defn{adjusting the value of an object}
2386 @Defn{adjustment}
2387 To adjust the value of a @Redundant[(nonlimited)] composite object,
2388 the values of the components of the object are first
2389 adjusted in an arbitrary order,
2390 and then, if the object is controlled,
2391 Adjust is called.
2392 Adjusting the value of an elementary object has no effect@Redundant[,
2393 nor does adjusting the value of a composite object with no
2394 controlled parts.]
2395 @begin{Ramification}
2396 Adjustment is never performed for values of
2397 a by-reference limited type, since these
2398 types do not support copying.
2399 @end{Ramification}
2400 @begin{Reason}
2401 The verbiage in the Initialize rule about access discriminants
2402 constrained by per-object expressions is not necessary here,
2403 since such types are limited,
2404 and therefore are never adjusted.
2405 @end{Reason}
2406
2407 @PDefn2{Term=[execution], Sec=(assignment_statement)}
2408 For an @nt{assignment_statement},
2409 @Redundant[ after the @nt{name} and
2410 @nt{expression} have been evaluated,
2411 and any conversion (including constraint checking) has been done,]
2412 an anonymous object is created, and
2413 the value is assigned into it;
2414 @Redundant[that is, the assignment operation is applied].
2415 @Redundant[(Assignment includes value adjustment.)]
2416 The target of the @nt{assignment_statement} is then finalized.
2417 The value of the anonymous object is then assigned into the target of
2418 the @nt{assignment_statement}.
2419 Finally, the anonymous object is finalized.
2420 @Redundant[As explained below,
2421 the implementation may eliminate the intermediate anonymous object,
2422 so this description subsumes the one given in
2423 @RefSec{Assignment Statements}.]
2424 @begin{Reason}
2425 @leading@;An alternative design for user-defined assignment might involve an
2426 Assign operation instead of Adjust:
2427 @begin{Example}
2428 @key[procedure] Assign(Target : @key[in] @key[out] Controlled; Source : @key[in] @key[out] Controlled);
2429 @end{Example}
2430
2431 @leading@keepnext@;Or perhaps even a syntax like this:
2432 @begin{Example}
2433 @key[procedure] ":="(Target : @key[in] @key[out] Controlled; Source : @key[in] @key[out] Controlled);
2434 @end{Example}
2435
2436 @leading@;Assign (or ":=") would have the responsibility of doing the copy,
2437 as well as whatever else is necessary.
2438 This would have the advantage that the Assign operation knows about both
2439 the target and the source at the same time @em it would be possible to
2440 do things like reuse storage belonging to the target, for example,
2441 which Adjust cannot do.
2442 However, this sort of design would not work in the case of unconstrained
2443 discriminated variables, because there is no way to change the
2444 discriminants individually.
2445 For example:
2446 @begin{Example}
2447 @key[type] Mutable(D : Integer := 0) @key[is]
2448 @key[record]
2449 X : Array_Of_Controlled_Things(1..D);
2450 @key[case] D @key[is]
2451 @key[when] 17 => Y : Controlled_Thing;
2452 @key[when] @key[others] => @key[null];
2453 @key[end] D;
2454 @key[end] @key[record];
2455 @end{Example}
2456
2457 An assignment to an unconstrained variable of type Mutable can cause
2458 some of the components of X, and the component Y, to appear and/or
2459 disappear.
2460 There is no way to write the Assign operation to handle this sort of
2461 case.
2462
2463 Forbidding such cases is not an option @em it would cause generic
2464 contract model violations.
2465 @end{Reason}
2466 @end{RunTime}
2467
2468 @begin{ImplReq}
2469 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0022],ARef=[AI95-00083-01]}
2470 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00318-02]}
2471 @ChgAdded{Version=[1],Text=[For an @nt{aggregate} of a controlled type
2472 whose value is assigned,
2473 other than by an @nt{assignment_statement}@Chg{Version=[2],New=[],Old=[ or
2474 a @nt{return_statement}]}, the
2475 implementation shall not create a separate anonymous object for the
2476 @nt{aggregate}. The aggregate value shall be constructed directly in the target
2477 of the assignment operation and Adjust is not called on the target object.]}
2478 @begin{Reason}
2479 @ChgRef{Version=[1],Kind=[Added]}
2480 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00318-02]}
2481 @ChgAdded{Version=[1],Type=[Leading],Text=[@Chg{Version=[2],
2482 New=[@PDefn{build-in-place}],Old=[]}This@Chg{Version=[2],
2483 New=[ @i<build-in-place> requirement],Old=[]} is necessary to prevent
2484 elaboration problems with deferred constants of controlled types. Consider:]}
2485 @begin{Example}
2486 @ChgRef{Version=[1],Kind=[Added]}
2487 @Chg{New=[@key[package] P @key[is]
2488 @key[type] Dyn_String @key[is private];
2489 Null_String : @key[constant] Dyn_String;
2490 ...
2491 @key[private]
2492 @key[type] Dyn_String @key[is new] Ada.Finalization.Controlled @key[with] ...
2493 @key[procedure] Finalize(X : @key[in out] Dyn_String);
2494 @key[procedure] Adjust(X : @key[in out] Dyn_String);
2495 @comment{Blank Line}
2496 Null_String : @key[constant] Dyn_String :=
2497 (Ada.Finalization.Controlled @key[with] ...);
2498 ...
2499 @key[end] P;],Old=[]}
2500 @end{Example}
2501 @ChgRef{Version=[1],Kind=[Added]}
2502 @Chg{New=[When Null_String is elaborated, the bodies of Finalize and Adjust
2503 clearly have not been elaborated. Without this rule, this declaration would
2504 necessarily raise Program_Error (unless the permissions given below are
2505 used by the implementation).],Old=[]}
2506 @end{Reason}
2507 @begin{Ramification}
2508 @ChgRef{Version=[2],Kind=[AddedNormal]}
2509 @ChgAdded{Version=[2],Text=[An @nt{aggregate} used in the return expression
2510 of a @nt{simple_@!return_@!statement} has to be built-in-place in the anonymous
2511 return object, as this is similar to an object declaration. (This is a change
2512 from Ada 95, but it is not an inconsistency as it only serves to restrict
2513 implementation choices.) But this only covers the @nt{aggregate}; a separate
2514 anonymous return object can still be used unless it too is required to be
2515 built-in-place (see @RefSecNum{Limited Types}).]}
2516 @end{Ramification}
2517 @end{ImplReq}
2518
2519 @begin{ImplPerm}
2520 An implementation is allowed to relax the above rules
2521 @Redundant[(for nonlimited controlled types)]
2522 in the following ways:
2523 @begin{TheProof}
2524 The phrase @lquotes@;for nonlimited controlled types@rquotes@; follows from the fact
2525 that all of the following permissions apply to cases involving
2526 assignment.
2527 It is important because the programmer can count on a stricter semantics
2528 for limited controlled types.
2529 @end{TheProof}
2530 @begin{Itemize}
2531 For an @nt{assignment_statement} that assigns to an object the value
2532 of that same object,
2533 the implementation need not do anything.
2534 @begin{Ramification}
2535 In other words, even if an object is controlled and a combination
2536 of Finalize and Adjust on the object might have a net
2537 side effect, they need not be performed.
2538 @end{Ramification}
2539
2540 For an @nt{assignment_statement} for a noncontrolled type,
2541 the implementation may finalize and assign each
2542 component of the variable separately (rather than finalizing the entire
2543 variable and assigning the entire new value)
2544 unless a discriminant of the variable is changed by the assignment.
2545 @begin{Reason}
2546 For example, in a slice assignment, an anonymous object is not necessary
2547 if the slice is copied component-by-component in the right direction,
2548 since array types are not controlled (although their components may be).
2549 Note that the direction, and even the fact that it's a slice assignment,
2550 can in general be determined only at run time.
2551 @end{Reason}
2552
2553 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00147-01]}
2554 For an @nt{aggregate} or function call whose value is assigned
2555 into a target object,
2556 the implementation need not create a separate anonymous object if
2557 it can safely create the value of
2558 the @nt{aggregate} or function call
2559 directly in the target object.
2560 Similarly, for an @nt{assignment_@!statement},
2561 the implementation need not create an anonymous object if
2562 the value being assigned is the result of evaluating a @nt{name}
2563 denoting an object (the source object) whose storage cannot overlap
2564 with the target. If the source object might overlap with the
2565 target object, then the implementation can avoid the need for
2566 an intermediary anonymous object by exercising one of the
2567 above permissions and perform the assignment one component
2568 at a time (for an overlapping array assignment), or not at all
2569 (for an assignment where the target and the source of the assignment are
2570 the same object).@Chg{Version=[2],New=[],Old=[ Even if an anonymous object is
2571 created, the implementation may move its value to the target object as part of
2572 the assignment without re-adjusting so long as the
2573 anonymous object has no aliased subcomponents.]}
2574 @begin{Ramification}
2575 In the @nt{aggregate} case, only one value adjustment is necessary,
2576 and there is no anonymous object to be finalized.
2577
2578 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00147-01]}
2579 @Chg{Version=[2],New=[Similarly, in the function call case, the
2580 anonymous object can be eliminated. Note, however, that Adjust
2581 must be called],Old=[In the @nt{assignment_statement} case as well,
2582 no finalization of the anonymous object is needed.
2583 On the other hand, if the target has aliased subcomponents,
2584 then an adjustment takes place]} directly on the target object
2585 as the last step of the assignment, since some of the
2586 subcomponents may be self-referential or otherwise
2587 position-dependent.@Chg{Version=[2],New=[ This Adjust can be eliminated only
2588 by using one of the following permissions.],Old=[]}
2589 @end{Ramification}
2590 @end{Itemize}
2591
2592 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00147-01]}
2593 @ChgAdded{Version=[2],Type=[Leading],Text=[Furthermore, an implementation is
2594 permitted to
2595 omit implicit Initialize, Adjust, and Finalize calls and associated assignment
2596 operations on an object of a nonlimited controlled type provided that:]}
2597
2598 @begin{Itemize}
2599 @ChgRef{Version=[2],Kind=[AddedNormal]}
2600 @ChgAdded{Version=[2],Text=[any omitted Initialize call is not a call on a
2601 user-defined Initialize procedure, and]}
2602 @begin{Honest}
2603 @ChgRef{Version=[2],Kind=[AddedNormal]}
2604 @ChgAdded{Version=[2],Text=[This does not apply to any calls to a user-defined
2605 Initialize routine that happen to occur in an Adjust or Finalize routine. It
2606 is intended that it is never necessary to look inside of an Adjust or Finalize
2607 routine to determine if the call can be omitted.]}
2608 @end{Honest}
2609 @begin{Reason}
2610 @ChgRef{Version=[2],Kind=[AddedNormal]}
2611 @ChgAdded{Version=[2],Text=[We don't want to eliminate objects for which the
2612 Initialize might have side effects (such as locking a resource).]}
2613 @end{Reason}
2614
2615 @ChgRef{Version=[2],Kind=[AddedNormal]}
2616 @ChgAdded{Version=[2],Text=[any usage of the value of the object after the implicit
2617 Initialize or Adjust call and before any subsequent Finalize call on the object
2618 does not change the external effect of the program, and]}
2619
2620 @ChgRef{Version=[2],Kind=[AddedNormal]}
2621 @ChgAdded{Version=[2],Text=[after the omission of such calls and operations, any
2622 execution of the program that executes an Initialize or Adjust call on an
2623 object or initializes an object by an @nt{aggregate} will also later execute a
2624 Finalize call on the object and will always do so prior to assigning a new
2625 value to the object, and]}
2626
2627 @ChgRef{Version=[2],Kind=[AddedNormal]}
2628 @ChgAdded{Version=[2],Text=[the assignment operations associated with omitted Adjust
2629 calls are also omitted.]}
2630 @end{Itemize}
2631
2632 @ChgRef{Version=[2],Kind=[AddedNormal]}
2633 @ChgAdded{Version=[2],Text=[This permission applies to Adjust and Finalize calls even
2634 if the implicit calls have additional external effects.]}
2635 @begin{Reason}
2636 @ChgRef{Version=[2],Kind=[AddedNormal]}
2637 @ChgAdded{Version=[2],Text=[The goal of the above permissions is to allow
2638 typical dead assignment and dead variable removal algorithms to work for
2639 nonlimited controlled types. We require that @lquotes@;pairs@rquotes@; of
2640 Initialize/Adjust/Finalize operations are removed. (These aren't always pairs,
2641 which is why we talk about @lquotes@;any execution of the program@rquotes@;.)]}
2642 @end{Reason}
2643
2644 @end{ImplPerm}
2645
2646 @begin{Extend83}
2647 @Defn{extensions to Ada 83}
2648 Controlled types and user-defined finalization are new to Ada 95.
2649 (Ada 83 had finalization semantics only for masters of tasks.)
2650 @end{Extend83}
2651
2652 @begin{Extend95}
2653 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00161-01]}
2654 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2655 @b[Amendment Correction:] Types Controlled and Limited_Controlled now have
2656 Preelaborable_Initialization, so that objects of types derived from these
2657 types can be used in preelaborated packages.]}
2658 @end{Extend95}
2659
2660 @begin{DiffWord95}
2661 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0020],ARef=[AI95-00126-01]}
2662 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified that Ada.Finalization
2663 is a remote types package.]}
2664
2665 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0021],ARef=[AI95-00182-01]}
2666 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Added wording to clarify that
2667 the default initialization (whatever it is) of an ancestor part is used.]}
2668
2669 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0022],ARef=[AI95-00083-01]}
2670 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified that Adjust is never
2671 called on an aggregate used for the initialization of an object or
2672 subaggregate, or passed as a parameter.]}
2673
2674 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00147-01]}
2675 @ChgAdded{Version=[2],Text=[Additional optimizations are allowed for
2676 nonlimited controlled types. These allow traditional dead variable
2677 elimination to be applied to such types.]}
2678
2679 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
2680 @ChgAdded{Version=[2],Text=[Corrected the build-in-place requirement
2681 for controlled @nt{aggregate}s to be consistent with the requirements
2682 for limited types.]}
2683
2684 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00348-01]}
2685 @ChgAdded{Version=[2],Text=[The operations of types Controlled and
2686 Limited_Controlled are now declared as null procedures (see
2687 @RefSecNum{Null Procedures}) to make the semantics clear (and to provide
2688 a good example of what null procedures can be used for).]}
2689
2690 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00360-01]}
2691 @ChgAdded{Version=[2],Text=[Types that need finalization are defined; this is
2692 used by the No_Nested_Finalization restriction
2693 (see @RefSec{Tasking Restrictions}).]}
2694
2695 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00373-01]}
2696 @ChgAdded{Version=[2],Text=[Generalized the description of objects that
2697 have Initialize called for them to say that it is done for all objects that
2698 are initialized by default. This is needed so that all of the new cases
2699 are covered.]}
2700 @end{DiffWord95}
2701
2702
2703 @LabeledSubClause{Completion and Finalization}
2704
2705 @begin{Intro}
2706 @redundant[This subclause defines @i{completion} and @i{leaving} of the execution
2707 of constructs and entities.
2708 A @i{master} is the execution of a construct that
2709 includes finalization of local objects after it is complete
2710 (and after waiting for any local tasks
2711 @em see @RefSecNum(Task Dependence - Termination of Tasks)),
2712 but before leaving.
2713 Other constructs and entities are left immediately upon completion.
2714 @IndexSee{Term=[cleanup],See=(finalization)}
2715 @IndexSee{Term=[destructor],See=(finalization)}]
2716 @end{Intro}
2717
2718 @begin{RunTime}
2719 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
2720 @Defn{completion and leaving (completed and left)}
2721 @Defn2{Term=[completion], Sec=(run-time concept)}
2722 The execution of a construct or entity
2723 is @i{complete} when the end of that execution has been reached,
2724 or when a transfer of control
2725 (see @RefSecNum{Simple and Compound Statements - Sequences of Statements})
2726 causes it to be abandoned.
2727 @Defn{normal completion}
2728 @Defn2{Term=[completion], Sec=(normal)}
2729 @Defn{abnormal completion}
2730 @Defn2{Term=[completion], Sec=(abnormal)}
2731 Completion due to reaching the end of execution,
2732 or due to the transfer of control of an @Chg{Version=[2],
2733 New=[@nt{exit_statement}, return statement, @nt{goto_statement}],
2734 Old=[@ntf{exit_}, @ntf{return_}, @ntf{goto_}]},
2735 or @nt{requeue_statement} or of the
2736 selection of a @nt{terminate_alternative}
2737 is @i{normal completion}.
2738 Completion is @i{abnormal} otherwise @Redundant[@em
2739 when control is transferred out of a construct
2740 due to abort or the raising of an exception].
2741 @begin{Discussion}
2742 Don't confuse the run-time concept of completion with
2743 the compile-time concept of completion
2744 defined in @RefSecNum{Completions of Declarations}.
2745 @end{Discussion}
2746
2747 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01],ARef=[AI95-00416-01]}
2748 @Defn{leaving}
2749 @Defn{left}
2750 After execution of a construct or entity is complete,
2751 it is @i{left},
2752 meaning that execution continues with the next action,
2753 as defined for the execution that is taking place.
2754 @Defn{master}
2755 Leaving an execution happens immediately after its completion,
2756 except in the case of a @i{master}:
2757 the execution of
2758 a @Chg{Version=[2],New=[body other than a @nt{package_body};
2759 the execution of a @nt{statement};
2760 or the evaluation of an @nt{expression}, @nt{function_call}, or @nt{range} that
2761 is not part of an enclosing @nt{expression}, @nt{function_call}, @nt{range}, or
2762 @nt{simple_@!statement} other than a @nt{simple_@!return_@!statement}],
2763 Old=[@nt{task_body}, a @nt{block_@!statement},
2764 a @nt{subprogram_body}, an @nt{entry_body}, or an @nt{accept_@!statement}]}.
2765 A master is finalized after it is
2766 complete, and before it is left.
2767
2768 @begin{Reason}
2769 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01],ARef=[AI95-00416-01]}
2770 @Chg{Version=[2],New=[@nt{Expression}s and @nt{statement}s
2771 are masters so that objects created by subprogram calls (in @nt{aggregate}s,
2772 @nt{allocator}s for anonymous access-to-object types, and so on) are
2773 finalized and have their tasks awaited before the @nt{expression}s or
2774 @nt{statement}s are left. Note that @nt{expression}s like the @nt{condition}
2775 of an @nt{if_statement} are masters, because they are not enclosed by a
2776 @nt{simple_statement}. Similarly, a @nt{function_call} which is renamed
2777 is a master, as it is not in a @nt{simple_@!statement}.],
2778 Old=[Note that although an @nt{accept_statement} has no
2779 @nt{declarative_part}, it can call functions and evaluate @nt{aggregate}s,
2780 possibly causing anonymous controlled objects to be created,
2781 and we don't want those objects to escape outside the rendezvous.]}
2782
2783 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
2784 @ChgAdded{Version=[2],Text=[We have to include @nt{function_call}s in the
2785 contexts that do not cause masters to occur so that @nt{expression}s
2786 contained in a @nt{function_call} (that is not part of an @nt{expression} or
2787 @nt{simple_statement}) do not individually become masters. We certainly do
2788 not want the parameter @nt{expression}s of a @nt{function_call} to be
2789 separate masters, as they would then be finalized before the function is
2790 called.]}
2791 @end{Reason}
2792 @begin{Ramification}
2793 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
2794 @ChgAdded{Version=[2],Text=[The fact that a @nt{function_call} is a master
2795 does not change the accessibility of the return object denoted by the
2796 @nt{function_call}; that depends on the use of the @nt{function_call}.
2797 The @nt{function_call} is the master
2798 of any short-lived entities (such as @nt{aggregate}s used as parameters of
2799 types with task or controlled parts).]}
2800 @end{Ramification}
2801
2802 @Defn2{Term=[finalization], Sec=(of a master)}
2803 For the @i{finalization} of a master,
2804 dependent tasks are first awaited,
2805 as explained in @RefSecNum{Task Dependence - Termination of Tasks}.
2806 Then each object whose accessibility level is
2807 the same as that of the master is finalized
2808 if the object was successfully initialized and still exists.
2809 @Redundant[These actions are performed whether the master is left
2810 by reaching the last statement or via a transfer of control.]
2811 When a transfer of control causes completion of an execution, each
2812 included master is finalized in order, from innermost outward.
2813 @begin{Ramification}
2814 As explained in @RefSecNum{Operations of Access Types},
2815 the set of objects with the same accessibility level
2816 as that of the master
2817 includes objects declared immediately within the master,
2818 objects declared in nested packages,
2819 objects created by @nt{allocator}s
2820 (if the ultimate ancestor access type is declared in one of those places)
2821 and subcomponents of all of these things.
2822 If an object was already finalized by Unchecked_Deallocation,
2823 then it is not finalized again when the master is left.
2824
2825 Note that any object whose accessibility level is deeper than that of
2826 the master would no longer exist;
2827 those objects would have been finalized by some inner master.
2828 Thus, after leaving a master, the only objects yet to be
2829 finalized are those whose accessibility level is less deep than that
2830 of the master.
2831
2832 @end{Ramification}
2833 @begin{Honest}
2834 Subcomponents of objects due to be finalized are not finalized
2835 by the finalization of the master;
2836 they are finalized by the finalization of the containing object.
2837 @end{Honest}
2838 @begin{Reason}
2839 We need to finalize subcomponents of objects even if the containing
2840 object is not going to get finalized because it was not fully
2841 initialized.
2842 But if the containing object is finalized, we don't want to require
2843 repeated finalization of the subcomponents,
2844 as might normally be implied by the recursion in finalization of a
2845 master and the recursion in finalization of an object.
2846 @end{Reason}
2847 @begin{Honest}
2848 Formally, completion and leaving refer to executions of constructs or
2849 entities.
2850 However, the standard sometimes (informally) refers
2851 to the constructs or entities whose executions are being completed.
2852 Thus, for example,
2853 @lquotes@;the subprogram call or task is complete@rquotes@;
2854 really means
2855 @lquotes@;@i{the execution of} the subprogram call or task is complete.@rquotes@;
2856 @end{Honest}
2857
2858 @leading@keepnext@RootDefn2{Term=[finalization], Sec=(of an object)}
2859 For the @i{finalization} of an object:
2860 @begin{Itemize}
2861 If the object is of an elementary type, finalization has no effect;
2862
2863 If the object is of a controlled type, the Finalize procedure is
2864 called;
2865
2866 If the object is of a protected type, the actions defined in
2867 @RefSecNum{Protected Units and Protected Objects} are performed;
2868
2869 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00416-01]}
2870 If the object is of a composite type, then after performing the
2871 above actions, if any, every component of the object
2872 is finalized in an arbitrary order, except as follows:
2873 if the object has
2874 a component with an access discriminant constrained by a per-object
2875 expression, this component is finalized before any components that
2876 do not have such discriminants; for an object with several components
2877 with such a discriminant, they are finalized in the
2878 reverse of the order of their
2879 @nt<component_declaration>s@Chg{Version=[2],New=[;],Old=[.]}
2880 @begin{Reason}
2881 This allows the finalization of a component with an access discriminant
2882 to refer to other components of the enclosing object prior to
2883 their being finalized.
2884 @end{Reason}
2885
2886 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00416-01]}
2887 @ChgAdded{Version=[2],Text=[If the object has coextensions (see
2888 @RefSecNum{Operations of Access Types}), each coextension is
2889 finalized after the object whose access discriminant designates it.]}
2890
2891 @end{Itemize}
2892
2893 @PDefn2{Term=[execution], Sec=(instance of Unchecked_Deallocation)}
2894 Immediately before an instance of Unchecked_Deallocation
2895 reclaims the storage of an object,
2896 the object is finalized.
2897 @Redundant[If an instance of Unchecked_Deallocation is never applied to
2898 an object created by an @nt{allocator},
2899 the object will still exist when the corresponding master completes,
2900 and it will be finalized then.]
2901
2902 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00280-01]}
2903 The order in which the finalization of a master performs finalization of
2904 objects is as follows:
2905 Objects created by declarations in the master are finalized
2906 in the reverse order of their creation.
2907 For objects that were created
2908 by @nt{allocator}s for an access type whose
2909 ultimate ancestor is declared in the master,
2910 this rule is applied as though each
2911 such object that still exists had been created
2912 in an arbitrary order
2913 at the first freezing point
2914 (see @RefSecNum{Freezing Rules})
2915 of the ultimate ancestor type@Chg{Version=[2],New=[; the finalization of
2916 these objects is called the @i<finalization of the
2917 collection>@Defn{finalization of the collection}@Defn2{Term=[collection],
2918 Sec=[finalization of]}],Old=[]}.@Chg{Version=[2],
2919 New=[ After the finalization of a master is complete, the objects finalized
2920 as part of its finalization cease to @i<exist>, as do any types and subtypes
2921 defined and created within the master.@PDefn2{Term=[exist],Sec=[cease to]}
2922 @PDefn2{Term=[cease to exist],Sec=[object]}
2923 @Defn2{Term=[cease to exist],Sec=[type]}],Old=[]}
2924 @begin{Reason}
2925 Note that we talk about the type of the @nt{allocator} here.
2926 There may be access values of a (general) access type
2927 pointing at objects created by @nt{allocator}s for some other
2928 type; these are not finalized at this point.
2929
2930 The freezing point of the ultimate ancestor access type is chosen
2931 because before that point, pool elements cannot be created,
2932 and after that point, access values designating (parts of)
2933 the pool elements can be created.
2934 This is also the point after which the pool object cannot have been
2935 declared.
2936 We don't want to finalize the pool elements until after anything
2937 finalizing objects that contain access values designating them.
2938 Nor do we want to finalize pool elements after finalizing the pool
2939 object itself.
2940 @end{Reason}
2941 @begin{Ramification}
2942 Finalization of allocated objects is done according
2943 to the (ultimate ancestor) @nt{allocator} type, not according to the storage pool
2944 in which they are allocated.
2945 Pool finalization might reclaim storage (see @RefSec{Storage Management}),
2946 but has nothing (directly) to do with finalization of the
2947 pool elements.
2948
2949 Note that finalization is done only for objects that still exist;
2950 if an instance of Unchecked_Deallocation has already gotten rid of a
2951 given pool element, that pool element will not be finalized when
2952 the master is left.
2953
2954 Note that a deferred constant declaration does not create the
2955 constant; the full constant declaration creates it.
2956 Therefore, the order of finalization depends on where the full
2957 constant declaration occurs,
2958 not the deferred constant declaration.
2959
2960 An imported object is not created by its declaration.
2961 It is neither initialized nor finalized.
2962 @end{Ramification}
2963 @begin{ImplNote}
2964 An implementation has to ensure that the storage for an object is not
2965 reclaimed when references to the object are still possible
2966 (unless, of course, the user explicitly requests reclamation via an
2967 instance of Unchecked_Deallocation).
2968 This implies, in general, that objects cannot be deallocated one by one
2969 as they are finalized;
2970 a subsequent finalization might reference an object that has been
2971 finalized, and that object had better be in its (well-defined)
2972 finalized state.
2973 @end{ImplNote}
2974
2975 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
2976 @PDefn2{Term=[execution], Sec=(assignment_statement)}
2977 The target of an @Chg{Version=[2],New=[@nt{assignment_statement}],
2978 Old=[assignment statement]} is finalized before copying in the
2979 new value, as explained
2980 in @RefSecNum{User-Defined Assignment and Finalization}.
2981
2982 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0021],ARef=[AI95-00182-01]}
2983 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01]}
2984 @Chg{Version=[2],New=[The master of an object is the master enclosing its
2985 creation whose accessibility level (see @RefSecNum{Operations of Access Types})
2986 is equal to that of the object.],
2987 Old=[@Chg{New=[If the @i{object_}@nt{name} in an @nt{object_renaming_declaration}, or
2988 the actual parameter for a generic formal @key[in out] parameter in a
2989 @nt{generic_instantiation}, denotes any part of an anonymous object created by
2990 a function call, the anonymous object is not finalized until after it is no
2991 longer accessible via any name. Otherwise, an],
2992 Old=[The]} anonymous object@Chg{New=[],Old=[s]} created by
2993 @Chg{New=[a ],Old=[]}function @Chg{New=[call or],Old=[calls and]} by
2994 @Chg{New=[an ],Old=[]}@nt{aggregate}@Chg{New=[ is],Old=[s are]}
2995 finalized no later than the end of the innermost enclosing
2996 @nt{declarative_item} or @nt{statement};
2997 if that is a @nt{compound_statement},
2998 @Chg{New=[the object is],Old=[they are]} finalized before starting the
2999 execution of any @nt{statement} within the @nt{compound_statement}.]}
3000 @begin{Honest}
3001 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00162-01]}@ChgNote{Should be ChgDeleted}
3002 @ChgDeleted{Version=[2],Text=[@leading@;This is not to be construed as permission to call Finalize
3003 asynchronously with respect to normal user code.
3004 For example,]}
3005 @begin{Example}
3006 @ChgRef{Version=[2],Kind=[Deleted]}
3007 @ChgDeleted{Version=[2],Text=[@key[declare]
3008 X : Some_Controlled_Type := F(G(...));
3009 --@RI{ The anonymous objects created for F and G are finalized}
3010 --@RI{ no later than this point.}
3011 Y : ...
3012 @key[begin]
3013 ...
3014 @key[end];]}
3015 @end{Example}
3016
3017 @ChgRef{Version=[2],Kind=[Deleted]}
3018 @ChgDeleted{Version=[2],Text=[
3019 The anonymous object for G should not be finalized at some random
3020 point in the middle of the body of F,
3021 because F might manipulate the same data structures as
3022 the Finalize operation, resulting in erroneous access
3023 to shared variables.]}
3024 @end{Honest}
3025 @begin{Reason}
3026 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00162-01]}
3027 @Chg{Version=[2],New=[This effectively imports all of the special rules for
3028 the accessibility level of renames, @nt{allocator}s, and so on, and applies
3029 them to determine where objects created in them are finalized. For instance,
3030 the master of a rename of a subprogram is that of the renamed subprogram.],
3031 Old=[It might be quite inconvenient for the implementation to defer
3032 finalization of the anonymous object for G until after copying the
3033 value of F into X, especially if the size of the result
3034 is not known at the call site.]}
3035 @end{Reason}
3036
3037 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
3038 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00162-01]}
3039 @Chg{Version=[2],New=[In the case of an @nt{expression} that is a master,
3040 finalization of any (anonymous) objects occurs as the final part of
3041 evaluation of the @nt{expression}.],
3042 Old=[@Chg{New=[If a transfer of control or raising of an exception occurs prior to
3043 performing a finalization of an anonymous object, the anonymous object is
3044 finalized as part of the finalizations due to be performed for the object's
3045 innermost enclosing master.],Old=[]}]}
3046
3047 @end{RunTime}
3048
3049 @begin{Bounded}
3050 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0023],ARef=[AI95-00169-01]}
3051 @leading@PDefn2{Term=(bounded error),Sec=(cause)}
3052 It is a bounded error for a call on Finalize or Adjust @Chg{New=[that occurs as
3053 part of object finalization or assignment ], Old=[]}to propagate an exception.
3054 The possible consequences depend on what action invoked the Finalize or
3055 Adjust operation:
3056 @begin{Ramification}
3057 It is not a bounded error for Initialize to propagate an
3058 exception. If Initialize propagates an exception,
3059 then no further calls on Initialize are performed,
3060 and those components that have already been initialized
3061 (either explicitly or by default)
3062 are finalized in the usual way.
3063
3064 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
3065 @Chg{New=[It also is not a bounded error for an explicit call to Finalize or
3066 Adjust to propagate an exception. We do not want implementations to have to
3067 treat explicit calls to these routines specially.],Old=[]}
3068 @end{Ramification}
3069 @begin{Itemize}
3070 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
3071 For a Finalize invoked as part of an @nt<assignment_statement>,
3072 Program_Error is raised at that point.
3073
3074 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0024],ARef=[AI95-00193-01]}
3075 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
3076 @Chg{New=[For an Adjust invoked as part of @Chg{Version=[2],New=[assignment
3077 operations other than those invoked as part of an @nt{assignment_statement}],
3078 Old=[the initialization of a controlled object]}, other adjustments due to be
3079 performed might or might not be performed, and then Program_Error is raised.
3080 During its propagation, finalization might or
3081 might not be applied to objects whose Adjust failed.],Old=[]}
3082 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
3083 For an Adjust invoked as part of an @Chg{Version=[2],New=[@nt{assignment_statement}],
3084 Old=[assignment @Chg{New=[statement],Old=[operation]}]}, any other adjustments
3085 due to be performed are performed, and then Program_Error is raised.
3086 @begin{Reason}
3087 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0024],ARef=[AI95-00193-01]}
3088 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
3089 @ChgAdded{Version=[1],Text=[In the case of assignments that are part of
3090 initialization, there is
3091 no need to complete all adjustments if one propagates an exception, as the
3092 object will immediately be finalized. So long as a subcomponent is not going
3093 to be finalized, it need not be adjusted, even if it is initialized as part of
3094 an enclosing composite assignment operation for which some adjustments are
3095 performed. However, there is no harm in an implementation making additional
3096 Adjust calls (as long as any additional components that are adjusted are also
3097 finalized), so we allow the implementation flexibility here.
3098 On the other hand, for an @Chg{Version=[2],New=[@nt{assignment_statement}],
3099 Old=[assignment statement]}, it is important that all
3100 adjustments be performed, even if one fails, because all controlled
3101 subcomponents are going to be finalized.@Chg{Version=[2],New=[ Other kinds of
3102 assignment are more like initialization than @nt{assignment_statement}s, so we
3103 include them as well in the permission.],Old=[]}]}
3104 @end{Reason}
3105 @begin{Ramification}
3106 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0024],ARef=[AI95-00193-01]}
3107 @ChgAdded{Version=[1],Text=[Even if an Adjust invoked as part of the
3108 initialization of a
3109 controlled object propagates an exception, objects whose initialization
3110 (including any Adjust or Initialize calls) successfully completed will be
3111 finalized. The permission above only applies to objects whose Adjust failed.
3112 Objects for which Adjust was never even invoked must not be finalized.]}
3113 @end{Ramification}
3114
3115 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
3116 For a Finalize invoked as part of a call on an instance of
3117 Unchecked_Deallocation, any other finalizations due to
3118 be performed are performed, and then Program_Error is raised.
3119 @begin{Discussion}
3120 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0104],ARef=[AI95-00179-01]}
3121 @ChgAdded{Version=[1],Text=[The standard does not specify if storage is
3122 recovered in this case.
3123 If storage is not recovered (and the object continues to exist), Finalize
3124 may be called on the object again (when the @nt<allocator>'s master is
3125 finalized).]}
3126 @end{Discussion}
3127
3128 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
3129 @Chg{New=[@Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
3130 For a Finalize invoked as part of the finalization of the anonymous
3131 object created by a function call or @nt{aggregate}, any other finalizations
3132 due to be performed are performed, and then Program_Error is raised.],Old=[]}
3133
3134 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0023],ARef=[AI95-00169-01]}
3135 @Chg{New=[@Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
3136 For a Finalize invoked due to reaching the end of the execution of a
3137 master, any other finalizations associated with the master are performed, and
3138 Program_Error is raised immediately after leaving the master.],Old=[]}
3139
3140 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
3141 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
3142 For a Finalize invoked by the transfer of control of
3143 an @Chg{Version=[2],
3144 New=[@nt{exit_statement}, return statement, @nt{goto_statement}],
3145 Old=[@ntf{exit_}, @ntf{return_}, @ntf{goto_}]},
3146 or @nt{requeue_@!statement},
3147 Program_Error is raised no earlier than after the finalization of the
3148 master being finalized when the exception occurred,
3149 and no later than the point where normal execution would have continued.
3150 Any other finalizations due to be performed up to that point are
3151 performed before raising Program_Error.
3152 @begin{Ramification}
3153 For example, upon leaving a @nt{block_statement} due to a
3154 @nt{goto_statement}, the Program_Error would be raised at the point of the
3155 target statement denoted by the label,
3156 or else in some more dynamically nested place,
3157 but not so nested as to allow an @nt{exception_handler} that has
3158 visibility upon the finalized object to handle it.
3159 For example,
3160 @begin{Example}
3161 @key[procedure] Main @key[is]
3162 @key[begin]
3163 <<The_Label>>
3164 Outer_Block_Statement : @key[declare]
3165 X : Some_Controlled_Type;
3166 @key[begin]
3167 Inner_Block_Statement : @key[declare]
3168 Y : Some_Controlled_Type;
3169 Z : Some_Controlled_Type;
3170 @key[begin]
3171 @key[goto] The_Label;
3172 @key[exception]
3173 @key[when] Program_Error => ... --@RI{ Handler number 1.}
3174 @key[end];
3175 @key[exception]
3176 @key[when] Program_Error => ... --@RI{ Handler number 2.}
3177 @key[end];
3178 @key[exception]
3179 @key[when] Program_Error => ... --@RI{ Handler number 3.}
3180 @key[end] Main;
3181 @end{Example}
3182
3183 The @nt{goto_statement} will first cause
3184 Finalize(Y) to be called.
3185 Suppose that Finalize(Y) propagates an exception.
3186 Program_Error will be raised after leaving Inner_Block_Statement,
3187 but before leaving Main.
3188 Thus, handler number 1 cannot handle this Program_Error;
3189 it will be handled either by handler number 2 or handler number 3.
3190 If it is handled by handler number 2,
3191 then Finalize(Z) will be done before executing the handler.
3192 If it is handled by handler number 3,
3193 then Finalize(Z) and Finalize(X) will both
3194 be done before executing the handler.
3195 @end{Ramification}
3196
3197 For a Finalize invoked by a transfer of control
3198 that is due to raising an exception,
3199 any other finalizations due to be performed for the same master are
3200 performed; Program_Error is raised immediately after leaving the master.
3201 @begin{Ramification}
3202 If, in the above example, the @nt{goto_statement} were replaced by a
3203 @nt{raise_statement}, then the Program_Error would be handled by
3204 handler number 2, and
3205 Finalize(Z) would be done before executing the handler.
3206 @end{Ramification}
3207 @begin{Reason}
3208 We considered treating this case in the same way as the others,
3209 but that would render certain @nt{exception_handler}s useless.
3210 For example, suppose the only @nt{exception_handler} is
3211 one for @key{others} in the main subprogram.
3212 If some deeply nested call raises an exception,
3213 causing some Finalize operation to be called,
3214 which then raises an exception,
3215 then normal execution @lquotes@;would have continued@rquotes@;
3216 at the beginning of the @nt{exception_handler}.
3217 Raising Program_Error at that point would cause that
3218 handler's code to be skipped.
3219 One would need two nested @nt{exception_handler}s
3220 to be sure of catching such cases!
3221
3222 On the other hand, the @nt{exception_handler} for a given master
3223 should not be allowed to handle exceptions raised during finalization
3224 of that master.
3225 @end{Reason}
3226
3227 For a Finalize invoked by a transfer of control due to
3228 an abort or selection of a terminate alternative,
3229 the exception is ignored;
3230 any other finalizations due to be performed are performed.
3231 @begin{Ramification}
3232 This case includes an asynchronous transfer of control.
3233 @end{Ramification}
3234 @begin{Honest}
3235 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
3236 This violates the general principle that it is always possible for
3237 a bounded error to raise Program_Error
3238 (see @RefSec{Classification of Errors}).
3239 @end{Honest}
3240 @end{Itemize}
3241 @end{Bounded}
3242
3243 @begin{Notes}
3244 The rules of Section 10 imply that
3245 immediately prior to partition termination, Finalize operations
3246 are applied to library-level controlled objects (including those
3247 created by @nt{allocator}s of library-level access types, except those
3248 already finalized).
3249 This occurs after waiting for library-level
3250 tasks to terminate.
3251 @begin{Discussion}
3252 We considered defining a pragma that would apply to a controlled
3253 type that would suppress Finalize operations for library-level objects
3254 of the type upon partition termination.
3255 This would be useful for types whose finalization actions
3256 consist of simply reclaiming global heap storage, when this is already
3257 provided automatically by the environment upon program termination.
3258 @end{Discussion}
3259
3260 A constant is only constant between its initialization and
3261 finalization.
3262 Both initialization and finalization are allowed to
3263 change the value of a constant.
3264
3265 Abort is deferred during certain operations related to controlled types,
3266 as explained in
3267 @RefSecNum{Abort of a Task - Abort of a Sequence of Statements}.
3268 Those rules prevent an abort from causing a controlled object
3269 to be left in an ill-defined state.
3270
3271 The Finalize procedure is called upon finalization of
3272 a controlled object, even if Finalize was called earlier,
3273 either explicitly or as part of an assignment; hence,
3274 if a controlled type is visibly controlled (implying that its Finalize
3275 primitive is directly callable), or is nonlimited (implying that
3276 assignment is allowed), its Finalize procedure should be
3277 designed to have no ill effect if it is applied a second time
3278 to the same object.
3279 @begin{Discussion}
3280 Or equivalently, a Finalize procedure
3281 should be @lquotes@;idempotent@rquotes@;;
3282 applying it twice to the same object should be equivalent to
3283 applying it once.
3284 @end{Discussion}
3285 @begin{Reason}
3286 A user-written Finalize procedure should be idempotent since it
3287 can be called explicitly
3288 by a client (at least if the type is "visibly" controlled).
3289 Also, Finalize is used implicitly as part of the
3290 @nt<assignment_statement> if the
3291 type is nonlimited, and an abort is permitted to disrupt an
3292 @nt<assignment_statement> between finalizing the left-hand side
3293 and assigning the new value to it (an abort is not permitted to
3294 disrupt an assignment operation between copying in the new value
3295 and adjusting it).
3296 @end{Reason}
3297 @begin{Discussion}
3298 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
3299 Either Initialize or Adjust,
3300 but not both, is applied to (almost) every controlled object
3301 when it is created:
3302 Initialize is done when no initial value is assigned to the object,
3303 whereas Adjust is done as part of assigning the initial value.
3304 The one exception is the @Chg{Version=[2],New=[],Old=[anonymous ]}object
3305 @Chg{Version=[2],New=[initialized],Old=[created]} by an
3306 @nt{aggregate}@Chg{Version=[2],New=[ (both the anonymous object created for
3307 an aggregate, or an object initialized by an @nt{aggregate} that is
3308 built-in-place)],Old=[]}; Initialize is not applied to the @nt{aggregate}
3309 as a whole, nor is the value of the @nt<aggregate> @Chg{Version=[2],
3310 New=[or object ],Old=[]}adjusted.
3311
3312 @leading@Defn2{Term=[assignment operation], Sec=(list of uses)}
3313 All of the following use the assignment operation,
3314 and thus perform value adjustment:
3315 @begin{Itemize}
3316 the @nt{assignment_statement} (see @RefSecNum{Assignment Statements});
3317
3318 explicit initialization of a stand-alone object
3319 (see @RefSecNum{Object Declarations})
3320 or of a pool element (see @RefSecNum{Allocators});
3321
3322 default initialization of a component of a stand-alone object
3323 or pool element
3324 (in this case, the value of each component is assigned, and therefore adjusted,
3325 but the value of the object as a whole is not adjusted);
3326
3327 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
3328 function return, when the result @Chg{Version=[2],New=[is not built-in-place],
3329 Old=[type is not a return-by-reference
3330 type (see @RefSecNum{Return Statements});]}
3331 (adjustment of the result happens before finalization of
3332 the function@Chg{Version=[2],New=[],Old=[;
3333 values of return-by-reference types are not
3334 adjusted]});
3335
3336 predefined operators (although the only one that matters is
3337 concatenation; see @RefSecNum{Binary Adding Operators});
3338
3339 generic formal objects of mode @key{in}
3340 (see @RefSecNum{Formal Objects});
3341 these are defined in terms of constant declarations; and
3342
3343 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
3344 @nt{aggregate}s (see @RefSecNum{Aggregates})@Chg{Version=[2],New=[, when
3345 the result is not built-in-place],Old=[]}
3346 (in this case, the value of each component, and the parent part, for an
3347 @nt{extension_aggregate}, is assigned, and therefore adjusted,
3348 but the value of the @nt{aggregate} as a whole is not adjusted;
3349 neither is Initialize called);
3350 @end{Itemize}
3351
3352 @leading@;The following also use the assignment operation,
3353 but adjustment never does anything interesting in these cases:
3354 @begin{Itemize}
3355 By-copy parameter passing uses the assignment operation
3356 (see @RefSecNum{Parameter Associations}),
3357 but controlled objects are always passed by reference,
3358 so the assignment operation never does anything interesting in
3359 this case.
3360 If we were to allow by-copy parameter passing for controlled objects,
3361 we would need to make sure that the actual is finalized before doing
3362 the copy back for [@key{in}] @key{out} parameters.
3363 The finalization of the parameter itself needs to happen
3364 after the copy back (if any),
3365 similar to the finalization of an anonymous function return
3366 object or @nt{aggregate} object.
3367
3368 @key{For} loops use the assignment operation
3369 (see @RefSecNum{Loop Statements}), but since the type
3370 of the loop parameter is never controlled,
3371 nothing interesting happens there, either.
3372
3373 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00318-02]}
3374 @ChgAdded{Version=[2],Text=[Objects initialized by function results and
3375 @nt{aggregate}s that are built-in-place. In this case, the assignment
3376 operation is never executed, and no adjustment takes place. While
3377 built-in-place
3378 is always allowed, it is required for some types @em see
3379 @RefSecNum{Limited Types} and
3380 @RefSecNum{User-Defined Assignment and Finalization} @em and that's
3381 important since limited types have no Adjust to call.]}
3382 @end{Itemize}
3383
3384 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00287-01]}
3385 @ChgDeleted{Version=[2],Text=[Because Controlled and Limited_Controlled
3386 are library-level tagged types,
3387 all controlled types will be library-level types,
3388 because of the accessibility rules
3389 (see @RefSecNum{Operations of Access Types} and @RefSecNum{Type Extensions}).
3390 This ensures that the Finalize operations may be applied without
3391 providing any @lquotes@;display@rquotes@; or @lquotes@;static-link.@rquotes@;
3392 This simplifies finalization as a result of garbage collection,
3393 abort, and asynchronous transfer of control.]}
3394
3395 Finalization of the parts of a protected object are not done as
3396 protected actions.
3397 It is possible (in pathological cases)
3398 to create tasks during finalization that
3399 access these parts in parallel with the finalization itself.
3400 This is an erroneous use of shared variables.
3401 @end{Discussion}
3402 @begin{ImplNote}
3403 One implementation technique for finalization is to chain the
3404 controlled objects together on a per-task list.
3405 When leaving a master, the list can be walked up to a marked place.
3406 The links needed to implement the list can be declared (privately)
3407 in types Controlled and Limited_Controlled,
3408 so they will be inherited by all controlled types.
3409
3410 Another implementation technique, which we refer to as the @lquotes@;PC-map@rquotes@;
3411 approach essentially implies inserting exception handlers at various
3412 places, and finalizing objects based on where the exception was
3413 raised.
3414
3415 @Defn{PC-map approach to finalization}
3416 @Defn{program-counter-map approach to finalization}
3417 The PC-map approach is for the compiler/linker to create a map of
3418 code addresses; when an exception is raised, or abort occurs,
3419 the map can be consulted to see where the task was executing,
3420 and what finalization needs to be performed.
3421 This approach was given in the Ada 83 Rationale as a
3422 possible implementation strategy for exception handling @em the
3423 map is consulted to determine which exception handler applies.
3424
3425 If the PC-map approach is used, the implementation must take care in the
3426 case of arrays. The generated code will generally contain a loop to
3427 initialize an array. If an exception is raised part way through the
3428 array, the components that have been initialized must be finalized,
3429 and the others must not be finalized.
3430
3431 It is our intention that both of these implementation methods should
3432 be possible.
3433 @end{ImplNote}
3434 @end{Notes}
3435
3436 @begin{DiffWord83}
3437 Finalization depends on the concepts of completion and leaving,
3438 and on the concept of a master.
3439 Therefore, we have moved the definitions of these concepts here,
3440 from where they used to be in Section 9.
3441 These concepts also needed to be generalized somewhat.
3442 Task waiting is closely related to user-defined finalization;
3443 the rules here refer to the task-waiting rules of Section 9.
3444 @end{DiffWord83}
3445
3446 @begin{DiffWord95}
3447 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0021],ARef=[AI95-00182-01]}
3448 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Fixed the wording to say that
3449 anonymous objects aren't finalized until the object can't be used anymore.]}
3450
3451 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0023],ARef=[AI95-00169-01]}
3452 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Added wording to clarify what
3453 happens when Adjust or Finalize raises an exception; some cases had been
3454 omitted.]}
3455
3456 @ChgRef{Version=[1],Kind=[AddedNormal],Ref=[8652/0024],ARef=[AI95-00193-01]}
3457 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00256-01]}
3458 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Stated that if Adjust raises an
3459 exception during initialization, nothing further is required. This is
3460 corrected in Ada 2005 to include all kinds of assignment other than
3461 @nt{assignment_statement}s.]}
3462
3463 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00162-01],ARef=[AI95-00416-01]}
3464 @ChgAdded{Version=[2],Text=[Revised the definition of master to include
3465 @nt{expression}s and @nt{statement}s, in order to cleanly define what
3466 happens for tasks and controlled objects created as part of a subprogram call.
3467 Having done that, all of the special wording to cover those cases is
3468 eliminated (at least until the Ada comments start rolling in).]}
3469
3470 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00280-01]}
3471 @ChgAdded{Version=[2],Text=[We define @i{finalization of the collection}
3472 here, so as to be able to conveniently refer to it in other rules (especially
3473 in @RefSec{Allocators}).]}
3474
3475 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
3476 @ChgAdded{Version=[2],Text=[Clarified that a coextension is finalized at
3477 the same time as the outer object. (This was intended for Ada 95, but since
3478 the concept did not have a name, it was overlooked.)]}
3479
3480 @end{DiffWord95}