| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- E X P _ C H 7 -- |
| 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 |
-- This package contains virtually all expansion mechanisms related to |
| 27 |
-- - controlled types |
| 28 |
-- - transient scopes |
| 29 |
|
| 30 |
with Atree; use Atree; |
| 31 |
with Debug; use Debug; |
| 32 |
with Einfo; use Einfo; |
| 33 |
with Einfo.Entities; use Einfo.Entities; |
| 34 |
with Einfo.Utils; use Einfo.Utils; |
| 35 |
with Elists; use Elists; |
| 36 |
with Errout; use Errout; |
| 37 |
with Exp_Ch6; use Exp_Ch6; |
| 38 |
with Exp_Ch9; use Exp_Ch9; |
| 39 |
with Exp_Ch11; use Exp_Ch11; |
| 40 |
with Exp_Dbug; use Exp_Dbug; |
| 41 |
with Exp_Dist; use Exp_Dist; |
| 42 |
with Exp_Disp; use Exp_Disp; |
| 43 |
with Exp_Prag; use Exp_Prag; |
| 44 |
with Exp_Tss; use Exp_Tss; |
| 45 |
with Exp_Util; use Exp_Util; |
| 46 |
with Freeze; use Freeze; |
| 47 |
with GNAT_CUDA; use GNAT_CUDA; |
| 48 |
with Inline; use Inline; |
| 49 |
with Lib; use Lib; |
| 50 |
with Nlists; use Nlists; |
| 51 |
with Nmake; use Nmake; |
| 52 |
with Opt; use Opt; |
| 53 |
with Output; use Output; |
| 54 |
with Restrict; use Restrict; |
| 55 |
with Rident; use Rident; |
| 56 |
with Rtsfind; use Rtsfind; |
| 57 |
with Sinfo; use Sinfo; |
| 58 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 59 |
with Sinfo.Utils; use Sinfo.Utils; |
| 60 |
with Sem; use Sem; |
| 61 |
with Sem_Aux; use Sem_Aux; |
| 62 |
with Sem_Ch7; use Sem_Ch7; |
| 63 |
with Sem_Ch8; use Sem_Ch8; |
| 64 |
with Sem_Res; use Sem_Res; |
| 65 |
with Sem_Util; use Sem_Util; |
| 66 |
with Snames; use Snames; |
| 67 |
with Stand; use Stand; |
| 68 |
with Tbuild; use Tbuild; |
| 69 |
with Ttypes; use Ttypes; |
| 70 |
with Uintp; use Uintp; |
| 71 |
|
| 72 |
package body Exp_Ch7 is |
| 73 |
|
| 74 |
----------------------------- |
| 75 |
-- Finalization Management -- |
| 76 |
----------------------------- |
| 77 |
|
| 78 |
-- This paragraph describes how Initialization/Adjustment/Finalization |
| 79 |
-- procedures are generated and called. Two cases must be considered: types |
| 80 |
-- that are controlled (Is_Controlled flag set) and composite types that |
| 81 |
-- contain controlled components (Has_Controlled_Component flag set). In |
| 82 |
-- the first case the procedures to call are the user-defined primitive |
| 83 |
-- operations Initialize/Adjust/Finalize. In the second case, the compiler |
| 84 |
-- generates Deep_Initialize, Deep_Adjust and Deep_Finalize that are in |
| 85 |
-- charge of calling the former procedures on the controlled components. |
| 86 |
|
| 87 |
-- Initialize calls: they are generated for either declarations or dynamic |
| 88 |
-- allocations of controlled objects with no initial value. They are always |
| 89 |
-- followed by an attachment to some finalization chain. For the dynamic |
| 90 |
-- dynamic allocation case, this is the collection attached to the access |
| 91 |
-- type definition; otherwise, this is the master of the current scope. |
| 92 |
|
| 93 |
-- Adjust calls: they are generated on two occasions: (1) for declarations |
| 94 |
-- or dynamic allocations of controlled objects with an initial value (with |
| 95 |
-- the exception of function calls), (2) after an assignment. In the first |
| 96 |
-- case they are followed by an attachment to the finalization chain, in |
| 97 |
-- the second case they are not. |
| 98 |
|
| 99 |
-- Finalization calls: they are generated on three occasions: (1) on scope |
| 100 |
-- exit, (2) assignments, (3) unchecked deallocations. In case (3) objects |
| 101 |
-- have to be detached from the finalization chain, in case (2) they must |
| 102 |
-- not and in case (1) this is optional as we are exiting the scope anyway. |
| 103 |
|
| 104 |
-- There are two kinds of finalization chain to which objects are attached, |
| 105 |
-- depending on the way they are created. For objects (statically) declared |
| 106 |
-- in a scope, the finalization chain is that of the master of the scope, |
| 107 |
-- which is embodied in a Finalization_Master object. As per RM 7.6.1(11/3) |
| 108 |
-- the finalization of the master (on scope exit) performs the finalization |
| 109 |
-- of objects attached to its chain in the reverse order of their creation. |
| 110 |
|
| 111 |
-- For dynamically allocated objects, the finalization chain is that of the |
| 112 |
-- finalization collection of the access type through which the objects are |
| 113 |
-- allocated, which is embodied in a Finalization_Collection object. As per |
| 114 |
-- RM 7.6.1(11.1/3), the finalization of the collection performs the |
| 115 |
-- finalization of objects attached to its chain in an arbitrary order. |
| 116 |
|
| 117 |
-- A Finalization_Collection object is implemented as a controlled object |
| 118 |
-- and its finalization is therefore driven by the finalization master of |
| 119 |
-- the scope where it is declared. As per RM 7.6.1(11.2/3), for a named |
| 120 |
-- access type, the Finalization_Collection object is declared in the list |
| 121 |
-- of actions of its freeze node. |
| 122 |
|
| 123 |
-- ??? For an anonymous access type, the implementation deviates from the |
| 124 |
-- RM 7.6.1 clause as follows: all the anonymous access types with the same |
| 125 |
-- designated type that are (implicitly) declared in a library unit share a |
| 126 |
-- single Finalization_Collection object declared in the outermost scope of |
| 127 |
-- the library unit, except if the designated type is declared in a dynamic |
| 128 |
-- scope nested in the unit; in this case no Finalization_Collection object |
| 129 |
-- is created. As a result, in the first case, objects allocated through |
| 130 |
-- the anonymous access types are finalized when the library unit goes out |
| 131 |
-- of scope, while in the second case, they are not finalized at all. |
| 132 |
|
| 133 |
-- Here is a simple example of the expansion of a controlled block: |
| 134 |
|
| 135 |
-- declare |
| 136 |
-- X : Ctrl; |
| 137 |
-- Y : Ctrl := Init; |
| 138 |
|
| 139 |
-- type Rec is record |
| 140 |
-- C : Ctrl; |
| 141 |
-- end record; |
| 142 |
|
| 143 |
-- W : Rec; |
| 144 |
-- Z : Rec := Init; |
| 145 |
|
| 146 |
-- begin |
| 147 |
-- X := Y; |
| 148 |
-- W := Z; |
| 149 |
-- end; |
| 150 |
-- |
| 151 |
-- is expanded into: |
| 152 |
-- |
| 153 |
-- declare |
| 154 |
-- Mnn : System.Finalization_Primitives.Finalization_Master; |
| 155 |
|
| 156 |
-- XMN : aliased System.Finalization_Primitives.Master_Node; |
| 157 |
-- X : Ctrl; |
| 158 |
-- Bnn : begin |
| 159 |
-- Abort_Defer; |
| 160 |
-- Initialize (X); |
| 161 |
-- System.Finalization_Primitives.Attach_To_Master |
| 162 |
-- (X'address, |
| 163 |
-- CtrlFD'unrestricted_access, |
| 164 |
-- XMN'unrestricted_access, |
| 165 |
-- Mnn); |
| 166 |
-- at end |
| 167 |
-- Abort_Undefer; |
| 168 |
-- end Bnn; |
| 169 |
|
| 170 |
-- YMN : aliased System.Finalization_Primitives.Master_Node; |
| 171 |
-- Y : Ctrl := Init; |
| 172 |
-- System.Finalization_Primitives.Attach_To_Master |
| 173 |
-- (Y'address, |
| 174 |
-- CtrlFD'unrestricted_access, |
| 175 |
-- YMN'unrestricted_access, |
| 176 |
-- Mnn); |
| 177 |
|
| 178 |
-- type Rec is record |
| 179 |
-- C : Ctrl; |
| 180 |
-- end record; |
| 181 |
|
| 182 |
-- WMN : aliased System.Finalization_Primitives.Master_Node; |
| 183 |
-- W : Rec; |
| 184 |
-- Bnn : begin |
| 185 |
-- Abort_Defer; |
| 186 |
-- Bnn : begin |
| 187 |
-- Deep_Initialize (W); |
| 188 |
-- System.Finalization_Primitives.Attach_To_Master |
| 189 |
-- (W'address, |
| 190 |
-- Rec_FD'unrestricted_access, |
| 191 |
-- WMN'unrestricted_access, |
| 192 |
-- Mnn); |
| 193 |
-- exception |
| 194 |
-- when others => |
| 195 |
-- Deep_Finalize (W); |
| 196 |
-- end Bnn; |
| 197 |
-- at end |
| 198 |
-- Abort_Undefer; |
| 199 |
-- end Bnn; |
| 200 |
|
| 201 |
-- ZMN : aliaed System.Finalization_Primitives.Master_Node; |
| 202 |
-- Z : Rec := Init; |
| 203 |
-- System.Finalization_Primitives.Attach_To_Master |
| 204 |
-- (Z'address, |
| 205 |
-- Rec_FD'unrestricted_access, |
| 206 |
-- ZMN'unrestricted_access, |
| 207 |
-- Mnn); |
| 208 |
|
| 209 |
-- procedure _Finalizer is |
| 210 |
-- Ann : constant Boolean := Ada.Exceptions.Triggered_By_Abort; |
| 211 |
-- Rnn : boolean := False; |
| 212 |
-- begin |
| 213 |
-- Abort_Defer; |
| 214 |
-- Bnn : begin |
| 215 |
-- System.Finalization_Primitives.Finalize_Master (Mnn); |
| 216 |
-- exceptions |
| 217 |
-- when others => |
| 218 |
-- Rnn := True; |
| 219 |
-- end Bnn; |
| 220 |
-- Abort_Undefer; |
| 221 |
-- if Rnn and then not Ann then |
| 222 |
-- [program_error "finalize raised exception"] |
| 223 |
-- end if; |
| 224 |
-- end _Finalizer; |
| 225 |
|
| 226 |
-- begin |
| 227 |
-- _Assign (X, Y); |
| 228 |
-- Deep_Finalize (W); |
| 229 |
-- W := Z; |
| 230 |
-- Deep_Adjust (W); |
| 231 |
-- end; |
| 232 |
-- at end |
| 233 |
-- _Finalizer; |
| 234 |
|
| 235 |
-- In the case of a block containing a single controlled object, the master |
| 236 |
-- degenerates into a single master node: |
| 237 |
|
| 238 |
-- declare |
| 239 |
-- X : Ctrl := Init; |
| 240 |
|
| 241 |
-- begin |
| 242 |
-- null; |
| 243 |
-- end; |
| 244 |
|
| 245 |
-- is expanded into: |
| 246 |
|
| 247 |
-- declare |
| 248 |
-- XMN : aliased System.Finalization_Primitives.Master_Node; |
| 249 |
-- X : Ctrl := Init; |
| 250 |
-- System.Finalization_Primitives.Attach_To_Node |
| 251 |
-- (X'address, |
| 252 |
-- CtrlFD'unrestricted_access, |
| 253 |
-- XMN'unrestricted_access); |
| 254 |
|
| 255 |
-- procedure _Finalizer is |
| 256 |
-- Ann : constant Boolean := Ada.Exceptions.Triggered_By_Abort; |
| 257 |
-- Rnn : boolean := False; |
| 258 |
-- begin |
| 259 |
-- Abort_Defer; |
| 260 |
-- Bnn : begin |
| 261 |
-- System.Finalization_Primitives.Finalize_Object (XMN); |
| 262 |
-- exceptions |
| 263 |
-- when others => |
| 264 |
-- Rnn := True; |
| 265 |
-- end Bnn; |
| 266 |
-- Abort_Undefer; |
| 267 |
-- if Rnn and then not Ann then |
| 268 |
-- [program_error "finalize raised exception"] |
| 269 |
-- end if; |
| 270 |
-- end _Finalizer; |
| 271 |
|
| 272 |
-- begin |
| 273 |
-- null; |
| 274 |
-- end; |
| 275 |
-- at end |
| 276 |
-- _Finalizer; |
| 277 |
|
| 278 |
-- Here is the version with a dynamically allocated object: |
| 279 |
|
| 280 |
-- declare |
| 281 |
-- X : P_Ctrl := new Ctrl; |
| 282 |
|
| 283 |
-- begin |
| 284 |
-- null; |
| 285 |
-- end; |
| 286 |
-- |
| 287 |
-- is expanded into: |
| 288 |
|
| 289 |
-- declare |
| 290 |
-- Cnn : System.Finalization_Primitives.Finalization_Collection_Ptr := |
| 291 |
-- P_CtrlFC'unrestricted_access; |
| 292 |
-- [...] |
| 293 |
-- Pnn : constant P_Ctrl := new Ctrl[...][...]; |
| 294 |
-- Bnn : begin |
| 295 |
-- Abort_Defer; |
| 296 |
-- Initialize (Pnn.all); |
| 297 |
-- System.Finalization_Primitives.Attach_To_Collection |
| 298 |
-- (Pnn.all'address, |
| 299 |
-- CtrlFD'unrestricted_access, |
| 300 |
-- Cnn.all); |
| 301 |
-- at end |
| 302 |
-- Abort_Undefer; |
| 303 |
-- end Bnn; |
| 304 |
-- X : P_Ctrl := Pnn; |
| 305 |
|
| 306 |
-- The implementation uses two different strategies for the finalization |
| 307 |
-- of (statically) declared objects and of dynamically allocated objects. |
| 308 |
|
| 309 |
-- For (statically) declared objects, the attachment to the finalization |
| 310 |
-- chain of the current scope and the call to the finalization procedure |
| 311 |
-- are generated during a post-processing phase of the expansion. These |
| 312 |
-- objects are first spotted in declarative parts and statement lists by |
| 313 |
-- Requires_Cleanup_Actions; then Build_Finalizer is called on the parent |
| 314 |
-- node to generate both the attachment and the finalization actions. |
| 315 |
|
| 316 |
-- This post processing is fully transparent for the rest of the expansion |
| 317 |
-- activities, in other words those have nothing to do or to care about. |
| 318 |
-- However this default processing may not be sufficient in specific cases, |
| 319 |
-- e.g. for the return object of an extended return statement in a function |
| 320 |
-- whose result type is controlled: in this case, the return object must be |
| 321 |
-- finalized only if the function returns abnormally. In order to deal with |
| 322 |
-- these cases, it is possible to directly generate detachment actions (for |
| 323 |
-- the return object case) or finalization actions (for transient objects) |
| 324 |
-- during the rest of expansion activities. |
| 325 |
|
| 326 |
-- These direct actions must be signalled to the post-processing machinery |
| 327 |
-- and this is achieved through the handling of Master_Node objects, which |
| 328 |
-- are the items actually chained in the finalization chains of masters. |
| 329 |
-- With the default processing, they are created by Build_Finalizer for the |
| 330 |
-- controlled objects spotted by Requires_Cleanup_Actions. But when direct |
| 331 |
-- actions are carried out, they are generated by these actions and later |
| 332 |
-- recognized by Requires_Cleanup_Actions and picked up by Build_Finalizer. |
| 333 |
|
| 334 |
-- For dynamically allocated objects, there is no post-processing phase and |
| 335 |
-- the attachment to the finalization chain of the access type, as well the |
| 336 |
-- the detachment from this chain for unchecked deallocation, are generated |
| 337 |
-- directly by the compiler during the expansion of allocators and calls to |
| 338 |
-- instances of the Unchecked_Deallocation procedure. |
| 339 |
|
| 340 |
-------------------------- |
| 341 |
-- Relaxed Finalization -- |
| 342 |
-------------------------- |
| 343 |
|
| 344 |
-- This paragraph describes the differences between the implementation of |
| 345 |
-- finalization as specified by the Ada RM (called "strict" and documented |
| 346 |
-- in the previous paragraph) and that of finalization as specified by the |
| 347 |
-- GNAT RM (called "relaxed") for a second category of controlled objects. |
| 348 |
|
| 349 |
-- For objects (statically) declared in a scope, the default implementation |
| 350 |
-- documented in the previous paragraph is used for the scope as a whole as |
| 351 |
-- soon as one controlled object with strict finalization is present in it, |
| 352 |
-- including one transient controlled object. Otherwise, that is to say, if |
| 353 |
-- all the controlled objects in the scope have relaxed finalization, then |
| 354 |
-- no Finalization_Master is built for this scope, and all the objects are |
| 355 |
-- finalized explicitly in the reverse order of their creation: |
| 356 |
|
| 357 |
-- declare |
| 358 |
-- X : Ctrl := Init; |
| 359 |
-- Y : Ctrl := Init; |
| 360 |
|
| 361 |
-- begin |
| 362 |
-- null; |
| 363 |
-- end; |
| 364 |
|
| 365 |
-- is expanded into: |
| 366 |
|
| 367 |
-- declare |
| 368 |
-- XMN : aliased System.Finalization_Primitives.Master_Node; |
| 369 |
-- X : Ctrl := Init; |
| 370 |
-- System.Finalization_Primitives.Attach_To_Node |
| 371 |
-- (X'address, |
| 372 |
-- CtrlFD'unrestricted_access, |
| 373 |
-- XMN'unrestricted_access); |
| 374 |
-- YMN : aliased System.Finalization_Primitives.Master_Node; |
| 375 |
-- Y : Ctrl := Init; |
| 376 |
-- System.Finalization_Primitives.Attach_To_Node |
| 377 |
-- (Y'address, |
| 378 |
-- CtrlFD'unrestricted_access, |
| 379 |
-- YMN'unrestricted_access); |
| 380 |
|
| 381 |
-- procedure _Finalizer is |
| 382 |
-- begin |
| 383 |
-- Abort_Defer; |
| 384 |
-- System.Finalization_Primitives.Finalize_Object (YMN); |
| 385 |
-- System.Finalization_Primitives.Finalize_Object (XMN); |
| 386 |
-- Abort_Undefer; |
| 387 |
-- end _Finalizer; |
| 388 |
|
| 389 |
-- begin |
| 390 |
-- null; |
| 391 |
-- end; |
| 392 |
-- at end |
| 393 |
-- _Finalizer; |
| 394 |
|
| 395 |
-- Dynamically allocated objects with relaxed finalization need not be |
| 396 |
-- finalized and, therefore, are not attached to any finalization chain. |
| 397 |
|
| 398 |
type Final_Primitives is |
| 399 |
(Initialize_Case, Adjust_Case, Finalize_Case, Address_Case); |
| 400 |
-- This enumeration type is defined in order to ease sharing code for |
| 401 |
-- building finalization procedures for composite types. |
| 402 |
|
| 403 |
Name_Of : constant array (Final_Primitives) of Name_Id := |
| 404 |
(Initialize_Case => Name_Initialize, |
| 405 |
Adjust_Case => Name_Adjust, |
| 406 |
Finalize_Case => Name_Finalize, |
| 407 |
Address_Case => Name_Finalize_Address); |
| 408 |
Deep_Name_Of : constant array (Final_Primitives) of TSS_Name_Type := |
| 409 |
(Initialize_Case => TSS_Deep_Initialize, |
| 410 |
Adjust_Case => TSS_Deep_Adjust, |
| 411 |
Finalize_Case => TSS_Deep_Finalize, |
| 412 |
Address_Case => TSS_Finalize_Address); |
| 413 |
|
| 414 |
function Allows_Finalization_Collection (Typ : Entity_Id) return Boolean; |
| 415 |
-- Determine whether access type Typ may have a finalization collection |
| 416 |
|
| 417 |
procedure Build_Array_Deep_Procs (Typ : Entity_Id); |
| 418 |
-- Build the deep Initialize/Adjust/Finalize for a record Typ with |
| 419 |
-- Has_Controlled_Component set and store them using the TSS mechanism. |
| 420 |
|
| 421 |
function Build_Cleanup_Statements |
| 422 |
(N : Node_Id; |
| 423 |
Additional_Cleanup : List_Id) return List_Id; |
| 424 |
-- Create the cleanup calls for an asynchronous call block, task master, |
| 425 |
-- protected subprogram body, task allocation block or task body, or |
| 426 |
-- additional cleanup actions parked on a transient block. If the context |
| 427 |
-- does not contain the above constructs, the routine returns an empty |
| 428 |
-- list. |
| 429 |
|
| 430 |
procedure Build_Finalizer_Call (N : Node_Id; Fin_Id : Entity_Id); |
| 431 |
-- N is a construct that contains a handled sequence of statements, Fin_Id |
| 432 |
-- is the entity of a finalizer. Create an At_End handler that covers the |
| 433 |
-- statements of N and calls Fin_Id. If the handled statement sequence has |
| 434 |
-- an exception handler, the statements will be wrapped in a block to avoid |
| 435 |
-- unwanted interaction with the new At_End handler. |
| 436 |
|
| 437 |
procedure Build_Record_Deep_Procs (Typ : Entity_Id); |
| 438 |
-- Build the deep Initialize/Adjust/Finalize for a record Typ with |
| 439 |
-- Has_Component_Component set and store them using the TSS mechanism. |
| 440 |
|
| 441 |
-------------------------------- |
| 442 |
-- Transient Scope Management -- |
| 443 |
-------------------------------- |
| 444 |
|
| 445 |
-- A transient scope is needed when certain temporary objects are created |
| 446 |
-- by the compiler. These temporary objects are allocated on the secondary |
| 447 |
-- stack and/or need finalization, and the transient scope is responsible |
| 448 |
-- for finalizing the objects and reclaiming the memory of the secondary |
| 449 |
-- stack at the appropriate time. They are generally objects allocated to |
| 450 |
-- store the result of a function returning an unconstrained or controlled |
| 451 |
-- value. Expressions needing to be wrapped in a transient scope may appear |
| 452 |
-- in three different contexts, which lead to different kinds of transient |
| 453 |
-- scope expansion: |
| 454 |
|
| 455 |
-- 1. In a simple statement (procedure call, assignment, ...). In this |
| 456 |
-- case the statement is wrapped into a transient block, which takes |
| 457 |
-- care of the finalization actions as well as the secondary stack |
| 458 |
-- deallocation, See Wrap_Transient_Statement for details. |
| 459 |
|
| 460 |
-- 2. In an expression of a control structure (test in a If statement, |
| 461 |
-- expression in a Case statement, ...). In this case the expression |
| 462 |
-- is replaced by a temporary and the enclosing statement is wrapped |
| 463 |
-- into a transient block, which takes care of the finalization actions |
| 464 |
-- and the secondary stack deallocation. See Wrap_Transient_Expression |
| 465 |
-- for details. |
| 466 |
|
| 467 |
-- 3. In an expression of an object declaration. No wrapping is possible |
| 468 |
-- here, so the finalization actions performed on the normal path, if |
| 469 |
-- any, are done right after the declaration, and those performed on |
| 470 |
-- the exceptional path, as well as the secondary stack deallocation, |
| 471 |
-- are deferred to the enclosing scope. See Wrap_Transient_Declaration |
| 472 |
-- for details. |
| 473 |
|
| 474 |
-- A transient scope is created by calling Establish_Transient_Scope on the |
| 475 |
-- node that needs to be serviced by it (the serviced node can subsequently |
| 476 |
-- be retrieved by invoking Node_To_Be_Wrapped when the current scope is a |
| 477 |
-- transient scope). Once this has been done, the normal processing of the |
| 478 |
-- Insert_Actions procedures is blocked and the procedures are redirected |
| 479 |
-- to the Store_xxx_Actions_In_Scope procedures and Store_Actions_In_Scope |
| 480 |
-- is ultimately invoked to store the pending actions. |
| 481 |
|
| 482 |
-- A transient scope is finalized by calling one of the Wrap_Transient_xxx |
| 483 |
-- procedures depending on the context as explained above. They ultimately |
| 484 |
-- invoke Insert_Actions_In_Scope_Around as per the following picture: |
| 485 |
|
| 486 |
-- Wrap_Transient_Expression Wrap_Transient_Statement |
| 487 |
-- | | |
| 488 |
-- V V |
| 489 |
-- Make_Transient_Block |
| 490 |
-- | |
| 491 |
-- Wrap_Transient_Declaration | |
| 492 |
-- | | |
| 493 |
-- V V |
| 494 |
-- Insert_Actions_In_Scope_Around |
| 495 |
|
| 496 |
procedure Insert_Actions_In_Scope_Around |
| 497 |
(N : Node_Id; |
| 498 |
Clean : Boolean; |
| 499 |
Manage_SS : Boolean); |
| 500 |
-- Insert the before-actions kept in the scope stack before N, and the |
| 501 |
-- after-actions after N, which must be a member of a list. If Clean is |
| 502 |
-- true, insert any cleanup actions kept in the scope stack and generate |
| 503 |
-- required finalization actions for the before-actions and after-actions. |
| 504 |
-- If Manage_SS is true, insert calls to mark/release the secondary stack. |
| 505 |
|
| 506 |
function Make_Transient_Block |
| 507 |
(Loc : Source_Ptr; |
| 508 |
Action : Node_Id; |
| 509 |
Par : Node_Id) return Node_Id; |
| 510 |
-- Action is a single statement or object declaration. Par is the proper |
| 511 |
-- parent of the generated block. Create a transient block whose name is |
| 512 |
-- the current scope and the only handled statement is Action. If Action |
| 513 |
-- involves controlled objects or secondary stack usage, the corresponding |
| 514 |
-- cleanup actions are performed at the end of the block. |
| 515 |
|
| 516 |
procedure Store_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id); |
| 517 |
-- Shared processing for the Store_xxx_Actions_In_Scope routines: attach |
| 518 |
-- the list L of actions to the list of actions stored in the top of the |
| 519 |
-- scope stack specified by AK. |
| 520 |
|
| 521 |
procedure Store_New_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id); |
| 522 |
-- Same as above for the case where the list of actions stored in the top |
| 523 |
-- of the scope stack specified by AK is empty. |
| 524 |
|
| 525 |
------------------------------------------- |
| 526 |
-- Unnesting procedures for CCG and LLVM -- |
| 527 |
------------------------------------------- |
| 528 |
|
| 529 |
-- Expansion generates subprograms for controlled types management that |
| 530 |
-- may appear in declarative lists in package declarations and bodies. |
| 531 |
-- These subprograms appear within generated blocks that contain local |
| 532 |
-- declarations and a call to finalization procedures. To ensure that |
| 533 |
-- such subprograms get activation records when needed, we transform the |
| 534 |
-- block into a procedure body, followed by a call to it in the same |
| 535 |
-- declarative list. |
| 536 |
|
| 537 |
procedure Check_Unnesting_Elaboration_Code (N : Node_Id); |
| 538 |
-- The statement part of a package body that is a compilation unit may |
| 539 |
-- contain blocks that declare local subprograms. In Subprogram_Unnesting_ |
| 540 |
-- Mode such subprograms must be handled as nested inside the (implicit) |
| 541 |
-- elaboration procedure that executes that statement part. To handle |
| 542 |
-- properly uplevel references we construct that subprogram explicitly, |
| 543 |
-- to contain blocks and inner subprograms, the statement part becomes |
| 544 |
-- a call to this subprogram. This is only done if blocks are present |
| 545 |
-- in the statement list of the body. (It would be nice to unify this |
| 546 |
-- procedure with Check_Unnesting_In_Decls_Or_Stmts, if possible, since |
| 547 |
-- they're doing very similar work, but are structured differently. ???) |
| 548 |
|
| 549 |
procedure Check_Unnesting_In_Decls_Or_Stmts (Decls_Or_Stmts : List_Id); |
| 550 |
-- Similarly, the declarations or statements in library-level packages may |
| 551 |
-- have created blocks with nested subprograms. Such a block must be |
| 552 |
-- transformed into a procedure followed by a call to it, so that unnesting |
| 553 |
-- can handle uplevel references within these nested subprograms (typically |
| 554 |
-- subprograms that handle finalization actions). This also applies to |
| 555 |
-- nested packages, including instantiations, in which case it must |
| 556 |
-- recursively process inner bodies. |
| 557 |
|
| 558 |
procedure Check_Unnesting_In_Handlers (N : Node_Id); |
| 559 |
-- Similarly, check for blocks with nested subprograms occurring within |
| 560 |
-- a set of exception handlers associated with a package body N. |
| 561 |
|
| 562 |
procedure Unnest_Block (Decl : Node_Id); |
| 563 |
-- Blocks that contain nested subprograms with up-level references need to |
| 564 |
-- create activation records for them. We do this by rewriting the block as |
| 565 |
-- a procedure, followed by a call to it in the same declarative list, to |
| 566 |
-- replicate the semantics of the original block. |
| 567 |
-- |
| 568 |
-- A common source for such block is a transient block created for a |
| 569 |
-- construct (declaration, assignment, etc.) that involves controlled |
| 570 |
-- actions or secondary-stack management, in which case the nested |
| 571 |
-- subprogram is a finalizer. |
| 572 |
|
| 573 |
procedure Unnest_If_Statement (If_Stmt : Node_Id); |
| 574 |
-- The separate statement lists associated with an if-statement (then part, |
| 575 |
-- elsif parts, else part) may require unnesting if they directly contain |
| 576 |
-- a subprogram body that references up-level objects. Each statement list |
| 577 |
-- is traversed to locate such subprogram bodies, and if a part's statement |
| 578 |
-- list contains a body, then the list is replaced with a new procedure |
| 579 |
-- containing the part's statements followed by a call to the procedure. |
| 580 |
-- Furthermore, any nested blocks, loops, or if statements will also be |
| 581 |
-- traversed to determine the need for further unnesting transformations. |
| 582 |
|
| 583 |
procedure Unnest_Statement_List (Stmts : in out List_Id); |
| 584 |
-- A list of statements that directly contains a subprogram at its outer |
| 585 |
-- level, that may reference objects declared in that same statement list, |
| 586 |
-- is rewritten as a procedure containing the statement list Stmts (which |
| 587 |
-- includes any such objects as well as the nested subprogram), followed by |
| 588 |
-- a call to the new procedure, and Stmts becomes the list containing the |
| 589 |
-- procedure and the call. This ensures that Unnest_Subprogram will later |
| 590 |
-- properly handle up-level references from the nested subprogram to |
| 591 |
-- objects declared earlier in statement list, by creating an activation |
| 592 |
-- record and passing it to the nested subprogram. This procedure also |
| 593 |
-- resets the Scope of objects declared in the statement list, as well as |
| 594 |
-- the Scope of the nested subprogram, to refer to the new procedure. |
| 595 |
-- Also, the new procedure is marked Has_Nested_Subprogram, so this should |
| 596 |
-- only be called when known that the statement list contains a subprogram. |
| 597 |
|
| 598 |
procedure Unnest_Loop (Loop_Stmt : Node_Id); |
| 599 |
-- Top-level Loops that contain nested subprograms with up-level references |
| 600 |
-- need to have activation records. We do this by rewriting the loop as a |
| 601 |
-- procedure containing the loop, followed by a call to the procedure in |
| 602 |
-- the same library-level declarative list, to replicate the semantics of |
| 603 |
-- the original loop. Such loops can occur due to aggregate expansions and |
| 604 |
-- other constructs. |
| 605 |
|
| 606 |
----------------------- |
| 607 |
-- Local Subprograms -- |
| 608 |
----------------------- |
| 609 |
|
| 610 |
procedure Check_Visibly_Controlled |
| 611 |
(Prim : Final_Primitives; |
| 612 |
Typ : Entity_Id; |
| 613 |
E : in out Entity_Id; |
| 614 |
Cref : in out Node_Id); |
| 615 |
-- The controlled operation declared for a derived type may not be |
| 616 |
-- overriding, if the controlled operations of the parent type are hidden, |
| 617 |
-- for example when the parent is a private type whose full view is |
| 618 |
-- controlled. For other primitive operations we modify the name of the |
| 619 |
-- operation to indicate that it is not overriding, but this is not |
| 620 |
-- possible for Initialize, etc. because they have to be retrievable by |
| 621 |
-- name. Before generating the proper call to one of these operations we |
| 622 |
-- check whether Typ is known to be controlled at the point of definition. |
| 623 |
-- If it is not then we must retrieve the hidden operation of the parent |
| 624 |
-- and use it instead. This is one case that might be solved more cleanly |
| 625 |
-- once Overriding pragmas or declarations are in place. |
| 626 |
|
| 627 |
function Contains_Subprogram (Blk : Entity_Id) return Boolean; |
| 628 |
-- Check recursively whether a loop or block contains a subprogram that |
| 629 |
-- may need an activation record. |
| 630 |
|
| 631 |
function Convert_View |
| 632 |
(Proc : Entity_Id; |
| 633 |
Arg : Node_Id; |
| 634 |
Typ : Entity_Id) return Node_Id; |
| 635 |
-- Proc is one of the Initialize/Adjust/Finalize operations, Arg is the one |
| 636 |
-- argument being passed to it, and Typ is its expected type. This function |
| 637 |
-- will, if necessary, generate a conversion between the partial and full |
| 638 |
-- views of Arg to match the type of the formal of Proc, or else force a |
| 639 |
-- conversion to the class-wide type in the case where the operation is |
| 640 |
-- abstract. |
| 641 |
|
| 642 |
function Finalize_Address_For_Node (Node : Entity_Id) return Entity_Id |
| 643 |
renames Einfo.Entities.Finalization_Master_Node; |
| 644 |
-- Return the Finalize_Address primitive for the object that has been |
| 645 |
-- attached to a finalization Master_Node. |
| 646 |
|
| 647 |
function Make_Call |
| 648 |
(Loc : Source_Ptr; |
| 649 |
Proc_Id : Entity_Id; |
| 650 |
Param : Node_Id; |
| 651 |
Skip_Self : Boolean := False) return Node_Id; |
| 652 |
-- Subsidiary to Make_Adjust_Call and Make_Final_Call. Given the entity of |
| 653 |
-- routine [Deep_]Adjust or [Deep_]Finalize and an object parameter, create |
| 654 |
-- an adjust or finalization call. When flag Skip_Self is set, the related |
| 655 |
-- action has an effect on the components only (if any). |
| 656 |
|
| 657 |
function Make_Deep_Proc |
| 658 |
(Prim : Final_Primitives; |
| 659 |
Typ : Entity_Id; |
| 660 |
Stmts : List_Id) return Entity_Id; |
| 661 |
-- This function generates the tree for Deep_Initialize, Deep_Adjust or |
| 662 |
-- Deep_Finalize procedures according to the first parameter. These |
| 663 |
-- procedures operate on the type Typ. The Stmts parameter gives the |
| 664 |
-- body of the procedure. |
| 665 |
|
| 666 |
function Make_Deep_Array_Body |
| 667 |
(Prim : Final_Primitives; |
| 668 |
Typ : Entity_Id) return List_Id; |
| 669 |
-- This function generates the list of statements for implementing |
| 670 |
-- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures according to |
| 671 |
-- the first parameter, these procedures operate on the array type Typ. |
| 672 |
|
| 673 |
function Make_Deep_Record_Body |
| 674 |
(Prim : Final_Primitives; |
| 675 |
Typ : Entity_Id; |
| 676 |
Is_Local : Boolean := False) return List_Id; |
| 677 |
-- This function generates the list of statements for implementing |
| 678 |
-- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures according to |
| 679 |
-- the first parameter, these procedures operate on the record type Typ. |
| 680 |
-- Flag Is_Local is used in conjunction with Deep_Finalize to designate |
| 681 |
-- whether the inner logic should be dictated by state counters. |
| 682 |
|
| 683 |
function Make_Finalize_Address_Stmts (Typ : Entity_Id) return List_Id; |
| 684 |
-- Subsidiary to Make_Finalize_Address_Body, Make_Deep_Array_Body and |
| 685 |
-- Make_Deep_Record_Body. Generate the following statements: |
| 686 |
-- |
| 687 |
-- declare |
| 688 |
-- type Acc_Typ is access all Typ; |
| 689 |
-- for Acc_Typ'Storage_Size use 0; |
| 690 |
-- begin |
| 691 |
-- [Deep_]Finalize (Acc_Typ (V).all); |
| 692 |
-- end; |
| 693 |
|
| 694 |
procedure Set_Finalize_Address_For_Node (Node, Fin_Id : Entity_Id) |
| 695 |
renames Einfo.Entities.Set_Finalization_Master_Node; |
| 696 |
-- Set the Finalize_Address primitive for the object that has been |
| 697 |
-- attached to a finalization Master_Node. |
| 698 |
|
| 699 |
function Shift_Address_For_Descriptor |
| 700 |
(Addr : Node_Id; |
| 701 |
Typ : Entity_Id; |
| 702 |
Op_Nam : Name_Id) return Node_Id |
| 703 |
with Pre => Is_Array_Type (Typ) |
| 704 |
and then not Is_Constrained (Typ) |
| 705 |
and then Op_Nam in Name_Op_Add | Name_Op_Subtract; |
| 706 |
-- Add to Addr, or subtract from Addr, the size of the descriptor of Typ |
| 707 |
|
| 708 |
---------------------------------- |
| 709 |
-- Attach_Object_To_Master_Node -- |
| 710 |
---------------------------------- |
| 711 |
|
| 712 |
procedure Attach_Object_To_Master_Node |
| 713 |
(Obj_Decl : Node_Id; |
| 714 |
Master_Node : Entity_Id) |
| 715 |
is |
| 716 |
Loc : constant Source_Ptr := Sloc (Obj_Decl); |
| 717 |
Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl); |
| 718 |
Func_Id : constant Entity_Id := |
| 719 |
(if Is_Return_Object (Obj_Id) |
| 720 |
then Return_Applies_To (Scope (Obj_Id)) |
| 721 |
else Empty); |
| 722 |
|
| 723 |
function Build_BIP_Cleanup_Stmts |
| 724 |
(Func_Id : Entity_Id; |
| 725 |
Obj_Addr : Node_Id) return Node_Id; |
| 726 |
-- Func_Id denotes a build-in-place function. Generate the following |
| 727 |
-- cleanup code: |
| 728 |
-- |
| 729 |
-- if BIPallocform > Secondary_Stack'Pos |
| 730 |
-- and then BIPcollection /= null |
| 731 |
-- then |
| 732 |
-- declare |
| 733 |
-- type Ptr_Typ is access Fun_Typ; |
| 734 |
-- for Ptr_Typ'Storage_Pool use BIPstoragepool.all; |
| 735 |
-- |
| 736 |
-- begin |
| 737 |
-- Free (Ptr_Typ (Obj_Addr)); |
| 738 |
-- end; |
| 739 |
-- end if; |
| 740 |
-- |
| 741 |
-- Fun_Typ is the return type of the Func_Id. |
| 742 |
|
| 743 |
----------------------------- |
| 744 |
-- Build_BIP_Cleanup_Stmts -- |
| 745 |
----------------------------- |
| 746 |
|
| 747 |
function Build_BIP_Cleanup_Stmts |
| 748 |
(Func_Id : Entity_Id; |
| 749 |
Obj_Addr : Node_Id) return Node_Id |
| 750 |
is |
| 751 |
Alloc_Id : constant Entity_Id := |
| 752 |
Build_In_Place_Formal (Func_Id, BIP_Alloc_Form); |
| 753 |
Decls : constant List_Id := New_List; |
| 754 |
Fin_Coll_Id : constant Entity_Id := |
| 755 |
Build_In_Place_Formal (Func_Id, BIP_Collection); |
| 756 |
Func_Typ : constant Entity_Id := Etype (Func_Id); |
| 757 |
|
| 758 |
Cond : Node_Id; |
| 759 |
Free_Blk : Node_Id; |
| 760 |
Free_Stmt : Node_Id; |
| 761 |
Pool_Id : Entity_Id; |
| 762 |
Ptr_Typ : Entity_Id; |
| 763 |
|
| 764 |
begin |
| 765 |
-- Generate: |
| 766 |
-- Pool_Id renames BIPstoragepool.all; |
| 767 |
|
| 768 |
-- This formal is not added on ZFP as those targets do not |
| 769 |
-- support pools. |
| 770 |
|
| 771 |
if RTE_Available (RE_Root_Storage_Pool_Ptr) then |
| 772 |
Pool_Id := Make_Temporary (Loc, 'P'); |
| 773 |
|
| 774 |
Append_To (Decls, |
| 775 |
Make_Object_Renaming_Declaration (Loc, |
| 776 |
Defining_Identifier => Pool_Id, |
| 777 |
Subtype_Mark => |
| 778 |
New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc), |
| 779 |
Name => |
| 780 |
Make_Explicit_Dereference (Loc, |
| 781 |
New_Occurrence_Of |
| 782 |
(Build_In_Place_Formal |
| 783 |
(Func_Id, BIP_Storage_Pool), Loc)))); |
| 784 |
|
| 785 |
if Debug_Generated_Code then |
| 786 |
Set_Debug_Info_Needed (Pool_Id); |
| 787 |
end if; |
| 788 |
|
| 789 |
else |
| 790 |
Pool_Id := Empty; |
| 791 |
end if; |
| 792 |
|
| 793 |
-- Create an access type which uses the storage pool of the caller |
| 794 |
|
| 795 |
-- Generate: |
| 796 |
-- type Ptr_Typ is access Func_Typ; |
| 797 |
|
| 798 |
Ptr_Typ := Make_Temporary (Loc, 'P'); |
| 799 |
|
| 800 |
Append_To (Decls, |
| 801 |
Make_Full_Type_Declaration (Loc, |
| 802 |
Defining_Identifier => Ptr_Typ, |
| 803 |
Type_Definition => |
| 804 |
Make_Access_To_Object_Definition (Loc, |
| 805 |
Subtype_Indication => New_Occurrence_Of (Func_Typ, Loc)))); |
| 806 |
|
| 807 |
-- Perform minor decoration in order to set the collection and the |
| 808 |
-- storage pool attributes. |
| 809 |
|
| 810 |
Mutate_Ekind (Ptr_Typ, E_Access_Type); |
| 811 |
Set_Finalization_Collection (Ptr_Typ, Fin_Coll_Id); |
| 812 |
Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id); |
| 813 |
|
| 814 |
-- Create an explicit free statement. Note that the free uses the |
| 815 |
-- caller's pool expressed as a renaming. |
| 816 |
|
| 817 |
Free_Stmt := |
| 818 |
Make_Free_Statement (Loc, |
| 819 |
Expression => |
| 820 |
Unchecked_Convert_To (Ptr_Typ, Obj_Addr)); |
| 821 |
|
| 822 |
Set_Storage_Pool (Free_Stmt, Pool_Id); |
| 823 |
|
| 824 |
-- Create a block to house the dummy type and the instantiation as |
| 825 |
-- well as to perform the cleanup the temporary. |
| 826 |
|
| 827 |
-- Generate: |
| 828 |
-- declare |
| 829 |
-- <Decls> |
| 830 |
-- begin |
| 831 |
-- Free (Ptr_Typ (Obj_Addr)); |
| 832 |
-- end; |
| 833 |
|
| 834 |
Free_Blk := |
| 835 |
Make_Block_Statement (Loc, |
| 836 |
Declarations => Decls, |
| 837 |
Handled_Statement_Sequence => |
| 838 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 839 |
Statements => New_List (Free_Stmt))); |
| 840 |
|
| 841 |
-- Generate: |
| 842 |
-- if BIPallocform > Secondary_Stack'Pos |
| 843 |
-- and then BIPcollection /= null |
| 844 |
-- then |
| 845 |
|
| 846 |
Cond := |
| 847 |
Make_And_Then (Loc, |
| 848 |
Left_Opnd => |
| 849 |
Make_Op_Gt (Loc, |
| 850 |
Left_Opnd => New_Occurrence_Of (Alloc_Id, Loc), |
| 851 |
Right_Opnd => |
| 852 |
Make_Integer_Literal (Loc, |
| 853 |
UI_From_Int (BIP_Allocation_Form'Pos (Secondary_Stack)))), |
| 854 |
Right_Opnd => |
| 855 |
Make_Op_Ne (Loc, |
| 856 |
Left_Opnd => New_Occurrence_Of (Fin_Coll_Id, Loc), |
| 857 |
Right_Opnd => Make_Null (Loc))); |
| 858 |
|
| 859 |
-- Generate: |
| 860 |
-- if <Cond> then |
| 861 |
-- <Free_Blk> |
| 862 |
-- end if; |
| 863 |
|
| 864 |
return |
| 865 |
Make_If_Statement (Loc, |
| 866 |
Condition => Cond, |
| 867 |
Then_Statements => New_List (Free_Blk)); |
| 868 |
end Build_BIP_Cleanup_Stmts; |
| 869 |
|
| 870 |
-- Local variables |
| 871 |
|
| 872 |
Fin_Id : Entity_Id; |
| 873 |
Master_Node_Attach : Node_Id; |
| 874 |
Master_Node_Ins : Node_Id; |
| 875 |
Obj_Ref : Node_Id; |
| 876 |
Obj_Typ : Entity_Id; |
| 877 |
|
| 878 |
-- Start of processing for Attach_Object_To_Master_Node |
| 879 |
|
| 880 |
begin |
| 881 |
-- Finalize_Address is not generated in CodePeer mode because the |
| 882 |
-- body contains address arithmetic. So we don't want to generate |
| 883 |
-- the attach in this case. |
| 884 |
|
| 885 |
if CodePeer_Mode then |
| 886 |
return; |
| 887 |
end if; |
| 888 |
|
| 889 |
-- When the object is initialized by an aggregate, the attachment must |
| 890 |
-- occur after the last aggregate assignment takes place; only then is |
| 891 |
-- the object considered initialized. Likewise if it is initialized by |
| 892 |
-- a build-in-place call: we must attach only after the call. |
| 893 |
|
| 894 |
if Ekind (Obj_Id) in E_Constant | E_Variable then |
| 895 |
if Present (Last_Aggregate_Assignment (Obj_Id)) then |
| 896 |
Master_Node_Ins := Last_Aggregate_Assignment (Obj_Id); |
| 897 |
elsif Present (BIP_Initialization_Call (Obj_Id)) then |
| 898 |
Master_Node_Ins := BIP_Initialization_Call (Obj_Id); |
| 899 |
else |
| 900 |
Master_Node_Ins := Obj_Decl; |
| 901 |
end if; |
| 902 |
|
| 903 |
else |
| 904 |
Master_Node_Ins := Obj_Decl; |
| 905 |
end if; |
| 906 |
|
| 907 |
-- Handle the object type and the reference to the object |
| 908 |
|
| 909 |
Obj_Ref := New_Occurrence_Of (Obj_Id, Loc); |
| 910 |
Obj_Typ := Etype (Obj_Id); |
| 911 |
if not Is_Class_Wide_Type (Obj_Typ) then |
| 912 |
Obj_Typ := Base_Type (Obj_Typ); |
| 913 |
end if; |
| 914 |
|
| 915 |
if Is_Access_Type (Obj_Typ) then |
| 916 |
Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref); |
| 917 |
Obj_Typ := Available_View (Designated_Type (Obj_Typ)); |
| 918 |
end if; |
| 919 |
|
| 920 |
-- If we are dealing with a return object of a build-in-place function |
| 921 |
-- and its allocation has been done in the function, we additionally |
| 922 |
-- need to detach it from the caller's finalization collection in order |
| 923 |
-- to prevent double finalization. |
| 924 |
|
| 925 |
if Present (Func_Id) |
| 926 |
and then Is_Build_In_Place_Function (Func_Id) |
| 927 |
and then Needs_BIP_Collection (Func_Id) |
| 928 |
then |
| 929 |
declare |
| 930 |
Ptr_Typ : constant Node_Id := Make_Temporary (Loc, 'P'); |
| 931 |
Param : constant Entity_Id := |
| 932 |
Make_Defining_Identifier (Loc, Name_V); |
| 933 |
|
| 934 |
Fin_Body : Node_Id; |
| 935 |
Fin_Stmts : List_Id; |
| 936 |
|
| 937 |
begin |
| 938 |
Fin_Stmts := Make_Finalize_Address_Stmts (Obj_Typ); |
| 939 |
|
| 940 |
Append_To (Fin_Stmts, |
| 941 |
Build_BIP_Cleanup_Stmts |
| 942 |
(Func_Id, New_Occurrence_Of (Param, Loc))); |
| 943 |
|
| 944 |
Fin_Id := |
| 945 |
Make_Defining_Identifier (Loc, |
| 946 |
Make_TSS_Name_Local |
| 947 |
(Obj_Typ, TSS_Finalize_Address)); |
| 948 |
|
| 949 |
Fin_Body := |
| 950 |
Make_Subprogram_Body (Loc, |
| 951 |
Specification => |
| 952 |
Make_Procedure_Specification (Loc, |
| 953 |
Defining_Unit_Name => Fin_Id, |
| 954 |
Parameter_Specifications => New_List ( |
| 955 |
Make_Parameter_Specification (Loc, |
| 956 |
Defining_Identifier => Param, |
| 957 |
Parameter_Type => |
| 958 |
New_Occurrence_Of (RTE (RE_Address), Loc)))), |
| 959 |
|
| 960 |
Declarations => New_List ( |
| 961 |
Make_Full_Type_Declaration (Loc, |
| 962 |
Defining_Identifier => Ptr_Typ, |
| 963 |
Type_Definition => |
| 964 |
Make_Access_To_Object_Definition (Loc, |
| 965 |
All_Present => True, |
| 966 |
Subtype_Indication => |
| 967 |
New_Occurrence_Of (Obj_Typ, Loc)))), |
| 968 |
|
| 969 |
Handled_Statement_Sequence => |
| 970 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 971 |
Statements => Fin_Stmts)); |
| 972 |
|
| 973 |
Insert_After_And_Analyze |
| 974 |
(Master_Node_Ins, Fin_Body, Suppress => All_Checks); |
| 975 |
|
| 976 |
Master_Node_Ins := Fin_Body; |
| 977 |
end; |
| 978 |
|
| 979 |
else |
| 980 |
Fin_Id := Finalize_Address (Obj_Typ); |
| 981 |
|
| 982 |
if No (Fin_Id) and then Ekind (Obj_Typ) = E_Class_Wide_Subtype then |
| 983 |
Fin_Id := TSS (Obj_Typ, TSS_Finalize_Address); |
| 984 |
end if; |
| 985 |
end if; |
| 986 |
|
| 987 |
-- Now build the attachment call that will initialize the object's |
| 988 |
-- Master_Node using the object's address and finalization procedure. |
| 989 |
|
| 990 |
Master_Node_Attach := |
| 991 |
Make_Procedure_Call_Statement (Loc, |
| 992 |
Name => |
| 993 |
New_Occurrence_Of (RTE (RE_Attach_Object_To_Node), Loc), |
| 994 |
Parameter_Associations => New_List ( |
| 995 |
Make_Address_For_Finalize (Loc, Obj_Ref, Obj_Typ), |
| 996 |
Make_Attribute_Reference (Loc, |
| 997 |
Prefix => |
| 998 |
New_Occurrence_Of (Fin_Id, Loc), |
| 999 |
Attribute_Name => Name_Unrestricted_Access), |
| 1000 |
New_Occurrence_Of (Master_Node, Loc))); |
| 1001 |
|
| 1002 |
Set_Finalize_Address_For_Node (Master_Node, Fin_Id); |
| 1003 |
|
| 1004 |
-- Propagate the Ghost policy from the procedure to the node |
| 1005 |
|
| 1006 |
Set_Is_Ignored_Ghost_Entity |
| 1007 |
(Master_Node, Is_Ignored_Ghost_Entity (Fin_Id)); |
| 1008 |
|
| 1009 |
Insert_After_And_Analyze |
| 1010 |
(Master_Node_Ins, Master_Node_Attach, Suppress => All_Checks); |
| 1011 |
end Attach_Object_To_Master_Node; |
| 1012 |
|
| 1013 |
------------------------------------ |
| 1014 |
-- Allows_Finalization_Collection -- |
| 1015 |
------------------------------------ |
| 1016 |
|
| 1017 |
function Allows_Finalization_Collection (Typ : Entity_Id) return Boolean is |
| 1018 |
function In_Deallocation_Instance (E : Entity_Id) return Boolean; |
| 1019 |
-- Determine whether entity E is inside a wrapper package created for |
| 1020 |
-- an instance of Ada.Unchecked_Deallocation. |
| 1021 |
|
| 1022 |
------------------------------ |
| 1023 |
-- In_Deallocation_Instance -- |
| 1024 |
------------------------------ |
| 1025 |
|
| 1026 |
function In_Deallocation_Instance (E : Entity_Id) return Boolean is |
| 1027 |
Pkg : constant Entity_Id := Scope (E); |
| 1028 |
Par : Node_Id := Empty; |
| 1029 |
|
| 1030 |
begin |
| 1031 |
if Ekind (Pkg) = E_Package |
| 1032 |
and then Present (Related_Instance (Pkg)) |
| 1033 |
and then Ekind (Related_Instance (Pkg)) = E_Procedure |
| 1034 |
then |
| 1035 |
Par := Generic_Parent (Parent (Related_Instance (Pkg))); |
| 1036 |
|
| 1037 |
return |
| 1038 |
Present (Par) |
| 1039 |
and then Chars (Par) = Name_Unchecked_Deallocation |
| 1040 |
and then Chars (Scope (Par)) = Name_Ada |
| 1041 |
and then Scope (Scope (Par)) = Standard_Standard; |
| 1042 |
end if; |
| 1043 |
|
| 1044 |
return False; |
| 1045 |
end In_Deallocation_Instance; |
| 1046 |
|
| 1047 |
-- Local variables |
| 1048 |
|
| 1049 |
Desig_Typ : constant Entity_Id := Designated_Type (Typ); |
| 1050 |
Ptr_Typ : constant Entity_Id := |
| 1051 |
Root_Type_Of_Full_View (Base_Type (Typ)); |
| 1052 |
|
| 1053 |
-- Start of processing for Allows_Finalization_Collection |
| 1054 |
|
| 1055 |
begin |
| 1056 |
-- Certain run-time configurations and targets do not provide support |
| 1057 |
-- for controlled types and therefore do not need collections. |
| 1058 |
|
| 1059 |
if Restriction_Active (No_Finalization) then |
| 1060 |
return False; |
| 1061 |
|
| 1062 |
-- Do not consider C and C++ types since it is assumed that the non-Ada |
| 1063 |
-- side will handle their cleanup. |
| 1064 |
|
| 1065 |
elsif Convention (Desig_Typ) = Convention_C |
| 1066 |
or else Convention (Desig_Typ) = Convention_CPP |
| 1067 |
then |
| 1068 |
return False; |
| 1069 |
|
| 1070 |
-- Do not consider controlled types with relaxed finalization |
| 1071 |
|
| 1072 |
elsif Has_Relaxed_Finalization (Desig_Typ) then |
| 1073 |
return False; |
| 1074 |
|
| 1075 |
-- Do not consider an access type that returns on the secondary stack |
| 1076 |
|
| 1077 |
elsif Present (Associated_Storage_Pool (Ptr_Typ)) |
| 1078 |
and then Is_RTE (Associated_Storage_Pool (Ptr_Typ), RE_SS_Pool) |
| 1079 |
then |
| 1080 |
return False; |
| 1081 |
|
| 1082 |
-- Do not consider an access type that can never allocate an object |
| 1083 |
|
| 1084 |
elsif No_Pool_Assigned (Ptr_Typ) then |
| 1085 |
return False; |
| 1086 |
|
| 1087 |
-- Do not consider an access type coming from an Unchecked_Deallocation |
| 1088 |
-- instance. Even though the designated type may be controlled, the |
| 1089 |
-- access type will never participate in any allocations. |
| 1090 |
|
| 1091 |
elsif In_Deallocation_Instance (Ptr_Typ) then |
| 1092 |
return False; |
| 1093 |
|
| 1094 |
-- Do not consider a non-library access type when No_Nested_Finalization |
| 1095 |
-- is in effect, because finalization collections are controlled objects |
| 1096 |
-- and, if created, will violate the restriction. |
| 1097 |
|
| 1098 |
elsif Restriction_Active (No_Nested_Finalization) |
| 1099 |
and then not Is_Library_Level_Entity (Ptr_Typ) |
| 1100 |
then |
| 1101 |
return False; |
| 1102 |
|
| 1103 |
-- Do not consider an access type subject to pragma No_Heap_Finalization |
| 1104 |
-- because objects allocated through such a type are not to be finalized |
| 1105 |
-- when the access type goes out of scope. |
| 1106 |
|
| 1107 |
elsif No_Heap_Finalization (Ptr_Typ) then |
| 1108 |
return False; |
| 1109 |
|
| 1110 |
-- Do not create finalization collections in GNATprove mode because this |
| 1111 |
-- causes unwanted extra expansion. Compilation in this mode must always |
| 1112 |
-- keep the tree as close as possible to the original sources. |
| 1113 |
|
| 1114 |
elsif GNATprove_Mode then |
| 1115 |
return False; |
| 1116 |
|
| 1117 |
-- Otherwise the access type may use a finalization collection |
| 1118 |
|
| 1119 |
else |
| 1120 |
return True; |
| 1121 |
end if; |
| 1122 |
end Allows_Finalization_Collection; |
| 1123 |
|
| 1124 |
-------------------------------- |
| 1125 |
-- Build_Anonymous_Collection -- |
| 1126 |
-------------------------------- |
| 1127 |
|
| 1128 |
procedure Build_Anonymous_Collection (Ptr_Typ : Entity_Id) is |
| 1129 |
function Create_Anonymous_Collection |
| 1130 |
(Desig_Typ : Entity_Id; |
| 1131 |
Unit_Id : Entity_Id; |
| 1132 |
Unit_Decl : Node_Id) return Entity_Id; |
| 1133 |
-- Create a new anonymous collection for access type Ptr_Typ with |
| 1134 |
-- designated type Desig_Typ. The declaration of the collection and |
| 1135 |
-- its initialization are inserted in the declarative part of unit |
| 1136 |
-- Unit_Decl. Unit_Id is the entity of Unit_Decl. |
| 1137 |
|
| 1138 |
function Current_Anonymous_Collection |
| 1139 |
(Desig_Typ : Entity_Id; |
| 1140 |
Unit_Id : Entity_Id) return Entity_Id; |
| 1141 |
-- Find an anonymous collection declared in unit Unit_Id which services |
| 1142 |
-- designated type Desig_Typ. If there is none, return Empty. |
| 1143 |
|
| 1144 |
--------------------------------- |
| 1145 |
-- Create_Anonymous_Collection -- |
| 1146 |
--------------------------------- |
| 1147 |
|
| 1148 |
function Create_Anonymous_Collection |
| 1149 |
(Desig_Typ : Entity_Id; |
| 1150 |
Unit_Id : Entity_Id; |
| 1151 |
Unit_Decl : Node_Id) return Entity_Id |
| 1152 |
is |
| 1153 |
Loc : constant Source_Ptr := Sloc (Unit_Id); |
| 1154 |
|
| 1155 |
All_FCs : Elist_Id; |
| 1156 |
Decls : List_Id; |
| 1157 |
FC_Decl : Node_Id; |
| 1158 |
FC_Id : Entity_Id; |
| 1159 |
Unit_Spec : Node_Id; |
| 1160 |
|
| 1161 |
begin |
| 1162 |
-- Generate: |
| 1163 |
-- <FC_Id> : Finalization_Collection; |
| 1164 |
|
| 1165 |
FC_Id := Make_Temporary (Loc, 'A'); |
| 1166 |
|
| 1167 |
FC_Decl := |
| 1168 |
Make_Object_Declaration (Loc, |
| 1169 |
Defining_Identifier => FC_Id, |
| 1170 |
Object_Definition => |
| 1171 |
New_Occurrence_Of (RTE (RE_Finalization_Collection), Loc)); |
| 1172 |
|
| 1173 |
-- Find the declarative list of the unit |
| 1174 |
|
| 1175 |
if Nkind (Unit_Decl) = N_Package_Declaration then |
| 1176 |
Unit_Spec := Specification (Unit_Decl); |
| 1177 |
Decls := Visible_Declarations (Unit_Spec); |
| 1178 |
|
| 1179 |
if No (Decls) then |
| 1180 |
Decls := New_List; |
| 1181 |
Set_Visible_Declarations (Unit_Spec, Decls); |
| 1182 |
end if; |
| 1183 |
|
| 1184 |
-- Package body or subprogram case |
| 1185 |
|
| 1186 |
-- ??? A subprogram spec or body that acts as a compilation unit may |
| 1187 |
-- contain a formal parameter of an anonymous access-to-controlled |
| 1188 |
-- type initialized by an allocator. |
| 1189 |
|
| 1190 |
-- procedure Comp_Unit_Proc (Param : access Ctrl := new Ctrl); |
| 1191 |
|
| 1192 |
-- There is no suitable place to create the collection because the |
| 1193 |
-- subprogram is not in a declarative list. |
| 1194 |
|
| 1195 |
else |
| 1196 |
Decls := Declarations (Unit_Decl); |
| 1197 |
|
| 1198 |
if No (Decls) then |
| 1199 |
Decls := New_List; |
| 1200 |
Set_Declarations (Unit_Decl, Decls); |
| 1201 |
end if; |
| 1202 |
end if; |
| 1203 |
|
| 1204 |
Prepend_To (Decls, FC_Decl); |
| 1205 |
|
| 1206 |
-- Use the scope of the unit when analyzing the declaration of the |
| 1207 |
-- collection and its initialization actions. |
| 1208 |
|
| 1209 |
Push_Scope (Unit_Id); |
| 1210 |
Analyze (FC_Decl); |
| 1211 |
Pop_Scope; |
| 1212 |
|
| 1213 |
-- Mark the collection as servicing this specific designated type |
| 1214 |
|
| 1215 |
Set_Anonymous_Designated_Type (FC_Id, Desig_Typ); |
| 1216 |
|
| 1217 |
-- Include it in the list of existing anonymous collections which |
| 1218 |
-- appear in this unit. This effectively creates a mapping between |
| 1219 |
-- collections and designated types, which in turn allows for the |
| 1220 |
-- reuse of collections on a per-unit basis. |
| 1221 |
|
| 1222 |
All_FCs := Anonymous_Collections (Unit_Id); |
| 1223 |
|
| 1224 |
if No (All_FCs) then |
| 1225 |
All_FCs := New_Elmt_List; |
| 1226 |
Set_Anonymous_Collections (Unit_Id, All_FCs); |
| 1227 |
end if; |
| 1228 |
|
| 1229 |
Prepend_Elmt (FC_Id, All_FCs); |
| 1230 |
|
| 1231 |
return FC_Id; |
| 1232 |
end Create_Anonymous_Collection; |
| 1233 |
|
| 1234 |
---------------------------------- |
| 1235 |
-- Current_Anonymous_Collection -- |
| 1236 |
---------------------------------- |
| 1237 |
|
| 1238 |
function Current_Anonymous_Collection |
| 1239 |
(Desig_Typ : Entity_Id; |
| 1240 |
Unit_Id : Entity_Id) return Entity_Id |
| 1241 |
is |
| 1242 |
All_FCs : constant Elist_Id := Anonymous_Collections (Unit_Id); |
| 1243 |
FC_Elmt : Elmt_Id; |
| 1244 |
FC_Id : Entity_Id; |
| 1245 |
|
| 1246 |
begin |
| 1247 |
-- Inspect the list of anonymous collections declared within the unit |
| 1248 |
-- looking for an existing collection which services the designated |
| 1249 |
-- type. |
| 1250 |
|
| 1251 |
if Present (All_FCs) then |
| 1252 |
FC_Elmt := First_Elmt (All_FCs); |
| 1253 |
while Present (FC_Elmt) loop |
| 1254 |
FC_Id := Node (FC_Elmt); |
| 1255 |
|
| 1256 |
-- The current collection services the same designated type. |
| 1257 |
-- As a result, the collection can be reused and associated |
| 1258 |
-- with another anonymous access-to-controlled type. |
| 1259 |
|
| 1260 |
if Anonymous_Designated_Type (FC_Id) = Desig_Typ then |
| 1261 |
return FC_Id; |
| 1262 |
end if; |
| 1263 |
|
| 1264 |
Next_Elmt (FC_Elmt); |
| 1265 |
end loop; |
| 1266 |
end if; |
| 1267 |
|
| 1268 |
return Empty; |
| 1269 |
end Current_Anonymous_Collection; |
| 1270 |
|
| 1271 |
-- Local variables |
| 1272 |
|
| 1273 |
Desig_Typ : Entity_Id; |
| 1274 |
FC_Id : Entity_Id; |
| 1275 |
Priv_View : Entity_Id; |
| 1276 |
Scop : Entity_Id; |
| 1277 |
Unit_Decl : Node_Id; |
| 1278 |
Unit_Id : Entity_Id; |
| 1279 |
|
| 1280 |
-- Start of processing for Build_Anonymous_Collection |
| 1281 |
|
| 1282 |
begin |
| 1283 |
-- Nothing to do if the circumstances do not allow for a finalization |
| 1284 |
-- collection. |
| 1285 |
|
| 1286 |
if not Allows_Finalization_Collection (Ptr_Typ) then |
| 1287 |
return; |
| 1288 |
end if; |
| 1289 |
|
| 1290 |
Unit_Decl := Unit (Cunit (Current_Sem_Unit)); |
| 1291 |
Unit_Id := Unique_Defining_Entity (Unit_Decl); |
| 1292 |
|
| 1293 |
-- The compilation unit is a package instantiation. In this case the |
| 1294 |
-- anonymous collection is associated with the package spec, as both |
| 1295 |
-- the spec and body appear at the same level. |
| 1296 |
|
| 1297 |
if Nkind (Unit_Decl) = N_Package_Body |
| 1298 |
and then Nkind (Original_Node (Unit_Decl)) = N_Package_Instantiation |
| 1299 |
then |
| 1300 |
Unit_Id := Corresponding_Spec (Unit_Decl); |
| 1301 |
Unit_Decl := Unit_Declaration_Node (Unit_Id); |
| 1302 |
end if; |
| 1303 |
|
| 1304 |
-- Use the initial declaration of the designated type when it denotes |
| 1305 |
-- the full view of an incomplete or private type. This ensures that |
| 1306 |
-- types with one and two views are treated the same. |
| 1307 |
|
| 1308 |
Desig_Typ := Directly_Designated_Type (Ptr_Typ); |
| 1309 |
Priv_View := Incomplete_Or_Partial_View (Desig_Typ); |
| 1310 |
|
| 1311 |
if Present (Priv_View) then |
| 1312 |
Desig_Typ := Priv_View; |
| 1313 |
end if; |
| 1314 |
|
| 1315 |
-- For a designated type not declared at library level, we cannot create |
| 1316 |
-- a finalization collection attached to an outer unit since this would |
| 1317 |
-- generate dangling references to the dynamic scope through access-to- |
| 1318 |
-- procedure values designating the local Finalize_Address primitive. |
| 1319 |
|
| 1320 |
Scop := Enclosing_Dynamic_Scope (Desig_Typ); |
| 1321 |
if Scop /= Standard_Standard |
| 1322 |
and then Scope_Depth (Scop) > Scope_Depth (Unit_Id) |
| 1323 |
then |
| 1324 |
return; |
| 1325 |
end if; |
| 1326 |
|
| 1327 |
-- For the access result type of a function that is a library unit, |
| 1328 |
-- we cannot create a finalization collection attached to the unit as |
| 1329 |
-- this would cause premature finalization of objects created through |
| 1330 |
-- the access result type, which may be returned from the function. |
| 1331 |
|
| 1332 |
if Is_Local_Anonymous_Access (Ptr_Typ) |
| 1333 |
and then Ekind (Unit_Id) = E_Function |
| 1334 |
and then Parent (Ptr_Typ) = |
| 1335 |
Result_Definition (Subprogram_Specification (Unit_Id)) |
| 1336 |
then |
| 1337 |
return; |
| 1338 |
end if; |
| 1339 |
|
| 1340 |
-- Determine whether the current semantic unit already has an anonymous |
| 1341 |
-- collection which services the designated type. |
| 1342 |
|
| 1343 |
FC_Id := Current_Anonymous_Collection (Desig_Typ, Unit_Id); |
| 1344 |
|
| 1345 |
-- If this is not the case, create a new collection |
| 1346 |
|
| 1347 |
if No (FC_Id) then |
| 1348 |
FC_Id := Create_Anonymous_Collection (Desig_Typ, Unit_Id, Unit_Decl); |
| 1349 |
end if; |
| 1350 |
|
| 1351 |
Set_Finalization_Collection (Ptr_Typ, FC_Id); |
| 1352 |
end Build_Anonymous_Collection; |
| 1353 |
|
| 1354 |
---------------------------- |
| 1355 |
-- Build_Array_Deep_Procs -- |
| 1356 |
---------------------------- |
| 1357 |
|
| 1358 |
procedure Build_Array_Deep_Procs (Typ : Entity_Id) is |
| 1359 |
begin |
| 1360 |
Set_TSS (Typ, |
| 1361 |
Make_Deep_Proc |
| 1362 |
(Prim => Initialize_Case, |
| 1363 |
Typ => Typ, |
| 1364 |
Stmts => Make_Deep_Array_Body (Initialize_Case, Typ))); |
| 1365 |
|
| 1366 |
if not Is_Inherently_Limited_Type (Typ) then |
| 1367 |
Set_TSS (Typ, |
| 1368 |
Make_Deep_Proc |
| 1369 |
(Prim => Adjust_Case, |
| 1370 |
Typ => Typ, |
| 1371 |
Stmts => Make_Deep_Array_Body (Adjust_Case, Typ))); |
| 1372 |
end if; |
| 1373 |
|
| 1374 |
-- Do not generate Deep_Finalize and Finalize_Address if finalization is |
| 1375 |
-- suppressed since these routine will not be used. |
| 1376 |
|
| 1377 |
if not Restriction_Active (No_Finalization) then |
| 1378 |
Set_TSS (Typ, |
| 1379 |
Make_Deep_Proc |
| 1380 |
(Prim => Finalize_Case, |
| 1381 |
Typ => Typ, |
| 1382 |
Stmts => Make_Deep_Array_Body (Finalize_Case, Typ))); |
| 1383 |
|
| 1384 |
-- Create TSS primitive Finalize_Address (unless CodePeer_Mode) |
| 1385 |
|
| 1386 |
if not CodePeer_Mode then |
| 1387 |
Set_TSS (Typ, |
| 1388 |
Make_Deep_Proc |
| 1389 |
(Prim => Address_Case, |
| 1390 |
Typ => Typ, |
| 1391 |
Stmts => Make_Deep_Array_Body (Address_Case, Typ))); |
| 1392 |
end if; |
| 1393 |
end if; |
| 1394 |
end Build_Array_Deep_Procs; |
| 1395 |
|
| 1396 |
------------------------------ |
| 1397 |
-- Build_Cleanup_Statements -- |
| 1398 |
------------------------------ |
| 1399 |
|
| 1400 |
function Build_Cleanup_Statements |
| 1401 |
(N : Node_Id; |
| 1402 |
Additional_Cleanup : List_Id) return List_Id |
| 1403 |
is |
| 1404 |
Is_Asynchronous_Call : constant Boolean := |
| 1405 |
Nkind (N) = N_Block_Statement and then Is_Asynchronous_Call_Block (N); |
| 1406 |
Is_Master : constant Boolean := |
| 1407 |
Nkind (N) /= N_Entry_Body and then Is_Task_Master (N); |
| 1408 |
Is_Protected_Subp_Body : constant Boolean := |
| 1409 |
Nkind (N) = N_Subprogram_Body |
| 1410 |
and then Is_Protected_Subprogram_Body (N); |
| 1411 |
Is_Task_Allocation : constant Boolean := |
| 1412 |
Nkind (N) = N_Block_Statement and then Is_Task_Allocation_Block (N); |
| 1413 |
Is_Task_Body : constant Boolean := |
| 1414 |
Nkind (Original_Node (N)) = N_Task_Body; |
| 1415 |
|
| 1416 |
Loc : constant Source_Ptr := Sloc (N); |
| 1417 |
Stmts : constant List_Id := New_List; |
| 1418 |
|
| 1419 |
begin |
| 1420 |
if Is_Task_Body then |
| 1421 |
if Restricted_Profile then |
| 1422 |
Append_To (Stmts, |
| 1423 |
Build_Runtime_Call (Loc, RE_Complete_Restricted_Task)); |
| 1424 |
else |
| 1425 |
Append_To (Stmts, Build_Runtime_Call (Loc, RE_Complete_Task)); |
| 1426 |
end if; |
| 1427 |
|
| 1428 |
elsif Is_Master then |
| 1429 |
if Restriction_Active (No_Task_Hierarchy) = False then |
| 1430 |
Append_To (Stmts, Build_Runtime_Call (Loc, RE_Complete_Master)); |
| 1431 |
end if; |
| 1432 |
|
| 1433 |
-- Add statements to undefer abort. |
| 1434 |
|
| 1435 |
elsif Is_Protected_Subp_Body then |
| 1436 |
if Abort_Allowed then |
| 1437 |
Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer)); |
| 1438 |
end if; |
| 1439 |
|
| 1440 |
-- Add a call to Expunge_Unactivated_Tasks for dynamically allocated |
| 1441 |
-- tasks. Other unactivated tasks are completed by Complete_Task or |
| 1442 |
-- Complete_Master. |
| 1443 |
|
| 1444 |
-- NOTE: The generated code references _chain, a local object |
| 1445 |
|
| 1446 |
elsif Is_Task_Allocation then |
| 1447 |
|
| 1448 |
-- Generate: |
| 1449 |
-- Expunge_Unactivated_Tasks (_chain); |
| 1450 |
|
| 1451 |
-- where _chain is the list of tasks created by the allocator but not |
| 1452 |
-- yet activated. This list will be empty unless the block completes |
| 1453 |
-- abnormally. |
| 1454 |
|
| 1455 |
Append_To (Stmts, |
| 1456 |
Make_Procedure_Call_Statement (Loc, |
| 1457 |
Name => |
| 1458 |
New_Occurrence_Of |
| 1459 |
(RTE (RE_Expunge_Unactivated_Tasks), Loc), |
| 1460 |
Parameter_Associations => New_List ( |
| 1461 |
New_Occurrence_Of (Activation_Chain_Entity (N), Loc)))); |
| 1462 |
|
| 1463 |
-- Attempt to cancel an asynchronous entry call whenever the block which |
| 1464 |
-- contains the abortable part is exited. |
| 1465 |
|
| 1466 |
-- NOTE: The generated code references Cnn, a local object |
| 1467 |
|
| 1468 |
elsif Is_Asynchronous_Call then |
| 1469 |
declare |
| 1470 |
Cancel_Param : constant Entity_Id := |
| 1471 |
Entry_Cancel_Parameter (Entity (Identifier (N))); |
| 1472 |
|
| 1473 |
begin |
| 1474 |
-- If it is of type Communication_Block, this must be a protected |
| 1475 |
-- entry call. Generate: |
| 1476 |
|
| 1477 |
-- if Enqueued (Cancel_Param) then |
| 1478 |
-- Cancel_Protected_Entry_Call (Cancel_Param); |
| 1479 |
-- end if; |
| 1480 |
|
| 1481 |
if Is_RTE (Etype (Cancel_Param), RE_Communication_Block) then |
| 1482 |
Append_To (Stmts, |
| 1483 |
Make_If_Statement (Loc, |
| 1484 |
Condition => |
| 1485 |
Make_Function_Call (Loc, |
| 1486 |
Name => |
| 1487 |
New_Occurrence_Of (RTE (RE_Enqueued), Loc), |
| 1488 |
Parameter_Associations => New_List ( |
| 1489 |
New_Occurrence_Of (Cancel_Param, Loc))), |
| 1490 |
|
| 1491 |
Then_Statements => New_List ( |
| 1492 |
Make_Procedure_Call_Statement (Loc, |
| 1493 |
Name => |
| 1494 |
New_Occurrence_Of |
| 1495 |
(RTE (RE_Cancel_Protected_Entry_Call), Loc), |
| 1496 |
Parameter_Associations => New_List ( |
| 1497 |
New_Occurrence_Of (Cancel_Param, Loc)))))); |
| 1498 |
|
| 1499 |
-- Asynchronous delay, generate: |
| 1500 |
-- Cancel_Async_Delay (Cancel_Param); |
| 1501 |
|
| 1502 |
elsif Is_RTE (Etype (Cancel_Param), RE_Delay_Block) then |
| 1503 |
Append_To (Stmts, |
| 1504 |
Make_Procedure_Call_Statement (Loc, |
| 1505 |
Name => |
| 1506 |
New_Occurrence_Of (RTE (RE_Cancel_Async_Delay), Loc), |
| 1507 |
Parameter_Associations => New_List ( |
| 1508 |
Make_Attribute_Reference (Loc, |
| 1509 |
Prefix => |
| 1510 |
New_Occurrence_Of (Cancel_Param, Loc), |
| 1511 |
Attribute_Name => Name_Unchecked_Access)))); |
| 1512 |
|
| 1513 |
-- Task entry call, generate: |
| 1514 |
-- Cancel_Task_Entry_Call (Cancel_Param); |
| 1515 |
|
| 1516 |
else |
| 1517 |
Append_To (Stmts, |
| 1518 |
Make_Procedure_Call_Statement (Loc, |
| 1519 |
Name => |
| 1520 |
New_Occurrence_Of (RTE (RE_Cancel_Task_Entry_Call), Loc), |
| 1521 |
Parameter_Associations => New_List ( |
| 1522 |
New_Occurrence_Of (Cancel_Param, Loc)))); |
| 1523 |
end if; |
| 1524 |
end; |
| 1525 |
end if; |
| 1526 |
|
| 1527 |
Append_List_To (Stmts, Additional_Cleanup); |
| 1528 |
return Stmts; |
| 1529 |
end Build_Cleanup_Statements; |
| 1530 |
|
| 1531 |
----------------------------- |
| 1532 |
-- Build_Controlling_Procs -- |
| 1533 |
----------------------------- |
| 1534 |
|
| 1535 |
procedure Build_Controlling_Procs (Typ : Entity_Id) is |
| 1536 |
begin |
| 1537 |
if Is_Array_Type (Typ) then |
| 1538 |
Build_Array_Deep_Procs (Typ); |
| 1539 |
else pragma Assert (Is_Record_Type (Typ)); |
| 1540 |
Build_Record_Deep_Procs (Typ); |
| 1541 |
end if; |
| 1542 |
end Build_Controlling_Procs; |
| 1543 |
|
| 1544 |
----------------------------- |
| 1545 |
-- Build_Exception_Handler -- |
| 1546 |
----------------------------- |
| 1547 |
|
| 1548 |
function Build_Exception_Handler |
| 1549 |
(Data : Finalization_Exception_Data; |
| 1550 |
For_Library : Boolean := False) return Node_Id |
| 1551 |
is |
| 1552 |
Actuals : List_Id; |
| 1553 |
Proc_To_Call : Entity_Id; |
| 1554 |
Except : Node_Id; |
| 1555 |
Stmts : List_Id; |
| 1556 |
|
| 1557 |
begin |
| 1558 |
pragma Assert (Present (Data.Raised_Id)); |
| 1559 |
|
| 1560 |
if Exception_Extra_Info |
| 1561 |
or else (For_Library and not Restricted_Profile) |
| 1562 |
then |
| 1563 |
if Exception_Extra_Info then |
| 1564 |
|
| 1565 |
-- Generate: |
| 1566 |
|
| 1567 |
-- Get_Current_Excep.all |
| 1568 |
|
| 1569 |
Except := |
| 1570 |
Make_Function_Call (Data.Loc, |
| 1571 |
Name => |
| 1572 |
Make_Explicit_Dereference (Data.Loc, |
| 1573 |
Prefix => |
| 1574 |
New_Occurrence_Of |
| 1575 |
(RTE (RE_Get_Current_Excep), Data.Loc))); |
| 1576 |
|
| 1577 |
else |
| 1578 |
-- Generate: |
| 1579 |
|
| 1580 |
-- null |
| 1581 |
|
| 1582 |
Except := Make_Null (Data.Loc); |
| 1583 |
end if; |
| 1584 |
|
| 1585 |
if For_Library and then not Restricted_Profile then |
| 1586 |
Proc_To_Call := RTE (RE_Save_Library_Occurrence); |
| 1587 |
Actuals := New_List (Except); |
| 1588 |
|
| 1589 |
else |
| 1590 |
Proc_To_Call := RTE (RE_Save_Occurrence); |
| 1591 |
|
| 1592 |
-- The dereference occurs only when Exception_Extra_Info is true, |
| 1593 |
-- and therefore Except is not null. |
| 1594 |
|
| 1595 |
Actuals := |
| 1596 |
New_List ( |
| 1597 |
New_Occurrence_Of (Data.E_Id, Data.Loc), |
| 1598 |
Make_Explicit_Dereference (Data.Loc, Except)); |
| 1599 |
end if; |
| 1600 |
|
| 1601 |
-- Generate: |
| 1602 |
|
| 1603 |
-- when others => |
| 1604 |
-- if not Raised_Id then |
| 1605 |
-- Raised_Id := True; |
| 1606 |
|
| 1607 |
-- Save_Occurrence (E_Id, Get_Current_Excep.all.all); |
| 1608 |
-- or |
| 1609 |
-- Save_Library_Occurrence (Get_Current_Excep.all); |
| 1610 |
-- end if; |
| 1611 |
|
| 1612 |
Stmts := |
| 1613 |
New_List ( |
| 1614 |
Make_If_Statement (Data.Loc, |
| 1615 |
Condition => |
| 1616 |
Make_Op_Not (Data.Loc, |
| 1617 |
Right_Opnd => New_Occurrence_Of (Data.Raised_Id, Data.Loc)), |
| 1618 |
|
| 1619 |
Then_Statements => New_List ( |
| 1620 |
Make_Assignment_Statement (Data.Loc, |
| 1621 |
Name => New_Occurrence_Of (Data.Raised_Id, Data.Loc), |
| 1622 |
Expression => New_Occurrence_Of (Standard_True, Data.Loc)), |
| 1623 |
|
| 1624 |
Make_Procedure_Call_Statement (Data.Loc, |
| 1625 |
Name => |
| 1626 |
New_Occurrence_Of (Proc_To_Call, Data.Loc), |
| 1627 |
Parameter_Associations => Actuals)))); |
| 1628 |
|
| 1629 |
else |
| 1630 |
-- Generate: |
| 1631 |
|
| 1632 |
-- Raised_Id := True; |
| 1633 |
|
| 1634 |
Stmts := New_List ( |
| 1635 |
Make_Assignment_Statement (Data.Loc, |
| 1636 |
Name => New_Occurrence_Of (Data.Raised_Id, Data.Loc), |
| 1637 |
Expression => New_Occurrence_Of (Standard_True, Data.Loc))); |
| 1638 |
end if; |
| 1639 |
|
| 1640 |
-- Generate: |
| 1641 |
|
| 1642 |
-- when others => |
| 1643 |
|
| 1644 |
return |
| 1645 |
Make_Exception_Handler (Data.Loc, |
| 1646 |
Exception_Choices => New_List (Make_Others_Choice (Data.Loc)), |
| 1647 |
Statements => Stmts); |
| 1648 |
end Build_Exception_Handler; |
| 1649 |
|
| 1650 |
----------------------------------- |
| 1651 |
-- Build_Finalization_Collection -- |
| 1652 |
----------------------------------- |
| 1653 |
|
| 1654 |
procedure Build_Finalization_Collection |
| 1655 |
(Typ : Entity_Id; |
| 1656 |
For_Lib_Level : Boolean := False; |
| 1657 |
For_Private : Boolean := False; |
| 1658 |
Context_Scope : Entity_Id := Empty; |
| 1659 |
Insertion_Node : Node_Id := Empty) |
| 1660 |
is |
| 1661 |
Ptr_Typ : constant Entity_Id := Root_Type_Of_Full_View (Base_Type (Typ)); |
| 1662 |
-- Finalization collections built for named access types are associated |
| 1663 |
-- with the full view (if applicable) as a consequence of freezing. The |
| 1664 |
-- full view criteria does not apply to anonymous access types because |
| 1665 |
-- those cannot have a private and a full view. |
| 1666 |
|
| 1667 |
-- Start of processing for Build_Finalization_Collection |
| 1668 |
|
| 1669 |
begin |
| 1670 |
-- Nothing to do if the circumstances do not allow for a finalization |
| 1671 |
-- collection. |
| 1672 |
|
| 1673 |
if not Allows_Finalization_Collection (Typ) then |
| 1674 |
return; |
| 1675 |
|
| 1676 |
-- Various machinery such as freezing may have already created a |
| 1677 |
-- finalization collection. |
| 1678 |
|
| 1679 |
elsif Present (Finalization_Collection (Ptr_Typ)) then |
| 1680 |
return; |
| 1681 |
end if; |
| 1682 |
|
| 1683 |
declare |
| 1684 |
Actions : constant List_Id := New_List; |
| 1685 |
Loc : constant Source_Ptr := Sloc (Ptr_Typ); |
| 1686 |
|
| 1687 |
Fin_Coll_Id : Entity_Id; |
| 1688 |
Pool_Id : Entity_Id; |
| 1689 |
|
| 1690 |
begin |
| 1691 |
-- Source access types use fixed names since the collection will be |
| 1692 |
-- inserted in the same source unit only once. The only exception to |
| 1693 |
-- this are instances using the same access type as generic actual. |
| 1694 |
|
| 1695 |
if Comes_From_Source (Ptr_Typ) and then not Inside_A_Generic then |
| 1696 |
Fin_Coll_Id := |
| 1697 |
Make_Defining_Identifier (Loc, |
| 1698 |
Chars => New_External_Name (Chars (Ptr_Typ), "FC")); |
| 1699 |
|
| 1700 |
-- Internally generated access types use temporaries as their names |
| 1701 |
-- due to possible collision with identical names coming from other |
| 1702 |
-- packages. |
| 1703 |
|
| 1704 |
else |
| 1705 |
Fin_Coll_Id := Make_Temporary (Loc, 'F'); |
| 1706 |
end if; |
| 1707 |
|
| 1708 |
Set_Finalization_Collection (Ptr_Typ, Fin_Coll_Id); |
| 1709 |
|
| 1710 |
-- Generate: |
| 1711 |
-- <Ptr_Typ>FC : aliased Finalization_Collection; |
| 1712 |
|
| 1713 |
Append_To (Actions, |
| 1714 |
Make_Object_Declaration (Loc, |
| 1715 |
Defining_Identifier => Fin_Coll_Id, |
| 1716 |
Aliased_Present => True, |
| 1717 |
Object_Definition => |
| 1718 |
New_Occurrence_Of (RTE (RE_Finalization_Collection), Loc))); |
| 1719 |
|
| 1720 |
if Debug_Generated_Code then |
| 1721 |
Set_Debug_Info_Needed (Fin_Coll_Id); |
| 1722 |
end if; |
| 1723 |
|
| 1724 |
-- Set the associated pool and primitive Finalize_Address of the new |
| 1725 |
-- finalization collection. |
| 1726 |
|
| 1727 |
-- The access type has a user-defined storage pool, use it |
| 1728 |
|
| 1729 |
if Present (Associated_Storage_Pool (Ptr_Typ)) then |
| 1730 |
Pool_Id := Associated_Storage_Pool (Ptr_Typ); |
| 1731 |
|
| 1732 |
-- Otherwise the default choice is the global storage pool |
| 1733 |
|
| 1734 |
else |
| 1735 |
Pool_Id := RTE (RE_Global_Pool_Object); |
| 1736 |
Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id); |
| 1737 |
end if; |
| 1738 |
|
| 1739 |
-- A finalization collection created for an access designating a type |
| 1740 |
-- with private components is inserted before a context-dependent |
| 1741 |
-- node. |
| 1742 |
|
| 1743 |
if For_Private then |
| 1744 |
|
| 1745 |
-- At this point both the scope of the context and the insertion |
| 1746 |
-- mode must be known. |
| 1747 |
|
| 1748 |
pragma Assert (Present (Context_Scope)); |
| 1749 |
pragma Assert (Present (Insertion_Node)); |
| 1750 |
|
| 1751 |
Push_Scope (Context_Scope); |
| 1752 |
|
| 1753 |
-- Treat use clauses as declarations and insert directly in front |
| 1754 |
-- of them. |
| 1755 |
|
| 1756 |
if Nkind (Insertion_Node) in |
| 1757 |
N_Use_Package_Clause | N_Use_Type_Clause |
| 1758 |
then |
| 1759 |
Insert_List_Before_And_Analyze (Insertion_Node, Actions); |
| 1760 |
else |
| 1761 |
Insert_Actions (Insertion_Node, Actions); |
| 1762 |
end if; |
| 1763 |
|
| 1764 |
Pop_Scope; |
| 1765 |
|
| 1766 |
-- The finalization collection belongs to an access type related |
| 1767 |
-- to a build-in-place function call used to initialize a library |
| 1768 |
-- level object. The collection must be inserted in front of the |
| 1769 |
-- access type declaration denoted by Insertion_Node. |
| 1770 |
|
| 1771 |
elsif For_Lib_Level then |
| 1772 |
pragma Assert (Present (Insertion_Node)); |
| 1773 |
Insert_Actions (Insertion_Node, Actions); |
| 1774 |
|
| 1775 |
-- Otherwise the finalization collection and its initialization |
| 1776 |
-- become a part of the freeze node. |
| 1777 |
|
| 1778 |
else |
| 1779 |
Append_Freeze_Actions (Ptr_Typ, Actions); |
| 1780 |
end if; |
| 1781 |
|
| 1782 |
Analyze_List (Actions); |
| 1783 |
|
| 1784 |
-- When the type the finalization collection is being generated for |
| 1785 |
-- was created to store a 'Old object, then mark it as such so its |
| 1786 |
-- finalization can be delayed until after postconditions have been |
| 1787 |
-- checked. |
| 1788 |
|
| 1789 |
if Stores_Attribute_Old_Prefix (Ptr_Typ) then |
| 1790 |
Set_Stores_Attribute_Old_Prefix (Fin_Coll_Id); |
| 1791 |
end if; |
| 1792 |
end; |
| 1793 |
end Build_Finalization_Collection; |
| 1794 |
|
| 1795 |
--------------------- |
| 1796 |
-- Build_Finalizer -- |
| 1797 |
--------------------- |
| 1798 |
|
| 1799 |
procedure Build_Finalizer |
| 1800 |
(N : Node_Id; |
| 1801 |
Clean_Stmts : List_Id; |
| 1802 |
Mark_Id : Entity_Id; |
| 1803 |
Defer_Abort : Boolean; |
| 1804 |
Fin_Id : out Entity_Id) |
| 1805 |
is |
| 1806 |
Acts_As_Clean : constant Boolean := |
| 1807 |
Present (Mark_Id) |
| 1808 |
or else |
| 1809 |
(Present (Clean_Stmts) |
| 1810 |
and then Is_Non_Empty_List (Clean_Stmts)); |
| 1811 |
|
| 1812 |
For_Package_Body : constant Boolean := Nkind (N) = N_Package_Body; |
| 1813 |
For_Package_Spec : constant Boolean := Nkind (N) = N_Package_Declaration; |
| 1814 |
For_Package : constant Boolean := |
| 1815 |
For_Package_Body or else For_Package_Spec; |
| 1816 |
Loc : constant Source_Ptr := Sloc (N); |
| 1817 |
|
| 1818 |
-- NOTE: Local variable declarations are conservative and do not create |
| 1819 |
-- structures right from the start. Entities and lists are created once |
| 1820 |
-- it has been established that N has at least one controlled object. |
| 1821 |
|
| 1822 |
Count : Nat := 0; |
| 1823 |
-- Holds the number of controlled objects encountered so far |
| 1824 |
|
| 1825 |
Decls : List_Id := No_List; |
| 1826 |
-- Declarative region of N (if available). If N is a package declaration |
| 1827 |
-- Decls denotes the visible declarations. |
| 1828 |
|
| 1829 |
Finalizer_Data : Finalization_Exception_Data; |
| 1830 |
-- Data for the exception |
| 1831 |
|
| 1832 |
Finalizer_Decls : List_Id := No_List; |
| 1833 |
-- Local variable declarations |
| 1834 |
|
| 1835 |
Finalization_Master : Entity_Id; |
| 1836 |
-- The Finalization Master object |
| 1837 |
|
| 1838 |
Finalizer_Stmts : List_Id := No_List; |
| 1839 |
-- The statement list of the finalizer body |
| 1840 |
|
| 1841 |
Has_Strict_Ctrl_Objs : Boolean := False; |
| 1842 |
-- A general flag which indicates whether N has at least one controlled |
| 1843 |
-- object with strict semantics for finalization. |
| 1844 |
|
| 1845 |
Has_Tagged_Types : Boolean := False; |
| 1846 |
-- A general flag which indicates whether N has at least one library- |
| 1847 |
-- level tagged type declaration. |
| 1848 |
|
| 1849 |
HSS : Node_Id := Empty; |
| 1850 |
-- The sequence of statements of N (if available) |
| 1851 |
|
| 1852 |
Prev_At_End : Entity_Id := Empty; |
| 1853 |
-- The previous at end procedure of the handled statements block of N |
| 1854 |
|
| 1855 |
Priv_Decls : List_Id := No_List; |
| 1856 |
-- The private declarations of N if N is a package declaration |
| 1857 |
|
| 1858 |
Spec_Id : Entity_Id := Empty; |
| 1859 |
Stmts : List_Id := No_List; |
| 1860 |
|
| 1861 |
Tagged_Type_Stmts : List_Id := No_List; |
| 1862 |
-- Contains calls to Ada.Tags.Unregister_Tag for all library-level |
| 1863 |
-- tagged types found in N. |
| 1864 |
|
| 1865 |
----------------------- |
| 1866 |
-- Local subprograms -- |
| 1867 |
----------------------- |
| 1868 |
|
| 1869 |
procedure Build_Components; |
| 1870 |
-- Create all entites and initialize all lists used in the creation of |
| 1871 |
-- the finalizer. |
| 1872 |
|
| 1873 |
procedure Create_Finalizer; |
| 1874 |
-- Create the spec and body of the finalizer and insert them in the |
| 1875 |
-- proper place in the tree depending on the context. |
| 1876 |
|
| 1877 |
function Has_Ctrl_Objs return Boolean is (Count > 0); |
| 1878 |
-- Return true if N contains a least one controlled object |
| 1879 |
|
| 1880 |
function New_Finalizer_Name |
| 1881 |
(Spec_Id : Node_Id; For_Spec : Boolean) return Name_Id; |
| 1882 |
-- Create a fully qualified name of a package spec or body finalizer. |
| 1883 |
-- The generated name is of the form: xx__yy__finalize_[spec|body]. |
| 1884 |
|
| 1885 |
procedure Process_Declarations |
| 1886 |
(Decls : List_Id; |
| 1887 |
Preprocess : Boolean := False); |
| 1888 |
-- Inspect a list of declarations or statements which may contain |
| 1889 |
-- objects that need finalization. When flag Preprocess is set, the |
| 1890 |
-- routine will simply count the total number of controlled objects in |
| 1891 |
-- Decls and set Count accordingly. |
| 1892 |
|
| 1893 |
procedure Process_Object_Declaration |
| 1894 |
(Decl : Node_Id; |
| 1895 |
Is_Protected : Boolean := False); |
| 1896 |
-- Generate all the machinery associated with the finalization of a |
| 1897 |
-- single object. Flag Is_Protected is set when Decl denotes a simple |
| 1898 |
-- protected object. |
| 1899 |
|
| 1900 |
procedure Process_Tagged_Type_Declaration (Decl : Node_Id); |
| 1901 |
-- Generate all the code necessary to unregister the external tag of a |
| 1902 |
-- tagged type. |
| 1903 |
|
| 1904 |
---------------------- |
| 1905 |
-- Build_Components -- |
| 1906 |
---------------------- |
| 1907 |
|
| 1908 |
procedure Build_Components is |
| 1909 |
Constraints : List_Id; |
| 1910 |
Master_Decl : Node_Id; |
| 1911 |
Master_Name : Name_Id; |
| 1912 |
|
| 1913 |
begin |
| 1914 |
pragma Assert (Present (Decls)); |
| 1915 |
|
| 1916 |
-- If the context contains controlled objects with strict semantics |
| 1917 |
-- for finalization, then we create the finalization master, unless |
| 1918 |
-- there is a single such object: in this common case, we'll directly |
| 1919 |
-- finalize the object. |
| 1920 |
|
| 1921 |
if Has_Strict_Ctrl_Objs then |
| 1922 |
if Count > 1 then |
| 1923 |
if For_Package_Spec then |
| 1924 |
Master_Name := |
| 1925 |
New_External_Name (Name_uMaster, Suffix => "_spec"); |
| 1926 |
elsif For_Package_Body then |
| 1927 |
Master_Name := |
| 1928 |
New_External_Name (Name_uMaster, Suffix => "_body"); |
| 1929 |
else |
| 1930 |
Master_Name := New_Internal_Name ('M'); |
| 1931 |
end if; |
| 1932 |
|
| 1933 |
Finalization_Master := |
| 1934 |
Make_Defining_Identifier (Loc, Master_Name); |
| 1935 |
|
| 1936 |
-- The master is statically parameterized by the context |
| 1937 |
|
| 1938 |
Constraints := New_List; |
| 1939 |
Append_To (Constraints, |
| 1940 |
New_Occurrence_Of (Boolean_Literals (Exceptions_OK), Loc)); |
| 1941 |
Append_To (Constraints, |
| 1942 |
New_Occurrence_Of |
| 1943 |
(Boolean_Literals (Exception_Extra_Info), Loc)); |
| 1944 |
Append_To (Constraints, |
| 1945 |
New_Occurrence_Of (Boolean_Literals (For_Package), Loc)); |
| 1946 |
|
| 1947 |
Master_Decl := |
| 1948 |
Make_Object_Declaration (Loc, |
| 1949 |
Defining_Identifier => Finalization_Master, |
| 1950 |
Object_Definition => |
| 1951 |
Make_Subtype_Indication (Loc, |
| 1952 |
Subtype_Mark => |
| 1953 |
New_Occurrence_Of |
| 1954 |
(RTE (RE_Finalization_Master), Loc), |
| 1955 |
Constraint => |
| 1956 |
Make_Index_Or_Discriminant_Constraint (Loc, |
| 1957 |
Constraints => Constraints))); |
| 1958 |
|
| 1959 |
Prepend_To (Decls, Master_Decl); |
| 1960 |
Analyze (Master_Decl, Suppress => All_Checks); |
| 1961 |
end if; |
| 1962 |
|
| 1963 |
if Exceptions_OK then |
| 1964 |
Finalizer_Decls := New_List; |
| 1965 |
Build_Object_Declarations |
| 1966 |
(Finalizer_Data, Finalizer_Decls, Loc, For_Package); |
| 1967 |
end if; |
| 1968 |
end if; |
| 1969 |
|
| 1970 |
-- If the context requires additional cleanup, the finalization |
| 1971 |
-- machinery is added after the cleanup code. |
| 1972 |
|
| 1973 |
if Acts_As_Clean then |
| 1974 |
Finalizer_Stmts := Clean_Stmts; |
| 1975 |
else |
| 1976 |
Finalizer_Stmts := New_List; |
| 1977 |
end if; |
| 1978 |
|
| 1979 |
if Has_Tagged_Types then |
| 1980 |
Tagged_Type_Stmts := New_List; |
| 1981 |
end if; |
| 1982 |
end Build_Components; |
| 1983 |
|
| 1984 |
---------------------- |
| 1985 |
-- Create_Finalizer -- |
| 1986 |
---------------------- |
| 1987 |
|
| 1988 |
procedure Create_Finalizer is |
| 1989 |
Body_Id : Entity_Id; |
| 1990 |
Fin_Body : Node_Id; |
| 1991 |
Fin_Call : Node_Id; |
| 1992 |
Fin_Spec : Node_Id; |
| 1993 |
|
| 1994 |
begin |
| 1995 |
-- Step 1: Creation of the finalizer name |
| 1996 |
|
| 1997 |
-- Packages must use a distinct name for their finalizers since the |
| 1998 |
-- binder will have to generate calls to them by name. The name is |
| 1999 |
-- of the following form: |
| 2000 |
|
| 2001 |
-- xx__yy__finalize_[spec|body] |
| 2002 |
|
| 2003 |
if For_Package then |
| 2004 |
Fin_Id := Make_Defining_Identifier |
| 2005 |
(Loc, New_Finalizer_Name (Spec_Id, For_Package_Spec)); |
| 2006 |
Set_Has_Qualified_Name (Fin_Id); |
| 2007 |
Set_Has_Fully_Qualified_Name (Fin_Id); |
| 2008 |
|
| 2009 |
-- The default name is _finalizer |
| 2010 |
|
| 2011 |
else |
| 2012 |
Fin_Id := |
| 2013 |
Make_Defining_Identifier (Loc, |
| 2014 |
Chars => New_External_Name (Name_uFinalizer)); |
| 2015 |
Set_Is_Finalizer (Fin_Id); |
| 2016 |
|
| 2017 |
-- The visibility semantics of At_End handlers force a strange |
| 2018 |
-- separation of spec and body for stack-related finalizers: |
| 2019 |
|
| 2020 |
-- declare : Enclosing_Scope |
| 2021 |
-- procedure _finalizer; |
| 2022 |
-- begin |
| 2023 |
-- <controlled objects> |
| 2024 |
-- procedure _finalizer is |
| 2025 |
-- ... |
| 2026 |
-- at end |
| 2027 |
-- _finalizer; |
| 2028 |
-- end; |
| 2029 |
|
| 2030 |
-- Both spec and body are within the same construct and scope, but |
| 2031 |
-- the body is part of the handled sequence of statements. This |
| 2032 |
-- placement confuses the elaboration mechanism on targets where |
| 2033 |
-- At_End handlers are expanded into "when all others" handlers: |
| 2034 |
|
| 2035 |
-- exception |
| 2036 |
-- when all others => |
| 2037 |
-- _finalizer; -- appears to require elab checks |
| 2038 |
-- at end |
| 2039 |
-- _finalizer; |
| 2040 |
-- end; |
| 2041 |
|
| 2042 |
-- Since the compiler guarantees that the body of a _finalizer is |
| 2043 |
-- always inserted in the same construct where the At_End handler |
| 2044 |
-- resides, there is no need for elaboration checks. |
| 2045 |
|
| 2046 |
Set_Kill_Elaboration_Checks (Fin_Id); |
| 2047 |
|
| 2048 |
-- Inlining the finalizer produces a substantial speedup at -O2. |
| 2049 |
-- It is inlined by default at -O3. Either way, it is called |
| 2050 |
-- exactly twice (once on the normal path, and once for |
| 2051 |
-- exceptions/abort), so this won't bloat the code too much. |
| 2052 |
|
| 2053 |
Set_Is_Inlined (Fin_Id); |
| 2054 |
end if; |
| 2055 |
|
| 2056 |
if Debug_Generated_Code then |
| 2057 |
Set_Debug_Info_Needed (Fin_Id); |
| 2058 |
end if; |
| 2059 |
|
| 2060 |
-- Step 2: Creation of the finalizer specification |
| 2061 |
|
| 2062 |
-- Generate: |
| 2063 |
-- procedure Fin_Id; |
| 2064 |
|
| 2065 |
Fin_Spec := |
| 2066 |
Make_Subprogram_Declaration (Loc, |
| 2067 |
Specification => |
| 2068 |
Make_Procedure_Specification (Loc, |
| 2069 |
Defining_Unit_Name => Fin_Id)); |
| 2070 |
|
| 2071 |
if For_Package then |
| 2072 |
Set_Is_Exported (Fin_Id); |
| 2073 |
Set_Interface_Name (Fin_Id, |
| 2074 |
Make_String_Literal (Loc, |
| 2075 |
Strval => Get_Name_String (Chars (Fin_Id)))); |
| 2076 |
end if; |
| 2077 |
|
| 2078 |
-- Step 3: Creation of the finalizer body |
| 2079 |
|
| 2080 |
-- Add the library-level tagged type unregistration machinery before |
| 2081 |
-- the finalization circuitry. This ensures that external tags will |
| 2082 |
-- be removed even if a finalization exception occurs at some point. |
| 2083 |
|
| 2084 |
if Has_Tagged_Types then |
| 2085 |
Prepend_List_To (Finalizer_Stmts, Tagged_Type_Stmts); |
| 2086 |
end if; |
| 2087 |
|
| 2088 |
-- Add a call to the previous At_End handler if it exists. The call |
| 2089 |
-- must always precede the finalization circuitry. |
| 2090 |
|
| 2091 |
if Present (Prev_At_End) then |
| 2092 |
Prepend_To (Finalizer_Stmts, |
| 2093 |
Make_Procedure_Call_Statement (Loc, Prev_At_End)); |
| 2094 |
|
| 2095 |
-- Clear the At_End handler since we have already generated the |
| 2096 |
-- proper replacement call for it. |
| 2097 |
|
| 2098 |
Set_At_End_Proc (HSS, Empty); |
| 2099 |
end if; |
| 2100 |
|
| 2101 |
-- If there are no controlled objects to be finalized, generate; |
| 2102 |
|
| 2103 |
-- procedure Fin_Id is |
| 2104 |
-- begin |
| 2105 |
-- Abort_Defer; -- Added if abort is allowed |
| 2106 |
-- <call to Prev_At_End> -- Added if exists |
| 2107 |
-- <tag unregistration> -- Added if Has_Tagged_Types |
| 2108 |
-- <cleanup statements> -- Added if Acts_As_Clean |
| 2109 |
-- <stack release> -- Added if Mark_Id exists |
| 2110 |
-- Abort_Undefer; -- Added if abort is allowed |
| 2111 |
-- end Fin_Id; |
| 2112 |
|
| 2113 |
-- If there are strict controlled objects to be finalized, generate: |
| 2114 |
|
| 2115 |
-- procedure Fin_Id is |
| 2116 |
-- Abort : constant Boolean := Triggered_By_Abort; |
| 2117 |
-- E : Exception_Occurrence; |
| 2118 |
-- Raised : Boolean := False; |
| 2119 |
-- begin |
| 2120 |
-- Abort_Defer; -- Added if abort is allowed |
| 2121 |
-- <call to Prev_At_End> -- Added if exists |
| 2122 |
-- <tag unregistration> -- Added if Has_Tagged_Types |
| 2123 |
-- <cleanup statements> -- Added if Acts_As_Clean |
| 2124 |
-- <finalization statements> |
| 2125 |
-- <stack release> -- Added if Mark_Id exists |
| 2126 |
-- Abort_Undefer; -- Added if abort is allowed |
| 2127 |
-- <exception propagation> |
| 2128 |
-- end Fin_Id; |
| 2129 |
|
| 2130 |
-- If there are only controlled objects with relaxed semantics for |
| 2131 |
-- finalization, only the <finalization statements> are generated. |
| 2132 |
|
| 2133 |
if Has_Strict_Ctrl_Objs and then Count > 1 then |
| 2134 |
Fin_Call := |
| 2135 |
Make_Procedure_Call_Statement (Loc, |
| 2136 |
Name => |
| 2137 |
New_Occurrence_Of (RTE (RE_Finalize_Master), Loc), |
| 2138 |
Parameter_Associations => |
| 2139 |
New_List (New_Occurrence_Of (Finalization_Master, Loc))); |
| 2140 |
|
| 2141 |
-- For CodePeer, the exception handlers normally generated here |
| 2142 |
-- generate complex flowgraphs which result in capacity problems. |
| 2143 |
-- Omitting these handlers for CodePeer is justified as follows: |
| 2144 |
|
| 2145 |
-- If a handler is dead, then omitting it is surely ok |
| 2146 |
|
| 2147 |
-- If a handler is live, then CodePeer should flag the |
| 2148 |
-- potentially-exception-raising construct that causes it |
| 2149 |
-- to be live. That is what we are interested in, not what |
| 2150 |
-- happens after the exception is raised. |
| 2151 |
|
| 2152 |
if Exceptions_OK and not CodePeer_Mode then |
| 2153 |
Fin_Call := |
| 2154 |
Make_Block_Statement (Loc, |
| 2155 |
Handled_Statement_Sequence => |
| 2156 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 2157 |
Statements => New_List (Fin_Call), |
| 2158 |
|
| 2159 |
Exception_Handlers => New_List ( |
| 2160 |
Build_Exception_Handler |
| 2161 |
(Finalizer_Data, For_Package)))); |
| 2162 |
end if; |
| 2163 |
|
| 2164 |
Append_To (Finalizer_Stmts, Fin_Call); |
| 2165 |
end if; |
| 2166 |
|
| 2167 |
-- Release the secondary stack |
| 2168 |
|
| 2169 |
if Present (Mark_Id) then |
| 2170 |
declare |
| 2171 |
Release : Node_Id := Build_SS_Release_Call (Loc, Mark_Id); |
| 2172 |
|
| 2173 |
begin |
| 2174 |
-- If the context is a build-in-place function, the secondary |
| 2175 |
-- stack must be released, unless the build-in-place function |
| 2176 |
-- itself is returning on the secondary stack. Generate: |
| 2177 |
-- |
| 2178 |
-- if BIP_Alloc_Form /= Secondary_Stack then |
| 2179 |
-- SS_Release (Mark_Id); |
| 2180 |
-- end if; |
| 2181 |
-- |
| 2182 |
-- Note that if the function returns on the secondary stack, |
| 2183 |
-- then the responsibility of reclaiming the space is always |
| 2184 |
-- left to the caller (recursively if needed). |
| 2185 |
|
| 2186 |
if Nkind (N) = N_Subprogram_Body then |
| 2187 |
declare |
| 2188 |
Spec_Id : constant Entity_Id := |
| 2189 |
Unique_Defining_Entity (N); |
| 2190 |
BIP_SS : constant Boolean := |
| 2191 |
Is_Build_In_Place_Function (Spec_Id) |
| 2192 |
and then Needs_BIP_Alloc_Form (Spec_Id); |
| 2193 |
begin |
| 2194 |
if BIP_SS then |
| 2195 |
Release := |
| 2196 |
Make_If_Statement (Loc, |
| 2197 |
Condition => |
| 2198 |
Make_Op_Ne (Loc, |
| 2199 |
Left_Opnd => |
| 2200 |
New_Occurrence_Of |
| 2201 |
(Build_In_Place_Formal |
| 2202 |
(Spec_Id, BIP_Alloc_Form), Loc), |
| 2203 |
Right_Opnd => |
| 2204 |
Make_Integer_Literal (Loc, |
| 2205 |
UI_From_Int |
| 2206 |
(BIP_Allocation_Form'Pos |
| 2207 |
(Secondary_Stack)))), |
| 2208 |
|
| 2209 |
Then_Statements => New_List (Release)); |
| 2210 |
end if; |
| 2211 |
end; |
| 2212 |
end if; |
| 2213 |
|
| 2214 |
Append_To (Finalizer_Stmts, Release); |
| 2215 |
end; |
| 2216 |
end if; |
| 2217 |
|
| 2218 |
-- Protect the statements with abort defer/undefer. This is only when |
| 2219 |
-- aborts are allowed and the cleanup statements require deferral or |
| 2220 |
-- there are controlled objects to be finalized. Note that the abort |
| 2221 |
-- defer/undefer pair does not require an extra block because the |
| 2222 |
-- finalization exception is caught in its corresponding finalization |
| 2223 |
-- block. As a result, the call to Abort_Defer always takes place. |
| 2224 |
|
| 2225 |
if Abort_Allowed and then (Defer_Abort or Has_Ctrl_Objs) then |
| 2226 |
Prepend_To (Finalizer_Stmts, |
| 2227 |
Build_Runtime_Call (Loc, RE_Abort_Defer)); |
| 2228 |
|
| 2229 |
Append_To (Finalizer_Stmts, |
| 2230 |
Build_Runtime_Call (Loc, RE_Abort_Undefer)); |
| 2231 |
end if; |
| 2232 |
|
| 2233 |
-- The local exception does not need to be reraised for library-level |
| 2234 |
-- finalizers. Note that this action must be carried out after object |
| 2235 |
-- cleanup, secondary stack release, and abort undeferral. Generate: |
| 2236 |
|
| 2237 |
-- if Raised and then not Abort then |
| 2238 |
-- Raise_From_Controlled_Operation (E); |
| 2239 |
-- end if; |
| 2240 |
|
| 2241 |
if Has_Strict_Ctrl_Objs and Exceptions_OK and not For_Package then |
| 2242 |
Append_To (Finalizer_Stmts, |
| 2243 |
Build_Raise_Statement (Finalizer_Data)); |
| 2244 |
end if; |
| 2245 |
|
| 2246 |
-- Create the body of the finalizer |
| 2247 |
|
| 2248 |
Body_Id := Make_Defining_Identifier (Loc, Chars (Fin_Id)); |
| 2249 |
|
| 2250 |
if Debug_Generated_Code then |
| 2251 |
Set_Debug_Info_Needed (Body_Id); |
| 2252 |
end if; |
| 2253 |
|
| 2254 |
if For_Package then |
| 2255 |
Set_Has_Qualified_Name (Body_Id); |
| 2256 |
Set_Has_Fully_Qualified_Name (Body_Id); |
| 2257 |
end if; |
| 2258 |
|
| 2259 |
Fin_Body := |
| 2260 |
Make_Subprogram_Body (Loc, |
| 2261 |
Specification => |
| 2262 |
Make_Procedure_Specification (Loc, |
| 2263 |
Defining_Unit_Name => Body_Id), |
| 2264 |
Declarations => Finalizer_Decls, |
| 2265 |
Handled_Statement_Sequence => |
| 2266 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 2267 |
Statements => Finalizer_Stmts)); |
| 2268 |
|
| 2269 |
-- Step 4: Spec and body insertion, analysis |
| 2270 |
|
| 2271 |
if For_Package then |
| 2272 |
|
| 2273 |
-- If a package spec has private declarations, both the finalizer |
| 2274 |
-- spec and body are inserted at the end of this list. |
| 2275 |
|
| 2276 |
if For_Package_Spec and then Present (Priv_Decls) then |
| 2277 |
Append_To (Priv_Decls, Fin_Spec); |
| 2278 |
Append_To (Priv_Decls, Fin_Body); |
| 2279 |
|
| 2280 |
-- Otherwise, and for a package body, both the finalizer spec and |
| 2281 |
-- body are inserted at the end of the package declarations. |
| 2282 |
|
| 2283 |
else |
| 2284 |
Append_To (Decls, Fin_Spec); |
| 2285 |
Append_To (Decls, Fin_Body); |
| 2286 |
end if; |
| 2287 |
|
| 2288 |
-- Non-package case |
| 2289 |
|
| 2290 |
else |
| 2291 |
-- Insert the spec for the finalizer. The At_End handler must be |
| 2292 |
-- able to call the body which resides in a nested structure. |
| 2293 |
|
| 2294 |
-- declare |
| 2295 |
-- procedure Fin_Id; -- Spec |
| 2296 |
-- begin |
| 2297 |
-- <objects and possibly statements> |
| 2298 |
-- procedure Fin_Id is ... -- Body |
| 2299 |
-- <statements> |
| 2300 |
-- at end |
| 2301 |
-- Fin_Id; -- At_End handler |
| 2302 |
-- end; |
| 2303 |
|
| 2304 |
pragma Assert (Present (Decls)); |
| 2305 |
|
| 2306 |
Append_To (Decls, Fin_Spec); |
| 2307 |
|
| 2308 |
-- When the finalizer acts solely as a cleanup routine, the body |
| 2309 |
-- is inserted right after the spec. |
| 2310 |
|
| 2311 |
if Acts_As_Clean and not Has_Ctrl_Objs then |
| 2312 |
Insert_After (Fin_Spec, Fin_Body); |
| 2313 |
|
| 2314 |
-- In other cases the body is inserted after the last statement |
| 2315 |
|
| 2316 |
else |
| 2317 |
-- Manually freeze the spec. This is somewhat of a hack because |
| 2318 |
-- a subprogram is frozen when its body is seen and the freeze |
| 2319 |
-- node appears right before the body. However, in this case, |
| 2320 |
-- the spec must be frozen earlier since the At_End handler |
| 2321 |
-- must be able to call it. |
| 2322 |
-- |
| 2323 |
-- declare |
| 2324 |
-- procedure Fin_Id; -- Spec |
| 2325 |
-- [Fin_Id] -- Freeze node |
| 2326 |
-- begin |
| 2327 |
-- ... |
| 2328 |
-- at end |
| 2329 |
-- Fin_Id; -- At_End handler |
| 2330 |
-- end; |
| 2331 |
|
| 2332 |
Ensure_Freeze_Node (Fin_Id); |
| 2333 |
Insert_After (Fin_Spec, Freeze_Node (Fin_Id)); |
| 2334 |
Set_Is_Frozen (Fin_Id); |
| 2335 |
|
| 2336 |
Append_To (Stmts, Fin_Body); |
| 2337 |
end if; |
| 2338 |
end if; |
| 2339 |
|
| 2340 |
Analyze (Fin_Spec, Suppress => All_Checks); |
| 2341 |
Analyze (Fin_Body, Suppress => All_Checks); |
| 2342 |
|
| 2343 |
-- Never consider that the finalizer procedure is enabled Ghost, even |
| 2344 |
-- when the corresponding unit is Ghost, as this would lead to an |
| 2345 |
-- an external name with a ___ghost_ prefix that the binder cannot |
| 2346 |
-- generate, as it has no knowledge of the Ghost status of units. |
| 2347 |
|
| 2348 |
Set_Is_Checked_Ghost_Entity (Fin_Id, False); |
| 2349 |
end Create_Finalizer; |
| 2350 |
|
| 2351 |
------------------------ |
| 2352 |
-- New_Finalizer_Name -- |
| 2353 |
------------------------ |
| 2354 |
|
| 2355 |
function New_Finalizer_Name |
| 2356 |
(Spec_Id : Node_Id; For_Spec : Boolean) return Name_Id |
| 2357 |
is |
| 2358 |
procedure New_Finalizer_Name (Id : Entity_Id); |
| 2359 |
-- Place "__<name-of-Id>" in the name buffer. If the identifier |
| 2360 |
-- has a non-standard scope, process the scope first. |
| 2361 |
|
| 2362 |
------------------------ |
| 2363 |
-- New_Finalizer_Name -- |
| 2364 |
------------------------ |
| 2365 |
|
| 2366 |
procedure New_Finalizer_Name (Id : Entity_Id) is |
| 2367 |
begin |
| 2368 |
if Scope (Id) = Standard_Standard then |
| 2369 |
Get_Name_String (Chars (Id)); |
| 2370 |
|
| 2371 |
else |
| 2372 |
New_Finalizer_Name (Scope (Id)); |
| 2373 |
Add_Str_To_Name_Buffer ("__"); |
| 2374 |
Get_Name_String_And_Append (Chars (Id)); |
| 2375 |
end if; |
| 2376 |
end New_Finalizer_Name; |
| 2377 |
|
| 2378 |
-- Start of processing for New_Finalizer_Name |
| 2379 |
|
| 2380 |
begin |
| 2381 |
-- Create the fully qualified name of the enclosing scope |
| 2382 |
|
| 2383 |
New_Finalizer_Name (Spec_Id); |
| 2384 |
|
| 2385 |
-- Generate: |
| 2386 |
-- __finalize_[spec|body] |
| 2387 |
|
| 2388 |
Add_Str_To_Name_Buffer ("__finalize_"); |
| 2389 |
|
| 2390 |
if For_Spec then |
| 2391 |
Add_Str_To_Name_Buffer ("spec"); |
| 2392 |
else |
| 2393 |
Add_Str_To_Name_Buffer ("body"); |
| 2394 |
end if; |
| 2395 |
|
| 2396 |
return Name_Find; |
| 2397 |
end New_Finalizer_Name; |
| 2398 |
|
| 2399 |
-------------------------- |
| 2400 |
-- Process_Declarations -- |
| 2401 |
-------------------------- |
| 2402 |
|
| 2403 |
procedure Process_Declarations |
| 2404 |
(Decls : List_Id; |
| 2405 |
Preprocess : Boolean := False) |
| 2406 |
is |
| 2407 |
procedure Process_Package_Body (Decl : Node_Id); |
| 2408 |
-- Process an N_Package_Body node |
| 2409 |
|
| 2410 |
procedure Processing_Actions |
| 2411 |
(Decl : Node_Id; |
| 2412 |
Is_Protected : Boolean := False; |
| 2413 |
Strict : Boolean := False); |
| 2414 |
-- Depending on the mode of operation of Process_Declarations, either |
| 2415 |
-- increment the controlled object count or process the declaration. |
| 2416 |
-- The Flag Is_Protected is set when the declaration denotes a simple |
| 2417 |
-- protected object. The flag Strict is true when the declaration is |
| 2418 |
-- for a controlled object with strict semantics for finalization. |
| 2419 |
|
| 2420 |
-------------------------- |
| 2421 |
-- Process_Package_Body -- |
| 2422 |
-------------------------- |
| 2423 |
|
| 2424 |
procedure Process_Package_Body (Decl : Node_Id) is |
| 2425 |
begin |
| 2426 |
-- Do not inspect an ignored Ghost package body because all |
| 2427 |
-- code found within will not appear in the final tree. |
| 2428 |
|
| 2429 |
if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then |
| 2430 |
null; |
| 2431 |
|
| 2432 |
elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package then |
| 2433 |
Process_Declarations (Declarations (Decl), Preprocess); |
| 2434 |
end if; |
| 2435 |
end Process_Package_Body; |
| 2436 |
|
| 2437 |
------------------------ |
| 2438 |
-- Processing_Actions -- |
| 2439 |
------------------------ |
| 2440 |
|
| 2441 |
procedure Processing_Actions |
| 2442 |
(Decl : Node_Id; |
| 2443 |
Is_Protected : Boolean := False; |
| 2444 |
Strict : Boolean := False) |
| 2445 |
is |
| 2446 |
begin |
| 2447 |
-- Library-level tagged type |
| 2448 |
|
| 2449 |
if Nkind (Decl) = N_Full_Type_Declaration then |
| 2450 |
if Preprocess then |
| 2451 |
Has_Tagged_Types := True; |
| 2452 |
|
| 2453 |
-- Unregister tagged type, unless No_Tagged_Type_Registration |
| 2454 |
-- is active. |
| 2455 |
|
| 2456 |
elsif not Restriction_Active (No_Tagged_Type_Registration) then |
| 2457 |
Process_Tagged_Type_Declaration (Decl); |
| 2458 |
end if; |
| 2459 |
|
| 2460 |
-- Controlled object declaration |
| 2461 |
|
| 2462 |
else |
| 2463 |
if Preprocess then |
| 2464 |
Count := Count + 1; |
| 2465 |
if Strict then |
| 2466 |
Has_Strict_Ctrl_Objs := True; |
| 2467 |
end if; |
| 2468 |
|
| 2469 |
else |
| 2470 |
Process_Object_Declaration (Decl, Is_Protected); |
| 2471 |
end if; |
| 2472 |
end if; |
| 2473 |
end Processing_Actions; |
| 2474 |
|
| 2475 |
-- Local variables |
| 2476 |
|
| 2477 |
Decl : Node_Id; |
| 2478 |
Obj_Id : Entity_Id; |
| 2479 |
Obj_Typ : Entity_Id; |
| 2480 |
Pack_Id : Entity_Id; |
| 2481 |
Prev : Node_Id; |
| 2482 |
Spec : Node_Id; |
| 2483 |
Typ : Entity_Id; |
| 2484 |
|
| 2485 |
-- Start of processing for Process_Declarations |
| 2486 |
|
| 2487 |
begin |
| 2488 |
if Is_Empty_List (Decls) then |
| 2489 |
return; |
| 2490 |
end if; |
| 2491 |
|
| 2492 |
-- Process all declarations in reverse order and be prepared for them |
| 2493 |
-- to be moved during the processing. |
| 2494 |
|
| 2495 |
Decl := Last_Non_Pragma (Decls); |
| 2496 |
while Present (Decl) loop |
| 2497 |
Prev := Prev_Non_Pragma (Decl); |
| 2498 |
|
| 2499 |
-- Library-level tagged types |
| 2500 |
|
| 2501 |
if Nkind (Decl) = N_Full_Type_Declaration then |
| 2502 |
Typ := Defining_Identifier (Decl); |
| 2503 |
|
| 2504 |
-- Ignored Ghost types do not need any cleanup actions because |
| 2505 |
-- they will not appear in the final tree. |
| 2506 |
|
| 2507 |
if Is_Ignored_Ghost_Entity (Typ) then |
| 2508 |
null; |
| 2509 |
|
| 2510 |
elsif Is_Tagged_Type (Typ) |
| 2511 |
and then Is_Library_Level_Entity (Typ) |
| 2512 |
and then Convention (Typ) = Convention_Ada |
| 2513 |
and then Present (Access_Disp_Table (Typ)) |
| 2514 |
and then not Is_Abstract_Type (Typ) |
| 2515 |
and then not No_Run_Time_Mode |
| 2516 |
and then not Restriction_Active (No_Tagged_Type_Registration) |
| 2517 |
and then RTE_Available (RE_Register_Tag) |
| 2518 |
then |
| 2519 |
Processing_Actions (Decl); |
| 2520 |
end if; |
| 2521 |
|
| 2522 |
-- Regular object declarations |
| 2523 |
|
| 2524 |
elsif Nkind (Decl) = N_Object_Declaration then |
| 2525 |
Obj_Id := Defining_Identifier (Decl); |
| 2526 |
Obj_Typ := Base_Type (Etype (Obj_Id)); |
| 2527 |
|
| 2528 |
-- Bypass any form of processing for objects which have their |
| 2529 |
-- finalization disabled. This applies only to objects at the |
| 2530 |
-- library level. |
| 2531 |
|
| 2532 |
if For_Package and then Finalize_Storage_Only (Obj_Typ) then |
| 2533 |
null; |
| 2534 |
|
| 2535 |
-- Finalization of transient objects is treated separately in |
| 2536 |
-- order to handle sensitive cases. These include: |
| 2537 |
|
| 2538 |
-- * Conditional expressions |
| 2539 |
-- * Expressions with actions |
| 2540 |
-- * Transient scopes |
| 2541 |
|
| 2542 |
elsif Is_Finalized_Transient (Obj_Id) then |
| 2543 |
null; |
| 2544 |
|
| 2545 |
-- Finalization of specific objects is also treated separately |
| 2546 |
|
| 2547 |
elsif Is_Ignored_For_Finalization (Obj_Id) then |
| 2548 |
null; |
| 2549 |
|
| 2550 |
-- Ignored Ghost objects do not need any cleanup actions |
| 2551 |
-- because they will not appear in the final tree. |
| 2552 |
|
| 2553 |
elsif Is_Ignored_Ghost_Entity (Obj_Id) then |
| 2554 |
null; |
| 2555 |
|
| 2556 |
-- Conversely, if one of the above cases created a Master_Node, |
| 2557 |
-- finalization actions are required for the associated object. |
| 2558 |
|
| 2559 |
elsif Ekind (Obj_Id) = E_Variable |
| 2560 |
and then Is_RTE (Obj_Typ, RE_Master_Node) |
| 2561 |
then |
| 2562 |
Processing_Actions |
| 2563 |
(Decl, Strict => not Is_Independent (Obj_Id)); |
| 2564 |
|
| 2565 |
-- The object is of the form: |
| 2566 |
-- Obj : [constant] Typ [:= Expr]; |
| 2567 |
|
| 2568 |
-- Do not process the incomplete view of a deferred constant. |
| 2569 |
-- Note that an object initialized by means of a BIP function |
| 2570 |
-- call may appear as a deferred constant after expansion |
| 2571 |
-- activities. These kinds of objects must be finalized. |
| 2572 |
|
| 2573 |
elsif not Is_Imported (Obj_Id) |
| 2574 |
and then Needs_Finalization (Obj_Typ) |
| 2575 |
and then not (Ekind (Obj_Id) = E_Constant |
| 2576 |
and then not Has_Completion (Obj_Id) |
| 2577 |
and then No (BIP_Initialization_Call (Obj_Id))) |
| 2578 |
then |
| 2579 |
Processing_Actions |
| 2580 |
(Decl, Strict => not Has_Relaxed_Finalization (Obj_Typ)); |
| 2581 |
|
| 2582 |
-- The object is an access-to-controlled that must be finalized |
| 2583 |
|
| 2584 |
elsif Is_Access_Type (Obj_Typ) |
| 2585 |
and then Is_Finalizable_Access (Decl) |
| 2586 |
then |
| 2587 |
Processing_Actions |
| 2588 |
(Decl, |
| 2589 |
Strict => not Has_Relaxed_Finalization |
| 2590 |
(Available_View (Designated_Type (Obj_Typ)))); |
| 2591 |
|
| 2592 |
-- Simple protected objects which use the type System.Tasking. |
| 2593 |
-- Protected_Objects.Protection to manage their locks should |
| 2594 |
-- be treated as controlled since they require manual cleanup. |
| 2595 |
-- but not for restricted run-time libraries (Ravenscar), see |
| 2596 |
-- also Cleanup_Protected_Object. |
| 2597 |
|
| 2598 |
-- The only exception is illustrated in the following example: |
| 2599 |
|
| 2600 |
-- package Pkg is |
| 2601 |
-- type Ctrl is new Controlled ... |
| 2602 |
-- procedure Finalize (Obj : in out Ctrl); |
| 2603 |
-- Lib_Obj : Ctrl; |
| 2604 |
-- end Pkg; |
| 2605 |
|
| 2606 |
-- package body Pkg is |
| 2607 |
-- protected Prot is |
| 2608 |
-- procedure Do_Something (Obj : in out Ctrl); |
| 2609 |
-- end Prot; |
| 2610 |
|
| 2611 |
-- protected body Prot is |
| 2612 |
-- procedure Do_Something (Obj : in out Ctrl) is ... |
| 2613 |
-- end Prot; |
| 2614 |
|
| 2615 |
-- procedure Finalize (Obj : in out Ctrl) is |
| 2616 |
-- begin |
| 2617 |
-- Prot.Do_Something (Obj); |
| 2618 |
-- end Finalize; |
| 2619 |
-- end Pkg; |
| 2620 |
|
| 2621 |
-- Since for the most part entities in package bodies depend on |
| 2622 |
-- those in package specs, Prot's lock should be cleaned up |
| 2623 |
-- first. The subsequent cleanup of the spec finalizes Lib_Obj. |
| 2624 |
-- This act however attempts to invoke Do_Something and fails |
| 2625 |
-- because the lock has disappeared. |
| 2626 |
|
| 2627 |
elsif Ekind (Obj_Id) = E_Variable |
| 2628 |
and then not In_Library_Level_Package_Body (Obj_Id) |
| 2629 |
and then Has_Simple_Protected_Object (Obj_Typ) |
| 2630 |
and then not Restricted_Profile |
| 2631 |
then |
| 2632 |
Processing_Actions |
| 2633 |
(Decl, Is_Protected => True, Strict => True); |
| 2634 |
end if; |
| 2635 |
|
| 2636 |
-- Inspect the freeze node of an access-to-controlled type and |
| 2637 |
-- look for a delayed finalization collection. This case arises |
| 2638 |
-- when the freeze actions are inserted at a later time than the |
| 2639 |
-- expansion of the context. Since Build_Finalizer is never called |
| 2640 |
-- on a single construct twice, the collection would be ultimately |
| 2641 |
-- left out and never finalized. This is also needed for freeze |
| 2642 |
-- actions of designated types themselves, since in some cases the |
| 2643 |
-- finalization collection is associated with a designated type's |
| 2644 |
-- freeze node rather than that of the access type (see handling |
| 2645 |
-- for freeze actions in Build_Finalization_Collection). |
| 2646 |
|
| 2647 |
elsif Nkind (Decl) = N_Freeze_Entity |
| 2648 |
and then Present (Actions (Decl)) |
| 2649 |
then |
| 2650 |
Typ := Entity (Decl); |
| 2651 |
|
| 2652 |
-- Freeze nodes for ignored Ghost types do not need cleanup |
| 2653 |
-- actions because they will never appear in the final tree. |
| 2654 |
|
| 2655 |
if Is_Ignored_Ghost_Entity (Typ) then |
| 2656 |
null; |
| 2657 |
|
| 2658 |
elsif (Is_Access_Object_Type (Typ) |
| 2659 |
and then Needs_Finalization |
| 2660 |
(Available_View (Designated_Type (Typ)))) |
| 2661 |
or else (Is_Type (Typ) and then Needs_Finalization (Typ)) |
| 2662 |
then |
| 2663 |
-- Freeze nodes are considered to be identical to packages |
| 2664 |
-- and blocks in terms of nesting. The difference is that |
| 2665 |
-- a finalization collection created inside the freeze node |
| 2666 |
-- is at the same nesting level as the node itself. |
| 2667 |
|
| 2668 |
Process_Declarations (Actions (Decl), Preprocess); |
| 2669 |
end if; |
| 2670 |
|
| 2671 |
-- Nested package declarations, avoid generics |
| 2672 |
|
| 2673 |
elsif Nkind (Decl) = N_Package_Declaration then |
| 2674 |
Pack_Id := Defining_Entity (Decl); |
| 2675 |
Spec := Specification (Decl); |
| 2676 |
|
| 2677 |
-- Do not inspect an ignored Ghost package because all code |
| 2678 |
-- found within will not appear in the final tree. |
| 2679 |
|
| 2680 |
if Is_Ignored_Ghost_Entity (Pack_Id) then |
| 2681 |
null; |
| 2682 |
|
| 2683 |
elsif Ekind (Pack_Id) /= E_Generic_Package then |
| 2684 |
Process_Declarations |
| 2685 |
(Private_Declarations (Spec), Preprocess); |
| 2686 |
Process_Declarations |
| 2687 |
(Visible_Declarations (Spec), Preprocess); |
| 2688 |
end if; |
| 2689 |
|
| 2690 |
-- Nested package bodies, avoid generics |
| 2691 |
|
| 2692 |
elsif Nkind (Decl) = N_Package_Body then |
| 2693 |
Process_Package_Body (Decl); |
| 2694 |
|
| 2695 |
elsif Nkind (Decl) = N_Package_Body_Stub |
| 2696 |
and then Present (Stub_Subunit (Decl)) |
| 2697 |
then |
| 2698 |
Process_Package_Body |
| 2699 |
(Proper_Body (Unit (Stub_Subunit (Decl)))); |
| 2700 |
end if; |
| 2701 |
|
| 2702 |
Decl := Prev; |
| 2703 |
end loop; |
| 2704 |
end Process_Declarations; |
| 2705 |
|
| 2706 |
-------------------------------- |
| 2707 |
-- Process_Object_Declaration -- |
| 2708 |
-------------------------------- |
| 2709 |
|
| 2710 |
procedure Process_Object_Declaration |
| 2711 |
(Decl : Node_Id; |
| 2712 |
Is_Protected : Boolean := False) |
| 2713 |
is |
| 2714 |
Obj_Id : constant Entity_Id := Defining_Identifier (Decl); |
| 2715 |
Loc : constant Source_Ptr := Sloc (Decl); |
| 2716 |
|
| 2717 |
Fin_Call : Node_Id; |
| 2718 |
Fin_Id : Entity_Id; |
| 2719 |
Master_Node_Attach : Node_Id; |
| 2720 |
Master_Node_Decl : Node_Id; |
| 2721 |
Master_Node_Id : Entity_Id; |
| 2722 |
Master_Node_Ins : Node_Id; |
| 2723 |
Master_Node_Loc : Source_Ptr; |
| 2724 |
Obj_Ref : Node_Id; |
| 2725 |
Obj_Typ : Entity_Id; |
| 2726 |
|
| 2727 |
-- Start of processing for Process_Object_Declaration |
| 2728 |
|
| 2729 |
begin |
| 2730 |
-- Handle the object type and the reference to the object. Note |
| 2731 |
-- that objects having simple protected components or of a CW type |
| 2732 |
-- must retain their original type for the processing below to work. |
| 2733 |
|
| 2734 |
Obj_Ref := New_Occurrence_Of (Obj_Id, Loc); |
| 2735 |
Obj_Typ := Etype (Obj_Id); |
| 2736 |
if not Is_Protected and then not Is_Class_Wide_Type (Obj_Typ) then |
| 2737 |
Obj_Typ := Base_Type (Obj_Typ); |
| 2738 |
end if; |
| 2739 |
|
| 2740 |
if Is_Access_Type (Obj_Typ) then |
| 2741 |
Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref); |
| 2742 |
Obj_Typ := Available_View (Designated_Type (Obj_Typ)); |
| 2743 |
end if; |
| 2744 |
|
| 2745 |
-- If the object is a Master_Node, then nothing to do, unless there |
| 2746 |
-- is no or a single controlled object with strict semantics, in |
| 2747 |
-- which case we move its declaration, call marker (if any) and |
| 2748 |
-- initialization call, and also mark it to avoid double processing. |
| 2749 |
|
| 2750 |
if Is_RTE (Obj_Typ, RE_Master_Node) then |
| 2751 |
Master_Node_Id := Obj_Id; |
| 2752 |
|
| 2753 |
if not Has_Strict_Ctrl_Objs or else Count = 1 then |
| 2754 |
if Nkind (Next (Decl)) = N_Call_Marker then |
| 2755 |
Prepend_To (Decls, Remove_Next (Next (Decl))); |
| 2756 |
end if; |
| 2757 |
Prepend_To (Decls, Remove_Next (Decl)); |
| 2758 |
Remove (Decl); |
| 2759 |
Prepend_To (Decls, Decl); |
| 2760 |
Set_Is_Ignored_For_Finalization (Obj_Id); |
| 2761 |
end if; |
| 2762 |
|
| 2763 |
-- Create the declaration of the Master_Node for the object and |
| 2764 |
-- insert it before the declaration of the object itself, unless |
| 2765 |
-- there is no or a single controlled object with strict semantics, |
| 2766 |
-- because it will effectively play the role of a degenerated master |
| 2767 |
-- and therefore needs to be inserted at the same place the master |
| 2768 |
-- would have been. |
| 2769 |
|
| 2770 |
else pragma Assert (No (Finalization_Master_Node (Obj_Id))); |
| 2771 |
-- In the latter case, use the Sloc the master would have had |
| 2772 |
|
| 2773 |
if not Has_Strict_Ctrl_Objs or else Count = 1 then |
| 2774 |
Master_Node_Loc := Sloc (N); |
| 2775 |
else |
| 2776 |
Master_Node_Loc := Loc; |
| 2777 |
end if; |
| 2778 |
|
| 2779 |
Master_Node_Id := |
| 2780 |
Make_Defining_Identifier (Master_Node_Loc, |
| 2781 |
Chars => New_External_Name (Chars (Obj_Id), Suffix => "MN")); |
| 2782 |
|
| 2783 |
Master_Node_Decl := |
| 2784 |
Make_Master_Node_Declaration (Master_Node_Loc, |
| 2785 |
Master_Node_Id, Obj_Id); |
| 2786 |
|
| 2787 |
Push_Scope (Scope (Obj_Id)); |
| 2788 |
|
| 2789 |
-- Avoid generating duplicate names for master nodes |
| 2790 |
|
| 2791 |
if Ekind (Obj_Id) = E_Loop_Parameter |
| 2792 |
and then |
| 2793 |
Present (Current_Entity_In_Scope (Chars (Master_Node_Id))) |
| 2794 |
then |
| 2795 |
Set_Chars (Master_Node_Id, |
| 2796 |
New_External_Name (Chars (Obj_Id), |
| 2797 |
Suffix => "MN", |
| 2798 |
Suffix_Index => -1)); |
| 2799 |
end if; |
| 2800 |
|
| 2801 |
if not Has_Strict_Ctrl_Objs or else Count = 1 then |
| 2802 |
Prepend_To (Decls, Master_Node_Decl); |
| 2803 |
else |
| 2804 |
Insert_Before (Decl, Master_Node_Decl); |
| 2805 |
end if; |
| 2806 |
|
| 2807 |
Analyze (Master_Node_Decl); |
| 2808 |
Pop_Scope; |
| 2809 |
|
| 2810 |
-- Mark the Master_Node to avoid double processing |
| 2811 |
|
| 2812 |
Set_Is_Ignored_For_Finalization (Master_Node_Id); |
| 2813 |
end if; |
| 2814 |
|
| 2815 |
-- Attach the Master_Node after all initialization has been done. The |
| 2816 |
-- place of insertion depends on the context. |
| 2817 |
|
| 2818 |
if Ekind (Obj_Id) in E_Constant | E_Variable then |
| 2819 |
|
| 2820 |
-- The object has delayed freezing. The Master_Node insertion |
| 2821 |
-- point is after the freeze node. |
| 2822 |
|
| 2823 |
if Has_Delayed_Freeze (Obj_Id) then |
| 2824 |
Master_Node_Ins := Freeze_Node (Obj_Id); |
| 2825 |
|
| 2826 |
-- The object is initialized by an aggregate. The Master_Node |
| 2827 |
-- insertion point is after the last aggregate assignment. |
| 2828 |
|
| 2829 |
elsif Present (Last_Aggregate_Assignment (Obj_Id)) then |
| 2830 |
Master_Node_Ins := Last_Aggregate_Assignment (Obj_Id); |
| 2831 |
|
| 2832 |
-- The object is initialized by a build-in-place function call. |
| 2833 |
-- The Master_Node insertion point is after the function call. |
| 2834 |
|
| 2835 |
elsif Present (BIP_Initialization_Call (Obj_Id)) then |
| 2836 |
Master_Node_Ins := BIP_Initialization_Call (Obj_Id); |
| 2837 |
|
| 2838 |
-- In other cases the Master_Node is inserted after the last call |
| 2839 |
-- to either [Deep_]Initialize or the type-specific init proc. |
| 2840 |
|
| 2841 |
else |
| 2842 |
Master_Node_Ins := Find_Last_Init (Decl); |
| 2843 |
end if; |
| 2844 |
|
| 2845 |
-- In all other cases the Master_Node is inserted after the last call |
| 2846 |
-- to either [Deep_]Initialize or the type-specific init proc. |
| 2847 |
|
| 2848 |
else |
| 2849 |
Master_Node_Ins := Find_Last_Init (Decl); |
| 2850 |
end if; |
| 2851 |
|
| 2852 |
-- If the Initialize function is null or trivial, the call will have |
| 2853 |
-- been replaced with a null statement and we place the attachment |
| 2854 |
-- of the Master_Node after the declaration of the object itself. |
| 2855 |
|
| 2856 |
if No (Master_Node_Ins) then |
| 2857 |
Master_Node_Ins := Decl; |
| 2858 |
end if; |
| 2859 |
|
| 2860 |
-- Processing for simple protected objects. Such objects require |
| 2861 |
-- manual finalization of their lock managers. Generate: |
| 2862 |
|
| 2863 |
-- procedure obj_typ_nnFD (v : system__address) is |
| 2864 |
-- type Ptr_Typ is access all Obj_Typ; |
| 2865 |
-- Rnn : Obj_Typ renames Ptr_Typ!(v).all; |
| 2866 |
-- begin |
| 2867 |
-- $system__tasking__protected_objects__finalize_protection |
| 2868 |
-- (Obj_TypV!(Rnn)._object); |
| 2869 |
-- exception |
| 2870 |
-- when others => |
| 2871 |
-- null; |
| 2872 |
-- end obj_typ_nnFD; |
| 2873 |
|
| 2874 |
if Is_Protected |
| 2875 |
or else (Has_Simple_Protected_Object (Obj_Typ) |
| 2876 |
and then No (Finalize_Address (Obj_Typ))) |
| 2877 |
then |
| 2878 |
declare |
| 2879 |
Param : constant Entity_Id := |
| 2880 |
Make_Defining_Identifier (Loc, Name_V); |
| 2881 |
Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'P'); |
| 2882 |
Ren_Id : constant Entity_Id := Make_Temporary (Loc, 'R'); |
| 2883 |
Ren_Ref : constant Node_Id := New_Occurrence_Of (Ren_Id, Loc); |
| 2884 |
|
| 2885 |
Fin_Body : Node_Id; |
| 2886 |
Fin_Call : Node_Id; |
| 2887 |
Fin_Stmts : List_Id := No_List; |
| 2888 |
HSS : Node_Id; |
| 2889 |
|
| 2890 |
begin |
| 2891 |
Set_Etype (Ren_Ref, Obj_Typ); |
| 2892 |
|
| 2893 |
if Is_Simple_Protected_Type (Obj_Typ) then |
| 2894 |
Fin_Call := Cleanup_Protected_Object (Decl, Ren_Ref); |
| 2895 |
|
| 2896 |
if Present (Fin_Call) then |
| 2897 |
Fin_Stmts := New_List (Fin_Call); |
| 2898 |
end if; |
| 2899 |
|
| 2900 |
elsif Is_Array_Type (Obj_Typ) then |
| 2901 |
Fin_Stmts := Cleanup_Array (Decl, Ren_Ref, Obj_Typ); |
| 2902 |
|
| 2903 |
else |
| 2904 |
Fin_Stmts := Cleanup_Record (Decl, Ren_Ref, Obj_Typ); |
| 2905 |
end if; |
| 2906 |
|
| 2907 |
if No (Fin_Stmts) then |
| 2908 |
return; |
| 2909 |
end if; |
| 2910 |
|
| 2911 |
HSS := |
| 2912 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 2913 |
Statements => Fin_Stmts); |
| 2914 |
|
| 2915 |
if Exceptions_OK then |
| 2916 |
Set_Exception_Handlers (HSS, New_List ( |
| 2917 |
Make_Exception_Handler (Loc, |
| 2918 |
Exception_Choices => New_List ( |
| 2919 |
Make_Others_Choice (Loc)), |
| 2920 |
Statements => New_List ( |
| 2921 |
Make_Null_Statement (Loc))))); |
| 2922 |
end if; |
| 2923 |
|
| 2924 |
Fin_Id := |
| 2925 |
Make_Defining_Identifier (Loc, |
| 2926 |
Make_TSS_Name_Local (Obj_Typ, TSS_Finalize_Address)); |
| 2927 |
|
| 2928 |
Fin_Body := |
| 2929 |
Make_Subprogram_Body (Loc, |
| 2930 |
Specification => |
| 2931 |
Make_Procedure_Specification (Loc, |
| 2932 |
Defining_Unit_Name => Fin_Id, |
| 2933 |
|
| 2934 |
Parameter_Specifications => New_List ( |
| 2935 |
Make_Parameter_Specification (Loc, |
| 2936 |
Defining_Identifier => Param, |
| 2937 |
Parameter_Type => |
| 2938 |
New_Occurrence_Of (RTE (RE_Address), Loc)))), |
| 2939 |
|
| 2940 |
Declarations => New_List ( |
| 2941 |
Make_Full_Type_Declaration (Loc, |
| 2942 |
Defining_Identifier => Ptr_Typ, |
| 2943 |
Type_Definition => |
| 2944 |
Make_Access_To_Object_Definition (Loc, |
| 2945 |
All_Present => True, |
| 2946 |
Subtype_Indication => |
| 2947 |
New_Occurrence_Of (Obj_Typ, Loc))), |
| 2948 |
|
| 2949 |
Make_Object_Renaming_Declaration (Loc, |
| 2950 |
Defining_Identifier => Ren_Id, |
| 2951 |
Subtype_Mark => |
| 2952 |
New_Occurrence_Of (Obj_Typ, Loc), |
| 2953 |
Name => |
| 2954 |
Make_Explicit_Dereference (Loc, |
| 2955 |
Prefix => |
| 2956 |
Unchecked_Convert_To |
| 2957 |
(Ptr_Typ, New_Occurrence_Of (Param, Loc))))), |
| 2958 |
|
| 2959 |
Handled_Statement_Sequence => HSS); |
| 2960 |
|
| 2961 |
Push_Scope (Scope (Obj_Id)); |
| 2962 |
Insert_After_And_Analyze |
| 2963 |
(Master_Node_Ins, Fin_Body, Suppress => All_Checks); |
| 2964 |
Pop_Scope; |
| 2965 |
|
| 2966 |
Master_Node_Ins := Fin_Body; |
| 2967 |
end; |
| 2968 |
|
| 2969 |
-- If the object's subtype is an array that has a constrained first |
| 2970 |
-- subtype and is not this first subtype, we need to build a special |
| 2971 |
-- Finalize_Address primitive for the object's subtype because the |
| 2972 |
-- Finalize_Address primitive of the base type has been tailored to |
| 2973 |
-- the first subtype (see Make_Finalize_Address_Stmts). Generate: |
| 2974 |
|
| 2975 |
-- procedure obj_typ_nnFD (v : system__address) is |
| 2976 |
-- type Ptr_Typ is access all Obj_Typ; |
| 2977 |
-- begin |
| 2978 |
-- obj_typBDF (Ptr_Typ!(v).all, f => true); |
| 2979 |
-- end obj_typ_nnFD; |
| 2980 |
|
| 2981 |
elsif Is_Array_Type (Etype (Obj_Id)) |
| 2982 |
and then Is_Constrained (First_Subtype (Etype (Obj_Id))) |
| 2983 |
and then Etype (Obj_Id) /= First_Subtype (Etype (Obj_Id)) |
| 2984 |
then |
| 2985 |
declare |
| 2986 |
Ptr_Typ : constant Node_Id := Make_Temporary (Loc, 'P'); |
| 2987 |
Param : constant Entity_Id := |
| 2988 |
Make_Defining_Identifier (Loc, Name_V); |
| 2989 |
|
| 2990 |
Fin_Body : Node_Id; |
| 2991 |
|
| 2992 |
begin |
| 2993 |
Obj_Typ := Etype (Obj_Id); |
| 2994 |
|
| 2995 |
Fin_Id := |
| 2996 |
Make_Defining_Identifier (Loc, |
| 2997 |
Make_TSS_Name_Local |
| 2998 |
(Obj_Typ, TSS_Finalize_Address)); |
| 2999 |
|
| 3000 |
Fin_Body := |
| 3001 |
Make_Subprogram_Body (Loc, |
| 3002 |
Specification => |
| 3003 |
Make_Procedure_Specification (Loc, |
| 3004 |
Defining_Unit_Name => Fin_Id, |
| 3005 |
Parameter_Specifications => New_List ( |
| 3006 |
Make_Parameter_Specification (Loc, |
| 3007 |
Defining_Identifier => Param, |
| 3008 |
Parameter_Type => |
| 3009 |
New_Occurrence_Of (RTE (RE_Address), Loc)))), |
| 3010 |
|
| 3011 |
Declarations => New_List ( |
| 3012 |
Make_Full_Type_Declaration (Loc, |
| 3013 |
Defining_Identifier => Ptr_Typ, |
| 3014 |
Type_Definition => |
| 3015 |
Make_Access_To_Object_Definition (Loc, |
| 3016 |
All_Present => True, |
| 3017 |
Subtype_Indication => |
| 3018 |
New_Occurrence_Of (Obj_Typ, Loc)))), |
| 3019 |
|
| 3020 |
Handled_Statement_Sequence => |
| 3021 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 3022 |
Statements => New_List ( |
| 3023 |
Make_Final_Call ( |
| 3024 |
Obj_Ref => |
| 3025 |
Make_Explicit_Dereference (Loc, |
| 3026 |
Prefix => |
| 3027 |
Unchecked_Convert_To (Ptr_Typ, |
| 3028 |
Make_Identifier (Loc, Name_V))), |
| 3029 |
Typ => Obj_Typ)))); |
| 3030 |
|
| 3031 |
Push_Scope (Scope (Obj_Id)); |
| 3032 |
Insert_After_And_Analyze |
| 3033 |
(Master_Node_Ins, Fin_Body, Suppress => All_Checks); |
| 3034 |
Pop_Scope; |
| 3035 |
|
| 3036 |
Master_Node_Ins := Fin_Body; |
| 3037 |
end; |
| 3038 |
|
| 3039 |
else |
| 3040 |
Fin_Id := Finalize_Address (Obj_Typ); |
| 3041 |
|
| 3042 |
if No (Fin_Id) and then Ekind (Obj_Typ) = E_Class_Wide_Subtype then |
| 3043 |
Fin_Id := TSS (Obj_Typ, TSS_Finalize_Address); |
| 3044 |
end if; |
| 3045 |
end if; |
| 3046 |
|
| 3047 |
-- Now build the attachment call that will initialize the object's |
| 3048 |
-- Master_Node using the object's address and type's finalization |
| 3049 |
-- procedure and then attach the Master_Node to the master, unless |
| 3050 |
-- there is no or a single controlled object with strict semantics. |
| 3051 |
|
| 3052 |
if not Has_Strict_Ctrl_Objs or else Count = 1 then |
| 3053 |
-- Finalize_Address is not generated in CodePeer mode because the |
| 3054 |
-- body contains address arithmetic. So we don't want to generate |
| 3055 |
-- the attach in this case. Ditto if the object is a Master_Node. |
| 3056 |
|
| 3057 |
if CodePeer_Mode or else Obj_Id = Master_Node_Id then |
| 3058 |
Master_Node_Attach := Make_Null_Statement (Loc); |
| 3059 |
|
| 3060 |
else |
| 3061 |
Master_Node_Attach := |
| 3062 |
Make_Procedure_Call_Statement (Loc, |
| 3063 |
Name => |
| 3064 |
New_Occurrence_Of (RTE (RE_Attach_Object_To_Node), Loc), |
| 3065 |
Parameter_Associations => New_List ( |
| 3066 |
Make_Address_For_Finalize (Loc, Obj_Ref, Obj_Typ), |
| 3067 |
Make_Attribute_Reference (Loc, |
| 3068 |
Prefix => New_Occurrence_Of (Fin_Id, Loc), |
| 3069 |
Attribute_Name => Name_Unrestricted_Access), |
| 3070 |
New_Occurrence_Of (Master_Node_Id, Loc))); |
| 3071 |
|
| 3072 |
Set_Finalize_Address_For_Node (Master_Node_Id, Fin_Id); |
| 3073 |
end if; |
| 3074 |
|
| 3075 |
-- We also generate the direct finalization call here |
| 3076 |
|
| 3077 |
Fin_Call := Make_Finalize_Call_For_Node (Loc, Master_Node_Id); |
| 3078 |
|
| 3079 |
-- For CodePeer, the exception handlers normally generated here |
| 3080 |
-- generate complex flowgraphs which result in capacity problems. |
| 3081 |
-- Omitting these handlers for CodePeer is justified as follows: |
| 3082 |
|
| 3083 |
-- If a handler is dead, then omitting it is surely ok |
| 3084 |
|
| 3085 |
-- If a handler is live, then CodePeer should flag the |
| 3086 |
-- potentially-exception-raising construct that causes it |
| 3087 |
-- to be live. That is what we are interested in, not what |
| 3088 |
-- happens after the exception is raised. |
| 3089 |
|
| 3090 |
if Has_Strict_Ctrl_Objs |
| 3091 |
and then Exceptions_OK |
| 3092 |
and then not CodePeer_Mode |
| 3093 |
then |
| 3094 |
Fin_Call := |
| 3095 |
Make_Block_Statement (Loc, |
| 3096 |
Handled_Statement_Sequence => |
| 3097 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 3098 |
Statements => New_List (Fin_Call), |
| 3099 |
|
| 3100 |
Exception_Handlers => New_List ( |
| 3101 |
Build_Exception_Handler |
| 3102 |
(Finalizer_Data, For_Package)))); |
| 3103 |
end if; |
| 3104 |
|
| 3105 |
Append_To (Finalizer_Stmts, Fin_Call); |
| 3106 |
|
| 3107 |
else |
| 3108 |
-- If the object is a Master_Node, we just need to chain it |
| 3109 |
|
| 3110 |
if Obj_Id = Master_Node_Id then |
| 3111 |
Master_Node_Attach := |
| 3112 |
Make_Procedure_Call_Statement (Loc, |
| 3113 |
Name => |
| 3114 |
New_Occurrence_Of (RTE (RE_Chain_Node_To_Master), Loc), |
| 3115 |
Parameter_Associations => New_List ( |
| 3116 |
Make_Attribute_Reference (Loc, |
| 3117 |
Prefix => New_Occurrence_Of (Obj_Id, Loc), |
| 3118 |
Attribute_Name => Name_Unrestricted_Access), |
| 3119 |
New_Occurrence_Of (Finalization_Master, Loc))); |
| 3120 |
|
| 3121 |
-- Finalize_Address is not generated in CodePeer mode because the |
| 3122 |
-- body contains address arithmetic. So we don't want to generate |
| 3123 |
-- the attach in this case. |
| 3124 |
|
| 3125 |
elsif CodePeer_Mode then |
| 3126 |
Master_Node_Attach := Make_Null_Statement (Loc); |
| 3127 |
|
| 3128 |
else |
| 3129 |
Master_Node_Attach := |
| 3130 |
Make_Procedure_Call_Statement (Loc, |
| 3131 |
Name => |
| 3132 |
New_Occurrence_Of (RTE (RE_Attach_Object_To_Master), Loc), |
| 3133 |
Parameter_Associations => New_List ( |
| 3134 |
Make_Address_For_Finalize (Loc, Obj_Ref, Obj_Typ), |
| 3135 |
Make_Attribute_Reference (Loc, |
| 3136 |
Prefix => New_Occurrence_Of (Fin_Id, Loc), |
| 3137 |
Attribute_Name => Name_Unrestricted_Access), |
| 3138 |
Make_Attribute_Reference (Loc, |
| 3139 |
Prefix => |
| 3140 |
New_Occurrence_Of (Master_Node_Id, Loc), |
| 3141 |
Attribute_Name => Name_Unrestricted_Access), |
| 3142 |
New_Occurrence_Of (Finalization_Master, Loc))); |
| 3143 |
end if; |
| 3144 |
end if; |
| 3145 |
|
| 3146 |
Insert_After_And_Analyze |
| 3147 |
(Master_Node_Ins, Master_Node_Attach, Suppress => All_Checks); |
| 3148 |
end Process_Object_Declaration; |
| 3149 |
|
| 3150 |
------------------------------------- |
| 3151 |
-- Process_Tagged_Type_Declaration -- |
| 3152 |
------------------------------------- |
| 3153 |
|
| 3154 |
procedure Process_Tagged_Type_Declaration (Decl : Node_Id) is |
| 3155 |
Typ : constant Entity_Id := Defining_Identifier (Decl); |
| 3156 |
DT_Ptr : constant Entity_Id := |
| 3157 |
Node (First_Elmt (Access_Disp_Table (Typ))); |
| 3158 |
begin |
| 3159 |
-- Generate: |
| 3160 |
-- Ada.Tags.Unregister_Tag (<Typ>P); |
| 3161 |
|
| 3162 |
Append_To (Tagged_Type_Stmts, |
| 3163 |
Make_Procedure_Call_Statement (Loc, |
| 3164 |
Name => |
| 3165 |
New_Occurrence_Of (RTE (RE_Unregister_Tag), Loc), |
| 3166 |
Parameter_Associations => New_List ( |
| 3167 |
New_Occurrence_Of (DT_Ptr, Loc)))); |
| 3168 |
end Process_Tagged_Type_Declaration; |
| 3169 |
|
| 3170 |
-- Start of processing for Build_Finalizer |
| 3171 |
|
| 3172 |
begin |
| 3173 |
Fin_Id := Empty; |
| 3174 |
|
| 3175 |
-- Do not perform this expansion in SPARK mode because it is not |
| 3176 |
-- necessary. |
| 3177 |
|
| 3178 |
if GNATprove_Mode then |
| 3179 |
return; |
| 3180 |
end if; |
| 3181 |
|
| 3182 |
-- Step 1: Extract all lists which may contain controlled objects or |
| 3183 |
-- library-level tagged types. |
| 3184 |
|
| 3185 |
if For_Package_Spec then |
| 3186 |
Decls := Visible_Declarations (Specification (N)); |
| 3187 |
Priv_Decls := Private_Declarations (Specification (N)); |
| 3188 |
|
| 3189 |
-- Retrieve the package spec id |
| 3190 |
|
| 3191 |
Spec_Id := Defining_Unit_Name (Specification (N)); |
| 3192 |
|
| 3193 |
if Nkind (Spec_Id) = N_Defining_Program_Unit_Name then |
| 3194 |
Spec_Id := Defining_Identifier (Spec_Id); |
| 3195 |
end if; |
| 3196 |
|
| 3197 |
-- Accept statement, block, entry body, package body, protected body, |
| 3198 |
-- subprogram body or task body. |
| 3199 |
|
| 3200 |
else |
| 3201 |
Decls := Declarations (N); |
| 3202 |
HSS := Handled_Statement_Sequence (N); |
| 3203 |
|
| 3204 |
if Present (HSS) then |
| 3205 |
if Present (Statements (HSS)) then |
| 3206 |
Stmts := Statements (HSS); |
| 3207 |
end if; |
| 3208 |
|
| 3209 |
if Present (At_End_Proc (HSS)) then |
| 3210 |
Prev_At_End := At_End_Proc (HSS); |
| 3211 |
end if; |
| 3212 |
end if; |
| 3213 |
|
| 3214 |
-- Retrieve the package spec id for package bodies |
| 3215 |
|
| 3216 |
if For_Package_Body then |
| 3217 |
Spec_Id := Corresponding_Spec (N); |
| 3218 |
end if; |
| 3219 |
end if; |
| 3220 |
|
| 3221 |
-- We do not need to process nested packages since they are handled by |
| 3222 |
-- the finalizer of the enclosing scope, including at library level. |
| 3223 |
-- And we do not build two finalizers for an instance without body that |
| 3224 |
-- is a library unit (see Analyze_Package_Instantiation). |
| 3225 |
|
| 3226 |
if For_Package |
| 3227 |
and then (not Is_Compilation_Unit (Spec_Id) |
| 3228 |
or else (Is_Generic_Instance (Spec_Id) |
| 3229 |
and then Package_Instantiation (Spec_Id) = N)) |
| 3230 |
then |
| 3231 |
return; |
| 3232 |
end if; |
| 3233 |
|
| 3234 |
-- Step 2: Object [pre]processing |
| 3235 |
|
| 3236 |
if For_Package then |
| 3237 |
-- For package specs and bodies, we are invoked from the Standard |
| 3238 |
-- scope, so we need to push the specs onto the scope stack first. |
| 3239 |
|
| 3240 |
Push_Scope (Spec_Id); |
| 3241 |
|
| 3242 |
-- Preprocess the visible declarations now in order to obtain the |
| 3243 |
-- correct number of controlled object by the time the private |
| 3244 |
-- declarations are processed. |
| 3245 |
|
| 3246 |
Process_Declarations (Decls, Preprocess => True); |
| 3247 |
|
| 3248 |
-- From all the possible contexts, only package specifications may |
| 3249 |
-- have private declarations. |
| 3250 |
|
| 3251 |
if For_Package_Spec then |
| 3252 |
Process_Declarations (Priv_Decls, Preprocess => True); |
| 3253 |
end if; |
| 3254 |
|
| 3255 |
-- The current context may lack controlled objects, but require some |
| 3256 |
-- other form of completion (task termination for instance). In such |
| 3257 |
-- cases, the finalizer must be created and carry the additional |
| 3258 |
-- statements. |
| 3259 |
|
| 3260 |
if Acts_As_Clean or else Has_Ctrl_Objs or else Has_Tagged_Types then |
| 3261 |
Build_Components; |
| 3262 |
end if; |
| 3263 |
|
| 3264 |
-- The preprocessing has determined that the context has controlled |
| 3265 |
-- objects or library-level tagged types. |
| 3266 |
|
| 3267 |
if Has_Ctrl_Objs or else Has_Tagged_Types then |
| 3268 |
|
| 3269 |
-- Private declarations are processed first in order to preserve |
| 3270 |
-- possible dependencies between public and private objects. |
| 3271 |
|
| 3272 |
if For_Package_Spec then |
| 3273 |
Process_Declarations (Priv_Decls); |
| 3274 |
end if; |
| 3275 |
|
| 3276 |
Process_Declarations (Decls); |
| 3277 |
end if; |
| 3278 |
|
| 3279 |
-- Non-package case |
| 3280 |
|
| 3281 |
else |
| 3282 |
-- Preprocess both declarations and statements |
| 3283 |
|
| 3284 |
Process_Declarations (Decls, Preprocess => True); |
| 3285 |
Process_Declarations (Stmts, Preprocess => True); |
| 3286 |
|
| 3287 |
-- At this point it is known that N has controlled objects. Ensure |
| 3288 |
-- that N has a declarative list since the finalizer spec will be |
| 3289 |
-- attached to it. |
| 3290 |
|
| 3291 |
if Has_Ctrl_Objs and then No (Decls) then |
| 3292 |
Set_Declarations (N, New_List); |
| 3293 |
Decls := Declarations (N); |
| 3294 |
end if; |
| 3295 |
|
| 3296 |
-- The current context may lack controlled objects, but require some |
| 3297 |
-- other form of completion (task termination for instance). In such |
| 3298 |
-- cases, the finalizer must be created and carry the additional |
| 3299 |
-- statements. |
| 3300 |
|
| 3301 |
if Acts_As_Clean or else Has_Ctrl_Objs or else Has_Tagged_Types then |
| 3302 |
Build_Components; |
| 3303 |
end if; |
| 3304 |
|
| 3305 |
if Has_Ctrl_Objs or else Has_Tagged_Types then |
| 3306 |
Process_Declarations (Stmts); |
| 3307 |
Process_Declarations (Decls); |
| 3308 |
end if; |
| 3309 |
end if; |
| 3310 |
|
| 3311 |
-- Step 3: Finalizer creation |
| 3312 |
|
| 3313 |
if Acts_As_Clean or else Has_Ctrl_Objs or else Has_Tagged_Types then |
| 3314 |
Create_Finalizer; |
| 3315 |
end if; |
| 3316 |
|
| 3317 |
-- Pop the scope that was pushed above for package specs and bodies |
| 3318 |
|
| 3319 |
if For_Package then |
| 3320 |
Pop_Scope; |
| 3321 |
end if; |
| 3322 |
end Build_Finalizer; |
| 3323 |
|
| 3324 |
-------------------------- |
| 3325 |
-- Build_Finalizer_Call -- |
| 3326 |
-------------------------- |
| 3327 |
|
| 3328 |
procedure Build_Finalizer_Call (N : Node_Id; Fin_Id : Entity_Id) is |
| 3329 |
begin |
| 3330 |
-- Do not perform this expansion in SPARK mode because we do not create |
| 3331 |
-- finalizers in the first place. |
| 3332 |
|
| 3333 |
if GNATprove_Mode then |
| 3334 |
return; |
| 3335 |
end if; |
| 3336 |
|
| 3337 |
-- If the construct to be cleaned up is a protected subprogram body, the |
| 3338 |
-- finalizer call needs to be associated with the block that wraps the |
| 3339 |
-- unprotected version of the subprogram. The following illustrates this |
| 3340 |
-- scenario: |
| 3341 |
|
| 3342 |
-- procedure Prot_SubpP is |
| 3343 |
-- procedure finalizer is |
| 3344 |
-- begin |
| 3345 |
-- Service_Entries (Prot_Obj); |
| 3346 |
-- Abort_Undefer; |
| 3347 |
-- end finalizer; |
| 3348 |
|
| 3349 |
-- begin |
| 3350 |
-- . . . |
| 3351 |
-- begin |
| 3352 |
-- Prot_SubpN (Prot_Obj); |
| 3353 |
-- at end |
| 3354 |
-- finalizer; |
| 3355 |
-- end; |
| 3356 |
-- end Prot_SubpP; |
| 3357 |
|
| 3358 |
declare |
| 3359 |
Loc : constant Source_Ptr := Sloc (N); |
| 3360 |
|
| 3361 |
Is_Protected_Subp_Body : constant Boolean := |
| 3362 |
Nkind (N) = N_Subprogram_Body |
| 3363 |
and then Is_Protected_Subprogram_Body (N); |
| 3364 |
-- True if N is the protected version of a subprogram that belongs to |
| 3365 |
-- a protected type. |
| 3366 |
|
| 3367 |
HSS : constant Node_Id := |
| 3368 |
(if Is_Protected_Subp_Body |
| 3369 |
then Handled_Statement_Sequence |
| 3370 |
(Last (Statements (Handled_Statement_Sequence (N)))) |
| 3371 |
else Handled_Statement_Sequence (N)); |
| 3372 |
|
| 3373 |
-- We attach the At_End_Proc to the HSS if this is an accept |
| 3374 |
-- statement or extended return statement. Also in the case of |
| 3375 |
-- a protected subprogram, because if Service_Entries raises an |
| 3376 |
-- exception, we do not lock the PO, so we also do not want to |
| 3377 |
-- unlock it. |
| 3378 |
|
| 3379 |
Use_HSS : constant Boolean := |
| 3380 |
Nkind (N) in N_Accept_Statement | N_Extended_Return_Statement |
| 3381 |
or else Is_Protected_Subp_Body; |
| 3382 |
|
| 3383 |
At_End_Proc_Bearer : constant Node_Id := (if Use_HSS then HSS else N); |
| 3384 |
begin |
| 3385 |
pragma Assert (No (At_End_Proc (At_End_Proc_Bearer))); |
| 3386 |
Set_At_End_Proc (At_End_Proc_Bearer, New_Occurrence_Of (Fin_Id, Loc)); |
| 3387 |
-- Attach reference to finalizer to tree, for LLVM use |
| 3388 |
Set_Parent (At_End_Proc (At_End_Proc_Bearer), At_End_Proc_Bearer); |
| 3389 |
Analyze (At_End_Proc (At_End_Proc_Bearer)); |
| 3390 |
Expand_At_End_Handler (At_End_Proc_Bearer, Empty); |
| 3391 |
end; |
| 3392 |
end Build_Finalizer_Call; |
| 3393 |
|
| 3394 |
--------------------- |
| 3395 |
-- Build_Late_Proc -- |
| 3396 |
--------------------- |
| 3397 |
|
| 3398 |
procedure Build_Late_Proc (Typ : Entity_Id; Nam : Name_Id) is |
| 3399 |
begin |
| 3400 |
for Final_Prim in Name_Of'Range loop |
| 3401 |
if Name_Of (Final_Prim) = Nam then |
| 3402 |
Set_TSS (Typ, |
| 3403 |
Make_Deep_Proc |
| 3404 |
(Prim => Final_Prim, |
| 3405 |
Typ => Typ, |
| 3406 |
Stmts => Make_Deep_Record_Body (Final_Prim, Typ))); |
| 3407 |
end if; |
| 3408 |
end loop; |
| 3409 |
end Build_Late_Proc; |
| 3410 |
|
| 3411 |
------------------------------- |
| 3412 |
-- Build_Object_Declarations -- |
| 3413 |
------------------------------- |
| 3414 |
|
| 3415 |
procedure Build_Object_Declarations |
| 3416 |
(Data : out Finalization_Exception_Data; |
| 3417 |
Decls : List_Id; |
| 3418 |
Loc : Source_Ptr; |
| 3419 |
For_Package : Boolean := False) |
| 3420 |
is |
| 3421 |
Decl : Node_Id; |
| 3422 |
|
| 3423 |
Dummy : Entity_Id; |
| 3424 |
-- This variable captures an unused dummy internal entity, see the |
| 3425 |
-- comment associated with its use. |
| 3426 |
|
| 3427 |
begin |
| 3428 |
pragma Assert (Decls /= No_List); |
| 3429 |
|
| 3430 |
-- Always set the proper location as it may be needed even when |
| 3431 |
-- exception propagation is forbidden. |
| 3432 |
|
| 3433 |
Data.Loc := Loc; |
| 3434 |
|
| 3435 |
if Restriction_Active (No_Exception_Propagation) then |
| 3436 |
Data.Abort_Id := Empty; |
| 3437 |
Data.E_Id := Empty; |
| 3438 |
Data.Raised_Id := Empty; |
| 3439 |
return; |
| 3440 |
end if; |
| 3441 |
|
| 3442 |
Data.Raised_Id := Make_Temporary (Loc, 'R'); |
| 3443 |
|
| 3444 |
-- In certain scenarios, finalization can be triggered by an abort. If |
| 3445 |
-- the finalization itself fails and raises an exception, the resulting |
| 3446 |
-- Program_Error must be supressed and replaced by an abort signal. In |
| 3447 |
-- order to detect this scenario, save the state of entry into the |
| 3448 |
-- finalization code. |
| 3449 |
|
| 3450 |
-- This is not needed for library-level finalizers as they are called by |
| 3451 |
-- the environment task and cannot be aborted. |
| 3452 |
|
| 3453 |
if not For_Package then |
| 3454 |
if Abort_Allowed then |
| 3455 |
Data.Abort_Id := Make_Temporary (Loc, 'A'); |
| 3456 |
|
| 3457 |
-- Generate: |
| 3458 |
-- Abort_Id : constant Boolean := <A_Expr>; |
| 3459 |
|
| 3460 |
Append_To (Decls, |
| 3461 |
Make_Object_Declaration (Loc, |
| 3462 |
Defining_Identifier => Data.Abort_Id, |
| 3463 |
Constant_Present => True, |
| 3464 |
Object_Definition => |
| 3465 |
New_Occurrence_Of (Standard_Boolean, Loc), |
| 3466 |
Expression => |
| 3467 |
New_Occurrence_Of (RTE (RE_Triggered_By_Abort), Loc))); |
| 3468 |
|
| 3469 |
-- Abort is not required |
| 3470 |
|
| 3471 |
else |
| 3472 |
-- Generate a dummy entity to ensure that the internal symbols are |
| 3473 |
-- in sync when a unit is compiled with and without aborts. |
| 3474 |
|
| 3475 |
Dummy := Make_Temporary (Loc, 'A'); |
| 3476 |
Data.Abort_Id := Empty; |
| 3477 |
end if; |
| 3478 |
|
| 3479 |
-- Library-level finalizers |
| 3480 |
|
| 3481 |
else |
| 3482 |
Data.Abort_Id := Empty; |
| 3483 |
end if; |
| 3484 |
|
| 3485 |
if Exception_Extra_Info then |
| 3486 |
Data.E_Id := Make_Temporary (Loc, 'E'); |
| 3487 |
|
| 3488 |
-- Generate: |
| 3489 |
-- E_Id : Exception_Occurrence; |
| 3490 |
|
| 3491 |
Decl := |
| 3492 |
Make_Object_Declaration (Loc, |
| 3493 |
Defining_Identifier => Data.E_Id, |
| 3494 |
Object_Definition => |
| 3495 |
New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc)); |
| 3496 |
Set_No_Initialization (Decl); |
| 3497 |
|
| 3498 |
Append_To (Decls, Decl); |
| 3499 |
|
| 3500 |
else |
| 3501 |
Data.E_Id := Empty; |
| 3502 |
end if; |
| 3503 |
|
| 3504 |
-- Generate: |
| 3505 |
-- Raised_Id : Boolean := False; |
| 3506 |
|
| 3507 |
Append_To (Decls, |
| 3508 |
Make_Object_Declaration (Loc, |
| 3509 |
Defining_Identifier => Data.Raised_Id, |
| 3510 |
Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), |
| 3511 |
Expression => New_Occurrence_Of (Standard_False, Loc))); |
| 3512 |
|
| 3513 |
if Debug_Generated_Code then |
| 3514 |
Set_Debug_Info_Needed (Data.Raised_Id); |
| 3515 |
end if; |
| 3516 |
end Build_Object_Declarations; |
| 3517 |
|
| 3518 |
--------------------------- |
| 3519 |
-- Build_Raise_Statement -- |
| 3520 |
--------------------------- |
| 3521 |
|
| 3522 |
function Build_Raise_Statement |
| 3523 |
(Data : Finalization_Exception_Data) return Node_Id |
| 3524 |
is |
| 3525 |
Stmt : Node_Id; |
| 3526 |
Expr : Node_Id; |
| 3527 |
|
| 3528 |
begin |
| 3529 |
-- Standard run-time use the specialized routine |
| 3530 |
-- Raise_From_Controlled_Operation. |
| 3531 |
|
| 3532 |
if Exception_Extra_Info |
| 3533 |
and then RTE_Available (RE_Raise_From_Controlled_Operation) |
| 3534 |
then |
| 3535 |
Stmt := |
| 3536 |
Make_Procedure_Call_Statement (Data.Loc, |
| 3537 |
Name => |
| 3538 |
New_Occurrence_Of |
| 3539 |
(RTE (RE_Raise_From_Controlled_Operation), Data.Loc), |
| 3540 |
Parameter_Associations => |
| 3541 |
New_List (New_Occurrence_Of (Data.E_Id, Data.Loc))); |
| 3542 |
|
| 3543 |
-- Restricted run-time: exception messages are not supported and hence |
| 3544 |
-- Raise_From_Controlled_Operation is not supported. Raise Program_Error |
| 3545 |
-- instead. |
| 3546 |
|
| 3547 |
else |
| 3548 |
Stmt := |
| 3549 |
Make_Raise_Program_Error (Data.Loc, |
| 3550 |
Reason => PE_Finalize_Raised_Exception); |
| 3551 |
end if; |
| 3552 |
|
| 3553 |
-- Generate: |
| 3554 |
|
| 3555 |
-- Raised_Id and then not Abort_Id |
| 3556 |
-- <or> |
| 3557 |
-- Raised_Id |
| 3558 |
|
| 3559 |
Expr := New_Occurrence_Of (Data.Raised_Id, Data.Loc); |
| 3560 |
|
| 3561 |
if Present (Data.Abort_Id) then |
| 3562 |
Expr := Make_And_Then (Data.Loc, |
| 3563 |
Left_Opnd => Expr, |
| 3564 |
Right_Opnd => |
| 3565 |
Make_Op_Not (Data.Loc, |
| 3566 |
Right_Opnd => New_Occurrence_Of (Data.Abort_Id, Data.Loc))); |
| 3567 |
end if; |
| 3568 |
|
| 3569 |
-- Generate: |
| 3570 |
|
| 3571 |
-- if Raised_Id and then not Abort_Id then |
| 3572 |
-- Raise_From_Controlled_Operation (E_Id); |
| 3573 |
-- <or> |
| 3574 |
-- raise Program_Error; -- restricted runtime |
| 3575 |
-- end if; |
| 3576 |
|
| 3577 |
return |
| 3578 |
Make_If_Statement (Data.Loc, |
| 3579 |
Condition => Expr, |
| 3580 |
Then_Statements => New_List (Stmt)); |
| 3581 |
end Build_Raise_Statement; |
| 3582 |
|
| 3583 |
----------------------------- |
| 3584 |
-- Build_Record_Deep_Procs -- |
| 3585 |
----------------------------- |
| 3586 |
|
| 3587 |
procedure Build_Record_Deep_Procs (Typ : Entity_Id) is |
| 3588 |
begin |
| 3589 |
Set_TSS (Typ, |
| 3590 |
Make_Deep_Proc |
| 3591 |
(Prim => Initialize_Case, |
| 3592 |
Typ => Typ, |
| 3593 |
Stmts => Make_Deep_Record_Body (Initialize_Case, Typ))); |
| 3594 |
|
| 3595 |
if not Is_Inherently_Limited_Type (Typ) then |
| 3596 |
Set_TSS (Typ, |
| 3597 |
Make_Deep_Proc |
| 3598 |
(Prim => Adjust_Case, |
| 3599 |
Typ => Typ, |
| 3600 |
Stmts => Make_Deep_Record_Body (Adjust_Case, Typ))); |
| 3601 |
end if; |
| 3602 |
|
| 3603 |
-- Do not generate Deep_Finalize and Finalize_Address if finalization is |
| 3604 |
-- suppressed since these routine will not be used. |
| 3605 |
|
| 3606 |
if not Restriction_Active (No_Finalization) then |
| 3607 |
Set_TSS (Typ, |
| 3608 |
Make_Deep_Proc |
| 3609 |
(Prim => Finalize_Case, |
| 3610 |
Typ => Typ, |
| 3611 |
Stmts => Make_Deep_Record_Body (Finalize_Case, Typ))); |
| 3612 |
|
| 3613 |
-- Create TSS primitive Finalize_Address (unless CodePeer_Mode) |
| 3614 |
|
| 3615 |
if not CodePeer_Mode then |
| 3616 |
Set_TSS (Typ, |
| 3617 |
Make_Deep_Proc |
| 3618 |
(Prim => Address_Case, |
| 3619 |
Typ => Typ, |
| 3620 |
Stmts => Make_Deep_Record_Body (Address_Case, Typ))); |
| 3621 |
end if; |
| 3622 |
end if; |
| 3623 |
end Build_Record_Deep_Procs; |
| 3624 |
|
| 3625 |
------------------- |
| 3626 |
-- Cleanup_Array -- |
| 3627 |
------------------- |
| 3628 |
|
| 3629 |
function Cleanup_Array |
| 3630 |
(N : Node_Id; |
| 3631 |
Obj : Node_Id; |
| 3632 |
Typ : Entity_Id) return List_Id |
| 3633 |
is |
| 3634 |
Loc : constant Source_Ptr := Sloc (N); |
| 3635 |
Index_List : constant List_Id := New_List; |
| 3636 |
|
| 3637 |
function Free_Component return List_Id; |
| 3638 |
-- Generate the code to finalize the task or protected subcomponents |
| 3639 |
-- of a single component of the array. |
| 3640 |
|
| 3641 |
function Free_One_Dimension (Dim : Int) return List_Id; |
| 3642 |
-- Generate a loop over one dimension of the array |
| 3643 |
|
| 3644 |
-------------------- |
| 3645 |
-- Free_Component -- |
| 3646 |
-------------------- |
| 3647 |
|
| 3648 |
function Free_Component return List_Id is |
| 3649 |
Stmts : List_Id := New_List; |
| 3650 |
Tsk : Node_Id; |
| 3651 |
C_Typ : constant Entity_Id := Component_Type (Typ); |
| 3652 |
|
| 3653 |
begin |
| 3654 |
-- Component type is known to contain tasks or protected objects |
| 3655 |
|
| 3656 |
Tsk := |
| 3657 |
Make_Indexed_Component (Loc, |
| 3658 |
Prefix => Duplicate_Subexpr_No_Checks (Obj), |
| 3659 |
Expressions => Index_List); |
| 3660 |
|
| 3661 |
Set_Etype (Tsk, C_Typ); |
| 3662 |
|
| 3663 |
if Is_Task_Type (C_Typ) then |
| 3664 |
Append_To (Stmts, Cleanup_Task (N, Tsk)); |
| 3665 |
|
| 3666 |
elsif Is_Simple_Protected_Type (C_Typ) then |
| 3667 |
Append_To (Stmts, Cleanup_Protected_Object (N, Tsk)); |
| 3668 |
|
| 3669 |
elsif Is_Record_Type (C_Typ) then |
| 3670 |
Stmts := Cleanup_Record (N, Tsk, C_Typ); |
| 3671 |
|
| 3672 |
elsif Is_Array_Type (C_Typ) then |
| 3673 |
Stmts := Cleanup_Array (N, Tsk, C_Typ); |
| 3674 |
end if; |
| 3675 |
|
| 3676 |
return Stmts; |
| 3677 |
end Free_Component; |
| 3678 |
|
| 3679 |
------------------------ |
| 3680 |
-- Free_One_Dimension -- |
| 3681 |
------------------------ |
| 3682 |
|
| 3683 |
function Free_One_Dimension (Dim : Int) return List_Id is |
| 3684 |
Index : Entity_Id; |
| 3685 |
|
| 3686 |
begin |
| 3687 |
if Dim > Number_Dimensions (Typ) then |
| 3688 |
return Free_Component; |
| 3689 |
|
| 3690 |
-- Here we generate the required loop |
| 3691 |
|
| 3692 |
else |
| 3693 |
Index := Make_Temporary (Loc, 'J'); |
| 3694 |
Append (New_Occurrence_Of (Index, Loc), Index_List); |
| 3695 |
|
| 3696 |
return New_List ( |
| 3697 |
Make_Implicit_Loop_Statement (N, |
| 3698 |
Identifier => Empty, |
| 3699 |
Iteration_Scheme => |
| 3700 |
Make_Iteration_Scheme (Loc, |
| 3701 |
Loop_Parameter_Specification => |
| 3702 |
Make_Loop_Parameter_Specification (Loc, |
| 3703 |
Defining_Identifier => Index, |
| 3704 |
Discrete_Subtype_Definition => |
| 3705 |
Make_Attribute_Reference (Loc, |
| 3706 |
Prefix => Duplicate_Subexpr (Obj), |
| 3707 |
Attribute_Name => Name_Range, |
| 3708 |
Expressions => New_List ( |
| 3709 |
Make_Integer_Literal (Loc, Dim))))), |
| 3710 |
Statements => Free_One_Dimension (Dim + 1))); |
| 3711 |
end if; |
| 3712 |
end Free_One_Dimension; |
| 3713 |
|
| 3714 |
-- Start of processing for Cleanup_Array |
| 3715 |
|
| 3716 |
begin |
| 3717 |
return Free_One_Dimension (1); |
| 3718 |
end Cleanup_Array; |
| 3719 |
|
| 3720 |
-------------------- |
| 3721 |
-- Cleanup_Record -- |
| 3722 |
-------------------- |
| 3723 |
|
| 3724 |
function Cleanup_Record |
| 3725 |
(N : Node_Id; |
| 3726 |
Obj : Node_Id; |
| 3727 |
Typ : Entity_Id) return List_Id |
| 3728 |
is |
| 3729 |
Loc : constant Source_Ptr := Sloc (N); |
| 3730 |
Stmts : constant List_Id := New_List; |
| 3731 |
U_Typ : constant Entity_Id := Underlying_Type (Typ); |
| 3732 |
|
| 3733 |
Comp : Entity_Id; |
| 3734 |
Tsk : Node_Id; |
| 3735 |
|
| 3736 |
begin |
| 3737 |
if Has_Discriminants (U_Typ) |
| 3738 |
and then Nkind (Parent (U_Typ)) = N_Full_Type_Declaration |
| 3739 |
and then Nkind (Type_Definition (Parent (U_Typ))) = N_Record_Definition |
| 3740 |
and then |
| 3741 |
Present |
| 3742 |
(Variant_Part (Component_List (Type_Definition (Parent (U_Typ))))) |
| 3743 |
then |
| 3744 |
-- For now, do not attempt to free a component that may appear in a |
| 3745 |
-- variant, and instead issue a warning. Doing this "properly" would |
| 3746 |
-- require building a case statement and would be quite a mess. Note |
| 3747 |
-- that the RM only requires that free "work" for the case of a task |
| 3748 |
-- access value, so already we go way beyond this in that we deal |
| 3749 |
-- with the array case and non-discriminated record cases. |
| 3750 |
|
| 3751 |
Error_Msg_N |
| 3752 |
("task/protected object in variant record will not be freed??", N); |
| 3753 |
return New_List (Make_Null_Statement (Loc)); |
| 3754 |
end if; |
| 3755 |
|
| 3756 |
Comp := First_Component (U_Typ); |
| 3757 |
while Present (Comp) loop |
| 3758 |
if Chars (Comp) /= Name_uParent |
| 3759 |
and then (Has_Task (Etype (Comp)) |
| 3760 |
or else Has_Simple_Protected_Object (Etype (Comp))) |
| 3761 |
then |
| 3762 |
Tsk := |
| 3763 |
Make_Selected_Component (Loc, |
| 3764 |
Prefix => Duplicate_Subexpr_No_Checks (Obj), |
| 3765 |
Selector_Name => New_Occurrence_Of (Comp, Loc)); |
| 3766 |
Set_Etype (Tsk, Etype (Comp)); |
| 3767 |
|
| 3768 |
if Is_Task_Type (Etype (Comp)) then |
| 3769 |
Append_To (Stmts, Cleanup_Task (N, Tsk)); |
| 3770 |
|
| 3771 |
elsif Is_Simple_Protected_Type (Etype (Comp)) then |
| 3772 |
Append_To (Stmts, Cleanup_Protected_Object (N, Tsk)); |
| 3773 |
|
| 3774 |
elsif Is_Record_Type (Etype (Comp)) then |
| 3775 |
|
| 3776 |
-- Recurse, by generating the prefix of the argument to the |
| 3777 |
-- eventual cleanup call. |
| 3778 |
|
| 3779 |
Append_List_To (Stmts, Cleanup_Record (N, Tsk, Etype (Comp))); |
| 3780 |
|
| 3781 |
elsif Is_Array_Type (Etype (Comp)) then |
| 3782 |
Append_List_To (Stmts, Cleanup_Array (N, Tsk, Etype (Comp))); |
| 3783 |
end if; |
| 3784 |
end if; |
| 3785 |
|
| 3786 |
Next_Component (Comp); |
| 3787 |
end loop; |
| 3788 |
|
| 3789 |
return Stmts; |
| 3790 |
end Cleanup_Record; |
| 3791 |
|
| 3792 |
------------------------------ |
| 3793 |
-- Cleanup_Protected_Object -- |
| 3794 |
------------------------------ |
| 3795 |
|
| 3796 |
function Cleanup_Protected_Object |
| 3797 |
(N : Node_Id; |
| 3798 |
Ref : Node_Id) return Node_Id |
| 3799 |
is |
| 3800 |
Loc : constant Source_Ptr := Sloc (N); |
| 3801 |
|
| 3802 |
begin |
| 3803 |
-- For restricted run-time libraries (Ravenscar), tasks are |
| 3804 |
-- non-terminating, and protected objects can only appear at library |
| 3805 |
-- level, so we do not want finalization of protected objects. |
| 3806 |
|
| 3807 |
if Restricted_Profile then |
| 3808 |
return Empty; |
| 3809 |
|
| 3810 |
else |
| 3811 |
return |
| 3812 |
Make_Procedure_Call_Statement (Loc, |
| 3813 |
Name => |
| 3814 |
New_Occurrence_Of (RTE (RE_Finalize_Protection), Loc), |
| 3815 |
Parameter_Associations => New_List (Concurrent_Ref (Ref))); |
| 3816 |
end if; |
| 3817 |
end Cleanup_Protected_Object; |
| 3818 |
|
| 3819 |
------------------ |
| 3820 |
-- Cleanup_Task -- |
| 3821 |
------------------ |
| 3822 |
|
| 3823 |
function Cleanup_Task |
| 3824 |
(N : Node_Id; |
| 3825 |
Ref : Node_Id) return Node_Id |
| 3826 |
is |
| 3827 |
Loc : constant Source_Ptr := Sloc (N); |
| 3828 |
|
| 3829 |
begin |
| 3830 |
-- For restricted run-time libraries (Ravenscar), tasks are |
| 3831 |
-- non-terminating and they can only appear at library level, |
| 3832 |
-- so we do not want finalization of task objects. |
| 3833 |
|
| 3834 |
if Restricted_Profile then |
| 3835 |
return Empty; |
| 3836 |
|
| 3837 |
else |
| 3838 |
return |
| 3839 |
Make_Procedure_Call_Statement (Loc, |
| 3840 |
Name => |
| 3841 |
New_Occurrence_Of (RTE (RE_Free_Task), Loc), |
| 3842 |
Parameter_Associations => New_List (Concurrent_Ref (Ref))); |
| 3843 |
end if; |
| 3844 |
end Cleanup_Task; |
| 3845 |
|
| 3846 |
-------------------------------------- |
| 3847 |
-- Check_Unnesting_Elaboration_Code -- |
| 3848 |
-------------------------------------- |
| 3849 |
|
| 3850 |
procedure Check_Unnesting_Elaboration_Code (N : Node_Id) is |
| 3851 |
Loc : constant Source_Ptr := Sloc (N); |
| 3852 |
Block_Elab_Proc : Entity_Id := Empty; |
| 3853 |
|
| 3854 |
procedure Set_Block_Elab_Proc; |
| 3855 |
-- Create a defining identifier for a procedure that will replace |
| 3856 |
-- a block with nested subprograms (unless it has already been created, |
| 3857 |
-- in which case this is a no-op). |
| 3858 |
|
| 3859 |
procedure Set_Block_Elab_Proc is |
| 3860 |
begin |
| 3861 |
if No (Block_Elab_Proc) then |
| 3862 |
Block_Elab_Proc := Make_Temporary (Loc, 'I'); |
| 3863 |
end if; |
| 3864 |
end Set_Block_Elab_Proc; |
| 3865 |
|
| 3866 |
procedure Reset_Scopes_To_Block_Elab_Proc (L : List_Id); |
| 3867 |
-- Find entities in the elaboration code of a library package body that |
| 3868 |
-- contain or represent a subprogram body. A body can appear within a |
| 3869 |
-- block or a loop or can appear by itself if generated for an object |
| 3870 |
-- declaration that involves controlled actions. The first such entity |
| 3871 |
-- forces creation of a new procedure entity (via Set_Block_Elab_Proc) |
| 3872 |
-- that will be used to reset the scopes of all entities that become |
| 3873 |
-- local to the new elaboration procedure. This is needed for subsequent |
| 3874 |
-- unnesting actions, which depend on proper setting of the Scope links |
| 3875 |
-- to determine the nesting level of each subprogram. |
| 3876 |
|
| 3877 |
-------------------------------------- |
| 3878 |
-- Reset_Scopes_To_Block_Elab_Proc -- |
| 3879 |
-------------------------------------- |
| 3880 |
Maybe_Reset_Scopes_For_Decl : constant Elist_Id := New_Elmt_List; |
| 3881 |
|
| 3882 |
procedure Reset_Scopes_To_Block_Elab_Proc (L : List_Id) is |
| 3883 |
Id : Entity_Id; |
| 3884 |
Stat : Node_Id; |
| 3885 |
Node : Node_Id; |
| 3886 |
|
| 3887 |
begin |
| 3888 |
Stat := First (L); |
| 3889 |
while Present (Stat) loop |
| 3890 |
case Nkind (Stat) is |
| 3891 |
when N_Block_Statement => |
| 3892 |
if Present (Identifier (Stat)) then |
| 3893 |
Id := Entity (Identifier (Stat)); |
| 3894 |
|
| 3895 |
-- The Scope of this block needs to be reset to the new |
| 3896 |
-- procedure if the block contains nested subprograms. |
| 3897 |
|
| 3898 |
if Present (Id) and then Contains_Subprogram (Id) then |
| 3899 |
Set_Block_Elab_Proc; |
| 3900 |
Set_Scope (Id, Block_Elab_Proc); |
| 3901 |
end if; |
| 3902 |
end if; |
| 3903 |
|
| 3904 |
when N_Loop_Statement => |
| 3905 |
Id := Entity (Identifier (Stat)); |
| 3906 |
|
| 3907 |
if Present (Id) and then Contains_Subprogram (Id) then |
| 3908 |
if Scope (Id) = Current_Scope then |
| 3909 |
Set_Block_Elab_Proc; |
| 3910 |
Set_Scope (Id, Block_Elab_Proc); |
| 3911 |
end if; |
| 3912 |
end if; |
| 3913 |
|
| 3914 |
-- We traverse the loop's statements as well, which may |
| 3915 |
-- include other block (etc.) statements that need to have |
| 3916 |
-- their Scope set to Block_Elab_Proc. (Is this really the |
| 3917 |
-- case, or do such nested blocks refer to the loop scope |
| 3918 |
-- rather than the loop's enclosing scope???.) |
| 3919 |
|
| 3920 |
Reset_Scopes_To_Block_Elab_Proc (Statements (Stat)); |
| 3921 |
|
| 3922 |
when N_If_Statement => |
| 3923 |
Reset_Scopes_To_Block_Elab_Proc (Then_Statements (Stat)); |
| 3924 |
Reset_Scopes_To_Block_Elab_Proc (Else_Statements (Stat)); |
| 3925 |
|
| 3926 |
Node := First (Elsif_Parts (Stat)); |
| 3927 |
while Present (Node) loop |
| 3928 |
Reset_Scopes_To_Block_Elab_Proc (Then_Statements (Node)); |
| 3929 |
Next (Node); |
| 3930 |
end loop; |
| 3931 |
|
| 3932 |
when N_Case_Statement => |
| 3933 |
Node := First (Alternatives (Stat)); |
| 3934 |
while Present (Node) loop |
| 3935 |
Reset_Scopes_To_Block_Elab_Proc (Statements (Node)); |
| 3936 |
Next (Node); |
| 3937 |
end loop; |
| 3938 |
|
| 3939 |
-- Reset the Scope of a subprogram and object declaration |
| 3940 |
-- occurring at the top level |
| 3941 |
|
| 3942 |
when N_Subprogram_Body => |
| 3943 |
Id := Defining_Entity (Stat); |
| 3944 |
|
| 3945 |
Set_Block_Elab_Proc; |
| 3946 |
Set_Scope (Id, Block_Elab_Proc); |
| 3947 |
|
| 3948 |
when N_Object_Declaration |
| 3949 |
| N_Object_Renaming_Declaration |
| 3950 |
=> |
| 3951 |
Id := Defining_Entity (Stat); |
| 3952 |
if No (Block_Elab_Proc) then |
| 3953 |
Append_Elmt (Id, Maybe_Reset_Scopes_For_Decl); |
| 3954 |
else |
| 3955 |
Set_Scope (Id, Block_Elab_Proc); |
| 3956 |
end if; |
| 3957 |
|
| 3958 |
when others => |
| 3959 |
null; |
| 3960 |
end case; |
| 3961 |
|
| 3962 |
Next (Stat); |
| 3963 |
end loop; |
| 3964 |
|
| 3965 |
-- If we are creating an Elab procedure, move all the gathered |
| 3966 |
-- declarations in its scope. |
| 3967 |
|
| 3968 |
if Present (Block_Elab_Proc) then |
| 3969 |
while not Is_Empty_Elmt_List (Maybe_Reset_Scopes_For_Decl) loop |
| 3970 |
Set_Scope |
| 3971 |
(Elists.Node |
| 3972 |
(Last_Elmt (Maybe_Reset_Scopes_For_Decl)), Block_Elab_Proc); |
| 3973 |
Remove_Last_Elmt (Maybe_Reset_Scopes_For_Decl); |
| 3974 |
end loop; |
| 3975 |
end if; |
| 3976 |
end Reset_Scopes_To_Block_Elab_Proc; |
| 3977 |
|
| 3978 |
-- Local variables |
| 3979 |
|
| 3980 |
H_Seq : constant Node_Id := Handled_Statement_Sequence (N); |
| 3981 |
Elab_Body : Node_Id; |
| 3982 |
Elab_Call : Node_Id; |
| 3983 |
|
| 3984 |
-- Start of processing for Check_Unnesting_Elaboration_Code |
| 3985 |
|
| 3986 |
begin |
| 3987 |
if Present (H_Seq) then |
| 3988 |
Reset_Scopes_To_Block_Elab_Proc (Statements (H_Seq)); |
| 3989 |
|
| 3990 |
-- There may be subprograms declared in the exception handlers |
| 3991 |
-- of the current body. |
| 3992 |
|
| 3993 |
if Present (Exception_Handlers (H_Seq)) then |
| 3994 |
declare |
| 3995 |
Handler : Node_Id := First (Exception_Handlers (H_Seq)); |
| 3996 |
begin |
| 3997 |
while Present (Handler) loop |
| 3998 |
Reset_Scopes_To_Block_Elab_Proc (Statements (Handler)); |
| 3999 |
|
| 4000 |
Next (Handler); |
| 4001 |
end loop; |
| 4002 |
end; |
| 4003 |
end if; |
| 4004 |
|
| 4005 |
if Present (Block_Elab_Proc) then |
| 4006 |
Elab_Body := |
| 4007 |
Make_Subprogram_Body (Loc, |
| 4008 |
Specification => |
| 4009 |
Make_Procedure_Specification (Loc, |
| 4010 |
Defining_Unit_Name => Block_Elab_Proc), |
| 4011 |
Declarations => New_List, |
| 4012 |
Handled_Statement_Sequence => |
| 4013 |
Relocate_Node (Handled_Statement_Sequence (N))); |
| 4014 |
|
| 4015 |
Elab_Call := |
| 4016 |
Make_Procedure_Call_Statement (Loc, |
| 4017 |
Name => New_Occurrence_Of (Block_Elab_Proc, Loc)); |
| 4018 |
|
| 4019 |
Append_To (Declarations (N), Elab_Body); |
| 4020 |
Analyze (Elab_Body); |
| 4021 |
Set_Has_Nested_Subprogram (Block_Elab_Proc); |
| 4022 |
|
| 4023 |
Set_Handled_Statement_Sequence (N, |
| 4024 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 4025 |
Statements => New_List (Elab_Call))); |
| 4026 |
|
| 4027 |
Analyze (Elab_Call); |
| 4028 |
|
| 4029 |
-- Could we reset the scopes of entities associated with the new |
| 4030 |
-- procedure here via a loop over entities rather than doing it in |
| 4031 |
-- the recursive Reset_Scopes_To_Elab_Proc procedure??? |
| 4032 |
end if; |
| 4033 |
end if; |
| 4034 |
end Check_Unnesting_Elaboration_Code; |
| 4035 |
|
| 4036 |
--------------------------------------- |
| 4037 |
-- Check_Unnesting_In_Decls_Or_Stmts -- |
| 4038 |
--------------------------------------- |
| 4039 |
|
| 4040 |
procedure Check_Unnesting_In_Decls_Or_Stmts (Decls_Or_Stmts : List_Id) is |
| 4041 |
Decl_Or_Stmt : Node_Id; |
| 4042 |
|
| 4043 |
begin |
| 4044 |
if Unnest_Subprogram_Mode |
| 4045 |
and then Present (Decls_Or_Stmts) |
| 4046 |
then |
| 4047 |
Decl_Or_Stmt := First (Decls_Or_Stmts); |
| 4048 |
while Present (Decl_Or_Stmt) loop |
| 4049 |
if Nkind (Decl_Or_Stmt) = N_Block_Statement |
| 4050 |
and then Contains_Subprogram (Entity (Identifier (Decl_Or_Stmt))) |
| 4051 |
then |
| 4052 |
Unnest_Block (Decl_Or_Stmt); |
| 4053 |
|
| 4054 |
-- If-statements may contain subprogram bodies at the outer level |
| 4055 |
-- of their statement lists, and the subprograms may make up-level |
| 4056 |
-- references (such as to objects declared in the same statement |
| 4057 |
-- list). Unlike block and loop cases, however, we don't have an |
| 4058 |
-- entity on which to test the Contains_Subprogram flag, so |
| 4059 |
-- Unnest_If_Statement must traverse the statement lists to |
| 4060 |
-- determine whether there are nested subprograms present. |
| 4061 |
|
| 4062 |
elsif Nkind (Decl_Or_Stmt) = N_If_Statement then |
| 4063 |
Unnest_If_Statement (Decl_Or_Stmt); |
| 4064 |
|
| 4065 |
elsif Nkind (Decl_Or_Stmt) = N_Loop_Statement then |
| 4066 |
declare |
| 4067 |
Id : constant Entity_Id := |
| 4068 |
Entity (Identifier (Decl_Or_Stmt)); |
| 4069 |
|
| 4070 |
begin |
| 4071 |
-- When a top-level loop within declarations of a library |
| 4072 |
-- package spec or body contains nested subprograms, we wrap |
| 4073 |
-- it in a procedure to handle possible up-level references |
| 4074 |
-- to entities associated with the loop (such as loop |
| 4075 |
-- parameters). |
| 4076 |
|
| 4077 |
if Present (Id) and then Contains_Subprogram (Id) then |
| 4078 |
Unnest_Loop (Decl_Or_Stmt); |
| 4079 |
end if; |
| 4080 |
end; |
| 4081 |
|
| 4082 |
elsif Nkind (Decl_Or_Stmt) = N_Package_Declaration then |
| 4083 |
Check_Unnesting_In_Decls_Or_Stmts |
| 4084 |
(Visible_Declarations (Specification (Decl_Or_Stmt))); |
| 4085 |
Check_Unnesting_In_Decls_Or_Stmts |
| 4086 |
(Private_Declarations (Specification (Decl_Or_Stmt))); |
| 4087 |
|
| 4088 |
elsif Nkind (Decl_Or_Stmt) = N_Package_Body then |
| 4089 |
Check_Unnesting_In_Decls_Or_Stmts (Declarations (Decl_Or_Stmt)); |
| 4090 |
if Present (Statements |
| 4091 |
(Handled_Statement_Sequence (Decl_Or_Stmt))) |
| 4092 |
then |
| 4093 |
Check_Unnesting_In_Decls_Or_Stmts (Statements |
| 4094 |
(Handled_Statement_Sequence (Decl_Or_Stmt))); |
| 4095 |
Check_Unnesting_In_Handlers (Decl_Or_Stmt); |
| 4096 |
end if; |
| 4097 |
end if; |
| 4098 |
|
| 4099 |
Next (Decl_Or_Stmt); |
| 4100 |
end loop; |
| 4101 |
end if; |
| 4102 |
end Check_Unnesting_In_Decls_Or_Stmts; |
| 4103 |
|
| 4104 |
--------------------------------- |
| 4105 |
-- Check_Unnesting_In_Handlers -- |
| 4106 |
--------------------------------- |
| 4107 |
|
| 4108 |
procedure Check_Unnesting_In_Handlers (N : Node_Id) is |
| 4109 |
Stmt_Seq : constant Node_Id := Handled_Statement_Sequence (N); |
| 4110 |
|
| 4111 |
begin |
| 4112 |
if Present (Stmt_Seq) |
| 4113 |
and then Present (Exception_Handlers (Stmt_Seq)) |
| 4114 |
then |
| 4115 |
declare |
| 4116 |
Handler : Node_Id := First (Exception_Handlers (Stmt_Seq)); |
| 4117 |
begin |
| 4118 |
while Present (Handler) loop |
| 4119 |
if Present (Statements (Handler)) then |
| 4120 |
Check_Unnesting_In_Decls_Or_Stmts (Statements (Handler)); |
| 4121 |
end if; |
| 4122 |
|
| 4123 |
Next (Handler); |
| 4124 |
end loop; |
| 4125 |
end; |
| 4126 |
end if; |
| 4127 |
end Check_Unnesting_In_Handlers; |
| 4128 |
|
| 4129 |
------------------------------ |
| 4130 |
-- Check_Visibly_Controlled -- |
| 4131 |
------------------------------ |
| 4132 |
|
| 4133 |
procedure Check_Visibly_Controlled |
| 4134 |
(Prim : Final_Primitives; |
| 4135 |
Typ : Entity_Id; |
| 4136 |
E : in out Entity_Id; |
| 4137 |
Cref : in out Node_Id) |
| 4138 |
is |
| 4139 |
Parent_Type : Entity_Id; |
| 4140 |
Op : Entity_Id; |
| 4141 |
|
| 4142 |
begin |
| 4143 |
if Is_Derived_Type (Typ) |
| 4144 |
and then Comes_From_Source (E) |
| 4145 |
and then No (Overridden_Operation (E)) |
| 4146 |
then |
| 4147 |
-- We know that the explicit operation on the type does not override |
| 4148 |
-- the inherited operation of the parent, and that the derivation |
| 4149 |
-- is from a private type that is not visibly controlled. |
| 4150 |
|
| 4151 |
Parent_Type := Etype (Typ); |
| 4152 |
Op := Find_Controlled_Prim_Op (Parent_Type, Name_Of (Prim)); |
| 4153 |
|
| 4154 |
if Present (Op) then |
| 4155 |
E := Op; |
| 4156 |
|
| 4157 |
-- Wrap the object to be initialized into the proper |
| 4158 |
-- unchecked conversion, to be compatible with the operation |
| 4159 |
-- to be called. |
| 4160 |
|
| 4161 |
if Nkind (Cref) = N_Unchecked_Type_Conversion then |
| 4162 |
Cref := Unchecked_Convert_To (Parent_Type, Expression (Cref)); |
| 4163 |
else |
| 4164 |
Cref := Unchecked_Convert_To (Parent_Type, Cref); |
| 4165 |
end if; |
| 4166 |
end if; |
| 4167 |
end if; |
| 4168 |
end Check_Visibly_Controlled; |
| 4169 |
|
| 4170 |
-------------------------- |
| 4171 |
-- Contains_Subprogram -- |
| 4172 |
-------------------------- |
| 4173 |
|
| 4174 |
function Contains_Subprogram (Blk : Entity_Id) return Boolean is |
| 4175 |
E : Entity_Id; |
| 4176 |
|
| 4177 |
begin |
| 4178 |
E := First_Entity (Blk); |
| 4179 |
|
| 4180 |
-- The compiler may generate loops with a declare block containing |
| 4181 |
-- nested procedures used for finalization. Recursively search for |
| 4182 |
-- subprograms in such constructs. |
| 4183 |
|
| 4184 |
if Ekind (Blk) = E_Loop |
| 4185 |
and then Parent_Kind (Blk) = N_Loop_Statement |
| 4186 |
then |
| 4187 |
declare |
| 4188 |
Stmt : Node_Id := First (Statements (Parent (Blk))); |
| 4189 |
begin |
| 4190 |
while Present (Stmt) loop |
| 4191 |
if Nkind (Stmt) = N_Block_Statement then |
| 4192 |
declare |
| 4193 |
Id : constant Entity_Id := |
| 4194 |
Entity (Identifier (Stmt)); |
| 4195 |
begin |
| 4196 |
if Contains_Subprogram (Id) then |
| 4197 |
return True; |
| 4198 |
end if; |
| 4199 |
end; |
| 4200 |
end if; |
| 4201 |
Next (Stmt); |
| 4202 |
end loop; |
| 4203 |
end; |
| 4204 |
end if; |
| 4205 |
|
| 4206 |
while Present (E) loop |
| 4207 |
if Is_Subprogram (E) then |
| 4208 |
return True; |
| 4209 |
|
| 4210 |
elsif Ekind (E) in E_Block | E_Loop |
| 4211 |
and then Contains_Subprogram (E) |
| 4212 |
then |
| 4213 |
return True; |
| 4214 |
end if; |
| 4215 |
|
| 4216 |
Next_Entity (E); |
| 4217 |
end loop; |
| 4218 |
|
| 4219 |
return False; |
| 4220 |
end Contains_Subprogram; |
| 4221 |
|
| 4222 |
------------------ |
| 4223 |
-- Convert_View -- |
| 4224 |
------------------ |
| 4225 |
|
| 4226 |
function Convert_View |
| 4227 |
(Proc : Entity_Id; |
| 4228 |
Arg : Node_Id; |
| 4229 |
Typ : Entity_Id) return Node_Id |
| 4230 |
is |
| 4231 |
Ftyp : constant Entity_Id := Etype (First_Formal (Proc)); |
| 4232 |
|
| 4233 |
Atyp : Entity_Id; |
| 4234 |
|
| 4235 |
begin |
| 4236 |
if Nkind (Arg) in N_Type_Conversion | N_Unchecked_Type_Conversion then |
| 4237 |
Atyp := Entity (Subtype_Mark (Arg)); |
| 4238 |
elsif Present (Etype (Arg)) then |
| 4239 |
Atyp := Etype (Arg); |
| 4240 |
else |
| 4241 |
Atyp := Typ; |
| 4242 |
end if; |
| 4243 |
|
| 4244 |
if Is_Abstract_Subprogram (Proc) and then Is_Tagged_Type (Ftyp) then |
| 4245 |
return Unchecked_Convert_To (Class_Wide_Type (Ftyp), Arg); |
| 4246 |
|
| 4247 |
elsif Present (Atyp) |
| 4248 |
and then Atyp /= Ftyp |
| 4249 |
and then (Is_Private_Type (Ftyp) |
| 4250 |
or else Is_Private_Type (Atyp) |
| 4251 |
or else Is_Private_Type (Base_Type (Atyp))) |
| 4252 |
and then Implementation_Base_Type (Atyp) = |
| 4253 |
Implementation_Base_Type (Ftyp) |
| 4254 |
then |
| 4255 |
return Unchecked_Convert_To (Ftyp, Arg); |
| 4256 |
|
| 4257 |
-- If the argument is already a conversion, as generated by |
| 4258 |
-- Make_Init_Call, set the target type to the type of the formal |
| 4259 |
-- directly, to avoid spurious typing problems. |
| 4260 |
|
| 4261 |
elsif Nkind (Arg) in N_Unchecked_Type_Conversion | N_Type_Conversion |
| 4262 |
and then not Is_Class_Wide_Type (Atyp) |
| 4263 |
then |
| 4264 |
Set_Subtype_Mark (Arg, New_Occurrence_Of (Ftyp, Sloc (Arg))); |
| 4265 |
Set_Etype (Arg, Ftyp); |
| 4266 |
return Arg; |
| 4267 |
|
| 4268 |
-- Otherwise, introduce a conversion when the designated object |
| 4269 |
-- has a type derived from the formal of the controlled routine. |
| 4270 |
|
| 4271 |
elsif Is_Private_Type (Ftyp) |
| 4272 |
and then Present (Atyp) |
| 4273 |
and then Is_Derived_Type (Underlying_Type (Base_Type (Atyp))) |
| 4274 |
then |
| 4275 |
return Unchecked_Convert_To (Ftyp, Arg); |
| 4276 |
|
| 4277 |
else |
| 4278 |
return Arg; |
| 4279 |
end if; |
| 4280 |
end Convert_View; |
| 4281 |
|
| 4282 |
------------------------------- |
| 4283 |
-- Establish_Transient_Scope -- |
| 4284 |
------------------------------- |
| 4285 |
|
| 4286 |
-- This procedure is called each time a transient block has to be inserted |
| 4287 |
-- that is to say for each call to a function with unconstrained or tagged |
| 4288 |
-- result. It creates a new scope on the scope stack in order to enclose |
| 4289 |
-- all transient variables generated. |
| 4290 |
|
| 4291 |
procedure Establish_Transient_Scope |
| 4292 |
(N : Node_Id; |
| 4293 |
Manage_Sec_Stack : Boolean) |
| 4294 |
is |
| 4295 |
function Is_Package_Or_Subprogram (Id : Entity_Id) return Boolean; |
| 4296 |
-- Determine whether arbitrary Id denotes a package or subprogram [body] |
| 4297 |
|
| 4298 |
function Find_Enclosing_Transient_Scope return Int; |
| 4299 |
-- Examine the scope stack looking for the nearest enclosing transient |
| 4300 |
-- scope within the innermost enclosing package or subprogram. Return |
| 4301 |
-- its index in the table or else -1 if no such scope exists. |
| 4302 |
|
| 4303 |
function Find_Transient_Context (N : Node_Id) return Node_Id; |
| 4304 |
-- Locate a suitable context for arbitrary node N which may need to be |
| 4305 |
-- serviced by a transient scope. Return Empty if no suitable context |
| 4306 |
-- is available. |
| 4307 |
|
| 4308 |
procedure Delegate_Sec_Stack_Management; |
| 4309 |
-- Move the management of the secondary stack to the nearest enclosing |
| 4310 |
-- suitable scope. |
| 4311 |
|
| 4312 |
procedure Create_Transient_Scope (Context : Node_Id); |
| 4313 |
-- Place a new scope on the scope stack in order to service construct |
| 4314 |
-- Context. Context is the node found by Find_Transient_Context. The |
| 4315 |
-- new scope may also manage the secondary stack. |
| 4316 |
|
| 4317 |
---------------------------- |
| 4318 |
-- Create_Transient_Scope -- |
| 4319 |
---------------------------- |
| 4320 |
|
| 4321 |
procedure Create_Transient_Scope (Context : Node_Id) is |
| 4322 |
Loc : constant Source_Ptr := Sloc (N); |
| 4323 |
|
| 4324 |
Iter_Loop : Entity_Id; |
| 4325 |
Trans_Scop : constant Entity_Id := |
| 4326 |
New_Internal_Entity (E_Block, Current_Scope, Loc, 'B'); |
| 4327 |
|
| 4328 |
begin |
| 4329 |
Set_Etype (Trans_Scop, Standard_Void_Type); |
| 4330 |
|
| 4331 |
-- Push a new scope, and set its Node_To_Be_Wrapped and Is_Transient |
| 4332 |
-- fields. |
| 4333 |
|
| 4334 |
Push_Scope (Trans_Scop); |
| 4335 |
Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped := Context; |
| 4336 |
Scope_Stack.Table (Scope_Stack.Last).Is_Transient := True; |
| 4337 |
|
| 4338 |
-- The transient scope must also manage the secondary stack |
| 4339 |
|
| 4340 |
if Manage_Sec_Stack then |
| 4341 |
Set_Uses_Sec_Stack (Trans_Scop); |
| 4342 |
Check_Restriction (No_Secondary_Stack, N); |
| 4343 |
|
| 4344 |
-- The expansion of iterator loops generates references to objects |
| 4345 |
-- in order to extract elements from a container: |
| 4346 |
|
| 4347 |
-- Ref : Reference_Type_Ptr := Reference (Container, Cursor); |
| 4348 |
-- Obj : <object type> renames Ref.all.Element.all; |
| 4349 |
|
| 4350 |
-- These references are controlled and returned on the secondary |
| 4351 |
-- stack. A new reference is created at each iteration of the loop |
| 4352 |
-- and as a result it must be finalized and the space occupied by |
| 4353 |
-- it on the secondary stack reclaimed at the end of the current |
| 4354 |
-- iteration. |
| 4355 |
|
| 4356 |
-- When the context that requires a transient scope is a call to |
| 4357 |
-- routine Reference, the node to be wrapped is the source object: |
| 4358 |
|
| 4359 |
-- for Obj of Container loop |
| 4360 |
|
| 4361 |
-- Routine Wrap_Transient_Declaration however does not generate |
| 4362 |
-- a physical block as wrapping a declaration will kill it too |
| 4363 |
-- early. To handle this peculiar case, mark the related iterator |
| 4364 |
-- loop as requiring the secondary stack. This signals the |
| 4365 |
-- finalization machinery to manage the secondary stack (see |
| 4366 |
-- routine Process_Statements_For_Controlled_Objects). |
| 4367 |
|
| 4368 |
Iter_Loop := Find_Enclosing_Iterator_Loop (Trans_Scop); |
| 4369 |
|
| 4370 |
if Present (Iter_Loop) then |
| 4371 |
Set_Uses_Sec_Stack (Iter_Loop); |
| 4372 |
end if; |
| 4373 |
end if; |
| 4374 |
|
| 4375 |
if Debug_Flag_W then |
| 4376 |
Write_Str (" <Transient>"); |
| 4377 |
Write_Eol; |
| 4378 |
end if; |
| 4379 |
end Create_Transient_Scope; |
| 4380 |
|
| 4381 |
----------------------------------- |
| 4382 |
-- Delegate_Sec_Stack_Management -- |
| 4383 |
----------------------------------- |
| 4384 |
|
| 4385 |
procedure Delegate_Sec_Stack_Management is |
| 4386 |
begin |
| 4387 |
for Index in reverse Scope_Stack.First .. Scope_Stack.Last loop |
| 4388 |
declare |
| 4389 |
Scope : Scope_Stack_Entry renames Scope_Stack.Table (Index); |
| 4390 |
begin |
| 4391 |
-- Prevent the search from going too far or within the scope |
| 4392 |
-- space of another unit. |
| 4393 |
|
| 4394 |
if Scope.Entity = Standard_Standard then |
| 4395 |
return; |
| 4396 |
|
| 4397 |
-- No transient scope should be encountered during the |
| 4398 |
-- traversal because Establish_Transient_Scope should have |
| 4399 |
-- already handled this case. |
| 4400 |
|
| 4401 |
elsif Scope.Is_Transient then |
| 4402 |
raise Program_Error; |
| 4403 |
|
| 4404 |
-- The construct that requires secondary stack management is |
| 4405 |
-- always enclosed by a package or subprogram scope. |
| 4406 |
|
| 4407 |
elsif Is_Package_Or_Subprogram (Scope.Entity) then |
| 4408 |
Set_Uses_Sec_Stack (Scope.Entity); |
| 4409 |
Check_Restriction (No_Secondary_Stack, N); |
| 4410 |
|
| 4411 |
return; |
| 4412 |
end if; |
| 4413 |
end; |
| 4414 |
end loop; |
| 4415 |
|
| 4416 |
-- At this point no suitable scope was found. This should never occur |
| 4417 |
-- because a construct is always enclosed by a compilation unit which |
| 4418 |
-- has a scope. |
| 4419 |
|
| 4420 |
pragma Assert (False); |
| 4421 |
end Delegate_Sec_Stack_Management; |
| 4422 |
|
| 4423 |
------------------------------------ |
| 4424 |
-- Find_Enclosing_Transient_Scope -- |
| 4425 |
------------------------------------ |
| 4426 |
|
| 4427 |
function Find_Enclosing_Transient_Scope return Int is |
| 4428 |
begin |
| 4429 |
for Index in reverse Scope_Stack.First .. Scope_Stack.Last loop |
| 4430 |
declare |
| 4431 |
Scope : Scope_Stack_Entry renames Scope_Stack.Table (Index); |
| 4432 |
begin |
| 4433 |
-- Prevent the search from going too far or within the scope |
| 4434 |
-- space of another unit. |
| 4435 |
|
| 4436 |
if Scope.Entity = Standard_Standard |
| 4437 |
or else Is_Package_Or_Subprogram (Scope.Entity) |
| 4438 |
then |
| 4439 |
exit; |
| 4440 |
|
| 4441 |
elsif Scope.Is_Transient then |
| 4442 |
return Index; |
| 4443 |
end if; |
| 4444 |
end; |
| 4445 |
end loop; |
| 4446 |
|
| 4447 |
return -1; |
| 4448 |
end Find_Enclosing_Transient_Scope; |
| 4449 |
|
| 4450 |
---------------------------- |
| 4451 |
-- Find_Transient_Context -- |
| 4452 |
---------------------------- |
| 4453 |
|
| 4454 |
function Find_Transient_Context (N : Node_Id) return Node_Id is |
| 4455 |
Curr : Node_Id := N; |
| 4456 |
Prev : Node_Id := Empty; |
| 4457 |
|
| 4458 |
begin |
| 4459 |
while Present (Curr) loop |
| 4460 |
case Nkind (Curr) is |
| 4461 |
|
| 4462 |
-- Declarations |
| 4463 |
|
| 4464 |
-- Declarations act as a boundary for a transient scope even if |
| 4465 |
-- they are not wrapped, see Wrap_Transient_Declaration. |
| 4466 |
|
| 4467 |
when N_Object_Declaration |
| 4468 |
| N_Object_Renaming_Declaration |
| 4469 |
| N_Subtype_Declaration |
| 4470 |
=> |
| 4471 |
return Curr; |
| 4472 |
|
| 4473 |
-- Statements |
| 4474 |
|
| 4475 |
-- Statements and statement-like constructs act as a boundary |
| 4476 |
-- for a transient scope. |
| 4477 |
|
| 4478 |
when N_Accept_Alternative |
| 4479 |
| N_Attribute_Definition_Clause |
| 4480 |
| N_Case_Statement |
| 4481 |
| N_Case_Statement_Alternative |
| 4482 |
| N_Code_Statement |
| 4483 |
| N_Delay_Alternative |
| 4484 |
| N_Delay_Until_Statement |
| 4485 |
| N_Delay_Relative_Statement |
| 4486 |
| N_Discriminant_Association |
| 4487 |
| N_Elsif_Part |
| 4488 |
| N_Entry_Body_Formal_Part |
| 4489 |
| N_Exit_Statement |
| 4490 |
| N_If_Statement |
| 4491 |
| N_Iteration_Scheme |
| 4492 |
| N_Terminate_Alternative |
| 4493 |
=> |
| 4494 |
pragma Assert (Present (Prev)); |
| 4495 |
return Prev; |
| 4496 |
|
| 4497 |
when N_Assignment_Statement => |
| 4498 |
return Curr; |
| 4499 |
|
| 4500 |
when N_Entry_Call_Statement |
| 4501 |
| N_Procedure_Call_Statement |
| 4502 |
=> |
| 4503 |
-- When an entry or procedure call acts as the alternative |
| 4504 |
-- of a conditional or timed entry call, the proper context |
| 4505 |
-- is that of the alternative. |
| 4506 |
|
| 4507 |
if Nkind (Parent (Curr)) = N_Entry_Call_Alternative |
| 4508 |
and then Nkind (Parent (Parent (Curr))) in |
| 4509 |
N_Conditional_Entry_Call | N_Timed_Entry_Call |
| 4510 |
then |
| 4511 |
return Parent (Parent (Curr)); |
| 4512 |
|
| 4513 |
-- General case for entry or procedure calls |
| 4514 |
|
| 4515 |
else |
| 4516 |
return Curr; |
| 4517 |
end if; |
| 4518 |
|
| 4519 |
when N_Pragma => |
| 4520 |
|
| 4521 |
-- Pragma Check is not a valid transient context in |
| 4522 |
-- GNATprove mode because the pragma must remain unchanged. |
| 4523 |
|
| 4524 |
if GNATprove_Mode |
| 4525 |
and then Get_Pragma_Id (Curr) = Pragma_Check |
| 4526 |
then |
| 4527 |
return Empty; |
| 4528 |
|
| 4529 |
-- General case for pragmas |
| 4530 |
|
| 4531 |
else |
| 4532 |
return Curr; |
| 4533 |
end if; |
| 4534 |
|
| 4535 |
when N_Raise_Statement => |
| 4536 |
return Curr; |
| 4537 |
|
| 4538 |
when N_Simple_Return_Statement => |
| 4539 |
declare |
| 4540 |
Fun_Id : constant Entity_Id := |
| 4541 |
Return_Applies_To (Return_Statement_Entity (Curr)); |
| 4542 |
|
| 4543 |
begin |
| 4544 |
-- A transient context that must manage the secondary |
| 4545 |
-- stack cannot be a return statement of a function that |
| 4546 |
-- itself requires secondary stack management, because |
| 4547 |
-- the function's result would be reclaimed too early. |
| 4548 |
-- And returns of thunks never require transient scopes. |
| 4549 |
|
| 4550 |
if (Manage_Sec_Stack |
| 4551 |
and then Needs_Secondary_Stack (Etype (Fun_Id))) |
| 4552 |
or else Is_Thunk (Fun_Id) |
| 4553 |
then |
| 4554 |
return Empty; |
| 4555 |
|
| 4556 |
-- General case for return statements |
| 4557 |
|
| 4558 |
else |
| 4559 |
return Curr; |
| 4560 |
end if; |
| 4561 |
end; |
| 4562 |
|
| 4563 |
-- Special |
| 4564 |
|
| 4565 |
when N_Attribute_Reference => |
| 4566 |
if Is_Procedure_Attribute_Name (Attribute_Name (Curr)) then |
| 4567 |
return Curr; |
| 4568 |
end if; |
| 4569 |
|
| 4570 |
-- An Ada 2012 iterator specification is not a valid context |
| 4571 |
-- because Analyze_Iterator_Specification already employs |
| 4572 |
-- special processing for it. |
| 4573 |
|
| 4574 |
when N_Iterator_Specification => |
| 4575 |
return Empty; |
| 4576 |
|
| 4577 |
when N_Loop_Parameter_Specification => |
| 4578 |
|
| 4579 |
-- An iteration scheme is not a valid context because |
| 4580 |
-- routine Analyze_Iteration_Scheme already employs |
| 4581 |
-- special processing. |
| 4582 |
|
| 4583 |
if Nkind (Parent (Curr)) = N_Iteration_Scheme then |
| 4584 |
return Empty; |
| 4585 |
else |
| 4586 |
return Parent (Curr); |
| 4587 |
end if; |
| 4588 |
|
| 4589 |
-- Termination |
| 4590 |
|
| 4591 |
-- The following nodes represent "dummy contexts" which do not |
| 4592 |
-- need to be wrapped. |
| 4593 |
|
| 4594 |
when N_Component_Declaration |
| 4595 |
| N_Discriminant_Specification |
| 4596 |
| N_Parameter_Specification |
| 4597 |
=> |
| 4598 |
return Empty; |
| 4599 |
|
| 4600 |
-- If the traversal leaves a scope without having been able to |
| 4601 |
-- find a construct to wrap, something is going wrong, but this |
| 4602 |
-- can happen in error situations that are not detected yet |
| 4603 |
-- (such as a dynamic string in a pragma Export). |
| 4604 |
|
| 4605 |
when N_Block_Statement |
| 4606 |
| N_Entry_Body |
| 4607 |
| N_Package_Body |
| 4608 |
| N_Package_Declaration |
| 4609 |
| N_Protected_Body |
| 4610 |
| N_Subprogram_Body |
| 4611 |
| N_Task_Body |
| 4612 |
=> |
| 4613 |
return Empty; |
| 4614 |
|
| 4615 |
-- Default |
| 4616 |
|
| 4617 |
when others => |
| 4618 |
null; |
| 4619 |
end case; |
| 4620 |
|
| 4621 |
Prev := Curr; |
| 4622 |
Curr := Parent (Curr); |
| 4623 |
end loop; |
| 4624 |
|
| 4625 |
return Empty; |
| 4626 |
end Find_Transient_Context; |
| 4627 |
|
| 4628 |
------------------------------ |
| 4629 |
-- Is_Package_Or_Subprogram -- |
| 4630 |
------------------------------ |
| 4631 |
|
| 4632 |
function Is_Package_Or_Subprogram (Id : Entity_Id) return Boolean is |
| 4633 |
begin |
| 4634 |
return Ekind (Id) in E_Entry |
| 4635 |
| E_Entry_Family |
| 4636 |
| E_Function |
| 4637 |
| E_Package |
| 4638 |
| E_Procedure |
| 4639 |
| E_Subprogram_Body; |
| 4640 |
end Is_Package_Or_Subprogram; |
| 4641 |
|
| 4642 |
-- Local variables |
| 4643 |
|
| 4644 |
Trans_Idx : constant Int := Find_Enclosing_Transient_Scope; |
| 4645 |
Context : Node_Id; |
| 4646 |
|
| 4647 |
-- Start of processing for Establish_Transient_Scope |
| 4648 |
|
| 4649 |
begin |
| 4650 |
-- Do not create a new transient scope if there is already an enclosing |
| 4651 |
-- transient scope within the innermost enclosing package or subprogram. |
| 4652 |
|
| 4653 |
if Trans_Idx >= 0 then |
| 4654 |
|
| 4655 |
-- If the transient scope was requested for purposes of managing the |
| 4656 |
-- secondary stack, then the existing scope must perform this task, |
| 4657 |
-- unless the node to be wrapped is a return statement of a function |
| 4658 |
-- that requires secondary stack management, because the function's |
| 4659 |
-- result would be reclaimed too early (see Find_Transient_Context). |
| 4660 |
|
| 4661 |
if Manage_Sec_Stack then |
| 4662 |
declare |
| 4663 |
SE : Scope_Stack_Entry renames Scope_Stack.Table (Trans_Idx); |
| 4664 |
|
| 4665 |
begin |
| 4666 |
if Nkind (SE.Node_To_Be_Wrapped) /= N_Simple_Return_Statement |
| 4667 |
or else not |
| 4668 |
Needs_Secondary_Stack |
| 4669 |
(Etype |
| 4670 |
(Return_Applies_To |
| 4671 |
(Return_Statement_Entity (SE.Node_To_Be_Wrapped)))) |
| 4672 |
then |
| 4673 |
Set_Uses_Sec_Stack (SE.Entity); |
| 4674 |
end if; |
| 4675 |
end; |
| 4676 |
end if; |
| 4677 |
|
| 4678 |
return; |
| 4679 |
end if; |
| 4680 |
|
| 4681 |
-- Find the construct that must be serviced by a new transient scope, if |
| 4682 |
-- it exists. |
| 4683 |
|
| 4684 |
Context := Find_Transient_Context (N); |
| 4685 |
|
| 4686 |
if Present (Context) then |
| 4687 |
if Nkind (Context) = N_Assignment_Statement then |
| 4688 |
|
| 4689 |
-- An assignment statement with suppressed controlled semantics |
| 4690 |
-- does not need a transient scope because finalization is not |
| 4691 |
-- desirable at this point. Note that No_Ctrl_Actions is also |
| 4692 |
-- set for non-controlled assignments to suppress dispatching |
| 4693 |
-- _assign. |
| 4694 |
|
| 4695 |
if No_Ctrl_Actions (Context) |
| 4696 |
and then Needs_Finalization (Etype (Name (Context))) |
| 4697 |
then |
| 4698 |
-- When a controlled component is initialized by a function |
| 4699 |
-- call, the result on the secondary stack is always assigned |
| 4700 |
-- to the component. Signal the nearest suitable scope that it |
| 4701 |
-- is safe to manage the secondary stack. |
| 4702 |
|
| 4703 |
if Manage_Sec_Stack and then Within_Init_Proc then |
| 4704 |
Delegate_Sec_Stack_Management; |
| 4705 |
end if; |
| 4706 |
|
| 4707 |
-- Otherwise the assignment is a normal transient context and thus |
| 4708 |
-- requires a transient scope. |
| 4709 |
|
| 4710 |
else |
| 4711 |
Create_Transient_Scope (Context); |
| 4712 |
end if; |
| 4713 |
|
| 4714 |
-- General case |
| 4715 |
|
| 4716 |
else |
| 4717 |
Create_Transient_Scope (Context); |
| 4718 |
end if; |
| 4719 |
end if; |
| 4720 |
end Establish_Transient_Scope; |
| 4721 |
|
| 4722 |
---------------------------- |
| 4723 |
-- Expand_Cleanup_Actions -- |
| 4724 |
---------------------------- |
| 4725 |
|
| 4726 |
procedure Expand_Cleanup_Actions (N : Node_Id) is |
| 4727 |
pragma Assert |
| 4728 |
(Nkind (N) in N_Block_Statement |
| 4729 |
| N_Subprogram_Body |
| 4730 |
| N_Task_Body |
| 4731 |
| N_Entry_Body |
| 4732 |
| N_Extended_Return_Statement); |
| 4733 |
|
| 4734 |
Scop : constant Entity_Id := Current_Scope; |
| 4735 |
|
| 4736 |
Is_Asynchronous_Call : constant Boolean := |
| 4737 |
Nkind (N) = N_Block_Statement |
| 4738 |
and then Is_Asynchronous_Call_Block (N); |
| 4739 |
Is_Master : constant Boolean := |
| 4740 |
Nkind (N) /= N_Extended_Return_Statement |
| 4741 |
and then Nkind (N) /= N_Entry_Body |
| 4742 |
and then Is_Task_Master (N); |
| 4743 |
Is_Protected_Subp_Body : constant Boolean := |
| 4744 |
Nkind (N) = N_Subprogram_Body |
| 4745 |
and then Is_Protected_Subprogram_Body (N); |
| 4746 |
Is_Task_Allocation : constant Boolean := |
| 4747 |
Nkind (N) = N_Block_Statement |
| 4748 |
and then Is_Task_Allocation_Block (N); |
| 4749 |
Is_Task_Body : constant Boolean := |
| 4750 |
Nkind (Original_Node (N)) = N_Task_Body; |
| 4751 |
|
| 4752 |
-- We mark the secondary stack if it is used in this construct, and |
| 4753 |
-- we're not returning a function result on the secondary stack, except |
| 4754 |
-- that a build-in-place function that might or might not return on the |
| 4755 |
-- secondary stack always needs a mark. A run-time test is required in |
| 4756 |
-- the case where the build-in-place function has a BIP_Alloc extra |
| 4757 |
-- parameter (see Create_Finalizer). |
| 4758 |
|
| 4759 |
Needs_Sec_Stack_Mark : constant Boolean := |
| 4760 |
(Uses_Sec_Stack (Scop) |
| 4761 |
and then |
| 4762 |
not Sec_Stack_Needed_For_Return (Scop)) |
| 4763 |
or else |
| 4764 |
(Is_Build_In_Place_Function (Scop) |
| 4765 |
and then Needs_BIP_Alloc_Form (Scop)); |
| 4766 |
|
| 4767 |
Needs_Custom_Cleanup : constant Boolean := |
| 4768 |
Nkind (N) = N_Block_Statement |
| 4769 |
and then Present (Cleanup_Actions (N)); |
| 4770 |
|
| 4771 |
Actions_Required : constant Boolean := |
| 4772 |
Requires_Cleanup_Actions (N, True) |
| 4773 |
or else Is_Asynchronous_Call |
| 4774 |
or else Is_Master |
| 4775 |
or else Is_Protected_Subp_Body |
| 4776 |
or else Is_Task_Allocation |
| 4777 |
or else Is_Task_Body |
| 4778 |
or else Needs_Sec_Stack_Mark |
| 4779 |
or else Needs_Custom_Cleanup; |
| 4780 |
|
| 4781 |
Loc : Source_Ptr; |
| 4782 |
Cln : List_Id; |
| 4783 |
|
| 4784 |
-- Start of processing for Expand_Cleanup_Actions |
| 4785 |
|
| 4786 |
begin |
| 4787 |
-- The current construct does not need any form of servicing |
| 4788 |
|
| 4789 |
if not Actions_Required then |
| 4790 |
return; |
| 4791 |
end if; |
| 4792 |
|
| 4793 |
-- If an extended return statement contains something like |
| 4794 |
-- |
| 4795 |
-- X := F (...); |
| 4796 |
-- |
| 4797 |
-- where F is a build-in-place function call returning a controlled |
| 4798 |
-- type, then a temporary object will be implicitly declared as part |
| 4799 |
-- of the statement list, and this will need cleanup. In such cases, |
| 4800 |
-- we transform: |
| 4801 |
-- |
| 4802 |
-- return Result : T := ... do |
| 4803 |
-- <statements> -- possibly with handlers |
| 4804 |
-- end return; |
| 4805 |
-- |
| 4806 |
-- into: |
| 4807 |
-- |
| 4808 |
-- return Result : T := ... do |
| 4809 |
-- declare -- no declarations |
| 4810 |
-- begin |
| 4811 |
-- <statements> -- possibly with handlers |
| 4812 |
-- end; -- no handlers |
| 4813 |
-- end return; |
| 4814 |
-- |
| 4815 |
-- So Expand_Cleanup_Actions will end up being called recursively on the |
| 4816 |
-- block statement. |
| 4817 |
|
| 4818 |
if Nkind (N) = N_Extended_Return_Statement then |
| 4819 |
declare |
| 4820 |
Block : constant Node_Id := |
| 4821 |
Make_Block_Statement (Sloc (N), |
| 4822 |
Declarations => Empty_List, |
| 4823 |
Handled_Statement_Sequence => |
| 4824 |
Handled_Statement_Sequence (N)); |
| 4825 |
begin |
| 4826 |
Set_Handled_Statement_Sequence (N, |
| 4827 |
Make_Handled_Sequence_Of_Statements (Sloc (N), |
| 4828 |
Statements => New_List (Block))); |
| 4829 |
|
| 4830 |
Analyze (Block); |
| 4831 |
end; |
| 4832 |
|
| 4833 |
-- Analysis of the block did all the work |
| 4834 |
|
| 4835 |
return; |
| 4836 |
end if; |
| 4837 |
|
| 4838 |
if Needs_Custom_Cleanup then |
| 4839 |
Cln := Cleanup_Actions (N); |
| 4840 |
else |
| 4841 |
Cln := No_List; |
| 4842 |
end if; |
| 4843 |
|
| 4844 |
if No (Declarations (N)) then |
| 4845 |
Set_Declarations (N, New_List); |
| 4846 |
end if; |
| 4847 |
|
| 4848 |
declare |
| 4849 |
Fin_Id : Entity_Id; |
| 4850 |
Mark : Entity_Id := Empty; |
| 4851 |
|
| 4852 |
begin |
| 4853 |
-- If we are generating expanded code for debugging purposes, use the |
| 4854 |
-- Sloc of the point of insertion for the cleanup code. The Sloc will |
| 4855 |
-- be updated subsequently to reference the proper line in .dg files. |
| 4856 |
-- If we are not debugging generated code, use No_Location instead, |
| 4857 |
-- so that no debug information is generated for the cleanup code. |
| 4858 |
-- This makes the behavior of the NEXT command in GDB monotonic, and |
| 4859 |
-- makes the placement of breakpoints more accurate. |
| 4860 |
|
| 4861 |
if Debug_Generated_Code then |
| 4862 |
Loc := Sloc (Scop); |
| 4863 |
else |
| 4864 |
Loc := No_Location; |
| 4865 |
end if; |
| 4866 |
|
| 4867 |
-- A task activation call has already been built for a task |
| 4868 |
-- allocation block. |
| 4869 |
|
| 4870 |
if not Is_Task_Allocation then |
| 4871 |
Build_Task_Activation_Call (N); |
| 4872 |
end if; |
| 4873 |
|
| 4874 |
if Is_Master then |
| 4875 |
Establish_Task_Master (N); |
| 4876 |
end if; |
| 4877 |
|
| 4878 |
-- If secondary stack is in use, generate: |
| 4879 |
-- |
| 4880 |
-- Mnn : constant Mark_Id := SS_Mark; |
| 4881 |
|
| 4882 |
if Needs_Sec_Stack_Mark then |
| 4883 |
Set_Uses_Sec_Stack (Scop, False); -- avoid duplicate SS marks |
| 4884 |
Mark := Make_Temporary (Loc, 'M'); |
| 4885 |
|
| 4886 |
declare |
| 4887 |
Mark_Call : constant Node_Id := Build_SS_Mark_Call (Loc, Mark); |
| 4888 |
begin |
| 4889 |
Prepend_To (Declarations (N), Mark_Call); |
| 4890 |
Analyze (Mark_Call); |
| 4891 |
end; |
| 4892 |
end if; |
| 4893 |
|
| 4894 |
-- Generate finalization calls for all controlled objects appearing |
| 4895 |
-- in the statements of N. Add context specific cleanup for various |
| 4896 |
-- constructs. |
| 4897 |
|
| 4898 |
Build_Finalizer |
| 4899 |
(N => N, |
| 4900 |
Clean_Stmts => Build_Cleanup_Statements (N, Cln), |
| 4901 |
Mark_Id => Mark, |
| 4902 |
Defer_Abort => Nkind (Original_Node (N)) = N_Task_Body |
| 4903 |
or else Is_Master, |
| 4904 |
Fin_Id => Fin_Id); |
| 4905 |
|
| 4906 |
if Present (Fin_Id) then |
| 4907 |
Build_Finalizer_Call (N, Fin_Id); |
| 4908 |
end if; |
| 4909 |
end; |
| 4910 |
end Expand_Cleanup_Actions; |
| 4911 |
|
| 4912 |
--------------------------- |
| 4913 |
-- Expand_N_Package_Body -- |
| 4914 |
--------------------------- |
| 4915 |
|
| 4916 |
-- Add call to Activate_Tasks if body is an activator (actual processing |
| 4917 |
-- is in chapter 9). |
| 4918 |
|
| 4919 |
-- Generate subprogram descriptor for elaboration routine |
| 4920 |
|
| 4921 |
-- Encode entity names in package body |
| 4922 |
|
| 4923 |
procedure Expand_N_Package_Body (N : Node_Id) is |
| 4924 |
Id : constant Entity_Id := Defining_Entity (N); |
| 4925 |
Spec_Id : constant Entity_Id := Corresponding_Spec (N); |
| 4926 |
|
| 4927 |
Fin_Id : Entity_Id; |
| 4928 |
|
| 4929 |
begin |
| 4930 |
-- This is done only for non-generic packages |
| 4931 |
|
| 4932 |
if Ekind (Spec_Id) = E_Package then |
| 4933 |
-- Build dispatch tables of library-level tagged types for bodies |
| 4934 |
-- that are not compilation units (see Analyze_Compilation_Unit), |
| 4935 |
-- except for instances because they have no N_Compilation_Unit. |
| 4936 |
|
| 4937 |
if Tagged_Type_Expansion |
| 4938 |
and then Is_Library_Level_Entity (Spec_Id) |
| 4939 |
and then (not Is_Compilation_Unit (Spec_Id) |
| 4940 |
or else Is_Generic_Instance (Spec_Id)) |
| 4941 |
then |
| 4942 |
Build_Static_Dispatch_Tables (N); |
| 4943 |
end if; |
| 4944 |
|
| 4945 |
Push_Scope (Spec_Id); |
| 4946 |
|
| 4947 |
Expand_CUDA_Package (N); |
| 4948 |
|
| 4949 |
Build_Task_Activation_Call (N); |
| 4950 |
|
| 4951 |
-- Verify the run-time semantics of pragma Initial_Condition at the |
| 4952 |
-- end of the body statements. |
| 4953 |
|
| 4954 |
Expand_Pragma_Initial_Condition (Spec_Id, N); |
| 4955 |
|
| 4956 |
-- If this is a library-level package and unnesting is enabled, |
| 4957 |
-- check for the presence of blocks with nested subprograms occurring |
| 4958 |
-- in elaboration code, and generate procedures to encapsulate the |
| 4959 |
-- blocks in case the nested subprograms make up-level references. |
| 4960 |
|
| 4961 |
if Unnest_Subprogram_Mode |
| 4962 |
and then |
| 4963 |
Is_Library_Level_Entity (Current_Scope) |
| 4964 |
then |
| 4965 |
Check_Unnesting_Elaboration_Code (N); |
| 4966 |
Check_Unnesting_In_Decls_Or_Stmts (Declarations (N)); |
| 4967 |
Check_Unnesting_In_Handlers (N); |
| 4968 |
end if; |
| 4969 |
|
| 4970 |
Pop_Scope; |
| 4971 |
end if; |
| 4972 |
|
| 4973 |
Set_Elaboration_Flag (N, Spec_Id); |
| 4974 |
Set_In_Package_Body (Spec_Id, False); |
| 4975 |
|
| 4976 |
-- Set to encode entity names in package body before gigi is called |
| 4977 |
|
| 4978 |
Qualify_Entity_Names (N); |
| 4979 |
|
| 4980 |
if Ekind (Spec_Id) /= E_Generic_Package |
| 4981 |
and then not Delay_Cleanups (Id) |
| 4982 |
then |
| 4983 |
Build_Finalizer |
| 4984 |
(N => N, |
| 4985 |
Clean_Stmts => No_List, |
| 4986 |
Mark_Id => Empty, |
| 4987 |
Defer_Abort => False, |
| 4988 |
Fin_Id => Fin_Id); |
| 4989 |
|
| 4990 |
if Present (Fin_Id) then |
| 4991 |
Set_Finalizer (Defining_Entity (N), Fin_Id); |
| 4992 |
end if; |
| 4993 |
end if; |
| 4994 |
end Expand_N_Package_Body; |
| 4995 |
|
| 4996 |
---------------------------------- |
| 4997 |
-- Expand_N_Package_Declaration -- |
| 4998 |
---------------------------------- |
| 4999 |
|
| 5000 |
-- Add call to Activate_Tasks if there are tasks declared and the package |
| 5001 |
-- has no body. Note that in Ada 83 this may result in premature activation |
| 5002 |
-- of some tasks, given that we cannot tell whether a body will eventually |
| 5003 |
-- appear. |
| 5004 |
|
| 5005 |
procedure Expand_N_Package_Declaration (N : Node_Id) is |
| 5006 |
Id : constant Entity_Id := Defining_Entity (N); |
| 5007 |
Spec : constant Node_Id := Specification (N); |
| 5008 |
Decls : List_Id; |
| 5009 |
Fin_Id : Entity_Id; |
| 5010 |
|
| 5011 |
No_Body : Boolean := False; |
| 5012 |
-- True in the case of a package declaration that is a compilation |
| 5013 |
-- unit and for which no associated body will be compiled in this |
| 5014 |
-- compilation. |
| 5015 |
|
| 5016 |
begin |
| 5017 |
-- Case of a package declaration other than a compilation unit |
| 5018 |
|
| 5019 |
if Nkind (Parent (N)) /= N_Compilation_Unit then |
| 5020 |
null; |
| 5021 |
|
| 5022 |
-- Case of a compilation unit that does not require a body |
| 5023 |
|
| 5024 |
elsif not Body_Required (Parent (N)) |
| 5025 |
and then not Unit_Requires_Body (Id) |
| 5026 |
then |
| 5027 |
No_Body := True; |
| 5028 |
|
| 5029 |
-- Special case of generating calling stubs for a remote call interface |
| 5030 |
-- package: even though the package declaration requires one, the body |
| 5031 |
-- won't be processed in this compilation (so any stubs for RACWs |
| 5032 |
-- declared in the package must be generated here, along with the spec). |
| 5033 |
|
| 5034 |
elsif Parent (N) = Cunit (Main_Unit) |
| 5035 |
and then Is_Remote_Call_Interface (Id) |
| 5036 |
and then Distribution_Stub_Mode = Generate_Caller_Stub_Body |
| 5037 |
then |
| 5038 |
No_Body := True; |
| 5039 |
end if; |
| 5040 |
|
| 5041 |
-- For a nested instance, delay processing until freeze point |
| 5042 |
|
| 5043 |
if Has_Delayed_Freeze (Id) |
| 5044 |
and then Nkind (Parent (N)) /= N_Compilation_Unit |
| 5045 |
then |
| 5046 |
return; |
| 5047 |
end if; |
| 5048 |
|
| 5049 |
-- For a package declaration that implies no associated body, generate |
| 5050 |
-- task activation call and RACW supporting bodies now (since we won't |
| 5051 |
-- have a specific separate compilation unit for that). |
| 5052 |
|
| 5053 |
if No_Body then |
| 5054 |
Push_Scope (Id); |
| 5055 |
|
| 5056 |
-- Generate RACW subprogram bodies |
| 5057 |
|
| 5058 |
if Has_RACW (Id) then |
| 5059 |
Decls := Private_Declarations (Spec); |
| 5060 |
|
| 5061 |
if No (Decls) then |
| 5062 |
Decls := Visible_Declarations (Spec); |
| 5063 |
end if; |
| 5064 |
|
| 5065 |
if No (Decls) then |
| 5066 |
Decls := New_List; |
| 5067 |
Set_Visible_Declarations (Spec, Decls); |
| 5068 |
end if; |
| 5069 |
|
| 5070 |
Append_RACW_Bodies (Decls, Id); |
| 5071 |
Analyze_List (Decls); |
| 5072 |
end if; |
| 5073 |
|
| 5074 |
-- Generate task activation call as last step of elaboration |
| 5075 |
|
| 5076 |
if Present (Activation_Chain_Entity (N)) then |
| 5077 |
Build_Task_Activation_Call (N); |
| 5078 |
end if; |
| 5079 |
|
| 5080 |
-- Verify the run-time semantics of pragma Initial_Condition at the |
| 5081 |
-- end of the private declarations when the package lacks a body. |
| 5082 |
|
| 5083 |
Expand_Pragma_Initial_Condition (Id, N); |
| 5084 |
|
| 5085 |
Pop_Scope; |
| 5086 |
end if; |
| 5087 |
|
| 5088 |
-- Build dispatch tables of library-level tagged types for instances |
| 5089 |
-- that are not compilation units (see Analyze_Compilation_Unit). |
| 5090 |
|
| 5091 |
if Tagged_Type_Expansion |
| 5092 |
and then Is_Library_Level_Entity (Id) |
| 5093 |
and then Is_Generic_Instance (Id) |
| 5094 |
and then not Is_Compilation_Unit (Id) |
| 5095 |
then |
| 5096 |
Build_Static_Dispatch_Tables (N); |
| 5097 |
end if; |
| 5098 |
|
| 5099 |
-- Note: it is not necessary to worry about generating a subprogram |
| 5100 |
-- descriptor, since the only way to get exception handlers into a |
| 5101 |
-- package spec is to include instantiations, and that would cause |
| 5102 |
-- generation of subprogram descriptors to be delayed in any case. |
| 5103 |
|
| 5104 |
-- Set to encode entity names in package spec before gigi is called |
| 5105 |
|
| 5106 |
Qualify_Entity_Names (N); |
| 5107 |
|
| 5108 |
if Ekind (Id) /= E_Generic_Package |
| 5109 |
and then not Delay_Cleanups (Id) |
| 5110 |
then |
| 5111 |
Build_Finalizer |
| 5112 |
(N => N, |
| 5113 |
Clean_Stmts => No_List, |
| 5114 |
Mark_Id => Empty, |
| 5115 |
Defer_Abort => False, |
| 5116 |
Fin_Id => Fin_Id); |
| 5117 |
|
| 5118 |
if Present (Fin_Id) then |
| 5119 |
Set_Finalizer (Id, Fin_Id); |
| 5120 |
end if; |
| 5121 |
end if; |
| 5122 |
|
| 5123 |
-- If this is a library-level package and unnesting is enabled, |
| 5124 |
-- check for the presence of blocks with nested subprograms occurring |
| 5125 |
-- in elaboration code, and generate procedures to encapsulate the |
| 5126 |
-- blocks in case the nested subprograms make up-level references. |
| 5127 |
|
| 5128 |
if Unnest_Subprogram_Mode |
| 5129 |
and then Is_Library_Level_Entity (Current_Scope) |
| 5130 |
then |
| 5131 |
Check_Unnesting_In_Decls_Or_Stmts (Visible_Declarations (Spec)); |
| 5132 |
Check_Unnesting_In_Decls_Or_Stmts (Private_Declarations (Spec)); |
| 5133 |
end if; |
| 5134 |
end Expand_N_Package_Declaration; |
| 5135 |
|
| 5136 |
--------------------------------- |
| 5137 |
-- Has_Simple_Protected_Object -- |
| 5138 |
--------------------------------- |
| 5139 |
|
| 5140 |
function Has_Simple_Protected_Object (T : Entity_Id) return Boolean is |
| 5141 |
begin |
| 5142 |
if Has_Task (T) then |
| 5143 |
return False; |
| 5144 |
|
| 5145 |
elsif Is_Simple_Protected_Type (T) then |
| 5146 |
return True; |
| 5147 |
|
| 5148 |
elsif Is_Array_Type (T) then |
| 5149 |
return Has_Simple_Protected_Object (Component_Type (T)); |
| 5150 |
|
| 5151 |
elsif Is_Record_Type (T) then |
| 5152 |
declare |
| 5153 |
Comp : Entity_Id; |
| 5154 |
|
| 5155 |
begin |
| 5156 |
Comp := First_Component (T); |
| 5157 |
while Present (Comp) loop |
| 5158 |
if Has_Simple_Protected_Object (Etype (Comp)) then |
| 5159 |
return True; |
| 5160 |
end if; |
| 5161 |
|
| 5162 |
Next_Component (Comp); |
| 5163 |
end loop; |
| 5164 |
|
| 5165 |
return False; |
| 5166 |
end; |
| 5167 |
|
| 5168 |
else |
| 5169 |
return False; |
| 5170 |
end if; |
| 5171 |
end Has_Simple_Protected_Object; |
| 5172 |
|
| 5173 |
------------------------------------ |
| 5174 |
-- Insert_Actions_In_Scope_Around -- |
| 5175 |
------------------------------------ |
| 5176 |
|
| 5177 |
procedure Insert_Actions_In_Scope_Around |
| 5178 |
(N : Node_Id; |
| 5179 |
Clean : Boolean; |
| 5180 |
Manage_SS : Boolean) |
| 5181 |
is |
| 5182 |
Act_Before : constant List_Id := |
| 5183 |
Scope_Stack.Table (Scope_Stack.Last).Actions_To_Be_Wrapped (Before); |
| 5184 |
Act_After : constant List_Id := |
| 5185 |
Scope_Stack.Table (Scope_Stack.Last).Actions_To_Be_Wrapped (After); |
| 5186 |
Act_Cleanup : constant List_Id := |
| 5187 |
Scope_Stack.Table (Scope_Stack.Last).Actions_To_Be_Wrapped (Cleanup); |
| 5188 |
-- Note: We used to use renamings of Scope_Stack.Table (Scope_Stack. |
| 5189 |
-- Last), but this was incorrect as Process_Transients_In_Scope may |
| 5190 |
-- introduce new scopes and cause a reallocation of Scope_Stack.Table. |
| 5191 |
|
| 5192 |
procedure Process_Transients_In_Scope |
| 5193 |
(First_Object : Node_Id; |
| 5194 |
Last_Object : Node_Id; |
| 5195 |
Related_Node : Node_Id); |
| 5196 |
-- Find all transient objects in the list First_Object .. Last_Object |
| 5197 |
-- and generate finalization actions for them. Related_Node denotes the |
| 5198 |
-- node which created all transient objects. |
| 5199 |
|
| 5200 |
--------------------------------- |
| 5201 |
-- Process_Transients_In_Scope -- |
| 5202 |
--------------------------------- |
| 5203 |
|
| 5204 |
procedure Process_Transients_In_Scope |
| 5205 |
(First_Object : Node_Id; |
| 5206 |
Last_Object : Node_Id; |
| 5207 |
Related_Node : Node_Id) |
| 5208 |
is |
| 5209 |
function Is_Subprogram_Call (N : Node_Id) return Traverse_Result; |
| 5210 |
-- Return Abandon if arbitrary node denotes a subprogram call |
| 5211 |
|
| 5212 |
function Has_Subprogram_Call is |
| 5213 |
new Traverse_Func (Is_Subprogram_Call); |
| 5214 |
|
| 5215 |
procedure Process_Transient_In_Scope |
| 5216 |
(Obj_Decl : Node_Id; |
| 5217 |
Insert_Nod : Node_Id; |
| 5218 |
Must_Export : Boolean); |
| 5219 |
-- Generate finalization actions for a single transient object |
| 5220 |
-- denoted by object declaration Obj_Decl. |
| 5221 |
|
| 5222 |
------------------------ |
| 5223 |
-- Is_Subprogram_Call -- |
| 5224 |
------------------------ |
| 5225 |
|
| 5226 |
function Is_Subprogram_Call (N : Node_Id) return Traverse_Result is |
| 5227 |
begin |
| 5228 |
-- A regular procedure or function call |
| 5229 |
|
| 5230 |
if Nkind (N) in N_Subprogram_Call then |
| 5231 |
return Abandon; |
| 5232 |
|
| 5233 |
-- Special cases |
| 5234 |
|
| 5235 |
-- Heavy expansion may relocate function calls outside the related |
| 5236 |
-- node. Inspect the original node to detect the initial placement |
| 5237 |
-- of the call. |
| 5238 |
|
| 5239 |
elsif Is_Rewrite_Substitution (N) then |
| 5240 |
return Has_Subprogram_Call (Original_Node (N)); |
| 5241 |
|
| 5242 |
-- Generalized indexing always involves a function call |
| 5243 |
|
| 5244 |
elsif Nkind (N) = N_Indexed_Component |
| 5245 |
and then Present (Generalized_Indexing (N)) |
| 5246 |
then |
| 5247 |
return Abandon; |
| 5248 |
|
| 5249 |
-- Keep searching |
| 5250 |
|
| 5251 |
else |
| 5252 |
return OK; |
| 5253 |
end if; |
| 5254 |
end Is_Subprogram_Call; |
| 5255 |
|
| 5256 |
-------------------------------- |
| 5257 |
-- Process_Transient_In_Scope -- |
| 5258 |
-------------------------------- |
| 5259 |
|
| 5260 |
procedure Process_Transient_In_Scope |
| 5261 |
(Obj_Decl : Node_Id; |
| 5262 |
Insert_Nod : Node_Id; |
| 5263 |
Must_Export : Boolean) |
| 5264 |
is |
| 5265 |
Loc : constant Source_Ptr := Sloc (Obj_Decl); |
| 5266 |
Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl); |
| 5267 |
|
| 5268 |
Master_Node_Id : Entity_Id; |
| 5269 |
Master_Node_Decl : Node_Id; |
| 5270 |
Obj_Ref : Node_Id; |
| 5271 |
Obj_Typ : Entity_Id; |
| 5272 |
|
| 5273 |
begin |
| 5274 |
-- Ignored Ghost objects do not need any cleanup actions because |
| 5275 |
-- they will not appear in the final tree. |
| 5276 |
|
| 5277 |
if Is_Ignored_Ghost_Entity (Obj_Id) then |
| 5278 |
return; |
| 5279 |
end if; |
| 5280 |
|
| 5281 |
-- If the object needs to be exported to the outer finalizer, |
| 5282 |
-- create the declaration of the Master_Node for the object, |
| 5283 |
-- which will later be picked up by Build_Finalizer. |
| 5284 |
|
| 5285 |
if Must_Export then |
| 5286 |
Master_Node_Id := Make_Temporary (Loc, 'N'); |
| 5287 |
Master_Node_Decl := |
| 5288 |
Make_Master_Node_Declaration (Loc, Master_Node_Id, Obj_Id); |
| 5289 |
Insert_Before_And_Analyze (Obj_Decl, Master_Node_Decl); |
| 5290 |
|
| 5291 |
-- Generate the attachment of the object to the Master_Node |
| 5292 |
|
| 5293 |
Attach_Object_To_Master_Node (Obj_Decl, Master_Node_Id); |
| 5294 |
|
| 5295 |
-- Then add the finalization call for the object |
| 5296 |
|
| 5297 |
Insert_After_And_Analyze (Insert_Nod, |
| 5298 |
Make_Finalize_Call_For_Node (Loc, Master_Node_Id)); |
| 5299 |
|
| 5300 |
-- Otherwise generate a direct finalization call for the object |
| 5301 |
|
| 5302 |
else |
| 5303 |
-- Handle the object type and the reference to the object |
| 5304 |
|
| 5305 |
Obj_Ref := New_Occurrence_Of (Obj_Id, Loc); |
| 5306 |
Obj_Typ := Base_Type (Etype (Obj_Id)); |
| 5307 |
|
| 5308 |
if Is_Access_Type (Obj_Typ) then |
| 5309 |
Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref); |
| 5310 |
Obj_Typ := Available_View (Designated_Type (Obj_Typ)); |
| 5311 |
end if; |
| 5312 |
|
| 5313 |
Insert_After_And_Analyze (Insert_Nod, |
| 5314 |
Make_Final_Call (Obj_Ref => Obj_Ref, Typ => Obj_Typ)); |
| 5315 |
end if; |
| 5316 |
|
| 5317 |
-- Mark the transient object to avoid double finalization |
| 5318 |
|
| 5319 |
Set_Is_Finalized_Transient (Obj_Id); |
| 5320 |
end Process_Transient_In_Scope; |
| 5321 |
|
| 5322 |
-- Local variables |
| 5323 |
|
| 5324 |
Insert_Nod : Node_Id; |
| 5325 |
-- Insertion node for the finalization actions |
| 5326 |
|
| 5327 |
Must_Export : Boolean; |
| 5328 |
-- Flag denoting whether the context requires transient object |
| 5329 |
-- export to the outer finalizer. |
| 5330 |
|
| 5331 |
Obj_Decl : Node_Id; |
| 5332 |
|
| 5333 |
-- Start of processing for Process_Transients_In_Scope |
| 5334 |
|
| 5335 |
begin |
| 5336 |
-- The expansion performed by this routine is as follows: |
| 5337 |
|
| 5338 |
-- Ctrl_Trans_Obj_1MN : Master_Node; |
| 5339 |
-- Ctrl_Trans_Obj_1 : ...; |
| 5340 |
-- . . . |
| 5341 |
-- Ctrl_Trans_Obj_NMN : Master_Node; |
| 5342 |
-- Ctrl_Trans_Obj_N : ...; |
| 5343 |
|
| 5344 |
-- Finalize_Object (Ctrl_Trans_Obj_NMN); |
| 5345 |
-- . . . |
| 5346 |
-- Finalize_Object (Ctrl_Trans_Obj_1MN); |
| 5347 |
|
| 5348 |
-- Recognize a scenario where the transient context is an object |
| 5349 |
-- declaration initialized by a build-in-place function call: |
| 5350 |
|
| 5351 |
-- Obj : ... := BIP_Function_Call (Ctrl_Func_Call); |
| 5352 |
|
| 5353 |
-- The rough expansion of the above is: |
| 5354 |
|
| 5355 |
-- Temp : ... := Ctrl_Func_Call; |
| 5356 |
-- Obj : ...; |
| 5357 |
-- Res : ... := BIP_Func_Call (..., Obj, ...); |
| 5358 |
|
| 5359 |
-- The finalization of any transient object must happen after the |
| 5360 |
-- build-in-place function call is executed. |
| 5361 |
|
| 5362 |
if Nkind (N) = N_Object_Declaration |
| 5363 |
and then Present (BIP_Initialization_Call (Defining_Identifier (N))) |
| 5364 |
then |
| 5365 |
Must_Export := True; |
| 5366 |
Insert_Nod := BIP_Initialization_Call (Defining_Identifier (N)); |
| 5367 |
|
| 5368 |
-- Search the context for at least one subprogram call. If found, the |
| 5369 |
-- machinery exports all transient objects to the enclosing finalizer |
| 5370 |
-- due to the possibility of abnormal call termination. |
| 5371 |
|
| 5372 |
else |
| 5373 |
Must_Export := Has_Subprogram_Call (N) = Abandon; |
| 5374 |
Insert_Nod := Last_Object; |
| 5375 |
end if; |
| 5376 |
|
| 5377 |
Insert_List_After_And_Analyze (Insert_Nod, Act_Cleanup); |
| 5378 |
|
| 5379 |
-- Examine all the objects in the list First_Object .. Last_Object |
| 5380 |
-- but skip the node to be wrapped because it is not transient as |
| 5381 |
-- far as this scope is concerned. |
| 5382 |
|
| 5383 |
Obj_Decl := First_Object; |
| 5384 |
while Present (Obj_Decl) loop |
| 5385 |
if Obj_Decl /= Related_Node |
| 5386 |
and then Nkind (Obj_Decl) = N_Object_Declaration |
| 5387 |
and then Analyzed (Obj_Decl) |
| 5388 |
and then Is_Finalizable_Transient (Obj_Decl, N) |
| 5389 |
then |
| 5390 |
Process_Transient_In_Scope (Obj_Decl, Insert_Nod, Must_Export); |
| 5391 |
end if; |
| 5392 |
|
| 5393 |
exit when Obj_Decl = Last_Object; |
| 5394 |
|
| 5395 |
Next (Obj_Decl); |
| 5396 |
end loop; |
| 5397 |
end Process_Transients_In_Scope; |
| 5398 |
|
| 5399 |
-- Local variables |
| 5400 |
|
| 5401 |
Loc : constant Source_Ptr := Sloc (N); |
| 5402 |
Node_To_Wrap : constant Node_Id := Node_To_Be_Wrapped; |
| 5403 |
First_Obj : Node_Id; |
| 5404 |
Last_Obj : Node_Id; |
| 5405 |
Mark_Id : Entity_Id; |
| 5406 |
Marker : Node_Id; |
| 5407 |
Target : Node_Id; |
| 5408 |
|
| 5409 |
-- Start of processing for Insert_Actions_In_Scope_Around |
| 5410 |
|
| 5411 |
begin |
| 5412 |
-- Nothing to do if the scope does not manage the secondary stack or |
| 5413 |
-- does not contain meaningful actions for insertion. |
| 5414 |
|
| 5415 |
if not Manage_SS |
| 5416 |
and then No (Act_Before) |
| 5417 |
and then No (Act_After) |
| 5418 |
and then No (Act_Cleanup) |
| 5419 |
then |
| 5420 |
return; |
| 5421 |
end if; |
| 5422 |
|
| 5423 |
-- If the node to be wrapped is the trigger of an asynchronous select, |
| 5424 |
-- it is not part of a statement list. The actions must be inserted |
| 5425 |
-- before the select itself, which is part of some list of statements. |
| 5426 |
-- Note that the triggering alternative includes the triggering |
| 5427 |
-- statement and an optional statement list. If the node to be |
| 5428 |
-- wrapped is part of that list, the normal insertion applies. |
| 5429 |
|
| 5430 |
if Nkind (Parent (Node_To_Wrap)) = N_Triggering_Alternative |
| 5431 |
and then not Is_List_Member (Node_To_Wrap) |
| 5432 |
then |
| 5433 |
Target := Parent (Parent (Node_To_Wrap)); |
| 5434 |
else |
| 5435 |
Target := N; |
| 5436 |
end if; |
| 5437 |
|
| 5438 |
-- Add all actions associated with a transient scope into the main tree. |
| 5439 |
-- There are several scenarios here: |
| 5440 |
|
| 5441 |
-- +--- Before ----+ +----- After ---+ |
| 5442 |
-- 1) First_Obj ....... Target ........ Last_Obj |
| 5443 |
|
| 5444 |
-- 2) First_Obj ....... Target |
| 5445 |
|
| 5446 |
-- 3) Target ........ Last_Obj |
| 5447 |
|
| 5448 |
-- Declarations are inserted before the target |
| 5449 |
|
| 5450 |
if Present (Act_Before) then |
| 5451 |
First_Obj := First (Act_Before); |
| 5452 |
Insert_List_Before (Target, Act_Before); |
| 5453 |
else |
| 5454 |
First_Obj := Target; |
| 5455 |
end if; |
| 5456 |
|
| 5457 |
-- Set a marker on the next statement |
| 5458 |
|
| 5459 |
Marker := Next (Target); |
| 5460 |
|
| 5461 |
-- Finalization calls are inserted after the target |
| 5462 |
|
| 5463 |
if Is_Non_Empty_List (Act_After) then |
| 5464 |
Last_Obj := Last (Act_After); |
| 5465 |
Insert_List_After (Target, Act_After); |
| 5466 |
else |
| 5467 |
Last_Obj := Target; |
| 5468 |
end if; |
| 5469 |
|
| 5470 |
-- Mark and release the secondary stack when the context warrants it |
| 5471 |
|
| 5472 |
if Manage_SS then |
| 5473 |
Mark_Id := Make_Temporary (Loc, 'M'); |
| 5474 |
|
| 5475 |
-- Generate: |
| 5476 |
-- Mnn : constant Mark_Id := SS_Mark; |
| 5477 |
|
| 5478 |
Insert_Before_And_Analyze |
| 5479 |
(First_Obj, Build_SS_Mark_Call (Loc, Mark_Id)); |
| 5480 |
|
| 5481 |
-- Generate: |
| 5482 |
-- SS_Release (Mnn); |
| 5483 |
|
| 5484 |
Insert_After_And_Analyze |
| 5485 |
(Last_Obj, Build_SS_Release_Call (Loc, Mark_Id)); |
| 5486 |
end if; |
| 5487 |
|
| 5488 |
-- If we are handling cleanups, check for transient objects associated |
| 5489 |
-- with Target and generate the required finalization actions for them. |
| 5490 |
|
| 5491 |
if Clean then |
| 5492 |
Process_Transients_In_Scope |
| 5493 |
(First_Object => First_Obj, |
| 5494 |
Last_Object => Last_Obj, |
| 5495 |
Related_Node => Target); |
| 5496 |
end if; |
| 5497 |
|
| 5498 |
-- If the target is the declaration of an object, park the generated |
| 5499 |
-- statements if need be. |
| 5500 |
|
| 5501 |
if Nkind (Target) = N_Object_Declaration |
| 5502 |
and then Next (Target) /= Marker |
| 5503 |
and then Needs_Initialization_Statements (Target) |
| 5504 |
then |
| 5505 |
Move_To_Initialization_Statements (Target, Marker); |
| 5506 |
end if; |
| 5507 |
|
| 5508 |
-- Reset the action lists |
| 5509 |
|
| 5510 |
Scope_Stack.Table |
| 5511 |
(Scope_Stack.Last).Actions_To_Be_Wrapped (Before) := No_List; |
| 5512 |
Scope_Stack.Table |
| 5513 |
(Scope_Stack.Last).Actions_To_Be_Wrapped (After) := No_List; |
| 5514 |
|
| 5515 |
if Clean then |
| 5516 |
Scope_Stack.Table |
| 5517 |
(Scope_Stack.Last).Actions_To_Be_Wrapped (Cleanup) := No_List; |
| 5518 |
end if; |
| 5519 |
end Insert_Actions_In_Scope_Around; |
| 5520 |
|
| 5521 |
------------------------------ |
| 5522 |
-- Is_Simple_Protected_Type -- |
| 5523 |
------------------------------ |
| 5524 |
|
| 5525 |
function Is_Simple_Protected_Type (T : Entity_Id) return Boolean is |
| 5526 |
begin |
| 5527 |
return |
| 5528 |
Is_Protected_Type (T) |
| 5529 |
and then not Uses_Lock_Free (T) |
| 5530 |
and then not Has_Entries (T) |
| 5531 |
and then Is_RTE (Find_Protection_Type (T), RE_Protection); |
| 5532 |
end Is_Simple_Protected_Type; |
| 5533 |
|
| 5534 |
------------------------------- |
| 5535 |
-- Make_Address_For_Finalize -- |
| 5536 |
------------------------------- |
| 5537 |
|
| 5538 |
function Make_Address_For_Finalize |
| 5539 |
(Loc : Source_Ptr; |
| 5540 |
Obj_Ref : Node_Id; |
| 5541 |
Obj_Typ : Entity_Id) return Node_Id |
| 5542 |
is |
| 5543 |
Utyp : constant Entity_Id := Underlying_Type (Obj_Typ); |
| 5544 |
|
| 5545 |
Obj_Addr : Node_Id; |
| 5546 |
|
| 5547 |
begin |
| 5548 |
Obj_Addr := |
| 5549 |
Make_Attribute_Reference (Loc, |
| 5550 |
Prefix => Obj_Ref, |
| 5551 |
Attribute_Name => Name_Address); |
| 5552 |
|
| 5553 |
-- If the type of a constrained array has an unconstrained first |
| 5554 |
-- subtype, its Finalize_Address primitive expects the address of |
| 5555 |
-- an object with a dope vector (see Make_Finalize_Address_Stmts). |
| 5556 |
-- This is achieved by setting Is_Constr_Array_Subt_With_Bounds, |
| 5557 |
-- but the address of the object is still that of its elements, |
| 5558 |
-- so we need to shift it back to skip the dope vector. |
| 5559 |
|
| 5560 |
if Is_Array_Type (Utyp) |
| 5561 |
and then not Is_Constrained (First_Subtype (Utyp)) |
| 5562 |
then |
| 5563 |
Obj_Addr := |
| 5564 |
Shift_Address_For_Descriptor |
| 5565 |
(Obj_Addr, First_Subtype (Utyp), Name_Op_Subtract); |
| 5566 |
end if; |
| 5567 |
|
| 5568 |
return Obj_Addr; |
| 5569 |
end Make_Address_For_Finalize; |
| 5570 |
|
| 5571 |
----------------------- |
| 5572 |
-- Make_Adjust_Call -- |
| 5573 |
----------------------- |
| 5574 |
|
| 5575 |
function Make_Adjust_Call |
| 5576 |
(Obj_Ref : Node_Id; |
| 5577 |
Typ : Entity_Id; |
| 5578 |
Skip_Self : Boolean := False) return Node_Id |
| 5579 |
is |
| 5580 |
Loc : constant Source_Ptr := Sloc (Obj_Ref); |
| 5581 |
Adj_Id : Entity_Id := Empty; |
| 5582 |
Ref : Node_Id; |
| 5583 |
Utyp : Entity_Id; |
| 5584 |
|
| 5585 |
begin |
| 5586 |
Ref := Obj_Ref; |
| 5587 |
|
| 5588 |
-- Recover the proper type which contains Deep_Adjust |
| 5589 |
|
| 5590 |
if Is_Class_Wide_Type (Typ) then |
| 5591 |
Utyp := Root_Type (Typ); |
| 5592 |
else |
| 5593 |
Utyp := Typ; |
| 5594 |
end if; |
| 5595 |
|
| 5596 |
Utyp := Underlying_Type (Base_Type (Utyp)); |
| 5597 |
Set_Assignment_OK (Ref); |
| 5598 |
|
| 5599 |
-- Deal with untagged derivation of private views |
| 5600 |
|
| 5601 |
if Present (Utyp) |
| 5602 |
and then Is_Untagged_Derivation (Typ) |
| 5603 |
and then Is_Implicit_Full_View (Utyp) |
| 5604 |
then |
| 5605 |
Utyp := Underlying_Type (Root_Type (Base_Type (Typ))); |
| 5606 |
Ref := Unchecked_Convert_To (Utyp, Ref); |
| 5607 |
Set_Assignment_OK (Ref); |
| 5608 |
end if; |
| 5609 |
|
| 5610 |
-- When dealing with the completion of a private type, use the base |
| 5611 |
-- type instead. |
| 5612 |
|
| 5613 |
if Present (Utyp) and then Utyp /= Base_Type (Utyp) then |
| 5614 |
pragma Assert (Is_Private_Type (Typ)); |
| 5615 |
|
| 5616 |
Utyp := Base_Type (Utyp); |
| 5617 |
Ref := Unchecked_Convert_To (Utyp, Ref); |
| 5618 |
end if; |
| 5619 |
|
| 5620 |
-- The underlying type may not be present due to a missing full view. In |
| 5621 |
-- this case freezing did not take place and there is no [Deep_]Adjust |
| 5622 |
-- primitive to call. |
| 5623 |
|
| 5624 |
if No (Utyp) then |
| 5625 |
return Empty; |
| 5626 |
|
| 5627 |
elsif Skip_Self then |
| 5628 |
if Has_Controlled_Component (Utyp) then |
| 5629 |
if Is_Tagged_Type (Utyp) then |
| 5630 |
Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust); |
| 5631 |
else |
| 5632 |
Adj_Id := TSS (Utyp, TSS_Deep_Adjust); |
| 5633 |
end if; |
| 5634 |
end if; |
| 5635 |
|
| 5636 |
-- Class-wide types, interfaces and types with controlled components |
| 5637 |
|
| 5638 |
elsif Is_Class_Wide_Type (Typ) |
| 5639 |
or else Is_Interface (Typ) |
| 5640 |
or else Has_Controlled_Component (Utyp) |
| 5641 |
then |
| 5642 |
if Is_Tagged_Type (Utyp) then |
| 5643 |
Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust); |
| 5644 |
else |
| 5645 |
Adj_Id := TSS (Utyp, TSS_Deep_Adjust); |
| 5646 |
end if; |
| 5647 |
|
| 5648 |
-- Derivations from [Limited_]Controlled |
| 5649 |
|
| 5650 |
elsif Is_Controlled (Utyp) then |
| 5651 |
Adj_Id := Find_Controlled_Prim_Op (Utyp, Name_Adjust); |
| 5652 |
|
| 5653 |
-- Tagged types |
| 5654 |
|
| 5655 |
elsif Is_Tagged_Type (Utyp) then |
| 5656 |
Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust); |
| 5657 |
|
| 5658 |
else |
| 5659 |
raise Program_Error; |
| 5660 |
end if; |
| 5661 |
|
| 5662 |
if Present (Adj_Id) then |
| 5663 |
-- The object reference may need another conversion depending on the |
| 5664 |
-- type of the formal and that of the actual. |
| 5665 |
|
| 5666 |
if not Is_Class_Wide_Type (Typ) then |
| 5667 |
Ref := Convert_View (Adj_Id, Ref, Typ); |
| 5668 |
end if; |
| 5669 |
|
| 5670 |
return |
| 5671 |
Make_Call (Loc, |
| 5672 |
Proc_Id => Adj_Id, |
| 5673 |
Param => Ref, |
| 5674 |
Skip_Self => Skip_Self); |
| 5675 |
else |
| 5676 |
return Empty; |
| 5677 |
end if; |
| 5678 |
end Make_Adjust_Call; |
| 5679 |
|
| 5680 |
--------------- |
| 5681 |
-- Make_Call -- |
| 5682 |
--------------- |
| 5683 |
|
| 5684 |
function Make_Call |
| 5685 |
(Loc : Source_Ptr; |
| 5686 |
Proc_Id : Entity_Id; |
| 5687 |
Param : Node_Id; |
| 5688 |
Skip_Self : Boolean := False) return Node_Id |
| 5689 |
is |
| 5690 |
Params : constant List_Id := New_List (Param); |
| 5691 |
|
| 5692 |
begin |
| 5693 |
-- Do not apply the controlled action to the object itself by signaling |
| 5694 |
-- the related routine to avoid self. |
| 5695 |
|
| 5696 |
if Skip_Self then |
| 5697 |
Append_To (Params, New_Occurrence_Of (Standard_False, Loc)); |
| 5698 |
end if; |
| 5699 |
|
| 5700 |
return |
| 5701 |
Make_Procedure_Call_Statement (Loc, |
| 5702 |
Name => New_Occurrence_Of (Proc_Id, Loc), |
| 5703 |
Parameter_Associations => Params); |
| 5704 |
end Make_Call; |
| 5705 |
|
| 5706 |
-------------------------- |
| 5707 |
-- Make_Deep_Array_Body -- |
| 5708 |
-------------------------- |
| 5709 |
|
| 5710 |
function Make_Deep_Array_Body |
| 5711 |
(Prim : Final_Primitives; |
| 5712 |
Typ : Entity_Id) return List_Id |
| 5713 |
is |
| 5714 |
function Build_Adjust_Or_Finalize_Statements |
| 5715 |
(Typ : Entity_Id) return List_Id; |
| 5716 |
-- Create the statements necessary to adjust or finalize an array of |
| 5717 |
-- controlled elements. Generate: |
| 5718 |
-- |
| 5719 |
-- declare |
| 5720 |
-- Abort : constant Boolean := Triggered_By_Abort; |
| 5721 |
-- <or> |
| 5722 |
-- Abort : constant Boolean := False; -- no abort |
| 5723 |
-- |
| 5724 |
-- E : Exception_Occurrence; |
| 5725 |
-- Raised : Boolean := False; |
| 5726 |
-- |
| 5727 |
-- begin |
| 5728 |
-- for J1 in [reverse] Typ'First (1) .. Typ'Last (1) loop |
| 5729 |
-- ^-- in the finalization case |
| 5730 |
-- ... |
| 5731 |
-- for Jn in [reverse] Typ'First (n) .. Typ'Last (n) loop |
| 5732 |
-- begin |
| 5733 |
-- [Deep_]Adjust / Finalize (V (J1, ..., Jn)); |
| 5734 |
-- |
| 5735 |
-- exception |
| 5736 |
-- when others => |
| 5737 |
-- if not Raised then |
| 5738 |
-- Raised := True; |
| 5739 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 5740 |
-- end if; |
| 5741 |
-- end; |
| 5742 |
-- end loop; |
| 5743 |
-- ... |
| 5744 |
-- end loop; |
| 5745 |
-- |
| 5746 |
-- if Raised and then not Abort then |
| 5747 |
-- Raise_From_Controlled_Operation (E); |
| 5748 |
-- end if; |
| 5749 |
-- end; |
| 5750 |
|
| 5751 |
function Build_Initialize_Statements (Typ : Entity_Id) return List_Id; |
| 5752 |
-- Create the statements necessary to initialize an array of controlled |
| 5753 |
-- elements. Include a mechanism to carry out partial finalization if an |
| 5754 |
-- exception occurs. Generate: |
| 5755 |
-- |
| 5756 |
-- declare |
| 5757 |
-- Counter : Integer := 0; |
| 5758 |
-- |
| 5759 |
-- begin |
| 5760 |
-- for J1 in V'Range (1) loop |
| 5761 |
-- ... |
| 5762 |
-- for JN in V'Range (N) loop |
| 5763 |
-- begin |
| 5764 |
-- [Deep_]Initialize (V (J1, ..., JN)); |
| 5765 |
-- |
| 5766 |
-- Counter := Counter + 1; |
| 5767 |
-- |
| 5768 |
-- exception |
| 5769 |
-- when others => |
| 5770 |
-- declare |
| 5771 |
-- Abort : constant Boolean := Triggered_By_Abort; |
| 5772 |
-- <or> |
| 5773 |
-- Abort : constant Boolean := False; -- no abort |
| 5774 |
-- E : Exception_Occurrence; |
| 5775 |
-- Raised : Boolean := False; |
| 5776 |
|
| 5777 |
-- begin |
| 5778 |
-- Counter := |
| 5779 |
-- V'Length (1) * |
| 5780 |
-- V'Length (2) * |
| 5781 |
-- ... |
| 5782 |
-- V'Length (N) - Counter; |
| 5783 |
|
| 5784 |
-- for F1 in reverse V'Range (1) loop |
| 5785 |
-- ... |
| 5786 |
-- for FN in reverse V'Range (N) loop |
| 5787 |
-- if Counter > 0 then |
| 5788 |
-- Counter := Counter - 1; |
| 5789 |
-- else |
| 5790 |
-- begin |
| 5791 |
-- [Deep_]Finalize (V (F1, ..., FN)); |
| 5792 |
|
| 5793 |
-- exception |
| 5794 |
-- when others => |
| 5795 |
-- if not Raised then |
| 5796 |
-- Raised := True; |
| 5797 |
-- Save_Occurrence (E, |
| 5798 |
-- Get_Current_Excep.all.all); |
| 5799 |
-- end if; |
| 5800 |
-- end; |
| 5801 |
-- end if; |
| 5802 |
-- end loop; |
| 5803 |
-- ... |
| 5804 |
-- end loop; |
| 5805 |
-- end; |
| 5806 |
-- |
| 5807 |
-- if Raised and then not Abort then |
| 5808 |
-- Raise_From_Controlled_Operation (E); |
| 5809 |
-- end if; |
| 5810 |
-- |
| 5811 |
-- raise; |
| 5812 |
-- end; |
| 5813 |
-- end loop; |
| 5814 |
-- end loop; |
| 5815 |
-- end; |
| 5816 |
|
| 5817 |
function New_References_To |
| 5818 |
(L : List_Id; |
| 5819 |
Loc : Source_Ptr) return List_Id; |
| 5820 |
-- Given a list of defining identifiers, return a list of references to |
| 5821 |
-- the original identifiers, in the same order as they appear. |
| 5822 |
|
| 5823 |
----------------------------------------- |
| 5824 |
-- Build_Adjust_Or_Finalize_Statements -- |
| 5825 |
----------------------------------------- |
| 5826 |
|
| 5827 |
function Build_Adjust_Or_Finalize_Statements |
| 5828 |
(Typ : Entity_Id) return List_Id |
| 5829 |
is |
| 5830 |
Comp_Typ : constant Entity_Id := Component_Type (Typ); |
| 5831 |
Index_List : constant List_Id := New_List; |
| 5832 |
Loc : constant Source_Ptr := Sloc (Typ); |
| 5833 |
Num_Dims : constant Int := Number_Dimensions (Typ); |
| 5834 |
|
| 5835 |
procedure Build_Indexes; |
| 5836 |
-- Generate the indexes used in the dimension loops |
| 5837 |
|
| 5838 |
------------------- |
| 5839 |
-- Build_Indexes -- |
| 5840 |
------------------- |
| 5841 |
|
| 5842 |
procedure Build_Indexes is |
| 5843 |
begin |
| 5844 |
-- Generate the following identifiers: |
| 5845 |
-- Jnn - for initialization |
| 5846 |
|
| 5847 |
for Dim in 1 .. Num_Dims loop |
| 5848 |
Append_To (Index_List, |
| 5849 |
Make_Defining_Identifier (Loc, New_External_Name ('J', Dim))); |
| 5850 |
end loop; |
| 5851 |
end Build_Indexes; |
| 5852 |
|
| 5853 |
-- Local variables |
| 5854 |
|
| 5855 |
Final_Decls : List_Id := No_List; |
| 5856 |
Final_Data : Finalization_Exception_Data; |
| 5857 |
Block : Node_Id; |
| 5858 |
Call : Node_Id; |
| 5859 |
Comp_Ref : Node_Id; |
| 5860 |
Core_Loop : Node_Id; |
| 5861 |
Dim : Int; |
| 5862 |
J : Entity_Id; |
| 5863 |
Loop_Id : Entity_Id; |
| 5864 |
Stmts : List_Id; |
| 5865 |
|
| 5866 |
-- Start of processing for Build_Adjust_Or_Finalize_Statements |
| 5867 |
|
| 5868 |
begin |
| 5869 |
Final_Decls := New_List; |
| 5870 |
|
| 5871 |
Build_Indexes; |
| 5872 |
Build_Object_Declarations (Final_Data, Final_Decls, Loc); |
| 5873 |
|
| 5874 |
Comp_Ref := |
| 5875 |
Make_Indexed_Component (Loc, |
| 5876 |
Prefix => Make_Identifier (Loc, Name_V), |
| 5877 |
Expressions => New_References_To (Index_List, Loc)); |
| 5878 |
Set_Etype (Comp_Ref, Comp_Typ); |
| 5879 |
|
| 5880 |
-- Generate: |
| 5881 |
-- [Deep_]Adjust (V (J1, ..., JN)) |
| 5882 |
|
| 5883 |
if Prim = Adjust_Case then |
| 5884 |
Call := Make_Adjust_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ); |
| 5885 |
|
| 5886 |
-- Generate: |
| 5887 |
-- [Deep_]Finalize (V (J1, ..., JN)) |
| 5888 |
|
| 5889 |
else pragma Assert (Prim = Finalize_Case); |
| 5890 |
Call := Make_Final_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ); |
| 5891 |
end if; |
| 5892 |
|
| 5893 |
if Present (Call) then |
| 5894 |
|
| 5895 |
-- Generate the block which houses the adjust or finalize call: |
| 5896 |
|
| 5897 |
-- begin |
| 5898 |
-- <adjust or finalize call> |
| 5899 |
|
| 5900 |
-- exception |
| 5901 |
-- when others => |
| 5902 |
-- if not Raised then |
| 5903 |
-- Raised := True; |
| 5904 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 5905 |
-- end if; |
| 5906 |
-- end; |
| 5907 |
|
| 5908 |
if Exceptions_OK then |
| 5909 |
Core_Loop := |
| 5910 |
Make_Block_Statement (Loc, |
| 5911 |
Handled_Statement_Sequence => |
| 5912 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 5913 |
Statements => New_List (Call), |
| 5914 |
Exception_Handlers => New_List ( |
| 5915 |
Build_Exception_Handler (Final_Data)))); |
| 5916 |
else |
| 5917 |
Core_Loop := Call; |
| 5918 |
end if; |
| 5919 |
|
| 5920 |
-- Generate the dimension loops starting from the innermost one |
| 5921 |
|
| 5922 |
-- for Jnn in [reverse] V'Range (Dim) loop |
| 5923 |
-- <core loop> |
| 5924 |
-- end loop; |
| 5925 |
|
| 5926 |
J := Last (Index_List); |
| 5927 |
Dim := Num_Dims; |
| 5928 |
while Present (J) and then Dim > 0 loop |
| 5929 |
Loop_Id := J; |
| 5930 |
Prev (J); |
| 5931 |
Remove (Loop_Id); |
| 5932 |
|
| 5933 |
Core_Loop := |
| 5934 |
Make_Loop_Statement (Loc, |
| 5935 |
Iteration_Scheme => |
| 5936 |
Make_Iteration_Scheme (Loc, |
| 5937 |
Loop_Parameter_Specification => |
| 5938 |
Make_Loop_Parameter_Specification (Loc, |
| 5939 |
Defining_Identifier => Loop_Id, |
| 5940 |
Discrete_Subtype_Definition => |
| 5941 |
Make_Attribute_Reference (Loc, |
| 5942 |
Prefix => Make_Identifier (Loc, Name_V), |
| 5943 |
Attribute_Name => Name_Range, |
| 5944 |
Expressions => New_List ( |
| 5945 |
Make_Integer_Literal (Loc, Dim))), |
| 5946 |
|
| 5947 |
Reverse_Present => |
| 5948 |
Prim = Finalize_Case)), |
| 5949 |
|
| 5950 |
Statements => New_List (Core_Loop), |
| 5951 |
End_Label => Empty); |
| 5952 |
|
| 5953 |
Dim := Dim - 1; |
| 5954 |
end loop; |
| 5955 |
|
| 5956 |
-- Generate the block which contains the core loop, declarations |
| 5957 |
-- of the abort flag, the exception occurrence, the raised flag |
| 5958 |
-- and the conditional raise: |
| 5959 |
|
| 5960 |
-- declare |
| 5961 |
-- Abort : constant Boolean := Triggered_By_Abort; |
| 5962 |
-- <or> |
| 5963 |
-- Abort : constant Boolean := False; -- no abort |
| 5964 |
|
| 5965 |
-- E : Exception_Occurrence; |
| 5966 |
-- Raised : Boolean := False; |
| 5967 |
|
| 5968 |
-- begin |
| 5969 |
-- <core loop> |
| 5970 |
|
| 5971 |
-- if Raised and then not Abort then |
| 5972 |
-- Raise_From_Controlled_Operation (E); |
| 5973 |
-- end if; |
| 5974 |
-- end; |
| 5975 |
|
| 5976 |
Stmts := New_List (Core_Loop); |
| 5977 |
|
| 5978 |
if Exceptions_OK then |
| 5979 |
Append_To (Stmts, Build_Raise_Statement (Final_Data)); |
| 5980 |
end if; |
| 5981 |
|
| 5982 |
Block := |
| 5983 |
Make_Block_Statement (Loc, |
| 5984 |
Declarations => Final_Decls, |
| 5985 |
Handled_Statement_Sequence => |
| 5986 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 5987 |
Statements => Stmts)); |
| 5988 |
|
| 5989 |
-- Otherwise previous errors or a missing full view may prevent the |
| 5990 |
-- proper freezing of the component type. If this is the case, there |
| 5991 |
-- is no [Deep_]Adjust or [Deep_]Finalize primitive to call. |
| 5992 |
|
| 5993 |
else |
| 5994 |
Block := Make_Null_Statement (Loc); |
| 5995 |
end if; |
| 5996 |
|
| 5997 |
return New_List (Block); |
| 5998 |
end Build_Adjust_Or_Finalize_Statements; |
| 5999 |
|
| 6000 |
--------------------------------- |
| 6001 |
-- Build_Initialize_Statements -- |
| 6002 |
--------------------------------- |
| 6003 |
|
| 6004 |
function Build_Initialize_Statements (Typ : Entity_Id) return List_Id is |
| 6005 |
Comp_Typ : constant Entity_Id := Component_Type (Typ); |
| 6006 |
Final_List : constant List_Id := New_List; |
| 6007 |
Index_List : constant List_Id := New_List; |
| 6008 |
Loc : constant Source_Ptr := Sloc (Typ); |
| 6009 |
Num_Dims : constant Int := Number_Dimensions (Typ); |
| 6010 |
|
| 6011 |
function Build_Assignment (Counter_Id : Entity_Id) return Node_Id; |
| 6012 |
-- Generate the following assignment: |
| 6013 |
-- Counter := V'Length (1) * |
| 6014 |
-- ... |
| 6015 |
-- V'Length (N) - Counter; |
| 6016 |
-- |
| 6017 |
-- Counter_Id denotes the entity of the counter. |
| 6018 |
|
| 6019 |
function Build_Finalization_Call return Node_Id; |
| 6020 |
-- Generate a deep finalization call for an array element |
| 6021 |
|
| 6022 |
procedure Build_Indexes; |
| 6023 |
-- Generate the initialization and finalization indexes used in the |
| 6024 |
-- dimension loops. |
| 6025 |
|
| 6026 |
function Build_Initialization_Call return Node_Id; |
| 6027 |
-- Generate a deep initialization call for an array element |
| 6028 |
|
| 6029 |
---------------------- |
| 6030 |
-- Build_Assignment -- |
| 6031 |
---------------------- |
| 6032 |
|
| 6033 |
function Build_Assignment (Counter_Id : Entity_Id) return Node_Id is |
| 6034 |
Dim : Int; |
| 6035 |
Expr : Node_Id; |
| 6036 |
|
| 6037 |
begin |
| 6038 |
-- Start from the first dimension and generate: |
| 6039 |
-- V'Length (1) |
| 6040 |
|
| 6041 |
Dim := 1; |
| 6042 |
Expr := |
| 6043 |
Make_Attribute_Reference (Loc, |
| 6044 |
Prefix => Make_Identifier (Loc, Name_V), |
| 6045 |
Attribute_Name => Name_Length, |
| 6046 |
Expressions => New_List (Make_Integer_Literal (Loc, Dim))); |
| 6047 |
|
| 6048 |
-- Process the rest of the dimensions, generate: |
| 6049 |
-- Expr * V'Length (N) |
| 6050 |
|
| 6051 |
Dim := Dim + 1; |
| 6052 |
while Dim <= Num_Dims loop |
| 6053 |
Expr := |
| 6054 |
Make_Op_Multiply (Loc, |
| 6055 |
Left_Opnd => Expr, |
| 6056 |
Right_Opnd => |
| 6057 |
Make_Attribute_Reference (Loc, |
| 6058 |
Prefix => Make_Identifier (Loc, Name_V), |
| 6059 |
Attribute_Name => Name_Length, |
| 6060 |
Expressions => New_List ( |
| 6061 |
Make_Integer_Literal (Loc, Dim)))); |
| 6062 |
|
| 6063 |
Dim := Dim + 1; |
| 6064 |
end loop; |
| 6065 |
|
| 6066 |
-- Generate: |
| 6067 |
-- Counter := Expr - Counter; |
| 6068 |
|
| 6069 |
return |
| 6070 |
Make_Assignment_Statement (Loc, |
| 6071 |
Name => New_Occurrence_Of (Counter_Id, Loc), |
| 6072 |
Expression => |
| 6073 |
Make_Op_Subtract (Loc, |
| 6074 |
Left_Opnd => Expr, |
| 6075 |
Right_Opnd => New_Occurrence_Of (Counter_Id, Loc))); |
| 6076 |
end Build_Assignment; |
| 6077 |
|
| 6078 |
----------------------------- |
| 6079 |
-- Build_Finalization_Call -- |
| 6080 |
----------------------------- |
| 6081 |
|
| 6082 |
function Build_Finalization_Call return Node_Id is |
| 6083 |
Comp_Ref : constant Node_Id := |
| 6084 |
Make_Indexed_Component (Loc, |
| 6085 |
Prefix => Make_Identifier (Loc, Name_V), |
| 6086 |
Expressions => New_References_To (Final_List, Loc)); |
| 6087 |
|
| 6088 |
begin |
| 6089 |
Set_Etype (Comp_Ref, Comp_Typ); |
| 6090 |
|
| 6091 |
-- Generate: |
| 6092 |
-- [Deep_]Finalize (V); |
| 6093 |
|
| 6094 |
return Make_Final_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ); |
| 6095 |
end Build_Finalization_Call; |
| 6096 |
|
| 6097 |
------------------- |
| 6098 |
-- Build_Indexes -- |
| 6099 |
------------------- |
| 6100 |
|
| 6101 |
procedure Build_Indexes is |
| 6102 |
begin |
| 6103 |
-- Generate the following identifiers: |
| 6104 |
-- Jnn - for initialization |
| 6105 |
-- Fnn - for finalization |
| 6106 |
|
| 6107 |
for Dim in 1 .. Num_Dims loop |
| 6108 |
Append_To (Index_List, |
| 6109 |
Make_Defining_Identifier (Loc, New_External_Name ('J', Dim))); |
| 6110 |
|
| 6111 |
Append_To (Final_List, |
| 6112 |
Make_Defining_Identifier (Loc, New_External_Name ('F', Dim))); |
| 6113 |
end loop; |
| 6114 |
end Build_Indexes; |
| 6115 |
|
| 6116 |
------------------------------- |
| 6117 |
-- Build_Initialization_Call -- |
| 6118 |
------------------------------- |
| 6119 |
|
| 6120 |
function Build_Initialization_Call return Node_Id is |
| 6121 |
Comp_Ref : constant Node_Id := |
| 6122 |
Make_Indexed_Component (Loc, |
| 6123 |
Prefix => Make_Identifier (Loc, Name_V), |
| 6124 |
Expressions => New_References_To (Index_List, Loc)); |
| 6125 |
|
| 6126 |
begin |
| 6127 |
Set_Etype (Comp_Ref, Comp_Typ); |
| 6128 |
|
| 6129 |
-- Generate: |
| 6130 |
-- [Deep_]Initialize (V (J1, ..., JN)); |
| 6131 |
|
| 6132 |
return Make_Init_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ); |
| 6133 |
end Build_Initialization_Call; |
| 6134 |
|
| 6135 |
-- Local variables |
| 6136 |
|
| 6137 |
Counter_Id : Entity_Id; |
| 6138 |
Dim : Int; |
| 6139 |
F : Node_Id; |
| 6140 |
Fin_Stmt : Node_Id; |
| 6141 |
Final_Block : Node_Id; |
| 6142 |
Final_Data : Finalization_Exception_Data; |
| 6143 |
Final_Decls : List_Id := No_List; |
| 6144 |
Final_Loop : Node_Id; |
| 6145 |
Init_Block : Node_Id; |
| 6146 |
Init_Call : Node_Id; |
| 6147 |
Init_Loop : Node_Id; |
| 6148 |
J : Node_Id; |
| 6149 |
Loop_Id : Node_Id; |
| 6150 |
Stmts : List_Id; |
| 6151 |
|
| 6152 |
-- Start of processing for Build_Initialize_Statements |
| 6153 |
|
| 6154 |
begin |
| 6155 |
Counter_Id := Make_Temporary (Loc, 'C'); |
| 6156 |
Final_Decls := New_List; |
| 6157 |
|
| 6158 |
Build_Indexes; |
| 6159 |
Build_Object_Declarations (Final_Data, Final_Decls, Loc); |
| 6160 |
|
| 6161 |
-- Generate the block which houses the finalization call, the index |
| 6162 |
-- guard and the handler which triggers Program_Error later on. |
| 6163 |
|
| 6164 |
-- if Counter > 0 then |
| 6165 |
-- Counter := Counter - 1; |
| 6166 |
-- else |
| 6167 |
-- begin |
| 6168 |
-- [Deep_]Finalize (V (F1, ..., FN)); |
| 6169 |
-- exception |
| 6170 |
-- when others => |
| 6171 |
-- if not Raised then |
| 6172 |
-- Raised := True; |
| 6173 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6174 |
-- end if; |
| 6175 |
-- end; |
| 6176 |
-- end if; |
| 6177 |
|
| 6178 |
Fin_Stmt := Build_Finalization_Call; |
| 6179 |
|
| 6180 |
if Present (Fin_Stmt) then |
| 6181 |
if Exceptions_OK then |
| 6182 |
Fin_Stmt := |
| 6183 |
Make_Block_Statement (Loc, |
| 6184 |
Handled_Statement_Sequence => |
| 6185 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 6186 |
Statements => New_List (Fin_Stmt), |
| 6187 |
Exception_Handlers => New_List ( |
| 6188 |
Build_Exception_Handler (Final_Data)))); |
| 6189 |
end if; |
| 6190 |
|
| 6191 |
-- This is the core of the loop, the dimension iterators are added |
| 6192 |
-- one by one in reverse. |
| 6193 |
|
| 6194 |
Final_Loop := |
| 6195 |
Make_If_Statement (Loc, |
| 6196 |
Condition => |
| 6197 |
Make_Op_Gt (Loc, |
| 6198 |
Left_Opnd => New_Occurrence_Of (Counter_Id, Loc), |
| 6199 |
Right_Opnd => Make_Integer_Literal (Loc, 0)), |
| 6200 |
|
| 6201 |
Then_Statements => New_List ( |
| 6202 |
Make_Assignment_Statement (Loc, |
| 6203 |
Name => New_Occurrence_Of (Counter_Id, Loc), |
| 6204 |
Expression => |
| 6205 |
Make_Op_Subtract (Loc, |
| 6206 |
Left_Opnd => New_Occurrence_Of (Counter_Id, Loc), |
| 6207 |
Right_Opnd => Make_Integer_Literal (Loc, 1)))), |
| 6208 |
|
| 6209 |
Else_Statements => New_List (Fin_Stmt)); |
| 6210 |
|
| 6211 |
-- Generate all finalization loops starting from the innermost |
| 6212 |
-- dimension. |
| 6213 |
|
| 6214 |
-- for Fnn in reverse V'Range (Dim) loop |
| 6215 |
-- <final loop> |
| 6216 |
-- end loop; |
| 6217 |
|
| 6218 |
F := Last (Final_List); |
| 6219 |
Dim := Num_Dims; |
| 6220 |
while Present (F) and then Dim > 0 loop |
| 6221 |
Loop_Id := F; |
| 6222 |
Prev (F); |
| 6223 |
Remove (Loop_Id); |
| 6224 |
|
| 6225 |
Final_Loop := |
| 6226 |
Make_Loop_Statement (Loc, |
| 6227 |
Iteration_Scheme => |
| 6228 |
Make_Iteration_Scheme (Loc, |
| 6229 |
Loop_Parameter_Specification => |
| 6230 |
Make_Loop_Parameter_Specification (Loc, |
| 6231 |
Defining_Identifier => Loop_Id, |
| 6232 |
Discrete_Subtype_Definition => |
| 6233 |
Make_Attribute_Reference (Loc, |
| 6234 |
Prefix => Make_Identifier (Loc, Name_V), |
| 6235 |
Attribute_Name => Name_Range, |
| 6236 |
Expressions => New_List ( |
| 6237 |
Make_Integer_Literal (Loc, Dim))), |
| 6238 |
|
| 6239 |
Reverse_Present => True)), |
| 6240 |
|
| 6241 |
Statements => New_List (Final_Loop), |
| 6242 |
End_Label => Empty); |
| 6243 |
|
| 6244 |
Dim := Dim - 1; |
| 6245 |
end loop; |
| 6246 |
|
| 6247 |
-- Generate the block which contains the finalization loops, the |
| 6248 |
-- declarations of the abort flag, the exception occurrence, the |
| 6249 |
-- raised flag and the conditional raise. |
| 6250 |
|
| 6251 |
-- declare |
| 6252 |
-- Abort : constant Boolean := Triggered_By_Abort; |
| 6253 |
-- <or> |
| 6254 |
-- Abort : constant Boolean := False; -- no abort |
| 6255 |
|
| 6256 |
-- E : Exception_Occurrence; |
| 6257 |
-- Raised : Boolean := False; |
| 6258 |
|
| 6259 |
-- begin |
| 6260 |
-- Counter := |
| 6261 |
-- V'Length (1) * |
| 6262 |
-- ... |
| 6263 |
-- V'Length (N) - Counter; |
| 6264 |
|
| 6265 |
-- <final loop> |
| 6266 |
|
| 6267 |
-- if Raised and then not Abort then |
| 6268 |
-- Raise_From_Controlled_Operation (E); |
| 6269 |
-- end if; |
| 6270 |
|
| 6271 |
-- raise; |
| 6272 |
-- end; |
| 6273 |
|
| 6274 |
Stmts := New_List (Build_Assignment (Counter_Id), Final_Loop); |
| 6275 |
|
| 6276 |
if Exceptions_OK then |
| 6277 |
Append_To (Stmts, Build_Raise_Statement (Final_Data)); |
| 6278 |
Append_To (Stmts, Make_Raise_Statement (Loc)); |
| 6279 |
end if; |
| 6280 |
|
| 6281 |
Final_Block := |
| 6282 |
Make_Block_Statement (Loc, |
| 6283 |
Declarations => Final_Decls, |
| 6284 |
Handled_Statement_Sequence => |
| 6285 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 6286 |
Statements => Stmts)); |
| 6287 |
|
| 6288 |
-- Otherwise previous errors or a missing full view may prevent the |
| 6289 |
-- proper freezing of the component type. If this is the case, there |
| 6290 |
-- is no [Deep_]Finalize primitive to call. |
| 6291 |
|
| 6292 |
else |
| 6293 |
Final_Block := Make_Null_Statement (Loc); |
| 6294 |
end if; |
| 6295 |
|
| 6296 |
-- Generate the block which contains the initialization call and |
| 6297 |
-- the partial finalization code. |
| 6298 |
|
| 6299 |
-- begin |
| 6300 |
-- [Deep_]Initialize (V (J1, ..., JN)); |
| 6301 |
|
| 6302 |
-- Counter := Counter + 1; |
| 6303 |
|
| 6304 |
-- exception |
| 6305 |
-- when others => |
| 6306 |
-- <finalization code> |
| 6307 |
-- end; |
| 6308 |
|
| 6309 |
Init_Call := Build_Initialization_Call; |
| 6310 |
|
| 6311 |
-- Only create finalization block if there is a nontrivial call |
| 6312 |
-- to initialization or a Default_Initial_Condition check to be |
| 6313 |
-- performed. |
| 6314 |
|
| 6315 |
if (Present (Init_Call) |
| 6316 |
and then Nkind (Init_Call) /= N_Null_Statement) |
| 6317 |
or else |
| 6318 |
(Has_DIC (Comp_Typ) |
| 6319 |
and then not GNATprove_Mode |
| 6320 |
and then Present (DIC_Procedure (Comp_Typ)) |
| 6321 |
and then not Has_Null_Body (DIC_Procedure (Comp_Typ))) |
| 6322 |
then |
| 6323 |
declare |
| 6324 |
Init_Stmts : constant List_Id := New_List; |
| 6325 |
|
| 6326 |
begin |
| 6327 |
if Present (Init_Call) then |
| 6328 |
Append_To (Init_Stmts, Init_Call); |
| 6329 |
end if; |
| 6330 |
|
| 6331 |
if Has_DIC (Comp_Typ) |
| 6332 |
and then Present (DIC_Procedure (Comp_Typ)) |
| 6333 |
then |
| 6334 |
Append_To |
| 6335 |
(Init_Stmts, |
| 6336 |
Build_DIC_Call (Loc, |
| 6337 |
Make_Indexed_Component (Loc, |
| 6338 |
Prefix => Make_Identifier (Loc, Name_V), |
| 6339 |
Expressions => New_References_To (Index_List, Loc)), |
| 6340 |
Comp_Typ)); |
| 6341 |
end if; |
| 6342 |
|
| 6343 |
Init_Loop := |
| 6344 |
Make_Block_Statement (Loc, |
| 6345 |
Handled_Statement_Sequence => |
| 6346 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 6347 |
Statements => Init_Stmts, |
| 6348 |
Exception_Handlers => New_List ( |
| 6349 |
Make_Exception_Handler (Loc, |
| 6350 |
Exception_Choices => New_List ( |
| 6351 |
Make_Others_Choice (Loc)), |
| 6352 |
Statements => New_List (Final_Block))))); |
| 6353 |
end; |
| 6354 |
|
| 6355 |
Append_To (Statements (Handled_Statement_Sequence (Init_Loop)), |
| 6356 |
Make_Assignment_Statement (Loc, |
| 6357 |
Name => New_Occurrence_Of (Counter_Id, Loc), |
| 6358 |
Expression => |
| 6359 |
Make_Op_Add (Loc, |
| 6360 |
Left_Opnd => New_Occurrence_Of (Counter_Id, Loc), |
| 6361 |
Right_Opnd => Make_Integer_Literal (Loc, 1)))); |
| 6362 |
|
| 6363 |
-- Generate all initialization loops starting from the innermost |
| 6364 |
-- dimension. |
| 6365 |
|
| 6366 |
-- for Jnn in V'Range (Dim) loop |
| 6367 |
-- <init loop> |
| 6368 |
-- end loop; |
| 6369 |
|
| 6370 |
J := Last (Index_List); |
| 6371 |
Dim := Num_Dims; |
| 6372 |
while Present (J) and then Dim > 0 loop |
| 6373 |
Loop_Id := J; |
| 6374 |
Prev (J); |
| 6375 |
Remove (Loop_Id); |
| 6376 |
|
| 6377 |
Init_Loop := |
| 6378 |
Make_Loop_Statement (Loc, |
| 6379 |
Iteration_Scheme => |
| 6380 |
Make_Iteration_Scheme (Loc, |
| 6381 |
Loop_Parameter_Specification => |
| 6382 |
Make_Loop_Parameter_Specification (Loc, |
| 6383 |
Defining_Identifier => Loop_Id, |
| 6384 |
Discrete_Subtype_Definition => |
| 6385 |
Make_Attribute_Reference (Loc, |
| 6386 |
Prefix => Make_Identifier (Loc, Name_V), |
| 6387 |
Attribute_Name => Name_Range, |
| 6388 |
Expressions => New_List ( |
| 6389 |
Make_Integer_Literal (Loc, Dim))))), |
| 6390 |
|
| 6391 |
Statements => New_List (Init_Loop), |
| 6392 |
End_Label => Empty); |
| 6393 |
|
| 6394 |
Dim := Dim - 1; |
| 6395 |
end loop; |
| 6396 |
|
| 6397 |
-- Generate the block which contains the counter variable and the |
| 6398 |
-- initialization loops. |
| 6399 |
|
| 6400 |
-- declare |
| 6401 |
-- Counter : Integer := 0; |
| 6402 |
-- begin |
| 6403 |
-- <init loop> |
| 6404 |
-- end; |
| 6405 |
|
| 6406 |
Init_Block := |
| 6407 |
Make_Block_Statement (Loc, |
| 6408 |
Declarations => New_List ( |
| 6409 |
Make_Object_Declaration (Loc, |
| 6410 |
Defining_Identifier => Counter_Id, |
| 6411 |
Object_Definition => |
| 6412 |
New_Occurrence_Of (Standard_Integer, Loc), |
| 6413 |
Expression => Make_Integer_Literal (Loc, 0))), |
| 6414 |
|
| 6415 |
Handled_Statement_Sequence => |
| 6416 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 6417 |
Statements => New_List (Init_Loop))); |
| 6418 |
|
| 6419 |
if Debug_Generated_Code then |
| 6420 |
Set_Debug_Info_Needed (Counter_Id); |
| 6421 |
end if; |
| 6422 |
|
| 6423 |
-- Otherwise previous errors or a missing full view may prevent the |
| 6424 |
-- proper freezing of the component type. If this is the case, there |
| 6425 |
-- is no [Deep_]Initialize primitive to call. |
| 6426 |
|
| 6427 |
else |
| 6428 |
Init_Block := Make_Null_Statement (Loc); |
| 6429 |
end if; |
| 6430 |
|
| 6431 |
return New_List (Init_Block); |
| 6432 |
end Build_Initialize_Statements; |
| 6433 |
|
| 6434 |
----------------------- |
| 6435 |
-- New_References_To -- |
| 6436 |
----------------------- |
| 6437 |
|
| 6438 |
function New_References_To |
| 6439 |
(L : List_Id; |
| 6440 |
Loc : Source_Ptr) return List_Id |
| 6441 |
is |
| 6442 |
Refs : constant List_Id := New_List; |
| 6443 |
Id : Node_Id; |
| 6444 |
|
| 6445 |
begin |
| 6446 |
Id := First (L); |
| 6447 |
while Present (Id) loop |
| 6448 |
Append_To (Refs, New_Occurrence_Of (Id, Loc)); |
| 6449 |
Next (Id); |
| 6450 |
end loop; |
| 6451 |
|
| 6452 |
return Refs; |
| 6453 |
end New_References_To; |
| 6454 |
|
| 6455 |
-- Start of processing for Make_Deep_Array_Body |
| 6456 |
|
| 6457 |
begin |
| 6458 |
case Prim is |
| 6459 |
when Address_Case => |
| 6460 |
return Make_Finalize_Address_Stmts (Typ); |
| 6461 |
|
| 6462 |
when Adjust_Case |
| 6463 |
| Finalize_Case |
| 6464 |
=> |
| 6465 |
return Build_Adjust_Or_Finalize_Statements (Typ); |
| 6466 |
|
| 6467 |
when Initialize_Case => |
| 6468 |
return Build_Initialize_Statements (Typ); |
| 6469 |
end case; |
| 6470 |
end Make_Deep_Array_Body; |
| 6471 |
|
| 6472 |
-------------------- |
| 6473 |
-- Make_Deep_Proc -- |
| 6474 |
-------------------- |
| 6475 |
|
| 6476 |
function Make_Deep_Proc |
| 6477 |
(Prim : Final_Primitives; |
| 6478 |
Typ : Entity_Id; |
| 6479 |
Stmts : List_Id) return Entity_Id |
| 6480 |
is |
| 6481 |
Loc : constant Source_Ptr := Sloc (Typ); |
| 6482 |
Formals : List_Id; |
| 6483 |
Proc_Id : Entity_Id; |
| 6484 |
|
| 6485 |
begin |
| 6486 |
-- Create the object formal, generate: |
| 6487 |
-- V : System.Address |
| 6488 |
|
| 6489 |
if Prim = Address_Case then |
| 6490 |
Formals := New_List ( |
| 6491 |
Make_Parameter_Specification (Loc, |
| 6492 |
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V), |
| 6493 |
Parameter_Type => |
| 6494 |
New_Occurrence_Of (RTE (RE_Address), Loc))); |
| 6495 |
|
| 6496 |
-- Default case |
| 6497 |
|
| 6498 |
else |
| 6499 |
-- V : in out Typ |
| 6500 |
|
| 6501 |
Formals := New_List ( |
| 6502 |
Make_Parameter_Specification (Loc, |
| 6503 |
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V), |
| 6504 |
In_Present => True, |
| 6505 |
Out_Present => True, |
| 6506 |
Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
| 6507 |
|
| 6508 |
-- F : Boolean := True |
| 6509 |
|
| 6510 |
if Prim = Adjust_Case |
| 6511 |
or else Prim = Finalize_Case |
| 6512 |
then |
| 6513 |
Append_To (Formals, |
| 6514 |
Make_Parameter_Specification (Loc, |
| 6515 |
Defining_Identifier => Make_Defining_Identifier (Loc, Name_F), |
| 6516 |
Parameter_Type => |
| 6517 |
New_Occurrence_Of (Standard_Boolean, Loc), |
| 6518 |
Expression => |
| 6519 |
New_Occurrence_Of (Standard_True, Loc))); |
| 6520 |
end if; |
| 6521 |
end if; |
| 6522 |
|
| 6523 |
Proc_Id := |
| 6524 |
Make_Defining_Identifier (Loc, |
| 6525 |
Chars => Make_TSS_Name (Typ, Deep_Name_Of (Prim))); |
| 6526 |
|
| 6527 |
-- Generate: |
| 6528 |
-- procedure Deep_Initialize / Adjust / Finalize (V : in out <typ>) is |
| 6529 |
-- begin |
| 6530 |
-- <stmts> |
| 6531 |
-- exception -- Finalize and Adjust cases only |
| 6532 |
-- raise Program_Error; |
| 6533 |
-- end Deep_Initialize / Adjust / Finalize; |
| 6534 |
|
| 6535 |
-- or |
| 6536 |
|
| 6537 |
-- procedure Finalize_Address (V : System.Address) is |
| 6538 |
-- begin |
| 6539 |
-- <stmts> |
| 6540 |
-- end Finalize_Address; |
| 6541 |
|
| 6542 |
Discard_Node ( |
| 6543 |
Make_Subprogram_Body (Loc, |
| 6544 |
Specification => |
| 6545 |
Make_Procedure_Specification (Loc, |
| 6546 |
Defining_Unit_Name => Proc_Id, |
| 6547 |
Parameter_Specifications => Formals), |
| 6548 |
|
| 6549 |
Declarations => Empty_List, |
| 6550 |
|
| 6551 |
Handled_Statement_Sequence => |
| 6552 |
Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts))); |
| 6553 |
|
| 6554 |
-- If there are no calls to component initialization, indicate that |
| 6555 |
-- the procedure is trivial, so prevent calls to it. |
| 6556 |
|
| 6557 |
if Is_Empty_List (Stmts) |
| 6558 |
or else Nkind (First (Stmts)) = N_Null_Statement |
| 6559 |
then |
| 6560 |
Set_Is_Trivial_Subprogram (Proc_Id); |
| 6561 |
end if; |
| 6562 |
|
| 6563 |
return Proc_Id; |
| 6564 |
end Make_Deep_Proc; |
| 6565 |
|
| 6566 |
--------------------------- |
| 6567 |
-- Make_Deep_Record_Body -- |
| 6568 |
--------------------------- |
| 6569 |
|
| 6570 |
function Make_Deep_Record_Body |
| 6571 |
(Prim : Final_Primitives; |
| 6572 |
Typ : Entity_Id; |
| 6573 |
Is_Local : Boolean := False) return List_Id |
| 6574 |
is |
| 6575 |
Loc : constant Source_Ptr := Sloc (Typ); |
| 6576 |
|
| 6577 |
function Build_Adjust_Statements (Typ : Entity_Id) return List_Id; |
| 6578 |
-- Build the statements necessary to adjust a record type. The type may |
| 6579 |
-- have discriminants and contain variant parts. Generate: |
| 6580 |
-- |
| 6581 |
-- begin |
| 6582 |
-- begin |
| 6583 |
-- [Deep_]Adjust (V.Comp_1); |
| 6584 |
-- exception |
| 6585 |
-- when Id : others => |
| 6586 |
-- if not Raised then |
| 6587 |
-- Raised := True; |
| 6588 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6589 |
-- end if; |
| 6590 |
-- end; |
| 6591 |
-- . . . |
| 6592 |
-- begin |
| 6593 |
-- [Deep_]Adjust (V.Comp_N); |
| 6594 |
-- exception |
| 6595 |
-- when Id : others => |
| 6596 |
-- if not Raised then |
| 6597 |
-- Raised := True; |
| 6598 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6599 |
-- end if; |
| 6600 |
-- end; |
| 6601 |
-- |
| 6602 |
-- begin |
| 6603 |
-- Deep_Adjust (V._parent, False); -- If applicable |
| 6604 |
-- exception |
| 6605 |
-- when Id : others => |
| 6606 |
-- if not Raised then |
| 6607 |
-- Raised := True; |
| 6608 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6609 |
-- end if; |
| 6610 |
-- end; |
| 6611 |
-- |
| 6612 |
-- if F then |
| 6613 |
-- begin |
| 6614 |
-- Adjust (V); -- If applicable |
| 6615 |
-- exception |
| 6616 |
-- when others => |
| 6617 |
-- if not Raised then |
| 6618 |
-- Raised := True; |
| 6619 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6620 |
-- end if; |
| 6621 |
-- end; |
| 6622 |
-- end if; |
| 6623 |
-- |
| 6624 |
-- if Raised and then not Abort then |
| 6625 |
-- Raise_From_Controlled_Operation (E); |
| 6626 |
-- end if; |
| 6627 |
-- end; |
| 6628 |
|
| 6629 |
function Build_Finalize_Statements (Typ : Entity_Id) return List_Id; |
| 6630 |
-- Build the statements necessary to finalize a record type. The type |
| 6631 |
-- may have discriminants and contain variant parts. Generate: |
| 6632 |
-- |
| 6633 |
-- declare |
| 6634 |
-- Abort : constant Boolean := Triggered_By_Abort; |
| 6635 |
-- <or> |
| 6636 |
-- Abort : constant Boolean := False; -- no abort |
| 6637 |
-- E : Exception_Occurrence; |
| 6638 |
-- Raised : Boolean := False; |
| 6639 |
-- |
| 6640 |
-- begin |
| 6641 |
-- if F then |
| 6642 |
-- begin |
| 6643 |
-- Finalize (V); -- If applicable |
| 6644 |
-- exception |
| 6645 |
-- when others => |
| 6646 |
-- if not Raised then |
| 6647 |
-- Raised := True; |
| 6648 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6649 |
-- end if; |
| 6650 |
-- end; |
| 6651 |
-- end if; |
| 6652 |
-- |
| 6653 |
-- case Variant_1 is |
| 6654 |
-- when Value_1 => |
| 6655 |
-- case State_Counter_N => -- If Is_Local is enabled |
| 6656 |
-- when N => . |
| 6657 |
-- goto LN; . |
| 6658 |
-- ... . |
| 6659 |
-- when 1 => . |
| 6660 |
-- goto L1; . |
| 6661 |
-- when others => . |
| 6662 |
-- goto L0; . |
| 6663 |
-- end case; . |
| 6664 |
-- |
| 6665 |
-- <<LN>> -- If Is_Local is enabled |
| 6666 |
-- begin |
| 6667 |
-- [Deep_]Finalize (V.Comp_N); |
| 6668 |
-- exception |
| 6669 |
-- when others => |
| 6670 |
-- if not Raised then |
| 6671 |
-- Raised := True; |
| 6672 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6673 |
-- end if; |
| 6674 |
-- end; |
| 6675 |
-- . . . |
| 6676 |
-- <<L1>> |
| 6677 |
-- begin |
| 6678 |
-- [Deep_]Finalize (V.Comp_1); |
| 6679 |
-- exception |
| 6680 |
-- when others => |
| 6681 |
-- if not Raised then |
| 6682 |
-- Raised := True; |
| 6683 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6684 |
-- end if; |
| 6685 |
-- end; |
| 6686 |
-- <<L0>> |
| 6687 |
-- end case; |
| 6688 |
-- |
| 6689 |
-- case State_Counter_1 => -- If Is_Local is enabled |
| 6690 |
-- when M => . |
| 6691 |
-- goto LM; . |
| 6692 |
-- ... |
| 6693 |
-- |
| 6694 |
-- begin |
| 6695 |
-- Deep_Finalize (V._parent, False); -- If applicable |
| 6696 |
-- exception |
| 6697 |
-- when Id : others => |
| 6698 |
-- if not Raised then |
| 6699 |
-- Raised := True; |
| 6700 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6701 |
-- end if; |
| 6702 |
-- end; |
| 6703 |
-- |
| 6704 |
-- if Raised and then not Abort then |
| 6705 |
-- Raise_From_Controlled_Operation (E); |
| 6706 |
-- end if; |
| 6707 |
-- end; |
| 6708 |
|
| 6709 |
function Parent_Field_Type (Typ : Entity_Id) return Entity_Id; |
| 6710 |
-- Given a derived tagged type Typ, traverse all components, find field |
| 6711 |
-- _parent and return its type. |
| 6712 |
|
| 6713 |
procedure Preprocess_Components |
| 6714 |
(Comps : Node_Id; |
| 6715 |
Num_Comps : out Nat; |
| 6716 |
Has_POC : out Boolean); |
| 6717 |
-- Examine all components in component list Comps, count all controlled |
| 6718 |
-- components and determine whether at least one of them is per-object |
| 6719 |
-- constrained. Component _parent is always skipped. |
| 6720 |
|
| 6721 |
----------------------------- |
| 6722 |
-- Build_Adjust_Statements -- |
| 6723 |
----------------------------- |
| 6724 |
|
| 6725 |
function Build_Adjust_Statements (Typ : Entity_Id) return List_Id is |
| 6726 |
Typ_Def : constant Node_Id := Type_Definition (Parent (Typ)); |
| 6727 |
|
| 6728 |
Finalizer_Data : Finalization_Exception_Data; |
| 6729 |
|
| 6730 |
function Process_Component_List_For_Adjust |
| 6731 |
(Comps : Node_Id) return List_Id; |
| 6732 |
-- Build all necessary adjust statements for a single component list |
| 6733 |
|
| 6734 |
--------------------------------------- |
| 6735 |
-- Process_Component_List_For_Adjust -- |
| 6736 |
--------------------------------------- |
| 6737 |
|
| 6738 |
function Process_Component_List_For_Adjust |
| 6739 |
(Comps : Node_Id) return List_Id |
| 6740 |
is |
| 6741 |
Stmts : constant List_Id := New_List; |
| 6742 |
|
| 6743 |
procedure Process_Component_For_Adjust (Decl : Node_Id); |
| 6744 |
-- Process the declaration of a single controlled component |
| 6745 |
|
| 6746 |
---------------------------------- |
| 6747 |
-- Process_Component_For_Adjust -- |
| 6748 |
---------------------------------- |
| 6749 |
|
| 6750 |
procedure Process_Component_For_Adjust (Decl : Node_Id) is |
| 6751 |
Id : constant Entity_Id := Defining_Identifier (Decl); |
| 6752 |
Typ : constant Entity_Id := Etype (Id); |
| 6753 |
|
| 6754 |
Adj_Call : Node_Id; |
| 6755 |
|
| 6756 |
begin |
| 6757 |
-- begin |
| 6758 |
-- [Deep_]Adjust (V.Id); |
| 6759 |
|
| 6760 |
-- exception |
| 6761 |
-- when others => |
| 6762 |
-- if not Raised then |
| 6763 |
-- Raised := True; |
| 6764 |
-- Save_Occurrence (E, Get_Current_Excep.all.all); |
| 6765 |
-- end if; |
| 6766 |
-- end; |
| 6767 |
|
| 6768 |
Adj_Call := |
| 6769 |
Make_Adjust_Call ( |
| 6770 |
Obj_Ref => |
| 6771 |
Make_Selected_Component (Loc, |
| 6772 |
Prefix => Make_Identifier (Loc, Name_V), |
| 6773 |
Selector_Name => Make_Identifier (Loc, Chars (Id))), |
| 6774 |
Typ => Typ); |
| 6775 |
|
| 6776 |
-- Guard against a missing [Deep_]Adjust when the component |
| 6777 |
-- type was not properly frozen. |
| 6778 |
|
| 6779 |
if Present (Adj_Call) then |
| 6780 |
if Exceptions_OK then |
| 6781 |
Adj_Call := |
| 6782 |
Make_Block_Statement (Loc, |
| 6783 |
Handled_Statement_Sequence => |
| 6784 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 6785 |
Statements => New_List (Adj_Call), |
| 6786 |
Exception_Handlers => New_List ( |
| 6787 |
Build_Exception_Handler (Finalizer_Data)))); |
| 6788 |
end if; |
| 6789 |
|
| 6790 |
Append_To (Stmts, Adj_Call); |
| 6791 |
end if; |
| 6792 |
end Process_Component_For_Adjust; |
| 6793 |
|
| 6794 |
-- Local variables |
| 6795 |
|
| 6796 |
Decl : Node_Id; |
| 6797 |
Decl_Id : Entity_Id; |
| 6798 |
Decl_Typ : Entity_Id; |
| 6799 |
Has_POC : Boolean; |
| 6800 |
Num_Comps : Nat; |
| 6801 |
Var_Case : Node_Id; |
| 6802 |
|
| 6803 |
-- Start of processing for Process_Component_List_For_Adjust |
| 6804 |
|
| 6805 |
begin |
| 6806 |
-- Perform an initial check, determine the number of controlled |
| 6807 |
-- components in the current list and whether at least one of them |
| 6808 |
-- is per-object constrained. |
| 6809 |
|
| 6810 |
Preprocess_Components (Comps, Num_Comps, Has_POC); |
| 6811 |
|
| 6812 |
-- The processing in this routine is done in the following order: |
| 6813 |
-- 1) Regular components |
| 6814 |
-- 2) Per-object constrained components |
| 6815 |
-- 3) Variant parts |
| 6816 |
|
| 6817 |
if Num_Comps > 0 then |
| 6818 |
|
| 6819 |
-- Process all regular components in order of declarations |
| 6820 |
|
| 6821 |
Decl := First_Non_Pragma (Component_Items (Comps)); |
| 6822 |
while Present (Decl) loop |
| 6823 |
Decl_Id := Defining_Identifier (Decl); |
| 6824 |
Decl_Typ := Etype (Decl_Id); |
| 6825 |
|
| 6826 |
-- Skip _parent as well as per-object constrained components |
| 6827 |
|
| 6828 |
if Chars (Decl_Id) /= Name_uParent |
| 6829 |
and then Needs_Finalization (Decl_Typ) |
| 6830 |
then |
| 6831 |
if Has_Access_Constraint (Decl_Id) |
| 6832 |
and then No (Expression (Decl)) |
| 6833 |
then |
| 6834 |
null; |
| 6835 |
else |
| 6836 |
Process_Component_For_Adjust (Decl); |
| 6837 |
end if; |
| 6838 |
end if; |
| 6839 |
|
| 6840 |
Next_Non_Pragma (Decl); |
| 6841 |
end loop; |
| 6842 |
|
| 6843 |
-- Process all per-object constrained components in order of |
| 6844 |
-- declarations. |
| 6845 |
|
| 6846 |
if Has_POC then |
| 6847 |
Decl := First_Non_Pragma (Component_Items (Comps)); |
| 6848 |
while Present (Decl) loop |
| 6849 |
Decl_Id := Defining_Identifier (Decl); |
| 6850 |
Decl_Typ := Etype (Decl_Id); |
| 6851 |
|
| 6852 |
-- Skip _parent |
| 6853 |
|
| 6854 |
if Chars (Decl_Id) /= Name_uParent |
| 6855 |
and then Needs_Finalization (Decl_Typ) |
| 6856 |
and then Has_Access_Constraint (Decl_Id) |
| 6857 |
and then No (Expression (Decl)) |
| 6858 |
then |
| 6859 |
Process_Component_For_Adjust (Decl); |
| 6860 |
end if; |
| 6861 |
|
| 6862 |
Next_Non_Pragma (Decl); |
| 6863 |
end loop; |
| 6864 |
end if; |
| 6865 |
end if; |
| 6866 |
|
| 6867 |
-- Process all variants, if any |
| 6868 |
|
| 6869 |
Var_Case := Empty; |
| 6870 |
if Present (Variant_Part (Comps)) then |
| 6871 |
declare |
| 6872 |
Var_Alts : constant List_Id := New_List; |
| 6873 |
Var : Node_Id; |
| 6874 |
|
| 6875 |
begin |
| 6876 |
Var := First_Non_Pragma (Variants (Variant_Part (Comps))); |
| 6877 |
while Present (Var) loop |
| 6878 |
|
| 6879 |
-- Generate: |
| 6880 |
-- when <discrete choices> => |
| 6881 |
-- <adjust statements> |
| 6882 |
|
| 6883 |
Append_To (Var_Alts, |
| 6884 |
Make_Case_Statement_Alternative (Loc, |
| 6885 |
Discrete_Choices => |
| 6886 |
New_Copy_List (Discrete_Choices (Var)), |
| 6887 |
Statements => |
| 6888 |
Process_Component_List_For_Adjust ( |
| 6889 |
Component_List (Var)))); |
| 6890 |
|
| 6891 |
Next_Non_Pragma (Var); |
| 6892 |
end loop; |
| 6893 |
|
| 6894 |
-- Generate: |
| 6895 |
-- case V.<discriminant> is |
| 6896 |
-- when <discrete choices 1> => |
| 6897 |
-- <adjust statements 1> |
| 6898 |
-- ... |
| 6899 |
-- when <discrete choices N> => |
| 6900 |
-- <adjust statements N> |
| 6901 |
-- end case; |
| 6902 |
|
| 6903 |
Var_Case := |
| 6904 |
Make_Case_Statement (Loc, |
| 6905 |
Expression => |
| 6906 |
Make_Selected_Component (Loc, |
| 6907 |
Prefix => Make_Identifier (Loc, Name_V), |
| 6908 |
Selector_Name => |
| 6909 |
Make_Identifier (Loc, |
| 6910 |
Chars => Chars (Name (Variant_Part (Comps))))), |
| 6911 |
Alternatives => Var_Alts); |
| 6912 |
end; |
| 6913 |
end if; |
| 6914 |
|
| 6915 |
-- Add the variant case statement to the list of statements |
| 6916 |
|
| 6917 |
if Present (Var_Case) then |
| 6918 |
Append_To (Stmts, Var_Case); |
| 6919 |
end if; |
| 6920 |
|
| 6921 |
-- If the component list did not have any controlled components |
| 6922 |
-- nor variants, return null. |
| 6923 |
|
| 6924 |
if Is_Empty_List (Stmts) then |
| 6925 |
Append_To (Stmts, Make_Null_Statement (Loc)); |
| 6926 |
end if; |
| 6927 |
|
| 6928 |
return Stmts; |
| 6929 |
end Process_Component_List_For_Adjust; |
| 6930 |
|
| 6931 |
-- Local variables |
| 6932 |
|
| 6933 |
Bod_Stmts : List_Id := No_List; |
| 6934 |
Finalizer_Decls : List_Id := No_List; |
| 6935 |
Rec_Def : Node_Id; |
| 6936 |
|
| 6937 |
-- Start of processing for Build_Adjust_Statements |
| 6938 |
|
| 6939 |
begin |
| 6940 |
Finalizer_Decls := New_List; |
| 6941 |
Build_Object_Declarations (Finalizer_Data, Finalizer_Decls, Loc); |
| 6942 |
|
| 6943 |
if Nkind (Typ_Def) = N_Derived_Type_Definition then |
| 6944 |
Rec_Def := Record_Extension_Part (Typ_Def); |
| 6945 |
else |
| 6946 |
Rec_Def := Typ_Def; |
| 6947 |
end if; |
| 6948 |
|
| 6949 |
-- Create an adjust sequence for all record components |
| 6950 |
|
| 6951 |
if Present (Component_List (Rec_Def)) then |
| 6952 |
Bod_Stmts := |
| 6953 |
Process_Component_List_For_Adjust (Component_List (Rec_Def)); |
| 6954 |
end if; |
| 6955 |
|
| 6956 |
-- A derived record type must adjust all inherited components. This |
| 6957 |
-- action poses the following problem: |
| 6958 |
|
| 6959 |
-- procedure Deep_Adjust (Obj : in out Parent_Typ) is |
| 6960 |
-- begin |
| 6961 |
-- Adjust (Obj); |
| 6962 |
-- ... |
| 6963 |
|
| 6964 |
-- procedure Deep_Adjust (Obj : in out Derived_Typ) is |
| 6965 |
-- begin |
| 6966 |
-- Deep_Adjust (Obj._parent); |
| 6967 |
-- ... |
| 6968 |
-- Adjust (Obj); |
| 6969 |
-- ... |
| 6970 |
|
| 6971 |
-- Adjusting the derived type will invoke Adjust of the parent and |
| 6972 |
-- then that of the derived type. This is undesirable because both |
| 6973 |
-- routines may modify shared components. Only the Adjust of the |
| 6974 |
-- derived type should be invoked. |
| 6975 |
|
| 6976 |
-- To prevent this double adjustment of shared components, |
| 6977 |
-- Deep_Adjust uses a flag to control the invocation of Adjust: |
| 6978 |
|
| 6979 |
-- procedure Deep_Adjust |
| 6980 |
-- (Obj : in out Some_Type; |
| 6981 |
-- Flag : Boolean := True) |
| 6982 |
-- is |
| 6983 |
-- begin |
| 6984 |
-- if Flag then |
| 6985 |
-- Adjust (Obj); |
| 6986 |
-- end if; |
| 6987 |
-- ... |
| 6988 |
|
| 6989 |
-- When Deep_Adjust is invoked for field _parent, a value of False is |
| 6990 |
-- provided for the flag: |
| 6991 |
|
| 6992 |
-- Deep_Adjust (Obj._parent, False); |
| 6993 |
|
| 6994 |
if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then |
| 6995 |
declare |
| 6996 |
Par_Typ : constant Entity_Id := Parent_Field_Type (Typ); |
| 6997 |
Adj_Stmt : Node_Id; |
| 6998 |
Call : Node_Id; |
| 6999 |
|
| 7000 |
begin |
| 7001 |
if Needs_Finalization (Par_Typ) then |
| 7002 |
Call := |
| 7003 |
Make_Adjust_Call |
| 7004 |
(Obj_Ref => |
| 7005 |
Make_Selected_Component (Loc, |
| 7006 |
Prefix => Make_Identifier (Loc, Name_V), |
| 7007 |
Selector_Name => |
| 7008 |
Make_Identifier (Loc, Name_uParent)), |
| 7009 |
Typ => Par_Typ, |
| 7010 |
Skip_Self => True); |
| 7011 |
|
| 7012 |
-- Generate: |
| 7013 |
-- begin |
| 7014 |
-- Deep_Adjust (V._parent, False); |
| 7015 |
|
| 7016 |
-- exception |
| 7017 |
-- when Id : others => |
| 7018 |
-- if not Raised then |
| 7019 |
-- Raised := True; |
| 7020 |
-- Save_Occurrence (E, |
| 7021 |
-- Get_Current_Excep.all.all); |
| 7022 |
-- end if; |
| 7023 |
-- end; |
| 7024 |
|
| 7025 |
if Present (Call) then |
| 7026 |
Adj_Stmt := Call; |
| 7027 |
|
| 7028 |
if Exceptions_OK then |
| 7029 |
Adj_Stmt := |
| 7030 |
Make_Block_Statement (Loc, |
| 7031 |
Handled_Statement_Sequence => |
| 7032 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 7033 |
Statements => New_List (Adj_Stmt), |
| 7034 |
Exception_Handlers => New_List ( |
| 7035 |
Build_Exception_Handler (Finalizer_Data)))); |
| 7036 |
end if; |
| 7037 |
|
| 7038 |
Prepend_To (Bod_Stmts, Adj_Stmt); |
| 7039 |
end if; |
| 7040 |
end if; |
| 7041 |
end; |
| 7042 |
end if; |
| 7043 |
|
| 7044 |
-- Adjust the object. This action must be performed last after all |
| 7045 |
-- components have been adjusted. |
| 7046 |
|
| 7047 |
if Is_Controlled (Typ) then |
| 7048 |
declare |
| 7049 |
Adj_Stmt : Node_Id; |
| 7050 |
Proc : Entity_Id; |
| 7051 |
|
| 7052 |
begin |
| 7053 |
Proc := Find_Controlled_Prim_Op (Typ, Name_Adjust); |
| 7054 |
|
| 7055 |
-- Generate: |
| 7056 |
-- if F then |
| 7057 |
-- begin |
| 7058 |
-- Adjust (V); |
| 7059 |
|
| 7060 |
-- exception |
| 7061 |
-- when others => |
| 7062 |
-- if not Raised then |
| 7063 |
-- Raised := True; |
| 7064 |
-- Save_Occurrence (E, |
| 7065 |
-- Get_Current_Excep.all.all); |
| 7066 |
-- end if; |
| 7067 |
-- end; |
| 7068 |
-- end if; |
| 7069 |
|
| 7070 |
if Present (Proc) then |
| 7071 |
Adj_Stmt := |
| 7072 |
Make_Procedure_Call_Statement (Loc, |
| 7073 |
Name => New_Occurrence_Of (Proc, Loc), |
| 7074 |
Parameter_Associations => New_List ( |
| 7075 |
Make_Identifier (Loc, Name_V))); |
| 7076 |
|
| 7077 |
if Exceptions_OK then |
| 7078 |
Adj_Stmt := |
| 7079 |
Make_Block_Statement (Loc, |
| 7080 |
Handled_Statement_Sequence => |
| 7081 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 7082 |
Statements => New_List (Adj_Stmt), |
| 7083 |
Exception_Handlers => New_List ( |
| 7084 |
Build_Exception_Handler |
| 7085 |
(Finalizer_Data)))); |
| 7086 |
end if; |
| 7087 |
|
| 7088 |
Append_To (Bod_Stmts, |
| 7089 |
Make_If_Statement (Loc, |
| 7090 |
Condition => Make_Identifier (Loc, Name_F), |
| 7091 |
Then_Statements => New_List (Adj_Stmt))); |
| 7092 |
end if; |
| 7093 |
end; |
| 7094 |
end if; |
| 7095 |
|
| 7096 |
-- At this point either all adjustment statements have been generated |
| 7097 |
-- or the type is not controlled. |
| 7098 |
|
| 7099 |
if Is_Empty_List (Bod_Stmts) then |
| 7100 |
Append_To (Bod_Stmts, Make_Null_Statement (Loc)); |
| 7101 |
|
| 7102 |
return Bod_Stmts; |
| 7103 |
|
| 7104 |
-- Generate: |
| 7105 |
-- declare |
| 7106 |
-- Abort : constant Boolean := Triggered_By_Abort; |
| 7107 |
-- <or> |
| 7108 |
-- Abort : constant Boolean := False; -- no abort |
| 7109 |
|
| 7110 |
-- E : Exception_Occurrence; |
| 7111 |
-- Raised : Boolean := False; |
| 7112 |
|
| 7113 |
-- begin |
| 7114 |
-- <adjust statements> |
| 7115 |
|
| 7116 |
-- if Raised and then not Abort then |
| 7117 |
-- Raise_From_Controlled_Operation (E); |
| 7118 |
-- end if; |
| 7119 |
-- end; |
| 7120 |
|
| 7121 |
else |
| 7122 |
if Exceptions_OK then |
| 7123 |
Append_To (Bod_Stmts, Build_Raise_Statement (Finalizer_Data)); |
| 7124 |
end if; |
| 7125 |
|
| 7126 |
return |
| 7127 |
New_List ( |
| 7128 |
Make_Block_Statement (Loc, |
| 7129 |
Declarations => |
| 7130 |
Finalizer_Decls, |
| 7131 |
Handled_Statement_Sequence => |
| 7132 |
Make_Handled_Sequence_Of_Statements (Loc, Bod_Stmts))); |
| 7133 |
end if; |
| 7134 |
end Build_Adjust_Statements; |
| 7135 |
|
| 7136 |
------------------------------- |
| 7137 |
-- Build_Finalize_Statements -- |
| 7138 |
------------------------------- |
| 7139 |
|
| 7140 |
function Build_Finalize_Statements (Typ : Entity_Id) return List_Id is |
| 7141 |
Typ_Def : constant Node_Id := Type_Definition (Parent (Typ)); |
| 7142 |
|
| 7143 |
Counter : Nat := 0; |
| 7144 |
Finalizer_Data : Finalization_Exception_Data; |
| 7145 |
Last_POC_Call : Node_Id := Empty; |
| 7146 |
|
| 7147 |
function Process_Component_List_For_Finalize |
| 7148 |
(Comps : Node_Id; |
| 7149 |
In_Variant_Part : Boolean := False) return List_Id; |
| 7150 |
-- Build all necessary finalization statements for a single component |
| 7151 |
-- list. The statements may include a jump circuitry if flag Is_Local |
| 7152 |
-- is enabled. In_Variant_Part indicates whether this is a recursive |
| 7153 |
-- call. |
| 7154 |
|
| 7155 |
----------------------------------------- |
| 7156 |
-- Process_Component_List_For_Finalize -- |
| 7157 |
----------------------------------------- |
| 7158 |
|
| 7159 |
function Process_Component_List_For_Finalize |
| 7160 |
(Comps : Node_Id; |
| 7161 |
In_Variant_Part : Boolean := False) return List_Id |
| 7162 |
is |
| 7163 |
procedure Process_Component_For_Finalize |
| 7164 |
(Decl : Node_Id; |
| 7165 |
Alts : List_Id; |
| 7166 |
Decls : List_Id; |
| 7167 |
Stmts : List_Id; |
| 7168 |
Num_Comps : in out Nat); |
| 7169 |
-- Process the declaration of a single controlled component. If |
| 7170 |
-- flag Is_Local is enabled, create the corresponding label and |
| 7171 |
-- jump circuitry. Alts is the list of case alternatives, Decls |
| 7172 |
-- is the top level declaration list where labels are declared |
| 7173 |
-- and Stmts is the list of finalization actions. Num_Comps |
| 7174 |
-- denotes the current number of components needing finalization. |
| 7175 |
|
| 7176 |
------------------------------------ |
| 7177 |
-- Process_Component_For_Finalize -- |
| 7178 |
------------------------------------ |
| 7179 |
|
| 7180 |
procedure Process_Component_For_Finalize |
| 7181 |
(Decl : Node_Id; |
| 7182 |
Alts : List_Id; |
| 7183 |
Decls : List_Id; |
| 7184 |
Stmts : List_Id; |
| 7185 |
Num_Comps : in out Nat) |
| 7186 |
is |
| 7187 |
Id : constant Entity_Id := Defining_Identifier (Decl); |
| 7188 |
Typ : constant Entity_Id := Etype (Id); |
| 7189 |
Fin_Call : Node_Id; |
| 7190 |
|
| 7191 |
begin |
| 7192 |
if Is_Local then |
| 7193 |
declare |
| 7194 |
Label : Node_Id; |
| 7195 |
Label_Id : Entity_Id; |
| 7196 |
|
| 7197 |
begin |
| 7198 |
-- Generate: |
| 7199 |
-- LN : label; |
| 7200 |
|
| 7201 |
Label_Id := |
| 7202 |
Make_Identifier (Loc, |
| 7203 |
Chars => New_External_Name ('L', Num_Comps)); |
| 7204 |
Set_Entity (Label_Id, |
| 7205 |
Make_Defining_Identifier (Loc, Chars (Label_Id))); |
| 7206 |
Label := Make_Label (Loc, Label_Id); |
| 7207 |
|
| 7208 |
Append_To (Decls, |
| 7209 |
Make_Implicit_Label_Declaration (Loc, |
| 7210 |
Defining_Identifier => Entity (Label_Id), |
| 7211 |
Label_Construct => Label)); |
| 7212 |
|
| 7213 |
-- Generate: |
| 7214 |
-- when N => |
| 7215 |
-- goto LN; |
| 7216 |
|
| 7217 |
Append_To (Alts, |
| 7218 |
Make_Case_Statement_Alternative (Loc, |
| 7219 |
Discrete_Choices => New_List ( |
| 7220 |
Make_Integer_Literal (Loc, Num_Comps)), |
| 7221 |
|
| 7222 |
Statements => New_List ( |
| 7223 |
Make_Goto_Statement (Loc, |
| 7224 |
Name => |
| 7225 |
New_Occurrence_Of (Entity (Label_Id), Loc))))); |
| 7226 |
|
| 7227 |
-- Generate: |
| 7228 |
-- <<LN>> |
| 7229 |
|
| 7230 |
Append_To (Stmts, Label); |
| 7231 |
|
| 7232 |
-- Decrease the number of components to be processed. |
| 7233 |
-- This action yields a new Label_Id in future calls. |
| 7234 |
|
| 7235 |
Num_Comps := Num_Comps - 1; |
| 7236 |
end; |
| 7237 |
end if; |
| 7238 |
|
| 7239 |
-- Generate: |
| 7240 |
-- [Deep_]Finalize (V.Id); -- No_Exception_Propagation |
| 7241 |
|
| 7242 |
-- begin -- Exception handlers allowed |
| 7243 |
-- [Deep_]Finalize (V.Id); |
| 7244 |
-- exception |
| 7245 |
-- when others => |
| 7246 |
-- if not Raised then |
| 7247 |
-- Raised := True; |
| 7248 |
-- Save_Occurrence (E, |
| 7249 |
-- Get_Current_Excep.all.all); |
| 7250 |
-- end if; |
| 7251 |
-- end; |
| 7252 |
|
| 7253 |
Fin_Call := |
| 7254 |
Make_Final_Call |
| 7255 |
(Obj_Ref => |
| 7256 |
Make_Selected_Component (Loc, |
| 7257 |
Prefix => Make_Identifier (Loc, Name_V), |
| 7258 |
Selector_Name => Make_Identifier (Loc, Chars (Id))), |
| 7259 |
Typ => Typ); |
| 7260 |
|
| 7261 |
-- Guard against a missing [Deep_]Finalize when the component |
| 7262 |
-- type was not properly frozen. |
| 7263 |
|
| 7264 |
if Present (Fin_Call) then |
| 7265 |
if Exceptions_OK then |
| 7266 |
Fin_Call := |
| 7267 |
Make_Block_Statement (Loc, |
| 7268 |
Handled_Statement_Sequence => |
| 7269 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 7270 |
Statements => New_List (Fin_Call), |
| 7271 |
Exception_Handlers => New_List ( |
| 7272 |
Build_Exception_Handler (Finalizer_Data)))); |
| 7273 |
end if; |
| 7274 |
|
| 7275 |
Append_To (Stmts, Fin_Call); |
| 7276 |
end if; |
| 7277 |
end Process_Component_For_Finalize; |
| 7278 |
|
| 7279 |
-- Local variables |
| 7280 |
|
| 7281 |
Alts : List_Id; |
| 7282 |
Counter_Id : Entity_Id := Empty; |
| 7283 |
Decl : Node_Id; |
| 7284 |
Decl_Id : Entity_Id; |
| 7285 |
Decl_Typ : Entity_Id; |
| 7286 |
Decls : List_Id; |
| 7287 |
Has_POC : Boolean; |
| 7288 |
Jump_Block : Node_Id; |
| 7289 |
Label : Node_Id; |
| 7290 |
Label_Id : Entity_Id; |
| 7291 |
Num_Comps : Nat; |
| 7292 |
Stmts : List_Id; |
| 7293 |
Var_Case : Node_Id; |
| 7294 |
|
| 7295 |
-- Start of processing for Process_Component_List_For_Finalize |
| 7296 |
|
| 7297 |
begin |
| 7298 |
-- Perform an initial check, look for controlled and per-object |
| 7299 |
-- constrained components. |
| 7300 |
|
| 7301 |
Preprocess_Components (Comps, Num_Comps, Has_POC); |
| 7302 |
|
| 7303 |
-- Create a state counter to service the current component list. |
| 7304 |
-- This step is performed before the variants are inspected in |
| 7305 |
-- order to generate the same state counter names as those from |
| 7306 |
-- Build_Initialize_Statements. |
| 7307 |
|
| 7308 |
if Num_Comps > 0 and then Is_Local then |
| 7309 |
Counter := Counter + 1; |
| 7310 |
|
| 7311 |
Counter_Id := |
| 7312 |
Make_Defining_Identifier (Loc, |
| 7313 |
Chars => New_External_Name ('C', Counter)); |
| 7314 |
end if; |
| 7315 |
|
| 7316 |
-- Process the component in the following order: |
| 7317 |
-- 1) Variants |
| 7318 |
-- 2) Per-object constrained components |
| 7319 |
-- 3) Regular components |
| 7320 |
|
| 7321 |
-- Start with the variant parts |
| 7322 |
|
| 7323 |
Var_Case := Empty; |
| 7324 |
if Present (Variant_Part (Comps)) then |
| 7325 |
declare |
| 7326 |
Var_Alts : constant List_Id := New_List; |
| 7327 |
Var : Node_Id; |
| 7328 |
|
| 7329 |
begin |
| 7330 |
Var := First_Non_Pragma (Variants (Variant_Part (Comps))); |
| 7331 |
while Present (Var) loop |
| 7332 |
|
| 7333 |
-- Generate: |
| 7334 |
-- when <discrete choices> => |
| 7335 |
-- <finalize statements> |
| 7336 |
|
| 7337 |
Append_To (Var_Alts, |
| 7338 |
Make_Case_Statement_Alternative (Loc, |
| 7339 |
Discrete_Choices => |
| 7340 |
New_Copy_List (Discrete_Choices (Var)), |
| 7341 |
Statements => |
| 7342 |
Process_Component_List_For_Finalize ( |
| 7343 |
Component_List (Var), |
| 7344 |
In_Variant_Part => True))); |
| 7345 |
|
| 7346 |
Next_Non_Pragma (Var); |
| 7347 |
end loop; |
| 7348 |
|
| 7349 |
-- Generate: |
| 7350 |
-- case V.<discriminant> is |
| 7351 |
-- when <discrete choices 1> => |
| 7352 |
-- <finalize statements 1> |
| 7353 |
-- ... |
| 7354 |
-- when <discrete choices N> => |
| 7355 |
-- <finalize statements N> |
| 7356 |
-- end case; |
| 7357 |
|
| 7358 |
Var_Case := |
| 7359 |
Make_Case_Statement (Loc, |
| 7360 |
Expression => |
| 7361 |
Make_Selected_Component (Loc, |
| 7362 |
Prefix => Make_Identifier (Loc, Name_V), |
| 7363 |
Selector_Name => |
| 7364 |
Make_Identifier (Loc, |
| 7365 |
Chars => Chars (Name (Variant_Part (Comps))))), |
| 7366 |
Alternatives => Var_Alts); |
| 7367 |
end; |
| 7368 |
end if; |
| 7369 |
|
| 7370 |
-- The current component list does not have a single controlled |
| 7371 |
-- component, however it may contain variants. Return the case |
| 7372 |
-- statement for the variants or nothing. |
| 7373 |
|
| 7374 |
if Num_Comps = 0 then |
| 7375 |
if Present (Var_Case) then |
| 7376 |
return New_List (Var_Case); |
| 7377 |
else |
| 7378 |
return New_List (Make_Null_Statement (Loc)); |
| 7379 |
end if; |
| 7380 |
end if; |
| 7381 |
|
| 7382 |
-- Prepare all lists |
| 7383 |
|
| 7384 |
Alts := New_List; |
| 7385 |
Decls := New_List; |
| 7386 |
Stmts := New_List; |
| 7387 |
|
| 7388 |
-- Process all per-object constrained components in reverse order |
| 7389 |
|
| 7390 |
if Has_POC then |
| 7391 |
Decl := Last_Non_Pragma (Component_Items (Comps)); |
| 7392 |
while Present (Decl) loop |
| 7393 |
Decl_Id := Defining_Identifier (Decl); |
| 7394 |
Decl_Typ := Etype (Decl_Id); |
| 7395 |
|
| 7396 |
-- Skip _parent |
| 7397 |
|
| 7398 |
if Chars (Decl_Id) /= Name_uParent |
| 7399 |
and then Needs_Finalization (Decl_Typ) |
| 7400 |
and then Has_Access_Constraint (Decl_Id) |
| 7401 |
and then No (Expression (Decl)) |
| 7402 |
then |
| 7403 |
Process_Component_For_Finalize |
| 7404 |
(Decl, Alts, Decls, Stmts, Num_Comps); |
| 7405 |
end if; |
| 7406 |
|
| 7407 |
Prev_Non_Pragma (Decl); |
| 7408 |
end loop; |
| 7409 |
end if; |
| 7410 |
|
| 7411 |
if not In_Variant_Part then |
| 7412 |
Last_POC_Call := Last (Stmts); |
| 7413 |
-- In the case of a type extension, the deep-finalize call |
| 7414 |
-- for the _Parent component will be inserted here. |
| 7415 |
end if; |
| 7416 |
|
| 7417 |
-- Process the rest of the components in reverse order |
| 7418 |
|
| 7419 |
Decl := Last_Non_Pragma (Component_Items (Comps)); |
| 7420 |
while Present (Decl) loop |
| 7421 |
Decl_Id := Defining_Identifier (Decl); |
| 7422 |
Decl_Typ := Etype (Decl_Id); |
| 7423 |
|
| 7424 |
-- Skip _parent |
| 7425 |
|
| 7426 |
if Chars (Decl_Id) /= Name_uParent |
| 7427 |
and then Needs_Finalization (Decl_Typ) |
| 7428 |
then |
| 7429 |
-- Skip per-object constrained components since they were |
| 7430 |
-- handled in the above step. |
| 7431 |
|
| 7432 |
if Has_Access_Constraint (Decl_Id) |
| 7433 |
and then No (Expression (Decl)) |
| 7434 |
then |
| 7435 |
null; |
| 7436 |
else |
| 7437 |
Process_Component_For_Finalize |
| 7438 |
(Decl, Alts, Decls, Stmts, Num_Comps); |
| 7439 |
end if; |
| 7440 |
end if; |
| 7441 |
|
| 7442 |
Prev_Non_Pragma (Decl); |
| 7443 |
end loop; |
| 7444 |
|
| 7445 |
-- Generate: |
| 7446 |
-- declare |
| 7447 |
-- LN : label; -- If Is_Local is enabled |
| 7448 |
-- ... . |
| 7449 |
-- L0 : label; . |
| 7450 |
|
| 7451 |
-- begin . |
| 7452 |
-- case CounterX is . |
| 7453 |
-- when N => . |
| 7454 |
-- goto LN; . |
| 7455 |
-- ... . |
| 7456 |
-- when 1 => . |
| 7457 |
-- goto L1; . |
| 7458 |
-- when others => . |
| 7459 |
-- goto L0; . |
| 7460 |
-- end case; . |
| 7461 |
|
| 7462 |
-- <<LN>> -- If Is_Local is enabled |
| 7463 |
-- begin |
| 7464 |
-- [Deep_]Finalize (V.CompY); |
| 7465 |
-- exception |
| 7466 |
-- when Id : others => |
| 7467 |
-- if not Raised then |
| 7468 |
-- Raised := True; |
| 7469 |
-- Save_Occurrence (E, |
| 7470 |
-- Get_Current_Excep.all.all); |
| 7471 |
-- end if; |
| 7472 |
-- end; |
| 7473 |
-- ... |
| 7474 |
-- <<L0>> -- If Is_Local is enabled |
| 7475 |
-- end; |
| 7476 |
|
| 7477 |
if Is_Local then |
| 7478 |
|
| 7479 |
-- Add the declaration of default jump location L0, its |
| 7480 |
-- corresponding alternative and its place in the statements. |
| 7481 |
|
| 7482 |
Label_Id := Make_Identifier (Loc, New_External_Name ('L', 0)); |
| 7483 |
Set_Entity (Label_Id, |
| 7484 |
Make_Defining_Identifier (Loc, Chars (Label_Id))); |
| 7485 |
Label := Make_Label (Loc, Label_Id); |
| 7486 |
|
| 7487 |
Append_To (Decls, -- declaration |
| 7488 |
Make_Implicit_Label_Declaration (Loc, |
| 7489 |
Defining_Identifier => Entity (Label_Id), |
| 7490 |
Label_Construct => Label)); |
| 7491 |
|
| 7492 |
Append_To (Alts, -- alternative |
| 7493 |
Make_Case_Statement_Alternative (Loc, |
| 7494 |
Discrete_Choices => New_List ( |
| 7495 |
Make_Others_Choice (Loc)), |
| 7496 |
|
| 7497 |
Statements => New_List ( |
| 7498 |
Make_Goto_Statement (Loc, |
| 7499 |
Name => New_Occurrence_Of (Entity (Label_Id), Loc))))); |
| 7500 |
|
| 7501 |
Append_To (Stmts, Label); -- statement |
| 7502 |
|
| 7503 |
-- Create the jump block |
| 7504 |
|
| 7505 |
Prepend_To (Stmts, |
| 7506 |
Make_Case_Statement (Loc, |
| 7507 |
Expression => Make_Identifier (Loc, Chars (Counter_Id)), |
| 7508 |
Alternatives => Alts)); |
| 7509 |
end if; |
| 7510 |
|
| 7511 |
Jump_Block := |
| 7512 |
Make_Block_Statement (Loc, |
| 7513 |
Declarations => Decls, |
| 7514 |
Handled_Statement_Sequence => |
| 7515 |
Make_Handled_Sequence_Of_Statements (Loc, Stmts)); |
| 7516 |
|
| 7517 |
if Present (Var_Case) then |
| 7518 |
return New_List (Var_Case, Jump_Block); |
| 7519 |
else |
| 7520 |
return New_List (Jump_Block); |
| 7521 |
end if; |
| 7522 |
end Process_Component_List_For_Finalize; |
| 7523 |
|
| 7524 |
-- Local variables |
| 7525 |
|
| 7526 |
Bod_Stmts : List_Id := No_List; |
| 7527 |
Finalizer_Decls : List_Id := No_List; |
| 7528 |
Rec_Def : Node_Id; |
| 7529 |
|
| 7530 |
-- Start of processing for Build_Finalize_Statements |
| 7531 |
|
| 7532 |
begin |
| 7533 |
Finalizer_Decls := New_List; |
| 7534 |
Build_Object_Declarations (Finalizer_Data, Finalizer_Decls, Loc); |
| 7535 |
|
| 7536 |
if Nkind (Typ_Def) = N_Derived_Type_Definition then |
| 7537 |
Rec_Def := Record_Extension_Part (Typ_Def); |
| 7538 |
else |
| 7539 |
Rec_Def := Typ_Def; |
| 7540 |
end if; |
| 7541 |
|
| 7542 |
-- Create a finalization sequence for all record components |
| 7543 |
|
| 7544 |
if Present (Component_List (Rec_Def)) then |
| 7545 |
Bod_Stmts := |
| 7546 |
Process_Component_List_For_Finalize (Component_List (Rec_Def)); |
| 7547 |
end if; |
| 7548 |
|
| 7549 |
-- A derived record type must finalize all inherited components. This |
| 7550 |
-- action poses the following problem: |
| 7551 |
|
| 7552 |
-- procedure Deep_Finalize (Obj : in out Parent_Typ) is |
| 7553 |
-- begin |
| 7554 |
-- Finalize (Obj); |
| 7555 |
-- ... |
| 7556 |
|
| 7557 |
-- procedure Deep_Finalize (Obj : in out Derived_Typ) is |
| 7558 |
-- begin |
| 7559 |
-- Deep_Finalize (Obj._parent); |
| 7560 |
-- ... |
| 7561 |
-- Finalize (Obj); |
| 7562 |
-- ... |
| 7563 |
|
| 7564 |
-- Finalizing the derived type will invoke Finalize of the parent and |
| 7565 |
-- then that of the derived type. This is undesirable because both |
| 7566 |
-- routines may modify shared components. Only the Finalize of the |
| 7567 |
-- derived type should be invoked. |
| 7568 |
|
| 7569 |
-- To prevent this double adjustment of shared components, |
| 7570 |
-- Deep_Finalize uses a flag to control the invocation of Finalize: |
| 7571 |
|
| 7572 |
-- procedure Deep_Finalize |
| 7573 |
-- (Obj : in out Some_Type; |
| 7574 |
-- Flag : Boolean := True) |
| 7575 |
-- is |
| 7576 |
-- begin |
| 7577 |
-- if Flag then |
| 7578 |
-- Finalize (Obj); |
| 7579 |
-- end if; |
| 7580 |
-- ... |
| 7581 |
|
| 7582 |
-- When Deep_Finalize is invoked for field _parent, a value of False |
| 7583 |
-- is provided for the flag: |
| 7584 |
|
| 7585 |
-- Deep_Finalize (Obj._parent, False); |
| 7586 |
|
| 7587 |
if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then |
| 7588 |
declare |
| 7589 |
Par_Typ : constant Entity_Id := Parent_Field_Type (Typ); |
| 7590 |
Call : Node_Id; |
| 7591 |
Fin_Stmt : Node_Id; |
| 7592 |
|
| 7593 |
begin |
| 7594 |
if Needs_Finalization (Par_Typ) then |
| 7595 |
Call := |
| 7596 |
Make_Final_Call |
| 7597 |
(Obj_Ref => |
| 7598 |
Make_Selected_Component (Loc, |
| 7599 |
Prefix => Make_Identifier (Loc, Name_V), |
| 7600 |
Selector_Name => |
| 7601 |
Make_Identifier (Loc, Name_uParent)), |
| 7602 |
Typ => Par_Typ, |
| 7603 |
Skip_Self => True); |
| 7604 |
|
| 7605 |
-- Generate: |
| 7606 |
-- begin |
| 7607 |
-- Deep_Finalize (V._parent, False); |
| 7608 |
|
| 7609 |
-- exception |
| 7610 |
-- when Id : others => |
| 7611 |
-- if not Raised then |
| 7612 |
-- Raised := True; |
| 7613 |
-- Save_Occurrence (E, |
| 7614 |
-- Get_Current_Excep.all.all); |
| 7615 |
-- end if; |
| 7616 |
-- end; |
| 7617 |
|
| 7618 |
if Present (Call) then |
| 7619 |
Fin_Stmt := Call; |
| 7620 |
|
| 7621 |
if Exceptions_OK then |
| 7622 |
Fin_Stmt := |
| 7623 |
Make_Block_Statement (Loc, |
| 7624 |
Handled_Statement_Sequence => |
| 7625 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 7626 |
Statements => New_List (Fin_Stmt), |
| 7627 |
Exception_Handlers => New_List ( |
| 7628 |
Build_Exception_Handler |
| 7629 |
(Finalizer_Data)))); |
| 7630 |
end if; |
| 7631 |
|
| 7632 |
-- The intended component finalization order is |
| 7633 |
-- 1) POC components of extension |
| 7634 |
-- 2) _Parent component |
| 7635 |
-- 3) non-POC components of extension. |
| 7636 |
-- |
| 7637 |
-- With this "finalize the parent part in the middle" |
| 7638 |
-- ordering, we can avoid the need for making two |
| 7639 |
-- calls to the parent's subprogram in the way that |
| 7640 |
-- is necessary for Init_Procs. This does have the |
| 7641 |
-- peculiar (but legal) consequence that the parent's |
| 7642 |
-- non-POC components are finalized before the |
| 7643 |
-- non-POC extension components. This violates the |
| 7644 |
-- usual "finalize in reverse declaration order" |
| 7645 |
-- principle, but that's ok (see RM 7.6.1(9)). |
| 7646 |
-- |
| 7647 |
-- Last_POC_Call should be non-empty if the extension |
| 7648 |
-- has at least one POC. Interactions with variant |
| 7649 |
-- parts are incorrectly ignored. |
| 7650 |
|
| 7651 |
if Present (Last_POC_Call) then |
| 7652 |
Insert_After (Last_POC_Call, Fin_Stmt); |
| 7653 |
else |
| 7654 |
-- At this point, we could look for the common case |
| 7655 |
-- where there are no POC components anywhere in |
| 7656 |
-- sight (inherited or not) and, in that common case, |
| 7657 |
-- call Append_To instead of Prepend_To. That would |
| 7658 |
-- result in finalizing the parent part after, rather |
| 7659 |
-- than before, the extension components. That might |
| 7660 |
-- be more intuitive (as discussed in preceding |
| 7661 |
-- comment), but it is not required. |
| 7662 |
Prepend_To (Bod_Stmts, Fin_Stmt); |
| 7663 |
end if; |
| 7664 |
end if; |
| 7665 |
end if; |
| 7666 |
end; |
| 7667 |
end if; |
| 7668 |
|
| 7669 |
-- Finalize the object. This action must be performed first before |
| 7670 |
-- all components have been finalized. |
| 7671 |
|
| 7672 |
if Is_Controlled (Typ) and then not Is_Local then |
| 7673 |
declare |
| 7674 |
Fin_Stmt : Node_Id; |
| 7675 |
Proc : Entity_Id; |
| 7676 |
|
| 7677 |
begin |
| 7678 |
Proc := Find_Controlled_Prim_Op (Typ, Name_Finalize); |
| 7679 |
|
| 7680 |
-- Generate: |
| 7681 |
-- if F then |
| 7682 |
-- begin |
| 7683 |
-- Finalize (V); |
| 7684 |
|
| 7685 |
-- exception |
| 7686 |
-- when others => |
| 7687 |
-- if not Raised then |
| 7688 |
-- Raised := True; |
| 7689 |
-- Save_Occurrence (E, |
| 7690 |
-- Get_Current_Excep.all.all); |
| 7691 |
-- end if; |
| 7692 |
-- end; |
| 7693 |
-- end if; |
| 7694 |
|
| 7695 |
if Present (Proc) then |
| 7696 |
Fin_Stmt := |
| 7697 |
Make_Procedure_Call_Statement (Loc, |
| 7698 |
Name => New_Occurrence_Of (Proc, Loc), |
| 7699 |
Parameter_Associations => New_List ( |
| 7700 |
Make_Identifier (Loc, Name_V))); |
| 7701 |
|
| 7702 |
if Exceptions_OK then |
| 7703 |
Fin_Stmt := |
| 7704 |
Make_Block_Statement (Loc, |
| 7705 |
Handled_Statement_Sequence => |
| 7706 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 7707 |
Statements => New_List (Fin_Stmt), |
| 7708 |
Exception_Handlers => New_List ( |
| 7709 |
Build_Exception_Handler |
| 7710 |
(Finalizer_Data)))); |
| 7711 |
end if; |
| 7712 |
|
| 7713 |
Prepend_To (Bod_Stmts, |
| 7714 |
Make_If_Statement (Loc, |
| 7715 |
Condition => Make_Identifier (Loc, Name_F), |
| 7716 |
Then_Statements => New_List (Fin_Stmt))); |
| 7717 |
end if; |
| 7718 |
end; |
| 7719 |
end if; |
| 7720 |
|
| 7721 |
-- At this point either all finalization statements have been |
| 7722 |
-- generated or the type is not controlled. |
| 7723 |
|
| 7724 |
if No (Bod_Stmts) then |
| 7725 |
return New_List (Make_Null_Statement (Loc)); |
| 7726 |
|
| 7727 |
-- Generate: |
| 7728 |
-- declare |
| 7729 |
-- Abort : constant Boolean := Triggered_By_Abort; |
| 7730 |
-- <or> |
| 7731 |
-- Abort : constant Boolean := False; -- no abort |
| 7732 |
|
| 7733 |
-- E : Exception_Occurrence; |
| 7734 |
-- Raised : Boolean := False; |
| 7735 |
|
| 7736 |
-- begin |
| 7737 |
-- <finalize statements> |
| 7738 |
|
| 7739 |
-- if Raised and then not Abort then |
| 7740 |
-- Raise_From_Controlled_Operation (E); |
| 7741 |
-- end if; |
| 7742 |
-- end; |
| 7743 |
|
| 7744 |
else |
| 7745 |
if Exceptions_OK then |
| 7746 |
Append_To (Bod_Stmts, Build_Raise_Statement (Finalizer_Data)); |
| 7747 |
end if; |
| 7748 |
|
| 7749 |
return |
| 7750 |
New_List ( |
| 7751 |
Make_Block_Statement (Loc, |
| 7752 |
Declarations => |
| 7753 |
Finalizer_Decls, |
| 7754 |
Handled_Statement_Sequence => |
| 7755 |
Make_Handled_Sequence_Of_Statements (Loc, Bod_Stmts))); |
| 7756 |
end if; |
| 7757 |
end Build_Finalize_Statements; |
| 7758 |
|
| 7759 |
----------------------- |
| 7760 |
-- Parent_Field_Type -- |
| 7761 |
----------------------- |
| 7762 |
|
| 7763 |
function Parent_Field_Type (Typ : Entity_Id) return Entity_Id is |
| 7764 |
Field : Entity_Id; |
| 7765 |
|
| 7766 |
begin |
| 7767 |
Field := First_Entity (Typ); |
| 7768 |
while Present (Field) loop |
| 7769 |
if Chars (Field) = Name_uParent then |
| 7770 |
return Etype (Field); |
| 7771 |
end if; |
| 7772 |
|
| 7773 |
Next_Entity (Field); |
| 7774 |
end loop; |
| 7775 |
|
| 7776 |
-- A derived tagged type should always have a parent field |
| 7777 |
|
| 7778 |
raise Program_Error; |
| 7779 |
end Parent_Field_Type; |
| 7780 |
|
| 7781 |
--------------------------- |
| 7782 |
-- Preprocess_Components -- |
| 7783 |
--------------------------- |
| 7784 |
|
| 7785 |
procedure Preprocess_Components |
| 7786 |
(Comps : Node_Id; |
| 7787 |
Num_Comps : out Nat; |
| 7788 |
Has_POC : out Boolean) |
| 7789 |
is |
| 7790 |
Decl : Node_Id; |
| 7791 |
Id : Entity_Id; |
| 7792 |
Typ : Entity_Id; |
| 7793 |
|
| 7794 |
begin |
| 7795 |
Num_Comps := 0; |
| 7796 |
Has_POC := False; |
| 7797 |
|
| 7798 |
Decl := First_Non_Pragma (Component_Items (Comps)); |
| 7799 |
while Present (Decl) loop |
| 7800 |
Id := Defining_Identifier (Decl); |
| 7801 |
Typ := Etype (Id); |
| 7802 |
|
| 7803 |
-- Skip field _parent |
| 7804 |
|
| 7805 |
if Chars (Id) /= Name_uParent |
| 7806 |
and then Needs_Finalization (Typ) |
| 7807 |
then |
| 7808 |
Num_Comps := Num_Comps + 1; |
| 7809 |
|
| 7810 |
if Has_Access_Constraint (Id) |
| 7811 |
and then No (Expression (Decl)) |
| 7812 |
then |
| 7813 |
Has_POC := True; |
| 7814 |
end if; |
| 7815 |
end if; |
| 7816 |
|
| 7817 |
Next_Non_Pragma (Decl); |
| 7818 |
end loop; |
| 7819 |
end Preprocess_Components; |
| 7820 |
|
| 7821 |
-- Start of processing for Make_Deep_Record_Body |
| 7822 |
|
| 7823 |
begin |
| 7824 |
case Prim is |
| 7825 |
when Address_Case => |
| 7826 |
return Make_Finalize_Address_Stmts (Typ); |
| 7827 |
|
| 7828 |
when Adjust_Case => |
| 7829 |
return Build_Adjust_Statements (Typ); |
| 7830 |
|
| 7831 |
when Finalize_Case => |
| 7832 |
return Build_Finalize_Statements (Typ); |
| 7833 |
|
| 7834 |
when Initialize_Case => |
| 7835 |
if Is_Controlled (Typ) then |
| 7836 |
declare |
| 7837 |
Intlz : constant Entity_Id := |
| 7838 |
Find_Controlled_Prim_Op (Typ, Name_Initialize); |
| 7839 |
begin |
| 7840 |
if Present (Intlz) then |
| 7841 |
return |
| 7842 |
New_List |
| 7843 |
(Make_Procedure_Call_Statement |
| 7844 |
(Loc, |
| 7845 |
Name => |
| 7846 |
New_Occurrence_Of (Intlz, Loc), |
| 7847 |
Parameter_Associations => |
| 7848 |
New_List (Make_Identifier (Loc, Name_V)))); |
| 7849 |
else |
| 7850 |
return Empty_List; |
| 7851 |
end if; |
| 7852 |
end; |
| 7853 |
else |
| 7854 |
return Empty_List; |
| 7855 |
end if; |
| 7856 |
end case; |
| 7857 |
end Make_Deep_Record_Body; |
| 7858 |
|
| 7859 |
---------------------- |
| 7860 |
-- Make_Final_Call -- |
| 7861 |
---------------------- |
| 7862 |
|
| 7863 |
function Make_Final_Call |
| 7864 |
(Obj_Ref : Node_Id; |
| 7865 |
Typ : Entity_Id; |
| 7866 |
Skip_Self : Boolean := False) return Node_Id |
| 7867 |
is |
| 7868 |
Loc : constant Source_Ptr := Sloc (Obj_Ref); |
| 7869 |
Atyp : Entity_Id; |
| 7870 |
Prot_Typ : Entity_Id := Empty; |
| 7871 |
Fin_Id : Entity_Id := Empty; |
| 7872 |
Ref : Node_Id; |
| 7873 |
Utyp : Entity_Id; |
| 7874 |
|
| 7875 |
begin |
| 7876 |
Ref := Obj_Ref; |
| 7877 |
|
| 7878 |
-- Recover the proper type which contains [Deep_]Finalize |
| 7879 |
|
| 7880 |
if Is_Class_Wide_Type (Typ) then |
| 7881 |
Utyp := Root_Type (Typ); |
| 7882 |
Atyp := Utyp; |
| 7883 |
|
| 7884 |
elsif Is_Concurrent_Type (Typ) then |
| 7885 |
Utyp := Corresponding_Record_Type (Typ); |
| 7886 |
Atyp := Empty; |
| 7887 |
Ref := Convert_Concurrent (Ref, Typ); |
| 7888 |
|
| 7889 |
elsif Is_Private_Type (Typ) |
| 7890 |
and then Present (Underlying_Type (Typ)) |
| 7891 |
and then Is_Concurrent_Type (Underlying_Type (Typ)) |
| 7892 |
then |
| 7893 |
Utyp := Corresponding_Record_Type (Underlying_Type (Typ)); |
| 7894 |
Atyp := Typ; |
| 7895 |
Ref := Convert_Concurrent (Ref, Underlying_Type (Typ)); |
| 7896 |
|
| 7897 |
else |
| 7898 |
Utyp := Typ; |
| 7899 |
Atyp := Typ; |
| 7900 |
end if; |
| 7901 |
|
| 7902 |
Utyp := Underlying_Type (Base_Type (Utyp)); |
| 7903 |
Set_Assignment_OK (Ref); |
| 7904 |
|
| 7905 |
-- Deal with untagged derivation of private views. If the parent type |
| 7906 |
-- is a protected type, Deep_Finalize is found on the corresponding |
| 7907 |
-- record of the ancestor. |
| 7908 |
|
| 7909 |
if Is_Untagged_Derivation (Typ) then |
| 7910 |
if Is_Protected_Type (Typ) then |
| 7911 |
Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ))); |
| 7912 |
elsif Is_Implicit_Full_View (Utyp) then |
| 7913 |
Utyp := Underlying_Type (Root_Type (Base_Type (Typ))); |
| 7914 |
|
| 7915 |
if Is_Protected_Type (Utyp) then |
| 7916 |
Utyp := Corresponding_Record_Type (Utyp); |
| 7917 |
end if; |
| 7918 |
end if; |
| 7919 |
|
| 7920 |
Ref := Unchecked_Convert_To (Utyp, Ref); |
| 7921 |
Set_Assignment_OK (Ref); |
| 7922 |
end if; |
| 7923 |
|
| 7924 |
-- Deal with derived private types which do not inherit primitives from |
| 7925 |
-- their parents. In this case, [Deep_]Finalize can be found in the full |
| 7926 |
-- view of the parent type. |
| 7927 |
|
| 7928 |
if Present (Utyp) |
| 7929 |
and then Is_Tagged_Type (Utyp) |
| 7930 |
and then Is_Derived_Type (Utyp) |
| 7931 |
and then Is_Empty_Elmt_List (Primitive_Operations (Utyp)) |
| 7932 |
and then Is_Private_Type (Etype (Utyp)) |
| 7933 |
and then Present (Full_View (Etype (Utyp))) |
| 7934 |
then |
| 7935 |
Utyp := Full_View (Etype (Utyp)); |
| 7936 |
Ref := Unchecked_Convert_To (Utyp, Ref); |
| 7937 |
Set_Assignment_OK (Ref); |
| 7938 |
end if; |
| 7939 |
|
| 7940 |
-- When dealing with the completion of a private type, use the base type |
| 7941 |
-- instead. |
| 7942 |
|
| 7943 |
if Present (Utyp) and then Utyp /= Base_Type (Utyp) then |
| 7944 |
pragma Assert (Present (Atyp) and then Is_Private_Type (Atyp)); |
| 7945 |
|
| 7946 |
Utyp := Base_Type (Utyp); |
| 7947 |
Ref := Unchecked_Convert_To (Utyp, Ref); |
| 7948 |
Set_Assignment_OK (Ref); |
| 7949 |
end if; |
| 7950 |
|
| 7951 |
-- Detect if Typ is a protected type or an expanded protected type and |
| 7952 |
-- store the relevant type within Prot_Typ for later processing. |
| 7953 |
|
| 7954 |
if Is_Protected_Type (Typ) then |
| 7955 |
Prot_Typ := Typ; |
| 7956 |
|
| 7957 |
elsif Ekind (Typ) = E_Record_Type |
| 7958 |
and then Present (Corresponding_Concurrent_Type (Typ)) |
| 7959 |
and then Is_Protected_Type (Corresponding_Concurrent_Type (Typ)) |
| 7960 |
then |
| 7961 |
Prot_Typ := Corresponding_Concurrent_Type (Typ); |
| 7962 |
end if; |
| 7963 |
|
| 7964 |
-- The underlying type may not be present due to a missing full view. In |
| 7965 |
-- this case freezing did not take place and there is no [Deep_]Finalize |
| 7966 |
-- primitive to call. |
| 7967 |
|
| 7968 |
if No (Utyp) then |
| 7969 |
return Empty; |
| 7970 |
|
| 7971 |
elsif Skip_Self then |
| 7972 |
if Has_Controlled_Component (Utyp) then |
| 7973 |
if Is_Tagged_Type (Utyp) then |
| 7974 |
Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize); |
| 7975 |
else |
| 7976 |
Fin_Id := TSS (Utyp, TSS_Deep_Finalize); |
| 7977 |
end if; |
| 7978 |
end if; |
| 7979 |
|
| 7980 |
-- Class-wide types, interfaces and types with controlled components |
| 7981 |
|
| 7982 |
elsif Is_Class_Wide_Type (Typ) |
| 7983 |
or else Is_Interface (Typ) |
| 7984 |
or else Has_Controlled_Component (Utyp) |
| 7985 |
then |
| 7986 |
if Is_Tagged_Type (Utyp) then |
| 7987 |
Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize); |
| 7988 |
else |
| 7989 |
Fin_Id := TSS (Utyp, TSS_Deep_Finalize); |
| 7990 |
end if; |
| 7991 |
|
| 7992 |
-- Derivations from [Limited_]Controlled |
| 7993 |
|
| 7994 |
elsif Is_Controlled (Utyp) then |
| 7995 |
Fin_Id := Find_Controlled_Prim_Op (Utyp, Name_Finalize); |
| 7996 |
|
| 7997 |
-- Tagged types |
| 7998 |
|
| 7999 |
elsif Is_Tagged_Type (Utyp) then |
| 8000 |
Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize); |
| 8001 |
|
| 8002 |
-- Protected types: these also require finalization even though they |
| 8003 |
-- are not marked controlled explicitly. |
| 8004 |
|
| 8005 |
elsif Present (Prot_Typ) then |
| 8006 |
-- Protected objects do not need to be finalized on restricted |
| 8007 |
-- runtimes. |
| 8008 |
|
| 8009 |
if Restricted_Profile then |
| 8010 |
return Empty; |
| 8011 |
|
| 8012 |
-- ??? Only handle the simple case for now. Will not support a record |
| 8013 |
-- or array containing protected objects. |
| 8014 |
|
| 8015 |
elsif Is_Simple_Protected_Type (Prot_Typ) then |
| 8016 |
Fin_Id := RTE (RE_Finalize_Protection); |
| 8017 |
else |
| 8018 |
raise Program_Error; |
| 8019 |
end if; |
| 8020 |
|
| 8021 |
else |
| 8022 |
raise Program_Error; |
| 8023 |
end if; |
| 8024 |
|
| 8025 |
if Present (Fin_Id) then |
| 8026 |
|
| 8027 |
-- When finalizing a class-wide object, do not convert to the root |
| 8028 |
-- type in order to produce a dispatching call. |
| 8029 |
|
| 8030 |
if Is_Class_Wide_Type (Typ) then |
| 8031 |
null; |
| 8032 |
|
| 8033 |
-- Ensure that a finalization routine is at least decorated in order |
| 8034 |
-- to inspect the object parameter. |
| 8035 |
|
| 8036 |
elsif Analyzed (Fin_Id) |
| 8037 |
or else Ekind (Fin_Id) = E_Procedure |
| 8038 |
then |
| 8039 |
-- In certain cases, such as the creation of Stream_Read, the |
| 8040 |
-- visible entity of the type is its full view. Since Stream_Read |
| 8041 |
-- will have to create an object of type Typ, the local object |
| 8042 |
-- will be finalzed by the scope finalizer generated later on. The |
| 8043 |
-- object parameter of Deep_Finalize will always use the private |
| 8044 |
-- view of the type. To avoid such a clash between a private and a |
| 8045 |
-- full view, perform an unchecked conversion of the object |
| 8046 |
-- reference to the private view. |
| 8047 |
|
| 8048 |
declare |
| 8049 |
Formal_Typ : constant Entity_Id := |
| 8050 |
Etype (First_Formal (Fin_Id)); |
| 8051 |
begin |
| 8052 |
if Is_Private_Type (Formal_Typ) |
| 8053 |
and then Present (Full_View (Formal_Typ)) |
| 8054 |
and then Full_View (Formal_Typ) = Utyp |
| 8055 |
then |
| 8056 |
Ref := Unchecked_Convert_To (Formal_Typ, Ref); |
| 8057 |
end if; |
| 8058 |
end; |
| 8059 |
|
| 8060 |
Ref := Convert_View (Fin_Id, Ref, Typ); |
| 8061 |
end if; |
| 8062 |
|
| 8063 |
return |
| 8064 |
Make_Call (Loc, |
| 8065 |
Proc_Id => Fin_Id, |
| 8066 |
Param => Ref, |
| 8067 |
Skip_Self => Skip_Self); |
| 8068 |
else |
| 8069 |
pragma Assert (Serious_Errors_Detected > 0 |
| 8070 |
or else not Has_Controlled_Component (Utyp)); |
| 8071 |
return Empty; |
| 8072 |
end if; |
| 8073 |
end Make_Final_Call; |
| 8074 |
|
| 8075 |
-------------------------------- |
| 8076 |
-- Make_Finalize_Address_Body -- |
| 8077 |
-------------------------------- |
| 8078 |
|
| 8079 |
procedure Make_Finalize_Address_Body (Typ : Entity_Id) is |
| 8080 |
Is_Task : constant Boolean := |
| 8081 |
Ekind (Typ) = E_Record_Type |
| 8082 |
and then Is_Concurrent_Record_Type (Typ) |
| 8083 |
and then Ekind (Corresponding_Concurrent_Type (Typ)) = |
| 8084 |
E_Task_Type; |
| 8085 |
Loc : constant Source_Ptr := Sloc (Typ); |
| 8086 |
Proc_Id : Entity_Id; |
| 8087 |
Stmts : List_Id; |
| 8088 |
|
| 8089 |
begin |
| 8090 |
-- The corresponding records of task types are not controlled by design. |
| 8091 |
-- For the sake of completeness, create an empty Finalize_Address to be |
| 8092 |
-- used in task class-wide allocations. |
| 8093 |
|
| 8094 |
if Is_Task then |
| 8095 |
null; |
| 8096 |
|
| 8097 |
-- Nothing to do if the type does not need finalization or already has |
| 8098 |
-- a TSS entry for Finalize_Address. Skip class-wide subtypes that do |
| 8099 |
-- not come from source, as they are usually generated for completeness |
| 8100 |
-- and need no Finalize_Address. |
| 8101 |
|
| 8102 |
elsif not Needs_Finalization (Typ) |
| 8103 |
or else Present (TSS (Typ, TSS_Finalize_Address)) |
| 8104 |
or else |
| 8105 |
(Is_Class_Wide_Type (Typ) |
| 8106 |
and then Ekind (Root_Type (Typ)) = E_Record_Subtype |
| 8107 |
and then not Comes_From_Source (Root_Type (Typ))) |
| 8108 |
then |
| 8109 |
return; |
| 8110 |
end if; |
| 8111 |
|
| 8112 |
-- Do not generate Finalize_Address routine for CodePeer |
| 8113 |
|
| 8114 |
if CodePeer_Mode then |
| 8115 |
return; |
| 8116 |
end if; |
| 8117 |
|
| 8118 |
Proc_Id := |
| 8119 |
Make_Defining_Identifier (Loc, |
| 8120 |
Make_TSS_Name (Typ, TSS_Finalize_Address)); |
| 8121 |
|
| 8122 |
-- Generate: |
| 8123 |
|
| 8124 |
-- procedure <Typ>FD (V : System.Address) is |
| 8125 |
-- begin |
| 8126 |
-- null; -- for tasks |
| 8127 |
|
| 8128 |
-- declare -- for all other types |
| 8129 |
-- type Pnn is access all Typ; |
| 8130 |
-- for Pnn'Storage_Size use 0; |
| 8131 |
-- begin |
| 8132 |
-- [Deep_]Finalize (Pnn (V).all); |
| 8133 |
-- end; |
| 8134 |
-- end TypFD; |
| 8135 |
|
| 8136 |
if Is_Task then |
| 8137 |
Stmts := New_List (Make_Null_Statement (Loc)); |
| 8138 |
else |
| 8139 |
Stmts := Make_Finalize_Address_Stmts (Typ); |
| 8140 |
end if; |
| 8141 |
|
| 8142 |
Discard_Node ( |
| 8143 |
Make_Subprogram_Body (Loc, |
| 8144 |
Specification => |
| 8145 |
Make_Procedure_Specification (Loc, |
| 8146 |
Defining_Unit_Name => Proc_Id, |
| 8147 |
|
| 8148 |
Parameter_Specifications => New_List ( |
| 8149 |
Make_Parameter_Specification (Loc, |
| 8150 |
Defining_Identifier => |
| 8151 |
Make_Defining_Identifier (Loc, Name_V), |
| 8152 |
Parameter_Type => |
| 8153 |
New_Occurrence_Of (RTE (RE_Address), Loc)))), |
| 8154 |
|
| 8155 |
Declarations => No_List, |
| 8156 |
|
| 8157 |
Handled_Statement_Sequence => |
| 8158 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 8159 |
Statements => Stmts))); |
| 8160 |
|
| 8161 |
-- If the type has relaxed semantics for finalization, the indirect |
| 8162 |
-- calls to Finalize_Address may be turned into direct ones and, in |
| 8163 |
-- this case, inlining them is generally profitable. |
| 8164 |
|
| 8165 |
if Has_Relaxed_Finalization (Typ) then |
| 8166 |
Set_Is_Inlined (Proc_Id); |
| 8167 |
end if; |
| 8168 |
|
| 8169 |
Set_TSS (Typ, Proc_Id); |
| 8170 |
end Make_Finalize_Address_Body; |
| 8171 |
|
| 8172 |
--------------------------------- |
| 8173 |
-- Make_Finalize_Address_Stmts -- |
| 8174 |
--------------------------------- |
| 8175 |
|
| 8176 |
function Make_Finalize_Address_Stmts (Typ : Entity_Id) return List_Id is |
| 8177 |
Loc : constant Source_Ptr := Sloc (Typ); |
| 8178 |
|
| 8179 |
Decls : List_Id; |
| 8180 |
Desig_Typ : Entity_Id; |
| 8181 |
Fin_Block : Node_Id; |
| 8182 |
Fin_Call : Node_Id; |
| 8183 |
Obj_Expr : Node_Id; |
| 8184 |
Ptr_Typ : Entity_Id; |
| 8185 |
|
| 8186 |
begin |
| 8187 |
-- Array types: picking the (unconstrained) base type as designated type |
| 8188 |
-- requires allocating the bounds alongside the data, so we only do this |
| 8189 |
-- when the first subtype itself was declared as unconstrained. |
| 8190 |
|
| 8191 |
if Is_Array_Type (Typ) then |
| 8192 |
if Is_Constrained (First_Subtype (Typ)) then |
| 8193 |
Desig_Typ := First_Subtype (Typ); |
| 8194 |
else |
| 8195 |
Desig_Typ := Base_Type (Typ); |
| 8196 |
end if; |
| 8197 |
|
| 8198 |
-- Class-wide types of constrained root types |
| 8199 |
|
| 8200 |
elsif Is_Class_Wide_Type (Typ) |
| 8201 |
and then Has_Discriminants (Root_Type (Typ)) |
| 8202 |
and then not |
| 8203 |
Is_Empty_Elmt_List (Discriminant_Constraint (Root_Type (Typ))) |
| 8204 |
then |
| 8205 |
declare |
| 8206 |
Parent_Typ : Entity_Id; |
| 8207 |
Parent_Utyp : Entity_Id; |
| 8208 |
|
| 8209 |
begin |
| 8210 |
-- Climb the parent type chain looking for a non-constrained type |
| 8211 |
|
| 8212 |
Parent_Typ := Root_Type (Typ); |
| 8213 |
while Parent_Typ /= Etype (Parent_Typ) |
| 8214 |
and then Has_Discriminants (Parent_Typ) |
| 8215 |
and then not |
| 8216 |
Is_Empty_Elmt_List (Discriminant_Constraint (Parent_Typ)) |
| 8217 |
loop |
| 8218 |
Parent_Typ := Etype (Parent_Typ); |
| 8219 |
end loop; |
| 8220 |
|
| 8221 |
-- Handle views created for tagged types with unknown |
| 8222 |
-- discriminants. |
| 8223 |
|
| 8224 |
if Is_Underlying_Record_View (Parent_Typ) then |
| 8225 |
Parent_Typ := Underlying_Record_View (Parent_Typ); |
| 8226 |
end if; |
| 8227 |
|
| 8228 |
Parent_Utyp := Underlying_Type (Parent_Typ); |
| 8229 |
|
| 8230 |
-- Handle views created for a synchronized private extension with |
| 8231 |
-- known, non-defaulted discriminants. In that case, parent_typ |
| 8232 |
-- will be the private extension, as it is the first "non |
| 8233 |
-- -constrained" type in the parent chain. Unfortunately, the |
| 8234 |
-- underlying type, being a protected or task type, is not the |
| 8235 |
-- "real" type needing finalization. Rather, the "corresponding |
| 8236 |
-- record type" should be the designated type here. In fact, TSS |
| 8237 |
-- finalizer generation is specifically skipped for the nominal |
| 8238 |
-- class-wide type of (the full view of) a concurrent type (see |
| 8239 |
-- exp_ch7.Expand_Freeze_Class_Wide_Type). If we don't designate |
| 8240 |
-- the underlying record (Tprot_typeVC), we will end up trying to |
| 8241 |
-- dispatch to prot_typeVDF from an incorrectly designated |
| 8242 |
-- Tprot_typeC, which is, of course, not actually a member of |
| 8243 |
-- prot_typeV'Class, and thus incompatible. |
| 8244 |
|
| 8245 |
if Ekind (Parent_Utyp) in Concurrent_Kind |
| 8246 |
and then Present (Corresponding_Record_Type (Parent_Utyp)) |
| 8247 |
then |
| 8248 |
Parent_Utyp := Corresponding_Record_Type (Parent_Utyp); |
| 8249 |
end if; |
| 8250 |
|
| 8251 |
Desig_Typ := Class_Wide_Type (Parent_Utyp); |
| 8252 |
end; |
| 8253 |
|
| 8254 |
-- General case |
| 8255 |
|
| 8256 |
else |
| 8257 |
Desig_Typ := Typ; |
| 8258 |
end if; |
| 8259 |
|
| 8260 |
-- Generate: |
| 8261 |
-- type Ptr_Typ is access all Typ; |
| 8262 |
-- for Ptr_Typ'Storage_Size use 0; |
| 8263 |
|
| 8264 |
Ptr_Typ := Make_Temporary (Loc, 'P'); |
| 8265 |
|
| 8266 |
Decls := New_List ( |
| 8267 |
Make_Full_Type_Declaration (Loc, |
| 8268 |
Defining_Identifier => Ptr_Typ, |
| 8269 |
Type_Definition => |
| 8270 |
Make_Access_To_Object_Definition (Loc, |
| 8271 |
All_Present => True, |
| 8272 |
Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc))), |
| 8273 |
|
| 8274 |
Make_Attribute_Definition_Clause (Loc, |
| 8275 |
Name => New_Occurrence_Of (Ptr_Typ, Loc), |
| 8276 |
Chars => Name_Storage_Size, |
| 8277 |
Expression => Make_Integer_Literal (Loc, 0))); |
| 8278 |
|
| 8279 |
Obj_Expr := Make_Identifier (Loc, Name_V); |
| 8280 |
|
| 8281 |
-- Unconstrained arrays require special processing in order to retrieve |
| 8282 |
-- the elements. To achieve this, we have to skip the dope vector which |
| 8283 |
-- lays in front of the elements and then use a thin pointer to perform |
| 8284 |
-- the address-to-access conversion. |
| 8285 |
|
| 8286 |
if Is_Array_Type (Typ) and then not Is_Constrained (Desig_Typ) then |
| 8287 |
Obj_Expr := |
| 8288 |
Shift_Address_For_Descriptor (Obj_Expr, Desig_Typ, Name_Op_Add); |
| 8289 |
|
| 8290 |
-- Ensure that Ptr_Typ is a thin pointer; generate: |
| 8291 |
-- for Ptr_Typ'Size use System.Address'Size; |
| 8292 |
|
| 8293 |
Append_To (Decls, |
| 8294 |
Make_Attribute_Definition_Clause (Loc, |
| 8295 |
Name => New_Occurrence_Of (Ptr_Typ, Loc), |
| 8296 |
Chars => Name_Size, |
| 8297 |
Expression => Make_Integer_Literal (Loc, System_Address_Size))); |
| 8298 |
end if; |
| 8299 |
|
| 8300 |
Fin_Call := |
| 8301 |
Make_Final_Call ( |
| 8302 |
Obj_Ref => |
| 8303 |
Make_Explicit_Dereference (Loc, |
| 8304 |
Prefix => Unchecked_Convert_To (Ptr_Typ, Obj_Expr)), |
| 8305 |
Typ => Desig_Typ); |
| 8306 |
|
| 8307 |
if Present (Fin_Call) then |
| 8308 |
Fin_Block := |
| 8309 |
Make_Block_Statement (Loc, |
| 8310 |
Declarations => Decls, |
| 8311 |
Handled_Statement_Sequence => |
| 8312 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 8313 |
Statements => New_List (Fin_Call))); |
| 8314 |
|
| 8315 |
-- Otherwise previous errors or a missing full view may prevent the |
| 8316 |
-- proper freezing of the designated type. If this is the case, there |
| 8317 |
-- is no [Deep_]Finalize primitive to call. |
| 8318 |
|
| 8319 |
else |
| 8320 |
Fin_Block := Make_Null_Statement (Loc); |
| 8321 |
end if; |
| 8322 |
|
| 8323 |
return New_List (Fin_Block); |
| 8324 |
end Make_Finalize_Address_Stmts; |
| 8325 |
|
| 8326 |
--------------------------------- |
| 8327 |
-- Make_Finalize_Call_For_Node -- |
| 8328 |
--------------------------------- |
| 8329 |
|
| 8330 |
function Make_Finalize_Call_For_Node |
| 8331 |
(Loc : Source_Ptr; |
| 8332 |
Node : Entity_Id) return Node_Id |
| 8333 |
is |
| 8334 |
Fin_Id : constant Entity_Id := Finalize_Address_For_Node (Node); |
| 8335 |
|
| 8336 |
Fin_Call : Node_Id; |
| 8337 |
Fin_Ref : Node_Id; |
| 8338 |
|
| 8339 |
begin |
| 8340 |
-- Finalize_Address is not generated in CodePeer mode because the |
| 8341 |
-- body contains address arithmetic. So we don't want to generate |
| 8342 |
-- the call in this case. |
| 8343 |
|
| 8344 |
if CodePeer_Mode then |
| 8345 |
return Make_Null_Statement (Loc); |
| 8346 |
end if; |
| 8347 |
|
| 8348 |
-- The Finalize_Address primitive may be missing when the Master_Node |
| 8349 |
-- is written down in the source code for testing purposes. |
| 8350 |
|
| 8351 |
if Present (Fin_Id) then |
| 8352 |
Fin_Ref := |
| 8353 |
Make_Attribute_Reference (Loc, |
| 8354 |
Prefix => New_Occurrence_Of (Fin_Id, Loc), |
| 8355 |
Attribute_Name => Name_Unrestricted_Access); |
| 8356 |
|
| 8357 |
else |
| 8358 |
Fin_Ref := |
| 8359 |
Make_Selected_Component (Loc, |
| 8360 |
Prefix => New_Occurrence_Of (Node, Loc), |
| 8361 |
Selector_Name => Make_Identifier (Loc, Name_Finalize_Address)); |
| 8362 |
end if; |
| 8363 |
|
| 8364 |
Fin_Call := |
| 8365 |
Make_Procedure_Call_Statement (Loc, |
| 8366 |
Name => |
| 8367 |
New_Occurrence_Of (RTE (RE_Finalize_Object), Loc), |
| 8368 |
Parameter_Associations => New_List ( |
| 8369 |
New_Occurrence_Of (Node, Loc), |
| 8370 |
Fin_Ref)); |
| 8371 |
|
| 8372 |
-- Present Finalize_Address procedure to the back end so that it can |
| 8373 |
-- inline the call to the procedure made by Finalize_Object. |
| 8374 |
|
| 8375 |
if Present (Fin_Id) and then Is_Inlined (Fin_Id) then |
| 8376 |
Add_Inlined_Body (Fin_Id, Fin_Call); |
| 8377 |
end if; |
| 8378 |
|
| 8379 |
return Fin_Call; |
| 8380 |
end Make_Finalize_Call_For_Node; |
| 8381 |
|
| 8382 |
------------------------------------- |
| 8383 |
-- Make_Handler_For_Ctrl_Operation -- |
| 8384 |
------------------------------------- |
| 8385 |
|
| 8386 |
-- Generate: |
| 8387 |
|
| 8388 |
-- when E : others => |
| 8389 |
-- Raise_From_Controlled_Operation (E); |
| 8390 |
|
| 8391 |
-- or: |
| 8392 |
|
| 8393 |
-- when others => |
| 8394 |
-- raise Program_Error [finalize raised exception]; |
| 8395 |
|
| 8396 |
-- depending on whether Raise_From_Controlled_Operation is available |
| 8397 |
|
| 8398 |
function Make_Handler_For_Ctrl_Operation |
| 8399 |
(Loc : Source_Ptr) return Node_Id |
| 8400 |
is |
| 8401 |
E_Occ : Entity_Id; |
| 8402 |
-- Choice parameter (for the first case above) |
| 8403 |
|
| 8404 |
Raise_Node : Node_Id; |
| 8405 |
-- Procedure call or raise statement |
| 8406 |
|
| 8407 |
begin |
| 8408 |
-- Standard run-time: add choice parameter E and pass it to |
| 8409 |
-- Raise_From_Controlled_Operation so that the original exception |
| 8410 |
-- name and message can be recorded in the exception message for |
| 8411 |
-- Program_Error. |
| 8412 |
|
| 8413 |
if RTE_Available (RE_Raise_From_Controlled_Operation) then |
| 8414 |
E_Occ := Make_Defining_Identifier (Loc, Name_E); |
| 8415 |
Raise_Node := |
| 8416 |
Make_Procedure_Call_Statement (Loc, |
| 8417 |
Name => |
| 8418 |
New_Occurrence_Of |
| 8419 |
(RTE (RE_Raise_From_Controlled_Operation), Loc), |
| 8420 |
Parameter_Associations => New_List ( |
| 8421 |
New_Occurrence_Of (E_Occ, Loc))); |
| 8422 |
|
| 8423 |
-- Restricted run-time: exception messages are not supported |
| 8424 |
|
| 8425 |
else |
| 8426 |
E_Occ := Empty; |
| 8427 |
Raise_Node := |
| 8428 |
Make_Raise_Program_Error (Loc, |
| 8429 |
Reason => PE_Finalize_Raised_Exception); |
| 8430 |
end if; |
| 8431 |
|
| 8432 |
return |
| 8433 |
Make_Implicit_Exception_Handler (Loc, |
| 8434 |
Exception_Choices => New_List (Make_Others_Choice (Loc)), |
| 8435 |
Choice_Parameter => E_Occ, |
| 8436 |
Statements => New_List (Raise_Node)); |
| 8437 |
end Make_Handler_For_Ctrl_Operation; |
| 8438 |
|
| 8439 |
-------------------- |
| 8440 |
-- Make_Init_Call -- |
| 8441 |
-------------------- |
| 8442 |
|
| 8443 |
function Make_Init_Call |
| 8444 |
(Obj_Ref : Node_Id; |
| 8445 |
Typ : Entity_Id) return Node_Id |
| 8446 |
is |
| 8447 |
Loc : constant Source_Ptr := Sloc (Obj_Ref); |
| 8448 |
Is_Conc : Boolean; |
| 8449 |
Proc : Entity_Id; |
| 8450 |
Ref : Node_Id; |
| 8451 |
Utyp : Entity_Id; |
| 8452 |
|
| 8453 |
begin |
| 8454 |
Ref := Obj_Ref; |
| 8455 |
|
| 8456 |
-- Deal with the type and object reference. Depending on the context, an |
| 8457 |
-- object reference may need several conversions. |
| 8458 |
|
| 8459 |
if Is_Concurrent_Type (Typ) then |
| 8460 |
Is_Conc := True; |
| 8461 |
Utyp := Corresponding_Record_Type (Typ); |
| 8462 |
Ref := Convert_Concurrent (Ref, Typ); |
| 8463 |
|
| 8464 |
elsif Is_Private_Type (Typ) |
| 8465 |
and then Present (Full_View (Typ)) |
| 8466 |
and then Is_Concurrent_Type (Underlying_Type (Typ)) |
| 8467 |
then |
| 8468 |
Is_Conc := True; |
| 8469 |
Utyp := Corresponding_Record_Type (Underlying_Type (Typ)); |
| 8470 |
Ref := Convert_Concurrent (Ref, Underlying_Type (Typ)); |
| 8471 |
|
| 8472 |
else |
| 8473 |
Is_Conc := False; |
| 8474 |
Utyp := Typ; |
| 8475 |
end if; |
| 8476 |
|
| 8477 |
Utyp := Underlying_Type (Base_Type (Utyp)); |
| 8478 |
Set_Assignment_OK (Ref); |
| 8479 |
|
| 8480 |
-- Deal with untagged derivation of private views |
| 8481 |
|
| 8482 |
if Is_Untagged_Derivation (Typ) |
| 8483 |
and then not Is_Conc |
| 8484 |
and then Is_Implicit_Full_View (Utyp) |
| 8485 |
then |
| 8486 |
Utyp := Underlying_Type (Root_Type (Base_Type (Typ))); |
| 8487 |
Ref := Unchecked_Convert_To (Utyp, Ref); |
| 8488 |
|
| 8489 |
-- The following is to prevent problems with UC see 1.156 RH ??? |
| 8490 |
|
| 8491 |
Set_Assignment_OK (Ref); |
| 8492 |
end if; |
| 8493 |
|
| 8494 |
-- If the underlying_type is a subtype, then we are dealing with the |
| 8495 |
-- completion of a private type. We need to access the base type and |
| 8496 |
-- generate a conversion to it. |
| 8497 |
|
| 8498 |
if Present (Utyp) and then Utyp /= Base_Type (Utyp) then |
| 8499 |
pragma Assert (Is_Private_Type (Typ)); |
| 8500 |
Utyp := Base_Type (Utyp); |
| 8501 |
Ref := Unchecked_Convert_To (Utyp, Ref); |
| 8502 |
end if; |
| 8503 |
|
| 8504 |
-- The underlying type may not be present due to a missing full view. |
| 8505 |
-- In this case freezing did not take place and there is no suitable |
| 8506 |
-- [Deep_]Initialize primitive to call. |
| 8507 |
-- If Typ is protected then no additional processing is needed either. |
| 8508 |
|
| 8509 |
if No (Utyp) |
| 8510 |
or else Is_Protected_Type (Typ) |
| 8511 |
then |
| 8512 |
return Empty; |
| 8513 |
end if; |
| 8514 |
|
| 8515 |
-- Select the appropriate version of initialize |
| 8516 |
|
| 8517 |
if Has_Controlled_Component (Utyp) then |
| 8518 |
Proc := TSS (Utyp, TSS_Deep_Initialize); |
| 8519 |
elsif Is_Mutably_Tagged_Type (Utyp) then |
| 8520 |
Proc := Find_Controlled_Prim_Op (Etype (Utyp), Name_Initialize); |
| 8521 |
Check_Visibly_Controlled (Initialize_Case, Etype (Typ), Proc, Ref); |
| 8522 |
else |
| 8523 |
Proc := Find_Controlled_Prim_Op (Utyp, Name_Initialize); |
| 8524 |
Check_Visibly_Controlled (Initialize_Case, Typ, Proc, Ref); |
| 8525 |
end if; |
| 8526 |
|
| 8527 |
-- If initialization procedure for an array of controlled objects is |
| 8528 |
-- trivial, do not generate a useless call to it. |
| 8529 |
-- The initialization procedure may be missing altogether in the case |
| 8530 |
-- of a derived container whose components have trivial initialization. |
| 8531 |
|
| 8532 |
if No (Proc) |
| 8533 |
or else (Is_Array_Type (Utyp) and then Is_Trivial_Subprogram (Proc)) |
| 8534 |
or else |
| 8535 |
(not Comes_From_Source (Proc) |
| 8536 |
and then Present (Alias (Proc)) |
| 8537 |
and then Is_Trivial_Subprogram (Alias (Proc))) |
| 8538 |
then |
| 8539 |
return Empty; |
| 8540 |
end if; |
| 8541 |
|
| 8542 |
-- The object reference may need another conversion depending on the |
| 8543 |
-- type of the formal and that of the actual. |
| 8544 |
|
| 8545 |
Ref := Convert_View (Proc, Ref, Typ); |
| 8546 |
|
| 8547 |
-- Generate: |
| 8548 |
-- [Deep_]Initialize (Ref); |
| 8549 |
|
| 8550 |
return |
| 8551 |
Make_Procedure_Call_Statement (Loc, |
| 8552 |
Name => New_Occurrence_Of (Proc, Loc), |
| 8553 |
Parameter_Associations => New_List (Ref)); |
| 8554 |
end Make_Init_Call; |
| 8555 |
|
| 8556 |
------------------------------ |
| 8557 |
-- Make_Local_Deep_Finalize -- |
| 8558 |
------------------------------ |
| 8559 |
|
| 8560 |
function Make_Local_Deep_Finalize |
| 8561 |
(Typ : Entity_Id; |
| 8562 |
Nam : Entity_Id) return Node_Id |
| 8563 |
is |
| 8564 |
Loc : constant Source_Ptr := Sloc (Typ); |
| 8565 |
Formals : List_Id; |
| 8566 |
|
| 8567 |
begin |
| 8568 |
Formals := New_List ( |
| 8569 |
|
| 8570 |
-- V : in out Typ |
| 8571 |
|
| 8572 |
Make_Parameter_Specification (Loc, |
| 8573 |
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V), |
| 8574 |
In_Present => True, |
| 8575 |
Out_Present => True, |
| 8576 |
Parameter_Type => New_Occurrence_Of (Typ, Loc)), |
| 8577 |
|
| 8578 |
-- F : Boolean := True |
| 8579 |
|
| 8580 |
Make_Parameter_Specification (Loc, |
| 8581 |
Defining_Identifier => Make_Defining_Identifier (Loc, Name_F), |
| 8582 |
Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc), |
| 8583 |
Expression => New_Occurrence_Of (Standard_True, Loc))); |
| 8584 |
|
| 8585 |
-- Add the necessary number of counters to represent the initialization |
| 8586 |
-- state of an object. |
| 8587 |
|
| 8588 |
return |
| 8589 |
Make_Subprogram_Body (Loc, |
| 8590 |
Specification => |
| 8591 |
Make_Procedure_Specification (Loc, |
| 8592 |
Defining_Unit_Name => Nam, |
| 8593 |
Parameter_Specifications => Formals), |
| 8594 |
|
| 8595 |
Declarations => No_List, |
| 8596 |
|
| 8597 |
Handled_Statement_Sequence => |
| 8598 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 8599 |
Statements => Make_Deep_Record_Body (Finalize_Case, Typ, True))); |
| 8600 |
end Make_Local_Deep_Finalize; |
| 8601 |
|
| 8602 |
---------------------------------- |
| 8603 |
-- Make_Master_Node_Declaration -- |
| 8604 |
---------------------------------- |
| 8605 |
|
| 8606 |
function Make_Master_Node_Declaration |
| 8607 |
(Loc : Source_Ptr; |
| 8608 |
Master_Node : Entity_Id; |
| 8609 |
Obj : Entity_Id) return Node_Id |
| 8610 |
is |
| 8611 |
begin |
| 8612 |
Set_Finalization_Master_Node (Obj, Master_Node); |
| 8613 |
|
| 8614 |
return |
| 8615 |
Make_Object_Declaration (Loc, |
| 8616 |
Defining_Identifier => Master_Node, |
| 8617 |
Aliased_Present => True, |
| 8618 |
Object_Definition => |
| 8619 |
New_Occurrence_Of (RTE (RE_Master_Node), Loc)); |
| 8620 |
end Make_Master_Node_Declaration; |
| 8621 |
|
| 8622 |
---------------------------------------- |
| 8623 |
-- Make_Suppress_Object_Finalize_Call -- |
| 8624 |
---------------------------------------- |
| 8625 |
|
| 8626 |
function Make_Suppress_Object_Finalize_Call |
| 8627 |
(Loc : Source_Ptr; |
| 8628 |
Obj : Entity_Id) return Node_Id |
| 8629 |
is |
| 8630 |
Obj_Decl : constant Node_Id := Declaration_Node (Obj); |
| 8631 |
|
| 8632 |
Master_Node_Decl : Node_Id; |
| 8633 |
Master_Node_Id : Entity_Id; |
| 8634 |
|
| 8635 |
begin |
| 8636 |
-- Create the declaration of the Master_Node for the object and |
| 8637 |
-- insert it before the declaration of the object itself. |
| 8638 |
|
| 8639 |
if Present (Finalization_Master_Node (Obj)) then |
| 8640 |
Master_Node_Id := Finalization_Master_Node (Obj); |
| 8641 |
|
| 8642 |
else |
| 8643 |
Master_Node_Id := Make_Temporary (Loc, 'N'); |
| 8644 |
Master_Node_Decl := |
| 8645 |
Make_Master_Node_Declaration (Loc, Master_Node_Id, Obj); |
| 8646 |
Insert_Before_And_Analyze (Obj_Decl, Master_Node_Decl); |
| 8647 |
|
| 8648 |
-- Generate the attachment of the object to the Master_Node |
| 8649 |
|
| 8650 |
Attach_Object_To_Master_Node (Obj_Decl, Master_Node_Id); |
| 8651 |
|
| 8652 |
-- Mark the object to avoid double finalization |
| 8653 |
|
| 8654 |
Set_Is_Ignored_For_Finalization (Obj); |
| 8655 |
end if; |
| 8656 |
|
| 8657 |
return |
| 8658 |
Make_Procedure_Call_Statement (Loc, |
| 8659 |
Name => |
| 8660 |
New_Occurrence_Of (RTE (RE_Suppress_Object_Finalize_At_End), Loc), |
| 8661 |
Parameter_Associations => New_List ( |
| 8662 |
New_Occurrence_Of (Master_Node_Id, Loc))); |
| 8663 |
end Make_Suppress_Object_Finalize_Call; |
| 8664 |
|
| 8665 |
-------------------------- |
| 8666 |
-- Make_Transient_Block -- |
| 8667 |
-------------------------- |
| 8668 |
|
| 8669 |
function Make_Transient_Block |
| 8670 |
(Loc : Source_Ptr; |
| 8671 |
Action : Node_Id; |
| 8672 |
Par : Node_Id) return Node_Id |
| 8673 |
is |
| 8674 |
function Manages_Sec_Stack (Id : Entity_Id) return Boolean; |
| 8675 |
-- Determine whether scoping entity Id manages the secondary stack |
| 8676 |
|
| 8677 |
function Within_Loop_Statement (N : Node_Id) return Boolean; |
| 8678 |
-- Return True when N appears within a loop and no block is containing N |
| 8679 |
|
| 8680 |
----------------------- |
| 8681 |
-- Manages_Sec_Stack -- |
| 8682 |
----------------------- |
| 8683 |
|
| 8684 |
function Manages_Sec_Stack (Id : Entity_Id) return Boolean is |
| 8685 |
begin |
| 8686 |
case Ekind (Id) is |
| 8687 |
|
| 8688 |
-- An exception handler with a choice parameter utilizes a dummy |
| 8689 |
-- block to provide a declarative region. Such a block should not |
| 8690 |
-- be considered because it never manifests in the tree and can |
| 8691 |
-- never release the secondary stack. |
| 8692 |
|
| 8693 |
when E_Block => |
| 8694 |
return |
| 8695 |
Uses_Sec_Stack (Id) and then not Is_Exception_Handler (Id); |
| 8696 |
|
| 8697 |
when E_Entry |
| 8698 |
| E_Entry_Family |
| 8699 |
| E_Function |
| 8700 |
| E_Procedure |
| 8701 |
=> |
| 8702 |
return Uses_Sec_Stack (Id); |
| 8703 |
|
| 8704 |
when others => |
| 8705 |
return False; |
| 8706 |
end case; |
| 8707 |
end Manages_Sec_Stack; |
| 8708 |
|
| 8709 |
--------------------------- |
| 8710 |
-- Within_Loop_Statement -- |
| 8711 |
--------------------------- |
| 8712 |
|
| 8713 |
function Within_Loop_Statement (N : Node_Id) return Boolean is |
| 8714 |
Par : Node_Id := Parent (N); |
| 8715 |
|
| 8716 |
begin |
| 8717 |
while Nkind (Par) not in |
| 8718 |
N_Handled_Sequence_Of_Statements | N_Loop_Statement | |
| 8719 |
N_Package_Specification | N_Proper_Body |
| 8720 |
loop |
| 8721 |
pragma Assert (Present (Par)); |
| 8722 |
Par := Parent (Par); |
| 8723 |
end loop; |
| 8724 |
|
| 8725 |
return Nkind (Par) = N_Loop_Statement; |
| 8726 |
end Within_Loop_Statement; |
| 8727 |
|
| 8728 |
-- Local variables |
| 8729 |
|
| 8730 |
Decls : constant List_Id := New_List; |
| 8731 |
Instrs : constant List_Id := New_List (Action); |
| 8732 |
Trans_Id : constant Entity_Id := Current_Scope; |
| 8733 |
|
| 8734 |
Block : Node_Id; |
| 8735 |
Insert : Node_Id; |
| 8736 |
Scop : Entity_Id; |
| 8737 |
|
| 8738 |
-- Start of processing for Make_Transient_Block |
| 8739 |
|
| 8740 |
begin |
| 8741 |
-- Even though the transient block is tasked with managing the secondary |
| 8742 |
-- stack, the block may forgo this functionality depending on how the |
| 8743 |
-- secondary stack is managed by enclosing scopes. |
| 8744 |
|
| 8745 |
if Manages_Sec_Stack (Trans_Id) then |
| 8746 |
|
| 8747 |
-- Determine whether an enclosing scope already manages the secondary |
| 8748 |
-- stack. |
| 8749 |
|
| 8750 |
Scop := Scope (Trans_Id); |
| 8751 |
while Present (Scop) loop |
| 8752 |
|
| 8753 |
-- It should not be possible to reach Standard without hitting one |
| 8754 |
-- of the other cases first unless Standard was manually pushed. |
| 8755 |
|
| 8756 |
if Scop = Standard_Standard then |
| 8757 |
exit; |
| 8758 |
|
| 8759 |
-- The transient block is within a function which returns on the |
| 8760 |
-- secondary stack. Take a conservative approach and assume that |
| 8761 |
-- the value on the secondary stack is part of the result. Note |
| 8762 |
-- that it is not possible to detect this dependency without flow |
| 8763 |
-- analysis which the compiler does not have. Letting the object |
| 8764 |
-- live longer than the transient block will not leak any memory |
| 8765 |
-- because the caller will reclaim the total storage used by the |
| 8766 |
-- function. |
| 8767 |
|
| 8768 |
elsif Ekind (Scop) = E_Function |
| 8769 |
and then Sec_Stack_Needed_For_Return (Scop) |
| 8770 |
then |
| 8771 |
Set_Uses_Sec_Stack (Trans_Id, False); |
| 8772 |
exit; |
| 8773 |
|
| 8774 |
-- The transient block must manage the secondary stack when the |
| 8775 |
-- block appears within a loop in order to reclaim the memory at |
| 8776 |
-- each iteration. |
| 8777 |
|
| 8778 |
elsif Ekind (Scop) = E_Loop then |
| 8779 |
exit; |
| 8780 |
|
| 8781 |
-- Ditto when the block appears without a block that does not |
| 8782 |
-- manage the secondary stack and is located within a loop. |
| 8783 |
|
| 8784 |
elsif Ekind (Scop) = E_Block |
| 8785 |
and then not Manages_Sec_Stack (Scop) |
| 8786 |
and then Present (Block_Node (Scop)) |
| 8787 |
and then Within_Loop_Statement (Block_Node (Scop)) |
| 8788 |
then |
| 8789 |
exit; |
| 8790 |
|
| 8791 |
-- The transient block does not need to manage the secondary stack |
| 8792 |
-- when there is an enclosing construct which already does that. |
| 8793 |
-- This optimization saves on SS_Mark and SS_Release calls but may |
| 8794 |
-- allow objects to live a little longer than required. |
| 8795 |
|
| 8796 |
-- The transient block must manage the secondary stack when switch |
| 8797 |
-- -gnatd.s (strict management) is in effect. |
| 8798 |
|
| 8799 |
elsif Manages_Sec_Stack (Scop) and then not Debug_Flag_Dot_S then |
| 8800 |
Set_Uses_Sec_Stack (Trans_Id, False); |
| 8801 |
exit; |
| 8802 |
|
| 8803 |
-- Prevent the search from going too far because transient blocks |
| 8804 |
-- are bounded by packages and subprogram scopes. |
| 8805 |
|
| 8806 |
elsif Ekind (Scop) in E_Entry |
| 8807 |
| E_Entry_Family |
| 8808 |
| E_Function |
| 8809 |
| E_Package |
| 8810 |
| E_Procedure |
| 8811 |
| E_Subprogram_Body |
| 8812 |
then |
| 8813 |
exit; |
| 8814 |
end if; |
| 8815 |
|
| 8816 |
Scop := Scope (Scop); |
| 8817 |
end loop; |
| 8818 |
end if; |
| 8819 |
|
| 8820 |
-- Create the transient block. Set the parent now since the block itself |
| 8821 |
-- is not part of the tree. The current scope is the E_Block entity that |
| 8822 |
-- has been pushed by Establish_Transient_Scope. |
| 8823 |
|
| 8824 |
pragma Assert (Ekind (Trans_Id) = E_Block); |
| 8825 |
|
| 8826 |
Block := |
| 8827 |
Make_Block_Statement (Loc, |
| 8828 |
Identifier => New_Occurrence_Of (Trans_Id, Loc), |
| 8829 |
Declarations => Decls, |
| 8830 |
Handled_Statement_Sequence => |
| 8831 |
Make_Handled_Sequence_Of_Statements (Loc, Statements => Instrs), |
| 8832 |
Has_Created_Identifier => True); |
| 8833 |
Set_Parent (Block, Par); |
| 8834 |
|
| 8835 |
-- Insert actions stuck in the transient scopes as well as all freezing |
| 8836 |
-- nodes needed by those actions. Do not insert cleanup actions here, |
| 8837 |
-- they will be transferred to the newly created block. |
| 8838 |
|
| 8839 |
Insert_Actions_In_Scope_Around |
| 8840 |
(Action, Clean => False, Manage_SS => False); |
| 8841 |
|
| 8842 |
Insert := Prev (Action); |
| 8843 |
|
| 8844 |
if Present (Insert) then |
| 8845 |
Freeze_All (First_Entity (Trans_Id), Insert); |
| 8846 |
end if; |
| 8847 |
|
| 8848 |
-- Transfer cleanup actions to the newly created block |
| 8849 |
|
| 8850 |
declare |
| 8851 |
Cleanup_Actions : List_Id |
| 8852 |
renames Scope_Stack.Table (Scope_Stack.Last). |
| 8853 |
Actions_To_Be_Wrapped (Cleanup); |
| 8854 |
begin |
| 8855 |
Set_Cleanup_Actions (Block, Cleanup_Actions); |
| 8856 |
Cleanup_Actions := No_List; |
| 8857 |
end; |
| 8858 |
|
| 8859 |
-- When the transient scope was established, we pushed the entry for the |
| 8860 |
-- transient scope onto the scope stack, so that the scope was active |
| 8861 |
-- for the installation of finalizable entities etc. Now we must remove |
| 8862 |
-- this entry, since we have constructed a proper block. |
| 8863 |
|
| 8864 |
Pop_Scope; |
| 8865 |
|
| 8866 |
return Block; |
| 8867 |
end Make_Transient_Block; |
| 8868 |
|
| 8869 |
------------------------ |
| 8870 |
-- Node_To_Be_Wrapped -- |
| 8871 |
------------------------ |
| 8872 |
|
| 8873 |
function Node_To_Be_Wrapped return Node_Id is |
| 8874 |
begin |
| 8875 |
return Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped; |
| 8876 |
end Node_To_Be_Wrapped; |
| 8877 |
|
| 8878 |
---------------------------------- |
| 8879 |
-- Shift_Address_For_Descriptor -- |
| 8880 |
---------------------------------- |
| 8881 |
|
| 8882 |
function Shift_Address_For_Descriptor |
| 8883 |
(Addr : Node_Id; |
| 8884 |
Typ : Entity_Id; |
| 8885 |
Op_Nam : Name_Id) return Node_Id |
| 8886 |
is |
| 8887 |
Loc : constant Source_Ptr := Sloc (Addr); |
| 8888 |
Dummy : constant Entity_Id := RTE (RE_Storage_Offset); |
| 8889 |
-- Make sure System_Storage_Elements is loaded for RTU_Entity |
| 8890 |
|
| 8891 |
begin |
| 8892 |
-- Generate: |
| 8893 |
-- Addr +/- (Typ'Descriptor_Size / Storage_Unit) |
| 8894 |
|
| 8895 |
return |
| 8896 |
Make_Function_Call (Loc, |
| 8897 |
Name => |
| 8898 |
Make_Expanded_Name (Loc, |
| 8899 |
Chars => Op_Nam, |
| 8900 |
Prefix => |
| 8901 |
New_Occurrence_Of |
| 8902 |
(RTU_Entity (System_Storage_Elements), Loc), |
| 8903 |
Selector_Name => Make_Identifier (Loc, Op_Nam)), |
| 8904 |
Parameter_Associations => New_List ( |
| 8905 |
Addr, |
| 8906 |
Make_Op_Divide (Loc, |
| 8907 |
Left_Opnd => |
| 8908 |
Make_Attribute_Reference (Loc, |
| 8909 |
Prefix => New_Occurrence_Of (Typ, Loc), |
| 8910 |
Attribute_Name => Name_Descriptor_Size), |
| 8911 |
Right_Opnd => |
| 8912 |
Make_Integer_Literal (Loc, System_Storage_Unit)))); |
| 8913 |
end Shift_Address_For_Descriptor; |
| 8914 |
|
| 8915 |
---------------------------- |
| 8916 |
-- Store_Actions_In_Scope -- |
| 8917 |
---------------------------- |
| 8918 |
|
| 8919 |
procedure Store_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id) is |
| 8920 |
SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last); |
| 8921 |
Actions : List_Id renames SE.Actions_To_Be_Wrapped (AK); |
| 8922 |
|
| 8923 |
begin |
| 8924 |
if Is_Empty_List (Actions) then |
| 8925 |
Store_New_Actions_In_Scope (AK, L); |
| 8926 |
Analyze_List (L); |
| 8927 |
|
| 8928 |
elsif AK = Before then |
| 8929 |
Insert_List_After_And_Analyze (Last (Actions), L); |
| 8930 |
|
| 8931 |
else |
| 8932 |
Insert_List_Before_And_Analyze (First (Actions), L); |
| 8933 |
end if; |
| 8934 |
end Store_Actions_In_Scope; |
| 8935 |
|
| 8936 |
---------------------------------- |
| 8937 |
-- Store_After_Actions_In_Scope -- |
| 8938 |
---------------------------------- |
| 8939 |
|
| 8940 |
procedure Store_After_Actions_In_Scope (L : List_Id) is |
| 8941 |
begin |
| 8942 |
Store_Actions_In_Scope (After, L); |
| 8943 |
end Store_After_Actions_In_Scope; |
| 8944 |
|
| 8945 |
--------------------------------------------------- |
| 8946 |
-- Store_After_Actions_In_Scope_Without_Analysis -- |
| 8947 |
--------------------------------------------------- |
| 8948 |
|
| 8949 |
procedure Store_After_Actions_In_Scope_Without_Analysis (L : List_Id) is |
| 8950 |
SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last); |
| 8951 |
Actions : List_Id renames SE.Actions_To_Be_Wrapped (After); |
| 8952 |
|
| 8953 |
begin |
| 8954 |
if Is_Empty_List (Actions) then |
| 8955 |
Store_New_Actions_In_Scope (After, L); |
| 8956 |
else |
| 8957 |
Insert_List_Before (First (Actions), L); |
| 8958 |
end if; |
| 8959 |
end Store_After_Actions_In_Scope_Without_Analysis; |
| 8960 |
|
| 8961 |
----------------------------------- |
| 8962 |
-- Store_Before_Actions_In_Scope -- |
| 8963 |
----------------------------------- |
| 8964 |
|
| 8965 |
procedure Store_Before_Actions_In_Scope (L : List_Id) is |
| 8966 |
begin |
| 8967 |
Store_Actions_In_Scope (Before, L); |
| 8968 |
end Store_Before_Actions_In_Scope; |
| 8969 |
|
| 8970 |
----------------------------------- |
| 8971 |
-- Store_Cleanup_Actions_In_Scope -- |
| 8972 |
----------------------------------- |
| 8973 |
|
| 8974 |
procedure Store_Cleanup_Actions_In_Scope (L : List_Id) is |
| 8975 |
begin |
| 8976 |
Store_Actions_In_Scope (Cleanup, L); |
| 8977 |
end Store_Cleanup_Actions_In_Scope; |
| 8978 |
|
| 8979 |
-------------------------------- |
| 8980 |
-- Store_New_Actions_In_Scope -- |
| 8981 |
-------------------------------- |
| 8982 |
|
| 8983 |
procedure Store_New_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id) |
| 8984 |
is |
| 8985 |
SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last); |
| 8986 |
Actions : List_Id renames SE.Actions_To_Be_Wrapped (AK); |
| 8987 |
|
| 8988 |
begin |
| 8989 |
pragma Assert (Is_Empty_List (Actions)); |
| 8990 |
|
| 8991 |
Actions := L; |
| 8992 |
|
| 8993 |
-- Set the Parent link to provide the context for the actions |
| 8994 |
|
| 8995 |
if Is_List_Member (SE.Node_To_Be_Wrapped) then |
| 8996 |
Set_Parent (L, Parent (SE.Node_To_Be_Wrapped)); |
| 8997 |
else |
| 8998 |
Set_Parent (L, SE.Node_To_Be_Wrapped); |
| 8999 |
end if; |
| 9000 |
end Store_New_Actions_In_Scope; |
| 9001 |
|
| 9002 |
------------------ |
| 9003 |
-- Unnest_Block -- |
| 9004 |
------------------ |
| 9005 |
|
| 9006 |
procedure Unnest_Block (Decl : Node_Id) is |
| 9007 |
Loc : constant Source_Ptr := Sloc (Decl); |
| 9008 |
Ent : Entity_Id; |
| 9009 |
Local_Body : Node_Id; |
| 9010 |
Local_Call : Node_Id; |
| 9011 |
Local_Proc : Entity_Id; |
| 9012 |
Local_Scop : Entity_Id; |
| 9013 |
|
| 9014 |
begin |
| 9015 |
Local_Scop := Entity (Identifier (Decl)); |
| 9016 |
Ent := First_Entity (Local_Scop); |
| 9017 |
|
| 9018 |
Local_Proc := Make_Temporary (Loc, 'P'); |
| 9019 |
|
| 9020 |
Local_Body := |
| 9021 |
Make_Subprogram_Body (Loc, |
| 9022 |
Specification => |
| 9023 |
Make_Procedure_Specification (Loc, |
| 9024 |
Defining_Unit_Name => Local_Proc), |
| 9025 |
Declarations => Declarations (Decl), |
| 9026 |
Handled_Statement_Sequence => |
| 9027 |
Handled_Statement_Sequence (Decl), |
| 9028 |
At_End_Proc => New_Copy_Tree (At_End_Proc (Decl))); |
| 9029 |
|
| 9030 |
-- Handlers in the block may contain nested subprograms that require |
| 9031 |
-- unnesting. |
| 9032 |
|
| 9033 |
Check_Unnesting_In_Handlers (Local_Body); |
| 9034 |
|
| 9035 |
Rewrite (Decl, Local_Body); |
| 9036 |
Analyze (Decl); |
| 9037 |
Set_Has_Nested_Subprogram (Local_Proc); |
| 9038 |
|
| 9039 |
Local_Call := |
| 9040 |
Make_Procedure_Call_Statement (Loc, |
| 9041 |
Name => New_Occurrence_Of (Local_Proc, Loc)); |
| 9042 |
|
| 9043 |
Insert_After (Decl, Local_Call); |
| 9044 |
Analyze (Local_Call); |
| 9045 |
|
| 9046 |
-- The new subprogram has the same scope as the original block |
| 9047 |
|
| 9048 |
Set_Scope (Local_Proc, Scope (Local_Scop)); |
| 9049 |
|
| 9050 |
-- And the entity list of the new procedure is that of the block |
| 9051 |
|
| 9052 |
Set_First_Entity (Local_Proc, Ent); |
| 9053 |
|
| 9054 |
-- Reset the scopes of all the entities to the new procedure |
| 9055 |
|
| 9056 |
while Present (Ent) loop |
| 9057 |
Set_Scope (Ent, Local_Proc); |
| 9058 |
Next_Entity (Ent); |
| 9059 |
end loop; |
| 9060 |
end Unnest_Block; |
| 9061 |
|
| 9062 |
------------------------- |
| 9063 |
-- Unnest_If_Statement -- |
| 9064 |
------------------------- |
| 9065 |
|
| 9066 |
procedure Unnest_If_Statement (If_Stmt : Node_Id) is |
| 9067 |
|
| 9068 |
procedure Check_Stmts_For_Subp_Unnesting (Stmts : in out List_Id); |
| 9069 |
-- A list of statements (that may be a list associated with a then, |
| 9070 |
-- elsif, or else part of an if-statement) is traversed at the top |
| 9071 |
-- level to determine whether it contains a subprogram body, and if so, |
| 9072 |
-- the statements will be replaced with a new procedure body containing |
| 9073 |
-- the statements followed by a call to the procedure. The individual |
| 9074 |
-- statements may also be blocks, loops, or other if statements that |
| 9075 |
-- themselves may require contain nested subprograms needing unnesting. |
| 9076 |
|
| 9077 |
procedure Check_Stmts_For_Subp_Unnesting (Stmts : in out List_Id) is |
| 9078 |
Subp_Found : Boolean := False; |
| 9079 |
|
| 9080 |
begin |
| 9081 |
if Is_Empty_List (Stmts) then |
| 9082 |
return; |
| 9083 |
end if; |
| 9084 |
|
| 9085 |
declare |
| 9086 |
Stmt : Node_Id := First (Stmts); |
| 9087 |
begin |
| 9088 |
while Present (Stmt) loop |
| 9089 |
if Nkind (Stmt) = N_Subprogram_Body then |
| 9090 |
Subp_Found := True; |
| 9091 |
exit; |
| 9092 |
end if; |
| 9093 |
|
| 9094 |
Next (Stmt); |
| 9095 |
end loop; |
| 9096 |
end; |
| 9097 |
|
| 9098 |
-- The statements themselves may be blocks, loops, etc. that in turn |
| 9099 |
-- contain nested subprograms requiring an unnesting transformation. |
| 9100 |
-- We perform this traversal after looking for subprogram bodies, to |
| 9101 |
-- avoid considering procedures created for one of those statements |
| 9102 |
-- (such as a block rewritten as a procedure) as a nested subprogram |
| 9103 |
-- of the statement list (which could result in an unneeded wrapper |
| 9104 |
-- procedure). |
| 9105 |
|
| 9106 |
Check_Unnesting_In_Decls_Or_Stmts (Stmts); |
| 9107 |
|
| 9108 |
-- If there was a top-level subprogram body in the statement list, |
| 9109 |
-- then perform an unnesting transformation on the list by replacing |
| 9110 |
-- the statements with a wrapper procedure body containing the |
| 9111 |
-- original statements followed by a call to that procedure. |
| 9112 |
|
| 9113 |
if Subp_Found then |
| 9114 |
Unnest_Statement_List (Stmts); |
| 9115 |
end if; |
| 9116 |
end Check_Stmts_For_Subp_Unnesting; |
| 9117 |
|
| 9118 |
-- Local variables |
| 9119 |
|
| 9120 |
Then_Stmts : List_Id := Then_Statements (If_Stmt); |
| 9121 |
Else_Stmts : List_Id := Else_Statements (If_Stmt); |
| 9122 |
|
| 9123 |
-- Start of processing for Unnest_If_Statement |
| 9124 |
|
| 9125 |
begin |
| 9126 |
Check_Stmts_For_Subp_Unnesting (Then_Stmts); |
| 9127 |
Set_Then_Statements (If_Stmt, Then_Stmts); |
| 9128 |
|
| 9129 |
if not Is_Empty_List (Elsif_Parts (If_Stmt)) then |
| 9130 |
declare |
| 9131 |
Elsif_Part : Node_Id := |
| 9132 |
First (Elsif_Parts (If_Stmt)); |
| 9133 |
Elsif_Stmts : List_Id; |
| 9134 |
begin |
| 9135 |
while Present (Elsif_Part) loop |
| 9136 |
Elsif_Stmts := Then_Statements (Elsif_Part); |
| 9137 |
|
| 9138 |
Check_Stmts_For_Subp_Unnesting (Elsif_Stmts); |
| 9139 |
Set_Then_Statements (Elsif_Part, Elsif_Stmts); |
| 9140 |
|
| 9141 |
Next (Elsif_Part); |
| 9142 |
end loop; |
| 9143 |
end; |
| 9144 |
end if; |
| 9145 |
|
| 9146 |
Check_Stmts_For_Subp_Unnesting (Else_Stmts); |
| 9147 |
Set_Else_Statements (If_Stmt, Else_Stmts); |
| 9148 |
end Unnest_If_Statement; |
| 9149 |
|
| 9150 |
----------------- |
| 9151 |
-- Unnest_Loop -- |
| 9152 |
----------------- |
| 9153 |
|
| 9154 |
procedure Unnest_Loop (Loop_Stmt : Node_Id) is |
| 9155 |
|
| 9156 |
procedure Fixup_Inner_Scopes (Loop_Or_Block : Node_Id); |
| 9157 |
-- This procedure fixes the scope for 2 identified cases of incorrect |
| 9158 |
-- scope information. |
| 9159 |
-- |
| 9160 |
-- 1) The loops created by the compiler for array aggregates can have |
| 9161 |
-- nested finalization procedure when the type of the array components |
| 9162 |
-- needs finalization. It has the following form: |
| 9163 |
|
| 9164 |
-- for J4b in 10 .. 12 loop |
| 9165 |
-- declare |
| 9166 |
-- procedure __finalizer; |
| 9167 |
-- begin |
| 9168 |
-- procedure __finalizer is |
| 9169 |
-- ... |
| 9170 |
-- end; |
| 9171 |
-- ... |
| 9172 |
-- obj (J4b) := ...; |
| 9173 |
|
| 9174 |
-- When the compiler creates the N_Block_Statement, it sets its scope to |
| 9175 |
-- the outer scope (the one containing the loop). |
| 9176 |
|
| 9177 |
-- The Unnest_Loop procedure moves the N_Loop_Statement inside a new |
| 9178 |
-- procedure and correctly sets the scopes for both the new procedure |
| 9179 |
-- and the loop entity. The inner block scope is not modified and this |
| 9180 |
-- leaves the Tree in an incoherent state (i.e. the inner procedure must |
| 9181 |
-- have its enclosing procedure in its scope ancestries). |
| 9182 |
|
| 9183 |
-- 2) The second case happens when an object declaration is created |
| 9184 |
-- within a loop used to initialize the 'others' components of an |
| 9185 |
-- aggregate that is nested within a transient scope. When the transient |
| 9186 |
-- scope is removed, the object scope is set to the outer scope. For |
| 9187 |
-- example: |
| 9188 |
|
| 9189 |
-- package pack |
| 9190 |
-- ... |
| 9191 |
-- L98s : for J90s in 2 .. 19 loop |
| 9192 |
-- B101s : declare |
| 9193 |
-- R92s : aliased some_type; |
| 9194 |
-- ... |
| 9195 |
|
| 9196 |
-- The loop L98s was initially wrapped in a transient scope B72s and |
| 9197 |
-- R92s was nested within it. Then the transient scope is removed and |
| 9198 |
-- the scope of R92s is set to 'pack'. And finally, when the unnester |
| 9199 |
-- moves the loop body in a new procedure, R92s's scope is still left |
| 9200 |
-- unchanged. |
| 9201 |
|
| 9202 |
-- This procedure finds the two previous patterns and fixes the scope |
| 9203 |
-- information. |
| 9204 |
|
| 9205 |
-- Another (better) fix would be to have the block scope set to be the |
| 9206 |
-- loop entity earlier (when the block is created or when the loop gets |
| 9207 |
-- an actual entity set). But unfortunately this proved harder to |
| 9208 |
-- implement ??? |
| 9209 |
|
| 9210 |
procedure Fixup_Inner_Scopes (Loop_Or_Block : Node_Id) is |
| 9211 |
Stmt : Node_Id; |
| 9212 |
Loop_Or_Block_Ent : Entity_Id; |
| 9213 |
Ent_To_Fix : Entity_Id; |
| 9214 |
Decl : Node_Id := Empty; |
| 9215 |
begin |
| 9216 |
pragma Assert (Nkind (Loop_Or_Block) in |
| 9217 |
N_Loop_Statement | N_Block_Statement); |
| 9218 |
|
| 9219 |
Loop_Or_Block_Ent := Entity (Identifier (Loop_Or_Block)); |
| 9220 |
if Nkind (Loop_Or_Block) = N_Loop_Statement then |
| 9221 |
Stmt := First (Statements (Loop_Or_Block)); |
| 9222 |
else -- N_Block_Statement |
| 9223 |
Stmt := First |
| 9224 |
(Statements (Handled_Statement_Sequence (Loop_Or_Block))); |
| 9225 |
Decl := First (Declarations (Loop_Or_Block)); |
| 9226 |
end if; |
| 9227 |
|
| 9228 |
-- Fix scopes for any object declaration found in the block |
| 9229 |
while Present (Decl) loop |
| 9230 |
if Nkind (Decl) = N_Object_Declaration then |
| 9231 |
Ent_To_Fix := Defining_Identifier (Decl); |
| 9232 |
Set_Scope (Ent_To_Fix, Loop_Or_Block_Ent); |
| 9233 |
end if; |
| 9234 |
Next (Decl); |
| 9235 |
end loop; |
| 9236 |
|
| 9237 |
while Present (Stmt) loop |
| 9238 |
if Nkind (Stmt) = N_Block_Statement |
| 9239 |
and then Is_Abort_Block (Stmt) |
| 9240 |
then |
| 9241 |
Ent_To_Fix := Entity (Identifier (Stmt)); |
| 9242 |
Set_Scope (Ent_To_Fix, Loop_Or_Block_Ent); |
| 9243 |
elsif Nkind (Stmt) in N_Block_Statement | N_Loop_Statement |
| 9244 |
then |
| 9245 |
Fixup_Inner_Scopes (Stmt); |
| 9246 |
end if; |
| 9247 |
Next (Stmt); |
| 9248 |
end loop; |
| 9249 |
end Fixup_Inner_Scopes; |
| 9250 |
|
| 9251 |
Loc : constant Source_Ptr := Sloc (Loop_Stmt); |
| 9252 |
Ent : Entity_Id; |
| 9253 |
Local_Body : Node_Id; |
| 9254 |
Local_Call : Node_Id; |
| 9255 |
Loop_Ent : Entity_Id; |
| 9256 |
Local_Proc : Entity_Id; |
| 9257 |
Loop_Copy : constant Node_Id := |
| 9258 |
Relocate_Node (Loop_Stmt); |
| 9259 |
begin |
| 9260 |
Loop_Ent := Entity (Identifier (Loop_Stmt)); |
| 9261 |
Ent := First_Entity (Loop_Ent); |
| 9262 |
|
| 9263 |
Local_Proc := Make_Temporary (Loc, 'P'); |
| 9264 |
|
| 9265 |
Local_Body := |
| 9266 |
Make_Subprogram_Body (Loc, |
| 9267 |
Specification => |
| 9268 |
Make_Procedure_Specification (Loc, |
| 9269 |
Defining_Unit_Name => Local_Proc), |
| 9270 |
Declarations => Empty_List, |
| 9271 |
Handled_Statement_Sequence => |
| 9272 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 9273 |
Statements => New_List (Loop_Copy))); |
| 9274 |
|
| 9275 |
Rewrite (Loop_Stmt, Local_Body); |
| 9276 |
Analyze (Loop_Stmt); |
| 9277 |
|
| 9278 |
Set_Has_Nested_Subprogram (Local_Proc); |
| 9279 |
|
| 9280 |
Local_Call := |
| 9281 |
Make_Procedure_Call_Statement (Loc, |
| 9282 |
Name => New_Occurrence_Of (Local_Proc, Loc)); |
| 9283 |
|
| 9284 |
Insert_After (Loop_Stmt, Local_Call); |
| 9285 |
Analyze (Local_Call); |
| 9286 |
|
| 9287 |
-- New procedure has the same scope as the original loop, and the scope |
| 9288 |
-- of the loop is the new procedure. |
| 9289 |
|
| 9290 |
Set_Scope (Local_Proc, Scope (Loop_Ent)); |
| 9291 |
Set_Scope (Loop_Ent, Local_Proc); |
| 9292 |
|
| 9293 |
Fixup_Inner_Scopes (Loop_Copy); |
| 9294 |
|
| 9295 |
-- The entity list of the new procedure is that of the loop |
| 9296 |
|
| 9297 |
Set_First_Entity (Local_Proc, Ent); |
| 9298 |
|
| 9299 |
-- Note that the entities associated with the loop don't need to have |
| 9300 |
-- their Scope fields reset, since they're still associated with the |
| 9301 |
-- same loop entity that now belongs to the copied loop statement. |
| 9302 |
end Unnest_Loop; |
| 9303 |
|
| 9304 |
--------------------------- |
| 9305 |
-- Unnest_Statement_List -- |
| 9306 |
--------------------------- |
| 9307 |
|
| 9308 |
procedure Unnest_Statement_List (Stmts : in out List_Id) is |
| 9309 |
Loc : constant Source_Ptr := Sloc (First (Stmts)); |
| 9310 |
Local_Body : Node_Id; |
| 9311 |
Local_Call : Node_Id; |
| 9312 |
Local_Proc : Entity_Id; |
| 9313 |
New_Stmts : constant List_Id := Empty_List; |
| 9314 |
|
| 9315 |
begin |
| 9316 |
Local_Proc := Make_Temporary (Loc, 'P'); |
| 9317 |
|
| 9318 |
Local_Body := |
| 9319 |
Make_Subprogram_Body (Loc, |
| 9320 |
Specification => |
| 9321 |
Make_Procedure_Specification (Loc, |
| 9322 |
Defining_Unit_Name => Local_Proc), |
| 9323 |
Declarations => Empty_List, |
| 9324 |
Handled_Statement_Sequence => |
| 9325 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 9326 |
Statements => Stmts)); |
| 9327 |
|
| 9328 |
Append_To (New_Stmts, Local_Body); |
| 9329 |
|
| 9330 |
Analyze (Local_Body); |
| 9331 |
|
| 9332 |
Set_Has_Nested_Subprogram (Local_Proc); |
| 9333 |
|
| 9334 |
Local_Call := |
| 9335 |
Make_Procedure_Call_Statement (Loc, |
| 9336 |
Name => New_Occurrence_Of (Local_Proc, Loc)); |
| 9337 |
|
| 9338 |
Append_To (New_Stmts, Local_Call); |
| 9339 |
Analyze (Local_Call); |
| 9340 |
|
| 9341 |
-- Traverse the statements, and for any that are declarations or |
| 9342 |
-- subprogram bodies that have entities, set the Scope of those |
| 9343 |
-- entities to the new procedure's Entity_Id. |
| 9344 |
|
| 9345 |
declare |
| 9346 |
Stmt : Node_Id := First (Stmts); |
| 9347 |
|
| 9348 |
begin |
| 9349 |
while Present (Stmt) loop |
| 9350 |
case Nkind (Stmt) is |
| 9351 |
when N_Declaration |
| 9352 |
| N_Renaming_Declaration |
| 9353 |
=> |
| 9354 |
Set_Scope (Defining_Identifier (Stmt), Local_Proc); |
| 9355 |
|
| 9356 |
when N_Subprogram_Body => |
| 9357 |
Set_Scope |
| 9358 |
(Defining_Unit_Name (Specification (Stmt)), Local_Proc); |
| 9359 |
|
| 9360 |
when others => |
| 9361 |
null; |
| 9362 |
end case; |
| 9363 |
|
| 9364 |
Next (Stmt); |
| 9365 |
end loop; |
| 9366 |
end; |
| 9367 |
|
| 9368 |
Stmts := New_Stmts; |
| 9369 |
end Unnest_Statement_List; |
| 9370 |
|
| 9371 |
-------------------------------- |
| 9372 |
-- Wrap_Transient_Declaration -- |
| 9373 |
-------------------------------- |
| 9374 |
|
| 9375 |
-- If a transient scope has been established during the processing of the |
| 9376 |
-- Expression of an Object_Declaration, it is not possible to wrap the |
| 9377 |
-- declaration into a transient block as usual case, otherwise the object |
| 9378 |
-- would be itself declared in the wrong scope. Therefore, all entities (if |
| 9379 |
-- any) defined in the transient block are moved to the proper enclosing |
| 9380 |
-- scope. Furthermore, if they are controlled variables they are finalized |
| 9381 |
-- right after the declaration. The finalization list of the transient |
| 9382 |
-- scope is defined as a renaming of the enclosing one so during their |
| 9383 |
-- initialization they will be attached to the proper finalization list. |
| 9384 |
-- For instance, the following declaration : |
| 9385 |
|
| 9386 |
-- X : Typ := F (G (A), G (B)); |
| 9387 |
|
| 9388 |
-- (where G(A) and G(B) return controlled values, expanded as _v1 and _v2) |
| 9389 |
-- is expanded into : |
| 9390 |
|
| 9391 |
-- X : Typ := [ complex Expression-Action ]; |
| 9392 |
-- [Deep_]Finalize (_v1); |
| 9393 |
-- [Deep_]Finalize (_v2); |
| 9394 |
|
| 9395 |
procedure Wrap_Transient_Declaration (N : Node_Id) is |
| 9396 |
Curr_S : Entity_Id; |
| 9397 |
Encl_S : Entity_Id; |
| 9398 |
|
| 9399 |
begin |
| 9400 |
Curr_S := Current_Scope; |
| 9401 |
Encl_S := Scope (Curr_S); |
| 9402 |
|
| 9403 |
-- Insert all actions including cleanup generated while analyzing or |
| 9404 |
-- expanding the transient context back into the tree. Manage the |
| 9405 |
-- secondary stack when the object declaration appears in a library |
| 9406 |
-- level package [body]. |
| 9407 |
|
| 9408 |
Insert_Actions_In_Scope_Around |
| 9409 |
(N => N, |
| 9410 |
Clean => True, |
| 9411 |
Manage_SS => |
| 9412 |
Uses_Sec_Stack (Curr_S) |
| 9413 |
and then Nkind (N) = N_Object_Declaration |
| 9414 |
and then Ekind (Encl_S) in E_Package | E_Package_Body |
| 9415 |
and then Is_Library_Level_Entity (Encl_S)); |
| 9416 |
Pop_Scope; |
| 9417 |
|
| 9418 |
-- Relocate local entities declared within the transient scope to the |
| 9419 |
-- enclosing scope. This action sets their Is_Public flag accordingly. |
| 9420 |
|
| 9421 |
Transfer_Entities (Curr_S, Encl_S); |
| 9422 |
|
| 9423 |
-- Mark the enclosing dynamic scope to ensure that the secondary stack |
| 9424 |
-- is properly released upon exiting the said scope. |
| 9425 |
|
| 9426 |
if Uses_Sec_Stack (Curr_S) then |
| 9427 |
Curr_S := Enclosing_Dynamic_Scope (Curr_S); |
| 9428 |
|
| 9429 |
-- Do not mark a function that returns on the secondary stack as the |
| 9430 |
-- reclamation is done by the caller. |
| 9431 |
|
| 9432 |
if Ekind (Curr_S) = E_Function |
| 9433 |
and then Needs_Secondary_Stack (Etype (Curr_S)) |
| 9434 |
then |
| 9435 |
null; |
| 9436 |
|
| 9437 |
-- Otherwise mark the enclosing dynamic scope |
| 9438 |
|
| 9439 |
else |
| 9440 |
Set_Uses_Sec_Stack (Curr_S); |
| 9441 |
Check_Restriction (No_Secondary_Stack, N); |
| 9442 |
end if; |
| 9443 |
end if; |
| 9444 |
end Wrap_Transient_Declaration; |
| 9445 |
|
| 9446 |
------------------------------- |
| 9447 |
-- Wrap_Transient_Expression -- |
| 9448 |
------------------------------- |
| 9449 |
|
| 9450 |
procedure Wrap_Transient_Expression (N : Node_Id) is |
| 9451 |
Loc : constant Source_Ptr := Sloc (N); |
| 9452 |
Expr : Node_Id := Relocate_Node (N); |
| 9453 |
Temp : constant Entity_Id := Make_Temporary (Loc, 'E', N); |
| 9454 |
Typ : constant Entity_Id := Etype (N); |
| 9455 |
|
| 9456 |
begin |
| 9457 |
-- Generate: |
| 9458 |
|
| 9459 |
-- Temp : Typ; |
| 9460 |
-- declare |
| 9461 |
-- M : constant Mark_Id := SS_Mark; |
| 9462 |
-- procedure Finalizer is ... (See Build_Finalizer) |
| 9463 |
|
| 9464 |
-- begin |
| 9465 |
-- Temp := <Expr>; -- general case |
| 9466 |
-- Temp := (if <Expr> then True else False); -- boolean case |
| 9467 |
|
| 9468 |
-- at end |
| 9469 |
-- Finalizer; |
| 9470 |
-- end; |
| 9471 |
|
| 9472 |
-- A special case is made for Boolean expressions so that the back end |
| 9473 |
-- knows to generate a conditional branch instruction, if running with |
| 9474 |
-- -fpreserve-control-flow. This ensures that a control-flow change |
| 9475 |
-- signaling the decision outcome occurs before the cleanup actions. |
| 9476 |
|
| 9477 |
if Opt.Suppress_Control_Flow_Optimizations |
| 9478 |
and then Is_Boolean_Type (Typ) |
| 9479 |
then |
| 9480 |
Expr := |
| 9481 |
Make_If_Expression (Loc, |
| 9482 |
Expressions => New_List ( |
| 9483 |
Expr, |
| 9484 |
New_Occurrence_Of (Standard_True, Loc), |
| 9485 |
New_Occurrence_Of (Standard_False, Loc))); |
| 9486 |
end if; |
| 9487 |
|
| 9488 |
Insert_Actions (N, New_List ( |
| 9489 |
Make_Object_Declaration (Loc, |
| 9490 |
Defining_Identifier => Temp, |
| 9491 |
Object_Definition => New_Occurrence_Of (Typ, Loc)), |
| 9492 |
|
| 9493 |
Make_Transient_Block (Loc, |
| 9494 |
Action => |
| 9495 |
Make_Assignment_Statement (Loc, |
| 9496 |
Name => New_Occurrence_Of (Temp, Loc), |
| 9497 |
Expression => Expr), |
| 9498 |
Par => Parent (N)))); |
| 9499 |
|
| 9500 |
if Debug_Generated_Code then |
| 9501 |
Set_Debug_Info_Needed (Temp); |
| 9502 |
end if; |
| 9503 |
|
| 9504 |
Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
| 9505 |
Analyze_And_Resolve (N, Typ); |
| 9506 |
end Wrap_Transient_Expression; |
| 9507 |
|
| 9508 |
------------------------------ |
| 9509 |
-- Wrap_Transient_Statement -- |
| 9510 |
------------------------------ |
| 9511 |
|
| 9512 |
procedure Wrap_Transient_Statement (N : Node_Id) is |
| 9513 |
Loc : constant Source_Ptr := Sloc (N); |
| 9514 |
New_Stmt : constant Node_Id := Relocate_Node (N); |
| 9515 |
|
| 9516 |
begin |
| 9517 |
-- Generate: |
| 9518 |
-- declare |
| 9519 |
-- M : constant Mark_Id := SS_Mark; |
| 9520 |
-- procedure Finalizer is ... (See Build_Finalizer) |
| 9521 |
-- |
| 9522 |
-- begin |
| 9523 |
-- <New_Stmt>; |
| 9524 |
-- |
| 9525 |
-- at end |
| 9526 |
-- Finalizer; |
| 9527 |
-- end; |
| 9528 |
|
| 9529 |
Rewrite (N, |
| 9530 |
Make_Transient_Block (Loc, |
| 9531 |
Action => New_Stmt, |
| 9532 |
Par => Parent (N))); |
| 9533 |
|
| 9534 |
-- With the scope stack back to normal, we can call analyze on the |
| 9535 |
-- resulting block. At this point, the transient scope is being |
| 9536 |
-- treated like a perfectly normal scope, so there is nothing |
| 9537 |
-- special about it. |
| 9538 |
|
| 9539 |
-- Note: Wrap_Transient_Statement is called with the node already |
| 9540 |
-- analyzed (i.e. Analyzed (N) is True). This is important, since |
| 9541 |
-- otherwise we would get a recursive processing of the node when |
| 9542 |
-- we do this Analyze call. |
| 9543 |
|
| 9544 |
Analyze (N); |
| 9545 |
end Wrap_Transient_Statement; |
| 9546 |
|
| 9547 |
end Exp_Ch7; |