| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- E X P _ C H 1 3 -- |
| 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 Atree; use Atree; |
| 27 |
with Checks; use Checks; |
| 28 |
with Einfo; use Einfo; |
| 29 |
with Einfo.Entities; use Einfo.Entities; |
| 30 |
with Einfo.Utils; use Einfo.Utils; |
| 31 |
with Exp_Ch3; use Exp_Ch3; |
| 32 |
with Exp_Ch6; |
| 33 |
with Exp_Imgv; use Exp_Imgv; |
| 34 |
with Exp_Tss; use Exp_Tss; |
| 35 |
with Exp_Util; use Exp_Util; |
| 36 |
with Freeze; use Freeze; |
| 37 |
with Namet; use Namet; |
| 38 |
with Nlists; use Nlists; |
| 39 |
with Nmake; use Nmake; |
| 40 |
with Opt; use Opt; |
| 41 |
with Restrict; use Restrict; |
| 42 |
with Rident; use Rident; |
| 43 |
with Rtsfind; use Rtsfind; |
| 44 |
with Sem; use Sem; |
| 45 |
with Sem_Aux; use Sem_Aux; |
| 46 |
with Sem_Ch7; use Sem_Ch7; |
| 47 |
with Sem_Ch8; use Sem_Ch8; |
| 48 |
with Sem_Eval; use Sem_Eval; |
| 49 |
with Sem_Util; use Sem_Util; |
| 50 |
with Sinfo; use Sinfo; |
| 51 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 52 |
with Sinfo.Utils; use Sinfo.Utils; |
| 53 |
with Snames; use Snames; |
| 54 |
with Tbuild; use Tbuild; |
| 55 |
with Uintp; use Uintp; |
| 56 |
with Validsw; use Validsw; |
| 57 |
|
| 58 |
package body Exp_Ch13 is |
| 59 |
|
| 60 |
------------------------------------------ |
| 61 |
-- Expand_N_Attribute_Definition_Clause -- |
| 62 |
------------------------------------------ |
| 63 |
|
| 64 |
-- Expansion action depends on attribute involved |
| 65 |
|
| 66 |
procedure Expand_N_Attribute_Definition_Clause (N : Node_Id) is |
| 67 |
Loc : constant Source_Ptr := Sloc (N); |
| 68 |
Exp : constant Node_Id := Expression (N); |
| 69 |
Ent : Entity_Id; |
| 70 |
V : Node_Id; |
| 71 |
|
| 72 |
begin |
| 73 |
Ent := Entity (Name (N)); |
| 74 |
|
| 75 |
if Is_Type (Ent) then |
| 76 |
Ent := Underlying_Type (Ent); |
| 77 |
end if; |
| 78 |
|
| 79 |
case Get_Attribute_Id (Chars (N)) is |
| 80 |
|
| 81 |
------------- |
| 82 |
-- Address -- |
| 83 |
------------- |
| 84 |
|
| 85 |
when Attribute_Address => |
| 86 |
|
| 87 |
-- If there is an initialization which did not come from the |
| 88 |
-- source program, then it is an artifact of our expansion, and we |
| 89 |
-- suppress it. The case we are most concerned about here is the |
| 90 |
-- initialization of a packed array to all false, which seems |
| 91 |
-- inappropriate for variable to which an address clause is |
| 92 |
-- applied. The expression may itself have been rewritten if the |
| 93 |
-- type is packed array, so we need to examine whether the |
| 94 |
-- original node is in the source. An exception though is the case |
| 95 |
-- of an access variable which is default initialized to null, and |
| 96 |
-- such initialization is retained. |
| 97 |
|
| 98 |
-- Furthermore, if the initialization is the equivalent aggregate |
| 99 |
-- of the type initialization procedure, it replaces an implicit |
| 100 |
-- call to the init proc, and must be respected. Note that for |
| 101 |
-- packed types we do not build equivalent aggregates. |
| 102 |
|
| 103 |
-- Also, if Init_Or_Norm_Scalars applies, then we need to retain |
| 104 |
-- any default initialization for objects of scalar types and |
| 105 |
-- types with scalar components. Normally a composite type will |
| 106 |
-- have an init_proc in the presence of Init_Or_Norm_Scalars, |
| 107 |
-- so when that flag is set we have just have to do a test for |
| 108 |
-- scalar and string types (the predefined string types such as |
| 109 |
-- String and Wide_String don't have an init_proc). |
| 110 |
|
| 111 |
declare |
| 112 |
Decl : constant Node_Id := Declaration_Node (Ent); |
| 113 |
Typ : constant Entity_Id := Etype (Ent); |
| 114 |
|
| 115 |
begin |
| 116 |
if Nkind (Decl) = N_Object_Declaration |
| 117 |
and then Present (Expression (Decl)) |
| 118 |
and then Nkind (Expression (Decl)) /= N_Null |
| 119 |
and then |
| 120 |
not Comes_From_Source (Original_Node (Expression (Decl))) |
| 121 |
then |
| 122 |
if Present (Base_Init_Proc (Typ)) |
| 123 |
and then |
| 124 |
Present (Static_Initialization (Base_Init_Proc (Typ))) |
| 125 |
then |
| 126 |
null; |
| 127 |
|
| 128 |
elsif Init_Or_Norm_Scalars |
| 129 |
and then (Is_Scalar_Type (Typ) |
| 130 |
or else Is_String_Type (Typ)) |
| 131 |
then |
| 132 |
null; |
| 133 |
|
| 134 |
else |
| 135 |
Set_Expression (Decl, Empty); |
| 136 |
end if; |
| 137 |
|
| 138 |
-- An object declaration to which an address clause applies |
| 139 |
-- has a delayed freeze, but the address expression itself |
| 140 |
-- must be elaborated at the point it appears. If the object |
| 141 |
-- is controlled, additional checks apply elsewhere. |
| 142 |
-- If the attribute comes from an aspect specification it |
| 143 |
-- is being elaborated at the freeze point and side effects |
| 144 |
-- need not be removed (and shouldn't, if the expression |
| 145 |
-- depends on other entities that have delayed freeze). |
| 146 |
-- This is another consequence of the delayed analysis of |
| 147 |
-- aspects, and a real semantic difference. |
| 148 |
|
| 149 |
elsif Nkind (Decl) = N_Object_Declaration |
| 150 |
and then not Needs_Constant_Address (Decl, Typ) |
| 151 |
and then not From_Aspect_Specification (N) |
| 152 |
then |
| 153 |
Remove_Side_Effects (Exp); |
| 154 |
end if; |
| 155 |
end; |
| 156 |
|
| 157 |
--------------- |
| 158 |
-- Alignment -- |
| 159 |
--------------- |
| 160 |
|
| 161 |
when Attribute_Alignment => |
| 162 |
|
| 163 |
-- As required by Gigi, we guarantee that the operand is an |
| 164 |
-- integer literal (this simplifies things in Gigi). |
| 165 |
|
| 166 |
if Nkind (Exp) /= N_Integer_Literal then |
| 167 |
Rewrite (Exp, Make_Integer_Literal (Loc, Expr_Value (Exp))); |
| 168 |
end if; |
| 169 |
|
| 170 |
-- A complex case arises if the alignment clause applies to an |
| 171 |
-- unconstrained object initialized with a function call. The |
| 172 |
-- result of the call is placed on the secondary stack, and the |
| 173 |
-- declaration is rewritten as a renaming of a dereference, which |
| 174 |
-- fails expansion. We must introduce a temporary and assign its |
| 175 |
-- value to the existing entity. |
| 176 |
|
| 177 |
if Nkind (Parent (Ent)) = N_Object_Renaming_Declaration |
| 178 |
and then not Is_Entity_Name (Renamed_Object (Ent)) |
| 179 |
then |
| 180 |
declare |
| 181 |
Decl : constant Node_Id := Parent (Ent); |
| 182 |
Loc : constant Source_Ptr := Sloc (N); |
| 183 |
Temp : constant Entity_Id := Make_Temporary (Loc, 'T'); |
| 184 |
|
| 185 |
New_Decl : Node_Id; |
| 186 |
|
| 187 |
begin |
| 188 |
-- Replace entity with temporary and reanalyze |
| 189 |
|
| 190 |
Set_Defining_Identifier (Decl, Temp); |
| 191 |
Set_Analyzed (Decl, False); |
| 192 |
Analyze (Decl); |
| 193 |
|
| 194 |
-- Introduce new declaration for entity but do not reanalyze |
| 195 |
-- because entity is already in scope. Type and expression |
| 196 |
-- are already resolved. |
| 197 |
|
| 198 |
New_Decl := |
| 199 |
Make_Object_Declaration (Loc, |
| 200 |
Defining_Identifier => Ent, |
| 201 |
Object_Definition => |
| 202 |
New_Occurrence_Of (Etype (Ent), Loc), |
| 203 |
Expression => New_Occurrence_Of (Temp, Loc)); |
| 204 |
|
| 205 |
Set_Renamed_Object (Ent, Empty); |
| 206 |
Insert_After (Decl, New_Decl); |
| 207 |
Set_Analyzed (Decl); |
| 208 |
end; |
| 209 |
end if; |
| 210 |
|
| 211 |
------------------ |
| 212 |
-- Storage_Size -- |
| 213 |
------------------ |
| 214 |
|
| 215 |
when Attribute_Storage_Size => |
| 216 |
|
| 217 |
-- If the type is a task type, then assign the value of the |
| 218 |
-- storage size to the Size variable associated with the task. |
| 219 |
-- Insert the assignment right after the declaration of the Size |
| 220 |
-- variable. |
| 221 |
|
| 222 |
-- Generate: |
| 223 |
|
| 224 |
-- task_typeZ := expression |
| 225 |
|
| 226 |
if Ekind (Ent) = E_Task_Type then |
| 227 |
|
| 228 |
declare |
| 229 |
Assign : Node_Id; |
| 230 |
begin |
| 231 |
Assign := |
| 232 |
Make_Assignment_Statement (Loc, |
| 233 |
Name => |
| 234 |
New_Occurrence_Of (Storage_Size_Variable (Ent), Loc), |
| 235 |
Expression => |
| 236 |
Convert_To (RTE (RE_Size_Type), Expression (N))); |
| 237 |
|
| 238 |
-- If the clause is not generated by an aspect, insert |
| 239 |
-- the assignment here. Freezing rules ensure that this |
| 240 |
-- is safe, or clause will have been rejected already. |
| 241 |
|
| 242 |
if Is_List_Member (N) then |
| 243 |
Insert_After (N, Assign); |
| 244 |
|
| 245 |
-- Otherwise, insert assignment after task declaration. |
| 246 |
|
| 247 |
else |
| 248 |
Insert_After |
| 249 |
(Parent (Storage_Size_Variable (Entity (N))), Assign); |
| 250 |
end if; |
| 251 |
|
| 252 |
Analyze (Assign); |
| 253 |
end; |
| 254 |
|
| 255 |
-- For Storage_Size for an access type, create a variable to hold |
| 256 |
-- the value of the specified size with name typeV and expand an |
| 257 |
-- assignment statement to initialize this value. |
| 258 |
|
| 259 |
elsif Is_Access_Type (Ent) then |
| 260 |
|
| 261 |
-- We don't need the variable for a storage size of zero |
| 262 |
|
| 263 |
if not No_Pool_Assigned (Ent) then |
| 264 |
V := |
| 265 |
Make_Defining_Identifier (Loc, |
| 266 |
Chars => New_External_Name (Chars (Ent), 'V')); |
| 267 |
|
| 268 |
-- Insert the declaration of the object. If the expression |
| 269 |
-- is not static it may depend on some other type that is |
| 270 |
-- not frozen yet, so attach the declaration that captures |
| 271 |
-- the value of the expression to the actions of the freeze |
| 272 |
-- node of the current type. |
| 273 |
|
| 274 |
declare |
| 275 |
Decl : constant Node_Id := |
| 276 |
Make_Object_Declaration (Loc, |
| 277 |
Defining_Identifier => V, |
| 278 |
Object_Definition => |
| 279 |
New_Occurrence_Of (RTE (RE_Storage_Offset), Loc), |
| 280 |
Expression => |
| 281 |
Convert_To |
| 282 |
(RTE (RE_Storage_Offset), Expression (N))); |
| 283 |
begin |
| 284 |
if not Is_OK_Static_Expression (Expression (N)) |
| 285 |
and then Present (Freeze_Node (Ent)) |
| 286 |
then |
| 287 |
if No (Actions (Freeze_Node (Ent))) then |
| 288 |
Set_Actions (Freeze_Node (Ent), New_List (Decl)); |
| 289 |
else |
| 290 |
Append (Decl, Actions (Freeze_Node (Ent))); |
| 291 |
end if; |
| 292 |
|
| 293 |
else |
| 294 |
Insert_Action (N, Decl); |
| 295 |
end if; |
| 296 |
end; |
| 297 |
|
| 298 |
Set_Storage_Size_Variable (Ent, Entity_Id (V)); |
| 299 |
end if; |
| 300 |
end if; |
| 301 |
|
| 302 |
-- Other attributes require no expansion |
| 303 |
|
| 304 |
when others => |
| 305 |
null; |
| 306 |
end case; |
| 307 |
end Expand_N_Attribute_Definition_Clause; |
| 308 |
|
| 309 |
----------------------------- |
| 310 |
-- Expand_N_Free_Statement -- |
| 311 |
----------------------------- |
| 312 |
|
| 313 |
procedure Expand_N_Free_Statement (N : Node_Id) is |
| 314 |
Expr : constant Node_Id := Expression (N); |
| 315 |
Typ : Entity_Id; |
| 316 |
|
| 317 |
begin |
| 318 |
-- Certain run-time configurations and targets do not provide support |
| 319 |
-- for controlled types. |
| 320 |
|
| 321 |
if Restriction_Active (No_Finalization) then |
| 322 |
return; |
| 323 |
end if; |
| 324 |
|
| 325 |
-- Use the base type to perform the check for finalization collection |
| 326 |
|
| 327 |
Typ := Etype (Expr); |
| 328 |
|
| 329 |
if Ekind (Typ) = E_Access_Subtype then |
| 330 |
Typ := Etype (Typ); |
| 331 |
end if; |
| 332 |
|
| 333 |
-- Handle private access types |
| 334 |
|
| 335 |
if Is_Private_Type (Typ) |
| 336 |
and then Present (Full_View (Typ)) |
| 337 |
then |
| 338 |
Typ := Full_View (Typ); |
| 339 |
end if; |
| 340 |
|
| 341 |
-- Do not create a custom Deallocate when the object has not been |
| 342 |
-- attached to a collection, since it does not need to be detached. |
| 343 |
-- Use a regular free statement instead. |
| 344 |
|
| 345 |
if No (Finalization_Collection (Typ)) then |
| 346 |
return; |
| 347 |
end if; |
| 348 |
|
| 349 |
-- Use a temporary to store the result of a complex expression. Perform |
| 350 |
-- the following transformation: |
| 351 |
-- |
| 352 |
-- Free (Complex_Expression); |
| 353 |
-- |
| 354 |
-- Temp : constant Type_Of_Expression := Complex_Expression; |
| 355 |
-- Free (Temp); |
| 356 |
|
| 357 |
if Nkind (Expr) /= N_Identifier then |
| 358 |
declare |
| 359 |
Expr_Typ : constant Entity_Id := Etype (Expr); |
| 360 |
Loc : constant Source_Ptr := Sloc (N); |
| 361 |
Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T'); |
| 362 |
|
| 363 |
begin |
| 364 |
Insert_Action (N, |
| 365 |
Make_Object_Declaration (Loc, |
| 366 |
Defining_Identifier => Temp_Id, |
| 367 |
Object_Definition => New_Occurrence_Of (Expr_Typ, Loc), |
| 368 |
Expression => Relocate_Node (Expr))); |
| 369 |
|
| 370 |
Set_Expression (N, New_Occurrence_Of (Temp_Id, Loc)); |
| 371 |
end; |
| 372 |
end if; |
| 373 |
|
| 374 |
-- Create a custom Deallocate for a controlled object. This routine |
| 375 |
-- ensures that the hidden list header will be deallocated along with |
| 376 |
-- the actual object. |
| 377 |
|
| 378 |
Build_Allocate_Deallocate_Proc (N); |
| 379 |
end Expand_N_Free_Statement; |
| 380 |
|
| 381 |
---------------------------- |
| 382 |
-- Expand_N_Freeze_Entity -- |
| 383 |
---------------------------- |
| 384 |
|
| 385 |
procedure Expand_N_Freeze_Entity (N : Node_Id) is |
| 386 |
E : constant Entity_Id := Entity (N); |
| 387 |
|
| 388 |
Decl : Node_Id; |
| 389 |
Delete : Boolean := False; |
| 390 |
E_Scope : Entity_Id; |
| 391 |
In_Other_Scope : Boolean; |
| 392 |
In_Outer_Scope : Boolean; |
| 393 |
|
| 394 |
begin |
| 395 |
-- If there are delayed aspect specifications, we insert them just |
| 396 |
-- before the freeze node. They are already analyzed so we don't need |
| 397 |
-- to reanalyze them (they were analyzed before the type was frozen), |
| 398 |
-- but we want them in the tree for the back end, and so that the |
| 399 |
-- listing from sprint is clearer on where these occur logically. |
| 400 |
|
| 401 |
if Has_Delayed_Aspects (E) then |
| 402 |
declare |
| 403 |
Aitem : Node_Id; |
| 404 |
Ritem : Node_Id; |
| 405 |
|
| 406 |
begin |
| 407 |
-- Look for aspect specs for this entity |
| 408 |
|
| 409 |
Ritem := First_Rep_Item (E); |
| 410 |
while Present (Ritem) loop |
| 411 |
if Nkind (Ritem) = N_Aspect_Specification |
| 412 |
and then Entity (Ritem) = E |
| 413 |
then |
| 414 |
Aitem := Aspect_Rep_Item (Ritem); |
| 415 |
|
| 416 |
-- Skip this for aspects (e.g. Current_Value) for which |
| 417 |
-- there is no corresponding pragma or attribute. |
| 418 |
|
| 419 |
if Present (Aitem) |
| 420 |
|
| 421 |
-- Also skip if we have a null statement rather than a |
| 422 |
-- delayed aspect (this happens when we are ignoring rep |
| 423 |
-- items from use of the -gnatI switch). |
| 424 |
|
| 425 |
and then Nkind (Aitem) /= N_Null_Statement |
| 426 |
then |
| 427 |
pragma Assert (Is_Delayed_Aspect (Aitem)); |
| 428 |
Insert_Before (N, Aitem); |
| 429 |
end if; |
| 430 |
end if; |
| 431 |
|
| 432 |
Next_Rep_Item (Ritem); |
| 433 |
end loop; |
| 434 |
end; |
| 435 |
end if; |
| 436 |
|
| 437 |
-- Processing for objects |
| 438 |
|
| 439 |
if Is_Object (E) then |
| 440 |
if Present (Address_Clause (E)) then |
| 441 |
Apply_Address_Clause_Check (E, N); |
| 442 |
end if; |
| 443 |
|
| 444 |
-- Analyze actions in freeze node, if any |
| 445 |
|
| 446 |
if Present (Actions (N)) then |
| 447 |
declare |
| 448 |
Act : Node_Id; |
| 449 |
begin |
| 450 |
Act := First (Actions (N)); |
| 451 |
while Present (Act) loop |
| 452 |
Analyze (Act); |
| 453 |
Next (Act); |
| 454 |
end loop; |
| 455 |
end; |
| 456 |
end if; |
| 457 |
|
| 458 |
-- If initialization statements have been captured in a compound |
| 459 |
-- statement, insert them back into the tree now. |
| 460 |
|
| 461 |
Explode_Initialization_Compound_Statement (E); |
| 462 |
return; |
| 463 |
|
| 464 |
-- Only other items requiring any front end action are types and |
| 465 |
-- subprograms. |
| 466 |
|
| 467 |
elsif not Is_Type (E) and then not Is_Subprogram (E) then |
| 468 |
return; |
| 469 |
end if; |
| 470 |
|
| 471 |
-- Here E is a type or a subprogram |
| 472 |
|
| 473 |
E_Scope := Scope (E); |
| 474 |
|
| 475 |
-- This is an error protection against previous errors |
| 476 |
|
| 477 |
if No (E_Scope) then |
| 478 |
Check_Error_Detected; |
| 479 |
return; |
| 480 |
end if; |
| 481 |
|
| 482 |
-- The entity may be a subtype declared for a constrained record |
| 483 |
-- component, in which case the relevant scope is the scope of |
| 484 |
-- the record. This happens for class-wide subtypes created for |
| 485 |
-- a constrained type extension with inherited discriminants. |
| 486 |
|
| 487 |
if Is_Type (E_Scope) |
| 488 |
and then not Is_Concurrent_Type (E_Scope) |
| 489 |
then |
| 490 |
E_Scope := Scope (E_Scope); |
| 491 |
|
| 492 |
-- The entity may be a subtype declared for an iterator |
| 493 |
|
| 494 |
elsif Ekind (E_Scope) = E_Loop then |
| 495 |
E_Scope := Scope (E_Scope); |
| 496 |
end if; |
| 497 |
|
| 498 |
-- Remember that we are processing a freezing entity and its freezing |
| 499 |
-- nodes. This flag (non-zero = set) is used to avoid the need of |
| 500 |
-- climbing through the tree while processing the freezing actions (ie. |
| 501 |
-- to avoid generating spurious warnings or to avoid killing constant |
| 502 |
-- indications while processing the code associated with freezing |
| 503 |
-- actions). We use a counter to deal with nesting. |
| 504 |
|
| 505 |
Inside_Freezing_Actions := Inside_Freezing_Actions + 1; |
| 506 |
|
| 507 |
-- If we are freezing entities defined in protected types, they belong |
| 508 |
-- in the enclosing scope, given that the original type has been |
| 509 |
-- expanded away. The same is true for entities in task types, in |
| 510 |
-- particular the parameter records of entries (Entities in bodies are |
| 511 |
-- all frozen within the body). If we are in the task body, this is a |
| 512 |
-- proper scope. If we are within a subprogram body, the proper scope |
| 513 |
-- is the corresponding spec. This may happen for itypes generated in |
| 514 |
-- the bodies of protected operations. |
| 515 |
|
| 516 |
if Ekind (E_Scope) = E_Protected_Type |
| 517 |
or else (Ekind (E_Scope) = E_Task_Type |
| 518 |
and then not Has_Completion (E_Scope)) |
| 519 |
then |
| 520 |
E_Scope := Scope (E_Scope); |
| 521 |
|
| 522 |
elsif Ekind (E_Scope) = E_Subprogram_Body then |
| 523 |
E_Scope := Corresponding_Spec (Unit_Declaration_Node (E_Scope)); |
| 524 |
end if; |
| 525 |
|
| 526 |
-- If the scope of the entity is in open scopes, it is the current one |
| 527 |
-- or an enclosing one, including a loop, a block, or a subprogram. |
| 528 |
|
| 529 |
if In_Open_Scopes (E_Scope) then |
| 530 |
In_Other_Scope := False; |
| 531 |
In_Outer_Scope := E_Scope /= Current_Scope; |
| 532 |
|
| 533 |
-- Otherwise it is a local package or a different compilation unit |
| 534 |
|
| 535 |
else |
| 536 |
In_Other_Scope := True; |
| 537 |
In_Outer_Scope := False; |
| 538 |
end if; |
| 539 |
|
| 540 |
-- If the entity being frozen is defined in a scope that is not |
| 541 |
-- currently on the scope stack, we must establish the proper |
| 542 |
-- visibility before freezing the entity and related subprograms. |
| 543 |
|
| 544 |
if In_Other_Scope then |
| 545 |
Push_Scope (E_Scope); |
| 546 |
|
| 547 |
-- Finalizers are little odd in terms of freezing. The spec of the |
| 548 |
-- procedure appears in the declarations while the body appears in |
| 549 |
-- the statement part of a single construct. Since the finalizer must |
| 550 |
-- be called by the At_End handler of the construct, the spec is |
| 551 |
-- manually frozen right after its declaration. The only side effect |
| 552 |
-- of this action appears in contexts where the construct is not in |
| 553 |
-- its final resting place. These contexts are: |
| 554 |
|
| 555 |
-- * Entry bodies - The declarations and statements are moved to |
| 556 |
-- the procedure equivalen of the entry. |
| 557 |
-- * Protected subprograms - The declarations and statements are |
| 558 |
-- moved to the non-protected version of the subprogram. |
| 559 |
-- * Task bodies - The declarations and statements are moved to the |
| 560 |
-- task body procedure. |
| 561 |
-- * Blocks that will be rewritten as subprograms when unnesting |
| 562 |
-- is in effect. |
| 563 |
|
| 564 |
-- Visible declarations do not need to be installed in these three |
| 565 |
-- cases since it does not make semantic sense to do so. All entities |
| 566 |
-- referenced by a finalizer are visible and already resolved, plus |
| 567 |
-- the enclosing scope may not have visible declarations at all. |
| 568 |
|
| 569 |
if Ekind (E) = E_Procedure |
| 570 |
and then Is_Finalizer (E) |
| 571 |
and then |
| 572 |
(Is_Entry (E_Scope) |
| 573 |
or else (Is_Subprogram (E_Scope) |
| 574 |
and then Is_Protected_Type (Scope (E_Scope))) |
| 575 |
or else Is_Task_Type (E_Scope) |
| 576 |
or else Ekind (E_Scope) = E_Block) |
| 577 |
then |
| 578 |
null; |
| 579 |
else |
| 580 |
Install_Visible_Declarations (E_Scope); |
| 581 |
end if; |
| 582 |
|
| 583 |
if Is_Concurrent_Type (E_Scope) |
| 584 |
or else Is_Package_Or_Generic_Package (E_Scope) |
| 585 |
then |
| 586 |
Install_Private_Declarations (E_Scope); |
| 587 |
end if; |
| 588 |
|
| 589 |
-- If the entity is in an outer scope, then that scope needs to |
| 590 |
-- temporarily become the current scope so that operations created |
| 591 |
-- during type freezing will be declared in the right scope and |
| 592 |
-- can properly override any corresponding inherited operations. |
| 593 |
|
| 594 |
elsif In_Outer_Scope then |
| 595 |
Push_Scope (E_Scope); |
| 596 |
end if; |
| 597 |
|
| 598 |
-- If type, freeze the type |
| 599 |
|
| 600 |
if Is_Type (E) then |
| 601 |
Delete := Freeze_Type (N); |
| 602 |
|
| 603 |
-- And for enumeration type, build the enumeration tables |
| 604 |
|
| 605 |
if Is_Enumeration_Type (E) then |
| 606 |
Build_Enumeration_Image_Tables (E, N); |
| 607 |
end if; |
| 608 |
|
| 609 |
-- If subprogram, freeze the subprogram |
| 610 |
|
| 611 |
elsif Is_Subprogram (E) then |
| 612 |
Exp_Ch6.Freeze_Subprogram (N); |
| 613 |
|
| 614 |
-- Ada 2005 (AI-251): Remove the freeze nodes associated with the |
| 615 |
-- entities internally used by the front end to register primitives |
| 616 |
-- covering abstract interfaces if they have no side effects. For the |
| 617 |
-- others, gigi must discard them after evaluating the side effects. |
| 618 |
|
| 619 |
Delete := Present (Interface_Alias (E)) and then No (Actions (N)); |
| 620 |
end if; |
| 621 |
|
| 622 |
-- Analyze actions generated by freezing. The init_proc contains source |
| 623 |
-- expressions that may raise Constraint_Error, the assignment |
| 624 |
-- procedure for complex types needs checks on individual component |
| 625 |
-- assignments, and wrappers may need checks. Other freezing actions |
| 626 |
-- should be compiled with all checks off. |
| 627 |
|
| 628 |
Decl := First (Actions (N)); |
| 629 |
while Present (Decl) loop |
| 630 |
if Nkind (Decl) = N_Subprogram_Body |
| 631 |
and then (Is_Init_Proc (Defining_Entity (Decl)) |
| 632 |
or else |
| 633 |
Chars (Defining_Entity (Decl)) = Name_uAssign |
| 634 |
or else |
| 635 |
(Present (Corresponding_Spec (Decl)) |
| 636 |
and then Is_Wrapper |
| 637 |
(Corresponding_Spec (Decl)))) |
| 638 |
then |
| 639 |
Analyze (Decl); |
| 640 |
|
| 641 |
-- A subprogram body created for a renaming_as_body completes |
| 642 |
-- a previous declaration, which may be in a different scope. |
| 643 |
-- Establish the proper scope before analysis. |
| 644 |
|
| 645 |
elsif Nkind (Decl) = N_Subprogram_Body |
| 646 |
and then Present (Corresponding_Spec (Decl)) |
| 647 |
and then Scope (Corresponding_Spec (Decl)) /= Current_Scope |
| 648 |
then |
| 649 |
Push_Scope (Scope (Corresponding_Spec (Decl))); |
| 650 |
Analyze (Decl, Suppress => All_Checks); |
| 651 |
Pop_Scope; |
| 652 |
|
| 653 |
-- We treat generated equality specially, if validity checks are |
| 654 |
-- enabled, in order to detect components default-initialized with |
| 655 |
-- invalid values. |
| 656 |
|
| 657 |
elsif Nkind (Decl) = N_Subprogram_Body |
| 658 |
and then Chars (Defining_Entity (Decl)) = Name_Op_Eq |
| 659 |
and then Validity_Checks_On |
| 660 |
and then Initialize_Scalars |
| 661 |
then |
| 662 |
declare |
| 663 |
Save_Force : constant Boolean := Force_Validity_Checks; |
| 664 |
begin |
| 665 |
Force_Validity_Checks := True; |
| 666 |
Analyze (Decl); |
| 667 |
Force_Validity_Checks := Save_Force; |
| 668 |
end; |
| 669 |
|
| 670 |
-- All other freezing actions |
| 671 |
|
| 672 |
else |
| 673 |
Analyze (Decl, Suppress => All_Checks); |
| 674 |
end if; |
| 675 |
|
| 676 |
Next (Decl); |
| 677 |
end loop; |
| 678 |
|
| 679 |
-- If we are to delete this N_Freeze_Entity, do so by rewriting so that |
| 680 |
-- a loop on all nodes being inserted will work propertly. |
| 681 |
|
| 682 |
if Delete then |
| 683 |
Rewrite (N, Make_Null_Statement (Sloc (N))); |
| 684 |
end if; |
| 685 |
|
| 686 |
-- Pop scope if we installed one for the analysis |
| 687 |
|
| 688 |
if In_Other_Scope then |
| 689 |
if Ekind (Current_Scope) = E_Package then |
| 690 |
End_Package_Scope (E_Scope); |
| 691 |
else |
| 692 |
End_Scope; |
| 693 |
end if; |
| 694 |
|
| 695 |
elsif In_Outer_Scope then |
| 696 |
Pop_Scope; |
| 697 |
end if; |
| 698 |
|
| 699 |
-- Restore previous value of the nesting-level counter that records |
| 700 |
-- whether we are inside a (possibly nested) call to this procedure. |
| 701 |
|
| 702 |
Inside_Freezing_Actions := Inside_Freezing_Actions - 1; |
| 703 |
end Expand_N_Freeze_Entity; |
| 704 |
|
| 705 |
------------------------------------------- |
| 706 |
-- Expand_N_Record_Representation_Clause -- |
| 707 |
------------------------------------------- |
| 708 |
|
| 709 |
-- The only expansion required is for the case of a mod clause present, |
| 710 |
-- which is removed, and translated into an alignment representation |
| 711 |
-- clause inserted immediately after the record rep clause with any |
| 712 |
-- initial pragmas inserted at the start of the component clause list. |
| 713 |
|
| 714 |
procedure Expand_N_Record_Representation_Clause (N : Node_Id) is |
| 715 |
Loc : constant Source_Ptr := Sloc (N); |
| 716 |
Rectype : constant Entity_Id := Entity (Identifier (N)); |
| 717 |
Mod_Val : Uint; |
| 718 |
Citems : List_Id; |
| 719 |
Repitem : Node_Id; |
| 720 |
AtM_Nod : Node_Id; |
| 721 |
|
| 722 |
begin |
| 723 |
if Present (Mod_Clause (N)) and then not Ignore_Rep_Clauses then |
| 724 |
Mod_Val := Expr_Value (Expression (Mod_Clause (N))); |
| 725 |
Citems := Pragmas_Before (Mod_Clause (N)); |
| 726 |
|
| 727 |
if Present (Citems) then |
| 728 |
Append_List_To (Citems, Component_Clauses (N)); |
| 729 |
Set_Component_Clauses (N, Citems); |
| 730 |
end if; |
| 731 |
|
| 732 |
AtM_Nod := |
| 733 |
Make_Attribute_Definition_Clause (Loc, |
| 734 |
Name => New_Occurrence_Of (Base_Type (Rectype), Loc), |
| 735 |
Chars => Name_Alignment, |
| 736 |
Expression => Make_Integer_Literal (Loc, Mod_Val)); |
| 737 |
|
| 738 |
Set_From_At_Mod (AtM_Nod); |
| 739 |
Insert_After (N, AtM_Nod); |
| 740 |
Set_Mod_Clause (N, Empty); |
| 741 |
end if; |
| 742 |
|
| 743 |
-- If the record representation clause has no components, then |
| 744 |
-- completely remove it. Note that we also have to remove |
| 745 |
-- ourself from the Rep Item list. |
| 746 |
|
| 747 |
if Is_Empty_List (Component_Clauses (N)) then |
| 748 |
if First_Rep_Item (Rectype) = N then |
| 749 |
Set_First_Rep_Item (Rectype, Next_Rep_Item (N)); |
| 750 |
else |
| 751 |
Repitem := First_Rep_Item (Rectype); |
| 752 |
while Present (Next_Rep_Item (Repitem)) loop |
| 753 |
if Next_Rep_Item (Repitem) = N then |
| 754 |
Set_Next_Rep_Item (Repitem, Next_Rep_Item (N)); |
| 755 |
exit; |
| 756 |
end if; |
| 757 |
|
| 758 |
Next_Rep_Item (Repitem); |
| 759 |
end loop; |
| 760 |
end if; |
| 761 |
|
| 762 |
Rewrite (N, |
| 763 |
Make_Null_Statement (Loc)); |
| 764 |
end if; |
| 765 |
end Expand_N_Record_Representation_Clause; |
| 766 |
|
| 767 |
end Exp_Ch13; |