]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_elab.adb
714a9f7221fcba7ca2ffc3cebe078e1a901e6e60
[thirdparty/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-2019, 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 ALI; use ALI;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Expander; use Expander;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Output; use Output;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Prag; use Sem_Prag;
54 with Sem_Util; use Sem_Util;
55 with Sinfo; use Sinfo;
56 with Sinput; use Sinput;
57 with Snames; use Snames;
58 with Stand; use Stand;
59 with Table;
60 with Tbuild; use Tbuild;
61 with Uintp; use Uintp;
62 with Uname; use Uname;
63
64 with GNAT; use GNAT;
65 with GNAT.Dynamic_HTables; use GNAT.Dynamic_HTables;
66 with GNAT.Lists; use GNAT.Lists;
67 with GNAT.Sets; use GNAT.Sets;
68
69 package body Sem_Elab is
70
71 -----------------------------------------
72 -- Access-before-elaboration mechanism --
73 -----------------------------------------
74
75 -- The access-before-elaboration (ABE) mechanism implemented in this unit
76 -- has the following objectives:
77 --
78 -- * Diagnose at compile-time or install run-time checks to prevent ABE
79 -- access to data and behaviour.
80 --
81 -- The high-level idea is to accurately diagnose ABE issues within a
82 -- single unit because the ABE mechanism can inspect the whole unit.
83 -- As soon as the elaboration graph extends to an external unit, the
84 -- diagnostics stop because the body of the unit may not be available.
85 -- Due to control and data flow, the ABE mechanism cannot accurately
86 -- determine whether a particular scenario will be elaborated or not.
87 -- Conditional ABE checks are therefore used to verify the elaboration
88 -- status of local and external targets at run time.
89 --
90 -- * Supply implicit elaboration dependencies for a unit to binde
91 --
92 -- The ABE mechanism creates implicit dependencies in the form of with
93 -- clauses subject to pragma Elaborate[_All] when the elaboration graph
94 -- reaches into an external unit. The implicit dependencies are encoded
95 -- in the ALI file of the main unit. GNATbind and binde then use these
96 -- dependencies to augment the library item graph and determine the
97 -- elaboration order of all units in the compilation.
98 --
99 -- * Supply pieces of the invocation graph for a unit to bindo
100 --
101 -- The ABE mechanism captures paths starting from elaboration code or
102 -- top level constructs that reach into an external unit. The paths are
103 -- encoded in the ALI file of the main unit in the form of declarations
104 -- which represent nodes, and relations which represent edges. GNATbind
105 -- and bindo then build the full invocation graph in order to augment
106 -- the library item graph and determine the elaboration order of all
107 -- units in the compilation.
108 --
109 -- The ABE mechanism supports three models of elaboration:
110 --
111 -- * Dynamic model - This is the most permissive of the three models.
112 -- When the dynamic model is in effect, the mechanism diagnoses and
113 -- installs run-time checks to detect ABE issues in the main unit.
114 -- The behaviour of this model is identical to that specified by the
115 -- Ada RM. This model is enabled with switch -gnatE.
116 --
117 -- Static model - This is the middle ground of the three models. When
118 -- the static model is in effect, the mechanism diagnoses and installs
119 -- run-time checks to detect ABE issues in the main unit. In addition,
120 -- the mechanism generates implicit dependencies between units in the
121 -- form of with clauses subject to pragma Elaborate[_All] to ensure
122 -- the prior elaboration of withed units. This is the default model.
123 --
124 -- * SPARK model - This is the most conservative of the three models and
125 -- impelements the semantics defined in SPARK RM 7.7. The SPARK model
126 -- is in effect only when a context resides in a SPARK_Mode On region,
127 -- otherwise the mechanism falls back to one of the previous models.
128 --
129 -- The ABE mechanism consists of a "recording" phase and a "processing"
130 -- phase.
131
132 -----------------
133 -- Terminology --
134 -----------------
135
136 -- * ABE - An attempt to invoke a scenario which has not been elaborated
137 -- yet.
138 --
139 -- * Bridge target - A type of target. A bridge target is a link between
140 -- scenarios. It is usually a byproduct of expansion and does not have
141 -- any direct ABE ramifications.
142 --
143 -- * Call marker - A special node used to indicate the presence of a call
144 -- in the tree in case expansion transforms or eliminates the original
145 -- call. N_Call_Marker nodes do not have static and run-time semantics.
146 --
147 -- * Conditional ABE - A type of ABE. A conditional ABE occurs when the
148 -- invocation of a target by a scenario within the main unit causes an
149 -- ABE, but does not cause an ABE for another scenarios within the main
150 -- unit.
151 --
152 -- * Declaration level - A type of enclosing level. A scenario or target is
153 -- at the declaration level when it appears within the declarations of a
154 -- block statement, entry body, subprogram body, or task body, ignoring
155 -- enclosing packages.
156 --
157 -- * Early call region - A section of code which ends at a subprogram body
158 -- and starts from the nearest non-preelaborable construct which precedes
159 -- the subprogram body. The early call region extends from a package body
160 -- to a package spec when the spec carries pragma Elaborate_Body.
161 --
162 -- * Generic library level - A type of enclosing level. A scenario or
163 -- target is at the generic library level if it appears in a generic
164 -- package library unit, ignoring enclosing packages.
165 --
166 -- * Guaranteed ABE - A type of ABE. A guaranteed ABE occurs when the
167 -- invocation of a target by all scenarios within the main unit causes
168 -- an ABE.
169 --
170 -- * Instantiation library level - A type of enclosing level. A scenario
171 -- or target is at the instantiation library level if it appears in an
172 -- instantiation library unit, ignoring enclosing packages.
173 --
174 -- * Invocation - The act of activating a task, calling a subprogram, or
175 -- instantiating a generic.
176 --
177 -- * Invocation construct - An entry declaration, [single] protected type,
178 -- subprogram declaration, subprogram instantiation, or a [single] task
179 -- type declared in the visible, private, or body declarations of the
180 -- main unit.
181 --
182 -- * Invocation relation - A flow link between two invocation constructs
183 --
184 -- * Invocation signature - A set of attributes that uniquely identify an
185 -- invocation construct within the namespace of all ALI files.
186 --
187 -- * Library level - A type of enclosing level. A scenario or target is at
188 -- the library level if it appears in a package library unit, ignoring
189 -- enclosng packages.
190 --
191 -- * Non-library-level encapsulator - A construct that cannot be elaborated
192 -- on its own and requires elaboration by a top-level scenario.
193 --
194 -- * Scenario - A construct or context which is invoked by elaboration code
195 -- or invocation construct. The scenarios recognized by the ABE mechanism
196 -- are as follows:
197 --
198 -- - '[Unrestricted_]Access of entries, operators, and subprograms
199 --
200 -- - Assignments to variables
201 --
202 -- - Calls to entries, operators, and subprograms
203 --
204 -- - Derived type declarations
205 --
206 -- - Instantiations
207 --
208 -- - Pragma Refined_State
209 --
210 -- - Reads of variables
211 --
212 -- - Task activation
213 --
214 -- * Target - A construct invoked by a scenario. The targets recognized by
215 -- the ABE mechanism are as follows:
216 --
217 -- - For '[Unrestricted_]Access of entries, operators, and subprograms,
218 -- the target is the entry, operator, or subprogram.
219 --
220 -- - For assignments to variables, the target is the variable
221 --
222 -- - For calls, the target is the entry, operator, or subprogram
223 --
224 -- - For derived type declarations, the target is the derived type
225 --
226 -- - For instantiations, the target is the generic template
227 --
228 -- - For pragma Refined_State, the targets are the constituents
229 --
230 -- - For reads of variables, the target is the variable
231 --
232 -- - For task activation, the target is the task body
233
234 ------------------
235 -- Architecture --
236 ------------------
237
238 -- Analysis/Resolution
239 -- |
240 -- +- Build_Call_Marker
241 -- |
242 -- +- Build_Variable_Reference_Marker
243 -- |
244 -- +- | -------------------- Recording phase ---------------------------+
245 -- | v |
246 -- | Record_Elaboration_Scenario |
247 -- | | |
248 -- | +--> Check_Preelaborated_Call |
249 -- | | |
250 -- | +--> Process_Guaranteed_ABE |
251 -- | | | |
252 -- | | +--> Process_Guaranteed_ABE_Activation |
253 -- | | +--> Process_Guaranteed_ABE_Call |
254 -- | | +--> Process_Guaranteed_ABE_Instantiation |
255 -- | | |
256 -- +- | ----------------------------------------------------------------+
257 -- |
258 -- |
259 -- +--> Internal_Representation
260 -- |
261 -- +--> Scenario_Storage
262 -- |
263 -- End of Compilation
264 -- |
265 -- +- | --------------------- Processing phase -------------------------+
266 -- | v |
267 -- | Check_Elaboration_Scenarios |
268 -- | | |
269 -- | +--> Check_Conditional_ABE_Scenarios |
270 -- | | | |
271 -- | | +--> Process_Conditional_ABE <----------------------+ |
272 -- | | | | |
273 -- | | +--> Process_Conditional_ABE_Activation | |
274 -- | | | | | |
275 -- | | | +-----------------------------+ | |
276 -- | | | | | |
277 -- | | +--> Process_Conditional_ABE_Call +---> Traverse_Body |
278 -- | | | | | |
279 -- | | | +-----------------------------+ |
280 -- | | | |
281 -- | | +--> Process_Conditional_ABE_Access_Taken |
282 -- | | +--> Process_Conditional_ABE_Instantiation |
283 -- | | +--> Process_Conditional_ABE_Variable_Assignment |
284 -- | | +--> Process_Conditional_ABE_Variable_Reference |
285 -- | | |
286 -- | +--> Check_SPARK_Scenario |
287 -- | | | |
288 -- | | +--> Process_SPARK_Scenario |
289 -- | | | |
290 -- | | +--> Process_SPARK_Derived_Type |
291 -- | | +--> Process_SPARK_Instantiation |
292 -- | | +--> Process_SPARK_Refined_State_Pragma |
293 -- | | |
294 -- | +--> Record_Invocation_Graph |
295 -- | | |
296 -- | +--> Process_Invocation_Body_Scenarios |
297 -- | +--> Process_Invocation_Spec_Scenarios |
298 -- | +--> Process_Main_Unit |
299 -- | | |
300 -- | +--> Process_Invocation_Scenario <-------------+ |
301 -- | | | |
302 -- | +--> Process_Invocation_Activation | |
303 -- | | | | |
304 -- | | +------------------------+ | |
305 -- | | | | |
306 -- | +--> Process_Invocation_Call +---> Traverse_Body |
307 -- | | | |
308 -- | +------------------------+ |
309 -- | |
310 -- +--------------------------------------------------------------------+
311
312 ---------------------
313 -- Recording phase --
314 ---------------------
315
316 -- The Recording phase coincides with the analysis/resolution phase of the
317 -- compiler. It has the following objectives:
318 --
319 -- * Record all suitable scenarios for examination by the Processing
320 -- phase.
321 --
322 -- Saving only a certain number of nodes improves the performance of
323 -- the ABE mechanism. This eliminates the need to examine the whole
324 -- tree in a separate pass.
325 --
326 -- * Record certain SPARK scenarios which are not necessarily invoked
327 -- during elaboration, but still require elaboration-related checks.
328 --
329 -- Saving only a certain number of nodes improves the performance of
330 -- the ABE mechanism. This eliminates the need to examine the whole
331 -- tree in a separate pass.
332 --
333 -- * Detect and diagnose calls in preelaborable or pure units, including
334 -- generic bodies.
335 --
336 -- This diagnostic is carried out during the Recording phase because it
337 -- does not need the heavy recursive traversal done by the Processing
338 -- phase.
339 --
340 -- * Detect and diagnose guaranteed ABEs caused by instantiations, calls,
341 -- and task activation.
342 --
343 -- The issues detected by the ABE mechanism are reported as warnings
344 -- because they do not violate Ada semantics. Forward instantiations
345 -- may thus reach gigi, however gigi cannot handle certain kinds of
346 -- premature instantiations and may crash. To avoid this limitation,
347 -- the ABE mechanism must identify forward instantiations as early as
348 -- possible and suppress their bodies. Calls and task activations are
349 -- included in this category for completeness.
350
351 ----------------------
352 -- Processing phase --
353 ----------------------
354
355 -- The Processing phase is a separate pass which starts after instantiating
356 -- and/or inlining of bodies, but before the removal of Ghost code. It has
357 -- the following objectives:
358 --
359 -- * Examine all scenarios saved during the Recording phase, and perform
360 -- the following actions:
361 --
362 -- - Dynamic model
363 --
364 -- Diagnose conditional ABEs, and install run-time conditional ABE
365 -- checks for all scenarios.
366 --
367 -- - SPARK model
368 --
369 -- Enforce the SPARK elaboration rules
370 --
371 -- - Static model
372 --
373 -- Diagnose conditional ABEs, install run-time conditional ABE
374 -- checks only for scenarios are reachable from elaboration code,
375 -- and guarantee the elaboration of external units by creating
376 -- implicit with clauses subject to pragma Elaborate[_All].
377 --
378 -- * Examine library-level scenarios and invocation constructs, and
379 -- perform the following actions:
380 --
381 -- - Determine whether the flow of execution reaches into an external
382 -- unit. If this is the case, encode the path in the ALI file of
383 -- the main unit.
384 --
385 -- - Create declarations for invocation constructs in the ALI file of
386 -- the main unit.
387
388 ----------------------
389 -- Important points --
390 ----------------------
391
392 -- The Processing phase starts after the analysis, resolution, expansion
393 -- phase has completed. As a result, no current semantic information is
394 -- available. The scope stack is empty, global flags such as In_Instance
395 -- or Inside_A_Generic become useless. To remedy this, the ABE mechanism
396 -- must either save or recompute semantic information.
397 --
398 -- Expansion heavily transforms calls and to some extent instantiations. To
399 -- remedy this, the ABE mechanism generates N_Call_Marker nodes in order to
400 -- capture the target and relevant attributes of the original call.
401 --
402 -- The diagnostics of the ABE mechanism depend on accurate source locations
403 -- to determine the spacial relation of nodes.
404
405 -----------------------------------------
406 -- Suppression of elaboration warnings --
407 -----------------------------------------
408
409 -- Elaboration warnings along multiple traversal paths rooted at a scenario
410 -- are suppressed when the scenario has elaboration warnings suppressed.
411 --
412 -- Root scenario
413 -- |
414 -- +-- Child scenario 1
415 -- | |
416 -- | +-- Grandchild scenario 1
417 -- | |
418 -- | +-- Grandchild scenario N
419 -- |
420 -- +-- Child scenario N
421 --
422 -- If the root scenario has elaboration warnings suppressed, then all its
423 -- child, grandchild, etc. scenarios will have their elaboration warnings
424 -- suppressed.
425 --
426 -- In addition to switch -gnatwL, pragma Warnings may be used to suppress
427 -- elaboration-related warnings when used in the following manner:
428 --
429 -- pragma Warnings ("L");
430 -- <scenario-or-target>
431 --
432 -- <target>
433 -- pragma Warnings (Off, target);
434 --
435 -- pragma Warnings (Off);
436 -- <scenario-or-target>
437 --
438 -- * To suppress elaboration warnings for '[Unrestricted_]Access of
439 -- entries, operators, and subprograms, either:
440 --
441 -- - Suppress the entry, operator, or subprogram, or
442 -- - Suppress the attribute, or
443 -- - Use switch -gnatw.f
444 --
445 -- * To suppress elaboration warnings for calls to entries, operators,
446 -- and subprograms, either:
447 --
448 -- - Suppress the entry, operator, or subprogram, or
449 -- - Suppress the call
450 --
451 -- * To suppress elaboration warnings for instantiations, suppress the
452 -- instantiation.
453 --
454 -- * To suppress elaboration warnings for task activations, either:
455 --
456 -- - Suppress the task object, or
457 -- - Suppress the task type, or
458 -- - Suppress the activation call
459
460 --------------
461 -- Switches --
462 --------------
463
464 -- The following switches may be used to control the behavior of the ABE
465 -- mechanism.
466 --
467 -- -gnatd_a stop elaboration checks on accept or select statement
468 --
469 -- The ABE mechanism stops the traversal of a task body when it
470 -- encounters an accept or a select statement. This behavior is
471 -- equivalent to restriction No_Entry_Calls_In_Elaboration_Code,
472 -- but without penalizing actual entry calls during elaboration.
473 --
474 -- -gnatd_e ignore entry calls and requeue statements for elaboration
475 --
476 -- The ABE mechanism does not generate N_Call_Marker nodes for
477 -- protected or task entry calls as well as requeue statements.
478 -- As a result, the calls and requeues are not recorded or
479 -- processed.
480 --
481 -- -gnatdE elaboration checks on predefined units
482 --
483 -- The ABE mechanism considers scenarios which appear in internal
484 -- units (Ada, GNAT, Interfaces, System).
485 --
486 -- -gnatd_F encode full invocation paths in ALI files
487 --
488 -- The ABE mechanism encodes the full path from an elaboration
489 -- procedure or invocable construct to an external target. The
490 -- path contains all intermediate activations, instantiations,
491 -- and calls.
492 --
493 -- -gnatd.G ignore calls through generic formal parameters for elaboration
494 --
495 -- The ABE mechanism does not generate N_Call_Marker nodes for
496 -- calls which occur in expanded instances, and invoke generic
497 -- actual subprograms through generic formal subprograms. As a
498 -- result, the calls are not recorded or processed.
499 --
500 -- -gnatd_i ignore activations and calls to instances for elaboration
501 --
502 -- The ABE mechanism ignores calls and task activations when they
503 -- target a subprogram or task type defined an external instance.
504 -- As a result, the calls and task activations are not processed.
505 --
506 -- -gnatdL ignore external calls from instances for elaboration
507 --
508 -- The ABE mechanism does not generate N_Call_Marker nodes for
509 -- calls which occur in expanded instances, do not invoke generic
510 -- actual subprograms through formal subprograms, and the target
511 -- is external to the instance. As a result, the calls are not
512 -- recorded or processed.
513 --
514 -- -gnatd.o conservative elaboration order for indirect calls
515 --
516 -- The ABE mechanism treats '[Unrestricted_]Access of an entry,
517 -- operator, or subprogram as an immediate invocation of the
518 -- target. As a result, it performs ABE checks and diagnostics on
519 -- the immediate call.
520 --
521 -- -gnatd_p ignore assertion pragmas for elaboration
522 --
523 -- The ABE mechanism does not generate N_Call_Marker nodes for
524 -- calls to subprograms which verify the run-time semantics of
525 -- the following assertion pragmas:
526 --
527 -- Default_Initial_Condition
528 -- Initial_Condition
529 -- Invariant
530 -- Invariant'Class
531 -- Post
532 -- Post'Class
533 -- Postcondition
534 -- Type_Invariant
535 -- Type_Invariant_Class
536 --
537 -- As a result, the assertion expressions of the pragmas are not
538 -- processed.
539 --
540 -- -gnatd_s stop elaboration checks on synchronous suspension
541 --
542 -- The ABE mechanism stops the traversal of a task body when it
543 -- encounters a call to one of the following routines:
544 --
545 -- Ada.Synchronous_Barriers.Wait_For_Release
546 -- Ada.Synchronous_Task_Control.Suspend_Until_True
547 --
548 -- -gnatd_T output trace information on invocation relation construction
549 --
550 -- The ABE mechanism outputs text information concerning relation
551 -- construction to standard output.
552 --
553 -- -gnatd.U ignore indirect calls for static elaboration
554 --
555 -- The ABE mechanism does not consider '[Unrestricted_]Access of
556 -- entries, operators, and subprograms. As a result, the scenarios
557 -- are not recorder or processed.
558 --
559 -- -gnatd.v enforce SPARK elaboration rules in SPARK code
560 --
561 -- The ABE mechanism applies some of the SPARK elaboration rules
562 -- defined in the SPARK reference manual, chapter 7.7. Note that
563 -- certain rules are always enforced, regardless of whether the
564 -- switch is active.
565 --
566 -- -gnatd.y disable implicit pragma Elaborate_All on task bodies
567 --
568 -- The ABE mechanism does not generate implicit Elaborate_All when
569 -- the need for the pragma came from a task body.
570 --
571 -- -gnatE dynamic elaboration checking mode enabled
572 --
573 -- The ABE mechanism assumes that any scenario is elaborated or
574 -- invoked by elaboration code. The ABE mechanism performs very
575 -- little diagnostics and generates condintional ABE checks to
576 -- detect ABE issues at run-time.
577 --
578 -- -gnatel turn on info messages on generated Elaborate[_All] pragmas
579 --
580 -- The ABE mechanism produces information messages on generated
581 -- implicit Elabote[_All] pragmas along with traceback showing
582 -- why the pragma was generated. In addition, the ABE mechanism
583 -- produces information messages for each scenario elaborated or
584 -- invoked by elaboration code.
585 --
586 -- -gnateL turn off info messages on generated Elaborate[_All] pragmas
587 --
588 -- The complementary switch for -gnatel.
589 --
590 -- -gnatH legacy elaboration checking mode enabled
591 --
592 -- When this switch is in effect, the pre-18.x ABE model becomes
593 -- the defacto ABE model. This ammounts to cutting off all entry
594 -- points into the new ABE mechanism, and giving full control to
595 -- the old ABE mechanism.
596 --
597 -- -gnatJ permissive elaboration checking mode enabled
598 --
599 -- This switch activates the following switches:
600 --
601 -- -gnatd_a
602 -- -gnatd_e
603 -- -gnatd.G
604 -- -gnatd_i
605 -- -gnatdL
606 -- -gnatd_p
607 -- -gnatd_s
608 -- -gnatd.U
609 -- -gnatd.y
610 --
611 -- IMPORTANT: The behavior of the ABE mechanism becomes more
612 -- permissive at the cost of accurate diagnostics and runtime
613 -- ABE checks.
614 --
615 -- -gnatw.f turn on warnings for suspicious Subp'Access
616 --
617 -- The ABE mechanism treats '[Unrestricted_]Access of an entry,
618 -- operator, or subprogram as a pseudo invocation of the target.
619 -- As a result, it performs ABE diagnostics on the pseudo call.
620 --
621 -- -gnatw.F turn off warnings for suspicious Subp'Access
622 --
623 -- The complementary switch for -gnatw.f.
624 --
625 -- -gnatwl turn on warnings for elaboration problems
626 --
627 -- The ABE mechanism produces warnings on detected ABEs along with
628 -- a traceback showing the graph of the ABE.
629 --
630 -- -gnatwL turn off warnings for elaboration problems
631 --
632 -- The complementary switch for -gnatwl.
633
634 --------------------------
635 -- Debugging ABE issues --
636 --------------------------
637
638 -- * If the issue involves a call, ensure that the call is eligible for ABE
639 -- processing and receives a corresponding call marker. The routines of
640 -- interest are
641 --
642 -- Build_Call_Marker
643 -- Record_Elaboration_Scenario
644 --
645 -- * If the issue involves an arbitrary scenario, ensure that the scenario
646 -- is either recorded, or is successfully recognized while traversing a
647 -- body. The routines of interest are
648 --
649 -- Record_Elaboration_Scenario
650 -- Process_Conditional_ABE
651 -- Process_Guaranteed_ABE
652 -- Traverse_Body
653 --
654 -- * If the issue involves a circularity in the elaboration order, examine
655 -- the ALI files and look for the following encodings next to units:
656 --
657 -- E indicates a source Elaborate
658 --
659 -- EA indicates a source Elaborate_All
660 --
661 -- AD indicates an implicit Elaborate_All
662 --
663 -- ED indicates an implicit Elaborate
664 --
665 -- If possible, compare these encodings with those generated by the old
666 -- ABE mechanism. The routines of interest are
667 --
668 -- Ensure_Prior_Elaboration
669
670 -----------
671 -- Kinds --
672 -----------
673
674 -- The following type enumerates all possible elaboration phase statutes
675
676 type Elaboration_Phase_Status is
677 (Inactive,
678 -- The elaboration phase of the compiler has not started yet
679
680 Active,
681 -- The elaboration phase of the compiler is currently in progress
682
683 Completed);
684 -- The elaboration phase of the compiler has finished
685
686 Elaboration_Phase : Elaboration_Phase_Status := Inactive;
687 -- The status of the elaboration phase. Use routine Set_Elaboration_Phase
688 -- to alter its value.
689
690 -- The following type enumerates all subprogram body traversal modes
691
692 type Body_Traversal_Kind is
693 (Deep_Traversal,
694 -- The traversal examines the internals of a subprogram
695
696 No_Traversal);
697
698 -- The following type enumerates all operation modes
699
700 type Processing_Kind is
701 (Conditional_ABE_Processing,
702 -- The ABE mechanism detects and diagnoses conditional ABEs for library
703 -- and declaration-level scenarios.
704
705 Dynamic_Model_Processing,
706 -- The ABE mechanism installs conditional ABE checks for all eligible
707 -- scenarios when the dynamic model is in effect.
708
709 Guaranteed_ABE_Processing,
710 -- The ABE mechanism detects and diagnoses guaranteed ABEs caused by
711 -- calls, instantiations, and task activations.
712
713 Invocation_Construct_Processing,
714 -- The ABE mechanism locates all invocation constructs within the main
715 -- unit and utilizes them as roots of miltiple DFS traversals aimed at
716 -- detecting transitions from the main unit to an external unit.
717
718 Invocation_Body_Processing,
719 -- The ABE mechanism utilizes all library-level body scenarios as roots
720 -- of miltiple DFS traversals aimed at detecting transitions from the
721 -- main unit to an external unit.
722
723 Invocation_Spec_Processing,
724 -- The ABE mechanism utilizes all library-level spec scenarios as roots
725 -- of miltiple DFS traversals aimed at detecting transitions from the
726 -- main unit to an external unit.
727
728 SPARK_Processing,
729 -- The ABE mechanism detects and diagnoses violations of the SPARK
730 -- elaboration rules for SPARK-specific scenarios.
731
732 No_Processing);
733
734 -- The following type enumerates all possible scenario kinds
735
736 type Scenario_Kind is
737 (Access_Taken_Scenario,
738 -- An attribute reference which takes 'Access or 'Unrestricted_Access of
739 -- an entry, operator, or subprogram.
740
741 Call_Scenario,
742 -- A call which invokes an entry, operator, or subprogram
743
744 Derived_Type_Scenario,
745 -- A declaration of a derived type. This is a SPARK-specific scenario.
746
747 Instantiation_Scenario,
748 -- An instantiation which instantiates a generic package or subprogram.
749 -- This scenario is also subject to SPARK-specific rules.
750
751 Refined_State_Pragma_Scenario,
752 -- A Refined_State pragma. This is a SPARK-specific scenario.
753
754 Task_Activation_Scenario,
755 -- A call which activates objects of various task types
756
757 Variable_Assignment_Scenario,
758 -- An assignment statement which modifies the value of some variable
759
760 Variable_Reference_Scenario,
761 -- A reference to a variable. This is a SPARK-specific scenario.
762
763 No_Scenario);
764
765 -- The following type enumerates all possible consistency models of target
766 -- and scenario representations.
767
768 type Representation_Kind is
769 (Inconsistent_Representation,
770 -- A representation is said to be "inconsistent" when it is created from
771 -- a partially analyzed tree. In such an environment, certain attributes
772 -- such as a completing body may not be available yet.
773
774 Consistent_Representation,
775 -- A representation is said to be "consistent" when it is created from a
776 -- fully analyzed tree, where all attributes are available.
777
778 No_Representation);
779
780 -- The following type enumerates all possible target kinds
781
782 type Target_Kind is
783 (Generic_Target,
784 -- A generic unit being instantiated
785
786 Package_Target,
787 -- The package form of an instantiation
788
789 Subprogram_Target,
790 -- An entry, operator, or subprogram being invoked, or aliased through
791 -- 'Access or 'Unrestricted_Access.
792
793 Task_Target,
794 -- A task being activated by an activation call
795
796 Variable_Target,
797 -- A variable being updated through an assignment statement, or read
798 -- through a variable reference.
799
800 No_Target);
801
802 -----------
803 -- Types --
804 -----------
805
806 procedure Destroy (NE : in out Node_Or_Entity_Id);
807 pragma Inline (Destroy);
808 -- Destroy node or entity NE
809
810 function Hash (NE : Node_Or_Entity_Id) return Bucket_Range_Type;
811 pragma Inline (Hash);
812 -- Obtain the hash value of key NE
813
814 -- The following is a general purpose list for nodes and entities
815
816 package NE_List is new Doubly_Linked_Lists
817 (Element_Type => Node_Or_Entity_Id,
818 "=" => "=",
819 Destroy_Element => Destroy);
820
821 -- The following is a general purpose map which relates nodes and entities
822 -- to lists of nodes and entities.
823
824 package NE_List_Map is new Dynamic_Hash_Tables
825 (Key_Type => Node_Or_Entity_Id,
826 Value_Type => NE_List.Doubly_Linked_List,
827 No_Value => NE_List.Nil,
828 Expansion_Threshold => 1.5,
829 Expansion_Factor => 2,
830 Compression_Threshold => 0.3,
831 Compression_Factor => 2,
832 "=" => "=",
833 Destroy_Value => NE_List.Destroy,
834 Hash => Hash);
835
836 -- The following is a general purpose membership set for nodes and entities
837
838 package NE_Set is new Membership_Sets
839 (Element_Type => Node_Or_Entity_Id,
840 "=" => "=",
841 Hash => Hash);
842
843 -- The following type captures relevant attributes which pertain to the
844 -- in state of the Processing phase.
845
846 type Processing_In_State is record
847 Processing : Processing_Kind := No_Processing;
848 -- Operation mode of the Processing phase. Once set, this value should
849 -- not be changed.
850
851 Representation : Representation_Kind := No_Representation;
852 -- Required level of scenario and target representation. Once set, this
853 -- value should not be changed.
854
855 Suppress_Checks : Boolean := False;
856 -- This flag is set when the Processing phase must not generate any ABE
857 -- checks.
858
859 Suppress_Implicit_Pragmas : Boolean := False;
860 -- This flag is set when the Processing phase must not generate any
861 -- implicit Elaborate[_All] pragmas.
862
863 Suppress_Info_Messages : Boolean := False;
864 -- This flag is set when the Processing phase must not emit any info
865 -- messages.
866
867 Suppress_Up_Level_Targets : Boolean := False;
868 -- This flag is set when the Processing phase must ignore up-level
869 -- targets.
870
871 Suppress_Warnings : Boolean := False;
872 -- This flag is set when the Processing phase must not emit any warnings
873 -- on elaboration problems.
874
875 Traversal : Body_Traversal_Kind := No_Traversal;
876 -- The subprogram body traversal mode. Once set, this value should not
877 -- be changed.
878
879 Within_Generic : Boolean := False;
880 -- This flag is set when the Processing phase is currently within a
881 -- generic unit.
882
883 Within_Initial_Condition : Boolean := False;
884 -- This flag is set when the Processing phase is currently examining a
885 -- scenario which was reached from an initial condition procedure.
886
887 Within_Partial_Finalization : Boolean := False;
888 -- This flag is set when the Processing phase is currently examining a
889 -- scenario which was reached from a partial finalization procedure.
890
891 Within_Task_Body : Boolean := False;
892 -- This flag is set when the Processing phase is currently examining a
893 -- scenario which was reached from a task body.
894 end record;
895
896 -- The following constants define the various operational states of the
897 -- Processing phase.
898
899 -- The conditional ABE state is used when processing scenarios that appear
900 -- at the declaration, instantiation, and library levels to detect errors
901 -- and install conditional ABE checks.
902
903 Conditional_ABE_State : constant Processing_In_State :=
904 (Processing => Conditional_ABE_Processing,
905 Representation => Consistent_Representation,
906 Traversal => Deep_Traversal,
907 others => False);
908
909 -- The dynamic model state is used to install conditional ABE checks when
910 -- switch -gnatE (dynamic elaboration checking mode enabled) is in effect.
911
912 Dynamic_Model_State : constant Processing_In_State :=
913 (Processing => Dynamic_Model_Processing,
914 Representation => Consistent_Representation,
915 Suppress_Implicit_Pragmas => True,
916 Suppress_Info_Messages => True,
917 Suppress_Up_Level_Targets => True,
918 Suppress_Warnings => True,
919 Traversal => No_Traversal,
920 others => False);
921
922 -- The guaranteed ABE state is used when processing scenarios that appear
923 -- at the declaration, instantiation, and library levels to detect errors
924 -- and install guarateed ABE failures.
925
926 Guaranteed_ABE_State : constant Processing_In_State :=
927 (Processing => Guaranteed_ABE_Processing,
928 Representation => Inconsistent_Representation,
929 Suppress_Implicit_Pragmas => True,
930 Traversal => No_Traversal,
931 others => False);
932
933 -- The invocation body state is used when processing scenarios that appear
934 -- at the body library level to encode paths that start from elaboration
935 -- code and ultimately reach into external units.
936
937 Invocation_Body_State : constant Processing_In_State :=
938 (Processing => Invocation_Body_Processing,
939 Representation => Consistent_Representation,
940 Suppress_Checks => True,
941 Suppress_Implicit_Pragmas => True,
942 Suppress_Info_Messages => True,
943 Suppress_Up_Level_Targets => True,
944 Suppress_Warnings => True,
945 Traversal => Deep_Traversal,
946 others => False);
947
948 -- The invocation construct state is used when processing constructs that
949 -- appear within the spec and body of the main unit and eventually reach
950 -- into external units.
951
952 Invocation_Construct_State : constant Processing_In_State :=
953 (Processing => Invocation_Construct_Processing,
954 Representation => Consistent_Representation,
955 Suppress_Checks => True,
956 Suppress_Implicit_Pragmas => True,
957 Suppress_Info_Messages => True,
958 Suppress_Up_Level_Targets => True,
959 Suppress_Warnings => True,
960 Traversal => Deep_Traversal,
961 others => False);
962
963 -- The invocation spec state is used when processing scenarios that appear
964 -- at the spec library level to encode paths that start from elaboration
965 -- code and ultimately reach into external units.
966
967 Invocation_Spec_State : constant Processing_In_State :=
968 (Processing => Invocation_Spec_Processing,
969 Representation => Consistent_Representation,
970 Suppress_Checks => True,
971 Suppress_Implicit_Pragmas => True,
972 Suppress_Info_Messages => True,
973 Suppress_Up_Level_Targets => True,
974 Suppress_Warnings => True,
975 Traversal => Deep_Traversal,
976 others => False);
977
978 -- The SPARK state is used when verying SPARK-specific semantics of certain
979 -- scenarios.
980
981 SPARK_State : constant Processing_In_State :=
982 (Processing => SPARK_Processing,
983 Representation => Consistent_Representation,
984 Traversal => No_Traversal,
985 others => False);
986
987 -- The following type identifies a scenario representation
988
989 type Scenario_Rep_Id is new Natural;
990
991 No_Scenario_Rep : constant Scenario_Rep_Id := Scenario_Rep_Id'First;
992 First_Scenario_Rep : constant Scenario_Rep_Id := No_Scenario_Rep + 1;
993
994 -- The following type identifies a target representation
995
996 type Target_Rep_Id is new Natural;
997
998 No_Target_Rep : constant Target_Rep_Id := Target_Rep_Id'First;
999 First_Target_Rep : constant Target_Rep_Id := No_Target_Rep + 1;
1000
1001 --------------
1002 -- Services --
1003 --------------
1004
1005 -- The following package keeps track of all active scenarios during a DFS
1006 -- traversal.
1007
1008 package Active_Scenarios is
1009
1010 -----------
1011 -- Types --
1012 -----------
1013
1014 -- The following type defines the position within the active scenario
1015 -- stack.
1016
1017 type Active_Scenario_Pos is new Natural;
1018
1019 ---------------------
1020 -- Data structures --
1021 ---------------------
1022
1023 -- The following table stores all active scenarios in a DFS traversal.
1024 -- This table must be maintained in a FIFO fashion.
1025
1026 package Active_Scenario_Stack is new Table.Table
1027 (Table_Index_Type => Active_Scenario_Pos,
1028 Table_Component_Type => Node_Id,
1029 Table_Low_Bound => 1,
1030 Table_Initial => 50,
1031 Table_Increment => 200,
1032 Table_Name => "Active_Scenario_Stack");
1033
1034 ---------
1035 -- API --
1036 ---------
1037
1038 procedure Output_Active_Scenarios
1039 (Error_Nod : Node_Id;
1040 In_State : Processing_In_State);
1041 pragma Inline (Output_Active_Scenarios);
1042 -- Output the contents of the active scenario stack from earliest to
1043 -- latest to supplement an earlier error emitted for node Error_Nod.
1044 -- In_State denotes the current state of the Processing phase.
1045
1046 procedure Pop_Active_Scenario (N : Node_Id);
1047 pragma Inline (Pop_Active_Scenario);
1048 -- Pop the top of the scenario stack. A check is made to ensure that the
1049 -- scenario being removed is the same as N.
1050
1051 procedure Push_Active_Scenario (N : Node_Id);
1052 pragma Inline (Push_Active_Scenario);
1053 -- Push scenario N on top of the scenario stack
1054
1055 function Root_Scenario return Node_Id;
1056 pragma Inline (Root_Scenario);
1057 -- Return the scenario which started a DFS traversal
1058
1059 end Active_Scenarios;
1060 use Active_Scenarios;
1061
1062 -- The following package provides the main entry point for task activation
1063 -- processing.
1064
1065 package Activation_Processor is
1066
1067 -----------
1068 -- Types --
1069 -----------
1070
1071 type Activation_Processor_Ptr is access procedure
1072 (Call : Node_Id;
1073 Call_Rep : Scenario_Rep_Id;
1074 Obj_Id : Entity_Id;
1075 Obj_Rep : Target_Rep_Id;
1076 Task_Typ : Entity_Id;
1077 Task_Rep : Target_Rep_Id;
1078 In_State : Processing_In_State);
1079 -- Reference to a procedure that takes all attributes of an activation
1080 -- and performs a desired action. Call is the activation call. Call_Rep
1081 -- is the representation of the call. Obj_Id is the task object being
1082 -- activated. Obj_Rep is the representation of the object. Task_Typ is
1083 -- the task type whose body is being activated. Task_Rep denotes the
1084 -- representation of the task type. In_State is the current state of
1085 -- the Processing phase.
1086
1087 ---------
1088 -- API --
1089 ---------
1090
1091 procedure Process_Activation
1092 (Call : Node_Id;
1093 Call_Rep : Scenario_Rep_Id;
1094 Processor : Activation_Processor_Ptr;
1095 In_State : Processing_In_State);
1096 -- Find all task objects activated by activation call Call and invoke
1097 -- Processor on them. Call_Rep denotes the representation of the call.
1098 -- In_State is the current state of the Processing phase.
1099
1100 end Activation_Processor;
1101 use Activation_Processor;
1102
1103 -- The following package profides functionality for traversing subprogram
1104 -- bodies in DFS manner and processing of eligible scenarios within.
1105
1106 package Body_Processor is
1107
1108 -----------
1109 -- Types --
1110 -----------
1111
1112 type Scenario_Predicate_Ptr is access function
1113 (N : Node_Id) return Boolean;
1114 -- Reference to a function which determines whether arbitrary node N
1115 -- denotes a suitable scenario for processing.
1116
1117 type Scenario_Processor_Ptr is access procedure
1118 (N : Node_Id; In_State : Processing_In_State);
1119 -- Reference to a procedure which processes scenario N. In_State is the
1120 -- current state of the Processing phase.
1121
1122 ---------
1123 -- API --
1124 ---------
1125
1126 procedure Traverse_Body
1127 (N : Node_Id;
1128 Requires_Processing : Scenario_Predicate_Ptr;
1129 Processor : Scenario_Processor_Ptr;
1130 In_State : Processing_In_State);
1131 pragma Inline (Traverse_Body);
1132 -- Traverse the declarations and handled statements of subprogram body
1133 -- N, looking for scenarios that satisfy predicate Requires_Processing.
1134 -- Routine Processor is invoked for each such scenario.
1135
1136 procedure Reset_Traversed_Bodies;
1137 pragma Inline (Reset_Traversed_Bodies);
1138 -- Reset the visited status of all subprogram bodies that have already
1139 -- been processed by routine Traverse_Body.
1140
1141 -----------------
1142 -- Maintenance --
1143 -----------------
1144
1145 procedure Finalize_Body_Processor;
1146 pragma Inline (Finalize_Body_Processor);
1147 -- Finalize all internal data structures
1148
1149 procedure Initialize_Body_Processor;
1150 pragma Inline (Initialize_Body_Processor);
1151 -- Initialize all internal data structures
1152
1153 end Body_Processor;
1154 use Body_Processor;
1155
1156 -- The following package provides functionality for installing ABE-related
1157 -- checks and failures.
1158
1159 package Check_Installer is
1160
1161 ---------
1162 -- API --
1163 ---------
1164
1165 function Check_Or_Failure_Generation_OK return Boolean;
1166 pragma Inline (Check_Or_Failure_Generation_OK);
1167 -- Determine whether a conditional ABE check or guaranteed ABE failure
1168 -- can be generated.
1169
1170 procedure Install_Dynamic_ABE_Checks;
1171 pragma Inline (Install_Dynamic_ABE_Checks);
1172 -- Install conditional ABE checks for all saved scenarios when the
1173 -- dynamic model is in effect.
1174
1175 procedure Install_Scenario_ABE_Check
1176 (N : Node_Id;
1177 Targ_Id : Entity_Id;
1178 Targ_Rep : Target_Rep_Id;
1179 Disable : Scenario_Rep_Id);
1180 pragma Inline (Install_Scenario_ABE_Check);
1181 -- Install a conditional ABE check for scenario N to ensure that target
1182 -- Targ_Id is properly elaborated. Targ_Rep is the representation of the
1183 -- target. If the check is installed, disable the elaboration checks of
1184 -- scenario Disable.
1185
1186 procedure Install_Scenario_ABE_Check
1187 (N : Node_Id;
1188 Targ_Id : Entity_Id;
1189 Targ_Rep : Target_Rep_Id;
1190 Disable : Target_Rep_Id);
1191 pragma Inline (Install_Scenario_ABE_Check);
1192 -- Install a conditional ABE check for scenario N to ensure that target
1193 -- Targ_Id is properly elaborated. Targ_Rep is the representation of the
1194 -- target. If the check is installed, disable the elaboration checks of
1195 -- target Disable.
1196
1197 procedure Install_Scenario_ABE_Failure
1198 (N : Node_Id;
1199 Targ_Id : Entity_Id;
1200 Targ_Rep : Target_Rep_Id;
1201 Disable : Scenario_Rep_Id);
1202 pragma Inline (Install_Scenario_ABE_Failure);
1203 -- Install a guaranteed ABE failure for scenario N with target Targ_Id.
1204 -- Targ_Rep denotes the representation of the target. If the failure is
1205 -- installed, disable the elaboration checks of scenario Disable.
1206
1207 procedure Install_Scenario_ABE_Failure
1208 (N : Node_Id;
1209 Targ_Id : Entity_Id;
1210 Targ_Rep : Target_Rep_Id;
1211 Disable : Target_Rep_Id);
1212 pragma Inline (Install_Scenario_ABE_Failure);
1213 -- Install a guaranteed ABE failure for scenario N with target Targ_Id.
1214 -- Targ_Rep denotes the representation of the target. If the failure is
1215 -- installed, disable the elaboration checks of target Disable.
1216
1217 procedure Install_Unit_ABE_Check
1218 (N : Node_Id;
1219 Unit_Id : Entity_Id;
1220 Disable : Scenario_Rep_Id);
1221 pragma Inline (Install_Unit_ABE_Check);
1222 -- Install a conditional ABE check for scenario N to ensure that unit
1223 -- Unit_Id is properly elaborated. If the check is installed, disable
1224 -- the elaboration checks of scenario Disable.
1225
1226 procedure Install_Unit_ABE_Check
1227 (N : Node_Id;
1228 Unit_Id : Entity_Id;
1229 Disable : Target_Rep_Id);
1230 pragma Inline (Install_Unit_ABE_Check);
1231 -- Install a conditional ABE check for scenario N to ensure that unit
1232 -- Unit_Id is properly elaborated. If the check is installed, disable
1233 -- the elaboration checks of target Disable.
1234
1235 end Check_Installer;
1236 use Check_Installer;
1237
1238 -- The following package provides the main entry point for conditional ABE
1239 -- checks and diagnostics.
1240
1241 package Conditional_ABE_Processor is
1242
1243 ---------
1244 -- API --
1245 ---------
1246
1247 procedure Check_Conditional_ABE_Scenarios
1248 (Iter : in out NE_Set.Iterator);
1249 pragma Inline (Check_Conditional_ABE_Scenarios);
1250 -- Perform conditional ABE checks and diagnostics for all scenarios
1251 -- available through iterator Iter.
1252
1253 procedure Process_Conditional_ABE
1254 (N : Node_Id;
1255 In_State : Processing_In_State);
1256 pragma Inline (Process_Conditional_ABE);
1257 -- Perform conditional ABE checks and diagnostics for scenario N.
1258 -- In_State denotes the current state of the Processing phase.
1259
1260 end Conditional_ABE_Processor;
1261 use Conditional_ABE_Processor;
1262
1263 -- The following package provides functionality to emit errors, information
1264 -- messages, and warnings.
1265
1266 package Diagnostics is
1267
1268 ---------
1269 -- API --
1270 ---------
1271
1272 procedure Elab_Msg_NE
1273 (Msg : String;
1274 N : Node_Id;
1275 Id : Entity_Id;
1276 Info_Msg : Boolean;
1277 In_SPARK : Boolean);
1278 pragma Inline (Elab_Msg_NE);
1279 -- Wrapper around Error_Msg_NE. Emit message Msg concerning arbitrary
1280 -- node N and entity. If flag Info_Msg is set, the routine emits an
1281 -- information message, otherwise it emits an error. If flag In_SPARK
1282 -- is set, then string " in SPARK" is added to the end of the message.
1283
1284 procedure Info_Call
1285 (Call : Node_Id;
1286 Subp_Id : Entity_Id;
1287 Info_Msg : Boolean;
1288 In_SPARK : Boolean);
1289 pragma Inline (Info_Call);
1290 -- Output information concerning call Call that invokes subprogram
1291 -- Subp_Id. When flag Info_Msg is set, the routine emits an information
1292 -- message, otherwise it emits an error. When flag In_SPARK is set, " in
1293 -- SPARK" is added to the end of the message.
1294
1295 procedure Info_Instantiation
1296 (Inst : Node_Id;
1297 Gen_Id : Entity_Id;
1298 Info_Msg : Boolean;
1299 In_SPARK : Boolean);
1300 pragma Inline (Info_Instantiation);
1301 -- Output information concerning instantiation Inst which instantiates
1302 -- generic unit Gen_Id. If flag Info_Msg is set, the routine emits an
1303 -- information message, otherwise it emits an error. If flag In_SPARK
1304 -- is set, then string " in SPARK" is added to the end of the message.
1305
1306 procedure Info_Variable_Reference
1307 (Ref : Node_Id;
1308 Var_Id : Entity_Id;
1309 Info_Msg : Boolean;
1310 In_SPARK : Boolean);
1311 pragma Inline (Info_Variable_Reference);
1312 -- Output information concerning reference Ref which mentions variable
1313 -- Var_Id. If flag Info_Msg is set, the routine emits an information
1314 -- message, otherwise it emits an error. If flag In_SPARK is set, then
1315 -- string " in SPARK" is added to the end of the message.
1316
1317 end Diagnostics;
1318 use Diagnostics;
1319
1320 -- The following package provides functionality to locate the early call
1321 -- region of a subprogram body.
1322
1323 package Early_Call_Region_Processor is
1324
1325 ---------
1326 -- API --
1327 ---------
1328
1329 function Find_Early_Call_Region
1330 (Body_Decl : Node_Id;
1331 Assume_Elab_Body : Boolean := False;
1332 Skip_Memoization : Boolean := False) return Node_Id;
1333 pragma Inline (Find_Early_Call_Region);
1334 -- Find the start of the early call region that belongs to subprogram
1335 -- body Body_Decl as defined in SPARK RM 7.7. This routine finds the
1336 -- early call region, memoizes it, and returns it, but this behavior
1337 -- can be altered. Flag Assume_Elab_Body should be set when a package
1338 -- spec may lack pragma Elaborate_Body, but the routine must still
1339 -- examine that spec. Flag Skip_Memoization should be set when the
1340 -- routine must avoid memoizing the region.
1341
1342 -----------------
1343 -- Maintenance --
1344 -----------------
1345
1346 procedure Finalize_Early_Call_Region_Processor;
1347 pragma Inline (Finalize_Early_Call_Region_Processor);
1348 -- Finalize all internal data structures
1349
1350 procedure Initialize_Early_Call_Region_Processor;
1351 pragma Inline (Initialize_Early_Call_Region_Processor);
1352 -- Initialize all internal data structures
1353
1354 end Early_Call_Region_Processor;
1355 use Early_Call_Region_Processor;
1356
1357 -- The following package provides access to the elaboration statuses of all
1358 -- units withed by the main unit.
1359
1360 package Elaborated_Units is
1361
1362 ---------
1363 -- API --
1364 ---------
1365
1366 procedure Collect_Elaborated_Units;
1367 pragma Inline (Collect_Elaborated_Units);
1368 -- Save the elaboration statuses of all units withed by the main unit
1369
1370 procedure Ensure_Prior_Elaboration
1371 (N : Node_Id;
1372 Unit_Id : Entity_Id;
1373 Prag_Nam : Name_Id;
1374 In_State : Processing_In_State);
1375 pragma Inline (Ensure_Prior_Elaboration);
1376 -- Guarantee the elaboration of unit Unit_Id with respect to the main
1377 -- unit by either suggesting or installing an Elaborate[_All] pragma
1378 -- denoted by Prag_Nam. N denotes the related scenario. In_State is the
1379 -- current state of the Processing phase.
1380
1381 function Has_Prior_Elaboration
1382 (Unit_Id : Entity_Id;
1383 Context_OK : Boolean := False;
1384 Elab_Body_OK : Boolean := False;
1385 Same_Unit_OK : Boolean := False) return Boolean;
1386 pragma Inline (Has_Prior_Elaboration);
1387 -- Determine whether unit Unit_Id is elaborated prior to the main unit.
1388 -- If flag Context_OK is set, the routine considers the following case
1389 -- as valid prior elaboration:
1390 --
1391 -- * Unit_Id is in the elaboration context of the main unit
1392 --
1393 -- If flag Elab_Body_OK is set, the routine considers the following case
1394 -- as valid prior elaboration:
1395 --
1396 -- * Unit_Id has pragma Elaborate_Body and is not the main unit
1397 --
1398 -- If flag Same_Unit_OK is set, the routine considers the following
1399 -- cases as valid prior elaboration:
1400 --
1401 -- * Unit_Id is the main unit
1402 --
1403 -- * Unit_Id denotes the spec of the main unit body
1404
1405 procedure Meet_Elaboration_Requirement
1406 (N : Node_Id;
1407 Targ_Id : Entity_Id;
1408 Req_Nam : Name_Id;
1409 In_State : Processing_In_State);
1410 pragma Inline (Meet_Elaboration_Requirement);
1411 -- Determine whether elaboration requirement Req_Nam for scenario N with
1412 -- target Targ_Id is met by the context of the main unit using the SPARK
1413 -- rules. Req_Nam must denote either Elaborate or Elaborate_All. Emit an
1414 -- error if this is not the case. In_State denotes the current state of
1415 -- the Processing phase.
1416
1417 -----------------
1418 -- Maintenance --
1419 -----------------
1420
1421 procedure Finalize_Elaborated_Units;
1422 pragma Inline (Finalize_Elaborated_Units);
1423 -- Finalize all internal data structures
1424
1425 procedure Initialize_Elaborated_Units;
1426 pragma Inline (Initialize_Elaborated_Units);
1427 -- Initialize all internal data structures
1428
1429 end Elaborated_Units;
1430 use Elaborated_Units;
1431
1432 -- The following package provides the main entry point for guaranteed ABE
1433 -- checks and diagnostics.
1434
1435 package Guaranteed_ABE_Processor is
1436
1437 ---------
1438 -- API --
1439 ---------
1440
1441 procedure Process_Guaranteed_ABE
1442 (N : Node_Id;
1443 In_State : Processing_In_State);
1444 pragma Inline (Process_Guaranteed_ABE);
1445 -- Perform guaranteed ABE checks and diagnostics for scenario N.
1446 -- In_State is the current state of the Processing phase.
1447
1448 end Guaranteed_ABE_Processor;
1449 use Guaranteed_ABE_Processor;
1450
1451 -- The following package provides access to the internal representation of
1452 -- scenarios and targets.
1453
1454 package Internal_Representation is
1455
1456 -----------
1457 -- Types --
1458 -----------
1459
1460 -- The following type enumerates all possible Ghost mode mode kinds
1461
1462 type Extended_Ghost_Mode is
1463 (Is_Ignored,
1464 Is_Checked_Or_Not_Specified);
1465
1466 -- The following type enumerates all possible SPARK mode kinds
1467
1468 type Extended_SPARK_Mode is
1469 (Is_On,
1470 Is_Off_Or_Not_Specified);
1471
1472 --------------
1473 -- Builders --
1474 --------------
1475
1476 function Scenario_Representation_Of
1477 (N : Node_Id;
1478 In_State : Processing_In_State) return Scenario_Rep_Id;
1479 pragma Inline (Scenario_Representation_Of);
1480 -- Obtain the id of elaboration scenario N's representation. The routine
1481 -- constructs the representation if it is not available. In_State is the
1482 -- current state of the Processing phase.
1483
1484 function Target_Representation_Of
1485 (Id : Entity_Id;
1486 In_State : Processing_In_State) return Target_Rep_Id;
1487 pragma Inline (Target_Representation_Of);
1488 -- Obtain the id of elaboration target Id's representation. The routine
1489 -- constructs the representation if it is not available. In_State is the
1490 -- current state of the Processing phase.
1491
1492 -------------------------
1493 -- Scenario attributes --
1494 -------------------------
1495
1496 function Activated_Task_Objects
1497 (S_Id : Scenario_Rep_Id) return NE_List.Doubly_Linked_List;
1498 pragma Inline (Activated_Task_Objects);
1499 -- For Task_Activation_Scenario S_Id, obtain the list of task objects
1500 -- the scenario is activating.
1501
1502 function Activated_Task_Type (S_Id : Scenario_Rep_Id) return Entity_Id;
1503 pragma Inline (Activated_Task_Type);
1504 -- For Task_Activation_Scenario S_Id, obtain the currently activated
1505 -- task type.
1506
1507 procedure Disable_Elaboration_Checks (S_Id : Scenario_Rep_Id);
1508 pragma Inline (Disable_Elaboration_Checks);
1509 -- Disable elaboration checks of scenario S_Id
1510
1511 function Elaboration_Checks_OK (S_Id : Scenario_Rep_Id) return Boolean;
1512 pragma Inline (Elaboration_Checks_OK);
1513 -- Determine whether scenario S_Id may be subjected to elaboration
1514 -- checks.
1515
1516 function Elaboration_Warnings_OK (S_Id : Scenario_Rep_Id) return Boolean;
1517 pragma Inline (Elaboration_Warnings_OK);
1518 -- Determine whether scenario S_Id may be subjected to elaboration
1519 -- warnings.
1520
1521 function Ghost_Mode_Of
1522 (S_Id : Scenario_Rep_Id) return Extended_Ghost_Mode;
1523 pragma Inline (Ghost_Mode_Of);
1524 -- Obtain the Ghost mode of scenario S_Id
1525
1526 function Is_Dispatching_Call (S_Id : Scenario_Rep_Id) return Boolean;
1527 pragma Inline (Is_Dispatching_Call);
1528 -- For Call_Scenario S_Id, determine whether the call is dispatching
1529
1530 function Is_Read_Reference (S_Id : Scenario_Rep_Id) return Boolean;
1531 pragma Inline (Is_Read_Reference);
1532 -- For Variable_Reference_Scenario S_Id, determine whether the reference
1533 -- is a read.
1534
1535 function Kind (S_Id : Scenario_Rep_Id) return Scenario_Kind;
1536 pragma Inline (Kind);
1537 -- Obtain the nature of scenario S_Id
1538
1539 function Level (S_Id : Scenario_Rep_Id) return Enclosing_Level_Kind;
1540 pragma Inline (Level);
1541 -- Obtain the enclosing level of scenario S_Id
1542
1543 procedure Set_Activated_Task_Objects
1544 (S_Id : Scenario_Rep_Id;
1545 Task_Objs : NE_List.Doubly_Linked_List);
1546 pragma Inline (Set_Activated_Task_Objects);
1547 -- For Task_Activation_Scenario S_Id, set the list of task objects
1548 -- activated by the scenario to Task_Objs.
1549
1550 procedure Set_Activated_Task_Type
1551 (S_Id : Scenario_Rep_Id;
1552 Task_Typ : Entity_Id);
1553 pragma Inline (Set_Activated_Task_Type);
1554 -- For Task_Activation_Scenario S_Id, set the currently activated task
1555 -- type to Task_Typ.
1556
1557 function SPARK_Mode_Of
1558 (S_Id : Scenario_Rep_Id) return Extended_SPARK_Mode;
1559 pragma Inline (SPARK_Mode_Of);
1560 -- Obtain the SPARK mode of scenario S_Id
1561
1562 function Target (S_Id : Scenario_Rep_Id) return Entity_Id;
1563 pragma Inline (Target);
1564 -- Obtain the target of scenario S_Id
1565
1566 -----------------------
1567 -- Target attributes --
1568 -----------------------
1569
1570 function Barrier_Body_Declaration (T_Id : Target_Rep_Id) return Node_Id;
1571 pragma Inline (Barrier_Body_Declaration);
1572 -- For Subprogram_Target T_Id, obtain the declaration of the barrier
1573 -- function's body.
1574
1575 function Body_Declaration (T_Id : Target_Rep_Id) return Node_Id;
1576 pragma Inline (Body_Declaration);
1577 -- Obtain the declaration of the body which belongs to target T_Id
1578
1579 procedure Disable_Elaboration_Checks (T_Id : Target_Rep_Id);
1580 pragma Inline (Disable_Elaboration_Checks);
1581 -- Disable elaboration checks of target T_Id
1582
1583 function Elaboration_Checks_OK (T_Id : Target_Rep_Id) return Boolean;
1584 pragma Inline (Elaboration_Checks_OK);
1585 -- Determine whether target T_Id may be subjected to elaboration checks
1586
1587 function Elaboration_Warnings_OK (T_Id : Target_Rep_Id) return Boolean;
1588 pragma Inline (Elaboration_Warnings_OK);
1589 -- Determine whether target T_Id may be subjected to elaboration
1590 -- warnings.
1591
1592 function Ghost_Mode_Of (T_Id : Target_Rep_Id) return Extended_Ghost_Mode;
1593 pragma Inline (Ghost_Mode_Of);
1594 -- Obtain the Ghost mode of target T_Id
1595
1596 function Kind (T_Id : Target_Rep_Id) return Target_Kind;
1597 pragma Inline (Kind);
1598 -- Obtain the nature of target T_Id
1599
1600 function SPARK_Mode_Of (T_Id : Target_Rep_Id) return Extended_SPARK_Mode;
1601 pragma Inline (SPARK_Mode_Of);
1602 -- Obtain the SPARK mode of target T_Id
1603
1604 function Spec_Declaration (T_Id : Target_Rep_Id) return Node_Id;
1605 pragma Inline (Spec_Declaration);
1606 -- Obtain the declaration of the spec which belongs to target T_Id
1607
1608 function Unit (T_Id : Target_Rep_Id) return Entity_Id;
1609 pragma Inline (Unit);
1610 -- Obtain the unit where the target is defined
1611
1612 function Variable_Declaration (T_Id : Target_Rep_Id) return Node_Id;
1613 pragma Inline (Variable_Declaration);
1614 -- For Variable_Target T_Id, obtain the declaration of the variable
1615
1616 -----------------
1617 -- Maintenance --
1618 -----------------
1619
1620 procedure Finalize_Internal_Representation;
1621 pragma Inline (Finalize_Internal_Representation);
1622 -- Finalize all internal data structures
1623
1624 procedure Initialize_Internal_Representation;
1625 pragma Inline (Initialize_Internal_Representation);
1626 -- Initialize all internal data structures
1627
1628 end Internal_Representation;
1629 use Internal_Representation;
1630
1631 -- The following package provides functionality for recording pieces of the
1632 -- invocation graph in the ALI file of the main unit.
1633
1634 package Invocation_Graph is
1635
1636 ---------
1637 -- API --
1638 ---------
1639
1640 procedure Record_Invocation_Graph;
1641 pragma Inline (Record_Invocation_Graph);
1642 -- Process all declaration, instantiation, and library level scenarios,
1643 -- along with invocation construct within the spec and body of the main
1644 -- unit to determine whether any of these reach into an external unit.
1645 -- If such a path exists, encode in the ALI file of the main unit.
1646
1647 -----------------
1648 -- Maintenance --
1649 -----------------
1650
1651 procedure Finalize_Invocation_Graph;
1652 pragma Inline (Finalize_Invocation_Graph);
1653 -- Finalize all internal data structures
1654
1655 procedure Initialize_Invocation_Graph;
1656 pragma Inline (Initialize_Invocation_Graph);
1657 -- Initialize all internal data structures
1658
1659 end Invocation_Graph;
1660 use Invocation_Graph;
1661
1662 -- The following package stores scenarios
1663
1664 package Scenario_Storage is
1665
1666 ---------
1667 -- API --
1668 ---------
1669
1670 procedure Add_Declaration_Scenario (N : Node_Id);
1671 pragma Inline (Add_Declaration_Scenario);
1672 -- Save declaration level scenario N
1673
1674 procedure Add_Dynamic_ABE_Check_Scenario (N : Node_Id);
1675 pragma Inline (Add_Dynamic_ABE_Check_Scenario);
1676 -- Save scenario N for conditional ABE check installation purposes when
1677 -- the dynamic model is in effect.
1678
1679 procedure Add_Library_Body_Scenario (N : Node_Id);
1680 pragma Inline (Add_Library_Body_Scenario);
1681 -- Save library-level body scenario N
1682
1683 procedure Add_Library_Spec_Scenario (N : Node_Id);
1684 pragma Inline (Add_Library_Spec_Scenario);
1685 -- Save library-level spec scenario N
1686
1687 procedure Add_SPARK_Scenario (N : Node_Id);
1688 pragma Inline (Add_SPARK_Scenario);
1689 -- Save SPARK scenario N
1690
1691 procedure Delete_Scenario (N : Node_Id);
1692 pragma Inline (Delete_Scenario);
1693 -- Delete arbitrary scenario N
1694
1695 function Iterate_Declaration_Scenarios return NE_Set.Iterator;
1696 pragma Inline (Iterate_Declaration_Scenarios);
1697 -- Obtain an iterator over all declaration level scenarios
1698
1699 function Iterate_Dynamic_ABE_Check_Scenarios return NE_Set.Iterator;
1700 pragma Inline (Iterate_Dynamic_ABE_Check_Scenarios);
1701 -- Obtain an iterator over all scenarios that require a conditional ABE
1702 -- check when the dynamic model is in effect.
1703
1704 function Iterate_Library_Body_Scenarios return NE_Set.Iterator;
1705 pragma Inline (Iterate_Library_Body_Scenarios);
1706 -- Obtain an iterator over all library level body scenarios
1707
1708 function Iterate_Library_Spec_Scenarios return NE_Set.Iterator;
1709 pragma Inline (Iterate_Library_Spec_Scenarios);
1710 -- Obtain an iterator over all library level spec scenarios
1711
1712 function Iterate_SPARK_Scenarios return NE_Set.Iterator;
1713 pragma Inline (Iterate_SPARK_Scenarios);
1714 -- Obtain an iterator over all SPARK scenarios
1715
1716 procedure Replace_Scenario (Old_N : Node_Id; New_N : Node_Id);
1717 pragma Inline (Replace_Scenario);
1718 -- Replace scenario Old_N with scenario New_N
1719
1720 -----------------
1721 -- Maintenance --
1722 -----------------
1723
1724 procedure Finalize_Scenario_Storage;
1725 pragma Inline (Finalize_Scenario_Storage);
1726 -- Finalize all internal data structures
1727
1728 procedure Initialize_Scenario_Storage;
1729 pragma Inline (Initialize_Scenario_Storage);
1730 -- Initialize all internal data structures
1731
1732 end Scenario_Storage;
1733 use Scenario_Storage;
1734
1735 -- The following package provides various semantic predicates
1736
1737 package Semantics is
1738
1739 ---------
1740 -- API --
1741 ---------
1742
1743 function Is_Accept_Alternative_Proc (Id : Entity_Id) return Boolean;
1744 pragma Inline (Is_Accept_Alternative_Proc);
1745 -- Determine whether arbitrary entity Id denotes an internally generated
1746 -- procedure which encapsulates the statements of an accept alternative.
1747
1748 function Is_Activation_Proc (Id : Entity_Id) return Boolean;
1749 pragma Inline (Is_Activation_Proc);
1750 -- Determine whether arbitrary entity Id denotes a runtime procedure in
1751 -- charge with activating tasks.
1752
1753 function Is_Ada_Semantic_Target (Id : Entity_Id) return Boolean;
1754 pragma Inline (Is_Ada_Semantic_Target);
1755 -- Determine whether arbitrary entity Id denodes a source or internally
1756 -- generated subprogram which emulates Ada semantics.
1757
1758 function Is_Assertion_Pragma_Target (Id : Entity_Id) return Boolean;
1759 pragma Inline (Is_Assertion_Pragma_Target);
1760 -- Determine whether arbitrary entity Id denotes a procedure which
1761 -- varifies the run-time semantics of an assertion pragma.
1762
1763 function Is_Bodiless_Subprogram (Subp_Id : Entity_Id) return Boolean;
1764 pragma Inline (Is_Bodiless_Subprogram);
1765 -- Determine whether subprogram Subp_Id will never have a body
1766
1767 function Is_Bridge_Target (Id : Entity_Id) return Boolean;
1768 pragma Inline (Is_Bridge_Target);
1769 -- Determine whether arbitrary entity Id denotes a bridge target
1770
1771 function Is_Controlled_Proc
1772 (Subp_Id : Entity_Id;
1773 Subp_Nam : Name_Id) return Boolean;
1774 pragma Inline (Is_Controlled_Proc);
1775 -- Determine whether subprogram Subp_Id denotes controlled type
1776 -- primitives Adjust, Finalize, or Initialize as denoted by name
1777 -- Subp_Nam.
1778
1779 function Is_Default_Initial_Condition_Proc
1780 (Id : Entity_Id) return Boolean;
1781 pragma Inline (Is_Default_Initial_Condition_Proc);
1782 -- Determine whether arbitrary entity Id denotes internally generated
1783 -- routine Default_Initial_Condition.
1784
1785 function Is_Finalizer_Proc (Id : Entity_Id) return Boolean;
1786 pragma Inline (Is_Finalizer_Proc);
1787 -- Determine whether arbitrary entity Id denotes internally generated
1788 -- routine _Finalizer.
1789
1790 function Is_Initial_Condition_Proc (Id : Entity_Id) return Boolean;
1791 pragma Inline (Is_Initial_Condition_Proc);
1792 -- Determine whether arbitrary entity Id denotes internally generated
1793 -- routine Initial_Condition.
1794
1795 function Is_Initialized (Obj_Decl : Node_Id) return Boolean;
1796 pragma Inline (Is_Initialized);
1797 -- Determine whether object declaration Obj_Decl is initialized
1798
1799 function Is_Invariant_Proc (Id : Entity_Id) return Boolean;
1800 pragma Inline (Is_Invariant_Proc);
1801 -- Determine whether arbitrary entity Id denotes an invariant procedure
1802
1803 function Is_Non_Library_Level_Encapsulator (N : Node_Id) return Boolean;
1804 pragma Inline (Is_Non_Library_Level_Encapsulator);
1805 -- Determine whether arbitrary node N is a non-library encapsulator
1806
1807 function Is_Partial_Invariant_Proc (Id : Entity_Id) return Boolean;
1808 pragma Inline (Is_Partial_Invariant_Proc);
1809 -- Determine whether arbitrary entity Id denotes a partial invariant
1810 -- procedure.
1811
1812 function Is_Postconditions_Proc (Id : Entity_Id) return Boolean;
1813 pragma Inline (Is_Postconditions_Proc);
1814 -- Determine whether arbitrary entity Id denotes internally generated
1815 -- routine _Postconditions.
1816
1817 function Is_Preelaborated_Unit (Id : Entity_Id) return Boolean;
1818 pragma Inline (Is_Preelaborated_Unit);
1819 -- Determine whether arbitrary entity Id denotes a unit which is subject
1820 -- to one of the following pragmas:
1821 --
1822 -- * Preelaborable
1823 -- * Pure
1824 -- * Remote_Call_Interface
1825 -- * Remote_Types
1826 -- * Shared_Passive
1827
1828 function Is_Protected_Entry (Id : Entity_Id) return Boolean;
1829 pragma Inline (Is_Protected_Entry);
1830 -- Determine whether arbitrary entity Id denotes a protected entry
1831
1832 function Is_Protected_Subp (Id : Entity_Id) return Boolean;
1833 pragma Inline (Is_Protected_Subp);
1834 -- Determine whether entity Id denotes a protected subprogram
1835
1836 function Is_Protected_Body_Subp (Id : Entity_Id) return Boolean;
1837 pragma Inline (Is_Protected_Body_Subp);
1838 -- Determine whether entity Id denotes the protected or unprotected
1839 -- version of a protected subprogram.
1840
1841 function Is_Scenario (N : Node_Id) return Boolean;
1842 pragma Inline (Is_Scenario);
1843 -- Determine whether attribute node N denotes a scenario. The scenario
1844 -- may not necessarily be eligible for ABE processing.
1845
1846 function Is_SPARK_Semantic_Target (Id : Entity_Id) return Boolean;
1847 pragma Inline (Is_SPARK_Semantic_Target);
1848 -- Determine whether arbitrary entity Id nodes a source or internally
1849 -- generated subprogram which emulates SPARK semantics.
1850
1851 function Is_Subprogram_Inst (Id : Entity_Id) return Boolean;
1852 pragma Inline (Is_Subprogram_Inst);
1853 -- Determine whether arbitrary entity Id denotes a subprogram instance
1854
1855 function Is_Suitable_Access_Taken (N : Node_Id) return Boolean;
1856 pragma Inline (Is_Suitable_Access_Taken);
1857 -- Determine whether arbitrary node N denotes a suitable attribute for
1858 -- ABE processing.
1859
1860 function Is_Suitable_Call (N : Node_Id) return Boolean;
1861 pragma Inline (Is_Suitable_Call);
1862 -- Determine whether arbitrary node N denotes a suitable call for ABE
1863 -- processing.
1864
1865 function Is_Suitable_Instantiation (N : Node_Id) return Boolean;
1866 pragma Inline (Is_Suitable_Instantiation);
1867 -- Determine whether arbitrary node N is a suitable instantiation for
1868 -- ABE processing.
1869
1870 function Is_Suitable_SPARK_Derived_Type (N : Node_Id) return Boolean;
1871 pragma Inline (Is_Suitable_SPARK_Derived_Type);
1872 -- Determine whether arbitrary node N denotes a suitable derived type
1873 -- declaration for ABE processing using the SPARK rules.
1874
1875 function Is_Suitable_SPARK_Instantiation (N : Node_Id) return Boolean;
1876 pragma Inline (Is_Suitable_SPARK_Instantiation);
1877 -- Determine whether arbitrary node N denotes a suitable instantiation
1878 -- for ABE processing using the SPARK rules.
1879
1880 function Is_Suitable_SPARK_Refined_State_Pragma
1881 (N : Node_Id) return Boolean;
1882 pragma Inline (Is_Suitable_SPARK_Refined_State_Pragma);
1883 -- Determine whether arbitrary node N denotes a suitable Refined_State
1884 -- pragma for ABE processing using the SPARK rules.
1885
1886 function Is_Suitable_Variable_Assignment (N : Node_Id) return Boolean;
1887 pragma Inline (Is_Suitable_Variable_Assignment);
1888 -- Determine whether arbitrary node N denotes a suitable assignment for
1889 -- ABE processing.
1890
1891 function Is_Suitable_Variable_Reference (N : Node_Id) return Boolean;
1892 pragma Inline (Is_Suitable_Variable_Reference);
1893 -- Determine whether arbitrary node N is a suitable variable reference
1894 -- for ABE processing.
1895
1896 function Is_Task_Entry (Id : Entity_Id) return Boolean;
1897 pragma Inline (Is_Task_Entry);
1898 -- Determine whether arbitrary entity Id denotes a task entry
1899
1900 function Is_Up_Level_Target
1901 (Targ_Decl : Node_Id;
1902 In_State : Processing_In_State) return Boolean;
1903 pragma Inline (Is_Up_Level_Target);
1904 -- Determine whether the current root resides at the declaration level.
1905 -- If this is the case, determine whether a target with by declaration
1906 -- Target_Decl is within a context which encloses the current root or is
1907 -- in a different unit. In_State is the current state of the Processing
1908 -- phase.
1909
1910 end Semantics;
1911 use Semantics;
1912
1913 -- The following package provides the main entry point for SPARK-related
1914 -- checks and diagnostics.
1915
1916 package SPARK_Processor is
1917
1918 ---------
1919 -- API --
1920 ---------
1921
1922 procedure Check_SPARK_Model_In_Effect;
1923 pragma Inline (Check_SPARK_Model_In_Effect);
1924 -- Determine whether a suitable elaboration model is currently in effect
1925 -- for verifying SPARK rules. Emit a warning if this is not the case.
1926
1927 procedure Check_SPARK_Scenarios;
1928 pragma Inline (Check_SPARK_Scenarios);
1929 -- Examine SPARK scenarios which are not necessarily executable during
1930 -- elaboration, but still requires elaboration-related checks.
1931
1932 end SPARK_Processor;
1933 use SPARK_Processor;
1934
1935 -----------------------
1936 -- Local subprograms --
1937 -----------------------
1938
1939 function Assignment_Target (Asmt : Node_Id) return Node_Id;
1940 pragma Inline (Assignment_Target);
1941 -- Obtain the target of assignment statement Asmt
1942
1943 function Call_Name (Call : Node_Id) return Node_Id;
1944 pragma Inline (Call_Name);
1945 -- Obtain the name of an entry, operator, or subprogram call Call
1946
1947 function Canonical_Subprogram (Subp_Id : Entity_Id) return Entity_Id;
1948 pragma Inline (Canonical_Subprogram);
1949 -- Obtain the uniform canonical entity of subprogram Subp_Id
1950
1951 function Compilation_Unit (Unit_Id : Entity_Id) return Node_Id;
1952 pragma Inline (Compilation_Unit);
1953 -- Return the N_Compilation_Unit node of unit Unit_Id
1954
1955 function Find_Enclosing_Instance (N : Node_Id) return Node_Id;
1956 pragma Inline (Find_Enclosing_Instance);
1957 -- Find the declaration or body of the nearest expanded instance which
1958 -- encloses arbitrary node N. Return Empty if no such instance exists.
1959
1960 function Find_Top_Unit (N : Node_Or_Entity_Id) return Entity_Id;
1961 pragma Inline (Find_Top_Unit);
1962 -- Return the top unit which contains arbitrary node or entity N. The unit
1963 -- is obtained by logically unwinding instantiations and subunits when N
1964 -- resides within one.
1965
1966 function Find_Unit_Entity (N : Node_Id) return Entity_Id;
1967 pragma Inline (Find_Unit_Entity);
1968 -- Return the entity of unit N
1969
1970 function First_Formal_Type (Subp_Id : Entity_Id) return Entity_Id;
1971 pragma Inline (First_Formal_Type);
1972 -- Return the type of subprogram Subp_Id's first formal parameter. If the
1973 -- subprogram lacks formal parameters, return Empty.
1974
1975 function Elaboration_Phase_Active return Boolean;
1976 pragma Inline (Elaboration_Phase_Active);
1977 -- Determine whether the elaboration phase of the compilation has started
1978
1979 procedure Finalize_All_Data_Structures;
1980 pragma Inline (Finalize_All_Data_Structures);
1981 -- Destroy all internal data structures
1982
1983 function Has_Body (Pack_Decl : Node_Id) return Boolean;
1984 pragma Inline (Has_Body);
1985 -- Determine whether package declaration Pack_Decl has a corresponding body
1986 -- or would eventually have one.
1987
1988 function In_External_Instance
1989 (N : Node_Id;
1990 Target_Decl : Node_Id) return Boolean;
1991 pragma Inline (In_External_Instance);
1992 -- Determine whether a target desctibed by its declaration Target_Decl
1993 -- resides in a package instance which is external to scenario N.
1994
1995 function In_Main_Context (N : Node_Id) return Boolean;
1996 pragma Inline (In_Main_Context);
1997 -- Determine whether arbitrary node N appears within the main compilation
1998 -- unit.
1999
2000 function In_Same_Context
2001 (N1 : Node_Id;
2002 N2 : Node_Id;
2003 Nested_OK : Boolean := False) return Boolean;
2004 pragma Inline (In_Same_Context);
2005 -- Determine whether two arbitrary nodes N1 and N2 appear within the same
2006 -- context ignoring enclosing library levels. Nested_OK should be set when
2007 -- the context of N1 can enclose that of N2.
2008
2009 procedure Initialize_All_Data_Structures;
2010 pragma Inline (Initialize_All_Data_Structures);
2011 -- Create all internal data structures
2012
2013 function Instantiated_Generic (Inst : Node_Id) return Entity_Id;
2014 pragma Inline (Instantiated_Generic);
2015 -- Obtain the generic instantiated by instance Inst
2016
2017 function Is_Safe_Activation
2018 (Call : Node_Id;
2019 Task_Rep : Target_Rep_Id) return Boolean;
2020 pragma Inline (Is_Safe_Activation);
2021 -- Determine whether activation call Call which activates an object of a
2022 -- task type described by representation Task_Rep is always ABE-safe.
2023
2024 function Is_Safe_Call
2025 (Call : Node_Id;
2026 Subp_Id : Entity_Id;
2027 Subp_Rep : Target_Rep_Id) return Boolean;
2028 pragma Inline (Is_Safe_Call);
2029 -- Determine whether call Call which invokes entry, operator, or subprogram
2030 -- Subp_Id is always ABE-safe. Subp_Rep is the representation of the entry,
2031 -- operator, or subprogram.
2032
2033 function Is_Safe_Instantiation
2034 (Inst : Node_Id;
2035 Gen_Id : Entity_Id;
2036 Gen_Rep : Target_Rep_Id) return Boolean;
2037 pragma Inline (Is_Safe_Instantiation);
2038 -- Determine whether instantiation Inst which instantiates generic Gen_Id
2039 -- is always ABE-safe. Gen_Rep is the representation of the generic.
2040
2041 function Is_Same_Unit
2042 (Unit_1 : Entity_Id;
2043 Unit_2 : Entity_Id) return Boolean;
2044 pragma Inline (Is_Same_Unit);
2045 -- Determine whether entities Unit_1 and Unit_2 denote the same unit
2046
2047 function Main_Unit_Entity return Entity_Id;
2048 pragma Inline (Main_Unit_Entity);
2049 -- Return the entity of the main unit
2050
2051 function Non_Private_View (Typ : Entity_Id) return Entity_Id;
2052 pragma Inline (Non_Private_View);
2053 -- Return the full view of private type Typ if available, otherwise return
2054 -- type Typ.
2055
2056 function Scenario (N : Node_Id) return Node_Id;
2057 pragma Inline (Scenario);
2058 -- Return the appropriate scenario node for scenario N
2059
2060 procedure Set_Elaboration_Phase (Status : Elaboration_Phase_Status);
2061 pragma Inline (Set_Elaboration_Phase);
2062 -- Change the status of the elaboration phase of the compiler to Status
2063
2064 procedure Spec_And_Body_From_Entity
2065 (Id : Node_Id;
2066 Spec_Decl : out Node_Id;
2067 Body_Decl : out Node_Id);
2068 pragma Inline (Spec_And_Body_From_Entity);
2069 -- Given arbitrary entity Id representing a construct with a spec and body,
2070 -- retrieve declaration of the spec in Spec_Decl and the declaration of the
2071 -- body in Body_Decl.
2072
2073 procedure Spec_And_Body_From_Node
2074 (N : Node_Id;
2075 Spec_Decl : out Node_Id;
2076 Body_Decl : out Node_Id);
2077 pragma Inline (Spec_And_Body_From_Node);
2078 -- Given arbitrary node N representing a construct with a spec and body,
2079 -- retrieve declaration of the spec in Spec_Decl and the declaration of
2080 -- the body in Body_Decl.
2081
2082 function Static_Elaboration_Checks return Boolean;
2083 pragma Inline (Static_Elaboration_Checks);
2084 -- Determine whether the static model is in effect
2085
2086 function Unit_Entity (Unit_Id : Entity_Id) return Entity_Id;
2087 pragma Inline (Unit_Entity);
2088 -- Return the entity of the initial declaration for unit Unit_Id
2089
2090 procedure Update_Elaboration_Scenario (New_N : Node_Id; Old_N : Node_Id);
2091 pragma Inline (Update_Elaboration_Scenario);
2092 -- Update all relevant internal data structures when scenario Old_N is
2093 -- transformed into scenario New_N by Atree.Rewrite.
2094
2095 ----------------------
2096 -- Active_Scenarios --
2097 ----------------------
2098
2099 package body Active_Scenarios is
2100
2101 -----------------------
2102 -- Local subprograms --
2103 -----------------------
2104
2105 procedure Output_Access_Taken
2106 (Attr : Node_Id;
2107 Attr_Rep : Scenario_Rep_Id;
2108 Error_Nod : Node_Id);
2109 pragma Inline (Output_Access_Taken);
2110 -- Emit a specific diagnostic message for 'Access attribute reference
2111 -- Attr with representation Attr_Rep. The message is associated with
2112 -- node Error_Nod.
2113
2114 procedure Output_Active_Scenario
2115 (N : Node_Id;
2116 Error_Nod : Node_Id;
2117 In_State : Processing_In_State);
2118 pragma Inline (Output_Active_Scenario);
2119 -- Top level dispatcher for outputting a scenario. Emit a specific
2120 -- diagnostic message for scenario N. The message is associated with
2121 -- node Error_Nod. In_State is the current state of the Processing
2122 -- phase.
2123
2124 procedure Output_Call
2125 (Call : Node_Id;
2126 Call_Rep : Scenario_Rep_Id;
2127 Error_Nod : Node_Id);
2128 pragma Inline (Output_Call);
2129 -- Emit a diagnostic message for call Call with representation Call_Rep.
2130 -- The message is associated with node Error_Nod.
2131
2132 procedure Output_Header (Error_Nod : Node_Id);
2133 pragma Inline (Output_Header);
2134 -- Emit a specific diagnostic message for the unit of the root scenario.
2135 -- The message is associated with node Error_Nod.
2136
2137 procedure Output_Instantiation
2138 (Inst : Node_Id;
2139 Inst_Rep : Scenario_Rep_Id;
2140 Error_Nod : Node_Id);
2141 pragma Inline (Output_Instantiation);
2142 -- Emit a specific diagnostic message for instantiation Inst with
2143 -- representation Inst_Rep. The message is associated with node
2144 -- Error_Nod.
2145
2146 procedure Output_Refined_State_Pragma
2147 (Prag : Node_Id;
2148 Prag_Rep : Scenario_Rep_Id;
2149 Error_Nod : Node_Id);
2150 pragma Inline (Output_Refined_State_Pragma);
2151 -- Emit a specific diagnostic message for Refined_State pragma Prag
2152 -- with representation Prag_Rep. The message is associated with node
2153 -- Error_Nod.
2154
2155 procedure Output_Task_Activation
2156 (Call : Node_Id;
2157 Call_Rep : Scenario_Rep_Id;
2158 Error_Nod : Node_Id);
2159 pragma Inline (Output_Task_Activation);
2160 -- Emit a specific diagnostic message for activation call Call
2161 -- with representation Call_Rep. The message is associated with
2162 -- node Error_Nod.
2163
2164 procedure Output_Variable_Assignment
2165 (Asmt : Node_Id;
2166 Asmt_Rep : Scenario_Rep_Id;
2167 Error_Nod : Node_Id);
2168 pragma Inline (Output_Variable_Assignment);
2169 -- Emit a specific diagnostic message for assignment statement Asmt
2170 -- with representation Asmt_Rep. The message is associated with node
2171 -- Error_Nod.
2172
2173 procedure Output_Variable_Reference
2174 (Ref : Node_Id;
2175 Ref_Rep : Scenario_Rep_Id;
2176 Error_Nod : Node_Id);
2177 pragma Inline (Output_Variable_Reference);
2178 -- Emit a specific diagnostic message for read reference Ref with
2179 -- representation Ref_Rep. The message is associated with node
2180 -- Error_Nod.
2181
2182 -------------------
2183 -- Output_Access --
2184 -------------------
2185
2186 procedure Output_Access_Taken
2187 (Attr : Node_Id;
2188 Attr_Rep : Scenario_Rep_Id;
2189 Error_Nod : Node_Id)
2190 is
2191 Subp_Id : constant Entity_Id := Target (Attr_Rep);
2192
2193 begin
2194 Error_Msg_Name_1 := Attribute_Name (Attr);
2195 Error_Msg_Sloc := Sloc (Attr);
2196 Error_Msg_NE ("\\ % of & taken #", Error_Nod, Subp_Id);
2197 end Output_Access_Taken;
2198
2199 ----------------------------
2200 -- Output_Active_Scenario --
2201 ----------------------------
2202
2203 procedure Output_Active_Scenario
2204 (N : Node_Id;
2205 Error_Nod : Node_Id;
2206 In_State : Processing_In_State)
2207 is
2208 Scen : constant Node_Id := Scenario (N);
2209 Scen_Rep : Scenario_Rep_Id;
2210
2211 begin
2212 -- 'Access
2213
2214 if Is_Suitable_Access_Taken (Scen) then
2215 Output_Access_Taken
2216 (Attr => Scen,
2217 Attr_Rep => Scenario_Representation_Of (Scen, In_State),
2218 Error_Nod => Error_Nod);
2219
2220 -- Call or task activation
2221
2222 elsif Is_Suitable_Call (Scen) then
2223 Scen_Rep := Scenario_Representation_Of (Scen, In_State);
2224
2225 if Kind (Scen_Rep) = Call_Scenario then
2226 Output_Call
2227 (Call => Scen,
2228 Call_Rep => Scen_Rep,
2229 Error_Nod => Error_Nod);
2230
2231 else
2232 pragma Assert (Kind (Scen_Rep) = Task_Activation_Scenario);
2233
2234 Output_Task_Activation
2235 (Call => Scen,
2236 Call_Rep => Scen_Rep,
2237 Error_Nod => Error_Nod);
2238 end if;
2239
2240 -- Instantiation
2241
2242 elsif Is_Suitable_Instantiation (Scen) then
2243 Output_Instantiation
2244 (Inst => Scen,
2245 Inst_Rep => Scenario_Representation_Of (Scen, In_State),
2246 Error_Nod => Error_Nod);
2247
2248 -- Pragma Refined_State
2249
2250 elsif Is_Suitable_SPARK_Refined_State_Pragma (Scen) then
2251 Output_Refined_State_Pragma
2252 (Prag => Scen,
2253 Prag_Rep => Scenario_Representation_Of (Scen, In_State),
2254 Error_Nod => Error_Nod);
2255
2256 -- Variable assignment
2257
2258 elsif Is_Suitable_Variable_Assignment (Scen) then
2259 Output_Variable_Assignment
2260 (Asmt => Scen,
2261 Asmt_Rep => Scenario_Representation_Of (Scen, In_State),
2262 Error_Nod => Error_Nod);
2263
2264 -- Variable reference
2265
2266 elsif Is_Suitable_Variable_Reference (Scen) then
2267 Output_Variable_Reference
2268 (Ref => Scen,
2269 Ref_Rep => Scenario_Representation_Of (Scen, In_State),
2270 Error_Nod => Error_Nod);
2271 end if;
2272 end Output_Active_Scenario;
2273
2274 -----------------------------
2275 -- Output_Active_Scenarios --
2276 -----------------------------
2277
2278 procedure Output_Active_Scenarios
2279 (Error_Nod : Node_Id;
2280 In_State : Processing_In_State)
2281 is
2282 package Scenarios renames Active_Scenario_Stack;
2283
2284 Header_Posted : Boolean := False;
2285
2286 begin
2287 -- Output the contents of the active scenario stack starting from the
2288 -- bottom, or the least recent scenario.
2289
2290 for Index in Scenarios.First .. Scenarios.Last loop
2291 if not Header_Posted then
2292 Output_Header (Error_Nod);
2293 Header_Posted := True;
2294 end if;
2295
2296 Output_Active_Scenario
2297 (N => Scenarios.Table (Index),
2298 Error_Nod => Error_Nod,
2299 In_State => In_State);
2300 end loop;
2301 end Output_Active_Scenarios;
2302
2303 -----------------
2304 -- Output_Call --
2305 -----------------
2306
2307 procedure Output_Call
2308 (Call : Node_Id;
2309 Call_Rep : Scenario_Rep_Id;
2310 Error_Nod : Node_Id)
2311 is
2312 procedure Output_Accept_Alternative (Alt_Id : Entity_Id);
2313 pragma Inline (Output_Accept_Alternative);
2314 -- Emit a specific diagnostic message concerning accept alternative
2315 -- with entity Alt_Id.
2316
2317 procedure Output_Call (Subp_Id : Entity_Id; Kind : String);
2318 pragma Inline (Output_Call);
2319 -- Emit a specific diagnostic message concerning a call of kind Kind
2320 -- which invokes subprogram Subp_Id.
2321
2322 procedure Output_Type_Actions (Subp_Id : Entity_Id; Action : String);
2323 pragma Inline (Output_Type_Actions);
2324 -- Emit a specific diagnostic message concerning action Action of a
2325 -- type performed by subprogram Subp_Id.
2326
2327 procedure Output_Verification_Call
2328 (Pred : String;
2329 Id : Entity_Id;
2330 Id_Kind : String);
2331 pragma Inline (Output_Verification_Call);
2332 -- Emit a specific diagnostic message concerning the verification of
2333 -- predicate Pred applied to related entity Id with kind Id_Kind.
2334
2335 -------------------------------
2336 -- Output_Accept_Alternative --
2337 -------------------------------
2338
2339 procedure Output_Accept_Alternative (Alt_Id : Entity_Id) is
2340 Entry_Id : constant Entity_Id := Receiving_Entry (Alt_Id);
2341
2342 begin
2343 pragma Assert (Present (Entry_Id));
2344
2345 Error_Msg_NE ("\\ entry & selected #", Error_Nod, Entry_Id);
2346 end Output_Accept_Alternative;
2347
2348 -----------------
2349 -- Output_Call --
2350 -----------------
2351
2352 procedure Output_Call (Subp_Id : Entity_Id; Kind : String) is
2353 begin
2354 Error_Msg_NE ("\\ " & Kind & " & called #", Error_Nod, Subp_Id);
2355 end Output_Call;
2356
2357 -------------------------
2358 -- Output_Type_Actions --
2359 -------------------------
2360
2361 procedure Output_Type_Actions
2362 (Subp_Id : Entity_Id;
2363 Action : String)
2364 is
2365 Typ : constant Entity_Id := First_Formal_Type (Subp_Id);
2366
2367 begin
2368 pragma Assert (Present (Typ));
2369
2370 Error_Msg_NE
2371 ("\\ " & Action & " actions for type & #", Error_Nod, Typ);
2372 end Output_Type_Actions;
2373
2374 ------------------------------
2375 -- Output_Verification_Call --
2376 ------------------------------
2377
2378 procedure Output_Verification_Call
2379 (Pred : String;
2380 Id : Entity_Id;
2381 Id_Kind : String)
2382 is
2383 begin
2384 pragma Assert (Present (Id));
2385
2386 Error_Msg_NE
2387 ("\\ " & Pred & " of " & Id_Kind & " & verified #",
2388 Error_Nod, Id);
2389 end Output_Verification_Call;
2390
2391 -- Local variables
2392
2393 Subp_Id : constant Entity_Id := Target (Call_Rep);
2394
2395 -- Start of processing for Output_Call
2396
2397 begin
2398 Error_Msg_Sloc := Sloc (Call);
2399
2400 -- Accept alternative
2401
2402 if Is_Accept_Alternative_Proc (Subp_Id) then
2403 Output_Accept_Alternative (Subp_Id);
2404
2405 -- Adjustment
2406
2407 elsif Is_TSS (Subp_Id, TSS_Deep_Adjust) then
2408 Output_Type_Actions (Subp_Id, "adjustment");
2409
2410 -- Default_Initial_Condition
2411
2412 elsif Is_Default_Initial_Condition_Proc (Subp_Id) then
2413 Output_Verification_Call
2414 (Pred => "Default_Initial_Condition",
2415 Id => First_Formal_Type (Subp_Id),
2416 Id_Kind => "type");
2417
2418 -- Entries
2419
2420 elsif Is_Protected_Entry (Subp_Id) then
2421 Output_Call (Subp_Id, "entry");
2422
2423 -- Task entry calls are never processed because the entry being
2424 -- invoked does not have a corresponding "body", it has a select. A
2425 -- task entry call appears in the stack of active scenarios for the
2426 -- sole purpose of checking No_Entry_Calls_In_Elaboration_Code and
2427 -- nothing more.
2428
2429 elsif Is_Task_Entry (Subp_Id) then
2430 null;
2431
2432 -- Finalization
2433
2434 elsif Is_TSS (Subp_Id, TSS_Deep_Finalize) then
2435 Output_Type_Actions (Subp_Id, "finalization");
2436
2437 -- Calls to _Finalizer procedures must not appear in the output
2438 -- because this creates confusing noise.
2439
2440 elsif Is_Finalizer_Proc (Subp_Id) then
2441 null;
2442
2443 -- Initial_Condition
2444
2445 elsif Is_Initial_Condition_Proc (Subp_Id) then
2446 Output_Verification_Call
2447 (Pred => "Initial_Condition",
2448 Id => Find_Enclosing_Scope (Call),
2449 Id_Kind => "package");
2450
2451 -- Initialization
2452
2453 elsif Is_Init_Proc (Subp_Id)
2454 or else Is_TSS (Subp_Id, TSS_Deep_Initialize)
2455 then
2456 Output_Type_Actions (Subp_Id, "initialization");
2457
2458 -- Invariant
2459
2460 elsif Is_Invariant_Proc (Subp_Id) then
2461 Output_Verification_Call
2462 (Pred => "invariants",
2463 Id => First_Formal_Type (Subp_Id),
2464 Id_Kind => "type");
2465
2466 -- Partial invariant calls must not appear in the output because this
2467 -- creates confusing noise. Note that a partial invariant is always
2468 -- invoked by the "full" invariant which is already placed on the
2469 -- stack.
2470
2471 elsif Is_Partial_Invariant_Proc (Subp_Id) then
2472 null;
2473
2474 -- _Postconditions
2475
2476 elsif Is_Postconditions_Proc (Subp_Id) then
2477 Output_Verification_Call
2478 (Pred => "postconditions",
2479 Id => Find_Enclosing_Scope (Call),
2480 Id_Kind => "subprogram");
2481
2482 -- Subprograms must come last because some of the previous cases fall
2483 -- under this category.
2484
2485 elsif Ekind (Subp_Id) = E_Function then
2486 Output_Call (Subp_Id, "function");
2487
2488 elsif Ekind (Subp_Id) = E_Procedure then
2489 Output_Call (Subp_Id, "procedure");
2490
2491 else
2492 pragma Assert (False);
2493 return;
2494 end if;
2495 end Output_Call;
2496
2497 -------------------
2498 -- Output_Header --
2499 -------------------
2500
2501 procedure Output_Header (Error_Nod : Node_Id) is
2502 Unit_Id : constant Entity_Id := Find_Top_Unit (Root_Scenario);
2503
2504 begin
2505 if Ekind (Unit_Id) = E_Package then
2506 Error_Msg_NE ("\\ spec of unit & elaborated", Error_Nod, Unit_Id);
2507
2508 elsif Ekind (Unit_Id) = E_Package_Body then
2509 Error_Msg_NE ("\\ body of unit & elaborated", Error_Nod, Unit_Id);
2510
2511 else
2512 Error_Msg_NE ("\\ in body of unit &", Error_Nod, Unit_Id);
2513 end if;
2514 end Output_Header;
2515
2516 --------------------------
2517 -- Output_Instantiation --
2518 --------------------------
2519
2520 procedure Output_Instantiation
2521 (Inst : Node_Id;
2522 Inst_Rep : Scenario_Rep_Id;
2523 Error_Nod : Node_Id)
2524 is
2525 procedure Output_Instantiation (Gen_Id : Entity_Id; Kind : String);
2526 pragma Inline (Output_Instantiation);
2527 -- Emit a specific diagnostic message concerning an instantiation of
2528 -- generic unit Gen_Id. Kind denotes the kind of the instantiation.
2529
2530 --------------------------
2531 -- Output_Instantiation --
2532 --------------------------
2533
2534 procedure Output_Instantiation (Gen_Id : Entity_Id; Kind : String) is
2535 begin
2536 Error_Msg_NE
2537 ("\\ " & Kind & " & instantiated as & #", Error_Nod, Gen_Id);
2538 end Output_Instantiation;
2539
2540 -- Local variables
2541
2542 Gen_Id : constant Entity_Id := Target (Inst_Rep);
2543
2544 -- Start of processing for Output_Instantiation
2545
2546 begin
2547 Error_Msg_Node_2 := Defining_Entity (Inst);
2548 Error_Msg_Sloc := Sloc (Inst);
2549
2550 if Nkind (Inst) = N_Function_Instantiation then
2551 Output_Instantiation (Gen_Id, "function");
2552
2553 elsif Nkind (Inst) = N_Package_Instantiation then
2554 Output_Instantiation (Gen_Id, "package");
2555
2556 elsif Nkind (Inst) = N_Procedure_Instantiation then
2557 Output_Instantiation (Gen_Id, "procedure");
2558
2559 else
2560 pragma Assert (False);
2561 return;
2562 end if;
2563 end Output_Instantiation;
2564
2565 ---------------------------------
2566 -- Output_Refined_State_Pragma --
2567 ---------------------------------
2568
2569 procedure Output_Refined_State_Pragma
2570 (Prag : Node_Id;
2571 Prag_Rep : Scenario_Rep_Id;
2572 Error_Nod : Node_Id)
2573 is
2574 pragma Unreferenced (Prag_Rep);
2575
2576 begin
2577 Error_Msg_Sloc := Sloc (Prag);
2578 Error_Msg_N ("\\ refinement constituents read #", Error_Nod);
2579 end Output_Refined_State_Pragma;
2580
2581 ----------------------------
2582 -- Output_Task_Activation --
2583 ----------------------------
2584
2585 procedure Output_Task_Activation
2586 (Call : Node_Id;
2587 Call_Rep : Scenario_Rep_Id;
2588 Error_Nod : Node_Id)
2589 is
2590 pragma Unreferenced (Call_Rep);
2591
2592 function Find_Activator return Entity_Id;
2593 -- Find the nearest enclosing construct which houses call Call
2594
2595 --------------------
2596 -- Find_Activator --
2597 --------------------
2598
2599 function Find_Activator return Entity_Id is
2600 Par : Node_Id;
2601
2602 begin
2603 -- Climb the parent chain looking for a package [body] or a
2604 -- construct with a statement sequence.
2605
2606 Par := Parent (Call);
2607 while Present (Par) loop
2608 if Nkind_In (Par, N_Package_Body, N_Package_Declaration) then
2609 return Defining_Entity (Par);
2610
2611 elsif Nkind (Par) = N_Handled_Sequence_Of_Statements then
2612 return Defining_Entity (Parent (Par));
2613 end if;
2614
2615 Par := Parent (Par);
2616 end loop;
2617
2618 return Empty;
2619 end Find_Activator;
2620
2621 -- Local variables
2622
2623 Activator : constant Entity_Id := Find_Activator;
2624
2625 -- Start of processing for Output_Task_Activation
2626
2627 begin
2628 pragma Assert (Present (Activator));
2629
2630 Error_Msg_NE ("\\ local tasks of & activated", Error_Nod, Activator);
2631 end Output_Task_Activation;
2632
2633 --------------------------------
2634 -- Output_Variable_Assignment --
2635 --------------------------------
2636
2637 procedure Output_Variable_Assignment
2638 (Asmt : Node_Id;
2639 Asmt_Rep : Scenario_Rep_Id;
2640 Error_Nod : Node_Id)
2641 is
2642 Var_Id : constant Entity_Id := Target (Asmt_Rep);
2643
2644 begin
2645 Error_Msg_Sloc := Sloc (Asmt);
2646 Error_Msg_NE ("\\ variable & assigned #", Error_Nod, Var_Id);
2647 end Output_Variable_Assignment;
2648
2649 -------------------------------
2650 -- Output_Variable_Reference --
2651 -------------------------------
2652
2653 procedure Output_Variable_Reference
2654 (Ref : Node_Id;
2655 Ref_Rep : Scenario_Rep_Id;
2656 Error_Nod : Node_Id)
2657 is
2658 Var_Id : constant Entity_Id := Target (Ref_Rep);
2659
2660 begin
2661 Error_Msg_Sloc := Sloc (Ref);
2662 Error_Msg_NE ("\\ variable & read #", Error_Nod, Var_Id);
2663 end Output_Variable_Reference;
2664
2665 -------------------------
2666 -- Pop_Active_Scenario --
2667 -------------------------
2668
2669 procedure Pop_Active_Scenario (N : Node_Id) is
2670 package Scenarios renames Active_Scenario_Stack;
2671 Top : Node_Id renames Scenarios.Table (Scenarios.Last);
2672
2673 begin
2674 pragma Assert (Top = N);
2675 Scenarios.Decrement_Last;
2676 end Pop_Active_Scenario;
2677
2678 --------------------------
2679 -- Push_Active_Scenario --
2680 --------------------------
2681
2682 procedure Push_Active_Scenario (N : Node_Id) is
2683 begin
2684 Active_Scenario_Stack.Append (N);
2685 end Push_Active_Scenario;
2686
2687 -------------------
2688 -- Root_Scenario --
2689 -------------------
2690
2691 function Root_Scenario return Node_Id is
2692 package Scenarios renames Active_Scenario_Stack;
2693
2694 begin
2695 -- Ensure that the scenario stack has at least one active scenario in
2696 -- it. The one at the bottom (index First) is the root scenario.
2697
2698 pragma Assert (Scenarios.Last >= Scenarios.First);
2699 return Scenarios.Table (Scenarios.First);
2700 end Root_Scenario;
2701 end Active_Scenarios;
2702
2703 --------------------------
2704 -- Activation_Processor --
2705 --------------------------
2706
2707 package body Activation_Processor is
2708
2709 ------------------------
2710 -- Process_Activation --
2711 ------------------------
2712
2713 procedure Process_Activation
2714 (Call : Node_Id;
2715 Call_Rep : Scenario_Rep_Id;
2716 Processor : Activation_Processor_Ptr;
2717 In_State : Processing_In_State)
2718 is
2719 procedure Process_Task_Object (Obj_Id : Entity_Id; Typ : Entity_Id);
2720 pragma Inline (Process_Task_Object);
2721 -- Invoke Processor for task object Obj_Id of type Typ
2722
2723 procedure Process_Task_Objects
2724 (Task_Objs : NE_List.Doubly_Linked_List);
2725 pragma Inline (Process_Task_Objects);
2726 -- Invoke Processor for all task objects found in list Task_Objs
2727
2728 procedure Traverse_List
2729 (List : List_Id;
2730 Task_Objs : NE_List.Doubly_Linked_List);
2731 pragma Inline (Traverse_List);
2732 -- Traverse declarative or statement list List while searching for
2733 -- objects of a task type, or containing task components. If such an
2734 -- object is found, first save it in list Task_Objs and then invoke
2735 -- Processor on it.
2736
2737 -------------------------
2738 -- Process_Task_Object --
2739 -------------------------
2740
2741 procedure Process_Task_Object (Obj_Id : Entity_Id; Typ : Entity_Id) is
2742 Root_Typ : constant Entity_Id :=
2743 Non_Private_View (Root_Type (Typ));
2744 Comp_Id : Entity_Id;
2745 Obj_Rep : Target_Rep_Id;
2746 Root_Rep : Target_Rep_Id;
2747
2748 New_In_State : Processing_In_State := In_State;
2749 -- Each step of the Processing phase constitutes a new state
2750
2751 begin
2752 if Is_Task_Type (Typ) then
2753 Obj_Rep := Target_Representation_Of (Obj_Id, New_In_State);
2754 Root_Rep := Target_Representation_Of (Root_Typ, New_In_State);
2755
2756 -- Warnings are suppressed when a prior scenario is already in
2757 -- that mode, or when the object, activation call, or task type
2758 -- have warnings suppressed. Update the state of the Processing
2759 -- phase to reflect this.
2760
2761 New_In_State.Suppress_Warnings :=
2762 New_In_State.Suppress_Warnings
2763 or else not Elaboration_Warnings_OK (Call_Rep)
2764 or else not Elaboration_Warnings_OK (Obj_Rep)
2765 or else not Elaboration_Warnings_OK (Root_Rep);
2766
2767 -- Update the state of the Processing phase to indicate that
2768 -- any further traversal is now within a task body.
2769
2770 New_In_State.Within_Task_Body := True;
2771
2772 -- Associate the current task type with the activation call
2773
2774 Set_Activated_Task_Type (Call_Rep, Root_Typ);
2775
2776 -- Process the activation of the current task object by calling
2777 -- the supplied processor.
2778
2779 Processor.all
2780 (Call => Call,
2781 Call_Rep => Call_Rep,
2782 Obj_Id => Obj_Id,
2783 Obj_Rep => Obj_Rep,
2784 Task_Typ => Root_Typ,
2785 Task_Rep => Root_Rep,
2786 In_State => New_In_State);
2787
2788 -- Reset the association between the current task and the
2789 -- activtion call.
2790
2791 Set_Activated_Task_Type (Call_Rep, Empty);
2792
2793 -- Examine the component type when the object is an array
2794
2795 elsif Is_Array_Type (Typ) and then Has_Task (Root_Typ) then
2796 Process_Task_Object
2797 (Obj_Id => Obj_Id,
2798 Typ => Component_Type (Typ));
2799
2800 -- Examine individual component types when the object is a record
2801
2802 elsif Is_Record_Type (Typ) and then Has_Task (Root_Typ) then
2803 Comp_Id := First_Component (Typ);
2804 while Present (Comp_Id) loop
2805 Process_Task_Object
2806 (Obj_Id => Obj_Id,
2807 Typ => Etype (Comp_Id));
2808
2809 Next_Component (Comp_Id);
2810 end loop;
2811 end if;
2812 end Process_Task_Object;
2813
2814 --------------------------
2815 -- Process_Task_Objects --
2816 --------------------------
2817
2818 procedure Process_Task_Objects
2819 (Task_Objs : NE_List.Doubly_Linked_List)
2820 is
2821 Iter : NE_List.Iterator;
2822 Obj_Id : Entity_Id;
2823
2824 begin
2825 Iter := NE_List.Iterate (Task_Objs);
2826 while NE_List.Has_Next (Iter) loop
2827 NE_List.Next (Iter, Obj_Id);
2828
2829 Process_Task_Object
2830 (Obj_Id => Obj_Id,
2831 Typ => Etype (Obj_Id));
2832 end loop;
2833 end Process_Task_Objects;
2834
2835 -------------------
2836 -- Traverse_List --
2837 -------------------
2838
2839 procedure Traverse_List
2840 (List : List_Id;
2841 Task_Objs : NE_List.Doubly_Linked_List)
2842 is
2843 Item : Node_Id;
2844 Item_Id : Entity_Id;
2845 Item_Typ : Entity_Id;
2846
2847 begin
2848 -- Examine the contents of the list looking for an object
2849 -- declaration of a task type or one that contains a task
2850 -- within.
2851
2852 Item := First (List);
2853 while Present (Item) loop
2854 if Nkind (Item) = N_Object_Declaration then
2855 Item_Id := Defining_Entity (Item);
2856 Item_Typ := Etype (Item_Id);
2857
2858 if Has_Task (Item_Typ) then
2859
2860 -- The object is either of a task type, or contains a
2861 -- task component. Save it in the list of task objects
2862 -- associated with the activation call.
2863
2864 NE_List.Append (Task_Objs, Item_Id);
2865
2866 Process_Task_Object
2867 (Obj_Id => Item_Id,
2868 Typ => Item_Typ);
2869 end if;
2870 end if;
2871
2872 Next (Item);
2873 end loop;
2874 end Traverse_List;
2875
2876 -- Local variables
2877
2878 Context : Node_Id;
2879 Spec : Node_Id;
2880 Task_Objs : NE_List.Doubly_Linked_List;
2881
2882 -- Start of processing for Process_Activation
2883
2884 begin
2885 -- Nothing to do when the activation is a guaranteed ABE
2886
2887 if Is_Known_Guaranteed_ABE (Call) then
2888 return;
2889 end if;
2890
2891 Task_Objs := Activated_Task_Objects (Call_Rep);
2892
2893 -- The activation call has been processed at least once, and all
2894 -- task objects have already been collected. Directly process the
2895 -- objects without having to reexamine the context of the call.
2896
2897 if NE_List.Present (Task_Objs) then
2898 Process_Task_Objects (Task_Objs);
2899
2900 -- Otherwise the activation call is being processed for the first
2901 -- time. Collect all task objects in case the call is reprocessed
2902 -- multiple times.
2903
2904 else
2905 Task_Objs := NE_List.Create;
2906 Set_Activated_Task_Objects (Call_Rep, Task_Objs);
2907
2908 -- Find the context of the activation call where all task objects
2909 -- being activated are declared. This is usually the parent of the
2910 -- call.
2911
2912 Context := Parent (Call);
2913
2914 -- Handle the case where the activation call appears within the
2915 -- handled statements of a block or a body.
2916
2917 if Nkind (Context) = N_Handled_Sequence_Of_Statements then
2918 Context := Parent (Context);
2919 end if;
2920
2921 -- Process all task objects in both the spec and body when the
2922 -- activation call appears in a package body.
2923
2924 if Nkind (Context) = N_Package_Body then
2925 Spec :=
2926 Specification
2927 (Unit_Declaration_Node (Corresponding_Spec (Context)));
2928
2929 Traverse_List
2930 (List => Visible_Declarations (Spec),
2931 Task_Objs => Task_Objs);
2932
2933 Traverse_List
2934 (List => Private_Declarations (Spec),
2935 Task_Objs => Task_Objs);
2936
2937 Traverse_List
2938 (List => Declarations (Context),
2939 Task_Objs => Task_Objs);
2940
2941 -- Process all task objects in the spec when the activation call
2942 -- appears in a package spec.
2943
2944 elsif Nkind (Context) = N_Package_Specification then
2945 Traverse_List
2946 (List => Visible_Declarations (Context),
2947 Task_Objs => Task_Objs);
2948
2949 Traverse_List
2950 (List => Private_Declarations (Context),
2951 Task_Objs => Task_Objs);
2952
2953 -- Otherwise the context must be a block or a body. Process all
2954 -- task objects found in the declarations.
2955
2956 else
2957 pragma Assert (Nkind_In (Context, N_Block_Statement,
2958 N_Entry_Body,
2959 N_Protected_Body,
2960 N_Subprogram_Body,
2961 N_Task_Body));
2962
2963 Traverse_List
2964 (List => Declarations (Context),
2965 Task_Objs => Task_Objs);
2966 end if;
2967 end if;
2968 end Process_Activation;
2969 end Activation_Processor;
2970
2971 -----------------------
2972 -- Assignment_Target --
2973 -----------------------
2974
2975 function Assignment_Target (Asmt : Node_Id) return Node_Id is
2976 Nam : Node_Id;
2977
2978 begin
2979 Nam := Name (Asmt);
2980
2981 -- When the name denotes an array or record component, find the whole
2982 -- object.
2983
2984 while Nkind_In (Nam, N_Explicit_Dereference,
2985 N_Indexed_Component,
2986 N_Selected_Component,
2987 N_Slice)
2988 loop
2989 Nam := Prefix (Nam);
2990 end loop;
2991
2992 return Nam;
2993 end Assignment_Target;
2994
2995 --------------------
2996 -- Body_Processor --
2997 --------------------
2998
2999 package body Body_Processor is
3000
3001 ---------------------
3002 -- Data structures --
3003 ---------------------
3004
3005 -- The following map relates scenario lists to subprogram bodies
3006
3007 Nested_Scenarios_Map : NE_List_Map.Dynamic_Hash_Table := NE_List_Map.Nil;
3008
3009 -- The following set contains all subprogram bodies that have been
3010 -- processed by routine Traverse_Body.
3011
3012 Traversed_Bodies_Set : NE_Set.Membership_Set := NE_Set.Nil;
3013
3014 -----------------------
3015 -- Local subprograms --
3016 -----------------------
3017
3018 function Is_Traversed_Body (N : Node_Id) return Boolean;
3019 pragma Inline (Is_Traversed_Body);
3020 -- Determine whether subprogram body N has already been traversed
3021
3022 function Nested_Scenarios
3023 (N : Node_Id) return NE_List.Doubly_Linked_List;
3024 pragma Inline (Nested_Scenarios);
3025 -- Obtain the list of scenarios associated with subprogram body N
3026
3027 procedure Set_Is_Traversed_Body
3028 (N : Node_Id;
3029 Val : Boolean := True);
3030 pragma Inline (Set_Is_Traversed_Body);
3031 -- Mark subprogram body N as traversed depending on value Val
3032
3033 procedure Set_Nested_Scenarios
3034 (N : Node_Id;
3035 Scenarios : NE_List.Doubly_Linked_List);
3036 pragma Inline (Set_Nested_Scenarios);
3037 -- Associate scenario list Scenarios with subprogram body N
3038
3039 -----------------------------
3040 -- Finalize_Body_Processor --
3041 -----------------------------
3042
3043 procedure Finalize_Body_Processor is
3044 begin
3045 NE_List_Map.Destroy (Nested_Scenarios_Map);
3046 NE_Set.Destroy (Traversed_Bodies_Set);
3047 end Finalize_Body_Processor;
3048
3049 -------------------------------
3050 -- Initialize_Body_Processor --
3051 -------------------------------
3052
3053 procedure Initialize_Body_Processor is
3054 begin
3055 Nested_Scenarios_Map := NE_List_Map.Create (250);
3056 Traversed_Bodies_Set := NE_Set.Create (250);
3057 end Initialize_Body_Processor;
3058
3059 -----------------------
3060 -- Is_Traversed_Body --
3061 -----------------------
3062
3063 function Is_Traversed_Body (N : Node_Id) return Boolean is
3064 pragma Assert (Present (N));
3065 begin
3066 return NE_Set.Contains (Traversed_Bodies_Set, N);
3067 end Is_Traversed_Body;
3068
3069 ----------------------
3070 -- Nested_Scenarios --
3071 ----------------------
3072
3073 function Nested_Scenarios
3074 (N : Node_Id) return NE_List.Doubly_Linked_List
3075 is
3076 pragma Assert (Present (N));
3077 pragma Assert (Nkind (N) = N_Subprogram_Body);
3078
3079 begin
3080 return NE_List_Map.Get (Nested_Scenarios_Map, N);
3081 end Nested_Scenarios;
3082
3083 ----------------------------
3084 -- Reset_Traversed_Bodies --
3085 ----------------------------
3086
3087 procedure Reset_Traversed_Bodies is
3088 begin
3089 NE_Set.Reset (Traversed_Bodies_Set);
3090 end Reset_Traversed_Bodies;
3091
3092 ---------------------------
3093 -- Set_Is_Traversed_Body --
3094 ---------------------------
3095
3096 procedure Set_Is_Traversed_Body
3097 (N : Node_Id;
3098 Val : Boolean := True)
3099 is
3100 pragma Assert (Present (N));
3101
3102 begin
3103 if Val then
3104 NE_Set.Insert (Traversed_Bodies_Set, N);
3105 else
3106 NE_Set.Delete (Traversed_Bodies_Set, N);
3107 end if;
3108 end Set_Is_Traversed_Body;
3109
3110 --------------------------
3111 -- Set_Nested_Scenarios --
3112 --------------------------
3113
3114 procedure Set_Nested_Scenarios
3115 (N : Node_Id;
3116 Scenarios : NE_List.Doubly_Linked_List)
3117 is
3118 pragma Assert (Present (N));
3119 begin
3120 NE_List_Map.Put (Nested_Scenarios_Map, N, Scenarios);
3121 end Set_Nested_Scenarios;
3122
3123 -------------------
3124 -- Traverse_Body --
3125 -------------------
3126
3127 procedure Traverse_Body
3128 (N : Node_Id;
3129 Requires_Processing : Scenario_Predicate_Ptr;
3130 Processor : Scenario_Processor_Ptr;
3131 In_State : Processing_In_State)
3132 is
3133 Scenarios : NE_List.Doubly_Linked_List := NE_List.Nil;
3134 -- The list of scenarios that appear within the declarations and
3135 -- statement of subprogram body N. The variable is intentionally
3136 -- global because Is_Potential_Scenario needs to populate it.
3137
3138 function In_Task_Body (Nod : Node_Id) return Boolean;
3139 pragma Inline (In_Task_Body);
3140 -- Determine whether arbitrary node Nod appears within a task body
3141
3142 function Is_Synchronous_Suspension_Call
3143 (Nod : Node_Id) return Boolean;
3144 pragma Inline (Is_Synchronous_Suspension_Call);
3145 -- Determine whether arbitrary node Nod denotes a call to one of
3146 -- these routines:
3147 --
3148 -- Ada.Synchronous_Barriers.Wait_For_Release
3149 -- Ada.Synchronous_Task_Control.Suspend_Until_True
3150
3151 procedure Traverse_Collected_Scenarios;
3152 pragma Inline (Traverse_Collected_Scenarios);
3153 -- Traverse the already collected scenarios in list Scenarios by
3154 -- invoking Processor on each individual one.
3155
3156 procedure Traverse_List (List : List_Id);
3157 pragma Inline (Traverse_List);
3158 -- Invoke Traverse_Potential_Scenarios on each node in list List
3159
3160 function Traverse_Potential_Scenario
3161 (Scen : Node_Id) return Traverse_Result;
3162 pragma Inline (Traverse_Potential_Scenario);
3163 -- Determine whether arbitrary node Scen is a suitable scenario using
3164 -- predicate Is_Scenario and traverse it by invoking Processor on it.
3165
3166 procedure Traverse_Potential_Scenarios is
3167 new Traverse_Proc (Traverse_Potential_Scenario);
3168
3169 ------------------
3170 -- In_Task_Body --
3171 ------------------
3172
3173 function In_Task_Body (Nod : Node_Id) return Boolean is
3174 Par : Node_Id;
3175
3176 begin
3177 -- Climb the parent chain looking for a task body [procedure]
3178
3179 Par := Nod;
3180 while Present (Par) loop
3181 if Nkind (Par) = N_Task_Body then
3182 return True;
3183
3184 elsif Nkind (Par) = N_Subprogram_Body
3185 and then Is_Task_Body_Procedure (Par)
3186 then
3187 return True;
3188
3189 -- Prevent the search from going too far. Note that this test
3190 -- shares nodes with the two cases above, and must come last.
3191
3192 elsif Is_Body_Or_Package_Declaration (Par) then
3193 return False;
3194 end if;
3195
3196 Par := Parent (Par);
3197 end loop;
3198
3199 return False;
3200 end In_Task_Body;
3201
3202 ------------------------------------
3203 -- Is_Synchronous_Suspension_Call --
3204 ------------------------------------
3205
3206 function Is_Synchronous_Suspension_Call
3207 (Nod : Node_Id) return Boolean
3208 is
3209 Subp_Id : Entity_Id;
3210
3211 begin
3212 -- To qualify, the call must invoke one of the runtime routines
3213 -- which perform synchronous suspension.
3214
3215 if Is_Suitable_Call (Nod) then
3216 Subp_Id := Target (Nod);
3217
3218 return
3219 Is_RTE (Subp_Id, RE_Suspend_Until_True)
3220 or else
3221 Is_RTE (Subp_Id, RE_Wait_For_Release);
3222 end if;
3223
3224 return False;
3225 end Is_Synchronous_Suspension_Call;
3226
3227 ----------------------------------
3228 -- Traverse_Collected_Scenarios --
3229 ----------------------------------
3230
3231 procedure Traverse_Collected_Scenarios is
3232 Iter : NE_List.Iterator;
3233 Scen : Node_Id;
3234
3235 begin
3236 Iter := NE_List.Iterate (Scenarios);
3237 while NE_List.Has_Next (Iter) loop
3238 NE_List.Next (Iter, Scen);
3239
3240 -- The current scenario satisfies the input predicate, process
3241 -- it.
3242
3243 if Requires_Processing.all (Scen) then
3244 Processor.all (Scen, In_State);
3245 end if;
3246 end loop;
3247 end Traverse_Collected_Scenarios;
3248
3249 -------------------
3250 -- Traverse_List --
3251 -------------------
3252
3253 procedure Traverse_List (List : List_Id) is
3254 Scen : Node_Id;
3255
3256 begin
3257 Scen := First (List);
3258 while Present (Scen) loop
3259 Traverse_Potential_Scenarios (Scen);
3260 Next (Scen);
3261 end loop;
3262 end Traverse_List;
3263
3264 ---------------------------------
3265 -- Traverse_Potential_Scenario --
3266 ---------------------------------
3267
3268 function Traverse_Potential_Scenario
3269 (Scen : Node_Id) return Traverse_Result
3270 is
3271 begin
3272 -- Special cases
3273
3274 -- Skip constructs which do not have elaboration of their own and
3275 -- need to be elaborated by other means such as invocation, task
3276 -- activation, etc.
3277
3278 if Is_Non_Library_Level_Encapsulator (Scen) then
3279 return Skip;
3280
3281 -- Terminate the traversal of a task body when encountering an
3282 -- accept or select statement, and
3283 --
3284 -- * Entry calls during elaboration are not allowed. In this
3285 -- case the accept or select statement will cause the task
3286 -- to block at elaboration time because there are no entry
3287 -- calls to unblock it.
3288 --
3289 -- or
3290 --
3291 -- * Switch -gnatd_a (stop elaboration checks on accept or
3292 -- select statement) is in effect.
3293
3294 elsif (Debug_Flag_Underscore_A
3295 or else Restriction_Active
3296 (No_Entry_Calls_In_Elaboration_Code))
3297 and then Nkind_In (Original_Node (Scen), N_Accept_Statement,
3298 N_Selective_Accept)
3299 then
3300 return Abandon;
3301
3302 -- Terminate the traversal of a task body when encountering a
3303 -- suspension call, and
3304 --
3305 -- * Entry calls during elaboration are not allowed. In this
3306 -- case the suspension call emulates an entry call and will
3307 -- cause the task to block at elaboration time.
3308 --
3309 -- or
3310 --
3311 -- * Switch -gnatd_s (stop elaboration checks on synchronous
3312 -- suspension) is in effect.
3313 --
3314 -- Note that the guard should not be checking the state of flag
3315 -- Within_Task_Body because only suspension calls which appear
3316 -- immediately within the statements of the task are supported.
3317 -- Flag Within_Task_Body carries over to deeper levels of the
3318 -- traversal.
3319
3320 elsif (Debug_Flag_Underscore_S
3321 or else Restriction_Active
3322 (No_Entry_Calls_In_Elaboration_Code))
3323 and then Is_Synchronous_Suspension_Call (Scen)
3324 and then In_Task_Body (Scen)
3325 then
3326 return Abandon;
3327
3328 -- Certain nodes carry semantic lists which act as repositories
3329 -- until expansion transforms the node and relocates the contents.
3330 -- Examine these lists in case expansion is disabled.
3331
3332 elsif Nkind_In (Scen, N_And_Then, N_Or_Else) then
3333 Traverse_List (Actions (Scen));
3334
3335 elsif Nkind_In (Scen, N_Elsif_Part, N_Iteration_Scheme) then
3336 Traverse_List (Condition_Actions (Scen));
3337
3338 elsif Nkind (Scen) = N_If_Expression then
3339 Traverse_List (Then_Actions (Scen));
3340 Traverse_List (Else_Actions (Scen));
3341
3342 elsif Nkind_In (Scen, N_Component_Association,
3343 N_Iterated_Component_Association)
3344 then
3345 Traverse_List (Loop_Actions (Scen));
3346
3347 -- General case
3348
3349 -- The current node satisfies the input predicate, process it
3350
3351 elsif Requires_Processing.all (Scen) then
3352 Processor.all (Scen, In_State);
3353 end if;
3354
3355 -- Save a general scenario regardless of whether it satisfies the
3356 -- input predicate. This allows for quick subsequent traversals of
3357 -- general scenarios, even with different predicates.
3358
3359 if Is_Suitable_Access_Taken (Scen)
3360 or else Is_Suitable_Call (Scen)
3361 or else Is_Suitable_Instantiation (Scen)
3362 or else Is_Suitable_Variable_Assignment (Scen)
3363 or else Is_Suitable_Variable_Reference (Scen)
3364 then
3365 NE_List.Append (Scenarios, Scen);
3366 end if;
3367
3368 return OK;
3369 end Traverse_Potential_Scenario;
3370
3371 -- Start of processing for Traverse_Body
3372
3373 begin
3374 -- Nothing to do when the traversal is suppressed
3375
3376 if In_State.Traversal = No_Traversal then
3377 return;
3378
3379 -- Nothing to do when there is no input
3380
3381 elsif No (N) then
3382 return;
3383
3384 -- Nothing to do when the input is not a subprogram body
3385
3386 elsif Nkind (N) /= N_Subprogram_Body then
3387 return;
3388
3389 -- Nothing to do if the subprogram body was already traversed
3390
3391 elsif Is_Traversed_Body (N) then
3392 return;
3393 end if;
3394
3395 -- Mark the subprogram body as traversed
3396
3397 Set_Is_Traversed_Body (N);
3398
3399 Scenarios := Nested_Scenarios (N);
3400
3401 -- The subprogram body has been traversed at least once, and all
3402 -- scenarios that appear within its declarations and statements
3403 -- have already been collected. Directly retraverse the scenarios
3404 -- without having to retraverse the subprogram body subtree.
3405
3406 if NE_List.Present (Scenarios) then
3407 Traverse_Collected_Scenarios;
3408
3409 -- Otherwise the subprogram body is being traversed for the first
3410 -- time. Collect all scenarios that appear within its declarations
3411 -- and statements in case the subprogram body has to be retraversed
3412 -- multiple times.
3413
3414 else
3415 Scenarios := NE_List.Create;
3416 Set_Nested_Scenarios (N, Scenarios);
3417
3418 Traverse_List (Declarations (N));
3419 Traverse_Potential_Scenarios (Handled_Statement_Sequence (N));
3420 end if;
3421 end Traverse_Body;
3422 end Body_Processor;
3423
3424 -----------------------
3425 -- Build_Call_Marker --
3426 -----------------------
3427
3428 procedure Build_Call_Marker (N : Node_Id) is
3429 function In_External_Context
3430 (Call : Node_Id;
3431 Subp_Id : Entity_Id) return Boolean;
3432 pragma Inline (In_External_Context);
3433 -- Determine whether entry, operator, or subprogram Subp_Id is external
3434 -- to call Call which must reside within an instance.
3435
3436 function In_Premature_Context (Call : Node_Id) return Boolean;
3437 pragma Inline (In_Premature_Context);
3438 -- Determine whether call Call appears within a premature context
3439
3440 function Is_Default_Expression (Call : Node_Id) return Boolean;
3441 pragma Inline (Is_Default_Expression);
3442 -- Determine whether call Call acts as the expression of a defaulted
3443 -- parameter within a source call.
3444
3445 function Is_Generic_Formal_Subp (Subp_Id : Entity_Id) return Boolean;
3446 pragma Inline (Is_Generic_Formal_Subp);
3447 -- Determine whether subprogram Subp_Id denotes a generic formal
3448 -- subprogram which appears in the "prologue" of an instantiation.
3449
3450 -------------------------
3451 -- In_External_Context --
3452 -------------------------
3453
3454 function In_External_Context
3455 (Call : Node_Id;
3456 Subp_Id : Entity_Id) return Boolean
3457 is
3458 Spec_Decl : constant Entity_Id := Unit_Declaration_Node (Subp_Id);
3459
3460 Inst : Node_Id;
3461 Inst_Body : Node_Id;
3462 Inst_Spec : Node_Id;
3463
3464 begin
3465 Inst := Find_Enclosing_Instance (Call);
3466
3467 -- The call appears within an instance
3468
3469 if Present (Inst) then
3470
3471 -- The call comes from the main unit and the target does not
3472
3473 if In_Extended_Main_Code_Unit (Call)
3474 and then not In_Extended_Main_Code_Unit (Spec_Decl)
3475 then
3476 return True;
3477
3478 -- Otherwise the target declaration must not appear within the
3479 -- instance spec or body.
3480
3481 else
3482 Spec_And_Body_From_Node
3483 (N => Inst,
3484 Spec_Decl => Inst_Spec,
3485 Body_Decl => Inst_Body);
3486
3487 return not In_Subtree
3488 (N => Spec_Decl,
3489 Root1 => Inst_Spec,
3490 Root2 => Inst_Body);
3491 end if;
3492 end if;
3493
3494 return False;
3495 end In_External_Context;
3496
3497 --------------------------
3498 -- In_Premature_Context --
3499 --------------------------
3500
3501 function In_Premature_Context (Call : Node_Id) return Boolean is
3502 Par : Node_Id;
3503
3504 begin
3505 -- Climb the parent chain looking for premature contexts
3506
3507 Par := Parent (Call);
3508 while Present (Par) loop
3509
3510 -- Aspect specifications and generic associations are premature
3511 -- contexts because nested calls has not been relocated to their
3512 -- final context.
3513
3514 if Nkind_In (Par, N_Aspect_Specification,
3515 N_Generic_Association)
3516 then
3517 return True;
3518
3519 -- Prevent the search from going too far
3520
3521 elsif Is_Body_Or_Package_Declaration (Par) then
3522 exit;
3523 end if;
3524
3525 Par := Parent (Par);
3526 end loop;
3527
3528 return False;
3529 end In_Premature_Context;
3530
3531 ---------------------------
3532 -- Is_Default_Expression --
3533 ---------------------------
3534
3535 function Is_Default_Expression (Call : Node_Id) return Boolean is
3536 Outer_Call : constant Node_Id := Parent (Call);
3537 Outer_Nam : Node_Id;
3538
3539 begin
3540 -- To qualify, the node must appear immediately within a source call
3541 -- which invokes a source target.
3542
3543 if Nkind_In (Outer_Call, N_Entry_Call_Statement,
3544 N_Function_Call,
3545 N_Procedure_Call_Statement)
3546 and then Comes_From_Source (Outer_Call)
3547 then
3548 Outer_Nam := Call_Name (Outer_Call);
3549
3550 return
3551 Is_Entity_Name (Outer_Nam)
3552 and then Present (Entity (Outer_Nam))
3553 and then Is_Subprogram_Or_Entry (Entity (Outer_Nam))
3554 and then Comes_From_Source (Entity (Outer_Nam));
3555 end if;
3556
3557 return False;
3558 end Is_Default_Expression;
3559
3560 ----------------------------
3561 -- Is_Generic_Formal_Subp --
3562 ----------------------------
3563
3564 function Is_Generic_Formal_Subp (Subp_Id : Entity_Id) return Boolean is
3565 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3566 Context : constant Node_Id := Parent (Subp_Decl);
3567
3568 begin
3569 -- To qualify, the subprogram must rename a generic actual subprogram
3570 -- where the enclosing context is an instantiation.
3571
3572 return
3573 Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration
3574 and then not Comes_From_Source (Subp_Decl)
3575 and then Nkind_In (Context, N_Function_Specification,
3576 N_Package_Specification,
3577 N_Procedure_Specification)
3578 and then Present (Generic_Parent (Context));
3579 end Is_Generic_Formal_Subp;
3580
3581 -- Local variables
3582
3583 Call_Nam : Node_Id;
3584 Marker : Node_Id;
3585 Subp_Id : Entity_Id;
3586
3587 -- Start of processing for Build_Call_Marker
3588
3589 begin
3590 -- Nothing to do when switch -gnatH (legacy elaboration checking mode
3591 -- enabled) is in effect because the legacy ABE mechanism does not need
3592 -- to carry out this action.
3593
3594 if Legacy_Elaboration_Checks then
3595 return;
3596
3597 -- Nothing to do for ASIS because ABE checks and diagnostics are not
3598 -- performed in this mode.
3599
3600 elsif ASIS_Mode then
3601 return;
3602
3603 -- Nothing to do when the call is being preanalyzed as the marker will
3604 -- be inserted in the wrong place.
3605
3606 elsif Preanalysis_Active then
3607 return;
3608
3609 -- Nothing to do when the elaboration phase of the compiler is not
3610 -- active.
3611
3612 elsif not Elaboration_Phase_Active then
3613 return;
3614
3615 -- Nothing to do when the input does not denote a call or a requeue
3616
3617 elsif not Nkind_In (N, N_Entry_Call_Statement,
3618 N_Function_Call,
3619 N_Procedure_Call_Statement,
3620 N_Requeue_Statement)
3621 then
3622 return;
3623
3624 -- Nothing to do when the input denotes entry call or requeue statement,
3625 -- and switch -gnatd_e (ignore entry calls and requeue statements for
3626 -- elaboration) is in effect.
3627
3628 elsif Debug_Flag_Underscore_E
3629 and then Nkind_In (N, N_Entry_Call_Statement, N_Requeue_Statement)
3630 then
3631 return;
3632
3633 -- Nothing to do when the call is analyzed/resolved too early within an
3634 -- intermediate context. This check is saved for last because it incurs
3635 -- a performance penalty.
3636
3637 elsif In_Premature_Context (N) then
3638 return;
3639 end if;
3640
3641 Call_Nam := Call_Name (N);
3642
3643 -- Nothing to do when the call is erroneous or left in a bad state
3644
3645 if not (Is_Entity_Name (Call_Nam)
3646 and then Present (Entity (Call_Nam))
3647 and then Is_Subprogram_Or_Entry (Entity (Call_Nam)))
3648 then
3649 return;
3650 end if;
3651
3652 Subp_Id := Canonical_Subprogram (Entity (Call_Nam));
3653
3654 -- Nothing to do when the call invokes a generic formal subprogram and
3655 -- switch -gnatd.G (ignore calls through generic formal parameters for
3656 -- elaboration) is in effect. This check must be performed with the
3657 -- direct target of the call to avoid the side effects of mapping
3658 -- actuals to formals using renamings.
3659
3660 if Debug_Flag_Dot_GG
3661 and then Is_Generic_Formal_Subp (Entity (Call_Nam))
3662 then
3663 return;
3664
3665 -- Nothing to do when the call appears within the expanded spec or
3666 -- body of an instantiated generic, the call does not invoke a generic
3667 -- formal subprogram, the target is external to the instance, and switch
3668 -- -gnatdL (ignore external calls from instances for elaboration) is in
3669 -- effect. This check must be performed with the direct target of the
3670 -- call to avoid the side effects of mapping actuals to formals using
3671 -- renamings.
3672
3673 elsif Debug_Flag_LL
3674 and then not Is_Generic_Formal_Subp (Entity (Call_Nam))
3675 and then In_External_Context
3676 (Call => N,
3677 Subp_Id => Subp_Id)
3678 then
3679 return;
3680
3681 -- Nothing to do when the call invokes an assertion pragma procedure
3682 -- and switch -gnatd_p (ignore assertion pragmas for elaboration) is
3683 -- in effect.
3684
3685 elsif Debug_Flag_Underscore_P
3686 and then Is_Assertion_Pragma_Target (Subp_Id)
3687 then
3688 return;
3689
3690 -- Source calls to source targets are always considered because they
3691 -- reflect the original call graph.
3692
3693 elsif Comes_From_Source (N) and then Comes_From_Source (Subp_Id) then
3694 null;
3695
3696 -- A call to a source function which acts as the default expression in
3697 -- another call requires special detection.
3698
3699 elsif Comes_From_Source (Subp_Id)
3700 and then Nkind (N) = N_Function_Call
3701 and then Is_Default_Expression (N)
3702 then
3703 null;
3704
3705 -- The target emulates Ada semantics
3706
3707 elsif Is_Ada_Semantic_Target (Subp_Id) then
3708 null;
3709
3710 -- The target acts as a link between scenarios
3711
3712 elsif Is_Bridge_Target (Subp_Id) then
3713 null;
3714
3715 -- The target emulates SPARK semantics
3716
3717 elsif Is_SPARK_Semantic_Target (Subp_Id) then
3718 null;
3719
3720 -- Otherwise the call is not suitable for ABE processing. This prevents
3721 -- the generation of call markers which will never play a role in ABE
3722 -- diagnostics.
3723
3724 else
3725 return;
3726 end if;
3727
3728 -- At this point it is known that the call will play some role in ABE
3729 -- checks and diagnostics. Create a corresponding call marker in case
3730 -- the original call is heavily transformed by expansion later on.
3731
3732 Marker := Make_Call_Marker (Sloc (N));
3733
3734 -- Inherit the attributes of the original call
3735
3736 Set_Is_Declaration_Level_Node
3737 (Marker, Find_Enclosing_Level (N) = Declaration_Level);
3738
3739 Set_Is_Dispatching_Call
3740 (Marker, Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
3741 and then Present (Controlling_Argument (N)));
3742
3743 Set_Is_Elaboration_Checks_OK_Node
3744 (Marker, Is_Elaboration_Checks_OK_Node (N));
3745
3746 Set_Is_Elaboration_Warnings_OK_Node
3747 (Marker, Is_Elaboration_Warnings_OK_Node (N));
3748
3749 Set_Is_Ignored_Ghost_Node (Marker, Is_Ignored_Ghost_Node (N));
3750 Set_Is_Source_Call (Marker, Comes_From_Source (N));
3751 Set_Is_SPARK_Mode_On_Node (Marker, Is_SPARK_Mode_On_Node (N));
3752 Set_Target (Marker, Subp_Id);
3753
3754 -- The marker is inserted prior to the original call. This placement has
3755 -- several desirable effects:
3756
3757 -- 1) The marker appears in the same context, in close proximity to
3758 -- the call.
3759
3760 -- <marker>
3761 -- <call>
3762
3763 -- 2) Inserting the marker prior to the call ensures that an ABE check
3764 -- will take effect prior to the call.
3765
3766 -- <ABE check>
3767 -- <marker>
3768 -- <call>
3769
3770 -- 3) The above two properties are preserved even when the call is a
3771 -- function which is subsequently relocated in order to capture its
3772 -- result. Note that if the call is relocated to a new context, the
3773 -- relocated call will receive a marker of its own.
3774
3775 -- <ABE check>
3776 -- <maker>
3777 -- Temp : ... := Func_Call ...;
3778 -- ... Temp ...
3779
3780 -- The insertion must take place even when the call does not occur in
3781 -- the main unit to keep the tree symmetric. This ensures that internal
3782 -- name serialization is consistent in case the call marker causes the
3783 -- tree to transform in some way.
3784
3785 Insert_Action (N, Marker);
3786
3787 -- The marker becomes the "corresponding" scenario for the call. Save
3788 -- the marker for later processing by the ABE phase.
3789
3790 Record_Elaboration_Scenario (Marker);
3791 end Build_Call_Marker;
3792
3793 -------------------------------------
3794 -- Build_Variable_Reference_Marker --
3795 -------------------------------------
3796
3797 procedure Build_Variable_Reference_Marker
3798 (N : Node_Id;
3799 Read : Boolean;
3800 Write : Boolean)
3801 is
3802 function Ultimate_Variable (Var_Id : Entity_Id) return Entity_Id;
3803 pragma Inline (Ultimate_Variable);
3804 -- Obtain the ultimate renamed variable of variable Var_Id
3805
3806 -----------------------
3807 -- Ultimate_Variable --
3808 -----------------------
3809
3810 function Ultimate_Variable (Var_Id : Entity_Id) return Entity_Id is
3811 Ren_Id : Entity_Id;
3812
3813 begin
3814 Ren_Id := Var_Id;
3815 while Present (Renamed_Entity (Ren_Id))
3816 and then Nkind (Renamed_Entity (Ren_Id)) in N_Entity
3817 loop
3818 Ren_Id := Renamed_Entity (Ren_Id);
3819 end loop;
3820
3821 return Ren_Id;
3822 end Ultimate_Variable;
3823
3824 -- Local variables
3825
3826 Var_Id : constant Entity_Id := Ultimate_Variable (Entity (N));
3827 Marker : Node_Id;
3828
3829 -- Start of processing for Build_Variable_Reference_Marker
3830
3831 begin
3832 -- Nothing to do when the elaboration phase of the compiler is not
3833 -- active.
3834
3835 if not Elaboration_Phase_Active then
3836 return;
3837 end if;
3838
3839 Marker := Make_Variable_Reference_Marker (Sloc (N));
3840
3841 -- Inherit the attributes of the original variable reference
3842
3843 Set_Is_Elaboration_Checks_OK_Node
3844 (Marker, Is_Elaboration_Checks_OK_Node (N));
3845
3846 Set_Is_Elaboration_Warnings_OK_Node
3847 (Marker, Is_Elaboration_Warnings_OK_Node (N));
3848
3849 Set_Is_Read (Marker, Read);
3850 Set_Is_SPARK_Mode_On_Node (Marker, Is_SPARK_Mode_On_Node (N));
3851 Set_Is_Write (Marker, Write);
3852 Set_Target (Marker, Var_Id);
3853
3854 -- The marker is inserted prior to the original variable reference. The
3855 -- insertion must take place even when the reference does not occur in
3856 -- the main unit to keep the tree symmetric. This ensures that internal
3857 -- name serialization is consistent in case the variable marker causes
3858 -- the tree to transform in some way.
3859
3860 Insert_Action (N, Marker);
3861
3862 -- The marker becomes the "corresponding" scenario for the reference.
3863 -- Save the marker for later processing for the ABE phase.
3864
3865 Record_Elaboration_Scenario (Marker);
3866 end Build_Variable_Reference_Marker;
3867
3868 ---------------
3869 -- Call_Name --
3870 ---------------
3871
3872 function Call_Name (Call : Node_Id) return Node_Id is
3873 Nam : Node_Id;
3874
3875 begin
3876 Nam := Name (Call);
3877
3878 -- When the call invokes an entry family, the name appears as an indexed
3879 -- component.
3880
3881 if Nkind (Nam) = N_Indexed_Component then
3882 Nam := Prefix (Nam);
3883 end if;
3884
3885 -- When the call employs the object.operation form, the name appears as
3886 -- a selected component.
3887
3888 if Nkind (Nam) = N_Selected_Component then
3889 Nam := Selector_Name (Nam);
3890 end if;
3891
3892 return Nam;
3893 end Call_Name;
3894
3895 --------------------------
3896 -- Canonical_Subprogram --
3897 --------------------------
3898
3899 function Canonical_Subprogram (Subp_Id : Entity_Id) return Entity_Id is
3900 Canon_Id : Entity_Id;
3901
3902 begin
3903 Canon_Id := Subp_Id;
3904
3905 -- Use the original protected subprogram when dealing with one of the
3906 -- specialized lock-manipulating versions.
3907
3908 if Is_Protected_Body_Subp (Canon_Id) then
3909 Canon_Id := Protected_Subprogram (Canon_Id);
3910 end if;
3911
3912 -- Obtain the original subprogram except when the subprogram is also
3913 -- an instantiation. In this case the alias is the internally generated
3914 -- subprogram which appears within the anonymous package created for the
3915 -- instantiation, making it unuitable.
3916
3917 if not Is_Generic_Instance (Canon_Id) then
3918 Canon_Id := Get_Renamed_Entity (Canon_Id);
3919 end if;
3920
3921 return Canon_Id;
3922 end Canonical_Subprogram;
3923
3924 ---------------------------------
3925 -- Check_Elaboration_Scenarios --
3926 ---------------------------------
3927
3928 procedure Check_Elaboration_Scenarios is
3929 Iter : NE_Set.Iterator;
3930
3931 begin
3932 -- Nothing to do when switch -gnatH (legacy elaboration checking mode
3933 -- enabled) is in effect because the legacy ABE mechanism does not need
3934 -- to carry out this action.
3935
3936 if Legacy_Elaboration_Checks then
3937 Finalize_All_Data_Structures;
3938 return;
3939
3940 -- Nothing to do for ASIS because ABE checks and diagnostics are not
3941 -- performed in this mode.
3942
3943 elsif ASIS_Mode then
3944 Finalize_All_Data_Structures;
3945 return;
3946
3947 -- Nothing to do when the elaboration phase of the compiler is not
3948 -- active.
3949
3950 elsif not Elaboration_Phase_Active then
3951 Finalize_All_Data_Structures;
3952 return;
3953 end if;
3954
3955 -- Restore the original elaboration model which was in effect when the
3956 -- scenarios were first recorded. The model may be specified by pragma
3957 -- Elaboration_Checks which appears on the initial declaration of the
3958 -- main unit.
3959
3960 Install_Elaboration_Model (Unit_Entity (Main_Unit_Entity));
3961
3962 -- Examine the context of the main unit and record all units with prior
3963 -- elaboration with respect to it.
3964
3965 Collect_Elaborated_Units;
3966
3967 -- Examine all scenarios saved during the Recording phase applying the
3968 -- Ada or SPARK elaboration rules in order to detect and diagnose ABE
3969 -- issues, install conditional ABE checks, and ensure the elaboration
3970 -- of units.
3971
3972 Iter := Iterate_Declaration_Scenarios;
3973 Check_Conditional_ABE_Scenarios (Iter);
3974
3975 Iter := Iterate_Library_Body_Scenarios;
3976 Check_Conditional_ABE_Scenarios (Iter);
3977
3978 Iter := Iterate_Library_Spec_Scenarios;
3979 Check_Conditional_ABE_Scenarios (Iter);
3980
3981 -- Examine each SPARK scenario saved during the Recording phase which
3982 -- is not necessarily executable during elaboration, but still requires
3983 -- elaboration-related checks.
3984
3985 Check_SPARK_Scenarios;
3986
3987 -- Add conditional ABE checks for all scenarios that require one when
3988 -- the dynamic model is in effect.
3989
3990 Install_Dynamic_ABE_Checks;
3991
3992 -- Examine all scenarios saved during the Recording phase along with
3993 -- invocation constructs within the spec and body of the main unit.
3994 -- Record the declarations and paths that reach into an external unit
3995 -- in the ALI file of the main unit.
3996
3997 Record_Invocation_Graph;
3998
3999 -- Destroy all internal data structures and complete the elaboration
4000 -- phase of the compiler.
4001
4002 Finalize_All_Data_Structures;
4003 Set_Elaboration_Phase (Completed);
4004 end Check_Elaboration_Scenarios;
4005
4006 ---------------------
4007 -- Check_Installer --
4008 ---------------------
4009
4010 package body Check_Installer is
4011
4012 -----------------------
4013 -- Local subprograms --
4014 -----------------------
4015
4016 function ABE_Check_Or_Failure_OK
4017 (N : Node_Id;
4018 Targ_Id : Entity_Id;
4019 Unit_Id : Entity_Id) return Boolean;
4020 pragma Inline (ABE_Check_Or_Failure_OK);
4021 -- Determine whether a conditional ABE check or guaranteed ABE failure
4022 -- can be installed for scenario N with target Targ_Id which resides in
4023 -- unit Unit_Id.
4024
4025 function Insertion_Node (N : Node_Id) return Node_Id;
4026 pragma Inline (Insertion_Node);
4027 -- Obtain the proper insertion node of an ABE check or failure for
4028 -- scenario N.
4029
4030 procedure Insert_ABE_Check_Or_Failure (N : Node_Id; Check : Node_Id);
4031 pragma Inline (Insert_ABE_Check_Or_Failure);
4032 -- Insert conditional ABE check or guaranteed ABE failure Check prior to
4033 -- scenario N.
4034
4035 procedure Install_Scenario_ABE_Check_Common
4036 (N : Node_Id;
4037 Targ_Id : Entity_Id;
4038 Targ_Rep : Target_Rep_Id);
4039 pragma Inline (Install_Scenario_ABE_Check_Common);
4040 -- Install a conditional ABE check for scenario N to ensure that target
4041 -- Targ_Id is properly elaborated. Targ_Rep is the representation of the
4042 -- target.
4043
4044 procedure Install_Scenario_ABE_Failure_Common (N : Node_Id);
4045 pragma Inline (Install_Scenario_ABE_Failure_Common);
4046 -- Install a guaranteed ABE failure for scenario N
4047
4048 procedure Install_Unit_ABE_Check_Common
4049 (N : Node_Id;
4050 Unit_Id : Entity_Id);
4051 pragma Inline (Install_Unit_ABE_Check_Common);
4052 -- Install a conditional ABE check for scenario N to ensure that unit
4053 -- Unit_Id is properly elaborated.
4054
4055 -----------------------------
4056 -- ABE_Check_Or_Failure_OK --
4057 -----------------------------
4058
4059 function ABE_Check_Or_Failure_OK
4060 (N : Node_Id;
4061 Targ_Id : Entity_Id;
4062 Unit_Id : Entity_Id) return Boolean
4063 is
4064 pragma Unreferenced (Targ_Id);
4065
4066 Ins_Node : constant Node_Id := Insertion_Node (N);
4067
4068 begin
4069 if not Check_Or_Failure_Generation_OK then
4070 return False;
4071
4072 -- Nothing to do when the scenario denots a compilation unit because
4073 -- there is no executable environment at that level.
4074
4075 elsif Nkind (Parent (Ins_Node)) = N_Compilation_Unit then
4076 return False;
4077
4078 -- An ABE check or failure is not needed when the target is defined
4079 -- in a unit which is elaborated prior to the main unit. This check
4080 -- must also consider the following cases:
4081 --
4082 -- * The unit of the target appears in the context of the main unit
4083 --
4084 -- * The unit of the target is subject to pragma Elaborate_Body. An
4085 -- ABE check MUST NOT be generated because the unit is always
4086 -- elaborated prior to the main unit.
4087 --
4088 -- * The unit of the target is the main unit. An ABE check MUST be
4089 -- added in this case because a conditional ABE may be raised
4090 -- depending on the flow of execution within the main unit (flag
4091 -- Same_Unit_OK is False).
4092
4093 elsif Has_Prior_Elaboration
4094 (Unit_Id => Unit_Id,
4095 Context_OK => True,
4096 Elab_Body_OK => True)
4097 then
4098 return False;
4099 end if;
4100
4101 return True;
4102 end ABE_Check_Or_Failure_OK;
4103
4104 ------------------------------------
4105 -- Check_Or_Failure_Generation_OK --
4106 ------------------------------------
4107
4108 function Check_Or_Failure_Generation_OK return Boolean is
4109 begin
4110 -- An ABE check or failure is not needed when the compilation will
4111 -- not produce an executable.
4112
4113 if Serious_Errors_Detected > 0 then
4114 return False;
4115
4116 -- An ABE check or failure must not be installed when compiling for
4117 -- GNATprove because raise statements are not supported.
4118
4119 elsif GNATprove_Mode then
4120 return False;
4121 end if;
4122
4123 return True;
4124 end Check_Or_Failure_Generation_OK;
4125
4126 --------------------
4127 -- Insertion_Node --
4128 --------------------
4129
4130 function Insertion_Node (N : Node_Id) return Node_Id is
4131 begin
4132 -- When the scenario denotes an instantiation, the proper insertion
4133 -- node is the instance spec. This ensures that the generic actuals
4134 -- will not be evaluated prior to a potential ABE.
4135
4136 if Nkind (N) in N_Generic_Instantiation
4137 and then Present (Instance_Spec (N))
4138 then
4139 return Instance_Spec (N);
4140
4141 -- Otherwise the proper insertion node is the scenario itself
4142
4143 else
4144 return N;
4145 end if;
4146 end Insertion_Node;
4147
4148 ---------------------------------
4149 -- Insert_ABE_Check_Or_Failure --
4150 ---------------------------------
4151
4152 procedure Insert_ABE_Check_Or_Failure (N : Node_Id; Check : Node_Id) is
4153 Ins_Nod : constant Node_Id := Insertion_Node (N);
4154 Scop_Id : constant Entity_Id := Find_Enclosing_Scope (Ins_Nod);
4155
4156 begin
4157 -- Install the nearest enclosing scope of the scenario as there must
4158 -- be something on the scope stack.
4159
4160 Push_Scope (Scop_Id);
4161
4162 Insert_Action (Ins_Nod, Check);
4163
4164 Pop_Scope;
4165 end Insert_ABE_Check_Or_Failure;
4166
4167 --------------------------------
4168 -- Install_Dynamic_ABE_Checks --
4169 --------------------------------
4170
4171 procedure Install_Dynamic_ABE_Checks is
4172 Iter : NE_Set.Iterator;
4173 N : Node_Id;
4174
4175 begin
4176 if not Check_Or_Failure_Generation_OK then
4177 return;
4178
4179 -- Nothing to do if the dynamic model is not in effect
4180
4181 elsif not Dynamic_Elaboration_Checks then
4182 return;
4183 end if;
4184
4185 -- Install a conditional ABE check for each saved scenario
4186
4187 Iter := Iterate_Dynamic_ABE_Check_Scenarios;
4188 while NE_Set.Has_Next (Iter) loop
4189 NE_Set.Next (Iter, N);
4190
4191 Process_Conditional_ABE
4192 (N => N,
4193 In_State => Dynamic_Model_State);
4194 end loop;
4195 end Install_Dynamic_ABE_Checks;
4196
4197 --------------------------------
4198 -- Install_Scenario_ABE_Check --
4199 --------------------------------
4200
4201 procedure Install_Scenario_ABE_Check
4202 (N : Node_Id;
4203 Targ_Id : Entity_Id;
4204 Targ_Rep : Target_Rep_Id;
4205 Disable : Scenario_Rep_Id)
4206 is
4207 begin
4208 -- Nothing to do when the scenario does not need an ABE check
4209
4210 if not ABE_Check_Or_Failure_OK
4211 (N => N,
4212 Targ_Id => Targ_Id,
4213 Unit_Id => Unit (Targ_Rep))
4214 then
4215 return;
4216 end if;
4217
4218 -- Prevent multiple attempts to install the same ABE check
4219
4220 Disable_Elaboration_Checks (Disable);
4221
4222 Install_Scenario_ABE_Check_Common
4223 (N => N,
4224 Targ_Id => Targ_Id,
4225 Targ_Rep => Targ_Rep);
4226 end Install_Scenario_ABE_Check;
4227
4228 --------------------------------
4229 -- Install_Scenario_ABE_Check --
4230 --------------------------------
4231
4232 procedure Install_Scenario_ABE_Check
4233 (N : Node_Id;
4234 Targ_Id : Entity_Id;
4235 Targ_Rep : Target_Rep_Id;
4236 Disable : Target_Rep_Id)
4237 is
4238 begin
4239 -- Nothing to do when the scenario does not need an ABE check
4240
4241 if not ABE_Check_Or_Failure_OK
4242 (N => N,
4243 Targ_Id => Targ_Id,
4244 Unit_Id => Unit (Targ_Rep))
4245 then
4246 return;
4247 end if;
4248
4249 -- Prevent multiple attempts to install the same ABE check
4250
4251 Disable_Elaboration_Checks (Disable);
4252
4253 Install_Scenario_ABE_Check_Common
4254 (N => N,
4255 Targ_Id => Targ_Id,
4256 Targ_Rep => Targ_Rep);
4257 end Install_Scenario_ABE_Check;
4258
4259 ---------------------------------------
4260 -- Install_Scenario_ABE_Check_Common --
4261 ---------------------------------------
4262
4263 procedure Install_Scenario_ABE_Check_Common
4264 (N : Node_Id;
4265 Targ_Id : Entity_Id;
4266 Targ_Rep : Target_Rep_Id)
4267 is
4268 Targ_Body : constant Node_Id := Body_Declaration (Targ_Rep);
4269 Targ_Decl : constant Node_Id := Spec_Declaration (Targ_Rep);
4270
4271 pragma Assert (Present (Targ_Body));
4272 pragma Assert (Present (Targ_Decl));
4273
4274 procedure Build_Elaboration_Entity;
4275 pragma Inline (Build_Elaboration_Entity);
4276 -- Create a new elaboration flag for Targ_Id, insert it prior to
4277 -- Targ_Decl, and set it after Targ_Body.
4278
4279 ------------------------------
4280 -- Build_Elaboration_Entity --
4281 ------------------------------
4282
4283 procedure Build_Elaboration_Entity is
4284 Loc : constant Source_Ptr := Sloc (Targ_Id);
4285 Flag_Id : Entity_Id;
4286
4287 begin
4288 -- Nothing to do if the target has an elaboration flag
4289
4290 if Present (Elaboration_Entity (Targ_Id)) then
4291 return;
4292 end if;
4293
4294 -- Create the declaration of the elaboration flag. The name
4295 -- carries a unique counter in case the name is overloaded.
4296
4297 Flag_Id :=
4298 Make_Defining_Identifier (Loc,
4299 Chars => New_External_Name (Chars (Targ_Id), 'E', -1));
4300
4301 Set_Elaboration_Entity (Targ_Id, Flag_Id);
4302 Set_Elaboration_Entity_Required (Targ_Id);
4303
4304 Push_Scope (Scope (Targ_Id));
4305
4306 -- Generate:
4307 -- Enn : Short_Integer := 0;
4308
4309 Insert_Action (Targ_Decl,
4310 Make_Object_Declaration (Loc,
4311 Defining_Identifier => Flag_Id,
4312 Object_Definition =>
4313 New_Occurrence_Of (Standard_Short_Integer, Loc),
4314 Expression => Make_Integer_Literal (Loc, Uint_0)));
4315
4316 -- Generate:
4317 -- Enn := 1;
4318
4319 Set_Elaboration_Flag (Targ_Body, Targ_Id);
4320
4321 Pop_Scope;
4322 end Build_Elaboration_Entity;
4323
4324 -- Local variables
4325
4326 Loc : constant Source_Ptr := Sloc (N);
4327
4328 -- Start for processing for Install_Scenario_ABE_Check_Common
4329
4330 begin
4331 -- Create an elaboration flag for the target when it does not have
4332 -- one.
4333
4334 Build_Elaboration_Entity;
4335
4336 -- Generate:
4337 -- if not Targ_Id'Elaborated then
4338 -- raise Program_Error with "access before elaboration";
4339 -- end if;
4340
4341 Insert_ABE_Check_Or_Failure
4342 (N => N,
4343 Check =>
4344 Make_Raise_Program_Error (Loc,
4345 Condition =>
4346 Make_Op_Not (Loc,
4347 Right_Opnd =>
4348 Make_Attribute_Reference (Loc,
4349 Prefix => New_Occurrence_Of (Targ_Id, Loc),
4350 Attribute_Name => Name_Elaborated)),
4351 Reason => PE_Access_Before_Elaboration));
4352 end Install_Scenario_ABE_Check_Common;
4353
4354 ----------------------------------
4355 -- Install_Scenario_ABE_Failure --
4356 ----------------------------------
4357
4358 procedure Install_Scenario_ABE_Failure
4359 (N : Node_Id;
4360 Targ_Id : Entity_Id;
4361 Targ_Rep : Target_Rep_Id;
4362 Disable : Scenario_Rep_Id)
4363 is
4364 begin
4365 -- Nothing to do when the scenario does not require an ABE failure
4366
4367 if not ABE_Check_Or_Failure_OK
4368 (N => N,
4369 Targ_Id => Targ_Id,
4370 Unit_Id => Unit (Targ_Rep))
4371 then
4372 return;
4373 end if;
4374
4375 -- Prevent multiple attempts to install the same ABE check
4376
4377 Disable_Elaboration_Checks (Disable);
4378
4379 Install_Scenario_ABE_Failure_Common (N);
4380 end Install_Scenario_ABE_Failure;
4381
4382 ----------------------------------
4383 -- Install_Scenario_ABE_Failure --
4384 ----------------------------------
4385
4386 procedure Install_Scenario_ABE_Failure
4387 (N : Node_Id;
4388 Targ_Id : Entity_Id;
4389 Targ_Rep : Target_Rep_Id;
4390 Disable : Target_Rep_Id)
4391 is
4392 begin
4393 -- Nothing to do when the scenario does not require an ABE failure
4394
4395 if not ABE_Check_Or_Failure_OK
4396 (N => N,
4397 Targ_Id => Targ_Id,
4398 Unit_Id => Unit (Targ_Rep))
4399 then
4400 return;
4401 end if;
4402
4403 -- Prevent multiple attempts to install the same ABE check
4404
4405 Disable_Elaboration_Checks (Disable);
4406
4407 Install_Scenario_ABE_Failure_Common (N);
4408 end Install_Scenario_ABE_Failure;
4409
4410 -----------------------------------------
4411 -- Install_Scenario_ABE_Failure_Common --
4412 -----------------------------------------
4413
4414 procedure Install_Scenario_ABE_Failure_Common (N : Node_Id) is
4415 Loc : constant Source_Ptr := Sloc (N);
4416
4417 begin
4418 -- Generate:
4419 -- raise Program_Error with "access before elaboration";
4420
4421 Insert_ABE_Check_Or_Failure
4422 (N => N,
4423 Check =>
4424 Make_Raise_Program_Error (Loc,
4425 Reason => PE_Access_Before_Elaboration));
4426 end Install_Scenario_ABE_Failure_Common;
4427
4428 ----------------------------
4429 -- Install_Unit_ABE_Check --
4430 ----------------------------
4431
4432 procedure Install_Unit_ABE_Check
4433 (N : Node_Id;
4434 Unit_Id : Entity_Id;
4435 Disable : Scenario_Rep_Id)
4436 is
4437 Spec_Id : constant Entity_Id := Unique_Entity (Unit_Id);
4438
4439 begin
4440 -- Nothing to do when the scenario does not require an ABE check
4441
4442 if not ABE_Check_Or_Failure_OK
4443 (N => N,
4444 Targ_Id => Empty,
4445 Unit_Id => Spec_Id)
4446 then
4447 return;
4448 end if;
4449
4450 -- Prevent multiple attempts to install the same ABE check
4451
4452 Disable_Elaboration_Checks (Disable);
4453
4454 Install_Unit_ABE_Check_Common
4455 (N => N,
4456 Unit_Id => Unit_Id);
4457 end Install_Unit_ABE_Check;
4458
4459 ----------------------------
4460 -- Install_Unit_ABE_Check --
4461 ----------------------------
4462
4463 procedure Install_Unit_ABE_Check
4464 (N : Node_Id;
4465 Unit_Id : Entity_Id;
4466 Disable : Target_Rep_Id)
4467 is
4468 Spec_Id : constant Entity_Id := Unique_Entity (Unit_Id);
4469
4470 begin
4471 -- Nothing to do when the scenario does not require an ABE check
4472
4473 if not ABE_Check_Or_Failure_OK
4474 (N => N,
4475 Targ_Id => Empty,
4476 Unit_Id => Spec_Id)
4477 then
4478 return;
4479 end if;
4480
4481 -- Prevent multiple attempts to install the same ABE check
4482
4483 Disable_Elaboration_Checks (Disable);
4484
4485 Install_Unit_ABE_Check_Common
4486 (N => N,
4487 Unit_Id => Unit_Id);
4488 end Install_Unit_ABE_Check;
4489
4490 -----------------------------------
4491 -- Install_Unit_ABE_Check_Common --
4492 -----------------------------------
4493
4494 procedure Install_Unit_ABE_Check_Common
4495 (N : Node_Id;
4496 Unit_Id : Entity_Id)
4497 is
4498 Loc : constant Source_Ptr := Sloc (N);
4499 Spec_Id : constant Entity_Id := Unique_Entity (Unit_Id);
4500
4501 begin
4502 -- Generate:
4503 -- if not Spec_Id'Elaborated then
4504 -- raise Program_Error with "access before elaboration";
4505 -- end if;
4506
4507 Insert_ABE_Check_Or_Failure
4508 (N => N,
4509 Check =>
4510 Make_Raise_Program_Error (Loc,
4511 Condition =>
4512 Make_Op_Not (Loc,
4513 Right_Opnd =>
4514 Make_Attribute_Reference (Loc,
4515 Prefix => New_Occurrence_Of (Spec_Id, Loc),
4516 Attribute_Name => Name_Elaborated)),
4517 Reason => PE_Access_Before_Elaboration));
4518 end Install_Unit_ABE_Check_Common;
4519 end Check_Installer;
4520
4521 ----------------------
4522 -- Compilation_Unit --
4523 ----------------------
4524
4525 function Compilation_Unit (Unit_Id : Entity_Id) return Node_Id is
4526 Comp_Unit : Node_Id;
4527
4528 begin
4529 Comp_Unit := Parent (Unit_Id);
4530
4531 -- Handle the case where a concurrent subunit is rewritten as a null
4532 -- statement due to expansion activities.
4533
4534 if Nkind (Comp_Unit) = N_Null_Statement
4535 and then Nkind_In (Original_Node (Comp_Unit), N_Protected_Body,
4536 N_Task_Body)
4537 then
4538 Comp_Unit := Parent (Comp_Unit);
4539 pragma Assert (Nkind (Comp_Unit) = N_Subunit);
4540
4541 -- Otherwise use the declaration node of the unit
4542
4543 else
4544 Comp_Unit := Parent (Unit_Declaration_Node (Unit_Id));
4545 end if;
4546
4547 -- Handle the case where a subprogram instantiation which acts as a
4548 -- compilation unit is expanded into an anonymous package that wraps
4549 -- the instantiated subprogram.
4550
4551 if Nkind (Comp_Unit) = N_Package_Specification
4552 and then Nkind_In (Original_Node (Parent (Comp_Unit)),
4553 N_Function_Instantiation,
4554 N_Procedure_Instantiation)
4555 then
4556 Comp_Unit := Parent (Parent (Comp_Unit));
4557
4558 -- Handle the case where the compilation unit is a subunit
4559
4560 elsif Nkind (Comp_Unit) = N_Subunit then
4561 Comp_Unit := Parent (Comp_Unit);
4562 end if;
4563
4564 pragma Assert (Nkind (Comp_Unit) = N_Compilation_Unit);
4565
4566 return Comp_Unit;
4567 end Compilation_Unit;
4568
4569 -------------------------------
4570 -- Conditional_ABE_Processor --
4571 -------------------------------
4572
4573 package body Conditional_ABE_Processor is
4574
4575 -----------------------
4576 -- Local subprograms --
4577 -----------------------
4578
4579 function Is_Conditional_ABE_Scenario (N : Node_Id) return Boolean;
4580 pragma Inline (Is_Conditional_ABE_Scenario);
4581 -- Determine whether node N is a suitable scenario for conditional ABE
4582 -- checks and diagnostics.
4583
4584 procedure Process_Conditional_ABE_Access_Taken
4585 (Attr : Node_Id;
4586 Attr_Rep : Scenario_Rep_Id;
4587 In_State : Processing_In_State);
4588 pragma Inline (Process_Conditional_ABE_Access_Taken);
4589 -- Perform ABE checks and diagnostics for attribute reference Attr with
4590 -- representation Attr_Rep which takes 'Access of an entry, operator, or
4591 -- subprogram. In_State is the current state of the Processing phase.
4592
4593 procedure Process_Conditional_ABE_Activation
4594 (Call : Node_Id;
4595 Call_Rep : Scenario_Rep_Id;
4596 Obj_Id : Entity_Id;
4597 Obj_Rep : Target_Rep_Id;
4598 Task_Typ : Entity_Id;
4599 Task_Rep : Target_Rep_Id;
4600 In_State : Processing_In_State);
4601 pragma Inline (Process_Conditional_ABE_Activation);
4602 -- Perform common conditional ABE checks and diagnostics for activation
4603 -- call Call which activates object Obj_Id of task type Task_Typ. Formal
4604 -- Call_Rep denotes the representation of the call. Obj_Rep denotes the
4605 -- representation of the object. Task_Rep denotes the representation of
4606 -- the task type. In_State is the current state of the Processing phase.
4607
4608 procedure Process_Conditional_ABE_Call
4609 (Call : Node_Id;
4610 Call_Rep : Scenario_Rep_Id;
4611 In_State : Processing_In_State);
4612 pragma Inline (Process_Conditional_ABE_Call);
4613 -- Top-level dispatcher for processing of calls. Perform ABE checks and
4614 -- diagnostics for call Call with representation Call_Rep. In_State is
4615 -- the current state of the Processing phase.
4616
4617 procedure Process_Conditional_ABE_Call_Ada
4618 (Call : Node_Id;
4619 Call_Rep : Scenario_Rep_Id;
4620 Subp_Id : Entity_Id;
4621 Subp_Rep : Target_Rep_Id;
4622 In_State : Processing_In_State);
4623 pragma Inline (Process_Conditional_ABE_Call_Ada);
4624 -- Perform ABE checks and diagnostics for call Call which invokes entry,
4625 -- operator, or subprogram Subp_Id using the Ada rules. Call_Rep denotes
4626 -- the representation of the call. Subp_Rep denotes the representation
4627 -- of the subprogram. In_State is the current state of the Processing
4628 -- phase.
4629
4630 procedure Process_Conditional_ABE_Call_SPARK
4631 (Call : Node_Id;
4632 Call_Rep : Scenario_Rep_Id;
4633 Subp_Id : Entity_Id;
4634 Subp_Rep : Target_Rep_Id;
4635 In_State : Processing_In_State);
4636 pragma Inline (Process_Conditional_ABE_Call_SPARK);
4637 -- Perform ABE checks and diagnostics for call Call which invokes entry,
4638 -- operator, or subprogram Subp_Id using the SPARK rules. Call_Rep is
4639 -- the representation of the call. Subp_Rep denotes the representation
4640 -- of the subprogram. In_State is the current state of the Processing
4641 -- phase.
4642
4643 procedure Process_Conditional_ABE_Instantiation
4644 (Inst : Node_Id;
4645 Inst_Rep : Scenario_Rep_Id;
4646 In_State : Processing_In_State);
4647 pragma Inline (Process_Conditional_ABE_Instantiation);
4648 -- Top-level dispatcher for processing of instantiations. Perform ABE
4649 -- checks and diagnostics for instantiation Inst with representation
4650 -- Inst_Rep. In_State is the current state of the Processing phase.
4651
4652 procedure Process_Conditional_ABE_Instantiation_Ada
4653 (Inst : Node_Id;
4654 Inst_Rep : Scenario_Rep_Id;
4655 Gen_Id : Entity_Id;
4656 Gen_Rep : Target_Rep_Id;
4657 In_State : Processing_In_State);
4658 pragma Inline (Process_Conditional_ABE_Instantiation_Ada);
4659 -- Perform ABE checks and diagnostics for instantiation Inst of generic
4660 -- Gen_Id using the Ada rules. Inst_Rep denotes the representation of
4661 -- the instnace. Gen_Rep is the representation of the generic. In_State
4662 -- is the current state of the Processing phase.
4663
4664 procedure Process_Conditional_ABE_Instantiation_SPARK
4665 (Inst : Node_Id;
4666 Inst_Rep : Scenario_Rep_Id;
4667 Gen_Id : Entity_Id;
4668 Gen_Rep : Target_Rep_Id;
4669 In_State : Processing_In_State);
4670 pragma Inline (Process_Conditional_ABE_Instantiation_SPARK);
4671 -- Perform ABE checks and diagnostics for instantiation Inst of generic
4672 -- Gen_Id using the SPARK rules. Inst_Rep denotes the representation of
4673 -- the instnace. Gen_Rep is the representation of the generic. In_State
4674 -- is the current state of the Processing phase.
4675
4676 procedure Process_Conditional_ABE_Variable_Assignment
4677 (Asmt : Node_Id;
4678 Asmt_Rep : Scenario_Rep_Id;
4679 In_State : Processing_In_State);
4680 pragma Inline (Process_Conditional_ABE_Variable_Assignment);
4681 -- Top-level dispatcher for processing of variable assignments. Perform
4682 -- ABE checks and diagnostics for assignment Asmt with representation
4683 -- Asmt_Rep. In_State denotes the current state of the Processing phase.
4684
4685 procedure Process_Conditional_ABE_Variable_Assignment_Ada
4686 (Asmt : Node_Id;
4687 Asmt_Rep : Scenario_Rep_Id;
4688 Var_Id : Entity_Id;
4689 Var_Rep : Target_Rep_Id;
4690 In_State : Processing_In_State);
4691 pragma Inline (Process_Conditional_ABE_Variable_Assignment_Ada);
4692 -- Perform ABE checks and diagnostics for assignment statement Asmt that
4693 -- modifies the value of variable Var_Id using the Ada rules. Asmt_Rep
4694 -- denotes the representation of the assignment. Var_Rep denotes the
4695 -- representation of the variable. In_State is the current state of the
4696 -- Processing phase.
4697
4698 procedure Process_Conditional_ABE_Variable_Assignment_SPARK
4699 (Asmt : Node_Id;
4700 Asmt_Rep : Scenario_Rep_Id;
4701 Var_Id : Entity_Id;
4702 Var_Rep : Target_Rep_Id;
4703 In_State : Processing_In_State);
4704 pragma Inline (Process_Conditional_ABE_Variable_Assignment_SPARK);
4705 -- Perform ABE checks and diagnostics for assignment statement Asmt that
4706 -- modifies the value of variable Var_Id using the SPARK rules. Asmt_Rep
4707 -- denotes the representation of the assignment. Var_Rep denotes the
4708 -- representation of the variable. In_State is the current state of the
4709 -- Processing phase.
4710
4711 procedure Process_Conditional_ABE_Variable_Reference
4712 (Ref : Node_Id;
4713 Ref_Rep : Scenario_Rep_Id;
4714 In_State : Processing_In_State);
4715 pragma Inline (Process_Conditional_ABE_Variable_Reference);
4716 -- Perform ABE checks and diagnostics for variable reference Ref with
4717 -- representation Ref_Rep. In_State denotes the current state of the
4718 -- Processing phase.
4719
4720 procedure Traverse_Conditional_ABE_Body
4721 (N : Node_Id;
4722 In_State : Processing_In_State);
4723 pragma Inline (Traverse_Conditional_ABE_Body);
4724 -- Traverse subprogram body N looking for suitable scenarios that need
4725 -- to be processed for conditional ABE checks and diagnostics. In_State
4726 -- is the current state of the Processing phase.
4727
4728 -------------------------------------
4729 -- Check_Conditional_ABE_Scenarios --
4730 -------------------------------------
4731
4732 procedure Check_Conditional_ABE_Scenarios
4733 (Iter : in out NE_Set.Iterator)
4734 is
4735 N : Node_Id;
4736
4737 begin
4738 while NE_Set.Has_Next (Iter) loop
4739 NE_Set.Next (Iter, N);
4740
4741 -- Reset the traversed status of all subprogram bodies because the
4742 -- current conditional scenario acts as a new DFS traversal root.
4743
4744 Reset_Traversed_Bodies;
4745
4746 Process_Conditional_ABE
4747 (N => N,
4748 In_State => Conditional_ABE_State);
4749 end loop;
4750 end Check_Conditional_ABE_Scenarios;
4751
4752 ---------------------------------
4753 -- Is_Conditional_ABE_Scenario --
4754 ---------------------------------
4755
4756 function Is_Conditional_ABE_Scenario (N : Node_Id) return Boolean is
4757 begin
4758 return
4759 Is_Suitable_Access_Taken (N)
4760 or else Is_Suitable_Call (N)
4761 or else Is_Suitable_Instantiation (N)
4762 or else Is_Suitable_Variable_Assignment (N)
4763 or else Is_Suitable_Variable_Reference (N);
4764 end Is_Conditional_ABE_Scenario;
4765
4766 -----------------------------
4767 -- Process_Conditional_ABE --
4768 -----------------------------
4769
4770 procedure Process_Conditional_ABE
4771 (N : Node_Id;
4772 In_State : Processing_In_State)
4773 is
4774 Scen : constant Node_Id := Scenario (N);
4775 Scen_Rep : Scenario_Rep_Id;
4776
4777 begin
4778 -- Add the current scenario to the stack of active scenarios
4779
4780 Push_Active_Scenario (Scen);
4781
4782 -- 'Access
4783
4784 if Is_Suitable_Access_Taken (Scen) then
4785 Process_Conditional_ABE_Access_Taken
4786 (Attr => Scen,
4787 Attr_Rep => Scenario_Representation_Of (Scen, In_State),
4788 In_State => In_State);
4789
4790 -- Call or task activation
4791
4792 elsif Is_Suitable_Call (Scen) then
4793 Scen_Rep := Scenario_Representation_Of (Scen, In_State);
4794
4795 -- Routine Build_Call_Marker creates call markers regardless of
4796 -- whether the call occurs within the main unit or not. This way
4797 -- the serialization of internal names is kept consistent. Only
4798 -- call markers found within the main unit must be processed.
4799
4800 if In_Main_Context (Scen) then
4801 Scen_Rep := Scenario_Representation_Of (Scen, In_State);
4802
4803 if Kind (Scen_Rep) = Call_Scenario then
4804 Process_Conditional_ABE_Call
4805 (Call => Scen,
4806 Call_Rep => Scen_Rep,
4807 In_State => In_State);
4808
4809 else
4810 pragma Assert (Kind (Scen_Rep) = Task_Activation_Scenario);
4811
4812 Process_Activation
4813 (Call => Scen,
4814 Call_Rep => Scen_Rep,
4815 Processor => Process_Conditional_ABE_Activation'Access,
4816 In_State => In_State);
4817 end if;
4818 end if;
4819
4820 -- Instantiation
4821
4822 elsif Is_Suitable_Instantiation (Scen) then
4823 Process_Conditional_ABE_Instantiation
4824 (Inst => Scen,
4825 Inst_Rep => Scenario_Representation_Of (Scen, In_State),
4826 In_State => In_State);
4827
4828 -- Variable assignments
4829
4830 elsif Is_Suitable_Variable_Assignment (Scen) then
4831 Process_Conditional_ABE_Variable_Assignment
4832 (Asmt => Scen,
4833 Asmt_Rep => Scenario_Representation_Of (Scen, In_State),
4834 In_State => In_State);
4835
4836 -- Variable references
4837
4838 elsif Is_Suitable_Variable_Reference (Scen) then
4839
4840 -- Routine Build_Variable_Reference_Marker makes variable markers
4841 -- regardless of whether the reference occurs within the main unit
4842 -- or not. This way the serialization of internal names is kept
4843 -- consistent. Only variable markers within the main unit must be
4844 -- processed.
4845
4846 if In_Main_Context (Scen) then
4847 Process_Conditional_ABE_Variable_Reference
4848 (Ref => Scen,
4849 Ref_Rep => Scenario_Representation_Of (Scen, In_State),
4850 In_State => In_State);
4851 end if;
4852 end if;
4853
4854 -- Remove the current scenario from the stack of active scenarios
4855 -- once all ABE diagnostics and checks have been performed.
4856
4857 Pop_Active_Scenario (Scen);
4858 end Process_Conditional_ABE;
4859
4860 ------------------------------------------
4861 -- Process_Conditional_ABE_Access_Taken --
4862 ------------------------------------------
4863
4864 procedure Process_Conditional_ABE_Access_Taken
4865 (Attr : Node_Id;
4866 Attr_Rep : Scenario_Rep_Id;
4867 In_State : Processing_In_State)
4868 is
4869 function Build_Access_Marker (Subp_Id : Entity_Id) return Node_Id;
4870 pragma Inline (Build_Access_Marker);
4871 -- Create a suitable call marker which invokes subprogram Subp_Id
4872
4873 -------------------------
4874 -- Build_Access_Marker --
4875 -------------------------
4876
4877 function Build_Access_Marker (Subp_Id : Entity_Id) return Node_Id is
4878 Marker : Node_Id;
4879
4880 begin
4881 Marker := Make_Call_Marker (Sloc (Attr));
4882
4883 -- Inherit relevant attributes from the attribute
4884
4885 Set_Target (Marker, Subp_Id);
4886 Set_Is_Declaration_Level_Node
4887 (Marker, Level (Attr_Rep) = Declaration_Level);
4888 Set_Is_Dispatching_Call
4889 (Marker, False);
4890 Set_Is_Elaboration_Checks_OK_Node
4891 (Marker, Elaboration_Checks_OK (Attr_Rep));
4892 Set_Is_Elaboration_Warnings_OK_Node
4893 (Marker, Elaboration_Warnings_OK (Attr_Rep));
4894 Set_Is_Source_Call
4895 (Marker, Comes_From_Source (Attr));
4896 Set_Is_SPARK_Mode_On_Node
4897 (Marker, SPARK_Mode_Of (Attr_Rep) = Is_On);
4898
4899 -- Partially insert the call marker into the tree by setting its
4900 -- parent pointer.
4901
4902 Set_Parent (Marker, Attr);
4903
4904 return Marker;
4905 end Build_Access_Marker;
4906
4907 -- Local variables
4908
4909 Root : constant Node_Id := Root_Scenario;
4910 Subp_Id : constant Entity_Id := Target (Attr_Rep);
4911 Subp_Rep : constant Target_Rep_Id :=
4912 Target_Representation_Of (Subp_Id, In_State);
4913 Body_Decl : constant Node_Id := Body_Declaration (Subp_Rep);
4914
4915 New_In_State : Processing_In_State := In_State;
4916 -- Each step of the Processing phase constitutes a new state
4917
4918 -- Start of processing for Process_Conditional_ABE_Access
4919
4920 begin
4921 -- Output relevant information when switch -gnatel (info messages on
4922 -- implicit Elaborate[_All] pragmas) is in effect.
4923
4924 if Elab_Info_Messages
4925 and then not New_In_State.Suppress_Info_Messages
4926 then
4927 Error_Msg_NE
4928 ("info: access to & during elaboration", Attr, Subp_Id);
4929 end if;
4930
4931 -- Warnings are suppressed when a prior scenario is already in that
4932 -- mode or when the attribute or the target have warnings suppressed.
4933 -- Update the state of the Processing phase to reflect this.
4934
4935 New_In_State.Suppress_Warnings :=
4936 New_In_State.Suppress_Warnings
4937 or else not Elaboration_Warnings_OK (Attr_Rep)
4938 or else not Elaboration_Warnings_OK (Subp_Rep);
4939
4940 -- Do not emit any ABE diagnostics when the current or previous
4941 -- scenario in this traversal has suppressed elaboration warnings.
4942
4943 if New_In_State.Suppress_Warnings then
4944 null;
4945
4946 -- Both the attribute and the corresponding subprogram body are in
4947 -- the same unit. The body must appear prior to the root scenario
4948 -- which started the recursive search. If this is not the case, then
4949 -- there is a potential ABE if the access value is used to call the
4950 -- subprogram. Emit a warning only when switch -gnatw.f (warnings on
4951 -- suspucious 'Access) is in effect.
4952
4953 elsif Warn_On_Elab_Access
4954 and then Present (Body_Decl)
4955 and then In_Extended_Main_Code_Unit (Body_Decl)
4956 and then Earlier_In_Extended_Unit (Root, Body_Decl)
4957 then
4958 Error_Msg_Name_1 := Attribute_Name (Attr);
4959 Error_Msg_NE
4960 ("??% attribute of & before body seen", Attr, Subp_Id);
4961 Error_Msg_N ("\possible Program_Error on later references", Attr);
4962
4963 Output_Active_Scenarios (Attr, New_In_State);
4964 end if;
4965
4966 -- Treat the attribute an an immediate invocation of the target when
4967 -- switch -gnatd.o (conservative elaboration order for indirect
4968 -- calls) is in effect. This has the following desirable effects:
4969 --
4970 -- * Ensure that the unit with the corresponding body is elaborated
4971 -- prior to the main unit.
4972 --
4973 -- * Perform conditional ABE checks and diagnostics
4974 --
4975 -- * Traverse the body of the target (if available)
4976
4977 if Debug_Flag_Dot_O then
4978 Process_Conditional_ABE
4979 (N => Build_Access_Marker (Subp_Id),
4980 In_State => New_In_State);
4981
4982 -- Otherwise ensure that the unit with the corresponding body is
4983 -- elaborated prior to the main unit.
4984
4985 else
4986 Ensure_Prior_Elaboration
4987 (N => Attr,
4988 Unit_Id => Unit (Subp_Rep),
4989 Prag_Nam => Name_Elaborate_All,
4990 In_State => New_In_State);
4991 end if;
4992 end Process_Conditional_ABE_Access_Taken;
4993
4994 ----------------------------------------
4995 -- Process_Conditional_ABE_Activation --
4996 ----------------------------------------
4997
4998 procedure Process_Conditional_ABE_Activation
4999 (Call : Node_Id;
5000 Call_Rep : Scenario_Rep_Id;
5001 Obj_Id : Entity_Id;
5002 Obj_Rep : Target_Rep_Id;
5003 Task_Typ : Entity_Id;
5004 Task_Rep : Target_Rep_Id;
5005 In_State : Processing_In_State)
5006 is
5007 pragma Unreferenced (Task_Typ);
5008
5009 Body_Decl : constant Node_Id := Body_Declaration (Task_Rep);
5010 Spec_Decl : constant Node_Id := Spec_Declaration (Task_Rep);
5011 Root : constant Node_Id := Root_Scenario;
5012 Unit_Id : constant Node_Id := Unit (Task_Rep);
5013
5014 Check_OK : constant Boolean :=
5015 not In_State.Suppress_Checks
5016 and then Ghost_Mode_Of (Obj_Rep) /= Is_Ignored
5017 and then Ghost_Mode_Of (Task_Rep) /= Is_Ignored
5018 and then Elaboration_Checks_OK (Obj_Rep)
5019 and then Elaboration_Checks_OK (Task_Rep);
5020 -- A run-time ABE check may be installed only when the object and the
5021 -- task type have active elaboration checks, and both are not ignored
5022 -- Ghost constructs.
5023
5024 New_In_State : Processing_In_State := In_State;
5025 -- Each step of the Processing phase constitutes a new state
5026
5027 begin
5028 -- Output relevant information when switch -gnatel (info messages on
5029 -- implicit Elaborate[_All] pragmas) is in effect.
5030
5031 if Elab_Info_Messages
5032 and then not New_In_State.Suppress_Info_Messages
5033 then
5034 Error_Msg_NE
5035 ("info: activation of & during elaboration", Call, Obj_Id);
5036 end if;
5037
5038 -- Nothing to do when the call activates a task whose type is defined
5039 -- within an instance and switch -gnatd_i (ignore activations and
5040 -- calls to instances for elaboration) is in effect.
5041
5042 if Debug_Flag_Underscore_I
5043 and then In_External_Instance
5044 (N => Call,
5045 Target_Decl => Spec_Decl)
5046 then
5047 return;
5048
5049 -- Nothing to do when the activation is a guaranteed ABE
5050
5051 elsif Is_Known_Guaranteed_ABE (Call) then
5052 return;
5053
5054 -- Nothing to do when the root scenario appears at the declaration
5055 -- level and the task is in the same unit, but outside this context.
5056 --
5057 -- task type Task_Typ; -- task declaration
5058 --
5059 -- procedure Proc is
5060 -- function A ... is
5061 -- begin
5062 -- if Some_Condition then
5063 -- declare
5064 -- T : Task_Typ;
5065 -- begin
5066 -- <activation call> -- activation site
5067 -- end;
5068 -- ...
5069 -- end A;
5070 --
5071 -- X : ... := A; -- root scenario
5072 -- ...
5073 --
5074 -- task body Task_Typ is
5075 -- ...
5076 -- end Task_Typ;
5077 --
5078 -- In the example above, the context of X is the declarative list of
5079 -- Proc. The "elaboration" of X may reach the activation of T whose
5080 -- body is defined outside of X's context. The task body is relevant
5081 -- only when Proc is invoked, but this happens only during "normal"
5082 -- elaboration, therefore the task body must not be considered if
5083 -- this is not the case.
5084
5085 elsif Is_Up_Level_Target
5086 (Targ_Decl => Spec_Decl,
5087 In_State => New_In_State)
5088 then
5089 return;
5090
5091 -- Nothing to do when the activation is ABE-safe
5092 --
5093 -- generic
5094 -- package Gen is
5095 -- task type Task_Typ;
5096 -- end Gen;
5097 --
5098 -- package body Gen is
5099 -- task body Task_Typ is
5100 -- begin
5101 -- ...
5102 -- end Task_Typ;
5103 -- end Gen;
5104 --
5105 -- with Gen;
5106 -- procedure Main is
5107 -- package Nested is
5108 -- package Inst is new Gen;
5109 -- T : Inst.Task_Typ;
5110 -- <activation call> -- safe activation
5111 -- end Nested;
5112 -- ...
5113
5114 elsif Is_Safe_Activation (Call, Task_Rep) then
5115
5116 -- Note that the task body must still be examined for any nested
5117 -- scenarios.
5118
5119 null;
5120
5121 -- The activation call and the task body are both in the main unit
5122 --
5123 -- If the root scenario appears prior to the task body, then this is
5124 -- a possible ABE with respect to the root scenario.
5125 --
5126 -- task type Task_Typ;
5127 --
5128 -- function A ... is
5129 -- begin
5130 -- if Some_Condition then
5131 -- declare
5132 -- package Pack is
5133 -- T : Task_Typ;
5134 -- end Pack; -- activation of T
5135 -- ...
5136 -- end A;
5137 --
5138 -- X : ... := A; -- root scenario
5139 --
5140 -- task body Task_Typ is -- task body
5141 -- ...
5142 -- end Task_Typ;
5143 --
5144 -- Y : ... := A; -- root scenario
5145 --
5146 -- IMPORTANT: The activation of T is a possible ABE for X, but
5147 -- not for Y. Intalling an unconditional ABE raise prior to the
5148 -- activation call would be wrong as it will fail for Y as well
5149 -- but in Y's case the activation of T is never an ABE.
5150
5151 elsif Present (Body_Decl)
5152 and then In_Extended_Main_Code_Unit (Body_Decl)
5153 then
5154 if Earlier_In_Extended_Unit (Root, Body_Decl) then
5155
5156 -- Do not emit any ABE diagnostics when a previous scenario in
5157 -- this traversal has suppressed elaboration warnings.
5158
5159 if New_In_State.Suppress_Warnings then
5160 null;
5161
5162 -- Do not emit any ABE diagnostics when the activation occurs
5163 -- in a partial finalization context because this action leads
5164 -- to confusing noise.
5165
5166 elsif New_In_State.Within_Partial_Finalization then
5167 null;
5168
5169 -- Otherwise emit the ABE disgnostic
5170
5171 else
5172 Error_Msg_Sloc := Sloc (Call);
5173 Error_Msg_N
5174 ("??task & will be activated # before elaboration of its "
5175 & "body", Obj_Id);
5176 Error_Msg_N
5177 ("\Program_Error may be raised at run time", Obj_Id);
5178
5179 Output_Active_Scenarios (Obj_Id, New_In_State);
5180 end if;
5181
5182 -- Install a conditional run-time ABE check to verify that the
5183 -- task body has been elaborated prior to the activation call.
5184
5185 if Check_OK then
5186 Install_Scenario_ABE_Check
5187 (N => Call,
5188 Targ_Id => Defining_Entity (Spec_Decl),
5189 Targ_Rep => Task_Rep,
5190 Disable => Obj_Rep);
5191
5192 -- Update the state of the Processing phase to indicate that
5193 -- no implicit Elaborate[_All] pragma must be generated from
5194 -- this point on.
5195 --
5196 -- task type Task_Typ;
5197 --
5198 -- function A ... is
5199 -- begin
5200 -- if Some_Condition then
5201 -- declare
5202 -- package Pack is
5203 -- <ABE check>
5204 -- T : Task_Typ;
5205 -- end Pack; -- activation of T
5206 -- ...
5207 -- end A;
5208 --
5209 -- X : ... := A;
5210 --
5211 -- task body Task_Typ is
5212 -- begin
5213 -- External.Subp; -- imparts Elaborate_All
5214 -- end Task_Typ;
5215 --
5216 -- If Some_Condition is True, then the ABE check will fail
5217 -- at runtime and the call to External.Subp will never take
5218 -- place, rendering the implicit Elaborate_All useless.
5219 --
5220 -- If the value of Some_Condition is False, then the call
5221 -- to External.Subp will never take place, rendering the
5222 -- implicit Elaborate_All useless.
5223
5224 New_In_State.Suppress_Implicit_Pragmas := True;
5225 end if;
5226 end if;
5227
5228 -- Otherwise the task body is not available in this compilation or
5229 -- it resides in an external unit. Install a run-time ABE check to
5230 -- verify that the task body has been elaborated prior to the
5231 -- activation call when the dynamic model is in effect.
5232
5233 elsif Check_OK
5234 and then New_In_State.Processing = Dynamic_Model_Processing
5235 then
5236 Install_Unit_ABE_Check
5237 (N => Call,
5238 Unit_Id => Unit_Id,
5239 Disable => Obj_Rep);
5240 end if;
5241
5242 -- Both the activation call and task type are subject to SPARK_Mode
5243 -- On, this triggers the SPARK rules for task activation. Compared
5244 -- to calls and instantiations, task activation in SPARK does not
5245 -- require the presence of Elaborate[_All] pragmas in case the task
5246 -- type is defined outside the main unit. This is because SPARK uses
5247 -- a special policy which activates all tasks after the main unit has
5248 -- finished its elaboration.
5249
5250 if SPARK_Mode_Of (Call_Rep) = Is_On
5251 and then SPARK_Mode_Of (Task_Rep) = Is_On
5252 then
5253 null;
5254
5255 -- Otherwise the Ada rules are in effect. Ensure that the unit with
5256 -- the task body is elaborated prior to the main unit.
5257
5258 else
5259 Ensure_Prior_Elaboration
5260 (N => Call,
5261 Unit_Id => Unit_Id,
5262 Prag_Nam => Name_Elaborate_All,
5263 In_State => New_In_State);
5264 end if;
5265
5266 Traverse_Conditional_ABE_Body
5267 (N => Body_Decl,
5268 In_State => New_In_State);
5269 end Process_Conditional_ABE_Activation;
5270
5271 ----------------------------------
5272 -- Process_Conditional_ABE_Call --
5273 ----------------------------------
5274
5275 procedure Process_Conditional_ABE_Call
5276 (Call : Node_Id;
5277 Call_Rep : Scenario_Rep_Id;
5278 In_State : Processing_In_State)
5279 is
5280 function In_Initialization_Context (N : Node_Id) return Boolean;
5281 pragma Inline (In_Initialization_Context);
5282 -- Determine whether arbitrary node N appears within a type init
5283 -- proc, primitive [Deep_]Initialize, or a block created for
5284 -- initialization purposes.
5285
5286 function Is_Partial_Finalization_Proc
5287 (Subp_Id : Entity_Id) return Boolean;
5288 pragma Inline (Is_Partial_Finalization_Proc);
5289 -- Determine whether subprogram Subp_Id is a partial finalization
5290 -- procedure.
5291
5292 -------------------------------
5293 -- In_Initialization_Context --
5294 -------------------------------
5295
5296 function In_Initialization_Context (N : Node_Id) return Boolean is
5297 Par : Node_Id;
5298 Spec_Id : Entity_Id;
5299
5300 begin
5301 -- Climb the parent chain looking for initialization actions
5302
5303 Par := Parent (N);
5304 while Present (Par) loop
5305
5306 -- A block may be part of the initialization actions of a
5307 -- default initialized object.
5308
5309 if Nkind (Par) = N_Block_Statement
5310 and then Is_Initialization_Block (Par)
5311 then
5312 return True;
5313
5314 -- A subprogram body may denote an initialization routine
5315
5316 elsif Nkind (Par) = N_Subprogram_Body then
5317 Spec_Id := Unique_Defining_Entity (Par);
5318
5319 -- The current subprogram body denotes a type init proc or
5320 -- primitive [Deep_]Initialize.
5321
5322 if Is_Init_Proc (Spec_Id)
5323 or else Is_Controlled_Proc (Spec_Id, Name_Initialize)
5324 or else Is_TSS (Spec_Id, TSS_Deep_Initialize)
5325 then
5326 return True;
5327 end if;
5328
5329 -- Prevent the search from going too far
5330
5331 elsif Is_Body_Or_Package_Declaration (Par) then
5332 exit;
5333 end if;
5334
5335 Par := Parent (Par);
5336 end loop;
5337
5338 return False;
5339 end In_Initialization_Context;
5340
5341 ----------------------------------
5342 -- Is_Partial_Finalization_Proc --
5343 ----------------------------------
5344
5345 function Is_Partial_Finalization_Proc
5346 (Subp_Id : Entity_Id) return Boolean
5347 is
5348 begin
5349 -- To qualify, the subprogram must denote a finalizer procedure
5350 -- or primitive [Deep_]Finalize, and the call must appear within
5351 -- an initialization context.
5352
5353 return
5354 (Is_Controlled_Proc (Subp_Id, Name_Finalize)
5355 or else Is_Finalizer_Proc (Subp_Id)
5356 or else Is_TSS (Subp_Id, TSS_Deep_Finalize))
5357 and then In_Initialization_Context (Call);
5358 end Is_Partial_Finalization_Proc;
5359
5360 -- Local variables
5361
5362 Subp_Id : constant Entity_Id := Target (Call_Rep);
5363 Subp_Rep : constant Target_Rep_Id :=
5364 Target_Representation_Of (Subp_Id, In_State);
5365 Subp_Decl : constant Node_Id := Spec_Declaration (Subp_Rep);
5366
5367 SPARK_Rules_On : constant Boolean :=
5368 SPARK_Mode_Of (Call_Rep) = Is_On
5369 and then SPARK_Mode_Of (Subp_Rep) = Is_On;
5370
5371 New_In_State : Processing_In_State := In_State;
5372 -- Each step of the Processing phase constitutes a new state
5373
5374 -- Start of processing for Process_Conditional_ABE_Call
5375
5376 begin
5377 -- Output relevant information when switch -gnatel (info messages on
5378 -- implicit Elaborate[_All] pragmas) is in effect.
5379
5380 if Elab_Info_Messages
5381 and then not New_In_State.Suppress_Info_Messages
5382 then
5383 Info_Call
5384 (Call => Call,
5385 Subp_Id => Subp_Id,
5386 Info_Msg => True,
5387 In_SPARK => SPARK_Rules_On);
5388 end if;
5389
5390 -- Check whether the invocation of an entry clashes with an existing
5391 -- restriction. This check is relevant only when the processing was
5392 -- started from some library-level scenario.
5393
5394 if Is_Protected_Entry (Subp_Id) then
5395 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, Call);
5396
5397 elsif Is_Task_Entry (Subp_Id) then
5398 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, Call);
5399
5400 -- Task entry calls are never processed because the entry being
5401 -- invoked does not have a corresponding "body", it has a select.
5402
5403 return;
5404 end if;
5405
5406 -- Nothing to do when the call invokes a target defined within an
5407 -- instance and switch -gnatd_i (ignore activations and calls to
5408 -- instances for elaboration) is in effect.
5409
5410 if Debug_Flag_Underscore_I
5411 and then In_External_Instance
5412 (N => Call,
5413 Target_Decl => Subp_Decl)
5414 then
5415 return;
5416
5417 -- Nothing to do when the call is a guaranteed ABE
5418
5419 elsif Is_Known_Guaranteed_ABE (Call) then
5420 return;
5421
5422 -- Nothing to do when the root scenario appears at the declaration
5423 -- level and the target is in the same unit but outside this context.
5424 --
5425 -- function B ...; -- target declaration
5426 --
5427 -- procedure Proc is
5428 -- function A ... is
5429 -- begin
5430 -- if Some_Condition then
5431 -- return B; -- call site
5432 -- ...
5433 -- end A;
5434 --
5435 -- X : ... := A; -- root scenario
5436 -- ...
5437 --
5438 -- function B ... is
5439 -- ...
5440 -- end B;
5441 --
5442 -- In the example above, the context of X is the declarative region
5443 -- of Proc. The "elaboration" of X may eventually reach B which is
5444 -- defined outside of X's context. B is relevant only when Proc is
5445 -- invoked, but this happens only by means of "normal" elaboration,
5446 -- therefore B must not be considered if this is not the case.
5447
5448 elsif Is_Up_Level_Target
5449 (Targ_Decl => Subp_Decl,
5450 In_State => New_In_State)
5451 then
5452 return;
5453 end if;
5454
5455 -- Warnings are suppressed when a prior scenario is already in that
5456 -- mode, or the call or target have warnings suppressed. Update the
5457 -- state of the Processing phase to reflect this.
5458
5459 New_In_State.Suppress_Warnings :=
5460 New_In_State.Suppress_Warnings
5461 or else not Elaboration_Warnings_OK (Call_Rep)
5462 or else not Elaboration_Warnings_OK (Subp_Rep);
5463
5464 -- The call occurs in an initial condition context when a prior
5465 -- scenario is already in that mode, or when the target is an
5466 -- Initial_Condition procedure. Update the state of the Processing
5467 -- phase to reflect this.
5468
5469 New_In_State.Within_Initial_Condition :=
5470 New_In_State.Within_Initial_Condition
5471 or else Is_Initial_Condition_Proc (Subp_Id);
5472
5473 -- The call occurs in a partial finalization context when a prior
5474 -- scenario is already in that mode, or when the target denotes a
5475 -- [Deep_]Finalize primitive or a finalizer within an initialization
5476 -- context. Update the state of the Processing phase to reflect this.
5477
5478 New_In_State.Within_Partial_Finalization :=
5479 New_In_State.Within_Partial_Finalization
5480 or else Is_Partial_Finalization_Proc (Subp_Id);
5481
5482 -- The SPARK rules are in effect. Note that -gnatd.v (enforce SPARK
5483 -- elaboration rules in SPARK code) is intentionally not taken into
5484 -- account here because Process_Conditional_ABE_Call_SPARK has two
5485 -- separate modes of operation.
5486
5487 if SPARK_Rules_On then
5488 Process_Conditional_ABE_Call_SPARK
5489 (Call => Call,
5490 Call_Rep => Call_Rep,
5491 Subp_Id => Subp_Id,
5492 Subp_Rep => Subp_Rep,
5493 In_State => New_In_State);
5494
5495 -- Otherwise the Ada rules are in effect
5496
5497 else
5498 Process_Conditional_ABE_Call_Ada
5499 (Call => Call,
5500 Call_Rep => Call_Rep,
5501 Subp_Id => Subp_Id,
5502 Subp_Rep => Subp_Rep,
5503 In_State => New_In_State);
5504 end if;
5505
5506 -- Inspect the target body (and barried function) for other suitable
5507 -- elaboration scenarios.
5508
5509 Traverse_Conditional_ABE_Body
5510 (N => Barrier_Body_Declaration (Subp_Rep),
5511 In_State => New_In_State);
5512
5513 Traverse_Conditional_ABE_Body
5514 (N => Body_Declaration (Subp_Rep),
5515 In_State => New_In_State);
5516 end Process_Conditional_ABE_Call;
5517
5518 --------------------------------------
5519 -- Process_Conditional_ABE_Call_Ada --
5520 --------------------------------------
5521
5522 procedure Process_Conditional_ABE_Call_Ada
5523 (Call : Node_Id;
5524 Call_Rep : Scenario_Rep_Id;
5525 Subp_Id : Entity_Id;
5526 Subp_Rep : Target_Rep_Id;
5527 In_State : Processing_In_State)
5528 is
5529 Body_Decl : constant Node_Id := Body_Declaration (Subp_Rep);
5530 Root : constant Node_Id := Root_Scenario;
5531 Unit_Id : constant Node_Id := Unit (Subp_Rep);
5532
5533 Check_OK : constant Boolean :=
5534 not In_State.Suppress_Checks
5535 and then Ghost_Mode_Of (Call_Rep) /= Is_Ignored
5536 and then Ghost_Mode_Of (Subp_Rep) /= Is_Ignored
5537 and then Elaboration_Checks_OK (Call_Rep)
5538 and then Elaboration_Checks_OK (Subp_Rep);
5539 -- A run-time ABE check may be installed only when both the call
5540 -- and the target have active elaboration checks, and both are not
5541 -- ignored Ghost constructs.
5542
5543 New_In_State : Processing_In_State := In_State;
5544 -- Each step of the Processing phase constitutes a new state
5545
5546 begin
5547 -- Nothing to do for an Ada dispatching call because there are no
5548 -- ABE diagnostics for either models. ABE checks for the dynamic
5549 -- model are handled by Install_Primitive_Elaboration_Check.
5550
5551 if Is_Dispatching_Call (Call_Rep) then
5552 return;
5553
5554 -- Nothing to do when the call is ABE-safe
5555 --
5556 -- generic
5557 -- function Gen ...;
5558 --
5559 -- function Gen ... is
5560 -- begin
5561 -- ...
5562 -- end Gen;
5563 --
5564 -- with Gen;
5565 -- procedure Main is
5566 -- function Inst is new Gen;
5567 -- X : ... := Inst; -- safe call
5568 -- ...
5569
5570 elsif Is_Safe_Call (Call, Subp_Id, Subp_Rep) then
5571 return;
5572
5573 -- The call and the target body are both in the main unit
5574 --
5575 -- If the root scenario appears prior to the target body, then this
5576 -- is a possible ABE with respect to the root scenario.
5577 --
5578 -- function B ...;
5579 --
5580 -- function A ... is
5581 -- begin
5582 -- if Some_Condition then
5583 -- return B; -- call site
5584 -- ...
5585 -- end A;
5586 --
5587 -- X : ... := A; -- root scenario
5588 --
5589 -- function B ... is -- target body
5590 -- ...
5591 -- end B;
5592 --
5593 -- Y : ... := A; -- root scenario
5594 --
5595 -- IMPORTANT: The call to B from A is a possible ABE for X, but
5596 -- not for Y. Installing an unconditional ABE raise prior to the
5597 -- call to B would be wrong as it will fail for Y as well, but in
5598 -- Y's case the call to B is never an ABE.
5599
5600 elsif Present (Body_Decl)
5601 and then In_Extended_Main_Code_Unit (Body_Decl)
5602 then
5603 if Earlier_In_Extended_Unit (Root, Body_Decl) then
5604
5605 -- Do not emit any ABE diagnostics when a previous scenario in
5606 -- this traversal has suppressed elaboration warnings.
5607
5608 if New_In_State.Suppress_Warnings then
5609 null;
5610
5611 -- Do not emit any ABE diagnostics when the call occurs in a
5612 -- partial finalization context because this leads to confusing
5613 -- noise.
5614
5615 elsif New_In_State.Within_Partial_Finalization then
5616 null;
5617
5618 -- Otherwise emit the ABE diagnostic
5619
5620 else
5621 Error_Msg_NE
5622 ("??cannot call & before body seen", Call, Subp_Id);
5623 Error_Msg_N
5624 ("\Program_Error may be raised at run time", Call);
5625
5626 Output_Active_Scenarios (Call, New_In_State);
5627 end if;
5628
5629 -- Install a conditional run-time ABE check to verify that the
5630 -- target body has been elaborated prior to the call.
5631
5632 if Check_OK then
5633 Install_Scenario_ABE_Check
5634 (N => Call,
5635 Targ_Id => Subp_Id,
5636 Targ_Rep => Subp_Rep,
5637 Disable => Call_Rep);
5638
5639 -- Update the state of the Processing phase to indicate that
5640 -- no implicit Elaborate[_All] pragma must be generated from
5641 -- this point on.
5642 --
5643 -- function B ...;
5644 --
5645 -- function A ... is
5646 -- begin
5647 -- if Some_Condition then
5648 -- <ABE check>
5649 -- return B;
5650 -- ...
5651 -- end A;
5652 --
5653 -- X : ... := A;
5654 --
5655 -- function B ... is
5656 -- External.Subp; -- imparts Elaborate_All
5657 -- end B;
5658 --
5659 -- If Some_Condition is True, then the ABE check will fail
5660 -- at runtime and the call to External.Subp will never take
5661 -- place, rendering the implicit Elaborate_All useless.
5662 --
5663 -- If the value of Some_Condition is False, then the call
5664 -- to External.Subp will never take place, rendering the
5665 -- implicit Elaborate_All useless.
5666
5667 New_In_State.Suppress_Implicit_Pragmas := True;
5668 end if;
5669 end if;
5670
5671 -- Otherwise the target body is not available in this compilation or
5672 -- it resides in an external unit. Install a run-time ABE check to
5673 -- verify that the target body has been elaborated prior to the call
5674 -- site when the dynamic model is in effect.
5675
5676 elsif Check_OK
5677 and then New_In_State.Processing = Dynamic_Model_Processing
5678 then
5679 Install_Unit_ABE_Check
5680 (N => Call,
5681 Unit_Id => Unit_Id,
5682 Disable => Call_Rep);
5683 end if;
5684
5685 -- Ensure that the unit with the target body is elaborated prior to
5686 -- the main unit. The implicit Elaborate[_All] is generated only when
5687 -- the call has elaboration checks enabled. This behaviour parallels
5688 -- that of the old ABE mechanism.
5689
5690 if Elaboration_Checks_OK (Call_Rep) then
5691 Ensure_Prior_Elaboration
5692 (N => Call,
5693 Unit_Id => Unit_Id,
5694 Prag_Nam => Name_Elaborate_All,
5695 In_State => New_In_State);
5696 end if;
5697 end Process_Conditional_ABE_Call_Ada;
5698
5699 ----------------------------------------
5700 -- Process_Conditional_ABE_Call_SPARK --
5701 ----------------------------------------
5702
5703 procedure Process_Conditional_ABE_Call_SPARK
5704 (Call : Node_Id;
5705 Call_Rep : Scenario_Rep_Id;
5706 Subp_Id : Entity_Id;
5707 Subp_Rep : Target_Rep_Id;
5708 In_State : Processing_In_State)
5709 is
5710 pragma Unreferenced (Call_Rep);
5711
5712 Body_Decl : constant Node_Id := Body_Declaration (Subp_Rep);
5713 Region : Node_Id;
5714
5715 begin
5716 -- Ensure that a suitable elaboration model is in effect for SPARK
5717 -- rule verification.
5718
5719 Check_SPARK_Model_In_Effect;
5720
5721 -- The call and the target body are both in the main unit
5722
5723 if Present (Body_Decl)
5724 and then In_Extended_Main_Code_Unit (Body_Decl)
5725 and then Earlier_In_Extended_Unit (Call, Body_Decl)
5726 then
5727 -- Do not emit any ABE diagnostics when a previous scenario in
5728 -- this traversal has suppressed elaboration warnings.
5729
5730 if In_State.Suppress_Warnings then
5731 null;
5732
5733 -- Do not emit any ABE diagnostics when the call occurs in an
5734 -- initial condition context because this leads to incorrect
5735 -- diagnostics.
5736
5737 elsif In_State.Within_Initial_Condition then
5738 null;
5739
5740 -- Do not emit any ABE diagnostics when the call occurs in a
5741 -- partial finalization context because this leads to confusing
5742 -- noise.
5743
5744 elsif In_State.Within_Partial_Finalization then
5745 null;
5746
5747 -- Ensure that a call that textually precedes the subprogram body
5748 -- it invokes appears within the early call region of the body.
5749 --
5750 -- IMPORTANT: This check must always be performed even when switch
5751 -- -gnatd.v (enforce SPARK elaboration rules in SPARK code) is not
5752 -- specified because the static model cannot guarantee the absence
5753 -- of elaboration issues when dispatching calls are involved.
5754
5755 else
5756 Region := Find_Early_Call_Region (Body_Decl);
5757
5758 if Earlier_In_Extended_Unit (Call, Region) then
5759 Error_Msg_NE
5760 ("call must appear within early call region of subprogram "
5761 & "body & (SPARK RM 7.7(3))",
5762 Call, Subp_Id);
5763
5764 Error_Msg_Sloc := Sloc (Region);
5765 Error_Msg_N ("\region starts #", Call);
5766
5767 Error_Msg_Sloc := Sloc (Body_Decl);
5768 Error_Msg_N ("\region ends #", Call);
5769
5770 Output_Active_Scenarios (Call, In_State);
5771 end if;
5772 end if;
5773 end if;
5774
5775 -- A call to a source target or to a target which emulates Ada
5776 -- or SPARK semantics imposes an Elaborate_All requirement on the
5777 -- context of the main unit. Determine whether the context has a
5778 -- pragma strong enough to meet the requirement.
5779 --
5780 -- IMPORTANT: This check must be performed only when switch -gnatd.v
5781 -- (enforce SPARK elaboration rules in SPARK code) is active because
5782 -- the static model can ensure the prior elaboration of the unit
5783 -- which contains a body by installing an implicit Elaborate[_All]
5784 -- pragma.
5785
5786 if Debug_Flag_Dot_V then
5787 if Comes_From_Source (Subp_Id)
5788 or else Is_Ada_Semantic_Target (Subp_Id)
5789 or else Is_SPARK_Semantic_Target (Subp_Id)
5790 then
5791 Meet_Elaboration_Requirement
5792 (N => Call,
5793 Targ_Id => Subp_Id,
5794 Req_Nam => Name_Elaborate_All,
5795 In_State => In_State);
5796 end if;
5797
5798 -- Otherwise ensure that the unit with the target body is elaborated
5799 -- prior to the main unit.
5800
5801 else
5802 Ensure_Prior_Elaboration
5803 (N => Call,
5804 Unit_Id => Unit (Subp_Rep),
5805 Prag_Nam => Name_Elaborate_All,
5806 In_State => In_State);
5807 end if;
5808 end Process_Conditional_ABE_Call_SPARK;
5809
5810 -------------------------------------------
5811 -- Process_Conditional_ABE_Instantiation --
5812 -------------------------------------------
5813
5814 procedure Process_Conditional_ABE_Instantiation
5815 (Inst : Node_Id;
5816 Inst_Rep : Scenario_Rep_Id;
5817 In_State : Processing_In_State)
5818 is
5819 Gen_Id : constant Entity_Id := Target (Inst_Rep);
5820 Gen_Rep : constant Target_Rep_Id :=
5821 Target_Representation_Of (Gen_Id, In_State);
5822
5823 SPARK_Rules_On : constant Boolean :=
5824 SPARK_Mode_Of (Inst_Rep) = Is_On
5825 and then SPARK_Mode_Of (Gen_Rep) = Is_On;
5826
5827 New_In_State : Processing_In_State := In_State;
5828 -- Each step of the Processing phase constitutes a new state
5829
5830 begin
5831 -- Output relevant information when switch -gnatel (info messages on
5832 -- implicit Elaborate[_All] pragmas) is in effect.
5833
5834 if Elab_Info_Messages
5835 and then not New_In_State.Suppress_Info_Messages
5836 then
5837 Info_Instantiation
5838 (Inst => Inst,
5839 Gen_Id => Gen_Id,
5840 Info_Msg => True,
5841 In_SPARK => SPARK_Rules_On);
5842 end if;
5843
5844 -- Nothing to do when the instantiation is a guaranteed ABE
5845
5846 if Is_Known_Guaranteed_ABE (Inst) then
5847 return;
5848
5849 -- Nothing to do when the root scenario appears at the declaration
5850 -- level and the generic is in the same unit, but outside this
5851 -- context.
5852 --
5853 -- generic
5854 -- procedure Gen is ...; -- generic declaration
5855 --
5856 -- procedure Proc is
5857 -- function A ... is
5858 -- begin
5859 -- if Some_Condition then
5860 -- declare
5861 -- procedure I is new Gen; -- instantiation site
5862 -- ...
5863 -- ...
5864 -- end A;
5865 --
5866 -- X : ... := A; -- root scenario
5867 -- ...
5868 --
5869 -- procedure Gen is
5870 -- ...
5871 -- end Gen;
5872 --
5873 -- In the example above, the context of X is the declarative region
5874 -- of Proc. The "elaboration" of X may eventually reach Gen which
5875 -- appears outside of X's context. Gen is relevant only when Proc is
5876 -- invoked, but this happens only by means of "normal" elaboration,
5877 -- therefore Gen must not be considered if this is not the case.
5878
5879 elsif Is_Up_Level_Target
5880 (Targ_Decl => Spec_Declaration (Gen_Rep),
5881 In_State => New_In_State)
5882 then
5883 return;
5884 end if;
5885
5886 -- Warnings are suppressed when a prior scenario is already in that
5887 -- mode, or when the instantiation has warnings suppressed. Update
5888 -- the state of the processing phase to reflect this.
5889
5890 New_In_State.Suppress_Warnings :=
5891 New_In_State.Suppress_Warnings
5892 or else not Elaboration_Warnings_OK (Inst_Rep);
5893
5894 -- The SPARK rules are in effect
5895
5896 if SPARK_Rules_On then
5897 Process_Conditional_ABE_Instantiation_SPARK
5898 (Inst => Inst,
5899 Inst_Rep => Inst_Rep,
5900 Gen_Id => Gen_Id,
5901 Gen_Rep => Gen_Rep,
5902 In_State => New_In_State);
5903
5904 -- Otherwise the Ada rules are in effect, or SPARK code is allowed to
5905 -- violate the SPARK rules.
5906
5907 else
5908 Process_Conditional_ABE_Instantiation_Ada
5909 (Inst => Inst,
5910 Inst_Rep => Inst_Rep,
5911 Gen_Id => Gen_Id,
5912 Gen_Rep => Gen_Rep,
5913 In_State => New_In_State);
5914 end if;
5915 end Process_Conditional_ABE_Instantiation;
5916
5917 -----------------------------------------------
5918 -- Process_Conditional_ABE_Instantiation_Ada --
5919 -----------------------------------------------
5920
5921 procedure Process_Conditional_ABE_Instantiation_Ada
5922 (Inst : Node_Id;
5923 Inst_Rep : Scenario_Rep_Id;
5924 Gen_Id : Entity_Id;
5925 Gen_Rep : Target_Rep_Id;
5926 In_State : Processing_In_State)
5927 is
5928 Body_Decl : constant Node_Id := Body_Declaration (Gen_Rep);
5929 Root : constant Node_Id := Root_Scenario;
5930 Unit_Id : constant Entity_Id := Unit (Gen_Rep);
5931
5932 Check_OK : constant Boolean :=
5933 not In_State.Suppress_Checks
5934 and then Ghost_Mode_Of (Inst_Rep) /= Is_Ignored
5935 and then Ghost_Mode_Of (Gen_Rep) /= Is_Ignored
5936 and then Elaboration_Checks_OK (Inst_Rep)
5937 and then Elaboration_Checks_OK (Gen_Rep);
5938 -- A run-time ABE check may be installed only when both the instance
5939 -- and the generic have active elaboration checks and both are not
5940 -- ignored Ghost constructs.
5941
5942 New_In_State : Processing_In_State := In_State;
5943 -- Each step of the Processing phase constitutes a new state
5944
5945 begin
5946 -- Nothing to do when the instantiation is ABE-safe
5947 --
5948 -- generic
5949 -- package Gen is
5950 -- ...
5951 -- end Gen;
5952 --
5953 -- package body Gen is
5954 -- ...
5955 -- end Gen;
5956 --
5957 -- with Gen;
5958 -- procedure Main is
5959 -- package Inst is new Gen (ABE); -- safe instantiation
5960 -- ...
5961
5962 if Is_Safe_Instantiation (Inst, Gen_Id, Gen_Rep) then
5963 return;
5964
5965 -- The instantiation and the generic body are both in the main unit
5966 --
5967 -- If the root scenario appears prior to the generic body, then this
5968 -- is a possible ABE with respect to the root scenario.
5969 --
5970 -- generic
5971 -- package Gen is
5972 -- ...
5973 -- end Gen;
5974 --
5975 -- function A ... is
5976 -- begin
5977 -- if Some_Condition then
5978 -- declare
5979 -- package Inst is new Gen; -- instantiation site
5980 -- ...
5981 -- end A;
5982 --
5983 -- X : ... := A; -- root scenario
5984 --
5985 -- package body Gen is -- generic body
5986 -- ...
5987 -- end Gen;
5988 --
5989 -- Y : ... := A; -- root scenario
5990 --
5991 -- IMPORTANT: The instantiation of Gen is a possible ABE for X,
5992 -- but not for Y. Installing an unconditional ABE raise prior to
5993 -- the instance site would be wrong as it will fail for Y as well,
5994 -- but in Y's case the instantiation of Gen is never an ABE.
5995
5996 elsif Present (Body_Decl)
5997 and then In_Extended_Main_Code_Unit (Body_Decl)
5998 then
5999 if Earlier_In_Extended_Unit (Root, Body_Decl) then
6000
6001 -- Do not emit any ABE diagnostics when a previous scenario in
6002 -- this traversal has suppressed elaboration warnings.
6003
6004 if New_In_State.Suppress_Warnings then
6005 null;
6006
6007 -- Do not emit any ABE diagnostics when the instantiation
6008 -- occurs in partial finalization context because this leads
6009 -- to unwanted noise.
6010
6011 elsif New_In_State.Within_Partial_Finalization then
6012 null;
6013
6014 -- Otherwise output the diagnostic
6015
6016 else
6017 Error_Msg_NE
6018 ("??cannot instantiate & before body seen", Inst, Gen_Id);
6019 Error_Msg_N
6020 ("\Program_Error may be raised at run time", Inst);
6021
6022 Output_Active_Scenarios (Inst, New_In_State);
6023 end if;
6024
6025 -- Install a conditional run-time ABE check to verify that the
6026 -- generic body has been elaborated prior to the instantiation.
6027
6028 if Check_OK then
6029 Install_Scenario_ABE_Check
6030 (N => Inst,
6031 Targ_Id => Gen_Id,
6032 Targ_Rep => Gen_Rep,
6033 Disable => Inst_Rep);
6034
6035 -- Update the state of the Processing phase to indicate that
6036 -- no implicit Elaborate[_All] pragma must be generated from
6037 -- this point on.
6038 --
6039 -- generic
6040 -- package Gen is
6041 -- ...
6042 -- end Gen;
6043 --
6044 -- function A ... is
6045 -- begin
6046 -- if Some_Condition then
6047 -- <ABE check>
6048 -- declare Inst is new Gen;
6049 -- ...
6050 -- end A;
6051 --
6052 -- X : ... := A;
6053 --
6054 -- package body Gen is
6055 -- begin
6056 -- External.Subp; -- imparts Elaborate_All
6057 -- end Gen;
6058 --
6059 -- If Some_Condition is True, then the ABE check will fail
6060 -- at runtime and the call to External.Subp will never take
6061 -- place, rendering the implicit Elaborate_All useless.
6062 --
6063 -- If the value of Some_Condition is False, then the call
6064 -- to External.Subp will never take place, rendering the
6065 -- implicit Elaborate_All useless.
6066
6067 New_In_State.Suppress_Implicit_Pragmas := True;
6068 end if;
6069 end if;
6070
6071 -- Otherwise the generic body is not available in this compilation
6072 -- or it resides in an external unit. Install a run-time ABE check
6073 -- to verify that the generic body has been elaborated prior to the
6074 -- instantiation when the dynamic model is in effect.
6075
6076 elsif Check_OK
6077 and then New_In_State.Processing = Dynamic_Model_Processing
6078 then
6079 Install_Unit_ABE_Check
6080 (N => Inst,
6081 Unit_Id => Unit_Id,
6082 Disable => Inst_Rep);
6083 end if;
6084
6085 -- Ensure that the unit with the generic body is elaborated prior
6086 -- to the main unit. No implicit pragma has to be generated if the
6087 -- instantiation has elaboration checks suppressed. This behaviour
6088 -- parallels that of the old ABE mechanism.
6089
6090 if Elaboration_Checks_OK (Inst_Rep) then
6091 Ensure_Prior_Elaboration
6092 (N => Inst,
6093 Unit_Id => Unit_Id,
6094 Prag_Nam => Name_Elaborate,
6095 In_State => New_In_State);
6096 end if;
6097 end Process_Conditional_ABE_Instantiation_Ada;
6098
6099 -------------------------------------------------
6100 -- Process_Conditional_ABE_Instantiation_SPARK --
6101 -------------------------------------------------
6102
6103 procedure Process_Conditional_ABE_Instantiation_SPARK
6104 (Inst : Node_Id;
6105 Inst_Rep : Scenario_Rep_Id;
6106 Gen_Id : Entity_Id;
6107 Gen_Rep : Target_Rep_Id;
6108 In_State : Processing_In_State)
6109 is
6110 pragma Unreferenced (Inst_Rep);
6111
6112 Req_Nam : Name_Id;
6113
6114 begin
6115 -- Ensure that a suitable elaboration model is in effect for SPARK
6116 -- rule verification.
6117
6118 Check_SPARK_Model_In_Effect;
6119
6120 -- A source instantiation imposes an Elaborate[_All] requirement
6121 -- on the context of the main unit. Determine whether the context
6122 -- has a pragma strong enough to meet the requirement. The check
6123 -- is orthogonal to the ABE ramifications of the instantiation.
6124 --
6125 -- IMPORTANT: This check must be performed only when switch -gnatd.v
6126 -- (enforce SPARK elaboration rules in SPARK code) is active because
6127 -- the static model can ensure the prior elaboration of the unit
6128 -- which contains a body by installing an implicit Elaborate[_All]
6129 -- pragma.
6130
6131 if Debug_Flag_Dot_V then
6132 if Nkind (Inst) = N_Package_Instantiation then
6133 Req_Nam := Name_Elaborate_All;
6134 else
6135 Req_Nam := Name_Elaborate;
6136 end if;
6137
6138 Meet_Elaboration_Requirement
6139 (N => Inst,
6140 Targ_Id => Gen_Id,
6141 Req_Nam => Req_Nam,
6142 In_State => In_State);
6143
6144 -- Otherwise ensure that the unit with the target body is elaborated
6145 -- prior to the main unit.
6146
6147 else
6148 Ensure_Prior_Elaboration
6149 (N => Inst,
6150 Unit_Id => Unit (Gen_Rep),
6151 Prag_Nam => Name_Elaborate,
6152 In_State => In_State);
6153 end if;
6154 end Process_Conditional_ABE_Instantiation_SPARK;
6155
6156 -------------------------------------------------
6157 -- Process_Conditional_ABE_Variable_Assignment --
6158 -------------------------------------------------
6159
6160 procedure Process_Conditional_ABE_Variable_Assignment
6161 (Asmt : Node_Id;
6162 Asmt_Rep : Scenario_Rep_Id;
6163 In_State : Processing_In_State)
6164 is
6165
6166 Var_Id : constant Entity_Id := Target (Asmt_Rep);
6167 Var_Rep : constant Target_Rep_Id :=
6168 Target_Representation_Of (Var_Id, In_State);
6169
6170 SPARK_Rules_On : constant Boolean :=
6171 SPARK_Mode_Of (Asmt_Rep) = Is_On
6172 and then SPARK_Mode_Of (Var_Rep) = Is_On;
6173
6174 begin
6175 -- Output relevant information when switch -gnatel (info messages on
6176 -- implicit Elaborate[_All] pragmas) is in effect.
6177
6178 if Elab_Info_Messages
6179 and then not In_State.Suppress_Info_Messages
6180 then
6181 Elab_Msg_NE
6182 (Msg => "assignment to & during elaboration",
6183 N => Asmt,
6184 Id => Var_Id,
6185 Info_Msg => True,
6186 In_SPARK => SPARK_Rules_On);
6187 end if;
6188
6189 -- The SPARK rules are in effect. These rules are applied regardless
6190 -- of whether switch -gnatd.v (enforce SPARK elaboration rules in
6191 -- SPARK code) is in effect because the static model cannot ensure
6192 -- safe assignment of variables.
6193
6194 if SPARK_Rules_On then
6195 Process_Conditional_ABE_Variable_Assignment_SPARK
6196 (Asmt => Asmt,
6197 Asmt_Rep => Asmt_Rep,
6198 Var_Id => Var_Id,
6199 Var_Rep => Var_Rep,
6200 In_State => In_State);
6201
6202 -- Otherwise the Ada rules are in effect
6203
6204 else
6205 Process_Conditional_ABE_Variable_Assignment_Ada
6206 (Asmt => Asmt,
6207 Asmt_Rep => Asmt_Rep,
6208 Var_Id => Var_Id,
6209 Var_Rep => Var_Rep,
6210 In_State => In_State);
6211 end if;
6212 end Process_Conditional_ABE_Variable_Assignment;
6213
6214 -----------------------------------------------------
6215 -- Process_Conditional_ABE_Variable_Assignment_Ada --
6216 -----------------------------------------------------
6217
6218 procedure Process_Conditional_ABE_Variable_Assignment_Ada
6219 (Asmt : Node_Id;
6220 Asmt_Rep : Scenario_Rep_Id;
6221 Var_Id : Entity_Id;
6222 Var_Rep : Target_Rep_Id;
6223 In_State : Processing_In_State)
6224 is
6225 pragma Unreferenced (Asmt_Rep);
6226
6227 Var_Decl : constant Node_Id := Variable_Declaration (Var_Rep);
6228 Unit_Id : constant Entity_Id := Unit (Var_Rep);
6229
6230 begin
6231 -- Emit a warning when an uninitialized variable declared in a
6232 -- package spec without a pragma Elaborate_Body is initialized
6233 -- by elaboration code within the corresponding body.
6234
6235 if Is_Elaboration_Warnings_OK_Id (Var_Id)
6236 and then not Is_Initialized (Var_Decl)
6237 and then not Has_Pragma_Elaborate_Body (Unit_Id)
6238 then
6239 -- Do not emit any ABE diagnostics when a previous scenario in
6240 -- this traversal has suppressed elaboration warnings.
6241
6242 if not In_State.Suppress_Warnings then
6243 Error_Msg_NE
6244 ("??variable & can be accessed by clients before this "
6245 & "initialization", Asmt, Var_Id);
6246
6247 Error_Msg_NE
6248 ("\add pragma ""Elaborate_Body"" to spec & to ensure proper "
6249 & "initialization", Asmt, Unit_Id);
6250
6251 Output_Active_Scenarios (Asmt, In_State);
6252 end if;
6253
6254 -- Generate an implicit Elaborate_Body in the spec
6255
6256 Set_Elaborate_Body_Desirable (Unit_Id);
6257 end if;
6258 end Process_Conditional_ABE_Variable_Assignment_Ada;
6259
6260 -------------------------------------------------------
6261 -- Process_Conditional_ABE_Variable_Assignment_SPARK --
6262 -------------------------------------------------------
6263
6264 procedure Process_Conditional_ABE_Variable_Assignment_SPARK
6265 (Asmt : Node_Id;
6266 Asmt_Rep : Scenario_Rep_Id;
6267 Var_Id : Entity_Id;
6268 Var_Rep : Target_Rep_Id;
6269 In_State : Processing_In_State)
6270 is
6271 pragma Unreferenced (Asmt_Rep);
6272
6273 Var_Decl : constant Node_Id := Variable_Declaration (Var_Rep);
6274 Unit_Id : constant Entity_Id := Unit (Var_Rep);
6275
6276 begin
6277 -- Ensure that a suitable elaboration model is in effect for SPARK
6278 -- rule verification.
6279
6280 Check_SPARK_Model_In_Effect;
6281
6282 -- Do not emit any ABE diagnostics when a previous scenario in this
6283 -- traversal has suppressed elaboration warnings.
6284
6285 if In_State.Suppress_Warnings then
6286 null;
6287
6288 -- Emit an error when an initialized variable declared in a package
6289 -- spec that is missing pragma Elaborate_Body is further modified by
6290 -- elaboration code within the corresponding body.
6291
6292 elsif Is_Elaboration_Warnings_OK_Id (Var_Id)
6293 and then Is_Initialized (Var_Decl)
6294 and then not Has_Pragma_Elaborate_Body (Unit_Id)
6295 then
6296 Error_Msg_NE
6297 ("variable & modified by elaboration code in package body",
6298 Asmt, Var_Id);
6299
6300 Error_Msg_NE
6301 ("\add pragma ""Elaborate_Body"" to spec & to ensure full "
6302 & "initialization", Asmt, Unit_Id);
6303
6304 Output_Active_Scenarios (Asmt, In_State);
6305 end if;
6306 end Process_Conditional_ABE_Variable_Assignment_SPARK;
6307
6308 ------------------------------------------------
6309 -- Process_Conditional_ABE_Variable_Reference --
6310 ------------------------------------------------
6311
6312 procedure Process_Conditional_ABE_Variable_Reference
6313 (Ref : Node_Id;
6314 Ref_Rep : Scenario_Rep_Id;
6315 In_State : Processing_In_State)
6316 is
6317 Var_Id : constant Entity_Id := Target (Ref);
6318 Var_Rep : Target_Rep_Id;
6319 Unit_Id : Entity_Id;
6320
6321 begin
6322 -- Nothing to do when the variable reference is not a read
6323
6324 if not Is_Read_Reference (Ref_Rep) then
6325 return;
6326 end if;
6327
6328 Var_Rep := Target_Representation_Of (Var_Id, In_State);
6329 Unit_Id := Unit (Var_Rep);
6330
6331 -- Output relevant information when switch -gnatel (info messages on
6332 -- implicit Elaborate[_All] pragmas) is in effect.
6333
6334 if Elab_Info_Messages
6335 and then not In_State.Suppress_Info_Messages
6336 then
6337 Elab_Msg_NE
6338 (Msg => "read of variable & during elaboration",
6339 N => Ref,
6340 Id => Var_Id,
6341 Info_Msg => True,
6342 In_SPARK => True);
6343 end if;
6344
6345 -- Nothing to do when the variable appears within the main unit
6346 -- because diagnostics on reads are relevant only for external
6347 -- variables.
6348
6349 if Is_Same_Unit (Unit_Id, Main_Unit_Entity) then
6350 null;
6351
6352 -- Nothing to do when the variable is already initialized. Note that
6353 -- the variable may be further modified by the external unit.
6354
6355 elsif Is_Initialized (Variable_Declaration (Var_Rep)) then
6356 null;
6357
6358 -- Nothing to do when the external unit guarantees the initialization
6359 -- of the variable by means of pragma Elaborate_Body.
6360
6361 elsif Has_Pragma_Elaborate_Body (Unit_Id) then
6362 null;
6363
6364 -- A variable read imposes an Elaborate requirement on the context of
6365 -- the main unit. Determine whether the context has a pragma strong
6366 -- enough to meet the requirement.
6367
6368 else
6369 Meet_Elaboration_Requirement
6370 (N => Ref,
6371 Targ_Id => Var_Id,
6372 Req_Nam => Name_Elaborate,
6373 In_State => In_State);
6374 end if;
6375 end Process_Conditional_ABE_Variable_Reference;
6376
6377 -----------------------------------
6378 -- Traverse_Conditional_ABE_Body --
6379 -----------------------------------
6380
6381 procedure Traverse_Conditional_ABE_Body
6382 (N : Node_Id;
6383 In_State : Processing_In_State)
6384 is
6385 begin
6386 Traverse_Body
6387 (N => N,
6388 Requires_Processing => Is_Conditional_ABE_Scenario'Access,
6389 Processor => Process_Conditional_ABE'Access,
6390 In_State => In_State);
6391 end Traverse_Conditional_ABE_Body;
6392 end Conditional_ABE_Processor;
6393
6394 -------------
6395 -- Destroy --
6396 -------------
6397
6398 procedure Destroy (NE : in out Node_Or_Entity_Id) is
6399 pragma Unreferenced (NE);
6400 begin
6401 null;
6402 end Destroy;
6403
6404 -----------------
6405 -- Diagnostics --
6406 -----------------
6407
6408 package body Diagnostics is
6409
6410 -----------------
6411 -- Elab_Msg_NE --
6412 -----------------
6413
6414 procedure Elab_Msg_NE
6415 (Msg : String;
6416 N : Node_Id;
6417 Id : Entity_Id;
6418 Info_Msg : Boolean;
6419 In_SPARK : Boolean)
6420 is
6421 function Prefix return String;
6422 pragma Inline (Prefix);
6423 -- Obtain the prefix of the message
6424
6425 function Suffix return String;
6426 pragma Inline (Suffix);
6427 -- Obtain the suffix of the message
6428
6429 ------------
6430 -- Prefix --
6431 ------------
6432
6433 function Prefix return String is
6434 begin
6435 if Info_Msg then
6436 return "info: ";
6437 else
6438 return "";
6439 end if;
6440 end Prefix;
6441
6442 ------------
6443 -- Suffix --
6444 ------------
6445
6446 function Suffix return String is
6447 begin
6448 if In_SPARK then
6449 return " in SPARK";
6450 else
6451 return "";
6452 end if;
6453 end Suffix;
6454
6455 -- Start of processing for Elab_Msg_NE
6456
6457 begin
6458 Error_Msg_NE (Prefix & Msg & Suffix, N, Id);
6459 end Elab_Msg_NE;
6460
6461 ---------------
6462 -- Info_Call --
6463 ---------------
6464
6465 procedure Info_Call
6466 (Call : Node_Id;
6467 Subp_Id : Entity_Id;
6468 Info_Msg : Boolean;
6469 In_SPARK : Boolean)
6470 is
6471 procedure Info_Accept_Alternative;
6472 pragma Inline (Info_Accept_Alternative);
6473 -- Output information concerning an accept alternative
6474
6475 procedure Info_Simple_Call;
6476 pragma Inline (Info_Simple_Call);
6477 -- Output information concerning the call
6478
6479 procedure Info_Type_Actions (Action : String);
6480 pragma Inline (Info_Type_Actions);
6481 -- Output information concerning action Action of a type
6482
6483 procedure Info_Verification_Call
6484 (Pred : String;
6485 Id : Entity_Id;
6486 Id_Kind : String);
6487 pragma Inline (Info_Verification_Call);
6488 -- Output information concerning the verification of predicate Pred
6489 -- applied to related entity Id with kind Id_Kind.
6490
6491 -----------------------------
6492 -- Info_Accept_Alternative --
6493 -----------------------------
6494
6495 procedure Info_Accept_Alternative is
6496 Entry_Id : constant Entity_Id := Receiving_Entry (Subp_Id);
6497 pragma Assert (Present (Entry_Id));
6498
6499 begin
6500 Elab_Msg_NE
6501 (Msg => "accept for entry & during elaboration",
6502 N => Call,
6503 Id => Entry_Id,
6504 Info_Msg => Info_Msg,
6505 In_SPARK => In_SPARK);
6506 end Info_Accept_Alternative;
6507
6508 ----------------------
6509 -- Info_Simple_Call --
6510 ----------------------
6511
6512 procedure Info_Simple_Call is
6513 begin
6514 Elab_Msg_NE
6515 (Msg => "call to & during elaboration",
6516 N => Call,
6517 Id => Subp_Id,
6518 Info_Msg => Info_Msg,
6519 In_SPARK => In_SPARK);
6520 end Info_Simple_Call;
6521
6522 -----------------------
6523 -- Info_Type_Actions --
6524 -----------------------
6525
6526 procedure Info_Type_Actions (Action : String) is
6527 Typ : constant Entity_Id := First_Formal_Type (Subp_Id);
6528 pragma Assert (Present (Typ));
6529
6530 begin
6531 Elab_Msg_NE
6532 (Msg => Action & " actions for type & during elaboration",
6533 N => Call,
6534 Id => Typ,
6535 Info_Msg => Info_Msg,
6536 In_SPARK => In_SPARK);
6537 end Info_Type_Actions;
6538
6539 ----------------------------
6540 -- Info_Verification_Call --
6541 ----------------------------
6542
6543 procedure Info_Verification_Call
6544 (Pred : String;
6545 Id : Entity_Id;
6546 Id_Kind : String)
6547 is
6548 pragma Assert (Present (Id));
6549
6550 begin
6551 Elab_Msg_NE
6552 (Msg =>
6553 "verification of " & Pred & " of " & Id_Kind & " & during "
6554 & "elaboration",
6555 N => Call,
6556 Id => Id,
6557 Info_Msg => Info_Msg,
6558 In_SPARK => In_SPARK);
6559 end Info_Verification_Call;
6560
6561 -- Start of processing for Info_Call
6562
6563 begin
6564 -- Do not output anything for targets defined in internal units
6565 -- because this creates noise.
6566
6567 if not In_Internal_Unit (Subp_Id) then
6568
6569 -- Accept alternative
6570
6571 if Is_Accept_Alternative_Proc (Subp_Id) then
6572 Info_Accept_Alternative;
6573
6574 -- Adjustment
6575
6576 elsif Is_TSS (Subp_Id, TSS_Deep_Adjust) then
6577 Info_Type_Actions ("adjustment");
6578
6579 -- Default_Initial_Condition
6580
6581 elsif Is_Default_Initial_Condition_Proc (Subp_Id) then
6582 Info_Verification_Call
6583 (Pred => "Default_Initial_Condition",
6584 Id => First_Formal_Type (Subp_Id),
6585 Id_Kind => "type");
6586
6587 -- Entries
6588
6589 elsif Is_Protected_Entry (Subp_Id) then
6590 Info_Simple_Call;
6591
6592 -- Task entry calls are never processed because the entry being
6593 -- invoked does not have a corresponding "body", it has a select.
6594
6595 elsif Is_Task_Entry (Subp_Id) then
6596 null;
6597
6598 -- Finalization
6599
6600 elsif Is_TSS (Subp_Id, TSS_Deep_Finalize) then
6601 Info_Type_Actions ("finalization");
6602
6603 -- Calls to _Finalizer procedures must not appear in the output
6604 -- because this creates confusing noise.
6605
6606 elsif Is_Finalizer_Proc (Subp_Id) then
6607 null;
6608
6609 -- Initial_Condition
6610
6611 elsif Is_Initial_Condition_Proc (Subp_Id) then
6612 Info_Verification_Call
6613 (Pred => "Initial_Condition",
6614 Id => Find_Enclosing_Scope (Call),
6615 Id_Kind => "package");
6616
6617 -- Initialization
6618
6619 elsif Is_Init_Proc (Subp_Id)
6620 or else Is_TSS (Subp_Id, TSS_Deep_Initialize)
6621 then
6622 Info_Type_Actions ("initialization");
6623
6624 -- Invariant
6625
6626 elsif Is_Invariant_Proc (Subp_Id) then
6627 Info_Verification_Call
6628 (Pred => "invariants",
6629 Id => First_Formal_Type (Subp_Id),
6630 Id_Kind => "type");
6631
6632 -- Partial invariant calls must not appear in the output because
6633 -- this creates confusing noise.
6634
6635 elsif Is_Partial_Invariant_Proc (Subp_Id) then
6636 null;
6637
6638 -- _Postconditions
6639
6640 elsif Is_Postconditions_Proc (Subp_Id) then
6641 Info_Verification_Call
6642 (Pred => "postconditions",
6643 Id => Find_Enclosing_Scope (Call),
6644 Id_Kind => "subprogram");
6645
6646 -- Subprograms must come last because some of the previous cases
6647 -- fall under this category.
6648
6649 elsif Ekind (Subp_Id) = E_Function then
6650 Info_Simple_Call;
6651
6652 elsif Ekind (Subp_Id) = E_Procedure then
6653 Info_Simple_Call;
6654
6655 else
6656 pragma Assert (False);
6657 return;
6658 end if;
6659 end if;
6660 end Info_Call;
6661
6662 ------------------------
6663 -- Info_Instantiation --
6664 ------------------------
6665
6666 procedure Info_Instantiation
6667 (Inst : Node_Id;
6668 Gen_Id : Entity_Id;
6669 Info_Msg : Boolean;
6670 In_SPARK : Boolean)
6671 is
6672 begin
6673 Elab_Msg_NE
6674 (Msg => "instantiation of & during elaboration",
6675 N => Inst,
6676 Id => Gen_Id,
6677 Info_Msg => Info_Msg,
6678 In_SPARK => In_SPARK);
6679 end Info_Instantiation;
6680
6681 -----------------------------
6682 -- Info_Variable_Reference --
6683 -----------------------------
6684
6685 procedure Info_Variable_Reference
6686 (Ref : Node_Id;
6687 Var_Id : Entity_Id;
6688 Info_Msg : Boolean;
6689 In_SPARK : Boolean)
6690 is
6691 begin
6692 if Is_Read (Ref) then
6693 Elab_Msg_NE
6694 (Msg => "read of variable & during elaboration",
6695 N => Ref,
6696 Id => Var_Id,
6697 Info_Msg => Info_Msg,
6698 In_SPARK => In_SPARK);
6699 end if;
6700 end Info_Variable_Reference;
6701 end Diagnostics;
6702
6703 ---------------------------------
6704 -- Early_Call_Region_Processor --
6705 ---------------------------------
6706
6707 package body Early_Call_Region_Processor is
6708
6709 ---------------------
6710 -- Data structures --
6711 ---------------------
6712
6713 -- The following map relates early call regions to subprogram bodies
6714
6715 procedure Destroy (N : in out Node_Id);
6716 -- Destroy node N
6717
6718 package ECR_Map is new Dynamic_Hash_Tables
6719 (Key_Type => Entity_Id,
6720 Value_Type => Node_Id,
6721 No_Value => Empty,
6722 Expansion_Threshold => 1.5,
6723 Expansion_Factor => 2,
6724 Compression_Threshold => 0.3,
6725 Compression_Factor => 2,
6726 "=" => "=",
6727 Destroy_Value => Destroy,
6728 Hash => Hash);
6729
6730 Early_Call_Regions_Map : ECR_Map.Dynamic_Hash_Table := ECR_Map.Nil;
6731
6732 -----------------------
6733 -- Local subprograms --
6734 -----------------------
6735
6736 function Early_Call_Region (Body_Id : Entity_Id) return Node_Id;
6737 pragma Inline (Early_Call_Region);
6738 -- Obtain the early call region associated with entry or subprogram body
6739 -- Body_Id.
6740
6741 procedure Set_Early_Call_Region (Body_Id : Entity_Id; Start : Node_Id);
6742 pragma Inline (Set_Early_Call_Region);
6743 -- Associate an early call region with begins at construct Start with
6744 -- entry or subprogram body Body_Id.
6745
6746 -------------
6747 -- Destroy --
6748 -------------
6749
6750 procedure Destroy (N : in out Node_Id) is
6751 pragma Unreferenced (N);
6752 begin
6753 null;
6754 end Destroy;
6755
6756 -----------------------
6757 -- Early_Call_Region --
6758 -----------------------
6759
6760 function Early_Call_Region (Body_Id : Entity_Id) return Node_Id is
6761 pragma Assert (Present (Body_Id));
6762 begin
6763 return ECR_Map.Get (Early_Call_Regions_Map, Body_Id);
6764 end Early_Call_Region;
6765
6766 ------------------------------------------
6767 -- Finalize_Early_Call_Region_Processor --
6768 ------------------------------------------
6769
6770 procedure Finalize_Early_Call_Region_Processor is
6771 begin
6772 ECR_Map.Destroy (Early_Call_Regions_Map);
6773 end Finalize_Early_Call_Region_Processor;
6774
6775 ----------------------------
6776 -- Find_Early_Call_Region --
6777 ----------------------------
6778
6779 function Find_Early_Call_Region
6780 (Body_Decl : Node_Id;
6781 Assume_Elab_Body : Boolean := False;
6782 Skip_Memoization : Boolean := False) return Node_Id
6783 is
6784 -- NOTE: The routines within Find_Early_Call_Region are intentionally
6785 -- unnested to avoid deep indentation of code.
6786
6787 ECR_Found : exception;
6788 -- This exception is raised when the early call region has been found
6789
6790 Start : Node_Id := Empty;
6791 -- The start of the early call region. This variable is updated by
6792 -- the various nested routines. Due to the use of exceptions, the
6793 -- variable must be global to the nested routines.
6794
6795 -- The algorithm implemented in this routine attempts to find the
6796 -- early call region of a subprogram body by inspecting constructs
6797 -- in reverse declarative order, while navigating the tree. The
6798 -- algorithm consists of an Inspection phase and Advancement phase.
6799 -- The pseudocode is as follows:
6800 --
6801 -- loop
6802 -- inspection phase
6803 -- advancement phase
6804 -- end loop
6805 --
6806 -- The infinite loop is terminated by raising exception ECR_Found.
6807 -- The algorithm utilizes two pointers, Curr and Start, to represent
6808 -- the current construct to inspect and the start of the early call
6809 -- region.
6810 --
6811 -- IMPORTANT: The algorithm must maintain the following invariant at
6812 -- all time for it to function properly:
6813 --
6814 -- A nested construct is entered only when it contains suitable
6815 -- constructs.
6816 --
6817 -- This guarantees that leaving a nested or encapsulating construct
6818 -- functions properly.
6819 --
6820 -- The Inspection phase determines whether the current construct is
6821 -- non-preelaborable, and if it is, the algorithm terminates.
6822 --
6823 -- The Advancement phase walks the tree in reverse declarative order,
6824 -- while entering and leaving nested and encapsulating constructs. It
6825 -- may also terminate the elaborithm. There are several special cases
6826 -- of advancement.
6827 --
6828 -- 1) General case:
6829 --
6830 -- <construct 1>
6831 -- ...
6832 -- <construct N-1> <- Curr
6833 -- <construct N> <- Start
6834 -- <subprogram body>
6835 --
6836 -- In the general case, a declarative or statement list is traversed
6837 -- in reverse order where Curr is the lead pointer, and Start is the
6838 -- last preelaborable construct.
6839 --
6840 -- 2) Entering handled bodies
6841 --
6842 -- package body Nested is <- Curr (2.3)
6843 -- <declarations> <- Curr (2.2)
6844 -- begin
6845 -- <statements> <- Curr (2.1)
6846 -- end Nested;
6847 -- <construct> <- Start
6848 --
6849 -- In this case, the algorithm enters a handled body by starting from
6850 -- the last statement (2.1), or the last declaration (2.2), or the
6851 -- body is consumed (2.3) because it is empty and thus preelaborable.
6852 --
6853 -- 3) Entering package declarations
6854 --
6855 -- package Nested is <- Curr (2.3)
6856 -- <visible declarations> <- Curr (2.2)
6857 -- private
6858 -- <private declarations> <- Curr (2.1)
6859 -- end Nested;
6860 -- <construct> <- Start
6861 --
6862 -- In this case, the algorithm enters a package declaration by
6863 -- starting from the last private declaration (2.1), the last visible
6864 -- declaration (2.2), or the package is consumed (2.3) because it is
6865 -- empty and thus preelaborable.
6866 --
6867 -- 4) Transitioning from list to list of the same construct
6868 --
6869 -- Certain constructs have two eligible lists. The algorithm must
6870 -- thus transition from the second to the first list when the second
6871 -- list is exhausted.
6872 --
6873 -- declare <- Curr (4.2)
6874 -- <declarations> <- Curr (4.1)
6875 -- begin
6876 -- <statements> <- Start
6877 -- end;
6878 --
6879 -- In this case, the algorithm has exhausted the second list (the
6880 -- statements in the example above), and continues with the last
6881 -- declaration (4.1) or the construct is consumed (4.2) because it
6882 -- contains only preelaborable code.
6883 --
6884 -- 5) Transitioning from list to construct
6885 --
6886 -- tack body Task is <- Curr (5.1)
6887 -- <- Curr (Empty)
6888 -- <construct 1> <- Start
6889 --
6890 -- In this case, the algorithm has exhausted a list, Curr is Empty,
6891 -- and the owner of the list is consumed (5.1).
6892 --
6893 -- 6) Transitioning from unit to unit
6894 --
6895 -- A package body with a spec subject to pragma Elaborate_Body
6896 -- extends the possible range of the early call region to the package
6897 -- spec.
6898 --
6899 -- package Pack is <- Curr (6.3)
6900 -- pragma Elaborate_Body; <- Curr (6.2)
6901 -- <visible declarations> <- Curr (6.2)
6902 -- private
6903 -- <private declarations> <- Curr (6.1)
6904 -- end Pack;
6905 --
6906 -- package body Pack is <- Curr, Start
6907 --
6908 -- In this case, the algorithm has reached a package body compilation
6909 -- unit whose spec is subject to pragma Elaborate_Body, or the caller
6910 -- of the algorithm has specified this behavior. This transition is
6911 -- equivalent to 3).
6912 --
6913 -- 7) Transitioning from unit to termination
6914 --
6915 -- Reaching a compilation unit always terminates the algorithm as
6916 -- there are no more lists to examine. This must take case 6) into
6917 -- account.
6918 --
6919 -- 8) Transitioning from subunit to stub
6920 --
6921 -- package body Pack is separate; <- Curr (8.1)
6922 --
6923 -- separate (...)
6924 -- package body Pack is <- Curr, Start
6925 --
6926 -- Reaching a subunit continues the search from the corresponding
6927 -- stub (8.1).
6928
6929 procedure Advance (Curr : in out Node_Id);
6930 pragma Inline (Advance);
6931 -- Update the Curr and Start pointers depending on their location
6932 -- in the tree to the next eligible construct. This routine raises
6933 -- ECR_Found.
6934
6935 procedure Enter_Handled_Body (Curr : in out Node_Id);
6936 pragma Inline (Enter_Handled_Body);
6937 -- Update the Curr and Start pointers to enter a nested handled body
6938 -- if applicable. This routine raises ECR_Found.
6939
6940 procedure Enter_Package_Declaration (Curr : in out Node_Id);
6941 pragma Inline (Enter_Package_Declaration);
6942 -- Update the Curr and Start pointers to enter a nested package spec
6943 -- if applicable. This routine raises ECR_Found.
6944
6945 function Find_ECR (N : Node_Id) return Node_Id;
6946 pragma Inline (Find_ECR);
6947 -- Find an early call region starting from arbitrary node N
6948
6949 function Has_Suitable_Construct (List : List_Id) return Boolean;
6950 pragma Inline (Has_Suitable_Construct);
6951 -- Determine whether list List contains a suitable construct for
6952 -- inclusion into an early call region.
6953
6954 procedure Include (N : Node_Id; Curr : out Node_Id);
6955 pragma Inline (Include);
6956 -- Update the Curr and Start pointers to include arbitrary construct
6957 -- N in the early call region. This routine raises ECR_Found.
6958
6959 function Is_OK_Preelaborable_Construct (N : Node_Id) return Boolean;
6960 pragma Inline (Is_OK_Preelaborable_Construct);
6961 -- Determine whether arbitrary node N denotes a preelaboration-safe
6962 -- construct.
6963
6964 function Is_Suitable_Construct (N : Node_Id) return Boolean;
6965 pragma Inline (Is_Suitable_Construct);
6966 -- Determine whether arbitrary node N denotes a suitable construct
6967 -- for inclusion into the early call region.
6968
6969 procedure Transition_Body_Declarations
6970 (Bod : Node_Id;
6971 Curr : out Node_Id);
6972 pragma Inline (Transition_Body_Declarations);
6973 -- Update the Curr and Start pointers when construct Bod denotes a
6974 -- block statement or a suitable body. This routine raises ECR_Found.
6975
6976 procedure Transition_Handled_Statements
6977 (HSS : Node_Id;
6978 Curr : out Node_Id);
6979 pragma Inline (Transition_Handled_Statements);
6980 -- Update the Curr and Start pointers when node HSS denotes a handled
6981 -- sequence of statements. This routine raises ECR_Found.
6982
6983 procedure Transition_Spec_Declarations
6984 (Spec : Node_Id;
6985 Curr : out Node_Id);
6986 pragma Inline (Transition_Spec_Declarations);
6987 -- Update the Curr and Start pointers when construct Spec denotes
6988 -- a concurrent definition or a package spec. This routine raises
6989 -- ECR_Found.
6990
6991 procedure Transition_Unit (Unit : Node_Id; Curr : out Node_Id);
6992 pragma Inline (Transition_Unit);
6993 -- Update the Curr and Start pointers when node Unit denotes a
6994 -- potential compilation unit. This routine raises ECR_Found.
6995
6996 -------------
6997 -- Advance --
6998 -------------
6999
7000 procedure Advance (Curr : in out Node_Id) is
7001 Context : Node_Id;
7002
7003 begin
7004 -- Curr denotes one of the following cases upon entry into this
7005 -- routine:
7006 --
7007 -- * Empty - There is no current construct when a declarative or
7008 -- a statement list has been exhausted. This does not indicate
7009 -- that the early call region has been computed as it is still
7010 -- possible to transition to another list.
7011 --
7012 -- * Encapsulator - The current construct wraps declarations
7013 -- and/or statements. This indicates that the early call
7014 -- region may extend within the nested construct.
7015 --
7016 -- * Preelaborable - The current construct is preelaborable
7017 -- because Find_ECR would not invoke Advance if this was not
7018 -- the case.
7019
7020 -- The current construct is an encapsulator or is preelaborable
7021
7022 if Present (Curr) then
7023
7024 -- Enter encapsulators by inspecting their declarations and/or
7025 -- statements.
7026
7027 if Nkind_In (Curr, N_Block_Statement, N_Package_Body) then
7028 Enter_Handled_Body (Curr);
7029
7030 elsif Nkind (Curr) = N_Package_Declaration then
7031 Enter_Package_Declaration (Curr);
7032
7033 -- Early call regions have a property which can be exploited to
7034 -- optimize the algorithm.
7035 --
7036 -- <preceding subprogram body>
7037 -- <preelaborable construct 1>
7038 -- ...
7039 -- <preelaborable construct N>
7040 -- <initiating subprogram body>
7041 --
7042 -- If a traversal initiated from a subprogram body reaches a
7043 -- preceding subprogram body, then both bodies share the same
7044 -- early call region.
7045 --
7046 -- The property results in the following desirable effects:
7047 --
7048 -- * If the preceding body already has an early call region,
7049 -- then the initiating body can reuse it. This minimizes the
7050 -- amount of processing performed by the algorithm.
7051 --
7052 -- * If the preceding body lack an early call region, then the
7053 -- algorithm can compute the early call region, and reuse it
7054 -- for the initiating body. This processing performs the same
7055 -- amount of work, but has the beneficial effect of computing
7056 -- the early call regions of all preceding bodies.
7057
7058 elsif Nkind_In (Curr, N_Entry_Body, N_Subprogram_Body) then
7059 Start :=
7060 Find_Early_Call_Region
7061 (Body_Decl => Curr,
7062 Assume_Elab_Body => Assume_Elab_Body,
7063 Skip_Memoization => Skip_Memoization);
7064
7065 raise ECR_Found;
7066
7067 -- Otherwise current construct is preelaborable. Unpdate the
7068 -- early call region to include it.
7069
7070 else
7071 Include (Curr, Curr);
7072 end if;
7073
7074 -- Otherwise the current construct is missing, indicating that the
7075 -- current list has been exhausted. Depending on the context of
7076 -- the list, several transitions are possible.
7077
7078 else
7079 -- The invariant of the algorithm ensures that Curr and Start
7080 -- are at the same level of nesting at the point of transition.
7081 -- The algorithm can determine which list the traversal came
7082 -- from by examining Start.
7083
7084 Context := Parent (Start);
7085
7086 -- Attempt the following transitions:
7087 --
7088 -- private declarations -> visible declarations
7089 -- private declarations -> upper level
7090 -- private declarations -> terminate
7091 -- visible declarations -> upper level
7092 -- visible declarations -> terminate
7093
7094 if Nkind_In (Context, N_Package_Specification,
7095 N_Protected_Definition,
7096 N_Task_Definition)
7097 then
7098 Transition_Spec_Declarations (Context, Curr);
7099
7100 -- Attempt the following transitions:
7101 --
7102 -- statements -> declarations
7103 -- statements -> upper level
7104 -- statements -> corresponding package spec (Elab_Body)
7105 -- statements -> terminate
7106
7107 elsif Nkind (Context) = N_Handled_Sequence_Of_Statements then
7108 Transition_Handled_Statements (Context, Curr);
7109
7110 -- Attempt the following transitions:
7111 --
7112 -- declarations -> upper level
7113 -- declarations -> corresponding package spec (Elab_Body)
7114 -- declarations -> terminate
7115
7116 elsif Nkind_In (Context, N_Block_Statement,
7117 N_Entry_Body,
7118 N_Package_Body,
7119 N_Protected_Body,
7120 N_Subprogram_Body,
7121 N_Task_Body)
7122 then
7123 Transition_Body_Declarations (Context, Curr);
7124
7125 -- Otherwise it is not possible to transition. Stop the search
7126 -- because there are no more declarations or statements to
7127 -- check.
7128
7129 else
7130 raise ECR_Found;
7131 end if;
7132 end if;
7133 end Advance;
7134
7135 --------------------------
7136 -- Enter_Handled_Body --
7137 --------------------------
7138
7139 procedure Enter_Handled_Body (Curr : in out Node_Id) is
7140 Decls : constant List_Id := Declarations (Curr);
7141 HSS : constant Node_Id := Handled_Statement_Sequence (Curr);
7142 Stmts : List_Id := No_List;
7143
7144 begin
7145 if Present (HSS) then
7146 Stmts := Statements (HSS);
7147 end if;
7148
7149 -- The handled body has a non-empty statement sequence. The
7150 -- construct to inspect is the last statement.
7151
7152 if Has_Suitable_Construct (Stmts) then
7153 Curr := Last (Stmts);
7154
7155 -- The handled body lacks statements, but has non-empty
7156 -- declarations. The construct to inspect is the last declaration.
7157
7158 elsif Has_Suitable_Construct (Decls) then
7159 Curr := Last (Decls);
7160
7161 -- Otherwise the handled body lacks both declarations and
7162 -- statements. The construct to inspect is the node which precedes
7163 -- the handled body. Update the early call region to include the
7164 -- handled body.
7165
7166 else
7167 Include (Curr, Curr);
7168 end if;
7169 end Enter_Handled_Body;
7170
7171 -------------------------------
7172 -- Enter_Package_Declaration --
7173 -------------------------------
7174
7175 procedure Enter_Package_Declaration (Curr : in out Node_Id) is
7176 Pack_Spec : constant Node_Id := Specification (Curr);
7177 Prv_Decls : constant List_Id := Private_Declarations (Pack_Spec);
7178 Vis_Decls : constant List_Id := Visible_Declarations (Pack_Spec);
7179
7180 begin
7181 -- The package has a non-empty private declarations. The construct
7182 -- to inspect is the last private declaration.
7183
7184 if Has_Suitable_Construct (Prv_Decls) then
7185 Curr := Last (Prv_Decls);
7186
7187 -- The package lacks private declarations, but has non-empty
7188 -- visible declarations. In this case the construct to inspect
7189 -- is the last visible declaration.
7190
7191 elsif Has_Suitable_Construct (Vis_Decls) then
7192 Curr := Last (Vis_Decls);
7193
7194 -- Otherwise the package lacks any declarations. The construct
7195 -- to inspect is the node which precedes the package. Update the
7196 -- early call region to include the package declaration.
7197
7198 else
7199 Include (Curr, Curr);
7200 end if;
7201 end Enter_Package_Declaration;
7202
7203 --------------
7204 -- Find_ECR --
7205 --------------
7206
7207 function Find_ECR (N : Node_Id) return Node_Id is
7208 Curr : Node_Id;
7209
7210 begin
7211 -- The early call region starts at N
7212
7213 Curr := Prev (N);
7214 Start := N;
7215
7216 -- Inspect each node in reverse declarative order while going in
7217 -- and out of nested and enclosing constructs. Note that the only
7218 -- way to terminate this infinite loop is to raise ECR_Found.
7219
7220 loop
7221 -- The current construct is not preelaboration-safe. Terminate
7222 -- the traversal.
7223
7224 if Present (Curr)
7225 and then not Is_OK_Preelaborable_Construct (Curr)
7226 then
7227 raise ECR_Found;
7228 end if;
7229
7230 -- Advance to the next suitable construct. This may terminate
7231 -- the traversal by raising ECR_Found.
7232
7233 Advance (Curr);
7234 end loop;
7235
7236 exception
7237 when ECR_Found =>
7238 return Start;
7239 end Find_ECR;
7240
7241 ----------------------------
7242 -- Has_Suitable_Construct --
7243 ----------------------------
7244
7245 function Has_Suitable_Construct (List : List_Id) return Boolean is
7246 Item : Node_Id;
7247
7248 begin
7249 -- Examine the list in reverse declarative order, looking for a
7250 -- suitable construct.
7251
7252 if Present (List) then
7253 Item := Last (List);
7254 while Present (Item) loop
7255 if Is_Suitable_Construct (Item) then
7256 return True;
7257 end if;
7258
7259 Prev (Item);
7260 end loop;
7261 end if;
7262
7263 return False;
7264 end Has_Suitable_Construct;
7265
7266 -------------
7267 -- Include --
7268 -------------
7269
7270 procedure Include (N : Node_Id; Curr : out Node_Id) is
7271 begin
7272 Start := N;
7273
7274 -- The input node is a compilation unit. This terminates the
7275 -- search because there are no more lists to inspect and there are
7276 -- no more enclosing constructs to climb up to. The transitions
7277 -- are:
7278 --
7279 -- private declarations -> terminate
7280 -- visible declarations -> terminate
7281 -- statements -> terminate
7282 -- declarations -> terminate
7283
7284 if Nkind (Parent (Start)) = N_Compilation_Unit then
7285 raise ECR_Found;
7286
7287 -- Otherwise the input node is still within some list
7288
7289 else
7290 Curr := Prev (Start);
7291 end if;
7292 end Include;
7293
7294 -----------------------------------
7295 -- Is_OK_Preelaborable_Construct --
7296 -----------------------------------
7297
7298 function Is_OK_Preelaborable_Construct (N : Node_Id) return Boolean is
7299 begin
7300 -- Assignment statements are acceptable as long as they were
7301 -- produced by the ABE mechanism to update elaboration flags.
7302
7303 if Nkind (N) = N_Assignment_Statement then
7304 return Is_Elaboration_Code (N);
7305
7306 -- Block statements are acceptable even though they directly
7307 -- violate preelaborability. The intention is not to penalize
7308 -- the early call region when a block contains only preelaborable
7309 -- constructs.
7310 --
7311 -- declare
7312 -- Val : constant Integer := 1;
7313 -- begin
7314 -- pragma Assert (Val = 1);
7315 -- null;
7316 -- end;
7317 --
7318 -- Note that the Advancement phase does enter blocks, and will
7319 -- detect any non-preelaborable declarations or statements within.
7320
7321 elsif Nkind (N) = N_Block_Statement then
7322 return True;
7323 end if;
7324
7325 -- Otherwise the construct must be preelaborable. The check must
7326 -- take the syntactic and semantic structure of the construct. DO
7327 -- NOT use Is_Preelaborable_Construct here.
7328
7329 return not Is_Non_Preelaborable_Construct (N);
7330 end Is_OK_Preelaborable_Construct;
7331
7332 ---------------------------
7333 -- Is_Suitable_Construct --
7334 ---------------------------
7335
7336 function Is_Suitable_Construct (N : Node_Id) return Boolean is
7337 Context : constant Node_Id := Parent (N);
7338
7339 begin
7340 -- An internally-generated statement sequence which contains only
7341 -- a single null statement is not a suitable construct because it
7342 -- is a byproduct of the parser. Such a null statement should be
7343 -- excluded from the early call region because it carries the
7344 -- source location of the "end" keyword, and may lead to confusing
7345 -- diagnistics.
7346
7347 if Nkind (N) = N_Null_Statement
7348 and then not Comes_From_Source (N)
7349 and then Present (Context)
7350 and then Nkind (Context) = N_Handled_Sequence_Of_Statements
7351 then
7352 return False;
7353 end if;
7354
7355 -- Otherwise only constructs which correspond to pure Ada
7356 -- constructs are considered suitable.
7357
7358 case Nkind (N) is
7359 when N_Call_Marker
7360 | N_Freeze_Entity
7361 | N_Freeze_Generic_Entity
7362 | N_Implicit_Label_Declaration
7363 | N_Itype_Reference
7364 | N_Pop_Constraint_Error_Label
7365 | N_Pop_Program_Error_Label
7366 | N_Pop_Storage_Error_Label
7367 | N_Push_Constraint_Error_Label
7368 | N_Push_Program_Error_Label
7369 | N_Push_Storage_Error_Label
7370 | N_SCIL_Dispatch_Table_Tag_Init
7371 | N_SCIL_Dispatching_Call
7372 | N_SCIL_Membership_Test
7373 | N_Variable_Reference_Marker
7374 =>
7375 return False;
7376
7377 when others =>
7378 return True;
7379 end case;
7380 end Is_Suitable_Construct;
7381
7382 ----------------------------------
7383 -- Transition_Body_Declarations --
7384 ----------------------------------
7385
7386 procedure Transition_Body_Declarations
7387 (Bod : Node_Id;
7388 Curr : out Node_Id)
7389 is
7390 Decls : constant List_Id := Declarations (Bod);
7391
7392 begin
7393 -- The search must come from the declarations of the body
7394
7395 pragma Assert
7396 (Is_Non_Empty_List (Decls)
7397 and then List_Containing (Start) = Decls);
7398
7399 -- The search finished inspecting the declarations. The construct
7400 -- to inspect is the node which precedes the handled body, unless
7401 -- the body is a compilation unit. The transitions are:
7402 --
7403 -- declarations -> upper level
7404 -- declarations -> corresponding package spec (Elab_Body)
7405 -- declarations -> terminate
7406
7407 Transition_Unit (Bod, Curr);
7408 end Transition_Body_Declarations;
7409
7410 -----------------------------------
7411 -- Transition_Handled_Statements --
7412 -----------------------------------
7413
7414 procedure Transition_Handled_Statements
7415 (HSS : Node_Id;
7416 Curr : out Node_Id)
7417 is
7418 Bod : constant Node_Id := Parent (HSS);
7419 Decls : constant List_Id := Declarations (Bod);
7420 Stmts : constant List_Id := Statements (HSS);
7421
7422 begin
7423 -- The search must come from the statements of certain bodies or
7424 -- statements.
7425
7426 pragma Assert (Nkind_In (Bod, N_Block_Statement,
7427 N_Entry_Body,
7428 N_Package_Body,
7429 N_Protected_Body,
7430 N_Subprogram_Body,
7431 N_Task_Body));
7432
7433 -- The search must come from the statements of the handled
7434 -- sequence.
7435
7436 pragma Assert
7437 (Is_Non_Empty_List (Stmts)
7438 and then List_Containing (Start) = Stmts);
7439
7440 -- The search finished inspecting the statements. The handled body
7441 -- has non-empty declarations. The construct to inspect is the
7442 -- last declaration. The transitions are:
7443 --
7444 -- statements -> declarations
7445
7446 if Has_Suitable_Construct (Decls) then
7447 Curr := Last (Decls);
7448
7449 -- Otherwise the handled body lacks declarations. The construct to
7450 -- inspect is the node which precedes the handled body, unless the
7451 -- body is a compilation unit. The transitions are:
7452 --
7453 -- statements -> upper level
7454 -- statements -> corresponding package spec (Elab_Body)
7455 -- statements -> terminate
7456
7457 else
7458 Transition_Unit (Bod, Curr);
7459 end if;
7460 end Transition_Handled_Statements;
7461
7462 ----------------------------------
7463 -- Transition_Spec_Declarations --
7464 ----------------------------------
7465
7466 procedure Transition_Spec_Declarations
7467 (Spec : Node_Id;
7468 Curr : out Node_Id)
7469 is
7470 Prv_Decls : constant List_Id := Private_Declarations (Spec);
7471 Vis_Decls : constant List_Id := Visible_Declarations (Spec);
7472
7473 begin
7474 pragma Assert (Present (Start) and then Is_List_Member (Start));
7475
7476 -- The search came from the private declarations and finished
7477 -- their inspection.
7478
7479 if Has_Suitable_Construct (Prv_Decls)
7480 and then List_Containing (Start) = Prv_Decls
7481 then
7482 -- The context has non-empty visible declarations. The node to
7483 -- inspect is the last visible declaration. The transitions
7484 -- are:
7485 --
7486 -- private declarations -> visible declarations
7487
7488 if Has_Suitable_Construct (Vis_Decls) then
7489 Curr := Last (Vis_Decls);
7490
7491 -- Otherwise the context lacks visible declarations. The
7492 -- construct to inspect is the node which precedes the context
7493 -- unless the context is a compilation unit. The transitions
7494 -- are:
7495 --
7496 -- private declarations -> upper level
7497 -- private declarations -> terminate
7498
7499 else
7500 Transition_Unit (Parent (Spec), Curr);
7501 end if;
7502
7503 -- The search came from the visible declarations and finished
7504 -- their inspections. The construct to inspect is the node which
7505 -- precedes the context, unless the context is a compilaton unit.
7506 -- The transitions are:
7507 --
7508 -- visible declarations -> upper level
7509 -- visible declarations -> terminate
7510
7511 elsif Has_Suitable_Construct (Vis_Decls)
7512 and then List_Containing (Start) = Vis_Decls
7513 then
7514 Transition_Unit (Parent (Spec), Curr);
7515
7516 -- At this point both declarative lists are empty, but the
7517 -- traversal still came from within the spec. This indicates
7518 -- that the invariant of the algorithm has been violated.
7519
7520 else
7521 pragma Assert (False);
7522 raise ECR_Found;
7523 end if;
7524 end Transition_Spec_Declarations;
7525
7526 ---------------------
7527 -- Transition_Unit --
7528 ---------------------
7529
7530 procedure Transition_Unit
7531 (Unit : Node_Id;
7532 Curr : out Node_Id)
7533 is
7534 Context : constant Node_Id := Parent (Unit);
7535
7536 begin
7537 -- The unit is a compilation unit. This terminates the search
7538 -- because there are no more lists to inspect and there are no
7539 -- more enclosing constructs to climb up to.
7540
7541 if Nkind (Context) = N_Compilation_Unit then
7542
7543 -- A package body with a corresponding spec subject to pragma
7544 -- Elaborate_Body is an exception to the above. The annotation
7545 -- allows the search to continue into the package declaration.
7546 -- The transitions are:
7547 --
7548 -- statements -> corresponding package spec (Elab_Body)
7549 -- declarations -> corresponding package spec (Elab_Body)
7550
7551 if Nkind (Unit) = N_Package_Body
7552 and then (Assume_Elab_Body
7553 or else Has_Pragma_Elaborate_Body
7554 (Corresponding_Spec (Unit)))
7555 then
7556 Curr := Unit_Declaration_Node (Corresponding_Spec (Unit));
7557 Enter_Package_Declaration (Curr);
7558
7559 -- Otherwise terminate the search. The transitions are:
7560 --
7561 -- private declarations -> terminate
7562 -- visible declarations -> terminate
7563 -- statements -> terminate
7564 -- declarations -> terminate
7565
7566 else
7567 raise ECR_Found;
7568 end if;
7569
7570 -- The unit is a subunit. The construct to inspect is the node
7571 -- which precedes the corresponding stub. Update the early call
7572 -- region to include the unit.
7573
7574 elsif Nkind (Context) = N_Subunit then
7575 Start := Unit;
7576 Curr := Corresponding_Stub (Context);
7577
7578 -- Otherwise the unit is nested. The construct to inspect is the
7579 -- node which precedes the unit. Update the early call region to
7580 -- include the unit.
7581
7582 else
7583 Include (Unit, Curr);
7584 end if;
7585 end Transition_Unit;
7586
7587 -- Local variables
7588
7589 Body_Id : constant Entity_Id := Unique_Defining_Entity (Body_Decl);
7590 Region : Node_Id;
7591
7592 -- Start of processing for Find_Early_Call_Region
7593
7594 begin
7595 -- The caller demands the start of the early call region without
7596 -- saving or retrieving it to/from internal data structures.
7597
7598 if Skip_Memoization then
7599 Region := Find_ECR (Body_Decl);
7600
7601 -- Default behavior
7602
7603 else
7604 -- Check whether the early call region of the subprogram body is
7605 -- available.
7606
7607 Region := Early_Call_Region (Body_Id);
7608
7609 if No (Region) then
7610 Region := Find_ECR (Body_Decl);
7611
7612 -- Associate the early call region with the subprogram body in
7613 -- case other scenarios need it.
7614
7615 Set_Early_Call_Region (Body_Id, Region);
7616 end if;
7617 end if;
7618
7619 -- A subprogram body must always have an early call region
7620
7621 pragma Assert (Present (Region));
7622
7623 return Region;
7624 end Find_Early_Call_Region;
7625
7626 --------------------------------------------
7627 -- Initialize_Early_Call_Region_Processor --
7628 --------------------------------------------
7629
7630 procedure Initialize_Early_Call_Region_Processor is
7631 begin
7632 Early_Call_Regions_Map := ECR_Map.Create (100);
7633 end Initialize_Early_Call_Region_Processor;
7634
7635 ---------------------------
7636 -- Set_Early_Call_Region --
7637 ---------------------------
7638
7639 procedure Set_Early_Call_Region (Body_Id : Entity_Id; Start : Node_Id) is
7640 pragma Assert (Present (Body_Id));
7641 pragma Assert (Present (Start));
7642
7643 begin
7644 ECR_Map.Put (Early_Call_Regions_Map, Body_Id, Start);
7645 end Set_Early_Call_Region;
7646 end Early_Call_Region_Processor;
7647
7648 ----------------------
7649 -- Elaborated_Units --
7650 ----------------------
7651
7652 package body Elaborated_Units is
7653
7654 -----------
7655 -- Types --
7656 -----------
7657
7658 -- The following type idenfities the elaboration attributes of a unit
7659
7660 type Elaboration_Attributes_Id is new Natural;
7661
7662 No_Elaboration_Attributes : constant Elaboration_Attributes_Id :=
7663 Elaboration_Attributes_Id'First;
7664 First_Elaboration_Attributes : constant Elaboration_Attributes_Id :=
7665 No_Elaboration_Attributes + 1;
7666
7667 -- The following type represents the elaboration attributes of a unit
7668
7669 type Elaboration_Attributes_Record is record
7670 Elab_Pragma : Node_Id := Empty;
7671 -- This attribute denotes a source Elaborate or Elaborate_All pragma
7672 -- which guarantees the prior elaboration of some unit with respect
7673 -- to the main unit. The pragma may come from the following contexts:
7674 --
7675 -- * The main unit
7676 -- * The spec of the main unit (if applicable)
7677 -- * Any parent spec of the main unit (if applicable)
7678 -- * Any parent subunit of the main unit (if applicable)
7679 --
7680 -- The attribute remains Empty if no such pragma is available. Source
7681 -- pragmas play a role in satisfying SPARK elaboration requirements.
7682
7683 With_Clause : Node_Id := Empty;
7684 -- This attribute denotes an internally-generated or a source with
7685 -- clause for some unit withed by the main unit. With clauses carry
7686 -- flags which represent implicit Elaborate or Elaborate_All pragmas.
7687 -- These clauses play a role in supplying elaboration dependencies to
7688 -- binde.
7689 end record;
7690
7691 ---------------------
7692 -- Data structures --
7693 ---------------------
7694
7695 -- The following table stores all elaboration attributes
7696
7697 package Elaboration_Attributes is new Table.Table
7698 (Table_Index_Type => Elaboration_Attributes_Id,
7699 Table_Component_Type => Elaboration_Attributes_Record,
7700 Table_Low_Bound => First_Elaboration_Attributes,
7701 Table_Initial => 250,
7702 Table_Increment => 200,
7703 Table_Name => "Elaboration_Attributes");
7704
7705 procedure Destroy (EA_Id : in out Elaboration_Attributes_Id);
7706 -- Destroy elaboration attributes EA_Id
7707
7708 package UA_Map is new Dynamic_Hash_Tables
7709 (Key_Type => Entity_Id,
7710 Value_Type => Elaboration_Attributes_Id,
7711 No_Value => No_Elaboration_Attributes,
7712 Expansion_Threshold => 1.5,
7713 Expansion_Factor => 2,
7714 Compression_Threshold => 0.3,
7715 Compression_Factor => 2,
7716 "=" => "=",
7717 Destroy_Value => Destroy,
7718 Hash => Hash);
7719
7720 -- The following map relates an elaboration attributes of a unit to the
7721 -- unit.
7722
7723 Unit_To_Attributes_Map : UA_Map.Dynamic_Hash_Table := UA_Map.Nil;
7724
7725 ------------------
7726 -- Constructors --
7727 ------------------
7728
7729 function Elaboration_Attributes_Of
7730 (Unit_Id : Entity_Id) return Elaboration_Attributes_Id;
7731 pragma Inline (Elaboration_Attributes_Of);
7732 -- Obtain the elaboration attributes of unit Unit_Id
7733
7734 -----------------------
7735 -- Local subprograms --
7736 -----------------------
7737
7738 function Elab_Pragma (EA_Id : Elaboration_Attributes_Id) return Node_Id;
7739 pragma Inline (Elab_Pragma);
7740 -- Obtain the Elaborate[_All] pragma of elaboration attributes EA_Id
7741
7742 procedure Ensure_Prior_Elaboration_Dynamic
7743 (N : Node_Id;
7744 Unit_Id : Entity_Id;
7745 Prag_Nam : Name_Id;
7746 In_State : Processing_In_State);
7747 pragma Inline (Ensure_Prior_Elaboration_Dynamic);
7748 -- Guarantee the elaboration of unit Unit_Id with respect to the main
7749 -- unit by suggesting the use of Elaborate[_All] with name Prag_Nam. N
7750 -- denotes the related scenario. In_State is the current state of the
7751 -- Processing phase.
7752
7753 procedure Ensure_Prior_Elaboration_Static
7754 (N : Node_Id;
7755 Unit_Id : Entity_Id;
7756 Prag_Nam : Name_Id;
7757 In_State : Processing_In_State);
7758 pragma Inline (Ensure_Prior_Elaboration_Static);
7759 -- Guarantee the elaboration of unit Unit_Id with respect to the main
7760 -- unit by installing an implicit Elaborate[_All] pragma with name
7761 -- Prag_Nam. N denotes the related scenario. In_State is the current
7762 -- state of the Processing phase.
7763
7764 function Present (EA_Id : Elaboration_Attributes_Id) return Boolean;
7765 pragma Inline (Present);
7766 -- Determine whether elaboration attributes UA_Id exist
7767
7768 procedure Set_Elab_Pragma
7769 (EA_Id : Elaboration_Attributes_Id;
7770 Prag : Node_Id);
7771 pragma Inline (Set_Elab_Pragma);
7772 -- Set the Elaborate[_All] pragma of elaboration attributes EA_Id to
7773 -- Prag.
7774
7775 procedure Set_With_Clause
7776 (EA_Id : Elaboration_Attributes_Id;
7777 Clause : Node_Id);
7778 pragma Inline (Set_With_Clause);
7779 -- Set the with clause of elaboration attributes EA_Id to Clause
7780
7781 function With_Clause (EA_Id : Elaboration_Attributes_Id) return Node_Id;
7782 pragma Inline (With_Clause);
7783 -- Obtain the implicit or source with clause of elaboration attributes
7784 -- EA_Id.
7785
7786 ------------------------------
7787 -- Collect_Elaborated_Units --
7788 ------------------------------
7789
7790 procedure Collect_Elaborated_Units is
7791 procedure Add_Pragma (Prag : Node_Id);
7792 pragma Inline (Add_Pragma);
7793 -- Determine whether pragma Prag denotes a legal Elaborate[_All]
7794 -- pragma. If this is the case, add the related unit to the context.
7795 -- For pragma Elaborate_All, include recursively all units withed by
7796 -- the related unit.
7797
7798 procedure Add_Unit
7799 (Unit_Id : Entity_Id;
7800 Prag : Node_Id;
7801 Full_Context : Boolean);
7802 pragma Inline (Add_Unit);
7803 -- Add unit Unit_Id to the elaboration context. Prag denotes the
7804 -- pragma which prompted the inclusion of the unit to the context.
7805 -- If flag Full_Context is set, examine the nonlimited clauses of
7806 -- unit Unit_Id and add each withed unit to the context.
7807
7808 procedure Find_Elaboration_Context (Comp_Unit : Node_Id);
7809 pragma Inline (Find_Elaboration_Context);
7810 -- Examine the context items of compilation unit Comp_Unit for
7811 -- suitable elaboration-related pragmas and add all related units
7812 -- to the context.
7813
7814 ----------------
7815 -- Add_Pragma --
7816 ----------------
7817
7818 procedure Add_Pragma (Prag : Node_Id) is
7819 Prag_Args : constant List_Id :=
7820 Pragma_Argument_Associations (Prag);
7821 Prag_Nam : constant Name_Id := Pragma_Name (Prag);
7822 Unit_Arg : Node_Id;
7823
7824 begin
7825 -- Nothing to do if the pragma is not related to elaboration
7826
7827 if not Nam_In (Prag_Nam, Name_Elaborate, Name_Elaborate_All) then
7828 return;
7829
7830 -- Nothing to do when the pragma is illegal
7831
7832 elsif Error_Posted (Prag) then
7833 return;
7834 end if;
7835
7836 Unit_Arg := Get_Pragma_Arg (First (Prag_Args));
7837
7838 -- The argument of the pragma may appear in package.package form
7839
7840 if Nkind (Unit_Arg) = N_Selected_Component then
7841 Unit_Arg := Selector_Name (Unit_Arg);
7842 end if;
7843
7844 Add_Unit
7845 (Unit_Id => Entity (Unit_Arg),
7846 Prag => Prag,
7847 Full_Context => Prag_Nam = Name_Elaborate_All);
7848 end Add_Pragma;
7849
7850 --------------
7851 -- Add_Unit --
7852 --------------
7853
7854 procedure Add_Unit
7855 (Unit_Id : Entity_Id;
7856 Prag : Node_Id;
7857 Full_Context : Boolean)
7858 is
7859 Clause : Node_Id;
7860 EA_Id : Elaboration_Attributes_Id;
7861 Unit_Prag : Node_Id;
7862
7863 begin
7864 -- Nothing to do when some previous error left a with clause or a
7865 -- pragma in a bad state.
7866
7867 if No (Unit_Id) then
7868 return;
7869 end if;
7870
7871 EA_Id := Elaboration_Attributes_Of (Unit_Id);
7872 Unit_Prag := Elab_Pragma (EA_Id);
7873
7874 -- The unit is already included in the context by means of pragma
7875 -- Elaborate[_All].
7876
7877 if Present (Unit_Prag) then
7878
7879 -- Upgrade an existing pragma Elaborate when the unit is
7880 -- subject to Elaborate_All because the new pragma covers a
7881 -- larger set of units.
7882
7883 if Pragma_Name (Unit_Prag) = Name_Elaborate
7884 and then Pragma_Name (Prag) = Name_Elaborate_All
7885 then
7886 Set_Elab_Pragma (EA_Id, Prag);
7887
7888 -- Otherwise the unit retains its existing pragma and does not
7889 -- need to be included in the context again.
7890
7891 else
7892 return;
7893 end if;
7894
7895 -- Otherwise the current unit is not included in the context
7896
7897 else
7898 Set_Elab_Pragma (EA_Id, Prag);
7899 end if;
7900
7901 -- Includes all units withed by the current one when computing the
7902 -- full context.
7903
7904 if Full_Context then
7905
7906 -- Process all nonlimited with clauses found in the context of
7907 -- the current unit. Note that limited clauses do not impose an
7908 -- elaboration order.
7909
7910 Clause := First (Context_Items (Compilation_Unit (Unit_Id)));
7911 while Present (Clause) loop
7912 if Nkind (Clause) = N_With_Clause
7913 and then not Error_Posted (Clause)
7914 and then not Limited_Present (Clause)
7915 then
7916 Add_Unit
7917 (Unit_Id => Entity (Name (Clause)),
7918 Prag => Prag,
7919 Full_Context => Full_Context);
7920 end if;
7921
7922 Next (Clause);
7923 end loop;
7924 end if;
7925 end Add_Unit;
7926
7927 ------------------------------
7928 -- Find_Elaboration_Context --
7929 ------------------------------
7930
7931 procedure Find_Elaboration_Context (Comp_Unit : Node_Id) is
7932 pragma Assert (Nkind (Comp_Unit) = N_Compilation_Unit);
7933
7934 Prag : Node_Id;
7935
7936 begin
7937 -- Process all elaboration-related pragmas found in the context of
7938 -- the compilation unit.
7939
7940 Prag := First (Context_Items (Comp_Unit));
7941 while Present (Prag) loop
7942 if Nkind (Prag) = N_Pragma then
7943 Add_Pragma (Prag);
7944 end if;
7945
7946 Next (Prag);
7947 end loop;
7948 end Find_Elaboration_Context;
7949
7950 -- Local variables
7951
7952 Par_Id : Entity_Id;
7953 Unit_Id : Node_Id;
7954
7955 -- Start of processing for Collect_Elaborated_Units
7956
7957 begin
7958 -- Perform a traversal to examines the context of the main unit. The
7959 -- traversal performs the following jumps:
7960 --
7961 -- subunit -> parent subunit
7962 -- parent subunit -> body
7963 -- body -> spec
7964 -- spec -> parent spec
7965 -- parent spec -> grandparent spec and so on
7966 --
7967 -- The traversal relies on units rather than scopes because the scope
7968 -- of a subunit is some spec, while this traversal must process the
7969 -- body as well. Given that protected and task bodies can also be
7970 -- subunits, this complicates the scope approach even further.
7971
7972 Unit_Id := Unit (Cunit (Main_Unit));
7973
7974 -- Perform the following traversals when the main unit is a subunit
7975 --
7976 -- subunit -> parent subunit
7977 -- parent subunit -> body
7978
7979 while Present (Unit_Id) and then Nkind (Unit_Id) = N_Subunit loop
7980 Find_Elaboration_Context (Parent (Unit_Id));
7981
7982 -- Continue the traversal by going to the unit which contains the
7983 -- corresponding stub.
7984
7985 if Present (Corresponding_Stub (Unit_Id)) then
7986 Unit_Id :=
7987 Unit (Cunit (Get_Source_Unit (Corresponding_Stub (Unit_Id))));
7988
7989 -- Otherwise the subunit may be erroneous or left in a bad state
7990
7991 else
7992 exit;
7993 end if;
7994 end loop;
7995
7996 -- Perform the following traversal now that subunits have been taken
7997 -- care of, or the main unit is a body.
7998 --
7999 -- body -> spec
8000
8001 if Present (Unit_Id)
8002 and then Nkind_In (Unit_Id, N_Package_Body, N_Subprogram_Body)
8003 then
8004 Find_Elaboration_Context (Parent (Unit_Id));
8005
8006 -- Continue the traversal by going to the unit which contains the
8007 -- corresponding spec.
8008
8009 if Present (Corresponding_Spec (Unit_Id)) then
8010 Unit_Id :=
8011 Unit (Cunit (Get_Source_Unit (Corresponding_Spec (Unit_Id))));
8012 end if;
8013 end if;
8014
8015 -- Perform the following traversals now that the body has been taken
8016 -- care of, or the main unit is a spec.
8017 --
8018 -- spec -> parent spec
8019 -- parent spec -> grandparent spec and so on
8020
8021 if Present (Unit_Id)
8022 and then Nkind_In (Unit_Id, N_Generic_Package_Declaration,
8023 N_Generic_Subprogram_Declaration,
8024 N_Package_Declaration,
8025 N_Subprogram_Declaration)
8026 then
8027 Find_Elaboration_Context (Parent (Unit_Id));
8028
8029 -- Process a potential chain of parent units which ends with the
8030 -- main unit spec. The traversal can now safely rely on the scope
8031 -- chain.
8032
8033 Par_Id := Scope (Defining_Entity (Unit_Id));
8034 while Present (Par_Id) and then Par_Id /= Standard_Standard loop
8035 Find_Elaboration_Context (Compilation_Unit (Par_Id));
8036
8037 Par_Id := Scope (Par_Id);
8038 end loop;
8039 end if;
8040 end Collect_Elaborated_Units;
8041
8042 -------------
8043 -- Destroy --
8044 -------------
8045
8046 procedure Destroy (EA_Id : in out Elaboration_Attributes_Id) is
8047 pragma Unreferenced (EA_Id);
8048 begin
8049 null;
8050 end Destroy;
8051
8052 -----------------
8053 -- Elab_Pragma --
8054 -----------------
8055
8056 function Elab_Pragma
8057 (EA_Id : Elaboration_Attributes_Id) return Node_Id
8058 is
8059 pragma Assert (Present (EA_Id));
8060 begin
8061 return Elaboration_Attributes.Table (EA_Id).Elab_Pragma;
8062 end Elab_Pragma;
8063
8064 -------------------------------
8065 -- Elaboration_Attributes_Of --
8066 -------------------------------
8067
8068 function Elaboration_Attributes_Of
8069 (Unit_Id : Entity_Id) return Elaboration_Attributes_Id
8070 is
8071 EA_Id : Elaboration_Attributes_Id;
8072
8073 begin
8074 EA_Id := UA_Map.Get (Unit_To_Attributes_Map, Unit_Id);
8075
8076 -- The unit lacks elaboration attributes. This indicates that the
8077 -- unit is encountered for the first time. Create the elaboration
8078 -- attributes for it.
8079
8080 if not Present (EA_Id) then
8081 Elaboration_Attributes.Append
8082 ((Elab_Pragma => Empty,
8083 With_Clause => Empty));
8084 EA_Id := Elaboration_Attributes.Last;
8085
8086 -- Associate the elaboration attributes with the unit
8087
8088 UA_Map.Put (Unit_To_Attributes_Map, Unit_Id, EA_Id);
8089 end if;
8090
8091 pragma Assert (Present (EA_Id));
8092
8093 return EA_Id;
8094 end Elaboration_Attributes_Of;
8095
8096 ------------------------------
8097 -- Ensure_Prior_Elaboration --
8098 ------------------------------
8099
8100 procedure Ensure_Prior_Elaboration
8101 (N : Node_Id;
8102 Unit_Id : Entity_Id;
8103 Prag_Nam : Name_Id;
8104 In_State : Processing_In_State)
8105 is
8106 pragma Assert (Nam_In (Prag_Nam, Name_Elaborate, Name_Elaborate_All));
8107
8108 begin
8109 -- Nothing to do when the need for prior elaboration came from a
8110 -- partial finalization routine which occurs in an initialization
8111 -- context. This behaviour parallels that of the old ABE mechanism.
8112
8113 if In_State.Within_Partial_Finalization then
8114 return;
8115
8116 -- Nothing to do when the need for prior elaboration came from a task
8117 -- body and switch -gnatd.y (disable implicit pragma Elaborate_All on
8118 -- task bodies) is in effect.
8119
8120 elsif Debug_Flag_Dot_Y and then In_State.Within_Task_Body then
8121 return;
8122
8123 -- Nothing to do when the unit is elaborated prior to the main unit.
8124 -- This check must also consider the following cases:
8125 --
8126 -- * No check is made against the context of the main unit because
8127 -- this is specific to the elaboration model in effect and requires
8128 -- custom handling (see Ensure_xxx_Prior_Elaboration).
8129 --
8130 -- * Unit_Id is subject to pragma Elaborate_Body. An implicit pragma
8131 -- Elaborate[_All] MUST be generated even though Unit_Id is always
8132 -- elaborated prior to the main unit. This conservative strategy
8133 -- ensures that other units withed by Unit_Id will not lead to an
8134 -- ABE.
8135 --
8136 -- package A is package body A is
8137 -- procedure ABE; procedure ABE is ... end ABE;
8138 -- end A; end A;
8139 --
8140 -- with A;
8141 -- package B is package body B is
8142 -- pragma Elaborate_Body; procedure Proc is
8143 -- begin
8144 -- procedure Proc; A.ABE;
8145 -- package B; end Proc;
8146 -- end B;
8147 --
8148 -- with B;
8149 -- package C is package body C is
8150 -- ... ...
8151 -- end C; begin
8152 -- B.Proc;
8153 -- end C;
8154 --
8155 -- In the example above, the elaboration of C invokes B.Proc. B is
8156 -- subject to pragma Elaborate_Body. If no pragma Elaborate[_All]
8157 -- is gnerated for B in C, then the following elaboratio order will
8158 -- lead to an ABE:
8159 --
8160 -- spec of A elaborated
8161 -- spec of B elaborated
8162 -- body of B elaborated
8163 -- spec of C elaborated
8164 -- body of C elaborated <-- calls B.Proc which calls A.ABE
8165 -- body of A elaborated <-- problem
8166 --
8167 -- The generation of an implicit pragma Elaborate_All (B) ensures
8168 -- that the elaboration-order mechanism will not pick the above
8169 -- order.
8170 --
8171 -- An implicit Elaborate is NOT generated when the unit is subject
8172 -- to Elaborate_Body because both pragmas have the same effect.
8173 --
8174 -- * Unit_Id is the main unit. An implicit pragma Elaborate[_All]
8175 -- MUST NOT be generated in this case because a unit cannot depend
8176 -- on its own elaboration. This case is therefore treated as valid
8177 -- prior elaboration.
8178
8179 elsif Has_Prior_Elaboration
8180 (Unit_Id => Unit_Id,
8181 Same_Unit_OK => True,
8182 Elab_Body_OK => Prag_Nam = Name_Elaborate)
8183 then
8184 return;
8185 end if;
8186
8187 -- Suggest the use of pragma Prag_Nam when the dynamic model is in
8188 -- effect.
8189
8190 if Dynamic_Elaboration_Checks then
8191 Ensure_Prior_Elaboration_Dynamic
8192 (N => N,
8193 Unit_Id => Unit_Id,
8194 Prag_Nam => Prag_Nam,
8195 In_State => In_State);
8196
8197 -- Install an implicit pragma Prag_Nam when the static model is in
8198 -- effect.
8199
8200 else
8201 pragma Assert (Static_Elaboration_Checks);
8202
8203 Ensure_Prior_Elaboration_Static
8204 (N => N,
8205 Unit_Id => Unit_Id,
8206 Prag_Nam => Prag_Nam,
8207 In_State => In_State);
8208 end if;
8209 end Ensure_Prior_Elaboration;
8210
8211 --------------------------------------
8212 -- Ensure_Prior_Elaboration_Dynamic --
8213 --------------------------------------
8214
8215 procedure Ensure_Prior_Elaboration_Dynamic
8216 (N : Node_Id;
8217 Unit_Id : Entity_Id;
8218 Prag_Nam : Name_Id;
8219 In_State : Processing_In_State)
8220 is
8221 procedure Info_Missing_Pragma;
8222 pragma Inline (Info_Missing_Pragma);
8223 -- Output information concerning missing Elaborate or Elaborate_All
8224 -- pragma with name Prag_Nam for scenario N, which would ensure the
8225 -- prior elaboration of Unit_Id.
8226
8227 -------------------------
8228 -- Info_Missing_Pragma --
8229 -------------------------
8230
8231 procedure Info_Missing_Pragma is
8232 begin
8233 -- Internal units are ignored as they cause unnecessary noise
8234
8235 if not In_Internal_Unit (Unit_Id) then
8236
8237 -- The name of the unit subjected to the elaboration pragma is
8238 -- fully qualified to improve the clarity of the info message.
8239
8240 Error_Msg_Name_1 := Prag_Nam;
8241 Error_Msg_Qual_Level := Nat'Last;
8242
8243 Error_Msg_NE ("info: missing pragma % for unit &", N, Unit_Id);
8244 Error_Msg_Qual_Level := 0;
8245 end if;
8246 end Info_Missing_Pragma;
8247
8248 -- Local variables
8249
8250 EA_Id : constant Elaboration_Attributes_Id :=
8251 Elaboration_Attributes_Of (Unit_Id);
8252 N_Lvl : Enclosing_Level_Kind;
8253 N_Rep : Scenario_Rep_Id;
8254
8255 -- Start of processing for Ensure_Prior_Elaboration_Dynamic
8256
8257 begin
8258 -- Nothing to do when the unit is guaranteed prior elaboration by
8259 -- means of a source Elaborate[_All] pragma.
8260
8261 if Present (Elab_Pragma (EA_Id)) then
8262 return;
8263 end if;
8264
8265 -- Output extra information on a missing Elaborate[_All] pragma when
8266 -- switch -gnatel (info messages on implicit Elaborate[_All] pragmas
8267 -- is in effect.
8268
8269 if Elab_Info_Messages
8270 and then not In_State.Suppress_Info_Messages
8271 then
8272 N_Rep := Scenario_Representation_Of (N, In_State);
8273 N_Lvl := Level (N_Rep);
8274
8275 -- Declaration-level scenario
8276
8277 if (Is_Suitable_Call (N) or else Is_Suitable_Instantiation (N))
8278 and then N_Lvl = Declaration_Level
8279 then
8280 null;
8281
8282 -- Library-level scenario
8283
8284 elsif N_Lvl in Library_Level then
8285 null;
8286
8287 -- Instantiation library-level scenario
8288
8289 elsif N_Lvl = Instantiation_Level then
8290 null;
8291
8292 -- Otherwise the scenario does not appear at the proper level
8293
8294 else
8295 return;
8296 end if;
8297
8298 Info_Missing_Pragma;
8299 end if;
8300 end Ensure_Prior_Elaboration_Dynamic;
8301
8302 -------------------------------------
8303 -- Ensure_Prior_Elaboration_Static --
8304 -------------------------------------
8305
8306 procedure Ensure_Prior_Elaboration_Static
8307 (N : Node_Id;
8308 Unit_Id : Entity_Id;
8309 Prag_Nam : Name_Id;
8310 In_State : Processing_In_State)
8311 is
8312 function Find_With_Clause
8313 (Items : List_Id;
8314 Withed_Id : Entity_Id) return Node_Id;
8315 pragma Inline (Find_With_Clause);
8316 -- Find a nonlimited with clause in the list of context items Items
8317 -- that withs unit Withed_Id. Return Empty if no such clause exists.
8318
8319 procedure Info_Implicit_Pragma;
8320 pragma Inline (Info_Implicit_Pragma);
8321 -- Output information concerning an implicitly generated Elaborate
8322 -- or Elaborate_All pragma with name Prag_Nam for scenario N which
8323 -- ensures the prior elaboration of unit Unit_Id.
8324
8325 ----------------------
8326 -- Find_With_Clause --
8327 ----------------------
8328
8329 function Find_With_Clause
8330 (Items : List_Id;
8331 Withed_Id : Entity_Id) return Node_Id
8332 is
8333 Item : Node_Id;
8334
8335 begin
8336 -- Examine the context clauses looking for a suitable with. Note
8337 -- that limited clauses do not affect the elaboration order.
8338
8339 Item := First (Items);
8340 while Present (Item) loop
8341 if Nkind (Item) = N_With_Clause
8342 and then not Error_Posted (Item)
8343 and then not Limited_Present (Item)
8344 and then Entity (Name (Item)) = Withed_Id
8345 then
8346 return Item;
8347 end if;
8348
8349 Next (Item);
8350 end loop;
8351
8352 return Empty;
8353 end Find_With_Clause;
8354
8355 --------------------------
8356 -- Info_Implicit_Pragma --
8357 --------------------------
8358
8359 procedure Info_Implicit_Pragma is
8360 begin
8361 -- Internal units are ignored as they cause unnecessary noise
8362
8363 if not In_Internal_Unit (Unit_Id) then
8364
8365 -- The name of the unit subjected to the elaboration pragma is
8366 -- fully qualified to improve the clarity of the info message.
8367
8368 Error_Msg_Name_1 := Prag_Nam;
8369 Error_Msg_Qual_Level := Nat'Last;
8370
8371 Error_Msg_NE
8372 ("info: implicit pragma % generated for unit &", N, Unit_Id);
8373
8374 Error_Msg_Qual_Level := 0;
8375 Output_Active_Scenarios (N, In_State);
8376 end if;
8377 end Info_Implicit_Pragma;
8378
8379 -- Local variables
8380
8381 EA_Id : constant Elaboration_Attributes_Id :=
8382 Elaboration_Attributes_Of (Unit_Id);
8383
8384 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
8385 Loc : constant Source_Ptr := Sloc (Main_Cunit);
8386 Unit_Cunit : constant Node_Id := Compilation_Unit (Unit_Id);
8387 Unit_Prag : constant Node_Id := Elab_Pragma (EA_Id);
8388 Unit_With : constant Node_Id := With_Clause (EA_Id);
8389
8390 Clause : Node_Id;
8391 Items : List_Id;
8392
8393 -- Start of processing for Ensure_Prior_Elaboration_Static
8394
8395 begin
8396 -- Nothing to do when the caller has suppressed the generation of
8397 -- implicit Elaborate[_All] pragmas.
8398
8399 if In_State.Suppress_Implicit_Pragmas then
8400 return;
8401
8402 -- Nothing to do when the unit is guaranteed prior elaboration by
8403 -- means of a source Elaborate[_All] pragma.
8404
8405 elsif Present (Unit_Prag) then
8406 return;
8407
8408 -- Nothing to do when the unit has an existing implicit Elaborate or
8409 -- Elaborate_All pragma installed by a previous scenario.
8410
8411 elsif Present (Unit_With) then
8412
8413 -- The unit is already guaranteed prior elaboration by means of an
8414 -- implicit Elaborate pragma, however the current scenario imposes
8415 -- a stronger requirement of Elaborate_All. "Upgrade" the existing
8416 -- pragma to match this new requirement.
8417
8418 if Elaborate_Desirable (Unit_With)
8419 and then Prag_Nam = Name_Elaborate_All
8420 then
8421 Set_Elaborate_All_Desirable (Unit_With);
8422 Set_Elaborate_Desirable (Unit_With, False);
8423 end if;
8424
8425 return;
8426 end if;
8427
8428 -- At this point it is known that the unit has no prior elaboration
8429 -- according to pragmas and hierarchical relationships.
8430
8431 Items := Context_Items (Main_Cunit);
8432
8433 if No (Items) then
8434 Items := New_List;
8435 Set_Context_Items (Main_Cunit, Items);
8436 end if;
8437
8438 -- Locate the with clause for the unit. Note that there may not be a
8439 -- clause if the unit is visible through a subunit-body, body-spec,
8440 -- or spec-parent relationship.
8441
8442 Clause :=
8443 Find_With_Clause
8444 (Items => Items,
8445 Withed_Id => Unit_Id);
8446
8447 -- Generate:
8448 -- with Id;
8449
8450 -- Note that adding implicit with clauses is safe because analysis,
8451 -- resolution, and expansion have already taken place and it is not
8452 -- possible to interfere with visibility.
8453
8454 if No (Clause) then
8455 Clause :=
8456 Make_With_Clause (Loc,
8457 Name => New_Occurrence_Of (Unit_Id, Loc));
8458
8459 Set_Implicit_With (Clause);
8460 Set_Library_Unit (Clause, Unit_Cunit);
8461
8462 Append_To (Items, Clause);
8463 end if;
8464
8465 -- Mark the with clause depending on the pragma required
8466
8467 if Prag_Nam = Name_Elaborate then
8468 Set_Elaborate_Desirable (Clause);
8469 else
8470 Set_Elaborate_All_Desirable (Clause);
8471 end if;
8472
8473 -- The implicit Elaborate[_All] ensures the prior elaboration of
8474 -- the unit. Include the unit in the elaboration context of the
8475 -- main unit.
8476
8477 Set_With_Clause (EA_Id, Clause);
8478
8479 -- Output extra information on an implicit Elaborate[_All] pragma
8480 -- when switch -gnatel (info messages on implicit Elaborate[_All]
8481 -- pragmas is in effect.
8482
8483 if Elab_Info_Messages then
8484 Info_Implicit_Pragma;
8485 end if;
8486 end Ensure_Prior_Elaboration_Static;
8487
8488 -------------------------------
8489 -- Finalize_Elaborated_Units --
8490 -------------------------------
8491
8492 procedure Finalize_Elaborated_Units is
8493 begin
8494 UA_Map.Destroy (Unit_To_Attributes_Map);
8495 end Finalize_Elaborated_Units;
8496
8497 ---------------------------
8498 -- Has_Prior_Elaboration --
8499 ---------------------------
8500
8501 function Has_Prior_Elaboration
8502 (Unit_Id : Entity_Id;
8503 Context_OK : Boolean := False;
8504 Elab_Body_OK : Boolean := False;
8505 Same_Unit_OK : Boolean := False) return Boolean
8506 is
8507 EA_Id : constant Elaboration_Attributes_Id :=
8508 Elaboration_Attributes_Of (Unit_Id);
8509 Main_Id : constant Entity_Id := Main_Unit_Entity;
8510 Unit_Prag : constant Node_Id := Elab_Pragma (EA_Id);
8511 Unit_With : constant Node_Id := With_Clause (EA_Id);
8512
8513 begin
8514 -- A preelaborated unit is always elaborated prior to the main unit
8515
8516 if Is_Preelaborated_Unit (Unit_Id) then
8517 return True;
8518
8519 -- An internal unit is always elaborated prior to a non-internal main
8520 -- unit.
8521
8522 elsif In_Internal_Unit (Unit_Id)
8523 and then not In_Internal_Unit (Main_Id)
8524 then
8525 return True;
8526
8527 -- A unit has prior elaboration if it appears within the context
8528 -- of the main unit. Consider this case only when requested by the
8529 -- caller.
8530
8531 elsif Context_OK
8532 and then (Present (Unit_Prag) or else Present (Unit_With))
8533 then
8534 return True;
8535
8536 -- A unit whose body is elaborated together with its spec has prior
8537 -- elaboration except with respect to itself. Consider this case only
8538 -- when requested by the caller.
8539
8540 elsif Elab_Body_OK
8541 and then Has_Pragma_Elaborate_Body (Unit_Id)
8542 and then not Is_Same_Unit (Unit_Id, Main_Id)
8543 then
8544 return True;
8545
8546 -- A unit has no prior elaboration with respect to itself, but does
8547 -- not require any means of ensuring its own elaboration either.
8548 -- Treat this case as valid prior elaboration only when requested by
8549 -- the caller.
8550
8551 elsif Same_Unit_OK and then Is_Same_Unit (Unit_Id, Main_Id) then
8552 return True;
8553 end if;
8554
8555 return False;
8556 end Has_Prior_Elaboration;
8557
8558 ---------------------------------
8559 -- Initialize_Elaborated_Units --
8560 ---------------------------------
8561
8562 procedure Initialize_Elaborated_Units is
8563 begin
8564 Unit_To_Attributes_Map := UA_Map.Create (250);
8565 end Initialize_Elaborated_Units;
8566
8567 ----------------------------------
8568 -- Meet_Elaboration_Requirement --
8569 ----------------------------------
8570
8571 procedure Meet_Elaboration_Requirement
8572 (N : Node_Id;
8573 Targ_Id : Entity_Id;
8574 Req_Nam : Name_Id;
8575 In_State : Processing_In_State)
8576 is
8577 pragma Assert (Nam_In (Req_Nam, Name_Elaborate, Name_Elaborate_All));
8578
8579 Main_Id : constant Entity_Id := Main_Unit_Entity;
8580 Unit_Id : constant Entity_Id := Find_Top_Unit (Targ_Id);
8581
8582 procedure Elaboration_Requirement_Error;
8583 pragma Inline (Elaboration_Requirement_Error);
8584 -- Emit an error concerning scenario N which has failed to meet the
8585 -- elaboration requirement.
8586
8587 function Find_Preelaboration_Pragma
8588 (Prag_Nam : Name_Id) return Node_Id;
8589 pragma Inline (Find_Preelaboration_Pragma);
8590 -- Traverse the visible declarations of unit Unit_Id and locate a
8591 -- source preelaboration-related pragma with name Prag_Nam.
8592
8593 procedure Info_Requirement_Met (Prag : Node_Id);
8594 pragma Inline (Info_Requirement_Met);
8595 -- Output information concerning pragma Prag which meets requirement
8596 -- Req_Nam.
8597
8598 -----------------------------------
8599 -- Elaboration_Requirement_Error --
8600 -----------------------------------
8601
8602 procedure Elaboration_Requirement_Error is
8603 begin
8604 if Is_Suitable_Call (N) then
8605 Info_Call
8606 (Call => N,
8607 Subp_Id => Targ_Id,
8608 Info_Msg => False,
8609 In_SPARK => True);
8610
8611 elsif Is_Suitable_Instantiation (N) then
8612 Info_Instantiation
8613 (Inst => N,
8614 Gen_Id => Targ_Id,
8615 Info_Msg => False,
8616 In_SPARK => True);
8617
8618 elsif Is_Suitable_SPARK_Refined_State_Pragma (N) then
8619 Error_Msg_N
8620 ("read of refinement constituents during elaboration in "
8621 & "SPARK", N);
8622
8623 elsif Is_Suitable_Variable_Reference (N) then
8624 Info_Variable_Reference
8625 (Ref => N,
8626 Var_Id => Targ_Id,
8627 Info_Msg => False,
8628 In_SPARK => True);
8629
8630 -- No other scenario may impose a requirement on the context of
8631 -- the main unit.
8632
8633 else
8634 pragma Assert (False);
8635 return;
8636 end if;
8637
8638 Error_Msg_Name_1 := Req_Nam;
8639 Error_Msg_Node_2 := Unit_Id;
8640 Error_Msg_NE ("\\unit & requires pragma % for &", N, Main_Id);
8641
8642 Output_Active_Scenarios (N, In_State);
8643 end Elaboration_Requirement_Error;
8644
8645 --------------------------------
8646 -- Find_Preelaboration_Pragma --
8647 --------------------------------
8648
8649 function Find_Preelaboration_Pragma
8650 (Prag_Nam : Name_Id) return Node_Id
8651 is
8652 Spec : constant Node_Id := Parent (Unit_Id);
8653 Decl : Node_Id;
8654
8655 begin
8656 -- A preelaboration-related pragma comes from source and appears
8657 -- at the top of the visible declarations of a package.
8658
8659 if Nkind (Spec) = N_Package_Specification then
8660 Decl := First (Visible_Declarations (Spec));
8661 while Present (Decl) loop
8662 if Comes_From_Source (Decl) then
8663 if Nkind (Decl) = N_Pragma
8664 and then Pragma_Name (Decl) = Prag_Nam
8665 then
8666 return Decl;
8667
8668 -- Otherwise the construct terminates the region where
8669 -- the preelaboration-related pragma may appear.
8670
8671 else
8672 exit;
8673 end if;
8674 end if;
8675
8676 Next (Decl);
8677 end loop;
8678 end if;
8679
8680 return Empty;
8681 end Find_Preelaboration_Pragma;
8682
8683 --------------------------
8684 -- Info_Requirement_Met --
8685 --------------------------
8686
8687 procedure Info_Requirement_Met (Prag : Node_Id) is
8688 pragma Assert (Present (Prag));
8689
8690 begin
8691 Error_Msg_Name_1 := Req_Nam;
8692 Error_Msg_Sloc := Sloc (Prag);
8693 Error_Msg_NE
8694 ("\\% requirement for unit & met by pragma #", N, Unit_Id);
8695 end Info_Requirement_Met;
8696
8697 -- Local variables
8698
8699 EA_Id : Elaboration_Attributes_Id;
8700 Elab_Nam : Name_Id;
8701 Req_Met : Boolean;
8702 Unit_Prag : Node_Id;
8703
8704 -- Start of processing for Meet_Elaboration_Requirement
8705
8706 begin
8707 -- Assume that the requirement has not been met
8708
8709 Req_Met := False;
8710
8711 -- If the target is within the main unit, either at the source level
8712 -- or through an instantiation, then there is no real requirement to
8713 -- meet because the main unit cannot force its own elaboration by
8714 -- means of an Elaborate[_All] pragma. Treat this case as valid
8715 -- coverage.
8716
8717 if In_Extended_Main_Code_Unit (Targ_Id) then
8718 Req_Met := True;
8719
8720 -- Otherwise the target resides in an external unit
8721
8722 -- The requirement is met when the target comes from an internal unit
8723 -- because such a unit is elaborated prior to a non-internal unit.
8724
8725 elsif In_Internal_Unit (Unit_Id)
8726 and then not In_Internal_Unit (Main_Id)
8727 then
8728 Req_Met := True;
8729
8730 -- The requirement is met when the target comes from a preelaborated
8731 -- unit. This portion must parallel predicate Is_Preelaborated_Unit.
8732
8733 elsif Is_Preelaborated_Unit (Unit_Id) then
8734 Req_Met := True;
8735
8736 -- Output extra information when switch -gnatel (info messages on
8737 -- implicit Elaborate[_All] pragmas.
8738
8739 if Elab_Info_Messages
8740 and then not In_State.Suppress_Info_Messages
8741 then
8742 if Is_Preelaborated (Unit_Id) then
8743 Elab_Nam := Name_Preelaborate;
8744
8745 elsif Is_Pure (Unit_Id) then
8746 Elab_Nam := Name_Pure;
8747
8748 elsif Is_Remote_Call_Interface (Unit_Id) then
8749 Elab_Nam := Name_Remote_Call_Interface;
8750
8751 elsif Is_Remote_Types (Unit_Id) then
8752 Elab_Nam := Name_Remote_Types;
8753
8754 else
8755 pragma Assert (Is_Shared_Passive (Unit_Id));
8756 Elab_Nam := Name_Shared_Passive;
8757 end if;
8758
8759 Info_Requirement_Met (Find_Preelaboration_Pragma (Elab_Nam));
8760 end if;
8761
8762 -- Determine whether the context of the main unit has a pragma strong
8763 -- enough to meet the requirement.
8764
8765 else
8766 EA_Id := Elaboration_Attributes_Of (Unit_Id);
8767 Unit_Prag := Elab_Pragma (EA_Id);
8768
8769 -- The pragma must be either Elaborate_All or be as strong as the
8770 -- requirement.
8771
8772 if Present (Unit_Prag)
8773 and then Nam_In (Pragma_Name (Unit_Prag), Name_Elaborate_All,
8774 Req_Nam)
8775 then
8776 Req_Met := True;
8777
8778 -- Output extra information when switch -gnatel (info messages
8779 -- on implicit Elaborate[_All] pragmas.
8780
8781 if Elab_Info_Messages
8782 and then not In_State.Suppress_Info_Messages
8783 then
8784 Info_Requirement_Met (Unit_Prag);
8785 end if;
8786 end if;
8787 end if;
8788
8789 -- The requirement was not met by the context of the main unit, issue
8790 -- an error.
8791
8792 if not Req_Met then
8793 Elaboration_Requirement_Error;
8794 end if;
8795 end Meet_Elaboration_Requirement;
8796
8797 -------------
8798 -- Present --
8799 -------------
8800
8801 function Present (EA_Id : Elaboration_Attributes_Id) return Boolean is
8802 begin
8803 return EA_Id /= No_Elaboration_Attributes;
8804 end Present;
8805
8806 ---------------------
8807 -- Set_Elab_Pragma --
8808 ---------------------
8809
8810 procedure Set_Elab_Pragma
8811 (EA_Id : Elaboration_Attributes_Id;
8812 Prag : Node_Id)
8813 is
8814 pragma Assert (Present (EA_Id));
8815 begin
8816 Elaboration_Attributes.Table (EA_Id).Elab_Pragma := Prag;
8817 end Set_Elab_Pragma;
8818
8819 ---------------------
8820 -- Set_With_Clause --
8821 ---------------------
8822
8823 procedure Set_With_Clause
8824 (EA_Id : Elaboration_Attributes_Id;
8825 Clause : Node_Id)
8826 is
8827 pragma Assert (Present (EA_Id));
8828 begin
8829 Elaboration_Attributes.Table (EA_Id).With_Clause := Clause;
8830 end Set_With_Clause;
8831
8832 -----------------
8833 -- With_Clause --
8834 -----------------
8835
8836 function With_Clause
8837 (EA_Id : Elaboration_Attributes_Id) return Node_Id
8838 is
8839 pragma Assert (Present (EA_Id));
8840 begin
8841 return Elaboration_Attributes.Table (EA_Id).With_Clause;
8842 end With_Clause;
8843 end Elaborated_Units;
8844
8845 ------------------------------
8846 -- Elaboration_Phase_Active --
8847 ------------------------------
8848
8849 function Elaboration_Phase_Active return Boolean is
8850 begin
8851 return Elaboration_Phase = Active;
8852 end Elaboration_Phase_Active;
8853
8854 ----------------------------------
8855 -- Finalize_All_Data_Structures --
8856 ----------------------------------
8857
8858 procedure Finalize_All_Data_Structures is
8859 begin
8860 Finalize_Body_Processor;
8861 Finalize_Early_Call_Region_Processor;
8862 Finalize_Elaborated_Units;
8863 Finalize_Internal_Representation;
8864 Finalize_Invocation_Graph;
8865 Finalize_Scenario_Storage;
8866 end Finalize_All_Data_Structures;
8867
8868 -----------------------------
8869 -- Find_Enclosing_Instance --
8870 -----------------------------
8871
8872 function Find_Enclosing_Instance (N : Node_Id) return Node_Id is
8873 Par : Node_Id;
8874
8875 begin
8876 -- Climb the parent chain looking for an enclosing instance spec or body
8877
8878 Par := N;
8879 while Present (Par) loop
8880 if Nkind_In (Par, N_Package_Body,
8881 N_Package_Declaration,
8882 N_Subprogram_Body,
8883 N_Subprogram_Declaration)
8884 and then Is_Generic_Instance (Unique_Defining_Entity (Par))
8885 then
8886 return Par;
8887 end if;
8888
8889 Par := Parent (Par);
8890 end loop;
8891
8892 return Empty;
8893 end Find_Enclosing_Instance;
8894
8895 --------------------------
8896 -- Find_Enclosing_Level --
8897 --------------------------
8898
8899 function Find_Enclosing_Level (N : Node_Id) return Enclosing_Level_Kind is
8900 function Level_Of (Unit : Node_Id) return Enclosing_Level_Kind;
8901 pragma Inline (Level_Of);
8902 -- Obtain the corresponding level of unit Unit
8903
8904 --------------
8905 -- Level_Of --
8906 --------------
8907
8908 function Level_Of (Unit : Node_Id) return Enclosing_Level_Kind is
8909 Spec_Id : Entity_Id;
8910
8911 begin
8912 if Nkind (Unit) in N_Generic_Instantiation then
8913 return Instantiation_Level;
8914
8915 elsif Nkind (Unit) = N_Generic_Package_Declaration then
8916 return Generic_Spec_Level;
8917
8918 elsif Nkind (Unit) = N_Package_Declaration then
8919 return Library_Spec_Level;
8920
8921 elsif Nkind (Unit) = N_Package_Body then
8922 Spec_Id := Corresponding_Spec (Unit);
8923
8924 -- The body belongs to a generic package
8925
8926 if Present (Spec_Id)
8927 and then Ekind (Spec_Id) = E_Generic_Package
8928 then
8929 return Generic_Body_Level;
8930
8931 -- Otherwise the body belongs to a non-generic package. This also
8932 -- treats an illegal package body without a corresponding spec as
8933 -- a non-generic package body.
8934
8935 else
8936 return Library_Body_Level;
8937 end if;
8938 end if;
8939
8940 return No_Level;
8941 end Level_Of;
8942
8943 -- Local variables
8944
8945 Context : Node_Id;
8946 Curr : Node_Id;
8947 Prev : Node_Id;
8948
8949 -- Start of processing for Find_Enclosing_Level
8950
8951 begin
8952 -- Call markers and instantiations which appear at the declaration level
8953 -- but are later relocated in a different context retain their original
8954 -- declaration level.
8955
8956 if Nkind_In (N, N_Call_Marker,
8957 N_Function_Instantiation,
8958 N_Package_Instantiation,
8959 N_Procedure_Instantiation)
8960 and then Is_Declaration_Level_Node (N)
8961 then
8962 return Declaration_Level;
8963 end if;
8964
8965 -- Climb the parent chain looking at the enclosing levels
8966
8967 Prev := N;
8968 Curr := Parent (Prev);
8969 while Present (Curr) loop
8970
8971 -- A traversal from a subunit continues via the corresponding stub
8972
8973 if Nkind (Curr) = N_Subunit then
8974 Curr := Corresponding_Stub (Curr);
8975
8976 -- The current construct is a package. Packages are ignored because
8977 -- they are always elaborated when the enclosing context is invoked
8978 -- or elaborated.
8979
8980 elsif Nkind_In (Curr, N_Package_Body, N_Package_Declaration) then
8981 null;
8982
8983 -- The current construct is a block statement
8984
8985 elsif Nkind (Curr) = N_Block_Statement then
8986
8987 -- Ignore internally generated blocks created by the expander for
8988 -- various purposes such as abort defer/undefer.
8989
8990 if not Comes_From_Source (Curr) then
8991 null;
8992
8993 -- If the traversal came from the handled sequence of statments,
8994 -- then the node appears at the level of the enclosing construct.
8995 -- This is a more reliable test because transients scopes within
8996 -- the declarative region of the encapsulator are hard to detect.
8997
8998 elsif Nkind (Prev) = N_Handled_Sequence_Of_Statements
8999 and then Handled_Statement_Sequence (Curr) = Prev
9000 then
9001 return Find_Enclosing_Level (Parent (Curr));
9002
9003 -- Otherwise the traversal came from the declarations, the node is
9004 -- at the declaration level.
9005
9006 else
9007 return Declaration_Level;
9008 end if;
9009
9010 -- The current construct is a declaration-level encapsulator
9011
9012 elsif Nkind_In (Curr, N_Entry_Body,
9013 N_Subprogram_Body,
9014 N_Task_Body)
9015 then
9016 -- If the traversal came from the handled sequence of statments,
9017 -- then the node cannot possibly appear at any level. This is
9018 -- a more reliable test because transients scopes within the
9019 -- declarative region of the encapsulator are hard to detect.
9020
9021 if Nkind (Prev) = N_Handled_Sequence_Of_Statements
9022 and then Handled_Statement_Sequence (Curr) = Prev
9023 then
9024 return No_Level;
9025
9026 -- Otherwise the traversal came from the declarations, the node is
9027 -- at the declaration level.
9028
9029 else
9030 return Declaration_Level;
9031 end if;
9032
9033 -- The current construct is a non-library-level encapsulator which
9034 -- indicates that the node cannot possibly appear at any level. Note
9035 -- that the check must come after the declaration-level check because
9036 -- both predicates share certain nodes.
9037
9038 elsif Is_Non_Library_Level_Encapsulator (Curr) then
9039 Context := Parent (Curr);
9040
9041 -- The sole exception is when the encapsulator is the compilation
9042 -- utit itself because the compilation unit node requires special
9043 -- processing (see below).
9044
9045 if Present (Context)
9046 and then Nkind (Context) = N_Compilation_Unit
9047 then
9048 null;
9049
9050 -- Otherwise the node is not at any level
9051
9052 else
9053 return No_Level;
9054 end if;
9055
9056 -- The current construct is a compilation unit. The node appears at
9057 -- the [generic] library level when the unit is a [generic] package.
9058
9059 elsif Nkind (Curr) = N_Compilation_Unit then
9060 return Level_Of (Unit (Curr));
9061 end if;
9062
9063 Prev := Curr;
9064 Curr := Parent (Prev);
9065 end loop;
9066
9067 return No_Level;
9068 end Find_Enclosing_Level;
9069
9070 -------------------
9071 -- Find_Top_Unit --
9072 -------------------
9073
9074 function Find_Top_Unit (N : Node_Or_Entity_Id) return Entity_Id is
9075 begin
9076 return Find_Unit_Entity (Unit (Cunit (Get_Top_Level_Code_Unit (N))));
9077 end Find_Top_Unit;
9078
9079 ----------------------
9080 -- Find_Unit_Entity --
9081 ----------------------
9082
9083 function Find_Unit_Entity (N : Node_Id) return Entity_Id is
9084 Context : constant Node_Id := Parent (N);
9085 Orig_N : constant Node_Id := Original_Node (N);
9086
9087 begin
9088 -- The unit denotes a package body of an instantiation which acts as
9089 -- a compilation unit. The proper entity is that of the package spec.
9090
9091 if Nkind (N) = N_Package_Body
9092 and then Nkind (Orig_N) = N_Package_Instantiation
9093 and then Nkind (Context) = N_Compilation_Unit
9094 then
9095 return Corresponding_Spec (N);
9096
9097 -- The unit denotes an anonymous package created to wrap a subprogram
9098 -- instantiation which acts as a compilation unit. The proper entity is
9099 -- that of the "related instance".
9100
9101 elsif Nkind (N) = N_Package_Declaration
9102 and then Nkind_In (Orig_N, N_Function_Instantiation,
9103 N_Procedure_Instantiation)
9104 and then Nkind (Context) = N_Compilation_Unit
9105 then
9106 return
9107 Related_Instance (Defining_Entity (N, Concurrent_Subunit => True));
9108
9109 -- Otherwise the proper entity is the defining entity
9110
9111 else
9112 return Defining_Entity (N, Concurrent_Subunit => True);
9113 end if;
9114 end Find_Unit_Entity;
9115
9116 -----------------------
9117 -- First_Formal_Type --
9118 -----------------------
9119
9120 function First_Formal_Type (Subp_Id : Entity_Id) return Entity_Id is
9121 Formal_Id : constant Entity_Id := First_Formal (Subp_Id);
9122 Typ : Entity_Id;
9123
9124 begin
9125 if Present (Formal_Id) then
9126 Typ := Etype (Formal_Id);
9127
9128 -- Handle various combinations of concurrent and private types
9129
9130 loop
9131 if Ekind_In (Typ, E_Protected_Type, E_Task_Type)
9132 and then Present (Anonymous_Object (Typ))
9133 then
9134 Typ := Anonymous_Object (Typ);
9135
9136 elsif Is_Concurrent_Record_Type (Typ) then
9137 Typ := Corresponding_Concurrent_Type (Typ);
9138
9139 elsif Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
9140 Typ := Full_View (Typ);
9141
9142 else
9143 exit;
9144 end if;
9145 end loop;
9146
9147 return Typ;
9148 end if;
9149
9150 return Empty;
9151 end First_Formal_Type;
9152
9153 ------------------------------
9154 -- Guaranteed_ABE_Processor --
9155 ------------------------------
9156
9157 package body Guaranteed_ABE_Processor is
9158 function Is_Guaranteed_ABE
9159 (N : Node_Id;
9160 Target_Decl : Node_Id;
9161 Target_Body : Node_Id) return Boolean;
9162 pragma Inline (Is_Guaranteed_ABE);
9163 -- Determine whether scenario N with a target described by its initial
9164 -- declaration Target_Decl and body Target_Decl results in a guaranteed
9165 -- ABE.
9166
9167 procedure Process_Guaranteed_ABE_Activation
9168 (Call : Node_Id;
9169 Call_Rep : Scenario_Rep_Id;
9170 Obj_Id : Entity_Id;
9171 Obj_Rep : Target_Rep_Id;
9172 Task_Typ : Entity_Id;
9173 Task_Rep : Target_Rep_Id;
9174 In_State : Processing_In_State);
9175 pragma Inline (Process_Guaranteed_ABE_Activation);
9176 -- Perform common guaranteed ABE checks and diagnostics for activation
9177 -- call Call which activates object Obj_Id of task type Task_Typ. Formal
9178 -- Call_Rep denotes the representation of the call. Obj_Rep denotes the
9179 -- representation of the object. Task_Rep denotes the representation of
9180 -- the task type. In_State is the current state of the Processing phase.
9181
9182 procedure Process_Guaranteed_ABE_Call
9183 (Call : Node_Id;
9184 Call_Rep : Scenario_Rep_Id;
9185 In_State : Processing_In_State);
9186 pragma Inline (Process_Guaranteed_ABE_Call);
9187 -- Perform common guaranteed ABE checks and diagnostics for call Call
9188 -- with representation Call_Rep. In_State denotes the current state of
9189 -- the Processing phase.
9190
9191 procedure Process_Guaranteed_ABE_Instantiation
9192 (Inst : Node_Id;
9193 Inst_Rep : Scenario_Rep_Id;
9194 In_State : Processing_In_State);
9195 pragma Inline (Process_Guaranteed_ABE_Instantiation);
9196 -- Perform common guaranteed ABE checks and diagnostics for instance
9197 -- Inst with representation Inst_Rep. In_State is the current state of
9198 -- the Processing phase.
9199
9200 -----------------------
9201 -- Is_Guaranteed_ABE --
9202 -----------------------
9203
9204 function Is_Guaranteed_ABE
9205 (N : Node_Id;
9206 Target_Decl : Node_Id;
9207 Target_Body : Node_Id) return Boolean
9208 is
9209 begin
9210 -- Avoid cascaded errors if there were previous serious infractions.
9211 -- As a result the scenario will not be treated as a guaranteed ABE.
9212 -- This behaviour parallels that of the old ABE mechanism.
9213
9214 if Serious_Errors_Detected > 0 then
9215 return False;
9216
9217 -- The scenario and the target appear in the same context ignoring
9218 -- enclosing library levels.
9219
9220 elsif In_Same_Context (N, Target_Decl) then
9221
9222 -- The target body has already been encountered. The scenario
9223 -- results in a guaranteed ABE if it appears prior to the body.
9224
9225 if Present (Target_Body) then
9226 return Earlier_In_Extended_Unit (N, Target_Body);
9227
9228 -- Otherwise the body has not been encountered yet. The scenario
9229 -- is a guaranteed ABE since the body will appear later. It is
9230 -- assumed that the caller has already ensured that the scenario
9231 -- is ABE-safe because optional bodies are not considered here.
9232
9233 else
9234 return True;
9235 end if;
9236 end if;
9237
9238 return False;
9239 end Is_Guaranteed_ABE;
9240
9241 ----------------------------
9242 -- Process_Guaranteed_ABE --
9243 ----------------------------
9244
9245 procedure Process_Guaranteed_ABE
9246 (N : Node_Id;
9247 In_State : Processing_In_State)
9248 is
9249 Scen : constant Node_Id := Scenario (N);
9250 Scen_Rep : Scenario_Rep_Id;
9251
9252 begin
9253 -- Add the current scenario to the stack of active scenarios
9254
9255 Push_Active_Scenario (Scen);
9256
9257 -- Only calls, instantiations, and task activations may result in a
9258 -- guaranteed ABE.
9259
9260 -- Call or task activation
9261
9262 if Is_Suitable_Call (Scen) then
9263 Scen_Rep := Scenario_Representation_Of (Scen, In_State);
9264
9265 if Kind (Scen_Rep) = Call_Scenario then
9266 Process_Guaranteed_ABE_Call
9267 (Call => Scen,
9268 Call_Rep => Scen_Rep,
9269 In_State => In_State);
9270
9271 else
9272 pragma Assert (Kind (Scen_Rep) = Task_Activation_Scenario);
9273
9274 Process_Activation
9275 (Call => Scen,
9276 Call_Rep => Scenario_Representation_Of (Scen, In_State),
9277 Processor => Process_Guaranteed_ABE_Activation'Access,
9278 In_State => In_State);
9279 end if;
9280
9281 -- Instantiation
9282
9283 elsif Is_Suitable_Instantiation (Scen) then
9284 Process_Guaranteed_ABE_Instantiation
9285 (Inst => Scen,
9286 Inst_Rep => Scenario_Representation_Of (Scen, In_State),
9287 In_State => In_State);
9288 end if;
9289
9290 -- Remove the current scenario from the stack of active scenarios
9291 -- once all ABE diagnostics and checks have been performed.
9292
9293 Pop_Active_Scenario (Scen);
9294 end Process_Guaranteed_ABE;
9295
9296 ---------------------------------------
9297 -- Process_Guaranteed_ABE_Activation --
9298 ---------------------------------------
9299
9300 procedure Process_Guaranteed_ABE_Activation
9301 (Call : Node_Id;
9302 Call_Rep : Scenario_Rep_Id;
9303 Obj_Id : Entity_Id;
9304 Obj_Rep : Target_Rep_Id;
9305 Task_Typ : Entity_Id;
9306 Task_Rep : Target_Rep_Id;
9307 In_State : Processing_In_State)
9308 is
9309 Spec_Decl : constant Node_Id := Spec_Declaration (Task_Rep);
9310
9311 Check_OK : constant Boolean :=
9312 not In_State.Suppress_Checks
9313 and then Ghost_Mode_Of (Obj_Rep) /= Is_Ignored
9314 and then Ghost_Mode_Of (Task_Rep) /= Is_Ignored
9315 and then Elaboration_Checks_OK (Obj_Rep)
9316 and then Elaboration_Checks_OK (Task_Rep);
9317 -- A run-time ABE check may be installed only when the object and the
9318 -- task type have active elaboration checks, and both are not ignored
9319 -- Ghost constructs.
9320
9321 begin
9322 -- Nothing to do when the root scenario appears at the declaration
9323 -- level and the task is in the same unit, but outside this context.
9324 --
9325 -- task type Task_Typ; -- task declaration
9326 --
9327 -- procedure Proc is
9328 -- function A ... is
9329 -- begin
9330 -- if Some_Condition then
9331 -- declare
9332 -- T : Task_Typ;
9333 -- begin
9334 -- <activation call> -- activation site
9335 -- end;
9336 -- ...
9337 -- end A;
9338 --
9339 -- X : ... := A; -- root scenario
9340 -- ...
9341 --
9342 -- task body Task_Typ is
9343 -- ...
9344 -- end Task_Typ;
9345 --
9346 -- In the example above, the context of X is the declarative list
9347 -- of Proc. The "elaboration" of X may reach the activation of T
9348 -- whose body is defined outside of X's context. The task body is
9349 -- relevant only when Proc is invoked, but this happens only in
9350 -- "normal" elaboration, therefore the task body must not be
9351 -- considered if this is not the case.
9352
9353 if Is_Up_Level_Target
9354 (Targ_Decl => Spec_Decl,
9355 In_State => In_State)
9356 then
9357 return;
9358
9359 -- Nothing to do when the activation is ABE-safe
9360 --
9361 -- generic
9362 -- package Gen is
9363 -- task type Task_Typ;
9364 -- end Gen;
9365 --
9366 -- package body Gen is
9367 -- task body Task_Typ is
9368 -- begin
9369 -- ...
9370 -- end Task_Typ;
9371 -- end Gen;
9372 --
9373 -- with Gen;
9374 -- procedure Main is
9375 -- package Nested is
9376 -- package Inst is new Gen;
9377 -- T : Inst.Task_Typ;
9378 -- end Nested; -- safe activation
9379 -- ...
9380
9381 elsif Is_Safe_Activation (Call, Task_Rep) then
9382 return;
9383
9384 -- An activation call leads to a guaranteed ABE when the activation
9385 -- call and the task appear within the same context ignoring library
9386 -- levels, and the body of the task has not been seen yet or appears
9387 -- after the activation call.
9388 --
9389 -- procedure Guaranteed_ABE is
9390 -- task type Task_Typ;
9391 --
9392 -- package Nested is
9393 -- T : Task_Typ;
9394 -- <activation call> -- guaranteed ABE
9395 -- end Nested;
9396 --
9397 -- task body Task_Typ is
9398 -- ...
9399 -- end Task_Typ;
9400 -- ...
9401
9402 elsif Is_Guaranteed_ABE
9403 (N => Call,
9404 Target_Decl => Spec_Decl,
9405 Target_Body => Body_Declaration (Task_Rep))
9406 then
9407 if Elaboration_Warnings_OK (Call_Rep) then
9408 Error_Msg_Sloc := Sloc (Call);
9409 Error_Msg_N
9410 ("??task & will be activated # before elaboration of its "
9411 & "body", Obj_Id);
9412 Error_Msg_N
9413 ("\Program_Error will be raised at run time", Obj_Id);
9414 end if;
9415
9416 -- Mark the activation call as a guaranteed ABE
9417
9418 Set_Is_Known_Guaranteed_ABE (Call);
9419
9420 -- Install a run-time ABE failue because this activation call will
9421 -- always result in an ABE.
9422
9423 if Check_OK then
9424 Install_Scenario_ABE_Failure
9425 (N => Call,
9426 Targ_Id => Task_Typ,
9427 Targ_Rep => Task_Rep,
9428 Disable => Obj_Rep);
9429 end if;
9430 end if;
9431 end Process_Guaranteed_ABE_Activation;
9432
9433 ---------------------------------
9434 -- Process_Guaranteed_ABE_Call --
9435 ---------------------------------
9436
9437 procedure Process_Guaranteed_ABE_Call
9438 (Call : Node_Id;
9439 Call_Rep : Scenario_Rep_Id;
9440 In_State : Processing_In_State)
9441 is
9442 Subp_Id : constant Entity_Id := Target (Call_Rep);
9443 Subp_Rep : constant Target_Rep_Id :=
9444 Target_Representation_Of (Subp_Id, In_State);
9445 Spec_Decl : constant Node_Id := Spec_Declaration (Subp_Rep);
9446
9447 Check_OK : constant Boolean :=
9448 not In_State.Suppress_Checks
9449 and then Ghost_Mode_Of (Call_Rep) /= Is_Ignored
9450 and then Ghost_Mode_Of (Subp_Rep) /= Is_Ignored
9451 and then Elaboration_Checks_OK (Call_Rep)
9452 and then Elaboration_Checks_OK (Subp_Rep);
9453 -- A run-time ABE check may be installed only when both the call
9454 -- and the target have active elaboration checks, and both are not
9455 -- ignored Ghost constructs.
9456
9457 begin
9458 -- Nothing to do when the root scenario appears at the declaration
9459 -- level and the target is in the same unit but outside this context.
9460 --
9461 -- function B ...; -- target declaration
9462 --
9463 -- procedure Proc is
9464 -- function A ... is
9465 -- begin
9466 -- if Some_Condition then
9467 -- return B; -- call site
9468 -- ...
9469 -- end A;
9470 --
9471 -- X : ... := A; -- root scenario
9472 -- ...
9473 --
9474 -- function B ... is
9475 -- ...
9476 -- end B;
9477 --
9478 -- In the example above, the context of X is the declarative region
9479 -- of Proc. The "elaboration" of X may eventually reach B which is
9480 -- defined outside of X's context. B is relevant only when Proc is
9481 -- invoked, but this happens only by means of "normal" elaboration,
9482 -- therefore B must not be considered if this is not the case.
9483
9484 if Is_Up_Level_Target
9485 (Targ_Decl => Spec_Decl,
9486 In_State => In_State)
9487 then
9488 return;
9489
9490 -- Nothing to do when the call is ABE-safe
9491 --
9492 -- generic
9493 -- function Gen ...;
9494 --
9495 -- function Gen ... is
9496 -- begin
9497 -- ...
9498 -- end Gen;
9499 --
9500 -- with Gen;
9501 -- procedure Main is
9502 -- function Inst is new Gen;
9503 -- X : ... := Inst; -- safe call
9504 -- ...
9505
9506 elsif Is_Safe_Call (Call, Subp_Id, Subp_Rep) then
9507 return;
9508
9509 -- A call leads to a guaranteed ABE when the call and the target
9510 -- appear within the same context ignoring library levels, and the
9511 -- body of the target has not been seen yet or appears after the
9512 -- call.
9513 --
9514 -- procedure Guaranteed_ABE is
9515 -- function Func ...;
9516 --
9517 -- package Nested is
9518 -- Obj : ... := Func; -- guaranteed ABE
9519 -- end Nested;
9520 --
9521 -- function Func ... is
9522 -- ...
9523 -- end Func;
9524 -- ...
9525
9526 elsif Is_Guaranteed_ABE
9527 (N => Call,
9528 Target_Decl => Spec_Decl,
9529 Target_Body => Body_Declaration (Subp_Rep))
9530 then
9531 if Elaboration_Warnings_OK (Call_Rep) then
9532 Error_Msg_NE
9533 ("??cannot call & before body seen", Call, Subp_Id);
9534 Error_Msg_N ("\Program_Error will be raised at run time", Call);
9535 end if;
9536
9537 -- Mark the call as a guarnateed ABE
9538
9539 Set_Is_Known_Guaranteed_ABE (Call);
9540
9541 -- Install a run-time ABE failure because the call will always
9542 -- result in an ABE.
9543
9544 if Check_OK then
9545 Install_Scenario_ABE_Failure
9546 (N => Call,
9547 Targ_Id => Subp_Id,
9548 Targ_Rep => Subp_Rep,
9549 Disable => Call_Rep);
9550 end if;
9551 end if;
9552 end Process_Guaranteed_ABE_Call;
9553
9554 ------------------------------------------
9555 -- Process_Guaranteed_ABE_Instantiation --
9556 ------------------------------------------
9557
9558 procedure Process_Guaranteed_ABE_Instantiation
9559 (Inst : Node_Id;
9560 Inst_Rep : Scenario_Rep_Id;
9561 In_State : Processing_In_State)
9562 is
9563 Gen_Id : constant Entity_Id := Target (Inst_Rep);
9564 Gen_Rep : constant Target_Rep_Id :=
9565 Target_Representation_Of (Gen_Id, In_State);
9566 Spec_Decl : constant Node_Id := Spec_Declaration (Gen_Rep);
9567
9568 Check_OK : constant Boolean :=
9569 not In_State.Suppress_Checks
9570 and then Ghost_Mode_Of (Inst_Rep) /= Is_Ignored
9571 and then Ghost_Mode_Of (Gen_Rep) /= Is_Ignored
9572 and then Elaboration_Checks_OK (Inst_Rep)
9573 and then Elaboration_Checks_OK (Gen_Rep);
9574 -- A run-time ABE check may be installed only when both the instance
9575 -- and the generic have active elaboration checks and both are not
9576 -- ignored Ghost constructs.
9577
9578 begin
9579 -- Nothing to do when the root scenario appears at the declaration
9580 -- level and the generic is in the same unit, but outside this
9581 -- context.
9582 --
9583 -- generic
9584 -- procedure Gen is ...; -- generic declaration
9585 --
9586 -- procedure Proc is
9587 -- function A ... is
9588 -- begin
9589 -- if Some_Condition then
9590 -- declare
9591 -- procedure I is new Gen; -- instantiation site
9592 -- ...
9593 -- ...
9594 -- end A;
9595 --
9596 -- X : ... := A; -- root scenario
9597 -- ...
9598 --
9599 -- procedure Gen is
9600 -- ...
9601 -- end Gen;
9602 --
9603 -- In the example above, the context of X is the declarative region
9604 -- of Proc. The "elaboration" of X may eventually reach Gen which
9605 -- appears outside of X's context. Gen is relevant only when Proc is
9606 -- invoked, but this happens only by means of "normal" elaboration,
9607 -- therefore Gen must not be considered if this is not the case.
9608
9609 if Is_Up_Level_Target
9610 (Targ_Decl => Spec_Decl,
9611 In_State => In_State)
9612 then
9613 return;
9614
9615 -- Nothing to do when the instantiation is ABE-safe
9616 --
9617 -- generic
9618 -- package Gen is
9619 -- ...
9620 -- end Gen;
9621 --
9622 -- package body Gen is
9623 -- ...
9624 -- end Gen;
9625 --
9626 -- with Gen;
9627 -- procedure Main is
9628 -- package Inst is new Gen (ABE); -- safe instantiation
9629 -- ...
9630
9631 elsif Is_Safe_Instantiation (Inst, Gen_Id, Gen_Rep) then
9632 return;
9633
9634 -- An instantiation leads to a guaranteed ABE when the instantiation
9635 -- and the generic appear within the same context ignoring library
9636 -- levels, and the body of the generic has not been seen yet or
9637 -- appears after the instantiation.
9638 --
9639 -- procedure Guaranteed_ABE is
9640 -- generic
9641 -- procedure Gen;
9642 --
9643 -- package Nested is
9644 -- procedure Inst is new Gen; -- guaranteed ABE
9645 -- end Nested;
9646 --
9647 -- procedure Gen is
9648 -- ...
9649 -- end Gen;
9650 -- ...
9651
9652 elsif Is_Guaranteed_ABE
9653 (N => Inst,
9654 Target_Decl => Spec_Decl,
9655 Target_Body => Body_Declaration (Gen_Rep))
9656 then
9657 if Elaboration_Warnings_OK (Inst_Rep) then
9658 Error_Msg_NE
9659 ("??cannot instantiate & before body seen", Inst, Gen_Id);
9660 Error_Msg_N ("\Program_Error will be raised at run time", Inst);
9661 end if;
9662
9663 -- Mark the instantiation as a guarantee ABE. This automatically
9664 -- suppresses the instantiation of the generic body.
9665
9666 Set_Is_Known_Guaranteed_ABE (Inst);
9667
9668 -- Install a run-time ABE failure because the instantiation will
9669 -- always result in an ABE.
9670
9671 if Check_OK then
9672 Install_Scenario_ABE_Failure
9673 (N => Inst,
9674 Targ_Id => Gen_Id,
9675 Targ_Rep => Gen_Rep,
9676 Disable => Inst_Rep);
9677 end if;
9678 end if;
9679 end Process_Guaranteed_ABE_Instantiation;
9680 end Guaranteed_ABE_Processor;
9681
9682 --------------
9683 -- Has_Body --
9684 --------------
9685
9686 function Has_Body (Pack_Decl : Node_Id) return Boolean is
9687 function Find_Corresponding_Body (Spec_Id : Entity_Id) return Node_Id;
9688 pragma Inline (Find_Corresponding_Body);
9689 -- Try to locate the corresponding body of spec Spec_Id. If no body is
9690 -- found, return Empty.
9691
9692 function Find_Body
9693 (Spec_Id : Entity_Id;
9694 From : Node_Id) return Node_Id;
9695 pragma Inline (Find_Body);
9696 -- Try to locate the corresponding body of spec Spec_Id in the node list
9697 -- which follows arbitrary node From. If no body is found, return Empty.
9698
9699 function Load_Package_Body (Unit_Nam : Unit_Name_Type) return Node_Id;
9700 pragma Inline (Load_Package_Body);
9701 -- Attempt to load the body of unit Unit_Nam. If the load failed, return
9702 -- Empty. If the compilation will not generate code, return Empty.
9703
9704 -----------------------------
9705 -- Find_Corresponding_Body --
9706 -----------------------------
9707
9708 function Find_Corresponding_Body (Spec_Id : Entity_Id) return Node_Id is
9709 Context : constant Entity_Id := Scope (Spec_Id);
9710 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
9711 Body_Decl : Node_Id;
9712 Body_Id : Entity_Id;
9713
9714 begin
9715 if Is_Compilation_Unit (Spec_Id) then
9716 Body_Id := Corresponding_Body (Spec_Decl);
9717
9718 if Present (Body_Id) then
9719 return Unit_Declaration_Node (Body_Id);
9720
9721 -- The package is at the library and requires a body. Load the
9722 -- corresponding body because the optional body may be declared
9723 -- there.
9724
9725 elsif Unit_Requires_Body (Spec_Id) then
9726 return
9727 Load_Package_Body
9728 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec_Decl))));
9729
9730 -- Otherwise there is no optional body
9731
9732 else
9733 return Empty;
9734 end if;
9735
9736 -- The immediate context is a package. The optional body may be
9737 -- within the body of that package.
9738
9739 -- procedure Proc is
9740 -- package Nested_1 is
9741 -- package Nested_2 is
9742 -- generic
9743 -- package Pack is
9744 -- end Pack;
9745 -- end Nested_2;
9746 -- end Nested_1;
9747
9748 -- package body Nested_1 is
9749 -- package body Nested_2 is separate;
9750 -- end Nested_1;
9751
9752 -- separate (Proc.Nested_1.Nested_2)
9753 -- package body Nested_2 is
9754 -- package body Pack is -- optional body
9755 -- ...
9756 -- end Pack;
9757 -- end Nested_2;
9758
9759 elsif Is_Package_Or_Generic_Package (Context) then
9760 Body_Decl := Find_Corresponding_Body (Context);
9761
9762 -- The optional body is within the body of the enclosing package
9763
9764 if Present (Body_Decl) then
9765 return
9766 Find_Body
9767 (Spec_Id => Spec_Id,
9768 From => First (Declarations (Body_Decl)));
9769
9770 -- Otherwise the enclosing package does not have a body. This may
9771 -- be the result of an error or a genuine lack of a body.
9772
9773 else
9774 return Empty;
9775 end if;
9776
9777 -- Otherwise the immediate context is a body. The optional body may
9778 -- be within the same list as the spec.
9779
9780 -- procedure Proc is
9781 -- generic
9782 -- package Pack is
9783 -- end Pack;
9784
9785 -- package body Pack is -- optional body
9786 -- ...
9787 -- end Pack;
9788
9789 else
9790 return
9791 Find_Body
9792 (Spec_Id => Spec_Id,
9793 From => Next (Spec_Decl));
9794 end if;
9795 end Find_Corresponding_Body;
9796
9797 ---------------
9798 -- Find_Body --
9799 ---------------
9800
9801 function Find_Body
9802 (Spec_Id : Entity_Id;
9803 From : Node_Id) return Node_Id
9804 is
9805 Spec_Nam : constant Name_Id := Chars (Spec_Id);
9806 Item : Node_Id;
9807 Lib_Unit : Node_Id;
9808
9809 begin
9810 Item := From;
9811 while Present (Item) loop
9812
9813 -- The current item denotes the optional body
9814
9815 if Nkind (Item) = N_Package_Body
9816 and then Chars (Defining_Entity (Item)) = Spec_Nam
9817 then
9818 return Item;
9819
9820 -- The current item denotes a stub, the optional body may be in
9821 -- the subunit.
9822
9823 elsif Nkind (Item) = N_Package_Body_Stub
9824 and then Chars (Defining_Entity (Item)) = Spec_Nam
9825 then
9826 Lib_Unit := Library_Unit (Item);
9827
9828 -- The corresponding subunit was previously loaded
9829
9830 if Present (Lib_Unit) then
9831 return Lib_Unit;
9832
9833 -- Otherwise attempt to load the corresponding subunit
9834
9835 else
9836 return Load_Package_Body (Get_Unit_Name (Item));
9837 end if;
9838 end if;
9839
9840 Next (Item);
9841 end loop;
9842
9843 return Empty;
9844 end Find_Body;
9845
9846 -----------------------
9847 -- Load_Package_Body --
9848 -----------------------
9849
9850 function Load_Package_Body (Unit_Nam : Unit_Name_Type) return Node_Id is
9851 Body_Decl : Node_Id;
9852 Unit_Num : Unit_Number_Type;
9853
9854 begin
9855 -- The load is performed only when the compilation will generate code
9856
9857 if Operating_Mode = Generate_Code then
9858 Unit_Num :=
9859 Load_Unit
9860 (Load_Name => Unit_Nam,
9861 Required => False,
9862 Subunit => False,
9863 Error_Node => Pack_Decl);
9864
9865 -- The load failed most likely because the physical file is
9866 -- missing.
9867
9868 if Unit_Num = No_Unit then
9869 return Empty;
9870
9871 -- Otherwise the load was successful, return the body of the unit
9872
9873 else
9874 Body_Decl := Unit (Cunit (Unit_Num));
9875
9876 -- If the unit is a subunit with an available proper body,
9877 -- return the proper body.
9878
9879 if Nkind (Body_Decl) = N_Subunit
9880 and then Present (Proper_Body (Body_Decl))
9881 then
9882 Body_Decl := Proper_Body (Body_Decl);
9883 end if;
9884
9885 return Body_Decl;
9886 end if;
9887 end if;
9888
9889 return Empty;
9890 end Load_Package_Body;
9891
9892 -- Local variables
9893
9894 Pack_Id : constant Entity_Id := Defining_Entity (Pack_Decl);
9895
9896 -- Start of processing for Has_Body
9897
9898 begin
9899 -- The body is available
9900
9901 if Present (Corresponding_Body (Pack_Decl)) then
9902 return True;
9903
9904 -- The body is required if the package spec contains a construct which
9905 -- requires a completion in a body.
9906
9907 elsif Unit_Requires_Body (Pack_Id) then
9908 return True;
9909
9910 -- The body may be optional
9911
9912 else
9913 return Present (Find_Corresponding_Body (Pack_Id));
9914 end if;
9915 end Has_Body;
9916
9917 ----------
9918 -- Hash --
9919 ----------
9920
9921 function Hash (NE : Node_Or_Entity_Id) return Bucket_Range_Type is
9922 pragma Assert (Present (NE));
9923 begin
9924 return Bucket_Range_Type (NE);
9925 end Hash;
9926
9927 --------------------------
9928 -- In_External_Instance --
9929 --------------------------
9930
9931 function In_External_Instance
9932 (N : Node_Id;
9933 Target_Decl : Node_Id) return Boolean
9934 is
9935 Inst : Node_Id;
9936 Inst_Body : Node_Id;
9937 Inst_Spec : Node_Id;
9938
9939 begin
9940 Inst := Find_Enclosing_Instance (Target_Decl);
9941
9942 -- The target declaration appears within an instance spec. Visibility is
9943 -- ignored because internally generated primitives for private types may
9944 -- reside in the private declarations and still be invoked from outside.
9945
9946 if Present (Inst) and then Nkind (Inst) = N_Package_Declaration then
9947
9948 -- The scenario comes from the main unit and the instance does not
9949
9950 if In_Extended_Main_Code_Unit (N)
9951 and then not In_Extended_Main_Code_Unit (Inst)
9952 then
9953 return True;
9954
9955 -- Otherwise the scenario must not appear within the instance spec or
9956 -- body.
9957
9958 else
9959 Spec_And_Body_From_Node
9960 (N => Inst,
9961 Spec_Decl => Inst_Spec,
9962 Body_Decl => Inst_Body);
9963
9964 return not In_Subtree
9965 (N => N,
9966 Root1 => Inst_Spec,
9967 Root2 => Inst_Body);
9968 end if;
9969 end if;
9970
9971 return False;
9972 end In_External_Instance;
9973
9974 ---------------------
9975 -- In_Main_Context --
9976 ---------------------
9977
9978 function In_Main_Context (N : Node_Id) return Boolean is
9979 begin
9980 -- Scenarios outside the main unit are not considered because the ALI
9981 -- information supplied to binde is for the main unit only.
9982
9983 if not In_Extended_Main_Code_Unit (N) then
9984 return False;
9985
9986 -- Scenarios within internal units are not considered unless switch
9987 -- -gnatdE (elaboration checks on predefined units) is in effect.
9988
9989 elsif not Debug_Flag_EE and then In_Internal_Unit (N) then
9990 return False;
9991 end if;
9992
9993 return True;
9994 end In_Main_Context;
9995
9996 ---------------------
9997 -- In_Same_Context --
9998 ---------------------
9999
10000 function In_Same_Context
10001 (N1 : Node_Id;
10002 N2 : Node_Id;
10003 Nested_OK : Boolean := False) return Boolean
10004 is
10005 function Find_Enclosing_Context (N : Node_Id) return Node_Id;
10006 pragma Inline (Find_Enclosing_Context);
10007 -- Return the nearest enclosing non-library-level or compilation unit
10008 -- node which which encapsulates arbitrary node N. Return Empty is no
10009 -- such context is available.
10010
10011 function In_Nested_Context
10012 (Outer : Node_Id;
10013 Inner : Node_Id) return Boolean;
10014 pragma Inline (In_Nested_Context);
10015 -- Determine whether arbitrary node Outer encapsulates arbitrary node
10016 -- Inner.
10017
10018 ----------------------------
10019 -- Find_Enclosing_Context --
10020 ----------------------------
10021
10022 function Find_Enclosing_Context (N : Node_Id) return Node_Id is
10023 Context : Node_Id;
10024 Par : Node_Id;
10025
10026 begin
10027 Par := Parent (N);
10028 while Present (Par) loop
10029
10030 -- A traversal from a subunit continues via the corresponding stub
10031
10032 if Nkind (Par) = N_Subunit then
10033 Par := Corresponding_Stub (Par);
10034
10035 -- Stop the traversal when the nearest enclosing non-library-level
10036 -- encapsulator has been reached.
10037
10038 elsif Is_Non_Library_Level_Encapsulator (Par) then
10039 Context := Parent (Par);
10040
10041 -- The sole exception is when the encapsulator is the unit of
10042 -- compilation because this case requires special processing
10043 -- (see below).
10044
10045 if Present (Context)
10046 and then Nkind (Context) = N_Compilation_Unit
10047 then
10048 null;
10049
10050 else
10051 return Par;
10052 end if;
10053
10054 -- Reaching a compilation unit node without hitting a non-library-
10055 -- level encapsulator indicates that N is at the library level in
10056 -- which case the compilation unit is the context.
10057
10058 elsif Nkind (Par) = N_Compilation_Unit then
10059 return Par;
10060 end if;
10061
10062 Par := Parent (Par);
10063 end loop;
10064
10065 return Empty;
10066 end Find_Enclosing_Context;
10067
10068 -----------------------
10069 -- In_Nested_Context --
10070 -----------------------
10071
10072 function In_Nested_Context
10073 (Outer : Node_Id;
10074 Inner : Node_Id) return Boolean
10075 is
10076 Par : Node_Id;
10077
10078 begin
10079 Par := Inner;
10080 while Present (Par) loop
10081
10082 -- A traversal from a subunit continues via the corresponding stub
10083
10084 if Nkind (Par) = N_Subunit then
10085 Par := Corresponding_Stub (Par);
10086
10087 elsif Par = Outer then
10088 return True;
10089 end if;
10090
10091 Par := Parent (Par);
10092 end loop;
10093
10094 return False;
10095 end In_Nested_Context;
10096
10097 -- Local variables
10098
10099 Context_1 : constant Node_Id := Find_Enclosing_Context (N1);
10100 Context_2 : constant Node_Id := Find_Enclosing_Context (N2);
10101
10102 -- Start of processing for In_Same_Context
10103
10104 begin
10105 -- Both nodes appear within the same context
10106
10107 if Context_1 = Context_2 then
10108 return True;
10109
10110 -- Both nodes appear in compilation units. Determine whether one unit
10111 -- is the body of the other.
10112
10113 elsif Nkind (Context_1) = N_Compilation_Unit
10114 and then Nkind (Context_2) = N_Compilation_Unit
10115 then
10116 return
10117 Is_Same_Unit
10118 (Unit_1 => Defining_Entity (Unit (Context_1)),
10119 Unit_2 => Defining_Entity (Unit (Context_2)));
10120
10121 -- The context of N1 encloses the context of N2
10122
10123 elsif Nested_OK and then In_Nested_Context (Context_1, Context_2) then
10124 return True;
10125 end if;
10126
10127 return False;
10128 end In_Same_Context;
10129
10130 ----------------
10131 -- Initialize --
10132 ----------------
10133
10134 procedure Initialize is
10135 begin
10136 -- Set the soft link which enables Atree.Rewrite to update a scenario
10137 -- each time it is transformed into another node.
10138
10139 Set_Rewriting_Proc (Update_Elaboration_Scenario'Access);
10140
10141 -- Create all internal data structures and activate the elaboration
10142 -- phase of the compiler.
10143
10144 Initialize_All_Data_Structures;
10145 Set_Elaboration_Phase (Active);
10146 end Initialize;
10147
10148 ------------------------------------
10149 -- Initialize_All_Data_Structures --
10150 ------------------------------------
10151
10152 procedure Initialize_All_Data_Structures is
10153 begin
10154 Initialize_Body_Processor;
10155 Initialize_Early_Call_Region_Processor;
10156 Initialize_Elaborated_Units;
10157 Initialize_Internal_Representation;
10158 Initialize_Invocation_Graph;
10159 Initialize_Scenario_Storage;
10160 end Initialize_All_Data_Structures;
10161
10162 --------------------------
10163 -- Instantiated_Generic --
10164 --------------------------
10165
10166 function Instantiated_Generic (Inst : Node_Id) return Entity_Id is
10167 begin
10168 -- Traverse a possible chain of renamings to obtain the original generic
10169 -- being instantiatied.
10170
10171 return Get_Renamed_Entity (Entity (Name (Inst)));
10172 end Instantiated_Generic;
10173
10174 -----------------------------
10175 -- Internal_Representation --
10176 -----------------------------
10177
10178 package body Internal_Representation is
10179
10180 -----------
10181 -- Types --
10182 -----------
10183
10184 -- The following type represents the contents of a scenario
10185
10186 type Scenario_Rep_Record is record
10187 Elab_Checks_OK : Boolean := False;
10188 -- The status of elaboration checks for the scenario
10189
10190 Elab_Warnings_OK : Boolean := False;
10191 -- The status of elaboration warnings for the scenario
10192
10193 GM : Extended_Ghost_Mode := Is_Checked_Or_Not_Specified;
10194 -- The Ghost mode of the scenario
10195
10196 Kind : Scenario_Kind := No_Scenario;
10197 -- The nature of the scenario
10198
10199 Level : Enclosing_Level_Kind := No_Level;
10200 -- The enclosing level where the scenario resides
10201
10202 SM : Extended_SPARK_Mode := Is_Off_Or_Not_Specified;
10203 -- The SPARK mode of the scenario
10204
10205 Target : Entity_Id := Empty;
10206 -- The target of the scenario
10207
10208 -- The following attributes are multiplexed and depend on the Kind of
10209 -- the scenario. They are mapped as follows:
10210 --
10211 -- Call_Scenario
10212 -- Is_Dispatching_Call (Flag_1)
10213 --
10214 -- Task_Activation_Scenario
10215 -- Activated_Task_Objects (List_1)
10216 -- Activated_Task_Type (Field_1)
10217 --
10218 -- Variable_Reference
10219 -- Is_Read_Reference (Flag_1)
10220
10221 Flag_1 : Boolean := False;
10222 Field_1 : Node_Or_Entity_Id := Empty;
10223 List_1 : NE_List.Doubly_Linked_List := NE_List.Nil;
10224 end record;
10225
10226 -- The following type represents the contents of a target
10227
10228 type Target_Rep_Record is record
10229 Body_Decl : Node_Id := Empty;
10230 -- The declaration of the target body
10231
10232 Elab_Checks_OK : Boolean := False;
10233 -- The status of elaboration checks for the target
10234
10235 Elab_Warnings_OK : Boolean := False;
10236 -- The status of elaboration warnings for the target
10237
10238 GM : Extended_Ghost_Mode := Is_Checked_Or_Not_Specified;
10239 -- The Ghost mode of the target
10240
10241 Kind : Target_Kind := No_Target;
10242 -- The nature of the target
10243
10244 SM : Extended_SPARK_Mode := Is_Off_Or_Not_Specified;
10245 -- The SPARK mode of the target
10246
10247 Spec_Decl : Node_Id := Empty;
10248 -- The declaration of the target spec
10249
10250 Unit : Entity_Id := Empty;
10251 -- The top unit where the target is declared
10252
10253 Version : Representation_Kind := No_Representation;
10254 -- The version of the target representation
10255
10256 -- The following attributes are multiplexed and depend on the Kind of
10257 -- the target. They are mapped as follows:
10258 --
10259 -- Subprogram_Target
10260 -- Barrier_Body_Declaration (Field_1)
10261 --
10262 -- Variable_Target
10263 -- Variable_Declaration (Field_1)
10264
10265 Field_1 : Node_Or_Entity_Id := Empty;
10266 end record;
10267
10268 ---------------------
10269 -- Data structures --
10270 ---------------------
10271
10272 procedure Destroy (T_Id : in out Target_Rep_Id);
10273 -- Destroy a target representation T_Id
10274
10275 package ETT_Map is new Dynamic_Hash_Tables
10276 (Key_Type => Entity_Id,
10277 Value_Type => Target_Rep_Id,
10278 No_Value => No_Target_Rep,
10279 Expansion_Threshold => 1.5,
10280 Expansion_Factor => 2,
10281 Compression_Threshold => 0.3,
10282 Compression_Factor => 2,
10283 "=" => "=",
10284 Destroy_Value => Destroy,
10285 Hash => Hash);
10286
10287 -- The following map relates target representations to entities
10288
10289 Entity_To_Target_Map : ETT_Map.Dynamic_Hash_Table := ETT_Map.Nil;
10290
10291 procedure Destroy (S_Id : in out Scenario_Rep_Id);
10292 -- Destroy a scenario representation S_Id
10293
10294 package NTS_Map is new Dynamic_Hash_Tables
10295 (Key_Type => Node_Id,
10296 Value_Type => Scenario_Rep_Id,
10297 No_Value => No_Scenario_Rep,
10298 Expansion_Threshold => 1.5,
10299 Expansion_Factor => 2,
10300 Compression_Threshold => 0.3,
10301 Compression_Factor => 2,
10302 "=" => "=",
10303 Destroy_Value => Destroy,
10304 Hash => Hash);
10305
10306 -- The following map relates scenario representations to nodes
10307
10308 Node_To_Scenario_Map : NTS_Map.Dynamic_Hash_Table := NTS_Map.Nil;
10309
10310 -- The following table stores all scenario representations
10311
10312 package Scenario_Reps is new Table.Table
10313 (Table_Index_Type => Scenario_Rep_Id,
10314 Table_Component_Type => Scenario_Rep_Record,
10315 Table_Low_Bound => First_Scenario_Rep,
10316 Table_Initial => 1000,
10317 Table_Increment => 200,
10318 Table_Name => "Scenario_Reps");
10319
10320 -- The following table stores all target representations
10321
10322 package Target_Reps is new Table.Table
10323 (Table_Index_Type => Target_Rep_Id,
10324 Table_Component_Type => Target_Rep_Record,
10325 Table_Low_Bound => First_Target_Rep,
10326 Table_Initial => 1000,
10327 Table_Increment => 200,
10328 Table_Name => "Target_Reps");
10329
10330 --------------
10331 -- Builders --
10332 --------------
10333
10334 function Create_Access_Taken_Rep
10335 (Attr : Node_Id) return Scenario_Rep_Record;
10336 pragma Inline (Create_Access_Taken_Rep);
10337 -- Create the representation of 'Access attribute Attr
10338
10339 function Create_Call_Or_Task_Activation_Rep
10340 (Call : Node_Id) return Scenario_Rep_Record;
10341 pragma Inline (Create_Call_Or_Task_Activation_Rep);
10342 -- Create the representation of call or task activation Call
10343
10344 function Create_Derived_Type_Rep
10345 (Typ_Decl : Node_Id) return Scenario_Rep_Record;
10346 pragma Inline (Create_Derived_Type_Rep);
10347 -- Create the representation of a derived type described by declaration
10348 -- Typ_Decl.
10349
10350 function Create_Generic_Rep
10351 (Gen_Id : Entity_Id) return Target_Rep_Record;
10352 pragma Inline (Create_Generic_Rep);
10353 -- Create the representation of generic Gen_Id
10354
10355 function Create_Instantiation_Rep
10356 (Inst : Node_Id) return Scenario_Rep_Record;
10357 pragma Inline (Create_Instantiation_Rep);
10358 -- Create the representation of instantiation Inst
10359
10360 function Create_Package_Rep
10361 (Pack_Id : Entity_Id) return Target_Rep_Record;
10362 pragma Inline (Create_Package_Rep);
10363 -- Create the representation of package Pack_Id
10364
10365 function Create_Protected_Entry_Rep
10366 (PE_Id : Entity_Id) return Target_Rep_Record;
10367 pragma Inline (Create_Protected_Entry_Rep);
10368 -- Create the representation of protected entry PE_Id
10369
10370 function Create_Protected_Subprogram_Rep
10371 (PS_Id : Entity_Id) return Target_Rep_Record;
10372 pragma Inline (Create_Protected_Subprogram_Rep);
10373 -- Create the representation of protected subprogram PS_Id
10374
10375 function Create_Refined_State_Pragma_Rep
10376 (Prag : Node_Id) return Scenario_Rep_Record;
10377 pragma Inline (Create_Refined_State_Pragma_Rep);
10378 -- Create the representation of Refined_State pragma Prag
10379
10380 function Create_Scenario_Rep
10381 (N : Node_Id;
10382 In_State : Processing_In_State) return Scenario_Rep_Record;
10383 pragma Inline (Create_Scenario_Rep);
10384 -- Top level dispatcher. Create the representation of elaboration
10385 -- scenario N. In_State is the current state of the Processing phase.
10386
10387 function Create_Subprogram_Rep
10388 (Subp_Id : Entity_Id) return Target_Rep_Record;
10389 pragma Inline (Create_Subprogram_Rep);
10390 -- Create the representation of entry, operator, or subprogram Subp_Id
10391
10392 function Create_Target_Rep
10393 (Id : Entity_Id;
10394 In_State : Processing_In_State) return Target_Rep_Record;
10395 pragma Inline (Create_Target_Rep);
10396 -- Top level dispatcher. Create the representation of elaboration target
10397 -- Id. In_State is the current state of the Processing phase.
10398
10399 function Create_Task_Entry_Rep
10400 (TE_Id : Entity_Id) return Target_Rep_Record;
10401 pragma Inline (Create_Task_Entry_Rep);
10402 -- Create the representation of task entry TE_Id
10403
10404 function Create_Task_Rep (Task_Typ : Entity_Id) return Target_Rep_Record;
10405 pragma Inline (Create_Task_Rep);
10406 -- Create the representation of task type Typ
10407
10408 function Create_Variable_Assignment_Rep
10409 (Asmt : Node_Id) return Scenario_Rep_Record;
10410 pragma Inline (Create_Variable_Assignment_Rep);
10411 -- Create the representation of variable assignment Asmt
10412
10413 function Create_Variable_Reference_Rep
10414 (Ref : Node_Id) return Scenario_Rep_Record;
10415 pragma Inline (Create_Variable_Reference_Rep);
10416 -- Create the representation of variable reference Ref
10417
10418 function Create_Variable_Rep
10419 (Var_Id : Entity_Id) return Target_Rep_Record;
10420 pragma Inline (Create_Variable_Rep);
10421 -- Create the representation of variable Var_Id
10422
10423 -----------------------
10424 -- Local subprograms --
10425 -----------------------
10426
10427 function Ghost_Mode_Of_Entity
10428 (Id : Entity_Id) return Extended_Ghost_Mode;
10429 pragma Inline (Ghost_Mode_Of_Entity);
10430 -- Obtain the extended Ghost mode of arbitrary entity Id
10431
10432 function Ghost_Mode_Of_Node (N : Node_Id) return Extended_Ghost_Mode;
10433 pragma Inline (Ghost_Mode_Of_Node);
10434 -- Obtain the extended Ghost mode of arbitrary node N
10435
10436 function Present (S_Id : Scenario_Rep_Id) return Boolean;
10437 pragma Inline (Present);
10438 -- Determine whether scenario representation S_Id exists
10439
10440 function Present (T_Id : Target_Rep_Id) return Boolean;
10441 pragma Inline (Present);
10442 -- Determine whether target representation T_Id exists
10443
10444 function SPARK_Mode_Of_Entity
10445 (Id : Entity_Id) return Extended_SPARK_Mode;
10446 pragma Inline (SPARK_Mode_Of_Entity);
10447 -- Obtain the extended SPARK mode of arbitrary entity Id
10448
10449 function SPARK_Mode_Of_Node (N : Node_Id) return Extended_SPARK_Mode;
10450 pragma Inline (SPARK_Mode_Of_Node);
10451 -- Obtain the extended SPARK mode of arbitrary node N
10452
10453 function To_Ghost_Mode
10454 (Ignored_Status : Boolean) return Extended_Ghost_Mode;
10455 pragma Inline (To_Ghost_Mode);
10456 -- Convert a Ghost mode indicated by Ignored_Status into its extended
10457 -- equivalent.
10458
10459 function To_SPARK_Mode (On_Status : Boolean) return Extended_SPARK_Mode;
10460 pragma Inline (To_SPARK_Mode);
10461 -- Convert a SPARK mode indicated by On_Status into its extended
10462 -- equivalent.
10463
10464 function Version (T_Id : Target_Rep_Id) return Representation_Kind;
10465 pragma Inline (Version);
10466 -- Obtain the version of target representation T_Id
10467
10468 ----------------------------
10469 -- Activated_Task_Objects --
10470 ----------------------------
10471
10472 function Activated_Task_Objects
10473 (S_Id : Scenario_Rep_Id) return NE_List.Doubly_Linked_List
10474 is
10475 pragma Assert (Present (S_Id));
10476 pragma Assert (Kind (S_Id) = Task_Activation_Scenario);
10477
10478 begin
10479 return Scenario_Reps.Table (S_Id).List_1;
10480 end Activated_Task_Objects;
10481
10482 -------------------------
10483 -- Activated_Task_Type --
10484 -------------------------
10485
10486 function Activated_Task_Type
10487 (S_Id : Scenario_Rep_Id) return Entity_Id
10488 is
10489 pragma Assert (Present (S_Id));
10490 pragma Assert (Kind (S_Id) = Task_Activation_Scenario);
10491
10492 begin
10493 return Scenario_Reps.Table (S_Id).Field_1;
10494 end Activated_Task_Type;
10495
10496 ------------------------------
10497 -- Barrier_Body_Declaration --
10498 ------------------------------
10499
10500 function Barrier_Body_Declaration
10501 (T_Id : Target_Rep_Id) return Node_Id
10502 is
10503 pragma Assert (Present (T_Id));
10504 pragma Assert (Kind (T_Id) = Subprogram_Target);
10505
10506 begin
10507 return Target_Reps.Table (T_Id).Field_1;
10508 end Barrier_Body_Declaration;
10509
10510 ----------------------
10511 -- Body_Declaration --
10512 ----------------------
10513
10514 function Body_Declaration (T_Id : Target_Rep_Id) return Node_Id is
10515 pragma Assert (Present (T_Id));
10516 begin
10517 return Target_Reps.Table (T_Id).Body_Decl;
10518 end Body_Declaration;
10519
10520 -----------------------------
10521 -- Create_Access_Taken_Rep --
10522 -----------------------------
10523
10524 function Create_Access_Taken_Rep
10525 (Attr : Node_Id) return Scenario_Rep_Record
10526 is
10527 Rec : Scenario_Rep_Record;
10528
10529 begin
10530 Rec.Elab_Checks_OK := Is_Elaboration_Checks_OK_Node (Attr);
10531 Rec.Elab_Warnings_OK := Is_Elaboration_Warnings_OK_Node (Attr);
10532 Rec.GM := Is_Checked_Or_Not_Specified;
10533 Rec.SM := SPARK_Mode_Of_Node (Attr);
10534 Rec.Kind := Access_Taken_Scenario;
10535 Rec.Target := Canonical_Subprogram (Entity (Prefix (Attr)));
10536
10537 return Rec;
10538 end Create_Access_Taken_Rep;
10539
10540 ----------------------------------------
10541 -- Create_Call_Or_Task_Activation_Rep --
10542 ----------------------------------------
10543
10544 function Create_Call_Or_Task_Activation_Rep
10545 (Call : Node_Id) return Scenario_Rep_Record
10546 is
10547 Subp_Id : constant Entity_Id := Canonical_Subprogram (Target (Call));
10548 Kind : Scenario_Kind;
10549 Rec : Scenario_Rep_Record;
10550
10551 begin
10552 if Is_Activation_Proc (Subp_Id) then
10553 Kind := Task_Activation_Scenario;
10554 else
10555 Kind := Call_Scenario;
10556 end if;
10557
10558 Rec.Elab_Checks_OK := Is_Elaboration_Checks_OK_Node (Call);
10559 Rec.Elab_Warnings_OK := Is_Elaboration_Warnings_OK_Node (Call);
10560 Rec.GM := Ghost_Mode_Of_Node (Call);
10561 Rec.SM := SPARK_Mode_Of_Node (Call);
10562 Rec.Kind := Kind;
10563 Rec.Target := Subp_Id;
10564
10565 -- Scenario-specific attributes
10566
10567 Rec.Flag_1 := Is_Dispatching_Call (Call); -- Dispatching_Call
10568
10569 return Rec;
10570 end Create_Call_Or_Task_Activation_Rep;
10571
10572 -----------------------------
10573 -- Create_Derived_Type_Rep --
10574 -----------------------------
10575
10576 function Create_Derived_Type_Rep
10577 (Typ_Decl : Node_Id) return Scenario_Rep_Record
10578 is
10579 Typ : constant Entity_Id := Defining_Entity (Typ_Decl);
10580 Rec : Scenario_Rep_Record;
10581
10582 begin
10583 Rec.Elab_Checks_OK := False; -- not relevant
10584 Rec.Elab_Warnings_OK := False; -- not relevant
10585 Rec.GM := Ghost_Mode_Of_Entity (Typ);
10586 Rec.SM := SPARK_Mode_Of_Entity (Typ);
10587 Rec.Kind := Derived_Type_Scenario;
10588 Rec.Target := Typ;
10589
10590 return Rec;
10591 end Create_Derived_Type_Rep;
10592
10593 ------------------------
10594 -- Create_Generic_Rep --
10595 ------------------------
10596
10597 function Create_Generic_Rep
10598 (Gen_Id : Entity_Id) return Target_Rep_Record
10599 is
10600 Rec : Target_Rep_Record;
10601
10602 begin
10603 Rec.Kind := Generic_Target;
10604
10605 Spec_And_Body_From_Entity
10606 (Id => Gen_Id,
10607 Body_Decl => Rec.Body_Decl,
10608 Spec_Decl => Rec.Spec_Decl);
10609
10610 return Rec;
10611 end Create_Generic_Rep;
10612
10613 ------------------------------
10614 -- Create_Instantiation_Rep --
10615 ------------------------------
10616
10617 function Create_Instantiation_Rep
10618 (Inst : Node_Id) return Scenario_Rep_Record
10619 is
10620 Rec : Scenario_Rep_Record;
10621
10622 begin
10623 Rec.Elab_Checks_OK := Is_Elaboration_Checks_OK_Node (Inst);
10624 Rec.Elab_Warnings_OK := Is_Elaboration_Warnings_OK_Node (Inst);
10625 Rec.GM := Ghost_Mode_Of_Node (Inst);
10626 Rec.SM := SPARK_Mode_Of_Node (Inst);
10627 Rec.Kind := Instantiation_Scenario;
10628 Rec.Target := Instantiated_Generic (Inst);
10629
10630 return Rec;
10631 end Create_Instantiation_Rep;
10632
10633 ------------------------
10634 -- Create_Package_Rep --
10635 ------------------------
10636
10637 function Create_Package_Rep
10638 (Pack_Id : Entity_Id) return Target_Rep_Record
10639 is
10640 Rec : Target_Rep_Record;
10641
10642 begin
10643 Rec.Kind := Package_Target;
10644
10645 Spec_And_Body_From_Entity
10646 (Id => Pack_Id,
10647 Body_Decl => Rec.Body_Decl,
10648 Spec_Decl => Rec.Spec_Decl);
10649
10650 return Rec;
10651 end Create_Package_Rep;
10652
10653 --------------------------------
10654 -- Create_Protected_Entry_Rep --
10655 --------------------------------
10656
10657 function Create_Protected_Entry_Rep
10658 (PE_Id : Entity_Id) return Target_Rep_Record
10659 is
10660 Prot_Id : constant Entity_Id := Protected_Body_Subprogram (PE_Id);
10661
10662 Barf_Id : Entity_Id;
10663 Dummy : Node_Id;
10664 Rec : Target_Rep_Record;
10665 Spec_Id : Entity_Id;
10666
10667 begin
10668 -- When the entry [family] has already been expanded, it carries both
10669 -- the procedure which emulates the behavior of the entry [family] as
10670 -- well as the barrier function.
10671
10672 if Present (Prot_Id) then
10673 Barf_Id := Barrier_Function (PE_Id);
10674 Spec_Id := Prot_Id;
10675
10676 -- Otherwise no expansion took place
10677
10678 else
10679 Barf_Id := Empty;
10680 Spec_Id := PE_Id;
10681 end if;
10682
10683 Rec.Kind := Subprogram_Target;
10684
10685 Spec_And_Body_From_Entity
10686 (Id => Spec_Id,
10687 Body_Decl => Rec.Body_Decl,
10688 Spec_Decl => Rec.Spec_Decl);
10689
10690 -- Target-specific attributes
10691
10692 if Present (Barf_Id) then
10693 Spec_And_Body_From_Entity
10694 (Id => Barf_Id,
10695 Body_Decl => Rec.Field_1, -- Barrier_Body_Declaration
10696 Spec_Decl => Dummy);
10697 end if;
10698
10699 return Rec;
10700 end Create_Protected_Entry_Rep;
10701
10702 -------------------------------------
10703 -- Create_Protected_Subprogram_Rep --
10704 -------------------------------------
10705
10706 function Create_Protected_Subprogram_Rep
10707 (PS_Id : Entity_Id) return Target_Rep_Record
10708 is
10709 Prot_Id : constant Entity_Id := Protected_Body_Subprogram (PS_Id);
10710 Rec : Target_Rep_Record;
10711 Spec_Id : Entity_Id;
10712
10713 begin
10714 -- When the protected subprogram has already been expanded, it
10715 -- carries the subprogram which seizes the lock and invokes the
10716 -- original statements.
10717
10718 if Present (Prot_Id) then
10719 Spec_Id := Prot_Id;
10720
10721 -- Otherwise no expansion took place
10722
10723 else
10724 Spec_Id := PS_Id;
10725 end if;
10726
10727 Rec.Kind := Subprogram_Target;
10728
10729 Spec_And_Body_From_Entity
10730 (Id => Spec_Id,
10731 Body_Decl => Rec.Body_Decl,
10732 Spec_Decl => Rec.Spec_Decl);
10733
10734 return Rec;
10735 end Create_Protected_Subprogram_Rep;
10736
10737 -------------------------------------
10738 -- Create_Refined_State_Pragma_Rep --
10739 -------------------------------------
10740
10741 function Create_Refined_State_Pragma_Rep
10742 (Prag : Node_Id) return Scenario_Rep_Record
10743 is
10744 Rec : Scenario_Rep_Record;
10745
10746 begin
10747 Rec.Elab_Checks_OK := False; -- not relevant
10748 Rec.Elab_Warnings_OK := False; -- not relevant
10749 Rec.GM :=
10750 To_Ghost_Mode (Is_Ignored_Ghost_Pragma (Prag));
10751 Rec.SM := Is_Off_Or_Not_Specified;
10752 Rec.Kind := Refined_State_Pragma_Scenario;
10753 Rec.Target := Empty;
10754
10755 return Rec;
10756 end Create_Refined_State_Pragma_Rep;
10757
10758 -------------------------
10759 -- Create_Scenario_Rep --
10760 -------------------------
10761
10762 function Create_Scenario_Rep
10763 (N : Node_Id;
10764 In_State : Processing_In_State) return Scenario_Rep_Record
10765 is
10766 pragma Unreferenced (In_State);
10767
10768 Rec : Scenario_Rep_Record;
10769
10770 begin
10771 if Is_Suitable_Access_Taken (N) then
10772 Rec := Create_Access_Taken_Rep (N);
10773
10774 elsif Is_Suitable_Call (N) then
10775 Rec := Create_Call_Or_Task_Activation_Rep (N);
10776
10777 elsif Is_Suitable_Instantiation (N) then
10778 Rec := Create_Instantiation_Rep (N);
10779
10780 elsif Is_Suitable_SPARK_Derived_Type (N) then
10781 Rec := Create_Derived_Type_Rep (N);
10782
10783 elsif Is_Suitable_SPARK_Refined_State_Pragma (N) then
10784 Rec := Create_Refined_State_Pragma_Rep (N);
10785
10786 elsif Is_Suitable_Variable_Assignment (N) then
10787 Rec := Create_Variable_Assignment_Rep (N);
10788
10789 elsif Is_Suitable_Variable_Reference (N) then
10790 Rec := Create_Variable_Reference_Rep (N);
10791
10792 else
10793 pragma Assert (False);
10794 return Rec;
10795 end if;
10796
10797 -- Common scenario attributes
10798
10799 Rec.Level := Find_Enclosing_Level (N);
10800
10801 return Rec;
10802 end Create_Scenario_Rep;
10803
10804 ---------------------------
10805 -- Create_Subprogram_Rep --
10806 ---------------------------
10807
10808 function Create_Subprogram_Rep
10809 (Subp_Id : Entity_Id) return Target_Rep_Record
10810 is
10811 Rec : Target_Rep_Record;
10812 Spec_Id : Entity_Id;
10813
10814 begin
10815 Spec_Id := Subp_Id;
10816
10817 -- The elaboration target denotes an internal function that returns a
10818 -- constrained array type in a SPARK-to-C compilation. In this case
10819 -- the function receives a corresponding procedure which has an out
10820 -- parameter. The proper body for ABE checks and diagnostics is that
10821 -- of the procedure.
10822
10823 if Ekind (Spec_Id) = E_Function
10824 and then Rewritten_For_C (Spec_Id)
10825 then
10826 Spec_Id := Corresponding_Procedure (Spec_Id);
10827 end if;
10828
10829 Rec.Kind := Subprogram_Target;
10830
10831 Spec_And_Body_From_Entity
10832 (Id => Spec_Id,
10833 Body_Decl => Rec.Body_Decl,
10834 Spec_Decl => Rec.Spec_Decl);
10835
10836 return Rec;
10837 end Create_Subprogram_Rep;
10838
10839 -----------------------
10840 -- Create_Target_Rep --
10841 -----------------------
10842
10843 function Create_Target_Rep
10844 (Id : Entity_Id;
10845 In_State : Processing_In_State) return Target_Rep_Record
10846 is
10847 Rec : Target_Rep_Record;
10848
10849 begin
10850 if Is_Generic_Unit (Id) then
10851 Rec := Create_Generic_Rep (Id);
10852
10853 elsif Is_Protected_Entry (Id) then
10854 Rec := Create_Protected_Entry_Rep (Id);
10855
10856 elsif Is_Protected_Subp (Id) then
10857 Rec := Create_Protected_Subprogram_Rep (Id);
10858
10859 elsif Is_Task_Entry (Id) then
10860 Rec := Create_Task_Entry_Rep (Id);
10861
10862 elsif Is_Task_Type (Id) then
10863 Rec := Create_Task_Rep (Id);
10864
10865 elsif Ekind_In (Id, E_Constant, E_Variable) then
10866 Rec := Create_Variable_Rep (Id);
10867
10868 elsif Ekind_In (Id, E_Entry,
10869 E_Function,
10870 E_Operator,
10871 E_Procedure)
10872 then
10873 Rec := Create_Subprogram_Rep (Id);
10874
10875 elsif Ekind (Id) = E_Package then
10876 Rec := Create_Package_Rep (Id);
10877
10878 else
10879 pragma Assert (False);
10880 return Rec;
10881 end if;
10882
10883 -- Common target attributes
10884
10885 Rec.Elab_Checks_OK := Is_Elaboration_Checks_OK_Id (Id);
10886 Rec.Elab_Warnings_OK := Is_Elaboration_Warnings_OK_Id (Id);
10887 Rec.GM := Ghost_Mode_Of_Entity (Id);
10888 Rec.SM := SPARK_Mode_Of_Entity (Id);
10889 Rec.Unit := Find_Top_Unit (Id);
10890 Rec.Version := In_State.Representation;
10891
10892 return Rec;
10893 end Create_Target_Rep;
10894
10895 ---------------------------
10896 -- Create_Task_Entry_Rep --
10897 ---------------------------
10898
10899 function Create_Task_Entry_Rep
10900 (TE_Id : Entity_Id) return Target_Rep_Record
10901 is
10902 Task_Typ : constant Entity_Id := Non_Private_View (Scope (TE_Id));
10903 Task_Body_Id : constant Entity_Id := Task_Body_Procedure (Task_Typ);
10904
10905 Rec : Target_Rep_Record;
10906 Spec_Id : Entity_Id;
10907
10908 begin
10909 -- The the task type has already been expanded, it carries the
10910 -- procedure which emulates the behavior of the task body.
10911
10912 if Present (Task_Body_Id) then
10913 Spec_Id := Task_Body_Id;
10914
10915 -- Otherwise no expansion took place
10916
10917 else
10918 Spec_Id := TE_Id;
10919 end if;
10920
10921 Rec.Kind := Subprogram_Target;
10922
10923 Spec_And_Body_From_Entity
10924 (Id => Spec_Id,
10925 Body_Decl => Rec.Body_Decl,
10926 Spec_Decl => Rec.Spec_Decl);
10927
10928 return Rec;
10929 end Create_Task_Entry_Rep;
10930
10931 ---------------------
10932 -- Create_Task_Rep --
10933 ---------------------
10934
10935 function Create_Task_Rep
10936 (Task_Typ : Entity_Id) return Target_Rep_Record
10937 is
10938 Task_Body_Id : constant Entity_Id := Task_Body_Procedure (Task_Typ);
10939
10940 Rec : Target_Rep_Record;
10941 Spec_Id : Entity_Id;
10942
10943 begin
10944 -- The the task type has already been expanded, it carries the
10945 -- procedure which emulates the behavior of the task body.
10946
10947 if Present (Task_Body_Id) then
10948 Spec_Id := Task_Body_Id;
10949
10950 -- Otherwise no expansion took place
10951
10952 else
10953 Spec_Id := Task_Typ;
10954 end if;
10955
10956 Rec.Kind := Task_Target;
10957
10958 Spec_And_Body_From_Entity
10959 (Id => Spec_Id,
10960 Body_Decl => Rec.Body_Decl,
10961 Spec_Decl => Rec.Spec_Decl);
10962
10963 return Rec;
10964 end Create_Task_Rep;
10965
10966 ------------------------------------
10967 -- Create_Variable_Assignment_Rep --
10968 ------------------------------------
10969
10970 function Create_Variable_Assignment_Rep
10971 (Asmt : Node_Id) return Scenario_Rep_Record
10972 is
10973 Var_Id : constant Entity_Id := Entity (Assignment_Target (Asmt));
10974 Rec : Scenario_Rep_Record;
10975
10976 begin
10977 Rec.Elab_Checks_OK := Is_Elaboration_Checks_OK_Node (Asmt);
10978 Rec.Elab_Warnings_OK := Is_Elaboration_Warnings_OK_Id (Var_Id);
10979 Rec.GM := Ghost_Mode_Of_Node (Asmt);
10980 Rec.SM := SPARK_Mode_Of_Node (Asmt);
10981 Rec.Kind := Variable_Assignment_Scenario;
10982 Rec.Target := Var_Id;
10983
10984 return Rec;
10985 end Create_Variable_Assignment_Rep;
10986
10987 -----------------------------------
10988 -- Create_Variable_Reference_Rep --
10989 -----------------------------------
10990
10991 function Create_Variable_Reference_Rep
10992 (Ref : Node_Id) return Scenario_Rep_Record
10993 is
10994 Rec : Scenario_Rep_Record;
10995
10996 begin
10997 Rec.Elab_Checks_OK := Is_Elaboration_Checks_OK_Node (Ref);
10998 Rec.Elab_Warnings_OK := Is_Elaboration_Warnings_OK_Node (Ref);
10999 Rec.GM := Ghost_Mode_Of_Node (Ref);
11000 Rec.SM := SPARK_Mode_Of_Node (Ref);
11001 Rec.Kind := Variable_Reference_Scenario;
11002 Rec.Target := Target (Ref);
11003
11004 -- Scenario-specific attributes
11005
11006 Rec.Flag_1 := Is_Read (Ref); -- Is_Read_Reference
11007
11008 return Rec;
11009 end Create_Variable_Reference_Rep;
11010
11011 -------------------------
11012 -- Create_Variable_Rep --
11013 -------------------------
11014
11015 function Create_Variable_Rep
11016 (Var_Id : Entity_Id) return Target_Rep_Record
11017 is
11018 Rec : Target_Rep_Record;
11019
11020 begin
11021 Rec.Kind := Variable_Target;
11022
11023 -- Target-specific attributes
11024
11025 Rec.Field_1 := Declaration_Node (Var_Id); -- Variable_Declaration
11026
11027 return Rec;
11028 end Create_Variable_Rep;
11029
11030 -------------
11031 -- Destroy --
11032 -------------
11033
11034 procedure Destroy (S_Id : in out Scenario_Rep_Id) is
11035 pragma Unreferenced (S_Id);
11036 begin
11037 null;
11038 end Destroy;
11039
11040 -------------
11041 -- Destroy --
11042 -------------
11043
11044 procedure Destroy (T_Id : in out Target_Rep_Id) is
11045 pragma Unreferenced (T_Id);
11046 begin
11047 null;
11048 end Destroy;
11049
11050 --------------------------------
11051 -- Disable_Elaboration_Checks --
11052 --------------------------------
11053
11054 procedure Disable_Elaboration_Checks (S_Id : Scenario_Rep_Id) is
11055 pragma Assert (Present (S_Id));
11056 begin
11057 Scenario_Reps.Table (S_Id).Elab_Checks_OK := False;
11058 end Disable_Elaboration_Checks;
11059
11060 --------------------------------
11061 -- Disable_Elaboration_Checks --
11062 --------------------------------
11063
11064 procedure Disable_Elaboration_Checks (T_Id : Target_Rep_Id) is
11065 pragma Assert (Present (T_Id));
11066 begin
11067 Target_Reps.Table (T_Id).Elab_Checks_OK := False;
11068 end Disable_Elaboration_Checks;
11069
11070 ---------------------------
11071 -- Elaboration_Checks_OK --
11072 ---------------------------
11073
11074 function Elaboration_Checks_OK (S_Id : Scenario_Rep_Id) return Boolean is
11075 pragma Assert (Present (S_Id));
11076 begin
11077 return Scenario_Reps.Table (S_Id).Elab_Checks_OK;
11078 end Elaboration_Checks_OK;
11079
11080 ---------------------------
11081 -- Elaboration_Checks_OK --
11082 ---------------------------
11083
11084 function Elaboration_Checks_OK (T_Id : Target_Rep_Id) return Boolean is
11085 pragma Assert (Present (T_Id));
11086 begin
11087 return Target_Reps.Table (T_Id).Elab_Checks_OK;
11088 end Elaboration_Checks_OK;
11089
11090 -----------------------------
11091 -- Elaboration_Warnings_OK --
11092 -----------------------------
11093
11094 function Elaboration_Warnings_OK
11095 (S_Id : Scenario_Rep_Id) return Boolean
11096 is
11097 pragma Assert (Present (S_Id));
11098 begin
11099 return Scenario_Reps.Table (S_Id).Elab_Warnings_OK;
11100 end Elaboration_Warnings_OK;
11101
11102 -----------------------------
11103 -- Elaboration_Warnings_OK --
11104 -----------------------------
11105
11106 function Elaboration_Warnings_OK (T_Id : Target_Rep_Id) return Boolean is
11107 pragma Assert (Present (T_Id));
11108 begin
11109 return Target_Reps.Table (T_Id).Elab_Warnings_OK;
11110 end Elaboration_Warnings_OK;
11111
11112 --------------------------------------
11113 -- Finalize_Internal_Representation --
11114 --------------------------------------
11115
11116 procedure Finalize_Internal_Representation is
11117 begin
11118 ETT_Map.Destroy (Entity_To_Target_Map);
11119 NTS_Map.Destroy (Node_To_Scenario_Map);
11120 end Finalize_Internal_Representation;
11121
11122 -------------------
11123 -- Ghost_Mode_Of --
11124 -------------------
11125
11126 function Ghost_Mode_Of
11127 (S_Id : Scenario_Rep_Id) return Extended_Ghost_Mode
11128 is
11129 pragma Assert (Present (S_Id));
11130 begin
11131 return Scenario_Reps.Table (S_Id).GM;
11132 end Ghost_Mode_Of;
11133
11134 -------------------
11135 -- Ghost_Mode_Of --
11136 -------------------
11137
11138 function Ghost_Mode_Of
11139 (T_Id : Target_Rep_Id) return Extended_Ghost_Mode
11140 is
11141 pragma Assert (Present (T_Id));
11142 begin
11143 return Target_Reps.Table (T_Id).GM;
11144 end Ghost_Mode_Of;
11145
11146 --------------------------
11147 -- Ghost_Mode_Of_Entity --
11148 --------------------------
11149
11150 function Ghost_Mode_Of_Entity
11151 (Id : Entity_Id) return Extended_Ghost_Mode
11152 is
11153 begin
11154 return To_Ghost_Mode (Is_Ignored_Ghost_Entity (Id));
11155 end Ghost_Mode_Of_Entity;
11156
11157 ------------------------
11158 -- Ghost_Mode_Of_Node --
11159 ------------------------
11160
11161 function Ghost_Mode_Of_Node (N : Node_Id) return Extended_Ghost_Mode is
11162 begin
11163 return To_Ghost_Mode (Is_Ignored_Ghost_Node (N));
11164 end Ghost_Mode_Of_Node;
11165
11166 ----------------------------------------
11167 -- Initialize_Internal_Representation --
11168 ----------------------------------------
11169
11170 procedure Initialize_Internal_Representation is
11171 begin
11172 Entity_To_Target_Map := ETT_Map.Create (500);
11173 Node_To_Scenario_Map := NTS_Map.Create (500);
11174 end Initialize_Internal_Representation;
11175
11176 -------------------------
11177 -- Is_Dispatching_Call --
11178 -------------------------
11179
11180 function Is_Dispatching_Call (S_Id : Scenario_Rep_Id) return Boolean is
11181 pragma Assert (Present (S_Id));
11182 pragma Assert (Kind (S_Id) = Call_Scenario);
11183
11184 begin
11185 return Scenario_Reps.Table (S_Id).Flag_1;
11186 end Is_Dispatching_Call;
11187
11188 -----------------------
11189 -- Is_Read_Reference --
11190 -----------------------
11191
11192 function Is_Read_Reference (S_Id : Scenario_Rep_Id) return Boolean is
11193 pragma Assert (Present (S_Id));
11194 pragma Assert (Kind (S_Id) = Variable_Reference_Scenario);
11195
11196 begin
11197 return Scenario_Reps.Table (S_Id).Flag_1;
11198 end Is_Read_Reference;
11199
11200 ----------
11201 -- Kind --
11202 ----------
11203
11204 function Kind (S_Id : Scenario_Rep_Id) return Scenario_Kind is
11205 pragma Assert (Present (S_Id));
11206 begin
11207 return Scenario_Reps.Table (S_Id).Kind;
11208 end Kind;
11209
11210 ----------
11211 -- Kind --
11212 ----------
11213
11214 function Kind (T_Id : Target_Rep_Id) return Target_Kind is
11215 pragma Assert (Present (T_Id));
11216 begin
11217 return Target_Reps.Table (T_Id).Kind;
11218 end Kind;
11219
11220 -----------
11221 -- Level --
11222 -----------
11223
11224 function Level (S_Id : Scenario_Rep_Id) return Enclosing_Level_Kind is
11225 pragma Assert (Present (S_Id));
11226 begin
11227 return Scenario_Reps.Table (S_Id).Level;
11228 end Level;
11229
11230 -------------
11231 -- Present --
11232 -------------
11233
11234 function Present (S_Id : Scenario_Rep_Id) return Boolean is
11235 begin
11236 return S_Id /= No_Scenario_Rep;
11237 end Present;
11238
11239 -------------
11240 -- Present --
11241 -------------
11242
11243 function Present (T_Id : Target_Rep_Id) return Boolean is
11244 begin
11245 return T_Id /= No_Target_Rep;
11246 end Present;
11247
11248 --------------------------------
11249 -- Scenario_Representation_Of --
11250 --------------------------------
11251
11252 function Scenario_Representation_Of
11253 (N : Node_Id;
11254 In_State : Processing_In_State) return Scenario_Rep_Id
11255 is
11256 S_Id : Scenario_Rep_Id;
11257
11258 begin
11259 S_Id := NTS_Map.Get (Node_To_Scenario_Map, N);
11260
11261 -- The elaboration scenario lacks a representation. This indicates
11262 -- that the scenario is encountered for the first time. Create the
11263 -- representation of it.
11264
11265 if not Present (S_Id) then
11266 Scenario_Reps.Append (Create_Scenario_Rep (N, In_State));
11267 S_Id := Scenario_Reps.Last;
11268
11269 -- Associate the internal representation with the elaboration
11270 -- scenario.
11271
11272 NTS_Map.Put (Node_To_Scenario_Map, N, S_Id);
11273 end if;
11274
11275 pragma Assert (Present (S_Id));
11276
11277 return S_Id;
11278 end Scenario_Representation_Of;
11279
11280 --------------------------------
11281 -- Set_Activated_Task_Objects --
11282 --------------------------------
11283
11284 procedure Set_Activated_Task_Objects
11285 (S_Id : Scenario_Rep_Id;
11286 Task_Objs : NE_List.Doubly_Linked_List)
11287 is
11288 pragma Assert (Present (S_Id));
11289 pragma Assert (Kind (S_Id) = Task_Activation_Scenario);
11290
11291 begin
11292 Scenario_Reps.Table (S_Id).List_1 := Task_Objs;
11293 end Set_Activated_Task_Objects;
11294
11295 -----------------------------
11296 -- Set_Activated_Task_Type --
11297 -----------------------------
11298
11299 procedure Set_Activated_Task_Type
11300 (S_Id : Scenario_Rep_Id;
11301 Task_Typ : Entity_Id)
11302 is
11303 pragma Assert (Present (S_Id));
11304 pragma Assert (Kind (S_Id) = Task_Activation_Scenario);
11305
11306 begin
11307 Scenario_Reps.Table (S_Id).Field_1 := Task_Typ;
11308 end Set_Activated_Task_Type;
11309
11310 -------------------
11311 -- SPARK_Mode_Of --
11312 -------------------
11313
11314 function SPARK_Mode_Of
11315 (S_Id : Scenario_Rep_Id) return Extended_SPARK_Mode
11316 is
11317 pragma Assert (Present (S_Id));
11318 begin
11319 return Scenario_Reps.Table (S_Id).SM;
11320 end SPARK_Mode_Of;
11321
11322 -------------------
11323 -- SPARK_Mode_Of --
11324 -------------------
11325
11326 function SPARK_Mode_Of
11327 (T_Id : Target_Rep_Id) return Extended_SPARK_Mode
11328 is
11329 pragma Assert (Present (T_Id));
11330 begin
11331 return Target_Reps.Table (T_Id).SM;
11332 end SPARK_Mode_Of;
11333
11334 --------------------------
11335 -- SPARK_Mode_Of_Entity --
11336 --------------------------
11337
11338 function SPARK_Mode_Of_Entity
11339 (Id : Entity_Id) return Extended_SPARK_Mode
11340 is
11341 Prag : constant Node_Id := SPARK_Pragma (Id);
11342
11343 begin
11344 return
11345 To_SPARK_Mode
11346 (Present (Prag)
11347 and then Get_SPARK_Mode_From_Annotation (Prag) = On);
11348 end SPARK_Mode_Of_Entity;
11349
11350 ------------------------
11351 -- SPARK_Mode_Of_Node --
11352 ------------------------
11353
11354 function SPARK_Mode_Of_Node (N : Node_Id) return Extended_SPARK_Mode is
11355 begin
11356 return To_SPARK_Mode (Is_SPARK_Mode_On_Node (N));
11357 end SPARK_Mode_Of_Node;
11358
11359 ----------------------
11360 -- Spec_Declaration --
11361 ----------------------
11362
11363 function Spec_Declaration (T_Id : Target_Rep_Id) return Node_Id is
11364 pragma Assert (Present (T_Id));
11365 begin
11366 return Target_Reps.Table (T_Id).Spec_Decl;
11367 end Spec_Declaration;
11368
11369 ------------
11370 -- Target --
11371 ------------
11372
11373 function Target (S_Id : Scenario_Rep_Id) return Entity_Id is
11374 pragma Assert (Present (S_Id));
11375 begin
11376 return Scenario_Reps.Table (S_Id).Target;
11377 end Target;
11378
11379 ------------------------------
11380 -- Target_Representation_Of --
11381 ------------------------------
11382
11383 function Target_Representation_Of
11384 (Id : Entity_Id;
11385 In_State : Processing_In_State) return Target_Rep_Id
11386 is
11387 T_Id : Target_Rep_Id;
11388
11389 begin
11390 T_Id := ETT_Map.Get (Entity_To_Target_Map, Id);
11391
11392 -- The elaboration target lacks an internal representation. This
11393 -- indicates that the target is encountered for the first time.
11394 -- Create the internal representation of it.
11395
11396 if not Present (T_Id) then
11397 Target_Reps.Append (Create_Target_Rep (Id, In_State));
11398 T_Id := Target_Reps.Last;
11399
11400 -- Associate the internal representation with the elaboration
11401 -- target.
11402
11403 ETT_Map.Put (Entity_To_Target_Map, Id, T_Id);
11404
11405 -- The Processing phase is working with a partially analyzed tree,
11406 -- where various attributes become available as analysis continues.
11407 -- This case arrises in the context of guaranteed ABE processing.
11408 -- Update the existing representation by including new attributes.
11409
11410 elsif In_State.Representation = Inconsistent_Representation then
11411 Target_Reps.Table (T_Id) := Create_Target_Rep (Id, In_State);
11412
11413 -- Otherwise the Processing phase imposes a particular representation
11414 -- version which is not satisfied by the target. This case arrises
11415 -- when the Processing phase switches from guaranteed ABE checks and
11416 -- diagnostics to some other mode of operation. Update the existing
11417 -- representation to include all attributes.
11418
11419 elsif In_State.Representation /= Version (T_Id) then
11420 Target_Reps.Table (T_Id) := Create_Target_Rep (Id, In_State);
11421 end if;
11422
11423 pragma Assert (Present (T_Id));
11424
11425 return T_Id;
11426 end Target_Representation_Of;
11427
11428 -------------------
11429 -- To_Ghost_Mode --
11430 -------------------
11431
11432 function To_Ghost_Mode
11433 (Ignored_Status : Boolean) return Extended_Ghost_Mode
11434 is
11435 begin
11436 if Ignored_Status then
11437 return Is_Ignored;
11438 else
11439 return Is_Checked_Or_Not_Specified;
11440 end if;
11441 end To_Ghost_Mode;
11442
11443 -------------------
11444 -- To_SPARK_Mode --
11445 -------------------
11446
11447 function To_SPARK_Mode
11448 (On_Status : Boolean) return Extended_SPARK_Mode
11449 is
11450 begin
11451 if On_Status then
11452 return Is_On;
11453 else
11454 return Is_Off_Or_Not_Specified;
11455 end if;
11456 end To_SPARK_Mode;
11457
11458 ----------
11459 -- Unit --
11460 ----------
11461
11462 function Unit (T_Id : Target_Rep_Id) return Entity_Id is
11463 pragma Assert (Present (T_Id));
11464 begin
11465 return Target_Reps.Table (T_Id).Unit;
11466 end Unit;
11467
11468 --------------------------
11469 -- Variable_Declaration --
11470 --------------------------
11471
11472 function Variable_Declaration (T_Id : Target_Rep_Id) return Node_Id is
11473 pragma Assert (Present (T_Id));
11474 pragma Assert (Kind (T_Id) = Variable_Target);
11475
11476 begin
11477 return Target_Reps.Table (T_Id).Field_1;
11478 end Variable_Declaration;
11479
11480 -------------
11481 -- Version --
11482 -------------
11483
11484 function Version (T_Id : Target_Rep_Id) return Representation_Kind is
11485 pragma Assert (Present (T_Id));
11486 begin
11487 return Target_Reps.Table (T_Id).Version;
11488 end Version;
11489 end Internal_Representation;
11490
11491 ----------------------
11492 -- Invocation_Graph --
11493 ----------------------
11494
11495 package body Invocation_Graph is
11496
11497 -----------
11498 -- Types --
11499 -----------
11500
11501 -- The following type represents simplified version of an invocation
11502 -- relation.
11503
11504 type Invoker_Target_Relation is record
11505 Invoker : Entity_Id := Empty;
11506 Target : Entity_Id := Empty;
11507 end record;
11508
11509 -- The following variables define the entities of the dummy elaboration
11510 -- procedures used as origins of library level paths.
11511
11512 Elab_Body_Id : Entity_Id := Empty;
11513 Elab_Spec_Id : Entity_Id := Empty;
11514
11515 ---------------------
11516 -- Data structures --
11517 ---------------------
11518
11519 -- The following set contains all declared invocation constructs. It
11520 -- ensures that the same construct is not declared multiple times in
11521 -- the ALI file of the main unit.
11522
11523 Saved_Constructs_Set : NE_Set.Membership_Set := NE_Set.Nil;
11524
11525 function Hash (Key : Invoker_Target_Relation) return Bucket_Range_Type;
11526 -- Obtain the hash value of pair Key
11527
11528 package IR_Set is new Membership_Sets
11529 (Element_Type => Invoker_Target_Relation,
11530 "=" => "=",
11531 Hash => Hash);
11532
11533 -- The following set contains all recorded simple invocation relations.
11534 -- It ensures that multiple relations involving the same invoker and
11535 -- target do not appear in the ALI file of the main unit.
11536
11537 Saved_Relations_Set : IR_Set.Membership_Set := IR_Set.Nil;
11538
11539 --------------
11540 -- Builders --
11541 --------------
11542
11543 function Signature_Of (Id : Entity_Id) return Invocation_Signature_Id;
11544 pragma Inline (Signature_Of);
11545 -- Obtain the invication signature id of arbitrary entity Id
11546
11547 -----------------------
11548 -- Local subprograms --
11549 -----------------------
11550
11551 procedure Build_Elaborate_Body_Procedure;
11552 pragma Inline (Build_Elaborate_Body_Procedure);
11553 -- Create a dummy elaborate body procedure and store its entity in
11554 -- Elab_Body_Id.
11555
11556 procedure Build_Elaborate_Procedure
11557 (Proc_Id : out Entity_Id;
11558 Proc_Nam : Name_Id;
11559 Loc : Source_Ptr);
11560 pragma Inline (Build_Elaborate_Procedure);
11561 -- Create a dummy elaborate procedure with name Proc_Nam and source
11562 -- location Loc. The entity is returned in Proc_Id.
11563
11564 procedure Build_Elaborate_Spec_Procedure;
11565 pragma Inline (Build_Elaborate_Spec_Procedure);
11566 -- Create a dummy elaborate spec procedure and store its entity in
11567 -- Elab_Spec_Id.
11568
11569 function Build_Subprogram_Invocation
11570 (Subp_Id : Entity_Id) return Node_Id;
11571 pragma Inline (Build_Subprogram_Invocation);
11572 -- Create a dummy call marker that invokes subprogram Subp_Id
11573
11574 function Build_Task_Activation
11575 (Task_Typ : Entity_Id;
11576 In_State : Processing_In_State) return Node_Id;
11577 pragma Inline (Build_Task_Activation);
11578 -- Create a dummy call marker that activates an anonymous task object of
11579 -- type Task_Typ.
11580
11581 procedure Declare_Invocation_Construct
11582 (Constr_Id : Entity_Id;
11583 In_State : Processing_In_State);
11584 pragma Inline (Declare_Invocation_Construct);
11585 -- Declare invocation construct Constr_Id by creating a declaration for
11586 -- it in the ALI file of the main unit. In_State is the current state of
11587 -- the Processing phase.
11588
11589 function Invocation_Graph_Recording_OK return Boolean;
11590 pragma Inline (Invocation_Graph_Recording_OK);
11591 -- Determine whether the invocation graph can be recorded
11592
11593 function Is_Invocation_Scenario (N : Node_Id) return Boolean;
11594 pragma Inline (Is_Invocation_Scenario);
11595 -- Determine whether node N is a suitable scenario for invocation graph
11596 -- recording purposes.
11597
11598 function Is_Invocation_Target (Id : Entity_Id) return Boolean;
11599 pragma Inline (Is_Invocation_Target);
11600 -- Determine whether arbitrary entity Id denotes an invocation target
11601
11602 function Is_Saved_Construct (Constr : Entity_Id) return Boolean;
11603 pragma Inline (Is_Saved_Construct);
11604 -- Determine whether invocation construct Constr has already been
11605 -- declared in the ALI file of the main unit.
11606
11607 function Is_Saved_Relation
11608 (Rel : Invoker_Target_Relation) return Boolean;
11609 pragma Inline (Is_Saved_Relation);
11610 -- Determine whether simple invocation relation Rel has already been
11611 -- recorded in the ALI file of the main unit.
11612
11613 procedure Process_Declarations
11614 (Decls : List_Id;
11615 In_State : Processing_In_State);
11616 pragma Inline (Process_Declarations);
11617 -- Process declaration list Decls by processing all invocation scenarios
11618 -- within it.
11619
11620 procedure Process_Freeze_Node
11621 (Fnode : Node_Id;
11622 In_State : Processing_In_State);
11623 pragma Inline (Process_Freeze_Node);
11624 -- Process freeze node Fnode by processing all invocation scenarios in
11625 -- its Actions list.
11626
11627 procedure Process_Invocation_Activation
11628 (Call : Node_Id;
11629 Call_Rep : Scenario_Rep_Id;
11630 Obj_Id : Entity_Id;
11631 Obj_Rep : Target_Rep_Id;
11632 Task_Typ : Entity_Id;
11633 Task_Rep : Target_Rep_Id;
11634 In_State : Processing_In_State);
11635 pragma Inline (Process_Invocation_Activation);
11636 -- Process activation call Call which activates object Obj_Id of task
11637 -- type Task_Typ by processing all invocation scenarios within the task
11638 -- body. Call_Rep is the representation of the call. Obj_Rep denotes the
11639 -- representation of the object. Task_Rep is the representation of the
11640 -- task type. In_State is the current state of the Processing phase.
11641
11642 procedure Process_Invocation_Body_Scenarios;
11643 pragma Inline (Process_Invocation_Body_Scenarios);
11644 -- Process all library level body scenarios
11645
11646 procedure Process_Invocation_Call
11647 (Call : Node_Id;
11648 Call_Rep : Scenario_Rep_Id;
11649 In_State : Processing_In_State);
11650 pragma Inline (Process_Invocation_Call);
11651 -- Process invocation call scenario Call with representation Call_Rep.
11652 -- In_State is the current state of the Processing phase.
11653
11654 procedure Process_Invocation_Instantiation
11655 (Inst : Node_Id;
11656 Inst_Rep : Scenario_Rep_Id;
11657 In_State : Processing_In_State);
11658 pragma Inline (Process_Invocation_Instantiation);
11659 -- Process invocation instantiation scenario Inst with representation
11660 -- Inst_Rep. In_State is the current state of the Processing phase.
11661
11662 procedure Process_Invocation_Scenario
11663 (N : Node_Id;
11664 In_State : Processing_In_State);
11665 pragma Inline (Process_Invocation_Scenario);
11666 -- Process single invocation scenario N. In_State is the current state
11667 -- of the Processing phase.
11668
11669 procedure Process_Invocation_Scenarios
11670 (Iter : in out NE_Set.Iterator;
11671 In_State : Processing_In_State);
11672 pragma Inline (Process_Invocation_Scenarios);
11673 -- Process all invocation scenarios obtained via iterator Iter. In_State
11674 -- is the current state of the Processing phase.
11675
11676 procedure Process_Invocation_Spec_Scenarios;
11677 pragma Inline (Process_Invocation_Spec_Scenarios);
11678 -- Process all library level spec scenarios
11679
11680 procedure Process_Main_Unit;
11681 pragma Inline (Process_Main_Unit);
11682 -- Process all invocation scenarios within the main unit
11683
11684 procedure Process_Package_Declaration
11685 (Pack_Decl : Node_Id;
11686 In_State : Processing_In_State);
11687 pragma Inline (Process_Package_Declaration);
11688 -- Process package declaration Pack_Decl by processing all invocation
11689 -- scenarios in its visible and private declarations. If the main unit
11690 -- contains a generic, the declarations of the body are also examined.
11691 -- In_State is the current state of the Processing phase.
11692
11693 procedure Process_Protected_Type_Declaration
11694 (Prot_Decl : Node_Id;
11695 In_State : Processing_In_State);
11696 pragma Inline (Process_Protected_Type_Declaration);
11697 -- Process the declarations of protected type Prot_Decl. In_State is the
11698 -- current state of the Processing phase.
11699
11700 procedure Process_Subprogram_Declaration
11701 (Subp_Decl : Node_Id;
11702 In_State : Processing_In_State);
11703 pragma Inline (Process_Subprogram_Declaration);
11704 -- Process subprogram declaration Subp_Decl by processing all invocation
11705 -- scenarios within its body. In_State denotes the current state of the
11706 -- Processing phase.
11707
11708 procedure Process_Subprogram_Instantiation
11709 (Inst : Node_Id;
11710 In_State : Processing_In_State);
11711 pragma Inline (Process_Subprogram_Instantiation);
11712 -- Process subprogram instantiation Inst. In_State is the current state
11713 -- of the Processing phase.
11714
11715 procedure Process_Task_Type_Declaration
11716 (Task_Decl : Node_Id;
11717 In_State : Processing_In_State);
11718 pragma Inline (Process_Task_Type_Declaration);
11719 -- Process task declaration Task_Decl by processing all invocation
11720 -- scenarios within its body. In_State is the current state of the
11721 -- Processing phase.
11722
11723 procedure Record_Full_Invocation_Path (In_State : Processing_In_State);
11724 pragma Inline (Record_Full_Invocation_Path);
11725 -- Record all relations between scenario pairs found in the stack of
11726 -- active scenarios. In_State is the current state of the Processing
11727 -- phase.
11728
11729 procedure Record_Invocation_Graph_Encoding;
11730 pragma Inline (Record_Invocation_Graph_Encoding);
11731 -- Record the encoding format used to capture information related to
11732 -- invocation constructs and relations.
11733
11734 procedure Record_Invocation_Path (In_State : Processing_In_State);
11735 pragma Inline (Record_Invocation_Path);
11736 -- Record the invocation relations found within the path represented in
11737 -- the active scenario stack. In_State denotes the current state of the
11738 -- Processing phase.
11739
11740 procedure Record_Simple_Invocation_Path (In_State : Processing_In_State);
11741 pragma Inline (Record_Simple_Invocation_Path);
11742 -- Record a single relation from the start to the end of the stack of
11743 -- active scenarios. In_State is the current state of the Processing
11744 -- phase.
11745
11746 procedure Record_Invocation_Relation
11747 (Invk_Id : Entity_Id;
11748 Targ_Id : Entity_Id;
11749 In_State : Processing_In_State);
11750 pragma Inline (Record_Invocation_Relation);
11751 -- Record an invocation relation with invoker Invk_Id and target Targ_Id
11752 -- by creating an entry for it in the ALI file of the main unit. Formal
11753 -- In_State denotes the current state of the Processing phase.
11754
11755 procedure Set_Is_Saved_Construct
11756 (Constr : Entity_Id;
11757 Val : Boolean := True);
11758 pragma Inline (Set_Is_Saved_Construct);
11759 -- Mark invocation construct Constr as declared in the ALI file of the
11760 -- main unit depending on value Val.
11761
11762 procedure Set_Is_Saved_Relation
11763 (Rel : Invoker_Target_Relation;
11764 Val : Boolean := True);
11765 pragma Inline (Set_Is_Saved_Relation);
11766 -- Mark simple invocation relation Rel as recorded in the ALI file of
11767 -- the main unit depending on value Val.
11768
11769 function Target_Of
11770 (Pos : Active_Scenario_Pos;
11771 In_State : Processing_In_State) return Entity_Id;
11772 pragma Inline (Target_Of);
11773 -- Given position within the active scenario stack Pos, obtain the
11774 -- target of the indicated scenario. In_State is the current state
11775 -- of the Processing phase.
11776
11777 procedure Traverse_Invocation_Body
11778 (N : Node_Id;
11779 In_State : Processing_In_State);
11780 pragma Inline (Traverse_Invocation_Body);
11781 -- Traverse subprogram body N looking for suitable invocation scenarios
11782 -- that need to be processed for invocation graph recording purposes.
11783 -- In_State is the current state of the Processing phase.
11784
11785 procedure Write_Invocation_Path (In_State : Processing_In_State);
11786 pragma Inline (Write_Invocation_Path);
11787 -- Write out a path represented by the active scenario on the stack to
11788 -- standard output. In_State denotes the current state of the Processing
11789 -- phase.
11790
11791 ------------------------------------
11792 -- Build_Elaborate_Body_Procedure --
11793 ------------------------------------
11794
11795 procedure Build_Elaborate_Body_Procedure is
11796 Body_Decl : Node_Id;
11797 Spec_Decl : Node_Id;
11798
11799 begin
11800 -- Nothing to do when a previous call already created the procedure
11801
11802 if Present (Elab_Body_Id) then
11803 return;
11804 end if;
11805
11806 Spec_And_Body_From_Entity
11807 (Id => Main_Unit_Entity,
11808 Body_Decl => Body_Decl,
11809 Spec_Decl => Spec_Decl);
11810
11811 pragma Assert (Present (Body_Decl));
11812
11813 Build_Elaborate_Procedure
11814 (Proc_Id => Elab_Body_Id,
11815 Proc_Nam => Name_B,
11816 Loc => Sloc (Body_Decl));
11817 end Build_Elaborate_Body_Procedure;
11818
11819 -------------------------------
11820 -- Build_Elaborate_Procedure --
11821 -------------------------------
11822
11823 procedure Build_Elaborate_Procedure
11824 (Proc_Id : out Entity_Id;
11825 Proc_Nam : Name_Id;
11826 Loc : Source_Ptr)
11827 is
11828 Proc_Decl : Node_Id;
11829 pragma Unreferenced (Proc_Decl);
11830
11831 begin
11832 Proc_Id := Make_Defining_Identifier (Loc, Proc_Nam);
11833
11834 -- Partially decorate the elaboration procedure because it will not
11835 -- be insertred into the tree and analyzed.
11836
11837 Set_Ekind (Proc_Id, E_Procedure);
11838 Set_Etype (Proc_Id, Standard_Void_Type);
11839 Set_Scope (Proc_Id, Unique_Entity (Main_Unit_Entity));
11840
11841 -- Create a dummy declaration for the elaboration procedure. The
11842 -- declaration does not need to be syntactically legal, but must
11843 -- carry an accurate source location.
11844
11845 Proc_Decl :=
11846 Make_Subprogram_Body (Loc,
11847 Specification =>
11848 Make_Procedure_Specification (Loc,
11849 Defining_Unit_Name => Proc_Id),
11850 Declarations => No_List,
11851 Handled_Statement_Sequence => Empty);
11852 end Build_Elaborate_Procedure;
11853
11854 ------------------------------------
11855 -- Build_Elaborate_Spec_Procedure --
11856 ------------------------------------
11857
11858 procedure Build_Elaborate_Spec_Procedure is
11859 Body_Decl : Node_Id;
11860 Spec_Decl : Node_Id;
11861
11862 begin
11863 -- Nothing to do when a previous call already created the procedure
11864
11865 if Present (Elab_Spec_Id) then
11866 return;
11867 end if;
11868
11869 Spec_And_Body_From_Entity
11870 (Id => Main_Unit_Entity,
11871 Body_Decl => Body_Decl,
11872 Spec_Decl => Spec_Decl);
11873
11874 pragma Assert (Present (Spec_Decl));
11875
11876 Build_Elaborate_Procedure
11877 (Proc_Id => Elab_Spec_Id,
11878 Proc_Nam => Name_S,
11879 Loc => Sloc (Spec_Decl));
11880 end Build_Elaborate_Spec_Procedure;
11881
11882 ---------------------------------
11883 -- Build_Subprogram_Invocation --
11884 ---------------------------------
11885
11886 function Build_Subprogram_Invocation
11887 (Subp_Id : Entity_Id) return Node_Id
11888 is
11889 Marker : constant Node_Id := Make_Call_Marker (Sloc (Subp_Id));
11890 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
11891
11892 begin
11893 -- Create a dummy call marker which invokes the subprogram
11894
11895 Set_Is_Declaration_Level_Node (Marker, False);
11896 Set_Is_Dispatching_Call (Marker, False);
11897 Set_Is_Elaboration_Checks_OK_Node (Marker, False);
11898 Set_Is_Elaboration_Warnings_OK_Node (Marker, False);
11899 Set_Is_Ignored_Ghost_Node (Marker, False);
11900 Set_Is_Source_Call (Marker, False);
11901 Set_Is_SPARK_Mode_On_Node (Marker, False);
11902
11903 -- Invoke the uniform canonical entity of the subprogram
11904
11905 Set_Target (Marker, Canonical_Subprogram (Subp_Id));
11906
11907 -- Partially insert the marker into the tree
11908
11909 Set_Parent (Marker, Parent (Subp_Decl));
11910
11911 return Marker;
11912 end Build_Subprogram_Invocation;
11913
11914 ---------------------------
11915 -- Build_Task_Activation --
11916 ---------------------------
11917
11918 function Build_Task_Activation
11919 (Task_Typ : Entity_Id;
11920 In_State : Processing_In_State) return Node_Id
11921 is
11922 Loc : constant Source_Ptr := Sloc (Task_Typ);
11923 Marker : constant Node_Id := Make_Call_Marker (Loc);
11924 Task_Decl : constant Node_Id := Unit_Declaration_Node (Task_Typ);
11925
11926 Activ_Id : Entity_Id;
11927 Marker_Rep_Id : Scenario_Rep_Id;
11928 Task_Obj : Entity_Id;
11929 Task_Objs : NE_List.Doubly_Linked_List;
11930
11931 begin
11932 -- Create a dummy call marker which activates some tasks
11933
11934 Set_Is_Declaration_Level_Node (Marker, False);
11935 Set_Is_Dispatching_Call (Marker, False);
11936 Set_Is_Elaboration_Checks_OK_Node (Marker, False);
11937 Set_Is_Elaboration_Warnings_OK_Node (Marker, False);
11938 Set_Is_Ignored_Ghost_Node (Marker, False);
11939 Set_Is_Source_Call (Marker, False);
11940 Set_Is_SPARK_Mode_On_Node (Marker, False);
11941
11942 -- Invoke the appropriate version of Activate_Tasks
11943
11944 if Restricted_Profile then
11945 Activ_Id := RTE (RE_Activate_Restricted_Tasks);
11946 else
11947 Activ_Id := RTE (RE_Activate_Tasks);
11948 end if;
11949
11950 Set_Target (Marker, Activ_Id);
11951
11952 -- Partially insert the marker into the tree
11953
11954 Set_Parent (Marker, Parent (Task_Decl));
11955
11956 -- Create a dummy task object. Partially decorate the object because
11957 -- it will not be inserted into the tree and analyzed.
11958
11959 Task_Obj := Make_Temporary (Loc, 'T');
11960 Set_Ekind (Task_Obj, E_Variable);
11961 Set_Etype (Task_Obj, Task_Typ);
11962
11963 -- Associate the dummy task object with the activation call
11964
11965 Task_Objs := NE_List.Create;
11966 NE_List.Append (Task_Objs, Task_Obj);
11967
11968 Marker_Rep_Id := Scenario_Representation_Of (Marker, In_State);
11969 Set_Activated_Task_Objects (Marker_Rep_Id, Task_Objs);
11970 Set_Activated_Task_Type (Marker_Rep_Id, Task_Typ);
11971
11972 return Marker;
11973 end Build_Task_Activation;
11974
11975 ----------------------------------
11976 -- Declare_Invocation_Construct --
11977 ----------------------------------
11978
11979 procedure Declare_Invocation_Construct
11980 (Constr_Id : Entity_Id;
11981 In_State : Processing_In_State)
11982 is
11983 function Body_Placement_Of
11984 (Id : Entity_Id) return Declaration_Placement_Kind;
11985 pragma Inline (Body_Placement_Of);
11986 -- Obtain the placement of arbitrary entity Id's body
11987
11988 function Declaration_Placement_Of_Node
11989 (N : Node_Id) return Declaration_Placement_Kind;
11990 pragma Inline (Declaration_Placement_Of_Node);
11991 -- Obtain the placement of arbitrary node N
11992
11993 function Kind_Of (Id : Entity_Id) return Invocation_Construct_Kind;
11994 pragma Inline (Kind_Of);
11995 -- Obtain the invocation construct kind of arbitrary entity Id
11996
11997 function Spec_Placement_Of
11998 (Id : Entity_Id) return Declaration_Placement_Kind;
11999 pragma Inline (Spec_Placement_Of);
12000 -- Obtain the placement of arbitrary entity Id's spec
12001
12002 -----------------------
12003 -- Body_Placement_Of --
12004 -----------------------
12005
12006 function Body_Placement_Of
12007 (Id : Entity_Id) return Declaration_Placement_Kind
12008 is
12009 Id_Rep : constant Target_Rep_Id :=
12010 Target_Representation_Of (Id, In_State);
12011 Body_Decl : constant Node_Id := Body_Declaration (Id_Rep);
12012 Spec_Decl : constant Node_Id := Spec_Declaration (Id_Rep);
12013
12014 begin
12015 -- The entity has a body
12016
12017 if Present (Body_Decl) then
12018 return Declaration_Placement_Of_Node (Body_Decl);
12019
12020 -- Otherwise the entity must have a spec
12021
12022 else
12023 pragma Assert (Present (Spec_Decl));
12024 return Declaration_Placement_Of_Node (Spec_Decl);
12025 end if;
12026 end Body_Placement_Of;
12027
12028 -----------------------------------
12029 -- Declaration_Placement_Of_Node --
12030 -----------------------------------
12031
12032 function Declaration_Placement_Of_Node
12033 (N : Node_Id) return Declaration_Placement_Kind
12034 is
12035 Main_Unit_Id : constant Entity_Id := Main_Unit_Entity;
12036 N_Unit_Id : constant Entity_Id := Find_Top_Unit (N);
12037
12038 begin
12039 -- The node is in the main unit, its placement depends on the main
12040 -- unit kind.
12041
12042 if N_Unit_Id = Main_Unit_Id then
12043
12044 -- The main unit is a body
12045
12046 if Ekind_In (Main_Unit_Id, E_Package_Body,
12047 E_Subprogram_Body)
12048 then
12049 return In_Body;
12050
12051 -- The main unit is a stand-alone subprogram body
12052
12053 elsif Ekind_In (Main_Unit_Id, E_Function, E_Procedure)
12054 and then Nkind (Unit_Declaration_Node (Main_Unit_Id)) =
12055 N_Subprogram_Body
12056 then
12057 return In_Body;
12058
12059 -- Otherwise the main unit is a spec
12060
12061 else
12062 return In_Spec;
12063 end if;
12064
12065 -- Otherwise the node is in the complementary unit of the main
12066 -- unit. The main unit is a body, the node is in the spec.
12067
12068 elsif Ekind_In (Main_Unit_Id, E_Package_Body,
12069 E_Subprogram_Body)
12070 then
12071 return In_Spec;
12072
12073 -- The main unit is a spec, the node is in the body
12074
12075 else
12076 return In_Body;
12077 end if;
12078 end Declaration_Placement_Of_Node;
12079
12080 -------------
12081 -- Kind_Of --
12082 -------------
12083
12084 function Kind_Of (Id : Entity_Id) return Invocation_Construct_Kind is
12085 begin
12086 if Id = Elab_Body_Id then
12087 return Elaborate_Body_Procedure;
12088
12089 elsif Id = Elab_Spec_Id then
12090 return Elaborate_Spec_Procedure;
12091
12092 else
12093 return Regular_Construct;
12094 end if;
12095 end Kind_Of;
12096
12097 -----------------------
12098 -- Spec_Placement_Of --
12099 -----------------------
12100
12101 function Spec_Placement_Of
12102 (Id : Entity_Id) return Declaration_Placement_Kind
12103 is
12104 Id_Rep : constant Target_Rep_Id :=
12105 Target_Representation_Of (Id, In_State);
12106 Body_Decl : constant Node_Id := Body_Declaration (Id_Rep);
12107 Spec_Decl : constant Node_Id := Spec_Declaration (Id_Rep);
12108
12109 begin
12110 -- The entity has a spec
12111
12112 if Present (Spec_Decl) then
12113 return Declaration_Placement_Of_Node (Spec_Decl);
12114
12115 -- Otherwise the entity must have a body
12116
12117 else
12118 pragma Assert (Present (Body_Decl));
12119 return Declaration_Placement_Of_Node (Body_Decl);
12120 end if;
12121 end Spec_Placement_Of;
12122
12123 -- Start of processing for Declare_Invocation_Construct
12124
12125 begin
12126 -- Nothing to do when the construct has already been declared in the
12127 -- ALI file.
12128
12129 if Is_Saved_Construct (Constr_Id) then
12130 return;
12131 end if;
12132
12133 -- Mark the construct as declared in the ALI file
12134
12135 Set_Is_Saved_Construct (Constr_Id);
12136
12137 -- Add the construct in the ALI file
12138
12139 Add_Invocation_Construct
12140 (Body_Placement => Body_Placement_Of (Constr_Id),
12141 Kind => Kind_Of (Constr_Id),
12142 Signature => Signature_Of (Constr_Id),
12143 Spec_Placement => Spec_Placement_Of (Constr_Id),
12144 Update_Units => False);
12145 end Declare_Invocation_Construct;
12146
12147 -------------------------------
12148 -- Finalize_Invocation_Graph --
12149 -------------------------------
12150
12151 procedure Finalize_Invocation_Graph is
12152 begin
12153 NE_Set.Destroy (Saved_Constructs_Set);
12154 IR_Set.Destroy (Saved_Relations_Set);
12155 end Finalize_Invocation_Graph;
12156
12157 ----------
12158 -- Hash --
12159 ----------
12160
12161 function Hash (Key : Invoker_Target_Relation) return Bucket_Range_Type is
12162 pragma Assert (Present (Key.Invoker));
12163 pragma Assert (Present (Key.Target));
12164
12165 begin
12166 return
12167 Hash_Two_Keys
12168 (Bucket_Range_Type (Key.Invoker),
12169 Bucket_Range_Type (Key.Target));
12170 end Hash;
12171
12172 ---------------------------------
12173 -- Initialize_Invocation_Graph --
12174 ---------------------------------
12175
12176 procedure Initialize_Invocation_Graph is
12177 begin
12178 Saved_Constructs_Set := NE_Set.Create (100);
12179 Saved_Relations_Set := IR_Set.Create (200);
12180 end Initialize_Invocation_Graph;
12181
12182 -----------------------------------
12183 -- Invocation_Graph_Recording_OK --
12184 -----------------------------------
12185
12186 function Invocation_Graph_Recording_OK return Boolean is
12187 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
12188
12189 begin
12190 -- Nothing to do when compiling for GNATprove because the invocation
12191 -- graph is not needed.
12192
12193 if GNATprove_Mode then
12194 return False;
12195
12196 -- Nothing to do when the compilation will not produce an ALI file
12197
12198 elsif Serious_Errors_Detected > 0 then
12199 return False;
12200
12201 -- Nothing to do when the main unit requires a body. Processing the
12202 -- completing body will create the ALI file for the unit and record
12203 -- the invocation graph.
12204
12205 elsif Body_Required (Main_Cunit) then
12206 return False;
12207 end if;
12208
12209 return True;
12210 end Invocation_Graph_Recording_OK;
12211
12212 ----------------------------
12213 -- Is_Invocation_Scenario --
12214 ----------------------------
12215
12216 function Is_Invocation_Scenario (N : Node_Id) return Boolean is
12217 begin
12218 return
12219 Is_Suitable_Access_Taken (N)
12220 or else Is_Suitable_Call (N)
12221 or else Is_Suitable_Instantiation (N);
12222 end Is_Invocation_Scenario;
12223
12224 --------------------------
12225 -- Is_Invocation_Target --
12226 --------------------------
12227
12228 function Is_Invocation_Target (Id : Entity_Id) return Boolean is
12229 begin
12230 -- To qualify, the entity must either come from source, or denote an
12231 -- Ada, bridge, or SPARK target.
12232
12233 return
12234 Comes_From_Source (Id)
12235 or else Is_Ada_Semantic_Target (Id)
12236 or else Is_Bridge_Target (Id)
12237 or else Is_SPARK_Semantic_Target (Id);
12238 end Is_Invocation_Target;
12239
12240 ------------------------
12241 -- Is_Saved_Construct --
12242 ------------------------
12243
12244 function Is_Saved_Construct (Constr : Entity_Id) return Boolean is
12245 pragma Assert (Present (Constr));
12246 begin
12247 return NE_Set.Contains (Saved_Constructs_Set, Constr);
12248 end Is_Saved_Construct;
12249
12250 -----------------------
12251 -- Is_Saved_Relation --
12252 -----------------------
12253
12254 function Is_Saved_Relation
12255 (Rel : Invoker_Target_Relation) return Boolean
12256 is
12257 pragma Assert (Present (Rel.Invoker));
12258 pragma Assert (Present (Rel.Target));
12259
12260 begin
12261 return IR_Set.Contains (Saved_Relations_Set, Rel);
12262 end Is_Saved_Relation;
12263
12264 --------------------------
12265 -- Process_Declarations --
12266 --------------------------
12267
12268 procedure Process_Declarations
12269 (Decls : List_Id;
12270 In_State : Processing_In_State)
12271 is
12272 Decl : Node_Id;
12273
12274 begin
12275 Decl := First (Decls);
12276 while Present (Decl) loop
12277
12278 -- Freeze node
12279
12280 if Nkind (Decl) = N_Freeze_Entity then
12281 Process_Freeze_Node
12282 (Fnode => Decl,
12283 In_State => In_State);
12284
12285 -- Package (nested)
12286
12287 elsif Nkind (Decl) = N_Package_Declaration then
12288 Process_Package_Declaration
12289 (Pack_Decl => Decl,
12290 In_State => In_State);
12291
12292 -- Protected type
12293
12294 elsif Nkind_In (Decl, N_Protected_Type_Declaration,
12295 N_Single_Protected_Declaration)
12296 then
12297 Process_Protected_Type_Declaration
12298 (Prot_Decl => Decl,
12299 In_State => In_State);
12300
12301 -- Subprogram or entry
12302
12303 elsif Nkind_In (Decl, N_Entry_Declaration,
12304 N_Subprogram_Declaration)
12305 then
12306 Process_Subprogram_Declaration
12307 (Subp_Decl => Decl,
12308 In_State => In_State);
12309
12310 -- Subprogram body (stand alone)
12311
12312 elsif Nkind (Decl) = N_Subprogram_Body
12313 and then No (Corresponding_Spec (Decl))
12314 then
12315 Process_Subprogram_Declaration
12316 (Subp_Decl => Decl,
12317 In_State => In_State);
12318
12319 -- Subprogram instantiation
12320
12321 elsif Nkind (Decl) in N_Subprogram_Instantiation then
12322 Process_Subprogram_Instantiation
12323 (Inst => Decl,
12324 In_State => In_State);
12325
12326 -- Task type
12327
12328 elsif Nkind_In (Decl, N_Single_Task_Declaration,
12329 N_Task_Type_Declaration)
12330 then
12331 Process_Task_Type_Declaration
12332 (Task_Decl => Decl,
12333 In_State => In_State);
12334
12335 -- Task type (derived)
12336
12337 elsif Nkind (Decl) = N_Full_Type_Declaration
12338 and then Is_Task_Type (Defining_Entity (Decl))
12339 then
12340 Process_Task_Type_Declaration
12341 (Task_Decl => Decl,
12342 In_State => In_State);
12343 end if;
12344
12345 Next (Decl);
12346 end loop;
12347 end Process_Declarations;
12348
12349 -------------------------
12350 -- Process_Freeze_Node --
12351 -------------------------
12352
12353 procedure Process_Freeze_Node
12354 (Fnode : Node_Id;
12355 In_State : Processing_In_State)
12356 is
12357 begin
12358 Process_Declarations
12359 (Decls => Actions (Fnode),
12360 In_State => In_State);
12361 end Process_Freeze_Node;
12362
12363 -----------------------------------
12364 -- Process_Invocation_Activation --
12365 -----------------------------------
12366
12367 procedure Process_Invocation_Activation
12368 (Call : Node_Id;
12369 Call_Rep : Scenario_Rep_Id;
12370 Obj_Id : Entity_Id;
12371 Obj_Rep : Target_Rep_Id;
12372 Task_Typ : Entity_Id;
12373 Task_Rep : Target_Rep_Id;
12374 In_State : Processing_In_State)
12375 is
12376 pragma Unreferenced (Call);
12377 pragma Unreferenced (Call_Rep);
12378 pragma Unreferenced (Obj_Id);
12379 pragma Unreferenced (Obj_Rep);
12380
12381 begin
12382 -- Nothing to do when the task type appears within an internal unit
12383
12384 if In_Internal_Unit (Task_Typ) then
12385 return;
12386 end if;
12387
12388 -- The task type being activated is within the main unit. Extend the
12389 -- DFS traversal into its body.
12390
12391 if In_Extended_Main_Code_Unit (Task_Typ) then
12392 Traverse_Invocation_Body
12393 (N => Body_Declaration (Task_Rep),
12394 In_State => In_State);
12395
12396 -- The task type being activated resides within an external unit
12397 --
12398 -- Main unit External unit
12399 -- +-----------+ +-------------+
12400 -- | | | |
12401 -- | Start ------------> Task_Typ |
12402 -- | | | |
12403 -- +-----------+ +-------------+
12404 --
12405 -- Record the invocation path which originates from Start and reaches
12406 -- the task type.
12407
12408 else
12409 Record_Invocation_Path (In_State);
12410 end if;
12411 end Process_Invocation_Activation;
12412
12413 ---------------------------------------
12414 -- Process_Invocation_Body_Scenarios --
12415 ---------------------------------------
12416
12417 procedure Process_Invocation_Body_Scenarios is
12418 Iter : NE_Set.Iterator := Iterate_Library_Body_Scenarios;
12419 begin
12420 Process_Invocation_Scenarios
12421 (Iter => Iter,
12422 In_State => Invocation_Body_State);
12423 end Process_Invocation_Body_Scenarios;
12424
12425 -----------------------------
12426 -- Process_Invocation_Call --
12427 -----------------------------
12428
12429 procedure Process_Invocation_Call
12430 (Call : Node_Id;
12431 Call_Rep : Scenario_Rep_Id;
12432 In_State : Processing_In_State)
12433 is
12434 pragma Unreferenced (Call);
12435
12436 Subp_Id : constant Entity_Id := Target (Call_Rep);
12437 Subp_Rep : constant Target_Rep_Id :=
12438 Target_Representation_Of (Subp_Id, In_State);
12439
12440 begin
12441 -- Nothing to do when the subprogram appears within an internal unit
12442
12443 if In_Internal_Unit (Subp_Id) then
12444 return;
12445
12446 -- Nothing to do for an abstract subprogram because it has no body to
12447 -- examine.
12448
12449 elsif Ekind_In (Subp_Id, E_Function, E_Procedure)
12450 and then Is_Abstract_Subprogram (Subp_Id)
12451 then
12452 return;
12453
12454 -- Nothin to do for a formal subprogram because it has no body to
12455 -- examine.
12456
12457 elsif Is_Formal_Subprogram (Subp_Id) then
12458 return;
12459 end if;
12460
12461 -- The subprogram being called is within the main unit. Extend the
12462 -- DFS traversal into its barrier function and body.
12463
12464 if In_Extended_Main_Code_Unit (Subp_Id) then
12465 if Ekind_In (Subp_Id, E_Entry, E_Entry_Family, E_Procedure) then
12466 Traverse_Invocation_Body
12467 (N => Barrier_Body_Declaration (Subp_Rep),
12468 In_State => In_State);
12469 end if;
12470
12471 Traverse_Invocation_Body
12472 (N => Body_Declaration (Subp_Rep),
12473 In_State => In_State);
12474
12475 -- The subprogram being called resides within an external unit
12476 --
12477 -- Main unit External unit
12478 -- +-----------+ +-------------+
12479 -- | | | |
12480 -- | Start ------------> Subp_Id |
12481 -- | | | |
12482 -- +-----------+ +-------------+
12483 --
12484 -- Record the invocation path which originates from Start and reaches
12485 -- the subprogram.
12486
12487 else
12488 Record_Invocation_Path (In_State);
12489 end if;
12490 end Process_Invocation_Call;
12491
12492 --------------------------------------
12493 -- Process_Invocation_Instantiation --
12494 --------------------------------------
12495
12496 procedure Process_Invocation_Instantiation
12497 (Inst : Node_Id;
12498 Inst_Rep : Scenario_Rep_Id;
12499 In_State : Processing_In_State)
12500 is
12501 pragma Unreferenced (Inst);
12502
12503 Gen_Id : constant Entity_Id := Target (Inst_Rep);
12504
12505 begin
12506 -- Nothing to do when the generic appears within an internal unit
12507
12508 if In_Internal_Unit (Gen_Id) then
12509 return;
12510 end if;
12511
12512 -- The generic being instantiated resides within an external unit
12513 --
12514 -- Main unit External unit
12515 -- +-----------+ +-------------+
12516 -- | | | |
12517 -- | Start ------------> Generic |
12518 -- | | | |
12519 -- +-----------+ +-------------+
12520 --
12521 -- Record the invocation path which originates from Start and reaches
12522 -- the generic.
12523
12524 if not In_Extended_Main_Code_Unit (Gen_Id) then
12525 Record_Invocation_Path (In_State);
12526 end if;
12527 end Process_Invocation_Instantiation;
12528
12529 ---------------------------------
12530 -- Process_Invocation_Scenario --
12531 ---------------------------------
12532
12533 procedure Process_Invocation_Scenario
12534 (N : Node_Id;
12535 In_State : Processing_In_State)
12536 is
12537 Scen : constant Node_Id := Scenario (N);
12538 Scen_Rep : Scenario_Rep_Id;
12539
12540 begin
12541 -- Add the current scenario to the stack of active scenarios
12542
12543 Push_Active_Scenario (Scen);
12544
12545 -- Call or task activation
12546
12547 if Is_Suitable_Call (Scen) then
12548 Scen_Rep := Scenario_Representation_Of (Scen, In_State);
12549
12550 -- Routine Build_Call_Marker creates call markers regardless of
12551 -- whether the call occurs within the main unit or not. This way
12552 -- the serialization of internal names is kept consistent. Only
12553 -- call markers found within the main unit must be processed.
12554
12555 if In_Main_Context (Scen) then
12556 Scen_Rep := Scenario_Representation_Of (Scen, In_State);
12557
12558 if Kind (Scen_Rep) = Call_Scenario then
12559 Process_Invocation_Call
12560 (Call => Scen,
12561 Call_Rep => Scen_Rep,
12562 In_State => In_State);
12563
12564 else
12565 pragma Assert (Kind (Scen_Rep) = Task_Activation_Scenario);
12566
12567 Process_Activation
12568 (Call => Scen,
12569 Call_Rep => Scen_Rep,
12570 Processor => Process_Invocation_Activation'Access,
12571 In_State => In_State);
12572 end if;
12573 end if;
12574
12575 -- Instantiation
12576
12577 elsif Is_Suitable_Instantiation (Scen) then
12578 Process_Invocation_Instantiation
12579 (Inst => Scen,
12580 Inst_Rep => Scenario_Representation_Of (Scen, In_State),
12581 In_State => In_State);
12582 end if;
12583
12584 -- Remove the current scenario from the stack of active scenarios
12585 -- once all invocation constructs and paths have been saved.
12586
12587 Pop_Active_Scenario (Scen);
12588 end Process_Invocation_Scenario;
12589
12590 ----------------------------------
12591 -- Process_Invocation_Scenarios --
12592 ----------------------------------
12593
12594 procedure Process_Invocation_Scenarios
12595 (Iter : in out NE_Set.Iterator;
12596 In_State : Processing_In_State)
12597 is
12598 N : Node_Id;
12599
12600 begin
12601 while NE_Set.Has_Next (Iter) loop
12602 NE_Set.Next (Iter, N);
12603
12604 -- Reset the traversed status of all subprogram bodies because the
12605 -- current invocation scenario acts as a new DFS traversal root.
12606
12607 Reset_Traversed_Bodies;
12608
12609 Process_Invocation_Scenario (N, In_State);
12610 end loop;
12611 end Process_Invocation_Scenarios;
12612
12613 ---------------------------------------
12614 -- Process_Invocation_Spec_Scenarios --
12615 ---------------------------------------
12616
12617 procedure Process_Invocation_Spec_Scenarios is
12618 Iter : NE_Set.Iterator := Iterate_Library_Spec_Scenarios;
12619 begin
12620 Process_Invocation_Scenarios
12621 (Iter => Iter,
12622 In_State => Invocation_Spec_State);
12623 end Process_Invocation_Spec_Scenarios;
12624
12625 -----------------------
12626 -- Process_Main_Unit --
12627 -----------------------
12628
12629 procedure Process_Main_Unit is
12630 Unit_Decl : constant Node_Id := Unit (Cunit (Main_Unit));
12631 Spec_Id : Entity_Id;
12632
12633 begin
12634 -- The main unit is a [generic] package body
12635
12636 if Nkind (Unit_Decl) = N_Package_Body then
12637 Spec_Id := Corresponding_Spec (Unit_Decl);
12638 pragma Assert (Present (Spec_Id));
12639
12640 Process_Package_Declaration
12641 (Pack_Decl => Unit_Declaration_Node (Spec_Id),
12642 In_State => Invocation_Construct_State);
12643
12644 -- The main unit is a [generic] package declaration
12645
12646 elsif Nkind (Unit_Decl) = N_Package_Declaration then
12647 Process_Package_Declaration
12648 (Pack_Decl => Unit_Decl,
12649 In_State => Invocation_Construct_State);
12650
12651 -- The main unit is a [generic] subprogram body
12652
12653 elsif Nkind (Unit_Decl) = N_Subprogram_Body then
12654 Spec_Id := Corresponding_Spec (Unit_Decl);
12655
12656 -- The body completes a previous declaration
12657
12658 if Present (Spec_Id) then
12659 Process_Subprogram_Declaration
12660 (Subp_Decl => Unit_Declaration_Node (Spec_Id),
12661 In_State => Invocation_Construct_State);
12662
12663 -- Otherwise the body is stand-alone
12664
12665 else
12666 Process_Subprogram_Declaration
12667 (Subp_Decl => Unit_Decl,
12668 In_State => Invocation_Construct_State);
12669 end if;
12670
12671 -- The main unit is a subprogram instantiation
12672
12673 elsif Nkind (Unit_Decl) in N_Subprogram_Instantiation then
12674 Process_Subprogram_Instantiation
12675 (Inst => Unit_Decl,
12676 In_State => Invocation_Construct_State);
12677
12678 -- The main unit is an imported subprogram declaration
12679
12680 elsif Nkind (Unit_Decl) = N_Subprogram_Declaration then
12681 Process_Subprogram_Declaration
12682 (Subp_Decl => Unit_Decl,
12683 In_State => Invocation_Construct_State);
12684 end if;
12685 end Process_Main_Unit;
12686
12687 ---------------------------------
12688 -- Process_Package_Declaration --
12689 ---------------------------------
12690
12691 procedure Process_Package_Declaration
12692 (Pack_Decl : Node_Id;
12693 In_State : Processing_In_State)
12694 is
12695 Body_Id : constant Entity_Id := Corresponding_Body (Pack_Decl);
12696 Spec : constant Node_Id := Specification (Pack_Decl);
12697 Spec_Id : constant Entity_Id := Defining_Entity (Pack_Decl);
12698
12699 begin
12700 -- Add a declaration for the generic package in the ALI of the main
12701 -- unit in case a client unit instantiates it.
12702
12703 if Ekind (Spec_Id) = E_Generic_Package then
12704 Declare_Invocation_Construct
12705 (Constr_Id => Spec_Id,
12706 In_State => In_State);
12707
12708 -- Otherwise inspect the visible and private declarations of the
12709 -- package for invocation constructs.
12710
12711 else
12712 Process_Declarations
12713 (Decls => Visible_Declarations (Spec),
12714 In_State => In_State);
12715
12716 Process_Declarations
12717 (Decls => Private_Declarations (Spec),
12718 In_State => In_State);
12719
12720 -- The package body containst at least one generic unit or an
12721 -- inlinable subprogram. Such constructs may grant clients of
12722 -- the main unit access to the private enclosing contexts of
12723 -- the constructs. Process the main unit body to discover and
12724 -- encode relevant invocation constructs and relations that
12725 -- may ultimately reach an external unit.
12726
12727 if Present (Body_Id)
12728 and then Save_Invocation_Graph_Of_Body (Cunit (Main_Unit))
12729 then
12730 Process_Declarations
12731 (Decls => Declarations (Unit_Declaration_Node (Body_Id)),
12732 In_State => In_State);
12733 end if;
12734 end if;
12735 end Process_Package_Declaration;
12736
12737 ----------------------------------------
12738 -- Process_Protected_Type_Declaration --
12739 ----------------------------------------
12740
12741 procedure Process_Protected_Type_Declaration
12742 (Prot_Decl : Node_Id;
12743 In_State : Processing_In_State)
12744 is
12745 Prot_Def : constant Node_Id := Protected_Definition (Prot_Decl);
12746
12747 begin
12748 if Present (Prot_Def) then
12749 Process_Declarations
12750 (Decls => Visible_Declarations (Prot_Def),
12751 In_State => In_State);
12752 end if;
12753 end Process_Protected_Type_Declaration;
12754
12755 ------------------------------------
12756 -- Process_Subprogram_Declaration --
12757 ------------------------------------
12758
12759 procedure Process_Subprogram_Declaration
12760 (Subp_Decl : Node_Id;
12761 In_State : Processing_In_State)
12762 is
12763 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
12764
12765 begin
12766 -- Nothing to do when the subprogram is not an invocation target
12767
12768 if not Is_Invocation_Target (Subp_Id) then
12769 return;
12770 end if;
12771
12772 -- Add a declaration for the subprogram in the ALI file of the main
12773 -- unit in case a client unit calls or instantiates it.
12774
12775 Declare_Invocation_Construct
12776 (Constr_Id => Subp_Id,
12777 In_State => In_State);
12778
12779 -- Do not process subprograms without a body because they do not
12780 -- contain any invocation scenarios.
12781
12782 if Is_Bodiless_Subprogram (Subp_Id) then
12783 null;
12784
12785 -- Do not process generic subprograms because generics must not be
12786 -- examined.
12787
12788 elsif Is_Generic_Subprogram (Subp_Id) then
12789 null;
12790
12791 -- Otherwise create a dummy scenario which calls the subprogram to
12792 -- act as a root for a DFS traversal.
12793
12794 else
12795 -- Reset the traversed status of all subprogram bodies because the
12796 -- subprogram acts as a new DFS traversal root.
12797
12798 Reset_Traversed_Bodies;
12799
12800 Process_Invocation_Scenario
12801 (N => Build_Subprogram_Invocation (Subp_Id),
12802 In_State => In_State);
12803 end if;
12804 end Process_Subprogram_Declaration;
12805
12806 --------------------------------------
12807 -- Process_Subprogram_Instantiation --
12808 --------------------------------------
12809
12810 procedure Process_Subprogram_Instantiation
12811 (Inst : Node_Id;
12812 In_State : Processing_In_State)
12813 is
12814 begin
12815 -- Add a declaration for the instantiation in the ALI file of the
12816 -- main unit in case a client unit calls it.
12817
12818 Declare_Invocation_Construct
12819 (Constr_Id => Defining_Entity (Inst),
12820 In_State => In_State);
12821 end Process_Subprogram_Instantiation;
12822
12823 -----------------------------------
12824 -- Process_Task_Type_Declaration --
12825 -----------------------------------
12826
12827 procedure Process_Task_Type_Declaration
12828 (Task_Decl : Node_Id;
12829 In_State : Processing_In_State)
12830 is
12831 Task_Typ : constant Entity_Id := Defining_Entity (Task_Decl);
12832 Task_Def : Node_Id;
12833
12834 begin
12835 -- Add a declaration for the task type the ALI file of the main unit
12836 -- in case a client unit creates a task object and activates it.
12837
12838 Declare_Invocation_Construct
12839 (Constr_Id => Task_Typ,
12840 In_State => In_State);
12841
12842 -- Process the entries of the task type because they represent valid
12843 -- entry points into the task body.
12844
12845 if Nkind_In (Task_Decl, N_Single_Task_Declaration,
12846 N_Task_Type_Declaration)
12847 then
12848 Task_Def := Task_Definition (Task_Decl);
12849
12850 if Present (Task_Def) then
12851 Process_Declarations
12852 (Decls => Visible_Declarations (Task_Def),
12853 In_State => In_State);
12854 end if;
12855 end if;
12856
12857 -- Reset the traversed status of all subprogram bodies because the
12858 -- task type acts as a new DFS traversal root.
12859
12860 Reset_Traversed_Bodies;
12861
12862 -- Create a dummy scenario which activates an anonymous object of the
12863 -- task type to acts as a root of a DFS traversal.
12864
12865 Process_Invocation_Scenario
12866 (N => Build_Task_Activation (Task_Typ, In_State),
12867 In_State => In_State);
12868 end Process_Task_Type_Declaration;
12869
12870 ---------------------------------
12871 -- Record_Full_Invocation_Path --
12872 ---------------------------------
12873
12874 procedure Record_Full_Invocation_Path (In_State : Processing_In_State) is
12875 package Scenarios renames Active_Scenario_Stack;
12876
12877 begin
12878 -- The path originates from the elaboration of the body. Add an extra
12879 -- relation from the elaboration body procedure to the first active
12880 -- scenario.
12881
12882 if In_State.Processing = Invocation_Body_Processing then
12883 Build_Elaborate_Body_Procedure;
12884
12885 Record_Invocation_Relation
12886 (Invk_Id => Elab_Body_Id,
12887 Targ_Id => Target_Of (Scenarios.First, In_State),
12888 In_State => In_State);
12889
12890 -- The path originates from the elaboration of the spec. Add an extra
12891 -- relation from the elaboration spec procedure to the first active
12892 -- scenario.
12893
12894 elsif In_State.Processing = Invocation_Spec_Processing then
12895 Build_Elaborate_Spec_Procedure;
12896
12897 Record_Invocation_Relation
12898 (Invk_Id => Elab_Spec_Id,
12899 Targ_Id => Target_Of (Scenarios.First, In_State),
12900 In_State => In_State);
12901 end if;
12902
12903 -- Record individual relations formed by pairs of scenarios
12904
12905 for Index in Scenarios.First .. Scenarios.Last - 1 loop
12906 Record_Invocation_Relation
12907 (Invk_Id => Target_Of (Index, In_State),
12908 Targ_Id => Target_Of (Index + 1, In_State),
12909 In_State => In_State);
12910 end loop;
12911 end Record_Full_Invocation_Path;
12912
12913 -----------------------------
12914 -- Record_Invocation_Graph --
12915 -----------------------------
12916
12917 procedure Record_Invocation_Graph is
12918 begin
12919 -- Nothing to do when the invocation graph is not recorded
12920
12921 if not Invocation_Graph_Recording_OK then
12922 return;
12923 end if;
12924
12925 -- Save the encoding format used to capture information about the
12926 -- invocation constructs and relations in the ALI file of the main
12927 -- unit.
12928
12929 Record_Invocation_Graph_Encoding;
12930
12931 -- Examine all library level invocation scenarios and perform DFS
12932 -- traversals from each one. Encode a path in the ALI file of the
12933 -- main unit if it reaches into an external unit.
12934
12935 Process_Invocation_Body_Scenarios;
12936 Process_Invocation_Spec_Scenarios;
12937
12938 -- Examine all invocation constructs within the spec and body of the
12939 -- main unit and perform DFS traversals from each one. Encode a path
12940 -- in the ALI file of the main unit if it reaches into an external
12941 -- unit.
12942
12943 Process_Main_Unit;
12944 end Record_Invocation_Graph;
12945
12946 --------------------------------------
12947 -- Record_Invocation_Graph_Encoding --
12948 --------------------------------------
12949
12950 procedure Record_Invocation_Graph_Encoding is
12951 Kind : Invocation_Graph_Encoding_Kind := No_Encoding;
12952
12953 begin
12954 -- Switch -gnatd_F (encode full invocation paths in ALI files) is in
12955 -- effect.
12956
12957 if Debug_Flag_Underscore_FF then
12958 Kind := Full_Path_Encoding;
12959 else
12960 Kind := Endpoints_Encoding;
12961 end if;
12962
12963 -- Save the encoding format in the ALI file of the main unit
12964
12965 Set_Invocation_Graph_Encoding
12966 (Kind => Kind,
12967 Update_Units => False);
12968 end Record_Invocation_Graph_Encoding;
12969
12970 ----------------------------
12971 -- Record_Invocation_Path --
12972 ----------------------------
12973
12974 procedure Record_Invocation_Path (In_State : Processing_In_State) is
12975 package Scenarios renames Active_Scenario_Stack;
12976
12977 begin
12978 -- Save a path when the active scenario stack contains at least one
12979 -- invocation scenario.
12980
12981 if Scenarios.Last - Scenarios.First < 0 then
12982 return;
12983 end if;
12984
12985 -- Register all relations in the path when switch -gnatd_F (encode
12986 -- full invocation paths in ALI files) is in effect.
12987
12988 if Debug_Flag_Underscore_FF then
12989 Record_Full_Invocation_Path (In_State);
12990
12991 -- Otherwise register a single relation
12992
12993 else
12994 Record_Simple_Invocation_Path (In_State);
12995 end if;
12996
12997 Write_Invocation_Path (In_State);
12998 end Record_Invocation_Path;
12999
13000 --------------------------------
13001 -- Record_Invocation_Relation --
13002 --------------------------------
13003
13004 procedure Record_Invocation_Relation
13005 (Invk_Id : Entity_Id;
13006 Targ_Id : Entity_Id;
13007 In_State : Processing_In_State)
13008 is
13009 pragma Assert (Present (Invk_Id));
13010 pragma Assert (Present (Targ_Id));
13011
13012 procedure Get_Invocation_Attributes
13013 (Extra : out Entity_Id;
13014 Kind : out Invocation_Kind);
13015 pragma Inline (Get_Invocation_Attributes);
13016 -- Return the additional entity used in error diagnostics in Extra
13017 -- and the invocation kind in Kind which pertain to the invocation
13018 -- relation with invoker Invk_Id and target Targ_Id.
13019
13020 -------------------------------
13021 -- Get_Invocation_Attributes --
13022 -------------------------------
13023
13024 procedure Get_Invocation_Attributes
13025 (Extra : out Entity_Id;
13026 Kind : out Invocation_Kind)
13027 is
13028 Targ_Rep : constant Target_Rep_Id :=
13029 Target_Representation_Of (Targ_Id, In_State);
13030 Spec_Decl : constant Node_Id := Spec_Declaration (Targ_Rep);
13031
13032 begin
13033 -- Accept within a task body
13034
13035 if Is_Accept_Alternative_Proc (Targ_Id) then
13036 Extra := Receiving_Entry (Targ_Id);
13037 Kind := Accept_Alternative;
13038
13039 -- Activation of a task object
13040
13041 elsif Is_Activation_Proc (Targ_Id)
13042 or else Is_Task_Type (Targ_Id)
13043 then
13044 Extra := Empty;
13045 Kind := Task_Activation;
13046
13047 -- Controlled adjustment actions
13048
13049 elsif Is_Controlled_Proc (Targ_Id, Name_Adjust) then
13050 Extra := First_Formal_Type (Targ_Id);
13051 Kind := Controlled_Adjustment;
13052
13053 -- Controlled finalization actions
13054
13055 elsif Is_Controlled_Proc (Targ_Id, Name_Finalize)
13056 or else Is_Finalizer_Proc (Targ_Id)
13057 then
13058 Extra := First_Formal_Type (Targ_Id);
13059 Kind := Controlled_Finalization;
13060
13061 -- Controlled initialization actions
13062
13063 elsif Is_Controlled_Proc (Targ_Id, Name_Initialize) then
13064 Extra := First_Formal_Type (Targ_Id);
13065 Kind := Controlled_Initialization;
13066
13067 -- Default_Initial_Condition verification
13068
13069 elsif Is_Default_Initial_Condition_Proc (Targ_Id) then
13070 Extra := First_Formal_Type (Targ_Id);
13071 Kind := Default_Initial_Condition_Verification;
13072
13073 -- Initialization of object
13074
13075 elsif Is_Init_Proc (Targ_Id) then
13076 Extra := First_Formal_Type (Targ_Id);
13077 Kind := Type_Initialization;
13078
13079 -- Initial_Condition verification
13080
13081 elsif Is_Initial_Condition_Proc (Targ_Id) then
13082 Extra := First_Formal_Type (Targ_Id);
13083 Kind := Initial_Condition_Verification;
13084
13085 -- Instantiation
13086
13087 elsif Is_Generic_Unit (Targ_Id) then
13088 Extra := Empty;
13089 Kind := Instantiation;
13090
13091 -- Internal controlled adjustment actions
13092
13093 elsif Is_TSS (Targ_Id, TSS_Deep_Adjust) then
13094 Extra := First_Formal_Type (Targ_Id);
13095 Kind := Internal_Controlled_Adjustment;
13096
13097 -- Internal controlled finalization actions
13098
13099 elsif Is_TSS (Targ_Id, TSS_Deep_Finalize) then
13100 Extra := First_Formal_Type (Targ_Id);
13101 Kind := Internal_Controlled_Finalization;
13102
13103 -- Internal controlled initialization actions
13104
13105 elsif Is_TSS (Targ_Id, TSS_Deep_Initialize) then
13106 Extra := First_Formal_Type (Targ_Id);
13107 Kind := Internal_Controlled_Initialization;
13108
13109 -- Invariant verification
13110
13111 elsif Is_Invariant_Proc (Targ_Id)
13112 or else Is_Partial_Invariant_Proc (Targ_Id)
13113 then
13114 Extra := First_Formal_Type (Targ_Id);
13115 Kind := Invariant_Verification;
13116
13117 -- Postcondition verification
13118
13119 elsif Is_Postconditions_Proc (Targ_Id) then
13120 Extra := Find_Enclosing_Scope (Spec_Decl);
13121 Kind := Postcondition_Verification;
13122
13123 -- Protected entry call
13124
13125 elsif Is_Protected_Entry (Targ_Id) then
13126 Extra := Empty;
13127 Kind := Protected_Entry_Call;
13128
13129 -- Protected subprogram call
13130
13131 elsif Is_Protected_Subp (Targ_Id) then
13132 Extra := Empty;
13133 Kind := Protected_Subprogram_Call;
13134
13135 -- Task entry call
13136
13137 elsif Is_Task_Entry (Targ_Id) then
13138 Extra := Empty;
13139 Kind := Task_Entry_Call;
13140
13141 -- Entry, operator, or subprogram call. This case must come last
13142 -- because most invocations above are variations of this case.
13143
13144 elsif Ekind_In (Targ_Id, E_Entry,
13145 E_Function,
13146 E_Operator,
13147 E_Procedure)
13148 then
13149 Extra := Empty;
13150 Kind := Call;
13151
13152 else
13153 pragma Assert (False);
13154 Extra := Empty;
13155 Kind := No_Invocation;
13156 end if;
13157 end Get_Invocation_Attributes;
13158
13159 -- Local variables
13160
13161 Extra : Entity_Id;
13162 Extra_Nam : Name_Id;
13163 Kind : Invocation_Kind;
13164 Rel : Invoker_Target_Relation;
13165
13166 -- Start of processing for Record_Invocation_Relation
13167
13168 begin
13169 Rel.Invoker := Invk_Id;
13170 Rel.Target := Targ_Id;
13171
13172 -- Nothing to do when the invocation relation has already been
13173 -- recorded in ALI file of the main unit.
13174
13175 if Is_Saved_Relation (Rel) then
13176 return;
13177 end if;
13178
13179 -- Mark the relation as recorded in the ALI file
13180
13181 Set_Is_Saved_Relation (Rel);
13182
13183 -- Declare the invoker in the ALI file
13184
13185 Declare_Invocation_Construct
13186 (Constr_Id => Invk_Id,
13187 In_State => In_State);
13188
13189 -- Obtain the invocation-specific attributes of the relation
13190
13191 Get_Invocation_Attributes (Extra, Kind);
13192
13193 -- Certain invocations lack an extra entity used in error diagnostics
13194
13195 if Present (Extra) then
13196 Extra_Nam := Chars (Extra);
13197 else
13198 Extra_Nam := No_Name;
13199 end if;
13200
13201 -- Add the relation in the ALI file
13202
13203 Add_Invocation_Relation
13204 (Extra => Extra_Nam,
13205 Invoker => Signature_Of (Invk_Id),
13206 Kind => Kind,
13207 Target => Signature_Of (Targ_Id),
13208 Update_Units => False);
13209 end Record_Invocation_Relation;
13210
13211 -----------------------------------
13212 -- Record_Simple_Invocation_Path --
13213 -----------------------------------
13214
13215 procedure Record_Simple_Invocation_Path
13216 (In_State : Processing_In_State)
13217 is
13218 package Scenarios renames Active_Scenario_Stack;
13219
13220 Last_Targ : constant Entity_Id :=
13221 Target_Of (Scenarios.Last, In_State);
13222 First_Targ : Entity_Id;
13223
13224 begin
13225 -- The path originates from the elaboration of the body. Add an extra
13226 -- relation from the elaboration body procedure to the first active
13227 -- scenario.
13228
13229 if In_State.Processing = Invocation_Body_Processing then
13230 Build_Elaborate_Body_Procedure;
13231 First_Targ := Elab_Body_Id;
13232
13233 -- The path originates from the elaboration of the spec. Add an extra
13234 -- relation from the elaboration spec procedure to the first active
13235 -- scenario.
13236
13237 elsif In_State.Processing = Invocation_Spec_Processing then
13238 Build_Elaborate_Spec_Procedure;
13239 First_Targ := Elab_Spec_Id;
13240
13241 else
13242 First_Targ := Target_Of (Scenarios.First, In_State);
13243 end if;
13244
13245 -- Record a single relation from the first to the last scenario
13246
13247 if First_Targ /= Last_Targ then
13248 Record_Invocation_Relation
13249 (Invk_Id => First_Targ,
13250 Targ_Id => Last_Targ,
13251 In_State => In_State);
13252 end if;
13253 end Record_Simple_Invocation_Path;
13254
13255 ----------------------------
13256 -- Set_Is_Saved_Construct --
13257 ----------------------------
13258
13259 procedure Set_Is_Saved_Construct
13260 (Constr : Entity_Id;
13261 Val : Boolean := True)
13262 is
13263 pragma Assert (Present (Constr));
13264
13265 begin
13266 if Val then
13267 NE_Set.Insert (Saved_Constructs_Set, Constr);
13268 else
13269 NE_Set.Delete (Saved_Constructs_Set, Constr);
13270 end if;
13271 end Set_Is_Saved_Construct;
13272
13273 ---------------------------
13274 -- Set_Is_Saved_Relation --
13275 ---------------------------
13276
13277 procedure Set_Is_Saved_Relation
13278 (Rel : Invoker_Target_Relation;
13279 Val : Boolean := True)
13280 is
13281 begin
13282 if Val then
13283 IR_Set.Insert (Saved_Relations_Set, Rel);
13284 else
13285 IR_Set.Delete (Saved_Relations_Set, Rel);
13286 end if;
13287 end Set_Is_Saved_Relation;
13288
13289 ------------------
13290 -- Signature_Of --
13291 ------------------
13292
13293 function Signature_Of (Id : Entity_Id) return Invocation_Signature_Id is
13294 Loc : constant Source_Ptr := Sloc (Id);
13295
13296 function Instantiation_Locations return Name_Id;
13297 pragma Inline (Instantiation_Locations);
13298 -- Create a concatenation of all lines and colums of each instance
13299 -- where source location Loc appears. Return No_Name if no instances
13300 -- exist.
13301
13302 function Qualified_Scope return Name_Id;
13303 pragma Inline (Qualified_Scope);
13304 -- Obtain the qualified name of Id's scope
13305
13306 -----------------------------
13307 -- Instantiation_Locations --
13308 -----------------------------
13309
13310 function Instantiation_Locations return Name_Id is
13311 Buffer : Bounded_String (2052);
13312 Inst : Source_Ptr;
13313 Loc_Nam : Name_Id;
13314 SFI : Source_File_Index;
13315
13316 begin
13317 SFI := Get_Source_File_Index (Loc);
13318 Inst := Instantiation (SFI);
13319
13320 -- The location is within an instance. Construct a concatenation
13321 -- of all lines and colums of each individual instance using the
13322 -- following format:
13323 --
13324 -- line1_column1_line2_column2_ ... _lineN_columnN
13325
13326 if Inst /= No_Location then
13327 loop
13328 Append (Buffer, Nat (Get_Logical_Line_Number (Inst)));
13329 Append (Buffer, '_');
13330 Append (Buffer, Nat (Get_Column_Number (Inst)));
13331
13332 SFI := Get_Source_File_Index (Inst);
13333 Inst := Instantiation (SFI);
13334
13335 exit when Inst = No_Location;
13336
13337 Append (Buffer, '_');
13338 end loop;
13339
13340 Loc_Nam := Name_Find (Buffer);
13341 return Loc_Nam;
13342
13343 -- Otherwise there no instances are involved
13344
13345 else
13346 return No_Name;
13347 end if;
13348 end Instantiation_Locations;
13349
13350 ---------------------
13351 -- Qualified_Scope --
13352 ---------------------
13353
13354 function Qualified_Scope return Name_Id is
13355 Scop : Entity_Id;
13356
13357 begin
13358 Scop := Scope (Id);
13359
13360 -- The entity appears within an anonymous concurrent type created
13361 -- for a single protected or task type declaration. Use the entity
13362 -- of the anonymous object as it represents the original scope.
13363
13364 if Is_Concurrent_Type (Scop)
13365 and then Present (Anonymous_Object (Scop))
13366 then
13367 Scop := Anonymous_Object (Scop);
13368 end if;
13369
13370 return Get_Qualified_Name (Scop);
13371 end Qualified_Scope;
13372
13373 -- Start of processing for Signature_Of
13374
13375 begin
13376 return
13377 Invocation_Signature_Of
13378 (Column => Nat (Get_Column_Number (Loc)),
13379 Line => Nat (Get_Logical_Line_Number (Loc)),
13380 Locations => Instantiation_Locations,
13381 Name => Chars (Id),
13382 Scope => Qualified_Scope);
13383 end Signature_Of;
13384
13385 ---------------
13386 -- Target_Of --
13387 ---------------
13388
13389 function Target_Of
13390 (Pos : Active_Scenario_Pos;
13391 In_State : Processing_In_State) return Entity_Id
13392 is
13393 package Scenarios renames Active_Scenario_Stack;
13394
13395 -- Ensure that the position is within the bounds of the active
13396 -- scenario stack.
13397
13398 pragma Assert (Scenarios.First <= Pos);
13399 pragma Assert (Pos <= Scenarios.Last);
13400
13401 Scen_Rep : constant Scenario_Rep_Id :=
13402 Scenario_Representation_Of
13403 (Scenarios.Table (Pos), In_State);
13404
13405 begin
13406 -- The true target of an activation call is the current task type
13407 -- rather than routine Activate_Tasks.
13408
13409 if Kind (Scen_Rep) = Task_Activation_Scenario then
13410 return Activated_Task_Type (Scen_Rep);
13411 else
13412 return Target (Scen_Rep);
13413 end if;
13414 end Target_Of;
13415
13416 ------------------------------
13417 -- Traverse_Invocation_Body --
13418 ------------------------------
13419
13420 procedure Traverse_Invocation_Body
13421 (N : Node_Id;
13422 In_State : Processing_In_State)
13423 is
13424 begin
13425 Traverse_Body
13426 (N => N,
13427 Requires_Processing => Is_Invocation_Scenario'Access,
13428 Processor => Process_Invocation_Scenario'Access,
13429 In_State => In_State);
13430 end Traverse_Invocation_Body;
13431
13432 ---------------------------
13433 -- Write_Invocation_Path --
13434 ---------------------------
13435
13436 procedure Write_Invocation_Path (In_State : Processing_In_State) is
13437 procedure Write_Target (Targ_Id : Entity_Id; Is_First : Boolean);
13438 pragma Inline (Write_Target);
13439 -- Write out invocation target Targ_Id to standard output. Flag
13440 -- Is_First should be set when the target is first in a path.
13441
13442 -------------
13443 -- Targ_Id --
13444 -------------
13445
13446 procedure Write_Target (Targ_Id : Entity_Id; Is_First : Boolean) is
13447 begin
13448 if not Is_First then
13449 Write_Str (" --> ");
13450 end if;
13451
13452 Write_Name (Get_Qualified_Name (Targ_Id));
13453 Write_Eol;
13454 end Write_Target;
13455
13456 -- Local variables
13457
13458 package Scenarios renames Active_Scenario_Stack;
13459
13460 First_Seen : Boolean := False;
13461
13462 -- Start of processing for Write_Invocation_Path
13463
13464 begin
13465 -- Nothing to do when flag -gnatd_T (output trace information on
13466 -- invocation path recording) is not in effect.
13467
13468 if not Debug_Flag_Underscore_TT then
13469 return;
13470 end if;
13471
13472 -- The path originates from the elaboration of the body. Write the
13473 -- elaboration body procedure.
13474
13475 if In_State.Processing = Invocation_Body_Processing then
13476 Write_Target (Elab_Body_Id, True);
13477 First_Seen := True;
13478
13479 -- The path originates from the elaboration of the spec. Write the
13480 -- elaboration spec procedure.
13481
13482 elsif In_State.Processing = Invocation_Spec_Processing then
13483 Write_Target (Elab_Spec_Id, True);
13484 First_Seen := True;
13485 end if;
13486
13487 -- Write each individual target invoked by its corresponding scenario
13488 -- on the active scenario stack.
13489
13490 for Index in Scenarios.First .. Scenarios.Last loop
13491 Write_Target
13492 (Targ_Id => Target_Of (Index, In_State),
13493 Is_First => Index = Scenarios.First and then not First_Seen);
13494 end loop;
13495
13496 Write_Eol;
13497 end Write_Invocation_Path;
13498 end Invocation_Graph;
13499
13500 ------------------------
13501 -- Is_Safe_Activation --
13502 ------------------------
13503
13504 function Is_Safe_Activation
13505 (Call : Node_Id;
13506 Task_Rep : Target_Rep_Id) return Boolean
13507 is
13508 begin
13509 -- The activation of a task coming from an external instance cannot
13510 -- cause an ABE because the generic was already instantiated. Note
13511 -- that the instantiation itself may lead to an ABE.
13512
13513 return
13514 In_External_Instance
13515 (N => Call,
13516 Target_Decl => Spec_Declaration (Task_Rep));
13517 end Is_Safe_Activation;
13518
13519 ------------------
13520 -- Is_Safe_Call --
13521 ------------------
13522
13523 function Is_Safe_Call
13524 (Call : Node_Id;
13525 Subp_Id : Entity_Id;
13526 Subp_Rep : Target_Rep_Id) return Boolean
13527 is
13528 Body_Decl : constant Node_Id := Body_Declaration (Subp_Rep);
13529 Spec_Decl : constant Node_Id := Spec_Declaration (Subp_Rep);
13530
13531 begin
13532 -- The target is either an abstract subprogram, formal subprogram, or
13533 -- imported, in which case it does not have a body at compile or bind
13534 -- time. Assume that the call is ABE-safe.
13535
13536 if Is_Bodiless_Subprogram (Subp_Id) then
13537 return True;
13538
13539 -- The target is an instantiation of a generic subprogram. The call
13540 -- cannot cause an ABE because the generic was already instantiated.
13541 -- Note that the instantiation itself may lead to an ABE.
13542
13543 elsif Is_Generic_Instance (Subp_Id) then
13544 return True;
13545
13546 -- The invocation of a target coming from an external instance cannot
13547 -- cause an ABE because the generic was already instantiated. Note that
13548 -- the instantiation itself may lead to an ABE.
13549
13550 elsif In_External_Instance
13551 (N => Call,
13552 Target_Decl => Spec_Decl)
13553 then
13554 return True;
13555
13556 -- The target is a subprogram body without a previous declaration. The
13557 -- call cannot cause an ABE because the body has already been seen.
13558
13559 elsif Nkind (Spec_Decl) = N_Subprogram_Body
13560 and then No (Corresponding_Spec (Spec_Decl))
13561 then
13562 return True;
13563
13564 -- The target is a subprogram body stub without a prior declaration.
13565 -- The call cannot cause an ABE because the proper body substitutes
13566 -- the stub.
13567
13568 elsif Nkind (Spec_Decl) = N_Subprogram_Body_Stub
13569 and then No (Corresponding_Spec_Of_Stub (Spec_Decl))
13570 then
13571 return True;
13572
13573 -- Subprogram bodies which wrap attribute references used as actuals
13574 -- in instantiations are always ABE-safe. These bodies are artifacts
13575 -- of expansion.
13576
13577 elsif Present (Body_Decl)
13578 and then Nkind (Body_Decl) = N_Subprogram_Body
13579 and then Was_Attribute_Reference (Body_Decl)
13580 then
13581 return True;
13582 end if;
13583
13584 return False;
13585 end Is_Safe_Call;
13586
13587 ---------------------------
13588 -- Is_Safe_Instantiation --
13589 ---------------------------
13590
13591 function Is_Safe_Instantiation
13592 (Inst : Node_Id;
13593 Gen_Id : Entity_Id;
13594 Gen_Rep : Target_Rep_Id) return Boolean
13595 is
13596 Spec_Decl : constant Node_Id := Spec_Declaration (Gen_Rep);
13597
13598 begin
13599 -- The generic is an intrinsic subprogram in which case it does not
13600 -- have a body at compile or bind time. Assume that the instantiation
13601 -- is ABE-safe.
13602
13603 if Is_Bodiless_Subprogram (Gen_Id) then
13604 return True;
13605
13606 -- The instantiation of an external nested generic cannot cause an ABE
13607 -- if the outer generic was already instantiated. Note that the instance
13608 -- of the outer generic may lead to an ABE.
13609
13610 elsif In_External_Instance
13611 (N => Inst,
13612 Target_Decl => Spec_Decl)
13613 then
13614 return True;
13615
13616 -- The generic is a package. The instantiation cannot cause an ABE when
13617 -- the package has no body.
13618
13619 elsif Ekind (Gen_Id) = E_Generic_Package
13620 and then not Has_Body (Spec_Decl)
13621 then
13622 return True;
13623 end if;
13624
13625 return False;
13626 end Is_Safe_Instantiation;
13627
13628 ------------------
13629 -- Is_Same_Unit --
13630 ------------------
13631
13632 function Is_Same_Unit
13633 (Unit_1 : Entity_Id;
13634 Unit_2 : Entity_Id) return Boolean
13635 is
13636 begin
13637 return Unit_Entity (Unit_1) = Unit_Entity (Unit_2);
13638 end Is_Same_Unit;
13639
13640 -------------------------------
13641 -- Kill_Elaboration_Scenario --
13642 -------------------------------
13643
13644 procedure Kill_Elaboration_Scenario (N : Node_Id) is
13645 begin
13646 -- Nothing to do when switch -gnatH (legacy elaboration checking mode
13647 -- enabled) is in effect because the legacy ABE lechanism does not need
13648 -- to carry out this action.
13649
13650 if Legacy_Elaboration_Checks then
13651 return;
13652
13653 -- Nothing to do when the elaboration phase of the compiler is not
13654 -- active.
13655
13656 elsif not Elaboration_Phase_Active then
13657 return;
13658 end if;
13659
13660 -- Eliminate a recorded scenario when it appears within dead code
13661 -- because it will not be executed at elaboration time.
13662
13663 if Is_Scenario (N) then
13664 Delete_Scenario (N);
13665 end if;
13666 end Kill_Elaboration_Scenario;
13667
13668 ----------------------
13669 -- Main_Unit_Entity --
13670 ----------------------
13671
13672 function Main_Unit_Entity return Entity_Id is
13673 begin
13674 -- Note that Cunit_Entity (Main_Unit) is not reliable in the presence of
13675 -- generic bodies and may return an outdated entity.
13676
13677 return Defining_Entity (Unit (Cunit (Main_Unit)));
13678 end Main_Unit_Entity;
13679
13680 ----------------------
13681 -- Non_Private_View --
13682 ----------------------
13683
13684 function Non_Private_View (Typ : Entity_Id) return Entity_Id is
13685 begin
13686 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
13687 return Full_View (Typ);
13688 else
13689 return Typ;
13690 end if;
13691 end Non_Private_View;
13692
13693 ---------------------------------
13694 -- Record_Elaboration_Scenario --
13695 ---------------------------------
13696
13697 procedure Record_Elaboration_Scenario (N : Node_Id) is
13698 procedure Check_Preelaborated_Call
13699 (Call : Node_Id;
13700 Call_Lvl : Enclosing_Level_Kind);
13701 pragma Inline (Check_Preelaborated_Call);
13702 -- Verify that entry, operator, or subprogram call Call with enclosing
13703 -- level Call_Lvl does not appear at the library level of preelaborated
13704 -- unit.
13705
13706 function Find_Code_Unit (Nod : Node_Or_Entity_Id) return Entity_Id;
13707 pragma Inline (Find_Code_Unit);
13708 -- Return the code unit which contains arbitrary node or entity Nod.
13709 -- This is the unit of the file which physically contains the related
13710 -- construct denoted by Nod except when Nod is within an instantiation.
13711 -- In that case the unit is that of the top-level instantiation.
13712
13713 function In_Preelaborated_Context (Nod : Node_Id) return Boolean;
13714 pragma Inline (In_Preelaborated_Context);
13715 -- Determine whether arbitrary node Nod appears within a preelaborated
13716 -- context.
13717
13718 procedure Record_Access_Taken
13719 (Attr : Node_Id;
13720 Attr_Lvl : Enclosing_Level_Kind);
13721 pragma Inline (Record_Access_Taken);
13722 -- Record 'Access scenario Attr with enclosing level Attr_Lvl
13723
13724 procedure Record_Call_Or_Task_Activation
13725 (Call : Node_Id;
13726 Call_Lvl : Enclosing_Level_Kind);
13727 pragma Inline (Record_Call_Or_Task_Activation);
13728 -- Record call scenario Call with enclosing level Call_Lvl
13729
13730 procedure Record_Instantiation
13731 (Inst : Node_Id;
13732 Inst_Lvl : Enclosing_Level_Kind);
13733 pragma Inline (Record_Instantiation);
13734 -- Record instantiation scenario Inst with enclosing level Inst_Lvl
13735
13736 procedure Record_Variable_Assignment
13737 (Asmt : Node_Id;
13738 Asmt_Lvl : Enclosing_Level_Kind);
13739 pragma Inline (Record_Variable_Assignment);
13740 -- Record variable assignment scenario Asmt with enclosing level
13741 -- Asmt_Lvl.
13742
13743 procedure Record_Variable_Reference
13744 (Ref : Node_Id;
13745 Ref_Lvl : Enclosing_Level_Kind);
13746 pragma Inline (Record_Variable_Reference);
13747 -- Record variable reference scenario Ref with enclosing level Ref_Lvl
13748
13749 ------------------------------
13750 -- Check_Preelaborated_Call --
13751 ------------------------------
13752
13753 procedure Check_Preelaborated_Call
13754 (Call : Node_Id;
13755 Call_Lvl : Enclosing_Level_Kind)
13756 is
13757 begin
13758 -- Nothing to do when the call is internally generated because it is
13759 -- assumed that it will never violate preelaboration.
13760
13761 if not Is_Source_Call (Call) then
13762 return;
13763
13764 -- Library-level calls are always considered because they are part of
13765 -- the associated unit's elaboration actions.
13766
13767 elsif Call_Lvl in Library_Level then
13768 null;
13769
13770 -- Calls at the library level of a generic package body have to be
13771 -- checked because they would render an instantiation illegal if the
13772 -- template is marked as preelaborated. Note that this does not apply
13773 -- to calls at the library level of a generic package spec.
13774
13775 elsif Call_Lvl = Generic_Body_Level then
13776 null;
13777
13778 -- Otherwise the call does not appear at the proper level and must
13779 -- not be considered for this check.
13780
13781 else
13782 return;
13783 end if;
13784
13785 -- The call appears within a preelaborated unit. Emit a warning only
13786 -- for internal uses, otherwise this is an error.
13787
13788 if In_Preelaborated_Context (Call) then
13789 Error_Msg_Warn := GNAT_Mode;
13790 Error_Msg_N
13791 ("<<non-static call not allowed in preelaborated unit", Call);
13792 end if;
13793 end Check_Preelaborated_Call;
13794
13795 --------------------
13796 -- Find_Code_Unit --
13797 --------------------
13798
13799 function Find_Code_Unit (Nod : Node_Or_Entity_Id) return Entity_Id is
13800 begin
13801 return Find_Unit_Entity (Unit (Cunit (Get_Code_Unit (Nod))));
13802 end Find_Code_Unit;
13803
13804 ------------------------------
13805 -- In_Preelaborated_Context --
13806 ------------------------------
13807
13808 function In_Preelaborated_Context (Nod : Node_Id) return Boolean is
13809 Body_Id : constant Entity_Id := Find_Code_Unit (Nod);
13810 Spec_Id : constant Entity_Id := Unique_Entity (Body_Id);
13811
13812 begin
13813 -- The node appears within a package body whose corresponding spec is
13814 -- subject to pragma Remote_Call_Interface or Remote_Types. This does
13815 -- not result in a preelaborated context because the package body may
13816 -- be on another machine.
13817
13818 if Ekind (Body_Id) = E_Package_Body
13819 and then Ekind_In (Spec_Id, E_Generic_Package, E_Package)
13820 and then (Is_Remote_Call_Interface (Spec_Id)
13821 or else Is_Remote_Types (Spec_Id))
13822 then
13823 return False;
13824
13825 -- Otherwise the node appears within a preelaborated context when the
13826 -- associated unit is preelaborated.
13827
13828 else
13829 return Is_Preelaborated_Unit (Spec_Id);
13830 end if;
13831 end In_Preelaborated_Context;
13832
13833 -------------------------
13834 -- Record_Access_Taken --
13835 -------------------------
13836
13837 procedure Record_Access_Taken
13838 (Attr : Node_Id;
13839 Attr_Lvl : Enclosing_Level_Kind)
13840 is
13841 begin
13842 -- Signal any enclosing local exception handlers that the 'Access may
13843 -- raise Program_Error due to a failed ABE check when switch -gnatd.o
13844 -- (conservative elaboration order for indirect calls) is in effect.
13845 -- Marking the exception handlers ensures proper expansion by both
13846 -- the front and back end restriction when No_Exception_Propagation
13847 -- is in effect.
13848
13849 if Debug_Flag_Dot_O then
13850 Possible_Local_Raise (Attr, Standard_Program_Error);
13851 end if;
13852
13853 -- Add 'Access to the appropriate set
13854
13855 if Attr_Lvl = Library_Body_Level then
13856 Add_Library_Body_Scenario (Attr);
13857
13858 elsif Attr_Lvl = Library_Spec_Level
13859 or else Attr_Lvl = Instantiation_Level
13860 then
13861 Add_Library_Spec_Scenario (Attr);
13862 end if;
13863
13864 -- 'Access requires a conditional ABE check when the dynamic model is
13865 -- in effect.
13866
13867 Add_Dynamic_ABE_Check_Scenario (Attr);
13868 end Record_Access_Taken;
13869
13870 ------------------------------------
13871 -- Record_Call_Or_Task_Activation --
13872 ------------------------------------
13873
13874 procedure Record_Call_Or_Task_Activation
13875 (Call : Node_Id;
13876 Call_Lvl : Enclosing_Level_Kind)
13877 is
13878 begin
13879 -- Signal any enclosing local exception handlers that the call may
13880 -- raise Program_Error due to failed ABE check. Marking the exception
13881 -- handlers ensures proper expansion by both the front and back end
13882 -- restriction when No_Exception_Propagation is in effect.
13883
13884 Possible_Local_Raise (Call, Standard_Program_Error);
13885
13886 -- Perform early detection of guaranteed ABEs in order to suppress
13887 -- the instantiation of generic bodies because gigi cannot handle
13888 -- certain types of premature instantiations.
13889
13890 Process_Guaranteed_ABE
13891 (N => Call,
13892 In_State => Guaranteed_ABE_State);
13893
13894 -- Add the call or task activation to the appropriate set
13895
13896 if Call_Lvl = Declaration_Level then
13897 Add_Declaration_Scenario (Call);
13898
13899 elsif Call_Lvl = Library_Body_Level then
13900 Add_Library_Body_Scenario (Call);
13901
13902 elsif Call_Lvl = Library_Spec_Level
13903 or else Call_Lvl = Instantiation_Level
13904 then
13905 Add_Library_Spec_Scenario (Call);
13906 end if;
13907
13908 -- A call or a task activation requires a conditional ABE check when
13909 -- the dynamic model is in effect.
13910
13911 Add_Dynamic_ABE_Check_Scenario (Call);
13912 end Record_Call_Or_Task_Activation;
13913
13914 --------------------------
13915 -- Record_Instantiation --
13916 --------------------------
13917
13918 procedure Record_Instantiation
13919 (Inst : Node_Id;
13920 Inst_Lvl : Enclosing_Level_Kind)
13921 is
13922 begin
13923 -- Signal enclosing local exception handlers that instantiation may
13924 -- raise Program_Error due to failed ABE check. Marking the exception
13925 -- handlers ensures proper expansion by both the front and back end
13926 -- restriction when No_Exception_Propagation is in effect.
13927
13928 Possible_Local_Raise (Inst, Standard_Program_Error);
13929
13930 -- Perform early detection of guaranteed ABEs in order to suppress
13931 -- the instantiation of generic bodies because gigi cannot handle
13932 -- certain types of premature instantiations.
13933
13934 Process_Guaranteed_ABE
13935 (N => Inst,
13936 In_State => Guaranteed_ABE_State);
13937
13938 -- Add the instantiation to the appropriate set
13939
13940 if Inst_Lvl = Declaration_Level then
13941 Add_Declaration_Scenario (Inst);
13942
13943 elsif Inst_Lvl = Library_Body_Level then
13944 Add_Library_Body_Scenario (Inst);
13945
13946 elsif Inst_Lvl = Library_Spec_Level
13947 or else Inst_Lvl = Instantiation_Level
13948 then
13949 Add_Library_Spec_Scenario (Inst);
13950 end if;
13951
13952 -- Instantiations of generics subject to SPARK_Mode On require
13953 -- elaboration-related checks even though the instantiations may
13954 -- not appear within elaboration code.
13955
13956 if Is_Suitable_SPARK_Instantiation (Inst) then
13957 Add_SPARK_Scenario (Inst);
13958 end if;
13959
13960 -- An instantiation requires a conditional ABE check when the dynamic
13961 -- model is in effect.
13962
13963 Add_Dynamic_ABE_Check_Scenario (Inst);
13964 end Record_Instantiation;
13965
13966 --------------------------------
13967 -- Record_Variable_Assignment --
13968 --------------------------------
13969
13970 procedure Record_Variable_Assignment
13971 (Asmt : Node_Id;
13972 Asmt_Lvl : Enclosing_Level_Kind)
13973 is
13974 begin
13975 -- Add the variable assignment to the appropriate set
13976
13977 if Asmt_Lvl = Library_Body_Level then
13978 Add_Library_Body_Scenario (Asmt);
13979
13980 elsif Asmt_Lvl = Library_Spec_Level
13981 or else Asmt_Lvl = Instantiation_Level
13982 then
13983 Add_Library_Spec_Scenario (Asmt);
13984 end if;
13985 end Record_Variable_Assignment;
13986
13987 -------------------------------
13988 -- Record_Variable_Reference --
13989 -------------------------------
13990
13991 procedure Record_Variable_Reference
13992 (Ref : Node_Id;
13993 Ref_Lvl : Enclosing_Level_Kind)
13994 is
13995 begin
13996 -- Add the variable reference to the appropriate set
13997
13998 if Ref_Lvl = Library_Body_Level then
13999 Add_Library_Body_Scenario (Ref);
14000
14001 elsif Ref_Lvl = Library_Spec_Level
14002 or else Ref_Lvl = Instantiation_Level
14003 then
14004 Add_Library_Spec_Scenario (Ref);
14005 end if;
14006 end Record_Variable_Reference;
14007
14008 -- Local variables
14009
14010 Scen : constant Node_Id := Scenario (N);
14011 Scen_Lvl : Enclosing_Level_Kind;
14012
14013 -- Start of processing for Record_Elaboration_Scenario
14014
14015 begin
14016 -- Nothing to do when switch -gnatH (legacy elaboration checking mode
14017 -- enabled) is in effect because the legacy ABE mechanism does not need
14018 -- to carry out this action.
14019
14020 if Legacy_Elaboration_Checks then
14021 return;
14022
14023 -- Nothing to do for ASIS because ABE checks and diagnostics are not
14024 -- performed in this mode.
14025
14026 elsif ASIS_Mode then
14027 return;
14028
14029 -- Nothing to do when the scenario is being preanalyzed
14030
14031 elsif Preanalysis_Active then
14032 return;
14033
14034 -- Nothing to do when the elaboration phase of the compiler is not
14035 -- active.
14036
14037 elsif not Elaboration_Phase_Active then
14038 return;
14039 end if;
14040
14041 Scen_Lvl := Find_Enclosing_Level (Scen);
14042
14043 -- Ensure that a library-level call does not appear in a preelaborated
14044 -- unit. The check must come before ignoring scenarios within external
14045 -- units or inside generics because calls in those context must also be
14046 -- verified.
14047
14048 if Is_Suitable_Call (Scen) then
14049 Check_Preelaborated_Call (Scen, Scen_Lvl);
14050 end if;
14051
14052 -- Nothing to do when the scenario does not appear within the main unit
14053
14054 if not In_Main_Context (Scen) then
14055 return;
14056
14057 -- Nothing to do when the scenario appears within a generic
14058
14059 elsif Inside_A_Generic then
14060 return;
14061
14062 -- 'Access
14063
14064 elsif Is_Suitable_Access_Taken (Scen) then
14065 Record_Access_Taken
14066 (Attr => Scen,
14067 Attr_Lvl => Scen_Lvl);
14068
14069 -- Call or task activation
14070
14071 elsif Is_Suitable_Call (Scen) then
14072 Record_Call_Or_Task_Activation
14073 (Call => Scen,
14074 Call_Lvl => Scen_Lvl);
14075
14076 -- Derived type declaration
14077
14078 elsif Is_Suitable_SPARK_Derived_Type (Scen) then
14079 Add_SPARK_Scenario (Scen);
14080
14081 -- Instantiation
14082
14083 elsif Is_Suitable_Instantiation (Scen) then
14084 Record_Instantiation
14085 (Inst => Scen,
14086 Inst_Lvl => Scen_Lvl);
14087
14088 -- Refined_State pragma
14089
14090 elsif Is_Suitable_SPARK_Refined_State_Pragma (Scen) then
14091 Add_SPARK_Scenario (Scen);
14092
14093 -- Variable assignment
14094
14095 elsif Is_Suitable_Variable_Assignment (Scen) then
14096 Record_Variable_Assignment
14097 (Asmt => Scen,
14098 Asmt_Lvl => Scen_Lvl);
14099
14100 -- Variable reference
14101
14102 elsif Is_Suitable_Variable_Reference (Scen) then
14103 Record_Variable_Reference
14104 (Ref => Scen,
14105 Ref_Lvl => Scen_Lvl);
14106 end if;
14107 end Record_Elaboration_Scenario;
14108
14109 --------------
14110 -- Scenario --
14111 --------------
14112
14113 function Scenario (N : Node_Id) return Node_Id is
14114 Orig_N : constant Node_Id := Original_Node (N);
14115
14116 begin
14117 -- An expanded instantiation is rewritten into a spec-body pair where
14118 -- N denotes the spec. In this case the original instantiation is the
14119 -- proper elaboration scenario.
14120
14121 if Nkind (Orig_N) in N_Generic_Instantiation then
14122 return Orig_N;
14123
14124 -- Otherwise the scenario is already in its proper form
14125
14126 else
14127 return N;
14128 end if;
14129 end Scenario;
14130
14131 ----------------------
14132 -- Scenario_Storage --
14133 ----------------------
14134
14135 package body Scenario_Storage is
14136
14137 ---------------------
14138 -- Data structures --
14139 ---------------------
14140
14141 -- The following sets store all scenarios
14142
14143 Declaration_Scenarios : NE_Set.Membership_Set := NE_Set.Nil;
14144 Dynamic_ABE_Check_Scenarios : NE_Set.Membership_Set := NE_Set.Nil;
14145 Library_Body_Scenarios : NE_Set.Membership_Set := NE_Set.Nil;
14146 Library_Spec_Scenarios : NE_Set.Membership_Set := NE_Set.Nil;
14147 SPARK_Scenarios : NE_Set.Membership_Set := NE_Set.Nil;
14148
14149 -------------------------------
14150 -- Finalize_Scenario_Storage --
14151 -------------------------------
14152
14153 procedure Finalize_Scenario_Storage is
14154 begin
14155 NE_Set.Destroy (Declaration_Scenarios);
14156 NE_Set.Destroy (Dynamic_ABE_Check_Scenarios);
14157 NE_Set.Destroy (Library_Body_Scenarios);
14158 NE_Set.Destroy (Library_Spec_Scenarios);
14159 NE_Set.Destroy (SPARK_Scenarios);
14160 end Finalize_Scenario_Storage;
14161
14162 ---------------------------------
14163 -- Initialize_Scenario_Storage --
14164 ---------------------------------
14165
14166 procedure Initialize_Scenario_Storage is
14167 begin
14168 Declaration_Scenarios := NE_Set.Create (1000);
14169 Dynamic_ABE_Check_Scenarios := NE_Set.Create (500);
14170 Library_Body_Scenarios := NE_Set.Create (1000);
14171 Library_Spec_Scenarios := NE_Set.Create (1000);
14172 SPARK_Scenarios := NE_Set.Create (100);
14173 end Initialize_Scenario_Storage;
14174
14175 ------------------------------
14176 -- Add_Declaration_Scenario --
14177 ------------------------------
14178
14179 procedure Add_Declaration_Scenario (N : Node_Id) is
14180 pragma Assert (Present (N));
14181 begin
14182 NE_Set.Insert (Declaration_Scenarios, N);
14183 end Add_Declaration_Scenario;
14184
14185 ------------------------------------
14186 -- Add_Dynamic_ABE_Check_Scenario --
14187 ------------------------------------
14188
14189 procedure Add_Dynamic_ABE_Check_Scenario (N : Node_Id) is
14190 pragma Assert (Present (N));
14191
14192 begin
14193 if not Check_Or_Failure_Generation_OK then
14194 return;
14195
14196 -- Nothing to do if the dynamic model is not in effect
14197
14198 elsif not Dynamic_Elaboration_Checks then
14199 return;
14200 end if;
14201
14202 NE_Set.Insert (Dynamic_ABE_Check_Scenarios, N);
14203 end Add_Dynamic_ABE_Check_Scenario;
14204
14205 -------------------------------
14206 -- Add_Library_Body_Scenario --
14207 -------------------------------
14208
14209 procedure Add_Library_Body_Scenario (N : Node_Id) is
14210 pragma Assert (Present (N));
14211 begin
14212 NE_Set.Insert (Library_Body_Scenarios, N);
14213 end Add_Library_Body_Scenario;
14214
14215 -------------------------------
14216 -- Add_Library_Spec_Scenario --
14217 -------------------------------
14218
14219 procedure Add_Library_Spec_Scenario (N : Node_Id) is
14220 pragma Assert (Present (N));
14221 begin
14222 NE_Set.Insert (Library_Spec_Scenarios, N);
14223 end Add_Library_Spec_Scenario;
14224
14225 ------------------------
14226 -- Add_SPARK_Scenario --
14227 ------------------------
14228
14229 procedure Add_SPARK_Scenario (N : Node_Id) is
14230 pragma Assert (Present (N));
14231 begin
14232 NE_Set.Insert (SPARK_Scenarios, N);
14233 end Add_SPARK_Scenario;
14234
14235 ---------------------
14236 -- Delete_Scenario --
14237 ---------------------
14238
14239 procedure Delete_Scenario (N : Node_Id) is
14240 pragma Assert (Present (N));
14241
14242 begin
14243 -- Delete the scenario from whichever set it belongs to
14244
14245 NE_Set.Delete (Declaration_Scenarios, N);
14246 NE_Set.Delete (Dynamic_ABE_Check_Scenarios, N);
14247 NE_Set.Delete (Library_Body_Scenarios, N);
14248 NE_Set.Delete (Library_Spec_Scenarios, N);
14249 NE_Set.Delete (SPARK_Scenarios, N);
14250 end Delete_Scenario;
14251
14252 -----------------------------------
14253 -- Iterate_Declaration_Scenarios --
14254 -----------------------------------
14255
14256 function Iterate_Declaration_Scenarios return NE_Set.Iterator is
14257 begin
14258 return NE_Set.Iterate (Declaration_Scenarios);
14259 end Iterate_Declaration_Scenarios;
14260
14261 -----------------------------------------
14262 -- Iterate_Dynamic_ABE_Check_Scenarios --
14263 -----------------------------------------
14264
14265 function Iterate_Dynamic_ABE_Check_Scenarios return NE_Set.Iterator is
14266 begin
14267 return NE_Set.Iterate (Dynamic_ABE_Check_Scenarios);
14268 end Iterate_Dynamic_ABE_Check_Scenarios;
14269
14270 ------------------------------------
14271 -- Iterate_Library_Body_Scenarios --
14272 ------------------------------------
14273
14274 function Iterate_Library_Body_Scenarios return NE_Set.Iterator is
14275 begin
14276 return NE_Set.Iterate (Library_Body_Scenarios);
14277 end Iterate_Library_Body_Scenarios;
14278
14279 ------------------------------------
14280 -- Iterate_Library_Spec_Scenarios --
14281 ------------------------------------
14282
14283 function Iterate_Library_Spec_Scenarios return NE_Set.Iterator is
14284 begin
14285 return NE_Set.Iterate (Library_Spec_Scenarios);
14286 end Iterate_Library_Spec_Scenarios;
14287
14288 -----------------------------
14289 -- Iterate_SPARK_Scenarios --
14290 -----------------------------
14291
14292 function Iterate_SPARK_Scenarios return NE_Set.Iterator is
14293 begin
14294 return NE_Set.Iterate (SPARK_Scenarios);
14295 end Iterate_SPARK_Scenarios;
14296
14297 ----------------------
14298 -- Replace_Scenario --
14299 ----------------------
14300
14301 procedure Replace_Scenario (Old_N : Node_Id; New_N : Node_Id) is
14302 procedure Replace_Scenario_In (Scenarios : NE_Set.Membership_Set);
14303 -- Determine whether scenario Old_N is present in set Scenarios, and
14304 -- if this is the case it, replace it with New_N.
14305
14306 -------------------------
14307 -- Replace_Scenario_In --
14308 -------------------------
14309
14310 procedure Replace_Scenario_In (Scenarios : NE_Set.Membership_Set) is
14311 begin
14312 -- The set is intentionally checked for existance because node
14313 -- rewriting may occur after Sem_Elab has verified all scenarios
14314 -- and data structures have been destroyed.
14315
14316 if NE_Set.Present (Scenarios)
14317 and then NE_Set.Contains (Scenarios, Old_N)
14318 then
14319 NE_Set.Delete (Scenarios, Old_N);
14320 NE_Set.Insert (Scenarios, New_N);
14321 end if;
14322 end Replace_Scenario_In;
14323
14324 -- Start of processing for Replace_Scenario
14325
14326 begin
14327 Replace_Scenario_In (Declaration_Scenarios);
14328 Replace_Scenario_In (Dynamic_ABE_Check_Scenarios);
14329 Replace_Scenario_In (Library_Body_Scenarios);
14330 Replace_Scenario_In (Library_Spec_Scenarios);
14331 Replace_Scenario_In (SPARK_Scenarios);
14332 end Replace_Scenario;
14333 end Scenario_Storage;
14334
14335 ---------------
14336 -- Semantics --
14337 ---------------
14338
14339 package body Semantics is
14340
14341 --------------------------------
14342 -- Is_Accept_Alternative_Proc --
14343 --------------------------------
14344
14345 function Is_Accept_Alternative_Proc (Id : Entity_Id) return Boolean is
14346 begin
14347 -- To qualify, the entity must denote a procedure with a receiving
14348 -- entry.
14349
14350 return
14351 Ekind (Id) = E_Procedure and then Present (Receiving_Entry (Id));
14352 end Is_Accept_Alternative_Proc;
14353
14354 ------------------------
14355 -- Is_Activation_Proc --
14356 ------------------------
14357
14358 function Is_Activation_Proc (Id : Entity_Id) return Boolean is
14359 begin
14360 -- To qualify, the entity must denote one of the runtime procedures
14361 -- in charge of task activation.
14362
14363 if Ekind (Id) = E_Procedure then
14364 if Restricted_Profile then
14365 return Is_RTE (Id, RE_Activate_Restricted_Tasks);
14366 else
14367 return Is_RTE (Id, RE_Activate_Tasks);
14368 end if;
14369 end if;
14370
14371 return False;
14372 end Is_Activation_Proc;
14373
14374 ----------------------------
14375 -- Is_Ada_Semantic_Target --
14376 ----------------------------
14377
14378 function Is_Ada_Semantic_Target (Id : Entity_Id) return Boolean is
14379 begin
14380 return
14381 Is_Activation_Proc (Id)
14382 or else Is_Controlled_Proc (Id, Name_Adjust)
14383 or else Is_Controlled_Proc (Id, Name_Finalize)
14384 or else Is_Controlled_Proc (Id, Name_Initialize)
14385 or else Is_Init_Proc (Id)
14386 or else Is_Invariant_Proc (Id)
14387 or else Is_Protected_Entry (Id)
14388 or else Is_Protected_Subp (Id)
14389 or else Is_Protected_Body_Subp (Id)
14390 or else Is_Subprogram_Inst (Id)
14391 or else Is_Task_Entry (Id);
14392 end Is_Ada_Semantic_Target;
14393
14394 --------------------------------
14395 -- Is_Assertion_Pragma_Target --
14396 --------------------------------
14397
14398 function Is_Assertion_Pragma_Target (Id : Entity_Id) return Boolean is
14399 begin
14400 return
14401 Is_Default_Initial_Condition_Proc (Id)
14402 or else Is_Initial_Condition_Proc (Id)
14403 or else Is_Invariant_Proc (Id)
14404 or else Is_Partial_Invariant_Proc (Id)
14405 or else Is_Postconditions_Proc (Id);
14406 end Is_Assertion_Pragma_Target;
14407
14408 ----------------------------
14409 -- Is_Bodiless_Subprogram --
14410 ----------------------------
14411
14412 function Is_Bodiless_Subprogram (Subp_Id : Entity_Id) return Boolean is
14413 begin
14414 -- An abstract subprogram does not have a body
14415
14416 if Ekind_In (Subp_Id, E_Function,
14417 E_Operator,
14418 E_Procedure)
14419 and then Is_Abstract_Subprogram (Subp_Id)
14420 then
14421 return True;
14422
14423 -- A formal subprogram does not have a body
14424
14425 elsif Is_Formal_Subprogram (Subp_Id) then
14426 return True;
14427
14428 -- An imported subprogram may have a body, however it is not known at
14429 -- compile or bind time where the body resides and whether it will be
14430 -- elaborated on time.
14431
14432 elsif Is_Imported (Subp_Id) then
14433 return True;
14434 end if;
14435
14436 return False;
14437 end Is_Bodiless_Subprogram;
14438
14439 ----------------------
14440 -- Is_Bridge_Target --
14441 ----------------------
14442
14443 function Is_Bridge_Target (Id : Entity_Id) return Boolean is
14444 begin
14445 return
14446 Is_Accept_Alternative_Proc (Id)
14447 or else Is_Finalizer_Proc (Id)
14448 or else Is_Partial_Invariant_Proc (Id)
14449 or else Is_Postconditions_Proc (Id)
14450 or else Is_TSS (Id, TSS_Deep_Adjust)
14451 or else Is_TSS (Id, TSS_Deep_Finalize)
14452 or else Is_TSS (Id, TSS_Deep_Initialize);
14453 end Is_Bridge_Target;
14454
14455 ------------------------
14456 -- Is_Controlled_Proc --
14457 ------------------------
14458
14459 function Is_Controlled_Proc
14460 (Subp_Id : Entity_Id;
14461 Subp_Nam : Name_Id) return Boolean
14462 is
14463 Formal_Id : Entity_Id;
14464
14465 begin
14466 pragma Assert (Nam_In (Subp_Nam, Name_Adjust,
14467 Name_Finalize,
14468 Name_Initialize));
14469
14470 -- To qualify, the subprogram must denote a source procedure with
14471 -- name Adjust, Finalize, or Initialize where the sole formal is
14472 -- controlled.
14473
14474 if Comes_From_Source (Subp_Id)
14475 and then Ekind (Subp_Id) = E_Procedure
14476 and then Chars (Subp_Id) = Subp_Nam
14477 then
14478 Formal_Id := First_Formal (Subp_Id);
14479
14480 return
14481 Present (Formal_Id)
14482 and then Is_Controlled (Etype (Formal_Id))
14483 and then No (Next_Formal (Formal_Id));
14484 end if;
14485
14486 return False;
14487 end Is_Controlled_Proc;
14488
14489 ---------------------------------------
14490 -- Is_Default_Initial_Condition_Proc --
14491 ---------------------------------------
14492
14493 function Is_Default_Initial_Condition_Proc
14494 (Id : Entity_Id) return Boolean
14495 is
14496 begin
14497 -- To qualify, the entity must denote a Default_Initial_Condition
14498 -- procedure.
14499
14500 return Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id);
14501 end Is_Default_Initial_Condition_Proc;
14502
14503 -----------------------
14504 -- Is_Finalizer_Proc --
14505 -----------------------
14506
14507 function Is_Finalizer_Proc (Id : Entity_Id) return Boolean is
14508 begin
14509 -- To qualify, the entity must denote a _Finalizer procedure
14510
14511 return Ekind (Id) = E_Procedure and then Chars (Id) = Name_uFinalizer;
14512 end Is_Finalizer_Proc;
14513
14514 -------------------------------
14515 -- Is_Initial_Condition_Proc --
14516 -------------------------------
14517
14518 function Is_Initial_Condition_Proc (Id : Entity_Id) return Boolean is
14519 begin
14520 -- To qualify, the entity must denote an Initial_Condition procedure
14521
14522 return
14523 Ekind (Id) = E_Procedure
14524 and then Is_Initial_Condition_Procedure (Id);
14525 end Is_Initial_Condition_Proc;
14526
14527 --------------------
14528 -- Is_Initialized --
14529 --------------------
14530
14531 function Is_Initialized (Obj_Decl : Node_Id) return Boolean is
14532 begin
14533 -- To qualify, the object declaration must have an expression
14534
14535 return
14536 Present (Expression (Obj_Decl))
14537 or else Has_Init_Expression (Obj_Decl);
14538 end Is_Initialized;
14539
14540 -----------------------
14541 -- Is_Invariant_Proc --
14542 -----------------------
14543
14544 function Is_Invariant_Proc (Id : Entity_Id) return Boolean is
14545 begin
14546 -- To qualify, the entity must denote the "full" invariant procedure
14547
14548 return Ekind (Id) = E_Procedure and then Is_Invariant_Procedure (Id);
14549 end Is_Invariant_Proc;
14550
14551 ---------------------------------------
14552 -- Is_Non_Library_Level_Encapsulator --
14553 ---------------------------------------
14554
14555 function Is_Non_Library_Level_Encapsulator
14556 (N : Node_Id) return Boolean
14557 is
14558 begin
14559 case Nkind (N) is
14560 when N_Abstract_Subprogram_Declaration
14561 | N_Aspect_Specification
14562 | N_Component_Declaration
14563 | N_Entry_Body
14564 | N_Entry_Declaration
14565 | N_Expression_Function
14566 | N_Formal_Abstract_Subprogram_Declaration
14567 | N_Formal_Concrete_Subprogram_Declaration
14568 | N_Formal_Object_Declaration
14569 | N_Formal_Package_Declaration
14570 | N_Formal_Type_Declaration
14571 | N_Generic_Association
14572 | N_Implicit_Label_Declaration
14573 | N_Incomplete_Type_Declaration
14574 | N_Private_Extension_Declaration
14575 | N_Private_Type_Declaration
14576 | N_Protected_Body
14577 | N_Protected_Type_Declaration
14578 | N_Single_Protected_Declaration
14579 | N_Single_Task_Declaration
14580 | N_Subprogram_Body
14581 | N_Subprogram_Declaration
14582 | N_Task_Body
14583 | N_Task_Type_Declaration
14584 =>
14585 return True;
14586
14587 when others =>
14588 return Is_Generic_Declaration_Or_Body (N);
14589 end case;
14590 end Is_Non_Library_Level_Encapsulator;
14591
14592 -------------------------------
14593 -- Is_Partial_Invariant_Proc --
14594 -------------------------------
14595
14596 function Is_Partial_Invariant_Proc (Id : Entity_Id) return Boolean is
14597 begin
14598 -- To qualify, the entity must denote the "partial" invariant
14599 -- procedure.
14600
14601 return
14602 Ekind (Id) = E_Procedure
14603 and then Is_Partial_Invariant_Procedure (Id);
14604 end Is_Partial_Invariant_Proc;
14605
14606 ----------------------------
14607 -- Is_Postconditions_Proc --
14608 ----------------------------
14609
14610 function Is_Postconditions_Proc (Id : Entity_Id) return Boolean is
14611 begin
14612 -- To qualify, the entity must denote a _Postconditions procedure
14613
14614 return
14615 Ekind (Id) = E_Procedure and then Chars (Id) = Name_uPostconditions;
14616 end Is_Postconditions_Proc;
14617
14618 ---------------------------
14619 -- Is_Preelaborated_Unit --
14620 ---------------------------
14621
14622 function Is_Preelaborated_Unit (Id : Entity_Id) return Boolean is
14623 begin
14624 return
14625 Is_Preelaborated (Id)
14626 or else Is_Pure (Id)
14627 or else Is_Remote_Call_Interface (Id)
14628 or else Is_Remote_Types (Id)
14629 or else Is_Shared_Passive (Id);
14630 end Is_Preelaborated_Unit;
14631
14632 ------------------------
14633 -- Is_Protected_Entry --
14634 ------------------------
14635
14636 function Is_Protected_Entry (Id : Entity_Id) return Boolean is
14637 begin
14638 -- To qualify, the entity must denote an entry defined in a protected
14639 -- type.
14640
14641 return
14642 Is_Entry (Id)
14643 and then Is_Protected_Type (Non_Private_View (Scope (Id)));
14644 end Is_Protected_Entry;
14645
14646 -----------------------
14647 -- Is_Protected_Subp --
14648 -----------------------
14649
14650 function Is_Protected_Subp (Id : Entity_Id) return Boolean is
14651 begin
14652 -- To qualify, the entity must denote a subprogram defined within a
14653 -- protected type.
14654
14655 return
14656 Ekind_In (Id, E_Function, E_Procedure)
14657 and then Is_Protected_Type (Non_Private_View (Scope (Id)));
14658 end Is_Protected_Subp;
14659
14660 ----------------------------
14661 -- Is_Protected_Body_Subp --
14662 ----------------------------
14663
14664 function Is_Protected_Body_Subp (Id : Entity_Id) return Boolean is
14665 begin
14666 -- To qualify, the entity must denote a subprogram with attribute
14667 -- Protected_Subprogram set.
14668
14669 return
14670 Ekind_In (Id, E_Function, E_Procedure)
14671 and then Present (Protected_Subprogram (Id));
14672 end Is_Protected_Body_Subp;
14673
14674 -----------------
14675 -- Is_Scenario --
14676 -----------------
14677
14678 function Is_Scenario (N : Node_Id) return Boolean is
14679 begin
14680 case Nkind (N) is
14681 when N_Assignment_Statement
14682 | N_Attribute_Reference
14683 | N_Call_Marker
14684 | N_Entry_Call_Statement
14685 | N_Expanded_Name
14686 | N_Function_Call
14687 | N_Function_Instantiation
14688 | N_Identifier
14689 | N_Package_Instantiation
14690 | N_Procedure_Call_Statement
14691 | N_Procedure_Instantiation
14692 | N_Requeue_Statement
14693 =>
14694 return True;
14695
14696 when others =>
14697 return False;
14698 end case;
14699 end Is_Scenario;
14700
14701 ------------------------------
14702 -- Is_SPARK_Semantic_Target --
14703 ------------------------------
14704
14705 function Is_SPARK_Semantic_Target (Id : Entity_Id) return Boolean is
14706 begin
14707 return
14708 Is_Default_Initial_Condition_Proc (Id)
14709 or else Is_Initial_Condition_Proc (Id);
14710 end Is_SPARK_Semantic_Target;
14711
14712 ------------------------
14713 -- Is_Subprogram_Inst --
14714 ------------------------
14715
14716 function Is_Subprogram_Inst (Id : Entity_Id) return Boolean is
14717 begin
14718 -- To qualify, the entity must denote a function or a procedure which
14719 -- is hidden within an anonymous package, and is a generic instance.
14720
14721 return
14722 Ekind_In (Id, E_Function, E_Procedure)
14723 and then Is_Hidden (Id)
14724 and then Is_Generic_Instance (Id);
14725 end Is_Subprogram_Inst;
14726
14727 ------------------------------
14728 -- Is_Suitable_Access_Taken --
14729 ------------------------------
14730
14731 function Is_Suitable_Access_Taken (N : Node_Id) return Boolean is
14732 Nam : Name_Id;
14733 Pref : Node_Id;
14734 Subp_Id : Entity_Id;
14735
14736 begin
14737 -- Nothing to do when switch -gnatd.U (ignore 'Access) is in effect
14738
14739 if Debug_Flag_Dot_UU then
14740 return False;
14741
14742 -- Nothing to do when the scenario is not an attribute reference
14743
14744 elsif Nkind (N) /= N_Attribute_Reference then
14745 return False;
14746
14747 -- Nothing to do for internally-generated attributes because they are
14748 -- assumed to be ABE safe.
14749
14750 elsif not Comes_From_Source (N) then
14751 return False;
14752 end if;
14753
14754 Nam := Attribute_Name (N);
14755 Pref := Prefix (N);
14756
14757 -- Sanitize the prefix of the attribute
14758
14759 if not Is_Entity_Name (Pref) then
14760 return False;
14761
14762 elsif No (Entity (Pref)) then
14763 return False;
14764 end if;
14765
14766 Subp_Id := Entity (Pref);
14767
14768 if not Is_Subprogram_Or_Entry (Subp_Id) then
14769 return False;
14770 end if;
14771
14772 -- Traverse a possible chain of renamings to obtain the original
14773 -- entry or subprogram which the prefix may rename.
14774
14775 Subp_Id := Get_Renamed_Entity (Subp_Id);
14776
14777 -- To qualify, the attribute must meet the following prerequisites:
14778
14779 return
14780
14781 -- The prefix must denote a source entry, operator, or subprogram
14782 -- which is not imported.
14783
14784 Comes_From_Source (Subp_Id)
14785 and then Is_Subprogram_Or_Entry (Subp_Id)
14786 and then not Is_Bodiless_Subprogram (Subp_Id)
14787
14788 -- The attribute name must be one of the 'Access forms. Note that
14789 -- 'Unchecked_Access cannot apply to a subprogram.
14790
14791 and then Nam_In (Nam, Name_Access, Name_Unrestricted_Access);
14792 end Is_Suitable_Access_Taken;
14793
14794 ----------------------
14795 -- Is_Suitable_Call --
14796 ----------------------
14797
14798 function Is_Suitable_Call (N : Node_Id) return Boolean is
14799 begin
14800 -- Entry and subprogram calls are intentionally ignored because they
14801 -- may undergo expansion depending on the compilation mode, previous
14802 -- errors, generic context, etc. Call markers play the role of calls
14803 -- and provide a uniform foundation for ABE processing.
14804
14805 return Nkind (N) = N_Call_Marker;
14806 end Is_Suitable_Call;
14807
14808 -------------------------------
14809 -- Is_Suitable_Instantiation --
14810 -------------------------------
14811
14812 function Is_Suitable_Instantiation (N : Node_Id) return Boolean is
14813 Inst : constant Node_Id := Scenario (N);
14814
14815 begin
14816 -- To qualify, the instantiation must come from source
14817
14818 return
14819 Comes_From_Source (Inst)
14820 and then Nkind (Inst) in N_Generic_Instantiation;
14821 end Is_Suitable_Instantiation;
14822
14823 ------------------------------------
14824 -- Is_Suitable_SPARK_Derived_Type --
14825 ------------------------------------
14826
14827 function Is_Suitable_SPARK_Derived_Type (N : Node_Id) return Boolean is
14828 Prag : Node_Id;
14829 Typ : Entity_Id;
14830
14831 begin
14832 -- To qualify, the type declaration must denote a derived tagged type
14833 -- with primitive operations, subject to pragma SPARK_Mode On.
14834
14835 if Nkind (N) = N_Full_Type_Declaration
14836 and then Nkind (Type_Definition (N)) = N_Derived_Type_Definition
14837 then
14838 Typ := Defining_Entity (N);
14839 Prag := SPARK_Pragma (Typ);
14840
14841 return
14842 Is_Tagged_Type (Typ)
14843 and then Has_Primitive_Operations (Typ)
14844 and then Present (Prag)
14845 and then Get_SPARK_Mode_From_Annotation (Prag) = On;
14846 end if;
14847
14848 return False;
14849 end Is_Suitable_SPARK_Derived_Type;
14850
14851 -------------------------------------
14852 -- Is_Suitable_SPARK_Instantiation --
14853 -------------------------------------
14854
14855 function Is_Suitable_SPARK_Instantiation (N : Node_Id) return Boolean is
14856 Inst : constant Node_Id := Scenario (N);
14857
14858 Gen_Id : Entity_Id;
14859 Prag : Node_Id;
14860
14861 begin
14862 -- To qualify, both the instantiation and the generic must be subject
14863 -- to SPARK_Mode On.
14864
14865 if Is_Suitable_Instantiation (N) then
14866 Gen_Id := Instantiated_Generic (Inst);
14867 Prag := SPARK_Pragma (Gen_Id);
14868
14869 return
14870 Is_SPARK_Mode_On_Node (Inst)
14871 and then Present (Prag)
14872 and then Get_SPARK_Mode_From_Annotation (Prag) = On;
14873 end if;
14874
14875 return False;
14876 end Is_Suitable_SPARK_Instantiation;
14877
14878 --------------------------------------------
14879 -- Is_Suitable_SPARK_Refined_State_Pragma --
14880 --------------------------------------------
14881
14882 function Is_Suitable_SPARK_Refined_State_Pragma
14883 (N : Node_Id) return Boolean
14884 is
14885 begin
14886 -- To qualfy, the pragma must denote Refined_State
14887
14888 return
14889 Nkind (N) = N_Pragma
14890 and then Pragma_Name (N) = Name_Refined_State;
14891 end Is_Suitable_SPARK_Refined_State_Pragma;
14892
14893 -------------------------------------
14894 -- Is_Suitable_Variable_Assignment --
14895 -------------------------------------
14896
14897 function Is_Suitable_Variable_Assignment (N : Node_Id) return Boolean is
14898 N_Unit : Node_Id;
14899 N_Unit_Id : Entity_Id;
14900 Nam : Node_Id;
14901 Var_Decl : Node_Id;
14902 Var_Id : Entity_Id;
14903 Var_Unit : Node_Id;
14904 Var_Unit_Id : Entity_Id;
14905
14906 begin
14907 -- Nothing to do when the scenario is not an assignment
14908
14909 if Nkind (N) /= N_Assignment_Statement then
14910 return False;
14911
14912 -- Nothing to do for internally-generated assignments because they
14913 -- are assumed to be ABE safe.
14914
14915 elsif not Comes_From_Source (N) then
14916 return False;
14917
14918 -- Assignments are ignored in GNAT mode on the assumption that
14919 -- they are ABE-safe. This behaviour parallels that of the old
14920 -- ABE mechanism.
14921
14922 elsif GNAT_Mode then
14923 return False;
14924 end if;
14925
14926 Nam := Assignment_Target (N);
14927
14928 -- Sanitize the left hand side of the assignment
14929
14930 if not Is_Entity_Name (Nam) then
14931 return False;
14932
14933 elsif No (Entity (Nam)) then
14934 return False;
14935 end if;
14936
14937 Var_Id := Entity (Nam);
14938
14939 -- Sanitize the variable
14940
14941 if Var_Id = Any_Id then
14942 return False;
14943
14944 elsif Ekind (Var_Id) /= E_Variable then
14945 return False;
14946 end if;
14947
14948 Var_Decl := Declaration_Node (Var_Id);
14949
14950 if Nkind (Var_Decl) /= N_Object_Declaration then
14951 return False;
14952 end if;
14953
14954 N_Unit_Id := Find_Top_Unit (N);
14955 N_Unit := Unit_Declaration_Node (N_Unit_Id);
14956
14957 Var_Unit_Id := Find_Top_Unit (Var_Decl);
14958 Var_Unit := Unit_Declaration_Node (Var_Unit_Id);
14959
14960 -- To qualify, the assignment must meet the following prerequisites:
14961
14962 return
14963 Comes_From_Source (Var_Id)
14964
14965 -- The variable must be declared in the spec of compilation unit
14966 -- U.
14967
14968 and then Nkind (Var_Unit) = N_Package_Declaration
14969 and then Find_Enclosing_Level (Var_Decl) = Library_Spec_Level
14970
14971 -- The assignment must occur in the body of compilation unit U
14972
14973 and then Nkind (N_Unit) = N_Package_Body
14974 and then Present (Corresponding_Body (Var_Unit))
14975 and then Corresponding_Body (Var_Unit) = N_Unit_Id;
14976 end Is_Suitable_Variable_Assignment;
14977
14978 ------------------------------------
14979 -- Is_Suitable_Variable_Reference --
14980 ------------------------------------
14981
14982 function Is_Suitable_Variable_Reference (N : Node_Id) return Boolean is
14983 begin
14984 -- Expanded names and identifiers are intentionally ignored because
14985 -- they be folded, optimized away, etc. Variable references markers
14986 -- play the role of variable references and provide a uniform
14987 -- foundation for ABE processing.
14988
14989 return Nkind (N) = N_Variable_Reference_Marker;
14990 end Is_Suitable_Variable_Reference;
14991
14992 -------------------
14993 -- Is_Task_Entry --
14994 -------------------
14995
14996 function Is_Task_Entry (Id : Entity_Id) return Boolean is
14997 begin
14998 -- To qualify, the entity must denote an entry defined in a task type
14999
15000 return
15001 Is_Entry (Id) and then Is_Task_Type (Non_Private_View (Scope (Id)));
15002 end Is_Task_Entry;
15003
15004 ------------------------
15005 -- Is_Up_Level_Target --
15006 ------------------------
15007
15008 function Is_Up_Level_Target
15009 (Targ_Decl : Node_Id;
15010 In_State : Processing_In_State) return Boolean
15011 is
15012 Root : constant Node_Id := Root_Scenario;
15013 Root_Rep : constant Scenario_Rep_Id :=
15014 Scenario_Representation_Of (Root, In_State);
15015
15016 begin
15017 -- The root appears within the declaratons of a block statement,
15018 -- entry body, subprogram body, or task body ignoring enclosing
15019 -- packages. The root is always within the main unit.
15020
15021 if not In_State.Suppress_Up_Level_Targets
15022 and then Level (Root_Rep) = Declaration_Level
15023 then
15024 -- The target is within the main unit. It acts as an up-level
15025 -- target when it appears within a context which encloses the
15026 -- root.
15027 --
15028 -- package body Main_Unit is
15029 -- function Func ...; -- target
15030 --
15031 -- procedure Proc is
15032 -- X : ... := Func; -- root scenario
15033
15034 if In_Extended_Main_Code_Unit (Targ_Decl) then
15035 return not In_Same_Context (Root, Targ_Decl, Nested_OK => True);
15036
15037 -- Otherwise the target is external to the main unit which makes
15038 -- it an up-level target.
15039
15040 else
15041 return True;
15042 end if;
15043 end if;
15044
15045 return False;
15046 end Is_Up_Level_Target;
15047 end Semantics;
15048
15049 ---------------------------
15050 -- Set_Elaboration_Phase --
15051 ---------------------------
15052
15053 procedure Set_Elaboration_Phase (Status : Elaboration_Phase_Status) is
15054 begin
15055 Elaboration_Phase := Status;
15056 end Set_Elaboration_Phase;
15057
15058 ---------------------
15059 -- SPARK_Processor --
15060 ---------------------
15061
15062 package body SPARK_Processor is
15063
15064 -----------------------
15065 -- Local subprograms --
15066 -----------------------
15067
15068 procedure Process_SPARK_Derived_Type
15069 (Typ_Decl : Node_Id;
15070 Typ_Rep : Scenario_Rep_Id;
15071 In_State : Processing_In_State);
15072 pragma Inline (Process_SPARK_Derived_Type);
15073 -- Verify that the freeze node of a derived type denoted by declaration
15074 -- Typ_Decl is within the early call region of each overriding primitive
15075 -- body that belongs to the derived type (SPARK RM 7.7(8)). Typ_Rep is
15076 -- the representation of the type. In_State denotes the current state of
15077 -- the Processing phase.
15078
15079 procedure Process_SPARK_Instantiation
15080 (Inst : Node_Id;
15081 Inst_Rep : Scenario_Rep_Id;
15082 In_State : Processing_In_State);
15083 pragma Inline (Process_SPARK_Instantiation);
15084 -- Verify that instanciation Inst does not precede the generic body it
15085 -- instantiates (SPARK RM 7.7(6)). Inst_Rep is the representation of the
15086 -- instantiation. In_State is the current state of the Processing phase.
15087
15088 procedure Process_SPARK_Refined_State_Pragma
15089 (Prag : Node_Id;
15090 Prag_Rep : Scenario_Rep_Id;
15091 In_State : Processing_In_State);
15092 pragma Inline (Process_SPARK_Refined_State_Pragma);
15093 -- Verify that each constituent of Refined_State pragma Prag which
15094 -- belongs to abstract state mentioned in pragma Initializes has prior
15095 -- elaboration with respect to the main unit (SPARK RM 7.7.1(7)).
15096 -- Prag_Rep is the representation of the pragma. In_State denotes the
15097 -- current state of the Processing phase.
15098
15099 procedure Process_SPARK_Scenario
15100 (N : Node_Id;
15101 In_State : Processing_In_State);
15102 pragma Inline (Process_SPARK_Scenario);
15103 -- Top-level dispatcher for verifying SPARK scenarios which are not
15104 -- always executable during elaboration but still need elaboration-
15105 -- related checks. In_State is the current state of the Processing
15106 -- phase.
15107
15108 ---------------------------------
15109 -- Check_SPARK_Model_In_Effect --
15110 ---------------------------------
15111
15112 SPARK_Model_Warning_Posted : Boolean := False;
15113 -- This flag prevents the same SPARK model-related warning from being
15114 -- emitted multiple times.
15115
15116 procedure Check_SPARK_Model_In_Effect is
15117 Spec_Id : constant Entity_Id := Unique_Entity (Main_Unit_Entity);
15118
15119 begin
15120 -- Do not emit the warning multiple times as this creates useless
15121 -- noise.
15122
15123 if SPARK_Model_Warning_Posted then
15124 null;
15125
15126 -- SPARK rule verification requires the "strict" static model
15127
15128 elsif Static_Elaboration_Checks
15129 and not Relaxed_Elaboration_Checks
15130 then
15131 null;
15132
15133 -- Any other combination of models does not guarantee the absence of
15134 -- ABE problems for SPARK rule verification purposes. Note that there
15135 -- is no need to check for the presence of the legacy ABE mechanism
15136 -- because the legacy code has its own dedicated processing for SPARK
15137 -- rules.
15138
15139 else
15140 SPARK_Model_Warning_Posted := True;
15141
15142 Error_Msg_N
15143 ("??SPARK elaboration checks require static elaboration model",
15144 Spec_Id);
15145
15146 if Dynamic_Elaboration_Checks then
15147 Error_Msg_N
15148 ("\dynamic elaboration model is in effect", Spec_Id);
15149
15150 else
15151 pragma Assert (Relaxed_Elaboration_Checks);
15152 Error_Msg_N
15153 ("\relaxed elaboration model is in effect", Spec_Id);
15154 end if;
15155 end if;
15156 end Check_SPARK_Model_In_Effect;
15157
15158 ---------------------------
15159 -- Check_SPARK_Scenarios --
15160 ---------------------------
15161
15162 procedure Check_SPARK_Scenarios is
15163 Iter : NE_Set.Iterator;
15164 N : Node_Id;
15165
15166 begin
15167 Iter := Iterate_SPARK_Scenarios;
15168 while NE_Set.Has_Next (Iter) loop
15169 NE_Set.Next (Iter, N);
15170
15171 Process_SPARK_Scenario
15172 (N => N,
15173 In_State => SPARK_State);
15174 end loop;
15175 end Check_SPARK_Scenarios;
15176
15177 --------------------------------
15178 -- Process_SPARK_Derived_Type --
15179 --------------------------------
15180
15181 procedure Process_SPARK_Derived_Type
15182 (Typ_Decl : Node_Id;
15183 Typ_Rep : Scenario_Rep_Id;
15184 In_State : Processing_In_State)
15185 is
15186 pragma Unreferenced (In_State);
15187
15188 Typ : constant Entity_Id := Target (Typ_Rep);
15189
15190 Stop_Check : exception;
15191 -- This exception is raised when the freeze node violates the
15192 -- placement rules.
15193
15194 procedure Check_Overriding_Primitive
15195 (Prim : Entity_Id;
15196 FNode : Node_Id);
15197 pragma Inline (Check_Overriding_Primitive);
15198 -- Verify that freeze node FNode is within the early call region of
15199 -- overriding primitive Prim's body.
15200
15201 function Freeze_Node_Location (FNode : Node_Id) return Source_Ptr;
15202 pragma Inline (Freeze_Node_Location);
15203 -- Return a more accurate source location associated with freeze node
15204 -- FNode.
15205
15206 function Precedes_Source_Construct (N : Node_Id) return Boolean;
15207 pragma Inline (Precedes_Source_Construct);
15208 -- Determine whether arbitrary node N appears prior to some source
15209 -- construct.
15210
15211 procedure Suggest_Elaborate_Body
15212 (N : Node_Id;
15213 Body_Decl : Node_Id;
15214 Error_Nod : Node_Id);
15215 pragma Inline (Suggest_Elaborate_Body);
15216 -- Suggest the use of pragma Elaborate_Body when the pragma will
15217 -- allow for node N to appear within the early call region of
15218 -- subprogram body Body_Decl. The suggestion is attached to
15219 -- Error_Nod as a continuation error.
15220
15221 --------------------------------
15222 -- Check_Overriding_Primitive --
15223 --------------------------------
15224
15225 procedure Check_Overriding_Primitive
15226 (Prim : Entity_Id;
15227 FNode : Node_Id)
15228 is
15229 Prim_Decl : constant Node_Id := Unit_Declaration_Node (Prim);
15230 Body_Decl : Node_Id;
15231 Body_Id : Entity_Id;
15232 Region : Node_Id;
15233
15234 begin
15235 -- Nothing to do for predefined primitives because they are
15236 -- artifacts of tagged type expansion and cannot override source
15237 -- primitives. Nothing to do as well for inherited primitives as
15238 -- the check concerns overridding ones.
15239
15240 if Is_Predefined_Dispatching_Operation (Prim)
15241 or else not Is_Overriding_Subprogram (Prim)
15242 then
15243 return;
15244 end if;
15245
15246 Body_Id := Corresponding_Body (Prim_Decl);
15247
15248 -- Nothing to do when the primitive does not have a corresponding
15249 -- body. This can happen when the unit with the bodies is not the
15250 -- main unit subjected to ABE checks.
15251
15252 if No (Body_Id) then
15253 return;
15254
15255 -- The primitive overrides a parent or progenitor primitive
15256
15257 elsif Present (Overridden_Operation (Prim)) then
15258
15259 -- Nothing to do when overriding an interface primitive happens
15260 -- by inheriting a non-interface primitive as the check would
15261 -- be done on the parent primitive.
15262
15263 if Present (Alias (Prim)) then
15264 return;
15265 end if;
15266
15267 -- Nothing to do when the primitive is not overriding. The body of
15268 -- such a primitive cannot be targeted by a dispatching call which
15269 -- is executable during elaboration, and cannot cause an ABE.
15270
15271 else
15272 return;
15273 end if;
15274
15275 Body_Decl := Unit_Declaration_Node (Body_Id);
15276 Region := Find_Early_Call_Region (Body_Decl);
15277
15278 -- The freeze node appears prior to the early call region of the
15279 -- primitive body.
15280
15281 -- IMPORTANT: This check must always be performed even when
15282 -- -gnatd.v (enforce SPARK elaboration rules in SPARK code) is not
15283 -- specified because the static model cannot guarantee the absence
15284 -- of ABEs in the presence of dispatching calls.
15285
15286 if Earlier_In_Extended_Unit (FNode, Region) then
15287 Error_Msg_Node_2 := Prim;
15288 Error_Msg_NE
15289 ("first freezing point of type & must appear within early "
15290 & "call region of primitive body & (SPARK RM 7.7(8))",
15291 Typ_Decl, Typ);
15292
15293 Error_Msg_Sloc := Sloc (Region);
15294 Error_Msg_N ("\region starts #", Typ_Decl);
15295
15296 Error_Msg_Sloc := Sloc (Body_Decl);
15297 Error_Msg_N ("\region ends #", Typ_Decl);
15298
15299 Error_Msg_Sloc := Freeze_Node_Location (FNode);
15300 Error_Msg_N ("\first freezing point #", Typ_Decl);
15301
15302 -- If applicable, suggest the use of pragma Elaborate_Body in
15303 -- the associated package spec.
15304
15305 Suggest_Elaborate_Body
15306 (N => FNode,
15307 Body_Decl => Body_Decl,
15308 Error_Nod => Typ_Decl);
15309
15310 raise Stop_Check;
15311 end if;
15312 end Check_Overriding_Primitive;
15313
15314 --------------------------
15315 -- Freeze_Node_Location --
15316 --------------------------
15317
15318 function Freeze_Node_Location (FNode : Node_Id) return Source_Ptr is
15319 Context : constant Node_Id := Parent (FNode);
15320 Loc : constant Source_Ptr := Sloc (FNode);
15321
15322 Prv_Decls : List_Id;
15323 Vis_Decls : List_Id;
15324
15325 begin
15326 -- In general, the source location of the freeze node is as close
15327 -- as possible to the real freeze point, except when the freeze
15328 -- node is at the "bottom" of a package spec.
15329
15330 if Nkind (Context) = N_Package_Specification then
15331 Prv_Decls := Private_Declarations (Context);
15332 Vis_Decls := Visible_Declarations (Context);
15333
15334 -- The freeze node appears in the private declarations of the
15335 -- package.
15336
15337 if Present (Prv_Decls)
15338 and then List_Containing (FNode) = Prv_Decls
15339 then
15340 null;
15341
15342 -- The freeze node appears in the visible declarations of the
15343 -- package and there are no private declarations.
15344
15345 elsif Present (Vis_Decls)
15346 and then List_Containing (FNode) = Vis_Decls
15347 and then (No (Prv_Decls) or else Is_Empty_List (Prv_Decls))
15348 then
15349 null;
15350
15351 -- Otherwise the freeze node is not in the "last" declarative
15352 -- list of the package. Use the existing source location of the
15353 -- freeze node.
15354
15355 else
15356 return Loc;
15357 end if;
15358
15359 -- The freeze node appears at the "bottom" of the package when
15360 -- it is in the "last" declarative list and is either the last
15361 -- in the list or is followed by internal constructs only. In
15362 -- that case the more appropriate source location is that of
15363 -- the package end label.
15364
15365 if not Precedes_Source_Construct (FNode) then
15366 return Sloc (End_Label (Context));
15367 end if;
15368 end if;
15369
15370 return Loc;
15371 end Freeze_Node_Location;
15372
15373 -------------------------------
15374 -- Precedes_Source_Construct --
15375 -------------------------------
15376
15377 function Precedes_Source_Construct (N : Node_Id) return Boolean is
15378 Decl : Node_Id;
15379
15380 begin
15381 Decl := Next (N);
15382 while Present (Decl) loop
15383 if Comes_From_Source (Decl) then
15384 return True;
15385
15386 -- A generated body for a source expression function is treated
15387 -- as a source construct.
15388
15389 elsif Nkind (Decl) = N_Subprogram_Body
15390 and then Was_Expression_Function (Decl)
15391 and then Comes_From_Source (Original_Node (Decl))
15392 then
15393 return True;
15394 end if;
15395
15396 Next (Decl);
15397 end loop;
15398
15399 return False;
15400 end Precedes_Source_Construct;
15401
15402 ----------------------------
15403 -- Suggest_Elaborate_Body --
15404 ----------------------------
15405
15406 procedure Suggest_Elaborate_Body
15407 (N : Node_Id;
15408 Body_Decl : Node_Id;
15409 Error_Nod : Node_Id)
15410 is
15411 Unit_Id : constant Node_Id := Unit (Cunit (Main_Unit));
15412 Region : Node_Id;
15413
15414 begin
15415 -- The suggestion applies only when the subprogram body resides in
15416 -- a compilation package body, and a pragma Elaborate_Body would
15417 -- allow for the node to appear in the early call region of the
15418 -- subprogram body. This implies that all code from the subprogram
15419 -- body up to the node is preelaborable.
15420
15421 if Nkind (Unit_Id) = N_Package_Body then
15422
15423 -- Find the start of the early call region again assuming that
15424 -- the package spec has pragma Elaborate_Body. Note that the
15425 -- internal data structures are intentionally not updated
15426 -- because this is a speculative search.
15427
15428 Region :=
15429 Find_Early_Call_Region
15430 (Body_Decl => Body_Decl,
15431 Assume_Elab_Body => True,
15432 Skip_Memoization => True);
15433
15434 -- If the node appears within the early call region, assuming
15435 -- that the package spec carries pragma Elaborate_Body, then it
15436 -- is safe to suggest the pragma.
15437
15438 if Earlier_In_Extended_Unit (Region, N) then
15439 Error_Msg_Name_1 := Name_Elaborate_Body;
15440 Error_Msg_NE
15441 ("\consider adding pragma % in spec of unit &",
15442 Error_Nod, Defining_Entity (Unit_Id));
15443 end if;
15444 end if;
15445 end Suggest_Elaborate_Body;
15446
15447 -- Local variables
15448
15449 FNode : constant Node_Id := Freeze_Node (Typ);
15450 Prims : constant Elist_Id := Direct_Primitive_Operations (Typ);
15451
15452 Prim_Elmt : Elmt_Id;
15453
15454 -- Start of processing for Process_SPARK_Derived_Type
15455
15456 begin
15457 -- A type should have its freeze node set by the time SPARK scenarios
15458 -- are being verified.
15459
15460 pragma Assert (Present (FNode));
15461
15462 -- Verify that the freeze node of the derived type is within the
15463 -- early call region of each overriding primitive body
15464 -- (SPARK RM 7.7(8)).
15465
15466 if Present (Prims) then
15467 Prim_Elmt := First_Elmt (Prims);
15468 while Present (Prim_Elmt) loop
15469 Check_Overriding_Primitive
15470 (Prim => Node (Prim_Elmt),
15471 FNode => FNode);
15472
15473 Next_Elmt (Prim_Elmt);
15474 end loop;
15475 end if;
15476
15477 exception
15478 when Stop_Check =>
15479 null;
15480 end Process_SPARK_Derived_Type;
15481
15482 ---------------------------------
15483 -- Process_SPARK_Instantiation --
15484 ---------------------------------
15485
15486 procedure Process_SPARK_Instantiation
15487 (Inst : Node_Id;
15488 Inst_Rep : Scenario_Rep_Id;
15489 In_State : Processing_In_State)
15490 is
15491 Gen_Id : constant Entity_Id := Target (Inst_Rep);
15492 Gen_Rep : constant Target_Rep_Id :=
15493 Target_Representation_Of (Gen_Id, In_State);
15494 Body_Decl : constant Node_Id := Body_Declaration (Gen_Rep);
15495
15496 begin
15497 -- The instantiation and the generic body are both in the main unit
15498
15499 if Present (Body_Decl)
15500 and then In_Extended_Main_Code_Unit (Body_Decl)
15501
15502 -- If the instantiation appears prior to the generic body, then the
15503 -- instantiation is illegal (SPARK RM 7.7(6)).
15504
15505 -- IMPORTANT: This check must always be performed even when
15506 -- -gnatd.v (enforce SPARK elaboration rules in SPARK code) is not
15507 -- specified because the rule prevents use-before-declaration of
15508 -- objects that may precede the generic body.
15509
15510 and then Earlier_In_Extended_Unit (Inst, Body_Decl)
15511 then
15512 Error_Msg_NE
15513 ("cannot instantiate & before body seen", Inst, Gen_Id);
15514 end if;
15515 end Process_SPARK_Instantiation;
15516
15517 ----------------------------
15518 -- Process_SPARK_Scenario --
15519 ----------------------------
15520
15521 procedure Process_SPARK_Scenario
15522 (N : Node_Id;
15523 In_State : Processing_In_State)
15524 is
15525 Scen : constant Node_Id := Scenario (N);
15526
15527 begin
15528 -- Ensure that a suitable elaboration model is in effect for SPARK
15529 -- rule verification.
15530
15531 Check_SPARK_Model_In_Effect;
15532
15533 -- Add the current scenario to the stack of active scenarios
15534
15535 Push_Active_Scenario (Scen);
15536
15537 -- Derived type
15538
15539 if Is_Suitable_SPARK_Derived_Type (Scen) then
15540 Process_SPARK_Derived_Type
15541 (Typ_Decl => Scen,
15542 Typ_Rep => Scenario_Representation_Of (Scen, In_State),
15543 In_State => In_State);
15544
15545 -- Instantiation
15546
15547 elsif Is_Suitable_SPARK_Instantiation (Scen) then
15548 Process_SPARK_Instantiation
15549 (Inst => Scen,
15550 Inst_Rep => Scenario_Representation_Of (Scen, In_State),
15551 In_State => In_State);
15552
15553 -- Refined_State pragma
15554
15555 elsif Is_Suitable_SPARK_Refined_State_Pragma (Scen) then
15556 Process_SPARK_Refined_State_Pragma
15557 (Prag => Scen,
15558 Prag_Rep => Scenario_Representation_Of (Scen, In_State),
15559 In_State => In_State);
15560 end if;
15561
15562 -- Remove the current scenario from the stack of active scenarios
15563 -- once all ABE diagnostics and checks have been performed.
15564
15565 Pop_Active_Scenario (Scen);
15566 end Process_SPARK_Scenario;
15567
15568 ----------------------------------------
15569 -- Process_SPARK_Refined_State_Pragma --
15570 ----------------------------------------
15571
15572 procedure Process_SPARK_Refined_State_Pragma
15573 (Prag : Node_Id;
15574 Prag_Rep : Scenario_Rep_Id;
15575 In_State : Processing_In_State)
15576 is
15577 pragma Unreferenced (Prag_Rep);
15578
15579 procedure Check_SPARK_Constituent (Constit_Id : Entity_Id);
15580 pragma Inline (Check_SPARK_Constituent);
15581 -- Ensure that a single constituent Constit_Id is elaborated prior to
15582 -- the main unit.
15583
15584 procedure Check_SPARK_Constituents (Constits : Elist_Id);
15585 pragma Inline (Check_SPARK_Constituents);
15586 -- Ensure that all constituents found in list Constits are elaborated
15587 -- prior to the main unit.
15588
15589 procedure Check_SPARK_Initialized_State (State : Node_Id);
15590 pragma Inline (Check_SPARK_Initialized_State);
15591 -- Ensure that the constituents of single abstract state State are
15592 -- elaborated prior to the main unit.
15593
15594 procedure Check_SPARK_Initialized_States (Pack_Id : Entity_Id);
15595 pragma Inline (Check_SPARK_Initialized_States);
15596 -- Ensure that the constituents of all abstract states which appear
15597 -- in the Initializes pragma of package Pack_Id are elaborated prior
15598 -- to the main unit.
15599
15600 -----------------------------
15601 -- Check_SPARK_Constituent --
15602 -----------------------------
15603
15604 procedure Check_SPARK_Constituent (Constit_Id : Entity_Id) is
15605 SM_Prag : Node_Id;
15606
15607 begin
15608 -- Nothing to do for "null" constituents
15609
15610 if Nkind (Constit_Id) = N_Null then
15611 return;
15612
15613 -- Nothing to do for illegal constituents
15614
15615 elsif Error_Posted (Constit_Id) then
15616 return;
15617 end if;
15618
15619 SM_Prag := SPARK_Pragma (Constit_Id);
15620
15621 -- The check applies only when the constituent is subject to
15622 -- pragma SPARK_Mode On.
15623
15624 if Present (SM_Prag)
15625 and then Get_SPARK_Mode_From_Annotation (SM_Prag) = On
15626 then
15627 -- An external constituent of an abstract state which appears
15628 -- in the Initializes pragma of a package spec imposes an
15629 -- Elaborate requirement on the context of the main unit.
15630 -- Determine whether the context has a pragma strong enough to
15631 -- meet the requirement.
15632
15633 -- IMPORTANT: This check is performed only when -gnatd.v
15634 -- (enforce SPARK elaboration rules in SPARK code) is in effect
15635 -- because the static model can ensure the prior elaboration of
15636 -- the unit which contains a constituent by installing implicit
15637 -- Elaborate pragma.
15638
15639 if Debug_Flag_Dot_V then
15640 Meet_Elaboration_Requirement
15641 (N => Prag,
15642 Targ_Id => Constit_Id,
15643 Req_Nam => Name_Elaborate,
15644 In_State => In_State);
15645
15646 -- Otherwise ensure that the unit with the external constituent
15647 -- is elaborated prior to the main unit.
15648
15649 else
15650 Ensure_Prior_Elaboration
15651 (N => Prag,
15652 Unit_Id => Find_Top_Unit (Constit_Id),
15653 Prag_Nam => Name_Elaborate,
15654 In_State => In_State);
15655 end if;
15656 end if;
15657 end Check_SPARK_Constituent;
15658
15659 ------------------------------
15660 -- Check_SPARK_Constituents --
15661 ------------------------------
15662
15663 procedure Check_SPARK_Constituents (Constits : Elist_Id) is
15664 Constit_Elmt : Elmt_Id;
15665
15666 begin
15667 if Present (Constits) then
15668 Constit_Elmt := First_Elmt (Constits);
15669 while Present (Constit_Elmt) loop
15670 Check_SPARK_Constituent (Node (Constit_Elmt));
15671 Next_Elmt (Constit_Elmt);
15672 end loop;
15673 end if;
15674 end Check_SPARK_Constituents;
15675
15676 -----------------------------------
15677 -- Check_SPARK_Initialized_State --
15678 -----------------------------------
15679
15680 procedure Check_SPARK_Initialized_State (State : Node_Id) is
15681 SM_Prag : Node_Id;
15682 State_Id : Entity_Id;
15683
15684 begin
15685 -- Nothing to do for "null" initialization items
15686
15687 if Nkind (State) = N_Null then
15688 return;
15689
15690 -- Nothing to do for illegal states
15691
15692 elsif Error_Posted (State) then
15693 return;
15694 end if;
15695
15696 State_Id := Entity_Of (State);
15697
15698 -- Sanitize the state
15699
15700 if No (State_Id) then
15701 return;
15702
15703 elsif Error_Posted (State_Id) then
15704 return;
15705
15706 elsif Ekind (State_Id) /= E_Abstract_State then
15707 return;
15708 end if;
15709
15710 -- The check is performed only when the abstract state is subject
15711 -- to SPARK_Mode On.
15712
15713 SM_Prag := SPARK_Pragma (State_Id);
15714
15715 if Present (SM_Prag)
15716 and then Get_SPARK_Mode_From_Annotation (SM_Prag) = On
15717 then
15718 Check_SPARK_Constituents (Refinement_Constituents (State_Id));
15719 end if;
15720 end Check_SPARK_Initialized_State;
15721
15722 ------------------------------------
15723 -- Check_SPARK_Initialized_States --
15724 ------------------------------------
15725
15726 procedure Check_SPARK_Initialized_States (Pack_Id : Entity_Id) is
15727 Init_Prag : constant Node_Id :=
15728 Get_Pragma (Pack_Id, Pragma_Initializes);
15729
15730 Init : Node_Id;
15731 Inits : Node_Id;
15732
15733 begin
15734 if Present (Init_Prag) then
15735 Inits := Expression (Get_Argument (Init_Prag, Pack_Id));
15736
15737 -- Avoid processing a "null" initialization list. The only
15738 -- other alternative is an aggregate.
15739
15740 if Nkind (Inits) = N_Aggregate then
15741
15742 -- The initialization items appear in list form:
15743 --
15744 -- (state1, state2)
15745
15746 if Present (Expressions (Inits)) then
15747 Init := First (Expressions (Inits));
15748 while Present (Init) loop
15749 Check_SPARK_Initialized_State (Init);
15750 Next (Init);
15751 end loop;
15752 end if;
15753
15754 -- The initialization items appear in associated form:
15755 --
15756 -- (state1 => item1,
15757 -- state2 => (item2, item3))
15758
15759 if Present (Component_Associations (Inits)) then
15760 Init := First (Component_Associations (Inits));
15761 while Present (Init) loop
15762 Check_SPARK_Initialized_State (Init);
15763 Next (Init);
15764 end loop;
15765 end if;
15766 end if;
15767 end if;
15768 end Check_SPARK_Initialized_States;
15769
15770 -- Local variables
15771
15772 Pack_Body : constant Node_Id := Find_Related_Package_Or_Body (Prag);
15773
15774 -- Start of processing for Process_SPARK_Refined_State_Pragma
15775
15776 begin
15777 -- Pragma Refined_State must be associated with a package body
15778
15779 pragma Assert
15780 (Present (Pack_Body) and then Nkind (Pack_Body) = N_Package_Body);
15781
15782 -- Verify that each external contitunent of an abstract state
15783 -- mentioned in pragma Initializes is properly elaborated.
15784
15785 Check_SPARK_Initialized_States (Unique_Defining_Entity (Pack_Body));
15786 end Process_SPARK_Refined_State_Pragma;
15787 end SPARK_Processor;
15788
15789 -------------------------------
15790 -- Spec_And_Body_From_Entity --
15791 -------------------------------
15792
15793 procedure Spec_And_Body_From_Entity
15794 (Id : Node_Id;
15795 Spec_Decl : out Node_Id;
15796 Body_Decl : out Node_Id)
15797 is
15798 begin
15799 Spec_And_Body_From_Node
15800 (N => Unit_Declaration_Node (Id),
15801 Spec_Decl => Spec_Decl,
15802 Body_Decl => Body_Decl);
15803 end Spec_And_Body_From_Entity;
15804
15805 -----------------------------
15806 -- Spec_And_Body_From_Node --
15807 -----------------------------
15808
15809 procedure Spec_And_Body_From_Node
15810 (N : Node_Id;
15811 Spec_Decl : out Node_Id;
15812 Body_Decl : out Node_Id)
15813 is
15814 Body_Id : Entity_Id;
15815 Spec_Id : Entity_Id;
15816
15817 begin
15818 -- Assume that the construct lacks spec and body
15819
15820 Body_Decl := Empty;
15821 Spec_Decl := Empty;
15822
15823 -- Bodies
15824
15825 if Nkind_In (N, N_Package_Body,
15826 N_Protected_Body,
15827 N_Subprogram_Body,
15828 N_Task_Body)
15829 then
15830 Spec_Id := Corresponding_Spec (N);
15831
15832 -- The body completes a previous declaration
15833
15834 if Present (Spec_Id) then
15835 Spec_Decl := Unit_Declaration_Node (Spec_Id);
15836
15837 -- Otherwise the body acts as the initial declaration, and is both a
15838 -- spec and body. There is no need to look for an optional body.
15839
15840 else
15841 Body_Decl := N;
15842 Spec_Decl := N;
15843 return;
15844 end if;
15845
15846 -- Declarations
15847
15848 elsif Nkind_In (N, N_Entry_Declaration,
15849 N_Generic_Package_Declaration,
15850 N_Generic_Subprogram_Declaration,
15851 N_Package_Declaration,
15852 N_Protected_Type_Declaration,
15853 N_Subprogram_Declaration,
15854 N_Task_Type_Declaration)
15855 then
15856 Spec_Decl := N;
15857
15858 -- Expression function
15859
15860 elsif Nkind (N) = N_Expression_Function then
15861 Spec_Id := Corresponding_Spec (N);
15862 pragma Assert (Present (Spec_Id));
15863
15864 Spec_Decl := Unit_Declaration_Node (Spec_Id);
15865
15866 -- Instantiations
15867
15868 elsif Nkind (N) in N_Generic_Instantiation then
15869 Spec_Decl := Instance_Spec (N);
15870 pragma Assert (Present (Spec_Decl));
15871
15872 -- Stubs
15873
15874 elsif Nkind (N) in N_Body_Stub then
15875 Spec_Id := Corresponding_Spec_Of_Stub (N);
15876
15877 -- The stub completes a previous declaration
15878
15879 if Present (Spec_Id) then
15880 Spec_Decl := Unit_Declaration_Node (Spec_Id);
15881
15882 -- Otherwise the stub acts as a spec
15883
15884 else
15885 Spec_Decl := N;
15886 end if;
15887 end if;
15888
15889 -- Obtain an optional or mandatory body
15890
15891 if Present (Spec_Decl) then
15892 Body_Id := Corresponding_Body (Spec_Decl);
15893
15894 if Present (Body_Id) then
15895 Body_Decl := Unit_Declaration_Node (Body_Id);
15896 end if;
15897 end if;
15898 end Spec_And_Body_From_Node;
15899
15900 -------------------------------
15901 -- Static_Elaboration_Checks --
15902 -------------------------------
15903
15904 function Static_Elaboration_Checks return Boolean is
15905 begin
15906 return not Dynamic_Elaboration_Checks;
15907 end Static_Elaboration_Checks;
15908
15909 -----------------
15910 -- Unit_Entity --
15911 -----------------
15912
15913 function Unit_Entity (Unit_Id : Entity_Id) return Entity_Id is
15914 function Is_Subunit (Id : Entity_Id) return Boolean;
15915 pragma Inline (Is_Subunit);
15916 -- Determine whether the entity of an initial declaration denotes a
15917 -- subunit.
15918
15919 ----------------
15920 -- Is_Subunit --
15921 ----------------
15922
15923 function Is_Subunit (Id : Entity_Id) return Boolean is
15924 Decl : constant Node_Id := Unit_Declaration_Node (Id);
15925
15926 begin
15927 return
15928 Nkind_In (Decl, N_Generic_Package_Declaration,
15929 N_Generic_Subprogram_Declaration,
15930 N_Package_Declaration,
15931 N_Protected_Type_Declaration,
15932 N_Subprogram_Declaration,
15933 N_Task_Type_Declaration)
15934 and then Present (Corresponding_Body (Decl))
15935 and then Nkind (Parent (Unit_Declaration_Node
15936 (Corresponding_Body (Decl)))) = N_Subunit;
15937 end Is_Subunit;
15938
15939 -- Local variables
15940
15941 Id : Entity_Id;
15942
15943 -- Start of processing for Unit_Entity
15944
15945 begin
15946 Id := Unique_Entity (Unit_Id);
15947
15948 -- Skip all subunits found in the scope chain which ends at the input
15949 -- unit.
15950
15951 while Is_Subunit (Id) loop
15952 Id := Scope (Id);
15953 end loop;
15954
15955 return Id;
15956 end Unit_Entity;
15957
15958 ---------------------------------
15959 -- Update_Elaboration_Scenario --
15960 ---------------------------------
15961
15962 procedure Update_Elaboration_Scenario (New_N : Node_Id; Old_N : Node_Id) is
15963 begin
15964 -- Nothing to do when the elaboration phase of the compiler is not
15965 -- active.
15966
15967 if not Elaboration_Phase_Active then
15968 return;
15969
15970 -- Nothing to do when the old and new scenarios are one and the same
15971
15972 elsif Old_N = New_N then
15973 return;
15974 end if;
15975
15976 -- A scenario is being transformed by Atree.Rewrite. Update all relevant
15977 -- internal data structures to reflect this change. This ensures that a
15978 -- potential run-time conditional ABE check or a guaranteed ABE failure
15979 -- is inserted at the proper place in the tree.
15980
15981 if Is_Scenario (Old_N) then
15982 Replace_Scenario (Old_N, New_N);
15983 end if;
15984 end Update_Elaboration_Scenario;
15985
15986 ---------------------------------------------------------------------------
15987 -- --
15988 -- L E G A C Y A C C E S S B E F O R E E L A B O R A T I O N --
15989 -- --
15990 -- M E C H A N I S M --
15991 -- --
15992 ---------------------------------------------------------------------------
15993
15994 -- This section contains the implementation of the pre-18.x legacy ABE
15995 -- mechanism. The mechanism can be activated using switch -gnatH (legacy
15996 -- elaboration checking mode enabled).
15997
15998 -----------------------------
15999 -- Description of Approach --
16000 -----------------------------
16001
16002 -- Every non-static call that is encountered by Sem_Res results in a call
16003 -- to Check_Elab_Call, with N being the call node, and Outer set to its
16004 -- default value of True. In addition X'Access is treated like a call
16005 -- for the access-to-procedure case, and in SPARK mode only we also
16006 -- check variable references.
16007
16008 -- The goal of Check_Elab_Call is to determine whether or not the reference
16009 -- in question can generate an access before elaboration error (raising
16010 -- Program_Error) either by directly calling a subprogram whose body
16011 -- has not yet been elaborated, or indirectly, by calling a subprogram
16012 -- whose body has been elaborated, but which contains a call to such a
16013 -- subprogram.
16014
16015 -- In addition, in SPARK mode, we are checking for a variable reference in
16016 -- another package, which requires an explicit Elaborate_All pragma.
16017
16018 -- The only references that we need to look at the outer level are
16019 -- references that occur in elaboration code. There are two cases. The
16020 -- reference can be at the outer level of elaboration code, or it can
16021 -- be within another unit, e.g. the elaboration code of a subprogram.
16022
16023 -- In the case of an elaboration call at the outer level, we must trace
16024 -- all calls to outer level routines either within the current unit or to
16025 -- other units that are with'ed. For calls within the current unit, we can
16026 -- determine if the body has been elaborated or not, and if it has not,
16027 -- then a warning is generated.
16028
16029 -- Note that there are two subcases. If the original call directly calls a
16030 -- subprogram whose body has not been elaborated, then we know that an ABE
16031 -- will take place, and we replace the call by a raise of Program_Error.
16032 -- If the call is indirect, then we don't know that the PE will be raised,
16033 -- since the call might be guarded by a conditional. In this case we set
16034 -- Do_Elab_Check on the call so that a dynamic check is generated, and
16035 -- output a warning.
16036
16037 -- For calls to a subprogram in a with'ed unit or a 'Access or variable
16038 -- reference (SPARK mode case), we require that a pragma Elaborate_All
16039 -- or pragma Elaborate be present, or that the referenced unit have a
16040 -- pragma Preelaborate, pragma Pure, or pragma Elaborate_Body. If none
16041 -- of these conditions is met, then a warning is generated that a pragma
16042 -- Elaborate_All may be needed (error in the SPARK case), or an implicit
16043 -- pragma is generated.
16044
16045 -- For the case of an elaboration call at some inner level, we are
16046 -- interested in tracing only calls to subprograms at the same level, i.e.
16047 -- those that can be called during elaboration. Any calls to outer level
16048 -- routines cannot cause ABE's as a result of the original call (there
16049 -- might be an outer level call to the subprogram from outside that causes
16050 -- the ABE, but that gets analyzed separately).
16051
16052 -- Note that we never trace calls to inner level subprograms, since these
16053 -- cannot result in ABE's unless there is an elaboration problem at a lower
16054 -- level, which will be separately detected.
16055
16056 -- Note on pragma Elaborate. The checking here assumes that a pragma
16057 -- Elaborate on a with'ed unit guarantees that subprograms within the unit
16058 -- can be called without causing an ABE. This is not in fact the case since
16059 -- pragma Elaborate does not guarantee the transitive coverage guaranteed
16060 -- by Elaborate_All. However, we decide to trust the user in this case.
16061
16062 --------------------------------------
16063 -- Instantiation Elaboration Errors --
16064 --------------------------------------
16065
16066 -- A special case arises when an instantiation appears in a context that is
16067 -- known to be before the body is elaborated, e.g.
16068
16069 -- generic package x is ...
16070 -- ...
16071 -- package xx is new x;
16072 -- ...
16073 -- package body x is ...
16074
16075 -- In this situation it is certain that an elaboration error will occur,
16076 -- and an unconditional raise Program_Error statement is inserted before
16077 -- the instantiation, and a warning generated.
16078
16079 -- The problem is that in this case we have no place to put the body of
16080 -- the instantiation. We can't put it in the normal place, because it is
16081 -- too early, and will cause errors to occur as a result of referencing
16082 -- entities before they are declared.
16083
16084 -- Our approach in this case is simply to avoid creating the body of the
16085 -- instantiation in such a case. The instantiation spec is modified to
16086 -- include dummy bodies for all subprograms, so that the resulting code
16087 -- does not contain subprogram specs with no corresponding bodies.
16088
16089 -- The following table records the recursive call chain for output in the
16090 -- Output routine. Each entry records the call node and the entity of the
16091 -- called routine. The number of entries in the table (i.e. the value of
16092 -- Elab_Call.Last) indicates the current depth of recursion and is used to
16093 -- identify the outer level.
16094
16095 type Elab_Call_Element is record
16096 Cloc : Source_Ptr;
16097 Ent : Entity_Id;
16098 end record;
16099
16100 package Elab_Call is new Table.Table
16101 (Table_Component_Type => Elab_Call_Element,
16102 Table_Index_Type => Int,
16103 Table_Low_Bound => 1,
16104 Table_Initial => 50,
16105 Table_Increment => 100,
16106 Table_Name => "Elab_Call");
16107
16108 -- The following table records all calls that have been processed starting
16109 -- from an outer level call. The table prevents both infinite recursion and
16110 -- useless reanalysis of calls within the same context. The use of context
16111 -- is important because it allows for proper checks in more complex code:
16112
16113 -- if ... then
16114 -- Call; -- requires a check
16115 -- Call; -- does not need a check thanks to the table
16116 -- elsif ... then
16117 -- Call; -- requires a check, different context
16118 -- end if;
16119
16120 -- Call; -- requires a check, different context
16121
16122 type Visited_Element is record
16123 Subp_Id : Entity_Id;
16124 -- The entity of the subprogram being called
16125
16126 Context : Node_Id;
16127 -- The context where the call to the subprogram occurs
16128 end record;
16129
16130 package Elab_Visited is new Table.Table
16131 (Table_Component_Type => Visited_Element,
16132 Table_Index_Type => Int,
16133 Table_Low_Bound => 1,
16134 Table_Initial => 200,
16135 Table_Increment => 100,
16136 Table_Name => "Elab_Visited");
16137
16138 -- The following table records delayed calls which must be examined after
16139 -- all generic bodies have been instantiated.
16140
16141 type Delay_Element is record
16142 N : Node_Id;
16143 -- The parameter N from the call to Check_Internal_Call. Note that this
16144 -- node may get rewritten over the delay period by expansion in the call
16145 -- case (but not in the instantiation case).
16146
16147 E : Entity_Id;
16148 -- The parameter E from the call to Check_Internal_Call
16149
16150 Orig_Ent : Entity_Id;
16151 -- The parameter Orig_Ent from the call to Check_Internal_Call
16152
16153 Curscop : Entity_Id;
16154 -- The current scope of the call. This is restored when we complete the
16155 -- delayed call, so that we do this in the right scope.
16156
16157 Outer_Scope : Entity_Id;
16158 -- Save scope of outer level call
16159
16160 From_Elab_Code : Boolean;
16161 -- Save indication of whether this call is from elaboration code
16162
16163 In_Task_Activation : Boolean;
16164 -- Save indication of whether this call is from a task body. Tasks are
16165 -- activated at the "begin", which is after all local procedure bodies,
16166 -- so calls to those procedures can't fail, even if they occur after the
16167 -- task body.
16168
16169 From_SPARK_Code : Boolean;
16170 -- Save indication of whether this call is under SPARK_Mode => On
16171 end record;
16172
16173 package Delay_Check is new Table.Table
16174 (Table_Component_Type => Delay_Element,
16175 Table_Index_Type => Int,
16176 Table_Low_Bound => 1,
16177 Table_Initial => 1000,
16178 Table_Increment => 100,
16179 Table_Name => "Delay_Check");
16180
16181 C_Scope : Entity_Id;
16182 -- Top-level scope of current scope. Compute this only once at the outer
16183 -- level, i.e. for a call to Check_Elab_Call from outside this unit.
16184
16185 Outer_Level_Sloc : Source_Ptr;
16186 -- Save Sloc value for outer level call node for comparisons of source
16187 -- locations. A body is too late if it appears after the *outer* level
16188 -- call, not the particular call that is being analyzed.
16189
16190 From_Elab_Code : Boolean;
16191 -- This flag shows whether the outer level call currently being examined
16192 -- is or is not in elaboration code. We are only interested in calls to
16193 -- routines in other units if this flag is True.
16194
16195 In_Task_Activation : Boolean := False;
16196 -- This flag indicates whether we are performing elaboration checks on task
16197 -- bodies, at the point of activation. If true, we do not raise
16198 -- Program_Error for calls to local procedures, because all local bodies
16199 -- are known to be elaborated. However, we still need to trace such calls,
16200 -- because a local procedure could call a procedure in another package,
16201 -- so we might need an implicit Elaborate_All.
16202
16203 Delaying_Elab_Checks : Boolean := True;
16204 -- This is set True till the compilation is complete, including the
16205 -- insertion of all instance bodies. Then when Check_Elab_Calls is called,
16206 -- the delay table is used to make the delayed calls and this flag is reset
16207 -- to False, so that the calls are processed.
16208
16209 -----------------------
16210 -- Local Subprograms --
16211 -----------------------
16212
16213 -- Note: Outer_Scope in all following specs represents the scope of
16214 -- interest of the outer level call. If it is set to Standard_Standard,
16215 -- then it means the outer level call was at elaboration level, and that
16216 -- thus all calls are of interest. If it was set to some other scope,
16217 -- then the original call was an inner call, and we are not interested
16218 -- in calls that go outside this scope.
16219
16220 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id);
16221 -- Analysis of construct N shows that we should set Elaborate_All_Desirable
16222 -- for the WITH clause for unit U (which will always be present). A special
16223 -- case is when N is a function or procedure instantiation, in which case
16224 -- it is sufficient to set Elaborate_Desirable, since in this case there is
16225 -- no possibility of transitive elaboration issues.
16226
16227 procedure Check_A_Call
16228 (N : Node_Id;
16229 E : Entity_Id;
16230 Outer_Scope : Entity_Id;
16231 Inter_Unit_Only : Boolean;
16232 Generate_Warnings : Boolean := True;
16233 In_Init_Proc : Boolean := False);
16234 -- This is the internal recursive routine that is called to check for
16235 -- possible elaboration error. The argument N is a subprogram call or
16236 -- generic instantiation, or 'Access attribute reference to be checked, and
16237 -- E is the entity of the called subprogram, or instantiated generic unit,
16238 -- or subprogram referenced by 'Access.
16239 --
16240 -- In SPARK mode, N can also be a variable reference, since in SPARK this
16241 -- also triggers a requirement for Elaborate_All, and in this case E is the
16242 -- entity being referenced.
16243 --
16244 -- Outer_Scope is the outer level scope for the original reference.
16245 -- Inter_Unit_Only is set if the call is only to be checked in the
16246 -- case where it is to another unit (and skipped if within a unit).
16247 -- Generate_Warnings is set to False to suppress warning messages about
16248 -- missing pragma Elaborate_All's. These messages are not wanted for
16249 -- inner calls in the dynamic model. Note that an instance of the Access
16250 -- attribute applied to a subprogram also generates a call to this
16251 -- procedure (since the referenced subprogram may be called later
16252 -- indirectly). Flag In_Init_Proc should be set whenever the current
16253 -- context is a type init proc.
16254 --
16255 -- Note: this might better be called Check_A_Reference to recognize the
16256 -- variable case for SPARK, but we prefer to retain the historical name
16257 -- since in practice this is mostly about checking calls for the possible
16258 -- occurrence of an access-before-elaboration exception.
16259
16260 procedure Check_Bad_Instantiation (N : Node_Id);
16261 -- N is a node for an instantiation (if called with any other node kind,
16262 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
16263 -- the special case of a generic instantiation of a generic spec in the
16264 -- same declarative part as the instantiation where a body is present and
16265 -- has not yet been seen. This is an obvious error, but needs to be checked
16266 -- specially at the time of the instantiation, since it is a case where we
16267 -- cannot insert the body anywhere. If this case is detected, warnings are
16268 -- generated, and a raise of Program_Error is inserted. In addition any
16269 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
16270 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
16271 -- flag as an indication that no attempt should be made to insert an
16272 -- instance body.
16273
16274 procedure Check_Internal_Call
16275 (N : Node_Id;
16276 E : Entity_Id;
16277 Outer_Scope : Entity_Id;
16278 Orig_Ent : Entity_Id);
16279 -- N is a function call or procedure statement call node and E is the
16280 -- entity of the called function, which is within the current compilation
16281 -- unit (where subunits count as part of the parent). This call checks if
16282 -- this call, or any call within any accessed body could cause an ABE, and
16283 -- if so, outputs a warning. Orig_Ent differs from E only in the case of
16284 -- renamings, and points to the original name of the entity. This is used
16285 -- for error messages. Outer_Scope is the outer level scope for the
16286 -- original call.
16287
16288 procedure Check_Internal_Call_Continue
16289 (N : Node_Id;
16290 E : Entity_Id;
16291 Outer_Scope : Entity_Id;
16292 Orig_Ent : Entity_Id);
16293 -- The processing for Check_Internal_Call is divided up into two phases,
16294 -- and this represents the second phase. The second phase is delayed if
16295 -- Delaying_Elab_Checks is set to True. In this delayed case, the first
16296 -- phase makes an entry in the Delay_Check table, which is processed when
16297 -- Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to
16298 -- Check_Internal_Call. Outer_Scope is the outer level scope for the
16299 -- original call.
16300
16301 function Get_Referenced_Ent (N : Node_Id) return Entity_Id;
16302 -- N is either a function or procedure call or an access attribute that
16303 -- references a subprogram. This call retrieves the relevant entity. If
16304 -- this is a call to a protected subprogram, the entity is a selected
16305 -- component. The callable entity may be absent, in which case Empty is
16306 -- returned. This happens with non-analyzed calls in nested generics.
16307 --
16308 -- If SPARK_Mode is On, then N can also be a reference to an E_Variable
16309 -- entity, in which case, the value returned is simply this entity.
16310
16311 function Has_Generic_Body (N : Node_Id) return Boolean;
16312 -- N is a generic package instantiation node, and this routine determines
16313 -- if this package spec does in fact have a generic body. If so, then
16314 -- True is returned, otherwise False. Note that this is not at all the
16315 -- same as checking if the unit requires a body, since it deals with
16316 -- the case of optional bodies accurately (i.e. if a body is optional,
16317 -- then it looks to see if a body is actually present). Note: this
16318 -- function can only do a fully correct job if in generating code mode
16319 -- where all bodies have to be present. If we are operating in semantics
16320 -- check only mode, then in some cases of optional bodies, a result of
16321 -- False may incorrectly be given. In practice this simply means that
16322 -- some cases of warnings for incorrect order of elaboration will only
16323 -- be given when generating code, which is not a big problem (and is
16324 -- inevitable, given the optional body semantics of Ada).
16325
16326 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty);
16327 -- Given code for an elaboration check (or unconditional raise if the check
16328 -- is not needed), inserts the code in the appropriate place. N is the call
16329 -- or instantiation node for which the check code is required. C is the
16330 -- test whose failure triggers the raise.
16331
16332 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean;
16333 -- Returns True if node N is a call to a generic formal subprogram
16334
16335 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean;
16336 -- Determine whether entity Id denotes a [Deep_]Finalize procedure
16337
16338 procedure Output_Calls
16339 (N : Node_Id;
16340 Check_Elab_Flag : Boolean);
16341 -- Outputs chain of calls stored in the Elab_Call table. The caller has
16342 -- already generated the main warning message, so the warnings generated
16343 -- are all continuation messages. The argument is the call node at which
16344 -- the messages are to be placed. When Check_Elab_Flag is set, calls are
16345 -- enumerated only when flag Elab_Warning is set for the dynamic case or
16346 -- when flag Elab_Info_Messages is set for the static case.
16347
16348 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean;
16349 -- Given two scopes, determine whether they are the same scope from an
16350 -- elaboration point of view, i.e. packages and blocks are ignored.
16351
16352 procedure Set_C_Scope;
16353 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
16354 -- to be the enclosing compilation unit of this scope.
16355
16356 procedure Set_Elaboration_Constraint
16357 (Call : Node_Id;
16358 Subp : Entity_Id;
16359 Scop : Entity_Id);
16360 -- The current unit U may depend semantically on some unit P that is not
16361 -- in the current context. If there is an elaboration call that reaches P,
16362 -- we need to indicate that P requires an Elaborate_All, but this is not
16363 -- effective in U's ali file, if there is no with_clause for P. In this
16364 -- case we add the Elaborate_All on the unit Q that directly or indirectly
16365 -- makes P available. This can happen in two cases:
16366 --
16367 -- a) Q declares a subtype of a type declared in P, and the call is an
16368 -- initialization call for an object of that subtype.
16369 --
16370 -- b) Q declares an object of some tagged type whose root type is
16371 -- declared in P, and the initialization call uses object notation on
16372 -- that object to reach a primitive operation or a classwide operation
16373 -- declared in P.
16374 --
16375 -- If P appears in the context of U, the current processing is correct.
16376 -- Otherwise we must identify these two cases to retrieve Q and place the
16377 -- Elaborate_All_Desirable on it.
16378
16379 function Spec_Entity (E : Entity_Id) return Entity_Id;
16380 -- Given a compilation unit entity, if it is a spec entity, it is returned
16381 -- unchanged. If it is a body entity, then the spec for the corresponding
16382 -- spec is returned
16383
16384 function Within (E1, E2 : Entity_Id) return Boolean;
16385 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
16386 -- of its contained scopes, False otherwise.
16387
16388 function Within_Elaborate_All
16389 (Unit : Unit_Number_Type;
16390 E : Entity_Id) return Boolean;
16391 -- Return True if we are within the scope of an Elaborate_All for E, or if
16392 -- we are within the scope of an Elaborate_All for some other unit U, and U
16393 -- with's E. This prevents spurious warnings when the called entity is
16394 -- renamed within U, or in case of generic instances.
16395
16396 --------------------------------------
16397 -- Activate_Elaborate_All_Desirable --
16398 --------------------------------------
16399
16400 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id) is
16401 UN : constant Unit_Number_Type := Get_Code_Unit (N);
16402 CU : constant Node_Id := Cunit (UN);
16403 UE : constant Entity_Id := Cunit_Entity (UN);
16404 Unm : constant Unit_Name_Type := Unit_Name (UN);
16405 CI : constant List_Id := Context_Items (CU);
16406 Itm : Node_Id;
16407 Ent : Entity_Id;
16408
16409 procedure Add_To_Context_And_Mark (Itm : Node_Id);
16410 -- This procedure is called when the elaborate indication must be
16411 -- applied to a unit not in the context of the referencing unit. The
16412 -- unit gets added to the context as an implicit with.
16413
16414 function In_Withs_Of (UEs : Entity_Id) return Boolean;
16415 -- UEs is the spec entity of a unit. If the unit to be marked is
16416 -- in the context item list of this unit spec, then the call returns
16417 -- True and Itm is left set to point to the relevant N_With_Clause node.
16418
16419 procedure Set_Elab_Flag (Itm : Node_Id);
16420 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
16421
16422 -----------------------------
16423 -- Add_To_Context_And_Mark --
16424 -----------------------------
16425
16426 procedure Add_To_Context_And_Mark (Itm : Node_Id) is
16427 CW : constant Node_Id :=
16428 Make_With_Clause (Sloc (Itm),
16429 Name => Name (Itm));
16430
16431 begin
16432 Set_Library_Unit (CW, Library_Unit (Itm));
16433 Set_Implicit_With (CW);
16434
16435 -- Set elaborate all desirable on copy and then append the copy to
16436 -- the list of body with's and we are done.
16437
16438 Set_Elab_Flag (CW);
16439 Append_To (CI, CW);
16440 end Add_To_Context_And_Mark;
16441
16442 -----------------
16443 -- In_Withs_Of --
16444 -----------------
16445
16446 function In_Withs_Of (UEs : Entity_Id) return Boolean is
16447 UNs : constant Unit_Number_Type := Get_Source_Unit (UEs);
16448 CUs : constant Node_Id := Cunit (UNs);
16449 CIs : constant List_Id := Context_Items (CUs);
16450
16451 begin
16452 Itm := First (CIs);
16453 while Present (Itm) loop
16454 if Nkind (Itm) = N_With_Clause then
16455 Ent :=
16456 Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
16457
16458 if U = Ent then
16459 return True;
16460 end if;
16461 end if;
16462
16463 Next (Itm);
16464 end loop;
16465
16466 return False;
16467 end In_Withs_Of;
16468
16469 -------------------
16470 -- Set_Elab_Flag --
16471 -------------------
16472
16473 procedure Set_Elab_Flag (Itm : Node_Id) is
16474 begin
16475 if Nkind (N) in N_Subprogram_Instantiation then
16476 Set_Elaborate_Desirable (Itm);
16477 else
16478 Set_Elaborate_All_Desirable (Itm);
16479 end if;
16480 end Set_Elab_Flag;
16481
16482 -- Start of processing for Activate_Elaborate_All_Desirable
16483
16484 begin
16485 -- Do not set binder indication if expansion is disabled, as when
16486 -- compiling a generic unit.
16487
16488 if not Expander_Active then
16489 return;
16490 end if;
16491
16492 -- If an instance of a generic package contains a controlled object (so
16493 -- we're calling Initialize at elaboration time), and the instance is in
16494 -- a package body P that says "with P;", then we need to return without
16495 -- adding "pragma Elaborate_All (P);" to P.
16496
16497 if U = Main_Unit_Entity then
16498 return;
16499 end if;
16500
16501 Itm := First (CI);
16502 while Present (Itm) loop
16503 if Nkind (Itm) = N_With_Clause then
16504 Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
16505
16506 -- If we find it, then mark elaborate all desirable and return
16507
16508 if U = Ent then
16509 Set_Elab_Flag (Itm);
16510 return;
16511 end if;
16512 end if;
16513
16514 Next (Itm);
16515 end loop;
16516
16517 -- If we fall through then the with clause is not present in the
16518 -- current unit. One legitimate possibility is that the with clause
16519 -- is present in the spec when we are a body.
16520
16521 if Is_Body_Name (Unm)
16522 and then In_Withs_Of (Spec_Entity (UE))
16523 then
16524 Add_To_Context_And_Mark (Itm);
16525 return;
16526 end if;
16527
16528 -- Similarly, we may be in the spec or body of a child unit, where
16529 -- the unit in question is with'ed by some ancestor of the child unit.
16530
16531 if Is_Child_Name (Unm) then
16532 declare
16533 Pkg : Entity_Id;
16534
16535 begin
16536 Pkg := UE;
16537 loop
16538 Pkg := Scope (Pkg);
16539 exit when Pkg = Standard_Standard;
16540
16541 if In_Withs_Of (Pkg) then
16542 Add_To_Context_And_Mark (Itm);
16543 return;
16544 end if;
16545 end loop;
16546 end;
16547 end if;
16548
16549 -- Here if we do not find with clause on spec or body. We just ignore
16550 -- this case; it means that the elaboration involves some other unit
16551 -- than the unit being compiled, and will be caught elsewhere.
16552 end Activate_Elaborate_All_Desirable;
16553
16554 ------------------
16555 -- Check_A_Call --
16556 ------------------
16557
16558 procedure Check_A_Call
16559 (N : Node_Id;
16560 E : Entity_Id;
16561 Outer_Scope : Entity_Id;
16562 Inter_Unit_Only : Boolean;
16563 Generate_Warnings : Boolean := True;
16564 In_Init_Proc : Boolean := False)
16565 is
16566 Access_Case : constant Boolean := Nkind (N) = N_Attribute_Reference;
16567 -- Indicates if we have Access attribute case
16568
16569 function Call_To_Instance_From_Outside (Id : Entity_Id) return Boolean;
16570 -- True if we're calling an instance of a generic subprogram, or a
16571 -- subprogram in an instance of a generic package, and the call is
16572 -- outside that instance.
16573
16574 procedure Elab_Warning
16575 (Msg_D : String;
16576 Msg_S : String;
16577 Ent : Node_Or_Entity_Id);
16578 -- Generate a call to Error_Msg_NE with parameters Msg_D or Msg_S (for
16579 -- dynamic or static elaboration model), N and Ent. Msg_D is a real
16580 -- warning (output if Msg_D is non-null and Elab_Warnings is set),
16581 -- Msg_S is an info message (output if Elab_Info_Messages is set).
16582
16583 function Find_W_Scope return Entity_Id;
16584 -- Find top-level scope for called entity (not following renamings
16585 -- or derivations). This is where the Elaborate_All will go if it is
16586 -- needed. We start with the called entity, except in the case of an
16587 -- initialization procedure outside the current package, where the init
16588 -- proc is in the root package, and we start from the entity of the name
16589 -- in the call.
16590
16591 -----------------------------------
16592 -- Call_To_Instance_From_Outside --
16593 -----------------------------------
16594
16595 function Call_To_Instance_From_Outside (Id : Entity_Id) return Boolean is
16596 Scop : Entity_Id := Id;
16597
16598 begin
16599 loop
16600 if Scop = Standard_Standard then
16601 return False;
16602 end if;
16603
16604 if Is_Generic_Instance (Scop) then
16605 return not In_Open_Scopes (Scop);
16606 end if;
16607
16608 Scop := Scope (Scop);
16609 end loop;
16610 end Call_To_Instance_From_Outside;
16611
16612 ------------------
16613 -- Elab_Warning --
16614 ------------------
16615
16616 procedure Elab_Warning
16617 (Msg_D : String;
16618 Msg_S : String;
16619 Ent : Node_Or_Entity_Id)
16620 is
16621 begin
16622 -- Dynamic elaboration checks, real warning
16623
16624 if Dynamic_Elaboration_Checks then
16625 if not Access_Case then
16626 if Msg_D /= "" and then Elab_Warnings then
16627 Error_Msg_NE (Msg_D, N, Ent);
16628 end if;
16629
16630 -- In the access case emit first warning message as well,
16631 -- otherwise list of calls will appear as errors.
16632
16633 elsif Elab_Warnings then
16634 Error_Msg_NE (Msg_S, N, Ent);
16635 end if;
16636
16637 -- Static elaboration checks, info message
16638
16639 else
16640 if Elab_Info_Messages then
16641 Error_Msg_NE (Msg_S, N, Ent);
16642 end if;
16643 end if;
16644 end Elab_Warning;
16645
16646 ------------------
16647 -- Find_W_Scope --
16648 ------------------
16649
16650 function Find_W_Scope return Entity_Id is
16651 Refed_Ent : constant Entity_Id := Get_Referenced_Ent (N);
16652 W_Scope : Entity_Id;
16653
16654 begin
16655 if Is_Init_Proc (Refed_Ent)
16656 and then not In_Same_Extended_Unit (N, Refed_Ent)
16657 then
16658 W_Scope := Scope (Refed_Ent);
16659 else
16660 W_Scope := E;
16661 end if;
16662
16663 -- Now loop through scopes to get to the enclosing compilation unit
16664
16665 while not Is_Compilation_Unit (W_Scope) loop
16666 W_Scope := Scope (W_Scope);
16667 end loop;
16668
16669 return W_Scope;
16670 end Find_W_Scope;
16671
16672 -- Local variables
16673
16674 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
16675 -- Indicates if we have instantiation case
16676
16677 Loc : constant Source_Ptr := Sloc (N);
16678
16679 Variable_Case : constant Boolean :=
16680 Nkind (N) in N_Has_Entity
16681 and then Present (Entity (N))
16682 and then Ekind (Entity (N)) = E_Variable;
16683 -- Indicates if we have variable reference case
16684
16685 W_Scope : constant Entity_Id := Find_W_Scope;
16686 -- Top-level scope of directly called entity for subprogram. This
16687 -- differs from E_Scope in the case where renamings or derivations
16688 -- are involved, since it does not follow these links. W_Scope is
16689 -- generally in a visible unit, and it is this scope that may require
16690 -- an Elaborate_All. However, there are some cases (initialization
16691 -- calls and calls involving object notation) where W_Scope might not
16692 -- be in the context of the current unit, and there is an intermediate
16693 -- package that is, in which case the Elaborate_All has to be placed
16694 -- on this intermediate package. These special cases are handled in
16695 -- Set_Elaboration_Constraint.
16696
16697 Ent : Entity_Id;
16698 Callee_Unit_Internal : Boolean;
16699 Caller_Unit_Internal : Boolean;
16700 Decl : Node_Id;
16701 Inst_Callee : Source_Ptr;
16702 Inst_Caller : Source_Ptr;
16703 Unit_Callee : Unit_Number_Type;
16704 Unit_Caller : Unit_Number_Type;
16705
16706 Body_Acts_As_Spec : Boolean;
16707 -- Set to true if call is to body acting as spec (no separate spec)
16708
16709 Cunit_SC : Boolean := False;
16710 -- Set to suppress dynamic elaboration checks where one of the
16711 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
16712 -- if a pragma Elaborate[_All] applies to that scope, in which case
16713 -- warnings on the scope are also suppressed. For the internal case,
16714 -- we ignore this flag.
16715
16716 E_Scope : Entity_Id;
16717 -- Top-level scope of entity for called subprogram. This value includes
16718 -- following renamings and derivations, so this scope can be in a
16719 -- non-visible unit. This is the scope that is to be investigated to
16720 -- see whether an elaboration check is required.
16721
16722 Is_DIC : Boolean;
16723 -- Flag set when the subprogram being invoked is the procedure generated
16724 -- for pragma Default_Initial_Condition.
16725
16726 SPARK_Elab_Errors : Boolean;
16727 -- Flag set when an entity is called or a variable is read during SPARK
16728 -- dynamic elaboration.
16729
16730 -- Start of processing for Check_A_Call
16731
16732 begin
16733 -- If the call is known to be within a local Suppress Elaboration
16734 -- pragma, nothing to check. This can happen in task bodies. But
16735 -- we ignore this for a call to a generic formal.
16736
16737 if Nkind (N) in N_Subprogram_Call
16738 and then No_Elaboration_Check (N)
16739 and then not Is_Call_Of_Generic_Formal (N)
16740 then
16741 return;
16742
16743 -- If this is a rewrite of a Valid_Scalars attribute, then nothing to
16744 -- check, we don't mind in this case if the call occurs before the body
16745 -- since this is all generated code.
16746
16747 elsif Nkind (Original_Node (N)) = N_Attribute_Reference
16748 and then Attribute_Name (Original_Node (N)) = Name_Valid_Scalars
16749 then
16750 return;
16751
16752 -- Intrinsics such as instances of Unchecked_Deallocation do not have
16753 -- any body, so elaboration checking is not needed, and would be wrong.
16754
16755 elsif Is_Intrinsic_Subprogram (E) then
16756 return;
16757
16758 -- Do not consider references to internal variables for SPARK semantics
16759
16760 elsif Variable_Case and then not Comes_From_Source (E) then
16761 return;
16762 end if;
16763
16764 -- Proceed with check
16765
16766 Ent := E;
16767
16768 -- For a variable reference, just set Body_Acts_As_Spec to False
16769
16770 if Variable_Case then
16771 Body_Acts_As_Spec := False;
16772
16773 -- Additional checks for all other cases
16774
16775 else
16776 -- Go to parent for derived subprogram, or to original subprogram in
16777 -- the case of a renaming (Alias covers both these cases).
16778
16779 loop
16780 if (Suppress_Elaboration_Warnings (Ent)
16781 or else Elaboration_Checks_Suppressed (Ent))
16782 and then (Inst_Case or else No (Alias (Ent)))
16783 then
16784 return;
16785 end if;
16786
16787 -- Nothing to do for imported entities
16788
16789 if Is_Imported (Ent) then
16790 return;
16791 end if;
16792
16793 exit when Inst_Case or else No (Alias (Ent));
16794 Ent := Alias (Ent);
16795 end loop;
16796
16797 Decl := Unit_Declaration_Node (Ent);
16798
16799 if Nkind (Decl) = N_Subprogram_Body then
16800 Body_Acts_As_Spec := True;
16801
16802 elsif Nkind_In (Decl, N_Subprogram_Declaration,
16803 N_Subprogram_Body_Stub)
16804 or else Inst_Case
16805 then
16806 Body_Acts_As_Spec := False;
16807
16808 -- If we have none of an instantiation, subprogram body or subprogram
16809 -- declaration, or in the SPARK case, a variable reference, then
16810 -- it is not a case that we want to check. (One case is a call to a
16811 -- generic formal subprogram, where we do not want the check in the
16812 -- template).
16813
16814 else
16815 return;
16816 end if;
16817 end if;
16818
16819 E_Scope := Ent;
16820 loop
16821 if Elaboration_Checks_Suppressed (E_Scope)
16822 or else Suppress_Elaboration_Warnings (E_Scope)
16823 then
16824 Cunit_SC := True;
16825 end if;
16826
16827 -- Exit when we get to compilation unit, not counting subunits
16828
16829 exit when Is_Compilation_Unit (E_Scope)
16830 and then (Is_Child_Unit (E_Scope)
16831 or else Scope (E_Scope) = Standard_Standard);
16832
16833 pragma Assert (E_Scope /= Standard_Standard);
16834
16835 -- Move up a scope looking for compilation unit
16836
16837 E_Scope := Scope (E_Scope);
16838 end loop;
16839
16840 -- No checks needed for pure or preelaborated compilation units
16841
16842 if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then
16843 return;
16844 end if;
16845
16846 -- If the generic entity is within a deeper instance than we are, then
16847 -- either the instantiation to which we refer itself caused an ABE, in
16848 -- which case that will be handled separately, or else we know that the
16849 -- body we need appears as needed at the point of the instantiation.
16850 -- However, this assumption is only valid if we are in static mode.
16851
16852 if not Dynamic_Elaboration_Checks
16853 and then
16854 Instantiation_Depth (Sloc (Ent)) > Instantiation_Depth (Sloc (N))
16855 then
16856 return;
16857 end if;
16858
16859 -- Do not give a warning for a package with no body
16860
16861 if Ekind (Ent) = E_Generic_Package and then not Has_Generic_Body (N) then
16862 return;
16863 end if;
16864
16865 -- Case of entity is in same unit as call or instantiation. In the
16866 -- instantiation case, W_Scope may be different from E_Scope; we want
16867 -- the unit in which the instantiation occurs, since we're analyzing
16868 -- based on the expansion.
16869
16870 if W_Scope = C_Scope then
16871 if not Inter_Unit_Only then
16872 Check_Internal_Call (N, Ent, Outer_Scope, E);
16873 end if;
16874
16875 return;
16876 end if;
16877
16878 -- Case of entity is not in current unit (i.e. with'ed unit case)
16879
16880 -- We are only interested in such calls if the outer call was from
16881 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
16882
16883 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
16884 return;
16885 end if;
16886
16887 -- Nothing to do if some scope said that no checks were required
16888
16889 if Cunit_SC then
16890 return;
16891 end if;
16892
16893 -- Nothing to do for a generic instance, because a call to an instance
16894 -- cannot fail the elaboration check, because the body of the instance
16895 -- is always elaborated immediately after the spec.
16896
16897 if Call_To_Instance_From_Outside (Ent) then
16898 return;
16899 end if;
16900
16901 -- Nothing to do if subprogram with no separate spec. However, a call
16902 -- to Deep_Initialize may result in a call to a user-defined Initialize
16903 -- procedure, which imposes a body dependency. This happens only if the
16904 -- type is controlled and the Initialize procedure is not inherited.
16905
16906 if Body_Acts_As_Spec then
16907 if Is_TSS (Ent, TSS_Deep_Initialize) then
16908 declare
16909 Typ : constant Entity_Id := Etype (First_Formal (Ent));
16910 Init : Entity_Id;
16911
16912 begin
16913 if not Is_Controlled (Typ) then
16914 return;
16915 else
16916 Init := Find_Prim_Op (Typ, Name_Initialize);
16917
16918 if Comes_From_Source (Init) then
16919 Ent := Init;
16920 else
16921 return;
16922 end if;
16923 end if;
16924 end;
16925
16926 else
16927 return;
16928 end if;
16929 end if;
16930
16931 -- Check cases of internal units
16932
16933 Callee_Unit_Internal := In_Internal_Unit (E_Scope);
16934
16935 -- Do not give a warning if the with'ed unit is internal and this is
16936 -- the generic instantiation case (this saves a lot of hassle dealing
16937 -- with the Text_IO special child units)
16938
16939 if Callee_Unit_Internal and Inst_Case then
16940 return;
16941 end if;
16942
16943 if C_Scope = Standard_Standard then
16944 Caller_Unit_Internal := False;
16945 else
16946 Caller_Unit_Internal := In_Internal_Unit (C_Scope);
16947 end if;
16948
16949 -- Do not give a warning if the with'ed unit is internal and the caller
16950 -- is not internal (since the binder always elaborates internal units
16951 -- first).
16952
16953 if Callee_Unit_Internal and not Caller_Unit_Internal then
16954 return;
16955 end if;
16956
16957 -- For now, if debug flag -gnatdE is not set, do no checking for one
16958 -- internal unit withing another. This fixes the problem with the sgi
16959 -- build and storage errors. To be resolved later ???
16960
16961 if (Callee_Unit_Internal and Caller_Unit_Internal)
16962 and not Debug_Flag_EE
16963 then
16964 return;
16965 end if;
16966
16967 if Is_TSS (E, TSS_Deep_Initialize) then
16968 Ent := E;
16969 end if;
16970
16971 -- If the call is in an instance, and the called entity is not
16972 -- defined in the same instance, then the elaboration issue focuses
16973 -- around the unit containing the template, it is this unit that
16974 -- requires an Elaborate_All.
16975
16976 -- However, if we are doing dynamic elaboration, we need to chase the
16977 -- call in the usual manner.
16978
16979 -- We also need to chase the call in the usual manner if it is a call
16980 -- to a generic formal parameter, since that case was not handled as
16981 -- part of the processing of the template.
16982
16983 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
16984 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
16985
16986 if Inst_Caller = No_Location then
16987 Unit_Caller := No_Unit;
16988 else
16989 Unit_Caller := Get_Source_Unit (N);
16990 end if;
16991
16992 if Inst_Callee = No_Location then
16993 Unit_Callee := No_Unit;
16994 else
16995 Unit_Callee := Get_Source_Unit (Ent);
16996 end if;
16997
16998 if Unit_Caller /= No_Unit
16999 and then Unit_Callee /= Unit_Caller
17000 and then not Dynamic_Elaboration_Checks
17001 and then not Is_Call_Of_Generic_Formal (N)
17002 then
17003 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
17004
17005 -- If we don't get a spec entity, just ignore call. Not quite
17006 -- clear why this check is necessary. ???
17007
17008 if No (E_Scope) then
17009 return;
17010 end if;
17011
17012 -- Otherwise step to enclosing compilation unit
17013
17014 while not Is_Compilation_Unit (E_Scope) loop
17015 E_Scope := Scope (E_Scope);
17016 end loop;
17017
17018 -- For the case where N is not an instance, and is not a call within
17019 -- instance to other than a generic formal, we recompute E_Scope
17020 -- for the error message, since we do NOT want to go to the unit
17021 -- that has the ultimate declaration in the case of renaming and
17022 -- derivation and we also want to go to the generic unit in the
17023 -- case of an instance, and no further.
17024
17025 else
17026 -- Loop to carefully follow renamings and derivations one step
17027 -- outside the current unit, but not further.
17028
17029 if not (Inst_Case or Variable_Case)
17030 and then Present (Alias (Ent))
17031 then
17032 E_Scope := Alias (Ent);
17033 else
17034 E_Scope := Ent;
17035 end if;
17036
17037 loop
17038 while not Is_Compilation_Unit (E_Scope) loop
17039 E_Scope := Scope (E_Scope);
17040 end loop;
17041
17042 -- If E_Scope is the same as C_Scope, it means that there
17043 -- definitely was a local renaming or derivation, and we
17044 -- are not yet out of the current unit.
17045
17046 exit when E_Scope /= C_Scope;
17047 Ent := Alias (Ent);
17048 E_Scope := Ent;
17049
17050 -- If no alias, there could be a previous error, but not if we've
17051 -- already reached the outermost level (Standard).
17052
17053 if No (Ent) then
17054 return;
17055 end if;
17056 end loop;
17057 end if;
17058
17059 if Within_Elaborate_All (Current_Sem_Unit, E_Scope) then
17060 return;
17061 end if;
17062
17063 -- Determine whether the Default_Initial_Condition procedure of some
17064 -- type is being invoked.
17065
17066 Is_DIC := Ekind (Ent) = E_Procedure and then Is_DIC_Procedure (Ent);
17067
17068 -- Checks related to Default_Initial_Condition fall under the SPARK
17069 -- umbrella because this is a SPARK-specific annotation.
17070
17071 SPARK_Elab_Errors :=
17072 SPARK_Mode = On and (Is_DIC or Dynamic_Elaboration_Checks);
17073
17074 -- Now check if an Elaborate_All (or dynamic check) is needed
17075
17076 if (Elab_Info_Messages or Elab_Warnings or SPARK_Elab_Errors)
17077 and then Generate_Warnings
17078 and then not Suppress_Elaboration_Warnings (Ent)
17079 and then not Elaboration_Checks_Suppressed (Ent)
17080 and then not Suppress_Elaboration_Warnings (E_Scope)
17081 and then not Elaboration_Checks_Suppressed (E_Scope)
17082 then
17083 -- Instantiation case
17084
17085 if Inst_Case then
17086 if Comes_From_Source (Ent) and then SPARK_Elab_Errors then
17087 Error_Msg_NE
17088 ("instantiation of & during elaboration in SPARK", N, Ent);
17089 else
17090 Elab_Warning
17091 ("instantiation of & may raise Program_Error?l?",
17092 "info: instantiation of & during elaboration?$?", Ent);
17093 end if;
17094
17095 -- Indirect call case, info message only in static elaboration
17096 -- case, because the attribute reference itself cannot raise an
17097 -- exception. Note that SPARK does not permit indirect calls.
17098
17099 elsif Access_Case then
17100 Elab_Warning ("", "info: access to & during elaboration?$?", Ent);
17101
17102 -- Variable reference in SPARK mode
17103
17104 elsif Variable_Case then
17105 if Comes_From_Source (Ent) and then SPARK_Elab_Errors then
17106 Error_Msg_NE
17107 ("reference to & during elaboration in SPARK", N, Ent);
17108 end if;
17109
17110 -- Subprogram call case
17111
17112 else
17113 if Nkind (Name (N)) in N_Has_Entity
17114 and then Is_Init_Proc (Entity (Name (N)))
17115 and then Comes_From_Source (Ent)
17116 then
17117 Elab_Warning
17118 ("implicit call to & may raise Program_Error?l?",
17119 "info: implicit call to & during elaboration?$?",
17120 Ent);
17121
17122 elsif SPARK_Elab_Errors then
17123
17124 -- Emit a specialized error message when the elaboration of an
17125 -- object of a private type evaluates the expression of pragma
17126 -- Default_Initial_Condition. This prevents the internal name
17127 -- of the procedure from appearing in the error message.
17128
17129 if Is_DIC then
17130 Error_Msg_N
17131 ("call to Default_Initial_Condition during elaboration in "
17132 & "SPARK", N);
17133 else
17134 Error_Msg_NE
17135 ("call to & during elaboration in SPARK", N, Ent);
17136 end if;
17137
17138 else
17139 Elab_Warning
17140 ("call to & may raise Program_Error?l?",
17141 "info: call to & during elaboration?$?",
17142 Ent);
17143 end if;
17144 end if;
17145
17146 Error_Msg_Qual_Level := Nat'Last;
17147
17148 -- Case of Elaborate_All not present and required, for SPARK this
17149 -- is an error, so give an error message.
17150
17151 if SPARK_Elab_Errors then
17152 Error_Msg_NE -- CODEFIX
17153 ("\Elaborate_All pragma required for&", N, W_Scope);
17154
17155 -- Otherwise we generate an implicit pragma. For a subprogram
17156 -- instantiation, Elaborate is good enough, since no transitive
17157 -- call is possible at elaboration time in this case.
17158
17159 elsif Nkind (N) in N_Subprogram_Instantiation then
17160 Elab_Warning
17161 ("\missing pragma Elaborate for&?l?",
17162 "\implicit pragma Elaborate for& generated?$?",
17163 W_Scope);
17164
17165 -- For all other cases, we need an implicit Elaborate_All
17166
17167 else
17168 Elab_Warning
17169 ("\missing pragma Elaborate_All for&?l?",
17170 "\implicit pragma Elaborate_All for & generated?$?",
17171 W_Scope);
17172 end if;
17173
17174 Error_Msg_Qual_Level := 0;
17175
17176 -- Take into account the flags related to elaboration warning
17177 -- messages when enumerating the various calls involved. This
17178 -- ensures the proper pairing of the main warning and the
17179 -- clarification messages generated by Output_Calls.
17180
17181 Output_Calls (N, Check_Elab_Flag => True);
17182
17183 -- Set flag to prevent further warnings for same unit unless in
17184 -- All_Errors_Mode.
17185
17186 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
17187 Set_Suppress_Elaboration_Warnings (W_Scope);
17188 end if;
17189 end if;
17190
17191 -- Check for runtime elaboration check required
17192
17193 if Dynamic_Elaboration_Checks then
17194 if not Elaboration_Checks_Suppressed (Ent)
17195 and then not Elaboration_Checks_Suppressed (W_Scope)
17196 and then not Elaboration_Checks_Suppressed (E_Scope)
17197 and then not Cunit_SC
17198 then
17199 -- Runtime elaboration check required. Generate check of the
17200 -- elaboration Boolean for the unit containing the entity.
17201
17202 -- Note that for this case, we do check the real unit (the one
17203 -- from following renamings, since that is the issue).
17204
17205 -- Could this possibly miss a useless but required PE???
17206
17207 Insert_Elab_Check (N,
17208 Make_Attribute_Reference (Loc,
17209 Attribute_Name => Name_Elaborated,
17210 Prefix =>
17211 New_Occurrence_Of (Spec_Entity (E_Scope), Loc)));
17212
17213 -- Prevent duplicate elaboration checks on the same call, which
17214 -- can happen if the body enclosing the call appears itself in a
17215 -- call whose elaboration check is delayed.
17216
17217 if Nkind (N) in N_Subprogram_Call then
17218 Set_No_Elaboration_Check (N);
17219 end if;
17220 end if;
17221
17222 -- Case of static elaboration model
17223
17224 else
17225 -- Do not do anything if elaboration checks suppressed. Note that
17226 -- we check Ent here, not E, since we want the real entity for the
17227 -- body to see if checks are suppressed for it, not the dummy
17228 -- entry for renamings or derivations.
17229
17230 if Elaboration_Checks_Suppressed (Ent)
17231 or else Elaboration_Checks_Suppressed (E_Scope)
17232 or else Elaboration_Checks_Suppressed (W_Scope)
17233 then
17234 null;
17235
17236 -- Do not generate an Elaborate_All for finalization routines
17237 -- that perform partial clean up as part of initialization.
17238
17239 elsif In_Init_Proc and then Is_Finalization_Procedure (Ent) then
17240 null;
17241
17242 -- Here we need to generate an implicit elaborate all
17243
17244 else
17245 -- Generate Elaborate_All warning unless suppressed
17246
17247 if (Elab_Info_Messages and Generate_Warnings and not Inst_Case)
17248 and then not Suppress_Elaboration_Warnings (Ent)
17249 and then not Suppress_Elaboration_Warnings (E_Scope)
17250 and then not Suppress_Elaboration_Warnings (W_Scope)
17251 then
17252 Error_Msg_Node_2 := W_Scope;
17253 Error_Msg_NE
17254 ("info: call to& in elaboration code requires pragma "
17255 & "Elaborate_All on&?$?", N, E);
17256 end if;
17257
17258 -- Set indication for binder to generate Elaborate_All
17259
17260 Set_Elaboration_Constraint (N, E, W_Scope);
17261 end if;
17262 end if;
17263 end Check_A_Call;
17264
17265 -----------------------------
17266 -- Check_Bad_Instantiation --
17267 -----------------------------
17268
17269 procedure Check_Bad_Instantiation (N : Node_Id) is
17270 Ent : Entity_Id;
17271
17272 begin
17273 -- Nothing to do if we do not have an instantiation (happens in some
17274 -- error cases, and also in the formal package declaration case)
17275
17276 if Nkind (N) not in N_Generic_Instantiation then
17277 return;
17278
17279 -- Nothing to do if serious errors detected (avoid cascaded errors)
17280
17281 elsif Serious_Errors_Detected /= 0 then
17282 return;
17283
17284 -- Nothing to do if not in full analysis mode
17285
17286 elsif not Full_Analysis then
17287 return;
17288
17289 -- Nothing to do if inside a generic template
17290
17291 elsif Inside_A_Generic then
17292 return;
17293
17294 -- Nothing to do if a library level instantiation
17295
17296 elsif Nkind (Parent (N)) = N_Compilation_Unit then
17297 return;
17298
17299 -- Nothing to do if we are compiling a proper body for semantic
17300 -- purposes only. The generic body may be in another proper body.
17301
17302 elsif
17303 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
17304 then
17305 return;
17306 end if;
17307
17308 Ent := Get_Generic_Entity (N);
17309
17310 -- The case we are interested in is when the generic spec is in the
17311 -- current declarative part
17312
17313 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
17314 or else not In_Same_Extended_Unit (N, Ent)
17315 then
17316 return;
17317 end if;
17318
17319 -- If the generic entity is within a deeper instance than we are, then
17320 -- either the instantiation to which we refer itself caused an ABE, in
17321 -- which case that will be handled separately. Otherwise, we know that
17322 -- the body we need appears as needed at the point of the instantiation.
17323 -- If they are both at the same level but not within the same instance
17324 -- then the body of the generic will be in the earlier instance.
17325
17326 declare
17327 D1 : constant Nat := Instantiation_Depth (Sloc (Ent));
17328 D2 : constant Nat := Instantiation_Depth (Sloc (N));
17329
17330 begin
17331 if D1 > D2 then
17332 return;
17333
17334 elsif D1 = D2
17335 and then Is_Generic_Instance (Scope (Ent))
17336 and then not In_Open_Scopes (Scope (Ent))
17337 then
17338 return;
17339 end if;
17340 end;
17341
17342 -- Now we can proceed, if the entity being called has a completion,
17343 -- then we are definitely OK, since we have already seen the body.
17344
17345 if Has_Completion (Ent) then
17346 return;
17347 end if;
17348
17349 -- If there is no body, then nothing to do
17350
17351 if not Has_Generic_Body (N) then
17352 return;
17353 end if;
17354
17355 -- Here we definitely have a bad instantiation
17356
17357 Error_Msg_Warn := SPARK_Mode /= On;
17358 Error_Msg_NE ("cannot instantiate& before body seen<<", N, Ent);
17359 Error_Msg_N ("\Program_Error [<<", N);
17360
17361 Insert_Elab_Check (N);
17362 Set_Is_Known_Guaranteed_ABE (N);
17363 end Check_Bad_Instantiation;
17364
17365 ---------------------
17366 -- Check_Elab_Call --
17367 ---------------------
17368
17369 procedure Check_Elab_Call
17370 (N : Node_Id;
17371 Outer_Scope : Entity_Id := Empty;
17372 In_Init_Proc : Boolean := False)
17373 is
17374 Ent : Entity_Id;
17375 P : Node_Id;
17376
17377 begin
17378 pragma Assert (Legacy_Elaboration_Checks);
17379
17380 -- If the reference is not in the main unit, there is nothing to check.
17381 -- Elaboration call from units in the context of the main unit will lead
17382 -- to semantic dependencies when those units are compiled.
17383
17384 if not In_Extended_Main_Code_Unit (N) then
17385 return;
17386 end if;
17387
17388 -- For an entry call, check relevant restriction
17389
17390 if Nkind (N) = N_Entry_Call_Statement
17391 and then not In_Subprogram_Or_Concurrent_Unit
17392 then
17393 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
17394
17395 -- Nothing to do if this is not an expected type of reference (happens
17396 -- in some error conditions, and in some cases where rewriting occurs).
17397
17398 elsif Nkind (N) not in N_Subprogram_Call
17399 and then Nkind (N) /= N_Attribute_Reference
17400 and then (SPARK_Mode /= On
17401 or else Nkind (N) not in N_Has_Entity
17402 or else No (Entity (N))
17403 or else Ekind (Entity (N)) /= E_Variable)
17404 then
17405 return;
17406
17407 -- Nothing to do if this is a call already rewritten for elab checking.
17408 -- Such calls appear as the targets of If_Expressions.
17409
17410 -- This check MUST be wrong, it catches far too much
17411
17412 elsif Nkind (Parent (N)) = N_If_Expression then
17413 return;
17414
17415 -- Nothing to do if inside a generic template
17416
17417 elsif Inside_A_Generic
17418 and then No (Enclosing_Generic_Body (N))
17419 then
17420 return;
17421
17422 -- Nothing to do if call is being preanalyzed, as when within a
17423 -- pre/postcondition, a predicate, or an invariant.
17424
17425 elsif In_Spec_Expression then
17426 return;
17427 end if;
17428
17429 -- Nothing to do if this is a call to a postcondition, which is always
17430 -- within a subprogram body, even though the current scope may be the
17431 -- enclosing scope of the subprogram.
17432
17433 if Nkind (N) = N_Procedure_Call_Statement
17434 and then Is_Entity_Name (Name (N))
17435 and then Chars (Entity (Name (N))) = Name_uPostconditions
17436 then
17437 return;
17438 end if;
17439
17440 -- Here we have a reference at elaboration time that must be checked
17441
17442 if Debug_Flag_Underscore_LL then
17443 Write_Str (" Check_Elab_Ref: ");
17444
17445 if Nkind (N) = N_Attribute_Reference then
17446 if not Is_Entity_Name (Prefix (N)) then
17447 Write_Str ("<<not entity name>>");
17448 else
17449 Write_Name (Chars (Entity (Prefix (N))));
17450 end if;
17451
17452 Write_Str ("'Access");
17453
17454 elsif No (Name (N)) or else not Is_Entity_Name (Name (N)) then
17455 Write_Str ("<<not entity name>> ");
17456
17457 else
17458 Write_Name (Chars (Entity (Name (N))));
17459 end if;
17460
17461 Write_Str (" reference at ");
17462 Write_Location (Sloc (N));
17463 Write_Eol;
17464 end if;
17465
17466 -- Climb up the tree to make sure we are not inside default expression
17467 -- of a parameter specification or a record component, since in both
17468 -- these cases, we will be doing the actual reference later, not now,
17469 -- and it is at the time of the actual reference (statically speaking)
17470 -- that we must do our static check, not at the time of its initial
17471 -- analysis).
17472
17473 -- However, we have to check references within component definitions
17474 -- (e.g. a function call that determines an array component bound),
17475 -- so we terminate the loop in that case.
17476
17477 P := Parent (N);
17478 while Present (P) loop
17479 if Nkind_In (P, N_Parameter_Specification,
17480 N_Component_Declaration)
17481 then
17482 return;
17483
17484 -- The reference occurs within the constraint of a component,
17485 -- so it must be checked.
17486
17487 elsif Nkind (P) = N_Component_Definition then
17488 exit;
17489
17490 else
17491 P := Parent (P);
17492 end if;
17493 end loop;
17494
17495 -- Stuff that happens only at the outer level
17496
17497 if No (Outer_Scope) then
17498 Elab_Visited.Set_Last (0);
17499
17500 -- Nothing to do if current scope is Standard (this is a bit odd, but
17501 -- it happens in the case of generic instantiations).
17502
17503 C_Scope := Current_Scope;
17504
17505 if C_Scope = Standard_Standard then
17506 return;
17507 end if;
17508
17509 -- First case, we are in elaboration code
17510
17511 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
17512
17513 if From_Elab_Code then
17514
17515 -- Complain if ref that comes from source in preelaborated unit
17516 -- and we are not inside a subprogram (i.e. we are in elab code).
17517
17518 if Comes_From_Source (N)
17519 and then In_Preelaborated_Unit
17520 and then not In_Inlined_Body
17521 and then Nkind (N) /= N_Attribute_Reference
17522 then
17523 -- This is a warning in GNAT mode allowing such calls to be
17524 -- used in the predefined library with appropriate care.
17525
17526 Error_Msg_Warn := GNAT_Mode;
17527 Error_Msg_N
17528 ("<<non-static call not allowed in preelaborated unit", N);
17529 return;
17530 end if;
17531
17532 -- Second case, we are inside a subprogram or concurrent unit, which
17533 -- means we are not in elaboration code.
17534
17535 else
17536 -- In this case, the issue is whether we are inside the
17537 -- declarative part of the unit in which we live, or inside its
17538 -- statements. In the latter case, there is no issue of ABE calls
17539 -- at this level (a call from outside to the unit in which we live
17540 -- might cause an ABE, but that will be detected when we analyze
17541 -- that outer level call, as it recurses into the called unit).
17542
17543 -- Climb up the tree, doing this test, and also testing for being
17544 -- inside a default expression, which, as discussed above, is not
17545 -- checked at this stage.
17546
17547 declare
17548 P : Node_Id;
17549 L : List_Id;
17550
17551 begin
17552 P := N;
17553 loop
17554 -- If we find a parentless subtree, it seems safe to assume
17555 -- that we are not in a declarative part and that no
17556 -- checking is required.
17557
17558 if No (P) then
17559 return;
17560 end if;
17561
17562 if Is_List_Member (P) then
17563 L := List_Containing (P);
17564 P := Parent (L);
17565 else
17566 L := No_List;
17567 P := Parent (P);
17568 end if;
17569
17570 exit when Nkind (P) = N_Subunit;
17571
17572 -- Filter out case of default expressions, where we do not
17573 -- do the check at this stage.
17574
17575 if Nkind_In (P, N_Parameter_Specification,
17576 N_Component_Declaration)
17577 then
17578 return;
17579 end if;
17580
17581 -- A protected body has no elaboration code and contains
17582 -- only other bodies.
17583
17584 if Nkind (P) = N_Protected_Body then
17585 return;
17586
17587 elsif Nkind_In (P, N_Subprogram_Body,
17588 N_Task_Body,
17589 N_Block_Statement,
17590 N_Entry_Body)
17591 then
17592 if L = Declarations (P) then
17593 exit;
17594
17595 -- We are not in elaboration code, but we are doing
17596 -- dynamic elaboration checks, in this case, we still
17597 -- need to do the reference, since the subprogram we are
17598 -- in could be called from another unit, also in dynamic
17599 -- elaboration check mode, at elaboration time.
17600
17601 elsif Dynamic_Elaboration_Checks then
17602
17603 -- We provide a debug flag to disable this check. That
17604 -- way we have an easy work around for regressions
17605 -- that are caused by this new check. This debug flag
17606 -- can be removed later.
17607
17608 if Debug_Flag_DD then
17609 return;
17610 end if;
17611
17612 -- Do the check in this case
17613
17614 exit;
17615
17616 elsif Nkind (P) = N_Task_Body then
17617
17618 -- The check is deferred until Check_Task_Activation
17619 -- but we need to capture local suppress pragmas
17620 -- that may inhibit checks on this call.
17621
17622 Ent := Get_Referenced_Ent (N);
17623
17624 if No (Ent) then
17625 return;
17626
17627 elsif Elaboration_Checks_Suppressed (Current_Scope)
17628 or else Elaboration_Checks_Suppressed (Ent)
17629 or else Elaboration_Checks_Suppressed (Scope (Ent))
17630 then
17631 if Nkind (N) in N_Subprogram_Call then
17632 Set_No_Elaboration_Check (N);
17633 end if;
17634 end if;
17635
17636 return;
17637
17638 -- Static model, call is not in elaboration code, we
17639 -- never need to worry, because in the static model the
17640 -- top-level caller always takes care of things.
17641
17642 else
17643 return;
17644 end if;
17645 end if;
17646 end loop;
17647 end;
17648 end if;
17649 end if;
17650
17651 Ent := Get_Referenced_Ent (N);
17652
17653 if No (Ent) then
17654 return;
17655 end if;
17656
17657 -- Determine whether a prior call to the same subprogram was already
17658 -- examined within the same context. If this is the case, then there is
17659 -- no need to proceed with the various warnings and checks because the
17660 -- work was already done for the previous call.
17661
17662 declare
17663 Self : constant Visited_Element :=
17664 (Subp_Id => Ent, Context => Parent (N));
17665
17666 begin
17667 for Index in 1 .. Elab_Visited.Last loop
17668 if Self = Elab_Visited.Table (Index) then
17669 return;
17670 end if;
17671 end loop;
17672 end;
17673
17674 -- See if we need to analyze this reference. We analyze it if either of
17675 -- the following conditions is met:
17676
17677 -- It is an inner level call (since in this case it was triggered
17678 -- by an outer level call from elaboration code), but only if the
17679 -- call is within the scope of the original outer level call.
17680
17681 -- It is an outer level reference from elaboration code, or a call to
17682 -- an entity is in the same elaboration scope.
17683
17684 -- And in these cases, we will check both inter-unit calls and
17685 -- intra-unit (within a single unit) calls.
17686
17687 C_Scope := Current_Scope;
17688
17689 -- If not outer level reference, then we follow it if it is within the
17690 -- original scope of the outer reference.
17691
17692 if Present (Outer_Scope)
17693 and then Within (Scope (Ent), Outer_Scope)
17694 then
17695 Set_C_Scope;
17696 Check_A_Call
17697 (N => N,
17698 E => Ent,
17699 Outer_Scope => Outer_Scope,
17700 Inter_Unit_Only => False,
17701 In_Init_Proc => In_Init_Proc);
17702
17703 -- Nothing to do if elaboration checks suppressed for this scope.
17704 -- However, an interesting exception, the fact that elaboration checks
17705 -- are suppressed within an instance (because we can trace the body when
17706 -- we process the template) does not extend to calls to generic formal
17707 -- subprograms.
17708
17709 elsif Elaboration_Checks_Suppressed (Current_Scope)
17710 and then not Is_Call_Of_Generic_Formal (N)
17711 then
17712 null;
17713
17714 elsif From_Elab_Code then
17715 Set_C_Scope;
17716 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
17717
17718 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
17719 Set_C_Scope;
17720 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
17721
17722 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
17723 -- is set, then we will do the check, but only in the inter-unit case
17724 -- (this is to accommodate unguarded elaboration calls from other units
17725 -- in which this same mode is set). We don't want warnings in this case,
17726 -- it would generate warnings having nothing to do with elaboration.
17727
17728 elsif Dynamic_Elaboration_Checks then
17729 Set_C_Scope;
17730 Check_A_Call
17731 (N,
17732 Ent,
17733 Standard_Standard,
17734 Inter_Unit_Only => True,
17735 Generate_Warnings => False);
17736
17737 -- Otherwise nothing to do
17738
17739 else
17740 return;
17741 end if;
17742
17743 -- A call to an Init_Proc in elaboration code may bring additional
17744 -- dependencies, if some of the record components thereof have
17745 -- initializations that are function calls that come from source. We
17746 -- treat the current node as a call to each of these functions, to check
17747 -- their elaboration impact.
17748
17749 if Is_Init_Proc (Ent) and then From_Elab_Code then
17750 Process_Init_Proc : declare
17751 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
17752
17753 function Check_Init_Call (Nod : Node_Id) return Traverse_Result;
17754 -- Find subprogram calls within body of Init_Proc for Traverse
17755 -- instantiation below.
17756
17757 procedure Traverse_Body is new Traverse_Proc (Check_Init_Call);
17758 -- Traversal procedure to find all calls with body of Init_Proc
17759
17760 ---------------------
17761 -- Check_Init_Call --
17762 ---------------------
17763
17764 function Check_Init_Call (Nod : Node_Id) return Traverse_Result is
17765 Func : Entity_Id;
17766
17767 begin
17768 if Nkind (Nod) in N_Subprogram_Call
17769 and then Is_Entity_Name (Name (Nod))
17770 then
17771 Func := Entity (Name (Nod));
17772
17773 if Comes_From_Source (Func) then
17774 Check_A_Call
17775 (N, Func, Standard_Standard, Inter_Unit_Only => True);
17776 end if;
17777
17778 return OK;
17779
17780 else
17781 return OK;
17782 end if;
17783 end Check_Init_Call;
17784
17785 -- Start of processing for Process_Init_Proc
17786
17787 begin
17788 if Nkind (Unit_Decl) = N_Subprogram_Body then
17789 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
17790 end if;
17791 end Process_Init_Proc;
17792 end if;
17793 end Check_Elab_Call;
17794
17795 -----------------------
17796 -- Check_Elab_Assign --
17797 -----------------------
17798
17799 procedure Check_Elab_Assign (N : Node_Id) is
17800 Ent : Entity_Id;
17801 Scop : Entity_Id;
17802
17803 Pkg_Spec : Entity_Id;
17804 Pkg_Body : Entity_Id;
17805
17806 begin
17807 pragma Assert (Legacy_Elaboration_Checks);
17808
17809 -- For record or array component, check prefix. If it is an access type,
17810 -- then there is nothing to do (we do not know what is being assigned),
17811 -- but otherwise this is an assignment to the prefix.
17812
17813 if Nkind_In (N, N_Indexed_Component,
17814 N_Selected_Component,
17815 N_Slice)
17816 then
17817 if not Is_Access_Type (Etype (Prefix (N))) then
17818 Check_Elab_Assign (Prefix (N));
17819 end if;
17820
17821 return;
17822 end if;
17823
17824 -- For type conversion, check expression
17825
17826 if Nkind (N) = N_Type_Conversion then
17827 Check_Elab_Assign (Expression (N));
17828 return;
17829 end if;
17830
17831 -- Nothing to do if this is not an entity reference otherwise get entity
17832
17833 if Is_Entity_Name (N) then
17834 Ent := Entity (N);
17835 else
17836 return;
17837 end if;
17838
17839 -- What we are looking for is a reference in the body of a package that
17840 -- modifies a variable declared in the visible part of the package spec.
17841
17842 if Present (Ent)
17843 and then Comes_From_Source (N)
17844 and then not Suppress_Elaboration_Warnings (Ent)
17845 and then Ekind (Ent) = E_Variable
17846 and then not In_Private_Part (Ent)
17847 and then Is_Library_Level_Entity (Ent)
17848 then
17849 Scop := Current_Scope;
17850 loop
17851 if No (Scop) or else Scop = Standard_Standard then
17852 return;
17853 elsif Ekind (Scop) = E_Package
17854 and then Is_Compilation_Unit (Scop)
17855 then
17856 exit;
17857 else
17858 Scop := Scope (Scop);
17859 end if;
17860 end loop;
17861
17862 -- Here Scop points to the containing library package
17863
17864 Pkg_Spec := Scop;
17865 Pkg_Body := Body_Entity (Pkg_Spec);
17866
17867 -- All OK if the package has an Elaborate_Body pragma
17868
17869 if Has_Pragma_Elaborate_Body (Scop) then
17870 return;
17871 end if;
17872
17873 -- OK if entity being modified is not in containing package spec
17874
17875 if not In_Same_Source_Unit (Scop, Ent) then
17876 return;
17877 end if;
17878
17879 -- All OK if entity appears in generic package or generic instance.
17880 -- We just get too messed up trying to give proper warnings in the
17881 -- presence of generics. Better no message than a junk one.
17882
17883 Scop := Scope (Ent);
17884 while Present (Scop) and then Scop /= Pkg_Spec loop
17885 if Ekind (Scop) = E_Generic_Package then
17886 return;
17887 elsif Ekind (Scop) = E_Package
17888 and then Is_Generic_Instance (Scop)
17889 then
17890 return;
17891 end if;
17892
17893 Scop := Scope (Scop);
17894 end loop;
17895
17896 -- All OK if in task, don't issue warnings there
17897
17898 if In_Task_Activation then
17899 return;
17900 end if;
17901
17902 -- OK if no package body
17903
17904 if No (Pkg_Body) then
17905 return;
17906 end if;
17907
17908 -- OK if reference is not in package body
17909
17910 if not In_Same_Source_Unit (Pkg_Body, N) then
17911 return;
17912 end if;
17913
17914 -- OK if package body has no handled statement sequence
17915
17916 declare
17917 HSS : constant Node_Id :=
17918 Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
17919 begin
17920 if No (HSS) or else not Comes_From_Source (HSS) then
17921 return;
17922 end if;
17923 end;
17924
17925 -- We definitely have a case of a modification of an entity in
17926 -- the package spec from the elaboration code of the package body.
17927 -- We may not give the warning (because there are some additional
17928 -- checks to avoid too many false positives), but it would be a good
17929 -- idea for the binder to try to keep the body elaboration close to
17930 -- the spec elaboration.
17931
17932 Set_Elaborate_Body_Desirable (Pkg_Spec);
17933
17934 -- All OK in gnat mode (we know what we are doing)
17935
17936 if GNAT_Mode then
17937 return;
17938 end if;
17939
17940 -- All OK if all warnings suppressed
17941
17942 if Warning_Mode = Suppress then
17943 return;
17944 end if;
17945
17946 -- All OK if elaboration checks suppressed for entity
17947
17948 if Checks_May_Be_Suppressed (Ent)
17949 and then Is_Check_Suppressed (Ent, Elaboration_Check)
17950 then
17951 return;
17952 end if;
17953
17954 -- OK if the entity is initialized. Note that the No_Initialization
17955 -- flag usually means that the initialization has been rewritten into
17956 -- assignments, but that still counts for us.
17957
17958 declare
17959 Decl : constant Node_Id := Declaration_Node (Ent);
17960 begin
17961 if Nkind (Decl) = N_Object_Declaration
17962 and then (Present (Expression (Decl))
17963 or else No_Initialization (Decl))
17964 then
17965 return;
17966 end if;
17967 end;
17968
17969 -- Here is where we give the warning
17970
17971 -- All OK if warnings suppressed on the entity
17972
17973 if not Has_Warnings_Off (Ent) then
17974 Error_Msg_Sloc := Sloc (Ent);
17975
17976 Error_Msg_NE
17977 ("??& can be accessed by clients before this initialization",
17978 N, Ent);
17979 Error_Msg_NE
17980 ("\??add Elaborate_Body to spec to ensure & is initialized",
17981 N, Ent);
17982 end if;
17983
17984 if not All_Errors_Mode then
17985 Set_Suppress_Elaboration_Warnings (Ent);
17986 end if;
17987 end if;
17988 end Check_Elab_Assign;
17989
17990 ----------------------
17991 -- Check_Elab_Calls --
17992 ----------------------
17993
17994 -- WARNING: This routine manages SPARK regions
17995
17996 procedure Check_Elab_Calls is
17997 Saved_SM : SPARK_Mode_Type;
17998 Saved_SMP : Node_Id;
17999
18000 begin
18001 pragma Assert (Legacy_Elaboration_Checks);
18002
18003 -- If expansion is disabled, do not generate any checks, unless we
18004 -- are in GNATprove mode, so that errors are issued in GNATprove for
18005 -- violations of static elaboration rules in SPARK code. Also skip
18006 -- checks if any subunits are missing because in either case we lack the
18007 -- full information that we need, and no object file will be created in
18008 -- any case.
18009
18010 if (not Expander_Active and not GNATprove_Mode)
18011 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
18012 or else Subunits_Missing
18013 then
18014 return;
18015 end if;
18016
18017 -- Skip delayed calls if we had any errors
18018
18019 if Serious_Errors_Detected = 0 then
18020 Delaying_Elab_Checks := False;
18021 Expander_Mode_Save_And_Set (True);
18022
18023 for J in Delay_Check.First .. Delay_Check.Last loop
18024 Push_Scope (Delay_Check.Table (J).Curscop);
18025 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
18026 In_Task_Activation := Delay_Check.Table (J).In_Task_Activation;
18027
18028 Saved_SM := SPARK_Mode;
18029 Saved_SMP := SPARK_Mode_Pragma;
18030
18031 -- Set appropriate value of SPARK_Mode
18032
18033 if Delay_Check.Table (J).From_SPARK_Code then
18034 SPARK_Mode := On;
18035 end if;
18036
18037 Check_Internal_Call_Continue
18038 (N => Delay_Check.Table (J).N,
18039 E => Delay_Check.Table (J).E,
18040 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
18041 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
18042
18043 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
18044 Pop_Scope;
18045 end loop;
18046
18047 -- Set Delaying_Elab_Checks back on for next main compilation
18048
18049 Expander_Mode_Restore;
18050 Delaying_Elab_Checks := True;
18051 end if;
18052 end Check_Elab_Calls;
18053
18054 ------------------------------
18055 -- Check_Elab_Instantiation --
18056 ------------------------------
18057
18058 procedure Check_Elab_Instantiation
18059 (N : Node_Id;
18060 Outer_Scope : Entity_Id := Empty)
18061 is
18062 Ent : Entity_Id;
18063
18064 begin
18065 pragma Assert (Legacy_Elaboration_Checks);
18066
18067 -- Check for and deal with bad instantiation case. There is some
18068 -- duplicated code here, but we will worry about this later ???
18069
18070 Check_Bad_Instantiation (N);
18071
18072 if Is_Known_Guaranteed_ABE (N) then
18073 return;
18074 end if;
18075
18076 -- Nothing to do if we do not have an instantiation (happens in some
18077 -- error cases, and also in the formal package declaration case)
18078
18079 if Nkind (N) not in N_Generic_Instantiation then
18080 return;
18081 end if;
18082
18083 -- Nothing to do if inside a generic template
18084
18085 if Inside_A_Generic then
18086 return;
18087 end if;
18088
18089 -- Nothing to do if the instantiation is not in the main unit
18090
18091 if not In_Extended_Main_Code_Unit (N) then
18092 return;
18093 end if;
18094
18095 Ent := Get_Generic_Entity (N);
18096 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
18097
18098 -- See if we need to analyze this instantiation. We analyze it if
18099 -- either of the following conditions is met:
18100
18101 -- It is an inner level instantiation (since in this case it was
18102 -- triggered by an outer level call from elaboration code), but
18103 -- only if the instantiation is within the scope of the original
18104 -- outer level call.
18105
18106 -- It is an outer level instantiation from elaboration code, or the
18107 -- instantiated entity is in the same elaboration scope.
18108
18109 -- And in these cases, we will check both the inter-unit case and
18110 -- the intra-unit (within a single unit) case.
18111
18112 C_Scope := Current_Scope;
18113
18114 if Present (Outer_Scope) and then Within (Scope (Ent), Outer_Scope) then
18115 Set_C_Scope;
18116 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
18117
18118 elsif From_Elab_Code then
18119 Set_C_Scope;
18120 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
18121
18122 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
18123 Set_C_Scope;
18124 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
18125
18126 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
18127 -- set, then we will do the check, but only in the inter-unit case (this
18128 -- is to accommodate unguarded elaboration calls from other units in
18129 -- which this same mode is set). We inhibit warnings in this case, since
18130 -- this instantiation is not occurring in elaboration code.
18131
18132 elsif Dynamic_Elaboration_Checks then
18133 Set_C_Scope;
18134 Check_A_Call
18135 (N,
18136 Ent,
18137 Standard_Standard,
18138 Inter_Unit_Only => True,
18139 Generate_Warnings => False);
18140
18141 else
18142 return;
18143 end if;
18144 end Check_Elab_Instantiation;
18145
18146 -------------------------
18147 -- Check_Internal_Call --
18148 -------------------------
18149
18150 procedure Check_Internal_Call
18151 (N : Node_Id;
18152 E : Entity_Id;
18153 Outer_Scope : Entity_Id;
18154 Orig_Ent : Entity_Id)
18155 is
18156 function Within_Initial_Condition (Call : Node_Id) return Boolean;
18157 -- Determine whether call Call occurs within pragma Initial_Condition or
18158 -- pragma Check with check_kind set to Initial_Condition.
18159
18160 ------------------------------
18161 -- Within_Initial_Condition --
18162 ------------------------------
18163
18164 function Within_Initial_Condition (Call : Node_Id) return Boolean is
18165 Args : List_Id;
18166 Nam : Name_Id;
18167 Par : Node_Id;
18168
18169 begin
18170 -- Traverse the parent chain looking for an enclosing pragma
18171
18172 Par := Call;
18173 while Present (Par) loop
18174 if Nkind (Par) = N_Pragma then
18175 Nam := Pragma_Name (Par);
18176
18177 -- Pragma Initial_Condition appears in its alternative from as
18178 -- Check (Initial_Condition, ...).
18179
18180 if Nam = Name_Check then
18181 Args := Pragma_Argument_Associations (Par);
18182
18183 -- Pragma Check should have at least two arguments
18184
18185 pragma Assert (Present (Args));
18186
18187 return
18188 Chars (Expression (First (Args))) = Name_Initial_Condition;
18189
18190 -- Direct match
18191
18192 elsif Nam = Name_Initial_Condition then
18193 return True;
18194
18195 -- Since pragmas are never nested within other pragmas, stop
18196 -- the traversal.
18197
18198 else
18199 return False;
18200 end if;
18201
18202 -- Prevent the search from going too far
18203
18204 elsif Is_Body_Or_Package_Declaration (Par) then
18205 exit;
18206 end if;
18207
18208 Par := Parent (Par);
18209
18210 -- If assertions are not enabled, the check pragma is rewritten
18211 -- as an if_statement in sem_prag, to generate various warnings
18212 -- on boolean expressions. Retrieve the original pragma.
18213
18214 if Nkind (Original_Node (Par)) = N_Pragma then
18215 Par := Original_Node (Par);
18216 end if;
18217 end loop;
18218
18219 return False;
18220 end Within_Initial_Condition;
18221
18222 -- Local variables
18223
18224 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
18225
18226 -- Start of processing for Check_Internal_Call
18227
18228 begin
18229 -- For P'Access, we want to warn if the -gnatw.f switch is set, and the
18230 -- node comes from source.
18231
18232 if Nkind (N) = N_Attribute_Reference
18233 and then ((not Warn_On_Elab_Access and then not Debug_Flag_Dot_O)
18234 or else not Comes_From_Source (N))
18235 then
18236 return;
18237
18238 -- If not function or procedure call, instantiation, or 'Access, then
18239 -- ignore call (this happens in some error cases and rewriting cases).
18240
18241 elsif not Nkind_In (N, N_Attribute_Reference,
18242 N_Function_Call,
18243 N_Procedure_Call_Statement)
18244 and then not Inst_Case
18245 then
18246 return;
18247
18248 -- Nothing to do if this is a call or instantiation that has already
18249 -- been found to be a sure ABE.
18250
18251 elsif Nkind (N) /= N_Attribute_Reference
18252 and then Is_Known_Guaranteed_ABE (N)
18253 then
18254 return;
18255
18256 -- Nothing to do if errors already detected (avoid cascaded errors)
18257
18258 elsif Serious_Errors_Detected /= 0 then
18259 return;
18260
18261 -- Nothing to do if not in full analysis mode
18262
18263 elsif not Full_Analysis then
18264 return;
18265
18266 -- Nothing to do if analyzing in special spec-expression mode, since the
18267 -- call is not actually being made at this time.
18268
18269 elsif In_Spec_Expression then
18270 return;
18271
18272 -- Nothing to do for call to intrinsic subprogram
18273
18274 elsif Is_Intrinsic_Subprogram (E) then
18275 return;
18276
18277 -- Nothing to do if call is within a generic unit
18278
18279 elsif Inside_A_Generic then
18280 return;
18281
18282 -- Nothing to do when the call appears within pragma Initial_Condition.
18283 -- The pragma is part of the elaboration statements of a package body
18284 -- and may only call external subprograms or subprograms whose body is
18285 -- already available.
18286
18287 elsif Within_Initial_Condition (N) then
18288 return;
18289 end if;
18290
18291 -- Delay this call if we are still delaying calls
18292
18293 if Delaying_Elab_Checks then
18294 Delay_Check.Append
18295 ((N => N,
18296 E => E,
18297 Orig_Ent => Orig_Ent,
18298 Curscop => Current_Scope,
18299 Outer_Scope => Outer_Scope,
18300 From_Elab_Code => From_Elab_Code,
18301 In_Task_Activation => In_Task_Activation,
18302 From_SPARK_Code => SPARK_Mode = On));
18303 return;
18304
18305 -- Otherwise, call phase 2 continuation right now
18306
18307 else
18308 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
18309 end if;
18310 end Check_Internal_Call;
18311
18312 ----------------------------------
18313 -- Check_Internal_Call_Continue --
18314 ----------------------------------
18315
18316 procedure Check_Internal_Call_Continue
18317 (N : Node_Id;
18318 E : Entity_Id;
18319 Outer_Scope : Entity_Id;
18320 Orig_Ent : Entity_Id)
18321 is
18322 function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
18323 -- Function applied to each node as we traverse the body. Checks for
18324 -- call or entity reference that needs checking, and if so checks it.
18325 -- Always returns OK, so entire tree is traversed, except that as
18326 -- described below subprogram bodies are skipped for now.
18327
18328 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
18329 -- Traverse procedure using above Find_Elab_Reference function
18330
18331 -------------------------
18332 -- Find_Elab_Reference --
18333 -------------------------
18334
18335 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
18336 Actual : Node_Id;
18337
18338 begin
18339 -- If user has specified that there are no entry calls in elaboration
18340 -- code, do not trace past an accept statement, because the rendez-
18341 -- vous will happen after elaboration.
18342
18343 if Nkind_In (Original_Node (N), N_Accept_Statement,
18344 N_Selective_Accept)
18345 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
18346 then
18347 return Abandon;
18348
18349 -- If we have a function call, check it
18350
18351 elsif Nkind (N) = N_Function_Call then
18352 Check_Elab_Call (N, Outer_Scope);
18353 return OK;
18354
18355 -- If we have a procedure call, check the call, and also check
18356 -- arguments that are assignments (OUT or IN OUT mode formals).
18357
18358 elsif Nkind (N) = N_Procedure_Call_Statement then
18359 Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E));
18360
18361 Actual := First_Actual (N);
18362 while Present (Actual) loop
18363 if Known_To_Be_Assigned (Actual) then
18364 Check_Elab_Assign (Actual);
18365 end if;
18366
18367 Next_Actual (Actual);
18368 end loop;
18369
18370 return OK;
18371
18372 -- If we have an access attribute for a subprogram, check it.
18373 -- Suppress this behavior under debug flag.
18374
18375 elsif not Debug_Flag_Dot_UU
18376 and then Nkind (N) = N_Attribute_Reference
18377 and then Nam_In (Attribute_Name (N), Name_Access,
18378 Name_Unrestricted_Access)
18379 and then Is_Entity_Name (Prefix (N))
18380 and then Is_Subprogram (Entity (Prefix (N)))
18381 then
18382 Check_Elab_Call (N, Outer_Scope);
18383 return OK;
18384
18385 -- In SPARK mode, if we have an entity reference to a variable, then
18386 -- check it. For now we consider any reference.
18387
18388 elsif SPARK_Mode = On
18389 and then Nkind (N) in N_Has_Entity
18390 and then Present (Entity (N))
18391 and then Ekind (Entity (N)) = E_Variable
18392 then
18393 Check_Elab_Call (N, Outer_Scope);
18394 return OK;
18395
18396 -- If we have a generic instantiation, check it
18397
18398 elsif Nkind (N) in N_Generic_Instantiation then
18399 Check_Elab_Instantiation (N, Outer_Scope);
18400 return OK;
18401
18402 -- Skip subprogram bodies that come from source (wait for call to
18403 -- analyze these). The reason for the come from source test is to
18404 -- avoid catching task bodies.
18405
18406 -- For task bodies, we should really avoid these too, waiting for the
18407 -- task activation, but that's too much trouble to catch for now, so
18408 -- we go in unconditionally. This is not so terrible, it means the
18409 -- error backtrace is not quite complete, and we are too eager to
18410 -- scan bodies of tasks that are unused, but this is hardly very
18411 -- significant.
18412
18413 elsif Nkind (N) = N_Subprogram_Body
18414 and then Comes_From_Source (N)
18415 then
18416 return Skip;
18417
18418 elsif Nkind (N) = N_Assignment_Statement
18419 and then Comes_From_Source (N)
18420 then
18421 Check_Elab_Assign (Name (N));
18422 return OK;
18423
18424 else
18425 return OK;
18426 end if;
18427 end Find_Elab_Reference;
18428
18429 Inst_Case : constant Boolean := Is_Generic_Unit (E);
18430 Loc : constant Source_Ptr := Sloc (N);
18431
18432 Ebody : Entity_Id;
18433 Sbody : Node_Id;
18434
18435 -- Start of processing for Check_Internal_Call_Continue
18436
18437 begin
18438 -- Save outer level call if at outer level
18439
18440 if Elab_Call.Last = 0 then
18441 Outer_Level_Sloc := Loc;
18442 end if;
18443
18444 -- If the call is to a function that renames a literal, no check needed
18445
18446 if Ekind (E) = E_Enumeration_Literal then
18447 return;
18448 end if;
18449
18450 -- Register the subprogram as examined within this particular context.
18451 -- This ensures that calls to the same subprogram but in different
18452 -- contexts receive warnings and checks of their own since the calls
18453 -- may be reached through different flow paths.
18454
18455 Elab_Visited.Append ((Subp_Id => E, Context => Parent (N)));
18456
18457 Sbody := Unit_Declaration_Node (E);
18458
18459 if not Nkind_In (Sbody, N_Subprogram_Body, N_Package_Body) then
18460 Ebody := Corresponding_Body (Sbody);
18461
18462 if No (Ebody) then
18463 return;
18464 else
18465 Sbody := Unit_Declaration_Node (Ebody);
18466 end if;
18467 end if;
18468
18469 -- If the body appears after the outer level call or instantiation then
18470 -- we have an error case handled below.
18471
18472 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
18473 and then not In_Task_Activation
18474 then
18475 null;
18476
18477 -- If we have the instantiation case we are done, since we now know that
18478 -- the body of the generic appeared earlier.
18479
18480 elsif Inst_Case then
18481 return;
18482
18483 -- Otherwise we have a call, so we trace through the called body to see
18484 -- if it has any problems.
18485
18486 else
18487 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
18488
18489 Elab_Call.Append ((Cloc => Loc, Ent => E));
18490
18491 if Debug_Flag_Underscore_LL then
18492 Write_Str ("Elab_Call.Last = ");
18493 Write_Int (Int (Elab_Call.Last));
18494 Write_Str (" Ent = ");
18495 Write_Name (Chars (E));
18496 Write_Str (" at ");
18497 Write_Location (Sloc (N));
18498 Write_Eol;
18499 end if;
18500
18501 -- Now traverse declarations and statements of subprogram body. Note
18502 -- that we cannot simply Traverse (Sbody), since traverse does not
18503 -- normally visit subprogram bodies.
18504
18505 declare
18506 Decl : Node_Id;
18507 begin
18508 Decl := First (Declarations (Sbody));
18509 while Present (Decl) loop
18510 Traverse (Decl);
18511 Next (Decl);
18512 end loop;
18513 end;
18514
18515 Traverse (Handled_Statement_Sequence (Sbody));
18516
18517 Elab_Call.Decrement_Last;
18518 return;
18519 end if;
18520
18521 -- Here is the case of calling a subprogram where the body has not yet
18522 -- been encountered. A warning message is needed, except if this is the
18523 -- case of appearing within an aspect specification that results in
18524 -- a check call, we do not really have such a situation, so no warning
18525 -- is needed (e.g. the case of a precondition, where the call appears
18526 -- textually before the body, but in actual fact is moved to the
18527 -- appropriate subprogram body and so does not need a check).
18528
18529 declare
18530 P : Node_Id;
18531 O : Node_Id;
18532
18533 begin
18534 P := Parent (N);
18535 loop
18536 -- Keep looking at parents if we are still in the subexpression
18537
18538 if Nkind (P) in N_Subexpr then
18539 P := Parent (P);
18540
18541 -- Here P is the parent of the expression, check for special case
18542
18543 else
18544 O := Original_Node (P);
18545
18546 -- Definitely not the special case if orig node is not a pragma
18547
18548 exit when Nkind (O) /= N_Pragma;
18549
18550 -- Check we have an If statement or a null statement (happens
18551 -- when the If has been expanded to be True).
18552
18553 exit when not Nkind_In (P, N_If_Statement, N_Null_Statement);
18554
18555 -- Our special case will be indicated either by the pragma
18556 -- coming from an aspect ...
18557
18558 if Present (Corresponding_Aspect (O)) then
18559 return;
18560
18561 -- Or, in the case of an initial condition, specifically by a
18562 -- Check pragma specifying an Initial_Condition check.
18563
18564 elsif Pragma_Name (O) = Name_Check
18565 and then
18566 Chars
18567 (Expression (First (Pragma_Argument_Associations (O)))) =
18568 Name_Initial_Condition
18569 then
18570 return;
18571
18572 -- For anything else, we have an error
18573
18574 else
18575 exit;
18576 end if;
18577 end if;
18578 end loop;
18579 end;
18580
18581 -- Not that special case, warning and dynamic check is required
18582
18583 -- If we have nothing in the call stack, then this is at the outer
18584 -- level, and the ABE is bound to occur, unless it's a 'Access, or
18585 -- it's a renaming.
18586
18587 if Elab_Call.Last = 0 then
18588 Error_Msg_Warn := SPARK_Mode /= On;
18589
18590 declare
18591 Insert_Check : Boolean := True;
18592 -- This flag is set to True if an elaboration check should be
18593 -- inserted.
18594
18595 begin
18596 if In_Task_Activation then
18597 Insert_Check := False;
18598
18599 elsif Inst_Case then
18600 Error_Msg_NE
18601 ("cannot instantiate& before body seen<<", N, Orig_Ent);
18602
18603 elsif Nkind (N) = N_Attribute_Reference then
18604 Error_Msg_NE
18605 ("Access attribute of & before body seen<<", N, Orig_Ent);
18606 Error_Msg_N ("\possible Program_Error on later references<", N);
18607 Insert_Check := False;
18608
18609 elsif Nkind (Unit_Declaration_Node (Orig_Ent)) /=
18610 N_Subprogram_Renaming_Declaration
18611 then
18612 Error_Msg_NE
18613 ("cannot call& before body seen<<", N, Orig_Ent);
18614
18615 elsif not Is_Generic_Actual_Subprogram (Orig_Ent) then
18616 Insert_Check := False;
18617 end if;
18618
18619 if Insert_Check then
18620 Error_Msg_N ("\Program_Error [<<", N);
18621 Insert_Elab_Check (N);
18622 end if;
18623 end;
18624
18625 -- Call is not at outer level
18626
18627 else
18628 -- Do not generate elaboration checks in GNATprove mode because the
18629 -- elaboration counter and the check are both forms of expansion.
18630
18631 if GNATprove_Mode then
18632 null;
18633
18634 -- Generate an elaboration check
18635
18636 elsif not Elaboration_Checks_Suppressed (E) then
18637 Set_Elaboration_Entity_Required (E);
18638
18639 -- Create a declaration of the elaboration entity, and insert it
18640 -- prior to the subprogram or the generic unit, within the same
18641 -- scope. Since the subprogram may be overloaded, create a unique
18642 -- entity.
18643
18644 if No (Elaboration_Entity (E)) then
18645 declare
18646 Loce : constant Source_Ptr := Sloc (E);
18647 Ent : constant Entity_Id :=
18648 Make_Defining_Identifier (Loc,
18649 New_External_Name (Chars (E), 'E', -1));
18650
18651 begin
18652 Set_Elaboration_Entity (E, Ent);
18653 Push_Scope (Scope (E));
18654
18655 Insert_Action (Declaration_Node (E),
18656 Make_Object_Declaration (Loce,
18657 Defining_Identifier => Ent,
18658 Object_Definition =>
18659 New_Occurrence_Of (Standard_Short_Integer, Loce),
18660 Expression =>
18661 Make_Integer_Literal (Loc, Uint_0)));
18662
18663 -- Set elaboration flag at the point of the body
18664
18665 Set_Elaboration_Flag (Sbody, E);
18666
18667 -- Kill current value indication. This is necessary because
18668 -- the tests of this flag are inserted out of sequence and
18669 -- must not pick up bogus indications of the wrong constant
18670 -- value. Also, this is never a true constant, since one way
18671 -- or another, it gets reset.
18672
18673 Set_Current_Value (Ent, Empty);
18674 Set_Last_Assignment (Ent, Empty);
18675 Set_Is_True_Constant (Ent, False);
18676 Pop_Scope;
18677 end;
18678 end if;
18679
18680 -- Generate:
18681 -- if Enn = 0 then
18682 -- raise Program_Error with "access before elaboration";
18683 -- end if;
18684
18685 Insert_Elab_Check (N,
18686 Make_Attribute_Reference (Loc,
18687 Attribute_Name => Name_Elaborated,
18688 Prefix => New_Occurrence_Of (E, Loc)));
18689 end if;
18690
18691 -- Generate the warning
18692
18693 if not Suppress_Elaboration_Warnings (E)
18694 and then not Elaboration_Checks_Suppressed (E)
18695
18696 -- Suppress this warning if we have a function call that occurred
18697 -- within an assertion expression, since we can get false warnings
18698 -- in this case, due to the out of order handling in this case.
18699
18700 and then
18701 (Nkind (Original_Node (N)) /= N_Function_Call
18702 or else not In_Assertion_Expression_Pragma (Original_Node (N)))
18703 then
18704 Error_Msg_Warn := SPARK_Mode /= On;
18705
18706 if Inst_Case then
18707 Error_Msg_NE
18708 ("instantiation of& may occur before body is seen<l<",
18709 N, Orig_Ent);
18710 else
18711 -- A rather specific check. For Finalize/Adjust/Initialize, if
18712 -- the type has Warnings_Off set, suppress the warning.
18713
18714 if Nam_In (Chars (E), Name_Adjust,
18715 Name_Finalize,
18716 Name_Initialize)
18717 and then Present (First_Formal (E))
18718 then
18719 declare
18720 T : constant Entity_Id := Etype (First_Formal (E));
18721 begin
18722 if Is_Controlled (T) then
18723 if Warnings_Off (T)
18724 or else (Ekind (T) = E_Private_Type
18725 and then Warnings_Off (Full_View (T)))
18726 then
18727 goto Output;
18728 end if;
18729 end if;
18730 end;
18731 end if;
18732
18733 -- Go ahead and give warning if not this special case
18734
18735 Error_Msg_NE
18736 ("call to& may occur before body is seen<l<", N, Orig_Ent);
18737 end if;
18738
18739 Error_Msg_N ("\Program_Error ]<l<", N);
18740
18741 -- There is no need to query the elaboration warning message flags
18742 -- because the main message is an error, not a warning, therefore
18743 -- all the clarification messages produces by Output_Calls must be
18744 -- emitted unconditionally.
18745
18746 <<Output>>
18747
18748 Output_Calls (N, Check_Elab_Flag => False);
18749 end if;
18750 end if;
18751 end Check_Internal_Call_Continue;
18752
18753 ---------------------------
18754 -- Check_Task_Activation --
18755 ---------------------------
18756
18757 procedure Check_Task_Activation (N : Node_Id) is
18758 Loc : constant Source_Ptr := Sloc (N);
18759 Inter_Procs : constant Elist_Id := New_Elmt_List;
18760 Intra_Procs : constant Elist_Id := New_Elmt_List;
18761 Ent : Entity_Id;
18762 P : Entity_Id;
18763 Task_Scope : Entity_Id;
18764 Cunit_SC : Boolean := False;
18765 Decl : Node_Id;
18766 Elmt : Elmt_Id;
18767 Enclosing : Entity_Id;
18768
18769 procedure Add_Task_Proc (Typ : Entity_Id);
18770 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
18771 -- For record types, this procedure recurses over component types.
18772
18773 procedure Collect_Tasks (Decls : List_Id);
18774 -- Collect the types of the tasks that are to be activated in the given
18775 -- list of declarations, in order to perform elaboration checks on the
18776 -- corresponding task procedures that are called implicitly here.
18777
18778 function Outer_Unit (E : Entity_Id) return Entity_Id;
18779 -- find enclosing compilation unit of Entity, ignoring subunits, or
18780 -- else enclosing subprogram. If E is not a package, there is no need
18781 -- for inter-unit elaboration checks.
18782
18783 -------------------
18784 -- Add_Task_Proc --
18785 -------------------
18786
18787 procedure Add_Task_Proc (Typ : Entity_Id) is
18788 Comp : Entity_Id;
18789 Proc : Entity_Id := Empty;
18790
18791 begin
18792 if Is_Task_Type (Typ) then
18793 Proc := Get_Task_Body_Procedure (Typ);
18794
18795 elsif Is_Array_Type (Typ)
18796 and then Has_Task (Base_Type (Typ))
18797 then
18798 Add_Task_Proc (Component_Type (Typ));
18799
18800 elsif Is_Record_Type (Typ)
18801 and then Has_Task (Base_Type (Typ))
18802 then
18803 Comp := First_Component (Typ);
18804 while Present (Comp) loop
18805 Add_Task_Proc (Etype (Comp));
18806 Comp := Next_Component (Comp);
18807 end loop;
18808 end if;
18809
18810 -- If the task type is another unit, we will perform the usual
18811 -- elaboration check on its enclosing unit. If the type is in the
18812 -- same unit, we can trace the task body as for an internal call,
18813 -- but we only need to examine other external calls, because at
18814 -- the point the task is activated, internal subprogram bodies
18815 -- will have been elaborated already. We keep separate lists for
18816 -- each kind of task.
18817
18818 -- Skip this test if errors have occurred, since in this case
18819 -- we can get false indications.
18820
18821 if Serious_Errors_Detected /= 0 then
18822 return;
18823 end if;
18824
18825 if Present (Proc) then
18826 if Outer_Unit (Scope (Proc)) = Enclosing then
18827
18828 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
18829 and then
18830 (not Is_Generic_Instance (Scope (Proc))
18831 or else Scope (Proc) = Scope (Defining_Identifier (Decl)))
18832 then
18833 Error_Msg_Warn := SPARK_Mode /= On;
18834 Error_Msg_N
18835 ("task will be activated before elaboration of its body<<",
18836 Decl);
18837 Error_Msg_N ("\Program_Error [<<", Decl);
18838
18839 elsif Present
18840 (Corresponding_Body (Unit_Declaration_Node (Proc)))
18841 then
18842 Append_Elmt (Proc, Intra_Procs);
18843 end if;
18844
18845 else
18846 -- No need for multiple entries of the same type
18847
18848 Elmt := First_Elmt (Inter_Procs);
18849 while Present (Elmt) loop
18850 if Node (Elmt) = Proc then
18851 return;
18852 end if;
18853
18854 Next_Elmt (Elmt);
18855 end loop;
18856
18857 Append_Elmt (Proc, Inter_Procs);
18858 end if;
18859 end if;
18860 end Add_Task_Proc;
18861
18862 -------------------
18863 -- Collect_Tasks --
18864 -------------------
18865
18866 procedure Collect_Tasks (Decls : List_Id) is
18867 begin
18868 if Present (Decls) then
18869 Decl := First (Decls);
18870 while Present (Decl) loop
18871 if Nkind (Decl) = N_Object_Declaration
18872 and then Has_Task (Etype (Defining_Identifier (Decl)))
18873 then
18874 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
18875 end if;
18876
18877 Next (Decl);
18878 end loop;
18879 end if;
18880 end Collect_Tasks;
18881
18882 ----------------
18883 -- Outer_Unit --
18884 ----------------
18885
18886 function Outer_Unit (E : Entity_Id) return Entity_Id is
18887 Outer : Entity_Id;
18888
18889 begin
18890 Outer := E;
18891 while Present (Outer) loop
18892 if Elaboration_Checks_Suppressed (Outer) then
18893 Cunit_SC := True;
18894 end if;
18895
18896 exit when Is_Child_Unit (Outer)
18897 or else Scope (Outer) = Standard_Standard
18898 or else Ekind (Outer) /= E_Package;
18899 Outer := Scope (Outer);
18900 end loop;
18901
18902 return Outer;
18903 end Outer_Unit;
18904
18905 -- Start of processing for Check_Task_Activation
18906
18907 begin
18908 pragma Assert (Legacy_Elaboration_Checks);
18909
18910 Enclosing := Outer_Unit (Current_Scope);
18911
18912 -- Find all tasks declared in the current unit
18913
18914 if Nkind (N) = N_Package_Body then
18915 P := Unit_Declaration_Node (Corresponding_Spec (N));
18916
18917 Collect_Tasks (Declarations (N));
18918 Collect_Tasks (Visible_Declarations (Specification (P)));
18919 Collect_Tasks (Private_Declarations (Specification (P)));
18920
18921 elsif Nkind (N) = N_Package_Declaration then
18922 Collect_Tasks (Visible_Declarations (Specification (N)));
18923 Collect_Tasks (Private_Declarations (Specification (N)));
18924
18925 else
18926 Collect_Tasks (Declarations (N));
18927 end if;
18928
18929 -- We only perform detailed checks in all tasks that are library level
18930 -- entities. If the master is a subprogram or task, activation will
18931 -- depend on the activation of the master itself.
18932
18933 -- Should dynamic checks be added in the more general case???
18934
18935 if Ekind (Enclosing) /= E_Package then
18936 return;
18937 end if;
18938
18939 -- For task types defined in other units, we want the unit containing
18940 -- the task body to be elaborated before the current one.
18941
18942 Elmt := First_Elmt (Inter_Procs);
18943 while Present (Elmt) loop
18944 Ent := Node (Elmt);
18945 Task_Scope := Outer_Unit (Scope (Ent));
18946
18947 if not Is_Compilation_Unit (Task_Scope) then
18948 null;
18949
18950 elsif Suppress_Elaboration_Warnings (Task_Scope)
18951 or else Elaboration_Checks_Suppressed (Task_Scope)
18952 then
18953 null;
18954
18955 elsif Dynamic_Elaboration_Checks then
18956 if not Elaboration_Checks_Suppressed (Ent)
18957 and then not Cunit_SC
18958 and then not Restriction_Active
18959 (No_Entry_Calls_In_Elaboration_Code)
18960 then
18961 -- Runtime elaboration check required. Generate check of the
18962 -- elaboration counter for the unit containing the entity.
18963
18964 Insert_Elab_Check (N,
18965 Make_Attribute_Reference (Loc,
18966 Prefix =>
18967 New_Occurrence_Of (Spec_Entity (Task_Scope), Loc),
18968 Attribute_Name => Name_Elaborated));
18969 end if;
18970
18971 else
18972 -- Force the binder to elaborate other unit first
18973
18974 if Elab_Info_Messages
18975 and then not Suppress_Elaboration_Warnings (Ent)
18976 and then not Elaboration_Checks_Suppressed (Ent)
18977 and then not Suppress_Elaboration_Warnings (Task_Scope)
18978 and then not Elaboration_Checks_Suppressed (Task_Scope)
18979 then
18980 Error_Msg_Node_2 := Task_Scope;
18981 Error_Msg_NE
18982 ("info: activation of an instance of task type & requires "
18983 & "pragma Elaborate_All on &?$?", N, Ent);
18984 end if;
18985
18986 Activate_Elaborate_All_Desirable (N, Task_Scope);
18987 Set_Suppress_Elaboration_Warnings (Task_Scope);
18988 end if;
18989
18990 Next_Elmt (Elmt);
18991 end loop;
18992
18993 -- For tasks declared in the current unit, trace other calls within the
18994 -- task procedure bodies, which are available.
18995
18996 if not Debug_Flag_Dot_Y then
18997 In_Task_Activation := True;
18998
18999 Elmt := First_Elmt (Intra_Procs);
19000 while Present (Elmt) loop
19001 Ent := Node (Elmt);
19002 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
19003 Next_Elmt (Elmt);
19004 end loop;
19005
19006 In_Task_Activation := False;
19007 end if;
19008 end Check_Task_Activation;
19009
19010 ------------------------
19011 -- Get_Referenced_Ent --
19012 ------------------------
19013
19014 function Get_Referenced_Ent (N : Node_Id) return Entity_Id is
19015 Nam : Node_Id;
19016
19017 begin
19018 if Nkind (N) in N_Has_Entity
19019 and then Present (Entity (N))
19020 and then Ekind (Entity (N)) = E_Variable
19021 then
19022 return Entity (N);
19023 end if;
19024
19025 if Nkind (N) = N_Attribute_Reference then
19026 Nam := Prefix (N);
19027 else
19028 Nam := Name (N);
19029 end if;
19030
19031 if No (Nam) then
19032 return Empty;
19033 elsif Nkind (Nam) = N_Selected_Component then
19034 return Entity (Selector_Name (Nam));
19035 elsif not Is_Entity_Name (Nam) then
19036 return Empty;
19037 else
19038 return Entity (Nam);
19039 end if;
19040 end Get_Referenced_Ent;
19041
19042 ----------------------
19043 -- Has_Generic_Body --
19044 ----------------------
19045
19046 function Has_Generic_Body (N : Node_Id) return Boolean is
19047 Ent : constant Entity_Id := Get_Generic_Entity (N);
19048 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
19049 Scop : Entity_Id;
19050
19051 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
19052 -- Determine if the list of nodes headed by N and linked by Next
19053 -- contains a package body for the package spec entity E, and if so
19054 -- return the package body. If not, then returns Empty.
19055
19056 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
19057 -- This procedure is called load the unit whose name is given by Nam.
19058 -- This unit is being loaded to see whether it contains an optional
19059 -- generic body. The returned value is the loaded unit, which is always
19060 -- a package body (only package bodies can contain other entities in the
19061 -- sense in which Has_Generic_Body is interested). We only attempt to
19062 -- load bodies if we are generating code. If we are in semantics check
19063 -- only mode, then it would be wrong to load bodies that are not
19064 -- required from a semantic point of view, so in this case we return
19065 -- Empty. The result is that the caller may incorrectly decide that a
19066 -- generic spec does not have a body when in fact it does, but the only
19067 -- harm in this is that some warnings on elaboration problems may be
19068 -- lost in semantic checks only mode, which is not big loss. We also
19069 -- return Empty if we go for a body and it is not there.
19070
19071 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
19072 -- PE is the entity for a package spec. This function locates the
19073 -- corresponding package body, returning Empty if none is found. The
19074 -- package body returned is fully parsed but may not yet be analyzed,
19075 -- so only syntactic fields should be referenced.
19076
19077 ------------------
19078 -- Find_Body_In --
19079 ------------------
19080
19081 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
19082 Nod : Node_Id;
19083
19084 begin
19085 Nod := N;
19086 while Present (Nod) loop
19087
19088 -- If we found the package body we are looking for, return it
19089
19090 if Nkind (Nod) = N_Package_Body
19091 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
19092 then
19093 return Nod;
19094
19095 -- If we found the stub for the body, go after the subunit,
19096 -- loading it if necessary.
19097
19098 elsif Nkind (Nod) = N_Package_Body_Stub
19099 and then Chars (Defining_Identifier (Nod)) = Chars (E)
19100 then
19101 if Present (Library_Unit (Nod)) then
19102 return Unit (Library_Unit (Nod));
19103
19104 else
19105 return Load_Package_Body (Get_Unit_Name (Nod));
19106 end if;
19107
19108 -- If neither package body nor stub, keep looking on chain
19109
19110 else
19111 Next (Nod);
19112 end if;
19113 end loop;
19114
19115 return Empty;
19116 end Find_Body_In;
19117
19118 -----------------------
19119 -- Load_Package_Body --
19120 -----------------------
19121
19122 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
19123 U : Unit_Number_Type;
19124
19125 begin
19126 if Operating_Mode /= Generate_Code then
19127 return Empty;
19128 else
19129 U :=
19130 Load_Unit
19131 (Load_Name => Nam,
19132 Required => False,
19133 Subunit => False,
19134 Error_Node => N);
19135
19136 if U = No_Unit then
19137 return Empty;
19138 else
19139 return Unit (Cunit (U));
19140 end if;
19141 end if;
19142 end Load_Package_Body;
19143
19144 -------------------------------
19145 -- Locate_Corresponding_Body --
19146 -------------------------------
19147
19148 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
19149 Spec : constant Node_Id := Declaration_Node (PE);
19150 Decl : constant Node_Id := Parent (Spec);
19151 Scop : constant Entity_Id := Scope (PE);
19152 PBody : Node_Id;
19153
19154 begin
19155 if Is_Library_Level_Entity (PE) then
19156
19157 -- If package is a library unit that requires a body, we have no
19158 -- choice but to go after that body because it might contain an
19159 -- optional body for the original generic package.
19160
19161 if Unit_Requires_Body (PE) then
19162
19163 -- Load the body. Note that we are a little careful here to use
19164 -- Spec to get the unit number, rather than PE or Decl, since
19165 -- in the case where the package is itself a library level
19166 -- instantiation, Spec will properly reference the generic
19167 -- template, which is what we really want.
19168
19169 return
19170 Load_Package_Body
19171 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
19172
19173 -- But if the package is a library unit that does NOT require
19174 -- a body, then no body is permitted, so we are sure that there
19175 -- is no body for the original generic package.
19176
19177 else
19178 return Empty;
19179 end if;
19180
19181 -- Otherwise look and see if we are embedded in a further package
19182
19183 elsif Is_Package_Or_Generic_Package (Scop) then
19184
19185 -- If so, get the body of the enclosing package, and look in
19186 -- its package body for the package body we are looking for.
19187
19188 PBody := Locate_Corresponding_Body (Scop);
19189
19190 if No (PBody) then
19191 return Empty;
19192 else
19193 return Find_Body_In (PE, First (Declarations (PBody)));
19194 end if;
19195
19196 -- If we are not embedded in a further package, then the body
19197 -- must be in the same declarative part as we are.
19198
19199 else
19200 return Find_Body_In (PE, Next (Decl));
19201 end if;
19202 end Locate_Corresponding_Body;
19203
19204 -- Start of processing for Has_Generic_Body
19205
19206 begin
19207 if Present (Corresponding_Body (Decl)) then
19208 return True;
19209
19210 elsif Unit_Requires_Body (Ent) then
19211 return True;
19212
19213 -- Compilation units cannot have optional bodies
19214
19215 elsif Is_Compilation_Unit (Ent) then
19216 return False;
19217
19218 -- Otherwise look at what scope we are in
19219
19220 else
19221 Scop := Scope (Ent);
19222
19223 -- Case of entity is in other than a package spec, in this case
19224 -- the body, if present, must be in the same declarative part.
19225
19226 if not Is_Package_Or_Generic_Package (Scop) then
19227 declare
19228 P : Node_Id;
19229
19230 begin
19231 -- Declaration node may get us a spec, so if so, go to
19232 -- the parent declaration.
19233
19234 P := Declaration_Node (Ent);
19235 while not Is_List_Member (P) loop
19236 P := Parent (P);
19237 end loop;
19238
19239 return Present (Find_Body_In (Ent, Next (P)));
19240 end;
19241
19242 -- If the entity is in a package spec, then we have to locate
19243 -- the corresponding package body, and look there.
19244
19245 else
19246 declare
19247 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
19248
19249 begin
19250 if No (PBody) then
19251 return False;
19252 else
19253 return
19254 Present
19255 (Find_Body_In (Ent, (First (Declarations (PBody)))));
19256 end if;
19257 end;
19258 end if;
19259 end if;
19260 end Has_Generic_Body;
19261
19262 -----------------------
19263 -- Insert_Elab_Check --
19264 -----------------------
19265
19266 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
19267 Nod : Node_Id;
19268 Loc : constant Source_Ptr := Sloc (N);
19269
19270 Chk : Node_Id;
19271 -- The check (N_Raise_Program_Error) node to be inserted
19272
19273 begin
19274 -- If expansion is disabled, do not generate any checks. Also
19275 -- skip checks if any subunits are missing because in either
19276 -- case we lack the full information that we need, and no object
19277 -- file will be created in any case.
19278
19279 if not Expander_Active or else Subunits_Missing then
19280 return;
19281 end if;
19282
19283 -- If we have a generic instantiation, where Instance_Spec is set,
19284 -- then this field points to a generic instance spec that has
19285 -- been inserted before the instantiation node itself, so that
19286 -- is where we want to insert a check.
19287
19288 if Nkind (N) in N_Generic_Instantiation
19289 and then Present (Instance_Spec (N))
19290 then
19291 Nod := Instance_Spec (N);
19292 else
19293 Nod := N;
19294 end if;
19295
19296 -- Build check node, possibly with condition
19297
19298 Chk :=
19299 Make_Raise_Program_Error (Loc, Reason => PE_Access_Before_Elaboration);
19300
19301 if Present (C) then
19302 Set_Condition (Chk, Make_Op_Not (Loc, Right_Opnd => C));
19303 end if;
19304
19305 -- If we are inserting at the top level, insert in Aux_Decls
19306
19307 if Nkind (Parent (Nod)) = N_Compilation_Unit then
19308 declare
19309 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
19310
19311 begin
19312 if No (Declarations (ADN)) then
19313 Set_Declarations (ADN, New_List (Chk));
19314 else
19315 Append_To (Declarations (ADN), Chk);
19316 end if;
19317
19318 Analyze (Chk);
19319 end;
19320
19321 -- Otherwise just insert as an action on the node in question
19322
19323 else
19324 Insert_Action (Nod, Chk);
19325 end if;
19326 end Insert_Elab_Check;
19327
19328 -------------------------------
19329 -- Is_Call_Of_Generic_Formal --
19330 -------------------------------
19331
19332 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean is
19333 begin
19334 return Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
19335
19336 -- Always return False if debug flag -gnatd.G is set
19337
19338 and then not Debug_Flag_Dot_GG
19339
19340 -- For now, we detect this by looking for the strange identifier
19341 -- node, whose Chars reflect the name of the generic formal, but
19342 -- the Chars of the Entity references the generic actual.
19343
19344 and then Nkind (Name (N)) = N_Identifier
19345 and then Chars (Name (N)) /= Chars (Entity (Name (N)));
19346 end Is_Call_Of_Generic_Formal;
19347
19348 -------------------------------
19349 -- Is_Finalization_Procedure --
19350 -------------------------------
19351
19352 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is
19353 begin
19354 -- Check whether Id is a procedure with at least one parameter
19355
19356 if Ekind (Id) = E_Procedure and then Present (First_Formal (Id)) then
19357 declare
19358 Typ : constant Entity_Id := Etype (First_Formal (Id));
19359 Deep_Fin : Entity_Id := Empty;
19360 Fin : Entity_Id := Empty;
19361
19362 begin
19363 -- If the type of the first formal does not require finalization
19364 -- actions, then this is definitely not [Deep_]Finalize.
19365
19366 if not Needs_Finalization (Typ) then
19367 return False;
19368 end if;
19369
19370 -- At this point we have the following scenario:
19371
19372 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
19373
19374 -- Recover the two possible versions of [Deep_]Finalize using the
19375 -- type of the first parameter and compare with the input.
19376
19377 Deep_Fin := TSS (Typ, TSS_Deep_Finalize);
19378
19379 if Is_Controlled (Typ) then
19380 Fin := Find_Prim_Op (Typ, Name_Finalize);
19381 end if;
19382
19383 return (Present (Deep_Fin) and then Id = Deep_Fin)
19384 or else (Present (Fin) and then Id = Fin);
19385 end;
19386 end if;
19387
19388 return False;
19389 end Is_Finalization_Procedure;
19390
19391 ------------------
19392 -- Output_Calls --
19393 ------------------
19394
19395 procedure Output_Calls
19396 (N : Node_Id;
19397 Check_Elab_Flag : Boolean)
19398 is
19399 function Emit (Flag : Boolean) return Boolean;
19400 -- Determine whether to emit an error message based on the combination
19401 -- of flags Check_Elab_Flag and Flag.
19402
19403 function Is_Printable_Error_Name return Boolean;
19404 -- An internal function, used to determine if a name, stored in the
19405 -- Name_Buffer, is either a non-internal name, or is an internal name
19406 -- that is printable by the error message circuits (i.e. it has a single
19407 -- upper case letter at the end).
19408
19409 ----------
19410 -- Emit --
19411 ----------
19412
19413 function Emit (Flag : Boolean) return Boolean is
19414 begin
19415 if Check_Elab_Flag then
19416 return Flag;
19417 else
19418 return True;
19419 end if;
19420 end Emit;
19421
19422 -----------------------------
19423 -- Is_Printable_Error_Name --
19424 -----------------------------
19425
19426 function Is_Printable_Error_Name return Boolean is
19427 begin
19428 if not Is_Internal_Name then
19429 return True;
19430
19431 elsif Name_Len = 1 then
19432 return False;
19433
19434 else
19435 Name_Len := Name_Len - 1;
19436 return not Is_Internal_Name;
19437 end if;
19438 end Is_Printable_Error_Name;
19439
19440 -- Local variables
19441
19442 Ent : Entity_Id;
19443
19444 -- Start of processing for Output_Calls
19445
19446 begin
19447 for J in reverse 1 .. Elab_Call.Last loop
19448 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
19449
19450 Ent := Elab_Call.Table (J).Ent;
19451 Get_Name_String (Chars (Ent));
19452
19453 -- Dynamic elaboration model, warnings controlled by -gnatwl
19454
19455 if Dynamic_Elaboration_Checks then
19456 if Emit (Elab_Warnings) then
19457 if Is_Generic_Unit (Ent) then
19458 Error_Msg_NE ("\\?l?& instantiated #", N, Ent);
19459 elsif Is_Init_Proc (Ent) then
19460 Error_Msg_N ("\\?l?initialization procedure called #", N);
19461 elsif Is_Printable_Error_Name then
19462 Error_Msg_NE ("\\?l?& called #", N, Ent);
19463 else
19464 Error_Msg_N ("\\?l?called #", N);
19465 end if;
19466 end if;
19467
19468 -- Static elaboration model, info messages controlled by -gnatel
19469
19470 else
19471 if Emit (Elab_Info_Messages) then
19472 if Is_Generic_Unit (Ent) then
19473 Error_Msg_NE ("\\?$?& instantiated #", N, Ent);
19474 elsif Is_Init_Proc (Ent) then
19475 Error_Msg_N ("\\?$?initialization procedure called #", N);
19476 elsif Is_Printable_Error_Name then
19477 Error_Msg_NE ("\\?$?& called #", N, Ent);
19478 else
19479 Error_Msg_N ("\\?$?called #", N);
19480 end if;
19481 end if;
19482 end if;
19483 end loop;
19484 end Output_Calls;
19485
19486 ----------------------------
19487 -- Same_Elaboration_Scope --
19488 ----------------------------
19489
19490 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
19491 S1 : Entity_Id;
19492 S2 : Entity_Id;
19493
19494 begin
19495 -- Find elaboration scope for Scop1
19496 -- This is either a subprogram or a compilation unit.
19497
19498 S1 := Scop1;
19499 while S1 /= Standard_Standard
19500 and then not Is_Compilation_Unit (S1)
19501 and then Ekind_In (S1, E_Package, E_Protected_Type, E_Block)
19502 loop
19503 S1 := Scope (S1);
19504 end loop;
19505
19506 -- Find elaboration scope for Scop2
19507
19508 S2 := Scop2;
19509 while S2 /= Standard_Standard
19510 and then not Is_Compilation_Unit (S2)
19511 and then Ekind_In (S2, E_Package, E_Protected_Type, E_Block)
19512 loop
19513 S2 := Scope (S2);
19514 end loop;
19515
19516 return S1 = S2;
19517 end Same_Elaboration_Scope;
19518
19519 -----------------
19520 -- Set_C_Scope --
19521 -----------------
19522
19523 procedure Set_C_Scope is
19524 begin
19525 while not Is_Compilation_Unit (C_Scope) loop
19526 C_Scope := Scope (C_Scope);
19527 end loop;
19528 end Set_C_Scope;
19529
19530 --------------------------------
19531 -- Set_Elaboration_Constraint --
19532 --------------------------------
19533
19534 procedure Set_Elaboration_Constraint
19535 (Call : Node_Id;
19536 Subp : Entity_Id;
19537 Scop : Entity_Id)
19538 is
19539 Elab_Unit : Entity_Id;
19540
19541 -- Check whether this is a call to an Initialize subprogram for a
19542 -- controlled type. Note that Call can also be a 'Access attribute
19543 -- reference, which now generates an elaboration check.
19544
19545 Init_Call : constant Boolean :=
19546 Nkind (Call) = N_Procedure_Call_Statement
19547 and then Chars (Subp) = Name_Initialize
19548 and then Comes_From_Source (Subp)
19549 and then Present (Parameter_Associations (Call))
19550 and then Is_Controlled (Etype (First_Actual (Call)));
19551
19552 begin
19553 -- If the unit is mentioned in a with_clause of the current unit, it is
19554 -- visible, and we can set the elaboration flag.
19555
19556 if Is_Immediately_Visible (Scop)
19557 or else (Is_Child_Unit (Scop) and then Is_Visible_Lib_Unit (Scop))
19558 then
19559 Activate_Elaborate_All_Desirable (Call, Scop);
19560 Set_Suppress_Elaboration_Warnings (Scop);
19561 return;
19562 end if;
19563
19564 -- If this is not an initialization call or a call using object notation
19565 -- we know that the unit of the called entity is in the context, and we
19566 -- can set the flag as well. The unit need not be visible if the call
19567 -- occurs within an instantiation.
19568
19569 if Is_Init_Proc (Subp)
19570 or else Init_Call
19571 or else Nkind (Original_Node (Call)) = N_Selected_Component
19572 then
19573 null; -- detailed processing follows.
19574
19575 else
19576 Activate_Elaborate_All_Desirable (Call, Scop);
19577 Set_Suppress_Elaboration_Warnings (Scop);
19578 return;
19579 end if;
19580
19581 -- If the unit is not in the context, there must be an intermediate unit
19582 -- that is, on which we need to place to elaboration flag. This happens
19583 -- with init proc calls.
19584
19585 if Is_Init_Proc (Subp) or else Init_Call then
19586
19587 -- The initialization call is on an object whose type is not declared
19588 -- in the same scope as the subprogram. The type of the object must
19589 -- be a subtype of the type of operation. This object is the first
19590 -- actual in the call.
19591
19592 declare
19593 Typ : constant Entity_Id :=
19594 Etype (First (Parameter_Associations (Call)));
19595 begin
19596 Elab_Unit := Scope (Typ);
19597 while (Present (Elab_Unit))
19598 and then not Is_Compilation_Unit (Elab_Unit)
19599 loop
19600 Elab_Unit := Scope (Elab_Unit);
19601 end loop;
19602 end;
19603
19604 -- If original node uses selected component notation, the prefix is
19605 -- visible and determines the scope that must be elaborated. After
19606 -- rewriting, the prefix is the first actual in the call.
19607
19608 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
19609 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
19610
19611 -- Not one of special cases above
19612
19613 else
19614 -- Using previously computed scope. If the elaboration check is
19615 -- done after analysis, the scope is not visible any longer, but
19616 -- must still be in the context.
19617
19618 Elab_Unit := Scop;
19619 end if;
19620
19621 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
19622 Set_Suppress_Elaboration_Warnings (Elab_Unit);
19623 end Set_Elaboration_Constraint;
19624
19625 -----------------
19626 -- Spec_Entity --
19627 -----------------
19628
19629 function Spec_Entity (E : Entity_Id) return Entity_Id is
19630 Decl : Node_Id;
19631
19632 begin
19633 -- Check for case of body entity
19634 -- Why is the check for E_Void needed???
19635
19636 if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then
19637 Decl := E;
19638
19639 loop
19640 Decl := Parent (Decl);
19641 exit when Nkind (Decl) in N_Proper_Body;
19642 end loop;
19643
19644 return Corresponding_Spec (Decl);
19645
19646 else
19647 return E;
19648 end if;
19649 end Spec_Entity;
19650
19651 ------------
19652 -- Within --
19653 ------------
19654
19655 function Within (E1, E2 : Entity_Id) return Boolean is
19656 Scop : Entity_Id;
19657 begin
19658 Scop := E1;
19659 loop
19660 if Scop = E2 then
19661 return True;
19662 elsif Scop = Standard_Standard then
19663 return False;
19664 else
19665 Scop := Scope (Scop);
19666 end if;
19667 end loop;
19668 end Within;
19669
19670 --------------------------
19671 -- Within_Elaborate_All --
19672 --------------------------
19673
19674 function Within_Elaborate_All
19675 (Unit : Unit_Number_Type;
19676 E : Entity_Id) return Boolean
19677 is
19678 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
19679 pragma Pack (Unit_Number_Set);
19680
19681 Seen : Unit_Number_Set := (others => False);
19682 -- Seen (X) is True after we have seen unit X in the walk. This is used
19683 -- to prevent processing the same unit more than once.
19684
19685 Result : Boolean := False;
19686
19687 procedure Helper (Unit : Unit_Number_Type);
19688 -- This helper procedure does all the work for Within_Elaborate_All. It
19689 -- walks the dependency graph, and sets Result to True if it finds an
19690 -- appropriate Elaborate_All.
19691
19692 ------------
19693 -- Helper --
19694 ------------
19695
19696 procedure Helper (Unit : Unit_Number_Type) is
19697 CU : constant Node_Id := Cunit (Unit);
19698
19699 Item : Node_Id;
19700 Item2 : Node_Id;
19701 Elab_Id : Entity_Id;
19702 Par : Node_Id;
19703
19704 begin
19705 if Seen (Unit) then
19706 return;
19707 else
19708 Seen (Unit) := True;
19709 end if;
19710
19711 -- First, check for Elaborate_Alls on this unit
19712
19713 Item := First (Context_Items (CU));
19714 while Present (Item) loop
19715 if Nkind (Item) = N_Pragma
19716 and then Pragma_Name (Item) = Name_Elaborate_All
19717 then
19718 -- Return if some previous error on the pragma itself. The
19719 -- pragma may be unanalyzed, because of a previous error, or
19720 -- if it is the context of a subunit, inherited by its parent.
19721
19722 if Error_Posted (Item) or else not Analyzed (Item) then
19723 return;
19724 end if;
19725
19726 Elab_Id :=
19727 Entity
19728 (Expression (First (Pragma_Argument_Associations (Item))));
19729
19730 if E = Elab_Id then
19731 Result := True;
19732 return;
19733 end if;
19734
19735 Par := Parent (Unit_Declaration_Node (Elab_Id));
19736
19737 Item2 := First (Context_Items (Par));
19738 while Present (Item2) loop
19739 if Nkind (Item2) = N_With_Clause
19740 and then Entity (Name (Item2)) = E
19741 and then not Limited_Present (Item2)
19742 then
19743 Result := True;
19744 return;
19745 end if;
19746
19747 Next (Item2);
19748 end loop;
19749 end if;
19750
19751 Next (Item);
19752 end loop;
19753
19754 -- Second, recurse on with's. We could do this as part of the above
19755 -- loop, but it's probably more efficient to have two loops, because
19756 -- the relevant Elaborate_All is likely to be on the initial unit. In
19757 -- other words, we're walking the with's breadth-first. This part is
19758 -- only necessary in the dynamic elaboration model.
19759
19760 if Dynamic_Elaboration_Checks then
19761 Item := First (Context_Items (CU));
19762 while Present (Item) loop
19763 if Nkind (Item) = N_With_Clause
19764 and then not Limited_Present (Item)
19765 then
19766 -- Note: the following call to Get_Cunit_Unit_Number does a
19767 -- linear search, which could be slow, but it's OK because
19768 -- we're about to give a warning anyway. Also, there might
19769 -- be hundreds of units, but not millions. If it turns out
19770 -- to be a problem, we could store the Get_Cunit_Unit_Number
19771 -- in each N_Compilation_Unit node, but that would involve
19772 -- rearranging N_Compilation_Unit_Aux to make room.
19773
19774 Helper (Get_Cunit_Unit_Number (Library_Unit (Item)));
19775
19776 if Result then
19777 return;
19778 end if;
19779 end if;
19780
19781 Next (Item);
19782 end loop;
19783 end if;
19784 end Helper;
19785
19786 -- Start of processing for Within_Elaborate_All
19787
19788 begin
19789 Helper (Unit);
19790 return Result;
19791 end Within_Elaborate_All;
19792
19793 end Sem_Elab;