1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
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;
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.
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
211 Scop : Entity_Id) return Node_Id;
213 procedure Expand_Protected_Subprogram_Call
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;
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.
272 Obj_Address := Return_Object;
273 Set_Parent (Obj_Address, Function_Call);
275 -- Apply Unrestricted_Access to caller's return object
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);
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;
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)))
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;
337 pragma Assert (Alloc_Form /= Unspecified);
340 Make_Integer_Literal (Loc,
341 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
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);
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;
370 Ptr_Typ : Entity_Id := Empty;
371 Master_Exp : Node_Id := Empty)
374 if not Needs_BIP_Finalization_Master (Func_Id) then
379 Formal : constant Entity_Id :=
380 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
381 Loc : constant Source_Ptr := Sloc (Func_Call);
384 Desig_Typ : Entity_Id;
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);
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
407 if Is_Library_Level_Entity (Ptr_Typ)
408 and then Finalize_Storage_Only (Desig_Typ)
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))
424 Build_Anonymous_Master (Ptr_Typ);
427 -- Access-to-controlled types should always have a master
429 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
432 Make_Attribute_Reference (Loc,
434 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
435 Attribute_Name => Name_Unrestricted_Access);
440 Actual := Make_Null (Loc);
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);
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;
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
479 -- Find last named actual, and append
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);
495 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
498 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
501 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
502 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
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));
520 Chain_Actual : Node_Id;
521 Chain_Formal : Node_Id;
522 Master_Formal : Node_Id;
525 -- No such extra parameters are needed if there are no tasks
527 if not Has_Task (Result_Subt) then
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);
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
558 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
560 -- Create the actual which is a pointer to the current activation chain
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
572 Make_Attribute_Reference (Loc,
573 Prefix => New_Occurrence_Of (Chain, Loc),
574 Attribute_Name => Name_Unrestricted_Access);
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
592 when BIP_Alloc_Form =>
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 =>
610 end BIP_Formal_Suffix;
612 ---------------------------
613 -- Build_In_Place_Formal --
614 ---------------------------
616 function Build_In_Place_Formal
618 Kind : BIP_Formal_Kind) return Entity_Id
620 Formal_Name : constant Name_Id :=
622 (Chars (Func), BIP_Formal_Suffix (Kind));
623 Extra_Formal : Entity_Id := Extra_Formals (Func);
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);
640 pragma Assert (Present (Extra_Formal));
641 exit when Chars (Extra_Formal) = Formal_Name;
643 Next_Formal_With_Extras (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
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
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
688 Alt := First (Alternatives (Stmt));
689 while Present (Alt) loop
690 Replace_Returns (Param_Id, Statements (Alt));
695 elsif Nkind (Stmt) = N_Extended_Return_Statement then
697 Ret_Obj : constant Entity_Id :=
699 (First (Return_Object_Declarations (Stmt)));
700 Assign : constant Node_Id :=
701 Make_Assignment_Statement (Sloc (Stmt),
703 New_Occurrence_Of (Param_Id, Loc),
705 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
709 -- The extended return may just contain the declaration
711 if Present (Handled_Statement_Sequence (Stmt)) then
712 Stmts := Statements (Handled_Statement_Sequence (Stmt));
717 Set_Assignment_OK (Name (Assign));
720 Make_Block_Statement (Sloc (Stmt),
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));
733 elsif Nkind (Stmt) = N_If_Statement then
734 Replace_Returns (Param_Id, Then_Statements (Stmt));
735 Replace_Returns (Param_Id, Else_Statements (Stmt));
740 Part := First (Elsif_Parts (Stmt));
741 while Present (Part) loop
742 Replace_Returns (Param_Id, Then_Statements (Part));
747 elsif Nkind (Stmt) = N_Loop_Statement then
748 Replace_Returns (Param_Id, Statements (Stmt));
750 elsif Nkind (Stmt) = N_Simple_Return_Statement then
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
777 -- Start of processing for Build_Procedure_Body_Form
780 -- This routine replaces the original function body:
782 -- function F (...) return Array_Typ is
788 -- with the following:
790 -- procedure P (..., Result : out Array_Typ) is
793 -- Result := Something;
797 Statements (Handled_Statement_Sequence (Func_Body));
798 Replace_Returns (Last_Entity (Proc_Id), Stmts);
801 Make_Subprogram_Body (Loc,
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
813 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
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);
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)
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);
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)
851 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
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
896 function Process (Nod : Node_Id) return Traverse_Result;
897 -- Function to traverse the subprogram body (using Traverse_Func)
903 function Process (Nod : Node_Id) return Traverse_Result is
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
915 Append_Elmt (Nod, Call_List);
918 -- Any other procedure call may have side effects
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)))
932 -- Case of an identifier reference
934 elsif Nkind (Nod) = N_Identifier then
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 ???
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
951 -- Ignore the reference if not to a more global object
953 elsif Scope_Depth (Scope (Ent)) >= Scop then
956 -- References to types, exceptions and constants are always OK
959 or else Ekind (Ent) = E_Exception
960 or else Ekind (Ent) = E_Constant
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)
974 -- Otherwise we have a reference to a global scalar
977 -- Loop through global entities already detected
979 Elm := First_Elmt (Var_List);
981 -- If not detected before, record this new global reference
984 Count_Vars := Count_Vars + 1;
986 if Count_Vars <= Max_Vars then
987 Append_Elmt (Entity (Nod), Var_List);
994 -- If recorded before, ignore
996 elsif Node (Elm) = Entity (Nod) then
999 -- Otherwise keep looking
1009 -- For all other node kinds, recursively visit syntactic children
1016 function Traverse_Body is new Traverse_Func (Process);
1018 -- Start of processing for Detect_Infinite_Recursion
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
1025 if Restriction_Active (No_Implicit_Conditionals) then
1029 -- Otherwise do traversal and quit if we get abandon signal
1031 if Traverse_Body (N) = Abandon then
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
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.
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
1055 Elm := First_Elmt (Var_List);
1056 while Present (Elm) loop
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.
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));
1074 Prepend (Decl, Declarations (N));
1076 Insert_After (Last, Decl);
1084 -- Loop through calls
1086 Call := First_Elmt (Call_List);
1087 while Present (Call) loop
1089 -- Build a predicate expression of the form
1092 -- and then global1 = temp1
1093 -- and then global2 = temp2
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
1110 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1111 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1117 -- Now we replace the call with the sequence
1119 -- if no-changes (see above) then
1120 -- raise Storage_Error;
1125 Rewrite (Node (Call),
1126 Make_If_Statement (Loc,
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));
1140 -- Remove temporary scope stack entry used for analysis
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);
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
1203 F_Typ : Entity_Id := Etype (Formal);
1211 if not Is_Legal_Copy then
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)
1222 F_Typ := Non_Limited_View (F_Typ);
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);
1232 Indic := New_Occurrence_Of (F_Typ, Loc);
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)));
1247 V_Typ := Etype (Actual);
1248 Var := Make_Var (Actual);
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
1261 if Ekind (Formal) = E_In_Out_Parameter
1262 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
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));
1268 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
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)
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).
1282 Make_Subtype_Indication (Loc,
1283 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1285 Make_Index_Or_Discriminant_Constraint (Loc,
1286 Constraints => New_List (
1289 Make_Attribute_Reference (Loc,
1290 Prefix => New_Occurrence_Of (Var, Loc),
1291 Attribute_Name => Name_First),
1293 Make_Attribute_Reference (Loc,
1294 Prefix => New_Occurrence_Of (Var, Loc),
1295 Attribute_Name => Name_Last)))));
1298 Init := New_Occurrence_Of (Var, Loc);
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)
1311 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1313 if Conversion_OK (Actual) then
1314 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1316 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
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));
1327 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1330 Init := New_Occurrence_Of (Var, Loc);
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.
1361 and then Present (Expression (N_Node))
1362 and then Is_Entity_Name (Expression (N_Node))
1364 Temp := Entity (Expression (N_Node));
1365 Rewrite (N_Node, Make_Null_Statement (Loc));
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));
1374 -- Processing for OUT or IN OUT parameter
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));
1389 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1392 Expr := New_Occurrence_Of (Temp, Loc);
1395 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
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.
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
1416 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1418 Obj := Renamed_Object (Var);
1420 Make_Selected_Component (Loc,
1422 New_Copy_Tree (Original_Node (Prefix (Obj))),
1423 Selector_Name => New_Copy (Selector_Name (Obj)));
1424 Reset_Analyzed_Flags (Lhs);
1427 Lhs := New_Occurrence_Of (Var, Loc);
1430 Set_Assignment_OK (Lhs);
1432 if Is_Access_Type (E_Formal)
1433 and then Is_Entity_Name (Lhs)
1435 Present (Effective_Extra_Accessibility (Entity (Lhs)))
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)
1445 Append_To (Post_Call,
1446 Make_Raise_Program_Error (Loc,
1447 Reason => PE_Accessibility_Check_Failed));
1450 Append_To (Post_Call,
1451 Make_Assignment_Statement (Loc,
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))));
1470 Append_To (Post_Call,
1471 Make_Assignment_Statement (Loc,
1473 Expression => Expr));
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
1485 F_Typ : Entity_Id := Etype (Formal);
1494 if not Is_Legal_Copy then
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)
1503 F_Typ := Non_Limited_View (F_Typ);
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);
1513 Indic := New_Occurrence_Of (F_Typ, Loc);
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
1536 if Has_Discriminants (F_Typ) then
1537 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1540 elsif Inside_Init_Proc then
1542 -- Could use a comment here to match comment below ???
1544 if Nkind (Actual) /= N_Selected_Component
1546 not Has_Discriminant_Dependent_Constraint
1547 (Entity (Selector_Name (Actual)))
1551 -- Otherwise, keep the component in order to generate the proper
1552 -- actual subtype, that depends on enclosing discriminants.
1560 Make_Object_Declaration (Loc,
1561 Defining_Identifier => Temp,
1562 Object_Definition => Indic,
1563 Expression => Incod);
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));
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
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);
1601 Append_To (Post_Call,
1602 Make_Assignment_Statement (Loc,
1604 Expression => Rhs));
1605 Set_Assignment_OK (Name (Last (Post_Call)));
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);
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???)
1621 if Convention (Subp) = Convention_Fortran
1622 and then Root_Type (Etype (Formal)) = Standard_Boolean
1623 and then Ekind (Formal) /= E_In_Parameter
1625 Var := Make_Var (Actual);
1626 Append_To (Post_Call,
1627 Make_Assignment_Statement (Loc,
1628 Name => New_Occurrence_Of (Var, Loc),
1630 Unchecked_Convert_To (
1633 Left_Opnd => New_Occurrence_Of (Var, Loc),
1635 Unchecked_Convert_To (
1637 New_Occurrence_Of (Standard_False, Loc))))));
1639 end Check_Fortran_Logical;
1645 function Is_Legal_Copy return Boolean is
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
1663 ("misaligned actual cannot be passed by reference", Actual);
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
1674 elsif Mechanism (Formal) = By_Reference
1675 and then Is_Valued_Procedure (Scope (Formal))
1678 ("by_reference actual may be misaligned??", Actual);
1690 function Make_Var (Actual : Node_Id) return Entity_Id is
1694 if Is_Entity_Name (Actual) then
1695 return Entity (Actual);
1698 Var := Make_Temporary (Loc, 'T', Actual);
1701 Make_Object_Renaming_Declaration (Loc,
1702 Defining_Identifier => Var,
1704 New_Occurrence_Of (Etype (Actual), Loc),
1705 Name => Relocate_Node (Actual));
1707 Insert_Action (N, N_Node);
1712 -------------------------
1713 -- Reset_Packed_Prefix --
1714 -------------------------
1716 procedure Reset_Packed_Prefix is
1717 Pfx : Node_Id := Actual;
1720 Set_Analyzed (Pfx, False);
1722 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1723 Pfx := Prefix (Pfx);
1725 end Reset_Packed_Prefix;
1727 -- Start of processing for Expand_Actuals
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)
1743 E_Formal := Non_Limited_View (E_Formal);
1746 if Is_Scalar_Type (E_Formal)
1747 or else Nkind (Actual) = N_Slice
1749 Check_Fortran_Logical;
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))
1763 if Scope (Subp) /= Entity (Actual) then
1765 ("operation outside protected type may not "
1766 & "call back its protected operations??", Actual);
1770 Expand_Protected_Object_Reference (N, Entity (Actual)));
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);
1785 Apply_Constraint_Check (Actual, E_Formal);
1787 -- Out parameter case. No constraint checks on access type
1790 elsif Is_Access_Type (E_Formal) then
1795 elsif Has_Discriminants (Base_Type (E_Formal))
1796 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1798 Apply_Constraint_Check (Actual, E_Formal);
1803 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
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
1815 if Is_Constrained (E_Formal) then
1816 Apply_Length_Check (Expression (Actual), E_Formal);
1818 Apply_Range_Check (Expression (Actual), E_Formal);
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
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
1837 Etype (Expression (Actual))))
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
1860 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1861 and then not Represented_As_Scalar (Etype (Formal))
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))
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)
1902 Add_Call_By_Copy_Code;
1904 -- The actual denotes a variable which captures the value of an
1905 -- object for validation purposes. Add a copy-back to reflect any
1906 -- potential changes in value back into the original object.
1908 -- Temp : ... := Object;
1909 -- if not Temp'Valid then ...
1913 elsif Is_Validation_Variable_Reference (Actual) then
1914 Append_To (Post_Call,
1915 Make_Assignment_Statement (Loc,
1916 Name => Validated_Object (Entity (Actual)),
1917 Expression => New_Occurrence_Of (Entity (Actual), Loc)));
1919 elsif Nkind (Actual) = N_Indexed_Component
1920 and then Is_Entity_Name (Prefix (Actual))
1921 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1923 Add_Call_By_Copy_Code;
1925 -- Add call-by-copy code for the case of scalar out parameters
1926 -- when it is not known at compile time that the subtype of the
1927 -- formal is a subrange of the subtype of the actual (or vice
1928 -- versa for in out parameters), in order to get range checks
1929 -- on such actuals. (Maybe this case should be handled earlier
1930 -- in the if statement???)
1932 elsif Is_Scalar_Type (E_Formal)
1934 (not In_Subrange_Of (E_Formal, E_Actual)
1936 (Ekind (Formal) = E_In_Out_Parameter
1937 and then not In_Subrange_Of (E_Actual, E_Formal)))
1939 -- Perhaps the setting back to False should be done within
1940 -- Add_Call_By_Copy_Code, since it could get set on other
1941 -- cases occurring above???
1943 if Do_Range_Check (Actual) then
1944 Set_Do_Range_Check (Actual, False);
1947 Add_Call_By_Copy_Code;
1950 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
1951 -- by-reference parameters on exit from the call. If the actual
1952 -- is a derived type and the operation is inherited, the body
1953 -- of the operation will not contain a call to the predicate
1954 -- function, so it must be done explicitly after the call. Ditto
1955 -- if the actual is an entity of a predicated subtype.
1957 -- The rule refers to by-reference types, but a check is needed
1958 -- for by-copy types as well. That check is subsumed by the rule
1959 -- for subtype conversion on assignment, but we can generate the
1960 -- required check now.
1962 -- Note also that Subp may be either a subprogram entity for
1963 -- direct calls, or a type entity for indirect calls, which must
1964 -- be handled separately because the name does not denote an
1965 -- overloadable entity.
1967 By_Ref_Predicate_Check : declare
1968 Aund : constant Entity_Id := Underlying_Type (E_Actual);
1971 function Is_Public_Subp return Boolean;
1972 -- Check whether the subprogram being called is a visible
1973 -- operation of the type of the actual. Used to determine
1974 -- whether an invariant check must be generated on the
1977 ---------------------
1978 -- Is_Public_Subp --
1979 ---------------------
1981 function Is_Public_Subp return Boolean is
1982 Pack : constant Entity_Id := Scope (Subp);
1983 Subp_Decl : Node_Id;
1986 if not Is_Subprogram (Subp) then
1989 -- The operation may be inherited, or a primitive of the
1993 Nkind_In (Parent (Subp), N_Private_Extension_Declaration,
1994 N_Full_Type_Declaration)
1996 Subp_Decl := Parent (Subp);
1999 Subp_Decl := Unit_Declaration_Node (Subp);
2002 return Ekind (Pack) = E_Package
2004 List_Containing (Subp_Decl) =
2005 Visible_Declarations
2006 (Specification (Unit_Declaration_Node (Pack)));
2009 -- Start of processing for By_Ref_Predicate_Check
2018 if Has_Predicates (Atyp)
2019 and then Present (Predicate_Function (Atyp))
2021 -- Skip predicate checks for special cases
2023 and then Predicate_Tests_On_Arguments (Subp)
2025 Append_To (Post_Call,
2026 Make_Predicate_Check (Atyp, Actual));
2029 -- We generated caller-side invariant checks in two cases:
2031 -- a) when calling an inherited operation, where there is an
2032 -- implicit view conversion of the actual to the parent type.
2034 -- b) When the conversion is explicit
2036 -- We treat these cases separately because the required
2037 -- conversion for a) is added later when expanding the call.
2039 if Has_Invariants (Etype (Actual))
2041 Nkind (Parent (Subp)) = N_Private_Extension_Declaration
2043 if Comes_From_Source (N) and then Is_Public_Subp then
2044 Append_To (Post_Call, Make_Invariant_Call (Actual));
2047 elsif Nkind (Actual) = N_Type_Conversion
2048 and then Has_Invariants (Etype (Expression (Actual)))
2050 if Comes_From_Source (N) and then Is_Public_Subp then
2051 Append_To (Post_Call,
2052 Make_Invariant_Call (Expression (Actual)));
2055 end By_Ref_Predicate_Check;
2057 -- Processing for IN parameters
2060 -- For IN parameters in the bit-packed array case, we expand an
2061 -- indexed component (the circuit in Exp_Ch4 deliberately left
2062 -- indexed components appearing as actuals untouched, so that
2063 -- the special processing above for the OUT and IN OUT cases
2064 -- could be performed. We could make the test in Exp_Ch4 more
2065 -- complex and have it detect the parameter mode, but it is
2066 -- easier simply to handle all cases here.)
2068 if Nkind (Actual) = N_Indexed_Component
2069 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2071 Reset_Packed_Prefix;
2072 Expand_Packed_Element_Reference (Actual);
2074 -- If we have a reference to a bit-packed array, we copy it, since
2075 -- the actual must be byte aligned.
2077 -- Is this really necessary in all cases???
2079 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2080 Add_Simple_Call_By_Copy_Code;
2082 -- If a non-scalar actual is possibly unaligned, we need a copy
2084 elsif Is_Possibly_Unaligned_Object (Actual)
2085 and then not Represented_As_Scalar (Etype (Formal))
2087 Add_Simple_Call_By_Copy_Code;
2089 -- Similarly, we have to expand slices of packed arrays here
2090 -- because the result must be byte aligned.
2092 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2093 Add_Call_By_Copy_Code;
2095 -- Only processing remaining is to pass by copy if this is a
2096 -- reference to a possibly unaligned slice, since the caller
2097 -- expects an appropriately aligned argument.
2099 elsif Is_Possibly_Unaligned_Slice (Actual) then
2100 Add_Call_By_Copy_Code;
2102 -- An unusual case: a current instance of an enclosing task can be
2103 -- an actual, and must be replaced by a reference to self.
2105 elsif Is_Entity_Name (Actual)
2106 and then Is_Task_Type (Entity (Actual))
2108 if In_Open_Scopes (Entity (Actual)) then
2110 (Make_Function_Call (Loc,
2111 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2114 -- A task type cannot otherwise appear as an actual
2117 raise Program_Error;
2122 Next_Formal (Formal);
2123 Next_Actual (Actual);
2126 -- Find right place to put post call stuff if it is present
2128 if not Is_Empty_List (Post_Call) then
2130 -- Cases where the call is not a member of a statement list.
2131 -- This includes the case where the call is an actual in another
2132 -- function call or indexing, i.e. an expression context as well.
2134 if not Is_List_Member (N)
2135 or else Nkind_In (Parent (N), N_Function_Call, N_Indexed_Component)
2137 -- In Ada 2012 the call may be a function call in an expression
2138 -- (since OUT and IN OUT parameters are now allowed for such
2139 -- calls). The write-back of (in)-out parameters is handled
2140 -- by the back-end, but the constraint checks generated when
2141 -- subtypes of formal and actual don't match must be inserted
2142 -- in the form of assignments.
2144 if Ada_Version >= Ada_2012
2145 and then Nkind (N) = N_Function_Call
2147 -- We used to just do handle this by climbing up parents to
2148 -- a non-statement/declaration and then simply making a call
2149 -- to Insert_Actions_After (P, Post_Call), but that doesn't
2150 -- work. If we are in the middle of an expression, e.g. the
2151 -- condition of an IF, this call would insert after the IF
2152 -- statement, which is much too late to be doing the write
2153 -- back. For example:
2155 -- if Clobber (X) then
2156 -- Put_Line (X'Img);
2161 -- Now assume Clobber changes X, if we put the write back
2162 -- after the IF, the Put_Line gets the wrong value and the
2163 -- goto causes the write back to be skipped completely.
2165 -- To deal with this, we replace the call by
2168 -- Tnnn : constant function-result-type := function-call;
2169 -- Post_Call actions
2175 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
2176 FRTyp : constant Entity_Id := Etype (N);
2177 Name : constant Node_Id := Relocate_Node (N);
2180 Prepend_To (Post_Call,
2181 Make_Object_Declaration (Loc,
2182 Defining_Identifier => Tnnn,
2183 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
2184 Constant_Present => True,
2185 Expression => Name));
2188 Make_Expression_With_Actions (Loc,
2189 Actions => Post_Call,
2190 Expression => New_Occurrence_Of (Tnnn, Loc)));
2192 -- We don't want to just blindly call Analyze_And_Resolve
2193 -- because that would cause unwanted recursion on the call.
2194 -- So for a moment set the call as analyzed to prevent that
2195 -- recursion, and get the rest analyzed properly, then reset
2196 -- the analyzed flag, so our caller can continue.
2198 Set_Analyzed (Name, True);
2199 Analyze_And_Resolve (N, FRTyp);
2200 Set_Analyzed (Name, False);
2202 -- Reset calling argument to point to function call inside
2203 -- the expression with actions so the caller can continue
2204 -- to process the call. In spite of the fact that it is
2205 -- marked Analyzed above, it may be rewritten by Remove_
2206 -- Side_Effects if validity checks are present, so go back
2207 -- to original call.
2209 N := Original_Node (Name);
2212 -- If not the special Ada 2012 case of a function call, then
2213 -- we must have the triggering statement of a triggering
2214 -- alternative or an entry call alternative, and we can add
2215 -- the post call stuff to the corresponding statement list.
2223 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
2224 N_Entry_Call_Alternative));
2226 if Is_Non_Empty_List (Statements (P)) then
2227 Insert_List_Before_And_Analyze
2228 (First (Statements (P)), Post_Call);
2230 Set_Statements (P, Post_Call);
2237 -- Otherwise, normal case where N is in a statement sequence,
2238 -- just put the post-call stuff after the call statement.
2241 Insert_Actions_After (N, Post_Call);
2246 -- The call node itself is re-analyzed in Expand_Call
2254 -- This procedure handles expansion of function calls and procedure call
2255 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2256 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2258 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2259 -- Provide values of actuals for all formals in Extra_Formals list
2260 -- Replace "call" to enumeration literal function by literal itself
2261 -- Rewrite call to predefined operator as operator
2262 -- Replace actuals to in-out parameters that are numeric conversions,
2263 -- with explicit assignment to temporaries before and after the call.
2265 -- Note that the list of actuals has been filled with default expressions
2266 -- during semantic analysis of the call. Only the extra actuals required
2267 -- for the 'Constrained attribute and for accessibility checks are added
2270 procedure Expand_Call (N : Node_Id) is
2271 Loc : constant Source_Ptr := Sloc (N);
2272 Call_Node : Node_Id := N;
2273 Extra_Actuals : List_Id := No_List;
2274 Prev : Node_Id := Empty;
2276 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2277 -- Adds one entry to the end of the actual parameter list. Used for
2278 -- default parameters and for extra actuals (for Extra_Formals). The
2279 -- argument is an N_Parameter_Association node.
2281 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2282 -- Adds an extra actual to the list of extra actuals. Expr is the
2283 -- expression for the value of the actual, EF is the entity for the
2286 procedure Add_View_Conversion_Invariants
2287 (Formal : Entity_Id;
2289 -- Adds invariant checks for every intermediate type between the range
2290 -- of a view converted argument to its ancestor (from parent to child).
2292 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2293 -- Within an instance, a type derived from an untagged formal derived
2294 -- type inherits from the original parent, not from the actual. The
2295 -- current derivation mechanism has the derived type inherit from the
2296 -- actual, which is only correct outside of the instance. If the
2297 -- subprogram is inherited, we test for this particular case through a
2298 -- convoluted tree traversal before setting the proper subprogram to be
2301 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2302 -- Return true if E comes from an instance that is not yet frozen
2304 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2305 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2307 function New_Value (From : Node_Id) return Node_Id;
2308 -- From is the original Expression. New_Value is equivalent to a call
2309 -- to Duplicate_Subexpr with an explicit dereference when From is an
2310 -- access parameter.
2312 --------------------------
2313 -- Add_Actual_Parameter --
2314 --------------------------
2316 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2317 Actual_Expr : constant Node_Id :=
2318 Explicit_Actual_Parameter (Insert_Param);
2321 -- Case of insertion is first named actual
2323 if No (Prev) or else
2324 Nkind (Parent (Prev)) /= N_Parameter_Association
2326 Set_Next_Named_Actual
2327 (Insert_Param, First_Named_Actual (Call_Node));
2328 Set_First_Named_Actual (Call_Node, Actual_Expr);
2331 if No (Parameter_Associations (Call_Node)) then
2332 Set_Parameter_Associations (Call_Node, New_List);
2335 Append (Insert_Param, Parameter_Associations (Call_Node));
2338 Insert_After (Prev, Insert_Param);
2341 -- Case of insertion is not first named actual
2344 Set_Next_Named_Actual
2345 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2346 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2347 Append (Insert_Param, Parameter_Associations (Call_Node));
2350 Prev := Actual_Expr;
2351 end Add_Actual_Parameter;
2353 ----------------------
2354 -- Add_Extra_Actual --
2355 ----------------------
2357 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2358 Loc : constant Source_Ptr := Sloc (Expr);
2361 if Extra_Actuals = No_List then
2362 Extra_Actuals := New_List;
2363 Set_Parent (Extra_Actuals, Call_Node);
2366 Append_To (Extra_Actuals,
2367 Make_Parameter_Association (Loc,
2368 Selector_Name => New_Occurrence_Of (EF, Loc),
2369 Explicit_Actual_Parameter => Expr));
2371 Analyze_And_Resolve (Expr, Etype (EF));
2373 if Nkind (Call_Node) = N_Function_Call then
2374 Set_Is_Accessibility_Actual (Parent (Expr));
2376 end Add_Extra_Actual;
2378 ------------------------------------
2379 -- Add_View_Conversion_Invariants --
2380 ------------------------------------
2382 procedure Add_View_Conversion_Invariants
2383 (Formal : Entity_Id;
2387 Curr_Typ : Entity_Id;
2388 Inv_Checks : List_Id;
2389 Par_Typ : Entity_Id;
2392 Inv_Checks := No_List;
2394 -- Extract the argument from a potentially nested set of view
2398 while Nkind (Arg) = N_Type_Conversion loop
2399 Arg := Expression (Arg);
2402 -- Move up the derivation chain starting with the type of the formal
2403 -- parameter down to the type of the actual object.
2406 Par_Typ := Etype (Arg);
2407 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
2408 Curr_Typ := Par_Typ;
2410 if Has_Invariants (Curr_Typ)
2411 and then Present (Invariant_Procedure (Curr_Typ))
2413 -- Verify the invariate of the current type. Generate:
2415 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
2417 Prepend_New_To (Inv_Checks,
2418 Make_Procedure_Call_Statement (Loc,
2421 (Invariant_Procedure (Curr_Typ), Loc),
2422 Parameter_Associations => New_List (
2423 Make_Type_Conversion (Loc,
2424 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
2425 Expression => New_Copy_Tree (Arg)))));
2428 Par_Typ := Base_Type (Etype (Curr_Typ));
2431 if not Is_Empty_List (Inv_Checks) then
2432 Insert_Actions_After (N, Inv_Checks);
2434 end Add_View_Conversion_Invariants;
2436 ---------------------------
2437 -- Inherited_From_Formal --
2438 ---------------------------
2440 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2442 Gen_Par : Entity_Id;
2443 Gen_Prim : Elist_Id;
2448 -- If the operation is inherited, it is attached to the corresponding
2449 -- type derivation. If the parent in the derivation is a generic
2450 -- actual, it is a subtype of the actual, and we have to recover the
2451 -- original derived type declaration to find the proper parent.
2453 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2454 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2455 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2456 N_Derived_Type_Definition
2457 or else not In_Instance
2464 (Type_Definition (Original_Node (Parent (S))));
2466 if Nkind (Indic) = N_Subtype_Indication then
2467 Par := Entity (Subtype_Mark (Indic));
2469 Par := Entity (Indic);
2473 if not Is_Generic_Actual_Type (Par)
2474 or else Is_Tagged_Type (Par)
2475 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2476 or else not In_Open_Scopes (Scope (Par))
2480 Gen_Par := Generic_Parent_Type (Parent (Par));
2483 -- If the actual has no generic parent type, the formal is not
2484 -- a formal derived type, so nothing to inherit.
2486 if No (Gen_Par) then
2490 -- If the generic parent type is still the generic type, this is a
2491 -- private formal, not a derived formal, and there are no operations
2492 -- inherited from the formal.
2494 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2498 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2500 Elmt := First_Elmt (Gen_Prim);
2501 while Present (Elmt) loop
2502 if Chars (Node (Elmt)) = Chars (S) then
2508 F1 := First_Formal (S);
2509 F2 := First_Formal (Node (Elmt));
2511 and then Present (F2)
2513 if Etype (F1) = Etype (F2)
2514 or else Etype (F2) = Gen_Par
2520 exit; -- not the right subprogram
2532 raise Program_Error;
2533 end Inherited_From_Formal;
2535 --------------------------
2536 -- In_Unfrozen_Instance --
2537 --------------------------
2539 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2544 while Present (S) and then S /= Standard_Standard loop
2545 if Is_Generic_Instance (S)
2546 and then Present (Freeze_Node (S))
2547 and then not Analyzed (Freeze_Node (S))
2556 end In_Unfrozen_Instance;
2558 -------------------------
2559 -- Is_Direct_Deep_Call --
2560 -------------------------
2562 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2564 if Is_TSS (Subp, TSS_Deep_Adjust)
2565 or else Is_TSS (Subp, TSS_Deep_Finalize)
2566 or else Is_TSS (Subp, TSS_Deep_Initialize)
2573 Actual := First (Parameter_Associations (N));
2574 Formal := First_Formal (Subp);
2575 while Present (Actual)
2576 and then Present (Formal)
2578 if Nkind (Actual) = N_Identifier
2579 and then Is_Controlling_Actual (Actual)
2580 and then Etype (Actual) = Etype (Formal)
2586 Next_Formal (Formal);
2592 end Is_Direct_Deep_Call;
2598 function New_Value (From : Node_Id) return Node_Id is
2599 Res : constant Node_Id := Duplicate_Subexpr (From);
2601 if Is_Access_Type (Etype (From)) then
2602 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2610 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2613 Orig_Subp : Entity_Id := Empty;
2614 Param_Count : Natural := 0;
2615 Parent_Formal : Entity_Id;
2616 Parent_Subp : Entity_Id;
2620 Prev_Orig : Node_Id;
2621 -- Original node for an actual, which may have been rewritten. If the
2622 -- actual is a function call that has been transformed from a selected
2623 -- component, the original node is unanalyzed. Otherwise, it carries
2624 -- semantic information used to generate additional actuals.
2626 CW_Interface_Formals_Present : Boolean := False;
2628 -- Start of processing for Expand_Call
2631 -- Expand the function or procedure call if the first actual has a
2632 -- declared dimension aspect, and the subprogram is declared in one
2633 -- of the dimension I/O packages.
2635 if Ada_Version >= Ada_2012
2637 Nkind_In (Call_Node, N_Procedure_Call_Statement, N_Function_Call)
2638 and then Present (Parameter_Associations (Call_Node))
2640 Expand_Put_Call_With_Symbol (Call_Node);
2643 -- Ignore if previous error
2645 if Nkind (Call_Node) in N_Has_Etype
2646 and then Etype (Call_Node) = Any_Type
2651 -- Call using access to subprogram with explicit dereference
2653 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2654 Subp := Etype (Name (Call_Node));
2655 Parent_Subp := Empty;
2657 -- Case of call to simple entry, where the Name is a selected component
2658 -- whose prefix is the task, and whose selector name is the entry name
2660 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2661 Subp := Entity (Selector_Name (Name (Call_Node)));
2662 Parent_Subp := Empty;
2664 -- Case of call to member of entry family, where Name is an indexed
2665 -- component, with the prefix being a selected component giving the
2666 -- task and entry family name, and the index being the entry index.
2668 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2669 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2670 Parent_Subp := Empty;
2675 Subp := Entity (Name (Call_Node));
2676 Parent_Subp := Alias (Subp);
2678 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2679 -- if we can tell that the first parameter cannot possibly be null.
2680 -- This improves efficiency by avoiding a run-time test.
2682 -- We do not do this if Raise_Exception_Always does not exist, which
2683 -- can happen in configurable run time profiles which provide only a
2686 if Is_RTE (Subp, RE_Raise_Exception)
2687 and then RTE_Available (RE_Raise_Exception_Always)
2690 FA : constant Node_Id :=
2691 Original_Node (First_Actual (Call_Node));
2694 -- The case we catch is where the first argument is obtained
2695 -- using the Identity attribute (which must always be
2698 if Nkind (FA) = N_Attribute_Reference
2699 and then Attribute_Name (FA) = Name_Identity
2701 Subp := RTE (RE_Raise_Exception_Always);
2702 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2707 if Ekind (Subp) = E_Entry then
2708 Parent_Subp := Empty;
2712 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2713 -- alternative in an asynchronous select or as an entry call in
2714 -- a conditional or timed select. Check whether the procedure call
2715 -- is a renaming of an entry and rewrite it as an entry call.
2717 if Ada_Version >= Ada_2005
2718 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2720 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2721 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2723 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2724 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2728 Ren_Root : Entity_Id := Subp;
2731 -- This may be a chain of renamings, find the root
2733 if Present (Alias (Ren_Root)) then
2734 Ren_Root := Alias (Ren_Root);
2737 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2738 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2740 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2742 Make_Entry_Call_Statement (Loc,
2744 New_Copy_Tree (Name (Ren_Decl)),
2745 Parameter_Associations =>
2747 (Parameter_Associations (Call_Node))));
2755 -- When generating C code, transform a function call that returns a
2756 -- constrained array type into procedure form.
2758 if Modify_Tree_For_C
2759 and then Nkind (Call_Node) = N_Function_Call
2760 and then Is_Entity_Name (Name (Call_Node))
2761 and then Rewritten_For_C (Ultimate_Alias (Entity (Name (Call_Node))))
2763 -- For internally generated calls ensure that they reference the
2764 -- entity of the spec of the called function (needed since the
2765 -- expander may generate calls using the entity of their body).
2766 -- See for example Expand_Boolean_Operator().
2768 if not (Comes_From_Source (Call_Node))
2769 and then Nkind (Unit_Declaration_Node
2770 (Ultimate_Alias (Entity (Name (Call_Node))))) =
2773 Set_Entity (Name (Call_Node),
2774 Corresponding_Function
2775 (Corresponding_Procedure
2776 (Ultimate_Alias (Entity (Name (Call_Node))))));
2779 Rewrite_Function_Call_For_C (Call_Node);
2783 -- First step, compute extra actuals, corresponding to any Extra_Formals
2784 -- present. Note that we do not access Extra_Formals directly, instead
2785 -- we simply note the presence of the extra formals as we process the
2786 -- regular formals collecting corresponding actuals in Extra_Actuals.
2788 -- We also generate any required range checks for actuals for in formals
2789 -- as we go through the loop, since this is a convenient place to do it.
2790 -- (Though it seems that this would be better done in Expand_Actuals???)
2792 -- Special case: Thunks must not compute the extra actuals; they must
2793 -- just propagate to the target primitive their extra actuals.
2795 if Is_Thunk (Current_Scope)
2796 and then Thunk_Entity (Current_Scope) = Subp
2797 and then Present (Extra_Formals (Subp))
2799 pragma Assert (Present (Extra_Formals (Current_Scope)));
2802 Target_Formal : Entity_Id;
2803 Thunk_Formal : Entity_Id;
2806 Target_Formal := Extra_Formals (Subp);
2807 Thunk_Formal := Extra_Formals (Current_Scope);
2808 while Present (Target_Formal) loop
2810 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2812 Target_Formal := Extra_Formal (Target_Formal);
2813 Thunk_Formal := Extra_Formal (Thunk_Formal);
2816 while Is_Non_Empty_List (Extra_Actuals) loop
2817 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2820 Expand_Actuals (Call_Node, Subp);
2825 Formal := First_Formal (Subp);
2826 Actual := First_Actual (Call_Node);
2828 while Present (Formal) loop
2830 -- Generate range check if required
2832 if Do_Range_Check (Actual)
2833 and then Ekind (Formal) = E_In_Parameter
2835 Generate_Range_Check
2836 (Actual, Etype (Formal), CE_Range_Check_Failed);
2839 -- Prepare to examine current entry
2842 Prev_Orig := Original_Node (Prev);
2844 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2845 -- to expand it in a further round.
2847 CW_Interface_Formals_Present :=
2848 CW_Interface_Formals_Present
2850 (Is_Class_Wide_Type (Etype (Formal))
2851 and then Is_Interface (Etype (Etype (Formal))))
2853 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2854 and then Is_Class_Wide_Type (Directly_Designated_Type
2855 (Etype (Etype (Formal))))
2856 and then Is_Interface (Directly_Designated_Type
2857 (Etype (Etype (Formal)))));
2859 -- Create possible extra actual for constrained case. Usually, the
2860 -- extra actual is of the form actual'constrained, but since this
2861 -- attribute is only available for unconstrained records, TRUE is
2862 -- expanded if the type of the formal happens to be constrained (for
2863 -- instance when this procedure is inherited from an unconstrained
2864 -- record to a constrained one) or if the actual has no discriminant
2865 -- (its type is constrained). An exception to this is the case of a
2866 -- private type without discriminants. In this case we pass FALSE
2867 -- because the object has underlying discriminants with defaults.
2869 if Present (Extra_Constrained (Formal)) then
2870 if Ekind (Etype (Prev)) in Private_Kind
2871 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2874 (New_Occurrence_Of (Standard_False, Loc),
2875 Extra_Constrained (Formal));
2877 elsif Is_Constrained (Etype (Formal))
2878 or else not Has_Discriminants (Etype (Prev))
2881 (New_Occurrence_Of (Standard_True, Loc),
2882 Extra_Constrained (Formal));
2884 -- Do not produce extra actuals for Unchecked_Union parameters.
2885 -- Jump directly to the end of the loop.
2887 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2888 goto Skip_Extra_Actual_Generation;
2891 -- If the actual is a type conversion, then the constrained
2892 -- test applies to the actual, not the target type.
2898 -- Test for unchecked conversions as well, which can occur
2899 -- as out parameter actuals on calls to stream procedures.
2902 while Nkind_In (Act_Prev, N_Type_Conversion,
2903 N_Unchecked_Type_Conversion)
2905 Act_Prev := Expression (Act_Prev);
2908 -- If the expression is a conversion of a dereference, this
2909 -- is internally generated code that manipulates addresses,
2910 -- e.g. when building interface tables. No check should
2911 -- occur in this case, and the discriminated object is not
2914 if not Comes_From_Source (Actual)
2915 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2916 and then Nkind (Act_Prev) = N_Explicit_Dereference
2919 (New_Occurrence_Of (Standard_False, Loc),
2920 Extra_Constrained (Formal));
2924 (Make_Attribute_Reference (Sloc (Prev),
2926 Duplicate_Subexpr_No_Checks
2927 (Act_Prev, Name_Req => True),
2928 Attribute_Name => Name_Constrained),
2929 Extra_Constrained (Formal));
2935 -- Create possible extra actual for accessibility level
2937 if Present (Extra_Accessibility (Formal)) then
2939 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2940 -- attribute, then the original actual may be an aliased object
2941 -- occurring as the prefix in a call using "Object.Operation"
2942 -- notation. In that case we must pass the level of the object,
2943 -- so Prev_Orig is reset to Prev and the attribute will be
2944 -- processed by the code for Access attributes further below.
2946 if Prev_Orig /= Prev
2947 and then Nkind (Prev) = N_Attribute_Reference
2949 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2950 and then Is_Aliased_View (Prev_Orig)
2955 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2956 -- accessibility levels.
2958 if Is_Thunk (Current_Scope) then
2960 Parm_Ent : Entity_Id;
2963 if Is_Controlling_Actual (Actual) then
2965 -- Find the corresponding actual of the thunk
2967 Parm_Ent := First_Entity (Current_Scope);
2968 for J in 2 .. Param_Count loop
2969 Next_Entity (Parm_Ent);
2972 -- Handle unchecked conversion of access types generated
2973 -- in thunks (cf. Expand_Interface_Thunk).
2975 elsif Is_Access_Type (Etype (Actual))
2976 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2978 Parm_Ent := Entity (Expression (Actual));
2980 else pragma Assert (Is_Entity_Name (Actual));
2981 Parm_Ent := Entity (Actual);
2985 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2986 Extra_Accessibility (Formal));
2989 elsif Is_Entity_Name (Prev_Orig) then
2991 -- When passing an access parameter, or a renaming of an access
2992 -- parameter, as the actual to another access parameter we need
2993 -- to pass along the actual's own access level parameter. This
2994 -- is done if we are within the scope of the formal access
2995 -- parameter (if this is an inlined body the extra formal is
2998 if (Is_Formal (Entity (Prev_Orig))
3000 (Present (Renamed_Object (Entity (Prev_Orig)))
3002 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
3005 (Entity (Renamed_Object (Entity (Prev_Orig))))))
3006 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
3007 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
3010 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
3013 pragma Assert (Present (Parm_Ent));
3015 if Present (Extra_Accessibility (Parm_Ent)) then
3018 (Extra_Accessibility (Parm_Ent), Loc),
3019 Extra_Accessibility (Formal));
3021 -- If the actual access parameter does not have an
3022 -- associated extra formal providing its scope level,
3023 -- then treat the actual as having library-level
3028 (Make_Integer_Literal (Loc,
3029 Intval => Scope_Depth (Standard_Standard)),
3030 Extra_Accessibility (Formal));
3034 -- The actual is a normal access value, so just pass the level
3035 -- of the actual's access type.
3039 (Dynamic_Accessibility_Level (Prev_Orig),
3040 Extra_Accessibility (Formal));
3043 -- If the actual is an access discriminant, then pass the level
3044 -- of the enclosing object (RM05-3.10.2(12.4/2)).
3046 elsif Nkind (Prev_Orig) = N_Selected_Component
3047 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
3049 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
3050 E_Anonymous_Access_Type
3053 (Make_Integer_Literal (Loc,
3054 Intval => Object_Access_Level (Prefix (Prev_Orig))),
3055 Extra_Accessibility (Formal));
3060 case Nkind (Prev_Orig) is
3061 when N_Attribute_Reference =>
3062 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
3064 -- For X'Access, pass on the level of the prefix X
3066 when Attribute_Access =>
3068 -- If this is an Access attribute applied to the
3069 -- the current instance object passed to a type
3070 -- initialization procedure, then use the level
3071 -- of the type itself. This is not really correct,
3072 -- as there should be an extra level parameter
3073 -- passed in with _init formals (only in the case
3074 -- where the type is immutably limited), but we
3075 -- don't have an easy way currently to create such
3076 -- an extra formal (init procs aren't ever frozen).
3077 -- For now we just use the level of the type,
3078 -- which may be too shallow, but that works better
3079 -- than passing Object_Access_Level of the type,
3080 -- which can be one level too deep in some cases.
3083 if Is_Entity_Name (Prefix (Prev_Orig))
3084 and then Is_Type (Entity (Prefix (Prev_Orig)))
3087 (Make_Integer_Literal (Loc,
3090 (Entity (Prefix (Prev_Orig)))),
3091 Extra_Accessibility (Formal));
3095 (Make_Integer_Literal (Loc,
3098 (Prefix (Prev_Orig))),
3099 Extra_Accessibility (Formal));
3102 -- Treat the unchecked attributes as library-level
3104 when Attribute_Unchecked_Access
3105 | Attribute_Unrestricted_Access
3108 (Make_Integer_Literal (Loc,
3109 Intval => Scope_Depth (Standard_Standard)),
3110 Extra_Accessibility (Formal));
3112 -- No other cases of attributes returning access
3113 -- values that can be passed to access parameters.
3116 raise Program_Error;
3120 -- For allocators we pass the level of the execution of the
3121 -- called subprogram, which is one greater than the current
3126 (Make_Integer_Literal (Loc,
3127 Intval => Scope_Depth (Current_Scope) + 1),
3128 Extra_Accessibility (Formal));
3130 -- For most other cases we simply pass the level of the
3131 -- actual's access type. The type is retrieved from
3132 -- Prev rather than Prev_Orig, because in some cases
3133 -- Prev_Orig denotes an original expression that has
3134 -- not been analyzed.
3138 (Dynamic_Accessibility_Level (Prev),
3139 Extra_Accessibility (Formal));
3144 -- Perform the check of 4.6(49) that prevents a null value from being
3145 -- passed as an actual to an access parameter. Note that the check
3146 -- is elided in the common cases of passing an access attribute or
3147 -- access parameter as an actual. Also, we currently don't enforce
3148 -- this check for expander-generated actuals and when -gnatdj is set.
3150 if Ada_Version >= Ada_2005 then
3152 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3153 -- the intent of 6.4.1(13) is that null-exclusion checks should
3154 -- not be done for 'out' parameters, even though it refers only
3155 -- to constraint checks, and a null_exclusion is not a constraint.
3156 -- Note that AI05-0196-1 corrects this mistake in the RM.
3158 if Is_Access_Type (Etype (Formal))
3159 and then Can_Never_Be_Null (Etype (Formal))
3160 and then Ekind (Formal) /= E_Out_Parameter
3161 and then Nkind (Prev) /= N_Raise_Constraint_Error
3162 and then (Known_Null (Prev)
3163 or else not Can_Never_Be_Null (Etype (Prev)))
3165 Install_Null_Excluding_Check (Prev);
3168 -- Ada_Version < Ada_2005
3171 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3172 or else Access_Checks_Suppressed (Subp)
3176 elsif Debug_Flag_J then
3179 elsif not Comes_From_Source (Prev) then
3182 elsif Is_Entity_Name (Prev)
3183 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3187 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3191 Install_Null_Excluding_Check (Prev);
3195 -- Perform appropriate validity checks on parameters that
3198 if Validity_Checks_On then
3199 if (Ekind (Formal) = E_In_Parameter
3200 and then Validity_Check_In_Params)
3202 (Ekind (Formal) = E_In_Out_Parameter
3203 and then Validity_Check_In_Out_Params)
3205 -- If the actual is an indexed component of a packed type (or
3206 -- is an indexed or selected component whose prefix recursively
3207 -- meets this condition), it has not been expanded yet. It will
3208 -- be copied in the validity code that follows, and has to be
3209 -- expanded appropriately, so reanalyze it.
3211 -- What we do is just to unset analyzed bits on prefixes till
3212 -- we reach something that does not have a prefix.
3219 while Nkind_In (Nod, N_Indexed_Component,
3220 N_Selected_Component)
3222 Set_Analyzed (Nod, False);
3223 Nod := Prefix (Nod);
3227 Ensure_Valid (Actual);
3231 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3232 -- since this is a left side reference. We only do this for calls
3233 -- from the source program since we assume that compiler generated
3234 -- calls explicitly generate any required checks. We also need it
3235 -- only if we are doing standard validity checks, since clearly it is
3236 -- not needed if validity checks are off, and in subscript validity
3237 -- checking mode, all indexed components are checked with a call
3238 -- directly from Expand_N_Indexed_Component.
3240 if Comes_From_Source (Call_Node)
3241 and then Ekind (Formal) /= E_In_Parameter
3242 and then Validity_Checks_On
3243 and then Validity_Check_Default
3244 and then not Validity_Check_Subscripts
3246 Check_Valid_Lvalue_Subscripts (Actual);
3249 -- Mark any scalar OUT parameter that is a simple variable as no
3250 -- longer known to be valid (unless the type is always valid). This
3251 -- reflects the fact that if an OUT parameter is never set in a
3252 -- procedure, then it can become invalid on the procedure return.
3254 if Ekind (Formal) = E_Out_Parameter
3255 and then Is_Entity_Name (Actual)
3256 and then Ekind (Entity (Actual)) = E_Variable
3257 and then not Is_Known_Valid (Etype (Actual))
3259 Set_Is_Known_Valid (Entity (Actual), False);
3262 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3263 -- clear current values, since they can be clobbered. We are probably
3264 -- doing this in more places than we need to, but better safe than
3265 -- sorry when it comes to retaining bad current values.
3267 if Ekind (Formal) /= E_In_Parameter
3268 and then Is_Entity_Name (Actual)
3269 and then Present (Entity (Actual))
3272 Ent : constant Entity_Id := Entity (Actual);
3276 -- For an OUT or IN OUT parameter that is an assignable entity,
3277 -- we do not want to clobber the Last_Assignment field, since
3278 -- if it is set, it was precisely because it is indeed an OUT
3279 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3280 -- since the subprogram could have returned in invalid value.
3282 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3283 and then Is_Assignable (Ent)
3285 Sav := Last_Assignment (Ent);
3286 Kill_Current_Values (Ent);
3287 Set_Last_Assignment (Ent, Sav);
3288 Set_Is_Known_Valid (Ent, False);
3290 -- For all other cases, just kill the current values
3293 Kill_Current_Values (Ent);
3298 -- If the formal is class wide and the actual is an aggregate, force
3299 -- evaluation so that the back end who does not know about class-wide
3300 -- type, does not generate a temporary of the wrong size.
3302 if not Is_Class_Wide_Type (Etype (Formal)) then
3305 elsif Nkind (Actual) = N_Aggregate
3306 or else (Nkind (Actual) = N_Qualified_Expression
3307 and then Nkind (Expression (Actual)) = N_Aggregate)
3309 Force_Evaluation (Actual);
3312 -- In a remote call, if the formal is of a class-wide type, check
3313 -- that the actual meets the requirements described in E.4(18).
3315 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3316 Insert_Action (Actual,
3317 Make_Transportable_Check (Loc,
3318 Duplicate_Subexpr_Move_Checks (Actual)));
3321 -- Perform invariant checks for all intermediate types in a view
3322 -- conversion after successful return from a call that passes the
3323 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
3324 -- 13/3, 14/3)). Consider only source conversion in order to avoid
3325 -- generating spurious checks on complex expansion such as object
3326 -- initialization through an extension aggregate.
3328 if Comes_From_Source (N)
3329 and then Ekind (Formal) /= E_In_Parameter
3330 and then Nkind (Actual) = N_Type_Conversion
3332 Add_View_Conversion_Invariants (Formal, Actual);
3335 -- Generating C the initialization of an allocator is performed by
3336 -- means of individual statements, and hence it must be done before
3339 if Modify_Tree_For_C
3340 and then Nkind (Actual) = N_Allocator
3341 and then Nkind (Expression (Actual)) = N_Qualified_Expression
3343 Remove_Side_Effects (Actual);
3346 -- This label is required when skipping extra actual generation for
3347 -- Unchecked_Union parameters.
3349 <<Skip_Extra_Actual_Generation>>
3351 Param_Count := Param_Count + 1;
3352 Next_Actual (Actual);
3353 Next_Formal (Formal);
3356 -- If we are calling an Ada 2012 function which needs to have the
3357 -- "accessibility level determined by the point of call" (AI05-0234)
3358 -- passed in to it, then pass it in.
3360 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3362 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3365 Ancestor : Node_Id := Parent (Call_Node);
3366 Level : Node_Id := Empty;
3367 Defer : Boolean := False;
3370 -- Unimplemented: if Subp returns an anonymous access type, then
3372 -- a) if the call is the operand of an explict conversion, then
3373 -- the target type of the conversion (a named access type)
3374 -- determines the accessibility level pass in;
3376 -- b) if the call defines an access discriminant of an object
3377 -- (e.g., the discriminant of an object being created by an
3378 -- allocator, or the discriminant of a function result),
3379 -- then the accessibility level to pass in is that of the
3380 -- discriminated object being initialized).
3384 while Nkind (Ancestor) = N_Qualified_Expression
3386 Ancestor := Parent (Ancestor);
3389 case Nkind (Ancestor) is
3392 -- At this point, we'd like to assign
3394 -- Level := Dynamic_Accessibility_Level (Ancestor);
3396 -- but Etype of Ancestor may not have been set yet,
3397 -- so that doesn't work.
3399 -- Handle this later in Expand_Allocator_Expression.
3403 when N_Object_Declaration
3404 | N_Object_Renaming_Declaration
3407 Def_Id : constant Entity_Id :=
3408 Defining_Identifier (Ancestor);
3411 if Is_Return_Object (Def_Id) then
3412 if Present (Extra_Accessibility_Of_Result
3413 (Return_Applies_To (Scope (Def_Id))))
3415 -- Pass along value that was passed in if the
3416 -- routine we are returning from also has an
3417 -- Accessibility_Of_Result formal.
3421 (Extra_Accessibility_Of_Result
3422 (Return_Applies_To (Scope (Def_Id))), Loc);
3426 Make_Integer_Literal (Loc,
3427 Intval => Object_Access_Level (Def_Id));
3431 when N_Simple_Return_Statement =>
3432 if Present (Extra_Accessibility_Of_Result
3434 (Return_Statement_Entity (Ancestor))))
3436 -- Pass along value that was passed in if the returned
3437 -- routine also has an Accessibility_Of_Result formal.
3441 (Extra_Accessibility_Of_Result
3443 (Return_Statement_Entity (Ancestor))), Loc);
3451 if not Present (Level) then
3453 -- The "innermost master that evaluates the function call".
3455 -- ??? - Should we use Integer'Last here instead in order
3456 -- to deal with (some of) the problems associated with
3457 -- calls to subps whose enclosing scope is unknown (e.g.,
3458 -- Anon_Access_To_Subp_Param.all)?
3461 Make_Integer_Literal (Loc,
3462 Intval => Scope_Depth (Current_Scope) + 1);
3467 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3472 -- If we are expanding the RHS of an assignment we need to check if tag
3473 -- propagation is needed. You might expect this processing to be in
3474 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3475 -- assignment might be transformed to a declaration for an unconstrained
3476 -- value if the expression is classwide.
3478 if Nkind (Call_Node) = N_Function_Call
3479 and then Is_Tag_Indeterminate (Call_Node)
3480 and then Is_Entity_Name (Name (Call_Node))
3483 Ass : Node_Id := Empty;
3486 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3487 Ass := Parent (Call_Node);
3489 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3490 and then Nkind (Parent (Parent (Call_Node))) =
3491 N_Assignment_Statement
3493 Ass := Parent (Parent (Call_Node));
3495 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3496 and then Nkind (Parent (Parent (Call_Node))) =
3497 N_Assignment_Statement
3499 Ass := Parent (Parent (Call_Node));
3503 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3505 if Is_Access_Type (Etype (Call_Node)) then
3506 if Designated_Type (Etype (Call_Node)) /=
3507 Root_Type (Etype (Name (Ass)))
3510 ("tag-indeterminate expression "
3511 & " must have designated type& (RM 5.2 (6))",
3512 Call_Node, Root_Type (Etype (Name (Ass))));
3514 Propagate_Tag (Name (Ass), Call_Node);
3517 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3519 ("tag-indeterminate expression must have type&"
3521 Call_Node, Root_Type (Etype (Name (Ass))));
3524 Propagate_Tag (Name (Ass), Call_Node);
3527 -- The call will be rewritten as a dispatching call, and
3528 -- expanded as such.
3535 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3536 -- it to point to the correct secondary virtual table
3538 if Nkind (Call_Node) in N_Subprogram_Call
3539 and then CW_Interface_Formals_Present
3541 Expand_Interface_Actuals (Call_Node);
3544 -- Deals with Dispatch_Call if we still have a call, before expanding
3545 -- extra actuals since this will be done on the re-analysis of the
3546 -- dispatching call. Note that we do not try to shorten the actual list
3547 -- for a dispatching call, it would not make sense to do so. Expansion
3548 -- of dispatching calls is suppressed for VM targets, because the VM
3549 -- back-ends directly handle the generation of dispatching calls and
3550 -- would have to undo any expansion to an indirect call.
3552 if Nkind (Call_Node) in N_Subprogram_Call
3553 and then Present (Controlling_Argument (Call_Node))
3556 Call_Typ : constant Entity_Id := Etype (Call_Node);
3557 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3558 Eq_Prim_Op : Entity_Id := Empty;
3561 Prev_Call : Node_Id;
3564 if not Is_Limited_Type (Typ) then
3565 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3568 if Tagged_Type_Expansion then
3569 Expand_Dispatching_Call (Call_Node);
3571 -- The following return is worrisome. Is it really OK to skip
3572 -- all remaining processing in this procedure ???
3579 Apply_Tag_Checks (Call_Node);
3581 -- If this is a dispatching "=", we must first compare the
3582 -- tags so we generate: x.tag = y.tag and then x = y
3584 if Subp = Eq_Prim_Op then
3586 -- Mark the node as analyzed to avoid reanalyzing this
3587 -- dispatching call (which would cause a never-ending loop)
3589 Prev_Call := Relocate_Node (Call_Node);
3590 Set_Analyzed (Prev_Call);
3592 Param := First_Actual (Call_Node);
3598 Make_Selected_Component (Loc,
3599 Prefix => New_Value (Param),
3602 (First_Tag_Component (Typ), Loc)),
3605 Make_Selected_Component (Loc,
3607 Unchecked_Convert_To (Typ,
3608 New_Value (Next_Actual (Param))),
3611 (First_Tag_Component (Typ), Loc))),
3612 Right_Opnd => Prev_Call);
3614 Rewrite (Call_Node, New_Call);
3617 (Call_Node, Call_Typ, Suppress => All_Checks);
3620 -- Expansion of a dispatching call results in an indirect call,
3621 -- which in turn causes current values to be killed (see
3622 -- Resolve_Call), so on VM targets we do the call here to
3623 -- ensure consistent warnings between VM and non-VM targets.
3625 Kill_Current_Values;
3628 -- If this is a dispatching "=" then we must update the reference
3629 -- to the call node because we generated:
3630 -- x.tag = y.tag and then x = y
3632 if Subp = Eq_Prim_Op then
3633 Call_Node := Right_Opnd (Call_Node);
3638 -- Similarly, expand calls to RCI subprograms on which pragma
3639 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3640 -- later. Do this only when the call comes from source since we
3641 -- do not want such a rewriting to occur in expanded code.
3643 if Is_All_Remote_Call (Call_Node) then
3644 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3646 -- Similarly, do not add extra actuals for an entry call whose entity
3647 -- is a protected procedure, or for an internal protected subprogram
3648 -- call, because it will be rewritten as a protected subprogram call
3649 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3651 elsif Is_Protected_Type (Scope (Subp))
3652 and then (Ekind (Subp) = E_Procedure
3653 or else Ekind (Subp) = E_Function)
3657 -- During that loop we gathered the extra actuals (the ones that
3658 -- correspond to Extra_Formals), so now they can be appended.
3661 while Is_Non_Empty_List (Extra_Actuals) loop
3662 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3666 -- At this point we have all the actuals, so this is the point at which
3667 -- the various expansion activities for actuals is carried out.
3669 Expand_Actuals (Call_Node, Subp);
3671 -- Verify that the actuals do not share storage. This check must be done
3672 -- on the caller side rather that inside the subprogram to avoid issues
3673 -- of parameter passing.
3675 if Check_Aliasing_Of_Parameters then
3676 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3679 -- If the subprogram is a renaming, or if it is inherited, replace it in
3680 -- the call with the name of the actual subprogram being called. If this
3681 -- is a dispatching call, the run-time decides what to call. The Alias
3682 -- attribute does not apply to entries.
3684 if Nkind (Call_Node) /= N_Entry_Call_Statement
3685 and then No (Controlling_Argument (Call_Node))
3686 and then Present (Parent_Subp)
3687 and then not Is_Direct_Deep_Call (Subp)
3689 if Present (Inherited_From_Formal (Subp)) then
3690 Parent_Subp := Inherited_From_Formal (Subp);
3692 Parent_Subp := Ultimate_Alias (Parent_Subp);
3695 -- The below setting of Entity is suspect, see F109-018 discussion???
3697 Set_Entity (Name (Call_Node), Parent_Subp);
3699 if Is_Abstract_Subprogram (Parent_Subp)
3700 and then not In_Instance
3703 ("cannot call abstract subprogram &!",
3704 Name (Call_Node), Parent_Subp);
3707 -- Inspect all formals of derived subprogram Subp. Compare parameter
3708 -- types with the parent subprogram and check whether an actual may
3709 -- need a type conversion to the corresponding formal of the parent
3712 -- Not clear whether intrinsic subprograms need such conversions. ???
3714 if not Is_Intrinsic_Subprogram (Parent_Subp)
3715 or else Is_Generic_Instance (Parent_Subp)
3718 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3719 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3720 -- and resolve the newly generated construct.
3726 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3728 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3735 Actual_Typ : Entity_Id;
3736 Formal_Typ : Entity_Id;
3737 Parent_Typ : Entity_Id;
3740 Actual := First_Actual (Call_Node);
3741 Formal := First_Formal (Subp);
3742 Parent_Formal := First_Formal (Parent_Subp);
3743 while Present (Formal) loop
3744 Actual_Typ := Etype (Actual);
3745 Formal_Typ := Etype (Formal);
3746 Parent_Typ := Etype (Parent_Formal);
3748 -- For an IN parameter of a scalar type, the parent formal
3749 -- type and derived formal type differ or the parent formal
3750 -- type and actual type do not match statically.
3752 if Is_Scalar_Type (Formal_Typ)
3753 and then Ekind (Formal) = E_In_Parameter
3754 and then Formal_Typ /= Parent_Typ
3756 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3757 and then not Raises_Constraint_Error (Actual)
3759 Convert (Actual, Parent_Typ);
3760 Enable_Range_Check (Actual);
3762 -- If the actual has been marked as requiring a range
3763 -- check, then generate it here.
3765 if Do_Range_Check (Actual) then
3766 Generate_Range_Check
3767 (Actual, Etype (Formal), CE_Range_Check_Failed);
3770 -- For access types, the parent formal type and actual type
3773 elsif Is_Access_Type (Formal_Typ)
3774 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3776 if Ekind (Formal) /= E_In_Parameter then
3777 Convert (Actual, Parent_Typ);
3779 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3780 and then Designated_Type (Parent_Typ) /=
3781 Designated_Type (Actual_Typ)
3782 and then not Is_Controlling_Formal (Formal)
3784 -- This unchecked conversion is not necessary unless
3785 -- inlining is enabled, because in that case the type
3786 -- mismatch may become visible in the body about to be
3790 Unchecked_Convert_To (Parent_Typ,
3791 Relocate_Node (Actual)));
3793 Resolve (Actual, Parent_Typ);
3796 -- If there is a change of representation, then generate a
3797 -- warning, and do the change of representation.
3799 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3801 ("??change of representation required", Actual);
3802 Convert (Actual, Parent_Typ);
3804 -- For array and record types, the parent formal type and
3805 -- derived formal type have different sizes or pragma Pack
3808 elsif ((Is_Array_Type (Formal_Typ)
3809 and then Is_Array_Type (Parent_Typ))
3811 (Is_Record_Type (Formal_Typ)
3812 and then Is_Record_Type (Parent_Typ)))
3814 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3815 or else Has_Pragma_Pack (Formal_Typ) /=
3816 Has_Pragma_Pack (Parent_Typ))
3818 Convert (Actual, Parent_Typ);
3821 Next_Actual (Actual);
3822 Next_Formal (Formal);
3823 Next_Formal (Parent_Formal);
3829 Subp := Parent_Subp;
3832 -- Deal with case where call is an explicit dereference
3834 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3836 -- Handle case of access to protected subprogram type
3838 if Is_Access_Protected_Subprogram_Type
3839 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3841 -- If this is a call through an access to protected operation, the
3842 -- prefix has the form (object'address, operation'access). Rewrite
3843 -- as a for other protected calls: the object is the 1st parameter
3844 -- of the list of actuals.
3851 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3853 T : constant Entity_Id :=
3854 Equivalent_Type (Base_Type (Etype (Ptr)));
3856 D_T : constant Entity_Id :=
3857 Designated_Type (Base_Type (Etype (Ptr)));
3861 Make_Selected_Component (Loc,
3862 Prefix => Unchecked_Convert_To (T, Ptr),
3864 New_Occurrence_Of (First_Entity (T), Loc));
3867 Make_Selected_Component (Loc,
3868 Prefix => Unchecked_Convert_To (T, Ptr),
3870 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3873 Make_Explicit_Dereference (Loc,
3876 if Present (Parameter_Associations (Call_Node)) then
3877 Parm := Parameter_Associations (Call_Node);
3882 Prepend (Obj, Parm);
3884 if Etype (D_T) = Standard_Void_Type then
3886 Make_Procedure_Call_Statement (Loc,
3888 Parameter_Associations => Parm);
3891 Make_Function_Call (Loc,
3893 Parameter_Associations => Parm);
3896 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3897 Set_Etype (Call, Etype (D_T));
3899 -- We do not re-analyze the call to avoid infinite recursion.
3900 -- We analyze separately the prefix and the object, and set
3901 -- the checks on the prefix that would otherwise be emitted
3902 -- when resolving a call.
3904 Rewrite (Call_Node, Call);
3906 Apply_Access_Check (Nam);
3913 -- If this is a call to an intrinsic subprogram, then perform the
3914 -- appropriate expansion to the corresponding tree node and we
3915 -- are all done (since after that the call is gone).
3917 -- In the case where the intrinsic is to be processed by the back end,
3918 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3919 -- since the idea in this case is to pass the call unchanged. If the
3920 -- intrinsic is an inherited unchecked conversion, and the derived type
3921 -- is the target type of the conversion, we must retain it as the return
3922 -- type of the expression. Otherwise the expansion below, which uses the
3923 -- parent operation, will yield the wrong type.
3925 if Is_Intrinsic_Subprogram (Subp) then
3926 Expand_Intrinsic_Call (Call_Node, Subp);
3928 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3929 and then Parent_Subp /= Orig_Subp
3930 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3932 Set_Etype (Call_Node, Etype (Orig_Subp));
3938 if Ekind_In (Subp, E_Function, E_Procedure) then
3940 -- We perform a simple optimization on calls for To_Address by
3941 -- replacing them with an unchecked conversion. Not only is this
3942 -- efficient, but it also avoids order of elaboration problems when
3943 -- address clauses are inlined (address expression elaborated at the
3944 -- at the wrong point).
3946 -- We perform this optimization regardless of whether we are in the
3947 -- main unit or in a unit in the context of the main unit, to ensure
3948 -- that tree generated is the same in both cases, for CodePeer use.
3950 if Is_RTE (Subp, RE_To_Address) then
3952 Unchecked_Convert_To
3953 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3957 -- Handle inlining. No action needed if the subprogram is not inlined
3959 if not Is_Inlined (Subp) then
3962 -- Frontend inlining of expression functions (performed also when
3963 -- backend inlining is enabled).
3965 elsif Is_Inlinable_Expression_Function (Subp) then
3966 Rewrite (N, New_Copy (Expression_Of_Expression_Function (Subp)));
3970 -- Handle frontend inlining
3972 elsif not Back_End_Inlining then
3973 Inlined_Subprogram : declare
3975 Must_Inline : Boolean := False;
3976 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3979 -- Verify that the body to inline has already been seen, and
3980 -- that if the body is in the current unit the inlining does
3981 -- not occur earlier. This avoids order-of-elaboration problems
3984 -- This should be documented in sinfo/einfo ???
3987 or else Nkind (Spec) /= N_Subprogram_Declaration
3988 or else No (Body_To_Inline (Spec))
3990 Must_Inline := False;
3992 -- If this an inherited function that returns a private type,
3993 -- do not inline if the full view is an unconstrained array,
3994 -- because such calls cannot be inlined.
3996 elsif Present (Orig_Subp)
3997 and then Is_Array_Type (Etype (Orig_Subp))
3998 and then not Is_Constrained (Etype (Orig_Subp))
4000 Must_Inline := False;
4002 elsif In_Unfrozen_Instance (Scope (Subp)) then
4003 Must_Inline := False;
4006 Bod := Body_To_Inline (Spec);
4008 if (In_Extended_Main_Code_Unit (Call_Node)
4009 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
4010 or else Has_Pragma_Inline_Always (Subp))
4011 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
4013 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
4015 Must_Inline := True;
4017 -- If we are compiling a package body that is not the main
4018 -- unit, it must be for inlining/instantiation purposes,
4019 -- in which case we inline the call to insure that the same
4020 -- temporaries are generated when compiling the body by
4021 -- itself. Otherwise link errors can occur.
4023 -- If the function being called is itself in the main unit,
4024 -- we cannot inline, because there is a risk of double
4025 -- elaboration and/or circularity: the inlining can make
4026 -- visible a private entity in the body of the main unit,
4027 -- that gigi will see before its sees its proper definition.
4029 elsif not (In_Extended_Main_Code_Unit (Call_Node))
4030 and then In_Package_Body
4032 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4034 -- Inline calls to _postconditions when generating C code
4036 elsif Modify_Tree_For_C
4037 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4038 and then Chars (Name (N)) = Name_uPostconditions
4040 Must_Inline := True;
4045 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4048 -- Let the back end handle it
4050 Add_Inlined_Body (Subp, Call_Node);
4052 if Front_End_Inlining
4053 and then Nkind (Spec) = N_Subprogram_Declaration
4054 and then (In_Extended_Main_Code_Unit (Call_Node))
4055 and then No (Body_To_Inline (Spec))
4056 and then not Has_Completion (Subp)
4057 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4060 ("cannot inline& (body not seen yet)?",
4064 end Inlined_Subprogram;
4066 -- Back end inlining: let the back end handle it
4068 elsif No (Unit_Declaration_Node (Subp))
4069 or else Nkind (Unit_Declaration_Node (Subp)) /=
4070 N_Subprogram_Declaration
4071 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
4072 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
4075 Add_Inlined_Body (Subp, Call_Node);
4077 -- If the inlined call appears within an instantiation and some
4078 -- level of optimization is required, ensure that the enclosing
4079 -- instance body is available so that the back-end can actually
4080 -- perform the inlining.
4083 and then Comes_From_Source (Subp)
4084 and then Optimization_Level > 0
4089 Inst_Node : Node_Id;
4092 Inst := Scope (Subp);
4094 -- Find enclosing instance
4096 while Present (Inst) and then Inst /= Standard_Standard loop
4097 exit when Is_Generic_Instance (Inst);
4098 Inst := Scope (Inst);
4102 and then Is_Generic_Instance (Inst)
4103 and then not Is_Inlined (Inst)
4105 Set_Is_Inlined (Inst);
4106 Decl := Unit_Declaration_Node (Inst);
4108 -- Do not add a pending instantiation if the body exits
4109 -- already, or if the instance is a compilation unit, or
4110 -- the instance node is missing.
4112 if Present (Corresponding_Body (Decl))
4113 or else Nkind (Parent (Decl)) = N_Compilation_Unit
4114 or else No (Next (Decl))
4119 -- The instantiation node usually follows the package
4120 -- declaration for the instance. If the generic unit
4121 -- has aspect specifications, they are transformed
4122 -- into pragmas in the instance, and the instance node
4123 -- appears after them.
4125 Inst_Node := Next (Decl);
4127 while Nkind (Inst_Node) /= N_Package_Instantiation loop
4128 Inst_Node := Next (Inst_Node);
4131 Add_Pending_Instantiation (Inst_Node, Decl);
4137 -- Front end expansion of simple functions returning unconstrained
4138 -- types (see Check_And_Split_Unconstrained_Function). Note that the
4139 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
4140 -- also Build_Renamed_Body) cannot be expanded here because this may
4141 -- give rise to order-of-elaboration issues for the types of the
4142 -- parameters of the subprogram, if any.
4145 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4149 -- Check for protected subprogram. This is either an intra-object call,
4150 -- or a protected function call. Protected procedure calls are rewritten
4151 -- as entry calls and handled accordingly.
4153 -- In Ada 2005, this may be an indirect call to an access parameter that
4154 -- is an access_to_subprogram. In that case the anonymous type has a
4155 -- scope that is a protected operation, but the call is a regular one.
4156 -- In either case do not expand call if subprogram is eliminated.
4158 Scop := Scope (Subp);
4160 if Nkind (Call_Node) /= N_Entry_Call_Statement
4161 and then Is_Protected_Type (Scop)
4162 and then Ekind (Subp) /= E_Subprogram_Type
4163 and then not Is_Eliminated (Subp)
4165 -- If the call is an internal one, it is rewritten as a call to the
4166 -- corresponding unprotected subprogram.
4168 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4171 -- Functions returning controlled objects need special attention. If
4172 -- the return type is limited, then the context is initialization and
4173 -- different processing applies. If the call is to a protected function,
4174 -- the expansion above will call Expand_Call recursively. Otherwise the
4175 -- function call is transformed into a temporary which obtains the
4176 -- result from the secondary stack.
4178 if Needs_Finalization (Etype (Subp)) then
4179 if not Is_Limited_View (Etype (Subp))
4181 (No (First_Formal (Subp))
4183 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4185 Expand_Ctrl_Function_Call (Call_Node);
4187 -- Build-in-place function calls which appear in anonymous contexts
4188 -- need a transient scope to ensure the proper finalization of the
4189 -- intermediate result after its use.
4191 elsif Is_Build_In_Place_Function_Call (Call_Node)
4193 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
4195 N_Indexed_Component,
4196 N_Object_Renaming_Declaration,
4197 N_Procedure_Call_Statement,
4198 N_Selected_Component,
4201 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
4206 -------------------------------
4207 -- Expand_Ctrl_Function_Call --
4208 -------------------------------
4210 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4211 function Is_Element_Reference (N : Node_Id) return Boolean;
4212 -- Determine whether node N denotes a reference to an Ada 2012 container
4215 --------------------------
4216 -- Is_Element_Reference --
4217 --------------------------
4219 function Is_Element_Reference (N : Node_Id) return Boolean is
4220 Ref : constant Node_Id := Original_Node (N);
4223 -- Analysis marks an element reference by setting the generalized
4224 -- indexing attribute of an indexed component before the component
4225 -- is rewritten into a function call.
4228 Nkind (Ref) = N_Indexed_Component
4229 and then Present (Generalized_Indexing (Ref));
4230 end Is_Element_Reference;
4232 -- Start of processing for Expand_Ctrl_Function_Call
4235 -- Optimization, if the returned value (which is on the sec-stack) is
4236 -- returned again, no need to copy/readjust/finalize, we can just pass
4237 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4238 -- attachment is needed
4240 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4244 -- Resolution is now finished, make sure we don't start analysis again
4245 -- because of the duplication.
4249 -- A function which returns a controlled object uses the secondary
4250 -- stack. Rewrite the call into a temporary which obtains the result of
4251 -- the function using 'reference.
4253 Remove_Side_Effects (N);
4255 -- The side effect removal of the function call produced a temporary.
4256 -- When the context is a case expression, if expression, or expression
4257 -- with actions, the lifetime of the temporary must be extended to match
4258 -- that of the context. Otherwise the function result will be finalized
4259 -- too early and affect the result of the expression. To prevent this
4260 -- unwanted effect, the temporary should not be considered for clean up
4261 -- actions by the general finalization machinery.
4263 -- Exception to this rule are references to Ada 2012 container elements.
4264 -- Such references must be finalized at the end of each iteration of the
4265 -- related quantified expression, otherwise the container will remain
4268 if Nkind (N) = N_Explicit_Dereference
4269 and then Within_Case_Or_If_Expression (N)
4270 and then not Is_Element_Reference (N)
4272 Set_Is_Ignored_Transient (Entity (Prefix (N)));
4274 end Expand_Ctrl_Function_Call;
4276 ----------------------------------------
4277 -- Expand_N_Extended_Return_Statement --
4278 ----------------------------------------
4280 -- If there is a Handled_Statement_Sequence, we rewrite this:
4282 -- return Result : T := <expression> do
4283 -- <handled_seq_of_stms>
4289 -- Result : T := <expression>;
4291 -- <handled_seq_of_stms>
4295 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4297 -- return Result : T := <expression>;
4301 -- return <expression>;
4303 -- unless it's build-in-place or there's no <expression>, in which case
4307 -- Result : T := <expression>;
4312 -- Note that this case could have been written by the user as an extended
4313 -- return statement, or could have been transformed to this from a simple
4314 -- return statement.
4316 -- That is, we need to have a reified return object if there are statements
4317 -- (which might refer to it) or if we're doing build-in-place (so we can
4318 -- set its address to the final resting place or if there is no expression
4319 -- (in which case default initial values might need to be set).
4321 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4322 Loc : constant Source_Ptr := Sloc (N);
4324 function Build_Heap_Allocator
4325 (Temp_Id : Entity_Id;
4326 Temp_Typ : Entity_Id;
4327 Func_Id : Entity_Id;
4328 Ret_Typ : Entity_Id;
4329 Alloc_Expr : Node_Id) return Node_Id;
4330 -- Create the statements necessary to allocate a return object on the
4331 -- caller's master. The master is available through implicit parameter
4332 -- BIPfinalizationmaster.
4334 -- if BIPfinalizationmaster /= null then
4336 -- type Ptr_Typ is access Ret_Typ;
4337 -- for Ptr_Typ'Storage_Pool use
4338 -- Base_Pool (BIPfinalizationmaster.all).all;
4342 -- procedure Allocate (...) is
4344 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4347 -- Local := <Alloc_Expr>;
4348 -- Temp_Id := Temp_Typ (Local);
4352 -- Temp_Id is the temporary which is used to reference the internally
4353 -- created object in all allocation forms. Temp_Typ is the type of the
4354 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4355 -- type of Func_Id. Alloc_Expr is the actual allocator.
4357 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
4358 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4360 -- From current activation chain
4361 -- To activation chain passed in by the caller
4362 -- New_Master master passed in by the caller
4364 -- Func_Id is the entity of the function where the extended return
4365 -- statement appears.
4367 --------------------------
4368 -- Build_Heap_Allocator --
4369 --------------------------
4371 function Build_Heap_Allocator
4372 (Temp_Id : Entity_Id;
4373 Temp_Typ : Entity_Id;
4374 Func_Id : Entity_Id;
4375 Ret_Typ : Entity_Id;
4376 Alloc_Expr : Node_Id) return Node_Id
4379 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4381 -- Processing for build-in-place object allocation.
4383 if Needs_Finalization (Ret_Typ) then
4385 Decls : constant List_Id := New_List;
4386 Fin_Mas_Id : constant Entity_Id :=
4387 Build_In_Place_Formal
4388 (Func_Id, BIP_Finalization_Master);
4389 Stmts : constant List_Id := New_List;
4390 Desig_Typ : Entity_Id;
4391 Local_Id : Entity_Id;
4392 Pool_Id : Entity_Id;
4393 Ptr_Typ : Entity_Id;
4397 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4399 Pool_Id := Make_Temporary (Loc, 'P');
4402 Make_Object_Renaming_Declaration (Loc,
4403 Defining_Identifier => Pool_Id,
4405 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4407 Make_Explicit_Dereference (Loc,
4409 Make_Function_Call (Loc,
4411 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4412 Parameter_Associations => New_List (
4413 Make_Explicit_Dereference (Loc,
4415 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4417 -- Create an access type which uses the storage pool of the
4418 -- caller's master. This additional type is necessary because
4419 -- the finalization master cannot be associated with the type
4420 -- of the temporary. Otherwise the secondary stack allocation
4423 Desig_Typ := Ret_Typ;
4425 -- Ensure that the build-in-place machinery uses a fat pointer
4426 -- when allocating an unconstrained array on the heap. In this
4427 -- case the result object type is a constrained array type even
4428 -- though the function type is unconstrained.
4430 if Ekind (Desig_Typ) = E_Array_Subtype then
4431 Desig_Typ := Base_Type (Desig_Typ);
4435 -- type Ptr_Typ is access Desig_Typ;
4437 Ptr_Typ := Make_Temporary (Loc, 'P');
4440 Make_Full_Type_Declaration (Loc,
4441 Defining_Identifier => Ptr_Typ,
4443 Make_Access_To_Object_Definition (Loc,
4444 Subtype_Indication =>
4445 New_Occurrence_Of (Desig_Typ, Loc))));
4447 -- Perform minor decoration in order to set the master and the
4448 -- storage pool attributes.
4450 Set_Ekind (Ptr_Typ, E_Access_Type);
4451 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4452 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4454 -- Create the temporary, generate:
4455 -- Local_Id : Ptr_Typ;
4457 Local_Id := Make_Temporary (Loc, 'T');
4460 Make_Object_Declaration (Loc,
4461 Defining_Identifier => Local_Id,
4462 Object_Definition =>
4463 New_Occurrence_Of (Ptr_Typ, Loc)));
4465 -- Allocate the object, generate:
4466 -- Local_Id := <Alloc_Expr>;
4469 Make_Assignment_Statement (Loc,
4470 Name => New_Occurrence_Of (Local_Id, Loc),
4471 Expression => Alloc_Expr));
4474 -- Temp_Id := Temp_Typ (Local_Id);
4477 Make_Assignment_Statement (Loc,
4478 Name => New_Occurrence_Of (Temp_Id, Loc),
4480 Unchecked_Convert_To (Temp_Typ,
4481 New_Occurrence_Of (Local_Id, Loc))));
4483 -- Wrap the allocation in a block. This is further conditioned
4484 -- by checking the caller finalization master at runtime. A
4485 -- null value indicates a non-existent master, most likely due
4486 -- to a Finalize_Storage_Only allocation.
4489 -- if BIPfinalizationmaster /= null then
4498 Make_If_Statement (Loc,
4501 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4502 Right_Opnd => Make_Null (Loc)),
4504 Then_Statements => New_List (
4505 Make_Block_Statement (Loc,
4506 Declarations => Decls,
4507 Handled_Statement_Sequence =>
4508 Make_Handled_Sequence_Of_Statements (Loc,
4509 Statements => Stmts))));
4512 -- For all other cases, generate:
4513 -- Temp_Id := <Alloc_Expr>;
4517 Make_Assignment_Statement (Loc,
4518 Name => New_Occurrence_Of (Temp_Id, Loc),
4519 Expression => Alloc_Expr);
4521 end Build_Heap_Allocator;
4523 ---------------------------
4524 -- Move_Activation_Chain --
4525 ---------------------------
4527 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
4530 Make_Procedure_Call_Statement (Loc,
4532 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4534 Parameter_Associations => New_List (
4538 Make_Attribute_Reference (Loc,
4539 Prefix => Make_Identifier (Loc, Name_uChain),
4540 Attribute_Name => Name_Unrestricted_Access),
4542 -- Destination chain
4545 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
4550 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
4551 end Move_Activation_Chain;
4555 Func_Id : constant Entity_Id :=
4556 Return_Applies_To (Return_Statement_Entity (N));
4557 Is_BIP_Func : constant Boolean :=
4558 Is_Build_In_Place_Function (Func_Id);
4559 Ret_Obj_Id : constant Entity_Id :=
4560 First_Entity (Return_Statement_Entity (N));
4561 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4562 Ret_Typ : constant Entity_Id := Etype (Func_Id);
4567 Return_Stmt : Node_Id;
4570 -- Start of processing for Expand_N_Extended_Return_Statement
4573 -- Given that functionality of interface thunks is simple (just displace
4574 -- the pointer to the object) they are always handled by means of
4575 -- simple return statements.
4577 pragma Assert (not Is_Thunk (Current_Scope));
4579 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4580 Exp := Expression (Ret_Obj_Decl);
4585 HSS := Handled_Statement_Sequence (N);
4587 -- If the returned object needs finalization actions, the function must
4588 -- perform the appropriate cleanup should it fail to return. The state
4589 -- of the function itself is tracked through a flag which is coupled
4590 -- with the scope finalizer. There is one flag per each return object
4591 -- in case of multiple returns.
4593 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4595 Flag_Decl : Node_Id;
4596 Flag_Id : Entity_Id;
4600 -- Recover the function body
4602 Func_Bod := Unit_Declaration_Node (Func_Id);
4604 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4605 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4608 -- Create a flag to track the function state
4610 Flag_Id := Make_Temporary (Loc, 'F');
4611 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4613 -- Insert the flag at the beginning of the function declarations,
4615 -- Fnn : Boolean := False;
4618 Make_Object_Declaration (Loc,
4619 Defining_Identifier => Flag_Id,
4620 Object_Definition =>
4621 New_Occurrence_Of (Standard_Boolean, Loc),
4623 New_Occurrence_Of (Standard_False, Loc));
4625 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4626 Analyze (Flag_Decl);
4630 -- Build a simple_return_statement that returns the return object when
4631 -- there is a statement sequence, or no expression, or the result will
4632 -- be built in place. Note however that we currently do this for all
4633 -- composite cases, even though nonlimited composite results are not yet
4634 -- built in place (though we plan to do so eventually).
4637 or else Is_Composite_Type (Ret_Typ)
4643 -- If the extended return has a handled statement sequence, then wrap
4644 -- it in a block and use the block as the first statement.
4648 Make_Block_Statement (Loc,
4649 Declarations => New_List,
4650 Handled_Statement_Sequence => HSS));
4653 -- If the result type contains tasks, we call Move_Activation_Chain.
4654 -- Later, the cleanup code will call Complete_Master, which will
4655 -- terminate any unactivated tasks belonging to the return statement
4656 -- master. But Move_Activation_Chain updates their master to be that
4657 -- of the caller, so they will not be terminated unless the return
4658 -- statement completes unsuccessfully due to exception, abort, goto,
4659 -- or exit. As a formality, we test whether the function requires the
4660 -- result to be built in place, though that's necessarily true for
4661 -- the case of result types with task parts.
4663 if Is_BIP_Func and then Has_Task (Ret_Typ) then
4665 -- The return expression is an aggregate for a complex type which
4666 -- contains tasks. This particular case is left unexpanded since
4667 -- the regular expansion would insert all temporaries and
4668 -- initialization code in the wrong block.
4670 if Nkind (Exp) = N_Aggregate then
4671 Expand_N_Aggregate (Exp);
4674 -- Do not move the activation chain if the return object does not
4677 if Has_Task (Etype (Ret_Obj_Id)) then
4678 Append_To (Stmts, Move_Activation_Chain (Func_Id));
4682 -- Update the state of the function right before the object is
4685 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4687 Flag_Id : constant Entity_Id :=
4688 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4695 Make_Assignment_Statement (Loc,
4696 Name => New_Occurrence_Of (Flag_Id, Loc),
4697 Expression => New_Occurrence_Of (Standard_True, Loc)));
4701 -- Build a simple_return_statement that returns the return object
4704 Make_Simple_Return_Statement (Loc,
4705 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4706 Append_To (Stmts, Return_Stmt);
4708 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4711 -- Case where we build a return statement block
4713 if Present (HSS) then
4715 Make_Block_Statement (Loc,
4716 Declarations => Return_Object_Declarations (N),
4717 Handled_Statement_Sequence => HSS);
4719 -- We set the entity of the new block statement to be that of the
4720 -- return statement. This is necessary so that various fields, such
4721 -- as Finalization_Chain_Entity carry over from the return statement
4722 -- to the block. Note that this block is unusual, in that its entity
4723 -- is an E_Return_Statement rather than an E_Block.
4726 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4728 -- If the object decl was already rewritten as a renaming, then we
4729 -- don't want to do the object allocation and transformation of
4730 -- the return object declaration to a renaming. This case occurs
4731 -- when the return object is initialized by a call to another
4732 -- build-in-place function, and that function is responsible for
4733 -- the allocation of the return object.
4736 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4739 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4740 and then Is_Build_In_Place_Function_Call
4741 (Expression (Original_Node (Ret_Obj_Decl))));
4743 -- Return the build-in-place result by reference
4745 Set_By_Ref (Return_Stmt);
4747 elsif Is_BIP_Func then
4749 -- Locate the implicit access parameter associated with the
4750 -- caller-supplied return object and convert the return
4751 -- statement's return object declaration to a renaming of a
4752 -- dereference of the access parameter. If the return object's
4753 -- declaration includes an expression that has not already been
4754 -- expanded as separate assignments, then add an assignment
4755 -- statement to ensure the return object gets initialized.
4758 -- Result : T [:= <expression>];
4765 -- Result : T renames FuncRA.all;
4766 -- [Result := <expression;]
4771 Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl);
4772 Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id);
4774 Init_Assignment : Node_Id := Empty;
4775 Obj_Acc_Formal : Entity_Id;
4776 Obj_Acc_Deref : Node_Id;
4777 Obj_Alloc_Formal : Entity_Id;
4780 -- Build-in-place results must be returned by reference
4782 Set_By_Ref (Return_Stmt);
4784 -- Retrieve the implicit access parameter passed by the caller
4787 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
4789 -- If the return object's declaration includes an expression
4790 -- and the declaration isn't marked as No_Initialization, then
4791 -- we need to generate an assignment to the object and insert
4792 -- it after the declaration before rewriting it as a renaming
4793 -- (otherwise we'll lose the initialization). The case where
4794 -- the result type is an interface (or class-wide interface)
4795 -- is also excluded because the context of the function call
4796 -- must be unconstrained, so the initialization will always
4797 -- be done as part of an allocator evaluation (storage pool
4798 -- or secondary stack), never to a constrained target object
4799 -- passed in by the caller. Besides the assignment being
4800 -- unneeded in this case, it avoids problems with trying to
4801 -- generate a dispatching assignment when the return expression
4802 -- is a nonlimited descendant of a limited interface (the
4803 -- interface has no assignment operation).
4805 if Present (Ret_Obj_Expr)
4806 and then not No_Initialization (Ret_Obj_Decl)
4807 and then not Is_Interface (Ret_Obj_Typ)
4810 Make_Assignment_Statement (Loc,
4811 Name => New_Occurrence_Of (Ret_Obj_Id, Loc),
4812 Expression => Relocate_Node (Ret_Obj_Expr));
4814 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
4815 Set_Assignment_OK (Name (Init_Assignment));
4816 Set_No_Ctrl_Actions (Init_Assignment);
4818 Set_Parent (Name (Init_Assignment), Init_Assignment);
4819 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4821 Set_Expression (Ret_Obj_Decl, Empty);
4823 if Is_Class_Wide_Type (Etype (Ret_Obj_Id))
4824 and then not Is_Class_Wide_Type
4825 (Etype (Expression (Init_Assignment)))
4827 Rewrite (Expression (Init_Assignment),
4828 Make_Type_Conversion (Loc,
4830 New_Occurrence_Of (Etype (Ret_Obj_Id), Loc),
4832 Relocate_Node (Expression (Init_Assignment))));
4835 -- In the case of functions where the calling context can
4836 -- determine the form of allocation needed, initialization
4837 -- is done with each part of the if statement that handles
4838 -- the different forms of allocation (this is true for
4839 -- unconstrained and tagged result subtypes).
4841 if Is_Constrained (Ret_Typ)
4842 and then not Is_Tagged_Type (Underlying_Type (Ret_Typ))
4844 Insert_After (Ret_Obj_Decl, Init_Assignment);
4848 -- When the function's subtype is unconstrained, a run-time
4849 -- test is needed to determine the form of allocation to use
4850 -- for the return object. The function has an implicit formal
4851 -- parameter indicating this. If the BIP_Alloc_Form formal has
4852 -- the value one, then the caller has passed access to an
4853 -- existing object for use as the return object. If the value
4854 -- is two, then the return object must be allocated on the
4855 -- secondary stack. Otherwise, the object must be allocated in
4856 -- a storage pool (currently only supported for the global
4857 -- heap, user-defined storage pools TBD ???). We generate an
4858 -- if statement to test the implicit allocation formal and
4859 -- initialize a local access value appropriately, creating
4860 -- allocators in the secondary stack and global heap cases.
4861 -- The special formal also exists and must be tested when the
4862 -- function has a tagged result, even when the result subtype
4863 -- is constrained, because in general such functions can be
4864 -- called in dispatching contexts and must be handled similarly
4865 -- to functions with a class-wide result.
4867 if not Is_Constrained (Ret_Typ)
4868 or else Is_Tagged_Type (Underlying_Type (Ret_Typ))
4871 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
4874 Pool_Id : constant Entity_Id :=
4875 Make_Temporary (Loc, 'P');
4876 Alloc_Obj_Id : Entity_Id;
4877 Alloc_Obj_Decl : Node_Id;
4878 Alloc_If_Stmt : Node_Id;
4879 Heap_Allocator : Node_Id;
4880 Pool_Decl : Node_Id;
4881 Pool_Allocator : Node_Id;
4882 Ptr_Type_Decl : Node_Id;
4883 Ref_Type : Entity_Id;
4884 SS_Allocator : Node_Id;
4887 -- Reuse the itype created for the function's implicit
4888 -- access formal. This avoids the need to create a new
4889 -- access type here, plus it allows assigning the access
4890 -- formal directly without applying a conversion.
4892 -- Ref_Type := Etype (Object_Access);
4894 -- Create an access type designating the function's
4897 Ref_Type := Make_Temporary (Loc, 'A');
4900 Make_Full_Type_Declaration (Loc,
4901 Defining_Identifier => Ref_Type,
4903 Make_Access_To_Object_Definition (Loc,
4904 All_Present => True,
4905 Subtype_Indication =>
4906 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
4908 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
4910 -- Create an access object that will be initialized to an
4911 -- access value denoting the return object, either coming
4912 -- from an implicit access value passed in by the caller
4913 -- or from the result of an allocator.
4915 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4916 Set_Etype (Alloc_Obj_Id, Ref_Type);
4919 Make_Object_Declaration (Loc,
4920 Defining_Identifier => Alloc_Obj_Id,
4921 Object_Definition =>
4922 New_Occurrence_Of (Ref_Type, Loc));
4924 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
4926 -- Create allocators for both the secondary stack and
4927 -- global heap. If there's an initialization expression,
4928 -- then create these as initialized allocators.
4930 if Present (Ret_Obj_Expr)
4931 and then not No_Initialization (Ret_Obj_Decl)
4933 -- Always use the type of the expression for the
4934 -- qualified expression, rather than the result type.
4935 -- In general we cannot always use the result type
4936 -- for the allocator, because the expression might be
4937 -- of a specific type, such as in the case of an
4938 -- aggregate or even a nonlimited object when the
4939 -- result type is a limited class-wide interface type.
4942 Make_Allocator (Loc,
4944 Make_Qualified_Expression (Loc,
4947 (Etype (Ret_Obj_Expr), Loc),
4948 Expression => New_Copy_Tree (Ret_Obj_Expr)));
4951 -- If the function returns a class-wide type we cannot
4952 -- use the return type for the allocator. Instead we
4953 -- use the type of the expression, which must be an
4954 -- aggregate of a definite type.
4956 if Is_Class_Wide_Type (Ret_Obj_Typ) then
4958 Make_Allocator (Loc,
4961 (Etype (Ret_Obj_Expr), Loc));
4964 Make_Allocator (Loc,
4966 New_Occurrence_Of (Ret_Obj_Typ, Loc));
4969 -- If the object requires default initialization then
4970 -- that will happen later following the elaboration of
4971 -- the object renaming. If we don't turn it off here
4972 -- then the object will be default initialized twice.
4974 Set_No_Initialization (Heap_Allocator);
4977 -- The Pool_Allocator is just like the Heap_Allocator,
4978 -- except we set Storage_Pool and Procedure_To_Call so
4979 -- it will use the user-defined storage pool.
4981 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
4983 -- Do not generate the renaming of the build-in-place
4984 -- pool parameter on ZFP because the parameter is not
4985 -- created in the first place.
4987 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
4989 Make_Object_Renaming_Declaration (Loc,
4990 Defining_Identifier => Pool_Id,
4993 (RTE (RE_Root_Storage_Pool), Loc),
4995 Make_Explicit_Dereference (Loc,
4997 (Build_In_Place_Formal
4998 (Func_Id, BIP_Storage_Pool), Loc)));
4999 Set_Storage_Pool (Pool_Allocator, Pool_Id);
5000 Set_Procedure_To_Call
5001 (Pool_Allocator, RTE (RE_Allocate_Any));
5003 Pool_Decl := Make_Null_Statement (Loc);
5006 -- If the No_Allocators restriction is active, then only
5007 -- an allocator for secondary stack allocation is needed.
5008 -- It's OK for such allocators to have Comes_From_Source
5009 -- set to False, because gigi knows not to flag them as
5010 -- being a violation of No_Implicit_Heap_Allocations.
5012 if Restriction_Active (No_Allocators) then
5013 SS_Allocator := Heap_Allocator;
5014 Heap_Allocator := Make_Null (Loc);
5015 Pool_Allocator := Make_Null (Loc);
5017 -- Otherwise the heap and pool allocators may be needed,
5018 -- so we make another allocator for secondary stack
5022 SS_Allocator := New_Copy_Tree (Heap_Allocator);
5024 -- The heap and pool allocators are marked as
5025 -- Comes_From_Source since they correspond to an
5026 -- explicit user-written allocator (that is, it will
5027 -- only be executed on behalf of callers that call the
5028 -- function as initialization for such an allocator).
5029 -- Prevents errors when No_Implicit_Heap_Allocations
5032 Set_Comes_From_Source (Heap_Allocator, True);
5033 Set_Comes_From_Source (Pool_Allocator, True);
5036 -- The allocator is returned on the secondary stack.
5038 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5039 Set_Procedure_To_Call
5040 (SS_Allocator, RTE (RE_SS_Allocate));
5042 -- The allocator is returned on the secondary stack,
5043 -- so indicate that the function return, as well as
5044 -- all blocks that encloses the allocator, must not
5045 -- release it. The flags must be set now because
5046 -- the decision to use the secondary stack is done
5047 -- very late in the course of expanding the return
5048 -- statement, past the point where these flags are
5051 Set_Uses_Sec_Stack (Func_Id);
5052 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5053 Set_Sec_Stack_Needed_For_Return
5054 (Return_Statement_Entity (N));
5055 Set_Enclosing_Sec_Stack_Return (N);
5057 -- Create an if statement to test the BIP_Alloc_Form
5058 -- formal and initialize the access object to either the
5059 -- BIP_Object_Access formal (BIP_Alloc_Form =
5060 -- Caller_Allocation), the result of allocating the
5061 -- object in the secondary stack (BIP_Alloc_Form =
5062 -- Secondary_Stack), or else an allocator to create the
5063 -- return object in the heap or user-defined pool
5064 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5066 -- ??? An unchecked type conversion must be made in the
5067 -- case of assigning the access object formal to the
5068 -- local access object, because a normal conversion would
5069 -- be illegal in some cases (such as converting access-
5070 -- to-unconstrained to access-to-constrained), but the
5071 -- the unchecked conversion will presumably fail to work
5072 -- right in just such cases. It's not clear at all how to
5076 Make_If_Statement (Loc,
5080 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5082 Make_Integer_Literal (Loc,
5083 UI_From_Int (BIP_Allocation_Form'Pos
5084 (Caller_Allocation)))),
5086 Then_Statements => New_List (
5087 Make_Assignment_Statement (Loc,
5089 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5091 Make_Unchecked_Type_Conversion (Loc,
5093 New_Occurrence_Of (Ref_Type, Loc),
5095 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
5097 Elsif_Parts => New_List (
5098 Make_Elsif_Part (Loc,
5102 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5104 Make_Integer_Literal (Loc,
5105 UI_From_Int (BIP_Allocation_Form'Pos
5106 (Secondary_Stack)))),
5108 Then_Statements => New_List (
5109 Make_Assignment_Statement (Loc,
5111 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5112 Expression => SS_Allocator))),
5114 Make_Elsif_Part (Loc,
5118 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5120 Make_Integer_Literal (Loc,
5121 UI_From_Int (BIP_Allocation_Form'Pos
5124 Then_Statements => New_List (
5125 Build_Heap_Allocator
5126 (Temp_Id => Alloc_Obj_Id,
5127 Temp_Typ => Ref_Type,
5129 Ret_Typ => Ret_Obj_Typ,
5130 Alloc_Expr => Heap_Allocator)))),
5132 Else_Statements => New_List (
5134 Build_Heap_Allocator
5135 (Temp_Id => Alloc_Obj_Id,
5136 Temp_Typ => Ref_Type,
5138 Ret_Typ => Ret_Obj_Typ,
5139 Alloc_Expr => Pool_Allocator)));
5141 -- If a separate initialization assignment was created
5142 -- earlier, append that following the assignment of the
5143 -- implicit access formal to the access object, to ensure
5144 -- that the return object is initialized in that case. In
5145 -- this situation, the target of the assignment must be
5146 -- rewritten to denote a dereference of the access to the
5147 -- return object passed in by the caller.
5149 if Present (Init_Assignment) then
5150 Rewrite (Name (Init_Assignment),
5151 Make_Explicit_Dereference (Loc,
5152 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
5154 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5157 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5160 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5162 -- Remember the local access object for use in the
5163 -- dereference of the renaming created below.
5165 Obj_Acc_Formal := Alloc_Obj_Id;
5169 -- Replace the return object declaration with a renaming of a
5170 -- dereference of the access value designating the return
5174 Make_Explicit_Dereference (Loc,
5175 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
5177 Rewrite (Ret_Obj_Decl,
5178 Make_Object_Renaming_Declaration (Loc,
5179 Defining_Identifier => Ret_Obj_Id,
5180 Access_Definition => Empty,
5181 Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc),
5182 Name => Obj_Acc_Deref));
5184 Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref);
5188 -- Case where we do not build a block
5191 -- We're about to drop Return_Object_Declarations on the floor, so
5192 -- we need to insert it, in case it got expanded into useful code.
5193 -- Remove side effects from expression, which may be duplicated in
5194 -- subsequent checks (see Expand_Simple_Function_Return).
5196 Insert_List_Before (N, Return_Object_Declarations (N));
5197 Remove_Side_Effects (Exp);
5199 -- Build simple_return_statement that returns the expression directly
5201 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5202 Result := Return_Stmt;
5205 -- Set the flag to prevent infinite recursion
5207 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5209 Rewrite (N, Result);
5211 end Expand_N_Extended_Return_Statement;
5213 ----------------------------
5214 -- Expand_N_Function_Call --
5215 ----------------------------
5217 procedure Expand_N_Function_Call (N : Node_Id) is
5220 end Expand_N_Function_Call;
5222 ---------------------------------------
5223 -- Expand_N_Procedure_Call_Statement --
5224 ---------------------------------------
5226 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5229 end Expand_N_Procedure_Call_Statement;
5231 --------------------------------------
5232 -- Expand_N_Simple_Return_Statement --
5233 --------------------------------------
5235 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5237 -- Defend against previous errors (i.e. the return statement calls a
5238 -- function that is not available in configurable runtime).
5240 if Present (Expression (N))
5241 and then Nkind (Expression (N)) = N_Empty
5243 Check_Error_Detected;
5247 -- Distinguish the function and non-function cases:
5249 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5251 | E_Generic_Function
5253 Expand_Simple_Function_Return (N);
5257 | E_Generic_Procedure
5259 | E_Return_Statement
5261 Expand_Non_Function_Return (N);
5264 raise Program_Error;
5268 when RE_Not_Available =>
5270 end Expand_N_Simple_Return_Statement;
5272 ------------------------------
5273 -- Expand_N_Subprogram_Body --
5274 ------------------------------
5276 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5279 -- Add dummy push/pop label nodes at start and end to clear any local
5280 -- exception indications if local-exception-to-goto optimization is active.
5282 -- Add return statement if last statement in body is not a return statement
5283 -- (this makes things easier on Gigi which does not want to have to handle
5284 -- a missing return).
5286 -- Add call to Activate_Tasks if body is a task activator
5288 -- Deal with possible detection of infinite recursion
5290 -- Eliminate body completely if convention stubbed
5292 -- Encode entity names within body, since we will not need to reference
5293 -- these entities any longer in the front end.
5295 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5297 -- Reset Pure indication if any parameter has root type System.Address
5298 -- or has any parameters of limited types, where limited means that the
5299 -- run-time view is limited (i.e. the full type is limited).
5303 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5304 Body_Id : constant Entity_Id := Defining_Entity (N);
5305 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5306 Loc : constant Source_Ptr := Sloc (N);
5308 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5309 -- Append a return statement to the statement sequence Stmts if the last
5310 -- statement is not already a return or a goto statement. Note that the
5311 -- latter test is not critical, it does not matter if we add a few extra
5312 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5313 -- the corresponding spec of the subprogram body.
5319 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5320 Last_Stmt : Node_Id;
5325 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5326 -- not relevant in this context since they are not executable.
5328 Last_Stmt := Last (Stmts);
5329 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5333 -- Now insert return unless last statement is a transfer
5335 if not Is_Transfer (Last_Stmt) then
5337 -- The source location for the return is the end label of the
5338 -- procedure if present. Otherwise use the sloc of the last
5339 -- statement in the list. If the list comes from a generated
5340 -- exception handler and we are not debugging generated code,
5341 -- all the statements within the handler are made invisible
5344 if Nkind (Parent (Stmts)) = N_Exception_Handler
5345 and then not Comes_From_Source (Parent (Stmts))
5347 Loc := Sloc (Last_Stmt);
5348 elsif Present (End_Label (HSS)) then
5349 Loc := Sloc (End_Label (HSS));
5351 Loc := Sloc (Last_Stmt);
5354 -- Append return statement, and set analyzed manually. We can't
5355 -- call Analyze on this return since the scope is wrong.
5357 -- Note: it almost works to push the scope and then do the Analyze
5358 -- call, but something goes wrong in some weird cases and it is
5359 -- not worth worrying about ???
5361 Stmt := Make_Simple_Return_Statement (Loc);
5363 -- The return statement is handled properly, and the call to the
5364 -- postcondition, inserted below, does not require information
5365 -- from the body either. However, that call is analyzed in the
5366 -- enclosing scope, and an elaboration check might improperly be
5367 -- added to it. A guard in Sem_Elab is needed to prevent that
5368 -- spurious check, see Check_Elab_Call.
5370 Append_To (Stmts, Stmt);
5371 Set_Analyzed (Stmt);
5373 -- Call the _Postconditions procedure if the related subprogram
5374 -- has contract assertions that need to be verified on exit.
5376 if Ekind (Spec_Id) = E_Procedure
5377 and then Present (Postconditions_Proc (Spec_Id))
5379 Insert_Action (Stmt,
5380 Make_Procedure_Call_Statement (Loc,
5382 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5391 Spec_Id : Entity_Id;
5393 -- Start of processing for Expand_N_Subprogram_Body
5396 if Present (Corresponding_Spec (N)) then
5397 Spec_Id := Corresponding_Spec (N);
5402 -- If this is a Pure function which has any parameters whose root type
5403 -- is System.Address, reset the Pure indication.
5404 -- This check is also performed when the subprogram is frozen, but we
5405 -- repeat it on the body so that the indication is consistent, and so
5406 -- it applies as well to bodies without separate specifications.
5408 if Is_Pure (Spec_Id)
5409 and then Is_Subprogram (Spec_Id)
5410 and then not Has_Pragma_Pure_Function (Spec_Id)
5412 Check_Function_With_Address_Parameter (Spec_Id);
5414 if Spec_Id /= Body_Id then
5415 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5419 -- Set L to either the list of declarations if present, or to the list
5420 -- of statements if no declarations are present. This is used to insert
5421 -- new stuff at the start.
5423 if Is_Non_Empty_List (Declarations (N)) then
5424 L := Declarations (N);
5426 L := Statements (HSS);
5429 -- If local-exception-to-goto optimization active, insert dummy push
5430 -- statements at start, and dummy pop statements at end, but inhibit
5431 -- this if we have No_Exception_Handlers, since they are useless and
5432 -- intefere with analysis, e.g. by codepeer.
5434 if (Debug_Flag_Dot_G
5435 or else Restriction_Active (No_Exception_Propagation))
5436 and then not Restriction_Active (No_Exception_Handlers)
5437 and then not CodePeer_Mode
5438 and then Is_Non_Empty_List (L)
5441 FS : constant Node_Id := First (L);
5442 FL : constant Source_Ptr := Sloc (FS);
5447 -- LS points to either last statement, if statements are present
5448 -- or to the last declaration if there are no statements present.
5449 -- It is the node after which the pop's are generated.
5451 if Is_Non_Empty_List (Statements (HSS)) then
5452 LS := Last (Statements (HSS));
5459 Insert_List_Before_And_Analyze (FS, New_List (
5460 Make_Push_Constraint_Error_Label (FL),
5461 Make_Push_Program_Error_Label (FL),
5462 Make_Push_Storage_Error_Label (FL)));
5464 Insert_List_After_And_Analyze (LS, New_List (
5465 Make_Pop_Constraint_Error_Label (LL),
5466 Make_Pop_Program_Error_Label (LL),
5467 Make_Pop_Storage_Error_Label (LL)));
5471 -- Need poll on entry to subprogram if polling enabled. We only do this
5472 -- for non-empty subprograms, since it does not seem necessary to poll
5473 -- for a dummy null subprogram.
5475 if Is_Non_Empty_List (L) then
5477 -- Do not add a polling call if the subprogram is to be inlined by
5478 -- the back-end, to avoid repeated calls with multiple inlinings.
5480 if Is_Inlined (Spec_Id)
5481 and then Front_End_Inlining
5482 and then Optimization_Level > 1
5486 Generate_Poll_Call (First (L));
5490 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5492 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5498 -- Loop through formals
5500 F := First_Formal (Spec_Id);
5501 while Present (F) loop
5502 if Is_Scalar_Type (Etype (F))
5503 and then Ekind (F) = E_Out_Parameter
5505 Check_Restriction (No_Default_Initialization, F);
5507 -- Insert the initialization. We turn off validity checks
5508 -- for this assignment, since we do not want any check on
5509 -- the initial value itself (which may well be invalid).
5510 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5513 Make_Assignment_Statement (Loc,
5514 Name => New_Occurrence_Of (F, Loc),
5515 Expression => Get_Simple_Init_Val (Etype (F), N));
5516 Set_Suppress_Assignment_Checks (A);
5518 Insert_Before_And_Analyze (First (L),
5519 A, Suppress => Validity_Check);
5527 -- Clear out statement list for stubbed procedure
5529 if Present (Corresponding_Spec (N)) then
5530 Set_Elaboration_Flag (N, Spec_Id);
5532 if Convention (Spec_Id) = Convention_Stubbed
5533 or else Is_Eliminated (Spec_Id)
5535 Set_Declarations (N, Empty_List);
5536 Set_Handled_Statement_Sequence (N,
5537 Make_Handled_Sequence_Of_Statements (Loc,
5538 Statements => New_List (Make_Null_Statement (Loc))));
5544 -- Create a set of discriminals for the next protected subprogram body
5546 if Is_List_Member (N)
5547 and then Present (Parent (List_Containing (N)))
5548 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5549 and then Present (Next_Protected_Operation (N))
5551 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5554 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5555 -- subprograms with no specs are not frozen.
5558 Typ : constant Entity_Id := Etype (Spec_Id);
5559 Utyp : constant Entity_Id := Underlying_Type (Typ);
5562 if Is_Limited_View (Typ) then
5563 Set_Returns_By_Ref (Spec_Id);
5565 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5566 Set_Returns_By_Ref (Spec_Id);
5570 -- For a procedure, we add a return for all possible syntactic ends of
5573 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5574 Add_Return (Spec_Id, Statements (HSS));
5576 if Present (Exception_Handlers (HSS)) then
5577 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5578 while Present (Except_H) loop
5579 Add_Return (Spec_Id, Statements (Except_H));
5580 Next_Non_Pragma (Except_H);
5584 -- For a function, we must deal with the case where there is at least
5585 -- one missing return. What we do is to wrap the entire body of the
5586 -- function in a block:
5599 -- raise Program_Error;
5602 -- This approach is necessary because the raise must be signalled to the
5603 -- caller, not handled by any local handler (RM 6.4(11)).
5605 -- Note: we do not need to analyze the constructed sequence here, since
5606 -- it has no handler, and an attempt to analyze the handled statement
5607 -- sequence twice is risky in various ways (e.g. the issue of expanding
5608 -- cleanup actions twice).
5610 elsif Has_Missing_Return (Spec_Id) then
5612 Hloc : constant Source_Ptr := Sloc (HSS);
5613 Blok : constant Node_Id :=
5614 Make_Block_Statement (Hloc,
5615 Handled_Statement_Sequence => HSS);
5616 Rais : constant Node_Id :=
5617 Make_Raise_Program_Error (Hloc,
5618 Reason => PE_Missing_Return);
5621 Set_Handled_Statement_Sequence (N,
5622 Make_Handled_Sequence_Of_Statements (Hloc,
5623 Statements => New_List (Blok, Rais)));
5625 Push_Scope (Spec_Id);
5632 -- If subprogram contains a parameterless recursive call, then we may
5633 -- have an infinite recursion, so see if we can generate code to check
5634 -- for this possibility if storage checks are not suppressed.
5636 if Ekind (Spec_Id) = E_Procedure
5637 and then Has_Recursive_Call (Spec_Id)
5638 and then not Storage_Checks_Suppressed (Spec_Id)
5640 Detect_Infinite_Recursion (N, Spec_Id);
5643 -- Set to encode entity names in package body before gigi is called
5645 Qualify_Entity_Names (N);
5646 end Expand_N_Subprogram_Body;
5648 -----------------------------------
5649 -- Expand_N_Subprogram_Body_Stub --
5650 -----------------------------------
5652 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5656 if Present (Corresponding_Body (N)) then
5657 Bod := Unit_Declaration_Node (Corresponding_Body (N));
5659 -- The body may have been expanded already when it is analyzed
5660 -- through the subunit node. Do no expand again: it interferes
5661 -- with the construction of unnesting tables when generating C.
5663 if not Analyzed (Bod) then
5664 Expand_N_Subprogram_Body (Bod);
5667 -- Add full qualification to entities that may be created late
5668 -- during unnesting.
5670 Qualify_Entity_Names (N);
5672 end Expand_N_Subprogram_Body_Stub;
5674 -------------------------------------
5675 -- Expand_N_Subprogram_Declaration --
5676 -------------------------------------
5678 -- If the declaration appears within a protected body, it is a private
5679 -- operation of the protected type. We must create the corresponding
5680 -- protected subprogram an associated formals. For a normal protected
5681 -- operation, this is done when expanding the protected type declaration.
5683 -- If the declaration is for a null procedure, emit null body
5685 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5686 Loc : constant Source_Ptr := Sloc (N);
5687 Subp : constant Entity_Id := Defining_Entity (N);
5691 Scop : constant Entity_Id := Scope (Subp);
5693 Prot_Decl : Node_Id;
5694 Prot_Id : Entity_Id;
5696 -- Start of processing for Expand_N_Subprogram_Declaration
5699 -- In SPARK, subprogram declarations are only allowed in package
5702 if Nkind (Parent (N)) /= N_Package_Specification then
5703 if Nkind (Parent (N)) = N_Compilation_Unit then
5704 Check_SPARK_05_Restriction
5705 ("subprogram declaration is not a library item", N);
5707 elsif Present (Next (N))
5708 and then Nkind (Next (N)) = N_Pragma
5709 and then Get_Pragma_Id (Next (N)) = Pragma_Import
5711 -- In SPARK, subprogram declarations are also permitted in
5712 -- declarative parts when immediately followed by a corresponding
5713 -- pragma Import. We only check here that there is some pragma
5718 Check_SPARK_05_Restriction
5719 ("subprogram declaration is not allowed here", N);
5723 -- Deal with case of protected subprogram. Do not generate protected
5724 -- operation if operation is flagged as eliminated.
5726 if Is_List_Member (N)
5727 and then Present (Parent (List_Containing (N)))
5728 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5729 and then Is_Protected_Type (Scop)
5731 if No (Protected_Body_Subprogram (Subp))
5732 and then not Is_Eliminated (Subp)
5735 Make_Subprogram_Declaration (Loc,
5737 Build_Protected_Sub_Specification
5738 (N, Scop, Unprotected_Mode));
5740 -- The protected subprogram is declared outside of the protected
5741 -- body. Given that the body has frozen all entities so far, we
5742 -- analyze the subprogram and perform freezing actions explicitly.
5743 -- including the generation of an explicit freeze node, to ensure
5744 -- that gigi has the proper order of elaboration.
5745 -- If the body is a subunit, the insertion point is before the
5746 -- stub in the parent.
5748 Prot_Bod := Parent (List_Containing (N));
5750 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5751 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5754 Insert_Before (Prot_Bod, Prot_Decl);
5755 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5756 Set_Has_Delayed_Freeze (Prot_Id);
5758 Push_Scope (Scope (Scop));
5759 Analyze (Prot_Decl);
5760 Freeze_Before (N, Prot_Id);
5761 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5763 -- Create protected operation as well. Even though the operation
5764 -- is only accessible within the body, it is possible to make it
5765 -- available outside of the protected object by using 'Access to
5766 -- provide a callback, so build protected version in all cases.
5769 Make_Subprogram_Declaration (Loc,
5771 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5772 Insert_Before (Prot_Bod, Prot_Decl);
5773 Analyze (Prot_Decl);
5778 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5779 -- cases this is superfluous because calls to it will be automatically
5780 -- inlined, but we definitely need the body if preconditions for the
5781 -- procedure are present, or if performing coverage analysis.
5783 elsif Nkind (Specification (N)) = N_Procedure_Specification
5784 and then Null_Present (Specification (N))
5787 Bod : constant Node_Id := Body_To_Inline (N);
5790 Set_Has_Completion (Subp, False);
5791 Append_Freeze_Action (Subp, Bod);
5793 -- The body now contains raise statements, so calls to it will
5796 Set_Is_Inlined (Subp, False);
5800 -- When generating C code, transform a function that returns a
5801 -- constrained array type into a procedure with an out parameter
5802 -- that carries the return value.
5804 -- We skip this transformation for unchecked conversions, since they
5805 -- are not needed by the C generator (and this also produces cleaner
5808 if Modify_Tree_For_C
5809 and then Nkind (Specification (N)) = N_Function_Specification
5810 and then Is_Array_Type (Etype (Subp))
5811 and then Is_Constrained (Etype (Subp))
5812 and then not Is_Unchecked_Conversion_Instance (Subp)
5814 Build_Procedure_Form (N);
5816 end Expand_N_Subprogram_Declaration;
5818 --------------------------------
5819 -- Expand_Non_Function_Return --
5820 --------------------------------
5822 procedure Expand_Non_Function_Return (N : Node_Id) is
5823 pragma Assert (No (Expression (N)));
5825 Loc : constant Source_Ptr := Sloc (N);
5826 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
5827 Kind : constant Entity_Kind := Ekind (Scope_Id);
5830 Goto_Stat : Node_Id;
5834 -- Call the _Postconditions procedure if the related subprogram has
5835 -- contract assertions that need to be verified on exit.
5837 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
5838 and then Present (Postconditions_Proc (Scope_Id))
5841 Make_Procedure_Call_Statement (Loc,
5842 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
5845 -- If it is a return from a procedure do no extra steps
5847 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5850 -- If it is a nested return within an extended one, replace it with a
5851 -- return of the previously declared return object.
5853 elsif Kind = E_Return_Statement then
5855 Make_Simple_Return_Statement (Loc,
5857 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5858 Set_Comes_From_Extended_Return_Statement (N);
5859 Set_Return_Statement_Entity (N, Scope_Id);
5860 Expand_Simple_Function_Return (N);
5864 pragma Assert (Is_Entry (Scope_Id));
5866 -- Look at the enclosing block to see whether the return is from an
5867 -- accept statement or an entry body.
5869 for J in reverse 0 .. Scope_Stack.Last loop
5870 Scope_Id := Scope_Stack.Table (J).Entity;
5871 exit when Is_Concurrent_Type (Scope_Id);
5874 -- If it is a return from accept statement it is expanded as call to
5875 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5877 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5878 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5880 if Is_Task_Type (Scope_Id) then
5883 Make_Procedure_Call_Statement (Loc,
5884 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
5885 Insert_Before (N, Call);
5886 -- why not insert actions here???
5889 Acc_Stat := Parent (N);
5890 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5891 Acc_Stat := Parent (Acc_Stat);
5894 Lab_Node := Last (Statements
5895 (Handled_Statement_Sequence (Acc_Stat)));
5897 Goto_Stat := Make_Goto_Statement (Loc,
5898 Name => New_Occurrence_Of
5899 (Entity (Identifier (Lab_Node)), Loc));
5901 Set_Analyzed (Goto_Stat);
5903 Rewrite (N, Goto_Stat);
5906 -- If it is a return from an entry body, put a Complete_Entry_Body call
5907 -- in front of the return.
5909 elsif Is_Protected_Type (Scope_Id) then
5911 Make_Procedure_Call_Statement (Loc,
5913 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
5914 Parameter_Associations => New_List (
5915 Make_Attribute_Reference (Loc,
5918 (Find_Protection_Object (Current_Scope), Loc),
5919 Attribute_Name => Name_Unchecked_Access)));
5921 Insert_Before (N, Call);
5924 end Expand_Non_Function_Return;
5926 ---------------------------------------
5927 -- Expand_Protected_Object_Reference --
5928 ---------------------------------------
5930 function Expand_Protected_Object_Reference
5932 Scop : Entity_Id) return Node_Id
5934 Loc : constant Source_Ptr := Sloc (N);
5941 Rec := Make_Identifier (Loc, Name_uObject);
5942 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5944 -- Find enclosing protected operation, and retrieve its first parameter,
5945 -- which denotes the enclosing protected object. If the enclosing
5946 -- operation is an entry, we are immediately within the protected body,
5947 -- and we can retrieve the object from the service entries procedure. A
5948 -- barrier function has the same signature as an entry. A barrier
5949 -- function is compiled within the protected object, but unlike
5950 -- protected operations its never needs locks, so that its protected
5951 -- body subprogram points to itself.
5953 Proc := Current_Scope;
5954 while Present (Proc)
5955 and then Scope (Proc) /= Scop
5957 Proc := Scope (Proc);
5960 Corr := Protected_Body_Subprogram (Proc);
5964 -- Previous error left expansion incomplete.
5965 -- Nothing to do on this call.
5972 (First (Parameter_Specifications (Parent (Corr))));
5974 if Is_Subprogram (Proc) and then Proc /= Corr then
5976 -- Protected function or procedure
5978 Set_Entity (Rec, Param);
5980 -- Rec is a reference to an entity which will not be in scope when
5981 -- the call is reanalyzed, and needs no further analysis.
5986 -- Entry or barrier function for entry body. The first parameter of
5987 -- the entry body procedure is pointer to the object. We create a
5988 -- local variable of the proper type, duplicating what is done to
5989 -- define _object later on.
5993 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
5997 Make_Full_Type_Declaration (Loc,
5998 Defining_Identifier => Obj_Ptr,
6000 Make_Access_To_Object_Definition (Loc,
6001 Subtype_Indication =>
6003 (Corresponding_Record_Type (Scop), Loc))));
6005 Insert_Actions (N, Decls);
6006 Freeze_Before (N, Obj_Ptr);
6009 Make_Explicit_Dereference (Loc,
6011 Unchecked_Convert_To (Obj_Ptr,
6012 New_Occurrence_Of (Param, Loc)));
6014 -- Analyze new actual. Other actuals in calls are already analyzed
6015 -- and the list of actuals is not reanalyzed after rewriting.
6017 Set_Parent (Rec, N);
6023 end Expand_Protected_Object_Reference;
6025 --------------------------------------
6026 -- Expand_Protected_Subprogram_Call --
6027 --------------------------------------
6029 procedure Expand_Protected_Subprogram_Call
6036 procedure Expand_Internal_Init_Call;
6037 -- A call to an operation of the type may occur in the initialization
6038 -- of a private component. In that case the prefix of the call is an
6039 -- entity name and the call is treated as internal even though it
6040 -- appears in code outside of the protected type.
6042 procedure Freeze_Called_Function;
6043 -- If it is a function call it can appear in elaboration code and
6044 -- the called entity must be frozen before the call. This must be
6045 -- done before the call is expanded, as the expansion may rewrite it
6046 -- to something other than a call (e.g. a temporary initialized in a
6047 -- transient block).
6049 -------------------------------
6050 -- Expand_Internal_Init_Call --
6051 -------------------------------
6053 procedure Expand_Internal_Init_Call is
6055 -- If the context is a protected object (rather than a protected
6056 -- type) the call itself is bound to raise program_error because
6057 -- the protected body will not have been elaborated yet. This is
6058 -- diagnosed subsequently in Sem_Elab.
6060 Freeze_Called_Function;
6062 -- The target of the internal call is the first formal of the
6063 -- enclosing initialization procedure.
6065 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6066 Build_Protected_Subprogram_Call (N,
6071 Resolve (N, Etype (Subp));
6072 end Expand_Internal_Init_Call;
6074 ----------------------------
6075 -- Freeze_Called_Function --
6076 ----------------------------
6078 procedure Freeze_Called_Function is
6080 if Ekind (Subp) = E_Function then
6081 Freeze_Expression (Name (N));
6083 end Freeze_Called_Function;
6085 -- Start of processing for Expand_Protected_Subprogram_Call
6088 -- If the protected object is not an enclosing scope, this is an inter-
6089 -- object function call. Inter-object procedure calls are expanded by
6090 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6091 -- subprogram being called is in the protected body being compiled, and
6092 -- if the protected object in the call is statically the enclosing type.
6093 -- The object may be a component of some other data structure, in which
6094 -- case this must be handled as an inter-object call.
6096 if not In_Open_Scopes (Scop)
6097 or else Is_Entry_Wrapper (Current_Scope)
6098 or else not Is_Entity_Name (Name (N))
6100 if Nkind (Name (N)) = N_Selected_Component then
6101 Rec := Prefix (Name (N));
6103 elsif Nkind (Name (N)) = N_Indexed_Component then
6104 Rec := Prefix (Prefix (Name (N)));
6106 -- If this is a call within an entry wrapper, it appears within a
6107 -- precondition that calls another primitive of the synchronized
6108 -- type. The target object of the call is the first actual on the
6109 -- wrapper. Note that this is an external call, because the wrapper
6110 -- is called outside of the synchronized object. This means that
6111 -- an entry call to an entry with preconditions involves two
6112 -- synchronized operations.
6114 elsif Ekind (Current_Scope) = E_Procedure
6115 and then Is_Entry_Wrapper (Current_Scope)
6117 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6120 -- If the context is the initialization procedure for a protected
6121 -- type, the call is legal because the called entity must be a
6122 -- function of that enclosing type, and this is treated as an
6126 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6128 Expand_Internal_Init_Call;
6132 Freeze_Called_Function;
6133 Build_Protected_Subprogram_Call (N,
6134 Name => New_Occurrence_Of (Subp, Sloc (N)),
6135 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6139 Rec := Expand_Protected_Object_Reference (N, Scop);
6145 Freeze_Called_Function;
6146 Build_Protected_Subprogram_Call (N,
6152 -- Analyze and resolve the new call. The actuals have already been
6153 -- resolved, but expansion of a function call will add extra actuals
6154 -- if needed. Analysis of a procedure call already includes resolution.
6158 if Ekind (Subp) = E_Function then
6159 Resolve (N, Etype (Subp));
6161 end Expand_Protected_Subprogram_Call;
6163 -----------------------------------
6164 -- Expand_Simple_Function_Return --
6165 -----------------------------------
6167 -- The "simple" comes from the syntax rule simple_return_statement. The
6168 -- semantics are not at all simple.
6170 procedure Expand_Simple_Function_Return (N : Node_Id) is
6171 Loc : constant Source_Ptr := Sloc (N);
6173 Scope_Id : constant Entity_Id :=
6174 Return_Applies_To (Return_Statement_Entity (N));
6175 -- The function we are returning from
6177 R_Type : constant Entity_Id := Etype (Scope_Id);
6178 -- The result type of the function
6180 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6182 Exp : Node_Id := Expression (N);
6183 pragma Assert (Present (Exp));
6185 Exptyp : constant Entity_Id := Etype (Exp);
6186 -- The type of the expression (not necessarily the same as R_Type)
6188 Subtype_Ind : Node_Id;
6189 -- If the result type of the function is class-wide and the expression
6190 -- has a specific type, then we use the expression's type as the type of
6191 -- the return object. In cases where the expression is an aggregate that
6192 -- is built in place, this avoids the need for an expensive conversion
6193 -- of the return object to the specific type on assignments to the
6194 -- individual components.
6197 if Is_Class_Wide_Type (R_Type)
6198 and then not Is_Class_Wide_Type (Exptyp)
6199 and then Nkind (Exp) /= N_Type_Conversion
6201 Subtype_Ind := New_Occurrence_Of (Exptyp, Loc);
6203 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6205 -- If the result type is class-wide and the expression is a view
6206 -- conversion, the conversion plays no role in the expansion because
6207 -- it does not modify the tag of the object. Remove the conversion
6208 -- altogether to prevent tag overwriting.
6210 if Is_Class_Wide_Type (R_Type)
6211 and then not Is_Class_Wide_Type (Exptyp)
6212 and then Nkind (Exp) = N_Type_Conversion
6214 Exp := Expression (Exp);
6218 -- For the case of a simple return that does not come from an extended
6219 -- return, in the case of Ada 2005 where we are returning a limited
6220 -- type, we rewrite "return <expression>;" to be:
6222 -- return _anon_ : <return_subtype> := <expression>
6224 -- The expansion produced by Expand_N_Extended_Return_Statement will
6225 -- contain simple return statements (for example, a block containing
6226 -- simple return of the return object), which brings us back here with
6227 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6228 -- checking for a simple return that does not come from an extended
6229 -- return is to avoid this infinite recursion.
6231 -- The reason for this design is that for Ada 2005 limited returns, we
6232 -- need to reify the return object, so we can build it "in place", and
6233 -- we need a block statement to hang finalization and tasking stuff.
6235 -- ??? In order to avoid disruption, we avoid translating to extended
6236 -- return except in the cases where we really need to (Ada 2005 for
6237 -- inherently limited). We might prefer to do this translation in all
6238 -- cases (except perhaps for the case of Ada 95 inherently limited),
6239 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6240 -- code. This would also allow us to do the build-in-place optimization
6241 -- for efficiency even in cases where it is semantically not required.
6243 -- As before, we check the type of the return expression rather than the
6244 -- return type of the function, because the latter may be a limited
6245 -- class-wide interface type, which is not a limited type, even though
6246 -- the type of the expression may be.
6248 if not Comes_From_Extended_Return_Statement (N)
6249 and then Is_Limited_View (Etype (Expression (N)))
6250 and then Ada_Version >= Ada_2005
6251 and then not Debug_Flag_Dot_L
6253 -- The functionality of interface thunks is simple and it is always
6254 -- handled by means of simple return statements. This leaves their
6255 -- expansion simple and clean.
6257 and then not Is_Thunk (Current_Scope)
6260 Return_Object_Entity : constant Entity_Id :=
6261 Make_Temporary (Loc, 'R', Exp);
6263 Obj_Decl : constant Node_Id :=
6264 Make_Object_Declaration (Loc,
6265 Defining_Identifier => Return_Object_Entity,
6266 Object_Definition => Subtype_Ind,
6269 Ext : constant Node_Id :=
6270 Make_Extended_Return_Statement (Loc,
6271 Return_Object_Declarations => New_List (Obj_Decl));
6272 -- Do not perform this high-level optimization if the result type
6273 -- is an interface because the "this" pointer must be displaced.
6282 -- Here we have a simple return statement that is part of the expansion
6283 -- of an extended return statement (either written by the user, or
6284 -- generated by the above code).
6286 -- Always normalize C/Fortran boolean result. This is not always needed,
6287 -- but it seems a good idea to minimize the passing around of non-
6288 -- normalized values, and in any case this handles the processing of
6289 -- barrier functions for protected types, which turn the condition into
6290 -- a return statement.
6292 if Is_Boolean_Type (Exptyp)
6293 and then Nonzero_Is_True (Exptyp)
6295 Adjust_Condition (Exp);
6296 Adjust_Result_Type (Exp, Exptyp);
6299 -- Do validity check if enabled for returns
6301 if Validity_Checks_On
6302 and then Validity_Check_Returns
6307 -- Check the result expression of a scalar function against the subtype
6308 -- of the function by inserting a conversion. This conversion must
6309 -- eventually be performed for other classes of types, but for now it's
6310 -- only done for scalars.
6313 if Is_Scalar_Type (Exptyp) then
6314 Rewrite (Exp, Convert_To (R_Type, Exp));
6316 -- The expression is resolved to ensure that the conversion gets
6317 -- expanded to generate a possible constraint check.
6319 Analyze_And_Resolve (Exp, R_Type);
6322 -- Deal with returning variable length objects and controlled types
6324 -- Nothing to do if we are returning by reference, or this is not a
6325 -- type that requires special processing (indicated by the fact that
6326 -- it requires a cleanup scope for the secondary stack case).
6328 if Is_Limited_View (Exptyp)
6329 or else Is_Limited_Interface (Exptyp)
6333 -- No copy needed for thunks returning interface type objects since
6334 -- the object is returned by reference and the maximum functionality
6335 -- required is just to displace the pointer.
6337 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
6340 -- If the call is within a thunk and the type is a limited view, the
6341 -- backend will eventually see the non-limited view of the type.
6343 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
6346 elsif not Requires_Transient_Scope (R_Type) then
6348 -- Mutable records with variable-length components are not returned
6349 -- on the sec-stack, so we need to make sure that the back end will
6350 -- only copy back the size of the actual value, and not the maximum
6351 -- size. We create an actual subtype for this purpose. However we
6352 -- need not do it if the expression is a function call since this
6353 -- will be done in the called function and doing it here too would
6354 -- cause a temporary with maximum size to be created.
6357 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6361 if Nkind (Exp) /= N_Function_Call
6362 and then Has_Discriminants (Ubt)
6363 and then not Is_Constrained (Ubt)
6364 and then not Has_Unchecked_Union (Ubt)
6366 Decl := Build_Actual_Subtype (Ubt, Exp);
6367 Ent := Defining_Identifier (Decl);
6368 Insert_Action (Exp, Decl);
6369 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6370 Analyze_And_Resolve (Exp);
6374 -- Here if secondary stack is used
6377 -- Prevent the reclamation of the secondary stack by all enclosing
6378 -- blocks and loops as well as the related function; otherwise the
6379 -- result would be reclaimed too early.
6381 Set_Enclosing_Sec_Stack_Return (N);
6383 -- Optimize the case where the result is a function call. In this
6384 -- case either the result is already on the secondary stack, or is
6385 -- already being returned with the stack pointer depressed and no
6386 -- further processing is required except to set the By_Ref flag
6387 -- to ensure that gigi does not attempt an extra unnecessary copy.
6388 -- (actually not just unnecessary but harmfully wrong in the case
6389 -- of a controlled type, where gigi does not know how to do a copy).
6390 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6391 -- for array types if the constrained status of the target type is
6392 -- different from that of the expression.
6394 if Requires_Transient_Scope (Exptyp)
6396 (not Is_Array_Type (Exptyp)
6397 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6398 or else CW_Or_Has_Controlled_Part (Utyp))
6399 and then Nkind (Exp) = N_Function_Call
6403 -- Remove side effects from the expression now so that other parts
6404 -- of the expander do not have to reanalyze this node without this
6407 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6409 -- For controlled types, do the allocation on the secondary stack
6410 -- manually in order to call adjust at the right time:
6412 -- type Anon1 is access R_Type;
6413 -- for Anon1'Storage_pool use ss_pool;
6414 -- Anon2 : anon1 := new R_Type'(expr);
6415 -- return Anon2.all;
6417 -- We do the same for classwide types that are not potentially
6418 -- controlled (by the virtue of restriction No_Finalization) because
6419 -- gigi is not able to properly allocate class-wide types.
6421 elsif CW_Or_Has_Controlled_Part (Utyp) then
6423 Loc : constant Source_Ptr := Sloc (N);
6424 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6425 Alloc_Node : Node_Id;
6429 Set_Ekind (Acc_Typ, E_Access_Type);
6431 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6433 -- This is an allocator for the secondary stack, and it's fine
6434 -- to have Comes_From_Source set False on it, as gigi knows not
6435 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6438 Make_Allocator (Loc,
6440 Make_Qualified_Expression (Loc,
6441 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6442 Expression => Relocate_Node (Exp)));
6444 -- We do not want discriminant checks on the declaration,
6445 -- given that it gets its value from the allocator.
6447 Set_No_Initialization (Alloc_Node);
6449 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6451 Insert_List_Before_And_Analyze (N, New_List (
6452 Make_Full_Type_Declaration (Loc,
6453 Defining_Identifier => Acc_Typ,
6455 Make_Access_To_Object_Definition (Loc,
6456 Subtype_Indication => Subtype_Ind)),
6458 Make_Object_Declaration (Loc,
6459 Defining_Identifier => Temp,
6460 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6461 Expression => Alloc_Node)));
6464 Make_Explicit_Dereference (Loc,
6465 Prefix => New_Occurrence_Of (Temp, Loc)));
6467 -- Ada 2005 (AI-251): If the type of the returned object is
6468 -- an interface then add an implicit type conversion to force
6469 -- displacement of the "this" pointer.
6471 if Is_Interface (R_Type) then
6472 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6475 Analyze_And_Resolve (Exp, R_Type);
6478 -- Otherwise use the gigi mechanism to allocate result on the
6482 Check_Restriction (No_Secondary_Stack, N);
6483 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6484 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6488 -- Implement the rules of 6.5(8-10), which require a tag check in
6489 -- the case of a limited tagged return type, and tag reassignment for
6490 -- nonlimited tagged results. These actions are needed when the return
6491 -- type is a specific tagged type and the result expression is a
6492 -- conversion or a formal parameter, because in that case the tag of
6493 -- the expression might differ from the tag of the specific result type.
6495 if Is_Tagged_Type (Utyp)
6496 and then not Is_Class_Wide_Type (Utyp)
6497 and then (Nkind_In (Exp, N_Type_Conversion,
6498 N_Unchecked_Type_Conversion)
6499 or else (Is_Entity_Name (Exp)
6500 and then Ekind (Entity (Exp)) in Formal_Kind))
6502 -- When the return type is limited, perform a check that the tag of
6503 -- the result is the same as the tag of the return type.
6505 if Is_Limited_Type (R_Type) then
6507 Make_Raise_Constraint_Error (Loc,
6511 Make_Selected_Component (Loc,
6512 Prefix => Duplicate_Subexpr (Exp),
6513 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6515 Make_Attribute_Reference (Loc,
6517 New_Occurrence_Of (Base_Type (Utyp), Loc),
6518 Attribute_Name => Name_Tag)),
6519 Reason => CE_Tag_Check_Failed));
6521 -- If the result type is a specific nonlimited tagged type, then we
6522 -- have to ensure that the tag of the result is that of the result
6523 -- type. This is handled by making a copy of the expression in
6524 -- the case where it might have a different tag, namely when the
6525 -- expression is a conversion or a formal parameter. We create a new
6526 -- object of the result type and initialize it from the expression,
6527 -- which will implicitly force the tag to be set appropriately.
6531 ExpR : constant Node_Id := Relocate_Node (Exp);
6532 Result_Id : constant Entity_Id :=
6533 Make_Temporary (Loc, 'R', ExpR);
6534 Result_Exp : constant Node_Id :=
6535 New_Occurrence_Of (Result_Id, Loc);
6536 Result_Obj : constant Node_Id :=
6537 Make_Object_Declaration (Loc,
6538 Defining_Identifier => Result_Id,
6539 Object_Definition =>
6540 New_Occurrence_Of (R_Type, Loc),
6541 Constant_Present => True,
6542 Expression => ExpR);
6545 Set_Assignment_OK (Result_Obj);
6546 Insert_Action (Exp, Result_Obj);
6548 Rewrite (Exp, Result_Exp);
6549 Analyze_And_Resolve (Exp, R_Type);
6553 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6554 -- a check that the level of the return expression's underlying type
6555 -- is not deeper than the level of the master enclosing the function.
6556 -- Always generate the check when the type of the return expression
6557 -- is class-wide, when it's a type conversion, or when it's a formal
6558 -- parameter. Otherwise, suppress the check in the case where the
6559 -- return expression has a specific type whose level is known not to
6560 -- be statically deeper than the function's result type.
6562 -- No runtime check needed in interface thunks since it is performed
6563 -- by the target primitive associated with the thunk.
6565 -- Note: accessibility check is skipped in the VM case, since there
6566 -- does not seem to be any practical way to implement this check.
6568 elsif Ada_Version >= Ada_2005
6569 and then Tagged_Type_Expansion
6570 and then Is_Class_Wide_Type (R_Type)
6571 and then not Is_Thunk (Current_Scope)
6572 and then not Scope_Suppress.Suppress (Accessibility_Check)
6574 (Is_Class_Wide_Type (Etype (Exp))
6575 or else Nkind_In (Exp, N_Type_Conversion,
6576 N_Unchecked_Type_Conversion)
6577 or else (Is_Entity_Name (Exp)
6578 and then Ekind (Entity (Exp)) in Formal_Kind)
6579 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6580 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6586 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6587 -- "this" to reference the base of the object. This is required to
6588 -- get access to the TSD of the object.
6590 if Is_Class_Wide_Type (Etype (Exp))
6591 and then Is_Interface (Etype (Exp))
6593 -- If the expression is an explicit dereference then we can
6594 -- directly displace the pointer to reference the base of
6597 if Nkind (Exp) = N_Explicit_Dereference then
6599 Make_Explicit_Dereference (Loc,
6601 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6602 Make_Function_Call (Loc,
6604 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6605 Parameter_Associations => New_List (
6606 Unchecked_Convert_To (RTE (RE_Address),
6607 Duplicate_Subexpr (Prefix (Exp)))))));
6609 -- Similar case to the previous one but the expression is a
6610 -- renaming of an explicit dereference.
6612 elsif Nkind (Exp) = N_Identifier
6613 and then Present (Renamed_Object (Entity (Exp)))
6614 and then Nkind (Renamed_Object (Entity (Exp)))
6615 = N_Explicit_Dereference
6618 Make_Explicit_Dereference (Loc,
6620 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6621 Make_Function_Call (Loc,
6623 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6624 Parameter_Associations => New_List (
6625 Unchecked_Convert_To (RTE (RE_Address),
6628 (Renamed_Object (Entity (Exp)))))))));
6630 -- Common case: obtain the address of the actual object and
6631 -- displace the pointer to reference the base of the object.
6635 Make_Explicit_Dereference (Loc,
6637 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6638 Make_Function_Call (Loc,
6640 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6641 Parameter_Associations => New_List (
6642 Make_Attribute_Reference (Loc,
6643 Prefix => Duplicate_Subexpr (Exp),
6644 Attribute_Name => Name_Address)))));
6648 Make_Attribute_Reference (Loc,
6649 Prefix => Duplicate_Subexpr (Exp),
6650 Attribute_Name => Name_Tag);
6653 -- CodePeer does not do anything useful with
6654 -- Ada.Tags.Type_Specific_Data components.
6656 if not CodePeer_Mode then
6658 Make_Raise_Program_Error (Loc,
6661 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6663 Make_Integer_Literal (Loc,
6664 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6665 Reason => PE_Accessibility_Check_Failed));
6669 -- AI05-0073: If function has a controlling access result, check that
6670 -- the tag of the return value, if it is not null, matches designated
6671 -- type of return type.
6673 -- The return expression is referenced twice in the code below, so it
6674 -- must be made free of side effects. Given that different compilers
6675 -- may evaluate these parameters in different order, both occurrences
6678 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6679 and then Has_Controlling_Result (Scope_Id)
6682 Make_Raise_Constraint_Error (Loc,
6687 Left_Opnd => Duplicate_Subexpr (Exp),
6688 Right_Opnd => Make_Null (Loc)),
6690 Right_Opnd => Make_Op_Ne (Loc,
6692 Make_Selected_Component (Loc,
6693 Prefix => Duplicate_Subexpr (Exp),
6694 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6697 Make_Attribute_Reference (Loc,
6699 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6700 Attribute_Name => Name_Tag))),
6702 Reason => CE_Tag_Check_Failed),
6703 Suppress => All_Checks);
6706 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6707 -- ensure that the function result does not outlive an
6708 -- object designated by one of it discriminants.
6710 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6711 and then Has_Unconstrained_Access_Discriminants (R_Type)
6714 Discrim_Source : Node_Id;
6716 procedure Check_Against_Result_Level (Level : Node_Id);
6717 -- Check the given accessibility level against the level
6718 -- determined by the point of call. (AI05-0234).
6720 --------------------------------
6721 -- Check_Against_Result_Level --
6722 --------------------------------
6724 procedure Check_Against_Result_Level (Level : Node_Id) is
6727 Make_Raise_Program_Error (Loc,
6733 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6734 Reason => PE_Accessibility_Check_Failed));
6735 end Check_Against_Result_Level;
6738 Discrim_Source := Exp;
6739 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6740 Discrim_Source := Expression (Discrim_Source);
6743 if Nkind (Discrim_Source) = N_Identifier
6744 and then Is_Return_Object (Entity (Discrim_Source))
6746 Discrim_Source := Entity (Discrim_Source);
6748 if Is_Constrained (Etype (Discrim_Source)) then
6749 Discrim_Source := Etype (Discrim_Source);
6751 Discrim_Source := Expression (Parent (Discrim_Source));
6754 elsif Nkind (Discrim_Source) = N_Identifier
6755 and then Nkind_In (Original_Node (Discrim_Source),
6756 N_Aggregate, N_Extension_Aggregate)
6758 Discrim_Source := Original_Node (Discrim_Source);
6760 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6761 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6763 Discrim_Source := Original_Node (Discrim_Source);
6766 while Nkind_In (Discrim_Source, N_Qualified_Expression,
6768 N_Unchecked_Type_Conversion)
6770 Discrim_Source := Expression (Discrim_Source);
6773 case Nkind (Discrim_Source) is
6774 when N_Defining_Identifier =>
6775 pragma Assert (Is_Composite_Type (Discrim_Source)
6776 and then Has_Discriminants (Discrim_Source)
6777 and then Is_Constrained (Discrim_Source));
6780 Discrim : Entity_Id :=
6781 First_Discriminant (Base_Type (R_Type));
6782 Disc_Elmt : Elmt_Id :=
6783 First_Elmt (Discriminant_Constraint
6787 if Ekind (Etype (Discrim)) =
6788 E_Anonymous_Access_Type
6790 Check_Against_Result_Level
6791 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
6794 Next_Elmt (Disc_Elmt);
6795 Next_Discriminant (Discrim);
6796 exit when not Present (Discrim);
6801 | N_Extension_Aggregate
6803 -- Unimplemented: extension aggregate case where discrims
6804 -- come from ancestor part, not extension part.
6807 Discrim : Entity_Id :=
6808 First_Discriminant (Base_Type (R_Type));
6810 Disc_Exp : Node_Id := Empty;
6812 Positionals_Exhausted
6813 : Boolean := not Present (Expressions
6816 function Associated_Expr
6817 (Comp_Id : Entity_Id;
6818 Associations : List_Id) return Node_Id;
6820 -- Given a component and a component associations list,
6821 -- locate the expression for that component; returns
6822 -- Empty if no such expression is found.
6824 ---------------------
6825 -- Associated_Expr --
6826 ---------------------
6828 function Associated_Expr
6829 (Comp_Id : Entity_Id;
6830 Associations : List_Id) return Node_Id
6836 -- Simple linear search seems ok here
6838 Assoc := First (Associations);
6839 while Present (Assoc) loop
6840 Choice := First (Choices (Assoc));
6841 while Present (Choice) loop
6842 if (Nkind (Choice) = N_Identifier
6843 and then Chars (Choice) = Chars (Comp_Id))
6844 or else (Nkind (Choice) = N_Others_Choice)
6846 return Expression (Assoc);
6856 end Associated_Expr;
6858 -- Start of processing for Expand_Simple_Function_Return
6861 if not Positionals_Exhausted then
6862 Disc_Exp := First (Expressions (Discrim_Source));
6866 if Positionals_Exhausted then
6870 Component_Associations (Discrim_Source));
6873 if Ekind (Etype (Discrim)) =
6874 E_Anonymous_Access_Type
6876 Check_Against_Result_Level
6877 (Dynamic_Accessibility_Level (Disc_Exp));
6880 Next_Discriminant (Discrim);
6881 exit when not Present (Discrim);
6883 if not Positionals_Exhausted then
6885 Positionals_Exhausted := not Present (Disc_Exp);
6890 when N_Function_Call =>
6892 -- No check needed (check performed by callee)
6898 Level : constant Node_Id :=
6899 Make_Integer_Literal (Loc,
6900 Object_Access_Level (Discrim_Source));
6903 -- Unimplemented: check for name prefix that includes
6904 -- a dereference of an access value with a dynamic
6905 -- accessibility level (e.g., an access param or a
6906 -- saooaaat) and use dynamic level in that case. For
6908 -- return Access_Param.all(Some_Index).Some_Component;
6911 Set_Etype (Level, Standard_Natural);
6912 Check_Against_Result_Level (Level);
6918 -- If we are returning an object that may not be bit-aligned, then copy
6919 -- the value into a temporary first. This copy may need to expand to a
6920 -- loop of component operations.
6922 if Is_Possibly_Unaligned_Slice (Exp)
6923 or else Is_Possibly_Unaligned_Object (Exp)
6926 ExpR : constant Node_Id := Relocate_Node (Exp);
6927 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6930 Make_Object_Declaration (Loc,
6931 Defining_Identifier => Tnn,
6932 Constant_Present => True,
6933 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6934 Expression => ExpR),
6935 Suppress => All_Checks);
6936 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6940 -- Call the _Postconditions procedure if the related function has
6941 -- contract assertions that need to be verified on exit.
6943 if Ekind (Scope_Id) = E_Function
6944 and then Present (Postconditions_Proc (Scope_Id))
6946 -- In the case of discriminated objects, we have created a
6947 -- constrained subtype above, and used the underlying type. This
6948 -- transformation is post-analysis and harmless, except that now the
6949 -- call to the post-condition will be analyzed and the type kinds
6952 if Nkind (Exp) = N_Unchecked_Type_Conversion
6953 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
6955 Rewrite (Exp, Expression (Relocate_Node (Exp)));
6958 -- We are going to reference the returned value twice in this case,
6959 -- once in the call to _Postconditions, and once in the actual return
6960 -- statement, but we can't have side effects happening twice.
6962 Force_Evaluation (Exp, Mode => Strict);
6964 -- Generate call to _Postconditions
6967 Make_Procedure_Call_Statement (Loc,
6969 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
6970 Parameter_Associations => New_List (New_Copy_Tree (Exp))));
6973 -- Ada 2005 (AI-251): If this return statement corresponds with an
6974 -- simple return statement associated with an extended return statement
6975 -- and the type of the returned object is an interface then generate an
6976 -- implicit conversion to force displacement of the "this" pointer.
6978 if Ada_Version >= Ada_2005
6979 and then Comes_From_Extended_Return_Statement (N)
6980 and then Nkind (Expression (N)) = N_Identifier
6981 and then Is_Interface (Utyp)
6982 and then Utyp /= Underlying_Type (Exptyp)
6984 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6985 Analyze_And_Resolve (Exp);
6987 end Expand_Simple_Function_Return;
6989 --------------------------------------------
6990 -- Has_Unconstrained_Access_Discriminants --
6991 --------------------------------------------
6993 function Has_Unconstrained_Access_Discriminants
6994 (Subtyp : Entity_Id) return Boolean
6999 if Has_Discriminants (Subtyp)
7000 and then not Is_Constrained (Subtyp)
7002 Discr := First_Discriminant (Subtyp);
7003 while Present (Discr) loop
7004 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7008 Next_Discriminant (Discr);
7013 end Has_Unconstrained_Access_Discriminants;
7015 --------------------------------
7016 -- Is_Build_In_Place_Function --
7017 --------------------------------
7019 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7021 -- This function is called from Expand_Subtype_From_Expr during
7022 -- semantic analysis, even when expansion is off. In those cases
7023 -- the build_in_place expansion will not take place.
7025 if not Expander_Active then
7029 -- For now we test whether E denotes a function or access-to-function
7030 -- type whose result subtype is inherently limited. Later this test
7031 -- may be revised to allow composite nonlimited types. Functions with
7032 -- a foreign convention or whose result type has a foreign convention
7035 if Ekind_In (E, E_Function, E_Generic_Function)
7036 or else (Ekind (E) = E_Subprogram_Type
7037 and then Etype (E) /= Standard_Void_Type)
7039 -- Note: If the function has a foreign convention, it cannot build
7040 -- its result in place, so you're on your own. On the other hand,
7041 -- if only the return type has a foreign convention, its layout is
7042 -- intended to be compatible with the other language, but the build-
7043 -- in place machinery can ensure that the object is not copied.
7045 if Has_Foreign_Convention (E) then
7048 -- In Ada 2005 all functions with an inherently limited return type
7049 -- must be handled using a build-in-place profile, including the case
7050 -- of a function with a limited interface result, where the function
7051 -- may return objects of nonlimited descendants.
7054 return Is_Limited_View (Etype (E))
7055 and then Ada_Version >= Ada_2005
7056 and then not Debug_Flag_Dot_L;
7062 end Is_Build_In_Place_Function;
7064 -------------------------------------
7065 -- Is_Build_In_Place_Function_Call --
7066 -------------------------------------
7068 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7069 Exp_Node : Node_Id := N;
7070 Function_Id : Entity_Id;
7073 -- Return False if the expander is currently inactive, since awareness
7074 -- of build-in-place treatment is only relevant during expansion. Note
7075 -- that Is_Build_In_Place_Function, which is called as part of this
7076 -- function, is also conditioned this way, but we need to check here as
7077 -- well to avoid blowing up on processing protected calls when expansion
7078 -- is disabled (such as with -gnatc) since those would trip over the
7079 -- raise of Program_Error below.
7081 -- In SPARK mode, build-in-place calls are not expanded, so that we
7082 -- may end up with a call that is neither resolved to an entity, nor
7083 -- an indirect call.
7085 if not Expander_Active then
7089 -- Step past qualification, type conversion (which can occur in actual
7090 -- parameter contexts), and unchecked conversion (which can occur in
7091 -- cases of calls to 'Input).
7093 if Nkind_In (Exp_Node, N_Qualified_Expression,
7095 N_Unchecked_Type_Conversion)
7097 Exp_Node := Expression (N);
7100 if Nkind (Exp_Node) /= N_Function_Call then
7104 if Is_Entity_Name (Name (Exp_Node)) then
7105 Function_Id := Entity (Name (Exp_Node));
7107 -- In the case of an explicitly dereferenced call, use the subprogram
7108 -- type generated for the dereference.
7110 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7111 Function_Id := Etype (Name (Exp_Node));
7113 -- This may be a call to a protected function.
7115 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7116 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7119 raise Program_Error;
7122 return Is_Build_In_Place_Function (Function_Id);
7124 end Is_Build_In_Place_Function_Call;
7126 -----------------------
7127 -- Freeze_Subprogram --
7128 -----------------------
7130 procedure Freeze_Subprogram (N : Node_Id) is
7131 Loc : constant Source_Ptr := Sloc (N);
7133 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7134 -- (Ada 2005): Register a predefined primitive in all the secondary
7135 -- dispatch tables of its primitive type.
7137 ----------------------------------
7138 -- Register_Predefined_DT_Entry --
7139 ----------------------------------
7141 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7142 Iface_DT_Ptr : Elmt_Id;
7143 Tagged_Typ : Entity_Id;
7144 Thunk_Id : Entity_Id;
7145 Thunk_Code : Node_Id;
7148 Tagged_Typ := Find_Dispatching_Type (Prim);
7150 if No (Access_Disp_Table (Tagged_Typ))
7151 or else not Has_Interfaces (Tagged_Typ)
7152 or else not RTE_Available (RE_Interface_Tag)
7153 or else Restriction_Active (No_Dispatching_Calls)
7158 -- Skip the first two access-to-dispatch-table pointers since they
7159 -- leads to the primary dispatch table (predefined DT and user
7160 -- defined DT). We are only concerned with the secondary dispatch
7161 -- table pointers. Note that the access-to- dispatch-table pointer
7162 -- corresponds to the first implemented interface retrieved below.
7165 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7167 while Present (Iface_DT_Ptr)
7168 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7170 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7171 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7173 if Present (Thunk_Code) then
7174 Insert_Actions_After (N, New_List (
7177 Build_Set_Predefined_Prim_Op_Address (Loc,
7179 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7180 Position => DT_Position (Prim),
7182 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7183 Make_Attribute_Reference (Loc,
7184 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7185 Attribute_Name => Name_Unrestricted_Access))),
7187 Build_Set_Predefined_Prim_Op_Address (Loc,
7190 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7192 Position => DT_Position (Prim),
7194 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7195 Make_Attribute_Reference (Loc,
7196 Prefix => New_Occurrence_Of (Prim, Loc),
7197 Attribute_Name => Name_Unrestricted_Access)))));
7200 -- Skip the tag of the predefined primitives dispatch table
7202 Next_Elmt (Iface_DT_Ptr);
7203 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7205 -- Skip tag of the no-thunks dispatch table
7207 Next_Elmt (Iface_DT_Ptr);
7208 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7210 -- Skip tag of predefined primitives no-thunks dispatch table
7212 Next_Elmt (Iface_DT_Ptr);
7213 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7215 Next_Elmt (Iface_DT_Ptr);
7217 end Register_Predefined_DT_Entry;
7221 Subp : constant Entity_Id := Entity (N);
7223 -- Start of processing for Freeze_Subprogram
7226 -- We suppress the initialization of the dispatch table entry when
7227 -- not Tagged_Type_Expansion because the dispatching mechanism is
7228 -- handled internally by the target.
7230 if Is_Dispatching_Operation (Subp)
7231 and then not Is_Abstract_Subprogram (Subp)
7232 and then Present (DTC_Entity (Subp))
7233 and then Present (Scope (DTC_Entity (Subp)))
7234 and then Tagged_Type_Expansion
7235 and then not Restriction_Active (No_Dispatching_Calls)
7236 and then RTE_Available (RE_Tag)
7239 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7242 -- Handle private overridden primitives
7244 if not Is_CPP_Class (Typ) then
7245 Check_Overriding_Operation (Subp);
7248 -- We assume that imported CPP primitives correspond with objects
7249 -- whose constructor is in the CPP side; therefore we don't need
7250 -- to generate code to register them in the dispatch table.
7252 if Is_CPP_Class (Typ) then
7255 -- Handle CPP primitives found in derivations of CPP_Class types.
7256 -- These primitives must have been inherited from some parent, and
7257 -- there is no need to register them in the dispatch table because
7258 -- Build_Inherit_Prims takes care of initializing these slots.
7260 elsif Is_Imported (Subp)
7261 and then (Convention (Subp) = Convention_CPP
7262 or else Convention (Subp) = Convention_C)
7266 -- Generate code to register the primitive in non statically
7267 -- allocated dispatch tables
7269 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7271 -- When a primitive is frozen, enter its name in its dispatch
7274 if not Is_Interface (Typ)
7275 or else Present (Interface_Alias (Subp))
7277 if Is_Predefined_Dispatching_Operation (Subp) then
7278 Register_Predefined_DT_Entry (Subp);
7281 Insert_Actions_After (N,
7282 Register_Primitive (Loc, Prim => Subp));
7288 -- Mark functions that return by reference. Note that it cannot be part
7289 -- of the normal semantic analysis of the spec since the underlying
7290 -- returned type may not be known yet (for private types).
7293 Typ : constant Entity_Id := Etype (Subp);
7294 Utyp : constant Entity_Id := Underlying_Type (Typ);
7297 if Is_Limited_View (Typ) then
7298 Set_Returns_By_Ref (Subp);
7300 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7301 Set_Returns_By_Ref (Subp);
7305 -- Wnen freezing a null procedure, analyze its delayed aspects now
7306 -- because we may not have reached the end of the declarative list when
7307 -- delayed aspects are normally analyzed. This ensures that dispatching
7308 -- calls are properly rewritten when the generated _Postcondition
7309 -- procedure is analyzed in the null procedure body.
7311 if Nkind (Parent (Subp)) = N_Procedure_Specification
7312 and then Null_Present (Parent (Subp))
7314 Analyze_Entry_Or_Subprogram_Contract (Subp);
7316 end Freeze_Subprogram;
7318 -----------------------
7319 -- Is_Null_Procedure --
7320 -----------------------
7322 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
7323 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
7326 if Ekind (Subp) /= E_Procedure then
7329 -- Check if this is a declared null procedure
7331 elsif Nkind (Decl) = N_Subprogram_Declaration then
7332 if not Null_Present (Specification (Decl)) then
7335 elsif No (Body_To_Inline (Decl)) then
7338 -- Check if the body contains only a null statement, followed by
7339 -- the return statement added during expansion.
7343 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
7349 if Nkind (Orig_Bod) /= N_Subprogram_Body then
7352 -- We must skip SCIL nodes because they are currently
7353 -- implemented as special N_Null_Statement nodes.
7357 (Statements (Handled_Statement_Sequence (Orig_Bod)));
7358 Stat2 := Next_Non_SCIL_Node (Stat);
7361 Is_Empty_List (Declarations (Orig_Bod))
7362 and then Nkind (Stat) = N_Null_Statement
7366 (Nkind (Stat2) = N_Simple_Return_Statement
7367 and then No (Next (Stat2))));
7375 end Is_Null_Procedure;
7377 -------------------------------------------
7378 -- Make_Build_In_Place_Call_In_Allocator --
7379 -------------------------------------------
7381 procedure Make_Build_In_Place_Call_In_Allocator
7382 (Allocator : Node_Id;
7383 Function_Call : Node_Id)
7385 Acc_Type : constant Entity_Id := Etype (Allocator);
7387 Func_Call : Node_Id := Function_Call;
7388 Ref_Func_Call : Node_Id;
7389 Function_Id : Entity_Id;
7390 Result_Subt : Entity_Id;
7391 New_Allocator : Node_Id;
7392 Return_Obj_Access : Entity_Id; -- temp for function result
7393 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
7394 Alloc_Form : BIP_Allocation_Form;
7395 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
7396 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
7397 Chain : Entity_Id; -- activation chain, in case of tasks
7400 -- Step past qualification or unchecked conversion (the latter can occur
7401 -- in cases of calls to 'Input).
7403 if Nkind_In (Func_Call,
7404 N_Qualified_Expression,
7406 N_Unchecked_Type_Conversion)
7408 Func_Call := Expression (Func_Call);
7411 -- If the call has already been processed to add build-in-place actuals
7412 -- then return. This should not normally occur in an allocator context,
7413 -- but we add the protection as a defensive measure.
7415 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7419 -- Mark the call as processed as a build-in-place call
7421 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7423 Loc := Sloc (Function_Call);
7425 if Is_Entity_Name (Name (Func_Call)) then
7426 Function_Id := Entity (Name (Func_Call));
7428 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7429 Function_Id := Etype (Name (Func_Call));
7432 raise Program_Error;
7435 Result_Subt := Available_View (Etype (Function_Id));
7437 -- Create a temp for the function result. In the caller-allocates case,
7438 -- this will be initialized to the result of a new uninitialized
7439 -- allocator. Note: we do not use Allocator as the Related_Node of
7440 -- Return_Obj_Access in call to Make_Temporary below as this would
7441 -- create a sort of infinite "recursion".
7443 Return_Obj_Access := Make_Temporary (Loc, 'R');
7444 Set_Etype (Return_Obj_Access, Acc_Type);
7446 -- When the result subtype is constrained, the return object is
7447 -- allocated on the caller side, and access to it is passed to the
7450 -- Here and in related routines, we must examine the full view of the
7451 -- type, because the view at the point of call may differ from that
7452 -- that in the function body, and the expansion mechanism depends on
7453 -- the characteristics of the full view.
7455 if Is_Constrained (Underlying_Type (Result_Subt)) then
7457 -- Replace the initialized allocator of form "new T'(Func (...))"
7458 -- with an uninitialized allocator of form "new T", where T is the
7459 -- result subtype of the called function. The call to the function
7460 -- is handled separately further below.
7463 Make_Allocator (Loc,
7464 Expression => New_Occurrence_Of (Result_Subt, Loc));
7465 Set_No_Initialization (New_Allocator);
7467 -- Copy attributes to new allocator. Note that the new allocator
7468 -- logically comes from source if the original one did, so copy the
7469 -- relevant flag. This ensures proper treatment of the restriction
7470 -- No_Implicit_Heap_Allocations in this case.
7472 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
7473 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
7474 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
7476 Rewrite (Allocator, New_Allocator);
7478 -- Initial value of the temp is the result of the uninitialized
7481 Temp_Init := Relocate_Node (Allocator);
7483 -- Indicate that caller allocates, and pass in the return object
7485 Alloc_Form := Caller_Allocation;
7486 Pool := Make_Null (No_Location);
7487 Return_Obj_Actual :=
7488 Make_Unchecked_Type_Conversion (Loc,
7489 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7491 Make_Explicit_Dereference (Loc,
7492 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
7494 -- When the result subtype is unconstrained, the function itself must
7495 -- perform the allocation of the return object, so we pass parameters
7501 -- Case of a user-defined storage pool. Pass an allocation parameter
7502 -- indicating that the function should allocate its result in the
7503 -- pool, and pass the pool. Use 'Unrestricted_Access because the
7504 -- pool may not be aliased.
7506 if Present (Associated_Storage_Pool (Acc_Type)) then
7507 Alloc_Form := User_Storage_Pool;
7509 Make_Attribute_Reference (Loc,
7512 (Associated_Storage_Pool (Acc_Type), Loc),
7513 Attribute_Name => Name_Unrestricted_Access);
7515 -- No user-defined pool; pass an allocation parameter indicating that
7516 -- the function should allocate its result on the heap.
7519 Alloc_Form := Global_Heap;
7520 Pool := Make_Null (No_Location);
7523 -- The caller does not provide the return object in this case, so we
7524 -- have to pass null for the object access actual.
7526 Return_Obj_Actual := Empty;
7529 -- Declare the temp object
7531 Insert_Action (Allocator,
7532 Make_Object_Declaration (Loc,
7533 Defining_Identifier => Return_Obj_Access,
7534 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
7535 Expression => Temp_Init));
7537 Ref_Func_Call := Make_Reference (Loc, Func_Call);
7539 -- Ada 2005 (AI-251): If the type of the allocator is an interface
7540 -- then generate an implicit conversion to force displacement of the
7543 if Is_Interface (Designated_Type (Acc_Type)) then
7546 OK_Convert_To (Acc_Type, Ref_Func_Call));
7550 Assign : constant Node_Id :=
7551 Make_Assignment_Statement (Loc,
7552 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
7553 Expression => Ref_Func_Call);
7554 -- Assign the result of the function call into the temp. In the
7555 -- caller-allocates case, this is overwriting the temp with its
7556 -- initial value, which has no effect. In the callee-allocates case,
7557 -- this is setting the temp to point to the object allocated by the
7561 -- Actions to be inserted. If there are no tasks, this is just the
7562 -- assignment statement. If the allocated object has tasks, we need
7563 -- to wrap the assignment in a block that activates them. The
7564 -- activation chain of that block must be passed to the function,
7565 -- rather than some outer chain.
7567 if Has_Task (Result_Subt) then
7568 Actions := New_List;
7569 Build_Task_Allocate_Block_With_Init_Stmts
7570 (Actions, Allocator, Init_Stmts => New_List (Assign));
7571 Chain := Activation_Chain_Entity (Last (Actions));
7573 Actions := New_List (Assign);
7577 Insert_Actions (Allocator, Actions);
7580 -- When the function has a controlling result, an allocation-form
7581 -- parameter must be passed indicating that the caller is allocating
7582 -- the result object. This is needed because such a function can be
7583 -- called as a dispatching operation and must be treated similarly
7584 -- to functions with unconstrained result subtypes.
7586 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7587 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
7589 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7590 (Func_Call, Function_Id, Acc_Type);
7592 Add_Task_Actuals_To_Build_In_Place_Call
7593 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
7596 -- Add an implicit actual to the function call that provides access
7597 -- to the allocated object. An unchecked conversion to the (specific)
7598 -- result subtype of the function is inserted to handle cases where
7599 -- the access type of the allocator has a class-wide designated type.
7601 Add_Access_Actual_To_Build_In_Place_Call
7602 (Func_Call, Function_Id, Return_Obj_Actual);
7604 -- Finally, replace the allocator node with a reference to the temp
7606 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
7608 Analyze_And_Resolve (Allocator, Acc_Type);
7609 end Make_Build_In_Place_Call_In_Allocator;
7611 ---------------------------------------------------
7612 -- Make_Build_In_Place_Call_In_Anonymous_Context --
7613 ---------------------------------------------------
7615 procedure Make_Build_In_Place_Call_In_Anonymous_Context
7616 (Function_Call : Node_Id)
7619 Func_Call : Node_Id := Function_Call;
7620 Function_Id : Entity_Id;
7621 Result_Subt : Entity_Id;
7622 Return_Obj_Id : Entity_Id;
7623 Return_Obj_Decl : Entity_Id;
7626 -- True if result subtype is definite, or has a size that does not
7627 -- require secondary stack usage (i.e. no variant part or components
7628 -- whose type depends on discriminants). In particular, untagged types
7629 -- with only access discriminants do not require secondary stack use.
7630 -- Note that if the return type is tagged we must always use the sec.
7631 -- stack because the call may dispatch on result.
7634 -- Step past qualification, type conversion (which can occur in actual
7635 -- parameter contexts), and unchecked conversion (which can occur in
7636 -- cases of calls to 'Input).
7638 if Nkind_In (Func_Call, N_Qualified_Expression,
7640 N_Unchecked_Type_Conversion)
7642 Func_Call := Expression (Func_Call);
7645 -- If the call has already been processed to add build-in-place actuals
7646 -- then return. One place this can occur is for calls to build-in-place
7647 -- functions that occur within a call to a protected operation, where
7648 -- due to rewriting and expansion of the protected call there can be
7649 -- more than one call to Expand_Actuals for the same set of actuals.
7651 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7655 -- Mark the call as processed as a build-in-place call
7657 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7659 Loc := Sloc (Function_Call);
7661 if Is_Entity_Name (Name (Func_Call)) then
7662 Function_Id := Entity (Name (Func_Call));
7664 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7665 Function_Id := Etype (Name (Func_Call));
7668 raise Program_Error;
7671 Result_Subt := Etype (Function_Id);
7673 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
7674 and then not Is_Tagged_Type (Result_Subt))
7675 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
7677 -- If the build-in-place function returns a controlled object, then the
7678 -- object needs to be finalized immediately after the context. Since
7679 -- this case produces a transient scope, the servicing finalizer needs
7680 -- to name the returned object. Create a temporary which is initialized
7681 -- with the function call:
7683 -- Temp_Id : Func_Type := BIP_Func_Call;
7685 -- The initialization expression of the temporary will be rewritten by
7686 -- the expander using the appropriate mechanism in Make_Build_In_Place_
7687 -- Call_In_Object_Declaration.
7689 if Needs_Finalization (Result_Subt) then
7691 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
7692 Temp_Decl : Node_Id;
7695 -- Reset the guard on the function call since the following does
7696 -- not perform actual call expansion.
7698 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
7701 Make_Object_Declaration (Loc,
7702 Defining_Identifier => Temp_Id,
7703 Object_Definition =>
7704 New_Occurrence_Of (Result_Subt, Loc),
7706 New_Copy_Tree (Function_Call));
7708 Insert_Action (Function_Call, Temp_Decl);
7710 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
7711 Analyze (Function_Call);
7714 -- When the result subtype is definite, an object of the subtype is
7715 -- declared and an access value designating it is passed as an actual.
7719 -- Create a temporary object to hold the function result
7721 Return_Obj_Id := Make_Temporary (Loc, 'R');
7722 Set_Etype (Return_Obj_Id, Result_Subt);
7725 Make_Object_Declaration (Loc,
7726 Defining_Identifier => Return_Obj_Id,
7727 Aliased_Present => True,
7728 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
7730 Set_No_Initialization (Return_Obj_Decl);
7732 Insert_Action (Func_Call, Return_Obj_Decl);
7734 -- When the function has a controlling result, an allocation-form
7735 -- parameter must be passed indicating that the caller is allocating
7736 -- the result object. This is needed because such a function can be
7737 -- called as a dispatching operation and must be treated similarly
7738 -- to functions with unconstrained result subtypes.
7740 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7741 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7743 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7744 (Func_Call, Function_Id);
7746 Add_Task_Actuals_To_Build_In_Place_Call
7747 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7749 -- Add an implicit actual to the function call that provides access
7750 -- to the caller's return object.
7752 Add_Access_Actual_To_Build_In_Place_Call
7753 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
7755 -- When the result subtype is unconstrained, the function must allocate
7756 -- the return object in the secondary stack, so appropriate implicit
7757 -- parameters are added to the call to indicate that. A transient
7758 -- scope is established to ensure eventual cleanup of the result.
7761 -- Pass an allocation parameter indicating that the function should
7762 -- allocate its result on the secondary stack.
7764 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7765 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
7767 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7768 (Func_Call, Function_Id);
7770 Add_Task_Actuals_To_Build_In_Place_Call
7771 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7773 -- Pass a null value to the function since no return object is
7774 -- available on the caller side.
7776 Add_Access_Actual_To_Build_In_Place_Call
7777 (Func_Call, Function_Id, Empty);
7779 end Make_Build_In_Place_Call_In_Anonymous_Context;
7781 --------------------------------------------
7782 -- Make_Build_In_Place_Call_In_Assignment --
7783 --------------------------------------------
7785 procedure Make_Build_In_Place_Call_In_Assignment
7787 Function_Call : Node_Id)
7789 Lhs : constant Node_Id := Name (Assign);
7790 Func_Call : Node_Id := Function_Call;
7791 Func_Id : Entity_Id;
7795 Ptr_Typ : Entity_Id;
7796 Ptr_Typ_Decl : Node_Id;
7798 Result_Subt : Entity_Id;
7802 -- Step past qualification or unchecked conversion (the latter can occur
7803 -- in cases of calls to 'Input).
7805 if Nkind_In (Func_Call, N_Qualified_Expression,
7806 N_Unchecked_Type_Conversion)
7808 Func_Call := Expression (Func_Call);
7811 -- If the call has already been processed to add build-in-place actuals
7812 -- then return. This should not normally occur in an assignment context,
7813 -- but we add the protection as a defensive measure.
7815 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7819 -- Mark the call as processed as a build-in-place call
7821 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7823 Loc := Sloc (Function_Call);
7825 if Is_Entity_Name (Name (Func_Call)) then
7826 Func_Id := Entity (Name (Func_Call));
7828 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7829 Func_Id := Etype (Name (Func_Call));
7832 raise Program_Error;
7835 Result_Subt := Etype (Func_Id);
7837 -- When the result subtype is unconstrained, an additional actual must
7838 -- be passed to indicate that the caller is providing the return object.
7839 -- This parameter must also be passed when the called function has a
7840 -- controlling result, because dispatching calls to the function needs
7841 -- to be treated effectively the same as calls to class-wide functions.
7843 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7844 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
7846 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7847 (Func_Call, Func_Id);
7849 Add_Task_Actuals_To_Build_In_Place_Call
7850 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
7852 -- Add an implicit actual to the function call that provides access to
7853 -- the caller's return object.
7855 Add_Access_Actual_To_Build_In_Place_Call
7858 Make_Unchecked_Type_Conversion (Loc,
7859 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7860 Expression => Relocate_Node (Lhs)));
7862 -- Create an access type designating the function's result subtype
7864 Ptr_Typ := Make_Temporary (Loc, 'A');
7867 Make_Full_Type_Declaration (Loc,
7868 Defining_Identifier => Ptr_Typ,
7870 Make_Access_To_Object_Definition (Loc,
7871 All_Present => True,
7872 Subtype_Indication =>
7873 New_Occurrence_Of (Result_Subt, Loc)));
7874 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
7876 -- Finally, create an access object initialized to a reference to the
7877 -- function call. We know this access value is non-null, so mark the
7878 -- entity accordingly to suppress junk access checks.
7880 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
7882 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
7883 Set_Etype (Obj_Id, Ptr_Typ);
7884 Set_Is_Known_Non_Null (Obj_Id);
7887 Make_Object_Declaration (Loc,
7888 Defining_Identifier => Obj_Id,
7889 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
7890 Expression => New_Expr);
7891 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
7893 Rewrite (Assign, Make_Null_Statement (Loc));
7895 -- Retrieve the target of the assignment
7897 if Nkind (Lhs) = N_Selected_Component then
7898 Target := Selector_Name (Lhs);
7899 elsif Nkind (Lhs) = N_Type_Conversion then
7900 Target := Expression (Lhs);
7905 -- If we are assigning to a return object or this is an expression of
7906 -- an extension aggregate, the target should either be an identifier
7907 -- or a simple expression. All other cases imply a different scenario.
7909 if Nkind (Target) in N_Has_Entity then
7910 Target := Entity (Target);
7914 end Make_Build_In_Place_Call_In_Assignment;
7916 ----------------------------------------------------
7917 -- Make_Build_In_Place_Call_In_Object_Declaration --
7918 ----------------------------------------------------
7920 procedure Make_Build_In_Place_Call_In_Object_Declaration
7921 (Obj_Decl : Node_Id;
7922 Function_Call : Node_Id)
7924 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
7925 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
7926 Loc : constant Source_Ptr := Sloc (Function_Call);
7927 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
7929 Call_Deref : Node_Id;
7930 Caller_Object : Node_Id;
7932 Fmaster_Actual : Node_Id := Empty;
7933 Func_Call : Node_Id := Function_Call;
7934 Function_Id : Entity_Id;
7935 Pool_Actual : Node_Id;
7936 Ptr_Typ : Entity_Id;
7937 Ptr_Typ_Decl : Node_Id;
7938 Pass_Caller_Acc : Boolean := False;
7940 Result_Subt : Entity_Id;
7943 -- True if result subtype is definite, or has a size that does not
7944 -- require secondary stack usage (i.e. no variant part or components
7945 -- whose type depends on discriminants). In particular, untagged types
7946 -- with only access discriminants do not require secondary stack use.
7947 -- Note that if the return type is tagged we must always use the sec.
7948 -- stack because the call may dispatch on result.
7951 -- Step past qualification or unchecked conversion (the latter can occur
7952 -- in cases of calls to 'Input).
7954 if Nkind_In (Func_Call, N_Qualified_Expression,
7955 N_Unchecked_Type_Conversion)
7957 Func_Call := Expression (Func_Call);
7960 -- If the call has already been processed to add build-in-place actuals
7961 -- then return. This should not normally occur in an object declaration,
7962 -- but we add the protection as a defensive measure.
7964 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7968 -- Mark the call as processed as a build-in-place call
7970 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7972 if Is_Entity_Name (Name (Func_Call)) then
7973 Function_Id := Entity (Name (Func_Call));
7975 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7976 Function_Id := Etype (Name (Func_Call));
7979 raise Program_Error;
7982 Result_Subt := Etype (Function_Id);
7984 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
7985 and then not Is_Tagged_Type (Result_Subt))
7986 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
7988 -- Create an access type designating the function's result subtype. We
7989 -- use the type of the original call because it may be a call to an
7990 -- inherited operation, which the expansion has replaced with the parent
7991 -- operation that yields the parent type. Note that this access type
7992 -- must be declared before we establish a transient scope, so that it
7993 -- receives the proper accessibility level.
7995 Ptr_Typ := Make_Temporary (Loc, 'A');
7997 Make_Full_Type_Declaration (Loc,
7998 Defining_Identifier => Ptr_Typ,
8000 Make_Access_To_Object_Definition (Loc,
8001 All_Present => True,
8002 Subtype_Indication =>
8003 New_Occurrence_Of (Etype (Function_Call), Loc)));
8005 -- The access type and its accompanying object must be inserted after
8006 -- the object declaration in the constrained case, so that the function
8007 -- call can be passed access to the object. In the indefinite case,
8008 -- or if the object declaration is for a return object, the access type
8009 -- and object must be inserted before the object, since the object
8010 -- declaration is rewritten to be a renaming of a dereference of the
8011 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8012 -- the result object is in a different (transient) scope, so won't
8016 and then not Is_Return_Object (Defining_Identifier (Obj_Decl))
8018 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
8020 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
8023 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8024 -- elaborated in an inner (transient) scope and thus won't cause
8025 -- freezing by itself.
8028 Ptr_Typ_Freeze_Ref : constant Node_Id :=
8029 New_Occurrence_Of (Ptr_Typ, Loc);
8031 Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl);
8032 Freeze_Expression (Ptr_Typ_Freeze_Ref);
8035 -- If the object is a return object of an enclosing build-in-place
8036 -- function, then the implicit build-in-place parameters of the
8037 -- enclosing function are simply passed along to the called function.
8038 -- (Unfortunately, this won't cover the case of extension aggregates
8039 -- where the ancestor part is a build-in-place indefinite function
8040 -- call that should be passed along the caller's parameters. Currently
8041 -- those get mishandled by reassigning the result of the call to the
8042 -- aggregate return object, when the call result should really be
8043 -- directly built in place in the aggregate and not in a temporary. ???)
8045 if Is_Return_Object (Defining_Identifier (Obj_Decl)) then
8046 Pass_Caller_Acc := True;
8048 -- When the enclosing function has a BIP_Alloc_Form formal then we
8049 -- pass it along to the callee (such as when the enclosing function
8050 -- has an unconstrained or tagged result type).
8052 if Needs_BIP_Alloc_Form (Encl_Func) then
8053 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8056 (Build_In_Place_Formal (Encl_Func, BIP_Storage_Pool), Loc);
8058 -- The build-in-place pool formal is not built on e.g. ZFP
8061 Pool_Actual := Empty;
8064 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8065 (Function_Call => Func_Call,
8066 Function_Id => Function_Id,
8069 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
8070 Pool_Actual => Pool_Actual);
8072 -- Otherwise, if enclosing function has a definite result subtype,
8073 -- then caller allocation will be used.
8076 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8077 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8080 if Needs_BIP_Finalization_Master (Encl_Func) then
8083 (Build_In_Place_Formal
8084 (Encl_Func, BIP_Finalization_Master), Loc);
8087 -- Retrieve the BIPacc formal from the enclosing function and convert
8088 -- it to the access type of the callee's BIP_Object_Access formal.
8091 Make_Unchecked_Type_Conversion (Loc,
8095 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
8099 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access),
8102 -- In the definite case, add an implicit actual to the function call
8103 -- that provides access to the declared object. An unchecked conversion
8104 -- to the (specific) result type of the function is inserted to handle
8105 -- the case where the object is declared with a class-wide type.
8109 Make_Unchecked_Type_Conversion (Loc,
8110 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8111 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
8113 -- When the function has a controlling result, an allocation-form
8114 -- parameter must be passed indicating that the caller is allocating
8115 -- the result object. This is needed because such a function can be
8116 -- called as a dispatching operation and must be treated similarly
8117 -- to functions with indefinite result subtypes.
8119 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8120 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8122 -- The allocation for indefinite library-level objects occurs on the
8123 -- heap as opposed to the secondary stack. This accommodates DLLs where
8124 -- the secondary stack is destroyed after each library unload. This is
8125 -- a hybrid mechanism where a stack-allocated object lives on the heap.
8127 elsif Is_Library_Level_Entity (Defining_Identifier (Obj_Decl))
8128 and then not Restriction_Active (No_Implicit_Heap_Allocations)
8130 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8131 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
8132 Caller_Object := Empty;
8134 -- Create a finalization master for the access result type to ensure
8135 -- that the heap allocation can properly chain the object and later
8136 -- finalize it when the library unit goes out of scope.
8138 if Needs_Finalization (Etype (Func_Call)) then
8139 Build_Finalization_Master
8141 For_Lib_Level => True,
8142 Insertion_Node => Ptr_Typ_Decl);
8145 Make_Attribute_Reference (Loc,
8147 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
8148 Attribute_Name => Name_Unrestricted_Access);
8151 -- In other indefinite cases, pass an indication to do the allocation
8152 -- on the secondary stack and set Caller_Object to Empty so that a null
8153 -- value will be passed for the caller's object address. A transient
8154 -- scope is established to ensure eventual cleanup of the result.
8157 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8158 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8159 Caller_Object := Empty;
8161 Establish_Transient_Scope (Obj_Decl, Sec_Stack => True);
8164 -- Pass along any finalization master actual, which is needed in the
8165 -- case where the called function initializes a return object of an
8166 -- enclosing build-in-place function.
8168 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8169 (Func_Call => Func_Call,
8170 Func_Id => Function_Id,
8171 Master_Exp => Fmaster_Actual);
8173 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
8174 and then Has_Task (Result_Subt)
8176 -- Here we're passing along the master that was passed in to this
8179 Add_Task_Actuals_To_Build_In_Place_Call
8180 (Func_Call, Function_Id,
8183 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
8186 Add_Task_Actuals_To_Build_In_Place_Call
8187 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8190 Add_Access_Actual_To_Build_In_Place_Call
8191 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
8193 -- Finally, create an access object initialized to a reference to the
8194 -- function call. We know this access value cannot be null, so mark the
8195 -- entity accordingly to suppress the access check.
8197 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
8198 Set_Etype (Def_Id, Ptr_Typ);
8199 Set_Is_Known_Non_Null (Def_Id);
8202 Make_Object_Declaration (Loc,
8203 Defining_Identifier => Def_Id,
8204 Constant_Present => True,
8205 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8207 Make_Reference (Loc, Relocate_Node (Func_Call)));
8209 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
8211 -- If the result subtype of the called function is definite and is not
8212 -- itself the return expression of an enclosing BIP function, then mark
8213 -- the object as having no initialization.
8216 and then not Is_Return_Object (Defining_Identifier (Obj_Decl))
8218 -- The related object declaration is encased in a transient block
8219 -- because the build-in-place function call contains at least one
8220 -- nested function call that produces a controlled transient
8223 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8225 -- Since the build-in-place expansion decouples the call from the
8226 -- object declaration, the finalization machinery lacks the context
8227 -- which prompted the generation of the transient block. To resolve
8228 -- this scenario, store the build-in-place call.
8230 if Scope_Is_Transient and then Node_To_Be_Wrapped = Obj_Decl then
8231 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
8234 Set_Expression (Obj_Decl, Empty);
8235 Set_No_Initialization (Obj_Decl);
8237 -- In case of an indefinite result subtype, or if the call is the
8238 -- return expression of an enclosing BIP function, rewrite the object
8239 -- declaration as an object renaming where the renamed object is a
8240 -- dereference of <function_Call>'reference:
8242 -- Obj : Subt renames <function_call>'Ref.all;
8246 Make_Explicit_Dereference (Obj_Loc,
8247 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
8250 Make_Object_Renaming_Declaration (Obj_Loc,
8251 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
8252 Subtype_Mark => New_Occurrence_Of (Result_Subt, Obj_Loc),
8253 Name => Call_Deref));
8255 Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref);
8257 -- If the original entity comes from source, then mark the new
8258 -- entity as needing debug information, even though it's defined
8259 -- by a generated renaming that does not come from source, so that
8260 -- the Materialize_Entity flag will be set on the entity when
8261 -- Debug_Renaming_Declaration is called during analysis.
8263 if Comes_From_Source (Obj_Def_Id) then
8264 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
8269 -- Replace the internal identifier of the renaming declaration's
8270 -- entity with identifier of the original object entity. We also have
8271 -- to exchange the entities containing their defining identifiers to
8272 -- ensure the correct replacement of the object declaration by the
8273 -- object renaming declaration to avoid homograph conflicts (since
8274 -- the object declaration's defining identifier was already entered
8275 -- in current scope). The Next_Entity links of the two entities also
8276 -- have to be swapped since the entities are part of the return
8277 -- scope's entity list and the list structure would otherwise be
8278 -- corrupted. Finally, the homonym chain must be preserved as well.
8281 Ren_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
8282 Next_Id : constant Entity_Id := Next_Entity (Ren_Id);
8285 Set_Chars (Ren_Id, Chars (Obj_Def_Id));
8287 -- Swap next entity links in preparation for exchanging entities
8289 Set_Next_Entity (Ren_Id, Next_Entity (Obj_Def_Id));
8290 Set_Next_Entity (Obj_Def_Id, Next_Id);
8291 Set_Homonym (Ren_Id, Homonym (Obj_Def_Id));
8293 Exchange_Entities (Ren_Id, Obj_Def_Id);
8295 -- Preserve source indication of original declaration, so that
8296 -- xref information is properly generated for the right entity.
8298 Preserve_Comes_From_Source (Obj_Decl, Original_Node (Obj_Decl));
8299 Preserve_Comes_From_Source (Obj_Def_Id, Original_Node (Obj_Decl));
8301 Set_Comes_From_Source (Ren_Id, False);
8305 -- If the object entity has a class-wide Etype, then we need to change
8306 -- it to the result subtype of the function call, because otherwise the
8307 -- object will be class-wide without an explicit initialization and
8308 -- won't be allocated properly by the back end. It seems unclean to make
8309 -- such a revision to the type at this point, and we should try to
8310 -- improve this treatment when build-in-place functions with class-wide
8311 -- results are implemented. ???
8313 if Is_Class_Wide_Type (Etype (Defining_Identifier (Obj_Decl))) then
8314 Set_Etype (Defining_Identifier (Obj_Decl), Result_Subt);
8316 end Make_Build_In_Place_Call_In_Object_Declaration;
8318 --------------------------------------------
8319 -- Make_CPP_Constructor_Call_In_Allocator --
8320 --------------------------------------------
8322 procedure Make_CPP_Constructor_Call_In_Allocator
8323 (Allocator : Node_Id;
8324 Function_Call : Node_Id)
8326 Loc : constant Source_Ptr := Sloc (Function_Call);
8327 Acc_Type : constant Entity_Id := Etype (Allocator);
8328 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
8329 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
8331 New_Allocator : Node_Id;
8332 Return_Obj_Access : Entity_Id;
8336 pragma Assert (Nkind (Allocator) = N_Allocator
8337 and then Nkind (Function_Call) = N_Function_Call);
8338 pragma Assert (Convention (Function_Id) = Convention_CPP
8339 and then Is_Constructor (Function_Id));
8340 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
8342 -- Replace the initialized allocator of form "new T'(Func (...))" with
8343 -- an uninitialized allocator of form "new T", where T is the result
8344 -- subtype of the called function. The call to the function is handled
8345 -- separately further below.
8348 Make_Allocator (Loc,
8349 Expression => New_Occurrence_Of (Result_Subt, Loc));
8350 Set_No_Initialization (New_Allocator);
8352 -- Copy attributes to new allocator. Note that the new allocator
8353 -- logically comes from source if the original one did, so copy the
8354 -- relevant flag. This ensures proper treatment of the restriction
8355 -- No_Implicit_Heap_Allocations in this case.
8357 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8358 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8359 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8361 Rewrite (Allocator, New_Allocator);
8363 -- Create a new access object and initialize it to the result of the
8364 -- new uninitialized allocator. Note: we do not use Allocator as the
8365 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
8366 -- as this would create a sort of infinite "recursion".
8368 Return_Obj_Access := Make_Temporary (Loc, 'R');
8369 Set_Etype (Return_Obj_Access, Acc_Type);
8372 -- Rnnn : constant ptr_T := new (T);
8373 -- Init (Rnn.all,...);
8376 Make_Object_Declaration (Loc,
8377 Defining_Identifier => Return_Obj_Access,
8378 Constant_Present => True,
8379 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8380 Expression => Relocate_Node (Allocator));
8381 Insert_Action (Allocator, Tmp_Obj);
8383 Insert_List_After_And_Analyze (Tmp_Obj,
8384 Build_Initialization_Call (Loc,
8386 Make_Explicit_Dereference (Loc,
8387 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
8388 Typ => Etype (Function_Id),
8389 Constructor_Ref => Function_Call));
8391 -- Finally, replace the allocator node with a reference to the result of
8392 -- the function call itself (which will effectively be an access to the
8393 -- object created by the allocator).
8395 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8397 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
8398 -- generate an implicit conversion to force displacement of the "this"
8401 if Is_Interface (Designated_Type (Acc_Type)) then
8402 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
8405 Analyze_And_Resolve (Allocator, Acc_Type);
8406 end Make_CPP_Constructor_Call_In_Allocator;
8408 -----------------------------------
8409 -- Needs_BIP_Finalization_Master --
8410 -----------------------------------
8412 function Needs_BIP_Finalization_Master
8413 (Func_Id : Entity_Id) return Boolean
8415 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8416 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8418 -- A formal giving the finalization master is needed for build-in-place
8419 -- functions whose result type needs finalization or is a tagged type.
8420 -- Tagged primitive build-in-place functions need such a formal because
8421 -- they can be called by a dispatching call, and extensions may require
8422 -- finalization even if the root type doesn't. This means they're also
8423 -- needed for tagged nonprimitive build-in-place functions with tagged
8424 -- results, since such functions can be called via access-to-function
8425 -- types, and those can be used to call primitives, so masters have to
8426 -- be passed to all such build-in-place functions, primitive or not.
8429 not Restriction_Active (No_Finalization)
8430 and then (Needs_Finalization (Func_Typ)
8431 or else Is_Tagged_Type (Func_Typ));
8432 end Needs_BIP_Finalization_Master;
8434 --------------------------
8435 -- Needs_BIP_Alloc_Form --
8436 --------------------------
8438 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
8439 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8440 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8442 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
8443 end Needs_BIP_Alloc_Form;
8445 --------------------------------------
8446 -- Needs_Result_Accessibility_Level --
8447 --------------------------------------
8449 function Needs_Result_Accessibility_Level
8450 (Func_Id : Entity_Id) return Boolean
8452 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8454 function Has_Unconstrained_Access_Discriminant_Component
8455 (Comp_Typ : Entity_Id) return Boolean;
8456 -- Returns True if any component of the type has an unconstrained access
8459 -----------------------------------------------------
8460 -- Has_Unconstrained_Access_Discriminant_Component --
8461 -----------------------------------------------------
8463 function Has_Unconstrained_Access_Discriminant_Component
8464 (Comp_Typ : Entity_Id) return Boolean
8467 if not Is_Limited_Type (Comp_Typ) then
8470 -- Only limited types can have access discriminants with
8473 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
8476 elsif Is_Array_Type (Comp_Typ) then
8477 return Has_Unconstrained_Access_Discriminant_Component
8478 (Underlying_Type (Component_Type (Comp_Typ)));
8480 elsif Is_Record_Type (Comp_Typ) then
8485 Comp := First_Component (Comp_Typ);
8486 while Present (Comp) loop
8487 if Has_Unconstrained_Access_Discriminant_Component
8488 (Underlying_Type (Etype (Comp)))
8493 Next_Component (Comp);
8499 end Has_Unconstrained_Access_Discriminant_Component;
8501 Feature_Disabled : constant Boolean := True;
8504 -- Start of processing for Needs_Result_Accessibility_Level
8507 -- False if completion unavailable (how does this happen???)
8509 if not Present (Func_Typ) then
8512 elsif Feature_Disabled then
8515 -- False if not a function, also handle enum-lit renames case
8517 elsif Func_Typ = Standard_Void_Type
8518 or else Is_Scalar_Type (Func_Typ)
8522 -- Handle a corner case, a cross-dialect subp renaming. For example,
8523 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
8524 -- an Ada 2005 (or earlier) unit references predefined run-time units.
8526 elsif Present (Alias (Func_Id)) then
8528 -- Unimplemented: a cross-dialect subp renaming which does not set
8529 -- the Alias attribute (e.g., a rename of a dereference of an access
8530 -- to subprogram value). ???
8532 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
8534 -- Remaining cases require Ada 2012 mode
8536 elsif Ada_Version < Ada_2012 then
8539 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
8540 or else Is_Tagged_Type (Func_Typ)
8542 -- In the case of, say, a null tagged record result type, the need
8543 -- for this extra parameter might not be obvious. This function
8544 -- returns True for all tagged types for compatibility reasons.
8545 -- A function with, say, a tagged null controlling result type might
8546 -- be overridden by a primitive of an extension having an access
8547 -- discriminant and the overrider and overridden must have compatible
8548 -- calling conventions (including implicitly declared parameters).
8549 -- Similarly, values of one access-to-subprogram type might designate
8550 -- both a primitive subprogram of a given type and a function
8551 -- which is, for example, not a primitive subprogram of any type.
8552 -- Again, this requires calling convention compatibility.
8553 -- It might be possible to solve these issues by introducing
8554 -- wrappers, but that is not the approach that was chosen.
8558 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
8561 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
8564 -- False for all other cases
8569 end Needs_Result_Accessibility_Level;
8571 ---------------------------------
8572 -- Rewrite_Function_Call_For_C --
8573 ---------------------------------
8575 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
8576 Orig_Func : constant Entity_Id := Entity (Name (N));
8577 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
8578 Par : constant Node_Id := Parent (N);
8579 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
8580 Loc : constant Source_Ptr := Sloc (Par);
8582 Last_Actual : Node_Id;
8583 Last_Formal : Entity_Id;
8585 -- Start of processing for Rewrite_Function_Call_For_C
8588 -- The actuals may be given by named associations, so the added actual
8589 -- that is the target of the return value of the call must be a named
8590 -- association as well, so we retrieve the name of the generated
8593 Last_Formal := First_Formal (Proc_Id);
8594 while Present (Next_Formal (Last_Formal)) loop
8595 Last_Formal := Next_Formal (Last_Formal);
8598 Actuals := Parameter_Associations (N);
8600 -- The original function may lack parameters
8602 if No (Actuals) then
8603 Actuals := New_List;
8606 -- If the function call is the expression of an assignment statement,
8607 -- transform the assignment into a procedure call. Generate:
8609 -- LHS := Func_Call (...);
8611 -- Proc_Call (..., LHS);
8613 -- If function is inherited, a conversion may be necessary.
8615 if Nkind (Par) = N_Assignment_Statement then
8616 Last_Actual := Name (Par);
8618 if not Comes_From_Source (Orig_Func)
8619 and then Etype (Orig_Func) /= Etype (Func_Id)
8622 Make_Type_Conversion (Loc,
8623 New_Occurrence_Of (Etype (Func_Id), Loc),
8628 Make_Parameter_Association (Loc,
8630 Make_Identifier (Loc, Chars (Last_Formal)),
8631 Explicit_Actual_Parameter => Last_Actual));
8634 Make_Procedure_Call_Statement (Loc,
8635 Name => New_Occurrence_Of (Proc_Id, Loc),
8636 Parameter_Associations => Actuals));
8639 -- Otherwise the context is an expression. Generate a temporary and a
8640 -- procedure call to obtain the function result. Generate:
8642 -- ... Func_Call (...) ...
8645 -- Proc_Call (..., Temp);
8650 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
8659 Make_Object_Declaration (Loc,
8660 Defining_Identifier => Temp_Id,
8661 Object_Definition =>
8662 New_Occurrence_Of (Etype (Func_Id), Loc));
8665 -- Proc_Call (..., Temp);
8668 Make_Parameter_Association (Loc,
8670 Make_Identifier (Loc, Chars (Last_Formal)),
8671 Explicit_Actual_Parameter =>
8672 New_Occurrence_Of (Temp_Id, Loc)));
8675 Make_Procedure_Call_Statement (Loc,
8676 Name => New_Occurrence_Of (Proc_Id, Loc),
8677 Parameter_Associations => Actuals);
8679 Insert_Actions (Par, New_List (Decl, Call));
8680 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
8683 end Rewrite_Function_Call_For_C;
8685 ------------------------------------
8686 -- Set_Enclosing_Sec_Stack_Return --
8687 ------------------------------------
8689 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
8693 -- Due to a possible mix of internally generated blocks, source blocks
8694 -- and loops, the scope stack may not be contiguous as all labels are
8695 -- inserted at the top level within the related function. Instead,
8696 -- perform a parent-based traversal and mark all appropriate constructs.
8698 while Present (P) loop
8700 -- Mark the label of a source or internally generated block or
8703 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
8704 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
8706 -- Mark the enclosing function
8708 elsif Nkind (P) = N_Subprogram_Body then
8709 if Present (Corresponding_Spec (P)) then
8710 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
8712 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
8715 -- Do not go beyond the enclosing function
8722 end Set_Enclosing_Sec_Stack_Return;