PR sanitizer/80403
[official-gcc.git] / gcc / ada / exp_ch6.adb
blobe9f13319ed51505464a7c959f517245cc6f61194
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-2016, 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 (N : in out Node_Id; Subp : Entity_Id);
162 -- For each actual of an in-out or out parameter which is a numeric
163 -- (view) conversion of the form T (A), where A denotes a variable,
164 -- we insert the declaration:
166 -- Temp : T[ := T (A)];
168 -- prior to the call. Then we replace the actual with a reference to Temp,
169 -- and append the assignment:
171 -- A := TypeA (Temp);
173 -- after the call. Here TypeA is the actual type of variable A. For out
174 -- parameters, the initial declaration has no expression. If A is not an
175 -- entity name, we generate instead:
177 -- Var : TypeA renames A;
178 -- Temp : T := Var; -- omitting expression for out parameter.
179 -- ...
180 -- Var := TypeA (Temp);
182 -- For other in-out parameters, we emit the required constraint checks
183 -- before and/or after the call.
185 -- For all parameter modes, actuals that denote components and slices of
186 -- packed arrays are expanded into suitable temporaries.
188 -- For non-scalar objects that are possibly unaligned, add call by copy
189 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
191 -- For OUT and IN OUT parameters, add predicate checks after the call
192 -- based on the predicates of the actual type.
194 -- The parameter N is IN OUT because in some cases, the expansion code
195 -- rewrites the call as an expression actions with the call inside. In
196 -- this case N is reset to point to the inside call so that the caller
197 -- can continue processing of this call.
199 procedure Expand_Ctrl_Function_Call (N : Node_Id);
200 -- N is a function call which returns a controlled object. Transform the
201 -- call into a temporary which retrieves the returned object from the
202 -- secondary stack using 'reference.
204 procedure Expand_Non_Function_Return (N : Node_Id);
205 -- Expand a simple return statement found in a procedure body, entry body,
206 -- accept statement, or an extended return statement. Note that all non-
207 -- function returns are simple return statements.
209 function Expand_Protected_Object_Reference
210 (N : Node_Id;
211 Scop : Entity_Id) return Node_Id;
213 procedure Expand_Protected_Subprogram_Call
214 (N : Node_Id;
215 Subp : Entity_Id;
216 Scop : Entity_Id);
217 -- A call to a protected subprogram within the protected object may appear
218 -- as a regular call. The list of actuals must be expanded to contain a
219 -- reference to the object itself, and the call becomes a call to the
220 -- corresponding protected subprogram.
222 procedure Expand_Simple_Function_Return (N : Node_Id);
223 -- Expand simple return from function. In the case where we are returning
224 -- from a function body this is called by Expand_N_Simple_Return_Statement.
226 function Has_Unconstrained_Access_Discriminants
227 (Subtyp : Entity_Id) return Boolean;
228 -- Returns True if the given subtype is unconstrained and has one or more
229 -- access discriminants.
231 procedure Rewrite_Function_Call_For_C (N : Node_Id);
232 -- When generating C code, replace a call to a function that returns an
233 -- array into the generated procedure with an additional out parameter.
235 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id);
236 -- N is a return statement for a function that returns its result on the
237 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
238 -- function and all blocks and loops that the return statement is jumping
239 -- out of. This ensures that the secondary stack is not released; otherwise
240 -- the function result would be reclaimed before returning to the caller.
242 ----------------------------------------------
243 -- Add_Access_Actual_To_Build_In_Place_Call --
244 ----------------------------------------------
246 procedure Add_Access_Actual_To_Build_In_Place_Call
247 (Function_Call : Node_Id;
248 Function_Id : Entity_Id;
249 Return_Object : Node_Id;
250 Is_Access : Boolean := False)
252 Loc : constant Source_Ptr := Sloc (Function_Call);
253 Obj_Address : Node_Id;
254 Obj_Acc_Formal : Entity_Id;
256 begin
257 -- Locate the implicit access parameter in the called function
259 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
261 -- If no return object is provided, then pass null
263 if not Present (Return_Object) then
264 Obj_Address := Make_Null (Loc);
265 Set_Parent (Obj_Address, Function_Call);
267 -- If Return_Object is already an expression of an access type, then use
268 -- it directly, since it must be an access value denoting the return
269 -- object, and couldn't possibly be the return object itself.
271 elsif Is_Access then
272 Obj_Address := Return_Object;
273 Set_Parent (Obj_Address, Function_Call);
275 -- Apply Unrestricted_Access to caller's return object
277 else
278 Obj_Address :=
279 Make_Attribute_Reference (Loc,
280 Prefix => Return_Object,
281 Attribute_Name => Name_Unrestricted_Access);
283 Set_Parent (Return_Object, Obj_Address);
284 Set_Parent (Obj_Address, Function_Call);
285 end if;
287 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
289 -- Build the parameter association for the new actual and add it to the
290 -- end of the function's actuals.
292 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
293 end Add_Access_Actual_To_Build_In_Place_Call;
295 ------------------------------------------------------
296 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
297 ------------------------------------------------------
299 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
300 (Function_Call : Node_Id;
301 Function_Id : Entity_Id;
302 Alloc_Form : BIP_Allocation_Form := Unspecified;
303 Alloc_Form_Exp : Node_Id := Empty;
304 Pool_Actual : Node_Id := Make_Null (No_Location))
306 Loc : constant Source_Ptr := Sloc (Function_Call);
307 Alloc_Form_Actual : Node_Id;
308 Alloc_Form_Formal : Node_Id;
309 Pool_Formal : Node_Id;
311 begin
312 -- The allocation form generally doesn't need to be passed in the case
313 -- of a constrained result subtype, since normally the caller performs
314 -- the allocation in that case. However this formal is still needed in
315 -- the case where the function has a tagged result, because generally
316 -- such functions can be called in a dispatching context and such calls
317 -- must be handled like calls to class-wide functions.
319 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
320 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
321 then
322 return;
323 end if;
325 -- Locate the implicit allocation form parameter in the called function.
326 -- Maybe it would be better for each implicit formal of a build-in-place
327 -- function to have a flag or a Uint attribute to identify it. ???
329 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
331 if Present (Alloc_Form_Exp) then
332 pragma Assert (Alloc_Form = Unspecified);
334 Alloc_Form_Actual := Alloc_Form_Exp;
336 else
337 pragma Assert (Alloc_Form /= Unspecified);
339 Alloc_Form_Actual :=
340 Make_Integer_Literal (Loc,
341 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
342 end if;
344 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
346 -- Build the parameter association for the new actual and add it to the
347 -- end of the function's actuals.
349 Add_Extra_Actual_To_Call
350 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
352 -- Pass the Storage_Pool parameter. This parameter is omitted on
353 -- ZFP as those targets do not support pools.
355 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
356 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
357 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
358 Add_Extra_Actual_To_Call
359 (Function_Call, Pool_Formal, Pool_Actual);
360 end if;
361 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
363 -----------------------------------------------------------
364 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
365 -----------------------------------------------------------
367 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
368 (Func_Call : Node_Id;
369 Func_Id : Entity_Id;
370 Ptr_Typ : Entity_Id := Empty;
371 Master_Exp : Node_Id := Empty)
373 begin
374 if not Needs_BIP_Finalization_Master (Func_Id) then
375 return;
376 end if;
378 declare
379 Formal : constant Entity_Id :=
380 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
381 Loc : constant Source_Ptr := Sloc (Func_Call);
383 Actual : Node_Id;
384 Desig_Typ : Entity_Id;
386 begin
387 -- If there is a finalization master actual, such as the implicit
388 -- finalization master of an enclosing build-in-place function,
389 -- then this must be added as an extra actual of the call.
391 if Present (Master_Exp) then
392 Actual := Master_Exp;
394 -- Case where the context does not require an actual master
396 elsif No (Ptr_Typ) then
397 Actual := Make_Null (Loc);
399 else
400 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
402 -- Check for a library-level access type whose designated type has
403 -- supressed finalization. Such an access types lack a master.
404 -- Pass a null actual to the callee in order to signal a missing
405 -- master.
407 if Is_Library_Level_Entity (Ptr_Typ)
408 and then Finalize_Storage_Only (Desig_Typ)
409 then
410 Actual := Make_Null (Loc);
412 -- Types in need of finalization actions
414 elsif Needs_Finalization (Desig_Typ) then
416 -- The general mechanism of creating finalization masters for
417 -- anonymous access types is disabled by default, otherwise
418 -- finalization masters will pop all over the place. Such types
419 -- use context-specific masters.
421 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
422 and then No (Finalization_Master (Ptr_Typ))
423 then
424 Build_Anonymous_Master (Ptr_Typ);
425 end if;
427 -- Access-to-controlled types should always have a master
429 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
431 Actual :=
432 Make_Attribute_Reference (Loc,
433 Prefix =>
434 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
435 Attribute_Name => Name_Unrestricted_Access);
437 -- Tagged types
439 else
440 Actual := Make_Null (Loc);
441 end if;
442 end if;
444 Analyze_And_Resolve (Actual, Etype (Formal));
446 -- Build the parameter association for the new actual and add it to
447 -- the end of the function's actuals.
449 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
450 end;
451 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
453 ------------------------------
454 -- Add_Extra_Actual_To_Call --
455 ------------------------------
457 procedure Add_Extra_Actual_To_Call
458 (Subprogram_Call : Node_Id;
459 Extra_Formal : Entity_Id;
460 Extra_Actual : Node_Id)
462 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
463 Param_Assoc : Node_Id;
465 begin
466 Param_Assoc :=
467 Make_Parameter_Association (Loc,
468 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
469 Explicit_Actual_Parameter => Extra_Actual);
471 Set_Parent (Param_Assoc, Subprogram_Call);
472 Set_Parent (Extra_Actual, Param_Assoc);
474 if Present (Parameter_Associations (Subprogram_Call)) then
475 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
476 N_Parameter_Association
477 then
479 -- Find last named actual, and append
481 declare
482 L : Node_Id;
483 begin
484 L := First_Actual (Subprogram_Call);
485 while Present (L) loop
486 if No (Next_Actual (L)) then
487 Set_Next_Named_Actual (Parent (L), Extra_Actual);
488 exit;
489 end if;
490 Next_Actual (L);
491 end loop;
492 end;
494 else
495 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
496 end if;
498 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
500 else
501 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
502 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
503 end if;
504 end Add_Extra_Actual_To_Call;
506 ---------------------------------------------
507 -- Add_Task_Actuals_To_Build_In_Place_Call --
508 ---------------------------------------------
510 procedure Add_Task_Actuals_To_Build_In_Place_Call
511 (Function_Call : Node_Id;
512 Function_Id : Entity_Id;
513 Master_Actual : Node_Id;
514 Chain : Node_Id := Empty)
516 Loc : constant Source_Ptr := Sloc (Function_Call);
517 Result_Subt : constant Entity_Id :=
518 Available_View (Etype (Function_Id));
519 Actual : Node_Id;
520 Chain_Actual : Node_Id;
521 Chain_Formal : Node_Id;
522 Master_Formal : Node_Id;
524 begin
525 -- No such extra parameters are needed if there are no tasks
527 if not Has_Task (Result_Subt) then
528 return;
529 end if;
531 Actual := Master_Actual;
533 -- Use a dummy _master actual in case of No_Task_Hierarchy
535 if Restriction_Active (No_Task_Hierarchy) then
536 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
538 -- In the case where we use the master associated with an access type,
539 -- the actual is an entity and requires an explicit reference.
541 elsif Nkind (Actual) = N_Defining_Identifier then
542 Actual := New_Occurrence_Of (Actual, Loc);
543 end if;
545 -- Locate the implicit master parameter in the called function
547 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
548 Analyze_And_Resolve (Actual, Etype (Master_Formal));
550 -- Build the parameter association for the new actual and add it to the
551 -- end of the function's actuals.
553 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
555 -- Locate the implicit activation chain parameter in the called function
557 Chain_Formal :=
558 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
560 -- Create the actual which is a pointer to the current activation chain
562 if No (Chain) then
563 Chain_Actual :=
564 Make_Attribute_Reference (Loc,
565 Prefix => Make_Identifier (Loc, Name_uChain),
566 Attribute_Name => Name_Unrestricted_Access);
568 -- Allocator case; make a reference to the Chain passed in by the caller
570 else
571 Chain_Actual :=
572 Make_Attribute_Reference (Loc,
573 Prefix => New_Occurrence_Of (Chain, Loc),
574 Attribute_Name => Name_Unrestricted_Access);
575 end if;
577 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
579 -- Build the parameter association for the new actual and add it to the
580 -- end of the function's actuals.
582 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
583 end Add_Task_Actuals_To_Build_In_Place_Call;
585 -----------------------
586 -- BIP_Formal_Suffix --
587 -----------------------
589 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
590 begin
591 case Kind is
592 when BIP_Alloc_Form =>
593 return "BIPalloc";
595 when BIP_Storage_Pool =>
596 return "BIPstoragepool";
598 when BIP_Finalization_Master =>
599 return "BIPfinalizationmaster";
601 when BIP_Task_Master =>
602 return "BIPtaskmaster";
604 when BIP_Activation_Chain =>
605 return "BIPactivationchain";
607 when BIP_Object_Access =>
608 return "BIPaccess";
609 end case;
610 end BIP_Formal_Suffix;
612 ---------------------------
613 -- Build_In_Place_Formal --
614 ---------------------------
616 function Build_In_Place_Formal
617 (Func : Entity_Id;
618 Kind : BIP_Formal_Kind) return Entity_Id
620 Formal_Name : constant Name_Id :=
621 New_External_Name
622 (Chars (Func), BIP_Formal_Suffix (Kind));
623 Extra_Formal : Entity_Id := Extra_Formals (Func);
625 begin
626 -- Maybe it would be better for each implicit formal of a build-in-place
627 -- function to have a flag or a Uint attribute to identify it. ???
629 -- The return type in the function declaration may have been a limited
630 -- view, and the extra formals for the function were not generated at
631 -- that point. At the point of call the full view must be available and
632 -- the extra formals can be created.
634 if No (Extra_Formal) then
635 Create_Extra_Formals (Func);
636 Extra_Formal := Extra_Formals (Func);
637 end if;
639 loop
640 pragma Assert (Present (Extra_Formal));
641 exit when Chars (Extra_Formal) = Formal_Name;
643 Next_Formal_With_Extras (Extra_Formal);
644 end loop;
646 return Extra_Formal;
647 end Build_In_Place_Formal;
649 -------------------------------
650 -- Build_Procedure_Body_Form --
651 -------------------------------
653 function Build_Procedure_Body_Form
654 (Func_Id : Entity_Id;
655 Func_Body : Node_Id) return Node_Id
657 Loc : constant Source_Ptr := Sloc (Func_Body);
659 Proc_Decl : constant Node_Id :=
660 Next (Unit_Declaration_Node (Func_Id));
661 -- It is assumed that the next node following the declaration of the
662 -- corresponding subprogram spec is the declaration of the procedure
663 -- form.
665 Proc_Id : constant Entity_Id := Defining_Entity (Proc_Decl);
667 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id);
668 -- Replace each return statement found in the list Stmts with an
669 -- assignment of the return expression to parameter Param_Id.
671 ---------------------
672 -- Replace_Returns --
673 ---------------------
675 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id) is
676 Stmt : Node_Id;
678 begin
679 Stmt := First (Stmts);
680 while Present (Stmt) loop
681 if Nkind (Stmt) = N_Block_Statement then
682 Replace_Returns (Param_Id, Statements (Stmt));
684 elsif Nkind (Stmt) = N_Case_Statement then
685 declare
686 Alt : Node_Id;
687 begin
688 Alt := First (Alternatives (Stmt));
689 while Present (Alt) loop
690 Replace_Returns (Param_Id, Statements (Alt));
691 Next (Alt);
692 end loop;
693 end;
695 elsif Nkind (Stmt) = N_Extended_Return_Statement then
696 declare
697 Ret_Obj : constant Entity_Id :=
698 Defining_Entity
699 (First (Return_Object_Declarations (Stmt)));
700 Assign : constant Node_Id :=
701 Make_Assignment_Statement (Sloc (Stmt),
702 Name =>
703 New_Occurrence_Of (Param_Id, Loc),
704 Expression =>
705 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
706 Stmts : List_Id;
708 begin
709 -- The extended return may just contain the declaration
711 if Present (Handled_Statement_Sequence (Stmt)) then
712 Stmts := Statements (Handled_Statement_Sequence (Stmt));
713 else
714 Stmts := New_List;
715 end if;
717 Set_Assignment_OK (Name (Assign));
719 Rewrite (Stmt,
720 Make_Block_Statement (Sloc (Stmt),
721 Declarations =>
722 Return_Object_Declarations (Stmt),
723 Handled_Statement_Sequence =>
724 Make_Handled_Sequence_Of_Statements (Loc,
725 Statements => Stmts)));
727 Replace_Returns (Param_Id, Stmts);
729 Append_To (Stmts, Assign);
730 Append_To (Stmts, Make_Simple_Return_Statement (Loc));
731 end;
733 elsif Nkind (Stmt) = N_If_Statement then
734 Replace_Returns (Param_Id, Then_Statements (Stmt));
735 Replace_Returns (Param_Id, Else_Statements (Stmt));
737 declare
738 Part : Node_Id;
739 begin
740 Part := First (Elsif_Parts (Stmt));
741 while Present (Part) loop
742 Replace_Returns (Param_Id, Then_Statements (Part));
743 Next (Part);
744 end loop;
745 end;
747 elsif Nkind (Stmt) = N_Loop_Statement then
748 Replace_Returns (Param_Id, Statements (Stmt));
750 elsif Nkind (Stmt) = N_Simple_Return_Statement then
752 -- Generate:
753 -- Param := Expr;
754 -- return;
756 Rewrite (Stmt,
757 Make_Assignment_Statement (Sloc (Stmt),
758 Name => New_Occurrence_Of (Param_Id, Loc),
759 Expression => Relocate_Node (Expression (Stmt))));
761 Insert_After (Stmt, Make_Simple_Return_Statement (Loc));
763 -- Skip the added return
765 Next (Stmt);
766 end if;
768 Next (Stmt);
769 end loop;
770 end Replace_Returns;
772 -- Local variables
774 Stmts : List_Id;
775 New_Body : Node_Id;
777 -- Start of processing for Build_Procedure_Body_Form
779 begin
780 -- This routine replaces the original function body:
782 -- function F (...) return Array_Typ is
783 -- begin
784 -- ...
785 -- return Something;
786 -- end F;
788 -- with the following:
790 -- procedure P (..., Result : out Array_Typ) is
791 -- begin
792 -- ...
793 -- Result := Something;
794 -- end P;
796 Stmts :=
797 Statements (Handled_Statement_Sequence (Func_Body));
798 Replace_Returns (Last_Entity (Proc_Id), Stmts);
800 New_Body :=
801 Make_Subprogram_Body (Loc,
802 Specification =>
803 Copy_Subprogram_Spec (Specification (Proc_Decl)),
804 Declarations => Declarations (Func_Body),
805 Handled_Statement_Sequence =>
806 Make_Handled_Sequence_Of_Statements (Loc,
807 Statements => Stmts));
809 -- If the function is a generic instance, so is the new procedure.
810 -- Set flag accordingly so that the proper renaming declarations are
811 -- generated.
813 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
814 return New_Body;
815 end Build_Procedure_Body_Form;
817 --------------------------------
818 -- Check_Overriding_Operation --
819 --------------------------------
821 procedure Check_Overriding_Operation (Subp : Entity_Id) is
822 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
823 Op_List : constant Elist_Id := Primitive_Operations (Typ);
824 Op_Elmt : Elmt_Id;
825 Prim_Op : Entity_Id;
826 Par_Op : Entity_Id;
828 begin
829 if Is_Derived_Type (Typ)
830 and then not Is_Private_Type (Typ)
831 and then In_Open_Scopes (Scope (Etype (Typ)))
832 and then Is_Base_Type (Typ)
833 then
834 -- Subp overrides an inherited private operation if there is an
835 -- inherited operation with a different name than Subp (see
836 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
837 -- same name as Subp.
839 Op_Elmt := First_Elmt (Op_List);
840 while Present (Op_Elmt) loop
841 Prim_Op := Node (Op_Elmt);
842 Par_Op := Alias (Prim_Op);
844 if Present (Par_Op)
845 and then not Comes_From_Source (Prim_Op)
846 and then Chars (Prim_Op) /= Chars (Par_Op)
847 and then Chars (Par_Op) = Chars (Subp)
848 and then Is_Hidden (Par_Op)
849 and then Type_Conformant (Prim_Op, Subp)
850 then
851 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
852 end if;
854 Next_Elmt (Op_Elmt);
855 end loop;
856 end if;
857 end Check_Overriding_Operation;
859 -------------------------------
860 -- Detect_Infinite_Recursion --
861 -------------------------------
863 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
864 Loc : constant Source_Ptr := Sloc (N);
866 Var_List : constant Elist_Id := New_Elmt_List;
867 -- List of globals referenced by body of procedure
869 Call_List : constant Elist_Id := New_Elmt_List;
870 -- List of recursive calls in body of procedure
872 Shad_List : constant Elist_Id := New_Elmt_List;
873 -- List of entity id's for entities created to capture the value of
874 -- referenced globals on entry to the procedure.
876 Scop : constant Uint := Scope_Depth (Spec);
877 -- This is used to record the scope depth of the current procedure, so
878 -- that we can identify global references.
880 Max_Vars : constant := 4;
881 -- Do not test more than four global variables
883 Count_Vars : Natural := 0;
884 -- Count variables found so far
886 Var : Entity_Id;
887 Elm : Elmt_Id;
888 Ent : Entity_Id;
889 Call : Elmt_Id;
890 Decl : Node_Id;
891 Test : Node_Id;
892 Elm1 : Elmt_Id;
893 Elm2 : Elmt_Id;
894 Last : Node_Id;
896 function Process (Nod : Node_Id) return Traverse_Result;
897 -- Function to traverse the subprogram body (using Traverse_Func)
899 -------------
900 -- Process --
901 -------------
903 function Process (Nod : Node_Id) return Traverse_Result is
904 begin
905 -- Procedure call
907 if Nkind (Nod) = N_Procedure_Call_Statement then
909 -- Case of one of the detected recursive calls
911 if Is_Entity_Name (Name (Nod))
912 and then Has_Recursive_Call (Entity (Name (Nod)))
913 and then Entity (Name (Nod)) = Spec
914 then
915 Append_Elmt (Nod, Call_List);
916 return Skip;
918 -- Any other procedure call may have side effects
920 else
921 return Abandon;
922 end if;
924 -- A call to a pure function can always be ignored
926 elsif Nkind (Nod) = N_Function_Call
927 and then Is_Entity_Name (Name (Nod))
928 and then Is_Pure (Entity (Name (Nod)))
929 then
930 return Skip;
932 -- Case of an identifier reference
934 elsif Nkind (Nod) = N_Identifier then
935 Ent := Entity (Nod);
937 -- If no entity, then ignore the reference
939 -- Not clear why this can happen. To investigate, remove this
940 -- test and look at the crash that occurs here in 3401-004 ???
942 if No (Ent) then
943 return Skip;
945 -- Ignore entities with no Scope, again not clear how this
946 -- can happen, to investigate, look at 4108-008 ???
948 elsif No (Scope (Ent)) then
949 return Skip;
951 -- Ignore the reference if not to a more global object
953 elsif Scope_Depth (Scope (Ent)) >= Scop then
954 return Skip;
956 -- References to types, exceptions and constants are always OK
958 elsif Is_Type (Ent)
959 or else Ekind (Ent) = E_Exception
960 or else Ekind (Ent) = E_Constant
961 then
962 return Skip;
964 -- If other than a non-volatile scalar variable, we have some
965 -- kind of global reference (e.g. to a function) that we cannot
966 -- deal with so we forget the attempt.
968 elsif Ekind (Ent) /= E_Variable
969 or else not Is_Scalar_Type (Etype (Ent))
970 or else Treat_As_Volatile (Ent)
971 then
972 return Abandon;
974 -- Otherwise we have a reference to a global scalar
976 else
977 -- Loop through global entities already detected
979 Elm := First_Elmt (Var_List);
980 loop
981 -- If not detected before, record this new global reference
983 if No (Elm) then
984 Count_Vars := Count_Vars + 1;
986 if Count_Vars <= Max_Vars then
987 Append_Elmt (Entity (Nod), Var_List);
988 else
989 return Abandon;
990 end if;
992 exit;
994 -- If recorded before, ignore
996 elsif Node (Elm) = Entity (Nod) then
997 return Skip;
999 -- Otherwise keep looking
1001 else
1002 Next_Elmt (Elm);
1003 end if;
1004 end loop;
1006 return Skip;
1007 end if;
1009 -- For all other node kinds, recursively visit syntactic children
1011 else
1012 return OK;
1013 end if;
1014 end Process;
1016 function Traverse_Body is new Traverse_Func (Process);
1018 -- Start of processing for Detect_Infinite_Recursion
1020 begin
1021 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1022 -- won't be able to generate the code to handle the recursion in any
1023 -- case.
1025 if Restriction_Active (No_Implicit_Conditionals) then
1026 return;
1027 end if;
1029 -- Otherwise do traversal and quit if we get abandon signal
1031 if Traverse_Body (N) = Abandon then
1032 return;
1034 -- We must have a call, since Has_Recursive_Call was set. If not just
1035 -- ignore (this is only an error check, so if we have a funny situation,
1036 -- due to bugs or errors, we do not want to bomb).
1038 elsif Is_Empty_Elmt_List (Call_List) then
1039 return;
1040 end if;
1042 -- Here is the case where we detect recursion at compile time
1044 -- Push our current scope for analyzing the declarations and code that
1045 -- we will insert for the checking.
1047 Push_Scope (Spec);
1049 -- This loop builds temporary variables for each of the referenced
1050 -- globals, so that at the end of the loop the list Shad_List contains
1051 -- these temporaries in one-to-one correspondence with the elements in
1052 -- Var_List.
1054 Last := Empty;
1055 Elm := First_Elmt (Var_List);
1056 while Present (Elm) loop
1057 Var := Node (Elm);
1058 Ent := Make_Temporary (Loc, 'S');
1059 Append_Elmt (Ent, Shad_List);
1061 -- Insert a declaration for this temporary at the start of the
1062 -- declarations for the procedure. The temporaries are declared as
1063 -- constant objects initialized to the current values of the
1064 -- corresponding temporaries.
1066 Decl :=
1067 Make_Object_Declaration (Loc,
1068 Defining_Identifier => Ent,
1069 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
1070 Constant_Present => True,
1071 Expression => New_Occurrence_Of (Var, Loc));
1073 if No (Last) then
1074 Prepend (Decl, Declarations (N));
1075 else
1076 Insert_After (Last, Decl);
1077 end if;
1079 Last := Decl;
1080 Analyze (Decl);
1081 Next_Elmt (Elm);
1082 end loop;
1084 -- Loop through calls
1086 Call := First_Elmt (Call_List);
1087 while Present (Call) loop
1089 -- Build a predicate expression of the form
1091 -- True
1092 -- and then global1 = temp1
1093 -- and then global2 = temp2
1094 -- ...
1096 -- This predicate determines if any of the global values
1097 -- referenced by the procedure have changed since the
1098 -- current call, if not an infinite recursion is assured.
1100 Test := New_Occurrence_Of (Standard_True, Loc);
1102 Elm1 := First_Elmt (Var_List);
1103 Elm2 := First_Elmt (Shad_List);
1104 while Present (Elm1) loop
1105 Test :=
1106 Make_And_Then (Loc,
1107 Left_Opnd => Test,
1108 Right_Opnd =>
1109 Make_Op_Eq (Loc,
1110 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1111 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1113 Next_Elmt (Elm1);
1114 Next_Elmt (Elm2);
1115 end loop;
1117 -- Now we replace the call with the sequence
1119 -- if no-changes (see above) then
1120 -- raise Storage_Error;
1121 -- else
1122 -- original-call
1123 -- end if;
1125 Rewrite (Node (Call),
1126 Make_If_Statement (Loc,
1127 Condition => Test,
1128 Then_Statements => New_List (
1129 Make_Raise_Storage_Error (Loc,
1130 Reason => SE_Infinite_Recursion)),
1132 Else_Statements => New_List (
1133 Relocate_Node (Node (Call)))));
1135 Analyze (Node (Call));
1137 Next_Elmt (Call);
1138 end loop;
1140 -- Remove temporary scope stack entry used for analysis
1142 Pop_Scope;
1143 end Detect_Infinite_Recursion;
1145 --------------------
1146 -- Expand_Actuals --
1147 --------------------
1149 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id) is
1150 Loc : constant Source_Ptr := Sloc (N);
1151 Actual : Node_Id;
1152 Formal : Entity_Id;
1153 N_Node : Node_Id;
1154 Post_Call : List_Id;
1155 E_Actual : Entity_Id;
1156 E_Formal : Entity_Id;
1158 procedure Add_Call_By_Copy_Code;
1159 -- For cases where the parameter must be passed by copy, this routine
1160 -- generates a temporary variable into which the actual is copied and
1161 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1162 -- an assignment is also generated to copy the result back. The call
1163 -- also takes care of any constraint checks required for the type
1164 -- conversion case (on both the way in and the way out).
1166 procedure Add_Simple_Call_By_Copy_Code;
1167 -- This is similar to the above, but is used in cases where we know
1168 -- that all that is needed is to simply create a temporary and copy
1169 -- the value in and out of the temporary.
1171 procedure Check_Fortran_Logical;
1172 -- A value of type Logical that is passed through a formal parameter
1173 -- must be normalized because .TRUE. usually does not have the same
1174 -- representation as True. We assume that .FALSE. = False = 0.
1175 -- What about functions that return a logical type ???
1177 function Is_Legal_Copy return Boolean;
1178 -- Check that an actual can be copied before generating the temporary
1179 -- to be used in the call. If the actual is of a by_reference type then
1180 -- the program is illegal (this can only happen in the presence of
1181 -- rep. clauses that force an incorrect alignment). If the formal is
1182 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1183 -- the effect that this might lead to unaligned arguments.
1185 function Make_Var (Actual : Node_Id) return Entity_Id;
1186 -- Returns an entity that refers to the given actual parameter, Actual
1187 -- (not including any type conversion). If Actual is an entity name,
1188 -- then this entity is returned unchanged, otherwise a renaming is
1189 -- created to provide an entity for the actual.
1191 procedure Reset_Packed_Prefix;
1192 -- The expansion of a packed array component reference is delayed in
1193 -- the context of a call. Now we need to complete the expansion, so we
1194 -- unmark the analyzed bits in all prefixes.
1196 ---------------------------
1197 -- Add_Call_By_Copy_Code --
1198 ---------------------------
1200 procedure Add_Call_By_Copy_Code is
1201 Crep : Boolean;
1202 Expr : Node_Id;
1203 F_Typ : Entity_Id := Etype (Formal);
1204 Indic : Node_Id;
1205 Init : Node_Id;
1206 Temp : Entity_Id;
1207 V_Typ : Entity_Id;
1208 Var : Entity_Id;
1210 begin
1211 if not Is_Legal_Copy then
1212 return;
1213 end if;
1215 Temp := Make_Temporary (Loc, 'T', Actual);
1217 -- Handle formals whose type comes from the limited view
1219 if From_Limited_With (F_Typ)
1220 and then Has_Non_Limited_View (F_Typ)
1221 then
1222 F_Typ := Non_Limited_View (F_Typ);
1223 end if;
1225 -- Use formal type for temp, unless formal type is an unconstrained
1226 -- array, in which case we don't have to worry about bounds checks,
1227 -- and we use the actual type, since that has appropriate bounds.
1229 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1230 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1231 else
1232 Indic := New_Occurrence_Of (F_Typ, Loc);
1233 end if;
1235 if Nkind (Actual) = N_Type_Conversion then
1236 V_Typ := Etype (Expression (Actual));
1238 -- If the formal is an (in-)out parameter, capture the name
1239 -- of the variable in order to build the post-call assignment.
1241 Var := Make_Var (Expression (Actual));
1243 Crep := not Same_Representation
1244 (F_Typ, Etype (Expression (Actual)));
1246 else
1247 V_Typ := Etype (Actual);
1248 Var := Make_Var (Actual);
1249 Crep := False;
1250 end if;
1252 -- Setup initialization for case of in out parameter, or an out
1253 -- parameter where the formal is an unconstrained array (in the
1254 -- latter case, we have to pass in an object with bounds).
1256 -- If this is an out parameter, the initial copy is wasteful, so as
1257 -- an optimization for the one-dimensional case we extract the
1258 -- bounds of the actual and build an uninitialized temporary of the
1259 -- right size.
1261 if Ekind (Formal) = E_In_Out_Parameter
1262 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1263 then
1264 if Nkind (Actual) = N_Type_Conversion then
1265 if Conversion_OK (Actual) then
1266 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1267 else
1268 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1269 end if;
1271 elsif Ekind (Formal) = E_Out_Parameter
1272 and then Is_Array_Type (F_Typ)
1273 and then Number_Dimensions (F_Typ) = 1
1274 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1275 then
1276 -- Actual is a one-dimensional array or slice, and the type
1277 -- requires no initialization. Create a temporary of the
1278 -- right size, but do not copy actual into it (optimization).
1280 Init := Empty;
1281 Indic :=
1282 Make_Subtype_Indication (Loc,
1283 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1284 Constraint =>
1285 Make_Index_Or_Discriminant_Constraint (Loc,
1286 Constraints => New_List (
1287 Make_Range (Loc,
1288 Low_Bound =>
1289 Make_Attribute_Reference (Loc,
1290 Prefix => New_Occurrence_Of (Var, Loc),
1291 Attribute_Name => Name_First),
1292 High_Bound =>
1293 Make_Attribute_Reference (Loc,
1294 Prefix => New_Occurrence_Of (Var, Loc),
1295 Attribute_Name => Name_Last)))));
1297 else
1298 Init := New_Occurrence_Of (Var, Loc);
1299 end if;
1301 -- An initialization is created for packed conversions as
1302 -- actuals for out parameters to enable Make_Object_Declaration
1303 -- to determine the proper subtype for N_Node. Note that this
1304 -- is wasteful because the extra copying on the call side is
1305 -- not required for such out parameters. ???
1307 elsif Ekind (Formal) = E_Out_Parameter
1308 and then Nkind (Actual) = N_Type_Conversion
1309 and then (Is_Bit_Packed_Array (F_Typ)
1310 or else
1311 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1312 then
1313 if Conversion_OK (Actual) then
1314 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1315 else
1316 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1317 end if;
1319 elsif Ekind (Formal) = E_In_Parameter then
1321 -- Handle the case in which the actual is a type conversion
1323 if Nkind (Actual) = N_Type_Conversion then
1324 if Conversion_OK (Actual) then
1325 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1326 else
1327 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1328 end if;
1329 else
1330 Init := New_Occurrence_Of (Var, Loc);
1331 end if;
1333 else
1334 Init := Empty;
1335 end if;
1337 N_Node :=
1338 Make_Object_Declaration (Loc,
1339 Defining_Identifier => Temp,
1340 Object_Definition => Indic,
1341 Expression => Init);
1342 Set_Assignment_OK (N_Node);
1343 Insert_Action (N, N_Node);
1345 -- Now, normally the deal here is that we use the defining
1346 -- identifier created by that object declaration. There is
1347 -- one exception to this. In the change of representation case
1348 -- the above declaration will end up looking like:
1350 -- temp : type := identifier;
1352 -- And in this case we might as well use the identifier directly
1353 -- and eliminate the temporary. Note that the analysis of the
1354 -- declaration was not a waste of time in that case, since it is
1355 -- what generated the necessary change of representation code. If
1356 -- the change of representation introduced additional code, as in
1357 -- a fixed-integer conversion, the expression is not an identifier
1358 -- and must be kept.
1360 if Crep
1361 and then Present (Expression (N_Node))
1362 and then Is_Entity_Name (Expression (N_Node))
1363 then
1364 Temp := Entity (Expression (N_Node));
1365 Rewrite (N_Node, Make_Null_Statement (Loc));
1366 end if;
1368 -- For IN parameter, all we do is to replace the actual
1370 if Ekind (Formal) = E_In_Parameter then
1371 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1372 Analyze (Actual);
1374 -- Processing for OUT or IN OUT parameter
1376 else
1377 -- Kill current value indications for the temporary variable we
1378 -- created, since we just passed it as an OUT parameter.
1380 Kill_Current_Values (Temp);
1381 Set_Is_Known_Valid (Temp, False);
1383 -- If type conversion, use reverse conversion on exit
1385 if Nkind (Actual) = N_Type_Conversion then
1386 if Conversion_OK (Actual) then
1387 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1388 else
1389 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1390 end if;
1391 else
1392 Expr := New_Occurrence_Of (Temp, Loc);
1393 end if;
1395 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1396 Analyze (Actual);
1398 -- If the actual is a conversion of a packed reference, it may
1399 -- already have been expanded by Remove_Side_Effects, and the
1400 -- resulting variable is a temporary which does not designate
1401 -- the proper out-parameter, which may not be addressable. In
1402 -- that case, generate an assignment to the original expression
1403 -- (before expansion of the packed reference) so that the proper
1404 -- expansion of assignment to a packed component can take place.
1406 declare
1407 Obj : Node_Id;
1408 Lhs : Node_Id;
1410 begin
1411 if Is_Renaming_Of_Object (Var)
1412 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1413 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1414 = N_Indexed_Component
1415 and then
1416 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1417 then
1418 Obj := Renamed_Object (Var);
1419 Lhs :=
1420 Make_Selected_Component (Loc,
1421 Prefix =>
1422 New_Copy_Tree (Original_Node (Prefix (Obj))),
1423 Selector_Name => New_Copy (Selector_Name (Obj)));
1424 Reset_Analyzed_Flags (Lhs);
1426 else
1427 Lhs := New_Occurrence_Of (Var, Loc);
1428 end if;
1430 Set_Assignment_OK (Lhs);
1432 if Is_Access_Type (E_Formal)
1433 and then Is_Entity_Name (Lhs)
1434 and then
1435 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1436 then
1437 -- Copyback target is an Ada 2012 stand-alone object of an
1438 -- anonymous access type.
1440 pragma Assert (Ada_Version >= Ada_2012);
1442 if Type_Access_Level (E_Formal) >
1443 Object_Access_Level (Lhs)
1444 then
1445 Append_To (Post_Call,
1446 Make_Raise_Program_Error (Loc,
1447 Reason => PE_Accessibility_Check_Failed));
1448 end if;
1450 Append_To (Post_Call,
1451 Make_Assignment_Statement (Loc,
1452 Name => Lhs,
1453 Expression => Expr));
1455 -- We would like to somehow suppress generation of the
1456 -- extra_accessibility assignment generated by the expansion
1457 -- of the above assignment statement. It's not a correctness
1458 -- issue because the following assignment renders it dead,
1459 -- but generating back-to-back assignments to the same
1460 -- target is undesirable. ???
1462 Append_To (Post_Call,
1463 Make_Assignment_Statement (Loc,
1464 Name => New_Occurrence_Of (
1465 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1466 Expression => Make_Integer_Literal (Loc,
1467 Type_Access_Level (E_Formal))));
1469 else
1470 Append_To (Post_Call,
1471 Make_Assignment_Statement (Loc,
1472 Name => Lhs,
1473 Expression => Expr));
1474 end if;
1475 end;
1476 end if;
1477 end Add_Call_By_Copy_Code;
1479 ----------------------------------
1480 -- Add_Simple_Call_By_Copy_Code --
1481 ----------------------------------
1483 procedure Add_Simple_Call_By_Copy_Code is
1484 Decl : Node_Id;
1485 F_Typ : Entity_Id := Etype (Formal);
1486 Incod : Node_Id;
1487 Indic : Node_Id;
1488 Lhs : Node_Id;
1489 Outcod : Node_Id;
1490 Rhs : Node_Id;
1491 Temp : Entity_Id;
1493 begin
1494 if not Is_Legal_Copy then
1495 return;
1496 end if;
1498 -- Handle formals whose type comes from the limited view
1500 if From_Limited_With (F_Typ)
1501 and then Has_Non_Limited_View (F_Typ)
1502 then
1503 F_Typ := Non_Limited_View (F_Typ);
1504 end if;
1506 -- Use formal type for temp, unless formal type is an unconstrained
1507 -- array, in which case we don't have to worry about bounds checks,
1508 -- and we use the actual type, since that has appropriate bounds.
1510 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1511 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1512 else
1513 Indic := New_Occurrence_Of (F_Typ, Loc);
1514 end if;
1516 -- Prepare to generate code
1518 Reset_Packed_Prefix;
1520 Temp := Make_Temporary (Loc, 'T', Actual);
1521 Incod := Relocate_Node (Actual);
1522 Outcod := New_Copy_Tree (Incod);
1524 -- Generate declaration of temporary variable, initializing it
1525 -- with the input parameter unless we have an OUT formal or
1526 -- this is an initialization call.
1528 -- If the formal is an out parameter with discriminants, the
1529 -- discriminants must be captured even if the rest of the object
1530 -- is in principle uninitialized, because the discriminants may
1531 -- be read by the called subprogram.
1533 if Ekind (Formal) = E_Out_Parameter then
1534 Incod := Empty;
1536 if Has_Discriminants (F_Typ) then
1537 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1538 end if;
1540 elsif Inside_Init_Proc then
1542 -- Could use a comment here to match comment below ???
1544 if Nkind (Actual) /= N_Selected_Component
1545 or else
1546 not Has_Discriminant_Dependent_Constraint
1547 (Entity (Selector_Name (Actual)))
1548 then
1549 Incod := Empty;
1551 -- Otherwise, keep the component in order to generate the proper
1552 -- actual subtype, that depends on enclosing discriminants.
1554 else
1555 null;
1556 end if;
1557 end if;
1559 Decl :=
1560 Make_Object_Declaration (Loc,
1561 Defining_Identifier => Temp,
1562 Object_Definition => Indic,
1563 Expression => Incod);
1565 if Inside_Init_Proc
1566 and then No (Incod)
1567 then
1568 -- If the call is to initialize a component of a composite type,
1569 -- and the component does not depend on discriminants, use the
1570 -- actual type of the component. This is required in case the
1571 -- component is constrained, because in general the formal of the
1572 -- initialization procedure will be unconstrained. Note that if
1573 -- the component being initialized is constrained by an enclosing
1574 -- discriminant, the presence of the initialization in the
1575 -- declaration will generate an expression for the actual subtype.
1577 Set_No_Initialization (Decl);
1578 Set_Object_Definition (Decl,
1579 New_Occurrence_Of (Etype (Actual), Loc));
1580 end if;
1582 Insert_Action (N, Decl);
1584 -- The actual is simply a reference to the temporary
1586 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1588 -- Generate copy out if OUT or IN OUT parameter
1590 if Ekind (Formal) /= E_In_Parameter then
1591 Lhs := Outcod;
1592 Rhs := New_Occurrence_Of (Temp, Loc);
1594 -- Deal with conversion
1596 if Nkind (Lhs) = N_Type_Conversion then
1597 Lhs := Expression (Lhs);
1598 Rhs := Convert_To (Etype (Actual), Rhs);
1599 end if;
1601 Append_To (Post_Call,
1602 Make_Assignment_Statement (Loc,
1603 Name => Lhs,
1604 Expression => Rhs));
1605 Set_Assignment_OK (Name (Last (Post_Call)));
1606 end if;
1607 end Add_Simple_Call_By_Copy_Code;
1609 ---------------------------
1610 -- Check_Fortran_Logical --
1611 ---------------------------
1613 procedure Check_Fortran_Logical is
1614 Logical : constant Entity_Id := Etype (Formal);
1615 Var : Entity_Id;
1617 -- Note: this is very incomplete, e.g. it does not handle arrays
1618 -- of logical values. This is really not the right approach at all???)
1620 begin
1621 if Convention (Subp) = Convention_Fortran
1622 and then Root_Type (Etype (Formal)) = Standard_Boolean
1623 and then Ekind (Formal) /= E_In_Parameter
1624 then
1625 Var := Make_Var (Actual);
1626 Append_To (Post_Call,
1627 Make_Assignment_Statement (Loc,
1628 Name => New_Occurrence_Of (Var, Loc),
1629 Expression =>
1630 Unchecked_Convert_To (
1631 Logical,
1632 Make_Op_Ne (Loc,
1633 Left_Opnd => New_Occurrence_Of (Var, Loc),
1634 Right_Opnd =>
1635 Unchecked_Convert_To (
1636 Logical,
1637 New_Occurrence_Of (Standard_False, Loc))))));
1638 end if;
1639 end Check_Fortran_Logical;
1641 -------------------
1642 -- Is_Legal_Copy --
1643 -------------------
1645 function Is_Legal_Copy return Boolean is
1646 begin
1647 -- An attempt to copy a value of such a type can only occur if
1648 -- representation clauses give the actual a misaligned address.
1650 if Is_By_Reference_Type (Etype (Formal)) then
1652 -- If the front-end does not perform full type layout, the actual
1653 -- may in fact be properly aligned but there is not enough front-
1654 -- end information to determine this. In that case gigi will emit
1655 -- an error if a copy is not legal, or generate the proper code.
1656 -- For other backends we report the error now.
1658 -- Seems wrong to be issuing an error in the expander, since it
1659 -- will be missed in -gnatc mode ???
1661 if Frontend_Layout_On_Target then
1662 Error_Msg_N
1663 ("misaligned actual cannot be passed by reference", Actual);
1664 end if;
1666 return False;
1668 -- For users of Starlet, we assume that the specification of by-
1669 -- reference mechanism is mandatory. This may lead to unaligned
1670 -- objects but at least for DEC legacy code it is known to work.
1671 -- The warning will alert users of this code that a problem may
1672 -- be lurking.
1674 elsif Mechanism (Formal) = By_Reference
1675 and then Is_Valued_Procedure (Scope (Formal))
1676 then
1677 Error_Msg_N
1678 ("by_reference actual may be misaligned??", Actual);
1679 return False;
1681 else
1682 return True;
1683 end if;
1684 end Is_Legal_Copy;
1686 --------------
1687 -- Make_Var --
1688 --------------
1690 function Make_Var (Actual : Node_Id) return Entity_Id is
1691 Var : Entity_Id;
1693 begin
1694 if Is_Entity_Name (Actual) then
1695 return Entity (Actual);
1697 else
1698 Var := Make_Temporary (Loc, 'T', Actual);
1700 N_Node :=
1701 Make_Object_Renaming_Declaration (Loc,
1702 Defining_Identifier => Var,
1703 Subtype_Mark =>
1704 New_Occurrence_Of (Etype (Actual), Loc),
1705 Name => Relocate_Node (Actual));
1707 Insert_Action (N, N_Node);
1708 return Var;
1709 end if;
1710 end Make_Var;
1712 -------------------------
1713 -- Reset_Packed_Prefix --
1714 -------------------------
1716 procedure Reset_Packed_Prefix is
1717 Pfx : Node_Id := Actual;
1718 begin
1719 loop
1720 Set_Analyzed (Pfx, False);
1721 exit when
1722 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1723 Pfx := Prefix (Pfx);
1724 end loop;
1725 end Reset_Packed_Prefix;
1727 -- Start of processing for Expand_Actuals
1729 begin
1730 Post_Call := New_List;
1732 Formal := First_Formal (Subp);
1733 Actual := First_Actual (N);
1734 while Present (Formal) loop
1735 E_Formal := Etype (Formal);
1736 E_Actual := Etype (Actual);
1738 -- Handle formals whose type comes from the limited view
1740 if From_Limited_With (E_Formal)
1741 and then Has_Non_Limited_View (E_Formal)
1742 then
1743 E_Formal := Non_Limited_View (E_Formal);
1744 end if;
1746 if Is_Scalar_Type (E_Formal)
1747 or else Nkind (Actual) = N_Slice
1748 then
1749 Check_Fortran_Logical;
1751 -- RM 6.4.1 (11)
1753 elsif Ekind (Formal) /= E_Out_Parameter then
1755 -- The unusual case of the current instance of a protected type
1756 -- requires special handling. This can only occur in the context
1757 -- of a call within the body of a protected operation.
1759 if Is_Entity_Name (Actual)
1760 and then Ekind (Entity (Actual)) = E_Protected_Type
1761 and then In_Open_Scopes (Entity (Actual))
1762 then
1763 if Scope (Subp) /= Entity (Actual) then
1764 Error_Msg_N
1765 ("operation outside protected type may not "
1766 & "call back its protected operations??", Actual);
1767 end if;
1769 Rewrite (Actual,
1770 Expand_Protected_Object_Reference (N, Entity (Actual)));
1771 end if;
1773 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1774 -- build-in-place function, then a temporary return object needs
1775 -- to be created and access to it must be passed to the function.
1776 -- Currently we limit such functions to those with inherently
1777 -- limited result subtypes, but eventually we plan to expand the
1778 -- functions that are treated as build-in-place to include other
1779 -- composite result types.
1781 if Is_Build_In_Place_Function_Call (Actual) then
1782 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1783 end if;
1785 Apply_Constraint_Check (Actual, E_Formal);
1787 -- Out parameter case. No constraint checks on access type
1788 -- RM 6.4.1 (13)
1790 elsif Is_Access_Type (E_Formal) then
1791 null;
1793 -- RM 6.4.1 (14)
1795 elsif Has_Discriminants (Base_Type (E_Formal))
1796 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1797 then
1798 Apply_Constraint_Check (Actual, E_Formal);
1800 -- RM 6.4.1 (15)
1802 else
1803 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1804 end if;
1806 -- Processing for IN-OUT and OUT parameters
1808 if Ekind (Formal) /= E_In_Parameter then
1810 -- For type conversions of arrays, apply length/range checks
1812 if Is_Array_Type (E_Formal)
1813 and then Nkind (Actual) = N_Type_Conversion
1814 then
1815 if Is_Constrained (E_Formal) then
1816 Apply_Length_Check (Expression (Actual), E_Formal);
1817 else
1818 Apply_Range_Check (Expression (Actual), E_Formal);
1819 end if;
1820 end if;
1822 -- If argument is a type conversion for a type that is passed
1823 -- by copy, then we must pass the parameter by copy.
1825 if Nkind (Actual) = N_Type_Conversion
1826 and then
1827 (Is_Numeric_Type (E_Formal)
1828 or else Is_Access_Type (E_Formal)
1829 or else Is_Enumeration_Type (E_Formal)
1830 or else Is_Bit_Packed_Array (Etype (Formal))
1831 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1833 -- Also pass by copy if change of representation
1835 or else not Same_Representation
1836 (Etype (Formal),
1837 Etype (Expression (Actual))))
1838 then
1839 Add_Call_By_Copy_Code;
1841 -- References to components of bit-packed arrays are expanded
1842 -- at this point, rather than at the point of analysis of the
1843 -- actuals, to handle the expansion of the assignment to
1844 -- [in] out parameters.
1846 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1847 Add_Simple_Call_By_Copy_Code;
1849 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1850 -- because the back-end cannot cope with such objects. In other
1851 -- cases where alignment forces a copy, the back-end generates
1852 -- it properly. It should not be generated unconditionally in the
1853 -- front-end because it does not know precisely the alignment
1854 -- requirements of the target, and makes too conservative an
1855 -- estimate, leading to superfluous copies or spurious errors
1856 -- on by-reference parameters.
1858 elsif Nkind (Actual) = N_Selected_Component
1859 and then
1860 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1861 and then not Represented_As_Scalar (Etype (Formal))
1862 then
1863 Add_Simple_Call_By_Copy_Code;
1865 -- References to slices of bit-packed arrays are expanded
1867 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1868 Add_Call_By_Copy_Code;
1870 -- References to possibly unaligned slices of arrays are expanded
1872 elsif Is_Possibly_Unaligned_Slice (Actual) then
1873 Add_Call_By_Copy_Code;
1875 -- Deal with access types where the actual subtype and the
1876 -- formal subtype are not the same, requiring a check.
1878 -- It is necessary to exclude tagged types because of "downward
1879 -- conversion" errors.
1881 elsif Is_Access_Type (E_Formal)
1882 and then not Same_Type (E_Formal, E_Actual)
1883 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1884 then
1885 Add_Call_By_Copy_Code;
1887 -- If the actual is not a scalar and is marked for volatile
1888 -- treatment, whereas the formal is not volatile, then pass
1889 -- by copy unless it is a by-reference type.
1891 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1892 -- because this is the enforcement of a language rule that applies
1893 -- only to "real" volatile variables, not e.g. to the address
1894 -- clause overlay case.
1896 elsif Is_Entity_Name (Actual)
1897 and then Is_Volatile (Entity (Actual))
1898 and then not Is_By_Reference_Type (E_Actual)
1899 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1900 and then not Is_Volatile (E_Formal)
1901 then
1902 Add_Call_By_Copy_Code;
1904 elsif Nkind (Actual) = N_Indexed_Component
1905 and then Is_Entity_Name (Prefix (Actual))
1906 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1907 then
1908 Add_Call_By_Copy_Code;
1910 -- Add call-by-copy code for the case of scalar out parameters
1911 -- when it is not known at compile time that the subtype of the
1912 -- formal is a subrange of the subtype of the actual (or vice
1913 -- versa for in out parameters), in order to get range checks
1914 -- on such actuals. (Maybe this case should be handled earlier
1915 -- in the if statement???)
1917 elsif Is_Scalar_Type (E_Formal)
1918 and then
1919 (not In_Subrange_Of (E_Formal, E_Actual)
1920 or else
1921 (Ekind (Formal) = E_In_Out_Parameter
1922 and then not In_Subrange_Of (E_Actual, E_Formal)))
1923 then
1924 -- Perhaps the setting back to False should be done within
1925 -- Add_Call_By_Copy_Code, since it could get set on other
1926 -- cases occurring above???
1928 if Do_Range_Check (Actual) then
1929 Set_Do_Range_Check (Actual, False);
1930 end if;
1932 Add_Call_By_Copy_Code;
1933 end if;
1935 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
1936 -- by-reference parameters on exit from the call. If the actual
1937 -- is a derived type and the operation is inherited, the body
1938 -- of the operation will not contain a call to the predicate
1939 -- function, so it must be done explicitly after the call. Ditto
1940 -- if the actual is an entity of a predicated subtype.
1942 -- The rule refers to by-reference types, but a check is needed
1943 -- for by-copy types as well. That check is subsumed by the rule
1944 -- for subtype conversion on assignment, but we can generate the
1945 -- required check now.
1947 -- Note also that Subp may be either a subprogram entity for
1948 -- direct calls, or a type entity for indirect calls, which must
1949 -- be handled separately because the name does not denote an
1950 -- overloadable entity.
1952 By_Ref_Predicate_Check : declare
1953 Aund : constant Entity_Id := Underlying_Type (E_Actual);
1954 Atyp : Entity_Id;
1956 function Is_Public_Subp return Boolean;
1957 -- Check whether the subprogram being called is a visible
1958 -- operation of the type of the actual. Used to determine
1959 -- whether an invariant check must be generated on the
1960 -- caller side.
1962 ---------------------
1963 -- Is_Public_Subp --
1964 ---------------------
1966 function Is_Public_Subp return Boolean is
1967 Pack : constant Entity_Id := Scope (Subp);
1968 Subp_Decl : Node_Id;
1970 begin
1971 if not Is_Subprogram (Subp) then
1972 return False;
1974 -- The operation may be inherited, or a primitive of the
1975 -- root type.
1977 elsif
1978 Nkind_In (Parent (Subp), N_Private_Extension_Declaration,
1979 N_Full_Type_Declaration)
1980 then
1981 Subp_Decl := Parent (Subp);
1983 else
1984 Subp_Decl := Unit_Declaration_Node (Subp);
1985 end if;
1987 return Ekind (Pack) = E_Package
1988 and then
1989 List_Containing (Subp_Decl) =
1990 Visible_Declarations
1991 (Specification (Unit_Declaration_Node (Pack)));
1992 end Is_Public_Subp;
1994 -- Start of processing for By_Ref_Predicate_Check
1996 begin
1997 if No (Aund) then
1998 Atyp := E_Actual;
1999 else
2000 Atyp := Aund;
2001 end if;
2003 if Has_Predicates (Atyp)
2004 and then Present (Predicate_Function (Atyp))
2006 -- Skip predicate checks for special cases
2008 and then Predicate_Tests_On_Arguments (Subp)
2009 then
2010 Append_To (Post_Call,
2011 Make_Predicate_Check (Atyp, Actual));
2012 end if;
2014 -- We generated caller-side invariant checks in two cases:
2016 -- a) when calling an inherited operation, where there is an
2017 -- implicit view conversion of the actual to the parent type.
2019 -- b) When the conversion is explicit
2021 -- We treat these cases separately because the required
2022 -- conversion for a) is added later when expanding the call.
2024 if Has_Invariants (Etype (Actual))
2025 and then
2026 Nkind (Parent (Subp)) = N_Private_Extension_Declaration
2027 then
2028 if Comes_From_Source (N) and then Is_Public_Subp then
2029 Append_To (Post_Call, Make_Invariant_Call (Actual));
2030 end if;
2032 elsif Nkind (Actual) = N_Type_Conversion
2033 and then Has_Invariants (Etype (Expression (Actual)))
2034 then
2035 if Comes_From_Source (N) and then Is_Public_Subp then
2036 Append_To (Post_Call,
2037 Make_Invariant_Call (Expression (Actual)));
2038 end if;
2039 end if;
2040 end By_Ref_Predicate_Check;
2042 -- Processing for IN parameters
2044 else
2045 -- For IN parameters in the bit-packed array case, we expand an
2046 -- indexed component (the circuit in Exp_Ch4 deliberately left
2047 -- indexed components appearing as actuals untouched, so that
2048 -- the special processing above for the OUT and IN OUT cases
2049 -- could be performed. We could make the test in Exp_Ch4 more
2050 -- complex and have it detect the parameter mode, but it is
2051 -- easier simply to handle all cases here.)
2053 if Nkind (Actual) = N_Indexed_Component
2054 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2055 then
2056 Reset_Packed_Prefix;
2057 Expand_Packed_Element_Reference (Actual);
2059 -- If we have a reference to a bit-packed array, we copy it, since
2060 -- the actual must be byte aligned.
2062 -- Is this really necessary in all cases???
2064 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2065 Add_Simple_Call_By_Copy_Code;
2067 -- If a non-scalar actual is possibly unaligned, we need a copy
2069 elsif Is_Possibly_Unaligned_Object (Actual)
2070 and then not Represented_As_Scalar (Etype (Formal))
2071 then
2072 Add_Simple_Call_By_Copy_Code;
2074 -- Similarly, we have to expand slices of packed arrays here
2075 -- because the result must be byte aligned.
2077 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2078 Add_Call_By_Copy_Code;
2080 -- Only processing remaining is to pass by copy if this is a
2081 -- reference to a possibly unaligned slice, since the caller
2082 -- expects an appropriately aligned argument.
2084 elsif Is_Possibly_Unaligned_Slice (Actual) then
2085 Add_Call_By_Copy_Code;
2087 -- An unusual case: a current instance of an enclosing task can be
2088 -- an actual, and must be replaced by a reference to self.
2090 elsif Is_Entity_Name (Actual)
2091 and then Is_Task_Type (Entity (Actual))
2092 then
2093 if In_Open_Scopes (Entity (Actual)) then
2094 Rewrite (Actual,
2095 (Make_Function_Call (Loc,
2096 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2097 Analyze (Actual);
2099 -- A task type cannot otherwise appear as an actual
2101 else
2102 raise Program_Error;
2103 end if;
2104 end if;
2105 end if;
2107 Next_Formal (Formal);
2108 Next_Actual (Actual);
2109 end loop;
2111 -- Find right place to put post call stuff if it is present
2113 if not Is_Empty_List (Post_Call) then
2115 -- Cases where the call is not a member of a statement list.
2116 -- This includes the case where the call is an actual in another
2117 -- function call or indexing, i.e. an expression context as well.
2119 if not Is_List_Member (N)
2120 or else Nkind_In (Parent (N), N_Function_Call, N_Indexed_Component)
2121 then
2122 -- In Ada 2012 the call may be a function call in an expression
2123 -- (since OUT and IN OUT parameters are now allowed for such
2124 -- calls). The write-back of (in)-out parameters is handled
2125 -- by the back-end, but the constraint checks generated when
2126 -- subtypes of formal and actual don't match must be inserted
2127 -- in the form of assignments.
2129 if Ada_Version >= Ada_2012
2130 and then Nkind (N) = N_Function_Call
2131 then
2132 -- We used to just do handle this by climbing up parents to
2133 -- a non-statement/declaration and then simply making a call
2134 -- to Insert_Actions_After (P, Post_Call), but that doesn't
2135 -- work. If we are in the middle of an expression, e.g. the
2136 -- condition of an IF, this call would insert after the IF
2137 -- statement, which is much too late to be doing the write
2138 -- back. For example:
2140 -- if Clobber (X) then
2141 -- Put_Line (X'Img);
2142 -- else
2143 -- goto Junk
2144 -- end if;
2146 -- Now assume Clobber changes X, if we put the write back
2147 -- after the IF, the Put_Line gets the wrong value and the
2148 -- goto causes the write back to be skipped completely.
2150 -- To deal with this, we replace the call by
2152 -- do
2153 -- Tnnn : constant function-result-type := function-call;
2154 -- Post_Call actions
2155 -- in
2156 -- Tnnn;
2157 -- end;
2159 declare
2160 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
2161 FRTyp : constant Entity_Id := Etype (N);
2162 Name : constant Node_Id := Relocate_Node (N);
2164 begin
2165 Prepend_To (Post_Call,
2166 Make_Object_Declaration (Loc,
2167 Defining_Identifier => Tnnn,
2168 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
2169 Constant_Present => True,
2170 Expression => Name));
2172 Rewrite (N,
2173 Make_Expression_With_Actions (Loc,
2174 Actions => Post_Call,
2175 Expression => New_Occurrence_Of (Tnnn, Loc)));
2177 -- We don't want to just blindly call Analyze_And_Resolve
2178 -- because that would cause unwanted recursion on the call.
2179 -- So for a moment set the call as analyzed to prevent that
2180 -- recursion, and get the rest analyzed properly, then reset
2181 -- the analyzed flag, so our caller can continue.
2183 Set_Analyzed (Name, True);
2184 Analyze_And_Resolve (N, FRTyp);
2185 Set_Analyzed (Name, False);
2187 -- Reset calling argument to point to function call inside
2188 -- the expression with actions so the caller can continue
2189 -- to process the call. In spite of the fact that it is
2190 -- marked Analyzed above, it may be rewritten by Remove_
2191 -- Side_Effects if validity checks are present, so go back
2192 -- to original call.
2194 N := Original_Node (Name);
2195 end;
2197 -- If not the special Ada 2012 case of a function call, then
2198 -- we must have the triggering statement of a triggering
2199 -- alternative or an entry call alternative, and we can add
2200 -- the post call stuff to the corresponding statement list.
2202 else
2203 declare
2204 P : Node_Id;
2206 begin
2207 P := Parent (N);
2208 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
2209 N_Entry_Call_Alternative));
2211 if Is_Non_Empty_List (Statements (P)) then
2212 Insert_List_Before_And_Analyze
2213 (First (Statements (P)), Post_Call);
2214 else
2215 Set_Statements (P, Post_Call);
2216 end if;
2218 return;
2219 end;
2220 end if;
2222 -- Otherwise, normal case where N is in a statement sequence,
2223 -- just put the post-call stuff after the call statement.
2225 else
2226 Insert_Actions_After (N, Post_Call);
2227 return;
2228 end if;
2229 end if;
2231 -- The call node itself is re-analyzed in Expand_Call
2233 end Expand_Actuals;
2235 -----------------
2236 -- Expand_Call --
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 (N : Node_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
2615 begin
2616 -- Expand the function or procedure call if the first actual has a
2617 -- declared dimension aspect, and the subprogram is declared in one
2618 -- of the dimension I/O packages.
2620 if Ada_Version >= Ada_2012
2621 and then
2622 Nkind_In (Call_Node, N_Procedure_Call_Statement, N_Function_Call)
2623 and then Present (Parameter_Associations (Call_Node))
2624 then
2625 Expand_Put_Call_With_Symbol (Call_Node);
2626 end if;
2628 -- Ignore if previous error
2630 if Nkind (Call_Node) in N_Has_Etype
2631 and then Etype (Call_Node) = Any_Type
2632 then
2633 return;
2634 end if;
2636 -- Call using access to subprogram with explicit dereference
2638 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2639 Subp := Etype (Name (Call_Node));
2640 Parent_Subp := Empty;
2642 -- Case of call to simple entry, where the Name is a selected component
2643 -- whose prefix is the task, and whose selector name is the entry name
2645 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2646 Subp := Entity (Selector_Name (Name (Call_Node)));
2647 Parent_Subp := Empty;
2649 -- Case of call to member of entry family, where Name is an indexed
2650 -- component, with the prefix being a selected component giving the
2651 -- task and entry family name, and the index being the entry index.
2653 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2654 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2655 Parent_Subp := Empty;
2657 -- Normal case
2659 else
2660 Subp := Entity (Name (Call_Node));
2661 Parent_Subp := Alias (Subp);
2663 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2664 -- if we can tell that the first parameter cannot possibly be null.
2665 -- This improves efficiency by avoiding a run-time test.
2667 -- We do not do this if Raise_Exception_Always does not exist, which
2668 -- can happen in configurable run time profiles which provide only a
2669 -- Raise_Exception.
2671 if Is_RTE (Subp, RE_Raise_Exception)
2672 and then RTE_Available (RE_Raise_Exception_Always)
2673 then
2674 declare
2675 FA : constant Node_Id :=
2676 Original_Node (First_Actual (Call_Node));
2678 begin
2679 -- The case we catch is where the first argument is obtained
2680 -- using the Identity attribute (which must always be
2681 -- non-null).
2683 if Nkind (FA) = N_Attribute_Reference
2684 and then Attribute_Name (FA) = Name_Identity
2685 then
2686 Subp := RTE (RE_Raise_Exception_Always);
2687 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2688 end if;
2689 end;
2690 end if;
2692 if Ekind (Subp) = E_Entry then
2693 Parent_Subp := Empty;
2694 end if;
2695 end if;
2697 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2698 -- alternative in an asynchronous select or as an entry call in
2699 -- a conditional or timed select. Check whether the procedure call
2700 -- is a renaming of an entry and rewrite it as an entry call.
2702 if Ada_Version >= Ada_2005
2703 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2704 and then
2705 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2706 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2707 or else
2708 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2709 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2710 then
2711 declare
2712 Ren_Decl : Node_Id;
2713 Ren_Root : Entity_Id := Subp;
2715 begin
2716 -- This may be a chain of renamings, find the root
2718 if Present (Alias (Ren_Root)) then
2719 Ren_Root := Alias (Ren_Root);
2720 end if;
2722 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2723 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2725 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2726 Rewrite (Call_Node,
2727 Make_Entry_Call_Statement (Loc,
2728 Name =>
2729 New_Copy_Tree (Name (Ren_Decl)),
2730 Parameter_Associations =>
2731 New_Copy_List_Tree
2732 (Parameter_Associations (Call_Node))));
2734 return;
2735 end if;
2736 end if;
2737 end;
2738 end if;
2740 -- When generating C code, transform a function call that returns a
2741 -- constrained array type into procedure form.
2743 if Modify_Tree_For_C
2744 and then Nkind (Call_Node) = N_Function_Call
2745 and then Is_Entity_Name (Name (Call_Node))
2746 and then Rewritten_For_C (Ultimate_Alias (Entity (Name (Call_Node))))
2747 then
2748 -- For internally generated calls ensure that they reference the
2749 -- entity of the spec of the called function (needed since the
2750 -- expander may generate calls using the entity of their body).
2751 -- See for example Expand_Boolean_Operator().
2753 if not (Comes_From_Source (Call_Node))
2754 and then Nkind (Unit_Declaration_Node
2755 (Ultimate_Alias (Entity (Name (Call_Node))))) =
2756 N_Subprogram_Body
2757 then
2758 Set_Entity (Name (Call_Node),
2759 Corresponding_Function
2760 (Corresponding_Procedure
2761 (Ultimate_Alias (Entity (Name (Call_Node))))));
2762 end if;
2764 Rewrite_Function_Call_For_C (Call_Node);
2765 return;
2766 end if;
2768 -- First step, compute extra actuals, corresponding to any Extra_Formals
2769 -- present. Note that we do not access Extra_Formals directly, instead
2770 -- we simply note the presence of the extra formals as we process the
2771 -- regular formals collecting corresponding actuals in Extra_Actuals.
2773 -- We also generate any required range checks for actuals for in formals
2774 -- as we go through the loop, since this is a convenient place to do it.
2775 -- (Though it seems that this would be better done in Expand_Actuals???)
2777 -- Special case: Thunks must not compute the extra actuals; they must
2778 -- just propagate to the target primitive their extra actuals.
2780 if Is_Thunk (Current_Scope)
2781 and then Thunk_Entity (Current_Scope) = Subp
2782 and then Present (Extra_Formals (Subp))
2783 then
2784 pragma Assert (Present (Extra_Formals (Current_Scope)));
2786 declare
2787 Target_Formal : Entity_Id;
2788 Thunk_Formal : Entity_Id;
2790 begin
2791 Target_Formal := Extra_Formals (Subp);
2792 Thunk_Formal := Extra_Formals (Current_Scope);
2793 while Present (Target_Formal) loop
2794 Add_Extra_Actual
2795 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2797 Target_Formal := Extra_Formal (Target_Formal);
2798 Thunk_Formal := Extra_Formal (Thunk_Formal);
2799 end loop;
2801 while Is_Non_Empty_List (Extra_Actuals) loop
2802 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2803 end loop;
2805 Expand_Actuals (Call_Node, Subp);
2806 return;
2807 end;
2808 end if;
2810 Formal := First_Formal (Subp);
2811 Actual := First_Actual (Call_Node);
2812 Param_Count := 1;
2813 while Present (Formal) loop
2815 -- Generate range check if required
2817 if Do_Range_Check (Actual)
2818 and then Ekind (Formal) = E_In_Parameter
2819 then
2820 Generate_Range_Check
2821 (Actual, Etype (Formal), CE_Range_Check_Failed);
2822 end if;
2824 -- Prepare to examine current entry
2826 Prev := Actual;
2827 Prev_Orig := Original_Node (Prev);
2829 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2830 -- to expand it in a further round.
2832 CW_Interface_Formals_Present :=
2833 CW_Interface_Formals_Present
2834 or else
2835 (Is_Class_Wide_Type (Etype (Formal))
2836 and then Is_Interface (Etype (Etype (Formal))))
2837 or else
2838 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2839 and then Is_Class_Wide_Type (Directly_Designated_Type
2840 (Etype (Etype (Formal))))
2841 and then Is_Interface (Directly_Designated_Type
2842 (Etype (Etype (Formal)))));
2844 -- Create possible extra actual for constrained case. Usually, the
2845 -- extra actual is of the form actual'constrained, but since this
2846 -- attribute is only available for unconstrained records, TRUE is
2847 -- expanded if the type of the formal happens to be constrained (for
2848 -- instance when this procedure is inherited from an unconstrained
2849 -- record to a constrained one) or if the actual has no discriminant
2850 -- (its type is constrained). An exception to this is the case of a
2851 -- private type without discriminants. In this case we pass FALSE
2852 -- because the object has underlying discriminants with defaults.
2854 if Present (Extra_Constrained (Formal)) then
2855 if Ekind (Etype (Prev)) in Private_Kind
2856 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2857 then
2858 Add_Extra_Actual
2859 (New_Occurrence_Of (Standard_False, Loc),
2860 Extra_Constrained (Formal));
2862 elsif Is_Constrained (Etype (Formal))
2863 or else not Has_Discriminants (Etype (Prev))
2864 then
2865 Add_Extra_Actual
2866 (New_Occurrence_Of (Standard_True, Loc),
2867 Extra_Constrained (Formal));
2869 -- Do not produce extra actuals for Unchecked_Union parameters.
2870 -- Jump directly to the end of the loop.
2872 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2873 goto Skip_Extra_Actual_Generation;
2875 else
2876 -- If the actual is a type conversion, then the constrained
2877 -- test applies to the actual, not the target type.
2879 declare
2880 Act_Prev : Node_Id;
2882 begin
2883 -- Test for unchecked conversions as well, which can occur
2884 -- as out parameter actuals on calls to stream procedures.
2886 Act_Prev := Prev;
2887 while Nkind_In (Act_Prev, N_Type_Conversion,
2888 N_Unchecked_Type_Conversion)
2889 loop
2890 Act_Prev := Expression (Act_Prev);
2891 end loop;
2893 -- If the expression is a conversion of a dereference, this
2894 -- is internally generated code that manipulates addresses,
2895 -- e.g. when building interface tables. No check should
2896 -- occur in this case, and the discriminated object is not
2897 -- directly a hand.
2899 if not Comes_From_Source (Actual)
2900 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2901 and then Nkind (Act_Prev) = N_Explicit_Dereference
2902 then
2903 Add_Extra_Actual
2904 (New_Occurrence_Of (Standard_False, Loc),
2905 Extra_Constrained (Formal));
2907 else
2908 Add_Extra_Actual
2909 (Make_Attribute_Reference (Sloc (Prev),
2910 Prefix =>
2911 Duplicate_Subexpr_No_Checks
2912 (Act_Prev, Name_Req => True),
2913 Attribute_Name => Name_Constrained),
2914 Extra_Constrained (Formal));
2915 end if;
2916 end;
2917 end if;
2918 end if;
2920 -- Create possible extra actual for accessibility level
2922 if Present (Extra_Accessibility (Formal)) then
2924 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2925 -- attribute, then the original actual may be an aliased object
2926 -- occurring as the prefix in a call using "Object.Operation"
2927 -- notation. In that case we must pass the level of the object,
2928 -- so Prev_Orig is reset to Prev and the attribute will be
2929 -- processed by the code for Access attributes further below.
2931 if Prev_Orig /= Prev
2932 and then Nkind (Prev) = N_Attribute_Reference
2933 and then
2934 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2935 and then Is_Aliased_View (Prev_Orig)
2936 then
2937 Prev_Orig := Prev;
2938 end if;
2940 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2941 -- accessibility levels.
2943 if Is_Thunk (Current_Scope) then
2944 declare
2945 Parm_Ent : Entity_Id;
2947 begin
2948 if Is_Controlling_Actual (Actual) then
2950 -- Find the corresponding actual of the thunk
2952 Parm_Ent := First_Entity (Current_Scope);
2953 for J in 2 .. Param_Count loop
2954 Next_Entity (Parm_Ent);
2955 end loop;
2957 -- Handle unchecked conversion of access types generated
2958 -- in thunks (cf. Expand_Interface_Thunk).
2960 elsif Is_Access_Type (Etype (Actual))
2961 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2962 then
2963 Parm_Ent := Entity (Expression (Actual));
2965 else pragma Assert (Is_Entity_Name (Actual));
2966 Parm_Ent := Entity (Actual);
2967 end if;
2969 Add_Extra_Actual
2970 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2971 Extra_Accessibility (Formal));
2972 end;
2974 elsif Is_Entity_Name (Prev_Orig) then
2976 -- When passing an access parameter, or a renaming of an access
2977 -- parameter, as the actual to another access parameter we need
2978 -- to pass along the actual's own access level parameter. This
2979 -- is done if we are within the scope of the formal access
2980 -- parameter (if this is an inlined body the extra formal is
2981 -- irrelevant).
2983 if (Is_Formal (Entity (Prev_Orig))
2984 or else
2985 (Present (Renamed_Object (Entity (Prev_Orig)))
2986 and then
2987 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2988 and then
2989 Is_Formal
2990 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2991 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2992 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2993 then
2994 declare
2995 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2997 begin
2998 pragma Assert (Present (Parm_Ent));
3000 if Present (Extra_Accessibility (Parm_Ent)) then
3001 Add_Extra_Actual
3002 (New_Occurrence_Of
3003 (Extra_Accessibility (Parm_Ent), Loc),
3004 Extra_Accessibility (Formal));
3006 -- If the actual access parameter does not have an
3007 -- associated extra formal providing its scope level,
3008 -- then treat the actual as having library-level
3009 -- accessibility.
3011 else
3012 Add_Extra_Actual
3013 (Make_Integer_Literal (Loc,
3014 Intval => Scope_Depth (Standard_Standard)),
3015 Extra_Accessibility (Formal));
3016 end if;
3017 end;
3019 -- The actual is a normal access value, so just pass the level
3020 -- of the actual's access type.
3022 else
3023 Add_Extra_Actual
3024 (Dynamic_Accessibility_Level (Prev_Orig),
3025 Extra_Accessibility (Formal));
3026 end if;
3028 -- If the actual is an access discriminant, then pass the level
3029 -- of the enclosing object (RM05-3.10.2(12.4/2)).
3031 elsif Nkind (Prev_Orig) = N_Selected_Component
3032 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
3033 E_Discriminant
3034 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
3035 E_Anonymous_Access_Type
3036 then
3037 Add_Extra_Actual
3038 (Make_Integer_Literal (Loc,
3039 Intval => Object_Access_Level (Prefix (Prev_Orig))),
3040 Extra_Accessibility (Formal));
3042 -- All other cases
3044 else
3045 case Nkind (Prev_Orig) is
3046 when N_Attribute_Reference =>
3047 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
3049 -- For X'Access, pass on the level of the prefix X
3051 when Attribute_Access =>
3053 -- If this is an Access attribute applied to the
3054 -- the current instance object passed to a type
3055 -- initialization procedure, then use the level
3056 -- of the type itself. This is not really correct,
3057 -- as there should be an extra level parameter
3058 -- passed in with _init formals (only in the case
3059 -- where the type is immutably limited), but we
3060 -- don't have an easy way currently to create such
3061 -- an extra formal (init procs aren't ever frozen).
3062 -- For now we just use the level of the type,
3063 -- which may be too shallow, but that works better
3064 -- than passing Object_Access_Level of the type,
3065 -- which can be one level too deep in some cases.
3066 -- ???
3068 if Is_Entity_Name (Prefix (Prev_Orig))
3069 and then Is_Type (Entity (Prefix (Prev_Orig)))
3070 then
3071 Add_Extra_Actual
3072 (Make_Integer_Literal (Loc,
3073 Intval =>
3074 Type_Access_Level
3075 (Entity (Prefix (Prev_Orig)))),
3076 Extra_Accessibility (Formal));
3078 else
3079 Add_Extra_Actual
3080 (Make_Integer_Literal (Loc,
3081 Intval =>
3082 Object_Access_Level
3083 (Prefix (Prev_Orig))),
3084 Extra_Accessibility (Formal));
3085 end if;
3087 -- Treat the unchecked attributes as library-level
3089 when Attribute_Unchecked_Access
3090 | Attribute_Unrestricted_Access
3092 Add_Extra_Actual
3093 (Make_Integer_Literal (Loc,
3094 Intval => Scope_Depth (Standard_Standard)),
3095 Extra_Accessibility (Formal));
3097 -- No other cases of attributes returning access
3098 -- values that can be passed to access parameters.
3100 when others =>
3101 raise Program_Error;
3103 end case;
3105 -- For allocators we pass the level of the execution of the
3106 -- called subprogram, which is one greater than the current
3107 -- scope level.
3109 when N_Allocator =>
3110 Add_Extra_Actual
3111 (Make_Integer_Literal (Loc,
3112 Intval => Scope_Depth (Current_Scope) + 1),
3113 Extra_Accessibility (Formal));
3115 -- For most other cases we simply pass the level of the
3116 -- actual's access type. The type is retrieved from
3117 -- Prev rather than Prev_Orig, because in some cases
3118 -- Prev_Orig denotes an original expression that has
3119 -- not been analyzed.
3121 when others =>
3122 Add_Extra_Actual
3123 (Dynamic_Accessibility_Level (Prev),
3124 Extra_Accessibility (Formal));
3125 end case;
3126 end if;
3127 end if;
3129 -- Perform the check of 4.6(49) that prevents a null value from being
3130 -- passed as an actual to an access parameter. Note that the check
3131 -- is elided in the common cases of passing an access attribute or
3132 -- access parameter as an actual. Also, we currently don't enforce
3133 -- this check for expander-generated actuals and when -gnatdj is set.
3135 if Ada_Version >= Ada_2005 then
3137 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3138 -- the intent of 6.4.1(13) is that null-exclusion checks should
3139 -- not be done for 'out' parameters, even though it refers only
3140 -- to constraint checks, and a null_exclusion is not a constraint.
3141 -- Note that AI05-0196-1 corrects this mistake in the RM.
3143 if Is_Access_Type (Etype (Formal))
3144 and then Can_Never_Be_Null (Etype (Formal))
3145 and then Ekind (Formal) /= E_Out_Parameter
3146 and then Nkind (Prev) /= N_Raise_Constraint_Error
3147 and then (Known_Null (Prev)
3148 or else not Can_Never_Be_Null (Etype (Prev)))
3149 then
3150 Install_Null_Excluding_Check (Prev);
3151 end if;
3153 -- Ada_Version < Ada_2005
3155 else
3156 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3157 or else Access_Checks_Suppressed (Subp)
3158 then
3159 null;
3161 elsif Debug_Flag_J then
3162 null;
3164 elsif not Comes_From_Source (Prev) then
3165 null;
3167 elsif Is_Entity_Name (Prev)
3168 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3169 then
3170 null;
3172 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3173 null;
3175 else
3176 Install_Null_Excluding_Check (Prev);
3177 end if;
3178 end if;
3180 -- Perform appropriate validity checks on parameters that
3181 -- are entities.
3183 if Validity_Checks_On then
3184 if (Ekind (Formal) = E_In_Parameter
3185 and then Validity_Check_In_Params)
3186 or else
3187 (Ekind (Formal) = E_In_Out_Parameter
3188 and then Validity_Check_In_Out_Params)
3189 then
3190 -- If the actual is an indexed component of a packed type (or
3191 -- is an indexed or selected component whose prefix recursively
3192 -- meets this condition), it has not been expanded yet. It will
3193 -- be copied in the validity code that follows, and has to be
3194 -- expanded appropriately, so reanalyze it.
3196 -- What we do is just to unset analyzed bits on prefixes till
3197 -- we reach something that does not have a prefix.
3199 declare
3200 Nod : Node_Id;
3202 begin
3203 Nod := Actual;
3204 while Nkind_In (Nod, N_Indexed_Component,
3205 N_Selected_Component)
3206 loop
3207 Set_Analyzed (Nod, False);
3208 Nod := Prefix (Nod);
3209 end loop;
3210 end;
3212 Ensure_Valid (Actual);
3213 end if;
3214 end if;
3216 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3217 -- since this is a left side reference. We only do this for calls
3218 -- from the source program since we assume that compiler generated
3219 -- calls explicitly generate any required checks. We also need it
3220 -- only if we are doing standard validity checks, since clearly it is
3221 -- not needed if validity checks are off, and in subscript validity
3222 -- checking mode, all indexed components are checked with a call
3223 -- directly from Expand_N_Indexed_Component.
3225 if Comes_From_Source (Call_Node)
3226 and then Ekind (Formal) /= E_In_Parameter
3227 and then Validity_Checks_On
3228 and then Validity_Check_Default
3229 and then not Validity_Check_Subscripts
3230 then
3231 Check_Valid_Lvalue_Subscripts (Actual);
3232 end if;
3234 -- Mark any scalar OUT parameter that is a simple variable as no
3235 -- longer known to be valid (unless the type is always valid). This
3236 -- reflects the fact that if an OUT parameter is never set in a
3237 -- procedure, then it can become invalid on the procedure return.
3239 if Ekind (Formal) = E_Out_Parameter
3240 and then Is_Entity_Name (Actual)
3241 and then Ekind (Entity (Actual)) = E_Variable
3242 and then not Is_Known_Valid (Etype (Actual))
3243 then
3244 Set_Is_Known_Valid (Entity (Actual), False);
3245 end if;
3247 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3248 -- clear current values, since they can be clobbered. We are probably
3249 -- doing this in more places than we need to, but better safe than
3250 -- sorry when it comes to retaining bad current values.
3252 if Ekind (Formal) /= E_In_Parameter
3253 and then Is_Entity_Name (Actual)
3254 and then Present (Entity (Actual))
3255 then
3256 declare
3257 Ent : constant Entity_Id := Entity (Actual);
3258 Sav : Node_Id;
3260 begin
3261 -- For an OUT or IN OUT parameter that is an assignable entity,
3262 -- we do not want to clobber the Last_Assignment field, since
3263 -- if it is set, it was precisely because it is indeed an OUT
3264 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3265 -- since the subprogram could have returned in invalid value.
3267 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3268 and then Is_Assignable (Ent)
3269 then
3270 Sav := Last_Assignment (Ent);
3271 Kill_Current_Values (Ent);
3272 Set_Last_Assignment (Ent, Sav);
3273 Set_Is_Known_Valid (Ent, False);
3275 -- For all other cases, just kill the current values
3277 else
3278 Kill_Current_Values (Ent);
3279 end if;
3280 end;
3281 end if;
3283 -- If the formal is class wide and the actual is an aggregate, force
3284 -- evaluation so that the back end who does not know about class-wide
3285 -- type, does not generate a temporary of the wrong size.
3287 if not Is_Class_Wide_Type (Etype (Formal)) then
3288 null;
3290 elsif Nkind (Actual) = N_Aggregate
3291 or else (Nkind (Actual) = N_Qualified_Expression
3292 and then Nkind (Expression (Actual)) = N_Aggregate)
3293 then
3294 Force_Evaluation (Actual);
3295 end if;
3297 -- In a remote call, if the formal is of a class-wide type, check
3298 -- that the actual meets the requirements described in E.4(18).
3300 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3301 Insert_Action (Actual,
3302 Make_Transportable_Check (Loc,
3303 Duplicate_Subexpr_Move_Checks (Actual)));
3304 end if;
3306 -- Perform invariant checks for all intermediate types in a view
3307 -- conversion after successful return from a call that passes the
3308 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
3309 -- 13/3, 14/3)). Consider only source conversion in order to avoid
3310 -- generating spurious checks on complex expansion such as object
3311 -- initialization through an extension aggregate.
3313 if Comes_From_Source (N)
3314 and then Ekind (Formal) /= E_In_Parameter
3315 and then Nkind (Actual) = N_Type_Conversion
3316 then
3317 Add_View_Conversion_Invariants (Formal, Actual);
3318 end if;
3320 -- Generating C the initialization of an allocator is performed by
3321 -- means of individual statements, and hence it must be done before
3322 -- the call.
3324 if Modify_Tree_For_C
3325 and then Nkind (Actual) = N_Allocator
3326 and then Nkind (Expression (Actual)) = N_Qualified_Expression
3327 then
3328 Remove_Side_Effects (Actual);
3329 end if;
3331 -- This label is required when skipping extra actual generation for
3332 -- Unchecked_Union parameters.
3334 <<Skip_Extra_Actual_Generation>>
3336 Param_Count := Param_Count + 1;
3337 Next_Actual (Actual);
3338 Next_Formal (Formal);
3339 end loop;
3341 -- If we are calling an Ada 2012 function which needs to have the
3342 -- "accessibility level determined by the point of call" (AI05-0234)
3343 -- passed in to it, then pass it in.
3345 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3346 and then
3347 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3348 then
3349 declare
3350 Ancestor : Node_Id := Parent (Call_Node);
3351 Level : Node_Id := Empty;
3352 Defer : Boolean := False;
3354 begin
3355 -- Unimplemented: if Subp returns an anonymous access type, then
3357 -- a) if the call is the operand of an explict conversion, then
3358 -- the target type of the conversion (a named access type)
3359 -- determines the accessibility level pass in;
3361 -- b) if the call defines an access discriminant of an object
3362 -- (e.g., the discriminant of an object being created by an
3363 -- allocator, or the discriminant of a function result),
3364 -- then the accessibility level to pass in is that of the
3365 -- discriminated object being initialized).
3367 -- ???
3369 while Nkind (Ancestor) = N_Qualified_Expression
3370 loop
3371 Ancestor := Parent (Ancestor);
3372 end loop;
3374 case Nkind (Ancestor) is
3375 when N_Allocator =>
3377 -- At this point, we'd like to assign
3379 -- Level := Dynamic_Accessibility_Level (Ancestor);
3381 -- but Etype of Ancestor may not have been set yet,
3382 -- so that doesn't work.
3384 -- Handle this later in Expand_Allocator_Expression.
3386 Defer := True;
3388 when N_Object_Declaration
3389 | N_Object_Renaming_Declaration
3391 declare
3392 Def_Id : constant Entity_Id :=
3393 Defining_Identifier (Ancestor);
3395 begin
3396 if Is_Return_Object (Def_Id) then
3397 if Present (Extra_Accessibility_Of_Result
3398 (Return_Applies_To (Scope (Def_Id))))
3399 then
3400 -- Pass along value that was passed in if the
3401 -- routine we are returning from also has an
3402 -- Accessibility_Of_Result formal.
3404 Level :=
3405 New_Occurrence_Of
3406 (Extra_Accessibility_Of_Result
3407 (Return_Applies_To (Scope (Def_Id))), Loc);
3408 end if;
3409 else
3410 Level :=
3411 Make_Integer_Literal (Loc,
3412 Intval => Object_Access_Level (Def_Id));
3413 end if;
3414 end;
3416 when N_Simple_Return_Statement =>
3417 if Present (Extra_Accessibility_Of_Result
3418 (Return_Applies_To
3419 (Return_Statement_Entity (Ancestor))))
3420 then
3421 -- Pass along value that was passed in if the returned
3422 -- routine also has an Accessibility_Of_Result formal.
3424 Level :=
3425 New_Occurrence_Of
3426 (Extra_Accessibility_Of_Result
3427 (Return_Applies_To
3428 (Return_Statement_Entity (Ancestor))), Loc);
3429 end if;
3431 when others =>
3432 null;
3433 end case;
3435 if not Defer then
3436 if not Present (Level) then
3438 -- The "innermost master that evaluates the function call".
3440 -- ??? - Should we use Integer'Last here instead in order
3441 -- to deal with (some of) the problems associated with
3442 -- calls to subps whose enclosing scope is unknown (e.g.,
3443 -- Anon_Access_To_Subp_Param.all)?
3445 Level :=
3446 Make_Integer_Literal (Loc,
3447 Intval => Scope_Depth (Current_Scope) + 1);
3448 end if;
3450 Add_Extra_Actual
3451 (Level,
3452 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3453 end if;
3454 end;
3455 end if;
3457 -- If we are expanding the RHS of an assignment we need to check if tag
3458 -- propagation is needed. You might expect this processing to be in
3459 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3460 -- assignment might be transformed to a declaration for an unconstrained
3461 -- value if the expression is classwide.
3463 if Nkind (Call_Node) = N_Function_Call
3464 and then Is_Tag_Indeterminate (Call_Node)
3465 and then Is_Entity_Name (Name (Call_Node))
3466 then
3467 declare
3468 Ass : Node_Id := Empty;
3470 begin
3471 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3472 Ass := Parent (Call_Node);
3474 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3475 and then Nkind (Parent (Parent (Call_Node))) =
3476 N_Assignment_Statement
3477 then
3478 Ass := Parent (Parent (Call_Node));
3480 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3481 and then Nkind (Parent (Parent (Call_Node))) =
3482 N_Assignment_Statement
3483 then
3484 Ass := Parent (Parent (Call_Node));
3485 end if;
3487 if Present (Ass)
3488 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3489 then
3490 if Is_Access_Type (Etype (Call_Node)) then
3491 if Designated_Type (Etype (Call_Node)) /=
3492 Root_Type (Etype (Name (Ass)))
3493 then
3494 Error_Msg_NE
3495 ("tag-indeterminate expression "
3496 & " must have designated type& (RM 5.2 (6))",
3497 Call_Node, Root_Type (Etype (Name (Ass))));
3498 else
3499 Propagate_Tag (Name (Ass), Call_Node);
3500 end if;
3502 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3503 Error_Msg_NE
3504 ("tag-indeterminate expression must have type&"
3505 & "(RM 5.2 (6))",
3506 Call_Node, Root_Type (Etype (Name (Ass))));
3508 else
3509 Propagate_Tag (Name (Ass), Call_Node);
3510 end if;
3512 -- The call will be rewritten as a dispatching call, and
3513 -- expanded as such.
3515 return;
3516 end if;
3517 end;
3518 end if;
3520 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3521 -- it to point to the correct secondary virtual table
3523 if Nkind (Call_Node) in N_Subprogram_Call
3524 and then CW_Interface_Formals_Present
3525 then
3526 Expand_Interface_Actuals (Call_Node);
3527 end if;
3529 -- Deals with Dispatch_Call if we still have a call, before expanding
3530 -- extra actuals since this will be done on the re-analysis of the
3531 -- dispatching call. Note that we do not try to shorten the actual list
3532 -- for a dispatching call, it would not make sense to do so. Expansion
3533 -- of dispatching calls is suppressed for VM targets, because the VM
3534 -- back-ends directly handle the generation of dispatching calls and
3535 -- would have to undo any expansion to an indirect call.
3537 if Nkind (Call_Node) in N_Subprogram_Call
3538 and then Present (Controlling_Argument (Call_Node))
3539 then
3540 declare
3541 Call_Typ : constant Entity_Id := Etype (Call_Node);
3542 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3543 Eq_Prim_Op : Entity_Id := Empty;
3544 New_Call : Node_Id;
3545 Param : Node_Id;
3546 Prev_Call : Node_Id;
3548 begin
3549 if not Is_Limited_Type (Typ) then
3550 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3551 end if;
3553 if Tagged_Type_Expansion then
3554 Expand_Dispatching_Call (Call_Node);
3556 -- The following return is worrisome. Is it really OK to skip
3557 -- all remaining processing in this procedure ???
3559 return;
3561 -- VM targets
3563 else
3564 Apply_Tag_Checks (Call_Node);
3566 -- If this is a dispatching "=", we must first compare the
3567 -- tags so we generate: x.tag = y.tag and then x = y
3569 if Subp = Eq_Prim_Op then
3571 -- Mark the node as analyzed to avoid reanalyzing this
3572 -- dispatching call (which would cause a never-ending loop)
3574 Prev_Call := Relocate_Node (Call_Node);
3575 Set_Analyzed (Prev_Call);
3577 Param := First_Actual (Call_Node);
3578 New_Call :=
3579 Make_And_Then (Loc,
3580 Left_Opnd =>
3581 Make_Op_Eq (Loc,
3582 Left_Opnd =>
3583 Make_Selected_Component (Loc,
3584 Prefix => New_Value (Param),
3585 Selector_Name =>
3586 New_Occurrence_Of
3587 (First_Tag_Component (Typ), Loc)),
3589 Right_Opnd =>
3590 Make_Selected_Component (Loc,
3591 Prefix =>
3592 Unchecked_Convert_To (Typ,
3593 New_Value (Next_Actual (Param))),
3594 Selector_Name =>
3595 New_Occurrence_Of
3596 (First_Tag_Component (Typ), Loc))),
3597 Right_Opnd => Prev_Call);
3599 Rewrite (Call_Node, New_Call);
3601 Analyze_And_Resolve
3602 (Call_Node, Call_Typ, Suppress => All_Checks);
3603 end if;
3605 -- Expansion of a dispatching call results in an indirect call,
3606 -- which in turn causes current values to be killed (see
3607 -- Resolve_Call), so on VM targets we do the call here to
3608 -- ensure consistent warnings between VM and non-VM targets.
3610 Kill_Current_Values;
3611 end if;
3613 -- If this is a dispatching "=" then we must update the reference
3614 -- to the call node because we generated:
3615 -- x.tag = y.tag and then x = y
3617 if Subp = Eq_Prim_Op then
3618 Call_Node := Right_Opnd (Call_Node);
3619 end if;
3620 end;
3621 end if;
3623 -- Similarly, expand calls to RCI subprograms on which pragma
3624 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3625 -- later. Do this only when the call comes from source since we
3626 -- do not want such a rewriting to occur in expanded code.
3628 if Is_All_Remote_Call (Call_Node) then
3629 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3631 -- Similarly, do not add extra actuals for an entry call whose entity
3632 -- is a protected procedure, or for an internal protected subprogram
3633 -- call, because it will be rewritten as a protected subprogram call
3634 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3636 elsif Is_Protected_Type (Scope (Subp))
3637 and then (Ekind (Subp) = E_Procedure
3638 or else Ekind (Subp) = E_Function)
3639 then
3640 null;
3642 -- During that loop we gathered the extra actuals (the ones that
3643 -- correspond to Extra_Formals), so now they can be appended.
3645 else
3646 while Is_Non_Empty_List (Extra_Actuals) loop
3647 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3648 end loop;
3649 end if;
3651 -- At this point we have all the actuals, so this is the point at which
3652 -- the various expansion activities for actuals is carried out.
3654 Expand_Actuals (Call_Node, Subp);
3656 -- Verify that the actuals do not share storage. This check must be done
3657 -- on the caller side rather that inside the subprogram to avoid issues
3658 -- of parameter passing.
3660 if Check_Aliasing_Of_Parameters then
3661 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3662 end if;
3664 -- If the subprogram is a renaming, or if it is inherited, replace it in
3665 -- the call with the name of the actual subprogram being called. If this
3666 -- is a dispatching call, the run-time decides what to call. The Alias
3667 -- attribute does not apply to entries.
3669 if Nkind (Call_Node) /= N_Entry_Call_Statement
3670 and then No (Controlling_Argument (Call_Node))
3671 and then Present (Parent_Subp)
3672 and then not Is_Direct_Deep_Call (Subp)
3673 then
3674 if Present (Inherited_From_Formal (Subp)) then
3675 Parent_Subp := Inherited_From_Formal (Subp);
3676 else
3677 Parent_Subp := Ultimate_Alias (Parent_Subp);
3678 end if;
3680 -- The below setting of Entity is suspect, see F109-018 discussion???
3682 Set_Entity (Name (Call_Node), Parent_Subp);
3684 if Is_Abstract_Subprogram (Parent_Subp)
3685 and then not In_Instance
3686 then
3687 Error_Msg_NE
3688 ("cannot call abstract subprogram &!",
3689 Name (Call_Node), Parent_Subp);
3690 end if;
3692 -- Inspect all formals of derived subprogram Subp. Compare parameter
3693 -- types with the parent subprogram and check whether an actual may
3694 -- need a type conversion to the corresponding formal of the parent
3695 -- subprogram.
3697 -- Not clear whether intrinsic subprograms need such conversions. ???
3699 if not Is_Intrinsic_Subprogram (Parent_Subp)
3700 or else Is_Generic_Instance (Parent_Subp)
3701 then
3702 declare
3703 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3704 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3705 -- and resolve the newly generated construct.
3707 -------------
3708 -- Convert --
3709 -------------
3711 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3712 begin
3713 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3714 Analyze (Act);
3715 Resolve (Act, Typ);
3716 end Convert;
3718 -- Local variables
3720 Actual_Typ : Entity_Id;
3721 Formal_Typ : Entity_Id;
3722 Parent_Typ : Entity_Id;
3724 begin
3725 Actual := First_Actual (Call_Node);
3726 Formal := First_Formal (Subp);
3727 Parent_Formal := First_Formal (Parent_Subp);
3728 while Present (Formal) loop
3729 Actual_Typ := Etype (Actual);
3730 Formal_Typ := Etype (Formal);
3731 Parent_Typ := Etype (Parent_Formal);
3733 -- For an IN parameter of a scalar type, the parent formal
3734 -- type and derived formal type differ or the parent formal
3735 -- type and actual type do not match statically.
3737 if Is_Scalar_Type (Formal_Typ)
3738 and then Ekind (Formal) = E_In_Parameter
3739 and then Formal_Typ /= Parent_Typ
3740 and then
3741 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3742 and then not Raises_Constraint_Error (Actual)
3743 then
3744 Convert (Actual, Parent_Typ);
3745 Enable_Range_Check (Actual);
3747 -- If the actual has been marked as requiring a range
3748 -- check, then generate it here.
3750 if Do_Range_Check (Actual) then
3751 Generate_Range_Check
3752 (Actual, Etype (Formal), CE_Range_Check_Failed);
3753 end if;
3755 -- For access types, the parent formal type and actual type
3756 -- differ.
3758 elsif Is_Access_Type (Formal_Typ)
3759 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3760 then
3761 if Ekind (Formal) /= E_In_Parameter then
3762 Convert (Actual, Parent_Typ);
3764 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3765 and then Designated_Type (Parent_Typ) /=
3766 Designated_Type (Actual_Typ)
3767 and then not Is_Controlling_Formal (Formal)
3768 then
3769 -- This unchecked conversion is not necessary unless
3770 -- inlining is enabled, because in that case the type
3771 -- mismatch may become visible in the body about to be
3772 -- inlined.
3774 Rewrite (Actual,
3775 Unchecked_Convert_To (Parent_Typ,
3776 Relocate_Node (Actual)));
3777 Analyze (Actual);
3778 Resolve (Actual, Parent_Typ);
3779 end if;
3781 -- If there is a change of representation, then generate a
3782 -- warning, and do the change of representation.
3784 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3785 Error_Msg_N
3786 ("??change of representation required", Actual);
3787 Convert (Actual, Parent_Typ);
3789 -- For array and record types, the parent formal type and
3790 -- derived formal type have different sizes or pragma Pack
3791 -- status.
3793 elsif ((Is_Array_Type (Formal_Typ)
3794 and then Is_Array_Type (Parent_Typ))
3795 or else
3796 (Is_Record_Type (Formal_Typ)
3797 and then Is_Record_Type (Parent_Typ)))
3798 and then
3799 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3800 or else Has_Pragma_Pack (Formal_Typ) /=
3801 Has_Pragma_Pack (Parent_Typ))
3802 then
3803 Convert (Actual, Parent_Typ);
3804 end if;
3806 Next_Actual (Actual);
3807 Next_Formal (Formal);
3808 Next_Formal (Parent_Formal);
3809 end loop;
3810 end;
3811 end if;
3813 Orig_Subp := Subp;
3814 Subp := Parent_Subp;
3815 end if;
3817 -- Deal with case where call is an explicit dereference
3819 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3821 -- Handle case of access to protected subprogram type
3823 if Is_Access_Protected_Subprogram_Type
3824 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3825 then
3826 -- If this is a call through an access to protected operation, the
3827 -- prefix has the form (object'address, operation'access). Rewrite
3828 -- as a for other protected calls: the object is the 1st parameter
3829 -- of the list of actuals.
3831 declare
3832 Call : Node_Id;
3833 Parm : List_Id;
3834 Nam : Node_Id;
3835 Obj : Node_Id;
3836 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3838 T : constant Entity_Id :=
3839 Equivalent_Type (Base_Type (Etype (Ptr)));
3841 D_T : constant Entity_Id :=
3842 Designated_Type (Base_Type (Etype (Ptr)));
3844 begin
3845 Obj :=
3846 Make_Selected_Component (Loc,
3847 Prefix => Unchecked_Convert_To (T, Ptr),
3848 Selector_Name =>
3849 New_Occurrence_Of (First_Entity (T), Loc));
3851 Nam :=
3852 Make_Selected_Component (Loc,
3853 Prefix => Unchecked_Convert_To (T, Ptr),
3854 Selector_Name =>
3855 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3857 Nam :=
3858 Make_Explicit_Dereference (Loc,
3859 Prefix => Nam);
3861 if Present (Parameter_Associations (Call_Node)) then
3862 Parm := Parameter_Associations (Call_Node);
3863 else
3864 Parm := New_List;
3865 end if;
3867 Prepend (Obj, Parm);
3869 if Etype (D_T) = Standard_Void_Type then
3870 Call :=
3871 Make_Procedure_Call_Statement (Loc,
3872 Name => Nam,
3873 Parameter_Associations => Parm);
3874 else
3875 Call :=
3876 Make_Function_Call (Loc,
3877 Name => Nam,
3878 Parameter_Associations => Parm);
3879 end if;
3881 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3882 Set_Etype (Call, Etype (D_T));
3884 -- We do not re-analyze the call to avoid infinite recursion.
3885 -- We analyze separately the prefix and the object, and set
3886 -- the checks on the prefix that would otherwise be emitted
3887 -- when resolving a call.
3889 Rewrite (Call_Node, Call);
3890 Analyze (Nam);
3891 Apply_Access_Check (Nam);
3892 Analyze (Obj);
3893 return;
3894 end;
3895 end if;
3896 end if;
3898 -- If this is a call to an intrinsic subprogram, then perform the
3899 -- appropriate expansion to the corresponding tree node and we
3900 -- are all done (since after that the call is gone).
3902 -- In the case where the intrinsic is to be processed by the back end,
3903 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3904 -- since the idea in this case is to pass the call unchanged. If the
3905 -- intrinsic is an inherited unchecked conversion, and the derived type
3906 -- is the target type of the conversion, we must retain it as the return
3907 -- type of the expression. Otherwise the expansion below, which uses the
3908 -- parent operation, will yield the wrong type.
3910 if Is_Intrinsic_Subprogram (Subp) then
3911 Expand_Intrinsic_Call (Call_Node, Subp);
3913 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3914 and then Parent_Subp /= Orig_Subp
3915 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3916 then
3917 Set_Etype (Call_Node, Etype (Orig_Subp));
3918 end if;
3920 return;
3921 end if;
3923 if Ekind_In (Subp, E_Function, E_Procedure) then
3925 -- We perform a simple optimization on calls for To_Address by
3926 -- replacing them with an unchecked conversion. Not only is this
3927 -- efficient, but it also avoids order of elaboration problems when
3928 -- address clauses are inlined (address expression elaborated at the
3929 -- at the wrong point).
3931 -- We perform this optimization regardless of whether we are in the
3932 -- main unit or in a unit in the context of the main unit, to ensure
3933 -- that tree generated is the same in both cases, for CodePeer use.
3935 if Is_RTE (Subp, RE_To_Address) then
3936 Rewrite (Call_Node,
3937 Unchecked_Convert_To
3938 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3939 return;
3940 end if;
3942 -- Handle inlining. No action needed if the subprogram is not inlined
3944 if not Is_Inlined (Subp) then
3945 null;
3947 -- Frontend inlining of expression functions (performed also when
3948 -- backend inlining is enabled).
3950 elsif Is_Inlinable_Expression_Function (Subp) then
3951 Rewrite (N, New_Copy (Expression_Of_Expression_Function (Subp)));
3952 Analyze (N);
3953 return;
3955 -- Handle frontend inlining
3957 elsif not Back_End_Inlining then
3958 Inlined_Subprogram : declare
3959 Bod : Node_Id;
3960 Must_Inline : Boolean := False;
3961 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3963 begin
3964 -- Verify that the body to inline has already been seen, and
3965 -- that if the body is in the current unit the inlining does
3966 -- not occur earlier. This avoids order-of-elaboration problems
3967 -- in the back end.
3969 -- This should be documented in sinfo/einfo ???
3971 if No (Spec)
3972 or else Nkind (Spec) /= N_Subprogram_Declaration
3973 or else No (Body_To_Inline (Spec))
3974 then
3975 Must_Inline := False;
3977 -- If this an inherited function that returns a private type,
3978 -- do not inline if the full view is an unconstrained array,
3979 -- because such calls cannot be inlined.
3981 elsif Present (Orig_Subp)
3982 and then Is_Array_Type (Etype (Orig_Subp))
3983 and then not Is_Constrained (Etype (Orig_Subp))
3984 then
3985 Must_Inline := False;
3987 elsif In_Unfrozen_Instance (Scope (Subp)) then
3988 Must_Inline := False;
3990 else
3991 Bod := Body_To_Inline (Spec);
3993 if (In_Extended_Main_Code_Unit (Call_Node)
3994 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3995 or else Has_Pragma_Inline_Always (Subp))
3996 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3997 or else
3998 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3999 then
4000 Must_Inline := True;
4002 -- If we are compiling a package body that is not the main
4003 -- unit, it must be for inlining/instantiation purposes,
4004 -- in which case we inline the call to insure that the same
4005 -- temporaries are generated when compiling the body by
4006 -- itself. Otherwise link errors can occur.
4008 -- If the function being called is itself in the main unit,
4009 -- we cannot inline, because there is a risk of double
4010 -- elaboration and/or circularity: the inlining can make
4011 -- visible a private entity in the body of the main unit,
4012 -- that gigi will see before its sees its proper definition.
4014 elsif not (In_Extended_Main_Code_Unit (Call_Node))
4015 and then In_Package_Body
4016 then
4017 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4019 -- Inline calls to _postconditions when generating C code
4021 elsif Modify_Tree_For_C
4022 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4023 and then Chars (Name (N)) = Name_uPostconditions
4024 then
4025 Must_Inline := True;
4026 end if;
4027 end if;
4029 if Must_Inline then
4030 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4032 else
4033 -- Let the back end handle it
4035 Add_Inlined_Body (Subp, Call_Node);
4037 if Front_End_Inlining
4038 and then Nkind (Spec) = N_Subprogram_Declaration
4039 and then (In_Extended_Main_Code_Unit (Call_Node))
4040 and then No (Body_To_Inline (Spec))
4041 and then not Has_Completion (Subp)
4042 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4043 then
4044 Cannot_Inline
4045 ("cannot inline& (body not seen yet)?",
4046 Call_Node, Subp);
4047 end if;
4048 end if;
4049 end Inlined_Subprogram;
4051 -- Back end inlining: let the back end handle it
4053 elsif No (Unit_Declaration_Node (Subp))
4054 or else Nkind (Unit_Declaration_Node (Subp)) /=
4055 N_Subprogram_Declaration
4056 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
4057 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
4058 N_Entity
4059 then
4060 Add_Inlined_Body (Subp, Call_Node);
4062 -- If the inlined call appears within an instantiation and some
4063 -- level of optimization is required, ensure that the enclosing
4064 -- instance body is available so that the back-end can actually
4065 -- perform the inlining.
4067 if In_Instance
4068 and then Comes_From_Source (Subp)
4069 and then Optimization_Level > 0
4070 then
4071 declare
4072 Decl : Node_Id;
4073 Inst : Entity_Id;
4074 Inst_Node : Node_Id;
4076 begin
4077 Inst := Scope (Subp);
4079 -- Find enclosing instance
4081 while Present (Inst) and then Inst /= Standard_Standard loop
4082 exit when Is_Generic_Instance (Inst);
4083 Inst := Scope (Inst);
4084 end loop;
4086 if Present (Inst)
4087 and then Is_Generic_Instance (Inst)
4088 and then not Is_Inlined (Inst)
4089 then
4090 Set_Is_Inlined (Inst);
4091 Decl := Unit_Declaration_Node (Inst);
4093 -- Do not add a pending instantiation if the body exits
4094 -- already, or if the instance is a compilation unit, or
4095 -- the instance node is missing.
4097 if Present (Corresponding_Body (Decl))
4098 or else Nkind (Parent (Decl)) = N_Compilation_Unit
4099 or else No (Next (Decl))
4100 then
4101 null;
4103 else
4104 -- The instantiation node usually follows the package
4105 -- declaration for the instance. If the generic unit
4106 -- has aspect specifications, they are transformed
4107 -- into pragmas in the instance, and the instance node
4108 -- appears after them.
4110 Inst_Node := Next (Decl);
4112 while Nkind (Inst_Node) /= N_Package_Instantiation loop
4113 Inst_Node := Next (Inst_Node);
4114 end loop;
4116 Add_Pending_Instantiation (Inst_Node, Decl);
4117 end if;
4118 end if;
4119 end;
4120 end if;
4122 -- Front end expansion of simple functions returning unconstrained
4123 -- types (see Check_And_Split_Unconstrained_Function). Note that the
4124 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
4125 -- also Build_Renamed_Body) cannot be expanded here because this may
4126 -- give rise to order-of-elaboration issues for the types of the
4127 -- parameters of the subprogram, if any.
4129 else
4130 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4131 end if;
4132 end if;
4134 -- Check for protected subprogram. This is either an intra-object call,
4135 -- or a protected function call. Protected procedure calls are rewritten
4136 -- as entry calls and handled accordingly.
4138 -- In Ada 2005, this may be an indirect call to an access parameter that
4139 -- is an access_to_subprogram. In that case the anonymous type has a
4140 -- scope that is a protected operation, but the call is a regular one.
4141 -- In either case do not expand call if subprogram is eliminated.
4143 Scop := Scope (Subp);
4145 if Nkind (Call_Node) /= N_Entry_Call_Statement
4146 and then Is_Protected_Type (Scop)
4147 and then Ekind (Subp) /= E_Subprogram_Type
4148 and then not Is_Eliminated (Subp)
4149 then
4150 -- If the call is an internal one, it is rewritten as a call to the
4151 -- corresponding unprotected subprogram.
4153 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4154 end if;
4156 -- Functions returning controlled objects need special attention. If
4157 -- the return type is limited, then the context is initialization and
4158 -- different processing applies. If the call is to a protected function,
4159 -- the expansion above will call Expand_Call recursively. Otherwise the
4160 -- function call is transformed into a temporary which obtains the
4161 -- result from the secondary stack.
4163 if Needs_Finalization (Etype (Subp)) then
4164 if not Is_Limited_View (Etype (Subp))
4165 and then
4166 (No (First_Formal (Subp))
4167 or else
4168 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4169 then
4170 Expand_Ctrl_Function_Call (Call_Node);
4172 -- Build-in-place function calls which appear in anonymous contexts
4173 -- need a transient scope to ensure the proper finalization of the
4174 -- intermediate result after its use.
4176 elsif Is_Build_In_Place_Function_Call (Call_Node)
4177 and then
4178 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
4179 N_Function_Call,
4180 N_Indexed_Component,
4181 N_Object_Renaming_Declaration,
4182 N_Procedure_Call_Statement,
4183 N_Selected_Component,
4184 N_Slice)
4185 then
4186 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
4187 end if;
4188 end if;
4189 end Expand_Call;
4191 -------------------------------
4192 -- Expand_Ctrl_Function_Call --
4193 -------------------------------
4195 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4196 function Is_Element_Reference (N : Node_Id) return Boolean;
4197 -- Determine whether node N denotes a reference to an Ada 2012 container
4198 -- element.
4200 --------------------------
4201 -- Is_Element_Reference --
4202 --------------------------
4204 function Is_Element_Reference (N : Node_Id) return Boolean is
4205 Ref : constant Node_Id := Original_Node (N);
4207 begin
4208 -- Analysis marks an element reference by setting the generalized
4209 -- indexing attribute of an indexed component before the component
4210 -- is rewritten into a function call.
4212 return
4213 Nkind (Ref) = N_Indexed_Component
4214 and then Present (Generalized_Indexing (Ref));
4215 end Is_Element_Reference;
4217 -- Start of processing for Expand_Ctrl_Function_Call
4219 begin
4220 -- Optimization, if the returned value (which is on the sec-stack) is
4221 -- returned again, no need to copy/readjust/finalize, we can just pass
4222 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4223 -- attachment is needed
4225 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4226 return;
4227 end if;
4229 -- Resolution is now finished, make sure we don't start analysis again
4230 -- because of the duplication.
4232 Set_Analyzed (N);
4234 -- A function which returns a controlled object uses the secondary
4235 -- stack. Rewrite the call into a temporary which obtains the result of
4236 -- the function using 'reference.
4238 Remove_Side_Effects (N);
4240 -- The side effect removal of the function call produced a temporary.
4241 -- When the context is a case expression, if expression, or expression
4242 -- with actions, the lifetime of the temporary must be extended to match
4243 -- that of the context. Otherwise the function result will be finalized
4244 -- too early and affect the result of the expression. To prevent this
4245 -- unwanted effect, the temporary should not be considered for clean up
4246 -- actions by the general finalization machinery.
4248 -- Exception to this rule are references to Ada 2012 container elements.
4249 -- Such references must be finalized at the end of each iteration of the
4250 -- related quantified expression, otherwise the container will remain
4251 -- busy.
4253 if Nkind (N) = N_Explicit_Dereference
4254 and then Within_Case_Or_If_Expression (N)
4255 and then not Is_Element_Reference (N)
4256 then
4257 Set_Is_Ignored_Transient (Entity (Prefix (N)));
4258 end if;
4259 end Expand_Ctrl_Function_Call;
4261 ----------------------------------------
4262 -- Expand_N_Extended_Return_Statement --
4263 ----------------------------------------
4265 -- If there is a Handled_Statement_Sequence, we rewrite this:
4267 -- return Result : T := <expression> do
4268 -- <handled_seq_of_stms>
4269 -- end return;
4271 -- to be:
4273 -- declare
4274 -- Result : T := <expression>;
4275 -- begin
4276 -- <handled_seq_of_stms>
4277 -- return Result;
4278 -- end;
4280 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4282 -- return Result : T := <expression>;
4284 -- to be:
4286 -- return <expression>;
4288 -- unless it's build-in-place or there's no <expression>, in which case
4289 -- we generate:
4291 -- declare
4292 -- Result : T := <expression>;
4293 -- begin
4294 -- return Result;
4295 -- end;
4297 -- Note that this case could have been written by the user as an extended
4298 -- return statement, or could have been transformed to this from a simple
4299 -- return statement.
4301 -- That is, we need to have a reified return object if there are statements
4302 -- (which might refer to it) or if we're doing build-in-place (so we can
4303 -- set its address to the final resting place or if there is no expression
4304 -- (in which case default initial values might need to be set).
4306 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4307 Loc : constant Source_Ptr := Sloc (N);
4309 function Build_Heap_Allocator
4310 (Temp_Id : Entity_Id;
4311 Temp_Typ : Entity_Id;
4312 Func_Id : Entity_Id;
4313 Ret_Typ : Entity_Id;
4314 Alloc_Expr : Node_Id) return Node_Id;
4315 -- Create the statements necessary to allocate a return object on the
4316 -- caller's master. The master is available through implicit parameter
4317 -- BIPfinalizationmaster.
4319 -- if BIPfinalizationmaster /= null then
4320 -- declare
4321 -- type Ptr_Typ is access Ret_Typ;
4322 -- for Ptr_Typ'Storage_Pool use
4323 -- Base_Pool (BIPfinalizationmaster.all).all;
4324 -- Local : Ptr_Typ;
4326 -- begin
4327 -- procedure Allocate (...) is
4328 -- begin
4329 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4330 -- end Allocate;
4332 -- Local := <Alloc_Expr>;
4333 -- Temp_Id := Temp_Typ (Local);
4334 -- end;
4335 -- end if;
4337 -- Temp_Id is the temporary which is used to reference the internally
4338 -- created object in all allocation forms. Temp_Typ is the type of the
4339 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4340 -- type of Func_Id. Alloc_Expr is the actual allocator.
4342 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
4343 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4344 -- with parameters:
4345 -- From current activation chain
4346 -- To activation chain passed in by the caller
4347 -- New_Master master passed in by the caller
4349 -- Func_Id is the entity of the function where the extended return
4350 -- statement appears.
4352 --------------------------
4353 -- Build_Heap_Allocator --
4354 --------------------------
4356 function Build_Heap_Allocator
4357 (Temp_Id : Entity_Id;
4358 Temp_Typ : Entity_Id;
4359 Func_Id : Entity_Id;
4360 Ret_Typ : Entity_Id;
4361 Alloc_Expr : Node_Id) return Node_Id
4363 begin
4364 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4366 -- Processing for build-in-place object allocation.
4368 if Needs_Finalization (Ret_Typ) then
4369 declare
4370 Decls : constant List_Id := New_List;
4371 Fin_Mas_Id : constant Entity_Id :=
4372 Build_In_Place_Formal
4373 (Func_Id, BIP_Finalization_Master);
4374 Stmts : constant List_Id := New_List;
4375 Desig_Typ : Entity_Id;
4376 Local_Id : Entity_Id;
4377 Pool_Id : Entity_Id;
4378 Ptr_Typ : Entity_Id;
4380 begin
4381 -- Generate:
4382 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4384 Pool_Id := Make_Temporary (Loc, 'P');
4386 Append_To (Decls,
4387 Make_Object_Renaming_Declaration (Loc,
4388 Defining_Identifier => Pool_Id,
4389 Subtype_Mark =>
4390 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4391 Name =>
4392 Make_Explicit_Dereference (Loc,
4393 Prefix =>
4394 Make_Function_Call (Loc,
4395 Name =>
4396 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4397 Parameter_Associations => New_List (
4398 Make_Explicit_Dereference (Loc,
4399 Prefix =>
4400 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4402 -- Create an access type which uses the storage pool of the
4403 -- caller's master. This additional type is necessary because
4404 -- the finalization master cannot be associated with the type
4405 -- of the temporary. Otherwise the secondary stack allocation
4406 -- will fail.
4408 Desig_Typ := Ret_Typ;
4410 -- Ensure that the build-in-place machinery uses a fat pointer
4411 -- when allocating an unconstrained array on the heap. In this
4412 -- case the result object type is a constrained array type even
4413 -- though the function type is unconstrained.
4415 if Ekind (Desig_Typ) = E_Array_Subtype then
4416 Desig_Typ := Base_Type (Desig_Typ);
4417 end if;
4419 -- Generate:
4420 -- type Ptr_Typ is access Desig_Typ;
4422 Ptr_Typ := Make_Temporary (Loc, 'P');
4424 Append_To (Decls,
4425 Make_Full_Type_Declaration (Loc,
4426 Defining_Identifier => Ptr_Typ,
4427 Type_Definition =>
4428 Make_Access_To_Object_Definition (Loc,
4429 Subtype_Indication =>
4430 New_Occurrence_Of (Desig_Typ, Loc))));
4432 -- Perform minor decoration in order to set the master and the
4433 -- storage pool attributes.
4435 Set_Ekind (Ptr_Typ, E_Access_Type);
4436 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4437 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4439 -- Create the temporary, generate:
4440 -- Local_Id : Ptr_Typ;
4442 Local_Id := Make_Temporary (Loc, 'T');
4444 Append_To (Decls,
4445 Make_Object_Declaration (Loc,
4446 Defining_Identifier => Local_Id,
4447 Object_Definition =>
4448 New_Occurrence_Of (Ptr_Typ, Loc)));
4450 -- Allocate the object, generate:
4451 -- Local_Id := <Alloc_Expr>;
4453 Append_To (Stmts,
4454 Make_Assignment_Statement (Loc,
4455 Name => New_Occurrence_Of (Local_Id, Loc),
4456 Expression => Alloc_Expr));
4458 -- Generate:
4459 -- Temp_Id := Temp_Typ (Local_Id);
4461 Append_To (Stmts,
4462 Make_Assignment_Statement (Loc,
4463 Name => New_Occurrence_Of (Temp_Id, Loc),
4464 Expression =>
4465 Unchecked_Convert_To (Temp_Typ,
4466 New_Occurrence_Of (Local_Id, Loc))));
4468 -- Wrap the allocation in a block. This is further conditioned
4469 -- by checking the caller finalization master at runtime. A
4470 -- null value indicates a non-existent master, most likely due
4471 -- to a Finalize_Storage_Only allocation.
4473 -- Generate:
4474 -- if BIPfinalizationmaster /= null then
4475 -- declare
4476 -- <Decls>
4477 -- begin
4478 -- <Stmts>
4479 -- end;
4480 -- end if;
4482 return
4483 Make_If_Statement (Loc,
4484 Condition =>
4485 Make_Op_Ne (Loc,
4486 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4487 Right_Opnd => Make_Null (Loc)),
4489 Then_Statements => New_List (
4490 Make_Block_Statement (Loc,
4491 Declarations => Decls,
4492 Handled_Statement_Sequence =>
4493 Make_Handled_Sequence_Of_Statements (Loc,
4494 Statements => Stmts))));
4495 end;
4497 -- For all other cases, generate:
4498 -- Temp_Id := <Alloc_Expr>;
4500 else
4501 return
4502 Make_Assignment_Statement (Loc,
4503 Name => New_Occurrence_Of (Temp_Id, Loc),
4504 Expression => Alloc_Expr);
4505 end if;
4506 end Build_Heap_Allocator;
4508 ---------------------------
4509 -- Move_Activation_Chain --
4510 ---------------------------
4512 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
4513 begin
4514 return
4515 Make_Procedure_Call_Statement (Loc,
4516 Name =>
4517 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4519 Parameter_Associations => New_List (
4521 -- Source chain
4523 Make_Attribute_Reference (Loc,
4524 Prefix => Make_Identifier (Loc, Name_uChain),
4525 Attribute_Name => Name_Unrestricted_Access),
4527 -- Destination chain
4529 New_Occurrence_Of
4530 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
4532 -- New master
4534 New_Occurrence_Of
4535 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
4536 end Move_Activation_Chain;
4538 -- Local variables
4540 Func_Id : constant Entity_Id :=
4541 Return_Applies_To (Return_Statement_Entity (N));
4542 Is_BIP_Func : constant Boolean :=
4543 Is_Build_In_Place_Function (Func_Id);
4544 Ret_Obj_Id : constant Entity_Id :=
4545 First_Entity (Return_Statement_Entity (N));
4546 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4547 Ret_Typ : constant Entity_Id := Etype (Func_Id);
4549 Exp : Node_Id;
4550 HSS : Node_Id;
4551 Result : Node_Id;
4552 Return_Stmt : Node_Id;
4553 Stmts : List_Id;
4555 -- Start of processing for Expand_N_Extended_Return_Statement
4557 begin
4558 -- Given that functionality of interface thunks is simple (just displace
4559 -- the pointer to the object) they are always handled by means of
4560 -- simple return statements.
4562 pragma Assert (not Is_Thunk (Current_Scope));
4564 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4565 Exp := Expression (Ret_Obj_Decl);
4566 else
4567 Exp := Empty;
4568 end if;
4570 HSS := Handled_Statement_Sequence (N);
4572 -- If the returned object needs finalization actions, the function must
4573 -- perform the appropriate cleanup should it fail to return. The state
4574 -- of the function itself is tracked through a flag which is coupled
4575 -- with the scope finalizer. There is one flag per each return object
4576 -- in case of multiple returns.
4578 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4579 declare
4580 Flag_Decl : Node_Id;
4581 Flag_Id : Entity_Id;
4582 Func_Bod : Node_Id;
4584 begin
4585 -- Recover the function body
4587 Func_Bod := Unit_Declaration_Node (Func_Id);
4589 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4590 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4591 end if;
4593 -- Create a flag to track the function state
4595 Flag_Id := Make_Temporary (Loc, 'F');
4596 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4598 -- Insert the flag at the beginning of the function declarations,
4599 -- generate:
4600 -- Fnn : Boolean := False;
4602 Flag_Decl :=
4603 Make_Object_Declaration (Loc,
4604 Defining_Identifier => Flag_Id,
4605 Object_Definition =>
4606 New_Occurrence_Of (Standard_Boolean, Loc),
4607 Expression =>
4608 New_Occurrence_Of (Standard_False, Loc));
4610 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4611 Analyze (Flag_Decl);
4612 end;
4613 end if;
4615 -- Build a simple_return_statement that returns the return object when
4616 -- there is a statement sequence, or no expression, or the result will
4617 -- be built in place. Note however that we currently do this for all
4618 -- composite cases, even though nonlimited composite results are not yet
4619 -- built in place (though we plan to do so eventually).
4621 if Present (HSS)
4622 or else Is_Composite_Type (Ret_Typ)
4623 or else No (Exp)
4624 then
4625 if No (HSS) then
4626 Stmts := New_List;
4628 -- If the extended return has a handled statement sequence, then wrap
4629 -- it in a block and use the block as the first statement.
4631 else
4632 Stmts := New_List (
4633 Make_Block_Statement (Loc,
4634 Declarations => New_List,
4635 Handled_Statement_Sequence => HSS));
4636 end if;
4638 -- If the result type contains tasks, we call Move_Activation_Chain.
4639 -- Later, the cleanup code will call Complete_Master, which will
4640 -- terminate any unactivated tasks belonging to the return statement
4641 -- master. But Move_Activation_Chain updates their master to be that
4642 -- of the caller, so they will not be terminated unless the return
4643 -- statement completes unsuccessfully due to exception, abort, goto,
4644 -- or exit. As a formality, we test whether the function requires the
4645 -- result to be built in place, though that's necessarily true for
4646 -- the case of result types with task parts.
4648 if Is_BIP_Func and then Has_Task (Ret_Typ) then
4650 -- The return expression is an aggregate for a complex type which
4651 -- contains tasks. This particular case is left unexpanded since
4652 -- the regular expansion would insert all temporaries and
4653 -- initialization code in the wrong block.
4655 if Nkind (Exp) = N_Aggregate then
4656 Expand_N_Aggregate (Exp);
4657 end if;
4659 -- Do not move the activation chain if the return object does not
4660 -- contain tasks.
4662 if Has_Task (Etype (Ret_Obj_Id)) then
4663 Append_To (Stmts, Move_Activation_Chain (Func_Id));
4664 end if;
4665 end if;
4667 -- Update the state of the function right before the object is
4668 -- returned.
4670 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4671 declare
4672 Flag_Id : constant Entity_Id :=
4673 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4675 begin
4676 -- Generate:
4677 -- Fnn := True;
4679 Append_To (Stmts,
4680 Make_Assignment_Statement (Loc,
4681 Name => New_Occurrence_Of (Flag_Id, Loc),
4682 Expression => New_Occurrence_Of (Standard_True, Loc)));
4683 end;
4684 end if;
4686 -- Build a simple_return_statement that returns the return object
4688 Return_Stmt :=
4689 Make_Simple_Return_Statement (Loc,
4690 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4691 Append_To (Stmts, Return_Stmt);
4693 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4694 end if;
4696 -- Case where we build a return statement block
4698 if Present (HSS) then
4699 Result :=
4700 Make_Block_Statement (Loc,
4701 Declarations => Return_Object_Declarations (N),
4702 Handled_Statement_Sequence => HSS);
4704 -- We set the entity of the new block statement to be that of the
4705 -- return statement. This is necessary so that various fields, such
4706 -- as Finalization_Chain_Entity carry over from the return statement
4707 -- to the block. Note that this block is unusual, in that its entity
4708 -- is an E_Return_Statement rather than an E_Block.
4710 Set_Identifier
4711 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4713 -- If the object decl was already rewritten as a renaming, then we
4714 -- don't want to do the object allocation and transformation of
4715 -- the return object declaration to a renaming. This case occurs
4716 -- when the return object is initialized by a call to another
4717 -- build-in-place function, and that function is responsible for
4718 -- the allocation of the return object.
4720 if Is_BIP_Func
4721 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4722 then
4723 pragma Assert
4724 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4725 and then Is_Build_In_Place_Function_Call
4726 (Expression (Original_Node (Ret_Obj_Decl))));
4728 -- Return the build-in-place result by reference
4730 Set_By_Ref (Return_Stmt);
4732 elsif Is_BIP_Func then
4734 -- Locate the implicit access parameter associated with the
4735 -- caller-supplied return object and convert the return
4736 -- statement's return object declaration to a renaming of a
4737 -- dereference of the access parameter. If the return object's
4738 -- declaration includes an expression that has not already been
4739 -- expanded as separate assignments, then add an assignment
4740 -- statement to ensure the return object gets initialized.
4742 -- declare
4743 -- Result : T [:= <expression>];
4744 -- begin
4745 -- ...
4747 -- is converted to
4749 -- declare
4750 -- Result : T renames FuncRA.all;
4751 -- [Result := <expression;]
4752 -- begin
4753 -- ...
4755 declare
4756 Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl);
4757 Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id);
4759 Init_Assignment : Node_Id := Empty;
4760 Obj_Acc_Formal : Entity_Id;
4761 Obj_Acc_Deref : Node_Id;
4762 Obj_Alloc_Formal : Entity_Id;
4764 begin
4765 -- Build-in-place results must be returned by reference
4767 Set_By_Ref (Return_Stmt);
4769 -- Retrieve the implicit access parameter passed by the caller
4771 Obj_Acc_Formal :=
4772 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
4774 -- If the return object's declaration includes an expression
4775 -- and the declaration isn't marked as No_Initialization, then
4776 -- we need to generate an assignment to the object and insert
4777 -- it after the declaration before rewriting it as a renaming
4778 -- (otherwise we'll lose the initialization). The case where
4779 -- the result type is an interface (or class-wide interface)
4780 -- is also excluded because the context of the function call
4781 -- must be unconstrained, so the initialization will always
4782 -- be done as part of an allocator evaluation (storage pool
4783 -- or secondary stack), never to a constrained target object
4784 -- passed in by the caller. Besides the assignment being
4785 -- unneeded in this case, it avoids problems with trying to
4786 -- generate a dispatching assignment when the return expression
4787 -- is a nonlimited descendant of a limited interface (the
4788 -- interface has no assignment operation).
4790 if Present (Ret_Obj_Expr)
4791 and then not No_Initialization (Ret_Obj_Decl)
4792 and then not Is_Interface (Ret_Obj_Typ)
4793 then
4794 Init_Assignment :=
4795 Make_Assignment_Statement (Loc,
4796 Name => New_Occurrence_Of (Ret_Obj_Id, Loc),
4797 Expression => Relocate_Node (Ret_Obj_Expr));
4799 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
4800 Set_Assignment_OK (Name (Init_Assignment));
4801 Set_No_Ctrl_Actions (Init_Assignment);
4803 Set_Parent (Name (Init_Assignment), Init_Assignment);
4804 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4806 Set_Expression (Ret_Obj_Decl, Empty);
4808 if Is_Class_Wide_Type (Etype (Ret_Obj_Id))
4809 and then not Is_Class_Wide_Type
4810 (Etype (Expression (Init_Assignment)))
4811 then
4812 Rewrite (Expression (Init_Assignment),
4813 Make_Type_Conversion (Loc,
4814 Subtype_Mark =>
4815 New_Occurrence_Of (Etype (Ret_Obj_Id), Loc),
4816 Expression =>
4817 Relocate_Node (Expression (Init_Assignment))));
4818 end if;
4820 -- In the case of functions where the calling context can
4821 -- determine the form of allocation needed, initialization
4822 -- is done with each part of the if statement that handles
4823 -- the different forms of allocation (this is true for
4824 -- unconstrained and tagged result subtypes).
4826 if Is_Constrained (Ret_Typ)
4827 and then not Is_Tagged_Type (Underlying_Type (Ret_Typ))
4828 then
4829 Insert_After (Ret_Obj_Decl, Init_Assignment);
4830 end if;
4831 end if;
4833 -- When the function's subtype is unconstrained, a run-time
4834 -- test is needed to determine the form of allocation to use
4835 -- for the return object. The function has an implicit formal
4836 -- parameter indicating this. If the BIP_Alloc_Form formal has
4837 -- the value one, then the caller has passed access to an
4838 -- existing object for use as the return object. If the value
4839 -- is two, then the return object must be allocated on the
4840 -- secondary stack. Otherwise, the object must be allocated in
4841 -- a storage pool (currently only supported for the global
4842 -- heap, user-defined storage pools TBD ???). We generate an
4843 -- if statement to test the implicit allocation formal and
4844 -- initialize a local access value appropriately, creating
4845 -- allocators in the secondary stack and global heap cases.
4846 -- The special formal also exists and must be tested when the
4847 -- function has a tagged result, even when the result subtype
4848 -- is constrained, because in general such functions can be
4849 -- called in dispatching contexts and must be handled similarly
4850 -- to functions with a class-wide result.
4852 if not Is_Constrained (Ret_Typ)
4853 or else Is_Tagged_Type (Underlying_Type (Ret_Typ))
4854 then
4855 Obj_Alloc_Formal :=
4856 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
4858 declare
4859 Pool_Id : constant Entity_Id :=
4860 Make_Temporary (Loc, 'P');
4861 Alloc_Obj_Id : Entity_Id;
4862 Alloc_Obj_Decl : Node_Id;
4863 Alloc_If_Stmt : Node_Id;
4864 Heap_Allocator : Node_Id;
4865 Pool_Decl : Node_Id;
4866 Pool_Allocator : Node_Id;
4867 Ptr_Type_Decl : Node_Id;
4868 Ref_Type : Entity_Id;
4869 SS_Allocator : Node_Id;
4871 begin
4872 -- Reuse the itype created for the function's implicit
4873 -- access formal. This avoids the need to create a new
4874 -- access type here, plus it allows assigning the access
4875 -- formal directly without applying a conversion.
4877 -- Ref_Type := Etype (Object_Access);
4879 -- Create an access type designating the function's
4880 -- result subtype.
4882 Ref_Type := Make_Temporary (Loc, 'A');
4884 Ptr_Type_Decl :=
4885 Make_Full_Type_Declaration (Loc,
4886 Defining_Identifier => Ref_Type,
4887 Type_Definition =>
4888 Make_Access_To_Object_Definition (Loc,
4889 All_Present => True,
4890 Subtype_Indication =>
4891 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
4893 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
4895 -- Create an access object that will be initialized to an
4896 -- access value denoting the return object, either coming
4897 -- from an implicit access value passed in by the caller
4898 -- or from the result of an allocator.
4900 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4901 Set_Etype (Alloc_Obj_Id, Ref_Type);
4903 Alloc_Obj_Decl :=
4904 Make_Object_Declaration (Loc,
4905 Defining_Identifier => Alloc_Obj_Id,
4906 Object_Definition =>
4907 New_Occurrence_Of (Ref_Type, Loc));
4909 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
4911 -- Create allocators for both the secondary stack and
4912 -- global heap. If there's an initialization expression,
4913 -- then create these as initialized allocators.
4915 if Present (Ret_Obj_Expr)
4916 and then not No_Initialization (Ret_Obj_Decl)
4917 then
4918 -- Always use the type of the expression for the
4919 -- qualified expression, rather than the result type.
4920 -- In general we cannot always use the result type
4921 -- for the allocator, because the expression might be
4922 -- of a specific type, such as in the case of an
4923 -- aggregate or even a nonlimited object when the
4924 -- result type is a limited class-wide interface type.
4926 Heap_Allocator :=
4927 Make_Allocator (Loc,
4928 Expression =>
4929 Make_Qualified_Expression (Loc,
4930 Subtype_Mark =>
4931 New_Occurrence_Of
4932 (Etype (Ret_Obj_Expr), Loc),
4933 Expression => New_Copy_Tree (Ret_Obj_Expr)));
4935 else
4936 -- If the function returns a class-wide type we cannot
4937 -- use the return type for the allocator. Instead we
4938 -- use the type of the expression, which must be an
4939 -- aggregate of a definite type.
4941 if Is_Class_Wide_Type (Ret_Obj_Typ) then
4942 Heap_Allocator :=
4943 Make_Allocator (Loc,
4944 Expression =>
4945 New_Occurrence_Of
4946 (Etype (Ret_Obj_Expr), Loc));
4947 else
4948 Heap_Allocator :=
4949 Make_Allocator (Loc,
4950 Expression =>
4951 New_Occurrence_Of (Ret_Obj_Typ, Loc));
4952 end if;
4954 -- If the object requires default initialization then
4955 -- that will happen later following the elaboration of
4956 -- the object renaming. If we don't turn it off here
4957 -- then the object will be default initialized twice.
4959 Set_No_Initialization (Heap_Allocator);
4960 end if;
4962 -- The Pool_Allocator is just like the Heap_Allocator,
4963 -- except we set Storage_Pool and Procedure_To_Call so
4964 -- it will use the user-defined storage pool.
4966 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
4968 -- Do not generate the renaming of the build-in-place
4969 -- pool parameter on ZFP because the parameter is not
4970 -- created in the first place.
4972 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
4973 Pool_Decl :=
4974 Make_Object_Renaming_Declaration (Loc,
4975 Defining_Identifier => Pool_Id,
4976 Subtype_Mark =>
4977 New_Occurrence_Of
4978 (RTE (RE_Root_Storage_Pool), Loc),
4979 Name =>
4980 Make_Explicit_Dereference (Loc,
4981 New_Occurrence_Of
4982 (Build_In_Place_Formal
4983 (Func_Id, BIP_Storage_Pool), Loc)));
4984 Set_Storage_Pool (Pool_Allocator, Pool_Id);
4985 Set_Procedure_To_Call
4986 (Pool_Allocator, RTE (RE_Allocate_Any));
4987 else
4988 Pool_Decl := Make_Null_Statement (Loc);
4989 end if;
4991 -- If the No_Allocators restriction is active, then only
4992 -- an allocator for secondary stack allocation is needed.
4993 -- It's OK for such allocators to have Comes_From_Source
4994 -- set to False, because gigi knows not to flag them as
4995 -- being a violation of No_Implicit_Heap_Allocations.
4997 if Restriction_Active (No_Allocators) then
4998 SS_Allocator := Heap_Allocator;
4999 Heap_Allocator := Make_Null (Loc);
5000 Pool_Allocator := Make_Null (Loc);
5002 -- Otherwise the heap and pool allocators may be needed,
5003 -- so we make another allocator for secondary stack
5004 -- allocation.
5006 else
5007 SS_Allocator := New_Copy_Tree (Heap_Allocator);
5009 -- The heap and pool allocators are marked as
5010 -- Comes_From_Source since they correspond to an
5011 -- explicit user-written allocator (that is, it will
5012 -- only be executed on behalf of callers that call the
5013 -- function as initialization for such an allocator).
5014 -- Prevents errors when No_Implicit_Heap_Allocations
5015 -- is in force.
5017 Set_Comes_From_Source (Heap_Allocator, True);
5018 Set_Comes_From_Source (Pool_Allocator, True);
5019 end if;
5021 -- The allocator is returned on the secondary stack.
5023 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5024 Set_Procedure_To_Call
5025 (SS_Allocator, RTE (RE_SS_Allocate));
5027 -- The allocator is returned on the secondary stack,
5028 -- so indicate that the function return, as well as
5029 -- all blocks that encloses the allocator, must not
5030 -- release it. The flags must be set now because
5031 -- the decision to use the secondary stack is done
5032 -- very late in the course of expanding the return
5033 -- statement, past the point where these flags are
5034 -- normally set.
5036 Set_Uses_Sec_Stack (Func_Id);
5037 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5038 Set_Sec_Stack_Needed_For_Return
5039 (Return_Statement_Entity (N));
5040 Set_Enclosing_Sec_Stack_Return (N);
5042 -- Create an if statement to test the BIP_Alloc_Form
5043 -- formal and initialize the access object to either the
5044 -- BIP_Object_Access formal (BIP_Alloc_Form =
5045 -- Caller_Allocation), the result of allocating the
5046 -- object in the secondary stack (BIP_Alloc_Form =
5047 -- Secondary_Stack), or else an allocator to create the
5048 -- return object in the heap or user-defined pool
5049 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5051 -- ??? An unchecked type conversion must be made in the
5052 -- case of assigning the access object formal to the
5053 -- local access object, because a normal conversion would
5054 -- be illegal in some cases (such as converting access-
5055 -- to-unconstrained to access-to-constrained), but the
5056 -- the unchecked conversion will presumably fail to work
5057 -- right in just such cases. It's not clear at all how to
5058 -- handle this. ???
5060 Alloc_If_Stmt :=
5061 Make_If_Statement (Loc,
5062 Condition =>
5063 Make_Op_Eq (Loc,
5064 Left_Opnd =>
5065 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5066 Right_Opnd =>
5067 Make_Integer_Literal (Loc,
5068 UI_From_Int (BIP_Allocation_Form'Pos
5069 (Caller_Allocation)))),
5071 Then_Statements => New_List (
5072 Make_Assignment_Statement (Loc,
5073 Name =>
5074 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5075 Expression =>
5076 Make_Unchecked_Type_Conversion (Loc,
5077 Subtype_Mark =>
5078 New_Occurrence_Of (Ref_Type, Loc),
5079 Expression =>
5080 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
5082 Elsif_Parts => New_List (
5083 Make_Elsif_Part (Loc,
5084 Condition =>
5085 Make_Op_Eq (Loc,
5086 Left_Opnd =>
5087 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5088 Right_Opnd =>
5089 Make_Integer_Literal (Loc,
5090 UI_From_Int (BIP_Allocation_Form'Pos
5091 (Secondary_Stack)))),
5093 Then_Statements => New_List (
5094 Make_Assignment_Statement (Loc,
5095 Name =>
5096 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5097 Expression => SS_Allocator))),
5099 Make_Elsif_Part (Loc,
5100 Condition =>
5101 Make_Op_Eq (Loc,
5102 Left_Opnd =>
5103 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5104 Right_Opnd =>
5105 Make_Integer_Literal (Loc,
5106 UI_From_Int (BIP_Allocation_Form'Pos
5107 (Global_Heap)))),
5109 Then_Statements => New_List (
5110 Build_Heap_Allocator
5111 (Temp_Id => Alloc_Obj_Id,
5112 Temp_Typ => Ref_Type,
5113 Func_Id => Func_Id,
5114 Ret_Typ => Ret_Obj_Typ,
5115 Alloc_Expr => Heap_Allocator)))),
5117 Else_Statements => New_List (
5118 Pool_Decl,
5119 Build_Heap_Allocator
5120 (Temp_Id => Alloc_Obj_Id,
5121 Temp_Typ => Ref_Type,
5122 Func_Id => Func_Id,
5123 Ret_Typ => Ret_Obj_Typ,
5124 Alloc_Expr => Pool_Allocator)));
5126 -- If a separate initialization assignment was created
5127 -- earlier, append that following the assignment of the
5128 -- implicit access formal to the access object, to ensure
5129 -- that the return object is initialized in that case. In
5130 -- this situation, the target of the assignment must be
5131 -- rewritten to denote a dereference of the access to the
5132 -- return object passed in by the caller.
5134 if Present (Init_Assignment) then
5135 Rewrite (Name (Init_Assignment),
5136 Make_Explicit_Dereference (Loc,
5137 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
5139 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5141 Append_To
5142 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5143 end if;
5145 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5147 -- Remember the local access object for use in the
5148 -- dereference of the renaming created below.
5150 Obj_Acc_Formal := Alloc_Obj_Id;
5151 end;
5152 end if;
5154 -- Replace the return object declaration with a renaming of a
5155 -- dereference of the access value designating the return
5156 -- object.
5158 Obj_Acc_Deref :=
5159 Make_Explicit_Dereference (Loc,
5160 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
5162 Rewrite (Ret_Obj_Decl,
5163 Make_Object_Renaming_Declaration (Loc,
5164 Defining_Identifier => Ret_Obj_Id,
5165 Access_Definition => Empty,
5166 Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc),
5167 Name => Obj_Acc_Deref));
5169 Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref);
5170 end;
5171 end if;
5173 -- Case where we do not build a block
5175 else
5176 -- We're about to drop Return_Object_Declarations on the floor, so
5177 -- we need to insert it, in case it got expanded into useful code.
5178 -- Remove side effects from expression, which may be duplicated in
5179 -- subsequent checks (see Expand_Simple_Function_Return).
5181 Insert_List_Before (N, Return_Object_Declarations (N));
5182 Remove_Side_Effects (Exp);
5184 -- Build simple_return_statement that returns the expression directly
5186 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5187 Result := Return_Stmt;
5188 end if;
5190 -- Set the flag to prevent infinite recursion
5192 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5194 Rewrite (N, Result);
5195 Analyze (N);
5196 end Expand_N_Extended_Return_Statement;
5198 ----------------------------
5199 -- Expand_N_Function_Call --
5200 ----------------------------
5202 procedure Expand_N_Function_Call (N : Node_Id) is
5203 begin
5204 Expand_Call (N);
5205 end Expand_N_Function_Call;
5207 ---------------------------------------
5208 -- Expand_N_Procedure_Call_Statement --
5209 ---------------------------------------
5211 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5212 begin
5213 Expand_Call (N);
5214 end Expand_N_Procedure_Call_Statement;
5216 --------------------------------------
5217 -- Expand_N_Simple_Return_Statement --
5218 --------------------------------------
5220 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5221 begin
5222 -- Defend against previous errors (i.e. the return statement calls a
5223 -- function that is not available in configurable runtime).
5225 if Present (Expression (N))
5226 and then Nkind (Expression (N)) = N_Empty
5227 then
5228 Check_Error_Detected;
5229 return;
5230 end if;
5232 -- Distinguish the function and non-function cases:
5234 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5235 when E_Function
5236 | E_Generic_Function
5238 Expand_Simple_Function_Return (N);
5240 when E_Entry
5241 | E_Entry_Family
5242 | E_Generic_Procedure
5243 | E_Procedure
5244 | E_Return_Statement
5246 Expand_Non_Function_Return (N);
5248 when others =>
5249 raise Program_Error;
5250 end case;
5252 exception
5253 when RE_Not_Available =>
5254 return;
5255 end Expand_N_Simple_Return_Statement;
5257 ------------------------------
5258 -- Expand_N_Subprogram_Body --
5259 ------------------------------
5261 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5262 -- by the back-end.
5264 -- Add dummy push/pop label nodes at start and end to clear any local
5265 -- exception indications if local-exception-to-goto optimization is active.
5267 -- Add return statement if last statement in body is not a return statement
5268 -- (this makes things easier on Gigi which does not want to have to handle
5269 -- a missing return).
5271 -- Add call to Activate_Tasks if body is a task activator
5273 -- Deal with possible detection of infinite recursion
5275 -- Eliminate body completely if convention stubbed
5277 -- Encode entity names within body, since we will not need to reference
5278 -- these entities any longer in the front end.
5280 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5282 -- Reset Pure indication if any parameter has root type System.Address
5283 -- or has any parameters of limited types, where limited means that the
5284 -- run-time view is limited (i.e. the full type is limited).
5286 -- Wrap thread body
5288 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5289 Body_Id : constant Entity_Id := Defining_Entity (N);
5290 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5291 Loc : constant Source_Ptr := Sloc (N);
5293 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5294 -- Append a return statement to the statement sequence Stmts if the last
5295 -- statement is not already a return or a goto statement. Note that the
5296 -- latter test is not critical, it does not matter if we add a few extra
5297 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5298 -- the corresponding spec of the subprogram body.
5300 ----------------
5301 -- Add_Return --
5302 ----------------
5304 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5305 Last_Stmt : Node_Id;
5306 Loc : Source_Ptr;
5307 Stmt : Node_Id;
5309 begin
5310 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5311 -- not relevant in this context since they are not executable.
5313 Last_Stmt := Last (Stmts);
5314 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5315 Prev (Last_Stmt);
5316 end loop;
5318 -- Now insert return unless last statement is a transfer
5320 if not Is_Transfer (Last_Stmt) then
5322 -- The source location for the return is the end label of the
5323 -- procedure if present. Otherwise use the sloc of the last
5324 -- statement in the list. If the list comes from a generated
5325 -- exception handler and we are not debugging generated code,
5326 -- all the statements within the handler are made invisible
5327 -- to the debugger.
5329 if Nkind (Parent (Stmts)) = N_Exception_Handler
5330 and then not Comes_From_Source (Parent (Stmts))
5331 then
5332 Loc := Sloc (Last_Stmt);
5333 elsif Present (End_Label (HSS)) then
5334 Loc := Sloc (End_Label (HSS));
5335 else
5336 Loc := Sloc (Last_Stmt);
5337 end if;
5339 -- Append return statement, and set analyzed manually. We can't
5340 -- call Analyze on this return since the scope is wrong.
5342 -- Note: it almost works to push the scope and then do the Analyze
5343 -- call, but something goes wrong in some weird cases and it is
5344 -- not worth worrying about ???
5346 Stmt := Make_Simple_Return_Statement (Loc);
5348 -- The return statement is handled properly, and the call to the
5349 -- postcondition, inserted below, does not require information
5350 -- from the body either. However, that call is analyzed in the
5351 -- enclosing scope, and an elaboration check might improperly be
5352 -- added to it. A guard in Sem_Elab is needed to prevent that
5353 -- spurious check, see Check_Elab_Call.
5355 Append_To (Stmts, Stmt);
5356 Set_Analyzed (Stmt);
5358 -- Call the _Postconditions procedure if the related subprogram
5359 -- has contract assertions that need to be verified on exit.
5361 if Ekind (Spec_Id) = E_Procedure
5362 and then Present (Postconditions_Proc (Spec_Id))
5363 then
5364 Insert_Action (Stmt,
5365 Make_Procedure_Call_Statement (Loc,
5366 Name =>
5367 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5368 end if;
5369 end if;
5370 end Add_Return;
5372 -- Local variables
5374 Except_H : Node_Id;
5375 L : List_Id;
5376 Spec_Id : Entity_Id;
5378 -- Start of processing for Expand_N_Subprogram_Body
5380 begin
5381 if Present (Corresponding_Spec (N)) then
5382 Spec_Id := Corresponding_Spec (N);
5383 else
5384 Spec_Id := Body_Id;
5385 end if;
5387 -- If this is a Pure function which has any parameters whose root type
5388 -- is System.Address, reset the Pure indication.
5389 -- This check is also performed when the subprogram is frozen, but we
5390 -- repeat it on the body so that the indication is consistent, and so
5391 -- it applies as well to bodies without separate specifications.
5393 if Is_Pure (Spec_Id)
5394 and then Is_Subprogram (Spec_Id)
5395 and then not Has_Pragma_Pure_Function (Spec_Id)
5396 then
5397 Check_Function_With_Address_Parameter (Spec_Id);
5399 if Spec_Id /= Body_Id then
5400 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5401 end if;
5402 end if;
5404 -- Set L to either the list of declarations if present, or to the list
5405 -- of statements if no declarations are present. This is used to insert
5406 -- new stuff at the start.
5408 if Is_Non_Empty_List (Declarations (N)) then
5409 L := Declarations (N);
5410 else
5411 L := Statements (HSS);
5412 end if;
5414 -- If local-exception-to-goto optimization active, insert dummy push
5415 -- statements at start, and dummy pop statements at end, but inhibit
5416 -- this if we have No_Exception_Handlers, since they are useless and
5417 -- intefere with analysis, e.g. by codepeer.
5419 if (Debug_Flag_Dot_G
5420 or else Restriction_Active (No_Exception_Propagation))
5421 and then not Restriction_Active (No_Exception_Handlers)
5422 and then not CodePeer_Mode
5423 and then Is_Non_Empty_List (L)
5424 then
5425 declare
5426 FS : constant Node_Id := First (L);
5427 FL : constant Source_Ptr := Sloc (FS);
5428 LS : Node_Id;
5429 LL : Source_Ptr;
5431 begin
5432 -- LS points to either last statement, if statements are present
5433 -- or to the last declaration if there are no statements present.
5434 -- It is the node after which the pop's are generated.
5436 if Is_Non_Empty_List (Statements (HSS)) then
5437 LS := Last (Statements (HSS));
5438 else
5439 LS := Last (L);
5440 end if;
5442 LL := Sloc (LS);
5444 Insert_List_Before_And_Analyze (FS, New_List (
5445 Make_Push_Constraint_Error_Label (FL),
5446 Make_Push_Program_Error_Label (FL),
5447 Make_Push_Storage_Error_Label (FL)));
5449 Insert_List_After_And_Analyze (LS, New_List (
5450 Make_Pop_Constraint_Error_Label (LL),
5451 Make_Pop_Program_Error_Label (LL),
5452 Make_Pop_Storage_Error_Label (LL)));
5453 end;
5454 end if;
5456 -- Need poll on entry to subprogram if polling enabled. We only do this
5457 -- for non-empty subprograms, since it does not seem necessary to poll
5458 -- for a dummy null subprogram.
5460 if Is_Non_Empty_List (L) then
5462 -- Do not add a polling call if the subprogram is to be inlined by
5463 -- the back-end, to avoid repeated calls with multiple inlinings.
5465 if Is_Inlined (Spec_Id)
5466 and then Front_End_Inlining
5467 and then Optimization_Level > 1
5468 then
5469 null;
5470 else
5471 Generate_Poll_Call (First (L));
5472 end if;
5473 end if;
5475 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5477 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5478 declare
5479 F : Entity_Id;
5480 A : Node_Id;
5482 begin
5483 -- Loop through formals
5485 F := First_Formal (Spec_Id);
5486 while Present (F) loop
5487 if Is_Scalar_Type (Etype (F))
5488 and then Ekind (F) = E_Out_Parameter
5489 then
5490 Check_Restriction (No_Default_Initialization, F);
5492 -- Insert the initialization. We turn off validity checks
5493 -- for this assignment, since we do not want any check on
5494 -- the initial value itself (which may well be invalid).
5495 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5497 A :=
5498 Make_Assignment_Statement (Loc,
5499 Name => New_Occurrence_Of (F, Loc),
5500 Expression => Get_Simple_Init_Val (Etype (F), N));
5501 Set_Suppress_Assignment_Checks (A);
5503 Insert_Before_And_Analyze (First (L),
5504 A, Suppress => Validity_Check);
5505 end if;
5507 Next_Formal (F);
5508 end loop;
5509 end;
5510 end if;
5512 -- Clear out statement list for stubbed procedure
5514 if Present (Corresponding_Spec (N)) then
5515 Set_Elaboration_Flag (N, Spec_Id);
5517 if Convention (Spec_Id) = Convention_Stubbed
5518 or else Is_Eliminated (Spec_Id)
5519 then
5520 Set_Declarations (N, Empty_List);
5521 Set_Handled_Statement_Sequence (N,
5522 Make_Handled_Sequence_Of_Statements (Loc,
5523 Statements => New_List (Make_Null_Statement (Loc))));
5525 return;
5526 end if;
5527 end if;
5529 -- Create a set of discriminals for the next protected subprogram body
5531 if Is_List_Member (N)
5532 and then Present (Parent (List_Containing (N)))
5533 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5534 and then Present (Next_Protected_Operation (N))
5535 then
5536 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5537 end if;
5539 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5540 -- subprograms with no specs are not frozen.
5542 declare
5543 Typ : constant Entity_Id := Etype (Spec_Id);
5544 Utyp : constant Entity_Id := Underlying_Type (Typ);
5546 begin
5547 if Is_Limited_View (Typ) then
5548 Set_Returns_By_Ref (Spec_Id);
5550 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5551 Set_Returns_By_Ref (Spec_Id);
5552 end if;
5553 end;
5555 -- For a procedure, we add a return for all possible syntactic ends of
5556 -- the subprogram.
5558 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5559 Add_Return (Spec_Id, Statements (HSS));
5561 if Present (Exception_Handlers (HSS)) then
5562 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5563 while Present (Except_H) loop
5564 Add_Return (Spec_Id, Statements (Except_H));
5565 Next_Non_Pragma (Except_H);
5566 end loop;
5567 end if;
5569 -- For a function, we must deal with the case where there is at least
5570 -- one missing return. What we do is to wrap the entire body of the
5571 -- function in a block:
5573 -- begin
5574 -- ...
5575 -- end;
5577 -- becomes
5579 -- begin
5580 -- begin
5581 -- ...
5582 -- end;
5584 -- raise Program_Error;
5585 -- end;
5587 -- This approach is necessary because the raise must be signalled to the
5588 -- caller, not handled by any local handler (RM 6.4(11)).
5590 -- Note: we do not need to analyze the constructed sequence here, since
5591 -- it has no handler, and an attempt to analyze the handled statement
5592 -- sequence twice is risky in various ways (e.g. the issue of expanding
5593 -- cleanup actions twice).
5595 elsif Has_Missing_Return (Spec_Id) then
5596 declare
5597 Hloc : constant Source_Ptr := Sloc (HSS);
5598 Blok : constant Node_Id :=
5599 Make_Block_Statement (Hloc,
5600 Handled_Statement_Sequence => HSS);
5601 Rais : constant Node_Id :=
5602 Make_Raise_Program_Error (Hloc,
5603 Reason => PE_Missing_Return);
5605 begin
5606 Set_Handled_Statement_Sequence (N,
5607 Make_Handled_Sequence_Of_Statements (Hloc,
5608 Statements => New_List (Blok, Rais)));
5610 Push_Scope (Spec_Id);
5611 Analyze (Blok);
5612 Analyze (Rais);
5613 Pop_Scope;
5614 end;
5615 end if;
5617 -- If subprogram contains a parameterless recursive call, then we may
5618 -- have an infinite recursion, so see if we can generate code to check
5619 -- for this possibility if storage checks are not suppressed.
5621 if Ekind (Spec_Id) = E_Procedure
5622 and then Has_Recursive_Call (Spec_Id)
5623 and then not Storage_Checks_Suppressed (Spec_Id)
5624 then
5625 Detect_Infinite_Recursion (N, Spec_Id);
5626 end if;
5628 -- Set to encode entity names in package body before gigi is called
5630 Qualify_Entity_Names (N);
5631 end Expand_N_Subprogram_Body;
5633 -----------------------------------
5634 -- Expand_N_Subprogram_Body_Stub --
5635 -----------------------------------
5637 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5638 Bod : Node_Id;
5640 begin
5641 if Present (Corresponding_Body (N)) then
5642 Bod := Unit_Declaration_Node (Corresponding_Body (N));
5644 -- The body may have been expanded already when it is analyzed
5645 -- through the subunit node. Do no expand again: it interferes
5646 -- with the construction of unnesting tables when generating C.
5648 if not Analyzed (Bod) then
5649 Expand_N_Subprogram_Body (Bod);
5650 end if;
5652 -- Add full qualification to entities that may be created late
5653 -- during unnesting.
5655 Qualify_Entity_Names (N);
5656 end if;
5657 end Expand_N_Subprogram_Body_Stub;
5659 -------------------------------------
5660 -- Expand_N_Subprogram_Declaration --
5661 -------------------------------------
5663 -- If the declaration appears within a protected body, it is a private
5664 -- operation of the protected type. We must create the corresponding
5665 -- protected subprogram an associated formals. For a normal protected
5666 -- operation, this is done when expanding the protected type declaration.
5668 -- If the declaration is for a null procedure, emit null body
5670 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5671 Loc : constant Source_Ptr := Sloc (N);
5672 Subp : constant Entity_Id := Defining_Entity (N);
5674 -- Local variables
5676 Scop : constant Entity_Id := Scope (Subp);
5677 Prot_Bod : Node_Id;
5678 Prot_Decl : Node_Id;
5679 Prot_Id : Entity_Id;
5681 -- Start of processing for Expand_N_Subprogram_Declaration
5683 begin
5684 -- In SPARK, subprogram declarations are only allowed in package
5685 -- specifications.
5687 if Nkind (Parent (N)) /= N_Package_Specification then
5688 if Nkind (Parent (N)) = N_Compilation_Unit then
5689 Check_SPARK_05_Restriction
5690 ("subprogram declaration is not a library item", N);
5692 elsif Present (Next (N))
5693 and then Nkind (Next (N)) = N_Pragma
5694 and then Get_Pragma_Id (Next (N)) = Pragma_Import
5695 then
5696 -- In SPARK, subprogram declarations are also permitted in
5697 -- declarative parts when immediately followed by a corresponding
5698 -- pragma Import. We only check here that there is some pragma
5699 -- Import.
5701 null;
5702 else
5703 Check_SPARK_05_Restriction
5704 ("subprogram declaration is not allowed here", N);
5705 end if;
5706 end if;
5708 -- Deal with case of protected subprogram. Do not generate protected
5709 -- operation if operation is flagged as eliminated.
5711 if Is_List_Member (N)
5712 and then Present (Parent (List_Containing (N)))
5713 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5714 and then Is_Protected_Type (Scop)
5715 then
5716 if No (Protected_Body_Subprogram (Subp))
5717 and then not Is_Eliminated (Subp)
5718 then
5719 Prot_Decl :=
5720 Make_Subprogram_Declaration (Loc,
5721 Specification =>
5722 Build_Protected_Sub_Specification
5723 (N, Scop, Unprotected_Mode));
5725 -- The protected subprogram is declared outside of the protected
5726 -- body. Given that the body has frozen all entities so far, we
5727 -- analyze the subprogram and perform freezing actions explicitly.
5728 -- including the generation of an explicit freeze node, to ensure
5729 -- that gigi has the proper order of elaboration.
5730 -- If the body is a subunit, the insertion point is before the
5731 -- stub in the parent.
5733 Prot_Bod := Parent (List_Containing (N));
5735 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5736 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5737 end if;
5739 Insert_Before (Prot_Bod, Prot_Decl);
5740 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5741 Set_Has_Delayed_Freeze (Prot_Id);
5743 Push_Scope (Scope (Scop));
5744 Analyze (Prot_Decl);
5745 Freeze_Before (N, Prot_Id);
5746 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5748 -- Create protected operation as well. Even though the operation
5749 -- is only accessible within the body, it is possible to make it
5750 -- available outside of the protected object by using 'Access to
5751 -- provide a callback, so build protected version in all cases.
5753 Prot_Decl :=
5754 Make_Subprogram_Declaration (Loc,
5755 Specification =>
5756 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5757 Insert_Before (Prot_Bod, Prot_Decl);
5758 Analyze (Prot_Decl);
5760 Pop_Scope;
5761 end if;
5763 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5764 -- cases this is superfluous because calls to it will be automatically
5765 -- inlined, but we definitely need the body if preconditions for the
5766 -- procedure are present.
5768 elsif Nkind (Specification (N)) = N_Procedure_Specification
5769 and then Null_Present (Specification (N))
5770 then
5771 declare
5772 Bod : constant Node_Id := Body_To_Inline (N);
5774 begin
5775 Set_Has_Completion (Subp, False);
5776 Append_Freeze_Action (Subp, Bod);
5778 -- The body now contains raise statements, so calls to it will
5779 -- not be inlined.
5781 Set_Is_Inlined (Subp, False);
5782 end;
5783 end if;
5785 -- When generating C code, transform a function that returns a
5786 -- constrained array type into a procedure with an out parameter
5787 -- that carries the return value.
5789 -- We skip this transformation for unchecked conversions, since they
5790 -- are not needed by the C generator (and this also produces cleaner
5791 -- output).
5793 if Modify_Tree_For_C
5794 and then Nkind (Specification (N)) = N_Function_Specification
5795 and then Is_Array_Type (Etype (Subp))
5796 and then Is_Constrained (Etype (Subp))
5797 and then not Is_Unchecked_Conversion_Instance (Subp)
5798 then
5799 Build_Procedure_Form (N);
5800 end if;
5801 end Expand_N_Subprogram_Declaration;
5803 --------------------------------
5804 -- Expand_Non_Function_Return --
5805 --------------------------------
5807 procedure Expand_Non_Function_Return (N : Node_Id) is
5808 pragma Assert (No (Expression (N)));
5810 Loc : constant Source_Ptr := Sloc (N);
5811 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
5812 Kind : constant Entity_Kind := Ekind (Scope_Id);
5813 Call : Node_Id;
5814 Acc_Stat : Node_Id;
5815 Goto_Stat : Node_Id;
5816 Lab_Node : Node_Id;
5818 begin
5819 -- Call the _Postconditions procedure if the related subprogram has
5820 -- contract assertions that need to be verified on exit.
5822 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
5823 and then Present (Postconditions_Proc (Scope_Id))
5824 then
5825 Insert_Action (N,
5826 Make_Procedure_Call_Statement (Loc,
5827 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
5828 end if;
5830 -- If it is a return from a procedure do no extra steps
5832 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5833 return;
5835 -- If it is a nested return within an extended one, replace it with a
5836 -- return of the previously declared return object.
5838 elsif Kind = E_Return_Statement then
5839 Rewrite (N,
5840 Make_Simple_Return_Statement (Loc,
5841 Expression =>
5842 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5843 Set_Comes_From_Extended_Return_Statement (N);
5844 Set_Return_Statement_Entity (N, Scope_Id);
5845 Expand_Simple_Function_Return (N);
5846 return;
5847 end if;
5849 pragma Assert (Is_Entry (Scope_Id));
5851 -- Look at the enclosing block to see whether the return is from an
5852 -- accept statement or an entry body.
5854 for J in reverse 0 .. Scope_Stack.Last loop
5855 Scope_Id := Scope_Stack.Table (J).Entity;
5856 exit when Is_Concurrent_Type (Scope_Id);
5857 end loop;
5859 -- If it is a return from accept statement it is expanded as call to
5860 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5862 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5863 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5865 if Is_Task_Type (Scope_Id) then
5867 Call :=
5868 Make_Procedure_Call_Statement (Loc,
5869 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
5870 Insert_Before (N, Call);
5871 -- why not insert actions here???
5872 Analyze (Call);
5874 Acc_Stat := Parent (N);
5875 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5876 Acc_Stat := Parent (Acc_Stat);
5877 end loop;
5879 Lab_Node := Last (Statements
5880 (Handled_Statement_Sequence (Acc_Stat)));
5882 Goto_Stat := Make_Goto_Statement (Loc,
5883 Name => New_Occurrence_Of
5884 (Entity (Identifier (Lab_Node)), Loc));
5886 Set_Analyzed (Goto_Stat);
5888 Rewrite (N, Goto_Stat);
5889 Analyze (N);
5891 -- If it is a return from an entry body, put a Complete_Entry_Body call
5892 -- in front of the return.
5894 elsif Is_Protected_Type (Scope_Id) then
5895 Call :=
5896 Make_Procedure_Call_Statement (Loc,
5897 Name =>
5898 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
5899 Parameter_Associations => New_List (
5900 Make_Attribute_Reference (Loc,
5901 Prefix =>
5902 New_Occurrence_Of
5903 (Find_Protection_Object (Current_Scope), Loc),
5904 Attribute_Name => Name_Unchecked_Access)));
5906 Insert_Before (N, Call);
5907 Analyze (Call);
5908 end if;
5909 end Expand_Non_Function_Return;
5911 ---------------------------------------
5912 -- Expand_Protected_Object_Reference --
5913 ---------------------------------------
5915 function Expand_Protected_Object_Reference
5916 (N : Node_Id;
5917 Scop : Entity_Id) return Node_Id
5919 Loc : constant Source_Ptr := Sloc (N);
5920 Corr : Entity_Id;
5921 Rec : Node_Id;
5922 Param : Entity_Id;
5923 Proc : Entity_Id;
5925 begin
5926 Rec := Make_Identifier (Loc, Name_uObject);
5927 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5929 -- Find enclosing protected operation, and retrieve its first parameter,
5930 -- which denotes the enclosing protected object. If the enclosing
5931 -- operation is an entry, we are immediately within the protected body,
5932 -- and we can retrieve the object from the service entries procedure. A
5933 -- barrier function has the same signature as an entry. A barrier
5934 -- function is compiled within the protected object, but unlike
5935 -- protected operations its never needs locks, so that its protected
5936 -- body subprogram points to itself.
5938 Proc := Current_Scope;
5939 while Present (Proc)
5940 and then Scope (Proc) /= Scop
5941 loop
5942 Proc := Scope (Proc);
5943 end loop;
5945 Corr := Protected_Body_Subprogram (Proc);
5947 if No (Corr) then
5949 -- Previous error left expansion incomplete.
5950 -- Nothing to do on this call.
5952 return Empty;
5953 end if;
5955 Param :=
5956 Defining_Identifier
5957 (First (Parameter_Specifications (Parent (Corr))));
5959 if Is_Subprogram (Proc) and then Proc /= Corr then
5961 -- Protected function or procedure
5963 Set_Entity (Rec, Param);
5965 -- Rec is a reference to an entity which will not be in scope when
5966 -- the call is reanalyzed, and needs no further analysis.
5968 Set_Analyzed (Rec);
5970 else
5971 -- Entry or barrier function for entry body. The first parameter of
5972 -- the entry body procedure is pointer to the object. We create a
5973 -- local variable of the proper type, duplicating what is done to
5974 -- define _object later on.
5976 declare
5977 Decls : List_Id;
5978 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
5980 begin
5981 Decls := New_List (
5982 Make_Full_Type_Declaration (Loc,
5983 Defining_Identifier => Obj_Ptr,
5984 Type_Definition =>
5985 Make_Access_To_Object_Definition (Loc,
5986 Subtype_Indication =>
5987 New_Occurrence_Of
5988 (Corresponding_Record_Type (Scop), Loc))));
5990 Insert_Actions (N, Decls);
5991 Freeze_Before (N, Obj_Ptr);
5993 Rec :=
5994 Make_Explicit_Dereference (Loc,
5995 Prefix =>
5996 Unchecked_Convert_To (Obj_Ptr,
5997 New_Occurrence_Of (Param, Loc)));
5999 -- Analyze new actual. Other actuals in calls are already analyzed
6000 -- and the list of actuals is not reanalyzed after rewriting.
6002 Set_Parent (Rec, N);
6003 Analyze (Rec);
6004 end;
6005 end if;
6007 return Rec;
6008 end Expand_Protected_Object_Reference;
6010 --------------------------------------
6011 -- Expand_Protected_Subprogram_Call --
6012 --------------------------------------
6014 procedure Expand_Protected_Subprogram_Call
6015 (N : Node_Id;
6016 Subp : Entity_Id;
6017 Scop : Entity_Id)
6019 Rec : Node_Id;
6021 procedure Expand_Internal_Init_Call;
6022 -- A call to an operation of the type may occur in the initialization
6023 -- of a private component. In that case the prefix of the call is an
6024 -- entity name and the call is treated as internal even though it
6025 -- appears in code outside of the protected type.
6027 procedure Freeze_Called_Function;
6028 -- If it is a function call it can appear in elaboration code and
6029 -- the called entity must be frozen before the call. This must be
6030 -- done before the call is expanded, as the expansion may rewrite it
6031 -- to something other than a call (e.g. a temporary initialized in a
6032 -- transient block).
6034 -------------------------------
6035 -- Expand_Internal_Init_Call --
6036 -------------------------------
6038 procedure Expand_Internal_Init_Call is
6039 begin
6040 -- If the context is a protected object (rather than a protected
6041 -- type) the call itself is bound to raise program_error because
6042 -- the protected body will not have been elaborated yet. This is
6043 -- diagnosed subsequently in Sem_Elab.
6045 Freeze_Called_Function;
6047 -- The target of the internal call is the first formal of the
6048 -- enclosing initialization procedure.
6050 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6051 Build_Protected_Subprogram_Call (N,
6052 Name => Name (N),
6053 Rec => Rec,
6054 External => False);
6055 Analyze (N);
6056 Resolve (N, Etype (Subp));
6057 end Expand_Internal_Init_Call;
6059 ----------------------------
6060 -- Freeze_Called_Function --
6061 ----------------------------
6063 procedure Freeze_Called_Function is
6064 begin
6065 if Ekind (Subp) = E_Function then
6066 Freeze_Expression (Name (N));
6067 end if;
6068 end Freeze_Called_Function;
6070 -- Start of processing for Expand_Protected_Subprogram_Call
6072 begin
6073 -- If the protected object is not an enclosing scope, this is an inter-
6074 -- object function call. Inter-object procedure calls are expanded by
6075 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6076 -- subprogram being called is in the protected body being compiled, and
6077 -- if the protected object in the call is statically the enclosing type.
6078 -- The object may be a component of some other data structure, in which
6079 -- case this must be handled as an inter-object call.
6081 if not In_Open_Scopes (Scop)
6082 or else Is_Entry_Wrapper (Current_Scope)
6083 or else not Is_Entity_Name (Name (N))
6084 then
6085 if Nkind (Name (N)) = N_Selected_Component then
6086 Rec := Prefix (Name (N));
6088 elsif Nkind (Name (N)) = N_Indexed_Component then
6089 Rec := Prefix (Prefix (Name (N)));
6091 -- If this is a call within an entry wrapper, it appears within a
6092 -- precondition that calls another primitive of the synchronized
6093 -- type. The target object of the call is the first actual on the
6094 -- wrapper. Note that this is an external call, because the wrapper
6095 -- is called outside of the synchronized object. This means that
6096 -- an entry call to an entry with preconditions involves two
6097 -- synchronized operations.
6099 elsif Ekind (Current_Scope) = E_Procedure
6100 and then Is_Entry_Wrapper (Current_Scope)
6101 then
6102 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6104 else
6105 -- If the context is the initialization procedure for a protected
6106 -- type, the call is legal because the called entity must be a
6107 -- function of that enclosing type, and this is treated as an
6108 -- internal call.
6110 pragma Assert
6111 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6113 Expand_Internal_Init_Call;
6114 return;
6115 end if;
6117 Freeze_Called_Function;
6118 Build_Protected_Subprogram_Call (N,
6119 Name => New_Occurrence_Of (Subp, Sloc (N)),
6120 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6121 External => True);
6123 else
6124 Rec := Expand_Protected_Object_Reference (N, Scop);
6126 if No (Rec) then
6127 return;
6128 end if;
6130 Freeze_Called_Function;
6131 Build_Protected_Subprogram_Call (N,
6132 Name => Name (N),
6133 Rec => Rec,
6134 External => False);
6135 end if;
6137 -- Analyze and resolve the new call. The actuals have already been
6138 -- resolved, but expansion of a function call will add extra actuals
6139 -- if needed. Analysis of a procedure call already includes resolution.
6141 Analyze (N);
6143 if Ekind (Subp) = E_Function then
6144 Resolve (N, Etype (Subp));
6145 end if;
6146 end Expand_Protected_Subprogram_Call;
6148 -----------------------------------
6149 -- Expand_Simple_Function_Return --
6150 -----------------------------------
6152 -- The "simple" comes from the syntax rule simple_return_statement. The
6153 -- semantics are not at all simple.
6155 procedure Expand_Simple_Function_Return (N : Node_Id) is
6156 Loc : constant Source_Ptr := Sloc (N);
6158 Scope_Id : constant Entity_Id :=
6159 Return_Applies_To (Return_Statement_Entity (N));
6160 -- The function we are returning from
6162 R_Type : constant Entity_Id := Etype (Scope_Id);
6163 -- The result type of the function
6165 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6167 Exp : Node_Id := Expression (N);
6168 pragma Assert (Present (Exp));
6170 Exptyp : constant Entity_Id := Etype (Exp);
6171 -- The type of the expression (not necessarily the same as R_Type)
6173 Subtype_Ind : Node_Id;
6174 -- If the result type of the function is class-wide and the expression
6175 -- has a specific type, then we use the expression's type as the type of
6176 -- the return object. In cases where the expression is an aggregate that
6177 -- is built in place, this avoids the need for an expensive conversion
6178 -- of the return object to the specific type on assignments to the
6179 -- individual components.
6181 begin
6182 if Is_Class_Wide_Type (R_Type)
6183 and then not Is_Class_Wide_Type (Exptyp)
6184 and then Nkind (Exp) /= N_Type_Conversion
6185 then
6186 Subtype_Ind := New_Occurrence_Of (Exptyp, Loc);
6187 else
6188 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6190 -- If the result type is class-wide and the expression is a view
6191 -- conversion, the conversion plays no role in the expansion because
6192 -- it does not modify the tag of the object. Remove the conversion
6193 -- altogether to prevent tag overwriting.
6195 if Is_Class_Wide_Type (R_Type)
6196 and then not Is_Class_Wide_Type (Exptyp)
6197 and then Nkind (Exp) = N_Type_Conversion
6198 then
6199 Exp := Expression (Exp);
6200 end if;
6201 end if;
6203 -- For the case of a simple return that does not come from an extended
6204 -- return, in the case of Ada 2005 where we are returning a limited
6205 -- type, we rewrite "return <expression>;" to be:
6207 -- return _anon_ : <return_subtype> := <expression>
6209 -- The expansion produced by Expand_N_Extended_Return_Statement will
6210 -- contain simple return statements (for example, a block containing
6211 -- simple return of the return object), which brings us back here with
6212 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6213 -- checking for a simple return that does not come from an extended
6214 -- return is to avoid this infinite recursion.
6216 -- The reason for this design is that for Ada 2005 limited returns, we
6217 -- need to reify the return object, so we can build it "in place", and
6218 -- we need a block statement to hang finalization and tasking stuff.
6220 -- ??? In order to avoid disruption, we avoid translating to extended
6221 -- return except in the cases where we really need to (Ada 2005 for
6222 -- inherently limited). We might prefer to do this translation in all
6223 -- cases (except perhaps for the case of Ada 95 inherently limited),
6224 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6225 -- code. This would also allow us to do the build-in-place optimization
6226 -- for efficiency even in cases where it is semantically not required.
6228 -- As before, we check the type of the return expression rather than the
6229 -- return type of the function, because the latter may be a limited
6230 -- class-wide interface type, which is not a limited type, even though
6231 -- the type of the expression may be.
6233 if not Comes_From_Extended_Return_Statement (N)
6234 and then Is_Limited_View (Etype (Expression (N)))
6235 and then Ada_Version >= Ada_2005
6236 and then not Debug_Flag_Dot_L
6238 -- The functionality of interface thunks is simple and it is always
6239 -- handled by means of simple return statements. This leaves their
6240 -- expansion simple and clean.
6242 and then not Is_Thunk (Current_Scope)
6243 then
6244 declare
6245 Return_Object_Entity : constant Entity_Id :=
6246 Make_Temporary (Loc, 'R', Exp);
6248 Obj_Decl : constant Node_Id :=
6249 Make_Object_Declaration (Loc,
6250 Defining_Identifier => Return_Object_Entity,
6251 Object_Definition => Subtype_Ind,
6252 Expression => Exp);
6254 Ext : constant Node_Id :=
6255 Make_Extended_Return_Statement (Loc,
6256 Return_Object_Declarations => New_List (Obj_Decl));
6257 -- Do not perform this high-level optimization if the result type
6258 -- is an interface because the "this" pointer must be displaced.
6260 begin
6261 Rewrite (N, Ext);
6262 Analyze (N);
6263 return;
6264 end;
6265 end if;
6267 -- Here we have a simple return statement that is part of the expansion
6268 -- of an extended return statement (either written by the user, or
6269 -- generated by the above code).
6271 -- Always normalize C/Fortran boolean result. This is not always needed,
6272 -- but it seems a good idea to minimize the passing around of non-
6273 -- normalized values, and in any case this handles the processing of
6274 -- barrier functions for protected types, which turn the condition into
6275 -- a return statement.
6277 if Is_Boolean_Type (Exptyp)
6278 and then Nonzero_Is_True (Exptyp)
6279 then
6280 Adjust_Condition (Exp);
6281 Adjust_Result_Type (Exp, Exptyp);
6282 end if;
6284 -- Do validity check if enabled for returns
6286 if Validity_Checks_On
6287 and then Validity_Check_Returns
6288 then
6289 Ensure_Valid (Exp);
6290 end if;
6292 -- Check the result expression of a scalar function against the subtype
6293 -- of the function by inserting a conversion. This conversion must
6294 -- eventually be performed for other classes of types, but for now it's
6295 -- only done for scalars.
6296 -- ???
6298 if Is_Scalar_Type (Exptyp) then
6299 Rewrite (Exp, Convert_To (R_Type, Exp));
6301 -- The expression is resolved to ensure that the conversion gets
6302 -- expanded to generate a possible constraint check.
6304 Analyze_And_Resolve (Exp, R_Type);
6305 end if;
6307 -- Deal with returning variable length objects and controlled types
6309 -- Nothing to do if we are returning by reference, or this is not a
6310 -- type that requires special processing (indicated by the fact that
6311 -- it requires a cleanup scope for the secondary stack case).
6313 if Is_Limited_View (Exptyp)
6314 or else Is_Limited_Interface (Exptyp)
6315 then
6316 null;
6318 -- No copy needed for thunks returning interface type objects since
6319 -- the object is returned by reference and the maximum functionality
6320 -- required is just to displace the pointer.
6322 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
6323 null;
6325 -- If the call is within a thunk and the type is a limited view, the
6326 -- backend will eventually see the non-limited view of the type.
6328 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
6329 return;
6331 elsif not Requires_Transient_Scope (R_Type) then
6333 -- Mutable records with variable-length components are not returned
6334 -- on the sec-stack, so we need to make sure that the back end will
6335 -- only copy back the size of the actual value, and not the maximum
6336 -- size. We create an actual subtype for this purpose. However we
6337 -- need not do it if the expression is a function call since this
6338 -- will be done in the called function and doing it here too would
6339 -- cause a temporary with maximum size to be created.
6341 declare
6342 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6343 Decl : Node_Id;
6344 Ent : Entity_Id;
6345 begin
6346 if Nkind (Exp) /= N_Function_Call
6347 and then Has_Discriminants (Ubt)
6348 and then not Is_Constrained (Ubt)
6349 and then not Has_Unchecked_Union (Ubt)
6350 then
6351 Decl := Build_Actual_Subtype (Ubt, Exp);
6352 Ent := Defining_Identifier (Decl);
6353 Insert_Action (Exp, Decl);
6354 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6355 Analyze_And_Resolve (Exp);
6356 end if;
6357 end;
6359 -- Here if secondary stack is used
6361 else
6362 -- Prevent the reclamation of the secondary stack by all enclosing
6363 -- blocks and loops as well as the related function; otherwise the
6364 -- result would be reclaimed too early.
6366 Set_Enclosing_Sec_Stack_Return (N);
6368 -- Optimize the case where the result is a function call. In this
6369 -- case either the result is already on the secondary stack, or is
6370 -- already being returned with the stack pointer depressed and no
6371 -- further processing is required except to set the By_Ref flag
6372 -- to ensure that gigi does not attempt an extra unnecessary copy.
6373 -- (actually not just unnecessary but harmfully wrong in the case
6374 -- of a controlled type, where gigi does not know how to do a copy).
6375 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6376 -- for array types if the constrained status of the target type is
6377 -- different from that of the expression.
6379 if Requires_Transient_Scope (Exptyp)
6380 and then
6381 (not Is_Array_Type (Exptyp)
6382 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6383 or else CW_Or_Has_Controlled_Part (Utyp))
6384 and then Nkind (Exp) = N_Function_Call
6385 then
6386 Set_By_Ref (N);
6388 -- Remove side effects from the expression now so that other parts
6389 -- of the expander do not have to reanalyze this node without this
6390 -- optimization
6392 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6394 -- For controlled types, do the allocation on the secondary stack
6395 -- manually in order to call adjust at the right time:
6397 -- type Anon1 is access R_Type;
6398 -- for Anon1'Storage_pool use ss_pool;
6399 -- Anon2 : anon1 := new R_Type'(expr);
6400 -- return Anon2.all;
6402 -- We do the same for classwide types that are not potentially
6403 -- controlled (by the virtue of restriction No_Finalization) because
6404 -- gigi is not able to properly allocate class-wide types.
6406 elsif CW_Or_Has_Controlled_Part (Utyp) then
6407 declare
6408 Loc : constant Source_Ptr := Sloc (N);
6409 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6410 Alloc_Node : Node_Id;
6411 Temp : Entity_Id;
6413 begin
6414 Set_Ekind (Acc_Typ, E_Access_Type);
6416 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6418 -- This is an allocator for the secondary stack, and it's fine
6419 -- to have Comes_From_Source set False on it, as gigi knows not
6420 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6422 Alloc_Node :=
6423 Make_Allocator (Loc,
6424 Expression =>
6425 Make_Qualified_Expression (Loc,
6426 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6427 Expression => Relocate_Node (Exp)));
6429 -- We do not want discriminant checks on the declaration,
6430 -- given that it gets its value from the allocator.
6432 Set_No_Initialization (Alloc_Node);
6434 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6436 Insert_List_Before_And_Analyze (N, New_List (
6437 Make_Full_Type_Declaration (Loc,
6438 Defining_Identifier => Acc_Typ,
6439 Type_Definition =>
6440 Make_Access_To_Object_Definition (Loc,
6441 Subtype_Indication => Subtype_Ind)),
6443 Make_Object_Declaration (Loc,
6444 Defining_Identifier => Temp,
6445 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6446 Expression => Alloc_Node)));
6448 Rewrite (Exp,
6449 Make_Explicit_Dereference (Loc,
6450 Prefix => New_Occurrence_Of (Temp, Loc)));
6452 -- Ada 2005 (AI-251): If the type of the returned object is
6453 -- an interface then add an implicit type conversion to force
6454 -- displacement of the "this" pointer.
6456 if Is_Interface (R_Type) then
6457 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6458 end if;
6460 Analyze_And_Resolve (Exp, R_Type);
6461 end;
6463 -- Otherwise use the gigi mechanism to allocate result on the
6464 -- secondary stack.
6466 else
6467 Check_Restriction (No_Secondary_Stack, N);
6468 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6469 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6470 end if;
6471 end if;
6473 -- Implement the rules of 6.5(8-10), which require a tag check in
6474 -- the case of a limited tagged return type, and tag reassignment for
6475 -- nonlimited tagged results. These actions are needed when the return
6476 -- type is a specific tagged type and the result expression is a
6477 -- conversion or a formal parameter, because in that case the tag of
6478 -- the expression might differ from the tag of the specific result type.
6480 if Is_Tagged_Type (Utyp)
6481 and then not Is_Class_Wide_Type (Utyp)
6482 and then (Nkind_In (Exp, N_Type_Conversion,
6483 N_Unchecked_Type_Conversion)
6484 or else (Is_Entity_Name (Exp)
6485 and then Ekind (Entity (Exp)) in Formal_Kind))
6486 then
6487 -- When the return type is limited, perform a check that the tag of
6488 -- the result is the same as the tag of the return type.
6490 if Is_Limited_Type (R_Type) then
6491 Insert_Action (Exp,
6492 Make_Raise_Constraint_Error (Loc,
6493 Condition =>
6494 Make_Op_Ne (Loc,
6495 Left_Opnd =>
6496 Make_Selected_Component (Loc,
6497 Prefix => Duplicate_Subexpr (Exp),
6498 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6499 Right_Opnd =>
6500 Make_Attribute_Reference (Loc,
6501 Prefix =>
6502 New_Occurrence_Of (Base_Type (Utyp), Loc),
6503 Attribute_Name => Name_Tag)),
6504 Reason => CE_Tag_Check_Failed));
6506 -- If the result type is a specific nonlimited tagged type, then we
6507 -- have to ensure that the tag of the result is that of the result
6508 -- type. This is handled by making a copy of the expression in
6509 -- the case where it might have a different tag, namely when the
6510 -- expression is a conversion or a formal parameter. We create a new
6511 -- object of the result type and initialize it from the expression,
6512 -- which will implicitly force the tag to be set appropriately.
6514 else
6515 declare
6516 ExpR : constant Node_Id := Relocate_Node (Exp);
6517 Result_Id : constant Entity_Id :=
6518 Make_Temporary (Loc, 'R', ExpR);
6519 Result_Exp : constant Node_Id :=
6520 New_Occurrence_Of (Result_Id, Loc);
6521 Result_Obj : constant Node_Id :=
6522 Make_Object_Declaration (Loc,
6523 Defining_Identifier => Result_Id,
6524 Object_Definition =>
6525 New_Occurrence_Of (R_Type, Loc),
6526 Constant_Present => True,
6527 Expression => ExpR);
6529 begin
6530 Set_Assignment_OK (Result_Obj);
6531 Insert_Action (Exp, Result_Obj);
6533 Rewrite (Exp, Result_Exp);
6534 Analyze_And_Resolve (Exp, R_Type);
6535 end;
6536 end if;
6538 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6539 -- a check that the level of the return expression's underlying type
6540 -- is not deeper than the level of the master enclosing the function.
6541 -- Always generate the check when the type of the return expression
6542 -- is class-wide, when it's a type conversion, or when it's a formal
6543 -- parameter. Otherwise, suppress the check in the case where the
6544 -- return expression has a specific type whose level is known not to
6545 -- be statically deeper than the function's result type.
6547 -- No runtime check needed in interface thunks since it is performed
6548 -- by the target primitive associated with the thunk.
6550 -- Note: accessibility check is skipped in the VM case, since there
6551 -- does not seem to be any practical way to implement this check.
6553 elsif Ada_Version >= Ada_2005
6554 and then Tagged_Type_Expansion
6555 and then Is_Class_Wide_Type (R_Type)
6556 and then not Is_Thunk (Current_Scope)
6557 and then not Scope_Suppress.Suppress (Accessibility_Check)
6558 and then
6559 (Is_Class_Wide_Type (Etype (Exp))
6560 or else Nkind_In (Exp, N_Type_Conversion,
6561 N_Unchecked_Type_Conversion)
6562 or else (Is_Entity_Name (Exp)
6563 and then Ekind (Entity (Exp)) in Formal_Kind)
6564 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6565 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6566 then
6567 declare
6568 Tag_Node : Node_Id;
6570 begin
6571 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6572 -- "this" to reference the base of the object. This is required to
6573 -- get access to the TSD of the object.
6575 if Is_Class_Wide_Type (Etype (Exp))
6576 and then Is_Interface (Etype (Exp))
6577 then
6578 -- If the expression is an explicit dereference then we can
6579 -- directly displace the pointer to reference the base of
6580 -- the object.
6582 if Nkind (Exp) = N_Explicit_Dereference then
6583 Tag_Node :=
6584 Make_Explicit_Dereference (Loc,
6585 Prefix =>
6586 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6587 Make_Function_Call (Loc,
6588 Name =>
6589 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6590 Parameter_Associations => New_List (
6591 Unchecked_Convert_To (RTE (RE_Address),
6592 Duplicate_Subexpr (Prefix (Exp)))))));
6594 -- Similar case to the previous one but the expression is a
6595 -- renaming of an explicit dereference.
6597 elsif Nkind (Exp) = N_Identifier
6598 and then Present (Renamed_Object (Entity (Exp)))
6599 and then Nkind (Renamed_Object (Entity (Exp)))
6600 = N_Explicit_Dereference
6601 then
6602 Tag_Node :=
6603 Make_Explicit_Dereference (Loc,
6604 Prefix =>
6605 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6606 Make_Function_Call (Loc,
6607 Name =>
6608 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6609 Parameter_Associations => New_List (
6610 Unchecked_Convert_To (RTE (RE_Address),
6611 Duplicate_Subexpr
6612 (Prefix
6613 (Renamed_Object (Entity (Exp)))))))));
6615 -- Common case: obtain the address of the actual object and
6616 -- displace the pointer to reference the base of the object.
6618 else
6619 Tag_Node :=
6620 Make_Explicit_Dereference (Loc,
6621 Prefix =>
6622 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6623 Make_Function_Call (Loc,
6624 Name =>
6625 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6626 Parameter_Associations => New_List (
6627 Make_Attribute_Reference (Loc,
6628 Prefix => Duplicate_Subexpr (Exp),
6629 Attribute_Name => Name_Address)))));
6630 end if;
6631 else
6632 Tag_Node :=
6633 Make_Attribute_Reference (Loc,
6634 Prefix => Duplicate_Subexpr (Exp),
6635 Attribute_Name => Name_Tag);
6636 end if;
6638 Insert_Action (Exp,
6639 Make_Raise_Program_Error (Loc,
6640 Condition =>
6641 Make_Op_Gt (Loc,
6642 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6643 Right_Opnd =>
6644 Make_Integer_Literal (Loc,
6645 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6646 Reason => PE_Accessibility_Check_Failed));
6647 end;
6649 -- AI05-0073: If function has a controlling access result, check that
6650 -- the tag of the return value, if it is not null, matches designated
6651 -- type of return type.
6653 -- The return expression is referenced twice in the code below, so it
6654 -- must be made free of side effects. Given that different compilers
6655 -- may evaluate these parameters in different order, both occurrences
6656 -- perform a copy.
6658 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6659 and then Has_Controlling_Result (Scope_Id)
6660 then
6661 Insert_Action (N,
6662 Make_Raise_Constraint_Error (Loc,
6663 Condition =>
6664 Make_And_Then (Loc,
6665 Left_Opnd =>
6666 Make_Op_Ne (Loc,
6667 Left_Opnd => Duplicate_Subexpr (Exp),
6668 Right_Opnd => Make_Null (Loc)),
6670 Right_Opnd => Make_Op_Ne (Loc,
6671 Left_Opnd =>
6672 Make_Selected_Component (Loc,
6673 Prefix => Duplicate_Subexpr (Exp),
6674 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6676 Right_Opnd =>
6677 Make_Attribute_Reference (Loc,
6678 Prefix =>
6679 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6680 Attribute_Name => Name_Tag))),
6682 Reason => CE_Tag_Check_Failed),
6683 Suppress => All_Checks);
6684 end if;
6686 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6687 -- ensure that the function result does not outlive an
6688 -- object designated by one of it discriminants.
6690 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6691 and then Has_Unconstrained_Access_Discriminants (R_Type)
6692 then
6693 declare
6694 Discrim_Source : Node_Id;
6696 procedure Check_Against_Result_Level (Level : Node_Id);
6697 -- Check the given accessibility level against the level
6698 -- determined by the point of call. (AI05-0234).
6700 --------------------------------
6701 -- Check_Against_Result_Level --
6702 --------------------------------
6704 procedure Check_Against_Result_Level (Level : Node_Id) is
6705 begin
6706 Insert_Action (N,
6707 Make_Raise_Program_Error (Loc,
6708 Condition =>
6709 Make_Op_Gt (Loc,
6710 Left_Opnd => Level,
6711 Right_Opnd =>
6712 New_Occurrence_Of
6713 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6714 Reason => PE_Accessibility_Check_Failed));
6715 end Check_Against_Result_Level;
6717 begin
6718 Discrim_Source := Exp;
6719 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6720 Discrim_Source := Expression (Discrim_Source);
6721 end loop;
6723 if Nkind (Discrim_Source) = N_Identifier
6724 and then Is_Return_Object (Entity (Discrim_Source))
6725 then
6726 Discrim_Source := Entity (Discrim_Source);
6728 if Is_Constrained (Etype (Discrim_Source)) then
6729 Discrim_Source := Etype (Discrim_Source);
6730 else
6731 Discrim_Source := Expression (Parent (Discrim_Source));
6732 end if;
6734 elsif Nkind (Discrim_Source) = N_Identifier
6735 and then Nkind_In (Original_Node (Discrim_Source),
6736 N_Aggregate, N_Extension_Aggregate)
6737 then
6738 Discrim_Source := Original_Node (Discrim_Source);
6740 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6741 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6742 then
6743 Discrim_Source := Original_Node (Discrim_Source);
6744 end if;
6746 while Nkind_In (Discrim_Source, N_Qualified_Expression,
6747 N_Type_Conversion,
6748 N_Unchecked_Type_Conversion)
6749 loop
6750 Discrim_Source := Expression (Discrim_Source);
6751 end loop;
6753 case Nkind (Discrim_Source) is
6754 when N_Defining_Identifier =>
6755 pragma Assert (Is_Composite_Type (Discrim_Source)
6756 and then Has_Discriminants (Discrim_Source)
6757 and then Is_Constrained (Discrim_Source));
6759 declare
6760 Discrim : Entity_Id :=
6761 First_Discriminant (Base_Type (R_Type));
6762 Disc_Elmt : Elmt_Id :=
6763 First_Elmt (Discriminant_Constraint
6764 (Discrim_Source));
6765 begin
6766 loop
6767 if Ekind (Etype (Discrim)) =
6768 E_Anonymous_Access_Type
6769 then
6770 Check_Against_Result_Level
6771 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
6772 end if;
6774 Next_Elmt (Disc_Elmt);
6775 Next_Discriminant (Discrim);
6776 exit when not Present (Discrim);
6777 end loop;
6778 end;
6780 when N_Aggregate
6781 | N_Extension_Aggregate
6783 -- Unimplemented: extension aggregate case where discrims
6784 -- come from ancestor part, not extension part.
6786 declare
6787 Discrim : Entity_Id :=
6788 First_Discriminant (Base_Type (R_Type));
6790 Disc_Exp : Node_Id := Empty;
6792 Positionals_Exhausted
6793 : Boolean := not Present (Expressions
6794 (Discrim_Source));
6796 function Associated_Expr
6797 (Comp_Id : Entity_Id;
6798 Associations : List_Id) return Node_Id;
6800 -- Given a component and a component associations list,
6801 -- locate the expression for that component; returns
6802 -- Empty if no such expression is found.
6804 ---------------------
6805 -- Associated_Expr --
6806 ---------------------
6808 function Associated_Expr
6809 (Comp_Id : Entity_Id;
6810 Associations : List_Id) return Node_Id
6812 Assoc : Node_Id;
6813 Choice : Node_Id;
6815 begin
6816 -- Simple linear search seems ok here
6818 Assoc := First (Associations);
6819 while Present (Assoc) loop
6820 Choice := First (Choices (Assoc));
6821 while Present (Choice) loop
6822 if (Nkind (Choice) = N_Identifier
6823 and then Chars (Choice) = Chars (Comp_Id))
6824 or else (Nkind (Choice) = N_Others_Choice)
6825 then
6826 return Expression (Assoc);
6827 end if;
6829 Next (Choice);
6830 end loop;
6832 Next (Assoc);
6833 end loop;
6835 return Empty;
6836 end Associated_Expr;
6838 -- Start of processing for Expand_Simple_Function_Return
6840 begin
6841 if not Positionals_Exhausted then
6842 Disc_Exp := First (Expressions (Discrim_Source));
6843 end if;
6845 loop
6846 if Positionals_Exhausted then
6847 Disc_Exp :=
6848 Associated_Expr
6849 (Discrim,
6850 Component_Associations (Discrim_Source));
6851 end if;
6853 if Ekind (Etype (Discrim)) =
6854 E_Anonymous_Access_Type
6855 then
6856 Check_Against_Result_Level
6857 (Dynamic_Accessibility_Level (Disc_Exp));
6858 end if;
6860 Next_Discriminant (Discrim);
6861 exit when not Present (Discrim);
6863 if not Positionals_Exhausted then
6864 Next (Disc_Exp);
6865 Positionals_Exhausted := not Present (Disc_Exp);
6866 end if;
6867 end loop;
6868 end;
6870 when N_Function_Call =>
6872 -- No check needed (check performed by callee)
6874 null;
6876 when others =>
6877 declare
6878 Level : constant Node_Id :=
6879 Make_Integer_Literal (Loc,
6880 Object_Access_Level (Discrim_Source));
6882 begin
6883 -- Unimplemented: check for name prefix that includes
6884 -- a dereference of an access value with a dynamic
6885 -- accessibility level (e.g., an access param or a
6886 -- saooaaat) and use dynamic level in that case. For
6887 -- example:
6888 -- return Access_Param.all(Some_Index).Some_Component;
6889 -- ???
6891 Set_Etype (Level, Standard_Natural);
6892 Check_Against_Result_Level (Level);
6893 end;
6894 end case;
6895 end;
6896 end if;
6898 -- If we are returning an object that may not be bit-aligned, then copy
6899 -- the value into a temporary first. This copy may need to expand to a
6900 -- loop of component operations.
6902 if Is_Possibly_Unaligned_Slice (Exp)
6903 or else Is_Possibly_Unaligned_Object (Exp)
6904 then
6905 declare
6906 ExpR : constant Node_Id := Relocate_Node (Exp);
6907 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6908 begin
6909 Insert_Action (Exp,
6910 Make_Object_Declaration (Loc,
6911 Defining_Identifier => Tnn,
6912 Constant_Present => True,
6913 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6914 Expression => ExpR),
6915 Suppress => All_Checks);
6916 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6917 end;
6918 end if;
6920 -- Call the _Postconditions procedure if the related function has
6921 -- contract assertions that need to be verified on exit.
6923 if Ekind (Scope_Id) = E_Function
6924 and then Present (Postconditions_Proc (Scope_Id))
6925 then
6926 -- In the case of discriminated objects, we have created a
6927 -- constrained subtype above, and used the underlying type. This
6928 -- transformation is post-analysis and harmless, except that now the
6929 -- call to the post-condition will be analyzed and the type kinds
6930 -- have to match.
6932 if Nkind (Exp) = N_Unchecked_Type_Conversion
6933 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
6934 then
6935 Rewrite (Exp, Expression (Relocate_Node (Exp)));
6936 end if;
6938 -- We are going to reference the returned value twice in this case,
6939 -- once in the call to _Postconditions, and once in the actual return
6940 -- statement, but we can't have side effects happening twice.
6942 Force_Evaluation (Exp, Mode => Strict);
6944 -- Generate call to _Postconditions
6946 Insert_Action (Exp,
6947 Make_Procedure_Call_Statement (Loc,
6948 Name =>
6949 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
6950 Parameter_Associations => New_List (New_Copy_Tree (Exp))));
6951 end if;
6953 -- Ada 2005 (AI-251): If this return statement corresponds with an
6954 -- simple return statement associated with an extended return statement
6955 -- and the type of the returned object is an interface then generate an
6956 -- implicit conversion to force displacement of the "this" pointer.
6958 if Ada_Version >= Ada_2005
6959 and then Comes_From_Extended_Return_Statement (N)
6960 and then Nkind (Expression (N)) = N_Identifier
6961 and then Is_Interface (Utyp)
6962 and then Utyp /= Underlying_Type (Exptyp)
6963 then
6964 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6965 Analyze_And_Resolve (Exp);
6966 end if;
6967 end Expand_Simple_Function_Return;
6969 --------------------------------------------
6970 -- Has_Unconstrained_Access_Discriminants --
6971 --------------------------------------------
6973 function Has_Unconstrained_Access_Discriminants
6974 (Subtyp : Entity_Id) return Boolean
6976 Discr : Entity_Id;
6978 begin
6979 if Has_Discriminants (Subtyp)
6980 and then not Is_Constrained (Subtyp)
6981 then
6982 Discr := First_Discriminant (Subtyp);
6983 while Present (Discr) loop
6984 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
6985 return True;
6986 end if;
6988 Next_Discriminant (Discr);
6989 end loop;
6990 end if;
6992 return False;
6993 end Has_Unconstrained_Access_Discriminants;
6995 --------------------------------
6996 -- Is_Build_In_Place_Function --
6997 --------------------------------
6999 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7000 begin
7001 -- This function is called from Expand_Subtype_From_Expr during
7002 -- semantic analysis, even when expansion is off. In those cases
7003 -- the build_in_place expansion will not take place.
7005 if not Expander_Active then
7006 return False;
7007 end if;
7009 -- For now we test whether E denotes a function or access-to-function
7010 -- type whose result subtype is inherently limited. Later this test
7011 -- may be revised to allow composite nonlimited types. Functions with
7012 -- a foreign convention or whose result type has a foreign convention
7013 -- never qualify.
7015 if Ekind_In (E, E_Function, E_Generic_Function)
7016 or else (Ekind (E) = E_Subprogram_Type
7017 and then Etype (E) /= Standard_Void_Type)
7018 then
7019 -- Note: If the function has a foreign convention, it cannot build
7020 -- its result in place, so you're on your own. On the other hand,
7021 -- if only the return type has a foreign convention, its layout is
7022 -- intended to be compatible with the other language, but the build-
7023 -- in place machinery can ensure that the object is not copied.
7025 if Has_Foreign_Convention (E) then
7026 return False;
7028 -- In Ada 2005 all functions with an inherently limited return type
7029 -- must be handled using a build-in-place profile, including the case
7030 -- of a function with a limited interface result, where the function
7031 -- may return objects of nonlimited descendants.
7033 else
7034 return Is_Limited_View (Etype (E))
7035 and then Ada_Version >= Ada_2005
7036 and then not Debug_Flag_Dot_L;
7037 end if;
7039 else
7040 return False;
7041 end if;
7042 end Is_Build_In_Place_Function;
7044 -------------------------------------
7045 -- Is_Build_In_Place_Function_Call --
7046 -------------------------------------
7048 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7049 Exp_Node : Node_Id := N;
7050 Function_Id : Entity_Id;
7052 begin
7053 -- Return False if the expander is currently inactive, since awareness
7054 -- of build-in-place treatment is only relevant during expansion. Note
7055 -- that Is_Build_In_Place_Function, which is called as part of this
7056 -- function, is also conditioned this way, but we need to check here as
7057 -- well to avoid blowing up on processing protected calls when expansion
7058 -- is disabled (such as with -gnatc) since those would trip over the
7059 -- raise of Program_Error below.
7061 -- In SPARK mode, build-in-place calls are not expanded, so that we
7062 -- may end up with a call that is neither resolved to an entity, nor
7063 -- an indirect call.
7065 if not Expander_Active then
7066 return False;
7067 end if;
7069 -- Step past qualification, type conversion (which can occur in actual
7070 -- parameter contexts), and unchecked conversion (which can occur in
7071 -- cases of calls to 'Input).
7073 if Nkind_In (Exp_Node, N_Qualified_Expression,
7074 N_Type_Conversion,
7075 N_Unchecked_Type_Conversion)
7076 then
7077 Exp_Node := Expression (N);
7078 end if;
7080 if Nkind (Exp_Node) /= N_Function_Call then
7081 return False;
7083 else
7084 if Is_Entity_Name (Name (Exp_Node)) then
7085 Function_Id := Entity (Name (Exp_Node));
7087 -- In the case of an explicitly dereferenced call, use the subprogram
7088 -- type generated for the dereference.
7090 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7091 Function_Id := Etype (Name (Exp_Node));
7093 -- This may be a call to a protected function.
7095 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7096 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7098 else
7099 raise Program_Error;
7100 end if;
7102 return Is_Build_In_Place_Function (Function_Id);
7103 end if;
7104 end Is_Build_In_Place_Function_Call;
7106 -----------------------
7107 -- Freeze_Subprogram --
7108 -----------------------
7110 procedure Freeze_Subprogram (N : Node_Id) is
7111 Loc : constant Source_Ptr := Sloc (N);
7113 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7114 -- (Ada 2005): Register a predefined primitive in all the secondary
7115 -- dispatch tables of its primitive type.
7117 ----------------------------------
7118 -- Register_Predefined_DT_Entry --
7119 ----------------------------------
7121 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7122 Iface_DT_Ptr : Elmt_Id;
7123 Tagged_Typ : Entity_Id;
7124 Thunk_Id : Entity_Id;
7125 Thunk_Code : Node_Id;
7127 begin
7128 Tagged_Typ := Find_Dispatching_Type (Prim);
7130 if No (Access_Disp_Table (Tagged_Typ))
7131 or else not Has_Interfaces (Tagged_Typ)
7132 or else not RTE_Available (RE_Interface_Tag)
7133 or else Restriction_Active (No_Dispatching_Calls)
7134 then
7135 return;
7136 end if;
7138 -- Skip the first two access-to-dispatch-table pointers since they
7139 -- leads to the primary dispatch table (predefined DT and user
7140 -- defined DT). We are only concerned with the secondary dispatch
7141 -- table pointers. Note that the access-to- dispatch-table pointer
7142 -- corresponds to the first implemented interface retrieved below.
7144 Iface_DT_Ptr :=
7145 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7147 while Present (Iface_DT_Ptr)
7148 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7149 loop
7150 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7151 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7153 if Present (Thunk_Code) then
7154 Insert_Actions_After (N, New_List (
7155 Thunk_Code,
7157 Build_Set_Predefined_Prim_Op_Address (Loc,
7158 Tag_Node =>
7159 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7160 Position => DT_Position (Prim),
7161 Address_Node =>
7162 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7163 Make_Attribute_Reference (Loc,
7164 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7165 Attribute_Name => Name_Unrestricted_Access))),
7167 Build_Set_Predefined_Prim_Op_Address (Loc,
7168 Tag_Node =>
7169 New_Occurrence_Of
7170 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7171 Loc),
7172 Position => DT_Position (Prim),
7173 Address_Node =>
7174 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7175 Make_Attribute_Reference (Loc,
7176 Prefix => New_Occurrence_Of (Prim, Loc),
7177 Attribute_Name => Name_Unrestricted_Access)))));
7178 end if;
7180 -- Skip the tag of the predefined primitives dispatch table
7182 Next_Elmt (Iface_DT_Ptr);
7183 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7185 -- Skip tag of the no-thunks dispatch table
7187 Next_Elmt (Iface_DT_Ptr);
7188 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7190 -- Skip tag of predefined primitives no-thunks dispatch table
7192 Next_Elmt (Iface_DT_Ptr);
7193 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7195 Next_Elmt (Iface_DT_Ptr);
7196 end loop;
7197 end Register_Predefined_DT_Entry;
7199 -- Local variables
7201 Subp : constant Entity_Id := Entity (N);
7203 -- Start of processing for Freeze_Subprogram
7205 begin
7206 -- We suppress the initialization of the dispatch table entry when
7207 -- not Tagged_Type_Expansion because the dispatching mechanism is
7208 -- handled internally by the target.
7210 if Is_Dispatching_Operation (Subp)
7211 and then not Is_Abstract_Subprogram (Subp)
7212 and then Present (DTC_Entity (Subp))
7213 and then Present (Scope (DTC_Entity (Subp)))
7214 and then Tagged_Type_Expansion
7215 and then not Restriction_Active (No_Dispatching_Calls)
7216 and then RTE_Available (RE_Tag)
7217 then
7218 declare
7219 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7221 begin
7222 -- Handle private overridden primitives
7224 if not Is_CPP_Class (Typ) then
7225 Check_Overriding_Operation (Subp);
7226 end if;
7228 -- We assume that imported CPP primitives correspond with objects
7229 -- whose constructor is in the CPP side; therefore we don't need
7230 -- to generate code to register them in the dispatch table.
7232 if Is_CPP_Class (Typ) then
7233 null;
7235 -- Handle CPP primitives found in derivations of CPP_Class types.
7236 -- These primitives must have been inherited from some parent, and
7237 -- there is no need to register them in the dispatch table because
7238 -- Build_Inherit_Prims takes care of initializing these slots.
7240 elsif Is_Imported (Subp)
7241 and then (Convention (Subp) = Convention_CPP
7242 or else Convention (Subp) = Convention_C)
7243 then
7244 null;
7246 -- Generate code to register the primitive in non statically
7247 -- allocated dispatch tables
7249 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7251 -- When a primitive is frozen, enter its name in its dispatch
7252 -- table slot.
7254 if not Is_Interface (Typ)
7255 or else Present (Interface_Alias (Subp))
7256 then
7257 if Is_Predefined_Dispatching_Operation (Subp) then
7258 Register_Predefined_DT_Entry (Subp);
7259 end if;
7261 Insert_Actions_After (N,
7262 Register_Primitive (Loc, Prim => Subp));
7263 end if;
7264 end if;
7265 end;
7266 end if;
7268 -- Mark functions that return by reference. Note that it cannot be part
7269 -- of the normal semantic analysis of the spec since the underlying
7270 -- returned type may not be known yet (for private types).
7272 declare
7273 Typ : constant Entity_Id := Etype (Subp);
7274 Utyp : constant Entity_Id := Underlying_Type (Typ);
7276 begin
7277 if Is_Limited_View (Typ) then
7278 Set_Returns_By_Ref (Subp);
7280 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7281 Set_Returns_By_Ref (Subp);
7282 end if;
7283 end;
7285 -- Wnen freezing a null procedure, analyze its delayed aspects now
7286 -- because we may not have reached the end of the declarative list when
7287 -- delayed aspects are normally analyzed. This ensures that dispatching
7288 -- calls are properly rewritten when the generated _Postcondition
7289 -- procedure is analyzed in the null procedure body.
7291 if Nkind (Parent (Subp)) = N_Procedure_Specification
7292 and then Null_Present (Parent (Subp))
7293 then
7294 Analyze_Entry_Or_Subprogram_Contract (Subp);
7295 end if;
7296 end Freeze_Subprogram;
7298 -----------------------
7299 -- Is_Null_Procedure --
7300 -----------------------
7302 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
7303 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
7305 begin
7306 if Ekind (Subp) /= E_Procedure then
7307 return False;
7309 -- Check if this is a declared null procedure
7311 elsif Nkind (Decl) = N_Subprogram_Declaration then
7312 if not Null_Present (Specification (Decl)) then
7313 return False;
7315 elsif No (Body_To_Inline (Decl)) then
7316 return False;
7318 -- Check if the body contains only a null statement, followed by
7319 -- the return statement added during expansion.
7321 else
7322 declare
7323 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
7325 Stat : Node_Id;
7326 Stat2 : Node_Id;
7328 begin
7329 if Nkind (Orig_Bod) /= N_Subprogram_Body then
7330 return False;
7331 else
7332 -- We must skip SCIL nodes because they are currently
7333 -- implemented as special N_Null_Statement nodes.
7335 Stat :=
7336 First_Non_SCIL_Node
7337 (Statements (Handled_Statement_Sequence (Orig_Bod)));
7338 Stat2 := Next_Non_SCIL_Node (Stat);
7340 return
7341 Is_Empty_List (Declarations (Orig_Bod))
7342 and then Nkind (Stat) = N_Null_Statement
7343 and then
7344 (No (Stat2)
7345 or else
7346 (Nkind (Stat2) = N_Simple_Return_Statement
7347 and then No (Next (Stat2))));
7348 end if;
7349 end;
7350 end if;
7352 else
7353 return False;
7354 end if;
7355 end Is_Null_Procedure;
7357 -------------------------------------------
7358 -- Make_Build_In_Place_Call_In_Allocator --
7359 -------------------------------------------
7361 procedure Make_Build_In_Place_Call_In_Allocator
7362 (Allocator : Node_Id;
7363 Function_Call : Node_Id)
7365 Acc_Type : constant Entity_Id := Etype (Allocator);
7366 Loc : Source_Ptr;
7367 Func_Call : Node_Id := Function_Call;
7368 Ref_Func_Call : Node_Id;
7369 Function_Id : Entity_Id;
7370 Result_Subt : Entity_Id;
7371 New_Allocator : Node_Id;
7372 Return_Obj_Access : Entity_Id; -- temp for function result
7373 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
7374 Alloc_Form : BIP_Allocation_Form;
7375 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
7376 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
7377 Chain : Entity_Id; -- activation chain, in case of tasks
7379 begin
7380 -- Step past qualification or unchecked conversion (the latter can occur
7381 -- in cases of calls to 'Input).
7383 if Nkind_In (Func_Call,
7384 N_Qualified_Expression,
7385 N_Type_Conversion,
7386 N_Unchecked_Type_Conversion)
7387 then
7388 Func_Call := Expression (Func_Call);
7389 end if;
7391 -- If the call has already been processed to add build-in-place actuals
7392 -- then return. This should not normally occur in an allocator context,
7393 -- but we add the protection as a defensive measure.
7395 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7396 return;
7397 end if;
7399 -- Mark the call as processed as a build-in-place call
7401 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7403 Loc := Sloc (Function_Call);
7405 if Is_Entity_Name (Name (Func_Call)) then
7406 Function_Id := Entity (Name (Func_Call));
7408 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7409 Function_Id := Etype (Name (Func_Call));
7411 else
7412 raise Program_Error;
7413 end if;
7415 Result_Subt := Available_View (Etype (Function_Id));
7417 -- Create a temp for the function result. In the caller-allocates case,
7418 -- this will be initialized to the result of a new uninitialized
7419 -- allocator. Note: we do not use Allocator as the Related_Node of
7420 -- Return_Obj_Access in call to Make_Temporary below as this would
7421 -- create a sort of infinite "recursion".
7423 Return_Obj_Access := Make_Temporary (Loc, 'R');
7424 Set_Etype (Return_Obj_Access, Acc_Type);
7426 -- When the result subtype is constrained, the return object is
7427 -- allocated on the caller side, and access to it is passed to the
7428 -- function.
7430 -- Here and in related routines, we must examine the full view of the
7431 -- type, because the view at the point of call may differ from that
7432 -- that in the function body, and the expansion mechanism depends on
7433 -- the characteristics of the full view.
7435 if Is_Constrained (Underlying_Type (Result_Subt)) then
7437 -- Replace the initialized allocator of form "new T'(Func (...))"
7438 -- with an uninitialized allocator of form "new T", where T is the
7439 -- result subtype of the called function. The call to the function
7440 -- is handled separately further below.
7442 New_Allocator :=
7443 Make_Allocator (Loc,
7444 Expression => New_Occurrence_Of (Result_Subt, Loc));
7445 Set_No_Initialization (New_Allocator);
7447 -- Copy attributes to new allocator. Note that the new allocator
7448 -- logically comes from source if the original one did, so copy the
7449 -- relevant flag. This ensures proper treatment of the restriction
7450 -- No_Implicit_Heap_Allocations in this case.
7452 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
7453 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
7454 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
7456 Rewrite (Allocator, New_Allocator);
7458 -- Initial value of the temp is the result of the uninitialized
7459 -- allocator
7461 Temp_Init := Relocate_Node (Allocator);
7463 -- Indicate that caller allocates, and pass in the return object
7465 Alloc_Form := Caller_Allocation;
7466 Pool := Make_Null (No_Location);
7467 Return_Obj_Actual :=
7468 Make_Unchecked_Type_Conversion (Loc,
7469 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7470 Expression =>
7471 Make_Explicit_Dereference (Loc,
7472 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
7474 -- When the result subtype is unconstrained, the function itself must
7475 -- perform the allocation of the return object, so we pass parameters
7476 -- indicating that.
7478 else
7479 Temp_Init := Empty;
7481 -- Case of a user-defined storage pool. Pass an allocation parameter
7482 -- indicating that the function should allocate its result in the
7483 -- pool, and pass the pool. Use 'Unrestricted_Access because the
7484 -- pool may not be aliased.
7486 if Present (Associated_Storage_Pool (Acc_Type)) then
7487 Alloc_Form := User_Storage_Pool;
7488 Pool :=
7489 Make_Attribute_Reference (Loc,
7490 Prefix =>
7491 New_Occurrence_Of
7492 (Associated_Storage_Pool (Acc_Type), Loc),
7493 Attribute_Name => Name_Unrestricted_Access);
7495 -- No user-defined pool; pass an allocation parameter indicating that
7496 -- the function should allocate its result on the heap.
7498 else
7499 Alloc_Form := Global_Heap;
7500 Pool := Make_Null (No_Location);
7501 end if;
7503 -- The caller does not provide the return object in this case, so we
7504 -- have to pass null for the object access actual.
7506 Return_Obj_Actual := Empty;
7507 end if;
7509 -- Declare the temp object
7511 Insert_Action (Allocator,
7512 Make_Object_Declaration (Loc,
7513 Defining_Identifier => Return_Obj_Access,
7514 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
7515 Expression => Temp_Init));
7517 Ref_Func_Call := Make_Reference (Loc, Func_Call);
7519 -- Ada 2005 (AI-251): If the type of the allocator is an interface
7520 -- then generate an implicit conversion to force displacement of the
7521 -- "this" pointer.
7523 if Is_Interface (Designated_Type (Acc_Type)) then
7524 Rewrite
7525 (Ref_Func_Call,
7526 OK_Convert_To (Acc_Type, Ref_Func_Call));
7527 end if;
7529 declare
7530 Assign : constant Node_Id :=
7531 Make_Assignment_Statement (Loc,
7532 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
7533 Expression => Ref_Func_Call);
7534 -- Assign the result of the function call into the temp. In the
7535 -- caller-allocates case, this is overwriting the temp with its
7536 -- initial value, which has no effect. In the callee-allocates case,
7537 -- this is setting the temp to point to the object allocated by the
7538 -- callee.
7540 Actions : List_Id;
7541 -- Actions to be inserted. If there are no tasks, this is just the
7542 -- assignment statement. If the allocated object has tasks, we need
7543 -- to wrap the assignment in a block that activates them. The
7544 -- activation chain of that block must be passed to the function,
7545 -- rather than some outer chain.
7546 begin
7547 if Has_Task (Result_Subt) then
7548 Actions := New_List;
7549 Build_Task_Allocate_Block_With_Init_Stmts
7550 (Actions, Allocator, Init_Stmts => New_List (Assign));
7551 Chain := Activation_Chain_Entity (Last (Actions));
7552 else
7553 Actions := New_List (Assign);
7554 Chain := Empty;
7555 end if;
7557 Insert_Actions (Allocator, Actions);
7558 end;
7560 -- When the function has a controlling result, an allocation-form
7561 -- parameter must be passed indicating that the caller is allocating
7562 -- the result object. This is needed because such a function can be
7563 -- called as a dispatching operation and must be treated similarly
7564 -- to functions with unconstrained result subtypes.
7566 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7567 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
7569 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7570 (Func_Call, Function_Id, Acc_Type);
7572 Add_Task_Actuals_To_Build_In_Place_Call
7573 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
7574 Chain => Chain);
7576 -- Add an implicit actual to the function call that provides access
7577 -- to the allocated object. An unchecked conversion to the (specific)
7578 -- result subtype of the function is inserted to handle cases where
7579 -- the access type of the allocator has a class-wide designated type.
7581 Add_Access_Actual_To_Build_In_Place_Call
7582 (Func_Call, Function_Id, Return_Obj_Actual);
7584 -- Finally, replace the allocator node with a reference to the temp
7586 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
7588 Analyze_And_Resolve (Allocator, Acc_Type);
7589 end Make_Build_In_Place_Call_In_Allocator;
7591 ---------------------------------------------------
7592 -- Make_Build_In_Place_Call_In_Anonymous_Context --
7593 ---------------------------------------------------
7595 procedure Make_Build_In_Place_Call_In_Anonymous_Context
7596 (Function_Call : Node_Id)
7598 Loc : Source_Ptr;
7599 Func_Call : Node_Id := Function_Call;
7600 Function_Id : Entity_Id;
7601 Result_Subt : Entity_Id;
7602 Return_Obj_Id : Entity_Id;
7603 Return_Obj_Decl : Entity_Id;
7605 Definite : Boolean;
7606 -- True if result subtype is definite, or has a size that does not
7607 -- require secondary stack usage (i.e. no variant part or components
7608 -- whose type depends on discriminants). In particular, untagged types
7609 -- with only access discriminants do not require secondary stack use.
7610 -- Note that if the return type is tagged we must always use the sec.
7611 -- stack because the call may dispatch on result.
7613 begin
7614 -- Step past qualification, type conversion (which can occur in actual
7615 -- parameter contexts), and unchecked conversion (which can occur in
7616 -- cases of calls to 'Input).
7618 if Nkind_In (Func_Call, N_Qualified_Expression,
7619 N_Type_Conversion,
7620 N_Unchecked_Type_Conversion)
7621 then
7622 Func_Call := Expression (Func_Call);
7623 end if;
7625 -- If the call has already been processed to add build-in-place actuals
7626 -- then return. One place this can occur is for calls to build-in-place
7627 -- functions that occur within a call to a protected operation, where
7628 -- due to rewriting and expansion of the protected call there can be
7629 -- more than one call to Expand_Actuals for the same set of actuals.
7631 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7632 return;
7633 end if;
7635 -- Mark the call as processed as a build-in-place call
7637 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7639 Loc := Sloc (Function_Call);
7641 if Is_Entity_Name (Name (Func_Call)) then
7642 Function_Id := Entity (Name (Func_Call));
7644 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7645 Function_Id := Etype (Name (Func_Call));
7647 else
7648 raise Program_Error;
7649 end if;
7651 Result_Subt := Etype (Function_Id);
7652 Definite :=
7653 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
7654 and then not Is_Tagged_Type (Result_Subt))
7655 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
7657 -- If the build-in-place function returns a controlled object, then the
7658 -- object needs to be finalized immediately after the context. Since
7659 -- this case produces a transient scope, the servicing finalizer needs
7660 -- to name the returned object. Create a temporary which is initialized
7661 -- with the function call:
7663 -- Temp_Id : Func_Type := BIP_Func_Call;
7665 -- The initialization expression of the temporary will be rewritten by
7666 -- the expander using the appropriate mechanism in Make_Build_In_Place_
7667 -- Call_In_Object_Declaration.
7669 if Needs_Finalization (Result_Subt) then
7670 declare
7671 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
7672 Temp_Decl : Node_Id;
7674 begin
7675 -- Reset the guard on the function call since the following does
7676 -- not perform actual call expansion.
7678 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
7680 Temp_Decl :=
7681 Make_Object_Declaration (Loc,
7682 Defining_Identifier => Temp_Id,
7683 Object_Definition =>
7684 New_Occurrence_Of (Result_Subt, Loc),
7685 Expression =>
7686 New_Copy_Tree (Function_Call));
7688 Insert_Action (Function_Call, Temp_Decl);
7690 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
7691 Analyze (Function_Call);
7692 end;
7694 -- When the result subtype is definite, an object of the subtype is
7695 -- declared and an access value designating it is passed as an actual.
7697 elsif Definite then
7699 -- Create a temporary object to hold the function result
7701 Return_Obj_Id := Make_Temporary (Loc, 'R');
7702 Set_Etype (Return_Obj_Id, Result_Subt);
7704 Return_Obj_Decl :=
7705 Make_Object_Declaration (Loc,
7706 Defining_Identifier => Return_Obj_Id,
7707 Aliased_Present => True,
7708 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
7710 Set_No_Initialization (Return_Obj_Decl);
7712 Insert_Action (Func_Call, Return_Obj_Decl);
7714 -- When the function has a controlling result, an allocation-form
7715 -- parameter must be passed indicating that the caller is allocating
7716 -- the result object. This is needed because such a function can be
7717 -- called as a dispatching operation and must be treated similarly
7718 -- to functions with unconstrained result subtypes.
7720 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7721 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7723 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7724 (Func_Call, Function_Id);
7726 Add_Task_Actuals_To_Build_In_Place_Call
7727 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7729 -- Add an implicit actual to the function call that provides access
7730 -- to the caller's return object.
7732 Add_Access_Actual_To_Build_In_Place_Call
7733 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
7735 -- When the result subtype is unconstrained, the function must allocate
7736 -- the return object in the secondary stack, so appropriate implicit
7737 -- parameters are added to the call to indicate that. A transient
7738 -- scope is established to ensure eventual cleanup of the result.
7740 else
7741 -- Pass an allocation parameter indicating that the function should
7742 -- allocate its result on the secondary stack.
7744 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7745 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
7747 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7748 (Func_Call, Function_Id);
7750 Add_Task_Actuals_To_Build_In_Place_Call
7751 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7753 -- Pass a null value to the function since no return object is
7754 -- available on the caller side.
7756 Add_Access_Actual_To_Build_In_Place_Call
7757 (Func_Call, Function_Id, Empty);
7758 end if;
7759 end Make_Build_In_Place_Call_In_Anonymous_Context;
7761 --------------------------------------------
7762 -- Make_Build_In_Place_Call_In_Assignment --
7763 --------------------------------------------
7765 procedure Make_Build_In_Place_Call_In_Assignment
7766 (Assign : Node_Id;
7767 Function_Call : Node_Id)
7769 Lhs : constant Node_Id := Name (Assign);
7770 Func_Call : Node_Id := Function_Call;
7771 Func_Id : Entity_Id;
7772 Loc : Source_Ptr;
7773 Obj_Decl : Node_Id;
7774 Obj_Id : Entity_Id;
7775 Ptr_Typ : Entity_Id;
7776 Ptr_Typ_Decl : Node_Id;
7777 New_Expr : Node_Id;
7778 Result_Subt : Entity_Id;
7779 Target : Node_Id;
7781 begin
7782 -- Step past qualification or unchecked conversion (the latter can occur
7783 -- in cases of calls to 'Input).
7785 if Nkind_In (Func_Call, N_Qualified_Expression,
7786 N_Unchecked_Type_Conversion)
7787 then
7788 Func_Call := Expression (Func_Call);
7789 end if;
7791 -- If the call has already been processed to add build-in-place actuals
7792 -- then return. This should not normally occur in an assignment context,
7793 -- but we add the protection as a defensive measure.
7795 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7796 return;
7797 end if;
7799 -- Mark the call as processed as a build-in-place call
7801 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7803 Loc := Sloc (Function_Call);
7805 if Is_Entity_Name (Name (Func_Call)) then
7806 Func_Id := Entity (Name (Func_Call));
7808 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7809 Func_Id := Etype (Name (Func_Call));
7811 else
7812 raise Program_Error;
7813 end if;
7815 Result_Subt := Etype (Func_Id);
7817 -- When the result subtype is unconstrained, an additional actual must
7818 -- be passed to indicate that the caller is providing the return object.
7819 -- This parameter must also be passed when the called function has a
7820 -- controlling result, because dispatching calls to the function needs
7821 -- to be treated effectively the same as calls to class-wide functions.
7823 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7824 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
7826 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7827 (Func_Call, Func_Id);
7829 Add_Task_Actuals_To_Build_In_Place_Call
7830 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
7832 -- Add an implicit actual to the function call that provides access to
7833 -- the caller's return object.
7835 Add_Access_Actual_To_Build_In_Place_Call
7836 (Func_Call,
7837 Func_Id,
7838 Make_Unchecked_Type_Conversion (Loc,
7839 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7840 Expression => Relocate_Node (Lhs)));
7842 -- Create an access type designating the function's result subtype
7844 Ptr_Typ := Make_Temporary (Loc, 'A');
7846 Ptr_Typ_Decl :=
7847 Make_Full_Type_Declaration (Loc,
7848 Defining_Identifier => Ptr_Typ,
7849 Type_Definition =>
7850 Make_Access_To_Object_Definition (Loc,
7851 All_Present => True,
7852 Subtype_Indication =>
7853 New_Occurrence_Of (Result_Subt, Loc)));
7854 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
7856 -- Finally, create an access object initialized to a reference to the
7857 -- function call. We know this access value is non-null, so mark the
7858 -- entity accordingly to suppress junk access checks.
7860 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
7862 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
7863 Set_Etype (Obj_Id, Ptr_Typ);
7864 Set_Is_Known_Non_Null (Obj_Id);
7866 Obj_Decl :=
7867 Make_Object_Declaration (Loc,
7868 Defining_Identifier => Obj_Id,
7869 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
7870 Expression => New_Expr);
7871 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
7873 Rewrite (Assign, Make_Null_Statement (Loc));
7875 -- Retrieve the target of the assignment
7877 if Nkind (Lhs) = N_Selected_Component then
7878 Target := Selector_Name (Lhs);
7879 elsif Nkind (Lhs) = N_Type_Conversion then
7880 Target := Expression (Lhs);
7881 else
7882 Target := Lhs;
7883 end if;
7885 -- If we are assigning to a return object or this is an expression of
7886 -- an extension aggregate, the target should either be an identifier
7887 -- or a simple expression. All other cases imply a different scenario.
7889 if Nkind (Target) in N_Has_Entity then
7890 Target := Entity (Target);
7891 else
7892 return;
7893 end if;
7894 end Make_Build_In_Place_Call_In_Assignment;
7896 ----------------------------------------------------
7897 -- Make_Build_In_Place_Call_In_Object_Declaration --
7898 ----------------------------------------------------
7900 procedure Make_Build_In_Place_Call_In_Object_Declaration
7901 (Obj_Decl : Node_Id;
7902 Function_Call : Node_Id)
7904 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
7905 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
7906 Loc : constant Source_Ptr := Sloc (Function_Call);
7907 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
7909 Call_Deref : Node_Id;
7910 Caller_Object : Node_Id;
7911 Def_Id : Entity_Id;
7912 Fmaster_Actual : Node_Id := Empty;
7913 Func_Call : Node_Id := Function_Call;
7914 Function_Id : Entity_Id;
7915 Pool_Actual : Node_Id;
7916 Ptr_Typ : Entity_Id;
7917 Ptr_Typ_Decl : Node_Id;
7918 Pass_Caller_Acc : Boolean := False;
7919 Res_Decl : Node_Id;
7920 Result_Subt : Entity_Id;
7922 Definite : Boolean;
7923 -- True if result subtype is definite, or has a size that does not
7924 -- require secondary stack usage (i.e. no variant part or components
7925 -- whose type depends on discriminants). In particular, untagged types
7926 -- with only access discriminants do not require secondary stack use.
7927 -- Note that if the return type is tagged we must always use the sec.
7928 -- stack because the call may dispatch on result.
7930 begin
7931 -- Step past qualification or unchecked conversion (the latter can occur
7932 -- in cases of calls to 'Input).
7934 if Nkind_In (Func_Call, N_Qualified_Expression,
7935 N_Unchecked_Type_Conversion)
7936 then
7937 Func_Call := Expression (Func_Call);
7938 end if;
7940 -- If the call has already been processed to add build-in-place actuals
7941 -- then return. This should not normally occur in an object declaration,
7942 -- but we add the protection as a defensive measure.
7944 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7945 return;
7946 end if;
7948 -- Mark the call as processed as a build-in-place call
7950 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7952 if Is_Entity_Name (Name (Func_Call)) then
7953 Function_Id := Entity (Name (Func_Call));
7955 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7956 Function_Id := Etype (Name (Func_Call));
7958 else
7959 raise Program_Error;
7960 end if;
7962 Result_Subt := Etype (Function_Id);
7963 Definite :=
7964 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
7965 and then not Is_Tagged_Type (Result_Subt))
7966 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
7968 -- Create an access type designating the function's result subtype. We
7969 -- use the type of the original call because it may be a call to an
7970 -- inherited operation, which the expansion has replaced with the parent
7971 -- operation that yields the parent type. Note that this access type
7972 -- must be declared before we establish a transient scope, so that it
7973 -- receives the proper accessibility level.
7975 Ptr_Typ := Make_Temporary (Loc, 'A');
7976 Ptr_Typ_Decl :=
7977 Make_Full_Type_Declaration (Loc,
7978 Defining_Identifier => Ptr_Typ,
7979 Type_Definition =>
7980 Make_Access_To_Object_Definition (Loc,
7981 All_Present => True,
7982 Subtype_Indication =>
7983 New_Occurrence_Of (Etype (Function_Call), Loc)));
7985 -- The access type and its accompanying object must be inserted after
7986 -- the object declaration in the constrained case, so that the function
7987 -- call can be passed access to the object. In the indefinite case,
7988 -- or if the object declaration is for a return object, the access type
7989 -- and object must be inserted before the object, since the object
7990 -- declaration is rewritten to be a renaming of a dereference of the
7991 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
7992 -- the result object is in a different (transient) scope, so won't
7993 -- cause freezing.
7995 if Definite
7996 and then not Is_Return_Object (Defining_Identifier (Obj_Decl))
7997 then
7998 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
7999 else
8000 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
8001 end if;
8003 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8004 -- elaborated in an inner (transient) scope and thus won't cause
8005 -- freezing by itself.
8007 declare
8008 Ptr_Typ_Freeze_Ref : constant Node_Id :=
8009 New_Occurrence_Of (Ptr_Typ, Loc);
8010 begin
8011 Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl);
8012 Freeze_Expression (Ptr_Typ_Freeze_Ref);
8013 end;
8015 -- If the object is a return object of an enclosing build-in-place
8016 -- function, then the implicit build-in-place parameters of the
8017 -- enclosing function are simply passed along to the called function.
8018 -- (Unfortunately, this won't cover the case of extension aggregates
8019 -- where the ancestor part is a build-in-place indefinite function
8020 -- call that should be passed along the caller's parameters. Currently
8021 -- those get mishandled by reassigning the result of the call to the
8022 -- aggregate return object, when the call result should really be
8023 -- directly built in place in the aggregate and not in a temporary. ???)
8025 if Is_Return_Object (Defining_Identifier (Obj_Decl)) then
8026 Pass_Caller_Acc := True;
8028 -- When the enclosing function has a BIP_Alloc_Form formal then we
8029 -- pass it along to the callee (such as when the enclosing function
8030 -- has an unconstrained or tagged result type).
8032 if Needs_BIP_Alloc_Form (Encl_Func) then
8033 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8034 Pool_Actual :=
8035 New_Occurrence_Of
8036 (Build_In_Place_Formal (Encl_Func, BIP_Storage_Pool), Loc);
8038 -- The build-in-place pool formal is not built on e.g. ZFP
8040 else
8041 Pool_Actual := Empty;
8042 end if;
8044 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8045 (Function_Call => Func_Call,
8046 Function_Id => Function_Id,
8047 Alloc_Form_Exp =>
8048 New_Occurrence_Of
8049 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
8050 Pool_Actual => Pool_Actual);
8052 -- Otherwise, if enclosing function has a definite result subtype,
8053 -- then caller allocation will be used.
8055 else
8056 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8057 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8058 end if;
8060 if Needs_BIP_Finalization_Master (Encl_Func) then
8061 Fmaster_Actual :=
8062 New_Occurrence_Of
8063 (Build_In_Place_Formal
8064 (Encl_Func, BIP_Finalization_Master), Loc);
8065 end if;
8067 -- Retrieve the BIPacc formal from the enclosing function and convert
8068 -- it to the access type of the callee's BIP_Object_Access formal.
8070 Caller_Object :=
8071 Make_Unchecked_Type_Conversion (Loc,
8072 Subtype_Mark =>
8073 New_Occurrence_Of
8074 (Etype
8075 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
8076 Loc),
8077 Expression =>
8078 New_Occurrence_Of
8079 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access),
8080 Loc));
8082 -- In the definite case, add an implicit actual to the function call
8083 -- that provides access to the declared object. An unchecked conversion
8084 -- to the (specific) result type of the function is inserted to handle
8085 -- the case where the object is declared with a class-wide type.
8087 elsif Definite then
8088 Caller_Object :=
8089 Make_Unchecked_Type_Conversion (Loc,
8090 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8091 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
8093 -- When the function has a controlling result, an allocation-form
8094 -- parameter must be passed indicating that the caller is allocating
8095 -- the result object. This is needed because such a function can be
8096 -- called as a dispatching operation and must be treated similarly
8097 -- to functions with indefinite result subtypes.
8099 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8100 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8102 -- The allocation for indefinite library-level objects occurs on the
8103 -- heap as opposed to the secondary stack. This accommodates DLLs where
8104 -- the secondary stack is destroyed after each library unload. This is
8105 -- a hybrid mechanism where a stack-allocated object lives on the heap.
8107 elsif Is_Library_Level_Entity (Defining_Identifier (Obj_Decl))
8108 and then not Restriction_Active (No_Implicit_Heap_Allocations)
8109 then
8110 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8111 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
8112 Caller_Object := Empty;
8114 -- Create a finalization master for the access result type to ensure
8115 -- that the heap allocation can properly chain the object and later
8116 -- finalize it when the library unit goes out of scope.
8118 if Needs_Finalization (Etype (Func_Call)) then
8119 Build_Finalization_Master
8120 (Typ => Ptr_Typ,
8121 For_Lib_Level => True,
8122 Insertion_Node => Ptr_Typ_Decl);
8124 Fmaster_Actual :=
8125 Make_Attribute_Reference (Loc,
8126 Prefix =>
8127 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
8128 Attribute_Name => Name_Unrestricted_Access);
8129 end if;
8131 -- In other indefinite cases, pass an indication to do the allocation
8132 -- on the secondary stack and set Caller_Object to Empty so that a null
8133 -- value will be passed for the caller's object address. A transient
8134 -- scope is established to ensure eventual cleanup of the result.
8136 else
8137 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8138 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8139 Caller_Object := Empty;
8141 Establish_Transient_Scope (Obj_Decl, Sec_Stack => True);
8142 end if;
8144 -- Pass along any finalization master actual, which is needed in the
8145 -- case where the called function initializes a return object of an
8146 -- enclosing build-in-place function.
8148 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8149 (Func_Call => Func_Call,
8150 Func_Id => Function_Id,
8151 Master_Exp => Fmaster_Actual);
8153 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
8154 and then Has_Task (Result_Subt)
8155 then
8156 -- Here we're passing along the master that was passed in to this
8157 -- function.
8159 Add_Task_Actuals_To_Build_In_Place_Call
8160 (Func_Call, Function_Id,
8161 Master_Actual =>
8162 New_Occurrence_Of
8163 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
8165 else
8166 Add_Task_Actuals_To_Build_In_Place_Call
8167 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8168 end if;
8170 Add_Access_Actual_To_Build_In_Place_Call
8171 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
8173 -- Finally, create an access object initialized to a reference to the
8174 -- function call. We know this access value cannot be null, so mark the
8175 -- entity accordingly to suppress the access check.
8177 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
8178 Set_Etype (Def_Id, Ptr_Typ);
8179 Set_Is_Known_Non_Null (Def_Id);
8181 Res_Decl :=
8182 Make_Object_Declaration (Loc,
8183 Defining_Identifier => Def_Id,
8184 Constant_Present => True,
8185 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8186 Expression =>
8187 Make_Reference (Loc, Relocate_Node (Func_Call)));
8189 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
8191 -- If the result subtype of the called function is definite and is not
8192 -- itself the return expression of an enclosing BIP function, then mark
8193 -- the object as having no initialization.
8195 if Definite
8196 and then not Is_Return_Object (Defining_Identifier (Obj_Decl))
8197 then
8198 -- The related object declaration is encased in a transient block
8199 -- because the build-in-place function call contains at least one
8200 -- nested function call that produces a controlled transient
8201 -- temporary:
8203 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8205 -- Since the build-in-place expansion decouples the call from the
8206 -- object declaration, the finalization machinery lacks the context
8207 -- which prompted the generation of the transient block. To resolve
8208 -- this scenario, store the build-in-place call.
8210 if Scope_Is_Transient and then Node_To_Be_Wrapped = Obj_Decl then
8211 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
8212 end if;
8214 Set_Expression (Obj_Decl, Empty);
8215 Set_No_Initialization (Obj_Decl);
8217 -- In case of an indefinite result subtype, or if the call is the
8218 -- return expression of an enclosing BIP function, rewrite the object
8219 -- declaration as an object renaming where the renamed object is a
8220 -- dereference of <function_Call>'reference:
8222 -- Obj : Subt renames <function_call>'Ref.all;
8224 else
8225 Call_Deref :=
8226 Make_Explicit_Dereference (Obj_Loc,
8227 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
8229 Rewrite (Obj_Decl,
8230 Make_Object_Renaming_Declaration (Obj_Loc,
8231 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
8232 Subtype_Mark => New_Occurrence_Of (Result_Subt, Obj_Loc),
8233 Name => Call_Deref));
8235 Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref);
8237 -- If the original entity comes from source, then mark the new
8238 -- entity as needing debug information, even though it's defined
8239 -- by a generated renaming that does not come from source, so that
8240 -- the Materialize_Entity flag will be set on the entity when
8241 -- Debug_Renaming_Declaration is called during analysis.
8243 if Comes_From_Source (Obj_Def_Id) then
8244 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
8245 end if;
8247 Analyze (Obj_Decl);
8249 -- Replace the internal identifier of the renaming declaration's
8250 -- entity with identifier of the original object entity. We also have
8251 -- to exchange the entities containing their defining identifiers to
8252 -- ensure the correct replacement of the object declaration by the
8253 -- object renaming declaration to avoid homograph conflicts (since
8254 -- the object declaration's defining identifier was already entered
8255 -- in current scope). The Next_Entity links of the two entities also
8256 -- have to be swapped since the entities are part of the return
8257 -- scope's entity list and the list structure would otherwise be
8258 -- corrupted. Finally, the homonym chain must be preserved as well.
8260 declare
8261 Ren_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
8262 Next_Id : constant Entity_Id := Next_Entity (Ren_Id);
8264 begin
8265 Set_Chars (Ren_Id, Chars (Obj_Def_Id));
8267 -- Swap next entity links in preparation for exchanging entities
8269 Set_Next_Entity (Ren_Id, Next_Entity (Obj_Def_Id));
8270 Set_Next_Entity (Obj_Def_Id, Next_Id);
8271 Set_Homonym (Ren_Id, Homonym (Obj_Def_Id));
8273 Exchange_Entities (Ren_Id, Obj_Def_Id);
8275 -- Preserve source indication of original declaration, so that
8276 -- xref information is properly generated for the right entity.
8278 Preserve_Comes_From_Source (Obj_Decl, Original_Node (Obj_Decl));
8279 Preserve_Comes_From_Source (Obj_Def_Id, Original_Node (Obj_Decl));
8281 Set_Comes_From_Source (Ren_Id, False);
8282 end;
8283 end if;
8285 -- If the object entity has a class-wide Etype, then we need to change
8286 -- it to the result subtype of the function call, because otherwise the
8287 -- object will be class-wide without an explicit initialization and
8288 -- won't be allocated properly by the back end. It seems unclean to make
8289 -- such a revision to the type at this point, and we should try to
8290 -- improve this treatment when build-in-place functions with class-wide
8291 -- results are implemented. ???
8293 if Is_Class_Wide_Type (Etype (Defining_Identifier (Obj_Decl))) then
8294 Set_Etype (Defining_Identifier (Obj_Decl), Result_Subt);
8295 end if;
8296 end Make_Build_In_Place_Call_In_Object_Declaration;
8298 --------------------------------------------
8299 -- Make_CPP_Constructor_Call_In_Allocator --
8300 --------------------------------------------
8302 procedure Make_CPP_Constructor_Call_In_Allocator
8303 (Allocator : Node_Id;
8304 Function_Call : Node_Id)
8306 Loc : constant Source_Ptr := Sloc (Function_Call);
8307 Acc_Type : constant Entity_Id := Etype (Allocator);
8308 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
8309 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
8311 New_Allocator : Node_Id;
8312 Return_Obj_Access : Entity_Id;
8313 Tmp_Obj : Node_Id;
8315 begin
8316 pragma Assert (Nkind (Allocator) = N_Allocator
8317 and then Nkind (Function_Call) = N_Function_Call);
8318 pragma Assert (Convention (Function_Id) = Convention_CPP
8319 and then Is_Constructor (Function_Id));
8320 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
8322 -- Replace the initialized allocator of form "new T'(Func (...))" with
8323 -- an uninitialized allocator of form "new T", where T is the result
8324 -- subtype of the called function. The call to the function is handled
8325 -- separately further below.
8327 New_Allocator :=
8328 Make_Allocator (Loc,
8329 Expression => New_Occurrence_Of (Result_Subt, Loc));
8330 Set_No_Initialization (New_Allocator);
8332 -- Copy attributes to new allocator. Note that the new allocator
8333 -- logically comes from source if the original one did, so copy the
8334 -- relevant flag. This ensures proper treatment of the restriction
8335 -- No_Implicit_Heap_Allocations in this case.
8337 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8338 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8339 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8341 Rewrite (Allocator, New_Allocator);
8343 -- Create a new access object and initialize it to the result of the
8344 -- new uninitialized allocator. Note: we do not use Allocator as the
8345 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
8346 -- as this would create a sort of infinite "recursion".
8348 Return_Obj_Access := Make_Temporary (Loc, 'R');
8349 Set_Etype (Return_Obj_Access, Acc_Type);
8351 -- Generate:
8352 -- Rnnn : constant ptr_T := new (T);
8353 -- Init (Rnn.all,...);
8355 Tmp_Obj :=
8356 Make_Object_Declaration (Loc,
8357 Defining_Identifier => Return_Obj_Access,
8358 Constant_Present => True,
8359 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8360 Expression => Relocate_Node (Allocator));
8361 Insert_Action (Allocator, Tmp_Obj);
8363 Insert_List_After_And_Analyze (Tmp_Obj,
8364 Build_Initialization_Call (Loc,
8365 Id_Ref =>
8366 Make_Explicit_Dereference (Loc,
8367 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
8368 Typ => Etype (Function_Id),
8369 Constructor_Ref => Function_Call));
8371 -- Finally, replace the allocator node with a reference to the result of
8372 -- the function call itself (which will effectively be an access to the
8373 -- object created by the allocator).
8375 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8377 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
8378 -- generate an implicit conversion to force displacement of the "this"
8379 -- pointer.
8381 if Is_Interface (Designated_Type (Acc_Type)) then
8382 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
8383 end if;
8385 Analyze_And_Resolve (Allocator, Acc_Type);
8386 end Make_CPP_Constructor_Call_In_Allocator;
8388 -----------------------------------
8389 -- Needs_BIP_Finalization_Master --
8390 -----------------------------------
8392 function Needs_BIP_Finalization_Master
8393 (Func_Id : Entity_Id) return Boolean
8395 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8396 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8397 begin
8398 -- A formal giving the finalization master is needed for build-in-place
8399 -- functions whose result type needs finalization or is a tagged type.
8400 -- Tagged primitive build-in-place functions need such a formal because
8401 -- they can be called by a dispatching call, and extensions may require
8402 -- finalization even if the root type doesn't. This means they're also
8403 -- needed for tagged nonprimitive build-in-place functions with tagged
8404 -- results, since such functions can be called via access-to-function
8405 -- types, and those can be used to call primitives, so masters have to
8406 -- be passed to all such build-in-place functions, primitive or not.
8408 return
8409 not Restriction_Active (No_Finalization)
8410 and then (Needs_Finalization (Func_Typ)
8411 or else Is_Tagged_Type (Func_Typ));
8412 end Needs_BIP_Finalization_Master;
8414 --------------------------
8415 -- Needs_BIP_Alloc_Form --
8416 --------------------------
8418 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
8419 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8420 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8421 begin
8422 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
8423 end Needs_BIP_Alloc_Form;
8425 --------------------------------------
8426 -- Needs_Result_Accessibility_Level --
8427 --------------------------------------
8429 function Needs_Result_Accessibility_Level
8430 (Func_Id : Entity_Id) return Boolean
8432 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8434 function Has_Unconstrained_Access_Discriminant_Component
8435 (Comp_Typ : Entity_Id) return Boolean;
8436 -- Returns True if any component of the type has an unconstrained access
8437 -- discriminant.
8439 -----------------------------------------------------
8440 -- Has_Unconstrained_Access_Discriminant_Component --
8441 -----------------------------------------------------
8443 function Has_Unconstrained_Access_Discriminant_Component
8444 (Comp_Typ : Entity_Id) return Boolean
8446 begin
8447 if not Is_Limited_Type (Comp_Typ) then
8448 return False;
8450 -- Only limited types can have access discriminants with
8451 -- defaults.
8453 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
8454 return True;
8456 elsif Is_Array_Type (Comp_Typ) then
8457 return Has_Unconstrained_Access_Discriminant_Component
8458 (Underlying_Type (Component_Type (Comp_Typ)));
8460 elsif Is_Record_Type (Comp_Typ) then
8461 declare
8462 Comp : Entity_Id;
8464 begin
8465 Comp := First_Component (Comp_Typ);
8466 while Present (Comp) loop
8467 if Has_Unconstrained_Access_Discriminant_Component
8468 (Underlying_Type (Etype (Comp)))
8469 then
8470 return True;
8471 end if;
8473 Next_Component (Comp);
8474 end loop;
8475 end;
8476 end if;
8478 return False;
8479 end Has_Unconstrained_Access_Discriminant_Component;
8481 Feature_Disabled : constant Boolean := True;
8482 -- Temporary
8484 -- Start of processing for Needs_Result_Accessibility_Level
8486 begin
8487 -- False if completion unavailable (how does this happen???)
8489 if not Present (Func_Typ) then
8490 return False;
8492 elsif Feature_Disabled then
8493 return False;
8495 -- False if not a function, also handle enum-lit renames case
8497 elsif Func_Typ = Standard_Void_Type
8498 or else Is_Scalar_Type (Func_Typ)
8499 then
8500 return False;
8502 -- Handle a corner case, a cross-dialect subp renaming. For example,
8503 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
8504 -- an Ada 2005 (or earlier) unit references predefined run-time units.
8506 elsif Present (Alias (Func_Id)) then
8508 -- Unimplemented: a cross-dialect subp renaming which does not set
8509 -- the Alias attribute (e.g., a rename of a dereference of an access
8510 -- to subprogram value). ???
8512 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
8514 -- Remaining cases require Ada 2012 mode
8516 elsif Ada_Version < Ada_2012 then
8517 return False;
8519 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
8520 or else Is_Tagged_Type (Func_Typ)
8521 then
8522 -- In the case of, say, a null tagged record result type, the need
8523 -- for this extra parameter might not be obvious. This function
8524 -- returns True for all tagged types for compatibility reasons.
8525 -- A function with, say, a tagged null controlling result type might
8526 -- be overridden by a primitive of an extension having an access
8527 -- discriminant and the overrider and overridden must have compatible
8528 -- calling conventions (including implicitly declared parameters).
8529 -- Similarly, values of one access-to-subprogram type might designate
8530 -- both a primitive subprogram of a given type and a function
8531 -- which is, for example, not a primitive subprogram of any type.
8532 -- Again, this requires calling convention compatibility.
8533 -- It might be possible to solve these issues by introducing
8534 -- wrappers, but that is not the approach that was chosen.
8536 return True;
8538 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
8539 return True;
8541 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
8542 return True;
8544 -- False for all other cases
8546 else
8547 return False;
8548 end if;
8549 end Needs_Result_Accessibility_Level;
8551 ---------------------------------
8552 -- Rewrite_Function_Call_For_C --
8553 ---------------------------------
8555 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
8556 Orig_Func : constant Entity_Id := Entity (Name (N));
8557 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
8558 Par : constant Node_Id := Parent (N);
8559 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
8560 Loc : constant Source_Ptr := Sloc (Par);
8561 Actuals : List_Id;
8562 Last_Actual : Node_Id;
8563 Last_Formal : Entity_Id;
8565 -- Start of processing for Rewrite_Function_Call_For_C
8567 begin
8568 -- The actuals may be given by named associations, so the added actual
8569 -- that is the target of the return value of the call must be a named
8570 -- association as well, so we retrieve the name of the generated
8571 -- out_formal.
8573 Last_Formal := First_Formal (Proc_Id);
8574 while Present (Next_Formal (Last_Formal)) loop
8575 Last_Formal := Next_Formal (Last_Formal);
8576 end loop;
8578 Actuals := Parameter_Associations (N);
8580 -- The original function may lack parameters
8582 if No (Actuals) then
8583 Actuals := New_List;
8584 end if;
8586 -- If the function call is the expression of an assignment statement,
8587 -- transform the assignment into a procedure call. Generate:
8589 -- LHS := Func_Call (...);
8591 -- Proc_Call (..., LHS);
8593 -- If function is inherited, a conversion may be necessary.
8595 if Nkind (Par) = N_Assignment_Statement then
8596 Last_Actual := Name (Par);
8598 if not Comes_From_Source (Orig_Func)
8599 and then Etype (Orig_Func) /= Etype (Func_Id)
8600 then
8601 Last_Actual :=
8602 Make_Type_Conversion (Loc,
8603 New_Occurrence_Of (Etype (Func_Id), Loc),
8604 Last_Actual);
8605 end if;
8607 Append_To (Actuals,
8608 Make_Parameter_Association (Loc,
8609 Selector_Name =>
8610 Make_Identifier (Loc, Chars (Last_Formal)),
8611 Explicit_Actual_Parameter => Last_Actual));
8613 Rewrite (Par,
8614 Make_Procedure_Call_Statement (Loc,
8615 Name => New_Occurrence_Of (Proc_Id, Loc),
8616 Parameter_Associations => Actuals));
8617 Analyze (Par);
8619 -- Otherwise the context is an expression. Generate a temporary and a
8620 -- procedure call to obtain the function result. Generate:
8622 -- ... Func_Call (...) ...
8624 -- Temp : ...;
8625 -- Proc_Call (..., Temp);
8626 -- ... Temp ...
8628 else
8629 declare
8630 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
8631 Call : Node_Id;
8632 Decl : Node_Id;
8634 begin
8635 -- Generate:
8636 -- Temp : ...;
8638 Decl :=
8639 Make_Object_Declaration (Loc,
8640 Defining_Identifier => Temp_Id,
8641 Object_Definition =>
8642 New_Occurrence_Of (Etype (Func_Id), Loc));
8644 -- Generate:
8645 -- Proc_Call (..., Temp);
8647 Append_To (Actuals,
8648 Make_Parameter_Association (Loc,
8649 Selector_Name =>
8650 Make_Identifier (Loc, Chars (Last_Formal)),
8651 Explicit_Actual_Parameter =>
8652 New_Occurrence_Of (Temp_Id, Loc)));
8654 Call :=
8655 Make_Procedure_Call_Statement (Loc,
8656 Name => New_Occurrence_Of (Proc_Id, Loc),
8657 Parameter_Associations => Actuals);
8659 Insert_Actions (Par, New_List (Decl, Call));
8660 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
8661 end;
8662 end if;
8663 end Rewrite_Function_Call_For_C;
8665 ------------------------------------
8666 -- Set_Enclosing_Sec_Stack_Return --
8667 ------------------------------------
8669 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
8670 P : Node_Id := N;
8672 begin
8673 -- Due to a possible mix of internally generated blocks, source blocks
8674 -- and loops, the scope stack may not be contiguous as all labels are
8675 -- inserted at the top level within the related function. Instead,
8676 -- perform a parent-based traversal and mark all appropriate constructs.
8678 while Present (P) loop
8680 -- Mark the label of a source or internally generated block or
8681 -- loop.
8683 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
8684 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
8686 -- Mark the enclosing function
8688 elsif Nkind (P) = N_Subprogram_Body then
8689 if Present (Corresponding_Spec (P)) then
8690 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
8691 else
8692 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
8693 end if;
8695 -- Do not go beyond the enclosing function
8697 exit;
8698 end if;
8700 P := Parent (P);
8701 end loop;
8702 end Set_Enclosing_Sec_Stack_Return;
8704 end Exp_Ch6;