| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- E X P _ U N S T -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2014-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 Atree; use Atree; |
| 27 |
with Debug; use Debug; |
| 28 |
with Einfo; use Einfo; |
| 29 |
with Einfo.Entities; use Einfo.Entities; |
| 30 |
with Einfo.Utils; use Einfo.Utils; |
| 31 |
with Elists; use Elists; |
| 32 |
with Lib; use Lib; |
| 33 |
with Namet; use Namet; |
| 34 |
with Nlists; use Nlists; |
| 35 |
with Nmake; use Nmake; |
| 36 |
with Opt; |
| 37 |
with Output; use Output; |
| 38 |
with Rtsfind; use Rtsfind; |
| 39 |
with Sem; use Sem; |
| 40 |
with Sem_Aux; use Sem_Aux; |
| 41 |
with Sem_Ch8; use Sem_Ch8; |
| 42 |
with Sem_Mech; use Sem_Mech; |
| 43 |
with Sem_Res; use Sem_Res; |
| 44 |
with Sem_Util; use Sem_Util; |
| 45 |
with Sinfo; use Sinfo; |
| 46 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 47 |
with Sinfo.Utils; use Sinfo.Utils; |
| 48 |
with Sinput; use Sinput; |
| 49 |
with Snames; use Snames; |
| 50 |
with Stand; use Stand; |
| 51 |
with Tbuild; use Tbuild; |
| 52 |
with Uintp; use Uintp; |
| 53 |
|
| 54 |
package body Exp_Unst is |
| 55 |
|
| 56 |
----------------------- |
| 57 |
-- Local Subprograms -- |
| 58 |
----------------------- |
| 59 |
|
| 60 |
procedure Unnest_Subprogram |
| 61 |
(Subp : Entity_Id; Subp_Body : Node_Id; For_Inline : Boolean := False); |
| 62 |
-- Subp is a library-level subprogram which has nested subprograms, and |
| 63 |
-- Subp_Body is the corresponding N_Subprogram_Body node. This procedure |
| 64 |
-- declares the AREC types and objects, adds assignments to the AREC record |
| 65 |
-- as required, defines the xxxPTR types for uplevel referenced objects, |
| 66 |
-- adds the ARECP parameter to all nested subprograms which need it, and |
| 67 |
-- modifies all uplevel references appropriately. If For_Inline is True, |
| 68 |
-- we're unnesting this subprogram because it's on the list of inlined |
| 69 |
-- subprograms and should unnest it despite it not being part of the main |
| 70 |
-- unit. |
| 71 |
|
| 72 |
----------- |
| 73 |
-- Calls -- |
| 74 |
----------- |
| 75 |
|
| 76 |
-- Table to record calls within the nest being analyzed. These are the |
| 77 |
-- calls which may need to have an AREC actual added. This table is built |
| 78 |
-- new for each subprogram nest and cleared at the end of processing each |
| 79 |
-- subprogram nest. |
| 80 |
|
| 81 |
type Call_Entry is record |
| 82 |
N : Node_Id; |
| 83 |
-- The actual call |
| 84 |
|
| 85 |
Caller : Entity_Id; |
| 86 |
-- Entity of the subprogram containing the call (can be at any level) |
| 87 |
|
| 88 |
Callee : Entity_Id; |
| 89 |
-- Entity of the subprogram called (always at level 2 or higher). Note |
| 90 |
-- that in accordance with the basic rules of nesting, the level of To |
| 91 |
-- is either less than or equal to the level of From, or one greater. |
| 92 |
end record; |
| 93 |
|
| 94 |
package Calls is new Table.Table ( |
| 95 |
Table_Component_Type => Call_Entry, |
| 96 |
Table_Index_Type => Nat, |
| 97 |
Table_Low_Bound => 1, |
| 98 |
Table_Initial => 100, |
| 99 |
Table_Increment => 200, |
| 100 |
Table_Name => "Unnest_Calls"); |
| 101 |
-- Records each call within the outer subprogram and all nested subprograms |
| 102 |
-- that are to other subprograms nested within the outer subprogram. These |
| 103 |
-- are the calls that may need an additional parameter. |
| 104 |
|
| 105 |
procedure Append_Unique_Call (Call : Call_Entry); |
| 106 |
-- Append a call entry to the Calls table. A check is made to see if the |
| 107 |
-- table already contains this entry and if so it has no effect. |
| 108 |
|
| 109 |
---------------------------------- |
| 110 |
-- Subprograms For Fat Pointers -- |
| 111 |
---------------------------------- |
| 112 |
|
| 113 |
function Build_Access_Type_Decl |
| 114 |
(E : Entity_Id; |
| 115 |
Scop : Entity_Id) return Node_Id; |
| 116 |
-- For an uplevel reference that involves an unconstrained array type, |
| 117 |
-- build an access type declaration for the corresponding activation |
| 118 |
-- record component. The relevant attributes of the access type are |
| 119 |
-- set here to avoid a full analysis that would require a scope stack. |
| 120 |
|
| 121 |
function Needs_Fat_Pointer (E : Entity_Id) return Boolean; |
| 122 |
-- A formal parameter of an unconstrained array type that appears in an |
| 123 |
-- uplevel reference requires the construction of an access type, to be |
| 124 |
-- used in the corresponding component declaration. |
| 125 |
|
| 126 |
----------- |
| 127 |
-- Urefs -- |
| 128 |
----------- |
| 129 |
|
| 130 |
-- Table to record explicit uplevel references to objects (variables, |
| 131 |
-- constants, formal parameters). These are the references that will |
| 132 |
-- need rewriting to use the activation table (AREC) pointers. Also |
| 133 |
-- included are implicit and explicit uplevel references to types, but |
| 134 |
-- these do not get rewritten by the front end. This table is built new |
| 135 |
-- for each subprogram nest and cleared at the end of processing each |
| 136 |
-- subprogram nest. |
| 137 |
|
| 138 |
type Uref_Entry is record |
| 139 |
Ref : Node_Id; |
| 140 |
-- The reference itself. For objects this is always an entity reference |
| 141 |
-- and the referenced entity will have its Is_Uplevel_Referenced_Entity |
| 142 |
-- flag set and will appear in the Uplevel_Referenced_Entities list of |
| 143 |
-- the subprogram declaring this entity. |
| 144 |
|
| 145 |
Ent : Entity_Id; |
| 146 |
-- The Entity_Id of the uplevel referenced object or type |
| 147 |
|
| 148 |
Caller : Entity_Id; |
| 149 |
-- The entity for the subprogram immediately containing this entity |
| 150 |
|
| 151 |
Callee : Entity_Id; |
| 152 |
-- The entity for the subprogram containing the referenced entity. Note |
| 153 |
-- that the level of Callee must be less than the level of Caller, since |
| 154 |
-- this is an uplevel reference. |
| 155 |
end record; |
| 156 |
|
| 157 |
package Urefs is new Table.Table ( |
| 158 |
Table_Component_Type => Uref_Entry, |
| 159 |
Table_Index_Type => Nat, |
| 160 |
Table_Low_Bound => 1, |
| 161 |
Table_Initial => 100, |
| 162 |
Table_Increment => 200, |
| 163 |
Table_Name => "Unnest_Urefs"); |
| 164 |
|
| 165 |
------------------------ |
| 166 |
-- Append_Unique_Call -- |
| 167 |
------------------------ |
| 168 |
|
| 169 |
procedure Append_Unique_Call (Call : Call_Entry) is |
| 170 |
begin |
| 171 |
for J in Calls.First .. Calls.Last loop |
| 172 |
if Calls.Table (J) = Call then |
| 173 |
return; |
| 174 |
end if; |
| 175 |
end loop; |
| 176 |
|
| 177 |
Calls.Append (Call); |
| 178 |
end Append_Unique_Call; |
| 179 |
|
| 180 |
----------------------------- |
| 181 |
-- Build_Access_Type_Decl -- |
| 182 |
----------------------------- |
| 183 |
|
| 184 |
function Build_Access_Type_Decl |
| 185 |
(E : Entity_Id; |
| 186 |
Scop : Entity_Id) return Node_Id |
| 187 |
is |
| 188 |
Loc : constant Source_Ptr := Sloc (E); |
| 189 |
Typ : Entity_Id; |
| 190 |
|
| 191 |
begin |
| 192 |
Typ := Make_Temporary (Loc, 'S'); |
| 193 |
Mutate_Ekind (Typ, E_General_Access_Type); |
| 194 |
Set_Etype (Typ, Typ); |
| 195 |
Set_Scope (Typ, Scop); |
| 196 |
Set_Directly_Designated_Type (Typ, Etype (E)); |
| 197 |
|
| 198 |
return |
| 199 |
Make_Full_Type_Declaration (Loc, |
| 200 |
Defining_Identifier => Typ, |
| 201 |
Type_Definition => |
| 202 |
Make_Access_To_Object_Definition (Loc, |
| 203 |
Subtype_Indication => New_Occurrence_Of (Etype (E), Loc))); |
| 204 |
end Build_Access_Type_Decl; |
| 205 |
|
| 206 |
--------------- |
| 207 |
-- Get_Level -- |
| 208 |
--------------- |
| 209 |
|
| 210 |
function Get_Level (Subp : Entity_Id; Sub : Entity_Id) return Nat is |
| 211 |
Lev : Nat; |
| 212 |
S : Entity_Id; |
| 213 |
|
| 214 |
begin |
| 215 |
Lev := 1; |
| 216 |
S := Sub; |
| 217 |
loop |
| 218 |
if S = Subp then |
| 219 |
return Lev; |
| 220 |
else |
| 221 |
Lev := Lev + 1; |
| 222 |
S := Enclosing_Subprogram (S); |
| 223 |
end if; |
| 224 |
end loop; |
| 225 |
end Get_Level; |
| 226 |
|
| 227 |
-------------------------- |
| 228 |
-- In_Synchronized_Unit -- |
| 229 |
-------------------------- |
| 230 |
|
| 231 |
function In_Synchronized_Unit (Subp : Entity_Id) return Boolean is |
| 232 |
S : Entity_Id := Scope (Subp); |
| 233 |
|
| 234 |
begin |
| 235 |
while Present (S) and then S /= Standard_Standard loop |
| 236 |
if Is_Concurrent_Type (S) then |
| 237 |
return True; |
| 238 |
|
| 239 |
elsif Is_Private_Type (S) |
| 240 |
and then Present (Full_View (S)) |
| 241 |
and then Is_Concurrent_Type (Full_View (S)) |
| 242 |
then |
| 243 |
return True; |
| 244 |
end if; |
| 245 |
|
| 246 |
S := Scope (S); |
| 247 |
end loop; |
| 248 |
|
| 249 |
return False; |
| 250 |
end In_Synchronized_Unit; |
| 251 |
|
| 252 |
----------------------- |
| 253 |
-- Needs_Fat_Pointer -- |
| 254 |
----------------------- |
| 255 |
|
| 256 |
function Needs_Fat_Pointer (E : Entity_Id) return Boolean is |
| 257 |
Typ : constant Entity_Id := Get_Fullest_View (Etype (E)); |
| 258 |
begin |
| 259 |
return Is_Array_Type (Typ) and then not Is_Constrained (Typ); |
| 260 |
end Needs_Fat_Pointer; |
| 261 |
|
| 262 |
---------------- |
| 263 |
-- Subp_Index -- |
| 264 |
---------------- |
| 265 |
|
| 266 |
function Subp_Index (Sub : Entity_Id) return SI_Type is |
| 267 |
E : Entity_Id := Sub; |
| 268 |
|
| 269 |
begin |
| 270 |
pragma Assert (Is_Subprogram (E)); |
| 271 |
|
| 272 |
if Field_Is_Initial_Zero (E, F_Subps_Index) |
| 273 |
or else Subps_Index (E) = Uint_0 |
| 274 |
then |
| 275 |
E := Ultimate_Alias (E); |
| 276 |
|
| 277 |
-- The body of a protected operation has a different name and |
| 278 |
-- has been scanned at this point, and thus has an entry in the |
| 279 |
-- subprogram table. |
| 280 |
|
| 281 |
if E = Sub and then Present (Protected_Body_Subprogram (E)) then |
| 282 |
E := Protected_Body_Subprogram (E); |
| 283 |
end if; |
| 284 |
end if; |
| 285 |
|
| 286 |
pragma Assert (Subps_Index (E) /= Uint_0); |
| 287 |
return SI_Type (UI_To_Int (Subps_Index (E))); |
| 288 |
end Subp_Index; |
| 289 |
|
| 290 |
----------------------- |
| 291 |
-- Unnest_Subprogram -- |
| 292 |
----------------------- |
| 293 |
|
| 294 |
procedure Unnest_Subprogram |
| 295 |
(Subp : Entity_Id; Subp_Body : Node_Id; For_Inline : Boolean := False) is |
| 296 |
function AREC_Name (J : Pos; S : String) return Name_Id; |
| 297 |
-- Returns name for string ARECjS, where j is the decimal value of j |
| 298 |
|
| 299 |
function Enclosing_Subp (Subp : SI_Type) return SI_Type; |
| 300 |
-- Subp is the index of a subprogram which has a Lev greater than 1. |
| 301 |
-- This function returns the index of the enclosing subprogram which |
| 302 |
-- will have a Lev value one less than this. |
| 303 |
|
| 304 |
function Img_Pos (N : Pos) return String; |
| 305 |
-- Return image of N without leading blank |
| 306 |
|
| 307 |
function Upref_Name |
| 308 |
(Ent : Entity_Id; |
| 309 |
Index : Pos; |
| 310 |
Clist : List_Id) return Name_Id; |
| 311 |
-- This function returns the name to be used in the activation record to |
| 312 |
-- reference the variable uplevel. Clist is the list of components that |
| 313 |
-- have been created in the activation record so far. Normally the name |
| 314 |
-- is just a copy of the Chars field of the entity. The exception is |
| 315 |
-- when the name has already been used, in which case we suffix the name |
| 316 |
-- with the index value Index to avoid duplication. This happens with |
| 317 |
-- declare blocks and generic parameters at least. |
| 318 |
|
| 319 |
--------------- |
| 320 |
-- AREC_Name -- |
| 321 |
--------------- |
| 322 |
|
| 323 |
function AREC_Name (J : Pos; S : String) return Name_Id is |
| 324 |
begin |
| 325 |
return Name_Find ("AREC" & Img_Pos (J) & S); |
| 326 |
end AREC_Name; |
| 327 |
|
| 328 |
-------------------- |
| 329 |
-- Enclosing_Subp -- |
| 330 |
-------------------- |
| 331 |
|
| 332 |
function Enclosing_Subp (Subp : SI_Type) return SI_Type is |
| 333 |
STJ : Subp_Entry renames Subps.Table (Subp); |
| 334 |
Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent)); |
| 335 |
begin |
| 336 |
pragma Assert (STJ.Lev > 1); |
| 337 |
pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1); |
| 338 |
return Ret; |
| 339 |
end Enclosing_Subp; |
| 340 |
|
| 341 |
------------- |
| 342 |
-- Img_Pos -- |
| 343 |
------------- |
| 344 |
|
| 345 |
function Img_Pos (N : Pos) return String is |
| 346 |
Buf : String (1 .. 20); |
| 347 |
Ptr : Natural; |
| 348 |
NV : Nat; |
| 349 |
|
| 350 |
begin |
| 351 |
Ptr := Buf'Last; |
| 352 |
NV := N; |
| 353 |
while NV /= 0 loop |
| 354 |
Buf (Ptr) := Character'Val (48 + NV mod 10); |
| 355 |
Ptr := Ptr - 1; |
| 356 |
NV := NV / 10; |
| 357 |
end loop; |
| 358 |
|
| 359 |
return Buf (Ptr + 1 .. Buf'Last); |
| 360 |
end Img_Pos; |
| 361 |
|
| 362 |
---------------- |
| 363 |
-- Upref_Name -- |
| 364 |
---------------- |
| 365 |
|
| 366 |
function Upref_Name |
| 367 |
(Ent : Entity_Id; |
| 368 |
Index : Pos; |
| 369 |
Clist : List_Id) return Name_Id |
| 370 |
is |
| 371 |
C : Node_Id; |
| 372 |
begin |
| 373 |
C := First (Clist); |
| 374 |
loop |
| 375 |
if No (C) then |
| 376 |
return Chars (Ent); |
| 377 |
|
| 378 |
elsif Chars (Defining_Identifier (C)) = Chars (Ent) then |
| 379 |
return |
| 380 |
Name_Find (Get_Name_String (Chars (Ent)) & Img_Pos (Index)); |
| 381 |
else |
| 382 |
Next (C); |
| 383 |
end if; |
| 384 |
end loop; |
| 385 |
end Upref_Name; |
| 386 |
|
| 387 |
-- Start of processing for Unnest_Subprogram |
| 388 |
|
| 389 |
begin |
| 390 |
-- Nothing to do inside a generic (all processing is for instance) |
| 391 |
|
| 392 |
if Inside_A_Generic then |
| 393 |
return; |
| 394 |
end if; |
| 395 |
|
| 396 |
-- If the main unit is a package body then we need to examine the spec |
| 397 |
-- to determine whether the main unit is generic (the scope stack is not |
| 398 |
-- present when this is called on the main unit). |
| 399 |
|
| 400 |
if not For_Inline |
| 401 |
and then Ekind (Cunit_Entity (Main_Unit)) = E_Package_Body |
| 402 |
and then Is_Generic_Unit (Spec_Entity (Cunit_Entity (Main_Unit))) |
| 403 |
then |
| 404 |
return; |
| 405 |
|
| 406 |
-- Only unnest when generating code for the main source unit or if |
| 407 |
-- we're unnesting for inline. But in some Annex E cases the Sloc |
| 408 |
-- points to a different unit, so also make sure that the Parent |
| 409 |
-- isn't in something that we know we're generating code for. |
| 410 |
|
| 411 |
elsif not For_Inline |
| 412 |
and then not In_Extended_Main_Code_Unit (Subp_Body) |
| 413 |
and then not In_Extended_Main_Code_Unit (Parent (Subp_Body)) |
| 414 |
then |
| 415 |
return; |
| 416 |
end if; |
| 417 |
|
| 418 |
-- This routine is called late, after the scope stack is gone. The |
| 419 |
-- following creates a suitable dummy scope stack to be used for the |
| 420 |
-- analyze/expand calls made from this routine. |
| 421 |
|
| 422 |
Push_Scope (Subp); |
| 423 |
|
| 424 |
-- First step, we must mark all nested subprograms that require a static |
| 425 |
-- link (activation record) because either they contain explicit uplevel |
| 426 |
-- references (as indicated by Is_Uplevel_Referenced_Entity being set at |
| 427 |
-- this point), or they make calls to other subprograms in the same nest |
| 428 |
-- that require a static link (in which case we set this flag). |
| 429 |
|
| 430 |
-- This is a recursive definition, and to implement this, we have to |
| 431 |
-- build a call graph for the set of nested subprograms, and then go |
| 432 |
-- over this graph to implement recursively the invariant that if a |
| 433 |
-- subprogram has a call to a subprogram requiring a static link, then |
| 434 |
-- the calling subprogram requires a static link. |
| 435 |
|
| 436 |
-- First populate the above tables |
| 437 |
|
| 438 |
Subps_First := Subps.Last + 1; |
| 439 |
Calls.Init; |
| 440 |
Urefs.Init; |
| 441 |
|
| 442 |
Build_Tables : declare |
| 443 |
Current_Subprogram : Entity_Id := Empty; |
| 444 |
-- When we scan a subprogram body, we set Current_Subprogram to the |
| 445 |
-- corresponding entity. This gets recursively saved and restored. |
| 446 |
|
| 447 |
function Visit_Node (N : Node_Id) return Traverse_Result; |
| 448 |
-- Visit a single node in Subp |
| 449 |
|
| 450 |
----------- |
| 451 |
-- Visit -- |
| 452 |
----------- |
| 453 |
|
| 454 |
procedure Visit is new Traverse_Proc (Visit_Node); |
| 455 |
-- Used to traverse the body of Subp, populating the tables |
| 456 |
|
| 457 |
---------------- |
| 458 |
-- Visit_Node -- |
| 459 |
---------------- |
| 460 |
|
| 461 |
function Visit_Node (N : Node_Id) return Traverse_Result is |
| 462 |
Ent : Entity_Id; |
| 463 |
Caller : Entity_Id; |
| 464 |
Callee : Entity_Id; |
| 465 |
|
| 466 |
procedure Check_Static_Type |
| 467 |
(In_T : Entity_Id; |
| 468 |
N : Node_Id; |
| 469 |
DT : in out Boolean; |
| 470 |
Check_Designated : Boolean := False); |
| 471 |
-- Given a type In_T, checks if it is a static type defined as |
| 472 |
-- a type with no dynamic bounds in sight. If so, the only |
| 473 |
-- action is to set Is_Static_Type True for In_T. If In_T is |
| 474 |
-- not a static type, then all types with dynamic bounds |
| 475 |
-- associated with In_T are detected, and their bounds are |
| 476 |
-- marked as uplevel referenced if not at the library level, |
| 477 |
-- and DT is set True. If N is specified, it's the node that |
| 478 |
-- will need to be replaced. If not specified, it means we |
| 479 |
-- can't do a replacement because the bound is implicit. |
| 480 |
|
| 481 |
-- If Check_Designated is True and In_T or its full view |
| 482 |
-- is an access type, check whether the designated type |
| 483 |
-- has dynamic bounds. |
| 484 |
|
| 485 |
procedure Note_Uplevel_Ref |
| 486 |
(E : Entity_Id; |
| 487 |
N : Node_Id; |
| 488 |
Caller : Entity_Id; |
| 489 |
Callee : Entity_Id); |
| 490 |
-- Called when we detect an explicit or implicit uplevel reference |
| 491 |
-- from within Caller to entity E declared in Callee. E can be a |
| 492 |
-- an object or a type. |
| 493 |
|
| 494 |
procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id); |
| 495 |
-- Enter a subprogram whose body is visible or which is a |
| 496 |
-- subprogram instance into the subprogram table. |
| 497 |
|
| 498 |
----------------------- |
| 499 |
-- Check_Static_Type -- |
| 500 |
----------------------- |
| 501 |
|
| 502 |
procedure Check_Static_Type |
| 503 |
(In_T : Entity_Id; |
| 504 |
N : Node_Id; |
| 505 |
DT : in out Boolean; |
| 506 |
Check_Designated : Boolean := False) |
| 507 |
is |
| 508 |
T : constant Entity_Id := Get_Fullest_View (In_T); |
| 509 |
|
| 510 |
procedure Note_Uplevel_Bound (N : Node_Id); |
| 511 |
-- N is the bound of a dynamic type. This procedure notes that |
| 512 |
-- this bound is uplevel referenced, it can handle references |
| 513 |
-- to entities (typically _FIRST and _LAST entities), and also |
| 514 |
-- attribute references of the form T'name (name is typically |
| 515 |
-- FIRST or LAST) where T is the uplevel referenced bound. |
| 516 |
|
| 517 |
------------------------ |
| 518 |
-- Note_Uplevel_Bound -- |
| 519 |
------------------------ |
| 520 |
|
| 521 |
procedure Note_Uplevel_Bound (N : Node_Id) is |
| 522 |
|
| 523 |
function Note_Uplevel_Bound_Trav |
| 524 |
(N : Node_Id) return Traverse_Result; |
| 525 |
-- Tree visitor that marks entities that are uplevel |
| 526 |
-- referenced. |
| 527 |
|
| 528 |
procedure Do_Note_Uplevel_Bound |
| 529 |
is new Traverse_Proc (Note_Uplevel_Bound_Trav); |
| 530 |
-- Subtree visitor instantiation |
| 531 |
|
| 532 |
----------------------------- |
| 533 |
-- Note_Uplevel_Bound_Trav -- |
| 534 |
----------------------------- |
| 535 |
|
| 536 |
function Note_Uplevel_Bound_Trav |
| 537 |
(N : Node_Id) return Traverse_Result |
| 538 |
is |
| 539 |
begin |
| 540 |
-- Entity name case. Make sure that the entity is |
| 541 |
-- declared in a subprogram. This may not be the case for |
| 542 |
-- a type in a loop appearing in a precondition. Exclude |
| 543 |
-- explicitly discriminants (that can appear in bounds of |
| 544 |
-- discriminated components), enumeration literals and |
| 545 |
-- block. |
| 546 |
|
| 547 |
if Is_Entity_Name (N) then |
| 548 |
if Present (Entity (N)) |
| 549 |
and then not Is_Type (Entity (N)) |
| 550 |
and then Present |
| 551 |
(Enclosing_Subprogram (Entity (N))) |
| 552 |
and then |
| 553 |
Ekind (Entity (N)) |
| 554 |
not in E_Discriminant | E_Enumeration_Literal |
| 555 |
| E_Block |
| 556 |
then |
| 557 |
Note_Uplevel_Ref |
| 558 |
(E => Entity (N), |
| 559 |
N => Empty, |
| 560 |
Caller => Current_Subprogram, |
| 561 |
Callee => Enclosing_Subprogram (Entity (N))); |
| 562 |
end if; |
| 563 |
end if; |
| 564 |
|
| 565 |
-- N_Function_Call are handled later, don't touch them |
| 566 |
-- yet. |
| 567 |
if Nkind (N) in N_Function_Call |
| 568 |
then |
| 569 |
return Skip; |
| 570 |
|
| 571 |
-- In N_Selected_Component and N_Expanded_Name, only the |
| 572 |
-- prefix may be referencing a uplevel entity. |
| 573 |
|
| 574 |
elsif Nkind (N) in N_Selected_Component |
| 575 |
| N_Expanded_Name |
| 576 |
then |
| 577 |
Do_Note_Uplevel_Bound (Prefix (N)); |
| 578 |
return Skip; |
| 579 |
|
| 580 |
-- The type of the prefix may be have an uplevel |
| 581 |
-- reference if this needs bounds. |
| 582 |
|
| 583 |
elsif Nkind (N) = N_Attribute_Reference then |
| 584 |
declare |
| 585 |
Attr : constant Attribute_Id := |
| 586 |
Get_Attribute_Id (Attribute_Name (N)); |
| 587 |
DT : Boolean := False; |
| 588 |
|
| 589 |
begin |
| 590 |
if Attr in |
| 591 |
Attribute_First |
| 592 |
| Attribute_Last |
| 593 |
| Attribute_Length |
| 594 |
and then Is_Constrained (Etype (Prefix (N))) |
| 595 |
then |
| 596 |
Check_Static_Type |
| 597 |
(Etype (Prefix (N)), Empty, DT); |
| 598 |
end if; |
| 599 |
end; |
| 600 |
end if; |
| 601 |
|
| 602 |
return OK; |
| 603 |
end Note_Uplevel_Bound_Trav; |
| 604 |
begin |
| 605 |
Do_Note_Uplevel_Bound (N); |
| 606 |
end Note_Uplevel_Bound; |
| 607 |
|
| 608 |
-- Start of processing for Check_Static_Type |
| 609 |
|
| 610 |
begin |
| 611 |
-- If already marked static, immediate return |
| 612 |
|
| 613 |
if Is_Static_Type (T) and then not Check_Designated then |
| 614 |
return; |
| 615 |
end if; |
| 616 |
|
| 617 |
-- If the type is at library level, always consider it static, |
| 618 |
-- since such uplevel references are irrelevant. |
| 619 |
|
| 620 |
if Is_Library_Level_Entity (T) then |
| 621 |
Set_Is_Static_Type (T); |
| 622 |
return; |
| 623 |
end if; |
| 624 |
|
| 625 |
-- Otherwise figure out what the story is with this type |
| 626 |
|
| 627 |
-- For a scalar type, check bounds |
| 628 |
|
| 629 |
if Is_Scalar_Type (T) then |
| 630 |
|
| 631 |
-- If both bounds static, then this is a static type |
| 632 |
|
| 633 |
declare |
| 634 |
LB : constant Node_Id := Type_Low_Bound (T); |
| 635 |
UB : constant Node_Id := Type_High_Bound (T); |
| 636 |
|
| 637 |
begin |
| 638 |
if not Is_Static_Expression (LB) then |
| 639 |
Note_Uplevel_Bound (LB); |
| 640 |
DT := True; |
| 641 |
end if; |
| 642 |
|
| 643 |
if not Is_Static_Expression (UB) then |
| 644 |
Note_Uplevel_Bound (UB); |
| 645 |
DT := True; |
| 646 |
end if; |
| 647 |
end; |
| 648 |
|
| 649 |
-- For record type, check all components and discriminant |
| 650 |
-- constraints if present. |
| 651 |
|
| 652 |
elsif Is_Record_Type (T) then |
| 653 |
declare |
| 654 |
C : Entity_Id; |
| 655 |
D : Elmt_Id; |
| 656 |
|
| 657 |
begin |
| 658 |
C := First_Component_Or_Discriminant (T); |
| 659 |
while Present (C) loop |
| 660 |
Check_Static_Type (Etype (C), N, DT); |
| 661 |
Next_Component_Or_Discriminant (C); |
| 662 |
end loop; |
| 663 |
|
| 664 |
if Has_Discriminants (T) |
| 665 |
and then Present (Discriminant_Constraint (T)) |
| 666 |
then |
| 667 |
D := First_Elmt (Discriminant_Constraint (T)); |
| 668 |
while Present (D) loop |
| 669 |
if not Is_Static_Expression (Node (D)) then |
| 670 |
Note_Uplevel_Bound (Node (D)); |
| 671 |
DT := True; |
| 672 |
end if; |
| 673 |
|
| 674 |
Next_Elmt (D); |
| 675 |
end loop; |
| 676 |
end if; |
| 677 |
end; |
| 678 |
|
| 679 |
-- For array type, check index types and component type |
| 680 |
|
| 681 |
elsif Is_Array_Type (T) then |
| 682 |
declare |
| 683 |
IX : Node_Id; |
| 684 |
begin |
| 685 |
Check_Static_Type (Component_Type (T), N, DT); |
| 686 |
|
| 687 |
IX := First_Index (T); |
| 688 |
while Present (IX) loop |
| 689 |
Check_Static_Type (Etype (IX), N, DT); |
| 690 |
Next_Index (IX); |
| 691 |
end loop; |
| 692 |
end; |
| 693 |
|
| 694 |
-- For private type, examine whether full view is static |
| 695 |
|
| 696 |
elsif Is_Incomplete_Or_Private_Type (T) |
| 697 |
and then Present (Full_View (T)) |
| 698 |
then |
| 699 |
Check_Static_Type (Full_View (T), N, DT, Check_Designated); |
| 700 |
|
| 701 |
if Is_Static_Type (Full_View (T)) then |
| 702 |
Set_Is_Static_Type (T); |
| 703 |
end if; |
| 704 |
|
| 705 |
-- For access types, check designated type when required |
| 706 |
|
| 707 |
elsif Is_Access_Type (T) and then Check_Designated then |
| 708 |
Check_Static_Type (Directly_Designated_Type (T), N, DT); |
| 709 |
|
| 710 |
-- For now, ignore other types |
| 711 |
|
| 712 |
else |
| 713 |
return; |
| 714 |
end if; |
| 715 |
|
| 716 |
if not DT then |
| 717 |
Set_Is_Static_Type (T); |
| 718 |
end if; |
| 719 |
end Check_Static_Type; |
| 720 |
|
| 721 |
---------------------- |
| 722 |
-- Note_Uplevel_Ref -- |
| 723 |
---------------------- |
| 724 |
|
| 725 |
procedure Note_Uplevel_Ref |
| 726 |
(E : Entity_Id; |
| 727 |
N : Node_Id; |
| 728 |
Caller : Entity_Id; |
| 729 |
Callee : Entity_Id) |
| 730 |
is |
| 731 |
Full_E : Entity_Id := E; |
| 732 |
begin |
| 733 |
-- Nothing to do for static type |
| 734 |
|
| 735 |
if Is_Static_Type (E) then |
| 736 |
return; |
| 737 |
end if; |
| 738 |
|
| 739 |
-- Nothing to do if Caller and Callee are the same |
| 740 |
|
| 741 |
if Caller = Callee then |
| 742 |
return; |
| 743 |
|
| 744 |
elsif Ekind (Callee) in E_Entry | E_Entry_Family then |
| 745 |
return; |
| 746 |
end if; |
| 747 |
|
| 748 |
-- We have a new uplevel referenced entity |
| 749 |
|
| 750 |
if Ekind (E) = E_Constant and then Present (Full_View (E)) then |
| 751 |
Full_E := Full_View (E); |
| 752 |
end if; |
| 753 |
|
| 754 |
-- All we do at this stage is to add the uplevel reference to |
| 755 |
-- the table. It's too early to do anything else, since this |
| 756 |
-- uplevel reference may come from an unreachable subprogram |
| 757 |
-- in which case the entry will be deleted. |
| 758 |
|
| 759 |
Urefs.Append ((N, Full_E, Caller, Callee)); |
| 760 |
end Note_Uplevel_Ref; |
| 761 |
|
| 762 |
------------------------- |
| 763 |
-- Register_Subprogram -- |
| 764 |
------------------------- |
| 765 |
|
| 766 |
procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is |
| 767 |
L : constant Nat := Get_Level (Subp, E); |
| 768 |
|
| 769 |
begin |
| 770 |
-- Subprograms declared in tasks and protected types cannot be |
| 771 |
-- eliminated because calls to them may be in other units, so |
| 772 |
-- they must be treated as reachable. |
| 773 |
|
| 774 |
Subps.Append |
| 775 |
((Ent => E, |
| 776 |
Bod => Bod, |
| 777 |
Lev => L, |
| 778 |
Reachable => In_Synchronized_Unit (E) |
| 779 |
or else Address_Taken (E), |
| 780 |
Uplevel_Ref => L, |
| 781 |
Declares_AREC => False, |
| 782 |
Uents => No_Elist, |
| 783 |
Last => 0, |
| 784 |
ARECnF => Empty, |
| 785 |
ARECn => Empty, |
| 786 |
ARECnT => Empty, |
| 787 |
ARECnPT => Empty, |
| 788 |
ARECnP => Empty, |
| 789 |
ARECnU => Empty)); |
| 790 |
|
| 791 |
Set_Subps_Index (E, UI_From_Int (Subps.Last)); |
| 792 |
|
| 793 |
-- If we marked this reachable because it's in a synchronized |
| 794 |
-- unit, we have to mark all enclosing subprograms as reachable |
| 795 |
-- as well. We do the same for subprograms with Address_Taken, |
| 796 |
-- because otherwise we can run into problems with looking at |
| 797 |
-- enclosing subprograms in Subps.Table due to their being |
| 798 |
-- unreachable (the Subp_Index of unreachable subps is later |
| 799 |
-- set to zero and their entry in Subps.Table is removed). |
| 800 |
|
| 801 |
if In_Synchronized_Unit (E) or else Address_Taken (E) then |
| 802 |
declare |
| 803 |
S : Entity_Id := E; |
| 804 |
|
| 805 |
begin |
| 806 |
for J in reverse 1 .. L - 1 loop |
| 807 |
S := Enclosing_Subprogram (S); |
| 808 |
Subps.Table (Subp_Index (S)).Reachable := True; |
| 809 |
end loop; |
| 810 |
end; |
| 811 |
end if; |
| 812 |
end Register_Subprogram; |
| 813 |
|
| 814 |
-- Start of processing for Visit_Node |
| 815 |
|
| 816 |
begin |
| 817 |
case Nkind (N) is |
| 818 |
|
| 819 |
-- Record a subprogram call |
| 820 |
|
| 821 |
when N_Function_Call |
| 822 |
| N_Procedure_Call_Statement |
| 823 |
=> |
| 824 |
-- We are only interested in direct calls, not indirect |
| 825 |
-- calls (where Name (N) is an explicit dereference) at |
| 826 |
-- least for now! |
| 827 |
|
| 828 |
if Nkind (Name (N)) in N_Has_Entity then |
| 829 |
Ent := Entity (Name (N)); |
| 830 |
|
| 831 |
-- We are only interested in calls to subprograms nested |
| 832 |
-- within Subp. Calls to Subp itself or to subprograms |
| 833 |
-- outside the nested structure do not affect us. |
| 834 |
|
| 835 |
if Is_Subprogram (Ent) |
| 836 |
and then not Is_Generic_Subprogram (Ent) |
| 837 |
and then not Is_Imported (Ent) |
| 838 |
and then not Is_Intrinsic_Subprogram (Ent) |
| 839 |
and then Scope_Within (Ultimate_Alias (Ent), Subp) |
| 840 |
then |
| 841 |
Append_Unique_Call ((N, Current_Subprogram, Ent)); |
| 842 |
end if; |
| 843 |
end if; |
| 844 |
|
| 845 |
-- For all calls where the formal is an unconstrained array |
| 846 |
-- and the actual is constrained we need to check the bounds |
| 847 |
-- for uplevel references. |
| 848 |
|
| 849 |
declare |
| 850 |
Actual : Entity_Id; |
| 851 |
DT : Boolean := False; |
| 852 |
Formal : Node_Id; |
| 853 |
Subp : Entity_Id; |
| 854 |
F_Type : Entity_Id; |
| 855 |
A_Type : Entity_Id; |
| 856 |
|
| 857 |
begin |
| 858 |
if Nkind (Name (N)) = N_Explicit_Dereference then |
| 859 |
Subp := Etype (Name (N)); |
| 860 |
else |
| 861 |
Subp := Entity (Name (N)); |
| 862 |
end if; |
| 863 |
|
| 864 |
Actual := First_Actual (N); |
| 865 |
Formal := First_Formal_With_Extras (Subp); |
| 866 |
|
| 867 |
while Present (Actual) loop |
| 868 |
F_Type := Get_Fullest_View (Etype (Formal)); |
| 869 |
A_Type := Get_Fullest_View (Etype (Actual)); |
| 870 |
|
| 871 |
if Is_Array_Type (F_Type) |
| 872 |
and then not Is_Constrained (F_Type) |
| 873 |
and then Is_Constrained (A_Type) |
| 874 |
then |
| 875 |
Check_Static_Type (A_Type, Empty, DT); |
| 876 |
end if; |
| 877 |
|
| 878 |
Next_Actual (Actual); |
| 879 |
Next_Formal_With_Extras (Formal); |
| 880 |
end loop; |
| 881 |
end; |
| 882 |
|
| 883 |
-- An At_End_Proc in a statement sequence indicates that there |
| 884 |
-- is a call from the enclosing construct or block to that |
| 885 |
-- subprogram. As above, the called entity must be local and |
| 886 |
-- not imported. |
| 887 |
|
| 888 |
when N_Handled_Sequence_Of_Statements | N_Block_Statement => |
| 889 |
if Present (At_End_Proc (N)) |
| 890 |
and then Scope_Within (Entity (At_End_Proc (N)), Subp) |
| 891 |
and then not Is_Imported (Entity (At_End_Proc (N))) |
| 892 |
then |
| 893 |
Append_Unique_Call |
| 894 |
((N, Current_Subprogram, Entity (At_End_Proc (N)))); |
| 895 |
end if; |
| 896 |
|
| 897 |
-- Similarly, the following constructs include a semantic |
| 898 |
-- attribute Procedure_To_Call that must be handled like |
| 899 |
-- other calls. Likewise for attribute Storage_Pool. |
| 900 |
|
| 901 |
when N_Allocator |
| 902 |
| N_Extended_Return_Statement |
| 903 |
| N_Free_Statement |
| 904 |
| N_Simple_Return_Statement |
| 905 |
=> |
| 906 |
declare |
| 907 |
Pool : constant Entity_Id := Storage_Pool (N); |
| 908 |
Proc : constant Entity_Id := Procedure_To_Call (N); |
| 909 |
|
| 910 |
begin |
| 911 |
if Present (Proc) |
| 912 |
and then Scope_Within (Proc, Subp) |
| 913 |
and then not Is_Imported (Proc) |
| 914 |
then |
| 915 |
Append_Unique_Call ((N, Current_Subprogram, Proc)); |
| 916 |
end if; |
| 917 |
|
| 918 |
if Present (Pool) |
| 919 |
and then not Is_Library_Level_Entity (Pool) |
| 920 |
and then Scope_Within_Or_Same (Scope (Pool), Subp) |
| 921 |
then |
| 922 |
Caller := Current_Subprogram; |
| 923 |
Callee := Enclosing_Subprogram (Pool); |
| 924 |
|
| 925 |
if Callee /= Caller then |
| 926 |
Note_Uplevel_Ref (Pool, Empty, Caller, Callee); |
| 927 |
end if; |
| 928 |
end if; |
| 929 |
end; |
| 930 |
|
| 931 |
-- For an allocator with a qualified expression, check type |
| 932 |
-- of expression being qualified. The explicit type name is |
| 933 |
-- handled as an entity reference. |
| 934 |
|
| 935 |
if Nkind (N) = N_Allocator |
| 936 |
and then Nkind (Expression (N)) = N_Qualified_Expression |
| 937 |
then |
| 938 |
declare |
| 939 |
DT : Boolean := False; |
| 940 |
begin |
| 941 |
Check_Static_Type |
| 942 |
(Etype (Expression (Expression (N))), Empty, DT); |
| 943 |
end; |
| 944 |
|
| 945 |
-- For a Return or Free (all other nodes we handle here), |
| 946 |
-- we usually need the size of the object, so we need to be |
| 947 |
-- sure that any nonstatic bounds of the expression's type |
| 948 |
-- that are uplevel are handled. |
| 949 |
|
| 950 |
elsif Nkind (N) /= N_Allocator |
| 951 |
and then Present (Expression (N)) |
| 952 |
then |
| 953 |
declare |
| 954 |
DT : Boolean := False; |
| 955 |
begin |
| 956 |
Check_Static_Type |
| 957 |
(Etype (Expression (N)), |
| 958 |
Empty, |
| 959 |
DT, |
| 960 |
Check_Designated => Nkind (N) = N_Free_Statement); |
| 961 |
end; |
| 962 |
end if; |
| 963 |
|
| 964 |
-- A 'Access reference is a (potential) call. So is 'Address, |
| 965 |
-- in particular on imported subprograms. Other attributes |
| 966 |
-- require special handling. |
| 967 |
|
| 968 |
when N_Attribute_Reference => |
| 969 |
declare |
| 970 |
Attr : constant Attribute_Id := |
| 971 |
Get_Attribute_Id (Attribute_Name (N)); |
| 972 |
begin |
| 973 |
case Attr is |
| 974 |
when Attribute_Access |
| 975 |
| Attribute_Unchecked_Access |
| 976 |
| Attribute_Unrestricted_Access |
| 977 |
| Attribute_Address |
| 978 |
=> |
| 979 |
if Nkind (Prefix (N)) in N_Has_Entity then |
| 980 |
Ent := Entity (Prefix (N)); |
| 981 |
|
| 982 |
-- We only need to examine calls to subprograms |
| 983 |
-- nested within current Subp. |
| 984 |
|
| 985 |
if Scope_Within (Ent, Subp) then |
| 986 |
if Is_Imported (Ent) then |
| 987 |
null; |
| 988 |
|
| 989 |
elsif Is_Subprogram (Ent) then |
| 990 |
Append_Unique_Call |
| 991 |
((N, Current_Subprogram, Ent)); |
| 992 |
end if; |
| 993 |
end if; |
| 994 |
end if; |
| 995 |
|
| 996 |
-- References to bounds can be uplevel references if |
| 997 |
-- the type isn't static. |
| 998 |
|
| 999 |
when Attribute_First |
| 1000 |
| Attribute_Last |
| 1001 |
| Attribute_Length |
| 1002 |
=> |
| 1003 |
-- Special-case attributes of objects whose bounds |
| 1004 |
-- may be uplevel references. More complex prefixes |
| 1005 |
-- handled during full traversal. Note that if the |
| 1006 |
-- nominal subtype of the prefix is unconstrained, |
| 1007 |
-- the bound must be obtained from the object, not |
| 1008 |
-- from the (possibly) uplevel reference. We call |
| 1009 |
-- Get_Referenced_Object to deal with prefixes that |
| 1010 |
-- are object renamings (prefixes that are types |
| 1011 |
-- can be passed and will simply be returned). But |
| 1012 |
-- it's also legal to get the bounds from the type |
| 1013 |
-- of the prefix, so we have to handle both cases. |
| 1014 |
|
| 1015 |
declare |
| 1016 |
DT : Boolean := False; |
| 1017 |
|
| 1018 |
begin |
| 1019 |
if Is_Constrained |
| 1020 |
(Etype (Get_Referenced_Object (Prefix (N)))) |
| 1021 |
then |
| 1022 |
Check_Static_Type |
| 1023 |
(Etype (Get_Referenced_Object (Prefix (N))), |
| 1024 |
Empty, DT); |
| 1025 |
end if; |
| 1026 |
|
| 1027 |
if Is_Constrained (Etype (Prefix (N))) then |
| 1028 |
Check_Static_Type |
| 1029 |
(Etype (Prefix (N)), Empty, DT); |
| 1030 |
end if; |
| 1031 |
end; |
| 1032 |
|
| 1033 |
when others => |
| 1034 |
null; |
| 1035 |
end case; |
| 1036 |
end; |
| 1037 |
|
| 1038 |
-- Component associations in aggregates are either static or |
| 1039 |
-- else the aggregate will be expanded into assignments, in |
| 1040 |
-- which case the expression is analyzed later and provides |
| 1041 |
-- no relevant code generation. |
| 1042 |
|
| 1043 |
when N_Component_Association => |
| 1044 |
if No (Expression (N)) |
| 1045 |
or else No (Etype (Expression (N))) |
| 1046 |
then |
| 1047 |
return Skip; |
| 1048 |
end if; |
| 1049 |
|
| 1050 |
-- Generic associations are not analyzed: the actuals are |
| 1051 |
-- transferred to renaming and subtype declarations that |
| 1052 |
-- are the ones that must be examined. |
| 1053 |
|
| 1054 |
when N_Generic_Association => |
| 1055 |
return Skip; |
| 1056 |
|
| 1057 |
-- Indexed references can be uplevel if the type isn't static |
| 1058 |
-- and if the lower bound (or an inner bound for a multi- |
| 1059 |
-- dimensional array) is uplevel. |
| 1060 |
|
| 1061 |
when N_Indexed_Component |
| 1062 |
| N_Slice |
| 1063 |
=> |
| 1064 |
if Is_Constrained (Etype (Prefix (N))) then |
| 1065 |
declare |
| 1066 |
DT : Boolean := False; |
| 1067 |
begin |
| 1068 |
Check_Static_Type (Etype (Prefix (N)), Empty, DT); |
| 1069 |
end; |
| 1070 |
end if; |
| 1071 |
|
| 1072 |
-- A selected component can have an implicit up-level |
| 1073 |
-- reference due to the bounds of previous fields in the |
| 1074 |
-- record. We simplify the processing here by examining |
| 1075 |
-- all components of the record. |
| 1076 |
|
| 1077 |
-- Selected components appear as unit names and end labels |
| 1078 |
-- for child units. Prefixes of these nodes denote parent |
| 1079 |
-- units and carry no type information so they are skipped. |
| 1080 |
|
| 1081 |
when N_Selected_Component => |
| 1082 |
if Present (Etype (Prefix (N))) then |
| 1083 |
declare |
| 1084 |
DT : Boolean := False; |
| 1085 |
begin |
| 1086 |
Check_Static_Type (Etype (Prefix (N)), Empty, DT); |
| 1087 |
end; |
| 1088 |
end if; |
| 1089 |
|
| 1090 |
-- For EQ/NE comparisons, we need the type of the operands |
| 1091 |
-- in order to do the comparison, which means we need the |
| 1092 |
-- bounds. |
| 1093 |
|
| 1094 |
when N_Op_Eq |
| 1095 |
| N_Op_Ne |
| 1096 |
=> |
| 1097 |
declare |
| 1098 |
DT : Boolean := False; |
| 1099 |
begin |
| 1100 |
Check_Static_Type (Etype (Left_Opnd (N)), Empty, DT); |
| 1101 |
Check_Static_Type (Etype (Right_Opnd (N)), Empty, DT); |
| 1102 |
end; |
| 1103 |
|
| 1104 |
-- Likewise we need the sizes to compute how much to move in |
| 1105 |
-- an assignment. |
| 1106 |
|
| 1107 |
when N_Assignment_Statement => |
| 1108 |
declare |
| 1109 |
DT : Boolean := False; |
| 1110 |
begin |
| 1111 |
Check_Static_Type (Etype (Name (N)), Empty, DT); |
| 1112 |
Check_Static_Type (Etype (Expression (N)), Empty, DT); |
| 1113 |
end; |
| 1114 |
|
| 1115 |
-- Record a subprogram. We record a subprogram body that acts |
| 1116 |
-- as a spec. Otherwise we record a subprogram declaration, |
| 1117 |
-- providing that it has a corresponding body we can get hold |
| 1118 |
-- of. The case of no corresponding body being available is |
| 1119 |
-- ignored for now. |
| 1120 |
|
| 1121 |
when N_Subprogram_Body => |
| 1122 |
Ent := Unique_Defining_Entity (N); |
| 1123 |
|
| 1124 |
-- Ignore generic subprogram |
| 1125 |
|
| 1126 |
if Is_Generic_Subprogram (Ent) then |
| 1127 |
return Skip; |
| 1128 |
end if; |
| 1129 |
|
| 1130 |
-- Make new entry in subprogram table if not already made |
| 1131 |
|
| 1132 |
Register_Subprogram (Ent, N); |
| 1133 |
|
| 1134 |
-- Record a call from an At_End_Proc |
| 1135 |
|
| 1136 |
if Present (At_End_Proc (N)) |
| 1137 |
and then Scope_Within (Entity (At_End_Proc (N)), Subp) |
| 1138 |
and then not Is_Imported (Entity (At_End_Proc (N))) |
| 1139 |
then |
| 1140 |
Append_Unique_Call ((N, Ent, Entity (At_End_Proc (N)))); |
| 1141 |
end if; |
| 1142 |
|
| 1143 |
-- We make a recursive call to scan the subprogram body, so |
| 1144 |
-- that we can save and restore Current_Subprogram. |
| 1145 |
|
| 1146 |
declare |
| 1147 |
Save_CS : constant Entity_Id := Current_Subprogram; |
| 1148 |
Decl : Node_Id; |
| 1149 |
|
| 1150 |
begin |
| 1151 |
Current_Subprogram := Ent; |
| 1152 |
|
| 1153 |
-- Scan declarations |
| 1154 |
|
| 1155 |
Decl := First (Declarations (N)); |
| 1156 |
while Present (Decl) loop |
| 1157 |
Visit (Decl); |
| 1158 |
Next (Decl); |
| 1159 |
end loop; |
| 1160 |
|
| 1161 |
-- Scan statements |
| 1162 |
|
| 1163 |
Visit (Handled_Statement_Sequence (N)); |
| 1164 |
|
| 1165 |
-- Restore current subprogram setting |
| 1166 |
|
| 1167 |
Current_Subprogram := Save_CS; |
| 1168 |
end; |
| 1169 |
|
| 1170 |
-- Now at this level, return skipping the subprogram body |
| 1171 |
-- descendants, since we already took care of them! |
| 1172 |
|
| 1173 |
return Skip; |
| 1174 |
|
| 1175 |
-- If we have a body stub, visit the associated subunit, which |
| 1176 |
-- is a semantic descendant of the stub. |
| 1177 |
|
| 1178 |
when N_Body_Stub => |
| 1179 |
Visit (Stub_Subunit (N)); |
| 1180 |
|
| 1181 |
-- A declaration of a wrapper package indicates a subprogram |
| 1182 |
-- instance for which there is no explicit body. Enter the |
| 1183 |
-- subprogram instance in the table. |
| 1184 |
|
| 1185 |
when N_Package_Declaration => |
| 1186 |
if Is_Wrapper_Package (Defining_Entity (N)) then |
| 1187 |
Register_Subprogram |
| 1188 |
(Related_Instance (Defining_Entity (N)), Empty); |
| 1189 |
end if; |
| 1190 |
|
| 1191 |
-- Skip generic declarations |
| 1192 |
|
| 1193 |
when N_Generic_Declaration => |
| 1194 |
return Skip; |
| 1195 |
|
| 1196 |
-- Skip generic package body |
| 1197 |
|
| 1198 |
when N_Package_Body => |
| 1199 |
if Present (Corresponding_Spec (N)) |
| 1200 |
and then Ekind (Corresponding_Spec (N)) = E_Generic_Package |
| 1201 |
then |
| 1202 |
return Skip; |
| 1203 |
end if; |
| 1204 |
|
| 1205 |
-- Aspects, pragmas and component declarations are ignored. |
| 1206 |
-- Quantified expressions are expanded into explicit loops |
| 1207 |
-- and the original epression must be ignored. |
| 1208 |
|
| 1209 |
when N_Aspect_Specification |
| 1210 |
| N_Component_Declaration |
| 1211 |
| N_Pragma |
| 1212 |
| N_Quantified_Expression |
| 1213 |
=> |
| 1214 |
return Skip; |
| 1215 |
|
| 1216 |
-- We want to skip the function spec for a generic function |
| 1217 |
-- to avoid looking at any generic types that might be in |
| 1218 |
-- its formals. |
| 1219 |
|
| 1220 |
when N_Function_Specification => |
| 1221 |
if Is_Generic_Subprogram (Unique_Defining_Entity (N)) then |
| 1222 |
return Skip; |
| 1223 |
end if; |
| 1224 |
|
| 1225 |
-- Otherwise record an uplevel reference in a local identifier |
| 1226 |
|
| 1227 |
when others => |
| 1228 |
if Nkind (N) in N_Has_Entity |
| 1229 |
and then Present (Entity (N)) |
| 1230 |
then |
| 1231 |
Ent := Entity (N); |
| 1232 |
|
| 1233 |
-- Only interested in entities declared within our nest |
| 1234 |
|
| 1235 |
if not Is_Library_Level_Entity (Ent) |
| 1236 |
and then Scope_Within_Or_Same (Scope (Ent), Subp) |
| 1237 |
|
| 1238 |
-- Skip entities defined in inlined subprograms |
| 1239 |
|
| 1240 |
and then |
| 1241 |
Chars (Enclosing_Subprogram (Ent)) /= Name_uParent |
| 1242 |
|
| 1243 |
-- Constants and variables are potentially uplevel |
| 1244 |
-- references to global declarations. |
| 1245 |
|
| 1246 |
and then |
| 1247 |
(Ekind (Ent) in E_Constant |
| 1248 |
| E_Loop_Parameter |
| 1249 |
| E_Variable |
| 1250 |
|
| 1251 |
-- Formals are interesting, but not if being used |
| 1252 |
-- as mere names of parameters for name notation |
| 1253 |
-- calls. |
| 1254 |
|
| 1255 |
or else |
| 1256 |
(Is_Formal (Ent) |
| 1257 |
and then not |
| 1258 |
(Nkind (Parent (N)) = N_Parameter_Association |
| 1259 |
and then Selector_Name (Parent (N)) = N)) |
| 1260 |
|
| 1261 |
-- Types other than known Is_Static types are |
| 1262 |
-- potentially interesting. |
| 1263 |
|
| 1264 |
or else |
| 1265 |
(Is_Type (Ent) and then not Is_Static_Type (Ent))) |
| 1266 |
then |
| 1267 |
-- Here we have a potentially interesting uplevel |
| 1268 |
-- reference to examine. |
| 1269 |
|
| 1270 |
if Is_Type (Ent) then |
| 1271 |
declare |
| 1272 |
DT : Boolean := False; |
| 1273 |
|
| 1274 |
begin |
| 1275 |
Check_Static_Type (Ent, N, DT); |
| 1276 |
return OK; |
| 1277 |
end; |
| 1278 |
end if; |
| 1279 |
|
| 1280 |
Caller := Current_Subprogram; |
| 1281 |
Callee := Enclosing_Subprogram (Ent); |
| 1282 |
|
| 1283 |
if Callee /= Caller |
| 1284 |
and then (not Is_Static_Type (Ent) |
| 1285 |
or else Needs_Fat_Pointer (Ent)) |
| 1286 |
then |
| 1287 |
Note_Uplevel_Ref (Ent, N, Caller, Callee); |
| 1288 |
|
| 1289 |
-- Check the type of a formal parameter of the current |
| 1290 |
-- subprogram, whose formal type may be an uplevel |
| 1291 |
-- reference. |
| 1292 |
|
| 1293 |
elsif Is_Formal (Ent) |
| 1294 |
and then Scope (Ent) = Current_Subprogram |
| 1295 |
then |
| 1296 |
declare |
| 1297 |
DT : Boolean := False; |
| 1298 |
|
| 1299 |
begin |
| 1300 |
Check_Static_Type (Etype (Ent), Empty, DT); |
| 1301 |
end; |
| 1302 |
end if; |
| 1303 |
end if; |
| 1304 |
end if; |
| 1305 |
end case; |
| 1306 |
|
| 1307 |
-- Fall through to continue scanning children of this node |
| 1308 |
|
| 1309 |
return OK; |
| 1310 |
end Visit_Node; |
| 1311 |
|
| 1312 |
-- Start of processing for Build_Tables |
| 1313 |
|
| 1314 |
begin |
| 1315 |
-- Traverse the body to get subprograms, calls and uplevel references |
| 1316 |
|
| 1317 |
Visit (Subp_Body); |
| 1318 |
end Build_Tables; |
| 1319 |
|
| 1320 |
-- Now do the first transitive closure which determines which |
| 1321 |
-- subprograms in the nest are actually reachable. |
| 1322 |
|
| 1323 |
Reachable_Closure : declare |
| 1324 |
Modified : Boolean; |
| 1325 |
|
| 1326 |
begin |
| 1327 |
Subps.Table (Subps_First).Reachable := True; |
| 1328 |
|
| 1329 |
-- We use a simple minded algorithm as follows (obviously this can |
| 1330 |
-- be done more efficiently, using one of the standard algorithms |
| 1331 |
-- for efficient transitive closure computation, but this is simple |
| 1332 |
-- and most likely fast enough that its speed does not matter). |
| 1333 |
|
| 1334 |
-- Repeatedly scan the list of calls. Any time we find a call from |
| 1335 |
-- A to B, where A is reachable, but B is not, then B is reachable, |
| 1336 |
-- and note that we have made a change by setting Modified True. We |
| 1337 |
-- repeat this until we make a pass with no modifications. |
| 1338 |
|
| 1339 |
Outer : loop |
| 1340 |
Modified := False; |
| 1341 |
Inner : for J in Calls.First .. Calls.Last loop |
| 1342 |
declare |
| 1343 |
CTJ : Call_Entry renames Calls.Table (J); |
| 1344 |
|
| 1345 |
SINF : constant SI_Type := Subp_Index (CTJ.Caller); |
| 1346 |
SINT : constant SI_Type := Subp_Index (CTJ.Callee); |
| 1347 |
|
| 1348 |
SUBF : Subp_Entry renames Subps.Table (SINF); |
| 1349 |
SUBT : Subp_Entry renames Subps.Table (SINT); |
| 1350 |
|
| 1351 |
begin |
| 1352 |
if SUBF.Reachable and then not SUBT.Reachable then |
| 1353 |
SUBT.Reachable := True; |
| 1354 |
Modified := True; |
| 1355 |
end if; |
| 1356 |
end; |
| 1357 |
end loop Inner; |
| 1358 |
|
| 1359 |
exit Outer when not Modified; |
| 1360 |
end loop Outer; |
| 1361 |
end Reachable_Closure; |
| 1362 |
|
| 1363 |
-- Remove calls from unreachable subprograms |
| 1364 |
|
| 1365 |
declare |
| 1366 |
New_Index : Nat; |
| 1367 |
|
| 1368 |
begin |
| 1369 |
New_Index := 0; |
| 1370 |
for J in Calls.First .. Calls.Last loop |
| 1371 |
declare |
| 1372 |
CTJ : Call_Entry renames Calls.Table (J); |
| 1373 |
|
| 1374 |
SINF : constant SI_Type := Subp_Index (CTJ.Caller); |
| 1375 |
SINT : constant SI_Type := Subp_Index (CTJ.Callee); |
| 1376 |
|
| 1377 |
SUBF : Subp_Entry renames Subps.Table (SINF); |
| 1378 |
SUBT : Subp_Entry renames Subps.Table (SINT); |
| 1379 |
|
| 1380 |
begin |
| 1381 |
if SUBF.Reachable then |
| 1382 |
pragma Assert (SUBT.Reachable); |
| 1383 |
New_Index := New_Index + 1; |
| 1384 |
Calls.Table (New_Index) := Calls.Table (J); |
| 1385 |
end if; |
| 1386 |
end; |
| 1387 |
end loop; |
| 1388 |
|
| 1389 |
Calls.Set_Last (New_Index); |
| 1390 |
end; |
| 1391 |
|
| 1392 |
-- Remove uplevel references from unreachable subprograms |
| 1393 |
|
| 1394 |
declare |
| 1395 |
New_Index : Nat; |
| 1396 |
|
| 1397 |
begin |
| 1398 |
New_Index := 0; |
| 1399 |
for J in Urefs.First .. Urefs.Last loop |
| 1400 |
declare |
| 1401 |
URJ : Uref_Entry renames Urefs.Table (J); |
| 1402 |
|
| 1403 |
SINF : constant SI_Type := Subp_Index (URJ.Caller); |
| 1404 |
SINT : constant SI_Type := Subp_Index (URJ.Callee); |
| 1405 |
|
| 1406 |
SUBF : Subp_Entry renames Subps.Table (SINF); |
| 1407 |
SUBT : Subp_Entry renames Subps.Table (SINT); |
| 1408 |
|
| 1409 |
S : Entity_Id; |
| 1410 |
|
| 1411 |
begin |
| 1412 |
-- Keep reachable reference |
| 1413 |
|
| 1414 |
if SUBF.Reachable then |
| 1415 |
New_Index := New_Index + 1; |
| 1416 |
Urefs.Table (New_Index) := Urefs.Table (J); |
| 1417 |
|
| 1418 |
-- And since we know we are keeping this one, this is a good |
| 1419 |
-- place to fill in information for a good reference. |
| 1420 |
|
| 1421 |
-- Mark all enclosing subprograms need to declare AREC |
| 1422 |
|
| 1423 |
S := URJ.Caller; |
| 1424 |
loop |
| 1425 |
S := Enclosing_Subprogram (S); |
| 1426 |
|
| 1427 |
-- If we are at the top level, as can happen with |
| 1428 |
-- references to formals in aspects of nested subprogram |
| 1429 |
-- declarations, there are no further subprograms to mark |
| 1430 |
-- as requiring activation records. |
| 1431 |
|
| 1432 |
exit when No (S); |
| 1433 |
|
| 1434 |
declare |
| 1435 |
SUBI : Subp_Entry renames Subps.Table (Subp_Index (S)); |
| 1436 |
begin |
| 1437 |
SUBI.Declares_AREC := True; |
| 1438 |
|
| 1439 |
-- If this entity was marked reachable because it is |
| 1440 |
-- in a task or protected type, there may not appear |
| 1441 |
-- to be any calls to it, which would normally adjust |
| 1442 |
-- the levels of the parent subprograms. So we need to |
| 1443 |
-- be sure that the uplevel reference of that entity |
| 1444 |
-- takes into account possible calls. |
| 1445 |
|
| 1446 |
if In_Synchronized_Unit (SUBF.Ent) |
| 1447 |
and then SUBT.Lev < SUBI.Uplevel_Ref |
| 1448 |
then |
| 1449 |
SUBI.Uplevel_Ref := SUBT.Lev; |
| 1450 |
end if; |
| 1451 |
end; |
| 1452 |
|
| 1453 |
exit when S = URJ.Callee; |
| 1454 |
end loop; |
| 1455 |
|
| 1456 |
-- Add to list of uplevel referenced entities for Callee. |
| 1457 |
-- We do not add types to this list, only actual references |
| 1458 |
-- to objects that will be referenced uplevel, and we use |
| 1459 |
-- the flag Is_Uplevel_Referenced_Entity to avoid making |
| 1460 |
-- duplicate entries in the list. Discriminants are also |
| 1461 |
-- excluded, only the enclosing object can appear in the |
| 1462 |
-- list. |
| 1463 |
|
| 1464 |
if not Is_Uplevel_Referenced_Entity (URJ.Ent) |
| 1465 |
and then Ekind (URJ.Ent) /= E_Discriminant |
| 1466 |
then |
| 1467 |
Set_Is_Uplevel_Referenced_Entity (URJ.Ent); |
| 1468 |
Append_New_Elmt (URJ.Ent, SUBT.Uents); |
| 1469 |
end if; |
| 1470 |
|
| 1471 |
-- And set uplevel indication for caller |
| 1472 |
|
| 1473 |
if SUBT.Lev < SUBF.Uplevel_Ref then |
| 1474 |
SUBF.Uplevel_Ref := SUBT.Lev; |
| 1475 |
end if; |
| 1476 |
end if; |
| 1477 |
end; |
| 1478 |
end loop; |
| 1479 |
|
| 1480 |
Urefs.Set_Last (New_Index); |
| 1481 |
end; |
| 1482 |
|
| 1483 |
-- Remove unreachable subprograms from Subps table. Note that we do |
| 1484 |
-- this after eliminating entries from the other two tables, since |
| 1485 |
-- those elimination steps depend on referencing the Subps table. |
| 1486 |
|
| 1487 |
declare |
| 1488 |
New_SI : SI_Type; |
| 1489 |
|
| 1490 |
begin |
| 1491 |
New_SI := Subps_First - 1; |
| 1492 |
for J in Subps_First .. Subps.Last loop |
| 1493 |
declare |
| 1494 |
STJ : Subp_Entry renames Subps.Table (J); |
| 1495 |
Spec : Node_Id; |
| 1496 |
Decl : Node_Id; |
| 1497 |
|
| 1498 |
begin |
| 1499 |
-- Subprogram is reachable, copy and reset index |
| 1500 |
|
| 1501 |
if STJ.Reachable then |
| 1502 |
New_SI := New_SI + 1; |
| 1503 |
Subps.Table (New_SI) := STJ; |
| 1504 |
Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI)); |
| 1505 |
|
| 1506 |
-- Subprogram is not reachable |
| 1507 |
|
| 1508 |
else |
| 1509 |
-- Clear index, since no longer active |
| 1510 |
|
| 1511 |
Set_Subps_Index (Subps.Table (J).Ent, Uint_0); |
| 1512 |
|
| 1513 |
-- Output debug information if -gnatd.3 set |
| 1514 |
|
| 1515 |
if Debug_Flag_Dot_3 then |
| 1516 |
Write_Str ("Eliminate "); |
| 1517 |
Write_Name (Chars (Subps.Table (J).Ent)); |
| 1518 |
Write_Str (" at "); |
| 1519 |
Write_Location (Sloc (Subps.Table (J).Ent)); |
| 1520 |
Write_Str (" (not referenced)"); |
| 1521 |
Write_Eol; |
| 1522 |
end if; |
| 1523 |
|
| 1524 |
-- Rewrite declaration, body, and corresponding freeze node |
| 1525 |
-- to null statements. |
| 1526 |
|
| 1527 |
-- A subprogram instantiation does not have an explicit |
| 1528 |
-- body. If unused, we could remove the corresponding |
| 1529 |
-- wrapper package and its body. |
| 1530 |
|
| 1531 |
if Present (STJ.Bod) then |
| 1532 |
Spec := Corresponding_Spec (STJ.Bod); |
| 1533 |
|
| 1534 |
if Present (Spec) then |
| 1535 |
Decl := Parent (Declaration_Node (Spec)); |
| 1536 |
Rewrite (Decl, Make_Null_Statement (Sloc (Decl))); |
| 1537 |
|
| 1538 |
if Present (Freeze_Node (Spec)) then |
| 1539 |
Rewrite (Freeze_Node (Spec), |
| 1540 |
Make_Null_Statement (Sloc (Decl))); |
| 1541 |
end if; |
| 1542 |
end if; |
| 1543 |
|
| 1544 |
Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod))); |
| 1545 |
end if; |
| 1546 |
end if; |
| 1547 |
end; |
| 1548 |
end loop; |
| 1549 |
|
| 1550 |
Subps.Set_Last (New_SI); |
| 1551 |
end; |
| 1552 |
|
| 1553 |
-- Now it is time for the second transitive closure, which follows calls |
| 1554 |
-- and makes sure that A calls B, and B has uplevel references, then A |
| 1555 |
-- is also marked as having uplevel references. |
| 1556 |
|
| 1557 |
Closure_Uplevel : declare |
| 1558 |
Modified : Boolean; |
| 1559 |
|
| 1560 |
begin |
| 1561 |
-- We use a simple minded algorithm as follows (obviously this can |
| 1562 |
-- be done more efficiently, using one of the standard algorithms |
| 1563 |
-- for efficient transitive closure computation, but this is simple |
| 1564 |
-- and most likely fast enough that its speed does not matter). |
| 1565 |
|
| 1566 |
-- Repeatedly scan the list of calls. Any time we find a call from |
| 1567 |
-- A to B, where B has uplevel references, make sure that A is marked |
| 1568 |
-- as having at least the same level of uplevel referencing. |
| 1569 |
|
| 1570 |
Outer2 : loop |
| 1571 |
Modified := False; |
| 1572 |
Inner2 : for J in Calls.First .. Calls.Last loop |
| 1573 |
declare |
| 1574 |
CTJ : Call_Entry renames Calls.Table (J); |
| 1575 |
SINF : constant SI_Type := Subp_Index (CTJ.Caller); |
| 1576 |
SINT : constant SI_Type := Subp_Index (CTJ.Callee); |
| 1577 |
SUBF : Subp_Entry renames Subps.Table (SINF); |
| 1578 |
SUBT : Subp_Entry renames Subps.Table (SINT); |
| 1579 |
begin |
| 1580 |
if SUBT.Lev > SUBT.Uplevel_Ref |
| 1581 |
and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref |
| 1582 |
then |
| 1583 |
SUBF.Uplevel_Ref := SUBT.Uplevel_Ref; |
| 1584 |
Modified := True; |
| 1585 |
end if; |
| 1586 |
end; |
| 1587 |
end loop Inner2; |
| 1588 |
|
| 1589 |
exit Outer2 when not Modified; |
| 1590 |
end loop Outer2; |
| 1591 |
end Closure_Uplevel; |
| 1592 |
|
| 1593 |
-- We have one more step before the tables are complete. An uplevel |
| 1594 |
-- call from subprogram A to subprogram B where subprogram B has uplevel |
| 1595 |
-- references is in effect an uplevel reference, and must arrange for |
| 1596 |
-- the proper activation link to be passed. |
| 1597 |
|
| 1598 |
for J in Calls.First .. Calls.Last loop |
| 1599 |
declare |
| 1600 |
CTJ : Call_Entry renames Calls.Table (J); |
| 1601 |
|
| 1602 |
SINF : constant SI_Type := Subp_Index (CTJ.Caller); |
| 1603 |
SINT : constant SI_Type := Subp_Index (CTJ.Callee); |
| 1604 |
|
| 1605 |
SUBF : Subp_Entry renames Subps.Table (SINF); |
| 1606 |
SUBT : Subp_Entry renames Subps.Table (SINT); |
| 1607 |
|
| 1608 |
A : Entity_Id; |
| 1609 |
|
| 1610 |
begin |
| 1611 |
-- If callee has uplevel references |
| 1612 |
|
| 1613 |
if SUBT.Uplevel_Ref < SUBT.Lev |
| 1614 |
|
| 1615 |
-- And this is an uplevel call |
| 1616 |
|
| 1617 |
and then SUBT.Lev < SUBF.Lev |
| 1618 |
then |
| 1619 |
-- We need to arrange for finding the uplink |
| 1620 |
|
| 1621 |
A := CTJ.Caller; |
| 1622 |
loop |
| 1623 |
A := Enclosing_Subprogram (A); |
| 1624 |
Subps.Table (Subp_Index (A)).Declares_AREC := True; |
| 1625 |
exit when A = CTJ.Callee; |
| 1626 |
|
| 1627 |
-- In any case exit when we get to the outer level. This |
| 1628 |
-- happens in some odd cases with generics (in particular |
| 1629 |
-- sem_ch3.adb does not compile without this kludge ???). |
| 1630 |
|
| 1631 |
exit when A = Subp; |
| 1632 |
end loop; |
| 1633 |
end if; |
| 1634 |
end; |
| 1635 |
end loop; |
| 1636 |
|
| 1637 |
-- The tables are now complete, so we can record the last index in the |
| 1638 |
-- Subps table for later reference in Cprint. |
| 1639 |
|
| 1640 |
Subps.Table (Subps_First).Last := Subps.Last; |
| 1641 |
|
| 1642 |
-- Next step, create the entities for code we will insert. We do this |
| 1643 |
-- at the start so that all the entities are defined, regardless of the |
| 1644 |
-- order in which we do the code insertions. |
| 1645 |
|
| 1646 |
Create_Entities : for J in Subps_First .. Subps.Last loop |
| 1647 |
declare |
| 1648 |
STJ : Subp_Entry renames Subps.Table (J); |
| 1649 |
Loc : constant Source_Ptr := Sloc (STJ.Bod); |
| 1650 |
|
| 1651 |
begin |
| 1652 |
-- First we create the ARECnF entity for the additional formal for |
| 1653 |
-- all subprograms which need an activation record passed. |
| 1654 |
|
| 1655 |
if STJ.Uplevel_Ref < STJ.Lev then |
| 1656 |
STJ.ARECnF := |
| 1657 |
Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F")); |
| 1658 |
end if; |
| 1659 |
|
| 1660 |
-- Define the AREC entities for the activation record if needed |
| 1661 |
|
| 1662 |
if STJ.Declares_AREC then |
| 1663 |
STJ.ARECn := |
| 1664 |
Make_Defining_Identifier (Loc, AREC_Name (J, "")); |
| 1665 |
STJ.ARECnT := |
| 1666 |
Make_Defining_Identifier (Loc, AREC_Name (J, "T")); |
| 1667 |
STJ.ARECnPT := |
| 1668 |
Make_Defining_Identifier (Loc, AREC_Name (J, "PT")); |
| 1669 |
STJ.ARECnP := |
| 1670 |
Make_Defining_Identifier (Loc, AREC_Name (J, "P")); |
| 1671 |
|
| 1672 |
-- Define uplink component entity if inner nesting case |
| 1673 |
|
| 1674 |
if Present (STJ.ARECnF) then |
| 1675 |
STJ.ARECnU := |
| 1676 |
Make_Defining_Identifier (Loc, AREC_Name (J, "U")); |
| 1677 |
end if; |
| 1678 |
end if; |
| 1679 |
end; |
| 1680 |
end loop Create_Entities; |
| 1681 |
|
| 1682 |
-- Loop through subprograms |
| 1683 |
|
| 1684 |
Subp_Loop : declare |
| 1685 |
Addr : Entity_Id := Empty; |
| 1686 |
|
| 1687 |
begin |
| 1688 |
for J in Subps_First .. Subps.Last loop |
| 1689 |
declare |
| 1690 |
STJ : Subp_Entry renames Subps.Table (J); |
| 1691 |
|
| 1692 |
begin |
| 1693 |
-- First add the extra formal if needed. This applies to all |
| 1694 |
-- nested subprograms that require an activation record to be |
| 1695 |
-- passed, as indicated by ARECnF being defined. |
| 1696 |
|
| 1697 |
if Present (STJ.ARECnF) then |
| 1698 |
|
| 1699 |
-- Here we need the extra formal. We do the expansion and |
| 1700 |
-- analysis of this manually, since it is fairly simple, |
| 1701 |
-- and it is not obvious how we can get what we want if we |
| 1702 |
-- try to use the normal Analyze circuit. |
| 1703 |
|
| 1704 |
Add_Extra_Formal : declare |
| 1705 |
Encl : constant SI_Type := Enclosing_Subp (J); |
| 1706 |
STJE : Subp_Entry renames Subps.Table (Encl); |
| 1707 |
-- Index and Subp_Entry for enclosing routine |
| 1708 |
|
| 1709 |
Form : constant Entity_Id := STJ.ARECnF; |
| 1710 |
-- The formal to be added. Note that n here is one less |
| 1711 |
-- than the level of the subprogram itself (STJ.Ent). |
| 1712 |
|
| 1713 |
procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id); |
| 1714 |
-- S is an N_Function/Procedure_Specification node, and F |
| 1715 |
-- is the new entity to add to this subprogram spec as |
| 1716 |
-- the last Extra_Formal. |
| 1717 |
|
| 1718 |
---------------------- |
| 1719 |
-- Add_Form_To_Spec -- |
| 1720 |
---------------------- |
| 1721 |
|
| 1722 |
procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is |
| 1723 |
Sub : constant Entity_Id := Defining_Entity (S); |
| 1724 |
Ent : Entity_Id; |
| 1725 |
|
| 1726 |
begin |
| 1727 |
-- Case of at least one Extra_Formal is present, set |
| 1728 |
-- ARECnF as the new last entry in the list. |
| 1729 |
|
| 1730 |
if Present (Extra_Formals (Sub)) then |
| 1731 |
Ent := Extra_Formals (Sub); |
| 1732 |
while Present (Extra_Formal (Ent)) loop |
| 1733 |
Ent := Extra_Formal (Ent); |
| 1734 |
end loop; |
| 1735 |
|
| 1736 |
Set_Extra_Formal (Ent, F); |
| 1737 |
|
| 1738 |
-- No Extra formals present |
| 1739 |
|
| 1740 |
else |
| 1741 |
Set_Extra_Formals (Sub, F); |
| 1742 |
Ent := Last_Formal (Sub); |
| 1743 |
|
| 1744 |
if Present (Ent) then |
| 1745 |
Set_Extra_Formal (Ent, F); |
| 1746 |
end if; |
| 1747 |
end if; |
| 1748 |
end Add_Form_To_Spec; |
| 1749 |
|
| 1750 |
-- Start of processing for Add_Extra_Formal |
| 1751 |
|
| 1752 |
begin |
| 1753 |
-- Decorate the new formal entity |
| 1754 |
|
| 1755 |
Set_Scope (Form, STJ.Ent); |
| 1756 |
Mutate_Ekind (Form, E_In_Parameter); |
| 1757 |
Set_Etype (Form, STJE.ARECnPT); |
| 1758 |
Set_Mechanism (Form, By_Copy); |
| 1759 |
Set_Never_Set_In_Source (Form, True); |
| 1760 |
Set_Analyzed (Form, True); |
| 1761 |
Set_Comes_From_Source (Form, False); |
| 1762 |
Set_Is_Activation_Record (Form, True); |
| 1763 |
|
| 1764 |
-- Case of only body present |
| 1765 |
|
| 1766 |
if Acts_As_Spec (STJ.Bod) then |
| 1767 |
Add_Form_To_Spec (Form, Specification (STJ.Bod)); |
| 1768 |
|
| 1769 |
-- Case of separate spec |
| 1770 |
|
| 1771 |
else |
| 1772 |
Add_Form_To_Spec (Form, Parent (STJ.Ent)); |
| 1773 |
end if; |
| 1774 |
end Add_Extra_Formal; |
| 1775 |
end if; |
| 1776 |
|
| 1777 |
-- Processing for subprograms that declare an activation record |
| 1778 |
|
| 1779 |
if Present (STJ.ARECn) then |
| 1780 |
|
| 1781 |
-- Local declarations for one such subprogram |
| 1782 |
|
| 1783 |
declare |
| 1784 |
Loc : constant Source_Ptr := Sloc (STJ.Bod); |
| 1785 |
|
| 1786 |
Decls : constant List_Id := New_List; |
| 1787 |
-- List of new declarations we create |
| 1788 |
|
| 1789 |
Clist : List_Id; |
| 1790 |
Comp : Entity_Id; |
| 1791 |
|
| 1792 |
Decl_Assign : Node_Id; |
| 1793 |
-- Assignment to set uplink, Empty if none |
| 1794 |
|
| 1795 |
Decl_ARECnT : Node_Id; |
| 1796 |
Decl_ARECnPT : Node_Id; |
| 1797 |
Decl_ARECn : Node_Id; |
| 1798 |
Decl_ARECnP : Node_Id; |
| 1799 |
-- Declaration nodes for the AREC entities we build |
| 1800 |
|
| 1801 |
begin |
| 1802 |
-- Build list of component declarations for ARECnT and |
| 1803 |
-- load System.Address. |
| 1804 |
|
| 1805 |
Clist := Empty_List; |
| 1806 |
|
| 1807 |
if No (Addr) then |
| 1808 |
Addr := RTE (RE_Address); |
| 1809 |
end if; |
| 1810 |
|
| 1811 |
-- If we are in a subprogram that has a static link that |
| 1812 |
-- is passed in (as indicated by ARECnF being defined), |
| 1813 |
-- then include ARECnU : ARECmPT where ARECmPT comes from |
| 1814 |
-- the level one higher than the current level, and the |
| 1815 |
-- entity ARECnPT comes from the enclosing subprogram. |
| 1816 |
|
| 1817 |
if Present (STJ.ARECnF) then |
| 1818 |
declare |
| 1819 |
STJE : Subp_Entry |
| 1820 |
renames Subps.Table (Enclosing_Subp (J)); |
| 1821 |
begin |
| 1822 |
Append_To (Clist, |
| 1823 |
Make_Component_Declaration (Loc, |
| 1824 |
Defining_Identifier => STJ.ARECnU, |
| 1825 |
Component_Definition => |
| 1826 |
Make_Component_Definition (Loc, |
| 1827 |
Subtype_Indication => |
| 1828 |
New_Occurrence_Of (STJE.ARECnPT, Loc)))); |
| 1829 |
end; |
| 1830 |
end if; |
| 1831 |
|
| 1832 |
-- Add components for uplevel referenced entities |
| 1833 |
|
| 1834 |
if Present (STJ.Uents) then |
| 1835 |
declare |
| 1836 |
Elmt : Elmt_Id; |
| 1837 |
Ptr_Decl : Node_Id; |
| 1838 |
Uent : Entity_Id; |
| 1839 |
|
| 1840 |
Indx : Nat; |
| 1841 |
-- 1's origin of index in list of elements. This is |
| 1842 |
-- used to uniquify names if needed in Upref_Name. |
| 1843 |
|
| 1844 |
begin |
| 1845 |
Elmt := First_Elmt (STJ.Uents); |
| 1846 |
Indx := 0; |
| 1847 |
while Present (Elmt) loop |
| 1848 |
Uent := Node (Elmt); |
| 1849 |
Indx := Indx + 1; |
| 1850 |
|
| 1851 |
Comp := |
| 1852 |
Make_Defining_Identifier (Loc, |
| 1853 |
Chars => Upref_Name (Uent, Indx, Clist)); |
| 1854 |
|
| 1855 |
Set_Activation_Record_Component |
| 1856 |
(Uent, Comp); |
| 1857 |
|
| 1858 |
if Needs_Fat_Pointer (Uent) then |
| 1859 |
|
| 1860 |
-- Build corresponding access type |
| 1861 |
|
| 1862 |
Ptr_Decl := |
| 1863 |
Build_Access_Type_Decl |
| 1864 |
(Etype (Uent), STJ.Ent); |
| 1865 |
Append_To (Decls, Ptr_Decl); |
| 1866 |
|
| 1867 |
-- And use its type in the corresponding |
| 1868 |
-- component. |
| 1869 |
|
| 1870 |
Append_To (Clist, |
| 1871 |
Make_Component_Declaration (Loc, |
| 1872 |
Defining_Identifier => Comp, |
| 1873 |
Component_Definition => |
| 1874 |
Make_Component_Definition (Loc, |
| 1875 |
Subtype_Indication => |
| 1876 |
New_Occurrence_Of |
| 1877 |
(Defining_Identifier (Ptr_Decl), |
| 1878 |
Loc)))); |
| 1879 |
else |
| 1880 |
Append_To (Clist, |
| 1881 |
Make_Component_Declaration (Loc, |
| 1882 |
Defining_Identifier => Comp, |
| 1883 |
Component_Definition => |
| 1884 |
Make_Component_Definition (Loc, |
| 1885 |
Subtype_Indication => |
| 1886 |
New_Occurrence_Of (Addr, Loc)))); |
| 1887 |
end if; |
| 1888 |
Next_Elmt (Elmt); |
| 1889 |
end loop; |
| 1890 |
end; |
| 1891 |
end if; |
| 1892 |
|
| 1893 |
-- Now we can insert the AREC declarations into the body |
| 1894 |
-- type ARECnT is record .. end record; |
| 1895 |
-- pragma Suppress_Initialization (ARECnT); |
| 1896 |
|
| 1897 |
-- Note that we need to set the Suppress_Initialization |
| 1898 |
-- flag after Decl_ARECnT has been analyzed. |
| 1899 |
|
| 1900 |
Decl_ARECnT := |
| 1901 |
Make_Full_Type_Declaration (Loc, |
| 1902 |
Defining_Identifier => STJ.ARECnT, |
| 1903 |
Type_Definition => |
| 1904 |
Make_Record_Definition (Loc, |
| 1905 |
Component_List => |
| 1906 |
Make_Component_List (Loc, |
| 1907 |
Component_Items => Clist))); |
| 1908 |
Append_To (Decls, Decl_ARECnT); |
| 1909 |
|
| 1910 |
-- type ARECnPT is access all ARECnT; |
| 1911 |
|
| 1912 |
Decl_ARECnPT := |
| 1913 |
Make_Full_Type_Declaration (Loc, |
| 1914 |
Defining_Identifier => STJ.ARECnPT, |
| 1915 |
Type_Definition => |
| 1916 |
Make_Access_To_Object_Definition (Loc, |
| 1917 |
All_Present => True, |
| 1918 |
Subtype_Indication => |
| 1919 |
New_Occurrence_Of (STJ.ARECnT, Loc))); |
| 1920 |
Append_To (Decls, Decl_ARECnPT); |
| 1921 |
|
| 1922 |
-- ARECn : aliased ARECnT; |
| 1923 |
|
| 1924 |
Decl_ARECn := |
| 1925 |
Make_Object_Declaration (Loc, |
| 1926 |
Defining_Identifier => STJ.ARECn, |
| 1927 |
Aliased_Present => True, |
| 1928 |
Object_Definition => |
| 1929 |
New_Occurrence_Of (STJ.ARECnT, Loc)); |
| 1930 |
Append_To (Decls, Decl_ARECn); |
| 1931 |
|
| 1932 |
-- ARECnP : constant ARECnPT := ARECn'Access; |
| 1933 |
|
| 1934 |
Decl_ARECnP := |
| 1935 |
Make_Object_Declaration (Loc, |
| 1936 |
Defining_Identifier => STJ.ARECnP, |
| 1937 |
Constant_Present => True, |
| 1938 |
Object_Definition => |
| 1939 |
New_Occurrence_Of (STJ.ARECnPT, Loc), |
| 1940 |
Expression => |
| 1941 |
Make_Attribute_Reference (Loc, |
| 1942 |
Prefix => |
| 1943 |
New_Occurrence_Of (STJ.ARECn, Loc), |
| 1944 |
Attribute_Name => Name_Access)); |
| 1945 |
Append_To (Decls, Decl_ARECnP); |
| 1946 |
|
| 1947 |
-- If we are in a subprogram that has a static link that |
| 1948 |
-- is passed in (as indicated by ARECnF being defined), |
| 1949 |
-- then generate ARECn.ARECmU := ARECmF where m is |
| 1950 |
-- one less than the current level to set the uplink. |
| 1951 |
|
| 1952 |
if Present (STJ.ARECnF) then |
| 1953 |
Decl_Assign := |
| 1954 |
Make_Assignment_Statement (Loc, |
| 1955 |
Name => |
| 1956 |
Make_Selected_Component (Loc, |
| 1957 |
Prefix => |
| 1958 |
New_Occurrence_Of (STJ.ARECn, Loc), |
| 1959 |
Selector_Name => |
| 1960 |
New_Occurrence_Of (STJ.ARECnU, Loc)), |
| 1961 |
Expression => |
| 1962 |
New_Occurrence_Of (STJ.ARECnF, Loc)); |
| 1963 |
Append_To (Decls, Decl_Assign); |
| 1964 |
|
| 1965 |
else |
| 1966 |
Decl_Assign := Empty; |
| 1967 |
end if; |
| 1968 |
|
| 1969 |
if No (Declarations (STJ.Bod)) then |
| 1970 |
Set_Declarations (STJ.Bod, Decls); |
| 1971 |
else |
| 1972 |
Prepend_List_To (Declarations (STJ.Bod), Decls); |
| 1973 |
end if; |
| 1974 |
|
| 1975 |
-- Analyze the newly inserted declarations. Note that we |
| 1976 |
-- do not need to establish the whole scope stack, since |
| 1977 |
-- we have already set all entity fields (so there will |
| 1978 |
-- be no searching of upper scopes to resolve names). But |
| 1979 |
-- we do set the scope of the current subprogram, so that |
| 1980 |
-- newly created entities go in the right entity chain. |
| 1981 |
|
| 1982 |
-- We analyze with all checks suppressed (since we do |
| 1983 |
-- not expect any exceptions). |
| 1984 |
|
| 1985 |
Push_Scope (STJ.Ent); |
| 1986 |
Analyze (Decl_ARECnT, Suppress => All_Checks); |
| 1987 |
|
| 1988 |
-- Note that we need to call Set_Suppress_Initialization |
| 1989 |
-- after Decl_ARECnT has been analyzed, but before |
| 1990 |
-- analyzing Decl_ARECnP so that the flag is properly |
| 1991 |
-- taking into account. |
| 1992 |
|
| 1993 |
Set_Suppress_Initialization (STJ.ARECnT); |
| 1994 |
|
| 1995 |
Analyze (Decl_ARECnPT, Suppress => All_Checks); |
| 1996 |
Analyze (Decl_ARECn, Suppress => All_Checks); |
| 1997 |
Analyze (Decl_ARECnP, Suppress => All_Checks); |
| 1998 |
|
| 1999 |
if Present (Decl_Assign) then |
| 2000 |
Analyze (Decl_Assign, Suppress => All_Checks); |
| 2001 |
end if; |
| 2002 |
|
| 2003 |
Pop_Scope; |
| 2004 |
|
| 2005 |
-- Next step, for each uplevel referenced entity, add |
| 2006 |
-- assignment operations to set the component in the |
| 2007 |
-- activation record. |
| 2008 |
|
| 2009 |
if Present (STJ.Uents) then |
| 2010 |
declare |
| 2011 |
Elmt : Elmt_Id; |
| 2012 |
|
| 2013 |
begin |
| 2014 |
Elmt := First_Elmt (STJ.Uents); |
| 2015 |
while Present (Elmt) loop |
| 2016 |
declare |
| 2017 |
Ent : constant Entity_Id := Node (Elmt); |
| 2018 |
Loc : constant Source_Ptr := Sloc (Ent); |
| 2019 |
Dec : constant Node_Id := |
| 2020 |
Declaration_Node (Ent); |
| 2021 |
|
| 2022 |
Asn : Node_Id; |
| 2023 |
Attr : Name_Id; |
| 2024 |
Comp : Entity_Id; |
| 2025 |
Ins : Node_Id; |
| 2026 |
Rhs : Node_Id; |
| 2027 |
|
| 2028 |
begin |
| 2029 |
-- For parameters, we insert the assignment |
| 2030 |
-- right after the declaration of ARECnP. |
| 2031 |
-- For all other entities, we insert the |
| 2032 |
-- assignment immediately after the |
| 2033 |
-- declaration of the entity or after the |
| 2034 |
-- freeze node if present. |
| 2035 |
|
| 2036 |
-- Note: we don't need to mark the entity |
| 2037 |
-- as being aliased, because the address |
| 2038 |
-- attribute will mark it as Address_Taken, |
| 2039 |
-- and that is good enough. |
| 2040 |
|
| 2041 |
if Is_Formal (Ent) then |
| 2042 |
Ins := Decl_ARECnP; |
| 2043 |
|
| 2044 |
elsif Has_Delayed_Freeze (Ent) then |
| 2045 |
Ins := Freeze_Node (Ent); |
| 2046 |
|
| 2047 |
else |
| 2048 |
Ins := Dec; |
| 2049 |
end if; |
| 2050 |
|
| 2051 |
-- Build and insert the assignment: |
| 2052 |
-- ARECn.nam := nam'Address |
| 2053 |
-- or else 'Unchecked_Access for |
| 2054 |
-- unconstrained array. |
| 2055 |
|
| 2056 |
if Needs_Fat_Pointer (Ent) then |
| 2057 |
Attr := Name_Unchecked_Access; |
| 2058 |
else |
| 2059 |
Attr := Name_Address; |
| 2060 |
end if; |
| 2061 |
|
| 2062 |
Rhs := |
| 2063 |
Make_Attribute_Reference (Loc, |
| 2064 |
Prefix => |
| 2065 |
New_Occurrence_Of (Ent, Loc), |
| 2066 |
Attribute_Name => Attr); |
| 2067 |
|
| 2068 |
-- If the entity is an unconstrained formal |
| 2069 |
-- we wrap the attribute reference in an |
| 2070 |
-- unchecked conversion to the type of the |
| 2071 |
-- activation record component, to prevent |
| 2072 |
-- spurious subtype conformance errors within |
| 2073 |
-- instances. |
| 2074 |
|
| 2075 |
if Is_Formal (Ent) |
| 2076 |
and then not Is_Constrained (Etype (Ent)) |
| 2077 |
then |
| 2078 |
-- Find target component and its type |
| 2079 |
|
| 2080 |
Comp := First_Component (STJ.ARECnT); |
| 2081 |
while Chars (Comp) /= Chars (Ent) loop |
| 2082 |
Next_Component (Comp); |
| 2083 |
end loop; |
| 2084 |
|
| 2085 |
Rhs := |
| 2086 |
Unchecked_Convert_To (Etype (Comp), Rhs); |
| 2087 |
end if; |
| 2088 |
|
| 2089 |
Asn := |
| 2090 |
Make_Assignment_Statement (Loc, |
| 2091 |
Name => |
| 2092 |
Make_Selected_Component (Loc, |
| 2093 |
Prefix => |
| 2094 |
New_Occurrence_Of (STJ.ARECn, Loc), |
| 2095 |
Selector_Name => |
| 2096 |
New_Occurrence_Of |
| 2097 |
(Activation_Record_Component |
| 2098 |
(Ent), |
| 2099 |
Loc)), |
| 2100 |
Expression => Rhs); |
| 2101 |
|
| 2102 |
-- If we have a loop parameter, we have |
| 2103 |
-- to insert before the first statement |
| 2104 |
-- of the loop. Ins points to the |
| 2105 |
-- N_Loop_Parameter_Specification or to |
| 2106 |
-- an N_Iterator_Specification. |
| 2107 |
|
| 2108 |
if Nkind (Ins) in |
| 2109 |
N_Iterator_Specification | |
| 2110 |
N_Loop_Parameter_Specification |
| 2111 |
then |
| 2112 |
-- Quantified expression are rewritten as |
| 2113 |
-- loops during expansion. |
| 2114 |
|
| 2115 |
if Nkind (Parent (Ins)) = |
| 2116 |
N_Quantified_Expression |
| 2117 |
then |
| 2118 |
null; |
| 2119 |
|
| 2120 |
else |
| 2121 |
Ins := |
| 2122 |
First |
| 2123 |
(Statements |
| 2124 |
(Parent (Parent (Ins)))); |
| 2125 |
Insert_Before (Ins, Asn); |
| 2126 |
end if; |
| 2127 |
|
| 2128 |
else |
| 2129 |
Insert_After (Ins, Asn); |
| 2130 |
end if; |
| 2131 |
|
| 2132 |
-- Analyze the assignment statement. We do |
| 2133 |
-- not need to establish the relevant scope |
| 2134 |
-- stack entries here, because we have |
| 2135 |
-- already set the correct entity references, |
| 2136 |
-- so no name resolution is required, and no |
| 2137 |
-- new entities are created, so we don't even |
| 2138 |
-- need to set the current scope. |
| 2139 |
|
| 2140 |
-- We analyze with all checks suppressed |
| 2141 |
-- (since we do not expect any exceptions). |
| 2142 |
|
| 2143 |
Analyze (Asn, Suppress => All_Checks); |
| 2144 |
end; |
| 2145 |
|
| 2146 |
Next_Elmt (Elmt); |
| 2147 |
end loop; |
| 2148 |
end; |
| 2149 |
end if; |
| 2150 |
end; |
| 2151 |
end if; |
| 2152 |
end; |
| 2153 |
end loop; |
| 2154 |
end Subp_Loop; |
| 2155 |
|
| 2156 |
-- Note: we used to process uplevel references, in particular for the |
| 2157 |
-- old CCG (cprint.adb). With GNAT LLVM, processing of uplevel |
| 2158 |
-- references needs to be done directly there which is more reliable, so |
| 2159 |
-- we no longer need to do it here. |
| 2160 |
|
| 2161 |
-- Finally, loop through all calls adding extra actual for the |
| 2162 |
-- activation record where it is required. |
| 2163 |
|
| 2164 |
Adjust_Calls : for J in Calls.First .. Calls.Last loop |
| 2165 |
|
| 2166 |
-- Process a single call, we are only interested in a call to a |
| 2167 |
-- subprogram that actually needs a pointer to an activation record, |
| 2168 |
-- as indicated by the ARECnF entity being set. This excludes the |
| 2169 |
-- top level subprogram, and any subprogram not having uplevel refs. |
| 2170 |
|
| 2171 |
Adjust_One_Call : declare |
| 2172 |
CTJ : Call_Entry renames Calls.Table (J); |
| 2173 |
STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller)); |
| 2174 |
STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee)); |
| 2175 |
|
| 2176 |
Loc : constant Source_Ptr := Sloc (CTJ.N); |
| 2177 |
|
| 2178 |
Extra : Node_Id; |
| 2179 |
ExtraP : Node_Id; |
| 2180 |
SubX : SI_Type; |
| 2181 |
Act : Node_Id; |
| 2182 |
|
| 2183 |
begin |
| 2184 |
if Present (STT.ARECnF) |
| 2185 |
and then Nkind (CTJ.N) in N_Subprogram_Call |
| 2186 |
then |
| 2187 |
-- CTJ.N is a call to a subprogram which may require a pointer |
| 2188 |
-- to an activation record. The subprogram containing the call |
| 2189 |
-- is CTJ.From and the subprogram being called is CTJ.To, so we |
| 2190 |
-- have a call from level STF.Lev to level STT.Lev. |
| 2191 |
|
| 2192 |
-- There are three possibilities: |
| 2193 |
|
| 2194 |
-- For a call to the same level, we just pass the activation |
| 2195 |
-- record passed to the calling subprogram. |
| 2196 |
|
| 2197 |
if STF.Lev = STT.Lev then |
| 2198 |
Extra := New_Occurrence_Of (STF.ARECnF, Loc); |
| 2199 |
|
| 2200 |
-- For a call that goes down a level, we pass a pointer to the |
| 2201 |
-- activation record constructed within the caller (which may |
| 2202 |
-- be the outer-level subprogram, but also may be a more deeply |
| 2203 |
-- nested caller). |
| 2204 |
|
| 2205 |
elsif STT.Lev = STF.Lev + 1 then |
| 2206 |
Extra := New_Occurrence_Of (STF.ARECnP, Loc); |
| 2207 |
|
| 2208 |
-- Otherwise we must have an upcall (STT.Lev < STF.LEV), |
| 2209 |
-- since it is not possible to do a downcall of more than |
| 2210 |
-- one level. |
| 2211 |
|
| 2212 |
-- For a call from level STF.Lev to level STT.Lev, we |
| 2213 |
-- have to find the activation record needed by the |
| 2214 |
-- callee. This is as follows: |
| 2215 |
|
| 2216 |
-- ARECaF.ARECbU.ARECcU....ARECmU |
| 2217 |
|
| 2218 |
-- where a,b,c .. m = |
| 2219 |
-- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev |
| 2220 |
|
| 2221 |
else |
| 2222 |
pragma Assert (STT.Lev < STF.Lev); |
| 2223 |
|
| 2224 |
Extra := New_Occurrence_Of (STF.ARECnF, Loc); |
| 2225 |
SubX := Subp_Index (CTJ.Caller); |
| 2226 |
for K in reverse STT.Lev .. STF.Lev - 1 loop |
| 2227 |
SubX := Enclosing_Subp (SubX); |
| 2228 |
Extra := |
| 2229 |
Make_Selected_Component (Loc, |
| 2230 |
Prefix => Extra, |
| 2231 |
Selector_Name => |
| 2232 |
New_Occurrence_Of |
| 2233 |
(Subps.Table (SubX).ARECnU, Loc)); |
| 2234 |
end loop; |
| 2235 |
end if; |
| 2236 |
|
| 2237 |
-- Extra is the additional parameter to be added. Build a |
| 2238 |
-- parameter association that we can append to the actuals. |
| 2239 |
|
| 2240 |
ExtraP := |
| 2241 |
Make_Parameter_Association (Loc, |
| 2242 |
Selector_Name => |
| 2243 |
New_Occurrence_Of (STT.ARECnF, Loc), |
| 2244 |
Explicit_Actual_Parameter => Extra); |
| 2245 |
|
| 2246 |
if No (Parameter_Associations (CTJ.N)) then |
| 2247 |
Set_Parameter_Associations (CTJ.N, Empty_List); |
| 2248 |
end if; |
| 2249 |
|
| 2250 |
Append (ExtraP, Parameter_Associations (CTJ.N)); |
| 2251 |
|
| 2252 |
-- We need to deal with the actual parameter chain as well. The |
| 2253 |
-- newly added parameter is always the last actual. |
| 2254 |
|
| 2255 |
Act := First_Named_Actual (CTJ.N); |
| 2256 |
|
| 2257 |
if No (Act) then |
| 2258 |
Set_First_Named_Actual (CTJ.N, Extra); |
| 2259 |
|
| 2260 |
-- If call has been relocated (as with an expression in |
| 2261 |
-- an aggregate), set First_Named pointer in original node |
| 2262 |
-- as well, because that's the parent of the parameter list. |
| 2263 |
|
| 2264 |
Set_First_Named_Actual |
| 2265 |
(Parent (List_Containing (ExtraP)), Extra); |
| 2266 |
|
| 2267 |
-- Here we must follow the chain and append the new entry |
| 2268 |
|
| 2269 |
else |
| 2270 |
loop |
| 2271 |
declare |
| 2272 |
PAN : Node_Id; |
| 2273 |
NNA : Node_Id; |
| 2274 |
|
| 2275 |
begin |
| 2276 |
PAN := Parent (Act); |
| 2277 |
pragma Assert (Nkind (PAN) = N_Parameter_Association); |
| 2278 |
NNA := Next_Named_Actual (PAN); |
| 2279 |
|
| 2280 |
if No (NNA) then |
| 2281 |
Set_Next_Named_Actual (PAN, Extra); |
| 2282 |
exit; |
| 2283 |
end if; |
| 2284 |
|
| 2285 |
Act := NNA; |
| 2286 |
end; |
| 2287 |
end loop; |
| 2288 |
end if; |
| 2289 |
|
| 2290 |
-- Analyze and resolve the new actual. We do not need to |
| 2291 |
-- establish the relevant scope stack entries here, because |
| 2292 |
-- we have already set all the correct entity references, so |
| 2293 |
-- no name resolution is needed. |
| 2294 |
|
| 2295 |
-- We analyze with all checks suppressed (since we do not |
| 2296 |
-- expect any exceptions, and also we temporarily turn off |
| 2297 |
-- Unested_Subprogram_Mode to avoid trying to mark uplevel |
| 2298 |
-- references (not needed at this stage, and in fact causes |
| 2299 |
-- a bit of recursive chaos). |
| 2300 |
|
| 2301 |
Opt.Unnest_Subprogram_Mode := False; |
| 2302 |
Analyze_And_Resolve |
| 2303 |
(Extra, Etype (STT.ARECnF), Suppress => All_Checks); |
| 2304 |
Opt.Unnest_Subprogram_Mode := True; |
| 2305 |
end if; |
| 2306 |
end Adjust_One_Call; |
| 2307 |
end loop Adjust_Calls; |
| 2308 |
|
| 2309 |
return; |
| 2310 |
end Unnest_Subprogram; |
| 2311 |
|
| 2312 |
------------------------ |
| 2313 |
-- Unnest_Subprograms -- |
| 2314 |
------------------------ |
| 2315 |
|
| 2316 |
procedure Unnest_Subprograms (N : Node_Id) is |
| 2317 |
function Search_Subprograms (N : Node_Id) return Traverse_Result; |
| 2318 |
-- Tree visitor that search for outer level procedures with nested |
| 2319 |
-- subprograms and invokes Unnest_Subprogram() |
| 2320 |
|
| 2321 |
--------------- |
| 2322 |
-- Do_Search -- |
| 2323 |
--------------- |
| 2324 |
|
| 2325 |
procedure Do_Search is new Traverse_Proc (Search_Subprograms); |
| 2326 |
-- Subtree visitor instantiation |
| 2327 |
|
| 2328 |
------------------------ |
| 2329 |
-- Search_Subprograms -- |
| 2330 |
------------------------ |
| 2331 |
|
| 2332 |
function Search_Subprograms (N : Node_Id) return Traverse_Result is |
| 2333 |
begin |
| 2334 |
if Nkind (N) in N_Subprogram_Body | N_Subprogram_Body_Stub then |
| 2335 |
declare |
| 2336 |
Spec_Id : constant Entity_Id := Unique_Defining_Entity (N); |
| 2337 |
|
| 2338 |
begin |
| 2339 |
-- We are only interested in subprograms (not generic |
| 2340 |
-- subprograms), that have nested subprograms. |
| 2341 |
|
| 2342 |
if Is_Subprogram (Spec_Id) |
| 2343 |
and then Has_Nested_Subprogram (Spec_Id) |
| 2344 |
and then Is_Library_Level_Entity (Spec_Id) |
| 2345 |
then |
| 2346 |
Unnest_Subprogram (Spec_Id, N); |
| 2347 |
else |
| 2348 |
return Skip; |
| 2349 |
end if; |
| 2350 |
end; |
| 2351 |
|
| 2352 |
-- The proper body of a stub may contain nested subprograms, and |
| 2353 |
-- therefore must be visited explicitly. Nested stubs are examined |
| 2354 |
-- recursively in Visit_Node. |
| 2355 |
|
| 2356 |
elsif Nkind (N) in N_Body_Stub then |
| 2357 |
Do_Search (Stub_Subunit (N)); |
| 2358 |
|
| 2359 |
-- Skip generic packages |
| 2360 |
|
| 2361 |
elsif Nkind (N) = N_Package_Body |
| 2362 |
and then Ekind (Corresponding_Spec (N)) = E_Generic_Package |
| 2363 |
then |
| 2364 |
return Skip; |
| 2365 |
end if; |
| 2366 |
|
| 2367 |
return OK; |
| 2368 |
end Search_Subprograms; |
| 2369 |
|
| 2370 |
Subp : Entity_Id; |
| 2371 |
Subp_Body : Node_Id; |
| 2372 |
|
| 2373 |
-- Start of processing for Unnest_Subprograms |
| 2374 |
|
| 2375 |
begin |
| 2376 |
if not Opt.Unnest_Subprogram_Mode or not Opt.Expander_Active then |
| 2377 |
return; |
| 2378 |
end if; |
| 2379 |
|
| 2380 |
-- A specification will contain bodies if it contains instantiations so |
| 2381 |
-- examine package or subprogram declaration of the main unit, when it |
| 2382 |
-- is present. |
| 2383 |
|
| 2384 |
if Nkind (Unit (N)) = N_Package_Body |
| 2385 |
or else (Nkind (Unit (N)) = N_Subprogram_Body |
| 2386 |
and then not Acts_As_Spec (N)) |
| 2387 |
then |
| 2388 |
Do_Search (Spec_Lib_Unit (N)); |
| 2389 |
end if; |
| 2390 |
|
| 2391 |
Do_Search (N); |
| 2392 |
|
| 2393 |
-- Unnest any subprograms passed on the list of inlined subprograms |
| 2394 |
|
| 2395 |
Subp := First_Inlined_Subprogram (N); |
| 2396 |
|
| 2397 |
while Present (Subp) loop |
| 2398 |
Subp_Body := Parent (Declaration_Node (Subp)); |
| 2399 |
|
| 2400 |
if Nkind (Subp_Body) = N_Subprogram_Declaration |
| 2401 |
and then Present (Corresponding_Body (Subp_Body)) |
| 2402 |
then |
| 2403 |
Subp_Body := Parent (Declaration_Node |
| 2404 |
(Corresponding_Body (Subp_Body))); |
| 2405 |
end if; |
| 2406 |
|
| 2407 |
Unnest_Subprogram (Subp, Subp_Body, For_Inline => True); |
| 2408 |
Next_Inlined_Subprogram (Subp); |
| 2409 |
end loop; |
| 2410 |
end Unnest_Subprograms; |
| 2411 |
|
| 2412 |
end Exp_Unst; |