Make more use of in_hard_reg_set_p
[official-gcc.git] / gcc / ada / exp_ch7.adb
blob2ca42de1939ec1888abf228aa1e397fc0484e52a
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-2017, 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 ------------------------------------------------------------------------------
26 -- This package contains virtually all expansion mechanisms related to
27 -- - controlled types
28 -- - transient scopes
30 with Atree; use Atree;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Ch11; use Exp_Ch11;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Prag; use Exp_Prag;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Freeze; use Freeze;
45 with Lib; use Lib;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Output; use Output;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sinfo; use Sinfo;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Tbuild; use Tbuild;
64 with Ttypes; use Ttypes;
65 with Uintp; use Uintp;
67 package body Exp_Ch7 is
69 --------------------------------
70 -- Transient Scope Management --
71 --------------------------------
73 -- A transient scope is created when temporary objects are created by the
74 -- compiler. These temporary objects are allocated on the secondary stack
75 -- and the transient scope is responsible for finalizing the object when
76 -- appropriate and reclaiming the memory at the right time. The temporary
77 -- objects are generally the objects allocated to store the result of a
78 -- function returning an unconstrained or a tagged value. Expressions
79 -- needing to be wrapped in a transient scope (functions calls returning
80 -- unconstrained or tagged values) may appear in 3 different contexts which
81 -- lead to 3 different kinds of transient scope expansion:
83 -- 1. In a simple statement (procedure call, assignment, ...). In this
84 -- case the instruction is wrapped into a transient block. See
85 -- Wrap_Transient_Statement for details.
87 -- 2. In an expression of a control structure (test in a IF statement,
88 -- expression in a CASE statement, ...). See Wrap_Transient_Expression
89 -- for details.
91 -- 3. In a expression of an object_declaration. No wrapping is possible
92 -- here, so the finalization actions, if any, are done right after the
93 -- declaration and the secondary stack deallocation is done in the
94 -- proper enclosing scope. See Wrap_Transient_Declaration for details.
96 -- Note about functions returning tagged types: it has been decided to
97 -- always allocate their result in the secondary stack, even though is not
98 -- absolutely mandatory when the tagged type is constrained because the
99 -- caller knows the size of the returned object and thus could allocate the
100 -- result in the primary stack. An exception to this is when the function
101 -- builds its result in place, as is done for functions with inherently
102 -- limited result types for Ada 2005. In that case, certain callers may
103 -- pass the address of a constrained object as the target object for the
104 -- function result.
106 -- By allocating tagged results in the secondary stack a number of
107 -- implementation difficulties are avoided:
109 -- - If it is a dispatching function call, the computation of the size of
110 -- the result is possible but complex from the outside.
112 -- - If the returned type is controlled, the assignment of the returned
113 -- value to the anonymous object involves an Adjust, and we have no
114 -- easy way to access the anonymous object created by the back end.
116 -- - If the returned type is class-wide, this is an unconstrained type
117 -- anyway.
119 -- Furthermore, the small loss in efficiency which is the result of this
120 -- decision is not such a big deal because functions returning tagged types
121 -- are not as common in practice compared to functions returning access to
122 -- a tagged type.
124 --------------------------------------------------
125 -- Transient Blocks and Finalization Management --
126 --------------------------------------------------
128 function Find_Node_To_Be_Wrapped (N : Node_Id) return Node_Id;
129 -- N is a node which may generate a transient scope. Loop over the parent
130 -- pointers of N until we find the appropriate node to wrap. If it returns
131 -- Empty, it means that no transient scope is needed in this context.
133 procedure Insert_Actions_In_Scope_Around
134 (N : Node_Id;
135 Clean : Boolean;
136 Manage_SS : Boolean);
137 -- Insert the before-actions kept in the scope stack before N, and the
138 -- after-actions after N, which must be a member of a list. If flag Clean
139 -- is set, insert any cleanup actions. If flag Manage_SS is set, insert
140 -- calls to mark and release the secondary stack.
142 function Make_Transient_Block
143 (Loc : Source_Ptr;
144 Action : Node_Id;
145 Par : Node_Id) return Node_Id;
146 -- Action is a single statement or object declaration. Par is the proper
147 -- parent of the generated block. Create a transient block whose name is
148 -- the current scope and the only handled statement is Action. If Action
149 -- involves controlled objects or secondary stack usage, the corresponding
150 -- cleanup actions are performed at the end of the block.
152 procedure Set_Node_To_Be_Wrapped (N : Node_Id);
153 -- Set the field Node_To_Be_Wrapped of the current scope
155 -- ??? The entire comment needs to be rewritten
156 -- ??? which entire comment?
158 procedure Store_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id);
159 -- Shared processing for Store_xxx_Actions_In_Scope
161 -----------------------------
162 -- Finalization Management --
163 -----------------------------
165 -- This part describe how Initialization/Adjustment/Finalization procedures
166 -- are generated and called. Two cases must be considered, types that are
167 -- Controlled (Is_Controlled flag set) and composite types that contain
168 -- controlled components (Has_Controlled_Component flag set). In the first
169 -- case the procedures to call are the user-defined primitive operations
170 -- Initialize/Adjust/Finalize. In the second case, GNAT generates
171 -- Deep_Initialize, Deep_Adjust and Deep_Finalize that are in charge
172 -- of calling the former procedures on the controlled components.
174 -- For records with Has_Controlled_Component set, a hidden "controller"
175 -- component is inserted. This controller component contains its own
176 -- finalization list on which all controlled components are attached
177 -- creating an indirection on the upper-level Finalization list. This
178 -- technique facilitates the management of objects whose number of
179 -- controlled components changes during execution. This controller
180 -- component is itself controlled and is attached to the upper-level
181 -- finalization chain. Its adjust primitive is in charge of calling adjust
182 -- on the components and adjusting the finalization pointer to match their
183 -- new location (see a-finali.adb).
185 -- It is not possible to use a similar technique for arrays that have
186 -- Has_Controlled_Component set. In this case, deep procedures are
187 -- generated that call initialize/adjust/finalize + attachment or
188 -- detachment on the finalization list for all component.
190 -- Initialize calls: they are generated for declarations or dynamic
191 -- allocations of Controlled objects with no initial value. They are always
192 -- followed by an attachment to the current Finalization Chain. For the
193 -- dynamic allocation case this the chain attached to the scope of the
194 -- access type definition otherwise, this is the chain of the current
195 -- scope.
197 -- Adjust Calls: They are generated on 2 occasions: (1) for declarations
198 -- or dynamic allocations of Controlled objects with an initial value.
199 -- (2) after an assignment. In the first case they are followed by an
200 -- attachment to the final chain, in the second case they are not.
202 -- Finalization Calls: They are generated on (1) scope exit, (2)
203 -- assignments, (3) unchecked deallocations. In case (3) they have to
204 -- be detached from the final chain, in case (2) they must not and in
205 -- case (1) this is not important since we are exiting the scope anyway.
207 -- Other details:
209 -- Type extensions will have a new record controller at each derivation
210 -- level containing controlled components. The record controller for
211 -- the parent/ancestor is attached to the finalization list of the
212 -- extension's record controller (i.e. the parent is like a component
213 -- of the extension).
215 -- For types that are both Is_Controlled and Has_Controlled_Components,
216 -- the record controller and the object itself are handled separately.
217 -- It could seem simpler to attach the object at the end of its record
218 -- controller but this would not tackle view conversions properly.
220 -- A classwide type can always potentially have controlled components
221 -- but the record controller of the corresponding actual type may not
222 -- be known at compile time so the dispatch table contains a special
223 -- field that allows computation of the offset of the record controller
224 -- dynamically. See s-finimp.Deep_Tag_Attach and a-tags.RC_Offset.
226 -- Here is a simple example of the expansion of a controlled block :
228 -- declare
229 -- X : Controlled;
230 -- Y : Controlled := Init;
232 -- type R is record
233 -- C : Controlled;
234 -- end record;
235 -- W : R;
236 -- Z : R := (C => X);
238 -- begin
239 -- X := Y;
240 -- W := Z;
241 -- end;
243 -- is expanded into
245 -- declare
246 -- _L : System.FI.Finalizable_Ptr;
248 -- procedure _Clean is
249 -- begin
250 -- Abort_Defer;
251 -- System.FI.Finalize_List (_L);
252 -- Abort_Undefer;
253 -- end _Clean;
255 -- X : Controlled;
256 -- begin
257 -- Abort_Defer;
258 -- Initialize (X);
259 -- Attach_To_Final_List (_L, Finalizable (X), 1);
260 -- at end: Abort_Undefer;
261 -- Y : Controlled := Init;
262 -- Adjust (Y);
263 -- Attach_To_Final_List (_L, Finalizable (Y), 1);
265 -- type R is record
266 -- C : Controlled;
267 -- end record;
268 -- W : R;
269 -- begin
270 -- Abort_Defer;
271 -- Deep_Initialize (W, _L, 1);
272 -- at end: Abort_Under;
273 -- Z : R := (C => X);
274 -- Deep_Adjust (Z, _L, 1);
276 -- begin
277 -- _Assign (X, Y);
278 -- Deep_Finalize (W, False);
279 -- <save W's final pointers>
280 -- W := Z;
281 -- <restore W's final pointers>
282 -- Deep_Adjust (W, _L, 0);
283 -- at end
284 -- _Clean;
285 -- end;
287 type Final_Primitives is
288 (Initialize_Case, Adjust_Case, Finalize_Case, Address_Case);
289 -- This enumeration type is defined in order to ease sharing code for
290 -- building finalization procedures for composite types.
292 Name_Of : constant array (Final_Primitives) of Name_Id :=
293 (Initialize_Case => Name_Initialize,
294 Adjust_Case => Name_Adjust,
295 Finalize_Case => Name_Finalize,
296 Address_Case => Name_Finalize_Address);
297 Deep_Name_Of : constant array (Final_Primitives) of TSS_Name_Type :=
298 (Initialize_Case => TSS_Deep_Initialize,
299 Adjust_Case => TSS_Deep_Adjust,
300 Finalize_Case => TSS_Deep_Finalize,
301 Address_Case => TSS_Finalize_Address);
303 function Allows_Finalization_Master (Typ : Entity_Id) return Boolean;
304 -- Determine whether access type Typ may have a finalization master
306 procedure Build_Array_Deep_Procs (Typ : Entity_Id);
307 -- Build the deep Initialize/Adjust/Finalize for a record Typ with
308 -- Has_Controlled_Component set and store them using the TSS mechanism.
310 function Build_Cleanup_Statements
311 (N : Node_Id;
312 Additional_Cleanup : List_Id) return List_Id;
313 -- Create the clean up calls for an asynchronous call block, task master,
314 -- protected subprogram body, task allocation block or task body, or
315 -- additional cleanup actions parked on a transient block. If the context
316 -- does not contain the above constructs, the routine returns an empty
317 -- list.
319 procedure Build_Finalizer
320 (N : Node_Id;
321 Clean_Stmts : List_Id;
322 Mark_Id : Entity_Id;
323 Top_Decls : List_Id;
324 Defer_Abort : Boolean;
325 Fin_Id : out Entity_Id);
326 -- N may denote an accept statement, block, entry body, package body,
327 -- package spec, protected body, subprogram body, or a task body. Create
328 -- a procedure which contains finalization calls for all controlled objects
329 -- declared in the declarative or statement region of N. The calls are
330 -- built in reverse order relative to the original declarations. In the
331 -- case of a task body, the routine delays the creation of the finalizer
332 -- until all statements have been moved to the task body procedure.
333 -- Clean_Stmts may contain additional context-dependent code used to abort
334 -- asynchronous calls or complete tasks (see Build_Cleanup_Statements).
335 -- Mark_Id is the secondary stack used in the current context or Empty if
336 -- missing. Top_Decls is the list on which the declaration of the finalizer
337 -- is attached in the non-package case. Defer_Abort indicates that the
338 -- statements passed in perform actions that require abort to be deferred,
339 -- such as for task termination. Fin_Id is the finalizer declaration
340 -- entity.
342 procedure Build_Finalizer_Call (N : Node_Id; Fin_Id : Entity_Id);
343 -- N is a construct which contains a handled sequence of statements, Fin_Id
344 -- is the entity of a finalizer. Create an At_End handler which covers the
345 -- statements of N and calls Fin_Id. If the handled statement sequence has
346 -- an exception handler, the statements will be wrapped in a block to avoid
347 -- unwanted interaction with the new At_End handler.
349 procedure Build_Record_Deep_Procs (Typ : Entity_Id);
350 -- Build the deep Initialize/Adjust/Finalize for a record Typ with
351 -- Has_Component_Component set and store them using the TSS mechanism.
353 procedure Check_Visibly_Controlled
354 (Prim : Final_Primitives;
355 Typ : Entity_Id;
356 E : in out Entity_Id;
357 Cref : in out Node_Id);
358 -- The controlled operation declared for a derived type may not be
359 -- overriding, if the controlled operations of the parent type are hidden,
360 -- for example when the parent is a private type whose full view is
361 -- controlled. For other primitive operations we modify the name of the
362 -- operation to indicate that it is not overriding, but this is not
363 -- possible for Initialize, etc. because they have to be retrievable by
364 -- name. Before generating the proper call to one of these operations we
365 -- check whether Typ is known to be controlled at the point of definition.
366 -- If it is not then we must retrieve the hidden operation of the parent
367 -- and use it instead. This is one case that might be solved more cleanly
368 -- once Overriding pragmas or declarations are in place.
370 function Convert_View
371 (Proc : Entity_Id;
372 Arg : Node_Id;
373 Ind : Pos := 1) return Node_Id;
374 -- Proc is one of the Initialize/Adjust/Finalize operations, and Arg is the
375 -- argument being passed to it. Ind indicates which formal of procedure
376 -- Proc we are trying to match. This function will, if necessary, generate
377 -- a conversion between the partial and full view of Arg to match the type
378 -- of the formal of Proc, or force a conversion to the class-wide type in
379 -- the case where the operation is abstract.
381 function Enclosing_Function (E : Entity_Id) return Entity_Id;
382 -- Given an arbitrary entity, traverse the scope chain looking for the
383 -- first enclosing function. Return Empty if no function was found.
385 function Make_Call
386 (Loc : Source_Ptr;
387 Proc_Id : Entity_Id;
388 Param : Node_Id;
389 Skip_Self : Boolean := False) return Node_Id;
390 -- Subsidiary to Make_Adjust_Call and Make_Final_Call. Given the entity of
391 -- routine [Deep_]Adjust or [Deep_]Finalize and an object parameter, create
392 -- an adjust or finalization call. Wnen flag Skip_Self is set, the related
393 -- action has an effect on the components only (if any).
395 function Make_Deep_Proc
396 (Prim : Final_Primitives;
397 Typ : Entity_Id;
398 Stmts : List_Id) return Node_Id;
399 -- This function generates the tree for Deep_Initialize, Deep_Adjust or
400 -- Deep_Finalize procedures according to the first parameter, these
401 -- procedures operate on the type Typ. The Stmts parameter gives the body
402 -- of the procedure.
404 function Make_Deep_Array_Body
405 (Prim : Final_Primitives;
406 Typ : Entity_Id) return List_Id;
407 -- This function generates the list of statements for implementing
408 -- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures according to
409 -- the first parameter, these procedures operate on the array type Typ.
411 function Make_Deep_Record_Body
412 (Prim : Final_Primitives;
413 Typ : Entity_Id;
414 Is_Local : Boolean := False) return List_Id;
415 -- This function generates the list of statements for implementing
416 -- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures according to
417 -- the first parameter, these procedures operate on the record type Typ.
418 -- Flag Is_Local is used in conjunction with Deep_Finalize to designate
419 -- whether the inner logic should be dictated by state counters.
421 function Make_Finalize_Address_Stmts (Typ : Entity_Id) return List_Id;
422 -- Subsidiary to Make_Finalize_Address_Body, Make_Deep_Array_Body and
423 -- Make_Deep_Record_Body. Generate the following statements:
425 -- declare
426 -- type Acc_Typ is access all Typ;
427 -- for Acc_Typ'Storage_Size use 0;
428 -- begin
429 -- [Deep_]Finalize (Acc_Typ (V).all);
430 -- end;
432 --------------------------------
433 -- Allows_Finalization_Master --
434 --------------------------------
436 function Allows_Finalization_Master (Typ : Entity_Id) return Boolean is
437 function In_Deallocation_Instance (E : Entity_Id) return Boolean;
438 -- Determine whether entity E is inside a wrapper package created for
439 -- an instance of Ada.Unchecked_Deallocation.
441 ------------------------------
442 -- In_Deallocation_Instance --
443 ------------------------------
445 function In_Deallocation_Instance (E : Entity_Id) return Boolean is
446 Pkg : constant Entity_Id := Scope (E);
447 Par : Node_Id := Empty;
449 begin
450 if Ekind (Pkg) = E_Package
451 and then Present (Related_Instance (Pkg))
452 and then Ekind (Related_Instance (Pkg)) = E_Procedure
453 then
454 Par := Generic_Parent (Parent (Related_Instance (Pkg)));
456 return
457 Present (Par)
458 and then Chars (Par) = Name_Unchecked_Deallocation
459 and then Chars (Scope (Par)) = Name_Ada
460 and then Scope (Scope (Par)) = Standard_Standard;
461 end if;
463 return False;
464 end In_Deallocation_Instance;
466 -- Local variables
468 Desig_Typ : constant Entity_Id := Designated_Type (Typ);
469 Ptr_Typ : constant Entity_Id :=
470 Root_Type_Of_Full_View (Base_Type (Typ));
472 -- Start of processing for Allows_Finalization_Master
474 begin
475 -- Certain run-time configurations and targets do not provide support
476 -- for controlled types and therefore do not need masters.
478 if Restriction_Active (No_Finalization) then
479 return False;
481 -- Do not consider C and C++ types since it is assumed that the non-Ada
482 -- side will handle their clean up.
484 elsif Convention (Desig_Typ) = Convention_C
485 or else Convention (Desig_Typ) = Convention_CPP
486 then
487 return False;
489 -- Do not consider an access type that returns on the secondary stack
491 elsif Present (Associated_Storage_Pool (Ptr_Typ))
492 and then Is_RTE (Associated_Storage_Pool (Ptr_Typ), RE_SS_Pool)
493 then
494 return False;
496 -- Do not consider an access type that can never allocate an object
498 elsif No_Pool_Assigned (Ptr_Typ) then
499 return False;
501 -- Do not consider an access type coming from an Unchecked_Deallocation
502 -- instance. Even though the designated type may be controlled, the
503 -- access type will never participate in any allocations.
505 elsif In_Deallocation_Instance (Ptr_Typ) then
506 return False;
508 -- Do not consider a non-library access type when No_Nested_Finalization
509 -- is in effect since finalization masters are controlled objects and if
510 -- created will violate the restriction.
512 elsif Restriction_Active (No_Nested_Finalization)
513 and then not Is_Library_Level_Entity (Ptr_Typ)
514 then
515 return False;
517 -- Do not consider an access type subject to pragma No_Heap_Finalization
518 -- because objects allocated through such a type are not to be finalized
519 -- when the access type goes out of scope.
521 elsif No_Heap_Finalization (Ptr_Typ) then
522 return False;
524 -- Do not create finalization masters in GNATprove mode because this
525 -- causes unwanted extra expansion. A compilation in this mode must
526 -- keep the tree as close as possible to the original sources.
528 elsif GNATprove_Mode then
529 return False;
531 -- Otherwise the access type may use a finalization master
533 else
534 return True;
535 end if;
536 end Allows_Finalization_Master;
538 ----------------------------
539 -- Build_Anonymous_Master --
540 ----------------------------
542 procedure Build_Anonymous_Master (Ptr_Typ : Entity_Id) is
543 function Create_Anonymous_Master
544 (Desig_Typ : Entity_Id;
545 Unit_Id : Entity_Id;
546 Unit_Decl : Node_Id) return Entity_Id;
547 -- Create a new anonymous master for access type Ptr_Typ with designated
548 -- type Desig_Typ. The declaration of the master and its initialization
549 -- are inserted in the declarative part of unit Unit_Decl. Unit_Id is
550 -- the entity of Unit_Decl.
552 function Current_Anonymous_Master
553 (Desig_Typ : Entity_Id;
554 Unit_Id : Entity_Id) return Entity_Id;
555 -- Find an anonymous master declared within unit Unit_Id which services
556 -- designated type Desig_Typ. If there is no such master, return Empty.
558 -----------------------------
559 -- Create_Anonymous_Master --
560 -----------------------------
562 function Create_Anonymous_Master
563 (Desig_Typ : Entity_Id;
564 Unit_Id : Entity_Id;
565 Unit_Decl : Node_Id) return Entity_Id
567 Loc : constant Source_Ptr := Sloc (Unit_Id);
569 All_FMs : Elist_Id;
570 Decls : List_Id;
571 FM_Decl : Node_Id;
572 FM_Id : Entity_Id;
573 FM_Init : Node_Id;
574 Unit_Spec : Node_Id;
576 begin
577 -- Generate:
578 -- <FM_Id> : Finalization_Master;
580 FM_Id := Make_Temporary (Loc, 'A');
582 FM_Decl :=
583 Make_Object_Declaration (Loc,
584 Defining_Identifier => FM_Id,
585 Object_Definition =>
586 New_Occurrence_Of (RTE (RE_Finalization_Master), Loc));
588 -- Generate:
589 -- Set_Base_Pool
590 -- (<FM_Id>, Global_Pool_Object'Unrestricted_Access);
592 FM_Init :=
593 Make_Procedure_Call_Statement (Loc,
594 Name =>
595 New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc),
596 Parameter_Associations => New_List (
597 New_Occurrence_Of (FM_Id, Loc),
598 Make_Attribute_Reference (Loc,
599 Prefix =>
600 New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc),
601 Attribute_Name => Name_Unrestricted_Access)));
603 -- Find the declarative list of the unit
605 if Nkind (Unit_Decl) = N_Package_Declaration then
606 Unit_Spec := Specification (Unit_Decl);
607 Decls := Visible_Declarations (Unit_Spec);
609 if No (Decls) then
610 Decls := New_List;
611 Set_Visible_Declarations (Unit_Spec, Decls);
612 end if;
614 -- Package body or subprogram case
616 -- ??? A subprogram spec or body that acts as a compilation unit may
617 -- contain a formal parameter of an anonymous access-to-controlled
618 -- type initialized by an allocator.
620 -- procedure Comp_Unit_Proc (Param : access Ctrl := new Ctrl);
622 -- There is no suitable place to create the master as the subprogram
623 -- is not in a declarative list.
625 else
626 Decls := Declarations (Unit_Decl);
628 if No (Decls) then
629 Decls := New_List;
630 Set_Declarations (Unit_Decl, Decls);
631 end if;
632 end if;
634 Prepend_To (Decls, FM_Init);
635 Prepend_To (Decls, FM_Decl);
637 -- Use the scope of the unit when analyzing the declaration of the
638 -- master and its initialization actions.
640 Push_Scope (Unit_Id);
641 Analyze (FM_Decl);
642 Analyze (FM_Init);
643 Pop_Scope;
645 -- Mark the master as servicing this specific designated type
647 Set_Anonymous_Designated_Type (FM_Id, Desig_Typ);
649 -- Include the anonymous master in the list of existing masters which
650 -- appear in this unit. This effectively creates a mapping between a
651 -- master and a designated type which in turn allows for the reuse of
652 -- masters on a per-unit basis.
654 All_FMs := Anonymous_Masters (Unit_Id);
656 if No (All_FMs) then
657 All_FMs := New_Elmt_List;
658 Set_Anonymous_Masters (Unit_Id, All_FMs);
659 end if;
661 Prepend_Elmt (FM_Id, All_FMs);
663 return FM_Id;
664 end Create_Anonymous_Master;
666 ------------------------------
667 -- Current_Anonymous_Master --
668 ------------------------------
670 function Current_Anonymous_Master
671 (Desig_Typ : Entity_Id;
672 Unit_Id : Entity_Id) return Entity_Id
674 All_FMs : constant Elist_Id := Anonymous_Masters (Unit_Id);
675 FM_Elmt : Elmt_Id;
676 FM_Id : Entity_Id;
678 begin
679 -- Inspect the list of anonymous masters declared within the unit
680 -- looking for an existing master which services the same designated
681 -- type.
683 if Present (All_FMs) then
684 FM_Elmt := First_Elmt (All_FMs);
685 while Present (FM_Elmt) loop
686 FM_Id := Node (FM_Elmt);
688 -- The currect master services the same designated type. As a
689 -- result the master can be reused and associated with another
690 -- anonymous access-to-controlled type.
692 if Anonymous_Designated_Type (FM_Id) = Desig_Typ then
693 return FM_Id;
694 end if;
696 Next_Elmt (FM_Elmt);
697 end loop;
698 end if;
700 return Empty;
701 end Current_Anonymous_Master;
703 -- Local variables
705 Desig_Typ : Entity_Id;
706 FM_Id : Entity_Id;
707 Priv_View : Entity_Id;
708 Unit_Decl : Node_Id;
709 Unit_Id : Entity_Id;
711 -- Start of processing for Build_Anonymous_Master
713 begin
714 -- Nothing to do if the circumstances do not allow for a finalization
715 -- master.
717 if not Allows_Finalization_Master (Ptr_Typ) then
718 return;
719 end if;
721 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
722 Unit_Id := Unique_Defining_Entity (Unit_Decl);
724 -- The compilation unit is a package instantiation. In this case the
725 -- anonymous master is associated with the package spec as both the
726 -- spec and body appear at the same level.
728 if Nkind (Unit_Decl) = N_Package_Body
729 and then Nkind (Original_Node (Unit_Decl)) = N_Package_Instantiation
730 then
731 Unit_Id := Corresponding_Spec (Unit_Decl);
732 Unit_Decl := Unit_Declaration_Node (Unit_Id);
733 end if;
735 -- Use the initial declaration of the designated type when it denotes
736 -- the full view of an incomplete or private type. This ensures that
737 -- types with one and two views are treated the same.
739 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
740 Priv_View := Incomplete_Or_Partial_View (Desig_Typ);
742 if Present (Priv_View) then
743 Desig_Typ := Priv_View;
744 end if;
746 -- Determine whether the current semantic unit already has an anonymous
747 -- master which services the designated type.
749 FM_Id := Current_Anonymous_Master (Desig_Typ, Unit_Id);
751 -- If this is not the case, create a new master
753 if No (FM_Id) then
754 FM_Id := Create_Anonymous_Master (Desig_Typ, Unit_Id, Unit_Decl);
755 end if;
757 Set_Finalization_Master (Ptr_Typ, FM_Id);
758 end Build_Anonymous_Master;
760 ----------------------------
761 -- Build_Array_Deep_Procs --
762 ----------------------------
764 procedure Build_Array_Deep_Procs (Typ : Entity_Id) is
765 begin
766 Set_TSS (Typ,
767 Make_Deep_Proc
768 (Prim => Initialize_Case,
769 Typ => Typ,
770 Stmts => Make_Deep_Array_Body (Initialize_Case, Typ)));
772 if not Is_Limited_View (Typ) then
773 Set_TSS (Typ,
774 Make_Deep_Proc
775 (Prim => Adjust_Case,
776 Typ => Typ,
777 Stmts => Make_Deep_Array_Body (Adjust_Case, Typ)));
778 end if;
780 -- Do not generate Deep_Finalize and Finalize_Address if finalization is
781 -- suppressed since these routine will not be used.
783 if not Restriction_Active (No_Finalization) then
784 Set_TSS (Typ,
785 Make_Deep_Proc
786 (Prim => Finalize_Case,
787 Typ => Typ,
788 Stmts => Make_Deep_Array_Body (Finalize_Case, Typ)));
790 -- Create TSS primitive Finalize_Address (unless CodePeer_Mode)
792 if not CodePeer_Mode then
793 Set_TSS (Typ,
794 Make_Deep_Proc
795 (Prim => Address_Case,
796 Typ => Typ,
797 Stmts => Make_Deep_Array_Body (Address_Case, Typ)));
798 end if;
799 end if;
800 end Build_Array_Deep_Procs;
802 ------------------------------
803 -- Build_Cleanup_Statements --
804 ------------------------------
806 function Build_Cleanup_Statements
807 (N : Node_Id;
808 Additional_Cleanup : List_Id) return List_Id
810 Is_Asynchronous_Call : constant Boolean :=
811 Nkind (N) = N_Block_Statement
812 and then Is_Asynchronous_Call_Block (N);
813 Is_Master : constant Boolean :=
814 Nkind (N) /= N_Entry_Body
815 and then Is_Task_Master (N);
816 Is_Protected_Body : constant Boolean :=
817 Nkind (N) = N_Subprogram_Body
818 and then Is_Protected_Subprogram_Body (N);
819 Is_Task_Allocation : constant Boolean :=
820 Nkind (N) = N_Block_Statement
821 and then Is_Task_Allocation_Block (N);
822 Is_Task_Body : constant Boolean :=
823 Nkind (Original_Node (N)) = N_Task_Body;
825 Loc : constant Source_Ptr := Sloc (N);
826 Stmts : constant List_Id := New_List;
828 begin
829 if Is_Task_Body then
830 if Restricted_Profile then
831 Append_To (Stmts,
832 Build_Runtime_Call (Loc, RE_Complete_Restricted_Task));
833 else
834 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Complete_Task));
835 end if;
837 elsif Is_Master then
838 if Restriction_Active (No_Task_Hierarchy) = False then
839 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Complete_Master));
840 end if;
842 -- Add statements to unlock the protected object parameter and to
843 -- undefer abort. If the context is a protected procedure and the object
844 -- has entries, call the entry service routine.
846 -- NOTE: The generated code references _object, a parameter to the
847 -- procedure.
849 elsif Is_Protected_Body then
850 declare
851 Spec : constant Node_Id := Parent (Corresponding_Spec (N));
852 Conc_Typ : Entity_Id;
853 Param : Node_Id;
854 Param_Typ : Entity_Id;
856 begin
857 -- Find the _object parameter representing the protected object
859 Param := First (Parameter_Specifications (Spec));
860 loop
861 Param_Typ := Etype (Parameter_Type (Param));
863 if Ekind (Param_Typ) = E_Record_Type then
864 Conc_Typ := Corresponding_Concurrent_Type (Param_Typ);
865 end if;
867 exit when No (Param) or else Present (Conc_Typ);
868 Next (Param);
869 end loop;
871 pragma Assert (Present (Param));
873 -- Historical note: In earlier versions of GNAT, there was code
874 -- at this point to generate stuff to service entry queues. It is
875 -- now abstracted in Build_Protected_Subprogram_Call_Cleanup.
877 Build_Protected_Subprogram_Call_Cleanup
878 (Specification (N), Conc_Typ, Loc, Stmts);
879 end;
881 -- Add a call to Expunge_Unactivated_Tasks for dynamically allocated
882 -- tasks. Other unactivated tasks are completed by Complete_Task or
883 -- Complete_Master.
885 -- NOTE: The generated code references _chain, a local object
887 elsif Is_Task_Allocation then
889 -- Generate:
890 -- Expunge_Unactivated_Tasks (_chain);
892 -- where _chain is the list of tasks created by the allocator but not
893 -- yet activated. This list will be empty unless the block completes
894 -- abnormally.
896 Append_To (Stmts,
897 Make_Procedure_Call_Statement (Loc,
898 Name =>
899 New_Occurrence_Of
900 (RTE (RE_Expunge_Unactivated_Tasks), Loc),
901 Parameter_Associations => New_List (
902 New_Occurrence_Of (Activation_Chain_Entity (N), Loc))));
904 -- Attempt to cancel an asynchronous entry call whenever the block which
905 -- contains the abortable part is exited.
907 -- NOTE: The generated code references Cnn, a local object
909 elsif Is_Asynchronous_Call then
910 declare
911 Cancel_Param : constant Entity_Id :=
912 Entry_Cancel_Parameter (Entity (Identifier (N)));
914 begin
915 -- If it is of type Communication_Block, this must be a protected
916 -- entry call. Generate:
918 -- if Enqueued (Cancel_Param) then
919 -- Cancel_Protected_Entry_Call (Cancel_Param);
920 -- end if;
922 if Is_RTE (Etype (Cancel_Param), RE_Communication_Block) then
923 Append_To (Stmts,
924 Make_If_Statement (Loc,
925 Condition =>
926 Make_Function_Call (Loc,
927 Name =>
928 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
929 Parameter_Associations => New_List (
930 New_Occurrence_Of (Cancel_Param, Loc))),
932 Then_Statements => New_List (
933 Make_Procedure_Call_Statement (Loc,
934 Name =>
935 New_Occurrence_Of
936 (RTE (RE_Cancel_Protected_Entry_Call), Loc),
937 Parameter_Associations => New_List (
938 New_Occurrence_Of (Cancel_Param, Loc))))));
940 -- Asynchronous delay, generate:
941 -- Cancel_Async_Delay (Cancel_Param);
943 elsif Is_RTE (Etype (Cancel_Param), RE_Delay_Block) then
944 Append_To (Stmts,
945 Make_Procedure_Call_Statement (Loc,
946 Name =>
947 New_Occurrence_Of (RTE (RE_Cancel_Async_Delay), Loc),
948 Parameter_Associations => New_List (
949 Make_Attribute_Reference (Loc,
950 Prefix =>
951 New_Occurrence_Of (Cancel_Param, Loc),
952 Attribute_Name => Name_Unchecked_Access))));
954 -- Task entry call, generate:
955 -- Cancel_Task_Entry_Call (Cancel_Param);
957 else
958 Append_To (Stmts,
959 Make_Procedure_Call_Statement (Loc,
960 Name =>
961 New_Occurrence_Of (RTE (RE_Cancel_Task_Entry_Call), Loc),
962 Parameter_Associations => New_List (
963 New_Occurrence_Of (Cancel_Param, Loc))));
964 end if;
965 end;
966 end if;
968 Append_List_To (Stmts, Additional_Cleanup);
969 return Stmts;
970 end Build_Cleanup_Statements;
972 -----------------------------
973 -- Build_Controlling_Procs --
974 -----------------------------
976 procedure Build_Controlling_Procs (Typ : Entity_Id) is
977 begin
978 if Is_Array_Type (Typ) then
979 Build_Array_Deep_Procs (Typ);
980 else pragma Assert (Is_Record_Type (Typ));
981 Build_Record_Deep_Procs (Typ);
982 end if;
983 end Build_Controlling_Procs;
985 -----------------------------
986 -- Build_Exception_Handler --
987 -----------------------------
989 function Build_Exception_Handler
990 (Data : Finalization_Exception_Data;
991 For_Library : Boolean := False) return Node_Id
993 Actuals : List_Id;
994 Proc_To_Call : Entity_Id;
995 Except : Node_Id;
996 Stmts : List_Id;
998 begin
999 pragma Assert (Present (Data.Raised_Id));
1001 if Exception_Extra_Info
1002 or else (For_Library and not Restricted_Profile)
1003 then
1004 if Exception_Extra_Info then
1006 -- Generate:
1008 -- Get_Current_Excep.all
1010 Except :=
1011 Make_Function_Call (Data.Loc,
1012 Name =>
1013 Make_Explicit_Dereference (Data.Loc,
1014 Prefix =>
1015 New_Occurrence_Of
1016 (RTE (RE_Get_Current_Excep), Data.Loc)));
1018 else
1019 -- Generate:
1021 -- null
1023 Except := Make_Null (Data.Loc);
1024 end if;
1026 if For_Library and then not Restricted_Profile then
1027 Proc_To_Call := RTE (RE_Save_Library_Occurrence);
1028 Actuals := New_List (Except);
1030 else
1031 Proc_To_Call := RTE (RE_Save_Occurrence);
1033 -- The dereference occurs only when Exception_Extra_Info is true,
1034 -- and therefore Except is not null.
1036 Actuals :=
1037 New_List (
1038 New_Occurrence_Of (Data.E_Id, Data.Loc),
1039 Make_Explicit_Dereference (Data.Loc, Except));
1040 end if;
1042 -- Generate:
1044 -- when others =>
1045 -- if not Raised_Id then
1046 -- Raised_Id := True;
1048 -- Save_Occurrence (E_Id, Get_Current_Excep.all.all);
1049 -- or
1050 -- Save_Library_Occurrence (Get_Current_Excep.all);
1051 -- end if;
1053 Stmts :=
1054 New_List (
1055 Make_If_Statement (Data.Loc,
1056 Condition =>
1057 Make_Op_Not (Data.Loc,
1058 Right_Opnd => New_Occurrence_Of (Data.Raised_Id, Data.Loc)),
1060 Then_Statements => New_List (
1061 Make_Assignment_Statement (Data.Loc,
1062 Name => New_Occurrence_Of (Data.Raised_Id, Data.Loc),
1063 Expression => New_Occurrence_Of (Standard_True, Data.Loc)),
1065 Make_Procedure_Call_Statement (Data.Loc,
1066 Name =>
1067 New_Occurrence_Of (Proc_To_Call, Data.Loc),
1068 Parameter_Associations => Actuals))));
1070 else
1071 -- Generate:
1073 -- Raised_Id := True;
1075 Stmts := New_List (
1076 Make_Assignment_Statement (Data.Loc,
1077 Name => New_Occurrence_Of (Data.Raised_Id, Data.Loc),
1078 Expression => New_Occurrence_Of (Standard_True, Data.Loc)));
1079 end if;
1081 -- Generate:
1083 -- when others =>
1085 return
1086 Make_Exception_Handler (Data.Loc,
1087 Exception_Choices => New_List (Make_Others_Choice (Data.Loc)),
1088 Statements => Stmts);
1089 end Build_Exception_Handler;
1091 -------------------------------
1092 -- Build_Finalization_Master --
1093 -------------------------------
1095 procedure Build_Finalization_Master
1096 (Typ : Entity_Id;
1097 For_Lib_Level : Boolean := False;
1098 For_Private : Boolean := False;
1099 Context_Scope : Entity_Id := Empty;
1100 Insertion_Node : Node_Id := Empty)
1102 procedure Add_Pending_Access_Type
1103 (Typ : Entity_Id;
1104 Ptr_Typ : Entity_Id);
1105 -- Add access type Ptr_Typ to the pending access type list for type Typ
1107 -----------------------------
1108 -- Add_Pending_Access_Type --
1109 -----------------------------
1111 procedure Add_Pending_Access_Type
1112 (Typ : Entity_Id;
1113 Ptr_Typ : Entity_Id)
1115 List : Elist_Id;
1117 begin
1118 if Present (Pending_Access_Types (Typ)) then
1119 List := Pending_Access_Types (Typ);
1120 else
1121 List := New_Elmt_List;
1122 Set_Pending_Access_Types (Typ, List);
1123 end if;
1125 Prepend_Elmt (Ptr_Typ, List);
1126 end Add_Pending_Access_Type;
1128 -- Local variables
1130 Desig_Typ : constant Entity_Id := Designated_Type (Typ);
1132 Ptr_Typ : constant Entity_Id := Root_Type_Of_Full_View (Base_Type (Typ));
1133 -- A finalization master created for a named access type is associated
1134 -- with the full view (if applicable) as a consequence of freezing. The
1135 -- full view criteria does not apply to anonymous access types because
1136 -- those cannot have a private and a full view.
1138 -- Start of processing for Build_Finalization_Master
1140 begin
1141 -- Nothing to do if the circumstances do not allow for a finalization
1142 -- master.
1144 if not Allows_Finalization_Master (Typ) then
1145 return;
1147 -- Various machinery such as freezing may have already created a
1148 -- finalization master.
1150 elsif Present (Finalization_Master (Ptr_Typ)) then
1151 return;
1152 end if;
1154 declare
1155 Actions : constant List_Id := New_List;
1156 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
1157 Fin_Mas_Id : Entity_Id;
1158 Pool_Id : Entity_Id;
1160 begin
1161 -- Source access types use fixed master names since the master is
1162 -- inserted in the same source unit only once. The only exception to
1163 -- this are instances using the same access type as generic actual.
1165 if Comes_From_Source (Ptr_Typ) and then not Inside_A_Generic then
1166 Fin_Mas_Id :=
1167 Make_Defining_Identifier (Loc,
1168 Chars => New_External_Name (Chars (Ptr_Typ), "FM"));
1170 -- Internally generated access types use temporaries as their names
1171 -- due to possible collision with identical names coming from other
1172 -- packages.
1174 else
1175 Fin_Mas_Id := Make_Temporary (Loc, 'F');
1176 end if;
1178 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
1180 -- Generate:
1181 -- <Ptr_Typ>FM : aliased Finalization_Master;
1183 Append_To (Actions,
1184 Make_Object_Declaration (Loc,
1185 Defining_Identifier => Fin_Mas_Id,
1186 Aliased_Present => True,
1187 Object_Definition =>
1188 New_Occurrence_Of (RTE (RE_Finalization_Master), Loc)));
1190 -- Set the associated pool and primitive Finalize_Address of the new
1191 -- finalization master.
1193 -- The access type has a user-defined storage pool, use it
1195 if Present (Associated_Storage_Pool (Ptr_Typ)) then
1196 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
1198 -- Otherwise the default choice is the global storage pool
1200 else
1201 Pool_Id := RTE (RE_Global_Pool_Object);
1202 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
1203 end if;
1205 -- Generate:
1206 -- Set_Base_Pool (<Ptr_Typ>FM, Pool_Id'Unchecked_Access);
1208 Append_To (Actions,
1209 Make_Procedure_Call_Statement (Loc,
1210 Name =>
1211 New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc),
1212 Parameter_Associations => New_List (
1213 New_Occurrence_Of (Fin_Mas_Id, Loc),
1214 Make_Attribute_Reference (Loc,
1215 Prefix => New_Occurrence_Of (Pool_Id, Loc),
1216 Attribute_Name => Name_Unrestricted_Access))));
1218 -- Finalize_Address is not generated in CodePeer mode because the
1219 -- body contains address arithmetic. Skip this step.
1221 if CodePeer_Mode then
1222 null;
1224 -- Associate the Finalize_Address primitive of the designated type
1225 -- with the finalization master of the access type. The designated
1226 -- type must be forzen as Finalize_Address is generated when the
1227 -- freeze node is expanded.
1229 elsif Is_Frozen (Desig_Typ)
1230 and then Present (Finalize_Address (Desig_Typ))
1232 -- The finalization master of an anonymous access type may need
1233 -- to be inserted in a specific place in the tree. For instance:
1235 -- type Comp_Typ;
1237 -- <finalization master of "access Comp_Typ">
1239 -- type Rec_Typ is record
1240 -- Comp : access Comp_Typ;
1241 -- end record;
1243 -- <freeze node for Comp_Typ>
1244 -- <freeze node for Rec_Typ>
1246 -- Due to this oddity, the anonymous access type is stored for
1247 -- later processing (see below).
1249 and then Ekind (Ptr_Typ) /= E_Anonymous_Access_Type
1250 then
1251 -- Generate:
1252 -- Set_Finalize_Address
1253 -- (<Ptr_Typ>FM, <Desig_Typ>FD'Unrestricted_Access);
1255 Append_To (Actions,
1256 Make_Set_Finalize_Address_Call
1257 (Loc => Loc,
1258 Ptr_Typ => Ptr_Typ));
1260 -- Otherwise the designated type is either anonymous access or a
1261 -- Taft-amendment type and has not been frozen. Store the access
1262 -- type for later processing (see Freeze_Type).
1264 else
1265 Add_Pending_Access_Type (Desig_Typ, Ptr_Typ);
1266 end if;
1268 -- A finalization master created for an access designating a type
1269 -- with private components is inserted before a context-dependent
1270 -- node.
1272 if For_Private then
1274 -- At this point both the scope of the context and the insertion
1275 -- mode must be known.
1277 pragma Assert (Present (Context_Scope));
1278 pragma Assert (Present (Insertion_Node));
1280 Push_Scope (Context_Scope);
1282 -- Treat use clauses as declarations and insert directly in front
1283 -- of them.
1285 if Nkind_In (Insertion_Node, N_Use_Package_Clause,
1286 N_Use_Type_Clause)
1287 then
1288 Insert_List_Before_And_Analyze (Insertion_Node, Actions);
1289 else
1290 Insert_Actions (Insertion_Node, Actions);
1291 end if;
1293 Pop_Scope;
1295 -- The finalization master belongs to an access result type related
1296 -- to a build-in-place function call used to initialize a library
1297 -- level object. The master must be inserted in front of the access
1298 -- result type declaration denoted by Insertion_Node.
1300 elsif For_Lib_Level then
1301 pragma Assert (Present (Insertion_Node));
1302 Insert_Actions (Insertion_Node, Actions);
1304 -- Otherwise the finalization master and its initialization become a
1305 -- part of the freeze node.
1307 else
1308 Append_Freeze_Actions (Ptr_Typ, Actions);
1309 end if;
1310 end;
1311 end Build_Finalization_Master;
1313 ---------------------
1314 -- Build_Finalizer --
1315 ---------------------
1317 procedure Build_Finalizer
1318 (N : Node_Id;
1319 Clean_Stmts : List_Id;
1320 Mark_Id : Entity_Id;
1321 Top_Decls : List_Id;
1322 Defer_Abort : Boolean;
1323 Fin_Id : out Entity_Id)
1325 Acts_As_Clean : constant Boolean :=
1326 Present (Mark_Id)
1327 or else
1328 (Present (Clean_Stmts)
1329 and then Is_Non_Empty_List (Clean_Stmts));
1330 Exceptions_OK : constant Boolean := Exceptions_In_Finalization_OK;
1331 For_Package_Body : constant Boolean := Nkind (N) = N_Package_Body;
1332 For_Package_Spec : constant Boolean := Nkind (N) = N_Package_Declaration;
1333 For_Package : constant Boolean :=
1334 For_Package_Body or else For_Package_Spec;
1335 Loc : constant Source_Ptr := Sloc (N);
1337 -- NOTE: Local variable declarations are conservative and do not create
1338 -- structures right from the start. Entities and lists are created once
1339 -- it has been established that N has at least one controlled object.
1341 Components_Built : Boolean := False;
1342 -- A flag used to avoid double initialization of entities and lists. If
1343 -- the flag is set then the following variables have been initialized:
1344 -- Counter_Id
1345 -- Finalizer_Decls
1346 -- Finalizer_Stmts
1347 -- Jump_Alts
1349 Counter_Id : Entity_Id := Empty;
1350 Counter_Val : Nat := 0;
1351 -- Name and value of the state counter
1353 Decls : List_Id := No_List;
1354 -- Declarative region of N (if available). If N is a package declaration
1355 -- Decls denotes the visible declarations.
1357 Finalizer_Data : Finalization_Exception_Data;
1358 -- Data for the exception
1360 Finalizer_Decls : List_Id := No_List;
1361 -- Local variable declarations. This list holds the label declarations
1362 -- of all jump block alternatives as well as the declaration of the
1363 -- local exception occurrence and the raised flag:
1364 -- E : Exception_Occurrence;
1365 -- Raised : Boolean := False;
1366 -- L<counter value> : label;
1368 Finalizer_Insert_Nod : Node_Id := Empty;
1369 -- Insertion point for the finalizer body. Depending on the context
1370 -- (Nkind of N) and the individual grouping of controlled objects, this
1371 -- node may denote a package declaration or body, package instantiation,
1372 -- block statement or a counter update statement.
1374 Finalizer_Stmts : List_Id := No_List;
1375 -- The statement list of the finalizer body. It contains the following:
1377 -- Abort_Defer; -- Added if abort is allowed
1378 -- <call to Prev_At_End> -- Added if exists
1379 -- <cleanup statements> -- Added if Acts_As_Clean
1380 -- <jump block> -- Added if Has_Ctrl_Objs
1381 -- <finalization statements> -- Added if Has_Ctrl_Objs
1382 -- <stack release> -- Added if Mark_Id exists
1383 -- Abort_Undefer; -- Added if abort is allowed
1385 Has_Ctrl_Objs : Boolean := False;
1386 -- A general flag which denotes whether N has at least one controlled
1387 -- object.
1389 Has_Tagged_Types : Boolean := False;
1390 -- A general flag which indicates whether N has at least one library-
1391 -- level tagged type declaration.
1393 HSS : Node_Id := Empty;
1394 -- The sequence of statements of N (if available)
1396 Jump_Alts : List_Id := No_List;
1397 -- Jump block alternatives. Depending on the value of the state counter,
1398 -- the control flow jumps to a sequence of finalization statements. This
1399 -- list contains the following:
1401 -- when <counter value> =>
1402 -- goto L<counter value>;
1404 Jump_Block_Insert_Nod : Node_Id := Empty;
1405 -- Specific point in the finalizer statements where the jump block is
1406 -- inserted.
1408 Last_Top_Level_Ctrl_Construct : Node_Id := Empty;
1409 -- The last controlled construct encountered when processing the top
1410 -- level lists of N. This can be a nested package, an instantiation or
1411 -- an object declaration.
1413 Prev_At_End : Entity_Id := Empty;
1414 -- The previous at end procedure of the handled statements block of N
1416 Priv_Decls : List_Id := No_List;
1417 -- The private declarations of N if N is a package declaration
1419 Spec_Id : Entity_Id := Empty;
1420 Spec_Decls : List_Id := Top_Decls;
1421 Stmts : List_Id := No_List;
1423 Tagged_Type_Stmts : List_Id := No_List;
1424 -- Contains calls to Ada.Tags.Unregister_Tag for all library-level
1425 -- tagged types found in N.
1427 -----------------------
1428 -- Local subprograms --
1429 -----------------------
1431 procedure Build_Components;
1432 -- Create all entites and initialize all lists used in the creation of
1433 -- the finalizer.
1435 procedure Create_Finalizer;
1436 -- Create the spec and body of the finalizer and insert them in the
1437 -- proper place in the tree depending on the context.
1439 procedure Process_Declarations
1440 (Decls : List_Id;
1441 Preprocess : Boolean := False;
1442 Top_Level : Boolean := False);
1443 -- Inspect a list of declarations or statements which may contain
1444 -- objects that need finalization. When flag Preprocess is set, the
1445 -- routine will simply count the total number of controlled objects in
1446 -- Decls. Flag Top_Level denotes whether the processing is done for
1447 -- objects in nested package declarations or instances.
1449 procedure Process_Object_Declaration
1450 (Decl : Node_Id;
1451 Has_No_Init : Boolean := False;
1452 Is_Protected : Boolean := False);
1453 -- Generate all the machinery associated with the finalization of a
1454 -- single object. Flag Has_No_Init is used to denote certain contexts
1455 -- where Decl does not have initialization call(s). Flag Is_Protected
1456 -- is set when Decl denotes a simple protected object.
1458 procedure Process_Tagged_Type_Declaration (Decl : Node_Id);
1459 -- Generate all the code necessary to unregister the external tag of a
1460 -- tagged type.
1462 ----------------------
1463 -- Build_Components --
1464 ----------------------
1466 procedure Build_Components is
1467 Counter_Decl : Node_Id;
1468 Counter_Typ : Entity_Id;
1469 Counter_Typ_Decl : Node_Id;
1471 begin
1472 pragma Assert (Present (Decls));
1474 -- This routine might be invoked several times when dealing with
1475 -- constructs that have two lists (either two declarative regions
1476 -- or declarations and statements). Avoid double initialization.
1478 if Components_Built then
1479 return;
1480 end if;
1482 Components_Built := True;
1484 if Has_Ctrl_Objs then
1486 -- Create entities for the counter, its type, the local exception
1487 -- and the raised flag.
1489 Counter_Id := Make_Temporary (Loc, 'C');
1490 Counter_Typ := Make_Temporary (Loc, 'T');
1492 Finalizer_Decls := New_List;
1494 Build_Object_Declarations
1495 (Finalizer_Data, Finalizer_Decls, Loc, For_Package);
1497 -- Since the total number of controlled objects is always known,
1498 -- build a subtype of Natural with precise bounds. This allows
1499 -- the backend to optimize the case statement. Generate:
1501 -- subtype Tnn is Natural range 0 .. Counter_Val;
1503 Counter_Typ_Decl :=
1504 Make_Subtype_Declaration (Loc,
1505 Defining_Identifier => Counter_Typ,
1506 Subtype_Indication =>
1507 Make_Subtype_Indication (Loc,
1508 Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
1509 Constraint =>
1510 Make_Range_Constraint (Loc,
1511 Range_Expression =>
1512 Make_Range (Loc,
1513 Low_Bound =>
1514 Make_Integer_Literal (Loc, Uint_0),
1515 High_Bound =>
1516 Make_Integer_Literal (Loc, Counter_Val)))));
1518 -- Generate the declaration of the counter itself:
1520 -- Counter : Integer := 0;
1522 Counter_Decl :=
1523 Make_Object_Declaration (Loc,
1524 Defining_Identifier => Counter_Id,
1525 Object_Definition => New_Occurrence_Of (Counter_Typ, Loc),
1526 Expression => Make_Integer_Literal (Loc, 0));
1528 -- Set the type of the counter explicitly to prevent errors when
1529 -- examining object declarations later on.
1531 Set_Etype (Counter_Id, Counter_Typ);
1533 -- The counter and its type are inserted before the source
1534 -- declarations of N.
1536 Prepend_To (Decls, Counter_Decl);
1537 Prepend_To (Decls, Counter_Typ_Decl);
1539 -- The counter and its associated type must be manually analyzed
1540 -- since N has already been analyzed. Use the scope of the spec
1541 -- when inserting in a package.
1543 if For_Package then
1544 Push_Scope (Spec_Id);
1545 Analyze (Counter_Typ_Decl);
1546 Analyze (Counter_Decl);
1547 Pop_Scope;
1549 else
1550 Analyze (Counter_Typ_Decl);
1551 Analyze (Counter_Decl);
1552 end if;
1554 Jump_Alts := New_List;
1555 end if;
1557 -- If the context requires additional clean up, the finalization
1558 -- machinery is added after the clean up code.
1560 if Acts_As_Clean then
1561 Finalizer_Stmts := Clean_Stmts;
1562 Jump_Block_Insert_Nod := Last (Finalizer_Stmts);
1563 else
1564 Finalizer_Stmts := New_List;
1565 end if;
1567 if Has_Tagged_Types then
1568 Tagged_Type_Stmts := New_List;
1569 end if;
1570 end Build_Components;
1572 ----------------------
1573 -- Create_Finalizer --
1574 ----------------------
1576 procedure Create_Finalizer is
1577 function New_Finalizer_Name return Name_Id;
1578 -- Create a fully qualified name of a package spec or body finalizer.
1579 -- The generated name is of the form: xx__yy__finalize_[spec|body].
1581 ------------------------
1582 -- New_Finalizer_Name --
1583 ------------------------
1585 function New_Finalizer_Name return Name_Id is
1586 procedure New_Finalizer_Name (Id : Entity_Id);
1587 -- Place "__<name-of-Id>" in the name buffer. If the identifier
1588 -- has a non-standard scope, process the scope first.
1590 ------------------------
1591 -- New_Finalizer_Name --
1592 ------------------------
1594 procedure New_Finalizer_Name (Id : Entity_Id) is
1595 begin
1596 if Scope (Id) = Standard_Standard then
1597 Get_Name_String (Chars (Id));
1599 else
1600 New_Finalizer_Name (Scope (Id));
1601 Add_Str_To_Name_Buffer ("__");
1602 Add_Str_To_Name_Buffer (Get_Name_String (Chars (Id)));
1603 end if;
1604 end New_Finalizer_Name;
1606 -- Start of processing for New_Finalizer_Name
1608 begin
1609 -- Create the fully qualified name of the enclosing scope
1611 New_Finalizer_Name (Spec_Id);
1613 -- Generate:
1614 -- __finalize_[spec|body]
1616 Add_Str_To_Name_Buffer ("__finalize_");
1618 if For_Package_Spec then
1619 Add_Str_To_Name_Buffer ("spec");
1620 else
1621 Add_Str_To_Name_Buffer ("body");
1622 end if;
1624 return Name_Find;
1625 end New_Finalizer_Name;
1627 -- Local variables
1629 Body_Id : Entity_Id;
1630 Fin_Body : Node_Id;
1631 Fin_Spec : Node_Id;
1632 Jump_Block : Node_Id;
1633 Label : Node_Id;
1634 Label_Id : Entity_Id;
1636 -- Start of processing for Create_Finalizer
1638 begin
1639 -- Step 1: Creation of the finalizer name
1641 -- Packages must use a distinct name for their finalizers since the
1642 -- binder will have to generate calls to them by name. The name is
1643 -- of the following form:
1645 -- xx__yy__finalize_[spec|body]
1647 if For_Package then
1648 Fin_Id := Make_Defining_Identifier (Loc, New_Finalizer_Name);
1649 Set_Has_Qualified_Name (Fin_Id);
1650 Set_Has_Fully_Qualified_Name (Fin_Id);
1652 -- The default name is _finalizer
1654 else
1655 Fin_Id :=
1656 Make_Defining_Identifier (Loc,
1657 Chars => New_External_Name (Name_uFinalizer));
1659 -- The visibility semantics of AT_END handlers force a strange
1660 -- separation of spec and body for stack-related finalizers:
1662 -- declare : Enclosing_Scope
1663 -- procedure _finalizer;
1664 -- begin
1665 -- <controlled objects>
1666 -- procedure _finalizer is
1667 -- ...
1668 -- at end
1669 -- _finalizer;
1670 -- end;
1672 -- Both spec and body are within the same construct and scope, but
1673 -- the body is part of the handled sequence of statements. This
1674 -- placement confuses the elaboration mechanism on targets where
1675 -- AT_END handlers are expanded into "when all others" handlers:
1677 -- exception
1678 -- when all others =>
1679 -- _finalizer; -- appears to require elab checks
1680 -- at end
1681 -- _finalizer;
1682 -- end;
1684 -- Since the compiler guarantees that the body of a _finalizer is
1685 -- always inserted in the same construct where the AT_END handler
1686 -- resides, there is no need for elaboration checks.
1688 Set_Kill_Elaboration_Checks (Fin_Id);
1690 -- Inlining the finalizer produces a substantial speedup at -O2.
1691 -- It is inlined by default at -O3. Either way, it is called
1692 -- exactly twice (once on the normal path, and once for
1693 -- exceptions/abort), so this won't bloat the code too much.
1695 Set_Is_Inlined (Fin_Id);
1696 end if;
1698 -- Step 2: Creation of the finalizer specification
1700 -- Generate:
1701 -- procedure Fin_Id;
1703 Fin_Spec :=
1704 Make_Subprogram_Declaration (Loc,
1705 Specification =>
1706 Make_Procedure_Specification (Loc,
1707 Defining_Unit_Name => Fin_Id));
1709 -- Step 3: Creation of the finalizer body
1711 if Has_Ctrl_Objs then
1713 -- Add L0, the default destination to the jump block
1715 Label_Id := Make_Identifier (Loc, New_External_Name ('L', 0));
1716 Set_Entity (Label_Id,
1717 Make_Defining_Identifier (Loc, Chars (Label_Id)));
1718 Label := Make_Label (Loc, Label_Id);
1720 -- Generate:
1721 -- L0 : label;
1723 Prepend_To (Finalizer_Decls,
1724 Make_Implicit_Label_Declaration (Loc,
1725 Defining_Identifier => Entity (Label_Id),
1726 Label_Construct => Label));
1728 -- Generate:
1729 -- when others =>
1730 -- goto L0;
1732 Append_To (Jump_Alts,
1733 Make_Case_Statement_Alternative (Loc,
1734 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
1735 Statements => New_List (
1736 Make_Goto_Statement (Loc,
1737 Name => New_Occurrence_Of (Entity (Label_Id), Loc)))));
1739 -- Generate:
1740 -- <<L0>>
1742 Append_To (Finalizer_Stmts, Label);
1744 -- Create the jump block which controls the finalization flow
1745 -- depending on the value of the state counter.
1747 Jump_Block :=
1748 Make_Case_Statement (Loc,
1749 Expression => Make_Identifier (Loc, Chars (Counter_Id)),
1750 Alternatives => Jump_Alts);
1752 if Acts_As_Clean and then Present (Jump_Block_Insert_Nod) then
1753 Insert_After (Jump_Block_Insert_Nod, Jump_Block);
1754 else
1755 Prepend_To (Finalizer_Stmts, Jump_Block);
1756 end if;
1757 end if;
1759 -- Add the library-level tagged type unregistration machinery before
1760 -- the jump block circuitry. This ensures that external tags will be
1761 -- removed even if a finalization exception occurs at some point.
1763 if Has_Tagged_Types then
1764 Prepend_List_To (Finalizer_Stmts, Tagged_Type_Stmts);
1765 end if;
1767 -- Add a call to the previous At_End handler if it exists. The call
1768 -- must always precede the jump block.
1770 if Present (Prev_At_End) then
1771 Prepend_To (Finalizer_Stmts,
1772 Make_Procedure_Call_Statement (Loc, Prev_At_End));
1774 -- Clear the At_End handler since we have already generated the
1775 -- proper replacement call for it.
1777 Set_At_End_Proc (HSS, Empty);
1778 end if;
1780 -- Release the secondary stack mark
1782 if Present (Mark_Id) then
1783 Append_To (Finalizer_Stmts, Build_SS_Release_Call (Loc, Mark_Id));
1784 end if;
1786 -- Protect the statements with abort defer/undefer. This is only when
1787 -- aborts are allowed and the clean up statements require deferral or
1788 -- there are controlled objects to be finalized. Note that the abort
1789 -- defer/undefer pair does not require an extra block because each
1790 -- finalization exception is caught in its corresponding finalization
1791 -- block. As a result, the call to Abort_Defer always takes place.
1793 if Abort_Allowed and then (Defer_Abort or Has_Ctrl_Objs) then
1794 Prepend_To (Finalizer_Stmts,
1795 Build_Runtime_Call (Loc, RE_Abort_Defer));
1797 Append_To (Finalizer_Stmts,
1798 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1799 end if;
1801 -- The local exception does not need to be reraised for library-level
1802 -- finalizers. Note that this action must be carried out after object
1803 -- clean up, secondary stack release and abort undeferral. Generate:
1805 -- if Raised and then not Abort then
1806 -- Raise_From_Controlled_Operation (E);
1807 -- end if;
1809 if Has_Ctrl_Objs and Exceptions_OK and not For_Package then
1810 Append_To (Finalizer_Stmts,
1811 Build_Raise_Statement (Finalizer_Data));
1812 end if;
1814 -- Generate:
1815 -- procedure Fin_Id is
1816 -- Abort : constant Boolean := Triggered_By_Abort;
1817 -- <or>
1818 -- Abort : constant Boolean := False; -- no abort
1820 -- E : Exception_Occurrence; -- All added if flag
1821 -- Raised : Boolean := False; -- Has_Ctrl_Objs is set
1822 -- L0 : label;
1823 -- ...
1824 -- Lnn : label;
1826 -- begin
1827 -- Abort_Defer; -- Added if abort is allowed
1828 -- <call to Prev_At_End> -- Added if exists
1829 -- <cleanup statements> -- Added if Acts_As_Clean
1830 -- <jump block> -- Added if Has_Ctrl_Objs
1831 -- <finalization statements> -- Added if Has_Ctrl_Objs
1832 -- <stack release> -- Added if Mark_Id exists
1833 -- Abort_Undefer; -- Added if abort is allowed
1834 -- <exception propagation> -- Added if Has_Ctrl_Objs
1835 -- end Fin_Id;
1837 -- Create the body of the finalizer
1839 Body_Id := Make_Defining_Identifier (Loc, Chars (Fin_Id));
1841 if For_Package then
1842 Set_Has_Qualified_Name (Body_Id);
1843 Set_Has_Fully_Qualified_Name (Body_Id);
1844 end if;
1846 Fin_Body :=
1847 Make_Subprogram_Body (Loc,
1848 Specification =>
1849 Make_Procedure_Specification (Loc,
1850 Defining_Unit_Name => Body_Id),
1851 Declarations => Finalizer_Decls,
1852 Handled_Statement_Sequence =>
1853 Make_Handled_Sequence_Of_Statements (Loc,
1854 Statements => Finalizer_Stmts));
1856 -- Step 4: Spec and body insertion, analysis
1858 if For_Package then
1860 -- If the package spec has private declarations, the finalizer
1861 -- body must be added to the end of the list in order to have
1862 -- visibility of all private controlled objects.
1864 if For_Package_Spec then
1865 if Present (Priv_Decls) then
1866 Append_To (Priv_Decls, Fin_Spec);
1867 Append_To (Priv_Decls, Fin_Body);
1868 else
1869 Append_To (Decls, Fin_Spec);
1870 Append_To (Decls, Fin_Body);
1871 end if;
1873 -- For package bodies, both the finalizer spec and body are
1874 -- inserted at the end of the package declarations.
1876 else
1877 Append_To (Decls, Fin_Spec);
1878 Append_To (Decls, Fin_Body);
1879 end if;
1881 -- Push the name of the package
1883 Push_Scope (Spec_Id);
1884 Analyze (Fin_Spec);
1885 Analyze (Fin_Body);
1886 Pop_Scope;
1888 -- Non-package case
1890 else
1891 -- Create the spec for the finalizer. The At_End handler must be
1892 -- able to call the body which resides in a nested structure.
1894 -- Generate:
1895 -- declare
1896 -- procedure Fin_Id; -- Spec
1897 -- begin
1898 -- <objects and possibly statements>
1899 -- procedure Fin_Id is ... -- Body
1900 -- <statements>
1901 -- at end
1902 -- Fin_Id; -- At_End handler
1903 -- end;
1905 pragma Assert (Present (Spec_Decls));
1907 Append_To (Spec_Decls, Fin_Spec);
1908 Analyze (Fin_Spec);
1910 -- When the finalizer acts solely as a clean up routine, the body
1911 -- is inserted right after the spec.
1913 if Acts_As_Clean and not Has_Ctrl_Objs then
1914 Insert_After (Fin_Spec, Fin_Body);
1916 -- In all other cases the body is inserted after either:
1918 -- 1) The counter update statement of the last controlled object
1919 -- 2) The last top level nested controlled package
1920 -- 3) The last top level controlled instantiation
1922 else
1923 -- Manually freeze the spec. This is somewhat of a hack because
1924 -- a subprogram is frozen when its body is seen and the freeze
1925 -- node appears right before the body. However, in this case,
1926 -- the spec must be frozen earlier since the At_End handler
1927 -- must be able to call it.
1929 -- declare
1930 -- procedure Fin_Id; -- Spec
1931 -- [Fin_Id] -- Freeze node
1932 -- begin
1933 -- ...
1934 -- at end
1935 -- Fin_Id; -- At_End handler
1936 -- end;
1938 Ensure_Freeze_Node (Fin_Id);
1939 Insert_After (Fin_Spec, Freeze_Node (Fin_Id));
1940 Set_Is_Frozen (Fin_Id);
1942 -- In the case where the last construct to contain a controlled
1943 -- object is either a nested package, an instantiation or a
1944 -- freeze node, the body must be inserted directly after the
1945 -- construct.
1947 if Nkind_In (Last_Top_Level_Ctrl_Construct,
1948 N_Freeze_Entity,
1949 N_Package_Declaration,
1950 N_Package_Body)
1951 then
1952 Finalizer_Insert_Nod := Last_Top_Level_Ctrl_Construct;
1953 end if;
1955 Insert_After (Finalizer_Insert_Nod, Fin_Body);
1956 end if;
1958 Analyze (Fin_Body);
1959 end if;
1960 end Create_Finalizer;
1962 --------------------------
1963 -- Process_Declarations --
1964 --------------------------
1966 procedure Process_Declarations
1967 (Decls : List_Id;
1968 Preprocess : Boolean := False;
1969 Top_Level : Boolean := False)
1971 Decl : Node_Id;
1972 Expr : Node_Id;
1973 Obj_Id : Entity_Id;
1974 Obj_Typ : Entity_Id;
1975 Pack_Id : Entity_Id;
1976 Spec : Node_Id;
1977 Typ : Entity_Id;
1979 Old_Counter_Val : Nat;
1980 -- This variable is used to determine whether a nested package or
1981 -- instance contains at least one controlled object.
1983 procedure Processing_Actions
1984 (Has_No_Init : Boolean := False;
1985 Is_Protected : Boolean := False);
1986 -- Depending on the mode of operation of Process_Declarations, either
1987 -- increment the controlled object counter, set the controlled object
1988 -- flag and store the last top level construct or process the current
1989 -- declaration. Flag Has_No_Init is used to propagate scenarios where
1990 -- the current declaration may not have initialization proc(s). Flag
1991 -- Is_Protected should be set when the current declaration denotes a
1992 -- simple protected object.
1994 ------------------------
1995 -- Processing_Actions --
1996 ------------------------
1998 procedure Processing_Actions
1999 (Has_No_Init : Boolean := False;
2000 Is_Protected : Boolean := False)
2002 begin
2003 -- Library-level tagged type
2005 if Nkind (Decl) = N_Full_Type_Declaration then
2006 if Preprocess then
2007 Has_Tagged_Types := True;
2009 if Top_Level and then No (Last_Top_Level_Ctrl_Construct) then
2010 Last_Top_Level_Ctrl_Construct := Decl;
2011 end if;
2013 else
2014 Process_Tagged_Type_Declaration (Decl);
2015 end if;
2017 -- Controlled object declaration
2019 else
2020 if Preprocess then
2021 Counter_Val := Counter_Val + 1;
2022 Has_Ctrl_Objs := True;
2024 if Top_Level and then No (Last_Top_Level_Ctrl_Construct) then
2025 Last_Top_Level_Ctrl_Construct := Decl;
2026 end if;
2028 else
2029 Process_Object_Declaration (Decl, Has_No_Init, Is_Protected);
2030 end if;
2031 end if;
2032 end Processing_Actions;
2034 -- Start of processing for Process_Declarations
2036 begin
2037 if No (Decls) or else Is_Empty_List (Decls) then
2038 return;
2039 end if;
2041 -- Process all declarations in reverse order
2043 Decl := Last_Non_Pragma (Decls);
2044 while Present (Decl) loop
2046 -- Library-level tagged types
2048 if Nkind (Decl) = N_Full_Type_Declaration then
2049 Typ := Defining_Identifier (Decl);
2051 -- Ignored Ghost types do not need any cleanup actions because
2052 -- they will not appear in the final tree.
2054 if Is_Ignored_Ghost_Entity (Typ) then
2055 null;
2057 elsif Is_Tagged_Type (Typ)
2058 and then Is_Library_Level_Entity (Typ)
2059 and then Convention (Typ) = Convention_Ada
2060 and then Present (Access_Disp_Table (Typ))
2061 and then RTE_Available (RE_Register_Tag)
2062 and then not Is_Abstract_Type (Typ)
2063 and then not No_Run_Time_Mode
2064 then
2065 Processing_Actions;
2066 end if;
2068 -- Regular object declarations
2070 elsif Nkind (Decl) = N_Object_Declaration then
2071 Obj_Id := Defining_Identifier (Decl);
2072 Obj_Typ := Base_Type (Etype (Obj_Id));
2073 Expr := Expression (Decl);
2075 -- Bypass any form of processing for objects which have their
2076 -- finalization disabled. This applies only to objects at the
2077 -- library level.
2079 if For_Package and then Finalize_Storage_Only (Obj_Typ) then
2080 null;
2082 -- Finalization of transient objects are treated separately in
2083 -- order to handle sensitive cases. These include:
2085 -- * Aggregate expansion
2086 -- * If, case, and expression with actions expansion
2087 -- * Transient scopes
2089 -- If one of those contexts has marked the transient object as
2090 -- ignored, do not generate finalization actions for it.
2092 elsif Is_Finalized_Transient (Obj_Id)
2093 or else Is_Ignored_Transient (Obj_Id)
2094 then
2095 null;
2097 -- Ignored Ghost objects do not need any cleanup actions
2098 -- because they will not appear in the final tree.
2100 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
2101 null;
2103 -- The object is of the form:
2104 -- Obj : [constant] Typ [:= Expr];
2106 -- Do not process tag-to-class-wide conversions because they do
2107 -- not yield an object. Do not process the incomplete view of a
2108 -- deferred constant. Note that an object initialized by means
2109 -- of a build-in-place function call may appear as a deferred
2110 -- constant after expansion activities. These kinds of objects
2111 -- must be finalized.
2113 elsif not Is_Imported (Obj_Id)
2114 and then Needs_Finalization (Obj_Typ)
2115 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
2116 and then not (Ekind (Obj_Id) = E_Constant
2117 and then not Has_Completion (Obj_Id)
2118 and then No (BIP_Initialization_Call (Obj_Id)))
2119 then
2120 Processing_Actions;
2122 -- The object is of the form:
2123 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
2125 -- Obj : Access_Typ :=
2126 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
2128 elsif Is_Access_Type (Obj_Typ)
2129 and then Needs_Finalization
2130 (Available_View (Designated_Type (Obj_Typ)))
2131 and then Present (Expr)
2132 and then
2133 (Is_Secondary_Stack_BIP_Func_Call (Expr)
2134 or else
2135 (Is_Non_BIP_Func_Call (Expr)
2136 and then not Is_Related_To_Func_Return (Obj_Id)))
2137 then
2138 Processing_Actions (Has_No_Init => True);
2140 -- Processing for "hook" objects generated for transient
2141 -- objects declared inside an Expression_With_Actions.
2143 elsif Is_Access_Type (Obj_Typ)
2144 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
2145 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
2146 N_Object_Declaration
2147 then
2148 Processing_Actions (Has_No_Init => True);
2150 -- Process intermediate results of an if expression with one
2151 -- of the alternatives using a controlled function call.
2153 elsif Is_Access_Type (Obj_Typ)
2154 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
2155 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
2156 N_Defining_Identifier
2157 and then Present (Expr)
2158 and then Nkind (Expr) = N_Null
2159 then
2160 Processing_Actions (Has_No_Init => True);
2162 -- Simple protected objects which use type System.Tasking.
2163 -- Protected_Objects.Protection to manage their locks should
2164 -- be treated as controlled since they require manual cleanup.
2165 -- The only exception is illustrated in the following example:
2167 -- package Pkg is
2168 -- type Ctrl is new Controlled ...
2169 -- procedure Finalize (Obj : in out Ctrl);
2170 -- Lib_Obj : Ctrl;
2171 -- end Pkg;
2173 -- package body Pkg is
2174 -- protected Prot is
2175 -- procedure Do_Something (Obj : in out Ctrl);
2176 -- end Prot;
2178 -- protected body Prot is
2179 -- procedure Do_Something (Obj : in out Ctrl) is ...
2180 -- end Prot;
2182 -- procedure Finalize (Obj : in out Ctrl) is
2183 -- begin
2184 -- Prot.Do_Something (Obj);
2185 -- end Finalize;
2186 -- end Pkg;
2188 -- Since for the most part entities in package bodies depend on
2189 -- those in package specs, Prot's lock should be cleaned up
2190 -- first. The subsequent cleanup of the spec finalizes Lib_Obj.
2191 -- This act however attempts to invoke Do_Something and fails
2192 -- because the lock has disappeared.
2194 elsif Ekind (Obj_Id) = E_Variable
2195 and then not In_Library_Level_Package_Body (Obj_Id)
2196 and then (Is_Simple_Protected_Type (Obj_Typ)
2197 or else Has_Simple_Protected_Object (Obj_Typ))
2198 then
2199 Processing_Actions (Is_Protected => True);
2200 end if;
2202 -- Specific cases of object renamings
2204 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
2205 Obj_Id := Defining_Identifier (Decl);
2206 Obj_Typ := Base_Type (Etype (Obj_Id));
2208 -- Bypass any form of processing for objects which have their
2209 -- finalization disabled. This applies only to objects at the
2210 -- library level.
2212 if For_Package and then Finalize_Storage_Only (Obj_Typ) then
2213 null;
2215 -- Ignored Ghost object renamings do not need any cleanup
2216 -- actions because they will not appear in the final tree.
2218 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
2219 null;
2221 -- Return object of a build-in-place function. This case is
2222 -- recognized and marked by the expansion of an extended return
2223 -- statement (see Expand_N_Extended_Return_Statement).
2225 elsif Needs_Finalization (Obj_Typ)
2226 and then Is_Return_Object (Obj_Id)
2227 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
2228 then
2229 Processing_Actions (Has_No_Init => True);
2231 -- Detect a case where a source object has been initialized by
2232 -- a controlled function call or another object which was later
2233 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
2235 -- Obj1 : CW_Type := Src_Obj;
2236 -- Obj2 : CW_Type := Function_Call (...);
2238 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
2239 -- Tmp : ... := Function_Call (...)'reference;
2240 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
2242 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
2243 Processing_Actions (Has_No_Init => True);
2244 end if;
2246 -- Inspect the freeze node of an access-to-controlled type and
2247 -- look for a delayed finalization master. This case arises when
2248 -- the freeze actions are inserted at a later time than the
2249 -- expansion of the context. Since Build_Finalizer is never called
2250 -- on a single construct twice, the master will be ultimately
2251 -- left out and never finalized. This is also needed for freeze
2252 -- actions of designated types themselves, since in some cases the
2253 -- finalization master is associated with a designated type's
2254 -- freeze node rather than that of the access type (see handling
2255 -- for freeze actions in Build_Finalization_Master).
2257 elsif Nkind (Decl) = N_Freeze_Entity
2258 and then Present (Actions (Decl))
2259 then
2260 Typ := Entity (Decl);
2262 -- Freeze nodes for ignored Ghost types do not need cleanup
2263 -- actions because they will never appear in the final tree.
2265 if Is_Ignored_Ghost_Entity (Typ) then
2266 null;
2268 elsif (Is_Access_Type (Typ)
2269 and then not Is_Access_Subprogram_Type (Typ)
2270 and then Needs_Finalization
2271 (Available_View (Designated_Type (Typ))))
2272 or else (Is_Type (Typ) and then Needs_Finalization (Typ))
2273 then
2274 Old_Counter_Val := Counter_Val;
2276 -- Freeze nodes are considered to be identical to packages
2277 -- and blocks in terms of nesting. The difference is that
2278 -- a finalization master created inside the freeze node is
2279 -- at the same nesting level as the node itself.
2281 Process_Declarations (Actions (Decl), Preprocess);
2283 -- The freeze node contains a finalization master
2285 if Preprocess
2286 and then Top_Level
2287 and then No (Last_Top_Level_Ctrl_Construct)
2288 and then Counter_Val > Old_Counter_Val
2289 then
2290 Last_Top_Level_Ctrl_Construct := Decl;
2291 end if;
2292 end if;
2294 -- Nested package declarations, avoid generics
2296 elsif Nkind (Decl) = N_Package_Declaration then
2297 Pack_Id := Defining_Entity (Decl);
2298 Spec := Specification (Decl);
2300 -- Do not inspect an ignored Ghost package because all code
2301 -- found within will not appear in the final tree.
2303 if Is_Ignored_Ghost_Entity (Pack_Id) then
2304 null;
2306 elsif Ekind (Pack_Id) /= E_Generic_Package then
2307 Old_Counter_Val := Counter_Val;
2308 Process_Declarations
2309 (Private_Declarations (Spec), Preprocess);
2310 Process_Declarations
2311 (Visible_Declarations (Spec), Preprocess);
2313 -- Either the visible or the private declarations contain a
2314 -- controlled object. The nested package declaration is the
2315 -- last such construct.
2317 if Preprocess
2318 and then Top_Level
2319 and then No (Last_Top_Level_Ctrl_Construct)
2320 and then Counter_Val > Old_Counter_Val
2321 then
2322 Last_Top_Level_Ctrl_Construct := Decl;
2323 end if;
2324 end if;
2326 -- Nested package bodies, avoid generics
2328 elsif Nkind (Decl) = N_Package_Body then
2330 -- Do not inspect an ignored Ghost package body because all
2331 -- code found within will not appear in the final tree.
2333 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
2334 null;
2336 elsif Ekind (Corresponding_Spec (Decl)) /=
2337 E_Generic_Package
2338 then
2339 Old_Counter_Val := Counter_Val;
2340 Process_Declarations (Declarations (Decl), Preprocess);
2342 -- The nested package body is the last construct to contain
2343 -- a controlled object.
2345 if Preprocess
2346 and then Top_Level
2347 and then No (Last_Top_Level_Ctrl_Construct)
2348 and then Counter_Val > Old_Counter_Val
2349 then
2350 Last_Top_Level_Ctrl_Construct := Decl;
2351 end if;
2352 end if;
2354 -- Handle a rare case caused by a controlled transient object
2355 -- created as part of a record init proc. The variable is wrapped
2356 -- in a block, but the block is not associated with a transient
2357 -- scope.
2359 elsif Nkind (Decl) = N_Block_Statement
2360 and then Inside_Init_Proc
2361 then
2362 Old_Counter_Val := Counter_Val;
2364 if Present (Handled_Statement_Sequence (Decl)) then
2365 Process_Declarations
2366 (Statements (Handled_Statement_Sequence (Decl)),
2367 Preprocess);
2368 end if;
2370 Process_Declarations (Declarations (Decl), Preprocess);
2372 -- Either the declaration or statement list of the block has a
2373 -- controlled object.
2375 if Preprocess
2376 and then Top_Level
2377 and then No (Last_Top_Level_Ctrl_Construct)
2378 and then Counter_Val > Old_Counter_Val
2379 then
2380 Last_Top_Level_Ctrl_Construct := Decl;
2381 end if;
2383 -- Handle the case where the original context has been wrapped in
2384 -- a block to avoid interference between exception handlers and
2385 -- At_End handlers. Treat the block as transparent and process its
2386 -- contents.
2388 elsif Nkind (Decl) = N_Block_Statement
2389 and then Is_Finalization_Wrapper (Decl)
2390 then
2391 if Present (Handled_Statement_Sequence (Decl)) then
2392 Process_Declarations
2393 (Statements (Handled_Statement_Sequence (Decl)),
2394 Preprocess);
2395 end if;
2397 Process_Declarations (Declarations (Decl), Preprocess);
2398 end if;
2400 Prev_Non_Pragma (Decl);
2401 end loop;
2402 end Process_Declarations;
2404 --------------------------------
2405 -- Process_Object_Declaration --
2406 --------------------------------
2408 procedure Process_Object_Declaration
2409 (Decl : Node_Id;
2410 Has_No_Init : Boolean := False;
2411 Is_Protected : Boolean := False)
2413 Loc : constant Source_Ptr := Sloc (Decl);
2414 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
2416 Init_Typ : Entity_Id;
2417 -- The initialization type of the related object declaration. Note
2418 -- that this is not necessarily the same type as Obj_Typ because of
2419 -- possible type derivations.
2421 Obj_Typ : Entity_Id;
2422 -- The type of the related object declaration
2424 function Build_BIP_Cleanup_Stmts (Func_Id : Entity_Id) return Node_Id;
2425 -- Func_Id denotes a build-in-place function. Generate the following
2426 -- cleanup code:
2428 -- if BIPallocfrom > Secondary_Stack'Pos
2429 -- and then BIPfinalizationmaster /= null
2430 -- then
2431 -- declare
2432 -- type Ptr_Typ is access Obj_Typ;
2433 -- for Ptr_Typ'Storage_Pool
2434 -- use Base_Pool (BIPfinalizationmaster);
2435 -- begin
2436 -- Free (Ptr_Typ (Temp));
2437 -- end;
2438 -- end if;
2440 -- Obj_Typ is the type of the current object, Temp is the original
2441 -- allocation which Obj_Id renames.
2443 procedure Find_Last_Init
2444 (Last_Init : out Node_Id;
2445 Body_Insert : out Node_Id);
2446 -- Find the last initialization call related to object declaration
2447 -- Decl. Last_Init denotes the last initialization call which follows
2448 -- Decl. Body_Insert denotes a node where the finalizer body could be
2449 -- potentially inserted after (if blocks are involved).
2451 -----------------------------
2452 -- Build_BIP_Cleanup_Stmts --
2453 -----------------------------
2455 function Build_BIP_Cleanup_Stmts
2456 (Func_Id : Entity_Id) return Node_Id
2458 Decls : constant List_Id := New_List;
2459 Fin_Mas_Id : constant Entity_Id :=
2460 Build_In_Place_Formal
2461 (Func_Id, BIP_Finalization_Master);
2462 Func_Typ : constant Entity_Id := Etype (Func_Id);
2463 Temp_Id : constant Entity_Id :=
2464 Entity (Prefix (Name (Parent (Obj_Id))));
2466 Cond : Node_Id;
2467 Free_Blk : Node_Id;
2468 Free_Stmt : Node_Id;
2469 Pool_Id : Entity_Id;
2470 Ptr_Typ : Entity_Id;
2472 begin
2473 -- Generate:
2474 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
2476 Pool_Id := Make_Temporary (Loc, 'P');
2478 Append_To (Decls,
2479 Make_Object_Renaming_Declaration (Loc,
2480 Defining_Identifier => Pool_Id,
2481 Subtype_Mark =>
2482 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
2483 Name =>
2484 Make_Explicit_Dereference (Loc,
2485 Prefix =>
2486 Make_Function_Call (Loc,
2487 Name =>
2488 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
2489 Parameter_Associations => New_List (
2490 Make_Explicit_Dereference (Loc,
2491 Prefix =>
2492 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
2494 -- Create an access type which uses the storage pool of the
2495 -- caller's finalization master.
2497 -- Generate:
2498 -- type Ptr_Typ is access Func_Typ;
2500 Ptr_Typ := Make_Temporary (Loc, 'P');
2502 Append_To (Decls,
2503 Make_Full_Type_Declaration (Loc,
2504 Defining_Identifier => Ptr_Typ,
2505 Type_Definition =>
2506 Make_Access_To_Object_Definition (Loc,
2507 Subtype_Indication => New_Occurrence_Of (Func_Typ, Loc))));
2509 -- Perform minor decoration in order to set the master and the
2510 -- storage pool attributes.
2512 Set_Ekind (Ptr_Typ, E_Access_Type);
2513 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
2514 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
2516 -- Create an explicit free statement. Note that the free uses the
2517 -- caller's pool expressed as a renaming.
2519 Free_Stmt :=
2520 Make_Free_Statement (Loc,
2521 Expression =>
2522 Unchecked_Convert_To (Ptr_Typ,
2523 New_Occurrence_Of (Temp_Id, Loc)));
2525 Set_Storage_Pool (Free_Stmt, Pool_Id);
2527 -- Create a block to house the dummy type and the instantiation as
2528 -- well as to perform the cleanup the temporary.
2530 -- Generate:
2531 -- declare
2532 -- <Decls>
2533 -- begin
2534 -- Free (Ptr_Typ (Temp_Id));
2535 -- end;
2537 Free_Blk :=
2538 Make_Block_Statement (Loc,
2539 Declarations => Decls,
2540 Handled_Statement_Sequence =>
2541 Make_Handled_Sequence_Of_Statements (Loc,
2542 Statements => New_List (Free_Stmt)));
2544 -- Generate:
2545 -- if BIPfinalizationmaster /= null then
2547 Cond :=
2548 Make_Op_Ne (Loc,
2549 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
2550 Right_Opnd => Make_Null (Loc));
2552 -- For constrained or tagged results escalate the condition to
2553 -- include the allocation format. Generate:
2555 -- if BIPallocform > Secondary_Stack'Pos
2556 -- and then BIPfinalizationmaster /= null
2557 -- then
2559 if not Is_Constrained (Func_Typ)
2560 or else Is_Tagged_Type (Func_Typ)
2561 then
2562 declare
2563 Alloc : constant Entity_Id :=
2564 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
2565 begin
2566 Cond :=
2567 Make_And_Then (Loc,
2568 Left_Opnd =>
2569 Make_Op_Gt (Loc,
2570 Left_Opnd => New_Occurrence_Of (Alloc, Loc),
2571 Right_Opnd =>
2572 Make_Integer_Literal (Loc,
2573 UI_From_Int
2574 (BIP_Allocation_Form'Pos (Secondary_Stack)))),
2576 Right_Opnd => Cond);
2577 end;
2578 end if;
2580 -- Generate:
2581 -- if <Cond> then
2582 -- <Free_Blk>
2583 -- end if;
2585 return
2586 Make_If_Statement (Loc,
2587 Condition => Cond,
2588 Then_Statements => New_List (Free_Blk));
2589 end Build_BIP_Cleanup_Stmts;
2591 --------------------
2592 -- Find_Last_Init --
2593 --------------------
2595 procedure Find_Last_Init
2596 (Last_Init : out Node_Id;
2597 Body_Insert : out Node_Id)
2599 function Find_Last_Init_In_Block (Blk : Node_Id) return Node_Id;
2600 -- Find the last initialization call within the statements of
2601 -- block Blk.
2603 function Is_Init_Call (N : Node_Id) return Boolean;
2604 -- Determine whether node N denotes one of the initialization
2605 -- procedures of types Init_Typ or Obj_Typ.
2607 function Next_Suitable_Statement (Stmt : Node_Id) return Node_Id;
2608 -- Given a statement which is part of a list, return the next
2609 -- statement while skipping over dynamic elab checks.
2611 -----------------------------
2612 -- Find_Last_Init_In_Block --
2613 -----------------------------
2615 function Find_Last_Init_In_Block (Blk : Node_Id) return Node_Id is
2616 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2617 Stmt : Node_Id;
2619 begin
2620 -- Examine the individual statements of the block in reverse to
2621 -- locate the last initialization call.
2623 if Present (HSS) and then Present (Statements (HSS)) then
2624 Stmt := Last (Statements (HSS));
2625 while Present (Stmt) loop
2627 -- Peek inside nested blocks in case aborts are allowed
2629 if Nkind (Stmt) = N_Block_Statement then
2630 return Find_Last_Init_In_Block (Stmt);
2632 elsif Is_Init_Call (Stmt) then
2633 return Stmt;
2634 end if;
2636 Prev (Stmt);
2637 end loop;
2638 end if;
2640 return Empty;
2641 end Find_Last_Init_In_Block;
2643 ------------------
2644 -- Is_Init_Call --
2645 ------------------
2647 function Is_Init_Call (N : Node_Id) return Boolean is
2648 function Is_Init_Proc_Of
2649 (Subp_Id : Entity_Id;
2650 Typ : Entity_Id) return Boolean;
2651 -- Determine whether subprogram Subp_Id is a valid init proc of
2652 -- type Typ.
2654 ---------------------
2655 -- Is_Init_Proc_Of --
2656 ---------------------
2658 function Is_Init_Proc_Of
2659 (Subp_Id : Entity_Id;
2660 Typ : Entity_Id) return Boolean
2662 Deep_Init : Entity_Id := Empty;
2663 Prim_Init : Entity_Id := Empty;
2664 Type_Init : Entity_Id := Empty;
2666 begin
2667 -- Obtain all possible initialization routines of the
2668 -- related type and try to match the subprogram entity
2669 -- against one of them.
2671 -- Deep_Initialize
2673 Deep_Init := TSS (Typ, TSS_Deep_Initialize);
2675 -- Primitive Initialize
2677 if Is_Controlled (Typ) then
2678 Prim_Init := Find_Optional_Prim_Op (Typ, Name_Initialize);
2680 if Present (Prim_Init) then
2681 Prim_Init := Ultimate_Alias (Prim_Init);
2682 end if;
2683 end if;
2685 -- Type initialization routine
2687 if Has_Non_Null_Base_Init_Proc (Typ) then
2688 Type_Init := Base_Init_Proc (Typ);
2689 end if;
2691 return
2692 (Present (Deep_Init) and then Subp_Id = Deep_Init)
2693 or else
2694 (Present (Prim_Init) and then Subp_Id = Prim_Init)
2695 or else
2696 (Present (Type_Init) and then Subp_Id = Type_Init);
2697 end Is_Init_Proc_Of;
2699 -- Local variables
2701 Call_Id : Entity_Id;
2703 -- Start of processing for Is_Init_Call
2705 begin
2706 if Nkind (N) = N_Procedure_Call_Statement
2707 and then Nkind (Name (N)) = N_Identifier
2708 then
2709 Call_Id := Entity (Name (N));
2711 -- Consider both the type of the object declaration and its
2712 -- related initialization type.
2714 return
2715 Is_Init_Proc_Of (Call_Id, Init_Typ)
2716 or else
2717 Is_Init_Proc_Of (Call_Id, Obj_Typ);
2718 end if;
2720 return False;
2721 end Is_Init_Call;
2723 -----------------------------
2724 -- Next_Suitable_Statement --
2725 -----------------------------
2727 function Next_Suitable_Statement (Stmt : Node_Id) return Node_Id is
2728 Result : Node_Id := Next (Stmt);
2730 begin
2731 -- Skip over access-before-elaboration checks
2733 if Dynamic_Elaboration_Checks
2734 and then Nkind (Result) = N_Raise_Program_Error
2735 then
2736 Result := Next (Result);
2737 end if;
2739 return Result;
2740 end Next_Suitable_Statement;
2742 -- Local variables
2744 Call : Node_Id;
2745 Stmt : Node_Id;
2746 Stmt_2 : Node_Id;
2748 Deep_Init_Found : Boolean := False;
2749 -- A flag set when a call to [Deep_]Initialize has been found
2751 -- Start of processing for Find_Last_Init
2753 begin
2754 Last_Init := Decl;
2755 Body_Insert := Empty;
2757 -- Object renamings and objects associated with controlled
2758 -- function results do not require initialization.
2760 if Has_No_Init then
2761 return;
2762 end if;
2764 Stmt := Next_Suitable_Statement (Decl);
2766 -- For an object with suppressed initialization, we check whether
2767 -- there is in fact no initialization expression. If there is not,
2768 -- then this is an object declaration that has been turned into a
2769 -- different object declaration that calls the build-in-place
2770 -- function in a 'Reference attribute, as in "F(...)'Reference".
2771 -- We search for that later object declaration, so that the
2772 -- Inc_Decl will be inserted after the call. Otherwise, if the
2773 -- call raises an exception, we will finalize the (uninitialized)
2774 -- object, which is wrong.
2776 if No_Initialization (Decl) then
2777 if No (Expression (Last_Init)) then
2778 loop
2779 Last_Init := Next (Last_Init);
2780 exit when No (Last_Init);
2781 exit when Nkind (Last_Init) = N_Object_Declaration
2782 and then Nkind (Expression (Last_Init)) = N_Reference
2783 and then Nkind (Prefix (Expression (Last_Init))) =
2784 N_Function_Call
2785 and then Is_Expanded_Build_In_Place_Call
2786 (Prefix (Expression (Last_Init)));
2787 end loop;
2788 end if;
2790 return;
2792 -- In all other cases the initialization calls follow the related
2793 -- object. The general structure of object initialization built by
2794 -- routine Default_Initialize_Object is as follows:
2796 -- [begin -- aborts allowed
2797 -- Abort_Defer;]
2798 -- Type_Init_Proc (Obj);
2799 -- [begin] -- exceptions allowed
2800 -- Deep_Initialize (Obj);
2801 -- [exception -- exceptions allowed
2802 -- when others =>
2803 -- Deep_Finalize (Obj, Self => False);
2804 -- raise;
2805 -- end;]
2806 -- [at end -- aborts allowed
2807 -- Abort_Undefer;
2808 -- end;]
2810 -- When aborts are allowed, the initialization calls are housed
2811 -- within a block.
2813 elsif Nkind (Stmt) = N_Block_Statement then
2814 Last_Init := Find_Last_Init_In_Block (Stmt);
2815 Body_Insert := Stmt;
2817 -- Otherwise the initialization calls follow the related object
2819 else
2820 Stmt_2 := Next_Suitable_Statement (Stmt);
2822 -- Check for an optional call to Deep_Initialize which may
2823 -- appear within a block depending on whether the object has
2824 -- controlled components.
2826 if Present (Stmt_2) then
2827 if Nkind (Stmt_2) = N_Block_Statement then
2828 Call := Find_Last_Init_In_Block (Stmt_2);
2830 if Present (Call) then
2831 Deep_Init_Found := True;
2832 Last_Init := Call;
2833 Body_Insert := Stmt_2;
2834 end if;
2836 elsif Is_Init_Call (Stmt_2) then
2837 Deep_Init_Found := True;
2838 Last_Init := Stmt_2;
2839 Body_Insert := Last_Init;
2840 end if;
2841 end if;
2843 -- If the object lacks a call to Deep_Initialize, then it must
2844 -- have a call to its related type init proc.
2846 if not Deep_Init_Found and then Is_Init_Call (Stmt) then
2847 Last_Init := Stmt;
2848 Body_Insert := Last_Init;
2849 end if;
2850 end if;
2851 end Find_Last_Init;
2853 -- Local variables
2855 Body_Ins : Node_Id;
2856 Count_Ins : Node_Id;
2857 Fin_Call : Node_Id;
2858 Fin_Stmts : List_Id := No_List;
2859 Inc_Decl : Node_Id;
2860 Label : Node_Id;
2861 Label_Id : Entity_Id;
2862 Obj_Ref : Node_Id;
2864 -- Start of processing for Process_Object_Declaration
2866 begin
2867 -- Handle the object type and the reference to the object
2869 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
2870 Obj_Typ := Base_Type (Etype (Obj_Id));
2872 loop
2873 if Is_Access_Type (Obj_Typ) then
2874 Obj_Typ := Directly_Designated_Type (Obj_Typ);
2875 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
2877 elsif Is_Concurrent_Type (Obj_Typ)
2878 and then Present (Corresponding_Record_Type (Obj_Typ))
2879 then
2880 Obj_Typ := Corresponding_Record_Type (Obj_Typ);
2881 Obj_Ref := Unchecked_Convert_To (Obj_Typ, Obj_Ref);
2883 elsif Is_Private_Type (Obj_Typ)
2884 and then Present (Full_View (Obj_Typ))
2885 then
2886 Obj_Typ := Full_View (Obj_Typ);
2887 Obj_Ref := Unchecked_Convert_To (Obj_Typ, Obj_Ref);
2889 elsif Obj_Typ /= Base_Type (Obj_Typ) then
2890 Obj_Typ := Base_Type (Obj_Typ);
2891 Obj_Ref := Unchecked_Convert_To (Obj_Typ, Obj_Ref);
2893 else
2894 exit;
2895 end if;
2896 end loop;
2898 Set_Etype (Obj_Ref, Obj_Typ);
2900 -- Handle the initialization type of the object declaration
2902 Init_Typ := Obj_Typ;
2903 loop
2904 if Is_Private_Type (Init_Typ)
2905 and then Present (Full_View (Init_Typ))
2906 then
2907 Init_Typ := Full_View (Init_Typ);
2909 elsif Is_Untagged_Derivation (Init_Typ) then
2910 Init_Typ := Root_Type (Init_Typ);
2912 else
2913 exit;
2914 end if;
2915 end loop;
2917 -- Set a new value for the state counter and insert the statement
2918 -- after the object declaration. Generate:
2920 -- Counter := <value>;
2922 Inc_Decl :=
2923 Make_Assignment_Statement (Loc,
2924 Name => New_Occurrence_Of (Counter_Id, Loc),
2925 Expression => Make_Integer_Literal (Loc, Counter_Val));
2927 -- Insert the counter after all initialization has been done. The
2928 -- place of insertion depends on the context.
2930 if Ekind_In (Obj_Id, E_Constant, E_Variable) then
2932 -- The object is initialized by a build-in-place function call.
2933 -- The counter insertion point is after the function call.
2935 if Present (BIP_Initialization_Call (Obj_Id)) then
2936 Count_Ins := BIP_Initialization_Call (Obj_Id);
2937 Body_Ins := Empty;
2939 -- The object is initialized by an aggregate. Insert the counter
2940 -- after the last aggregate assignment.
2942 elsif Present (Last_Aggregate_Assignment (Obj_Id)) then
2943 Count_Ins := Last_Aggregate_Assignment (Obj_Id);
2944 Body_Ins := Empty;
2946 -- In all other cases the counter is inserted after the last call
2947 -- to either [Deep_]Initialize or the type-specific init proc.
2949 else
2950 Find_Last_Init (Count_Ins, Body_Ins);
2951 end if;
2953 -- In all other cases the counter is inserted after the last call to
2954 -- either [Deep_]Initialize or the type-specific init proc.
2956 else
2957 Find_Last_Init (Count_Ins, Body_Ins);
2958 end if;
2960 -- If the Initialize function is null or trivial, the call will have
2961 -- been replaced with a null statement, in which case place counter
2962 -- declaration after object declaration itself.
2964 if No (Count_Ins) then
2965 Count_Ins := Decl;
2966 end if;
2968 Insert_After (Count_Ins, Inc_Decl);
2969 Analyze (Inc_Decl);
2971 -- If the current declaration is the last in the list, the finalizer
2972 -- body needs to be inserted after the set counter statement for the
2973 -- current object declaration. This is complicated by the fact that
2974 -- the set counter statement may appear in abort deferred block. In
2975 -- that case, the proper insertion place is after the block.
2977 if No (Finalizer_Insert_Nod) then
2979 -- Insertion after an abort deferred block
2981 if Present (Body_Ins) then
2982 Finalizer_Insert_Nod := Body_Ins;
2983 else
2984 Finalizer_Insert_Nod := Inc_Decl;
2985 end if;
2986 end if;
2988 -- Create the associated label with this object, generate:
2990 -- L<counter> : label;
2992 Label_Id :=
2993 Make_Identifier (Loc, New_External_Name ('L', Counter_Val));
2994 Set_Entity
2995 (Label_Id, Make_Defining_Identifier (Loc, Chars (Label_Id)));
2996 Label := Make_Label (Loc, Label_Id);
2998 Prepend_To (Finalizer_Decls,
2999 Make_Implicit_Label_Declaration (Loc,
3000 Defining_Identifier => Entity (Label_Id),
3001 Label_Construct => Label));
3003 -- Create the associated jump with this object, generate:
3005 -- when <counter> =>
3006 -- goto L<counter>;
3008 Prepend_To (Jump_Alts,
3009 Make_Case_Statement_Alternative (Loc,
3010 Discrete_Choices => New_List (
3011 Make_Integer_Literal (Loc, Counter_Val)),
3012 Statements => New_List (
3013 Make_Goto_Statement (Loc,
3014 Name => New_Occurrence_Of (Entity (Label_Id), Loc)))));
3016 -- Insert the jump destination, generate:
3018 -- <<L<counter>>>
3020 Append_To (Finalizer_Stmts, Label);
3022 -- Processing for simple protected objects. Such objects require
3023 -- manual finalization of their lock managers.
3025 if Is_Protected then
3026 if Is_Simple_Protected_Type (Obj_Typ) then
3027 Fin_Call := Cleanup_Protected_Object (Decl, Obj_Ref);
3029 if Present (Fin_Call) then
3030 Fin_Stmts := New_List (Fin_Call);
3031 end if;
3033 elsif Has_Simple_Protected_Object (Obj_Typ) then
3034 if Is_Record_Type (Obj_Typ) then
3035 Fin_Stmts := Cleanup_Record (Decl, Obj_Ref, Obj_Typ);
3036 elsif Is_Array_Type (Obj_Typ) then
3037 Fin_Stmts := Cleanup_Array (Decl, Obj_Ref, Obj_Typ);
3038 end if;
3039 end if;
3041 -- Generate:
3042 -- begin
3043 -- System.Tasking.Protected_Objects.Finalize_Protection
3044 -- (Obj._object);
3046 -- exception
3047 -- when others =>
3048 -- null;
3049 -- end;
3051 if Present (Fin_Stmts) and then Exceptions_OK then
3052 Fin_Stmts := New_List (
3053 Make_Block_Statement (Loc,
3054 Handled_Statement_Sequence =>
3055 Make_Handled_Sequence_Of_Statements (Loc,
3056 Statements => Fin_Stmts,
3058 Exception_Handlers => New_List (
3059 Make_Exception_Handler (Loc,
3060 Exception_Choices => New_List (
3061 Make_Others_Choice (Loc)),
3063 Statements => New_List (
3064 Make_Null_Statement (Loc)))))));
3065 end if;
3067 -- Processing for regular controlled objects
3069 else
3070 -- Generate:
3071 -- begin
3072 -- [Deep_]Finalize (Obj);
3074 -- exception
3075 -- when Id : others =>
3076 -- if not Raised then
3077 -- Raised := True;
3078 -- Save_Occurrence (E, Id);
3079 -- end if;
3080 -- end;
3082 Fin_Call :=
3083 Make_Final_Call (
3084 Obj_Ref => Obj_Ref,
3085 Typ => Obj_Typ);
3087 -- Guard against a missing [Deep_]Finalize when the object type
3088 -- was not properly frozen.
3090 if No (Fin_Call) then
3091 Fin_Call := Make_Null_Statement (Loc);
3092 end if;
3094 -- For CodePeer, the exception handlers normally generated here
3095 -- generate complex flowgraphs which result in capacity problems.
3096 -- Omitting these handlers for CodePeer is justified as follows:
3098 -- If a handler is dead, then omitting it is surely ok
3100 -- If a handler is live, then CodePeer should flag the
3101 -- potentially-exception-raising construct that causes it
3102 -- to be live. That is what we are interested in, not what
3103 -- happens after the exception is raised.
3105 if Exceptions_OK and not CodePeer_Mode then
3106 Fin_Stmts := New_List (
3107 Make_Block_Statement (Loc,
3108 Handled_Statement_Sequence =>
3109 Make_Handled_Sequence_Of_Statements (Loc,
3110 Statements => New_List (Fin_Call),
3112 Exception_Handlers => New_List (
3113 Build_Exception_Handler
3114 (Finalizer_Data, For_Package)))));
3116 -- When exception handlers are prohibited, the finalization call
3117 -- appears unprotected. Any exception raised during finalization
3118 -- will bypass the circuitry which ensures the cleanup of all
3119 -- remaining objects.
3121 else
3122 Fin_Stmts := New_List (Fin_Call);
3123 end if;
3125 -- If we are dealing with a return object of a build-in-place
3126 -- function, generate the following cleanup statements:
3128 -- if BIPallocfrom > Secondary_Stack'Pos
3129 -- and then BIPfinalizationmaster /= null
3130 -- then
3131 -- declare
3132 -- type Ptr_Typ is access Obj_Typ;
3133 -- for Ptr_Typ'Storage_Pool use
3134 -- Base_Pool (BIPfinalizationmaster.all).all;
3135 -- begin
3136 -- Free (Ptr_Typ (Temp));
3137 -- end;
3138 -- end if;
3140 -- The generated code effectively detaches the temporary from the
3141 -- caller finalization master and deallocates the object.
3143 if Is_Return_Object (Obj_Id) then
3144 declare
3145 Func_Id : constant Entity_Id := Enclosing_Function (Obj_Id);
3146 begin
3147 if Is_Build_In_Place_Function (Func_Id)
3148 and then Needs_BIP_Finalization_Master (Func_Id)
3149 then
3150 Append_To (Fin_Stmts, Build_BIP_Cleanup_Stmts (Func_Id));
3151 end if;
3152 end;
3153 end if;
3155 if Ekind_In (Obj_Id, E_Constant, E_Variable)
3156 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
3157 then
3158 -- Temporaries created for the purpose of "exporting" a
3159 -- transient object out of an Expression_With_Actions (EWA)
3160 -- need guards. The following illustrates the usage of such
3161 -- temporaries.
3163 -- Access_Typ : access [all] Obj_Typ;
3164 -- Temp : Access_Typ := null;
3165 -- <Counter> := ...;
3167 -- do
3168 -- Ctrl_Trans : [access [all]] Obj_Typ := ...;
3169 -- Temp := Access_Typ (Ctrl_Trans); -- when a pointer
3170 -- <or>
3171 -- Temp := Ctrl_Trans'Unchecked_Access;
3172 -- in ... end;
3174 -- The finalization machinery does not process EWA nodes as
3175 -- this may lead to premature finalization of expressions. Note
3176 -- that Temp is marked as being properly initialized regardless
3177 -- of whether the initialization of Ctrl_Trans succeeded. Since
3178 -- a failed initialization may leave Temp with a value of null,
3179 -- add a guard to handle this case:
3181 -- if Obj /= null then
3182 -- <object finalization statements>
3183 -- end if;
3185 if Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
3186 N_Object_Declaration
3187 then
3188 Fin_Stmts := New_List (
3189 Make_If_Statement (Loc,
3190 Condition =>
3191 Make_Op_Ne (Loc,
3192 Left_Opnd => New_Occurrence_Of (Obj_Id, Loc),
3193 Right_Opnd => Make_Null (Loc)),
3194 Then_Statements => Fin_Stmts));
3196 -- Return objects use a flag to aid in processing their
3197 -- potential finalization when the enclosing function fails
3198 -- to return properly. Generate:
3200 -- if not Flag then
3201 -- <object finalization statements>
3202 -- end if;
3204 else
3205 Fin_Stmts := New_List (
3206 Make_If_Statement (Loc,
3207 Condition =>
3208 Make_Op_Not (Loc,
3209 Right_Opnd =>
3210 New_Occurrence_Of
3211 (Status_Flag_Or_Transient_Decl (Obj_Id), Loc)),
3213 Then_Statements => Fin_Stmts));
3214 end if;
3215 end if;
3216 end if;
3218 Append_List_To (Finalizer_Stmts, Fin_Stmts);
3220 -- Since the declarations are examined in reverse, the state counter
3221 -- must be decremented in order to keep with the true position of
3222 -- objects.
3224 Counter_Val := Counter_Val - 1;
3225 end Process_Object_Declaration;
3227 -------------------------------------
3228 -- Process_Tagged_Type_Declaration --
3229 -------------------------------------
3231 procedure Process_Tagged_Type_Declaration (Decl : Node_Id) is
3232 Typ : constant Entity_Id := Defining_Identifier (Decl);
3233 DT_Ptr : constant Entity_Id :=
3234 Node (First_Elmt (Access_Disp_Table (Typ)));
3235 begin
3236 -- Generate:
3237 -- Ada.Tags.Unregister_Tag (<Typ>P);
3239 Append_To (Tagged_Type_Stmts,
3240 Make_Procedure_Call_Statement (Loc,
3241 Name =>
3242 New_Occurrence_Of (RTE (RE_Unregister_Tag), Loc),
3243 Parameter_Associations => New_List (
3244 New_Occurrence_Of (DT_Ptr, Loc))));
3245 end Process_Tagged_Type_Declaration;
3247 -- Start of processing for Build_Finalizer
3249 begin
3250 Fin_Id := Empty;
3252 -- Do not perform this expansion in SPARK mode because it is not
3253 -- necessary.
3255 if GNATprove_Mode then
3256 return;
3257 end if;
3259 -- Step 1: Extract all lists which may contain controlled objects or
3260 -- library-level tagged types.
3262 if For_Package_Spec then
3263 Decls := Visible_Declarations (Specification (N));
3264 Priv_Decls := Private_Declarations (Specification (N));
3266 -- Retrieve the package spec id
3268 Spec_Id := Defining_Unit_Name (Specification (N));
3270 if Nkind (Spec_Id) = N_Defining_Program_Unit_Name then
3271 Spec_Id := Defining_Identifier (Spec_Id);
3272 end if;
3274 -- Accept statement, block, entry body, package body, protected body,
3275 -- subprogram body or task body.
3277 else
3278 Decls := Declarations (N);
3279 HSS := Handled_Statement_Sequence (N);
3281 if Present (HSS) then
3282 if Present (Statements (HSS)) then
3283 Stmts := Statements (HSS);
3284 end if;
3286 if Present (At_End_Proc (HSS)) then
3287 Prev_At_End := At_End_Proc (HSS);
3288 end if;
3289 end if;
3291 -- Retrieve the package spec id for package bodies
3293 if For_Package_Body then
3294 Spec_Id := Corresponding_Spec (N);
3295 end if;
3296 end if;
3298 -- Do not process nested packages since those are handled by the
3299 -- enclosing scope's finalizer. Do not process non-expanded package
3300 -- instantiations since those will be re-analyzed and re-expanded.
3302 if For_Package
3303 and then
3304 (not Is_Library_Level_Entity (Spec_Id)
3306 -- Nested packages are considered to be library level entities,
3307 -- but do not need to be processed separately. True library level
3308 -- packages have a scope value of 1.
3310 or else Scope_Depth_Value (Spec_Id) /= Uint_1
3311 or else (Is_Generic_Instance (Spec_Id)
3312 and then Package_Instantiation (Spec_Id) /= N))
3313 then
3314 return;
3315 end if;
3317 -- Step 2: Object [pre]processing
3319 if For_Package then
3321 -- Preprocess the visible declarations now in order to obtain the
3322 -- correct number of controlled object by the time the private
3323 -- declarations are processed.
3325 Process_Declarations (Decls, Preprocess => True, Top_Level => True);
3327 -- From all the possible contexts, only package specifications may
3328 -- have private declarations.
3330 if For_Package_Spec then
3331 Process_Declarations
3332 (Priv_Decls, Preprocess => True, Top_Level => True);
3333 end if;
3335 -- The current context may lack controlled objects, but require some
3336 -- other form of completion (task termination for instance). In such
3337 -- cases, the finalizer must be created and carry the additional
3338 -- statements.
3340 if Acts_As_Clean or Has_Ctrl_Objs or Has_Tagged_Types then
3341 Build_Components;
3342 end if;
3344 -- The preprocessing has determined that the context has controlled
3345 -- objects or library-level tagged types.
3347 if Has_Ctrl_Objs or Has_Tagged_Types then
3349 -- Private declarations are processed first in order to preserve
3350 -- possible dependencies between public and private objects.
3352 if For_Package_Spec then
3353 Process_Declarations (Priv_Decls);
3354 end if;
3356 Process_Declarations (Decls);
3357 end if;
3359 -- Non-package case
3361 else
3362 -- Preprocess both declarations and statements
3364 Process_Declarations (Decls, Preprocess => True, Top_Level => True);
3365 Process_Declarations (Stmts, Preprocess => True, Top_Level => True);
3367 -- At this point it is known that N has controlled objects. Ensure
3368 -- that N has a declarative list since the finalizer spec will be
3369 -- attached to it.
3371 if Has_Ctrl_Objs and then No (Decls) then
3372 Set_Declarations (N, New_List);
3373 Decls := Declarations (N);
3374 Spec_Decls := Decls;
3375 end if;
3377 -- The current context may lack controlled objects, but require some
3378 -- other form of completion (task termination for instance). In such
3379 -- cases, the finalizer must be created and carry the additional
3380 -- statements.
3382 if Acts_As_Clean or Has_Ctrl_Objs or Has_Tagged_Types then
3383 Build_Components;
3384 end if;
3386 if Has_Ctrl_Objs or Has_Tagged_Types then
3387 Process_Declarations (Stmts);
3388 Process_Declarations (Decls);
3389 end if;
3390 end if;
3392 -- Step 3: Finalizer creation
3394 if Acts_As_Clean or Has_Ctrl_Objs or Has_Tagged_Types then
3395 Create_Finalizer;
3396 end if;
3397 end Build_Finalizer;
3399 --------------------------
3400 -- Build_Finalizer_Call --
3401 --------------------------
3403 procedure Build_Finalizer_Call (N : Node_Id; Fin_Id : Entity_Id) is
3404 Is_Prot_Body : constant Boolean :=
3405 Nkind (N) = N_Subprogram_Body
3406 and then Is_Protected_Subprogram_Body (N);
3407 -- Determine whether N denotes the protected version of a subprogram
3408 -- which belongs to a protected type.
3410 Loc : constant Source_Ptr := Sloc (N);
3411 HSS : Node_Id;
3413 begin
3414 -- Do not perform this expansion in SPARK mode because we do not create
3415 -- finalizers in the first place.
3417 if GNATprove_Mode then
3418 return;
3419 end if;
3421 -- The At_End handler should have been assimilated by the finalizer
3423 HSS := Handled_Statement_Sequence (N);
3424 pragma Assert (No (At_End_Proc (HSS)));
3426 -- If the construct to be cleaned up is a protected subprogram body, the
3427 -- finalizer call needs to be associated with the block which wraps the
3428 -- unprotected version of the subprogram. The following illustrates this
3429 -- scenario:
3431 -- procedure Prot_SubpP is
3432 -- procedure finalizer is
3433 -- begin
3434 -- Service_Entries (Prot_Obj);
3435 -- Abort_Undefer;
3436 -- end finalizer;
3438 -- begin
3439 -- . . .
3440 -- begin
3441 -- Prot_SubpN (Prot_Obj);
3442 -- at end
3443 -- finalizer;
3444 -- end;
3445 -- end Prot_SubpP;
3447 if Is_Prot_Body then
3448 HSS := Handled_Statement_Sequence (Last (Statements (HSS)));
3450 -- An At_End handler and regular exception handlers cannot coexist in
3451 -- the same statement sequence. Wrap the original statements in a block.
3453 elsif Present (Exception_Handlers (HSS)) then
3454 declare
3455 End_Lab : constant Node_Id := End_Label (HSS);
3456 Block : Node_Id;
3458 begin
3459 Block :=
3460 Make_Block_Statement (Loc, Handled_Statement_Sequence => HSS);
3462 Set_Handled_Statement_Sequence (N,
3463 Make_Handled_Sequence_Of_Statements (Loc, New_List (Block)));
3465 HSS := Handled_Statement_Sequence (N);
3466 Set_End_Label (HSS, End_Lab);
3467 end;
3468 end if;
3470 Set_At_End_Proc (HSS, New_Occurrence_Of (Fin_Id, Loc));
3472 Analyze (At_End_Proc (HSS));
3473 Expand_At_End_Handler (HSS, Empty);
3474 end Build_Finalizer_Call;
3476 ---------------------
3477 -- Build_Late_Proc --
3478 ---------------------
3480 procedure Build_Late_Proc (Typ : Entity_Id; Nam : Name_Id) is
3481 begin
3482 for Final_Prim in Name_Of'Range loop
3483 if Name_Of (Final_Prim) = Nam then
3484 Set_TSS (Typ,
3485 Make_Deep_Proc
3486 (Prim => Final_Prim,
3487 Typ => Typ,
3488 Stmts => Make_Deep_Record_Body (Final_Prim, Typ)));
3489 end if;
3490 end loop;
3491 end Build_Late_Proc;
3493 -------------------------------
3494 -- Build_Object_Declarations --
3495 -------------------------------
3497 procedure Build_Object_Declarations
3498 (Data : out Finalization_Exception_Data;
3499 Decls : List_Id;
3500 Loc : Source_Ptr;
3501 For_Package : Boolean := False)
3503 Decl : Node_Id;
3505 Dummy : Entity_Id;
3506 -- This variable captures an unused dummy internal entity, see the
3507 -- comment associated with its use.
3509 begin
3510 pragma Assert (Decls /= No_List);
3512 -- Always set the proper location as it may be needed even when
3513 -- exception propagation is forbidden.
3515 Data.Loc := Loc;
3517 if Restriction_Active (No_Exception_Propagation) then
3518 Data.Abort_Id := Empty;
3519 Data.E_Id := Empty;
3520 Data.Raised_Id := Empty;
3521 return;
3522 end if;
3524 Data.Raised_Id := Make_Temporary (Loc, 'R');
3526 -- In certain scenarios, finalization can be triggered by an abort. If
3527 -- the finalization itself fails and raises an exception, the resulting
3528 -- Program_Error must be supressed and replaced by an abort signal. In
3529 -- order to detect this scenario, save the state of entry into the
3530 -- finalization code.
3532 -- This is not needed for library-level finalizers as they are called by
3533 -- the environment task and cannot be aborted.
3535 if not For_Package then
3536 if Abort_Allowed then
3537 Data.Abort_Id := Make_Temporary (Loc, 'A');
3539 -- Generate:
3540 -- Abort_Id : constant Boolean := <A_Expr>;
3542 Append_To (Decls,
3543 Make_Object_Declaration (Loc,
3544 Defining_Identifier => Data.Abort_Id,
3545 Constant_Present => True,
3546 Object_Definition =>
3547 New_Occurrence_Of (Standard_Boolean, Loc),
3548 Expression =>
3549 New_Occurrence_Of (RTE (RE_Triggered_By_Abort), Loc)));
3551 -- Abort is not required
3553 else
3554 -- Generate a dummy entity to ensure that the internal symbols are
3555 -- in sync when a unit is compiled with and without aborts.
3557 Dummy := Make_Temporary (Loc, 'A');
3558 Data.Abort_Id := Empty;
3559 end if;
3561 -- Library-level finalizers
3563 else
3564 Data.Abort_Id := Empty;
3565 end if;
3567 if Exception_Extra_Info then
3568 Data.E_Id := Make_Temporary (Loc, 'E');
3570 -- Generate:
3571 -- E_Id : Exception_Occurrence;
3573 Decl :=
3574 Make_Object_Declaration (Loc,
3575 Defining_Identifier => Data.E_Id,
3576 Object_Definition =>
3577 New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc));
3578 Set_No_Initialization (Decl);
3580 Append_To (Decls, Decl);
3582 else
3583 Data.E_Id := Empty;
3584 end if;
3586 -- Generate:
3587 -- Raised_Id : Boolean := False;
3589 Append_To (Decls,
3590 Make_Object_Declaration (Loc,
3591 Defining_Identifier => Data.Raised_Id,
3592 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
3593 Expression => New_Occurrence_Of (Standard_False, Loc)));
3594 end Build_Object_Declarations;
3596 ---------------------------
3597 -- Build_Raise_Statement --
3598 ---------------------------
3600 function Build_Raise_Statement
3601 (Data : Finalization_Exception_Data) return Node_Id
3603 Stmt : Node_Id;
3604 Expr : Node_Id;
3606 begin
3607 -- Standard run-time use the specialized routine
3608 -- Raise_From_Controlled_Operation.
3610 if Exception_Extra_Info
3611 and then RTE_Available (RE_Raise_From_Controlled_Operation)
3612 then
3613 Stmt :=
3614 Make_Procedure_Call_Statement (Data.Loc,
3615 Name =>
3616 New_Occurrence_Of
3617 (RTE (RE_Raise_From_Controlled_Operation), Data.Loc),
3618 Parameter_Associations =>
3619 New_List (New_Occurrence_Of (Data.E_Id, Data.Loc)));
3621 -- Restricted run-time: exception messages are not supported and hence
3622 -- Raise_From_Controlled_Operation is not supported. Raise Program_Error
3623 -- instead.
3625 else
3626 Stmt :=
3627 Make_Raise_Program_Error (Data.Loc,
3628 Reason => PE_Finalize_Raised_Exception);
3629 end if;
3631 -- Generate:
3633 -- Raised_Id and then not Abort_Id
3634 -- <or>
3635 -- Raised_Id
3637 Expr := New_Occurrence_Of (Data.Raised_Id, Data.Loc);
3639 if Present (Data.Abort_Id) then
3640 Expr := Make_And_Then (Data.Loc,
3641 Left_Opnd => Expr,
3642 Right_Opnd =>
3643 Make_Op_Not (Data.Loc,
3644 Right_Opnd => New_Occurrence_Of (Data.Abort_Id, Data.Loc)));
3645 end if;
3647 -- Generate:
3649 -- if Raised_Id and then not Abort_Id then
3650 -- Raise_From_Controlled_Operation (E_Id);
3651 -- <or>
3652 -- raise Program_Error; -- restricted runtime
3653 -- end if;
3655 return
3656 Make_If_Statement (Data.Loc,
3657 Condition => Expr,
3658 Then_Statements => New_List (Stmt));
3659 end Build_Raise_Statement;
3661 -----------------------------
3662 -- Build_Record_Deep_Procs --
3663 -----------------------------
3665 procedure Build_Record_Deep_Procs (Typ : Entity_Id) is
3666 begin
3667 Set_TSS (Typ,
3668 Make_Deep_Proc
3669 (Prim => Initialize_Case,
3670 Typ => Typ,
3671 Stmts => Make_Deep_Record_Body (Initialize_Case, Typ)));
3673 if not Is_Limited_View (Typ) then
3674 Set_TSS (Typ,
3675 Make_Deep_Proc
3676 (Prim => Adjust_Case,
3677 Typ => Typ,
3678 Stmts => Make_Deep_Record_Body (Adjust_Case, Typ)));
3679 end if;
3681 -- Do not generate Deep_Finalize and Finalize_Address if finalization is
3682 -- suppressed since these routine will not be used.
3684 if not Restriction_Active (No_Finalization) then
3685 Set_TSS (Typ,
3686 Make_Deep_Proc
3687 (Prim => Finalize_Case,
3688 Typ => Typ,
3689 Stmts => Make_Deep_Record_Body (Finalize_Case, Typ)));
3691 -- Create TSS primitive Finalize_Address (unless CodePeer_Mode)
3693 if not CodePeer_Mode then
3694 Set_TSS (Typ,
3695 Make_Deep_Proc
3696 (Prim => Address_Case,
3697 Typ => Typ,
3698 Stmts => Make_Deep_Record_Body (Address_Case, Typ)));
3699 end if;
3700 end if;
3701 end Build_Record_Deep_Procs;
3703 -------------------
3704 -- Cleanup_Array --
3705 -------------------
3707 function Cleanup_Array
3708 (N : Node_Id;
3709 Obj : Node_Id;
3710 Typ : Entity_Id) return List_Id
3712 Loc : constant Source_Ptr := Sloc (N);
3713 Index_List : constant List_Id := New_List;
3715 function Free_Component return List_Id;
3716 -- Generate the code to finalize the task or protected subcomponents
3717 -- of a single component of the array.
3719 function Free_One_Dimension (Dim : Int) return List_Id;
3720 -- Generate a loop over one dimension of the array
3722 --------------------
3723 -- Free_Component --
3724 --------------------
3726 function Free_Component return List_Id is
3727 Stmts : List_Id := New_List;
3728 Tsk : Node_Id;
3729 C_Typ : constant Entity_Id := Component_Type (Typ);
3731 begin
3732 -- Component type is known to contain tasks or protected objects
3734 Tsk :=
3735 Make_Indexed_Component (Loc,
3736 Prefix => Duplicate_Subexpr_No_Checks (Obj),
3737 Expressions => Index_List);
3739 Set_Etype (Tsk, C_Typ);
3741 if Is_Task_Type (C_Typ) then
3742 Append_To (Stmts, Cleanup_Task (N, Tsk));
3744 elsif Is_Simple_Protected_Type (C_Typ) then
3745 Append_To (Stmts, Cleanup_Protected_Object (N, Tsk));
3747 elsif Is_Record_Type (C_Typ) then
3748 Stmts := Cleanup_Record (N, Tsk, C_Typ);
3750 elsif Is_Array_Type (C_Typ) then
3751 Stmts := Cleanup_Array (N, Tsk, C_Typ);
3752 end if;
3754 return Stmts;
3755 end Free_Component;
3757 ------------------------
3758 -- Free_One_Dimension --
3759 ------------------------
3761 function Free_One_Dimension (Dim : Int) return List_Id is
3762 Index : Entity_Id;
3764 begin
3765 if Dim > Number_Dimensions (Typ) then
3766 return Free_Component;
3768 -- Here we generate the required loop
3770 else
3771 Index := Make_Temporary (Loc, 'J');
3772 Append (New_Occurrence_Of (Index, Loc), Index_List);
3774 return New_List (
3775 Make_Implicit_Loop_Statement (N,
3776 Identifier => Empty,
3777 Iteration_Scheme =>
3778 Make_Iteration_Scheme (Loc,
3779 Loop_Parameter_Specification =>
3780 Make_Loop_Parameter_Specification (Loc,
3781 Defining_Identifier => Index,
3782 Discrete_Subtype_Definition =>
3783 Make_Attribute_Reference (Loc,
3784 Prefix => Duplicate_Subexpr (Obj),
3785 Attribute_Name => Name_Range,
3786 Expressions => New_List (
3787 Make_Integer_Literal (Loc, Dim))))),
3788 Statements => Free_One_Dimension (Dim + 1)));
3789 end if;
3790 end Free_One_Dimension;
3792 -- Start of processing for Cleanup_Array
3794 begin
3795 return Free_One_Dimension (1);
3796 end Cleanup_Array;
3798 --------------------
3799 -- Cleanup_Record --
3800 --------------------
3802 function Cleanup_Record
3803 (N : Node_Id;
3804 Obj : Node_Id;
3805 Typ : Entity_Id) return List_Id
3807 Loc : constant Source_Ptr := Sloc (N);
3808 Tsk : Node_Id;
3809 Comp : Entity_Id;
3810 Stmts : constant List_Id := New_List;
3811 U_Typ : constant Entity_Id := Underlying_Type (Typ);
3813 begin
3814 if Has_Discriminants (U_Typ)
3815 and then Nkind (Parent (U_Typ)) = N_Full_Type_Declaration
3816 and then Nkind (Type_Definition (Parent (U_Typ))) = N_Record_Definition
3817 and then
3818 Present
3819 (Variant_Part (Component_List (Type_Definition (Parent (U_Typ)))))
3820 then
3821 -- For now, do not attempt to free a component that may appear in a
3822 -- variant, and instead issue a warning. Doing this "properly" would
3823 -- require building a case statement and would be quite a mess. Note
3824 -- that the RM only requires that free "work" for the case of a task
3825 -- access value, so already we go way beyond this in that we deal
3826 -- with the array case and non-discriminated record cases.
3828 Error_Msg_N
3829 ("task/protected object in variant record will not be freed??", N);
3830 return New_List (Make_Null_Statement (Loc));
3831 end if;
3833 Comp := First_Component (Typ);
3834 while Present (Comp) loop
3835 if Has_Task (Etype (Comp))
3836 or else Has_Simple_Protected_Object (Etype (Comp))
3837 then
3838 Tsk :=
3839 Make_Selected_Component (Loc,
3840 Prefix => Duplicate_Subexpr_No_Checks (Obj),
3841 Selector_Name => New_Occurrence_Of (Comp, Loc));
3842 Set_Etype (Tsk, Etype (Comp));
3844 if Is_Task_Type (Etype (Comp)) then
3845 Append_To (Stmts, Cleanup_Task (N, Tsk));
3847 elsif Is_Simple_Protected_Type (Etype (Comp)) then
3848 Append_To (Stmts, Cleanup_Protected_Object (N, Tsk));
3850 elsif Is_Record_Type (Etype (Comp)) then
3852 -- Recurse, by generating the prefix of the argument to
3853 -- the eventual cleanup call.
3855 Append_List_To (Stmts, Cleanup_Record (N, Tsk, Etype (Comp)));
3857 elsif Is_Array_Type (Etype (Comp)) then
3858 Append_List_To (Stmts, Cleanup_Array (N, Tsk, Etype (Comp)));
3859 end if;
3860 end if;
3862 Next_Component (Comp);
3863 end loop;
3865 return Stmts;
3866 end Cleanup_Record;
3868 ------------------------------
3869 -- Cleanup_Protected_Object --
3870 ------------------------------
3872 function Cleanup_Protected_Object
3873 (N : Node_Id;
3874 Ref : Node_Id) return Node_Id
3876 Loc : constant Source_Ptr := Sloc (N);
3878 begin
3879 -- For restricted run-time libraries (Ravenscar), tasks are
3880 -- non-terminating, and protected objects can only appear at library
3881 -- level, so we do not want finalization of protected objects.
3883 if Restricted_Profile then
3884 return Empty;
3886 else
3887 return
3888 Make_Procedure_Call_Statement (Loc,
3889 Name =>
3890 New_Occurrence_Of (RTE (RE_Finalize_Protection), Loc),
3891 Parameter_Associations => New_List (Concurrent_Ref (Ref)));
3892 end if;
3893 end Cleanup_Protected_Object;
3895 ------------------
3896 -- Cleanup_Task --
3897 ------------------
3899 function Cleanup_Task
3900 (N : Node_Id;
3901 Ref : Node_Id) return Node_Id
3903 Loc : constant Source_Ptr := Sloc (N);
3905 begin
3906 -- For restricted run-time libraries (Ravenscar), tasks are
3907 -- non-terminating and they can only appear at library level, so we do
3908 -- not want finalization of task objects.
3910 if Restricted_Profile then
3911 return Empty;
3913 else
3914 return
3915 Make_Procedure_Call_Statement (Loc,
3916 Name =>
3917 New_Occurrence_Of (RTE (RE_Free_Task), Loc),
3918 Parameter_Associations => New_List (Concurrent_Ref (Ref)));
3919 end if;
3920 end Cleanup_Task;
3922 ------------------------------
3923 -- Check_Visibly_Controlled --
3924 ------------------------------
3926 procedure Check_Visibly_Controlled
3927 (Prim : Final_Primitives;
3928 Typ : Entity_Id;
3929 E : in out Entity_Id;
3930 Cref : in out Node_Id)
3932 Parent_Type : Entity_Id;
3933 Op : Entity_Id;
3935 begin
3936 if Is_Derived_Type (Typ)
3937 and then Comes_From_Source (E)
3938 and then not Present (Overridden_Operation (E))
3939 then
3940 -- We know that the explicit operation on the type does not override
3941 -- the inherited operation of the parent, and that the derivation
3942 -- is from a private type that is not visibly controlled.
3944 Parent_Type := Etype (Typ);
3945 Op := Find_Optional_Prim_Op (Parent_Type, Name_Of (Prim));
3947 if Present (Op) then
3948 E := Op;
3950 -- Wrap the object to be initialized into the proper
3951 -- unchecked conversion, to be compatible with the operation
3952 -- to be called.
3954 if Nkind (Cref) = N_Unchecked_Type_Conversion then
3955 Cref := Unchecked_Convert_To (Parent_Type, Expression (Cref));
3956 else
3957 Cref := Unchecked_Convert_To (Parent_Type, Cref);
3958 end if;
3959 end if;
3960 end if;
3961 end Check_Visibly_Controlled;
3963 ------------------
3964 -- Convert_View --
3965 ------------------
3967 function Convert_View
3968 (Proc : Entity_Id;
3969 Arg : Node_Id;
3970 Ind : Pos := 1) return Node_Id
3972 Fent : Entity_Id := First_Entity (Proc);
3973 Ftyp : Entity_Id;
3974 Atyp : Entity_Id;
3976 begin
3977 for J in 2 .. Ind loop
3978 Next_Entity (Fent);
3979 end loop;
3981 Ftyp := Etype (Fent);
3983 if Nkind_In (Arg, N_Type_Conversion, N_Unchecked_Type_Conversion) then
3984 Atyp := Entity (Subtype_Mark (Arg));
3985 else
3986 Atyp := Etype (Arg);
3987 end if;
3989 if Is_Abstract_Subprogram (Proc) and then Is_Tagged_Type (Ftyp) then
3990 return Unchecked_Convert_To (Class_Wide_Type (Ftyp), Arg);
3992 elsif Ftyp /= Atyp
3993 and then Present (Atyp)
3994 and then (Is_Private_Type (Ftyp) or else Is_Private_Type (Atyp))
3995 and then Base_Type (Underlying_Type (Atyp)) =
3996 Base_Type (Underlying_Type (Ftyp))
3997 then
3998 return Unchecked_Convert_To (Ftyp, Arg);
4000 -- If the argument is already a conversion, as generated by
4001 -- Make_Init_Call, set the target type to the type of the formal
4002 -- directly, to avoid spurious typing problems.
4004 elsif Nkind_In (Arg, N_Unchecked_Type_Conversion, N_Type_Conversion)
4005 and then not Is_Class_Wide_Type (Atyp)
4006 then
4007 Set_Subtype_Mark (Arg, New_Occurrence_Of (Ftyp, Sloc (Arg)));
4008 Set_Etype (Arg, Ftyp);
4009 return Arg;
4011 -- Otherwise, introduce a conversion when the designated object
4012 -- has a type derived from the formal of the controlled routine.
4014 elsif Is_Private_Type (Ftyp)
4015 and then Present (Atyp)
4016 and then Is_Derived_Type (Underlying_Type (Base_Type (Atyp)))
4017 then
4018 return Unchecked_Convert_To (Ftyp, Arg);
4020 else
4021 return Arg;
4022 end if;
4023 end Convert_View;
4025 -------------------------------
4026 -- CW_Or_Has_Controlled_Part --
4027 -------------------------------
4029 function CW_Or_Has_Controlled_Part (T : Entity_Id) return Boolean is
4030 begin
4031 return Is_Class_Wide_Type (T) or else Needs_Finalization (T);
4032 end CW_Or_Has_Controlled_Part;
4034 ------------------------
4035 -- Enclosing_Function --
4036 ------------------------
4038 function Enclosing_Function (E : Entity_Id) return Entity_Id is
4039 Func_Id : Entity_Id;
4041 begin
4042 Func_Id := E;
4043 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
4044 if Ekind (Func_Id) = E_Function then
4045 return Func_Id;
4046 end if;
4048 Func_Id := Scope (Func_Id);
4049 end loop;
4051 return Empty;
4052 end Enclosing_Function;
4054 -------------------------------
4055 -- Establish_Transient_Scope --
4056 -------------------------------
4058 -- This procedure is called each time a transient block has to be inserted
4059 -- that is to say for each call to a function with unconstrained or tagged
4060 -- result. It creates a new scope on the stack scope in order to enclose
4061 -- all transient variables generated.
4063 procedure Establish_Transient_Scope (N : Node_Id; Sec_Stack : Boolean) is
4064 Loc : constant Source_Ptr := Sloc (N);
4065 Iter_Loop : Entity_Id;
4066 Scop_Id : Entity_Id;
4067 Scop_Rec : Scope_Stack_Entry;
4068 Wrap_Node : Node_Id;
4070 begin
4071 -- Do not create a new transient scope if there is an existing transient
4072 -- scope on the stack.
4074 for Index in reverse Scope_Stack.First .. Scope_Stack.Last loop
4075 Scop_Rec := Scope_Stack.Table (Index);
4076 Scop_Id := Scop_Rec.Entity;
4078 -- The current scope is transient. If the scope being established
4079 -- needs to manage the secondary stack, then the existing scope
4080 -- overtakes that function.
4082 if Scop_Rec.Is_Transient then
4083 if Sec_Stack then
4084 Set_Uses_Sec_Stack (Scop_Id);
4085 end if;
4087 return;
4089 -- Prevent the search from going too far because transient blocks
4090 -- are bounded by packages and subprogram scopes. Reaching Standard
4091 -- should be impossible without hitting one of the other cases first
4092 -- unless Standard was manually pushed.
4094 elsif Scop_Id = Standard_Standard
4095 or else Ekind_In (Scop_Id, E_Entry,
4096 E_Entry_Family,
4097 E_Function,
4098 E_Package,
4099 E_Procedure,
4100 E_Subprogram_Body)
4101 then
4102 exit;
4103 end if;
4104 end loop;
4106 Wrap_Node := Find_Node_To_Be_Wrapped (N);
4108 -- The context does not contain a node that requires a transient scope,
4109 -- nothing to do.
4111 if No (Wrap_Node) then
4112 null;
4114 -- If the node to wrap is an iteration_scheme, the expression is one of
4115 -- the bounds, and the expansion will make an explicit declaration for
4116 -- it (see Analyze_Iteration_Scheme, sem_ch5.adb), so do not apply any
4117 -- transformations here. Same for an Ada 2012 iterator specification,
4118 -- where a block is created for the expression that build the container.
4120 elsif Nkind_In (Wrap_Node, N_Iteration_Scheme,
4121 N_Iterator_Specification)
4122 then
4123 null;
4125 -- In formal verification mode, if the node to wrap is a pragma check,
4126 -- this node and enclosed expression are not expanded, so do not apply
4127 -- any transformations here.
4129 elsif GNATprove_Mode
4130 and then Nkind (Wrap_Node) = N_Pragma
4131 and then Get_Pragma_Id (Wrap_Node) = Pragma_Check
4132 then
4133 null;
4135 -- Create a block entity to act as a transient scope. Note that when the
4136 -- node to be wrapped is an expression or a statement, a real physical
4137 -- block is constructed (see routines Wrap_Transient_Expression and
4138 -- Wrap_Transient_Statement) and inserted into the tree.
4140 else
4141 Push_Scope (New_Internal_Entity (E_Block, Current_Scope, Loc, 'B'));
4142 Set_Scope_Is_Transient;
4144 -- The transient scope must also take care of the secondary stack
4145 -- management.
4147 if Sec_Stack then
4148 Set_Uses_Sec_Stack (Current_Scope);
4149 Check_Restriction (No_Secondary_Stack, N);
4151 -- The expansion of iterator loops generates references to objects
4152 -- in order to extract elements from a container:
4154 -- Ref : Reference_Type_Ptr := Reference (Container, Cursor);
4155 -- Obj : <object type> renames Ref.all.Element.all;
4157 -- These references are controlled and returned on the secondary
4158 -- stack. A new reference is created at each iteration of the loop
4159 -- and as a result it must be finalized and the space occupied by
4160 -- it on the secondary stack reclaimed at the end of the current
4161 -- iteration.
4163 -- When the context that requires a transient scope is a call to
4164 -- routine Reference, the node to be wrapped is the source object:
4166 -- for Obj of Container loop
4168 -- Routine Wrap_Transient_Declaration however does not generate a
4169 -- physical block as wrapping a declaration will kill it too ealy.
4170 -- To handle this peculiar case, mark the related iterator loop as
4171 -- requiring the secondary stack. This signals the finalization
4172 -- machinery to manage the secondary stack (see routine
4173 -- Process_Statements_For_Controlled_Objects).
4175 Iter_Loop := Find_Enclosing_Iterator_Loop (Current_Scope);
4177 if Present (Iter_Loop) then
4178 Set_Uses_Sec_Stack (Iter_Loop);
4179 end if;
4180 end if;
4182 Set_Etype (Current_Scope, Standard_Void_Type);
4183 Set_Node_To_Be_Wrapped (Wrap_Node);
4185 if Debug_Flag_W then
4186 Write_Str (" <Transient>");
4187 Write_Eol;
4188 end if;
4189 end if;
4190 end Establish_Transient_Scope;
4192 ----------------------------
4193 -- Expand_Cleanup_Actions --
4194 ----------------------------
4196 procedure Expand_Cleanup_Actions (N : Node_Id) is
4197 Scop : constant Entity_Id := Current_Scope;
4199 Is_Asynchronous_Call : constant Boolean :=
4200 Nkind (N) = N_Block_Statement
4201 and then Is_Asynchronous_Call_Block (N);
4202 Is_Master : constant Boolean :=
4203 Nkind (N) /= N_Entry_Body
4204 and then Is_Task_Master (N);
4205 Is_Protected_Subp_Body : constant Boolean :=
4206 Nkind (N) = N_Subprogram_Body
4207 and then Is_Protected_Subprogram_Body (N);
4208 Is_Task_Allocation : constant Boolean :=
4209 Nkind (N) = N_Block_Statement
4210 and then Is_Task_Allocation_Block (N);
4211 Is_Task_Body : constant Boolean :=
4212 Nkind (Original_Node (N)) = N_Task_Body;
4213 Needs_Sec_Stack_Mark : constant Boolean :=
4214 Uses_Sec_Stack (Scop)
4215 and then
4216 not Sec_Stack_Needed_For_Return (Scop);
4217 Needs_Custom_Cleanup : constant Boolean :=
4218 Nkind (N) = N_Block_Statement
4219 and then Present (Cleanup_Actions (N));
4221 Actions_Required : constant Boolean :=
4222 Requires_Cleanup_Actions (N, True)
4223 or else Is_Asynchronous_Call
4224 or else Is_Master
4225 or else Is_Protected_Subp_Body
4226 or else Is_Task_Allocation
4227 or else Is_Task_Body
4228 or else Needs_Sec_Stack_Mark
4229 or else Needs_Custom_Cleanup;
4231 HSS : Node_Id := Handled_Statement_Sequence (N);
4232 Loc : Source_Ptr;
4233 Cln : List_Id;
4235 procedure Wrap_HSS_In_Block;
4236 -- Move HSS inside a new block along with the original exception
4237 -- handlers. Make the newly generated block the sole statement of HSS.
4239 -----------------------
4240 -- Wrap_HSS_In_Block --
4241 -----------------------
4243 procedure Wrap_HSS_In_Block is
4244 Block : Node_Id;
4245 Block_Id : Entity_Id;
4246 End_Lab : Node_Id;
4248 begin
4249 -- Preserve end label to provide proper cross-reference information
4251 End_Lab := End_Label (HSS);
4252 Block :=
4253 Make_Block_Statement (Loc, Handled_Statement_Sequence => HSS);
4255 Block_Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
4256 Set_Identifier (Block, New_Occurrence_Of (Block_Id, Loc));
4257 Set_Etype (Block_Id, Standard_Void_Type);
4258 Set_Block_Node (Block_Id, Identifier (Block));
4260 -- Signal the finalization machinery that this particular block
4261 -- contains the original context.
4263 Set_Is_Finalization_Wrapper (Block);
4265 Set_Handled_Statement_Sequence (N,
4266 Make_Handled_Sequence_Of_Statements (Loc, New_List (Block)));
4267 HSS := Handled_Statement_Sequence (N);
4269 Set_First_Real_Statement (HSS, Block);
4270 Set_End_Label (HSS, End_Lab);
4272 -- Comment needed here, see RH for 1.306 ???
4274 if Nkind (N) = N_Subprogram_Body then
4275 Set_Has_Nested_Block_With_Handler (Scop);
4276 end if;
4277 end Wrap_HSS_In_Block;
4279 -- Start of processing for Expand_Cleanup_Actions
4281 begin
4282 -- The current construct does not need any form of servicing
4284 if not Actions_Required then
4285 return;
4287 -- If the current node is a rewritten task body and the descriptors have
4288 -- not been delayed (due to some nested instantiations), do not generate
4289 -- redundant cleanup actions.
4291 elsif Is_Task_Body
4292 and then Nkind (N) = N_Subprogram_Body
4293 and then not Delay_Subprogram_Descriptors (Corresponding_Spec (N))
4294 then
4295 return;
4296 end if;
4298 if Needs_Custom_Cleanup then
4299 Cln := Cleanup_Actions (N);
4300 else
4301 Cln := No_List;
4302 end if;
4304 declare
4305 Decls : List_Id := Declarations (N);
4306 Fin_Id : Entity_Id;
4307 Mark : Entity_Id := Empty;
4308 New_Decls : List_Id;
4309 Old_Poll : Boolean;
4311 begin
4312 -- If we are generating expanded code for debugging purposes, use the
4313 -- Sloc of the point of insertion for the cleanup code. The Sloc will
4314 -- be updated subsequently to reference the proper line in .dg files.
4315 -- If we are not debugging generated code, use No_Location instead,
4316 -- so that no debug information is generated for the cleanup code.
4317 -- This makes the behavior of the NEXT command in GDB monotonic, and
4318 -- makes the placement of breakpoints more accurate.
4320 if Debug_Generated_Code then
4321 Loc := Sloc (Scop);
4322 else
4323 Loc := No_Location;
4324 end if;
4326 -- Set polling off. The finalization and cleanup code is executed
4327 -- with aborts deferred.
4329 Old_Poll := Polling_Required;
4330 Polling_Required := False;
4332 -- A task activation call has already been built for a task
4333 -- allocation block.
4335 if not Is_Task_Allocation then
4336 Build_Task_Activation_Call (N);
4337 end if;
4339 if Is_Master then
4340 Establish_Task_Master (N);
4341 end if;
4343 New_Decls := New_List;
4345 -- If secondary stack is in use, generate:
4347 -- Mnn : constant Mark_Id := SS_Mark;
4349 if Needs_Sec_Stack_Mark then
4350 Mark := Make_Temporary (Loc, 'M');
4352 Append_To (New_Decls, Build_SS_Mark_Call (Loc, Mark));
4353 Set_Uses_Sec_Stack (Scop, False);
4354 end if;
4356 -- If exception handlers are present, wrap the sequence of statements
4357 -- in a block since it is not possible to have exception handlers and
4358 -- an At_End handler in the same construct.
4360 if Present (Exception_Handlers (HSS)) then
4361 Wrap_HSS_In_Block;
4363 -- Ensure that the First_Real_Statement field is set
4365 elsif No (First_Real_Statement (HSS)) then
4366 Set_First_Real_Statement (HSS, First (Statements (HSS)));
4367 end if;
4369 -- Do not move the Activation_Chain declaration in the context of
4370 -- task allocation blocks. Task allocation blocks use _chain in their
4371 -- cleanup handlers and gigi complains if it is declared in the
4372 -- sequence of statements of the scope that declares the handler.
4374 if Is_Task_Allocation then
4375 declare
4376 Chain : constant Entity_Id := Activation_Chain_Entity (N);
4377 Decl : Node_Id;
4379 begin
4380 Decl := First (Decls);
4381 while Nkind (Decl) /= N_Object_Declaration
4382 or else Defining_Identifier (Decl) /= Chain
4383 loop
4384 Next (Decl);
4386 -- A task allocation block should always include a _chain
4387 -- declaration.
4389 pragma Assert (Present (Decl));
4390 end loop;
4392 Remove (Decl);
4393 Prepend_To (New_Decls, Decl);
4394 end;
4395 end if;
4397 -- Ensure the presence of a declaration list in order to successfully
4398 -- append all original statements to it.
4400 if No (Decls) then
4401 Set_Declarations (N, New_List);
4402 Decls := Declarations (N);
4403 end if;
4405 -- Move the declarations into the sequence of statements in order to
4406 -- have them protected by the At_End handler. It may seem weird to
4407 -- put declarations in the sequence of statement but in fact nothing
4408 -- forbids that at the tree level.
4410 Append_List_To (Decls, Statements (HSS));
4411 Set_Statements (HSS, Decls);
4413 -- Reset the Sloc of the handled statement sequence to properly
4414 -- reflect the new initial "statement" in the sequence.
4416 Set_Sloc (HSS, Sloc (First (Decls)));
4418 -- The declarations of finalizer spec and auxiliary variables replace
4419 -- the old declarations that have been moved inward.
4421 Set_Declarations (N, New_Decls);
4422 Analyze_Declarations (New_Decls);
4424 -- Generate finalization calls for all controlled objects appearing
4425 -- in the statements of N. Add context specific cleanup for various
4426 -- constructs.
4428 Build_Finalizer
4429 (N => N,
4430 Clean_Stmts => Build_Cleanup_Statements (N, Cln),
4431 Mark_Id => Mark,
4432 Top_Decls => New_Decls,
4433 Defer_Abort => Nkind (Original_Node (N)) = N_Task_Body
4434 or else Is_Master,
4435 Fin_Id => Fin_Id);
4437 if Present (Fin_Id) then
4438 Build_Finalizer_Call (N, Fin_Id);
4439 end if;
4441 -- Restore saved polling mode
4443 Polling_Required := Old_Poll;
4444 end;
4445 end Expand_Cleanup_Actions;
4447 ---------------------------
4448 -- Expand_N_Package_Body --
4449 ---------------------------
4451 -- Add call to Activate_Tasks if body is an activator (actual processing
4452 -- is in chapter 9).
4454 -- Generate subprogram descriptor for elaboration routine
4456 -- Encode entity names in package body
4458 procedure Expand_N_Package_Body (N : Node_Id) is
4459 Spec_Id : constant Entity_Id := Corresponding_Spec (N);
4460 Fin_Id : Entity_Id;
4462 begin
4463 -- This is done only for non-generic packages
4465 if Ekind (Spec_Id) = E_Package then
4466 Push_Scope (Corresponding_Spec (N));
4468 -- Build dispatch tables of library level tagged types
4470 if Tagged_Type_Expansion
4471 and then Is_Library_Level_Entity (Spec_Id)
4472 then
4473 Build_Static_Dispatch_Tables (N);
4474 end if;
4476 Build_Task_Activation_Call (N);
4478 -- When the package is subject to pragma Initial_Condition, the
4479 -- assertion expression must be verified at the end of the body
4480 -- statements.
4482 if Present (Get_Pragma (Spec_Id, Pragma_Initial_Condition)) then
4483 Expand_Pragma_Initial_Condition (N);
4484 end if;
4486 Pop_Scope;
4487 end if;
4489 Set_Elaboration_Flag (N, Corresponding_Spec (N));
4490 Set_In_Package_Body (Spec_Id, False);
4492 -- Set to encode entity names in package body before gigi is called
4494 Qualify_Entity_Names (N);
4496 if Ekind (Spec_Id) /= E_Generic_Package then
4497 Build_Finalizer
4498 (N => N,
4499 Clean_Stmts => No_List,
4500 Mark_Id => Empty,
4501 Top_Decls => No_List,
4502 Defer_Abort => False,
4503 Fin_Id => Fin_Id);
4505 if Present (Fin_Id) then
4506 declare
4507 Body_Ent : Node_Id := Defining_Unit_Name (N);
4509 begin
4510 if Nkind (Body_Ent) = N_Defining_Program_Unit_Name then
4511 Body_Ent := Defining_Identifier (Body_Ent);
4512 end if;
4514 Set_Finalizer (Body_Ent, Fin_Id);
4515 end;
4516 end if;
4517 end if;
4518 end Expand_N_Package_Body;
4520 ----------------------------------
4521 -- Expand_N_Package_Declaration --
4522 ----------------------------------
4524 -- Add call to Activate_Tasks if there are tasks declared and the package
4525 -- has no body. Note that in Ada 83 this may result in premature activation
4526 -- of some tasks, given that we cannot tell whether a body will eventually
4527 -- appear.
4529 procedure Expand_N_Package_Declaration (N : Node_Id) is
4530 Id : constant Entity_Id := Defining_Entity (N);
4531 Spec : constant Node_Id := Specification (N);
4532 Decls : List_Id;
4533 Fin_Id : Entity_Id;
4535 No_Body : Boolean := False;
4536 -- True in the case of a package declaration that is a compilation
4537 -- unit and for which no associated body will be compiled in this
4538 -- compilation.
4540 begin
4541 -- Case of a package declaration other than a compilation unit
4543 if Nkind (Parent (N)) /= N_Compilation_Unit then
4544 null;
4546 -- Case of a compilation unit that does not require a body
4548 elsif not Body_Required (Parent (N))
4549 and then not Unit_Requires_Body (Id)
4550 then
4551 No_Body := True;
4553 -- Special case of generating calling stubs for a remote call interface
4554 -- package: even though the package declaration requires one, the body
4555 -- won't be processed in this compilation (so any stubs for RACWs
4556 -- declared in the package must be generated here, along with the spec).
4558 elsif Parent (N) = Cunit (Main_Unit)
4559 and then Is_Remote_Call_Interface (Id)
4560 and then Distribution_Stub_Mode = Generate_Caller_Stub_Body
4561 then
4562 No_Body := True;
4563 end if;
4565 -- For a nested instance, delay processing until freeze point
4567 if Has_Delayed_Freeze (Id)
4568 and then Nkind (Parent (N)) /= N_Compilation_Unit
4569 then
4570 return;
4571 end if;
4573 -- For a package declaration that implies no associated body, generate
4574 -- task activation call and RACW supporting bodies now (since we won't
4575 -- have a specific separate compilation unit for that).
4577 if No_Body then
4578 Push_Scope (Id);
4580 -- Generate RACW subprogram bodies
4582 if Has_RACW (Id) then
4583 Decls := Private_Declarations (Spec);
4585 if No (Decls) then
4586 Decls := Visible_Declarations (Spec);
4587 end if;
4589 if No (Decls) then
4590 Decls := New_List;
4591 Set_Visible_Declarations (Spec, Decls);
4592 end if;
4594 Append_RACW_Bodies (Decls, Id);
4595 Analyze_List (Decls);
4596 end if;
4598 -- Generate task activation call as last step of elaboration
4600 if Present (Activation_Chain_Entity (N)) then
4601 Build_Task_Activation_Call (N);
4602 end if;
4604 -- When the package is subject to pragma Initial_Condition and lacks
4605 -- a body, the assertion expression must be verified at the end of
4606 -- the visible declarations. Otherwise the check is performed at the
4607 -- end of the body statements (see Expand_N_Package_Body).
4609 if Present (Get_Pragma (Id, Pragma_Initial_Condition)) then
4610 Expand_Pragma_Initial_Condition (N);
4611 end if;
4613 Pop_Scope;
4614 end if;
4616 -- Build dispatch tables of library level tagged types
4618 if Tagged_Type_Expansion
4619 and then (Is_Compilation_Unit (Id)
4620 or else (Is_Generic_Instance (Id)
4621 and then Is_Library_Level_Entity (Id)))
4622 then
4623 Build_Static_Dispatch_Tables (N);
4624 end if;
4626 -- Note: it is not necessary to worry about generating a subprogram
4627 -- descriptor, since the only way to get exception handlers into a
4628 -- package spec is to include instantiations, and that would cause
4629 -- generation of subprogram descriptors to be delayed in any case.
4631 -- Set to encode entity names in package spec before gigi is called
4633 Qualify_Entity_Names (N);
4635 if Ekind (Id) /= E_Generic_Package then
4636 Build_Finalizer
4637 (N => N,
4638 Clean_Stmts => No_List,
4639 Mark_Id => Empty,
4640 Top_Decls => No_List,
4641 Defer_Abort => False,
4642 Fin_Id => Fin_Id);
4644 Set_Finalizer (Id, Fin_Id);
4645 end if;
4646 end Expand_N_Package_Declaration;
4648 -----------------------------
4649 -- Find_Node_To_Be_Wrapped --
4650 -----------------------------
4652 function Find_Node_To_Be_Wrapped (N : Node_Id) return Node_Id is
4653 P : Node_Id;
4654 The_Parent : Node_Id;
4656 begin
4657 The_Parent := N;
4658 P := Empty;
4659 loop
4660 case Nkind (The_Parent) is
4662 -- Simple statement can be wrapped
4664 when N_Pragma =>
4665 return The_Parent;
4667 -- Usually assignments are good candidate for wrapping except
4668 -- when they have been generated as part of a controlled aggregate
4669 -- where the wrapping should take place more globally. Note that
4670 -- No_Ctrl_Actions may be set also for non-controlled assignements
4671 -- in order to disable the use of dispatching _assign, so we need
4672 -- to test explicitly for a controlled type here.
4674 when N_Assignment_Statement =>
4675 if No_Ctrl_Actions (The_Parent)
4676 and then Needs_Finalization (Etype (Name (The_Parent)))
4677 then
4678 null;
4679 else
4680 return The_Parent;
4681 end if;
4683 -- An entry call statement is a special case if it occurs in the
4684 -- context of a Timed_Entry_Call. In this case we wrap the entire
4685 -- timed entry call.
4687 when N_Entry_Call_Statement
4688 | N_Procedure_Call_Statement
4690 if Nkind (Parent (The_Parent)) = N_Entry_Call_Alternative
4691 and then Nkind_In (Parent (Parent (The_Parent)),
4692 N_Timed_Entry_Call,
4693 N_Conditional_Entry_Call)
4694 then
4695 return Parent (Parent (The_Parent));
4696 else
4697 return The_Parent;
4698 end if;
4700 -- Object declarations are also a boundary for the transient scope
4701 -- even if they are not really wrapped. For further details, see
4702 -- Wrap_Transient_Declaration.
4704 when N_Object_Declaration
4705 | N_Object_Renaming_Declaration
4706 | N_Subtype_Declaration
4708 return The_Parent;
4710 -- The expression itself is to be wrapped if its parent is a
4711 -- compound statement or any other statement where the expression
4712 -- is known to be scalar.
4714 when N_Accept_Alternative
4715 | N_Attribute_Definition_Clause
4716 | N_Case_Statement
4717 | N_Code_Statement
4718 | N_Delay_Alternative
4719 | N_Delay_Until_Statement
4720 | N_Delay_Relative_Statement
4721 | N_Discriminant_Association
4722 | N_Elsif_Part
4723 | N_Entry_Body_Formal_Part
4724 | N_Exit_Statement
4725 | N_If_Statement
4726 | N_Iteration_Scheme
4727 | N_Terminate_Alternative
4729 pragma Assert (Present (P));
4730 return P;
4732 when N_Attribute_Reference =>
4733 if Is_Procedure_Attribute_Name
4734 (Attribute_Name (The_Parent))
4735 then
4736 return The_Parent;
4737 end if;
4739 -- A raise statement can be wrapped. This will arise when the
4740 -- expression in a raise_with_expression uses the secondary
4741 -- stack, for example.
4743 when N_Raise_Statement =>
4744 return The_Parent;
4746 -- If the expression is within the iteration scheme of a loop,
4747 -- we must create a declaration for it, followed by an assignment
4748 -- in order to have a usable statement to wrap.
4750 when N_Loop_Parameter_Specification =>
4751 return Parent (The_Parent);
4753 -- The following nodes contains "dummy calls" which don't need to
4754 -- be wrapped.
4756 when N_Component_Declaration
4757 | N_Discriminant_Specification
4758 | N_Parameter_Specification
4760 return Empty;
4762 -- The return statement is not to be wrapped when the function
4763 -- itself needs wrapping at the outer-level
4765 when N_Simple_Return_Statement =>
4766 declare
4767 Applies_To : constant Entity_Id :=
4768 Return_Applies_To
4769 (Return_Statement_Entity (The_Parent));
4770 Return_Type : constant Entity_Id := Etype (Applies_To);
4771 begin
4772 if Requires_Transient_Scope (Return_Type) then
4773 return Empty;
4774 else
4775 return The_Parent;
4776 end if;
4777 end;
4779 -- If we leave a scope without having been able to find a node to
4780 -- wrap, something is going wrong but this can happen in error
4781 -- situation that are not detected yet (such as a dynamic string
4782 -- in a pragma export)
4784 when N_Block_Statement
4785 | N_Package_Body
4786 | N_Package_Declaration
4787 | N_Subprogram_Body
4789 return Empty;
4791 -- Otherwise continue the search
4793 when others =>
4794 null;
4795 end case;
4797 P := The_Parent;
4798 The_Parent := Parent (P);
4799 end loop;
4800 end Find_Node_To_Be_Wrapped;
4802 ----------------------------------
4803 -- Has_New_Controlled_Component --
4804 ----------------------------------
4806 function Has_New_Controlled_Component (E : Entity_Id) return Boolean is
4807 Comp : Entity_Id;
4809 begin
4810 if not Is_Tagged_Type (E) then
4811 return Has_Controlled_Component (E);
4812 elsif not Is_Derived_Type (E) then
4813 return Has_Controlled_Component (E);
4814 end if;
4816 Comp := First_Component (E);
4817 while Present (Comp) loop
4818 if Chars (Comp) = Name_uParent then
4819 null;
4821 elsif Scope (Original_Record_Component (Comp)) = E
4822 and then Needs_Finalization (Etype (Comp))
4823 then
4824 return True;
4825 end if;
4827 Next_Component (Comp);
4828 end loop;
4830 return False;
4831 end Has_New_Controlled_Component;
4833 ---------------------------------
4834 -- Has_Simple_Protected_Object --
4835 ---------------------------------
4837 function Has_Simple_Protected_Object (T : Entity_Id) return Boolean is
4838 begin
4839 if Has_Task (T) then
4840 return False;
4842 elsif Is_Simple_Protected_Type (T) then
4843 return True;
4845 elsif Is_Array_Type (T) then
4846 return Has_Simple_Protected_Object (Component_Type (T));
4848 elsif Is_Record_Type (T) then
4849 declare
4850 Comp : Entity_Id;
4852 begin
4853 Comp := First_Component (T);
4854 while Present (Comp) loop
4855 if Has_Simple_Protected_Object (Etype (Comp)) then
4856 return True;
4857 end if;
4859 Next_Component (Comp);
4860 end loop;
4862 return False;
4863 end;
4865 else
4866 return False;
4867 end if;
4868 end Has_Simple_Protected_Object;
4870 ------------------------------------
4871 -- Insert_Actions_In_Scope_Around --
4872 ------------------------------------
4874 procedure Insert_Actions_In_Scope_Around
4875 (N : Node_Id;
4876 Clean : Boolean;
4877 Manage_SS : Boolean)
4879 Act_Before : constant List_Id :=
4880 Scope_Stack.Table (Scope_Stack.Last).Actions_To_Be_Wrapped (Before);
4881 Act_After : constant List_Id :=
4882 Scope_Stack.Table (Scope_Stack.Last).Actions_To_Be_Wrapped (After);
4883 Act_Cleanup : constant List_Id :=
4884 Scope_Stack.Table (Scope_Stack.Last).Actions_To_Be_Wrapped (Cleanup);
4885 -- Note: We used to use renamings of Scope_Stack.Table (Scope_Stack.
4886 -- Last), but this was incorrect as Process_Transients_In_Scope may
4887 -- introduce new scopes and cause a reallocation of Scope_Stack.Table.
4889 procedure Process_Transients_In_Scope
4890 (First_Object : Node_Id;
4891 Last_Object : Node_Id;
4892 Related_Node : Node_Id);
4893 -- Find all transient objects in the list First_Object .. Last_Object
4894 -- and generate finalization actions for them. Related_Node denotes the
4895 -- node which created all transient objects.
4897 ---------------------------------
4898 -- Process_Transients_In_Scope --
4899 ---------------------------------
4901 procedure Process_Transients_In_Scope
4902 (First_Object : Node_Id;
4903 Last_Object : Node_Id;
4904 Related_Node : Node_Id)
4906 Exceptions_OK : constant Boolean := Exceptions_In_Finalization_OK;
4908 Must_Hook : Boolean := False;
4909 -- Flag denoting whether the context requires transient object
4910 -- export to the outer finalizer.
4912 function Is_Subprogram_Call (N : Node_Id) return Traverse_Result;
4913 -- Determine whether an arbitrary node denotes a subprogram call
4915 procedure Detect_Subprogram_Call is
4916 new Traverse_Proc (Is_Subprogram_Call);
4918 procedure Process_Transient_In_Scope
4919 (Obj_Decl : Node_Id;
4920 Blk_Data : Finalization_Exception_Data;
4921 Blk_Stmts : List_Id);
4922 -- Generate finalization actions for a single transient object
4923 -- denoted by object declaration Obj_Decl. Blk_Data is the
4924 -- exception data of the enclosing block. Blk_Stmts denotes the
4925 -- statements of the enclosing block.
4927 ------------------------
4928 -- Is_Subprogram_Call --
4929 ------------------------
4931 function Is_Subprogram_Call (N : Node_Id) return Traverse_Result is
4932 begin
4933 -- A regular procedure or function call
4935 if Nkind (N) in N_Subprogram_Call then
4936 Must_Hook := True;
4937 return Abandon;
4939 -- Special cases
4941 -- Heavy expansion may relocate function calls outside the related
4942 -- node. Inspect the original node to detect the initial placement
4943 -- of the call.
4945 elsif Original_Node (N) /= N then
4946 Detect_Subprogram_Call (Original_Node (N));
4948 if Must_Hook then
4949 return Abandon;
4950 else
4951 return OK;
4952 end if;
4954 -- Generalized indexing always involves a function call
4956 elsif Nkind (N) = N_Indexed_Component
4957 and then Present (Generalized_Indexing (N))
4958 then
4959 Must_Hook := True;
4960 return Abandon;
4962 -- Keep searching
4964 else
4965 return OK;
4966 end if;
4967 end Is_Subprogram_Call;
4969 --------------------------------
4970 -- Process_Transient_In_Scope --
4971 --------------------------------
4973 procedure Process_Transient_In_Scope
4974 (Obj_Decl : Node_Id;
4975 Blk_Data : Finalization_Exception_Data;
4976 Blk_Stmts : List_Id)
4978 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4979 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4980 Fin_Call : Node_Id;
4981 Fin_Stmts : List_Id;
4982 Hook_Assign : Node_Id;
4983 Hook_Clear : Node_Id;
4984 Hook_Decl : Node_Id;
4985 Hook_Insert : Node_Id;
4986 Ptr_Decl : Node_Id;
4988 begin
4989 -- Mark the transient object as successfully processed to avoid
4990 -- double finalization.
4992 Set_Is_Finalized_Transient (Obj_Id);
4994 -- Construct all the pieces necessary to hook and finalize the
4995 -- transient object.
4997 Build_Transient_Object_Statements
4998 (Obj_Decl => Obj_Decl,
4999 Fin_Call => Fin_Call,
5000 Hook_Assign => Hook_Assign,
5001 Hook_Clear => Hook_Clear,
5002 Hook_Decl => Hook_Decl,
5003 Ptr_Decl => Ptr_Decl);
5005 -- The context contains at least one subprogram call which may
5006 -- raise an exception. This scenario employs "hooking" to pass
5007 -- transient objects to the enclosing finalizer in case of an
5008 -- exception.
5010 if Must_Hook then
5012 -- Add the access type which provides a reference to the
5013 -- transient object. Generate:
5015 -- type Ptr_Typ is access all Desig_Typ;
5017 Insert_Action (Obj_Decl, Ptr_Decl);
5019 -- Add the temporary which acts as a hook to the transient
5020 -- object. Generate:
5022 -- Hook : Ptr_Typ := null;
5024 Insert_Action (Obj_Decl, Hook_Decl);
5026 -- When the transient object is initialized by an aggregate,
5027 -- the hook must capture the object after the last aggregate
5028 -- assignment takes place. Only then is the object considered
5029 -- fully initialized. Generate:
5031 -- Hook := Ptr_Typ (Obj_Id);
5032 -- <or>
5033 -- Hook := Obj_Id'Unrestricted_Access;
5035 if Ekind_In (Obj_Id, E_Constant, E_Variable)
5036 and then Present (Last_Aggregate_Assignment (Obj_Id))
5037 then
5038 Hook_Insert := Last_Aggregate_Assignment (Obj_Id);
5040 -- Otherwise the hook seizes the related object immediately
5042 else
5043 Hook_Insert := Obj_Decl;
5044 end if;
5046 Insert_After_And_Analyze (Hook_Insert, Hook_Assign);
5047 end if;
5049 -- When exception propagation is enabled wrap the hook clear
5050 -- statement and the finalization call into a block to catch
5051 -- potential exceptions raised during finalization. Generate:
5053 -- begin
5054 -- [Hook := null;]
5055 -- [Deep_]Finalize (Obj_Ref);
5057 -- exception
5058 -- when others =>
5059 -- if not Raised then
5060 -- Raised := True;
5061 -- Save_Occurrence
5062 -- (Enn, Get_Current_Excep.all.all);
5063 -- end if;
5064 -- end;
5066 if Exceptions_OK then
5067 Fin_Stmts := New_List;
5069 if Must_Hook then
5070 Append_To (Fin_Stmts, Hook_Clear);
5071 end if;
5073 Append_To (Fin_Stmts, Fin_Call);
5075 Prepend_To (Blk_Stmts,
5076 Make_Block_Statement (Loc,
5077 Handled_Statement_Sequence =>
5078 Make_Handled_Sequence_Of_Statements (Loc,
5079 Statements => Fin_Stmts,
5080 Exception_Handlers => New_List (
5081 Build_Exception_Handler (Blk_Data)))));
5083 -- Otherwise generate:
5085 -- [Hook := null;]
5086 -- [Deep_]Finalize (Obj_Ref);
5088 -- Note that the statements are inserted in reverse order to
5089 -- achieve the desired final order outlined above.
5091 else
5092 Prepend_To (Blk_Stmts, Fin_Call);
5094 if Must_Hook then
5095 Prepend_To (Blk_Stmts, Hook_Clear);
5096 end if;
5097 end if;
5098 end Process_Transient_In_Scope;
5100 -- Local variables
5102 Built : Boolean := False;
5103 Blk_Data : Finalization_Exception_Data;
5104 Blk_Decl : Node_Id := Empty;
5105 Blk_Decls : List_Id := No_List;
5106 Blk_Ins : Node_Id;
5107 Blk_Stmts : List_Id;
5108 Loc : Source_Ptr;
5109 Obj_Decl : Node_Id;
5111 -- Start of processing for Process_Transients_In_Scope
5113 begin
5114 -- The expansion performed by this routine is as follows:
5116 -- type Ptr_Typ_1 is access all Ctrl_Trans_Obj_1_Typ;
5117 -- Hook_1 : Ptr_Typ_1 := null;
5118 -- Ctrl_Trans_Obj_1 : ...;
5119 -- Hook_1 := Ctrl_Trans_Obj_1'Unrestricted_Access;
5120 -- . . .
5121 -- type Ptr_Typ_N is access all Ctrl_Trans_Obj_N_Typ;
5122 -- Hook_N : Ptr_Typ_N := null;
5123 -- Ctrl_Trans_Obj_N : ...;
5124 -- Hook_N := Ctrl_Trans_Obj_N'Unrestricted_Access;
5126 -- declare
5127 -- Abrt : constant Boolean := ...;
5128 -- Ex : Exception_Occurrence;
5129 -- Raised : Boolean := False;
5131 -- begin
5132 -- Abort_Defer;
5134 -- begin
5135 -- Hook_N := null;
5136 -- [Deep_]Finalize (Ctrl_Trans_Obj_N);
5138 -- exception
5139 -- when others =>
5140 -- if not Raised then
5141 -- Raised := True;
5142 -- Save_Occurrence (Ex, Get_Current_Excep.all.all);
5143 -- end;
5144 -- . . .
5145 -- begin
5146 -- Hook_1 := null;
5147 -- [Deep_]Finalize (Ctrl_Trans_Obj_1);
5149 -- exception
5150 -- when others =>
5151 -- if not Raised then
5152 -- Raised := True;
5153 -- Save_Occurrence (Ex, Get_Current_Excep.all.all);
5154 -- end;
5156 -- Abort_Undefer;
5158 -- if Raised and not Abrt then
5159 -- Raise_From_Controlled_Operation (Ex);
5160 -- end if;
5161 -- end;
5163 -- Recognize a scenario where the transient context is an object
5164 -- declaration initialized by a build-in-place function call:
5166 -- Obj : ... := BIP_Function_Call (Ctrl_Func_Call);
5168 -- The rough expansion of the above is:
5170 -- Temp : ... := Ctrl_Func_Call;
5171 -- Obj : ...;
5172 -- Res : ... := BIP_Func_Call (..., Obj, ...);
5174 -- The finalization of any transient object must happen after the
5175 -- build-in-place function call is executed.
5177 if Nkind (N) = N_Object_Declaration
5178 and then Present (BIP_Initialization_Call (Defining_Identifier (N)))
5179 then
5180 Must_Hook := True;
5181 Blk_Ins := BIP_Initialization_Call (Defining_Identifier (N));
5183 -- Search the context for at least one subprogram call. If found, the
5184 -- machinery exports all transient objects to the enclosing finalizer
5185 -- due to the possibility of abnormal call termination.
5187 else
5188 Detect_Subprogram_Call (N);
5189 Blk_Ins := Last_Object;
5190 end if;
5192 if Clean then
5193 Insert_List_After_And_Analyze (Blk_Ins, Act_Cleanup);
5194 end if;
5196 -- Examine all objects in the list First_Object .. Last_Object
5198 Obj_Decl := First_Object;
5199 while Present (Obj_Decl) loop
5200 if Nkind (Obj_Decl) = N_Object_Declaration
5201 and then Analyzed (Obj_Decl)
5202 and then Is_Finalizable_Transient (Obj_Decl, N)
5204 -- Do not process the node to be wrapped since it will be
5205 -- handled by the enclosing finalizer.
5207 and then Obj_Decl /= Related_Node
5208 then
5209 Loc := Sloc (Obj_Decl);
5211 -- Before generating the clean up code for the first transient
5212 -- object, create a wrapper block which houses all hook clear
5213 -- statements and finalization calls. This wrapper is needed by
5214 -- the back-end.
5216 if not Built then
5217 Built := True;
5218 Blk_Stmts := New_List;
5220 -- Generate:
5221 -- Abrt : constant Boolean := ...;
5222 -- Ex : Exception_Occurrence;
5223 -- Raised : Boolean := False;
5225 if Exceptions_OK then
5226 Blk_Decls := New_List;
5227 Build_Object_Declarations (Blk_Data, Blk_Decls, Loc);
5228 end if;
5230 Blk_Decl :=
5231 Make_Block_Statement (Loc,
5232 Declarations => Blk_Decls,
5233 Handled_Statement_Sequence =>
5234 Make_Handled_Sequence_Of_Statements (Loc,
5235 Statements => Blk_Stmts));
5236 end if;
5238 -- Construct all necessary circuitry to hook and finalize a
5239 -- single transient object.
5241 Process_Transient_In_Scope
5242 (Obj_Decl => Obj_Decl,
5243 Blk_Data => Blk_Data,
5244 Blk_Stmts => Blk_Stmts);
5245 end if;
5247 -- Terminate the scan after the last object has been processed to
5248 -- avoid touching unrelated code.
5250 if Obj_Decl = Last_Object then
5251 exit;
5252 end if;
5254 Next (Obj_Decl);
5255 end loop;
5257 -- Complete the decoration of the enclosing finalization block and
5258 -- insert it into the tree.
5260 if Present (Blk_Decl) then
5262 -- Note that this Abort_Undefer does not require a extra block or
5263 -- an AT_END handler because each finalization exception is caught
5264 -- in its own corresponding finalization block. As a result, the
5265 -- call to Abort_Defer always takes place.
5267 if Abort_Allowed then
5268 Prepend_To (Blk_Stmts,
5269 Build_Runtime_Call (Loc, RE_Abort_Defer));
5271 Append_To (Blk_Stmts,
5272 Build_Runtime_Call (Loc, RE_Abort_Undefer));
5273 end if;
5275 -- Generate:
5276 -- if Raised and then not Abrt then
5277 -- Raise_From_Controlled_Operation (Ex);
5278 -- end if;
5280 if Exceptions_OK then
5281 Append_To (Blk_Stmts, Build_Raise_Statement (Blk_Data));
5282 end if;
5284 Insert_After_And_Analyze (Blk_Ins, Blk_Decl);
5285 end if;
5286 end Process_Transients_In_Scope;
5288 -- Local variables
5290 Loc : constant Source_Ptr := Sloc (N);
5291 Node_To_Wrap : constant Node_Id := Node_To_Be_Wrapped;
5292 First_Obj : Node_Id;
5293 Last_Obj : Node_Id;
5294 Mark_Id : Entity_Id;
5295 Target : Node_Id;
5297 -- Start of processing for Insert_Actions_In_Scope_Around
5299 begin
5300 -- Nothing to do if the scope does not manage the secondary stack or
5301 -- does not contain meaninful actions for insertion.
5303 if not Manage_SS
5304 and then No (Act_Before)
5305 and then No (Act_After)
5306 and then No (Act_Cleanup)
5307 then
5308 return;
5309 end if;
5311 -- If the node to be wrapped is the trigger of an asynchronous select,
5312 -- it is not part of a statement list. The actions must be inserted
5313 -- before the select itself, which is part of some list of statements.
5314 -- Note that the triggering alternative includes the triggering
5315 -- statement and an optional statement list. If the node to be
5316 -- wrapped is part of that list, the normal insertion applies.
5318 if Nkind (Parent (Node_To_Wrap)) = N_Triggering_Alternative
5319 and then not Is_List_Member (Node_To_Wrap)
5320 then
5321 Target := Parent (Parent (Node_To_Wrap));
5322 else
5323 Target := N;
5324 end if;
5326 First_Obj := Target;
5327 Last_Obj := Target;
5329 -- Add all actions associated with a transient scope into the main tree.
5330 -- There are several scenarios here:
5332 -- +--- Before ----+ +----- After ---+
5333 -- 1) First_Obj ....... Target ........ Last_Obj
5335 -- 2) First_Obj ....... Target
5337 -- 3) Target ........ Last_Obj
5339 -- Flag declarations are inserted before the first object
5341 if Present (Act_Before) then
5342 First_Obj := First (Act_Before);
5343 Insert_List_Before (Target, Act_Before);
5344 end if;
5346 -- Finalization calls are inserted after the last object
5348 if Present (Act_After) then
5349 Last_Obj := Last (Act_After);
5350 Insert_List_After (Target, Act_After);
5351 end if;
5353 -- Mark and release the secondary stack when the context warrants it
5355 if Manage_SS then
5356 Mark_Id := Make_Temporary (Loc, 'M');
5358 -- Generate:
5359 -- Mnn : constant Mark_Id := SS_Mark;
5361 Insert_Before_And_Analyze
5362 (First_Obj, Build_SS_Mark_Call (Loc, Mark_Id));
5364 -- Generate:
5365 -- SS_Release (Mnn);
5367 Insert_After_And_Analyze
5368 (Last_Obj, Build_SS_Release_Call (Loc, Mark_Id));
5369 end if;
5371 -- Check for transient objects associated with Target and generate the
5372 -- appropriate finalization actions for them.
5374 Process_Transients_In_Scope
5375 (First_Object => First_Obj,
5376 Last_Object => Last_Obj,
5377 Related_Node => Target);
5379 -- Reset the action lists
5381 Scope_Stack.Table
5382 (Scope_Stack.Last).Actions_To_Be_Wrapped (Before) := No_List;
5383 Scope_Stack.Table
5384 (Scope_Stack.Last).Actions_To_Be_Wrapped (After) := No_List;
5386 if Clean then
5387 Scope_Stack.Table
5388 (Scope_Stack.Last).Actions_To_Be_Wrapped (Cleanup) := No_List;
5389 end if;
5390 end Insert_Actions_In_Scope_Around;
5392 ------------------------------
5393 -- Is_Simple_Protected_Type --
5394 ------------------------------
5396 function Is_Simple_Protected_Type (T : Entity_Id) return Boolean is
5397 begin
5398 return
5399 Is_Protected_Type (T)
5400 and then not Uses_Lock_Free (T)
5401 and then not Has_Entries (T)
5402 and then Is_RTE (Find_Protection_Type (T), RE_Protection);
5403 end Is_Simple_Protected_Type;
5405 -----------------------
5406 -- Make_Adjust_Call --
5407 -----------------------
5409 function Make_Adjust_Call
5410 (Obj_Ref : Node_Id;
5411 Typ : Entity_Id;
5412 Skip_Self : Boolean := False) return Node_Id
5414 Loc : constant Source_Ptr := Sloc (Obj_Ref);
5415 Adj_Id : Entity_Id := Empty;
5416 Ref : Node_Id;
5417 Utyp : Entity_Id;
5419 begin
5420 Ref := Obj_Ref;
5422 -- Recover the proper type which contains Deep_Adjust
5424 if Is_Class_Wide_Type (Typ) then
5425 Utyp := Root_Type (Typ);
5426 else
5427 Utyp := Typ;
5428 end if;
5430 Utyp := Underlying_Type (Base_Type (Utyp));
5431 Set_Assignment_OK (Ref);
5433 -- Deal with untagged derivation of private views
5435 if Present (Utyp) and then Is_Untagged_Derivation (Typ) then
5436 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5437 Ref := Unchecked_Convert_To (Utyp, Ref);
5438 Set_Assignment_OK (Ref);
5439 end if;
5441 -- When dealing with the completion of a private type, use the base
5442 -- type instead.
5444 if Present (Utyp) and then Utyp /= Base_Type (Utyp) then
5445 pragma Assert (Is_Private_Type (Typ));
5447 Utyp := Base_Type (Utyp);
5448 Ref := Unchecked_Convert_To (Utyp, Ref);
5449 end if;
5451 -- The underlying type may not be present due to a missing full view. In
5452 -- this case freezing did not take place and there is no [Deep_]Adjust
5453 -- primitive to call.
5455 if No (Utyp) then
5456 return Empty;
5458 elsif Skip_Self then
5459 if Has_Controlled_Component (Utyp) then
5460 if Is_Tagged_Type (Utyp) then
5461 Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust);
5462 else
5463 Adj_Id := TSS (Utyp, TSS_Deep_Adjust);
5464 end if;
5465 end if;
5467 -- Class-wide types, interfaces and types with controlled components
5469 elsif Is_Class_Wide_Type (Typ)
5470 or else Is_Interface (Typ)
5471 or else Has_Controlled_Component (Utyp)
5472 then
5473 if Is_Tagged_Type (Utyp) then
5474 Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust);
5475 else
5476 Adj_Id := TSS (Utyp, TSS_Deep_Adjust);
5477 end if;
5479 -- Derivations from [Limited_]Controlled
5481 elsif Is_Controlled (Utyp) then
5482 if Has_Controlled_Component (Utyp) then
5483 Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust);
5484 else
5485 Adj_Id := Find_Optional_Prim_Op (Utyp, Name_Of (Adjust_Case));
5486 end if;
5488 -- Tagged types
5490 elsif Is_Tagged_Type (Utyp) then
5491 Adj_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Adjust);
5493 else
5494 raise Program_Error;
5495 end if;
5497 if Present (Adj_Id) then
5499 -- If the object is unanalyzed, set its expected type for use in
5500 -- Convert_View in case an additional conversion is needed.
5502 if No (Etype (Ref))
5503 and then Nkind (Ref) /= N_Unchecked_Type_Conversion
5504 then
5505 Set_Etype (Ref, Typ);
5506 end if;
5508 -- The object reference may need another conversion depending on the
5509 -- type of the formal and that of the actual.
5511 if not Is_Class_Wide_Type (Typ) then
5512 Ref := Convert_View (Adj_Id, Ref);
5513 end if;
5515 return
5516 Make_Call (Loc,
5517 Proc_Id => Adj_Id,
5518 Param => Ref,
5519 Skip_Self => Skip_Self);
5520 else
5521 return Empty;
5522 end if;
5523 end Make_Adjust_Call;
5525 ----------------------
5526 -- Make_Detach_Call --
5527 ----------------------
5529 function Make_Detach_Call (Obj_Ref : Node_Id) return Node_Id is
5530 Loc : constant Source_Ptr := Sloc (Obj_Ref);
5532 begin
5533 return
5534 Make_Procedure_Call_Statement (Loc,
5535 Name =>
5536 New_Occurrence_Of (RTE (RE_Detach), Loc),
5537 Parameter_Associations => New_List (
5538 Unchecked_Convert_To (RTE (RE_Root_Controlled_Ptr), Obj_Ref)));
5539 end Make_Detach_Call;
5541 ---------------
5542 -- Make_Call --
5543 ---------------
5545 function Make_Call
5546 (Loc : Source_Ptr;
5547 Proc_Id : Entity_Id;
5548 Param : Node_Id;
5549 Skip_Self : Boolean := False) return Node_Id
5551 Params : constant List_Id := New_List (Param);
5553 begin
5554 -- Do not apply the controlled action to the object itself by signaling
5555 -- the related routine to avoid self.
5557 if Skip_Self then
5558 Append_To (Params, New_Occurrence_Of (Standard_False, Loc));
5559 end if;
5561 return
5562 Make_Procedure_Call_Statement (Loc,
5563 Name => New_Occurrence_Of (Proc_Id, Loc),
5564 Parameter_Associations => Params);
5565 end Make_Call;
5567 --------------------------
5568 -- Make_Deep_Array_Body --
5569 --------------------------
5571 function Make_Deep_Array_Body
5572 (Prim : Final_Primitives;
5573 Typ : Entity_Id) return List_Id
5575 Exceptions_OK : constant Boolean := Exceptions_In_Finalization_OK;
5577 function Build_Adjust_Or_Finalize_Statements
5578 (Typ : Entity_Id) return List_Id;
5579 -- Create the statements necessary to adjust or finalize an array of
5580 -- controlled elements. Generate:
5582 -- declare
5583 -- Abort : constant Boolean := Triggered_By_Abort;
5584 -- <or>
5585 -- Abort : constant Boolean := False; -- no abort
5587 -- E : Exception_Occurrence;
5588 -- Raised : Boolean := False;
5590 -- begin
5591 -- for J1 in [reverse] Typ'First (1) .. Typ'Last (1) loop
5592 -- ^-- in the finalization case
5593 -- ...
5594 -- for Jn in [reverse] Typ'First (n) .. Typ'Last (n) loop
5595 -- begin
5596 -- [Deep_]Adjust / Finalize (V (J1, ..., Jn));
5598 -- exception
5599 -- when others =>
5600 -- if not Raised then
5601 -- Raised := True;
5602 -- Save_Occurrence (E, Get_Current_Excep.all.all);
5603 -- end if;
5604 -- end;
5605 -- end loop;
5606 -- ...
5607 -- end loop;
5609 -- if Raised and then not Abort then
5610 -- Raise_From_Controlled_Operation (E);
5611 -- end if;
5612 -- end;
5614 function Build_Initialize_Statements (Typ : Entity_Id) return List_Id;
5615 -- Create the statements necessary to initialize an array of controlled
5616 -- elements. Include a mechanism to carry out partial finalization if an
5617 -- exception occurs. Generate:
5619 -- declare
5620 -- Counter : Integer := 0;
5622 -- begin
5623 -- for J1 in V'Range (1) loop
5624 -- ...
5625 -- for JN in V'Range (N) loop
5626 -- begin
5627 -- [Deep_]Initialize (V (J1, ..., JN));
5629 -- Counter := Counter + 1;
5631 -- exception
5632 -- when others =>
5633 -- declare
5634 -- Abort : constant Boolean := Triggered_By_Abort;
5635 -- <or>
5636 -- Abort : constant Boolean := False; -- no abort
5637 -- E : Exception_Occurrence;
5638 -- Raised : Boolean := False;
5640 -- begin
5641 -- Counter :=
5642 -- V'Length (1) *
5643 -- V'Length (2) *
5644 -- ...
5645 -- V'Length (N) - Counter;
5647 -- for F1 in reverse V'Range (1) loop
5648 -- ...
5649 -- for FN in reverse V'Range (N) loop
5650 -- if Counter > 0 then
5651 -- Counter := Counter - 1;
5652 -- else
5653 -- begin
5654 -- [Deep_]Finalize (V (F1, ..., FN));
5656 -- exception
5657 -- when others =>
5658 -- if not Raised then
5659 -- Raised := True;
5660 -- Save_Occurrence (E,
5661 -- Get_Current_Excep.all.all);
5662 -- end if;
5663 -- end;
5664 -- end if;
5665 -- end loop;
5666 -- ...
5667 -- end loop;
5668 -- end;
5670 -- if Raised and then not Abort then
5671 -- Raise_From_Controlled_Operation (E);
5672 -- end if;
5674 -- raise;
5675 -- end;
5676 -- end loop;
5677 -- end loop;
5678 -- end;
5680 function New_References_To
5681 (L : List_Id;
5682 Loc : Source_Ptr) return List_Id;
5683 -- Given a list of defining identifiers, return a list of references to
5684 -- the original identifiers, in the same order as they appear.
5686 -----------------------------------------
5687 -- Build_Adjust_Or_Finalize_Statements --
5688 -----------------------------------------
5690 function Build_Adjust_Or_Finalize_Statements
5691 (Typ : Entity_Id) return List_Id
5693 Comp_Typ : constant Entity_Id := Component_Type (Typ);
5694 Index_List : constant List_Id := New_List;
5695 Loc : constant Source_Ptr := Sloc (Typ);
5696 Num_Dims : constant Int := Number_Dimensions (Typ);
5698 procedure Build_Indexes;
5699 -- Generate the indexes used in the dimension loops
5701 -------------------
5702 -- Build_Indexes --
5703 -------------------
5705 procedure Build_Indexes is
5706 begin
5707 -- Generate the following identifiers:
5708 -- Jnn - for initialization
5710 for Dim in 1 .. Num_Dims loop
5711 Append_To (Index_List,
5712 Make_Defining_Identifier (Loc, New_External_Name ('J', Dim)));
5713 end loop;
5714 end Build_Indexes;
5716 -- Local variables
5718 Final_Decls : List_Id := No_List;
5719 Final_Data : Finalization_Exception_Data;
5720 Block : Node_Id;
5721 Call : Node_Id;
5722 Comp_Ref : Node_Id;
5723 Core_Loop : Node_Id;
5724 Dim : Int;
5725 J : Entity_Id;
5726 Loop_Id : Entity_Id;
5727 Stmts : List_Id;
5729 -- Start of processing for Build_Adjust_Or_Finalize_Statements
5731 begin
5732 Final_Decls := New_List;
5734 Build_Indexes;
5735 Build_Object_Declarations (Final_Data, Final_Decls, Loc);
5737 Comp_Ref :=
5738 Make_Indexed_Component (Loc,
5739 Prefix => Make_Identifier (Loc, Name_V),
5740 Expressions => New_References_To (Index_List, Loc));
5741 Set_Etype (Comp_Ref, Comp_Typ);
5743 -- Generate:
5744 -- [Deep_]Adjust (V (J1, ..., JN))
5746 if Prim = Adjust_Case then
5747 Call := Make_Adjust_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ);
5749 -- Generate:
5750 -- [Deep_]Finalize (V (J1, ..., JN))
5752 else pragma Assert (Prim = Finalize_Case);
5753 Call := Make_Final_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ);
5754 end if;
5756 if Present (Call) then
5758 -- Generate the block which houses the adjust or finalize call:
5760 -- begin
5761 -- <adjust or finalize call>
5763 -- exception
5764 -- when others =>
5765 -- if not Raised then
5766 -- Raised := True;
5767 -- Save_Occurrence (E, Get_Current_Excep.all.all);
5768 -- end if;
5769 -- end;
5771 if Exceptions_OK then
5772 Core_Loop :=
5773 Make_Block_Statement (Loc,
5774 Handled_Statement_Sequence =>
5775 Make_Handled_Sequence_Of_Statements (Loc,
5776 Statements => New_List (Call),
5777 Exception_Handlers => New_List (
5778 Build_Exception_Handler (Final_Data))));
5779 else
5780 Core_Loop := Call;
5781 end if;
5783 -- Generate the dimension loops starting from the innermost one
5785 -- for Jnn in [reverse] V'Range (Dim) loop
5786 -- <core loop>
5787 -- end loop;
5789 J := Last (Index_List);
5790 Dim := Num_Dims;
5791 while Present (J) and then Dim > 0 loop
5792 Loop_Id := J;
5793 Prev (J);
5794 Remove (Loop_Id);
5796 Core_Loop :=
5797 Make_Loop_Statement (Loc,
5798 Iteration_Scheme =>
5799 Make_Iteration_Scheme (Loc,
5800 Loop_Parameter_Specification =>
5801 Make_Loop_Parameter_Specification (Loc,
5802 Defining_Identifier => Loop_Id,
5803 Discrete_Subtype_Definition =>
5804 Make_Attribute_Reference (Loc,
5805 Prefix => Make_Identifier (Loc, Name_V),
5806 Attribute_Name => Name_Range,
5807 Expressions => New_List (
5808 Make_Integer_Literal (Loc, Dim))),
5810 Reverse_Present =>
5811 Prim = Finalize_Case)),
5813 Statements => New_List (Core_Loop),
5814 End_Label => Empty);
5816 Dim := Dim - 1;
5817 end loop;
5819 -- Generate the block which contains the core loop, declarations
5820 -- of the abort flag, the exception occurrence, the raised flag
5821 -- and the conditional raise:
5823 -- declare
5824 -- Abort : constant Boolean := Triggered_By_Abort;
5825 -- <or>
5826 -- Abort : constant Boolean := False; -- no abort
5828 -- E : Exception_Occurrence;
5829 -- Raised : Boolean := False;
5831 -- begin
5832 -- <core loop>
5834 -- if Raised and then not Abort then
5835 -- Raise_From_Controlled_Operation (E);
5836 -- end if;
5837 -- end;
5839 Stmts := New_List (Core_Loop);
5841 if Exceptions_OK then
5842 Append_To (Stmts, Build_Raise_Statement (Final_Data));
5843 end if;
5845 Block :=
5846 Make_Block_Statement (Loc,
5847 Declarations => Final_Decls,
5848 Handled_Statement_Sequence =>
5849 Make_Handled_Sequence_Of_Statements (Loc,
5850 Statements => Stmts));
5852 -- Otherwise previous errors or a missing full view may prevent the
5853 -- proper freezing of the component type. If this is the case, there
5854 -- is no [Deep_]Adjust or [Deep_]Finalize primitive to call.
5856 else
5857 Block := Make_Null_Statement (Loc);
5858 end if;
5860 return New_List (Block);
5861 end Build_Adjust_Or_Finalize_Statements;
5863 ---------------------------------
5864 -- Build_Initialize_Statements --
5865 ---------------------------------
5867 function Build_Initialize_Statements (Typ : Entity_Id) return List_Id is
5868 Comp_Typ : constant Entity_Id := Component_Type (Typ);
5869 Final_List : constant List_Id := New_List;
5870 Index_List : constant List_Id := New_List;
5871 Loc : constant Source_Ptr := Sloc (Typ);
5872 Num_Dims : constant Int := Number_Dimensions (Typ);
5874 function Build_Assignment (Counter_Id : Entity_Id) return Node_Id;
5875 -- Generate the following assignment:
5876 -- Counter := V'Length (1) *
5877 -- ...
5878 -- V'Length (N) - Counter;
5880 -- Counter_Id denotes the entity of the counter.
5882 function Build_Finalization_Call return Node_Id;
5883 -- Generate a deep finalization call for an array element
5885 procedure Build_Indexes;
5886 -- Generate the initialization and finalization indexes used in the
5887 -- dimension loops.
5889 function Build_Initialization_Call return Node_Id;
5890 -- Generate a deep initialization call for an array element
5892 ----------------------
5893 -- Build_Assignment --
5894 ----------------------
5896 function Build_Assignment (Counter_Id : Entity_Id) return Node_Id is
5897 Dim : Int;
5898 Expr : Node_Id;
5900 begin
5901 -- Start from the first dimension and generate:
5902 -- V'Length (1)
5904 Dim := 1;
5905 Expr :=
5906 Make_Attribute_Reference (Loc,
5907 Prefix => Make_Identifier (Loc, Name_V),
5908 Attribute_Name => Name_Length,
5909 Expressions => New_List (Make_Integer_Literal (Loc, Dim)));
5911 -- Process the rest of the dimensions, generate:
5912 -- Expr * V'Length (N)
5914 Dim := Dim + 1;
5915 while Dim <= Num_Dims loop
5916 Expr :=
5917 Make_Op_Multiply (Loc,
5918 Left_Opnd => Expr,
5919 Right_Opnd =>
5920 Make_Attribute_Reference (Loc,
5921 Prefix => Make_Identifier (Loc, Name_V),
5922 Attribute_Name => Name_Length,
5923 Expressions => New_List (
5924 Make_Integer_Literal (Loc, Dim))));
5926 Dim := Dim + 1;
5927 end loop;
5929 -- Generate:
5930 -- Counter := Expr - Counter;
5932 return
5933 Make_Assignment_Statement (Loc,
5934 Name => New_Occurrence_Of (Counter_Id, Loc),
5935 Expression =>
5936 Make_Op_Subtract (Loc,
5937 Left_Opnd => Expr,
5938 Right_Opnd => New_Occurrence_Of (Counter_Id, Loc)));
5939 end Build_Assignment;
5941 -----------------------------
5942 -- Build_Finalization_Call --
5943 -----------------------------
5945 function Build_Finalization_Call return Node_Id is
5946 Comp_Ref : constant Node_Id :=
5947 Make_Indexed_Component (Loc,
5948 Prefix => Make_Identifier (Loc, Name_V),
5949 Expressions => New_References_To (Final_List, Loc));
5951 begin
5952 Set_Etype (Comp_Ref, Comp_Typ);
5954 -- Generate:
5955 -- [Deep_]Finalize (V);
5957 return Make_Final_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ);
5958 end Build_Finalization_Call;
5960 -------------------
5961 -- Build_Indexes --
5962 -------------------
5964 procedure Build_Indexes is
5965 begin
5966 -- Generate the following identifiers:
5967 -- Jnn - for initialization
5968 -- Fnn - for finalization
5970 for Dim in 1 .. Num_Dims loop
5971 Append_To (Index_List,
5972 Make_Defining_Identifier (Loc, New_External_Name ('J', Dim)));
5974 Append_To (Final_List,
5975 Make_Defining_Identifier (Loc, New_External_Name ('F', Dim)));
5976 end loop;
5977 end Build_Indexes;
5979 -------------------------------
5980 -- Build_Initialization_Call --
5981 -------------------------------
5983 function Build_Initialization_Call return Node_Id is
5984 Comp_Ref : constant Node_Id :=
5985 Make_Indexed_Component (Loc,
5986 Prefix => Make_Identifier (Loc, Name_V),
5987 Expressions => New_References_To (Index_List, Loc));
5989 begin
5990 Set_Etype (Comp_Ref, Comp_Typ);
5992 -- Generate:
5993 -- [Deep_]Initialize (V (J1, ..., JN));
5995 return Make_Init_Call (Obj_Ref => Comp_Ref, Typ => Comp_Typ);
5996 end Build_Initialization_Call;
5998 -- Local variables
6000 Counter_Id : Entity_Id;
6001 Dim : Int;
6002 F : Node_Id;
6003 Fin_Stmt : Node_Id;
6004 Final_Block : Node_Id;
6005 Final_Data : Finalization_Exception_Data;
6006 Final_Decls : List_Id := No_List;
6007 Final_Loop : Node_Id;
6008 Init_Block : Node_Id;
6009 Init_Call : Node_Id;
6010 Init_Loop : Node_Id;
6011 J : Node_Id;
6012 Loop_Id : Node_Id;
6013 Stmts : List_Id;
6015 -- Start of processing for Build_Initialize_Statements
6017 begin
6018 Counter_Id := Make_Temporary (Loc, 'C');
6019 Final_Decls := New_List;
6021 Build_Indexes;
6022 Build_Object_Declarations (Final_Data, Final_Decls, Loc);
6024 -- Generate the block which houses the finalization call, the index
6025 -- guard and the handler which triggers Program_Error later on.
6027 -- if Counter > 0 then
6028 -- Counter := Counter - 1;
6029 -- else
6030 -- begin
6031 -- [Deep_]Finalize (V (F1, ..., FN));
6032 -- exception
6033 -- when others =>
6034 -- if not Raised then
6035 -- Raised := True;
6036 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6037 -- end if;
6038 -- end;
6039 -- end if;
6041 Fin_Stmt := Build_Finalization_Call;
6043 if Present (Fin_Stmt) then
6044 if Exceptions_OK then
6045 Fin_Stmt :=
6046 Make_Block_Statement (Loc,
6047 Handled_Statement_Sequence =>
6048 Make_Handled_Sequence_Of_Statements (Loc,
6049 Statements => New_List (Fin_Stmt),
6050 Exception_Handlers => New_List (
6051 Build_Exception_Handler (Final_Data))));
6052 end if;
6054 -- This is the core of the loop, the dimension iterators are added
6055 -- one by one in reverse.
6057 Final_Loop :=
6058 Make_If_Statement (Loc,
6059 Condition =>
6060 Make_Op_Gt (Loc,
6061 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
6062 Right_Opnd => Make_Integer_Literal (Loc, 0)),
6064 Then_Statements => New_List (
6065 Make_Assignment_Statement (Loc,
6066 Name => New_Occurrence_Of (Counter_Id, Loc),
6067 Expression =>
6068 Make_Op_Subtract (Loc,
6069 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
6070 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
6072 Else_Statements => New_List (Fin_Stmt));
6074 -- Generate all finalization loops starting from the innermost
6075 -- dimension.
6077 -- for Fnn in reverse V'Range (Dim) loop
6078 -- <final loop>
6079 -- end loop;
6081 F := Last (Final_List);
6082 Dim := Num_Dims;
6083 while Present (F) and then Dim > 0 loop
6084 Loop_Id := F;
6085 Prev (F);
6086 Remove (Loop_Id);
6088 Final_Loop :=
6089 Make_Loop_Statement (Loc,
6090 Iteration_Scheme =>
6091 Make_Iteration_Scheme (Loc,
6092 Loop_Parameter_Specification =>
6093 Make_Loop_Parameter_Specification (Loc,
6094 Defining_Identifier => Loop_Id,
6095 Discrete_Subtype_Definition =>
6096 Make_Attribute_Reference (Loc,
6097 Prefix => Make_Identifier (Loc, Name_V),
6098 Attribute_Name => Name_Range,
6099 Expressions => New_List (
6100 Make_Integer_Literal (Loc, Dim))),
6102 Reverse_Present => True)),
6104 Statements => New_List (Final_Loop),
6105 End_Label => Empty);
6107 Dim := Dim - 1;
6108 end loop;
6110 -- Generate the block which contains the finalization loops, the
6111 -- declarations of the abort flag, the exception occurrence, the
6112 -- raised flag and the conditional raise.
6114 -- declare
6115 -- Abort : constant Boolean := Triggered_By_Abort;
6116 -- <or>
6117 -- Abort : constant Boolean := False; -- no abort
6119 -- E : Exception_Occurrence;
6120 -- Raised : Boolean := False;
6122 -- begin
6123 -- Counter :=
6124 -- V'Length (1) *
6125 -- ...
6126 -- V'Length (N) - Counter;
6128 -- <final loop>
6130 -- if Raised and then not Abort then
6131 -- Raise_From_Controlled_Operation (E);
6132 -- end if;
6134 -- raise;
6135 -- end;
6137 Stmts := New_List (Build_Assignment (Counter_Id), Final_Loop);
6139 if Exceptions_OK then
6140 Append_To (Stmts, Build_Raise_Statement (Final_Data));
6141 Append_To (Stmts, Make_Raise_Statement (Loc));
6142 end if;
6144 Final_Block :=
6145 Make_Block_Statement (Loc,
6146 Declarations => Final_Decls,
6147 Handled_Statement_Sequence =>
6148 Make_Handled_Sequence_Of_Statements (Loc,
6149 Statements => Stmts));
6151 -- Otherwise previous errors or a missing full view may prevent the
6152 -- proper freezing of the component type. If this is the case, there
6153 -- is no [Deep_]Finalize primitive to call.
6155 else
6156 Final_Block := Make_Null_Statement (Loc);
6157 end if;
6159 -- Generate the block which contains the initialization call and
6160 -- the partial finalization code.
6162 -- begin
6163 -- [Deep_]Initialize (V (J1, ..., JN));
6165 -- Counter := Counter + 1;
6167 -- exception
6168 -- when others =>
6169 -- <finalization code>
6170 -- end;
6172 Init_Call := Build_Initialization_Call;
6174 -- Only create finalization block if there is a non-trivial
6175 -- call to initialization.
6177 if Present (Init_Call)
6178 and then Nkind (Init_Call) /= N_Null_Statement
6179 then
6180 Init_Loop :=
6181 Make_Block_Statement (Loc,
6182 Handled_Statement_Sequence =>
6183 Make_Handled_Sequence_Of_Statements (Loc,
6184 Statements => New_List (Init_Call),
6185 Exception_Handlers => New_List (
6186 Make_Exception_Handler (Loc,
6187 Exception_Choices => New_List (
6188 Make_Others_Choice (Loc)),
6189 Statements => New_List (Final_Block)))));
6191 Append_To (Statements (Handled_Statement_Sequence (Init_Loop)),
6192 Make_Assignment_Statement (Loc,
6193 Name => New_Occurrence_Of (Counter_Id, Loc),
6194 Expression =>
6195 Make_Op_Add (Loc,
6196 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
6197 Right_Opnd => Make_Integer_Literal (Loc, 1))));
6199 -- Generate all initialization loops starting from the innermost
6200 -- dimension.
6202 -- for Jnn in V'Range (Dim) loop
6203 -- <init loop>
6204 -- end loop;
6206 J := Last (Index_List);
6207 Dim := Num_Dims;
6208 while Present (J) and then Dim > 0 loop
6209 Loop_Id := J;
6210 Prev (J);
6211 Remove (Loop_Id);
6213 Init_Loop :=
6214 Make_Loop_Statement (Loc,
6215 Iteration_Scheme =>
6216 Make_Iteration_Scheme (Loc,
6217 Loop_Parameter_Specification =>
6218 Make_Loop_Parameter_Specification (Loc,
6219 Defining_Identifier => Loop_Id,
6220 Discrete_Subtype_Definition =>
6221 Make_Attribute_Reference (Loc,
6222 Prefix => Make_Identifier (Loc, Name_V),
6223 Attribute_Name => Name_Range,
6224 Expressions => New_List (
6225 Make_Integer_Literal (Loc, Dim))))),
6227 Statements => New_List (Init_Loop),
6228 End_Label => Empty);
6230 Dim := Dim - 1;
6231 end loop;
6233 -- Generate the block which contains the counter variable and the
6234 -- initialization loops.
6236 -- declare
6237 -- Counter : Integer := 0;
6238 -- begin
6239 -- <init loop>
6240 -- end;
6242 Init_Block :=
6243 Make_Block_Statement (Loc,
6244 Declarations => New_List (
6245 Make_Object_Declaration (Loc,
6246 Defining_Identifier => Counter_Id,
6247 Object_Definition =>
6248 New_Occurrence_Of (Standard_Integer, Loc),
6249 Expression => Make_Integer_Literal (Loc, 0))),
6251 Handled_Statement_Sequence =>
6252 Make_Handled_Sequence_Of_Statements (Loc,
6253 Statements => New_List (Init_Loop)));
6255 -- Otherwise previous errors or a missing full view may prevent the
6256 -- proper freezing of the component type. If this is the case, there
6257 -- is no [Deep_]Initialize primitive to call.
6259 else
6260 Init_Block := Make_Null_Statement (Loc);
6261 end if;
6263 return New_List (Init_Block);
6264 end Build_Initialize_Statements;
6266 -----------------------
6267 -- New_References_To --
6268 -----------------------
6270 function New_References_To
6271 (L : List_Id;
6272 Loc : Source_Ptr) return List_Id
6274 Refs : constant List_Id := New_List;
6275 Id : Node_Id;
6277 begin
6278 Id := First (L);
6279 while Present (Id) loop
6280 Append_To (Refs, New_Occurrence_Of (Id, Loc));
6281 Next (Id);
6282 end loop;
6284 return Refs;
6285 end New_References_To;
6287 -- Start of processing for Make_Deep_Array_Body
6289 begin
6290 case Prim is
6291 when Address_Case =>
6292 return Make_Finalize_Address_Stmts (Typ);
6294 when Adjust_Case
6295 | Finalize_Case
6297 return Build_Adjust_Or_Finalize_Statements (Typ);
6299 when Initialize_Case =>
6300 return Build_Initialize_Statements (Typ);
6301 end case;
6302 end Make_Deep_Array_Body;
6304 --------------------
6305 -- Make_Deep_Proc --
6306 --------------------
6308 function Make_Deep_Proc
6309 (Prim : Final_Primitives;
6310 Typ : Entity_Id;
6311 Stmts : List_Id) return Entity_Id
6313 Loc : constant Source_Ptr := Sloc (Typ);
6314 Formals : List_Id;
6315 Proc_Id : Entity_Id;
6317 begin
6318 -- Create the object formal, generate:
6319 -- V : System.Address
6321 if Prim = Address_Case then
6322 Formals := New_List (
6323 Make_Parameter_Specification (Loc,
6324 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
6325 Parameter_Type =>
6326 New_Occurrence_Of (RTE (RE_Address), Loc)));
6328 -- Default case
6330 else
6331 -- V : in out Typ
6333 Formals := New_List (
6334 Make_Parameter_Specification (Loc,
6335 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
6336 In_Present => True,
6337 Out_Present => True,
6338 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
6340 -- F : Boolean := True
6342 if Prim = Adjust_Case
6343 or else Prim = Finalize_Case
6344 then
6345 Append_To (Formals,
6346 Make_Parameter_Specification (Loc,
6347 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
6348 Parameter_Type =>
6349 New_Occurrence_Of (Standard_Boolean, Loc),
6350 Expression =>
6351 New_Occurrence_Of (Standard_True, Loc)));
6352 end if;
6353 end if;
6355 Proc_Id :=
6356 Make_Defining_Identifier (Loc,
6357 Chars => Make_TSS_Name (Typ, Deep_Name_Of (Prim)));
6359 -- Generate:
6360 -- procedure Deep_Initialize / Adjust / Finalize (V : in out <typ>) is
6361 -- begin
6362 -- <stmts>
6363 -- exception -- Finalize and Adjust cases only
6364 -- raise Program_Error;
6365 -- end Deep_Initialize / Adjust / Finalize;
6367 -- or
6369 -- procedure Finalize_Address (V : System.Address) is
6370 -- begin
6371 -- <stmts>
6372 -- end Finalize_Address;
6374 Discard_Node (
6375 Make_Subprogram_Body (Loc,
6376 Specification =>
6377 Make_Procedure_Specification (Loc,
6378 Defining_Unit_Name => Proc_Id,
6379 Parameter_Specifications => Formals),
6381 Declarations => Empty_List,
6383 Handled_Statement_Sequence =>
6384 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)));
6386 -- If there are no calls to component initialization, indicate that
6387 -- the procedure is trivial, so prevent calls to it.
6389 if Is_Empty_List (Stmts)
6390 or else Nkind (First (Stmts)) = N_Null_Statement
6391 then
6392 Set_Is_Trivial_Subprogram (Proc_Id);
6393 end if;
6395 return Proc_Id;
6396 end Make_Deep_Proc;
6398 ---------------------------
6399 -- Make_Deep_Record_Body --
6400 ---------------------------
6402 function Make_Deep_Record_Body
6403 (Prim : Final_Primitives;
6404 Typ : Entity_Id;
6405 Is_Local : Boolean := False) return List_Id
6407 Exceptions_OK : constant Boolean := Exceptions_In_Finalization_OK;
6409 function Build_Adjust_Statements (Typ : Entity_Id) return List_Id;
6410 -- Build the statements necessary to adjust a record type. The type may
6411 -- have discriminants and contain variant parts. Generate:
6413 -- begin
6414 -- begin
6415 -- [Deep_]Adjust (V.Comp_1);
6416 -- exception
6417 -- when Id : others =>
6418 -- if not Raised then
6419 -- Raised := True;
6420 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6421 -- end if;
6422 -- end;
6423 -- . . .
6424 -- begin
6425 -- [Deep_]Adjust (V.Comp_N);
6426 -- exception
6427 -- when Id : others =>
6428 -- if not Raised then
6429 -- Raised := True;
6430 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6431 -- end if;
6432 -- end;
6434 -- begin
6435 -- Deep_Adjust (V._parent, False); -- If applicable
6436 -- exception
6437 -- when Id : others =>
6438 -- if not Raised then
6439 -- Raised := True;
6440 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6441 -- end if;
6442 -- end;
6444 -- if F then
6445 -- begin
6446 -- Adjust (V); -- If applicable
6447 -- exception
6448 -- when others =>
6449 -- if not Raised then
6450 -- Raised := True;
6451 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6452 -- end if;
6453 -- end;
6454 -- end if;
6456 -- if Raised and then not Abort then
6457 -- Raise_From_Controlled_Operation (E);
6458 -- end if;
6459 -- end;
6461 function Build_Finalize_Statements (Typ : Entity_Id) return List_Id;
6462 -- Build the statements necessary to finalize a record type. The type
6463 -- may have discriminants and contain variant parts. Generate:
6465 -- declare
6466 -- Abort : constant Boolean := Triggered_By_Abort;
6467 -- <or>
6468 -- Abort : constant Boolean := False; -- no abort
6469 -- E : Exception_Occurrence;
6470 -- Raised : Boolean := False;
6472 -- begin
6473 -- if F then
6474 -- begin
6475 -- Finalize (V); -- If applicable
6476 -- exception
6477 -- when others =>
6478 -- if not Raised then
6479 -- Raised := True;
6480 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6481 -- end if;
6482 -- end;
6483 -- end if;
6485 -- case Variant_1 is
6486 -- when Value_1 =>
6487 -- case State_Counter_N => -- If Is_Local is enabled
6488 -- when N => .
6489 -- goto LN; .
6490 -- ... .
6491 -- when 1 => .
6492 -- goto L1; .
6493 -- when others => .
6494 -- goto L0; .
6495 -- end case; .
6497 -- <<LN>> -- If Is_Local is enabled
6498 -- begin
6499 -- [Deep_]Finalize (V.Comp_N);
6500 -- exception
6501 -- when others =>
6502 -- if not Raised then
6503 -- Raised := True;
6504 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6505 -- end if;
6506 -- end;
6507 -- . . .
6508 -- <<L1>>
6509 -- begin
6510 -- [Deep_]Finalize (V.Comp_1);
6511 -- exception
6512 -- when others =>
6513 -- if not Raised then
6514 -- Raised := True;
6515 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6516 -- end if;
6517 -- end;
6518 -- <<L0>>
6519 -- end case;
6521 -- case State_Counter_1 => -- If Is_Local is enabled
6522 -- when M => .
6523 -- goto LM; .
6524 -- ...
6526 -- begin
6527 -- Deep_Finalize (V._parent, False); -- If applicable
6528 -- exception
6529 -- when Id : others =>
6530 -- if not Raised then
6531 -- Raised := True;
6532 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6533 -- end if;
6534 -- end;
6536 -- if Raised and then not Abort then
6537 -- Raise_From_Controlled_Operation (E);
6538 -- end if;
6539 -- end;
6541 function Parent_Field_Type (Typ : Entity_Id) return Entity_Id;
6542 -- Given a derived tagged type Typ, traverse all components, find field
6543 -- _parent and return its type.
6545 procedure Preprocess_Components
6546 (Comps : Node_Id;
6547 Num_Comps : out Nat;
6548 Has_POC : out Boolean);
6549 -- Examine all components in component list Comps, count all controlled
6550 -- components and determine whether at least one of them is per-object
6551 -- constrained. Component _parent is always skipped.
6553 -----------------------------
6554 -- Build_Adjust_Statements --
6555 -----------------------------
6557 function Build_Adjust_Statements (Typ : Entity_Id) return List_Id is
6558 Loc : constant Source_Ptr := Sloc (Typ);
6559 Typ_Def : constant Node_Id := Type_Definition (Parent (Typ));
6561 Finalizer_Data : Finalization_Exception_Data;
6563 function Process_Component_List_For_Adjust
6564 (Comps : Node_Id) return List_Id;
6565 -- Build all necessary adjust statements for a single component list
6567 ---------------------------------------
6568 -- Process_Component_List_For_Adjust --
6569 ---------------------------------------
6571 function Process_Component_List_For_Adjust
6572 (Comps : Node_Id) return List_Id
6574 Stmts : constant List_Id := New_List;
6576 procedure Process_Component_For_Adjust (Decl : Node_Id);
6577 -- Process the declaration of a single controlled component
6579 ----------------------------------
6580 -- Process_Component_For_Adjust --
6581 ----------------------------------
6583 procedure Process_Component_For_Adjust (Decl : Node_Id) is
6584 Id : constant Entity_Id := Defining_Identifier (Decl);
6585 Typ : constant Entity_Id := Etype (Id);
6587 Adj_Call : Node_Id;
6589 begin
6590 -- begin
6591 -- [Deep_]Adjust (V.Id);
6593 -- exception
6594 -- when others =>
6595 -- if not Raised then
6596 -- Raised := True;
6597 -- Save_Occurrence (E, Get_Current_Excep.all.all);
6598 -- end if;
6599 -- end;
6601 Adj_Call :=
6602 Make_Adjust_Call (
6603 Obj_Ref =>
6604 Make_Selected_Component (Loc,
6605 Prefix => Make_Identifier (Loc, Name_V),
6606 Selector_Name => Make_Identifier (Loc, Chars (Id))),
6607 Typ => Typ);
6609 -- Guard against a missing [Deep_]Adjust when the component
6610 -- type was not properly frozen.
6612 if Present (Adj_Call) then
6613 if Exceptions_OK then
6614 Adj_Call :=
6615 Make_Block_Statement (Loc,
6616 Handled_Statement_Sequence =>
6617 Make_Handled_Sequence_Of_Statements (Loc,
6618 Statements => New_List (Adj_Call),
6619 Exception_Handlers => New_List (
6620 Build_Exception_Handler (Finalizer_Data))));
6621 end if;
6623 Append_To (Stmts, Adj_Call);
6624 end if;
6625 end Process_Component_For_Adjust;
6627 -- Local variables
6629 Decl : Node_Id;
6630 Decl_Id : Entity_Id;
6631 Decl_Typ : Entity_Id;
6632 Has_POC : Boolean;
6633 Num_Comps : Nat;
6634 Var_Case : Node_Id;
6636 -- Start of processing for Process_Component_List_For_Adjust
6638 begin
6639 -- Perform an initial check, determine the number of controlled
6640 -- components in the current list and whether at least one of them
6641 -- is per-object constrained.
6643 Preprocess_Components (Comps, Num_Comps, Has_POC);
6645 -- The processing in this routine is done in the following order:
6646 -- 1) Regular components
6647 -- 2) Per-object constrained components
6648 -- 3) Variant parts
6650 if Num_Comps > 0 then
6652 -- Process all regular components in order of declarations
6654 Decl := First_Non_Pragma (Component_Items (Comps));
6655 while Present (Decl) loop
6656 Decl_Id := Defining_Identifier (Decl);
6657 Decl_Typ := Etype (Decl_Id);
6659 -- Skip _parent as well as per-object constrained components
6661 if Chars (Decl_Id) /= Name_uParent
6662 and then Needs_Finalization (Decl_Typ)
6663 then
6664 if Has_Access_Constraint (Decl_Id)
6665 and then No (Expression (Decl))
6666 then
6667 null;
6668 else
6669 Process_Component_For_Adjust (Decl);
6670 end if;
6671 end if;
6673 Next_Non_Pragma (Decl);
6674 end loop;
6676 -- Process all per-object constrained components in order of
6677 -- declarations.
6679 if Has_POC then
6680 Decl := First_Non_Pragma (Component_Items (Comps));
6681 while Present (Decl) loop
6682 Decl_Id := Defining_Identifier (Decl);
6683 Decl_Typ := Etype (Decl_Id);
6685 -- Skip _parent
6687 if Chars (Decl_Id) /= Name_uParent
6688 and then Needs_Finalization (Decl_Typ)
6689 and then Has_Access_Constraint (Decl_Id)
6690 and then No (Expression (Decl))
6691 then
6692 Process_Component_For_Adjust (Decl);
6693 end if;
6695 Next_Non_Pragma (Decl);
6696 end loop;
6697 end if;
6698 end if;
6700 -- Process all variants, if any
6702 Var_Case := Empty;
6703 if Present (Variant_Part (Comps)) then
6704 declare
6705 Var_Alts : constant List_Id := New_List;
6706 Var : Node_Id;
6708 begin
6709 Var := First_Non_Pragma (Variants (Variant_Part (Comps)));
6710 while Present (Var) loop
6712 -- Generate:
6713 -- when <discrete choices> =>
6714 -- <adjust statements>
6716 Append_To (Var_Alts,
6717 Make_Case_Statement_Alternative (Loc,
6718 Discrete_Choices =>
6719 New_Copy_List (Discrete_Choices (Var)),
6720 Statements =>
6721 Process_Component_List_For_Adjust (
6722 Component_List (Var))));
6724 Next_Non_Pragma (Var);
6725 end loop;
6727 -- Generate:
6728 -- case V.<discriminant> is
6729 -- when <discrete choices 1> =>
6730 -- <adjust statements 1>
6731 -- ...
6732 -- when <discrete choices N> =>
6733 -- <adjust statements N>
6734 -- end case;
6736 Var_Case :=
6737 Make_Case_Statement (Loc,
6738 Expression =>
6739 Make_Selected_Component (Loc,
6740 Prefix => Make_Identifier (Loc, Name_V),
6741 Selector_Name =>
6742 Make_Identifier (Loc,
6743 Chars => Chars (Name (Variant_Part (Comps))))),
6744 Alternatives => Var_Alts);
6745 end;
6746 end if;
6748 -- Add the variant case statement to the list of statements
6750 if Present (Var_Case) then
6751 Append_To (Stmts, Var_Case);
6752 end if;
6754 -- If the component list did not have any controlled components
6755 -- nor variants, return null.
6757 if Is_Empty_List (Stmts) then
6758 Append_To (Stmts, Make_Null_Statement (Loc));
6759 end if;
6761 return Stmts;
6762 end Process_Component_List_For_Adjust;
6764 -- Local variables
6766 Bod_Stmts : List_Id := No_List;
6767 Finalizer_Decls : List_Id := No_List;
6768 Rec_Def : Node_Id;
6770 -- Start of processing for Build_Adjust_Statements
6772 begin
6773 Finalizer_Decls := New_List;
6774 Build_Object_Declarations (Finalizer_Data, Finalizer_Decls, Loc);
6776 if Nkind (Typ_Def) = N_Derived_Type_Definition then
6777 Rec_Def := Record_Extension_Part (Typ_Def);
6778 else
6779 Rec_Def := Typ_Def;
6780 end if;
6782 -- Create an adjust sequence for all record components
6784 if Present (Component_List (Rec_Def)) then
6785 Bod_Stmts :=
6786 Process_Component_List_For_Adjust (Component_List (Rec_Def));
6787 end if;
6789 -- A derived record type must adjust all inherited components. This
6790 -- action poses the following problem:
6792 -- procedure Deep_Adjust (Obj : in out Parent_Typ) is
6793 -- begin
6794 -- Adjust (Obj);
6795 -- ...
6797 -- procedure Deep_Adjust (Obj : in out Derived_Typ) is
6798 -- begin
6799 -- Deep_Adjust (Obj._parent);
6800 -- ...
6801 -- Adjust (Obj);
6802 -- ...
6804 -- Adjusting the derived type will invoke Adjust of the parent and
6805 -- then that of the derived type. This is undesirable because both
6806 -- routines may modify shared components. Only the Adjust of the
6807 -- derived type should be invoked.
6809 -- To prevent this double adjustment of shared components,
6810 -- Deep_Adjust uses a flag to control the invocation of Adjust:
6812 -- procedure Deep_Adjust
6813 -- (Obj : in out Some_Type;
6814 -- Flag : Boolean := True)
6815 -- is
6816 -- begin
6817 -- if Flag then
6818 -- Adjust (Obj);
6819 -- end if;
6820 -- ...
6822 -- When Deep_Adjust is invokes for field _parent, a value of False is
6823 -- provided for the flag:
6825 -- Deep_Adjust (Obj._parent, False);
6827 if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then
6828 declare
6829 Par_Typ : constant Entity_Id := Parent_Field_Type (Typ);
6830 Adj_Stmt : Node_Id;
6831 Call : Node_Id;
6833 begin
6834 if Needs_Finalization (Par_Typ) then
6835 Call :=
6836 Make_Adjust_Call
6837 (Obj_Ref =>
6838 Make_Selected_Component (Loc,
6839 Prefix => Make_Identifier (Loc, Name_V),
6840 Selector_Name =>
6841 Make_Identifier (Loc, Name_uParent)),
6842 Typ => Par_Typ,
6843 Skip_Self => True);
6845 -- Generate:
6846 -- begin
6847 -- Deep_Adjust (V._parent, False);
6849 -- exception
6850 -- when Id : others =>
6851 -- if not Raised then
6852 -- Raised := True;
6853 -- Save_Occurrence (E,
6854 -- Get_Current_Excep.all.all);
6855 -- end if;
6856 -- end;
6858 if Present (Call) then
6859 Adj_Stmt := Call;
6861 if Exceptions_OK then
6862 Adj_Stmt :=
6863 Make_Block_Statement (Loc,
6864 Handled_Statement_Sequence =>
6865 Make_Handled_Sequence_Of_Statements (Loc,
6866 Statements => New_List (Adj_Stmt),
6867 Exception_Handlers => New_List (
6868 Build_Exception_Handler (Finalizer_Data))));
6869 end if;
6871 Prepend_To (Bod_Stmts, Adj_Stmt);
6872 end if;
6873 end if;
6874 end;
6875 end if;
6877 -- Adjust the object. This action must be performed last after all
6878 -- components have been adjusted.
6880 if Is_Controlled (Typ) then
6881 declare
6882 Adj_Stmt : Node_Id;
6883 Proc : Entity_Id;
6885 begin
6886 Proc := Find_Optional_Prim_Op (Typ, Name_Adjust);
6888 -- Generate:
6889 -- if F then
6890 -- begin
6891 -- Adjust (V);
6893 -- exception
6894 -- when others =>
6895 -- if not Raised then
6896 -- Raised := True;
6897 -- Save_Occurrence (E,
6898 -- Get_Current_Excep.all.all);
6899 -- end if;
6900 -- end;
6901 -- end if;
6903 if Present (Proc) then
6904 Adj_Stmt :=
6905 Make_Procedure_Call_Statement (Loc,
6906 Name => New_Occurrence_Of (Proc, Loc),
6907 Parameter_Associations => New_List (
6908 Make_Identifier (Loc, Name_V)));
6910 if Exceptions_OK then
6911 Adj_Stmt :=
6912 Make_Block_Statement (Loc,
6913 Handled_Statement_Sequence =>
6914 Make_Handled_Sequence_Of_Statements (Loc,
6915 Statements => New_List (Adj_Stmt),
6916 Exception_Handlers => New_List (
6917 Build_Exception_Handler
6918 (Finalizer_Data))));
6919 end if;
6921 Append_To (Bod_Stmts,
6922 Make_If_Statement (Loc,
6923 Condition => Make_Identifier (Loc, Name_F),
6924 Then_Statements => New_List (Adj_Stmt)));
6925 end if;
6926 end;
6927 end if;
6929 -- At this point either all adjustment statements have been generated
6930 -- or the type is not controlled.
6932 if Is_Empty_List (Bod_Stmts) then
6933 Append_To (Bod_Stmts, Make_Null_Statement (Loc));
6935 return Bod_Stmts;
6937 -- Generate:
6938 -- declare
6939 -- Abort : constant Boolean := Triggered_By_Abort;
6940 -- <or>
6941 -- Abort : constant Boolean := False; -- no abort
6943 -- E : Exception_Occurrence;
6944 -- Raised : Boolean := False;
6946 -- begin
6947 -- <adjust statements>
6949 -- if Raised and then not Abort then
6950 -- Raise_From_Controlled_Operation (E);
6951 -- end if;
6952 -- end;
6954 else
6955 if Exceptions_OK then
6956 Append_To (Bod_Stmts, Build_Raise_Statement (Finalizer_Data));
6957 end if;
6959 return
6960 New_List (
6961 Make_Block_Statement (Loc,
6962 Declarations =>
6963 Finalizer_Decls,
6964 Handled_Statement_Sequence =>
6965 Make_Handled_Sequence_Of_Statements (Loc, Bod_Stmts)));
6966 end if;
6967 end Build_Adjust_Statements;
6969 -------------------------------
6970 -- Build_Finalize_Statements --
6971 -------------------------------
6973 function Build_Finalize_Statements (Typ : Entity_Id) return List_Id is
6974 Loc : constant Source_Ptr := Sloc (Typ);
6975 Typ_Def : constant Node_Id := Type_Definition (Parent (Typ));
6977 Counter : Int := 0;
6978 Finalizer_Data : Finalization_Exception_Data;
6980 function Process_Component_List_For_Finalize
6981 (Comps : Node_Id) return List_Id;
6982 -- Build all necessary finalization statements for a single component
6983 -- list. The statements may include a jump circuitry if flag Is_Local
6984 -- is enabled.
6986 -----------------------------------------
6987 -- Process_Component_List_For_Finalize --
6988 -----------------------------------------
6990 function Process_Component_List_For_Finalize
6991 (Comps : Node_Id) return List_Id
6993 procedure Process_Component_For_Finalize
6994 (Decl : Node_Id;
6995 Alts : List_Id;
6996 Decls : List_Id;
6997 Stmts : List_Id;
6998 Num_Comps : in out Nat);
6999 -- Process the declaration of a single controlled component. If
7000 -- flag Is_Local is enabled, create the corresponding label and
7001 -- jump circuitry. Alts is the list of case alternatives, Decls
7002 -- is the top level declaration list where labels are declared
7003 -- and Stmts is the list of finalization actions. Num_Comps
7004 -- denotes the current number of components needing finalization.
7006 ------------------------------------
7007 -- Process_Component_For_Finalize --
7008 ------------------------------------
7010 procedure Process_Component_For_Finalize
7011 (Decl : Node_Id;
7012 Alts : List_Id;
7013 Decls : List_Id;
7014 Stmts : List_Id;
7015 Num_Comps : in out Nat)
7017 Id : constant Entity_Id := Defining_Identifier (Decl);
7018 Typ : constant Entity_Id := Etype (Id);
7019 Fin_Call : Node_Id;
7021 begin
7022 if Is_Local then
7023 declare
7024 Label : Node_Id;
7025 Label_Id : Entity_Id;
7027 begin
7028 -- Generate:
7029 -- LN : label;
7031 Label_Id :=
7032 Make_Identifier (Loc,
7033 Chars => New_External_Name ('L', Num_Comps));
7034 Set_Entity (Label_Id,
7035 Make_Defining_Identifier (Loc, Chars (Label_Id)));
7036 Label := Make_Label (Loc, Label_Id);
7038 Append_To (Decls,
7039 Make_Implicit_Label_Declaration (Loc,
7040 Defining_Identifier => Entity (Label_Id),
7041 Label_Construct => Label));
7043 -- Generate:
7044 -- when N =>
7045 -- goto LN;
7047 Append_To (Alts,
7048 Make_Case_Statement_Alternative (Loc,
7049 Discrete_Choices => New_List (
7050 Make_Integer_Literal (Loc, Num_Comps)),
7052 Statements => New_List (
7053 Make_Goto_Statement (Loc,
7054 Name =>
7055 New_Occurrence_Of (Entity (Label_Id), Loc)))));
7057 -- Generate:
7058 -- <<LN>>
7060 Append_To (Stmts, Label);
7062 -- Decrease the number of components to be processed.
7063 -- This action yields a new Label_Id in future calls.
7065 Num_Comps := Num_Comps - 1;
7066 end;
7067 end if;
7069 -- Generate:
7070 -- [Deep_]Finalize (V.Id); -- No_Exception_Propagation
7072 -- begin -- Exception handlers allowed
7073 -- [Deep_]Finalize (V.Id);
7074 -- exception
7075 -- when others =>
7076 -- if not Raised then
7077 -- Raised := True;
7078 -- Save_Occurrence (E,
7079 -- Get_Current_Excep.all.all);
7080 -- end if;
7081 -- end;
7083 Fin_Call :=
7084 Make_Final_Call
7085 (Obj_Ref =>
7086 Make_Selected_Component (Loc,
7087 Prefix => Make_Identifier (Loc, Name_V),
7088 Selector_Name => Make_Identifier (Loc, Chars (Id))),
7089 Typ => Typ);
7091 -- Guard against a missing [Deep_]Finalize when the component
7092 -- type was not properly frozen.
7094 if Present (Fin_Call) then
7095 if Exceptions_OK then
7096 Fin_Call :=
7097 Make_Block_Statement (Loc,
7098 Handled_Statement_Sequence =>
7099 Make_Handled_Sequence_Of_Statements (Loc,
7100 Statements => New_List (Fin_Call),
7101 Exception_Handlers => New_List (
7102 Build_Exception_Handler (Finalizer_Data))));
7103 end if;
7105 Append_To (Stmts, Fin_Call);
7106 end if;
7107 end Process_Component_For_Finalize;
7109 -- Local variables
7111 Alts : List_Id;
7112 Counter_Id : Entity_Id := Empty;
7113 Decl : Node_Id;
7114 Decl_Id : Entity_Id;
7115 Decl_Typ : Entity_Id;
7116 Decls : List_Id;
7117 Has_POC : Boolean;
7118 Jump_Block : Node_Id;
7119 Label : Node_Id;
7120 Label_Id : Entity_Id;
7121 Num_Comps : Nat;
7122 Stmts : List_Id;
7123 Var_Case : Node_Id;
7125 -- Start of processing for Process_Component_List_For_Finalize
7127 begin
7128 -- Perform an initial check, look for controlled and per-object
7129 -- constrained components.
7131 Preprocess_Components (Comps, Num_Comps, Has_POC);
7133 -- Create a state counter to service the current component list.
7134 -- This step is performed before the variants are inspected in
7135 -- order to generate the same state counter names as those from
7136 -- Build_Initialize_Statements.
7138 if Num_Comps > 0 and then Is_Local then
7139 Counter := Counter + 1;
7141 Counter_Id :=
7142 Make_Defining_Identifier (Loc,
7143 Chars => New_External_Name ('C', Counter));
7144 end if;
7146 -- Process the component in the following order:
7147 -- 1) Variants
7148 -- 2) Per-object constrained components
7149 -- 3) Regular components
7151 -- Start with the variant parts
7153 Var_Case := Empty;
7154 if Present (Variant_Part (Comps)) then
7155 declare
7156 Var_Alts : constant List_Id := New_List;
7157 Var : Node_Id;
7159 begin
7160 Var := First_Non_Pragma (Variants (Variant_Part (Comps)));
7161 while Present (Var) loop
7163 -- Generate:
7164 -- when <discrete choices> =>
7165 -- <finalize statements>
7167 Append_To (Var_Alts,
7168 Make_Case_Statement_Alternative (Loc,
7169 Discrete_Choices =>
7170 New_Copy_List (Discrete_Choices (Var)),
7171 Statements =>
7172 Process_Component_List_For_Finalize (
7173 Component_List (Var))));
7175 Next_Non_Pragma (Var);
7176 end loop;
7178 -- Generate:
7179 -- case V.<discriminant> is
7180 -- when <discrete choices 1> =>
7181 -- <finalize statements 1>
7182 -- ...
7183 -- when <discrete choices N> =>
7184 -- <finalize statements N>
7185 -- end case;
7187 Var_Case :=
7188 Make_Case_Statement (Loc,
7189 Expression =>
7190 Make_Selected_Component (Loc,
7191 Prefix => Make_Identifier (Loc, Name_V),
7192 Selector_Name =>
7193 Make_Identifier (Loc,
7194 Chars => Chars (Name (Variant_Part (Comps))))),
7195 Alternatives => Var_Alts);
7196 end;
7197 end if;
7199 -- The current component list does not have a single controlled
7200 -- component, however it may contain variants. Return the case
7201 -- statement for the variants or nothing.
7203 if Num_Comps = 0 then
7204 if Present (Var_Case) then
7205 return New_List (Var_Case);
7206 else
7207 return New_List (Make_Null_Statement (Loc));
7208 end if;
7209 end if;
7211 -- Prepare all lists
7213 Alts := New_List;
7214 Decls := New_List;
7215 Stmts := New_List;
7217 -- Process all per-object constrained components in reverse order
7219 if Has_POC then
7220 Decl := Last_Non_Pragma (Component_Items (Comps));
7221 while Present (Decl) loop
7222 Decl_Id := Defining_Identifier (Decl);
7223 Decl_Typ := Etype (Decl_Id);
7225 -- Skip _parent
7227 if Chars (Decl_Id) /= Name_uParent
7228 and then Needs_Finalization (Decl_Typ)
7229 and then Has_Access_Constraint (Decl_Id)
7230 and then No (Expression (Decl))
7231 then
7232 Process_Component_For_Finalize
7233 (Decl, Alts, Decls, Stmts, Num_Comps);
7234 end if;
7236 Prev_Non_Pragma (Decl);
7237 end loop;
7238 end if;
7240 -- Process the rest of the components in reverse order
7242 Decl := Last_Non_Pragma (Component_Items (Comps));
7243 while Present (Decl) loop
7244 Decl_Id := Defining_Identifier (Decl);
7245 Decl_Typ := Etype (Decl_Id);
7247 -- Skip _parent
7249 if Chars (Decl_Id) /= Name_uParent
7250 and then Needs_Finalization (Decl_Typ)
7251 then
7252 -- Skip per-object constrained components since they were
7253 -- handled in the above step.
7255 if Has_Access_Constraint (Decl_Id)
7256 and then No (Expression (Decl))
7257 then
7258 null;
7259 else
7260 Process_Component_For_Finalize
7261 (Decl, Alts, Decls, Stmts, Num_Comps);
7262 end if;
7263 end if;
7265 Prev_Non_Pragma (Decl);
7266 end loop;
7268 -- Generate:
7269 -- declare
7270 -- LN : label; -- If Is_Local is enabled
7271 -- ... .
7272 -- L0 : label; .
7274 -- begin .
7275 -- case CounterX is .
7276 -- when N => .
7277 -- goto LN; .
7278 -- ... .
7279 -- when 1 => .
7280 -- goto L1; .
7281 -- when others => .
7282 -- goto L0; .
7283 -- end case; .
7285 -- <<LN>> -- If Is_Local is enabled
7286 -- begin
7287 -- [Deep_]Finalize (V.CompY);
7288 -- exception
7289 -- when Id : others =>
7290 -- if not Raised then
7291 -- Raised := True;
7292 -- Save_Occurrence (E,
7293 -- Get_Current_Excep.all.all);
7294 -- end if;
7295 -- end;
7296 -- ...
7297 -- <<L0>> -- If Is_Local is enabled
7298 -- end;
7300 if Is_Local then
7302 -- Add the declaration of default jump location L0, its
7303 -- corresponding alternative and its place in the statements.
7305 Label_Id := Make_Identifier (Loc, New_External_Name ('L', 0));
7306 Set_Entity (Label_Id,
7307 Make_Defining_Identifier (Loc, Chars (Label_Id)));
7308 Label := Make_Label (Loc, Label_Id);
7310 Append_To (Decls, -- declaration
7311 Make_Implicit_Label_Declaration (Loc,
7312 Defining_Identifier => Entity (Label_Id),
7313 Label_Construct => Label));
7315 Append_To (Alts, -- alternative
7316 Make_Case_Statement_Alternative (Loc,
7317 Discrete_Choices => New_List (
7318 Make_Others_Choice (Loc)),
7320 Statements => New_List (
7321 Make_Goto_Statement (Loc,
7322 Name => New_Occurrence_Of (Entity (Label_Id), Loc)))));
7324 Append_To (Stmts, Label); -- statement
7326 -- Create the jump block
7328 Prepend_To (Stmts,
7329 Make_Case_Statement (Loc,
7330 Expression => Make_Identifier (Loc, Chars (Counter_Id)),
7331 Alternatives => Alts));
7332 end if;
7334 Jump_Block :=
7335 Make_Block_Statement (Loc,
7336 Declarations => Decls,
7337 Handled_Statement_Sequence =>
7338 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
7340 if Present (Var_Case) then
7341 return New_List (Var_Case, Jump_Block);
7342 else
7343 return New_List (Jump_Block);
7344 end if;
7345 end Process_Component_List_For_Finalize;
7347 -- Local variables
7349 Bod_Stmts : List_Id := No_List;
7350 Finalizer_Decls : List_Id := No_List;
7351 Rec_Def : Node_Id;
7353 -- Start of processing for Build_Finalize_Statements
7355 begin
7356 Finalizer_Decls := New_List;
7357 Build_Object_Declarations (Finalizer_Data, Finalizer_Decls, Loc);
7359 if Nkind (Typ_Def) = N_Derived_Type_Definition then
7360 Rec_Def := Record_Extension_Part (Typ_Def);
7361 else
7362 Rec_Def := Typ_Def;
7363 end if;
7365 -- Create a finalization sequence for all record components
7367 if Present (Component_List (Rec_Def)) then
7368 Bod_Stmts :=
7369 Process_Component_List_For_Finalize (Component_List (Rec_Def));
7370 end if;
7372 -- A derived record type must finalize all inherited components. This
7373 -- action poses the following problem:
7375 -- procedure Deep_Finalize (Obj : in out Parent_Typ) is
7376 -- begin
7377 -- Finalize (Obj);
7378 -- ...
7380 -- procedure Deep_Finalize (Obj : in out Derived_Typ) is
7381 -- begin
7382 -- Deep_Finalize (Obj._parent);
7383 -- ...
7384 -- Finalize (Obj);
7385 -- ...
7387 -- Finalizing the derived type will invoke Finalize of the parent and
7388 -- then that of the derived type. This is undesirable because both
7389 -- routines may modify shared components. Only the Finalize of the
7390 -- derived type should be invoked.
7392 -- To prevent this double adjustment of shared components,
7393 -- Deep_Finalize uses a flag to control the invocation of Finalize:
7395 -- procedure Deep_Finalize
7396 -- (Obj : in out Some_Type;
7397 -- Flag : Boolean := True)
7398 -- is
7399 -- begin
7400 -- if Flag then
7401 -- Finalize (Obj);
7402 -- end if;
7403 -- ...
7405 -- When Deep_Finalize is invoked for field _parent, a value of False
7406 -- is provided for the flag:
7408 -- Deep_Finalize (Obj._parent, False);
7410 if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then
7411 declare
7412 Par_Typ : constant Entity_Id := Parent_Field_Type (Typ);
7413 Call : Node_Id;
7414 Fin_Stmt : Node_Id;
7416 begin
7417 if Needs_Finalization (Par_Typ) then
7418 Call :=
7419 Make_Final_Call
7420 (Obj_Ref =>
7421 Make_Selected_Component (Loc,
7422 Prefix => Make_Identifier (Loc, Name_V),
7423 Selector_Name =>
7424 Make_Identifier (Loc, Name_uParent)),
7425 Typ => Par_Typ,
7426 Skip_Self => True);
7428 -- Generate:
7429 -- begin
7430 -- Deep_Finalize (V._parent, False);
7432 -- exception
7433 -- when Id : others =>
7434 -- if not Raised then
7435 -- Raised := True;
7436 -- Save_Occurrence (E,
7437 -- Get_Current_Excep.all.all);
7438 -- end if;
7439 -- end;
7441 if Present (Call) then
7442 Fin_Stmt := Call;
7444 if Exceptions_OK then
7445 Fin_Stmt :=
7446 Make_Block_Statement (Loc,
7447 Handled_Statement_Sequence =>
7448 Make_Handled_Sequence_Of_Statements (Loc,
7449 Statements => New_List (Fin_Stmt),
7450 Exception_Handlers => New_List (
7451 Build_Exception_Handler
7452 (Finalizer_Data))));
7453 end if;
7455 Append_To (Bod_Stmts, Fin_Stmt);
7456 end if;
7457 end if;
7458 end;
7459 end if;
7461 -- Finalize the object. This action must be performed first before
7462 -- all components have been finalized.
7464 if Is_Controlled (Typ) and then not Is_Local then
7465 declare
7466 Fin_Stmt : Node_Id;
7467 Proc : Entity_Id;
7469 begin
7470 Proc := Find_Optional_Prim_Op (Typ, Name_Finalize);
7472 -- Generate:
7473 -- if F then
7474 -- begin
7475 -- Finalize (V);
7477 -- exception
7478 -- when others =>
7479 -- if not Raised then
7480 -- Raised := True;
7481 -- Save_Occurrence (E,
7482 -- Get_Current_Excep.all.all);
7483 -- end if;
7484 -- end;
7485 -- end if;
7487 if Present (Proc) then
7488 Fin_Stmt :=
7489 Make_Procedure_Call_Statement (Loc,
7490 Name => New_Occurrence_Of (Proc, Loc),
7491 Parameter_Associations => New_List (
7492 Make_Identifier (Loc, Name_V)));
7494 if Exceptions_OK then
7495 Fin_Stmt :=
7496 Make_Block_Statement (Loc,
7497 Handled_Statement_Sequence =>
7498 Make_Handled_Sequence_Of_Statements (Loc,
7499 Statements => New_List (Fin_Stmt),
7500 Exception_Handlers => New_List (
7501 Build_Exception_Handler
7502 (Finalizer_Data))));
7503 end if;
7505 Prepend_To (Bod_Stmts,
7506 Make_If_Statement (Loc,
7507 Condition => Make_Identifier (Loc, Name_F),
7508 Then_Statements => New_List (Fin_Stmt)));
7509 end if;
7510 end;
7511 end if;
7513 -- At this point either all finalization statements have been
7514 -- generated or the type is not controlled.
7516 if No (Bod_Stmts) then
7517 return New_List (Make_Null_Statement (Loc));
7519 -- Generate:
7520 -- declare
7521 -- Abort : constant Boolean := Triggered_By_Abort;
7522 -- <or>
7523 -- Abort : constant Boolean := False; -- no abort
7525 -- E : Exception_Occurrence;
7526 -- Raised : Boolean := False;
7528 -- begin
7529 -- <finalize statements>
7531 -- if Raised and then not Abort then
7532 -- Raise_From_Controlled_Operation (E);
7533 -- end if;
7534 -- end;
7536 else
7537 if Exceptions_OK then
7538 Append_To (Bod_Stmts, Build_Raise_Statement (Finalizer_Data));
7539 end if;
7541 return
7542 New_List (
7543 Make_Block_Statement (Loc,
7544 Declarations =>
7545 Finalizer_Decls,
7546 Handled_Statement_Sequence =>
7547 Make_Handled_Sequence_Of_Statements (Loc, Bod_Stmts)));
7548 end if;
7549 end Build_Finalize_Statements;
7551 -----------------------
7552 -- Parent_Field_Type --
7553 -----------------------
7555 function Parent_Field_Type (Typ : Entity_Id) return Entity_Id is
7556 Field : Entity_Id;
7558 begin
7559 Field := First_Entity (Typ);
7560 while Present (Field) loop
7561 if Chars (Field) = Name_uParent then
7562 return Etype (Field);
7563 end if;
7565 Next_Entity (Field);
7566 end loop;
7568 -- A derived tagged type should always have a parent field
7570 raise Program_Error;
7571 end Parent_Field_Type;
7573 ---------------------------
7574 -- Preprocess_Components --
7575 ---------------------------
7577 procedure Preprocess_Components
7578 (Comps : Node_Id;
7579 Num_Comps : out Nat;
7580 Has_POC : out Boolean)
7582 Decl : Node_Id;
7583 Id : Entity_Id;
7584 Typ : Entity_Id;
7586 begin
7587 Num_Comps := 0;
7588 Has_POC := False;
7590 Decl := First_Non_Pragma (Component_Items (Comps));
7591 while Present (Decl) loop
7592 Id := Defining_Identifier (Decl);
7593 Typ := Etype (Id);
7595 -- Skip field _parent
7597 if Chars (Id) /= Name_uParent
7598 and then Needs_Finalization (Typ)
7599 then
7600 Num_Comps := Num_Comps + 1;
7602 if Has_Access_Constraint (Id)
7603 and then No (Expression (Decl))
7604 then
7605 Has_POC := True;
7606 end if;
7607 end if;
7609 Next_Non_Pragma (Decl);
7610 end loop;
7611 end Preprocess_Components;
7613 -- Start of processing for Make_Deep_Record_Body
7615 begin
7616 case Prim is
7617 when Address_Case =>
7618 return Make_Finalize_Address_Stmts (Typ);
7620 when Adjust_Case =>
7621 return Build_Adjust_Statements (Typ);
7623 when Finalize_Case =>
7624 return Build_Finalize_Statements (Typ);
7626 when Initialize_Case =>
7627 declare
7628 Loc : constant Source_Ptr := Sloc (Typ);
7630 begin
7631 if Is_Controlled (Typ) then
7632 return New_List (
7633 Make_Procedure_Call_Statement (Loc,
7634 Name =>
7635 New_Occurrence_Of
7636 (Find_Prim_Op (Typ, Name_Of (Prim)), Loc),
7637 Parameter_Associations => New_List (
7638 Make_Identifier (Loc, Name_V))));
7639 else
7640 return Empty_List;
7641 end if;
7642 end;
7643 end case;
7644 end Make_Deep_Record_Body;
7646 ----------------------
7647 -- Make_Final_Call --
7648 ----------------------
7650 function Make_Final_Call
7651 (Obj_Ref : Node_Id;
7652 Typ : Entity_Id;
7653 Skip_Self : Boolean := False) return Node_Id
7655 Loc : constant Source_Ptr := Sloc (Obj_Ref);
7656 Atyp : Entity_Id;
7657 Fin_Id : Entity_Id := Empty;
7658 Ref : Node_Id;
7659 Utyp : Entity_Id;
7661 begin
7662 Ref := Obj_Ref;
7664 -- Recover the proper type which contains [Deep_]Finalize
7666 if Is_Class_Wide_Type (Typ) then
7667 Utyp := Root_Type (Typ);
7668 Atyp := Utyp;
7670 elsif Is_Concurrent_Type (Typ) then
7671 Utyp := Corresponding_Record_Type (Typ);
7672 Atyp := Empty;
7673 Ref := Convert_Concurrent (Ref, Typ);
7675 elsif Is_Private_Type (Typ)
7676 and then Present (Full_View (Typ))
7677 and then Is_Concurrent_Type (Full_View (Typ))
7678 then
7679 Utyp := Corresponding_Record_Type (Full_View (Typ));
7680 Atyp := Typ;
7681 Ref := Convert_Concurrent (Ref, Full_View (Typ));
7683 else
7684 Utyp := Typ;
7685 Atyp := Typ;
7686 end if;
7688 Utyp := Underlying_Type (Base_Type (Utyp));
7689 Set_Assignment_OK (Ref);
7691 -- Deal with untagged derivation of private views. If the parent type
7692 -- is a protected type, Deep_Finalize is found on the corresponding
7693 -- record of the ancestor.
7695 if Is_Untagged_Derivation (Typ) then
7696 if Is_Protected_Type (Typ) then
7697 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
7698 else
7699 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
7701 if Is_Protected_Type (Utyp) then
7702 Utyp := Corresponding_Record_Type (Utyp);
7703 end if;
7704 end if;
7706 Ref := Unchecked_Convert_To (Utyp, Ref);
7707 Set_Assignment_OK (Ref);
7708 end if;
7710 -- Deal with derived private types which do not inherit primitives from
7711 -- their parents. In this case, [Deep_]Finalize can be found in the full
7712 -- view of the parent type.
7714 if Present (Utyp)
7715 and then Is_Tagged_Type (Utyp)
7716 and then Is_Derived_Type (Utyp)
7717 and then Is_Empty_Elmt_List (Primitive_Operations (Utyp))
7718 and then Is_Private_Type (Etype (Utyp))
7719 and then Present (Full_View (Etype (Utyp)))
7720 then
7721 Utyp := Full_View (Etype (Utyp));
7722 Ref := Unchecked_Convert_To (Utyp, Ref);
7723 Set_Assignment_OK (Ref);
7724 end if;
7726 -- When dealing with the completion of a private type, use the base type
7727 -- instead.
7729 if Present (Utyp) and then Utyp /= Base_Type (Utyp) then
7730 pragma Assert (Present (Atyp) and then Is_Private_Type (Atyp));
7732 Utyp := Base_Type (Utyp);
7733 Ref := Unchecked_Convert_To (Utyp, Ref);
7734 Set_Assignment_OK (Ref);
7735 end if;
7737 -- The underlying type may not be present due to a missing full view. In
7738 -- this case freezing did not take place and there is no [Deep_]Finalize
7739 -- primitive to call.
7741 if No (Utyp) then
7742 return Empty;
7744 elsif Skip_Self then
7745 if Has_Controlled_Component (Utyp) then
7746 if Is_Tagged_Type (Utyp) then
7747 Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize);
7748 else
7749 Fin_Id := TSS (Utyp, TSS_Deep_Finalize);
7750 end if;
7751 end if;
7753 -- Class-wide types, interfaces and types with controlled components
7755 elsif Is_Class_Wide_Type (Typ)
7756 or else Is_Interface (Typ)
7757 or else Has_Controlled_Component (Utyp)
7758 then
7759 if Is_Tagged_Type (Utyp) then
7760 Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize);
7761 else
7762 Fin_Id := TSS (Utyp, TSS_Deep_Finalize);
7763 end if;
7765 -- Derivations from [Limited_]Controlled
7767 elsif Is_Controlled (Utyp) then
7768 if Has_Controlled_Component (Utyp) then
7769 Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize);
7770 else
7771 Fin_Id := Find_Optional_Prim_Op (Utyp, Name_Of (Finalize_Case));
7772 end if;
7774 -- Tagged types
7776 elsif Is_Tagged_Type (Utyp) then
7777 Fin_Id := Find_Optional_Prim_Op (Utyp, TSS_Deep_Finalize);
7779 else
7780 raise Program_Error;
7781 end if;
7783 if Present (Fin_Id) then
7785 -- When finalizing a class-wide object, do not convert to the root
7786 -- type in order to produce a dispatching call.
7788 if Is_Class_Wide_Type (Typ) then
7789 null;
7791 -- Ensure that a finalization routine is at least decorated in order
7792 -- to inspect the object parameter.
7794 elsif Analyzed (Fin_Id)
7795 or else Ekind (Fin_Id) = E_Procedure
7796 then
7797 -- In certain cases, such as the creation of Stream_Read, the
7798 -- visible entity of the type is its full view. Since Stream_Read
7799 -- will have to create an object of type Typ, the local object
7800 -- will be finalzed by the scope finalizer generated later on. The
7801 -- object parameter of Deep_Finalize will always use the private
7802 -- view of the type. To avoid such a clash between a private and a
7803 -- full view, perform an unchecked conversion of the object
7804 -- reference to the private view.
7806 declare
7807 Formal_Typ : constant Entity_Id :=
7808 Etype (First_Formal (Fin_Id));
7809 begin
7810 if Is_Private_Type (Formal_Typ)
7811 and then Present (Full_View (Formal_Typ))
7812 and then Full_View (Formal_Typ) = Utyp
7813 then
7814 Ref := Unchecked_Convert_To (Formal_Typ, Ref);
7815 end if;
7816 end;
7818 Ref := Convert_View (Fin_Id, Ref);
7819 end if;
7821 return
7822 Make_Call (Loc,
7823 Proc_Id => Fin_Id,
7824 Param => Ref,
7825 Skip_Self => Skip_Self);
7826 else
7827 return Empty;
7828 end if;
7829 end Make_Final_Call;
7831 --------------------------------
7832 -- Make_Finalize_Address_Body --
7833 --------------------------------
7835 procedure Make_Finalize_Address_Body (Typ : Entity_Id) is
7836 Is_Task : constant Boolean :=
7837 Ekind (Typ) = E_Record_Type
7838 and then Is_Concurrent_Record_Type (Typ)
7839 and then Ekind (Corresponding_Concurrent_Type (Typ)) =
7840 E_Task_Type;
7841 Loc : constant Source_Ptr := Sloc (Typ);
7842 Proc_Id : Entity_Id;
7843 Stmts : List_Id;
7845 begin
7846 -- The corresponding records of task types are not controlled by design.
7847 -- For the sake of completeness, create an empty Finalize_Address to be
7848 -- used in task class-wide allocations.
7850 if Is_Task then
7851 null;
7853 -- Nothing to do if the type is not controlled or it already has a
7854 -- TSS entry for Finalize_Address. Skip class-wide subtypes which do not
7855 -- come from source. These are usually generated for completeness and
7856 -- do not need the Finalize_Address primitive.
7858 elsif not Needs_Finalization (Typ)
7859 or else Present (TSS (Typ, TSS_Finalize_Address))
7860 or else
7861 (Is_Class_Wide_Type (Typ)
7862 and then Ekind (Root_Type (Typ)) = E_Record_Subtype
7863 and then not Comes_From_Source (Root_Type (Typ)))
7864 then
7865 return;
7866 end if;
7868 -- Do not generate Finalize_Address routine for CodePeer
7870 if CodePeer_Mode then
7871 return;
7872 end if;
7874 Proc_Id :=
7875 Make_Defining_Identifier (Loc,
7876 Make_TSS_Name (Typ, TSS_Finalize_Address));
7878 -- Generate:
7880 -- procedure <Typ>FD (V : System.Address) is
7881 -- begin
7882 -- null; -- for tasks
7884 -- declare -- for all other types
7885 -- type Pnn is access all Typ;
7886 -- for Pnn'Storage_Size use 0;
7887 -- begin
7888 -- [Deep_]Finalize (Pnn (V).all);
7889 -- end;
7890 -- end TypFD;
7892 if Is_Task then
7893 Stmts := New_List (Make_Null_Statement (Loc));
7894 else
7895 Stmts := Make_Finalize_Address_Stmts (Typ);
7896 end if;
7898 Discard_Node (
7899 Make_Subprogram_Body (Loc,
7900 Specification =>
7901 Make_Procedure_Specification (Loc,
7902 Defining_Unit_Name => Proc_Id,
7904 Parameter_Specifications => New_List (
7905 Make_Parameter_Specification (Loc,
7906 Defining_Identifier =>
7907 Make_Defining_Identifier (Loc, Name_V),
7908 Parameter_Type =>
7909 New_Occurrence_Of (RTE (RE_Address), Loc)))),
7911 Declarations => No_List,
7913 Handled_Statement_Sequence =>
7914 Make_Handled_Sequence_Of_Statements (Loc,
7915 Statements => Stmts)));
7917 Set_TSS (Typ, Proc_Id);
7918 end Make_Finalize_Address_Body;
7920 ---------------------------------
7921 -- Make_Finalize_Address_Stmts --
7922 ---------------------------------
7924 function Make_Finalize_Address_Stmts (Typ : Entity_Id) return List_Id is
7925 Loc : constant Source_Ptr := Sloc (Typ);
7927 Decls : List_Id;
7928 Desig_Typ : Entity_Id;
7929 Fin_Block : Node_Id;
7930 Fin_Call : Node_Id;
7931 Obj_Expr : Node_Id;
7932 Ptr_Typ : Entity_Id;
7934 begin
7935 if Is_Array_Type (Typ) then
7936 if Is_Constrained (First_Subtype (Typ)) then
7937 Desig_Typ := First_Subtype (Typ);
7938 else
7939 Desig_Typ := Base_Type (Typ);
7940 end if;
7942 -- Class-wide types of constrained root types
7944 elsif Is_Class_Wide_Type (Typ)
7945 and then Has_Discriminants (Root_Type (Typ))
7946 and then not
7947 Is_Empty_Elmt_List (Discriminant_Constraint (Root_Type (Typ)))
7948 then
7949 declare
7950 Parent_Typ : Entity_Id;
7952 begin
7953 -- Climb the parent type chain looking for a non-constrained type
7955 Parent_Typ := Root_Type (Typ);
7956 while Parent_Typ /= Etype (Parent_Typ)
7957 and then Has_Discriminants (Parent_Typ)
7958 and then not
7959 Is_Empty_Elmt_List (Discriminant_Constraint (Parent_Typ))
7960 loop
7961 Parent_Typ := Etype (Parent_Typ);
7962 end loop;
7964 -- Handle views created for tagged types with unknown
7965 -- discriminants.
7967 if Is_Underlying_Record_View (Parent_Typ) then
7968 Parent_Typ := Underlying_Record_View (Parent_Typ);
7969 end if;
7971 Desig_Typ := Class_Wide_Type (Underlying_Type (Parent_Typ));
7972 end;
7974 -- General case
7976 else
7977 Desig_Typ := Typ;
7978 end if;
7980 -- Generate:
7981 -- type Ptr_Typ is access all Typ;
7982 -- for Ptr_Typ'Storage_Size use 0;
7984 Ptr_Typ := Make_Temporary (Loc, 'P');
7986 Decls := New_List (
7987 Make_Full_Type_Declaration (Loc,
7988 Defining_Identifier => Ptr_Typ,
7989 Type_Definition =>
7990 Make_Access_To_Object_Definition (Loc,
7991 All_Present => True,
7992 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc))),
7994 Make_Attribute_Definition_Clause (Loc,
7995 Name => New_Occurrence_Of (Ptr_Typ, Loc),
7996 Chars => Name_Storage_Size,
7997 Expression => Make_Integer_Literal (Loc, 0)));
7999 Obj_Expr := Make_Identifier (Loc, Name_V);
8001 -- Unconstrained arrays require special processing in order to retrieve
8002 -- the elements. To achieve this, we have to skip the dope vector which
8003 -- lays in front of the elements and then use a thin pointer to perform
8004 -- the address-to-access conversion.
8006 if Is_Array_Type (Typ)
8007 and then not Is_Constrained (First_Subtype (Typ))
8008 then
8009 declare
8010 Dope_Id : Entity_Id;
8012 begin
8013 -- Ensure that Ptr_Typ a thin pointer, generate:
8014 -- for Ptr_Typ'Size use System.Address'Size;
8016 Append_To (Decls,
8017 Make_Attribute_Definition_Clause (Loc,
8018 Name => New_Occurrence_Of (Ptr_Typ, Loc),
8019 Chars => Name_Size,
8020 Expression =>
8021 Make_Integer_Literal (Loc, System_Address_Size)));
8023 -- Generate:
8024 -- Dnn : constant Storage_Offset :=
8025 -- Desig_Typ'Descriptor_Size / Storage_Unit;
8027 Dope_Id := Make_Temporary (Loc, 'D');
8029 Append_To (Decls,
8030 Make_Object_Declaration (Loc,
8031 Defining_Identifier => Dope_Id,
8032 Constant_Present => True,
8033 Object_Definition =>
8034 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
8035 Expression =>
8036 Make_Op_Divide (Loc,
8037 Left_Opnd =>
8038 Make_Attribute_Reference (Loc,
8039 Prefix => New_Occurrence_Of (Desig_Typ, Loc),
8040 Attribute_Name => Name_Descriptor_Size),
8041 Right_Opnd =>
8042 Make_Integer_Literal (Loc, System_Storage_Unit))));
8044 -- Shift the address from the start of the dope vector to the
8045 -- start of the elements:
8047 -- V + Dnn
8049 -- Note that this is done through a wrapper routine since RTSfind
8050 -- cannot retrieve operations with string names of the form "+".
8052 Obj_Expr :=
8053 Make_Function_Call (Loc,
8054 Name =>
8055 New_Occurrence_Of (RTE (RE_Add_Offset_To_Address), Loc),
8056 Parameter_Associations => New_List (
8057 Obj_Expr,
8058 New_Occurrence_Of (Dope_Id, Loc)));
8059 end;
8060 end if;
8062 Fin_Call :=
8063 Make_Final_Call (
8064 Obj_Ref =>
8065 Make_Explicit_Dereference (Loc,
8066 Prefix => Unchecked_Convert_To (Ptr_Typ, Obj_Expr)),
8067 Typ => Desig_Typ);
8069 if Present (Fin_Call) then
8070 Fin_Block :=
8071 Make_Block_Statement (Loc,
8072 Declarations => Decls,
8073 Handled_Statement_Sequence =>
8074 Make_Handled_Sequence_Of_Statements (Loc,
8075 Statements => New_List (Fin_Call)));
8077 -- Otherwise previous errors or a missing full view may prevent the
8078 -- proper freezing of the designated type. If this is the case, there
8079 -- is no [Deep_]Finalize primitive to call.
8081 else
8082 Fin_Block := Make_Null_Statement (Loc);
8083 end if;
8085 return New_List (Fin_Block);
8086 end Make_Finalize_Address_Stmts;
8088 -------------------------------------
8089 -- Make_Handler_For_Ctrl_Operation --
8090 -------------------------------------
8092 -- Generate:
8094 -- when E : others =>
8095 -- Raise_From_Controlled_Operation (E);
8097 -- or:
8099 -- when others =>
8100 -- raise Program_Error [finalize raised exception];
8102 -- depending on whether Raise_From_Controlled_Operation is available
8104 function Make_Handler_For_Ctrl_Operation
8105 (Loc : Source_Ptr) return Node_Id
8107 E_Occ : Entity_Id;
8108 -- Choice parameter (for the first case above)
8110 Raise_Node : Node_Id;
8111 -- Procedure call or raise statement
8113 begin
8114 -- Standard run-time: add choice parameter E and pass it to
8115 -- Raise_From_Controlled_Operation so that the original exception
8116 -- name and message can be recorded in the exception message for
8117 -- Program_Error.
8119 if RTE_Available (RE_Raise_From_Controlled_Operation) then
8120 E_Occ := Make_Defining_Identifier (Loc, Name_E);
8121 Raise_Node :=
8122 Make_Procedure_Call_Statement (Loc,
8123 Name =>
8124 New_Occurrence_Of
8125 (RTE (RE_Raise_From_Controlled_Operation), Loc),
8126 Parameter_Associations => New_List (
8127 New_Occurrence_Of (E_Occ, Loc)));
8129 -- Restricted run-time: exception messages are not supported
8131 else
8132 E_Occ := Empty;
8133 Raise_Node :=
8134 Make_Raise_Program_Error (Loc,
8135 Reason => PE_Finalize_Raised_Exception);
8136 end if;
8138 return
8139 Make_Implicit_Exception_Handler (Loc,
8140 Exception_Choices => New_List (Make_Others_Choice (Loc)),
8141 Choice_Parameter => E_Occ,
8142 Statements => New_List (Raise_Node));
8143 end Make_Handler_For_Ctrl_Operation;
8145 --------------------
8146 -- Make_Init_Call --
8147 --------------------
8149 function Make_Init_Call
8150 (Obj_Ref : Node_Id;
8151 Typ : Entity_Id) return Node_Id
8153 Loc : constant Source_Ptr := Sloc (Obj_Ref);
8154 Is_Conc : Boolean;
8155 Proc : Entity_Id;
8156 Ref : Node_Id;
8157 Utyp : Entity_Id;
8159 begin
8160 Ref := Obj_Ref;
8162 -- Deal with the type and object reference. Depending on the context, an
8163 -- object reference may need several conversions.
8165 if Is_Concurrent_Type (Typ) then
8166 Is_Conc := True;
8167 Utyp := Corresponding_Record_Type (Typ);
8168 Ref := Convert_Concurrent (Ref, Typ);
8170 elsif Is_Private_Type (Typ)
8171 and then Present (Full_View (Typ))
8172 and then Is_Concurrent_Type (Underlying_Type (Typ))
8173 then
8174 Is_Conc := True;
8175 Utyp := Corresponding_Record_Type (Underlying_Type (Typ));
8176 Ref := Convert_Concurrent (Ref, Underlying_Type (Typ));
8178 else
8179 Is_Conc := False;
8180 Utyp := Typ;
8181 end if;
8183 Utyp := Underlying_Type (Base_Type (Utyp));
8184 Set_Assignment_OK (Ref);
8186 -- Deal with untagged derivation of private views
8188 if Is_Untagged_Derivation (Typ) and then not Is_Conc then
8189 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
8190 Ref := Unchecked_Convert_To (Utyp, Ref);
8192 -- The following is to prevent problems with UC see 1.156 RH ???
8194 Set_Assignment_OK (Ref);
8195 end if;
8197 -- If the underlying_type is a subtype, then we are dealing with the
8198 -- completion of a private type. We need to access the base type and
8199 -- generate a conversion to it.
8201 if Present (Utyp) and then Utyp /= Base_Type (Utyp) then
8202 pragma Assert (Is_Private_Type (Typ));
8203 Utyp := Base_Type (Utyp);
8204 Ref := Unchecked_Convert_To (Utyp, Ref);
8205 end if;
8207 -- The underlying type may not be present due to a missing full view.
8208 -- In this case freezing did not take place and there is no suitable
8209 -- [Deep_]Initialize primitive to call.
8211 if No (Utyp) then
8212 return Empty;
8213 end if;
8215 -- Select the appropriate version of initialize
8217 if Has_Controlled_Component (Utyp) then
8218 Proc := TSS (Utyp, Deep_Name_Of (Initialize_Case));
8219 else
8220 Proc := Find_Prim_Op (Utyp, Name_Of (Initialize_Case));
8221 Check_Visibly_Controlled (Initialize_Case, Typ, Proc, Ref);
8222 end if;
8224 -- If initialization procedure for an array of controlled objects is
8225 -- trivial, do not generate a useless call to it.
8227 if (Is_Array_Type (Utyp) and then Is_Trivial_Subprogram (Proc))
8228 or else
8229 (not Comes_From_Source (Proc)
8230 and then Present (Alias (Proc))
8231 and then Is_Trivial_Subprogram (Alias (Proc)))
8232 then
8233 return Make_Null_Statement (Loc);
8234 end if;
8236 -- The object reference may need another conversion depending on the
8237 -- type of the formal and that of the actual.
8239 Ref := Convert_View (Proc, Ref);
8241 -- Generate:
8242 -- [Deep_]Initialize (Ref);
8244 return
8245 Make_Procedure_Call_Statement (Loc,
8246 Name => New_Occurrence_Of (Proc, Loc),
8247 Parameter_Associations => New_List (Ref));
8248 end Make_Init_Call;
8250 ------------------------------
8251 -- Make_Local_Deep_Finalize --
8252 ------------------------------
8254 function Make_Local_Deep_Finalize
8255 (Typ : Entity_Id;
8256 Nam : Entity_Id) return Node_Id
8258 Loc : constant Source_Ptr := Sloc (Typ);
8259 Formals : List_Id;
8261 begin
8262 Formals := New_List (
8264 -- V : in out Typ
8266 Make_Parameter_Specification (Loc,
8267 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
8268 In_Present => True,
8269 Out_Present => True,
8270 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
8272 -- F : Boolean := True
8274 Make_Parameter_Specification (Loc,
8275 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
8276 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
8277 Expression => New_Occurrence_Of (Standard_True, Loc)));
8279 -- Add the necessary number of counters to represent the initialization
8280 -- state of an object.
8282 return
8283 Make_Subprogram_Body (Loc,
8284 Specification =>
8285 Make_Procedure_Specification (Loc,
8286 Defining_Unit_Name => Nam,
8287 Parameter_Specifications => Formals),
8289 Declarations => No_List,
8291 Handled_Statement_Sequence =>
8292 Make_Handled_Sequence_Of_Statements (Loc,
8293 Statements => Make_Deep_Record_Body (Finalize_Case, Typ, True)));
8294 end Make_Local_Deep_Finalize;
8296 ------------------------------------
8297 -- Make_Set_Finalize_Address_Call --
8298 ------------------------------------
8300 function Make_Set_Finalize_Address_Call
8301 (Loc : Source_Ptr;
8302 Ptr_Typ : Entity_Id) return Node_Id
8304 -- It is possible for Ptr_Typ to be a partial view, if the access type
8305 -- is a full view declared in the private part of a nested package, and
8306 -- the finalization actions take place when completing analysis of the
8307 -- enclosing unit. For this reason use Underlying_Type twice below.
8309 Desig_Typ : constant Entity_Id :=
8310 Available_View
8311 (Designated_Type (Underlying_Type (Ptr_Typ)));
8312 Fin_Addr : constant Entity_Id := Finalize_Address (Desig_Typ);
8313 Fin_Mas : constant Entity_Id :=
8314 Finalization_Master (Underlying_Type (Ptr_Typ));
8316 begin
8317 -- Both the finalization master and primitive Finalize_Address must be
8318 -- available.
8320 pragma Assert (Present (Fin_Addr) and Present (Fin_Mas));
8322 -- Generate:
8323 -- Set_Finalize_Address
8324 -- (<Ptr_Typ>FM, <Desig_Typ>FD'Unrestricted_Access);
8326 return
8327 Make_Procedure_Call_Statement (Loc,
8328 Name =>
8329 New_Occurrence_Of (RTE (RE_Set_Finalize_Address), Loc),
8330 Parameter_Associations => New_List (
8331 New_Occurrence_Of (Fin_Mas, Loc),
8333 Make_Attribute_Reference (Loc,
8334 Prefix => New_Occurrence_Of (Fin_Addr, Loc),
8335 Attribute_Name => Name_Unrestricted_Access)));
8336 end Make_Set_Finalize_Address_Call;
8338 --------------------------
8339 -- Make_Transient_Block --
8340 --------------------------
8342 function Make_Transient_Block
8343 (Loc : Source_Ptr;
8344 Action : Node_Id;
8345 Par : Node_Id) return Node_Id
8347 function Manages_Sec_Stack (Id : Entity_Id) return Boolean;
8348 -- Determine whether scoping entity Id manages the secondary stack
8350 -----------------------
8351 -- Manages_Sec_Stack --
8352 -----------------------
8354 function Manages_Sec_Stack (Id : Entity_Id) return Boolean is
8355 begin
8356 case Ekind (Id) is
8358 -- An exception handler with a choice parameter utilizes a dummy
8359 -- block to provide a declarative region. Such a block should not
8360 -- be considered because it never manifests in the tree and can
8361 -- never release the secondary stack.
8363 when E_Block =>
8364 return
8365 Uses_Sec_Stack (Id) and then not Is_Exception_Handler (Id);
8367 when E_Entry
8368 | E_Entry_Family
8369 | E_Function
8370 | E_Procedure
8372 return Uses_Sec_Stack (Id);
8374 when others =>
8375 return False;
8376 end case;
8377 end Manages_Sec_Stack;
8379 -- Local variables
8381 Decls : constant List_Id := New_List;
8382 Instrs : constant List_Id := New_List (Action);
8383 Trans_Id : constant Entity_Id := Current_Scope;
8385 Block : Node_Id;
8386 Insert : Node_Id;
8387 Scop : Entity_Id;
8389 -- Start of processing for Make_Transient_Block
8391 begin
8392 -- Even though the transient block is tasked with managing the secondary
8393 -- stack, the block may forgo this functionality depending on how the
8394 -- secondary stack is managed by enclosing scopes.
8396 if Manages_Sec_Stack (Trans_Id) then
8398 -- Determine whether an enclosing scope already manages the secondary
8399 -- stack.
8401 Scop := Scope (Trans_Id);
8402 while Present (Scop) loop
8404 -- It should not be possible to reach Standard without hitting one
8405 -- of the other cases first unless Standard was manually pushed.
8407 if Scop = Standard_Standard then
8408 exit;
8410 -- The transient block is within a function which returns on the
8411 -- secondary stack. Take a conservative approach and assume that
8412 -- the value on the secondary stack is part of the result. Note
8413 -- that it is not possible to detect this dependency without flow
8414 -- analysis which the compiler does not have. Letting the object
8415 -- live longer than the transient block will not leak any memory
8416 -- because the caller will reclaim the total storage used by the
8417 -- function.
8419 elsif Ekind (Scop) = E_Function
8420 and then Sec_Stack_Needed_For_Return (Scop)
8421 then
8422 Set_Uses_Sec_Stack (Trans_Id, False);
8423 exit;
8425 -- The transient block must manage the secondary stack when the
8426 -- block appears within a loop in order to reclaim the memory at
8427 -- each iteration.
8429 elsif Ekind (Scop) = E_Loop then
8430 exit;
8432 -- The transient block does not need to manage the secondary stack
8433 -- when there is an enclosing construct which already does that.
8434 -- This optimization saves on SS_Mark and SS_Release calls but may
8435 -- allow objects to live a little longer than required.
8437 -- The transient block must manage the secondary stack when switch
8438 -- -gnatd.s (strict management) is in effect.
8440 elsif Manages_Sec_Stack (Scop) and then not Debug_Flag_Dot_S then
8441 Set_Uses_Sec_Stack (Trans_Id, False);
8442 exit;
8444 -- Prevent the search from going too far because transient blocks
8445 -- are bounded by packages and subprogram scopes.
8447 elsif Ekind_In (Scop, E_Entry,
8448 E_Entry_Family,
8449 E_Function,
8450 E_Package,
8451 E_Procedure,
8452 E_Subprogram_Body)
8453 then
8454 exit;
8455 end if;
8457 Scop := Scope (Scop);
8458 end loop;
8459 end if;
8461 -- Create the transient block. Set the parent now since the block itself
8462 -- is not part of the tree. The current scope is the E_Block entity that
8463 -- has been pushed by Establish_Transient_Scope.
8465 pragma Assert (Ekind (Trans_Id) = E_Block);
8467 Block :=
8468 Make_Block_Statement (Loc,
8469 Identifier => New_Occurrence_Of (Trans_Id, Loc),
8470 Declarations => Decls,
8471 Handled_Statement_Sequence =>
8472 Make_Handled_Sequence_Of_Statements (Loc, Statements => Instrs),
8473 Has_Created_Identifier => True);
8474 Set_Parent (Block, Par);
8476 -- Insert actions stuck in the transient scopes as well as all freezing
8477 -- nodes needed by those actions. Do not insert cleanup actions here,
8478 -- they will be transferred to the newly created block.
8480 Insert_Actions_In_Scope_Around
8481 (Action, Clean => False, Manage_SS => False);
8483 Insert := Prev (Action);
8485 if Present (Insert) then
8486 Freeze_All (First_Entity (Trans_Id), Insert);
8487 end if;
8489 -- Transfer cleanup actions to the newly created block
8491 declare
8492 Cleanup_Actions : List_Id
8493 renames Scope_Stack.Table (Scope_Stack.Last).
8494 Actions_To_Be_Wrapped (Cleanup);
8495 begin
8496 Set_Cleanup_Actions (Block, Cleanup_Actions);
8497 Cleanup_Actions := No_List;
8498 end;
8500 -- When the transient scope was established, we pushed the entry for the
8501 -- transient scope onto the scope stack, so that the scope was active
8502 -- for the installation of finalizable entities etc. Now we must remove
8503 -- this entry, since we have constructed a proper block.
8505 Pop_Scope;
8507 return Block;
8508 end Make_Transient_Block;
8510 ------------------------
8511 -- Node_To_Be_Wrapped --
8512 ------------------------
8514 function Node_To_Be_Wrapped return Node_Id is
8515 begin
8516 return Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped;
8517 end Node_To_Be_Wrapped;
8519 ----------------------------
8520 -- Set_Node_To_Be_Wrapped --
8521 ----------------------------
8523 procedure Set_Node_To_Be_Wrapped (N : Node_Id) is
8524 begin
8525 Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped := N;
8526 end Set_Node_To_Be_Wrapped;
8528 ----------------------------
8529 -- Store_Actions_In_Scope --
8530 ----------------------------
8532 procedure Store_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id) is
8533 SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8534 Actions : List_Id renames SE.Actions_To_Be_Wrapped (AK);
8536 begin
8537 if No (Actions) then
8538 Actions := L;
8540 if Is_List_Member (SE.Node_To_Be_Wrapped) then
8541 Set_Parent (L, Parent (SE.Node_To_Be_Wrapped));
8542 else
8543 Set_Parent (L, SE.Node_To_Be_Wrapped);
8544 end if;
8546 Analyze_List (L);
8548 elsif AK = Before then
8549 Insert_List_After_And_Analyze (Last (Actions), L);
8551 else
8552 Insert_List_Before_And_Analyze (First (Actions), L);
8553 end if;
8554 end Store_Actions_In_Scope;
8556 ----------------------------------
8557 -- Store_After_Actions_In_Scope --
8558 ----------------------------------
8560 procedure Store_After_Actions_In_Scope (L : List_Id) is
8561 begin
8562 Store_Actions_In_Scope (After, L);
8563 end Store_After_Actions_In_Scope;
8565 -----------------------------------
8566 -- Store_Before_Actions_In_Scope --
8567 -----------------------------------
8569 procedure Store_Before_Actions_In_Scope (L : List_Id) is
8570 begin
8571 Store_Actions_In_Scope (Before, L);
8572 end Store_Before_Actions_In_Scope;
8574 -----------------------------------
8575 -- Store_Cleanup_Actions_In_Scope --
8576 -----------------------------------
8578 procedure Store_Cleanup_Actions_In_Scope (L : List_Id) is
8579 begin
8580 Store_Actions_In_Scope (Cleanup, L);
8581 end Store_Cleanup_Actions_In_Scope;
8583 --------------------------------
8584 -- Wrap_Transient_Declaration --
8585 --------------------------------
8587 -- If a transient scope has been established during the processing of the
8588 -- Expression of an Object_Declaration, it is not possible to wrap the
8589 -- declaration into a transient block as usual case, otherwise the object
8590 -- would be itself declared in the wrong scope. Therefore, all entities (if
8591 -- any) defined in the transient block are moved to the proper enclosing
8592 -- scope. Furthermore, if they are controlled variables they are finalized
8593 -- right after the declaration. The finalization list of the transient
8594 -- scope is defined as a renaming of the enclosing one so during their
8595 -- initialization they will be attached to the proper finalization list.
8596 -- For instance, the following declaration :
8598 -- X : Typ := F (G (A), G (B));
8600 -- (where G(A) and G(B) return controlled values, expanded as _v1 and _v2)
8601 -- is expanded into :
8603 -- X : Typ := [ complex Expression-Action ];
8604 -- [Deep_]Finalize (_v1);
8605 -- [Deep_]Finalize (_v2);
8607 procedure Wrap_Transient_Declaration (N : Node_Id) is
8608 Curr_S : Entity_Id;
8609 Encl_S : Entity_Id;
8611 begin
8612 Curr_S := Current_Scope;
8613 Encl_S := Scope (Curr_S);
8615 -- Insert all actions including cleanup generated while analyzing or
8616 -- expanding the transient context back into the tree. Manage the
8617 -- secondary stack when the object declaration appears in a library
8618 -- level package [body].
8620 Insert_Actions_In_Scope_Around
8621 (N => N,
8622 Clean => True,
8623 Manage_SS =>
8624 Uses_Sec_Stack (Curr_S)
8625 and then Nkind (N) = N_Object_Declaration
8626 and then Ekind_In (Encl_S, E_Package, E_Package_Body)
8627 and then Is_Library_Level_Entity (Encl_S));
8628 Pop_Scope;
8630 -- Relocate local entities declared within the transient scope to the
8631 -- enclosing scope. This action sets their Is_Public flag accordingly.
8633 Transfer_Entities (Curr_S, Encl_S);
8635 -- Mark the enclosing dynamic scope to ensure that the secondary stack
8636 -- is properly released upon exiting the said scope.
8638 if Uses_Sec_Stack (Curr_S) then
8639 Curr_S := Enclosing_Dynamic_Scope (Curr_S);
8641 -- Do not mark a function that returns on the secondary stack as the
8642 -- reclamation is done by the caller.
8644 if Ekind (Curr_S) = E_Function
8645 and then Requires_Transient_Scope (Etype (Curr_S))
8646 then
8647 null;
8649 -- Otherwise mark the enclosing dynamic scope
8651 else
8652 Set_Uses_Sec_Stack (Curr_S);
8653 Check_Restriction (No_Secondary_Stack, N);
8654 end if;
8655 end if;
8656 end Wrap_Transient_Declaration;
8658 -------------------------------
8659 -- Wrap_Transient_Expression --
8660 -------------------------------
8662 procedure Wrap_Transient_Expression (N : Node_Id) is
8663 Loc : constant Source_Ptr := Sloc (N);
8664 Expr : Node_Id := Relocate_Node (N);
8665 Temp : constant Entity_Id := Make_Temporary (Loc, 'E', N);
8666 Typ : constant Entity_Id := Etype (N);
8668 begin
8669 -- Generate:
8671 -- Temp : Typ;
8672 -- declare
8673 -- M : constant Mark_Id := SS_Mark;
8674 -- procedure Finalizer is ... (See Build_Finalizer)
8676 -- begin
8677 -- Temp := <Expr>; -- general case
8678 -- Temp := (if <Expr> then True else False); -- boolean case
8680 -- at end
8681 -- Finalizer;
8682 -- end;
8684 -- A special case is made for Boolean expressions so that the back-end
8685 -- knows to generate a conditional branch instruction, if running with
8686 -- -fpreserve-control-flow. This ensures that a control flow change
8687 -- signalling the decision outcome occurs before the cleanup actions.
8689 if Opt.Suppress_Control_Flow_Optimizations
8690 and then Is_Boolean_Type (Typ)
8691 then
8692 Expr :=
8693 Make_If_Expression (Loc,
8694 Expressions => New_List (
8695 Expr,
8696 New_Occurrence_Of (Standard_True, Loc),
8697 New_Occurrence_Of (Standard_False, Loc)));
8698 end if;
8700 Insert_Actions (N, New_List (
8701 Make_Object_Declaration (Loc,
8702 Defining_Identifier => Temp,
8703 Object_Definition => New_Occurrence_Of (Typ, Loc)),
8705 Make_Transient_Block (Loc,
8706 Action =>
8707 Make_Assignment_Statement (Loc,
8708 Name => New_Occurrence_Of (Temp, Loc),
8709 Expression => Expr),
8710 Par => Parent (N))));
8712 Rewrite (N, New_Occurrence_Of (Temp, Loc));
8713 Analyze_And_Resolve (N, Typ);
8714 end Wrap_Transient_Expression;
8716 ------------------------------
8717 -- Wrap_Transient_Statement --
8718 ------------------------------
8720 procedure Wrap_Transient_Statement (N : Node_Id) is
8721 Loc : constant Source_Ptr := Sloc (N);
8722 New_Stmt : constant Node_Id := Relocate_Node (N);
8724 begin
8725 -- Generate:
8726 -- declare
8727 -- M : constant Mark_Id := SS_Mark;
8728 -- procedure Finalizer is ... (See Build_Finalizer)
8730 -- begin
8731 -- <New_Stmt>;
8733 -- at end
8734 -- Finalizer;
8735 -- end;
8737 Rewrite (N,
8738 Make_Transient_Block (Loc,
8739 Action => New_Stmt,
8740 Par => Parent (N)));
8742 -- With the scope stack back to normal, we can call analyze on the
8743 -- resulting block. At this point, the transient scope is being
8744 -- treated like a perfectly normal scope, so there is nothing
8745 -- special about it.
8747 -- Note: Wrap_Transient_Statement is called with the node already
8748 -- analyzed (i.e. Analyzed (N) is True). This is important, since
8749 -- otherwise we would get a recursive processing of the node when
8750 -- we do this Analyze call.
8752 Analyze (N);
8753 end Wrap_Transient_Statement;
8755 end Exp_Ch7;