1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             S E M _ E L A B                              --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1997-2020, 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--  This package contains routines which handle access-before-elaboration
27--  run-time checks and compile-time diagnostics. See the body for details.
28
29with Types; use Types;
30
31package Sem_Elab is
32
33   -----------
34   -- Types --
35   -----------
36
37   --  The following type classifies the various enclosing levels used in ABE
38   --  diagnostics.
39
40   type Enclosing_Level_Kind is
41     (Declaration_Level,
42      --  A construct is at the "declaration level" when it appears within the
43      --  declarations of a block statement, an entry body, a subprogram body,
44      --  or a task body, ignoring enclosing packages. Example:
45
46      --    package Pack is
47      --       procedure Proc is                 --  subprogram body
48      --          package Nested is              --  enclosing package ignored
49      --             X ...                       --  at declaration level
50
51      Generic_Spec_Level,
52      Generic_Body_Level,
53      --  A construct is at the "generic level" when it appears in a
54      --  generic package library unit, ignoring enclosing packages. Example:
55
56      --    generic
57      --    package Pack is                      --  generic package spec
58      --       package Nested is                 --  enclosing package ignored
59      --          X ...                          --  at generic library level
60
61      Instantiation_Level,
62      --  A construct is at the "instantiation library level" when it appears
63      --  in a library unit which is also an instantiation. Example:
64
65      --    package Inst is new Gen;             --  at instantiation level
66
67      Library_Spec_Level,
68      Library_Body_Level,
69      --  A construct is at the "library level" when it appears in a package
70      --  library unit, ignoring enclosing packages. Example:
71
72      --    package body Pack is                 --  package body
73      --       package Nested is                 --  enclosing package ignored
74      --          X ...                          --  at library level
75
76      No_Level);
77      --  This value is used to indicate that none of the levels above are in
78      --  effect.
79
80   subtype Generic_Level is Enclosing_Level_Kind range
81     Generic_Spec_Level ..
82     Generic_Body_Level;
83
84   subtype Library_Level is Enclosing_Level_Kind range
85     Library_Spec_Level ..
86     Library_Body_Level;
87
88   subtype Library_Or_Instantiation_Level is Enclosing_Level_Kind range
89     Instantiation_Level ..
90     Library_Body_Level;
91
92   procedure Build_Call_Marker (N : Node_Id);
93   pragma Inline (Build_Call_Marker);
94   --  Create a call marker for call or requeue statement N and record it for
95   --  later processing by the ABE mechanism.
96
97   procedure Build_Variable_Reference_Marker
98     (N     : Node_Id;
99      Read  : Boolean;
100      Write : Boolean);
101   pragma Inline (Build_Variable_Reference_Marker);
102   --  Create a variable reference marker for arbitrary node N if it mentions a
103   --  variable, and record it for later processing by the ABE mechanism. Flag
104   --  Read should be set when the reference denotes a read. Flag Write should
105   --  be set when the reference denotes a write.
106
107   procedure Check_Elaboration_Scenarios;
108   --  Examine each scenario recorded during analysis/resolution and apply the
109   --  Ada or SPARK elaboration rules taking into account the model in effect.
110   --  This processing detects and diagnoses ABE issues, installs conditional
111   --  ABE checks or guaranteed ABE failures, and ensures the elaboration of
112   --  units.
113
114   function Find_Enclosing_Level (N : Node_Id) return Enclosing_Level_Kind;
115   pragma Inline (Find_Enclosing_Level);
116   --  Determine the enclosing level of arbitrary node N
117
118   procedure Initialize;
119   pragma Inline (Initialize);
120   --  Initialize the internal structures of this unit
121
122   procedure Kill_Elaboration_Scenario (N : Node_Id);
123   --  Determine whether arbitrary node N denotes a scenario which requires
124   --  ABE diagnostics or runtime checks and eliminate it from a region with
125   --  dead code.
126
127   procedure Record_Elaboration_Scenario (N : Node_Id);
128   pragma Inline (Record_Elaboration_Scenario);
129   --  Determine whether atribtray node N denotes a scenario which requires
130   --  ABE diagnostics or runtime checks. If this is the case, store N for
131   --  later processing.
132
133   ---------------------------------------------------------------------------
134   --                                                                       --
135   --  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   --
136   --                                                                       --
137   --                          M E C H A N I S M                            --
138   --                                                                       --
139   ---------------------------------------------------------------------------
140
141   --  This section contains the implementation of the pre-18.x Legacy ABE
142   --  Mechanism. The mechanism can be activated using switch -gnatH (legacy
143   --  elaboration checking mode enabled).
144
145   procedure Check_Elab_Assign (N : Node_Id);
146   --  N is either the left side of an assignment, or a procedure argument for
147   --  a mode OUT or IN OUT formal. This procedure checks for a possible case
148   --  of access to an entity from elaboration code before the entity has been
149   --  initialized, and issues appropriate warnings.
150
151   procedure Check_Elab_Call
152     (N            : Node_Id;
153      Outer_Scope  : Entity_Id := Empty;
154      In_Init_Proc : Boolean   := False);
155   --  Check a call for possible elaboration problems. The node N is either an
156   --  N_Function_Call or N_Procedure_Call_Statement node or an access
157   --  attribute reference whose prefix is a subprogram.
158   --
159   --  If SPARK_Mode is On, then N can also be a variable reference, since
160   --  SPARK requires the use of Elaborate_All for references to variables
161   --  in other packages.
162
163   --  The Outer_Scope argument indicates whether this is an outer level
164   --  call from Sem_Res (Outer_Scope set to Empty), or an internal recursive
165   --  call (Outer_Scope set to entity of outermost call, see body). The flag
166   --  In_Init_Proc should be set whenever the current context is a type
167   --  init proc.
168
169   --  Note: this might better be called Check_Elab_Reference (to recognize
170   --  the SPARK case), but we prefer to keep the original name, since this
171   --  is primarily used for checking for calls that could generate an ABE).
172
173   procedure Check_Elab_Calls;
174   --  Not all the processing for Check_Elab_Call can be done at the time
175   --  of calls to Check_Elab_Call. This is because for internal calls, we
176   --  need to wait to complete the check until all generic bodies have been
177   --  instantiated. The Check_Elab_Calls procedure cleans up these waiting
178   --  checks. It is called once after the completion of instantiation.
179
180   procedure Check_Elab_Instantiation
181     (N           : Node_Id;
182      Outer_Scope : Entity_Id := Empty);
183   --  Check an instantiation for possible elaboration problems. N is an
184   --  instantiation node (N_Package_Instantiation, N_Function_Instantiation,
185   --  or N_Procedure_Instantiation), and Outer_Scope indicates if this is
186   --  an outer level call from Sem_Ch12 (Outer_Scope set to Empty), or an
187   --  internal recursive call (Outer_Scope set to scope of outermost call,
188   --  see body for further details). The returned value is relevant only
189   --  for an outer level call, and is set to False if an elaboration error
190   --  is bound to occur on the instantiation, and True otherwise. This is
191   --  used by the caller to signal that the body of the instance should
192   --  not be generated (see detailed description in body).
193
194   procedure Check_Task_Activation (N : Node_Id);
195   --  At the point at which tasks are activated in a package body, check
196   --  that the bodies of the tasks are elaborated.
197
198end Sem_Elab;
199