[AArch64] Remove use of wider vector modes
[official-gcc.git] / gcc / ada / exp_ch6.adb
blob3fb546805ff39b65e9aa733c5284c9e5a1bcdc67
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-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Contracts; use Contracts;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Elists; use Elists;
33 with Exp_Aggr; use Exp_Aggr;
34 with Exp_Atag; use Exp_Atag;
35 with Exp_Ch2; use Exp_Ch2;
36 with Exp_Ch3; use Exp_Ch3;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Dbug; use Exp_Dbug;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Dist; use Exp_Dist;
42 with Exp_Intr; use Exp_Intr;
43 with Exp_Pakd; use Exp_Pakd;
44 with Exp_Tss; use Exp_Tss;
45 with Exp_Util; use Exp_Util;
46 with Freeze; use Freeze;
47 with Inline; use Inline;
48 with Lib; use Lib;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sem; use Sem;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch12; use Sem_Ch12;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Res; use Sem_Res;
68 with Sem_SCIL; use Sem_SCIL;
69 with Sem_Util; use Sem_Util;
70 with Sinfo; use Sinfo;
71 with Snames; use Snames;
72 with Stand; use Stand;
73 with Targparm; use Targparm;
74 with Tbuild; use Tbuild;
75 with Uintp; use Uintp;
76 with Validsw; use Validsw;
78 package body Exp_Ch6 is
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Add_Access_Actual_To_Build_In_Place_Call
85 (Function_Call : Node_Id;
86 Function_Id : Entity_Id;
87 Return_Object : Node_Id;
88 Is_Access : Boolean := False);
89 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
90 -- object name given by Return_Object and add the attribute to the end of
91 -- the actual parameter list associated with the build-in-place function
92 -- call denoted by Function_Call. However, if Is_Access is True, then
93 -- Return_Object is already an access expression, in which case it's passed
94 -- along directly to the build-in-place function. Finally, if Return_Object
95 -- is empty, then pass a null literal as the actual.
97 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
98 (Function_Call : Node_Id;
99 Function_Id : Entity_Id;
100 Alloc_Form : BIP_Allocation_Form := Unspecified;
101 Alloc_Form_Exp : Node_Id := Empty;
102 Pool_Actual : Node_Id := Make_Null (No_Location));
103 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
104 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
105 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
106 -- otherwise pass a literal corresponding to the Alloc_Form parameter
107 -- (which must not be Unspecified in that case). Pool_Actual is the
108 -- parameter to pass to BIP_Storage_Pool.
110 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
111 (Func_Call : Node_Id;
112 Func_Id : Entity_Id;
113 Ptr_Typ : Entity_Id := Empty;
114 Master_Exp : Node_Id := Empty);
115 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
116 -- finalization actions, add an actual parameter which is a pointer to the
117 -- finalization master of the caller. If Master_Exp is not Empty, then that
118 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
119 -- will result in an automatic "null" value for the actual.
121 procedure Add_Task_Actuals_To_Build_In_Place_Call
122 (Function_Call : Node_Id;
123 Function_Id : Entity_Id;
124 Master_Actual : Node_Id;
125 Chain : Node_Id := Empty);
126 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
127 -- contains tasks, add two actual parameters: the master, and a pointer to
128 -- the caller's activation chain. Master_Actual is the actual parameter
129 -- expression to pass for the master. In most cases, this is the current
130 -- master (_master). The two exceptions are: If the function call is the
131 -- initialization expression for an allocator, we pass the master of the
132 -- access type. If the function call is the initialization expression for a
133 -- return object, we pass along the master passed in by the caller. In most
134 -- contexts, the activation chain to pass is the local one, which is
135 -- indicated by No (Chain). However, in an allocator, the caller passes in
136 -- the activation Chain. Note: Master_Actual can be Empty, but only if
137 -- there are no tasks.
139 procedure Check_Overriding_Operation (Subp : Entity_Id);
140 -- Subp is a dispatching operation. Check whether it may override an
141 -- inherited private operation, in which case its DT entry is that of
142 -- the hidden operation, not the one it may have received earlier.
143 -- This must be done before emitting the code to set the corresponding
144 -- DT to the address of the subprogram. The actual placement of Subp in
145 -- the proper place in the list of primitive operations is done in
146 -- Declare_Inherited_Private_Subprograms, which also has to deal with
147 -- implicit operations. This duplication is unavoidable for now???
149 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
150 -- This procedure is called only if the subprogram body N, whose spec
151 -- has the given entity Spec, contains a parameterless recursive call.
152 -- It attempts to generate runtime code to detect if this a case of
153 -- infinite recursion.
155 -- The body is scanned to determine dependencies. If the only external
156 -- dependencies are on a small set of scalar variables, then the values
157 -- of these variables are captured on entry to the subprogram, and if
158 -- the values are not changed for the call, we know immediately that
159 -- we have an infinite recursion.
161 procedure Expand_Actuals
162 (N : Node_Id;
163 Subp : Entity_Id;
164 Post_Call : out List_Id);
165 -- Return a list of actions to take place after the call in Post_Call. The
166 -- call will later be rewritten as an Expression_With_Actions, with the
167 -- Post_Call actions inserted, and the call inside.
169 -- For each actual of an in-out or out parameter which is a numeric (view)
170 -- conversion of the form T (A), where A denotes a variable, we insert the
171 -- declaration:
173 -- Temp : T[ := T (A)];
175 -- prior to the call. Then we replace the actual with a reference to Temp,
176 -- and append the assignment:
178 -- A := TypeA (Temp);
180 -- after the call. Here TypeA is the actual type of variable A. For out
181 -- parameters, the initial declaration has no expression. If A is not an
182 -- entity name, we generate instead:
184 -- Var : TypeA renames A;
185 -- Temp : T := Var; -- omitting expression for out parameter.
186 -- ...
187 -- Var := TypeA (Temp);
189 -- For other in-out parameters, we emit the required constraint checks
190 -- before and/or after the call.
192 -- For all parameter modes, actuals that denote components and slices of
193 -- packed arrays are expanded into suitable temporaries.
195 -- For non-scalar objects that are possibly unaligned, add call by copy
196 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
198 -- For OUT and IN OUT parameters, add predicate checks after the call
199 -- based on the predicates of the actual type.
201 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id);
202 -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals.
204 procedure Expand_Ctrl_Function_Call (N : Node_Id);
205 -- N is a function call which returns a controlled object. Transform the
206 -- call into a temporary which retrieves the returned object from the
207 -- secondary stack using 'reference.
209 procedure Expand_Non_Function_Return (N : Node_Id);
210 -- Expand a simple return statement found in a procedure body, entry body,
211 -- accept statement, or an extended return statement. Note that all non-
212 -- function returns are simple return statements.
214 function Expand_Protected_Object_Reference
215 (N : Node_Id;
216 Scop : Entity_Id) return Node_Id;
218 procedure Expand_Protected_Subprogram_Call
219 (N : Node_Id;
220 Subp : Entity_Id;
221 Scop : Entity_Id);
222 -- A call to a protected subprogram within the protected object may appear
223 -- as a regular call. The list of actuals must be expanded to contain a
224 -- reference to the object itself, and the call becomes a call to the
225 -- corresponding protected subprogram.
227 procedure Expand_Simple_Function_Return (N : Node_Id);
228 -- Expand simple return from function. In the case where we are returning
229 -- from a function body this is called by Expand_N_Simple_Return_Statement.
231 function Has_Unconstrained_Access_Discriminants
232 (Subtyp : Entity_Id) return Boolean;
233 -- Returns True if the given subtype is unconstrained and has one or more
234 -- access discriminants.
236 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id);
237 -- Insert the Post_Call list previously produced by routine Expand_Actuals
238 -- or Expand_Call_Helper into the tree.
240 procedure Rewrite_Function_Call_For_C (N : Node_Id);
241 -- When generating C code, replace a call to a function that returns an
242 -- array into the generated procedure with an additional out parameter.
244 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id);
245 -- N is a return statement for a function that returns its result on the
246 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
247 -- function and all blocks and loops that the return statement is jumping
248 -- out of. This ensures that the secondary stack is not released; otherwise
249 -- the function result would be reclaimed before returning to the caller.
251 ----------------------------------------------
252 -- Add_Access_Actual_To_Build_In_Place_Call --
253 ----------------------------------------------
255 procedure Add_Access_Actual_To_Build_In_Place_Call
256 (Function_Call : Node_Id;
257 Function_Id : Entity_Id;
258 Return_Object : Node_Id;
259 Is_Access : Boolean := False)
261 Loc : constant Source_Ptr := Sloc (Function_Call);
262 Obj_Address : Node_Id;
263 Obj_Acc_Formal : Entity_Id;
265 begin
266 -- Locate the implicit access parameter in the called function
268 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
270 -- If no return object is provided, then pass null
272 if not Present (Return_Object) then
273 Obj_Address := Make_Null (Loc);
274 Set_Parent (Obj_Address, Function_Call);
276 -- If Return_Object is already an expression of an access type, then use
277 -- it directly, since it must be an access value denoting the return
278 -- object, and couldn't possibly be the return object itself.
280 elsif Is_Access then
281 Obj_Address := Return_Object;
282 Set_Parent (Obj_Address, Function_Call);
284 -- Apply Unrestricted_Access to caller's return object
286 else
287 Obj_Address :=
288 Make_Attribute_Reference (Loc,
289 Prefix => Return_Object,
290 Attribute_Name => Name_Unrestricted_Access);
292 Set_Parent (Return_Object, Obj_Address);
293 Set_Parent (Obj_Address, Function_Call);
294 end if;
296 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
298 -- Build the parameter association for the new actual and add it to the
299 -- end of the function's actuals.
301 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
302 end Add_Access_Actual_To_Build_In_Place_Call;
304 ------------------------------------------------------
305 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
306 ------------------------------------------------------
308 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
309 (Function_Call : Node_Id;
310 Function_Id : Entity_Id;
311 Alloc_Form : BIP_Allocation_Form := Unspecified;
312 Alloc_Form_Exp : Node_Id := Empty;
313 Pool_Actual : Node_Id := Make_Null (No_Location))
315 Loc : constant Source_Ptr := Sloc (Function_Call);
316 Alloc_Form_Actual : Node_Id;
317 Alloc_Form_Formal : Node_Id;
318 Pool_Formal : Node_Id;
320 begin
321 -- The allocation form generally doesn't need to be passed in the case
322 -- of a constrained result subtype, since normally the caller performs
323 -- the allocation in that case. However this formal is still needed in
324 -- the case where the function has a tagged result, because generally
325 -- such functions can be called in a dispatching context and such calls
326 -- must be handled like calls to class-wide functions.
328 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
329 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
330 then
331 return;
332 end if;
334 -- Locate the implicit allocation form parameter in the called function.
335 -- Maybe it would be better for each implicit formal of a build-in-place
336 -- function to have a flag or a Uint attribute to identify it. ???
338 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
340 if Present (Alloc_Form_Exp) then
341 pragma Assert (Alloc_Form = Unspecified);
343 Alloc_Form_Actual := Alloc_Form_Exp;
345 else
346 pragma Assert (Alloc_Form /= Unspecified);
348 Alloc_Form_Actual :=
349 Make_Integer_Literal (Loc,
350 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
351 end if;
353 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
355 -- Build the parameter association for the new actual and add it to the
356 -- end of the function's actuals.
358 Add_Extra_Actual_To_Call
359 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
361 -- Pass the Storage_Pool parameter. This parameter is omitted on
362 -- ZFP as those targets do not support pools.
364 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
365 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
366 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
367 Add_Extra_Actual_To_Call
368 (Function_Call, Pool_Formal, Pool_Actual);
369 end if;
370 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
372 -----------------------------------------------------------
373 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
374 -----------------------------------------------------------
376 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
377 (Func_Call : Node_Id;
378 Func_Id : Entity_Id;
379 Ptr_Typ : Entity_Id := Empty;
380 Master_Exp : Node_Id := Empty)
382 begin
383 if not Needs_BIP_Finalization_Master (Func_Id) then
384 return;
385 end if;
387 declare
388 Formal : constant Entity_Id :=
389 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
390 Loc : constant Source_Ptr := Sloc (Func_Call);
392 Actual : Node_Id;
393 Desig_Typ : Entity_Id;
395 begin
396 -- If there is a finalization master actual, such as the implicit
397 -- finalization master of an enclosing build-in-place function,
398 -- then this must be added as an extra actual of the call.
400 if Present (Master_Exp) then
401 Actual := Master_Exp;
403 -- Case where the context does not require an actual master
405 elsif No (Ptr_Typ) then
406 Actual := Make_Null (Loc);
408 else
409 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
411 -- Check for a library-level access type whose designated type has
412 -- suppressed finalization or the access type is subject to pragma
413 -- No_Heap_Finalization. Such an access type lacks a master. Pass
414 -- a null actual to callee in order to signal a missing master.
416 if Is_Library_Level_Entity (Ptr_Typ)
417 and then (Finalize_Storage_Only (Desig_Typ)
418 or else No_Heap_Finalization (Ptr_Typ))
419 then
420 Actual := Make_Null (Loc);
422 -- Types in need of finalization actions
424 elsif Needs_Finalization (Desig_Typ) then
426 -- The general mechanism of creating finalization masters for
427 -- anonymous access types is disabled by default, otherwise
428 -- finalization masters will pop all over the place. Such types
429 -- use context-specific masters.
431 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
432 and then No (Finalization_Master (Ptr_Typ))
433 then
434 Build_Anonymous_Master (Ptr_Typ);
435 end if;
437 -- Access-to-controlled types should always have a master
439 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
441 Actual :=
442 Make_Attribute_Reference (Loc,
443 Prefix =>
444 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
445 Attribute_Name => Name_Unrestricted_Access);
447 -- Tagged types
449 else
450 Actual := Make_Null (Loc);
451 end if;
452 end if;
454 Analyze_And_Resolve (Actual, Etype (Formal));
456 -- Build the parameter association for the new actual and add it to
457 -- the end of the function's actuals.
459 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
460 end;
461 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
463 ------------------------------
464 -- Add_Extra_Actual_To_Call --
465 ------------------------------
467 procedure Add_Extra_Actual_To_Call
468 (Subprogram_Call : Node_Id;
469 Extra_Formal : Entity_Id;
470 Extra_Actual : Node_Id)
472 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
473 Param_Assoc : Node_Id;
475 begin
476 Param_Assoc :=
477 Make_Parameter_Association (Loc,
478 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
479 Explicit_Actual_Parameter => Extra_Actual);
481 Set_Parent (Param_Assoc, Subprogram_Call);
482 Set_Parent (Extra_Actual, Param_Assoc);
484 if Present (Parameter_Associations (Subprogram_Call)) then
485 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
486 N_Parameter_Association
487 then
489 -- Find last named actual, and append
491 declare
492 L : Node_Id;
493 begin
494 L := First_Actual (Subprogram_Call);
495 while Present (L) loop
496 if No (Next_Actual (L)) then
497 Set_Next_Named_Actual (Parent (L), Extra_Actual);
498 exit;
499 end if;
500 Next_Actual (L);
501 end loop;
502 end;
504 else
505 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
506 end if;
508 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
510 else
511 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
512 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
513 end if;
514 end Add_Extra_Actual_To_Call;
516 ---------------------------------------------
517 -- Add_Task_Actuals_To_Build_In_Place_Call --
518 ---------------------------------------------
520 procedure Add_Task_Actuals_To_Build_In_Place_Call
521 (Function_Call : Node_Id;
522 Function_Id : Entity_Id;
523 Master_Actual : Node_Id;
524 Chain : Node_Id := Empty)
526 Loc : constant Source_Ptr := Sloc (Function_Call);
527 Result_Subt : constant Entity_Id :=
528 Available_View (Etype (Function_Id));
529 Actual : Node_Id;
530 Chain_Actual : Node_Id;
531 Chain_Formal : Node_Id;
532 Master_Formal : Node_Id;
534 begin
535 -- No such extra parameters are needed if there are no tasks
537 if not Has_Task (Result_Subt) then
538 return;
539 end if;
541 Actual := Master_Actual;
543 -- Use a dummy _master actual in case of No_Task_Hierarchy
545 if Restriction_Active (No_Task_Hierarchy) then
546 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
548 -- In the case where we use the master associated with an access type,
549 -- the actual is an entity and requires an explicit reference.
551 elsif Nkind (Actual) = N_Defining_Identifier then
552 Actual := New_Occurrence_Of (Actual, Loc);
553 end if;
555 -- Locate the implicit master parameter in the called function
557 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
558 Analyze_And_Resolve (Actual, Etype (Master_Formal));
560 -- Build the parameter association for the new actual and add it to the
561 -- end of the function's actuals.
563 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
565 -- Locate the implicit activation chain parameter in the called function
567 Chain_Formal :=
568 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
570 -- Create the actual which is a pointer to the current activation chain
572 if No (Chain) then
573 Chain_Actual :=
574 Make_Attribute_Reference (Loc,
575 Prefix => Make_Identifier (Loc, Name_uChain),
576 Attribute_Name => Name_Unrestricted_Access);
578 -- Allocator case; make a reference to the Chain passed in by the caller
580 else
581 Chain_Actual :=
582 Make_Attribute_Reference (Loc,
583 Prefix => New_Occurrence_Of (Chain, Loc),
584 Attribute_Name => Name_Unrestricted_Access);
585 end if;
587 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
589 -- Build the parameter association for the new actual and add it to the
590 -- end of the function's actuals.
592 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
593 end Add_Task_Actuals_To_Build_In_Place_Call;
595 -----------------------
596 -- BIP_Formal_Suffix --
597 -----------------------
599 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
600 begin
601 case Kind is
602 when BIP_Alloc_Form =>
603 return "BIPalloc";
605 when BIP_Storage_Pool =>
606 return "BIPstoragepool";
608 when BIP_Finalization_Master =>
609 return "BIPfinalizationmaster";
611 when BIP_Task_Master =>
612 return "BIPtaskmaster";
614 when BIP_Activation_Chain =>
615 return "BIPactivationchain";
617 when BIP_Object_Access =>
618 return "BIPaccess";
619 end case;
620 end BIP_Formal_Suffix;
622 ---------------------------
623 -- Build_In_Place_Formal --
624 ---------------------------
626 function Build_In_Place_Formal
627 (Func : Entity_Id;
628 Kind : BIP_Formal_Kind) return Entity_Id
630 Formal_Name : constant Name_Id :=
631 New_External_Name
632 (Chars (Func), BIP_Formal_Suffix (Kind));
633 Extra_Formal : Entity_Id := Extra_Formals (Func);
635 begin
636 -- Maybe it would be better for each implicit formal of a build-in-place
637 -- function to have a flag or a Uint attribute to identify it. ???
639 -- The return type in the function declaration may have been a limited
640 -- view, and the extra formals for the function were not generated at
641 -- that point. At the point of call the full view must be available and
642 -- the extra formals can be created.
644 if No (Extra_Formal) then
645 Create_Extra_Formals (Func);
646 Extra_Formal := Extra_Formals (Func);
647 end if;
649 loop
650 pragma Assert (Present (Extra_Formal));
651 exit when Chars (Extra_Formal) = Formal_Name;
653 Next_Formal_With_Extras (Extra_Formal);
654 end loop;
656 return Extra_Formal;
657 end Build_In_Place_Formal;
659 -------------------------------
660 -- Build_Procedure_Body_Form --
661 -------------------------------
663 function Build_Procedure_Body_Form
664 (Func_Id : Entity_Id;
665 Func_Body : Node_Id) return Node_Id
667 Loc : constant Source_Ptr := Sloc (Func_Body);
669 Proc_Decl : constant Node_Id :=
670 Next (Unit_Declaration_Node (Func_Id));
671 -- It is assumed that the next node following the declaration of the
672 -- corresponding subprogram spec is the declaration of the procedure
673 -- form.
675 Proc_Id : constant Entity_Id := Defining_Entity (Proc_Decl);
677 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id);
678 -- Replace each return statement found in the list Stmts with an
679 -- assignment of the return expression to parameter Param_Id.
681 ---------------------
682 -- Replace_Returns --
683 ---------------------
685 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id) is
686 Stmt : Node_Id;
688 begin
689 Stmt := First (Stmts);
690 while Present (Stmt) loop
691 if Nkind (Stmt) = N_Block_Statement then
692 Replace_Returns (Param_Id, Statements (Stmt));
694 elsif Nkind (Stmt) = N_Case_Statement then
695 declare
696 Alt : Node_Id;
697 begin
698 Alt := First (Alternatives (Stmt));
699 while Present (Alt) loop
700 Replace_Returns (Param_Id, Statements (Alt));
701 Next (Alt);
702 end loop;
703 end;
705 elsif Nkind (Stmt) = N_Extended_Return_Statement then
706 declare
707 Ret_Obj : constant Entity_Id :=
708 Defining_Entity
709 (First (Return_Object_Declarations (Stmt)));
710 Assign : constant Node_Id :=
711 Make_Assignment_Statement (Sloc (Stmt),
712 Name =>
713 New_Occurrence_Of (Param_Id, Loc),
714 Expression =>
715 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
716 Stmts : List_Id;
718 begin
719 -- The extended return may just contain the declaration
721 if Present (Handled_Statement_Sequence (Stmt)) then
722 Stmts := Statements (Handled_Statement_Sequence (Stmt));
723 else
724 Stmts := New_List;
725 end if;
727 Set_Assignment_OK (Name (Assign));
729 Rewrite (Stmt,
730 Make_Block_Statement (Sloc (Stmt),
731 Declarations =>
732 Return_Object_Declarations (Stmt),
733 Handled_Statement_Sequence =>
734 Make_Handled_Sequence_Of_Statements (Loc,
735 Statements => Stmts)));
737 Replace_Returns (Param_Id, Stmts);
739 Append_To (Stmts, Assign);
740 Append_To (Stmts, Make_Simple_Return_Statement (Loc));
741 end;
743 elsif Nkind (Stmt) = N_If_Statement then
744 Replace_Returns (Param_Id, Then_Statements (Stmt));
745 Replace_Returns (Param_Id, Else_Statements (Stmt));
747 declare
748 Part : Node_Id;
749 begin
750 Part := First (Elsif_Parts (Stmt));
751 while Present (Part) loop
752 Replace_Returns (Param_Id, Then_Statements (Part));
753 Next (Part);
754 end loop;
755 end;
757 elsif Nkind (Stmt) = N_Loop_Statement then
758 Replace_Returns (Param_Id, Statements (Stmt));
760 elsif Nkind (Stmt) = N_Simple_Return_Statement then
762 -- Generate:
763 -- Param := Expr;
764 -- return;
766 Rewrite (Stmt,
767 Make_Assignment_Statement (Sloc (Stmt),
768 Name => New_Occurrence_Of (Param_Id, Loc),
769 Expression => Relocate_Node (Expression (Stmt))));
771 Insert_After (Stmt, Make_Simple_Return_Statement (Loc));
773 -- Skip the added return
775 Next (Stmt);
776 end if;
778 Next (Stmt);
779 end loop;
780 end Replace_Returns;
782 -- Local variables
784 Stmts : List_Id;
785 New_Body : Node_Id;
787 -- Start of processing for Build_Procedure_Body_Form
789 begin
790 -- This routine replaces the original function body:
792 -- function F (...) return Array_Typ is
793 -- begin
794 -- ...
795 -- return Something;
796 -- end F;
798 -- with the following:
800 -- procedure P (..., Result : out Array_Typ) is
801 -- begin
802 -- ...
803 -- Result := Something;
804 -- end P;
806 Stmts :=
807 Statements (Handled_Statement_Sequence (Func_Body));
808 Replace_Returns (Last_Entity (Proc_Id), Stmts);
810 New_Body :=
811 Make_Subprogram_Body (Loc,
812 Specification =>
813 Copy_Subprogram_Spec (Specification (Proc_Decl)),
814 Declarations => Declarations (Func_Body),
815 Handled_Statement_Sequence =>
816 Make_Handled_Sequence_Of_Statements (Loc,
817 Statements => Stmts));
819 -- If the function is a generic instance, so is the new procedure.
820 -- Set flag accordingly so that the proper renaming declarations are
821 -- generated.
823 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
824 return New_Body;
825 end Build_Procedure_Body_Form;
827 --------------------------------
828 -- Check_Overriding_Operation --
829 --------------------------------
831 procedure Check_Overriding_Operation (Subp : Entity_Id) is
832 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
833 Op_List : constant Elist_Id := Primitive_Operations (Typ);
834 Op_Elmt : Elmt_Id;
835 Prim_Op : Entity_Id;
836 Par_Op : Entity_Id;
838 begin
839 if Is_Derived_Type (Typ)
840 and then not Is_Private_Type (Typ)
841 and then In_Open_Scopes (Scope (Etype (Typ)))
842 and then Is_Base_Type (Typ)
843 then
844 -- Subp overrides an inherited private operation if there is an
845 -- inherited operation with a different name than Subp (see
846 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
847 -- same name as Subp.
849 Op_Elmt := First_Elmt (Op_List);
850 while Present (Op_Elmt) loop
851 Prim_Op := Node (Op_Elmt);
852 Par_Op := Alias (Prim_Op);
854 if Present (Par_Op)
855 and then not Comes_From_Source (Prim_Op)
856 and then Chars (Prim_Op) /= Chars (Par_Op)
857 and then Chars (Par_Op) = Chars (Subp)
858 and then Is_Hidden (Par_Op)
859 and then Type_Conformant (Prim_Op, Subp)
860 then
861 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
862 end if;
864 Next_Elmt (Op_Elmt);
865 end loop;
866 end if;
867 end Check_Overriding_Operation;
869 -------------------------------
870 -- Detect_Infinite_Recursion --
871 -------------------------------
873 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
874 Loc : constant Source_Ptr := Sloc (N);
876 Var_List : constant Elist_Id := New_Elmt_List;
877 -- List of globals referenced by body of procedure
879 Call_List : constant Elist_Id := New_Elmt_List;
880 -- List of recursive calls in body of procedure
882 Shad_List : constant Elist_Id := New_Elmt_List;
883 -- List of entity id's for entities created to capture the value of
884 -- referenced globals on entry to the procedure.
886 Scop : constant Uint := Scope_Depth (Spec);
887 -- This is used to record the scope depth of the current procedure, so
888 -- that we can identify global references.
890 Max_Vars : constant := 4;
891 -- Do not test more than four global variables
893 Count_Vars : Natural := 0;
894 -- Count variables found so far
896 Var : Entity_Id;
897 Elm : Elmt_Id;
898 Ent : Entity_Id;
899 Call : Elmt_Id;
900 Decl : Node_Id;
901 Test : Node_Id;
902 Elm1 : Elmt_Id;
903 Elm2 : Elmt_Id;
904 Last : Node_Id;
906 function Process (Nod : Node_Id) return Traverse_Result;
907 -- Function to traverse the subprogram body (using Traverse_Func)
909 -------------
910 -- Process --
911 -------------
913 function Process (Nod : Node_Id) return Traverse_Result is
914 begin
915 -- Procedure call
917 if Nkind (Nod) = N_Procedure_Call_Statement then
919 -- Case of one of the detected recursive calls
921 if Is_Entity_Name (Name (Nod))
922 and then Has_Recursive_Call (Entity (Name (Nod)))
923 and then Entity (Name (Nod)) = Spec
924 then
925 Append_Elmt (Nod, Call_List);
926 return Skip;
928 -- Any other procedure call may have side effects
930 else
931 return Abandon;
932 end if;
934 -- A call to a pure function can always be ignored
936 elsif Nkind (Nod) = N_Function_Call
937 and then Is_Entity_Name (Name (Nod))
938 and then Is_Pure (Entity (Name (Nod)))
939 then
940 return Skip;
942 -- Case of an identifier reference
944 elsif Nkind (Nod) = N_Identifier then
945 Ent := Entity (Nod);
947 -- If no entity, then ignore the reference
949 -- Not clear why this can happen. To investigate, remove this
950 -- test and look at the crash that occurs here in 3401-004 ???
952 if No (Ent) then
953 return Skip;
955 -- Ignore entities with no Scope, again not clear how this
956 -- can happen, to investigate, look at 4108-008 ???
958 elsif No (Scope (Ent)) then
959 return Skip;
961 -- Ignore the reference if not to a more global object
963 elsif Scope_Depth (Scope (Ent)) >= Scop then
964 return Skip;
966 -- References to types, exceptions and constants are always OK
968 elsif Is_Type (Ent)
969 or else Ekind (Ent) = E_Exception
970 or else Ekind (Ent) = E_Constant
971 then
972 return Skip;
974 -- If other than a non-volatile scalar variable, we have some
975 -- kind of global reference (e.g. to a function) that we cannot
976 -- deal with so we forget the attempt.
978 elsif Ekind (Ent) /= E_Variable
979 or else not Is_Scalar_Type (Etype (Ent))
980 or else Treat_As_Volatile (Ent)
981 then
982 return Abandon;
984 -- Otherwise we have a reference to a global scalar
986 else
987 -- Loop through global entities already detected
989 Elm := First_Elmt (Var_List);
990 loop
991 -- If not detected before, record this new global reference
993 if No (Elm) then
994 Count_Vars := Count_Vars + 1;
996 if Count_Vars <= Max_Vars then
997 Append_Elmt (Entity (Nod), Var_List);
998 else
999 return Abandon;
1000 end if;
1002 exit;
1004 -- If recorded before, ignore
1006 elsif Node (Elm) = Entity (Nod) then
1007 return Skip;
1009 -- Otherwise keep looking
1011 else
1012 Next_Elmt (Elm);
1013 end if;
1014 end loop;
1016 return Skip;
1017 end if;
1019 -- For all other node kinds, recursively visit syntactic children
1021 else
1022 return OK;
1023 end if;
1024 end Process;
1026 function Traverse_Body is new Traverse_Func (Process);
1028 -- Start of processing for Detect_Infinite_Recursion
1030 begin
1031 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1032 -- won't be able to generate the code to handle the recursion in any
1033 -- case.
1035 if Restriction_Active (No_Implicit_Conditionals) then
1036 return;
1037 end if;
1039 -- Otherwise do traversal and quit if we get abandon signal
1041 if Traverse_Body (N) = Abandon then
1042 return;
1044 -- We must have a call, since Has_Recursive_Call was set. If not just
1045 -- ignore (this is only an error check, so if we have a funny situation,
1046 -- due to bugs or errors, we do not want to bomb).
1048 elsif Is_Empty_Elmt_List (Call_List) then
1049 return;
1050 end if;
1052 -- Here is the case where we detect recursion at compile time
1054 -- Push our current scope for analyzing the declarations and code that
1055 -- we will insert for the checking.
1057 Push_Scope (Spec);
1059 -- This loop builds temporary variables for each of the referenced
1060 -- globals, so that at the end of the loop the list Shad_List contains
1061 -- these temporaries in one-to-one correspondence with the elements in
1062 -- Var_List.
1064 Last := Empty;
1065 Elm := First_Elmt (Var_List);
1066 while Present (Elm) loop
1067 Var := Node (Elm);
1068 Ent := Make_Temporary (Loc, 'S');
1069 Append_Elmt (Ent, Shad_List);
1071 -- Insert a declaration for this temporary at the start of the
1072 -- declarations for the procedure. The temporaries are declared as
1073 -- constant objects initialized to the current values of the
1074 -- corresponding temporaries.
1076 Decl :=
1077 Make_Object_Declaration (Loc,
1078 Defining_Identifier => Ent,
1079 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
1080 Constant_Present => True,
1081 Expression => New_Occurrence_Of (Var, Loc));
1083 if No (Last) then
1084 Prepend (Decl, Declarations (N));
1085 else
1086 Insert_After (Last, Decl);
1087 end if;
1089 Last := Decl;
1090 Analyze (Decl);
1091 Next_Elmt (Elm);
1092 end loop;
1094 -- Loop through calls
1096 Call := First_Elmt (Call_List);
1097 while Present (Call) loop
1099 -- Build a predicate expression of the form
1101 -- True
1102 -- and then global1 = temp1
1103 -- and then global2 = temp2
1104 -- ...
1106 -- This predicate determines if any of the global values
1107 -- referenced by the procedure have changed since the
1108 -- current call, if not an infinite recursion is assured.
1110 Test := New_Occurrence_Of (Standard_True, Loc);
1112 Elm1 := First_Elmt (Var_List);
1113 Elm2 := First_Elmt (Shad_List);
1114 while Present (Elm1) loop
1115 Test :=
1116 Make_And_Then (Loc,
1117 Left_Opnd => Test,
1118 Right_Opnd =>
1119 Make_Op_Eq (Loc,
1120 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1121 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1123 Next_Elmt (Elm1);
1124 Next_Elmt (Elm2);
1125 end loop;
1127 -- Now we replace the call with the sequence
1129 -- if no-changes (see above) then
1130 -- raise Storage_Error;
1131 -- else
1132 -- original-call
1133 -- end if;
1135 Rewrite (Node (Call),
1136 Make_If_Statement (Loc,
1137 Condition => Test,
1138 Then_Statements => New_List (
1139 Make_Raise_Storage_Error (Loc,
1140 Reason => SE_Infinite_Recursion)),
1142 Else_Statements => New_List (
1143 Relocate_Node (Node (Call)))));
1145 Analyze (Node (Call));
1147 Next_Elmt (Call);
1148 end loop;
1150 -- Remove temporary scope stack entry used for analysis
1152 Pop_Scope;
1153 end Detect_Infinite_Recursion;
1155 --------------------
1156 -- Expand_Actuals --
1157 --------------------
1159 procedure Expand_Actuals
1160 (N : Node_Id;
1161 Subp : Entity_Id;
1162 Post_Call : out List_Id)
1164 Loc : constant Source_Ptr := Sloc (N);
1165 Actual : Node_Id;
1166 Formal : Entity_Id;
1167 N_Node : Node_Id;
1168 E_Actual : Entity_Id;
1169 E_Formal : Entity_Id;
1171 procedure Add_Call_By_Copy_Code;
1172 -- For cases where the parameter must be passed by copy, this routine
1173 -- generates a temporary variable into which the actual is copied and
1174 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1175 -- an assignment is also generated to copy the result back. The call
1176 -- also takes care of any constraint checks required for the type
1177 -- conversion case (on both the way in and the way out).
1179 procedure Add_Simple_Call_By_Copy_Code;
1180 -- This is similar to the above, but is used in cases where we know
1181 -- that all that is needed is to simply create a temporary and copy
1182 -- the value in and out of the temporary.
1184 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id);
1185 -- Perform copy-back for actual parameter Act which denotes a validation
1186 -- variable.
1188 procedure Check_Fortran_Logical;
1189 -- A value of type Logical that is passed through a formal parameter
1190 -- must be normalized because .TRUE. usually does not have the same
1191 -- representation as True. We assume that .FALSE. = False = 0.
1192 -- What about functions that return a logical type ???
1194 function Is_Legal_Copy return Boolean;
1195 -- Check that an actual can be copied before generating the temporary
1196 -- to be used in the call. If the actual is of a by_reference type then
1197 -- the program is illegal (this can only happen in the presence of
1198 -- rep. clauses that force an incorrect alignment). If the formal is
1199 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1200 -- the effect that this might lead to unaligned arguments.
1202 function Make_Var (Actual : Node_Id) return Entity_Id;
1203 -- Returns an entity that refers to the given actual parameter, Actual
1204 -- (not including any type conversion). If Actual is an entity name,
1205 -- then this entity is returned unchanged, otherwise a renaming is
1206 -- created to provide an entity for the actual.
1208 procedure Reset_Packed_Prefix;
1209 -- The expansion of a packed array component reference is delayed in
1210 -- the context of a call. Now we need to complete the expansion, so we
1211 -- unmark the analyzed bits in all prefixes.
1213 ---------------------------
1214 -- Add_Call_By_Copy_Code --
1215 ---------------------------
1217 procedure Add_Call_By_Copy_Code is
1218 Crep : Boolean;
1219 Expr : Node_Id;
1220 F_Typ : Entity_Id := Etype (Formal);
1221 Indic : Node_Id;
1222 Init : Node_Id;
1223 Temp : Entity_Id;
1224 V_Typ : Entity_Id;
1225 Var : Entity_Id;
1227 begin
1228 if not Is_Legal_Copy then
1229 return;
1230 end if;
1232 Temp := Make_Temporary (Loc, 'T', Actual);
1234 -- Handle formals whose type comes from the limited view
1236 if From_Limited_With (F_Typ)
1237 and then Has_Non_Limited_View (F_Typ)
1238 then
1239 F_Typ := Non_Limited_View (F_Typ);
1240 end if;
1242 -- Use formal type for temp, unless formal type is an unconstrained
1243 -- array, in which case we don't have to worry about bounds checks,
1244 -- and we use the actual type, since that has appropriate bounds.
1246 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1247 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1248 else
1249 Indic := New_Occurrence_Of (F_Typ, Loc);
1250 end if;
1252 if Nkind (Actual) = N_Type_Conversion then
1253 V_Typ := Etype (Expression (Actual));
1255 -- If the formal is an (in-)out parameter, capture the name
1256 -- of the variable in order to build the post-call assignment.
1258 Var := Make_Var (Expression (Actual));
1260 Crep := not Same_Representation
1261 (F_Typ, Etype (Expression (Actual)));
1263 else
1264 V_Typ := Etype (Actual);
1265 Var := Make_Var (Actual);
1266 Crep := False;
1267 end if;
1269 -- Setup initialization for case of in out parameter, or an out
1270 -- parameter where the formal is an unconstrained array (in the
1271 -- latter case, we have to pass in an object with bounds).
1273 -- If this is an out parameter, the initial copy is wasteful, so as
1274 -- an optimization for the one-dimensional case we extract the
1275 -- bounds of the actual and build an uninitialized temporary of the
1276 -- right size.
1278 if Ekind (Formal) = E_In_Out_Parameter
1279 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1280 then
1281 if Nkind (Actual) = N_Type_Conversion then
1282 if Conversion_OK (Actual) then
1283 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1284 else
1285 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1286 end if;
1288 elsif Ekind (Formal) = E_Out_Parameter
1289 and then Is_Array_Type (F_Typ)
1290 and then Number_Dimensions (F_Typ) = 1
1291 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1292 then
1293 -- Actual is a one-dimensional array or slice, and the type
1294 -- requires no initialization. Create a temporary of the
1295 -- right size, but do not copy actual into it (optimization).
1297 Init := Empty;
1298 Indic :=
1299 Make_Subtype_Indication (Loc,
1300 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1301 Constraint =>
1302 Make_Index_Or_Discriminant_Constraint (Loc,
1303 Constraints => New_List (
1304 Make_Range (Loc,
1305 Low_Bound =>
1306 Make_Attribute_Reference (Loc,
1307 Prefix => New_Occurrence_Of (Var, Loc),
1308 Attribute_Name => Name_First),
1309 High_Bound =>
1310 Make_Attribute_Reference (Loc,
1311 Prefix => New_Occurrence_Of (Var, Loc),
1312 Attribute_Name => Name_Last)))));
1314 else
1315 Init := New_Occurrence_Of (Var, Loc);
1316 end if;
1318 -- An initialization is created for packed conversions as
1319 -- actuals for out parameters to enable Make_Object_Declaration
1320 -- to determine the proper subtype for N_Node. Note that this
1321 -- is wasteful because the extra copying on the call side is
1322 -- not required for such out parameters. ???
1324 elsif Ekind (Formal) = E_Out_Parameter
1325 and then Nkind (Actual) = N_Type_Conversion
1326 and then (Is_Bit_Packed_Array (F_Typ)
1327 or else
1328 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1329 then
1330 if Conversion_OK (Actual) then
1331 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1332 else
1333 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1334 end if;
1336 elsif Ekind (Formal) = E_In_Parameter then
1338 -- Handle the case in which the actual is a type conversion
1340 if Nkind (Actual) = N_Type_Conversion then
1341 if Conversion_OK (Actual) then
1342 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1343 else
1344 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1345 end if;
1346 else
1347 Init := New_Occurrence_Of (Var, Loc);
1348 end if;
1350 else
1351 Init := Empty;
1352 end if;
1354 N_Node :=
1355 Make_Object_Declaration (Loc,
1356 Defining_Identifier => Temp,
1357 Object_Definition => Indic,
1358 Expression => Init);
1359 Set_Assignment_OK (N_Node);
1360 Insert_Action (N, N_Node);
1362 -- Now, normally the deal here is that we use the defining
1363 -- identifier created by that object declaration. There is
1364 -- one exception to this. In the change of representation case
1365 -- the above declaration will end up looking like:
1367 -- temp : type := identifier;
1369 -- And in this case we might as well use the identifier directly
1370 -- and eliminate the temporary. Note that the analysis of the
1371 -- declaration was not a waste of time in that case, since it is
1372 -- what generated the necessary change of representation code. If
1373 -- the change of representation introduced additional code, as in
1374 -- a fixed-integer conversion, the expression is not an identifier
1375 -- and must be kept.
1377 if Crep
1378 and then Present (Expression (N_Node))
1379 and then Is_Entity_Name (Expression (N_Node))
1380 then
1381 Temp := Entity (Expression (N_Node));
1382 Rewrite (N_Node, Make_Null_Statement (Loc));
1383 end if;
1385 -- For IN parameter, all we do is to replace the actual
1387 if Ekind (Formal) = E_In_Parameter then
1388 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1389 Analyze (Actual);
1391 -- Processing for OUT or IN OUT parameter
1393 else
1394 -- Kill current value indications for the temporary variable we
1395 -- created, since we just passed it as an OUT parameter.
1397 Kill_Current_Values (Temp);
1398 Set_Is_Known_Valid (Temp, False);
1400 -- If type conversion, use reverse conversion on exit
1402 if Nkind (Actual) = N_Type_Conversion then
1403 if Conversion_OK (Actual) then
1404 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1405 else
1406 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1407 end if;
1408 else
1409 Expr := New_Occurrence_Of (Temp, Loc);
1410 end if;
1412 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1413 Analyze (Actual);
1415 -- If the actual is a conversion of a packed reference, it may
1416 -- already have been expanded by Remove_Side_Effects, and the
1417 -- resulting variable is a temporary which does not designate
1418 -- the proper out-parameter, which may not be addressable. In
1419 -- that case, generate an assignment to the original expression
1420 -- (before expansion of the packed reference) so that the proper
1421 -- expansion of assignment to a packed component can take place.
1423 declare
1424 Obj : Node_Id;
1425 Lhs : Node_Id;
1427 begin
1428 if Is_Renaming_Of_Object (Var)
1429 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1430 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1431 = N_Indexed_Component
1432 and then
1433 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1434 then
1435 Obj := Renamed_Object (Var);
1436 Lhs :=
1437 Make_Selected_Component (Loc,
1438 Prefix =>
1439 New_Copy_Tree (Original_Node (Prefix (Obj))),
1440 Selector_Name => New_Copy (Selector_Name (Obj)));
1441 Reset_Analyzed_Flags (Lhs);
1443 else
1444 Lhs := New_Occurrence_Of (Var, Loc);
1445 end if;
1447 Set_Assignment_OK (Lhs);
1449 if Is_Access_Type (E_Formal)
1450 and then Is_Entity_Name (Lhs)
1451 and then
1452 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1453 then
1454 -- Copyback target is an Ada 2012 stand-alone object of an
1455 -- anonymous access type.
1457 pragma Assert (Ada_Version >= Ada_2012);
1459 if Type_Access_Level (E_Formal) >
1460 Object_Access_Level (Lhs)
1461 then
1462 Append_To (Post_Call,
1463 Make_Raise_Program_Error (Loc,
1464 Reason => PE_Accessibility_Check_Failed));
1465 end if;
1467 Append_To (Post_Call,
1468 Make_Assignment_Statement (Loc,
1469 Name => Lhs,
1470 Expression => Expr));
1472 -- We would like to somehow suppress generation of the
1473 -- extra_accessibility assignment generated by the expansion
1474 -- of the above assignment statement. It's not a correctness
1475 -- issue because the following assignment renders it dead,
1476 -- but generating back-to-back assignments to the same
1477 -- target is undesirable. ???
1479 Append_To (Post_Call,
1480 Make_Assignment_Statement (Loc,
1481 Name => New_Occurrence_Of (
1482 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1483 Expression => Make_Integer_Literal (Loc,
1484 Type_Access_Level (E_Formal))));
1486 else
1487 Append_To (Post_Call,
1488 Make_Assignment_Statement (Loc,
1489 Name => Lhs,
1490 Expression => Expr));
1491 end if;
1492 end;
1493 end if;
1494 end Add_Call_By_Copy_Code;
1496 ----------------------------------
1497 -- Add_Simple_Call_By_Copy_Code --
1498 ----------------------------------
1500 procedure Add_Simple_Call_By_Copy_Code is
1501 Decl : Node_Id;
1502 F_Typ : Entity_Id := Etype (Formal);
1503 Incod : Node_Id;
1504 Indic : Node_Id;
1505 Lhs : Node_Id;
1506 Outcod : Node_Id;
1507 Rhs : Node_Id;
1508 Temp : Entity_Id;
1510 begin
1511 if not Is_Legal_Copy then
1512 return;
1513 end if;
1515 -- Handle formals whose type comes from the limited view
1517 if From_Limited_With (F_Typ)
1518 and then Has_Non_Limited_View (F_Typ)
1519 then
1520 F_Typ := Non_Limited_View (F_Typ);
1521 end if;
1523 -- Use formal type for temp, unless formal type is an unconstrained
1524 -- array, in which case we don't have to worry about bounds checks,
1525 -- and we use the actual type, since that has appropriate bounds.
1527 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1528 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1529 else
1530 Indic := New_Occurrence_Of (F_Typ, Loc);
1531 end if;
1533 -- Prepare to generate code
1535 Reset_Packed_Prefix;
1537 Temp := Make_Temporary (Loc, 'T', Actual);
1538 Incod := Relocate_Node (Actual);
1539 Outcod := New_Copy_Tree (Incod);
1541 -- Generate declaration of temporary variable, initializing it
1542 -- with the input parameter unless we have an OUT formal or
1543 -- this is an initialization call.
1545 -- If the formal is an out parameter with discriminants, the
1546 -- discriminants must be captured even if the rest of the object
1547 -- is in principle uninitialized, because the discriminants may
1548 -- be read by the called subprogram.
1550 if Ekind (Formal) = E_Out_Parameter then
1551 Incod := Empty;
1553 if Has_Discriminants (F_Typ) then
1554 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1555 end if;
1557 elsif Inside_Init_Proc then
1559 -- Could use a comment here to match comment below ???
1561 if Nkind (Actual) /= N_Selected_Component
1562 or else
1563 not Has_Discriminant_Dependent_Constraint
1564 (Entity (Selector_Name (Actual)))
1565 then
1566 Incod := Empty;
1568 -- Otherwise, keep the component in order to generate the proper
1569 -- actual subtype, that depends on enclosing discriminants.
1571 else
1572 null;
1573 end if;
1574 end if;
1576 Decl :=
1577 Make_Object_Declaration (Loc,
1578 Defining_Identifier => Temp,
1579 Object_Definition => Indic,
1580 Expression => Incod);
1582 if Inside_Init_Proc
1583 and then No (Incod)
1584 then
1585 -- If the call is to initialize a component of a composite type,
1586 -- and the component does not depend on discriminants, use the
1587 -- actual type of the component. This is required in case the
1588 -- component is constrained, because in general the formal of the
1589 -- initialization procedure will be unconstrained. Note that if
1590 -- the component being initialized is constrained by an enclosing
1591 -- discriminant, the presence of the initialization in the
1592 -- declaration will generate an expression for the actual subtype.
1594 Set_No_Initialization (Decl);
1595 Set_Object_Definition (Decl,
1596 New_Occurrence_Of (Etype (Actual), Loc));
1597 end if;
1599 Insert_Action (N, Decl);
1601 -- The actual is simply a reference to the temporary
1603 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1605 -- Generate copy out if OUT or IN OUT parameter
1607 if Ekind (Formal) /= E_In_Parameter then
1608 Lhs := Outcod;
1609 Rhs := New_Occurrence_Of (Temp, Loc);
1611 -- Deal with conversion
1613 if Nkind (Lhs) = N_Type_Conversion then
1614 Lhs := Expression (Lhs);
1615 Rhs := Convert_To (Etype (Actual), Rhs);
1616 end if;
1618 Append_To (Post_Call,
1619 Make_Assignment_Statement (Loc,
1620 Name => Lhs,
1621 Expression => Rhs));
1622 Set_Assignment_OK (Name (Last (Post_Call)));
1623 end if;
1624 end Add_Simple_Call_By_Copy_Code;
1626 --------------------------------------
1627 -- Add_Validation_Call_By_Copy_Code --
1628 --------------------------------------
1630 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id) is
1631 Expr : Node_Id;
1632 Obj : Node_Id;
1633 Obj_Typ : Entity_Id;
1634 Var : Node_Id;
1635 Var_Id : Entity_Id;
1637 begin
1638 Var := Act;
1640 -- Use the expression when the context qualifies a reference in some
1641 -- fashion.
1643 while Nkind_In (Var, N_Qualified_Expression,
1644 N_Type_Conversion,
1645 N_Unchecked_Type_Conversion)
1646 loop
1647 Var := Expression (Var);
1648 end loop;
1650 -- Copy the value of the validation variable back into the object
1651 -- being validated.
1653 if Is_Entity_Name (Var) then
1654 Var_Id := Entity (Var);
1655 Obj := Validated_Object (Var_Id);
1656 Obj_Typ := Etype (Obj);
1658 Expr := New_Occurrence_Of (Var_Id, Loc);
1660 -- A type conversion is needed when the validation variable and
1661 -- the validated object carry different types. This case occurs
1662 -- when the actual is qualified in some fashion.
1664 -- Common:
1665 -- subtype Int is Integer range ...;
1666 -- procedure Call (Val : in out Integer);
1668 -- Original:
1669 -- Object : Int;
1670 -- Call (Integer (Object));
1672 -- Expanded:
1673 -- Object : Int;
1674 -- Var : Integer := Object; -- conversion to base type
1675 -- if not Var'Valid then -- validity check
1676 -- Call (Var); -- modify Var
1677 -- Object := Int (Var); -- conversion to subtype
1679 if Etype (Var_Id) /= Obj_Typ then
1680 Expr :=
1681 Make_Type_Conversion (Loc,
1682 Subtype_Mark => New_Occurrence_Of (Obj_Typ, Loc),
1683 Expression => Expr);
1684 end if;
1686 -- Generate:
1687 -- Object := Var;
1688 -- <or>
1689 -- Object := Object_Type (Var);
1691 Append_To (Post_Call,
1692 Make_Assignment_Statement (Loc,
1693 Name => Obj,
1694 Expression => Expr));
1696 -- If the flow reaches this point, then this routine was invoked with
1697 -- an actual which does not denote a validation variable.
1699 else
1700 pragma Assert (False);
1701 null;
1702 end if;
1703 end Add_Validation_Call_By_Copy_Code;
1705 ---------------------------
1706 -- Check_Fortran_Logical --
1707 ---------------------------
1709 procedure Check_Fortran_Logical is
1710 Logical : constant Entity_Id := Etype (Formal);
1711 Var : Entity_Id;
1713 -- Note: this is very incomplete, e.g. it does not handle arrays
1714 -- of logical values. This is really not the right approach at all???)
1716 begin
1717 if Convention (Subp) = Convention_Fortran
1718 and then Root_Type (Etype (Formal)) = Standard_Boolean
1719 and then Ekind (Formal) /= E_In_Parameter
1720 then
1721 Var := Make_Var (Actual);
1722 Append_To (Post_Call,
1723 Make_Assignment_Statement (Loc,
1724 Name => New_Occurrence_Of (Var, Loc),
1725 Expression =>
1726 Unchecked_Convert_To (
1727 Logical,
1728 Make_Op_Ne (Loc,
1729 Left_Opnd => New_Occurrence_Of (Var, Loc),
1730 Right_Opnd =>
1731 Unchecked_Convert_To (
1732 Logical,
1733 New_Occurrence_Of (Standard_False, Loc))))));
1734 end if;
1735 end Check_Fortran_Logical;
1737 -------------------
1738 -- Is_Legal_Copy --
1739 -------------------
1741 function Is_Legal_Copy return Boolean is
1742 begin
1743 -- An attempt to copy a value of such a type can only occur if
1744 -- representation clauses give the actual a misaligned address.
1746 if Is_By_Reference_Type (Etype (Formal)) then
1748 -- If the front-end does not perform full type layout, the actual
1749 -- may in fact be properly aligned but there is not enough front-
1750 -- end information to determine this. In that case gigi will emit
1751 -- an error if a copy is not legal, or generate the proper code.
1752 -- For other backends we report the error now.
1754 -- Seems wrong to be issuing an error in the expander, since it
1755 -- will be missed in -gnatc mode ???
1757 if Frontend_Layout_On_Target then
1758 Error_Msg_N
1759 ("misaligned actual cannot be passed by reference", Actual);
1760 end if;
1762 return False;
1764 -- For users of Starlet, we assume that the specification of by-
1765 -- reference mechanism is mandatory. This may lead to unaligned
1766 -- objects but at least for DEC legacy code it is known to work.
1767 -- The warning will alert users of this code that a problem may
1768 -- be lurking.
1770 elsif Mechanism (Formal) = By_Reference
1771 and then Is_Valued_Procedure (Scope (Formal))
1772 then
1773 Error_Msg_N
1774 ("by_reference actual may be misaligned??", Actual);
1775 return False;
1777 else
1778 return True;
1779 end if;
1780 end Is_Legal_Copy;
1782 --------------
1783 -- Make_Var --
1784 --------------
1786 function Make_Var (Actual : Node_Id) return Entity_Id is
1787 Var : Entity_Id;
1789 begin
1790 if Is_Entity_Name (Actual) then
1791 return Entity (Actual);
1793 else
1794 Var := Make_Temporary (Loc, 'T', Actual);
1796 N_Node :=
1797 Make_Object_Renaming_Declaration (Loc,
1798 Defining_Identifier => Var,
1799 Subtype_Mark =>
1800 New_Occurrence_Of (Etype (Actual), Loc),
1801 Name => Relocate_Node (Actual));
1803 Insert_Action (N, N_Node);
1804 return Var;
1805 end if;
1806 end Make_Var;
1808 -------------------------
1809 -- Reset_Packed_Prefix --
1810 -------------------------
1812 procedure Reset_Packed_Prefix is
1813 Pfx : Node_Id := Actual;
1814 begin
1815 loop
1816 Set_Analyzed (Pfx, False);
1817 exit when
1818 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1819 Pfx := Prefix (Pfx);
1820 end loop;
1821 end Reset_Packed_Prefix;
1823 -- Start of processing for Expand_Actuals
1825 begin
1826 Post_Call := New_List;
1828 Formal := First_Formal (Subp);
1829 Actual := First_Actual (N);
1830 while Present (Formal) loop
1831 E_Formal := Etype (Formal);
1832 E_Actual := Etype (Actual);
1834 -- Handle formals whose type comes from the limited view
1836 if From_Limited_With (E_Formal)
1837 and then Has_Non_Limited_View (E_Formal)
1838 then
1839 E_Formal := Non_Limited_View (E_Formal);
1840 end if;
1842 if Is_Scalar_Type (E_Formal)
1843 or else Nkind (Actual) = N_Slice
1844 then
1845 Check_Fortran_Logical;
1847 -- RM 6.4.1 (11)
1849 elsif Ekind (Formal) /= E_Out_Parameter then
1851 -- The unusual case of the current instance of a protected type
1852 -- requires special handling. This can only occur in the context
1853 -- of a call within the body of a protected operation.
1855 if Is_Entity_Name (Actual)
1856 and then Ekind (Entity (Actual)) = E_Protected_Type
1857 and then In_Open_Scopes (Entity (Actual))
1858 then
1859 if Scope (Subp) /= Entity (Actual) then
1860 Error_Msg_N
1861 ("operation outside protected type may not "
1862 & "call back its protected operations??", Actual);
1863 end if;
1865 Rewrite (Actual,
1866 Expand_Protected_Object_Reference (N, Entity (Actual)));
1867 end if;
1869 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1870 -- build-in-place function, then a temporary return object needs
1871 -- to be created and access to it must be passed to the function.
1872 -- Currently we limit such functions to those with inherently
1873 -- limited result subtypes, but eventually we plan to expand the
1874 -- functions that are treated as build-in-place to include other
1875 -- composite result types.
1877 if Is_Build_In_Place_Function_Call (Actual) then
1878 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1879 end if;
1881 Apply_Constraint_Check (Actual, E_Formal);
1883 -- Out parameter case. No constraint checks on access type
1884 -- RM 6.4.1 (13)
1886 elsif Is_Access_Type (E_Formal) then
1887 null;
1889 -- RM 6.4.1 (14)
1891 elsif Has_Discriminants (Base_Type (E_Formal))
1892 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1893 then
1894 Apply_Constraint_Check (Actual, E_Formal);
1896 -- RM 6.4.1 (15)
1898 else
1899 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1900 end if;
1902 -- Processing for IN-OUT and OUT parameters
1904 if Ekind (Formal) /= E_In_Parameter then
1906 -- For type conversions of arrays, apply length/range checks
1908 if Is_Array_Type (E_Formal)
1909 and then Nkind (Actual) = N_Type_Conversion
1910 then
1911 if Is_Constrained (E_Formal) then
1912 Apply_Length_Check (Expression (Actual), E_Formal);
1913 else
1914 Apply_Range_Check (Expression (Actual), E_Formal);
1915 end if;
1916 end if;
1918 -- The actual denotes a variable which captures the value of an
1919 -- object for validation purposes. Add a copy-back to reflect any
1920 -- potential changes in value back into the original object.
1922 -- Var : ... := Object;
1923 -- if not Var'Valid then -- validity check
1924 -- Call (Var); -- modify var
1925 -- Object := Var; -- update Object
1927 -- This case is given higher priority because the subsequent check
1928 -- for type conversion may add an extra copy of the variable and
1929 -- prevent proper value propagation back in the original object.
1931 if Is_Validation_Variable_Reference (Actual) then
1932 Add_Validation_Call_By_Copy_Code (Actual);
1934 -- If argument is a type conversion for a type that is passed by
1935 -- copy, then we must pass the parameter by copy.
1937 elsif Nkind (Actual) = N_Type_Conversion
1938 and then
1939 (Is_Numeric_Type (E_Formal)
1940 or else Is_Access_Type (E_Formal)
1941 or else Is_Enumeration_Type (E_Formal)
1942 or else Is_Bit_Packed_Array (Etype (Formal))
1943 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1945 -- Also pass by copy if change of representation
1947 or else not Same_Representation
1948 (Etype (Formal),
1949 Etype (Expression (Actual))))
1950 then
1951 Add_Call_By_Copy_Code;
1953 -- References to components of bit-packed arrays are expanded
1954 -- at this point, rather than at the point of analysis of the
1955 -- actuals, to handle the expansion of the assignment to
1956 -- [in] out parameters.
1958 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1959 Add_Simple_Call_By_Copy_Code;
1961 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1962 -- because the back-end cannot cope with such objects. In other
1963 -- cases where alignment forces a copy, the back-end generates
1964 -- it properly. It should not be generated unconditionally in the
1965 -- front-end because it does not know precisely the alignment
1966 -- requirements of the target, and makes too conservative an
1967 -- estimate, leading to superfluous copies or spurious errors
1968 -- on by-reference parameters.
1970 elsif Nkind (Actual) = N_Selected_Component
1971 and then
1972 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1973 and then not Represented_As_Scalar (Etype (Formal))
1974 then
1975 Add_Simple_Call_By_Copy_Code;
1977 -- References to slices of bit-packed arrays are expanded
1979 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1980 Add_Call_By_Copy_Code;
1982 -- References to possibly unaligned slices of arrays are expanded
1984 elsif Is_Possibly_Unaligned_Slice (Actual) then
1985 Add_Call_By_Copy_Code;
1987 -- Deal with access types where the actual subtype and the
1988 -- formal subtype are not the same, requiring a check.
1990 -- It is necessary to exclude tagged types because of "downward
1991 -- conversion" errors.
1993 elsif Is_Access_Type (E_Formal)
1994 and then not Same_Type (E_Formal, E_Actual)
1995 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1996 then
1997 Add_Call_By_Copy_Code;
1999 -- If the actual is not a scalar and is marked for volatile
2000 -- treatment, whereas the formal is not volatile, then pass
2001 -- by copy unless it is a by-reference type.
2003 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
2004 -- because this is the enforcement of a language rule that applies
2005 -- only to "real" volatile variables, not e.g. to the address
2006 -- clause overlay case.
2008 elsif Is_Entity_Name (Actual)
2009 and then Is_Volatile (Entity (Actual))
2010 and then not Is_By_Reference_Type (E_Actual)
2011 and then not Is_Scalar_Type (Etype (Entity (Actual)))
2012 and then not Is_Volatile (E_Formal)
2013 then
2014 Add_Call_By_Copy_Code;
2016 elsif Nkind (Actual) = N_Indexed_Component
2017 and then Is_Entity_Name (Prefix (Actual))
2018 and then Has_Volatile_Components (Entity (Prefix (Actual)))
2019 then
2020 Add_Call_By_Copy_Code;
2022 -- Add call-by-copy code for the case of scalar out parameters
2023 -- when it is not known at compile time that the subtype of the
2024 -- formal is a subrange of the subtype of the actual (or vice
2025 -- versa for in out parameters), in order to get range checks
2026 -- on such actuals. (Maybe this case should be handled earlier
2027 -- in the if statement???)
2029 elsif Is_Scalar_Type (E_Formal)
2030 and then
2031 (not In_Subrange_Of (E_Formal, E_Actual)
2032 or else
2033 (Ekind (Formal) = E_In_Out_Parameter
2034 and then not In_Subrange_Of (E_Actual, E_Formal)))
2035 then
2036 -- Perhaps the setting back to False should be done within
2037 -- Add_Call_By_Copy_Code, since it could get set on other
2038 -- cases occurring above???
2040 if Do_Range_Check (Actual) then
2041 Set_Do_Range_Check (Actual, False);
2042 end if;
2044 Add_Call_By_Copy_Code;
2045 end if;
2047 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2048 -- by-reference parameters on exit from the call. If the actual
2049 -- is a derived type and the operation is inherited, the body
2050 -- of the operation will not contain a call to the predicate
2051 -- function, so it must be done explicitly after the call. Ditto
2052 -- if the actual is an entity of a predicated subtype.
2054 -- The rule refers to by-reference types, but a check is needed
2055 -- for by-copy types as well. That check is subsumed by the rule
2056 -- for subtype conversion on assignment, but we can generate the
2057 -- required check now.
2059 -- Note also that Subp may be either a subprogram entity for
2060 -- direct calls, or a type entity for indirect calls, which must
2061 -- be handled separately because the name does not denote an
2062 -- overloadable entity.
2064 By_Ref_Predicate_Check : declare
2065 Aund : constant Entity_Id := Underlying_Type (E_Actual);
2066 Atyp : Entity_Id;
2068 function Is_Public_Subp return Boolean;
2069 -- Check whether the subprogram being called is a visible
2070 -- operation of the type of the actual. Used to determine
2071 -- whether an invariant check must be generated on the
2072 -- caller side.
2074 ---------------------
2075 -- Is_Public_Subp --
2076 ---------------------
2078 function Is_Public_Subp return Boolean is
2079 Pack : constant Entity_Id := Scope (Subp);
2080 Subp_Decl : Node_Id;
2082 begin
2083 if not Is_Subprogram (Subp) then
2084 return False;
2086 -- The operation may be inherited, or a primitive of the
2087 -- root type.
2089 elsif
2090 Nkind_In (Parent (Subp), N_Private_Extension_Declaration,
2091 N_Full_Type_Declaration)
2092 then
2093 Subp_Decl := Parent (Subp);
2095 else
2096 Subp_Decl := Unit_Declaration_Node (Subp);
2097 end if;
2099 return Ekind (Pack) = E_Package
2100 and then
2101 List_Containing (Subp_Decl) =
2102 Visible_Declarations
2103 (Specification (Unit_Declaration_Node (Pack)));
2104 end Is_Public_Subp;
2106 -- Start of processing for By_Ref_Predicate_Check
2108 begin
2109 if No (Aund) then
2110 Atyp := E_Actual;
2111 else
2112 Atyp := Aund;
2113 end if;
2115 if Has_Predicates (Atyp)
2116 and then Present (Predicate_Function (Atyp))
2118 -- Skip predicate checks for special cases
2120 and then Predicate_Tests_On_Arguments (Subp)
2121 then
2122 Append_To (Post_Call,
2123 Make_Predicate_Check (Atyp, Actual));
2124 end if;
2126 -- We generated caller-side invariant checks in two cases:
2128 -- a) when calling an inherited operation, where there is an
2129 -- implicit view conversion of the actual to the parent type.
2131 -- b) When the conversion is explicit
2133 -- We treat these cases separately because the required
2134 -- conversion for a) is added later when expanding the call.
2136 if Has_Invariants (Etype (Actual))
2137 and then
2138 Nkind (Parent (Subp)) = N_Private_Extension_Declaration
2139 then
2140 if Comes_From_Source (N) and then Is_Public_Subp then
2141 Append_To (Post_Call, Make_Invariant_Call (Actual));
2142 end if;
2144 elsif Nkind (Actual) = N_Type_Conversion
2145 and then Has_Invariants (Etype (Expression (Actual)))
2146 then
2147 if Comes_From_Source (N) and then Is_Public_Subp then
2148 Append_To (Post_Call,
2149 Make_Invariant_Call (Expression (Actual)));
2150 end if;
2151 end if;
2152 end By_Ref_Predicate_Check;
2154 -- Processing for IN parameters
2156 else
2157 -- For IN parameters in the bit-packed array case, we expand an
2158 -- indexed component (the circuit in Exp_Ch4 deliberately left
2159 -- indexed components appearing as actuals untouched, so that
2160 -- the special processing above for the OUT and IN OUT cases
2161 -- could be performed. We could make the test in Exp_Ch4 more
2162 -- complex and have it detect the parameter mode, but it is
2163 -- easier simply to handle all cases here.)
2165 if Nkind (Actual) = N_Indexed_Component
2166 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2167 then
2168 Reset_Packed_Prefix;
2169 Expand_Packed_Element_Reference (Actual);
2171 -- If we have a reference to a bit-packed array, we copy it, since
2172 -- the actual must be byte aligned.
2174 -- Is this really necessary in all cases???
2176 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2177 Add_Simple_Call_By_Copy_Code;
2179 -- If a non-scalar actual is possibly unaligned, we need a copy
2181 elsif Is_Possibly_Unaligned_Object (Actual)
2182 and then not Represented_As_Scalar (Etype (Formal))
2183 then
2184 Add_Simple_Call_By_Copy_Code;
2186 -- Similarly, we have to expand slices of packed arrays here
2187 -- because the result must be byte aligned.
2189 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2190 Add_Call_By_Copy_Code;
2192 -- Only processing remaining is to pass by copy if this is a
2193 -- reference to a possibly unaligned slice, since the caller
2194 -- expects an appropriately aligned argument.
2196 elsif Is_Possibly_Unaligned_Slice (Actual) then
2197 Add_Call_By_Copy_Code;
2199 -- An unusual case: a current instance of an enclosing task can be
2200 -- an actual, and must be replaced by a reference to self.
2202 elsif Is_Entity_Name (Actual)
2203 and then Is_Task_Type (Entity (Actual))
2204 then
2205 if In_Open_Scopes (Entity (Actual)) then
2206 Rewrite (Actual,
2207 (Make_Function_Call (Loc,
2208 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2209 Analyze (Actual);
2211 -- A task type cannot otherwise appear as an actual
2213 else
2214 raise Program_Error;
2215 end if;
2216 end if;
2217 end if;
2219 Next_Formal (Formal);
2220 Next_Actual (Actual);
2221 end loop;
2222 end Expand_Actuals;
2224 -----------------
2225 -- Expand_Call --
2226 -----------------
2228 procedure Expand_Call (N : Node_Id) is
2229 Post_Call : List_Id;
2230 begin
2231 Expand_Call_Helper (N, Post_Call);
2232 Insert_Post_Call_Actions (N, Post_Call);
2233 end Expand_Call;
2235 ------------------------
2236 -- Expand_Call_Helper --
2237 ------------------------
2239 -- This procedure handles expansion of function calls and procedure call
2240 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2241 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2243 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2244 -- Provide values of actuals for all formals in Extra_Formals list
2245 -- Replace "call" to enumeration literal function by literal itself
2246 -- Rewrite call to predefined operator as operator
2247 -- Replace actuals to in-out parameters that are numeric conversions,
2248 -- with explicit assignment to temporaries before and after the call.
2250 -- Note that the list of actuals has been filled with default expressions
2251 -- during semantic analysis of the call. Only the extra actuals required
2252 -- for the 'Constrained attribute and for accessibility checks are added
2253 -- at this point.
2255 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is
2256 Loc : constant Source_Ptr := Sloc (N);
2257 Call_Node : Node_Id := N;
2258 Extra_Actuals : List_Id := No_List;
2259 Prev : Node_Id := Empty;
2261 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2262 -- Adds one entry to the end of the actual parameter list. Used for
2263 -- default parameters and for extra actuals (for Extra_Formals). The
2264 -- argument is an N_Parameter_Association node.
2266 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2267 -- Adds an extra actual to the list of extra actuals. Expr is the
2268 -- expression for the value of the actual, EF is the entity for the
2269 -- extra formal.
2271 procedure Add_View_Conversion_Invariants
2272 (Formal : Entity_Id;
2273 Actual : Node_Id);
2274 -- Adds invariant checks for every intermediate type between the range
2275 -- of a view converted argument to its ancestor (from parent to child).
2277 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2278 -- Within an instance, a type derived from an untagged formal derived
2279 -- type inherits from the original parent, not from the actual. The
2280 -- current derivation mechanism has the derived type inherit from the
2281 -- actual, which is only correct outside of the instance. If the
2282 -- subprogram is inherited, we test for this particular case through a
2283 -- convoluted tree traversal before setting the proper subprogram to be
2284 -- called.
2286 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2287 -- Return true if E comes from an instance that is not yet frozen
2289 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2290 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2292 function New_Value (From : Node_Id) return Node_Id;
2293 -- From is the original Expression. New_Value is equivalent to a call
2294 -- to Duplicate_Subexpr with an explicit dereference when From is an
2295 -- access parameter.
2297 --------------------------
2298 -- Add_Actual_Parameter --
2299 --------------------------
2301 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2302 Actual_Expr : constant Node_Id :=
2303 Explicit_Actual_Parameter (Insert_Param);
2305 begin
2306 -- Case of insertion is first named actual
2308 if No (Prev) or else
2309 Nkind (Parent (Prev)) /= N_Parameter_Association
2310 then
2311 Set_Next_Named_Actual
2312 (Insert_Param, First_Named_Actual (Call_Node));
2313 Set_First_Named_Actual (Call_Node, Actual_Expr);
2315 if No (Prev) then
2316 if No (Parameter_Associations (Call_Node)) then
2317 Set_Parameter_Associations (Call_Node, New_List);
2318 end if;
2320 Append (Insert_Param, Parameter_Associations (Call_Node));
2322 else
2323 Insert_After (Prev, Insert_Param);
2324 end if;
2326 -- Case of insertion is not first named actual
2328 else
2329 Set_Next_Named_Actual
2330 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2331 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2332 Append (Insert_Param, Parameter_Associations (Call_Node));
2333 end if;
2335 Prev := Actual_Expr;
2336 end Add_Actual_Parameter;
2338 ----------------------
2339 -- Add_Extra_Actual --
2340 ----------------------
2342 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2343 Loc : constant Source_Ptr := Sloc (Expr);
2345 begin
2346 if Extra_Actuals = No_List then
2347 Extra_Actuals := New_List;
2348 Set_Parent (Extra_Actuals, Call_Node);
2349 end if;
2351 Append_To (Extra_Actuals,
2352 Make_Parameter_Association (Loc,
2353 Selector_Name => New_Occurrence_Of (EF, Loc),
2354 Explicit_Actual_Parameter => Expr));
2356 Analyze_And_Resolve (Expr, Etype (EF));
2358 if Nkind (Call_Node) = N_Function_Call then
2359 Set_Is_Accessibility_Actual (Parent (Expr));
2360 end if;
2361 end Add_Extra_Actual;
2363 ------------------------------------
2364 -- Add_View_Conversion_Invariants --
2365 ------------------------------------
2367 procedure Add_View_Conversion_Invariants
2368 (Formal : Entity_Id;
2369 Actual : Node_Id)
2371 Arg : Entity_Id;
2372 Curr_Typ : Entity_Id;
2373 Inv_Checks : List_Id;
2374 Par_Typ : Entity_Id;
2376 begin
2377 Inv_Checks := No_List;
2379 -- Extract the argument from a potentially nested set of view
2380 -- conversions.
2382 Arg := Actual;
2383 while Nkind (Arg) = N_Type_Conversion loop
2384 Arg := Expression (Arg);
2385 end loop;
2387 -- Move up the derivation chain starting with the type of the formal
2388 -- parameter down to the type of the actual object.
2390 Curr_Typ := Empty;
2391 Par_Typ := Etype (Arg);
2392 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
2393 Curr_Typ := Par_Typ;
2395 if Has_Invariants (Curr_Typ)
2396 and then Present (Invariant_Procedure (Curr_Typ))
2397 then
2398 -- Verify the invariate of the current type. Generate:
2400 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
2402 Prepend_New_To (Inv_Checks,
2403 Make_Procedure_Call_Statement (Loc,
2404 Name =>
2405 New_Occurrence_Of
2406 (Invariant_Procedure (Curr_Typ), Loc),
2407 Parameter_Associations => New_List (
2408 Make_Type_Conversion (Loc,
2409 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
2410 Expression => New_Copy_Tree (Arg)))));
2411 end if;
2413 Par_Typ := Base_Type (Etype (Curr_Typ));
2414 end loop;
2416 if not Is_Empty_List (Inv_Checks) then
2417 Insert_Actions_After (N, Inv_Checks);
2418 end if;
2419 end Add_View_Conversion_Invariants;
2421 ---------------------------
2422 -- Inherited_From_Formal --
2423 ---------------------------
2425 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2426 Par : Entity_Id;
2427 Gen_Par : Entity_Id;
2428 Gen_Prim : Elist_Id;
2429 Elmt : Elmt_Id;
2430 Indic : Node_Id;
2432 begin
2433 -- If the operation is inherited, it is attached to the corresponding
2434 -- type derivation. If the parent in the derivation is a generic
2435 -- actual, it is a subtype of the actual, and we have to recover the
2436 -- original derived type declaration to find the proper parent.
2438 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2439 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2440 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2441 N_Derived_Type_Definition
2442 or else not In_Instance
2443 then
2444 return Empty;
2446 else
2447 Indic :=
2448 Subtype_Indication
2449 (Type_Definition (Original_Node (Parent (S))));
2451 if Nkind (Indic) = N_Subtype_Indication then
2452 Par := Entity (Subtype_Mark (Indic));
2453 else
2454 Par := Entity (Indic);
2455 end if;
2456 end if;
2458 if not Is_Generic_Actual_Type (Par)
2459 or else Is_Tagged_Type (Par)
2460 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2461 or else not In_Open_Scopes (Scope (Par))
2462 then
2463 return Empty;
2464 else
2465 Gen_Par := Generic_Parent_Type (Parent (Par));
2466 end if;
2468 -- If the actual has no generic parent type, the formal is not
2469 -- a formal derived type, so nothing to inherit.
2471 if No (Gen_Par) then
2472 return Empty;
2473 end if;
2475 -- If the generic parent type is still the generic type, this is a
2476 -- private formal, not a derived formal, and there are no operations
2477 -- inherited from the formal.
2479 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2480 return Empty;
2481 end if;
2483 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2485 Elmt := First_Elmt (Gen_Prim);
2486 while Present (Elmt) loop
2487 if Chars (Node (Elmt)) = Chars (S) then
2488 declare
2489 F1 : Entity_Id;
2490 F2 : Entity_Id;
2492 begin
2493 F1 := First_Formal (S);
2494 F2 := First_Formal (Node (Elmt));
2495 while Present (F1)
2496 and then Present (F2)
2497 loop
2498 if Etype (F1) = Etype (F2)
2499 or else Etype (F2) = Gen_Par
2500 then
2501 Next_Formal (F1);
2502 Next_Formal (F2);
2503 else
2504 Next_Elmt (Elmt);
2505 exit; -- not the right subprogram
2506 end if;
2508 return Node (Elmt);
2509 end loop;
2510 end;
2512 else
2513 Next_Elmt (Elmt);
2514 end if;
2515 end loop;
2517 raise Program_Error;
2518 end Inherited_From_Formal;
2520 --------------------------
2521 -- In_Unfrozen_Instance --
2522 --------------------------
2524 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2525 S : Entity_Id;
2527 begin
2528 S := E;
2529 while Present (S) and then S /= Standard_Standard loop
2530 if Is_Generic_Instance (S)
2531 and then Present (Freeze_Node (S))
2532 and then not Analyzed (Freeze_Node (S))
2533 then
2534 return True;
2535 end if;
2537 S := Scope (S);
2538 end loop;
2540 return False;
2541 end In_Unfrozen_Instance;
2543 -------------------------
2544 -- Is_Direct_Deep_Call --
2545 -------------------------
2547 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2548 begin
2549 if Is_TSS (Subp, TSS_Deep_Adjust)
2550 or else Is_TSS (Subp, TSS_Deep_Finalize)
2551 or else Is_TSS (Subp, TSS_Deep_Initialize)
2552 then
2553 declare
2554 Actual : Node_Id;
2555 Formal : Node_Id;
2557 begin
2558 Actual := First (Parameter_Associations (N));
2559 Formal := First_Formal (Subp);
2560 while Present (Actual)
2561 and then Present (Formal)
2562 loop
2563 if Nkind (Actual) = N_Identifier
2564 and then Is_Controlling_Actual (Actual)
2565 and then Etype (Actual) = Etype (Formal)
2566 then
2567 return True;
2568 end if;
2570 Next (Actual);
2571 Next_Formal (Formal);
2572 end loop;
2573 end;
2574 end if;
2576 return False;
2577 end Is_Direct_Deep_Call;
2579 ---------------
2580 -- New_Value --
2581 ---------------
2583 function New_Value (From : Node_Id) return Node_Id is
2584 Res : constant Node_Id := Duplicate_Subexpr (From);
2585 begin
2586 if Is_Access_Type (Etype (From)) then
2587 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2588 else
2589 return Res;
2590 end if;
2591 end New_Value;
2593 -- Local variables
2595 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2596 Actual : Node_Id;
2597 Formal : Entity_Id;
2598 Orig_Subp : Entity_Id := Empty;
2599 Param_Count : Natural := 0;
2600 Parent_Formal : Entity_Id;
2601 Parent_Subp : Entity_Id;
2602 Scop : Entity_Id;
2603 Subp : Entity_Id;
2605 Prev_Orig : Node_Id;
2606 -- Original node for an actual, which may have been rewritten. If the
2607 -- actual is a function call that has been transformed from a selected
2608 -- component, the original node is unanalyzed. Otherwise, it carries
2609 -- semantic information used to generate additional actuals.
2611 CW_Interface_Formals_Present : Boolean := False;
2613 -- Start of processing for Expand_Call_Helper
2615 begin
2616 Post_Call := New_List;
2618 -- Expand the function or procedure call if the first actual has a
2619 -- declared dimension aspect, and the subprogram is declared in one
2620 -- of the dimension I/O packages.
2622 if Ada_Version >= Ada_2012
2623 and then
2624 Nkind_In (Call_Node, N_Procedure_Call_Statement, N_Function_Call)
2625 and then Present (Parameter_Associations (Call_Node))
2626 then
2627 Expand_Put_Call_With_Symbol (Call_Node);
2628 end if;
2630 -- Ignore if previous error
2632 if Nkind (Call_Node) in N_Has_Etype
2633 and then Etype (Call_Node) = Any_Type
2634 then
2635 return;
2636 end if;
2638 -- Call using access to subprogram with explicit dereference
2640 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2641 Subp := Etype (Name (Call_Node));
2642 Parent_Subp := Empty;
2644 -- Case of call to simple entry, where the Name is a selected component
2645 -- whose prefix is the task, and whose selector name is the entry name
2647 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2648 Subp := Entity (Selector_Name (Name (Call_Node)));
2649 Parent_Subp := Empty;
2651 -- Case of call to member of entry family, where Name is an indexed
2652 -- component, with the prefix being a selected component giving the
2653 -- task and entry family name, and the index being the entry index.
2655 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2656 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2657 Parent_Subp := Empty;
2659 -- Normal case
2661 else
2662 Subp := Entity (Name (Call_Node));
2663 Parent_Subp := Alias (Subp);
2665 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2666 -- if we can tell that the first parameter cannot possibly be null.
2667 -- This improves efficiency by avoiding a run-time test.
2669 -- We do not do this if Raise_Exception_Always does not exist, which
2670 -- can happen in configurable run time profiles which provide only a
2671 -- Raise_Exception.
2673 if Is_RTE (Subp, RE_Raise_Exception)
2674 and then RTE_Available (RE_Raise_Exception_Always)
2675 then
2676 declare
2677 FA : constant Node_Id :=
2678 Original_Node (First_Actual (Call_Node));
2680 begin
2681 -- The case we catch is where the first argument is obtained
2682 -- using the Identity attribute (which must always be
2683 -- non-null).
2685 if Nkind (FA) = N_Attribute_Reference
2686 and then Attribute_Name (FA) = Name_Identity
2687 then
2688 Subp := RTE (RE_Raise_Exception_Always);
2689 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2690 end if;
2691 end;
2692 end if;
2694 if Ekind (Subp) = E_Entry then
2695 Parent_Subp := Empty;
2696 end if;
2697 end if;
2699 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2700 -- alternative in an asynchronous select or as an entry call in
2701 -- a conditional or timed select. Check whether the procedure call
2702 -- is a renaming of an entry and rewrite it as an entry call.
2704 if Ada_Version >= Ada_2005
2705 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2706 and then
2707 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2708 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2709 or else
2710 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2711 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2712 then
2713 declare
2714 Ren_Decl : Node_Id;
2715 Ren_Root : Entity_Id := Subp;
2717 begin
2718 -- This may be a chain of renamings, find the root
2720 if Present (Alias (Ren_Root)) then
2721 Ren_Root := Alias (Ren_Root);
2722 end if;
2724 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2725 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2727 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2728 Rewrite (Call_Node,
2729 Make_Entry_Call_Statement (Loc,
2730 Name =>
2731 New_Copy_Tree (Name (Ren_Decl)),
2732 Parameter_Associations =>
2733 New_Copy_List_Tree
2734 (Parameter_Associations (Call_Node))));
2736 return;
2737 end if;
2738 end if;
2739 end;
2740 end if;
2742 -- When generating C code, transform a function call that returns a
2743 -- constrained array type into procedure form.
2745 if Modify_Tree_For_C
2746 and then Nkind (Call_Node) = N_Function_Call
2747 and then Is_Entity_Name (Name (Call_Node))
2748 and then Rewritten_For_C (Ultimate_Alias (Entity (Name (Call_Node))))
2749 then
2750 -- For internally generated calls ensure that they reference the
2751 -- entity of the spec of the called function (needed since the
2752 -- expander may generate calls using the entity of their body).
2753 -- See for example Expand_Boolean_Operator().
2755 if not (Comes_From_Source (Call_Node))
2756 and then Nkind (Unit_Declaration_Node
2757 (Ultimate_Alias (Entity (Name (Call_Node))))) =
2758 N_Subprogram_Body
2759 then
2760 Set_Entity (Name (Call_Node),
2761 Corresponding_Function
2762 (Corresponding_Procedure
2763 (Ultimate_Alias (Entity (Name (Call_Node))))));
2764 end if;
2766 Rewrite_Function_Call_For_C (Call_Node);
2767 return;
2768 end if;
2770 -- First step, compute extra actuals, corresponding to any Extra_Formals
2771 -- present. Note that we do not access Extra_Formals directly, instead
2772 -- we simply note the presence of the extra formals as we process the
2773 -- regular formals collecting corresponding actuals in Extra_Actuals.
2775 -- We also generate any required range checks for actuals for in formals
2776 -- as we go through the loop, since this is a convenient place to do it.
2777 -- (Though it seems that this would be better done in Expand_Actuals???)
2779 -- Special case: Thunks must not compute the extra actuals; they must
2780 -- just propagate to the target primitive their extra actuals.
2782 if Is_Thunk (Current_Scope)
2783 and then Thunk_Entity (Current_Scope) = Subp
2784 and then Present (Extra_Formals (Subp))
2785 then
2786 pragma Assert (Present (Extra_Formals (Current_Scope)));
2788 declare
2789 Target_Formal : Entity_Id;
2790 Thunk_Formal : Entity_Id;
2792 begin
2793 Target_Formal := Extra_Formals (Subp);
2794 Thunk_Formal := Extra_Formals (Current_Scope);
2795 while Present (Target_Formal) loop
2796 Add_Extra_Actual
2797 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2799 Target_Formal := Extra_Formal (Target_Formal);
2800 Thunk_Formal := Extra_Formal (Thunk_Formal);
2801 end loop;
2803 while Is_Non_Empty_List (Extra_Actuals) loop
2804 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2805 end loop;
2807 Expand_Actuals (Call_Node, Subp, Post_Call);
2808 pragma Assert (Is_Empty_List (Post_Call));
2809 return;
2810 end;
2811 end if;
2813 Formal := First_Formal (Subp);
2814 Actual := First_Actual (Call_Node);
2815 Param_Count := 1;
2816 while Present (Formal) loop
2818 -- Generate range check if required
2820 if Do_Range_Check (Actual)
2821 and then Ekind (Formal) = E_In_Parameter
2822 then
2823 Generate_Range_Check
2824 (Actual, Etype (Formal), CE_Range_Check_Failed);
2825 end if;
2827 -- Prepare to examine current entry
2829 Prev := Actual;
2830 Prev_Orig := Original_Node (Prev);
2832 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2833 -- to expand it in a further round.
2835 CW_Interface_Formals_Present :=
2836 CW_Interface_Formals_Present
2837 or else
2838 (Is_Class_Wide_Type (Etype (Formal))
2839 and then Is_Interface (Etype (Etype (Formal))))
2840 or else
2841 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2842 and then Is_Class_Wide_Type (Directly_Designated_Type
2843 (Etype (Etype (Formal))))
2844 and then Is_Interface (Directly_Designated_Type
2845 (Etype (Etype (Formal)))));
2847 -- Create possible extra actual for constrained case. Usually, the
2848 -- extra actual is of the form actual'constrained, but since this
2849 -- attribute is only available for unconstrained records, TRUE is
2850 -- expanded if the type of the formal happens to be constrained (for
2851 -- instance when this procedure is inherited from an unconstrained
2852 -- record to a constrained one) or if the actual has no discriminant
2853 -- (its type is constrained). An exception to this is the case of a
2854 -- private type without discriminants. In this case we pass FALSE
2855 -- because the object has underlying discriminants with defaults.
2857 if Present (Extra_Constrained (Formal)) then
2858 if Ekind (Etype (Prev)) in Private_Kind
2859 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2860 then
2861 Add_Extra_Actual
2862 (New_Occurrence_Of (Standard_False, Loc),
2863 Extra_Constrained (Formal));
2865 elsif Is_Constrained (Etype (Formal))
2866 or else not Has_Discriminants (Etype (Prev))
2867 then
2868 Add_Extra_Actual
2869 (New_Occurrence_Of (Standard_True, Loc),
2870 Extra_Constrained (Formal));
2872 -- Do not produce extra actuals for Unchecked_Union parameters.
2873 -- Jump directly to the end of the loop.
2875 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2876 goto Skip_Extra_Actual_Generation;
2878 else
2879 -- If the actual is a type conversion, then the constrained
2880 -- test applies to the actual, not the target type.
2882 declare
2883 Act_Prev : Node_Id;
2885 begin
2886 -- Test for unchecked conversions as well, which can occur
2887 -- as out parameter actuals on calls to stream procedures.
2889 Act_Prev := Prev;
2890 while Nkind_In (Act_Prev, N_Type_Conversion,
2891 N_Unchecked_Type_Conversion)
2892 loop
2893 Act_Prev := Expression (Act_Prev);
2894 end loop;
2896 -- If the expression is a conversion of a dereference, this
2897 -- is internally generated code that manipulates addresses,
2898 -- e.g. when building interface tables. No check should
2899 -- occur in this case, and the discriminated object is not
2900 -- directly a hand.
2902 if not Comes_From_Source (Actual)
2903 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2904 and then Nkind (Act_Prev) = N_Explicit_Dereference
2905 then
2906 Add_Extra_Actual
2907 (New_Occurrence_Of (Standard_False, Loc),
2908 Extra_Constrained (Formal));
2910 else
2911 Add_Extra_Actual
2912 (Make_Attribute_Reference (Sloc (Prev),
2913 Prefix =>
2914 Duplicate_Subexpr_No_Checks
2915 (Act_Prev, Name_Req => True),
2916 Attribute_Name => Name_Constrained),
2917 Extra_Constrained (Formal));
2918 end if;
2919 end;
2920 end if;
2921 end if;
2923 -- Create possible extra actual for accessibility level
2925 if Present (Extra_Accessibility (Formal)) then
2927 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2928 -- attribute, then the original actual may be an aliased object
2929 -- occurring as the prefix in a call using "Object.Operation"
2930 -- notation. In that case we must pass the level of the object,
2931 -- so Prev_Orig is reset to Prev and the attribute will be
2932 -- processed by the code for Access attributes further below.
2934 if Prev_Orig /= Prev
2935 and then Nkind (Prev) = N_Attribute_Reference
2936 and then
2937 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2938 and then Is_Aliased_View (Prev_Orig)
2939 then
2940 Prev_Orig := Prev;
2942 -- If the actual is a formal of an enclosing subprogram it is
2943 -- the right entity, even if it is a rewriting. This happens
2944 -- when the call is within an inherited condition or predicate.
2946 elsif Is_Entity_Name (Actual)
2947 and then Is_Formal (Entity (Actual))
2948 and then In_Open_Scopes (Scope (Entity (Actual)))
2949 then
2950 Prev_Orig := Prev;
2951 end if;
2953 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2954 -- accessibility levels.
2956 if Is_Thunk (Current_Scope) then
2957 declare
2958 Parm_Ent : Entity_Id;
2960 begin
2961 if Is_Controlling_Actual (Actual) then
2963 -- Find the corresponding actual of the thunk
2965 Parm_Ent := First_Entity (Current_Scope);
2966 for J in 2 .. Param_Count loop
2967 Next_Entity (Parm_Ent);
2968 end loop;
2970 -- Handle unchecked conversion of access types generated
2971 -- in thunks (cf. Expand_Interface_Thunk).
2973 elsif Is_Access_Type (Etype (Actual))
2974 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2975 then
2976 Parm_Ent := Entity (Expression (Actual));
2978 else pragma Assert (Is_Entity_Name (Actual));
2979 Parm_Ent := Entity (Actual);
2980 end if;
2982 Add_Extra_Actual
2983 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2984 Extra_Accessibility (Formal));
2985 end;
2987 elsif Is_Entity_Name (Prev_Orig) then
2989 -- When passing an access parameter, or a renaming of an access
2990 -- parameter, as the actual to another access parameter we need
2991 -- to pass along the actual's own access level parameter. This
2992 -- is done if we are within the scope of the formal access
2993 -- parameter (if this is an inlined body the extra formal is
2994 -- irrelevant).
2996 if (Is_Formal (Entity (Prev_Orig))
2997 or else
2998 (Present (Renamed_Object (Entity (Prev_Orig)))
2999 and then
3000 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
3001 and then
3002 Is_Formal
3003 (Entity (Renamed_Object (Entity (Prev_Orig))))))
3004 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
3005 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
3006 then
3007 declare
3008 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
3010 begin
3011 pragma Assert (Present (Parm_Ent));
3013 if Present (Extra_Accessibility (Parm_Ent)) then
3014 Add_Extra_Actual
3015 (New_Occurrence_Of
3016 (Extra_Accessibility (Parm_Ent), Loc),
3017 Extra_Accessibility (Formal));
3019 -- If the actual access parameter does not have an
3020 -- associated extra formal providing its scope level,
3021 -- then treat the actual as having library-level
3022 -- accessibility.
3024 else
3025 Add_Extra_Actual
3026 (Make_Integer_Literal (Loc,
3027 Intval => Scope_Depth (Standard_Standard)),
3028 Extra_Accessibility (Formal));
3029 end if;
3030 end;
3032 -- The actual is a normal access value, so just pass the level
3033 -- of the actual's access type.
3035 else
3036 Add_Extra_Actual
3037 (Dynamic_Accessibility_Level (Prev_Orig),
3038 Extra_Accessibility (Formal));
3039 end if;
3041 -- If the actual is an access discriminant, then pass the level
3042 -- of the enclosing object (RM05-3.10.2(12.4/2)).
3044 elsif Nkind (Prev_Orig) = N_Selected_Component
3045 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
3046 E_Discriminant
3047 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
3048 E_Anonymous_Access_Type
3049 then
3050 Add_Extra_Actual
3051 (Make_Integer_Literal (Loc,
3052 Intval => Object_Access_Level (Prefix (Prev_Orig))),
3053 Extra_Accessibility (Formal));
3055 -- All other cases
3057 else
3058 case Nkind (Prev_Orig) is
3059 when N_Attribute_Reference =>
3060 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
3062 -- For X'Access, pass on the level of the prefix X
3064 when Attribute_Access =>
3066 -- If this is an Access attribute applied to the
3067 -- the current instance object passed to a type
3068 -- initialization procedure, then use the level
3069 -- of the type itself. This is not really correct,
3070 -- as there should be an extra level parameter
3071 -- passed in with _init formals (only in the case
3072 -- where the type is immutably limited), but we
3073 -- don't have an easy way currently to create such
3074 -- an extra formal (init procs aren't ever frozen).
3075 -- For now we just use the level of the type,
3076 -- which may be too shallow, but that works better
3077 -- than passing Object_Access_Level of the type,
3078 -- which can be one level too deep in some cases.
3079 -- ???
3081 if Is_Entity_Name (Prefix (Prev_Orig))
3082 and then Is_Type (Entity (Prefix (Prev_Orig)))
3083 then
3084 Add_Extra_Actual
3085 (Make_Integer_Literal (Loc,
3086 Intval =>
3087 Type_Access_Level
3088 (Entity (Prefix (Prev_Orig)))),
3089 Extra_Accessibility (Formal));
3091 else
3092 Add_Extra_Actual
3093 (Make_Integer_Literal (Loc,
3094 Intval =>
3095 Object_Access_Level
3096 (Prefix (Prev_Orig))),
3097 Extra_Accessibility (Formal));
3098 end if;
3100 -- Treat the unchecked attributes as library-level
3102 when Attribute_Unchecked_Access
3103 | Attribute_Unrestricted_Access
3105 Add_Extra_Actual
3106 (Make_Integer_Literal (Loc,
3107 Intval => Scope_Depth (Standard_Standard)),
3108 Extra_Accessibility (Formal));
3110 -- No other cases of attributes returning access
3111 -- values that can be passed to access parameters.
3113 when others =>
3114 raise Program_Error;
3116 end case;
3118 -- For allocators we pass the level of the execution of the
3119 -- called subprogram, which is one greater than the current
3120 -- scope level.
3122 when N_Allocator =>
3123 Add_Extra_Actual
3124 (Make_Integer_Literal (Loc,
3125 Intval => Scope_Depth (Current_Scope) + 1),
3126 Extra_Accessibility (Formal));
3128 -- For most other cases we simply pass the level of the
3129 -- actual's access type. The type is retrieved from
3130 -- Prev rather than Prev_Orig, because in some cases
3131 -- Prev_Orig denotes an original expression that has
3132 -- not been analyzed.
3134 when others =>
3135 Add_Extra_Actual
3136 (Dynamic_Accessibility_Level (Prev),
3137 Extra_Accessibility (Formal));
3138 end case;
3139 end if;
3140 end if;
3142 -- Perform the check of 4.6(49) that prevents a null value from being
3143 -- passed as an actual to an access parameter. Note that the check
3144 -- is elided in the common cases of passing an access attribute or
3145 -- access parameter as an actual. Also, we currently don't enforce
3146 -- this check for expander-generated actuals and when -gnatdj is set.
3148 if Ada_Version >= Ada_2005 then
3150 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3151 -- the intent of 6.4.1(13) is that null-exclusion checks should
3152 -- not be done for 'out' parameters, even though it refers only
3153 -- to constraint checks, and a null_exclusion is not a constraint.
3154 -- Note that AI05-0196-1 corrects this mistake in the RM.
3156 if Is_Access_Type (Etype (Formal))
3157 and then Can_Never_Be_Null (Etype (Formal))
3158 and then Ekind (Formal) /= E_Out_Parameter
3159 and then Nkind (Prev) /= N_Raise_Constraint_Error
3160 and then (Known_Null (Prev)
3161 or else not Can_Never_Be_Null (Etype (Prev)))
3162 then
3163 Install_Null_Excluding_Check (Prev);
3164 end if;
3166 -- Ada_Version < Ada_2005
3168 else
3169 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3170 or else Access_Checks_Suppressed (Subp)
3171 then
3172 null;
3174 elsif Debug_Flag_J then
3175 null;
3177 elsif not Comes_From_Source (Prev) then
3178 null;
3180 elsif Is_Entity_Name (Prev)
3181 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3182 then
3183 null;
3185 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3186 null;
3188 else
3189 Install_Null_Excluding_Check (Prev);
3190 end if;
3191 end if;
3193 -- Perform appropriate validity checks on parameters that
3194 -- are entities.
3196 if Validity_Checks_On then
3197 if (Ekind (Formal) = E_In_Parameter
3198 and then Validity_Check_In_Params)
3199 or else
3200 (Ekind (Formal) = E_In_Out_Parameter
3201 and then Validity_Check_In_Out_Params)
3202 then
3203 -- If the actual is an indexed component of a packed type (or
3204 -- is an indexed or selected component whose prefix recursively
3205 -- meets this condition), it has not been expanded yet. It will
3206 -- be copied in the validity code that follows, and has to be
3207 -- expanded appropriately, so reanalyze it.
3209 -- What we do is just to unset analyzed bits on prefixes till
3210 -- we reach something that does not have a prefix.
3212 declare
3213 Nod : Node_Id;
3215 begin
3216 Nod := Actual;
3217 while Nkind_In (Nod, N_Indexed_Component,
3218 N_Selected_Component)
3219 loop
3220 Set_Analyzed (Nod, False);
3221 Nod := Prefix (Nod);
3222 end loop;
3223 end;
3225 Ensure_Valid (Actual);
3226 end if;
3227 end if;
3229 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3230 -- since this is a left side reference. We only do this for calls
3231 -- from the source program since we assume that compiler generated
3232 -- calls explicitly generate any required checks. We also need it
3233 -- only if we are doing standard validity checks, since clearly it is
3234 -- not needed if validity checks are off, and in subscript validity
3235 -- checking mode, all indexed components are checked with a call
3236 -- directly from Expand_N_Indexed_Component.
3238 if Comes_From_Source (Call_Node)
3239 and then Ekind (Formal) /= E_In_Parameter
3240 and then Validity_Checks_On
3241 and then Validity_Check_Default
3242 and then not Validity_Check_Subscripts
3243 then
3244 Check_Valid_Lvalue_Subscripts (Actual);
3245 end if;
3247 -- Mark any scalar OUT parameter that is a simple variable as no
3248 -- longer known to be valid (unless the type is always valid). This
3249 -- reflects the fact that if an OUT parameter is never set in a
3250 -- procedure, then it can become invalid on the procedure return.
3252 if Ekind (Formal) = E_Out_Parameter
3253 and then Is_Entity_Name (Actual)
3254 and then Ekind (Entity (Actual)) = E_Variable
3255 and then not Is_Known_Valid (Etype (Actual))
3256 then
3257 Set_Is_Known_Valid (Entity (Actual), False);
3258 end if;
3260 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3261 -- clear current values, since they can be clobbered. We are probably
3262 -- doing this in more places than we need to, but better safe than
3263 -- sorry when it comes to retaining bad current values.
3265 if Ekind (Formal) /= E_In_Parameter
3266 and then Is_Entity_Name (Actual)
3267 and then Present (Entity (Actual))
3268 then
3269 declare
3270 Ent : constant Entity_Id := Entity (Actual);
3271 Sav : Node_Id;
3273 begin
3274 -- For an OUT or IN OUT parameter that is an assignable entity,
3275 -- we do not want to clobber the Last_Assignment field, since
3276 -- if it is set, it was precisely because it is indeed an OUT
3277 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3278 -- since the subprogram could have returned in invalid value.
3280 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3281 and then Is_Assignable (Ent)
3282 then
3283 Sav := Last_Assignment (Ent);
3284 Kill_Current_Values (Ent);
3285 Set_Last_Assignment (Ent, Sav);
3286 Set_Is_Known_Valid (Ent, False);
3288 -- For all other cases, just kill the current values
3290 else
3291 Kill_Current_Values (Ent);
3292 end if;
3293 end;
3294 end if;
3296 -- If the formal is class wide and the actual is an aggregate, force
3297 -- evaluation so that the back end who does not know about class-wide
3298 -- type, does not generate a temporary of the wrong size.
3300 if not Is_Class_Wide_Type (Etype (Formal)) then
3301 null;
3303 elsif Nkind (Actual) = N_Aggregate
3304 or else (Nkind (Actual) = N_Qualified_Expression
3305 and then Nkind (Expression (Actual)) = N_Aggregate)
3306 then
3307 Force_Evaluation (Actual);
3308 end if;
3310 -- In a remote call, if the formal is of a class-wide type, check
3311 -- that the actual meets the requirements described in E.4(18).
3313 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3314 Insert_Action (Actual,
3315 Make_Transportable_Check (Loc,
3316 Duplicate_Subexpr_Move_Checks (Actual)));
3317 end if;
3319 -- Perform invariant checks for all intermediate types in a view
3320 -- conversion after successful return from a call that passes the
3321 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
3322 -- 13/3, 14/3)). Consider only source conversion in order to avoid
3323 -- generating spurious checks on complex expansion such as object
3324 -- initialization through an extension aggregate.
3326 if Comes_From_Source (N)
3327 and then Ekind (Formal) /= E_In_Parameter
3328 and then Nkind (Actual) = N_Type_Conversion
3329 then
3330 Add_View_Conversion_Invariants (Formal, Actual);
3331 end if;
3333 -- Generating C the initialization of an allocator is performed by
3334 -- means of individual statements, and hence it must be done before
3335 -- the call.
3337 if Modify_Tree_For_C
3338 and then Nkind (Actual) = N_Allocator
3339 and then Nkind (Expression (Actual)) = N_Qualified_Expression
3340 then
3341 Remove_Side_Effects (Actual);
3342 end if;
3344 -- This label is required when skipping extra actual generation for
3345 -- Unchecked_Union parameters.
3347 <<Skip_Extra_Actual_Generation>>
3349 Param_Count := Param_Count + 1;
3350 Next_Actual (Actual);
3351 Next_Formal (Formal);
3352 end loop;
3354 -- If we are calling an Ada 2012 function which needs to have the
3355 -- "accessibility level determined by the point of call" (AI05-0234)
3356 -- passed in to it, then pass it in.
3358 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3359 and then
3360 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3361 then
3362 declare
3363 Ancestor : Node_Id := Parent (Call_Node);
3364 Level : Node_Id := Empty;
3365 Defer : Boolean := False;
3367 begin
3368 -- Unimplemented: if Subp returns an anonymous access type, then
3370 -- a) if the call is the operand of an explict conversion, then
3371 -- the target type of the conversion (a named access type)
3372 -- determines the accessibility level pass in;
3374 -- b) if the call defines an access discriminant of an object
3375 -- (e.g., the discriminant of an object being created by an
3376 -- allocator, or the discriminant of a function result),
3377 -- then the accessibility level to pass in is that of the
3378 -- discriminated object being initialized).
3380 -- ???
3382 while Nkind (Ancestor) = N_Qualified_Expression
3383 loop
3384 Ancestor := Parent (Ancestor);
3385 end loop;
3387 case Nkind (Ancestor) is
3388 when N_Allocator =>
3390 -- At this point, we'd like to assign
3392 -- Level := Dynamic_Accessibility_Level (Ancestor);
3394 -- but Etype of Ancestor may not have been set yet,
3395 -- so that doesn't work.
3397 -- Handle this later in Expand_Allocator_Expression.
3399 Defer := True;
3401 when N_Object_Declaration
3402 | N_Object_Renaming_Declaration
3404 declare
3405 Def_Id : constant Entity_Id :=
3406 Defining_Identifier (Ancestor);
3408 begin
3409 if Is_Return_Object (Def_Id) then
3410 if Present (Extra_Accessibility_Of_Result
3411 (Return_Applies_To (Scope (Def_Id))))
3412 then
3413 -- Pass along value that was passed in if the
3414 -- routine we are returning from also has an
3415 -- Accessibility_Of_Result formal.
3417 Level :=
3418 New_Occurrence_Of
3419 (Extra_Accessibility_Of_Result
3420 (Return_Applies_To (Scope (Def_Id))), Loc);
3421 end if;
3422 else
3423 Level :=
3424 Make_Integer_Literal (Loc,
3425 Intval => Object_Access_Level (Def_Id));
3426 end if;
3427 end;
3429 when N_Simple_Return_Statement =>
3430 if Present (Extra_Accessibility_Of_Result
3431 (Return_Applies_To
3432 (Return_Statement_Entity (Ancestor))))
3433 then
3434 -- Pass along value that was passed in if the returned
3435 -- routine also has an Accessibility_Of_Result formal.
3437 Level :=
3438 New_Occurrence_Of
3439 (Extra_Accessibility_Of_Result
3440 (Return_Applies_To
3441 (Return_Statement_Entity (Ancestor))), Loc);
3442 end if;
3444 when others =>
3445 null;
3446 end case;
3448 if not Defer then
3449 if not Present (Level) then
3451 -- The "innermost master that evaluates the function call".
3453 -- ??? - Should we use Integer'Last here instead in order
3454 -- to deal with (some of) the problems associated with
3455 -- calls to subps whose enclosing scope is unknown (e.g.,
3456 -- Anon_Access_To_Subp_Param.all)?
3458 Level :=
3459 Make_Integer_Literal (Loc,
3460 Intval => Scope_Depth (Current_Scope) + 1);
3461 end if;
3463 Add_Extra_Actual
3464 (Level,
3465 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3466 end if;
3467 end;
3468 end if;
3470 -- If we are expanding the RHS of an assignment we need to check if tag
3471 -- propagation is needed. You might expect this processing to be in
3472 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3473 -- assignment might be transformed to a declaration for an unconstrained
3474 -- value if the expression is classwide.
3476 if Nkind (Call_Node) = N_Function_Call
3477 and then Is_Tag_Indeterminate (Call_Node)
3478 and then Is_Entity_Name (Name (Call_Node))
3479 then
3480 declare
3481 Ass : Node_Id := Empty;
3483 begin
3484 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3485 Ass := Parent (Call_Node);
3487 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3488 and then Nkind (Parent (Parent (Call_Node))) =
3489 N_Assignment_Statement
3490 then
3491 Ass := Parent (Parent (Call_Node));
3493 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3494 and then Nkind (Parent (Parent (Call_Node))) =
3495 N_Assignment_Statement
3496 then
3497 Ass := Parent (Parent (Call_Node));
3498 end if;
3500 if Present (Ass)
3501 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3502 then
3503 if Is_Access_Type (Etype (Call_Node)) then
3504 if Designated_Type (Etype (Call_Node)) /=
3505 Root_Type (Etype (Name (Ass)))
3506 then
3507 Error_Msg_NE
3508 ("tag-indeterminate expression "
3509 & " must have designated type& (RM 5.2 (6))",
3510 Call_Node, Root_Type (Etype (Name (Ass))));
3511 else
3512 Propagate_Tag (Name (Ass), Call_Node);
3513 end if;
3515 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3516 Error_Msg_NE
3517 ("tag-indeterminate expression must have type&"
3518 & "(RM 5.2 (6))",
3519 Call_Node, Root_Type (Etype (Name (Ass))));
3521 else
3522 Propagate_Tag (Name (Ass), Call_Node);
3523 end if;
3525 -- The call will be rewritten as a dispatching call, and
3526 -- expanded as such.
3528 return;
3529 end if;
3530 end;
3531 end if;
3533 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3534 -- it to point to the correct secondary virtual table
3536 if Nkind (Call_Node) in N_Subprogram_Call
3537 and then CW_Interface_Formals_Present
3538 then
3539 Expand_Interface_Actuals (Call_Node);
3540 end if;
3542 -- Deals with Dispatch_Call if we still have a call, before expanding
3543 -- extra actuals since this will be done on the re-analysis of the
3544 -- dispatching call. Note that we do not try to shorten the actual list
3545 -- for a dispatching call, it would not make sense to do so. Expansion
3546 -- of dispatching calls is suppressed for VM targets, because the VM
3547 -- back-ends directly handle the generation of dispatching calls and
3548 -- would have to undo any expansion to an indirect call.
3550 if Nkind (Call_Node) in N_Subprogram_Call
3551 and then Present (Controlling_Argument (Call_Node))
3552 then
3553 declare
3554 Call_Typ : constant Entity_Id := Etype (Call_Node);
3555 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3556 Eq_Prim_Op : Entity_Id := Empty;
3557 New_Call : Node_Id;
3558 Param : Node_Id;
3559 Prev_Call : Node_Id;
3561 begin
3562 if not Is_Limited_Type (Typ) then
3563 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3564 end if;
3566 if Tagged_Type_Expansion then
3567 Expand_Dispatching_Call (Call_Node);
3569 -- The following return is worrisome. Is it really OK to skip
3570 -- all remaining processing in this procedure ???
3572 return;
3574 -- VM targets
3576 else
3577 Apply_Tag_Checks (Call_Node);
3579 -- If this is a dispatching "=", we must first compare the
3580 -- tags so we generate: x.tag = y.tag and then x = y
3582 if Subp = Eq_Prim_Op then
3584 -- Mark the node as analyzed to avoid reanalyzing this
3585 -- dispatching call (which would cause a never-ending loop)
3587 Prev_Call := Relocate_Node (Call_Node);
3588 Set_Analyzed (Prev_Call);
3590 Param := First_Actual (Call_Node);
3591 New_Call :=
3592 Make_And_Then (Loc,
3593 Left_Opnd =>
3594 Make_Op_Eq (Loc,
3595 Left_Opnd =>
3596 Make_Selected_Component (Loc,
3597 Prefix => New_Value (Param),
3598 Selector_Name =>
3599 New_Occurrence_Of
3600 (First_Tag_Component (Typ), Loc)),
3602 Right_Opnd =>
3603 Make_Selected_Component (Loc,
3604 Prefix =>
3605 Unchecked_Convert_To (Typ,
3606 New_Value (Next_Actual (Param))),
3607 Selector_Name =>
3608 New_Occurrence_Of
3609 (First_Tag_Component (Typ), Loc))),
3610 Right_Opnd => Prev_Call);
3612 Rewrite (Call_Node, New_Call);
3614 Analyze_And_Resolve
3615 (Call_Node, Call_Typ, Suppress => All_Checks);
3616 end if;
3618 -- Expansion of a dispatching call results in an indirect call,
3619 -- which in turn causes current values to be killed (see
3620 -- Resolve_Call), so on VM targets we do the call here to
3621 -- ensure consistent warnings between VM and non-VM targets.
3623 Kill_Current_Values;
3624 end if;
3626 -- If this is a dispatching "=" then we must update the reference
3627 -- to the call node because we generated:
3628 -- x.tag = y.tag and then x = y
3630 if Subp = Eq_Prim_Op then
3631 Call_Node := Right_Opnd (Call_Node);
3632 end if;
3633 end;
3634 end if;
3636 -- Similarly, expand calls to RCI subprograms on which pragma
3637 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3638 -- later. Do this only when the call comes from source since we
3639 -- do not want such a rewriting to occur in expanded code.
3641 if Is_All_Remote_Call (Call_Node) then
3642 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3644 -- Similarly, do not add extra actuals for an entry call whose entity
3645 -- is a protected procedure, or for an internal protected subprogram
3646 -- call, because it will be rewritten as a protected subprogram call
3647 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3649 elsif Is_Protected_Type (Scope (Subp))
3650 and then (Ekind (Subp) = E_Procedure
3651 or else Ekind (Subp) = E_Function)
3652 then
3653 null;
3655 -- During that loop we gathered the extra actuals (the ones that
3656 -- correspond to Extra_Formals), so now they can be appended.
3658 else
3659 while Is_Non_Empty_List (Extra_Actuals) loop
3660 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3661 end loop;
3662 end if;
3664 -- At this point we have all the actuals, so this is the point at which
3665 -- the various expansion activities for actuals is carried out.
3667 Expand_Actuals (Call_Node, Subp, Post_Call);
3669 -- Verify that the actuals do not share storage. This check must be done
3670 -- on the caller side rather that inside the subprogram to avoid issues
3671 -- of parameter passing.
3673 if Check_Aliasing_Of_Parameters then
3674 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3675 end if;
3677 -- If the subprogram is a renaming, or if it is inherited, replace it in
3678 -- the call with the name of the actual subprogram being called. If this
3679 -- is a dispatching call, the run-time decides what to call. The Alias
3680 -- attribute does not apply to entries.
3682 if Nkind (Call_Node) /= N_Entry_Call_Statement
3683 and then No (Controlling_Argument (Call_Node))
3684 and then Present (Parent_Subp)
3685 and then not Is_Direct_Deep_Call (Subp)
3686 then
3687 if Present (Inherited_From_Formal (Subp)) then
3688 Parent_Subp := Inherited_From_Formal (Subp);
3689 else
3690 Parent_Subp := Ultimate_Alias (Parent_Subp);
3691 end if;
3693 -- The below setting of Entity is suspect, see F109-018 discussion???
3695 Set_Entity (Name (Call_Node), Parent_Subp);
3697 if Is_Abstract_Subprogram (Parent_Subp)
3698 and then not In_Instance
3699 then
3700 Error_Msg_NE
3701 ("cannot call abstract subprogram &!",
3702 Name (Call_Node), Parent_Subp);
3703 end if;
3705 -- Inspect all formals of derived subprogram Subp. Compare parameter
3706 -- types with the parent subprogram and check whether an actual may
3707 -- need a type conversion to the corresponding formal of the parent
3708 -- subprogram.
3710 -- Not clear whether intrinsic subprograms need such conversions. ???
3712 if not Is_Intrinsic_Subprogram (Parent_Subp)
3713 or else Is_Generic_Instance (Parent_Subp)
3714 then
3715 declare
3716 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3717 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3718 -- and resolve the newly generated construct.
3720 -------------
3721 -- Convert --
3722 -------------
3724 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3725 begin
3726 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3727 Analyze (Act);
3728 Resolve (Act, Typ);
3729 end Convert;
3731 -- Local variables
3733 Actual_Typ : Entity_Id;
3734 Formal_Typ : Entity_Id;
3735 Parent_Typ : Entity_Id;
3737 begin
3738 Actual := First_Actual (Call_Node);
3739 Formal := First_Formal (Subp);
3740 Parent_Formal := First_Formal (Parent_Subp);
3741 while Present (Formal) loop
3742 Actual_Typ := Etype (Actual);
3743 Formal_Typ := Etype (Formal);
3744 Parent_Typ := Etype (Parent_Formal);
3746 -- For an IN parameter of a scalar type, the parent formal
3747 -- type and derived formal type differ or the parent formal
3748 -- type and actual type do not match statically.
3750 if Is_Scalar_Type (Formal_Typ)
3751 and then Ekind (Formal) = E_In_Parameter
3752 and then Formal_Typ /= Parent_Typ
3753 and then
3754 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3755 and then not Raises_Constraint_Error (Actual)
3756 then
3757 Convert (Actual, Parent_Typ);
3758 Enable_Range_Check (Actual);
3760 -- If the actual has been marked as requiring a range
3761 -- check, then generate it here.
3763 if Do_Range_Check (Actual) then
3764 Generate_Range_Check
3765 (Actual, Etype (Formal), CE_Range_Check_Failed);
3766 end if;
3768 -- For access types, the parent formal type and actual type
3769 -- differ.
3771 elsif Is_Access_Type (Formal_Typ)
3772 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3773 then
3774 if Ekind (Formal) /= E_In_Parameter then
3775 Convert (Actual, Parent_Typ);
3777 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3778 and then Designated_Type (Parent_Typ) /=
3779 Designated_Type (Actual_Typ)
3780 and then not Is_Controlling_Formal (Formal)
3781 then
3782 -- This unchecked conversion is not necessary unless
3783 -- inlining is enabled, because in that case the type
3784 -- mismatch may become visible in the body about to be
3785 -- inlined.
3787 Rewrite (Actual,
3788 Unchecked_Convert_To (Parent_Typ,
3789 Relocate_Node (Actual)));
3790 Analyze (Actual);
3791 Resolve (Actual, Parent_Typ);
3792 end if;
3794 -- If there is a change of representation, then generate a
3795 -- warning, and do the change of representation.
3797 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3798 Error_Msg_N
3799 ("??change of representation required", Actual);
3800 Convert (Actual, Parent_Typ);
3802 -- For array and record types, the parent formal type and
3803 -- derived formal type have different sizes or pragma Pack
3804 -- status.
3806 elsif ((Is_Array_Type (Formal_Typ)
3807 and then Is_Array_Type (Parent_Typ))
3808 or else
3809 (Is_Record_Type (Formal_Typ)
3810 and then Is_Record_Type (Parent_Typ)))
3811 and then
3812 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3813 or else Has_Pragma_Pack (Formal_Typ) /=
3814 Has_Pragma_Pack (Parent_Typ))
3815 then
3816 Convert (Actual, Parent_Typ);
3817 end if;
3819 Next_Actual (Actual);
3820 Next_Formal (Formal);
3821 Next_Formal (Parent_Formal);
3822 end loop;
3823 end;
3824 end if;
3826 Orig_Subp := Subp;
3827 Subp := Parent_Subp;
3828 end if;
3830 -- Deal with case where call is an explicit dereference
3832 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3834 -- Handle case of access to protected subprogram type
3836 if Is_Access_Protected_Subprogram_Type
3837 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3838 then
3839 -- If this is a call through an access to protected operation, the
3840 -- prefix has the form (object'address, operation'access). Rewrite
3841 -- as a for other protected calls: the object is the 1st parameter
3842 -- of the list of actuals.
3844 declare
3845 Call : Node_Id;
3846 Parm : List_Id;
3847 Nam : Node_Id;
3848 Obj : Node_Id;
3849 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3851 T : constant Entity_Id :=
3852 Equivalent_Type (Base_Type (Etype (Ptr)));
3854 D_T : constant Entity_Id :=
3855 Designated_Type (Base_Type (Etype (Ptr)));
3857 begin
3858 Obj :=
3859 Make_Selected_Component (Loc,
3860 Prefix => Unchecked_Convert_To (T, Ptr),
3861 Selector_Name =>
3862 New_Occurrence_Of (First_Entity (T), Loc));
3864 Nam :=
3865 Make_Selected_Component (Loc,
3866 Prefix => Unchecked_Convert_To (T, Ptr),
3867 Selector_Name =>
3868 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3870 Nam :=
3871 Make_Explicit_Dereference (Loc,
3872 Prefix => Nam);
3874 if Present (Parameter_Associations (Call_Node)) then
3875 Parm := Parameter_Associations (Call_Node);
3876 else
3877 Parm := New_List;
3878 end if;
3880 Prepend (Obj, Parm);
3882 if Etype (D_T) = Standard_Void_Type then
3883 Call :=
3884 Make_Procedure_Call_Statement (Loc,
3885 Name => Nam,
3886 Parameter_Associations => Parm);
3887 else
3888 Call :=
3889 Make_Function_Call (Loc,
3890 Name => Nam,
3891 Parameter_Associations => Parm);
3892 end if;
3894 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3895 Set_Etype (Call, Etype (D_T));
3897 -- We do not re-analyze the call to avoid infinite recursion.
3898 -- We analyze separately the prefix and the object, and set
3899 -- the checks on the prefix that would otherwise be emitted
3900 -- when resolving a call.
3902 Rewrite (Call_Node, Call);
3903 Analyze (Nam);
3904 Apply_Access_Check (Nam);
3905 Analyze (Obj);
3906 return;
3907 end;
3908 end if;
3909 end if;
3911 -- If this is a call to an intrinsic subprogram, then perform the
3912 -- appropriate expansion to the corresponding tree node and we
3913 -- are all done (since after that the call is gone).
3915 -- In the case where the intrinsic is to be processed by the back end,
3916 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3917 -- since the idea in this case is to pass the call unchanged. If the
3918 -- intrinsic is an inherited unchecked conversion, and the derived type
3919 -- is the target type of the conversion, we must retain it as the return
3920 -- type of the expression. Otherwise the expansion below, which uses the
3921 -- parent operation, will yield the wrong type.
3923 if Is_Intrinsic_Subprogram (Subp) then
3924 Expand_Intrinsic_Call (Call_Node, Subp);
3926 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3927 and then Parent_Subp /= Orig_Subp
3928 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3929 then
3930 Set_Etype (Call_Node, Etype (Orig_Subp));
3931 end if;
3933 return;
3934 end if;
3936 if Ekind_In (Subp, E_Function, E_Procedure) then
3938 -- We perform a simple optimization on calls for To_Address by
3939 -- replacing them with an unchecked conversion. Not only is this
3940 -- efficient, but it also avoids order of elaboration problems when
3941 -- address clauses are inlined (address expression elaborated at the
3942 -- wrong point).
3944 -- We perform this optimization regardless of whether we are in the
3945 -- main unit or in a unit in the context of the main unit, to ensure
3946 -- that the generated tree is the same in both cases, for CodePeer
3947 -- use.
3949 if Is_RTE (Subp, RE_To_Address) then
3950 Rewrite (Call_Node,
3951 Unchecked_Convert_To
3952 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3953 return;
3954 end if;
3956 -- Handle inlining. No action needed if the subprogram is not inlined
3958 if not Is_Inlined (Subp) then
3959 null;
3961 -- Frontend inlining of expression functions (performed also when
3962 -- backend inlining is enabled).
3964 elsif Is_Inlinable_Expression_Function (Subp) then
3965 Rewrite (N, New_Copy (Expression_Of_Expression_Function (Subp)));
3966 Analyze (N);
3967 return;
3969 -- Handle frontend inlining
3971 elsif not Back_End_Inlining then
3972 Inlined_Subprogram : declare
3973 Bod : Node_Id;
3974 Must_Inline : Boolean := False;
3975 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3977 begin
3978 -- Verify that the body to inline has already been seen, and
3979 -- that if the body is in the current unit the inlining does
3980 -- not occur earlier. This avoids order-of-elaboration problems
3981 -- in the back end.
3983 -- This should be documented in sinfo/einfo ???
3985 if No (Spec)
3986 or else Nkind (Spec) /= N_Subprogram_Declaration
3987 or else No (Body_To_Inline (Spec))
3988 then
3989 Must_Inline := False;
3991 -- If this an inherited function that returns a private type,
3992 -- do not inline if the full view is an unconstrained array,
3993 -- because such calls cannot be inlined.
3995 elsif Present (Orig_Subp)
3996 and then Is_Array_Type (Etype (Orig_Subp))
3997 and then not Is_Constrained (Etype (Orig_Subp))
3998 then
3999 Must_Inline := False;
4001 elsif In_Unfrozen_Instance (Scope (Subp)) then
4002 Must_Inline := False;
4004 else
4005 Bod := Body_To_Inline (Spec);
4007 if (In_Extended_Main_Code_Unit (Call_Node)
4008 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
4009 or else Has_Pragma_Inline_Always (Subp))
4010 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
4011 or else
4012 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
4013 then
4014 Must_Inline := True;
4016 -- If we are compiling a package body that is not the main
4017 -- unit, it must be for inlining/instantiation purposes,
4018 -- in which case we inline the call to insure that the same
4019 -- temporaries are generated when compiling the body by
4020 -- itself. Otherwise link errors can occur.
4022 -- If the function being called is itself in the main unit,
4023 -- we cannot inline, because there is a risk of double
4024 -- elaboration and/or circularity: the inlining can make
4025 -- visible a private entity in the body of the main unit,
4026 -- that gigi will see before its sees its proper definition.
4028 elsif not (In_Extended_Main_Code_Unit (Call_Node))
4029 and then In_Package_Body
4030 then
4031 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4033 -- Inline calls to _postconditions when generating C code
4035 elsif Modify_Tree_For_C
4036 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4037 and then Chars (Name (N)) = Name_uPostconditions
4038 then
4039 Must_Inline := True;
4040 end if;
4041 end if;
4043 if Must_Inline then
4044 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4046 else
4047 -- Let the back end handle it
4049 Add_Inlined_Body (Subp, Call_Node);
4051 if Front_End_Inlining
4052 and then Nkind (Spec) = N_Subprogram_Declaration
4053 and then (In_Extended_Main_Code_Unit (Call_Node))
4054 and then No (Body_To_Inline (Spec))
4055 and then not Has_Completion (Subp)
4056 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4057 then
4058 Cannot_Inline
4059 ("cannot inline& (body not seen yet)?",
4060 Call_Node, Subp);
4061 end if;
4062 end if;
4063 end Inlined_Subprogram;
4065 -- Back end inlining: let the back end handle it
4067 elsif No (Unit_Declaration_Node (Subp))
4068 or else Nkind (Unit_Declaration_Node (Subp)) /=
4069 N_Subprogram_Declaration
4070 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
4071 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
4072 N_Entity
4073 then
4074 Add_Inlined_Body (Subp, Call_Node);
4076 -- If the inlined call appears within an instantiation and some
4077 -- level of optimization is required, ensure that the enclosing
4078 -- instance body is available so that the back-end can actually
4079 -- perform the inlining.
4081 if In_Instance
4082 and then Comes_From_Source (Subp)
4083 and then Optimization_Level > 0
4084 then
4085 declare
4086 Decl : Node_Id;
4087 Inst : Entity_Id;
4088 Inst_Node : Node_Id;
4090 begin
4091 Inst := Scope (Subp);
4093 -- Find enclosing instance
4095 while Present (Inst) and then Inst /= Standard_Standard loop
4096 exit when Is_Generic_Instance (Inst);
4097 Inst := Scope (Inst);
4098 end loop;
4100 if Present (Inst)
4101 and then Is_Generic_Instance (Inst)
4102 and then not Is_Inlined (Inst)
4103 then
4104 Set_Is_Inlined (Inst);
4105 Decl := Unit_Declaration_Node (Inst);
4107 -- Do not add a pending instantiation if the body exits
4108 -- already, or if the instance is a compilation unit, or
4109 -- the instance node is missing.
4111 if Present (Corresponding_Body (Decl))
4112 or else Nkind (Parent (Decl)) = N_Compilation_Unit
4113 or else No (Next (Decl))
4114 then
4115 null;
4117 else
4118 -- The instantiation node usually follows the package
4119 -- declaration for the instance. If the generic unit
4120 -- has aspect specifications, they are transformed
4121 -- into pragmas in the instance, and the instance node
4122 -- appears after them.
4124 Inst_Node := Next (Decl);
4126 while Nkind (Inst_Node) /= N_Package_Instantiation loop
4127 Inst_Node := Next (Inst_Node);
4128 end loop;
4130 Add_Pending_Instantiation (Inst_Node, Decl);
4131 end if;
4132 end if;
4133 end;
4134 end if;
4136 -- Front end expansion of simple functions returning unconstrained
4137 -- types (see Check_And_Split_Unconstrained_Function). Note that the
4138 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
4139 -- also Build_Renamed_Body) cannot be expanded here because this may
4140 -- give rise to order-of-elaboration issues for the types of the
4141 -- parameters of the subprogram, if any.
4143 else
4144 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4145 end if;
4146 end if;
4148 -- Check for protected subprogram. This is either an intra-object call,
4149 -- or a protected function call. Protected procedure calls are rewritten
4150 -- as entry calls and handled accordingly.
4152 -- In Ada 2005, this may be an indirect call to an access parameter that
4153 -- is an access_to_subprogram. In that case the anonymous type has a
4154 -- scope that is a protected operation, but the call is a regular one.
4155 -- In either case do not expand call if subprogram is eliminated.
4157 Scop := Scope (Subp);
4159 if Nkind (Call_Node) /= N_Entry_Call_Statement
4160 and then Is_Protected_Type (Scop)
4161 and then Ekind (Subp) /= E_Subprogram_Type
4162 and then not Is_Eliminated (Subp)
4163 then
4164 -- If the call is an internal one, it is rewritten as a call to the
4165 -- corresponding unprotected subprogram.
4167 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4168 end if;
4170 -- Functions returning controlled objects need special attention. If
4171 -- the return type is limited, then the context is initialization and
4172 -- different processing applies. If the call is to a protected function,
4173 -- the expansion above will call Expand_Call recursively. Otherwise the
4174 -- function call is transformed into a temporary which obtains the
4175 -- result from the secondary stack.
4177 if Needs_Finalization (Etype (Subp)) then
4178 if not Is_Limited_View (Etype (Subp))
4179 and then
4180 (No (First_Formal (Subp))
4181 or else
4182 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4183 then
4184 Expand_Ctrl_Function_Call (Call_Node);
4186 -- Build-in-place function calls which appear in anonymous contexts
4187 -- need a transient scope to ensure the proper finalization of the
4188 -- intermediate result after its use.
4190 elsif Is_Build_In_Place_Function_Call (Call_Node)
4191 and then
4192 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
4193 N_Function_Call,
4194 N_Indexed_Component,
4195 N_Object_Renaming_Declaration,
4196 N_Procedure_Call_Statement,
4197 N_Selected_Component,
4198 N_Slice)
4199 then
4200 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
4201 end if;
4202 end if;
4203 end Expand_Call_Helper;
4205 -------------------------------
4206 -- Expand_Ctrl_Function_Call --
4207 -------------------------------
4209 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4210 function Is_Element_Reference (N : Node_Id) return Boolean;
4211 -- Determine whether node N denotes a reference to an Ada 2012 container
4212 -- element.
4214 --------------------------
4215 -- Is_Element_Reference --
4216 --------------------------
4218 function Is_Element_Reference (N : Node_Id) return Boolean is
4219 Ref : constant Node_Id := Original_Node (N);
4221 begin
4222 -- Analysis marks an element reference by setting the generalized
4223 -- indexing attribute of an indexed component before the component
4224 -- is rewritten into a function call.
4226 return
4227 Nkind (Ref) = N_Indexed_Component
4228 and then Present (Generalized_Indexing (Ref));
4229 end Is_Element_Reference;
4231 -- Start of processing for Expand_Ctrl_Function_Call
4233 begin
4234 -- Optimization, if the returned value (which is on the sec-stack) is
4235 -- returned again, no need to copy/readjust/finalize, we can just pass
4236 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4237 -- attachment is needed
4239 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4240 return;
4241 end if;
4243 -- Resolution is now finished, make sure we don't start analysis again
4244 -- because of the duplication.
4246 Set_Analyzed (N);
4248 -- A function which returns a controlled object uses the secondary
4249 -- stack. Rewrite the call into a temporary which obtains the result of
4250 -- the function using 'reference.
4252 Remove_Side_Effects (N);
4254 -- The side effect removal of the function call produced a temporary.
4255 -- When the context is a case expression, if expression, or expression
4256 -- with actions, the lifetime of the temporary must be extended to match
4257 -- that of the context. Otherwise the function result will be finalized
4258 -- too early and affect the result of the expression. To prevent this
4259 -- unwanted effect, the temporary should not be considered for clean up
4260 -- actions by the general finalization machinery.
4262 -- Exception to this rule are references to Ada 2012 container elements.
4263 -- Such references must be finalized at the end of each iteration of the
4264 -- related quantified expression, otherwise the container will remain
4265 -- busy.
4267 if Nkind (N) = N_Explicit_Dereference
4268 and then Within_Case_Or_If_Expression (N)
4269 and then not Is_Element_Reference (N)
4270 then
4271 Set_Is_Ignored_Transient (Entity (Prefix (N)));
4272 end if;
4273 end Expand_Ctrl_Function_Call;
4275 ----------------------------------------
4276 -- Expand_N_Extended_Return_Statement --
4277 ----------------------------------------
4279 -- If there is a Handled_Statement_Sequence, we rewrite this:
4281 -- return Result : T := <expression> do
4282 -- <handled_seq_of_stms>
4283 -- end return;
4285 -- to be:
4287 -- declare
4288 -- Result : T := <expression>;
4289 -- begin
4290 -- <handled_seq_of_stms>
4291 -- return Result;
4292 -- end;
4294 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4296 -- return Result : T := <expression>;
4298 -- to be:
4300 -- return <expression>;
4302 -- unless it's build-in-place or there's no <expression>, in which case
4303 -- we generate:
4305 -- declare
4306 -- Result : T := <expression>;
4307 -- begin
4308 -- return Result;
4309 -- end;
4311 -- Note that this case could have been written by the user as an extended
4312 -- return statement, or could have been transformed to this from a simple
4313 -- return statement.
4315 -- That is, we need to have a reified return object if there are statements
4316 -- (which might refer to it) or if we're doing build-in-place (so we can
4317 -- set its address to the final resting place or if there is no expression
4318 -- (in which case default initial values might need to be set).
4320 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4321 Loc : constant Source_Ptr := Sloc (N);
4323 function Build_Heap_Allocator
4324 (Temp_Id : Entity_Id;
4325 Temp_Typ : Entity_Id;
4326 Func_Id : Entity_Id;
4327 Ret_Typ : Entity_Id;
4328 Alloc_Expr : Node_Id) return Node_Id;
4329 -- Create the statements necessary to allocate a return object on the
4330 -- caller's master. The master is available through implicit parameter
4331 -- BIPfinalizationmaster.
4333 -- if BIPfinalizationmaster /= null then
4334 -- declare
4335 -- type Ptr_Typ is access Ret_Typ;
4336 -- for Ptr_Typ'Storage_Pool use
4337 -- Base_Pool (BIPfinalizationmaster.all).all;
4338 -- Local : Ptr_Typ;
4340 -- begin
4341 -- procedure Allocate (...) is
4342 -- begin
4343 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4344 -- end Allocate;
4346 -- Local := <Alloc_Expr>;
4347 -- Temp_Id := Temp_Typ (Local);
4348 -- end;
4349 -- end if;
4351 -- Temp_Id is the temporary which is used to reference the internally
4352 -- created object in all allocation forms. Temp_Typ is the type of the
4353 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4354 -- type of Func_Id. Alloc_Expr is the actual allocator.
4356 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
4357 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4358 -- with parameters:
4359 -- From current activation chain
4360 -- To activation chain passed in by the caller
4361 -- New_Master master passed in by the caller
4363 -- Func_Id is the entity of the function where the extended return
4364 -- statement appears.
4366 --------------------------
4367 -- Build_Heap_Allocator --
4368 --------------------------
4370 function Build_Heap_Allocator
4371 (Temp_Id : Entity_Id;
4372 Temp_Typ : Entity_Id;
4373 Func_Id : Entity_Id;
4374 Ret_Typ : Entity_Id;
4375 Alloc_Expr : Node_Id) return Node_Id
4377 begin
4378 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4380 -- Processing for build-in-place object allocation.
4382 if Needs_Finalization (Ret_Typ) then
4383 declare
4384 Decls : constant List_Id := New_List;
4385 Fin_Mas_Id : constant Entity_Id :=
4386 Build_In_Place_Formal
4387 (Func_Id, BIP_Finalization_Master);
4388 Stmts : constant List_Id := New_List;
4389 Desig_Typ : Entity_Id;
4390 Local_Id : Entity_Id;
4391 Pool_Id : Entity_Id;
4392 Ptr_Typ : Entity_Id;
4394 begin
4395 -- Generate:
4396 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4398 Pool_Id := Make_Temporary (Loc, 'P');
4400 Append_To (Decls,
4401 Make_Object_Renaming_Declaration (Loc,
4402 Defining_Identifier => Pool_Id,
4403 Subtype_Mark =>
4404 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4405 Name =>
4406 Make_Explicit_Dereference (Loc,
4407 Prefix =>
4408 Make_Function_Call (Loc,
4409 Name =>
4410 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4411 Parameter_Associations => New_List (
4412 Make_Explicit_Dereference (Loc,
4413 Prefix =>
4414 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4416 -- Create an access type which uses the storage pool of the
4417 -- caller's master. This additional type is necessary because
4418 -- the finalization master cannot be associated with the type
4419 -- of the temporary. Otherwise the secondary stack allocation
4420 -- will fail.
4422 Desig_Typ := Ret_Typ;
4424 -- Ensure that the build-in-place machinery uses a fat pointer
4425 -- when allocating an unconstrained array on the heap. In this
4426 -- case the result object type is a constrained array type even
4427 -- though the function type is unconstrained.
4429 if Ekind (Desig_Typ) = E_Array_Subtype then
4430 Desig_Typ := Base_Type (Desig_Typ);
4431 end if;
4433 -- Generate:
4434 -- type Ptr_Typ is access Desig_Typ;
4436 Ptr_Typ := Make_Temporary (Loc, 'P');
4438 Append_To (Decls,
4439 Make_Full_Type_Declaration (Loc,
4440 Defining_Identifier => Ptr_Typ,
4441 Type_Definition =>
4442 Make_Access_To_Object_Definition (Loc,
4443 Subtype_Indication =>
4444 New_Occurrence_Of (Desig_Typ, Loc))));
4446 -- Perform minor decoration in order to set the master and the
4447 -- storage pool attributes.
4449 Set_Ekind (Ptr_Typ, E_Access_Type);
4450 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4451 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4453 -- Create the temporary, generate:
4454 -- Local_Id : Ptr_Typ;
4456 Local_Id := Make_Temporary (Loc, 'T');
4458 Append_To (Decls,
4459 Make_Object_Declaration (Loc,
4460 Defining_Identifier => Local_Id,
4461 Object_Definition =>
4462 New_Occurrence_Of (Ptr_Typ, Loc)));
4464 -- Allocate the object, generate:
4465 -- Local_Id := <Alloc_Expr>;
4467 Append_To (Stmts,
4468 Make_Assignment_Statement (Loc,
4469 Name => New_Occurrence_Of (Local_Id, Loc),
4470 Expression => Alloc_Expr));
4472 -- Generate:
4473 -- Temp_Id := Temp_Typ (Local_Id);
4475 Append_To (Stmts,
4476 Make_Assignment_Statement (Loc,
4477 Name => New_Occurrence_Of (Temp_Id, Loc),
4478 Expression =>
4479 Unchecked_Convert_To (Temp_Typ,
4480 New_Occurrence_Of (Local_Id, Loc))));
4482 -- Wrap the allocation in a block. This is further conditioned
4483 -- by checking the caller finalization master at runtime. A
4484 -- null value indicates a non-existent master, most likely due
4485 -- to a Finalize_Storage_Only allocation.
4487 -- Generate:
4488 -- if BIPfinalizationmaster /= null then
4489 -- declare
4490 -- <Decls>
4491 -- begin
4492 -- <Stmts>
4493 -- end;
4494 -- end if;
4496 return
4497 Make_If_Statement (Loc,
4498 Condition =>
4499 Make_Op_Ne (Loc,
4500 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4501 Right_Opnd => Make_Null (Loc)),
4503 Then_Statements => New_List (
4504 Make_Block_Statement (Loc,
4505 Declarations => Decls,
4506 Handled_Statement_Sequence =>
4507 Make_Handled_Sequence_Of_Statements (Loc,
4508 Statements => Stmts))));
4509 end;
4511 -- For all other cases, generate:
4512 -- Temp_Id := <Alloc_Expr>;
4514 else
4515 return
4516 Make_Assignment_Statement (Loc,
4517 Name => New_Occurrence_Of (Temp_Id, Loc),
4518 Expression => Alloc_Expr);
4519 end if;
4520 end Build_Heap_Allocator;
4522 ---------------------------
4523 -- Move_Activation_Chain --
4524 ---------------------------
4526 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
4527 begin
4528 return
4529 Make_Procedure_Call_Statement (Loc,
4530 Name =>
4531 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4533 Parameter_Associations => New_List (
4535 -- Source chain
4537 Make_Attribute_Reference (Loc,
4538 Prefix => Make_Identifier (Loc, Name_uChain),
4539 Attribute_Name => Name_Unrestricted_Access),
4541 -- Destination chain
4543 New_Occurrence_Of
4544 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
4546 -- New master
4548 New_Occurrence_Of
4549 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
4550 end Move_Activation_Chain;
4552 -- Local variables
4554 Func_Id : constant Entity_Id :=
4555 Return_Applies_To (Return_Statement_Entity (N));
4556 Is_BIP_Func : constant Boolean :=
4557 Is_Build_In_Place_Function (Func_Id);
4558 Ret_Obj_Id : constant Entity_Id :=
4559 First_Entity (Return_Statement_Entity (N));
4560 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4561 Ret_Typ : constant Entity_Id := Etype (Func_Id);
4563 Exp : Node_Id;
4564 HSS : Node_Id;
4565 Result : Node_Id;
4566 Return_Stmt : Node_Id;
4567 Stmts : List_Id;
4569 -- Start of processing for Expand_N_Extended_Return_Statement
4571 begin
4572 -- Given that functionality of interface thunks is simple (just displace
4573 -- the pointer to the object) they are always handled by means of
4574 -- simple return statements.
4576 pragma Assert (not Is_Thunk (Current_Scope));
4578 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4579 Exp := Expression (Ret_Obj_Decl);
4580 else
4581 Exp := Empty;
4582 end if;
4584 HSS := Handled_Statement_Sequence (N);
4586 -- If the returned object needs finalization actions, the function must
4587 -- perform the appropriate cleanup should it fail to return. The state
4588 -- of the function itself is tracked through a flag which is coupled
4589 -- with the scope finalizer. There is one flag per each return object
4590 -- in case of multiple returns.
4592 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4593 declare
4594 Flag_Decl : Node_Id;
4595 Flag_Id : Entity_Id;
4596 Func_Bod : Node_Id;
4598 begin
4599 -- Recover the function body
4601 Func_Bod := Unit_Declaration_Node (Func_Id);
4603 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4604 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4605 end if;
4607 -- Create a flag to track the function state
4609 Flag_Id := Make_Temporary (Loc, 'F');
4610 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4612 -- Insert the flag at the beginning of the function declarations,
4613 -- generate:
4614 -- Fnn : Boolean := False;
4616 Flag_Decl :=
4617 Make_Object_Declaration (Loc,
4618 Defining_Identifier => Flag_Id,
4619 Object_Definition =>
4620 New_Occurrence_Of (Standard_Boolean, Loc),
4621 Expression =>
4622 New_Occurrence_Of (Standard_False, Loc));
4624 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4625 Analyze (Flag_Decl);
4626 end;
4627 end if;
4629 -- Build a simple_return_statement that returns the return object when
4630 -- there is a statement sequence, or no expression, or the result will
4631 -- be built in place. Note however that we currently do this for all
4632 -- composite cases, even though nonlimited composite results are not yet
4633 -- built in place (though we plan to do so eventually).
4635 if Present (HSS)
4636 or else Is_Composite_Type (Ret_Typ)
4637 or else No (Exp)
4638 then
4639 if No (HSS) then
4640 Stmts := New_List;
4642 -- If the extended return has a handled statement sequence, then wrap
4643 -- it in a block and use the block as the first statement.
4645 else
4646 Stmts := New_List (
4647 Make_Block_Statement (Loc,
4648 Declarations => New_List,
4649 Handled_Statement_Sequence => HSS));
4650 end if;
4652 -- If the result type contains tasks, we call Move_Activation_Chain.
4653 -- Later, the cleanup code will call Complete_Master, which will
4654 -- terminate any unactivated tasks belonging to the return statement
4655 -- master. But Move_Activation_Chain updates their master to be that
4656 -- of the caller, so they will not be terminated unless the return
4657 -- statement completes unsuccessfully due to exception, abort, goto,
4658 -- or exit. As a formality, we test whether the function requires the
4659 -- result to be built in place, though that's necessarily true for
4660 -- the case of result types with task parts.
4662 if Is_BIP_Func and then Has_Task (Ret_Typ) then
4664 -- The return expression is an aggregate for a complex type which
4665 -- contains tasks. This particular case is left unexpanded since
4666 -- the regular expansion would insert all temporaries and
4667 -- initialization code in the wrong block.
4669 if Nkind (Exp) = N_Aggregate then
4670 Expand_N_Aggregate (Exp);
4671 end if;
4673 -- Do not move the activation chain if the return object does not
4674 -- contain tasks.
4676 if Has_Task (Etype (Ret_Obj_Id)) then
4677 Append_To (Stmts, Move_Activation_Chain (Func_Id));
4678 end if;
4679 end if;
4681 -- Update the state of the function right before the object is
4682 -- returned.
4684 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4685 declare
4686 Flag_Id : constant Entity_Id :=
4687 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4689 begin
4690 -- Generate:
4691 -- Fnn := True;
4693 Append_To (Stmts,
4694 Make_Assignment_Statement (Loc,
4695 Name => New_Occurrence_Of (Flag_Id, Loc),
4696 Expression => New_Occurrence_Of (Standard_True, Loc)));
4697 end;
4698 end if;
4700 -- Build a simple_return_statement that returns the return object
4702 Return_Stmt :=
4703 Make_Simple_Return_Statement (Loc,
4704 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4705 Append_To (Stmts, Return_Stmt);
4707 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4708 end if;
4710 -- Case where we build a return statement block
4712 if Present (HSS) then
4713 Result :=
4714 Make_Block_Statement (Loc,
4715 Declarations => Return_Object_Declarations (N),
4716 Handled_Statement_Sequence => HSS);
4718 -- We set the entity of the new block statement to be that of the
4719 -- return statement. This is necessary so that various fields, such
4720 -- as Finalization_Chain_Entity carry over from the return statement
4721 -- to the block. Note that this block is unusual, in that its entity
4722 -- is an E_Return_Statement rather than an E_Block.
4724 Set_Identifier
4725 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4727 -- If the object decl was already rewritten as a renaming, then we
4728 -- don't want to do the object allocation and transformation of
4729 -- the return object declaration to a renaming. This case occurs
4730 -- when the return object is initialized by a call to another
4731 -- build-in-place function, and that function is responsible for
4732 -- the allocation of the return object.
4734 if Is_BIP_Func
4735 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4736 then
4737 pragma Assert
4738 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4739 and then Is_Build_In_Place_Function_Call
4740 (Expression (Original_Node (Ret_Obj_Decl))));
4742 -- Return the build-in-place result by reference
4744 Set_By_Ref (Return_Stmt);
4746 elsif Is_BIP_Func then
4748 -- Locate the implicit access parameter associated with the
4749 -- caller-supplied return object and convert the return
4750 -- statement's return object declaration to a renaming of a
4751 -- dereference of the access parameter. If the return object's
4752 -- declaration includes an expression that has not already been
4753 -- expanded as separate assignments, then add an assignment
4754 -- statement to ensure the return object gets initialized.
4756 -- declare
4757 -- Result : T [:= <expression>];
4758 -- begin
4759 -- ...
4761 -- is converted to
4763 -- declare
4764 -- Result : T renames FuncRA.all;
4765 -- [Result := <expression;]
4766 -- begin
4767 -- ...
4769 declare
4770 Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl);
4771 Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id);
4773 Init_Assignment : Node_Id := Empty;
4774 Obj_Acc_Formal : Entity_Id;
4775 Obj_Acc_Deref : Node_Id;
4776 Obj_Alloc_Formal : Entity_Id;
4778 begin
4779 -- Build-in-place results must be returned by reference
4781 Set_By_Ref (Return_Stmt);
4783 -- Retrieve the implicit access parameter passed by the caller
4785 Obj_Acc_Formal :=
4786 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
4788 -- If the return object's declaration includes an expression
4789 -- and the declaration isn't marked as No_Initialization, then
4790 -- we need to generate an assignment to the object and insert
4791 -- it after the declaration before rewriting it as a renaming
4792 -- (otherwise we'll lose the initialization). The case where
4793 -- the result type is an interface (or class-wide interface)
4794 -- is also excluded because the context of the function call
4795 -- must be unconstrained, so the initialization will always
4796 -- be done as part of an allocator evaluation (storage pool
4797 -- or secondary stack), never to a constrained target object
4798 -- passed in by the caller. Besides the assignment being
4799 -- unneeded in this case, it avoids problems with trying to
4800 -- generate a dispatching assignment when the return expression
4801 -- is a nonlimited descendant of a limited interface (the
4802 -- interface has no assignment operation).
4804 if Present (Ret_Obj_Expr)
4805 and then not No_Initialization (Ret_Obj_Decl)
4806 and then not Is_Interface (Ret_Obj_Typ)
4807 then
4808 Init_Assignment :=
4809 Make_Assignment_Statement (Loc,
4810 Name => New_Occurrence_Of (Ret_Obj_Id, Loc),
4811 Expression => New_Copy_Tree (Ret_Obj_Expr));
4813 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
4814 Set_Assignment_OK (Name (Init_Assignment));
4815 Set_No_Ctrl_Actions (Init_Assignment);
4817 Set_Parent (Name (Init_Assignment), Init_Assignment);
4818 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4820 Set_Expression (Ret_Obj_Decl, Empty);
4822 if Is_Class_Wide_Type (Etype (Ret_Obj_Id))
4823 and then not Is_Class_Wide_Type
4824 (Etype (Expression (Init_Assignment)))
4825 then
4826 Rewrite (Expression (Init_Assignment),
4827 Make_Type_Conversion (Loc,
4828 Subtype_Mark =>
4829 New_Occurrence_Of (Etype (Ret_Obj_Id), Loc),
4830 Expression =>
4831 Relocate_Node (Expression (Init_Assignment))));
4832 end if;
4834 -- In the case of functions where the calling context can
4835 -- determine the form of allocation needed, initialization
4836 -- is done with each part of the if statement that handles
4837 -- the different forms of allocation (this is true for
4838 -- unconstrained and tagged result subtypes).
4840 if Is_Constrained (Ret_Typ)
4841 and then not Is_Tagged_Type (Underlying_Type (Ret_Typ))
4842 then
4843 Insert_After (Ret_Obj_Decl, Init_Assignment);
4844 end if;
4845 end if;
4847 -- When the function's subtype is unconstrained, a run-time
4848 -- test is needed to determine the form of allocation to use
4849 -- for the return object. The function has an implicit formal
4850 -- parameter indicating this. If the BIP_Alloc_Form formal has
4851 -- the value one, then the caller has passed access to an
4852 -- existing object for use as the return object. If the value
4853 -- is two, then the return object must be allocated on the
4854 -- secondary stack. Otherwise, the object must be allocated in
4855 -- a storage pool (currently only supported for the global
4856 -- heap, user-defined storage pools TBD ???). We generate an
4857 -- if statement to test the implicit allocation formal and
4858 -- initialize a local access value appropriately, creating
4859 -- allocators in the secondary stack and global heap cases.
4860 -- The special formal also exists and must be tested when the
4861 -- function has a tagged result, even when the result subtype
4862 -- is constrained, because in general such functions can be
4863 -- called in dispatching contexts and must be handled similarly
4864 -- to functions with a class-wide result.
4866 if not Is_Constrained (Ret_Typ)
4867 or else Is_Tagged_Type (Underlying_Type (Ret_Typ))
4868 then
4869 Obj_Alloc_Formal :=
4870 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
4872 declare
4873 Pool_Id : constant Entity_Id :=
4874 Make_Temporary (Loc, 'P');
4875 Alloc_Obj_Id : Entity_Id;
4876 Alloc_Obj_Decl : Node_Id;
4877 Alloc_If_Stmt : Node_Id;
4878 Heap_Allocator : Node_Id;
4879 Pool_Decl : Node_Id;
4880 Pool_Allocator : Node_Id;
4881 Ptr_Type_Decl : Node_Id;
4882 Ref_Type : Entity_Id;
4883 SS_Allocator : Node_Id;
4885 begin
4886 -- Reuse the itype created for the function's implicit
4887 -- access formal. This avoids the need to create a new
4888 -- access type here, plus it allows assigning the access
4889 -- formal directly without applying a conversion.
4891 -- Ref_Type := Etype (Object_Access);
4893 -- Create an access type designating the function's
4894 -- result subtype.
4896 Ref_Type := Make_Temporary (Loc, 'A');
4898 Ptr_Type_Decl :=
4899 Make_Full_Type_Declaration (Loc,
4900 Defining_Identifier => Ref_Type,
4901 Type_Definition =>
4902 Make_Access_To_Object_Definition (Loc,
4903 All_Present => True,
4904 Subtype_Indication =>
4905 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
4907 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
4909 -- Create an access object that will be initialized to an
4910 -- access value denoting the return object, either coming
4911 -- from an implicit access value passed in by the caller
4912 -- or from the result of an allocator.
4914 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4915 Set_Etype (Alloc_Obj_Id, Ref_Type);
4917 Alloc_Obj_Decl :=
4918 Make_Object_Declaration (Loc,
4919 Defining_Identifier => Alloc_Obj_Id,
4920 Object_Definition =>
4921 New_Occurrence_Of (Ref_Type, Loc));
4923 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
4925 -- Create allocators for both the secondary stack and
4926 -- global heap. If there's an initialization expression,
4927 -- then create these as initialized allocators.
4929 if Present (Ret_Obj_Expr)
4930 and then not No_Initialization (Ret_Obj_Decl)
4931 then
4932 -- Always use the type of the expression for the
4933 -- qualified expression, rather than the result type.
4934 -- In general we cannot always use the result type
4935 -- for the allocator, because the expression might be
4936 -- of a specific type, such as in the case of an
4937 -- aggregate or even a nonlimited object when the
4938 -- result type is a limited class-wide interface type.
4940 Heap_Allocator :=
4941 Make_Allocator (Loc,
4942 Expression =>
4943 Make_Qualified_Expression (Loc,
4944 Subtype_Mark =>
4945 New_Occurrence_Of
4946 (Etype (Ret_Obj_Expr), Loc),
4947 Expression => New_Copy_Tree (Ret_Obj_Expr)));
4949 else
4950 -- If the function returns a class-wide type we cannot
4951 -- use the return type for the allocator. Instead we
4952 -- use the type of the expression, which must be an
4953 -- aggregate of a definite type.
4955 if Is_Class_Wide_Type (Ret_Obj_Typ) then
4956 Heap_Allocator :=
4957 Make_Allocator (Loc,
4958 Expression =>
4959 New_Occurrence_Of
4960 (Etype (Ret_Obj_Expr), Loc));
4961 else
4962 Heap_Allocator :=
4963 Make_Allocator (Loc,
4964 Expression =>
4965 New_Occurrence_Of (Ret_Obj_Typ, Loc));
4966 end if;
4968 -- If the object requires default initialization then
4969 -- that will happen later following the elaboration of
4970 -- the object renaming. If we don't turn it off here
4971 -- then the object will be default initialized twice.
4973 Set_No_Initialization (Heap_Allocator);
4974 end if;
4976 -- The Pool_Allocator is just like the Heap_Allocator,
4977 -- except we set Storage_Pool and Procedure_To_Call so
4978 -- it will use the user-defined storage pool.
4980 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
4982 -- Do not generate the renaming of the build-in-place
4983 -- pool parameter on ZFP because the parameter is not
4984 -- created in the first place.
4986 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
4987 Pool_Decl :=
4988 Make_Object_Renaming_Declaration (Loc,
4989 Defining_Identifier => Pool_Id,
4990 Subtype_Mark =>
4991 New_Occurrence_Of
4992 (RTE (RE_Root_Storage_Pool), Loc),
4993 Name =>
4994 Make_Explicit_Dereference (Loc,
4995 New_Occurrence_Of
4996 (Build_In_Place_Formal
4997 (Func_Id, BIP_Storage_Pool), Loc)));
4998 Set_Storage_Pool (Pool_Allocator, Pool_Id);
4999 Set_Procedure_To_Call
5000 (Pool_Allocator, RTE (RE_Allocate_Any));
5001 else
5002 Pool_Decl := Make_Null_Statement (Loc);
5003 end if;
5005 -- If the No_Allocators restriction is active, then only
5006 -- an allocator for secondary stack allocation is needed.
5007 -- It's OK for such allocators to have Comes_From_Source
5008 -- set to False, because gigi knows not to flag them as
5009 -- being a violation of No_Implicit_Heap_Allocations.
5011 if Restriction_Active (No_Allocators) then
5012 SS_Allocator := Heap_Allocator;
5013 Heap_Allocator := Make_Null (Loc);
5014 Pool_Allocator := Make_Null (Loc);
5016 -- Otherwise the heap and pool allocators may be needed,
5017 -- so we make another allocator for secondary stack
5018 -- allocation.
5020 else
5021 SS_Allocator := New_Copy_Tree (Heap_Allocator);
5023 -- The heap and pool allocators are marked as
5024 -- Comes_From_Source since they correspond to an
5025 -- explicit user-written allocator (that is, it will
5026 -- only be executed on behalf of callers that call the
5027 -- function as initialization for such an allocator).
5028 -- Prevents errors when No_Implicit_Heap_Allocations
5029 -- is in force.
5031 Set_Comes_From_Source (Heap_Allocator, True);
5032 Set_Comes_From_Source (Pool_Allocator, True);
5033 end if;
5035 -- The allocator is returned on the secondary stack.
5037 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5038 Set_Procedure_To_Call
5039 (SS_Allocator, RTE (RE_SS_Allocate));
5041 -- The allocator is returned on the secondary stack,
5042 -- so indicate that the function return, as well as
5043 -- all blocks that encloses the allocator, must not
5044 -- release it. The flags must be set now because
5045 -- the decision to use the secondary stack is done
5046 -- very late in the course of expanding the return
5047 -- statement, past the point where these flags are
5048 -- normally set.
5050 Set_Uses_Sec_Stack (Func_Id);
5051 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5052 Set_Sec_Stack_Needed_For_Return
5053 (Return_Statement_Entity (N));
5054 Set_Enclosing_Sec_Stack_Return (N);
5056 -- Create an if statement to test the BIP_Alloc_Form
5057 -- formal and initialize the access object to either the
5058 -- BIP_Object_Access formal (BIP_Alloc_Form =
5059 -- Caller_Allocation), the result of allocating the
5060 -- object in the secondary stack (BIP_Alloc_Form =
5061 -- Secondary_Stack), or else an allocator to create the
5062 -- return object in the heap or user-defined pool
5063 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5065 -- ??? An unchecked type conversion must be made in the
5066 -- case of assigning the access object formal to the
5067 -- local access object, because a normal conversion would
5068 -- be illegal in some cases (such as converting access-
5069 -- to-unconstrained to access-to-constrained), but the
5070 -- the unchecked conversion will presumably fail to work
5071 -- right in just such cases. It's not clear at all how to
5072 -- handle this. ???
5074 Alloc_If_Stmt :=
5075 Make_If_Statement (Loc,
5076 Condition =>
5077 Make_Op_Eq (Loc,
5078 Left_Opnd =>
5079 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5080 Right_Opnd =>
5081 Make_Integer_Literal (Loc,
5082 UI_From_Int (BIP_Allocation_Form'Pos
5083 (Caller_Allocation)))),
5085 Then_Statements => New_List (
5086 Make_Assignment_Statement (Loc,
5087 Name =>
5088 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5089 Expression =>
5090 Make_Unchecked_Type_Conversion (Loc,
5091 Subtype_Mark =>
5092 New_Occurrence_Of (Ref_Type, Loc),
5093 Expression =>
5094 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
5096 Elsif_Parts => New_List (
5097 Make_Elsif_Part (Loc,
5098 Condition =>
5099 Make_Op_Eq (Loc,
5100 Left_Opnd =>
5101 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5102 Right_Opnd =>
5103 Make_Integer_Literal (Loc,
5104 UI_From_Int (BIP_Allocation_Form'Pos
5105 (Secondary_Stack)))),
5107 Then_Statements => New_List (
5108 Make_Assignment_Statement (Loc,
5109 Name =>
5110 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5111 Expression => SS_Allocator))),
5113 Make_Elsif_Part (Loc,
5114 Condition =>
5115 Make_Op_Eq (Loc,
5116 Left_Opnd =>
5117 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5118 Right_Opnd =>
5119 Make_Integer_Literal (Loc,
5120 UI_From_Int (BIP_Allocation_Form'Pos
5121 (Global_Heap)))),
5123 Then_Statements => New_List (
5124 Build_Heap_Allocator
5125 (Temp_Id => Alloc_Obj_Id,
5126 Temp_Typ => Ref_Type,
5127 Func_Id => Func_Id,
5128 Ret_Typ => Ret_Obj_Typ,
5129 Alloc_Expr => Heap_Allocator)))),
5131 Else_Statements => New_List (
5132 Pool_Decl,
5133 Build_Heap_Allocator
5134 (Temp_Id => Alloc_Obj_Id,
5135 Temp_Typ => Ref_Type,
5136 Func_Id => Func_Id,
5137 Ret_Typ => Ret_Obj_Typ,
5138 Alloc_Expr => Pool_Allocator)));
5140 -- If a separate initialization assignment was created
5141 -- earlier, append that following the assignment of the
5142 -- implicit access formal to the access object, to ensure
5143 -- that the return object is initialized in that case. In
5144 -- this situation, the target of the assignment must be
5145 -- rewritten to denote a dereference of the access to the
5146 -- return object passed in by the caller.
5148 if Present (Init_Assignment) then
5149 Rewrite (Name (Init_Assignment),
5150 Make_Explicit_Dereference (Loc,
5151 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
5153 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5155 Append_To
5156 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5157 end if;
5159 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5161 -- Remember the local access object for use in the
5162 -- dereference of the renaming created below.
5164 Obj_Acc_Formal := Alloc_Obj_Id;
5165 end;
5166 end if;
5168 -- Replace the return object declaration with a renaming of a
5169 -- dereference of the access value designating the return
5170 -- object.
5172 Obj_Acc_Deref :=
5173 Make_Explicit_Dereference (Loc,
5174 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
5176 Rewrite (Ret_Obj_Decl,
5177 Make_Object_Renaming_Declaration (Loc,
5178 Defining_Identifier => Ret_Obj_Id,
5179 Access_Definition => Empty,
5180 Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc),
5181 Name => Obj_Acc_Deref));
5183 Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref);
5184 end;
5185 end if;
5187 -- Case where we do not build a block
5189 else
5190 -- We're about to drop Return_Object_Declarations on the floor, so
5191 -- we need to insert it, in case it got expanded into useful code.
5192 -- Remove side effects from expression, which may be duplicated in
5193 -- subsequent checks (see Expand_Simple_Function_Return).
5195 Insert_List_Before (N, Return_Object_Declarations (N));
5196 Remove_Side_Effects (Exp);
5198 -- Build simple_return_statement that returns the expression directly
5200 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5201 Result := Return_Stmt;
5202 end if;
5204 -- Set the flag to prevent infinite recursion
5206 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5208 Rewrite (N, Result);
5209 Analyze (N);
5210 end Expand_N_Extended_Return_Statement;
5212 ----------------------------
5213 -- Expand_N_Function_Call --
5214 ----------------------------
5216 procedure Expand_N_Function_Call (N : Node_Id) is
5217 begin
5218 Expand_Call (N);
5219 end Expand_N_Function_Call;
5221 ---------------------------------------
5222 -- Expand_N_Procedure_Call_Statement --
5223 ---------------------------------------
5225 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5226 begin
5227 Expand_Call (N);
5228 end Expand_N_Procedure_Call_Statement;
5230 --------------------------------------
5231 -- Expand_N_Simple_Return_Statement --
5232 --------------------------------------
5234 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5235 begin
5236 -- Defend against previous errors (i.e. the return statement calls a
5237 -- function that is not available in configurable runtime).
5239 if Present (Expression (N))
5240 and then Nkind (Expression (N)) = N_Empty
5241 then
5242 Check_Error_Detected;
5243 return;
5244 end if;
5246 -- Distinguish the function and non-function cases:
5248 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5249 when E_Function
5250 | E_Generic_Function
5252 Expand_Simple_Function_Return (N);
5254 when E_Entry
5255 | E_Entry_Family
5256 | E_Generic_Procedure
5257 | E_Procedure
5258 | E_Return_Statement
5260 Expand_Non_Function_Return (N);
5262 when others =>
5263 raise Program_Error;
5264 end case;
5266 exception
5267 when RE_Not_Available =>
5268 return;
5269 end Expand_N_Simple_Return_Statement;
5271 ------------------------------
5272 -- Expand_N_Subprogram_Body --
5273 ------------------------------
5275 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5276 -- by the back-end.
5278 -- Add dummy push/pop label nodes at start and end to clear any local
5279 -- exception indications if local-exception-to-goto optimization is active.
5281 -- Add return statement if last statement in body is not a return statement
5282 -- (this makes things easier on Gigi which does not want to have to handle
5283 -- a missing return).
5285 -- Add call to Activate_Tasks if body is a task activator
5287 -- Deal with possible detection of infinite recursion
5289 -- Eliminate body completely if convention stubbed
5291 -- Encode entity names within body, since we will not need to reference
5292 -- these entities any longer in the front end.
5294 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5296 -- Reset Pure indication if any parameter has root type System.Address
5297 -- or has any parameters of limited types, where limited means that the
5298 -- run-time view is limited (i.e. the full type is limited).
5300 -- Wrap thread body
5302 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5303 Body_Id : constant Entity_Id := Defining_Entity (N);
5304 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5305 Loc : constant Source_Ptr := Sloc (N);
5307 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5308 -- Append a return statement to the statement sequence Stmts if the last
5309 -- statement is not already a return or a goto statement. Note that the
5310 -- latter test is not critical, it does not matter if we add a few extra
5311 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5312 -- the corresponding spec of the subprogram body.
5314 ----------------
5315 -- Add_Return --
5316 ----------------
5318 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5319 Last_Stmt : Node_Id;
5320 Loc : Source_Ptr;
5321 Stmt : Node_Id;
5323 begin
5324 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5325 -- not relevant in this context since they are not executable.
5327 Last_Stmt := Last (Stmts);
5328 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5329 Prev (Last_Stmt);
5330 end loop;
5332 -- Now insert return unless last statement is a transfer
5334 if not Is_Transfer (Last_Stmt) then
5336 -- The source location for the return is the end label of the
5337 -- procedure if present. Otherwise use the sloc of the last
5338 -- statement in the list. If the list comes from a generated
5339 -- exception handler and we are not debugging generated code,
5340 -- all the statements within the handler are made invisible
5341 -- to the debugger.
5343 if Nkind (Parent (Stmts)) = N_Exception_Handler
5344 and then not Comes_From_Source (Parent (Stmts))
5345 then
5346 Loc := Sloc (Last_Stmt);
5347 elsif Present (End_Label (HSS)) then
5348 Loc := Sloc (End_Label (HSS));
5349 else
5350 Loc := Sloc (Last_Stmt);
5351 end if;
5353 -- Append return statement, and set analyzed manually. We can't
5354 -- call Analyze on this return since the scope is wrong.
5356 -- Note: it almost works to push the scope and then do the Analyze
5357 -- call, but something goes wrong in some weird cases and it is
5358 -- not worth worrying about ???
5360 Stmt := Make_Simple_Return_Statement (Loc);
5362 -- The return statement is handled properly, and the call to the
5363 -- postcondition, inserted below, does not require information
5364 -- from the body either. However, that call is analyzed in the
5365 -- enclosing scope, and an elaboration check might improperly be
5366 -- added to it. A guard in Sem_Elab is needed to prevent that
5367 -- spurious check, see Check_Elab_Call.
5369 Append_To (Stmts, Stmt);
5370 Set_Analyzed (Stmt);
5372 -- Call the _Postconditions procedure if the related subprogram
5373 -- has contract assertions that need to be verified on exit.
5375 if Ekind (Spec_Id) = E_Procedure
5376 and then Present (Postconditions_Proc (Spec_Id))
5377 then
5378 Insert_Action (Stmt,
5379 Make_Procedure_Call_Statement (Loc,
5380 Name =>
5381 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5382 end if;
5383 end if;
5384 end Add_Return;
5386 -- Local variables
5388 Except_H : Node_Id;
5389 L : List_Id;
5390 Spec_Id : Entity_Id;
5392 -- Start of processing for Expand_N_Subprogram_Body
5394 begin
5395 if Present (Corresponding_Spec (N)) then
5396 Spec_Id := Corresponding_Spec (N);
5397 else
5398 Spec_Id := Body_Id;
5399 end if;
5401 -- If this is a Pure function which has any parameters whose root type
5402 -- is System.Address, reset the Pure indication.
5403 -- This check is also performed when the subprogram is frozen, but we
5404 -- repeat it on the body so that the indication is consistent, and so
5405 -- it applies as well to bodies without separate specifications.
5407 if Is_Pure (Spec_Id)
5408 and then Is_Subprogram (Spec_Id)
5409 and then not Has_Pragma_Pure_Function (Spec_Id)
5410 then
5411 Check_Function_With_Address_Parameter (Spec_Id);
5413 if Spec_Id /= Body_Id then
5414 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5415 end if;
5416 end if;
5418 -- Set L to either the list of declarations if present, or to the list
5419 -- of statements if no declarations are present. This is used to insert
5420 -- new stuff at the start.
5422 if Is_Non_Empty_List (Declarations (N)) then
5423 L := Declarations (N);
5424 else
5425 L := Statements (HSS);
5426 end if;
5428 -- If local-exception-to-goto optimization active, insert dummy push
5429 -- statements at start, and dummy pop statements at end, but inhibit
5430 -- this if we have No_Exception_Handlers, since they are useless and
5431 -- intefere with analysis, e.g. by codepeer.
5433 if (Debug_Flag_Dot_G
5434 or else Restriction_Active (No_Exception_Propagation))
5435 and then not Restriction_Active (No_Exception_Handlers)
5436 and then not CodePeer_Mode
5437 and then Is_Non_Empty_List (L)
5438 then
5439 declare
5440 FS : constant Node_Id := First (L);
5441 FL : constant Source_Ptr := Sloc (FS);
5442 LS : Node_Id;
5443 LL : Source_Ptr;
5445 begin
5446 -- LS points to either last statement, if statements are present
5447 -- or to the last declaration if there are no statements present.
5448 -- It is the node after which the pop's are generated.
5450 if Is_Non_Empty_List (Statements (HSS)) then
5451 LS := Last (Statements (HSS));
5452 else
5453 LS := Last (L);
5454 end if;
5456 LL := Sloc (LS);
5458 Insert_List_Before_And_Analyze (FS, New_List (
5459 Make_Push_Constraint_Error_Label (FL),
5460 Make_Push_Program_Error_Label (FL),
5461 Make_Push_Storage_Error_Label (FL)));
5463 Insert_List_After_And_Analyze (LS, New_List (
5464 Make_Pop_Constraint_Error_Label (LL),
5465 Make_Pop_Program_Error_Label (LL),
5466 Make_Pop_Storage_Error_Label (LL)));
5467 end;
5468 end if;
5470 -- Need poll on entry to subprogram if polling enabled. We only do this
5471 -- for non-empty subprograms, since it does not seem necessary to poll
5472 -- for a dummy null subprogram.
5474 if Is_Non_Empty_List (L) then
5476 -- Do not add a polling call if the subprogram is to be inlined by
5477 -- the back-end, to avoid repeated calls with multiple inlinings.
5479 if Is_Inlined (Spec_Id)
5480 and then Front_End_Inlining
5481 and then Optimization_Level > 1
5482 then
5483 null;
5484 else
5485 Generate_Poll_Call (First (L));
5486 end if;
5487 end if;
5489 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5491 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5492 declare
5493 F : Entity_Id;
5494 A : Node_Id;
5496 begin
5497 -- Loop through formals
5499 F := First_Formal (Spec_Id);
5500 while Present (F) loop
5501 if Is_Scalar_Type (Etype (F))
5502 and then Ekind (F) = E_Out_Parameter
5503 then
5504 Check_Restriction (No_Default_Initialization, F);
5506 -- Insert the initialization. We turn off validity checks
5507 -- for this assignment, since we do not want any check on
5508 -- the initial value itself (which may well be invalid).
5509 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5511 A :=
5512 Make_Assignment_Statement (Loc,
5513 Name => New_Occurrence_Of (F, Loc),
5514 Expression => Get_Simple_Init_Val (Etype (F), N));
5515 Set_Suppress_Assignment_Checks (A);
5517 Insert_Before_And_Analyze (First (L),
5518 A, Suppress => Validity_Check);
5519 end if;
5521 Next_Formal (F);
5522 end loop;
5523 end;
5524 end if;
5526 -- Clear out statement list for stubbed procedure
5528 if Present (Corresponding_Spec (N)) then
5529 Set_Elaboration_Flag (N, Spec_Id);
5531 if Convention (Spec_Id) = Convention_Stubbed
5532 or else Is_Eliminated (Spec_Id)
5533 then
5534 Set_Declarations (N, Empty_List);
5535 Set_Handled_Statement_Sequence (N,
5536 Make_Handled_Sequence_Of_Statements (Loc,
5537 Statements => New_List (Make_Null_Statement (Loc))));
5539 return;
5540 end if;
5541 end if;
5543 -- Create a set of discriminals for the next protected subprogram body
5545 if Is_List_Member (N)
5546 and then Present (Parent (List_Containing (N)))
5547 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5548 and then Present (Next_Protected_Operation (N))
5549 then
5550 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5551 end if;
5553 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5554 -- subprograms with no specs are not frozen.
5556 declare
5557 Typ : constant Entity_Id := Etype (Spec_Id);
5558 Utyp : constant Entity_Id := Underlying_Type (Typ);
5560 begin
5561 if Is_Limited_View (Typ) then
5562 Set_Returns_By_Ref (Spec_Id);
5564 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5565 Set_Returns_By_Ref (Spec_Id);
5566 end if;
5567 end;
5569 -- For a procedure, we add a return for all possible syntactic ends of
5570 -- the subprogram.
5572 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5573 Add_Return (Spec_Id, Statements (HSS));
5575 if Present (Exception_Handlers (HSS)) then
5576 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5577 while Present (Except_H) loop
5578 Add_Return (Spec_Id, Statements (Except_H));
5579 Next_Non_Pragma (Except_H);
5580 end loop;
5581 end if;
5583 -- For a function, we must deal with the case where there is at least
5584 -- one missing return. What we do is to wrap the entire body of the
5585 -- function in a block:
5587 -- begin
5588 -- ...
5589 -- end;
5591 -- becomes
5593 -- begin
5594 -- begin
5595 -- ...
5596 -- end;
5598 -- raise Program_Error;
5599 -- end;
5601 -- This approach is necessary because the raise must be signalled to the
5602 -- caller, not handled by any local handler (RM 6.4(11)).
5604 -- Note: we do not need to analyze the constructed sequence here, since
5605 -- it has no handler, and an attempt to analyze the handled statement
5606 -- sequence twice is risky in various ways (e.g. the issue of expanding
5607 -- cleanup actions twice).
5609 elsif Has_Missing_Return (Spec_Id) then
5610 declare
5611 Hloc : constant Source_Ptr := Sloc (HSS);
5612 Blok : constant Node_Id :=
5613 Make_Block_Statement (Hloc,
5614 Handled_Statement_Sequence => HSS);
5615 Rais : constant Node_Id :=
5616 Make_Raise_Program_Error (Hloc,
5617 Reason => PE_Missing_Return);
5619 begin
5620 Set_Handled_Statement_Sequence (N,
5621 Make_Handled_Sequence_Of_Statements (Hloc,
5622 Statements => New_List (Blok, Rais)));
5624 Push_Scope (Spec_Id);
5625 Analyze (Blok);
5626 Analyze (Rais);
5627 Pop_Scope;
5628 end;
5629 end if;
5631 -- If subprogram contains a parameterless recursive call, then we may
5632 -- have an infinite recursion, so see if we can generate code to check
5633 -- for this possibility if storage checks are not suppressed.
5635 if Ekind (Spec_Id) = E_Procedure
5636 and then Has_Recursive_Call (Spec_Id)
5637 and then not Storage_Checks_Suppressed (Spec_Id)
5638 then
5639 Detect_Infinite_Recursion (N, Spec_Id);
5640 end if;
5642 -- Set to encode entity names in package body before gigi is called
5644 Qualify_Entity_Names (N);
5646 -- If the body belongs to a nonabstract library-level source primitive
5647 -- of a tagged type, install an elaboration check which ensures that a
5648 -- dispatching call targeting the primitive will not execute the body
5649 -- without it being previously elaborated.
5651 Install_Primitive_Elaboration_Check (N);
5652 end Expand_N_Subprogram_Body;
5654 -----------------------------------
5655 -- Expand_N_Subprogram_Body_Stub --
5656 -----------------------------------
5658 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5659 Bod : Node_Id;
5661 begin
5662 if Present (Corresponding_Body (N)) then
5663 Bod := Unit_Declaration_Node (Corresponding_Body (N));
5665 -- The body may have been expanded already when it is analyzed
5666 -- through the subunit node. Do no expand again: it interferes
5667 -- with the construction of unnesting tables when generating C.
5669 if not Analyzed (Bod) then
5670 Expand_N_Subprogram_Body (Bod);
5671 end if;
5673 -- Add full qualification to entities that may be created late
5674 -- during unnesting.
5676 Qualify_Entity_Names (N);
5677 end if;
5678 end Expand_N_Subprogram_Body_Stub;
5680 -------------------------------------
5681 -- Expand_N_Subprogram_Declaration --
5682 -------------------------------------
5684 -- If the declaration appears within a protected body, it is a private
5685 -- operation of the protected type. We must create the corresponding
5686 -- protected subprogram an associated formals. For a normal protected
5687 -- operation, this is done when expanding the protected type declaration.
5689 -- If the declaration is for a null procedure, emit null body
5691 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5692 Loc : constant Source_Ptr := Sloc (N);
5693 Subp : constant Entity_Id := Defining_Entity (N);
5695 -- Local variables
5697 Scop : constant Entity_Id := Scope (Subp);
5698 Prot_Bod : Node_Id;
5699 Prot_Decl : Node_Id;
5700 Prot_Id : Entity_Id;
5702 -- Start of processing for Expand_N_Subprogram_Declaration
5704 begin
5705 -- In SPARK, subprogram declarations are only allowed in package
5706 -- specifications.
5708 if Nkind (Parent (N)) /= N_Package_Specification then
5709 if Nkind (Parent (N)) = N_Compilation_Unit then
5710 Check_SPARK_05_Restriction
5711 ("subprogram declaration is not a library item", N);
5713 elsif Present (Next (N))
5714 and then Nkind (Next (N)) = N_Pragma
5715 and then Get_Pragma_Id (Next (N)) = Pragma_Import
5716 then
5717 -- In SPARK, subprogram declarations are also permitted in
5718 -- declarative parts when immediately followed by a corresponding
5719 -- pragma Import. We only check here that there is some pragma
5720 -- Import.
5722 null;
5723 else
5724 Check_SPARK_05_Restriction
5725 ("subprogram declaration is not allowed here", N);
5726 end if;
5727 end if;
5729 -- Deal with case of protected subprogram. Do not generate protected
5730 -- operation if operation is flagged as eliminated.
5732 if Is_List_Member (N)
5733 and then Present (Parent (List_Containing (N)))
5734 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5735 and then Is_Protected_Type (Scop)
5736 then
5737 if No (Protected_Body_Subprogram (Subp))
5738 and then not Is_Eliminated (Subp)
5739 then
5740 Prot_Decl :=
5741 Make_Subprogram_Declaration (Loc,
5742 Specification =>
5743 Build_Protected_Sub_Specification
5744 (N, Scop, Unprotected_Mode));
5746 -- The protected subprogram is declared outside of the protected
5747 -- body. Given that the body has frozen all entities so far, we
5748 -- analyze the subprogram and perform freezing actions explicitly.
5749 -- including the generation of an explicit freeze node, to ensure
5750 -- that gigi has the proper order of elaboration.
5751 -- If the body is a subunit, the insertion point is before the
5752 -- stub in the parent.
5754 Prot_Bod := Parent (List_Containing (N));
5756 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5757 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5758 end if;
5760 Insert_Before (Prot_Bod, Prot_Decl);
5761 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5762 Set_Has_Delayed_Freeze (Prot_Id);
5764 Push_Scope (Scope (Scop));
5765 Analyze (Prot_Decl);
5766 Freeze_Before (N, Prot_Id);
5767 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5769 -- Create protected operation as well. Even though the operation
5770 -- is only accessible within the body, it is possible to make it
5771 -- available outside of the protected object by using 'Access to
5772 -- provide a callback, so build protected version in all cases.
5774 Prot_Decl :=
5775 Make_Subprogram_Declaration (Loc,
5776 Specification =>
5777 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5778 Insert_Before (Prot_Bod, Prot_Decl);
5779 Analyze (Prot_Decl);
5781 Pop_Scope;
5782 end if;
5784 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5785 -- cases this is superfluous because calls to it will be automatically
5786 -- inlined, but we definitely need the body if preconditions for the
5787 -- procedure are present, or if performing coverage analysis.
5789 elsif Nkind (Specification (N)) = N_Procedure_Specification
5790 and then Null_Present (Specification (N))
5791 then
5792 declare
5793 Bod : constant Node_Id := Body_To_Inline (N);
5795 begin
5796 Set_Has_Completion (Subp, False);
5797 Append_Freeze_Action (Subp, Bod);
5799 -- The body now contains raise statements, so calls to it will
5800 -- not be inlined.
5802 Set_Is_Inlined (Subp, False);
5803 end;
5804 end if;
5806 -- When generating C code, transform a function that returns a
5807 -- constrained array type into a procedure with an out parameter
5808 -- that carries the return value.
5810 -- We skip this transformation for unchecked conversions, since they
5811 -- are not needed by the C generator (and this also produces cleaner
5812 -- output).
5814 if Modify_Tree_For_C
5815 and then Nkind (Specification (N)) = N_Function_Specification
5816 and then Is_Array_Type (Etype (Subp))
5817 and then Is_Constrained (Etype (Subp))
5818 and then not Is_Unchecked_Conversion_Instance (Subp)
5819 then
5820 Build_Procedure_Form (N);
5821 end if;
5822 end Expand_N_Subprogram_Declaration;
5824 --------------------------------
5825 -- Expand_Non_Function_Return --
5826 --------------------------------
5828 procedure Expand_Non_Function_Return (N : Node_Id) is
5829 pragma Assert (No (Expression (N)));
5831 Loc : constant Source_Ptr := Sloc (N);
5832 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
5833 Kind : constant Entity_Kind := Ekind (Scope_Id);
5834 Call : Node_Id;
5835 Acc_Stat : Node_Id;
5836 Goto_Stat : Node_Id;
5837 Lab_Node : Node_Id;
5839 begin
5840 -- Call the _Postconditions procedure if the related subprogram has
5841 -- contract assertions that need to be verified on exit.
5843 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
5844 and then Present (Postconditions_Proc (Scope_Id))
5845 then
5846 Insert_Action (N,
5847 Make_Procedure_Call_Statement (Loc,
5848 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
5849 end if;
5851 -- If it is a return from a procedure do no extra steps
5853 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5854 return;
5856 -- If it is a nested return within an extended one, replace it with a
5857 -- return of the previously declared return object.
5859 elsif Kind = E_Return_Statement then
5860 Rewrite (N,
5861 Make_Simple_Return_Statement (Loc,
5862 Expression =>
5863 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5864 Set_Comes_From_Extended_Return_Statement (N);
5865 Set_Return_Statement_Entity (N, Scope_Id);
5866 Expand_Simple_Function_Return (N);
5867 return;
5868 end if;
5870 pragma Assert (Is_Entry (Scope_Id));
5872 -- Look at the enclosing block to see whether the return is from an
5873 -- accept statement or an entry body.
5875 for J in reverse 0 .. Scope_Stack.Last loop
5876 Scope_Id := Scope_Stack.Table (J).Entity;
5877 exit when Is_Concurrent_Type (Scope_Id);
5878 end loop;
5880 -- If it is a return from accept statement it is expanded as call to
5881 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5883 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5884 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5886 if Is_Task_Type (Scope_Id) then
5888 Call :=
5889 Make_Procedure_Call_Statement (Loc,
5890 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
5891 Insert_Before (N, Call);
5892 -- why not insert actions here???
5893 Analyze (Call);
5895 Acc_Stat := Parent (N);
5896 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5897 Acc_Stat := Parent (Acc_Stat);
5898 end loop;
5900 Lab_Node := Last (Statements
5901 (Handled_Statement_Sequence (Acc_Stat)));
5903 Goto_Stat := Make_Goto_Statement (Loc,
5904 Name => New_Occurrence_Of
5905 (Entity (Identifier (Lab_Node)), Loc));
5907 Set_Analyzed (Goto_Stat);
5909 Rewrite (N, Goto_Stat);
5910 Analyze (N);
5912 -- If it is a return from an entry body, put a Complete_Entry_Body call
5913 -- in front of the return.
5915 elsif Is_Protected_Type (Scope_Id) then
5916 Call :=
5917 Make_Procedure_Call_Statement (Loc,
5918 Name =>
5919 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
5920 Parameter_Associations => New_List (
5921 Make_Attribute_Reference (Loc,
5922 Prefix =>
5923 New_Occurrence_Of
5924 (Find_Protection_Object (Current_Scope), Loc),
5925 Attribute_Name => Name_Unchecked_Access)));
5927 Insert_Before (N, Call);
5928 Analyze (Call);
5929 end if;
5930 end Expand_Non_Function_Return;
5932 ---------------------------------------
5933 -- Expand_Protected_Object_Reference --
5934 ---------------------------------------
5936 function Expand_Protected_Object_Reference
5937 (N : Node_Id;
5938 Scop : Entity_Id) return Node_Id
5940 Loc : constant Source_Ptr := Sloc (N);
5941 Corr : Entity_Id;
5942 Rec : Node_Id;
5943 Param : Entity_Id;
5944 Proc : Entity_Id;
5946 begin
5947 Rec := Make_Identifier (Loc, Name_uObject);
5948 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5950 -- Find enclosing protected operation, and retrieve its first parameter,
5951 -- which denotes the enclosing protected object. If the enclosing
5952 -- operation is an entry, we are immediately within the protected body,
5953 -- and we can retrieve the object from the service entries procedure. A
5954 -- barrier function has the same signature as an entry. A barrier
5955 -- function is compiled within the protected object, but unlike
5956 -- protected operations its never needs locks, so that its protected
5957 -- body subprogram points to itself.
5959 Proc := Current_Scope;
5960 while Present (Proc)
5961 and then Scope (Proc) /= Scop
5962 loop
5963 Proc := Scope (Proc);
5964 end loop;
5966 Corr := Protected_Body_Subprogram (Proc);
5968 if No (Corr) then
5970 -- Previous error left expansion incomplete.
5971 -- Nothing to do on this call.
5973 return Empty;
5974 end if;
5976 Param :=
5977 Defining_Identifier
5978 (First (Parameter_Specifications (Parent (Corr))));
5980 if Is_Subprogram (Proc) and then Proc /= Corr then
5982 -- Protected function or procedure
5984 Set_Entity (Rec, Param);
5986 -- Rec is a reference to an entity which will not be in scope when
5987 -- the call is reanalyzed, and needs no further analysis.
5989 Set_Analyzed (Rec);
5991 else
5992 -- Entry or barrier function for entry body. The first parameter of
5993 -- the entry body procedure is pointer to the object. We create a
5994 -- local variable of the proper type, duplicating what is done to
5995 -- define _object later on.
5997 declare
5998 Decls : List_Id;
5999 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6001 begin
6002 Decls := New_List (
6003 Make_Full_Type_Declaration (Loc,
6004 Defining_Identifier => Obj_Ptr,
6005 Type_Definition =>
6006 Make_Access_To_Object_Definition (Loc,
6007 Subtype_Indication =>
6008 New_Occurrence_Of
6009 (Corresponding_Record_Type (Scop), Loc))));
6011 Insert_Actions (N, Decls);
6012 Freeze_Before (N, Obj_Ptr);
6014 Rec :=
6015 Make_Explicit_Dereference (Loc,
6016 Prefix =>
6017 Unchecked_Convert_To (Obj_Ptr,
6018 New_Occurrence_Of (Param, Loc)));
6020 -- Analyze new actual. Other actuals in calls are already analyzed
6021 -- and the list of actuals is not reanalyzed after rewriting.
6023 Set_Parent (Rec, N);
6024 Analyze (Rec);
6025 end;
6026 end if;
6028 return Rec;
6029 end Expand_Protected_Object_Reference;
6031 --------------------------------------
6032 -- Expand_Protected_Subprogram_Call --
6033 --------------------------------------
6035 procedure Expand_Protected_Subprogram_Call
6036 (N : Node_Id;
6037 Subp : Entity_Id;
6038 Scop : Entity_Id)
6040 Rec : Node_Id;
6042 procedure Expand_Internal_Init_Call;
6043 -- A call to an operation of the type may occur in the initialization
6044 -- of a private component. In that case the prefix of the call is an
6045 -- entity name and the call is treated as internal even though it
6046 -- appears in code outside of the protected type.
6048 procedure Freeze_Called_Function;
6049 -- If it is a function call it can appear in elaboration code and
6050 -- the called entity must be frozen before the call. This must be
6051 -- done before the call is expanded, as the expansion may rewrite it
6052 -- to something other than a call (e.g. a temporary initialized in a
6053 -- transient block).
6055 -------------------------------
6056 -- Expand_Internal_Init_Call --
6057 -------------------------------
6059 procedure Expand_Internal_Init_Call is
6060 begin
6061 -- If the context is a protected object (rather than a protected
6062 -- type) the call itself is bound to raise program_error because
6063 -- the protected body will not have been elaborated yet. This is
6064 -- diagnosed subsequently in Sem_Elab.
6066 Freeze_Called_Function;
6068 -- The target of the internal call is the first formal of the
6069 -- enclosing initialization procedure.
6071 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6072 Build_Protected_Subprogram_Call (N,
6073 Name => Name (N),
6074 Rec => Rec,
6075 External => False);
6076 Analyze (N);
6077 Resolve (N, Etype (Subp));
6078 end Expand_Internal_Init_Call;
6080 ----------------------------
6081 -- Freeze_Called_Function --
6082 ----------------------------
6084 procedure Freeze_Called_Function is
6085 begin
6086 if Ekind (Subp) = E_Function then
6087 Freeze_Expression (Name (N));
6088 end if;
6089 end Freeze_Called_Function;
6091 -- Start of processing for Expand_Protected_Subprogram_Call
6093 begin
6094 -- If the protected object is not an enclosing scope, this is an inter-
6095 -- object function call. Inter-object procedure calls are expanded by
6096 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6097 -- subprogram being called is in the protected body being compiled, and
6098 -- if the protected object in the call is statically the enclosing type.
6099 -- The object may be a component of some other data structure, in which
6100 -- case this must be handled as an inter-object call.
6102 if not In_Open_Scopes (Scop)
6103 or else Is_Entry_Wrapper (Current_Scope)
6104 or else not Is_Entity_Name (Name (N))
6105 then
6106 if Nkind (Name (N)) = N_Selected_Component then
6107 Rec := Prefix (Name (N));
6109 elsif Nkind (Name (N)) = N_Indexed_Component then
6110 Rec := Prefix (Prefix (Name (N)));
6112 -- If this is a call within an entry wrapper, it appears within a
6113 -- precondition that calls another primitive of the synchronized
6114 -- type. The target object of the call is the first actual on the
6115 -- wrapper. Note that this is an external call, because the wrapper
6116 -- is called outside of the synchronized object. This means that
6117 -- an entry call to an entry with preconditions involves two
6118 -- synchronized operations.
6120 elsif Ekind (Current_Scope) = E_Procedure
6121 and then Is_Entry_Wrapper (Current_Scope)
6122 then
6123 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6125 else
6126 -- If the context is the initialization procedure for a protected
6127 -- type, the call is legal because the called entity must be a
6128 -- function of that enclosing type, and this is treated as an
6129 -- internal call.
6131 pragma Assert
6132 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6134 Expand_Internal_Init_Call;
6135 return;
6136 end if;
6138 Freeze_Called_Function;
6139 Build_Protected_Subprogram_Call (N,
6140 Name => New_Occurrence_Of (Subp, Sloc (N)),
6141 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6142 External => True);
6144 else
6145 Rec := Expand_Protected_Object_Reference (N, Scop);
6147 if No (Rec) then
6148 return;
6149 end if;
6151 Freeze_Called_Function;
6152 Build_Protected_Subprogram_Call (N,
6153 Name => Name (N),
6154 Rec => Rec,
6155 External => False);
6156 end if;
6158 -- Analyze and resolve the new call. The actuals have already been
6159 -- resolved, but expansion of a function call will add extra actuals
6160 -- if needed. Analysis of a procedure call already includes resolution.
6162 Analyze (N);
6164 if Ekind (Subp) = E_Function then
6165 Resolve (N, Etype (Subp));
6166 end if;
6167 end Expand_Protected_Subprogram_Call;
6169 -----------------------------------
6170 -- Expand_Simple_Function_Return --
6171 -----------------------------------
6173 -- The "simple" comes from the syntax rule simple_return_statement. The
6174 -- semantics are not at all simple.
6176 procedure Expand_Simple_Function_Return (N : Node_Id) is
6177 Loc : constant Source_Ptr := Sloc (N);
6179 Scope_Id : constant Entity_Id :=
6180 Return_Applies_To (Return_Statement_Entity (N));
6181 -- The function we are returning from
6183 R_Type : constant Entity_Id := Etype (Scope_Id);
6184 -- The result type of the function
6186 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6188 Exp : Node_Id := Expression (N);
6189 pragma Assert (Present (Exp));
6191 Exptyp : constant Entity_Id := Etype (Exp);
6192 -- The type of the expression (not necessarily the same as R_Type)
6194 Subtype_Ind : Node_Id;
6195 -- If the result type of the function is class-wide and the expression
6196 -- has a specific type, then we use the expression's type as the type of
6197 -- the return object. In cases where the expression is an aggregate that
6198 -- is built in place, this avoids the need for an expensive conversion
6199 -- of the return object to the specific type on assignments to the
6200 -- individual components.
6202 begin
6203 if Is_Class_Wide_Type (R_Type)
6204 and then not Is_Class_Wide_Type (Exptyp)
6205 and then Nkind (Exp) /= N_Type_Conversion
6206 then
6207 Subtype_Ind := New_Occurrence_Of (Exptyp, Loc);
6208 else
6209 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6211 -- If the result type is class-wide and the expression is a view
6212 -- conversion, the conversion plays no role in the expansion because
6213 -- it does not modify the tag of the object. Remove the conversion
6214 -- altogether to prevent tag overwriting.
6216 if Is_Class_Wide_Type (R_Type)
6217 and then not Is_Class_Wide_Type (Exptyp)
6218 and then Nkind (Exp) = N_Type_Conversion
6219 then
6220 Exp := Expression (Exp);
6221 end if;
6222 end if;
6224 -- For the case of a simple return that does not come from an extended
6225 -- return, in the case of Ada 2005 where we are returning a limited
6226 -- type, we rewrite "return <expression>;" to be:
6228 -- return _anon_ : <return_subtype> := <expression>
6230 -- The expansion produced by Expand_N_Extended_Return_Statement will
6231 -- contain simple return statements (for example, a block containing
6232 -- simple return of the return object), which brings us back here with
6233 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6234 -- checking for a simple return that does not come from an extended
6235 -- return is to avoid this infinite recursion.
6237 -- The reason for this design is that for Ada 2005 limited returns, we
6238 -- need to reify the return object, so we can build it "in place", and
6239 -- we need a block statement to hang finalization and tasking stuff.
6241 -- ??? In order to avoid disruption, we avoid translating to extended
6242 -- return except in the cases where we really need to (Ada 2005 for
6243 -- inherently limited). We might prefer to do this translation in all
6244 -- cases (except perhaps for the case of Ada 95 inherently limited),
6245 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6246 -- code. This would also allow us to do the build-in-place optimization
6247 -- for efficiency even in cases where it is semantically not required.
6249 -- As before, we check the type of the return expression rather than the
6250 -- return type of the function, because the latter may be a limited
6251 -- class-wide interface type, which is not a limited type, even though
6252 -- the type of the expression may be.
6254 if not Comes_From_Extended_Return_Statement (N)
6255 and then Is_Limited_View (Etype (Expression (N)))
6256 and then Ada_Version >= Ada_2005
6257 and then not Debug_Flag_Dot_L
6259 -- The functionality of interface thunks is simple and it is always
6260 -- handled by means of simple return statements. This leaves their
6261 -- expansion simple and clean.
6263 and then not Is_Thunk (Current_Scope)
6264 then
6265 declare
6266 Return_Object_Entity : constant Entity_Id :=
6267 Make_Temporary (Loc, 'R', Exp);
6269 Obj_Decl : constant Node_Id :=
6270 Make_Object_Declaration (Loc,
6271 Defining_Identifier => Return_Object_Entity,
6272 Object_Definition => Subtype_Ind,
6273 Expression => Exp);
6275 Ext : constant Node_Id :=
6276 Make_Extended_Return_Statement (Loc,
6277 Return_Object_Declarations => New_List (Obj_Decl));
6278 -- Do not perform this high-level optimization if the result type
6279 -- is an interface because the "this" pointer must be displaced.
6281 begin
6282 Rewrite (N, Ext);
6283 Analyze (N);
6284 return;
6285 end;
6286 end if;
6288 -- Here we have a simple return statement that is part of the expansion
6289 -- of an extended return statement (either written by the user, or
6290 -- generated by the above code).
6292 -- Always normalize C/Fortran boolean result. This is not always needed,
6293 -- but it seems a good idea to minimize the passing around of non-
6294 -- normalized values, and in any case this handles the processing of
6295 -- barrier functions for protected types, which turn the condition into
6296 -- a return statement.
6298 if Is_Boolean_Type (Exptyp)
6299 and then Nonzero_Is_True (Exptyp)
6300 then
6301 Adjust_Condition (Exp);
6302 Adjust_Result_Type (Exp, Exptyp);
6303 end if;
6305 -- Do validity check if enabled for returns
6307 if Validity_Checks_On
6308 and then Validity_Check_Returns
6309 then
6310 Ensure_Valid (Exp);
6311 end if;
6313 -- Check the result expression of a scalar function against the subtype
6314 -- of the function by inserting a conversion. This conversion must
6315 -- eventually be performed for other classes of types, but for now it's
6316 -- only done for scalars.
6317 -- ???
6319 if Is_Scalar_Type (Exptyp) then
6320 Rewrite (Exp, Convert_To (R_Type, Exp));
6322 -- The expression is resolved to ensure that the conversion gets
6323 -- expanded to generate a possible constraint check.
6325 Analyze_And_Resolve (Exp, R_Type);
6326 end if;
6328 -- Deal with returning variable length objects and controlled types
6330 -- Nothing to do if we are returning by reference, or this is not a
6331 -- type that requires special processing (indicated by the fact that
6332 -- it requires a cleanup scope for the secondary stack case).
6334 if Is_Limited_View (Exptyp)
6335 or else Is_Limited_Interface (Exptyp)
6336 then
6337 null;
6339 -- No copy needed for thunks returning interface type objects since
6340 -- the object is returned by reference and the maximum functionality
6341 -- required is just to displace the pointer.
6343 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
6344 null;
6346 -- If the call is within a thunk and the type is a limited view, the
6347 -- backend will eventually see the non-limited view of the type.
6349 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
6350 return;
6352 elsif not Requires_Transient_Scope (R_Type) then
6354 -- Mutable records with variable-length components are not returned
6355 -- on the sec-stack, so we need to make sure that the back end will
6356 -- only copy back the size of the actual value, and not the maximum
6357 -- size. We create an actual subtype for this purpose. However we
6358 -- need not do it if the expression is a function call since this
6359 -- will be done in the called function and doing it here too would
6360 -- cause a temporary with maximum size to be created.
6362 declare
6363 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6364 Decl : Node_Id;
6365 Ent : Entity_Id;
6366 begin
6367 if Nkind (Exp) /= N_Function_Call
6368 and then Has_Discriminants (Ubt)
6369 and then not Is_Constrained (Ubt)
6370 and then not Has_Unchecked_Union (Ubt)
6371 then
6372 Decl := Build_Actual_Subtype (Ubt, Exp);
6373 Ent := Defining_Identifier (Decl);
6374 Insert_Action (Exp, Decl);
6375 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6376 Analyze_And_Resolve (Exp);
6377 end if;
6378 end;
6380 -- Here if secondary stack is used
6382 else
6383 -- Prevent the reclamation of the secondary stack by all enclosing
6384 -- blocks and loops as well as the related function; otherwise the
6385 -- result would be reclaimed too early.
6387 Set_Enclosing_Sec_Stack_Return (N);
6389 -- Optimize the case where the result is a function call. In this
6390 -- case either the result is already on the secondary stack, or is
6391 -- already being returned with the stack pointer depressed and no
6392 -- further processing is required except to set the By_Ref flag
6393 -- to ensure that gigi does not attempt an extra unnecessary copy.
6394 -- (actually not just unnecessary but harmfully wrong in the case
6395 -- of a controlled type, where gigi does not know how to do a copy).
6396 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6397 -- for array types if the constrained status of the target type is
6398 -- different from that of the expression.
6400 if Requires_Transient_Scope (Exptyp)
6401 and then
6402 (not Is_Array_Type (Exptyp)
6403 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6404 or else CW_Or_Has_Controlled_Part (Utyp))
6405 and then Nkind (Exp) = N_Function_Call
6406 then
6407 Set_By_Ref (N);
6409 -- Remove side effects from the expression now so that other parts
6410 -- of the expander do not have to reanalyze this node without this
6411 -- optimization
6413 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6415 -- For controlled types, do the allocation on the secondary stack
6416 -- manually in order to call adjust at the right time:
6418 -- type Anon1 is access R_Type;
6419 -- for Anon1'Storage_pool use ss_pool;
6420 -- Anon2 : anon1 := new R_Type'(expr);
6421 -- return Anon2.all;
6423 -- We do the same for classwide types that are not potentially
6424 -- controlled (by the virtue of restriction No_Finalization) because
6425 -- gigi is not able to properly allocate class-wide types.
6427 elsif CW_Or_Has_Controlled_Part (Utyp) then
6428 declare
6429 Loc : constant Source_Ptr := Sloc (N);
6430 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6431 Alloc_Node : Node_Id;
6432 Temp : Entity_Id;
6434 begin
6435 Set_Ekind (Acc_Typ, E_Access_Type);
6437 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6439 -- This is an allocator for the secondary stack, and it's fine
6440 -- to have Comes_From_Source set False on it, as gigi knows not
6441 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6443 Alloc_Node :=
6444 Make_Allocator (Loc,
6445 Expression =>
6446 Make_Qualified_Expression (Loc,
6447 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6448 Expression => Relocate_Node (Exp)));
6450 -- We do not want discriminant checks on the declaration,
6451 -- given that it gets its value from the allocator.
6453 Set_No_Initialization (Alloc_Node);
6455 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6457 Insert_List_Before_And_Analyze (N, New_List (
6458 Make_Full_Type_Declaration (Loc,
6459 Defining_Identifier => Acc_Typ,
6460 Type_Definition =>
6461 Make_Access_To_Object_Definition (Loc,
6462 Subtype_Indication => Subtype_Ind)),
6464 Make_Object_Declaration (Loc,
6465 Defining_Identifier => Temp,
6466 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6467 Expression => Alloc_Node)));
6469 Rewrite (Exp,
6470 Make_Explicit_Dereference (Loc,
6471 Prefix => New_Occurrence_Of (Temp, Loc)));
6473 -- Ada 2005 (AI-251): If the type of the returned object is
6474 -- an interface then add an implicit type conversion to force
6475 -- displacement of the "this" pointer.
6477 if Is_Interface (R_Type) then
6478 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6479 end if;
6481 Analyze_And_Resolve (Exp, R_Type);
6482 end;
6484 -- Otherwise use the gigi mechanism to allocate result on the
6485 -- secondary stack.
6487 else
6488 Check_Restriction (No_Secondary_Stack, N);
6489 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6490 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6491 end if;
6492 end if;
6494 -- Implement the rules of 6.5(8-10), which require a tag check in
6495 -- the case of a limited tagged return type, and tag reassignment for
6496 -- nonlimited tagged results. These actions are needed when the return
6497 -- type is a specific tagged type and the result expression is a
6498 -- conversion or a formal parameter, because in that case the tag of
6499 -- the expression might differ from the tag of the specific result type.
6501 if Is_Tagged_Type (Utyp)
6502 and then not Is_Class_Wide_Type (Utyp)
6503 and then (Nkind_In (Exp, N_Type_Conversion,
6504 N_Unchecked_Type_Conversion)
6505 or else (Is_Entity_Name (Exp)
6506 and then Ekind (Entity (Exp)) in Formal_Kind))
6507 then
6508 -- When the return type is limited, perform a check that the tag of
6509 -- the result is the same as the tag of the return type.
6511 if Is_Limited_Type (R_Type) then
6512 Insert_Action (Exp,
6513 Make_Raise_Constraint_Error (Loc,
6514 Condition =>
6515 Make_Op_Ne (Loc,
6516 Left_Opnd =>
6517 Make_Selected_Component (Loc,
6518 Prefix => Duplicate_Subexpr (Exp),
6519 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6520 Right_Opnd =>
6521 Make_Attribute_Reference (Loc,
6522 Prefix =>
6523 New_Occurrence_Of (Base_Type (Utyp), Loc),
6524 Attribute_Name => Name_Tag)),
6525 Reason => CE_Tag_Check_Failed));
6527 -- If the result type is a specific nonlimited tagged type, then we
6528 -- have to ensure that the tag of the result is that of the result
6529 -- type. This is handled by making a copy of the expression in
6530 -- the case where it might have a different tag, namely when the
6531 -- expression is a conversion or a formal parameter. We create a new
6532 -- object of the result type and initialize it from the expression,
6533 -- which will implicitly force the tag to be set appropriately.
6535 else
6536 declare
6537 ExpR : constant Node_Id := Relocate_Node (Exp);
6538 Result_Id : constant Entity_Id :=
6539 Make_Temporary (Loc, 'R', ExpR);
6540 Result_Exp : constant Node_Id :=
6541 New_Occurrence_Of (Result_Id, Loc);
6542 Result_Obj : constant Node_Id :=
6543 Make_Object_Declaration (Loc,
6544 Defining_Identifier => Result_Id,
6545 Object_Definition =>
6546 New_Occurrence_Of (R_Type, Loc),
6547 Constant_Present => True,
6548 Expression => ExpR);
6550 begin
6551 Set_Assignment_OK (Result_Obj);
6552 Insert_Action (Exp, Result_Obj);
6554 Rewrite (Exp, Result_Exp);
6555 Analyze_And_Resolve (Exp, R_Type);
6556 end;
6557 end if;
6559 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6560 -- a check that the level of the return expression's underlying type
6561 -- is not deeper than the level of the master enclosing the function.
6562 -- Always generate the check when the type of the return expression
6563 -- is class-wide, when it's a type conversion, or when it's a formal
6564 -- parameter. Otherwise, suppress the check in the case where the
6565 -- return expression has a specific type whose level is known not to
6566 -- be statically deeper than the function's result type.
6568 -- No runtime check needed in interface thunks since it is performed
6569 -- by the target primitive associated with the thunk.
6571 -- Note: accessibility check is skipped in the VM case, since there
6572 -- does not seem to be any practical way to implement this check.
6574 elsif Ada_Version >= Ada_2005
6575 and then Tagged_Type_Expansion
6576 and then Is_Class_Wide_Type (R_Type)
6577 and then not Is_Thunk (Current_Scope)
6578 and then not Scope_Suppress.Suppress (Accessibility_Check)
6579 and then
6580 (Is_Class_Wide_Type (Etype (Exp))
6581 or else Nkind_In (Exp, N_Type_Conversion,
6582 N_Unchecked_Type_Conversion)
6583 or else (Is_Entity_Name (Exp)
6584 and then Ekind (Entity (Exp)) in Formal_Kind)
6585 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6586 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6587 then
6588 declare
6589 Tag_Node : Node_Id;
6591 begin
6592 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6593 -- "this" to reference the base of the object. This is required to
6594 -- get access to the TSD of the object.
6596 if Is_Class_Wide_Type (Etype (Exp))
6597 and then Is_Interface (Etype (Exp))
6598 then
6599 -- If the expression is an explicit dereference then we can
6600 -- directly displace the pointer to reference the base of
6601 -- the object.
6603 if Nkind (Exp) = N_Explicit_Dereference then
6604 Tag_Node :=
6605 Make_Explicit_Dereference (Loc,
6606 Prefix =>
6607 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6608 Make_Function_Call (Loc,
6609 Name =>
6610 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6611 Parameter_Associations => New_List (
6612 Unchecked_Convert_To (RTE (RE_Address),
6613 Duplicate_Subexpr (Prefix (Exp)))))));
6615 -- Similar case to the previous one but the expression is a
6616 -- renaming of an explicit dereference.
6618 elsif Nkind (Exp) = N_Identifier
6619 and then Present (Renamed_Object (Entity (Exp)))
6620 and then Nkind (Renamed_Object (Entity (Exp)))
6621 = N_Explicit_Dereference
6622 then
6623 Tag_Node :=
6624 Make_Explicit_Dereference (Loc,
6625 Prefix =>
6626 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6627 Make_Function_Call (Loc,
6628 Name =>
6629 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6630 Parameter_Associations => New_List (
6631 Unchecked_Convert_To (RTE (RE_Address),
6632 Duplicate_Subexpr
6633 (Prefix
6634 (Renamed_Object (Entity (Exp)))))))));
6636 -- Common case: obtain the address of the actual object and
6637 -- displace the pointer to reference the base of the object.
6639 else
6640 Tag_Node :=
6641 Make_Explicit_Dereference (Loc,
6642 Prefix =>
6643 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6644 Make_Function_Call (Loc,
6645 Name =>
6646 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6647 Parameter_Associations => New_List (
6648 Make_Attribute_Reference (Loc,
6649 Prefix => Duplicate_Subexpr (Exp),
6650 Attribute_Name => Name_Address)))));
6651 end if;
6652 else
6653 Tag_Node :=
6654 Make_Attribute_Reference (Loc,
6655 Prefix => Duplicate_Subexpr (Exp),
6656 Attribute_Name => Name_Tag);
6657 end if;
6659 -- CodePeer does not do anything useful with
6660 -- Ada.Tags.Type_Specific_Data components.
6662 if not CodePeer_Mode then
6663 Insert_Action (Exp,
6664 Make_Raise_Program_Error (Loc,
6665 Condition =>
6666 Make_Op_Gt (Loc,
6667 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6668 Right_Opnd =>
6669 Make_Integer_Literal (Loc,
6670 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6671 Reason => PE_Accessibility_Check_Failed));
6672 end if;
6673 end;
6675 -- AI05-0073: If function has a controlling access result, check that
6676 -- the tag of the return value, if it is not null, matches designated
6677 -- type of return type.
6679 -- The return expression is referenced twice in the code below, so it
6680 -- must be made free of side effects. Given that different compilers
6681 -- may evaluate these parameters in different order, both occurrences
6682 -- perform a copy.
6684 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6685 and then Has_Controlling_Result (Scope_Id)
6686 then
6687 Insert_Action (N,
6688 Make_Raise_Constraint_Error (Loc,
6689 Condition =>
6690 Make_And_Then (Loc,
6691 Left_Opnd =>
6692 Make_Op_Ne (Loc,
6693 Left_Opnd => Duplicate_Subexpr (Exp),
6694 Right_Opnd => Make_Null (Loc)),
6696 Right_Opnd => Make_Op_Ne (Loc,
6697 Left_Opnd =>
6698 Make_Selected_Component (Loc,
6699 Prefix => Duplicate_Subexpr (Exp),
6700 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6702 Right_Opnd =>
6703 Make_Attribute_Reference (Loc,
6704 Prefix =>
6705 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6706 Attribute_Name => Name_Tag))),
6708 Reason => CE_Tag_Check_Failed),
6709 Suppress => All_Checks);
6710 end if;
6712 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6713 -- ensure that the function result does not outlive an
6714 -- object designated by one of it discriminants.
6716 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6717 and then Has_Unconstrained_Access_Discriminants (R_Type)
6718 then
6719 declare
6720 Discrim_Source : Node_Id;
6722 procedure Check_Against_Result_Level (Level : Node_Id);
6723 -- Check the given accessibility level against the level
6724 -- determined by the point of call. (AI05-0234).
6726 --------------------------------
6727 -- Check_Against_Result_Level --
6728 --------------------------------
6730 procedure Check_Against_Result_Level (Level : Node_Id) is
6731 begin
6732 Insert_Action (N,
6733 Make_Raise_Program_Error (Loc,
6734 Condition =>
6735 Make_Op_Gt (Loc,
6736 Left_Opnd => Level,
6737 Right_Opnd =>
6738 New_Occurrence_Of
6739 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6740 Reason => PE_Accessibility_Check_Failed));
6741 end Check_Against_Result_Level;
6743 begin
6744 Discrim_Source := Exp;
6745 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6746 Discrim_Source := Expression (Discrim_Source);
6747 end loop;
6749 if Nkind (Discrim_Source) = N_Identifier
6750 and then Is_Return_Object (Entity (Discrim_Source))
6751 then
6752 Discrim_Source := Entity (Discrim_Source);
6754 if Is_Constrained (Etype (Discrim_Source)) then
6755 Discrim_Source := Etype (Discrim_Source);
6756 else
6757 Discrim_Source := Expression (Parent (Discrim_Source));
6758 end if;
6760 elsif Nkind (Discrim_Source) = N_Identifier
6761 and then Nkind_In (Original_Node (Discrim_Source),
6762 N_Aggregate, N_Extension_Aggregate)
6763 then
6764 Discrim_Source := Original_Node (Discrim_Source);
6766 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6767 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6768 then
6769 Discrim_Source := Original_Node (Discrim_Source);
6770 end if;
6772 while Nkind_In (Discrim_Source, N_Qualified_Expression,
6773 N_Type_Conversion,
6774 N_Unchecked_Type_Conversion)
6775 loop
6776 Discrim_Source := Expression (Discrim_Source);
6777 end loop;
6779 case Nkind (Discrim_Source) is
6780 when N_Defining_Identifier =>
6781 pragma Assert (Is_Composite_Type (Discrim_Source)
6782 and then Has_Discriminants (Discrim_Source)
6783 and then Is_Constrained (Discrim_Source));
6785 declare
6786 Discrim : Entity_Id :=
6787 First_Discriminant (Base_Type (R_Type));
6788 Disc_Elmt : Elmt_Id :=
6789 First_Elmt (Discriminant_Constraint
6790 (Discrim_Source));
6791 begin
6792 loop
6793 if Ekind (Etype (Discrim)) =
6794 E_Anonymous_Access_Type
6795 then
6796 Check_Against_Result_Level
6797 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
6798 end if;
6800 Next_Elmt (Disc_Elmt);
6801 Next_Discriminant (Discrim);
6802 exit when not Present (Discrim);
6803 end loop;
6804 end;
6806 when N_Aggregate
6807 | N_Extension_Aggregate
6809 -- Unimplemented: extension aggregate case where discrims
6810 -- come from ancestor part, not extension part.
6812 declare
6813 Discrim : Entity_Id :=
6814 First_Discriminant (Base_Type (R_Type));
6816 Disc_Exp : Node_Id := Empty;
6818 Positionals_Exhausted
6819 : Boolean := not Present (Expressions
6820 (Discrim_Source));
6822 function Associated_Expr
6823 (Comp_Id : Entity_Id;
6824 Associations : List_Id) return Node_Id;
6826 -- Given a component and a component associations list,
6827 -- locate the expression for that component; returns
6828 -- Empty if no such expression is found.
6830 ---------------------
6831 -- Associated_Expr --
6832 ---------------------
6834 function Associated_Expr
6835 (Comp_Id : Entity_Id;
6836 Associations : List_Id) return Node_Id
6838 Assoc : Node_Id;
6839 Choice : Node_Id;
6841 begin
6842 -- Simple linear search seems ok here
6844 Assoc := First (Associations);
6845 while Present (Assoc) loop
6846 Choice := First (Choices (Assoc));
6847 while Present (Choice) loop
6848 if (Nkind (Choice) = N_Identifier
6849 and then Chars (Choice) = Chars (Comp_Id))
6850 or else (Nkind (Choice) = N_Others_Choice)
6851 then
6852 return Expression (Assoc);
6853 end if;
6855 Next (Choice);
6856 end loop;
6858 Next (Assoc);
6859 end loop;
6861 return Empty;
6862 end Associated_Expr;
6864 -- Start of processing for Expand_Simple_Function_Return
6866 begin
6867 if not Positionals_Exhausted then
6868 Disc_Exp := First (Expressions (Discrim_Source));
6869 end if;
6871 loop
6872 if Positionals_Exhausted then
6873 Disc_Exp :=
6874 Associated_Expr
6875 (Discrim,
6876 Component_Associations (Discrim_Source));
6877 end if;
6879 if Ekind (Etype (Discrim)) =
6880 E_Anonymous_Access_Type
6881 then
6882 Check_Against_Result_Level
6883 (Dynamic_Accessibility_Level (Disc_Exp));
6884 end if;
6886 Next_Discriminant (Discrim);
6887 exit when not Present (Discrim);
6889 if not Positionals_Exhausted then
6890 Next (Disc_Exp);
6891 Positionals_Exhausted := not Present (Disc_Exp);
6892 end if;
6893 end loop;
6894 end;
6896 when N_Function_Call =>
6898 -- No check needed (check performed by callee)
6900 null;
6902 when others =>
6903 declare
6904 Level : constant Node_Id :=
6905 Make_Integer_Literal (Loc,
6906 Object_Access_Level (Discrim_Source));
6908 begin
6909 -- Unimplemented: check for name prefix that includes
6910 -- a dereference of an access value with a dynamic
6911 -- accessibility level (e.g., an access param or a
6912 -- saooaaat) and use dynamic level in that case. For
6913 -- example:
6914 -- return Access_Param.all(Some_Index).Some_Component;
6915 -- ???
6917 Set_Etype (Level, Standard_Natural);
6918 Check_Against_Result_Level (Level);
6919 end;
6920 end case;
6921 end;
6922 end if;
6924 -- If we are returning an object that may not be bit-aligned, then copy
6925 -- the value into a temporary first. This copy may need to expand to a
6926 -- loop of component operations.
6928 if Is_Possibly_Unaligned_Slice (Exp)
6929 or else Is_Possibly_Unaligned_Object (Exp)
6930 then
6931 declare
6932 ExpR : constant Node_Id := Relocate_Node (Exp);
6933 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6934 begin
6935 Insert_Action (Exp,
6936 Make_Object_Declaration (Loc,
6937 Defining_Identifier => Tnn,
6938 Constant_Present => True,
6939 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6940 Expression => ExpR),
6941 Suppress => All_Checks);
6942 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6943 end;
6944 end if;
6946 -- Call the _Postconditions procedure if the related function has
6947 -- contract assertions that need to be verified on exit.
6949 if Ekind (Scope_Id) = E_Function
6950 and then Present (Postconditions_Proc (Scope_Id))
6951 then
6952 -- In the case of discriminated objects, we have created a
6953 -- constrained subtype above, and used the underlying type. This
6954 -- transformation is post-analysis and harmless, except that now the
6955 -- call to the post-condition will be analyzed and the type kinds
6956 -- have to match.
6958 if Nkind (Exp) = N_Unchecked_Type_Conversion
6959 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
6960 then
6961 Rewrite (Exp, Expression (Relocate_Node (Exp)));
6962 end if;
6964 -- We are going to reference the returned value twice in this case,
6965 -- once in the call to _Postconditions, and once in the actual return
6966 -- statement, but we can't have side effects happening twice.
6968 Force_Evaluation (Exp, Mode => Strict);
6970 -- Generate call to _Postconditions
6972 Insert_Action (Exp,
6973 Make_Procedure_Call_Statement (Loc,
6974 Name =>
6975 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
6976 Parameter_Associations => New_List (New_Copy_Tree (Exp))));
6977 end if;
6979 -- Ada 2005 (AI-251): If this return statement corresponds with an
6980 -- simple return statement associated with an extended return statement
6981 -- and the type of the returned object is an interface then generate an
6982 -- implicit conversion to force displacement of the "this" pointer.
6984 if Ada_Version >= Ada_2005
6985 and then Comes_From_Extended_Return_Statement (N)
6986 and then Nkind (Expression (N)) = N_Identifier
6987 and then Is_Interface (Utyp)
6988 and then Utyp /= Underlying_Type (Exptyp)
6989 then
6990 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6991 Analyze_And_Resolve (Exp);
6992 end if;
6993 end Expand_Simple_Function_Return;
6995 --------------------------------------------
6996 -- Has_Unconstrained_Access_Discriminants --
6997 --------------------------------------------
6999 function Has_Unconstrained_Access_Discriminants
7000 (Subtyp : Entity_Id) return Boolean
7002 Discr : Entity_Id;
7004 begin
7005 if Has_Discriminants (Subtyp)
7006 and then not Is_Constrained (Subtyp)
7007 then
7008 Discr := First_Discriminant (Subtyp);
7009 while Present (Discr) loop
7010 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7011 return True;
7012 end if;
7014 Next_Discriminant (Discr);
7015 end loop;
7016 end if;
7018 return False;
7019 end Has_Unconstrained_Access_Discriminants;
7021 --------------------------------
7022 -- Is_Build_In_Place_Function --
7023 --------------------------------
7025 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7026 begin
7027 -- This function is called from Expand_Subtype_From_Expr during
7028 -- semantic analysis, even when expansion is off. In those cases
7029 -- the build_in_place expansion will not take place.
7031 if not Expander_Active then
7032 return False;
7033 end if;
7035 -- For now we test whether E denotes a function or access-to-function
7036 -- type whose result subtype is inherently limited. Later this test
7037 -- may be revised to allow composite nonlimited types. Functions with
7038 -- a foreign convention or whose result type has a foreign convention
7039 -- never qualify.
7041 if Ekind_In (E, E_Function, E_Generic_Function)
7042 or else (Ekind (E) = E_Subprogram_Type
7043 and then Etype (E) /= Standard_Void_Type)
7044 then
7045 -- Note: If the function has a foreign convention, it cannot build
7046 -- its result in place, so you're on your own. On the other hand,
7047 -- if only the return type has a foreign convention, its layout is
7048 -- intended to be compatible with the other language, but the build-
7049 -- in place machinery can ensure that the object is not copied.
7051 if Has_Foreign_Convention (E) then
7052 return False;
7054 -- In Ada 2005 all functions with an inherently limited return type
7055 -- must be handled using a build-in-place profile, including the case
7056 -- of a function with a limited interface result, where the function
7057 -- may return objects of nonlimited descendants.
7059 else
7060 return Is_Limited_View (Etype (E))
7061 and then Ada_Version >= Ada_2005
7062 and then not Debug_Flag_Dot_L;
7063 end if;
7065 else
7066 return False;
7067 end if;
7068 end Is_Build_In_Place_Function;
7070 -------------------------------------
7071 -- Is_Build_In_Place_Function_Call --
7072 -------------------------------------
7074 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7075 Exp_Node : Node_Id := N;
7076 Function_Id : Entity_Id;
7078 begin
7079 -- Return False if the expander is currently inactive, since awareness
7080 -- of build-in-place treatment is only relevant during expansion. Note
7081 -- that Is_Build_In_Place_Function, which is called as part of this
7082 -- function, is also conditioned this way, but we need to check here as
7083 -- well to avoid blowing up on processing protected calls when expansion
7084 -- is disabled (such as with -gnatc) since those would trip over the
7085 -- raise of Program_Error below.
7087 -- In SPARK mode, build-in-place calls are not expanded, so that we
7088 -- may end up with a call that is neither resolved to an entity, nor
7089 -- an indirect call.
7091 if not Expander_Active then
7092 return False;
7093 end if;
7095 -- Step past qualification, type conversion (which can occur in actual
7096 -- parameter contexts), and unchecked conversion (which can occur in
7097 -- cases of calls to 'Input).
7099 if Nkind_In (Exp_Node, N_Qualified_Expression,
7100 N_Type_Conversion,
7101 N_Unchecked_Type_Conversion)
7102 then
7103 Exp_Node := Expression (N);
7104 end if;
7106 if Nkind (Exp_Node) /= N_Function_Call then
7107 return False;
7109 else
7110 if Is_Entity_Name (Name (Exp_Node)) then
7111 Function_Id := Entity (Name (Exp_Node));
7113 -- In the case of an explicitly dereferenced call, use the subprogram
7114 -- type generated for the dereference.
7116 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7117 Function_Id := Etype (Name (Exp_Node));
7119 -- This may be a call to a protected function.
7121 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7122 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7124 else
7125 raise Program_Error;
7126 end if;
7128 return Is_Build_In_Place_Function (Function_Id);
7129 end if;
7130 end Is_Build_In_Place_Function_Call;
7132 -----------------------
7133 -- Freeze_Subprogram --
7134 -----------------------
7136 procedure Freeze_Subprogram (N : Node_Id) is
7137 Loc : constant Source_Ptr := Sloc (N);
7139 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7140 -- (Ada 2005): Register a predefined primitive in all the secondary
7141 -- dispatch tables of its primitive type.
7143 ----------------------------------
7144 -- Register_Predefined_DT_Entry --
7145 ----------------------------------
7147 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7148 Iface_DT_Ptr : Elmt_Id;
7149 Tagged_Typ : Entity_Id;
7150 Thunk_Id : Entity_Id;
7151 Thunk_Code : Node_Id;
7153 begin
7154 Tagged_Typ := Find_Dispatching_Type (Prim);
7156 if No (Access_Disp_Table (Tagged_Typ))
7157 or else not Has_Interfaces (Tagged_Typ)
7158 or else not RTE_Available (RE_Interface_Tag)
7159 or else Restriction_Active (No_Dispatching_Calls)
7160 then
7161 return;
7162 end if;
7164 -- Skip the first two access-to-dispatch-table pointers since they
7165 -- leads to the primary dispatch table (predefined DT and user
7166 -- defined DT). We are only concerned with the secondary dispatch
7167 -- table pointers. Note that the access-to- dispatch-table pointer
7168 -- corresponds to the first implemented interface retrieved below.
7170 Iface_DT_Ptr :=
7171 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7173 while Present (Iface_DT_Ptr)
7174 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7175 loop
7176 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7177 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7179 if Present (Thunk_Code) then
7180 Insert_Actions_After (N, New_List (
7181 Thunk_Code,
7183 Build_Set_Predefined_Prim_Op_Address (Loc,
7184 Tag_Node =>
7185 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7186 Position => DT_Position (Prim),
7187 Address_Node =>
7188 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7189 Make_Attribute_Reference (Loc,
7190 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7191 Attribute_Name => Name_Unrestricted_Access))),
7193 Build_Set_Predefined_Prim_Op_Address (Loc,
7194 Tag_Node =>
7195 New_Occurrence_Of
7196 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7197 Loc),
7198 Position => DT_Position (Prim),
7199 Address_Node =>
7200 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7201 Make_Attribute_Reference (Loc,
7202 Prefix => New_Occurrence_Of (Prim, Loc),
7203 Attribute_Name => Name_Unrestricted_Access)))));
7204 end if;
7206 -- Skip the tag of the predefined primitives dispatch table
7208 Next_Elmt (Iface_DT_Ptr);
7209 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7211 -- Skip tag of the no-thunks dispatch table
7213 Next_Elmt (Iface_DT_Ptr);
7214 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7216 -- Skip tag of predefined primitives no-thunks dispatch table
7218 Next_Elmt (Iface_DT_Ptr);
7219 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7221 Next_Elmt (Iface_DT_Ptr);
7222 end loop;
7223 end Register_Predefined_DT_Entry;
7225 -- Local variables
7227 Subp : constant Entity_Id := Entity (N);
7229 -- Start of processing for Freeze_Subprogram
7231 begin
7232 -- We suppress the initialization of the dispatch table entry when
7233 -- not Tagged_Type_Expansion because the dispatching mechanism is
7234 -- handled internally by the target.
7236 if Is_Dispatching_Operation (Subp)
7237 and then not Is_Abstract_Subprogram (Subp)
7238 and then Present (DTC_Entity (Subp))
7239 and then Present (Scope (DTC_Entity (Subp)))
7240 and then Tagged_Type_Expansion
7241 and then not Restriction_Active (No_Dispatching_Calls)
7242 and then RTE_Available (RE_Tag)
7243 then
7244 declare
7245 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7247 begin
7248 -- Handle private overridden primitives
7250 if not Is_CPP_Class (Typ) then
7251 Check_Overriding_Operation (Subp);
7252 end if;
7254 -- We assume that imported CPP primitives correspond with objects
7255 -- whose constructor is in the CPP side; therefore we don't need
7256 -- to generate code to register them in the dispatch table.
7258 if Is_CPP_Class (Typ) then
7259 null;
7261 -- Handle CPP primitives found in derivations of CPP_Class types.
7262 -- These primitives must have been inherited from some parent, and
7263 -- there is no need to register them in the dispatch table because
7264 -- Build_Inherit_Prims takes care of initializing these slots.
7266 elsif Is_Imported (Subp)
7267 and then (Convention (Subp) = Convention_CPP
7268 or else Convention (Subp) = Convention_C)
7269 then
7270 null;
7272 -- Generate code to register the primitive in non statically
7273 -- allocated dispatch tables
7275 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7277 -- When a primitive is frozen, enter its name in its dispatch
7278 -- table slot.
7280 if not Is_Interface (Typ)
7281 or else Present (Interface_Alias (Subp))
7282 then
7283 if Is_Predefined_Dispatching_Operation (Subp) then
7284 Register_Predefined_DT_Entry (Subp);
7285 end if;
7287 Insert_Actions_After (N,
7288 Register_Primitive (Loc, Prim => Subp));
7289 end if;
7290 end if;
7291 end;
7292 end if;
7294 -- Mark functions that return by reference. Note that it cannot be part
7295 -- of the normal semantic analysis of the spec since the underlying
7296 -- returned type may not be known yet (for private types).
7298 declare
7299 Typ : constant Entity_Id := Etype (Subp);
7300 Utyp : constant Entity_Id := Underlying_Type (Typ);
7302 begin
7303 if Is_Limited_View (Typ) then
7304 Set_Returns_By_Ref (Subp);
7306 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7307 Set_Returns_By_Ref (Subp);
7308 end if;
7309 end;
7311 -- Wnen freezing a null procedure, analyze its delayed aspects now
7312 -- because we may not have reached the end of the declarative list when
7313 -- delayed aspects are normally analyzed. This ensures that dispatching
7314 -- calls are properly rewritten when the generated _Postcondition
7315 -- procedure is analyzed in the null procedure body.
7317 if Nkind (Parent (Subp)) = N_Procedure_Specification
7318 and then Null_Present (Parent (Subp))
7319 then
7320 Analyze_Entry_Or_Subprogram_Contract (Subp);
7321 end if;
7322 end Freeze_Subprogram;
7324 ------------------------------
7325 -- Insert_Post_Call_Actions --
7326 ------------------------------
7328 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is
7329 Context : constant Node_Id := Parent (N);
7331 begin
7332 if Is_Empty_List (Post_Call) then
7333 return;
7334 end if;
7336 -- Cases where the call is not a member of a statement list. This
7337 -- includes the case where the call is an actual in another function
7338 -- call or indexing, i.e. an expression context as well.
7340 if not Is_List_Member (N)
7341 or else Nkind_In (Context, N_Function_Call, N_Indexed_Component)
7342 then
7343 -- In Ada 2012 the call may be a function call in an expression
7344 -- (since OUT and IN OUT parameters are now allowed for such calls).
7345 -- The write-back of (in)-out parameters is handled by the back-end,
7346 -- but the constraint checks generated when subtypes of formal and
7347 -- actual don't match must be inserted in the form of assignments.
7349 if Nkind (Original_Node (N)) = N_Function_Call then
7350 pragma Assert (Ada_Version >= Ada_2012);
7351 -- Functions with '[in] out' parameters are only allowed in Ada
7352 -- 2012.
7354 -- We used to handle this by climbing up parents to a
7355 -- non-statement/declaration and then simply making a call to
7356 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7357 -- for Ada 2012. If we are in the middle of an expression, e.g.
7358 -- the condition of an IF, this call would insert after the IF
7359 -- statement, which is much too late to be doing the write back.
7360 -- For example:
7362 -- if Clobber (X) then
7363 -- Put_Line (X'Img);
7364 -- else
7365 -- goto Junk
7366 -- end if;
7368 -- Now assume Clobber changes X, if we put the write back after
7369 -- the IF, the Put_Line gets the wrong value and the goto causes
7370 -- the write back to be skipped completely.
7372 -- To deal with this, we replace the call by
7374 -- do
7375 -- Tnnn : constant function-result-type := function-call;
7376 -- Post_Call actions
7377 -- in
7378 -- Tnnn;
7379 -- end;
7381 declare
7382 Loc : constant Source_Ptr := Sloc (N);
7383 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
7384 FRTyp : constant Entity_Id := Etype (N);
7385 Name : constant Node_Id := Relocate_Node (N);
7387 begin
7388 Prepend_To (Post_Call,
7389 Make_Object_Declaration (Loc,
7390 Defining_Identifier => Tnnn,
7391 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
7392 Constant_Present => True,
7393 Expression => Name));
7395 Rewrite (N,
7396 Make_Expression_With_Actions (Loc,
7397 Actions => Post_Call,
7398 Expression => New_Occurrence_Of (Tnnn, Loc)));
7400 -- We don't want to just blindly call Analyze_And_Resolve
7401 -- because that would cause unwanted recursion on the call.
7402 -- So for a moment set the call as analyzed to prevent that
7403 -- recursion, and get the rest analyzed properly, then reset
7404 -- the analyzed flag, so our caller can continue.
7406 Set_Analyzed (Name, True);
7407 Analyze_And_Resolve (N, FRTyp);
7408 Set_Analyzed (Name, False);
7409 end;
7411 -- If not the special Ada 2012 case of a function call, then we must
7412 -- have the triggering statement of a triggering alternative or an
7413 -- entry call alternative, and we can add the post call stuff to the
7414 -- corresponding statement list.
7416 else
7417 pragma Assert (Nkind_In (Context, N_Entry_Call_Alternative,
7418 N_Triggering_Alternative));
7420 if Is_Non_Empty_List (Statements (Context)) then
7421 Insert_List_Before_And_Analyze
7422 (First (Statements (Context)), Post_Call);
7423 else
7424 Set_Statements (Context, Post_Call);
7425 end if;
7426 end if;
7428 -- A procedure call is always part of a declarative or statement list,
7429 -- however a function call may appear nested within a construct. Most
7430 -- cases of function call nesting are handled in the special case above.
7431 -- The only exception is when the function call acts as an actual in a
7432 -- procedure call. In this case the function call is in a list, but the
7433 -- post-call actions must be inserted after the procedure call.
7435 elsif Nkind (Context) = N_Procedure_Call_Statement then
7436 Insert_Actions_After (Context, Post_Call);
7438 -- Otherwise, normal case where N is in a statement sequence, just put
7439 -- the post-call stuff after the call statement.
7441 else
7442 Insert_Actions_After (N, Post_Call);
7443 end if;
7444 end Insert_Post_Call_Actions;
7446 -----------------------
7447 -- Is_Null_Procedure --
7448 -----------------------
7450 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
7451 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
7453 begin
7454 if Ekind (Subp) /= E_Procedure then
7455 return False;
7457 -- Check if this is a declared null procedure
7459 elsif Nkind (Decl) = N_Subprogram_Declaration then
7460 if not Null_Present (Specification (Decl)) then
7461 return False;
7463 elsif No (Body_To_Inline (Decl)) then
7464 return False;
7466 -- Check if the body contains only a null statement, followed by
7467 -- the return statement added during expansion.
7469 else
7470 declare
7471 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
7473 Stat : Node_Id;
7474 Stat2 : Node_Id;
7476 begin
7477 if Nkind (Orig_Bod) /= N_Subprogram_Body then
7478 return False;
7479 else
7480 -- We must skip SCIL nodes because they are currently
7481 -- implemented as special N_Null_Statement nodes.
7483 Stat :=
7484 First_Non_SCIL_Node
7485 (Statements (Handled_Statement_Sequence (Orig_Bod)));
7486 Stat2 := Next_Non_SCIL_Node (Stat);
7488 return
7489 Is_Empty_List (Declarations (Orig_Bod))
7490 and then Nkind (Stat) = N_Null_Statement
7491 and then
7492 (No (Stat2)
7493 or else
7494 (Nkind (Stat2) = N_Simple_Return_Statement
7495 and then No (Next (Stat2))));
7496 end if;
7497 end;
7498 end if;
7500 else
7501 return False;
7502 end if;
7503 end Is_Null_Procedure;
7505 -------------------------------------------
7506 -- Make_Build_In_Place_Call_In_Allocator --
7507 -------------------------------------------
7509 procedure Make_Build_In_Place_Call_In_Allocator
7510 (Allocator : Node_Id;
7511 Function_Call : Node_Id)
7513 Acc_Type : constant Entity_Id := Etype (Allocator);
7514 Loc : Source_Ptr;
7515 Func_Call : Node_Id := Function_Call;
7516 Ref_Func_Call : Node_Id;
7517 Function_Id : Entity_Id;
7518 Result_Subt : Entity_Id;
7519 New_Allocator : Node_Id;
7520 Return_Obj_Access : Entity_Id; -- temp for function result
7521 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
7522 Alloc_Form : BIP_Allocation_Form;
7523 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
7524 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
7525 Chain : Entity_Id; -- activation chain, in case of tasks
7527 begin
7528 -- Step past qualification or unchecked conversion (the latter can occur
7529 -- in cases of calls to 'Input).
7531 if Nkind_In (Func_Call,
7532 N_Qualified_Expression,
7533 N_Type_Conversion,
7534 N_Unchecked_Type_Conversion)
7535 then
7536 Func_Call := Expression (Func_Call);
7537 end if;
7539 -- If the call has already been processed to add build-in-place actuals
7540 -- then return. This should not normally occur in an allocator context,
7541 -- but we add the protection as a defensive measure.
7543 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7544 return;
7545 end if;
7547 -- Mark the call as processed as a build-in-place call
7549 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7551 Loc := Sloc (Function_Call);
7553 if Is_Entity_Name (Name (Func_Call)) then
7554 Function_Id := Entity (Name (Func_Call));
7556 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7557 Function_Id := Etype (Name (Func_Call));
7559 else
7560 raise Program_Error;
7561 end if;
7563 Result_Subt := Available_View (Etype (Function_Id));
7565 -- Create a temp for the function result. In the caller-allocates case,
7566 -- this will be initialized to the result of a new uninitialized
7567 -- allocator. Note: we do not use Allocator as the Related_Node of
7568 -- Return_Obj_Access in call to Make_Temporary below as this would
7569 -- create a sort of infinite "recursion".
7571 Return_Obj_Access := Make_Temporary (Loc, 'R');
7572 Set_Etype (Return_Obj_Access, Acc_Type);
7574 -- When the result subtype is constrained, the return object is
7575 -- allocated on the caller side, and access to it is passed to the
7576 -- function.
7578 -- Here and in related routines, we must examine the full view of the
7579 -- type, because the view at the point of call may differ from that
7580 -- that in the function body, and the expansion mechanism depends on
7581 -- the characteristics of the full view.
7583 if Is_Constrained (Underlying_Type (Result_Subt)) then
7585 -- Replace the initialized allocator of form "new T'(Func (...))"
7586 -- with an uninitialized allocator of form "new T", where T is the
7587 -- result subtype of the called function. The call to the function
7588 -- is handled separately further below.
7590 New_Allocator :=
7591 Make_Allocator (Loc,
7592 Expression => New_Occurrence_Of (Result_Subt, Loc));
7593 Set_No_Initialization (New_Allocator);
7595 -- Copy attributes to new allocator. Note that the new allocator
7596 -- logically comes from source if the original one did, so copy the
7597 -- relevant flag. This ensures proper treatment of the restriction
7598 -- No_Implicit_Heap_Allocations in this case.
7600 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
7601 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
7602 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
7604 Rewrite (Allocator, New_Allocator);
7606 -- Initial value of the temp is the result of the uninitialized
7607 -- allocator
7609 Temp_Init := Relocate_Node (Allocator);
7611 -- Indicate that caller allocates, and pass in the return object
7613 Alloc_Form := Caller_Allocation;
7614 Pool := Make_Null (No_Location);
7615 Return_Obj_Actual :=
7616 Make_Unchecked_Type_Conversion (Loc,
7617 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7618 Expression =>
7619 Make_Explicit_Dereference (Loc,
7620 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
7622 -- When the result subtype is unconstrained, the function itself must
7623 -- perform the allocation of the return object, so we pass parameters
7624 -- indicating that.
7626 else
7627 Temp_Init := Empty;
7629 -- Case of a user-defined storage pool. Pass an allocation parameter
7630 -- indicating that the function should allocate its result in the
7631 -- pool, and pass the pool. Use 'Unrestricted_Access because the
7632 -- pool may not be aliased.
7634 if Present (Associated_Storage_Pool (Acc_Type)) then
7635 Alloc_Form := User_Storage_Pool;
7636 Pool :=
7637 Make_Attribute_Reference (Loc,
7638 Prefix =>
7639 New_Occurrence_Of
7640 (Associated_Storage_Pool (Acc_Type), Loc),
7641 Attribute_Name => Name_Unrestricted_Access);
7643 -- No user-defined pool; pass an allocation parameter indicating that
7644 -- the function should allocate its result on the heap.
7646 else
7647 Alloc_Form := Global_Heap;
7648 Pool := Make_Null (No_Location);
7649 end if;
7651 -- The caller does not provide the return object in this case, so we
7652 -- have to pass null for the object access actual.
7654 Return_Obj_Actual := Empty;
7655 end if;
7657 -- Declare the temp object
7659 Insert_Action (Allocator,
7660 Make_Object_Declaration (Loc,
7661 Defining_Identifier => Return_Obj_Access,
7662 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
7663 Expression => Temp_Init));
7665 Ref_Func_Call := Make_Reference (Loc, Func_Call);
7667 -- Ada 2005 (AI-251): If the type of the allocator is an interface
7668 -- then generate an implicit conversion to force displacement of the
7669 -- "this" pointer.
7671 if Is_Interface (Designated_Type (Acc_Type)) then
7672 Rewrite
7673 (Ref_Func_Call,
7674 OK_Convert_To (Acc_Type, Ref_Func_Call));
7675 end if;
7677 declare
7678 Assign : constant Node_Id :=
7679 Make_Assignment_Statement (Loc,
7680 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
7681 Expression => Ref_Func_Call);
7682 -- Assign the result of the function call into the temp. In the
7683 -- caller-allocates case, this is overwriting the temp with its
7684 -- initial value, which has no effect. In the callee-allocates case,
7685 -- this is setting the temp to point to the object allocated by the
7686 -- callee.
7688 Actions : List_Id;
7689 -- Actions to be inserted. If there are no tasks, this is just the
7690 -- assignment statement. If the allocated object has tasks, we need
7691 -- to wrap the assignment in a block that activates them. The
7692 -- activation chain of that block must be passed to the function,
7693 -- rather than some outer chain.
7694 begin
7695 if Has_Task (Result_Subt) then
7696 Actions := New_List;
7697 Build_Task_Allocate_Block_With_Init_Stmts
7698 (Actions, Allocator, Init_Stmts => New_List (Assign));
7699 Chain := Activation_Chain_Entity (Last (Actions));
7700 else
7701 Actions := New_List (Assign);
7702 Chain := Empty;
7703 end if;
7705 Insert_Actions (Allocator, Actions);
7706 end;
7708 -- When the function has a controlling result, an allocation-form
7709 -- parameter must be passed indicating that the caller is allocating
7710 -- the result object. This is needed because such a function can be
7711 -- called as a dispatching operation and must be treated similarly
7712 -- to functions with unconstrained result subtypes.
7714 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7715 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
7717 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7718 (Func_Call, Function_Id, Acc_Type);
7720 Add_Task_Actuals_To_Build_In_Place_Call
7721 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
7722 Chain => Chain);
7724 -- Add an implicit actual to the function call that provides access
7725 -- to the allocated object. An unchecked conversion to the (specific)
7726 -- result subtype of the function is inserted to handle cases where
7727 -- the access type of the allocator has a class-wide designated type.
7729 Add_Access_Actual_To_Build_In_Place_Call
7730 (Func_Call, Function_Id, Return_Obj_Actual);
7732 -- Finally, replace the allocator node with a reference to the temp
7734 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
7736 Analyze_And_Resolve (Allocator, Acc_Type);
7737 end Make_Build_In_Place_Call_In_Allocator;
7739 ---------------------------------------------------
7740 -- Make_Build_In_Place_Call_In_Anonymous_Context --
7741 ---------------------------------------------------
7743 procedure Make_Build_In_Place_Call_In_Anonymous_Context
7744 (Function_Call : Node_Id)
7746 Loc : Source_Ptr;
7747 Func_Call : Node_Id := Function_Call;
7748 Function_Id : Entity_Id;
7749 Result_Subt : Entity_Id;
7750 Return_Obj_Id : Entity_Id;
7751 Return_Obj_Decl : Entity_Id;
7753 Definite : Boolean;
7754 -- True if result subtype is definite, or has a size that does not
7755 -- require secondary stack usage (i.e. no variant part or components
7756 -- whose type depends on discriminants). In particular, untagged types
7757 -- with only access discriminants do not require secondary stack use.
7758 -- Note that if the return type is tagged we must always use the sec.
7759 -- stack because the call may dispatch on result.
7761 begin
7762 -- Step past qualification, type conversion (which can occur in actual
7763 -- parameter contexts), and unchecked conversion (which can occur in
7764 -- cases of calls to 'Input).
7766 if Nkind_In (Func_Call, N_Qualified_Expression,
7767 N_Type_Conversion,
7768 N_Unchecked_Type_Conversion)
7769 then
7770 Func_Call := Expression (Func_Call);
7771 end if;
7773 -- If the call has already been processed to add build-in-place actuals
7774 -- then return. One place this can occur is for calls to build-in-place
7775 -- functions that occur within a call to a protected operation, where
7776 -- due to rewriting and expansion of the protected call there can be
7777 -- more than one call to Expand_Actuals for the same set of actuals.
7779 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7780 return;
7781 end if;
7783 -- Mark the call as processed as a build-in-place call
7785 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7787 Loc := Sloc (Function_Call);
7789 if Is_Entity_Name (Name (Func_Call)) then
7790 Function_Id := Entity (Name (Func_Call));
7792 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7793 Function_Id := Etype (Name (Func_Call));
7795 else
7796 raise Program_Error;
7797 end if;
7799 Result_Subt := Etype (Function_Id);
7800 Definite :=
7801 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
7802 and then not Is_Tagged_Type (Result_Subt))
7803 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
7805 -- If the build-in-place function returns a controlled object, then the
7806 -- object needs to be finalized immediately after the context. Since
7807 -- this case produces a transient scope, the servicing finalizer needs
7808 -- to name the returned object. Create a temporary which is initialized
7809 -- with the function call:
7811 -- Temp_Id : Func_Type := BIP_Func_Call;
7813 -- The initialization expression of the temporary will be rewritten by
7814 -- the expander using the appropriate mechanism in Make_Build_In_Place_
7815 -- Call_In_Object_Declaration.
7817 if Needs_Finalization (Result_Subt) then
7818 declare
7819 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
7820 Temp_Decl : Node_Id;
7822 begin
7823 -- Reset the guard on the function call since the following does
7824 -- not perform actual call expansion.
7826 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
7828 Temp_Decl :=
7829 Make_Object_Declaration (Loc,
7830 Defining_Identifier => Temp_Id,
7831 Object_Definition =>
7832 New_Occurrence_Of (Result_Subt, Loc),
7833 Expression =>
7834 New_Copy_Tree (Function_Call));
7836 Insert_Action (Function_Call, Temp_Decl);
7838 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
7839 Analyze (Function_Call);
7840 end;
7842 -- When the result subtype is definite, an object of the subtype is
7843 -- declared and an access value designating it is passed as an actual.
7845 elsif Definite then
7847 -- Create a temporary object to hold the function result
7849 Return_Obj_Id := Make_Temporary (Loc, 'R');
7850 Set_Etype (Return_Obj_Id, Result_Subt);
7852 Return_Obj_Decl :=
7853 Make_Object_Declaration (Loc,
7854 Defining_Identifier => Return_Obj_Id,
7855 Aliased_Present => True,
7856 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
7858 Set_No_Initialization (Return_Obj_Decl);
7860 Insert_Action (Func_Call, Return_Obj_Decl);
7862 -- When the function has a controlling result, an allocation-form
7863 -- parameter must be passed indicating that the caller is allocating
7864 -- the result object. This is needed because such a function can be
7865 -- called as a dispatching operation and must be treated similarly
7866 -- to functions with unconstrained result subtypes.
7868 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7869 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7871 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7872 (Func_Call, Function_Id);
7874 Add_Task_Actuals_To_Build_In_Place_Call
7875 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7877 -- Add an implicit actual to the function call that provides access
7878 -- to the caller's return object.
7880 Add_Access_Actual_To_Build_In_Place_Call
7881 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
7883 -- When the result subtype is unconstrained, the function must allocate
7884 -- the return object in the secondary stack, so appropriate implicit
7885 -- parameters are added to the call to indicate that. A transient
7886 -- scope is established to ensure eventual cleanup of the result.
7888 else
7889 -- Pass an allocation parameter indicating that the function should
7890 -- allocate its result on the secondary stack.
7892 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7893 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
7895 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7896 (Func_Call, Function_Id);
7898 Add_Task_Actuals_To_Build_In_Place_Call
7899 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7901 -- Pass a null value to the function since no return object is
7902 -- available on the caller side.
7904 Add_Access_Actual_To_Build_In_Place_Call
7905 (Func_Call, Function_Id, Empty);
7906 end if;
7907 end Make_Build_In_Place_Call_In_Anonymous_Context;
7909 --------------------------------------------
7910 -- Make_Build_In_Place_Call_In_Assignment --
7911 --------------------------------------------
7913 procedure Make_Build_In_Place_Call_In_Assignment
7914 (Assign : Node_Id;
7915 Function_Call : Node_Id)
7917 Lhs : constant Node_Id := Name (Assign);
7918 Func_Call : Node_Id := Function_Call;
7919 Func_Id : Entity_Id;
7920 Loc : Source_Ptr;
7921 Obj_Decl : Node_Id;
7922 Obj_Id : Entity_Id;
7923 Ptr_Typ : Entity_Id;
7924 Ptr_Typ_Decl : Node_Id;
7925 New_Expr : Node_Id;
7926 Result_Subt : Entity_Id;
7927 Target : Node_Id;
7929 begin
7930 -- Step past qualification or unchecked conversion (the latter can occur
7931 -- in cases of calls to 'Input).
7933 if Nkind_In (Func_Call, N_Qualified_Expression,
7934 N_Unchecked_Type_Conversion)
7935 then
7936 Func_Call := Expression (Func_Call);
7937 end if;
7939 -- If the call has already been processed to add build-in-place actuals
7940 -- then return. This should not normally occur in an assignment context,
7941 -- but we add the protection as a defensive measure.
7943 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7944 return;
7945 end if;
7947 -- Mark the call as processed as a build-in-place call
7949 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7951 Loc := Sloc (Function_Call);
7953 if Is_Entity_Name (Name (Func_Call)) then
7954 Func_Id := Entity (Name (Func_Call));
7956 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7957 Func_Id := Etype (Name (Func_Call));
7959 else
7960 raise Program_Error;
7961 end if;
7963 Result_Subt := Etype (Func_Id);
7965 -- When the result subtype is unconstrained, an additional actual must
7966 -- be passed to indicate that the caller is providing the return object.
7967 -- This parameter must also be passed when the called function has a
7968 -- controlling result, because dispatching calls to the function needs
7969 -- to be treated effectively the same as calls to class-wide functions.
7971 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7972 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
7974 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7975 (Func_Call, Func_Id);
7977 Add_Task_Actuals_To_Build_In_Place_Call
7978 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
7980 -- Add an implicit actual to the function call that provides access to
7981 -- the caller's return object.
7983 Add_Access_Actual_To_Build_In_Place_Call
7984 (Func_Call,
7985 Func_Id,
7986 Make_Unchecked_Type_Conversion (Loc,
7987 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7988 Expression => Relocate_Node (Lhs)));
7990 -- Create an access type designating the function's result subtype
7992 Ptr_Typ := Make_Temporary (Loc, 'A');
7994 Ptr_Typ_Decl :=
7995 Make_Full_Type_Declaration (Loc,
7996 Defining_Identifier => Ptr_Typ,
7997 Type_Definition =>
7998 Make_Access_To_Object_Definition (Loc,
7999 All_Present => True,
8000 Subtype_Indication =>
8001 New_Occurrence_Of (Result_Subt, Loc)));
8002 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8004 -- Finally, create an access object initialized to a reference to the
8005 -- function call. We know this access value is non-null, so mark the
8006 -- entity accordingly to suppress junk access checks.
8008 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8010 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8011 Set_Etype (Obj_Id, Ptr_Typ);
8012 Set_Is_Known_Non_Null (Obj_Id);
8014 Obj_Decl :=
8015 Make_Object_Declaration (Loc,
8016 Defining_Identifier => Obj_Id,
8017 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8018 Expression => New_Expr);
8019 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8021 Rewrite (Assign, Make_Null_Statement (Loc));
8023 -- Retrieve the target of the assignment
8025 if Nkind (Lhs) = N_Selected_Component then
8026 Target := Selector_Name (Lhs);
8027 elsif Nkind (Lhs) = N_Type_Conversion then
8028 Target := Expression (Lhs);
8029 else
8030 Target := Lhs;
8031 end if;
8033 -- If we are assigning to a return object or this is an expression of
8034 -- an extension aggregate, the target should either be an identifier
8035 -- or a simple expression. All other cases imply a different scenario.
8037 if Nkind (Target) in N_Has_Entity then
8038 Target := Entity (Target);
8039 else
8040 return;
8041 end if;
8042 end Make_Build_In_Place_Call_In_Assignment;
8044 ----------------------------------------------------
8045 -- Make_Build_In_Place_Call_In_Object_Declaration --
8046 ----------------------------------------------------
8048 procedure Make_Build_In_Place_Call_In_Object_Declaration
8049 (Obj_Decl : Node_Id;
8050 Function_Call : Node_Id)
8052 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
8053 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
8054 Loc : constant Source_Ptr := Sloc (Function_Call);
8055 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
8057 Call_Deref : Node_Id;
8058 Caller_Object : Node_Id;
8059 Def_Id : Entity_Id;
8060 Fmaster_Actual : Node_Id := Empty;
8061 Func_Call : Node_Id := Function_Call;
8062 Function_Id : Entity_Id;
8063 Pool_Actual : Node_Id;
8064 Ptr_Typ : Entity_Id;
8065 Ptr_Typ_Decl : Node_Id;
8066 Pass_Caller_Acc : Boolean := False;
8067 Res_Decl : Node_Id;
8068 Result_Subt : Entity_Id;
8070 Definite : Boolean;
8071 -- True if result subtype is definite, or has a size that does not
8072 -- require secondary stack usage (i.e. no variant part or components
8073 -- whose type depends on discriminants). In particular, untagged types
8074 -- with only access discriminants do not require secondary stack use.
8075 -- Note that if the return type is tagged we must always use the sec.
8076 -- stack because the call may dispatch on result.
8078 begin
8079 -- Step past qualification or unchecked conversion (the latter can occur
8080 -- in cases of calls to 'Input).
8082 if Nkind_In (Func_Call, N_Qualified_Expression,
8083 N_Unchecked_Type_Conversion)
8084 then
8085 Func_Call := Expression (Func_Call);
8086 end if;
8088 -- If the call has already been processed to add build-in-place actuals
8089 -- then return. This should not normally occur in an object declaration,
8090 -- but we add the protection as a defensive measure.
8092 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8093 return;
8094 end if;
8096 -- Mark the call as processed as a build-in-place call
8098 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8100 if Is_Entity_Name (Name (Func_Call)) then
8101 Function_Id := Entity (Name (Func_Call));
8103 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8104 Function_Id := Etype (Name (Func_Call));
8106 else
8107 raise Program_Error;
8108 end if;
8110 Result_Subt := Etype (Function_Id);
8111 Definite :=
8112 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
8113 and then not Is_Tagged_Type (Result_Subt))
8114 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
8116 -- Create an access type designating the function's result subtype. We
8117 -- use the type of the original call because it may be a call to an
8118 -- inherited operation, which the expansion has replaced with the parent
8119 -- operation that yields the parent type. Note that this access type
8120 -- must be declared before we establish a transient scope, so that it
8121 -- receives the proper accessibility level.
8123 Ptr_Typ := Make_Temporary (Loc, 'A');
8124 Ptr_Typ_Decl :=
8125 Make_Full_Type_Declaration (Loc,
8126 Defining_Identifier => Ptr_Typ,
8127 Type_Definition =>
8128 Make_Access_To_Object_Definition (Loc,
8129 All_Present => True,
8130 Subtype_Indication =>
8131 New_Occurrence_Of (Etype (Function_Call), Loc)));
8133 -- The access type and its accompanying object must be inserted after
8134 -- the object declaration in the constrained case, so that the function
8135 -- call can be passed access to the object. In the indefinite case,
8136 -- or if the object declaration is for a return object, the access type
8137 -- and object must be inserted before the object, since the object
8138 -- declaration is rewritten to be a renaming of a dereference of the
8139 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8140 -- the result object is in a different (transient) scope, so won't
8141 -- cause freezing.
8143 if Definite
8144 and then not Is_Return_Object (Defining_Identifier (Obj_Decl))
8145 then
8146 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
8147 else
8148 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
8149 end if;
8151 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8152 -- elaborated in an inner (transient) scope and thus won't cause
8153 -- freezing by itself.
8155 declare
8156 Ptr_Typ_Freeze_Ref : constant Node_Id :=
8157 New_Occurrence_Of (Ptr_Typ, Loc);
8158 begin
8159 Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl);
8160 Freeze_Expression (Ptr_Typ_Freeze_Ref);
8161 end;
8163 -- If the object is a return object of an enclosing build-in-place
8164 -- function, then the implicit build-in-place parameters of the
8165 -- enclosing function are simply passed along to the called function.
8166 -- (Unfortunately, this won't cover the case of extension aggregates
8167 -- where the ancestor part is a build-in-place indefinite function
8168 -- call that should be passed along the caller's parameters. Currently
8169 -- those get mishandled by reassigning the result of the call to the
8170 -- aggregate return object, when the call result should really be
8171 -- directly built in place in the aggregate and not in a temporary. ???)
8173 if Is_Return_Object (Defining_Identifier (Obj_Decl)) then
8174 Pass_Caller_Acc := True;
8176 -- When the enclosing function has a BIP_Alloc_Form formal then we
8177 -- pass it along to the callee (such as when the enclosing function
8178 -- has an unconstrained or tagged result type).
8180 if Needs_BIP_Alloc_Form (Encl_Func) then
8181 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8182 Pool_Actual :=
8183 New_Occurrence_Of
8184 (Build_In_Place_Formal (Encl_Func, BIP_Storage_Pool), Loc);
8186 -- The build-in-place pool formal is not built on e.g. ZFP
8188 else
8189 Pool_Actual := Empty;
8190 end if;
8192 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8193 (Function_Call => Func_Call,
8194 Function_Id => Function_Id,
8195 Alloc_Form_Exp =>
8196 New_Occurrence_Of
8197 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
8198 Pool_Actual => Pool_Actual);
8200 -- Otherwise, if enclosing function has a definite result subtype,
8201 -- then caller allocation will be used.
8203 else
8204 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8205 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8206 end if;
8208 if Needs_BIP_Finalization_Master (Encl_Func) then
8209 Fmaster_Actual :=
8210 New_Occurrence_Of
8211 (Build_In_Place_Formal
8212 (Encl_Func, BIP_Finalization_Master), Loc);
8213 end if;
8215 -- Retrieve the BIPacc formal from the enclosing function and convert
8216 -- it to the access type of the callee's BIP_Object_Access formal.
8218 Caller_Object :=
8219 Make_Unchecked_Type_Conversion (Loc,
8220 Subtype_Mark =>
8221 New_Occurrence_Of
8222 (Etype
8223 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
8224 Loc),
8225 Expression =>
8226 New_Occurrence_Of
8227 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access),
8228 Loc));
8230 -- In the definite case, add an implicit actual to the function call
8231 -- that provides access to the declared object. An unchecked conversion
8232 -- to the (specific) result type of the function is inserted to handle
8233 -- the case where the object is declared with a class-wide type.
8235 elsif Definite then
8236 Caller_Object :=
8237 Make_Unchecked_Type_Conversion (Loc,
8238 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8239 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
8241 -- When the function has a controlling result, an allocation-form
8242 -- parameter must be passed indicating that the caller is allocating
8243 -- the result object. This is needed because such a function can be
8244 -- called as a dispatching operation and must be treated similarly
8245 -- to functions with indefinite result subtypes.
8247 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8248 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8250 -- The allocation for indefinite library-level objects occurs on the
8251 -- heap as opposed to the secondary stack. This accommodates DLLs where
8252 -- the secondary stack is destroyed after each library unload. This is
8253 -- a hybrid mechanism where a stack-allocated object lives on the heap.
8255 elsif Is_Library_Level_Entity (Defining_Identifier (Obj_Decl))
8256 and then not Restriction_Active (No_Implicit_Heap_Allocations)
8257 then
8258 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8259 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
8260 Caller_Object := Empty;
8262 -- Create a finalization master for the access result type to ensure
8263 -- that the heap allocation can properly chain the object and later
8264 -- finalize it when the library unit goes out of scope.
8266 if Needs_Finalization (Etype (Func_Call)) then
8267 Build_Finalization_Master
8268 (Typ => Ptr_Typ,
8269 For_Lib_Level => True,
8270 Insertion_Node => Ptr_Typ_Decl);
8272 Fmaster_Actual :=
8273 Make_Attribute_Reference (Loc,
8274 Prefix =>
8275 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
8276 Attribute_Name => Name_Unrestricted_Access);
8277 end if;
8279 -- In other indefinite cases, pass an indication to do the allocation
8280 -- on the secondary stack and set Caller_Object to Empty so that a null
8281 -- value will be passed for the caller's object address. A transient
8282 -- scope is established to ensure eventual cleanup of the result.
8284 else
8285 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8286 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8287 Caller_Object := Empty;
8289 Establish_Transient_Scope (Obj_Decl, Sec_Stack => True);
8290 end if;
8292 -- Pass along any finalization master actual, which is needed in the
8293 -- case where the called function initializes a return object of an
8294 -- enclosing build-in-place function.
8296 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8297 (Func_Call => Func_Call,
8298 Func_Id => Function_Id,
8299 Master_Exp => Fmaster_Actual);
8301 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
8302 and then Has_Task (Result_Subt)
8303 then
8304 -- Here we're passing along the master that was passed in to this
8305 -- function.
8307 Add_Task_Actuals_To_Build_In_Place_Call
8308 (Func_Call, Function_Id,
8309 Master_Actual =>
8310 New_Occurrence_Of
8311 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
8313 else
8314 Add_Task_Actuals_To_Build_In_Place_Call
8315 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8316 end if;
8318 Add_Access_Actual_To_Build_In_Place_Call
8319 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
8321 -- Finally, create an access object initialized to a reference to the
8322 -- function call. We know this access value cannot be null, so mark the
8323 -- entity accordingly to suppress the access check.
8325 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
8326 Set_Etype (Def_Id, Ptr_Typ);
8327 Set_Is_Known_Non_Null (Def_Id);
8329 Res_Decl :=
8330 Make_Object_Declaration (Loc,
8331 Defining_Identifier => Def_Id,
8332 Constant_Present => True,
8333 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8334 Expression =>
8335 Make_Reference (Loc, Relocate_Node (Func_Call)));
8337 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
8339 -- If the result subtype of the called function is definite and is not
8340 -- itself the return expression of an enclosing BIP function, then mark
8341 -- the object as having no initialization.
8343 if Definite
8344 and then not Is_Return_Object (Defining_Identifier (Obj_Decl))
8345 then
8346 -- The related object declaration is encased in a transient block
8347 -- because the build-in-place function call contains at least one
8348 -- nested function call that produces a controlled transient
8349 -- temporary:
8351 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8353 -- Since the build-in-place expansion decouples the call from the
8354 -- object declaration, the finalization machinery lacks the context
8355 -- which prompted the generation of the transient block. To resolve
8356 -- this scenario, store the build-in-place call.
8358 if Scope_Is_Transient and then Node_To_Be_Wrapped = Obj_Decl then
8359 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
8360 end if;
8362 Set_Expression (Obj_Decl, Empty);
8363 Set_No_Initialization (Obj_Decl);
8365 -- In case of an indefinite result subtype, or if the call is the
8366 -- return expression of an enclosing BIP function, rewrite the object
8367 -- declaration as an object renaming where the renamed object is a
8368 -- dereference of <function_Call>'reference:
8370 -- Obj : Subt renames <function_call>'Ref.all;
8372 else
8373 Call_Deref :=
8374 Make_Explicit_Dereference (Obj_Loc,
8375 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
8377 Rewrite (Obj_Decl,
8378 Make_Object_Renaming_Declaration (Obj_Loc,
8379 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
8380 Subtype_Mark => New_Occurrence_Of (Result_Subt, Obj_Loc),
8381 Name => Call_Deref));
8383 Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref);
8385 -- If the original entity comes from source, then mark the new
8386 -- entity as needing debug information, even though it's defined
8387 -- by a generated renaming that does not come from source, so that
8388 -- the Materialize_Entity flag will be set on the entity when
8389 -- Debug_Renaming_Declaration is called during analysis.
8391 if Comes_From_Source (Obj_Def_Id) then
8392 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
8393 end if;
8395 Analyze (Obj_Decl);
8397 -- Replace the internal identifier of the renaming declaration's
8398 -- entity with identifier of the original object entity. We also have
8399 -- to exchange the entities containing their defining identifiers to
8400 -- ensure the correct replacement of the object declaration by the
8401 -- object renaming declaration to avoid homograph conflicts (since
8402 -- the object declaration's defining identifier was already entered
8403 -- in current scope). The Next_Entity links of the two entities also
8404 -- have to be swapped since the entities are part of the return
8405 -- scope's entity list and the list structure would otherwise be
8406 -- corrupted. Finally, the homonym chain must be preserved as well.
8408 declare
8409 Ren_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
8410 Next_Id : constant Entity_Id := Next_Entity (Ren_Id);
8412 begin
8413 Set_Chars (Ren_Id, Chars (Obj_Def_Id));
8415 -- Swap next entity links in preparation for exchanging entities
8417 Set_Next_Entity (Ren_Id, Next_Entity (Obj_Def_Id));
8418 Set_Next_Entity (Obj_Def_Id, Next_Id);
8419 Set_Homonym (Ren_Id, Homonym (Obj_Def_Id));
8421 Exchange_Entities (Ren_Id, Obj_Def_Id);
8423 -- Preserve source indication of original declaration, so that
8424 -- xref information is properly generated for the right entity.
8426 Preserve_Comes_From_Source (Obj_Decl, Original_Node (Obj_Decl));
8427 Preserve_Comes_From_Source (Obj_Def_Id, Original_Node (Obj_Decl));
8429 Set_Comes_From_Source (Ren_Id, False);
8430 end;
8431 end if;
8433 -- If the object entity has a class-wide Etype, then we need to change
8434 -- it to the result subtype of the function call, because otherwise the
8435 -- object will be class-wide without an explicit initialization and
8436 -- won't be allocated properly by the back end. It seems unclean to make
8437 -- such a revision to the type at this point, and we should try to
8438 -- improve this treatment when build-in-place functions with class-wide
8439 -- results are implemented. ???
8441 if Is_Class_Wide_Type (Etype (Defining_Identifier (Obj_Decl))) then
8442 Set_Etype (Defining_Identifier (Obj_Decl), Result_Subt);
8443 end if;
8444 end Make_Build_In_Place_Call_In_Object_Declaration;
8446 --------------------------------------------
8447 -- Make_CPP_Constructor_Call_In_Allocator --
8448 --------------------------------------------
8450 procedure Make_CPP_Constructor_Call_In_Allocator
8451 (Allocator : Node_Id;
8452 Function_Call : Node_Id)
8454 Loc : constant Source_Ptr := Sloc (Function_Call);
8455 Acc_Type : constant Entity_Id := Etype (Allocator);
8456 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
8457 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
8459 New_Allocator : Node_Id;
8460 Return_Obj_Access : Entity_Id;
8461 Tmp_Obj : Node_Id;
8463 begin
8464 pragma Assert (Nkind (Allocator) = N_Allocator
8465 and then Nkind (Function_Call) = N_Function_Call);
8466 pragma Assert (Convention (Function_Id) = Convention_CPP
8467 and then Is_Constructor (Function_Id));
8468 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
8470 -- Replace the initialized allocator of form "new T'(Func (...))" with
8471 -- an uninitialized allocator of form "new T", where T is the result
8472 -- subtype of the called function. The call to the function is handled
8473 -- separately further below.
8475 New_Allocator :=
8476 Make_Allocator (Loc,
8477 Expression => New_Occurrence_Of (Result_Subt, Loc));
8478 Set_No_Initialization (New_Allocator);
8480 -- Copy attributes to new allocator. Note that the new allocator
8481 -- logically comes from source if the original one did, so copy the
8482 -- relevant flag. This ensures proper treatment of the restriction
8483 -- No_Implicit_Heap_Allocations in this case.
8485 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8486 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8487 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8489 Rewrite (Allocator, New_Allocator);
8491 -- Create a new access object and initialize it to the result of the
8492 -- new uninitialized allocator. Note: we do not use Allocator as the
8493 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
8494 -- as this would create a sort of infinite "recursion".
8496 Return_Obj_Access := Make_Temporary (Loc, 'R');
8497 Set_Etype (Return_Obj_Access, Acc_Type);
8499 -- Generate:
8500 -- Rnnn : constant ptr_T := new (T);
8501 -- Init (Rnn.all,...);
8503 Tmp_Obj :=
8504 Make_Object_Declaration (Loc,
8505 Defining_Identifier => Return_Obj_Access,
8506 Constant_Present => True,
8507 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8508 Expression => Relocate_Node (Allocator));
8509 Insert_Action (Allocator, Tmp_Obj);
8511 Insert_List_After_And_Analyze (Tmp_Obj,
8512 Build_Initialization_Call (Loc,
8513 Id_Ref =>
8514 Make_Explicit_Dereference (Loc,
8515 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
8516 Typ => Etype (Function_Id),
8517 Constructor_Ref => Function_Call));
8519 -- Finally, replace the allocator node with a reference to the result of
8520 -- the function call itself (which will effectively be an access to the
8521 -- object created by the allocator).
8523 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8525 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
8526 -- generate an implicit conversion to force displacement of the "this"
8527 -- pointer.
8529 if Is_Interface (Designated_Type (Acc_Type)) then
8530 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
8531 end if;
8533 Analyze_And_Resolve (Allocator, Acc_Type);
8534 end Make_CPP_Constructor_Call_In_Allocator;
8536 -----------------------------------
8537 -- Needs_BIP_Finalization_Master --
8538 -----------------------------------
8540 function Needs_BIP_Finalization_Master
8541 (Func_Id : Entity_Id) return Boolean
8543 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8544 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8545 begin
8546 -- A formal giving the finalization master is needed for build-in-place
8547 -- functions whose result type needs finalization or is a tagged type.
8548 -- Tagged primitive build-in-place functions need such a formal because
8549 -- they can be called by a dispatching call, and extensions may require
8550 -- finalization even if the root type doesn't. This means they're also
8551 -- needed for tagged nonprimitive build-in-place functions with tagged
8552 -- results, since such functions can be called via access-to-function
8553 -- types, and those can be used to call primitives, so masters have to
8554 -- be passed to all such build-in-place functions, primitive or not.
8556 return
8557 not Restriction_Active (No_Finalization)
8558 and then (Needs_Finalization (Func_Typ)
8559 or else Is_Tagged_Type (Func_Typ));
8560 end Needs_BIP_Finalization_Master;
8562 --------------------------
8563 -- Needs_BIP_Alloc_Form --
8564 --------------------------
8566 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
8567 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8568 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8569 begin
8570 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
8571 end Needs_BIP_Alloc_Form;
8573 --------------------------------------
8574 -- Needs_Result_Accessibility_Level --
8575 --------------------------------------
8577 function Needs_Result_Accessibility_Level
8578 (Func_Id : Entity_Id) return Boolean
8580 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8582 function Has_Unconstrained_Access_Discriminant_Component
8583 (Comp_Typ : Entity_Id) return Boolean;
8584 -- Returns True if any component of the type has an unconstrained access
8585 -- discriminant.
8587 -----------------------------------------------------
8588 -- Has_Unconstrained_Access_Discriminant_Component --
8589 -----------------------------------------------------
8591 function Has_Unconstrained_Access_Discriminant_Component
8592 (Comp_Typ : Entity_Id) return Boolean
8594 begin
8595 if not Is_Limited_Type (Comp_Typ) then
8596 return False;
8598 -- Only limited types can have access discriminants with
8599 -- defaults.
8601 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
8602 return True;
8604 elsif Is_Array_Type (Comp_Typ) then
8605 return Has_Unconstrained_Access_Discriminant_Component
8606 (Underlying_Type (Component_Type (Comp_Typ)));
8608 elsif Is_Record_Type (Comp_Typ) then
8609 declare
8610 Comp : Entity_Id;
8612 begin
8613 Comp := First_Component (Comp_Typ);
8614 while Present (Comp) loop
8615 if Has_Unconstrained_Access_Discriminant_Component
8616 (Underlying_Type (Etype (Comp)))
8617 then
8618 return True;
8619 end if;
8621 Next_Component (Comp);
8622 end loop;
8623 end;
8624 end if;
8626 return False;
8627 end Has_Unconstrained_Access_Discriminant_Component;
8629 Feature_Disabled : constant Boolean := True;
8630 -- Temporary
8632 -- Start of processing for Needs_Result_Accessibility_Level
8634 begin
8635 -- False if completion unavailable (how does this happen???)
8637 if not Present (Func_Typ) then
8638 return False;
8640 elsif Feature_Disabled then
8641 return False;
8643 -- False if not a function, also handle enum-lit renames case
8645 elsif Func_Typ = Standard_Void_Type
8646 or else Is_Scalar_Type (Func_Typ)
8647 then
8648 return False;
8650 -- Handle a corner case, a cross-dialect subp renaming. For example,
8651 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
8652 -- an Ada 2005 (or earlier) unit references predefined run-time units.
8654 elsif Present (Alias (Func_Id)) then
8656 -- Unimplemented: a cross-dialect subp renaming which does not set
8657 -- the Alias attribute (e.g., a rename of a dereference of an access
8658 -- to subprogram value). ???
8660 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
8662 -- Remaining cases require Ada 2012 mode
8664 elsif Ada_Version < Ada_2012 then
8665 return False;
8667 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
8668 or else Is_Tagged_Type (Func_Typ)
8669 then
8670 -- In the case of, say, a null tagged record result type, the need
8671 -- for this extra parameter might not be obvious. This function
8672 -- returns True for all tagged types for compatibility reasons.
8673 -- A function with, say, a tagged null controlling result type might
8674 -- be overridden by a primitive of an extension having an access
8675 -- discriminant and the overrider and overridden must have compatible
8676 -- calling conventions (including implicitly declared parameters).
8677 -- Similarly, values of one access-to-subprogram type might designate
8678 -- both a primitive subprogram of a given type and a function
8679 -- which is, for example, not a primitive subprogram of any type.
8680 -- Again, this requires calling convention compatibility.
8681 -- It might be possible to solve these issues by introducing
8682 -- wrappers, but that is not the approach that was chosen.
8684 return True;
8686 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
8687 return True;
8689 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
8690 return True;
8692 -- False for all other cases
8694 else
8695 return False;
8696 end if;
8697 end Needs_Result_Accessibility_Level;
8699 ---------------------------------
8700 -- Rewrite_Function_Call_For_C --
8701 ---------------------------------
8703 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
8704 Orig_Func : constant Entity_Id := Entity (Name (N));
8705 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
8706 Par : constant Node_Id := Parent (N);
8707 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
8708 Loc : constant Source_Ptr := Sloc (Par);
8709 Actuals : List_Id;
8710 Last_Actual : Node_Id;
8711 Last_Formal : Entity_Id;
8713 -- Start of processing for Rewrite_Function_Call_For_C
8715 begin
8716 -- The actuals may be given by named associations, so the added actual
8717 -- that is the target of the return value of the call must be a named
8718 -- association as well, so we retrieve the name of the generated
8719 -- out_formal.
8721 Last_Formal := First_Formal (Proc_Id);
8722 while Present (Next_Formal (Last_Formal)) loop
8723 Last_Formal := Next_Formal (Last_Formal);
8724 end loop;
8726 Actuals := Parameter_Associations (N);
8728 -- The original function may lack parameters
8730 if No (Actuals) then
8731 Actuals := New_List;
8732 end if;
8734 -- If the function call is the expression of an assignment statement,
8735 -- transform the assignment into a procedure call. Generate:
8737 -- LHS := Func_Call (...);
8739 -- Proc_Call (..., LHS);
8741 -- If function is inherited, a conversion may be necessary.
8743 if Nkind (Par) = N_Assignment_Statement then
8744 Last_Actual := Name (Par);
8746 if not Comes_From_Source (Orig_Func)
8747 and then Etype (Orig_Func) /= Etype (Func_Id)
8748 then
8749 Last_Actual :=
8750 Make_Type_Conversion (Loc,
8751 New_Occurrence_Of (Etype (Func_Id), Loc),
8752 Last_Actual);
8753 end if;
8755 Append_To (Actuals,
8756 Make_Parameter_Association (Loc,
8757 Selector_Name =>
8758 Make_Identifier (Loc, Chars (Last_Formal)),
8759 Explicit_Actual_Parameter => Last_Actual));
8761 Rewrite (Par,
8762 Make_Procedure_Call_Statement (Loc,
8763 Name => New_Occurrence_Of (Proc_Id, Loc),
8764 Parameter_Associations => Actuals));
8765 Analyze (Par);
8767 -- Otherwise the context is an expression. Generate a temporary and a
8768 -- procedure call to obtain the function result. Generate:
8770 -- ... Func_Call (...) ...
8772 -- Temp : ...;
8773 -- Proc_Call (..., Temp);
8774 -- ... Temp ...
8776 else
8777 declare
8778 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
8779 Call : Node_Id;
8780 Decl : Node_Id;
8782 begin
8783 -- Generate:
8784 -- Temp : ...;
8786 Decl :=
8787 Make_Object_Declaration (Loc,
8788 Defining_Identifier => Temp_Id,
8789 Object_Definition =>
8790 New_Occurrence_Of (Etype (Func_Id), Loc));
8792 -- Generate:
8793 -- Proc_Call (..., Temp);
8795 Append_To (Actuals,
8796 Make_Parameter_Association (Loc,
8797 Selector_Name =>
8798 Make_Identifier (Loc, Chars (Last_Formal)),
8799 Explicit_Actual_Parameter =>
8800 New_Occurrence_Of (Temp_Id, Loc)));
8802 Call :=
8803 Make_Procedure_Call_Statement (Loc,
8804 Name => New_Occurrence_Of (Proc_Id, Loc),
8805 Parameter_Associations => Actuals);
8807 Insert_Actions (Par, New_List (Decl, Call));
8808 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
8809 end;
8810 end if;
8811 end Rewrite_Function_Call_For_C;
8813 ------------------------------------
8814 -- Set_Enclosing_Sec_Stack_Return --
8815 ------------------------------------
8817 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
8818 P : Node_Id := N;
8820 begin
8821 -- Due to a possible mix of internally generated blocks, source blocks
8822 -- and loops, the scope stack may not be contiguous as all labels are
8823 -- inserted at the top level within the related function. Instead,
8824 -- perform a parent-based traversal and mark all appropriate constructs.
8826 while Present (P) loop
8828 -- Mark the label of a source or internally generated block or
8829 -- loop.
8831 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
8832 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
8834 -- Mark the enclosing function
8836 elsif Nkind (P) = N_Subprogram_Body then
8837 if Present (Corresponding_Spec (P)) then
8838 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
8839 else
8840 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
8841 end if;
8843 -- Do not go beyond the enclosing function
8845 exit;
8846 end if;
8848 P := Parent (P);
8849 end loop;
8850 end Set_Enclosing_Sec_Stack_Return;
8852 end Exp_Ch6;