]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_elab.adb
1217a2cc688a00d3d88a4a9a7dc4677fee2b8be3
[gcc.git] / gcc / ada / sem_elab.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ E L A B --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1997-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Ch11; use Exp_Ch11;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Lib; use Lib;
35 with Lib.Load; use Lib.Load;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
39 with Opt; use Opt;
40 with Restrict; use Restrict;
41 with Rident; use Rident;
42 with Rtsfind; use Rtsfind;
43 with Sem; use Sem;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Ch7; use Sem_Ch7;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Prag; use Sem_Prag;
48 with Sem_Util; use Sem_Util;
49 with Sinfo; use Sinfo;
50 with Snames; use Snames;
51 with Stand; use Stand;
52 with Table;
53 with Tbuild; use Tbuild;
54 with Uintp; use Uintp;
55 with Uname; use Uname;
56
57 with GNAT.HTable; use GNAT.HTable;
58
59 package body Sem_Elab is
60
61 -----------------------------------------
62 -- Access-before-elaboration mechanism --
63 -----------------------------------------
64
65 -- The access-before-elaboration (ABE) mechanism implemented in this unit
66 -- has the following objectives:
67 --
68 -- * Diagnose at compile-time or install run-time checks to prevent ABE
69 -- access to data and behaviour.
70 --
71 -- The high-level idea is to accurately diagnose ABE issues within a
72 -- single unit because the ABE mechanism can inspect the whole unit.
73 -- As soon as the elaboration graph extends to an external unit, the
74 -- diagnostics stop because the body of the unit may not be available.
75 -- Due to control and data flow, the ABE mechanism cannot accurately
76 -- determine whether a particular scenario will be elaborated or not.
77 -- Conditional ABE checks are therefore used to verify the elaboration
78 -- status of a local and external target at run time.
79 --
80 -- * Supply elaboration dependencies for a unit to binde
81 --
82 -- The ABE mechanism registers each outgoing elaboration edge for the
83 -- main unit in its ALI file. GNATbind and binde can then reconstruct
84 -- the full elaboration graph and determine the proper elaboration
85 -- order for all units in the compilation.
86 --
87 -- The ABE mechanism supports three models of elaboration:
88 --
89 -- * Dynamic model - This is the most permissive of the three models.
90 -- When the dynamic model is in effect, the mechanism performs very
91 -- little diagnostics and generates run-time checks to detect ABE
92 -- issues. The behaviour of this model is identical to that specified
93 -- by the Ada RM. This model is enabled with switch -gnatE.
94 --
95 -- * Static model - This is the middle ground of the three models. When
96 -- the static model is in effect, the mechanism diagnoses and installs
97 -- run-time checks to detect ABE issues in the main unit. In addition,
98 -- the mechanism generates implicit Elaborate or Elaborate_All pragmas
99 -- to ensure the prior elaboration of withed units. The model employs
100 -- textual order, with clause context, and elaboration-related source
101 -- pragmas. This is the default model.
102 --
103 -- * SPARK model - This is the most conservative of the three models and
104 -- impelements the semantics defined in SPARK RM 7.7. The SPARK model
105 -- is in effect only when a context resides in a SPARK_Mode On region,
106 -- otherwise the mechanism falls back to one of the previous models.
107 --
108 -- The ABE mechanism consists of a "recording" phase and a "processing"
109 -- phase.
110
111 -----------------
112 -- Terminology --
113 -----------------
114
115 -- * Bridge target - A type of target. A bridge target is a link between
116 -- scenarios. It is usually a byproduct of expansion and does not have
117 -- any direct ABE ramifications.
118 --
119 -- * Call marker - A special node used to indicate the presence of a call
120 -- in the tree in case expansion transforms or eliminates the original
121 -- call. N_Call_Marker nodes do not have static and run-time semantics.
122 --
123 -- * Conditional ABE - A type of ABE. A conditional ABE occurs when the
124 -- elaboration or invocation of a target by a scenario within the main
125 -- unit causes an ABE, but does not cause an ABE for another scenarios
126 -- within the main unit.
127 --
128 -- * Declaration level - A type of enclosing level. A scenario or target is
129 -- at the declaration level when it appears within the declarations of a
130 -- block statement, entry body, subprogram body, or task body, ignoring
131 -- enclosing packages.
132 --
133 -- * Early call region - A section of code which ends at a subprogram body
134 -- and starts from the nearest non-preelaborable construct which precedes
135 -- the subprogram body. The early call region extends from a package body
136 -- to a package spec when the spec carries pragma Elaborate_Body.
137 --
138 -- * Generic library level - A type of enclosing level. A scenario or
139 -- target is at the generic library level if it appears in a generic
140 -- package library unit, ignoring enclosing packages.
141 --
142 -- * Guaranteed ABE - A type of ABE. A guaranteed ABE occurs when the
143 -- elaboration or invocation of a target by all scenarios within the
144 -- main unit causes an ABE.
145 --
146 -- * Instantiation library level - A type of enclosing level. A scenario
147 -- or target is at the instantiation library level if it appears in an
148 -- instantiation library unit, ignoring enclosing packages.
149 --
150 -- * Library level - A type of enclosing level. A scenario or target is at
151 -- the library level if it appears in a package library unit, ignoring
152 -- enclosng packages.
153 --
154 -- * Non-library-level encapsulator - A construct that cannot be elaborated
155 -- on its own and requires elaboration by a top-level scenario.
156 --
157 -- * Scenario - A construct or context which may be elaborated or executed
158 -- by elaboration code. The scenarios recognized by the ABE mechanism are
159 -- as follows:
160 --
161 -- - '[Unrestricted_]Access of entries, operators, and subprograms
162 --
163 -- - Assignments to variables
164 --
165 -- - Calls to entries, operators, and subprograms
166 --
167 -- - Derived type declarations
168 --
169 -- - Instantiations
170 --
171 -- - Pragma Refined_State
172 --
173 -- - Reads of variables
174 --
175 -- - Task activation
176 --
177 -- * Target - A construct referenced by a scenario. The targets recognized
178 -- by the ABE mechanism are as follows:
179 --
180 -- - For '[Unrestricted_]Access of entries, operators, and subprograms,
181 -- the target is the entry, operator, or subprogram.
182 --
183 -- - For assignments to variables, the target is the variable
184 --
185 -- - For calls, the target is the entry, operator, or subprogram
186 --
187 -- - For derived type declarations, the target is the derived type
188 --
189 -- - For instantiations, the target is the generic template
190 --
191 -- - For pragma Refined_State, the targets are the constituents
192 --
193 -- - For reads of variables, the target is the variable
194 --
195 -- - For task activation, the target is the task body
196 --
197 -- * Top-level scenario - A scenario which appears in a non-generic main
198 -- unit. Depending on the elaboration model is in effect, the following
199 -- addotional restrictions apply:
200 --
201 -- - Dynamic model - No restrictions
202 --
203 -- - SPARK model - Falls back to either the dynamic or static model
204 --
205 -- - Static model - The scenario must be at the library level
206
207 ---------------------
208 -- Recording phase --
209 ---------------------
210
211 -- The Recording phase coincides with the analysis/resolution phase of the
212 -- compiler. It has the following objectives:
213 --
214 -- * Record all top-level scenarios for examination by the Processing
215 -- phase.
216 --
217 -- Saving only a certain number of nodes improves the performance of
218 -- the ABE mechanism. This eliminates the need to examine the whole
219 -- tree in a separate pass.
220 --
221 -- * Record certain SPARK scenarios which are not necessarily executable
222 -- during elaboration, but still require elaboration-related checks.
223 --
224 -- Saving only a certain number of nodes improves the performance of
225 -- the ABE mechanism. This eliminates the need to examine the whole
226 -- tree in a separate pass.
227 --
228 -- * Detect and diagnose calls in preelaborable or pure units, including
229 -- generic bodies.
230 --
231 -- This diagnostic is carried out during the Recording phase because it
232 -- does not need the heavy recursive traversal done by the Processing
233 -- phase.
234 --
235 -- * Detect and diagnose guaranteed ABEs caused by instantiations,
236 -- calls, and task activation.
237 --
238 -- The issues detected by the ABE mechanism are reported as warnings
239 -- because they do not violate Ada semantics. Forward instantiations
240 -- may thus reach gigi, however gigi cannot handle certain kinds of
241 -- premature instantiations and may crash. To avoid this limitation,
242 -- the ABE mechanism must identify forward instantiations as early as
243 -- possible and suppress their bodies. Calls and task activations are
244 -- included in this category for completeness.
245
246 ----------------------
247 -- Processing phase --
248 ----------------------
249
250 -- The Processing phase is a separate pass which starts after instantiating
251 -- and/or inlining of bodies, but before the removal of Ghost code. It has
252 -- the following objectives:
253 --
254 -- * Examine all top-level scenarios saved during the Recording phase
255 --
256 -- The top-level scenarios act as roots for depth-first traversal of
257 -- the call/instantiation/task activation graph. The traversal stops
258 -- when an outgoing edge leaves the main unit.
259 --
260 -- * Examine all SPARK scenarios saved during the Recording phase
261 --
262 -- * Depending on the elaboration model in effect, perform the following
263 -- actions:
264 --
265 -- - Dynamic model - Diagnose guaranteed ABEs and install run-time
266 -- conditional ABE checks.
267 --
268 -- - SPARK model - Enforce the SPARK elaboration rules
269 --
270 -- - Static model - Diagnose conditional/guaranteed ABEs, install
271 -- run-time conditional ABE checks, and guarantee the elaboration
272 -- of external units.
273 --
274 -- * Examine nested scenarios
275 --
276 -- Nested scenarios discovered during the depth-first traversal are
277 -- in turn subjected to the same actions outlined above and examined
278 -- for the next level of nested scenarios.
279
280 ------------------
281 -- Architecture --
282 ------------------
283
284 -- Analysis/Resolution
285 -- |
286 -- +- Build_Call_Marker
287 -- |
288 -- +- Build_Variable_Reference_Marker
289 -- |
290 -- +- | -------------------- Recording phase ---------------------------+
291 -- | v |
292 -- | Record_Elaboration_Scenario |
293 -- | | |
294 -- | +--> Check_Preelaborated_Call |
295 -- | | |
296 -- | +--> Process_Guaranteed_ABE |
297 -- | | | |
298 -- | | +--> Process_Guaranteed_ABE_Activation |
299 -- | | | |
300 -- | | +--> Process_Guaranteed_ABE_Call |
301 -- | | | |
302 -- | | +--> Process_Guaranteed_ABE_Instantiation |
303 -- | | |
304 -- +- | ----------------------------------------------------------------+
305 -- |
306 -- |
307 -- +--> SPARK_Scenarios
308 -- | +-----------+-----------+ .. +-----------+
309 -- | | Scenario1 | Scenario2 | .. | ScenarioN |
310 -- | +-----------+-----------+ .. +-----------+
311 -- |
312 -- +--> Top_Level_Scenarios
313 -- | +-----------+-----------+ .. +-----------+
314 -- | | Scenario1 | Scenario2 | .. | ScenarioN |
315 -- | +-----------+-----------+ .. +-----------+
316 -- |
317 -- End of Compilation
318 -- |
319 -- +- | --------------------- Processing phase -------------------------+
320 -- | v |
321 -- | Check_Elaboration_Scenarios |
322 -- | | |
323 -- | +--> Check_SPARK_Scenario |
324 -- | | | |
325 -- | | +--> Check_SPARK_Derived_Type |
326 -- | | | |
327 -- | | +--> Check_SPARK_Instantiation |
328 -- | | | |
329 -- | | +--> Check_SPARK_Refined_State_Pragma |
330 -- | | |
331 -- | +--> Process_Conditional_ABE <---------------------------+ |
332 -- | | | |
333 -- | +--> Process_Conditional_ABE_Access Is_Suitable_Scenario |
334 -- | | ^ |
335 -- | +--> Process_Conditional_ABE_Activation | |
336 -- | | | | |
337 -- | | +-----------------------------+ | |
338 -- | | | | |
339 -- | +--> Process_Conditional_ABE_Call +--------> Traverse_Body |
340 -- | | | | |
341 -- | | +-----------------------------+ |
342 -- | | |
343 -- | +--> Process_Conditional_ABE_Instantiation |
344 -- | | |
345 -- | +--> Process_Conditional_ABE_Variable_Assignment |
346 -- | | |
347 -- | +--> Process_Conditional_ABE_Variable_Reference |
348 -- | |
349 -- +--------------------------------------------------------------------+
350
351 ----------------------
352 -- Important points --
353 ----------------------
354
355 -- The Processing phase starts after the analysis, resolution, expansion
356 -- phase has completed. As a result, no current semantic information is
357 -- available. The scope stack is empty, global flags such as In_Instance
358 -- or Inside_A_Generic become useless. To remedy this, the ABE mechanism
359 -- must either save or recompute semantic information.
360
361 -- Expansion heavily transforms calls and to some extent instantiations. To
362 -- remedy this, the ABE mechanism generates N_Call_Marker nodes in order to
363 -- capture the target and relevant attributes of the original call.
364
365 -- The diagnostics of the ABE mechanism depend on accurate source locations
366 -- to determine the spacial relation of nodes.
367
368 --------------
369 -- Switches --
370 --------------
371
372 -- The following switches may be used to control the behavior of the ABE
373 -- mechanism.
374 --
375 -- -gnatdE elaboration checks on predefined units
376 --
377 -- The ABE mechanism considers scenarios which appear in internal
378 -- units (Ada, GNAT, Interfaces, System).
379 --
380 -- -gnatd.G ignore calls through generic formal parameters for elaboration
381 --
382 -- The ABE mechanism does not generate N_Call_Marker nodes for
383 -- calls which occur in expanded instances, and invoke generic
384 -- actual subprograms through generic formal subprograms. As a
385 -- result, the calls are not recorded or processed.
386 --
387 -- If switches -gnatd.G and -gnatdL are used together, then the
388 -- ABE mechanism effectively ignores all calls which cause the
389 -- elaboration flow to "leave" the instance.
390 --
391 -- -gnatdL ignore external calls from instances for elaboration
392 --
393 -- The ABE mechanism does not generate N_Call_Marker nodes for
394 -- calls which occur in expanded instances, do not invoke generic
395 -- actual subprograms through formal subprograms, and the target
396 -- is external to the instance. As a result, the calls are not
397 -- recorded or processed.
398 --
399 -- If switches -gnatd.G and -gnatdL are used together, then the
400 -- ABE mechanism effectively ignores all calls which cause the
401 -- elaboration flow to "leave" the instance.
402 --
403 -- -gnatd.o conservative elaboration order for indirect calls
404 --
405 -- The ABE mechanism treats '[Unrestricted_]Access of an entry,
406 -- operator, or subprogram as an immediate invocation of the
407 -- target. As a result, it performs ABE checks and diagnostics on
408 -- the immediate call.
409 --
410 -- -gnatd.U ignore indirect calls for static elaboration
411 --
412 -- The ABE mechanism does not consider '[Unrestricted_]Access of
413 -- entries, operators, and subprograms. As a result, the scenarios
414 -- are not recorder or processed.
415 --
416 -- -gnatd.v enforce SPARK elaboration rules in SPARK code
417 --
418 -- The ABE mechanism applies some of the SPARK elaboration rules
419 -- defined in the SPARK reference manual, chapter 7.7. Note that
420 -- certain rules are always enforced, regardless of whether the
421 -- switch is active.
422 --
423 -- -gnatd.y disable implicit pragma Elaborate_All on task bodies
424 --
425 -- The ABE mechanism does not generate implicit Elaborate_All when
426 -- the need for the pragma came from a task body.
427 --
428 -- -gnatE dynamic elaboration checking mode enabled
429 --
430 -- The ABE mechanism assumes that any scenario is elaborated or
431 -- invoked by elaboration code. The ABE mechanism performs very
432 -- little diagnostics and generates condintional ABE checks to
433 -- detect ABE issues at run-time.
434 --
435 -- -gnatel turn on info messages on generated Elaborate[_All] pragmas
436 --
437 -- The ABE mechanism produces information messages on generated
438 -- implicit Elabote[_All] pragmas along with traceback showing
439 -- why the pragma was generated. In addition, the ABE mechanism
440 -- produces information messages for each scenario elaborated or
441 -- invoked by elaboration code.
442 --
443 -- -gnateL turn off info messages on generated Elaborate[_All] pragmas
444 --
445 -- The complimentary switch for -gnatel.
446 --
447 -- -gnatwl turn on warnings for elaboration problems
448 --
449 -- The ABE mechanism produces warnings on detected ABEs along with
450 -- traceback showing the graph of the ABE.
451 --
452 -- -gnatwL turn off warnings for elaboration problems
453 --
454 -- The complimentary switch for -gnatwl.
455 --
456 -- -gnatw.f turn on warnings for suspicious Subp'Access
457 --
458 -- The ABE mechanism treats '[Unrestricted_]Access of an entry,
459 -- operator, or subprogram as a pseudo invocation of the target.
460 -- As a result, it performs ABE diagnostics on the pseudo call.
461 --
462 -- -gnatw.F turn off warnings for suspicious Subp'Access
463 --
464 -- The complimentary switch for -gnatw.f.
465
466 ---------------------------
467 -- Adding a new scenario --
468 ---------------------------
469
470 -- The following steps describe how to add a new elaboration scenario and
471 -- preserve the existing architecture. Note that not all of the steps may
472 -- need to be carried out.
473 --
474 -- 1) Update predicate Is_Scenario
475 --
476 -- 2) Add predicate Is_Suitable_xxx. Include a call to it in predicate
477 -- Is_Suitable_Scenario.
478 --
479 -- 3) Update routine Record_Elaboration_Scenario
480 --
481 -- 4) Add routine Process_Conditional_ABE_xxx. Include a call to it in
482 -- routine Process_Conditional_ABE.
483 --
484 -- 5) Add routine Process_Guaranteed_ABE_xxx. Include a call to it in
485 -- routine Process_Guaranteed_ABE.
486 --
487 -- 6) Add routine Check_SPARK_xxx. Include a call to it in routine
488 -- Check_SPARK_Scenario.
489 --
490 -- 7) Add routine Info_xxx. Include a call to it in routine
491 -- Process_Conditional_ABE_xxx.
492 --
493 -- 8) Add routine Output_xxx. Include a call to it in routine
494 -- Output_Active_Scenarios.
495 --
496 -- 9) Add routine Extract_xxx_Attributes
497 --
498 -- 10) Update routine Is_Potential_Scenario
499
500 -------------------------
501 -- Adding a new target --
502 -------------------------
503
504 -- The following steps describe how to add a new elaboration target and
505 -- preserve the existing architecture. Note that not all of the steps may
506 -- need to be carried out.
507 --
508 -- 1) Add predicate Is_xxx.
509 --
510 -- 2) Update predicates Is_Ada_Semantic_Target, Is_Bridge_Target, or
511 -- Is_SPARK_Semantic_Target. If necessary, create a new category.
512 --
513 -- 3) Update the appropriate Info_xxx routine.
514 --
515 -- 4) Update the appropriate Output_xxx routine.
516 --
517 -- 5) Update routine Extract_Target_Attributes. If necessary, create a
518 -- new Extract_xxx routine.
519
520 --------------------------
521 -- Debugging ABE issues --
522 --------------------------
523
524 -- * If the issue involves a call, ensure that the call is eligible for ABE
525 -- processing and receives a corresponding call marker. The routines of
526 -- interest are
527 --
528 -- Build_Call_Marker
529 -- Record_Elaboration_Scenario
530
531 -- * If the issue involves an arbitrary scenario, ensure that the scenario
532 -- is either recorded, or is successfully recognized while traversing a
533 -- body. The routines of interest are
534 --
535 -- Record_Elaboration_Scenario
536 -- Process_Conditional_ABE
537 -- Process_Guaranteed_ABE
538 -- Traverse_Body
539
540 -- * If the issue involves a circularity in the elaboration order, examine
541 -- the ALI files and look for the following encodings next to units:
542 --
543 -- E indicates a source Elaborate
544 --
545 -- EA indicates a source Elaborate_All
546 --
547 -- AD indicates an implicit Elaborate_All
548 --
549 -- ED indicates an implicit Elaborate
550 --
551 -- If possible, compare these encodings with those generated by the old
552 -- ABE mechanism. The routines of interest are
553 --
554 -- Ensure_Prior_Elaboration
555
556 ----------------
557 -- Attributes --
558 ----------------
559
560 -- To minimize the amount of code within routines, the ABE mechanism relies
561 -- on "attribute" records to capture relevant information for a scenario or
562 -- a target.
563
564 -- The following type captures relevant attributes which pertain to a call
565
566 type Call_Attributes is record
567 Elab_Checks_OK : Boolean;
568 -- This flag is set when the call has elaboration checks enabled
569
570 From_Source : Boolean;
571 -- This flag is set when the call comes from source
572
573 Ghost_Mode_Ignore : Boolean;
574 -- This flag is set when the call appears in a region subject to pragma
575 -- Ghost with policy Ignore.
576
577 In_Declarations : Boolean;
578 -- This flag is set when the call appears at the declaration level
579
580 Is_Dispatching : Boolean;
581 -- This flag is set when the call is dispatching
582
583 SPARK_Mode_On : Boolean;
584 -- This flag is set when the call appears in a region subject to pragma
585 -- SPARK_Mode with value On.
586 end record;
587
588 -- The following type captures relevant attributes which pertain to the
589 -- prior elaboration of a unit. This type is coupled together with a unit
590 -- to form a key -> value relationship.
591
592 type Elaboration_Attributes is record
593 Source_Pragma : Node_Id;
594 -- This attribute denotes a source Elaborate or Elaborate_All pragma
595 -- which guarantees the prior elaboration of some unit with respect
596 -- to the main unit. The pragma may come from the following contexts:
597
598 -- * The main unit
599 -- * The spec of the main unit (if applicable)
600 -- * Any parent spec of the main unit (if applicable)
601 -- * Any parent subunit of the main unit (if applicable)
602
603 -- The attribute remains Empty if no such pragma is available. Source
604 -- pragmas play a role in satisfying SPARK elaboration requirements.
605
606 With_Clause : Node_Id;
607 -- This attribute denotes an internally generated or source with clause
608 -- for some unit withed by the main unit. With clauses carry flags which
609 -- represent implicit Elaborate or Elaborate_All pragmas. These clauses
610 -- play a role in supplying the elaboration dependencies to binde.
611 end record;
612
613 No_Elaboration_Attributes : constant Elaboration_Attributes :=
614 (Source_Pragma => Empty,
615 With_Clause => Empty);
616
617 -- The following type captures relevant attributes which pertain to an
618 -- instantiation.
619
620 type Instantiation_Attributes is record
621 Elab_Checks_OK : Boolean;
622 -- This flag is set when the instantiation has elaboration checks
623 -- enabled.
624
625 Ghost_Mode_Ignore : Boolean;
626 -- This flag is set when the instantiation appears in a region subject
627 -- to pragma Ghost with policy ignore, or starts one such region.
628
629 In_Declarations : Boolean;
630 -- This flag is set when the instantiation appears at the declaration
631 -- level.
632
633 SPARK_Mode_On : Boolean;
634 -- This flag is set when the instantiation appears in a region subject
635 -- to pragma SPARK_Mode with value On, or starts one such region.
636 end record;
637
638 -- The following type captures relevant attributes which pertain to a
639 -- target.
640
641 type Target_Attributes is record
642 Elab_Checks_OK : Boolean;
643 -- This flag is set when the target has elaboration checks enabled
644
645 From_Source : Boolean;
646 -- This flag is set when the target comes from source
647
648 Ghost_Mode_Ignore : Boolean;
649 -- This flag is set when the target appears in a region subject to
650 -- pragma Ghost with policy ignore, or starts one such region.
651
652 SPARK_Mode_On : Boolean;
653 -- This flag is set when the target appears in a region subject to
654 -- pragma SPARK_Mode with value On, or starts one such region.
655
656 Spec_Decl : Node_Id;
657 -- This attribute denotes the declaration of Spec_Id
658
659 Unit_Id : Entity_Id;
660 -- This attribute denotes the top unit where Spec_Id resides
661
662 -- The semantics of the following attributes depend on the target
663
664 Body_Barf : Node_Id;
665 Body_Decl : Node_Id;
666 Spec_Id : Entity_Id;
667
668 -- The target is a generic package or a subprogram
669 --
670 -- * Body_Barf - Empty
671 --
672 -- * Body_Decl - This attribute denotes the generic or subprogram
673 -- body.
674 --
675 -- * Spec_Id - This attribute denotes the entity of the generic
676 -- package or subprogram.
677
678 -- The target is a protected entry
679 --
680 -- * Body_Barf - This attribute denotes the body of the barrier
681 -- function if expansion took place, otherwise it is Empty.
682 --
683 -- * Body_Decl - This attribute denotes the body of the procedure
684 -- which emulates the entry if expansion took place, otherwise it
685 -- denotes the body of the protected entry.
686 --
687 -- * Spec_Id - This attribute denotes the entity of the procedure
688 -- which emulates the entry if expansion took place, otherwise it
689 -- denotes the protected entry.
690
691 -- The target is a protected subprogram
692 --
693 -- * Body_Barf - Empty
694 --
695 -- * Body_Decl - This attribute denotes the body of the protected or
696 -- unprotected version of the protected subprogram if expansion took
697 -- place, otherwise it denotes the body of the protected subprogram.
698 --
699 -- * Spec_Id - This attribute denotes the entity of the protected or
700 -- unprotected version of the protected subprogram if expansion took
701 -- place, otherwise it is the entity of the protected subprogram.
702
703 -- The target is a task entry
704 --
705 -- * Body_Barf - Empty
706 --
707 -- * Body_Decl - This attribute denotes the body of the procedure
708 -- which emulates the task body if expansion took place, otherwise
709 -- it denotes the body of the task type.
710 --
711 -- * Spec_Id - This attribute denotes the entity of the procedure
712 -- which emulates the task body if expansion took place, otherwise
713 -- it denotes the entity of the task type.
714 end record;
715
716 -- The following type captures relevant attributes which pertain to a task
717 -- type.
718
719 type Task_Attributes is record
720 Body_Decl : Node_Id;
721 -- This attribute denotes the declaration of the procedure body which
722 -- emulates the behaviour of the task body.
723
724 Elab_Checks_OK : Boolean;
725 -- This flag is set when the task type has elaboration checks enabled
726
727 Ghost_Mode_Ignore : Boolean;
728 -- This flag is set when the task type appears in a region subject to
729 -- pragma Ghost with policy ignore, or starts one such region.
730
731 SPARK_Mode_On : Boolean;
732 -- This flag is set when the task type appears in a region subject to
733 -- pragma SPARK_Mode with value On, or starts one such region.
734
735 Spec_Id : Entity_Id;
736 -- This attribute denotes the entity of the initial declaration of the
737 -- procedure body which emulates the behaviour of the task body.
738
739 Task_Decl : Node_Id;
740 -- This attribute denotes the declaration of the task type
741
742 Unit_Id : Entity_Id;
743 -- This attribute denotes the entity of the compilation unit where the
744 -- task type resides.
745 end record;
746
747 -- The following type captures relevant attributes which pertain to a
748 -- variable.
749
750 type Variable_Attributes is record
751 Unit_Id : Entity_Id;
752 -- This attribute denotes the entity of the compilation unit where the
753 -- variable resides.
754 end record;
755
756 ---------------------
757 -- Data structures --
758 ---------------------
759
760 -- The ABE mechanism employs lists and hash tables to store information
761 -- pertaining to scenarios and targets, as well as the Processing phase.
762 -- The need for data structures comes partly from the size limitation of
763 -- nodes. Note that the use of hash tables is conservative and operations
764 -- are carried out only when a particular hash table has at least one key
765 -- value pair (see xxx_In_Use flags).
766
767 -- The following table stores the early call regions of subprogram bodies
768
769 Early_Call_Regions_Max : constant := 101;
770
771 type Early_Call_Regions_Index is range 0 .. Early_Call_Regions_Max - 1;
772
773 function Early_Call_Regions_Hash
774 (Key : Entity_Id) return Early_Call_Regions_Index;
775 -- Obtain the hash value of entity Key
776
777 Early_Call_Regions_In_Use : Boolean := False;
778 -- This flag determines whether table Early_Call_Regions contains at least
779 -- least one key/value pair.
780
781 Early_Call_Regions_No_Element : constant Node_Id := Empty;
782
783 package Early_Call_Regions is new Simple_HTable
784 (Header_Num => Early_Call_Regions_Index,
785 Element => Node_Id,
786 No_Element => Early_Call_Regions_No_Element,
787 Key => Entity_Id,
788 Hash => Early_Call_Regions_Hash,
789 Equal => "=");
790
791 -- The following table stores the elaboration status of all units withed by
792 -- the main unit.
793
794 Elaboration_Statuses_Max : constant := 1009;
795
796 type Elaboration_Statuses_Index is range 0 .. Elaboration_Statuses_Max - 1;
797
798 function Elaboration_Statuses_Hash
799 (Key : Entity_Id) return Elaboration_Statuses_Index;
800 -- Obtain the hash value of entity Key
801
802 Elaboration_Statuses_In_Use : Boolean := False;
803 -- This flag flag determines whether table Elaboration_Statuses contains at
804 -- least one key/value pair.
805
806 Elaboration_Statuses_No_Element : constant Elaboration_Attributes :=
807 No_Elaboration_Attributes;
808
809 package Elaboration_Statuses is new Simple_HTable
810 (Header_Num => Elaboration_Statuses_Index,
811 Element => Elaboration_Attributes,
812 No_Element => Elaboration_Statuses_No_Element,
813 Key => Entity_Id,
814 Hash => Elaboration_Statuses_Hash,
815 Equal => "=");
816
817 -- The following table stores a status flag for each SPARK scenario saved
818 -- in table SPARK_Scenarios.
819
820 Recorded_SPARK_Scenarios_Max : constant := 127;
821
822 type Recorded_SPARK_Scenarios_Index is
823 range 0 .. Recorded_SPARK_Scenarios_Max - 1;
824
825 function Recorded_SPARK_Scenarios_Hash
826 (Key : Node_Id) return Recorded_SPARK_Scenarios_Index;
827 -- Obtain the hash value of Key
828
829 Recorded_SPARK_Scenarios_In_Use : Boolean := False;
830 -- This flag flag determines whether table Recorded_SPARK_Scenarios
831 -- contains at least one key/value pair.
832
833 Recorded_SPARK_Scenarios_No_Element : constant Boolean := False;
834
835 package Recorded_SPARK_Scenarios is new Simple_HTable
836 (Header_Num => Recorded_SPARK_Scenarios_Index,
837 Element => Boolean,
838 No_Element => Recorded_SPARK_Scenarios_No_Element,
839 Key => Node_Id,
840 Hash => Recorded_SPARK_Scenarios_Hash,
841 Equal => "=");
842
843 -- The following table stores a status flag for each top-level scenario
844 -- recorded in table Top_Level_Scenarios.
845
846 Recorded_Top_Level_Scenarios_Max : constant := 503;
847
848 type Recorded_Top_Level_Scenarios_Index is
849 range 0 .. Recorded_Top_Level_Scenarios_Max - 1;
850
851 function Recorded_Top_Level_Scenarios_Hash
852 (Key : Node_Id) return Recorded_Top_Level_Scenarios_Index;
853 -- Obtain the hash value of entity Key
854
855 Recorded_Top_Level_Scenarios_In_Use : Boolean := False;
856 -- This flag flag determines whether table Recorded_Top_Level_Scenarios
857 -- contains at least one key/value pair.
858
859 Recorded_Top_Level_Scenarios_No_Element : constant Boolean := False;
860
861 package Recorded_Top_Level_Scenarios is new Simple_HTable
862 (Header_Num => Recorded_Top_Level_Scenarios_Index,
863 Element => Boolean,
864 No_Element => Recorded_Top_Level_Scenarios_No_Element,
865 Key => Node_Id,
866 Hash => Recorded_Top_Level_Scenarios_Hash,
867 Equal => "=");
868
869 -- The following table stores all active scenarios in a recursive traversal
870 -- starting from a top-level scenario. This table must be maintained in a
871 -- FIFO fashion.
872
873 package Scenario_Stack is new Table.Table
874 (Table_Component_Type => Node_Id,
875 Table_Index_Type => Int,
876 Table_Low_Bound => 1,
877 Table_Initial => 50,
878 Table_Increment => 100,
879 Table_Name => "Scenario_Stack");
880
881 -- The following table stores SPARK scenarios which are not necessarily
882 -- executable during elaboration, but still require elaboration-related
883 -- checks.
884
885 package SPARK_Scenarios is new Table.Table
886 (Table_Component_Type => Node_Id,
887 Table_Index_Type => Int,
888 Table_Low_Bound => 1,
889 Table_Initial => 50,
890 Table_Increment => 100,
891 Table_Name => "SPARK_Scenarios");
892
893 -- The following table stores all top-level scenario saved during the
894 -- Recording phase. The contents of this table act as traversal roots
895 -- later in the Processing phase. This table must be maintained in a
896 -- LIFO fashion.
897
898 package Top_Level_Scenarios is new Table.Table
899 (Table_Component_Type => Node_Id,
900 Table_Index_Type => Int,
901 Table_Low_Bound => 1,
902 Table_Initial => 1000,
903 Table_Increment => 100,
904 Table_Name => "Top_Level_Scenarios");
905
906 -- The following table stores the bodies of all eligible scenarios visited
907 -- during a traversal starting from a top-level scenario. The contents of
908 -- this table must be reset upon each new traversal.
909
910 Visited_Bodies_Max : constant := 511;
911
912 type Visited_Bodies_Index is range 0 .. Visited_Bodies_Max - 1;
913
914 function Visited_Bodies_Hash (Key : Node_Id) return Visited_Bodies_Index;
915 -- Obtain the hash value of node Key
916
917 Visited_Bodies_In_Use : Boolean := False;
918 -- This flag determines whether table Visited_Bodies contains at least one
919 -- key/value pair.
920
921 Visited_Bodies_No_Element : constant Boolean := False;
922
923 package Visited_Bodies is new Simple_HTable
924 (Header_Num => Visited_Bodies_Index,
925 Element => Boolean,
926 No_Element => Visited_Bodies_No_Element,
927 Key => Node_Id,
928 Hash => Visited_Bodies_Hash,
929 Equal => "=");
930
931 -----------------------
932 -- Local subprograms --
933 -----------------------
934
935 -- Multiple local subprograms are utilized to lower the semantic complexity
936 -- of the Recording and Processing phase.
937
938 procedure Check_Preelaborated_Call (Call : Node_Id);
939 pragma Inline (Check_Preelaborated_Call);
940 -- Verify that entry, operator, or subprogram call Call does not appear at
941 -- the library level of a preelaborated unit.
942
943 procedure Check_SPARK_Derived_Type (Typ_Decl : Node_Id);
944 pragma Inline (Check_SPARK_Derived_Type);
945 -- Verify that the freeze node of a derived type denoted by declaration
946 -- Typ_Decl is within the early call region of each overriding primitive
947 -- body that belongs to the derived type (SPARK RM 7.7(8)).
948
949 procedure Check_SPARK_Instantiation (Exp_Inst : Node_Id);
950 pragma Inline (Check_SPARK_Instantiation);
951 -- Verify that expanded instance Exp_Inst does not precede the generic body
952 -- it instantiates (SPARK RM 7.7(6)).
953
954 procedure Check_SPARK_Scenario (N : Node_Id);
955 pragma Inline (Check_SPARK_Scenario);
956 -- Top-level dispatcher for verifying SPARK scenarios which are not always
957 -- executable during elaboration but still need elaboration-related checks.
958
959 procedure Check_SPARK_Refined_State_Pragma (N : Node_Id);
960 pragma Inline (Check_SPARK_Refined_State_Pragma);
961 -- Verify that each constituent of Refined_State pragma N which belongs to
962 -- an abstract state mentioned in pragma Initializes has prior elaboration
963 -- with respect to the main unit (SPARK RM 7.7.1(7)).
964
965 function Compilation_Unit (Unit_Id : Entity_Id) return Node_Id;
966 pragma Inline (Compilation_Unit);
967 -- Return the N_Compilation_Unit node of unit Unit_Id
968
969 function Early_Call_Region (Body_Id : Entity_Id) return Node_Id;
970 pragma Inline (Early_Call_Region);
971 -- Return the early call region associated with entry or subprogram body
972 -- Body_Id. IMPORTANT: This routine does not find the early call region.
973 -- To compute it, use routine Find_Early_Call_Region.
974
975 procedure Elab_Msg_NE
976 (Msg : String;
977 N : Node_Id;
978 Id : Entity_Id;
979 Info_Msg : Boolean;
980 In_SPARK : Boolean);
981 pragma Inline (Elab_Msg_NE);
982 -- Wrapper around Error_Msg_NE. Emit message Msg concerning arbitrary node
983 -- N and entity. If flag Info_Msg is set, the routine emits an information
984 -- message, otherwise it emits an error. If flag In_SPARK is set, then
985 -- string " in SPARK" is added to the end of the message.
986
987 function Elaboration_Status
988 (Unit_Id : Entity_Id) return Elaboration_Attributes;
989 pragma Inline (Elaboration_Status);
990 -- Return the set of elaboration attributes associated with unit Unit_Id
991
992 procedure Ensure_Prior_Elaboration
993 (N : Node_Id;
994 Unit_Id : Entity_Id;
995 Prag_Nam : Name_Id;
996 In_Partial_Fin : Boolean;
997 In_Task_Body : Boolean);
998 -- Guarantee the elaboration of unit Unit_Id with respect to the main unit
999 -- by installing pragma Elaborate or Elaborate_All denoted by Prag_Nam. N
1000 -- denotes the related scenario. The flags should be set when the need for
1001 -- elaboration was initiated as follows:
1002 --
1003 -- In_Partial_Fin - partial finalization procedure
1004 -- In_Task_Body - task body
1005
1006 procedure Ensure_Prior_Elaboration_Dynamic
1007 (N : Node_Id;
1008 Unit_Id : Entity_Id;
1009 Prag_Nam : Name_Id);
1010 -- Guarantee the elaboration of unit Unit_Id with respect to the main unit
1011 -- by suggesting the use of Elaborate[_All] with name Prag_Nam. N denotes
1012 -- the related scenario.
1013
1014 procedure Ensure_Prior_Elaboration_Static
1015 (N : Node_Id;
1016 Unit_Id : Entity_Id;
1017 Prag_Nam : Name_Id);
1018 -- Guarantee the elaboration of unit Unit_Id with respect to the main unit
1019 -- by installing an implicit Elaborate[_All] pragma with name Prag_Nam. N
1020 -- denotes the related scenario.
1021
1022 function Extract_Assignment_Name (Asmt : Node_Id) return Node_Id;
1023 pragma Inline (Extract_Assignment_Name);
1024 -- Obtain the Name attribute of assignment statement Asmt
1025
1026 procedure Extract_Call_Attributes
1027 (Call : Node_Id;
1028 Target_Id : out Entity_Id;
1029 Attrs : out Call_Attributes);
1030 pragma Inline (Extract_Call_Attributes);
1031 -- Obtain attributes Attrs associated with call Call. Target_Id is the
1032 -- entity of the call target.
1033
1034 function Extract_Call_Name (Call : Node_Id) return Node_Id;
1035 pragma Inline (Extract_Call_Name);
1036 -- Obtain the Name attribute of entry or subprogram call Call
1037
1038 procedure Extract_Instance_Attributes
1039 (Exp_Inst : Node_Id;
1040 Inst_Body : out Node_Id;
1041 Inst_Decl : out Node_Id);
1042 pragma Inline (Extract_Instance_Attributes);
1043 -- Obtain body Inst_Body and spec Inst_Decl of expanded instance Exp_Inst
1044
1045 procedure Extract_Instantiation_Attributes
1046 (Exp_Inst : Node_Id;
1047 Inst : out Node_Id;
1048 Inst_Id : out Entity_Id;
1049 Gen_Id : out Entity_Id;
1050 Attrs : out Instantiation_Attributes);
1051 pragma Inline (Extract_Instantiation_Attributes);
1052 -- Obtain attributes Attrs associated with expanded instantiation Exp_Inst.
1053 -- Inst is the instantiation. Inst_Id is the entity of the instance. Gen_Id
1054 -- is the entity of the generic unit being instantiated.
1055
1056 procedure Extract_Target_Attributes
1057 (Target_Id : Entity_Id;
1058 Attrs : out Target_Attributes);
1059 -- Obtain attributes Attrs associated with an entry, package, or subprogram
1060 -- denoted by Target_Id.
1061
1062 procedure Extract_Task_Attributes
1063 (Typ : Entity_Id;
1064 Attrs : out Task_Attributes);
1065 pragma Inline (Extract_Task_Attributes);
1066 -- Obtain attributes Attrs associated with task type Typ
1067
1068 procedure Extract_Variable_Reference_Attributes
1069 (Ref : Node_Id;
1070 Var_Id : out Entity_Id;
1071 Attrs : out Variable_Attributes);
1072 pragma Inline (Extract_Variable_Reference_Attributes);
1073 -- Obtain attributes Attrs associated with reference Ref that mentions
1074 -- variable Var_Id.
1075
1076 function Find_Code_Unit (N : Node_Or_Entity_Id) return Entity_Id;
1077 pragma Inline (Find_Code_Unit);
1078 -- Return the code unit which contains arbitrary node or entity N. This
1079 -- is the unit of the file which physically contains the related construct
1080 -- denoted by N except when N is within an instantiation. In that case the
1081 -- unit is that of the top-level instantiation.
1082
1083 function Find_Early_Call_Region
1084 (Body_Decl : Node_Id;
1085 Assume_Elab_Body : Boolean := False;
1086 Skip_Memoization : Boolean := False) return Node_Id;
1087 -- Find the start of the early call region which belongs to subprogram body
1088 -- Body_Decl as defined in SPARK RM 7.7. The behavior of the routine is to
1089 -- find the early call region, memoize it, and return it, but this behavior
1090 -- can be altered. Flag Assume_Elab_Body should be set when a package spec
1091 -- may lack pragma Elaborate_Body, but the routine must still examine that
1092 -- spec. Flag Skip_Memoization should be set when the routine must avoid
1093 -- memoizing the region.
1094
1095 procedure Find_Elaborated_Units;
1096 -- Populate table Elaboration_Statuses with all units which have prior
1097 -- elaboration with respect to the main unit.
1098
1099 function Find_Enclosing_Instance (N : Node_Id) return Node_Id;
1100 pragma Inline (Find_Enclosing_Instance);
1101 -- Find the declaration or body of the nearest expanded instance which
1102 -- encloses arbitrary node N. Return Empty if no such instance exists.
1103
1104 function Find_Top_Unit (N : Node_Or_Entity_Id) return Entity_Id;
1105 pragma Inline (Find_Top_Unit);
1106 -- Return the top unit which contains arbitrary node or entity N. The unit
1107 -- is obtained by logically unwinding instantiations and subunits when N
1108 -- resides within one.
1109
1110 function Find_Unit_Entity (N : Node_Id) return Entity_Id;
1111 pragma Inline (Find_Unit_Entity);
1112 -- Return the entity of unit N
1113
1114 function First_Formal_Type (Subp_Id : Entity_Id) return Entity_Id;
1115 pragma Inline (First_Formal_Type);
1116 -- Return the type of subprogram Subp_Id's first formal parameter. If the
1117 -- subprogram lacks formal parameters, return Empty.
1118
1119 function Has_Body (Pack_Decl : Node_Id) return Boolean;
1120 -- Determine whether package declaration Pack_Decl has a corresponding body
1121 -- or would eventually have one.
1122
1123 function Has_Prior_Elaboration
1124 (Unit_Id : Entity_Id;
1125 Context_OK : Boolean := False;
1126 Elab_Body_OK : Boolean := False;
1127 Same_Unit_OK : Boolean := False) return Boolean;
1128 pragma Inline (Has_Prior_Elaboration);
1129 -- Determine whether unit Unit_Id is elaborated prior to the main unit.
1130 -- If flag Context_OK is set, the routine considers the following case
1131 -- as valid prior elaboration:
1132 --
1133 -- * Unit_Id is in the elaboration context of the main unit
1134 --
1135 -- If flag Elab_Body_OK is set, the routine considers the following case
1136 -- as valid prior elaboration:
1137 --
1138 -- * Unit_Id has pragma Elaborate_Body and is not the main unit
1139 --
1140 -- If flag Same_Unit_OK is set, the routine considers the following cases
1141 -- as valid prior elaboration:
1142 --
1143 -- * Unit_Id is the main unit
1144 --
1145 -- * Unit_Id denotes the spec of the main unit body
1146
1147 function In_External_Instance
1148 (N : Node_Id;
1149 Target_Decl : Node_Id) return Boolean;
1150 pragma Inline (In_External_Instance);
1151 -- Determine whether a target desctibed by its declaration Target_Decl
1152 -- resides in a package instance which is external to scenario N.
1153
1154 function In_Main_Context (N : Node_Id) return Boolean;
1155 pragma Inline (In_Main_Context);
1156 -- Determine whether arbitrary node N appears within the main compilation
1157 -- unit.
1158
1159 function In_Same_Context
1160 (N1 : Node_Id;
1161 N2 : Node_Id;
1162 Nested_OK : Boolean := False) return Boolean;
1163 -- Determine whether two arbitrary nodes N1 and N2 appear within the same
1164 -- context ignoring enclosing library levels. Nested_OK should be set when
1165 -- the context of N1 can enclose that of N2.
1166
1167 procedure Info_Call
1168 (Call : Node_Id;
1169 Target_Id : Entity_Id;
1170 Info_Msg : Boolean;
1171 In_SPARK : Boolean);
1172 -- Output information concerning call Call which invokes target Target_Id.
1173 -- If flag Info_Msg is set, the routine emits an information message,
1174 -- otherwise it emits an error. If flag In_SPARK is set, then the string
1175 -- " in SPARK" is added to the end of the message.
1176
1177 procedure Info_Instantiation
1178 (Inst : Node_Id;
1179 Gen_Id : Entity_Id;
1180 Info_Msg : Boolean;
1181 In_SPARK : Boolean);
1182 pragma Inline (Info_Instantiation);
1183 -- Output information concerning instantiation Inst which instantiates
1184 -- generic unit Gen_Id. If flag Info_Msg is set, the routine emits an
1185 -- information message, otherwise it emits an error. If flag In_SPARK
1186 -- is set, then string " in SPARK" is added to the end of the message.
1187
1188 procedure Info_Variable_Reference
1189 (Ref : Node_Id;
1190 Var_Id : Entity_Id;
1191 Info_Msg : Boolean;
1192 In_SPARK : Boolean);
1193 pragma Inline (Info_Variable_Reference);
1194 -- Output information concerning reference Ref which mentions variable
1195 -- Var_Id. If flag Info_Msg is set, the routine emits an information
1196 -- message, otherwise it emits an error. If flag In_SPARK is set, then
1197 -- string " in SPARK" is added to the end of the message.
1198
1199 function Insertion_Node (N : Node_Id; Ins_Nod : Node_Id) return Node_Id;
1200 pragma Inline (Insertion_Node);
1201 -- Obtain the proper insertion node of an ABE check or failure for scenario
1202 -- N and candidate insertion node Ins_Nod.
1203
1204 procedure Install_ABE_Check
1205 (N : Node_Id;
1206 Id : Entity_Id;
1207 Ins_Nod : Node_Id);
1208 -- Insert a run-time ABE check for elaboration scenario N which verifies
1209 -- whether arbitrary entity Id is elaborated. The check in inserted prior
1210 -- to node Ins_Nod.
1211
1212 procedure Install_ABE_Check
1213 (N : Node_Id;
1214 Target_Id : Entity_Id;
1215 Target_Decl : Node_Id;
1216 Target_Body : Node_Id;
1217 Ins_Nod : Node_Id);
1218 -- Insert a run-time ABE check for elaboration scenario N which verifies
1219 -- whether target Target_Id with initial declaration Target_Decl and body
1220 -- Target_Body is elaborated. The check is inserted prior to node Ins_Nod.
1221
1222 procedure Install_ABE_Failure (N : Node_Id; Ins_Nod : Node_Id);
1223 -- Insert a Program_Error concerning a guaranteed ABE for elaboration
1224 -- scenario N. The failure is inserted prior to node Node_Id.
1225
1226 function Is_Accept_Alternative_Proc (Id : Entity_Id) return Boolean;
1227 pragma Inline (Is_Accept_Alternative_Proc);
1228 -- Determine whether arbitrary entity Id denotes an internally generated
1229 -- procedure which encapsulates the statements of an accept alternative.
1230
1231 function Is_Activation_Proc (Id : Entity_Id) return Boolean;
1232 pragma Inline (Is_Activation_Proc);
1233 -- Determine whether arbitrary entity Id denotes a runtime procedure in
1234 -- charge with activating tasks.
1235
1236 function Is_Ada_Semantic_Target (Id : Entity_Id) return Boolean;
1237 pragma Inline (Is_Ada_Semantic_Target);
1238 -- Determine whether arbitrary entity Id nodes a source or internally
1239 -- generated subprogram which emulates Ada semantics.
1240
1241 function Is_Bodiless_Subprogram (Subp_Id : Entity_Id) return Boolean;
1242 pragma Inline (Is_Bodiless_Subprogram);
1243 -- Determine whether subprogram Subp_Id will never have a body
1244
1245 function Is_Controlled_Proc
1246 (Subp_Id : Entity_Id;
1247 Subp_Nam : Name_Id) return Boolean;
1248 pragma Inline (Is_Controlled_Proc);
1249 -- Determine whether subprogram Subp_Id denotes controlled type primitives
1250 -- Adjust, Finalize, or Initialize as denoted by name Subp_Nam.
1251
1252 function Is_Default_Initial_Condition_Proc (Id : Entity_Id) return Boolean;
1253 pragma Inline (Is_Default_Initial_Condition_Proc);
1254 -- Determine whether arbitrary entity Id denotes internally generated
1255 -- routine Default_Initial_Condition.
1256
1257 function Is_Finalizer_Proc (Id : Entity_Id) return Boolean;
1258 pragma Inline (Is_Finalizer_Proc);
1259 -- Determine whether arbitrary entity Id denotes internally generated
1260 -- routine _Finalizer.
1261
1262 function Is_Guaranteed_ABE
1263 (N : Node_Id;
1264 Target_Decl : Node_Id;
1265 Target_Body : Node_Id) return Boolean;
1266 pragma Inline (Is_Guaranteed_ABE);
1267 -- Determine whether scenario N with a target described by its initial
1268 -- declaration Target_Decl and body Target_Decl results in a guaranteed
1269 -- ABE.
1270
1271 function Is_Initial_Condition_Proc (Id : Entity_Id) return Boolean;
1272 pragma Inline (Is_Initial_Condition_Proc);
1273 -- Determine whether arbitrary entity Id denotes internally generated
1274 -- routine Initial_Condition.
1275
1276 function Is_Initialized (Obj_Decl : Node_Id) return Boolean;
1277 pragma Inline (Is_Initialized);
1278 -- Determine whether object declaration Obj_Decl is initialized
1279
1280 function Is_Invariant_Proc (Id : Entity_Id) return Boolean;
1281 pragma Inline (Is_Invariant_Proc);
1282 -- Determine whether arbitrary entity Id denotes an invariant procedure
1283
1284 function Is_Non_Library_Level_Encapsulator (N : Node_Id) return Boolean;
1285 pragma Inline (Is_Non_Library_Level_Encapsulator);
1286 -- Determine whether arbitrary node N is a non-library encapsulator
1287
1288 function Is_Partial_Invariant_Proc (Id : Entity_Id) return Boolean;
1289 pragma Inline (Is_Partial_Invariant_Proc);
1290 -- Determine whether arbitrary entity Id denotes a partial invariant
1291 -- procedure.
1292
1293 function Is_Postconditions_Proc (Id : Entity_Id) return Boolean;
1294 pragma Inline (Is_Postconditions_Proc);
1295 -- Determine whether arbitrary entity Id denotes internally generated
1296 -- routine _Postconditions.
1297
1298 function Is_Preelaborated_Unit (Id : Entity_Id) return Boolean;
1299 pragma Inline (Is_Preelaborated_Unit);
1300 -- Determine whether arbitrary entity Id denotes a unit which is subject to
1301 -- one of the following pragmas:
1302 --
1303 -- * Preelaborable
1304 -- * Pure
1305 -- * Remote_Call_Interface
1306 -- * Remote_Types
1307 -- * Shared_Passive
1308
1309 function Is_Protected_Entry (Id : Entity_Id) return Boolean;
1310 pragma Inline (Is_Protected_Entry);
1311 -- Determine whether arbitrary entity Id denotes a protected entry
1312
1313 function Is_Protected_Subp (Id : Entity_Id) return Boolean;
1314 pragma Inline (Is_Protected_Subp);
1315 -- Determine whether entity Id denotes a protected subprogram
1316
1317 function Is_Protected_Body_Subp (Id : Entity_Id) return Boolean;
1318 pragma Inline (Is_Protected_Body_Subp);
1319 -- Determine whether entity Id denotes the protected or unprotected version
1320 -- of a protected subprogram.
1321
1322 function Is_Recorded_SPARK_Scenario (N : Node_Id) return Boolean;
1323 pragma Inline (Is_Recorded_SPARK_Scenario);
1324 -- Determine whether arbitrary node N is a recorded SPARK scenario which
1325 -- appears in table SPARK_Scenarios.
1326
1327 function Is_Recorded_Top_Level_Scenario (N : Node_Id) return Boolean;
1328 pragma Inline (Is_Recorded_Top_Level_Scenario);
1329 -- Determine whether arbitrary node N is a recorded top-level scenario
1330 -- which appears in table Top_Level_Scenarios.
1331
1332 function Is_Safe_Activation
1333 (Call : Node_Id;
1334 Task_Decl : Node_Id) return Boolean;
1335 pragma Inline (Is_Safe_Activation);
1336 -- Determine whether call Call which activates a task object described by
1337 -- declaration Task_Decl is always ABE-safe.
1338
1339 function Is_Safe_Call
1340 (Call : Node_Id;
1341 Target_Attrs : Target_Attributes) return Boolean;
1342 pragma Inline (Is_Safe_Call);
1343 -- Determine whether call Call which invokes a target described by
1344 -- attributes Target_Attrs is always ABE-safe.
1345
1346 function Is_Safe_Instantiation
1347 (Inst : Node_Id;
1348 Gen_Attrs : Target_Attributes) return Boolean;
1349 pragma Inline (Is_Safe_Instantiation);
1350 -- Determine whether instance Inst which instantiates a generic unit
1351 -- described by attributes Gen_Attrs is always ABE-safe.
1352
1353 function Is_Same_Unit
1354 (Unit_1 : Entity_Id;
1355 Unit_2 : Entity_Id) return Boolean;
1356 pragma Inline (Is_Same_Unit);
1357 -- Determine whether entities Unit_1 and Unit_2 denote the same unit
1358
1359 function Is_Scenario (N : Node_Id) return Boolean;
1360 pragma Inline (Is_Scenario);
1361 -- Determine whether attribute node N denotes a scenario. The scenario may
1362 -- not necessarily be eligible for ABE processing.
1363
1364 function Is_SPARK_Semantic_Target (Id : Entity_Id) return Boolean;
1365 pragma Inline (Is_SPARK_Semantic_Target);
1366 -- Determine whether arbitrary entity Id nodes a source or internally
1367 -- generated subprogram which emulates SPARK semantics.
1368
1369 function Is_Suitable_Access (N : Node_Id) return Boolean;
1370 pragma Inline (Is_Suitable_Access);
1371 -- Determine whether arbitrary node N denotes a suitable attribute for ABE
1372 -- processing.
1373
1374 function Is_Suitable_Call (N : Node_Id) return Boolean;
1375 pragma Inline (Is_Suitable_Call);
1376 -- Determine whether arbitrary node N denotes a suitable call for ABE
1377 -- processing.
1378
1379 function Is_Suitable_Instantiation (N : Node_Id) return Boolean;
1380 pragma Inline (Is_Suitable_Instantiation);
1381 -- Determine whether arbitrary node N is a suitable instantiation for ABE
1382 -- processing.
1383
1384 function Is_Suitable_Scenario (N : Node_Id) return Boolean;
1385 pragma Inline (Is_Suitable_Scenario);
1386 -- Determine whether arbitrary node N is a suitable scenario for ABE
1387 -- processing.
1388
1389 function Is_Suitable_SPARK_Derived_Type (N : Node_Id) return Boolean;
1390 pragma Inline (Is_Suitable_SPARK_Derived_Type);
1391 -- Determine whether arbitrary node N denotes a suitable derived type
1392 -- declaration for ABE processing using the SPARK rules.
1393
1394 function Is_Suitable_SPARK_Instantiation (N : Node_Id) return Boolean;
1395 pragma Inline (Is_Suitable_SPARK_Instantiation);
1396 -- Determine whether arbitrary node N denotes a suitable instantiation for
1397 -- ABE processing using the SPARK rules.
1398
1399 function Is_Suitable_SPARK_Refined_State_Pragma
1400 (N : Node_Id) return Boolean;
1401 pragma Inline (Is_Suitable_SPARK_Refined_State_Pragma);
1402 -- Determine whether arbitrary node N denotes a suitable Refined_State
1403 -- pragma for ABE processing using the SPARK rules.
1404
1405 function Is_Suitable_Variable_Assignment (N : Node_Id) return Boolean;
1406 pragma Inline (Is_Suitable_Variable_Assignment);
1407 -- Determine whether arbitrary node N denotes a suitable assignment for ABE
1408 -- processing.
1409
1410 function Is_Suitable_Variable_Reference (N : Node_Id) return Boolean;
1411 pragma Inline (Is_Suitable_Variable_Reference);
1412 -- Determine whether arbitrary node N is a suitable variable reference for
1413 -- ABE processing.
1414
1415 function Is_Task_Entry (Id : Entity_Id) return Boolean;
1416 pragma Inline (Is_Task_Entry);
1417 -- Determine whether arbitrary entity Id denotes a task entry
1418
1419 function Is_Up_Level_Target (Target_Decl : Node_Id) return Boolean;
1420 pragma Inline (Is_Up_Level_Target);
1421 -- Determine whether the current root resides at the declaration level. If
1422 -- this is the case, determine whether a target described by declaration
1423 -- Target_Decl is within a context which encloses the current root or is in
1424 -- a different unit.
1425
1426 function Is_Visited_Body (Body_Decl : Node_Id) return Boolean;
1427 pragma Inline (Is_Visited_Body);
1428 -- Determine whether subprogram body Body_Decl is already visited during a
1429 -- recursive traversal started from a top-level scenario.
1430
1431 procedure Meet_Elaboration_Requirement
1432 (N : Node_Id;
1433 Target_Id : Entity_Id;
1434 Req_Nam : Name_Id);
1435 -- Determine whether elaboration requirement Req_Nam for scenario N with
1436 -- target Target_Id is met by the context of the main unit using the SPARK
1437 -- rules. Req_Nam must denote either Elaborate or Elaborate_All. Emit an
1438 -- error if this is not the case.
1439
1440 function Non_Private_View (Typ : Entity_Id) return Entity_Id;
1441 pragma Inline (Non_Private_View);
1442 -- Return the full view of private type Typ if available, otherwise return
1443 -- type Typ.
1444
1445 procedure Output_Active_Scenarios (Error_Nod : Node_Id);
1446 -- Output the contents of the active scenario stack from earliest to latest
1447 -- to supplement an earlier error emitted for node Error_Nod.
1448
1449 procedure Pop_Active_Scenario (N : Node_Id);
1450 pragma Inline (Pop_Active_Scenario);
1451 -- Pop the top of the scenario stack. A check is made to ensure that the
1452 -- scenario being removed is the same as N.
1453
1454 generic
1455 with procedure Process_Single_Activation
1456 (Call : Node_Id;
1457 Call_Attrs : Call_Attributes;
1458 Obj_Id : Entity_Id;
1459 Task_Attrs : Task_Attributes;
1460 In_Init_Cond : Boolean;
1461 In_Partial_Fin : Boolean;
1462 In_Task_Body : Boolean);
1463 -- Perform ABE checks and diagnostics for task activation call Call
1464 -- which activates task Obj_Id. Call_Attrs are the attributes of the
1465 -- activation call. Task_Attrs are the attributes of the task type.
1466 -- The flags should be set when the processing was initiated as follows:
1467 --
1468 -- In_Init_Cond - initial condition procedure
1469 -- In_Partial_Fin - partial finalization procedure
1470 -- In_Task_Body - task body
1471
1472 procedure Process_Activation_Generic
1473 (Call : Node_Id;
1474 Call_Attrs : Call_Attributes;
1475 In_Init_Cond : Boolean;
1476 In_Partial_Fin : Boolean;
1477 In_Task_Body : Boolean);
1478 -- Perform ABE checks and diagnostics for activation call Call by invoking
1479 -- routine Process_Single_Activation on each task object being activated.
1480 -- Call_Attrs are the attributes of the activation call. The flags should
1481 -- be set when the processing was initiated as follows:
1482 --
1483 -- In_Init_Cond - initial condition procedure
1484 -- In_Partial_Fin - partial finalization procedure
1485 -- In_Task_Body - task body
1486
1487 procedure Process_Conditional_ABE
1488 (N : Node_Id;
1489 In_Init_Cond : Boolean := False;
1490 In_Partial_Fin : Boolean := False;
1491 In_Task_Body : Boolean := False);
1492 -- Top-level dispatcher for processing of various elaboration scenarios.
1493 -- Perform conditional ABE checks and diagnostics for scenario N. The flags
1494 -- should be set when the processing was initiated as follows:
1495 --
1496 -- In_Init_Cond - initial condition procedure
1497 -- In_Partial_Fin - partial finalization procedure
1498 -- In_Task_Body - task body
1499
1500 procedure Process_Conditional_ABE_Access
1501 (Attr : Node_Id;
1502 In_Init_Cond : Boolean;
1503 In_Partial_Fin : Boolean;
1504 In_Task_Body : Boolean);
1505 -- Perform ABE checks and diagnostics for 'Access to entry, operator, or
1506 -- subprogram denoted by Attr. The flags should be set when the processing
1507 -- was initiated as follows:
1508 --
1509 -- In_Init_Cond - initial condition procedure
1510 -- In_Partial_Fin - partial finalization procedure
1511 -- In_Task_Body - task body
1512
1513 procedure Process_Conditional_ABE_Activation_Impl
1514 (Call : Node_Id;
1515 Call_Attrs : Call_Attributes;
1516 Obj_Id : Entity_Id;
1517 Task_Attrs : Task_Attributes;
1518 In_Init_Cond : Boolean;
1519 In_Partial_Fin : Boolean;
1520 In_Task_Body : Boolean);
1521 -- Perform common conditional ABE checks and diagnostics for call Call
1522 -- which activates task Obj_Id ignoring the Ada or SPARK rules. CAll_Attrs
1523 -- are the attributes of the activation call. Task_Attrs are the attributes
1524 -- of the task type. The flags should be set when the processing was
1525 -- initiated as follows:
1526 --
1527 -- In_Init_Cond - initial condition procedure
1528 -- In_Partial_Fin - partial finalization procedure
1529 -- In_Task_Body - task body
1530
1531 procedure Process_Conditional_ABE_Call
1532 (Call : Node_Id;
1533 Call_Attrs : Call_Attributes;
1534 Target_Id : Entity_Id;
1535 In_Init_Cond : Boolean;
1536 In_Partial_Fin : Boolean;
1537 In_Task_Body : Boolean);
1538 -- Top-level dispatcher for processing of calls. Perform ABE checks and
1539 -- diagnostics for call Call which invokes target Target_Id. Call_Attrs
1540 -- are the attributes of the call. The flags should be set when the
1541 -- processing was initiated as follows:
1542 --
1543 -- In_Init_Cond - initial condition procedure
1544 -- In_Partial_Fin - partial finalization procedure
1545 -- In_Task_Body - task body
1546
1547 procedure Process_Conditional_ABE_Call_Ada
1548 (Call : Node_Id;
1549 Call_Attrs : Call_Attributes;
1550 Target_Id : Entity_Id;
1551 Target_Attrs : Target_Attributes;
1552 In_Partial_Fin : Boolean;
1553 In_Task_Body : Boolean);
1554 -- Perform ABE checks and diagnostics for call Call which invokes target
1555 -- Target_Id using the Ada rules. Call_Attrs are the attributes of the
1556 -- call. Target_Attrs are attributes of the target. The flags should be
1557 -- set when the processing was initiated as follows:
1558 --
1559 -- In_Partial_Fin - partial finalization procedure
1560 -- In_Task_Body - task body
1561
1562 procedure Process_Conditional_ABE_Call_SPARK
1563 (Call : Node_Id;
1564 Target_Id : Entity_Id;
1565 Target_Attrs : Target_Attributes;
1566 In_Init_Cond : Boolean;
1567 In_Partial_Fin : Boolean;
1568 In_Task_Body : Boolean);
1569 -- Perform ABE checks and diagnostics for call Call which invokes target
1570 -- Target_Id using the SPARK rules. Target_Attrs denotes the attributes of
1571 -- the target. The flags should be set when the processing was initiated as
1572 -- follows:
1573 --
1574 -- In_Init_Cond - initial condition procedure
1575 -- In_Partial_Fin - partial finalization procedure
1576 -- In_Task_Body - task body
1577
1578 procedure Process_Conditional_ABE_Instantiation
1579 (Exp_Inst : Node_Id;
1580 In_Partial_Fin : Boolean;
1581 In_Task_Body : Boolean);
1582 -- Top-level dispatcher for processing of instantiations. Perform ABE
1583 -- checks and diagnostics for expanded instantiation Exp_Inst. The flags
1584 -- should be set when the processing was initiated as follows:
1585 --
1586 -- In_Partial_Fin - partial finalization procedure
1587 -- In_Task_Body - task body
1588
1589 procedure Process_Conditional_ABE_Instantiation_Ada
1590 (Exp_Inst : Node_Id;
1591 Inst : Node_Id;
1592 Inst_Attrs : Instantiation_Attributes;
1593 Gen_Id : Entity_Id;
1594 Gen_Attrs : Target_Attributes;
1595 In_Partial_Fin : Boolean;
1596 In_Task_Body : Boolean);
1597 -- Perform ABE checks and diagnostics for expanded instantiation Exp_Inst
1598 -- of generic Gen_Id using the Ada rules. Inst is the instantiation node.
1599 -- Inst_Attrs are the attributes of the instance. Gen_Attrs denotes the
1600 -- attributes of the generic. The flags should be set when the processing
1601 -- was initiated as follows:
1602 --
1603 -- In_Partial_Fin - partial finalization procedure
1604 -- In_Task_Body - task body
1605
1606 procedure Process_Conditional_ABE_Instantiation_SPARK
1607 (Inst : Node_Id;
1608 Gen_Id : Entity_Id;
1609 Gen_Attrs : Target_Attributes;
1610 In_Partial_Fin : Boolean;
1611 In_Task_Body : Boolean);
1612 -- Perform ABE checks and diagnostics for instantiation Inst of generic
1613 -- Gen_Id using the SPARK rules. Gen_Attrs denotes the attributes of the
1614 -- generic. The flags should be set when the processing was initiated as
1615 -- follows:
1616 --
1617 -- In_Partial_Fin - partial finalization procedure
1618 -- In_Task_Body - task body
1619
1620 procedure Process_Conditional_ABE_Variable_Assignment (Asmt : Node_Id);
1621 -- Top-level dispatcher for processing of variable assignments. Perform ABE
1622 -- checks and diagnostics for assignment statement Asmt.
1623
1624 procedure Process_Conditional_ABE_Variable_Assignment_Ada
1625 (Asmt : Node_Id;
1626 Var_Id : Entity_Id);
1627 -- Perform ABE checks and diagnostics for assignment statement Asmt that
1628 -- updates the value of variable Var_Id using the Ada rules.
1629
1630 procedure Process_Conditional_ABE_Variable_Assignment_SPARK
1631 (Asmt : Node_Id;
1632 Var_Id : Entity_Id);
1633 -- Perform ABE checks and diagnostics for assignment statement Asmt that
1634 -- updates the value of variable Var_Id using the SPARK rules.
1635
1636 procedure Process_Conditional_ABE_Variable_Reference (Ref : Node_Id);
1637 -- Top-level dispatcher for processing of variable references. Perform ABE
1638 -- checks and diagnostics for variable reference Ref.
1639
1640 procedure Process_Conditional_ABE_Variable_Reference_Read
1641 (Ref : Node_Id;
1642 Var_Id : Entity_Id;
1643 Attrs : Variable_Attributes);
1644 -- Perform ABE checks and diagnostics for reference Ref described by its
1645 -- attributes Attrs, that reads variable Var_Id.
1646
1647 procedure Process_Guaranteed_ABE (N : Node_Id);
1648 -- Top-level dispatcher for processing of scenarios which result in a
1649 -- guaranteed ABE.
1650
1651 procedure Process_Guaranteed_ABE_Activation_Impl
1652 (Call : Node_Id;
1653 Call_Attrs : Call_Attributes;
1654 Obj_Id : Entity_Id;
1655 Task_Attrs : Task_Attributes;
1656 In_Init_Cond : Boolean;
1657 In_Partial_Fin : Boolean;
1658 In_Task_Body : Boolean);
1659 -- Perform common guaranteed ABE checks and diagnostics for call Call which
1660 -- activates task Obj_Id ignoring the Ada or SPARK rules. Task_Attrs are
1661 -- the attributes of the task type. The following parameters are provided
1662 -- for compatibility and are unused.
1663 --
1664 -- Call_Attrs
1665 -- In_Init_Cond
1666 -- In_Partial_Fin
1667 -- In_Task_Body
1668
1669 procedure Process_Guaranteed_ABE_Call
1670 (Call : Node_Id;
1671 Call_Attrs : Call_Attributes;
1672 Target_Id : Entity_Id);
1673 -- Perform common guaranteed ABE checks and diagnostics for call Call which
1674 -- invokes target Target_Id ignoring the Ada or SPARK rules. Call_Attrs are
1675 -- the attributes of the call.
1676
1677 procedure Process_Guaranteed_ABE_Instantiation (Exp_Inst : Node_Id);
1678 -- Perform common guaranteed ABE checks and diagnostics for expanded
1679 -- instantiation Exp_Inst of generic Gen_Id ignoring the Ada or SPARK
1680 -- rules.
1681
1682 procedure Push_Active_Scenario (N : Node_Id);
1683 pragma Inline (Push_Active_Scenario);
1684 -- Push scenario N on top of the scenario stack
1685
1686 procedure Record_SPARK_Elaboration_Scenario (N : Node_Id);
1687 pragma Inline (Record_SPARK_Elaboration_Scenario);
1688 -- Save SPARK scenario N in table SPARK_Scenarios for later processing
1689
1690 procedure Reset_Visited_Bodies;
1691 pragma Inline (Reset_Visited_Bodies);
1692 -- Clear the contents of table Visited_Bodies
1693
1694 function Root_Scenario return Node_Id;
1695 pragma Inline (Root_Scenario);
1696 -- Return the top-level scenario which started a recursive search for other
1697 -- scenarios. It is assumed that there is a valid top-level scenario on the
1698 -- active scenario stack.
1699
1700 procedure Set_Early_Call_Region (Body_Id : Entity_Id; Start : Node_Id);
1701 pragma Inline (Set_Early_Call_Region);
1702 -- Associate an early call region with begins at construct Start with entry
1703 -- or subprogram body Body_Id.
1704
1705 procedure Set_Elaboration_Status
1706 (Unit_Id : Entity_Id;
1707 Val : Elaboration_Attributes);
1708 pragma Inline (Set_Elaboration_Status);
1709 -- Associate an set of elaboration attributes with unit Unit_Id
1710
1711 procedure Set_Is_Recorded_SPARK_Scenario
1712 (N : Node_Id;
1713 Val : Boolean := True);
1714 pragma Inline (Set_Is_Recorded_SPARK_Scenario);
1715 -- Mark scenario N as being recorded in table SPARK_Scenarios
1716
1717 procedure Set_Is_Recorded_Top_Level_Scenario
1718 (N : Node_Id;
1719 Val : Boolean := True);
1720 pragma Inline (Set_Is_Recorded_Top_Level_Scenario);
1721 -- Mark scenario N as being recorded in table Top_Level_Scenarios
1722
1723 procedure Set_Is_Visited_Body (Subp_Body : Node_Id);
1724 pragma Inline (Set_Is_Visited_Body);
1725 -- Mark subprogram body Subp_Body as being visited during a recursive
1726 -- traversal started from a top-level scenario.
1727
1728 function Static_Elaboration_Checks return Boolean;
1729 pragma Inline (Static_Elaboration_Checks);
1730 -- Determine whether the static model is in effect
1731
1732 procedure Traverse_Body
1733 (N : Node_Id;
1734 In_Init_Cond : Boolean;
1735 In_Partial_Fin : Boolean;
1736 In_Task_Body : Boolean);
1737 -- Inspect the declarations and statements of subprogram body N for
1738 -- suitable elaboration scenarios and process them. The flags should
1739 -- be set when the processing was initiated as follows:
1740 --
1741 -- In_Init_Cond - initial condition procedure
1742 -- In_Partial_Fin - partial finalization procedure
1743 -- In_Task_Body - task body
1744
1745 procedure Update_Elaboration_Scenario (New_N : Node_Id; Old_N : Node_Id);
1746 pragma Inline (Update_Elaboration_Scenario);
1747 -- Update all relevant internal data structures when scenario Old_N is
1748 -- transformed into scenario New_N by Atree.Rewrite.
1749
1750 -----------------------
1751 -- Build_Call_Marker --
1752 -----------------------
1753
1754 procedure Build_Call_Marker (N : Node_Id) is
1755 function In_External_Context
1756 (Call : Node_Id;
1757 Target_Id : Entity_Id) return Boolean;
1758 pragma Inline (In_External_Context);
1759 -- Determine whether target Target_Id is external to call N which must
1760 -- reside within an instance.
1761
1762 function In_Premature_Context (Call : Node_Id) return Boolean;
1763 -- Determine whether call Call appears within a premature context
1764
1765 function Is_Bridge_Target (Id : Entity_Id) return Boolean;
1766 pragma Inline (Is_Bridge_Target);
1767 -- Determine whether arbitrary entity Id denotes a bridge target
1768
1769 function Is_Default_Expression (Call : Node_Id) return Boolean;
1770 pragma Inline (Is_Default_Expression);
1771 -- Determine whether call Call acts as the expression of a defaulted
1772 -- parameter within a source call.
1773
1774 function Is_Generic_Formal_Subp (Subp_Id : Entity_Id) return Boolean;
1775 pragma Inline (Is_Generic_Formal_Subp);
1776 -- Determine whether subprogram Subp_Id denotes a generic formal
1777 -- subprogram which appears in the "prologue" of an instantiation.
1778
1779 -------------------------
1780 -- In_External_Context --
1781 -------------------------
1782
1783 function In_External_Context
1784 (Call : Node_Id;
1785 Target_Id : Entity_Id) return Boolean
1786 is
1787 Target_Decl : constant Node_Id := Unit_Declaration_Node (Target_Id);
1788
1789 Inst : Node_Id;
1790 Inst_Body : Node_Id;
1791 Inst_Decl : Node_Id;
1792
1793 begin
1794 -- Performance note: parent traversal
1795
1796 Inst := Find_Enclosing_Instance (Call);
1797
1798 -- The call appears within an instance
1799
1800 if Present (Inst) then
1801
1802 -- The call comes from the main unit and the target does not
1803
1804 if In_Extended_Main_Code_Unit (Call)
1805 and then not In_Extended_Main_Code_Unit (Target_Decl)
1806 then
1807 return True;
1808
1809 -- Otherwise the target declaration must not appear within the
1810 -- instance spec or body.
1811
1812 else
1813 Extract_Instance_Attributes
1814 (Exp_Inst => Inst,
1815 Inst_Decl => Inst_Decl,
1816 Inst_Body => Inst_Body);
1817
1818 -- Performance note: parent traversal
1819
1820 return not In_Subtree
1821 (N => Target_Decl,
1822 Root1 => Inst_Decl,
1823 Root2 => Inst_Body);
1824 end if;
1825 end if;
1826
1827 return False;
1828 end In_External_Context;
1829
1830 --------------------------
1831 -- In_Premature_Context --
1832 --------------------------
1833
1834 function In_Premature_Context (Call : Node_Id) return Boolean is
1835 Par : Node_Id;
1836
1837 begin
1838 -- Climb the parent chain looking for premature contexts
1839
1840 Par := Parent (Call);
1841 while Present (Par) loop
1842
1843 -- Aspect specifications and generic associations are premature
1844 -- contexts because nested calls has not been relocated to their
1845 -- final context.
1846
1847 if Nkind_In (Par, N_Aspect_Specification,
1848 N_Generic_Association)
1849 then
1850 return True;
1851
1852 -- Prevent the search from going too far
1853
1854 elsif Is_Body_Or_Package_Declaration (Par) then
1855 exit;
1856 end if;
1857
1858 Par := Parent (Par);
1859 end loop;
1860
1861 return False;
1862 end In_Premature_Context;
1863
1864 ----------------------
1865 -- Is_Bridge_Target --
1866 ----------------------
1867
1868 function Is_Bridge_Target (Id : Entity_Id) return Boolean is
1869 begin
1870 return
1871 Is_Accept_Alternative_Proc (Id)
1872 or else Is_Finalizer_Proc (Id)
1873 or else Is_Partial_Invariant_Proc (Id)
1874 or else Is_Postconditions_Proc (Id)
1875 or else Is_TSS (Id, TSS_Deep_Adjust)
1876 or else Is_TSS (Id, TSS_Deep_Finalize)
1877 or else Is_TSS (Id, TSS_Deep_Initialize);
1878 end Is_Bridge_Target;
1879
1880 ---------------------------
1881 -- Is_Default_Expression --
1882 ---------------------------
1883
1884 function Is_Default_Expression (Call : Node_Id) return Boolean is
1885 Outer_Call : constant Node_Id := Parent (Call);
1886 Outer_Nam : Node_Id;
1887
1888 begin
1889 -- To qualify, the node must appear immediately within a source call
1890 -- which invokes a source target.
1891
1892 if Nkind_In (Outer_Call, N_Entry_Call_Statement,
1893 N_Function_Call,
1894 N_Procedure_Call_Statement)
1895 and then Comes_From_Source (Outer_Call)
1896 then
1897 Outer_Nam := Extract_Call_Name (Outer_Call);
1898
1899 return
1900 Is_Entity_Name (Outer_Nam)
1901 and then Present (Entity (Outer_Nam))
1902 and then Is_Subprogram_Or_Entry (Entity (Outer_Nam))
1903 and then Comes_From_Source (Entity (Outer_Nam));
1904 end if;
1905
1906 return False;
1907 end Is_Default_Expression;
1908
1909 ----------------------------
1910 -- Is_Generic_Formal_Subp --
1911 ----------------------------
1912
1913 function Is_Generic_Formal_Subp (Subp_Id : Entity_Id) return Boolean is
1914 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
1915 Context : constant Node_Id := Parent (Subp_Decl);
1916
1917 begin
1918 -- To qualify, the subprogram must rename a generic actual subprogram
1919 -- where the enclosing context is an instantiation.
1920
1921 return
1922 Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration
1923 and then not Comes_From_Source (Subp_Decl)
1924 and then Nkind_In (Context, N_Function_Specification,
1925 N_Package_Specification,
1926 N_Procedure_Specification)
1927 and then Present (Generic_Parent (Context));
1928 end Is_Generic_Formal_Subp;
1929
1930 -- Local variables
1931
1932 Call_Attrs : Call_Attributes;
1933 Call_Nam : Node_Id;
1934 Marker : Node_Id;
1935 Target_Id : Entity_Id;
1936
1937 -- Start of processing for Build_Call_Marker
1938
1939 begin
1940 -- Nothing to do for ASIS. As a result, ABE checks and diagnostics are
1941 -- not performed in this mode.
1942
1943 if ASIS_Mode then
1944 return;
1945
1946 -- Nothing to do when the call is being preanalyzed as the marker will
1947 -- be inserted in the wrong place.
1948
1949 elsif Preanalysis_Active then
1950 return;
1951
1952 -- Nothing to do when the input does not denote a call or a requeue
1953
1954 elsif not Nkind_In (N, N_Entry_Call_Statement,
1955 N_Function_Call,
1956 N_Procedure_Call_Statement,
1957 N_Requeue_Statement)
1958 then
1959 return;
1960
1961 -- Nothing to do when the call is analyzed/resolved too early within an
1962 -- intermediate context.
1963
1964 -- Performance note: parent traversal
1965
1966 elsif In_Premature_Context (N) then
1967 return;
1968 end if;
1969
1970 Call_Nam := Extract_Call_Name (N);
1971
1972 -- Nothing to do when the call is erroneous or left in a bad state
1973
1974 if not (Is_Entity_Name (Call_Nam)
1975 and then Present (Entity (Call_Nam))
1976 and then Is_Subprogram_Or_Entry (Entity (Call_Nam)))
1977 then
1978 return;
1979
1980 -- Nothing to do when the call invokes a generic formal subprogram and
1981 -- switch -gnatd.G (ignore calls through generic formal parameters for
1982 -- elaboration) is in effect. This check must be performed with the
1983 -- direct target of the call to avoid the side effects of mapping
1984 -- actuals to formals using renamings.
1985
1986 elsif Debug_Flag_Dot_GG
1987 and then Is_Generic_Formal_Subp (Entity (Call_Nam))
1988 then
1989 return;
1990 end if;
1991
1992 Extract_Call_Attributes
1993 (Call => N,
1994 Target_Id => Target_Id,
1995 Attrs => Call_Attrs);
1996
1997 -- Nothing to do when the call appears within the expanded spec or
1998 -- body of an instantiated generic, the call does not invoke a generic
1999 -- formal subprogram, the target is external to the instance, and switch
2000 -- -gnatdL (ignore external calls from instances for elaboration) is in
2001 -- effect. This behaviour approximates that of the old ABE mechanism.
2002
2003 if Debug_Flag_LL
2004 and then not Is_Generic_Formal_Subp (Entity (Call_Nam))
2005
2006 -- Performance note: parent traversal
2007
2008 and then In_External_Context
2009 (Call => N,
2010 Target_Id => Target_Id)
2011 then
2012 return;
2013
2014 -- Source calls to source targets are always considered because they
2015 -- reflect the original call graph.
2016
2017 elsif Comes_From_Source (Target_Id) and then Call_Attrs.From_Source then
2018 null;
2019
2020 -- A call to a source function which acts as the default expression in
2021 -- another call requires special detection.
2022
2023 elsif Comes_From_Source (Target_Id)
2024 and then Nkind (N) = N_Function_Call
2025 and then Is_Default_Expression (N)
2026 then
2027 null;
2028
2029 -- The target emulates Ada semantics
2030
2031 elsif Is_Ada_Semantic_Target (Target_Id) then
2032 null;
2033
2034 -- The target acts as a link between scenarios
2035
2036 elsif Is_Bridge_Target (Target_Id) then
2037 null;
2038
2039 -- The target emulates SPARK semantics
2040
2041 elsif Is_SPARK_Semantic_Target (Target_Id) then
2042 null;
2043
2044 -- Otherwise the call is not suitable for ABE processing. This prevents
2045 -- the generation of call markers which will never play a role in ABE
2046 -- diagnostics.
2047
2048 else
2049 return;
2050 end if;
2051
2052 -- At this point it is known that the call will play some role in ABE
2053 -- checks and diagnostics. Create a corresponding call marker in case
2054 -- the original call is heavily transformed by expansion later on.
2055
2056 Marker := Make_Call_Marker (Sloc (N));
2057
2058 -- Inherit the attributes of the original call
2059
2060 Set_Target (Marker, Target_Id);
2061 Set_Is_Elaboration_Checks_OK_Node (Marker, Call_Attrs.Elab_Checks_OK);
2062 Set_Is_Declaration_Level_Node (Marker, Call_Attrs.In_Declarations);
2063 Set_Is_Dispatching_Call (Marker, Call_Attrs.Is_Dispatching);
2064 Set_Is_Ignored_Ghost_Node (Marker, Call_Attrs.Ghost_Mode_Ignore);
2065 Set_Is_Source_Call (Marker, Call_Attrs.From_Source);
2066 Set_Is_SPARK_Mode_On_Node (Marker, Call_Attrs.SPARK_Mode_On);
2067
2068 -- The marker is inserted prior to the original call. This placement has
2069 -- several desirable effects:
2070
2071 -- 1) The marker appears in the same context, in close proximity to
2072 -- the call.
2073
2074 -- <marker>
2075 -- <call>
2076
2077 -- 2) Inserting the marker prior to the call ensures that an ABE check
2078 -- will take effect prior to the call.
2079
2080 -- <ABE check>
2081 -- <marker>
2082 -- <call>
2083
2084 -- 3) The above two properties are preserved even when the call is a
2085 -- function which is subsequently relocated in order to capture its
2086 -- result. Note that if the call is relocated to a new context, the
2087 -- relocated call will receive a marker of its own.
2088
2089 -- <ABE check>
2090 -- <maker>
2091 -- Temp : ... := Func_Call ...;
2092 -- ... Temp ...
2093
2094 -- The insertion must take place even when the call does not occur in
2095 -- the main unit to keep the tree symmetric. This ensures that internal
2096 -- name serialization is consistent in case the call marker causes the
2097 -- tree to transform in some way.
2098
2099 Insert_Action (N, Marker);
2100
2101 -- The marker becomes the "corresponding" scenario for the call. Save
2102 -- the marker for later processing by the ABE phase.
2103
2104 Record_Elaboration_Scenario (Marker);
2105 end Build_Call_Marker;
2106
2107 -------------------------------------
2108 -- Build_Variable_Reference_Marker --
2109 -------------------------------------
2110
2111 procedure Build_Variable_Reference_Marker
2112 (N : Node_Id;
2113 Read : Boolean;
2114 Write : Boolean)
2115 is
2116 function In_Pragma (Nod : Node_Id) return Boolean;
2117 -- Determine whether arbitrary node Nod appears within a pragma
2118
2119 ---------------
2120 -- In_Pragma --
2121 ---------------
2122
2123 function In_Pragma (Nod : Node_Id) return Boolean is
2124 Par : Node_Id;
2125
2126 begin
2127 Par := Nod;
2128 while Present (Par) loop
2129 if Nkind (Par) = N_Pragma then
2130 return True;
2131
2132 -- Prevent the search from going too far
2133
2134 elsif Is_Body_Or_Package_Declaration (Par) then
2135 exit;
2136 end if;
2137
2138 Par := Parent (Par);
2139 end loop;
2140
2141 return False;
2142 end In_Pragma;
2143
2144 -- Local variables
2145
2146 Marker : Node_Id;
2147 Prag : Node_Id;
2148 Var_Attrs : Variable_Attributes;
2149 Var_Id : Entity_Id;
2150
2151 -- Start of processing for Build_Variable_Reference_Marker
2152
2153 begin
2154 -- Nothing to do for ASIS. As a result, ABE checks and diagnostics are
2155 -- not performed in this mode.
2156
2157 if ASIS_Mode then
2158 return;
2159
2160 -- Nothing to do when the reference is being preanalyzed as the marker
2161 -- will be inserted in the wrong place.
2162
2163 elsif Preanalysis_Active then
2164 return;
2165
2166 -- Nothing to do when the input does not denote a reference
2167
2168 elsif not Nkind_In (N, N_Expanded_Name, N_Identifier) then
2169 return;
2170
2171 -- Nothing to do for internally-generated references
2172
2173 elsif not Comes_From_Source (N) then
2174 return;
2175
2176 -- Nothing to do when the reference is erroneous, left in a bad state,
2177 -- or does not denote a variable.
2178
2179 elsif not (Present (Entity (N))
2180 and then Ekind (Entity (N)) = E_Variable
2181 and then Entity (N) /= Any_Id)
2182 then
2183 return;
2184 end if;
2185
2186 Extract_Variable_Reference_Attributes
2187 (Ref => N,
2188 Var_Id => Var_Id,
2189 Attrs => Var_Attrs);
2190
2191 Prag := SPARK_Pragma (Var_Id);
2192
2193 if Comes_From_Source (Var_Id)
2194
2195 -- Both the variable and the reference must appear in SPARK_Mode On
2196 -- regions because this scenario falls under the SPARK rules.
2197
2198 and then Present (Prag)
2199 and then Get_SPARK_Mode_From_Annotation (Prag) = On
2200 and then Is_SPARK_Mode_On_Node (N)
2201
2202 -- The reference must not be considered when it appears in a pragma.
2203 -- If the pragma has run-time semantics, then the reference will be
2204 -- reconsidered once the pragma is expanded.
2205
2206 -- Performance note: parent traversal
2207
2208 and then not In_Pragma (N)
2209 then
2210 null;
2211
2212 -- Otherwise the reference is not suitable for ABE processing. This
2213 -- prevents the generation of variable markers which will never play
2214 -- a role in ABE diagnostics.
2215
2216 else
2217 return;
2218 end if;
2219
2220 -- At this point it is known that the variable reference will play some
2221 -- role in ABE checks and diagnostics. Create a corresponding variable
2222 -- marker in case the original variable reference is folded or optimized
2223 -- away.
2224
2225 Marker := Make_Variable_Reference_Marker (Sloc (N));
2226
2227 -- Inherit the attributes of the original variable reference
2228
2229 Set_Target (Marker, Var_Id);
2230 Set_Is_Read (Marker, Read);
2231 Set_Is_Write (Marker, Write);
2232
2233 -- The marker is inserted prior to the original variable reference. The
2234 -- insertion must take place even when the reference does not occur in
2235 -- the main unit to keep the tree symmetric. This ensures that internal
2236 -- name serialization is consistent in case the variable marker causes
2237 -- the tree to transform in some way.
2238
2239 Insert_Action (N, Marker);
2240
2241 -- The marker becomes the "corresponding" scenario for the reference.
2242 -- Save the marker for later processing for the ABE phase.
2243
2244 Record_Elaboration_Scenario (Marker);
2245 end Build_Variable_Reference_Marker;
2246
2247 ---------------------------------
2248 -- Check_Elaboration_Scenarios --
2249 ---------------------------------
2250
2251 procedure Check_Elaboration_Scenarios is
2252 begin
2253 -- Nothing to do for ASIS. As a result, no ABE checks and diagnostics
2254 -- are performed in this mode.
2255
2256 if ASIS_Mode then
2257 return;
2258 end if;
2259
2260 -- Examine the context of the main unit and record all units with prior
2261 -- elaboration with respect to it.
2262
2263 Find_Elaborated_Units;
2264
2265 -- Examine each top-level scenario saved during the Recording phase for
2266 -- conditional ABEs and perform various actions depending on the model
2267 -- in effect. The table of visited bodies is created for each new top-
2268 -- level scenario.
2269
2270 for Index in Top_Level_Scenarios.First .. Top_Level_Scenarios.Last loop
2271 Reset_Visited_Bodies;
2272
2273 Process_Conditional_ABE (Top_Level_Scenarios.Table (Index));
2274 end loop;
2275
2276 -- Examine each SPARK scenario saved during the Recording phase which
2277 -- is not necessarily executable during elaboration, but still requires
2278 -- elaboration-related checks.
2279
2280 for Index in SPARK_Scenarios.First .. SPARK_Scenarios.Last loop
2281 Check_SPARK_Scenario (SPARK_Scenarios.Table (Index));
2282 end loop;
2283 end Check_Elaboration_Scenarios;
2284
2285 ------------------------------
2286 -- Check_Preelaborated_Call --
2287 ------------------------------
2288
2289 procedure Check_Preelaborated_Call (Call : Node_Id) is
2290 function In_Preelaborated_Context (N : Node_Id) return Boolean;
2291 -- Determine whether arbitrary node appears in a preelaborated context
2292
2293 ------------------------------
2294 -- In_Preelaborated_Context --
2295 ------------------------------
2296
2297 function In_Preelaborated_Context (N : Node_Id) return Boolean is
2298 Body_Id : constant Entity_Id := Find_Code_Unit (N);
2299 Spec_Id : constant Entity_Id := Unique_Entity (Body_Id);
2300
2301 begin
2302 -- The node appears within a package body whose corresponding spec is
2303 -- subject to pragma Remote_Call_Interface or Remote_Types. This does
2304 -- not result in a preelaborated context because the package body may
2305 -- be on another machine.
2306
2307 if Ekind (Body_Id) = E_Package_Body
2308 and then Ekind_In (Spec_Id, E_Generic_Package, E_Package)
2309 and then (Is_Remote_Call_Interface (Spec_Id)
2310 or else Is_Remote_Types (Spec_Id))
2311 then
2312 return False;
2313
2314 -- Otherwise the node appears within a preelaborated context when the
2315 -- associated unit is preelaborated.
2316
2317 else
2318 return Is_Preelaborated_Unit (Spec_Id);
2319 end if;
2320 end In_Preelaborated_Context;
2321
2322 -- Local variables
2323
2324 Call_Attrs : Call_Attributes;
2325 Level : Enclosing_Level_Kind;
2326 Target_Id : Entity_Id;
2327
2328 -- Start of processing for Check_Preelaborated_Call
2329
2330 begin
2331 Extract_Call_Attributes
2332 (Call => Call,
2333 Target_Id => Target_Id,
2334 Attrs => Call_Attrs);
2335
2336 -- Nothing to do when the call is internally generated because it is
2337 -- assumed that it will never violate preelaboration.
2338
2339 if not Call_Attrs.From_Source then
2340 return;
2341 end if;
2342
2343 -- Performance note: parent traversal
2344
2345 Level := Find_Enclosing_Level (Call);
2346
2347 -- Library-level calls are always considered because they are part of
2348 -- the associated unit's elaboration actions.
2349
2350 if Level in Library_Level then
2351 null;
2352
2353 -- Calls at the library level of a generic package body must be checked
2354 -- because they would render an instantiation illegal if the template is
2355 -- marked as preelaborated. Note that this does not apply to calls at
2356 -- the library level of a generic package spec.
2357
2358 elsif Level = Generic_Package_Body then
2359 null;
2360
2361 -- Otherwise the call does not appear at the proper level and must not
2362 -- be considered for this check.
2363
2364 else
2365 return;
2366 end if;
2367
2368 -- The call appears within a preelaborated unit. Emit a warning only for
2369 -- internal uses, otherwise this is an error.
2370
2371 if In_Preelaborated_Context (Call) then
2372 Error_Msg_Warn := GNAT_Mode;
2373 Error_Msg_N
2374 ("<<non-static call not allowed in preelaborated unit", Call);
2375 end if;
2376 end Check_Preelaborated_Call;
2377
2378 ------------------------------
2379 -- Check_SPARK_Derived_Type --
2380 ------------------------------
2381
2382 procedure Check_SPARK_Derived_Type (Typ_Decl : Node_Id) is
2383 Typ : constant Entity_Id := Defining_Entity (Typ_Decl);
2384
2385 -- NOTE: The routines within Check_SPARK_Derived_Type are intentionally
2386 -- unnested to avoid deep indentation of code.
2387
2388 Stop_Check : exception;
2389 -- This exception is raised when the freeze node violates the placement
2390 -- rules.
2391
2392 procedure Check_Overriding_Primitive
2393 (Prim : Entity_Id;
2394 FNode : Node_Id);
2395 pragma Inline (Check_Overriding_Primitive);
2396 -- Verify that freeze node FNode is within the early call region of
2397 -- overriding primitive Prim's body.
2398
2399 function Freeze_Node_Location (FNode : Node_Id) return Source_Ptr;
2400 pragma Inline (Freeze_Node_Location);
2401 -- Return a more accurate source location associated with freeze node
2402 -- FNode.
2403
2404 function Precedes_Source_Construct (N : Node_Id) return Boolean;
2405 pragma Inline (Precedes_Source_Construct);
2406 -- Determine whether arbitrary node N appears prior to some source
2407 -- construct.
2408
2409 procedure Suggest_Elaborate_Body
2410 (N : Node_Id;
2411 Body_Decl : Node_Id;
2412 Error_Nod : Node_Id);
2413 pragma Inline (Suggest_Elaborate_Body);
2414 -- Suggest the use of pragma Elaborate_Body when the pragma will allow
2415 -- for node N to appear within the early call region of subprogram body
2416 -- Body_Decl. The suggestion is attached to Error_Nod as a continuation
2417 -- error.
2418
2419 --------------------------------
2420 -- Check_Overriding_Primitive --
2421 --------------------------------
2422
2423 procedure Check_Overriding_Primitive
2424 (Prim : Entity_Id;
2425 FNode : Node_Id)
2426 is
2427 Prim_Decl : constant Node_Id := Unit_Declaration_Node (Prim);
2428 Body_Decl : Node_Id;
2429 Body_Id : Entity_Id;
2430 Region : Node_Id;
2431
2432 begin
2433 Body_Id := Corresponding_Body (Prim_Decl);
2434
2435 -- Nothing to do when the primitive does not have a corresponding
2436 -- body. This can happen when the unit with the bodies is not the
2437 -- main unit subjected to ABE checks.
2438
2439 if No (Body_Id) then
2440 return;
2441
2442 -- The primitive overrides a parent or progenitor primitive
2443
2444 elsif Present (Overridden_Operation (Prim)) then
2445
2446 -- Nothing to do when overriding an interface primitive happens by
2447 -- inheriting a non-interface primitive as the check would be done
2448 -- on the parent primitive.
2449
2450 if Present (Alias (Prim)) then
2451 return;
2452 end if;
2453
2454 -- Nothing to do when the primitive is not overriding. The body of
2455 -- such a primitive cannot be targeted by a dispatching call which
2456 -- is executable during elaboration, and cannot cause an ABE.
2457
2458 else
2459 return;
2460 end if;
2461
2462 Body_Decl := Unit_Declaration_Node (Body_Id);
2463 Region := Find_Early_Call_Region (Body_Decl);
2464
2465 -- The freeze node appears prior to the early call region of the
2466 -- primitive body.
2467
2468 -- IMPORTANT: This check must always be performed even when -gnatd.v
2469 -- (enforce SPARK elaboration rules in SPARK code) is not specified
2470 -- because the static model cannot guarantee the absence of ABEs in
2471 -- in the presence of dispatching calls.
2472
2473 if Earlier_In_Extended_Unit (FNode, Region) then
2474 Error_Msg_Node_2 := Prim;
2475 Error_Msg_NE
2476 ("first freezing point of type & must appear within early call "
2477 & "region of primitive body & (SPARK RM 7.7(8))",
2478 Typ_Decl, Typ);
2479
2480 Error_Msg_Sloc := Sloc (Region);
2481 Error_Msg_N ("\region starts #", Typ_Decl);
2482
2483 Error_Msg_Sloc := Sloc (Body_Decl);
2484 Error_Msg_N ("\region ends #", Typ_Decl);
2485
2486 Error_Msg_Sloc := Freeze_Node_Location (FNode);
2487 Error_Msg_N ("\first freezing point #", Typ_Decl);
2488
2489 -- If applicable, suggest the use of pragma Elaborate_Body in the
2490 -- associated package spec.
2491
2492 Suggest_Elaborate_Body
2493 (N => FNode,
2494 Body_Decl => Body_Decl,
2495 Error_Nod => Typ_Decl);
2496
2497 raise Stop_Check;
2498 end if;
2499 end Check_Overriding_Primitive;
2500
2501 --------------------------
2502 -- Freeze_Node_Location --
2503 --------------------------
2504
2505 function Freeze_Node_Location (FNode : Node_Id) return Source_Ptr is
2506 Context : constant Node_Id := Parent (FNode);
2507 Loc : constant Source_Ptr := Sloc (FNode);
2508
2509 Prv_Decls : List_Id;
2510 Vis_Decls : List_Id;
2511
2512 begin
2513 -- In general, the source location of the freeze node is as close as
2514 -- possible to the real freeze point, except when the freeze node is
2515 -- at the "bottom" of a package spec.
2516
2517 if Nkind (Context) = N_Package_Specification then
2518 Prv_Decls := Private_Declarations (Context);
2519 Vis_Decls := Visible_Declarations (Context);
2520
2521 -- The freeze node appears in the private declarations of the
2522 -- package.
2523
2524 if Present (Prv_Decls)
2525 and then List_Containing (FNode) = Prv_Decls
2526 then
2527 null;
2528
2529 -- The freeze node appears in the visible declarations of the
2530 -- package and there are no private declarations.
2531
2532 elsif Present (Vis_Decls)
2533 and then List_Containing (FNode) = Vis_Decls
2534 and then (No (Prv_Decls) or else Is_Empty_List (Prv_Decls))
2535 then
2536 null;
2537
2538 -- Otherwise the freeze node is not in the "last" declarative list
2539 -- of the package. Use the existing source location of the freeze
2540 -- node.
2541
2542 else
2543 return Loc;
2544 end if;
2545
2546 -- The freeze node appears at the "bottom" of the package when it
2547 -- is in the "last" declarative list and is either the last in the
2548 -- list or is followed by internal constructs only. In that case
2549 -- the more appropriate source location is that of the package end
2550 -- label.
2551
2552 if not Precedes_Source_Construct (FNode) then
2553 return Sloc (End_Label (Context));
2554 end if;
2555 end if;
2556
2557 return Loc;
2558 end Freeze_Node_Location;
2559
2560 -------------------------------
2561 -- Precedes_Source_Construct --
2562 -------------------------------
2563
2564 function Precedes_Source_Construct (N : Node_Id) return Boolean is
2565 Decl : Node_Id;
2566
2567 begin
2568 Decl := Next (N);
2569 while Present (Decl) loop
2570 if Comes_From_Source (Decl) then
2571 return True;
2572
2573 -- A generated body for a source expression function is treated as
2574 -- a source construct.
2575
2576 elsif Nkind (Decl) = N_Subprogram_Body
2577 and then Was_Expression_Function (Decl)
2578 and then Comes_From_Source (Original_Node (Decl))
2579 then
2580 return True;
2581 end if;
2582
2583 Next (Decl);
2584 end loop;
2585
2586 return False;
2587 end Precedes_Source_Construct;
2588
2589 ----------------------------
2590 -- Suggest_Elaborate_Body --
2591 ----------------------------
2592
2593 procedure Suggest_Elaborate_Body
2594 (N : Node_Id;
2595 Body_Decl : Node_Id;
2596 Error_Nod : Node_Id)
2597 is
2598 Unt : constant Node_Id := Unit (Cunit (Main_Unit));
2599 Region : Node_Id;
2600
2601 begin
2602 -- The suggestion applies only when the subprogram body resides in a
2603 -- compilation package body, and a pragma Elaborate_Body would allow
2604 -- for the node to appear in the early call region of the subprogram
2605 -- body. This implies that all code from the subprogram body up to
2606 -- the node is preelaborable.
2607
2608 if Nkind (Unt) = N_Package_Body then
2609
2610 -- Find the start of the early call region again assuming that the
2611 -- package spec has pragma Elaborate_Body. Note that the internal
2612 -- data structures are intentionally not updated because this is a
2613 -- speculative search.
2614
2615 Region :=
2616 Find_Early_Call_Region
2617 (Body_Decl => Body_Decl,
2618 Assume_Elab_Body => True,
2619 Skip_Memoization => True);
2620
2621 -- If the node appears within the early call region, assuming that
2622 -- the package spec carries pragma Elaborate_Body, then it is safe
2623 -- to suggest the pragma.
2624
2625 if Earlier_In_Extended_Unit (Region, N) then
2626 Error_Msg_Name_1 := Name_Elaborate_Body;
2627 Error_Msg_NE
2628 ("\consider adding pragma % in spec of unit &",
2629 Error_Nod, Defining_Entity (Unt));
2630 end if;
2631 end if;
2632 end Suggest_Elaborate_Body;
2633
2634 -- Local variables
2635
2636 FNode : constant Node_Id := Freeze_Node (Typ);
2637 Prims : constant Elist_Id := Direct_Primitive_Operations (Typ);
2638
2639 Prim_Elmt : Elmt_Id;
2640
2641 -- Start of processing for Check_SPARK_Derived_Type
2642
2643 begin
2644 -- A type should have its freeze node set by the time SPARK scenarios
2645 -- are being verified.
2646
2647 pragma Assert (Present (FNode));
2648
2649 -- Verify that the freeze node of the derived type is within the early
2650 -- call region of each overriding primitive body (SPARK RM 7.7(8)).
2651
2652 if Present (Prims) then
2653 Prim_Elmt := First_Elmt (Prims);
2654 while Present (Prim_Elmt) loop
2655 Check_Overriding_Primitive
2656 (Prim => Node (Prim_Elmt),
2657 FNode => FNode);
2658
2659 Next_Elmt (Prim_Elmt);
2660 end loop;
2661 end if;
2662
2663 exception
2664 when Stop_Check =>
2665 null;
2666 end Check_SPARK_Derived_Type;
2667
2668 -------------------------------
2669 -- Check_SPARK_Instantiation --
2670 -------------------------------
2671
2672 procedure Check_SPARK_Instantiation (Exp_Inst : Node_Id) is
2673 Gen_Attrs : Target_Attributes;
2674 Gen_Id : Entity_Id;
2675 Inst : Node_Id;
2676 Inst_Attrs : Instantiation_Attributes;
2677 Inst_Id : Entity_Id;
2678
2679 begin
2680 Extract_Instantiation_Attributes
2681 (Exp_Inst => Exp_Inst,
2682 Inst => Inst,
2683 Inst_Id => Inst_Id,
2684 Gen_Id => Gen_Id,
2685 Attrs => Inst_Attrs);
2686
2687 Extract_Target_Attributes (Gen_Id, Gen_Attrs);
2688
2689 -- The instantiation and the generic body are both in the main unit
2690
2691 if Present (Gen_Attrs.Body_Decl)
2692 and then In_Extended_Main_Code_Unit (Gen_Attrs.Body_Decl)
2693
2694 -- If the instantiation appears prior to the generic body, then the
2695 -- instantiation is illegal (SPARK RM 7.7(6)).
2696
2697 -- IMPORTANT: This check must always be performed even when -gnatd.v
2698 -- (enforce SPARK elaboration rules in SPARK code) is not specified
2699 -- because the rule prevents use-before-declaration of objects that
2700 -- may precede the generic body.
2701
2702 and then Earlier_In_Extended_Unit (Inst, Gen_Attrs.Body_Decl)
2703 then
2704 Error_Msg_NE ("cannot instantiate & before body seen", Inst, Gen_Id);
2705 end if;
2706 end Check_SPARK_Instantiation;
2707
2708 --------------------------
2709 -- Check_SPARK_Scenario --
2710 --------------------------
2711
2712 procedure Check_SPARK_Scenario (N : Node_Id) is
2713 begin
2714 -- Add the current scenario to the stack of active scenarios
2715
2716 Push_Active_Scenario (N);
2717
2718 if Is_Suitable_SPARK_Derived_Type (N) then
2719 Check_SPARK_Derived_Type (N);
2720
2721 elsif Is_Suitable_SPARK_Instantiation (N) then
2722 Check_SPARK_Instantiation (N);
2723
2724 elsif Is_Suitable_SPARK_Refined_State_Pragma (N) then
2725 Check_SPARK_Refined_State_Pragma (N);
2726 end if;
2727
2728 -- Remove the current scenario from the stack of active scenarios once
2729 -- all ABE diagnostics and checks have been performed.
2730
2731 Pop_Active_Scenario (N);
2732 end Check_SPARK_Scenario;
2733
2734 --------------------------------------
2735 -- Check_SPARK_Refined_State_Pragma --
2736 --------------------------------------
2737
2738 procedure Check_SPARK_Refined_State_Pragma (N : Node_Id) is
2739
2740 -- NOTE: The routines within Check_SPARK_Refined_State_Pragma are
2741 -- intentionally unnested to avoid deep indentation of code.
2742
2743 procedure Check_SPARK_Constituent (Constit_Id : Entity_Id);
2744 pragma Inline (Check_SPARK_Constituent);
2745 -- Ensure that a single constituent Constit_Id is elaborated prior to
2746 -- the main unit.
2747
2748 procedure Check_SPARK_Constituents (Constits : Elist_Id);
2749 pragma Inline (Check_SPARK_Constituents);
2750 -- Ensure that all constituents found in list Constits are elaborated
2751 -- prior to the main unit.
2752
2753 procedure Check_SPARK_Initialized_State (State : Node_Id);
2754 pragma Inline (Check_SPARK_Initialized_State);
2755 -- Ensure that the constituents of single abstract state State are
2756 -- elaborated prior to the main unit.
2757
2758 procedure Check_SPARK_Initialized_States (Pack_Id : Entity_Id);
2759 pragma Inline (Check_SPARK_Initialized_States);
2760 -- Ensure that the constituents of all abstract states which appear in
2761 -- the Initializes pragma of package Pack_Id are elaborated prior to the
2762 -- main unit.
2763
2764 -----------------------------
2765 -- Check_SPARK_Constituent --
2766 -----------------------------
2767
2768 procedure Check_SPARK_Constituent (Constit_Id : Entity_Id) is
2769 Prag : Node_Id;
2770
2771 begin
2772 -- Nothing to do for "null" constituents
2773
2774 if Nkind (Constit_Id) = N_Null then
2775 return;
2776
2777 -- Nothing to do for illegal constituents
2778
2779 elsif Error_Posted (Constit_Id) then
2780 return;
2781 end if;
2782
2783 Prag := SPARK_Pragma (Constit_Id);
2784
2785 -- The check applies only when the constituent is subject to pragma
2786 -- SPARK_Mode On.
2787
2788 if Present (Prag)
2789 and then Get_SPARK_Mode_From_Annotation (Prag) = On
2790 then
2791 -- An external constituent of an abstract state which appears in
2792 -- the Initializes pragma of a package spec imposes an Elaborate
2793 -- requirement on the context of the main unit. Determine whether
2794 -- the context has a pragma strong enough to meet the requirement.
2795
2796 -- IMPORTANT: This check is performed only when -gnatd.v (enforce
2797 -- SPARK elaboration rules in SPARK code) is in effect because the
2798 -- static model can ensure the prior elaboration of the unit which
2799 -- contains a constituent by installing implicit Elaborate pragma.
2800
2801 if Debug_Flag_Dot_V then
2802 Meet_Elaboration_Requirement
2803 (N => N,
2804 Target_Id => Constit_Id,
2805 Req_Nam => Name_Elaborate);
2806
2807 -- Otherwise ensure that the unit with the external constituent is
2808 -- elaborated prior to the main unit.
2809
2810 else
2811 Ensure_Prior_Elaboration
2812 (N => N,
2813 Unit_Id => Find_Top_Unit (Constit_Id),
2814 Prag_Nam => Name_Elaborate,
2815 In_Partial_Fin => False,
2816 In_Task_Body => False);
2817 end if;
2818 end if;
2819 end Check_SPARK_Constituent;
2820
2821 ------------------------------
2822 -- Check_SPARK_Constituents --
2823 ------------------------------
2824
2825 procedure Check_SPARK_Constituents (Constits : Elist_Id) is
2826 Constit_Elmt : Elmt_Id;
2827
2828 begin
2829 if Present (Constits) then
2830 Constit_Elmt := First_Elmt (Constits);
2831 while Present (Constit_Elmt) loop
2832 Check_SPARK_Constituent (Node (Constit_Elmt));
2833 Next_Elmt (Constit_Elmt);
2834 end loop;
2835 end if;
2836 end Check_SPARK_Constituents;
2837
2838 -----------------------------------
2839 -- Check_SPARK_Initialized_State --
2840 -----------------------------------
2841
2842 procedure Check_SPARK_Initialized_State (State : Node_Id) is
2843 Prag : Node_Id;
2844 State_Id : Entity_Id;
2845
2846 begin
2847 -- Nothing to do for "null" initialization items
2848
2849 if Nkind (State) = N_Null then
2850 return;
2851
2852 -- Nothing to do for illegal states
2853
2854 elsif Error_Posted (State) then
2855 return;
2856 end if;
2857
2858 State_Id := Entity_Of (State);
2859
2860 -- Sanitize the state
2861
2862 if No (State_Id) then
2863 return;
2864
2865 elsif Error_Posted (State_Id) then
2866 return;
2867
2868 elsif Ekind (State_Id) /= E_Abstract_State then
2869 return;
2870 end if;
2871
2872 -- The check is performed only when the abstract state is subject to
2873 -- SPARK_Mode On.
2874
2875 Prag := SPARK_Pragma (State_Id);
2876
2877 if Present (Prag)
2878 and then Get_SPARK_Mode_From_Annotation (Prag) = On
2879 then
2880 Check_SPARK_Constituents (Refinement_Constituents (State_Id));
2881 end if;
2882 end Check_SPARK_Initialized_State;
2883
2884 ------------------------------------
2885 -- Check_SPARK_Initialized_States --
2886 ------------------------------------
2887
2888 procedure Check_SPARK_Initialized_States (Pack_Id : Entity_Id) is
2889 Prag : constant Node_Id := Get_Pragma (Pack_Id, Pragma_Initializes);
2890 Init : Node_Id;
2891 Inits : Node_Id;
2892
2893 begin
2894 if Present (Prag) then
2895 Inits := Expression (Get_Argument (Prag, Pack_Id));
2896
2897 -- Avoid processing a "null" initialization list. The only other
2898 -- alternative is an aggregate.
2899
2900 if Nkind (Inits) = N_Aggregate then
2901
2902 -- The initialization items appear in list form:
2903 --
2904 -- (state1, state2)
2905
2906 if Present (Expressions (Inits)) then
2907 Init := First (Expressions (Inits));
2908 while Present (Init) loop
2909 Check_SPARK_Initialized_State (Init);
2910 Next (Init);
2911 end loop;
2912 end if;
2913
2914 -- The initialization items appear in associated form:
2915 --
2916 -- (state1 => item1,
2917 -- state2 => (item2, item3))
2918
2919 if Present (Component_Associations (Inits)) then
2920 Init := First (Component_Associations (Inits));
2921 while Present (Init) loop
2922 Check_SPARK_Initialized_State (Init);
2923 Next (Init);
2924 end loop;
2925 end if;
2926 end if;
2927 end if;
2928 end Check_SPARK_Initialized_States;
2929
2930 -- Local variables
2931
2932 Pack_Body : constant Node_Id := Find_Related_Package_Or_Body (N);
2933
2934 -- Start of processing for Check_SPARK_Refined_State_Pragma
2935
2936 begin
2937 -- Pragma Refined_State must be associated with a package body
2938
2939 pragma Assert
2940 (Present (Pack_Body) and then Nkind (Pack_Body) = N_Package_Body);
2941
2942 -- Verify that each external contitunent of an abstract state mentioned
2943 -- in pragma Initializes is properly elaborated.
2944
2945 Check_SPARK_Initialized_States (Unique_Defining_Entity (Pack_Body));
2946 end Check_SPARK_Refined_State_Pragma;
2947
2948 ----------------------
2949 -- Compilation_Unit --
2950 ----------------------
2951
2952 function Compilation_Unit (Unit_Id : Entity_Id) return Node_Id is
2953 Comp_Unit : Node_Id;
2954
2955 begin
2956 Comp_Unit := Parent (Unit_Id);
2957
2958 -- Handle the case where a concurrent subunit is rewritten as a null
2959 -- statement due to expansion activities.
2960
2961 if Nkind (Comp_Unit) = N_Null_Statement
2962 and then Nkind_In (Original_Node (Comp_Unit), N_Protected_Body,
2963 N_Task_Body)
2964 then
2965 Comp_Unit := Parent (Comp_Unit);
2966 pragma Assert (Nkind (Comp_Unit) = N_Subunit);
2967
2968 -- Otherwise use the declaration node of the unit
2969
2970 else
2971 Comp_Unit := Parent (Unit_Declaration_Node (Unit_Id));
2972 end if;
2973
2974 -- Handle the case where a subprogram instantiation which acts as a
2975 -- compilation unit is expanded into an anonymous package that wraps
2976 -- the instantiated subprogram.
2977
2978 if Nkind (Comp_Unit) = N_Package_Specification
2979 and then Nkind_In (Original_Node (Parent (Comp_Unit)),
2980 N_Function_Instantiation,
2981 N_Procedure_Instantiation)
2982 then
2983 Comp_Unit := Parent (Parent (Comp_Unit));
2984
2985 -- Handle the case where the compilation unit is a subunit
2986
2987 elsif Nkind (Comp_Unit) = N_Subunit then
2988 Comp_Unit := Parent (Comp_Unit);
2989 end if;
2990
2991 pragma Assert (Nkind (Comp_Unit) = N_Compilation_Unit);
2992
2993 return Comp_Unit;
2994 end Compilation_Unit;
2995
2996 -----------------------
2997 -- Early_Call_Region --
2998 -----------------------
2999
3000 function Early_Call_Region (Body_Id : Entity_Id) return Node_Id is
3001 begin
3002 pragma Assert (Ekind_In (Body_Id, E_Entry,
3003 E_Entry_Family,
3004 E_Function,
3005 E_Procedure,
3006 E_Subprogram_Body));
3007
3008 if Early_Call_Regions_In_Use then
3009 return Early_Call_Regions.Get (Body_Id);
3010 end if;
3011
3012 return Early_Call_Regions_No_Element;
3013 end Early_Call_Region;
3014
3015 -----------------------------
3016 -- Early_Call_Regions_Hash --
3017 -----------------------------
3018
3019 function Early_Call_Regions_Hash
3020 (Key : Entity_Id) return Early_Call_Regions_Index
3021 is
3022 begin
3023 return Early_Call_Regions_Index (Key mod Early_Call_Regions_Max);
3024 end Early_Call_Regions_Hash;
3025
3026 -----------------
3027 -- Elab_Msg_NE --
3028 -----------------
3029
3030 procedure Elab_Msg_NE
3031 (Msg : String;
3032 N : Node_Id;
3033 Id : Entity_Id;
3034 Info_Msg : Boolean;
3035 In_SPARK : Boolean)
3036 is
3037 function Prefix return String;
3038 -- Obtain the prefix of the message
3039
3040 function Suffix return String;
3041 -- Obtain the suffix of the message
3042
3043 ------------
3044 -- Prefix --
3045 ------------
3046
3047 function Prefix return String is
3048 begin
3049 if Info_Msg then
3050 return "info: ";
3051 else
3052 return "";
3053 end if;
3054 end Prefix;
3055
3056 ------------
3057 -- Suffix --
3058 ------------
3059
3060 function Suffix return String is
3061 begin
3062 if In_SPARK then
3063 return " in SPARK";
3064 else
3065 return "";
3066 end if;
3067 end Suffix;
3068
3069 -- Start of processing for Elab_Msg_NE
3070
3071 begin
3072 Error_Msg_NE (Prefix & Msg & Suffix, N, Id);
3073 end Elab_Msg_NE;
3074
3075 ------------------------
3076 -- Elaboration_Status --
3077 ------------------------
3078
3079 function Elaboration_Status
3080 (Unit_Id : Entity_Id) return Elaboration_Attributes
3081 is
3082 begin
3083 if Elaboration_Statuses_In_Use then
3084 return Elaboration_Statuses.Get (Unit_Id);
3085 end if;
3086
3087 return Elaboration_Statuses_No_Element;
3088 end Elaboration_Status;
3089
3090 -------------------------------
3091 -- Elaboration_Statuses_Hash --
3092 -------------------------------
3093
3094 function Elaboration_Statuses_Hash
3095 (Key : Entity_Id) return Elaboration_Statuses_Index
3096 is
3097 begin
3098 return Elaboration_Statuses_Index (Key mod Elaboration_Statuses_Max);
3099 end Elaboration_Statuses_Hash;
3100
3101 ------------------------------
3102 -- Ensure_Prior_Elaboration --
3103 ------------------------------
3104
3105 procedure Ensure_Prior_Elaboration
3106 (N : Node_Id;
3107 Unit_Id : Entity_Id;
3108 Prag_Nam : Name_Id;
3109 In_Partial_Fin : Boolean;
3110 In_Task_Body : Boolean)
3111 is
3112 begin
3113 pragma Assert (Nam_In (Prag_Nam, Name_Elaborate, Name_Elaborate_All));
3114
3115 -- Nothing to do when the need for prior elaboration came from a partial
3116 -- finalization routine which occurs in an initialization context. This
3117 -- behaviour parallels that of the old ABE mechanism.
3118
3119 if In_Partial_Fin then
3120 return;
3121
3122 -- Nothing to do when the need for prior elaboration came from a task
3123 -- body and switch -gnatd.y (disable implicit pragma Elaborate_All on
3124 -- task bodies) is in effect.
3125
3126 elsif Debug_Flag_Dot_Y and then In_Task_Body then
3127 return;
3128
3129 -- Nothing to do when the unit is elaborated prior to the main unit.
3130 -- This check must also consider the following cases:
3131
3132 -- * No check is made against the context of the main unit because this
3133 -- is specific to the elaboration model in effect and requires custom
3134 -- handling (see Ensure_xxx_Prior_Elaboration).
3135
3136 -- * Unit_Id is subject to pragma Elaborate_Body. An implicit pragma
3137 -- Elaborate[_All] MUST be generated even though Unit_Id is always
3138 -- elaborated prior to the main unit. This is a conservative strategy
3139 -- which ensures that other units withed by Unit_Id will not lead to
3140 -- an ABE.
3141
3142 -- package A is package body A is
3143 -- procedure ABE; procedure ABE is ... end ABE;
3144 -- end A; end A;
3145
3146 -- with A;
3147 -- package B is package body B is
3148 -- pragma Elaborate_Body; procedure Proc is
3149 -- begin
3150 -- procedure Proc; A.ABE;
3151 -- package B; end Proc;
3152 -- end B;
3153
3154 -- with B;
3155 -- package C is package body C is
3156 -- ... ...
3157 -- end C; begin
3158 -- B.Proc;
3159 -- end C;
3160
3161 -- In the example above, the elaboration of C invokes B.Proc. B is
3162 -- subject to pragma Elaborate_Body. If no pragma Elaborate[_All] is
3163 -- generated for B in C, then the following elaboratio order will lead
3164 -- to an ABE:
3165
3166 -- spec of A elaborated
3167 -- spec of B elaborated
3168 -- body of B elaborated
3169 -- spec of C elaborated
3170 -- body of C elaborated <-- calls B.Proc which calls A.ABE
3171 -- body of A elaborated <-- problem
3172
3173 -- The generation of an implicit pragma Elaborate_All (B) ensures that
3174 -- the elaboration order mechanism will not pick the above order.
3175
3176 -- An implicit Elaborate is NOT generated when the unit is subject to
3177 -- Elaborate_Body because both pragmas have the exact same effect.
3178
3179 -- * Unit_Id is the main unit. An implicit pragma Elaborate[_All] MUST
3180 -- NOT be generated in this case because a unit cannot depend on its
3181 -- own elaboration. This case is therefore treated as valid prior
3182 -- elaboration.
3183
3184 elsif Has_Prior_Elaboration
3185 (Unit_Id => Unit_Id,
3186 Same_Unit_OK => True,
3187 Elab_Body_OK => Prag_Nam = Name_Elaborate)
3188 then
3189 return;
3190
3191 -- Suggest the use of pragma Prag_Nam when the dynamic model is in
3192 -- effect.
3193
3194 elsif Dynamic_Elaboration_Checks then
3195 Ensure_Prior_Elaboration_Dynamic
3196 (N => N,
3197 Unit_Id => Unit_Id,
3198 Prag_Nam => Prag_Nam);
3199
3200 -- Install an implicit pragma Prag_Nam when the static model is in
3201 -- effect.
3202
3203 else
3204 pragma Assert (Static_Elaboration_Checks);
3205
3206 Ensure_Prior_Elaboration_Static
3207 (N => N,
3208 Unit_Id => Unit_Id,
3209 Prag_Nam => Prag_Nam);
3210 end if;
3211 end Ensure_Prior_Elaboration;
3212
3213 --------------------------------------
3214 -- Ensure_Prior_Elaboration_Dynamic --
3215 --------------------------------------
3216
3217 procedure Ensure_Prior_Elaboration_Dynamic
3218 (N : Node_Id;
3219 Unit_Id : Entity_Id;
3220 Prag_Nam : Name_Id)
3221 is
3222 procedure Info_Missing_Pragma;
3223 pragma Inline (Info_Missing_Pragma);
3224 -- Output information concerning missing Elaborate or Elaborate_All
3225 -- pragma with name Prag_Nam for scenario N, which would ensure the
3226 -- prior elaboration of Unit_Id.
3227
3228 -------------------------
3229 -- Info_Missing_Pragma --
3230 -------------------------
3231
3232 procedure Info_Missing_Pragma is
3233 begin
3234 -- Internal units are ignored as they cause unnecessary noise
3235
3236 if not In_Internal_Unit (Unit_Id) then
3237
3238 -- The name of the unit subjected to the elaboration pragma is
3239 -- fully qualified to improve the clarity of the info message.
3240
3241 Error_Msg_Name_1 := Prag_Nam;
3242 Error_Msg_Qual_Level := Nat'Last;
3243
3244 Error_Msg_NE ("info: missing pragma % for unit &", N, Unit_Id);
3245 Error_Msg_Qual_Level := 0;
3246 end if;
3247 end Info_Missing_Pragma;
3248
3249 -- Local variables
3250
3251 Elab_Attrs : Elaboration_Attributes;
3252 Level : Enclosing_Level_Kind;
3253
3254 -- Start of processing for Ensure_Prior_Elaboration_Dynamic
3255
3256 begin
3257 Elab_Attrs := Elaboration_Status (Unit_Id);
3258
3259 -- Nothing to do when the unit is guaranteed prior elaboration by means
3260 -- of a source Elaborate[_All] pragma.
3261
3262 if Present (Elab_Attrs.Source_Pragma) then
3263 return;
3264 end if;
3265
3266 -- Output extra information on a missing Elaborate[_All] pragma when
3267 -- switch -gnatel (info messages on implicit Elaborate[_All] pragmas
3268 -- is in effect.
3269
3270 if Elab_Info_Messages then
3271
3272 -- Performance note: parent traversal
3273
3274 Level := Find_Enclosing_Level (N);
3275
3276 -- Declaration-level scenario
3277
3278 if (Is_Suitable_Call (N) or else Is_Suitable_Instantiation (N))
3279 and then Level = Declaration_Level
3280 then
3281 null;
3282
3283 -- Library-level scenario
3284
3285 elsif Level in Library_Level then
3286 null;
3287
3288 -- Instantiation library-level scenario
3289
3290 elsif Level = Instantiation then
3291 null;
3292
3293 -- Otherwise the scenario does not appear at the proper level and
3294 -- cannot possibly act as a top-level scenario.
3295
3296 else
3297 return;
3298 end if;
3299
3300 Info_Missing_Pragma;
3301 end if;
3302 end Ensure_Prior_Elaboration_Dynamic;
3303
3304 -------------------------------------
3305 -- Ensure_Prior_Elaboration_Static --
3306 -------------------------------------
3307
3308 procedure Ensure_Prior_Elaboration_Static
3309 (N : Node_Id;
3310 Unit_Id : Entity_Id;
3311 Prag_Nam : Name_Id)
3312 is
3313 function Find_With_Clause
3314 (Items : List_Id;
3315 Withed_Id : Entity_Id) return Node_Id;
3316 pragma Inline (Find_With_Clause);
3317 -- Find a nonlimited with clause in the list of context items Items
3318 -- that withs unit Withed_Id. Return Empty if no such clause is found.
3319
3320 procedure Info_Implicit_Pragma;
3321 pragma Inline (Info_Implicit_Pragma);
3322 -- Output information concerning an implicitly generated Elaborate or
3323 -- Elaborate_All pragma with name Prag_Nam for scenario N which ensures
3324 -- the prior elaboration of unit Unit_Id.
3325
3326 ----------------------
3327 -- Find_With_Clause --
3328 ----------------------
3329
3330 function Find_With_Clause
3331 (Items : List_Id;
3332 Withed_Id : Entity_Id) return Node_Id
3333 is
3334 Item : Node_Id;
3335
3336 begin
3337 -- Examine the context clauses looking for a suitable with. Note that
3338 -- limited clauses do not affect the elaboration order.
3339
3340 Item := First (Items);
3341 while Present (Item) loop
3342 if Nkind (Item) = N_With_Clause
3343 and then not Error_Posted (Item)
3344 and then not Limited_Present (Item)
3345 and then Entity (Name (Item)) = Withed_Id
3346 then
3347 return Item;
3348 end if;
3349
3350 Next (Item);
3351 end loop;
3352
3353 return Empty;
3354 end Find_With_Clause;
3355
3356 --------------------------
3357 -- Info_Implicit_Pragma --
3358 --------------------------
3359
3360 procedure Info_Implicit_Pragma is
3361 begin
3362 -- Internal units are ignored as they cause unnecessary noise
3363
3364 if not In_Internal_Unit (Unit_Id) then
3365
3366 -- The name of the unit subjected to the elaboration pragma is
3367 -- fully qualified to improve the clarity of the info message.
3368
3369 Error_Msg_Name_1 := Prag_Nam;
3370 Error_Msg_Qual_Level := Nat'Last;
3371
3372 Error_Msg_NE
3373 ("info: implicit pragma % generated for unit &", N, Unit_Id);
3374
3375 Error_Msg_Qual_Level := 0;
3376 Output_Active_Scenarios (N);
3377 end if;
3378 end Info_Implicit_Pragma;
3379
3380 -- Local variables
3381
3382 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
3383 Loc : constant Source_Ptr := Sloc (Main_Cunit);
3384 Unit_Cunit : constant Node_Id := Compilation_Unit (Unit_Id);
3385
3386 Is_Instantiation : constant Boolean :=
3387 Nkind (N) in N_Generic_Instantiation;
3388
3389 Clause : Node_Id;
3390 Elab_Attrs : Elaboration_Attributes;
3391 Items : List_Id;
3392
3393 -- Start of processing for Ensure_Prior_Elaboration_Static
3394
3395 begin
3396 Elab_Attrs := Elaboration_Status (Unit_Id);
3397
3398 -- Nothing to do when the unit is guaranteed prior elaboration by means
3399 -- of a source Elaborate[_All] pragma.
3400
3401 if Present (Elab_Attrs.Source_Pragma) then
3402 return;
3403
3404 -- Nothing to do when the unit has an existing implicit Elaborate[_All]
3405 -- pragma installed by a previous scenario.
3406
3407 elsif Present (Elab_Attrs.With_Clause) then
3408
3409 -- The unit is already guaranteed prior elaboration by means of an
3410 -- implicit Elaborate pragma, however the current scenario imposes
3411 -- a stronger requirement of Elaborate_All. "Upgrade" the existing
3412 -- pragma to match this new requirement.
3413
3414 if Elaborate_Desirable (Elab_Attrs.With_Clause)
3415 and then Prag_Nam = Name_Elaborate_All
3416 then
3417 Set_Elaborate_All_Desirable (Elab_Attrs.With_Clause);
3418 Set_Elaborate_Desirable (Elab_Attrs.With_Clause, False);
3419 end if;
3420
3421 return;
3422 end if;
3423
3424 -- At this point it is known that the unit has no prior elaboration
3425 -- according to pragmas and hierarchical relationships.
3426
3427 Items := Context_Items (Main_Cunit);
3428
3429 if No (Items) then
3430 Items := New_List;
3431 Set_Context_Items (Main_Cunit, Items);
3432 end if;
3433
3434 -- Locate the with clause for the unit. Note that there may not be a
3435 -- clause if the unit is visible through a subunit-body, body-spec, or
3436 -- spec-parent relationship.
3437
3438 Clause :=
3439 Find_With_Clause
3440 (Items => Items,
3441 Withed_Id => Unit_Id);
3442
3443 -- Generate:
3444 -- with Id;
3445
3446 -- Note that adding implicit with clauses is safe because analysis,
3447 -- resolution, and expansion have already taken place and it is not
3448 -- possible to interfere with visibility.
3449
3450 if No (Clause) then
3451 Clause :=
3452 Make_With_Clause (Loc,
3453 Name => New_Occurrence_Of (Unit_Id, Loc));
3454
3455 Set_Implicit_With (Clause);
3456 Set_Library_Unit (Clause, Unit_Cunit);
3457
3458 Append_To (Items, Clause);
3459 end if;
3460
3461 -- Instantiations require an implicit Elaborate because Elaborate_All is
3462 -- too conservative and may introduce non-existent elaboration cycles.
3463
3464 if Is_Instantiation then
3465 Set_Elaborate_Desirable (Clause);
3466
3467 -- Otherwise generate an implicit Elaborate_All
3468
3469 else
3470 Set_Elaborate_All_Desirable (Clause);
3471 end if;
3472
3473 -- The implicit Elaborate[_All] ensures the prior elaboration of the
3474 -- unit. Include the unit in the elaboration context of the main unit.
3475
3476 Set_Elaboration_Status
3477 (Unit_Id => Unit_Id,
3478 Val => Elaboration_Attributes'(Source_Pragma => Empty,
3479 With_Clause => Clause));
3480
3481 -- Output extra information on an implicit Elaborate[_All] pragma when
3482 -- switch -gnatel (info messages on implicit Elaborate[_All] pragmas is
3483 -- in effect.
3484
3485 if Elab_Info_Messages then
3486 Info_Implicit_Pragma;
3487 end if;
3488 end Ensure_Prior_Elaboration_Static;
3489
3490 -----------------------------
3491 -- Extract_Assignment_Name --
3492 -----------------------------
3493
3494 function Extract_Assignment_Name (Asmt : Node_Id) return Node_Id is
3495 Nam : Node_Id;
3496
3497 begin
3498 Nam := Name (Asmt);
3499
3500 -- When the name denotes an array or record component, find the whole
3501 -- object.
3502
3503 while Nkind_In (Nam, N_Explicit_Dereference,
3504 N_Indexed_Component,
3505 N_Selected_Component,
3506 N_Slice)
3507 loop
3508 Nam := Prefix (Nam);
3509 end loop;
3510
3511 return Nam;
3512 end Extract_Assignment_Name;
3513
3514 -----------------------------
3515 -- Extract_Call_Attributes --
3516 -----------------------------
3517
3518 procedure Extract_Call_Attributes
3519 (Call : Node_Id;
3520 Target_Id : out Entity_Id;
3521 Attrs : out Call_Attributes)
3522 is
3523 From_Source : Boolean;
3524 In_Declarations : Boolean;
3525 Is_Dispatching : Boolean;
3526
3527 begin
3528 -- Extraction for call markers
3529
3530 if Nkind (Call) = N_Call_Marker then
3531 Target_Id := Target (Call);
3532 From_Source := Is_Source_Call (Call);
3533 In_Declarations := Is_Declaration_Level_Node (Call);
3534 Is_Dispatching := Is_Dispatching_Call (Call);
3535
3536 -- Extraction for entry calls, requeue, and subprogram calls
3537
3538 else
3539 pragma Assert (Nkind_In (Call, N_Entry_Call_Statement,
3540 N_Function_Call,
3541 N_Procedure_Call_Statement,
3542 N_Requeue_Statement));
3543
3544 Target_Id := Entity (Extract_Call_Name (Call));
3545 From_Source := Comes_From_Source (Call);
3546
3547 -- Performance note: parent traversal
3548
3549 In_Declarations := Find_Enclosing_Level (Call) = Declaration_Level;
3550 Is_Dispatching :=
3551 Nkind_In (Call, N_Function_Call, N_Procedure_Call_Statement)
3552 and then Present (Controlling_Argument (Call));
3553 end if;
3554
3555 -- Obtain the original entry or subprogram which the target may rename
3556 -- except when the target is an instantiation. In this case the alias
3557 -- is the internally generated subprogram which appears within the the
3558 -- anonymous package created for the instantiation. Such an alias is not
3559 -- a suitable target.
3560
3561 if not (Is_Subprogram (Target_Id)
3562 and then Is_Generic_Instance (Target_Id))
3563 then
3564 Target_Id := Get_Renamed_Entity (Target_Id);
3565 end if;
3566
3567 -- Set all attributes
3568
3569 Attrs.Elab_Checks_OK := Is_Elaboration_Checks_OK_Node (Call);
3570 Attrs.From_Source := From_Source;
3571 Attrs.Ghost_Mode_Ignore := Is_Ignored_Ghost_Node (Call);
3572 Attrs.In_Declarations := In_Declarations;
3573 Attrs.Is_Dispatching := Is_Dispatching;
3574 Attrs.SPARK_Mode_On := Is_SPARK_Mode_On_Node (Call);
3575 end Extract_Call_Attributes;
3576
3577 -----------------------
3578 -- Extract_Call_Name --
3579 -----------------------
3580
3581 function Extract_Call_Name (Call : Node_Id) return Node_Id is
3582 Nam : Node_Id;
3583
3584 begin
3585 Nam := Name (Call);
3586
3587 -- When the call invokes an entry family, the name appears as an indexed
3588 -- component.
3589
3590 if Nkind (Nam) = N_Indexed_Component then
3591 Nam := Prefix (Nam);
3592 end if;
3593
3594 -- When the call employs the object.operation form, the name appears as
3595 -- a selected component.
3596
3597 if Nkind (Nam) = N_Selected_Component then
3598 Nam := Selector_Name (Nam);
3599 end if;
3600
3601 return Nam;
3602 end Extract_Call_Name;
3603
3604 ---------------------------------
3605 -- Extract_Instance_Attributes --
3606 ---------------------------------
3607
3608 procedure Extract_Instance_Attributes
3609 (Exp_Inst : Node_Id;
3610 Inst_Body : out Node_Id;
3611 Inst_Decl : out Node_Id)
3612 is
3613 Body_Id : Entity_Id;
3614
3615 begin
3616 -- Assume that the attributes are unavailable
3617
3618 Inst_Body := Empty;
3619 Inst_Decl := Empty;
3620
3621 -- Generic package or subprogram spec
3622
3623 if Nkind_In (Exp_Inst, N_Package_Declaration,
3624 N_Subprogram_Declaration)
3625 then
3626 Inst_Decl := Exp_Inst;
3627 Body_Id := Corresponding_Body (Inst_Decl);
3628
3629 if Present (Body_Id) then
3630 Inst_Body := Unit_Declaration_Node (Body_Id);
3631 end if;
3632
3633 -- Generic package or subprogram body
3634
3635 else
3636 pragma Assert
3637 (Nkind_In (Exp_Inst, N_Package_Body, N_Subprogram_Body));
3638
3639 Inst_Body := Exp_Inst;
3640 Inst_Decl := Unit_Declaration_Node (Corresponding_Spec (Inst_Body));
3641 end if;
3642 end Extract_Instance_Attributes;
3643
3644 --------------------------------------
3645 -- Extract_Instantiation_Attributes --
3646 --------------------------------------
3647
3648 procedure Extract_Instantiation_Attributes
3649 (Exp_Inst : Node_Id;
3650 Inst : out Node_Id;
3651 Inst_Id : out Entity_Id;
3652 Gen_Id : out Entity_Id;
3653 Attrs : out Instantiation_Attributes)
3654 is
3655 begin
3656 Inst := Original_Node (Exp_Inst);
3657 Inst_Id := Defining_Entity (Inst);
3658
3659 -- Traverse a possible chain of renamings to obtain the original generic
3660 -- being instantiatied.
3661
3662 Gen_Id := Get_Renamed_Entity (Entity (Name (Inst)));
3663
3664 -- Set all attributes
3665
3666 Attrs.Elab_Checks_OK := Is_Elaboration_Checks_OK_Node (Inst);
3667 Attrs.Ghost_Mode_Ignore := Is_Ignored_Ghost_Node (Inst);
3668 Attrs.In_Declarations := Is_Declaration_Level_Node (Inst);
3669 Attrs.SPARK_Mode_On := Is_SPARK_Mode_On_Node (Inst);
3670 end Extract_Instantiation_Attributes;
3671
3672 -------------------------------
3673 -- Extract_Target_Attributes --
3674 -------------------------------
3675
3676 procedure Extract_Target_Attributes
3677 (Target_Id : Entity_Id;
3678 Attrs : out Target_Attributes)
3679 is
3680 procedure Extract_Package_Or_Subprogram_Attributes
3681 (Spec_Id : out Entity_Id;
3682 Body_Decl : out Node_Id);
3683 -- Obtain the attributes associated with a package or a subprogram.
3684 -- Spec_Id is the package or subprogram. Body_Decl is the declaration
3685 -- of the corresponding package or subprogram body.
3686
3687 procedure Extract_Protected_Entry_Attributes
3688 (Spec_Id : out Entity_Id;
3689 Body_Decl : out Node_Id;
3690 Body_Barf : out Node_Id);
3691 -- Obtain the attributes associated with a protected entry [family].
3692 -- Spec_Id is the entity of the protected body subprogram. Body_Decl
3693 -- is the declaration of Spec_Id's corresponding body. Body_Barf is
3694 -- the declaration of the barrier function body.
3695
3696 procedure Extract_Protected_Subprogram_Attributes
3697 (Spec_Id : out Entity_Id;
3698 Body_Decl : out Node_Id);
3699 -- Obtain the attributes associated with a protected subprogram. Formal
3700 -- Spec_Id is the entity of the protected body subprogram. Body_Decl is
3701 -- the declaration of Spec_Id's corresponding body.
3702
3703 procedure Extract_Task_Entry_Attributes
3704 (Spec_Id : out Entity_Id;
3705 Body_Decl : out Node_Id);
3706 -- Obtain the attributes associated with a task entry [family]. Formal
3707 -- Spec_Id is the entity of the task body procedure. Body_Decl is the
3708 -- declaration of Spec_Id's corresponding body.
3709
3710 ----------------------------------------------
3711 -- Extract_Package_Or_Subprogram_Attributes --
3712 ----------------------------------------------
3713
3714 procedure Extract_Package_Or_Subprogram_Attributes
3715 (Spec_Id : out Entity_Id;
3716 Body_Decl : out Node_Id)
3717 is
3718 Body_Id : Entity_Id;
3719 Init_Id : Entity_Id;
3720 Spec_Decl : Node_Id;
3721
3722 begin
3723 -- Assume that the body is not available
3724
3725 Body_Decl := Empty;
3726 Spec_Id := Target_Id;
3727
3728 -- For body retrieval purposes, the entity of the initial declaration
3729 -- is that of the spec.
3730
3731 Init_Id := Spec_Id;
3732
3733 -- The only exception to the above is a function which returns a
3734 -- constrained array type in a SPARK-to-C compilation. In this case
3735 -- the function receives a corresponding procedure which has an out
3736 -- parameter. The proper body for ABE checks and diagnostics is that
3737 -- of the procedure.
3738
3739 if Ekind (Init_Id) = E_Function
3740 and then Rewritten_For_C (Init_Id)
3741 then
3742 Init_Id := Corresponding_Procedure (Init_Id);
3743 end if;
3744
3745 -- Extract the attributes of the body
3746
3747 Spec_Decl := Unit_Declaration_Node (Init_Id);
3748
3749 -- The initial declaration is a stand alone subprogram body
3750
3751 if Nkind (Spec_Decl) = N_Subprogram_Body then
3752 Body_Decl := Spec_Decl;
3753
3754 -- Otherwise the package or subprogram has a spec and a completing
3755 -- body.
3756
3757 elsif Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
3758 N_Generic_Subprogram_Declaration,
3759 N_Package_Declaration,
3760 N_Subprogram_Body_Stub,
3761 N_Subprogram_Declaration)
3762 then
3763 Body_Id := Corresponding_Body (Spec_Decl);
3764
3765 if Present (Body_Id) then
3766 Body_Decl := Unit_Declaration_Node (Body_Id);
3767 end if;
3768 end if;
3769 end Extract_Package_Or_Subprogram_Attributes;
3770
3771 ----------------------------------------
3772 -- Extract_Protected_Entry_Attributes --
3773 ----------------------------------------
3774
3775 procedure Extract_Protected_Entry_Attributes
3776 (Spec_Id : out Entity_Id;
3777 Body_Decl : out Node_Id;
3778 Body_Barf : out Node_Id)
3779 is
3780 Barf_Id : Entity_Id;
3781 Body_Id : Entity_Id;
3782
3783 begin
3784 -- Assume that the bodies are not available
3785
3786 Body_Barf := Empty;
3787 Body_Decl := Empty;
3788
3789 -- When the entry [family] has already been expanded, it carries both
3790 -- the procedure which emulates the behavior of the entry [family] as
3791 -- well as the barrier function.
3792
3793 if Present (Protected_Body_Subprogram (Target_Id)) then
3794 Spec_Id := Protected_Body_Subprogram (Target_Id);
3795
3796 -- Extract the attributes of the barrier function
3797
3798 Barf_Id :=
3799 Corresponding_Body
3800 (Unit_Declaration_Node (Barrier_Function (Target_Id)));
3801
3802 if Present (Barf_Id) then
3803 Body_Barf := Unit_Declaration_Node (Barf_Id);
3804 end if;
3805
3806 -- Otherwise no expansion took place
3807
3808 else
3809 Spec_Id := Target_Id;
3810 end if;
3811
3812 -- Extract the attributes of the entry body
3813
3814 Body_Id := Corresponding_Body (Unit_Declaration_Node (Spec_Id));
3815
3816 if Present (Body_Id) then
3817 Body_Decl := Unit_Declaration_Node (Body_Id);
3818 end if;
3819 end Extract_Protected_Entry_Attributes;
3820
3821 ---------------------------------------------
3822 -- Extract_Protected_Subprogram_Attributes --
3823 ---------------------------------------------
3824
3825 procedure Extract_Protected_Subprogram_Attributes
3826 (Spec_Id : out Entity_Id;
3827 Body_Decl : out Node_Id)
3828 is
3829 Body_Id : Entity_Id;
3830
3831 begin
3832 -- Assume that the body is not available
3833
3834 Body_Decl := Empty;
3835
3836 -- When the protected subprogram has already been expanded, it
3837 -- carries the subprogram which seizes the lock and invokes the
3838 -- original statements.
3839
3840 if Present (Protected_Subprogram (Target_Id)) then
3841 Spec_Id :=
3842 Protected_Body_Subprogram (Protected_Subprogram (Target_Id));
3843
3844 -- Otherwise no expansion took place
3845
3846 else
3847 Spec_Id := Target_Id;
3848 end if;
3849
3850 -- Extract the attributes of the body
3851
3852 Body_Id := Corresponding_Body (Unit_Declaration_Node (Spec_Id));
3853
3854 if Present (Body_Id) then
3855 Body_Decl := Unit_Declaration_Node (Body_Id);
3856 end if;
3857 end Extract_Protected_Subprogram_Attributes;
3858
3859 -----------------------------------
3860 -- Extract_Task_Entry_Attributes --
3861 -----------------------------------
3862
3863 procedure Extract_Task_Entry_Attributes
3864 (Spec_Id : out Entity_Id;
3865 Body_Decl : out Node_Id)
3866 is
3867 Task_Typ : constant Entity_Id := Non_Private_View (Scope (Target_Id));
3868 Body_Id : Entity_Id;
3869
3870 begin
3871 -- Assume that the body is not available
3872
3873 Body_Decl := Empty;
3874
3875 -- The the task type has already been expanded, it carries the
3876 -- procedure which emulates the behavior of the task body.
3877
3878 if Present (Task_Body_Procedure (Task_Typ)) then
3879 Spec_Id := Task_Body_Procedure (Task_Typ);
3880
3881 -- Otherwise no expansion took place
3882
3883 else
3884 Spec_Id := Task_Typ;
3885 end if;
3886
3887 -- Extract the attributes of the body
3888
3889 Body_Id := Corresponding_Body (Unit_Declaration_Node (Spec_Id));
3890
3891 if Present (Body_Id) then
3892 Body_Decl := Unit_Declaration_Node (Body_Id);
3893 end if;
3894 end Extract_Task_Entry_Attributes;
3895
3896 -- Local variables
3897
3898 Prag : constant Node_Id := SPARK_Pragma (Target_Id);
3899 Body_Barf : Node_Id;
3900 Body_Decl : Node_Id;
3901 Spec_Id : Entity_Id;
3902
3903 -- Start of processing for Extract_Target_Attributes
3904
3905 begin
3906 -- Assume that the body of the barrier function is not available
3907
3908 Body_Barf := Empty;
3909
3910 -- The target is a protected entry [family]
3911
3912 if Is_Protected_Entry (Target_Id) then
3913 Extract_Protected_Entry_Attributes
3914 (Spec_Id => Spec_Id,
3915 Body_Decl => Body_Decl,
3916 Body_Barf => Body_Barf);
3917
3918 -- The target is a protected subprogram
3919
3920 elsif Is_Protected_Subp (Target_Id)
3921 or else Is_Protected_Body_Subp (Target_Id)
3922 then
3923 Extract_Protected_Subprogram_Attributes
3924 (Spec_Id => Spec_Id,
3925 Body_Decl => Body_Decl);
3926
3927 -- The target is a task entry [family]
3928
3929 elsif Is_Task_Entry (Target_Id) then
3930 Extract_Task_Entry_Attributes
3931 (Spec_Id => Spec_Id,
3932 Body_Decl => Body_Decl);
3933
3934 -- Otherwise the target is a package or a subprogram
3935
3936 else
3937 Extract_Package_Or_Subprogram_Attributes
3938 (Spec_Id => Spec_Id,
3939 Body_Decl => Body_Decl);
3940 end if;
3941
3942 -- Set all attributes
3943
3944 Attrs.Body_Barf := Body_Barf;
3945 Attrs.Body_Decl := Body_Decl;
3946 Attrs.Elab_Checks_OK := Is_Elaboration_Checks_OK_Id (Target_Id);
3947 Attrs.From_Source := Comes_From_Source (Target_Id);
3948 Attrs.Ghost_Mode_Ignore := Is_Ignored_Ghost_Entity (Target_Id);
3949 Attrs.SPARK_Mode_On :=
3950 Present (Prag) and then Get_SPARK_Mode_From_Annotation (Prag) = On;
3951 Attrs.Spec_Decl := Unit_Declaration_Node (Spec_Id);
3952 Attrs.Spec_Id := Spec_Id;
3953 Attrs.Unit_Id := Find_Top_Unit (Target_Id);
3954
3955 -- At this point certain attributes should always be available
3956
3957 pragma Assert (Present (Attrs.Spec_Decl));
3958 pragma Assert (Present (Attrs.Spec_Id));
3959 pragma Assert (Present (Attrs.Unit_Id));
3960 end Extract_Target_Attributes;
3961
3962 -----------------------------
3963 -- Extract_Task_Attributes --
3964 -----------------------------
3965
3966 procedure Extract_Task_Attributes
3967 (Typ : Entity_Id;
3968 Attrs : out Task_Attributes)
3969 is
3970 Task_Typ : constant Entity_Id := Non_Private_View (Typ);
3971
3972 Body_Decl : Node_Id;
3973 Body_Id : Entity_Id;
3974 Prag : Node_Id;
3975 Spec_Id : Entity_Id;
3976
3977 begin
3978 -- Assume that the body of the task procedure is not available
3979
3980 Body_Decl := Empty;
3981
3982 -- The initial declaration is that of the task body procedure
3983
3984 Spec_Id := Get_Task_Body_Procedure (Task_Typ);
3985 Body_Id := Corresponding_Body (Unit_Declaration_Node (Spec_Id));
3986
3987 if Present (Body_Id) then
3988 Body_Decl := Unit_Declaration_Node (Body_Id);
3989 end if;
3990
3991 Prag := SPARK_Pragma (Task_Typ);
3992
3993 -- Set all attributes
3994
3995 Attrs.Body_Decl := Body_Decl;
3996 Attrs.Elab_Checks_OK := Is_Elaboration_Checks_OK_Id (Task_Typ);
3997 Attrs.Ghost_Mode_Ignore := Is_Ignored_Ghost_Entity (Task_Typ);
3998 Attrs.SPARK_Mode_On :=
3999 Present (Prag) and then Get_SPARK_Mode_From_Annotation (Prag) = On;
4000 Attrs.Spec_Id := Spec_Id;
4001 Attrs.Task_Decl := Declaration_Node (Task_Typ);
4002 Attrs.Unit_Id := Find_Top_Unit (Task_Typ);
4003
4004 -- At this point certain attributes should always be available
4005
4006 pragma Assert (Present (Attrs.Spec_Id));
4007 pragma Assert (Present (Attrs.Task_Decl));
4008 pragma Assert (Present (Attrs.Unit_Id));
4009 end Extract_Task_Attributes;
4010
4011 -------------------------------------------
4012 -- Extract_Variable_Reference_Attributes --
4013 -------------------------------------------
4014
4015 procedure Extract_Variable_Reference_Attributes
4016 (Ref : Node_Id;
4017 Var_Id : out Entity_Id;
4018 Attrs : out Variable_Attributes)
4019 is
4020 function Get_Renamed_Variable (Id : Entity_Id) return Entity_Id;
4021 -- Obtain the ultimate renamed variable of variable Id
4022
4023 --------------------------
4024 -- Get_Renamed_Variable --
4025 --------------------------
4026
4027 function Get_Renamed_Variable (Id : Entity_Id) return Entity_Id is
4028 Ren_Id : Entity_Id;
4029
4030 begin
4031 Ren_Id := Id;
4032 while Present (Renamed_Entity (Ren_Id))
4033 and then Nkind (Renamed_Entity (Ren_Id)) in N_Entity
4034 loop
4035 Ren_Id := Renamed_Entity (Ren_Id);
4036 end loop;
4037
4038 return Ren_Id;
4039 end Get_Renamed_Variable;
4040
4041 -- Start of processing for Extract_Variable_Reference_Attributes
4042
4043 begin
4044 -- Extraction for variable reference markers
4045
4046 if Nkind (Ref) = N_Variable_Reference_Marker then
4047 Var_Id := Target (Ref);
4048
4049 -- Extraction for expanded names and identifiers
4050
4051 else
4052 Var_Id := Entity (Ref);
4053 end if;
4054
4055 -- Obtain the original variable which the reference mentions
4056
4057 Var_Id := Get_Renamed_Variable (Var_Id);
4058 Attrs.Unit_Id := Find_Top_Unit (Var_Id);
4059
4060 -- At this point certain attributes should always be available
4061
4062 pragma Assert (Present (Attrs.Unit_Id));
4063 end Extract_Variable_Reference_Attributes;
4064
4065 --------------------
4066 -- Find_Code_Unit --
4067 --------------------
4068
4069 function Find_Code_Unit (N : Node_Or_Entity_Id) return Entity_Id is
4070 begin
4071 return Find_Unit_Entity (Unit (Cunit (Get_Code_Unit (N))));
4072 end Find_Code_Unit;
4073
4074 ----------------------------
4075 -- Find_Early_Call_Region --
4076 ----------------------------
4077
4078 function Find_Early_Call_Region
4079 (Body_Decl : Node_Id;
4080 Assume_Elab_Body : Boolean := False;
4081 Skip_Memoization : Boolean := False) return Node_Id
4082 is
4083 -- NOTE: The routines within Find_Early_Call_Region are intentionally
4084 -- unnested to avoid deep indentation of code.
4085
4086 ECR_Found : exception;
4087 -- This exception is raised when the early call region has been found
4088
4089 Start : Node_Id := Empty;
4090 -- The start of the early call region. This variable is updated by the
4091 -- various nested routines. Due to the use of exceptions, the variable
4092 -- must be global to the nested routines.
4093
4094 -- The algorithm implemented in this routine attempts to find the early
4095 -- call region of a subprogram body by inspecting constructs in reverse
4096 -- declarative order, while navigating the tree. The algorithm consists
4097 -- of an Inspection phase and an Advancement phase. The pseudocode is as
4098 -- follows:
4099 --
4100 -- loop
4101 -- inspection phase
4102 -- advancement phase
4103 -- end loop
4104 --
4105 -- The infinite loop is terminated by raising exception ECR_Found. The
4106 -- algorithm utilizes two pointers, Curr and Start, to represent the
4107 -- current construct to inspect and the start of the early call region.
4108 --
4109 -- IMPORTANT: The algorithm must maintain the following invariant at all
4110 -- time for it to function properly - a nested construct is entered only
4111 -- when it contains suitable constructs. This guarantees that leaving a
4112 -- nested or encapsulating construct functions properly.
4113 --
4114 -- The Inspection phase determines whether the current construct is non-
4115 -- preelaborable, and if it is, the algorithm terminates.
4116 --
4117 -- The Advancement phase walks the tree in reverse declarative order,
4118 -- while entering and leaving nested and encapsulating constructs. It
4119 -- may also terminate the elaborithm. There are several special cases
4120 -- of advancement.
4121 --
4122 -- 1) General case:
4123 --
4124 -- <construct 1>
4125 -- ...
4126 -- <construct N-1> <- Curr
4127 -- <construct N> <- Start
4128 -- <subprogram body>
4129 --
4130 -- In the general case, a declarative or statement list is traversed in
4131 -- reverse order where Curr is the lead pointer, and Start indicates the
4132 -- last preelaborable construct.
4133 --
4134 -- 2) Entering handled bodies
4135 --
4136 -- package body Nested is <- Curr (2.3)
4137 -- <declarations> <- Curr (2.2)
4138 -- begin
4139 -- <statements> <- Curr (2.1)
4140 -- end Nested;
4141 -- <construct> <- Start
4142 --
4143 -- In this case, the algorithm enters a handled body by starting from
4144 -- the last statement (2.1), or the last declaration (2.2), or the body
4145 -- is consumed (2.3) because it is empty and thus preelaborable.
4146 --
4147 -- 3) Entering package declarations
4148 --
4149 -- package Nested is <- Curr (2.3)
4150 -- <visible declarations> <- Curr (2.2)
4151 -- private
4152 -- <private declarations> <- Curr (2.1)
4153 -- end Nested;
4154 -- <construct> <- Start
4155 --
4156 -- In this case, the algorithm enters a package declaration by starting
4157 -- from the last private declaration (2.1), the last visible declaration
4158 -- (2.2), or the package is consumed (2.3) because it is empty and thus
4159 -- preelaborable.
4160 --
4161 -- 4) Transitioning from list to list of the same construct
4162 --
4163 -- Certain constructs have two eligible lists. The algorithm must thus
4164 -- transition from the second to the first list when the second list is
4165 -- exhausted.
4166 --
4167 -- declare <- Curr (4.2)
4168 -- <declarations> <- Curr (4.1)
4169 -- begin
4170 -- <statements> <- Start
4171 -- end;
4172 --
4173 -- In this case, the algorithm has exhausted the second list (statements
4174 -- in the example), and continues with the last declaration (4.1) or the
4175 -- construct is consumed (4.2) because it contains only preelaborable
4176 -- code.
4177 --
4178 -- 5) Transitioning from list to construct
4179 --
4180 -- tack body Task is <- Curr (5.1)
4181 -- <- Curr (Empty)
4182 -- <construct 1> <- Start
4183 --
4184 -- In this case, the algorithm has exhausted a list, Curr is Empty, and
4185 -- the owner of the list is consumed (5.1).
4186 --
4187 -- 6) Transitioning from unit to unit
4188 --
4189 -- A package body with a spec subject to pragma Elaborate_Body extends
4190 -- the possible range of the early call region to the package spec.
4191 --
4192 -- package Pack is <- Curr (6.3)
4193 -- pragma Elaborate_Body; <- Curr (6.2)
4194 -- <visible declarations> <- Curr (6.2)
4195 -- private
4196 -- <private declarations> <- Curr (6.1)
4197 -- end Pack;
4198 --
4199 -- package body Pack is <- Curr, Start
4200 --
4201 -- In this case, the algorithm has reached a package body compilation
4202 -- unit whose spec is subject to pragma Elaborate_Body, or the caller
4203 -- of the algorithm has specified this behavior. This transition is
4204 -- equivalent to 3).
4205 --
4206 -- 7) Transitioning from unit to termination
4207 --
4208 -- Reaching a compilation unit always terminates the algorithm as there
4209 -- are no more lists to examine. This must take 6) into account.
4210 --
4211 -- 8) Transitioning from subunit to stub
4212 --
4213 -- package body Pack is separate; <- Curr (8.1)
4214 --
4215 -- separate (...)
4216 -- package body Pack is <- Curr, Start
4217 --
4218 -- Reaching a subunit continues the search from the corresponding stub
4219 -- (8.1).
4220
4221 procedure Advance (Curr : in out Node_Id);
4222 pragma Inline (Advance);
4223 -- Update the Curr and Start pointers depending on their location in the
4224 -- tree to the next eligible construct. This routine raises ECR_Found.
4225
4226 procedure Enter_Handled_Body (Curr : in out Node_Id);
4227 pragma Inline (Enter_Handled_Body);
4228 -- Update the Curr and Start pointers to enter a nested handled body if
4229 -- applicable. This routine raises ECR_Found.
4230
4231 procedure Enter_Package_Declaration (Curr : in out Node_Id);
4232 pragma Inline (Enter_Package_Declaration);
4233 -- Update the Curr and Start pointers to enter a nested package spec if
4234 -- applicable. This routine raises ECR_Found.
4235
4236 function Find_ECR (N : Node_Id) return Node_Id;
4237 pragma Inline (Find_ECR);
4238 -- Find an early call region starting from arbitrary node N
4239
4240 function Has_Suitable_Construct (List : List_Id) return Boolean;
4241 pragma Inline (Has_Suitable_Construct);
4242 -- Determine whether list List contains at least one suitable construct
4243 -- for inclusion into an early call region.
4244
4245 procedure Include (N : Node_Id; Curr : in out Node_Id);
4246 pragma Inline (Include);
4247 -- Update the Curr and Start pointers to include arbitrary construct N
4248 -- in the early call region.
4249
4250 function Is_OK_Preelaborable_Construct (N : Node_Id) return Boolean;
4251 pragma Inline (Is_OK_Preelaborable_Construct);
4252 -- Determine whether arbitrary node N denotes a preelaboration-safe
4253 -- construct.
4254
4255 function Is_Suitable_Construct (N : Node_Id) return Boolean;
4256 pragma Inline (Is_Suitable_Construct);
4257 -- Determine whether arbitrary node N denotes a suitable construct for
4258 -- inclusion into the early call region.
4259
4260 procedure Transition_Body_Declarations
4261 (Bod : Node_Id;
4262 Curr : in out Node_Id);
4263 pragma Inline (Transition_Body_Declarations);
4264 -- Update the Curr and Start pointers when construct Bod denotes a block
4265 -- statement or a suitable body. This routine raises ECR_Found.
4266
4267 procedure Transition_Handled_Statements
4268 (HSS : Node_Id;
4269 Curr : in out Node_Id);
4270 pragma Inline (Transition_Handled_Statements);
4271 -- Update the Curr and Start pointers when node HSS denotes a handled
4272 -- sequence of statements. This routine raises ECR_Found.
4273
4274 procedure Transition_Spec_Declarations
4275 (Spec : Node_Id;
4276 Curr : in out Node_Id);
4277 pragma Inline (Transition_Spec_Declarations);
4278 -- Update the Curr and Start pointers when construct Spec denotes
4279 -- a concurrent definition or a package spec. This routine raises
4280 -- ECR_Found.
4281
4282 procedure Transition_Unit (Unit : Node_Id; Curr : in out Node_Id);
4283 pragma Inline (Transition_Unit);
4284 -- Update the Curr and Start pointers when node Unit denotes a potential
4285 -- compilation unit. This routine raises ECR_Found.
4286
4287 -------------
4288 -- Advance --
4289 -------------
4290
4291 procedure Advance (Curr : in out Node_Id) is
4292 Context : Node_Id;
4293
4294 begin
4295 -- Curr denotes one of the following cases upon entry into this
4296 -- routine:
4297 --
4298 -- * Empty - There is no current construct when a declarative or a
4299 -- statement list has been exhausted. This does not necessarily
4300 -- indicate that the early call region has been computed as it
4301 -- may still be possible to transition to another list.
4302 --
4303 -- * Encapsulator - The current construct encapsulates declarations
4304 -- and/or statements. This indicates that the early call region
4305 -- may extend within the nested construct.
4306 --
4307 -- * Preelaborable - The current construct is always preelaborable
4308 -- because Find_ECR would not invoke Advance if this was not the
4309 -- case.
4310
4311 -- The current construct is an encapsulator or is preelaborable
4312
4313 if Present (Curr) then
4314
4315 -- Enter encapsulators by inspecting their declarations and/or
4316 -- statements.
4317
4318 if Nkind_In (Curr, N_Block_Statement, N_Package_Body) then
4319 Enter_Handled_Body (Curr);
4320
4321 elsif Nkind (Curr) = N_Package_Declaration then
4322 Enter_Package_Declaration (Curr);
4323
4324 -- Early call regions have a property which can be exploited to
4325 -- optimize the algorithm.
4326 --
4327 -- <preceding subprogram body>
4328 -- <preelaborable construct 1>
4329 -- ...
4330 -- <preelaborable construct N>
4331 -- <initiating subprogram body>
4332 --
4333 -- If a traversal initiated from a subprogram body reaches a
4334 -- preceding subprogram body, then both bodies share the same
4335 -- early call region.
4336 --
4337 -- The property results in the following desirable effects:
4338 --
4339 -- * If the preceding body already has an early call region, then
4340 -- the initiating body can reuse it. This minimizes the amount
4341 -- of processing performed by the algorithm.
4342 --
4343 -- * If the preceding body lack an early call region, then the
4344 -- algorithm can compute the early call region, and reuse it
4345 -- for the initiating body. This processing performs the same
4346 -- amount of work, but has the beneficial effect of computing
4347 -- the early call regions of all preceding bodies.
4348
4349 elsif Nkind_In (Curr, N_Entry_Body, N_Subprogram_Body) then
4350 Start :=
4351 Find_Early_Call_Region
4352 (Body_Decl => Curr,
4353 Assume_Elab_Body => Assume_Elab_Body,
4354 Skip_Memoization => Skip_Memoization);
4355
4356 raise ECR_Found;
4357
4358 -- Otherwise current construct is preelaborable. Unpdate the early
4359 -- call region to include it.
4360
4361 else
4362 Include (Curr, Curr);
4363 end if;
4364
4365 -- Otherwise the current construct is missing, indicating that the
4366 -- current list has been exhausted. Depending on the context of the
4367 -- list, several transitions are possible.
4368
4369 else
4370 -- The invariant of the algorithm ensures that Curr and Start are
4371 -- at the same level of nesting at the point of a transition. The
4372 -- algorithm can determine which list the traversal came from by
4373 -- examining Start.
4374
4375 Context := Parent (Start);
4376
4377 -- Attempt the following transitions:
4378 --
4379 -- private declarations -> visible declarations
4380 -- private declarations -> upper level
4381 -- private declarations -> terminate
4382 -- visible declarations -> upper level
4383 -- visible declarations -> terminate
4384
4385 if Nkind_In (Context, N_Package_Specification,
4386 N_Protected_Definition,
4387 N_Task_Definition)
4388 then
4389 Transition_Spec_Declarations (Context, Curr);
4390
4391 -- Attempt the following transitions:
4392 --
4393 -- statements -> declarations
4394 -- statements -> upper level
4395 -- statements -> corresponding package spec (Elab_Body)
4396 -- statements -> terminate
4397
4398 elsif Nkind (Context) = N_Handled_Sequence_Of_Statements then
4399 Transition_Handled_Statements (Context, Curr);
4400
4401 -- Attempt the following transitions:
4402 --
4403 -- declarations -> upper level
4404 -- declarations -> corresponding package spec (Elab_Body)
4405 -- declarations -> terminate
4406
4407 elsif Nkind_In (Context, N_Block_Statement,
4408 N_Entry_Body,
4409 N_Package_Body,
4410 N_Protected_Body,
4411 N_Subprogram_Body,
4412 N_Task_Body)
4413 then
4414 Transition_Body_Declarations (Context, Curr);
4415
4416 -- Otherwise it is not possible to transition. Stop the search
4417 -- because there are no more declarations or statements to check.
4418
4419 else
4420 raise ECR_Found;
4421 end if;
4422 end if;
4423 end Advance;
4424
4425 --------------------------
4426 -- Enter_Handled_Body --
4427 --------------------------
4428
4429 procedure Enter_Handled_Body (Curr : in out Node_Id) is
4430 Decls : constant List_Id := Declarations (Curr);
4431 HSS : constant Node_Id := Handled_Statement_Sequence (Curr);
4432 Stmts : List_Id := No_List;
4433
4434 begin
4435 if Present (HSS) then
4436 Stmts := Statements (HSS);
4437 end if;
4438
4439 -- The handled body has a non-empty statement sequence. The construct
4440 -- to inspect is the last statement.
4441
4442 if Has_Suitable_Construct (Stmts) then
4443 Curr := Last (Stmts);
4444
4445 -- The handled body lacks statements, but has non-empty declarations.
4446 -- The construct to inspect is the last declaration.
4447
4448 elsif Has_Suitable_Construct (Decls) then
4449 Curr := Last (Decls);
4450
4451 -- Otherwise the handled body lacks both declarations and statements.
4452 -- The construct to inspect is the node which precedes the handled
4453 -- body. Update the early call region to include the handled body.
4454
4455 else
4456 Include (Curr, Curr);
4457 end if;
4458 end Enter_Handled_Body;
4459
4460 -------------------------------
4461 -- Enter_Package_Declaration --
4462 -------------------------------
4463
4464 procedure Enter_Package_Declaration (Curr : in out Node_Id) is
4465 Pack_Spec : constant Node_Id := Specification (Curr);
4466 Prv_Decls : constant List_Id := Private_Declarations (Pack_Spec);
4467 Vis_Decls : constant List_Id := Visible_Declarations (Pack_Spec);
4468
4469 begin
4470 -- The package has a non-empty private declarations. The construct to
4471 -- inspect is the last private declaration.
4472
4473 if Has_Suitable_Construct (Prv_Decls) then
4474 Curr := Last (Prv_Decls);
4475
4476 -- The package lacks private declarations, but has non-empty visible
4477 -- declarations. In this case the construct to inspect is the last
4478 -- visible declaration.
4479
4480 elsif Has_Suitable_Construct (Vis_Decls) then
4481 Curr := Last (Vis_Decls);
4482
4483 -- Otherwise the package lacks any declarations. The construct to
4484 -- inspect is the node which precedes the package. Update the early
4485 -- call region to include the package declaration.
4486
4487 else
4488 Include (Curr, Curr);
4489 end if;
4490 end Enter_Package_Declaration;
4491
4492 --------------
4493 -- Find_ECR --
4494 --------------
4495
4496 function Find_ECR (N : Node_Id) return Node_Id is
4497 Curr : Node_Id;
4498
4499 begin
4500 -- The early call region starts at N
4501
4502 Curr := Prev (N);
4503 Start := N;
4504
4505 -- Inspect each node in reverse declarative order while going in and
4506 -- out of nested and enclosing constructs. Note that the only way to
4507 -- terminate this infinite loop is to raise exception ECR_Found.
4508
4509 loop
4510 -- The current construct is not preelaboration-safe. Terminate the
4511 -- traversal.
4512
4513 if Present (Curr)
4514 and then not Is_OK_Preelaborable_Construct (Curr)
4515 then
4516 raise ECR_Found;
4517 end if;
4518
4519 -- Advance to the next suitable construct. This may terminate the
4520 -- traversal by raising ECR_Found.
4521
4522 Advance (Curr);
4523 end loop;
4524
4525 exception
4526 when ECR_Found =>
4527 return Start;
4528 end Find_ECR;
4529
4530 ----------------------------
4531 -- Has_Suitable_Construct --
4532 ----------------------------
4533
4534 function Has_Suitable_Construct (List : List_Id) return Boolean is
4535 Item : Node_Id;
4536
4537 begin
4538 -- Examine the list in reverse declarative order, looking for a
4539 -- suitable construct.
4540
4541 if Present (List) then
4542 Item := Last (List);
4543 while Present (Item) loop
4544 if Is_Suitable_Construct (Item) then
4545 return True;
4546 end if;
4547
4548 Prev (Item);
4549 end loop;
4550 end if;
4551
4552 return False;
4553 end Has_Suitable_Construct;
4554
4555 -------------
4556 -- Include --
4557 -------------
4558
4559 procedure Include (N : Node_Id; Curr : in out Node_Id) is
4560 begin
4561 Start := N;
4562 Curr := Prev (Start);
4563 end Include;
4564
4565 -----------------------------------
4566 -- Is_OK_Preelaborable_Construct --
4567 -----------------------------------
4568
4569 function Is_OK_Preelaborable_Construct (N : Node_Id) return Boolean is
4570 begin
4571 -- Assignment statements are acceptable as long as they were produced
4572 -- by the ABE mechanism to update elaboration flags.
4573
4574 if Nkind (N) = N_Assignment_Statement then
4575 return Is_Elaboration_Code (N);
4576
4577 -- Block statements are acceptable even though they directly violate
4578 -- preelaborability. The intention is not to penalize the early call
4579 -- region when a block contains only preelaborable constructs.
4580 --
4581 -- declare
4582 -- Val : constant Integer := 1;
4583 -- begin
4584 -- pragma Assert (Val = 1);
4585 -- null;
4586 -- end;
4587 --
4588 -- Note that the Advancement phase does enter blocks, and will detect
4589 -- any non-preelaborable declarations or statements within.
4590
4591 elsif Nkind (N) = N_Block_Statement then
4592 return True;
4593 end if;
4594
4595 -- Otherwise the construct must be preelaborable. The check must take
4596 -- the syntactic and semantic structure of the construct. DO NOT use
4597 -- Is_Preelaborable_Construct here.
4598
4599 return not Is_Non_Preelaborable_Construct (N);
4600 end Is_OK_Preelaborable_Construct;
4601
4602 ---------------------------
4603 -- Is_Suitable_Construct --
4604 ---------------------------
4605
4606 function Is_Suitable_Construct (N : Node_Id) return Boolean is
4607 Context : constant Node_Id := Parent (N);
4608
4609 begin
4610 -- An internally-generated statement sequence which contains only a
4611 -- single null statement is not a suitable construct because it is a
4612 -- byproduct of the parser. Such a null statement should be excluded
4613 -- from the early call region because it carries the source location
4614 -- of the "end" keyword, and may lead to confusing diagnistics.
4615
4616 if Nkind (N) = N_Null_Statement
4617 and then not Comes_From_Source (N)
4618 and then Present (Context)
4619 and then Nkind (Context) = N_Handled_Sequence_Of_Statements
4620 and then not Comes_From_Source (N)
4621 then
4622 return False;
4623 end if;
4624
4625 -- Otherwise only constructs which correspond to pure Ada constructs
4626 -- are considered suitable.
4627
4628 case Nkind (N) is
4629 when N_Call_Marker
4630 | N_Freeze_Entity
4631 | N_Freeze_Generic_Entity
4632 | N_Implicit_Label_Declaration
4633 | N_Itype_Reference
4634 | N_Pop_Constraint_Error_Label
4635 | N_Pop_Program_Error_Label
4636 | N_Pop_Storage_Error_Label
4637 | N_Push_Constraint_Error_Label
4638 | N_Push_Program_Error_Label
4639 | N_Push_Storage_Error_Label
4640 | N_SCIL_Dispatch_Table_Tag_Init
4641 | N_SCIL_Dispatching_Call
4642 | N_SCIL_Membership_Test
4643 | N_Variable_Reference_Marker
4644 =>
4645 return False;
4646
4647 when others =>
4648 return True;
4649 end case;
4650 end Is_Suitable_Construct;
4651
4652 ----------------------------------
4653 -- Transition_Body_Declarations --
4654 ----------------------------------
4655
4656 procedure Transition_Body_Declarations
4657 (Bod : Node_Id;
4658 Curr : in out Node_Id)
4659 is
4660 Decls : constant List_Id := Declarations (Bod);
4661
4662 begin
4663 -- The search must come from the declarations of the body
4664
4665 pragma Assert
4666 (Is_Non_Empty_List (Decls)
4667 and then List_Containing (Start) = Decls);
4668
4669 -- The search finished inspecting the declarations. The construct
4670 -- to inspect is the node which precedes the handled body, unless
4671 -- the body is a compilation unit. The transitions are:
4672 --
4673 -- declarations -> upper level
4674 -- declarations -> corresponding package spec (Elab_Body)
4675 -- declarations -> terminate
4676
4677 Transition_Unit (Bod, Curr);
4678 end Transition_Body_Declarations;
4679
4680 -----------------------------------
4681 -- Transition_Handled_Statements --
4682 -----------------------------------
4683
4684 procedure Transition_Handled_Statements
4685 (HSS : Node_Id;
4686 Curr : in out Node_Id)
4687 is
4688 Bod : constant Node_Id := Parent (HSS);
4689 Decls : constant List_Id := Declarations (Bod);
4690 Stmts : constant List_Id := Statements (HSS);
4691
4692 begin
4693 -- The search must come from the statements of certain bodies or
4694 -- statements.
4695
4696 pragma Assert (Nkind_In (Bod, N_Block_Statement,
4697 N_Entry_Body,
4698 N_Package_Body,
4699 N_Protected_Body,
4700 N_Subprogram_Body,
4701 N_Task_Body));
4702
4703 -- The search must come from the statements of the handled sequence
4704
4705 pragma Assert
4706 (Is_Non_Empty_List (Stmts)
4707 and then List_Containing (Start) = Stmts);
4708
4709 -- The search finished inspecting the statements. The handled body
4710 -- has non-empty declarations. The construct to inspect is the last
4711 -- declaration. The transitions are:
4712 --
4713 -- statements -> declarations
4714
4715 if Has_Suitable_Construct (Decls) then
4716 Curr := Last (Decls);
4717
4718 -- Otherwise the handled body lacks declarations. The construct to
4719 -- inspect is the node which precedes the handled body, unless the
4720 -- body is a compilation unit. The transitions are:
4721 --
4722 -- statements -> upper level
4723 -- statements -> corresponding package spec (Elab_Body)
4724 -- statements -> terminate
4725
4726 else
4727 Transition_Unit (Bod, Curr);
4728 end if;
4729 end Transition_Handled_Statements;
4730
4731 ----------------------------------
4732 -- Transition_Spec_Declarations --
4733 ----------------------------------
4734
4735 procedure Transition_Spec_Declarations
4736 (Spec : Node_Id;
4737 Curr : in out Node_Id)
4738 is
4739 Prv_Decls : constant List_Id := Private_Declarations (Spec);
4740 Vis_Decls : constant List_Id := Visible_Declarations (Spec);
4741
4742 begin
4743 pragma Assert (Present (Start) and then Is_List_Member (Start));
4744
4745 -- The search came from the private declarations and finished their
4746 -- inspection.
4747
4748 if Has_Suitable_Construct (Prv_Decls)
4749 and then List_Containing (Start) = Prv_Decls
4750 then
4751 -- The context has non-empty visible declarations. The node to
4752 -- inspect is the last visible declaration. The transitions are:
4753 --
4754 -- private declarations -> visible declarations
4755
4756 if Has_Suitable_Construct (Vis_Decls) then
4757 Curr := Last (Vis_Decls);
4758
4759 -- Otherwise the context lacks visible declarations. The construct
4760 -- to inspect is the node which precedes the context unless the
4761 -- context is a compilation unit. The transitions are:
4762 --
4763 -- private declarations -> upper level
4764 -- private declarations -> terminate
4765
4766 else
4767 Transition_Unit (Parent (Spec), Curr);
4768 end if;
4769
4770 -- The search came from the visible declarations and finished their
4771 -- inspections. The construct to inspect is the node which precedes
4772 -- the context, unless the context is a compilaton unit. The
4773 -- transitions are:
4774 --
4775 -- visible declarations -> upper level
4776 -- visible declarations -> terminate
4777
4778 elsif Has_Suitable_Construct (Vis_Decls)
4779 and then List_Containing (Start) = Vis_Decls
4780 then
4781 Transition_Unit (Parent (Spec), Curr);
4782
4783 -- At this point both declarative lists are empty, but the traversal
4784 -- still came from within the spec. This indicates that the invariant
4785 -- of the algorithm has been violated.
4786
4787 else
4788 pragma Assert (False);
4789 raise ECR_Found;
4790 end if;
4791 end Transition_Spec_Declarations;
4792
4793 ---------------------
4794 -- Transition_Unit --
4795 ---------------------
4796
4797 procedure Transition_Unit
4798 (Unit : Node_Id;
4799 Curr : in out Node_Id)
4800 is
4801 Context : constant Node_Id := Parent (Unit);
4802
4803 begin
4804 -- The unit is a compilation unit. This terminates the search because
4805 -- there are no more lists to inspect and there are no more enclosing
4806 -- constructs to climb up to.
4807
4808 if Nkind (Context) = N_Compilation_Unit then
4809
4810 -- A package body with a corresponding spec subject to pragma
4811 -- Elaborate_Body is an exception to the above. The annotation
4812 -- allows the search to continue into the package declaration.
4813 -- The transitions are:
4814 --
4815 -- statements -> corresponding package spec (Elab_Body)
4816 -- declarations -> corresponding package spec (Elab_Body)
4817
4818 if Nkind (Unit) = N_Package_Body
4819 and then (Assume_Elab_Body
4820 or else Has_Pragma_Elaborate_Body
4821 (Corresponding_Spec (Unit)))
4822 then
4823 Curr := Unit_Declaration_Node (Corresponding_Spec (Unit));
4824 Enter_Package_Declaration (Curr);
4825
4826 -- Otherwise terminate the search. The transitions are:
4827 --
4828 -- private declarations -> terminate
4829 -- visible declarations -> terminate
4830 -- statements -> terminate
4831 -- declarations -> terminate
4832
4833 else
4834 raise ECR_Found;
4835 end if;
4836
4837 -- The unit is a subunit. The construct to inspect is the node which
4838 -- precedes the corresponding stub. Update the early call region to
4839 -- include the unit.
4840
4841 elsif Nkind (Context) = N_Subunit then
4842 Start := Unit;
4843 Curr := Corresponding_Stub (Context);
4844
4845 -- Otherwise the unit is nested. The construct to inspect is the node
4846 -- which precedes the unit. Update the early call region to include
4847 -- the unit.
4848
4849 else
4850 Include (Unit, Curr);
4851 end if;
4852 end Transition_Unit;
4853
4854 -- Local variables
4855
4856 Body_Id : constant Entity_Id := Defining_Entity (Body_Decl);
4857 Region : Node_Id;
4858
4859 -- Start of processing for Find_Early_Call_Region
4860
4861 begin
4862 -- The caller demands the start of the early call region without saving
4863 -- or retrieving it to/from internal data structures.
4864
4865 if Skip_Memoization then
4866 Region := Find_ECR (Body_Decl);
4867
4868 -- Default behavior
4869
4870 else
4871 -- Check whether the early call region of the subprogram body is
4872 -- available.
4873
4874 Region := Early_Call_Region (Body_Id);
4875
4876 if No (Region) then
4877
4878 -- Traverse the declarations in reverse order, starting from the
4879 -- subprogram body, searching for the nearest non-preelaborable
4880 -- construct. The early call region starts after this construct
4881 -- and ends at the subprogram body.
4882
4883 Region := Find_ECR (Body_Decl);
4884
4885 -- Associate the early call region with the subprogram body in
4886 -- case other scenarios need it.
4887
4888 Set_Early_Call_Region (Body_Id, Region);
4889 end if;
4890 end if;
4891
4892 -- A subprogram body must always have an early call region
4893
4894 pragma Assert (Present (Region));
4895
4896 return Region;
4897 end Find_Early_Call_Region;
4898
4899 ---------------------------
4900 -- Find_Elaborated_Units --
4901 ---------------------------
4902
4903 procedure Find_Elaborated_Units is
4904 procedure Add_Pragma (Prag : Node_Id);
4905 -- Determine whether pragma Prag denotes a legal Elaborate[_All] pragma.
4906 -- If this is the case, add the related unit to the elaboration context.
4907 -- For pragma Elaborate_All, include recursively all units withed by the
4908 -- related unit.
4909
4910 procedure Add_Unit
4911 (Unit_Id : Entity_Id;
4912 Prag : Node_Id;
4913 Full_Context : Boolean);
4914 -- Add unit Unit_Id to the elaboration context. Prag denotes the pragma
4915 -- which prompted the inclusion of the unit to the elaboration context.
4916 -- If flag Full_Context is set, examine the nonlimited clauses of unit
4917 -- Unit_Id and add each withed unit to the context.
4918
4919 procedure Find_Elaboration_Context (Comp_Unit : Node_Id);
4920 -- Examine the context items of compilation unit Comp_Unit for suitable
4921 -- elaboration-related pragmas and add all related units to the context.
4922
4923 ----------------
4924 -- Add_Pragma --
4925 ----------------
4926
4927 procedure Add_Pragma (Prag : Node_Id) is
4928 Prag_Args : constant List_Id := Pragma_Argument_Associations (Prag);
4929 Prag_Nam : constant Name_Id := Pragma_Name (Prag);
4930 Unit_Arg : Node_Id;
4931
4932 begin
4933 -- Nothing to do if the pragma is not related to elaboration
4934
4935 if not Nam_In (Prag_Nam, Name_Elaborate, Name_Elaborate_All) then
4936 return;
4937
4938 -- Nothing to do when the pragma is illegal
4939
4940 elsif Error_Posted (Prag) then
4941 return;
4942 end if;
4943
4944 Unit_Arg := Get_Pragma_Arg (First (Prag_Args));
4945
4946 -- The argument of the pragma may appear in package.package form
4947
4948 if Nkind (Unit_Arg) = N_Selected_Component then
4949 Unit_Arg := Selector_Name (Unit_Arg);
4950 end if;
4951
4952 Add_Unit
4953 (Unit_Id => Entity (Unit_Arg),
4954 Prag => Prag,
4955 Full_Context => Prag_Nam = Name_Elaborate_All);
4956 end Add_Pragma;
4957
4958 --------------
4959 -- Add_Unit --
4960 --------------
4961
4962 procedure Add_Unit
4963 (Unit_Id : Entity_Id;
4964 Prag : Node_Id;
4965 Full_Context : Boolean)
4966 is
4967 Clause : Node_Id;
4968 Elab_Attrs : Elaboration_Attributes;
4969
4970 begin
4971 -- Nothing to do when some previous error left a with clause or a
4972 -- pragma in a bad state.
4973
4974 if No (Unit_Id) then
4975 return;
4976 end if;
4977
4978 Elab_Attrs := Elaboration_Status (Unit_Id);
4979
4980 -- The unit is already included in the context by means of pragma
4981 -- Elaborate[_All].
4982
4983 if Present (Elab_Attrs.Source_Pragma) then
4984
4985 -- Upgrade an existing pragma Elaborate when the unit is subject
4986 -- to Elaborate_All because the new pragma covers a larger set of
4987 -- units.
4988
4989 if Pragma_Name (Elab_Attrs.Source_Pragma) = Name_Elaborate
4990 and then Pragma_Name (Prag) = Name_Elaborate_All
4991 then
4992 Elab_Attrs.Source_Pragma := Prag;
4993
4994 -- Otherwise the unit retains its existing pragma and does not
4995 -- need to be included in the context again.
4996
4997 else
4998 return;
4999 end if;
5000
5001 -- The current unit is not part of the context. Prepare a new set of
5002 -- attributes.
5003
5004 else
5005 Elab_Attrs :=
5006 Elaboration_Attributes'(Source_Pragma => Prag,
5007 With_Clause => Empty);
5008 end if;
5009
5010 -- Add or update the attributes of the unit
5011
5012 Set_Elaboration_Status (Unit_Id, Elab_Attrs);
5013
5014 -- Includes all units withed by the current one when computing the
5015 -- full context.
5016
5017 if Full_Context then
5018
5019 -- Process all nonlimited with clauses found in the context of
5020 -- the current unit. Note that limited clauses do not impose an
5021 -- elaboration order.
5022
5023 Clause := First (Context_Items (Compilation_Unit (Unit_Id)));
5024 while Present (Clause) loop
5025 if Nkind (Clause) = N_With_Clause
5026 and then not Error_Posted (Clause)
5027 and then not Limited_Present (Clause)
5028 then
5029 Add_Unit
5030 (Unit_Id => Entity (Name (Clause)),
5031 Prag => Prag,
5032 Full_Context => Full_Context);
5033 end if;
5034
5035 Next (Clause);
5036 end loop;
5037 end if;
5038 end Add_Unit;
5039
5040 ------------------------------
5041 -- Find_Elaboration_Context --
5042 ------------------------------
5043
5044 procedure Find_Elaboration_Context (Comp_Unit : Node_Id) is
5045 Prag : Node_Id;
5046
5047 begin
5048 pragma Assert (Nkind (Comp_Unit) = N_Compilation_Unit);
5049
5050 -- Process all elaboration-related pragmas found in the context of
5051 -- the compilation unit.
5052
5053 Prag := First (Context_Items (Comp_Unit));
5054 while Present (Prag) loop
5055 if Nkind (Prag) = N_Pragma then
5056 Add_Pragma (Prag);
5057 end if;
5058
5059 Next (Prag);
5060 end loop;
5061 end Find_Elaboration_Context;
5062
5063 -- Local variables
5064
5065 Par_Id : Entity_Id;
5066 Unt : Node_Id;
5067
5068 -- Start of processing for Find_Elaborated_Units
5069
5070 begin
5071 -- Perform a traversal which examines the context of the main unit and
5072 -- populates the Elaboration_Context table with all units elaborated
5073 -- prior to the main unit. The traversal performs the following jumps:
5074
5075 -- subunit -> parent subunit
5076 -- parent subunit -> body
5077 -- body -> spec
5078 -- spec -> parent spec
5079 -- parent spec -> grandparent spec and so on
5080
5081 -- The traversal relies on units rather than scopes because the scope of
5082 -- a subunit is some spec, while this traversal must process the body as
5083 -- well. Given that protected and task bodies can also be subunits, this
5084 -- complicates the scope approach even further.
5085
5086 Unt := Unit (Cunit (Main_Unit));
5087
5088 -- Perform the following traversals when the main unit is a subunit
5089
5090 -- subunit -> parent subunit
5091 -- parent subunit -> body
5092
5093 while Present (Unt) and then Nkind (Unt) = N_Subunit loop
5094 Find_Elaboration_Context (Parent (Unt));
5095
5096 -- Continue the traversal by going to the unit which contains the
5097 -- corresponding stub.
5098
5099 if Present (Corresponding_Stub (Unt)) then
5100 Unt := Unit (Cunit (Get_Source_Unit (Corresponding_Stub (Unt))));
5101
5102 -- Otherwise the subunit may be erroneous or left in a bad state
5103
5104 else
5105 exit;
5106 end if;
5107 end loop;
5108
5109 -- Perform the following traversal now that subunits have been taken
5110 -- care of, or the main unit is a body.
5111
5112 -- body -> spec
5113
5114 if Present (Unt)
5115 and then Nkind_In (Unt, N_Package_Body, N_Subprogram_Body)
5116 then
5117 Find_Elaboration_Context (Parent (Unt));
5118
5119 -- Continue the traversal by going to the unit which contains the
5120 -- corresponding spec.
5121
5122 if Present (Corresponding_Spec (Unt)) then
5123 Unt := Unit (Cunit (Get_Source_Unit (Corresponding_Spec (Unt))));
5124 end if;
5125 end if;
5126
5127 -- Perform the following traversals now that the body has been taken
5128 -- care of, or the main unit is a spec.
5129
5130 -- spec -> parent spec
5131 -- parent spec -> grandparent spec and so on
5132
5133 if Present (Unt)
5134 and then Nkind_In (Unt, N_Generic_Package_Declaration,
5135 N_Generic_Subprogram_Declaration,
5136 N_Package_Declaration,
5137 N_Subprogram_Declaration)
5138 then
5139 Find_Elaboration_Context (Parent (Unt));
5140
5141 -- Process a potential chain of parent units which ends with the
5142 -- main unit spec. The traversal can now safely rely on the scope
5143 -- chain.
5144
5145 Par_Id := Scope (Defining_Entity (Unt));
5146 while Present (Par_Id) and then Par_Id /= Standard_Standard loop
5147 Find_Elaboration_Context (Compilation_Unit (Par_Id));
5148
5149 Par_Id := Scope (Par_Id);
5150 end loop;
5151 end if;
5152 end Find_Elaborated_Units;
5153
5154 -----------------------------
5155 -- Find_Enclosing_Instance --
5156 -----------------------------
5157
5158 function Find_Enclosing_Instance (N : Node_Id) return Node_Id is
5159 Par : Node_Id;
5160 Spec_Id : Entity_Id;
5161
5162 begin
5163 -- Climb the parent chain looking for an enclosing instance spec or body
5164
5165 Par := N;
5166 while Present (Par) loop
5167
5168 -- Generic package or subprogram spec
5169
5170 if Nkind_In (Par, N_Package_Declaration,
5171 N_Subprogram_Declaration)
5172 and then Is_Generic_Instance (Defining_Entity (Par))
5173 then
5174 return Par;
5175
5176 -- Generic package or subprogram body
5177
5178 elsif Nkind_In (Par, N_Package_Body, N_Subprogram_Body) then
5179 Spec_Id := Corresponding_Spec (Par);
5180
5181 if Present (Spec_Id) and then Is_Generic_Instance (Spec_Id) then
5182 return Par;
5183 end if;
5184 end if;
5185
5186 Par := Parent (Par);
5187 end loop;
5188
5189 return Empty;
5190 end Find_Enclosing_Instance;
5191
5192 --------------------------
5193 -- Find_Enclosing_Level --
5194 --------------------------
5195
5196 function Find_Enclosing_Level (N : Node_Id) return Enclosing_Level_Kind is
5197 function Level_Of (Unit : Node_Id) return Enclosing_Level_Kind;
5198 -- Obtain the corresponding level of unit Unit
5199
5200 --------------
5201 -- Level_Of --
5202 --------------
5203
5204 function Level_Of (Unit : Node_Id) return Enclosing_Level_Kind is
5205 Spec_Id : Entity_Id;
5206
5207 begin
5208 if Nkind (Unit) in N_Generic_Instantiation then
5209 return Instantiation;
5210
5211 elsif Nkind (Unit) = N_Generic_Package_Declaration then
5212 return Generic_Package_Spec;
5213
5214 elsif Nkind (Unit) = N_Package_Declaration then
5215 return Package_Spec;
5216
5217 elsif Nkind (Unit) = N_Package_Body then
5218 Spec_Id := Corresponding_Spec (Unit);
5219
5220 -- The body belongs to a generic package
5221
5222 if Present (Spec_Id)
5223 and then Ekind (Spec_Id) = E_Generic_Package
5224 then
5225 return Generic_Package_Body;
5226
5227 -- Otherwise the body belongs to a non-generic package. This also
5228 -- treats an illegal package body without a corresponding spec as
5229 -- a non-generic package body.
5230
5231 else
5232 return Package_Body;
5233 end if;
5234 end if;
5235
5236 return No_Level;
5237 end Level_Of;
5238
5239 -- Local variables
5240
5241 Context : Node_Id;
5242 Curr : Node_Id;
5243 Prev : Node_Id;
5244
5245 -- Start of processing for Find_Enclosing_Level
5246
5247 begin
5248 -- Call markers and instantiations which appear at the declaration level
5249 -- but are later relocated in a different context retain their original
5250 -- declaration level.
5251
5252 if Nkind_In (N, N_Call_Marker,
5253 N_Function_Instantiation,
5254 N_Package_Instantiation,
5255 N_Procedure_Instantiation)
5256 and then Is_Declaration_Level_Node (N)
5257 then
5258 return Declaration_Level;
5259 end if;
5260
5261 -- Climb the parent chain looking at the enclosing levels
5262
5263 Prev := N;
5264 Curr := Parent (Prev);
5265 while Present (Curr) loop
5266
5267 -- A traversal from a subunit continues via the corresponding stub
5268
5269 if Nkind (Curr) = N_Subunit then
5270 Curr := Corresponding_Stub (Curr);
5271
5272 -- The current construct is a package. Packages are ignored because
5273 -- they are always elaborated when the enclosing context is invoked
5274 -- or elaborated.
5275
5276 elsif Nkind_In (Curr, N_Package_Body, N_Package_Declaration) then
5277 null;
5278
5279 -- The current construct is a block statement
5280
5281 elsif Nkind (Curr) = N_Block_Statement then
5282
5283 -- Ignore internally generated blocks created by the expander for
5284 -- various purposes such as abort defer/undefer.
5285
5286 if not Comes_From_Source (Curr) then
5287 null;
5288
5289 -- If the traversal came from the handled sequence of statments,
5290 -- then the node appears at the level of the enclosing construct.
5291 -- This is a more reliable test because transients scopes within
5292 -- the declarative region of the encapsulator are hard to detect.
5293
5294 elsif Nkind (Prev) = N_Handled_Sequence_Of_Statements
5295 and then Handled_Statement_Sequence (Curr) = Prev
5296 then
5297 return Find_Enclosing_Level (Parent (Curr));
5298
5299 -- Otherwise the traversal came from the declarations, the node is
5300 -- at the declaration level.
5301
5302 else
5303 return Declaration_Level;
5304 end if;
5305
5306 -- The current construct is a declaration-level encapsulator
5307
5308 elsif Nkind_In (Curr, N_Entry_Body,
5309 N_Subprogram_Body,
5310 N_Task_Body)
5311 then
5312 -- If the traversal came from the handled sequence of statments,
5313 -- then the node cannot possibly appear at any level. This is
5314 -- a more reliable test because transients scopes within the
5315 -- declarative region of the encapsulator are hard to detect.
5316
5317 if Nkind (Prev) = N_Handled_Sequence_Of_Statements
5318 and then Handled_Statement_Sequence (Curr) = Prev
5319 then
5320 return No_Level;
5321
5322 -- Otherwise the traversal came from the declarations, the node is
5323 -- at the declaration level.
5324
5325 else
5326 return Declaration_Level;
5327 end if;
5328
5329 -- The current construct is a non-library-level encapsulator which
5330 -- indicates that the node cannot possibly appear at any level.
5331 -- Note that this check must come after the declaration-level check
5332 -- because both predicates share certain nodes.
5333
5334 elsif Is_Non_Library_Level_Encapsulator (Curr) then
5335 Context := Parent (Curr);
5336
5337 -- The sole exception is when the encapsulator is the compilation
5338 -- utit itself because the compilation unit node requires special
5339 -- processing (see below).
5340
5341 if Present (Context)
5342 and then Nkind (Context) = N_Compilation_Unit
5343 then
5344 null;
5345
5346 -- Otherwise the node is not at any level
5347
5348 else
5349 return No_Level;
5350 end if;
5351
5352 -- The current construct is a compilation unit. The node appears at
5353 -- the [generic] library level when the unit is a [generic] package.
5354
5355 elsif Nkind (Curr) = N_Compilation_Unit then
5356 return Level_Of (Unit (Curr));
5357 end if;
5358
5359 Prev := Curr;
5360 Curr := Parent (Prev);
5361 end loop;
5362
5363 return No_Level;
5364 end Find_Enclosing_Level;
5365
5366 -------------------
5367 -- Find_Top_Unit --
5368 -------------------
5369
5370 function Find_Top_Unit (N : Node_Or_Entity_Id) return Entity_Id is
5371 begin
5372 return Find_Unit_Entity (Unit (Cunit (Get_Top_Level_Code_Unit (N))));
5373 end Find_Top_Unit;
5374
5375 ----------------------
5376 -- Find_Unit_Entity --
5377 ----------------------
5378
5379 function Find_Unit_Entity (N : Node_Id) return Entity_Id is
5380 Context : constant Node_Id := Parent (N);
5381 Orig_N : constant Node_Id := Original_Node (N);
5382
5383 begin
5384 -- The unit denotes a package body of an instantiation which acts as
5385 -- a compilation unit. The proper entity is that of the package spec.
5386
5387 if Nkind (N) = N_Package_Body
5388 and then Nkind (Orig_N) = N_Package_Instantiation
5389 and then Nkind (Context) = N_Compilation_Unit
5390 then
5391 return Corresponding_Spec (N);
5392
5393 -- The unit denotes an anonymous package created to wrap a subprogram
5394 -- instantiation which acts as a compilation unit. The proper entity is
5395 -- that of the "related instance".
5396
5397 elsif Nkind (N) = N_Package_Declaration
5398 and then Nkind_In (Orig_N, N_Function_Instantiation,
5399 N_Procedure_Instantiation)
5400 and then Nkind (Context) = N_Compilation_Unit
5401 then
5402 return
5403 Related_Instance (Defining_Entity (N, Concurrent_Subunit => True));
5404
5405 -- Otherwise the proper entity is the defining entity
5406
5407 else
5408 return Defining_Entity (N, Concurrent_Subunit => True);
5409 end if;
5410 end Find_Unit_Entity;
5411
5412 -----------------------
5413 -- First_Formal_Type --
5414 -----------------------
5415
5416 function First_Formal_Type (Subp_Id : Entity_Id) return Entity_Id is
5417 Formal_Id : constant Entity_Id := First_Formal (Subp_Id);
5418 Typ : Entity_Id;
5419
5420 begin
5421 if Present (Formal_Id) then
5422 Typ := Etype (Formal_Id);
5423
5424 -- Handle various combinations of concurrent and private types
5425
5426 loop
5427 if Ekind_In (Typ, E_Protected_Type, E_Task_Type)
5428 and then Present (Anonymous_Object (Typ))
5429 then
5430 Typ := Anonymous_Object (Typ);
5431
5432 elsif Is_Concurrent_Record_Type (Typ) then
5433 Typ := Corresponding_Concurrent_Type (Typ);
5434
5435 elsif Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5436 Typ := Full_View (Typ);
5437
5438 else
5439 exit;
5440 end if;
5441 end loop;
5442
5443 return Typ;
5444 end if;
5445
5446 return Empty;
5447 end First_Formal_Type;
5448
5449 --------------
5450 -- Has_Body --
5451 --------------
5452
5453 function Has_Body (Pack_Decl : Node_Id) return Boolean is
5454 function Find_Corresponding_Body (Spec_Id : Entity_Id) return Node_Id;
5455 -- Try to locate the corresponding body of spec Spec_Id. If no body is
5456 -- found, return Empty.
5457
5458 function Find_Body
5459 (Spec_Id : Entity_Id;
5460 From : Node_Id) return Node_Id;
5461 -- Try to locate the corresponding body of spec Spec_Id in the node list
5462 -- which follows arbitrary node From. If no body is found, return Empty.
5463
5464 function Load_Package_Body (Unit_Nam : Unit_Name_Type) return Node_Id;
5465 -- Attempt to load the body of unit Unit_Nam. If the load failed, return
5466 -- Empty. If the compilation will not generate code, return Empty.
5467
5468 -----------------------------
5469 -- Find_Corresponding_Body --
5470 -----------------------------
5471
5472 function Find_Corresponding_Body (Spec_Id : Entity_Id) return Node_Id is
5473 Context : constant Entity_Id := Scope (Spec_Id);
5474 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
5475 Body_Decl : Node_Id;
5476 Body_Id : Entity_Id;
5477
5478 begin
5479 if Is_Compilation_Unit (Spec_Id) then
5480 Body_Id := Corresponding_Body (Spec_Decl);
5481
5482 if Present (Body_Id) then
5483 return Unit_Declaration_Node (Body_Id);
5484
5485 -- The package is at the library and requires a body. Load the
5486 -- corresponding body because the optional body may be declared
5487 -- there.
5488
5489 elsif Unit_Requires_Body (Spec_Id) then
5490 return
5491 Load_Package_Body
5492 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec_Decl))));
5493
5494 -- Otherwise there is no optional body
5495
5496 else
5497 return Empty;
5498 end if;
5499
5500 -- The immediate context is a package. The optional body may be
5501 -- within the body of that package.
5502
5503 -- procedure Proc is
5504 -- package Nested_1 is
5505 -- package Nested_2 is
5506 -- generic
5507 -- package Pack is
5508 -- end Pack;
5509 -- end Nested_2;
5510 -- end Nested_1;
5511
5512 -- package body Nested_1 is
5513 -- package body Nested_2 is separate;
5514 -- end Nested_1;
5515
5516 -- separate (Proc.Nested_1.Nested_2)
5517 -- package body Nested_2 is
5518 -- package body Pack is -- optional body
5519 -- ...
5520 -- end Pack;
5521 -- end Nested_2;
5522
5523 elsif Is_Package_Or_Generic_Package (Context) then
5524 Body_Decl := Find_Corresponding_Body (Context);
5525
5526 -- The optional body is within the body of the enclosing package
5527
5528 if Present (Body_Decl) then
5529 return
5530 Find_Body
5531 (Spec_Id => Spec_Id,
5532 From => First (Declarations (Body_Decl)));
5533
5534 -- Otherwise the enclosing package does not have a body. This may
5535 -- be the result of an error or a genuine lack of a body.
5536
5537 else
5538 return Empty;
5539 end if;
5540
5541 -- Otherwise the immediate context is a body. The optional body may
5542 -- be within the same list as the spec.
5543
5544 -- procedure Proc is
5545 -- generic
5546 -- package Pack is
5547 -- end Pack;
5548
5549 -- package body Pack is -- optional body
5550 -- ...
5551 -- end Pack;
5552
5553 else
5554 return
5555 Find_Body
5556 (Spec_Id => Spec_Id,
5557 From => Next (Spec_Decl));
5558 end if;
5559 end Find_Corresponding_Body;
5560
5561 ---------------
5562 -- Find_Body --
5563 ---------------
5564
5565 function Find_Body
5566 (Spec_Id : Entity_Id;
5567 From : Node_Id) return Node_Id
5568 is
5569 Spec_Nam : constant Name_Id := Chars (Spec_Id);
5570 Item : Node_Id;
5571 Lib_Unit : Node_Id;
5572
5573 begin
5574 Item := From;
5575 while Present (Item) loop
5576
5577 -- The current item denotes the optional body
5578
5579 if Nkind (Item) = N_Package_Body
5580 and then Chars (Defining_Entity (Item)) = Spec_Nam
5581 then
5582 return Item;
5583
5584 -- The current item denotes a stub, the optional body may be in
5585 -- the subunit.
5586
5587 elsif Nkind (Item) = N_Package_Body_Stub
5588 and then Chars (Defining_Entity (Item)) = Spec_Nam
5589 then
5590 Lib_Unit := Library_Unit (Item);
5591
5592 -- The corresponding subunit was previously loaded
5593
5594 if Present (Lib_Unit) then
5595 return Lib_Unit;
5596
5597 -- Otherwise attempt to load the corresponding subunit
5598
5599 else
5600 return Load_Package_Body (Get_Unit_Name (Item));
5601 end if;
5602 end if;
5603
5604 Next (Item);
5605 end loop;
5606
5607 return Empty;
5608 end Find_Body;
5609
5610 -----------------------
5611 -- Load_Package_Body --
5612 -----------------------
5613
5614 function Load_Package_Body (Unit_Nam : Unit_Name_Type) return Node_Id is
5615 Body_Decl : Node_Id;
5616 Unit_Num : Unit_Number_Type;
5617
5618 begin
5619 -- The load is performed only when the compilation will generate code
5620
5621 if Operating_Mode = Generate_Code then
5622 Unit_Num :=
5623 Load_Unit
5624 (Load_Name => Unit_Nam,
5625 Required => False,
5626 Subunit => False,
5627 Error_Node => Pack_Decl);
5628
5629 -- The load failed most likely because the physical file is
5630 -- missing.
5631
5632 if Unit_Num = No_Unit then
5633 return Empty;
5634
5635 -- Otherwise the load was successful, return the body of the unit
5636
5637 else
5638 Body_Decl := Unit (Cunit (Unit_Num));
5639
5640 -- If the unit is a subunit with an available proper body,
5641 -- return the proper body.
5642
5643 if Nkind (Body_Decl) = N_Subunit
5644 and then Present (Proper_Body (Body_Decl))
5645 then
5646 Body_Decl := Proper_Body (Body_Decl);
5647 end if;
5648
5649 return Body_Decl;
5650 end if;
5651 end if;
5652
5653 return Empty;
5654 end Load_Package_Body;
5655
5656 -- Local variables
5657
5658 Pack_Id : constant Entity_Id := Defining_Entity (Pack_Decl);
5659
5660 -- Start of processing for Has_Body
5661
5662 begin
5663 -- The body is available
5664
5665 if Present (Corresponding_Body (Pack_Decl)) then
5666 return True;
5667
5668 -- The body is required if the package spec contains a construct which
5669 -- requires a completion in a body.
5670
5671 elsif Unit_Requires_Body (Pack_Id) then
5672 return True;
5673
5674 -- The body may be optional
5675
5676 else
5677 return Present (Find_Corresponding_Body (Pack_Id));
5678 end if;
5679 end Has_Body;
5680
5681 ---------------------------
5682 -- Has_Prior_Elaboration --
5683 ---------------------------
5684
5685 function Has_Prior_Elaboration
5686 (Unit_Id : Entity_Id;
5687 Context_OK : Boolean := False;
5688 Elab_Body_OK : Boolean := False;
5689 Same_Unit_OK : Boolean := False) return Boolean
5690 is
5691 Main_Id : constant Entity_Id := Cunit_Entity (Main_Unit);
5692
5693 begin
5694 -- A preelaborated unit is always elaborated prior to the main unit
5695
5696 if Is_Preelaborated_Unit (Unit_Id) then
5697 return True;
5698
5699 -- An internal unit is always elaborated prior to a non-internal main
5700 -- unit.
5701
5702 elsif In_Internal_Unit (Unit_Id)
5703 and then not In_Internal_Unit (Main_Id)
5704 then
5705 return True;
5706
5707 -- A unit has prior elaboration if it appears within the context of the
5708 -- main unit. Consider this case only when requested by the caller.
5709
5710 elsif Context_OK
5711 and then Elaboration_Status (Unit_Id) /= No_Elaboration_Attributes
5712 then
5713 return True;
5714
5715 -- A unit whose body is elaborated together with its spec has prior
5716 -- elaboration except with respect to itself. Consider this case only
5717 -- when requested by the caller.
5718
5719 elsif Elab_Body_OK
5720 and then Has_Pragma_Elaborate_Body (Unit_Id)
5721 and then not Is_Same_Unit (Unit_Id, Main_Id)
5722 then
5723 return True;
5724
5725 -- A unit has no prior elaboration with respect to itself, but does not
5726 -- require any means of ensuring its own elaboration either. Treat this
5727 -- case as valid prior elaboration only when requested by the caller.
5728
5729 elsif Same_Unit_OK and then Is_Same_Unit (Unit_Id, Main_Id) then
5730 return True;
5731 end if;
5732
5733 return False;
5734 end Has_Prior_Elaboration;
5735
5736 --------------------------
5737 -- In_External_Instance --
5738 --------------------------
5739
5740 function In_External_Instance
5741 (N : Node_Id;
5742 Target_Decl : Node_Id) return Boolean
5743 is
5744 Dummy : Node_Id;
5745 Inst_Body : Node_Id;
5746 Inst_Decl : Node_Id;
5747
5748 begin
5749 -- Performance note: parent traversal
5750
5751 Inst_Decl := Find_Enclosing_Instance (Target_Decl);
5752
5753 -- The target declaration appears within an instance spec. Visibility is
5754 -- ignored because internally generated primitives for private types may
5755 -- reside in the private declarations and still be invoked from outside.
5756
5757 if Present (Inst_Decl)
5758 and then Nkind (Inst_Decl) = N_Package_Declaration
5759 then
5760 -- The scenario comes from the main unit and the instance does not
5761
5762 if In_Extended_Main_Code_Unit (N)
5763 and then not In_Extended_Main_Code_Unit (Inst_Decl)
5764 then
5765 return True;
5766
5767 -- Otherwise the scenario must not appear within the instance spec or
5768 -- body.
5769
5770 else
5771 Extract_Instance_Attributes
5772 (Exp_Inst => Inst_Decl,
5773 Inst_Body => Inst_Body,
5774 Inst_Decl => Dummy);
5775
5776 -- Performance note: parent traversal
5777
5778 return not In_Subtree
5779 (N => N,
5780 Root1 => Inst_Decl,
5781 Root2 => Inst_Body);
5782 end if;
5783 end if;
5784
5785 return False;
5786 end In_External_Instance;
5787
5788 ---------------------
5789 -- In_Main_Context --
5790 ---------------------
5791
5792 function In_Main_Context (N : Node_Id) return Boolean is
5793 begin
5794 -- Scenarios outside the main unit are not considered because the ALI
5795 -- information supplied to binde is for the main unit only.
5796
5797 if not In_Extended_Main_Code_Unit (N) then
5798 return False;
5799
5800 -- Scenarios within internal units are not considered unless switch
5801 -- -gnatdE (elaboration checks on predefined units) is in effect.
5802
5803 elsif not Debug_Flag_EE and then In_Internal_Unit (N) then
5804 return False;
5805 end if;
5806
5807 return True;
5808 end In_Main_Context;
5809
5810 ---------------------
5811 -- In_Same_Context --
5812 ---------------------
5813
5814 function In_Same_Context
5815 (N1 : Node_Id;
5816 N2 : Node_Id;
5817 Nested_OK : Boolean := False) return Boolean
5818 is
5819 function Find_Enclosing_Context (N : Node_Id) return Node_Id;
5820 -- Return the nearest enclosing non-library-level or compilation unit
5821 -- node which which encapsulates arbitrary node N. Return Empty is no
5822 -- such context is available.
5823
5824 function In_Nested_Context
5825 (Outer : Node_Id;
5826 Inner : Node_Id) return Boolean;
5827 -- Determine whether arbitrary node Outer encapsulates arbitrary node
5828 -- Inner.
5829
5830 ----------------------------
5831 -- Find_Enclosing_Context --
5832 ----------------------------
5833
5834 function Find_Enclosing_Context (N : Node_Id) return Node_Id is
5835 Context : Node_Id;
5836 Par : Node_Id;
5837
5838 begin
5839 Par := Parent (N);
5840 while Present (Par) loop
5841
5842 -- A traversal from a subunit continues via the corresponding stub
5843
5844 if Nkind (Par) = N_Subunit then
5845 Par := Corresponding_Stub (Par);
5846
5847 -- Stop the traversal when the nearest enclosing non-library-level
5848 -- encapsulator has been reached.
5849
5850 elsif Is_Non_Library_Level_Encapsulator (Par) then
5851 Context := Parent (Par);
5852
5853 -- The sole exception is when the encapsulator is the unit of
5854 -- compilation because this case requires special processing
5855 -- (see below).
5856
5857 if Present (Context)
5858 and then Nkind (Context) = N_Compilation_Unit
5859 then
5860 null;
5861
5862 else
5863 return Par;
5864 end if;
5865
5866 -- Reaching a compilation unit node without hitting a non-library-
5867 -- level encapsulator indicates that N is at the library level in
5868 -- which case the compilation unit is the context.
5869
5870 elsif Nkind (Par) = N_Compilation_Unit then
5871 return Par;
5872 end if;
5873
5874 Par := Parent (Par);
5875 end loop;
5876
5877 return Empty;
5878 end Find_Enclosing_Context;
5879
5880 -----------------------
5881 -- In_Nested_Context --
5882 -----------------------
5883
5884 function In_Nested_Context
5885 (Outer : Node_Id;
5886 Inner : Node_Id) return Boolean
5887 is
5888 Par : Node_Id;
5889
5890 begin
5891 Par := Inner;
5892 while Present (Par) loop
5893
5894 -- A traversal from a subunit continues via the corresponding stub
5895
5896 if Nkind (Par) = N_Subunit then
5897 Par := Corresponding_Stub (Par);
5898
5899 elsif Par = Outer then
5900 return True;
5901 end if;
5902
5903 Par := Parent (Par);
5904 end loop;
5905
5906 return False;
5907 end In_Nested_Context;
5908
5909 -- Local variables
5910
5911 Context_1 : constant Node_Id := Find_Enclosing_Context (N1);
5912 Context_2 : constant Node_Id := Find_Enclosing_Context (N2);
5913
5914 -- Start of processing for In_Same_Context
5915
5916 begin
5917 -- Both nodes appear within the same context
5918
5919 if Context_1 = Context_2 then
5920 return True;
5921
5922 -- Both nodes appear in compilation units. Determine whether one unit
5923 -- is the body of the other.
5924
5925 elsif Nkind (Context_1) = N_Compilation_Unit
5926 and then Nkind (Context_2) = N_Compilation_Unit
5927 then
5928 return
5929 Is_Same_Unit
5930 (Unit_1 => Defining_Entity (Unit (Context_1)),
5931 Unit_2 => Defining_Entity (Unit (Context_2)));
5932
5933 -- The context of N1 encloses the context of N2
5934
5935 elsif Nested_OK and then In_Nested_Context (Context_1, Context_2) then
5936 return True;
5937 end if;
5938
5939 return False;
5940 end In_Same_Context;
5941
5942 ----------------
5943 -- Initialize --
5944 ----------------
5945
5946 procedure Initialize is
5947 begin
5948 -- Set the soft link which enables Atree.Rewrite to update a top-level
5949 -- scenario each time it is transformed into another node.
5950
5951 Set_Rewriting_Proc (Update_Elaboration_Scenario'Access);
5952 end Initialize;
5953
5954 ---------------
5955 -- Info_Call --
5956 ---------------
5957
5958 procedure Info_Call
5959 (Call : Node_Id;
5960 Target_Id : Entity_Id;
5961 Info_Msg : Boolean;
5962 In_SPARK : Boolean)
5963 is
5964 procedure Info_Accept_Alternative;
5965 pragma Inline (Info_Accept_Alternative);
5966 -- Output information concerning an accept alternative
5967
5968 procedure Info_Simple_Call;
5969 pragma Inline (Info_Simple_Call);
5970 -- Output information concerning the call
5971
5972 procedure Info_Type_Actions (Action : String);
5973 pragma Inline (Info_Type_Actions);
5974 -- Output information concerning action Action of a type
5975
5976 procedure Info_Verification_Call
5977 (Pred : String;
5978 Id : Entity_Id;
5979 Id_Kind : String);
5980 pragma Inline (Info_Verification_Call);
5981 -- Output information concerning the verification of predicate Pred
5982 -- applied to related entity Id with kind Id_Kind.
5983
5984 -----------------------------
5985 -- Info_Accept_Alternative --
5986 -----------------------------
5987
5988 procedure Info_Accept_Alternative is
5989 Entry_Id : constant Entity_Id := Receiving_Entry (Target_Id);
5990
5991 begin
5992 pragma Assert (Present (Entry_Id));
5993
5994 Elab_Msg_NE
5995 (Msg => "accept for entry & during elaboration",
5996 N => Call,
5997 Id => Entry_Id,
5998 Info_Msg => Info_Msg,
5999 In_SPARK => In_SPARK);
6000 end Info_Accept_Alternative;
6001
6002 ----------------------
6003 -- Info_Simple_Call --
6004 ----------------------
6005
6006 procedure Info_Simple_Call is
6007 begin
6008 Elab_Msg_NE
6009 (Msg => "call to & during elaboration",
6010 N => Call,
6011 Id => Target_Id,
6012 Info_Msg => Info_Msg,
6013 In_SPARK => In_SPARK);
6014 end Info_Simple_Call;
6015
6016 -----------------------
6017 -- Info_Type_Actions --
6018 -----------------------
6019
6020 procedure Info_Type_Actions (Action : String) is
6021 Typ : constant Entity_Id := First_Formal_Type (Target_Id);
6022
6023 begin
6024 pragma Assert (Present (Typ));
6025
6026 Elab_Msg_NE
6027 (Msg => Action & " actions for type & during elaboration",
6028 N => Call,
6029 Id => Typ,
6030 Info_Msg => Info_Msg,
6031 In_SPARK => In_SPARK);
6032 end Info_Type_Actions;
6033
6034 ----------------------------
6035 -- Info_Verification_Call --
6036 ----------------------------
6037
6038 procedure Info_Verification_Call
6039 (Pred : String;
6040 Id : Entity_Id;
6041 Id_Kind : String)
6042 is
6043 begin
6044 pragma Assert (Present (Id));
6045
6046 Elab_Msg_NE
6047 (Msg =>
6048 "verification of " & Pred & " of " & Id_Kind & " & during "
6049 & "elaboration",
6050 N => Call,
6051 Id => Id,
6052 Info_Msg => Info_Msg,
6053 In_SPARK => In_SPARK);
6054 end Info_Verification_Call;
6055
6056 -- Start of processing for Info_Call
6057
6058 begin
6059 -- Do not output anything for targets defined in internal units because
6060 -- this creates noise.
6061
6062 if not In_Internal_Unit (Target_Id) then
6063
6064 -- Accept alternative
6065
6066 if Is_Accept_Alternative_Proc (Target_Id) then
6067 Info_Accept_Alternative;
6068
6069 -- Adjustment
6070
6071 elsif Is_TSS (Target_Id, TSS_Deep_Adjust) then
6072 Info_Type_Actions ("adjustment");
6073
6074 -- Default_Initial_Condition
6075
6076 elsif Is_Default_Initial_Condition_Proc (Target_Id) then
6077 Info_Verification_Call
6078 (Pred => "Default_Initial_Condition",
6079 Id => First_Formal_Type (Target_Id),
6080 Id_Kind => "type");
6081
6082 -- Entries
6083
6084 elsif Is_Protected_Entry (Target_Id) then
6085 Info_Simple_Call;
6086
6087 -- Task entry calls are never processed because the entry being
6088 -- invoked does not have a corresponding "body", it has a select.
6089
6090 elsif Is_Task_Entry (Target_Id) then
6091 null;
6092
6093 -- Finalization
6094
6095 elsif Is_TSS (Target_Id, TSS_Deep_Finalize) then
6096 Info_Type_Actions ("finalization");
6097
6098 -- Calls to _Finalizer procedures must not appear in the output
6099 -- because this creates confusing noise.
6100
6101 elsif Is_Finalizer_Proc (Target_Id) then
6102 null;
6103
6104 -- Initial_Condition
6105
6106 elsif Is_Initial_Condition_Proc (Target_Id) then
6107 Info_Verification_Call
6108 (Pred => "Initial_Condition",
6109 Id => Find_Enclosing_Scope (Call),
6110 Id_Kind => "package");
6111
6112 -- Initialization
6113
6114 elsif Is_Init_Proc (Target_Id)
6115 or else Is_TSS (Target_Id, TSS_Deep_Initialize)
6116 then
6117 Info_Type_Actions ("initialization");
6118
6119 -- Invariant
6120
6121 elsif Is_Invariant_Proc (Target_Id) then
6122 Info_Verification_Call
6123 (Pred => "invariants",
6124 Id => First_Formal_Type (Target_Id),
6125 Id_Kind => "type");
6126
6127 -- Partial invariant calls must not appear in the output because this
6128 -- creates confusing noise.
6129
6130 elsif Is_Partial_Invariant_Proc (Target_Id) then
6131 null;
6132
6133 -- _Postconditions
6134
6135 elsif Is_Postconditions_Proc (Target_Id) then
6136 Info_Verification_Call
6137 (Pred => "postconditions",
6138 Id => Find_Enclosing_Scope (Call),
6139 Id_Kind => "subprogram");
6140
6141 -- Subprograms must come last because some of the previous cases fall
6142 -- under this category.
6143
6144 elsif Ekind (Target_Id) = E_Function then
6145 Info_Simple_Call;
6146
6147 elsif Ekind (Target_Id) = E_Procedure then
6148 Info_Simple_Call;
6149
6150 else
6151 pragma Assert (False);
6152 null;
6153 end if;
6154 end if;
6155 end Info_Call;
6156
6157 ------------------------
6158 -- Info_Instantiation --
6159 ------------------------
6160
6161 procedure Info_Instantiation
6162 (Inst : Node_Id;
6163 Gen_Id : Entity_Id;
6164 Info_Msg : Boolean;
6165 In_SPARK : Boolean)
6166 is
6167 begin
6168 Elab_Msg_NE
6169 (Msg => "instantiation of & during elaboration",
6170 N => Inst,
6171 Id => Gen_Id,
6172 Info_Msg => Info_Msg,
6173 In_SPARK => In_SPARK);
6174 end Info_Instantiation;
6175
6176 -----------------------------
6177 -- Info_Variable_Reference --
6178 -----------------------------
6179
6180 procedure Info_Variable_Reference
6181 (Ref : Node_Id;
6182 Var_Id : Entity_Id;
6183 Info_Msg : Boolean;
6184 In_SPARK : Boolean)
6185 is
6186 begin
6187 if Is_Read (Ref) then
6188 Elab_Msg_NE
6189 (Msg => "read of variable & during elaboration",
6190 N => Ref,
6191 Id => Var_Id,
6192 Info_Msg => Info_Msg,
6193 In_SPARK => In_SPARK);
6194 end if;
6195 end Info_Variable_Reference;
6196
6197 --------------------
6198 -- Insertion_Node --
6199 --------------------
6200
6201 function Insertion_Node (N : Node_Id; Ins_Nod : Node_Id) return Node_Id is
6202 begin
6203 -- When the scenario denotes an instantiation, the proper insertion node
6204 -- is the instance spec. This ensures that the generic actuals will not
6205 -- be evaluated prior to a potential ABE.
6206
6207 if Nkind (N) in N_Generic_Instantiation
6208 and then Present (Instance_Spec (N))
6209 then
6210 return Instance_Spec (N);
6211
6212 -- Otherwise the proper insertion node is the candidate insertion node
6213
6214 else
6215 return Ins_Nod;
6216 end if;
6217 end Insertion_Node;
6218
6219 -----------------------
6220 -- Install_ABE_Check --
6221 -----------------------
6222
6223 procedure Install_ABE_Check
6224 (N : Node_Id;
6225 Id : Entity_Id;
6226 Ins_Nod : Node_Id)
6227 is
6228 Check_Ins_Nod : constant Node_Id := Insertion_Node (N, Ins_Nod);
6229 -- Insert the check prior to this node
6230
6231 Loc : constant Source_Ptr := Sloc (N);
6232 Spec_Id : constant Entity_Id := Unique_Entity (Id);
6233 Unit_Id : constant Entity_Id := Find_Top_Unit (Id);
6234 Scop_Id : Entity_Id;
6235
6236 begin
6237 -- Nothing to do when compiling for GNATprove because raise statements
6238 -- are not supported.
6239
6240 if GNATprove_Mode then
6241 return;
6242
6243 -- Nothing to do when the compilation will not produce an executable
6244
6245 elsif Serious_Errors_Detected > 0 then
6246 return;
6247
6248 -- Nothing to do for a compilation unit because there is no executable
6249 -- environment at that level.
6250
6251 elsif Nkind (Parent (Check_Ins_Nod)) = N_Compilation_Unit then
6252 return;
6253
6254 -- Nothing to do when the unit is elaborated prior to the main unit.
6255 -- This check must also consider the following cases:
6256
6257 -- * Id's unit appears in the context of the main unit
6258
6259 -- * Id's unit is subject to pragma Elaborate_Body. An ABE check MUST
6260 -- NOT be generated because Id's unit is always elaborated prior to
6261 -- the main unit.
6262
6263 -- * Id's unit is the main unit. An ABE check MUST be generated in this
6264 -- case because a conditional ABE may be raised depending on the flow
6265 -- of execution within the main unit (flag Same_Unit_OK is False).
6266
6267 elsif Has_Prior_Elaboration
6268 (Unit_Id => Unit_Id,
6269 Context_OK => True,
6270 Elab_Body_OK => True)
6271 then
6272 return;
6273 end if;
6274
6275 -- Prevent multiple scenarios from installing the same ABE check
6276
6277 Set_Is_Elaboration_Checks_OK_Node (N, False);
6278
6279 -- Install the nearest enclosing scope of the scenario as there must be
6280 -- something on the scope stack.
6281
6282 -- Performance note: parent traversal
6283
6284 Scop_Id := Find_Enclosing_Scope (Check_Ins_Nod);
6285 pragma Assert (Present (Scop_Id));
6286
6287 Push_Scope (Scop_Id);
6288
6289 -- Generate:
6290 -- if not Spec_Id'Elaborated then
6291 -- raise Program_Error with "access before elaboration";
6292 -- end if;
6293
6294 Insert_Action (Check_Ins_Nod,
6295 Make_Raise_Program_Error (Loc,
6296 Condition =>
6297 Make_Op_Not (Loc,
6298 Right_Opnd =>
6299 Make_Attribute_Reference (Loc,
6300 Prefix => New_Occurrence_Of (Spec_Id, Loc),
6301 Attribute_Name => Name_Elaborated)),
6302 Reason => PE_Access_Before_Elaboration));
6303
6304 Pop_Scope;
6305 end Install_ABE_Check;
6306
6307 -----------------------
6308 -- Install_ABE_Check --
6309 -----------------------
6310
6311 procedure Install_ABE_Check
6312 (N : Node_Id;
6313 Target_Id : Entity_Id;
6314 Target_Decl : Node_Id;
6315 Target_Body : Node_Id;
6316 Ins_Nod : Node_Id)
6317 is
6318 procedure Build_Elaboration_Entity;
6319 pragma Inline (Build_Elaboration_Entity);
6320 -- Create a new elaboration flag for Target_Id, insert it prior to
6321 -- Target_Decl, and set it after Body_Decl.
6322
6323 ------------------------------
6324 -- Build_Elaboration_Entity --
6325 ------------------------------
6326
6327 procedure Build_Elaboration_Entity is
6328 Loc : constant Source_Ptr := Sloc (Target_Id);
6329 Flag_Id : Entity_Id;
6330
6331 begin
6332 -- Create the declaration of the elaboration flag. The name carries a
6333 -- unique counter in case of name overloading.
6334
6335 Flag_Id :=
6336 Make_Defining_Identifier (Loc,
6337 Chars => New_External_Name (Chars (Target_Id), 'E', -1));
6338
6339 Set_Elaboration_Entity (Target_Id, Flag_Id);
6340 Set_Elaboration_Entity_Required (Target_Id);
6341
6342 Push_Scope (Scope (Target_Id));
6343
6344 -- Generate:
6345 -- Enn : Short_Integer := 0;
6346
6347 Insert_Action (Target_Decl,
6348 Make_Object_Declaration (Loc,
6349 Defining_Identifier => Flag_Id,
6350 Object_Definition =>
6351 New_Occurrence_Of (Standard_Short_Integer, Loc),
6352 Expression => Make_Integer_Literal (Loc, Uint_0)));
6353
6354 -- Generate:
6355 -- Enn := 1;
6356
6357 Set_Elaboration_Flag (Target_Body, Target_Id);
6358
6359 Pop_Scope;
6360 end Build_Elaboration_Entity;
6361
6362 -- Local variables
6363
6364 Target_Unit_Id : constant Entity_Id := Find_Top_Unit (Target_Id);
6365
6366 -- Start for processing for Install_ABE_Check
6367
6368 begin
6369 -- Nothing to do when compiling for GNATprove because raise statements
6370 -- are not supported.
6371
6372 if GNATprove_Mode then
6373 return;
6374
6375 -- Nothing to do when the compilation will not produce an executable
6376
6377 elsif Serious_Errors_Detected > 0 then
6378 return;
6379
6380 -- Nothing to do when the target is a protected subprogram because the
6381 -- check is associated with the protected body subprogram.
6382
6383 elsif Is_Protected_Subp (Target_Id) then
6384 return;
6385
6386 -- Nothing to do when the target is elaborated prior to the main unit.
6387 -- This check must also consider the following cases:
6388
6389 -- * The unit of the target appears in the context of the main unit
6390
6391 -- * The unit of the target is subject to pragma Elaborate_Body. An ABE
6392 -- check MUST NOT be generated because the unit is always elaborated
6393 -- prior to the main unit.
6394
6395 -- * The unit of the target is the main unit. An ABE check MUST be added
6396 -- in this case because a conditional ABE may be raised depending on
6397 -- the flow of execution within the main unit (flag Same_Unit_OK is
6398 -- False).
6399
6400 elsif Has_Prior_Elaboration
6401 (Unit_Id => Target_Unit_Id,
6402 Context_OK => True,
6403 Elab_Body_OK => True)
6404 then
6405 return;
6406
6407 -- Create an elaboration flag for the target when it does not have one
6408
6409 elsif No (Elaboration_Entity (Target_Id)) then
6410 Build_Elaboration_Entity;
6411 end if;
6412
6413 Install_ABE_Check
6414 (N => N,
6415 Ins_Nod => Ins_Nod,
6416 Id => Target_Id);
6417 end Install_ABE_Check;
6418
6419 -------------------------
6420 -- Install_ABE_Failure --
6421 -------------------------
6422
6423 procedure Install_ABE_Failure (N : Node_Id; Ins_Nod : Node_Id) is
6424 Fail_Ins_Nod : constant Node_Id := Insertion_Node (N, Ins_Nod);
6425 -- Insert the failure prior to this node
6426
6427 Loc : constant Source_Ptr := Sloc (N);
6428 Scop_Id : Entity_Id;
6429
6430 begin
6431 -- Nothing to do when compiling for GNATprove because raise statements
6432 -- are not supported.
6433
6434 if GNATprove_Mode then
6435 return;
6436
6437 -- Nothing to do when the compilation will not produce an executable
6438
6439 elsif Serious_Errors_Detected > 0 then
6440 return;
6441
6442 -- Do not install an ABE check for a compilation unit because there is
6443 -- no executable environment at that level.
6444
6445 elsif Nkind (Parent (Fail_Ins_Nod)) = N_Compilation_Unit then
6446 return;
6447 end if;
6448
6449 -- Prevent multiple scenarios from installing the same ABE failure
6450
6451 Set_Is_Elaboration_Checks_OK_Node (N, False);
6452
6453 -- Install the nearest enclosing scope of the scenario as there must be
6454 -- something on the scope stack.
6455
6456 -- Performance note: parent traversal
6457
6458 Scop_Id := Find_Enclosing_Scope (Fail_Ins_Nod);
6459 pragma Assert (Present (Scop_Id));
6460
6461 Push_Scope (Scop_Id);
6462
6463 -- Generate:
6464 -- raise Program_Error with "access before elaboration";
6465
6466 Insert_Action (Fail_Ins_Nod,
6467 Make_Raise_Program_Error (Loc,
6468 Reason => PE_Access_Before_Elaboration));
6469
6470 Pop_Scope;
6471 end Install_ABE_Failure;
6472
6473 --------------------------------
6474 -- Is_Accept_Alternative_Proc --
6475 --------------------------------
6476
6477 function Is_Accept_Alternative_Proc (Id : Entity_Id) return Boolean is
6478 begin
6479 -- To qualify, the entity must denote a procedure with a receiving entry
6480
6481 return Ekind (Id) = E_Procedure and then Present (Receiving_Entry (Id));
6482 end Is_Accept_Alternative_Proc;
6483
6484 ------------------------
6485 -- Is_Activation_Proc --
6486 ------------------------
6487
6488 function Is_Activation_Proc (Id : Entity_Id) return Boolean is
6489 begin
6490 -- To qualify, the entity must denote one of the runtime procedures in
6491 -- charge of task activation.
6492
6493 if Ekind (Id) = E_Procedure then
6494 if Restricted_Profile then
6495 return Is_RTE (Id, RE_Activate_Restricted_Tasks);
6496 else
6497 return Is_RTE (Id, RE_Activate_Tasks);
6498 end if;
6499 end if;
6500
6501 return False;
6502 end Is_Activation_Proc;
6503
6504 ----------------------------
6505 -- Is_Ada_Semantic_Target --
6506 ----------------------------
6507
6508 function Is_Ada_Semantic_Target (Id : Entity_Id) return Boolean is
6509 begin
6510 return
6511 Is_Activation_Proc (Id)
6512 or else Is_Controlled_Proc (Id, Name_Adjust)
6513 or else Is_Controlled_Proc (Id, Name_Finalize)
6514 or else Is_Controlled_Proc (Id, Name_Initialize)
6515 or else Is_Init_Proc (Id)
6516 or else Is_Invariant_Proc (Id)
6517 or else Is_Protected_Entry (Id)
6518 or else Is_Protected_Subp (Id)
6519 or else Is_Protected_Body_Subp (Id)
6520 or else Is_Task_Entry (Id);
6521 end Is_Ada_Semantic_Target;
6522
6523 ----------------------------
6524 -- Is_Bodiless_Subprogram --
6525 ----------------------------
6526
6527 function Is_Bodiless_Subprogram (Subp_Id : Entity_Id) return Boolean is
6528 begin
6529 -- An abstract subprogram does not have a body
6530
6531 if Ekind_In (Subp_Id, E_Function,
6532 E_Operator,
6533 E_Procedure)
6534 and then Is_Abstract_Subprogram (Subp_Id)
6535 then
6536 return True;
6537
6538 -- A formal subprogram does not have a body
6539
6540 elsif Is_Formal_Subprogram (Subp_Id) then
6541 return True;
6542
6543 -- An imported subprogram may have a body, however it is not known at
6544 -- compile or bind time where the body resides and whether it will be
6545 -- elaborated on time.
6546
6547 elsif Is_Imported (Subp_Id) then
6548 return True;
6549 end if;
6550
6551 return False;
6552 end Is_Bodiless_Subprogram;
6553
6554 ------------------------
6555 -- Is_Controlled_Proc --
6556 ------------------------
6557
6558 function Is_Controlled_Proc
6559 (Subp_Id : Entity_Id;
6560 Subp_Nam : Name_Id) return Boolean
6561 is
6562 Formal_Id : Entity_Id;
6563
6564 begin
6565 pragma Assert (Nam_In (Subp_Nam, Name_Adjust,
6566 Name_Finalize,
6567 Name_Initialize));
6568
6569 -- To qualify, the subprogram must denote a source procedure with name
6570 -- Adjust, Finalize, or Initialize where the sole formal is controlled.
6571
6572 if Comes_From_Source (Subp_Id)
6573 and then Ekind (Subp_Id) = E_Procedure
6574 and then Chars (Subp_Id) = Subp_Nam
6575 then
6576 Formal_Id := First_Formal (Subp_Id);
6577
6578 return
6579 Present (Formal_Id)
6580 and then Is_Controlled (Etype (Formal_Id))
6581 and then No (Next_Formal (Formal_Id));
6582 end if;
6583
6584 return False;
6585 end Is_Controlled_Proc;
6586
6587 ---------------------------------------
6588 -- Is_Default_Initial_Condition_Proc --
6589 ---------------------------------------
6590
6591 function Is_Default_Initial_Condition_Proc
6592 (Id : Entity_Id) return Boolean
6593 is
6594 begin
6595 -- To qualify, the entity must denote a Default_Initial_Condition
6596 -- procedure.
6597
6598 return Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id);
6599 end Is_Default_Initial_Condition_Proc;
6600
6601 -----------------------
6602 -- Is_Finalizer_Proc --
6603 -----------------------
6604
6605 function Is_Finalizer_Proc (Id : Entity_Id) return Boolean is
6606 begin
6607 -- To qualify, the entity must denote a _Finalizer procedure
6608
6609 return Ekind (Id) = E_Procedure and then Chars (Id) = Name_uFinalizer;
6610 end Is_Finalizer_Proc;
6611
6612 -----------------------
6613 -- Is_Guaranteed_ABE --
6614 -----------------------
6615
6616 function Is_Guaranteed_ABE
6617 (N : Node_Id;
6618 Target_Decl : Node_Id;
6619 Target_Body : Node_Id) return Boolean
6620 is
6621 begin
6622 -- Avoid cascaded errors if there were previous serious infractions.
6623 -- As a result the scenario will not be treated as a guaranteed ABE.
6624 -- This behaviour parallels that of the old ABE mechanism.
6625
6626 if Serious_Errors_Detected > 0 then
6627 return False;
6628
6629 -- The scenario and the target appear within the same context ignoring
6630 -- enclosing library levels.
6631
6632 -- Performance note: parent traversal
6633
6634 elsif In_Same_Context (N, Target_Decl) then
6635
6636 -- The target body has already been encountered. The scenario results
6637 -- in a guaranteed ABE if it appears prior to the body.
6638
6639 if Present (Target_Body) then
6640 return Earlier_In_Extended_Unit (N, Target_Body);
6641
6642 -- Otherwise the body has not been encountered yet. The scenario is
6643 -- a guaranteed ABE since the body will appear later. It is assumed
6644 -- that the caller has already checked whether the scenario is ABE-
6645 -- safe as optional bodies are not considered here.
6646
6647 else
6648 return True;
6649 end if;
6650 end if;
6651
6652 return False;
6653 end Is_Guaranteed_ABE;
6654
6655 -------------------------------
6656 -- Is_Initial_Condition_Proc --
6657 -------------------------------
6658
6659 function Is_Initial_Condition_Proc (Id : Entity_Id) return Boolean is
6660 begin
6661 -- To qualify, the entity must denote an Initial_Condition procedure
6662
6663 return
6664 Ekind (Id) = E_Procedure and then Is_Initial_Condition_Procedure (Id);
6665 end Is_Initial_Condition_Proc;
6666
6667 --------------------
6668 -- Is_Initialized --
6669 --------------------
6670
6671 function Is_Initialized (Obj_Decl : Node_Id) return Boolean is
6672 begin
6673 -- To qualify, the object declaration must have an expression
6674
6675 return
6676 Present (Expression (Obj_Decl)) or else Has_Init_Expression (Obj_Decl);
6677 end Is_Initialized;
6678
6679 -----------------------
6680 -- Is_Invariant_Proc --
6681 -----------------------
6682
6683 function Is_Invariant_Proc (Id : Entity_Id) return Boolean is
6684 begin
6685 -- To qualify, the entity must denote the "full" invariant procedure
6686
6687 return Ekind (Id) = E_Procedure and then Is_Invariant_Procedure (Id);
6688 end Is_Invariant_Proc;
6689
6690 ---------------------------------------
6691 -- Is_Non_Library_Level_Encapsulator --
6692 ---------------------------------------
6693
6694 function Is_Non_Library_Level_Encapsulator (N : Node_Id) return Boolean is
6695 begin
6696 case Nkind (N) is
6697 when N_Abstract_Subprogram_Declaration
6698 | N_Aspect_Specification
6699 | N_Component_Declaration
6700 | N_Entry_Body
6701 | N_Entry_Declaration
6702 | N_Expression_Function
6703 | N_Formal_Abstract_Subprogram_Declaration
6704 | N_Formal_Concrete_Subprogram_Declaration
6705 | N_Formal_Object_Declaration
6706 | N_Formal_Package_Declaration
6707 | N_Formal_Type_Declaration
6708 | N_Generic_Association
6709 | N_Implicit_Label_Declaration
6710 | N_Incomplete_Type_Declaration
6711 | N_Private_Extension_Declaration
6712 | N_Private_Type_Declaration
6713 | N_Protected_Body
6714 | N_Protected_Type_Declaration
6715 | N_Single_Protected_Declaration
6716 | N_Single_Task_Declaration
6717 | N_Subprogram_Body
6718 | N_Subprogram_Declaration
6719 | N_Task_Body
6720 | N_Task_Type_Declaration
6721 =>
6722 return True;
6723
6724 when others =>
6725 return Is_Generic_Declaration_Or_Body (N);
6726 end case;
6727 end Is_Non_Library_Level_Encapsulator;
6728
6729 -------------------------------
6730 -- Is_Partial_Invariant_Proc --
6731 -------------------------------
6732
6733 function Is_Partial_Invariant_Proc (Id : Entity_Id) return Boolean is
6734 begin
6735 -- To qualify, the entity must denote the "partial" invariant procedure
6736
6737 return
6738 Ekind (Id) = E_Procedure and then Is_Partial_Invariant_Procedure (Id);
6739 end Is_Partial_Invariant_Proc;
6740
6741 ----------------------------
6742 -- Is_Postconditions_Proc --
6743 ----------------------------
6744
6745 function Is_Postconditions_Proc (Id : Entity_Id) return Boolean is
6746 begin
6747 -- To qualify, the entity must denote a _Postconditions procedure
6748
6749 return
6750 Ekind (Id) = E_Procedure and then Chars (Id) = Name_uPostconditions;
6751 end Is_Postconditions_Proc;
6752
6753 ---------------------------
6754 -- Is_Preelaborated_Unit --
6755 ---------------------------
6756
6757 function Is_Preelaborated_Unit (Id : Entity_Id) return Boolean is
6758 begin
6759 return
6760 Is_Preelaborated (Id)
6761 or else Is_Pure (Id)
6762 or else Is_Remote_Call_Interface (Id)
6763 or else Is_Remote_Types (Id)
6764 or else Is_Shared_Passive (Id);
6765 end Is_Preelaborated_Unit;
6766
6767 ------------------------
6768 -- Is_Protected_Entry --
6769 ------------------------
6770
6771 function Is_Protected_Entry (Id : Entity_Id) return Boolean is
6772 begin
6773 -- To qualify, the entity must denote an entry defined in a protected
6774 -- type.
6775
6776 return
6777 Is_Entry (Id)
6778 and then Is_Protected_Type (Non_Private_View (Scope (Id)));
6779 end Is_Protected_Entry;
6780
6781 -----------------------
6782 -- Is_Protected_Subp --
6783 -----------------------
6784
6785 function Is_Protected_Subp (Id : Entity_Id) return Boolean is
6786 begin
6787 -- To qualify, the entity must denote a subprogram defined within a
6788 -- protected type.
6789
6790 return
6791 Ekind_In (Id, E_Function, E_Procedure)
6792 and then Is_Protected_Type (Non_Private_View (Scope (Id)));
6793 end Is_Protected_Subp;
6794
6795 ----------------------------
6796 -- Is_Protected_Body_Subp --
6797 ----------------------------
6798
6799 function Is_Protected_Body_Subp (Id : Entity_Id) return Boolean is
6800 begin
6801 -- To qualify, the entity must denote a subprogram with attribute
6802 -- Protected_Subprogram set.
6803
6804 return
6805 Ekind_In (Id, E_Function, E_Procedure)
6806 and then Present (Protected_Subprogram (Id));
6807 end Is_Protected_Body_Subp;
6808
6809 --------------------------------
6810 -- Is_Recorded_SPARK_Scenario --
6811 --------------------------------
6812
6813 function Is_Recorded_SPARK_Scenario (N : Node_Id) return Boolean is
6814 begin
6815 if Recorded_SPARK_Scenarios_In_Use then
6816 return Recorded_SPARK_Scenarios.Get (N);
6817 end if;
6818
6819 return Recorded_SPARK_Scenarios_No_Element;
6820 end Is_Recorded_SPARK_Scenario;
6821
6822 ------------------------------------
6823 -- Is_Recorded_Top_Level_Scenario --
6824 ------------------------------------
6825
6826 function Is_Recorded_Top_Level_Scenario (N : Node_Id) return Boolean is
6827 begin
6828 if Recorded_Top_Level_Scenarios_In_Use then
6829 return Recorded_Top_Level_Scenarios.Get (N);
6830 end if;
6831
6832 return Recorded_Top_Level_Scenarios_No_Element;
6833 end Is_Recorded_Top_Level_Scenario;
6834
6835 ------------------------
6836 -- Is_Safe_Activation --
6837 ------------------------
6838
6839 function Is_Safe_Activation
6840 (Call : Node_Id;
6841 Task_Decl : Node_Id) return Boolean
6842 is
6843 begin
6844 -- The activation of a task coming from an external instance cannot
6845 -- cause an ABE because the generic was already instantiated. Note
6846 -- that the instantiation itself may lead to an ABE.
6847
6848 return
6849 In_External_Instance
6850 (N => Call,
6851 Target_Decl => Task_Decl);
6852 end Is_Safe_Activation;
6853
6854 ------------------
6855 -- Is_Safe_Call --
6856 ------------------
6857
6858 function Is_Safe_Call
6859 (Call : Node_Id;
6860 Target_Attrs : Target_Attributes) return Boolean
6861 is
6862 begin
6863 -- The target is either an abstract subprogram, formal subprogram, or
6864 -- imported, in which case it does not have a body at compile or bind
6865 -- time. Assume that the call is ABE-safe.
6866
6867 if Is_Bodiless_Subprogram (Target_Attrs.Spec_Id) then
6868 return True;
6869
6870 -- The target is an instantiation of a generic subprogram. The call
6871 -- cannot cause an ABE because the generic was already instantiated.
6872 -- Note that the instantiation itself may lead to an ABE.
6873
6874 elsif Is_Generic_Instance (Target_Attrs.Spec_Id) then
6875 return True;
6876
6877 -- The invocation of a target coming from an external instance cannot
6878 -- cause an ABE because the generic was already instantiated. Note that
6879 -- the instantiation itself may lead to an ABE.
6880
6881 elsif In_External_Instance
6882 (N => Call,
6883 Target_Decl => Target_Attrs.Spec_Decl)
6884 then
6885 return True;
6886
6887 -- The target is a subprogram body without a previous declaration. The
6888 -- call cannot cause an ABE because the body has already been seen.
6889
6890 elsif Nkind (Target_Attrs.Spec_Decl) = N_Subprogram_Body
6891 and then No (Corresponding_Spec (Target_Attrs.Spec_Decl))
6892 then
6893 return True;
6894
6895 -- The target is a subprogram body stub without a prior declaration.
6896 -- The call cannot cause an ABE because the proper body substitutes
6897 -- the stub.
6898
6899 elsif Nkind (Target_Attrs.Spec_Decl) = N_Subprogram_Body_Stub
6900 and then No (Corresponding_Spec_Of_Stub (Target_Attrs.Spec_Decl))
6901 then
6902 return True;
6903
6904 -- Subprogram bodies which wrap attribute references used as actuals
6905 -- in instantiations are always ABE-safe. These bodies are artifacts
6906 -- of expansion.
6907
6908 elsif Present (Target_Attrs.Body_Decl)
6909 and then Nkind (Target_Attrs.Body_Decl) = N_Subprogram_Body
6910 and then Was_Attribute_Reference (Target_Attrs.Body_Decl)
6911 then
6912 return True;
6913 end if;
6914
6915 return False;
6916 end Is_Safe_Call;
6917
6918 ---------------------------
6919 -- Is_Safe_Instantiation --
6920 ---------------------------
6921
6922 function Is_Safe_Instantiation
6923 (Inst : Node_Id;
6924 Gen_Attrs : Target_Attributes) return Boolean
6925 is
6926 begin
6927 -- The generic is an intrinsic subprogram in which case it does not
6928 -- have a body at compile or bind time. Assume that the instantiation
6929 -- is ABE-safe.
6930
6931 if Is_Bodiless_Subprogram (Gen_Attrs.Spec_Id) then
6932 return True;
6933
6934 -- The instantiation of an external nested generic cannot cause an ABE
6935 -- if the outer generic was already instantiated. Note that the instance
6936 -- of the outer generic may lead to an ABE.
6937
6938 elsif In_External_Instance
6939 (N => Inst,
6940 Target_Decl => Gen_Attrs.Spec_Decl)
6941 then
6942 return True;
6943
6944 -- The generic is a package. The instantiation cannot cause an ABE when
6945 -- the package has no body.
6946
6947 elsif Ekind (Gen_Attrs.Spec_Id) = E_Generic_Package
6948 and then not Has_Body (Gen_Attrs.Spec_Decl)
6949 then
6950 return True;
6951 end if;
6952
6953 return False;
6954 end Is_Safe_Instantiation;
6955
6956 ------------------
6957 -- Is_Same_Unit --
6958 ------------------
6959
6960 function Is_Same_Unit
6961 (Unit_1 : Entity_Id;
6962 Unit_2 : Entity_Id) return Boolean
6963 is
6964 function Is_Subunit (Unit_Id : Entity_Id) return Boolean;
6965 pragma Inline (Is_Subunit);
6966 -- Determine whether unit Unit_Id is a subunit
6967
6968 function Normalize_Unit (Unit_Id : Entity_Id) return Entity_Id;
6969 -- Strip a potential subunit chain ending with unit Unit_Id and return
6970 -- the corresponding spec.
6971
6972 ----------------
6973 -- Is_Subunit --
6974 ----------------
6975
6976 function Is_Subunit (Unit_Id : Entity_Id) return Boolean is
6977 begin
6978 return Nkind (Parent (Unit_Declaration_Node (Unit_Id))) = N_Subunit;
6979 end Is_Subunit;
6980
6981 --------------------
6982 -- Normalize_Unit --
6983 --------------------
6984
6985 function Normalize_Unit (Unit_Id : Entity_Id) return Entity_Id is
6986 Result : Entity_Id;
6987
6988 begin
6989 -- Eliminate a potential chain of subunits to reach to proper body
6990
6991 Result := Unit_Id;
6992 while Present (Result)
6993 and then Result /= Standard_Standard
6994 and then Is_Subunit (Result)
6995 loop
6996 Result := Scope (Result);
6997 end loop;
6998
6999 -- Obtain the entity of the corresponding spec (if any)
7000
7001 return Unique_Entity (Result);
7002 end Normalize_Unit;
7003
7004 -- Start of processing for Is_Same_Unit
7005
7006 begin
7007 return Normalize_Unit (Unit_1) = Normalize_Unit (Unit_2);
7008 end Is_Same_Unit;
7009
7010 -----------------
7011 -- Is_Scenario --
7012 -----------------
7013
7014 function Is_Scenario (N : Node_Id) return Boolean is
7015 begin
7016 case Nkind (N) is
7017 when N_Assignment_Statement
7018 | N_Attribute_Reference
7019 | N_Call_Marker
7020 | N_Entry_Call_Statement
7021 | N_Expanded_Name
7022 | N_Function_Call
7023 | N_Function_Instantiation
7024 | N_Identifier
7025 | N_Package_Instantiation
7026 | N_Procedure_Call_Statement
7027 | N_Procedure_Instantiation
7028 | N_Requeue_Statement
7029 =>
7030 return True;
7031
7032 when others =>
7033 return False;
7034 end case;
7035 end Is_Scenario;
7036
7037 ------------------------------
7038 -- Is_SPARK_Semantic_Target --
7039 ------------------------------
7040
7041 function Is_SPARK_Semantic_Target (Id : Entity_Id) return Boolean is
7042 begin
7043 return
7044 Is_Default_Initial_Condition_Proc (Id)
7045 or else Is_Initial_Condition_Proc (Id);
7046 end Is_SPARK_Semantic_Target;
7047
7048 ------------------------
7049 -- Is_Suitable_Access --
7050 ------------------------
7051
7052 function Is_Suitable_Access (N : Node_Id) return Boolean is
7053 Nam : Name_Id;
7054 Pref : Node_Id;
7055 Subp_Id : Entity_Id;
7056
7057 begin
7058 -- This scenario is relevant only when the static model is in effect
7059 -- because it is graph-dependent and does not involve any run-time
7060 -- checks. Allowing it in the dynamic model would create confusing
7061 -- noise.
7062
7063 if not Static_Elaboration_Checks then
7064 return False;
7065
7066 -- Nothing to do when switch -gnatd.U (ignore 'Access) is in effect
7067
7068 elsif Debug_Flag_Dot_UU then
7069 return False;
7070
7071 -- Nothing to do when the scenario is not an attribute reference
7072
7073 elsif Nkind (N) /= N_Attribute_Reference then
7074 return False;
7075
7076 -- Nothing to do for internally-generated attributes because they are
7077 -- assumed to be ABE safe.
7078
7079 elsif not Comes_From_Source (N) then
7080 return False;
7081 end if;
7082
7083 Nam := Attribute_Name (N);
7084 Pref := Prefix (N);
7085
7086 -- Sanitize the prefix of the attribute
7087
7088 if not Is_Entity_Name (Pref) then
7089 return False;
7090
7091 elsif No (Entity (Pref)) then
7092 return False;
7093 end if;
7094
7095 Subp_Id := Entity (Pref);
7096
7097 if not Is_Subprogram_Or_Entry (Subp_Id) then
7098 return False;
7099 end if;
7100
7101 -- Traverse a possible chain of renamings to obtain the original entry
7102 -- or subprogram which the prefix may rename.
7103
7104 Subp_Id := Get_Renamed_Entity (Subp_Id);
7105
7106 -- To qualify, the attribute must meet the following prerequisites:
7107
7108 return
7109
7110 -- The prefix must denote a source entry, operator, or subprogram
7111 -- which is not imported.
7112
7113 Comes_From_Source (Subp_Id)
7114 and then Is_Subprogram_Or_Entry (Subp_Id)
7115 and then not Is_Bodiless_Subprogram (Subp_Id)
7116
7117 -- The attribute name must be one of the 'Access forms. Note that
7118 -- 'Unchecked_Access cannot apply to a subprogram.
7119
7120 and then Nam_In (Nam, Name_Access, Name_Unrestricted_Access);
7121 end Is_Suitable_Access;
7122
7123 ----------------------
7124 -- Is_Suitable_Call --
7125 ----------------------
7126
7127 function Is_Suitable_Call (N : Node_Id) return Boolean is
7128 begin
7129 -- Entry and subprogram calls are intentionally ignored because they
7130 -- may undergo expansion depending on the compilation mode, previous
7131 -- errors, generic context, etc. Call markers play the role of calls
7132 -- and provide a uniform foundation for ABE processing.
7133
7134 return Nkind (N) = N_Call_Marker;
7135 end Is_Suitable_Call;
7136
7137 -------------------------------
7138 -- Is_Suitable_Instantiation --
7139 -------------------------------
7140
7141 function Is_Suitable_Instantiation (N : Node_Id) return Boolean is
7142 Orig_N : constant Node_Id := Original_Node (N);
7143 -- Use the original node in case an instantiation library unit is
7144 -- rewritten as a package or subprogram.
7145
7146 begin
7147 -- To qualify, the instantiation must come from source
7148
7149 return
7150 Comes_From_Source (Orig_N)
7151 and then Nkind (Orig_N) in N_Generic_Instantiation;
7152 end Is_Suitable_Instantiation;
7153
7154 --------------------------
7155 -- Is_Suitable_Scenario --
7156 --------------------------
7157
7158 function Is_Suitable_Scenario (N : Node_Id) return Boolean is
7159 begin
7160 -- NOTE: Derived types and pragma Refined_State are intentionally left
7161 -- out because they are not executable during elaboration.
7162
7163 return
7164 Is_Suitable_Access (N)
7165 or else Is_Suitable_Call (N)
7166 or else Is_Suitable_Instantiation (N)
7167 or else Is_Suitable_Variable_Assignment (N)
7168 or else Is_Suitable_Variable_Reference (N);
7169 end Is_Suitable_Scenario;
7170
7171 ------------------------------------
7172 -- Is_Suitable_SPARK_Derived_Type --
7173 ------------------------------------
7174
7175 function Is_Suitable_SPARK_Derived_Type (N : Node_Id) return Boolean is
7176 Prag : Node_Id;
7177 Typ : Entity_Id;
7178
7179 begin
7180 -- To qualify, the type declaration must denote a derived tagged type
7181 -- with primitive operations, subject to pragma SPARK_Mode On.
7182
7183 if Nkind (N) = N_Full_Type_Declaration
7184 and then Nkind (Type_Definition (N)) = N_Derived_Type_Definition
7185 then
7186 Typ := Defining_Entity (N);
7187 Prag := SPARK_Pragma (Typ);
7188
7189 return
7190 Is_Tagged_Type (Typ)
7191 and then Has_Primitive_Operations (Typ)
7192 and then Present (Prag)
7193 and then Get_SPARK_Mode_From_Annotation (Prag) = On;
7194 end if;
7195
7196 return False;
7197 end Is_Suitable_SPARK_Derived_Type;
7198
7199 -------------------------------------
7200 -- Is_Suitable_SPARK_Instantiation --
7201 -------------------------------------
7202
7203 function Is_Suitable_SPARK_Instantiation (N : Node_Id) return Boolean is
7204 Gen_Attrs : Target_Attributes;
7205 Gen_Id : Entity_Id;
7206 Inst : Node_Id;
7207 Inst_Attrs : Instantiation_Attributes;
7208 Inst_Id : Entity_Id;
7209
7210 begin
7211 -- To qualify, both the instantiation and the generic must be subject to
7212 -- SPARK_Mode On.
7213
7214 if Is_Suitable_Instantiation (N) then
7215 Extract_Instantiation_Attributes
7216 (Exp_Inst => N,
7217 Inst => Inst,
7218 Inst_Id => Inst_Id,
7219 Gen_Id => Gen_Id,
7220 Attrs => Inst_Attrs);
7221
7222 Extract_Target_Attributes (Gen_Id, Gen_Attrs);
7223
7224 return Inst_Attrs.SPARK_Mode_On and Gen_Attrs.SPARK_Mode_On;
7225 end if;
7226
7227 return False;
7228 end Is_Suitable_SPARK_Instantiation;
7229
7230 --------------------------------------------
7231 -- Is_Suitable_SPARK_Refined_State_Pragma --
7232 --------------------------------------------
7233
7234 function Is_Suitable_SPARK_Refined_State_Pragma
7235 (N : Node_Id) return Boolean
7236 is
7237 begin
7238 -- To qualfy, the pragma must denote Refined_State
7239
7240 return
7241 Nkind (N) = N_Pragma
7242 and then Pragma_Name (N) = Name_Refined_State;
7243 end Is_Suitable_SPARK_Refined_State_Pragma;
7244
7245 -------------------------------------
7246 -- Is_Suitable_Variable_Assignment --
7247 -------------------------------------
7248
7249 function Is_Suitable_Variable_Assignment (N : Node_Id) return Boolean is
7250 N_Unit : Node_Id;
7251 N_Unit_Id : Entity_Id;
7252 Nam : Node_Id;
7253 Var_Decl : Node_Id;
7254 Var_Id : Entity_Id;
7255 Var_Unit : Node_Id;
7256 Var_Unit_Id : Entity_Id;
7257
7258 begin
7259 -- This scenario is relevant only when the static model is in effect
7260 -- because it is graph-dependent and does not involve any run-time
7261 -- checks. Allowing it in the dynamic model would create confusing
7262 -- noise.
7263
7264 if not Static_Elaboration_Checks then
7265 return False;
7266
7267 -- Nothing to do when the scenario is not an assignment
7268
7269 elsif Nkind (N) /= N_Assignment_Statement then
7270 return False;
7271
7272 -- Nothing to do for internally-generated assignments because they are
7273 -- assumed to be ABE safe.
7274
7275 elsif not Comes_From_Source (N) then
7276 return False;
7277
7278 -- Assignments are ignored in GNAT mode on the assumption that they are
7279 -- ABE-safe. This behaviour parallels that of the old ABE mechanism.
7280
7281 elsif GNAT_Mode then
7282 return False;
7283 end if;
7284
7285 Nam := Extract_Assignment_Name (N);
7286
7287 -- Sanitize the left hand side of the assignment
7288
7289 if not Is_Entity_Name (Nam) then
7290 return False;
7291
7292 elsif No (Entity (Nam)) then
7293 return False;
7294 end if;
7295
7296 Var_Id := Entity (Nam);
7297
7298 -- Sanitize the variable
7299
7300 if Var_Id = Any_Id then
7301 return False;
7302
7303 elsif Ekind (Var_Id) /= E_Variable then
7304 return False;
7305 end if;
7306
7307 Var_Decl := Declaration_Node (Var_Id);
7308
7309 if Nkind (Var_Decl) /= N_Object_Declaration then
7310 return False;
7311 end if;
7312
7313 N_Unit_Id := Find_Top_Unit (N);
7314 N_Unit := Unit_Declaration_Node (N_Unit_Id);
7315
7316 Var_Unit_Id := Find_Top_Unit (Var_Decl);
7317 Var_Unit := Unit_Declaration_Node (Var_Unit_Id);
7318
7319 -- To qualify, the assignment must meet the following prerequisites:
7320
7321 return
7322 Comes_From_Source (Var_Id)
7323
7324 -- The variable must be declared in the spec of compilation unit U
7325
7326 and then Nkind (Var_Unit) = N_Package_Declaration
7327
7328 -- Performance note: parent traversal
7329
7330 and then Find_Enclosing_Level (Var_Decl) = Package_Spec
7331
7332 -- The assignment must occur in the body of compilation unit U
7333
7334 and then Nkind (N_Unit) = N_Package_Body
7335 and then Present (Corresponding_Body (Var_Unit))
7336 and then Corresponding_Body (Var_Unit) = N_Unit_Id;
7337 end Is_Suitable_Variable_Assignment;
7338
7339 ------------------------------------
7340 -- Is_Suitable_Variable_Reference --
7341 ------------------------------------
7342
7343 function Is_Suitable_Variable_Reference (N : Node_Id) return Boolean is
7344 begin
7345 -- Expanded names and identifiers are intentionally ignored because they
7346 -- be folded, optimized away, etc. Variable references markers play the
7347 -- role of variable references and provide a uniform foundation for ABE
7348 -- processing.
7349
7350 return Nkind (N) = N_Variable_Reference_Marker;
7351 end Is_Suitable_Variable_Reference;
7352
7353 -------------------
7354 -- Is_Task_Entry --
7355 -------------------
7356
7357 function Is_Task_Entry (Id : Entity_Id) return Boolean is
7358 begin
7359 -- To qualify, the entity must denote an entry defined in a task type
7360
7361 return
7362 Is_Entry (Id) and then Is_Task_Type (Non_Private_View (Scope (Id)));
7363 end Is_Task_Entry;
7364
7365 ------------------------
7366 -- Is_Up_Level_Target --
7367 ------------------------
7368
7369 function Is_Up_Level_Target (Target_Decl : Node_Id) return Boolean is
7370 Root : constant Node_Id := Root_Scenario;
7371
7372 begin
7373 -- The root appears within the declaratons of a block statement, entry
7374 -- body, subprogram body, or task body ignoring enclosing packages. The
7375 -- root is always within the main unit. An up-level target is a notion
7376 -- applicable only to the static model because scenarios are reached by
7377 -- means of graph traversal started from a fixed declarative or library
7378 -- level.
7379
7380 -- Performance note: parent traversal
7381
7382 if Static_Elaboration_Checks
7383 and then Find_Enclosing_Level (Root) = Declaration_Level
7384 then
7385 -- The target is within the main unit. It acts as an up-level target
7386 -- when it appears within a context which encloses the root.
7387
7388 -- package body Main_Unit is
7389 -- function Func ...; -- target
7390
7391 -- procedure Proc is
7392 -- X : ... := Func; -- root scenario
7393
7394 if In_Extended_Main_Code_Unit (Target_Decl) then
7395
7396 -- Performance note: parent traversal
7397
7398 return not In_Same_Context (Root, Target_Decl, Nested_OK => True);
7399
7400 -- Otherwise the target is external to the main unit which makes it
7401 -- an up-level target.
7402
7403 else
7404 return True;
7405 end if;
7406 end if;
7407
7408 return False;
7409 end Is_Up_Level_Target;
7410
7411 ---------------------
7412 -- Is_Visited_Body --
7413 ---------------------
7414
7415 function Is_Visited_Body (Body_Decl : Node_Id) return Boolean is
7416 begin
7417 if Visited_Bodies_In_Use then
7418 return Visited_Bodies.Get (Body_Decl);
7419 end if;
7420
7421 return Visited_Bodies_No_Element;
7422 end Is_Visited_Body;
7423
7424 -------------------------------
7425 -- Kill_Elaboration_Scenario --
7426 -------------------------------
7427
7428 procedure Kill_Elaboration_Scenario (N : Node_Id) is
7429 procedure Kill_SPARK_Scenario;
7430 pragma Inline (Kill_SPARK_Scenario);
7431 -- Eliminate scenario N from table SPARK_Scenarios if it is recorded
7432 -- there.
7433
7434 procedure Kill_Top_Level_Scenario;
7435 pragma Inline (Kill_Top_Level_Scenario);
7436 -- Eliminate scenario N from table Top_Level_Scenarios if it is recorded
7437 -- there.
7438
7439 -------------------------
7440 -- Kill_SPARK_Scenario --
7441 -------------------------
7442
7443 procedure Kill_SPARK_Scenario is
7444 package Scenarios renames SPARK_Scenarios;
7445
7446 begin
7447 if Is_Recorded_SPARK_Scenario (N) then
7448
7449 -- Performance note: list traversal
7450
7451 for Index in Scenarios.First .. Scenarios.Last loop
7452 if Scenarios.Table (Index) = N then
7453 Scenarios.Table (Index) := Empty;
7454
7455 -- The SPARK scenario is no longer recorded
7456
7457 Set_Is_Recorded_SPARK_Scenario (N, False);
7458 return;
7459 end if;
7460 end loop;
7461
7462 -- A recorded SPARK scenario must be in the table of recorded
7463 -- SPARK scenarios.
7464
7465 pragma Assert (False);
7466 end if;
7467 end Kill_SPARK_Scenario;
7468
7469 -----------------------------
7470 -- Kill_Top_Level_Scenario --
7471 -----------------------------
7472
7473 procedure Kill_Top_Level_Scenario is
7474 package Scenarios renames Top_Level_Scenarios;
7475
7476 begin
7477 if Is_Recorded_Top_Level_Scenario (N) then
7478
7479 -- Performance node: list traversal
7480
7481 for Index in Scenarios.First .. Scenarios.Last loop
7482 if Scenarios.Table (Index) = N then
7483 Scenarios.Table (Index) := Empty;
7484
7485 -- The top-level scenario is no longer recorded
7486
7487 Set_Is_Recorded_Top_Level_Scenario (N, False);
7488 return;
7489 end if;
7490 end loop;
7491
7492 -- A recorded top-level scenario must be in the table of recorded
7493 -- top-level scenarios.
7494
7495 pragma Assert (False);
7496 end if;
7497 end Kill_Top_Level_Scenario;
7498
7499 -- Start of processing for Kill_Elaboration_Scenario
7500
7501 begin
7502 -- Eliminate a recorded scenario when it appears within dead code
7503 -- because it will not be executed at elaboration time.
7504
7505 if Is_Scenario (N) then
7506 Kill_SPARK_Scenario;
7507 Kill_Top_Level_Scenario;
7508 end if;
7509 end Kill_Elaboration_Scenario;
7510
7511 ----------------------------------
7512 -- Meet_Elaboration_Requirement --
7513 ----------------------------------
7514
7515 procedure Meet_Elaboration_Requirement
7516 (N : Node_Id;
7517 Target_Id : Entity_Id;
7518 Req_Nam : Name_Id)
7519 is
7520 Main_Id : constant Entity_Id := Cunit_Entity (Main_Unit);
7521 Unit_Id : constant Entity_Id := Find_Top_Unit (Target_Id);
7522
7523 function Find_Preelaboration_Pragma
7524 (Prag_Nam : Name_Id) return Node_Id;
7525 pragma Inline (Find_Preelaboration_Pragma);
7526 -- Traverse the visible declarations of unit Unit_Id and locate a source
7527 -- preelaboration-related pragma with name Prag_Nam.
7528
7529 procedure Info_Requirement_Met (Prag : Node_Id);
7530 pragma Inline (Info_Requirement_Met);
7531 -- Output information concerning pragma Prag which meets requirement
7532 -- Req_Nam.
7533
7534 procedure Info_Scenario;
7535 pragma Inline (Info_Scenario);
7536 -- Output information concerning scenario N
7537
7538 --------------------------------
7539 -- Find_Preelaboration_Pragma --
7540 --------------------------------
7541
7542 function Find_Preelaboration_Pragma
7543 (Prag_Nam : Name_Id) return Node_Id
7544 is
7545 Spec : constant Node_Id := Parent (Unit_Id);
7546 Decl : Node_Id;
7547
7548 begin
7549 -- A preelaboration-related pragma comes from source and appears at
7550 -- the top of the visible declarations of a package.
7551
7552 if Nkind (Spec) = N_Package_Specification then
7553 Decl := First (Visible_Declarations (Spec));
7554 while Present (Decl) loop
7555 if Comes_From_Source (Decl) then
7556 if Nkind (Decl) = N_Pragma
7557 and then Pragma_Name (Decl) = Prag_Nam
7558 then
7559 return Decl;
7560
7561 -- Otherwise the construct terminates the region where the
7562 -- preelabortion-related pragma may appear.
7563
7564 else
7565 exit;
7566 end if;
7567 end if;
7568
7569 Next (Decl);
7570 end loop;
7571 end if;
7572
7573 return Empty;
7574 end Find_Preelaboration_Pragma;
7575
7576 --------------------------
7577 -- Info_Requirement_Met --
7578 --------------------------
7579
7580 procedure Info_Requirement_Met (Prag : Node_Id) is
7581 begin
7582 pragma Assert (Present (Prag));
7583
7584 Error_Msg_Name_1 := Req_Nam;
7585 Error_Msg_Sloc := Sloc (Prag);
7586 Error_Msg_NE
7587 ("\\% requirement for unit & met by pragma #", N, Unit_Id);
7588 end Info_Requirement_Met;
7589
7590 -------------------
7591 -- Info_Scenario --
7592 -------------------
7593
7594 procedure Info_Scenario is
7595 begin
7596 if Is_Suitable_Call (N) then
7597 Info_Call
7598 (Call => N,
7599 Target_Id => Target_Id,
7600 Info_Msg => False,
7601 In_SPARK => True);
7602
7603 elsif Is_Suitable_Instantiation (N) then
7604 Info_Instantiation
7605 (Inst => N,
7606 Gen_Id => Target_Id,
7607 Info_Msg => False,
7608 In_SPARK => True);
7609
7610 elsif Is_Suitable_SPARK_Refined_State_Pragma (N) then
7611 Error_Msg_N
7612 ("read of refinement constituents during elaboration in SPARK",
7613 N);
7614
7615 elsif Is_Suitable_Variable_Reference (N) then
7616 Info_Variable_Reference
7617 (Ref => N,
7618 Var_Id => Target_Id,
7619 Info_Msg => False,
7620 In_SPARK => True);
7621
7622 -- No other scenario may impose a requirement on the context of the
7623 -- main unit.
7624
7625 else
7626 pragma Assert (False);
7627 null;
7628 end if;
7629 end Info_Scenario;
7630
7631 -- Local variables
7632
7633 Elab_Attrs : Elaboration_Attributes;
7634 Elab_Nam : Name_Id;
7635 Req_Met : Boolean;
7636
7637 -- Start of processing for Meet_Elaboration_Requirement
7638
7639 begin
7640 pragma Assert (Nam_In (Req_Nam, Name_Elaborate, Name_Elaborate_All));
7641
7642 -- Assume that the requirement has not been met
7643
7644 Req_Met := False;
7645
7646 -- Elaboration requirements are verified only when the static model is
7647 -- in effect because this diagnostic is graph-dependent.
7648
7649 if not Static_Elaboration_Checks then
7650 return;
7651
7652 -- If the target is within the main unit, either at the source level or
7653 -- through an instantiation, then there is no real requirement to meet
7654 -- because the main unit cannot force its own elaboration by means of an
7655 -- Elaborate[_All] pragma. Treat this case as valid coverage.
7656
7657 elsif In_Extended_Main_Code_Unit (Target_Id) then
7658 Req_Met := True;
7659
7660 -- Otherwise the target resides in an external unit
7661
7662 -- The requirement is met when the target comes from an internal unit
7663 -- because such a unit is elaborated prior to a non-internal unit.
7664
7665 elsif In_Internal_Unit (Unit_Id)
7666 and then not In_Internal_Unit (Main_Id)
7667 then
7668 Req_Met := True;
7669
7670 -- The requirement is met when the target comes from a preelaborated
7671 -- unit. This portion must parallel predicate Is_Preelaborated_Unit.
7672
7673 elsif Is_Preelaborated_Unit (Unit_Id) then
7674 Req_Met := True;
7675
7676 -- Output extra information when switch -gnatel (info messages on
7677 -- implicit Elaborate[_All] pragmas.
7678
7679 if Elab_Info_Messages then
7680 if Is_Preelaborated (Unit_Id) then
7681 Elab_Nam := Name_Preelaborate;
7682
7683 elsif Is_Pure (Unit_Id) then
7684 Elab_Nam := Name_Pure;
7685
7686 elsif Is_Remote_Call_Interface (Unit_Id) then
7687 Elab_Nam := Name_Remote_Call_Interface;
7688
7689 elsif Is_Remote_Types (Unit_Id) then
7690 Elab_Nam := Name_Remote_Types;
7691
7692 else
7693 pragma Assert (Is_Shared_Passive (Unit_Id));
7694 Elab_Nam := Name_Shared_Passive;
7695 end if;
7696
7697 Info_Requirement_Met (Find_Preelaboration_Pragma (Elab_Nam));
7698 end if;
7699
7700 -- Determine whether the context of the main unit has a pragma strong
7701 -- enough to meet the requirement.
7702
7703 else
7704 Elab_Attrs := Elaboration_Status (Unit_Id);
7705
7706 -- The pragma must be either Elaborate_All or be as strong as the
7707 -- requirement.
7708
7709 if Present (Elab_Attrs.Source_Pragma)
7710 and then Nam_In (Pragma_Name (Elab_Attrs.Source_Pragma),
7711 Name_Elaborate_All,
7712 Req_Nam)
7713 then
7714 Req_Met := True;
7715
7716 -- Output extra information when switch -gnatel (info messages on
7717 -- implicit Elaborate[_All] pragmas.
7718
7719 if Elab_Info_Messages then
7720 Info_Requirement_Met (Elab_Attrs.Source_Pragma);
7721 end if;
7722 end if;
7723 end if;
7724
7725 -- The requirement was not met by the context of the main unit, issue an
7726 -- error.
7727
7728 if not Req_Met then
7729 Info_Scenario;
7730
7731 Error_Msg_Name_1 := Req_Nam;
7732 Error_Msg_Node_2 := Unit_Id;
7733 Error_Msg_NE ("\\unit & requires pragma % for &", N, Main_Id);
7734
7735 Output_Active_Scenarios (N);
7736 end if;
7737 end Meet_Elaboration_Requirement;
7738
7739 ----------------------
7740 -- Non_Private_View --
7741 ----------------------
7742
7743 function Non_Private_View (Typ : Entity_Id) return Entity_Id is
7744 Result : Entity_Id;
7745
7746 begin
7747 Result := Typ;
7748
7749 if Is_Private_Type (Result) and then Present (Full_View (Result)) then
7750 Result := Full_View (Result);
7751 end if;
7752
7753 return Result;
7754 end Non_Private_View;
7755
7756 -----------------------------
7757 -- Output_Active_Scenarios --
7758 -----------------------------
7759
7760 procedure Output_Active_Scenarios (Error_Nod : Node_Id) is
7761 procedure Output_Access (N : Node_Id);
7762 -- Emit a specific diagnostic message for 'Access denote by N
7763
7764 procedure Output_Activation_Call (N : Node_Id);
7765 -- Emit a specific diagnostic message for task activation N
7766
7767 procedure Output_Call (N : Node_Id; Target_Id : Entity_Id);
7768 -- Emit a specific diagnostic message for call N which invokes target
7769 -- Target_Id.
7770
7771 procedure Output_Header;
7772 -- Emit a specific diagnostic message for the unit of the root scenario
7773
7774 procedure Output_Instantiation (N : Node_Id);
7775 -- Emit a specific diagnostic message for instantiation N
7776
7777 procedure Output_SPARK_Refined_State_Pragma (N : Node_Id);
7778 -- Emit a specific diagnostic message for Refined_State pragma N
7779
7780 procedure Output_Variable_Assignment (N : Node_Id);
7781 -- Emit a specific diagnostic message for assignment statement N
7782
7783 procedure Output_Variable_Reference (N : Node_Id);
7784 -- Emit a specific diagnostic message for reference N which mentions a
7785 -- variable.
7786
7787 -------------------
7788 -- Output_Access --
7789 -------------------
7790
7791 procedure Output_Access (N : Node_Id) is
7792 Subp_Id : constant Entity_Id := Entity (Prefix (N));
7793
7794 begin
7795 Error_Msg_Name_1 := Attribute_Name (N);
7796 Error_Msg_Sloc := Sloc (N);
7797 Error_Msg_NE ("\\ % of & taken #", Error_Nod, Subp_Id);
7798 end Output_Access;
7799
7800 ----------------------------
7801 -- Output_Activation_Call --
7802 ----------------------------
7803
7804 procedure Output_Activation_Call (N : Node_Id) is
7805 function Find_Activator (Call : Node_Id) return Entity_Id;
7806 -- Find the nearest enclosing construct which houses call Call
7807
7808 --------------------
7809 -- Find_Activator --
7810 --------------------
7811
7812 function Find_Activator (Call : Node_Id) return Entity_Id is
7813 Par : Node_Id;
7814
7815 begin
7816 -- Climb the parent chain looking for a package [body] or a
7817 -- construct with a statement sequence.
7818
7819 Par := Parent (Call);
7820 while Present (Par) loop
7821 if Nkind_In (Par, N_Package_Body, N_Package_Declaration) then
7822 return Defining_Entity (Par);
7823
7824 elsif Nkind (Par) = N_Handled_Sequence_Of_Statements then
7825 return Defining_Entity (Parent (Par));
7826 end if;
7827
7828 Par := Parent (Par);
7829 end loop;
7830
7831 return Empty;
7832 end Find_Activator;
7833
7834 -- Local variables
7835
7836 Activator : constant Entity_Id := Find_Activator (N);
7837
7838 -- Start of processing for Output_Activation_Call
7839
7840 begin
7841 pragma Assert (Present (Activator));
7842
7843 Error_Msg_NE ("\\ local tasks of & activated", Error_Nod, Activator);
7844 end Output_Activation_Call;
7845
7846 -----------------
7847 -- Output_Call --
7848 -----------------
7849
7850 procedure Output_Call (N : Node_Id; Target_Id : Entity_Id) is
7851 procedure Output_Accept_Alternative;
7852 pragma Inline (Output_Accept_Alternative);
7853 -- Emit a specific diagnostic message concerning an accept
7854 -- alternative.
7855
7856 procedure Output_Call (Kind : String);
7857 pragma Inline (Output_Call);
7858 -- Emit a specific diagnostic message concerning a call of kind Kind
7859
7860 procedure Output_Type_Actions (Action : String);
7861 pragma Inline (Output_Type_Actions);
7862 -- Emit a specific diagnostic message concerning action Action of a
7863 -- type.
7864
7865 procedure Output_Verification_Call
7866 (Pred : String;
7867 Id : Entity_Id;
7868 Id_Kind : String);
7869 pragma Inline (Output_Verification_Call);
7870 -- Emit a specific diagnostic message concerning the verification of
7871 -- predicate Pred applied to related entity Id with kind Id_Kind.
7872
7873 -------------------------------
7874 -- Output_Accept_Alternative --
7875 -------------------------------
7876
7877 procedure Output_Accept_Alternative is
7878 Entry_Id : constant Entity_Id := Receiving_Entry (Target_Id);
7879
7880 begin
7881 pragma Assert (Present (Entry_Id));
7882
7883 Error_Msg_NE ("\\ entry & selected #", Error_Nod, Entry_Id);
7884 end Output_Accept_Alternative;
7885
7886 -----------------
7887 -- Output_Call --
7888 -----------------
7889
7890 procedure Output_Call (Kind : String) is
7891 begin
7892 Error_Msg_NE ("\\ " & Kind & " & called #", Error_Nod, Target_Id);
7893 end Output_Call;
7894
7895 -------------------------
7896 -- Output_Type_Actions --
7897 -------------------------
7898
7899 procedure Output_Type_Actions (Action : String) is
7900 Typ : constant Entity_Id := First_Formal_Type (Target_Id);
7901
7902 begin
7903 pragma Assert (Present (Typ));
7904
7905 Error_Msg_NE
7906 ("\\ " & Action & " actions for type & #", Error_Nod, Typ);
7907 end Output_Type_Actions;
7908
7909 ------------------------------
7910 -- Output_Verification_Call --
7911 ------------------------------
7912
7913 procedure Output_Verification_Call
7914 (Pred : String;
7915 Id : Entity_Id;
7916 Id_Kind : String)
7917 is
7918 begin
7919 pragma Assert (Present (Id));
7920
7921 Error_Msg_NE
7922 ("\\ " & Pred & " of " & Id_Kind & " & verified #",
7923 Error_Nod, Id);
7924 end Output_Verification_Call;
7925
7926 -- Start of processing for Output_Call
7927
7928 begin
7929 Error_Msg_Sloc := Sloc (N);
7930
7931 -- Accept alternative
7932
7933 if Is_Accept_Alternative_Proc (Target_Id) then
7934 Output_Accept_Alternative;
7935
7936 -- Adjustment
7937
7938 elsif Is_TSS (Target_Id, TSS_Deep_Adjust) then
7939 Output_Type_Actions ("adjustment");
7940
7941 -- Default_Initial_Condition
7942
7943 elsif Is_Default_Initial_Condition_Proc (Target_Id) then
7944 Output_Verification_Call
7945 (Pred => "Default_Initial_Condition",
7946 Id => First_Formal_Type (Target_Id),
7947 Id_Kind => "type");
7948
7949 -- Entries
7950
7951 elsif Is_Protected_Entry (Target_Id) then
7952 Output_Call ("entry");
7953
7954 -- Task entry calls are never processed because the entry being
7955 -- invoked does not have a corresponding "body", it has a select. A
7956 -- task entry call appears in the stack of active scenarios for the
7957 -- sole purpose of checking No_Entry_Calls_In_Elaboration_Code and
7958 -- nothing more.
7959
7960 elsif Is_Task_Entry (Target_Id) then
7961 null;
7962
7963 -- Finalization
7964
7965 elsif Is_TSS (Target_Id, TSS_Deep_Finalize) then
7966 Output_Type_Actions ("finalization");
7967
7968 -- Calls to _Finalizer procedures must not appear in the output
7969 -- because this creates confusing noise.
7970
7971 elsif Is_Finalizer_Proc (Target_Id) then
7972 null;
7973
7974 -- Initial_Condition
7975
7976 elsif Is_Initial_Condition_Proc (Target_Id) then
7977 Output_Verification_Call
7978 (Pred => "Initial_Condition",
7979 Id => Find_Enclosing_Scope (N),
7980 Id_Kind => "package");
7981
7982 -- Initialization
7983
7984 elsif Is_Init_Proc (Target_Id)
7985 or else Is_TSS (Target_Id, TSS_Deep_Initialize)
7986 then
7987 Output_Type_Actions ("initialization");
7988
7989 -- Invariant
7990
7991 elsif Is_Invariant_Proc (Target_Id) then
7992 Output_Verification_Call
7993 (Pred => "invariants",
7994 Id => First_Formal_Type (Target_Id),
7995 Id_Kind => "type");
7996
7997 -- Partial invariant calls must not appear in the output because this
7998 -- creates confusing noise. Note that a partial invariant is always
7999 -- invoked by the "full" invariant which is already placed on the
8000 -- stack.
8001
8002 elsif Is_Partial_Invariant_Proc (Target_Id) then
8003 null;
8004
8005 -- _Postconditions
8006
8007 elsif Is_Postconditions_Proc (Target_Id) then
8008 Output_Verification_Call
8009 (Pred => "postconditions",
8010 Id => Find_Enclosing_Scope (N),
8011 Id_Kind => "subprogram");
8012
8013 -- Subprograms must come last because some of the previous cases fall
8014 -- under this category.
8015
8016 elsif Ekind (Target_Id) = E_Function then
8017 Output_Call ("function");
8018
8019 elsif Ekind (Target_Id) = E_Procedure then
8020 Output_Call ("procedure");
8021
8022 else
8023 pragma Assert (False);
8024 null;
8025 end if;
8026 end Output_Call;
8027
8028 -------------------
8029 -- Output_Header --
8030 -------------------
8031
8032 procedure Output_Header is
8033 Unit_Id : constant Entity_Id := Find_Top_Unit (Root_Scenario);
8034
8035 begin
8036 if Ekind (Unit_Id) = E_Package then
8037 Error_Msg_NE ("\\ spec of unit & elaborated", Error_Nod, Unit_Id);
8038
8039 elsif Ekind (Unit_Id) = E_Package_Body then
8040 Error_Msg_NE ("\\ body of unit & elaborated", Error_Nod, Unit_Id);
8041
8042 else
8043 Error_Msg_NE ("\\ in body of unit &", Error_Nod, Unit_Id);
8044 end if;
8045 end Output_Header;
8046
8047 --------------------------
8048 -- Output_Instantiation --
8049 --------------------------
8050
8051 procedure Output_Instantiation (N : Node_Id) is
8052 procedure Output_Instantiation (Gen_Id : Entity_Id; Kind : String);
8053 pragma Inline (Output_Instantiation);
8054 -- Emit a specific diagnostic message concerning an instantiation of
8055 -- generic unit Gen_Id. Kind denotes the kind of the instantiation.
8056
8057 --------------------------
8058 -- Output_Instantiation --
8059 --------------------------
8060
8061 procedure Output_Instantiation (Gen_Id : Entity_Id; Kind : String) is
8062 begin
8063 Error_Msg_NE
8064 ("\\ " & Kind & " & instantiated as & #", Error_Nod, Gen_Id);
8065 end Output_Instantiation;
8066
8067 -- Local variables
8068
8069 Inst : Node_Id;
8070 Inst_Attrs : Instantiation_Attributes;
8071 Inst_Id : Entity_Id;
8072 Gen_Id : Entity_Id;
8073
8074 -- Start of processing for Output_Instantiation
8075
8076 begin
8077 Extract_Instantiation_Attributes
8078 (Exp_Inst => N,
8079 Inst => Inst,
8080 Inst_Id => Inst_Id,
8081 Gen_Id => Gen_Id,
8082 Attrs => Inst_Attrs);
8083
8084 Error_Msg_Node_2 := Inst_Id;
8085 Error_Msg_Sloc := Sloc (Inst);
8086
8087 if Nkind (Inst) = N_Function_Instantiation then
8088 Output_Instantiation (Gen_Id, "function");
8089
8090 elsif Nkind (Inst) = N_Package_Instantiation then
8091 Output_Instantiation (Gen_Id, "package");
8092
8093 elsif Nkind (Inst) = N_Procedure_Instantiation then
8094 Output_Instantiation (Gen_Id, "procedure");
8095
8096 else
8097 pragma Assert (False);
8098 null;
8099 end if;
8100 end Output_Instantiation;
8101
8102 ---------------------------------------
8103 -- Output_SPARK_Refined_State_Pragma --
8104 ---------------------------------------
8105
8106 procedure Output_SPARK_Refined_State_Pragma (N : Node_Id) is
8107 begin
8108 Error_Msg_Sloc := Sloc (N);
8109 Error_Msg_N ("\\ refinement constituents read #", Error_Nod);
8110 end Output_SPARK_Refined_State_Pragma;
8111
8112 --------------------------------
8113 -- Output_Variable_Assignment --
8114 --------------------------------
8115
8116 procedure Output_Variable_Assignment (N : Node_Id) is
8117 Var_Id : constant Entity_Id := Entity (Extract_Assignment_Name (N));
8118
8119 begin
8120 Error_Msg_Sloc := Sloc (N);
8121 Error_Msg_NE ("\\ variable & assigned #", Error_Nod, Var_Id);
8122 end Output_Variable_Assignment;
8123
8124 -------------------------------
8125 -- Output_Variable_Reference --
8126 -------------------------------
8127
8128 procedure Output_Variable_Reference (N : Node_Id) is
8129 Dummy : Variable_Attributes;
8130 Var_Id : Entity_Id;
8131
8132 begin
8133 Extract_Variable_Reference_Attributes
8134 (Ref => N,
8135 Var_Id => Var_Id,
8136 Attrs => Dummy);
8137
8138 Error_Msg_Sloc := Sloc (N);
8139
8140 if Is_Read (N) then
8141 Error_Msg_NE ("\\ variable & read #", Error_Nod, Var_Id);
8142
8143 else
8144 pragma Assert (False);
8145 null;
8146 end if;
8147 end Output_Variable_Reference;
8148
8149 -- Local variables
8150
8151 package Stack renames Scenario_Stack;
8152
8153 Dummy : Call_Attributes;
8154 N : Node_Id;
8155 Posted : Boolean;
8156 Target_Id : Entity_Id;
8157
8158 -- Start of processing for Output_Active_Scenarios
8159
8160 begin
8161 -- Active scenarios are emitted only when the static model is in effect
8162 -- because there is an inherent order by which all these scenarios were
8163 -- reached from the declaration or library level.
8164
8165 if not Static_Elaboration_Checks then
8166 return;
8167 end if;
8168
8169 Posted := False;
8170
8171 for Index in Stack.First .. Stack.Last loop
8172 N := Stack.Table (Index);
8173
8174 if not Posted then
8175 Posted := True;
8176 Output_Header;
8177 end if;
8178
8179 -- 'Access
8180
8181 if Nkind (N) = N_Attribute_Reference then
8182 Output_Access (N);
8183
8184 -- Calls
8185
8186 elsif Is_Suitable_Call (N) then
8187 Extract_Call_Attributes
8188 (Call => N,
8189 Target_Id => Target_Id,
8190 Attrs => Dummy);
8191
8192 if Is_Activation_Proc (Target_Id) then
8193 Output_Activation_Call (N);
8194 else
8195 Output_Call (N, Target_Id);
8196 end if;
8197
8198 -- Instantiations
8199
8200 elsif Is_Suitable_Instantiation (N) then
8201 Output_Instantiation (N);
8202
8203 -- Pragma Refined_State
8204
8205 elsif Is_Suitable_SPARK_Refined_State_Pragma (N) then
8206 Output_SPARK_Refined_State_Pragma (N);
8207
8208 -- Variable assignments
8209
8210 elsif Nkind (N) = N_Assignment_Statement then
8211 Output_Variable_Assignment (N);
8212
8213 -- Variable references
8214
8215 elsif Is_Suitable_Variable_Reference (N) then
8216 Output_Variable_Reference (N);
8217
8218 else
8219 pragma Assert (False);
8220 null;
8221 end if;
8222 end loop;
8223 end Output_Active_Scenarios;
8224
8225 -------------------------
8226 -- Pop_Active_Scenario --
8227 -------------------------
8228
8229 procedure Pop_Active_Scenario (N : Node_Id) is
8230 Top : Node_Id renames Scenario_Stack.Table (Scenario_Stack.Last);
8231
8232 begin
8233 pragma Assert (Top = N);
8234 Scenario_Stack.Decrement_Last;
8235 end Pop_Active_Scenario;
8236
8237 --------------------------------
8238 -- Process_Activation_Generic --
8239 --------------------------------
8240
8241 procedure Process_Activation_Generic
8242 (Call : Node_Id;
8243 Call_Attrs : Call_Attributes;
8244 In_Init_Cond : Boolean;
8245 In_Partial_Fin : Boolean;
8246 In_Task_Body : Boolean)
8247 is
8248 procedure Process_Task_Object (Obj_Id : Entity_Id; Typ : Entity_Id);
8249 -- Perform ABE checks and diagnostics for object Obj_Id with type Typ.
8250 -- Typ may be a task type or a composite type with at least one task
8251 -- component.
8252
8253 procedure Process_Task_Objects (List : List_Id);
8254 -- Perform ABE checks and diagnostics for all task objects found in
8255 -- the list List.
8256
8257 -------------------------
8258 -- Process_Task_Object --
8259 -------------------------
8260
8261 procedure Process_Task_Object (Obj_Id : Entity_Id; Typ : Entity_Id) is
8262 Base_Typ : constant Entity_Id := Base_Type (Typ);
8263
8264 Comp_Id : Entity_Id;
8265 Task_Attrs : Task_Attributes;
8266
8267 begin
8268 if Is_Task_Type (Typ) then
8269 Extract_Task_Attributes
8270 (Typ => Base_Typ,
8271 Attrs => Task_Attrs);
8272
8273 Process_Single_Activation
8274 (Call => Call,
8275 Call_Attrs => Call_Attrs,
8276 Obj_Id => Obj_Id,
8277 Task_Attrs => Task_Attrs,
8278 In_Init_Cond => In_Init_Cond,
8279 In_Partial_Fin => In_Partial_Fin,
8280 In_Task_Body => In_Task_Body);
8281
8282 -- Examine the component type when the object is an array
8283
8284 elsif Is_Array_Type (Typ) and then Has_Task (Base_Typ) then
8285 Process_Task_Object (Obj_Id, Component_Type (Typ));
8286
8287 -- Examine individual component types when the object is a record
8288
8289 elsif Is_Record_Type (Typ) and then Has_Task (Base_Typ) then
8290 Comp_Id := First_Component (Typ);
8291 while Present (Comp_Id) loop
8292 Process_Task_Object (Obj_Id, Etype (Comp_Id));
8293 Next_Component (Comp_Id);
8294 end loop;
8295 end if;
8296 end Process_Task_Object;
8297
8298 --------------------------
8299 -- Process_Task_Objects --
8300 --------------------------
8301
8302 procedure Process_Task_Objects (List : List_Id) is
8303 Item : Node_Id;
8304 Item_Id : Entity_Id;
8305 Item_Typ : Entity_Id;
8306
8307 begin
8308 -- Examine the contents of the list looking for an object declaration
8309 -- of a task type or one that contains a task within.
8310
8311 Item := First (List);
8312 while Present (Item) loop
8313 if Nkind (Item) = N_Object_Declaration then
8314 Item_Id := Defining_Entity (Item);
8315 Item_Typ := Etype (Item_Id);
8316
8317 if Has_Task (Item_Typ) then
8318 Process_Task_Object (Item_Id, Item_Typ);
8319 end if;
8320 end if;
8321
8322 Next (Item);
8323 end loop;
8324 end Process_Task_Objects;
8325
8326 -- Local variables
8327
8328 Context : Node_Id;
8329 Spec : Node_Id;
8330
8331 -- Start of processing for Process_Activation_Generic
8332
8333 begin
8334 -- Nothing to do when the activation is a guaranteed ABE
8335
8336 if Is_Known_Guaranteed_ABE (Call) then
8337 return;
8338 end if;
8339
8340 -- Find the proper context of the activation call where all task objects
8341 -- being activated are declared. This is usually the immediate parent of
8342 -- the call.
8343
8344 Context := Parent (Call);
8345
8346 -- In the case of package bodies, the activation call is in the handled
8347 -- sequence of statements, but the task objects are in the declaration
8348 -- list of the body.
8349
8350 if Nkind (Context) = N_Handled_Sequence_Of_Statements
8351 and then Nkind (Parent (Context)) = N_Package_Body
8352 then
8353 Context := Parent (Context);
8354 end if;
8355
8356 -- Process all task objects defined in both the spec and body when the
8357 -- activation call precedes the "begin" of a package body.
8358
8359 if Nkind (Context) = N_Package_Body then
8360 Spec :=
8361 Specification
8362 (Unit_Declaration_Node (Corresponding_Spec (Context)));
8363
8364 Process_Task_Objects (Visible_Declarations (Spec));
8365 Process_Task_Objects (Private_Declarations (Spec));
8366 Process_Task_Objects (Declarations (Context));
8367
8368 -- Process all task objects defined in the spec when the activation call
8369 -- appears at the end of a package spec.
8370
8371 elsif Nkind (Context) = N_Package_Specification then
8372 Process_Task_Objects (Visible_Declarations (Context));
8373 Process_Task_Objects (Private_Declarations (Context));
8374
8375 -- Otherwise the context of the activation is some construct with a
8376 -- declarative part. Note that the corresponding record type of a task
8377 -- type is controlled. Because of this, the finalization machinery must
8378 -- relocate the task object to the handled statements of the construct
8379 -- to perform proper finalization in case of an exception. Examine the
8380 -- statements of the construct rather than the declarations.
8381
8382 else
8383 pragma Assert (Nkind (Context) = N_Handled_Sequence_Of_Statements);
8384
8385 Process_Task_Objects (Statements (Context));
8386 end if;
8387 end Process_Activation_Generic;
8388
8389 ------------------------------------
8390 -- Process_Conditional_ABE_Access --
8391 ------------------------------------
8392
8393 procedure Process_Conditional_ABE_Access
8394 (Attr : Node_Id;
8395 In_Init_Cond : Boolean;
8396 In_Partial_Fin : Boolean;
8397 In_Task_Body : Boolean)
8398 is
8399 function Build_Access_Marker (Target_Id : Entity_Id) return Node_Id;
8400 pragma Inline (Build_Access_Marker);
8401 -- Create a suitable call marker which invokes target Target_Id
8402
8403 -------------------------
8404 -- Build_Access_Marker --
8405 -------------------------
8406
8407 function Build_Access_Marker (Target_Id : Entity_Id) return Node_Id is
8408 Marker : Node_Id;
8409
8410 begin
8411 Marker := Make_Call_Marker (Sloc (Attr));
8412
8413 -- Inherit relevant attributes from the attribute
8414
8415 -- Performance note: parent traversal
8416
8417 Set_Target (Marker, Target_Id);
8418 Set_Is_Declaration_Level_Node
8419 (Marker, Find_Enclosing_Level (Attr) = Declaration_Level);
8420 Set_Is_Dispatching_Call
8421 (Marker, False);
8422 Set_Is_Elaboration_Checks_OK_Node
8423 (Marker, Is_Elaboration_Checks_OK_Node (Attr));
8424 Set_Is_Source_Call
8425 (Marker, Comes_From_Source (Attr));
8426 Set_Is_SPARK_Mode_On_Node
8427 (Marker, Is_SPARK_Mode_On_Node (Attr));
8428
8429 -- Partially insert the call marker into the tree by setting its
8430 -- parent pointer.
8431
8432 Set_Parent (Marker, Attr);
8433
8434 return Marker;
8435 end Build_Access_Marker;
8436
8437 -- Local variables
8438
8439 Root : constant Node_Id := Root_Scenario;
8440 Target_Id : constant Entity_Id := Entity (Prefix (Attr));
8441
8442 Target_Attrs : Target_Attributes;
8443
8444 -- Start of processing for Process_Conditional_ABE_Access
8445
8446 begin
8447 -- Output relevant information when switch -gnatel (info messages on
8448 -- implicit Elaborate[_All] pragmas) is in effect.
8449
8450 if Elab_Info_Messages then
8451 Error_Msg_NE
8452 ("info: access to & during elaboration", Attr, Target_Id);
8453 end if;
8454
8455 Extract_Target_Attributes
8456 (Target_Id => Target_Id,
8457 Attrs => Target_Attrs);
8458
8459 -- Both the attribute and the corresponding body are in the same unit.
8460 -- The corresponding body must appear prior to the root scenario which
8461 -- started the recursive search. If this is not the case, then there is
8462 -- a potential ABE if the access value is used to call the subprogram.
8463 -- Emit a warning only when switch -gnatw.f (warnings on suspucious
8464 -- 'Access) is in effect.
8465
8466 if Warn_On_Elab_Access
8467 and then Present (Target_Attrs.Body_Decl)
8468 and then In_Extended_Main_Code_Unit (Target_Attrs.Body_Decl)
8469 and then Earlier_In_Extended_Unit (Root, Target_Attrs.Body_Decl)
8470 then
8471 Error_Msg_Name_1 := Attribute_Name (Attr);
8472 Error_Msg_NE ("??% attribute of & before body seen", Attr, Target_Id);
8473 Error_Msg_N ("\possible Program_Error on later references", Attr);
8474
8475 Output_Active_Scenarios (Attr);
8476 end if;
8477
8478 -- Treat the attribute as an immediate invocation of the target when
8479 -- switch -gnatd.o (conservative elaboration order for indirect calls)
8480 -- is in effect. Note that the prior elaboration of the unit containing
8481 -- the target is ensured processing the corresponding call marker.
8482
8483 if Debug_Flag_Dot_O then
8484 Process_Conditional_ABE
8485 (N => Build_Access_Marker (Target_Id),
8486 In_Init_Cond => In_Init_Cond,
8487 In_Partial_Fin => In_Partial_Fin,
8488 In_Task_Body => In_Task_Body);
8489
8490 -- Otherwise ensure that the unit with the corresponding body is
8491 -- elaborated prior to the main unit.
8492
8493 else
8494 Ensure_Prior_Elaboration
8495 (N => Attr,
8496 Unit_Id => Target_Attrs.Unit_Id,
8497 Prag_Nam => Name_Elaborate_All,
8498 In_Partial_Fin => In_Partial_Fin,
8499 In_Task_Body => In_Task_Body);
8500 end if;
8501 end Process_Conditional_ABE_Access;
8502
8503 ---------------------------------------------
8504 -- Process_Conditional_ABE_Activation_Impl --
8505 ---------------------------------------------
8506
8507 procedure Process_Conditional_ABE_Activation_Impl
8508 (Call : Node_Id;
8509 Call_Attrs : Call_Attributes;
8510 Obj_Id : Entity_Id;
8511 Task_Attrs : Task_Attributes;
8512 In_Init_Cond : Boolean;
8513 In_Partial_Fin : Boolean;
8514 In_Task_Body : Boolean)
8515 is
8516 Check_OK : constant Boolean :=
8517 not Is_Ignored_Ghost_Entity (Obj_Id)
8518 and then not Task_Attrs.Ghost_Mode_Ignore
8519 and then Is_Elaboration_Checks_OK_Id (Obj_Id)
8520 and then Task_Attrs.Elab_Checks_OK;
8521 -- A run-time ABE check may be installed only when the object and the
8522 -- task type have active elaboration checks, and both are not ignored
8523 -- Ghost constructs.
8524
8525 Root : constant Node_Id := Root_Scenario;
8526
8527 begin
8528 -- Output relevant information when switch -gnatel (info messages on
8529 -- implicit Elaborate[_All] pragmas) is in effect.
8530
8531 if Elab_Info_Messages then
8532 Error_Msg_NE
8533 ("info: activation of & during elaboration", Call, Obj_Id);
8534 end if;
8535
8536 -- Nothing to do when the activation is a guaranteed ABE
8537
8538 if Is_Known_Guaranteed_ABE (Call) then
8539 return;
8540
8541 -- Nothing to do when the root scenario appears at the declaration
8542 -- level and the task is in the same unit, but outside this context.
8543
8544 -- task type Task_Typ; -- task declaration
8545
8546 -- procedure Proc is
8547 -- function A ... is
8548 -- begin
8549 -- if Some_Condition then
8550 -- declare
8551 -- T : Task_Typ;
8552 -- begin
8553 -- <activation call> -- activation site
8554 -- end;
8555 -- ...
8556 -- end A;
8557
8558 -- X : ... := A; -- root scenario
8559 -- ...
8560
8561 -- task body Task_Typ is
8562 -- ...
8563 -- end Task_Typ;
8564
8565 -- In the example above, the context of X is the declarative list of
8566 -- Proc. The "elaboration" of X may reach the activation of T whose body
8567 -- is defined outside of X's context. The task body is relevant only
8568 -- when Proc is invoked, but this happens only in "normal" elaboration,
8569 -- therefore the task body must not be considered if this is not the
8570 -- case.
8571
8572 -- Performance note: parent traversal
8573
8574 elsif Is_Up_Level_Target (Task_Attrs.Task_Decl) then
8575 return;
8576
8577 -- Nothing to do when the activation is ABE-safe
8578
8579 -- generic
8580 -- package Gen is
8581 -- task type Task_Typ;
8582 -- end Gen;
8583
8584 -- package body Gen is
8585 -- task body Task_Typ is
8586 -- begin
8587 -- ...
8588 -- end Task_Typ;
8589 -- end Gen;
8590
8591 -- with Gen;
8592 -- procedure Main is
8593 -- package Nested is
8594 -- ...
8595 -- end Nested;
8596
8597 -- package body Nested is
8598 -- package Inst is new Gen;
8599 -- T : Inst.Task_Typ;
8600 -- [begin]
8601 -- <activation call> -- safe activation
8602 -- end Nested;
8603 -- ...
8604
8605 elsif Is_Safe_Activation (Call, Task_Attrs.Task_Decl) then
8606
8607 -- Note that the task body must still be examined for any nested
8608 -- scenarios.
8609
8610 null;
8611
8612 -- The activation call and the task body are both in the main unit
8613
8614 elsif Present (Task_Attrs.Body_Decl)
8615 and then In_Extended_Main_Code_Unit (Task_Attrs.Body_Decl)
8616 then
8617 -- If the root scenario appears prior to the task body, then this is
8618 -- a possible ABE with respect to the root scenario.
8619
8620 -- task type Task_Typ;
8621
8622 -- function A ... is
8623 -- begin
8624 -- if Some_Condition then
8625 -- declare
8626 -- package Pack is
8627 -- ...
8628 -- end Pack;
8629
8630 -- package body Pack is
8631 -- T : Task_Typ;
8632 -- [begin]
8633 -- <activation call> -- activation of T
8634 -- end Pack;
8635 -- ...
8636 -- end A;
8637
8638 -- X : ... := A; -- root scenario
8639
8640 -- task body Task_Typ is -- task body
8641 -- ...
8642 -- end Task_Typ;
8643
8644 -- Y : ... := A; -- root scenario
8645
8646 -- IMPORTANT: The activation of T is a possible ABE for X, but
8647 -- not for Y. Intalling an unconditional ABE raise prior to the
8648 -- activation call would be wrong as it will fail for Y as well
8649 -- but in Y's case the activation of T is never an ABE.
8650
8651 if Earlier_In_Extended_Unit (Root, Task_Attrs.Body_Decl) then
8652
8653 -- Do not emit any ABE diagnostics when the activation occurs in
8654 -- a partial finalization context because this leads to confusing
8655 -- noise.
8656
8657 if In_Partial_Fin then
8658 null;
8659
8660 -- ABE diagnostics are emitted only in the static model because
8661 -- there is a well-defined order to visiting scenarios. Without
8662 -- this order diagnostics appear jumbled and result in unwanted
8663 -- noise.
8664
8665 elsif Static_Elaboration_Checks then
8666 Error_Msg_Sloc := Sloc (Call);
8667 Error_Msg_N
8668 ("??task & will be activated # before elaboration of its "
8669 & "body", Obj_Id);
8670 Error_Msg_N
8671 ("\Program_Error may be raised at run time", Obj_Id);
8672
8673 Output_Active_Scenarios (Obj_Id);
8674 end if;
8675
8676 -- Install a conditional run-time ABE check to verify that the
8677 -- task body has been elaborated prior to the activation call.
8678
8679 if Check_OK then
8680 Install_ABE_Check
8681 (N => Call,
8682 Ins_Nod => Call,
8683 Target_Id => Task_Attrs.Spec_Id,
8684 Target_Decl => Task_Attrs.Task_Decl,
8685 Target_Body => Task_Attrs.Body_Decl);
8686 end if;
8687 end if;
8688
8689 -- Otherwise the task body is not available in this compilation or it
8690 -- resides in an external unit. Install a run-time ABE check to verify
8691 -- that the task body has been elaborated prior to the activation call
8692 -- when the dynamic model is in effect.
8693
8694 elsif Dynamic_Elaboration_Checks and then Check_OK then
8695 Install_ABE_Check
8696 (N => Call,
8697 Ins_Nod => Call,
8698 Id => Task_Attrs.Unit_Id);
8699 end if;
8700
8701 -- Both the activation call and task type are subject to SPARK_Mode
8702 -- On, this triggers the SPARK rules for task activation. Compared to
8703 -- calls and instantiations, task activation in SPARK does not require
8704 -- the presence of Elaborate[_All] pragmas in case the task type is
8705 -- defined outside the main unit. This is because SPARK utilizes a
8706 -- special policy which activates all tasks after the main unit has
8707 -- finished its elaboration.
8708
8709 if Call_Attrs.SPARK_Mode_On and Task_Attrs.SPARK_Mode_On then
8710 null;
8711
8712 -- Otherwise the Ada rules are in effect. Ensure that the unit with the
8713 -- task body is elaborated prior to the main unit.
8714
8715 else
8716 Ensure_Prior_Elaboration
8717 (N => Call,
8718 Unit_Id => Task_Attrs.Unit_Id,
8719 Prag_Nam => Name_Elaborate_All,
8720 In_Partial_Fin => In_Partial_Fin,
8721 In_Task_Body => In_Task_Body);
8722 end if;
8723
8724 Traverse_Body
8725 (N => Task_Attrs.Body_Decl,
8726 In_Init_Cond => In_Init_Cond,
8727 In_Partial_Fin => In_Partial_Fin,
8728 In_Task_Body => True);
8729 end Process_Conditional_ABE_Activation_Impl;
8730
8731 procedure Process_Conditional_ABE_Activation is
8732 new Process_Activation_Generic (Process_Conditional_ABE_Activation_Impl);
8733
8734 ----------------------------------
8735 -- Process_Conditional_ABE_Call --
8736 ----------------------------------
8737
8738 procedure Process_Conditional_ABE_Call
8739 (Call : Node_Id;
8740 Call_Attrs : Call_Attributes;
8741 Target_Id : Entity_Id;
8742 In_Init_Cond : Boolean;
8743 In_Partial_Fin : Boolean;
8744 In_Task_Body : Boolean)
8745 is
8746 function In_Initialization_Context (N : Node_Id) return Boolean;
8747 -- Determine whether arbitrary node N appears within a type init proc,
8748 -- primitive [Deep_]Initialize, or a block created for initialization
8749 -- purposes.
8750
8751 function Is_Partial_Finalization_Proc return Boolean;
8752 pragma Inline (Is_Partial_Finalization_Proc);
8753 -- Determine whether call Call with target Target_Id invokes a partial
8754 -- finalization procedure.
8755
8756 -------------------------------
8757 -- In_Initialization_Context --
8758 -------------------------------
8759
8760 function In_Initialization_Context (N : Node_Id) return Boolean is
8761 Par : Node_Id;
8762 Spec_Id : Entity_Id;
8763
8764 begin
8765 -- Climb the parent chain looking for initialization actions
8766
8767 Par := Parent (N);
8768 while Present (Par) loop
8769
8770 -- A block may be part of the initialization actions of a default
8771 -- initialized object.
8772
8773 if Nkind (Par) = N_Block_Statement
8774 and then Is_Initialization_Block (Par)
8775 then
8776 return True;
8777
8778 -- A subprogram body may denote an initialization routine
8779
8780 elsif Nkind (Par) = N_Subprogram_Body then
8781 Spec_Id := Unique_Defining_Entity (Par);
8782
8783 -- The current subprogram body denotes a type init proc or
8784 -- primitive [Deep_]Initialize.
8785
8786 if Is_Init_Proc (Spec_Id)
8787 or else Is_Controlled_Proc (Spec_Id, Name_Initialize)
8788 or else Is_TSS (Spec_Id, TSS_Deep_Initialize)
8789 then
8790 return True;
8791 end if;
8792
8793 -- Prevent the search from going too far
8794
8795 elsif Is_Body_Or_Package_Declaration (Par) then
8796 exit;
8797 end if;
8798
8799 Par := Parent (Par);
8800 end loop;
8801
8802 return False;
8803 end In_Initialization_Context;
8804
8805 ----------------------------------
8806 -- Is_Partial_Finalization_Proc --
8807 ----------------------------------
8808
8809 function Is_Partial_Finalization_Proc return Boolean is
8810 begin
8811 -- To qualify, the target must denote primitive [Deep_]Finalize or a
8812 -- finalizer procedure, and the call must appear in an initialization
8813 -- context.
8814
8815 return
8816 (Is_Controlled_Proc (Target_Id, Name_Finalize)
8817 or else Is_Finalizer_Proc (Target_Id)
8818 or else Is_TSS (Target_Id, TSS_Deep_Finalize))
8819 and then In_Initialization_Context (Call);
8820 end Is_Partial_Finalization_Proc;
8821
8822 -- Local variables
8823
8824 Init_Cond_On : Boolean;
8825 Partial_Fin_On : Boolean;
8826 SPARK_Rules_On : Boolean;
8827 Target_Attrs : Target_Attributes;
8828
8829 -- Start of processing for Process_Conditional_ABE_Call
8830
8831 begin
8832 Extract_Target_Attributes
8833 (Target_Id => Target_Id,
8834 Attrs => Target_Attrs);
8835
8836 -- The call occurs in an initial condition context when a prior
8837 -- scenario is already in that mode, or when the target denotes
8838 -- an Initial_Condition procedure.
8839
8840 Init_Cond_On :=
8841 In_Init_Cond or else Is_Initial_Condition_Proc (Target_Id);
8842
8843 -- The call occurs in a partial finalization context when a prior
8844 -- scenario is already in that mode, or when the target denotes a
8845 -- [Deep_]Finalize primitive or a finalizer within an initialization
8846 -- context.
8847
8848 Partial_Fin_On := In_Partial_Fin or else Is_Partial_Finalization_Proc;
8849
8850 -- The SPARK rules are in effect when both the call and target are
8851 -- subject to SPARK_Mode On.
8852
8853 SPARK_Rules_On :=
8854 Call_Attrs.SPARK_Mode_On and Target_Attrs.SPARK_Mode_On;
8855
8856 -- Output relevant information when switch -gnatel (info messages on
8857 -- implicit Elaborate[_All] pragmas) is in effect.
8858
8859 if Elab_Info_Messages then
8860 Info_Call
8861 (Call => Call,
8862 Target_Id => Target_Id,
8863 Info_Msg => True,
8864 In_SPARK => SPARK_Rules_On);
8865 end if;
8866
8867 -- Check whether the invocation of an entry clashes with an existing
8868 -- restriction.
8869
8870 if Is_Protected_Entry (Target_Id) then
8871 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, Call);
8872
8873 elsif Is_Task_Entry (Target_Id) then
8874 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, Call);
8875
8876 -- Task entry calls are never processed because the entry being
8877 -- invoked does not have a corresponding "body", it has a select.
8878
8879 return;
8880 end if;
8881
8882 -- Nothing to do when the call is a guaranteed ABE
8883
8884 if Is_Known_Guaranteed_ABE (Call) then
8885 return;
8886
8887 -- Nothing to do when the root scenario appears at the declaration level
8888 -- and the target is in the same unit, but outside this context.
8889
8890 -- function B ...; -- target declaration
8891
8892 -- procedure Proc is
8893 -- function A ... is
8894 -- begin
8895 -- if Some_Condition then
8896 -- return B; -- call site
8897 -- ...
8898 -- end A;
8899
8900 -- X : ... := A; -- root scenario
8901 -- ...
8902
8903 -- function B ... is
8904 -- ...
8905 -- end B;
8906
8907 -- In the example above, the context of X is the declarative region of
8908 -- Proc. The "elaboration" of X may eventually reach B which is defined
8909 -- outside of X's context. B is relevant only when Proc is invoked, but
8910 -- this happens only by means of "normal" elaboration, therefore B must
8911 -- not be considered if this is not the case.
8912
8913 -- Performance note: parent traversal
8914
8915 elsif Is_Up_Level_Target (Target_Attrs.Spec_Decl) then
8916 return;
8917
8918 -- The SPARK rules are in effect. Note that -gnatd.v (enforce SPARK
8919 -- elaboration rules in SPARK code) is intentionally not taken into
8920 -- account here because Process_Conditional_ABE_Call_SPARK has two
8921 -- separate modes of operation.
8922
8923 elsif SPARK_Rules_On then
8924 Process_Conditional_ABE_Call_SPARK
8925 (Call => Call,
8926 Target_Id => Target_Id,
8927 Target_Attrs => Target_Attrs,
8928 In_Init_Cond => Init_Cond_On,
8929 In_Partial_Fin => Partial_Fin_On,
8930 In_Task_Body => In_Task_Body);
8931
8932 -- Otherwise the Ada rules are in effect
8933
8934 else
8935 Process_Conditional_ABE_Call_Ada
8936 (Call => Call,
8937 Call_Attrs => Call_Attrs,
8938 Target_Id => Target_Id,
8939 Target_Attrs => Target_Attrs,
8940 In_Partial_Fin => Partial_Fin_On,
8941 In_Task_Body => In_Task_Body);
8942 end if;
8943
8944 -- Inspect the target body (and barried function) for other suitable
8945 -- elaboration scenarios.
8946
8947 Traverse_Body
8948 (N => Target_Attrs.Body_Barf,
8949 In_Init_Cond => Init_Cond_On,
8950 In_Partial_Fin => Partial_Fin_On,
8951 In_Task_Body => In_Task_Body);
8952
8953 Traverse_Body
8954 (N => Target_Attrs.Body_Decl,
8955 In_Init_Cond => Init_Cond_On,
8956 In_Partial_Fin => Partial_Fin_On,
8957 In_Task_Body => In_Task_Body);
8958 end Process_Conditional_ABE_Call;
8959
8960 --------------------------------------
8961 -- Process_Conditional_ABE_Call_Ada --
8962 --------------------------------------
8963
8964 procedure Process_Conditional_ABE_Call_Ada
8965 (Call : Node_Id;
8966 Call_Attrs : Call_Attributes;
8967 Target_Id : Entity_Id;
8968 Target_Attrs : Target_Attributes;
8969 In_Partial_Fin : Boolean;
8970 In_Task_Body : Boolean)
8971 is
8972 Check_OK : constant Boolean :=
8973 not Call_Attrs.Ghost_Mode_Ignore
8974 and then not Target_Attrs.Ghost_Mode_Ignore
8975 and then Call_Attrs.Elab_Checks_OK
8976 and then Target_Attrs.Elab_Checks_OK;
8977 -- A run-time ABE check may be installed only when both the call and the
8978 -- target have active elaboration checks, and both are not ignored Ghost
8979 -- constructs.
8980
8981 Root : constant Node_Id := Root_Scenario;
8982
8983 begin
8984 -- Nothing to do for an Ada dispatching call because there are no ABE
8985 -- diagnostics for either models. ABE checks for the dynamic model are
8986 -- handled by Install_Primitive_Elaboration_Check.
8987
8988 if Call_Attrs.Is_Dispatching then
8989 return;
8990
8991 -- Nothing to do when the call is ABE-safe
8992
8993 -- generic
8994 -- function Gen ...;
8995
8996 -- function Gen ... is
8997 -- begin
8998 -- ...
8999 -- end Gen;
9000
9001 -- with Gen;
9002 -- procedure Main is
9003 -- function Inst is new Gen;
9004 -- X : ... := Inst; -- safe call
9005 -- ...
9006
9007 elsif Is_Safe_Call (Call, Target_Attrs) then
9008 return;
9009
9010 -- The call and the target body are both in the main unit
9011
9012 elsif Present (Target_Attrs.Body_Decl)
9013 and then In_Extended_Main_Code_Unit (Target_Attrs.Body_Decl)
9014 then
9015 -- If the root scenario appears prior to the target body, then this
9016 -- is a possible ABE with respect to the root scenario.
9017
9018 -- function B ...;
9019
9020 -- function A ... is
9021 -- begin
9022 -- if Some_Condition then
9023 -- return B; -- call site
9024 -- ...
9025 -- end A;
9026
9027 -- X : ... := A; -- root scenario
9028
9029 -- function B ... is -- target body
9030 -- ...
9031 -- end B;
9032
9033 -- Y : ... := A; -- root scenario
9034
9035 -- IMPORTANT: The call to B from A is a possible ABE for X, but not
9036 -- for Y. Installing an unconditional ABE raise prior to the call to
9037 -- B would be wrong as it will fail for Y as well, but in Y's case
9038 -- the call to B is never an ABE.
9039
9040 if Earlier_In_Extended_Unit (Root, Target_Attrs.Body_Decl) then
9041
9042 -- Do not emit any ABE diagnostics when the call occurs in a
9043 -- partial finalization context because this leads to confusing
9044 -- noise.
9045
9046 if In_Partial_Fin then
9047 null;
9048
9049 -- ABE diagnostics are emitted only in the static model because
9050 -- there is a well-defined order to visiting scenarios. Without
9051 -- this order diagnostics appear jumbled and result in unwanted
9052 -- noise.
9053
9054 elsif Static_Elaboration_Checks then
9055 Error_Msg_NE
9056 ("??cannot call & before body seen", Call, Target_Id);
9057 Error_Msg_N ("\Program_Error may be raised at run time", Call);
9058
9059 Output_Active_Scenarios (Call);
9060 end if;
9061
9062 -- Install a conditional run-time ABE check to verify that the
9063 -- target body has been elaborated prior to the call.
9064
9065 if Check_OK then
9066 Install_ABE_Check
9067 (N => Call,
9068 Ins_Nod => Call,
9069 Target_Id => Target_Attrs.Spec_Id,
9070 Target_Decl => Target_Attrs.Spec_Decl,
9071 Target_Body => Target_Attrs.Body_Decl);
9072 end if;
9073 end if;
9074
9075 -- Otherwise the target body is not available in this compilation or it
9076 -- resides in an external unit. Install a run-time ABE check to verify
9077 -- that the target body has been elaborated prior to the call site when
9078 -- the dynamic model is in effect.
9079
9080 elsif Dynamic_Elaboration_Checks and then Check_OK then
9081 Install_ABE_Check
9082 (N => Call,
9083 Ins_Nod => Call,
9084 Id => Target_Attrs.Unit_Id);
9085 end if;
9086
9087 -- Ensure that the unit with the target body is elaborated prior to the
9088 -- main unit. The implicit Elaborate[_All] is generated only when the
9089 -- call has elaboration checks enabled. This behaviour parallels that of
9090 -- the old ABE mechanism.
9091
9092 if Call_Attrs.Elab_Checks_OK then
9093 Ensure_Prior_Elaboration
9094 (N => Call,
9095 Unit_Id => Target_Attrs.Unit_Id,
9096 Prag_Nam => Name_Elaborate_All,
9097 In_Partial_Fin => In_Partial_Fin,
9098 In_Task_Body => In_Task_Body);
9099 end if;
9100 end Process_Conditional_ABE_Call_Ada;
9101
9102 ----------------------------------------
9103 -- Process_Conditional_ABE_Call_SPARK --
9104 ----------------------------------------
9105
9106 procedure Process_Conditional_ABE_Call_SPARK
9107 (Call : Node_Id;
9108 Target_Id : Entity_Id;
9109 Target_Attrs : Target_Attributes;
9110 In_Init_Cond : Boolean;
9111 In_Partial_Fin : Boolean;
9112 In_Task_Body : Boolean)
9113 is
9114 Region : Node_Id;
9115
9116 begin
9117 -- The call and the target body are both in the main unit
9118
9119 if Present (Target_Attrs.Body_Decl)
9120 and then In_Extended_Main_Code_Unit (Target_Attrs.Body_Decl)
9121 then
9122 -- If the call appears prior to the target body, then the call must
9123 -- appear within the early call region of the target body.
9124
9125 -- function B ...;
9126
9127 -- X : ... := B; -- call site
9128
9129 -- <preelaborable construct 1> --+
9130 -- ... | early call region
9131 -- <preelaborable construct N> --+
9132
9133 -- function B ... is -- target body
9134 -- ...
9135 -- end B;
9136
9137 -- When the call to B is not nested within some other scenario, the
9138 -- call is automatically illegal because it can never appear in the
9139 -- early call region of B's body. This is equivalent to a guaranteed
9140 -- ABE.
9141
9142 -- <preelaborable construct 1> --+
9143 -- |
9144 -- function B ...; |
9145 -- |
9146 -- function A ... is |
9147 -- begin | early call region
9148 -- if Some_Condition then
9149 -- return B; -- call site
9150 -- ...
9151 -- end A; |
9152 -- |
9153 -- <preelaborable construct N> --+
9154
9155 -- function B ... is -- target body
9156 -- ...
9157 -- end B;
9158
9159 -- When the call to B is nested within some other scenario, the call
9160 -- is always ABE-safe. It is not immediately obvious why this is the
9161 -- case. The elaboration safety follows from the early call region
9162 -- rule being applied to ALL calls preceding their associated bodies.
9163
9164 -- In the example above, the call to B is safe as long as the call to
9165 -- A is safe. There are several cases to consider:
9166
9167 -- <call 1 to A>
9168 -- function B ...;
9169
9170 -- <call 2 to A>
9171 -- function A ... is
9172 -- begin
9173 -- if Some_Condition then
9174 -- return B;
9175 -- ...
9176 -- end A;
9177
9178 -- <call 3 to A>
9179 -- function B ... is
9180 -- ...
9181 -- end B;
9182
9183 -- * Call 1 - This call is either nested within some scenario or not,
9184 -- which falls under the two general cases outlined above.
9185
9186 -- * Call 2 - This is the same case as Call 1.
9187
9188 -- * Call 3 - The placement of this call limits the range of B's
9189 -- early call region unto call 3, therefore the call to B is no
9190 -- longer within the early call region of B's body, making it ABE-
9191 -- unsafe and therefore illegal.
9192
9193 if Earlier_In_Extended_Unit (Call, Target_Attrs.Body_Decl) then
9194
9195 -- Do not emit any ABE diagnostics when the call occurs in an
9196 -- initial condition context because this leads to incorrect
9197 -- diagnostics.
9198
9199 if In_Init_Cond then
9200 null;
9201
9202 -- Do not emit any ABE diagnostics when the call occurs in a
9203 -- partial finalization context because this leads to confusing
9204 -- noise.
9205
9206 elsif In_Partial_Fin then
9207 null;
9208
9209 -- ABE diagnostics are emitted only in the static model because
9210 -- there is a well-defined order to visiting scenarios. Without
9211 -- this order diagnostics appear jumbled and result in unwanted
9212 -- noise.
9213
9214 elsif Static_Elaboration_Checks then
9215
9216 -- Ensure that a call which textually precedes the subprogram
9217 -- body it invokes appears within the early call region of the
9218 -- subprogram body.
9219
9220 -- IMPORTANT: This check must always be performed even when
9221 -- -gnatd.v (enforce SPARK elaboration rules in SPARK code) is
9222 -- not specified because the static model cannot guarantee the
9223 -- absence of elaboration issues in the presence of dispatching
9224 -- calls.
9225
9226 Region := Find_Early_Call_Region (Target_Attrs.Body_Decl);
9227
9228 if Earlier_In_Extended_Unit (Call, Region) then
9229 Error_Msg_NE
9230 ("call must appear within early call region of subprogram "
9231 & "body & (SPARK RM 7.7(3))", Call, Target_Id);
9232
9233 Error_Msg_Sloc := Sloc (Region);
9234 Error_Msg_N ("\region starts #", Call);
9235
9236 Error_Msg_Sloc := Sloc (Target_Attrs.Body_Decl);
9237 Error_Msg_N ("\region ends #", Call);
9238
9239 Output_Active_Scenarios (Call);
9240 end if;
9241 end if;
9242
9243 -- Otherwise the call appears after the target body. The call is
9244 -- ABE-safe as a consequence of applying the early call region rule
9245 -- to ALL calls preceding their associated bodies.
9246
9247 else
9248 null;
9249 end if;
9250 end if;
9251
9252 -- A call to a source target or to a target which emulates Ada or SPARK
9253 -- semantics imposes an Elaborate_All requirement on the context of the
9254 -- main unit. Determine whether the context has a pragma strong enough
9255 -- to meet the requirement.
9256
9257 -- IMPORTANT: This check must be performed only when -gnatd.v (enforce
9258 -- SPARK elaboration rules in SPARK code) is active because the static
9259 -- model can ensure the prior elaboration of the unit which contains a
9260 -- body by installing an implicit Elaborate[_All] pragma.
9261
9262 if Debug_Flag_Dot_V then
9263 if Target_Attrs.From_Source
9264 or else Is_Ada_Semantic_Target (Target_Id)
9265 or else Is_SPARK_Semantic_Target (Target_Id)
9266 then
9267 Meet_Elaboration_Requirement
9268 (N => Call,
9269 Target_Id => Target_Id,
9270 Req_Nam => Name_Elaborate_All);
9271 end if;
9272
9273 -- Otherwise ensure that the unit with the target body is elaborated
9274 -- prior to the main unit.
9275
9276 else
9277 Ensure_Prior_Elaboration
9278 (N => Call,
9279 Unit_Id => Target_Attrs.Unit_Id,
9280 Prag_Nam => Name_Elaborate_All,
9281 In_Partial_Fin => In_Partial_Fin,
9282 In_Task_Body => In_Task_Body);
9283 end if;
9284 end Process_Conditional_ABE_Call_SPARK;
9285
9286 -------------------------------------------
9287 -- Process_Conditional_ABE_Instantiation --
9288 -------------------------------------------
9289
9290 procedure Process_Conditional_ABE_Instantiation
9291 (Exp_Inst : Node_Id;
9292 In_Partial_Fin : Boolean;
9293 In_Task_Body : Boolean)
9294 is
9295 Gen_Attrs : Target_Attributes;
9296 Gen_Id : Entity_Id;
9297 Inst : Node_Id;
9298 Inst_Attrs : Instantiation_Attributes;
9299 Inst_Id : Entity_Id;
9300
9301 SPARK_Rules_On : Boolean;
9302 -- This flag is set when the SPARK rules are in effect
9303
9304 begin
9305 Extract_Instantiation_Attributes
9306 (Exp_Inst => Exp_Inst,
9307 Inst => Inst,
9308 Inst_Id => Inst_Id,
9309 Gen_Id => Gen_Id,
9310 Attrs => Inst_Attrs);
9311
9312 Extract_Target_Attributes (Gen_Id, Gen_Attrs);
9313
9314 -- The SPARK rules are in effect when both the instantiation and generic
9315 -- are subject to SPARK_Mode On.
9316
9317 SPARK_Rules_On := Inst_Attrs.SPARK_Mode_On and Gen_Attrs.SPARK_Mode_On;
9318
9319 -- Output relevant information when switch -gnatel (info messages on
9320 -- implicit Elaborate[_All] pragmas) is in effect.
9321
9322 if Elab_Info_Messages then
9323 Info_Instantiation
9324 (Inst => Inst,
9325 Gen_Id => Gen_Id,
9326 Info_Msg => True,
9327 In_SPARK => SPARK_Rules_On);
9328 end if;
9329
9330 -- Nothing to do when the instantiation is a guaranteed ABE
9331
9332 if Is_Known_Guaranteed_ABE (Inst) then
9333 return;
9334
9335 -- Nothing to do when the root scenario appears at the declaration level
9336 -- and the generic is in the same unit, but outside this context.
9337
9338 -- generic
9339 -- procedure Gen is ...; -- generic declaration
9340
9341 -- procedure Proc is
9342 -- function A ... is
9343 -- begin
9344 -- if Some_Condition then
9345 -- declare
9346 -- procedure I is new Gen; -- instantiation site
9347 -- ...
9348 -- ...
9349 -- end A;
9350
9351 -- X : ... := A; -- root scenario
9352 -- ...
9353
9354 -- procedure Gen is
9355 -- ...
9356 -- end Gen;
9357
9358 -- In the example above, the context of X is the declarative region of
9359 -- Proc. The "elaboration" of X may eventually reach Gen which appears
9360 -- outside of X's context. Gen is relevant only when Proc is invoked,
9361 -- but this happens only by means of "normal" elaboration, therefore
9362 -- Gen must not be considered if this is not the case.
9363
9364 -- Performance note: parent traversal
9365
9366 elsif Is_Up_Level_Target (Gen_Attrs.Spec_Decl) then
9367 return;
9368
9369 -- The SPARK rules are in effect
9370
9371 elsif SPARK_Rules_On then
9372 Process_Conditional_ABE_Instantiation_SPARK
9373 (Inst => Inst,
9374 Gen_Id => Gen_Id,
9375 Gen_Attrs => Gen_Attrs,
9376 In_Partial_Fin => In_Partial_Fin,
9377 In_Task_Body => In_Task_Body);
9378
9379 -- Otherwise the Ada rules are in effect, or SPARK code is allowed to
9380 -- violate the SPARK rules.
9381
9382 else
9383 Process_Conditional_ABE_Instantiation_Ada
9384 (Exp_Inst => Exp_Inst,
9385 Inst => Inst,
9386 Inst_Attrs => Inst_Attrs,
9387 Gen_Id => Gen_Id,
9388 Gen_Attrs => Gen_Attrs,
9389 In_Partial_Fin => In_Partial_Fin,
9390 In_Task_Body => In_Task_Body);
9391 end if;
9392 end Process_Conditional_ABE_Instantiation;
9393
9394 -----------------------------------------------
9395 -- Process_Conditional_ABE_Instantiation_Ada --
9396 -----------------------------------------------
9397
9398 procedure Process_Conditional_ABE_Instantiation_Ada
9399 (Exp_Inst : Node_Id;
9400 Inst : Node_Id;
9401 Inst_Attrs : Instantiation_Attributes;
9402 Gen_Id : Entity_Id;
9403 Gen_Attrs : Target_Attributes;
9404 In_Partial_Fin : Boolean;
9405 In_Task_Body : Boolean)
9406 is
9407 Check_OK : constant Boolean :=
9408 not Inst_Attrs.Ghost_Mode_Ignore
9409 and then not Gen_Attrs.Ghost_Mode_Ignore
9410 and then Inst_Attrs.Elab_Checks_OK
9411 and then Gen_Attrs.Elab_Checks_OK;
9412 -- A run-time ABE check may be installed only when both the instance and
9413 -- the generic have active elaboration checks and both are not ignored
9414 -- Ghost constructs.
9415
9416 Root : constant Node_Id := Root_Scenario;
9417
9418 begin
9419 -- Nothing to do when the instantiation is ABE-safe
9420
9421 -- generic
9422 -- package Gen is
9423 -- ...
9424 -- end Gen;
9425
9426 -- package body Gen is
9427 -- ...
9428 -- end Gen;
9429
9430 -- with Gen;
9431 -- procedure Main is
9432 -- package Inst is new Gen (ABE); -- safe instantiation
9433 -- ...
9434
9435 if Is_Safe_Instantiation (Inst, Gen_Attrs) then
9436 return;
9437
9438 -- The instantiation and the generic body are both in the main unit
9439
9440 elsif Present (Gen_Attrs.Body_Decl)
9441 and then In_Extended_Main_Code_Unit (Gen_Attrs.Body_Decl)
9442 then
9443 -- If the root scenario appears prior to the generic body, then this
9444 -- is a possible ABE with respect to the root scenario.
9445
9446 -- generic
9447 -- package Gen is
9448 -- ...
9449 -- end Gen;
9450
9451 -- function A ... is
9452 -- begin
9453 -- if Some_Condition then
9454 -- declare
9455 -- package Inst is new Gen; -- instantiation site
9456 -- ...
9457 -- end A;
9458
9459 -- X : ... := A; -- root scenario
9460
9461 -- package body Gen is -- generic body
9462 -- ...
9463 -- end Gen;
9464
9465 -- Y : ... := A; -- root scenario
9466
9467 -- IMPORTANT: The instantiation of Gen is a possible ABE for X, but
9468 -- not for Y. Installing an unconditional ABE raise prior to the
9469 -- instance site would be wrong as it will fail for Y as well, but in
9470 -- Y's case the instantiation of Gen is never an ABE.
9471
9472 if Earlier_In_Extended_Unit (Root, Gen_Attrs.Body_Decl) then
9473
9474 -- Do not emit any ABE diagnostics when the instantiation occurs
9475 -- in partial finalization context because this leads to unwanted
9476 -- noise.
9477
9478 if In_Partial_Fin then
9479 null;
9480
9481 -- ABE diagnostics are emitted only in the static model because
9482 -- there is a well-defined order to visiting scenarios. Without
9483 -- this order diagnostics appear jumbled and result in unwanted
9484 -- noise.
9485
9486 elsif Static_Elaboration_Checks then
9487 Error_Msg_NE
9488 ("??cannot instantiate & before body seen", Inst, Gen_Id);
9489 Error_Msg_N ("\Program_Error may be raised at run time", Inst);
9490
9491 Output_Active_Scenarios (Inst);
9492 end if;
9493
9494 -- Install a conditional run-time ABE check to verify that the
9495 -- generic body has been elaborated prior to the instantiation.
9496
9497 if Check_OK then
9498 Install_ABE_Check
9499 (N => Inst,
9500 Ins_Nod => Exp_Inst,
9501 Target_Id => Gen_Attrs.Spec_Id,
9502 Target_Decl => Gen_Attrs.Spec_Decl,
9503 Target_Body => Gen_Attrs.Body_Decl);
9504 end if;
9505 end if;
9506
9507 -- Otherwise the generic body is not available in this compilation or it
9508 -- resides in an external unit. Install a run-time ABE check to verify
9509 -- that the generic body has been elaborated prior to the instantiation
9510 -- when the dynamic model is in effect.
9511
9512 elsif Dynamic_Elaboration_Checks and then Check_OK then
9513 Install_ABE_Check
9514 (N => Inst,
9515 Ins_Nod => Exp_Inst,
9516 Id => Gen_Attrs.Unit_Id);
9517 end if;
9518
9519 -- Ensure that the unit with the generic body is elaborated prior to
9520 -- the main unit. No implicit pragma Elaborate is generated if the
9521 -- instantiation has elaboration checks suppressed. This behaviour
9522 -- parallels that of the old ABE mechanism.
9523
9524 if Inst_Attrs.Elab_Checks_OK then
9525 Ensure_Prior_Elaboration
9526 (N => Inst,
9527 Unit_Id => Gen_Attrs.Unit_Id,
9528 Prag_Nam => Name_Elaborate,
9529 In_Partial_Fin => In_Partial_Fin,
9530 In_Task_Body => In_Task_Body);
9531 end if;
9532 end Process_Conditional_ABE_Instantiation_Ada;
9533
9534 -------------------------------------------------
9535 -- Process_Conditional_ABE_Instantiation_SPARK --
9536 -------------------------------------------------
9537
9538 procedure Process_Conditional_ABE_Instantiation_SPARK
9539 (Inst : Node_Id;
9540 Gen_Id : Entity_Id;
9541 Gen_Attrs : Target_Attributes;
9542 In_Partial_Fin : Boolean;
9543 In_Task_Body : Boolean)
9544 is
9545 Req_Nam : Name_Id;
9546
9547 begin
9548 -- A source instantiation imposes an Elaborate[_All] requirement on the
9549 -- context of the main unit. Determine whether the context has a pragma
9550 -- strong enough to meet the requirement. The check is orthogonal to the
9551 -- ABE ramifications of the instantiation.
9552
9553 -- IMPORTANT: This check must be performed only when -gnatd.v (enforce
9554 -- SPARK elaboration rules in SPARK code) is active because the static
9555 -- model can ensure the prior elaboration of the unit which contains a
9556 -- body by installing an implicit Elaborate[_All] pragma.
9557
9558 if Debug_Flag_Dot_V then
9559 if Nkind (Inst) = N_Package_Instantiation then
9560 Req_Nam := Name_Elaborate_All;
9561 else
9562 Req_Nam := Name_Elaborate;
9563 end if;
9564
9565 Meet_Elaboration_Requirement
9566 (N => Inst,
9567 Target_Id => Gen_Id,
9568 Req_Nam => Req_Nam);
9569
9570 -- Otherwise ensure that the unit with the target body is elaborated
9571 -- prior to the main unit.
9572
9573 else
9574 Ensure_Prior_Elaboration
9575 (N => Inst,
9576 Unit_Id => Gen_Attrs.Unit_Id,
9577 Prag_Nam => Name_Elaborate,
9578 In_Partial_Fin => In_Partial_Fin,
9579 In_Task_Body => In_Task_Body);
9580 end if;
9581 end Process_Conditional_ABE_Instantiation_SPARK;
9582
9583 -------------------------------------------------
9584 -- Process_Conditional_ABE_Variable_Assignment --
9585 -------------------------------------------------
9586
9587 procedure Process_Conditional_ABE_Variable_Assignment (Asmt : Node_Id) is
9588 Var_Id : constant Entity_Id := Entity (Extract_Assignment_Name (Asmt));
9589 Prag : constant Node_Id := SPARK_Pragma (Var_Id);
9590
9591 SPARK_Rules_On : Boolean;
9592 -- This flag is set when the SPARK rules are in effect
9593
9594 begin
9595 -- The SPARK rules are in effect when both the assignment and the
9596 -- variable are subject to SPARK_Mode On.
9597
9598 SPARK_Rules_On :=
9599 Present (Prag)
9600 and then Get_SPARK_Mode_From_Annotation (Prag) = On
9601 and then Is_SPARK_Mode_On_Node (Asmt);
9602
9603 -- Output relevant information when switch -gnatel (info messages on
9604 -- implicit Elaborate[_All] pragmas) is in effect.
9605
9606 if Elab_Info_Messages then
9607 Elab_Msg_NE
9608 (Msg => "assignment to & during elaboration",
9609 N => Asmt,
9610 Id => Var_Id,
9611 Info_Msg => True,
9612 In_SPARK => SPARK_Rules_On);
9613 end if;
9614
9615 -- The SPARK rules are in effect. These rules are applied regardless of
9616 -- whether -gnatd.v (enforce SPARK elaboration rules in SPARK code) is
9617 -- in effect because the static model cannot ensure safe assignment of
9618 -- variables.
9619
9620 if SPARK_Rules_On then
9621 Process_Conditional_ABE_Variable_Assignment_SPARK
9622 (Asmt => Asmt,
9623 Var_Id => Var_Id);
9624
9625 -- Otherwise the Ada rules are in effect
9626
9627 else
9628 Process_Conditional_ABE_Variable_Assignment_Ada
9629 (Asmt => Asmt,
9630 Var_Id => Var_Id);
9631 end if;
9632 end Process_Conditional_ABE_Variable_Assignment;
9633
9634 -----------------------------------------------------
9635 -- Process_Conditional_ABE_Variable_Assignment_Ada --
9636 -----------------------------------------------------
9637
9638 procedure Process_Conditional_ABE_Variable_Assignment_Ada
9639 (Asmt : Node_Id;
9640 Var_Id : Entity_Id)
9641 is
9642 Var_Decl : constant Node_Id := Declaration_Node (Var_Id);
9643 Spec_Id : constant Entity_Id := Find_Top_Unit (Var_Decl);
9644
9645 begin
9646 -- Emit a warning when an uninitialized variable declared in a package
9647 -- spec without a pragma Elaborate_Body is initialized by elaboration
9648 -- code within the corresponding body.
9649
9650 if not Warnings_Off (Var_Id)
9651 and then not Is_Initialized (Var_Decl)
9652 and then not Has_Pragma_Elaborate_Body (Spec_Id)
9653 then
9654 -- Generate an implicit Elaborate_Body in the spec
9655
9656 Set_Elaborate_Body_Desirable (Spec_Id);
9657
9658 Error_Msg_NE
9659 ("??variable & can be accessed by clients before this "
9660 & "initialization", Asmt, Var_Id);
9661
9662 Error_Msg_NE
9663 ("\add pragma ""Elaborate_Body"" to spec & to ensure proper "
9664 & "initialization", Asmt, Spec_Id);
9665
9666 Output_Active_Scenarios (Asmt);
9667 end if;
9668 end Process_Conditional_ABE_Variable_Assignment_Ada;
9669
9670 -------------------------------------------------------
9671 -- Process_Conditional_ABE_Variable_Assignment_SPARK --
9672 -------------------------------------------------------
9673
9674 procedure Process_Conditional_ABE_Variable_Assignment_SPARK
9675 (Asmt : Node_Id;
9676 Var_Id : Entity_Id)
9677 is
9678 Var_Decl : constant Node_Id := Declaration_Node (Var_Id);
9679 Spec_Id : constant Entity_Id := Find_Top_Unit (Var_Decl);
9680
9681 begin
9682 -- Emit an error when an initialized variable declared in a package spec
9683 -- without pragma Elaborate_Body is further modified by elaboration code
9684 -- within the corresponding body.
9685
9686 if Is_Initialized (Var_Decl)
9687 and then not Has_Pragma_Elaborate_Body (Spec_Id)
9688 then
9689 Error_Msg_NE
9690 ("variable & modified by elaboration code in package body",
9691 Asmt, Var_Id);
9692
9693 Error_Msg_NE
9694 ("\add pragma ""Elaborate_Body"" to spec & to ensure full "
9695 & "initialization", Asmt, Spec_Id);
9696
9697 Output_Active_Scenarios (Asmt);
9698 end if;
9699 end Process_Conditional_ABE_Variable_Assignment_SPARK;
9700
9701 ------------------------------------------------
9702 -- Process_Conditional_ABE_Variable_Reference --
9703 ------------------------------------------------
9704
9705 procedure Process_Conditional_ABE_Variable_Reference (Ref : Node_Id) is
9706 Var_Attrs : Variable_Attributes;
9707 Var_Id : Entity_Id;
9708
9709 begin
9710 Extract_Variable_Reference_Attributes
9711 (Ref => Ref,
9712 Var_Id => Var_Id,
9713 Attrs => Var_Attrs);
9714
9715 if Is_Read (Ref) then
9716 Process_Conditional_ABE_Variable_Reference_Read
9717 (Ref => Ref,
9718 Var_Id => Var_Id,
9719 Attrs => Var_Attrs);
9720 end if;
9721 end Process_Conditional_ABE_Variable_Reference;
9722
9723 -----------------------------------------------------
9724 -- Process_Conditional_ABE_Variable_Reference_Read --
9725 -----------------------------------------------------
9726
9727 procedure Process_Conditional_ABE_Variable_Reference_Read
9728 (Ref : Node_Id;
9729 Var_Id : Entity_Id;
9730 Attrs : Variable_Attributes)
9731 is
9732 begin
9733 -- Output relevant information when switch -gnatel (info messages on
9734 -- implicit Elaborate[_All] pragmas) is in effect.
9735
9736 if Elab_Info_Messages then
9737 Elab_Msg_NE
9738 (Msg => "read of variable & during elaboration",
9739 N => Ref,
9740 Id => Var_Id,
9741 Info_Msg => True,
9742 In_SPARK => True);
9743 end if;
9744
9745 -- Nothing to do when the variable appears within the main unit because
9746 -- diagnostics on reads are relevant only for external variables.
9747
9748 if Is_Same_Unit (Attrs.Unit_Id, Cunit_Entity (Main_Unit)) then
9749 null;
9750
9751 -- Nothing to do when the variable is already initialized. Note that the
9752 -- variable may be further modified by the external unit.
9753
9754 elsif Is_Initialized (Declaration_Node (Var_Id)) then
9755 null;
9756
9757 -- Nothing to do when the external unit guarantees the initialization of
9758 -- the variable by means of pragma Elaborate_Body.
9759
9760 elsif Has_Pragma_Elaborate_Body (Attrs.Unit_Id) then
9761 null;
9762
9763 -- A variable read imposes an Elaborate requirement on the context of
9764 -- the main unit. Determine whether the context has a pragma strong
9765 -- enough to meet the requirement.
9766
9767 else
9768 Meet_Elaboration_Requirement
9769 (N => Ref,
9770 Target_Id => Var_Id,
9771 Req_Nam => Name_Elaborate);
9772 end if;
9773 end Process_Conditional_ABE_Variable_Reference_Read;
9774
9775 -----------------------------
9776 -- Process_Conditional_ABE --
9777 -----------------------------
9778
9779 -- NOTE: The body of this routine is intentionally out of order because it
9780 -- invokes an instantiated subprogram (Process_Conditional_ABE_Activation).
9781 -- Placing the body in alphabetical order will result in a guaranteed ABE.
9782
9783 procedure Process_Conditional_ABE
9784 (N : Node_Id;
9785 In_Init_Cond : Boolean := False;
9786 In_Partial_Fin : Boolean := False;
9787 In_Task_Body : Boolean := False)
9788 is
9789 Call_Attrs : Call_Attributes;
9790 Target_Id : Entity_Id;
9791
9792 begin
9793 -- Add the current scenario to the stack of active scenarios
9794
9795 Push_Active_Scenario (N);
9796
9797 -- 'Access
9798
9799 if Is_Suitable_Access (N) then
9800 Process_Conditional_ABE_Access
9801 (Attr => N,
9802 In_Init_Cond => In_Init_Cond,
9803 In_Partial_Fin => In_Partial_Fin,
9804 In_Task_Body => In_Task_Body);
9805
9806 -- Calls
9807
9808 elsif Is_Suitable_Call (N) then
9809
9810 -- In general, only calls found within the main unit are processed
9811 -- because the ALI information supplied to binde is for the main
9812 -- unit only. However, to preserve the consistency of the tree and
9813 -- ensure proper serialization of internal names, external calls
9814 -- also receive corresponding call markers (see Build_Call_Marker).
9815 -- Regardless of the reason, external calls must not be processed.
9816
9817 if In_Main_Context (N) then
9818 Extract_Call_Attributes
9819 (Call => N,
9820 Target_Id => Target_Id,
9821 Attrs => Call_Attrs);
9822
9823 if Is_Activation_Proc (Target_Id) then
9824 Process_Conditional_ABE_Activation
9825 (Call => N,
9826 Call_Attrs => Call_Attrs,
9827 In_Init_Cond => In_Init_Cond,
9828 In_Partial_Fin => In_Partial_Fin,
9829 In_Task_Body => In_Task_Body);
9830
9831 else
9832 Process_Conditional_ABE_Call
9833 (Call => N,
9834 Call_Attrs => Call_Attrs,
9835 Target_Id => Target_Id,
9836 In_Init_Cond => In_Init_Cond,
9837 In_Partial_Fin => In_Partial_Fin,
9838 In_Task_Body => In_Task_Body);
9839 end if;
9840 end if;
9841
9842 -- Instantiations
9843
9844 elsif Is_Suitable_Instantiation (N) then
9845 Process_Conditional_ABE_Instantiation
9846 (Exp_Inst => N,
9847 In_Partial_Fin => In_Partial_Fin,
9848 In_Task_Body => In_Task_Body);
9849
9850 -- Variable assignments
9851
9852 elsif Is_Suitable_Variable_Assignment (N) then
9853 Process_Conditional_ABE_Variable_Assignment (N);
9854
9855 -- Variable references
9856
9857 elsif Is_Suitable_Variable_Reference (N) then
9858
9859 -- In general, only variable references found within the main unit
9860 -- are processed because the ALI information supplied to binde is for
9861 -- the main unit only. However, to preserve the consistency of the
9862 -- tree and ensure proper serialization of internal names, external
9863 -- variable references also receive corresponding variable reference
9864 -- markers (see Build_Varaible_Reference_Marker). Regardless of the
9865 -- reason, external variable references must not be processed.
9866
9867 if In_Main_Context (N) then
9868 Process_Conditional_ABE_Variable_Reference (N);
9869 end if;
9870 end if;
9871
9872 -- Remove the current scenario from the stack of active scenarios once
9873 -- all ABE diagnostics and checks have been performed.
9874
9875 Pop_Active_Scenario (N);
9876 end Process_Conditional_ABE;
9877
9878 --------------------------------------------
9879 -- Process_Guaranteed_ABE_Activation_Impl --
9880 --------------------------------------------
9881
9882 procedure Process_Guaranteed_ABE_Activation_Impl
9883 (Call : Node_Id;
9884 Call_Attrs : Call_Attributes;
9885 Obj_Id : Entity_Id;
9886 Task_Attrs : Task_Attributes;
9887 In_Init_Cond : Boolean;
9888 In_Partial_Fin : Boolean;
9889 In_Task_Body : Boolean)
9890 is
9891 pragma Unreferenced (Call_Attrs);
9892 pragma Unreferenced (In_Init_Cond);
9893 pragma Unreferenced (In_Partial_Fin);
9894 pragma Unreferenced (In_Task_Body);
9895
9896 Check_OK : constant Boolean :=
9897 not Is_Ignored_Ghost_Entity (Obj_Id)
9898 and then not Task_Attrs.Ghost_Mode_Ignore
9899 and then Is_Elaboration_Checks_OK_Id (Obj_Id)
9900 and then Task_Attrs.Elab_Checks_OK;
9901 -- A run-time ABE check may be installed only when the object and the
9902 -- task type have active elaboration checks, and both are not ignored
9903 -- Ghost constructs.
9904
9905 begin
9906 -- Nothing to do when the root scenario appears at the declaration
9907 -- level and the task is in the same unit, but outside this context.
9908
9909 -- task type Task_Typ; -- task declaration
9910
9911 -- procedure Proc is
9912 -- function A ... is
9913 -- begin
9914 -- if Some_Condition then
9915 -- declare
9916 -- T : Task_Typ;
9917 -- begin
9918 -- <activation call> -- activation site
9919 -- end;
9920 -- ...
9921 -- end A;
9922
9923 -- X : ... := A; -- root scenario
9924 -- ...
9925
9926 -- task body Task_Typ is
9927 -- ...
9928 -- end Task_Typ;
9929
9930 -- In the example above, the context of X is the declarative list of
9931 -- Proc. The "elaboration" of X may reach the activation of T whose body
9932 -- is defined outside of X's context. The task body is relevant only
9933 -- when Proc is invoked, but this happens only in "normal" elaboration,
9934 -- therefore the task body must not be considered if this is not the
9935 -- case.
9936
9937 -- Performance note: parent traversal
9938
9939 if Is_Up_Level_Target (Task_Attrs.Task_Decl) then
9940 return;
9941
9942 -- Nothing to do when the activation is ABE-safe
9943
9944 -- generic
9945 -- package Gen is
9946 -- task type Task_Typ;
9947 -- end Gen;
9948
9949 -- package body Gen is
9950 -- task body Task_Typ is
9951 -- begin
9952 -- ...
9953 -- end Task_Typ;
9954 -- end Gen;
9955
9956 -- with Gen;
9957 -- procedure Main is
9958 -- package Nested is
9959 -- ...
9960 -- end Nested;
9961
9962 -- package body Nested is
9963 -- package Inst is new Gen;
9964 -- T : Inst.Task_Typ;
9965 -- [begin]
9966 -- <activation call> -- safe activation
9967 -- end Nested;
9968 -- ...
9969
9970 elsif Is_Safe_Activation (Call, Task_Attrs.Task_Decl) then
9971 return;
9972
9973 -- An activation call leads to a guaranteed ABE when the activation
9974 -- call and the task appear within the same context ignoring library
9975 -- levels, and the body of the task has not been seen yet or appears
9976 -- after the activation call.
9977
9978 -- procedure Guaranteed_ABE is
9979 -- task type Task_Typ;
9980
9981 -- package Nested is
9982 -- ...
9983 -- end Nested;
9984
9985 -- package body Nested is
9986 -- T : Task_Typ;
9987 -- [begin]
9988 -- <activation call> -- guaranteed ABE
9989 -- end Nested;
9990
9991 -- task body Task_Typ is
9992 -- ...
9993 -- end Task_Typ;
9994 -- ...
9995
9996 -- Performance note: parent traversal
9997
9998 elsif Is_Guaranteed_ABE
9999 (N => Call,
10000 Target_Decl => Task_Attrs.Task_Decl,
10001 Target_Body => Task_Attrs.Body_Decl)
10002 then
10003 Error_Msg_Sloc := Sloc (Call);
10004 Error_Msg_N
10005 ("??task & will be activated # before elaboration of its body",
10006 Obj_Id);
10007 Error_Msg_N ("\Program_Error will be raised at run time", Obj_Id);
10008
10009 -- Mark the activation call as a guaranteed ABE
10010
10011 Set_Is_Known_Guaranteed_ABE (Call);
10012
10013 -- Install a run-time ABE failue because this activation call will
10014 -- always result in an ABE.
10015
10016 if Check_OK then
10017 Install_ABE_Failure
10018 (N => Call,
10019 Ins_Nod => Call);
10020 end if;
10021 end if;
10022 end Process_Guaranteed_ABE_Activation_Impl;
10023
10024 procedure Process_Guaranteed_ABE_Activation is
10025 new Process_Activation_Generic (Process_Guaranteed_ABE_Activation_Impl);
10026
10027 ---------------------------------
10028 -- Process_Guaranteed_ABE_Call --
10029 ---------------------------------
10030
10031 procedure Process_Guaranteed_ABE_Call
10032 (Call : Node_Id;
10033 Call_Attrs : Call_Attributes;
10034 Target_Id : Entity_Id)
10035 is
10036 Target_Attrs : Target_Attributes;
10037
10038 begin
10039 Extract_Target_Attributes
10040 (Target_Id => Target_Id,
10041 Attrs => Target_Attrs);
10042
10043 -- Nothing to do when the root scenario appears at the declaration level
10044 -- and the target is in the same unit, but outside this context.
10045
10046 -- function B ...; -- target declaration
10047
10048 -- procedure Proc is
10049 -- function A ... is
10050 -- begin
10051 -- if Some_Condition then
10052 -- return B; -- call site
10053 -- ...
10054 -- end A;
10055
10056 -- X : ... := A; -- root scenario
10057 -- ...
10058
10059 -- function B ... is
10060 -- ...
10061 -- end B;
10062
10063 -- In the example above, the context of X is the declarative region of
10064 -- Proc. The "elaboration" of X may eventually reach B which is defined
10065 -- outside of X's context. B is relevant only when Proc is invoked, but
10066 -- this happens only by means of "normal" elaboration, therefore B must
10067 -- not be considered if this is not the case.
10068
10069 -- Performance note: parent traversal
10070
10071 if Is_Up_Level_Target (Target_Attrs.Spec_Decl) then
10072 return;
10073
10074 -- Nothing to do when the call is ABE-safe
10075
10076 -- generic
10077 -- function Gen ...;
10078
10079 -- function Gen ... is
10080 -- begin
10081 -- ...
10082 -- end Gen;
10083
10084 -- with Gen;
10085 -- procedure Main is
10086 -- function Inst is new Gen;
10087 -- X : ... := Inst; -- safe call
10088 -- ...
10089
10090 elsif Is_Safe_Call (Call, Target_Attrs) then
10091 return;
10092
10093 -- A call leads to a guaranteed ABE when the call and the target appear
10094 -- within the same context ignoring library levels, and the body of the
10095 -- target has not been seen yet or appears after the call.
10096
10097 -- procedure Guaranteed_ABE is
10098 -- function Func ...;
10099
10100 -- package Nested is
10101 -- Obj : ... := Func; -- guaranteed ABE
10102 -- end Nested;
10103
10104 -- function Func ... is
10105 -- ...
10106 -- end Func;
10107 -- ...
10108
10109 -- Performance note: parent traversal
10110
10111 elsif Is_Guaranteed_ABE
10112 (N => Call,
10113 Target_Decl => Target_Attrs.Spec_Decl,
10114 Target_Body => Target_Attrs.Body_Decl)
10115 then
10116 Error_Msg_NE ("??cannot call & before body seen", Call, Target_Id);
10117 Error_Msg_N ("\Program_Error will be raised at run time", Call);
10118
10119 -- Mark the call as a guarnateed ABE
10120
10121 Set_Is_Known_Guaranteed_ABE (Call);
10122
10123 -- Install a run-time ABE failure because the call will always result
10124 -- in an ABE. The failure is installed when both the call and target
10125 -- have enabled elaboration checks, and both are not ignored Ghost
10126 -- constructs.
10127
10128 if Call_Attrs.Elab_Checks_OK
10129 and then Target_Attrs.Elab_Checks_OK
10130 and then not Call_Attrs.Ghost_Mode_Ignore
10131 and then not Target_Attrs.Ghost_Mode_Ignore
10132 then
10133 Install_ABE_Failure
10134 (N => Call,
10135 Ins_Nod => Call);
10136 end if;
10137 end if;
10138 end Process_Guaranteed_ABE_Call;
10139
10140 ------------------------------------------
10141 -- Process_Guaranteed_ABE_Instantiation --
10142 ------------------------------------------
10143
10144 procedure Process_Guaranteed_ABE_Instantiation (Exp_Inst : Node_Id) is
10145 Gen_Attrs : Target_Attributes;
10146 Gen_Id : Entity_Id;
10147 Inst : Node_Id;
10148 Inst_Attrs : Instantiation_Attributes;
10149 Inst_Id : Entity_Id;
10150
10151 begin
10152 Extract_Instantiation_Attributes
10153 (Exp_Inst => Exp_Inst,
10154 Inst => Inst,
10155 Inst_Id => Inst_Id,
10156 Gen_Id => Gen_Id,
10157 Attrs => Inst_Attrs);
10158
10159 Extract_Target_Attributes (Gen_Id, Gen_Attrs);
10160
10161 -- Nothing to do when the root scenario appears at the declaration level
10162 -- and the generic is in the same unit, but outside this context.
10163
10164 -- generic
10165 -- procedure Gen is ...; -- generic declaration
10166
10167 -- procedure Proc is
10168 -- function A ... is
10169 -- begin
10170 -- if Some_Condition then
10171 -- declare
10172 -- procedure I is new Gen; -- instantiation site
10173 -- ...
10174 -- ...
10175 -- end A;
10176
10177 -- X : ... := A; -- root scenario
10178 -- ...
10179
10180 -- procedure Gen is
10181 -- ...
10182 -- end Gen;
10183
10184 -- In the example above, the context of X is the declarative region of
10185 -- Proc. The "elaboration" of X may eventually reach Gen which appears
10186 -- outside of X's context. Gen is relevant only when Proc is invoked,
10187 -- but this happens only by means of "normal" elaboration, therefore
10188 -- Gen must not be considered if this is not the case.
10189
10190 -- Performance note: parent traversal
10191
10192 if Is_Up_Level_Target (Gen_Attrs.Spec_Decl) then
10193 return;
10194
10195 -- Nothing to do when the instantiation is ABE-safe
10196
10197 -- generic
10198 -- package Gen is
10199 -- ...
10200 -- end Gen;
10201
10202 -- package body Gen is
10203 -- ...
10204 -- end Gen;
10205
10206 -- with Gen;
10207 -- procedure Main is
10208 -- package Inst is new Gen (ABE); -- safe instantiation
10209 -- ...
10210
10211 elsif Is_Safe_Instantiation (Inst, Gen_Attrs) then
10212 return;
10213
10214 -- An instantiation leads to a guaranteed ABE when the instantiation and
10215 -- the generic appear within the same context ignoring library levels,
10216 -- and the body of the generic has not been seen yet or appears after
10217 -- the instantiation.
10218
10219 -- procedure Guaranteed_ABE is
10220 -- generic
10221 -- procedure Gen;
10222
10223 -- package Nested is
10224 -- procedure Inst is new Gen; -- guaranteed ABE
10225 -- end Nested;
10226
10227 -- procedure Gen is
10228 -- ...
10229 -- end Gen;
10230 -- ...
10231
10232 -- Performance note: parent traversal
10233
10234 elsif Is_Guaranteed_ABE
10235 (N => Inst,
10236 Target_Decl => Gen_Attrs.Spec_Decl,
10237 Target_Body => Gen_Attrs.Body_Decl)
10238 then
10239 Error_Msg_NE
10240 ("??cannot instantiate & before body seen", Inst, Gen_Id);
10241 Error_Msg_N ("\Program_Error will be raised at run time", Inst);
10242
10243 -- Mark the instantiation as a guarantee ABE. This automatically
10244 -- suppresses the instantiation of the generic body.
10245
10246 Set_Is_Known_Guaranteed_ABE (Inst);
10247
10248 -- Install a run-time ABE failure because the instantiation will
10249 -- always result in an ABE. The failure is installed when both the
10250 -- instance and the generic have enabled elaboration checks, and both
10251 -- are not ignored Ghost constructs.
10252
10253 if Inst_Attrs.Elab_Checks_OK
10254 and then Gen_Attrs.Elab_Checks_OK
10255 and then not Inst_Attrs.Ghost_Mode_Ignore
10256 and then not Gen_Attrs.Ghost_Mode_Ignore
10257 then
10258 Install_ABE_Failure
10259 (N => Inst,
10260 Ins_Nod => Exp_Inst);
10261 end if;
10262 end if;
10263 end Process_Guaranteed_ABE_Instantiation;
10264
10265 ----------------------------
10266 -- Process_Guaranteed_ABE --
10267 ----------------------------
10268
10269 -- NOTE: The body of this routine is intentionally out of order because it
10270 -- invokes an instantiated subprogram (Process_Guaranteed_ABE_Activation).
10271 -- Placing the body in alphabetical order will result in a guaranteed ABE.
10272
10273 procedure Process_Guaranteed_ABE (N : Node_Id) is
10274 Call_Attrs : Call_Attributes;
10275 Target_Id : Entity_Id;
10276
10277 begin
10278 -- Add the current scenario to the stack of active scenarios
10279
10280 Push_Active_Scenario (N);
10281
10282 -- Only calls, instantiations, and task activations may result in a
10283 -- guaranteed ABE.
10284
10285 if Is_Suitable_Call (N) then
10286 Extract_Call_Attributes
10287 (Call => N,
10288 Target_Id => Target_Id,
10289 Attrs => Call_Attrs);
10290
10291 if Is_Activation_Proc (Target_Id) then
10292 Process_Guaranteed_ABE_Activation
10293 (Call => N,
10294 Call_Attrs => Call_Attrs,
10295 In_Init_Cond => False,
10296 In_Partial_Fin => False,
10297 In_Task_Body => False);
10298
10299 else
10300 Process_Guaranteed_ABE_Call
10301 (Call => N,
10302 Call_Attrs => Call_Attrs,
10303 Target_Id => Target_Id);
10304 end if;
10305
10306 elsif Is_Suitable_Instantiation (N) then
10307 Process_Guaranteed_ABE_Instantiation (N);
10308 end if;
10309
10310 -- Remove the current scenario from the stack of active scenarios once
10311 -- all ABE diagnostics and checks have been performed.
10312
10313 Pop_Active_Scenario (N);
10314 end Process_Guaranteed_ABE;
10315
10316 --------------------------
10317 -- Push_Active_Scenario --
10318 --------------------------
10319
10320 procedure Push_Active_Scenario (N : Node_Id) is
10321 begin
10322 Scenario_Stack.Append (N);
10323 end Push_Active_Scenario;
10324
10325 ---------------------------------
10326 -- Record_Elaboration_Scenario --
10327 ---------------------------------
10328
10329 procedure Record_Elaboration_Scenario (N : Node_Id) is
10330 Level : Enclosing_Level_Kind;
10331
10332 Any_Level_OK : Boolean;
10333 -- This flag is set when a particular scenario is allowed to appear at
10334 -- any level.
10335
10336 Declaration_Level_OK : Boolean;
10337 -- This flag is set when a particular scenario is allowed to appear at
10338 -- the declaration level.
10339
10340 Library_Level_OK : Boolean;
10341 -- This flag is set when a particular scenario is allowed to appear at
10342 -- the library level.
10343
10344 begin
10345 -- Assume that the scenario cannot appear on any level
10346
10347 Any_Level_OK := False;
10348 Declaration_Level_OK := False;
10349 Library_Level_OK := False;
10350
10351 -- Nothing to do for ASIS. As a result, no ABE checks and diagnostics
10352 -- are performed in this mode.
10353
10354 if ASIS_Mode then
10355 return;
10356
10357 -- Nothing to do when the scenario is being preanalyzed
10358
10359 elsif Preanalysis_Active then
10360 return;
10361 end if;
10362
10363 -- Ensure that a library-level call does not appear in a preelaborated
10364 -- unit. The check must come before ignoring scenarios within external
10365 -- units or inside generics because calls in those context must also be
10366 -- verified.
10367
10368 if Is_Suitable_Call (N) then
10369 Check_Preelaborated_Call (N);
10370 end if;
10371
10372 -- Nothing to do when the scenario does not appear within the main unit
10373
10374 if not In_Main_Context (N) then
10375 return;
10376
10377 -- Scenarios within a generic unit are never considered because generics
10378 -- cannot be elaborated.
10379
10380 elsif Inside_A_Generic then
10381 return;
10382
10383 -- Scenarios which do not fall in one of the elaboration categories
10384 -- listed below are not considered. The categories are:
10385
10386 -- 'Access for entries, operators, and subprograms
10387 -- Assignments to variables
10388 -- Calls (includes task activation)
10389 -- Derived types
10390 -- Instantiations
10391 -- Pragma Refined_State
10392 -- Reads of variables
10393
10394 elsif Is_Suitable_Access (N) then
10395 Library_Level_OK := True;
10396
10397 -- Signal any enclosing local exception handlers that the 'Access may
10398 -- raise Program_Error due to a failed ABE check when switch -gnatd.o
10399 -- (conservative elaboration order for indirect calls) is in effect.
10400 -- Marking the exception handlers ensures proper expansion by both
10401 -- the front and back end restriction when No_Exception_Propagation
10402 -- is in effect.
10403
10404 if Debug_Flag_Dot_O then
10405 Possible_Local_Raise (N, Standard_Program_Error);
10406 end if;
10407
10408 elsif Is_Suitable_Call (N) or else Is_Suitable_Instantiation (N) then
10409 Declaration_Level_OK := True;
10410 Library_Level_OK := True;
10411
10412 -- Signal any enclosing local exception handlers that the call or
10413 -- instantiation may raise Program_Error due to a failed ABE check.
10414 -- Marking the exception handlers ensures proper expansion by both
10415 -- the front and back end restriction when No_Exception_Propagation
10416 -- is in effect.
10417
10418 Possible_Local_Raise (N, Standard_Program_Error);
10419
10420 elsif Is_Suitable_SPARK_Derived_Type (N) then
10421 Any_Level_OK := True;
10422
10423 elsif Is_Suitable_SPARK_Refined_State_Pragma (N) then
10424 Library_Level_OK := True;
10425
10426 elsif Is_Suitable_Variable_Assignment (N)
10427 or else Is_Suitable_Variable_Reference (N)
10428 then
10429 Library_Level_OK := True;
10430
10431 -- Otherwise the input does not denote a suitable scenario
10432
10433 else
10434 return;
10435 end if;
10436
10437 -- The static model imposes additional restrictions on the placement of
10438 -- scenarios. In contrast, the dynamic model assumes that every scenario
10439 -- will be elaborated or invoked at some point.
10440
10441 if Static_Elaboration_Checks then
10442
10443 -- Certain scenarios are allowed to appear at any level. This check
10444 -- is performed here in order to save on a parent traversal.
10445
10446 if Any_Level_OK then
10447 null;
10448
10449 -- Otherwise the scenario must appear at a specific level
10450
10451 else
10452 -- Performance note: parent traversal
10453
10454 Level := Find_Enclosing_Level (N);
10455
10456 -- Declaration-level scenario
10457
10458 if Declaration_Level_OK and then Level = Declaration_Level then
10459 null;
10460
10461 -- Library-level scenario
10462
10463 elsif Library_Level_OK
10464 and then Level in Library_Or_Instantiation_Level
10465 then
10466 null;
10467
10468 -- Otherwise the scenario does not appear at the proper level and
10469 -- cannot possibly act as a top-level scenario.
10470
10471 else
10472 return;
10473 end if;
10474 end if;
10475 end if;
10476
10477 -- Derived types subject to SPARK_Mode On require elaboration-related
10478 -- checks even though the type may not be declared within elaboration
10479 -- code. The types are recorded in a separate table which is examined
10480 -- during the Processing phase. Note that the checks must be delayed
10481 -- because the bodies of overriding primitives are not available yet.
10482
10483 if Is_Suitable_SPARK_Derived_Type (N) then
10484 Record_SPARK_Elaboration_Scenario (N);
10485
10486 -- Nothing left to do for derived types
10487
10488 return;
10489
10490 -- Instantiations of generics both subject to SPARK_Mode On require
10491 -- elaboration-related checks even though the instantiations may not
10492 -- appear within elaboration code. The instantiations are recored in
10493 -- a separate table which is examined during the Procesing phase. Note
10494 -- that the checks must be delayed because it is not known yet whether
10495 -- the generic unit has a body or not.
10496
10497 -- IMPORTANT: A SPARK instantiation is also a normal instantiation which
10498 -- is subject to common conditional and guaranteed ABE checks.
10499
10500 elsif Is_Suitable_SPARK_Instantiation (N) then
10501 Record_SPARK_Elaboration_Scenario (N);
10502
10503 -- External constituents that refine abstract states which appear in
10504 -- pragma Initializes require elaboration-related checks even though
10505 -- a Refined_State pragma lacks any elaboration semantic.
10506
10507 elsif Is_Suitable_SPARK_Refined_State_Pragma (N) then
10508 Record_SPARK_Elaboration_Scenario (N);
10509
10510 -- Nothing left to do for pragma Refined_State
10511
10512 return;
10513 end if;
10514
10515 -- Perform early detection of guaranteed ABEs in order to suppress the
10516 -- instantiation of generic bodies as gigi cannot handle certain types
10517 -- of premature instantiations.
10518
10519 Process_Guaranteed_ABE (N);
10520
10521 -- At this point all checks have been performed. Record the scenario for
10522 -- later processing by the ABE phase.
10523
10524 Top_Level_Scenarios.Append (N);
10525 Set_Is_Recorded_Top_Level_Scenario (N);
10526 end Record_Elaboration_Scenario;
10527
10528 ---------------------------------------
10529 -- Record_SPARK_Elaboration_Scenario --
10530 ---------------------------------------
10531
10532 procedure Record_SPARK_Elaboration_Scenario (N : Node_Id) is
10533 begin
10534 SPARK_Scenarios.Append (N);
10535 Set_Is_Recorded_SPARK_Scenario (N);
10536 end Record_SPARK_Elaboration_Scenario;
10537
10538 -----------------------------------
10539 -- Recorded_SPARK_Scenarios_Hash --
10540 -----------------------------------
10541
10542 function Recorded_SPARK_Scenarios_Hash
10543 (Key : Node_Id) return Recorded_SPARK_Scenarios_Index
10544 is
10545 begin
10546 return
10547 Recorded_SPARK_Scenarios_Index (Key mod Recorded_SPARK_Scenarios_Max);
10548 end Recorded_SPARK_Scenarios_Hash;
10549
10550 ---------------------------------------
10551 -- Recorded_Top_Level_Scenarios_Hash --
10552 ---------------------------------------
10553
10554 function Recorded_Top_Level_Scenarios_Hash
10555 (Key : Node_Id) return Recorded_Top_Level_Scenarios_Index
10556 is
10557 begin
10558 return
10559 Recorded_Top_Level_Scenarios_Index
10560 (Key mod Recorded_Top_Level_Scenarios_Max);
10561 end Recorded_Top_Level_Scenarios_Hash;
10562
10563 --------------------------
10564 -- Reset_Visited_Bodies --
10565 --------------------------
10566
10567 procedure Reset_Visited_Bodies is
10568 begin
10569 if Visited_Bodies_In_Use then
10570 Visited_Bodies_In_Use := False;
10571 Visited_Bodies.Reset;
10572 end if;
10573 end Reset_Visited_Bodies;
10574
10575 -------------------
10576 -- Root_Scenario --
10577 -------------------
10578
10579 function Root_Scenario return Node_Id is
10580 package Stack renames Scenario_Stack;
10581
10582 begin
10583 -- Ensure that the scenario stack has at least one active scenario in
10584 -- it. The one at the bottom (index First) is the root scenario.
10585
10586 pragma Assert (Stack.Last >= Stack.First);
10587 return Stack.Table (Stack.First);
10588 end Root_Scenario;
10589
10590 ---------------------------
10591 -- Set_Early_Call_Region --
10592 ---------------------------
10593
10594 procedure Set_Early_Call_Region (Body_Id : Entity_Id; Start : Node_Id) is
10595 begin
10596 pragma Assert (Ekind_In (Body_Id, E_Entry,
10597 E_Entry_Family,
10598 E_Function,
10599 E_Procedure,
10600 E_Subprogram_Body));
10601
10602 Early_Call_Regions_In_Use := True;
10603 Early_Call_Regions.Set (Body_Id, Start);
10604 end Set_Early_Call_Region;
10605
10606 ----------------------------
10607 -- Set_Elaboration_Status --
10608 ----------------------------
10609
10610 procedure Set_Elaboration_Status
10611 (Unit_Id : Entity_Id;
10612 Val : Elaboration_Attributes)
10613 is
10614 begin
10615 Elaboration_Statuses_In_Use := True;
10616 Elaboration_Statuses.Set (Unit_Id, Val);
10617 end Set_Elaboration_Status;
10618
10619 ------------------------------------
10620 -- Set_Is_Recorded_SPARK_Scenario --
10621 ------------------------------------
10622
10623 procedure Set_Is_Recorded_SPARK_Scenario
10624 (N : Node_Id;
10625 Val : Boolean := True)
10626 is
10627 begin
10628 Recorded_SPARK_Scenarios_In_Use := True;
10629 Recorded_SPARK_Scenarios.Set (N, Val);
10630 end Set_Is_Recorded_SPARK_Scenario;
10631
10632 ----------------------------------------
10633 -- Set_Is_Recorded_Top_Level_Scenario --
10634 ----------------------------------------
10635
10636 procedure Set_Is_Recorded_Top_Level_Scenario
10637 (N : Node_Id;
10638 Val : Boolean := True)
10639 is
10640 begin
10641 Recorded_Top_Level_Scenarios_In_Use := True;
10642 Recorded_Top_Level_Scenarios.Set (N, Val);
10643 end Set_Is_Recorded_Top_Level_Scenario;
10644
10645 -------------------------
10646 -- Set_Is_Visited_Body --
10647 -------------------------
10648
10649 procedure Set_Is_Visited_Body (Subp_Body : Node_Id) is
10650 begin
10651 Visited_Bodies_In_Use := True;
10652 Visited_Bodies.Set (Subp_Body, True);
10653 end Set_Is_Visited_Body;
10654
10655 -------------------------------
10656 -- Static_Elaboration_Checks --
10657 -------------------------------
10658
10659 function Static_Elaboration_Checks return Boolean is
10660 begin
10661 return not Dynamic_Elaboration_Checks;
10662 end Static_Elaboration_Checks;
10663
10664 -------------------
10665 -- Traverse_Body --
10666 -------------------
10667
10668 procedure Traverse_Body
10669 (N : Node_Id;
10670 In_Init_Cond : Boolean;
10671 In_Partial_Fin : Boolean;
10672 In_Task_Body : Boolean)
10673 is
10674 procedure Find_And_Process_Nested_Scenarios;
10675 pragma Inline (Find_And_Process_Nested_Scenarios);
10676 -- Examine the declarations and statements of subprogram body N for
10677 -- suitable scenarios. Save each discovered scenario and process it
10678 -- accordingly.
10679
10680 procedure Process_Nested_Scenarios (Nested : Elist_Id);
10681 pragma Inline (Process_Nested_Scenarios);
10682 -- Invoke Process_Conditional_ABE on each individual scenario found in
10683 -- list Nested.
10684
10685 ---------------------------------------
10686 -- Find_And_Process_Nested_Scenarios --
10687 ---------------------------------------
10688
10689 procedure Find_And_Process_Nested_Scenarios is
10690 Body_Id : constant Entity_Id := Defining_Entity (N);
10691
10692 function Is_Potential_Scenario
10693 (Nod : Node_Id) return Traverse_Result;
10694 -- Determine whether arbitrary node Nod denotes a suitable scenario.
10695 -- If it does, save it in the Nested_Scenarios list of the subprogram
10696 -- body, and process it.
10697
10698 procedure Save_Scenario (Nod : Node_Id);
10699 pragma Inline (Save_Scenario);
10700 -- Save scenario Nod in the Nested_Scenarios list of the subprogram
10701 -- body.
10702
10703 procedure Traverse_List (List : List_Id);
10704 pragma Inline (Traverse_List);
10705 -- Invoke Traverse_Potential_Scenarios on each node in list List
10706
10707 procedure Traverse_Potential_Scenarios is
10708 new Traverse_Proc (Is_Potential_Scenario);
10709
10710 ---------------------------
10711 -- Is_Potential_Scenario --
10712 ---------------------------
10713
10714 function Is_Potential_Scenario
10715 (Nod : Node_Id) return Traverse_Result
10716 is
10717 begin
10718 -- Special cases
10719
10720 -- Skip constructs which do not have elaboration of their own and
10721 -- need to be elaborated by other means such as invocation, task
10722 -- activation, etc.
10723
10724 if Is_Non_Library_Level_Encapsulator (Nod) then
10725 return Skip;
10726
10727 -- Terminate the traversal of a task body with an accept statement
10728 -- when no entry calls in elaboration are allowed because the task
10729 -- will block at run-time and the remaining statements will not be
10730 -- executed.
10731
10732 elsif Nkind_In (Original_Node (Nod), N_Accept_Statement,
10733 N_Selective_Accept)
10734 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
10735 then
10736 return Abandon;
10737
10738 -- Certain nodes carry semantic lists which act as repositories
10739 -- until expansion transforms the node and relocates the contents.
10740 -- Examine these lists in case expansion is disabled.
10741
10742 elsif Nkind_In (Nod, N_And_Then, N_Or_Else) then
10743 Traverse_List (Actions (Nod));
10744
10745 elsif Nkind_In (Nod, N_Elsif_Part, N_Iteration_Scheme) then
10746 Traverse_List (Condition_Actions (Nod));
10747
10748 elsif Nkind (Nod) = N_If_Expression then
10749 Traverse_List (Then_Actions (Nod));
10750 Traverse_List (Else_Actions (Nod));
10751
10752 elsif Nkind_In (Nod, N_Component_Association,
10753 N_Iterated_Component_Association)
10754 then
10755 Traverse_List (Loop_Actions (Nod));
10756
10757 -- General case
10758
10759 -- Save a suitable scenario in the Nested_Scenarios list of the
10760 -- subprogram body. As a result any subsequent traversals of the
10761 -- subprogram body started from a different top-level scenario no
10762 -- longer need to reexamine the tree.
10763
10764 elsif Is_Suitable_Scenario (Nod) then
10765 Save_Scenario (Nod);
10766
10767 Process_Conditional_ABE
10768 (N => Nod,
10769 In_Init_Cond => In_Init_Cond,
10770 In_Partial_Fin => In_Partial_Fin,
10771 In_Task_Body => In_Task_Body);
10772 end if;
10773
10774 return OK;
10775 end Is_Potential_Scenario;
10776
10777 -------------------
10778 -- Save_Scenario --
10779 -------------------
10780
10781 procedure Save_Scenario (Nod : Node_Id) is
10782 Nested : Elist_Id;
10783
10784 begin
10785 Nested := Nested_Scenarios (Body_Id);
10786
10787 if No (Nested) then
10788 Nested := New_Elmt_List;
10789 Set_Nested_Scenarios (Body_Id, Nested);
10790 end if;
10791
10792 Append_Elmt (Nod, Nested);
10793 end Save_Scenario;
10794
10795 -------------------
10796 -- Traverse_List --
10797 -------------------
10798
10799 procedure Traverse_List (List : List_Id) is
10800 Item : Node_Id;
10801
10802 begin
10803 Item := First (List);
10804 while Present (Item) loop
10805 Traverse_Potential_Scenarios (Item);
10806 Next (Item);
10807 end loop;
10808 end Traverse_List;
10809
10810 -- Start of processing for Find_And_Process_Nested_Scenarios
10811
10812 begin
10813 -- Examine the declarations for suitable scenarios
10814
10815 Traverse_List (Declarations (N));
10816
10817 -- Examine the handled sequence of statements. This also includes any
10818 -- exceptions handlers.
10819
10820 Traverse_Potential_Scenarios (Handled_Statement_Sequence (N));
10821 end Find_And_Process_Nested_Scenarios;
10822
10823 ------------------------------
10824 -- Process_Nested_Scenarios --
10825 ------------------------------
10826
10827 procedure Process_Nested_Scenarios (Nested : Elist_Id) is
10828 Nested_Elmt : Elmt_Id;
10829
10830 begin
10831 Nested_Elmt := First_Elmt (Nested);
10832 while Present (Nested_Elmt) loop
10833 Process_Conditional_ABE
10834 (N => Node (Nested_Elmt),
10835 In_Init_Cond => In_Init_Cond,
10836 In_Partial_Fin => In_Partial_Fin,
10837 In_Task_Body => In_Task_Body);
10838
10839 Next_Elmt (Nested_Elmt);
10840 end loop;
10841 end Process_Nested_Scenarios;
10842
10843 -- Local variables
10844
10845 Nested : Elist_Id;
10846
10847 -- Start of processing for Traverse_Body
10848
10849 begin
10850 -- Nothing to do when there is no body
10851
10852 if No (N) then
10853 return;
10854
10855 elsif Nkind (N) /= N_Subprogram_Body then
10856 return;
10857 end if;
10858
10859 -- Nothing to do if the body was already traversed during the processing
10860 -- of the same top-level scenario.
10861
10862 if Is_Visited_Body (N) then
10863 return;
10864
10865 -- Otherwise mark the body as traversed
10866
10867 else
10868 Set_Is_Visited_Body (N);
10869 end if;
10870
10871 Nested := Nested_Scenarios (Defining_Entity (N));
10872
10873 -- The subprogram body was already examined as part of the elaboration
10874 -- graph starting from a different top-level scenario. There is no need
10875 -- to traverse the declarations and statements again because this will
10876 -- yield the exact same scenarios. Use the nested scenarios collected
10877 -- during the first inspection of the body.
10878
10879 if Present (Nested) then
10880 Process_Nested_Scenarios (Nested);
10881
10882 -- Otherwise examine the declarations and statements of the subprogram
10883 -- body for suitable scenarios, save and process them accordingly.
10884
10885 else
10886 Find_And_Process_Nested_Scenarios;
10887 end if;
10888 end Traverse_Body;
10889
10890 ---------------------------------
10891 -- Update_Elaboration_Scenario --
10892 ---------------------------------
10893
10894 procedure Update_Elaboration_Scenario (New_N : Node_Id; Old_N : Node_Id) is
10895 procedure Update_SPARK_Scenario;
10896 pragma Inline (Update_SPARK_Scenario);
10897 -- Update the contents of table SPARK_Scenarios if Old_N is recorded
10898 -- there.
10899
10900 procedure Update_Top_Level_Scenario;
10901 pragma Inline (Update_Top_Level_Scenario);
10902 -- Update the contexts of table Top_Level_Scenarios if Old_N is recorded
10903 -- there.
10904
10905 ---------------------------
10906 -- Update_SPARK_Scenario --
10907 ---------------------------
10908
10909 procedure Update_SPARK_Scenario is
10910 package Scenarios renames SPARK_Scenarios;
10911
10912 begin
10913 if Is_Recorded_SPARK_Scenario (Old_N) then
10914
10915 -- Performance note: list traversal
10916
10917 for Index in Scenarios.First .. Scenarios.Last loop
10918 if Scenarios.Table (Index) = Old_N then
10919 Scenarios.Table (Index) := New_N;
10920
10921 -- The old SPARK scenario is no longer recorded, but the new
10922 -- one is.
10923
10924 Set_Is_Recorded_Top_Level_Scenario (Old_N, False);
10925 Set_Is_Recorded_Top_Level_Scenario (New_N);
10926 return;
10927 end if;
10928 end loop;
10929
10930 -- A recorded SPARK scenario must be in the table of recorded
10931 -- SPARK scenarios.
10932
10933 pragma Assert (False);
10934 end if;
10935 end Update_SPARK_Scenario;
10936
10937 -------------------------------
10938 -- Update_Top_Level_Scenario --
10939 -------------------------------
10940
10941 procedure Update_Top_Level_Scenario is
10942 package Scenarios renames Top_Level_Scenarios;
10943
10944 begin
10945 if Is_Recorded_Top_Level_Scenario (Old_N) then
10946
10947 -- Performance note: list traversal
10948
10949 for Index in Scenarios.First .. Scenarios.Last loop
10950 if Scenarios.Table (Index) = Old_N then
10951 Scenarios.Table (Index) := New_N;
10952
10953 -- The old top-level scenario is no longer recorded, but the
10954 -- new one is.
10955
10956 Set_Is_Recorded_Top_Level_Scenario (Old_N, False);
10957 Set_Is_Recorded_Top_Level_Scenario (New_N);
10958 return;
10959 end if;
10960 end loop;
10961
10962 -- A recorded top-level scenario must be in the table of recorded
10963 -- top-level scenarios.
10964
10965 pragma Assert (False);
10966 end if;
10967 end Update_Top_Level_Scenario;
10968
10969 -- Start of processing for Update_Elaboration_Requirement
10970
10971 begin
10972 -- Nothing to do when the old and new scenarios are one and the same
10973
10974 if Old_N = New_N then
10975 return;
10976
10977 -- A scenario is being transformed by Atree.Rewrite. Update all relevant
10978 -- internal data structures to reflect this change. This ensures that a
10979 -- potential run-time conditional ABE check or a guaranteed ABE failure
10980 -- is inserted at the proper place in the tree.
10981
10982 elsif Is_Scenario (Old_N) then
10983 Update_SPARK_Scenario;
10984 Update_Top_Level_Scenario;
10985 end if;
10986 end Update_Elaboration_Scenario;
10987
10988 -------------------------
10989 -- Visited_Bodies_Hash --
10990 -------------------------
10991
10992 function Visited_Bodies_Hash (Key : Node_Id) return Visited_Bodies_Index is
10993 begin
10994 return Visited_Bodies_Index (Key mod Visited_Bodies_Max);
10995 end Visited_Bodies_Hash;
10996
10997 end Sem_Elab;
This page took 0.495166 seconds and 4 git commands to generate.