annotate gcc/ada/doc/gnat_ugn/elaboration_order_handling_in_gnat.rst @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
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
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
633 .. index:: Legacy elaboration model
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
634
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
635 * *Legacy elaboration model*
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
636
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
637 In addition to the three elaboration models outlined above, GNAT provides the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
638 elaboration model of pre-18.x versions referred to as `legacy elaboration
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
639 model`. The legacy elaboration model is enabled with compiler switch
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
640 :switch:`-gnatH`.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
641
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
642 .. index:: Relaxed elaboration mode
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
643
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
644 The dynamic, legacy, and static models can be relaxed using compiler switch
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
645 :switch:`-gnatJ`, making them more permissive. Note that in this mode, GNAT
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
646 may not diagnose certain elaboration issues or install run-time checks.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
647
111
kono
parents:
diff changeset
648 .. _Common_Elaboration_Model_Traits":
kono
parents:
diff changeset
649
kono
parents:
diff changeset
650 Common Elaboration-model Traits
kono
parents:
diff changeset
651 ===============================
kono
parents:
diff changeset
652
kono
parents:
diff changeset
653 All three GNAT models are able to detect elaboration problems related to
kono
parents:
diff changeset
654 dispatching calls and a particular kind of ABE referred to as *guaranteed ABE*.
kono
parents:
diff changeset
655
kono
parents:
diff changeset
656 * *Dispatching calls*
kono
parents:
diff changeset
657
kono
parents:
diff changeset
658 GNAT installs run-time checks for each primitive subprogram of each tagged
kono
parents:
diff changeset
659 type defined in a partition on the assumption that a dispatching call
kono
parents:
diff changeset
660 invoked at elaboration time will execute one of these primitives. As a
kono
parents:
diff changeset
661 result, a dispatching call that executes a primitive whose body has not
kono
parents:
diff changeset
662 been elaborated yet will raise exception ``Program_Error`` at run time. The
kono
parents:
diff changeset
663 checks can be suppressed using pragma ``Suppress (Elaboration_Check)``.
kono
parents:
diff changeset
664
kono
parents:
diff changeset
665 * *Guaranteed ABE*
kono
parents:
diff changeset
666
kono
parents:
diff changeset
667 A guaranteed ABE arises when the body of a target is not elaborated early
kono
parents:
diff changeset
668 enough, and causes all scenarios that directly execute the target to fail.
kono
parents:
diff changeset
669
kono
parents:
diff changeset
670 ::
kono
parents:
diff changeset
671
kono
parents:
diff changeset
672 package body Guaranteed_ABE is
kono
parents:
diff changeset
673 function ABE return Integer;
kono
parents:
diff changeset
674
kono
parents:
diff changeset
675 Val : constant Integer := ABE;
kono
parents:
diff changeset
676
kono
parents:
diff changeset
677 function ABE return Integer is
kono
parents:
diff changeset
678 begin
kono
parents:
diff changeset
679 ...
kono
parents:
diff changeset
680 end ABE;
kono
parents:
diff changeset
681 end Guaranteed_ABE;
kono
parents:
diff changeset
682
kono
parents:
diff changeset
683 In the example above, the elaboration of ``Guaranteed_ABE``'s body elaborates
kono
parents:
diff changeset
684 the declaration of ``Val``. This invokes function ``ABE``, however the body
kono
parents:
diff changeset
685 of ``ABE`` has not been elaborated yet. GNAT emits similar diagnostics in all
kono
parents:
diff changeset
686 three models:
kono
parents:
diff changeset
687
kono
parents:
diff changeset
688 ::
kono
parents:
diff changeset
689
kono
parents:
diff changeset
690 1. package body Guaranteed_ABE is
kono
parents:
diff changeset
691 2. function ABE return Integer;
kono
parents:
diff changeset
692 3.
kono
parents:
diff changeset
693 4. Val : constant Integer := ABE;
kono
parents:
diff changeset
694 |
kono
parents:
diff changeset
695 >>> warning: cannot call "ABE" before body seen
kono
parents:
diff changeset
696 >>> warning: Program_Error will be raised at run time
kono
parents:
diff changeset
697
kono
parents:
diff changeset
698 5.
kono
parents:
diff changeset
699 6. function ABE return Integer is
kono
parents:
diff changeset
700 7. begin
kono
parents:
diff changeset
701 8. ...
kono
parents:
diff changeset
702 9. end ABE;
kono
parents:
diff changeset
703 10. end Guaranteed_ABE;
kono
parents:
diff changeset
704
kono
parents:
diff changeset
705 Note that GNAT emits warnings rather than hard errors whenever it encounters an
kono
parents:
diff changeset
706 elaboration problem. This is because the elaboration model in effect may be too
kono
parents:
diff changeset
707 conservative, or a particular scenario may not be elaborated or executed due to
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
708 data and control flow. The warnings can be suppressed selectively with ``pragma
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
709 Warnigns (Off)`` or globally with compiler switch :switch:`-gnatwL`.
111
kono
parents:
diff changeset
710
kono
parents:
diff changeset
711 .. _Dynamic_Elaboration_Model_in_GNAT:
kono
parents:
diff changeset
712
kono
parents:
diff changeset
713 Dynamic Elaboration Model in GNAT
kono
parents:
diff changeset
714 =================================
kono
parents:
diff changeset
715
kono
parents:
diff changeset
716 The dynamic model assumes that all code within all units in a partition is
kono
parents:
diff changeset
717 elaboration code. As a result, run-time checks are installed for each scenario
kono
parents:
diff changeset
718 regardless of whether the target is internal or external. The checks can be
kono
parents:
diff changeset
719 suppressed using pragma ``Suppress (Elaboration_Check)``. This behavior is
kono
parents:
diff changeset
720 identical to that specified by the Ada Reference Manual. The following example
kono
parents:
diff changeset
721 showcases run-time checks installed by GNAT to verify the elaboration state of
kono
parents:
diff changeset
722 package ``Dynamic_Model``.
kono
parents:
diff changeset
723
kono
parents:
diff changeset
724 ::
kono
parents:
diff changeset
725
kono
parents:
diff changeset
726 with Server;
kono
parents:
diff changeset
727 package body Dynamic_Model is
kono
parents:
diff changeset
728 procedure API is
kono
parents:
diff changeset
729 begin
kono
parents:
diff changeset
730 ...
kono
parents:
diff changeset
731 end API;
kono
parents:
diff changeset
732
kono
parents:
diff changeset
733 <check that the body of Server.Gen is elaborated>
kono
parents:
diff changeset
734 package Inst is new Server.Gen;
kono
parents:
diff changeset
735
kono
parents:
diff changeset
736 T : Server.Task_Type;
kono
parents:
diff changeset
737
kono
parents:
diff changeset
738 begin
kono
parents:
diff changeset
739 <check that the body of Server.Task_Type is elaborated>
kono
parents:
diff changeset
740
kono
parents:
diff changeset
741 <check that the body of Server.Proc is elaborated>
kono
parents:
diff changeset
742 Server.Proc;
kono
parents:
diff changeset
743 end Dynamic_Model;
kono
parents:
diff changeset
744
kono
parents:
diff changeset
745 The checks verify that the body of a target has been successfully elaborated
kono
parents:
diff changeset
746 before a scenario activates, calls, or instantiates a target.
kono
parents:
diff changeset
747
kono
parents:
diff changeset
748 Note that no scenario within package ``Dynamic_Model`` calls procedure ``API``.
kono
parents:
diff changeset
749 In fact, procedure ``API`` may not be invoked by elaboration code within the
kono
parents:
diff changeset
750 partition, however the dynamic model assumes that this can happen.
kono
parents:
diff changeset
751
kono
parents:
diff changeset
752 The dynamic model emits very few diagnostics, but can make suggestions on
kono
parents:
diff changeset
753 missing ``Elaborate`` and ``Elaborate_All`` pragmas for library-level
kono
parents:
diff changeset
754 scenarios. This information is available when compiler switch :switch:`-gnatel`
kono
parents:
diff changeset
755 is in effect.
kono
parents:
diff changeset
756
kono
parents:
diff changeset
757 ::
kono
parents:
diff changeset
758
kono
parents:
diff changeset
759 1. with Server;
kono
parents:
diff changeset
760 2. package body Dynamic_Model is
kono
parents:
diff changeset
761 3. Val : constant Integer := Server.Func;
kono
parents:
diff changeset
762 |
kono
parents:
diff changeset
763 >>> info: call to "Func" during elaboration
kono
parents:
diff changeset
764 >>> info: missing pragma "Elaborate_All" for unit "Server"
kono
parents:
diff changeset
765
kono
parents:
diff changeset
766 4. end Dynamic_Model;
kono
parents:
diff changeset
767
kono
parents:
diff changeset
768 .. _Static_Elaboration_Model_in_GNAT:
kono
parents:
diff changeset
769
kono
parents:
diff changeset
770 Static Elaboration Model in GNAT
kono
parents:
diff changeset
771 ================================
kono
parents:
diff changeset
772
kono
parents:
diff changeset
773 In contrast to the dynamic model, the static model is more precise in its
kono
parents:
diff changeset
774 analysis of elaboration code. The model makes a clear distinction between
kono
parents:
diff changeset
775 internal and external targets, and resorts to different diagnostics and
kono
parents:
diff changeset
776 run-time checks based on the nature of the target.
kono
parents:
diff changeset
777
kono
parents:
diff changeset
778 * *Internal targets*
kono
parents:
diff changeset
779
kono
parents:
diff changeset
780 The static model performs extensive diagnostics on scenarios which elaborate
kono
parents:
diff changeset
781 or execute internal targets. The warnings resulting from these diagnostics
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
782 are enabled by default, but can be suppressed selectively with ``pragma
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
783 Warnings (Off)`` or globally with compiler switch :switch:`-gnatwL`.
111
kono
parents:
diff changeset
784
kono
parents:
diff changeset
785 ::
kono
parents:
diff changeset
786
kono
parents:
diff changeset
787 1. package body Static_Model is
kono
parents:
diff changeset
788 2. generic
kono
parents:
diff changeset
789 3. with function Func return Integer;
kono
parents:
diff changeset
790 4. package Gen is
kono
parents:
diff changeset
791 5. Val : constant Integer := Func;
kono
parents:
diff changeset
792 6. end Gen;
kono
parents:
diff changeset
793 7.
kono
parents:
diff changeset
794 8. function ABE return Integer;
kono
parents:
diff changeset
795 9.
kono
parents:
diff changeset
796 10. function Cause_ABE return Boolean is
kono
parents:
diff changeset
797 11. package Inst is new Gen (ABE);
kono
parents:
diff changeset
798 |
kono
parents:
diff changeset
799 >>> warning: in instantiation at line 5
kono
parents:
diff changeset
800 >>> warning: cannot call "ABE" before body seen
kono
parents:
diff changeset
801 >>> warning: Program_Error may be raised at run time
kono
parents:
diff changeset
802 >>> warning: body of unit "Static_Model" elaborated
kono
parents:
diff changeset
803 >>> warning: function "Cause_ABE" called at line 16
kono
parents:
diff changeset
804 >>> warning: function "ABE" called at line 5, instance at line 11
kono
parents:
diff changeset
805
kono
parents:
diff changeset
806 12. begin
kono
parents:
diff changeset
807 13. ...
kono
parents:
diff changeset
808 14. end Cause_ABE;
kono
parents:
diff changeset
809 15.
kono
parents:
diff changeset
810 16. Val : constant Boolean := Cause_ABE;
kono
parents:
diff changeset
811 17.
kono
parents:
diff changeset
812 18. function ABE return Integer is
kono
parents:
diff changeset
813 19. begin
kono
parents:
diff changeset
814 20. ...
kono
parents:
diff changeset
815 21. end ABE;
kono
parents:
diff changeset
816 22. end Static_Model;
kono
parents:
diff changeset
817
kono
parents:
diff changeset
818 The example above illustrates an ABE problem within package ``Static_Model``,
kono
parents:
diff changeset
819 which is hidden by several layers of indirection. The elaboration of package
kono
parents:
diff changeset
820 body ``Static_Model`` elaborates the declaration of ``Val``. This invokes
kono
parents:
diff changeset
821 function ``Cause_ABE``, which instantiates generic unit ``Gen`` as ``Inst``.
kono
parents:
diff changeset
822 The elaboration of ``Inst`` invokes function ``ABE``, however the body of
kono
parents:
diff changeset
823 ``ABE`` has not been elaborated yet.
kono
parents:
diff changeset
824
kono
parents:
diff changeset
825 * *External targets*
kono
parents:
diff changeset
826
kono
parents:
diff changeset
827 The static model installs run-time checks to verify the elaboration status
kono
parents:
diff changeset
828 of server targets only when the scenario that elaborates or executes that
kono
parents:
diff changeset
829 target is part of the elaboration code of the client unit. The checks can be
kono
parents:
diff changeset
830 suppressed using pragma ``Suppress (Elaboration_Check)``.
kono
parents:
diff changeset
831
kono
parents:
diff changeset
832 ::
kono
parents:
diff changeset
833
kono
parents:
diff changeset
834 with Server;
kono
parents:
diff changeset
835 package body Static_Model is
kono
parents:
diff changeset
836 generic
kono
parents:
diff changeset
837 with function Func return Integer;
kono
parents:
diff changeset
838 package Gen is
kono
parents:
diff changeset
839 Val : constant Integer := Func;
kono
parents:
diff changeset
840 end Gen;
kono
parents:
diff changeset
841
kono
parents:
diff changeset
842 function Call_Func return Boolean is
kono
parents:
diff changeset
843 <check that the body of Server.Func is elaborated>
kono
parents:
diff changeset
844 package Inst is new Gen (Server.Func);
kono
parents:
diff changeset
845 begin
kono
parents:
diff changeset
846 ...
kono
parents:
diff changeset
847 end Call_Func;
kono
parents:
diff changeset
848
kono
parents:
diff changeset
849 Val : constant Boolean := Call_Func;
kono
parents:
diff changeset
850 end Static_Model;
kono
parents:
diff changeset
851
kono
parents:
diff changeset
852 In the example above, the elaboration of package body ``Static_Model``
kono
parents:
diff changeset
853 elaborates the declaration of ``Val``. This invokes function ``Call_Func``,
kono
parents:
diff changeset
854 which instantiates generic unit ``Gen`` as ``Inst``. The elaboration of
kono
parents:
diff changeset
855 ``Inst`` invokes function ``Server.Func``. Since ``Server.Func`` is an
kono
parents:
diff changeset
856 external target, GNAT installs a run-time check to verify that its body has
kono
parents:
diff changeset
857 been elaborated.
kono
parents:
diff changeset
858
kono
parents:
diff changeset
859 In addition to checks, the static model installs implicit ``Elaborate`` and
kono
parents:
diff changeset
860 ``Elaborate_All`` pragmas to guarantee safe elaboration use of server units.
kono
parents:
diff changeset
861 This information is available when compiler switch :switch:`-gnatel` is in
kono
parents:
diff changeset
862 effect.
kono
parents:
diff changeset
863
kono
parents:
diff changeset
864 ::
kono
parents:
diff changeset
865
kono
parents:
diff changeset
866 1. with Server;
kono
parents:
diff changeset
867 2. package body Static_Model is
kono
parents:
diff changeset
868 3. generic
kono
parents:
diff changeset
869 4. with function Func return Integer;
kono
parents:
diff changeset
870 5. package Gen is
kono
parents:
diff changeset
871 6. Val : constant Integer := Func;
kono
parents:
diff changeset
872 7. end Gen;
kono
parents:
diff changeset
873 8.
kono
parents:
diff changeset
874 9. function Call_Func return Boolean is
kono
parents:
diff changeset
875 10. package Inst is new Gen (Server.Func);
kono
parents:
diff changeset
876 |
kono
parents:
diff changeset
877 >>> info: instantiation of "Gen" during elaboration
kono
parents:
diff changeset
878 >>> info: in instantiation at line 6
kono
parents:
diff changeset
879 >>> info: call to "Func" during elaboration
kono
parents:
diff changeset
880 >>> info: in instantiation at line 6
kono
parents:
diff changeset
881 >>> info: implicit pragma "Elaborate_All" generated for unit "Server"
kono
parents:
diff changeset
882 >>> info: body of unit "Static_Model" elaborated
kono
parents:
diff changeset
883 >>> info: function "Call_Func" called at line 15
kono
parents:
diff changeset
884 >>> info: function "Func" called at line 6, instance at line 10
kono
parents:
diff changeset
885
kono
parents:
diff changeset
886 11. begin
kono
parents:
diff changeset
887 12. ...
kono
parents:
diff changeset
888 13. end Call_Func;
kono
parents:
diff changeset
889 14.
kono
parents:
diff changeset
890 15. Val : constant Boolean := Call_Func;
kono
parents:
diff changeset
891 |
kono
parents:
diff changeset
892 >>> info: call to "Call_Func" during elaboration
kono
parents:
diff changeset
893
kono
parents:
diff changeset
894 16. end Static_Model;
kono
parents:
diff changeset
895
kono
parents:
diff changeset
896 In the example above, the elaboration of package body ``Static_Model``
kono
parents:
diff changeset
897 elaborates the declaration of ``Val``. This invokes function ``Call_Func``,
kono
parents:
diff changeset
898 which instantiates generic unit ``Gen`` as ``Inst``. The elaboration of
kono
parents:
diff changeset
899 ``Inst`` invokes function ``Server.Func``. Since ``Server.Func`` is an
kono
parents:
diff changeset
900 external target, GNAT installs an implicit ``Elaborate_All`` pragma for unit
kono
parents:
diff changeset
901 ``Server``. The pragma guarantees that both the spec and body of ``Server``,
kono
parents:
diff changeset
902 along with any additional dependencies that ``Server`` may require, are
kono
parents:
diff changeset
903 elaborated prior to the body of ``Static_Model``.
kono
parents:
diff changeset
904
kono
parents:
diff changeset
905 .. _SPARK_Elaboration_Model_in_GNAT:
kono
parents:
diff changeset
906
kono
parents:
diff changeset
907 SPARK Elaboration Model in GNAT
kono
parents:
diff changeset
908 ===============================
kono
parents:
diff changeset
909
kono
parents:
diff changeset
910 The SPARK model is identical to the static model in its handling of internal
kono
parents:
diff changeset
911 targets. The SPARK model, however, requires explicit ``Elaborate`` or
kono
parents:
diff changeset
912 ``Elaborate_All`` pragmas to be present in the program when a target is
kono
parents:
diff changeset
913 external, and compiler switch :switch:`-gnatd.v` is in effect.
kono
parents:
diff changeset
914
kono
parents:
diff changeset
915 ::
kono
parents:
diff changeset
916
kono
parents:
diff changeset
917 1. with Server;
kono
parents:
diff changeset
918 2. package body SPARK_Model with SPARK_Mode is
kono
parents:
diff changeset
919 3. Val : constant Integer := Server.Func;
kono
parents:
diff changeset
920 |
kono
parents:
diff changeset
921 >>> call to "Func" during elaboration in SPARK
kono
parents:
diff changeset
922 >>> unit "SPARK_Model" requires pragma "Elaborate_All" for "Server"
kono
parents:
diff changeset
923 >>> body of unit "SPARK_Model" elaborated
kono
parents:
diff changeset
924 >>> function "Func" called at line 3
kono
parents:
diff changeset
925
kono
parents:
diff changeset
926 4. end SPARK_Model;
kono
parents:
diff changeset
927
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
928 Legacy Elaboration Model in GNAT
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
929 ================================
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
930
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
931 The legacy elaboration model is provided for compatibility with code bases
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
932 developed with pre-18.x versions of GNAT. It is similar in functionality to
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
933 the dynamic and static models of post-18.x version of GNAT, but may differ
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
934 in terms of diagnostics and run-time checks. The legacy elaboration model is
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
935 enabled with compiler switch :switch:`-gnatH`.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
936
111
kono
parents:
diff changeset
937 .. _Mixing_Elaboration_Models:
kono
parents:
diff changeset
938
kono
parents:
diff changeset
939 Mixing Elaboration Models
kono
parents:
diff changeset
940 =========================
kono
parents:
diff changeset
941
kono
parents:
diff changeset
942 It is possible to mix units compiled with a different elaboration model,
kono
parents:
diff changeset
943 however the following rules must be observed:
kono
parents:
diff changeset
944
kono
parents:
diff changeset
945 * A client unit compiled with the dynamic model can only |with| a server unit
kono
parents:
diff changeset
946 that meets at least one of the following criteria:
kono
parents:
diff changeset
947
kono
parents:
diff changeset
948 - The server unit is compiled with the dynamic model.
kono
parents:
diff changeset
949
kono
parents:
diff changeset
950 - The server unit is a GNAT implementation unit from the Ada, GNAT,
kono
parents:
diff changeset
951 Interfaces, or System hierarchies.
kono
parents:
diff changeset
952
kono
parents:
diff changeset
953 - The server unit has pragma ``Pure`` or ``Preelaborate``.
kono
parents:
diff changeset
954
kono
parents:
diff changeset
955 - The client unit has an explicit ``Elaborate_All`` pragma for the server
kono
parents:
diff changeset
956 unit.
kono
parents:
diff changeset
957
kono
parents:
diff changeset
958 These rules ensure that elaboration checks are not omitted. If the rules are
kono
parents:
diff changeset
959 violated, the binder emits a warning:
kono
parents:
diff changeset
960
kono
parents:
diff changeset
961 ::
kono
parents:
diff changeset
962
kono
parents:
diff changeset
963 warning: "x.ads" has dynamic elaboration checks and with's
kono
parents:
diff changeset
964 warning: "y.ads" which has static elaboration checks
kono
parents:
diff changeset
965
kono
parents:
diff changeset
966 The warnings can be suppressed by binder switch :switch:`-ws`.
kono
parents:
diff changeset
967
kono
parents:
diff changeset
968 .. _Elaboration_Circularities:
kono
parents:
diff changeset
969
kono
parents:
diff changeset
970 Elaboration Circularities
kono
parents:
diff changeset
971 =========================
kono
parents:
diff changeset
972
kono
parents:
diff changeset
973 If the binder cannot find an acceptable elaboration order, it outputs detailed
kono
parents:
diff changeset
974 diagnostics describing an **elaboration circularity**.
kono
parents:
diff changeset
975
kono
parents:
diff changeset
976 ::
kono
parents:
diff changeset
977
kono
parents:
diff changeset
978 package Server is
kono
parents:
diff changeset
979 function Func return Integer;
kono
parents:
diff changeset
980 end Server;
kono
parents:
diff changeset
981
kono
parents:
diff changeset
982 ::
kono
parents:
diff changeset
983
kono
parents:
diff changeset
984 with Client;
kono
parents:
diff changeset
985 package body Server is
kono
parents:
diff changeset
986 function Func return Integer is
kono
parents:
diff changeset
987 begin
kono
parents:
diff changeset
988 ...
kono
parents:
diff changeset
989 end Func;
kono
parents:
diff changeset
990 end Server;
kono
parents:
diff changeset
991
kono
parents:
diff changeset
992 ::
kono
parents:
diff changeset
993
kono
parents:
diff changeset
994 with Server;
kono
parents:
diff changeset
995 package Client is
kono
parents:
diff changeset
996 Val : constant Integer := Server.Func;
kono
parents:
diff changeset
997 end Client;
kono
parents:
diff changeset
998
kono
parents:
diff changeset
999 ::
kono
parents:
diff changeset
1000
kono
parents:
diff changeset
1001 with Client;
kono
parents:
diff changeset
1002 procedure Main is begin null; end Main;
kono
parents:
diff changeset
1003
kono
parents:
diff changeset
1004 ::
kono
parents:
diff changeset
1005
kono
parents:
diff changeset
1006 error: elaboration circularity detected
kono
parents:
diff changeset
1007 info: "server (body)" must be elaborated before "client (spec)"
kono
parents:
diff changeset
1008 info: reason: implicit Elaborate_All in unit "client (spec)"
kono
parents:
diff changeset
1009 info: recompile "client (spec)" with -gnatel for full details
kono
parents:
diff changeset
1010 info: "server (body)"
kono
parents:
diff changeset
1011 info: must be elaborated along with its spec:
kono
parents:
diff changeset
1012 info: "server (spec)"
kono
parents:
diff changeset
1013 info: which is withed by:
kono
parents:
diff changeset
1014 info: "client (spec)"
kono
parents:
diff changeset
1015 info: "client (spec)" must be elaborated before "server (body)"
kono
parents:
diff changeset
1016 info: reason: with clause
kono
parents:
diff changeset
1017
kono
parents:
diff changeset
1018 In the example above, ``Client`` must be elaborated prior to ``Main`` by virtue
kono
parents:
diff changeset
1019 of a |with| clause. The elaboration of ``Client`` invokes ``Server.Func``, and
kono
parents:
diff changeset
1020 static model generates an implicit ``Elaborate_All`` pragma for ``Server``. The
kono
parents:
diff changeset
1021 pragma implies that both the spec and body of ``Server``, along with any units
kono
parents:
diff changeset
1022 they |with|, must be elaborated prior to ``Client``. However, ``Server``'s body
kono
parents:
diff changeset
1023 |withs| ``Client``, implying that ``Client`` must be elaborated prior to
kono
parents:
diff changeset
1024 ``Server``. The end result is that ``Client`` must be elaborated prior to
kono
parents:
diff changeset
1025 ``Client``, and this leads to a circularity.
kono
parents:
diff changeset
1026
kono
parents:
diff changeset
1027 .. _Resolving_Elaboration_Circularities:
kono
parents:
diff changeset
1028
kono
parents:
diff changeset
1029 Resolving Elaboration Circularities
kono
parents:
diff changeset
1030 ===================================
kono
parents:
diff changeset
1031
kono
parents:
diff changeset
1032 When faced with an elaboration circularity, a programmer has several options
kono
parents:
diff changeset
1033 available.
kono
parents:
diff changeset
1034
kono
parents:
diff changeset
1035 * *Fix the program*
kono
parents:
diff changeset
1036
kono
parents:
diff changeset
1037 The most desirable option from the point of view of long-term maintenance
kono
parents:
diff changeset
1038 is to rearrange the program so that the elaboration problems are avoided.
kono
parents:
diff changeset
1039 One useful technique is to place the elaboration code into separate child
kono
parents:
diff changeset
1040 packages. Another is to move some of the initialization code to explicitly
kono
parents:
diff changeset
1041 invoked subprograms, where the program controls the order of initialization
kono
parents:
diff changeset
1042 explicitly. Although this is the most desirable option, it may be impractical
kono
parents:
diff changeset
1043 and involve too much modification, especially in the case of complex legacy
kono
parents:
diff changeset
1044 code.
kono
parents:
diff changeset
1045
kono
parents:
diff changeset
1046 * *Switch to more permissive elaboration model*
kono
parents:
diff changeset
1047
kono
parents:
diff changeset
1048 If the compilation was performed using the static model, enable the dynamic
kono
parents:
diff changeset
1049 model with compiler switch :switch:`-gnatE`. GNAT will no longer generate
kono
parents:
diff changeset
1050 implicit ``Elaborate`` and ``Elaborate_All`` pragmas, resulting in a behavior
kono
parents:
diff changeset
1051 identical to that specified by the Ada Reference Manual. The binder will
kono
parents:
diff changeset
1052 generate an executable program that may or may not raise ``Program_Error``,
kono
parents:
diff changeset
1053 and it is the programmer's responsibility to ensure that it does not raise
kono
parents:
diff changeset
1054 ``Program_Error``.
kono
parents:
diff changeset
1055
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1056 If the compilation was performed using a post-18.x version of GNAT, consider
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1057 using the legacy elaboration model, in the following order:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1058
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1059 - Use the legacy static elaboration model, with compiler switch
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1060 :switch:`-gnatH`.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1061
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1062 - Use the legacy dynamic elaboration model, with compiler switches
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1063 :switch:`-gnatH` :switch:`-gnatE`.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1064
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1065 - Use the relaxed legacy static elaboration model, with compiler switches
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1066 :switch:`-gnatH` :switch:`-gnatJ`.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1067
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1068 - Use the relaxed legacy dynamic elaboration model, with compiler switches
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1069 :switch:`-gnatH` :switch:`-gnatJ` :switch:`-gnatE`.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1070
111
kono
parents:
diff changeset
1071 * *Suppress all elaboration checks*
kono
parents:
diff changeset
1072
kono
parents:
diff changeset
1073 The drawback of run-time checks is that they generate overhead at run time,
kono
parents:
diff changeset
1074 both in space and time. If the programmer is absolutely sure that a program
kono
parents:
diff changeset
1075 will not raise an elaboration-related ``Program_Error``, then using the
kono
parents:
diff changeset
1076 pragma ``Suppress (Elaboration_Check)`` globally (as a configuration pragma)
kono
parents:
diff changeset
1077 will eliminate all run-time checks.
kono
parents:
diff changeset
1078
kono
parents:
diff changeset
1079 * *Suppress elaboration checks selectively*
kono
parents:
diff changeset
1080
kono
parents:
diff changeset
1081 If a scenario cannot possibly lead to an elaboration ``Program_Error``,
kono
parents:
diff changeset
1082 and the binder nevertheless complains about implicit ``Elaborate`` and
kono
parents:
diff changeset
1083 ``Elaborate_All`` pragmas that lead to elaboration circularities, it
kono
parents:
diff changeset
1084 is possible to suppress the generation of implicit ``Elaborate`` and
kono
parents:
diff changeset
1085 ``Elaborate_All`` pragmas, as well as run-time checks. Clearly this can
kono
parents:
diff changeset
1086 be unsafe, and it is the responsibility of the programmer to make sure
kono
parents:
diff changeset
1087 that the resulting program has no elaboration anomalies. Pragma
kono
parents:
diff changeset
1088 ``Suppress (Elaboration_Check)`` can be used with different levels of
kono
parents:
diff changeset
1089 granularity to achieve these effects.
kono
parents:
diff changeset
1090
kono
parents:
diff changeset
1091 - *Target suppression*
kono
parents:
diff changeset
1092
kono
parents:
diff changeset
1093 When the pragma is placed in a declarative part, without a second argument
kono
parents:
diff changeset
1094 naming an entity, it will suppress implicit ``Elaborate`` and
kono
parents:
diff changeset
1095 ``Elaborate_All`` pragma generation, as well as run-time checks, on all
kono
parents:
diff changeset
1096 targets within the region.
kono
parents:
diff changeset
1097
kono
parents:
diff changeset
1098 ::
kono
parents:
diff changeset
1099
kono
parents:
diff changeset
1100 package Range_Suppress is
kono
parents:
diff changeset
1101 pragma Suppress (Elaboration_Check);
kono
parents:
diff changeset
1102
kono
parents:
diff changeset
1103 function Func return Integer;
kono
parents:
diff changeset
1104
kono
parents:
diff changeset
1105 generic
kono
parents:
diff changeset
1106 procedure Gen;
kono
parents:
diff changeset
1107
kono
parents:
diff changeset
1108 pragma Unsuppress (Elaboration_Check);
kono
parents:
diff changeset
1109
kono
parents:
diff changeset
1110 task type Tsk;
kono
parents:
diff changeset
1111 end Range_Suppress;
kono
parents:
diff changeset
1112
kono
parents:
diff changeset
1113 In the example above, a pair of Suppress/Unsuppress pragmas define a region
kono
parents:
diff changeset
1114 of suppression within package ``Range_Suppress``. As a result, no implicit
kono
parents:
diff changeset
1115 ``Elaborate`` and ``Elaborate_All`` pragmas, nor any run-time checks, will
kono
parents:
diff changeset
1116 be generated by callers of ``Func`` and instantiators of ``Gen``. Note that
kono
parents:
diff changeset
1117 task type ``Tsk`` is not within this region.
kono
parents:
diff changeset
1118
kono
parents:
diff changeset
1119 An alternative to the region-based suppression is to use multiple
kono
parents:
diff changeset
1120 ``Suppress`` pragmas with arguments naming specific entities for which
kono
parents:
diff changeset
1121 elaboration checks should be suppressed:
kono
parents:
diff changeset
1122
kono
parents:
diff changeset
1123 ::
kono
parents:
diff changeset
1124
kono
parents:
diff changeset
1125 package Range_Suppress is
kono
parents:
diff changeset
1126 function Func return Integer;
kono
parents:
diff changeset
1127 pragma Suppress (Elaboration_Check, Func);
kono
parents:
diff changeset
1128
kono
parents:
diff changeset
1129 generic
kono
parents:
diff changeset
1130 procedure Gen;
kono
parents:
diff changeset
1131 pragma Suppress (Elaboration_Check, Gen);
kono
parents:
diff changeset
1132
kono
parents:
diff changeset
1133 task type Tsk;
kono
parents:
diff changeset
1134 end Range_Suppress;
kono
parents:
diff changeset
1135
kono
parents:
diff changeset
1136 - *Scenario suppression*
kono
parents:
diff changeset
1137
kono
parents:
diff changeset
1138 When the pragma ``Suppress`` is placed in a declarative or statement
kono
parents:
diff changeset
1139 part, without an entity argument, it will suppress implicit ``Elaborate``
kono
parents:
diff changeset
1140 and ``Elaborate_All`` pragma generation, as well as run-time checks, on
kono
parents:
diff changeset
1141 all scenarios within the region.
kono
parents:
diff changeset
1142
kono
parents:
diff changeset
1143 ::
kono
parents:
diff changeset
1144
kono
parents:
diff changeset
1145 with Server;
kono
parents:
diff changeset
1146 package body Range_Suppress is
kono
parents:
diff changeset
1147 pragma Suppress (Elaboration_Check);
kono
parents:
diff changeset
1148
kono
parents:
diff changeset
1149 function Func return Integer is
kono
parents:
diff changeset
1150 begin
kono
parents:
diff changeset
1151 return Server.Func;
kono
parents:
diff changeset
1152 end Func;
kono
parents:
diff changeset
1153
kono
parents:
diff changeset
1154 procedure Gen is
kono
parents:
diff changeset
1155 begin
kono
parents:
diff changeset
1156 Server.Proc;
kono
parents:
diff changeset
1157 end Gen;
kono
parents:
diff changeset
1158
kono
parents:
diff changeset
1159 pragma Unsuppress (Elaboration_Check);
kono
parents:
diff changeset
1160
kono
parents:
diff changeset
1161 task body Tsk is
kono
parents:
diff changeset
1162 begin
kono
parents:
diff changeset
1163 Server.Proc;
kono
parents:
diff changeset
1164 end Tsk;
kono
parents:
diff changeset
1165 end Range_Suppress;
kono
parents:
diff changeset
1166
kono
parents:
diff changeset
1167 In the example above, a pair of Suppress/Unsuppress pragmas define a region
kono
parents:
diff changeset
1168 of suppression within package body ``Range_Suppress``. As a result, the
kono
parents:
diff changeset
1169 calls to ``Server.Func`` in ``Func`` and ``Server.Proc`` in ``Gen`` will
kono
parents:
diff changeset
1170 not generate any implicit ``Elaborate`` and ``Elaborate_All`` pragmas or
kono
parents:
diff changeset
1171 run-time checks.
kono
parents:
diff changeset
1172
kono
parents:
diff changeset
1173 .. _Resolving_Task_Issues:
kono
parents:
diff changeset
1174
kono
parents:
diff changeset
1175 Resolving Task Issues
kono
parents:
diff changeset
1176 =====================
kono
parents:
diff changeset
1177
kono
parents:
diff changeset
1178 The model of execution in Ada dictates that elaboration must first take place,
kono
parents:
diff changeset
1179 and only then can the main program be started. Tasks which are activated during
kono
parents:
diff changeset
1180 elaboration violate this model and may lead to serious concurrent problems at
kono
parents:
diff changeset
1181 elaboration time.
kono
parents:
diff changeset
1182
kono
parents:
diff changeset
1183 A task can be activated in two different ways:
kono
parents:
diff changeset
1184
kono
parents:
diff changeset
1185 * The task is created by an allocator in which case it is activated immediately
kono
parents:
diff changeset
1186 after the allocator is evaluated.
kono
parents:
diff changeset
1187
kono
parents:
diff changeset
1188 * The task is declared at the library level or within some nested master in
kono
parents:
diff changeset
1189 which case it is activated before starting execution of the statement
kono
parents:
diff changeset
1190 sequence of the master defining the task.
kono
parents:
diff changeset
1191
kono
parents:
diff changeset
1192 Since the elaboration of a partition is performed by the environment task
kono
parents:
diff changeset
1193 servicing that partition, any tasks activated during elaboration may be in
kono
parents:
diff changeset
1194 a race with the environment task, and lead to unpredictable state and behavior.
kono
parents:
diff changeset
1195 The static model seeks to avoid such interactions by assuming that all code in
kono
parents:
diff changeset
1196 the task body is executed at elaboration time, if the task was activated by
kono
parents:
diff changeset
1197 elaboration code.
kono
parents:
diff changeset
1198
kono
parents:
diff changeset
1199 ::
kono
parents:
diff changeset
1200
kono
parents:
diff changeset
1201 package Decls is
kono
parents:
diff changeset
1202 task Lib_Task is
kono
parents:
diff changeset
1203 entry Start;
kono
parents:
diff changeset
1204 end Lib_Task;
kono
parents:
diff changeset
1205
kono
parents:
diff changeset
1206 type My_Int is new Integer;
kono
parents:
diff changeset
1207
kono
parents:
diff changeset
1208 function Ident (M : My_Int) return My_Int;
kono
parents:
diff changeset
1209 end Decls;
kono
parents:
diff changeset
1210
kono
parents:
diff changeset
1211 ::
kono
parents:
diff changeset
1212
kono
parents:
diff changeset
1213 with Utils;
kono
parents:
diff changeset
1214 package body Decls is
kono
parents:
diff changeset
1215 task body Lib_Task is
kono
parents:
diff changeset
1216 begin
kono
parents:
diff changeset
1217 accept Start;
kono
parents:
diff changeset
1218 Utils.Put_Val (2);
kono
parents:
diff changeset
1219 end Lib_Task;
kono
parents:
diff changeset
1220
kono
parents:
diff changeset
1221 function Ident (M : My_Int) return My_Int is
kono
parents:
diff changeset
1222 begin
kono
parents:
diff changeset
1223 return M;
kono
parents:
diff changeset
1224 end Ident;
kono
parents:
diff changeset
1225 end Decls;
kono
parents:
diff changeset
1226
kono
parents:
diff changeset
1227 ::
kono
parents:
diff changeset
1228
kono
parents:
diff changeset
1229 with Decls;
kono
parents:
diff changeset
1230 package Utils is
kono
parents:
diff changeset
1231 procedure Put_Val (Arg : Decls.My_Int);
kono
parents:
diff changeset
1232 end Utils;
kono
parents:
diff changeset
1233
kono
parents:
diff changeset
1234 ::
kono
parents:
diff changeset
1235
kono
parents:
diff changeset
1236 with Ada.Text_IO; use Ada.Text_IO;
kono
parents:
diff changeset
1237 package body Utils is
kono
parents:
diff changeset
1238 procedure Put_Val (Arg : Decls.My_Int) is
kono
parents:
diff changeset
1239 begin
kono
parents:
diff changeset
1240 Put_Line (Arg'Img);
kono
parents:
diff changeset
1241 end Put_Val;
kono
parents:
diff changeset
1242 end Utils;
kono
parents:
diff changeset
1243
kono
parents:
diff changeset
1244 ::
kono
parents:
diff changeset
1245
kono
parents:
diff changeset
1246 with Decls;
kono
parents:
diff changeset
1247 procedure Main is
kono
parents:
diff changeset
1248 begin
kono
parents:
diff changeset
1249 Decls.Lib_Task.Start;
kono
parents:
diff changeset
1250 end Main;
kono
parents:
diff changeset
1251
kono
parents:
diff changeset
1252 When the above example is compiled with the static model, an elaboration
kono
parents:
diff changeset
1253 circularity arises:
kono
parents:
diff changeset
1254
kono
parents:
diff changeset
1255 ::
kono
parents:
diff changeset
1256
kono
parents:
diff changeset
1257 error: elaboration circularity detected
kono
parents:
diff changeset
1258 info: "decls (body)" must be elaborated before "decls (body)"
kono
parents:
diff changeset
1259 info: reason: implicit Elaborate_All in unit "decls (body)"
kono
parents:
diff changeset
1260 info: recompile "decls (body)" with -gnatel for full details
kono
parents:
diff changeset
1261 info: "decls (body)"
kono
parents:
diff changeset
1262 info: must be elaborated along with its spec:
kono
parents:
diff changeset
1263 info: "decls (spec)"
kono
parents:
diff changeset
1264 info: which is withed by:
kono
parents:
diff changeset
1265 info: "utils (spec)"
kono
parents:
diff changeset
1266 info: which is withed by:
kono
parents:
diff changeset
1267 info: "decls (body)"
kono
parents:
diff changeset
1268
kono
parents:
diff changeset
1269 In the above example, ``Decls`` must be elaborated prior to ``Main`` by virtue
kono
parents:
diff changeset
1270 of a with clause. The elaboration of ``Decls`` activates task ``Lib_Task``. The
kono
parents:
diff changeset
1271 static model conservatibely assumes that all code within the body of
kono
parents:
diff changeset
1272 ``Lib_Task`` is executed, and generates an implicit ``Elaborate_All`` pragma
kono
parents:
diff changeset
1273 for ``Units`` due to the call to ``Utils.Put_Val``. The pragma implies that
kono
parents:
diff changeset
1274 both the spec and body of ``Utils``, along with any units they |with|,
kono
parents:
diff changeset
1275 must be elaborated prior to ``Decls``. However, ``Utils``'s spec |withs|
kono
parents:
diff changeset
1276 ``Decls``, implying that ``Decls`` must be elaborated before ``Utils``. The end
kono
parents:
diff changeset
1277 result is that ``Utils`` must be elaborated prior to ``Utils``, and this
kono
parents:
diff changeset
1278 leads to a circularity.
kono
parents:
diff changeset
1279
kono
parents:
diff changeset
1280 In reality, the example above will not exhibit an ABE problem at run time.
kono
parents:
diff changeset
1281 When the body of task ``Lib_Task`` is activated, execution will wait for entry
kono
parents:
diff changeset
1282 ``Start`` to be accepted, and the call to ``Utils.Put_Val`` will not take place
kono
parents:
diff changeset
1283 at elaboration time. Task ``Lib_Task`` will resume its execution after the main
kono
parents:
diff changeset
1284 program is executed because ``Main`` performs a rendezvous with
kono
parents:
diff changeset
1285 ``Lib_Task.Start``, and at that point all units have already been elaborated.
kono
parents:
diff changeset
1286 As a result, the static model may seem overly conservative, partly because it
kono
parents:
diff changeset
1287 does not take control and data flow into account.
kono
parents:
diff changeset
1288
kono
parents:
diff changeset
1289 When faced with a task elaboration circularity, a programmer has several
kono
parents:
diff changeset
1290 options available:
kono
parents:
diff changeset
1291
kono
parents:
diff changeset
1292 * *Use the dynamic model*
kono
parents:
diff changeset
1293
kono
parents:
diff changeset
1294 The dynamic model does not generate implicit ``Elaborate`` and
kono
parents:
diff changeset
1295 ``Elaborate_All`` pragmas. Instead, it will install checks prior to every
kono
parents:
diff changeset
1296 call in the example above, thus verifying the successful elaboration of
kono
parents:
diff changeset
1297 ``Utils.Put_Val`` in case the call to it takes place at elaboration time.
kono
parents:
diff changeset
1298 The dynamic model is enabled with compiler switch :switch:`-gnatE`.
kono
parents:
diff changeset
1299
kono
parents:
diff changeset
1300 * *Isolate the tasks*
kono
parents:
diff changeset
1301
kono
parents:
diff changeset
1302 Relocating tasks in their own separate package could decouple them from
kono
parents:
diff changeset
1303 dependencies that would otherwise cause an elaboration circularity. The
kono
parents:
diff changeset
1304 example above can be rewritten as follows:
kono
parents:
diff changeset
1305
kono
parents:
diff changeset
1306 ::
kono
parents:
diff changeset
1307
kono
parents:
diff changeset
1308 package Decls1 is -- new
kono
parents:
diff changeset
1309 task Lib_Task is
kono
parents:
diff changeset
1310 entry Start;
kono
parents:
diff changeset
1311 end Lib_Task;
kono
parents:
diff changeset
1312 end Decls1;
kono
parents:
diff changeset
1313
kono
parents:
diff changeset
1314 ::
kono
parents:
diff changeset
1315
kono
parents:
diff changeset
1316 with Utils;
kono
parents:
diff changeset
1317 package body Decls1 is -- new
kono
parents:
diff changeset
1318 task body Lib_Task is
kono
parents:
diff changeset
1319 begin
kono
parents:
diff changeset
1320 accept Start;
kono
parents:
diff changeset
1321 Utils.Put_Val (2);
kono
parents:
diff changeset
1322 end Lib_Task;
kono
parents:
diff changeset
1323 end Decls1;
kono
parents:
diff changeset
1324
kono
parents:
diff changeset
1325 ::
kono
parents:
diff changeset
1326
kono
parents:
diff changeset
1327 package Decls2 is -- new
kono
parents:
diff changeset
1328 type My_Int is new Integer;
kono
parents:
diff changeset
1329 function Ident (M : My_Int) return My_Int;
kono
parents:
diff changeset
1330 end Decls2;
kono
parents:
diff changeset
1331
kono
parents:
diff changeset
1332 ::
kono
parents:
diff changeset
1333
kono
parents:
diff changeset
1334 with Utils;
kono
parents:
diff changeset
1335 package body Decls2 is -- new
kono
parents:
diff changeset
1336 function Ident (M : My_Int) return My_Int is
kono
parents:
diff changeset
1337 begin
kono
parents:
diff changeset
1338 return M;
kono
parents:
diff changeset
1339 end Ident;
kono
parents:
diff changeset
1340 end Decls2;
kono
parents:
diff changeset
1341
kono
parents:
diff changeset
1342 ::
kono
parents:
diff changeset
1343
kono
parents:
diff changeset
1344 with Decls2;
kono
parents:
diff changeset
1345 package Utils is
kono
parents:
diff changeset
1346 procedure Put_Val (Arg : Decls2.My_Int);
kono
parents:
diff changeset
1347 end Utils;
kono
parents:
diff changeset
1348
kono
parents:
diff changeset
1349 ::
kono
parents:
diff changeset
1350
kono
parents:
diff changeset
1351 with Ada.Text_IO; use Ada.Text_IO;
kono
parents:
diff changeset
1352 package body Utils is
kono
parents:
diff changeset
1353 procedure Put_Val (Arg : Decls2.My_Int) is
kono
parents:
diff changeset
1354 begin
kono
parents:
diff changeset
1355 Put_Line (Arg'Img);
kono
parents:
diff changeset
1356 end Put_Val;
kono
parents:
diff changeset
1357 end Utils;
kono
parents:
diff changeset
1358
kono
parents:
diff changeset
1359 ::
kono
parents:
diff changeset
1360
kono
parents:
diff changeset
1361 with Decls1;
kono
parents:
diff changeset
1362 procedure Main is
kono
parents:
diff changeset
1363 begin
kono
parents:
diff changeset
1364 Decls1.Lib_Task.Start;
kono
parents:
diff changeset
1365 end Main;
kono
parents:
diff changeset
1366
kono
parents:
diff changeset
1367 * *Declare the tasks*
kono
parents:
diff changeset
1368
kono
parents:
diff changeset
1369 The original example uses a single task declaration for ``Lib_Task``. An
kono
parents:
diff changeset
1370 explicit task type declaration and a properly placed task object could avoid
kono
parents:
diff changeset
1371 the dependencies that would otherwise cause an elaboration circularity. The
kono
parents:
diff changeset
1372 example can be rewritten as follows:
kono
parents:
diff changeset
1373
kono
parents:
diff changeset
1374 ::
kono
parents:
diff changeset
1375
kono
parents:
diff changeset
1376 package Decls is
kono
parents:
diff changeset
1377 task type Lib_Task is -- new
kono
parents:
diff changeset
1378 entry Start;
kono
parents:
diff changeset
1379 end Lib_Task;
kono
parents:
diff changeset
1380
kono
parents:
diff changeset
1381 type My_Int is new Integer;
kono
parents:
diff changeset
1382
kono
parents:
diff changeset
1383 function Ident (M : My_Int) return My_Int;
kono
parents:
diff changeset
1384 end Decls;
kono
parents:
diff changeset
1385
kono
parents:
diff changeset
1386 ::
kono
parents:
diff changeset
1387
kono
parents:
diff changeset
1388 with Utils;
kono
parents:
diff changeset
1389 package body Decls is
kono
parents:
diff changeset
1390 task body Lib_Task is
kono
parents:
diff changeset
1391 begin
kono
parents:
diff changeset
1392 accept Start;
kono
parents:
diff changeset
1393 Utils.Put_Val (2);
kono
parents:
diff changeset
1394 end Lib_Task;
kono
parents:
diff changeset
1395
kono
parents:
diff changeset
1396 function Ident (M : My_Int) return My_Int is
kono
parents:
diff changeset
1397 begin
kono
parents:
diff changeset
1398 return M;
kono
parents:
diff changeset
1399 end Ident;
kono
parents:
diff changeset
1400 end Decls;
kono
parents:
diff changeset
1401
kono
parents:
diff changeset
1402 ::
kono
parents:
diff changeset
1403
kono
parents:
diff changeset
1404 with Decls;
kono
parents:
diff changeset
1405 package Utils is
kono
parents:
diff changeset
1406 procedure Put_Val (Arg : Decls.My_Int);
kono
parents:
diff changeset
1407 end Utils;
kono
parents:
diff changeset
1408
kono
parents:
diff changeset
1409 ::
kono
parents:
diff changeset
1410
kono
parents:
diff changeset
1411 with Ada.Text_IO; use Ada.Text_IO;
kono
parents:
diff changeset
1412 package body Utils is
kono
parents:
diff changeset
1413 procedure Put_Val (Arg : Decls.My_Int) is
kono
parents:
diff changeset
1414 begin
kono
parents:
diff changeset
1415 Put_Line (Arg'Img);
kono
parents:
diff changeset
1416 end Put_Val;
kono
parents:
diff changeset
1417 end Utils;
kono
parents:
diff changeset
1418
kono
parents:
diff changeset
1419 ::
kono
parents:
diff changeset
1420
kono
parents:
diff changeset
1421 with Decls;
kono
parents:
diff changeset
1422 package Obj_Decls is -- new
kono
parents:
diff changeset
1423 Task_Obj : Decls.Lib_Task;
kono
parents:
diff changeset
1424 end Obj_Decls;
kono
parents:
diff changeset
1425
kono
parents:
diff changeset
1426 ::
kono
parents:
diff changeset
1427
kono
parents:
diff changeset
1428 with Obj_Decls;
kono
parents:
diff changeset
1429 procedure Main is
kono
parents:
diff changeset
1430 begin
kono
parents:
diff changeset
1431 Obj_Decls.Task_Obj.Start; -- new
kono
parents:
diff changeset
1432 end Main;
kono
parents:
diff changeset
1433
kono
parents:
diff changeset
1434 * *Use restriction No_Entry_Calls_In_Elaboration_Code*
kono
parents:
diff changeset
1435
kono
parents:
diff changeset
1436 The issue exhibited in the original example under this section revolves
kono
parents:
diff changeset
1437 around the body of ``Lib_Task`` blocking on an accept statement. There is
kono
parents:
diff changeset
1438 no rule to prevent elaboration code from performing entry calls, however in
kono
parents:
diff changeset
1439 practice this is highly unusual. In addition, the pattern of starting tasks
kono
parents:
diff changeset
1440 at elaboration time and then immediately blocking on accept or select
kono
parents:
diff changeset
1441 statements is quite common.
kono
parents:
diff changeset
1442
kono
parents:
diff changeset
1443 If a programmer knows that elaboration code will not perform any entry
kono
parents:
diff changeset
1444 calls, then the programmer can indicate that the static model should not
kono
parents:
diff changeset
1445 process the remainder of a task body once an accept or select statement has
kono
parents:
diff changeset
1446 been encountered. This behavior can be specified by a configuration pragma:
kono
parents:
diff changeset
1447
kono
parents:
diff changeset
1448 ::
kono
parents:
diff changeset
1449
kono
parents:
diff changeset
1450 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
kono
parents:
diff changeset
1451
kono
parents:
diff changeset
1452 In addition to the change in behavior with respect to task bodies, the
kono
parents:
diff changeset
1453 static model will verify that no entry calls take place at elaboration time.
kono
parents:
diff changeset
1454
kono
parents:
diff changeset
1455 .. _Elaboration_Related_Compiler_Switches:
kono
parents:
diff changeset
1456
kono
parents:
diff changeset
1457 Elaboration-related Compiler Switches
kono
parents:
diff changeset
1458 =====================================
kono
parents:
diff changeset
1459
kono
parents:
diff changeset
1460 GNAT has several switches that affect the elaboration model and consequently
kono
parents:
diff changeset
1461 the elaboration order chosen by the binder.
kono
parents:
diff changeset
1462
kono
parents:
diff changeset
1463 .. index:: -gnatE (gnat)
kono
parents:
diff changeset
1464
kono
parents:
diff changeset
1465 :switch:`-gnatE`
kono
parents:
diff changeset
1466 Dynamic elaboration checking mode enabled
kono
parents:
diff changeset
1467
kono
parents:
diff changeset
1468 When this switch is in effect, GNAT activates the dynamic elaboration model.
kono
parents:
diff changeset
1469
kono
parents:
diff changeset
1470 .. index:: -gnatel (gnat)
kono
parents:
diff changeset
1471
kono
parents:
diff changeset
1472 :switch:`-gnatel`
kono
parents:
diff changeset
1473 Turn on info messages on generated Elaborate[_All] pragmas
kono
parents:
diff changeset
1474
kono
parents:
diff changeset
1475 When this switch is in effect, GNAT will emit the following supplementary
kono
parents:
diff changeset
1476 information depending on the elaboration model in effect.
kono
parents:
diff changeset
1477
kono
parents:
diff changeset
1478 - *Dynamic model*
kono
parents:
diff changeset
1479
kono
parents:
diff changeset
1480 GNAT will indicate missing ``Elaborate`` and ``Elaborate_All`` pragmas for
kono
parents:
diff changeset
1481 all library-level scenarios within the partition.
kono
parents:
diff changeset
1482
kono
parents:
diff changeset
1483 - *Static model*
kono
parents:
diff changeset
1484
kono
parents:
diff changeset
1485 GNAT will indicate all scenarios executed during elaboration. In addition,
kono
parents:
diff changeset
1486 it will provide detailed traceback when an implicit ``Elaborate`` or
kono
parents:
diff changeset
1487 ``Elaborate_All`` pragma is generated.
kono
parents:
diff changeset
1488
kono
parents:
diff changeset
1489 - *SPARK model*
kono
parents:
diff changeset
1490
kono
parents:
diff changeset
1491 GNAT will indicate how an elaboration requirement is met by the context of
kono
parents:
diff changeset
1492 a unit. This diagnostic requires compiler switch :switch:`-gnatd.v`.
kono
parents:
diff changeset
1493
kono
parents:
diff changeset
1494 ::
kono
parents:
diff changeset
1495
kono
parents:
diff changeset
1496 1. with Server; pragma Elaborate_All (Server);
kono
parents:
diff changeset
1497 2. package Client with SPARK_Mode is
kono
parents:
diff changeset
1498 3. Val : constant Integer := Server.Func;
kono
parents:
diff changeset
1499 |
kono
parents:
diff changeset
1500 >>> info: call to "Func" during elaboration in SPARK
kono
parents:
diff changeset
1501 >>> info: "Elaborate_All" requirement for unit "Server" met by pragma at line 1
kono
parents:
diff changeset
1502
kono
parents:
diff changeset
1503 4. end Client;
kono
parents:
diff changeset
1504
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1505 .. index:: -gnatH (gnat)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1506
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1507 :switch:`-gnatH`
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1508 Legacy elaboration checking mode enabled
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1509
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1510 When this switch is in effect, GNAT will utilize the pre-18.x elaboration
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1511 model.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1512
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1513 .. index:: -gnatJ (gnat)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1514
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1515 :switch:`-gnatJ`
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1516 Relaxed elaboration checking mode enabled
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1517
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1518 When this switch is in effect, GNAT will not process certain scenarios,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1519 resulting in a more permissive elaboration model. Note that this may
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1520 eliminate some diagnostics and run-time checks.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1521
111
kono
parents:
diff changeset
1522 .. index:: -gnatw.f (gnat)
kono
parents:
diff changeset
1523
kono
parents:
diff changeset
1524 :switch:`-gnatw.f`
kono
parents:
diff changeset
1525 Turn on warnings for suspicious Subp'Access
kono
parents:
diff changeset
1526
kono
parents:
diff changeset
1527 When this switch is in effect, GNAT will treat ``'Access`` of an entry,
kono
parents:
diff changeset
1528 operator, or subprogram as a potential call to the target and issue warnings:
kono
parents:
diff changeset
1529
kono
parents:
diff changeset
1530 ::
kono
parents:
diff changeset
1531
kono
parents:
diff changeset
1532 1. package body Attribute_Call is
kono
parents:
diff changeset
1533 2. function Func return Integer;
kono
parents:
diff changeset
1534 3. type Func_Ptr is access function return Integer;
kono
parents:
diff changeset
1535 4.
kono
parents:
diff changeset
1536 5. Ptr : constant Func_Ptr := Func'Access;
kono
parents:
diff changeset
1537 |
kono
parents:
diff changeset
1538 >>> warning: "Access" attribute of "Func" before body seen
kono
parents:
diff changeset
1539 >>> warning: possible Program_Error on later references
kono
parents:
diff changeset
1540 >>> warning: body of unit "Attribute_Call" elaborated
kono
parents:
diff changeset
1541 >>> warning: "Access" of "Func" taken at line 5
kono
parents:
diff changeset
1542
kono
parents:
diff changeset
1543 6.
kono
parents:
diff changeset
1544 7. function Func return Integer is
kono
parents:
diff changeset
1545 8. begin
kono
parents:
diff changeset
1546 9. ...
kono
parents:
diff changeset
1547 10. end Func;
kono
parents:
diff changeset
1548 11. end Attribute_Call;
kono
parents:
diff changeset
1549
kono
parents:
diff changeset
1550 In the example above, the elaboration of declaration ``Ptr`` is assigned
kono
parents:
diff changeset
1551 ``Func'Access`` before the body of ``Func`` has been elaborated.
kono
parents:
diff changeset
1552
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1553 .. index:: -gnatwl (gnat)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1554
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1555 :switch:`-gnatwl`
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1556 Turn on warnings for elaboration problems
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1557
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1558 When this switch is in effect, GNAT emits diagnostics in the form of warnings
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1559 concerning various elaboration problems. The warnings are enabled by default.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1560 The switch is provided in case all warnings are suppressed, but elaboration
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1561 warnings are still desired.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1562
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1563 :switch:`-gnatwL`
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1564 Turn off warnings for elaboration problems
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1565
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1566 When this switch is in effect, GNAT no longer emits any diagnostics in the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1567 form of warnings. Selective suppression of elaboration problems is possible
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1568 using ``pragma Warnings (Off)``.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1569
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1570 ::
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1571
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1572 1. package body Selective_Suppression is
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1573 2. function ABE return Integer;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1574 3.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1575 4. Val_1 : constant Integer := ABE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1576 |
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1577 >>> warning: cannot call "ABE" before body seen
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1578 >>> warning: Program_Error will be raised at run time
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1579
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1580 5.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1581 6. pragma Warnings (Off);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1582 7. Val_2 : constant Integer := ABE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1583 8. pragma Warnings (On);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1584 9.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1585 10. function ABE return Integer is
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1586 11. begin
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1587 12. ...
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1588 13. end ABE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1589 14. end Selective_Suppression;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1590
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1591 Note that suppressing elaboration warnings does not eliminate run-time
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1592 checks. The example above will still fail at run time with an ABE.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1593
111
kono
parents:
diff changeset
1594 .. _Summary_of_Procedures_for_Elaboration_Control:
kono
parents:
diff changeset
1595
kono
parents:
diff changeset
1596 Summary of Procedures for Elaboration Control
kono
parents:
diff changeset
1597 =============================================
kono
parents:
diff changeset
1598
kono
parents:
diff changeset
1599 A programmer should first compile the program with the default options, using
kono
parents:
diff changeset
1600 none of the binder or compiler switches. If the binder succeeds in finding an
kono
parents:
diff changeset
1601 elaboration order, then apart from possible cases involing dispatching calls
kono
parents:
diff changeset
1602 and access-to-subprogram types, the program is free of elaboration errors.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1603
111
kono
parents:
diff changeset
1604 If it is important for the program to be portable to compilers other than GNAT,
kono
parents:
diff changeset
1605 then the programmer should use compiler switch :switch:`-gnatel` and consider
kono
parents:
diff changeset
1606 the messages about missing or implicitly created ``Elaborate`` and
kono
parents:
diff changeset
1607 ``Elaborate_All`` pragmas.
kono
parents:
diff changeset
1608
kono
parents:
diff changeset
1609 If the binder reports an elaboration circularity, the programmer has several
kono
parents:
diff changeset
1610 options:
kono
parents:
diff changeset
1611
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1612 * Ensure that elaboration warnings are enabled. This will allow the static
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1613 model to output trace information of elaboration issues. The trace
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1614 information could shed light on previously unforeseen dependencies, as well
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1615 as their origins. Elaboration warnings are enabled with compiler switch
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1616 :switch:`-gnatwl`.
111
kono
parents:
diff changeset
1617
kono
parents:
diff changeset
1618 * Use switch :switch:`-gnatel` to obtain messages on generated implicit
kono
parents:
diff changeset
1619 ``Elaborate`` and ``Elaborate_All`` pragmas. The trace information could
kono
parents:
diff changeset
1620 indicate why a server unit must be elaborated prior to a client unit.
kono
parents:
diff changeset
1621
kono
parents:
diff changeset
1622 * If the warnings produced by the static model indicate that a task is
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1623 involved, consider the options in section `Resolving Task Issues`_.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1624
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1625 * If none of the steps outlined above resolve the circularity, use a more
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1626 permissive elaboration model, in the following order:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1627
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1628 - Use the dynamic elaboration model, with compiler switch :switch:`-gnatE`.
111
kono
parents:
diff changeset
1629
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1630 - Use the legacy static elaboration model, with compiler switch
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1631 :switch:`-gnatH`.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1632
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1633 - Use the legacy dynamic elaboration model, with compiler switches
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1634 :switch:`-gnatH` :switch:`-gnatE`.
111
kono
parents:
diff changeset
1635
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1636 - Use the relaxed legacy static elaboration model, with compiler switches
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1637 :switch:`-gnatH` :switch:`-gnatJ`.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1638
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1639 - Use the relaxed legacy dynamic elaboration model, with compiler switches
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1640 :switch:`-gnatH` :switch:`-gnatJ` :switch:`-gnatE`.
111
kono
parents:
diff changeset
1641
kono
parents:
diff changeset
1642 .. _Inspecting_the_Chosen_Elaboration_Order:
kono
parents:
diff changeset
1643
kono
parents:
diff changeset
1644 Inspecting the Chosen Elaboration Order
kono
parents:
diff changeset
1645 =======================================
kono
parents:
diff changeset
1646
kono
parents:
diff changeset
1647 To see the elaboration order chosen by the binder, inspect the contents of file
kono
parents:
diff changeset
1648 `b~xxx.adb`. On certain targets, this file appears as `b_xxx.adb`. The
kono
parents:
diff changeset
1649 elaboration order appears as a sequence of calls to ``Elab_Body`` and
kono
parents:
diff changeset
1650 ``Elab_Spec``, interspersed with assignments to `Exxx` which indicates that a
kono
parents:
diff changeset
1651 particular unit is elaborated. For example:
kono
parents:
diff changeset
1652
kono
parents:
diff changeset
1653 ::
kono
parents:
diff changeset
1654
kono
parents:
diff changeset
1655 System.Soft_Links'Elab_Body;
kono
parents:
diff changeset
1656 E14 := True;
kono
parents:
diff changeset
1657 System.Secondary_Stack'Elab_Body;
kono
parents:
diff changeset
1658 E18 := True;
kono
parents:
diff changeset
1659 System.Exception_Table'Elab_Body;
kono
parents:
diff changeset
1660 E24 := True;
kono
parents:
diff changeset
1661 Ada.Io_Exceptions'Elab_Spec;
kono
parents:
diff changeset
1662 E67 := True;
kono
parents:
diff changeset
1663 Ada.Tags'Elab_Spec;
kono
parents:
diff changeset
1664 Ada.Streams'Elab_Spec;
kono
parents:
diff changeset
1665 E43 := True;
kono
parents:
diff changeset
1666 Interfaces.C'Elab_Spec;
kono
parents:
diff changeset
1667 E69 := True;
kono
parents:
diff changeset
1668 System.Finalization_Root'Elab_Spec;
kono
parents:
diff changeset
1669 E60 := True;
kono
parents:
diff changeset
1670 System.Os_Lib'Elab_Body;
kono
parents:
diff changeset
1671 E71 := True;
kono
parents:
diff changeset
1672 System.Finalization_Implementation'Elab_Spec;
kono
parents:
diff changeset
1673 System.Finalization_Implementation'Elab_Body;
kono
parents:
diff changeset
1674 E62 := True;
kono
parents:
diff changeset
1675 Ada.Finalization'Elab_Spec;
kono
parents:
diff changeset
1676 E58 := True;
kono
parents:
diff changeset
1677 Ada.Finalization.List_Controller'Elab_Spec;
kono
parents:
diff changeset
1678 E76 := True;
kono
parents:
diff changeset
1679 System.File_Control_Block'Elab_Spec;
kono
parents:
diff changeset
1680 E74 := True;
kono
parents:
diff changeset
1681 System.File_Io'Elab_Body;
kono
parents:
diff changeset
1682 E56 := True;
kono
parents:
diff changeset
1683 Ada.Tags'Elab_Body;
kono
parents:
diff changeset
1684 E45 := True;
kono
parents:
diff changeset
1685 Ada.Text_Io'Elab_Spec;
kono
parents:
diff changeset
1686 Ada.Text_Io'Elab_Body;
kono
parents:
diff changeset
1687 E07 := True;
kono
parents:
diff changeset
1688
kono
parents:
diff changeset
1689 Note also binder switch :switch:`-l`, which outputs the chosen elaboration
kono
parents:
diff changeset
1690 order and provides a more readable form of the above:
kono
parents:
diff changeset
1691
kono
parents:
diff changeset
1692 ::
kono
parents:
diff changeset
1693
kono
parents:
diff changeset
1694 ada (spec)
kono
parents:
diff changeset
1695 interfaces (spec)
kono
parents:
diff changeset
1696 system (spec)
kono
parents:
diff changeset
1697 system.case_util (spec)
kono
parents:
diff changeset
1698 system.case_util (body)
kono
parents:
diff changeset
1699 system.concat_2 (spec)
kono
parents:
diff changeset
1700 system.concat_2 (body)
kono
parents:
diff changeset
1701 system.concat_3 (spec)
kono
parents:
diff changeset
1702 system.concat_3 (body)
kono
parents:
diff changeset
1703 system.htable (spec)
kono
parents:
diff changeset
1704 system.parameters (spec)
kono
parents:
diff changeset
1705 system.parameters (body)
kono
parents:
diff changeset
1706 system.crtl (spec)
kono
parents:
diff changeset
1707 interfaces.c_streams (spec)
kono
parents:
diff changeset
1708 interfaces.c_streams (body)
kono
parents:
diff changeset
1709 system.restrictions (spec)
kono
parents:
diff changeset
1710 system.restrictions (body)
kono
parents:
diff changeset
1711 system.standard_library (spec)
kono
parents:
diff changeset
1712 system.exceptions (spec)
kono
parents:
diff changeset
1713 system.exceptions (body)
kono
parents:
diff changeset
1714 system.storage_elements (spec)
kono
parents:
diff changeset
1715 system.storage_elements (body)
kono
parents:
diff changeset
1716 system.secondary_stack (spec)
kono
parents:
diff changeset
1717 system.stack_checking (spec)
kono
parents:
diff changeset
1718 system.stack_checking (body)
kono
parents:
diff changeset
1719 system.string_hash (spec)
kono
parents:
diff changeset
1720 system.string_hash (body)
kono
parents:
diff changeset
1721 system.htable (body)
kono
parents:
diff changeset
1722 system.strings (spec)
kono
parents:
diff changeset
1723 system.strings (body)
kono
parents:
diff changeset
1724 system.traceback (spec)
kono
parents:
diff changeset
1725 system.traceback (body)
kono
parents:
diff changeset
1726 system.traceback_entries (spec)
kono
parents:
diff changeset
1727 system.traceback_entries (body)
kono
parents:
diff changeset
1728 ada.exceptions (spec)
kono
parents:
diff changeset
1729 ada.exceptions.last_chance_handler (spec)
kono
parents:
diff changeset
1730 system.soft_links (spec)
kono
parents:
diff changeset
1731 system.soft_links (body)
kono
parents:
diff changeset
1732 ada.exceptions.last_chance_handler (body)
kono
parents:
diff changeset
1733 system.secondary_stack (body)
kono
parents:
diff changeset
1734 system.exception_table (spec)
kono
parents:
diff changeset
1735 system.exception_table (body)
kono
parents:
diff changeset
1736 ada.io_exceptions (spec)
kono
parents:
diff changeset
1737 ada.tags (spec)
kono
parents:
diff changeset
1738 ada.streams (spec)
kono
parents:
diff changeset
1739 interfaces.c (spec)
kono
parents:
diff changeset
1740 interfaces.c (body)
kono
parents:
diff changeset
1741 system.finalization_root (spec)
kono
parents:
diff changeset
1742 system.finalization_root (body)
kono
parents:
diff changeset
1743 system.memory (spec)
kono
parents:
diff changeset
1744 system.memory (body)
kono
parents:
diff changeset
1745 system.standard_library (body)
kono
parents:
diff changeset
1746 system.os_lib (spec)
kono
parents:
diff changeset
1747 system.os_lib (body)
kono
parents:
diff changeset
1748 system.unsigned_types (spec)
kono
parents:
diff changeset
1749 system.stream_attributes (spec)
kono
parents:
diff changeset
1750 system.stream_attributes (body)
kono
parents:
diff changeset
1751 system.finalization_implementation (spec)
kono
parents:
diff changeset
1752 system.finalization_implementation (body)
kono
parents:
diff changeset
1753 ada.finalization (spec)
kono
parents:
diff changeset
1754 ada.finalization (body)
kono
parents:
diff changeset
1755 ada.finalization.list_controller (spec)
kono
parents:
diff changeset
1756 ada.finalization.list_controller (body)
kono
parents:
diff changeset
1757 system.file_control_block (spec)
kono
parents:
diff changeset
1758 system.file_io (spec)
kono
parents:
diff changeset
1759 system.file_io (body)
kono
parents:
diff changeset
1760 system.val_uns (spec)
kono
parents:
diff changeset
1761 system.val_util (spec)
kono
parents:
diff changeset
1762 system.val_util (body)
kono
parents:
diff changeset
1763 system.val_uns (body)
kono
parents:
diff changeset
1764 system.wch_con (spec)
kono
parents:
diff changeset
1765 system.wch_con (body)
kono
parents:
diff changeset
1766 system.wch_cnv (spec)
kono
parents:
diff changeset
1767 system.wch_jis (spec)
kono
parents:
diff changeset
1768 system.wch_jis (body)
kono
parents:
diff changeset
1769 system.wch_cnv (body)
kono
parents:
diff changeset
1770 system.wch_stw (spec)
kono
parents:
diff changeset
1771 system.wch_stw (body)
kono
parents:
diff changeset
1772 ada.tags (body)
kono
parents:
diff changeset
1773 ada.exceptions (body)
kono
parents:
diff changeset
1774 ada.text_io (spec)
kono
parents:
diff changeset
1775 ada.text_io (body)
kono
parents:
diff changeset
1776 text_io (spec)
kono
parents:
diff changeset
1777 gdbstr (body)