annotate gcc/ada/doc/gnat_ugn/elaboration_order_handling_in_gnat.rst @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 .. role:: switch(samp)
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 .. |with| replace:: *with*
kono
parents:
diff changeset
4 .. |withs| replace:: *with*\ s
kono
parents:
diff changeset
5 .. |withed| replace:: *with*\ ed
kono
parents:
diff changeset
6 .. |withing| replace:: *with*\ ing
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 .. -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 .. _Elaboration_Order_Handling_in_GNAT:
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 **********************************
kono
parents:
diff changeset
14 Elaboration Order Handling in GNAT
kono
parents:
diff changeset
15 **********************************
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 .. index:: Order of elaboration
kono
parents:
diff changeset
18 .. index:: Elaboration control
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 This appendix describes the handling of elaboration code in Ada and GNAT, and
kono
parents:
diff changeset
21 discusses how the order of elaboration of program units can be controlled in
kono
parents:
diff changeset
22 GNAT, either automatically or with explicit programming features.
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 .. _Elaboration_Code:
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 Elaboration Code
kono
parents:
diff changeset
27 ================
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 Ada defines the term *execution* as the process by which a construct achieves
kono
parents:
diff changeset
30 its run-time effect. This process is also referred to as **elaboration** for
kono
parents:
diff changeset
31 declarations and *evaluation* for expressions.
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 The execution model in Ada allows for certain sections of an Ada program to be
kono
parents:
diff changeset
34 executed prior to execution of the program itself, primarily with the intent of
kono
parents:
diff changeset
35 initializing data. These sections are referred to as **elaboration code**.
kono
parents:
diff changeset
36 Elaboration code is executed as follows:
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 * All partitions of an Ada program are executed in parallel with one another,
kono
parents:
diff changeset
39 possibly in a separate address space, and possibly on a separate computer.
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 * The execution of a partition involves running the environment task for that
kono
parents:
diff changeset
42 partition.
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 * The environment task executes all elaboration code (if available) for all
kono
parents:
diff changeset
45 units within that partition. This code is said to be executed at
kono
parents:
diff changeset
46 **elaboration time**.
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 * The environment task executes the Ada program (if available) for that
kono
parents:
diff changeset
49 partition.
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 In addition to the Ada terminology, this appendix defines the following terms:
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 * *Scenario*
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 A construct that is elaborated or executed by elaboration code is referred to
kono
parents:
diff changeset
56 as an *elaboration scenario* or simply a **scenario**. GNAT recognizes the
kono
parents:
diff changeset
57 following scenarios:
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 - ``'Access`` of entries, operators, and subprograms
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 - Activation of tasks
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 - Calls to entries, operators, and subprograms
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 - Instantiations of generic templates
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 * *Target*
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 A construct elaborated by a scenario is referred to as *elaboration target*
kono
parents:
diff changeset
70 or simply **target**. GNAT recognizes the following targets:
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 - For ``'Access`` of entries, operators, and subprograms, the target is the
kono
parents:
diff changeset
73 entry, operator, or subprogram being aliased.
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 - For activation of tasks, the target is the task body
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 - For calls to entries, operators, and subprograms, the target is the entry,
kono
parents:
diff changeset
78 operator, or subprogram being invoked.
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 - For instantiations of generic templates, the target is the generic template
kono
parents:
diff changeset
81 being instantiated.
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 Elaboration code may appear in two distinct contexts:
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 * *Library level*
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 A scenario appears at the library level when it is encapsulated by a package
kono
parents:
diff changeset
88 [body] compilation unit, ignoring any other package [body] declarations in
kono
parents:
diff changeset
89 between.
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 ::
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 with Server;
kono
parents:
diff changeset
94 package Client is
kono
parents:
diff changeset
95 procedure Proc;
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 package Nested is
kono
parents:
diff changeset
98 Val : ... := Server.Func;
kono
parents:
diff changeset
99 end Nested;
kono
parents:
diff changeset
100 end Client;
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 In the example above, the call to ``Server.Func`` is an elaboration scenario
kono
parents:
diff changeset
103 because it appears at the library level of package ``Client``. Note that the
kono
parents:
diff changeset
104 declaration of package ``Nested`` is ignored according to the definition
kono
parents:
diff changeset
105 given above. As a result, the call to ``Server.Func`` will be executed when
kono
parents:
diff changeset
106 the spec of unit ``Client`` is elaborated.
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 * *Package body statements*
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 A scenario appears within the statement sequence of a package body when it is
kono
parents:
diff changeset
111 bounded by the region starting from the ``begin`` keyword of the package body
kono
parents:
diff changeset
112 and ending at the ``end`` keyword of the package body.
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 ::
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 package body Client is
kono
parents:
diff changeset
117 procedure Proc is
kono
parents:
diff changeset
118 begin
kono
parents:
diff changeset
119 ...
kono
parents:
diff changeset
120 end Proc;
kono
parents:
diff changeset
121 begin
kono
parents:
diff changeset
122 Proc;
kono
parents:
diff changeset
123 end Client;
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 In the example above, the call to ``Proc`` is an elaboration scenario because
kono
parents:
diff changeset
126 it appears within the statement sequence of package body ``Client``. As a
kono
parents:
diff changeset
127 result, the call to ``Proc`` will be executed when the body of ``Client`` is
kono
parents:
diff changeset
128 elaborated.
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 .. _Elaboration_Order:
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 Elaboration Order
kono
parents:
diff changeset
133 =================
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 The sequence by which the elaboration code of all units within a partition is
kono
parents:
diff changeset
136 executed is referred to as **elaboration order**.
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 Within a single unit, elaboration code is executed in sequential order.
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 ::
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 package body Client is
kono
parents:
diff changeset
143 Result : ... := Server.Func;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 procedure Proc is
kono
parents:
diff changeset
146 package Inst is new Server.Gen;
kono
parents:
diff changeset
147 begin
kono
parents:
diff changeset
148 Inst.Eval (Result);
kono
parents:
diff changeset
149 end Proc;
kono
parents:
diff changeset
150 begin
kono
parents:
diff changeset
151 Proc;
kono
parents:
diff changeset
152 end Client;
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 In the example above, the elaboration order within package body ``Client`` is
kono
parents:
diff changeset
155 as follows:
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 1. The object declaration of ``Result`` is elaborated.
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 * Function ``Server.Func`` is invoked.
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 2. The subprogram body of ``Proc`` is elaborated.
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 3. Procedure ``Proc`` is invoked.
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 * Generic unit ``Server.Gen`` is instantiated as ``Inst``.
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 * Instance ``Inst`` is elaborated.
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 * Procedure ``Inst.Eval`` is invoked.
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 The elaboration order of all units within a partition depends on the following
kono
parents:
diff changeset
172 factors:
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 * |withed| units
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 * purity of units
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 * preelaborability of units
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 * presence of elaboration control pragmas
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 A program may have several elaboration orders depending on its structure.
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 ::
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 package Server is
kono
parents:
diff changeset
187 function Func (Index : Integer) return Integer;
kono
parents:
diff changeset
188 end Server;
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 ::
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 package body Server is
kono
parents:
diff changeset
193 Results : array (1 .. 5) of Integer := (1, 2, 3, 4, 5);
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 function Func (Index : Integer) return Integer is
kono
parents:
diff changeset
196 begin
kono
parents:
diff changeset
197 return Results (Index);
kono
parents:
diff changeset
198 end Func;
kono
parents:
diff changeset
199 end Server;
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 ::
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 with Server;
kono
parents:
diff changeset
204 package Client is
kono
parents:
diff changeset
205 Val : constant Integer := Server.Func (3);
kono
parents:
diff changeset
206 end Client;
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 ::
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 with Client;
kono
parents:
diff changeset
211 procedure Main is begin null; end Main;
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 The following elaboration order exhibits a fundamental problem referred to as
kono
parents:
diff changeset
214 *access-before-elaboration* or simply **ABE**.
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 ::
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 spec of Server
kono
parents:
diff changeset
219 spec of Client
kono
parents:
diff changeset
220 body of Server
kono
parents:
diff changeset
221 body of Main
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 The elaboration of ``Server``'s spec materializes function ``Func``, making it
kono
parents:
diff changeset
224 callable. The elaboration of ``Client``'s spec elaborates the declaration of
kono
parents:
diff changeset
225 ``Val``. This invokes function ``Server.Func``, however the body of
kono
parents:
diff changeset
226 ``Server.Func`` has not been elaborated yet because ``Server``'s body comes
kono
parents:
diff changeset
227 after ``Client``'s spec in the elaboration order. As a result, the value of
kono
parents:
diff changeset
228 constant ``Val`` is now undefined.
kono
parents:
diff changeset
229
kono
parents:
diff changeset
230 Without any guarantees from the language, an undetected ABE problem may hinder
kono
parents:
diff changeset
231 proper initialization of data, which in turn may lead to undefined behavior at
kono
parents:
diff changeset
232 run time. To prevent such ABE problems, Ada employs dynamic checks in the same
kono
parents:
diff changeset
233 vein as index or null exclusion checks. A failed ABE check raises exception
kono
parents:
diff changeset
234 ``Program_Error``.
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 The following elaboration order avoids the ABE problem and the program can be
kono
parents:
diff changeset
237 successfully elaborated.
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 ::
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 spec of Server
kono
parents:
diff changeset
242 body of Server
kono
parents:
diff changeset
243 spec of Client
kono
parents:
diff changeset
244 body of Main
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 Ada states that a total elaboration order must exist, but it does not define
kono
parents:
diff changeset
247 what this order is. A compiler is thus tasked with choosing a suitable
kono
parents:
diff changeset
248 elaboration order which satisfies the dependencies imposed by |with| clauses,
kono
parents:
diff changeset
249 unit categorization, and elaboration control pragmas. Ideally an order which
kono
parents:
diff changeset
250 avoids ABE problems should be chosen, however a compiler may not always find
kono
parents:
diff changeset
251 such an order due to complications with respect to control and data flow.
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 .. _Checking_the_Elaboration_Order:
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 Checking the Elaboration Order
kono
parents:
diff changeset
256 ==============================
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 To avoid placing the entire elaboration order burden on the programmer, Ada
kono
parents:
diff changeset
259 provides three lines of defense:
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 * *Static semantics*
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 Static semantic rules restrict the possible choice of elaboration order. For
kono
parents:
diff changeset
264 instance, if unit Client |withs| unit Server, then the spec of Server is
kono
parents:
diff changeset
265 always elaborated prior to Client. The same principle applies to child units
kono
parents:
diff changeset
266 - the spec of a parent unit is always elaborated prior to the child unit.
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 * *Dynamic semantics*
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 Dynamic checks are performed at run time, to ensure that a target is
kono
parents:
diff changeset
271 elaborated prior to a scenario that executes it, thus avoiding ABE problems.
kono
parents:
diff changeset
272 A failed run-time check raises exception ``Program_Error``. The following
kono
parents:
diff changeset
273 restrictions apply:
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 - *Restrictions on calls*
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 An entry, operator, or subprogram can be called from elaboration code only
kono
parents:
diff changeset
278 when the corresponding body has been elaborated.
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 - *Restrictions on instantiations*
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 A generic unit can be instantiated by elaboration code only when the
kono
parents:
diff changeset
283 corresponding body has been elaborated.
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 - *Restrictions on task activation*
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 A task can be activated by elaboration code only when the body of the
kono
parents:
diff changeset
288 associated task type has been elaborated.
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 The restrictions above can be summarized by the following rule:
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 *If a target has a body, then this body must be elaborated prior to the
kono
parents:
diff changeset
293 execution of the scenario that invokes, instantiates, or activates the
kono
parents:
diff changeset
294 target.*
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 * *Elaboration control*
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 Pragmas are provided for the programmer to specify the desired elaboration
kono
parents:
diff changeset
299 order.
kono
parents:
diff changeset
300
kono
parents:
diff changeset
301 .. _Controlling_the_Elaboration_Order_in_Ada:
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 Controlling the Elaboration Order in Ada
kono
parents:
diff changeset
304 ========================================
kono
parents:
diff changeset
305
kono
parents:
diff changeset
306 Ada provides several idioms and pragmas to aid the programmer with specifying
kono
parents:
diff changeset
307 the desired elaboration order and avoiding ABE problems altogether.
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 * *Packages without a body*
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 A library package which does not require a completing body does not suffer
kono
parents:
diff changeset
312 from ABE problems.
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 ::
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 package Pack is
kono
parents:
diff changeset
317 generic
kono
parents:
diff changeset
318 type Element is private;
kono
parents:
diff changeset
319 package Containers is
kono
parents:
diff changeset
320 type Element_Array is array (1 .. 10) of Element;
kono
parents:
diff changeset
321 end Containers;
kono
parents:
diff changeset
322 end Pack;
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324 In the example above, package ``Pack`` does not require a body because it
kono
parents:
diff changeset
325 does not contain any constructs which require completion in a body. As a
kono
parents:
diff changeset
326 result, generic ``Pack.Containers`` can be instantiated without encountering
kono
parents:
diff changeset
327 any ABE problems.
kono
parents:
diff changeset
328
kono
parents:
diff changeset
329 .. index:: pragma Pure
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 * *pragma Pure*
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 Pragma ``Pure`` places sufficient restrictions on a unit to guarantee that no
kono
parents:
diff changeset
334 scenario within the unit can result in an ABE problem.
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 .. index:: pragma Preelaborate
kono
parents:
diff changeset
337
kono
parents:
diff changeset
338 * *pragma Preelaborate*
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 Pragma ``Preelaborate`` is slightly less restrictive than pragma ``Pure``,
kono
parents:
diff changeset
341 but still strong enough to prevent ABE problems within a unit.
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 .. index:: pragma Elaborate_Body
kono
parents:
diff changeset
344
kono
parents:
diff changeset
345 * *pragma Elaborate_Body*
kono
parents:
diff changeset
346
kono
parents:
diff changeset
347 Pragma ``Elaborate_Body`` requires that the body of a unit is elaborated
kono
parents:
diff changeset
348 immediately after its spec. This restriction guarantees that no client
kono
parents:
diff changeset
349 scenario can execute a server target before the target body has been
kono
parents:
diff changeset
350 elaborated because the spec and body are effectively "glued" together.
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 ::
kono
parents:
diff changeset
353
kono
parents:
diff changeset
354 package Server is
kono
parents:
diff changeset
355 pragma Elaborate_Body;
kono
parents:
diff changeset
356
kono
parents:
diff changeset
357 function Func return Integer;
kono
parents:
diff changeset
358 end Server;
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 ::
kono
parents:
diff changeset
361
kono
parents:
diff changeset
362 package body Server is
kono
parents:
diff changeset
363 function Func return Integer is
kono
parents:
diff changeset
364 begin
kono
parents:
diff changeset
365 ...
kono
parents:
diff changeset
366 end Func;
kono
parents:
diff changeset
367 end Server;
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 ::
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 with Server;
kono
parents:
diff changeset
372 package Client is
kono
parents:
diff changeset
373 Val : constant Integer := Server.Func;
kono
parents:
diff changeset
374 end Client;
kono
parents:
diff changeset
375
kono
parents:
diff changeset
376 In the example above, pragma ``Elaborate_Body`` guarantees the following
kono
parents:
diff changeset
377 elaboration order:
kono
parents:
diff changeset
378
kono
parents:
diff changeset
379 ::
kono
parents:
diff changeset
380
kono
parents:
diff changeset
381 spec of Server
kono
parents:
diff changeset
382 body of Server
kono
parents:
diff changeset
383 spec of Client
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385 because the spec of ``Server`` must be elaborated prior to ``Client`` by
kono
parents:
diff changeset
386 virtue of the |with| clause, and in addition the body of ``Server`` must be
kono
parents:
diff changeset
387 elaborated immediately after the spec of ``Server``.
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 Removing pragma ``Elaborate_Body`` could result in the following incorrect
kono
parents:
diff changeset
390 elaboration order:
kono
parents:
diff changeset
391
kono
parents:
diff changeset
392 ::
kono
parents:
diff changeset
393
kono
parents:
diff changeset
394 spec of Server
kono
parents:
diff changeset
395 spec of Client
kono
parents:
diff changeset
396 body of Server
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 where ``Client`` invokes ``Server.Func``, but the body of ``Server.Func`` has
kono
parents:
diff changeset
399 not been elaborated yet.
kono
parents:
diff changeset
400
kono
parents:
diff changeset
401 The pragmas outlined above allow a server unit to guarantee safe elaboration
kono
parents:
diff changeset
402 use by client units. Thus it is a good rule to mark units as ``Pure`` or
kono
parents:
diff changeset
403 ``Preelaborate``, and if this is not possible, mark them as ``Elaborate_Body``.
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 There are however situations where ``Pure``, ``Preelaborate``, and
kono
parents:
diff changeset
406 ``Elaborate_Body`` are not applicable. Ada provides another set of pragmas for
kono
parents:
diff changeset
407 use by client units to help ensure the elaboration safety of server units they
kono
parents:
diff changeset
408 depend on.
kono
parents:
diff changeset
409
kono
parents:
diff changeset
410 .. index:: pragma Elaborate (Unit)
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 * *pragma Elaborate (Unit)*
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 Pragma ``Elaborate`` can be placed in the context clauses of a unit, after a
kono
parents:
diff changeset
415 |with| clause. It guarantees that both the spec and body of its argument will
kono
parents:
diff changeset
416 be elaborated prior to the unit with the pragma. Note that other unrelated
kono
parents:
diff changeset
417 units may be elaborated in between the spec and the body.
kono
parents:
diff changeset
418
kono
parents:
diff changeset
419 ::
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 package Server is
kono
parents:
diff changeset
422 function Func return Integer;
kono
parents:
diff changeset
423 end Server;
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 ::
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 package body Server is
kono
parents:
diff changeset
428 function Func return Integer is
kono
parents:
diff changeset
429 begin
kono
parents:
diff changeset
430 ...
kono
parents:
diff changeset
431 end Func;
kono
parents:
diff changeset
432 end Server;
kono
parents:
diff changeset
433
kono
parents:
diff changeset
434 ::
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 with Server;
kono
parents:
diff changeset
437 pragma Elaborate (Server);
kono
parents:
diff changeset
438 package Client is
kono
parents:
diff changeset
439 Val : constant Integer := Server.Func;
kono
parents:
diff changeset
440 end Client;
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 In the example above, pragma ``Elaborate`` guarantees the following
kono
parents:
diff changeset
443 elaboration order:
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 ::
kono
parents:
diff changeset
446
kono
parents:
diff changeset
447 spec of Server
kono
parents:
diff changeset
448 body of Server
kono
parents:
diff changeset
449 spec of Client
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 Removing pragma ``Elaborate`` could result in the following incorrect
kono
parents:
diff changeset
452 elaboration order:
kono
parents:
diff changeset
453
kono
parents:
diff changeset
454 ::
kono
parents:
diff changeset
455
kono
parents:
diff changeset
456 spec of Server
kono
parents:
diff changeset
457 spec of Client
kono
parents:
diff changeset
458 body of Server
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 where ``Client`` invokes ``Server.Func``, but the body of ``Server.Func``
kono
parents:
diff changeset
461 has not been elaborated yet.
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 .. index:: pragma Elaborate_All (Unit)
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 * *pragma Elaborate_All (Unit)*
kono
parents:
diff changeset
466
kono
parents:
diff changeset
467 Pragma ``Elaborate_All`` is placed in the context clauses of a unit, after
kono
parents:
diff changeset
468 a |with| clause. It guarantees that both the spec and body of its argument
kono
parents:
diff changeset
469 will be elaborated prior to the unit with the pragma, as well as all units
kono
parents:
diff changeset
470 |withed| by the spec and body of the argument, recursively. Note that other
kono
parents:
diff changeset
471 unrelated units may be elaborated in between the spec and the body.
kono
parents:
diff changeset
472
kono
parents:
diff changeset
473 ::
kono
parents:
diff changeset
474
kono
parents:
diff changeset
475 package Math is
kono
parents:
diff changeset
476 function Factorial (Val : Natural) return Natural;
kono
parents:
diff changeset
477 end Math;
kono
parents:
diff changeset
478
kono
parents:
diff changeset
479 ::
kono
parents:
diff changeset
480
kono
parents:
diff changeset
481 package body Math is
kono
parents:
diff changeset
482 function Factorial (Val : Natural) return Natural is
kono
parents:
diff changeset
483 begin
kono
parents:
diff changeset
484 ...;
kono
parents:
diff changeset
485 end Factorial;
kono
parents:
diff changeset
486 end Math;
kono
parents:
diff changeset
487
kono
parents:
diff changeset
488 ::
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490 package Computer is
kono
parents:
diff changeset
491 type Operation_Kind is (None, Op_Factorial);
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 function Compute
kono
parents:
diff changeset
494 (Val : Natural;
kono
parents:
diff changeset
495 Op : Operation_Kind) return Natural;
kono
parents:
diff changeset
496 end Computer;
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 ::
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 with Math;
kono
parents:
diff changeset
501 package body Computer is
kono
parents:
diff changeset
502 function Compute
kono
parents:
diff changeset
503 (Val : Natural;
kono
parents:
diff changeset
504 Op : Operation_Kind) return Natural
kono
parents:
diff changeset
505 is
kono
parents:
diff changeset
506 if Op = Op_Factorial then
kono
parents:
diff changeset
507 return Math.Factorial (Val);
kono
parents:
diff changeset
508 end if;
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510 return 0;
kono
parents:
diff changeset
511 end Compute;
kono
parents:
diff changeset
512 end Computer;
kono
parents:
diff changeset
513
kono
parents:
diff changeset
514 ::
kono
parents:
diff changeset
515
kono
parents:
diff changeset
516 with Computer;
kono
parents:
diff changeset
517 pragma Elaborate_All (Computer);
kono
parents:
diff changeset
518 package Client is
kono
parents:
diff changeset
519 Val : constant Natural :=
kono
parents:
diff changeset
520 Computer.Compute (123, Computer.Op_Factorial);
kono
parents:
diff changeset
521 end Client;
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 In the example above, pragma ``Elaborate_All`` can result in the following
kono
parents:
diff changeset
524 elaboration order:
kono
parents:
diff changeset
525
kono
parents:
diff changeset
526 ::
kono
parents:
diff changeset
527
kono
parents:
diff changeset
528 spec of Math
kono
parents:
diff changeset
529 body of Math
kono
parents:
diff changeset
530 spec of Computer
kono
parents:
diff changeset
531 body of Computer
kono
parents:
diff changeset
532 spec of Client
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 Note that there are several allowable suborders for the specs and bodies of
kono
parents:
diff changeset
535 ``Math`` and ``Computer``, but the point is that these specs and bodies will
kono
parents:
diff changeset
536 be elaborated prior to ``Client``.
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 Removing pragma ``Elaborate_All`` could result in the following incorrect
kono
parents:
diff changeset
539 elaboration order
kono
parents:
diff changeset
540
kono
parents:
diff changeset
541 ::
kono
parents:
diff changeset
542
kono
parents:
diff changeset
543 spec of Math
kono
parents:
diff changeset
544 spec of Computer
kono
parents:
diff changeset
545 body of Computer
kono
parents:
diff changeset
546 spec of Client
kono
parents:
diff changeset
547 body of Math
kono
parents:
diff changeset
548
kono
parents:
diff changeset
549 where ``Client`` invokes ``Computer.Compute``, which in turn invokes
kono
parents:
diff changeset
550 ``Math.Factorial``, but the body of ``Math.Factorial`` has not been
kono
parents:
diff changeset
551 elaborated yet.
kono
parents:
diff changeset
552
kono
parents:
diff changeset
553 All pragmas shown above can be summarized by the following rule:
kono
parents:
diff changeset
554
kono
parents:
diff changeset
555 *If a client unit elaborates a server target directly or indirectly, then if
kono
parents:
diff changeset
556 the server unit requires a body and does not have pragma Pure, Preelaborate,
kono
parents:
diff changeset
557 or Elaborate_Body, then the client unit should have pragma Elaborate or
kono
parents:
diff changeset
558 Elaborate_All for the server unit.*
kono
parents:
diff changeset
559
kono
parents:
diff changeset
560 If the rule outlined above is not followed, then a program may fall in one of
kono
parents:
diff changeset
561 the following states:
kono
parents:
diff changeset
562
kono
parents:
diff changeset
563 * *No elaboration order exists*
kono
parents:
diff changeset
564
kono
parents:
diff changeset
565 In this case a compiler must diagnose the situation, and refuse to build an
kono
parents:
diff changeset
566 executable program.
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 * *One or more incorrect elaboration orders exist*
kono
parents:
diff changeset
569
kono
parents:
diff changeset
570 In this case a compiler can build an executable program, but
kono
parents:
diff changeset
571 ``Program_Error`` will be raised when the program is run.
kono
parents:
diff changeset
572
kono
parents:
diff changeset
573 * *Several elaboration orders exist, some correct, some incorrect*
kono
parents:
diff changeset
574
kono
parents:
diff changeset
575 In this case the programmer has not controlled the elaboration order. As a
kono
parents:
diff changeset
576 result, a compiler may or may not pick one of the correct orders, and the
kono
parents:
diff changeset
577 program may or may not raise ``Program_Error`` when it is run. This is the
kono
parents:
diff changeset
578 worst possible state because the program may fail on another compiler, or
kono
parents:
diff changeset
579 even another version of the same compiler.
kono
parents:
diff changeset
580
kono
parents:
diff changeset
581 * *One or more correct orders exist*
kono
parents:
diff changeset
582
kono
parents:
diff changeset
583 In this case a compiler can build an executable program, and the program is
kono
parents:
diff changeset
584 run successfully. This state may be guaranteed by following the outlined
kono
parents:
diff changeset
585 rules, or may be the result of good program architecture.
kono
parents:
diff changeset
586
kono
parents:
diff changeset
587 Note that one additional advantage of using ``Elaborate`` and ``Elaborate_All``
kono
parents:
diff changeset
588 is that the program continues to stay in the last state (one or more correct
kono
parents:
diff changeset
589 orders exist) even if maintenance changes the bodies of targets.
kono
parents:
diff changeset
590
kono
parents:
diff changeset
591 .. _Controlling_the_Elaboration_Order_in_GNAT:
kono
parents:
diff changeset
592
kono
parents:
diff changeset
593 Controlling the Elaboration Order in GNAT
kono
parents:
diff changeset
594 =========================================
kono
parents:
diff changeset
595
kono
parents:
diff changeset
596 In addition to Ada semantics and rules synthesized from them, GNAT offers
kono
parents:
diff changeset
597 three elaboration models to aid the programmer with specifying the correct
kono
parents:
diff changeset
598 elaboration order and to diagnose elaboration problems.
kono
parents:
diff changeset
599
kono
parents:
diff changeset
600 .. index:: Dynamic elaboration model
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 * *Dynamic elaboration model*
kono
parents:
diff changeset
603
kono
parents:
diff changeset
604 This is the most permissive of the three elaboration models. When the
kono
parents:
diff changeset
605 dynamic model is in effect, GNAT assumes that all code within all units in
kono
parents:
diff changeset
606 a partition is elaboration code. GNAT performs very few diagnostics and
kono
parents:
diff changeset
607 generates run-time checks to verify the elaboration order of a program. This
kono
parents:
diff changeset
608 behavior is identical to that specified by the Ada Reference Manual. The
kono
parents:
diff changeset
609 dynamic model is enabled with compiler switch :switch:`-gnatE`.
kono
parents:
diff changeset
610
kono
parents:
diff changeset
611 .. index:: Static elaboration model
kono
parents:
diff changeset
612
kono
parents:
diff changeset
613 * *Static elaboration model*
kono
parents:
diff changeset
614
kono
parents:
diff changeset
615 This is the middle ground of the three models. When the static model is in
kono
parents:
diff changeset
616 effect, GNAT performs extensive diagnostics on a unit-by-unit basis for all
kono
parents:
diff changeset
617 scenarios that elaborate or execute internal targets. GNAT also generates
kono
parents:
diff changeset
618 run-time checks for all external targets and for all scenarios that may
kono
parents:
diff changeset
619 exhibit ABE problems. Finally, GNAT installs implicit ``Elaborate`` and
kono
parents:
diff changeset
620 ``Elaborate_All`` pragmas for server units based on the dependencies of
kono
parents:
diff changeset
621 client units. The static model is the default model in GNAT.
kono
parents:
diff changeset
622
kono
parents:
diff changeset
623 .. index:: SPARK elaboration model
kono
parents:
diff changeset
624
kono
parents:
diff changeset
625 * *SPARK elaboration model*
kono
parents:
diff changeset
626
kono
parents:
diff changeset
627 This is the most conservative of the three models and enforces the SPARK
kono
parents:
diff changeset
628 rules of elaboration as defined in the SPARK Reference Manual, section 7.7.
kono
parents:
diff changeset
629 The SPARK model is in effect only when a scenario and a target reside in a
kono
parents:
diff changeset
630 region subject to SPARK_Mode On, otherwise the dynamic or static model is in
kono
parents:
diff changeset
631 effect.
kono
parents:
diff changeset
632
kono
parents:
diff changeset
633 .. _Common_Elaboration_Model_Traits":
kono
parents:
diff changeset
634
kono
parents:
diff changeset
635 Common Elaboration-model Traits
kono
parents:
diff changeset
636 ===============================
kono
parents:
diff changeset
637
kono
parents:
diff changeset
638 All three GNAT models are able to detect elaboration problems related to
kono
parents:
diff changeset
639 dispatching calls and a particular kind of ABE referred to as *guaranteed ABE*.
kono
parents:
diff changeset
640
kono
parents:
diff changeset
641 * *Dispatching calls*
kono
parents:
diff changeset
642
kono
parents:
diff changeset
643 GNAT installs run-time checks for each primitive subprogram of each tagged
kono
parents:
diff changeset
644 type defined in a partition on the assumption that a dispatching call
kono
parents:
diff changeset
645 invoked at elaboration time will execute one of these primitives. As a
kono
parents:
diff changeset
646 result, a dispatching call that executes a primitive whose body has not
kono
parents:
diff changeset
647 been elaborated yet will raise exception ``Program_Error`` at run time. The
kono
parents:
diff changeset
648 checks can be suppressed using pragma ``Suppress (Elaboration_Check)``.
kono
parents:
diff changeset
649
kono
parents:
diff changeset
650 * *Guaranteed ABE*
kono
parents:
diff changeset
651
kono
parents:
diff changeset
652 A guaranteed ABE arises when the body of a target is not elaborated early
kono
parents:
diff changeset
653 enough, and causes all scenarios that directly execute the target to fail.
kono
parents:
diff changeset
654
kono
parents:
diff changeset
655 ::
kono
parents:
diff changeset
656
kono
parents:
diff changeset
657 package body Guaranteed_ABE is
kono
parents:
diff changeset
658 function ABE return Integer;
kono
parents:
diff changeset
659
kono
parents:
diff changeset
660 Val : constant Integer := ABE;
kono
parents:
diff changeset
661
kono
parents:
diff changeset
662 function ABE return Integer is
kono
parents:
diff changeset
663 begin
kono
parents:
diff changeset
664 ...
kono
parents:
diff changeset
665 end ABE;
kono
parents:
diff changeset
666 end Guaranteed_ABE;
kono
parents:
diff changeset
667
kono
parents:
diff changeset
668 In the example above, the elaboration of ``Guaranteed_ABE``'s body elaborates
kono
parents:
diff changeset
669 the declaration of ``Val``. This invokes function ``ABE``, however the body
kono
parents:
diff changeset
670 of ``ABE`` has not been elaborated yet. GNAT emits similar diagnostics in all
kono
parents:
diff changeset
671 three models:
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 ::
kono
parents:
diff changeset
674
kono
parents:
diff changeset
675 1. package body Guaranteed_ABE is
kono
parents:
diff changeset
676 2. function ABE return Integer;
kono
parents:
diff changeset
677 3.
kono
parents:
diff changeset
678 4. Val : constant Integer := ABE;
kono
parents:
diff changeset
679 |
kono
parents:
diff changeset
680 >>> warning: cannot call "ABE" before body seen
kono
parents:
diff changeset
681 >>> warning: Program_Error will be raised at run time
kono
parents:
diff changeset
682
kono
parents:
diff changeset
683 5.
kono
parents:
diff changeset
684 6. function ABE return Integer is
kono
parents:
diff changeset
685 7. begin
kono
parents:
diff changeset
686 8. ...
kono
parents:
diff changeset
687 9. end ABE;
kono
parents:
diff changeset
688 10. end Guaranteed_ABE;
kono
parents:
diff changeset
689
kono
parents:
diff changeset
690 Note that GNAT emits warnings rather than hard errors whenever it encounters an
kono
parents:
diff changeset
691 elaboration problem. This is because the elaboration model in effect may be too
kono
parents:
diff changeset
692 conservative, or a particular scenario may not be elaborated or executed due to
kono
parents:
diff changeset
693 data and control flow. The warnings can be suppressed with compiler switch
kono
parents:
diff changeset
694 :switch:`-gnatws`.
kono
parents:
diff changeset
695
kono
parents:
diff changeset
696 .. _Dynamic_Elaboration_Model_in_GNAT:
kono
parents:
diff changeset
697
kono
parents:
diff changeset
698 Dynamic Elaboration Model in GNAT
kono
parents:
diff changeset
699 =================================
kono
parents:
diff changeset
700
kono
parents:
diff changeset
701 The dynamic model assumes that all code within all units in a partition is
kono
parents:
diff changeset
702 elaboration code. As a result, run-time checks are installed for each scenario
kono
parents:
diff changeset
703 regardless of whether the target is internal or external. The checks can be
kono
parents:
diff changeset
704 suppressed using pragma ``Suppress (Elaboration_Check)``. This behavior is
kono
parents:
diff changeset
705 identical to that specified by the Ada Reference Manual. The following example
kono
parents:
diff changeset
706 showcases run-time checks installed by GNAT to verify the elaboration state of
kono
parents:
diff changeset
707 package ``Dynamic_Model``.
kono
parents:
diff changeset
708
kono
parents:
diff changeset
709 ::
kono
parents:
diff changeset
710
kono
parents:
diff changeset
711 with Server;
kono
parents:
diff changeset
712 package body Dynamic_Model is
kono
parents:
diff changeset
713 procedure API is
kono
parents:
diff changeset
714 begin
kono
parents:
diff changeset
715 ...
kono
parents:
diff changeset
716 end API;
kono
parents:
diff changeset
717
kono
parents:
diff changeset
718 <check that the body of Server.Gen is elaborated>
kono
parents:
diff changeset
719 package Inst is new Server.Gen;
kono
parents:
diff changeset
720
kono
parents:
diff changeset
721 T : Server.Task_Type;
kono
parents:
diff changeset
722
kono
parents:
diff changeset
723 begin
kono
parents:
diff changeset
724 <check that the body of Server.Task_Type is elaborated>
kono
parents:
diff changeset
725
kono
parents:
diff changeset
726 <check that the body of Server.Proc is elaborated>
kono
parents:
diff changeset
727 Server.Proc;
kono
parents:
diff changeset
728 end Dynamic_Model;
kono
parents:
diff changeset
729
kono
parents:
diff changeset
730 The checks verify that the body of a target has been successfully elaborated
kono
parents:
diff changeset
731 before a scenario activates, calls, or instantiates a target.
kono
parents:
diff changeset
732
kono
parents:
diff changeset
733 Note that no scenario within package ``Dynamic_Model`` calls procedure ``API``.
kono
parents:
diff changeset
734 In fact, procedure ``API`` may not be invoked by elaboration code within the
kono
parents:
diff changeset
735 partition, however the dynamic model assumes that this can happen.
kono
parents:
diff changeset
736
kono
parents:
diff changeset
737 The dynamic model emits very few diagnostics, but can make suggestions on
kono
parents:
diff changeset
738 missing ``Elaborate`` and ``Elaborate_All`` pragmas for library-level
kono
parents:
diff changeset
739 scenarios. This information is available when compiler switch :switch:`-gnatel`
kono
parents:
diff changeset
740 is in effect.
kono
parents:
diff changeset
741
kono
parents:
diff changeset
742 ::
kono
parents:
diff changeset
743
kono
parents:
diff changeset
744 1. with Server;
kono
parents:
diff changeset
745 2. package body Dynamic_Model is
kono
parents:
diff changeset
746 3. Val : constant Integer := Server.Func;
kono
parents:
diff changeset
747 |
kono
parents:
diff changeset
748 >>> info: call to "Func" during elaboration
kono
parents:
diff changeset
749 >>> info: missing pragma "Elaborate_All" for unit "Server"
kono
parents:
diff changeset
750
kono
parents:
diff changeset
751 4. end Dynamic_Model;
kono
parents:
diff changeset
752
kono
parents:
diff changeset
753 .. _Static_Elaboration_Model_in_GNAT:
kono
parents:
diff changeset
754
kono
parents:
diff changeset
755 Static Elaboration Model in GNAT
kono
parents:
diff changeset
756 ================================
kono
parents:
diff changeset
757
kono
parents:
diff changeset
758 In contrast to the dynamic model, the static model is more precise in its
kono
parents:
diff changeset
759 analysis of elaboration code. The model makes a clear distinction between
kono
parents:
diff changeset
760 internal and external targets, and resorts to different diagnostics and
kono
parents:
diff changeset
761 run-time checks based on the nature of the target.
kono
parents:
diff changeset
762
kono
parents:
diff changeset
763 * *Internal targets*
kono
parents:
diff changeset
764
kono
parents:
diff changeset
765 The static model performs extensive diagnostics on scenarios which elaborate
kono
parents:
diff changeset
766 or execute internal targets. The warnings resulting from these diagnostics
kono
parents:
diff changeset
767 are enabled by default, but can be suppressed using compiler switch
kono
parents:
diff changeset
768 :switch:`-gnatws`.
kono
parents:
diff changeset
769
kono
parents:
diff changeset
770 ::
kono
parents:
diff changeset
771
kono
parents:
diff changeset
772 1. package body Static_Model is
kono
parents:
diff changeset
773 2. generic
kono
parents:
diff changeset
774 3. with function Func return Integer;
kono
parents:
diff changeset
775 4. package Gen is
kono
parents:
diff changeset
776 5. Val : constant Integer := Func;
kono
parents:
diff changeset
777 6. end Gen;
kono
parents:
diff changeset
778 7.
kono
parents:
diff changeset
779 8. function ABE return Integer;
kono
parents:
diff changeset
780 9.
kono
parents:
diff changeset
781 10. function Cause_ABE return Boolean is
kono
parents:
diff changeset
782 11. package Inst is new Gen (ABE);
kono
parents:
diff changeset
783 |
kono
parents:
diff changeset
784 >>> warning: in instantiation at line 5
kono
parents:
diff changeset
785 >>> warning: cannot call "ABE" before body seen
kono
parents:
diff changeset
786 >>> warning: Program_Error may be raised at run time
kono
parents:
diff changeset
787 >>> warning: body of unit "Static_Model" elaborated
kono
parents:
diff changeset
788 >>> warning: function "Cause_ABE" called at line 16
kono
parents:
diff changeset
789 >>> warning: function "ABE" called at line 5, instance at line 11
kono
parents:
diff changeset
790
kono
parents:
diff changeset
791 12. begin
kono
parents:
diff changeset
792 13. ...
kono
parents:
diff changeset
793 14. end Cause_ABE;
kono
parents:
diff changeset
794 15.
kono
parents:
diff changeset
795 16. Val : constant Boolean := Cause_ABE;
kono
parents:
diff changeset
796 17.
kono
parents:
diff changeset
797 18. function ABE return Integer is
kono
parents:
diff changeset
798 19. begin
kono
parents:
diff changeset
799 20. ...
kono
parents:
diff changeset
800 21. end ABE;
kono
parents:
diff changeset
801 22. end Static_Model;
kono
parents:
diff changeset
802
kono
parents:
diff changeset
803 The example above illustrates an ABE problem within package ``Static_Model``,
kono
parents:
diff changeset
804 which is hidden by several layers of indirection. The elaboration of package
kono
parents:
diff changeset
805 body ``Static_Model`` elaborates the declaration of ``Val``. This invokes
kono
parents:
diff changeset
806 function ``Cause_ABE``, which instantiates generic unit ``Gen`` as ``Inst``.
kono
parents:
diff changeset
807 The elaboration of ``Inst`` invokes function ``ABE``, however the body of
kono
parents:
diff changeset
808 ``ABE`` has not been elaborated yet.
kono
parents:
diff changeset
809
kono
parents:
diff changeset
810 * *External targets*
kono
parents:
diff changeset
811
kono
parents:
diff changeset
812 The static model installs run-time checks to verify the elaboration status
kono
parents:
diff changeset
813 of server targets only when the scenario that elaborates or executes that
kono
parents:
diff changeset
814 target is part of the elaboration code of the client unit. The checks can be
kono
parents:
diff changeset
815 suppressed using pragma ``Suppress (Elaboration_Check)``.
kono
parents:
diff changeset
816
kono
parents:
diff changeset
817 ::
kono
parents:
diff changeset
818
kono
parents:
diff changeset
819 with Server;
kono
parents:
diff changeset
820 package body Static_Model is
kono
parents:
diff changeset
821 generic
kono
parents:
diff changeset
822 with function Func return Integer;
kono
parents:
diff changeset
823 package Gen is
kono
parents:
diff changeset
824 Val : constant Integer := Func;
kono
parents:
diff changeset
825 end Gen;
kono
parents:
diff changeset
826
kono
parents:
diff changeset
827 function Call_Func return Boolean is
kono
parents:
diff changeset
828 <check that the body of Server.Func is elaborated>
kono
parents:
diff changeset
829 package Inst is new Gen (Server.Func);
kono
parents:
diff changeset
830 begin
kono
parents:
diff changeset
831 ...
kono
parents:
diff changeset
832 end Call_Func;
kono
parents:
diff changeset
833
kono
parents:
diff changeset
834 Val : constant Boolean := Call_Func;
kono
parents:
diff changeset
835 end Static_Model;
kono
parents:
diff changeset
836
kono
parents:
diff changeset
837 In the example above, the elaboration of package body ``Static_Model``
kono
parents:
diff changeset
838 elaborates the declaration of ``Val``. This invokes function ``Call_Func``,
kono
parents:
diff changeset
839 which instantiates generic unit ``Gen`` as ``Inst``. The elaboration of
kono
parents:
diff changeset
840 ``Inst`` invokes function ``Server.Func``. Since ``Server.Func`` is an
kono
parents:
diff changeset
841 external target, GNAT installs a run-time check to verify that its body has
kono
parents:
diff changeset
842 been elaborated.
kono
parents:
diff changeset
843
kono
parents:
diff changeset
844 In addition to checks, the static model installs implicit ``Elaborate`` and
kono
parents:
diff changeset
845 ``Elaborate_All`` pragmas to guarantee safe elaboration use of server units.
kono
parents:
diff changeset
846 This information is available when compiler switch :switch:`-gnatel` is in
kono
parents:
diff changeset
847 effect.
kono
parents:
diff changeset
848
kono
parents:
diff changeset
849 ::
kono
parents:
diff changeset
850
kono
parents:
diff changeset
851 1. with Server;
kono
parents:
diff changeset
852 2. package body Static_Model is
kono
parents:
diff changeset
853 3. generic
kono
parents:
diff changeset
854 4. with function Func return Integer;
kono
parents:
diff changeset
855 5. package Gen is
kono
parents:
diff changeset
856 6. Val : constant Integer := Func;
kono
parents:
diff changeset
857 7. end Gen;
kono
parents:
diff changeset
858 8.
kono
parents:
diff changeset
859 9. function Call_Func return Boolean is
kono
parents:
diff changeset
860 10. package Inst is new Gen (Server.Func);
kono
parents:
diff changeset
861 |
kono
parents:
diff changeset
862 >>> info: instantiation of "Gen" during elaboration
kono
parents:
diff changeset
863 >>> info: in instantiation at line 6
kono
parents:
diff changeset
864 >>> info: call to "Func" during elaboration
kono
parents:
diff changeset
865 >>> info: in instantiation at line 6
kono
parents:
diff changeset
866 >>> info: implicit pragma "Elaborate_All" generated for unit "Server"
kono
parents:
diff changeset
867 >>> info: body of unit "Static_Model" elaborated
kono
parents:
diff changeset
868 >>> info: function "Call_Func" called at line 15
kono
parents:
diff changeset
869 >>> info: function "Func" called at line 6, instance at line 10
kono
parents:
diff changeset
870
kono
parents:
diff changeset
871 11. begin
kono
parents:
diff changeset
872 12. ...
kono
parents:
diff changeset
873 13. end Call_Func;
kono
parents:
diff changeset
874 14.
kono
parents:
diff changeset
875 15. Val : constant Boolean := Call_Func;
kono
parents:
diff changeset
876 |
kono
parents:
diff changeset
877 >>> info: call to "Call_Func" during elaboration
kono
parents:
diff changeset
878
kono
parents:
diff changeset
879 16. end Static_Model;
kono
parents:
diff changeset
880
kono
parents:
diff changeset
881 In the example above, the elaboration of package body ``Static_Model``
kono
parents:
diff changeset
882 elaborates the declaration of ``Val``. This invokes function ``Call_Func``,
kono
parents:
diff changeset
883 which instantiates generic unit ``Gen`` as ``Inst``. The elaboration of
kono
parents:
diff changeset
884 ``Inst`` invokes function ``Server.Func``. Since ``Server.Func`` is an
kono
parents:
diff changeset
885 external target, GNAT installs an implicit ``Elaborate_All`` pragma for unit
kono
parents:
diff changeset
886 ``Server``. The pragma guarantees that both the spec and body of ``Server``,
kono
parents:
diff changeset
887 along with any additional dependencies that ``Server`` may require, are
kono
parents:
diff changeset
888 elaborated prior to the body of ``Static_Model``.
kono
parents:
diff changeset
889
kono
parents:
diff changeset
890 .. _SPARK_Elaboration_Model_in_GNAT:
kono
parents:
diff changeset
891
kono
parents:
diff changeset
892 SPARK Elaboration Model in GNAT
kono
parents:
diff changeset
893 ===============================
kono
parents:
diff changeset
894
kono
parents:
diff changeset
895 The SPARK model is identical to the static model in its handling of internal
kono
parents:
diff changeset
896 targets. The SPARK model, however, requires explicit ``Elaborate`` or
kono
parents:
diff changeset
897 ``Elaborate_All`` pragmas to be present in the program when a target is
kono
parents:
diff changeset
898 external, and compiler switch :switch:`-gnatd.v` is in effect.
kono
parents:
diff changeset
899
kono
parents:
diff changeset
900 ::
kono
parents:
diff changeset
901
kono
parents:
diff changeset
902 1. with Server;
kono
parents:
diff changeset
903 2. package body SPARK_Model with SPARK_Mode is
kono
parents:
diff changeset
904 3. Val : constant Integer := Server.Func;
kono
parents:
diff changeset
905 |
kono
parents:
diff changeset
906 >>> call to "Func" during elaboration in SPARK
kono
parents:
diff changeset
907 >>> unit "SPARK_Model" requires pragma "Elaborate_All" for "Server"
kono
parents:
diff changeset
908 >>> body of unit "SPARK_Model" elaborated
kono
parents:
diff changeset
909 >>> function "Func" called at line 3
kono
parents:
diff changeset
910
kono
parents:
diff changeset
911 4. end SPARK_Model;
kono
parents:
diff changeset
912
kono
parents:
diff changeset
913 .. _Mixing_Elaboration_Models:
kono
parents:
diff changeset
914
kono
parents:
diff changeset
915 Mixing Elaboration Models
kono
parents:
diff changeset
916 =========================
kono
parents:
diff changeset
917
kono
parents:
diff changeset
918 It is possible to mix units compiled with a different elaboration model,
kono
parents:
diff changeset
919 however the following rules must be observed:
kono
parents:
diff changeset
920
kono
parents:
diff changeset
921 * A client unit compiled with the dynamic model can only |with| a server unit
kono
parents:
diff changeset
922 that meets at least one of the following criteria:
kono
parents:
diff changeset
923
kono
parents:
diff changeset
924 - The server unit is compiled with the dynamic model.
kono
parents:
diff changeset
925
kono
parents:
diff changeset
926 - The server unit is a GNAT implementation unit from the Ada, GNAT,
kono
parents:
diff changeset
927 Interfaces, or System hierarchies.
kono
parents:
diff changeset
928
kono
parents:
diff changeset
929 - The server unit has pragma ``Pure`` or ``Preelaborate``.
kono
parents:
diff changeset
930
kono
parents:
diff changeset
931 - The client unit has an explicit ``Elaborate_All`` pragma for the server
kono
parents:
diff changeset
932 unit.
kono
parents:
diff changeset
933
kono
parents:
diff changeset
934 These rules ensure that elaboration checks are not omitted. If the rules are
kono
parents:
diff changeset
935 violated, the binder emits a warning:
kono
parents:
diff changeset
936
kono
parents:
diff changeset
937 ::
kono
parents:
diff changeset
938
kono
parents:
diff changeset
939 warning: "x.ads" has dynamic elaboration checks and with's
kono
parents:
diff changeset
940 warning: "y.ads" which has static elaboration checks
kono
parents:
diff changeset
941
kono
parents:
diff changeset
942 The warnings can be suppressed by binder switch :switch:`-ws`.
kono
parents:
diff changeset
943
kono
parents:
diff changeset
944 .. _Elaboration_Circularities:
kono
parents:
diff changeset
945
kono
parents:
diff changeset
946 Elaboration Circularities
kono
parents:
diff changeset
947 =========================
kono
parents:
diff changeset
948
kono
parents:
diff changeset
949 If the binder cannot find an acceptable elaboration order, it outputs detailed
kono
parents:
diff changeset
950 diagnostics describing an **elaboration circularity**.
kono
parents:
diff changeset
951
kono
parents:
diff changeset
952 ::
kono
parents:
diff changeset
953
kono
parents:
diff changeset
954 package Server is
kono
parents:
diff changeset
955 function Func return Integer;
kono
parents:
diff changeset
956 end Server;
kono
parents:
diff changeset
957
kono
parents:
diff changeset
958 ::
kono
parents:
diff changeset
959
kono
parents:
diff changeset
960 with Client;
kono
parents:
diff changeset
961 package body Server is
kono
parents:
diff changeset
962 function Func return Integer is
kono
parents:
diff changeset
963 begin
kono
parents:
diff changeset
964 ...
kono
parents:
diff changeset
965 end Func;
kono
parents:
diff changeset
966 end Server;
kono
parents:
diff changeset
967
kono
parents:
diff changeset
968 ::
kono
parents:
diff changeset
969
kono
parents:
diff changeset
970 with Server;
kono
parents:
diff changeset
971 package Client is
kono
parents:
diff changeset
972 Val : constant Integer := Server.Func;
kono
parents:
diff changeset
973 end Client;
kono
parents:
diff changeset
974
kono
parents:
diff changeset
975 ::
kono
parents:
diff changeset
976
kono
parents:
diff changeset
977 with Client;
kono
parents:
diff changeset
978 procedure Main is begin null; end Main;
kono
parents:
diff changeset
979
kono
parents:
diff changeset
980 ::
kono
parents:
diff changeset
981
kono
parents:
diff changeset
982 error: elaboration circularity detected
kono
parents:
diff changeset
983 info: "server (body)" must be elaborated before "client (spec)"
kono
parents:
diff changeset
984 info: reason: implicit Elaborate_All in unit "client (spec)"
kono
parents:
diff changeset
985 info: recompile "client (spec)" with -gnatel for full details
kono
parents:
diff changeset
986 info: "server (body)"
kono
parents:
diff changeset
987 info: must be elaborated along with its spec:
kono
parents:
diff changeset
988 info: "server (spec)"
kono
parents:
diff changeset
989 info: which is withed by:
kono
parents:
diff changeset
990 info: "client (spec)"
kono
parents:
diff changeset
991 info: "client (spec)" must be elaborated before "server (body)"
kono
parents:
diff changeset
992 info: reason: with clause
kono
parents:
diff changeset
993
kono
parents:
diff changeset
994 In the example above, ``Client`` must be elaborated prior to ``Main`` by virtue
kono
parents:
diff changeset
995 of a |with| clause. The elaboration of ``Client`` invokes ``Server.Func``, and
kono
parents:
diff changeset
996 static model generates an implicit ``Elaborate_All`` pragma for ``Server``. The
kono
parents:
diff changeset
997 pragma implies that both the spec and body of ``Server``, along with any units
kono
parents:
diff changeset
998 they |with|, must be elaborated prior to ``Client``. However, ``Server``'s body
kono
parents:
diff changeset
999 |withs| ``Client``, implying that ``Client`` must be elaborated prior to
kono
parents:
diff changeset
1000 ``Server``. The end result is that ``Client`` must be elaborated prior to
kono
parents:
diff changeset
1001 ``Client``, and this leads to a circularity.
kono
parents:
diff changeset
1002
kono
parents:
diff changeset
1003 .. _Resolving_Elaboration_Circularities:
kono
parents:
diff changeset
1004
kono
parents:
diff changeset
1005 Resolving Elaboration Circularities
kono
parents:
diff changeset
1006 ===================================
kono
parents:
diff changeset
1007
kono
parents:
diff changeset
1008 When faced with an elaboration circularity, a programmer has several options
kono
parents:
diff changeset
1009 available.
kono
parents:
diff changeset
1010
kono
parents:
diff changeset
1011 * *Fix the program*
kono
parents:
diff changeset
1012
kono
parents:
diff changeset
1013 The most desirable option from the point of view of long-term maintenance
kono
parents:
diff changeset
1014 is to rearrange the program so that the elaboration problems are avoided.
kono
parents:
diff changeset
1015 One useful technique is to place the elaboration code into separate child
kono
parents:
diff changeset
1016 packages. Another is to move some of the initialization code to explicitly
kono
parents:
diff changeset
1017 invoked subprograms, where the program controls the order of initialization
kono
parents:
diff changeset
1018 explicitly. Although this is the most desirable option, it may be impractical
kono
parents:
diff changeset
1019 and involve too much modification, especially in the case of complex legacy
kono
parents:
diff changeset
1020 code.
kono
parents:
diff changeset
1021
kono
parents:
diff changeset
1022 * *Switch to more permissive elaboration model*
kono
parents:
diff changeset
1023
kono
parents:
diff changeset
1024 If the compilation was performed using the static model, enable the dynamic
kono
parents:
diff changeset
1025 model with compiler switch :switch:`-gnatE`. GNAT will no longer generate
kono
parents:
diff changeset
1026 implicit ``Elaborate`` and ``Elaborate_All`` pragmas, resulting in a behavior
kono
parents:
diff changeset
1027 identical to that specified by the Ada Reference Manual. The binder will
kono
parents:
diff changeset
1028 generate an executable program that may or may not raise ``Program_Error``,
kono
parents:
diff changeset
1029 and it is the programmer's responsibility to ensure that it does not raise
kono
parents:
diff changeset
1030 ``Program_Error``.
kono
parents:
diff changeset
1031
kono
parents:
diff changeset
1032 * *Suppress all elaboration checks*
kono
parents:
diff changeset
1033
kono
parents:
diff changeset
1034 The drawback of run-time checks is that they generate overhead at run time,
kono
parents:
diff changeset
1035 both in space and time. If the programmer is absolutely sure that a program
kono
parents:
diff changeset
1036 will not raise an elaboration-related ``Program_Error``, then using the
kono
parents:
diff changeset
1037 pragma ``Suppress (Elaboration_Check)`` globally (as a configuration pragma)
kono
parents:
diff changeset
1038 will eliminate all run-time checks.
kono
parents:
diff changeset
1039
kono
parents:
diff changeset
1040 * *Suppress elaboration checks selectively*
kono
parents:
diff changeset
1041
kono
parents:
diff changeset
1042 If a scenario cannot possibly lead to an elaboration ``Program_Error``,
kono
parents:
diff changeset
1043 and the binder nevertheless complains about implicit ``Elaborate`` and
kono
parents:
diff changeset
1044 ``Elaborate_All`` pragmas that lead to elaboration circularities, it
kono
parents:
diff changeset
1045 is possible to suppress the generation of implicit ``Elaborate`` and
kono
parents:
diff changeset
1046 ``Elaborate_All`` pragmas, as well as run-time checks. Clearly this can
kono
parents:
diff changeset
1047 be unsafe, and it is the responsibility of the programmer to make sure
kono
parents:
diff changeset
1048 that the resulting program has no elaboration anomalies. Pragma
kono
parents:
diff changeset
1049 ``Suppress (Elaboration_Check)`` can be used with different levels of
kono
parents:
diff changeset
1050 granularity to achieve these effects.
kono
parents:
diff changeset
1051
kono
parents:
diff changeset
1052 - *Target suppression*
kono
parents:
diff changeset
1053
kono
parents:
diff changeset
1054 When the pragma is placed in a declarative part, without a second argument
kono
parents:
diff changeset
1055 naming an entity, it will suppress implicit ``Elaborate`` and
kono
parents:
diff changeset
1056 ``Elaborate_All`` pragma generation, as well as run-time checks, on all
kono
parents:
diff changeset
1057 targets within the region.
kono
parents:
diff changeset
1058
kono
parents:
diff changeset
1059 ::
kono
parents:
diff changeset
1060
kono
parents:
diff changeset
1061 package Range_Suppress is
kono
parents:
diff changeset
1062 pragma Suppress (Elaboration_Check);
kono
parents:
diff changeset
1063
kono
parents:
diff changeset
1064 function Func return Integer;
kono
parents:
diff changeset
1065
kono
parents:
diff changeset
1066 generic
kono
parents:
diff changeset
1067 procedure Gen;
kono
parents:
diff changeset
1068
kono
parents:
diff changeset
1069 pragma Unsuppress (Elaboration_Check);
kono
parents:
diff changeset
1070
kono
parents:
diff changeset
1071 task type Tsk;
kono
parents:
diff changeset
1072 end Range_Suppress;
kono
parents:
diff changeset
1073
kono
parents:
diff changeset
1074 In the example above, a pair of Suppress/Unsuppress pragmas define a region
kono
parents:
diff changeset
1075 of suppression within package ``Range_Suppress``. As a result, no implicit
kono
parents:
diff changeset
1076 ``Elaborate`` and ``Elaborate_All`` pragmas, nor any run-time checks, will
kono
parents:
diff changeset
1077 be generated by callers of ``Func`` and instantiators of ``Gen``. Note that
kono
parents:
diff changeset
1078 task type ``Tsk`` is not within this region.
kono
parents:
diff changeset
1079
kono
parents:
diff changeset
1080 An alternative to the region-based suppression is to use multiple
kono
parents:
diff changeset
1081 ``Suppress`` pragmas with arguments naming specific entities for which
kono
parents:
diff changeset
1082 elaboration checks should be suppressed:
kono
parents:
diff changeset
1083
kono
parents:
diff changeset
1084 ::
kono
parents:
diff changeset
1085
kono
parents:
diff changeset
1086 package Range_Suppress is
kono
parents:
diff changeset
1087 function Func return Integer;
kono
parents:
diff changeset
1088 pragma Suppress (Elaboration_Check, Func);
kono
parents:
diff changeset
1089
kono
parents:
diff changeset
1090 generic
kono
parents:
diff changeset
1091 procedure Gen;
kono
parents:
diff changeset
1092 pragma Suppress (Elaboration_Check, Gen);
kono
parents:
diff changeset
1093
kono
parents:
diff changeset
1094 task type Tsk;
kono
parents:
diff changeset
1095 end Range_Suppress;
kono
parents:
diff changeset
1096
kono
parents:
diff changeset
1097 - *Scenario suppression*
kono
parents:
diff changeset
1098
kono
parents:
diff changeset
1099 When the pragma ``Suppress`` is placed in a declarative or statement
kono
parents:
diff changeset
1100 part, without an entity argument, it will suppress implicit ``Elaborate``
kono
parents:
diff changeset
1101 and ``Elaborate_All`` pragma generation, as well as run-time checks, on
kono
parents:
diff changeset
1102 all scenarios within the region.
kono
parents:
diff changeset
1103
kono
parents:
diff changeset
1104 ::
kono
parents:
diff changeset
1105
kono
parents:
diff changeset
1106 with Server;
kono
parents:
diff changeset
1107 package body Range_Suppress is
kono
parents:
diff changeset
1108 pragma Suppress (Elaboration_Check);
kono
parents:
diff changeset
1109
kono
parents:
diff changeset
1110 function Func return Integer is
kono
parents:
diff changeset
1111 begin
kono
parents:
diff changeset
1112 return Server.Func;
kono
parents:
diff changeset
1113 end Func;
kono
parents:
diff changeset
1114
kono
parents:
diff changeset
1115 procedure Gen is
kono
parents:
diff changeset
1116 begin
kono
parents:
diff changeset
1117 Server.Proc;
kono
parents:
diff changeset
1118 end Gen;
kono
parents:
diff changeset
1119
kono
parents:
diff changeset
1120 pragma Unsuppress (Elaboration_Check);
kono
parents:
diff changeset
1121
kono
parents:
diff changeset
1122 task body Tsk is
kono
parents:
diff changeset
1123 begin
kono
parents:
diff changeset
1124 Server.Proc;
kono
parents:
diff changeset
1125 end Tsk;
kono
parents:
diff changeset
1126 end Range_Suppress;
kono
parents:
diff changeset
1127
kono
parents:
diff changeset
1128 In the example above, a pair of Suppress/Unsuppress pragmas define a region
kono
parents:
diff changeset
1129 of suppression within package body ``Range_Suppress``. As a result, the
kono
parents:
diff changeset
1130 calls to ``Server.Func`` in ``Func`` and ``Server.Proc`` in ``Gen`` will
kono
parents:
diff changeset
1131 not generate any implicit ``Elaborate`` and ``Elaborate_All`` pragmas or
kono
parents:
diff changeset
1132 run-time checks.
kono
parents:
diff changeset
1133
kono
parents:
diff changeset
1134 .. _Resolving_Task_Issues:
kono
parents:
diff changeset
1135
kono
parents:
diff changeset
1136 Resolving Task Issues
kono
parents:
diff changeset
1137 =====================
kono
parents:
diff changeset
1138
kono
parents:
diff changeset
1139 The model of execution in Ada dictates that elaboration must first take place,
kono
parents:
diff changeset
1140 and only then can the main program be started. Tasks which are activated during
kono
parents:
diff changeset
1141 elaboration violate this model and may lead to serious concurrent problems at
kono
parents:
diff changeset
1142 elaboration time.
kono
parents:
diff changeset
1143
kono
parents:
diff changeset
1144 A task can be activated in two different ways:
kono
parents:
diff changeset
1145
kono
parents:
diff changeset
1146 * The task is created by an allocator in which case it is activated immediately
kono
parents:
diff changeset
1147 after the allocator is evaluated.
kono
parents:
diff changeset
1148
kono
parents:
diff changeset
1149 * The task is declared at the library level or within some nested master in
kono
parents:
diff changeset
1150 which case it is activated before starting execution of the statement
kono
parents:
diff changeset
1151 sequence of the master defining the task.
kono
parents:
diff changeset
1152
kono
parents:
diff changeset
1153 Since the elaboration of a partition is performed by the environment task
kono
parents:
diff changeset
1154 servicing that partition, any tasks activated during elaboration may be in
kono
parents:
diff changeset
1155 a race with the environment task, and lead to unpredictable state and behavior.
kono
parents:
diff changeset
1156 The static model seeks to avoid such interactions by assuming that all code in
kono
parents:
diff changeset
1157 the task body is executed at elaboration time, if the task was activated by
kono
parents:
diff changeset
1158 elaboration code.
kono
parents:
diff changeset
1159
kono
parents:
diff changeset
1160 ::
kono
parents:
diff changeset
1161
kono
parents:
diff changeset
1162 package Decls is
kono
parents:
diff changeset
1163 task Lib_Task is
kono
parents:
diff changeset
1164 entry Start;
kono
parents:
diff changeset
1165 end Lib_Task;
kono
parents:
diff changeset
1166
kono
parents:
diff changeset
1167 type My_Int is new Integer;
kono
parents:
diff changeset
1168
kono
parents:
diff changeset
1169 function Ident (M : My_Int) return My_Int;
kono
parents:
diff changeset
1170 end Decls;
kono
parents:
diff changeset
1171
kono
parents:
diff changeset
1172 ::
kono
parents:
diff changeset
1173
kono
parents:
diff changeset
1174 with Utils;
kono
parents:
diff changeset
1175 package body Decls is
kono
parents:
diff changeset
1176 task body Lib_Task is
kono
parents:
diff changeset
1177 begin
kono
parents:
diff changeset
1178 accept Start;
kono
parents:
diff changeset
1179 Utils.Put_Val (2);
kono
parents:
diff changeset
1180 end Lib_Task;
kono
parents:
diff changeset
1181
kono
parents:
diff changeset
1182 function Ident (M : My_Int) return My_Int is
kono
parents:
diff changeset
1183 begin
kono
parents:
diff changeset
1184 return M;
kono
parents:
diff changeset
1185 end Ident;
kono
parents:
diff changeset
1186 end Decls;
kono
parents:
diff changeset
1187
kono
parents:
diff changeset
1188 ::
kono
parents:
diff changeset
1189
kono
parents:
diff changeset
1190 with Decls;
kono
parents:
diff changeset
1191 package Utils is
kono
parents:
diff changeset
1192 procedure Put_Val (Arg : Decls.My_Int);
kono
parents:
diff changeset
1193 end Utils;
kono
parents:
diff changeset
1194
kono
parents:
diff changeset
1195 ::
kono
parents:
diff changeset
1196
kono
parents:
diff changeset
1197 with Ada.Text_IO; use Ada.Text_IO;
kono
parents:
diff changeset
1198 package body Utils is
kono
parents:
diff changeset
1199 procedure Put_Val (Arg : Decls.My_Int) is
kono
parents:
diff changeset
1200 begin
kono
parents:
diff changeset
1201 Put_Line (Arg'Img);
kono
parents:
diff changeset
1202 end Put_Val;
kono
parents:
diff changeset
1203 end Utils;
kono
parents:
diff changeset
1204
kono
parents:
diff changeset
1205 ::
kono
parents:
diff changeset
1206
kono
parents:
diff changeset
1207 with Decls;
kono
parents:
diff changeset
1208 procedure Main is
kono
parents:
diff changeset
1209 begin
kono
parents:
diff changeset
1210 Decls.Lib_Task.Start;
kono
parents:
diff changeset
1211 end Main;
kono
parents:
diff changeset
1212
kono
parents:
diff changeset
1213 When the above example is compiled with the static model, an elaboration
kono
parents:
diff changeset
1214 circularity arises:
kono
parents:
diff changeset
1215
kono
parents:
diff changeset
1216 ::
kono
parents:
diff changeset
1217
kono
parents:
diff changeset
1218 error: elaboration circularity detected
kono
parents:
diff changeset
1219 info: "decls (body)" must be elaborated before "decls (body)"
kono
parents:
diff changeset
1220 info: reason: implicit Elaborate_All in unit "decls (body)"
kono
parents:
diff changeset
1221 info: recompile "decls (body)" with -gnatel for full details
kono
parents:
diff changeset
1222 info: "decls (body)"
kono
parents:
diff changeset
1223 info: must be elaborated along with its spec:
kono
parents:
diff changeset
1224 info: "decls (spec)"
kono
parents:
diff changeset
1225 info: which is withed by:
kono
parents:
diff changeset
1226 info: "utils (spec)"
kono
parents:
diff changeset
1227 info: which is withed by:
kono
parents:
diff changeset
1228 info: "decls (body)"
kono
parents:
diff changeset
1229
kono
parents:
diff changeset
1230 In the above example, ``Decls`` must be elaborated prior to ``Main`` by virtue
kono
parents:
diff changeset
1231 of a with clause. The elaboration of ``Decls`` activates task ``Lib_Task``. The
kono
parents:
diff changeset
1232 static model conservatibely assumes that all code within the body of
kono
parents:
diff changeset
1233 ``Lib_Task`` is executed, and generates an implicit ``Elaborate_All`` pragma
kono
parents:
diff changeset
1234 for ``Units`` due to the call to ``Utils.Put_Val``. The pragma implies that
kono
parents:
diff changeset
1235 both the spec and body of ``Utils``, along with any units they |with|,
kono
parents:
diff changeset
1236 must be elaborated prior to ``Decls``. However, ``Utils``'s spec |withs|
kono
parents:
diff changeset
1237 ``Decls``, implying that ``Decls`` must be elaborated before ``Utils``. The end
kono
parents:
diff changeset
1238 result is that ``Utils`` must be elaborated prior to ``Utils``, and this
kono
parents:
diff changeset
1239 leads to a circularity.
kono
parents:
diff changeset
1240
kono
parents:
diff changeset
1241 In reality, the example above will not exhibit an ABE problem at run time.
kono
parents:
diff changeset
1242 When the body of task ``Lib_Task`` is activated, execution will wait for entry
kono
parents:
diff changeset
1243 ``Start`` to be accepted, and the call to ``Utils.Put_Val`` will not take place
kono
parents:
diff changeset
1244 at elaboration time. Task ``Lib_Task`` will resume its execution after the main
kono
parents:
diff changeset
1245 program is executed because ``Main`` performs a rendezvous with
kono
parents:
diff changeset
1246 ``Lib_Task.Start``, and at that point all units have already been elaborated.
kono
parents:
diff changeset
1247 As a result, the static model may seem overly conservative, partly because it
kono
parents:
diff changeset
1248 does not take control and data flow into account.
kono
parents:
diff changeset
1249
kono
parents:
diff changeset
1250 When faced with a task elaboration circularity, a programmer has several
kono
parents:
diff changeset
1251 options available:
kono
parents:
diff changeset
1252
kono
parents:
diff changeset
1253 * *Use the dynamic model*
kono
parents:
diff changeset
1254
kono
parents:
diff changeset
1255 The dynamic model does not generate implicit ``Elaborate`` and
kono
parents:
diff changeset
1256 ``Elaborate_All`` pragmas. Instead, it will install checks prior to every
kono
parents:
diff changeset
1257 call in the example above, thus verifying the successful elaboration of
kono
parents:
diff changeset
1258 ``Utils.Put_Val`` in case the call to it takes place at elaboration time.
kono
parents:
diff changeset
1259 The dynamic model is enabled with compiler switch :switch:`-gnatE`.
kono
parents:
diff changeset
1260
kono
parents:
diff changeset
1261 * *Isolate the tasks*
kono
parents:
diff changeset
1262
kono
parents:
diff changeset
1263 Relocating tasks in their own separate package could decouple them from
kono
parents:
diff changeset
1264 dependencies that would otherwise cause an elaboration circularity. The
kono
parents:
diff changeset
1265 example above can be rewritten as follows:
kono
parents:
diff changeset
1266
kono
parents:
diff changeset
1267 ::
kono
parents:
diff changeset
1268
kono
parents:
diff changeset
1269 package Decls1 is -- new
kono
parents:
diff changeset
1270 task Lib_Task is
kono
parents:
diff changeset
1271 entry Start;
kono
parents:
diff changeset
1272 end Lib_Task;
kono
parents:
diff changeset
1273 end Decls1;
kono
parents:
diff changeset
1274
kono
parents:
diff changeset
1275 ::
kono
parents:
diff changeset
1276
kono
parents:
diff changeset
1277 with Utils;
kono
parents:
diff changeset
1278 package body Decls1 is -- new
kono
parents:
diff changeset
1279 task body Lib_Task is
kono
parents:
diff changeset
1280 begin
kono
parents:
diff changeset
1281 accept Start;
kono
parents:
diff changeset
1282 Utils.Put_Val (2);
kono
parents:
diff changeset
1283 end Lib_Task;
kono
parents:
diff changeset
1284 end Decls1;
kono
parents:
diff changeset
1285
kono
parents:
diff changeset
1286 ::
kono
parents:
diff changeset
1287
kono
parents:
diff changeset
1288 package Decls2 is -- new
kono
parents:
diff changeset
1289 type My_Int is new Integer;
kono
parents:
diff changeset
1290 function Ident (M : My_Int) return My_Int;
kono
parents:
diff changeset
1291 end Decls2;
kono
parents:
diff changeset
1292
kono
parents:
diff changeset
1293 ::
kono
parents:
diff changeset
1294
kono
parents:
diff changeset
1295 with Utils;
kono
parents:
diff changeset
1296 package body Decls2 is -- new
kono
parents:
diff changeset
1297 function Ident (M : My_Int) return My_Int is
kono
parents:
diff changeset
1298 begin
kono
parents:
diff changeset
1299 return M;
kono
parents:
diff changeset
1300 end Ident;
kono
parents:
diff changeset
1301 end Decls2;
kono
parents:
diff changeset
1302
kono
parents:
diff changeset
1303 ::
kono
parents:
diff changeset
1304
kono
parents:
diff changeset
1305 with Decls2;
kono
parents:
diff changeset
1306 package Utils is
kono
parents:
diff changeset
1307 procedure Put_Val (Arg : Decls2.My_Int);
kono
parents:
diff changeset
1308 end Utils;
kono
parents:
diff changeset
1309
kono
parents:
diff changeset
1310 ::
kono
parents:
diff changeset
1311
kono
parents:
diff changeset
1312 with Ada.Text_IO; use Ada.Text_IO;
kono
parents:
diff changeset
1313 package body Utils is
kono
parents:
diff changeset
1314 procedure Put_Val (Arg : Decls2.My_Int) is
kono
parents:
diff changeset
1315 begin
kono
parents:
diff changeset
1316 Put_Line (Arg'Img);
kono
parents:
diff changeset
1317 end Put_Val;
kono
parents:
diff changeset
1318 end Utils;
kono
parents:
diff changeset
1319
kono
parents:
diff changeset
1320 ::
kono
parents:
diff changeset
1321
kono
parents:
diff changeset
1322 with Decls1;
kono
parents:
diff changeset
1323 procedure Main is
kono
parents:
diff changeset
1324 begin
kono
parents:
diff changeset
1325 Decls1.Lib_Task.Start;
kono
parents:
diff changeset
1326 end Main;
kono
parents:
diff changeset
1327
kono
parents:
diff changeset
1328 * *Declare the tasks*
kono
parents:
diff changeset
1329
kono
parents:
diff changeset
1330 The original example uses a single task declaration for ``Lib_Task``. An
kono
parents:
diff changeset
1331 explicit task type declaration and a properly placed task object could avoid
kono
parents:
diff changeset
1332 the dependencies that would otherwise cause an elaboration circularity. The
kono
parents:
diff changeset
1333 example can be rewritten as follows:
kono
parents:
diff changeset
1334
kono
parents:
diff changeset
1335 ::
kono
parents:
diff changeset
1336
kono
parents:
diff changeset
1337 package Decls is
kono
parents:
diff changeset
1338 task type Lib_Task is -- new
kono
parents:
diff changeset
1339 entry Start;
kono
parents:
diff changeset
1340 end Lib_Task;
kono
parents:
diff changeset
1341
kono
parents:
diff changeset
1342 type My_Int is new Integer;
kono
parents:
diff changeset
1343
kono
parents:
diff changeset
1344 function Ident (M : My_Int) return My_Int;
kono
parents:
diff changeset
1345 end Decls;
kono
parents:
diff changeset
1346
kono
parents:
diff changeset
1347 ::
kono
parents:
diff changeset
1348
kono
parents:
diff changeset
1349 with Utils;
kono
parents:
diff changeset
1350 package body Decls is
kono
parents:
diff changeset
1351 task body Lib_Task is
kono
parents:
diff changeset
1352 begin
kono
parents:
diff changeset
1353 accept Start;
kono
parents:
diff changeset
1354 Utils.Put_Val (2);
kono
parents:
diff changeset
1355 end Lib_Task;
kono
parents:
diff changeset
1356
kono
parents:
diff changeset
1357 function Ident (M : My_Int) return My_Int is
kono
parents:
diff changeset
1358 begin
kono
parents:
diff changeset
1359 return M;
kono
parents:
diff changeset
1360 end Ident;
kono
parents:
diff changeset
1361 end Decls;
kono
parents:
diff changeset
1362
kono
parents:
diff changeset
1363 ::
kono
parents:
diff changeset
1364
kono
parents:
diff changeset
1365 with Decls;
kono
parents:
diff changeset
1366 package Utils is
kono
parents:
diff changeset
1367 procedure Put_Val (Arg : Decls.My_Int);
kono
parents:
diff changeset
1368 end Utils;
kono
parents:
diff changeset
1369
kono
parents:
diff changeset
1370 ::
kono
parents:
diff changeset
1371
kono
parents:
diff changeset
1372 with Ada.Text_IO; use Ada.Text_IO;
kono
parents:
diff changeset
1373 package body Utils is
kono
parents:
diff changeset
1374 procedure Put_Val (Arg : Decls.My_Int) is
kono
parents:
diff changeset
1375 begin
kono
parents:
diff changeset
1376 Put_Line (Arg'Img);
kono
parents:
diff changeset
1377 end Put_Val;
kono
parents:
diff changeset
1378 end Utils;
kono
parents:
diff changeset
1379
kono
parents:
diff changeset
1380 ::
kono
parents:
diff changeset
1381
kono
parents:
diff changeset
1382 with Decls;
kono
parents:
diff changeset
1383 package Obj_Decls is -- new
kono
parents:
diff changeset
1384 Task_Obj : Decls.Lib_Task;
kono
parents:
diff changeset
1385 end Obj_Decls;
kono
parents:
diff changeset
1386
kono
parents:
diff changeset
1387 ::
kono
parents:
diff changeset
1388
kono
parents:
diff changeset
1389 with Obj_Decls;
kono
parents:
diff changeset
1390 procedure Main is
kono
parents:
diff changeset
1391 begin
kono
parents:
diff changeset
1392 Obj_Decls.Task_Obj.Start; -- new
kono
parents:
diff changeset
1393 end Main;
kono
parents:
diff changeset
1394
kono
parents:
diff changeset
1395 * *Use restriction No_Entry_Calls_In_Elaboration_Code*
kono
parents:
diff changeset
1396
kono
parents:
diff changeset
1397 The issue exhibited in the original example under this section revolves
kono
parents:
diff changeset
1398 around the body of ``Lib_Task`` blocking on an accept statement. There is
kono
parents:
diff changeset
1399 no rule to prevent elaboration code from performing entry calls, however in
kono
parents:
diff changeset
1400 practice this is highly unusual. In addition, the pattern of starting tasks
kono
parents:
diff changeset
1401 at elaboration time and then immediately blocking on accept or select
kono
parents:
diff changeset
1402 statements is quite common.
kono
parents:
diff changeset
1403
kono
parents:
diff changeset
1404 If a programmer knows that elaboration code will not perform any entry
kono
parents:
diff changeset
1405 calls, then the programmer can indicate that the static model should not
kono
parents:
diff changeset
1406 process the remainder of a task body once an accept or select statement has
kono
parents:
diff changeset
1407 been encountered. This behavior can be specified by a configuration pragma:
kono
parents:
diff changeset
1408
kono
parents:
diff changeset
1409 ::
kono
parents:
diff changeset
1410
kono
parents:
diff changeset
1411 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
kono
parents:
diff changeset
1412
kono
parents:
diff changeset
1413 In addition to the change in behavior with respect to task bodies, the
kono
parents:
diff changeset
1414 static model will verify that no entry calls take place at elaboration time.
kono
parents:
diff changeset
1415
kono
parents:
diff changeset
1416 .. _Elaboration_Related_Compiler_Switches:
kono
parents:
diff changeset
1417
kono
parents:
diff changeset
1418 Elaboration-related Compiler Switches
kono
parents:
diff changeset
1419 =====================================
kono
parents:
diff changeset
1420
kono
parents:
diff changeset
1421 GNAT has several switches that affect the elaboration model and consequently
kono
parents:
diff changeset
1422 the elaboration order chosen by the binder.
kono
parents:
diff changeset
1423
kono
parents:
diff changeset
1424 .. index:: -gnatdE (gnat)
kono
parents:
diff changeset
1425
kono
parents:
diff changeset
1426 :switch:`-gnatdE`
kono
parents:
diff changeset
1427 Elaboration checks on predefined units
kono
parents:
diff changeset
1428
kono
parents:
diff changeset
1429 When this switch is in effect, GNAT will consider scenarios and targets that
kono
parents:
diff changeset
1430 come from the Ada, GNAT, Interfaces, and System hierarchies. This switch is
kono
parents:
diff changeset
1431 useful when a programmer has defined a custom grandchild of those packages.
kono
parents:
diff changeset
1432
kono
parents:
diff changeset
1433 .. index:: -gnatd.G (gnat)
kono
parents:
diff changeset
1434
kono
parents:
diff changeset
1435 :switch:`-gnatd.G`
kono
parents:
diff changeset
1436 Ignore calls through generic formal parameters for elaboration
kono
parents:
diff changeset
1437
kono
parents:
diff changeset
1438 When this switch is in effect, GNAT will ignore calls that invoke generic
kono
parents:
diff changeset
1439 actual entries, operators, or subprograms via generic formal subprograms. As
kono
parents:
diff changeset
1440 a result, GNAT will not generate implicit ``Elaborate`` and ``Elaborate_All``
kono
parents:
diff changeset
1441 pragmas, and run-time checks for such calls. Note that this switch does not
kono
parents:
diff changeset
1442 overlap with :switch:`-gnatdL`.
kono
parents:
diff changeset
1443
kono
parents:
diff changeset
1444 ::
kono
parents:
diff changeset
1445
kono
parents:
diff changeset
1446 package body Ignore_Calls is
kono
parents:
diff changeset
1447 function ABE return Integer;
kono
parents:
diff changeset
1448
kono
parents:
diff changeset
1449 generic
kono
parents:
diff changeset
1450 with function Gen_Formal return Integer;
kono
parents:
diff changeset
1451 package Gen is
kono
parents:
diff changeset
1452 Val : constant Integer := Gen_Formal;
kono
parents:
diff changeset
1453 end Gen;
kono
parents:
diff changeset
1454
kono
parents:
diff changeset
1455 package Inst is new Gen (ABE);
kono
parents:
diff changeset
1456
kono
parents:
diff changeset
1457 function ABE return Integer is
kono
parents:
diff changeset
1458 begin
kono
parents:
diff changeset
1459 ...
kono
parents:
diff changeset
1460 end ABE;
kono
parents:
diff changeset
1461 end Ignore_Calls;
kono
parents:
diff changeset
1462
kono
parents:
diff changeset
1463 In the example above, the call to function ``ABE`` will be ignored because it
kono
parents:
diff changeset
1464 occurs during the elaboration of instance ``Inst``, through a call to generic
kono
parents:
diff changeset
1465 formal subprogram ``Gen_Formal``.
kono
parents:
diff changeset
1466
kono
parents:
diff changeset
1467 .. index:: -gnatdL (gnat)
kono
parents:
diff changeset
1468
kono
parents:
diff changeset
1469 :switch:`-gnatdL`
kono
parents:
diff changeset
1470 Ignore external calls from instances for elaboration
kono
parents:
diff changeset
1471
kono
parents:
diff changeset
1472 When this switch is in effect, GNAT will ignore calls that originate from
kono
parents:
diff changeset
1473 within an instance and directly target an entry, operator, or subprogram
kono
parents:
diff changeset
1474 defined outside the instance. As a result, GNAT will not generate implicit
kono
parents:
diff changeset
1475 ``Elaborate`` and ``Elaborate_All`` pragmas, and run-time checks for such
kono
parents:
diff changeset
1476 calls. Note that this switch does not overlap with :switch:`-gnatd.G`.
kono
parents:
diff changeset
1477
kono
parents:
diff changeset
1478 ::
kono
parents:
diff changeset
1479
kono
parents:
diff changeset
1480 package body Ignore_Calls is
kono
parents:
diff changeset
1481 function ABE return Integer;
kono
parents:
diff changeset
1482
kono
parents:
diff changeset
1483 generic
kono
parents:
diff changeset
1484 package Gen is
kono
parents:
diff changeset
1485 Val : constant Integer := ABE;
kono
parents:
diff changeset
1486 end Gen;
kono
parents:
diff changeset
1487
kono
parents:
diff changeset
1488 package Inst is new Gen;
kono
parents:
diff changeset
1489
kono
parents:
diff changeset
1490 function ABE return Integer is
kono
parents:
diff changeset
1491 begin
kono
parents:
diff changeset
1492 ...
kono
parents:
diff changeset
1493 end ABE;
kono
parents:
diff changeset
1494 end Ignore_Calls;
kono
parents:
diff changeset
1495
kono
parents:
diff changeset
1496 In the example above, the call to function ``ABE`` will be ignored because it
kono
parents:
diff changeset
1497 originates from within an instance and targets a subprogram defined outside
kono
parents:
diff changeset
1498 the instance.
kono
parents:
diff changeset
1499
kono
parents:
diff changeset
1500 .. index:: -gnatd.o (gnat)
kono
parents:
diff changeset
1501
kono
parents:
diff changeset
1502 :switch:`-gnatd.o`
kono
parents:
diff changeset
1503 Conservative elaboration order for indirect calls
kono
parents:
diff changeset
1504
kono
parents:
diff changeset
1505 When this switch is in effect, GNAT will treat ``'Access`` of an entry,
kono
parents:
diff changeset
1506 operator, or subprogram as an immediate call to that target. As a result,
kono
parents:
diff changeset
1507 GNAT will generate implicit ``Elaborate`` and ``Elaborate_All`` pragmas as
kono
parents:
diff changeset
1508 well as run-time checks for such attribute references.
kono
parents:
diff changeset
1509
kono
parents:
diff changeset
1510 ::
kono
parents:
diff changeset
1511
kono
parents:
diff changeset
1512 1. package body Attribute_Call is
kono
parents:
diff changeset
1513 2. function Func return Integer;
kono
parents:
diff changeset
1514 3. type Func_Ptr is access function return Integer;
kono
parents:
diff changeset
1515 4.
kono
parents:
diff changeset
1516 5. Ptr : constant Func_Ptr := Func'Access;
kono
parents:
diff changeset
1517 |
kono
parents:
diff changeset
1518 >>> warning: cannot call "Func" before body seen
kono
parents:
diff changeset
1519 >>> warning: Program_Error may be raised at run time
kono
parents:
diff changeset
1520 >>> warning: body of unit "Attribute_Call" elaborated
kono
parents:
diff changeset
1521 >>> warning: "Access" of "Func" taken at line 5
kono
parents:
diff changeset
1522 >>> warning: function "Func" called at line 5
kono
parents:
diff changeset
1523
kono
parents:
diff changeset
1524 6.
kono
parents:
diff changeset
1525 7. function Func return Integer is
kono
parents:
diff changeset
1526 8. begin
kono
parents:
diff changeset
1527 9. ...
kono
parents:
diff changeset
1528 10. end Func;
kono
parents:
diff changeset
1529 11. end Attribute_Call;
kono
parents:
diff changeset
1530
kono
parents:
diff changeset
1531 In the example above, the elaboration of declaration ``Ptr`` is assigned
kono
parents:
diff changeset
1532 ``Func'Access`` before the body of ``Func`` has been elaborated.
kono
parents:
diff changeset
1533
kono
parents:
diff changeset
1534 .. index:: -gnatd.U (gnat)
kono
parents:
diff changeset
1535
kono
parents:
diff changeset
1536 :switch:`-gnatd.U`
kono
parents:
diff changeset
1537 Ignore indirect calls for static elaboration
kono
parents:
diff changeset
1538
kono
parents:
diff changeset
1539 When this switch is in effect, GNAT will ignore ``'Access`` of an entry,
kono
parents:
diff changeset
1540 operator, or subprogram when the static model is in effect.
kono
parents:
diff changeset
1541
kono
parents:
diff changeset
1542 .. index:: -gnatd.v (gnat)
kono
parents:
diff changeset
1543
kono
parents:
diff changeset
1544 :switch:`-gnatd.v`
kono
parents:
diff changeset
1545 Enforce SPARK elaboration rules in SPARK code
kono
parents:
diff changeset
1546
kono
parents:
diff changeset
1547 When this switch is in effect, GNAT will enforce the SPARK rules of
kono
parents:
diff changeset
1548 elaboration as defined in the SPARK Reference Manual, section 7.7. As a
kono
parents:
diff changeset
1549 result, constructs which violate the SPARK elaboration rules are no longer
kono
parents:
diff changeset
1550 accepted, even if GNAT is able to statically ensure that these constructs
kono
parents:
diff changeset
1551 will not lead to ABE problems.
kono
parents:
diff changeset
1552
kono
parents:
diff changeset
1553 .. index:: -gnatd.y (gnat)
kono
parents:
diff changeset
1554
kono
parents:
diff changeset
1555 :switch:`-gnatd.y`
kono
parents:
diff changeset
1556 Disable implicit pragma Elaborate[_All] on task bodies
kono
parents:
diff changeset
1557
kono
parents:
diff changeset
1558 When this switch is in effect, GNAT will not generate ``Elaborate`` and
kono
parents:
diff changeset
1559 ``Elaborate_All`` pragmas if the need for the pragma came directly or
kono
parents:
diff changeset
1560 indirectly from a task body.
kono
parents:
diff changeset
1561
kono
parents:
diff changeset
1562 ::
kono
parents:
diff changeset
1563
kono
parents:
diff changeset
1564 with Server;
kono
parents:
diff changeset
1565 package body Disable_Task is
kono
parents:
diff changeset
1566 task T;
kono
parents:
diff changeset
1567
kono
parents:
diff changeset
1568 task body T is
kono
parents:
diff changeset
1569 begin
kono
parents:
diff changeset
1570 Server.Proc;
kono
parents:
diff changeset
1571 end T;
kono
parents:
diff changeset
1572 end Disable_Task;
kono
parents:
diff changeset
1573
kono
parents:
diff changeset
1574 In the example above, the activation of single task ``T`` invokes
kono
parents:
diff changeset
1575 ``Server.Proc``, which implies that ``Server`` requires ``Elaborate_All``,
kono
parents:
diff changeset
1576 however GNAT will not generate the pragma.
kono
parents:
diff changeset
1577
kono
parents:
diff changeset
1578 .. index:: -gnatE (gnat)
kono
parents:
diff changeset
1579
kono
parents:
diff changeset
1580 :switch:`-gnatE`
kono
parents:
diff changeset
1581 Dynamic elaboration checking mode enabled
kono
parents:
diff changeset
1582
kono
parents:
diff changeset
1583 When this switch is in effect, GNAT activates the dynamic elaboration model.
kono
parents:
diff changeset
1584
kono
parents:
diff changeset
1585 .. index:: -gnatel (gnat)
kono
parents:
diff changeset
1586
kono
parents:
diff changeset
1587 :switch:`-gnatel`
kono
parents:
diff changeset
1588 Turn on info messages on generated Elaborate[_All] pragmas
kono
parents:
diff changeset
1589
kono
parents:
diff changeset
1590 When this switch is in effect, GNAT will emit the following supplementary
kono
parents:
diff changeset
1591 information depending on the elaboration model in effect.
kono
parents:
diff changeset
1592
kono
parents:
diff changeset
1593 - *Dynamic model*
kono
parents:
diff changeset
1594
kono
parents:
diff changeset
1595 GNAT will indicate missing ``Elaborate`` and ``Elaborate_All`` pragmas for
kono
parents:
diff changeset
1596 all library-level scenarios within the partition.
kono
parents:
diff changeset
1597
kono
parents:
diff changeset
1598 - *Static model*
kono
parents:
diff changeset
1599
kono
parents:
diff changeset
1600 GNAT will indicate all scenarios executed during elaboration. In addition,
kono
parents:
diff changeset
1601 it will provide detailed traceback when an implicit ``Elaborate`` or
kono
parents:
diff changeset
1602 ``Elaborate_All`` pragma is generated.
kono
parents:
diff changeset
1603
kono
parents:
diff changeset
1604 - *SPARK model*
kono
parents:
diff changeset
1605
kono
parents:
diff changeset
1606 GNAT will indicate how an elaboration requirement is met by the context of
kono
parents:
diff changeset
1607 a unit. This diagnostic requires compiler switch :switch:`-gnatd.v`.
kono
parents:
diff changeset
1608
kono
parents:
diff changeset
1609 ::
kono
parents:
diff changeset
1610
kono
parents:
diff changeset
1611 1. with Server; pragma Elaborate_All (Server);
kono
parents:
diff changeset
1612 2. package Client with SPARK_Mode is
kono
parents:
diff changeset
1613 3. Val : constant Integer := Server.Func;
kono
parents:
diff changeset
1614 |
kono
parents:
diff changeset
1615 >>> info: call to "Func" during elaboration in SPARK
kono
parents:
diff changeset
1616 >>> info: "Elaborate_All" requirement for unit "Server" met by pragma at line 1
kono
parents:
diff changeset
1617
kono
parents:
diff changeset
1618 4. end Client;
kono
parents:
diff changeset
1619
kono
parents:
diff changeset
1620 .. index:: -gnatw.f (gnat)
kono
parents:
diff changeset
1621
kono
parents:
diff changeset
1622 :switch:`-gnatw.f`
kono
parents:
diff changeset
1623 Turn on warnings for suspicious Subp'Access
kono
parents:
diff changeset
1624
kono
parents:
diff changeset
1625 When this switch is in effect, GNAT will treat ``'Access`` of an entry,
kono
parents:
diff changeset
1626 operator, or subprogram as a potential call to the target and issue warnings:
kono
parents:
diff changeset
1627
kono
parents:
diff changeset
1628 ::
kono
parents:
diff changeset
1629
kono
parents:
diff changeset
1630 1. package body Attribute_Call is
kono
parents:
diff changeset
1631 2. function Func return Integer;
kono
parents:
diff changeset
1632 3. type Func_Ptr is access function return Integer;
kono
parents:
diff changeset
1633 4.
kono
parents:
diff changeset
1634 5. Ptr : constant Func_Ptr := Func'Access;
kono
parents:
diff changeset
1635 |
kono
parents:
diff changeset
1636 >>> warning: "Access" attribute of "Func" before body seen
kono
parents:
diff changeset
1637 >>> warning: possible Program_Error on later references
kono
parents:
diff changeset
1638 >>> warning: body of unit "Attribute_Call" elaborated
kono
parents:
diff changeset
1639 >>> warning: "Access" of "Func" taken at line 5
kono
parents:
diff changeset
1640
kono
parents:
diff changeset
1641 6.
kono
parents:
diff changeset
1642 7. function Func return Integer is
kono
parents:
diff changeset
1643 8. begin
kono
parents:
diff changeset
1644 9. ...
kono
parents:
diff changeset
1645 10. end Func;
kono
parents:
diff changeset
1646 11. end Attribute_Call;
kono
parents:
diff changeset
1647
kono
parents:
diff changeset
1648 In the example above, the elaboration of declaration ``Ptr`` is assigned
kono
parents:
diff changeset
1649 ``Func'Access`` before the body of ``Func`` has been elaborated.
kono
parents:
diff changeset
1650
kono
parents:
diff changeset
1651 .. _Summary_of_Procedures_for_Elaboration_Control:
kono
parents:
diff changeset
1652
kono
parents:
diff changeset
1653 Summary of Procedures for Elaboration Control
kono
parents:
diff changeset
1654 =============================================
kono
parents:
diff changeset
1655
kono
parents:
diff changeset
1656 A programmer should first compile the program with the default options, using
kono
parents:
diff changeset
1657 none of the binder or compiler switches. If the binder succeeds in finding an
kono
parents:
diff changeset
1658 elaboration order, then apart from possible cases involing dispatching calls
kono
parents:
diff changeset
1659 and access-to-subprogram types, the program is free of elaboration errors.
kono
parents:
diff changeset
1660 If it is important for the program to be portable to compilers other than GNAT,
kono
parents:
diff changeset
1661 then the programmer should use compiler switch :switch:`-gnatel` and consider
kono
parents:
diff changeset
1662 the messages about missing or implicitly created ``Elaborate`` and
kono
parents:
diff changeset
1663 ``Elaborate_All`` pragmas.
kono
parents:
diff changeset
1664
kono
parents:
diff changeset
1665 If the binder reports an elaboration circularity, the programmer has several
kono
parents:
diff changeset
1666 options:
kono
parents:
diff changeset
1667
kono
parents:
diff changeset
1668 * Ensure that warnings are enabled. This will allow the static model to output
kono
parents:
diff changeset
1669 trace information of elaboration issues. The trace information could shed
kono
parents:
diff changeset
1670 light on previously unforeseen dependencies, as well as their origins.
kono
parents:
diff changeset
1671
kono
parents:
diff changeset
1672 * Use switch :switch:`-gnatel` to obtain messages on generated implicit
kono
parents:
diff changeset
1673 ``Elaborate`` and ``Elaborate_All`` pragmas. The trace information could
kono
parents:
diff changeset
1674 indicate why a server unit must be elaborated prior to a client unit.
kono
parents:
diff changeset
1675
kono
parents:
diff changeset
1676 * If the warnings produced by the static model indicate that a task is
kono
parents:
diff changeset
1677 involved, consider the options in the section on resolving task issues as
kono
parents:
diff changeset
1678 well as compiler switch :switch:`-gnatd.y`.
kono
parents:
diff changeset
1679
kono
parents:
diff changeset
1680 * If the warnings produced by the static model indicate that an generic
kono
parents:
diff changeset
1681 instantiations are involved, consider using compiler switches
kono
parents:
diff changeset
1682 :switch:`-gnatd.G` and :switch:`-gnatdL`.
kono
parents:
diff changeset
1683
kono
parents:
diff changeset
1684 * If none of the steps outlined above resolve the circularity, recompile the
kono
parents:
diff changeset
1685 program using the dynamic model by using compiler switch :switch:`-gnatE`.
kono
parents:
diff changeset
1686
kono
parents:
diff changeset
1687 .. _Inspecting_the_Chosen_Elaboration_Order:
kono
parents:
diff changeset
1688
kono
parents:
diff changeset
1689 Inspecting the Chosen Elaboration Order
kono
parents:
diff changeset
1690 =======================================
kono
parents:
diff changeset
1691
kono
parents:
diff changeset
1692 To see the elaboration order chosen by the binder, inspect the contents of file
kono
parents:
diff changeset
1693 `b~xxx.adb`. On certain targets, this file appears as `b_xxx.adb`. The
kono
parents:
diff changeset
1694 elaboration order appears as a sequence of calls to ``Elab_Body`` and
kono
parents:
diff changeset
1695 ``Elab_Spec``, interspersed with assignments to `Exxx` which indicates that a
kono
parents:
diff changeset
1696 particular unit is elaborated. For example:
kono
parents:
diff changeset
1697
kono
parents:
diff changeset
1698 ::
kono
parents:
diff changeset
1699
kono
parents:
diff changeset
1700 System.Soft_Links'Elab_Body;
kono
parents:
diff changeset
1701 E14 := True;
kono
parents:
diff changeset
1702 System.Secondary_Stack'Elab_Body;
kono
parents:
diff changeset
1703 E18 := True;
kono
parents:
diff changeset
1704 System.Exception_Table'Elab_Body;
kono
parents:
diff changeset
1705 E24 := True;
kono
parents:
diff changeset
1706 Ada.Io_Exceptions'Elab_Spec;
kono
parents:
diff changeset
1707 E67 := True;
kono
parents:
diff changeset
1708 Ada.Tags'Elab_Spec;
kono
parents:
diff changeset
1709 Ada.Streams'Elab_Spec;
kono
parents:
diff changeset
1710 E43 := True;
kono
parents:
diff changeset
1711 Interfaces.C'Elab_Spec;
kono
parents:
diff changeset
1712 E69 := True;
kono
parents:
diff changeset
1713 System.Finalization_Root'Elab_Spec;
kono
parents:
diff changeset
1714 E60 := True;
kono
parents:
diff changeset
1715 System.Os_Lib'Elab_Body;
kono
parents:
diff changeset
1716 E71 := True;
kono
parents:
diff changeset
1717 System.Finalization_Implementation'Elab_Spec;
kono
parents:
diff changeset
1718 System.Finalization_Implementation'Elab_Body;
kono
parents:
diff changeset
1719 E62 := True;
kono
parents:
diff changeset
1720 Ada.Finalization'Elab_Spec;
kono
parents:
diff changeset
1721 E58 := True;
kono
parents:
diff changeset
1722 Ada.Finalization.List_Controller'Elab_Spec;
kono
parents:
diff changeset
1723 E76 := True;
kono
parents:
diff changeset
1724 System.File_Control_Block'Elab_Spec;
kono
parents:
diff changeset
1725 E74 := True;
kono
parents:
diff changeset
1726 System.File_Io'Elab_Body;
kono
parents:
diff changeset
1727 E56 := True;
kono
parents:
diff changeset
1728 Ada.Tags'Elab_Body;
kono
parents:
diff changeset
1729 E45 := True;
kono
parents:
diff changeset
1730 Ada.Text_Io'Elab_Spec;
kono
parents:
diff changeset
1731 Ada.Text_Io'Elab_Body;
kono
parents:
diff changeset
1732 E07 := True;
kono
parents:
diff changeset
1733
kono
parents:
diff changeset
1734 Note also binder switch :switch:`-l`, which outputs the chosen elaboration
kono
parents:
diff changeset
1735 order and provides a more readable form of the above:
kono
parents:
diff changeset
1736
kono
parents:
diff changeset
1737 ::
kono
parents:
diff changeset
1738
kono
parents:
diff changeset
1739 ada (spec)
kono
parents:
diff changeset
1740 interfaces (spec)
kono
parents:
diff changeset
1741 system (spec)
kono
parents:
diff changeset
1742 system.case_util (spec)
kono
parents:
diff changeset
1743 system.case_util (body)
kono
parents:
diff changeset
1744 system.concat_2 (spec)
kono
parents:
diff changeset
1745 system.concat_2 (body)
kono
parents:
diff changeset
1746 system.concat_3 (spec)
kono
parents:
diff changeset
1747 system.concat_3 (body)
kono
parents:
diff changeset
1748 system.htable (spec)
kono
parents:
diff changeset
1749 system.parameters (spec)
kono
parents:
diff changeset
1750 system.parameters (body)
kono
parents:
diff changeset
1751 system.crtl (spec)
kono
parents:
diff changeset
1752 interfaces.c_streams (spec)
kono
parents:
diff changeset
1753 interfaces.c_streams (body)
kono
parents:
diff changeset
1754 system.restrictions (spec)
kono
parents:
diff changeset
1755 system.restrictions (body)
kono
parents:
diff changeset
1756 system.standard_library (spec)
kono
parents:
diff changeset
1757 system.exceptions (spec)
kono
parents:
diff changeset
1758 system.exceptions (body)
kono
parents:
diff changeset
1759 system.storage_elements (spec)
kono
parents:
diff changeset
1760 system.storage_elements (body)
kono
parents:
diff changeset
1761 system.secondary_stack (spec)
kono
parents:
diff changeset
1762 system.stack_checking (spec)
kono
parents:
diff changeset
1763 system.stack_checking (body)
kono
parents:
diff changeset
1764 system.string_hash (spec)
kono
parents:
diff changeset
1765 system.string_hash (body)
kono
parents:
diff changeset
1766 system.htable (body)
kono
parents:
diff changeset
1767 system.strings (spec)
kono
parents:
diff changeset
1768 system.strings (body)
kono
parents:
diff changeset
1769 system.traceback (spec)
kono
parents:
diff changeset
1770 system.traceback (body)
kono
parents:
diff changeset
1771 system.traceback_entries (spec)
kono
parents:
diff changeset
1772 system.traceback_entries (body)
kono
parents:
diff changeset
1773 ada.exceptions (spec)
kono
parents:
diff changeset
1774 ada.exceptions.last_chance_handler (spec)
kono
parents:
diff changeset
1775 system.soft_links (spec)
kono
parents:
diff changeset
1776 system.soft_links (body)
kono
parents:
diff changeset
1777 ada.exceptions.last_chance_handler (body)
kono
parents:
diff changeset
1778 system.secondary_stack (body)
kono
parents:
diff changeset
1779 system.exception_table (spec)
kono
parents:
diff changeset
1780 system.exception_table (body)
kono
parents:
diff changeset
1781 ada.io_exceptions (spec)
kono
parents:
diff changeset
1782 ada.tags (spec)
kono
parents:
diff changeset
1783 ada.streams (spec)
kono
parents:
diff changeset
1784 interfaces.c (spec)
kono
parents:
diff changeset
1785 interfaces.c (body)
kono
parents:
diff changeset
1786 system.finalization_root (spec)
kono
parents:
diff changeset
1787 system.finalization_root (body)
kono
parents:
diff changeset
1788 system.memory (spec)
kono
parents:
diff changeset
1789 system.memory (body)
kono
parents:
diff changeset
1790 system.standard_library (body)
kono
parents:
diff changeset
1791 system.os_lib (spec)
kono
parents:
diff changeset
1792 system.os_lib (body)
kono
parents:
diff changeset
1793 system.unsigned_types (spec)
kono
parents:
diff changeset
1794 system.stream_attributes (spec)
kono
parents:
diff changeset
1795 system.stream_attributes (body)
kono
parents:
diff changeset
1796 system.finalization_implementation (spec)
kono
parents:
diff changeset
1797 system.finalization_implementation (body)
kono
parents:
diff changeset
1798 ada.finalization (spec)
kono
parents:
diff changeset
1799 ada.finalization (body)
kono
parents:
diff changeset
1800 ada.finalization.list_controller (spec)
kono
parents:
diff changeset
1801 ada.finalization.list_controller (body)
kono
parents:
diff changeset
1802 system.file_control_block (spec)
kono
parents:
diff changeset
1803 system.file_io (spec)
kono
parents:
diff changeset
1804 system.file_io (body)
kono
parents:
diff changeset
1805 system.val_uns (spec)
kono
parents:
diff changeset
1806 system.val_util (spec)
kono
parents:
diff changeset
1807 system.val_util (body)
kono
parents:
diff changeset
1808 system.val_uns (body)
kono
parents:
diff changeset
1809 system.wch_con (spec)
kono
parents:
diff changeset
1810 system.wch_con (body)
kono
parents:
diff changeset
1811 system.wch_cnv (spec)
kono
parents:
diff changeset
1812 system.wch_jis (spec)
kono
parents:
diff changeset
1813 system.wch_jis (body)
kono
parents:
diff changeset
1814 system.wch_cnv (body)
kono
parents:
diff changeset
1815 system.wch_stw (spec)
kono
parents:
diff changeset
1816 system.wch_stw (body)
kono
parents:
diff changeset
1817 ada.tags (body)
kono
parents:
diff changeset
1818 ada.exceptions (body)
kono
parents:
diff changeset
1819 ada.text_io (spec)
kono
parents:
diff changeset
1820 ada.text_io (body)
kono
parents:
diff changeset
1821 text_io (spec)
kono
parents:
diff changeset
1822 gdbstr (body)