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