1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>AARM95 - User-Defined Assignment and Finalization</TITLE>
<META NAME="Author" CONTENT="JTC1/SC22/WG9/ARG, by Randall Brukardt, ARG Editor">
<META NAME="GENERATOR" CONTENT="Arm_Form.Exe, Ada Reference Manual generator">
<STYLE type="text/css">
DIV.paranum {position: absolute; font-family: Arial, Helvetica, sans-serif; left: 0.5 em; top: auto}
TT {font-family: "Courier New", monospace}
DT {display: compact}
DIV.Normal {font-family: "Times New Roman", Times, serif; margin-bottom: 0.6em}
DIV.Wide {font-family: "Times New Roman", Times, serif; margin-top: 0.6em; margin-bottom: 0.6em}
DIV.Annotations {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-bottom: 0.6em}
DIV.WideAnnotations {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0.6em; margin-bottom: 0.6em}
DIV.Index {font-family: "Times New Roman", Times, serif}
DIV.SyntaxSummary {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.4em}
DIV.Notes {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.6em}
DIV.NotesHeader {font-family: "Times New Roman", Times, serif; margin-left: 2.0em}
DIV.SyntaxIndented {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.4em}
DIV.Indented {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-bottom: 0.6em}
DIV.CodeIndented {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-bottom: 0.6em}
DIV.SmallIndented {font-family: "Times New Roman", Times, serif; margin-left: 10.0em; margin-bottom: 0.6em}
DIV.SmallCodeIndented {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-bottom: 0.6em}
DIV.Examples {font-family: "Courier New", monospace; margin-left: 2.0em; margin-bottom: 0.6em}
DIV.SmallExamples {font-family: "Courier New", monospace; font-size: 80%; margin-left: 7.5em; margin-bottom: 0.6em}
DIV.IndentedExamples {font-family: "Courier New", monospace; margin-left: 8.0em; margin-bottom: 0.6em}
DIV.SmallIndentedExamples {font-family: "Courier New", monospace; font-size: 80%; margin-left: 15.0em; margin-bottom: 0.6em}
UL.Bulleted {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SmallBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SmallNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.IndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.CodeIndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.CodeIndentedNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SyntaxIndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NotesBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NotesNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
DL.Hanging {font-family: "Times New Roman", Times, serif; margin-top: 0em; margin-bottom: 0.6em}
DD.Hanging {margin-left: 6.0em}
DL.IndentedHanging {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.IndentedHanging {margin-left: 2.0em}
DL.HangingInBulleted {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.HangingInBulleted {margin-left: 4.0em}
DL.SmallHanging {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.SmallHanging {margin-left: 7.5em}
DL.SmallIndentedHanging {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.SmallIndentedHanging {margin-left: 2.0em}
DL.SmallHangingInBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.SmallHangingInBulleted {margin-left: 5.0em}
DL.Enumerated {font-family: "Times New Roman", Times, serif; margin-right: 0.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.Enumerated {margin-left: 2.0em}
DL.SmallEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.SmallEnumerated {margin-left: 2.5em}
DL.NestedEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
DL.SmallNestedEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
</STYLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFF0" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
<P><A HREF="AA-TOC.html">Contents</A> <A HREF="AA-0-29.html">Index</A> <A HREF="AA-7-5.html">Previous</A> <A HREF="AA-7-6-1.html">Next</A></P>
<HR>
<H1> 7.6 User-Defined Assignment and Finalization</H1>
<DIV Class="Paranum"><FONT SIZE=-2>1</FONT></DIV>
<DIV Class="Normal"> [<A NAME="I3259"></A> <A NAME="I3260"></A>Three
kinds of actions are fundamental to the manipulation of objects: initialization,
finalization, and assignment. Every object is initialized, either explicitly
or by default, after being created (for example, by an <FONT FACE="Arial, Helvetica">object_declaration</FONT>
or <FONT FACE="Arial, Helvetica">allocator</FONT>). Every object is finalized
before being destroyed (for example, by leaving a <FONT FACE="Arial, Helvetica">subprogram_body</FONT>
containing an <FONT FACE="Arial, Helvetica">object_declaration</FONT>,
or by a call to an instance of Unchecked_Deallocation). An assignment
operation is used as part of <FONT FACE="Arial, Helvetica">assignment_statement</FONT>s,
explicit initialization, parameter passing, and other operations. <A NAME="I3261"></A><A NAME="I3262"></A><A NAME="I3263"></A></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>2</FONT></DIV>
<DIV Class="Normal"> Default definitions for these three fundamental
operations are provided by the language, but <A NAME="I3264"></A>a <I>controlled</I>
type gives the user additional control over parts of these operations.
<A NAME="I3265"></A><A NAME="I3266"></A><A NAME="I3267"></A>In particular,
the user can define, for a controlled type, an Initialize procedure which
is invoked immediately after the normal default initialization of a controlled
object, a Finalize procedure which is invoked immediately before finalization
of any of the components of a controlled object, and an Adjust procedure
which is invoked as the last step of an assignment to a (nonlimited)
controlled object.] </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>2.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Glossary entry: </B>A controlled
type supports user-defined assignment and finalization. Objects are always
finalized before being destroyed.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>2.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Ramification: </B>Here's the
basic idea of initialization, value adjustment, and finalization, whether
or not user defined: When an object is created, if it is explicitly assigned
an initial value, the assignment copies and adjusts the initial value.
Otherwise, Initialize is applied to it (except in the case of an <FONT FACE="Arial, Helvetica">aggregate</FONT>
as a whole). An <FONT FACE="Arial, Helvetica">assignment_statement</FONT>
finalizes the target before copying in and adjusting the new value. Whenever
an object goes away, it is finalized. Calls on Initialize and Adjust
happen bottom-up; that is, components first, followed by the containing
object. Calls on Finalize happens top-down; that is, first the containing
object, and then its components. These ordering rules ensure that any
components will be in a well-defined state when Initialize, Adjust, or
Finalize is applied to the containing object. </FONT></DIV>
<H4 ALIGN=CENTER>Static Semantics</H4>
<DIV Class="Paranum"><FONT SIZE=-2>3</FONT></DIV>
<DIV Class="Normal" Style="margin-bottom: 0.4em"> The following language-defined
library package exists: </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4/1</FONT></DIV>
<DIV Class="Examples"><TT>{<I><A HREF="defect1.html#8652/0020">8652/0020</A></I>}
<BR>
<B>package</B> Ada.Finalization <B>is</B><A NAME="I3268"></A><A NAME="I3269"></A><A NAME="I3270"></A><BR>
<B>pragma</B> Preelaborate(Finalization);<U><BR>
<B>pragma</B> Remote_Types(Finalization);</U></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>5</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I3271"></A><A NAME="I3272"></A>Controlled <B>is abstract tagged private</B>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>6</FONT></DIV>
<DIV Class="Examples"><TT> <B>procedure</B> <A NAME="I3273"></A><A NAME="I3274"></A>Initialize (Object : <B>in out</B> Controlled);<BR>
<B>procedure</B> <A NAME="I3275"></A><A NAME="I3276"></A>Adjust (Object : <B>in out</B> Controlled);<BR>
<B>procedure</B> <A NAME="I3277"></A><A NAME="I3278"></A>Finalize (Object : <B>in out</B> Controlled);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>7</FONT></DIV>
<DIV Class="Examples"><TT> <B>type</B> <A NAME="I3279"></A><A NAME="I3280"></A>Limited_Controlled <B>is abstract tagged limited private</B>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>8</FONT></DIV>
<DIV Class="Examples"><TT> <B>procedure</B> <A NAME="I3281"></A><A NAME="I3282"></A>Initialize (Object : <B>in out</B> Limited_Controlled);<BR>
<B>procedure</B> <A NAME="I3283"></A><A NAME="I3284"></A>Finalize (Object : <B>in out</B> Limited_Controlled);<BR>
<B>private</B><BR>
... -- <I>not specified by the language</I><BR>
<B>end</B> Ada.Finalization;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>9</FONT></DIV>
<DIV Class="Normal"> <A NAME="I3285"></A>A controlled type is a descendant
of Controlled or Limited_Controlled. The (default) implementations of
Initialize, Adjust, and Finalize have no effect. The predefined "="
operator of type Controlled always returns True, [since this operator
is incorporated into the implementation of the predefined equality operator
of types derived from Controlled, as explained in <A HREF="AA-4-5-2.html">4.5.2</A>.]
The type Limited_Controlled is like Controlled, except that it is limited
and it lacks the primitive subprogram Adjust. </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>9.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Discussion: </B>We say ``nonlimited
controlled type'' (rather than just ``controlled type''; when we
want to talk about descendants of Controlled only. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>9.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Reason: </B>We considered making
Adjust and Finalize abstract. However, a reasonable coding convention
is e.g. for Finalize to always call the parent's Finalize after doing
whatever work is needed for the extension part. (Unlike CLOS, we have
no way to do that automatically in Ada 95.) For this to work, Finalize
cannot be abstract. In a generic unit, for a generic formal abstract
derived type whose ancestor is Controlled or Limited_Controlled, calling
the ancestor's Finalize would be illegal if it were abstract, even though
the actual type might have a concrete version.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>9.c</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>Types Controlled and Limited_Controlled
are abstract, even though they have no abstract primitive subprograms.
It is not clear that they need to be abstract, but there seems to be
no harm in it, and it might make an implementation's life easier to know
that there are no objects of these types -- in case the implementation
wishes to make them ``magic'' in some way. </FONT></DIV>
<H4 ALIGN=CENTER>Dynamic Semantics</H4>
<DIV Class="Paranum"><FONT SIZE=-2>10</FONT></DIV>
<DIV Class="Normal"> <A NAME="I3286"></A>During the elaboration of
an <FONT FACE="Arial, Helvetica">object_declaration</FONT>, for every
controlled subcomponent of the object that is not assigned an initial
value (as defined in <A HREF="AA-3-3-1.html">3.3.1</A>), Initialize is
called on that subcomponent. Similarly, if the object as a whole is controlled
and is not assigned an initial value, Initialize is called on the object.
The same applies to the evaluation of an <FONT FACE="Arial, Helvetica">allocator</FONT>,
as explained in <A HREF="AA-4-8.html">4.8</A>.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11/1</FONT></DIV>
<DIV Class="Normal"> {<I><A HREF="defect1.html#8652/0021">8652/0021</A></I>}
For an <FONT FACE="Arial, Helvetica">extension_aggregate</FONT> whose
<FONT FACE="Arial, Helvetica">ancestor_part</FONT> is a <FONT FACE="Arial, Helvetica">subtype_mark</FONT>,
<U>for each controlled subcomponent of the ancestor part, either Initialize
is called, or its initial value is assigned, as appropriate</U><S>Initialize
is called on all controlled subcomponents of the ancestor part</S>; if
the type of the ancestor part is itself controlled, the Initialize procedure
of the ancestor type is called, unless that Initialize procedure is abstract.
</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11.a</FONT></DIV>
<DIV Class="Annotations" Style="margin-bottom: 0.4em"><FONT SIZE=-1><B>Discussion:
</B>Example: </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11.b</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>type</B> T1 <B>is</B> <B>new</B> Controlled <B>with</B><BR>
<B>record</B><BR>
... --<I> some components might have defaults</I><BR>
<B>end</B> <B>record</B>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11.c</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>type</B> T2 <B>is</B> <B>new</B> Controlled <B>with</B><BR>
<B>record</B><BR>
X : T1; --<I> no default</I><BR>
Y : T1 := ...; --<I> default</I><BR>
<B>end</B> <B>record</B>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11.d</FONT></DIV>
<DIV Class="SmallExamples"><TT>A : T2;<BR>
B : T2 := ...;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11.e</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>As part of the elaboration of
A's declaration, A.Y is assigned a value; therefore Initialize is not
applied to A.Y. Instead, Adjust is applied to A.Y as part of the assignment
operation. Initialize is applied to A.X and to A, since those objects
are not assigned an initial value. The assignment to A.Y is not considered
an assignment to A.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11.f</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>For the elaboration of B's declaration,
Initialize is not called at all. Instead the assignment adjusts B's value;
that is, it applies Adjust to B.X, B.Y, and B.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11.f.1/1</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>{<I><A HREF="defect1.html#8652/0021">8652/0021</A></I>}
<U>The <FONT FACE="Arial, Helvetica">ancestor_part</FONT> of an <FONT FACE="Arial, Helvetica">extension_aggregate</FONT>
is handled similarly.</U> </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>12</FONT></DIV>
<DIV Class="Normal"> Initialize and other initialization operations
are done in an arbitrary order, except as follows. Initialize is applied
to an object after initialization of its subcomponents, if any [(including
both implicit initialization and Initialize calls)]. If an object has
a component with an access discriminant constrained by a per-object expression,
Initialize is applied to this component after any components that do
not have such discriminants. For an object with several components with
such a discriminant, Initialize is applied to them in order of their
<FONT FACE="Arial, Helvetica">component_declaration</FONT>s. For an <FONT FACE="Arial, Helvetica">allocator</FONT>,
any task activations follow all calls on Initialize. </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>12.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Reason: </B>The fact that Initialize
is done for subcomponents first allows Initialize for a composite object
to refer to its subcomponents knowing they have been properly initialized.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>12.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>The fact that Initialize is done
for components with access discriminants after other components allows
the Initialize operation for a component with a self-referential access
discriminant to assume that other components of the enclosing object
have already been properly initialized. For multiple such components,
it allows some predictability. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>13</FONT></DIV>
<DIV Class="Normal" Style="margin-bottom: 0.4em"> <A NAME="I3287"></A>When
a target object with any controlled parts is assigned a value, [either
when created or in a subsequent <FONT FACE="Arial, Helvetica">assignment_statement</FONT>,]
the <I>assignment operation</I> proceeds as follows: </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>14</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>The value of the target becomes the assigned value.</LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>15</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC><A NAME="I3288"></A><A NAME="I3289"></A>The value of the
target is <I>adjusted.</I> </LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>15.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Ramification: </B>If any parts
of the object are controlled, abort is deferred during the assignment
operation. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>16</FONT></DIV>
<DIV Class="Normal"> <A NAME="I3290"></A><A NAME="I3291"></A>To adjust
the value of a [(nonlimited)] composite object, the values of the components
of the object are first adjusted in an arbitrary order, and then, if
the object is controlled, Adjust is called. Adjusting the value of an
elementary object has no effect[, nor does adjusting the value of a composite
object with no controlled parts.] </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>16.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Ramification: </B>Adjustment
is never performed for values of a by-reference limited type, since these
types do not support copying. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>16.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Reason: </B>The verbiage in
the Initialize rule about access discriminants constrained by per-object
expressions is not necessary here, since such types are limited, and
therefore are never adjusted. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17</FONT></DIV>
<DIV Class="Normal"> <A NAME="I3292"></A>For an <FONT FACE="Arial, Helvetica">assignment_statement</FONT>,
[ after the <FONT FACE="Arial, Helvetica">name</FONT> and <FONT FACE="Arial, Helvetica">expression</FONT>
have been evaluated, and any conversion (including constraint checking)
has been done,] an anonymous object is created, and the value is assigned
into it; [that is, the assignment operation is applied]. [(Assignment
includes value adjustment.)] The target of the <FONT FACE="Arial, Helvetica">assignment_statement</FONT>
is then finalized. The value of the anonymous object is then assigned
into the target of the <FONT FACE="Arial, Helvetica">assignment_statement</FONT>.
Finally, the anonymous object is finalized. [As explained below, the
implementation may eliminate the intermediate anonymous object, so this
description subsumes the one given in <A HREF="AA-5-2.html">5.2</A>,
``<A HREF="AA-5-2.html">Assignment Statements</A>''.] </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.a</FONT></DIV>
<DIV Class="Annotations" Style="margin-bottom: 0.4em"><FONT SIZE=-1><B>Reason:
</B>An alternative design for user-defined assignment might involve an
Assign operation instead of Adjust: </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.b</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>procedure</B> Assign(Target : <B>in</B> <B>out</B> Controlled; Source : <B>in</B> <B>out</B> Controlled);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.c</FONT></DIV>
<DIV Class="Annotations" Style="margin-bottom: 0.4em"><FONT SIZE=-1>Or
perhaps even a syntax like this: </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.d</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>procedure</B> ":="(Target : <B>in</B> <B>out</B> Controlled; Source : <B>in</B> <B>out</B> Controlled);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.e</FONT></DIV>
<DIV Class="Annotations" Style="margin-bottom: 0.4em"><FONT SIZE=-1>Assign
(or ":=") would have the responsibility of doing the copy,
as well as whatever else is necessary. This would have the advantage
that the Assign operation knows about both the target and the source
at the same time -- it would be possible to do things like reuse storage
belonging to the target, for example, which Adjust cannot do. However,
this sort of design would not work in the case of unconstrained discriminated
variables, because there is no way to change the discriminants individually.
For example: </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.f</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>type</B> Mutable(D : Integer := 0) <B>is</B><BR>
<B>record</B><BR>
X : Array_Of_Controlled_Things(1..D);<BR>
<B>case</B> D <B>is</B><BR>
<B>when</B> 17 => Y : Controlled_Thing;<BR>
<B>when</B> <B>others</B> => <B>null</B>;<BR>
<B>end</B> D;<BR>
<B>end</B> <B>record</B>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.g</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>An assignment to an unconstrained
variable of type Mutable can cause some of the components of X, and the
component Y, to appear and/or disappear. There is no way to write the
Assign operation to handle this sort of case.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.h</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>Forbidding such cases is not an
option -- it would cause generic contract model violations. </FONT></DIV>
<H4 ALIGN=CENTER>Implementation Requirements</H4>
<DIV Class="Paranum"><FONT SIZE=-2>17.1/1</FONT></DIV>
<DIV Class="Normal"> {<I><A HREF="defect1.html#8652/0022">8652/0022</A></I>}
<U>For an <FONT FACE="Arial, Helvetica">aggregate</FONT> of a controlled
type whose value is assigned, other than by an <FONT FACE="Arial, Helvetica">assignment_statement</FONT>
or a <FONT FACE="Arial, Helvetica">return_statement</FONT>, the implementation
shall not create a separate anonymous object for the <FONT FACE="Arial, Helvetica">aggregate</FONT>.
The aggregate value shall be constructed directly in the target of the
assignment operation and Adjust is not called on the target object.</U>
</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.h.1/1</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Reason: </B><U>This is necessary
to prevent elaboration problems with deferred constants of controlled
types. Consider:</U> </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.h.2/1</FONT></DIV>
<DIV Class="SmallExamples"><TT><U><B>package</B> P <B>is</B><BR>
<B>type</B> Dyn_String <B>is private</B>;<BR>
Null_String : <B>constant</B> Dyn_String;<BR>
...<BR>
<B>private</B><BR>
<B>type</B> Dyn_String <B>is new</B> Ada.Finalization.Controlled <B>with</B> ...<BR>
<B>procedure</B> Finalize(X : <B>in out</B> Dyn_String);<BR>
<B>procedure</B> Adjust(X : <B>in out</B> Dyn_String);<BR>
<BR>
Null_String : <B>constant</B> Dyn_String :=<BR>
(Ada.Finalization.Controlled <B>with</B> ...);<BR>
...<BR>
<B>end</B> P;</U></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17.h.3/1</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><U>When Null_String is elaborated,
the bodies of Finalize and Adjust clearly have not been elaborated. Without
this rule, this declaration would necessarily raise Program_Error (unless
the permissions given below are used by the compiler).</U> </FONT></DIV>
<H4 ALIGN=CENTER>Implementation Permissions</H4>
<DIV Class="Paranum"><FONT SIZE=-2>18</FONT></DIV>
<DIV Class="Normal"> An implementation is allowed to relax the above
rules [(for nonlimited controlled types)] in the following ways: </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>18.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Proof: </B>The phrase ``for
nonlimited controlled types'' follows from the fact that all of the following
permissions apply to cases involving assignment. It is important because
the programmer can count on a stricter semantics for limited controlled
types. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>19</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>For an <FONT FACE="Arial, Helvetica">assignment_statement</FONT>
that assigns to an object the value of that same object, the implementation
need not do anything. </LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>19.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Ramification: </B>In other
words, even if an object is controlled and a combination of Finalize
and Adjust on the object might have a net side effect, they need not
be performed. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>20</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>For an <FONT FACE="Arial, Helvetica">assignment_statement</FONT>
for a noncontrolled type, the implementation may finalize and assign
each component of the variable separately (rather than finalizing the
entire variable and assigning the entire new value) unless a discriminant
of the variable is changed by the assignment. </LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>20.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Reason: </B>For example, in
a slice assignment, an anonymous object is not necessary if the slice
is copied component-by-component in the right direction, since array
types are not controlled (although their components may be). Note that
the direction, and even the fact that it's a slice assignment, can in
general be determined only at run time. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>21</FONT></DIV>
<UL Class="Bulleted"><LI TYPE=DISC>For an <FONT FACE="Arial, Helvetica">aggregate</FONT> or
function call whose value is assigned into a target object, the implementation
need not create a separate anonymous object if it can safely create the
value of the <FONT FACE="Arial, Helvetica">aggregate</FONT> or function
call directly in the target object. Similarly, for an <FONT FACE="Arial, Helvetica">assignment_statement</FONT>,
the implementation need not create an anonymous object if the value being
assigned is the result of evaluating a <FONT FACE="Arial, Helvetica">name</FONT>
denoting an object (the source object) whose storage cannot overlap with
the target. If the source object might overlap with the target object,
then the implementation can avoid the need for an intermediary anonymous
object by exercising one of the above permissions and perform the assignment
one component at a time (for an overlapping array assignment), or not
at all (for an assignment where the target and the source of the assignment
are the same object). Even if an anonymous object is created, the implementation
may move its value to the target object as part of the assignment without
re-adjusting so long as the anonymous object has no aliased subcomponents.
</LI></UL>
<DIV Class="Paranum"><FONT SIZE=-2>21.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Ramification: </B>In the <FONT FACE="Arial, Helvetica">aggregate</FONT>
case, only one value adjustment is necessary, and there is no anonymous
object to be finalized.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>21.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>In the <FONT FACE="Arial, Helvetica">assignment_statement</FONT>
case as well, no finalization of the anonymous object is needed. On the
other hand, if the target has aliased subcomponents, then an adjustment
takes place directly on the target object as the last step of the assignment,
since some of the subcomponents may be self-referential or otherwise
position-dependent. </FONT></DIV>
<H4 ALIGN=CENTER>Extensions to Ada 83</H4>
<DIV Class="Paranum"><FONT SIZE=-2>21.c</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><A NAME="I3293"></A>Controlled
types and user-defined finalization are new to Ada 95. (Ada 83 had finalization
semantics only for masters of tasks.) </FONT></DIV>
<HR>
<P><A HREF="AA-TOC.html">Contents</A> <A HREF="AA-0-29.html">Index</A> <A HREF="AA-7-5.html">Previous</A> <A HREF="AA-7-6-1.html">Next</A> <A HREF="AA-TTL.html">Legal</A></P>
</BODY>
</HTML>
|