c++: record template specialization hash
[official-gcc.git] / gcc / ada / exp_ch6.adb
blobc868234655ea861e216f1ae2beee8e04c6ef41a9
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2024, 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 with Accessibility; use Accessibility;
27 with Atree; use Atree;
28 with Aspects; use Aspects;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Errout; use Errout;
35 with Elists; use Elists;
36 with Expander; use Expander;
37 with Exp_Aggr; use Exp_Aggr;
38 with Exp_Atag; use Exp_Atag;
39 with Exp_Ch3; use Exp_Ch3;
40 with Exp_Ch4; use Exp_Ch4;
41 with Exp_Ch7; use Exp_Ch7;
42 with Exp_Ch9; use Exp_Ch9;
43 with Exp_Dbug; use Exp_Dbug;
44 with Exp_Disp; use Exp_Disp;
45 with Exp_Dist; use Exp_Dist;
46 with Exp_Intr; use Exp_Intr;
47 with Exp_Pakd; use Exp_Pakd;
48 with Exp_Tss; use Exp_Tss;
49 with Exp_Util; use Exp_Util;
50 with Freeze; use Freeze;
51 with Inline; use Inline;
52 with Itypes; use Itypes;
53 with Lib; use Lib;
54 with Namet; use Namet;
55 with Nlists; use Nlists;
56 with Nmake; use Nmake;
57 with Opt; use Opt;
58 with Restrict; use Restrict;
59 with Rident; use Rident;
60 with Rtsfind; use Rtsfind;
61 with Sem; use Sem;
62 with Sem_Aux; use Sem_Aux;
63 with Sem_Ch6; use Sem_Ch6;
64 with Sem_Ch8; use Sem_Ch8;
65 with Sem_Ch13; use Sem_Ch13;
66 with Sem_Dim; use Sem_Dim;
67 with Sem_Disp; use Sem_Disp;
68 with Sem_Dist; use Sem_Dist;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Res; use Sem_Res;
72 with Sem_SCIL; use Sem_SCIL;
73 with Sem_Util; use Sem_Util;
74 use Sem_Util.Storage_Model_Support;
75 with Sinfo; use Sinfo;
76 with Sinfo.Nodes; use Sinfo.Nodes;
77 with Sinfo.Utils; use Sinfo.Utils;
78 with Sinput; use Sinput;
79 with Snames; use Snames;
80 with Stand; use Stand;
81 with Tbuild; use Tbuild;
82 with Uintp; use Uintp;
83 with Validsw; use Validsw;
85 package body Exp_Ch6 is
87 --------------------------------
88 -- Function return mechanisms --
89 --------------------------------
91 -- This is a summary of the various function return mechanisms implemented
92 -- in GNAT for Ada 2005 and later versions of the language. In the below
93 -- table, the first column must be read as an if expression: if the result
94 -- type of the function is limited, then the return mechanism is and ...;
95 -- elsif the result type is indefinite or large definite, then ...; elsif
96 -- ...; else ... The different mechanisms are implemented either in the
97 -- front end, or in the back end, or partly in both ends, depending on the
98 -- result type.
100 -- Result type | Return mechanism | Front end | Back end
101 -- --------------------------------------------------------------------
103 -- Limited Build In Place All
105 -- Indefinite/ Secondary Stack Needs Fin. Others
106 -- Large definite
108 -- Needs Fin. Secondary Stack All
109 -- (BERS False)
111 -- Needs Fin. Invisible Parameter All All
112 -- (BERS True) (return) (call)
114 -- By Reference Invisible Parameter All
116 -- Others Primary stack/ All
117 -- Registers
119 -- Needs Fin.: type needs finalization [RM 7.6(9.1/2-9.6/2)]
120 -- BERS: Opt.Back_End_Return_Slot setting
122 -- The table is valid for all calls except for those dispatching on result;
123 -- the latter calls are considered as returning a class-wide type and thus
124 -- always return on the secondary stack, with the help of a small wrapper
125 -- function (thunk) if the original result type is not itself returned on
126 -- the secondary stack as per the above table.
128 -- Suffixes for Build-In-Place extra formals
130 BIP_Alloc_Suffix : constant String := "BIPalloc";
131 BIP_Storage_Pool_Suffix : constant String := "BIPstoragepool";
132 BIP_Collection_Suffix : constant String := "BIPcollection";
133 BIP_Task_Master_Suffix : constant String := "BIPtaskmaster";
134 BIP_Activation_Chain_Suffix : constant String := "BIPactivationchain";
135 BIP_Object_Access_Suffix : constant String := "BIPaccess";
137 -----------------------
138 -- Local Subprograms --
139 -----------------------
141 procedure Add_Access_Actual_To_Build_In_Place_Call
142 (Function_Call : Node_Id;
143 Function_Id : Entity_Id;
144 Return_Object : Node_Id;
145 Is_Access : Boolean := False);
146 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
147 -- object name given by Return_Object and add the attribute to the end of
148 -- the actual parameter list associated with the build-in-place function
149 -- call denoted by Function_Call. However, if Is_Access is True, then
150 -- Return_Object is already an access expression, in which case it's passed
151 -- along directly to the build-in-place function. Finally, if Return_Object
152 -- is empty, then pass a null literal as the actual.
154 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
155 (Function_Call : Node_Id;
156 Function_Id : Entity_Id;
157 Alloc_Form : BIP_Allocation_Form := Unspecified;
158 Alloc_Form_Exp : Node_Id := Empty;
159 Pool_Exp : Node_Id := Empty);
160 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
161 -- them, add the actuals parameters BIP_Alloc_Form and BIP_Storage_Pool.
162 -- If Alloc_Form_Exp is present, then pass it for the first parameter,
163 -- otherwise pass a literal corresponding to the Alloc_Form parameter
164 -- (which must not be Unspecified in that case). If Pool_Exp is present,
165 -- then use it for BIP_Storage_Pool, otherwise pass "null".
167 procedure Add_Collection_Actual_To_Build_In_Place_Call
168 (Function_Call : Node_Id;
169 Function_Id : Entity_Id;
170 Ptr_Typ : Entity_Id := Empty;
171 Collection_Exp : Node_Id := Empty);
172 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
173 -- finalization actions, add an actual parameter which is a pointer to the
174 -- collection of the access type used by the caller. If Collection_Exp is
175 -- present, then that will be passed as the actual. Otherwise, if Ptr_Typ
176 -- is Empty, this will result in an automatic "null" value for the actual.
178 procedure Add_Task_Actuals_To_Build_In_Place_Call
179 (Function_Call : Node_Id;
180 Function_Id : Entity_Id;
181 Master_Actual : Node_Id;
182 Chain : Node_Id := Empty);
183 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
184 -- contains tasks, add two actual parameters: the master, and a pointer to
185 -- the caller's activation chain. Master_Actual is the actual parameter
186 -- expression to pass for the master. In most cases, this is the current
187 -- master (_master). The two exceptions are: If the function call is the
188 -- initialization expression for an allocator, we pass the master of the
189 -- access type. If the function call is the initialization expression for a
190 -- return object, we pass along the master passed in by the caller. In most
191 -- contexts, the activation chain to pass is the local one, which is
192 -- indicated by No (Chain). However, in an allocator, the caller passes in
193 -- the activation Chain. Note: Master_Actual can be Empty, but only if
194 -- there are no tasks.
196 function Caller_Known_Size
197 (Func_Call : Node_Id;
198 Result_Subt : Entity_Id) return Boolean;
199 -- True if result subtype is definite or has a size that does not require
200 -- secondary stack usage (i.e. no variant part or components whose type
201 -- depends on discriminants). In particular, untagged types with only
202 -- access discriminants do not require secondary stack use. Note we must
203 -- always use the secondary stack for dispatching-on-result calls.
205 function Check_BIP_Actuals
206 (Subp_Call : Node_Id;
207 Subp_Id : Entity_Id) return Boolean;
208 -- Given a subprogram call to the given subprogram return True if the
209 -- names of BIP extra actual and formal parameters match, and the number
210 -- of actuals (including extra actuals) matches the number of formals.
212 function Check_Number_Of_Actuals
213 (Subp_Call : Node_Id;
214 Subp_Id : Entity_Id) return Boolean;
215 -- Given a subprogram call to the given subprogram return True if the
216 -- number of actual parameters (including extra actuals) is correct.
218 procedure Check_Overriding_Operation (Subp : Entity_Id);
219 -- Subp is a dispatching operation. Check whether it may override an
220 -- inherited private operation, in which case its DT entry is that of
221 -- the hidden operation, not the one it may have received earlier.
222 -- This must be done before emitting the code to set the corresponding
223 -- DT to the address of the subprogram. The actual placement of Subp in
224 -- the proper place in the list of primitive operations is done in
225 -- Declare_Inherited_Private_Subprograms, which also has to deal with
226 -- implicit operations. This duplication is unavoidable for now???
228 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
229 -- This procedure is called only if the subprogram body N, whose spec
230 -- has the given entity Spec, contains a parameterless recursive call.
231 -- It attempts to generate runtime code to detect if this a case of
232 -- infinite recursion.
234 -- The body is scanned to determine dependencies. If the only external
235 -- dependencies are on a small set of scalar variables, then the values
236 -- of these variables are captured on entry to the subprogram, and if
237 -- the values are not changed for the call, we know immediately that
238 -- we have an infinite recursion.
240 procedure Expand_Actuals
241 (N : Node_Id;
242 Subp : Entity_Id;
243 Post_Call : out List_Id);
244 -- Return a list of actions to take place after the call in Post_Call. The
245 -- call will later be rewritten as an Expression_With_Actions, with the
246 -- Post_Call actions inserted, and the call inside.
248 -- For each actual of an in-out or out parameter which is a numeric (view)
249 -- conversion of the form T (A), where A denotes a variable, we insert the
250 -- declaration:
252 -- Temp : T[ := T (A)];
254 -- prior to the call. Then we replace the actual with a reference to Temp,
255 -- and append the assignment:
257 -- A := TypeA (Temp);
259 -- after the call. Here TypeA is the actual type of variable A. For out
260 -- parameters, the initial declaration has no expression. If A is not an
261 -- entity name, we generate instead:
263 -- Var : TypeA renames A;
264 -- Temp : T := Var; -- omitting expression for out parameter.
265 -- ...
266 -- Var := TypeA (Temp);
268 -- For other in-out parameters, we emit the required constraint checks
269 -- before and/or after the call.
271 -- For all parameter modes, actuals that denote components and slices of
272 -- packed arrays are expanded into suitable temporaries.
274 -- For nonscalar objects that are possibly unaligned, add call by copy code
275 -- (copy in for IN and IN OUT, copy out for OUT and IN OUT).
277 -- For OUT and IN OUT parameters, add predicate checks after the call
278 -- based on the predicates of the actual type.
280 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id);
281 -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals.
283 procedure Expand_Ctrl_Function_Call (N : Node_Id; Use_Sec_Stack : Boolean);
284 -- N is a function call which returns a controlled object. Transform the
285 -- call into a temporary which retrieves the returned object from the
286 -- primary or secondary stack (Use_Sec_Stack says which) using 'reference.
288 procedure Expand_Non_Function_Return (N : Node_Id);
289 -- Expand a simple return statement found in a procedure body, entry body,
290 -- accept statement, or an extended return statement. Note that all non-
291 -- function returns are simple return statements.
293 function Expand_Protected_Object_Reference
294 (N : Node_Id;
295 Scop : Entity_Id) return Node_Id;
297 procedure Expand_Protected_Subprogram_Call
298 (N : Node_Id;
299 Subp : Entity_Id;
300 Scop : Entity_Id);
301 -- A call to a protected subprogram within the protected object may appear
302 -- as a regular call. The list of actuals must be expanded to contain a
303 -- reference to the object itself, and the call becomes a call to the
304 -- corresponding protected subprogram.
306 procedure Expand_Simple_Function_Return (N : Node_Id);
307 -- Expand simple return from function. In the case where we are returning
308 -- from a function body this is called by Expand_N_Simple_Return_Statement.
310 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id);
311 -- Insert the Post_Call list previously produced by routine Expand_Actuals
312 -- or Expand_Call_Helper into the tree.
314 function Is_Function_Call_With_BIP_Formals (N : Node_Id) return Boolean;
315 -- Ada 2005 (AI-318-02): Returns True if N denotes a call to a function
316 -- that requires handling as a build-in-place call, that is, BIP function
317 -- calls and calls to functions with inherited BIP formals. For example:
319 -- type Iface is limited interface;
320 -- function Get_Object return Iface;
321 -- -- This function has BIP extra formals
323 -- type Root1 is limited tagged record ...
324 -- type T1 is new Root1 and Iface with ...
325 -- function Get_Object return T1;
326 -- -- This primitive requires the BIP formals, and the evaluation of
327 -- -- Is_Build_In_Place_Function_Call returns True.
329 -- type Root2 is tagged record ...
330 -- type T2 is new Root2 and Iface with ...
331 -- function Get_Object return T2;
332 -- -- This primitive inherits the BIP formals of the interface primitive
333 -- -- but, given that T2 is not a limited type, it does not require such
334 -- -- formals; therefore Is_Build_In_Place_Function_Call returns False.
336 procedure Replace_Renaming_Declaration_Id
337 (New_Decl : Node_Id;
338 Orig_Decl : Node_Id);
339 -- Replace the internal identifier of the new renaming declaration New_Decl
340 -- with the identifier of its original declaration Orig_Decl exchanging the
341 -- entities containing their defining identifiers to ensure the correct
342 -- replacement of the object declaration by the object renaming declaration
343 -- to avoid homograph conflicts (since the object declaration's defining
344 -- identifier was already entered in the current scope). The Next_Entity
345 -- links of the two entities are also swapped since the entities are part
346 -- of the return scope's entity list and the list structure would otherwise
347 -- be corrupted. The homonym chain is preserved as well.
349 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id);
350 -- N is a return statement for a function that returns its result on the
351 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
352 -- function and all blocks and loops that the return statement is jumping
353 -- out of. This ensures that the secondary stack is not released; otherwise
354 -- the function result would be reclaimed before returning to the caller.
356 procedure Warn_BIP (Func_Call : Node_Id);
357 -- Give a warning on a build-in-place function call if the -gnatd_B switch
358 -- was given.
360 ----------------------------------------------
361 -- Add_Access_Actual_To_Build_In_Place_Call --
362 ----------------------------------------------
364 procedure Add_Access_Actual_To_Build_In_Place_Call
365 (Function_Call : Node_Id;
366 Function_Id : Entity_Id;
367 Return_Object : Node_Id;
368 Is_Access : Boolean := False)
370 Loc : constant Source_Ptr := Sloc (Function_Call);
371 Obj_Address : Node_Id;
372 Obj_Acc_Formal : Entity_Id;
374 begin
375 -- Locate the implicit access parameter in the called function
377 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
379 -- If no return object is provided, then pass null
381 if No (Return_Object) then
382 Obj_Address := Make_Null (Loc);
383 Set_Parent (Obj_Address, Function_Call);
385 -- If Return_Object is already an expression of an access type, then use
386 -- it directly, since it must be an access value denoting the return
387 -- object, and couldn't possibly be the return object itself.
389 elsif Is_Access then
390 Obj_Address := Return_Object;
391 Set_Parent (Obj_Address, Function_Call);
393 -- Apply Unrestricted_Access to caller's return object
395 else
396 Obj_Address :=
397 Make_Attribute_Reference (Loc,
398 Prefix => Return_Object,
399 Attribute_Name => Name_Unrestricted_Access);
401 Set_Parent (Return_Object, Obj_Address);
402 Set_Parent (Obj_Address, Function_Call);
403 end if;
405 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
407 -- Build the parameter association for the new actual and add it to the
408 -- end of the function's actuals.
410 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
411 end Add_Access_Actual_To_Build_In_Place_Call;
413 ------------------------------------------------------
414 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
415 ------------------------------------------------------
417 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
418 (Function_Call : Node_Id;
419 Function_Id : Entity_Id;
420 Alloc_Form : BIP_Allocation_Form := Unspecified;
421 Alloc_Form_Exp : Node_Id := Empty;
422 Pool_Exp : Node_Id := Empty)
424 Loc : constant Source_Ptr := Sloc (Function_Call);
426 Alloc_Form_Actual : Node_Id;
427 Alloc_Form_Formal : Node_Id;
429 begin
430 -- Nothing to do when the size of the object is known, and the caller is
431 -- in charge of allocating it, and the callee doesn't unconditionally
432 -- require an allocation form (such as due to having a tagged result).
434 if not Needs_BIP_Alloc_Form (Function_Id) then
435 return;
436 end if;
438 -- Locate the implicit allocation form parameter in the called function.
439 -- Maybe it would be better for each implicit formal of a build-in-place
440 -- function to have a flag or a Uint attribute to identify it. ???
442 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
444 if Present (Alloc_Form_Exp) then
445 pragma Assert (Alloc_Form = Unspecified);
447 Alloc_Form_Actual := Alloc_Form_Exp;
449 else
450 pragma Assert (Alloc_Form /= Unspecified);
452 Alloc_Form_Actual :=
453 Make_Integer_Literal (Loc,
454 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
455 end if;
457 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
459 -- Build the parameter association for the new actual and add it to the
460 -- end of the function's actuals.
462 Add_Extra_Actual_To_Call
463 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
465 -- Pass the Storage_Pool parameter. This parameter is omitted on ZFP as
466 -- those targets do not support pools.
468 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
469 declare
470 Pool_Actual : constant Node_Id :=
471 (if Present (Pool_Exp) then Pool_Exp else Make_Null (Loc));
472 Pool_Formal : constant Node_Id :=
473 Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
475 begin
476 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
477 Add_Extra_Actual_To_Call (Function_Call, Pool_Formal, Pool_Actual);
478 end;
479 end if;
480 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
482 --------------------------------------------------
483 -- Add_Collection_Actual_To_Build_In_Place_Call --
484 --------------------------------------------------
486 procedure Add_Collection_Actual_To_Build_In_Place_Call
487 (Function_Call : Node_Id;
488 Function_Id : Entity_Id;
489 Ptr_Typ : Entity_Id := Empty;
490 Collection_Exp : Node_Id := Empty)
492 Loc : constant Source_Ptr := Sloc (Function_Call);
494 Actual : Node_Id;
495 Formal : Node_Id;
496 Desig_Typ : Entity_Id;
498 begin
499 if not Needs_BIP_Collection (Function_Id) then
500 return;
501 end if;
503 Formal := Build_In_Place_Formal (Function_Id, BIP_Collection);
505 -- If there is a finalization collection actual, such as the implicit
506 -- finalization collection of an enclosing build-in-place function,
507 -- then this must be added as an extra actual of the call.
509 if Present (Collection_Exp) then
510 Actual := Collection_Exp;
512 -- Case where the context does not require an actual collection
514 elsif No (Ptr_Typ) then
515 Actual := Make_Null (Loc);
517 else
518 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
520 -- Check for a type that is subject to pragma No_Heap_Finalization.
521 -- Such a type lacks a collection. Pass a null actual to callee to
522 -- signal a missing collection.
524 if No_Heap_Finalization (Ptr_Typ) then
525 Actual := Make_Null (Loc);
527 -- Types in need of finalization actions
529 elsif Needs_Finalization (Desig_Typ) then
531 -- The general mechanism of creating finalization collections
532 -- for anonymous access types is disabled by default, otherwise
533 -- finalization collections will pop all over the place. Instead
534 -- such types use context-specific collections.
536 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
537 and then No (Finalization_Collection (Ptr_Typ))
538 then
539 Build_Anonymous_Collection (Ptr_Typ);
540 end if;
542 -- Access-to-controlled types should always have a collection
544 pragma Assert (Present (Finalization_Collection (Ptr_Typ)));
546 Actual :=
547 Make_Attribute_Reference (Loc,
548 Prefix =>
549 New_Occurrence_Of (Finalization_Collection (Ptr_Typ), Loc),
550 Attribute_Name => Name_Unrestricted_Access);
552 -- Tagged types
554 else
555 Actual := Make_Null (Loc);
556 end if;
557 end if;
559 Analyze_And_Resolve (Actual, Etype (Formal));
561 -- Build the parameter association for the new actual and add it to
562 -- the end of the function's actuals.
564 Add_Extra_Actual_To_Call (Function_Call, Formal, Actual);
565 end Add_Collection_Actual_To_Build_In_Place_Call;
567 ------------------------------
568 -- Add_Extra_Actual_To_Call --
569 ------------------------------
571 procedure Add_Extra_Actual_To_Call
572 (Subprogram_Call : Node_Id;
573 Extra_Formal : Entity_Id;
574 Extra_Actual : Node_Id)
576 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
577 Param_Assoc : Node_Id;
579 begin
580 Param_Assoc :=
581 Make_Parameter_Association (Loc,
582 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
583 Explicit_Actual_Parameter => Extra_Actual);
585 Set_Parent (Param_Assoc, Subprogram_Call);
586 Set_Parent (Extra_Actual, Param_Assoc);
588 if Present (Parameter_Associations (Subprogram_Call)) then
589 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
590 N_Parameter_Association
591 then
593 -- Find last named actual, and append
595 declare
596 L : Node_Id;
597 begin
598 L := First_Actual (Subprogram_Call);
599 while Present (L) loop
600 if No (Next_Actual (L)) then
601 Set_Next_Named_Actual (Parent (L), Extra_Actual);
602 exit;
603 end if;
604 Next_Actual (L);
605 end loop;
606 end;
608 else
609 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
610 end if;
612 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
614 else
615 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
616 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
617 end if;
618 end Add_Extra_Actual_To_Call;
620 ---------------------------------------------
621 -- Add_Task_Actuals_To_Build_In_Place_Call --
622 ---------------------------------------------
624 procedure Add_Task_Actuals_To_Build_In_Place_Call
625 (Function_Call : Node_Id;
626 Function_Id : Entity_Id;
627 Master_Actual : Node_Id;
628 Chain : Node_Id := Empty)
630 Loc : constant Source_Ptr := Sloc (Function_Call);
631 Actual : Node_Id;
632 Chain_Actual : Node_Id;
633 Chain_Formal : Node_Id;
634 Master_Formal : Node_Id;
636 begin
637 pragma Assert (Ekind (Function_Id) in E_Function
638 | E_Subprogram_Type);
640 -- No such extra parameters are needed if there are no tasks
642 if not Needs_BIP_Task_Actuals (Function_Id) then
644 -- However we must add dummy extra actuals if the function is
645 -- a dispatching operation that inherited these extra formals
646 -- or an access-to-subprogram type that requires these extra
647 -- actuals.
649 if Has_BIP_Extra_Formal (Function_Id, BIP_Task_Master,
650 Must_Be_Frozen => False)
651 then
652 Master_Formal :=
653 Build_In_Place_Formal (Function_Id, BIP_Task_Master);
654 Actual := Make_Integer_Literal (Loc, Uint_0);
655 Analyze_And_Resolve (Actual, Etype (Master_Formal));
656 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
658 Chain_Formal :=
659 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
660 Chain_Actual := Make_Null (Loc);
661 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
662 Add_Extra_Actual_To_Call
663 (Function_Call, Chain_Formal, Chain_Actual);
664 end if;
666 return;
667 end if;
669 Actual := Master_Actual;
671 -- Use a dummy _master actual in case of No_Task_Hierarchy
673 if Restriction_Active (No_Task_Hierarchy) then
674 Actual := Make_Integer_Literal (Loc, Library_Task_Level);
676 -- In the case where we use the master associated with an access type,
677 -- the actual is an entity and requires an explicit reference.
679 elsif Nkind (Actual) = N_Defining_Identifier then
680 Actual := New_Occurrence_Of (Actual, Loc);
681 end if;
683 -- Locate the implicit master parameter in the called function
685 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
686 Analyze_And_Resolve (Actual, Etype (Master_Formal));
688 -- Build the parameter association for the new actual and add it to the
689 -- end of the function's actuals.
691 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
693 -- Locate the implicit activation chain parameter in the called function
695 Chain_Formal :=
696 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
698 -- Create the actual which is a pointer to the current activation chain
700 if Restriction_Active (No_Task_Hierarchy) then
701 Chain_Actual := Make_Null (Loc);
703 elsif No (Chain) then
704 Chain_Actual :=
705 Make_Attribute_Reference (Loc,
706 Prefix => Make_Identifier (Loc, Name_uChain),
707 Attribute_Name => Name_Unrestricted_Access);
709 -- Allocator case; make a reference to the Chain passed in by the caller
711 else
712 Chain_Actual :=
713 Make_Attribute_Reference (Loc,
714 Prefix => New_Occurrence_Of (Chain, Loc),
715 Attribute_Name => Name_Unrestricted_Access);
716 end if;
718 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
720 -- Build the parameter association for the new actual and add it to the
721 -- end of the function's actuals.
723 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
724 end Add_Task_Actuals_To_Build_In_Place_Call;
726 ----------------------------------
727 -- Apply_CW_Accessibility_Check --
728 ----------------------------------
730 procedure Apply_CW_Accessibility_Check (Exp : Node_Id; Func : Entity_Id) is
731 Loc : constant Source_Ptr := Sloc (Exp);
733 begin
734 -- CodePeer does not do anything useful on Ada.Tags.Type_Specific_Data
735 -- components.
737 if Ada_Version >= Ada_2005
738 and then not CodePeer_Mode
739 and then Tagged_Type_Expansion
740 and then not Scope_Suppress.Suppress (Accessibility_Check)
741 and then
742 (Is_Class_Wide_Type (Etype (Exp))
743 or else Nkind (Exp) in
744 N_Type_Conversion | N_Unchecked_Type_Conversion
745 or else (Is_Entity_Name (Exp)
746 and then Is_Formal (Entity (Exp)))
747 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
748 Scope_Depth (Enclosing_Dynamic_Scope (Func)))
749 then
750 declare
751 Tag_Node : Node_Id;
753 begin
754 -- Ada 2005 (AI-251): In class-wide interface objects we displace
755 -- "this" to reference the base of the object. This is required to
756 -- get access to the TSD of the object.
758 if Is_Class_Wide_Type (Etype (Exp))
759 and then Is_Interface (Etype (Exp))
760 then
761 -- If the expression is an explicit dereference then we can
762 -- directly displace the pointer to reference the base of
763 -- the object.
765 if Nkind (Exp) = N_Explicit_Dereference then
766 Tag_Node :=
767 Make_Explicit_Dereference (Loc,
768 Prefix =>
769 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
770 Make_Function_Call (Loc,
771 Name =>
772 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
773 Parameter_Associations => New_List (
774 Unchecked_Convert_To (RTE (RE_Address),
775 Duplicate_Subexpr (Prefix (Exp)))))));
777 -- Similar case to the previous one but the expression is a
778 -- renaming of an explicit dereference.
780 elsif Nkind (Exp) = N_Identifier
781 and then Present (Renamed_Object (Entity (Exp)))
782 and then Nkind (Renamed_Object (Entity (Exp)))
783 = N_Explicit_Dereference
784 then
785 Tag_Node :=
786 Make_Explicit_Dereference (Loc,
787 Prefix =>
788 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
789 Make_Function_Call (Loc,
790 Name =>
791 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
792 Parameter_Associations => New_List (
793 Unchecked_Convert_To (RTE (RE_Address),
794 Duplicate_Subexpr
795 (Prefix
796 (Renamed_Object (Entity (Exp)))))))));
798 -- Common case: obtain the address of the actual object and
799 -- displace the pointer to reference the base of the object.
801 else
802 Tag_Node :=
803 Make_Explicit_Dereference (Loc,
804 Prefix =>
805 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
806 Make_Function_Call (Loc,
807 Name =>
808 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
809 Parameter_Associations => New_List (
810 Make_Attribute_Reference (Loc,
811 Prefix => Duplicate_Subexpr (Exp),
812 Attribute_Name => Name_Address)))));
813 end if;
814 else
815 Tag_Node :=
816 Make_Attribute_Reference (Loc,
817 Prefix => Duplicate_Subexpr (Exp),
818 Attribute_Name => Name_Tag);
819 end if;
821 -- Suppress junk access chacks on RE_Tag_Ptr
823 Insert_Action (Exp,
824 Make_Raise_Program_Error (Loc,
825 Condition =>
826 Make_Op_Gt (Loc,
827 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
828 Right_Opnd =>
829 Make_Integer_Literal (Loc,
830 Scope_Depth (Enclosing_Dynamic_Scope (Func)))),
831 Reason => PE_Accessibility_Check_Failed),
832 Suppress => Access_Check);
833 end;
834 end if;
835 end Apply_CW_Accessibility_Check;
837 -----------------------
838 -- BIP_Formal_Suffix --
839 -----------------------
841 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
842 begin
843 case Kind is
844 when BIP_Alloc_Form =>
845 return BIP_Alloc_Suffix;
847 when BIP_Storage_Pool =>
848 return BIP_Storage_Pool_Suffix;
850 when BIP_Collection =>
851 return BIP_Collection_Suffix;
853 when BIP_Task_Master =>
854 return BIP_Task_Master_Suffix;
856 when BIP_Activation_Chain =>
857 return BIP_Activation_Chain_Suffix;
859 when BIP_Object_Access =>
860 return BIP_Object_Access_Suffix;
861 end case;
862 end BIP_Formal_Suffix;
864 ---------------------
865 -- BIP_Suffix_Kind --
866 ---------------------
868 function BIP_Suffix_Kind (E : Entity_Id) return BIP_Formal_Kind is
869 Nam : constant String := Get_Name_String (Chars (E));
871 function Has_Suffix (Suffix : String) return Boolean;
872 -- Return True if Nam has suffix Suffix
874 function Has_Suffix (Suffix : String) return Boolean is
875 Len : constant Natural := Suffix'Length;
876 begin
877 return Nam'Length > Len
878 and then Nam (Nam'Last - Len + 1 .. Nam'Last) = Suffix;
879 end Has_Suffix;
881 -- Start of processing for BIP_Suffix_Kind
883 begin
884 if Has_Suffix (BIP_Alloc_Suffix) then
885 return BIP_Alloc_Form;
887 elsif Has_Suffix (BIP_Storage_Pool_Suffix) then
888 return BIP_Storage_Pool;
890 elsif Has_Suffix (BIP_Collection_Suffix) then
891 return BIP_Collection;
893 elsif Has_Suffix (BIP_Task_Master_Suffix) then
894 return BIP_Task_Master;
896 elsif Has_Suffix (BIP_Activation_Chain_Suffix) then
897 return BIP_Activation_Chain;
899 elsif Has_Suffix (BIP_Object_Access_Suffix) then
900 return BIP_Object_Access;
902 else
903 raise Program_Error;
904 end if;
905 end BIP_Suffix_Kind;
907 ---------------------------
908 -- Build_In_Place_Formal --
909 ---------------------------
911 function Build_In_Place_Formal
912 (Func : Entity_Id;
913 Kind : BIP_Formal_Kind) return Entity_Id
915 Extra_Formal : Entity_Id := Extra_Formals (Func);
916 Formal_Suffix : constant String := BIP_Formal_Suffix (Kind);
918 begin
919 -- Maybe it would be better for each implicit formal of a build-in-place
920 -- function to have a flag or a Uint attribute to identify it. ???
922 -- The return type in the function declaration may have been a limited
923 -- view, and the extra formals for the function were not generated at
924 -- that point. At the point of call the full view must be available and
925 -- the extra formals can be created and Returns_By_Ref computed.
927 if No (Extra_Formal) then
928 Create_Extra_Formals (Func);
929 Extra_Formal := Extra_Formals (Func);
930 Compute_Returns_By_Ref (Func);
931 end if;
933 -- We search for a formal with a matching suffix. We can't search
934 -- for the full name, because of the code at the end of Sem_Ch6.-
935 -- Create_Extra_Formals, which copies the Extra_Formals over to
936 -- the Alias of an instance, which will cause the formals to have
937 -- "incorrect" names.
939 while Present (Extra_Formal) loop
940 declare
941 Name : constant String := Get_Name_String (Chars (Extra_Formal));
942 begin
943 exit when Name'Length >= Formal_Suffix'Length
944 and then Formal_Suffix =
945 Name (Name'Last - Formal_Suffix'Length + 1 .. Name'Last);
946 end;
948 Next_Formal_With_Extras (Extra_Formal);
949 end loop;
951 if No (Extra_Formal) then
952 raise Program_Error;
953 end if;
955 return Extra_Formal;
956 end Build_In_Place_Formal;
958 -------------------------------
959 -- Build_Procedure_Body_Form --
960 -------------------------------
962 function Build_Procedure_Body_Form
963 (Func_Id : Entity_Id;
964 Func_Body : Node_Id) return Node_Id
966 Loc : constant Source_Ptr := Sloc (Func_Body);
968 Proc_Decl : constant Node_Id := Prev (Unit_Declaration_Node (Func_Id));
969 -- It is assumed that the node before the declaration of the
970 -- corresponding subprogram spec is the declaration of the procedure
971 -- form.
973 Proc_Id : constant Entity_Id := Defining_Entity (Proc_Decl);
975 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id);
976 -- Replace each return statement found in the list Stmts with an
977 -- assignment of the return expression to parameter Param_Id.
979 ---------------------
980 -- Replace_Returns --
981 ---------------------
983 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id) is
984 Stmt : Node_Id;
986 begin
987 Stmt := First (Stmts);
988 while Present (Stmt) loop
989 if Nkind (Stmt) = N_Block_Statement then
990 Replace_Returns (Param_Id,
991 Statements (Handled_Statement_Sequence (Stmt)));
993 elsif Nkind (Stmt) = N_Case_Statement then
994 declare
995 Alt : Node_Id;
996 begin
997 Alt := First (Alternatives (Stmt));
998 while Present (Alt) loop
999 Replace_Returns (Param_Id, Statements (Alt));
1000 Next (Alt);
1001 end loop;
1002 end;
1004 elsif Nkind (Stmt) = N_Extended_Return_Statement then
1005 declare
1006 Ret_Obj : constant Entity_Id :=
1007 Defining_Entity
1008 (First (Return_Object_Declarations (Stmt)));
1009 Assign : constant Node_Id :=
1010 Make_Assignment_Statement (Sloc (Stmt),
1011 Name =>
1012 New_Occurrence_Of (Param_Id, Loc),
1013 Expression =>
1014 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
1015 Stmts : List_Id;
1017 begin
1018 -- The extended return may just contain the declaration
1020 if Present (Handled_Statement_Sequence (Stmt)) then
1021 Stmts := Statements (Handled_Statement_Sequence (Stmt));
1022 else
1023 Stmts := New_List;
1024 end if;
1026 Set_Assignment_OK (Name (Assign));
1028 Rewrite (Stmt,
1029 Make_Block_Statement (Sloc (Stmt),
1030 Declarations =>
1031 Return_Object_Declarations (Stmt),
1032 Handled_Statement_Sequence =>
1033 Make_Handled_Sequence_Of_Statements (Loc,
1034 Statements => Stmts)));
1036 Replace_Returns (Param_Id, Stmts);
1038 Append_To (Stmts, Assign);
1039 Append_To (Stmts, Make_Simple_Return_Statement (Loc));
1040 end;
1042 elsif Nkind (Stmt) = N_If_Statement then
1043 Replace_Returns (Param_Id, Then_Statements (Stmt));
1044 Replace_Returns (Param_Id, Else_Statements (Stmt));
1046 declare
1047 Part : Node_Id;
1048 begin
1049 Part := First (Elsif_Parts (Stmt));
1050 while Present (Part) loop
1051 Replace_Returns (Param_Id, Then_Statements (Part));
1052 Next (Part);
1053 end loop;
1054 end;
1056 elsif Nkind (Stmt) = N_Loop_Statement then
1057 Replace_Returns (Param_Id, Statements (Stmt));
1059 elsif Nkind (Stmt) = N_Simple_Return_Statement then
1061 -- Generate:
1062 -- Param := Expr;
1063 -- return;
1065 Rewrite (Stmt,
1066 Make_Assignment_Statement (Sloc (Stmt),
1067 Name => New_Occurrence_Of (Param_Id, Loc),
1068 Expression => Relocate_Node (Expression (Stmt))));
1070 Insert_After (Stmt, Make_Simple_Return_Statement (Loc));
1072 -- Skip the added return
1074 Next (Stmt);
1075 end if;
1077 Next (Stmt);
1078 end loop;
1079 end Replace_Returns;
1081 -- Local variables
1083 Stmts : List_Id;
1084 New_Body : Node_Id;
1086 -- Start of processing for Build_Procedure_Body_Form
1088 begin
1089 -- This routine replaces the original function body:
1091 -- function F (...) return Array_Typ is
1092 -- begin
1093 -- ...
1094 -- return Something;
1095 -- end F;
1097 -- with the following:
1099 -- procedure P (..., Result : out Array_Typ) is
1100 -- begin
1101 -- ...
1102 -- Result := Something;
1103 -- end P;
1105 Stmts :=
1106 Statements (Handled_Statement_Sequence (Func_Body));
1107 Replace_Returns (Last_Entity (Proc_Id), Stmts);
1109 New_Body :=
1110 Make_Subprogram_Body (Loc,
1111 Specification =>
1112 Copy_Subprogram_Spec (Specification (Proc_Decl)),
1113 Declarations => Declarations (Func_Body),
1114 Handled_Statement_Sequence =>
1115 Make_Handled_Sequence_Of_Statements (Loc,
1116 Statements => Stmts));
1118 -- If the function is a generic instance, so is the new procedure.
1119 -- Set flag accordingly so that the proper renaming declarations are
1120 -- generated.
1122 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
1123 return New_Body;
1124 end Build_Procedure_Body_Form;
1126 -----------------------
1127 -- Caller_Known_Size --
1128 -----------------------
1130 function Caller_Known_Size
1131 (Func_Call : Node_Id;
1132 Result_Subt : Entity_Id) return Boolean
1134 Utyp : constant Entity_Id := Underlying_Type (Result_Subt);
1136 begin
1137 return not Needs_Secondary_Stack (Utyp)
1138 and then not (Is_Tagged_Type (Utyp)
1139 and then Present (Controlling_Argument (Func_Call)));
1140 end Caller_Known_Size;
1142 -----------------------
1143 -- Check_BIP_Actuals --
1144 -----------------------
1146 function Check_BIP_Actuals
1147 (Subp_Call : Node_Id;
1148 Subp_Id : Entity_Id) return Boolean
1150 Formal : Entity_Id;
1151 Actual : Node_Id;
1153 begin
1154 pragma Assert (Nkind (Subp_Call) in N_Entry_Call_Statement
1155 | N_Function_Call
1156 | N_Procedure_Call_Statement);
1158 -- In CodePeer_Mode, the tree for `'Elab_Spec` procedures will be
1159 -- malformed because GNAT does not perform the usual expansion that
1160 -- results in the importation of external elaboration procedure symbols.
1161 -- This is expected: the CodePeer backend has special handling for this
1162 -- malformed tree.
1163 -- Thus, we do not need to check the tree (and in fact can't, because
1164 -- it's malformed).
1166 if CodePeer_Mode
1167 and then Nkind (Name (Subp_Call)) = N_Attribute_Reference
1168 and then Attribute_Name (Name (Subp_Call)) in Name_Elab_Spec
1169 | Name_Elab_Body
1170 | Name_Elab_Subp_Body
1171 then
1172 return True;
1173 end if;
1175 Formal := First_Formal_With_Extras (Subp_Id);
1176 Actual := First_Actual (Subp_Call);
1178 while Present (Formal) and then Present (Actual) loop
1179 if Is_Build_In_Place_Entity (Formal)
1180 and then Nkind (Actual) = N_Identifier
1181 and then Is_Build_In_Place_Entity (Entity (Actual))
1182 and then BIP_Suffix_Kind (Formal)
1183 /= BIP_Suffix_Kind (Entity (Actual))
1184 then
1185 return False;
1186 end if;
1188 Next_Formal_With_Extras (Formal);
1189 Next_Actual (Actual);
1190 end loop;
1192 return No (Formal) and then No (Actual);
1193 end Check_BIP_Actuals;
1195 -----------------------------
1196 -- Check_Number_Of_Actuals --
1197 -----------------------------
1199 function Check_Number_Of_Actuals
1200 (Subp_Call : Node_Id;
1201 Subp_Id : Entity_Id) return Boolean
1203 Formal : Entity_Id;
1204 Actual : Node_Id;
1206 begin
1207 pragma Assert (Nkind (Subp_Call) in N_Entry_Call_Statement
1208 | N_Function_Call
1209 | N_Procedure_Call_Statement);
1211 Formal := First_Formal_With_Extras (Subp_Id);
1212 Actual := First_Actual (Subp_Call);
1214 while Present (Formal) and then Present (Actual) loop
1215 Next_Formal_With_Extras (Formal);
1216 Next_Actual (Actual);
1217 end loop;
1219 return No (Formal) and then No (Actual);
1220 end Check_Number_Of_Actuals;
1222 --------------------------------
1223 -- Check_Overriding_Operation --
1224 --------------------------------
1226 procedure Check_Overriding_Operation (Subp : Entity_Id) is
1227 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1228 Op_List : constant Elist_Id := Primitive_Operations (Typ);
1229 Op_Elmt : Elmt_Id;
1230 Prim_Op : Entity_Id;
1231 Par_Op : Entity_Id;
1233 begin
1234 if Is_Derived_Type (Typ)
1235 and then not Is_Private_Type (Typ)
1236 and then In_Open_Scopes (Scope (Etype (Typ)))
1237 and then Is_Base_Type (Typ)
1238 then
1239 -- Subp overrides an inherited private operation if there is an
1240 -- inherited operation with a different name than Subp (see
1241 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
1242 -- same name as Subp.
1244 Op_Elmt := First_Elmt (Op_List);
1245 while Present (Op_Elmt) loop
1246 Prim_Op := Node (Op_Elmt);
1247 Par_Op := Alias (Prim_Op);
1249 if Present (Par_Op)
1250 and then not Comes_From_Source (Prim_Op)
1251 and then Chars (Prim_Op) /= Chars (Par_Op)
1252 and then Chars (Par_Op) = Chars (Subp)
1253 and then Is_Hidden (Par_Op)
1254 and then Type_Conformant (Prim_Op, Subp)
1255 then
1256 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
1257 end if;
1259 Next_Elmt (Op_Elmt);
1260 end loop;
1261 end if;
1262 end Check_Overriding_Operation;
1264 -------------------------------
1265 -- Detect_Infinite_Recursion --
1266 -------------------------------
1268 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
1269 Loc : constant Source_Ptr := Sloc (N);
1271 Var_List : constant Elist_Id := New_Elmt_List;
1272 -- List of globals referenced by body of procedure
1274 Call_List : constant Elist_Id := New_Elmt_List;
1275 -- List of recursive calls in body of procedure
1277 Shad_List : constant Elist_Id := New_Elmt_List;
1278 -- List of entity id's for entities created to capture the value of
1279 -- referenced globals on entry to the procedure.
1281 Scop : constant Uint := Scope_Depth (Spec);
1282 -- This is used to record the scope depth of the current procedure, so
1283 -- that we can identify global references.
1285 Max_Vars : constant := 4;
1286 -- Do not test more than four global variables
1288 Count_Vars : Natural := 0;
1289 -- Count variables found so far
1291 Var : Entity_Id;
1292 Elm : Elmt_Id;
1293 Ent : Entity_Id;
1294 Call : Elmt_Id;
1295 Decl : Node_Id;
1296 Test : Node_Id;
1297 Elm1 : Elmt_Id;
1298 Elm2 : Elmt_Id;
1299 Last : Node_Id;
1301 function Process (Nod : Node_Id) return Traverse_Result;
1302 -- Function to traverse the subprogram body (using Traverse_Func)
1304 -------------
1305 -- Process --
1306 -------------
1308 function Process (Nod : Node_Id) return Traverse_Result is
1309 begin
1310 -- Procedure call
1312 if Nkind (Nod) = N_Procedure_Call_Statement then
1314 -- Case of one of the detected recursive calls
1316 if Is_Entity_Name (Name (Nod))
1317 and then Has_Recursive_Call (Entity (Name (Nod)))
1318 and then Entity (Name (Nod)) = Spec
1319 then
1320 Append_Elmt (Nod, Call_List);
1321 return Skip;
1323 -- Any other procedure call may have side effects
1325 else
1326 return Abandon;
1327 end if;
1329 -- A call to a pure function can always be ignored
1331 elsif Nkind (Nod) = N_Function_Call
1332 and then Is_Entity_Name (Name (Nod))
1333 and then Is_Pure (Entity (Name (Nod)))
1334 then
1335 return Skip;
1337 -- Case of an identifier reference
1339 elsif Nkind (Nod) = N_Identifier then
1340 Ent := Entity (Nod);
1342 -- If no entity, then ignore the reference
1344 -- Not clear why this can happen. To investigate, remove this
1345 -- test and look at the crash that occurs here in 3401-004 ???
1347 if No (Ent) then
1348 return Skip;
1350 -- Ignore entities with no Scope, again not clear how this
1351 -- can happen, to investigate, look at 4108-008 ???
1353 elsif No (Scope (Ent)) then
1354 return Skip;
1356 -- Ignore the reference if not to a more global object
1358 elsif Scope_Depth (Scope (Ent)) >= Scop then
1359 return Skip;
1361 -- References to types, exceptions and constants are always OK
1363 elsif Is_Type (Ent)
1364 or else Ekind (Ent) = E_Exception
1365 or else Ekind (Ent) = E_Constant
1366 then
1367 return Skip;
1369 -- If other than a non-volatile scalar variable, we have some
1370 -- kind of global reference (e.g. to a function) that we cannot
1371 -- deal with so we forget the attempt.
1373 elsif Ekind (Ent) /= E_Variable
1374 or else not Is_Scalar_Type (Etype (Ent))
1375 or else Treat_As_Volatile (Ent)
1376 then
1377 return Abandon;
1379 -- Otherwise we have a reference to a global scalar
1381 else
1382 -- Loop through global entities already detected
1384 Elm := First_Elmt (Var_List);
1385 loop
1386 -- If not detected before, record this new global reference
1388 if No (Elm) then
1389 Count_Vars := Count_Vars + 1;
1391 if Count_Vars <= Max_Vars then
1392 Append_Elmt (Entity (Nod), Var_List);
1393 else
1394 return Abandon;
1395 end if;
1397 exit;
1399 -- If recorded before, ignore
1401 elsif Node (Elm) = Entity (Nod) then
1402 return Skip;
1404 -- Otherwise keep looking
1406 else
1407 Next_Elmt (Elm);
1408 end if;
1409 end loop;
1411 return Skip;
1412 end if;
1414 -- For all other node kinds, recursively visit syntactic children
1416 else
1417 return OK;
1418 end if;
1419 end Process;
1421 function Traverse_Body is new Traverse_Func (Process);
1423 -- Start of processing for Detect_Infinite_Recursion
1425 begin
1426 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1427 -- won't be able to generate the code to handle the recursion in any
1428 -- case.
1430 if Restriction_Active (No_Implicit_Conditionals) then
1431 return;
1432 end if;
1434 -- Otherwise do traversal and quit if we get abandon signal
1436 if Traverse_Body (N) = Abandon then
1437 return;
1439 -- We must have a call, since Has_Recursive_Call was set. If not just
1440 -- ignore (this is only an error check, so if we have a funny situation,
1441 -- due to bugs or errors, we do not want to bomb).
1443 elsif Is_Empty_Elmt_List (Call_List) then
1444 return;
1445 end if;
1447 -- Here is the case where we detect recursion at compile time
1449 -- Push our current scope for analyzing the declarations and code that
1450 -- we will insert for the checking.
1452 Push_Scope (Spec);
1454 -- This loop builds temporary variables for each of the referenced
1455 -- globals, so that at the end of the loop the list Shad_List contains
1456 -- these temporaries in one-to-one correspondence with the elements in
1457 -- Var_List.
1459 Last := Empty;
1460 Elm := First_Elmt (Var_List);
1461 while Present (Elm) loop
1462 Var := Node (Elm);
1463 Ent := Make_Temporary (Loc, 'S');
1464 Append_Elmt (Ent, Shad_List);
1466 -- Insert a declaration for this temporary at the start of the
1467 -- declarations for the procedure. The temporaries are declared as
1468 -- constant objects initialized to the current values of the
1469 -- corresponding temporaries.
1471 Decl :=
1472 Make_Object_Declaration (Loc,
1473 Defining_Identifier => Ent,
1474 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
1475 Constant_Present => True,
1476 Expression => New_Occurrence_Of (Var, Loc));
1478 if No (Last) then
1479 Prepend (Decl, Declarations (N));
1480 else
1481 Insert_After (Last, Decl);
1482 end if;
1484 Last := Decl;
1485 Analyze (Decl);
1486 Next_Elmt (Elm);
1487 end loop;
1489 -- Loop through calls
1491 Call := First_Elmt (Call_List);
1492 while Present (Call) loop
1494 -- Build a predicate expression of the form
1496 -- True
1497 -- and then global1 = temp1
1498 -- and then global2 = temp2
1499 -- ...
1501 -- This predicate determines if any of the global values
1502 -- referenced by the procedure have changed since the
1503 -- current call, if not an infinite recursion is assured.
1505 Test := New_Occurrence_Of (Standard_True, Loc);
1507 Elm1 := First_Elmt (Var_List);
1508 Elm2 := First_Elmt (Shad_List);
1509 while Present (Elm1) loop
1510 Test :=
1511 Make_And_Then (Loc,
1512 Left_Opnd => Test,
1513 Right_Opnd =>
1514 Make_Op_Eq (Loc,
1515 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1516 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1518 Next_Elmt (Elm1);
1519 Next_Elmt (Elm2);
1520 end loop;
1522 -- Now we replace the call with the sequence
1524 -- if no-changes (see above) then
1525 -- raise Storage_Error;
1526 -- else
1527 -- original-call
1528 -- end if;
1530 Rewrite (Node (Call),
1531 Make_If_Statement (Loc,
1532 Condition => Test,
1533 Then_Statements => New_List (
1534 Make_Raise_Storage_Error (Loc,
1535 Reason => SE_Infinite_Recursion)),
1537 Else_Statements => New_List (
1538 Relocate_Node (Node (Call)))));
1540 Analyze (Node (Call));
1542 Next_Elmt (Call);
1543 end loop;
1545 -- Remove temporary scope stack entry used for analysis
1547 Pop_Scope;
1548 end Detect_Infinite_Recursion;
1550 --------------------
1551 -- Expand_Actuals --
1552 --------------------
1554 procedure Expand_Actuals
1555 (N : Node_Id;
1556 Subp : Entity_Id;
1557 Post_Call : out List_Id)
1559 Loc : constant Source_Ptr := Sloc (N);
1560 Actual : Node_Id;
1561 Formal : Entity_Id;
1562 N_Node : Node_Id;
1563 E_Actual : Entity_Id;
1564 E_Formal : Entity_Id;
1566 procedure Add_Call_By_Copy_Code;
1567 -- For cases where the parameter must be passed by copy, this routine
1568 -- generates a temporary variable into which the actual is copied and
1569 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1570 -- an assignment is also generated to copy the result back. The call
1571 -- also takes care of any constraint checks required for the type
1572 -- conversion case (on both the way in and the way out).
1574 procedure Add_Simple_Call_By_Copy_Code (Force : Boolean);
1575 -- This is similar to the above, but is used in cases where we know
1576 -- that all that is needed is to simply create a temporary and copy
1577 -- the value in and out of the temporary. If Force is True, then the
1578 -- procedure may disregard legality considerations.
1580 -- ??? We need to do the copy for a bit-packed array because this is
1581 -- where the rewriting into a mask-and-shift sequence is done. But of
1582 -- course this may break the program if it expects bits to be really
1583 -- passed by reference. That's what we have done historically though.
1585 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id);
1586 -- Perform copy-back for actual parameter Act which denotes a validation
1587 -- variable.
1589 procedure Check_Fortran_Logical;
1590 -- A value of type Logical that is passed through a formal parameter
1591 -- must be normalized because .TRUE. usually does not have the same
1592 -- representation as True. We assume that .FALSE. = False = 0.
1593 -- What about functions that return a logical type ???
1595 function Is_Legal_Copy return Boolean;
1596 -- Check that an actual can be copied before generating the temporary
1597 -- to be used in the call. If the formal is of a by_reference type or
1598 -- is aliased, then the program is illegal (this can only happen in
1599 -- the presence of representation clauses that force a misalignment)
1600 -- If the formal is a by_reference parameter imposed by a DEC pragma,
1601 -- emit a warning that this might lead to unaligned arguments.
1603 function Make_Var (Actual : Node_Id) return Entity_Id;
1604 -- Returns an entity that refers to the given actual parameter, Actual
1605 -- (not including any type conversion). If Actual is an entity name,
1606 -- then this entity is returned unchanged, otherwise a renaming is
1607 -- created to provide an entity for the actual.
1609 procedure Reset_Packed_Prefix;
1610 -- The expansion of a packed array component reference is delayed in
1611 -- the context of a call. Now we need to complete the expansion, so we
1612 -- unmark the analyzed bits in all prefixes.
1614 function Requires_Atomic_Or_Volatile_Copy return Boolean;
1615 -- Returns whether a copy is required as per RM C.6(19) and gives a
1616 -- warning in this case.
1618 ---------------------------
1619 -- Add_Call_By_Copy_Code --
1620 ---------------------------
1622 procedure Add_Call_By_Copy_Code is
1623 Crep : Boolean;
1624 Expr : Node_Id;
1625 F_Typ : Entity_Id := Etype (Formal);
1626 Indic : Node_Id;
1627 Init : Node_Id;
1628 Temp : Entity_Id;
1629 V_Typ : Entity_Id;
1630 Var : Entity_Id;
1632 begin
1633 if not Is_Legal_Copy then
1634 return;
1635 end if;
1637 Temp := Make_Temporary (Loc, 'T', Actual);
1639 -- Handle formals whose type comes from the limited view
1641 if From_Limited_With (F_Typ)
1642 and then Has_Non_Limited_View (F_Typ)
1643 then
1644 F_Typ := Non_Limited_View (F_Typ);
1645 end if;
1647 -- Use formal type for temp, unless formal type is an unconstrained
1648 -- array, in which case we don't have to worry about bounds checks,
1649 -- and we use the actual type, since that has appropriate bounds.
1651 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1652 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1653 else
1654 Indic := New_Occurrence_Of (F_Typ, Loc);
1655 end if;
1657 -- The new code will be properly analyzed below and the setting of
1658 -- the Do_Range_Check flag recomputed so remove the obsolete one.
1660 Set_Do_Range_Check (Actual, False);
1662 if Nkind (Actual) = N_Type_Conversion then
1663 Set_Do_Range_Check (Expression (Actual), False);
1665 V_Typ := Etype (Expression (Actual));
1667 -- If the formal is an (in-)out parameter, capture the name
1668 -- of the variable in order to build the post-call assignment.
1670 Var := Make_Var (Expression (Actual));
1672 Crep := not Has_Compatible_Representation
1673 (Target_Typ => F_Typ,
1674 Operand_Typ => Etype (Expression (Actual)));
1676 else
1677 V_Typ := Etype (Actual);
1678 Var := Make_Var (Actual);
1679 Crep := False;
1680 end if;
1682 -- If the actual denotes a variable which captures the value of an
1683 -- object for validation purposes, we propagate the link with this
1684 -- object to the new variable made from the actual just above.
1686 if Ekind (Formal) /= E_In_Parameter
1687 and then Is_Validation_Variable_Reference (Actual)
1688 then
1689 declare
1690 Ref : constant Node_Id := Unqual_Conv (Actual);
1692 begin
1693 if Is_Entity_Name (Ref) then
1694 Set_Validated_Object (Var, Validated_Object (Entity (Ref)));
1696 else
1697 pragma Assert (False);
1698 null;
1699 end if;
1700 end;
1701 end if;
1703 -- Setup initialization for case of in out parameter, or an out
1704 -- parameter where the formal is an unconstrained array (in the
1705 -- latter case, we have to pass in an object with bounds).
1707 -- If this is an out parameter, the initial copy is wasteful, so as
1708 -- an optimization for the one-dimensional case we extract the
1709 -- bounds of the actual and build an uninitialized temporary of the
1710 -- right size.
1712 -- If the formal is an out parameter with discriminants, the
1713 -- discriminants must be captured even if the rest of the object
1714 -- is in principle uninitialized, because the discriminants may
1715 -- be read by the called subprogram.
1717 if Ekind (Formal) = E_In_Out_Parameter
1718 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1719 or else Has_Discriminants (F_Typ)
1720 then
1721 if Nkind (Actual) = N_Type_Conversion then
1722 if Conversion_OK (Actual) then
1723 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1724 else
1725 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1726 end if;
1728 elsif Ekind (Formal) = E_Out_Parameter
1729 and then Is_Array_Type (F_Typ)
1730 and then Number_Dimensions (F_Typ) = 1
1731 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1732 then
1733 -- Actual is a one-dimensional array or slice, and the type
1734 -- requires no initialization. Create a temporary of the
1735 -- right size, but do not copy actual into it (optimization).
1737 Init := Empty;
1738 Indic :=
1739 Make_Subtype_Indication (Loc,
1740 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1741 Constraint =>
1742 Make_Index_Or_Discriminant_Constraint (Loc,
1743 Constraints => New_List (
1744 Make_Range (Loc,
1745 Low_Bound =>
1746 Make_Attribute_Reference (Loc,
1747 Prefix => New_Occurrence_Of (Var, Loc),
1748 Attribute_Name => Name_First),
1749 High_Bound =>
1750 Make_Attribute_Reference (Loc,
1751 Prefix => New_Occurrence_Of (Var, Loc),
1752 Attribute_Name => Name_Last)))));
1754 else
1755 Init := New_Occurrence_Of (Var, Loc);
1756 end if;
1758 -- An initialization is created for packed conversions as
1759 -- actuals for out parameters to enable Make_Object_Declaration
1760 -- to determine the proper subtype for N_Node. Note that this
1761 -- is wasteful because the extra copying on the call side is
1762 -- not required for such out parameters. ???
1764 elsif Ekind (Formal) = E_Out_Parameter
1765 and then Nkind (Actual) = N_Type_Conversion
1766 and then (Is_Bit_Packed_Array (F_Typ)
1767 or else
1768 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1769 then
1770 if Conversion_OK (Actual) then
1771 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1772 else
1773 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1774 end if;
1776 elsif Ekind (Formal) = E_In_Parameter then
1778 -- Handle the case in which the actual is a type conversion
1780 if Nkind (Actual) = N_Type_Conversion then
1781 if Conversion_OK (Actual) then
1782 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1783 else
1784 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1785 end if;
1786 else
1787 Init := New_Occurrence_Of (Var, Loc);
1788 end if;
1790 -- Access types are passed in without checks, but if a copy-back is
1791 -- required for a null-excluding check on an in-out or out parameter,
1792 -- then the initial value is that of the actual.
1794 elsif Is_Access_Type (E_Formal)
1795 and then Can_Never_Be_Null (Etype (Actual))
1796 and then not Can_Never_Be_Null (E_Formal)
1797 then
1798 Init := New_Occurrence_Of (Var, Loc);
1800 -- View conversions when the formal type has the Default_Value aspect
1801 -- require passing in the value of the conversion's operand. The type
1802 -- of that operand also has Default_Value, as required by AI12-0074
1803 -- (RM 6.4.1(5.3/4)). The subtype denoted by the subtype_indication
1804 -- is changed to the base type of the formal subtype, to ensure that
1805 -- the actual's value can be assigned without a constraint check
1806 -- (note that no check is done on passing to an out parameter). Also
1807 -- note that the two types necessarily share the same ancestor type,
1808 -- as required by 6.4.1(5.2/4), so underlying base types will match.
1810 elsif Ekind (Formal) = E_Out_Parameter
1811 and then Is_Scalar_Type (Etype (F_Typ))
1812 and then Nkind (Actual) = N_Type_Conversion
1813 and then Present (Default_Aspect_Value (Etype (F_Typ)))
1814 then
1815 Indic := New_Occurrence_Of (Base_Type (F_Typ), Loc);
1816 Init := Convert_To
1817 (Base_Type (F_Typ), New_Occurrence_Of (Var, Loc));
1819 else
1820 Init := Empty;
1821 end if;
1823 N_Node :=
1824 Make_Object_Declaration (Loc,
1825 Defining_Identifier => Temp,
1826 Object_Definition => Indic,
1827 Expression => Init);
1828 Set_Assignment_OK (N_Node);
1829 Insert_Action (N, N_Node);
1831 -- Now, normally the deal here is that we use the defining
1832 -- identifier created by that object declaration. There is
1833 -- one exception to this. In the change of representation case
1834 -- the above declaration will end up looking like:
1836 -- temp : type := identifier;
1838 -- And in this case we might as well use the identifier directly
1839 -- and eliminate the temporary. Note that the analysis of the
1840 -- declaration was not a waste of time in that case, since it is
1841 -- what generated the necessary change of representation code. If
1842 -- the change of representation introduced additional code, as in
1843 -- a fixed-integer conversion, the expression is not an identifier
1844 -- and must be kept.
1846 if Crep
1847 and then Present (Expression (N_Node))
1848 and then Is_Entity_Name (Expression (N_Node))
1849 then
1850 Temp := Entity (Expression (N_Node));
1851 Rewrite (N_Node, Make_Null_Statement (Loc));
1852 end if;
1854 -- For IN parameter, all we do is to replace the actual
1856 if Ekind (Formal) = E_In_Parameter then
1857 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1858 Analyze (Actual);
1860 -- Processing for OUT or IN OUT parameter
1862 else
1863 -- Kill current value indications for the temporary variable we
1864 -- created, since we just passed it as an OUT parameter.
1866 Kill_Current_Values (Temp);
1867 Set_Is_Known_Valid (Temp, False);
1868 Set_Is_True_Constant (Temp, False);
1870 -- If type conversion, use reverse conversion on exit
1872 if Nkind (Actual) = N_Type_Conversion then
1873 if Conversion_OK (Actual) then
1874 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1875 else
1876 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1877 end if;
1878 else
1879 Expr := New_Occurrence_Of (Temp, Loc);
1880 end if;
1882 Rewrite (Actual, New_Occurrence_Of (Temp, Sloc (Actual)));
1883 Analyze (Actual);
1885 -- If the actual is a conversion of a packed reference, it may
1886 -- already have been expanded by Remove_Side_Effects, and the
1887 -- resulting variable is a temporary which does not designate
1888 -- the proper out-parameter, which may not be addressable. In
1889 -- that case, generate an assignment to the original expression
1890 -- (before expansion of the packed reference) so that the proper
1891 -- expansion of assignment to a packed component can take place.
1893 declare
1894 Obj : Node_Id;
1895 Lhs : Node_Id;
1897 begin
1898 if Is_Renaming_Of_Object (Var)
1899 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1900 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1901 = N_Indexed_Component
1902 and then
1903 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1904 then
1905 Obj := Renamed_Object (Var);
1906 Lhs :=
1907 Make_Selected_Component (Loc,
1908 Prefix =>
1909 New_Copy_Tree (Original_Node (Prefix (Obj))),
1910 Selector_Name => New_Copy (Selector_Name (Obj)));
1911 Reset_Analyzed_Flags (Lhs);
1913 else
1914 Lhs := New_Occurrence_Of (Var, Loc);
1915 end if;
1917 Set_Assignment_OK (Lhs);
1919 if Is_Access_Type (E_Formal)
1920 and then Is_Entity_Name (Lhs)
1921 and then
1922 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1923 and then not No_Dynamic_Accessibility_Checks_Enabled (Lhs)
1924 then
1925 -- Copyback target is an Ada 2012 stand-alone object of an
1926 -- anonymous access type.
1928 pragma Assert (Ada_Version >= Ada_2012);
1930 Apply_Accessibility_Check (Lhs, E_Formal, N);
1932 Append_To (Post_Call,
1933 Make_Assignment_Statement (Loc,
1934 Name => Lhs,
1935 Expression => Expr));
1937 -- We would like to somehow suppress generation of the
1938 -- extra_accessibility assignment generated by the expansion
1939 -- of the above assignment statement. It's not a correctness
1940 -- issue because the following assignment renders it dead,
1941 -- but generating back-to-back assignments to the same
1942 -- target is undesirable. ???
1944 Append_To (Post_Call,
1945 Make_Assignment_Statement (Loc,
1946 Name => New_Occurrence_Of (
1947 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1948 Expression => Make_Integer_Literal (Loc,
1949 Type_Access_Level (E_Formal))));
1951 else
1952 if Is_Access_Type (E_Formal)
1953 and then Can_Never_Be_Null (Etype (Actual))
1954 and then not Can_Never_Be_Null (E_Formal)
1955 then
1956 Append_To (Post_Call,
1957 Make_Raise_Constraint_Error (Loc,
1958 Condition =>
1959 Make_Op_Eq (Loc,
1960 Left_Opnd => New_Occurrence_Of (Temp, Loc),
1961 Right_Opnd => Make_Null (Loc)),
1962 Reason => CE_Access_Check_Failed));
1963 end if;
1965 Append_To (Post_Call,
1966 Make_Assignment_Statement (Loc,
1967 Name => Lhs,
1968 Expression => Expr));
1969 end if;
1971 -- Add a copy-back to reflect any potential changes in value
1972 -- back into the original object, if any.
1974 if Is_Validation_Variable_Reference (Lhs) then
1975 Add_Validation_Call_By_Copy_Code (Lhs);
1976 end if;
1977 end;
1978 end if;
1979 end Add_Call_By_Copy_Code;
1981 ----------------------------------
1982 -- Add_Simple_Call_By_Copy_Code --
1983 ----------------------------------
1985 procedure Add_Simple_Call_By_Copy_Code (Force : Boolean) is
1986 With_Storage_Model : constant Boolean :=
1987 Nkind (Actual) = N_Explicit_Dereference
1988 and then
1989 Has_Designated_Storage_Model_Aspect (Etype (Prefix (Actual)));
1991 Cpcod : List_Id;
1992 Decl : Node_Id;
1993 F_Typ : Entity_Id;
1994 Incod : Node_Id;
1995 Indic : Node_Id;
1996 Lhs : Node_Id;
1997 Outcod : Node_Id;
1998 Rhs : Node_Id;
1999 Temp : Entity_Id;
2001 begin
2002 -- Unless forced not to, check the legality of the copy operation
2004 if not Force and then not Is_Legal_Copy then
2005 return;
2006 end if;
2008 F_Typ := Etype (Formal);
2010 -- Handle formals whose type comes from the limited view
2012 if From_Limited_With (F_Typ)
2013 and then Has_Non_Limited_View (F_Typ)
2014 then
2015 F_Typ := Non_Limited_View (F_Typ);
2016 end if;
2018 -- Use formal type for temp, unless formal type is an unconstrained
2019 -- composite, in which case we don't have to worry about checks and
2020 -- we can use the actual type, since that has appropriate bounds.
2022 if Is_Composite_Type (F_Typ) and then not Is_Constrained (F_Typ) then
2023 Indic := New_Occurrence_Of (Get_Actual_Subtype (Actual), Loc);
2024 else
2025 Indic := New_Occurrence_Of (F_Typ, Loc);
2026 end if;
2028 -- Prepare to generate code
2030 Reset_Packed_Prefix;
2032 Incod := Relocate_Node (Actual);
2033 Outcod := New_Copy_Tree (Incod);
2035 -- Generate declaration of temporary variable, initializing it
2036 -- with the input parameter unless we have an OUT formal or
2037 -- this is an initialization call.
2039 if Ekind (Formal) = E_Out_Parameter then
2040 Incod := Empty;
2042 elsif Inside_Init_Proc then
2044 -- Skip using the actual as the expression in Decl if we are in
2045 -- an init proc and it is not a component which depends on a
2046 -- discriminant, because, in this case, we need to use the actual
2047 -- type of the component instead.
2049 if Nkind (Actual) /= N_Selected_Component
2050 or else
2051 not Has_Discriminant_Dependent_Constraint
2052 (Entity (Selector_Name (Actual)))
2053 then
2054 Incod := Empty;
2056 -- Otherwise, keep the component so we can generate the proper
2057 -- actual subtype - since the subtype depends on enclosing
2058 -- discriminants.
2060 else
2061 null;
2062 end if;
2063 end if;
2065 Cpcod := New_List;
2067 if With_Storage_Model then
2068 Temp :=
2069 Build_Temporary_On_Secondary_Stack (Loc, Entity (Indic), Cpcod);
2071 if Present (Incod) then
2072 Append_To (Cpcod,
2073 Make_Assignment_Statement (Loc,
2074 Name =>
2075 Make_Explicit_Dereference (Loc,
2076 Prefix => New_Occurrence_Of (Temp, Loc)),
2077 Expression => Incod));
2078 Set_Suppress_Assignment_Checks (Last (Cpcod));
2079 end if;
2081 else
2082 Temp := Make_Temporary (Loc, 'T', Actual);
2084 Decl :=
2085 Make_Object_Declaration (Loc,
2086 Defining_Identifier => Temp,
2087 Object_Definition => Indic,
2088 Expression => Incod);
2090 -- If the call is to initialize a component of a composite type,
2091 -- and the component does not depend on discriminants, use the
2092 -- actual type of the component. This is required in case the
2093 -- component is constrained, because in general the formal of the
2094 -- initialization procedure will be unconstrained. Note that if
2095 -- the component being initialized is constrained by an enclosing
2096 -- discriminant, the presence of the initialization in the
2097 -- declaration will generate an expression for the actual subtype.
2099 if Inside_Init_Proc and then No (Incod) then
2100 Set_No_Initialization (Decl);
2101 Set_Object_Definition (Decl,
2102 New_Occurrence_Of (Etype (Actual), Loc));
2103 end if;
2105 Append_To (Cpcod, Decl);
2106 end if;
2108 Insert_Actions (N, Cpcod);
2110 -- The actual is simply a reference to the temporary
2112 if With_Storage_Model then
2113 Rewrite (Actual,
2114 Make_Explicit_Dereference (Loc,
2115 Prefix => New_Occurrence_Of (Temp, Loc)));
2116 else
2117 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
2118 end if;
2120 Analyze (Actual);
2122 -- Generate copy out if OUT or IN OUT parameter
2124 if Ekind (Formal) /= E_In_Parameter then
2125 Lhs := Outcod;
2127 if With_Storage_Model then
2128 Rhs :=
2129 Make_Explicit_Dereference (Loc,
2130 Prefix => New_Occurrence_Of (Temp, Loc));
2131 else
2132 Rhs := New_Occurrence_Of (Temp, Loc);
2133 Set_Is_True_Constant (Temp, False);
2134 end if;
2136 -- Deal with conversion
2138 if Nkind (Lhs) = N_Type_Conversion then
2139 Lhs := Expression (Lhs);
2140 Rhs := Convert_To (Etype (Actual), Rhs);
2141 end if;
2143 Append_To (Post_Call,
2144 Make_Assignment_Statement (Loc,
2145 Name => Lhs,
2146 Expression => Rhs));
2147 Set_Suppress_Assignment_Checks (Last (Post_Call));
2148 Set_Assignment_OK (Name (Last (Post_Call)));
2149 end if;
2150 end Add_Simple_Call_By_Copy_Code;
2152 --------------------------------------
2153 -- Add_Validation_Call_By_Copy_Code --
2154 --------------------------------------
2156 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id) is
2157 Var : constant Node_Id := Unqual_Conv (Act);
2159 Expr : Node_Id;
2160 Obj : Node_Id;
2161 Obj_Typ : Entity_Id;
2162 Var_Id : Entity_Id;
2164 begin
2165 -- Generate range check if required
2167 if Do_Range_Check (Actual) then
2168 Generate_Range_Check (Actual, E_Formal, CE_Range_Check_Failed);
2169 end if;
2171 -- If there is a type conversion in the actual, it will be reinstated
2172 -- below, the new instance will be properly analyzed and the setting
2173 -- of the Do_Range_Check flag recomputed so remove the obsolete one.
2175 if Nkind (Actual) = N_Type_Conversion then
2176 Set_Do_Range_Check (Expression (Actual), False);
2177 end if;
2179 -- Copy the value of the validation variable back into the object
2180 -- being validated.
2182 if Is_Entity_Name (Var) then
2183 Var_Id := Entity (Var);
2184 Obj := Validated_Object (Var_Id);
2185 Obj_Typ := Etype (Obj);
2187 Expr := New_Occurrence_Of (Var_Id, Loc);
2189 -- A type conversion is needed when the validation variable and
2190 -- the validated object carry different types. This case occurs
2191 -- when the actual is qualified in some fashion.
2193 -- Common:
2194 -- subtype Int is Integer range ...;
2195 -- procedure Call (Val : in out Integer);
2197 -- Original:
2198 -- Object : Int;
2199 -- Call (Integer (Object));
2201 -- Expanded:
2202 -- Object : Int;
2203 -- Var : Integer := Object; -- conversion to base type
2204 -- if not Var'Valid then -- validity check
2205 -- Call (Var); -- modify Var
2206 -- Object := Int (Var); -- conversion to subtype
2208 if Etype (Var_Id) /= Obj_Typ then
2209 Expr :=
2210 Make_Type_Conversion (Loc,
2211 Subtype_Mark => New_Occurrence_Of (Obj_Typ, Loc),
2212 Expression => Expr);
2213 end if;
2215 -- Generate:
2216 -- Object := Var;
2217 -- <or>
2218 -- Object := Object_Type (Var);
2220 Append_To (Post_Call,
2221 Make_Assignment_Statement (Loc,
2222 Name => Obj,
2223 Expression => Expr));
2225 -- If the flow reaches this point, then this routine was invoked with
2226 -- an actual which does not denote a validation variable.
2228 else
2229 pragma Assert (False);
2230 null;
2231 end if;
2232 end Add_Validation_Call_By_Copy_Code;
2234 ---------------------------
2235 -- Check_Fortran_Logical --
2236 ---------------------------
2238 procedure Check_Fortran_Logical is
2239 Logical : constant Entity_Id := Etype (Formal);
2240 Var : Entity_Id;
2242 -- Note: this is very incomplete, e.g. it does not handle arrays
2243 -- of logical values. This is really not the right approach at all???)
2245 begin
2246 if Convention (Subp) = Convention_Fortran
2247 and then Root_Type (Etype (Formal)) = Standard_Boolean
2248 and then Ekind (Formal) /= E_In_Parameter
2249 then
2250 Var := Make_Var (Actual);
2251 Append_To (Post_Call,
2252 Make_Assignment_Statement (Loc,
2253 Name => New_Occurrence_Of (Var, Loc),
2254 Expression =>
2255 Unchecked_Convert_To (
2256 Logical,
2257 Make_Op_Ne (Loc,
2258 Left_Opnd => New_Occurrence_Of (Var, Loc),
2259 Right_Opnd =>
2260 Unchecked_Convert_To (
2261 Logical,
2262 New_Occurrence_Of (Standard_False, Loc))))));
2263 end if;
2264 end Check_Fortran_Logical;
2266 -------------------
2267 -- Is_Legal_Copy --
2268 -------------------
2270 function Is_Legal_Copy return Boolean is
2271 begin
2272 -- An attempt to copy a value of such a type can only occur if
2273 -- representation clauses give the actual a misaligned address.
2275 if Is_By_Reference_Type (Etype (Formal))
2276 or else Is_Aliased (Formal)
2277 or else (Mechanism (Formal) = By_Reference
2278 and then not Has_Foreign_Convention (Subp))
2279 then
2281 -- The actual may in fact be properly aligned but there is not
2282 -- enough front-end information to determine this. In that case
2283 -- gigi will emit an error or a warning if a copy is not legal,
2284 -- or generate the proper code.
2286 return False;
2288 -- For users of Starlet, we assume that the specification of by-
2289 -- reference mechanism is mandatory. This may lead to unaligned
2290 -- objects but at least for DEC legacy code it is known to work.
2291 -- The warning will alert users of this code that a problem may
2292 -- be lurking.
2294 elsif Mechanism (Formal) = By_Reference
2295 and then Ekind (Scope (Formal)) = E_Procedure
2296 and then Is_Valued_Procedure (Scope (Formal))
2297 then
2298 Error_Msg_N
2299 ("by_reference actual may be misaligned??", Actual);
2300 return False;
2302 else
2303 return True;
2304 end if;
2305 end Is_Legal_Copy;
2307 --------------
2308 -- Make_Var --
2309 --------------
2311 function Make_Var (Actual : Node_Id) return Entity_Id is
2312 Var : Entity_Id;
2314 begin
2315 if Is_Entity_Name (Actual) then
2316 return Entity (Actual);
2318 else
2319 Var := Make_Temporary (Loc, 'T', Actual);
2321 N_Node :=
2322 Make_Object_Renaming_Declaration (Loc,
2323 Defining_Identifier => Var,
2324 Subtype_Mark =>
2325 New_Occurrence_Of (Etype (Actual), Loc),
2326 Name => Relocate_Node (Actual));
2328 Insert_Action (N, N_Node);
2329 return Var;
2330 end if;
2331 end Make_Var;
2333 -------------------------
2334 -- Reset_Packed_Prefix --
2335 -------------------------
2337 procedure Reset_Packed_Prefix is
2338 Pfx : Node_Id := Actual;
2339 begin
2340 loop
2341 Set_Analyzed (Pfx, False);
2342 exit when
2343 Nkind (Pfx) not in N_Selected_Component | N_Indexed_Component;
2344 Pfx := Prefix (Pfx);
2345 end loop;
2346 end Reset_Packed_Prefix;
2348 ----------------------------------------
2349 -- Requires_Atomic_Or_Volatile_Copy --
2350 ----------------------------------------
2352 function Requires_Atomic_Or_Volatile_Copy return Boolean is
2353 begin
2354 -- If the formal is already passed by copy, no need to do anything
2356 if Is_By_Copy_Type (E_Formal) then
2357 return False;
2358 end if;
2360 -- There is no requirement inside initialization procedures and this
2361 -- would generate copies for atomic or volatile composite components.
2363 if Inside_Init_Proc then
2364 return False;
2365 end if;
2367 -- Check for atomicity mismatch
2369 if Is_Atomic_Object (Actual) and then not Is_Atomic (E_Formal)
2370 then
2371 if Comes_From_Source (N) then
2372 Error_Msg_N
2373 ("??atomic actual passed by copy (RM C.6(19))", Actual);
2374 end if;
2375 return True;
2376 end if;
2378 -- Check for volatility mismatch
2380 if Is_Volatile_Object_Ref (Actual) and then not Is_Volatile (E_Formal)
2381 then
2382 if Comes_From_Source (N) then
2383 Error_Msg_N
2384 ("??volatile actual passed by copy (RM C.6(19))", Actual);
2385 end if;
2386 return True;
2387 end if;
2389 return False;
2390 end Requires_Atomic_Or_Volatile_Copy;
2392 -- Start of processing for Expand_Actuals
2394 begin
2395 Post_Call := New_List;
2397 Formal := First_Formal (Subp);
2398 Actual := First_Actual (N);
2399 while Present (Formal) loop
2400 E_Formal := Etype (Formal);
2401 E_Actual := Etype (Actual);
2403 -- Handle formals whose type comes from the limited view
2405 if From_Limited_With (E_Formal)
2406 and then Has_Non_Limited_View (E_Formal)
2407 then
2408 E_Formal := Non_Limited_View (E_Formal);
2409 end if;
2411 if Is_Scalar_Type (E_Formal)
2412 or else Nkind (Actual) = N_Slice
2413 then
2414 Check_Fortran_Logical;
2416 -- RM 6.4.1 (11)
2418 elsif Ekind (Formal) /= E_Out_Parameter then
2420 -- The unusual case of the current instance of a protected type
2421 -- requires special handling. This can only occur in the context
2422 -- of a call within the body of a protected operation.
2424 if Is_Entity_Name (Actual)
2425 and then Ekind (Entity (Actual)) = E_Protected_Type
2426 and then In_Open_Scopes (Entity (Actual))
2427 then
2428 if Scope (Subp) /= Entity (Actual) then
2429 Error_Msg_N
2430 ("operation outside protected type may not "
2431 & "call back its protected operations??", Actual);
2432 end if;
2434 Rewrite (Actual,
2435 Expand_Protected_Object_Reference (N, Entity (Actual)));
2436 end if;
2438 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
2439 -- build-in-place function, then a temporary return object needs
2440 -- to be created and access to it must be passed to the function
2441 -- (and ensure that we have an activation chain defined for tasks
2442 -- and a Master variable).
2444 -- Currently we limit such functions to those with inherently
2445 -- limited result subtypes, but eventually we plan to expand the
2446 -- functions that are treated as build-in-place to include other
2447 -- composite result types.
2449 -- But do not do it here for intrinsic subprograms since this will
2450 -- be done properly after the subprogram is expanded.
2452 if Is_Intrinsic_Subprogram (Subp) then
2453 null;
2455 elsif Is_Build_In_Place_Function_Call (Actual) then
2456 if Might_Have_Tasks (Etype (Actual)) then
2457 Build_Activation_Chain_Entity (N);
2458 Build_Master_Entity (Etype (Actual));
2459 end if;
2461 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
2463 -- Ada 2005 (AI-318-02): Specialization of the previous case for
2464 -- actuals containing build-in-place function calls whose returned
2465 -- object covers interface types.
2467 elsif Present (Unqual_BIP_Iface_Function_Call (Actual)) then
2468 Build_Activation_Chain_Entity (N);
2469 Build_Master_Entity (Etype (Actual));
2470 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Actual);
2471 end if;
2473 Apply_Constraint_Check (Actual, E_Formal);
2475 -- Out parameter case. No constraint checks on access type
2476 -- RM 6.4.1 (13), but on return a null-excluding check may be
2477 -- required (see below).
2479 elsif Is_Access_Type (E_Formal) then
2480 null;
2482 -- RM 6.4.1 (14)
2484 elsif Has_Discriminants (Base_Type (E_Formal))
2485 or else Has_Non_Null_Base_Init_Proc (E_Formal)
2486 then
2487 Apply_Constraint_Check (Actual, E_Formal);
2489 -- RM 6.4.1 (15)
2491 else
2492 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
2493 end if;
2495 -- Processing for IN-OUT and OUT parameters
2497 if Ekind (Formal) /= E_In_Parameter then
2499 -- For type conversions of arrays, apply length/range checks
2501 if Is_Array_Type (E_Formal)
2502 and then Nkind (Actual) = N_Type_Conversion
2503 then
2504 if Is_Constrained (E_Formal) then
2505 Apply_Length_Check (Expression (Actual), E_Formal);
2506 else
2507 Apply_Range_Check (Expression (Actual), E_Formal);
2508 end if;
2509 end if;
2511 -- If argument is a type conversion for a type that is passed by
2512 -- copy, then we must pass the parameter by copy.
2514 if Nkind (Actual) = N_Type_Conversion
2515 and then
2516 (Is_Elementary_Type (E_Formal)
2517 or else Is_Bit_Packed_Array (Etype (Formal))
2518 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
2520 -- Also pass by copy if change of representation
2522 or else not Has_Compatible_Representation
2523 (Target_Typ => Etype (Formal),
2524 Operand_Typ => Etype (Expression (Actual))))
2525 then
2526 Add_Call_By_Copy_Code;
2528 -- References to components of bit-packed arrays are expanded
2529 -- at this point, rather than at the point of analysis of the
2530 -- actuals, to handle the expansion of the assignment to
2531 -- [in] out parameters.
2533 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2534 Add_Simple_Call_By_Copy_Code (Force => True);
2536 -- If the actual has a nonnative storage model, we need a copy
2538 elsif Nkind (Actual) = N_Explicit_Dereference
2539 and then
2540 Has_Designated_Storage_Model_Aspect (Etype (Prefix (Actual)))
2541 and then
2542 (Present (Storage_Model_Copy_To
2543 (Storage_Model_Object (Etype (Prefix (Actual)))))
2544 or else
2545 (Ekind (Formal) = E_In_Out_Parameter
2546 and then
2547 Present (Storage_Model_Copy_From
2548 (Storage_Model_Object (Etype (Prefix (Actual)))))))
2549 then
2550 Add_Simple_Call_By_Copy_Code (Force => True);
2552 -- If a nonscalar actual is possibly bit-aligned, we need a copy
2553 -- because the back-end cannot cope with such objects. In other
2554 -- cases where alignment forces a copy, the back-end generates
2555 -- it properly. It should not be generated unconditionally in the
2556 -- front-end because it does not know precisely the alignment
2557 -- requirements of the target, and makes too conservative an
2558 -- estimate, leading to superfluous copies or spurious errors
2559 -- on by-reference parameters.
2561 elsif Nkind (Actual) = N_Selected_Component
2562 and then
2563 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
2564 and then not Represented_As_Scalar (Etype (Formal))
2565 then
2566 Add_Simple_Call_By_Copy_Code (Force => False);
2568 -- References to slices of bit-packed arrays are expanded
2570 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2571 Add_Call_By_Copy_Code;
2573 -- References to possibly unaligned slices of arrays are expanded
2575 elsif Is_Possibly_Unaligned_Slice (Actual) then
2576 Add_Call_By_Copy_Code;
2578 -- Deal with access types where the actual subtype and the
2579 -- formal subtype are not the same, requiring a check.
2581 -- It is necessary to exclude tagged types because of "downward
2582 -- conversion" errors, but null-excluding checks on return may be
2583 -- required.
2585 elsif Is_Access_Type (E_Formal)
2586 and then not Is_Tagged_Type (Designated_Type (E_Formal))
2587 and then (not Same_Type (E_Formal, E_Actual)
2588 or else (Can_Never_Be_Null (E_Actual)
2589 and then not Can_Never_Be_Null (E_Formal)))
2590 then
2591 Add_Call_By_Copy_Code;
2593 -- We may need to force a copy because of atomicity or volatility
2594 -- considerations.
2596 elsif Requires_Atomic_Or_Volatile_Copy then
2597 Add_Call_By_Copy_Code;
2599 -- Add call-by-copy code for the case of scalar out parameters
2600 -- when it is not known at compile time that the subtype of the
2601 -- formal is a subrange of the subtype of the actual (or vice
2602 -- versa for in out parameters), in order to get range checks
2603 -- on such actuals. (Maybe this case should be handled earlier
2604 -- in the if statement???)
2606 elsif Is_Scalar_Type (E_Formal)
2607 and then
2608 (not In_Subrange_Of (E_Formal, E_Actual)
2609 or else
2610 (Ekind (Formal) = E_In_Out_Parameter
2611 and then not In_Subrange_Of (E_Actual, E_Formal)))
2612 then
2613 Add_Call_By_Copy_Code;
2615 -- The actual denotes a variable which captures the value of an
2616 -- object for validation purposes. Add a copy-back to reflect any
2617 -- potential changes in value back into the original object.
2619 -- Var : ... := Object;
2620 -- if not Var'Valid then -- validity check
2621 -- Call (Var); -- modify var
2622 -- Object := Var; -- update Object
2624 elsif Is_Validation_Variable_Reference (Actual) then
2625 Add_Validation_Call_By_Copy_Code (Actual);
2626 end if;
2628 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2629 -- by-reference parameters on exit from the call. If the actual
2630 -- is a derived type and the operation is inherited, the body
2631 -- of the operation will not contain a call to the predicate
2632 -- function, so it must be done explicitly after the call. Ditto
2633 -- if the actual is an entity of a predicated subtype.
2635 -- The rule refers to by-reference types, but a check is needed
2636 -- for by-copy types as well. That check is subsumed by the rule
2637 -- for subtype conversion on assignment, but we can generate the
2638 -- required check now.
2640 -- Note also that Subp may be either a subprogram entity for
2641 -- direct calls, or a type entity for indirect calls, which must
2642 -- be handled separately because the name does not denote an
2643 -- overloadable entity.
2645 By_Ref_Predicate_Check : declare
2646 Aund : constant Entity_Id := Underlying_Type (E_Actual);
2647 Atyp : Entity_Id;
2649 begin
2650 if No (Aund) then
2651 Atyp := E_Actual;
2652 else
2653 Atyp := Aund;
2654 end if;
2656 if Predicate_Enabled (Atyp)
2658 -- Skip predicate checks for special cases
2660 and then Predicate_Tests_On_Arguments (Subp)
2661 then
2662 Append_To (Post_Call,
2663 Make_Predicate_Check (Atyp, Actual));
2664 end if;
2665 end By_Ref_Predicate_Check;
2667 -- Processing for IN parameters
2669 else
2670 -- Generate range check if required
2672 if Do_Range_Check (Actual) then
2673 Generate_Range_Check (Actual, E_Formal, CE_Range_Check_Failed);
2674 end if;
2676 -- For IN parameters in the bit-packed array case, we expand an
2677 -- indexed component (the circuit in Exp_Ch4 deliberately left
2678 -- indexed components appearing as actuals untouched, so that
2679 -- the special processing above for the OUT and IN OUT cases
2680 -- could be performed. We could make the test in Exp_Ch4 more
2681 -- complex and have it detect the parameter mode, but it is
2682 -- easier simply to handle all cases here.)
2684 if Nkind (Actual) = N_Indexed_Component
2685 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2686 then
2687 Reset_Packed_Prefix;
2688 Expand_Packed_Element_Reference (Actual);
2690 -- If we have a reference to a bit-packed array, we copy it, since
2691 -- the actual must be byte aligned.
2693 -- Is this really necessary in all cases???
2695 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2696 Add_Simple_Call_By_Copy_Code (Force => True);
2698 -- If the actual has a nonnative storage model, we need a copy
2700 elsif Nkind (Actual) = N_Explicit_Dereference
2701 and then
2702 Has_Designated_Storage_Model_Aspect (Etype (Prefix (Actual)))
2703 and then
2704 Present (Storage_Model_Copy_From
2705 (Storage_Model_Object (Etype (Prefix (Actual)))))
2706 then
2707 Add_Simple_Call_By_Copy_Code (Force => True);
2709 -- If we have a C++ constructor call, we need to create the object
2711 elsif Is_CPP_Constructor_Call (Actual) then
2712 Add_Simple_Call_By_Copy_Code (Force => True);
2714 -- If a nonscalar actual is possibly unaligned, we need a copy
2716 elsif Is_Possibly_Unaligned_Object (Actual)
2717 and then not Represented_As_Scalar (Etype (Formal))
2718 then
2719 Add_Simple_Call_By_Copy_Code (Force => False);
2721 -- Similarly, we have to expand slices of packed arrays here
2722 -- because the result must be byte aligned.
2724 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2725 Add_Call_By_Copy_Code;
2727 -- Only processing remaining is to pass by copy if this is a
2728 -- reference to a possibly unaligned slice, since the caller
2729 -- expects an appropriately aligned argument.
2731 elsif Is_Possibly_Unaligned_Slice (Actual) then
2732 Add_Call_By_Copy_Code;
2734 -- We may need to force a copy because of atomicity or volatility
2735 -- considerations.
2737 elsif Requires_Atomic_Or_Volatile_Copy then
2738 Add_Call_By_Copy_Code;
2740 -- An unusual case: a current instance of an enclosing task can be
2741 -- an actual, and must be replaced by a reference to self.
2743 elsif Is_Entity_Name (Actual)
2744 and then Is_Task_Type (Entity (Actual))
2745 then
2746 if In_Open_Scopes (Entity (Actual)) then
2747 Rewrite (Actual,
2748 (Make_Function_Call (Loc,
2749 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2750 Analyze (Actual);
2752 -- A task type cannot otherwise appear as an actual
2754 else
2755 raise Program_Error;
2756 end if;
2757 end if;
2758 end if;
2760 -- Type-invariant checks for in-out and out parameters, as well as
2761 -- for in parameters of procedures (AI05-0289 and AI12-0044).
2763 if Ekind (Formal) /= E_In_Parameter
2764 or else Ekind (Subp) = E_Procedure
2765 then
2766 Caller_Side_Invariant_Checks : declare
2768 function Is_Public_Subp return Boolean;
2769 -- Check whether the subprogram being called is a visible
2770 -- operation of the type of the actual. Used to determine
2771 -- whether an invariant check must be generated on the
2772 -- caller side.
2774 ---------------------
2775 -- Is_Public_Subp --
2776 ---------------------
2778 function Is_Public_Subp return Boolean is
2779 Pack : constant Entity_Id := Scope (Subp);
2780 Subp_Decl : Node_Id;
2782 begin
2783 if not Is_Subprogram (Subp) then
2784 return False;
2786 -- The operation may be inherited, or a primitive of the
2787 -- root type.
2789 elsif
2790 Nkind (Parent (Subp)) in N_Private_Extension_Declaration
2791 | N_Full_Type_Declaration
2792 then
2793 Subp_Decl := Parent (Subp);
2795 else
2796 Subp_Decl := Unit_Declaration_Node (Subp);
2797 end if;
2799 return Ekind (Pack) = E_Package
2800 and then
2801 List_Containing (Subp_Decl) =
2802 Visible_Declarations
2803 (Specification (Unit_Declaration_Node (Pack)));
2804 end Is_Public_Subp;
2806 -- Start of processing for Caller_Side_Invariant_Checks
2808 begin
2809 -- We generate caller-side invariant checks in two cases:
2811 -- a) when calling an inherited operation, where there is an
2812 -- implicit view conversion of the actual to the parent type.
2814 -- b) When the conversion is explicit
2816 -- We treat these cases separately because the required
2817 -- conversion for a) is added later when expanding the call.
2819 if Has_Invariants (Etype (Actual))
2820 and then
2821 Nkind (Parent (Etype (Actual)))
2822 = N_Private_Extension_Declaration
2823 then
2824 if Comes_From_Source (N) and then Is_Public_Subp then
2825 Append_To (Post_Call, Make_Invariant_Call (Actual));
2826 end if;
2828 elsif Nkind (Actual) = N_Type_Conversion
2829 and then Has_Invariants (Etype (Expression (Actual)))
2830 then
2831 if Comes_From_Source (N) and then Is_Public_Subp then
2832 Append_To
2833 (Post_Call, Make_Invariant_Call (Expression (Actual)));
2834 end if;
2835 end if;
2836 end Caller_Side_Invariant_Checks;
2837 end if;
2839 Next_Formal (Formal);
2840 Next_Actual (Actual);
2841 end loop;
2842 end Expand_Actuals;
2844 -----------------
2845 -- Expand_Call --
2846 -----------------
2848 procedure Expand_Call (N : Node_Id) is
2849 function Is_Unchecked_Union_Equality (N : Node_Id) return Boolean;
2850 -- Return True if N is a call to the predefined equality operator of an
2851 -- unchecked union type, or a renaming thereof.
2853 ---------------------------------
2854 -- Is_Unchecked_Union_Equality --
2855 ---------------------------------
2857 function Is_Unchecked_Union_Equality (N : Node_Id) return Boolean is
2858 begin
2859 if Is_Entity_Name (Name (N))
2860 and then Ekind (Entity (Name (N))) = E_Function
2861 and then Present (First_Formal (Entity (Name (N))))
2862 and then
2863 Is_Unchecked_Union (Etype (First_Formal (Entity (Name (N)))))
2864 then
2865 declare
2866 Func : constant Entity_Id := Entity (Name (N));
2867 Typ : constant Entity_Id := Etype (First_Formal (Func));
2868 Decl : constant Node_Id :=
2869 Original_Node (Parent (Declaration_Node (Func)));
2871 begin
2872 return Func = TSS (Typ, TSS_Composite_Equality)
2873 or else (Nkind (Decl) = N_Subprogram_Renaming_Declaration
2874 and then Nkind (Name (Decl)) = N_Operator_Symbol
2875 and then Chars (Name (Decl)) = Name_Op_Eq
2876 and then Ekind (Entity (Name (Decl))) = E_Operator);
2877 end;
2879 else
2880 return False;
2881 end if;
2882 end Is_Unchecked_Union_Equality;
2884 -- If this is an indirect call through an Access_To_Subprogram
2885 -- with contract specifications, it is rewritten as a call to
2886 -- the corresponding Access_Subprogram_Wrapper with the same
2887 -- actuals, whose body contains a naked indirect call (which
2888 -- itself must not be rewritten, to prevent infinite recursion).
2890 Must_Rewrite_Indirect_Call : constant Boolean :=
2891 Ada_Version >= Ada_2022
2892 and then Nkind (Name (N)) = N_Explicit_Dereference
2893 and then Ekind (Etype (Name (N))) = E_Subprogram_Type
2894 and then Present
2895 (Access_Subprogram_Wrapper (Etype (Name (N))));
2897 Post_Call : List_Id;
2899 -- Start of processing for Expand_Call
2901 begin
2902 pragma Assert (Nkind (N) in N_Entry_Call_Statement
2903 | N_Function_Call
2904 | N_Procedure_Call_Statement);
2906 -- Check that this is not the call in the body of the access
2907 -- subprogram wrapper or the postconditions wrapper.
2909 if Must_Rewrite_Indirect_Call
2910 and then (not Is_Overloadable (Current_Scope)
2911 or else not (Is_Access_Subprogram_Wrapper (Current_Scope)
2912 or else
2913 (Chars (Current_Scope) = Name_uWrapped_Statements
2914 and then Is_Access_Subprogram_Wrapper
2915 (Scope (Current_Scope)))))
2916 then
2917 declare
2918 Loc : constant Source_Ptr := Sloc (N);
2919 Wrapper : constant Entity_Id :=
2920 Access_Subprogram_Wrapper (Etype (Name (N)));
2921 Ptr : constant Node_Id := Prefix (Name (N));
2922 Ptr_Type : constant Entity_Id := Etype (Ptr);
2923 Typ : constant Entity_Id := Etype (N);
2925 New_N : Node_Id;
2926 Parms : List_Id := Parameter_Associations (N);
2927 Ptr_Act : Node_Id;
2929 begin
2930 -- The last actual in the call is the pointer itself.
2931 -- If the aspect is inherited, convert the pointer to the
2932 -- parent type that specifies the contract.
2933 -- If the original access_to_subprogram has defaults for
2934 -- in-mode parameters, the call may include named associations,
2935 -- so we create one for the pointer as well.
2937 if Is_Derived_Type (Ptr_Type)
2938 and then Ptr_Type /= Etype (Last_Formal (Wrapper))
2939 then
2940 Ptr_Act :=
2941 Make_Type_Conversion (Loc,
2942 New_Occurrence_Of
2943 (Etype (Last_Formal (Wrapper)), Loc), Ptr);
2945 else
2946 Ptr_Act := Ptr;
2947 end if;
2949 -- Handle parameterless subprogram.
2951 if No (Parms) then
2952 Parms := New_List;
2953 end if;
2955 Append
2956 (Make_Parameter_Association (Loc,
2957 Selector_Name => Make_Identifier (Loc,
2958 Chars (Last_Formal (Wrapper))),
2959 Explicit_Actual_Parameter => Ptr_Act),
2960 Parms);
2962 if Nkind (N) = N_Procedure_Call_Statement then
2963 New_N := Make_Procedure_Call_Statement (Loc,
2964 Name => New_Occurrence_Of (Wrapper, Loc),
2965 Parameter_Associations => Parms);
2966 else
2967 New_N := Make_Function_Call (Loc,
2968 Name => New_Occurrence_Of (Wrapper, Loc),
2969 Parameter_Associations => Parms);
2970 end if;
2972 Rewrite (N, New_N);
2973 Analyze_And_Resolve (N, Typ);
2974 end;
2976 -- Case of a call to the predefined equality operator of an unchecked
2977 -- union type, which requires specific processing.
2979 elsif Is_Unchecked_Union_Equality (N) then
2980 declare
2981 Eq : constant Entity_Id := Entity (Name (N));
2983 begin
2984 Expand_Unchecked_Union_Equality (N);
2986 -- If the call was not rewritten as a raise, expand the actuals
2988 if Nkind (N) = N_Function_Call then
2989 pragma Assert (Check_Number_Of_Actuals (N, Eq));
2990 Expand_Actuals (N, Eq, Post_Call);
2991 pragma Assert (Is_Empty_List (Post_Call));
2992 end if;
2993 end;
2995 -- Normal case
2997 else
2998 Expand_Call_Helper (N, Post_Call);
2999 Insert_Post_Call_Actions (N, Post_Call);
3000 end if;
3001 end Expand_Call;
3003 ------------------------
3004 -- Expand_Call_Helper --
3005 ------------------------
3007 -- This procedure handles expansion of function calls and procedure call
3008 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
3009 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
3011 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
3012 -- Provide values of actuals for all formals in Extra_Formals list
3013 -- Replace "call" to enumeration literal function by literal itself
3014 -- Rewrite call to predefined operator as operator
3015 -- Replace actuals to in-out parameters that are numeric conversions,
3016 -- with explicit assignment to temporaries before and after the call.
3018 -- Note that the list of actuals has been filled with default expressions
3019 -- during semantic analysis of the call. Only the extra actuals required
3020 -- for the 'Constrained attribute and for accessibility checks are added
3021 -- at this point.
3023 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is
3024 Loc : constant Source_Ptr := Sloc (N);
3025 Call_Node : Node_Id := N;
3026 Extra_Actuals : List_Id := No_List;
3027 Prev : Node_Id := Empty;
3029 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
3030 -- Adds one entry to the end of the actual parameter list. Used for
3031 -- default parameters and for extra actuals (for Extra_Formals). The
3032 -- argument is an N_Parameter_Association node.
3034 procedure Add_Cond_Expression_Extra_Actual (Formal : Entity_Id);
3035 -- Adds extra accessibility actuals in the case of a conditional
3036 -- expression corresponding to Formal.
3038 -- Note: Conditional expressions used as actuals for anonymous access
3039 -- formals complicate the process of propagating extra accessibility
3040 -- actuals and must be handled in a recursive fashion since they can
3041 -- be embedded within each other.
3043 procedure Add_Dummy_Build_In_Place_Actuals
3044 (Function_Id : Entity_Id;
3045 Num_Added_Extra_Actuals : Nat := 0);
3046 -- Adds dummy actuals for the BIP extra formals of the called function.
3047 -- Num_Added_Extra_Actuals is the number of non-BIP extra actuals added
3048 -- to the actuals immediately before calling this subprogram.
3050 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
3051 -- Adds an extra actual to the list of extra actuals. Expr is the
3052 -- expression for the value of the actual, EF is the entity for the
3053 -- extra formal.
3055 procedure Add_View_Conversion_Invariants
3056 (Formal : Entity_Id;
3057 Actual : Node_Id);
3058 -- Adds invariant checks for every intermediate type between the range
3059 -- of a view converted argument to its ancestor (from parent to child).
3061 function Can_Fold_Predicate_Call (P : Entity_Id) return Boolean;
3062 -- Try to constant-fold a predicate check, which often enough is a
3063 -- simple arithmetic expression that can be computed statically if
3064 -- its argument is static. This cleans up the output of CCG, even
3065 -- though useless predicate checks will be generally removed by
3066 -- back-end optimizations.
3068 procedure Check_Subprogram_Variant;
3069 -- Emit a call to the internally generated procedure with checks for
3070 -- aspect Subprogram_Variant, if present and enabled.
3072 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
3073 -- Within an instance, a type derived from an untagged formal derived
3074 -- type inherits from the original parent, not from the actual. The
3075 -- current derivation mechanism has the derived type inherit from the
3076 -- actual, which is only correct outside of the instance. If the
3077 -- subprogram is inherited, we test for this particular case through a
3078 -- convoluted tree traversal before setting the proper subprogram to be
3079 -- called.
3081 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
3082 -- Return true if E comes from an instance that is not yet frozen
3084 function Is_Class_Wide_Interface_Type (E : Entity_Id) return Boolean;
3085 -- Return True when E is a class-wide interface type or an access to
3086 -- a class-wide interface type.
3088 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
3089 -- Determine if Subp denotes a non-dispatching call to a Deep routine
3091 function New_Value (From : Node_Id) return Node_Id;
3092 -- From is the original Expression. New_Value is equivalent to a call
3093 -- to Duplicate_Subexpr with an explicit dereference when From is an
3094 -- access parameter.
3096 --------------------------
3097 -- Add_Actual_Parameter --
3098 --------------------------
3100 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
3101 Actual_Expr : constant Node_Id :=
3102 Explicit_Actual_Parameter (Insert_Param);
3104 begin
3105 -- Case of insertion is first named actual
3107 if No (Prev) or else
3108 Nkind (Parent (Prev)) /= N_Parameter_Association
3109 then
3110 Set_Next_Named_Actual
3111 (Insert_Param, First_Named_Actual (Call_Node));
3112 Set_First_Named_Actual (Call_Node, Actual_Expr);
3114 if No (Prev) then
3115 if No (Parameter_Associations (Call_Node)) then
3116 Set_Parameter_Associations (Call_Node, New_List);
3117 end if;
3119 Append (Insert_Param, Parameter_Associations (Call_Node));
3121 else
3122 Insert_After (Prev, Insert_Param);
3123 end if;
3125 -- Case of insertion is not first named actual
3127 else
3128 Set_Next_Named_Actual
3129 (Insert_Param, Next_Named_Actual (Parent (Prev)));
3130 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
3131 Append (Insert_Param, Parameter_Associations (Call_Node));
3132 end if;
3134 Prev := Actual_Expr;
3135 end Add_Actual_Parameter;
3137 --------------------------------------
3138 -- Add_Cond_Expression_Extra_Actual --
3139 --------------------------------------
3141 procedure Add_Cond_Expression_Extra_Actual
3142 (Formal : Entity_Id)
3144 Decl : Node_Id;
3145 Lvl : Entity_Id;
3147 procedure Insert_Level_Assign (Branch : Node_Id);
3148 -- Recursively add assignment of the level temporary on each branch
3149 -- while moving through nested conditional expressions.
3151 -------------------------
3152 -- Insert_Level_Assign --
3153 -------------------------
3155 procedure Insert_Level_Assign (Branch : Node_Id) is
3157 procedure Expand_Branch (Res_Assn : Node_Id);
3158 -- Perform expansion or iterate further within nested
3159 -- conditionals given the object declaration or assignment to
3160 -- result object created during expansion which represents a
3161 -- branch of the conditional expression.
3163 -------------------
3164 -- Expand_Branch --
3165 -------------------
3167 procedure Expand_Branch (Res_Assn : Node_Id) is
3168 begin
3169 pragma Assert (Nkind (Res_Assn) in
3170 N_Assignment_Statement |
3171 N_Object_Declaration);
3173 -- There are more nested conditional expressions so we must go
3174 -- deeper.
3176 if Nkind (Expression (Res_Assn)) = N_Expression_With_Actions
3177 and then
3178 Nkind (Original_Node (Expression (Res_Assn)))
3179 in N_Case_Expression | N_If_Expression
3180 then
3181 Insert_Level_Assign
3182 (Expression (Res_Assn));
3184 -- Add the level assignment
3186 else
3187 Insert_Before_And_Analyze (Res_Assn,
3188 Make_Assignment_Statement (Loc,
3189 Name => New_Occurrence_Of (Lvl, Loc),
3190 Expression =>
3191 Accessibility_Level
3192 (Expr => Expression (Res_Assn),
3193 Level => Dynamic_Level,
3194 Allow_Alt_Model => False)));
3195 end if;
3196 end Expand_Branch;
3198 Cond : Node_Id;
3199 Alt : Node_Id;
3201 -- Start of processing for Insert_Level_Assign
3203 begin
3204 -- Examine further nested conditionals
3206 pragma Assert (Nkind (Branch) =
3207 N_Expression_With_Actions);
3209 -- Find the relevant statement in the actions
3211 Cond := First (Actions (Branch));
3212 while Present (Cond) loop
3213 exit when Nkind (Cond) in N_Case_Statement | N_If_Statement;
3214 Next (Cond);
3215 end loop;
3217 -- The conditional expression may have been optimized away, so
3218 -- examine the actions in the branch.
3220 if No (Cond) then
3221 Expand_Branch (Last (Actions (Branch)));
3223 -- Iterate through if expression branches
3225 elsif Nkind (Cond) = N_If_Statement then
3226 Expand_Branch (Last (Then_Statements (Cond)));
3227 Expand_Branch (Last (Else_Statements (Cond)));
3229 -- Iterate through case alternatives
3231 elsif Nkind (Cond) = N_Case_Statement then
3233 Alt := First (Alternatives (Cond));
3234 while Present (Alt) loop
3235 Expand_Branch (Last (Statements (Alt)));
3236 Next (Alt);
3237 end loop;
3238 end if;
3239 end Insert_Level_Assign;
3241 -- Start of processing for cond expression case
3243 begin
3244 -- Create declaration of a temporary to store the accessibility
3245 -- level of each branch of the conditional expression.
3247 Lvl := Make_Temporary (Loc, 'L');
3248 Decl := Make_Object_Declaration (Loc,
3249 Defining_Identifier => Lvl,
3250 Object_Definition =>
3251 New_Occurrence_Of (Standard_Natural, Loc));
3253 -- Install the declaration and perform necessary expansion if we
3254 -- are dealing with a procedure call.
3256 if Nkind (Call_Node) = N_Procedure_Call_Statement then
3257 -- Generate:
3258 -- Lvl : Natural;
3259 -- Call (
3260 -- {do
3261 -- If_Exp_Res : Typ;
3262 -- if Cond then
3263 -- Lvl := 0; -- Access level
3264 -- If_Exp_Res := Exp;
3265 -- ...
3266 -- in If_Exp_Res end;},
3267 -- Lvl,
3268 -- ...
3269 -- )
3271 Insert_Before_And_Analyze (Call_Node, Decl);
3273 -- Ditto for a function call. Note that we do not wrap the function
3274 -- call into an expression with action to avoid bad interactions with
3275 -- Exp_Ch4.Process_Transient_In_Expression.
3277 else
3278 -- Generate:
3279 -- Lvl : Natural; -- placed above the function call
3280 -- ...
3281 -- Func_Call (
3282 -- {do
3283 -- If_Exp_Res : Typ
3284 -- if Cond then
3285 -- Lvl := 0; -- Access level
3286 -- If_Exp_Res := Exp;
3287 -- in If_Exp_Res end;},
3288 -- Lvl,
3289 -- ...
3290 -- )
3292 Insert_Action (Call_Node, Decl);
3293 Analyze (Call_Node);
3294 end if;
3296 -- Decorate the conditional expression with assignments to our level
3297 -- temporary.
3299 Insert_Level_Assign (Prev);
3301 -- Make our level temporary the passed actual
3303 Add_Extra_Actual
3304 (Expr => New_Occurrence_Of (Lvl, Loc),
3305 EF => Extra_Accessibility (Formal));
3306 end Add_Cond_Expression_Extra_Actual;
3308 --------------------------------------
3309 -- Add_Dummy_Build_In_Place_Actuals --
3310 --------------------------------------
3312 procedure Add_Dummy_Build_In_Place_Actuals
3313 (Function_Id : Entity_Id;
3314 Num_Added_Extra_Actuals : Nat := 0)
3316 Loc : constant Source_Ptr := Sloc (Call_Node);
3317 Formal : Entity_Id := Extra_Formals (Function_Id);
3318 Actual : Node_Id;
3319 Skip_Extra : Nat;
3321 begin
3322 -- We never generate extra formals if expansion is not active because
3323 -- we don't need them unless we are generating code. No action needed
3324 -- for thunks since they propagate all their extra actuals.
3326 if not Expander_Active
3327 or else Is_Thunk (Current_Scope)
3328 then
3329 return;
3330 end if;
3332 -- Skip already-added non-BIP extra actuals
3334 Skip_Extra := Num_Added_Extra_Actuals;
3335 while Skip_Extra > 0 loop
3336 pragma Assert (not Is_Build_In_Place_Entity (Formal));
3337 Formal := Extra_Formal (Formal);
3338 Skip_Extra := Skip_Extra - 1;
3339 end loop;
3341 -- Append the dummy BIP extra actuals
3343 while Present (Formal) loop
3344 pragma Assert (Is_Build_In_Place_Entity (Formal));
3346 -- BIPalloc
3348 if Etype (Formal) = Standard_Natural then
3349 Actual := Make_Integer_Literal (Loc, Uint_0);
3350 Analyze_And_Resolve (Actual, Standard_Natural);
3351 Add_Extra_Actual_To_Call (N, Formal, Actual);
3353 -- BIPtaskmaster
3355 elsif Etype (Formal) = Standard_Integer then
3356 Actual := Make_Integer_Literal (Loc, Uint_0);
3357 Analyze_And_Resolve (Actual, Standard_Integer);
3358 Add_Extra_Actual_To_Call (N, Formal, Actual);
3360 -- BIPstoragepool, BIPcollection, BIPactivationchain,
3361 -- and BIPaccess.
3363 elsif Is_Access_Type (Etype (Formal)) then
3364 Actual := Make_Null (Loc);
3365 Analyze_And_Resolve (Actual, Etype (Formal));
3366 Add_Extra_Actual_To_Call (N, Formal, Actual);
3368 else
3369 pragma Assert (False);
3370 raise Program_Error;
3371 end if;
3373 Formal := Extra_Formal (Formal);
3374 end loop;
3376 -- Mark the call as processed build-in-place call; required
3377 -- to avoid adding the extra formals twice.
3379 Set_Is_Expanded_Build_In_Place_Call (Call_Node);
3381 pragma Assert (Check_Number_Of_Actuals (Call_Node, Function_Id));
3382 pragma Assert (Check_BIP_Actuals (Call_Node, Function_Id));
3383 end Add_Dummy_Build_In_Place_Actuals;
3385 ----------------------
3386 -- Add_Extra_Actual --
3387 ----------------------
3389 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
3390 Loc : constant Source_Ptr := Sloc (Expr);
3392 begin
3393 if Extra_Actuals = No_List then
3394 Extra_Actuals := New_List;
3395 Set_Parent (Extra_Actuals, Call_Node);
3396 end if;
3398 Append_To (Extra_Actuals,
3399 Make_Parameter_Association (Loc,
3400 Selector_Name => New_Occurrence_Of (EF, Loc),
3401 Explicit_Actual_Parameter => Expr));
3403 Analyze_And_Resolve (Expr, Etype (EF));
3405 if Nkind (Call_Node) = N_Function_Call then
3406 Set_Is_Accessibility_Actual (Parent (Expr));
3407 end if;
3408 end Add_Extra_Actual;
3410 ------------------------------------
3411 -- Add_View_Conversion_Invariants --
3412 ------------------------------------
3414 procedure Add_View_Conversion_Invariants
3415 (Formal : Entity_Id;
3416 Actual : Node_Id)
3418 Arg : Entity_Id;
3419 Curr_Typ : Entity_Id;
3420 Inv_Checks : List_Id;
3421 Par_Typ : Entity_Id;
3423 begin
3424 Inv_Checks := No_List;
3426 -- Extract the argument from a potentially nested set of view
3427 -- conversions.
3429 Arg := Actual;
3430 while Nkind (Arg) = N_Type_Conversion loop
3431 Arg := Expression (Arg);
3432 end loop;
3434 -- Move up the derivation chain starting with the type of the formal
3435 -- parameter down to the type of the actual object.
3437 Curr_Typ := Empty;
3438 Par_Typ := Etype (Arg);
3439 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
3440 Curr_Typ := Par_Typ;
3442 if Has_Invariants (Curr_Typ)
3443 and then Present (Invariant_Procedure (Curr_Typ))
3444 then
3445 -- Verify the invariant of the current type. Generate:
3447 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
3449 Prepend_New_To (Inv_Checks,
3450 Make_Procedure_Call_Statement (Loc,
3451 Name =>
3452 New_Occurrence_Of
3453 (Invariant_Procedure (Curr_Typ), Loc),
3454 Parameter_Associations => New_List (
3455 Make_Type_Conversion (Loc,
3456 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
3457 Expression => New_Copy_Tree (Arg)))));
3458 end if;
3460 Par_Typ := Base_Type (Etype (Curr_Typ));
3461 end loop;
3463 -- If the node is a function call the generated tests have been
3464 -- already handled in Insert_Post_Call_Actions.
3466 if not Is_Empty_List (Inv_Checks)
3467 and then Nkind (Call_Node) = N_Procedure_Call_Statement
3468 then
3469 Insert_Actions_After (Call_Node, Inv_Checks);
3470 end if;
3471 end Add_View_Conversion_Invariants;
3473 -----------------------------
3474 -- Can_Fold_Predicate_Call --
3475 -----------------------------
3477 function Can_Fold_Predicate_Call (P : Entity_Id) return Boolean is
3478 Actual : Node_Id;
3480 function Augments_Other_Dynamic_Predicate (DP_Aspect_Spec : Node_Id)
3481 return Boolean;
3482 -- Given a Dynamic_Predicate aspect aspecification for a
3483 -- discrete type, returns True iff another DP specification
3484 -- applies (indirectly, via a subtype type or a derived type)
3485 -- to the same entity that this aspect spec applies to.
3487 function May_Fold (N : Node_Id) return Traverse_Result;
3488 -- The predicate expression is foldable if it only contains operators
3489 -- and literals. During this check, we also replace occurrences of
3490 -- the formal of the constructed predicate function with the static
3491 -- value of the actual. This is done on a copy of the analyzed
3492 -- expression for the predicate.
3494 --------------------------------------
3495 -- Augments_Other_Dynamic_Predicate --
3496 --------------------------------------
3498 function Augments_Other_Dynamic_Predicate (DP_Aspect_Spec : Node_Id)
3499 return Boolean
3501 Aspect_Bearer : Entity_Id := Entity (DP_Aspect_Spec);
3502 begin
3503 loop
3504 Aspect_Bearer := Nearest_Ancestor (Aspect_Bearer);
3506 if No (Aspect_Bearer) then
3507 return False;
3508 end if;
3510 declare
3511 Aspect_Spec : constant Node_Id :=
3512 Find_Aspect (Aspect_Bearer, Aspect_Dynamic_Predicate);
3513 begin
3514 if Present (Aspect_Spec)
3515 and then Aspect_Spec /= DP_Aspect_Spec
3516 then
3517 -- Found another Dynamic_Predicate aspect spec
3518 return True;
3519 end if;
3520 end;
3521 end loop;
3522 end Augments_Other_Dynamic_Predicate;
3524 --------------
3525 -- May_Fold --
3526 --------------
3528 function May_Fold (N : Node_Id) return Traverse_Result is
3529 begin
3530 case Nkind (N) is
3531 when N_Op =>
3532 return OK;
3534 when N_Expanded_Name
3535 | N_Identifier
3537 if Ekind (Entity (N)) = E_In_Parameter
3538 and then Entity (N) = First_Entity (P)
3539 then
3540 Rewrite (N, New_Copy (Actual));
3541 Set_Is_Static_Expression (N);
3542 return OK;
3544 elsif Ekind (Entity (N)) = E_Enumeration_Literal then
3545 return OK;
3547 else
3548 return Abandon;
3549 end if;
3551 when N_Case_Expression
3552 | N_If_Expression
3554 return OK;
3556 when N_Integer_Literal =>
3557 return OK;
3559 when others =>
3560 return Abandon;
3561 end case;
3562 end May_Fold;
3564 function Try_Fold is new Traverse_Func (May_Fold);
3566 -- Other Local variables
3568 Subt : constant Entity_Id := Etype (First_Entity (P));
3569 Aspect : Node_Id;
3570 Pred : Node_Id;
3572 -- Start of processing for Can_Fold_Predicate_Call
3574 begin
3575 -- Folding is only interesting if the actual is static and its type
3576 -- has a Dynamic_Predicate aspect. For CodePeer we preserve the
3577 -- function call.
3579 Actual := First (Parameter_Associations (Call_Node));
3580 Aspect := Find_Aspect (Subt, Aspect_Dynamic_Predicate);
3582 -- If actual is a declared constant, retrieve its value
3584 if Is_Entity_Name (Actual)
3585 and then Ekind (Entity (Actual)) = E_Constant
3586 then
3587 Actual := Constant_Value (Entity (Actual));
3588 end if;
3590 if No (Actual)
3591 or else Nkind (Actual) /= N_Integer_Literal
3592 or else not Has_Dynamic_Predicate_Aspect (Subt)
3593 or else No (Aspect)
3595 -- Do not fold if multiple applicable predicate aspects
3596 or else Has_Ghost_Predicate_Aspect (Subt)
3597 or else Has_Aspect (Subt, Aspect_Static_Predicate)
3598 or else Has_Aspect (Subt, Aspect_Predicate)
3599 or else Augments_Other_Dynamic_Predicate (Aspect)
3600 or else CodePeer_Mode
3601 then
3602 return False;
3603 end if;
3605 -- Retrieve the analyzed expression for the predicate
3607 Pred := New_Copy_Tree (Expression (Aspect));
3609 if Try_Fold (Pred) = OK then
3610 Rewrite (Call_Node, Pred);
3611 Analyze_And_Resolve (Call_Node, Standard_Boolean);
3612 return True;
3614 -- Otherwise continue the expansion of the function call
3616 else
3617 return False;
3618 end if;
3619 end Can_Fold_Predicate_Call;
3621 ------------------------------
3622 -- Check_Subprogram_Variant --
3623 ------------------------------
3625 procedure Check_Subprogram_Variant is
3627 function Duplicate_Params_Without_Extra_Actuals
3628 (Call_Node : Node_Id) return List_Id;
3629 -- Duplicate actual parameters of Call_Node into New_Call without
3630 -- extra actuals.
3632 --------------------------------------------
3633 -- Duplicate_Params_Without_Extra_Actuals --
3634 --------------------------------------------
3636 function Duplicate_Params_Without_Extra_Actuals
3637 (Call_Node : Node_Id) return List_Id
3639 Proc_Id : constant Entity_Id := Entity (Name (Call_Node));
3640 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3641 NL : List_Id;
3642 Actual : Node_Or_Entity_Id;
3643 Formal : Entity_Id;
3645 begin
3646 if Actuals = No_List then
3647 return No_List;
3649 else
3650 NL := New_List;
3651 Actual := First (Actuals);
3652 Formal := First_Formal (Proc_Id);
3654 while Present (Formal)
3655 and then Formal /= Extra_Formals (Proc_Id)
3656 loop
3657 Append (New_Copy (Actual), NL);
3658 Next (Actual);
3660 Next_Formal (Formal);
3661 end loop;
3663 return NL;
3664 end if;
3665 end Duplicate_Params_Without_Extra_Actuals;
3667 -- Local variables
3669 Variant_Prag : constant Node_Id :=
3670 Get_Pragma (Current_Scope, Pragma_Subprogram_Variant);
3672 New_Call : Node_Id;
3673 Pragma_Arg1 : Node_Id;
3674 Variant_Proc : Entity_Id;
3676 begin
3677 if Present (Variant_Prag) and then Is_Checked (Variant_Prag) then
3679 Pragma_Arg1 :=
3680 Expression (First (Pragma_Argument_Associations (Variant_Prag)));
3682 -- If pragma parameter is still an aggregate, it comes from a
3683 -- structural variant, which is not expanded and ignored for
3684 -- run-time execution.
3686 if Nkind (Pragma_Arg1) = N_Aggregate then
3687 pragma Assert
3688 (Chars
3689 (First
3690 (Choices
3691 (First (Component_Associations (Pragma_Arg1))))) =
3692 Name_Structural);
3693 return;
3694 end if;
3696 -- Otherwise, analysis of the pragma rewrites its argument with a
3697 -- reference to the internally generated procedure.
3699 Variant_Proc := Entity (Pragma_Arg1);
3701 New_Call :=
3702 Make_Procedure_Call_Statement (Loc,
3703 Name =>
3704 New_Occurrence_Of (Variant_Proc, Loc),
3705 Parameter_Associations =>
3706 Duplicate_Params_Without_Extra_Actuals (Call_Node));
3708 Insert_Action (Call_Node, New_Call);
3710 pragma Assert (Etype (New_Call) /= Any_Type
3711 or else Serious_Errors_Detected > 0);
3712 end if;
3713 end Check_Subprogram_Variant;
3715 ---------------------------
3716 -- Inherited_From_Formal --
3717 ---------------------------
3719 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
3720 Par : Entity_Id;
3721 Gen_Par : Entity_Id;
3722 Gen_Prim : Elist_Id;
3723 Elmt : Elmt_Id;
3724 Indic : Node_Id;
3726 begin
3727 -- If the operation is inherited, it is attached to the corresponding
3728 -- type derivation. If the parent in the derivation is a generic
3729 -- actual, it is a subtype of the actual, and we have to recover the
3730 -- original derived type declaration to find the proper parent.
3732 if Nkind (Parent (S)) /= N_Full_Type_Declaration
3733 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
3734 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
3735 N_Derived_Type_Definition
3736 or else not In_Instance
3737 then
3738 return Empty;
3740 else
3741 Indic :=
3742 Subtype_Indication
3743 (Type_Definition (Original_Node (Parent (S))));
3745 if Nkind (Indic) = N_Subtype_Indication then
3746 Par := Entity (Subtype_Mark (Indic));
3747 else
3748 Par := Entity (Indic);
3749 end if;
3750 end if;
3752 if not Is_Generic_Actual_Type (Par)
3753 or else Is_Tagged_Type (Par)
3754 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
3755 or else not In_Open_Scopes (Scope (Par))
3756 then
3757 return Empty;
3758 else
3759 Gen_Par := Generic_Parent_Type (Parent (Par));
3760 end if;
3762 -- If the actual has no generic parent type, the formal is not
3763 -- a formal derived type, so nothing to inherit.
3765 if No (Gen_Par) then
3766 return Empty;
3767 end if;
3769 -- If the generic parent type is still the generic type, this is a
3770 -- private formal, not a derived formal, and there are no operations
3771 -- inherited from the formal.
3773 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
3774 return Empty;
3775 end if;
3777 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
3779 Elmt := First_Elmt (Gen_Prim);
3780 while Present (Elmt) loop
3781 if Chars (Node (Elmt)) = Chars (S) then
3782 declare
3783 F1 : Entity_Id;
3784 F2 : Entity_Id;
3786 begin
3787 F1 := First_Formal (S);
3788 F2 := First_Formal (Node (Elmt));
3789 while Present (F1)
3790 and then Present (F2)
3791 loop
3792 if Etype (F1) = Etype (F2)
3793 or else Etype (F2) = Gen_Par
3794 then
3795 Next_Formal (F1);
3796 Next_Formal (F2);
3797 else
3798 Next_Elmt (Elmt);
3799 exit; -- not the right subprogram
3800 end if;
3802 return Node (Elmt);
3803 end loop;
3804 end;
3806 else
3807 Next_Elmt (Elmt);
3808 end if;
3809 end loop;
3811 raise Program_Error;
3812 end Inherited_From_Formal;
3814 --------------------------
3815 -- In_Unfrozen_Instance --
3816 --------------------------
3818 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
3819 S : Entity_Id;
3821 begin
3822 S := E;
3823 while Present (S) and then S /= Standard_Standard loop
3824 if Is_Generic_Instance (S)
3825 and then Present (Freeze_Node (S))
3826 and then not Analyzed (Freeze_Node (S))
3827 then
3828 return True;
3829 end if;
3831 S := Scope (S);
3832 end loop;
3834 return False;
3835 end In_Unfrozen_Instance;
3837 ----------------------------------
3838 -- Is_Class_Wide_Interface_Type --
3839 ----------------------------------
3841 function Is_Class_Wide_Interface_Type (E : Entity_Id) return Boolean is
3842 DDT : Entity_Id;
3843 Typ : Entity_Id := E;
3845 begin
3846 if Has_Non_Limited_View (Typ) then
3847 Typ := Non_Limited_View (Typ);
3848 end if;
3850 if Ekind (Typ) = E_Anonymous_Access_Type then
3851 DDT := Directly_Designated_Type (Typ);
3853 if Has_Non_Limited_View (DDT) then
3854 DDT := Non_Limited_View (DDT);
3855 end if;
3857 return Is_Class_Wide_Type (DDT) and then Is_Interface (DDT);
3858 else
3859 return Is_Class_Wide_Type (Typ) and then Is_Interface (Typ);
3860 end if;
3861 end Is_Class_Wide_Interface_Type;
3863 -------------------------
3864 -- Is_Direct_Deep_Call --
3865 -------------------------
3867 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
3868 begin
3869 if Is_TSS (Subp, TSS_Deep_Adjust)
3870 or else Is_TSS (Subp, TSS_Deep_Finalize)
3871 or else Is_TSS (Subp, TSS_Deep_Initialize)
3872 then
3873 declare
3874 Actual : Node_Id;
3875 Formal : Entity_Id;
3877 begin
3878 Actual := First_Actual (Call_Node);
3879 Formal := First_Formal (Subp);
3880 while Present (Actual)
3881 and then Present (Formal)
3882 loop
3883 if Nkind (Actual) = N_Identifier
3884 and then Is_Controlling_Actual (Actual)
3885 and then Etype (Actual) = Etype (Formal)
3886 then
3887 return True;
3888 end if;
3890 Next_Actual (Actual);
3891 Next_Formal (Formal);
3892 end loop;
3893 end;
3894 end if;
3896 return False;
3897 end Is_Direct_Deep_Call;
3899 ---------------
3900 -- New_Value --
3901 ---------------
3903 function New_Value (From : Node_Id) return Node_Id is
3904 Res : constant Node_Id := Duplicate_Subexpr (From);
3905 begin
3906 if Is_Access_Type (Etype (From)) then
3907 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
3908 else
3909 return Res;
3910 end if;
3911 end New_Value;
3913 -- Local variables
3915 Remote : constant Boolean := Is_Remote_Call (Call_Node);
3916 Actual : Node_Id;
3917 Formal : Entity_Id;
3918 Orig_Subp : Entity_Id := Empty;
3919 Param_Count : Positive;
3920 Parent_Formal : Entity_Id;
3921 Parent_Subp : Entity_Id;
3922 Scop : Entity_Id;
3923 Subp : Entity_Id;
3925 CW_Interface_Formals_Present : Boolean := False;
3927 -- Start of processing for Expand_Call_Helper
3929 begin
3930 Post_Call := New_List;
3932 -- Expand the function or procedure call if the first actual has a
3933 -- declared dimension aspect, and the subprogram is declared in one
3934 -- of the dimension I/O packages.
3936 if Ada_Version >= Ada_2012
3937 and then Nkind (Call_Node) in N_Subprogram_Call
3938 and then Present (Parameter_Associations (Call_Node))
3939 then
3940 Expand_Put_Call_With_Symbol (Call_Node);
3941 end if;
3943 -- Ignore if previous error
3945 if Nkind (Call_Node) in N_Has_Etype
3946 and then Etype (Call_Node) = Any_Type
3947 then
3948 return;
3949 end if;
3951 -- Call using access to subprogram with explicit dereference
3953 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3954 Subp := Etype (Name (Call_Node));
3955 Parent_Subp := Empty;
3957 -- Case of call to simple entry, where the Name is a selected component
3958 -- whose prefix is the task, and whose selector name is the entry name
3960 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
3961 Subp := Entity (Selector_Name (Name (Call_Node)));
3962 Parent_Subp := Empty;
3964 -- Case of call to member of entry family, where Name is an indexed
3965 -- component, with the prefix being a selected component giving the
3966 -- task and entry family name, and the index being the entry index.
3968 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
3969 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
3970 Parent_Subp := Empty;
3972 -- Normal case
3974 else
3975 Subp := Entity (Name (Call_Node));
3976 Parent_Subp := Alias (Subp);
3978 -- Replace call to Raise_Exception by call to Raise_Exception_Always
3979 -- if we can tell that the first parameter cannot possibly be null.
3980 -- This improves efficiency by avoiding a run-time test.
3982 -- We do not do this if Raise_Exception_Always does not exist, which
3983 -- can happen in configurable run time profiles which provide only a
3984 -- Raise_Exception.
3986 if Is_RTE (Subp, RE_Raise_Exception)
3987 and then RTE_Available (RE_Raise_Exception_Always)
3988 then
3989 declare
3990 FA : constant Node_Id :=
3991 Original_Node (First_Actual (Call_Node));
3993 begin
3994 -- The case we catch is where the first argument is obtained
3995 -- using the Identity attribute (which must always be
3996 -- non-null).
3998 if Nkind (FA) = N_Attribute_Reference
3999 and then Attribute_Name (FA) = Name_Identity
4000 then
4001 Subp := RTE (RE_Raise_Exception_Always);
4002 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
4003 end if;
4004 end;
4005 end if;
4007 if Ekind (Subp) = E_Entry then
4008 Parent_Subp := Empty;
4009 end if;
4010 end if;
4012 -- Ensure that the called subprogram has all its formals
4014 if not Is_Frozen (Subp) then
4015 Create_Extra_Formals (Subp);
4016 end if;
4018 -- Ada 2005 (AI-345): We have a procedure call as a triggering
4019 -- alternative in an asynchronous select or as an entry call in
4020 -- a conditional or timed select. Check whether the procedure call
4021 -- is a renaming of an entry and rewrite it as an entry call.
4023 if Ada_Version >= Ada_2005
4024 and then Nkind (Call_Node) = N_Procedure_Call_Statement
4025 and then
4026 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
4027 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
4028 or else
4029 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
4030 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
4031 then
4032 declare
4033 Ren_Decl : Node_Id;
4034 Ren_Root : Entity_Id := Subp;
4036 begin
4037 -- This may be a chain of renamings, find the root
4039 if Present (Alias (Ren_Root)) then
4040 Ren_Root := Alias (Ren_Root);
4041 end if;
4043 if Present (Parent (Ren_Root))
4044 and then Present (Original_Node (Parent (Parent (Ren_Root))))
4045 then
4046 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
4048 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
4049 Rewrite (Call_Node,
4050 Make_Entry_Call_Statement (Loc,
4051 Name =>
4052 New_Copy_Tree (Name (Ren_Decl)),
4053 Parameter_Associations =>
4054 New_Copy_List_Tree
4055 (Parameter_Associations (Call_Node))));
4057 return;
4058 end if;
4059 end if;
4060 end;
4061 end if;
4063 -- If this is a call to a predicate function, try to constant fold it
4065 if Nkind (Call_Node) = N_Function_Call
4066 and then Is_Entity_Name (Name (Call_Node))
4067 and then Is_Predicate_Function (Subp)
4068 and then Can_Fold_Predicate_Call (Subp)
4069 then
4070 return;
4071 end if;
4073 -- First step, compute extra actuals, corresponding to any Extra_Formals
4074 -- present. Note that we do not access Extra_Formals directly, instead
4075 -- we simply note the presence of the extra formals as we process the
4076 -- regular formals collecting corresponding actuals in Extra_Actuals.
4078 -- We also generate any required range checks for actuals for in formals
4079 -- as we go through the loop, since this is a convenient place to do it.
4080 -- (Though it seems that this would be better done in Expand_Actuals???)
4082 -- Special case: Thunks must not compute the extra actuals; they must
4083 -- just propagate to the target primitive their extra actuals.
4085 if Is_Thunk (Current_Scope)
4086 and then Thunk_Entity (Current_Scope) = Subp
4087 and then Present (Extra_Formals (Subp))
4088 then
4089 pragma Assert (Extra_Formals_Match_OK (Current_Scope, Subp));
4091 declare
4092 Target_Formal : Entity_Id;
4093 Thunk_Formal : Entity_Id;
4095 begin
4096 Target_Formal := Extra_Formals (Subp);
4097 Thunk_Formal := Extra_Formals (Current_Scope);
4098 while Present (Target_Formal) loop
4099 Add_Extra_Actual
4100 (Expr => New_Occurrence_Of (Thunk_Formal, Loc),
4101 EF => Thunk_Formal);
4103 Target_Formal := Extra_Formal (Target_Formal);
4104 Thunk_Formal := Extra_Formal (Thunk_Formal);
4105 end loop;
4107 while Is_Non_Empty_List (Extra_Actuals) loop
4108 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
4109 end loop;
4111 -- Mark the call as processed build-in-place call; required
4112 -- to avoid adding the extra formals twice.
4114 if Nkind (Call_Node) = N_Function_Call then
4115 Set_Is_Expanded_Build_In_Place_Call (Call_Node);
4116 end if;
4118 Expand_Actuals (Call_Node, Subp, Post_Call);
4119 pragma Assert (Is_Empty_List (Post_Call));
4120 pragma Assert (Check_Number_Of_Actuals (Call_Node, Subp));
4121 pragma Assert (Check_BIP_Actuals (Call_Node, Subp));
4122 return;
4123 end;
4124 end if;
4126 Formal := First_Formal (Subp);
4127 Actual := First_Actual (Call_Node);
4128 Param_Count := 1;
4129 while Present (Formal) loop
4130 -- Prepare to examine current entry
4132 Prev := Actual;
4134 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
4135 -- to expand it in a further round.
4137 CW_Interface_Formals_Present :=
4138 CW_Interface_Formals_Present
4139 or else Is_Class_Wide_Interface_Type (Etype (Formal));
4141 -- Create possible extra actual for constrained case. Usually, the
4142 -- extra actual is of the form actual'constrained, but since this
4143 -- attribute is only available for unconstrained records, TRUE is
4144 -- expanded if the type of the formal happens to be constrained (for
4145 -- instance when this procedure is inherited from an unconstrained
4146 -- record to a constrained one) or if the actual has no discriminant
4147 -- (its type is constrained). An exception to this is the case of a
4148 -- private type without discriminants. In this case we pass FALSE
4149 -- because the object has underlying discriminants with defaults.
4151 if Present (Extra_Constrained (Formal)) then
4152 if Is_Mutably_Tagged_Type (Etype (Actual))
4153 or else (Is_Private_Type (Etype (Prev))
4154 and then not Has_Discriminants
4155 (Base_Type (Etype (Prev))))
4156 then
4157 Add_Extra_Actual
4158 (Expr => New_Occurrence_Of (Standard_False, Loc),
4159 EF => Extra_Constrained (Formal));
4161 elsif Is_Constrained (Etype (Formal))
4162 or else not Has_Discriminants (Etype (Prev))
4163 then
4164 Add_Extra_Actual
4165 (Expr => New_Occurrence_Of (Standard_True, Loc),
4166 EF => Extra_Constrained (Formal));
4168 -- Do not produce extra actuals for Unchecked_Union parameters.
4169 -- Jump directly to the end of the loop.
4171 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
4172 goto Skip_Extra_Actual_Generation;
4174 else
4175 -- If the actual is a type conversion, then the constrained
4176 -- test applies to the actual, not the target type.
4178 declare
4179 Act_Prev : Node_Id;
4181 begin
4182 -- Test for unchecked conversions as well, which can occur
4183 -- as out parameter actuals on calls to stream procedures.
4185 Act_Prev := Prev;
4186 while Nkind (Act_Prev) in N_Type_Conversion
4187 | N_Unchecked_Type_Conversion
4188 loop
4189 Act_Prev := Expression (Act_Prev);
4190 end loop;
4192 -- If the expression is a conversion of a dereference, this
4193 -- is internally generated code that manipulates addresses,
4194 -- e.g. when building interface tables. No check should
4195 -- occur in this case, and the discriminated object is not
4196 -- directly at hand.
4198 if not Comes_From_Source (Actual)
4199 and then Nkind (Actual) = N_Unchecked_Type_Conversion
4200 and then Nkind (Act_Prev) = N_Explicit_Dereference
4201 then
4202 Add_Extra_Actual
4203 (Expr => New_Occurrence_Of (Standard_False, Loc),
4204 EF => Extra_Constrained (Formal));
4206 else
4207 Add_Extra_Actual
4208 (Expr =>
4209 Make_Attribute_Reference (Sloc (Prev),
4210 Prefix =>
4211 Duplicate_Subexpr_No_Checks
4212 (Act_Prev, Name_Req => True),
4213 Attribute_Name => Name_Constrained),
4214 EF => Extra_Constrained (Formal));
4215 end if;
4216 end;
4217 end if;
4218 end if;
4220 -- Create possible extra actual for accessibility level
4222 if Present (Extra_Accessibility (Formal)) then
4223 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
4224 -- accessibility levels.
4226 if Is_Thunk (Current_Scope) then
4227 declare
4228 Parm_Ent : Entity_Id;
4230 begin
4231 if Is_Controlling_Actual (Actual) then
4233 -- Find the corresponding actual of the thunk
4235 Parm_Ent := First_Entity (Current_Scope);
4236 for J in 2 .. Param_Count loop
4237 Next_Entity (Parm_Ent);
4238 end loop;
4240 -- Handle unchecked conversion of access types generated
4241 -- in thunks (cf. Expand_Interface_Thunk).
4243 elsif Is_Access_Type (Etype (Actual))
4244 and then Nkind (Actual) = N_Unchecked_Type_Conversion
4245 then
4246 Parm_Ent := Entity (Expression (Actual));
4248 else pragma Assert (Is_Entity_Name (Actual));
4249 Parm_Ent := Entity (Actual);
4250 end if;
4252 Add_Extra_Actual
4253 (Expr => Accessibility_Level
4254 (Expr => Parm_Ent,
4255 Level => Dynamic_Level,
4256 Allow_Alt_Model => False),
4257 EF => Extra_Accessibility (Formal));
4258 end;
4260 -- Conditional expressions
4262 elsif Nkind (Prev) = N_Expression_With_Actions
4263 and then Nkind (Original_Node (Prev)) in
4264 N_If_Expression | N_Case_Expression
4265 then
4266 Add_Cond_Expression_Extra_Actual (Formal);
4268 -- Internal constant generated to remove side effects (normally
4269 -- from the expansion of dispatching calls).
4271 -- First verify the actual is internal
4273 elsif not Comes_From_Source (Prev)
4274 and then not Is_Rewrite_Substitution (Prev)
4276 -- Next check that the actual is a constant
4278 and then Nkind (Prev) = N_Identifier
4279 and then Ekind (Entity (Prev)) = E_Constant
4280 and then Nkind (Parent (Entity (Prev))) = N_Object_Declaration
4281 then
4282 -- Generate the accessibility level based on the expression in
4283 -- the constant's declaration.
4285 declare
4286 Ent : Entity_Id := Entity (Prev);
4288 begin
4289 -- Handle deferred constants
4291 if Present (Full_View (Ent)) then
4292 Ent := Full_View (Ent);
4293 end if;
4295 Add_Extra_Actual
4296 (Expr => Accessibility_Level
4297 (Expr => Expression (Parent (Ent)),
4298 Level => Dynamic_Level,
4299 Allow_Alt_Model => False),
4300 EF => Extra_Accessibility (Formal));
4301 end;
4303 -- Normal case
4305 else
4306 Add_Extra_Actual
4307 (Expr => Accessibility_Level
4308 (Expr => Prev,
4309 Level => Dynamic_Level,
4310 Allow_Alt_Model => False),
4311 EF => Extra_Accessibility (Formal));
4312 end if;
4313 end if;
4315 -- Perform the check of 4.6(49) that prevents a null value from being
4316 -- passed as an actual to an access parameter. Note that the check
4317 -- is elided in the common cases of passing an access attribute or
4318 -- access parameter as an actual. Also, we currently don't enforce
4319 -- this check for expander-generated actuals and when -gnatdj is set.
4321 if Ada_Version >= Ada_2005 then
4323 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
4324 -- the intent of 6.4.1(13) is that null-exclusion checks should
4325 -- not be done for 'out' parameters, even though it refers only
4326 -- to constraint checks, and a null_exclusion is not a constraint.
4327 -- Note that AI05-0196-1 corrects this mistake in the RM.
4329 if Is_Access_Type (Etype (Formal))
4330 and then Can_Never_Be_Null (Etype (Formal))
4331 and then Ekind (Formal) /= E_Out_Parameter
4332 and then Nkind (Prev) /= N_Raise_Constraint_Error
4333 and then (Known_Null (Prev)
4334 or else not Can_Never_Be_Null (Etype (Prev)))
4335 then
4336 Install_Null_Excluding_Check (Prev);
4337 end if;
4339 -- Ada_Version < Ada_2005
4341 else
4342 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
4343 or else Access_Checks_Suppressed (Subp)
4344 then
4345 null;
4347 elsif Debug_Flag_J then
4348 null;
4350 elsif not Comes_From_Source (Prev) then
4351 null;
4353 elsif Is_Entity_Name (Prev)
4354 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
4355 then
4356 null;
4358 elsif Nkind (Prev) in N_Allocator | N_Attribute_Reference then
4359 null;
4361 else
4362 Install_Null_Excluding_Check (Prev);
4363 end if;
4364 end if;
4366 -- Perform appropriate validity checks on parameters that
4367 -- are entities.
4369 if Validity_Checks_On then
4370 if (Ekind (Formal) = E_In_Parameter
4371 and then Validity_Check_In_Params)
4372 or else
4373 (Ekind (Formal) = E_In_Out_Parameter
4374 and then Validity_Check_In_Out_Params)
4375 then
4376 -- If the actual is an indexed component of a packed type (or
4377 -- is an indexed or selected component whose prefix recursively
4378 -- meets this condition), it has not been expanded yet. It will
4379 -- be copied in the validity code that follows, and has to be
4380 -- expanded appropriately, so reanalyze it.
4382 -- What we do is just to unset analyzed bits on prefixes till
4383 -- we reach something that does not have a prefix.
4385 declare
4386 Nod : Node_Id;
4388 begin
4389 Nod := Actual;
4390 while Nkind (Nod) in
4391 N_Indexed_Component | N_Selected_Component
4392 loop
4393 Set_Analyzed (Nod, False);
4394 Nod := Prefix (Nod);
4395 end loop;
4396 end;
4398 Ensure_Valid (Actual);
4399 end if;
4400 end if;
4402 -- For IN OUT and OUT parameters, ensure that subscripts are valid
4403 -- since this is a left side reference. We only do this for calls
4404 -- from the source program since we assume that compiler generated
4405 -- calls explicitly generate any required checks. We also need it
4406 -- only if we are doing standard validity checks, since clearly it is
4407 -- not needed if validity checks are off, and in subscript validity
4408 -- checking mode, all indexed components are checked with a call
4409 -- directly from Expand_N_Indexed_Component.
4411 if Comes_From_Source (Call_Node)
4412 and then Ekind (Formal) /= E_In_Parameter
4413 and then Validity_Checks_On
4414 and then Validity_Check_Default
4415 and then not Validity_Check_Subscripts
4416 then
4417 Check_Valid_Lvalue_Subscripts (Actual);
4418 end if;
4420 -- Mark any scalar OUT parameter that is a simple variable as no
4421 -- longer known to be valid (unless the type is always valid). This
4422 -- reflects the fact that if an OUT parameter is never set in a
4423 -- procedure, then it can become invalid on the procedure return.
4425 if Ekind (Formal) = E_Out_Parameter
4426 and then Is_Entity_Name (Actual)
4427 and then Ekind (Entity (Actual)) = E_Variable
4428 and then not Is_Known_Valid (Etype (Actual))
4429 then
4430 Set_Is_Known_Valid (Entity (Actual), False);
4431 end if;
4433 -- For an OUT or IN OUT parameter, if the actual is an entity, then
4434 -- clear current values, since they can be clobbered. We are probably
4435 -- doing this in more places than we need to, but better safe than
4436 -- sorry when it comes to retaining bad current values.
4438 if Ekind (Formal) /= E_In_Parameter
4439 and then Is_Entity_Name (Actual)
4440 and then Present (Entity (Actual))
4441 then
4442 declare
4443 Ent : constant Entity_Id := Entity (Actual);
4444 Sav : Node_Id;
4446 begin
4447 -- For an OUT or IN OUT parameter that is an assignable entity,
4448 -- we do not want to clobber the Last_Assignment field, since
4449 -- if it is set, it was precisely because it is indeed an OUT
4450 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
4451 -- since the subprogram could have returned in invalid value.
4453 if Is_Assignable (Ent) then
4454 Sav := Last_Assignment (Ent);
4455 Kill_Current_Values (Ent);
4456 Set_Last_Assignment (Ent, Sav);
4457 Set_Is_Known_Valid (Ent, False);
4458 Set_Is_True_Constant (Ent, False);
4460 -- For all other cases, just kill the current values
4462 else
4463 Kill_Current_Values (Ent);
4464 end if;
4465 end;
4466 end if;
4468 -- If the formal is class-wide and the actual is an aggregate, force
4469 -- evaluation so that the back end who does not know about class-wide
4470 -- type, does not generate a temporary of the wrong size.
4472 if not Is_Class_Wide_Type (Etype (Formal)) then
4473 null;
4475 elsif Nkind (Actual) = N_Aggregate
4476 or else (Nkind (Actual) = N_Qualified_Expression
4477 and then Nkind (Expression (Actual)) = N_Aggregate)
4478 then
4479 Force_Evaluation (Actual);
4480 end if;
4482 -- In a remote call, if the formal is of a class-wide type, check
4483 -- that the actual meets the requirements described in E.4(18).
4485 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
4486 Insert_Action (Actual,
4487 Make_Transportable_Check (Loc,
4488 Duplicate_Subexpr_Move_Checks (Actual)));
4489 end if;
4491 -- Perform invariant checks for all intermediate types in a view
4492 -- conversion after successful return from a call that passes the
4493 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
4494 -- 13/3, 14/3)). Consider only source conversion in order to avoid
4495 -- generating spurious checks on complex expansion such as object
4496 -- initialization through an extension aggregate.
4498 if Comes_From_Source (Call_Node)
4499 and then Ekind (Formal) /= E_In_Parameter
4500 and then Nkind (Actual) = N_Type_Conversion
4501 then
4502 Add_View_Conversion_Invariants (Formal, Actual);
4503 end if;
4505 -- This label is required when skipping extra actual generation for
4506 -- Unchecked_Union parameters.
4508 <<Skip_Extra_Actual_Generation>>
4510 Param_Count := Param_Count + 1;
4511 Next_Actual (Actual);
4512 Next_Formal (Formal);
4513 end loop;
4515 -- If we are calling an Ada 2012 function which needs to have the
4516 -- "accessibility level determined by the point of call" (AI05-0234)
4517 -- passed in to it, then pass it in.
4519 if Ekind (Subp) in E_Function | E_Operator | E_Subprogram_Type
4520 and then
4521 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
4522 then
4523 declare
4524 Extra_Form : Node_Id := Empty;
4525 Level : Node_Id := Empty;
4527 begin
4528 -- Detect cases where the function call has been internally
4529 -- generated by examining the original node and return library
4530 -- level - taking care to avoid ignoring function calls expanded
4531 -- in prefix notation.
4533 if Nkind (Original_Node (Call_Node)) not in N_Function_Call
4534 | N_Selected_Component
4535 | N_Indexed_Component
4536 then
4537 Level := Make_Integer_Literal
4538 (Loc, Scope_Depth (Standard_Standard));
4540 -- Otherwise get the level normally based on the call node
4542 else
4543 Level := Accessibility_Level
4544 (Expr => Call_Node,
4545 Level => Dynamic_Level,
4546 Allow_Alt_Model => False);
4547 end if;
4549 -- It may be possible that we are re-expanding an already
4550 -- expanded call when are are dealing with dispatching ???
4552 if No (Parameter_Associations (Call_Node))
4553 or else Nkind (Last (Parameter_Associations (Call_Node)))
4554 /= N_Parameter_Association
4555 or else not Is_Accessibility_Actual
4556 (Last (Parameter_Associations (Call_Node)))
4557 then
4558 Extra_Form := Extra_Accessibility_Of_Result
4559 (Ultimate_Alias (Subp));
4561 Add_Extra_Actual
4562 (Expr => Level,
4563 EF => Extra_Form);
4564 end if;
4565 end;
4566 end if;
4568 -- If we are expanding the RHS of an assignment we need to check if tag
4569 -- propagation is needed. You might expect this processing to be in
4570 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
4571 -- assignment might be transformed to a declaration for an unconstrained
4572 -- value if the expression is classwide.
4574 if Nkind (Call_Node) = N_Function_Call
4575 and then Is_Tag_Indeterminate (Call_Node)
4576 and then Is_Entity_Name (Name (Call_Node))
4577 then
4578 declare
4579 Ass : Node_Id := Empty;
4581 begin
4582 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
4583 Ass := Parent (Call_Node);
4585 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
4586 and then Nkind (Parent (Parent (Call_Node))) =
4587 N_Assignment_Statement
4588 then
4589 Ass := Parent (Parent (Call_Node));
4591 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
4592 and then Nkind (Parent (Parent (Call_Node))) =
4593 N_Assignment_Statement
4594 then
4595 Ass := Parent (Parent (Call_Node));
4596 end if;
4598 if Present (Ass)
4599 and then Is_Class_Wide_Type (Etype (Name (Ass)))
4600 then
4601 -- Move the error messages below to sem???
4603 if Is_Access_Type (Etype (Call_Node)) then
4604 if Designated_Type (Etype (Call_Node)) /=
4605 Root_Type (Etype (Name (Ass)))
4606 then
4607 Error_Msg_NE
4608 ("tag-indeterminate expression must have designated "
4609 & "type& (RM 5.2 (6))",
4610 Call_Node, Root_Type (Etype (Name (Ass))));
4611 else
4612 Propagate_Tag (Name (Ass), Call_Node);
4613 end if;
4615 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
4616 Error_Msg_NE
4617 ("tag-indeterminate expression must have type & "
4618 & "(RM 5.2 (6))",
4619 Call_Node, Root_Type (Etype (Name (Ass))));
4621 else
4622 Propagate_Tag (Name (Ass), Call_Node);
4623 end if;
4625 -- The call will be rewritten as a dispatching call, and
4626 -- expanded as such.
4628 return;
4629 end if;
4630 end;
4631 end if;
4633 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
4634 -- it to point to the correct secondary virtual table.
4636 if Nkind (Call_Node) in N_Subprogram_Call
4637 and then CW_Interface_Formals_Present
4638 then
4639 Expand_Interface_Actuals (Call_Node);
4640 end if;
4642 -- Install class-wide preconditions runtime check when this is a
4643 -- dispatching primitive that has or inherits class-wide preconditions;
4644 -- otherwise no runtime check is installed.
4646 if Nkind (Call_Node) in N_Subprogram_Call
4647 and then Is_Dispatching_Operation (Subp)
4648 then
4649 Install_Class_Preconditions_Check (Call_Node);
4650 end if;
4652 -- Deals with Dispatch_Call if we still have a call, before expanding
4653 -- extra actuals since this will be done on the re-analysis of the
4654 -- dispatching call. Note that we do not try to shorten the actual list
4655 -- for a dispatching call, it would not make sense to do so. Expansion
4656 -- of dispatching calls is suppressed for VM targets, because the VM
4657 -- back-ends directly handle the generation of dispatching calls and
4658 -- would have to undo any expansion to an indirect call.
4660 if Nkind (Call_Node) in N_Subprogram_Call
4661 and then Present (Controlling_Argument (Call_Node))
4662 then
4663 if Tagged_Type_Expansion then
4664 Expand_Dispatching_Call (Call_Node);
4666 -- Expand_Dispatching_Call takes care of all the needed processing
4668 return;
4669 end if;
4671 -- VM targets
4673 declare
4674 Call_Typ : constant Entity_Id := Etype (Call_Node);
4675 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
4676 Eq_Prim_Op : Entity_Id := Empty;
4677 New_Call : Node_Id;
4678 Param : Node_Id;
4679 Prev_Call : Node_Id;
4681 begin
4682 Apply_Tag_Checks (Call_Node);
4684 if not Is_Limited_Type (Typ) then
4685 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
4686 end if;
4688 -- If this is a dispatching "=", we must first compare the
4689 -- tags so we generate: x.tag = y.tag and then x = y
4691 if Subp = Eq_Prim_Op then
4693 -- Mark the node as analyzed to avoid reanalyzing this
4694 -- dispatching call (which would cause a never-ending loop)
4696 Prev_Call := Relocate_Node (Call_Node);
4697 Set_Analyzed (Prev_Call);
4699 Param := First_Actual (Call_Node);
4700 New_Call :=
4701 Make_And_Then (Loc,
4702 Left_Opnd =>
4703 Make_Op_Eq (Loc,
4704 Left_Opnd =>
4705 Make_Selected_Component (Loc,
4706 Prefix => New_Value (Param),
4707 Selector_Name =>
4708 New_Occurrence_Of
4709 (First_Tag_Component (Typ), Loc)),
4711 Right_Opnd =>
4712 Make_Selected_Component (Loc,
4713 Prefix =>
4714 Unchecked_Convert_To (Typ,
4715 New_Value (Next_Actual (Param))),
4716 Selector_Name =>
4717 New_Occurrence_Of
4718 (First_Tag_Component (Typ), Loc))),
4719 Right_Opnd => Prev_Call);
4721 Rewrite (Call_Node, New_Call);
4722 Analyze_And_Resolve
4723 (Call_Node, Call_Typ, Suppress => All_Checks);
4724 end if;
4726 -- Expansion of a dispatching call results in an indirect call,
4727 -- which in turn causes current values to be killed (see
4728 -- Resolve_Call), so on VM targets we do the call here to
4729 -- ensure consistent warnings between VM and non-VM targets.
4731 Kill_Current_Values;
4733 -- If this is a dispatching "=" then we must update the reference
4734 -- to the call node because we generated:
4735 -- x.tag = y.tag and then x = y
4737 if Subp = Eq_Prim_Op then
4738 Call_Node := Right_Opnd (Call_Node);
4739 end if;
4740 end;
4741 end if;
4743 -- Similarly, expand calls to RCI subprograms on which pragma
4744 -- All_Calls_Remote applies. The rewriting will be reanalyzed
4745 -- later. Do this only when the call comes from source since we
4746 -- do not want such a rewriting to occur in expanded code.
4748 if Is_All_Remote_Call (Call_Node) then
4749 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
4751 -- Similarly, do not add extra actuals for an entry call whose entity
4752 -- is a protected procedure, or for an internal protected subprogram
4753 -- call, because it will be rewritten as a protected subprogram call
4754 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
4756 elsif Is_Protected_Type (Scope (Subp))
4757 and then Ekind (Subp) in E_Procedure | E_Function
4758 then
4759 null;
4761 -- During that loop we gathered the extra actuals (the ones that
4762 -- correspond to Extra_Formals), so now they can be appended.
4764 elsif Is_Non_Empty_List (Extra_Actuals) then
4765 declare
4766 Num_Extra_Actuals : constant Nat := List_Length (Extra_Actuals);
4768 begin
4769 while Is_Non_Empty_List (Extra_Actuals) loop
4770 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
4771 end loop;
4773 -- Add dummy extra BIP actuals if we are calling a function that
4774 -- inherited the BIP extra actuals but does not require them.
4776 if Nkind (Call_Node) = N_Function_Call
4777 and then Is_Function_Call_With_BIP_Formals (Call_Node)
4778 and then not Is_Build_In_Place_Function_Call (Call_Node)
4779 then
4780 Add_Dummy_Build_In_Place_Actuals (Subp,
4781 Num_Added_Extra_Actuals => Num_Extra_Actuals);
4782 end if;
4783 end;
4785 -- Add dummy extra BIP actuals if we are calling a function that
4786 -- inherited the BIP extra actuals but does not require them.
4788 elsif Nkind (Call_Node) = N_Function_Call
4789 and then Is_Function_Call_With_BIP_Formals (Call_Node)
4790 and then not Is_Build_In_Place_Function_Call (Call_Node)
4791 then
4792 Add_Dummy_Build_In_Place_Actuals (Subp);
4793 end if;
4795 -- At this point we have all the actuals, so this is the point at which
4796 -- the various expansion activities for actuals is carried out.
4798 Expand_Actuals (Call_Node, Subp, Post_Call);
4800 -- If it is a recursive call then call the internal procedure that
4801 -- verifies Subprogram_Variant contract (if present and enabled).
4802 -- Detecting calls to subprogram aliases is necessary for recursive
4803 -- calls in instances of generic subprograms, where the renaming of
4804 -- the current subprogram is called.
4806 if Is_Subprogram (Subp)
4807 and then not Is_Ignored_Ghost_Entity (Subp)
4808 and then Same_Or_Aliased_Subprograms (Subp, Current_Scope)
4809 then
4810 Check_Subprogram_Variant;
4811 end if;
4813 -- Verify that the actuals do not share storage. This check must be done
4814 -- on the caller side rather that inside the subprogram to avoid issues
4815 -- of parameter passing.
4817 if Check_Aliasing_Of_Parameters then
4818 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
4819 end if;
4821 -- If the subprogram is a renaming, or if it is inherited, replace it in
4822 -- the call with the name of the actual subprogram being called. If this
4823 -- is a dispatching call, the run-time decides what to call. The Alias
4824 -- attribute does not apply to entries.
4826 if Nkind (Call_Node) /= N_Entry_Call_Statement
4827 and then No (Controlling_Argument (Call_Node))
4828 and then Present (Parent_Subp)
4829 and then not Is_Direct_Deep_Call (Subp)
4830 then
4831 if Present (Inherited_From_Formal (Subp)) then
4832 Parent_Subp := Inherited_From_Formal (Subp);
4833 else
4834 Parent_Subp := Ultimate_Alias (Parent_Subp);
4835 end if;
4837 -- The below setting of Entity is suspect, see F109-018 discussion???
4839 Set_Entity (Name (Call_Node), Parent_Subp);
4841 -- Inspect all formals of derived subprogram Subp. Compare parameter
4842 -- types with the parent subprogram and check whether an actual may
4843 -- need a type conversion to the corresponding formal of the parent
4844 -- subprogram.
4846 -- Not clear whether intrinsic subprograms need such conversions. ???
4848 if not Is_Intrinsic_Subprogram (Parent_Subp)
4849 or else Is_Generic_Instance (Parent_Subp)
4850 then
4851 declare
4852 procedure Convert (Act : Node_Id; Typ : Entity_Id);
4853 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
4854 -- and resolve the newly generated construct.
4856 -------------
4857 -- Convert --
4858 -------------
4860 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
4861 begin
4862 Rewrite (Act, OK_Convert_To (Typ, Act));
4863 Analyze_And_Resolve (Act, Typ);
4864 end Convert;
4866 -- Local variables
4868 Actual_Typ : Entity_Id;
4869 Formal_Typ : Entity_Id;
4870 Parent_Typ : Entity_Id;
4872 begin
4873 Actual := First_Actual (Call_Node);
4874 Formal := First_Formal (Subp);
4875 Parent_Formal := First_Formal (Parent_Subp);
4876 while Present (Formal) loop
4877 Actual_Typ := Etype (Actual);
4878 Formal_Typ := Etype (Formal);
4879 Parent_Typ := Etype (Parent_Formal);
4881 -- For an IN parameter of a scalar type, the derived formal
4882 -- type and parent formal type differ, and the parent formal
4883 -- type and actual type do not match statically.
4885 if Is_Scalar_Type (Formal_Typ)
4886 and then Ekind (Formal) = E_In_Parameter
4887 and then Formal_Typ /= Parent_Typ
4888 and then
4889 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
4890 and then not Raises_Constraint_Error (Actual)
4891 then
4892 Convert (Actual, Parent_Typ);
4894 -- For access types, the parent formal type and actual type
4895 -- differ.
4897 elsif Is_Access_Type (Formal_Typ)
4898 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
4899 then
4900 if Ekind (Formal) /= E_In_Parameter then
4901 Convert (Actual, Parent_Typ);
4903 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
4904 and then Designated_Type (Parent_Typ) /=
4905 Designated_Type (Actual_Typ)
4906 and then not Is_Controlling_Formal (Formal)
4907 then
4908 -- This unchecked conversion is not necessary unless
4909 -- inlining is enabled, because in that case the type
4910 -- mismatch may become visible in the body about to be
4911 -- inlined.
4913 Rewrite (Actual,
4914 Unchecked_Convert_To (Parent_Typ, Actual));
4915 Analyze_And_Resolve (Actual, Parent_Typ);
4916 end if;
4918 -- If there is a change of representation, then generate a
4919 -- warning, and do the change of representation.
4921 elsif not Has_Compatible_Representation
4922 (Target_Typ => Formal_Typ,
4923 Operand_Typ => Parent_Typ)
4924 then
4925 Error_Msg_N
4926 ("??change of representation required", Actual);
4927 Convert (Actual, Parent_Typ);
4929 -- For array and record types, the parent formal type and
4930 -- derived formal type have different sizes or pragma Pack
4931 -- status.
4933 elsif ((Is_Array_Type (Formal_Typ)
4934 and then Is_Array_Type (Parent_Typ))
4935 or else
4936 (Is_Record_Type (Formal_Typ)
4937 and then Is_Record_Type (Parent_Typ)))
4938 and then Known_Esize (Formal_Typ)
4939 and then Known_Esize (Parent_Typ)
4940 and then
4941 (Esize (Formal_Typ) /= Esize (Parent_Typ)
4942 or else Has_Pragma_Pack (Formal_Typ) /=
4943 Has_Pragma_Pack (Parent_Typ))
4944 then
4945 Convert (Actual, Parent_Typ);
4946 end if;
4948 Next_Actual (Actual);
4949 Next_Formal (Formal);
4950 Next_Formal (Parent_Formal);
4951 end loop;
4952 end;
4953 end if;
4955 Orig_Subp := Subp;
4956 Subp := Parent_Subp;
4957 end if;
4959 -- Deal with case where call is an explicit dereference
4961 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
4963 -- Handle case of access to protected subprogram type
4965 if Is_Access_Protected_Subprogram_Type
4966 (Base_Type (Etype (Prefix (Name (Call_Node)))))
4967 then
4968 -- If this is a call through an access to protected operation, the
4969 -- prefix has the form (object'address, operation'access). Rewrite
4970 -- as a for other protected calls: the object is the 1st parameter
4971 -- of the list of actuals.
4973 declare
4974 Call : Node_Id;
4975 Parm : List_Id;
4976 Nam : Node_Id;
4977 Obj : Node_Id;
4978 Ptr : constant Node_Id := Prefix (Name (Call_Node));
4980 T : constant Entity_Id :=
4981 Equivalent_Type (Base_Type (Etype (Ptr)));
4983 D_T : constant Entity_Id :=
4984 Designated_Type (Base_Type (Etype (Ptr)));
4986 begin
4987 Obj :=
4988 Make_Selected_Component (Loc,
4989 Prefix => Unchecked_Convert_To (T, Ptr),
4990 Selector_Name =>
4991 New_Occurrence_Of (First_Entity (T), Loc));
4993 Nam :=
4994 Make_Selected_Component (Loc,
4995 Prefix => Unchecked_Convert_To (T, Ptr),
4996 Selector_Name =>
4997 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
4999 Nam :=
5000 Make_Explicit_Dereference (Loc,
5001 Prefix => Nam);
5003 if Present (Parameter_Associations (Call_Node)) then
5004 Parm := Parameter_Associations (Call_Node);
5005 else
5006 Parm := New_List;
5007 end if;
5009 Prepend (Obj, Parm);
5011 if Etype (D_T) = Standard_Void_Type then
5012 Call :=
5013 Make_Procedure_Call_Statement (Loc,
5014 Name => Nam,
5015 Parameter_Associations => Parm);
5016 else
5017 Call :=
5018 Make_Function_Call (Loc,
5019 Name => Nam,
5020 Parameter_Associations => Parm);
5021 end if;
5023 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
5024 Set_Etype (Call, Etype (D_T));
5026 -- We do not re-analyze the call to avoid infinite recursion.
5027 -- We analyze separately the prefix and the object, and set
5028 -- the checks on the prefix that would otherwise be emitted
5029 -- when resolving a call.
5031 Rewrite (Call_Node, Call);
5032 Analyze (Nam);
5033 Apply_Access_Check (Nam);
5034 Analyze (Obj);
5035 return;
5036 end;
5037 end if;
5038 end if;
5040 -- If this is a call to an intrinsic subprogram, then perform the
5041 -- appropriate expansion to the corresponding tree node and we
5042 -- are all done (since after that the call is gone).
5044 -- In the case where the intrinsic is to be processed by the back end,
5045 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
5046 -- since the idea in this case is to pass the call unchanged. If the
5047 -- intrinsic is an inherited unchecked conversion, and the derived type
5048 -- is the target type of the conversion, we must retain it as the return
5049 -- type of the expression. Otherwise the expansion below, which uses the
5050 -- parent operation, will yield the wrong type.
5052 if Is_Intrinsic_Subprogram (Subp) then
5053 Expand_Intrinsic_Call (Call_Node, Subp);
5055 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
5056 and then Parent_Subp /= Orig_Subp
5057 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
5058 then
5059 Set_Etype (Call_Node, Etype (Orig_Subp));
5060 end if;
5062 return;
5063 end if;
5065 if Ekind (Subp) in E_Function | E_Procedure then
5067 -- We perform a simple optimization on calls for To_Address by
5068 -- replacing them with an unchecked conversion. Not only is this
5069 -- efficient, but it also avoids order of elaboration problems when
5070 -- address clauses are inlined (address expression elaborated at the
5071 -- wrong point).
5073 -- We perform this optimization regardless of whether we are in the
5074 -- main unit or in a unit in the context of the main unit, to ensure
5075 -- that the generated tree is the same in both cases, for CodePeer
5076 -- use.
5078 if Is_RTE (Subp, RE_To_Address) then
5079 Rewrite (Call_Node,
5080 Unchecked_Convert_To
5081 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
5082 return;
5084 -- A call to a null procedure is replaced by a null statement, but we
5085 -- are not allowed to ignore possible side effects of the call, so we
5086 -- make sure that actuals are evaluated.
5087 -- We also suppress this optimization for GNATcoverage.
5089 elsif Is_Null_Procedure (Subp)
5090 and then not Opt.Suppress_Control_Flow_Optimizations
5091 then
5092 Actual := First_Actual (Call_Node);
5093 while Present (Actual) loop
5094 Remove_Side_Effects (Actual);
5095 Next_Actual (Actual);
5096 end loop;
5098 Rewrite (Call_Node, Make_Null_Statement (Loc));
5099 return;
5100 end if;
5102 -- Handle inlining. No action needed if the subprogram is not inlined
5104 if not Is_Inlined (Subp) then
5105 null;
5107 -- Front-end inlining of expression functions (performed also when
5108 -- back-end inlining is enabled).
5110 elsif Is_Inlinable_Expression_Function (Subp) then
5111 Rewrite
5112 (Call_Node, New_Copy (Expression_Of_Expression_Function (Subp)));
5113 Analyze (Call_Node);
5114 return;
5116 -- Handle front-end inlining
5118 elsif not Back_End_Inlining then
5119 Inlined_Subprogram : declare
5120 Bod : Node_Id;
5121 Must_Inline : Boolean := False;
5122 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
5124 begin
5125 -- Verify that the body to inline has already been seen, and
5126 -- that if the body is in the current unit the inlining does
5127 -- not occur earlier. This avoids order-of-elaboration problems
5128 -- in the back end.
5130 -- This should be documented in sinfo/einfo ???
5132 if No (Spec)
5133 or else Nkind (Spec) /= N_Subprogram_Declaration
5134 or else No (Body_To_Inline (Spec))
5135 then
5136 Must_Inline := False;
5138 -- If this an inherited function that returns a private type,
5139 -- do not inline if the full view is an unconstrained array,
5140 -- because such calls cannot be inlined.
5142 elsif Present (Orig_Subp)
5143 and then Is_Array_Type (Etype (Orig_Subp))
5144 and then not Is_Constrained (Etype (Orig_Subp))
5145 then
5146 Must_Inline := False;
5148 elsif In_Unfrozen_Instance (Scope (Subp)) then
5149 Must_Inline := False;
5151 else
5152 Bod := Body_To_Inline (Spec);
5154 if (In_Extended_Main_Code_Unit (Call_Node)
5155 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
5156 or else Has_Pragma_Inline_Always (Subp))
5157 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
5158 or else
5159 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
5160 then
5161 Must_Inline := True;
5163 -- If we are compiling a package body that is not the main
5164 -- unit, it must be for inlining/instantiation purposes,
5165 -- in which case we inline the call to insure that the same
5166 -- temporaries are generated when compiling the body by
5167 -- itself. Otherwise link errors can occur.
5169 -- If the function being called is itself in the main unit,
5170 -- we cannot inline, because there is a risk of double
5171 -- elaboration and/or circularity: the inlining can make
5172 -- visible a private entity in the body of the main unit,
5173 -- that gigi will see before its sees its proper definition.
5175 elsif not In_Extended_Main_Code_Unit (Call_Node)
5176 and then In_Package_Body
5177 then
5178 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
5179 end if;
5180 end if;
5182 if Must_Inline then
5183 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
5185 else
5186 -- Let the back end handle it
5188 Add_Inlined_Body (Subp, Call_Node);
5190 if Front_End_Inlining
5191 and then Nkind (Spec) = N_Subprogram_Declaration
5192 and then In_Extended_Main_Code_Unit (Call_Node)
5193 and then No (Body_To_Inline (Spec))
5194 and then not Has_Completion (Subp)
5195 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
5196 then
5197 Cannot_Inline
5198 ("cannot inline& (body not seen yet)?",
5199 Call_Node, Subp);
5200 end if;
5201 end if;
5202 end Inlined_Subprogram;
5204 -- Front-end expansion of simple functions returning unconstrained
5205 -- types (see Check_And_Split_Unconstrained_Function). Note that the
5206 -- case of a simple renaming (Body_To_Inline in N_Entity below, see
5207 -- also Build_Renamed_Body) cannot be expanded here because this may
5208 -- give rise to order-of-elaboration issues for the types of the
5209 -- parameters of the subprogram, if any.
5211 elsif Present (Unit_Declaration_Node (Subp))
5212 and then Nkind (Unit_Declaration_Node (Subp)) =
5213 N_Subprogram_Declaration
5214 and then Present (Body_To_Inline (Unit_Declaration_Node (Subp)))
5215 and then
5216 Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) not in
5217 N_Entity
5218 then
5219 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
5221 -- Back-end inlining either if optimization is enabled, we're
5222 -- generating C, or the call is required to be inlined.
5224 elsif Optimization_Level > 0
5225 or else CCG_Mode
5226 or else Has_Pragma_Inline_Always (Subp)
5227 then
5228 Add_Inlined_Body (Subp, Call_Node);
5229 end if;
5230 end if;
5232 -- Check for protected subprogram. This is either an intra-object call,
5233 -- or a protected function call. Protected procedure calls are rewritten
5234 -- as entry calls and handled accordingly.
5236 -- In Ada 2005, this may be an indirect call to an access parameter that
5237 -- is an access_to_subprogram. In that case the anonymous type has a
5238 -- scope that is a protected operation, but the call is a regular one.
5239 -- In either case do not expand call if subprogram is eliminated.
5241 Scop := Scope (Subp);
5243 if Nkind (Call_Node) /= N_Entry_Call_Statement
5244 and then Is_Protected_Type (Scop)
5245 and then Ekind (Subp) /= E_Subprogram_Type
5246 and then not Is_Eliminated (Subp)
5247 then
5248 -- If the call is an internal one, it is rewritten as a call to the
5249 -- corresponding unprotected subprogram.
5251 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
5252 end if;
5254 -- Functions returning controlled objects need special attention. If
5255 -- the return type is limited, then the context is initialization and
5256 -- different processing applies. If the call is to a protected function,
5257 -- the expansion above will call Expand_Call recursively. Otherwise the
5258 -- function call is transformed into a reference to the result that has
5259 -- been built either on the primary or the secondary stack.
5261 if Nkind (Call_Node) = N_Function_Call
5262 and then Needs_Finalization (Etype (Call_Node))
5263 then
5264 if not Is_Build_In_Place_Function_Call (Call_Node)
5265 and then
5266 (No (First_Formal (Subp))
5267 or else
5268 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
5269 then
5270 Expand_Ctrl_Function_Call
5271 (Call_Node, Needs_Secondary_Stack (Etype (Call_Node)));
5273 -- Build-in-place function calls which appear in anonymous contexts
5274 -- need a transient scope to ensure the proper finalization of the
5275 -- intermediate result after its use.
5277 elsif Is_Build_In_Place_Function_Call (Call_Node)
5278 and then Nkind (Parent (Unqual_Conv (Call_Node))) in
5279 N_Attribute_Reference
5280 | N_Function_Call
5281 | N_Indexed_Component
5282 | N_Object_Renaming_Declaration
5283 | N_Procedure_Call_Statement
5284 | N_Selected_Component
5285 | N_Slice
5286 and then
5287 (Ekind (Current_Scope) /= E_Loop
5288 or else Nkind (Parent (Call_Node)) /= N_Function_Call
5289 or else not
5290 Is_Build_In_Place_Function_Call (Parent (Call_Node)))
5291 then
5292 Establish_Transient_Scope
5293 (Call_Node, Needs_Secondary_Stack (Etype (Call_Node)));
5294 end if;
5295 end if;
5296 end Expand_Call_Helper;
5298 -------------------------------
5299 -- Expand_Ctrl_Function_Call --
5300 -------------------------------
5302 procedure Expand_Ctrl_Function_Call (N : Node_Id; Use_Sec_Stack : Boolean)
5304 Par : constant Node_Id := Parent (N);
5306 begin
5307 -- Optimization: if the returned value is returned again, then no need
5308 -- to copy/readjust/finalize, we can just pass the value through (see
5309 -- Expand_N_Simple_Return_Statement), and thus no attachment is needed.
5310 -- Note that simple return statements are distributed into conditional
5311 -- expressions but we may be invoked before this distribution is done.
5313 if Nkind (Par) = N_Simple_Return_Statement
5314 or else (Nkind (Par) = N_If_Expression
5315 and then Nkind (Parent (Par)) = N_Simple_Return_Statement)
5316 or else (Nkind (Par) = N_Case_Expression_Alternative
5317 and then
5318 Nkind (Parent (Parent (Par))) = N_Simple_Return_Statement)
5319 then
5320 return;
5321 end if;
5323 -- Another optimization: if the returned value is used to initialize an
5324 -- object, then no need to copy/readjust/finalize, we can initialize it
5325 -- in place. However, if the call returns on the secondary stack, then
5326 -- we need the expansion because we'll be renaming the temporary as the
5327 -- (permanent) object. We also apply it in the case of the expression of
5328 -- a delta aggregate, since it is used only to initialize a temporary.
5330 if Nkind (Par) in N_Object_Declaration | N_Delta_Aggregate
5331 and then Expression (Par) = N
5332 and then not Use_Sec_Stack
5333 then
5334 return;
5335 end if;
5337 -- Resolution is now finished, make sure we don't start analysis again
5338 -- because of the duplication.
5340 Set_Analyzed (N);
5342 -- Apply the transformation unless it was already applied earlier. This
5343 -- may happen because Remove_Side_Effects can be called during semantic
5344 -- analysis, for example from Build_Actual_Subtype_Of_Component. It is
5345 -- crucial to avoid creating a reference of reference here, because it
5346 -- would not be subsequently recognized by the Is_Finalizable_Transient
5347 -- and Requires_Cleanup_Actions predicates.
5349 if Nkind (Par) /= N_Reference then
5350 Remove_Side_Effects (N);
5351 end if;
5352 end Expand_Ctrl_Function_Call;
5354 ----------------------------------------
5355 -- Expand_N_Extended_Return_Statement --
5356 ----------------------------------------
5358 -- If there is a Handled_Statement_Sequence, we rewrite this:
5360 -- return Result : T := <expression> do
5361 -- <handled_seq_of_stms>
5362 -- end return;
5364 -- to be:
5366 -- declare
5367 -- Result : T := <expression>;
5368 -- begin
5369 -- <handled_seq_of_stms>
5370 -- return Result;
5371 -- end;
5373 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5375 -- return Result : T := <expression>;
5377 -- to be:
5379 -- return <expression>;
5381 -- unless it's build-in-place or there's no <expression>, in which case
5382 -- we generate:
5384 -- declare
5385 -- Result : T := <expression>;
5386 -- begin
5387 -- return Result;
5388 -- end;
5390 -- Note that this case could have been written by the user as an extended
5391 -- return statement, or could have been transformed to this from a simple
5392 -- return statement.
5394 -- That is, we need to have a reified return object if there are statements
5395 -- (which might refer to it) or if we're doing build-in-place (so we can
5396 -- set its address to the final resting place or if there is no expression
5397 -- (in which case default initial values might need to be set)).
5399 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
5400 Loc : constant Source_Ptr := Sloc (N);
5401 Func_Id : constant Entity_Id :=
5402 Return_Applies_To (Return_Statement_Entity (N));
5403 Is_BIP_Func : constant Boolean :=
5404 Is_Build_In_Place_Function (Func_Id);
5405 Ret_Obj_Id : constant Entity_Id :=
5406 First_Entity (Return_Statement_Entity (N));
5407 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
5408 Ret_Typ : constant Entity_Id := Etype (Func_Id);
5410 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
5411 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5412 -- with parameters:
5413 -- From current activation chain
5414 -- To activation chain passed in by the caller
5415 -- New_Master master passed in by the caller
5417 -- Func_Id is the entity of the function where the extended return
5418 -- statement appears.
5420 ---------------------------
5421 -- Move_Activation_Chain --
5422 ---------------------------
5424 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
5425 begin
5426 return
5427 Make_Procedure_Call_Statement (Loc,
5428 Name =>
5429 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
5431 Parameter_Associations => New_List (
5433 -- Source chain
5435 Make_Attribute_Reference (Loc,
5436 Prefix => Make_Identifier (Loc, Name_uChain),
5437 Attribute_Name => Name_Unrestricted_Access),
5439 -- Destination chain
5441 New_Occurrence_Of
5442 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
5444 -- New master
5446 New_Occurrence_Of
5447 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
5448 end Move_Activation_Chain;
5450 -- Local variables
5452 Exp : Node_Id;
5453 HSS : Node_Id;
5454 Result : Node_Id;
5455 Stmts : List_Id := No_List;
5457 Return_Stmt : Node_Id := Empty;
5458 -- Force initialization to facilitate static analysis
5460 -- Start of processing for Expand_N_Extended_Return_Statement
5462 begin
5463 -- Given that functionality of interface thunks is simple (just displace
5464 -- the pointer to the object) they are always handled by means of
5465 -- simple return statements.
5467 pragma Assert (not Is_Thunk (Current_Subprogram));
5469 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
5470 Exp := Expression (Ret_Obj_Decl);
5472 -- Assert that if F says "return R : T := G(...) do..."
5473 -- then F and G are both b-i-p, or neither b-i-p.
5475 if Present (Exp) and then Nkind (Exp) = N_Function_Call then
5476 pragma Assert (Ekind (Current_Subprogram) = E_Function);
5477 pragma Assert
5478 (Is_Build_In_Place_Function (Current_Subprogram) =
5479 Is_Build_In_Place_Function_Call (Exp));
5480 null;
5481 end if;
5483 else
5484 Exp := Empty;
5485 end if;
5487 HSS := Handled_Statement_Sequence (N);
5489 -- Build a simple_return_statement that returns the return object when
5490 -- there is a statement sequence, or no expression, or the analysis of
5491 -- the return object declaration generated extra actions, or the result
5492 -- will be built in place. Note however that we currently do this for
5493 -- all composite cases, even though they are not built in place.
5495 if Present (HSS)
5496 or else No (Exp)
5497 or else List_Length (Return_Object_Declarations (N)) > 1
5498 or else Is_Composite_Type (Ret_Typ)
5499 then
5500 if No (HSS) then
5501 Stmts := New_List;
5503 -- If the extended return has a handled statement sequence, then wrap
5504 -- it in a block and use the block as the first statement.
5506 else
5507 Stmts := New_List (
5508 Make_Block_Statement (Loc,
5509 Declarations => New_List,
5510 Handled_Statement_Sequence => HSS));
5511 end if;
5513 -- If the result type contains tasks, we call Move_Activation_Chain.
5514 -- Later, the cleanup code will call Complete_Master, which will
5515 -- terminate any unactivated tasks belonging to the return statement
5516 -- master. But Move_Activation_Chain updates their master to be that
5517 -- of the caller, so they will not be terminated unless the return
5518 -- statement completes unsuccessfully due to exception, abort, goto,
5519 -- or exit. As a formality, we test whether the function requires the
5520 -- result to be built in place, though that's necessarily true for
5521 -- the case of result types with task parts.
5523 if Is_BIP_Func and then Has_Task (Ret_Typ) then
5525 -- The return expression is an aggregate for a complex type which
5526 -- contains tasks. This particular case is left unexpanded since
5527 -- the regular expansion would insert all temporaries and
5528 -- initialization code in the wrong block.
5530 if Nkind (Exp) = N_Aggregate then
5531 Expand_N_Aggregate (Exp);
5532 end if;
5534 -- Do not move the activation chain if the return object does not
5535 -- contain tasks.
5537 if Has_Task (Etype (Ret_Obj_Id)) then
5538 Append_To (Stmts, Move_Activation_Chain (Func_Id));
5539 end if;
5540 end if;
5542 -- If the returned object needs finalization actions, the function
5543 -- must perform the appropriate cleanup should it fail to return.
5545 if Needs_Finalization (Etype (Ret_Obj_Id)) then
5546 Append_To
5547 (Stmts, Make_Suppress_Object_Finalize_Call (Loc, Ret_Obj_Id));
5548 end if;
5550 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
5551 end if;
5553 -- Case where we build a return statement block
5555 if Present (HSS) then
5556 Result :=
5557 Make_Block_Statement (Loc,
5558 Declarations => Return_Object_Declarations (N),
5559 Handled_Statement_Sequence => HSS);
5561 -- We set the entity of the new block statement to be that of the
5562 -- return statement. This is necessary so that various fields, such
5563 -- as Finalization_Chain_Entity carry over from the return statement
5564 -- to the block. Note that this block is unusual, in that its entity
5565 -- is an E_Return_Statement rather than an E_Block.
5567 Set_Identifier
5568 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
5570 -- Build a simple_return_statement that returns the return object
5572 Return_Stmt :=
5573 Make_Simple_Return_Statement (Loc,
5574 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
5575 Append_To (Stmts, Return_Stmt);
5577 -- Case where we do not need to build a block. But we're about to drop
5578 -- Return_Object_Declarations on the floor, so assert that it contains
5579 -- only the return object declaration.
5581 else pragma Assert (List_Length (Return_Object_Declarations (N)) = 1);
5583 -- Build simple_return_statement that returns the expression directly
5585 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5586 Result := Return_Stmt;
5587 end if;
5589 -- Set the flag to prevent infinite recursion
5591 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5592 Set_Return_Statement (Ret_Obj_Id, Return_Stmt);
5594 Rewrite (N, Result);
5596 -- AI12-043: The checks of 6.5(8.1/3) and 6.5(21/3) are made immediately
5597 -- before an object is returned. A predicate that applies to the return
5598 -- subtype is checked immediately before an object is returned.
5600 Analyze (N);
5601 end Expand_N_Extended_Return_Statement;
5603 ----------------------------
5604 -- Expand_N_Function_Call --
5605 ----------------------------
5607 procedure Expand_N_Function_Call (N : Node_Id) is
5608 begin
5609 Expand_Call (N);
5610 end Expand_N_Function_Call;
5612 ---------------------------------------
5613 -- Expand_N_Procedure_Call_Statement --
5614 ---------------------------------------
5616 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5617 begin
5618 Expand_Call (N);
5619 end Expand_N_Procedure_Call_Statement;
5621 ------------------------------------
5622 -- Expand_N_Return_When_Statement --
5623 ------------------------------------
5625 procedure Expand_N_Return_When_Statement (N : Node_Id) is
5626 Loc : constant Source_Ptr := Sloc (N);
5627 begin
5628 Rewrite (N,
5629 Make_If_Statement (Loc,
5630 Condition => Condition (N),
5631 Then_Statements => New_List (
5632 Make_Simple_Return_Statement (Loc,
5633 Expression => Expression (N)))));
5635 Analyze (N);
5636 end Expand_N_Return_When_Statement;
5638 --------------------------------------
5639 -- Expand_N_Simple_Return_Statement --
5640 --------------------------------------
5642 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5643 begin
5644 -- Defend against previous errors (i.e. the return statement calls a
5645 -- function that is not available in configurable runtime).
5647 if Present (Expression (N))
5648 and then Nkind (Expression (N)) = N_Empty
5649 then
5650 Check_Error_Detected;
5651 return;
5652 end if;
5654 -- Distinguish the function and non-function cases:
5656 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5657 when E_Function
5658 | E_Generic_Function
5660 Expand_Simple_Function_Return (N);
5662 when E_Entry
5663 | E_Entry_Family
5664 | E_Generic_Procedure
5665 | E_Procedure
5666 | E_Return_Statement
5668 Expand_Non_Function_Return (N);
5670 when others =>
5671 raise Program_Error;
5672 end case;
5674 exception
5675 when RE_Not_Available =>
5676 return;
5677 end Expand_N_Simple_Return_Statement;
5679 ------------------------------
5680 -- Expand_N_Subprogram_Body --
5681 ------------------------------
5683 -- Add dummy push/pop label nodes at start and end to clear any local
5684 -- exception indications if local-exception-to-goto optimization is active.
5686 -- Add return statement if last statement in body is not a return statement
5687 -- (this makes things easier on Gigi which does not want to have to handle
5688 -- a missing return).
5690 -- Deal with possible detection of infinite recursion
5692 -- Eliminate body completely if convention stubbed
5694 -- Encode entity names within body, since we will not need to reference
5695 -- these entities any longer in the front end.
5697 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5699 -- Reset Pure indication if any parameter has root type System.Address
5700 -- or has any parameters of limited types, where limited means that the
5701 -- run-time view is limited (i.e. the full type is limited).
5703 -- Apply raise check
5705 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5706 Body_Id : constant Entity_Id := Defining_Entity (N);
5707 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5708 Loc : constant Source_Ptr := Sloc (N);
5710 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5711 -- Append a return statement to the statement sequence Stmts if the last
5712 -- statement is not already a return or a goto statement. Note that the
5713 -- latter test is not critical, it does not matter if we add a few extra
5714 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5715 -- the corresponding spec of the subprogram body.
5717 ----------------
5718 -- Add_Return --
5719 ----------------
5721 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5722 Last_Stmt : Node_Id;
5723 Loc : Source_Ptr;
5724 Stmt : Node_Id;
5726 begin
5727 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5728 -- not relevant in this context since they are not executable.
5730 Last_Stmt := Last (Stmts);
5731 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5732 Prev (Last_Stmt);
5733 end loop;
5735 -- Now insert return unless last statement is a transfer
5737 if not Is_Transfer (Last_Stmt) then
5739 -- The source location for the return is the end label of the
5740 -- procedure if present. Otherwise use the sloc of the last
5741 -- statement in the list. If the list comes from a generated
5742 -- exception handler and we are not debugging generated code,
5743 -- all the statements within the handler are made invisible
5744 -- to the debugger.
5746 if Nkind (Parent (Stmts)) = N_Exception_Handler
5747 and then not Comes_From_Source (Parent (Stmts))
5748 then
5749 Loc := Sloc (Last_Stmt);
5750 elsif Present (End_Label (HSS)) then
5751 Loc := Sloc (End_Label (HSS));
5752 else
5753 Loc := Sloc (Last_Stmt);
5754 end if;
5756 -- Append return statement, and set analyzed manually. We can't
5757 -- call Analyze on this return since the scope is wrong.
5759 -- Note: it almost works to push the scope and then do the Analyze
5760 -- call, but something goes wrong in some weird cases and it is
5761 -- not worth worrying about ???
5763 Stmt := Make_Simple_Return_Statement (Loc);
5765 -- The return statement is handled properly, and the call to the
5766 -- postcondition, inserted below, does not require information
5767 -- from the body either. However, that call is analyzed in the
5768 -- enclosing scope, and an elaboration check might improperly be
5769 -- added to it. A guard in Sem_Elab is needed to prevent that
5770 -- spurious check, see Check_Elab_Call.
5772 Append_To (Stmts, Stmt);
5773 Set_Analyzed (Stmt);
5775 -- Ada 2022 (AI12-0279): append the call to 'Yield unless this is
5776 -- a generic subprogram (since in such case it will be added to
5777 -- the instantiations).
5779 if Has_Yield_Aspect (Spec_Id)
5780 and then Ekind (Spec_Id) /= E_Generic_Procedure
5781 and then RTE_Available (RE_Yield)
5782 then
5783 Insert_Action (Stmt,
5784 Make_Procedure_Call_Statement (Loc,
5785 New_Occurrence_Of (RTE (RE_Yield), Loc)));
5786 end if;
5787 end if;
5788 end Add_Return;
5790 -- Local variables
5792 Except_H : Node_Id;
5793 L : List_Id;
5794 Spec_Id : Entity_Id;
5796 -- Start of processing for Expand_N_Subprogram_Body
5798 begin
5799 if Present (Corresponding_Spec (N)) then
5800 Spec_Id := Corresponding_Spec (N);
5801 else
5802 Spec_Id := Body_Id;
5803 end if;
5805 -- If this is a Pure function which has any parameters whose root type
5806 -- is System.Address, reset the Pure indication.
5807 -- This check is also performed when the subprogram is frozen, but we
5808 -- repeat it on the body so that the indication is consistent, and so
5809 -- it applies as well to bodies without separate specifications.
5811 if Is_Pure (Spec_Id)
5812 and then Is_Subprogram (Spec_Id)
5813 and then not Has_Pragma_Pure_Function (Spec_Id)
5814 then
5815 Check_Function_With_Address_Parameter (Spec_Id);
5817 if Spec_Id /= Body_Id then
5818 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5819 end if;
5820 end if;
5822 -- Set L to either the list of declarations if present, or to the list
5823 -- of statements if no declarations are present. This is used to insert
5824 -- new stuff at the start.
5826 if Is_Non_Empty_List (Declarations (N)) then
5827 L := Declarations (N);
5828 else
5829 L := Statements (HSS);
5830 end if;
5832 -- If local-exception-to-goto optimization active, insert dummy push
5833 -- statements at start, and dummy pop statements at end, but inhibit
5834 -- this if we have No_Exception_Handlers or expanding a entry barrier
5835 -- function, since they are useless and interfere with analysis (e.g. by
5836 -- CodePeer) and other optimizations. We also don't need these if we're
5837 -- unnesting subprograms because the only purpose of these nodes is to
5838 -- ensure we don't set a label in one subprogram and branch to it in
5839 -- another.
5841 if (Debug_Flag_Dot_G
5842 or else Restriction_Active (No_Exception_Propagation))
5843 and then not Restriction_Active (No_Exception_Handlers)
5844 and then not CodePeer_Mode
5845 and then not Is_Entry_Barrier_Function (N)
5846 and then not Unnest_Subprogram_Mode
5847 and then Is_Non_Empty_List (L)
5848 then
5849 declare
5850 FS : constant Node_Id := First (L);
5851 FL : constant Source_Ptr := Sloc (FS);
5852 LS : Node_Id;
5853 LL : Source_Ptr;
5855 begin
5856 -- LS points to either last statement, if statements are present
5857 -- or to the last declaration if there are no statements present.
5858 -- It is the node after which the pop's are generated.
5860 if Is_Non_Empty_List (Statements (HSS)) then
5861 LS := Last (Statements (HSS));
5862 else
5863 LS := Last (L);
5864 end if;
5866 LL := Sloc (LS);
5868 Insert_List_Before_And_Analyze (FS, New_List (
5869 Make_Push_Constraint_Error_Label (FL),
5870 Make_Push_Program_Error_Label (FL),
5871 Make_Push_Storage_Error_Label (FL)));
5873 Insert_List_After_And_Analyze (LS, New_List (
5874 Make_Pop_Constraint_Error_Label (LL),
5875 Make_Pop_Program_Error_Label (LL),
5876 Make_Pop_Storage_Error_Label (LL)));
5877 end;
5878 end if;
5880 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5882 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5883 declare
5884 F : Entity_Id;
5885 A : Node_Id;
5887 begin
5888 -- Loop through formals
5890 F := First_Formal (Spec_Id);
5891 while Present (F) loop
5892 if Is_Scalar_Type (Etype (F))
5893 and then Ekind (F) = E_Out_Parameter
5894 then
5895 Check_Restriction (No_Default_Initialization, F);
5897 -- Insert the initialization. We turn off validity checks
5898 -- for this assignment, since we do not want any check on
5899 -- the initial value itself (which may well be invalid).
5900 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5902 A :=
5903 Make_Assignment_Statement (Loc,
5904 Name => New_Occurrence_Of (F, Loc),
5905 Expression => Get_Simple_Init_Val (Etype (F), N));
5906 Set_Suppress_Assignment_Checks (A);
5908 Insert_Before_And_Analyze (First (L),
5909 A, Suppress => Validity_Check);
5910 end if;
5912 Next_Formal (F);
5913 end loop;
5914 end;
5915 end if;
5917 -- Clear out statement list for stubbed procedure
5919 if Present (Corresponding_Spec (N)) then
5920 Set_Elaboration_Flag (N, Spec_Id);
5922 if Convention (Spec_Id) = Convention_Stubbed
5923 or else Is_Eliminated (Spec_Id)
5924 then
5925 Set_Declarations (N, Empty_List);
5926 Set_Handled_Statement_Sequence (N,
5927 Make_Handled_Sequence_Of_Statements (Loc,
5928 Statements => New_List (Make_Null_Statement (Loc))));
5930 return;
5931 end if;
5932 end if;
5934 -- Create a set of discriminals for the next protected subprogram body
5936 if Is_List_Member (N)
5937 and then Present (Parent (List_Containing (N)))
5938 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5939 and then Present (Next_Protected_Operation (N))
5940 then
5941 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5942 end if;
5944 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5945 -- subprograms with no specs are not frozen.
5947 Compute_Returns_By_Ref (Spec_Id);
5949 -- For a procedure, we add a return for all possible syntactic ends of
5950 -- the subprogram.
5952 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure then
5953 Add_Return (Spec_Id, Statements (HSS));
5955 if Present (Exception_Handlers (HSS)) then
5956 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5957 while Present (Except_H) loop
5958 Add_Return (Spec_Id, Statements (Except_H));
5959 Next_Non_Pragma (Except_H);
5960 end loop;
5961 end if;
5963 -- For a function, we must deal with the case where there is at least
5964 -- one missing return. What we do is to wrap the entire body of the
5965 -- function in a block:
5967 -- begin
5968 -- ...
5969 -- end;
5971 -- becomes
5973 -- begin
5974 -- begin
5975 -- ...
5976 -- end;
5978 -- raise Program_Error;
5979 -- end;
5981 -- This approach is necessary because the raise must be signalled to the
5982 -- caller, not handled by any local handler (RM 6.4(11)).
5984 -- Note: we do not need to analyze the constructed sequence here, since
5985 -- it has no handler, and an attempt to analyze the handled statement
5986 -- sequence twice is risky in various ways (e.g. the issue of expanding
5987 -- cleanup actions twice).
5989 elsif Has_Missing_Return (Spec_Id) then
5990 declare
5991 Hloc : constant Source_Ptr := Sloc (HSS);
5992 Blok : constant Node_Id :=
5993 Make_Block_Statement (Hloc,
5994 Handled_Statement_Sequence => HSS);
5995 Rais : constant Node_Id :=
5996 Make_Raise_Program_Error (Hloc,
5997 Reason => PE_Missing_Return);
5999 begin
6000 Set_Handled_Statement_Sequence (N,
6001 Make_Handled_Sequence_Of_Statements (Hloc,
6002 Statements => New_List (Blok, Rais)));
6004 Push_Scope (Spec_Id);
6005 Analyze (Blok);
6006 Analyze (Rais);
6007 Pop_Scope;
6008 end;
6009 end if;
6011 -- If subprogram contains a parameterless recursive call, then we may
6012 -- have an infinite recursion, so see if we can generate code to check
6013 -- for this possibility if storage checks are not suppressed.
6015 if Ekind (Spec_Id) = E_Procedure
6016 and then Has_Recursive_Call (Spec_Id)
6017 and then not Storage_Checks_Suppressed (Spec_Id)
6018 then
6019 Detect_Infinite_Recursion (N, Spec_Id);
6020 end if;
6022 -- Set to encode entity names in package body before gigi is called
6024 Qualify_Entity_Names (N);
6026 -- If the body belongs to a nonabstract library-level source primitive
6027 -- of a tagged type, install an elaboration check which ensures that a
6028 -- dispatching call targeting the primitive will not execute the body
6029 -- without it being previously elaborated.
6031 Install_Primitive_Elaboration_Check (N);
6033 -- If the subprogram is subject to pragma No_Raise, apply the check
6035 Apply_Raise_Check (N);
6036 end Expand_N_Subprogram_Body;
6038 -----------------------------------
6039 -- Expand_N_Subprogram_Body_Stub --
6040 -----------------------------------
6042 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
6043 Bod : Node_Id;
6045 begin
6046 if Present (Corresponding_Body (N)) then
6047 Bod := Unit_Declaration_Node (Corresponding_Body (N));
6049 -- The body may have been expanded already when it is analyzed
6050 -- through the subunit node. Do no expand again: it interferes
6051 -- with the construction of unnesting tables when generating C.
6053 if not Analyzed (Bod) then
6054 Expand_N_Subprogram_Body (Bod);
6055 end if;
6057 -- Add full qualification to entities that may be created late
6058 -- during unnesting.
6060 Qualify_Entity_Names (N);
6061 end if;
6062 end Expand_N_Subprogram_Body_Stub;
6064 -------------------------------------
6065 -- Expand_N_Subprogram_Declaration --
6066 -------------------------------------
6068 -- If the declaration appears within a protected body, it is a private
6069 -- operation of the protected type. We must create the corresponding
6070 -- protected subprogram an associated formals. For a normal protected
6071 -- operation, this is done when expanding the protected type declaration.
6073 -- If the declaration is for a null procedure, emit null body
6075 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
6076 Loc : constant Source_Ptr := Sloc (N);
6077 Subp : constant Entity_Id := Defining_Entity (N);
6079 -- Local variables
6081 Scop : constant Entity_Id := Scope (Subp);
6082 Prot_Bod : Node_Id;
6083 Prot_Decl : Node_Id;
6084 Prot_Id : Entity_Id;
6086 begin
6087 -- Deal with case of protected subprogram. Do not generate protected
6088 -- operation if operation is flagged as eliminated.
6090 if Is_List_Member (N)
6091 and then Present (Parent (List_Containing (N)))
6092 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6093 and then Is_Protected_Type (Scop)
6094 then
6095 if No (Protected_Body_Subprogram (Subp))
6096 and then not Is_Eliminated (Subp)
6097 then
6098 Prot_Decl :=
6099 Make_Subprogram_Declaration (Loc,
6100 Specification =>
6101 Build_Protected_Sub_Specification
6102 (N, Scop, Unprotected_Mode));
6104 -- The protected subprogram is declared outside of the protected
6105 -- body. Given that the body has frozen all entities so far, we
6106 -- analyze the subprogram and perform freezing actions explicitly.
6107 -- including the generation of an explicit freeze node, to ensure
6108 -- that gigi has the proper order of elaboration.
6109 -- If the body is a subunit, the insertion point is before the
6110 -- stub in the parent.
6112 Prot_Bod := Parent (List_Containing (N));
6114 if Nkind (Parent (Prot_Bod)) = N_Subunit then
6115 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
6116 end if;
6118 Insert_Before (Prot_Bod, Prot_Decl);
6119 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
6120 Set_Has_Delayed_Freeze (Prot_Id);
6122 Push_Scope (Scope (Scop));
6123 Analyze (Prot_Decl);
6124 Freeze_Before (N, Prot_Id);
6125 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6126 Pop_Scope;
6127 end if;
6129 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6130 -- cases this is superfluous because calls to it will be automatically
6131 -- inlined, but we definitely need the body if preconditions for the
6132 -- procedure are present, or if performing coverage analysis.
6134 elsif Nkind (Specification (N)) = N_Procedure_Specification
6135 and then Null_Present (Specification (N))
6136 then
6137 declare
6138 Bod : constant Node_Id := Body_To_Inline (N);
6140 begin
6141 Set_Has_Completion (Subp, False);
6142 Append_Freeze_Action (Subp, Bod);
6144 -- The body now contains raise statements, so calls to it will
6145 -- not be inlined.
6147 Set_Is_Inlined (Subp, False);
6148 end;
6149 end if;
6150 end Expand_N_Subprogram_Declaration;
6152 --------------------------------
6153 -- Expand_Non_Function_Return --
6154 --------------------------------
6156 procedure Expand_Non_Function_Return (N : Node_Id) is
6157 pragma Assert (No (Expression (N)));
6159 Loc : constant Source_Ptr := Sloc (N);
6160 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
6161 Kind : constant Entity_Kind := Ekind (Scope_Id);
6162 Call : Node_Id;
6163 Acc_Stat : Node_Id;
6164 Goto_Stat : Node_Id;
6165 Lab_Node : Node_Id;
6167 begin
6168 -- Ada 2022 (AI12-0279)
6170 if Has_Yield_Aspect (Scope_Id)
6171 and then RTE_Available (RE_Yield)
6172 then
6173 Insert_Action (N,
6174 Make_Procedure_Call_Statement (Loc,
6175 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6176 end if;
6178 -- If it is a return from a procedure do no extra steps
6180 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6181 return;
6183 -- If it is a nested return within an extended one, replace it with a
6184 -- return of the previously declared return object.
6186 elsif Kind = E_Return_Statement then
6187 declare
6188 Ret_Obj_Id : constant Entity_Id := First_Entity (Scope_Id);
6190 begin
6191 -- Apply the same processing as Expand_N_Extended_Return_Statement
6192 -- if the returned object needs finalization actions. Note that we
6193 -- are invoked before Expand_N_Extended_Return_Statement but there
6194 -- may be multiple nested returns within the extended one.
6196 if Needs_Finalization (Etype (Ret_Obj_Id)) then
6197 Insert_Action
6198 (N, Make_Suppress_Object_Finalize_Call (Loc, Ret_Obj_Id));
6199 end if;
6201 Rewrite (N,
6202 Make_Simple_Return_Statement (Loc,
6203 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc)));
6204 Set_Comes_From_Extended_Return_Statement (N);
6205 Set_Return_Statement_Entity (N, Scope_Id);
6206 Expand_Simple_Function_Return (N);
6207 return;
6208 end;
6209 end if;
6211 pragma Assert (Is_Entry (Scope_Id));
6213 -- Look at the enclosing block to see whether the return is from an
6214 -- accept statement or an entry body.
6216 for J in reverse 0 .. Scope_Stack.Last loop
6217 Scope_Id := Scope_Stack.Table (J).Entity;
6218 exit when Is_Concurrent_Type (Scope_Id);
6219 end loop;
6221 -- If it is a return from accept statement it is expanded as call to
6222 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6224 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6225 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6227 if Is_Task_Type (Scope_Id) then
6229 Call :=
6230 Make_Procedure_Call_Statement (Loc,
6231 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6232 Insert_Before (N, Call);
6233 -- why not insert actions here???
6234 Analyze (Call);
6236 Acc_Stat := Parent (N);
6237 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6238 Acc_Stat := Parent (Acc_Stat);
6239 end loop;
6241 Lab_Node := Last (Statements
6242 (Handled_Statement_Sequence (Acc_Stat)));
6244 Goto_Stat := Make_Goto_Statement (Loc,
6245 Name => New_Occurrence_Of
6246 (Entity (Identifier (Lab_Node)), Loc));
6248 Set_Analyzed (Goto_Stat);
6250 Rewrite (N, Goto_Stat);
6251 Analyze (N);
6253 -- If it is a return from an entry body, put a Complete_Entry_Body call
6254 -- in front of the return.
6256 elsif Is_Protected_Type (Scope_Id) then
6257 Call :=
6258 Make_Procedure_Call_Statement (Loc,
6259 Name =>
6260 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6261 Parameter_Associations => New_List (
6262 Make_Attribute_Reference (Loc,
6263 Prefix =>
6264 New_Occurrence_Of
6265 (Find_Protection_Object (Current_Scope), Loc),
6266 Attribute_Name => Name_Unchecked_Access)));
6268 Insert_Before (N, Call);
6269 Analyze (Call);
6270 end if;
6271 end Expand_Non_Function_Return;
6273 ---------------------------------------
6274 -- Expand_Protected_Object_Reference --
6275 ---------------------------------------
6277 function Expand_Protected_Object_Reference
6278 (N : Node_Id;
6279 Scop : Entity_Id) return Node_Id
6281 Loc : constant Source_Ptr := Sloc (N);
6282 Corr : Entity_Id;
6283 Rec : Node_Id;
6284 Param : Entity_Id;
6285 Proc : Entity_Id;
6287 begin
6288 Rec := Make_Identifier (Loc, Name_uObject);
6289 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6291 -- Find enclosing protected operation, and retrieve its first parameter,
6292 -- which denotes the enclosing protected object. If the enclosing
6293 -- operation is an entry, we are immediately within the protected body,
6294 -- and we can retrieve the object from the service entries procedure. A
6295 -- barrier function has the same signature as an entry. A barrier
6296 -- function is compiled within the protected object, but unlike
6297 -- protected operations its never needs locks, so that its protected
6298 -- body subprogram points to itself.
6300 Proc := Current_Scope;
6301 while Present (Proc) and then Scope (Proc) /= Scop loop
6302 Proc := Scope (Proc);
6303 if Is_Subprogram (Proc)
6304 and then Present (Protected_Subprogram (Proc))
6305 then
6306 Proc := Protected_Subprogram (Proc);
6307 end if;
6308 end loop;
6310 Corr := Protected_Body_Subprogram (Proc);
6312 if No (Corr) then
6314 -- Previous error left expansion incomplete.
6315 -- Nothing to do on this call.
6317 return Empty;
6318 end if;
6320 Param :=
6321 Defining_Identifier
6322 (First (Parameter_Specifications (Parent (Corr))));
6324 if Is_Subprogram (Proc) and then Proc /= Corr then
6326 -- Protected function or procedure
6328 Set_Entity (Rec, Param);
6330 -- Rec is a reference to an entity which will not be in scope when
6331 -- the call is reanalyzed, and needs no further analysis.
6333 Set_Analyzed (Rec);
6335 else
6336 -- Entry or barrier function for entry body. The first parameter of
6337 -- the entry body procedure is pointer to the object. We create a
6338 -- local variable of the proper type, duplicating what is done to
6339 -- define _object later on.
6341 declare
6342 Decls : List_Id;
6343 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6345 begin
6346 Decls := New_List (
6347 Make_Full_Type_Declaration (Loc,
6348 Defining_Identifier => Obj_Ptr,
6349 Type_Definition =>
6350 Make_Access_To_Object_Definition (Loc,
6351 Subtype_Indication =>
6352 New_Occurrence_Of
6353 (Corresponding_Record_Type (Scop), Loc))));
6355 Insert_Actions (N, Decls);
6356 Freeze_Before (N, Obj_Ptr);
6358 Rec :=
6359 Make_Explicit_Dereference (Loc,
6360 Prefix =>
6361 Unchecked_Convert_To (Obj_Ptr,
6362 New_Occurrence_Of (Param, Loc)));
6364 -- Analyze new actual. Other actuals in calls are already analyzed
6365 -- and the list of actuals is not reanalyzed after rewriting.
6367 Set_Parent (Rec, N);
6368 Analyze (Rec);
6369 end;
6370 end if;
6372 return Rec;
6373 end Expand_Protected_Object_Reference;
6375 --------------------------------------
6376 -- Expand_Protected_Subprogram_Call --
6377 --------------------------------------
6379 procedure Expand_Protected_Subprogram_Call
6380 (N : Node_Id;
6381 Subp : Entity_Id;
6382 Scop : Entity_Id)
6384 Rec : Node_Id;
6386 procedure Expand_Internal_Init_Call;
6387 -- A call to an operation of the type may occur in the initialization
6388 -- of a private component. In that case the prefix of the call is an
6389 -- entity name and the call is treated as internal even though it
6390 -- appears in code outside of the protected type.
6392 procedure Freeze_Called_Function;
6393 -- If it is a function call it can appear in elaboration code and
6394 -- the called entity must be frozen before the call. This must be
6395 -- done before the call is expanded, as the expansion may rewrite it
6396 -- to something other than a call (e.g. a temporary initialized in a
6397 -- transient block).
6399 -------------------------------
6400 -- Expand_Internal_Init_Call --
6401 -------------------------------
6403 procedure Expand_Internal_Init_Call is
6404 begin
6405 -- If the context is a protected object (rather than a protected
6406 -- type) the call itself is bound to raise program_error because
6407 -- the protected body will not have been elaborated yet. This is
6408 -- diagnosed subsequently in Sem_Elab.
6410 Freeze_Called_Function;
6412 -- The target of the internal call is the first formal of the
6413 -- enclosing initialization procedure.
6415 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6416 Build_Protected_Subprogram_Call (N,
6417 Name => Name (N),
6418 Rec => Rec,
6419 External => False);
6420 Analyze (N);
6421 Resolve (N, Etype (Subp));
6422 end Expand_Internal_Init_Call;
6424 ----------------------------
6425 -- Freeze_Called_Function --
6426 ----------------------------
6428 procedure Freeze_Called_Function is
6429 begin
6430 if Ekind (Subp) = E_Function then
6431 Freeze_Expression (Name (N));
6432 end if;
6433 end Freeze_Called_Function;
6435 -- Start of processing for Expand_Protected_Subprogram_Call
6437 begin
6438 -- If the protected object is not an enclosing scope, this is an inter-
6439 -- object function call. Inter-object procedure calls are expanded by
6440 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6441 -- subprogram being called is in the protected body being compiled, and
6442 -- if the protected object in the call is statically the enclosing type.
6443 -- The object may be a component of some other data structure, in which
6444 -- case this must be handled as an inter-object call.
6446 if not Scope_Within_Or_Same (Inner => Current_Scope, Outer => Scop)
6447 or else Is_Entry_Wrapper (Current_Scope)
6448 or else not Is_Entity_Name (Name (N))
6449 then
6450 if Nkind (Name (N)) = N_Selected_Component then
6451 Rec := Prefix (Name (N));
6453 elsif Nkind (Name (N)) = N_Indexed_Component then
6454 Rec := Prefix (Prefix (Name (N)));
6456 -- If this is a call within an entry wrapper, it appears within a
6457 -- precondition that calls another primitive of the synchronized
6458 -- type. The target object of the call is the first actual on the
6459 -- wrapper. Note that this is an external call, because the wrapper
6460 -- is called outside of the synchronized object. This means that
6461 -- an entry call to an entry with preconditions involves two
6462 -- synchronized operations.
6464 elsif Ekind (Current_Scope) = E_Procedure
6465 and then Is_Entry_Wrapper (Current_Scope)
6466 then
6467 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6469 -- A default parameter of a protected operation may be a call to
6470 -- a protected function of the type. This appears as an internal
6471 -- call in the profile of the operation, but if the context is an
6472 -- external call we must convert the call into an external one,
6473 -- using the protected object that is the target, so that:
6475 -- Prot.P (F)
6476 -- is transformed into
6477 -- Prot.P (Prot.F)
6479 elsif Nkind (Parent (N)) = N_Procedure_Call_Statement
6480 and then Nkind (Name (Parent (N))) = N_Selected_Component
6481 and then Is_Protected_Type (Etype (Prefix (Name (Parent (N)))))
6482 and then Is_Entity_Name (Name (N))
6483 and then Scope (Entity (Name (N))) =
6484 Etype (Prefix (Name (Parent (N))))
6485 then
6486 Rewrite (Name (N),
6487 Make_Selected_Component (Sloc (N),
6488 Prefix => New_Copy_Tree (Prefix (Name (Parent (N)))),
6489 Selector_Name => Relocate_Node (Name (N))));
6491 Analyze_And_Resolve (N);
6492 return;
6494 else
6495 -- If the context is the initialization procedure for a protected
6496 -- type, the call is legal because the called entity must be a
6497 -- function of that enclosing type, and this is treated as an
6498 -- internal call.
6500 pragma Assert
6501 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6503 Expand_Internal_Init_Call;
6504 return;
6505 end if;
6507 Freeze_Called_Function;
6508 Build_Protected_Subprogram_Call (N,
6509 Name => New_Occurrence_Of (Subp, Sloc (N)),
6510 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6511 External => True);
6513 else
6514 Rec := Expand_Protected_Object_Reference (N, Scop);
6516 if No (Rec) then
6517 return;
6518 end if;
6520 Freeze_Called_Function;
6521 Build_Protected_Subprogram_Call (N,
6522 Name => Name (N),
6523 Rec => Rec,
6524 External => False);
6525 end if;
6527 -- Analyze and resolve the new call. The actuals have already been
6528 -- resolved, but expansion of a function call will add extra actuals
6529 -- if needed. Analysis of a procedure call already includes resolution.
6531 Analyze (N);
6533 if Ekind (Subp) = E_Function then
6534 Resolve (N, Etype (Subp));
6535 end if;
6536 end Expand_Protected_Subprogram_Call;
6538 -----------------------------------
6539 -- Expand_Simple_Function_Return --
6540 -----------------------------------
6542 -- The "simple" comes from the syntax rule simple_return_statement. The
6543 -- semantics are not at all simple.
6545 procedure Expand_Simple_Function_Return (N : Node_Id) is
6546 Loc : constant Source_Ptr := Sloc (N);
6548 Scope_Id : constant Entity_Id :=
6549 Return_Applies_To (Return_Statement_Entity (N));
6550 -- The function we are returning from
6552 R_Type : constant Entity_Id := Etype (Scope_Id);
6553 -- The result type of the function
6555 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6556 -- The underlying result type of the function
6558 Exp : Node_Id := Expression (N);
6559 pragma Assert (Present (Exp));
6561 Exp_Is_Function_Call : constant Boolean :=
6562 Nkind (Exp) = N_Function_Call
6563 or else
6564 (Is_Captured_Function_Call (Exp)
6565 and then Is_Related_To_Func_Return (Entity (Prefix (Exp))));
6566 -- If the expression is a captured function call, then we need to make
6567 -- sure that the object doing the capture is properly recognized by the
6568 -- Is_Related_To_Func_Return predicate; otherwise, if it is of a type
6569 -- that needs finalization, Requires_Cleanup_Actions would return true
6570 -- because of this and Build_Finalizer would finalize it prematurely.
6572 Exp_Typ : constant Entity_Id := Etype (Exp);
6573 -- The type of the expression (not necessarily the same as R_Type)
6575 Subtype_Ind : Node_Id;
6576 -- If the result type of the function is class-wide and the expression
6577 -- has a specific type, then we use the expression's type as the type of
6578 -- the return object. In cases where the expression is an aggregate that
6579 -- is built in place, this avoids the need for an expensive conversion
6580 -- of the return object to the specific type on assignments to the
6581 -- individual components.
6583 -- Start of processing for Expand_Simple_Function_Return
6585 begin
6586 if Is_Class_Wide_Type (R_Type)
6587 and then not Is_Class_Wide_Type (Exp_Typ)
6588 and then Nkind (Exp) /= N_Type_Conversion
6589 then
6590 Subtype_Ind := New_Occurrence_Of (Exp_Typ, Loc);
6591 else
6592 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6594 -- If the result type is class-wide and the expression is a view
6595 -- conversion, the conversion plays no role in the expansion because
6596 -- it does not modify the tag of the object. Remove the conversion
6597 -- altogether to prevent tag overwriting.
6599 if Is_Class_Wide_Type (R_Type)
6600 and then not Is_Class_Wide_Type (Exp_Typ)
6601 and then Nkind (Exp) = N_Type_Conversion
6602 then
6603 Exp := Expression (Exp);
6604 end if;
6605 end if;
6607 -- For the case of a simple return that does not come from an
6608 -- extended return, in the case of build-in-place, we rewrite
6609 -- "return <expression>;" to be:
6611 -- return _anon_ : <return_subtype> := <expression>
6613 -- The expansion produced by Expand_N_Extended_Return_Statement will
6614 -- contain simple return statements (for example, a block containing
6615 -- simple return of the return object), which brings us back here with
6616 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6617 -- checking for a simple return that does not come from an extended
6618 -- return is to avoid this infinite recursion.
6620 -- The reason for this design is that for Ada 2005 limited returns, we
6621 -- need to reify the return object, so we can build it "in place", and
6622 -- we need a block statement to hang finalization and tasking stuff.
6624 pragma Assert
6625 (Comes_From_Extended_Return_Statement (N)
6626 or else not Is_Build_In_Place_Function_Call (Exp)
6627 or else Has_BIP_Formals (Scope_Id));
6629 if not Comes_From_Extended_Return_Statement (N)
6630 and then Is_Build_In_Place_Function (Scope_Id)
6632 -- The functionality of interface thunks is simple and it is always
6633 -- handled by means of simple return statements. This leaves their
6634 -- expansion simple and clean.
6636 and then not Is_Thunk (Scope_Id)
6637 then
6638 declare
6639 Return_Object_Entity : constant Entity_Id :=
6640 Make_Temporary (Loc, 'R', Exp);
6642 Obj_Decl : constant Node_Id :=
6643 Make_Object_Declaration (Loc,
6644 Defining_Identifier => Return_Object_Entity,
6645 Object_Definition => Subtype_Ind,
6646 Expression => Exp);
6648 Ext : constant Node_Id :=
6649 Make_Extended_Return_Statement (Loc,
6650 Return_Object_Declarations => New_List (Obj_Decl));
6651 -- Do not perform this high-level optimization if the result type
6652 -- is an interface because the "this" pointer must be displaced.
6654 begin
6655 Rewrite (N, Ext);
6656 Analyze (N);
6657 return;
6658 end;
6659 end if;
6661 -- Assert that if F says "return G(...);"
6662 -- then F and G are both b-i-p, or neither b-i-p.
6664 if Nkind (Exp) = N_Function_Call then
6665 pragma Assert (Ekind (Scope_Id) = E_Function);
6666 pragma Assert
6667 (Is_Build_In_Place_Function (Scope_Id) =
6668 Is_Build_In_Place_Function_Call (Exp));
6669 null;
6670 end if;
6672 -- Here we have a simple return statement that is part of the expansion
6673 -- of an extended return statement (either written by the user, or
6674 -- generated by the above code).
6676 -- Always normalize C/Fortran boolean result. This is not always needed,
6677 -- but it seems a good idea to minimize the passing around of non-
6678 -- normalized values, and in any case this handles the processing of
6679 -- barrier functions for protected types, which turn the condition into
6680 -- a return statement.
6682 if Is_Boolean_Type (Exp_Typ) and then Nonzero_Is_True (Exp_Typ) then
6683 Adjust_Condition (Exp);
6684 Adjust_Result_Type (Exp, Exp_Typ);
6686 -- The adjustment of the expression may have rewritten the return
6687 -- statement itself, e.g. when it is turned into an if expression.
6689 if Nkind (N) /= N_Simple_Return_Statement then
6690 return;
6691 end if;
6692 end if;
6694 -- Do validity check if enabled for returns
6696 if Validity_Checks_On and then Validity_Check_Returns then
6697 Ensure_Valid (Exp);
6698 end if;
6700 -- Check the result expression of a scalar function against the subtype
6701 -- of the function by inserting a conversion. This conversion must
6702 -- eventually be performed for other classes of types, but for now it's
6703 -- only done for scalars ???
6705 if Is_Scalar_Type (Exp_Typ) and then Exp_Typ /= R_Type then
6706 Rewrite (Exp, Convert_To (R_Type, Exp));
6708 -- The expression is resolved to ensure that the conversion gets
6709 -- expanded to generate a possible constraint check.
6711 Analyze_And_Resolve (Exp, R_Type);
6712 end if;
6714 -- Deal with returning variable length objects and controlled types
6716 -- Nothing to do if we are returning by reference
6718 if Is_Build_In_Place_Function (Scope_Id) then
6719 -- Prevent the reclamation of the secondary stack by all enclosing
6720 -- blocks and loops as well as the related function; otherwise the
6721 -- result would be reclaimed too early.
6723 if Needs_BIP_Alloc_Form (Scope_Id) then
6724 Set_Enclosing_Sec_Stack_Return (N);
6725 end if;
6727 elsif Is_Inherently_Limited_Type (R_Type) then
6728 null;
6730 -- No copy needed for thunks returning interface type objects since
6731 -- the object is returned by reference and the maximum functionality
6732 -- required is just to displace the pointer.
6734 elsif Is_Thunk (Scope_Id) and then Is_Interface (Exp_Typ) then
6735 null;
6737 -- If the call is within a thunk and the type is a limited view, the
6738 -- back end will eventually see the non-limited view of the type.
6740 elsif Is_Thunk (Scope_Id) and then Is_Incomplete_Type (Exp_Typ) then
6741 return;
6743 -- A return statement from an ignored Ghost function does not use the
6744 -- secondary stack (or any other one).
6746 elsif (not Needs_Secondary_Stack (R_Type)
6747 and then not Is_Secondary_Stack_Thunk (Scope_Id))
6748 or else Is_Ignored_Ghost_Entity (Scope_Id)
6749 then
6750 -- Mutable records with variable-length components are not returned
6751 -- on the sec-stack, so we need to make sure that the back end will
6752 -- only copy back the size of the actual value, and not the maximum
6753 -- size. We create an actual subtype for this purpose. However we
6754 -- need not do it if the expression is a function call since this
6755 -- will be done in the called function and doing it here too would
6756 -- cause a temporary with maximum size to be created. Likewise for
6757 -- a special return object, since there is no copy in this case.
6759 declare
6760 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exp_Typ));
6761 Decl : Node_Id;
6762 Ent : Entity_Id;
6764 begin
6765 if not Exp_Is_Function_Call
6766 and then not (Is_Entity_Name (Exp)
6767 and then Is_Special_Return_Object (Entity (Exp)))
6768 and then Has_Defaulted_Discriminants (Ubt)
6769 and then not Is_Constrained (Ubt)
6770 and then not Has_Unchecked_Union (Ubt)
6771 then
6772 Decl := Build_Actual_Subtype (Ubt, Exp);
6773 Ent := Defining_Identifier (Decl);
6774 Insert_Action (Exp, Decl);
6775 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6776 Analyze_And_Resolve (Exp);
6777 end if;
6778 end;
6780 -- For types which need finalization, do the allocation on the return
6781 -- stack manually in order to call Adjust at the right time:
6783 -- type Ann is access R_Type;
6784 -- for Ann'Storage_pool use rs_pool;
6785 -- Rnn : constant Ann := new Exp_Typ'(Exp);
6786 -- return Rnn.all;
6788 -- but optimize the case where the result is a function call that
6789 -- also needs finalization. In this case the result can directly be
6790 -- allocated on the return stack of the caller and no further
6791 -- processing is required. Likewise if this is a return object.
6793 if Comes_From_Extended_Return_Statement (N) then
6794 null;
6796 elsif Present (Utyp)
6797 and then Needs_Finalization (Utyp)
6798 and then not (Exp_Is_Function_Call
6799 and then Needs_Finalization (Exp_Typ))
6800 then
6801 declare
6802 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6804 Alloc_Node : Node_Id;
6805 Temp : Entity_Id;
6807 begin
6808 Mutate_Ekind (Acc_Typ, E_Access_Type);
6810 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_RS_Pool));
6812 -- This is an allocator for the return stack, and it's fine
6813 -- to have Comes_From_Source set False on it, as gigi knows not
6814 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6816 Alloc_Node :=
6817 Make_Allocator (Loc,
6818 Expression =>
6819 Make_Qualified_Expression (Loc,
6820 Subtype_Mark => New_Occurrence_Of (Exp_Typ, Loc),
6821 Expression => Relocate_Node (Exp)));
6823 -- We do not want discriminant checks on the declaration,
6824 -- given that it gets its value from the allocator.
6826 Set_No_Initialization (Alloc_Node);
6828 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6830 Insert_Actions (Exp, New_List (
6831 Make_Full_Type_Declaration (Loc,
6832 Defining_Identifier => Acc_Typ,
6833 Type_Definition =>
6834 Make_Access_To_Object_Definition (Loc,
6835 Subtype_Indication => Subtype_Ind)),
6837 Make_Object_Declaration (Loc,
6838 Defining_Identifier => Temp,
6839 Constant_Present => True,
6840 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6841 Expression => Alloc_Node)));
6843 Rewrite (Exp,
6844 Make_Explicit_Dereference (Loc,
6845 Prefix => New_Occurrence_Of (Temp, Loc)));
6847 Analyze_And_Resolve (Exp, R_Type);
6848 end;
6849 end if;
6851 -- Here if secondary stack is used
6853 else
6854 -- Prevent the reclamation of the secondary stack by all enclosing
6855 -- blocks and loops as well as the related function; otherwise the
6856 -- result would be reclaimed too early.
6858 Set_Enclosing_Sec_Stack_Return (N);
6860 -- Nothing else to do for a return object
6862 if Comes_From_Extended_Return_Statement (N) then
6863 null;
6865 -- Optimize the case where the result is a function call that also
6866 -- returns on the secondary stack; in this case the result is already
6867 -- on the secondary stack and no further processing is required.
6869 elsif Exp_Is_Function_Call
6870 and then Needs_Secondary_Stack (Exp_Typ)
6871 then
6872 -- Remove side effects from the expression now so that other parts
6873 -- of the expander do not have to reanalyze this node without this
6874 -- optimization
6876 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6878 -- Ada 2005 (AI-251): If the type of the returned object is
6879 -- an interface then add an implicit type conversion to force
6880 -- displacement of the "this" pointer.
6882 if Is_Interface (R_Type) then
6883 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6884 end if;
6886 Analyze_And_Resolve (Exp, R_Type);
6888 -- For types which both need finalization and are returned on the
6889 -- secondary stack, do the allocation on secondary stack manually
6890 -- in order to call Adjust at the right time:
6892 -- type Ann is access R_Type;
6893 -- for Ann'Storage_pool use ss_pool;
6894 -- Rnn : constant Ann := new Exp_Typ'(Exp);
6895 -- return Rnn.all;
6897 -- And we do the same for class-wide types that are not potentially
6898 -- controlled (by the virtue of restriction No_Finalization) because
6899 -- gigi is not able to properly allocate class-wide types.
6901 -- But optimize the case where the result is a function call that
6902 -- also needs finalization; in this case the result can directly be
6903 -- allocated on the secondary stack and no further processing is
6904 -- required, unless the returned object is an interface.
6906 elsif CW_Or_Needs_Finalization (Utyp)
6907 and then (Is_Interface (R_Type)
6908 or else not (Exp_Is_Function_Call
6909 and then Needs_Finalization (Exp_Typ)))
6910 then
6911 declare
6912 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6914 Alloc_Node : Node_Id;
6915 Temp : Entity_Id;
6917 begin
6918 Mutate_Ekind (Acc_Typ, E_Access_Type);
6919 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6921 -- This is an allocator for the secondary stack, and it's fine
6922 -- to have Comes_From_Source set False on it, as gigi knows not
6923 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6925 Alloc_Node :=
6926 Make_Allocator (Loc,
6927 Expression =>
6928 Make_Qualified_Expression (Loc,
6929 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6930 Expression => Relocate_Node (Exp)));
6932 -- We do not want discriminant checks on the declaration,
6933 -- given that it gets its value from the allocator.
6935 Set_No_Initialization (Alloc_Node);
6937 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6939 Insert_Actions (Exp, New_List (
6940 Make_Full_Type_Declaration (Loc,
6941 Defining_Identifier => Acc_Typ,
6942 Type_Definition =>
6943 Make_Access_To_Object_Definition (Loc,
6944 Subtype_Indication => Subtype_Ind)),
6946 Make_Object_Declaration (Loc,
6947 Defining_Identifier => Temp,
6948 Constant_Present => True,
6949 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6950 Expression => Alloc_Node)));
6952 Rewrite (Exp,
6953 Make_Explicit_Dereference (Loc,
6954 Prefix => New_Occurrence_Of (Temp, Loc)));
6956 -- Ada 2005 (AI-251): If the type of the returned object is
6957 -- an interface then add an implicit type conversion to force
6958 -- displacement of the "this" pointer.
6960 if Is_Interface (R_Type) then
6961 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6962 end if;
6964 Analyze_And_Resolve (Exp, R_Type);
6965 end;
6967 -- Otherwise use the gigi mechanism to allocate result on the
6968 -- secondary stack.
6970 else
6971 Check_Restriction (No_Secondary_Stack, N);
6972 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6973 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6974 end if;
6975 end if;
6977 -- Implement the rules of 6.5(8-10), which require a tag check in
6978 -- the case of a limited tagged return type, and tag reassignment for
6979 -- nonlimited tagged results. These actions are needed when the return
6980 -- type is a specific tagged type and the result expression is a
6981 -- conversion or a formal parameter, because in that case the tag of
6982 -- the expression might differ from the tag of the specific result type.
6984 -- We must also verify an underlying type exists for the return type in
6985 -- case it is incomplete - in which case is not necessary to generate a
6986 -- check anyway since an incomplete limited tagged return type would
6987 -- qualify as a premature usage.
6989 if Present (Utyp)
6990 and then Is_Tagged_Type (Utyp)
6991 and then not Is_Class_Wide_Type (Utyp)
6992 and then Is_Conversion_Or_Reference_To_Formal (Exp)
6993 then
6994 -- When the return type is limited, perform a check that the tag of
6995 -- the result is the same as the tag of the return type.
6997 if Is_Limited_Type (R_Type) then
6998 Insert_Action (Exp,
6999 Make_Raise_Constraint_Error (Loc,
7000 Condition =>
7001 Make_Op_Ne (Loc,
7002 Left_Opnd =>
7003 Make_Selected_Component (Loc,
7004 Prefix => Duplicate_Subexpr (Exp),
7005 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7006 Right_Opnd =>
7007 Make_Attribute_Reference (Loc,
7008 Prefix =>
7009 New_Occurrence_Of (Base_Type (Utyp), Loc),
7010 Attribute_Name => Name_Tag)),
7011 Reason => CE_Tag_Check_Failed));
7013 -- If the result type is a specific nonlimited tagged type, then we
7014 -- have to ensure that the tag of the result is that of the result
7015 -- type. This is handled by making a copy of the expression in
7016 -- the case where it might have a different tag, namely when the
7017 -- expression is a conversion or a formal parameter. We create a new
7018 -- object of the result type and initialize it from the expression,
7019 -- which will implicitly force the tag to be set appropriately.
7021 else
7022 declare
7023 ExpR : constant Node_Id := Relocate_Node (Exp);
7024 Result_Id : constant Entity_Id :=
7025 Make_Temporary (Loc, 'R', ExpR);
7026 Result_Exp : constant Node_Id :=
7027 New_Occurrence_Of (Result_Id, Loc);
7028 Result_Obj : constant Node_Id :=
7029 Make_Object_Declaration (Loc,
7030 Defining_Identifier => Result_Id,
7031 Object_Definition =>
7032 New_Occurrence_Of (R_Type, Loc),
7033 Constant_Present => True,
7034 Expression => ExpR);
7036 begin
7037 Set_Assignment_OK (Result_Obj);
7038 Insert_Action (Exp, Result_Obj);
7040 Rewrite (Exp, Result_Exp);
7041 Analyze_And_Resolve (Exp, R_Type);
7042 end;
7043 end if;
7045 -- Ada 2005 (AI95-344): If the result type is class-wide, then insert
7046 -- a check that the level of the return expression's underlying type
7047 -- is not deeper than the level of the master enclosing the function.
7049 -- AI12-043: The check is made immediately after the return object is
7050 -- created. This means that we do not apply it to the simple return
7051 -- generated by the expansion of an extended return statement.
7053 -- No runtime check needed in interface thunks since it is performed
7054 -- by the target primitive associated with the thunk.
7056 elsif Is_Class_Wide_Type (R_Type)
7057 and then not Comes_From_Extended_Return_Statement (N)
7058 and then not Is_Thunk (Scope_Id)
7059 then
7060 Apply_CW_Accessibility_Check (Exp, Scope_Id);
7062 -- Ada 2012 (AI05-0073): If the result subtype of the function is
7063 -- defined by an access_definition designating a specific tagged
7064 -- type T, a check is made that the result value is null or the tag
7065 -- of the object designated by the result value identifies T.
7067 -- The return expression is referenced twice in the code below, so it
7068 -- must be made free of side effects. Given that different compilers
7069 -- may evaluate these parameters in different order, both occurrences
7070 -- perform a copy.
7072 elsif Ekind (R_Type) = E_Anonymous_Access_Type
7073 and then Is_Tagged_Type (Designated_Type (R_Type))
7074 and then not Is_Class_Wide_Type (Designated_Type (R_Type))
7075 and then Nkind (Original_Node (Exp)) /= N_Null
7076 and then not Tag_Checks_Suppressed (Designated_Type (R_Type))
7077 then
7078 -- Generate:
7079 -- [Constraint_Error
7080 -- when Exp /= null
7081 -- and then Exp.all not in Designated_Type]
7083 Insert_Action (N,
7084 Make_Raise_Constraint_Error (Loc,
7085 Condition =>
7086 Make_And_Then (Loc,
7087 Left_Opnd =>
7088 Make_Op_Ne (Loc,
7089 Left_Opnd => Duplicate_Subexpr (Exp),
7090 Right_Opnd => Make_Null (Loc)),
7092 Right_Opnd =>
7093 Make_Not_In (Loc,
7094 Left_Opnd =>
7095 Make_Explicit_Dereference (Loc,
7096 Prefix => Duplicate_Subexpr (Exp)),
7097 Right_Opnd =>
7098 New_Occurrence_Of (Designated_Type (R_Type), Loc))),
7100 Reason => CE_Tag_Check_Failed),
7101 Suppress => All_Checks);
7102 end if;
7104 -- If the result is of an unconstrained array subtype with fixed lower
7105 -- bound, then sliding to that bound may be needed.
7107 if Is_Fixed_Lower_Bound_Array_Subtype (R_Type) then
7108 Expand_Sliding_Conversion (Exp, R_Type);
7109 end if;
7111 -- If we are returning a nonscalar object that is possibly unaligned,
7112 -- then copy the value into a temporary first. This copy may need to
7113 -- expand to a loop of component operations.
7115 if Is_Possibly_Unaligned_Slice (Exp)
7116 or else (Is_Possibly_Unaligned_Object (Exp)
7117 and then not Represented_As_Scalar (Etype (Exp)))
7118 then
7119 declare
7120 ExpR : constant Node_Id := Relocate_Node (Exp);
7121 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7122 begin
7123 Insert_Action (Exp,
7124 Make_Object_Declaration (Loc,
7125 Defining_Identifier => Tnn,
7126 Constant_Present => True,
7127 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7128 Expression => ExpR),
7129 Suppress => All_Checks);
7130 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7131 end;
7132 end if;
7134 -- Ada 2005 (AI-251): If this return statement corresponds with an
7135 -- simple return statement associated with an extended return statement
7136 -- and the type of the returned object is an interface then generate an
7137 -- implicit conversion to force displacement of the "this" pointer.
7139 if Ada_Version >= Ada_2005
7140 and then Comes_From_Extended_Return_Statement (N)
7141 and then Nkind (Expression (N)) = N_Identifier
7142 and then Is_Interface (Utyp)
7143 and then Utyp /= Underlying_Type (Exp_Typ)
7144 then
7145 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7146 Analyze_And_Resolve (Exp);
7147 end if;
7149 -- Ada 2022 (AI12-0279)
7151 if Has_Yield_Aspect (Scope_Id)
7152 and then RTE_Available (RE_Yield)
7153 then
7154 Insert_Action (N,
7155 Make_Procedure_Call_Statement (Loc,
7156 New_Occurrence_Of (RTE (RE_Yield), Loc)));
7157 end if;
7158 end Expand_Simple_Function_Return;
7160 -----------------------
7161 -- Freeze_Subprogram --
7162 -----------------------
7164 procedure Freeze_Subprogram (N : Node_Id) is
7165 Loc : constant Source_Ptr := Sloc (N);
7166 Subp : constant Entity_Id := Entity (N);
7168 begin
7169 -- We suppress the initialization of the dispatch table entry when
7170 -- not Tagged_Type_Expansion because the dispatching mechanism is
7171 -- handled internally by the target.
7173 if Is_Dispatching_Operation (Subp)
7174 and then not Is_Abstract_Subprogram (Subp)
7175 and then Present (DTC_Entity (Subp))
7176 and then Present (Scope (DTC_Entity (Subp)))
7177 and then Tagged_Type_Expansion
7178 and then not Restriction_Active (No_Dispatching_Calls)
7179 and then RTE_Available (RE_Tag)
7180 then
7181 declare
7182 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7184 L : List_Id;
7186 begin
7187 -- Handle private overridden primitives
7189 if not Is_CPP_Class (Typ) then
7190 Check_Overriding_Operation (Subp);
7191 end if;
7193 -- We assume that imported CPP primitives correspond with objects
7194 -- whose constructor is in the CPP side; therefore we don't need
7195 -- to generate code to register them in the dispatch table.
7197 if Is_CPP_Class (Typ) then
7198 null;
7200 -- Handle CPP primitives found in derivations of CPP_Class types.
7201 -- These primitives must have been inherited from some parent, and
7202 -- there is no need to register them in the dispatch table because
7203 -- Build_Inherit_Prims takes care of initializing these slots.
7205 elsif Is_Imported (Subp)
7206 and then Convention (Subp) in Convention_C_Family
7207 then
7208 null;
7210 -- Generate code to register the primitive in non statically
7211 -- allocated dispatch tables
7213 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7215 -- When a primitive is frozen, enter its name in its dispatch
7216 -- table slot.
7218 if not Is_Interface (Typ)
7219 or else Present (Interface_Alias (Subp))
7220 then
7221 if Is_Predefined_Dispatching_Operation (Subp) then
7222 L := Register_Predefined_Primitive (Loc, Subp);
7223 else
7224 L := New_List;
7225 end if;
7227 Append_List_To (L, Register_Primitive (Loc, Subp));
7229 if Is_Empty_List (L) then
7230 null;
7232 elsif No (Actions (N)) then
7233 Set_Actions (N, L);
7235 else
7236 Append_List (L, Actions (N));
7237 end if;
7238 end if;
7239 end if;
7240 end;
7241 end if;
7243 -- Mark functions that return by reference. Note that it cannot be part
7244 -- of the normal semantic analysis of the spec since the underlying
7245 -- returned type may not be known yet (for private types).
7247 Compute_Returns_By_Ref (Subp);
7248 end Freeze_Subprogram;
7250 --------------------------
7251 -- Has_BIP_Extra_Formal --
7252 --------------------------
7254 function Has_BIP_Extra_Formal
7255 (E : Entity_Id;
7256 Kind : BIP_Formal_Kind;
7257 Must_Be_Frozen : Boolean := True) return Boolean
7259 Extra_Formal : Entity_Id := Extra_Formals (E);
7261 begin
7262 -- We can only rely on the availability of the extra formals in frozen
7263 -- entities or in subprogram types of dispatching calls (since their
7264 -- extra formals are added when the target subprogram is frozen; see
7265 -- Expand_Dispatching_Call).
7267 pragma Assert ((Is_Frozen (E) or else not Must_Be_Frozen)
7268 or else (Ekind (E) = E_Subprogram_Type
7269 and then Is_Dispatch_Table_Entity (E))
7270 or else (Is_Dispatching_Operation (E)
7271 and then Is_Frozen (Find_Dispatching_Type (E))));
7273 while Present (Extra_Formal) loop
7274 if Is_Build_In_Place_Entity (Extra_Formal)
7275 and then BIP_Suffix_Kind (Extra_Formal) = Kind
7276 then
7277 return True;
7278 end if;
7280 Next_Formal_With_Extras (Extra_Formal);
7281 end loop;
7283 return False;
7284 end Has_BIP_Extra_Formal;
7286 ------------------------------
7287 -- Insert_Post_Call_Actions --
7288 ------------------------------
7290 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is
7291 Context : constant Node_Id := Parent (N);
7293 begin
7294 if Is_Empty_List (Post_Call) then
7295 return;
7296 end if;
7298 -- Cases where the call is not a member of a statement list. This also
7299 -- includes the cases where the call is an actual in another function
7300 -- call, or is an index, or is an operand of an if-expression, i.e. is
7301 -- in an expression context.
7303 if not Is_List_Member (N)
7304 or else Nkind (Context) in N_Function_Call
7305 | N_If_Expression
7306 | N_Indexed_Component
7307 then
7308 -- In Ada 2012 the call may be a function call in an expression
7309 -- (since OUT and IN OUT parameters are now allowed for such calls).
7310 -- The write-back of (in)-out parameters is handled by the back-end,
7311 -- but the constraint checks generated when subtypes of formal and
7312 -- actual don't match must be inserted in the form of assignments.
7313 -- Also do this in the case of explicit dereferences, which can occur
7314 -- due to rewritings of function calls with controlled results.
7316 if Nkind (N) = N_Function_Call
7317 or else Nkind (Original_Node (N)) = N_Function_Call
7318 or else Nkind (N) = N_Explicit_Dereference
7319 then
7320 pragma Assert (Ada_Version >= Ada_2012);
7321 -- Functions with '[in] out' parameters are only allowed in Ada
7322 -- 2012.
7324 -- We used to handle this by climbing up parents to a
7325 -- non-statement/declaration and then simply making a call to
7326 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7327 -- for Ada 2012. If we are in the middle of an expression, e.g.
7328 -- the condition of an IF, this call would insert after the IF
7329 -- statement, which is much too late to be doing the write back.
7330 -- For example:
7332 -- if Clobber (X) then
7333 -- Put_Line (X'Img);
7334 -- else
7335 -- goto Junk
7336 -- end if;
7338 -- Now assume Clobber changes X, if we put the write back after
7339 -- the IF, the Put_Line gets the wrong value and the goto causes
7340 -- the write back to be skipped completely.
7342 -- To deal with this, we replace the call by
7344 -- do
7345 -- Tnnn : constant function-result-type := function-call;
7346 -- Post_Call actions
7347 -- in
7348 -- Tnnn;
7349 -- end;
7351 -- However, that doesn't work if function-result-type requires
7352 -- finalization (because function-call's result never gets
7353 -- finalized). So in that case, we instead replace the call by
7355 -- do
7356 -- type Ref is access all function-result-type;
7357 -- Ptr : constant Ref := function-call'Reference;
7358 -- Tnnn : constant function-result-type := Ptr.all;
7359 -- Finalize (Ptr.all);
7360 -- Post_Call actions
7361 -- in
7362 -- Tnnn;
7363 -- end;
7366 declare
7367 Loc : constant Source_Ptr := Sloc (N);
7368 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
7369 FRTyp : constant Entity_Id := Etype (N);
7370 Name : constant Node_Id := Relocate_Node (N);
7372 begin
7373 if Needs_Finalization (FRTyp) then
7374 declare
7375 Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7377 Ptr_Typ_Decl : constant Node_Id :=
7378 Make_Full_Type_Declaration (Loc,
7379 Defining_Identifier => Ptr_Typ,
7380 Type_Definition =>
7381 Make_Access_To_Object_Definition (Loc,
7382 All_Present => True,
7383 Subtype_Indication =>
7384 New_Occurrence_Of (FRTyp, Loc)));
7386 Ptr_Obj : constant Entity_Id :=
7387 Make_Temporary (Loc, 'P');
7389 Ptr_Obj_Decl : constant Node_Id :=
7390 Make_Object_Declaration (Loc,
7391 Defining_Identifier => Ptr_Obj,
7392 Object_Definition =>
7393 New_Occurrence_Of (Ptr_Typ, Loc),
7394 Constant_Present => True,
7395 Expression =>
7396 Make_Attribute_Reference (Loc,
7397 Prefix => Name,
7398 Attribute_Name => Name_Unrestricted_Access));
7400 function Ptr_Dereference return Node_Id is
7401 (Make_Explicit_Dereference (Loc,
7402 Prefix => New_Occurrence_Of (Ptr_Obj, Loc)));
7404 Tnn_Decl : constant Node_Id :=
7405 Make_Object_Declaration (Loc,
7406 Defining_Identifier => Tnnn,
7407 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
7408 Constant_Present => True,
7409 Expression => Ptr_Dereference);
7411 Finalize_Call : constant Node_Id :=
7412 Make_Final_Call
7413 (Obj_Ref => Ptr_Dereference, Typ => FRTyp);
7414 begin
7415 -- Prepend in reverse order
7417 Prepend_To (Post_Call, Finalize_Call);
7418 Prepend_To (Post_Call, Tnn_Decl);
7419 Prepend_To (Post_Call, Ptr_Obj_Decl);
7420 Prepend_To (Post_Call, Ptr_Typ_Decl);
7421 end;
7422 else
7423 Prepend_To (Post_Call,
7424 Make_Object_Declaration (Loc,
7425 Defining_Identifier => Tnnn,
7426 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
7427 Constant_Present => True,
7428 Expression => Name));
7429 end if;
7431 Rewrite (N,
7432 Make_Expression_With_Actions (Loc,
7433 Actions => Post_Call,
7434 Expression => New_Occurrence_Of (Tnnn, Loc)));
7436 -- We don't want to just blindly call Analyze_And_Resolve
7437 -- because that would cause unwanted recursion on the call.
7438 -- So for a moment set the call as analyzed to prevent that
7439 -- recursion, and get the rest analyzed properly, then reset
7440 -- the analyzed flag, so our caller can continue.
7442 Set_Analyzed (Name, True);
7443 Analyze_And_Resolve (N, FRTyp);
7444 Set_Analyzed (Name, False);
7445 end;
7447 -- If not the special Ada 2012 case of a function call, then we must
7448 -- have the triggering statement of a triggering alternative or an
7449 -- entry call alternative, and we can add the post call stuff to the
7450 -- corresponding statement list.
7452 else
7453 pragma Assert (Nkind (Context) in N_Entry_Call_Alternative
7454 | N_Triggering_Alternative);
7456 if Is_Non_Empty_List (Statements (Context)) then
7457 Insert_List_Before_And_Analyze
7458 (First (Statements (Context)), Post_Call);
7459 else
7460 Set_Statements (Context, Post_Call);
7461 end if;
7462 end if;
7464 -- A procedure call is always part of a declarative or statement list,
7465 -- however a function call may appear nested within a construct. Most
7466 -- cases of function call nesting are handled in the special case above.
7467 -- The only exception is when the function call acts as an actual in a
7468 -- procedure call. In this case the function call is in a list, but the
7469 -- post-call actions must be inserted after the procedure call.
7470 -- What if the function call is an aggregate component ???
7472 elsif Nkind (Context) = N_Procedure_Call_Statement then
7473 Insert_Actions_After (Context, Post_Call);
7475 -- Otherwise, normal case where N is in a statement sequence, just put
7476 -- the post-call stuff after the call statement.
7478 else
7479 Insert_Actions_After (N, Post_Call);
7480 end if;
7481 end Insert_Post_Call_Actions;
7483 ---------------------------------------
7484 -- Install_Class_Preconditions_Check --
7485 ---------------------------------------
7487 procedure Install_Class_Preconditions_Check (Call_Node : Node_Id) is
7488 Loc : constant Source_Ptr := Sloc (Call_Node);
7490 function Build_Dynamic_Check_Helper_Call return Node_Id;
7491 -- Build call to the helper runtime function of the nearest ancestor
7492 -- of the target subprogram that dynamically evaluates the merged
7493 -- or-else preconditions.
7495 function Build_Error_Message (Subp_Id : Entity_Id) return Node_Id;
7496 -- Build message associated with the class-wide precondition of Subp_Id
7497 -- indicating the call that caused it.
7499 function Build_Static_Check_Helper_Call return Node_Id;
7500 -- Build call to the helper runtime function of the nearest ancestor
7501 -- of the target subprogram that dynamically evaluates the merged
7502 -- or-else preconditions.
7504 function Class_Preconditions_Subprogram
7505 (Spec_Id : Entity_Id;
7506 Dynamic : Boolean) return Node_Id;
7507 -- Return the nearest ancestor of Spec_Id defining a helper function
7508 -- that evaluates a combined or-else expression containing all the
7509 -- inherited class-wide preconditions; Dynamic enables searching for
7510 -- the helper that dynamically evaluates preconditions using dispatching
7511 -- calls; if False it searches for the helper that statically evaluates
7512 -- preconditions; return Empty when not available (which means that no
7513 -- preconditions check is required).
7515 -------------------------------------
7516 -- Build_Dynamic_Check_Helper_Call --
7517 -------------------------------------
7519 function Build_Dynamic_Check_Helper_Call return Node_Id is
7520 Spec_Id : constant Entity_Id := Entity (Name (Call_Node));
7521 CW_Subp : constant Entity_Id :=
7522 Class_Preconditions_Subprogram (Spec_Id,
7523 Dynamic => True);
7524 Helper_Id : constant Entity_Id :=
7525 Dynamic_Call_Helper (CW_Subp);
7526 Actuals : constant List_Id := New_List;
7527 A : Node_Id := First_Actual (Call_Node);
7529 begin
7530 while Present (A) loop
7532 -- Ensure that the evaluation of the actuals will not produce
7533 -- side effects.
7535 Remove_Side_Effects (A);
7537 Append_To (Actuals, New_Copy_Tree (A));
7539 Next_Actual (A);
7540 end loop;
7542 return
7543 Make_Function_Call (Loc,
7544 Name => New_Occurrence_Of (Helper_Id, Loc),
7545 Parameter_Associations => Actuals);
7546 end Build_Dynamic_Check_Helper_Call;
7548 -------------------------
7549 -- Build_Error_Message --
7550 -------------------------
7552 function Build_Error_Message (Subp_Id : Entity_Id) return Node_Id is
7554 procedure Append_Message
7555 (Id : Entity_Id;
7556 Is_First : in out Boolean);
7557 -- Build the fragment of the message associated with subprogram Id;
7558 -- Is_First facilitates identifying continuation messages.
7560 --------------------
7561 -- Append_Message --
7562 --------------------
7564 procedure Append_Message
7565 (Id : Entity_Id;
7566 Is_First : in out Boolean)
7568 Prag : constant Node_Id :=
7569 Get_Class_Wide_Pragma (Id, Pragma_Precondition);
7571 begin
7572 if No (Prag) or else Is_Ignored (Prag) then
7573 return;
7574 end if;
7576 if Is_First then
7577 Is_First := False;
7579 if Id /= Subp_Id then
7580 Append
7581 (Global_Name_Buffer, "failed inherited precondition ");
7582 else
7583 Append (Global_Name_Buffer, "failed precondition ");
7584 end if;
7586 else
7587 Append (Global_Name_Buffer, ASCII.LF);
7588 Append (Global_Name_Buffer, " or ");
7590 Append (Global_Name_Buffer, "failed inherited precondition ");
7591 end if;
7593 Append (Global_Name_Buffer, "from " &
7594 Build_Location_String
7595 (Sloc
7596 (First_Node
7597 (Expression
7598 (First (Pragma_Argument_Associations (Prag)))))));
7599 end Append_Message;
7601 -- Local variables
7603 Str_Loc : constant String := Build_Location_String (Loc);
7604 Subps : constant Subprogram_List :=
7605 Inherited_Subprograms (Subp_Id);
7606 Is_First : Boolean := True;
7608 -- Start of processing for Build_Error_Message
7610 begin
7611 Name_Len := 0;
7612 Append_Message (Subp_Id, Is_First);
7614 for Index in Subps'Range loop
7615 Append_Message (Subps (Index), Is_First);
7616 end loop;
7618 if Present (Controlling_Argument (Call_Node)) then
7619 Append (Global_Name_Buffer, " in dispatching call at ");
7620 else
7621 Append (Global_Name_Buffer, " in call at ");
7622 end if;
7624 Append (Global_Name_Buffer, Str_Loc);
7626 return Make_String_Literal (Loc, Name_Buffer (1 .. Name_Len));
7627 end Build_Error_Message;
7629 ------------------------------------
7630 -- Build_Static_Check_Helper_Call --
7631 ------------------------------------
7633 function Build_Static_Check_Helper_Call return Node_Id is
7634 Actuals : constant List_Id := New_List;
7635 A : Node_Id;
7636 Helper_Id : Entity_Id;
7637 F : Entity_Id;
7638 CW_Subp : Entity_Id;
7639 Spec_Id : constant Entity_Id := Entity (Name (Call_Node));
7641 begin
7642 -- The target is the wrapper built to support inheriting body but
7643 -- overriding pre/postconditions (AI12-0195).
7645 if Is_Dispatch_Table_Wrapper (Spec_Id) then
7646 CW_Subp := Spec_Id;
7648 -- Common case
7650 else
7651 CW_Subp := Class_Preconditions_Subprogram (Spec_Id,
7652 Dynamic => False);
7653 end if;
7655 Helper_Id := Static_Call_Helper (CW_Subp);
7657 F := First_Formal (Helper_Id);
7658 A := First_Actual (Call_Node);
7659 while Present (A) loop
7661 -- Ensure that the evaluation of the actuals will not produce
7662 -- side effects.
7664 Remove_Side_Effects (A);
7666 -- Ensure matching types to avoid reporting spurious errors since
7667 -- the called helper may have been built for a parent type.
7669 if Etype (F) /= Etype (A) then
7670 Append_To (Actuals,
7671 Unchecked_Convert_To (Etype (F), New_Copy_Tree (A)));
7672 else
7673 Append_To (Actuals, New_Copy_Tree (A));
7674 end if;
7676 Next_Formal (F);
7677 Next_Actual (A);
7678 end loop;
7680 return
7681 Make_Function_Call (Loc,
7682 Name => New_Occurrence_Of (Helper_Id, Loc),
7683 Parameter_Associations => Actuals);
7684 end Build_Static_Check_Helper_Call;
7686 ------------------------------------
7687 -- Class_Preconditions_Subprogram --
7688 ------------------------------------
7690 function Class_Preconditions_Subprogram
7691 (Spec_Id : Entity_Id;
7692 Dynamic : Boolean) return Node_Id
7694 Subp_Id : constant Entity_Id := Ultimate_Alias (Spec_Id);
7696 begin
7697 -- Prevent cascaded errors
7699 if not Is_Dispatching_Operation (Subp_Id) then
7700 return Empty;
7702 -- No need to search if this subprogram has the helper we are
7703 -- searching
7705 elsif Dynamic then
7706 if Present (Dynamic_Call_Helper (Subp_Id)) then
7707 return Subp_Id;
7708 end if;
7709 else
7710 if Present (Static_Call_Helper (Subp_Id)) then
7711 return Subp_Id;
7712 end if;
7713 end if;
7715 -- Process inherited subprograms looking for class-wide
7716 -- preconditions.
7718 declare
7719 Subps : constant Subprogram_List :=
7720 Inherited_Subprograms (Subp_Id);
7721 Subp_Id : Entity_Id;
7723 begin
7724 for Index in Subps'Range loop
7725 Subp_Id := Subps (Index);
7727 if Present (Alias (Subp_Id)) then
7728 Subp_Id := Ultimate_Alias (Subp_Id);
7729 end if;
7731 -- Wrappers of class-wide pre/postconditions reference the
7732 -- parent primitive that has the inherited contract.
7734 if Is_Wrapper (Subp_Id)
7735 and then Present (LSP_Subprogram (Subp_Id))
7736 then
7737 Subp_Id := LSP_Subprogram (Subp_Id);
7738 end if;
7740 if Dynamic then
7741 if Present (Dynamic_Call_Helper (Subp_Id)) then
7742 return Subp_Id;
7743 end if;
7744 else
7745 if Present (Static_Call_Helper (Subp_Id)) then
7746 return Subp_Id;
7747 end if;
7748 end if;
7749 end loop;
7750 end;
7752 return Empty;
7753 end Class_Preconditions_Subprogram;
7755 -- Local variables
7757 Dynamic_Check : constant Boolean :=
7758 Present (Controlling_Argument (Call_Node));
7759 Class_Subp : Entity_Id;
7760 Cond : Node_Id;
7761 Fail : Node_Id;
7762 Subp : Entity_Id;
7764 -- Start of processing for Install_Class_Preconditions_Check
7766 begin
7767 -- Do not expand the check if we are compiling under restriction
7768 -- No_Dispatching_Calls; the semantic analyzer has previously
7769 -- notified the violation of this restriction.
7771 if Dynamic_Check
7772 and then Restriction_Active (No_Dispatching_Calls)
7773 then
7774 return;
7776 -- Class-wide precondition check not needed in interface thunks since
7777 -- they are installed in the dispatching call that caused invoking the
7778 -- thunk.
7780 elsif Is_Thunk (Current_Scope) then
7781 return;
7782 end if;
7784 Subp := Entity (Name (Call_Node));
7786 -- No check needed for this subprogram call if no class-wide
7787 -- preconditions apply (or if the unique available preconditions
7788 -- are ignored preconditions).
7790 Class_Subp := Class_Preconditions_Subprogram (Subp, Dynamic_Check);
7792 if No (Class_Subp)
7793 or else No (Class_Preconditions (Class_Subp))
7794 then
7795 return;
7796 end if;
7798 -- Build and install the check
7800 if Dynamic_Check then
7801 Cond := Build_Dynamic_Check_Helper_Call;
7802 else
7803 Cond := Build_Static_Check_Helper_Call;
7804 end if;
7806 if Exception_Locations_Suppressed then
7807 Fail :=
7808 Make_Raise_Statement (Loc,
7809 Name =>
7810 New_Occurrence_Of
7811 (RTE (RE_Assert_Failure), Loc));
7813 -- Failed check with message indicating the failed precondition and the
7814 -- call that caused it.
7816 else
7817 Fail :=
7818 Make_Procedure_Call_Statement (Loc,
7819 Name =>
7820 New_Occurrence_Of
7821 (RTE (RE_Raise_Assert_Failure), Loc),
7822 Parameter_Associations =>
7823 New_List (Build_Error_Message (Subp)));
7824 end if;
7826 Insert_Action (Call_Node,
7827 Make_If_Statement (Loc,
7828 Condition => Make_Op_Not (Loc, Cond),
7829 Then_Statements => New_List (Fail)));
7830 end Install_Class_Preconditions_Check;
7832 ------------------------------
7833 -- Is_Build_In_Place_Entity --
7834 ------------------------------
7836 function Is_Build_In_Place_Entity (E : Entity_Id) return Boolean is
7837 Nam : constant String := Get_Name_String (Chars (E));
7839 function Has_Suffix (Suffix : String) return Boolean;
7840 -- Return True if Nam has suffix Suffix
7842 function Has_Suffix (Suffix : String) return Boolean is
7843 Len : constant Natural := Suffix'Length;
7844 begin
7845 return Nam'Length > Len
7846 and then Nam (Nam'Last - Len + 1 .. Nam'Last) = Suffix;
7847 end Has_Suffix;
7849 -- Start of processing for Is_Build_In_Place_Entity
7851 begin
7852 return Has_Suffix (BIP_Alloc_Suffix)
7853 or else Has_Suffix (BIP_Storage_Pool_Suffix)
7854 or else Has_Suffix (BIP_Collection_Suffix)
7855 or else Has_Suffix (BIP_Task_Master_Suffix)
7856 or else Has_Suffix (BIP_Activation_Chain_Suffix)
7857 or else Has_Suffix (BIP_Object_Access_Suffix);
7858 end Is_Build_In_Place_Entity;
7860 --------------------------------
7861 -- Is_Build_In_Place_Function --
7862 --------------------------------
7864 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7865 Kind : constant Entity_Kind := Ekind (E);
7866 Typ : constant Entity_Id := Etype (E);
7868 begin
7869 -- This function is called from Expand_Subtype_From_Expr during
7870 -- semantic analysis, even when expansion is off. In those cases
7871 -- the build_in_place expansion will not take place.
7873 if not Expander_Active then
7874 return False;
7875 end if;
7877 -- We never use build-in-place if the convention is other than Ada,
7878 -- but note that it is OK for a build-in-place function to return a
7879 -- type with a foreign convention because the machinery ensures there
7880 -- is no copying.
7882 return (Kind in E_Function | E_Generic_Function
7883 or else
7884 (Kind = E_Subprogram_Type and then Typ /= Standard_Void_Type))
7885 and then Is_Build_In_Place_Result_Type (Typ)
7886 and then not Has_Foreign_Convention (E);
7887 end Is_Build_In_Place_Function;
7889 -------------------------------------
7890 -- Is_Build_In_Place_Function_Call --
7891 -------------------------------------
7893 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7894 Exp_Node : constant Node_Id := Unqual_Conv (N);
7895 Function_Id : Entity_Id;
7897 begin
7898 -- Return False if the expander is currently inactive, since awareness
7899 -- of build-in-place treatment is only relevant during expansion. Note
7900 -- that Is_Build_In_Place_Function, which is called as part of this
7901 -- function, is also conditioned this way, but we need to check here as
7902 -- well to avoid blowing up on processing protected calls when expansion
7903 -- is disabled (such as with -gnatc) since those would trip over the
7904 -- raise of Program_Error below.
7906 -- In SPARK mode, build-in-place calls are not expanded, so that we
7907 -- may end up with a call that is neither resolved to an entity, nor
7908 -- an indirect call.
7910 if not Expander_Active or else Nkind (Exp_Node) /= N_Function_Call then
7911 return False;
7912 end if;
7914 if Is_Entity_Name (Name (Exp_Node)) then
7915 Function_Id := Entity (Name (Exp_Node));
7917 -- In the case of an explicitly dereferenced call, use the subprogram
7918 -- type generated for the dereference.
7920 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7921 Function_Id := Etype (Name (Exp_Node));
7923 -- This may be a call to a protected function.
7925 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7926 -- The selector in question might not have been analyzed due to a
7927 -- previous error, so analyze it here to output the appropriate
7928 -- error message instead of crashing when attempting to fetch its
7929 -- entity.
7931 if not Analyzed (Selector_Name (Name (Exp_Node))) then
7932 Analyze (Selector_Name (Name (Exp_Node)));
7933 end if;
7935 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7937 else
7938 raise Program_Error;
7939 end if;
7941 declare
7942 Result : constant Boolean := Is_Build_In_Place_Function (Function_Id);
7943 -- So we can stop here in the debugger
7944 begin
7945 return Result;
7946 end;
7947 end Is_Build_In_Place_Function_Call;
7949 ---------------------------------------
7950 -- Is_Function_Call_With_BIP_Formals --
7951 ---------------------------------------
7953 function Is_Function_Call_With_BIP_Formals (N : Node_Id) return Boolean is
7954 Exp_Node : constant Node_Id := Unqual_Conv (N);
7955 Function_Id : Entity_Id;
7957 begin
7958 -- Return False if the expander is currently inactive, since awareness
7959 -- of build-in-place treatment is only relevant during expansion. Note
7960 -- that Is_Build_In_Place_Function, which is called as part of this
7961 -- function, is also conditioned this way, but we need to check here as
7962 -- well to avoid blowing up on processing protected calls when expansion
7963 -- is disabled (such as with -gnatc) since those would trip over the
7964 -- raise of Program_Error below.
7966 -- In SPARK mode, build-in-place calls are not expanded, so that we
7967 -- may end up with a call that is neither resolved to an entity, nor
7968 -- an indirect call.
7970 if not Expander_Active or else Nkind (Exp_Node) /= N_Function_Call then
7971 return False;
7972 end if;
7974 if Is_Entity_Name (Name (Exp_Node)) then
7975 Function_Id := Entity (Name (Exp_Node));
7977 -- In the case of an explicitly dereferenced call, use the subprogram
7978 -- type generated for the dereference.
7980 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7981 Function_Id := Etype (Name (Exp_Node));
7983 -- This may be a call to a protected function.
7985 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7986 -- The selector in question might not have been analyzed due to a
7987 -- previous error, so analyze it here to output the appropriate
7988 -- error message instead of crashing when attempting to fetch its
7989 -- entity.
7991 if not Analyzed (Selector_Name (Name (Exp_Node))) then
7992 Analyze (Selector_Name (Name (Exp_Node)));
7993 end if;
7995 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7997 else
7998 raise Program_Error;
7999 end if;
8001 if Is_Build_In_Place_Function (Function_Id) then
8002 return True;
8004 -- True also if the function has BIP Formals
8006 else
8007 declare
8008 Kind : constant Entity_Kind := Ekind (Function_Id);
8010 begin
8011 if (Kind in E_Function | E_Generic_Function
8012 or else (Kind = E_Subprogram_Type
8013 and then
8014 Etype (Function_Id) /= Standard_Void_Type))
8015 and then Has_BIP_Formals (Function_Id)
8016 then
8017 -- So we can stop here in the debugger
8018 return True;
8019 else
8020 return False;
8021 end if;
8022 end;
8023 end if;
8024 end Is_Function_Call_With_BIP_Formals;
8026 -----------------------------------
8027 -- Is_Build_In_Place_Result_Type --
8028 -----------------------------------
8030 function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean is
8031 begin
8032 if not Expander_Active then
8033 return False;
8034 end if;
8036 -- In Ada 2005 all functions with an inherently limited return type
8037 -- must be handled using a build-in-place profile, including the case
8038 -- of a function with a limited interface result, where the function
8039 -- may return objects of nonlimited descendants.
8041 return Is_Inherently_Limited_Type (Typ)
8042 and then Ada_Version >= Ada_2005
8043 and then not Debug_Flag_Dot_L;
8044 end Is_Build_In_Place_Result_Type;
8046 -------------------------------------
8047 -- Is_Build_In_Place_Return_Object --
8048 -------------------------------------
8050 function Is_Build_In_Place_Return_Object (E : Entity_Id) return Boolean is
8051 begin
8052 return Is_Return_Object (E)
8053 and then Is_Build_In_Place_Function (Return_Applies_To (Scope (E)));
8054 end Is_Build_In_Place_Return_Object;
8056 -----------------------------------
8057 -- Is_By_Reference_Return_Object --
8058 -----------------------------------
8060 function Is_By_Reference_Return_Object (E : Entity_Id) return Boolean is
8061 begin
8062 return Is_Return_Object (E)
8063 and then Is_By_Reference_Type (Etype (Return_Applies_To (Scope (E))));
8064 end Is_By_Reference_Return_Object;
8066 -----------------------
8067 -- Is_Null_Procedure --
8068 -----------------------
8070 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8071 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8073 begin
8074 if Ekind (Subp) /= E_Procedure then
8075 return False;
8077 -- Check if this is a declared null procedure
8079 elsif Nkind (Decl) = N_Subprogram_Declaration then
8080 if not Null_Present (Specification (Decl)) then
8081 return False;
8083 elsif No (Body_To_Inline (Decl)) then
8084 return False;
8086 -- Check if the body contains only a null statement, followed by
8087 -- the return statement added during expansion.
8089 else
8090 declare
8091 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8093 Stat : Node_Id;
8094 Stat2 : Node_Id;
8096 begin
8097 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8098 return False;
8099 else
8100 -- We must skip SCIL nodes because they are currently
8101 -- implemented as special N_Null_Statement nodes.
8103 Stat :=
8104 First_Non_SCIL_Node
8105 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8106 Stat2 := Next_Non_SCIL_Node (Stat);
8108 return
8109 Is_Empty_List (Declarations (Orig_Bod))
8110 and then Nkind (Stat) = N_Null_Statement
8111 and then
8112 (No (Stat2)
8113 or else
8114 (Nkind (Stat2) = N_Simple_Return_Statement
8115 and then No (Next (Stat2))));
8116 end if;
8117 end;
8118 end if;
8120 else
8121 return False;
8122 end if;
8123 end Is_Null_Procedure;
8125 --------------------------------------
8126 -- Is_Secondary_Stack_Return_Object --
8127 --------------------------------------
8129 function Is_Secondary_Stack_Return_Object (E : Entity_Id) return Boolean is
8130 begin
8131 return Is_Return_Object (E)
8132 and then Needs_Secondary_Stack (Etype (Return_Applies_To (Scope (E))));
8133 end Is_Secondary_Stack_Return_Object;
8135 ------------------------------
8136 -- Is_Special_Return_Object --
8137 ------------------------------
8139 function Is_Special_Return_Object (E : Entity_Id) return Boolean is
8140 begin
8141 return Is_Build_In_Place_Return_Object (E)
8142 or else Is_Secondary_Stack_Return_Object (E)
8143 or else (Back_End_Return_Slot
8144 and then Is_By_Reference_Return_Object (E));
8145 end Is_Special_Return_Object;
8147 -------------------------------------------
8148 -- Make_Build_In_Place_Call_In_Allocator --
8149 -------------------------------------------
8151 procedure Make_Build_In_Place_Call_In_Allocator
8152 (Allocator : Node_Id;
8153 Function_Call : Node_Id)
8155 Acc_Type : constant Entity_Id := Etype (Allocator);
8156 Loc : constant Source_Ptr := Sloc (Function_Call);
8157 Func_Call : Node_Id := Function_Call;
8158 Ref_Func_Call : Node_Id;
8159 Function_Id : Entity_Id;
8160 Result_Subt : Entity_Id;
8161 New_Allocator : Node_Id;
8162 Return_Obj_Access : Entity_Id; -- temp for function result
8163 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
8164 Alloc_Form : BIP_Allocation_Form;
8165 Pool_Actual : Node_Id; -- Present if Alloc_Form = User_Storage_Pool
8166 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
8167 Chain : Entity_Id; -- activation chain, in case of tasks
8169 begin
8170 -- Step past qualification or unchecked conversion (the latter can occur
8171 -- in cases of calls to 'Input).
8173 if Nkind (Func_Call) in N_Qualified_Expression
8174 | N_Type_Conversion
8175 | N_Unchecked_Type_Conversion
8176 then
8177 Func_Call := Expression (Func_Call);
8178 end if;
8180 -- Mark the call as processed as a build-in-place call
8182 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8183 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8185 if Is_Entity_Name (Name (Func_Call)) then
8186 Function_Id := Entity (Name (Func_Call));
8188 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8189 Function_Id := Etype (Name (Func_Call));
8191 else
8192 raise Program_Error;
8193 end if;
8195 Warn_BIP (Func_Call);
8197 Result_Subt := Available_View (Etype (Function_Id));
8199 -- Create a temp for the function result. In the caller-allocates case,
8200 -- this will be initialized to the result of a new uninitialized
8201 -- allocator. Note: we do not use Allocator as the Related_Node of
8202 -- Return_Obj_Access in call to Make_Temporary below as this would
8203 -- create a sort of infinite "recursion".
8205 Return_Obj_Access := Make_Temporary (Loc, 'R');
8206 Set_Etype (Return_Obj_Access, Acc_Type);
8207 Set_Can_Never_Be_Null (Acc_Type, False);
8208 -- It gets initialized to null, so we can't have that
8210 -- When the result subtype is returned on the secondary stack or is
8211 -- tagged, the called function itself must perform the allocation of
8212 -- the return object, so we pass parameters indicating that.
8214 -- But that's also the case when the result subtype needs finalization
8215 -- actions because the caller side allocation may result in undesirable
8216 -- finalization. Consider the following example:
8218 -- function Make_Lim_Ctrl return Lim_Ctrl is
8219 -- begin
8220 -- return Result : Lim_Ctrl := raise Program_Error do
8221 -- null;
8222 -- end return;
8223 -- end Make_Lim_Ctrl;
8225 -- Obj : Lim_Ctrl_Ptr := new Lim_Ctrl'(Make_Lim_Ctrl);
8227 -- Even though the size of limited controlled type Lim_Ctrl is known,
8228 -- allocating Obj at the caller side will chain Obj on Lim_Ctrl_Ptr's
8229 -- finalization collection. The subsequent call to Make_Lim_Ctrl will
8230 -- fail during the initialization actions for Result, which means that
8231 -- Result (and Obj by extension) should not be finalized. However Obj
8232 -- will be finalized when access type Lim_Ctrl_Ptr goes out of scope
8233 -- since it is already attached on the its finalization collection.
8235 if Needs_BIP_Alloc_Form (Function_Id) then
8236 Temp_Init := Empty;
8238 -- Case of a user-defined storage pool. Pass an allocation parameter
8239 -- indicating that the function should allocate its result in the
8240 -- pool, and pass an access to the pool. Use 'Unrestricted_Access
8241 -- because the pool may not be aliased.
8243 if Present (Associated_Storage_Pool (Acc_Type)) then
8244 Alloc_Form := User_Storage_Pool;
8245 Pool_Actual :=
8246 Make_Attribute_Reference (Loc,
8247 Prefix =>
8248 New_Occurrence_Of
8249 (Associated_Storage_Pool (Acc_Type), Loc),
8250 Attribute_Name => Name_Unrestricted_Access);
8252 -- No user-defined pool; pass an allocation parameter indicating that
8253 -- the function should allocate its result on the heap. When there is
8254 -- a finalization collection, a pool reference is required.
8256 elsif Needs_BIP_Collection (Function_Id) then
8257 Alloc_Form := Global_Heap;
8258 Pool_Actual :=
8259 Make_Attribute_Reference (Loc,
8260 Prefix =>
8261 New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc),
8262 Attribute_Name => Name_Unrestricted_Access);
8264 else
8265 Alloc_Form := Global_Heap;
8266 Pool_Actual := Empty;
8267 end if;
8269 -- The caller does not provide the return object in this case, so we
8270 -- have to pass null for the object access actual.
8272 Return_Obj_Actual := Empty;
8274 -- When the result subtype neither is returned on the secondary stack
8275 -- nor is tagged, the return object is created on the caller side, and
8276 -- access to it is passed to the function.
8278 else
8279 -- Replace the initialized allocator of form "new T'(Func (...))"
8280 -- with an uninitialized allocator of form "new T", where T is the
8281 -- result subtype of the called function. The call to the function
8282 -- is handled separately further below.
8284 New_Allocator :=
8285 Make_Allocator (Loc,
8286 Expression => New_Occurrence_Of (Result_Subt, Loc));
8287 Set_No_Initialization (New_Allocator);
8289 -- Copy attributes to new allocator. Note that the new allocator
8290 -- logically comes from source if the original one did, so copy the
8291 -- relevant flag. This ensures proper treatment of the restriction
8292 -- No_Implicit_Heap_Allocations in this case.
8294 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8295 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8296 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8298 Rewrite (Allocator, New_Allocator);
8300 -- Initial value of the temp is the result of the uninitialized
8301 -- allocator. Unchecked_Convert is needed for T'Input where T is
8302 -- derived from a controlled type.
8304 Temp_Init := Relocate_Node (Allocator);
8306 if Nkind (Function_Call) in
8307 N_Type_Conversion | N_Unchecked_Type_Conversion
8308 then
8309 Temp_Init := Unchecked_Convert_To (Acc_Type, Temp_Init);
8310 end if;
8312 -- Indicate that caller allocates, and pass in the return object
8314 Alloc_Form := Caller_Allocation;
8315 Pool_Actual := Empty;
8316 Return_Obj_Actual := Unchecked_Convert_To
8317 (Result_Subt,
8318 Make_Explicit_Dereference (Loc,
8319 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
8320 end if;
8322 -- Declare the temp object
8324 Insert_Action (Allocator,
8325 Make_Object_Declaration (Loc,
8326 Defining_Identifier => Return_Obj_Access,
8327 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8328 Expression => Temp_Init));
8330 Ref_Func_Call := Make_Reference (Loc, Func_Call);
8332 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8333 -- then generate an implicit conversion to force displacement of the
8334 -- "this" pointer.
8336 if Is_Interface (Designated_Type (Acc_Type)) then
8337 Rewrite
8338 (Ref_Func_Call,
8339 OK_Convert_To (Acc_Type, Ref_Func_Call));
8341 -- If the types are incompatible, we need an unchecked conversion. Note
8342 -- that the full types will be compatible, but the types not visibly
8343 -- compatible.
8345 elsif Nkind (Function_Call)
8346 in N_Type_Conversion | N_Unchecked_Type_Conversion
8347 then
8348 Ref_Func_Call := Unchecked_Convert_To (Acc_Type, Ref_Func_Call);
8349 end if;
8351 declare
8352 Assign : constant Node_Id :=
8353 Make_Assignment_Statement (Loc,
8354 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
8355 Expression => Ref_Func_Call);
8356 -- Assign the result of the function call into the temp. In the
8357 -- caller-allocates case, this is overwriting the temp with its
8358 -- initial value, which has no effect. In the callee-allocates case,
8359 -- this is setting the temp to point to the object allocated by the
8360 -- callee. Unchecked_Convert is needed for T'Input where T is derived
8361 -- from a controlled type.
8363 Actions : List_Id;
8364 -- Actions to be inserted. If there are no tasks, this is just the
8365 -- assignment statement. If the allocated object has tasks, we need
8366 -- to wrap the assignment in a block that activates them. The
8367 -- activation chain of that block must be passed to the function,
8368 -- rather than some outer chain.
8370 begin
8371 if Might_Have_Tasks (Result_Subt) then
8372 Actions := New_List;
8373 Build_Task_Allocate_Block
8374 (Actions, Allocator, Init_Stmts => New_List (Assign));
8375 Chain := Activation_Chain_Entity (Last (Actions));
8376 else
8377 Actions := New_List (Assign);
8378 Chain := Empty;
8379 end if;
8381 Insert_Actions (Allocator, Actions);
8382 end;
8384 -- When the function has a controlling result, an allocation-form
8385 -- parameter must be passed indicating that the caller is allocating
8386 -- the result object. This is needed because such a function can be
8387 -- called as a dispatching operation and must be treated similarly
8388 -- to functions with unconstrained result subtypes.
8390 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8391 (Func_Call,
8392 Function_Id,
8393 Alloc_Form => Alloc_Form,
8394 Pool_Exp => Pool_Actual);
8396 Add_Collection_Actual_To_Build_In_Place_Call
8397 (Func_Call, Function_Id, Ptr_Typ => Acc_Type);
8399 Add_Task_Actuals_To_Build_In_Place_Call
8400 (Func_Call,
8401 Function_Id,
8402 Master_Actual => Master_Id (Acc_Type),
8403 Chain => Chain);
8405 -- Add an implicit actual to the function call that provides access
8406 -- to the allocated object. An unchecked conversion to the (specific)
8407 -- result subtype of the function is inserted to handle cases where
8408 -- the access type of the allocator has a class-wide designated type.
8410 Add_Access_Actual_To_Build_In_Place_Call
8411 (Func_Call, Function_Id, Return_Obj_Actual);
8413 -- Finally, replace the allocator node with a reference to the temp
8415 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8417 Analyze_And_Resolve (Allocator, Acc_Type);
8418 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8419 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8420 end Make_Build_In_Place_Call_In_Allocator;
8422 ---------------------------------------------------
8423 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8424 ---------------------------------------------------
8426 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8427 (Function_Call : Node_Id)
8429 Loc : constant Source_Ptr := Sloc (Function_Call);
8430 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8431 Function_Id : Entity_Id;
8432 Result_Subt : Entity_Id;
8433 Return_Obj_Id : Entity_Id;
8434 Return_Obj_Decl : Entity_Id;
8436 begin
8437 -- If the call has already been processed to add build-in-place actuals
8438 -- then return. One place this can occur is for calls to build-in-place
8439 -- functions that occur within a call to a protected operation, where
8440 -- due to rewriting and expansion of the protected call there can be
8441 -- more than one call to Expand_Actuals for the same set of actuals.
8443 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8444 return;
8445 end if;
8447 -- Mark the call as processed as a build-in-place call
8449 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8451 if Is_Entity_Name (Name (Func_Call)) then
8452 Function_Id := Entity (Name (Func_Call));
8454 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8455 Function_Id := Etype (Name (Func_Call));
8457 else
8458 raise Program_Error;
8459 end if;
8461 Warn_BIP (Func_Call);
8463 Result_Subt := Etype (Function_Id);
8465 -- If the build-in-place function returns a controlled object, then the
8466 -- object needs to be finalized immediately after the context. Since
8467 -- this case produces a transient scope, the servicing finalizer needs
8468 -- to name the returned object. Create a temporary which is initialized
8469 -- with the function call:
8471 -- Temp_Id : Func_Type := BIP_Func_Call;
8473 -- The initialization expression of the temporary will be rewritten by
8474 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8475 -- Call_In_Object_Declaration.
8477 if Needs_Finalization (Result_Subt) then
8478 declare
8479 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8480 Temp_Decl : Node_Id;
8482 begin
8483 -- Reset the guard on the function call since the following does
8484 -- not perform actual call expansion.
8486 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8488 Temp_Decl :=
8489 Make_Object_Declaration (Loc,
8490 Defining_Identifier => Temp_Id,
8491 Object_Definition =>
8492 New_Occurrence_Of (Result_Subt, Loc),
8493 Expression =>
8494 New_Copy_Tree (Function_Call));
8496 Insert_Action (Function_Call, Temp_Decl);
8498 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8499 Analyze (Function_Call);
8500 end;
8502 -- When the result subtype is definite, an object of the subtype is
8503 -- declared and an access value designating it is passed as an actual.
8505 elsif Caller_Known_Size (Func_Call, Result_Subt) then
8507 -- Create a temporary object to hold the function result
8509 Return_Obj_Id := Make_Temporary (Loc, 'R');
8510 Set_Etype (Return_Obj_Id, Result_Subt);
8512 Return_Obj_Decl :=
8513 Make_Object_Declaration (Loc,
8514 Defining_Identifier => Return_Obj_Id,
8515 Aliased_Present => True,
8516 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8518 Set_No_Initialization (Return_Obj_Decl);
8520 Insert_Action (Func_Call, Return_Obj_Decl);
8522 -- When the function has a controlling result, an allocation-form
8523 -- parameter must be passed indicating that the caller is allocating
8524 -- the result object. This is needed because such a function can be
8525 -- called as a dispatching operation and must be treated similarly
8526 -- to functions with unconstrained result subtypes.
8528 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8529 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8531 Add_Collection_Actual_To_Build_In_Place_Call
8532 (Func_Call, Function_Id);
8534 Add_Task_Actuals_To_Build_In_Place_Call
8535 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8537 -- Add an implicit actual to the function call that provides access
8538 -- to the caller's return object.
8540 Add_Access_Actual_To_Build_In_Place_Call
8541 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8543 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8544 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8546 -- When the result subtype is unconstrained, the function must allocate
8547 -- the return object in the secondary stack, so appropriate implicit
8548 -- parameters are added to the call to indicate that. A transient
8549 -- scope is established to ensure eventual cleanup of the result.
8551 else
8552 -- Pass an allocation parameter indicating that the function should
8553 -- allocate its result on the secondary stack.
8555 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8556 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8558 Add_Collection_Actual_To_Build_In_Place_Call
8559 (Func_Call, Function_Id);
8561 Add_Task_Actuals_To_Build_In_Place_Call
8562 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8564 -- Pass a null value to the function since no return object is
8565 -- available on the caller side.
8567 Add_Access_Actual_To_Build_In_Place_Call
8568 (Func_Call, Function_Id, Empty);
8570 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8571 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8572 end if;
8573 end Make_Build_In_Place_Call_In_Anonymous_Context;
8575 --------------------------------------------
8576 -- Make_Build_In_Place_Call_In_Assignment --
8577 --------------------------------------------
8579 procedure Make_Build_In_Place_Call_In_Assignment
8580 (Assign : Node_Id;
8581 Function_Call : Node_Id)
8583 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8584 Lhs : constant Node_Id := Name (Assign);
8585 Loc : constant Source_Ptr := Sloc (Function_Call);
8586 Func_Id : Entity_Id;
8587 Obj_Decl : Node_Id;
8588 Obj_Id : Entity_Id;
8589 Ptr_Typ : Entity_Id;
8590 Ptr_Typ_Decl : Node_Id;
8591 New_Expr : Node_Id;
8592 Result_Subt : Entity_Id;
8594 begin
8595 -- Mark the call as processed as a build-in-place call
8597 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8598 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8600 if Is_Entity_Name (Name (Func_Call)) then
8601 Func_Id := Entity (Name (Func_Call));
8603 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8604 Func_Id := Etype (Name (Func_Call));
8606 else
8607 raise Program_Error;
8608 end if;
8610 Warn_BIP (Func_Call);
8612 Result_Subt := Etype (Func_Id);
8614 -- When the result subtype is unconstrained, an additional actual must
8615 -- be passed to indicate that the caller is providing the return object.
8616 -- This parameter must also be passed when the called function has a
8617 -- controlling result, because dispatching calls to the function needs
8618 -- to be treated effectively the same as calls to class-wide functions.
8620 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8621 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8623 Add_Collection_Actual_To_Build_In_Place_Call
8624 (Func_Call, Func_Id);
8626 Add_Task_Actuals_To_Build_In_Place_Call
8627 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8629 -- Add an implicit actual to the function call that provides access to
8630 -- the caller's return object.
8632 Add_Access_Actual_To_Build_In_Place_Call
8633 (Func_Call, Func_Id, Unchecked_Convert_To (Result_Subt, Lhs));
8635 -- Create an access type designating the function's result subtype
8637 Ptr_Typ := Make_Temporary (Loc, 'A');
8639 Ptr_Typ_Decl :=
8640 Make_Full_Type_Declaration (Loc,
8641 Defining_Identifier => Ptr_Typ,
8642 Type_Definition =>
8643 Make_Access_To_Object_Definition (Loc,
8644 All_Present => True,
8645 Subtype_Indication =>
8646 New_Occurrence_Of (Result_Subt, Loc)));
8647 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8649 -- Finally, create an access object initialized to a reference to the
8650 -- function call. We know this access value is non-null, so mark the
8651 -- entity accordingly to suppress junk access checks.
8653 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8655 -- Add a conversion if it's the wrong type
8657 New_Expr := Unchecked_Convert_To (Ptr_Typ, New_Expr);
8659 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8660 Set_Etype (Obj_Id, Ptr_Typ);
8661 Set_Is_Known_Non_Null (Obj_Id);
8663 Obj_Decl :=
8664 Make_Object_Declaration (Loc,
8665 Defining_Identifier => Obj_Id,
8666 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8667 Expression => New_Expr);
8668 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8670 Rewrite (Assign, Make_Null_Statement (Loc));
8671 pragma Assert (Check_Number_Of_Actuals (Func_Call, Func_Id));
8672 pragma Assert (Check_BIP_Actuals (Func_Call, Func_Id));
8673 end Make_Build_In_Place_Call_In_Assignment;
8675 ----------------------------------------------------
8676 -- Make_Build_In_Place_Call_In_Object_Declaration --
8677 ----------------------------------------------------
8679 procedure Make_Build_In_Place_Call_In_Object_Declaration
8680 (Obj_Decl : Node_Id;
8681 Function_Call : Node_Id)
8683 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id;
8684 -- Get the value of Function_Id, below
8686 ---------------------
8687 -- Get_Function_Id --
8688 ---------------------
8690 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id is
8691 begin
8692 if Is_Entity_Name (Name (Func_Call)) then
8693 return Entity (Name (Func_Call));
8695 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8696 return Etype (Name (Func_Call));
8698 else
8699 raise Program_Error;
8700 end if;
8701 end Get_Function_Id;
8703 -- Local variables
8705 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8706 Function_Id : constant Entity_Id := Get_Function_Id (Func_Call);
8707 Loc : constant Source_Ptr := Sloc (Function_Call);
8708 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
8709 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
8710 Obj_Typ : constant Entity_Id := Etype (Obj_Def_Id);
8711 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
8712 Result_Subt : constant Entity_Id := Etype (Function_Id);
8714 Call_Deref : Node_Id;
8715 Caller_Object : Node_Id;
8716 Collection_Actual : Node_Id := Empty;
8717 Def_Id : Entity_Id;
8718 Designated_Type : Entity_Id;
8719 Pool_Actual : Node_Id;
8720 Ptr_Typ : Entity_Id;
8721 Ptr_Typ_Decl : Node_Id;
8722 Pass_Caller_Acc : Boolean := False;
8723 Res_Decl : Node_Id;
8725 Definite : constant Boolean :=
8726 Caller_Known_Size (Func_Call, Result_Subt)
8727 and then not Is_Class_Wide_Type (Obj_Typ);
8728 -- In the case of "X : T'Class := F(...);", where F returns a
8729 -- Caller_Known_Size (specific) tagged type, we treat it as
8730 -- indefinite, because the code for the Definite case below sets the
8731 -- initialization expression of the object to Empty, which would be
8732 -- illegal Ada, and would cause gigi to misallocate X.
8734 Is_OK_Return_Object : constant Boolean :=
8735 Is_Return_Object (Obj_Def_Id)
8736 and then
8737 not Has_Foreign_Convention (Return_Applies_To (Scope (Obj_Def_Id)));
8739 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8741 begin
8742 -- If the call has already been processed to add build-in-place actuals
8743 -- then return.
8745 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8746 return;
8747 end if;
8749 -- Mark the call as processed as a build-in-place call
8751 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8753 Warn_BIP (Func_Call);
8755 -- Create an access type designating the function's result subtype.
8756 -- We use the type of the original call because it may be a call to an
8757 -- inherited operation, which the expansion has replaced with the parent
8758 -- operation that yields the parent type. Note that this access type
8759 -- must be declared before we establish a transient scope, so that it
8760 -- receives the proper accessibility level.
8762 if Is_Class_Wide_Type (Obj_Typ)
8763 and then not Is_Interface (Obj_Typ)
8764 and then not Is_Class_Wide_Type (Etype (Function_Call))
8765 then
8766 Designated_Type := Obj_Typ;
8767 else
8768 Designated_Type := Etype (Function_Call);
8769 end if;
8771 Ptr_Typ := Make_Temporary (Loc, 'A');
8772 Ptr_Typ_Decl :=
8773 Make_Full_Type_Declaration (Loc,
8774 Defining_Identifier => Ptr_Typ,
8775 Type_Definition =>
8776 Make_Access_To_Object_Definition (Loc,
8777 All_Present => True,
8778 Subtype_Indication =>
8779 New_Occurrence_Of (Designated_Type, Loc)));
8781 -- The access type and its accompanying object must be inserted after
8782 -- the object declaration in the constrained case, so that the function
8783 -- call can be passed access to the object. In the indefinite case, or
8784 -- if the object declaration is for a return object, the access type and
8785 -- object must be inserted before the object, since the object
8786 -- declaration is rewritten to be a renaming of a dereference of the
8787 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8788 -- the result object is in a different (transient) scope, so won't cause
8789 -- freezing.
8791 if Definite and then not Is_OK_Return_Object then
8793 -- The presence of an address clause complicates the build-in-place
8794 -- expansion because the indicated address must be processed before
8795 -- the indirect call is generated (including the definition of a
8796 -- local pointer to the object). The address clause may come from
8797 -- an aspect specification or from an explicit attribute
8798 -- specification appearing after the object declaration. These two
8799 -- cases require different processing.
8801 if Has_Aspect (Obj_Def_Id, Aspect_Address) then
8803 -- Skip non-delayed pragmas that correspond to other aspects, if
8804 -- any, to find proper insertion point for freeze node of object.
8806 declare
8807 D : Node_Id := Obj_Decl;
8808 N : Node_Id := Next (D);
8810 begin
8811 while Present (N)
8812 and then Nkind (N) in N_Attribute_Reference | N_Pragma
8813 loop
8814 Analyze (N);
8815 D := N;
8816 Next (N);
8817 end loop;
8819 Insert_After (D, Ptr_Typ_Decl);
8821 -- Freeze object before pointer declaration, to ensure that
8822 -- generated attribute for address is inserted at the proper
8823 -- place.
8825 Freeze_Before (Ptr_Typ_Decl, Obj_Def_Id);
8826 end;
8828 Analyze (Ptr_Typ_Decl);
8830 elsif Present (Following_Address_Clause (Obj_Decl)) then
8832 -- Locate explicit address clause, which may also follow pragmas
8833 -- generated by other aspect specifications.
8835 declare
8836 Addr : constant Node_Id := Following_Address_Clause (Obj_Decl);
8837 D : Node_Id := Next (Obj_Decl);
8839 begin
8840 while Present (D) loop
8841 Analyze (D);
8842 exit when D = Addr;
8843 Next (D);
8844 end loop;
8846 Insert_After_And_Analyze (Addr, Ptr_Typ_Decl);
8847 end;
8849 else
8850 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
8851 end if;
8852 else
8853 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
8854 end if;
8856 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8857 -- elaborated in an inner (transient) scope and thus won't cause
8858 -- freezing by itself. It's not an itype, but it needs to be frozen
8859 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
8861 Freeze_Itype (Ptr_Typ, Ptr_Typ_Decl);
8863 -- If the object is a return object of an enclosing build-in-place
8864 -- function, then the implicit build-in-place parameters of the
8865 -- enclosing function are simply passed along to the called function.
8866 -- (Unfortunately, this won't cover the case of extension aggregates
8867 -- where the ancestor part is a build-in-place indefinite function
8868 -- call that should be passed along the caller's parameters.
8869 -- Currently those get mishandled by reassigning the result of the
8870 -- call to the aggregate return object, when the call result should
8871 -- really be directly built in place in the aggregate and not in a
8872 -- temporary. ???)
8874 if Is_OK_Return_Object then
8875 Pass_Caller_Acc := True;
8877 -- When the enclosing function has a BIP_Alloc_Form formal then we
8878 -- pass it along to the callee (such as when the enclosing function
8879 -- has an unconstrained or tagged result type).
8881 if Needs_BIP_Alloc_Form (Encl_Func) then
8882 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8883 Pool_Actual :=
8884 New_Occurrence_Of
8885 (Build_In_Place_Formal
8886 (Encl_Func, BIP_Storage_Pool), Loc);
8888 -- The build-in-place pool formal is not built on e.g. ZFP
8890 else
8891 Pool_Actual := Empty;
8892 end if;
8894 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8895 (Function_Call => Func_Call,
8896 Function_Id => Function_Id,
8897 Alloc_Form_Exp =>
8898 New_Occurrence_Of
8899 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
8900 Pool_Exp => Pool_Actual);
8902 -- Otherwise, if enclosing function has a definite result subtype,
8903 -- then caller allocation will be used.
8905 else
8906 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8907 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8908 end if;
8910 if Needs_BIP_Collection (Encl_Func) then
8911 Collection_Actual :=
8912 New_Occurrence_Of
8913 (Build_In_Place_Formal
8914 (Encl_Func, BIP_Collection), Loc);
8915 end if;
8917 -- Retrieve the BIPacc formal from the enclosing function and convert
8918 -- it to the access type of the callee's BIP_Object_Access formal.
8920 Caller_Object :=
8921 Unchecked_Convert_To
8922 (Etype (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
8923 New_Occurrence_Of
8924 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access), Loc));
8926 -- In the definite case, add an implicit actual to the function call
8927 -- that provides access to the declared object. An unchecked conversion
8928 -- to the (specific) result type of the function is inserted to handle
8929 -- the case where the object is declared with a class-wide type.
8931 elsif Definite then
8932 Caller_Object := Unchecked_Convert_To
8933 (Result_Subt, New_Occurrence_Of (Obj_Def_Id, Loc));
8935 -- When the function has a controlling result, an allocation-form
8936 -- parameter must be passed indicating that the caller is allocating
8937 -- the result object. This is needed because such a function can be
8938 -- called as a dispatching operation and must be treated similarly to
8939 -- functions with indefinite result subtypes.
8941 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8942 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8944 -- The allocation for indefinite library-level objects occurs on the
8945 -- heap as opposed to the secondary stack. This accommodates DLLs where
8946 -- the secondary stack is destroyed after each library unload. This is a
8947 -- hybrid mechanism where a stack-allocated object lives on the heap.
8949 elsif Is_Library_Level_Entity (Obj_Def_Id)
8950 and then not Restriction_Active (No_Implicit_Heap_Allocations)
8951 then
8952 -- Create a finalization collection for the access result type to
8953 -- ensure that the heap allocation can properly chain the object
8954 -- and later finalize it when the library unit goes out of scope.
8956 if Needs_BIP_Collection (Func_Call) then
8957 Build_Finalization_Collection
8958 (Typ => Ptr_Typ,
8959 For_Lib_Level => True,
8960 Insertion_Node => Ptr_Typ_Decl);
8962 Collection_Actual :=
8963 Make_Attribute_Reference (Loc,
8964 Prefix =>
8965 New_Occurrence_Of (Finalization_Collection (Ptr_Typ), Loc),
8966 Attribute_Name => Name_Unrestricted_Access);
8968 Pool_Actual :=
8969 Make_Attribute_Reference (Loc,
8970 Prefix =>
8971 New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc),
8972 Attribute_Name => Name_Unrestricted_Access);
8974 else
8975 Pool_Actual := Empty;
8976 end if;
8978 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8979 (Func_Call,
8980 Function_Id,
8981 Alloc_Form => Global_Heap,
8982 Pool_Exp => Pool_Actual);
8983 Caller_Object := Empty;
8985 -- In other indefinite cases, pass an indication to do the allocation
8986 -- on the secondary stack and set Caller_Object to Empty so that a null
8987 -- value will be passed for the caller's object address. A transient
8988 -- scope is established to ensure eventual cleanup of the result.
8990 else
8991 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8992 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8993 Caller_Object := Empty;
8995 Establish_Transient_Scope (Obj_Decl, Manage_Sec_Stack => True);
8996 end if;
8998 -- Pass along any finalization collection actual, which is needed in
8999 -- the case where the called function initializes a return object of
9000 -- an enclosing build-in-place function.
9002 Add_Collection_Actual_To_Build_In_Place_Call
9003 (Func_Call, Function_Id, Collection_Exp => Collection_Actual);
9005 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
9006 and then Needs_BIP_Task_Actuals (Function_Id)
9007 then
9008 -- Here we're passing along the master that was passed in to this
9009 -- function.
9011 Add_Task_Actuals_To_Build_In_Place_Call
9012 (Func_Call, Function_Id,
9013 Master_Actual =>
9014 New_Occurrence_Of
9015 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
9017 else
9018 Add_Task_Actuals_To_Build_In_Place_Call
9019 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9020 end if;
9022 Add_Access_Actual_To_Build_In_Place_Call
9023 (Func_Call,
9024 Function_Id,
9025 Caller_Object,
9026 Is_Access => Pass_Caller_Acc);
9028 -- Finally, create an access object initialized to a reference to the
9029 -- function call. We know this access value cannot be null, so mark the
9030 -- entity accordingly to suppress the access check. We need to suppress
9031 -- warnings, because this can be part of the expansion of "for ... of"
9032 -- and similar constructs that generate finalization actions. Such
9033 -- finalization actions are safe, because they check a count that
9034 -- indicates which objects should be finalized, but the back end
9035 -- nonetheless warns about uninitialized objects.
9037 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
9038 Set_Warnings_Off (Def_Id);
9039 Set_Etype (Def_Id, Ptr_Typ);
9040 Set_Is_Known_Non_Null (Def_Id);
9042 if Nkind (Function_Call) in N_Type_Conversion
9043 | N_Unchecked_Type_Conversion
9044 then
9045 Res_Decl :=
9046 Make_Object_Declaration (Loc,
9047 Defining_Identifier => Def_Id,
9048 Constant_Present => True,
9049 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9050 Expression =>
9051 Unchecked_Convert_To
9052 (Ptr_Typ, Make_Reference (Loc, Relocate_Node (Func_Call))));
9053 else
9054 Res_Decl :=
9055 Make_Object_Declaration (Loc,
9056 Defining_Identifier => Def_Id,
9057 Constant_Present => True,
9058 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9059 Expression =>
9060 Make_Reference (Loc, Relocate_Node (Func_Call)));
9061 end if;
9063 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
9065 -- If the result subtype of the called function is definite and is not
9066 -- itself the return expression of an enclosing BIP function, then mark
9067 -- the object as having no initialization.
9069 if Definite and then not Is_OK_Return_Object then
9071 -- The related object declaration is encased in a transient block
9072 -- because the build-in-place function call contains at least one
9073 -- nested function call that produces a controlled transient
9074 -- temporary:
9076 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9078 -- Since the build-in-place expansion decouples the call from the
9079 -- object declaration, the finalization machinery lacks the context
9080 -- which prompted the generation of the transient block. To resolve
9081 -- this scenario, store the build-in-place call.
9083 if Scope_Is_Transient then
9084 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
9085 end if;
9087 Set_Expression (Obj_Decl, Empty);
9088 Set_No_Initialization (Obj_Decl);
9090 -- In case of an indefinite result subtype, or if the call is the
9091 -- return expression of an enclosing BIP function, rewrite the object
9092 -- declaration as an object renaming where the renamed object is a
9093 -- dereference of <function_Call>'reference:
9095 -- Obj : Subt renames <function_call>'Ref.all;
9097 else
9098 Call_Deref :=
9099 Make_Explicit_Dereference (Obj_Loc,
9100 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
9102 Rewrite (Obj_Decl,
9103 Make_Object_Renaming_Declaration (Obj_Loc,
9104 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
9105 Subtype_Mark =>
9106 New_Occurrence_Of (Designated_Type, Obj_Loc),
9107 Name => Call_Deref));
9109 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
9110 -- to Obj_Def_Id.
9112 pragma Assert (Ekind (Defining_Identifier (Obj_Decl)) = E_Void);
9113 Set_Renamed_Object_Of_Possibly_Void
9114 (Defining_Identifier (Obj_Decl), Call_Deref);
9116 -- If the original entity comes from source, then mark the new
9117 -- entity as needing debug information, even though it's defined
9118 -- by a generated renaming that does not come from source, so that
9119 -- the Materialize_Entity flag will be set on the entity when
9120 -- Debug_Renaming_Declaration is called during analysis.
9122 if Comes_From_Source (Obj_Def_Id) then
9123 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
9124 end if;
9126 Analyze (Obj_Decl);
9127 Replace_Renaming_Declaration_Id
9128 (Obj_Decl, Original_Node (Obj_Decl));
9129 end if;
9131 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
9132 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
9133 end Make_Build_In_Place_Call_In_Object_Declaration;
9135 -------------------------------------------------
9136 -- Make_Build_In_Place_Iface_Call_In_Allocator --
9137 -------------------------------------------------
9139 procedure Make_Build_In_Place_Iface_Call_In_Allocator
9140 (Allocator : Node_Id;
9141 Function_Call : Node_Id)
9143 BIP_Func_Call : constant Node_Id :=
9144 Unqual_BIP_Iface_Function_Call (Function_Call);
9145 Loc : constant Source_Ptr := Sloc (Function_Call);
9147 Anon_Type : Entity_Id;
9148 Tmp_Decl : Node_Id;
9149 Tmp_Id : Entity_Id;
9151 begin
9152 -- No action if the call has already been processed
9154 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9155 return;
9156 end if;
9158 Tmp_Id := Make_Temporary (Loc, 'D');
9160 -- Insert a temporary before N initialized with the BIP function call
9161 -- without its enclosing type conversions and analyze it without its
9162 -- expansion. This temporary facilitates us reusing the BIP machinery,
9163 -- which takes care of adding the extra build-in-place actuals and
9164 -- transforms this object declaration into an object renaming
9165 -- declaration.
9167 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Function_Call);
9168 Set_Directly_Designated_Type (Anon_Type, Etype (BIP_Func_Call));
9169 Set_Etype (Anon_Type, Anon_Type);
9170 Build_Class_Wide_Master (Anon_Type);
9172 Tmp_Decl :=
9173 Make_Object_Declaration (Loc,
9174 Defining_Identifier => Tmp_Id,
9175 Object_Definition => New_Occurrence_Of (Anon_Type, Loc),
9176 Expression =>
9177 Make_Allocator (Loc,
9178 Expression =>
9179 Make_Qualified_Expression (Loc,
9180 Subtype_Mark =>
9181 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
9182 Expression => New_Copy_Tree (BIP_Func_Call))));
9184 -- Manually set the associated node for the anonymous access type to
9185 -- be its local declaration, to avoid confusing and complicating
9186 -- the accessibility machinery.
9188 Set_Associated_Node_For_Itype (Anon_Type, Tmp_Decl);
9190 Expander_Mode_Save_And_Set (False);
9191 Insert_Action (Allocator, Tmp_Decl);
9192 Expander_Mode_Restore;
9194 Make_Build_In_Place_Call_In_Allocator
9195 (Allocator => Expression (Tmp_Decl),
9196 Function_Call => Expression (Expression (Tmp_Decl)));
9198 -- Add a conversion to displace the pointer to the allocated object
9199 -- to reference the corresponding dispatch table.
9201 Rewrite (Allocator,
9202 Convert_To (Etype (Allocator),
9203 New_Occurrence_Of (Tmp_Id, Loc)));
9204 end Make_Build_In_Place_Iface_Call_In_Allocator;
9206 ---------------------------------------------------------
9207 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
9208 ---------------------------------------------------------
9210 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
9211 (Function_Call : Node_Id)
9213 BIP_Func_Call : constant Node_Id :=
9214 Unqual_BIP_Iface_Function_Call (Function_Call);
9215 Loc : constant Source_Ptr := Sloc (Function_Call);
9217 Tmp_Decl : Node_Id;
9218 Tmp_Id : Entity_Id;
9220 begin
9221 -- No action of the call has already been processed
9223 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9224 return;
9225 end if;
9227 pragma Assert (Needs_Finalization (Etype (BIP_Func_Call)));
9229 -- Insert a temporary before the call initialized with function call to
9230 -- reuse the BIP machinery which takes care of adding the extra build-in
9231 -- place actuals and transforms this object declaration into an object
9232 -- renaming declaration.
9234 Tmp_Id := Make_Temporary (Loc, 'D');
9236 Tmp_Decl :=
9237 Make_Object_Declaration (Loc,
9238 Defining_Identifier => Tmp_Id,
9239 Object_Definition =>
9240 New_Occurrence_Of (Etype (Function_Call), Loc),
9241 Expression => Relocate_Node (Function_Call));
9243 Expander_Mode_Save_And_Set (False);
9244 Insert_Action (Function_Call, Tmp_Decl);
9245 Expander_Mode_Restore;
9247 Make_Build_In_Place_Iface_Call_In_Object_Declaration
9248 (Obj_Decl => Tmp_Decl,
9249 Function_Call => Expression (Tmp_Decl));
9250 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context;
9252 ----------------------------------------------------------
9253 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
9254 ----------------------------------------------------------
9256 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
9257 (Obj_Decl : Node_Id;
9258 Function_Call : Node_Id)
9260 BIP_Func_Call : constant Node_Id :=
9261 Unqual_BIP_Iface_Function_Call (Function_Call);
9262 Loc : constant Source_Ptr := Sloc (Function_Call);
9263 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
9265 Tmp_Decl : Node_Id;
9266 Tmp_Id : Entity_Id;
9268 begin
9269 -- No action of the call has already been processed
9271 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9272 return;
9273 end if;
9275 Tmp_Id := Make_Temporary (Loc, 'D');
9277 -- Insert a temporary before N initialized with the BIP function call
9278 -- without its enclosing type conversions and analyze it without its
9279 -- expansion. This temporary facilitates us reusing the BIP machinery,
9280 -- which takes care of adding the extra build-in-place actuals and
9281 -- transforms this object declaration into an object renaming
9282 -- declaration.
9284 Tmp_Decl :=
9285 Make_Object_Declaration (Loc,
9286 Defining_Identifier => Tmp_Id,
9287 Object_Definition =>
9288 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
9289 Expression => New_Copy_Tree (BIP_Func_Call));
9291 Expander_Mode_Save_And_Set (False);
9292 Insert_Action (Obj_Decl, Tmp_Decl);
9293 Expander_Mode_Restore;
9295 -- Inherit Is_Return_Object from the parent object to the temp object,
9296 -- so that Make_In_Build_Place_Call_In_Object_Declaration will handle
9297 -- the temp properly in cases where there's a BIP_Alloc_Form formal of
9298 -- an enclosing function that should be passed along (and which also
9299 -- ensures that if the BIP call is used as a function result and it
9300 -- requires finalization, then it will not be finalized prematurely
9301 -- or redundantly).
9303 Set_Is_Return_Object (Tmp_Id, Is_Return_Object (Obj_Id));
9305 Make_Build_In_Place_Call_In_Object_Declaration
9306 (Obj_Decl => Tmp_Decl,
9307 Function_Call => Expression (Tmp_Decl));
9309 pragma Assert (Nkind (Tmp_Decl) = N_Object_Renaming_Declaration);
9311 -- Replace the original build-in-place function call by a reference to
9312 -- the resulting temporary object renaming declaration. In this way,
9313 -- all the interface conversions performed in the original Function_Call
9314 -- on the build-in-place object are preserved.
9316 Rewrite (BIP_Func_Call, New_Occurrence_Of (Tmp_Id, Loc));
9318 -- Replace the original object declaration by an internal object
9319 -- renaming declaration. This leaves the generated code more clean (the
9320 -- build-in-place function call in an object renaming declaration and
9321 -- displacements of the pointer to the build-in-place object in another
9322 -- renaming declaration) and allows us to invoke the routine that takes
9323 -- care of replacing the identifier of the renaming declaration (routine
9324 -- originally developed for the regular build-in-place management).
9326 Rewrite (Obj_Decl,
9327 Make_Object_Renaming_Declaration (Loc,
9328 Defining_Identifier => Make_Temporary (Loc, 'D'),
9329 Subtype_Mark => New_Occurrence_Of (Etype (Obj_Id), Loc),
9330 Name => Function_Call));
9331 Analyze (Obj_Decl);
9333 Replace_Renaming_Declaration_Id (Obj_Decl, Original_Node (Obj_Decl));
9334 end Make_Build_In_Place_Iface_Call_In_Object_Declaration;
9336 --------------------------------------------
9337 -- Make_CPP_Constructor_Call_In_Allocator --
9338 --------------------------------------------
9340 procedure Make_CPP_Constructor_Call_In_Allocator
9341 (Allocator : Node_Id;
9342 Function_Call : Node_Id)
9344 Loc : constant Source_Ptr := Sloc (Function_Call);
9345 Acc_Type : constant Entity_Id := Etype (Allocator);
9346 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9347 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9349 New_Allocator : Node_Id;
9350 Return_Obj_Access : Entity_Id;
9351 Tmp_Obj : Node_Id;
9353 begin
9354 pragma Assert (Nkind (Allocator) = N_Allocator
9355 and then Nkind (Function_Call) = N_Function_Call);
9356 pragma Assert (Convention (Function_Id) = Convention_CPP
9357 and then Is_Constructor (Function_Id));
9358 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9360 -- Replace the initialized allocator of form "new T'(Func (...))" with
9361 -- an uninitialized allocator of form "new T", where T is the result
9362 -- subtype of the called function. The call to the function is handled
9363 -- separately further below.
9365 New_Allocator :=
9366 Make_Allocator (Loc,
9367 Expression => New_Occurrence_Of (Result_Subt, Loc));
9368 Set_No_Initialization (New_Allocator);
9370 -- Copy attributes to new allocator. Note that the new allocator
9371 -- logically comes from source if the original one did, so copy the
9372 -- relevant flag. This ensures proper treatment of the restriction
9373 -- No_Implicit_Heap_Allocations in this case.
9375 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9376 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9377 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9379 Rewrite (Allocator, New_Allocator);
9381 -- Create a new access object and initialize it to the result of the
9382 -- new uninitialized allocator. Note: we do not use Allocator as the
9383 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9384 -- as this would create a sort of infinite "recursion".
9386 Return_Obj_Access := Make_Temporary (Loc, 'R');
9387 Set_Etype (Return_Obj_Access, Acc_Type);
9389 -- Generate:
9390 -- Rnnn : constant ptr_T := new (T);
9391 -- Init (Rnn.all,...);
9393 Tmp_Obj :=
9394 Make_Object_Declaration (Loc,
9395 Defining_Identifier => Return_Obj_Access,
9396 Constant_Present => True,
9397 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9398 Expression => Relocate_Node (Allocator));
9399 Insert_Action (Allocator, Tmp_Obj);
9401 Insert_List_After_And_Analyze (Tmp_Obj,
9402 Build_Initialization_Call (Allocator,
9403 Id_Ref =>
9404 Make_Explicit_Dereference (Loc,
9405 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
9406 Typ => Etype (Function_Id),
9407 Constructor_Ref => Function_Call));
9409 -- Finally, replace the allocator node with a reference to the result of
9410 -- the function call itself (which will effectively be an access to the
9411 -- object created by the allocator).
9413 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
9415 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9416 -- generate an implicit conversion to force displacement of the "this"
9417 -- pointer.
9419 if Is_Interface (Designated_Type (Acc_Type)) then
9420 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9421 end if;
9423 Analyze_And_Resolve (Allocator, Acc_Type);
9424 end Make_CPP_Constructor_Call_In_Allocator;
9426 ----------------------
9427 -- Might_Have_Tasks --
9428 ----------------------
9430 function Might_Have_Tasks (Typ : Entity_Id) return Boolean is
9431 begin
9432 return not Global_No_Tasking
9433 and then not No_Run_Time_Mode
9434 and then (Has_Task (Typ)
9435 or else (Is_Class_Wide_Type (Typ)
9436 and then Is_Limited_Record (Typ)
9437 and then not Has_Aspect
9438 (Etype (Typ), Aspect_No_Task_Parts)));
9439 end Might_Have_Tasks;
9441 ----------------------------
9442 -- Needs_BIP_Task_Actuals --
9443 ----------------------------
9445 function Needs_BIP_Task_Actuals (Func_Id : Entity_Id) return Boolean is
9446 Subp_Id : Entity_Id;
9447 Func_Typ : Entity_Id;
9449 begin
9450 if Global_No_Tasking or else No_Run_Time_Mode then
9451 return False;
9452 end if;
9454 -- For thunks we must rely on their target entity; otherwise, given that
9455 -- the profile of thunks for functions returning a limited interface
9456 -- type returns a class-wide type, we would erroneously add these extra
9457 -- formals.
9459 if Is_Thunk (Func_Id) then
9460 Subp_Id := Thunk_Target (Func_Id);
9462 -- Common case
9464 else
9465 Subp_Id := Func_Id;
9466 end if;
9468 Func_Typ := Underlying_Type (Etype (Subp_Id));
9470 -- Functions returning types with foreign convention don't have extra
9471 -- formals.
9473 if Has_Foreign_Convention (Func_Typ) then
9474 return False;
9476 -- At first sight, for all the following cases, we could add assertions
9477 -- to ensure that if Func_Id is frozen then the computed result matches
9478 -- with the availability of the task master extra formal; unfortunately
9479 -- this is not feasible because we may be precisely freezing this entity
9480 -- (that is, Is_Frozen has been set by Freeze_Entity but it has not
9481 -- completed its work).
9483 elsif Has_Task (Func_Typ) then
9484 return True;
9486 elsif Ekind (Func_Id) = E_Function then
9487 return Might_Have_Tasks (Func_Typ);
9489 -- Handle subprogram type internally generated for dispatching call. We
9490 -- cannot rely on the return type of the subprogram type of dispatching
9491 -- calls since it is always a class-wide type (cf. Expand_Dispatching_
9492 -- Call).
9494 elsif Ekind (Func_Id) = E_Subprogram_Type then
9495 if Is_Dispatch_Table_Entity (Func_Id) then
9496 return Has_BIP_Extra_Formal (Func_Id, BIP_Task_Master);
9497 else
9498 return Might_Have_Tasks (Func_Typ);
9499 end if;
9501 else
9502 raise Program_Error;
9503 end if;
9504 end Needs_BIP_Task_Actuals;
9506 --------------------------
9507 -- Needs_BIP_Collection --
9508 --------------------------
9510 function Needs_BIP_Collection (Func_Id : Entity_Id) return Boolean is
9511 Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9513 begin
9514 -- A formal for the finalization collection is needed for build-in-place
9515 -- functions whose result type needs finalization or is a tagged type.
9516 -- Tagged primitive build-in-place functions need such a formal because
9517 -- they can be called by a dispatching call, and extensions may require
9518 -- finalization even if the root type doesn't. This means nonprimitive
9519 -- build-in-place functions with tagged results also need it, since such
9520 -- functions can be called via access-to-function types, and those can
9521 -- be used to call primitives, so the formal needs to be passed to all
9522 -- such build-in-place functions, primitive or not.
9524 return not Restriction_Active (No_Finalization)
9525 and then ((Needs_Finalization (Typ)
9526 and then not Has_Relaxed_Finalization (Typ))
9527 or else Is_Tagged_Type (Typ))
9528 and then not Has_Foreign_Convention (Typ);
9529 end Needs_BIP_Collection;
9531 --------------------------
9532 -- Needs_BIP_Alloc_Form --
9533 --------------------------
9535 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9536 Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9538 begin
9539 -- See Make_Build_In_Place_Call_In_Allocator for the rationale
9541 if Needs_BIP_Collection (Func_Id) then
9542 return True;
9543 end if;
9545 -- A formal giving the allocation method is needed for build-in-place
9546 -- functions whose result type is returned on the secondary stack or
9547 -- is a tagged type. Tagged primitive build-in-place functions need
9548 -- such a formal because they can be called by a dispatching call, and
9549 -- the secondary stack is always used for dispatching-on-result calls.
9550 -- This means nonprimitive build-in-place functions with tagged results
9551 -- also need it, as such functions can be called via access-to-function
9552 -- types, and those can be used to call primitives, so the formal needs
9553 -- to be passed to all such build-in-place functions, primitive or not.
9555 -- We never use build-in-place if the function has foreign convention,
9556 -- but note that it is OK for a build-in-place function to return a
9557 -- type with a foreign convention because the machinery ensures there
9558 -- is no copying.
9560 return not Restriction_Active (No_Secondary_Stack)
9561 and then (Needs_Secondary_Stack (Typ) or else Is_Tagged_Type (Typ))
9562 and then not Has_Foreign_Convention (Func_Id);
9563 end Needs_BIP_Alloc_Form;
9565 -------------------------------------
9566 -- Replace_Renaming_Declaration_Id --
9567 -------------------------------------
9569 procedure Replace_Renaming_Declaration_Id
9570 (New_Decl : Node_Id;
9571 Orig_Decl : Node_Id)
9573 New_Id : constant Entity_Id := Defining_Entity (New_Decl);
9574 Orig_Id : constant Entity_Id := Defining_Entity (Orig_Decl);
9576 begin
9577 Set_Chars (New_Id, Chars (Orig_Id));
9579 -- Swap next entity links in preparation for exchanging entities
9581 declare
9582 Next_Id : constant Entity_Id := Next_Entity (New_Id);
9583 begin
9584 Link_Entities (New_Id, Next_Entity (Orig_Id));
9585 Link_Entities (Orig_Id, Next_Id);
9586 end;
9588 Set_Homonym (New_Id, Homonym (Orig_Id));
9589 Exchange_Entities (New_Id, Orig_Id);
9591 -- Preserve source indication of original declaration, so that xref
9592 -- information is properly generated for the right entity.
9594 Preserve_Comes_From_Source (New_Decl, Orig_Decl);
9595 Preserve_Comes_From_Source (Orig_Id, Orig_Decl);
9597 Set_Comes_From_Source (New_Id, False);
9599 -- Preserve aliased indication
9601 Set_Is_Aliased (Orig_Id, Is_Aliased (New_Id));
9602 end Replace_Renaming_Declaration_Id;
9604 ------------------------------------
9605 -- Set_Enclosing_Sec_Stack_Return --
9606 ------------------------------------
9608 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
9609 P : Node_Id := N;
9611 begin
9612 -- Due to a possible mix of internally generated blocks, source blocks
9613 -- and loops, the scope stack may not be contiguous as all labels are
9614 -- inserted at the top level within the related function. Instead,
9615 -- perform a parent-based traversal and mark all appropriate constructs.
9617 while Present (P) loop
9619 -- Mark the label of a source or internally generated block or
9620 -- loop.
9622 if Nkind (P) in N_Block_Statement | N_Loop_Statement then
9623 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
9625 -- Mark the enclosing function
9627 elsif Nkind (P) = N_Subprogram_Body then
9628 if Present (Corresponding_Spec (P)) then
9629 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
9630 else
9631 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
9632 end if;
9634 -- Do not go beyond the enclosing function
9636 exit;
9637 end if;
9639 P := Parent (P);
9640 end loop;
9641 end Set_Enclosing_Sec_Stack_Return;
9643 ------------------------------------
9644 -- Unqual_BIP_Iface_Function_Call --
9645 ------------------------------------
9647 function Unqual_BIP_Iface_Function_Call (Expr : Node_Id) return Node_Id is
9648 Has_Pointer_Displacement : Boolean := False;
9649 On_Object_Declaration : Boolean := False;
9650 -- Remember if processing the renaming expressions on recursion we have
9651 -- traversed an object declaration, since we can traverse many object
9652 -- declaration renamings but just one regular object declaration.
9654 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id;
9655 -- Search for a build-in-place function call skipping any qualification
9656 -- including qualified expressions, type conversions, references, calls
9657 -- to displace the pointer to the object, and renamings. Return Empty if
9658 -- no build-in-place function call is found.
9660 ------------------------------
9661 -- Unqual_BIP_Function_Call --
9662 ------------------------------
9664 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id is
9665 begin
9666 -- Recurse to handle case of multiple levels of qualification and/or
9667 -- conversion.
9669 if Nkind (Expr) in N_Qualified_Expression
9670 | N_Type_Conversion
9671 | N_Unchecked_Type_Conversion
9672 then
9673 return Unqual_BIP_Function_Call (Expression (Expr));
9675 -- Recurse to handle case of multiple levels of references and
9676 -- explicit dereferences.
9678 elsif Nkind (Expr) in N_Attribute_Reference
9679 | N_Explicit_Dereference
9680 | N_Reference
9681 then
9682 return Unqual_BIP_Function_Call (Prefix (Expr));
9684 -- Recurse on object renamings
9686 elsif Nkind (Expr) = N_Identifier
9687 and then Present (Entity (Expr))
9688 and then Ekind (Entity (Expr)) in E_Constant | E_Variable
9689 and then Nkind (Parent (Entity (Expr))) =
9690 N_Object_Renaming_Declaration
9691 and then Present (Renamed_Object (Entity (Expr)))
9692 then
9693 return Unqual_BIP_Function_Call (Renamed_Object (Entity (Expr)));
9695 -- Recurse on the initializing expression of the first reference of
9696 -- an object declaration.
9698 elsif not On_Object_Declaration
9699 and then Nkind (Expr) = N_Identifier
9700 and then Present (Entity (Expr))
9701 and then Ekind (Entity (Expr)) in E_Constant | E_Variable
9702 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
9703 and then Present (Expression (Parent (Entity (Expr))))
9704 then
9705 On_Object_Declaration := True;
9706 return
9707 Unqual_BIP_Function_Call (Expression (Parent (Entity (Expr))));
9709 -- Recurse to handle calls to displace the pointer to the object to
9710 -- reference a secondary dispatch table.
9712 elsif Nkind (Expr) = N_Function_Call
9713 and then Nkind (Name (Expr)) in N_Has_Entity
9714 and then Present (Entity (Name (Expr)))
9715 and then Is_RTE (Entity (Name (Expr)), RE_Displace)
9716 then
9717 Has_Pointer_Displacement := True;
9718 return
9719 Unqual_BIP_Function_Call (First (Parameter_Associations (Expr)));
9721 -- Normal case: check if the inner expression is a BIP function call
9722 -- and the pointer to the object is displaced.
9724 elsif Has_Pointer_Displacement
9725 and then Is_Build_In_Place_Function_Call (Expr)
9726 then
9727 return Expr;
9729 else
9730 return Empty;
9731 end if;
9732 end Unqual_BIP_Function_Call;
9734 -- Start of processing for Unqual_BIP_Iface_Function_Call
9736 begin
9737 if Nkind (Expr) = N_Identifier and then No (Entity (Expr)) then
9739 -- Can happen for X'Elab_Spec in the binder-generated file
9741 return Empty;
9742 end if;
9744 return Unqual_BIP_Function_Call (Expr);
9745 end Unqual_BIP_Iface_Function_Call;
9747 -------------------------------
9748 -- Validate_Subprogram_Calls --
9749 -------------------------------
9751 procedure Validate_Subprogram_Calls (N : Node_Id) is
9753 function Process_Node (Nod : Node_Id) return Traverse_Result;
9754 -- Function to traverse the subtree of N using Traverse_Proc.
9756 ------------------
9757 -- Process_Node --
9758 ------------------
9760 function Process_Node (Nod : Node_Id) return Traverse_Result is
9761 begin
9762 case Nkind (Nod) is
9763 when N_Entry_Call_Statement
9764 | N_Procedure_Call_Statement
9765 | N_Function_Call
9767 declare
9768 Call_Node : Node_Id renames Nod;
9769 Subp : Entity_Id;
9771 begin
9772 -- Call using access to subprogram with explicit dereference
9774 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
9775 Subp := Etype (Name (Call_Node));
9777 -- Prefix notation calls
9779 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
9780 Subp := Entity (Selector_Name (Name (Call_Node)));
9782 -- Call to member of entry family, where Name is an indexed
9783 -- component, with the prefix being a selected component
9784 -- giving the task and entry family name, and the index
9785 -- being the entry index.
9787 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
9788 Subp :=
9789 Entity (Selector_Name (Prefix (Name (Call_Node))));
9791 -- Normal case
9793 else
9794 Subp := Entity (Name (Call_Node));
9795 end if;
9797 pragma Assert (Check_BIP_Actuals (Call_Node, Subp));
9798 end;
9800 -- Skip generic bodies
9802 when N_Package_Body =>
9803 if Ekind (Unique_Defining_Entity (Nod)) = E_Generic_Package then
9804 return Skip;
9805 end if;
9807 when N_Subprogram_Body =>
9808 if Ekind (Unique_Defining_Entity (Nod)) in E_Generic_Function
9809 | E_Generic_Procedure
9810 then
9811 return Skip;
9812 end if;
9814 -- Nodes we want to ignore
9816 -- Skip calls placed in the full declaration of record types since
9817 -- the call will be performed by their Init Proc; for example,
9818 -- calls initializing default values of discriminants or calls
9819 -- providing the initial value of record type components. Other
9820 -- full type declarations are processed because they may have
9821 -- calls that must be checked. For example:
9823 -- type T is array (1 .. Some_Function_Call (...)) of Some_Type;
9825 -- ??? More work needed here to handle the following case:
9827 -- type Rec is record
9828 -- F : String (1 .. <some complicated expression>);
9829 -- end record;
9831 when N_Full_Type_Declaration =>
9832 if Is_Record_Type (Defining_Entity (Nod)) then
9833 return Skip;
9834 end if;
9836 -- Skip calls placed in subprogram specifications since function
9837 -- calls initializing default parameter values will be processed
9838 -- when the call to the subprogram is found (if the default actual
9839 -- parameter is required), and calls found in aspects will be
9840 -- processed when their corresponding pragma is found, or in the
9841 -- specific case of class-wide pre-/postconditions, when their
9842 -- helpers are found.
9844 when N_Procedure_Specification
9845 | N_Function_Specification
9847 return Skip;
9849 when N_Abstract_Subprogram_Declaration
9850 | N_Aspect_Specification
9851 | N_At_Clause
9852 | N_Call_Marker
9853 | N_Empty
9854 | N_Enumeration_Representation_Clause
9855 | N_Enumeration_Type_Definition
9856 | N_Function_Instantiation
9857 | N_Freeze_Generic_Entity
9858 | N_Generic_Function_Renaming_Declaration
9859 | N_Generic_Package_Renaming_Declaration
9860 | N_Generic_Procedure_Renaming_Declaration
9861 | N_Generic_Package_Declaration
9862 | N_Generic_Subprogram_Declaration
9863 | N_Itype_Reference
9864 | N_Number_Declaration
9865 | N_Package_Instantiation
9866 | N_Package_Renaming_Declaration
9867 | N_Pragma
9868 | N_Procedure_Instantiation
9869 | N_Protected_Type_Declaration
9870 | N_Record_Representation_Clause
9871 | N_Validate_Unchecked_Conversion
9872 | N_Variable_Reference_Marker
9873 | N_Use_Package_Clause
9874 | N_Use_Type_Clause
9875 | N_With_Clause
9877 return Skip;
9879 when others =>
9880 null;
9881 end case;
9883 return OK;
9884 end Process_Node;
9886 procedure Check_Calls is new Traverse_Proc (Process_Node);
9888 -- Start of processing for Validate_Subprogram_Calls
9890 begin
9891 -- No action required if we are not generating code or compiling sources
9892 -- that have errors.
9894 if Serious_Errors_Detected > 0
9895 or else Operating_Mode /= Generate_Code
9896 then
9897 return;
9898 end if;
9900 Check_Calls (N);
9901 end Validate_Subprogram_Calls;
9903 --------------
9904 -- Warn_BIP --
9905 --------------
9907 procedure Warn_BIP (Func_Call : Node_Id) is
9908 begin
9909 if Debug_Flag_Underscore_BB then
9910 Error_Msg_N ("build-in-place function call??", Func_Call);
9911 end if;
9912 end Warn_BIP;
9914 end Exp_Ch6;