2009-07-17 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / ada / exp_ch7.adb
bloba8a32fb5114b477e299feb03c40b24c4ecad1852
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-2009, 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 Errout; use Errout;
34 with Exp_Ch9; use Exp_Ch9;
35 with Exp_Ch11; use Exp_Ch11;
36 with Exp_Dbug; use Exp_Dbug;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Lib; use Lib;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Output; use Output;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sinfo; use Sinfo;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Targparm; use Targparm;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
65 package body Exp_Ch7 is
67 --------------------------------
68 -- Transient Scope Management --
69 --------------------------------
71 -- A transient scope is created when temporary objects are created by the
72 -- compiler. These temporary objects are allocated on the secondary stack
73 -- and the transient scope is responsible for finalizing the object when
74 -- appropriate and reclaiming the memory at the right time. The temporary
75 -- objects are generally the objects allocated to store the result of a
76 -- function returning an unconstrained or a tagged value. Expressions
77 -- needing to be wrapped in a transient scope (functions calls returning
78 -- unconstrained or tagged values) may appear in 3 different contexts which
79 -- lead to 3 different kinds of transient scope expansion:
81 -- 1. In a simple statement (procedure call, assignment, ...). In
82 -- this case the instruction is wrapped into a transient block.
83 -- (See Wrap_Transient_Statement for details)
85 -- 2. In an expression of a control structure (test in a IF statement,
86 -- expression in a CASE statement, ...).
87 -- (See Wrap_Transient_Expression for details)
89 -- 3. In a expression of an object_declaration. No wrapping is possible
90 -- here, so the finalization actions, if any, are done right after the
91 -- declaration and the secondary stack deallocation is done in the
92 -- proper enclosing scope (see Wrap_Transient_Declaration for details)
94 -- Note about functions returning tagged types: it has been decided to
95 -- always allocate their result in the secondary stack, even though is not
96 -- absolutely mandatory when the tagged type is constrained because the
97 -- caller knows the size of the returned object and thus could allocate the
98 -- result in the primary stack. An exception to this is when the function
99 -- builds its result in place, as is done for functions with inherently
100 -- limited result types for Ada 2005. In that case, certain callers may
101 -- pass the address of a constrained object as the target object for the
102 -- function result.
104 -- By allocating tagged results in the secondary stack a number of
105 -- implementation difficulties are avoided:
107 -- - If it is a dispatching function call, the computation of the size of
108 -- the result is possible but complex from the outside.
110 -- - If the returned type is controlled, the assignment of the returned
111 -- value to the anonymous object involves an Adjust, and we have no
112 -- easy way to access the anonymous object created by the back end.
114 -- - If the returned type is class-wide, this is an unconstrained type
115 -- anyway.
117 -- Furthermore, the small loss in efficiency which is the result of this
118 -- decision is not such a big deal because functions returning tagged types
119 -- are not as common in practice compared to functions returning access to
120 -- a tagged type.
122 --------------------------------------------------
123 -- Transient Blocks and Finalization Management --
124 --------------------------------------------------
126 function Find_Node_To_Be_Wrapped (N : Node_Id) return Node_Id;
127 -- N is a node which may generate a transient scope. Loop over the parent
128 -- pointers of N until it find the appropriate node to wrap. If it returns
129 -- Empty, it means that no transient scope is needed in this context.
131 function Make_Clean
132 (N : Node_Id;
133 Clean : Entity_Id;
134 Mark : Entity_Id;
135 Flist : Entity_Id;
136 Is_Task : Boolean;
137 Is_Master : Boolean;
138 Is_Protected_Subprogram : Boolean;
139 Is_Task_Allocation_Block : Boolean;
140 Is_Asynchronous_Call_Block : Boolean;
141 Chained_Cleanup_Action : Node_Id) return Node_Id;
142 -- Expand the clean-up procedure for a controlled and/or transient block,
143 -- and/or task master or task body, or a block used to implement task
144 -- allocation or asynchronous entry calls, or a procedure used to implement
145 -- protected procedures. Clean is the entity for such a procedure. Mark
146 -- is the entity for the secondary stack mark, if empty only controlled
147 -- block clean-up will be performed. Flist is the entity for the local
148 -- final list, if empty only transient scope clean-up will be performed.
149 -- The flags Is_Task and Is_Master control the calls to the corresponding
150 -- finalization actions for a task body or for an entity that is a task
151 -- master. Finally if Chained_Cleanup_Action is present, it is a reference
152 -- to a previous cleanup procedure, a call to which is appended at the
153 -- end of the generated one.
155 procedure Set_Node_To_Be_Wrapped (N : Node_Id);
156 -- Set the field Node_To_Be_Wrapped of the current scope
158 procedure Insert_Actions_In_Scope_Around (N : Node_Id);
159 -- Insert the before-actions kept in the scope stack before N, and the
160 -- after-actions after N, which must be a member of a list.
162 function Make_Transient_Block
163 (Loc : Source_Ptr;
164 Action : Node_Id) return Node_Id;
165 -- Create a transient block whose name is Scope, which is also a controlled
166 -- block if Flist is not empty and whose only code is Action (either a
167 -- single statement or single declaration).
169 type Final_Primitives is (Initialize_Case, Adjust_Case, Finalize_Case);
170 -- This enumeration type is defined in order to ease sharing code for
171 -- building finalization procedures for composite types.
173 Name_Of : constant array (Final_Primitives) of Name_Id :=
174 (Initialize_Case => Name_Initialize,
175 Adjust_Case => Name_Adjust,
176 Finalize_Case => Name_Finalize);
178 Deep_Name_Of : constant array (Final_Primitives) of TSS_Name_Type :=
179 (Initialize_Case => TSS_Deep_Initialize,
180 Adjust_Case => TSS_Deep_Adjust,
181 Finalize_Case => TSS_Deep_Finalize);
183 procedure Build_Record_Deep_Procs (Typ : Entity_Id);
184 -- Build the deep Initialize/Adjust/Finalize for a record Typ with
185 -- Has_Component_Component set and store them using the TSS mechanism.
187 procedure Build_Array_Deep_Procs (Typ : Entity_Id);
188 -- Build the deep Initialize/Adjust/Finalize for a record Typ with
189 -- Has_Controlled_Component set and store them using the TSS mechanism.
191 function Make_Deep_Proc
192 (Prim : Final_Primitives;
193 Typ : Entity_Id;
194 Stmts : List_Id) return Node_Id;
195 -- This function generates the tree for Deep_Initialize, Deep_Adjust or
196 -- Deep_Finalize procedures according to the first parameter, these
197 -- procedures operate on the type Typ. The Stmts parameter gives the body
198 -- of the procedure.
200 function Make_Deep_Array_Body
201 (Prim : Final_Primitives;
202 Typ : Entity_Id) return List_Id;
203 -- This function generates the list of statements for implementing
204 -- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures according to
205 -- the first parameter, these procedures operate on the array type Typ.
207 function Make_Deep_Record_Body
208 (Prim : Final_Primitives;
209 Typ : Entity_Id) return List_Id;
210 -- This function generates the list of statements for implementing
211 -- Deep_Initialize, Deep_Adjust or Deep_Finalize procedures according to
212 -- the first parameter, these procedures operate on the record type Typ.
214 procedure Check_Visibly_Controlled
215 (Prim : Final_Primitives;
216 Typ : Entity_Id;
217 E : in out Entity_Id;
218 Cref : in out Node_Id);
219 -- The controlled operation declared for a derived type may not be
220 -- overriding, if the controlled operations of the parent type are
221 -- hidden, for example when the parent is a private type whose full
222 -- view is controlled. For other primitive operations we modify the
223 -- name of the operation to indicate that it is not overriding, but
224 -- this is not possible for Initialize, etc. because they have to be
225 -- retrievable by name. Before generating the proper call to one of
226 -- these operations we check whether Typ is known to be controlled at
227 -- the point of definition. If it is not then we must retrieve the
228 -- hidden operation of the parent and use it instead. This is one
229 -- case that might be solved more cleanly once Overriding pragmas or
230 -- declarations are in place.
232 function Convert_View
233 (Proc : Entity_Id;
234 Arg : Node_Id;
235 Ind : Pos := 1) return Node_Id;
236 -- Proc is one of the Initialize/Adjust/Finalize operations, and
237 -- Arg is the argument being passed to it. Ind indicates which
238 -- formal of procedure Proc we are trying to match. This function
239 -- will, if necessary, generate an conversion between the partial
240 -- and full view of Arg to match the type of the formal of Proc,
241 -- or force a conversion to the class-wide type in the case where
242 -- the operation is abstract.
244 -----------------------------
245 -- Finalization Management --
246 -----------------------------
248 -- This part describe how Initialization/Adjustment/Finalization procedures
249 -- are generated and called. Two cases must be considered, types that are
250 -- Controlled (Is_Controlled flag set) and composite types that contain
251 -- controlled components (Has_Controlled_Component flag set). In the first
252 -- case the procedures to call are the user-defined primitive operations
253 -- Initialize/Adjust/Finalize. In the second case, GNAT generates
254 -- Deep_Initialize, Deep_Adjust and Deep_Finalize that are in charge
255 -- of calling the former procedures on the controlled components.
257 -- For records with Has_Controlled_Component set, a hidden "controller"
258 -- component is inserted. This controller component contains its own
259 -- finalization list on which all controlled components are attached
260 -- creating an indirection on the upper-level Finalization list. This
261 -- technique facilitates the management of objects whose number of
262 -- controlled components changes during execution. This controller
263 -- component is itself controlled and is attached to the upper-level
264 -- finalization chain. Its adjust primitive is in charge of calling adjust
265 -- on the components and adjusting the finalization pointer to match their
266 -- new location (see a-finali.adb).
268 -- It is not possible to use a similar technique for arrays that have
269 -- Has_Controlled_Component set. In this case, deep procedures are
270 -- generated that call initialize/adjust/finalize + attachment or
271 -- detachment on the finalization list for all component.
273 -- Initialize calls: they are generated for declarations or dynamic
274 -- allocations of Controlled objects with no initial value. They are always
275 -- followed by an attachment to the current Finalization Chain. For the
276 -- dynamic allocation case this the chain attached to the scope of the
277 -- access type definition otherwise, this is the chain of the current
278 -- scope.
280 -- Adjust Calls: They are generated on 2 occasions: (1) for
281 -- declarations or dynamic allocations of Controlled objects with an
282 -- initial value. (2) after an assignment. In the first case they are
283 -- followed by an attachment to the final chain, in the second case
284 -- they are not.
286 -- Finalization Calls: They are generated on (1) scope exit, (2)
287 -- assignments, (3) unchecked deallocations. In case (3) they have to
288 -- be detached from the final chain, in case (2) they must not and in
289 -- case (1) this is not important since we are exiting the scope anyway.
291 -- Other details:
293 -- Type extensions will have a new record controller at each derivation
294 -- level containing controlled components. The record controller for
295 -- the parent/ancestor is attached to the finalization list of the
296 -- extension's record controller (i.e. the parent is like a component
297 -- of the extension).
299 -- For types that are both Is_Controlled and Has_Controlled_Components,
300 -- the record controller and the object itself are handled separately.
301 -- It could seem simpler to attach the object at the end of its record
302 -- controller but this would not tackle view conversions properly.
304 -- A classwide type can always potentially have controlled components
305 -- but the record controller of the corresponding actual type may not
306 -- be known at compile time so the dispatch table contains a special
307 -- field that allows to compute the offset of the record controller
308 -- dynamically. See s-finimp.Deep_Tag_Attach and a-tags.RC_Offset.
310 -- Here is a simple example of the expansion of a controlled block :
312 -- declare
313 -- X : Controlled;
314 -- Y : Controlled := Init;
316 -- type R is record
317 -- C : Controlled;
318 -- end record;
319 -- W : R;
320 -- Z : R := (C => X);
321 -- begin
322 -- X := Y;
323 -- W := Z;
324 -- end;
326 -- is expanded into
328 -- declare
329 -- _L : System.FI.Finalizable_Ptr;
331 -- procedure _Clean is
332 -- begin
333 -- Abort_Defer;
334 -- System.FI.Finalize_List (_L);
335 -- Abort_Undefer;
336 -- end _Clean;
338 -- X : Controlled;
339 -- begin
340 -- Abort_Defer;
341 -- Initialize (X);
342 -- Attach_To_Final_List (_L, Finalizable (X), 1);
343 -- at end: Abort_Undefer;
344 -- Y : Controlled := Init;
345 -- Adjust (Y);
346 -- Attach_To_Final_List (_L, Finalizable (Y), 1);
348 -- type R is record
349 -- _C : Record_Controller;
350 -- C : Controlled;
351 -- end record;
352 -- W : R;
353 -- begin
354 -- Abort_Defer;
355 -- Deep_Initialize (W, _L, 1);
356 -- at end: Abort_Under;
357 -- Z : R := (C => X);
358 -- Deep_Adjust (Z, _L, 1);
360 -- begin
361 -- _Assign (X, Y);
362 -- Deep_Finalize (W, False);
363 -- <save W's final pointers>
364 -- W := Z;
365 -- <restore W's final pointers>
366 -- Deep_Adjust (W, _L, 0);
367 -- at end
368 -- _Clean;
369 -- end;
371 function Global_Flist_Ref (Flist_Ref : Node_Id) return Boolean;
372 -- Return True if Flist_Ref refers to a global final list, either the
373 -- object Global_Final_List which is used to attach standalone objects,
374 -- or any of the list controllers associated with library-level access
375 -- to controlled objects.
377 procedure Clean_Simple_Protected_Objects (N : Node_Id);
378 -- Protected objects without entries are not controlled types, and the
379 -- locks have to be released explicitly when such an object goes out
380 -- of scope. Traverse declarations in scope to determine whether such
381 -- objects are present.
383 ----------------------------
384 -- Build_Array_Deep_Procs --
385 ----------------------------
387 procedure Build_Array_Deep_Procs (Typ : Entity_Id) is
388 begin
389 Set_TSS (Typ,
390 Make_Deep_Proc (
391 Prim => Initialize_Case,
392 Typ => Typ,
393 Stmts => Make_Deep_Array_Body (Initialize_Case, Typ)));
395 if not Is_Inherently_Limited_Type (Typ) then
396 Set_TSS (Typ,
397 Make_Deep_Proc (
398 Prim => Adjust_Case,
399 Typ => Typ,
400 Stmts => Make_Deep_Array_Body (Adjust_Case, Typ)));
401 end if;
403 Set_TSS (Typ,
404 Make_Deep_Proc (
405 Prim => Finalize_Case,
406 Typ => Typ,
407 Stmts => Make_Deep_Array_Body (Finalize_Case, Typ)));
408 end Build_Array_Deep_Procs;
410 -----------------------------
411 -- Build_Controlling_Procs --
412 -----------------------------
414 procedure Build_Controlling_Procs (Typ : Entity_Id) is
415 begin
416 if Is_Array_Type (Typ) then
417 Build_Array_Deep_Procs (Typ);
419 else pragma Assert (Is_Record_Type (Typ));
420 Build_Record_Deep_Procs (Typ);
421 end if;
422 end Build_Controlling_Procs;
424 ----------------------
425 -- Build_Final_List --
426 ----------------------
428 procedure Build_Final_List (N : Node_Id; Typ : Entity_Id) is
429 Loc : constant Source_Ptr := Sloc (N);
430 Decl : Node_Id;
432 begin
433 Set_Associated_Final_Chain (Typ,
434 Make_Defining_Identifier (Loc,
435 New_External_Name (Chars (Typ), 'L')));
437 Decl :=
438 Make_Object_Declaration (Loc,
439 Defining_Identifier =>
440 Associated_Final_Chain (Typ),
441 Object_Definition =>
442 New_Reference_To
443 (RTE (RE_List_Controller), Loc));
445 -- If the type is declared in a package declaration and designates a
446 -- Taft amendment type that requires finalization, place declaration
447 -- of finalization list in the body, because no client of the package
448 -- can create objects of the type and thus make use of this list. This
449 -- ensures the tree for the spec is identical whenever it is compiled.
451 if Has_Completion_In_Body (Directly_Designated_Type (Typ))
452 and then In_Package_Body (Current_Scope)
453 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
454 and then
455 Nkind (Parent (Declaration_Node (Typ))) = N_Package_Specification
456 then
457 Insert_Action (Parent (Designated_Type (Typ)), Decl);
459 -- The type may have been frozen already, and this is a late freezing
460 -- action, in which case the declaration must be elaborated at once.
461 -- If the call is for an allocator, the chain must also be created now,
462 -- because the freezing of the type does not build one. Otherwise, the
463 -- declaration is one of the freezing actions for a user-defined type.
465 elsif Is_Frozen (Typ)
466 or else (Nkind (N) = N_Allocator
467 and then Ekind (Etype (N)) = E_Anonymous_Access_Type)
468 then
469 Insert_Action (N, Decl);
471 else
472 Append_Freeze_Action (Typ, Decl);
473 end if;
474 end Build_Final_List;
476 ---------------------
477 -- Build_Late_Proc --
478 ---------------------
480 procedure Build_Late_Proc (Typ : Entity_Id; Nam : Name_Id) is
481 begin
482 for Final_Prim in Name_Of'Range loop
483 if Name_Of (Final_Prim) = Nam then
484 Set_TSS (Typ,
485 Make_Deep_Proc (
486 Prim => Final_Prim,
487 Typ => Typ,
488 Stmts => Make_Deep_Record_Body (Final_Prim, Typ)));
489 end if;
490 end loop;
491 end Build_Late_Proc;
493 -----------------------------
494 -- Build_Record_Deep_Procs --
495 -----------------------------
497 procedure Build_Record_Deep_Procs (Typ : Entity_Id) is
498 begin
499 Set_TSS (Typ,
500 Make_Deep_Proc (
501 Prim => Initialize_Case,
502 Typ => Typ,
503 Stmts => Make_Deep_Record_Body (Initialize_Case, Typ)));
505 if not Is_Inherently_Limited_Type (Typ) then
506 Set_TSS (Typ,
507 Make_Deep_Proc (
508 Prim => Adjust_Case,
509 Typ => Typ,
510 Stmts => Make_Deep_Record_Body (Adjust_Case, Typ)));
511 end if;
513 Set_TSS (Typ,
514 Make_Deep_Proc (
515 Prim => Finalize_Case,
516 Typ => Typ,
517 Stmts => Make_Deep_Record_Body (Finalize_Case, Typ)));
518 end Build_Record_Deep_Procs;
520 -------------------
521 -- Cleanup_Array --
522 -------------------
524 function Cleanup_Array
525 (N : Node_Id;
526 Obj : Node_Id;
527 Typ : Entity_Id) return List_Id
529 Loc : constant Source_Ptr := Sloc (N);
530 Index_List : constant List_Id := New_List;
532 function Free_Component return List_Id;
533 -- Generate the code to finalize the task or protected subcomponents
534 -- of a single component of the array.
536 function Free_One_Dimension (Dim : Int) return List_Id;
537 -- Generate a loop over one dimension of the array
539 --------------------
540 -- Free_Component --
541 --------------------
543 function Free_Component return List_Id is
544 Stmts : List_Id := New_List;
545 Tsk : Node_Id;
546 C_Typ : constant Entity_Id := Component_Type (Typ);
548 begin
549 -- Component type is known to contain tasks or protected objects
551 Tsk :=
552 Make_Indexed_Component (Loc,
553 Prefix => Duplicate_Subexpr_No_Checks (Obj),
554 Expressions => Index_List);
556 Set_Etype (Tsk, C_Typ);
558 if Is_Task_Type (C_Typ) then
559 Append_To (Stmts, Cleanup_Task (N, Tsk));
561 elsif Is_Simple_Protected_Type (C_Typ) then
562 Append_To (Stmts, Cleanup_Protected_Object (N, Tsk));
564 elsif Is_Record_Type (C_Typ) then
565 Stmts := Cleanup_Record (N, Tsk, C_Typ);
567 elsif Is_Array_Type (C_Typ) then
568 Stmts := Cleanup_Array (N, Tsk, C_Typ);
569 end if;
571 return Stmts;
572 end Free_Component;
574 ------------------------
575 -- Free_One_Dimension --
576 ------------------------
578 function Free_One_Dimension (Dim : Int) return List_Id is
579 Index : Entity_Id;
581 begin
582 if Dim > Number_Dimensions (Typ) then
583 return Free_Component;
585 -- Here we generate the required loop
587 else
588 Index :=
589 Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
591 Append (New_Reference_To (Index, Loc), Index_List);
593 return New_List (
594 Make_Implicit_Loop_Statement (N,
595 Identifier => Empty,
596 Iteration_Scheme =>
597 Make_Iteration_Scheme (Loc,
598 Loop_Parameter_Specification =>
599 Make_Loop_Parameter_Specification (Loc,
600 Defining_Identifier => Index,
601 Discrete_Subtype_Definition =>
602 Make_Attribute_Reference (Loc,
603 Prefix => Duplicate_Subexpr (Obj),
604 Attribute_Name => Name_Range,
605 Expressions => New_List (
606 Make_Integer_Literal (Loc, Dim))))),
607 Statements => Free_One_Dimension (Dim + 1)));
608 end if;
609 end Free_One_Dimension;
611 -- Start of processing for Cleanup_Array
613 begin
614 return Free_One_Dimension (1);
615 end Cleanup_Array;
617 --------------------
618 -- Cleanup_Record --
619 --------------------
621 function Cleanup_Record
622 (N : Node_Id;
623 Obj : Node_Id;
624 Typ : Entity_Id) return List_Id
626 Loc : constant Source_Ptr := Sloc (N);
627 Tsk : Node_Id;
628 Comp : Entity_Id;
629 Stmts : constant List_Id := New_List;
630 U_Typ : constant Entity_Id := Underlying_Type (Typ);
632 begin
633 if Has_Discriminants (U_Typ)
634 and then Nkind (Parent (U_Typ)) = N_Full_Type_Declaration
635 and then
636 Nkind (Type_Definition (Parent (U_Typ))) = N_Record_Definition
637 and then
638 Present
639 (Variant_Part
640 (Component_List (Type_Definition (Parent (U_Typ)))))
641 then
642 -- For now, do not attempt to free a component that may appear in
643 -- a variant, and instead issue a warning. Doing this "properly"
644 -- would require building a case statement and would be quite a
645 -- mess. Note that the RM only requires that free "work" for the
646 -- case of a task access value, so already we go way beyond this
647 -- in that we deal with the array case and non-discriminated
648 -- record cases.
650 Error_Msg_N
651 ("task/protected object in variant record will not be freed?", N);
652 return New_List (Make_Null_Statement (Loc));
653 end if;
655 Comp := First_Component (Typ);
657 while Present (Comp) loop
658 if Has_Task (Etype (Comp))
659 or else Has_Simple_Protected_Object (Etype (Comp))
660 then
661 Tsk :=
662 Make_Selected_Component (Loc,
663 Prefix => Duplicate_Subexpr_No_Checks (Obj),
664 Selector_Name => New_Occurrence_Of (Comp, Loc));
665 Set_Etype (Tsk, Etype (Comp));
667 if Is_Task_Type (Etype (Comp)) then
668 Append_To (Stmts, Cleanup_Task (N, Tsk));
670 elsif Is_Simple_Protected_Type (Etype (Comp)) then
671 Append_To (Stmts, Cleanup_Protected_Object (N, Tsk));
673 elsif Is_Record_Type (Etype (Comp)) then
675 -- Recurse, by generating the prefix of the argument to
676 -- the eventual cleanup call.
678 Append_List_To
679 (Stmts, Cleanup_Record (N, Tsk, Etype (Comp)));
681 elsif Is_Array_Type (Etype (Comp)) then
682 Append_List_To
683 (Stmts, Cleanup_Array (N, Tsk, Etype (Comp)));
684 end if;
685 end if;
687 Next_Component (Comp);
688 end loop;
690 return Stmts;
691 end Cleanup_Record;
693 ------------------------------
694 -- Cleanup_Protected_Object --
695 ------------------------------
697 function Cleanup_Protected_Object
698 (N : Node_Id;
699 Ref : Node_Id) return Node_Id
701 Loc : constant Source_Ptr := Sloc (N);
703 begin
704 return
705 Make_Procedure_Call_Statement (Loc,
706 Name => New_Reference_To (RTE (RE_Finalize_Protection), Loc),
707 Parameter_Associations => New_List (
708 Concurrent_Ref (Ref)));
709 end Cleanup_Protected_Object;
711 ------------------------------------
712 -- Clean_Simple_Protected_Objects --
713 ------------------------------------
715 procedure Clean_Simple_Protected_Objects (N : Node_Id) is
716 Stmts : constant List_Id := Statements (Handled_Statement_Sequence (N));
717 Stmt : Node_Id := Last (Stmts);
718 E : Entity_Id;
720 begin
721 E := First_Entity (Current_Scope);
722 while Present (E) loop
723 if (Ekind (E) = E_Variable
724 or else Ekind (E) = E_Constant)
725 and then Has_Simple_Protected_Object (Etype (E))
726 and then not Has_Task (Etype (E))
727 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
728 then
729 declare
730 Typ : constant Entity_Id := Etype (E);
731 Ref : constant Node_Id := New_Occurrence_Of (E, Sloc (Stmt));
733 begin
734 if Is_Simple_Protected_Type (Typ) then
735 Append_To (Stmts, Cleanup_Protected_Object (N, Ref));
737 elsif Has_Simple_Protected_Object (Typ) then
738 if Is_Record_Type (Typ) then
739 Append_List_To (Stmts, Cleanup_Record (N, Ref, Typ));
741 elsif Is_Array_Type (Typ) then
742 Append_List_To (Stmts, Cleanup_Array (N, Ref, Typ));
743 end if;
744 end if;
745 end;
746 end if;
748 Next_Entity (E);
749 end loop;
751 -- Analyze inserted cleanup statements
753 if Present (Stmt) then
754 Stmt := Next (Stmt);
756 while Present (Stmt) loop
757 Analyze (Stmt);
758 Next (Stmt);
759 end loop;
760 end if;
761 end Clean_Simple_Protected_Objects;
763 ------------------
764 -- Cleanup_Task --
765 ------------------
767 function Cleanup_Task
768 (N : Node_Id;
769 Ref : Node_Id) return Node_Id
771 Loc : constant Source_Ptr := Sloc (N);
772 begin
773 return
774 Make_Procedure_Call_Statement (Loc,
775 Name => New_Reference_To (RTE (RE_Free_Task), Loc),
776 Parameter_Associations =>
777 New_List (Concurrent_Ref (Ref)));
778 end Cleanup_Task;
780 ---------------------------------
781 -- Has_Simple_Protected_Object --
782 ---------------------------------
784 function Has_Simple_Protected_Object (T : Entity_Id) return Boolean is
785 Comp : Entity_Id;
787 begin
788 if Is_Simple_Protected_Type (T) then
789 return True;
791 elsif Is_Array_Type (T) then
792 return Has_Simple_Protected_Object (Component_Type (T));
794 elsif Is_Record_Type (T) then
795 Comp := First_Component (T);
797 while Present (Comp) loop
798 if Has_Simple_Protected_Object (Etype (Comp)) then
799 return True;
800 end if;
802 Next_Component (Comp);
803 end loop;
805 return False;
807 else
808 return False;
809 end if;
810 end Has_Simple_Protected_Object;
812 ------------------------------
813 -- Is_Simple_Protected_Type --
814 ------------------------------
816 function Is_Simple_Protected_Type (T : Entity_Id) return Boolean is
817 begin
818 return Is_Protected_Type (T) and then not Has_Entries (T);
819 end Is_Simple_Protected_Type;
821 ------------------------------
822 -- Check_Visibly_Controlled --
823 ------------------------------
825 procedure Check_Visibly_Controlled
826 (Prim : Final_Primitives;
827 Typ : Entity_Id;
828 E : in out Entity_Id;
829 Cref : in out Node_Id)
831 Parent_Type : Entity_Id;
832 Op : Entity_Id;
834 begin
835 if Is_Derived_Type (Typ)
836 and then Comes_From_Source (E)
837 and then not Is_Overriding_Operation (E)
838 then
839 -- We know that the explicit operation on the type does not override
840 -- the inherited operation of the parent, and that the derivation
841 -- is from a private type that is not visibly controlled.
843 Parent_Type := Etype (Typ);
844 Op := Find_Prim_Op (Parent_Type, Name_Of (Prim));
846 if Present (Op) then
847 E := Op;
849 -- Wrap the object to be initialized into the proper
850 -- unchecked conversion, to be compatible with the operation
851 -- to be called.
853 if Nkind (Cref) = N_Unchecked_Type_Conversion then
854 Cref := Unchecked_Convert_To (Parent_Type, Expression (Cref));
855 else
856 Cref := Unchecked_Convert_To (Parent_Type, Cref);
857 end if;
858 end if;
859 end if;
860 end Check_Visibly_Controlled;
862 -------------------------------
863 -- CW_Or_Has_Controlled_Part --
864 -------------------------------
866 function CW_Or_Has_Controlled_Part (T : Entity_Id) return Boolean is
867 begin
868 return Is_Class_Wide_Type (T) or else Needs_Finalization (T);
869 end CW_Or_Has_Controlled_Part;
871 --------------------------
872 -- Controller_Component --
873 --------------------------
875 function Controller_Component (Typ : Entity_Id) return Entity_Id is
876 T : Entity_Id := Base_Type (Typ);
877 Comp : Entity_Id;
878 Comp_Scop : Entity_Id;
879 Res : Entity_Id := Empty;
880 Res_Scop : Entity_Id := Empty;
882 begin
883 if Is_Class_Wide_Type (T) then
884 T := Root_Type (T);
885 end if;
887 if Is_Private_Type (T) then
888 T := Underlying_Type (T);
889 end if;
891 -- Fetch the outermost controller
893 Comp := First_Entity (T);
894 while Present (Comp) loop
895 if Chars (Comp) = Name_uController then
896 Comp_Scop := Scope (Original_Record_Component (Comp));
898 -- If this controller is at the outermost level, no need to
899 -- look for another one
901 if Comp_Scop = T then
902 return Comp;
904 -- Otherwise record the outermost one and continue looking
906 elsif Res = Empty or else Is_Ancestor (Res_Scop, Comp_Scop) then
907 Res := Comp;
908 Res_Scop := Comp_Scop;
909 end if;
910 end if;
912 Next_Entity (Comp);
913 end loop;
915 -- If we fall through the loop, there is no controller component
917 return Res;
918 end Controller_Component;
920 ------------------
921 -- Convert_View --
922 ------------------
924 function Convert_View
925 (Proc : Entity_Id;
926 Arg : Node_Id;
927 Ind : Pos := 1) return Node_Id
929 Fent : Entity_Id := First_Entity (Proc);
930 Ftyp : Entity_Id;
931 Atyp : Entity_Id;
933 begin
934 for J in 2 .. Ind loop
935 Next_Entity (Fent);
936 end loop;
938 Ftyp := Etype (Fent);
940 if Nkind_In (Arg, N_Type_Conversion, N_Unchecked_Type_Conversion) then
941 Atyp := Entity (Subtype_Mark (Arg));
942 else
943 Atyp := Etype (Arg);
944 end if;
946 if Is_Abstract_Subprogram (Proc) and then Is_Tagged_Type (Ftyp) then
947 return Unchecked_Convert_To (Class_Wide_Type (Ftyp), Arg);
949 elsif Ftyp /= Atyp
950 and then Present (Atyp)
951 and then
952 (Is_Private_Type (Ftyp) or else Is_Private_Type (Atyp))
953 and then
954 Base_Type (Underlying_Type (Atyp)) =
955 Base_Type (Underlying_Type (Ftyp))
956 then
957 return Unchecked_Convert_To (Ftyp, Arg);
959 -- If the argument is already a conversion, as generated by
960 -- Make_Init_Call, set the target type to the type of the formal
961 -- directly, to avoid spurious typing problems.
963 elsif Nkind_In (Arg, N_Unchecked_Type_Conversion, N_Type_Conversion)
964 and then not Is_Class_Wide_Type (Atyp)
965 then
966 Set_Subtype_Mark (Arg, New_Occurrence_Of (Ftyp, Sloc (Arg)));
967 Set_Etype (Arg, Ftyp);
968 return Arg;
970 else
971 return Arg;
972 end if;
973 end Convert_View;
975 -------------------------------
976 -- Establish_Transient_Scope --
977 -------------------------------
979 -- This procedure is called each time a transient block has to be inserted
980 -- that is to say for each call to a function with unconstrained or tagged
981 -- result. It creates a new scope on the stack scope in order to enclose
982 -- all transient variables generated
984 procedure Establish_Transient_Scope (N : Node_Id; Sec_Stack : Boolean) is
985 Loc : constant Source_Ptr := Sloc (N);
986 Wrap_Node : Node_Id;
988 begin
989 -- Nothing to do for virtual machines where memory is GCed
991 if VM_Target /= No_VM then
992 return;
993 end if;
995 -- Do not create a transient scope if we are already inside one
997 for S in reverse Scope_Stack.First .. Scope_Stack.Last loop
998 if Scope_Stack.Table (S).Is_Transient then
999 if Sec_Stack then
1000 Set_Uses_Sec_Stack (Scope_Stack.Table (S).Entity);
1001 end if;
1003 return;
1005 -- If we have encountered Standard there are no enclosing
1006 -- transient scopes.
1008 elsif Scope_Stack.Table (S).Entity = Standard_Standard then
1009 exit;
1011 end if;
1012 end loop;
1014 Wrap_Node := Find_Node_To_Be_Wrapped (N);
1016 -- Case of no wrap node, false alert, no transient scope needed
1018 if No (Wrap_Node) then
1019 null;
1021 -- If the node to wrap is an iteration_scheme, the expression is
1022 -- one of the bounds, and the expansion will make an explicit
1023 -- declaration for it (see Analyze_Iteration_Scheme, sem_ch5.adb),
1024 -- so do not apply any transformations here.
1026 elsif Nkind (Wrap_Node) = N_Iteration_Scheme then
1027 null;
1029 else
1030 Push_Scope (New_Internal_Entity (E_Block, Current_Scope, Loc, 'B'));
1031 Set_Scope_Is_Transient;
1033 if Sec_Stack then
1034 Set_Uses_Sec_Stack (Current_Scope);
1035 Check_Restriction (No_Secondary_Stack, N);
1036 end if;
1038 Set_Etype (Current_Scope, Standard_Void_Type);
1039 Set_Node_To_Be_Wrapped (Wrap_Node);
1041 if Debug_Flag_W then
1042 Write_Str (" <Transient>");
1043 Write_Eol;
1044 end if;
1045 end if;
1046 end Establish_Transient_Scope;
1048 ----------------------------
1049 -- Expand_Cleanup_Actions --
1050 ----------------------------
1052 procedure Expand_Cleanup_Actions (N : Node_Id) is
1053 S : constant Entity_Id := Current_Scope;
1054 Flist : constant Entity_Id := Finalization_Chain_Entity (S);
1055 Is_Task : constant Boolean := Nkind (Original_Node (N)) = N_Task_Body;
1057 Is_Master : constant Boolean :=
1058 Nkind (N) /= N_Entry_Body
1059 and then Is_Task_Master (N);
1060 Is_Protected : constant Boolean :=
1061 Nkind (N) = N_Subprogram_Body
1062 and then Is_Protected_Subprogram_Body (N);
1063 Is_Task_Allocation : constant Boolean :=
1064 Nkind (N) = N_Block_Statement
1065 and then Is_Task_Allocation_Block (N);
1066 Is_Asynchronous_Call : constant Boolean :=
1067 Nkind (N) = N_Block_Statement
1068 and then Is_Asynchronous_Call_Block (N);
1070 Previous_At_End_Proc : constant Node_Id :=
1071 At_End_Proc (Handled_Statement_Sequence (N));
1073 Clean : Entity_Id;
1074 Loc : Source_Ptr;
1075 Mark : Entity_Id := Empty;
1076 New_Decls : constant List_Id := New_List;
1077 Blok : Node_Id;
1078 End_Lab : Node_Id;
1079 Wrapped : Boolean;
1080 Chain : Entity_Id := Empty;
1081 Decl : Node_Id;
1082 Old_Poll : Boolean;
1084 begin
1085 -- If we are generating expanded code for debugging purposes, use
1086 -- the Sloc of the point of insertion for the cleanup code. The Sloc
1087 -- will be updated subsequently to reference the proper line in the
1088 -- .dg file. If we are not debugging generated code, use instead
1089 -- No_Location, so that no debug information is generated for the
1090 -- cleanup code. This makes the behavior of the NEXT command in GDB
1091 -- monotonic, and makes the placement of breakpoints more accurate.
1093 if Debug_Generated_Code then
1094 Loc := Sloc (S);
1095 else
1096 Loc := No_Location;
1097 end if;
1099 -- There are cleanup actions only if the secondary stack needs
1100 -- releasing or some finalizations are needed or in the context
1101 -- of tasking
1103 if Uses_Sec_Stack (Current_Scope)
1104 and then not Sec_Stack_Needed_For_Return (Current_Scope)
1105 then
1106 null;
1107 elsif No (Flist)
1108 and then not Is_Master
1109 and then not Is_Task
1110 and then not Is_Protected
1111 and then not Is_Task_Allocation
1112 and then not Is_Asynchronous_Call
1113 then
1114 Clean_Simple_Protected_Objects (N);
1115 return;
1116 end if;
1118 -- If the current scope is the subprogram body that is the rewriting
1119 -- of a task body, and the descriptors have not been delayed (due to
1120 -- some nested instantiations) do not generate redundant cleanup
1121 -- actions: the cleanup procedure already exists for this body.
1123 if Nkind (N) = N_Subprogram_Body
1124 and then Nkind (Original_Node (N)) = N_Task_Body
1125 and then not Delay_Subprogram_Descriptors (Corresponding_Spec (N))
1126 then
1127 return;
1128 end if;
1130 -- Set polling off, since we don't need to poll during cleanup
1131 -- actions, and indeed for the cleanup routine, which is executed
1132 -- with aborts deferred, we don't want polling.
1134 Old_Poll := Polling_Required;
1135 Polling_Required := False;
1137 -- Make sure we have a declaration list, since we will add to it
1139 if No (Declarations (N)) then
1140 Set_Declarations (N, New_List);
1141 end if;
1143 -- The task activation call has already been built for task
1144 -- allocation blocks.
1146 if not Is_Task_Allocation then
1147 Build_Task_Activation_Call (N);
1148 end if;
1150 if Is_Master then
1151 Establish_Task_Master (N);
1152 end if;
1154 -- If secondary stack is in use, expand:
1155 -- _Mxx : constant Mark_Id := SS_Mark;
1157 -- Suppress calls to SS_Mark and SS_Release if VM_Target,
1158 -- since we never use the secondary stack on the VM.
1160 if Uses_Sec_Stack (Current_Scope)
1161 and then not Sec_Stack_Needed_For_Return (Current_Scope)
1162 and then VM_Target = No_VM
1163 then
1164 Mark := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
1165 Append_To (New_Decls,
1166 Make_Object_Declaration (Loc,
1167 Defining_Identifier => Mark,
1168 Object_Definition => New_Reference_To (RTE (RE_Mark_Id), Loc),
1169 Expression =>
1170 Make_Function_Call (Loc,
1171 Name => New_Reference_To (RTE (RE_SS_Mark), Loc))));
1173 Set_Uses_Sec_Stack (Current_Scope, False);
1174 end if;
1176 -- If finalization list is present then expand:
1177 -- Local_Final_List : System.FI.Finalizable_Ptr;
1179 if Present (Flist) then
1180 Append_To (New_Decls,
1181 Make_Object_Declaration (Loc,
1182 Defining_Identifier => Flist,
1183 Object_Definition =>
1184 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
1185 end if;
1187 -- Clean-up procedure definition
1189 Clean := Make_Defining_Identifier (Loc, Name_uClean);
1190 Set_Suppress_Elaboration_Warnings (Clean);
1191 Append_To (New_Decls,
1192 Make_Clean (N, Clean, Mark, Flist,
1193 Is_Task,
1194 Is_Master,
1195 Is_Protected,
1196 Is_Task_Allocation,
1197 Is_Asynchronous_Call,
1198 Previous_At_End_Proc));
1200 -- The previous AT END procedure, if any, has been captured in Clean:
1201 -- reset it to Empty now because we check further on that we never
1202 -- overwrite an existing AT END call.
1204 Set_At_End_Proc (Handled_Statement_Sequence (N), Empty);
1206 -- If exception handlers are present, wrap the Sequence of statements in
1207 -- a block because it is not possible to get exception handlers and an
1208 -- AT END call in the same scope.
1210 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1212 -- Preserve end label to provide proper cross-reference information
1214 End_Lab := End_Label (Handled_Statement_Sequence (N));
1215 Blok :=
1216 Make_Block_Statement (Loc,
1217 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
1218 Set_Handled_Statement_Sequence (N,
1219 Make_Handled_Sequence_Of_Statements (Loc, New_List (Blok)));
1220 Set_End_Label (Handled_Statement_Sequence (N), End_Lab);
1221 Wrapped := True;
1223 -- Comment needed here, see RH for 1.306 ???
1225 if Nkind (N) = N_Subprogram_Body then
1226 Set_Has_Nested_Block_With_Handler (Current_Scope);
1227 end if;
1229 -- Otherwise we do not wrap
1231 else
1232 Wrapped := False;
1233 Blok := Empty;
1234 end if;
1236 -- Don't move the _chain Activation_Chain declaration in task
1237 -- allocation blocks. Task allocation blocks use this object
1238 -- in their cleanup handlers, and gigi complains if it is declared
1239 -- in the sequence of statements of the scope that declares the
1240 -- handler.
1242 if Is_Task_Allocation then
1243 Chain := Activation_Chain_Entity (N);
1245 Decl := First (Declarations (N));
1246 while Nkind (Decl) /= N_Object_Declaration
1247 or else Defining_Identifier (Decl) /= Chain
1248 loop
1249 Next (Decl);
1250 pragma Assert (Present (Decl));
1251 end loop;
1253 Remove (Decl);
1254 Prepend_To (New_Decls, Decl);
1255 end if;
1257 -- Now we move the declarations into the Sequence of statements
1258 -- in order to get them protected by the AT END call. It may seem
1259 -- weird to put declarations in the sequence of statement but in
1260 -- fact nothing forbids that at the tree level. We also set the
1261 -- First_Real_Statement field so that we remember where the real
1262 -- statements (i.e. original statements) begin. Note that if we
1263 -- wrapped the statements, the first real statement is inside the
1264 -- inner block. If the First_Real_Statement is already set (as is
1265 -- the case for subprogram bodies that are expansions of task bodies)
1266 -- then do not reset it, because its declarative part would migrate
1267 -- to the statement part.
1269 if not Wrapped then
1270 if No (First_Real_Statement (Handled_Statement_Sequence (N))) then
1271 Set_First_Real_Statement (Handled_Statement_Sequence (N),
1272 First (Statements (Handled_Statement_Sequence (N))));
1273 end if;
1275 else
1276 Set_First_Real_Statement (Handled_Statement_Sequence (N), Blok);
1277 end if;
1279 Append_List_To (Declarations (N),
1280 Statements (Handled_Statement_Sequence (N)));
1281 Set_Statements (Handled_Statement_Sequence (N), Declarations (N));
1283 -- We need to reset the Sloc of the handled statement sequence to
1284 -- properly reflect the new initial "statement" in the sequence.
1286 Set_Sloc
1287 (Handled_Statement_Sequence (N), Sloc (First (Declarations (N))));
1289 -- The declarations of the _Clean procedure and finalization chain
1290 -- replace the old declarations that have been moved inward.
1292 Set_Declarations (N, New_Decls);
1293 Analyze_Declarations (New_Decls);
1295 -- The At_End call is attached to the sequence of statements
1297 declare
1298 HSS : Node_Id;
1300 begin
1301 -- If the construct is a protected subprogram, then the call to
1302 -- the corresponding unprotected subprogram appears in a block which
1303 -- is the last statement in the body, and it is this block that must
1304 -- be covered by the At_End handler.
1306 if Is_Protected then
1307 HSS := Handled_Statement_Sequence
1308 (Last (Statements (Handled_Statement_Sequence (N))));
1309 else
1310 HSS := Handled_Statement_Sequence (N);
1311 end if;
1313 -- Never overwrite an existing AT END call
1315 pragma Assert (No (At_End_Proc (HSS)));
1317 Set_At_End_Proc (HSS, New_Occurrence_Of (Clean, Loc));
1318 Expand_At_End_Handler (HSS, Empty);
1319 end;
1321 -- Restore saved polling mode
1323 Polling_Required := Old_Poll;
1324 end Expand_Cleanup_Actions;
1326 -------------------------------
1327 -- Expand_Ctrl_Function_Call --
1328 -------------------------------
1330 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
1331 Loc : constant Source_Ptr := Sloc (N);
1332 Rtype : constant Entity_Id := Etype (N);
1333 Utype : constant Entity_Id := Underlying_Type (Rtype);
1334 Ref : Node_Id;
1335 Action : Node_Id;
1336 Action2 : Node_Id := Empty;
1338 Attach_Level : Uint := Uint_1;
1339 Len_Ref : Node_Id := Empty;
1341 function Last_Array_Component
1342 (Ref : Node_Id;
1343 Typ : Entity_Id) return Node_Id;
1344 -- Creates a reference to the last component of the array object
1345 -- designated by Ref whose type is Typ.
1347 --------------------------
1348 -- Last_Array_Component --
1349 --------------------------
1351 function Last_Array_Component
1352 (Ref : Node_Id;
1353 Typ : Entity_Id) return Node_Id
1355 Index_List : constant List_Id := New_List;
1357 begin
1358 for N in 1 .. Number_Dimensions (Typ) loop
1359 Append_To (Index_List,
1360 Make_Attribute_Reference (Loc,
1361 Prefix => Duplicate_Subexpr_No_Checks (Ref),
1362 Attribute_Name => Name_Last,
1363 Expressions => New_List (
1364 Make_Integer_Literal (Loc, N))));
1365 end loop;
1367 return
1368 Make_Indexed_Component (Loc,
1369 Prefix => Duplicate_Subexpr (Ref),
1370 Expressions => Index_List);
1371 end Last_Array_Component;
1373 -- Start of processing for Expand_Ctrl_Function_Call
1375 begin
1376 -- Optimization, if the returned value (which is on the sec-stack) is
1377 -- returned again, no need to copy/readjust/finalize, we can just pass
1378 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
1379 -- attachment is needed
1381 if Nkind (Parent (N)) = N_Simple_Return_Statement then
1382 return;
1383 end if;
1385 -- Resolution is now finished, make sure we don't start analysis again
1386 -- because of the duplication.
1388 Set_Analyzed (N);
1389 Ref := Duplicate_Subexpr_No_Checks (N);
1391 -- Now we can generate the Attach Call. Note that this value is always
1392 -- on the (secondary) stack and thus is attached to a singly linked
1393 -- final list:
1395 -- Resx := F (X)'reference;
1396 -- Attach_To_Final_List (_Lx, Resx.all, 1);
1398 -- or when there are controlled components:
1400 -- Attach_To_Final_List (_Lx, Resx._controller, 1);
1402 -- or when it is both Is_Controlled and Has_Controlled_Components:
1404 -- Attach_To_Final_List (_Lx, Resx._controller, 1);
1405 -- Attach_To_Final_List (_Lx, Resx, 1);
1407 -- or if it is an array with Is_Controlled (and Has_Controlled)
1409 -- Attach_To_Final_List (_Lx, Resx (Resx'last), 3);
1411 -- An attach level of 3 means that a whole array is to be attached to
1412 -- the finalization list (including the controlled components).
1414 -- or if it is an array with Has_Controlled_Components but not
1415 -- Is_Controlled:
1417 -- Attach_To_Final_List (_Lx, Resx (Resx'last)._controller, 3);
1419 -- Case where type has controlled components
1421 if Has_Controlled_Component (Rtype) then
1422 declare
1423 T1 : Entity_Id := Rtype;
1424 T2 : Entity_Id := Utype;
1426 begin
1427 if Is_Array_Type (T2) then
1428 Len_Ref :=
1429 Make_Attribute_Reference (Loc,
1430 Prefix =>
1431 Duplicate_Subexpr_Move_Checks
1432 (Unchecked_Convert_To (T2, Ref)),
1433 Attribute_Name => Name_Length);
1434 end if;
1436 while Is_Array_Type (T2) loop
1437 if T1 /= T2 then
1438 Ref := Unchecked_Convert_To (T2, Ref);
1439 end if;
1441 Ref := Last_Array_Component (Ref, T2);
1442 Attach_Level := Uint_3;
1443 T1 := Component_Type (T2);
1444 T2 := Underlying_Type (T1);
1445 end loop;
1447 -- If the type has controlled components, go to the controller
1448 -- except in the case of arrays of controlled objects since in
1449 -- this case objects and their components are already chained
1450 -- and the head of the chain is the last array element.
1452 if Is_Array_Type (Rtype) and then Is_Controlled (T2) then
1453 null;
1455 elsif Has_Controlled_Component (T2) then
1456 if T1 /= T2 then
1457 Ref := Unchecked_Convert_To (T2, Ref);
1458 end if;
1460 Ref :=
1461 Make_Selected_Component (Loc,
1462 Prefix => Ref,
1463 Selector_Name => Make_Identifier (Loc, Name_uController));
1464 end if;
1465 end;
1467 -- Here we know that 'Ref' has a controller so we may as well attach
1468 -- it directly.
1470 Action :=
1471 Make_Attach_Call (
1472 Obj_Ref => Ref,
1473 Flist_Ref => Find_Final_List (Current_Scope),
1474 With_Attach => Make_Integer_Literal (Loc, Attach_Level));
1476 -- If it is also Is_Controlled we need to attach the global object
1478 if Is_Controlled (Rtype) then
1479 Action2 :=
1480 Make_Attach_Call (
1481 Obj_Ref => Duplicate_Subexpr_No_Checks (N),
1482 Flist_Ref => Find_Final_List (Current_Scope),
1483 With_Attach => Make_Integer_Literal (Loc, Attach_Level));
1484 end if;
1486 -- Here, we have a controlled type that does not seem to have controlled
1487 -- components but it could be a class wide type whose further
1488 -- derivations have controlled components. So we don't know if the
1489 -- object itself needs to be attached or if it has a record controller.
1490 -- We need to call a runtime function (Deep_Tag_Attach) which knows what
1491 -- to do thanks to the RC_Offset in the dispatch table.
1493 else
1494 Action :=
1495 Make_Procedure_Call_Statement (Loc,
1496 Name => New_Reference_To (RTE (RE_Deep_Tag_Attach), Loc),
1497 Parameter_Associations => New_List (
1498 Find_Final_List (Current_Scope),
1500 Make_Attribute_Reference (Loc,
1501 Prefix => Ref,
1502 Attribute_Name => Name_Address),
1504 Make_Integer_Literal (Loc, Attach_Level)));
1505 end if;
1507 if Present (Len_Ref) then
1508 Action :=
1509 Make_Implicit_If_Statement (N,
1510 Condition => Make_Op_Gt (Loc,
1511 Left_Opnd => Len_Ref,
1512 Right_Opnd => Make_Integer_Literal (Loc, 0)),
1513 Then_Statements => New_List (Action));
1514 end if;
1516 Insert_Action (N, Action);
1517 if Present (Action2) then
1518 Insert_Action (N, Action2);
1519 end if;
1520 end Expand_Ctrl_Function_Call;
1522 ---------------------------
1523 -- Expand_N_Package_Body --
1524 ---------------------------
1526 -- Add call to Activate_Tasks if body is an activator (actual processing
1527 -- is in chapter 9).
1529 -- Generate subprogram descriptor for elaboration routine
1531 -- Encode entity names in package body
1533 procedure Expand_N_Package_Body (N : Node_Id) is
1534 Ent : constant Entity_Id := Corresponding_Spec (N);
1536 begin
1537 -- This is done only for non-generic packages
1539 if Ekind (Ent) = E_Package then
1540 Push_Scope (Corresponding_Spec (N));
1542 -- Build dispatch tables of library level tagged types
1544 if Is_Library_Level_Entity (Ent) then
1545 Build_Static_Dispatch_Tables (N);
1546 end if;
1548 Build_Task_Activation_Call (N);
1549 Pop_Scope;
1550 end if;
1552 Set_Elaboration_Flag (N, Corresponding_Spec (N));
1553 Set_In_Package_Body (Ent, False);
1555 -- Set to encode entity names in package body before gigi is called
1557 Qualify_Entity_Names (N);
1558 end Expand_N_Package_Body;
1560 ----------------------------------
1561 -- Expand_N_Package_Declaration --
1562 ----------------------------------
1564 -- Add call to Activate_Tasks if there are tasks declared and the package
1565 -- has no body. Note that in Ada83, this may result in premature activation
1566 -- of some tasks, given that we cannot tell whether a body will eventually
1567 -- appear.
1569 procedure Expand_N_Package_Declaration (N : Node_Id) is
1570 Spec : constant Node_Id := Specification (N);
1571 Id : constant Entity_Id := Defining_Entity (N);
1572 Decls : List_Id;
1573 No_Body : Boolean := False;
1574 -- True in the case of a package declaration that is a compilation unit
1575 -- and for which no associated body will be compiled in
1576 -- this compilation.
1578 begin
1579 -- Case of a package declaration other than a compilation unit
1581 if Nkind (Parent (N)) /= N_Compilation_Unit then
1582 null;
1584 -- Case of a compilation unit that does not require a body
1586 elsif not Body_Required (Parent (N))
1587 and then not Unit_Requires_Body (Id)
1588 then
1589 No_Body := True;
1591 -- Special case of generating calling stubs for a remote call interface
1592 -- package: even though the package declaration requires one, the
1593 -- body won't be processed in this compilation (so any stubs for RACWs
1594 -- declared in the package must be generated here, along with the
1595 -- spec).
1597 elsif Parent (N) = Cunit (Main_Unit)
1598 and then Is_Remote_Call_Interface (Id)
1599 and then Distribution_Stub_Mode = Generate_Caller_Stub_Body
1600 then
1601 No_Body := True;
1602 end if;
1604 -- For a package declaration that implies no associated body, generate
1605 -- task activation call and RACW supporting bodies now (since we won't
1606 -- have a specific separate compilation unit for that).
1608 if No_Body then
1609 Push_Scope (Id);
1611 if Has_RACW (Id) then
1613 -- Generate RACW subprogram bodies
1615 Decls := Private_Declarations (Spec);
1617 if No (Decls) then
1618 Decls := Visible_Declarations (Spec);
1619 end if;
1621 if No (Decls) then
1622 Decls := New_List;
1623 Set_Visible_Declarations (Spec, Decls);
1624 end if;
1626 Append_RACW_Bodies (Decls, Id);
1627 Analyze_List (Decls);
1628 end if;
1630 if Present (Activation_Chain_Entity (N)) then
1632 -- Generate task activation call as last step of elaboration
1634 Build_Task_Activation_Call (N);
1635 end if;
1637 Pop_Scope;
1638 end if;
1640 -- Build dispatch tables of library level tagged types
1642 if Is_Compilation_Unit (Id)
1643 or else (Is_Generic_Instance (Id)
1644 and then Is_Library_Level_Entity (Id))
1645 then
1646 Build_Static_Dispatch_Tables (N);
1647 end if;
1649 -- Note: it is not necessary to worry about generating a subprogram
1650 -- descriptor, since the only way to get exception handlers into a
1651 -- package spec is to include instantiations, and that would cause
1652 -- generation of subprogram descriptors to be delayed in any case.
1654 -- Set to encode entity names in package spec before gigi is called
1656 Qualify_Entity_Names (N);
1657 end Expand_N_Package_Declaration;
1659 ---------------------
1660 -- Find_Final_List --
1661 ---------------------
1663 function Find_Final_List
1664 (E : Entity_Id;
1665 Ref : Node_Id := Empty) return Node_Id
1667 Loc : constant Source_Ptr := Sloc (Ref);
1668 S : Entity_Id;
1669 Id : Entity_Id;
1670 R : Node_Id;
1672 begin
1673 -- If the restriction No_Finalization applies, then there's not any
1674 -- finalization list available to return, so return Empty.
1676 if Restriction_Active (No_Finalization) then
1677 return Empty;
1679 -- Case of an internal component. The Final list is the record
1680 -- controller of the enclosing record.
1682 elsif Present (Ref) then
1683 R := Ref;
1684 loop
1685 case Nkind (R) is
1686 when N_Unchecked_Type_Conversion | N_Type_Conversion =>
1687 R := Expression (R);
1689 when N_Indexed_Component | N_Explicit_Dereference =>
1690 R := Prefix (R);
1692 when N_Selected_Component =>
1693 R := Prefix (R);
1694 exit;
1696 when N_Identifier =>
1697 exit;
1699 when others =>
1700 raise Program_Error;
1701 end case;
1702 end loop;
1704 return
1705 Make_Selected_Component (Loc,
1706 Prefix =>
1707 Make_Selected_Component (Loc,
1708 Prefix => R,
1709 Selector_Name => Make_Identifier (Loc, Name_uController)),
1710 Selector_Name => Make_Identifier (Loc, Name_F));
1712 -- Case of a dynamically allocated object whose access type has an
1713 -- Associated_Final_Chain. The final list is the corresponding list
1714 -- controller (the next entity in the scope of the access type with
1715 -- the right type). If the type comes from a With_Type clause, no
1716 -- controller was created, we use the global chain instead. (The code
1717 -- related to with_type clauses should presumably be removed at some
1718 -- point since that feature is obsolete???)
1720 -- An anonymous access type either has a list created for it when the
1721 -- allocator is a for an access parameter or an access discriminant,
1722 -- or else it uses the list of the enclosing dynamic scope, when the
1723 -- context is a declaration or an assignment.
1725 elsif Is_Access_Type (E)
1726 and then (Present (Associated_Final_Chain (E))
1727 or else From_With_Type (E))
1728 then
1729 if From_With_Type (E) then
1730 return New_Reference_To (RTE (RE_Global_Final_List), Sloc (E));
1732 -- Use the access type's associated finalization chain
1734 else
1735 return
1736 Make_Selected_Component (Loc,
1737 Prefix =>
1738 New_Reference_To
1739 (Associated_Final_Chain (Base_Type (E)), Loc),
1740 Selector_Name => Make_Identifier (Loc, Name_F));
1741 end if;
1743 else
1744 if Is_Dynamic_Scope (E) then
1745 S := E;
1746 else
1747 S := Enclosing_Dynamic_Scope (E);
1748 end if;
1750 -- When the finalization chain entity is 'Error', it means that there
1751 -- should not be any chain at that level and that the enclosing one
1752 -- should be used.
1754 -- This is a nasty kludge, see ??? note in exp_ch11
1756 while Finalization_Chain_Entity (S) = Error loop
1757 S := Enclosing_Dynamic_Scope (S);
1758 end loop;
1760 if S = Standard_Standard then
1761 return New_Reference_To (RTE (RE_Global_Final_List), Sloc (E));
1762 else
1763 if No (Finalization_Chain_Entity (S)) then
1765 -- In the case where the scope is a subprogram, retrieve the
1766 -- Sloc of subprogram's body for association with the chain,
1767 -- since using the Sloc of the spec would be confusing during
1768 -- source-line stepping within the debugger.
1770 declare
1771 Flist_Loc : Source_Ptr := Sloc (S);
1772 Subp_Body : Node_Id;
1774 begin
1775 if Ekind (S) in Subprogram_Kind then
1776 Subp_Body := Unit_Declaration_Node (S);
1778 if Nkind (Subp_Body) /= N_Subprogram_Body then
1779 Subp_Body := Corresponding_Body (Subp_Body);
1780 end if;
1782 if Present (Subp_Body) then
1783 Flist_Loc := Sloc (Subp_Body);
1784 end if;
1785 end if;
1787 Id :=
1788 Make_Defining_Identifier (Flist_Loc,
1789 Chars => New_Internal_Name ('F'));
1790 end;
1792 Set_Finalization_Chain_Entity (S, Id);
1794 -- Set momentarily some semantics attributes to allow normal
1795 -- analysis of expansions containing references to this chain.
1796 -- Will be fully decorated during the expansion of the scope
1797 -- itself.
1799 Set_Ekind (Id, E_Variable);
1800 Set_Etype (Id, RTE (RE_Finalizable_Ptr));
1801 end if;
1803 return New_Reference_To (Finalization_Chain_Entity (S), Sloc (E));
1804 end if;
1805 end if;
1806 end Find_Final_List;
1808 -----------------------------
1809 -- Find_Node_To_Be_Wrapped --
1810 -----------------------------
1812 function Find_Node_To_Be_Wrapped (N : Node_Id) return Node_Id is
1813 P : Node_Id;
1814 The_Parent : Node_Id;
1816 begin
1817 The_Parent := N;
1818 loop
1819 P := The_Parent;
1820 pragma Assert (P /= Empty);
1821 The_Parent := Parent (P);
1823 case Nkind (The_Parent) is
1825 -- Simple statement can be wrapped
1827 when N_Pragma =>
1828 return The_Parent;
1830 -- Usually assignments are good candidate for wrapping
1831 -- except when they have been generated as part of a
1832 -- controlled aggregate where the wrapping should take
1833 -- place more globally.
1835 when N_Assignment_Statement =>
1836 if No_Ctrl_Actions (The_Parent) then
1837 null;
1838 else
1839 return The_Parent;
1840 end if;
1842 -- An entry call statement is a special case if it occurs in
1843 -- the context of a Timed_Entry_Call. In this case we wrap
1844 -- the entire timed entry call.
1846 when N_Entry_Call_Statement |
1847 N_Procedure_Call_Statement =>
1848 if Nkind (Parent (The_Parent)) = N_Entry_Call_Alternative
1849 and then Nkind_In (Parent (Parent (The_Parent)),
1850 N_Timed_Entry_Call,
1851 N_Conditional_Entry_Call)
1852 then
1853 return Parent (Parent (The_Parent));
1854 else
1855 return The_Parent;
1856 end if;
1858 -- Object declarations are also a boundary for the transient scope
1859 -- even if they are not really wrapped
1860 -- (see Wrap_Transient_Declaration)
1862 when N_Object_Declaration |
1863 N_Object_Renaming_Declaration |
1864 N_Subtype_Declaration =>
1865 return The_Parent;
1867 -- The expression itself is to be wrapped if its parent is a
1868 -- compound statement or any other statement where the expression
1869 -- is known to be scalar
1871 when N_Accept_Alternative |
1872 N_Attribute_Definition_Clause |
1873 N_Case_Statement |
1874 N_Code_Statement |
1875 N_Delay_Alternative |
1876 N_Delay_Until_Statement |
1877 N_Delay_Relative_Statement |
1878 N_Discriminant_Association |
1879 N_Elsif_Part |
1880 N_Entry_Body_Formal_Part |
1881 N_Exit_Statement |
1882 N_If_Statement |
1883 N_Iteration_Scheme |
1884 N_Terminate_Alternative =>
1885 return P;
1887 when N_Attribute_Reference =>
1889 if Is_Procedure_Attribute_Name
1890 (Attribute_Name (The_Parent))
1891 then
1892 return The_Parent;
1893 end if;
1895 -- A raise statement can be wrapped. This will arise when the
1896 -- expression in a raise_with_expression uses the secondary
1897 -- stack, for example.
1899 when N_Raise_Statement =>
1900 return The_Parent;
1902 -- If the expression is within the iteration scheme of a loop,
1903 -- we must create a declaration for it, followed by an assignment
1904 -- in order to have a usable statement to wrap.
1906 when N_Loop_Parameter_Specification =>
1907 return Parent (The_Parent);
1909 -- The following nodes contains "dummy calls" which don't
1910 -- need to be wrapped.
1912 when N_Parameter_Specification |
1913 N_Discriminant_Specification |
1914 N_Component_Declaration =>
1915 return Empty;
1917 -- The return statement is not to be wrapped when the function
1918 -- itself needs wrapping at the outer-level
1920 when N_Simple_Return_Statement =>
1921 declare
1922 Applies_To : constant Entity_Id :=
1923 Return_Applies_To
1924 (Return_Statement_Entity (The_Parent));
1925 Return_Type : constant Entity_Id := Etype (Applies_To);
1926 begin
1927 if Requires_Transient_Scope (Return_Type) then
1928 return Empty;
1929 else
1930 return The_Parent;
1931 end if;
1932 end;
1934 -- If we leave a scope without having been able to find a node to
1935 -- wrap, something is going wrong but this can happen in error
1936 -- situation that are not detected yet (such as a dynamic string
1937 -- in a pragma export)
1939 when N_Subprogram_Body |
1940 N_Package_Declaration |
1941 N_Package_Body |
1942 N_Block_Statement =>
1943 return Empty;
1945 -- otherwise continue the search
1947 when others =>
1948 null;
1949 end case;
1950 end loop;
1951 end Find_Node_To_Be_Wrapped;
1953 ----------------------
1954 -- Global_Flist_Ref --
1955 ----------------------
1957 function Global_Flist_Ref (Flist_Ref : Node_Id) return Boolean is
1958 Flist : Entity_Id;
1960 begin
1961 -- Look for the Global_Final_List
1963 if Is_Entity_Name (Flist_Ref) then
1964 Flist := Entity (Flist_Ref);
1966 -- Look for the final list associated with an access to controlled
1968 elsif Nkind (Flist_Ref) = N_Selected_Component
1969 and then Is_Entity_Name (Prefix (Flist_Ref))
1970 then
1971 Flist := Entity (Prefix (Flist_Ref));
1972 else
1973 return False;
1974 end if;
1976 return Present (Flist)
1977 and then Present (Scope (Flist))
1978 and then Enclosing_Dynamic_Scope (Flist) = Standard_Standard;
1979 end Global_Flist_Ref;
1981 ----------------------------------
1982 -- Has_New_Controlled_Component --
1983 ----------------------------------
1985 function Has_New_Controlled_Component (E : Entity_Id) return Boolean is
1986 Comp : Entity_Id;
1988 begin
1989 if not Is_Tagged_Type (E) then
1990 return Has_Controlled_Component (E);
1991 elsif not Is_Derived_Type (E) then
1992 return Has_Controlled_Component (E);
1993 end if;
1995 Comp := First_Component (E);
1996 while Present (Comp) loop
1998 if Chars (Comp) = Name_uParent then
1999 null;
2001 elsif Scope (Original_Record_Component (Comp)) = E
2002 and then Needs_Finalization (Etype (Comp))
2003 then
2004 return True;
2005 end if;
2007 Next_Component (Comp);
2008 end loop;
2010 return False;
2011 end Has_New_Controlled_Component;
2013 --------------------------
2014 -- In_Finalization_Root --
2015 --------------------------
2017 -- It would seem simpler to test Scope (RTE (RE_Root_Controlled)) but
2018 -- the purpose of this function is to avoid a circular call to Rtsfind
2019 -- which would been caused by such a test.
2021 function In_Finalization_Root (E : Entity_Id) return Boolean is
2022 S : constant Entity_Id := Scope (E);
2024 begin
2025 return Chars (Scope (S)) = Name_System
2026 and then Chars (S) = Name_Finalization_Root
2027 and then Scope (Scope (S)) = Standard_Standard;
2028 end In_Finalization_Root;
2030 ------------------------------------
2031 -- Insert_Actions_In_Scope_Around --
2032 ------------------------------------
2034 procedure Insert_Actions_In_Scope_Around (N : Node_Id) is
2035 SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
2036 Target : Node_Id;
2038 begin
2039 -- If the node to be wrapped is the triggering statement of an
2040 -- asynchronous select, it is not part of a statement list. The
2041 -- actions must be inserted before the Select itself, which is
2042 -- part of some list of statements. Note that the triggering
2043 -- alternative includes the triggering statement and an optional
2044 -- statement list. If the node to be wrapped is part of that list,
2045 -- the normal insertion applies.
2047 if Nkind (Parent (Node_To_Be_Wrapped)) = N_Triggering_Alternative
2048 and then not Is_List_Member (Node_To_Be_Wrapped)
2049 then
2050 Target := Parent (Parent (Node_To_Be_Wrapped));
2051 else
2052 Target := N;
2053 end if;
2055 if Present (SE.Actions_To_Be_Wrapped_Before) then
2056 Insert_List_Before (Target, SE.Actions_To_Be_Wrapped_Before);
2057 SE.Actions_To_Be_Wrapped_Before := No_List;
2058 end if;
2060 if Present (SE.Actions_To_Be_Wrapped_After) then
2061 Insert_List_After (Target, SE.Actions_To_Be_Wrapped_After);
2062 SE.Actions_To_Be_Wrapped_After := No_List;
2063 end if;
2064 end Insert_Actions_In_Scope_Around;
2066 -----------------------
2067 -- Make_Adjust_Call --
2068 -----------------------
2070 function Make_Adjust_Call
2071 (Ref : Node_Id;
2072 Typ : Entity_Id;
2073 Flist_Ref : Node_Id;
2074 With_Attach : Node_Id;
2075 Allocator : Boolean := False) return List_Id
2077 Loc : constant Source_Ptr := Sloc (Ref);
2078 Res : constant List_Id := New_List;
2079 Utyp : Entity_Id;
2080 Proc : Entity_Id;
2081 Cref : Node_Id := Ref;
2082 Cref2 : Node_Id;
2083 Attach : Node_Id := With_Attach;
2085 begin
2086 if Is_Class_Wide_Type (Typ) then
2087 Utyp := Underlying_Type (Base_Type (Root_Type (Typ)));
2088 else
2089 Utyp := Underlying_Type (Base_Type (Typ));
2090 end if;
2092 Set_Assignment_OK (Cref);
2094 -- Deal with non-tagged derivation of private views
2096 if Is_Untagged_Derivation (Typ) then
2097 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
2098 Cref := Unchecked_Convert_To (Utyp, Cref);
2099 Set_Assignment_OK (Cref);
2100 -- To prevent problems with UC see 1.156 RH ???
2101 end if;
2103 -- If the underlying_type is a subtype, we are dealing with
2104 -- the completion of a private type. We need to access
2105 -- the base type and generate a conversion to it.
2107 if Utyp /= Base_Type (Utyp) then
2108 pragma Assert (Is_Private_Type (Typ));
2109 Utyp := Base_Type (Utyp);
2110 Cref := Unchecked_Convert_To (Utyp, Cref);
2111 end if;
2113 -- If the object is unanalyzed, set its expected type for use
2114 -- in Convert_View in case an additional conversion is needed.
2116 if No (Etype (Cref))
2117 and then Nkind (Cref) /= N_Unchecked_Type_Conversion
2118 then
2119 Set_Etype (Cref, Typ);
2120 end if;
2122 -- We do not need to attach to one of the Global Final Lists
2123 -- the objects whose type is Finalize_Storage_Only
2125 if Finalize_Storage_Only (Typ)
2126 and then (Global_Flist_Ref (Flist_Ref)
2127 or else Entity (Constant_Value (RTE (RE_Garbage_Collected)))
2128 = Standard_True)
2129 then
2130 Attach := Make_Integer_Literal (Loc, 0);
2131 end if;
2133 -- Special case for allocators: need initialization of the chain
2134 -- pointers. For the 0 case, reset them to null.
2136 if Allocator then
2137 pragma Assert (Nkind (Attach) = N_Integer_Literal);
2139 if Intval (Attach) = 0 then
2140 Set_Intval (Attach, Uint_4);
2141 end if;
2142 end if;
2144 -- Generate:
2145 -- Deep_Adjust (Flist_Ref, Ref, Attach);
2147 if Has_Controlled_Component (Utyp)
2148 or else Is_Class_Wide_Type (Typ)
2149 then
2150 if Is_Tagged_Type (Utyp) then
2151 Proc := Find_Prim_Op (Utyp, TSS_Deep_Adjust);
2153 else
2154 Proc := TSS (Utyp, TSS_Deep_Adjust);
2155 end if;
2157 Cref := Convert_View (Proc, Cref, 2);
2159 Append_To (Res,
2160 Make_Procedure_Call_Statement (Loc,
2161 Name => New_Reference_To (Proc, Loc),
2162 Parameter_Associations =>
2163 New_List (Flist_Ref, Cref, Attach)));
2165 -- Generate:
2166 -- if With_Attach then
2167 -- Attach_To_Final_List (Ref, Flist_Ref);
2168 -- end if;
2169 -- Adjust (Ref);
2171 else -- Is_Controlled (Utyp)
2173 Proc := Find_Prim_Op (Utyp, Name_Of (Adjust_Case));
2174 Cref := Convert_View (Proc, Cref);
2175 Cref2 := New_Copy_Tree (Cref);
2177 Append_To (Res,
2178 Make_Procedure_Call_Statement (Loc,
2179 Name => New_Reference_To (Proc, Loc),
2180 Parameter_Associations => New_List (Cref2)));
2182 Append_To (Res, Make_Attach_Call (Cref, Flist_Ref, Attach));
2183 end if;
2185 return Res;
2186 end Make_Adjust_Call;
2188 ----------------------
2189 -- Make_Attach_Call --
2190 ----------------------
2192 -- Generate:
2193 -- System.FI.Attach_To_Final_List (Flist, Ref, Nb_Link)
2195 function Make_Attach_Call
2196 (Obj_Ref : Node_Id;
2197 Flist_Ref : Node_Id;
2198 With_Attach : Node_Id) return Node_Id
2200 Loc : constant Source_Ptr := Sloc (Obj_Ref);
2202 begin
2203 -- Optimization: If the number of links is statically '0', don't
2204 -- call the attach_proc.
2206 if Nkind (With_Attach) = N_Integer_Literal
2207 and then Intval (With_Attach) = Uint_0
2208 then
2209 return Make_Null_Statement (Loc);
2210 end if;
2212 return
2213 Make_Procedure_Call_Statement (Loc,
2214 Name => New_Reference_To (RTE (RE_Attach_To_Final_List), Loc),
2215 Parameter_Associations => New_List (
2216 Flist_Ref,
2217 OK_Convert_To (RTE (RE_Finalizable), Obj_Ref),
2218 With_Attach));
2219 end Make_Attach_Call;
2221 ----------------
2222 -- Make_Clean --
2223 ----------------
2225 function Make_Clean
2226 (N : Node_Id;
2227 Clean : Entity_Id;
2228 Mark : Entity_Id;
2229 Flist : Entity_Id;
2230 Is_Task : Boolean;
2231 Is_Master : Boolean;
2232 Is_Protected_Subprogram : Boolean;
2233 Is_Task_Allocation_Block : Boolean;
2234 Is_Asynchronous_Call_Block : Boolean;
2235 Chained_Cleanup_Action : Node_Id) return Node_Id
2237 Loc : constant Source_Ptr := Sloc (Clean);
2238 Stmt : constant List_Id := New_List;
2240 Sbody : Node_Id;
2241 Spec : Node_Id;
2242 Name : Node_Id;
2243 Param : Node_Id;
2244 Param_Type : Entity_Id;
2245 Pid : Entity_Id := Empty;
2246 Cancel_Param : Entity_Id;
2248 begin
2249 if Is_Task then
2250 if Restricted_Profile then
2251 Append_To
2252 (Stmt, Build_Runtime_Call (Loc, RE_Complete_Restricted_Task));
2253 else
2254 Append_To (Stmt, Build_Runtime_Call (Loc, RE_Complete_Task));
2255 end if;
2257 elsif Is_Master then
2258 if Restriction_Active (No_Task_Hierarchy) = False then
2259 Append_To (Stmt, Build_Runtime_Call (Loc, RE_Complete_Master));
2260 end if;
2262 elsif Is_Protected_Subprogram then
2264 -- Add statements to the cleanup handler of the (ordinary)
2265 -- subprogram expanded to implement a protected subprogram,
2266 -- unlocking the protected object parameter and undeferring abort.
2267 -- If this is a protected procedure, and the object contains
2268 -- entries, this also calls the entry service routine.
2270 -- NOTE: This cleanup handler references _object, a parameter
2271 -- to the procedure.
2273 -- Find the _object parameter representing the protected object
2275 Spec := Parent (Corresponding_Spec (N));
2277 Param := First (Parameter_Specifications (Spec));
2278 loop
2279 Param_Type := Etype (Parameter_Type (Param));
2281 if Ekind (Param_Type) = E_Record_Type then
2282 Pid := Corresponding_Concurrent_Type (Param_Type);
2283 end if;
2285 exit when No (Param) or else Present (Pid);
2286 Next (Param);
2287 end loop;
2289 pragma Assert (Present (Param));
2291 -- If the associated protected object declares entries,
2292 -- a protected procedure has to service entry queues.
2293 -- In this case, add
2295 -- Service_Entries (_object._object'Access);
2297 -- _object is the record used to implement the protected object.
2298 -- It is a parameter to the protected subprogram.
2300 if Nkind (Specification (N)) = N_Procedure_Specification
2301 and then Has_Entries (Pid)
2302 then
2303 case Corresponding_Runtime_Package (Pid) is
2304 when System_Tasking_Protected_Objects_Entries =>
2305 Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
2307 when System_Tasking_Protected_Objects_Single_Entry =>
2308 Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
2310 when others =>
2311 raise Program_Error;
2312 end case;
2314 Append_To (Stmt,
2315 Make_Procedure_Call_Statement (Loc,
2316 Name => Name,
2317 Parameter_Associations => New_List (
2318 Make_Attribute_Reference (Loc,
2319 Prefix =>
2320 Make_Selected_Component (Loc,
2321 Prefix => New_Reference_To (
2322 Defining_Identifier (Param), Loc),
2323 Selector_Name =>
2324 Make_Identifier (Loc, Name_uObject)),
2325 Attribute_Name => Name_Unchecked_Access))));
2327 else
2328 -- Unlock (_object._object'Access);
2330 -- object is the record used to implement the protected object.
2331 -- It is a parameter to the protected subprogram.
2333 case Corresponding_Runtime_Package (Pid) is
2334 when System_Tasking_Protected_Objects_Entries =>
2335 Name := New_Reference_To (RTE (RE_Unlock_Entries), Loc);
2337 when System_Tasking_Protected_Objects_Single_Entry =>
2338 Name := New_Reference_To (RTE (RE_Unlock_Entry), Loc);
2340 when System_Tasking_Protected_Objects =>
2341 Name := New_Reference_To (RTE (RE_Unlock), Loc);
2343 when others =>
2344 raise Program_Error;
2345 end case;
2347 Append_To (Stmt,
2348 Make_Procedure_Call_Statement (Loc,
2349 Name => Name,
2350 Parameter_Associations => New_List (
2351 Make_Attribute_Reference (Loc,
2352 Prefix =>
2353 Make_Selected_Component (Loc,
2354 Prefix =>
2355 New_Reference_To (Defining_Identifier (Param), Loc),
2356 Selector_Name =>
2357 Make_Identifier (Loc, Name_uObject)),
2358 Attribute_Name => Name_Unchecked_Access))));
2359 end if;
2361 if Abort_Allowed then
2363 -- Abort_Undefer;
2365 Append_To (Stmt,
2366 Make_Procedure_Call_Statement (Loc,
2367 Name =>
2368 New_Reference_To (
2369 RTE (RE_Abort_Undefer), Loc),
2370 Parameter_Associations => Empty_List));
2371 end if;
2373 elsif Is_Task_Allocation_Block then
2375 -- Add a call to Expunge_Unactivated_Tasks to the cleanup
2376 -- handler of a block created for the dynamic allocation of
2377 -- tasks:
2379 -- Expunge_Unactivated_Tasks (_chain);
2381 -- where _chain is the list of tasks created by the allocator
2382 -- but not yet activated. This list will be empty unless
2383 -- the block completes abnormally.
2385 -- This only applies to dynamically allocated tasks;
2386 -- other unactivated tasks are completed by Complete_Task or
2387 -- Complete_Master.
2389 -- NOTE: This cleanup handler references _chain, a local
2390 -- object.
2392 Append_To (Stmt,
2393 Make_Procedure_Call_Statement (Loc,
2394 Name =>
2395 New_Reference_To (
2396 RTE (RE_Expunge_Unactivated_Tasks), Loc),
2397 Parameter_Associations => New_List (
2398 New_Reference_To (Activation_Chain_Entity (N), Loc))));
2400 elsif Is_Asynchronous_Call_Block then
2402 -- Add a call to attempt to cancel the asynchronous entry call
2403 -- whenever the block containing the abortable part is exited.
2405 -- NOTE: This cleanup handler references C, a local object
2407 -- Get the argument to the Cancel procedure
2408 Cancel_Param := Entry_Cancel_Parameter (Entity (Identifier (N)));
2410 -- If it is of type Communication_Block, this must be a
2411 -- protected entry call.
2413 if Is_RTE (Etype (Cancel_Param), RE_Communication_Block) then
2415 Append_To (Stmt,
2417 -- if Enqueued (Cancel_Parameter) then
2419 Make_Implicit_If_Statement (Clean,
2420 Condition => Make_Function_Call (Loc,
2421 Name => New_Reference_To (
2422 RTE (RE_Enqueued), Loc),
2423 Parameter_Associations => New_List (
2424 New_Reference_To (Cancel_Param, Loc))),
2425 Then_Statements => New_List (
2427 -- Cancel_Protected_Entry_Call (Cancel_Param);
2429 Make_Procedure_Call_Statement (Loc,
2430 Name => New_Reference_To (
2431 RTE (RE_Cancel_Protected_Entry_Call), Loc),
2432 Parameter_Associations => New_List (
2433 New_Reference_To (Cancel_Param, Loc))))));
2435 -- Asynchronous delay
2437 elsif Is_RTE (Etype (Cancel_Param), RE_Delay_Block) then
2438 Append_To (Stmt,
2439 Make_Procedure_Call_Statement (Loc,
2440 Name => New_Reference_To (RTE (RE_Cancel_Async_Delay), Loc),
2441 Parameter_Associations => New_List (
2442 Make_Attribute_Reference (Loc,
2443 Prefix => New_Reference_To (Cancel_Param, Loc),
2444 Attribute_Name => Name_Unchecked_Access))));
2446 -- Task entry call
2448 else
2449 -- Append call to Cancel_Task_Entry_Call (C);
2451 Append_To (Stmt,
2452 Make_Procedure_Call_Statement (Loc,
2453 Name => New_Reference_To (
2454 RTE (RE_Cancel_Task_Entry_Call),
2455 Loc),
2456 Parameter_Associations => New_List (
2457 New_Reference_To (Cancel_Param, Loc))));
2459 end if;
2460 end if;
2462 if Present (Flist) then
2463 Append_To (Stmt,
2464 Make_Procedure_Call_Statement (Loc,
2465 Name => New_Reference_To (RTE (RE_Finalize_List), Loc),
2466 Parameter_Associations => New_List (
2467 New_Reference_To (Flist, Loc))));
2468 end if;
2470 if Present (Mark) then
2471 Append_To (Stmt,
2472 Make_Procedure_Call_Statement (Loc,
2473 Name => New_Reference_To (RTE (RE_SS_Release), Loc),
2474 Parameter_Associations => New_List (
2475 New_Reference_To (Mark, Loc))));
2476 end if;
2478 if Present (Chained_Cleanup_Action) then
2479 Append_To (Stmt,
2480 Make_Procedure_Call_Statement (Loc,
2481 Name => Chained_Cleanup_Action));
2482 end if;
2484 Sbody :=
2485 Make_Subprogram_Body (Loc,
2486 Specification =>
2487 Make_Procedure_Specification (Loc,
2488 Defining_Unit_Name => Clean),
2490 Declarations => New_List,
2492 Handled_Statement_Sequence =>
2493 Make_Handled_Sequence_Of_Statements (Loc,
2494 Statements => Stmt));
2496 if Present (Flist) or else Is_Task or else Is_Master then
2497 Wrap_Cleanup_Procedure (Sbody);
2498 end if;
2500 -- We do not want debug information for _Clean routines,
2501 -- since it just confuses the debugging operation unless
2502 -- we are debugging generated code.
2504 if not Debug_Generated_Code then
2505 Set_Debug_Info_Off (Clean, True);
2506 end if;
2508 return Sbody;
2509 end Make_Clean;
2511 --------------------------
2512 -- Make_Deep_Array_Body --
2513 --------------------------
2515 -- Array components are initialized and adjusted in the normal order
2516 -- and finalized in the reverse order. Exceptions are handled and
2517 -- Program_Error is re-raise in the Adjust and Finalize case
2518 -- (RM 7.6.1(12)). Generate the following code :
2520 -- procedure Deep_<P> -- with <P> being Initialize or Adjust or Finalize
2521 -- (L : in out Finalizable_Ptr;
2522 -- V : in out Typ)
2523 -- is
2524 -- begin
2525 -- for J1 in Typ'First (1) .. Typ'Last (1) loop
2526 -- ^ reverse ^ -- in the finalization case
2527 -- ...
2528 -- for J2 in Typ'First (n) .. Typ'Last (n) loop
2529 -- Make_<P>_Call (Typ, V (J1, .. , Jn), L, V);
2530 -- end loop;
2531 -- ...
2532 -- end loop;
2533 -- exception -- not in the
2534 -- when others => raise Program_Error; -- Initialize case
2535 -- end Deep_<P>;
2537 function Make_Deep_Array_Body
2538 (Prim : Final_Primitives;
2539 Typ : Entity_Id) return List_Id
2541 Loc : constant Source_Ptr := Sloc (Typ);
2543 Index_List : constant List_Id := New_List;
2544 -- Stores the list of references to the indexes (one per dimension)
2546 function One_Component return List_Id;
2547 -- Create one statement to initialize/adjust/finalize one array
2548 -- component, designated by a full set of indices.
2550 function One_Dimension (N : Int) return List_Id;
2551 -- Create loop to deal with one dimension of the array. The single
2552 -- statement in the body of the loop initializes the inner dimensions if
2553 -- any, or else a single component.
2555 -------------------
2556 -- One_Component --
2557 -------------------
2559 function One_Component return List_Id is
2560 Comp_Typ : constant Entity_Id := Component_Type (Typ);
2561 Comp_Ref : constant Node_Id :=
2562 Make_Indexed_Component (Loc,
2563 Prefix => Make_Identifier (Loc, Name_V),
2564 Expressions => Index_List);
2566 begin
2567 -- Set the etype of the component Reference, which is used to
2568 -- determine whether a conversion to a parent type is needed.
2570 Set_Etype (Comp_Ref, Comp_Typ);
2572 case Prim is
2573 when Initialize_Case =>
2574 return Make_Init_Call (Comp_Ref, Comp_Typ,
2575 Make_Identifier (Loc, Name_L),
2576 Make_Identifier (Loc, Name_B));
2578 when Adjust_Case =>
2579 return Make_Adjust_Call (Comp_Ref, Comp_Typ,
2580 Make_Identifier (Loc, Name_L),
2581 Make_Identifier (Loc, Name_B));
2583 when Finalize_Case =>
2584 return Make_Final_Call (Comp_Ref, Comp_Typ,
2585 Make_Identifier (Loc, Name_B));
2586 end case;
2587 end One_Component;
2589 -------------------
2590 -- One_Dimension --
2591 -------------------
2593 function One_Dimension (N : Int) return List_Id is
2594 Index : Entity_Id;
2596 begin
2597 if N > Number_Dimensions (Typ) then
2598 return One_Component;
2600 else
2601 Index :=
2602 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
2604 Append_To (Index_List, New_Reference_To (Index, Loc));
2606 return New_List (
2607 Make_Implicit_Loop_Statement (Typ,
2608 Identifier => Empty,
2609 Iteration_Scheme =>
2610 Make_Iteration_Scheme (Loc,
2611 Loop_Parameter_Specification =>
2612 Make_Loop_Parameter_Specification (Loc,
2613 Defining_Identifier => Index,
2614 Discrete_Subtype_Definition =>
2615 Make_Attribute_Reference (Loc,
2616 Prefix => Make_Identifier (Loc, Name_V),
2617 Attribute_Name => Name_Range,
2618 Expressions => New_List (
2619 Make_Integer_Literal (Loc, N))),
2620 Reverse_Present => Prim = Finalize_Case)),
2621 Statements => One_Dimension (N + 1)));
2622 end if;
2623 end One_Dimension;
2625 -- Start of processing for Make_Deep_Array_Body
2627 begin
2628 return One_Dimension (1);
2629 end Make_Deep_Array_Body;
2631 --------------------
2632 -- Make_Deep_Proc --
2633 --------------------
2635 -- Generate:
2636 -- procedure DEEP_<prim>
2637 -- (L : IN OUT Finalizable_Ptr; -- not for Finalize
2638 -- V : IN OUT <typ>;
2639 -- B : IN Short_Short_Integer) is
2640 -- begin
2641 -- <stmts>;
2642 -- exception -- Finalize and Adjust Cases only
2643 -- raise Program_Error; -- idem
2644 -- end DEEP_<prim>;
2646 function Make_Deep_Proc
2647 (Prim : Final_Primitives;
2648 Typ : Entity_Id;
2649 Stmts : List_Id) return Entity_Id
2651 Loc : constant Source_Ptr := Sloc (Typ);
2652 Formals : List_Id;
2653 Proc_Name : Entity_Id;
2654 Handler : List_Id := No_List;
2655 Type_B : Entity_Id;
2657 begin
2658 if Prim = Finalize_Case then
2659 Formals := New_List;
2660 Type_B := Standard_Boolean;
2662 else
2663 Formals := New_List (
2664 Make_Parameter_Specification (Loc,
2665 Defining_Identifier => Make_Defining_Identifier (Loc, Name_L),
2666 In_Present => True,
2667 Out_Present => True,
2668 Parameter_Type =>
2669 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
2670 Type_B := Standard_Short_Short_Integer;
2671 end if;
2673 Append_To (Formals,
2674 Make_Parameter_Specification (Loc,
2675 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
2676 In_Present => True,
2677 Out_Present => True,
2678 Parameter_Type => New_Reference_To (Typ, Loc)));
2680 Append_To (Formals,
2681 Make_Parameter_Specification (Loc,
2682 Defining_Identifier => Make_Defining_Identifier (Loc, Name_B),
2683 Parameter_Type => New_Reference_To (Type_B, Loc)));
2685 if Prim = Finalize_Case or else Prim = Adjust_Case then
2686 Handler := New_List (Make_Handler_For_Ctrl_Operation (Loc));
2687 end if;
2689 Proc_Name :=
2690 Make_Defining_Identifier (Loc,
2691 Chars => Make_TSS_Name (Typ, Deep_Name_Of (Prim)));
2693 Discard_Node (
2694 Make_Subprogram_Body (Loc,
2695 Specification =>
2696 Make_Procedure_Specification (Loc,
2697 Defining_Unit_Name => Proc_Name,
2698 Parameter_Specifications => Formals),
2700 Declarations => Empty_List,
2701 Handled_Statement_Sequence =>
2702 Make_Handled_Sequence_Of_Statements (Loc,
2703 Statements => Stmts,
2704 Exception_Handlers => Handler)));
2706 return Proc_Name;
2707 end Make_Deep_Proc;
2709 ---------------------------
2710 -- Make_Deep_Record_Body --
2711 ---------------------------
2713 -- The Deep procedures call the appropriate Controlling proc on the
2714 -- the controller component. In the init case, it also attach the
2715 -- controller to the current finalization list.
2717 function Make_Deep_Record_Body
2718 (Prim : Final_Primitives;
2719 Typ : Entity_Id) return List_Id
2721 Loc : constant Source_Ptr := Sloc (Typ);
2722 Controller_Typ : Entity_Id;
2723 Obj_Ref : constant Node_Id := Make_Identifier (Loc, Name_V);
2724 Controller_Ref : constant Node_Id :=
2725 Make_Selected_Component (Loc,
2726 Prefix => Obj_Ref,
2727 Selector_Name =>
2728 Make_Identifier (Loc, Name_uController));
2729 Res : constant List_Id := New_List;
2731 begin
2732 if Is_Inherently_Limited_Type (Typ) then
2733 Controller_Typ := RTE (RE_Limited_Record_Controller);
2734 else
2735 Controller_Typ := RTE (RE_Record_Controller);
2736 end if;
2738 case Prim is
2739 when Initialize_Case =>
2740 Append_List_To (Res,
2741 Make_Init_Call (
2742 Ref => Controller_Ref,
2743 Typ => Controller_Typ,
2744 Flist_Ref => Make_Identifier (Loc, Name_L),
2745 With_Attach => Make_Identifier (Loc, Name_B)));
2747 -- When the type is also a controlled type by itself,
2748 -- initialize it and attach it to the finalization chain.
2750 if Is_Controlled (Typ) then
2751 Append_To (Res,
2752 Make_Procedure_Call_Statement (Loc,
2753 Name => New_Reference_To (
2754 Find_Prim_Op (Typ, Name_Of (Prim)), Loc),
2755 Parameter_Associations =>
2756 New_List (New_Copy_Tree (Obj_Ref))));
2758 Append_To (Res, Make_Attach_Call (
2759 Obj_Ref => New_Copy_Tree (Obj_Ref),
2760 Flist_Ref => Make_Identifier (Loc, Name_L),
2761 With_Attach => Make_Identifier (Loc, Name_B)));
2762 end if;
2764 when Adjust_Case =>
2765 Append_List_To (Res,
2766 Make_Adjust_Call (Controller_Ref, Controller_Typ,
2767 Make_Identifier (Loc, Name_L),
2768 Make_Identifier (Loc, Name_B)));
2770 -- When the type is also a controlled type by itself,
2771 -- adjust it and attach it to the finalization chain.
2773 if Is_Controlled (Typ) then
2774 Append_To (Res,
2775 Make_Procedure_Call_Statement (Loc,
2776 Name => New_Reference_To (
2777 Find_Prim_Op (Typ, Name_Of (Prim)), Loc),
2778 Parameter_Associations =>
2779 New_List (New_Copy_Tree (Obj_Ref))));
2781 Append_To (Res, Make_Attach_Call (
2782 Obj_Ref => New_Copy_Tree (Obj_Ref),
2783 Flist_Ref => Make_Identifier (Loc, Name_L),
2784 With_Attach => Make_Identifier (Loc, Name_B)));
2785 end if;
2787 when Finalize_Case =>
2788 if Is_Controlled (Typ) then
2789 Append_To (Res,
2790 Make_Implicit_If_Statement (Obj_Ref,
2791 Condition => Make_Identifier (Loc, Name_B),
2792 Then_Statements => New_List (
2793 Make_Procedure_Call_Statement (Loc,
2794 Name => New_Reference_To (RTE (RE_Finalize_One), Loc),
2795 Parameter_Associations => New_List (
2796 OK_Convert_To (RTE (RE_Finalizable),
2797 New_Copy_Tree (Obj_Ref))))),
2799 Else_Statements => New_List (
2800 Make_Procedure_Call_Statement (Loc,
2801 Name => New_Reference_To (
2802 Find_Prim_Op (Typ, Name_Of (Prim)), Loc),
2803 Parameter_Associations =>
2804 New_List (New_Copy_Tree (Obj_Ref))))));
2805 end if;
2807 Append_List_To (Res,
2808 Make_Final_Call (Controller_Ref, Controller_Typ,
2809 Make_Identifier (Loc, Name_B)));
2810 end case;
2811 return Res;
2812 end Make_Deep_Record_Body;
2814 ----------------------
2815 -- Make_Final_Call --
2816 ----------------------
2818 function Make_Final_Call
2819 (Ref : Node_Id;
2820 Typ : Entity_Id;
2821 With_Detach : Node_Id) return List_Id
2823 Loc : constant Source_Ptr := Sloc (Ref);
2824 Res : constant List_Id := New_List;
2825 Cref : Node_Id;
2826 Cref2 : Node_Id;
2827 Proc : Entity_Id;
2828 Utyp : Entity_Id;
2830 begin
2831 if Is_Class_Wide_Type (Typ) then
2832 Utyp := Root_Type (Typ);
2833 Cref := Ref;
2835 elsif Is_Concurrent_Type (Typ) then
2836 Utyp := Corresponding_Record_Type (Typ);
2837 Cref := Convert_Concurrent (Ref, Typ);
2839 elsif Is_Private_Type (Typ)
2840 and then Present (Full_View (Typ))
2841 and then Is_Concurrent_Type (Full_View (Typ))
2842 then
2843 Utyp := Corresponding_Record_Type (Full_View (Typ));
2844 Cref := Convert_Concurrent (Ref, Full_View (Typ));
2845 else
2846 Utyp := Typ;
2847 Cref := Ref;
2848 end if;
2850 Utyp := Underlying_Type (Base_Type (Utyp));
2851 Set_Assignment_OK (Cref);
2853 -- Deal with non-tagged derivation of private views. If the parent is
2854 -- now known to be protected, the finalization routine is the one
2855 -- defined on the corresponding record of the ancestor (corresponding
2856 -- records do not automatically inherit operations, but maybe they
2857 -- should???)
2859 if Is_Untagged_Derivation (Typ) then
2860 if Is_Protected_Type (Typ) then
2861 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
2862 else
2863 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
2864 end if;
2866 Cref := Unchecked_Convert_To (Utyp, Cref);
2868 -- We need to set Assignment_OK to prevent problems with unchecked
2869 -- conversions, where we do not want them to be converted back in the
2870 -- case of untagged record derivation (see code in Make_*_Call
2871 -- procedures for similar situations).
2873 Set_Assignment_OK (Cref);
2874 end if;
2876 -- If the underlying_type is a subtype, we are dealing with
2877 -- the completion of a private type. We need to access
2878 -- the base type and generate a conversion to it.
2880 if Utyp /= Base_Type (Utyp) then
2881 pragma Assert (Is_Private_Type (Typ));
2882 Utyp := Base_Type (Utyp);
2883 Cref := Unchecked_Convert_To (Utyp, Cref);
2884 end if;
2886 -- Generate:
2887 -- Deep_Finalize (Ref, With_Detach);
2889 if Has_Controlled_Component (Utyp)
2890 or else Is_Class_Wide_Type (Typ)
2891 then
2892 if Is_Tagged_Type (Utyp) then
2893 Proc := Find_Prim_Op (Utyp, TSS_Deep_Finalize);
2894 else
2895 Proc := TSS (Utyp, TSS_Deep_Finalize);
2896 end if;
2898 Cref := Convert_View (Proc, Cref);
2900 Append_To (Res,
2901 Make_Procedure_Call_Statement (Loc,
2902 Name => New_Reference_To (Proc, Loc),
2903 Parameter_Associations =>
2904 New_List (Cref, With_Detach)));
2906 -- Generate:
2907 -- if With_Detach then
2908 -- Finalize_One (Ref);
2909 -- else
2910 -- Finalize (Ref);
2911 -- end if;
2913 else
2914 Proc := Find_Prim_Op (Utyp, Name_Of (Finalize_Case));
2916 if Chars (With_Detach) = Chars (Standard_True) then
2917 Append_To (Res,
2918 Make_Procedure_Call_Statement (Loc,
2919 Name => New_Reference_To (RTE (RE_Finalize_One), Loc),
2920 Parameter_Associations => New_List (
2921 OK_Convert_To (RTE (RE_Finalizable), Cref))));
2923 elsif Chars (With_Detach) = Chars (Standard_False) then
2924 Append_To (Res,
2925 Make_Procedure_Call_Statement (Loc,
2926 Name => New_Reference_To (Proc, Loc),
2927 Parameter_Associations =>
2928 New_List (Convert_View (Proc, Cref))));
2930 else
2931 Cref2 := New_Copy_Tree (Cref);
2932 Append_To (Res,
2933 Make_Implicit_If_Statement (Ref,
2934 Condition => With_Detach,
2935 Then_Statements => New_List (
2936 Make_Procedure_Call_Statement (Loc,
2937 Name => New_Reference_To (RTE (RE_Finalize_One), Loc),
2938 Parameter_Associations => New_List (
2939 OK_Convert_To (RTE (RE_Finalizable), Cref)))),
2941 Else_Statements => New_List (
2942 Make_Procedure_Call_Statement (Loc,
2943 Name => New_Reference_To (Proc, Loc),
2944 Parameter_Associations =>
2945 New_List (Convert_View (Proc, Cref2))))));
2946 end if;
2947 end if;
2949 return Res;
2950 end Make_Final_Call;
2952 -------------------------------------
2953 -- Make_Handler_For_Ctrl_Operation --
2954 -------------------------------------
2956 -- Generate:
2958 -- when E : others =>
2959 -- Raise_From_Controlled_Operation (X => E);
2961 -- or:
2963 -- when others =>
2964 -- raise Program_Error [finalize raised exception];
2966 -- depending on whether Raise_From_Controlled_Operation is available
2968 function Make_Handler_For_Ctrl_Operation
2969 (Loc : Source_Ptr) return Node_Id
2971 E_Occ : Entity_Id;
2972 -- Choice parameter (for the first case above)
2974 Raise_Node : Node_Id;
2975 -- Procedure call or raise statement
2977 begin
2978 if RTE_Available (RE_Raise_From_Controlled_Operation) then
2980 -- Standard runtime: add choice parameter E, and pass it to
2981 -- Raise_From_Controlled_Operation so that the original exception
2982 -- name and message can be recorded in the exception message for
2983 -- Program_Error.
2985 E_Occ := Make_Defining_Identifier (Loc, Name_E);
2986 Raise_Node := Make_Procedure_Call_Statement (Loc,
2987 Name =>
2988 New_Occurrence_Of (
2989 RTE (RE_Raise_From_Controlled_Operation), Loc),
2990 Parameter_Associations => New_List (
2991 New_Occurrence_Of (E_Occ, Loc)));
2993 else
2994 -- Restricted runtime: exception messages are not supported
2996 E_Occ := Empty;
2997 Raise_Node := Make_Raise_Program_Error (Loc,
2998 Reason => PE_Finalize_Raised_Exception);
2999 end if;
3001 return Make_Implicit_Exception_Handler (Loc,
3002 Exception_Choices => New_List (Make_Others_Choice (Loc)),
3003 Choice_Parameter => E_Occ,
3004 Statements => New_List (Raise_Node));
3005 end Make_Handler_For_Ctrl_Operation;
3007 --------------------
3008 -- Make_Init_Call --
3009 --------------------
3011 function Make_Init_Call
3012 (Ref : Node_Id;
3013 Typ : Entity_Id;
3014 Flist_Ref : Node_Id;
3015 With_Attach : Node_Id) return List_Id
3017 Loc : constant Source_Ptr := Sloc (Ref);
3018 Is_Conc : Boolean;
3019 Res : constant List_Id := New_List;
3020 Proc : Entity_Id;
3021 Utyp : Entity_Id;
3022 Cref : Node_Id;
3023 Cref2 : Node_Id;
3024 Attach : Node_Id := With_Attach;
3026 begin
3027 if Is_Concurrent_Type (Typ) then
3028 Is_Conc := True;
3029 Utyp := Corresponding_Record_Type (Typ);
3030 Cref := Convert_Concurrent (Ref, Typ);
3032 elsif Is_Private_Type (Typ)
3033 and then Present (Full_View (Typ))
3034 and then Is_Concurrent_Type (Underlying_Type (Typ))
3035 then
3036 Is_Conc := True;
3037 Utyp := Corresponding_Record_Type (Underlying_Type (Typ));
3038 Cref := Convert_Concurrent (Ref, Underlying_Type (Typ));
3040 else
3041 Is_Conc := False;
3042 Utyp := Typ;
3043 Cref := Ref;
3044 end if;
3046 Utyp := Underlying_Type (Base_Type (Utyp));
3048 Set_Assignment_OK (Cref);
3050 -- Deal with non-tagged derivation of private views
3052 if Is_Untagged_Derivation (Typ)
3053 and then not Is_Conc
3054 then
3055 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
3056 Cref := Unchecked_Convert_To (Utyp, Cref);
3057 Set_Assignment_OK (Cref);
3058 -- To prevent problems with UC see 1.156 RH ???
3059 end if;
3061 -- If the underlying_type is a subtype, we are dealing with
3062 -- the completion of a private type. We need to access
3063 -- the base type and generate a conversion to it.
3065 if Utyp /= Base_Type (Utyp) then
3066 pragma Assert (Is_Private_Type (Typ));
3067 Utyp := Base_Type (Utyp);
3068 Cref := Unchecked_Convert_To (Utyp, Cref);
3069 end if;
3071 -- We do not need to attach to one of the Global Final Lists
3072 -- the objects whose type is Finalize_Storage_Only
3074 if Finalize_Storage_Only (Typ)
3075 and then (Global_Flist_Ref (Flist_Ref)
3076 or else Entity (Constant_Value (RTE (RE_Garbage_Collected)))
3077 = Standard_True)
3078 then
3079 Attach := Make_Integer_Literal (Loc, 0);
3080 end if;
3082 -- Generate:
3083 -- Deep_Initialize (Ref, Flist_Ref);
3085 if Has_Controlled_Component (Utyp) then
3086 Proc := TSS (Utyp, Deep_Name_Of (Initialize_Case));
3088 Cref := Convert_View (Proc, Cref, 2);
3090 Append_To (Res,
3091 Make_Procedure_Call_Statement (Loc,
3092 Name => New_Reference_To (Proc, Loc),
3093 Parameter_Associations => New_List (
3094 Node1 => Flist_Ref,
3095 Node2 => Cref,
3096 Node3 => Attach)));
3098 -- Generate:
3099 -- Attach_To_Final_List (Ref, Flist_Ref);
3100 -- Initialize (Ref);
3102 else -- Is_Controlled (Utyp)
3103 Proc := Find_Prim_Op (Utyp, Name_Of (Initialize_Case));
3104 Check_Visibly_Controlled (Initialize_Case, Typ, Proc, Cref);
3106 Cref := Convert_View (Proc, Cref);
3107 Cref2 := New_Copy_Tree (Cref);
3109 Append_To (Res,
3110 Make_Procedure_Call_Statement (Loc,
3111 Name => New_Reference_To (Proc, Loc),
3112 Parameter_Associations => New_List (Cref2)));
3114 Append_To (Res,
3115 Make_Attach_Call (Cref, Flist_Ref, Attach));
3116 end if;
3118 return Res;
3119 end Make_Init_Call;
3121 --------------------------
3122 -- Make_Transient_Block --
3123 --------------------------
3125 -- If finalization is involved, this function just wraps the instruction
3126 -- into a block whose name is the transient block entity, and then
3127 -- Expand_Cleanup_Actions (called on the expansion of the handled
3128 -- sequence of statements will do the necessary expansions for
3129 -- cleanups).
3131 function Make_Transient_Block
3132 (Loc : Source_Ptr;
3133 Action : Node_Id) return Node_Id
3135 Flist : constant Entity_Id := Finalization_Chain_Entity (Current_Scope);
3136 Decls : constant List_Id := New_List;
3137 Par : constant Node_Id := Parent (Action);
3138 Instrs : constant List_Id := New_List (Action);
3139 Blk : Node_Id;
3141 begin
3142 -- Case where only secondary stack use is involved
3144 if VM_Target = No_VM
3145 and then Uses_Sec_Stack (Current_Scope)
3146 and then No (Flist)
3147 and then Nkind (Action) /= N_Simple_Return_Statement
3148 and then Nkind (Par) /= N_Exception_Handler
3149 then
3150 declare
3151 S : Entity_Id;
3152 K : Entity_Kind;
3154 begin
3155 S := Scope (Current_Scope);
3156 loop
3157 K := Ekind (S);
3159 -- At the outer level, no need to release the sec stack
3161 if S = Standard_Standard then
3162 Set_Uses_Sec_Stack (Current_Scope, False);
3163 exit;
3165 -- In a function, only release the sec stack if the
3166 -- function does not return on the sec stack otherwise
3167 -- the result may be lost. The caller is responsible for
3168 -- releasing.
3170 elsif K = E_Function then
3171 Set_Uses_Sec_Stack (Current_Scope, False);
3173 if not Requires_Transient_Scope (Etype (S)) then
3174 Set_Uses_Sec_Stack (S, True);
3175 Check_Restriction (No_Secondary_Stack, Action);
3176 end if;
3178 exit;
3180 -- In a loop or entry we should install a block encompassing
3181 -- all the construct. For now just release right away.
3183 elsif K = E_Loop or else K = E_Entry then
3184 exit;
3186 -- In a procedure or a block, we release on exit of the
3187 -- procedure or block. ??? memory leak can be created by
3188 -- recursive calls.
3190 elsif K = E_Procedure
3191 or else K = E_Block
3192 then
3193 Set_Uses_Sec_Stack (S, True);
3194 Check_Restriction (No_Secondary_Stack, Action);
3195 Set_Uses_Sec_Stack (Current_Scope, False);
3196 exit;
3198 else
3199 S := Scope (S);
3200 end if;
3201 end loop;
3202 end;
3203 end if;
3205 -- Insert actions stuck in the transient scopes as well as all
3206 -- freezing nodes needed by those actions
3208 Insert_Actions_In_Scope_Around (Action);
3210 declare
3211 Last_Inserted : Node_Id := Prev (Action);
3212 begin
3213 if Present (Last_Inserted) then
3214 Freeze_All (First_Entity (Current_Scope), Last_Inserted);
3215 end if;
3216 end;
3218 Blk :=
3219 Make_Block_Statement (Loc,
3220 Identifier => New_Reference_To (Current_Scope, Loc),
3221 Declarations => Decls,
3222 Handled_Statement_Sequence =>
3223 Make_Handled_Sequence_Of_Statements (Loc, Statements => Instrs),
3224 Has_Created_Identifier => True);
3226 -- When the transient scope was established, we pushed the entry for
3227 -- the transient scope onto the scope stack, so that the scope was
3228 -- active for the installation of finalizable entities etc. Now we
3229 -- must remove this entry, since we have constructed a proper block.
3231 Pop_Scope;
3233 return Blk;
3234 end Make_Transient_Block;
3236 ------------------------
3237 -- Needs_Finalization --
3238 ------------------------
3240 function Needs_Finalization (T : Entity_Id) return Boolean is
3242 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
3243 -- If type is not frozen yet, check explicitly among its components,
3244 -- because the Has_Controlled_Component flag is not necessarily set.
3246 -----------------------------------
3247 -- Has_Some_Controlled_Component --
3248 -----------------------------------
3250 function Has_Some_Controlled_Component
3251 (Rec : Entity_Id) return Boolean
3253 Comp : Entity_Id;
3255 begin
3256 if Has_Controlled_Component (Rec) then
3257 return True;
3259 elsif not Is_Frozen (Rec) then
3260 if Is_Record_Type (Rec) then
3261 Comp := First_Entity (Rec);
3263 while Present (Comp) loop
3264 if not Is_Type (Comp)
3265 and then Needs_Finalization (Etype (Comp))
3266 then
3267 return True;
3268 end if;
3270 Next_Entity (Comp);
3271 end loop;
3273 return False;
3275 elsif Is_Array_Type (Rec) then
3276 return Needs_Finalization (Component_Type (Rec));
3278 else
3279 return Has_Controlled_Component (Rec);
3280 end if;
3281 else
3282 return False;
3283 end if;
3284 end Has_Some_Controlled_Component;
3286 -- Start of processing for Needs_Finalization
3288 begin
3289 -- Class-wide types must be treated as controlled because they may
3290 -- contain an extension that has controlled components
3292 -- We can skip this if finalization is not available
3294 return (Is_Class_Wide_Type (T)
3295 and then not In_Finalization_Root (T)
3296 and then not Restriction_Active (No_Finalization))
3297 or else Is_Controlled (T)
3298 or else Has_Some_Controlled_Component (T)
3299 or else (Is_Concurrent_Type (T)
3300 and then Present (Corresponding_Record_Type (T))
3301 and then Needs_Finalization (Corresponding_Record_Type (T)));
3302 end Needs_Finalization;
3304 ------------------------
3305 -- Node_To_Be_Wrapped --
3306 ------------------------
3308 function Node_To_Be_Wrapped return Node_Id is
3309 begin
3310 return Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped;
3311 end Node_To_Be_Wrapped;
3313 ----------------------------
3314 -- Set_Node_To_Be_Wrapped --
3315 ----------------------------
3317 procedure Set_Node_To_Be_Wrapped (N : Node_Id) is
3318 begin
3319 Scope_Stack.Table (Scope_Stack.Last).Node_To_Be_Wrapped := N;
3320 end Set_Node_To_Be_Wrapped;
3322 ----------------------------------
3323 -- Store_After_Actions_In_Scope --
3324 ----------------------------------
3326 procedure Store_After_Actions_In_Scope (L : List_Id) is
3327 SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
3329 begin
3330 if Present (SE.Actions_To_Be_Wrapped_After) then
3331 Insert_List_Before_And_Analyze (
3332 First (SE.Actions_To_Be_Wrapped_After), L);
3334 else
3335 SE.Actions_To_Be_Wrapped_After := L;
3337 if Is_List_Member (SE.Node_To_Be_Wrapped) then
3338 Set_Parent (L, Parent (SE.Node_To_Be_Wrapped));
3339 else
3340 Set_Parent (L, SE.Node_To_Be_Wrapped);
3341 end if;
3343 Analyze_List (L);
3344 end if;
3345 end Store_After_Actions_In_Scope;
3347 -----------------------------------
3348 -- Store_Before_Actions_In_Scope --
3349 -----------------------------------
3351 procedure Store_Before_Actions_In_Scope (L : List_Id) is
3352 SE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
3354 begin
3355 if Present (SE.Actions_To_Be_Wrapped_Before) then
3356 Insert_List_After_And_Analyze (
3357 Last (SE.Actions_To_Be_Wrapped_Before), L);
3359 else
3360 SE.Actions_To_Be_Wrapped_Before := L;
3362 if Is_List_Member (SE.Node_To_Be_Wrapped) then
3363 Set_Parent (L, Parent (SE.Node_To_Be_Wrapped));
3364 else
3365 Set_Parent (L, SE.Node_To_Be_Wrapped);
3366 end if;
3368 Analyze_List (L);
3369 end if;
3370 end Store_Before_Actions_In_Scope;
3372 --------------------------------
3373 -- Wrap_Transient_Declaration --
3374 --------------------------------
3376 -- If a transient scope has been established during the processing of the
3377 -- Expression of an Object_Declaration, it is not possible to wrap the
3378 -- declaration into a transient block as usual case, otherwise the object
3379 -- would be itself declared in the wrong scope. Therefore, all entities (if
3380 -- any) defined in the transient block are moved to the proper enclosing
3381 -- scope, furthermore, if they are controlled variables they are finalized
3382 -- right after the declaration. The finalization list of the transient
3383 -- scope is defined as a renaming of the enclosing one so during their
3384 -- initialization they will be attached to the proper finalization
3385 -- list. For instance, the following declaration :
3387 -- X : Typ := F (G (A), G (B));
3389 -- (where G(A) and G(B) return controlled values, expanded as _v1 and _v2)
3390 -- is expanded into :
3392 -- _local_final_list_1 : Finalizable_Ptr;
3393 -- X : Typ := [ complex Expression-Action ];
3394 -- Finalize_One(_v1);
3395 -- Finalize_One (_v2);
3397 procedure Wrap_Transient_Declaration (N : Node_Id) is
3398 S : Entity_Id;
3399 LC : Entity_Id := Empty;
3400 Nodes : List_Id;
3401 Loc : constant Source_Ptr := Sloc (N);
3402 First_Decl_Loc : Source_Ptr;
3403 Enclosing_S : Entity_Id;
3404 Uses_SS : Boolean;
3405 Next_N : constant Node_Id := Next (N);
3407 begin
3408 S := Current_Scope;
3409 Enclosing_S := Scope (S);
3411 -- Insert Actions kept in the Scope stack
3413 Insert_Actions_In_Scope_Around (N);
3415 -- If the declaration is consuming some secondary stack, mark the
3416 -- Enclosing scope appropriately.
3418 Uses_SS := Uses_Sec_Stack (S);
3419 Pop_Scope;
3421 -- Create a List controller and rename the final list to be its
3422 -- internal final pointer:
3423 -- Lxxx : Simple_List_Controller;
3424 -- Fxxx : Finalizable_Ptr renames Lxxx.F;
3426 if Present (Finalization_Chain_Entity (S)) then
3427 LC := Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
3429 -- Use the Sloc of the first declaration of N's containing list, to
3430 -- maintain monotonicity of source-line stepping during debugging.
3432 First_Decl_Loc := Sloc (First (List_Containing (N)));
3434 Nodes := New_List (
3435 Make_Object_Declaration (First_Decl_Loc,
3436 Defining_Identifier => LC,
3437 Object_Definition =>
3438 New_Reference_To
3439 (RTE (RE_Simple_List_Controller), First_Decl_Loc)),
3441 Make_Object_Renaming_Declaration (First_Decl_Loc,
3442 Defining_Identifier => Finalization_Chain_Entity (S),
3443 Subtype_Mark =>
3444 New_Reference_To (RTE (RE_Finalizable_Ptr), First_Decl_Loc),
3445 Name =>
3446 Make_Selected_Component (Loc,
3447 Prefix => New_Reference_To (LC, First_Decl_Loc),
3448 Selector_Name => Make_Identifier (First_Decl_Loc, Name_F))));
3450 -- Put the declaration at the beginning of the declaration part
3451 -- to make sure it will be before all other actions that have been
3452 -- inserted before N.
3454 Insert_List_Before_And_Analyze (First (List_Containing (N)), Nodes);
3456 -- Generate the Finalization calls by finalizing the list controller
3457 -- right away. It will be re-finalized on scope exit but it doesn't
3458 -- matter. It cannot be done when the call initializes a renaming
3459 -- object though because in this case, the object becomes a pointer
3460 -- to the temporary and thus increases its life span. Ditto if this
3461 -- is a renaming of a component of an expression (such as a function
3462 -- call).
3464 -- Note that there is a problem if an actual in the call needs
3465 -- finalization, because in that case the call itself is the master,
3466 -- and the actual should be finalized on return from the call ???
3468 if Nkind (N) = N_Object_Renaming_Declaration
3469 and then Needs_Finalization (Etype (Defining_Identifier (N)))
3470 then
3471 null;
3473 elsif Nkind (N) = N_Object_Renaming_Declaration
3474 and then
3475 Nkind_In (Renamed_Object (Defining_Identifier (N)),
3476 N_Selected_Component,
3477 N_Indexed_Component)
3478 and then
3479 Needs_Finalization
3480 (Etype (Prefix (Renamed_Object (Defining_Identifier (N)))))
3481 then
3482 null;
3484 else
3485 Nodes :=
3486 Make_Final_Call
3487 (Ref => New_Reference_To (LC, Loc),
3488 Typ => Etype (LC),
3489 With_Detach => New_Reference_To (Standard_False, Loc));
3491 if Present (Next_N) then
3492 Insert_List_Before_And_Analyze (Next_N, Nodes);
3493 else
3494 Append_List_To (List_Containing (N), Nodes);
3495 end if;
3496 end if;
3497 end if;
3499 -- Put the local entities back in the enclosing scope, and set the
3500 -- Is_Public flag appropriately.
3502 Transfer_Entities (S, Enclosing_S);
3504 -- Mark the enclosing dynamic scope so that the sec stack will be
3505 -- released upon its exit unless this is a function that returns on
3506 -- the sec stack in which case this will be done by the caller.
3508 if VM_Target = No_VM and then Uses_SS then
3509 S := Enclosing_Dynamic_Scope (S);
3511 if Ekind (S) = E_Function
3512 and then Requires_Transient_Scope (Etype (S))
3513 then
3514 null;
3515 else
3516 Set_Uses_Sec_Stack (S);
3517 Check_Restriction (No_Secondary_Stack, N);
3518 end if;
3519 end if;
3520 end Wrap_Transient_Declaration;
3522 -------------------------------
3523 -- Wrap_Transient_Expression --
3524 -------------------------------
3526 -- Insert actions before <Expression>:
3528 -- (lines marked with <CTRL> are expanded only in presence of Controlled
3529 -- objects needing finalization)
3531 -- _E : Etyp;
3532 -- declare
3533 -- _M : constant Mark_Id := SS_Mark;
3534 -- Local_Final_List : System.FI.Finalizable_Ptr; <CTRL>
3536 -- procedure _Clean is
3537 -- begin
3538 -- Abort_Defer;
3539 -- System.FI.Finalize_List (Local_Final_List); <CTRL>
3540 -- SS_Release (M);
3541 -- Abort_Undefer;
3542 -- end _Clean;
3544 -- begin
3545 -- _E := <Expression>;
3546 -- at end
3547 -- _Clean;
3548 -- end;
3550 -- then expression is replaced by _E
3552 procedure Wrap_Transient_Expression (N : Node_Id) is
3553 Loc : constant Source_Ptr := Sloc (N);
3554 E : constant Entity_Id := Make_Temporary (Loc, 'E', N);
3555 Etyp : constant Entity_Id := Etype (N);
3557 begin
3558 Insert_Actions (N, New_List (
3559 Make_Object_Declaration (Loc,
3560 Defining_Identifier => E,
3561 Object_Definition => New_Reference_To (Etyp, Loc)),
3563 Make_Transient_Block (Loc,
3564 Action =>
3565 Make_Assignment_Statement (Loc,
3566 Name => New_Reference_To (E, Loc),
3567 Expression => Relocate_Node (N)))));
3569 Rewrite (N, New_Reference_To (E, Loc));
3570 Analyze_And_Resolve (N, Etyp);
3571 end Wrap_Transient_Expression;
3573 ------------------------------
3574 -- Wrap_Transient_Statement --
3575 ------------------------------
3577 -- Transform <Instruction> into
3579 -- (lines marked with <CTRL> are expanded only in presence of Controlled
3580 -- objects needing finalization)
3582 -- declare
3583 -- _M : Mark_Id := SS_Mark;
3584 -- Local_Final_List : System.FI.Finalizable_Ptr ; <CTRL>
3586 -- procedure _Clean is
3587 -- begin
3588 -- Abort_Defer;
3589 -- System.FI.Finalize_List (Local_Final_List); <CTRL>
3590 -- SS_Release (_M);
3591 -- Abort_Undefer;
3592 -- end _Clean;
3594 -- begin
3595 -- <Instruction>;
3596 -- at end
3597 -- _Clean;
3598 -- end;
3600 procedure Wrap_Transient_Statement (N : Node_Id) is
3601 Loc : constant Source_Ptr := Sloc (N);
3602 New_Statement : constant Node_Id := Relocate_Node (N);
3604 begin
3605 Rewrite (N, Make_Transient_Block (Loc, New_Statement));
3607 -- With the scope stack back to normal, we can call analyze on the
3608 -- resulting block. At this point, the transient scope is being
3609 -- treated like a perfectly normal scope, so there is nothing
3610 -- special about it.
3612 -- Note: Wrap_Transient_Statement is called with the node already
3613 -- analyzed (i.e. Analyzed (N) is True). This is important, since
3614 -- otherwise we would get a recursive processing of the node when
3615 -- we do this Analyze call.
3617 Analyze (N);
3618 end Wrap_Transient_Statement;
3620 end Exp_Ch7;