| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- L O C A L _ R E S T R I C T -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-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 |
with Aspects; use Aspects; |
| 27 |
with Atree; use Atree; |
| 28 |
with Einfo; use Einfo; |
| 29 |
with Einfo.Entities; use Einfo.Entities; |
| 30 |
with Einfo.Utils; use Einfo.Utils; |
| 31 |
with Errout; use Errout; |
| 32 |
with Lib; use Lib; |
| 33 |
with Restrict; use Restrict; |
| 34 |
with Rident; use Rident; |
| 35 |
with Sem_Aux; use Sem_Aux; |
| 36 |
with Sem_Ch13; use Sem_Ch13; |
| 37 |
with Sem_Util; use Sem_Util; |
| 38 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 39 |
with Sinfo.Utils; use Sinfo.Utils; |
| 40 |
|
| 41 |
package body Local_Restrict is |
| 42 |
function L_R_Image (L_R : Local_Restriction) return String is |
| 43 |
(case L_R is when No_Secondary_Stack => "No_Secondary_Stack", |
| 44 |
when No_Heap_Allocations => "No_Heap_Allocations"); |
| 45 |
-- Like Local_Restriction'Image, but with casing appropriate for |
| 46 |
-- error messages. |
| 47 |
|
| 48 |
function Active_Restriction |
| 49 |
(L_R : Local_Restriction; E : Entity_Id := Current_Scope) return Node_Id; |
| 50 |
-- Returns the Local_Restrictions aspect specification that is in effect |
| 51 |
-- for E and that imposes the given local restriction; returns Empty if |
| 52 |
-- no such aspect specification exists. |
| 53 |
|
| 54 |
procedure Check_For_Corresponding_Local_Restriction |
| 55 |
(Violated : All_Restrictions; N : Node_Id); |
| 56 |
-- Generate error for node N if a violation of the given (non-local) |
| 57 |
-- restriction implies a violation of a corresponding local restriction |
| 58 |
-- that is in effect. |
| 59 |
|
| 60 |
procedure Check_For_Local_Restriction |
| 61 |
(L_R : Local_Restriction; N : Node_Id); |
| 62 |
-- Generate error for node N if given restriction is in effect. |
| 63 |
|
| 64 |
------------------------ |
| 65 |
-- Active_Restriction -- |
| 66 |
------------------------ |
| 67 |
|
| 68 |
function Active_Restriction |
| 69 |
(L_R : Local_Restriction; E : Entity_Id := Current_Scope) return Node_Id |
| 70 |
is |
| 71 |
Scop : Node_Id := E; |
| 72 |
Result : Node_Id; |
| 73 |
begin |
| 74 |
-- If performance of this function becomes a problem then |
| 75 |
-- one possible solution would be cache a set of scopes |
| 76 |
-- for which it is known that no local restrictions apply. |
| 77 |
-- Perhaps with fixed size (maybe 8?) and LRU replacement? |
| 78 |
-- |
| 79 |
-- Or perhaps just a single global Boolean indicating that |
| 80 |
-- no Local_Restrictions aspect specification has been seen |
| 81 |
-- at any time in any context during the current compilation. |
| 82 |
-- If the flag is set, then Active_Restriction returns Empty |
| 83 |
-- without any looping. |
| 84 |
|
| 85 |
while Present (Scop) loop |
| 86 |
Result := Find_Aspect (Scop, Aspect_Local_Restrictions); |
| 87 |
if Present (Result) |
| 88 |
and then Parse_Aspect_Local_Restrictions (Result) (L_R) |
| 89 |
then |
| 90 |
return Result; |
| 91 |
end if; |
| 92 |
|
| 93 |
declare |
| 94 |
Saved_Scope : constant Node_Id := Scop; |
| 95 |
begin |
| 96 |
Scop := Enclosing_Declaration (Scop); |
| 97 |
if Present (Scop) then |
| 98 |
Scop := Parent (Scop); |
| 99 |
if Present (Scop) then |
| 100 |
-- For a subprogram associated with a type, we don't care |
| 101 |
-- where the type was frozen; continue from the type. |
| 102 |
|
| 103 |
if Nkind (Scop) = N_Freeze_Entity then |
| 104 |
Scop := Scope (Entity (Scop)); |
| 105 |
elsif Nkind (Parent (Scop)) = N_Freeze_Entity then |
| 106 |
Scop := Scope (Entity (Parent (Scop))); |
| 107 |
elsif Present (Scope (Saved_Scope)) then |
| 108 |
Scop := Scope (Saved_Scope); |
| 109 |
else |
| 110 |
Scop := Find_Enclosing_Scope (Scop); |
| 111 |
end if; |
| 112 |
end if; |
| 113 |
end if; |
| 114 |
end; |
| 115 |
end loop; |
| 116 |
|
| 117 |
return Empty; |
| 118 |
end Active_Restriction; |
| 119 |
|
| 120 |
----------------------------------------------- |
| 121 |
-- Check_For_Corresponding_Local_Restriction -- |
| 122 |
----------------------------------------------- |
| 123 |
|
| 124 |
procedure Check_For_Corresponding_Local_Restriction |
| 125 |
(Violated : All_Restrictions; N : Node_Id) |
| 126 |
is |
| 127 |
L_R : Local_Restriction; |
| 128 |
begin |
| 129 |
-- Some restrictions map to a corresponding local restriction. |
| 130 |
-- In those cases, check whether the local restriction is in effect. |
| 131 |
-- This is the point at which the specific semantics of each |
| 132 |
-- local restriction is effectively defined. |
| 133 |
case Violated is |
| 134 |
when No_Secondary_Stack => |
| 135 |
L_R := No_Secondary_Stack; |
| 136 |
when No_Allocators | No_Implicit_Heap_Allocations => |
| 137 |
L_R := No_Heap_Allocations; |
| 138 |
when others => |
| 139 |
return; |
| 140 |
end case; |
| 141 |
|
| 142 |
Check_For_Local_Restriction (L_R, N); |
| 143 |
end Check_For_Corresponding_Local_Restriction; |
| 144 |
|
| 145 |
--------------------------------- |
| 146 |
-- Check_For_Local_Restriction -- |
| 147 |
--------------------------------- |
| 148 |
|
| 149 |
procedure Check_For_Local_Restriction |
| 150 |
(L_R : Local_Restriction; N : Node_Id) |
| 151 |
is |
| 152 |
L_R_Aspect_Spec : constant Node_Id := Active_Restriction (L_R); |
| 153 |
begin |
| 154 |
if Present (L_R_Aspect_Spec) then |
| 155 |
Error_Msg_Sloc := Sloc (L_R_Aspect_Spec); |
| 156 |
Error_Msg_N |
| 157 |
("violation of local restriction " & L_R_Image (L_R) & "#", N); |
| 158 |
end if; |
| 159 |
end Check_For_Local_Restriction; |
| 160 |
|
| 161 |
---------------- |
| 162 |
-- Check_Call -- |
| 163 |
---------------- |
| 164 |
|
| 165 |
procedure Check_Call (Call : Node_Id; Callee : Entity_Id := Empty) is |
| 166 |
Restrictions_Enforced_By_Callee : Local_Restriction_Set := |
| 167 |
(others => False); |
| 168 |
|
| 169 |
Real_Callee : Entity_Id; |
| 170 |
begin |
| 171 |
if Present (Callee) then |
| 172 |
Real_Callee := Ultimate_Alias (Callee); |
| 173 |
|
| 174 |
if Is_Intrinsic_Subprogram (Real_Callee) |
| 175 |
or else In_Predefined_Unit (Real_Callee) |
| 176 |
then |
| 177 |
-- If an intrinsic or predefined subprogram violates a local |
| 178 |
-- restriction then we don't catch it here. For that, we rely |
| 179 |
-- on the same mechanism that is used to catch violations of |
| 180 |
-- the corresponding global restriction (i.e., the |
| 181 |
-- Local_Restriction_Checking_Hook call in Check_Restriction). |
| 182 |
return; |
| 183 |
end if; |
| 184 |
|
| 185 |
for L_R in Local_Restriction loop |
| 186 |
if Present (Active_Restriction (L_R, Real_Callee)) then |
| 187 |
Restrictions_Enforced_By_Callee (L_R) := True; |
| 188 |
end if; |
| 189 |
end loop; |
| 190 |
end if; |
| 191 |
|
| 192 |
for L_R in Local_Restriction loop |
| 193 |
if not Restrictions_Enforced_By_Callee (L_R) then |
| 194 |
-- Complain if caller must enforce L_R and callee |
| 195 |
-- does not promise to do that. |
| 196 |
|
| 197 |
Check_For_Local_Restriction (L_R, Call); |
| 198 |
end if; |
| 199 |
end loop; |
| 200 |
end Check_Call; |
| 201 |
|
| 202 |
----------------------- |
| 203 |
-- Check_Overriding -- |
| 204 |
----------------------- |
| 205 |
|
| 206 |
procedure Check_Overriding (Overrider_Op, Overridden_Op : Entity_Id) is |
| 207 |
Ultimate_Overrider : constant Entity_Id := |
| 208 |
Ultimate_Alias (Overrider_Op); |
| 209 |
Ultimate_Overridden : constant Entity_Id := |
| 210 |
Ultimate_Alias (Overridden_Op); |
| 211 |
begin |
| 212 |
-- a minor optimization |
| 213 |
if Ultimate_Overrider = Ultimate_Overridden then |
| 214 |
return; |
| 215 |
end if; |
| 216 |
|
| 217 |
for L_R in Local_Restriction loop |
| 218 |
if Present (Active_Restriction (L_R, Ultimate_Overridden)) |
| 219 |
and then No (Active_Restriction (L_R, Ultimate_Overrider)) |
| 220 |
then |
| 221 |
Error_Msg_Sloc := |
| 222 |
Sloc (Active_Restriction (L_R, Ultimate_Overridden)); |
| 223 |
Error_Msg_N |
| 224 |
("overriding incompatible with local restriction " & |
| 225 |
L_R_Image (L_R) & "#", |
| 226 |
Ultimate_Overrider); |
| 227 |
end if; |
| 228 |
end loop; |
| 229 |
end Check_Overriding; |
| 230 |
|
| 231 |
------------------------------------------ |
| 232 |
-- Check_Actual_Subprogram_For_Instance -- |
| 233 |
------------------------------------------ |
| 234 |
|
| 235 |
procedure Check_Actual_Subprogram_For_Instance |
| 236 |
(Actual_Subp_Name : Node_Id; Formal_Subp : Entity_Id) |
| 237 |
is |
| 238 |
Actual_Subp : Entity_Id := Empty; |
| 239 |
begin |
| 240 |
if Is_Entity_Name (Actual_Subp_Name) then |
| 241 |
Actual_Subp := Entity (Actual_Subp_Name); |
| 242 |
end if; |
| 243 |
|
| 244 |
for L_R in Local_Restriction loop |
| 245 |
-- Complain if some local restriction is in effect for |
| 246 |
-- the formal subprogram but not for the actual subprogram. |
| 247 |
|
| 248 |
if Present (Active_Restriction (L_R, Formal_Subp)) |
| 249 |
and then |
| 250 |
(No (Actual_Subp) |
| 251 |
or else No (Active_Restriction (L_R, Actual_Subp))) |
| 252 |
then |
| 253 |
Error_Msg_Sloc := Sloc (Active_Restriction (L_R, Formal_Subp)); |
| 254 |
Error_Msg_N |
| 255 |
("actual subprogram incompatible with local restriction " & |
| 256 |
L_R_Image (L_R) & " #", |
| 257 |
Actual_Subp_Name); |
| 258 |
end if; |
| 259 |
end loop; |
| 260 |
end Check_Actual_Subprogram_For_Instance; |
| 261 |
|
| 262 |
begin |
| 263 |
-- Allow package Restrict to call package Local_Restrict without |
| 264 |
-- pulling the bulk of semantics into the closure of package Restrict. |
| 265 |
-- |
| 266 |
-- For example, if an allocator is encountered, then package |
| 267 |
-- Restrict is called to check whether a No_Allocators restriction is |
| 268 |
-- in effect. At that point, we also want to check whether a |
| 269 |
-- No_Heap_Allocations local restriction is in effect. This |
| 270 |
-- registration makes that possible. |
| 271 |
|
| 272 |
Local_Restrictions.Local_Restriction_Checking_Hook := |
| 273 |
Check_For_Corresponding_Local_Restriction'Access; |
| 274 |
end Local_Restrict; |