1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Elists; use Elists;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch2; use Exp_Ch2;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Intr; use Exp_Intr;
41 with Exp_Pakd; use Exp_Pakd;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Exp_VFpt; use Exp_VFpt;
45 with Fname; use Fname;
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_Eval; use Sem_Eval;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Mech; use Sem_Mech;
66 with Sem_Res; use Sem_Res;
67 with Sem_SCIL; use Sem_SCIL;
68 with Sem_Util; use Sem_Util;
69 with Sinfo; use Sinfo;
70 with Snames; use Snames;
71 with Stand; use Stand;
72 with Targparm; use Targparm;
73 with Tbuild; use Tbuild;
74 with Uintp; use Uintp;
75 with Validsw; use Validsw;
77 package body Exp_Ch6 is
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 procedure Add_Access_Actual_To_Build_In_Place_Call
84 (Function_Call : Node_Id;
85 Function_Id : Entity_Id;
86 Return_Object : Node_Id;
87 Is_Access : Boolean := False);
88 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
89 -- object name given by Return_Object and add the attribute to the end of
90 -- the actual parameter list associated with the build-in-place function
91 -- call denoted by Function_Call. However, if Is_Access is True, then
92 -- Return_Object is already an access expression, in which case it's passed
93 -- along directly to the build-in-place function. Finally, if Return_Object
94 -- is empty, then pass a null literal as the actual.
96 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
97 (Function_Call : Node_Id;
98 Function_Id : Entity_Id;
99 Alloc_Form : BIP_Allocation_Form := Unspecified;
100 Alloc_Form_Exp : Node_Id := Empty);
101 -- Ada 2005 (AI-318-02): Add an actual indicating the form of allocation,
102 -- if any, to be done by a build-in-place function. If Alloc_Form_Exp is
103 -- present, then use it, otherwise pass a literal corresponding to the
104 -- Alloc_Form parameter (which must not be Unspecified in that case).
106 procedure Add_Extra_Actual_To_Call
107 (Subprogram_Call : Node_Id;
108 Extra_Formal : Entity_Id;
109 Extra_Actual : Node_Id);
110 -- Adds Extra_Actual as a named parameter association for the formal
111 -- Extra_Formal in Subprogram_Call.
113 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
114 (Func_Call : Node_Id;
116 Ptr_Typ : Entity_Id := Empty);
117 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
118 -- finalization actions, add an actual parameter which is a pointer to the
119 -- finalization master of the caller. If Ptr_Typ is left Empty, this will
120 -- result in an automatic "null" value for the actual.
122 procedure Add_Task_Actuals_To_Build_In_Place_Call
123 (Function_Call : Node_Id;
124 Function_Id : Entity_Id;
125 Master_Actual : Node_Id);
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. The
134 -- activation chain to pass is always the local one. Note: Master_Actual
135 -- can be Empty, but only if there are no tasks.
137 procedure Check_Overriding_Operation (Subp : Entity_Id);
138 -- Subp is a dispatching operation. Check whether it may override an
139 -- inherited private operation, in which case its DT entry is that of
140 -- the hidden operation, not the one it may have received earlier.
141 -- This must be done before emitting the code to set the corresponding
142 -- DT to the address of the subprogram. The actual placement of Subp in
143 -- the proper place in the list of primitive operations is done in
144 -- Declare_Inherited_Private_Subprograms, which also has to deal with
145 -- implicit operations. This duplication is unavoidable for now???
147 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
148 -- This procedure is called only if the subprogram body N, whose spec
149 -- has the given entity Spec, contains a parameterless recursive call.
150 -- It attempts to generate runtime code to detect if this a case of
151 -- infinite recursion.
153 -- The body is scanned to determine dependencies. If the only external
154 -- dependencies are on a small set of scalar variables, then the values
155 -- of these variables are captured on entry to the subprogram, and if
156 -- the values are not changed for the call, we know immediately that
157 -- we have an infinite recursion.
159 procedure Expand_Ctrl_Function_Call (N : Node_Id);
160 -- N is a function call which returns a controlled object. Transform the
161 -- call into a temporary which retrieves the returned object from the
162 -- secondary stack using 'reference.
164 procedure Expand_Inlined_Call
167 Orig_Subp : Entity_Id);
168 -- If called subprogram can be inlined by the front-end, retrieve the
169 -- analyzed body, replace formals with actuals and expand call in place.
170 -- Generate thunks for actuals that are expressions, and insert the
171 -- corresponding constant declarations before the call. If the original
172 -- call is to a derived operation, the return type is the one of the
173 -- derived operation, but the body is that of the original, so return
174 -- expressions in the body must be converted to the desired type (which
175 -- is simply not noted in the tree without inline expansion).
177 procedure Expand_Non_Function_Return (N : Node_Id);
178 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
179 -- a procedure body, entry body, accept statement, or extended return
180 -- statement. Note that all non-function returns are simple return
183 function Expand_Protected_Object_Reference
185 Scop : Entity_Id) return Node_Id;
187 procedure Expand_Protected_Subprogram_Call
191 -- A call to a protected subprogram within the protected object may appear
192 -- as a regular call. The list of actuals must be expanded to contain a
193 -- reference to the object itself, and the call becomes a call to the
194 -- corresponding protected subprogram.
196 procedure Expand_Simple_Function_Return (N : Node_Id);
197 -- Expand simple return from function. In the case where we are returning
198 -- from a function body this is called by Expand_N_Simple_Return_Statement.
200 ----------------------------------------------
201 -- Add_Access_Actual_To_Build_In_Place_Call --
202 ----------------------------------------------
204 procedure Add_Access_Actual_To_Build_In_Place_Call
205 (Function_Call : Node_Id;
206 Function_Id : Entity_Id;
207 Return_Object : Node_Id;
208 Is_Access : Boolean := False)
210 Loc : constant Source_Ptr := Sloc (Function_Call);
211 Obj_Address : Node_Id;
212 Obj_Acc_Formal : Entity_Id;
215 -- Locate the implicit access parameter in the called function
217 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
219 -- If no return object is provided, then pass null
221 if not Present (Return_Object) then
222 Obj_Address := Make_Null (Loc);
223 Set_Parent (Obj_Address, Function_Call);
225 -- If Return_Object is already an expression of an access type, then use
226 -- it directly, since it must be an access value denoting the return
227 -- object, and couldn't possibly be the return object itself.
230 Obj_Address := Return_Object;
231 Set_Parent (Obj_Address, Function_Call);
233 -- Apply Unrestricted_Access to caller's return object
237 Make_Attribute_Reference (Loc,
238 Prefix => Return_Object,
239 Attribute_Name => Name_Unrestricted_Access);
241 Set_Parent (Return_Object, Obj_Address);
242 Set_Parent (Obj_Address, Function_Call);
245 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
247 -- Build the parameter association for the new actual and add it to the
248 -- end of the function's actuals.
250 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
251 end Add_Access_Actual_To_Build_In_Place_Call;
253 --------------------------------------------------
254 -- Add_Alloc_Form_Actual_To_Build_In_Place_Call --
255 --------------------------------------------------
257 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
258 (Function_Call : Node_Id;
259 Function_Id : Entity_Id;
260 Alloc_Form : BIP_Allocation_Form := Unspecified;
261 Alloc_Form_Exp : Node_Id := Empty)
263 Loc : constant Source_Ptr := Sloc (Function_Call);
264 Alloc_Form_Actual : Node_Id;
265 Alloc_Form_Formal : Node_Id;
268 -- The allocation form generally doesn't need to be passed in the case
269 -- of a constrained result subtype, since normally the caller performs
270 -- the allocation in that case. However this formal is still needed in
271 -- the case where the function has a tagged result, because generally
272 -- such functions can be called in a dispatching context and such calls
273 -- must be handled like calls to class-wide functions.
275 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
276 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
281 -- Locate the implicit allocation form parameter in the called function.
282 -- Maybe it would be better for each implicit formal of a build-in-place
283 -- function to have a flag or a Uint attribute to identify it. ???
285 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
287 if Present (Alloc_Form_Exp) then
288 pragma Assert (Alloc_Form = Unspecified);
290 Alloc_Form_Actual := Alloc_Form_Exp;
293 pragma Assert (Alloc_Form /= Unspecified);
296 Make_Integer_Literal (Loc,
297 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
300 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
302 -- Build the parameter association for the new actual and add it to the
303 -- end of the function's actuals.
305 Add_Extra_Actual_To_Call
306 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
307 end Add_Alloc_Form_Actual_To_Build_In_Place_Call;
309 -----------------------------------------------------------
310 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
311 -----------------------------------------------------------
313 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
314 (Func_Call : Node_Id;
316 Ptr_Typ : Entity_Id := Empty)
319 if not Needs_BIP_Finalization_Master (Func_Id) then
324 Formal : constant Entity_Id :=
325 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
326 Loc : constant Source_Ptr := Sloc (Func_Call);
329 Desig_Typ : Entity_Id;
332 -- Case where the context does not require an actual master
335 Actual := Make_Null (Loc);
338 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
340 -- Check for a library-level access type whose designated type has
341 -- supressed finalization. Such an access types lack a master.
342 -- Pass a null actual to the callee in order to signal a missing
345 if Is_Library_Level_Entity (Ptr_Typ)
346 and then Finalize_Storage_Only (Desig_Typ)
348 Actual := Make_Null (Loc);
350 -- Types in need of finalization actions
352 elsif Needs_Finalization (Desig_Typ) then
354 -- The general mechanism of creating finalization masters for
355 -- anonymous access types is disabled by default, otherwise
356 -- finalization masters will pop all over the place. Such types
357 -- use context-specific masters.
359 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
360 and then No (Finalization_Master (Ptr_Typ))
362 Build_Finalization_Master
364 Ins_Node => Associated_Node_For_Itype (Ptr_Typ),
365 Encl_Scope => Scope (Ptr_Typ));
368 -- Access-to-controlled types should always have a master
370 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
373 Make_Attribute_Reference (Loc,
375 New_Reference_To (Finalization_Master (Ptr_Typ), Loc),
376 Attribute_Name => Name_Unrestricted_Access);
381 Actual := Make_Null (Loc);
385 Analyze_And_Resolve (Actual, Etype (Formal));
387 -- Build the parameter association for the new actual and add it to
388 -- the end of the function's actuals.
390 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
392 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
394 ------------------------------
395 -- Add_Extra_Actual_To_Call --
396 ------------------------------
398 procedure Add_Extra_Actual_To_Call
399 (Subprogram_Call : Node_Id;
400 Extra_Formal : Entity_Id;
401 Extra_Actual : Node_Id)
403 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
404 Param_Assoc : Node_Id;
408 Make_Parameter_Association (Loc,
409 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
410 Explicit_Actual_Parameter => Extra_Actual);
412 Set_Parent (Param_Assoc, Subprogram_Call);
413 Set_Parent (Extra_Actual, Param_Assoc);
415 if Present (Parameter_Associations (Subprogram_Call)) then
416 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
417 N_Parameter_Association
420 -- Find last named actual, and append
425 L := First_Actual (Subprogram_Call);
426 while Present (L) loop
427 if No (Next_Actual (L)) then
428 Set_Next_Named_Actual (Parent (L), Extra_Actual);
436 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
439 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
442 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
443 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
445 end Add_Extra_Actual_To_Call;
447 ---------------------------------------------
448 -- Add_Task_Actuals_To_Build_In_Place_Call --
449 ---------------------------------------------
451 procedure Add_Task_Actuals_To_Build_In_Place_Call
452 (Function_Call : Node_Id;
453 Function_Id : Entity_Id;
454 Master_Actual : Node_Id)
456 Loc : constant Source_Ptr := Sloc (Function_Call);
457 Actual : Node_Id := Master_Actual;
460 -- No such extra parameters are needed if there are no tasks
462 if not Has_Task (Etype (Function_Id)) then
466 -- Use a dummy _master actual in case of No_Task_Hierarchy
468 if Restriction_Active (No_Task_Hierarchy) then
469 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
475 Master_Formal : Node_Id;
477 -- Locate implicit master parameter in the called function
479 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Master);
481 Analyze_And_Resolve (Actual, Etype (Master_Formal));
483 -- Build the parameter association for the new actual and add it to
484 -- the end of the function's actuals.
486 Add_Extra_Actual_To_Call
487 (Function_Call, Master_Formal, Actual);
490 -- The activation chain
493 Activation_Chain_Actual : Node_Id;
494 Activation_Chain_Formal : Node_Id;
497 -- Locate implicit activation chain parameter in the called function
499 Activation_Chain_Formal := Build_In_Place_Formal
500 (Function_Id, BIP_Activation_Chain);
502 -- Create the actual which is a pointer to the current activation
505 Activation_Chain_Actual :=
506 Make_Attribute_Reference (Loc,
507 Prefix => Make_Identifier (Loc, Name_uChain),
508 Attribute_Name => Name_Unrestricted_Access);
511 (Activation_Chain_Actual, Etype (Activation_Chain_Formal));
513 -- Build the parameter association for the new actual and add it to
514 -- the end of the function's actuals.
516 Add_Extra_Actual_To_Call
517 (Function_Call, Activation_Chain_Formal, Activation_Chain_Actual);
519 end Add_Task_Actuals_To_Build_In_Place_Call;
521 -----------------------
522 -- BIP_Formal_Suffix --
523 -----------------------
525 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
528 when BIP_Alloc_Form =>
530 when BIP_Finalization_Master =>
531 return "BIPfinalizationmaster";
534 when BIP_Activation_Chain =>
535 return "BIPactivationchain";
536 when BIP_Object_Access =>
539 end BIP_Formal_Suffix;
541 ---------------------------
542 -- Build_In_Place_Formal --
543 ---------------------------
545 function Build_In_Place_Formal
547 Kind : BIP_Formal_Kind) return Entity_Id
549 Extra_Formal : Entity_Id := Extra_Formals (Func);
552 -- Maybe it would be better for each implicit formal of a build-in-place
553 -- function to have a flag or a Uint attribute to identify it. ???
556 pragma Assert (Present (Extra_Formal));
558 Chars (Extra_Formal) =
559 New_External_Name (Chars (Func), BIP_Formal_Suffix (Kind));
560 Next_Formal_With_Extras (Extra_Formal);
564 end Build_In_Place_Formal;
566 --------------------------------
567 -- Check_Overriding_Operation --
568 --------------------------------
570 procedure Check_Overriding_Operation (Subp : Entity_Id) is
571 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
572 Op_List : constant Elist_Id := Primitive_Operations (Typ);
578 if Is_Derived_Type (Typ)
579 and then not Is_Private_Type (Typ)
580 and then In_Open_Scopes (Scope (Etype (Typ)))
581 and then Is_Base_Type (Typ)
583 -- Subp overrides an inherited private operation if there is an
584 -- inherited operation with a different name than Subp (see
585 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
586 -- same name as Subp.
588 Op_Elmt := First_Elmt (Op_List);
589 while Present (Op_Elmt) loop
590 Prim_Op := Node (Op_Elmt);
591 Par_Op := Alias (Prim_Op);
594 and then not Comes_From_Source (Prim_Op)
595 and then Chars (Prim_Op) /= Chars (Par_Op)
596 and then Chars (Par_Op) = Chars (Subp)
597 and then Is_Hidden (Par_Op)
598 and then Type_Conformant (Prim_Op, Subp)
600 Set_DT_Position (Subp, DT_Position (Prim_Op));
606 end Check_Overriding_Operation;
608 -------------------------------
609 -- Detect_Infinite_Recursion --
610 -------------------------------
612 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
613 Loc : constant Source_Ptr := Sloc (N);
615 Var_List : constant Elist_Id := New_Elmt_List;
616 -- List of globals referenced by body of procedure
618 Call_List : constant Elist_Id := New_Elmt_List;
619 -- List of recursive calls in body of procedure
621 Shad_List : constant Elist_Id := New_Elmt_List;
622 -- List of entity id's for entities created to capture the value of
623 -- referenced globals on entry to the procedure.
625 Scop : constant Uint := Scope_Depth (Spec);
626 -- This is used to record the scope depth of the current procedure, so
627 -- that we can identify global references.
629 Max_Vars : constant := 4;
630 -- Do not test more than four global variables
632 Count_Vars : Natural := 0;
633 -- Count variables found so far
645 function Process (Nod : Node_Id) return Traverse_Result;
646 -- Function to traverse the subprogram body (using Traverse_Func)
652 function Process (Nod : Node_Id) return Traverse_Result is
656 if Nkind (Nod) = N_Procedure_Call_Statement then
658 -- Case of one of the detected recursive calls
660 if Is_Entity_Name (Name (Nod))
661 and then Has_Recursive_Call (Entity (Name (Nod)))
662 and then Entity (Name (Nod)) = Spec
664 Append_Elmt (Nod, Call_List);
667 -- Any other procedure call may have side effects
673 -- A call to a pure function can always be ignored
675 elsif Nkind (Nod) = N_Function_Call
676 and then Is_Entity_Name (Name (Nod))
677 and then Is_Pure (Entity (Name (Nod)))
681 -- Case of an identifier reference
683 elsif Nkind (Nod) = N_Identifier then
686 -- If no entity, then ignore the reference
688 -- Not clear why this can happen. To investigate, remove this
689 -- test and look at the crash that occurs here in 3401-004 ???
694 -- Ignore entities with no Scope, again not clear how this
695 -- can happen, to investigate, look at 4108-008 ???
697 elsif No (Scope (Ent)) then
700 -- Ignore the reference if not to a more global object
702 elsif Scope_Depth (Scope (Ent)) >= Scop then
705 -- References to types, exceptions and constants are always OK
708 or else Ekind (Ent) = E_Exception
709 or else Ekind (Ent) = E_Constant
713 -- If other than a non-volatile scalar variable, we have some
714 -- kind of global reference (e.g. to a function) that we cannot
715 -- deal with so we forget the attempt.
717 elsif Ekind (Ent) /= E_Variable
718 or else not Is_Scalar_Type (Etype (Ent))
719 or else Treat_As_Volatile (Ent)
723 -- Otherwise we have a reference to a global scalar
726 -- Loop through global entities already detected
728 Elm := First_Elmt (Var_List);
730 -- If not detected before, record this new global reference
733 Count_Vars := Count_Vars + 1;
735 if Count_Vars <= Max_Vars then
736 Append_Elmt (Entity (Nod), Var_List);
743 -- If recorded before, ignore
745 elsif Node (Elm) = Entity (Nod) then
748 -- Otherwise keep looking
758 -- For all other node kinds, recursively visit syntactic children
765 function Traverse_Body is new Traverse_Func (Process);
767 -- Start of processing for Detect_Infinite_Recursion
770 -- Do not attempt detection in No_Implicit_Conditional mode, since we
771 -- won't be able to generate the code to handle the recursion in any
774 if Restriction_Active (No_Implicit_Conditionals) then
778 -- Otherwise do traversal and quit if we get abandon signal
780 if Traverse_Body (N) = Abandon then
783 -- We must have a call, since Has_Recursive_Call was set. If not just
784 -- ignore (this is only an error check, so if we have a funny situation,
785 -- due to bugs or errors, we do not want to bomb!)
787 elsif Is_Empty_Elmt_List (Call_List) then
791 -- Here is the case where we detect recursion at compile time
793 -- Push our current scope for analyzing the declarations and code that
794 -- we will insert for the checking.
798 -- This loop builds temporary variables for each of the referenced
799 -- globals, so that at the end of the loop the list Shad_List contains
800 -- these temporaries in one-to-one correspondence with the elements in
804 Elm := First_Elmt (Var_List);
805 while Present (Elm) loop
807 Ent := Make_Temporary (Loc, 'S');
808 Append_Elmt (Ent, Shad_List);
810 -- Insert a declaration for this temporary at the start of the
811 -- declarations for the procedure. The temporaries are declared as
812 -- constant objects initialized to the current values of the
813 -- corresponding temporaries.
816 Make_Object_Declaration (Loc,
817 Defining_Identifier => Ent,
818 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
819 Constant_Present => True,
820 Expression => New_Occurrence_Of (Var, Loc));
823 Prepend (Decl, Declarations (N));
825 Insert_After (Last, Decl);
833 -- Loop through calls
835 Call := First_Elmt (Call_List);
836 while Present (Call) loop
838 -- Build a predicate expression of the form
841 -- and then global1 = temp1
842 -- and then global2 = temp2
845 -- This predicate determines if any of the global values
846 -- referenced by the procedure have changed since the
847 -- current call, if not an infinite recursion is assured.
849 Test := New_Occurrence_Of (Standard_True, Loc);
851 Elm1 := First_Elmt (Var_List);
852 Elm2 := First_Elmt (Shad_List);
853 while Present (Elm1) loop
859 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
860 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
866 -- Now we replace the call with the sequence
868 -- if no-changes (see above) then
869 -- raise Storage_Error;
874 Rewrite (Node (Call),
875 Make_If_Statement (Loc,
877 Then_Statements => New_List (
878 Make_Raise_Storage_Error (Loc,
879 Reason => SE_Infinite_Recursion)),
881 Else_Statements => New_List (
882 Relocate_Node (Node (Call)))));
884 Analyze (Node (Call));
889 -- Remove temporary scope stack entry used for analysis
892 end Detect_Infinite_Recursion;
898 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
899 Loc : constant Source_Ptr := Sloc (N);
904 E_Formal : Entity_Id;
906 procedure Add_Call_By_Copy_Code;
907 -- For cases where the parameter must be passed by copy, this routine
908 -- generates a temporary variable into which the actual is copied and
909 -- then passes this as the parameter. For an OUT or IN OUT parameter,
910 -- an assignment is also generated to copy the result back. The call
911 -- also takes care of any constraint checks required for the type
912 -- conversion case (on both the way in and the way out).
914 procedure Add_Simple_Call_By_Copy_Code;
915 -- This is similar to the above, but is used in cases where we know
916 -- that all that is needed is to simply create a temporary and copy
917 -- the value in and out of the temporary.
919 procedure Check_Fortran_Logical;
920 -- A value of type Logical that is passed through a formal parameter
921 -- must be normalized because .TRUE. usually does not have the same
922 -- representation as True. We assume that .FALSE. = False = 0.
923 -- What about functions that return a logical type ???
925 function Is_Legal_Copy return Boolean;
926 -- Check that an actual can be copied before generating the temporary
927 -- to be used in the call. If the actual is of a by_reference type then
928 -- the program is illegal (this can only happen in the presence of
929 -- rep. clauses that force an incorrect alignment). If the formal is
930 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
931 -- the effect that this might lead to unaligned arguments.
933 function Make_Var (Actual : Node_Id) return Entity_Id;
934 -- Returns an entity that refers to the given actual parameter,
935 -- Actual (not including any type conversion). If Actual is an
936 -- entity name, then this entity is returned unchanged, otherwise
937 -- a renaming is created to provide an entity for the actual.
939 procedure Reset_Packed_Prefix;
940 -- The expansion of a packed array component reference is delayed in
941 -- the context of a call. Now we need to complete the expansion, so we
942 -- unmark the analyzed bits in all prefixes.
944 ---------------------------
945 -- Add_Call_By_Copy_Code --
946 ---------------------------
948 procedure Add_Call_By_Copy_Code is
954 F_Typ : constant Entity_Id := Etype (Formal);
959 if not Is_Legal_Copy then
963 Temp := Make_Temporary (Loc, 'T', Actual);
965 -- Use formal type for temp, unless formal type is an unconstrained
966 -- array, in which case we don't have to worry about bounds checks,
967 -- and we use the actual type, since that has appropriate bounds.
969 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
970 Indic := New_Occurrence_Of (Etype (Actual), Loc);
972 Indic := New_Occurrence_Of (Etype (Formal), Loc);
975 if Nkind (Actual) = N_Type_Conversion then
976 V_Typ := Etype (Expression (Actual));
978 -- If the formal is an (in-)out parameter, capture the name
979 -- of the variable in order to build the post-call assignment.
981 Var := Make_Var (Expression (Actual));
983 Crep := not Same_Representation
984 (F_Typ, Etype (Expression (Actual)));
987 V_Typ := Etype (Actual);
988 Var := Make_Var (Actual);
992 -- Setup initialization for case of in out parameter, or an out
993 -- parameter where the formal is an unconstrained array (in the
994 -- latter case, we have to pass in an object with bounds).
996 -- If this is an out parameter, the initial copy is wasteful, so as
997 -- an optimization for the one-dimensional case we extract the
998 -- bounds of the actual and build an uninitialized temporary of the
1001 if Ekind (Formal) = E_In_Out_Parameter
1002 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1004 if Nkind (Actual) = N_Type_Conversion then
1005 if Conversion_OK (Actual) then
1006 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1008 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1011 elsif Ekind (Formal) = E_Out_Parameter
1012 and then Is_Array_Type (F_Typ)
1013 and then Number_Dimensions (F_Typ) = 1
1014 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1016 -- Actual is a one-dimensional array or slice, and the type
1017 -- requires no initialization. Create a temporary of the
1018 -- right size, but do not copy actual into it (optimization).
1022 Make_Subtype_Indication (Loc,
1024 New_Occurrence_Of (F_Typ, Loc),
1026 Make_Index_Or_Discriminant_Constraint (Loc,
1027 Constraints => New_List (
1030 Make_Attribute_Reference (Loc,
1031 Prefix => New_Occurrence_Of (Var, Loc),
1032 Attribute_Name => Name_First),
1034 Make_Attribute_Reference (Loc,
1035 Prefix => New_Occurrence_Of (Var, Loc),
1036 Attribute_Name => Name_Last)))));
1039 Init := New_Occurrence_Of (Var, Loc);
1042 -- An initialization is created for packed conversions as
1043 -- actuals for out parameters to enable Make_Object_Declaration
1044 -- to determine the proper subtype for N_Node. Note that this
1045 -- is wasteful because the extra copying on the call side is
1046 -- not required for such out parameters. ???
1048 elsif Ekind (Formal) = E_Out_Parameter
1049 and then Nkind (Actual) = N_Type_Conversion
1050 and then (Is_Bit_Packed_Array (F_Typ)
1052 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1054 if Conversion_OK (Actual) then
1055 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1057 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1060 elsif Ekind (Formal) = E_In_Parameter then
1062 -- Handle the case in which the actual is a type conversion
1064 if Nkind (Actual) = N_Type_Conversion then
1065 if Conversion_OK (Actual) then
1066 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1068 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1071 Init := New_Occurrence_Of (Var, Loc);
1079 Make_Object_Declaration (Loc,
1080 Defining_Identifier => Temp,
1081 Object_Definition => Indic,
1082 Expression => Init);
1083 Set_Assignment_OK (N_Node);
1084 Insert_Action (N, N_Node);
1086 -- Now, normally the deal here is that we use the defining
1087 -- identifier created by that object declaration. There is
1088 -- one exception to this. In the change of representation case
1089 -- the above declaration will end up looking like:
1091 -- temp : type := identifier;
1093 -- And in this case we might as well use the identifier directly
1094 -- and eliminate the temporary. Note that the analysis of the
1095 -- declaration was not a waste of time in that case, since it is
1096 -- what generated the necessary change of representation code. If
1097 -- the change of representation introduced additional code, as in
1098 -- a fixed-integer conversion, the expression is not an identifier
1099 -- and must be kept.
1102 and then Present (Expression (N_Node))
1103 and then Is_Entity_Name (Expression (N_Node))
1105 Temp := Entity (Expression (N_Node));
1106 Rewrite (N_Node, Make_Null_Statement (Loc));
1109 -- For IN parameter, all we do is to replace the actual
1111 if Ekind (Formal) = E_In_Parameter then
1112 Rewrite (Actual, New_Reference_To (Temp, Loc));
1115 -- Processing for OUT or IN OUT parameter
1118 -- Kill current value indications for the temporary variable we
1119 -- created, since we just passed it as an OUT parameter.
1121 Kill_Current_Values (Temp);
1122 Set_Is_Known_Valid (Temp, False);
1124 -- If type conversion, use reverse conversion on exit
1126 if Nkind (Actual) = N_Type_Conversion then
1127 if Conversion_OK (Actual) then
1128 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1130 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1133 Expr := New_Occurrence_Of (Temp, Loc);
1136 Rewrite (Actual, New_Reference_To (Temp, Loc));
1139 -- If the actual is a conversion of a packed reference, it may
1140 -- already have been expanded by Remove_Side_Effects, and the
1141 -- resulting variable is a temporary which does not designate
1142 -- the proper out-parameter, which may not be addressable. In
1143 -- that case, generate an assignment to the original expression
1144 -- (before expansion of the packed reference) so that the proper
1145 -- expansion of assignment to a packed component can take place.
1152 if Is_Renaming_Of_Object (Var)
1153 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1154 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1155 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1156 = N_Indexed_Component
1158 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1160 Obj := Renamed_Object (Var);
1162 Make_Selected_Component (Loc,
1164 New_Copy_Tree (Original_Node (Prefix (Obj))),
1165 Selector_Name => New_Copy (Selector_Name (Obj)));
1166 Reset_Analyzed_Flags (Lhs);
1169 Lhs := New_Occurrence_Of (Var, Loc);
1172 Set_Assignment_OK (Lhs);
1174 if Is_Access_Type (E_Formal)
1175 and then Is_Entity_Name (Lhs)
1177 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1179 -- Copyback target is an Ada 2012 stand-alone object
1180 -- of an anonymous access type
1182 pragma Assert (Ada_Version >= Ada_2012);
1184 if Type_Access_Level (E_Formal) >
1185 Object_Access_Level (Lhs)
1187 Append_To (Post_Call,
1188 Make_Raise_Program_Error (Loc,
1189 Reason => PE_Accessibility_Check_Failed));
1192 Append_To (Post_Call,
1193 Make_Assignment_Statement (Loc,
1195 Expression => Expr));
1197 -- We would like to somehow suppress generation of the
1198 -- extra_accessibility assignment generated by the expansion
1199 -- of the above assignment statement. It's not a correctness
1200 -- issue because the following assignment renders it dead,
1201 -- but generating back-to-back assignments to the same
1202 -- target is undesirable. ???
1204 Append_To (Post_Call,
1205 Make_Assignment_Statement (Loc,
1206 Name => New_Occurrence_Of (
1207 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1208 Expression => Make_Integer_Literal (Loc,
1209 Type_Access_Level (E_Formal))));
1212 Append_To (Post_Call,
1213 Make_Assignment_Statement (Loc,
1215 Expression => Expr));
1219 end Add_Call_By_Copy_Code;
1221 ----------------------------------
1222 -- Add_Simple_Call_By_Copy_Code --
1223 ----------------------------------
1225 procedure Add_Simple_Call_By_Copy_Code is
1233 F_Typ : constant Entity_Id := Etype (Formal);
1236 if not Is_Legal_Copy then
1240 -- Use formal type for temp, unless formal type is an unconstrained
1241 -- array, in which case we don't have to worry about bounds checks,
1242 -- and we use the actual type, since that has appropriate bounds.
1244 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1245 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1247 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1250 -- Prepare to generate code
1252 Reset_Packed_Prefix;
1254 Temp := Make_Temporary (Loc, 'T', Actual);
1255 Incod := Relocate_Node (Actual);
1256 Outcod := New_Copy_Tree (Incod);
1258 -- Generate declaration of temporary variable, initializing it
1259 -- with the input parameter unless we have an OUT formal or
1260 -- this is an initialization call.
1262 -- If the formal is an out parameter with discriminants, the
1263 -- discriminants must be captured even if the rest of the object
1264 -- is in principle uninitialized, because the discriminants may
1265 -- be read by the called subprogram.
1267 if Ekind (Formal) = E_Out_Parameter then
1270 if Has_Discriminants (Etype (Formal)) then
1271 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1274 elsif Inside_Init_Proc then
1276 -- Could use a comment here to match comment below ???
1278 if Nkind (Actual) /= N_Selected_Component
1280 not Has_Discriminant_Dependent_Constraint
1281 (Entity (Selector_Name (Actual)))
1285 -- Otherwise, keep the component in order to generate the proper
1286 -- actual subtype, that depends on enclosing discriminants.
1294 Make_Object_Declaration (Loc,
1295 Defining_Identifier => Temp,
1296 Object_Definition => Indic,
1297 Expression => Incod);
1302 -- If the call is to initialize a component of a composite type,
1303 -- and the component does not depend on discriminants, use the
1304 -- actual type of the component. This is required in case the
1305 -- component is constrained, because in general the formal of the
1306 -- initialization procedure will be unconstrained. Note that if
1307 -- the component being initialized is constrained by an enclosing
1308 -- discriminant, the presence of the initialization in the
1309 -- declaration will generate an expression for the actual subtype.
1311 Set_No_Initialization (Decl);
1312 Set_Object_Definition (Decl,
1313 New_Occurrence_Of (Etype (Actual), Loc));
1316 Insert_Action (N, Decl);
1318 -- The actual is simply a reference to the temporary
1320 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1322 -- Generate copy out if OUT or IN OUT parameter
1324 if Ekind (Formal) /= E_In_Parameter then
1326 Rhs := New_Occurrence_Of (Temp, Loc);
1328 -- Deal with conversion
1330 if Nkind (Lhs) = N_Type_Conversion then
1331 Lhs := Expression (Lhs);
1332 Rhs := Convert_To (Etype (Actual), Rhs);
1335 Append_To (Post_Call,
1336 Make_Assignment_Statement (Loc,
1338 Expression => Rhs));
1339 Set_Assignment_OK (Name (Last (Post_Call)));
1341 end Add_Simple_Call_By_Copy_Code;
1343 ---------------------------
1344 -- Check_Fortran_Logical --
1345 ---------------------------
1347 procedure Check_Fortran_Logical is
1348 Logical : constant Entity_Id := Etype (Formal);
1351 -- Note: this is very incomplete, e.g. it does not handle arrays
1352 -- of logical values. This is really not the right approach at all???)
1355 if Convention (Subp) = Convention_Fortran
1356 and then Root_Type (Etype (Formal)) = Standard_Boolean
1357 and then Ekind (Formal) /= E_In_Parameter
1359 Var := Make_Var (Actual);
1360 Append_To (Post_Call,
1361 Make_Assignment_Statement (Loc,
1362 Name => New_Occurrence_Of (Var, Loc),
1364 Unchecked_Convert_To (
1367 Left_Opnd => New_Occurrence_Of (Var, Loc),
1369 Unchecked_Convert_To (
1371 New_Occurrence_Of (Standard_False, Loc))))));
1373 end Check_Fortran_Logical;
1379 function Is_Legal_Copy return Boolean is
1381 -- An attempt to copy a value of such a type can only occur if
1382 -- representation clauses give the actual a misaligned address.
1384 if Is_By_Reference_Type (Etype (Formal)) then
1386 ("misaligned actual cannot be passed by reference", Actual);
1389 -- For users of Starlet, we assume that the specification of by-
1390 -- reference mechanism is mandatory. This may lead to unaligned
1391 -- objects but at least for DEC legacy code it is known to work.
1392 -- The warning will alert users of this code that a problem may
1395 elsif Mechanism (Formal) = By_Reference
1396 and then Is_Valued_Procedure (Scope (Formal))
1399 ("by_reference actual may be misaligned?", Actual);
1411 function Make_Var (Actual : Node_Id) return Entity_Id is
1415 if Is_Entity_Name (Actual) then
1416 return Entity (Actual);
1419 Var := Make_Temporary (Loc, 'T', Actual);
1422 Make_Object_Renaming_Declaration (Loc,
1423 Defining_Identifier => Var,
1425 New_Occurrence_Of (Etype (Actual), Loc),
1426 Name => Relocate_Node (Actual));
1428 Insert_Action (N, N_Node);
1433 -------------------------
1434 -- Reset_Packed_Prefix --
1435 -------------------------
1437 procedure Reset_Packed_Prefix is
1438 Pfx : Node_Id := Actual;
1441 Set_Analyzed (Pfx, False);
1443 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1444 Pfx := Prefix (Pfx);
1446 end Reset_Packed_Prefix;
1448 -- Start of processing for Expand_Actuals
1451 Post_Call := New_List;
1453 Formal := First_Formal (Subp);
1454 Actual := First_Actual (N);
1455 while Present (Formal) loop
1456 E_Formal := Etype (Formal);
1458 if Is_Scalar_Type (E_Formal)
1459 or else Nkind (Actual) = N_Slice
1461 Check_Fortran_Logical;
1465 elsif Ekind (Formal) /= E_Out_Parameter then
1467 -- The unusual case of the current instance of a protected type
1468 -- requires special handling. This can only occur in the context
1469 -- of a call within the body of a protected operation.
1471 if Is_Entity_Name (Actual)
1472 and then Ekind (Entity (Actual)) = E_Protected_Type
1473 and then In_Open_Scopes (Entity (Actual))
1475 if Scope (Subp) /= Entity (Actual) then
1476 Error_Msg_N ("operation outside protected type may not "
1477 & "call back its protected operations?", Actual);
1481 Expand_Protected_Object_Reference (N, Entity (Actual)));
1484 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1485 -- build-in-place function, then a temporary return object needs
1486 -- to be created and access to it must be passed to the function.
1487 -- Currently we limit such functions to those with inherently
1488 -- limited result subtypes, but eventually we plan to expand the
1489 -- functions that are treated as build-in-place to include other
1490 -- composite result types.
1492 if Is_Build_In_Place_Function_Call (Actual) then
1493 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1496 Apply_Constraint_Check (Actual, E_Formal);
1498 -- Out parameter case. No constraint checks on access type
1501 elsif Is_Access_Type (E_Formal) then
1506 elsif Has_Discriminants (Base_Type (E_Formal))
1507 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1509 Apply_Constraint_Check (Actual, E_Formal);
1514 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1517 -- Processing for IN-OUT and OUT parameters
1519 if Ekind (Formal) /= E_In_Parameter then
1521 -- For type conversions of arrays, apply length/range checks
1523 if Is_Array_Type (E_Formal)
1524 and then Nkind (Actual) = N_Type_Conversion
1526 if Is_Constrained (E_Formal) then
1527 Apply_Length_Check (Expression (Actual), E_Formal);
1529 Apply_Range_Check (Expression (Actual), E_Formal);
1533 -- If argument is a type conversion for a type that is passed
1534 -- by copy, then we must pass the parameter by copy.
1536 if Nkind (Actual) = N_Type_Conversion
1538 (Is_Numeric_Type (E_Formal)
1539 or else Is_Access_Type (E_Formal)
1540 or else Is_Enumeration_Type (E_Formal)
1541 or else Is_Bit_Packed_Array (Etype (Formal))
1542 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1544 -- Also pass by copy if change of representation
1546 or else not Same_Representation
1548 Etype (Expression (Actual))))
1550 Add_Call_By_Copy_Code;
1552 -- References to components of bit packed arrays are expanded
1553 -- at this point, rather than at the point of analysis of the
1554 -- actuals, to handle the expansion of the assignment to
1555 -- [in] out parameters.
1557 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1558 Add_Simple_Call_By_Copy_Code;
1560 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1561 -- because the back-end cannot cope with such objects. In other
1562 -- cases where alignment forces a copy, the back-end generates
1563 -- it properly. It should not be generated unconditionally in the
1564 -- front-end because it does not know precisely the alignment
1565 -- requirements of the target, and makes too conservative an
1566 -- estimate, leading to superfluous copies or spurious errors
1567 -- on by-reference parameters.
1569 elsif Nkind (Actual) = N_Selected_Component
1571 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1572 and then not Represented_As_Scalar (Etype (Formal))
1574 Add_Simple_Call_By_Copy_Code;
1576 -- References to slices of bit packed arrays are expanded
1578 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1579 Add_Call_By_Copy_Code;
1581 -- References to possibly unaligned slices of arrays are expanded
1583 elsif Is_Possibly_Unaligned_Slice (Actual) then
1584 Add_Call_By_Copy_Code;
1586 -- Deal with access types where the actual subtype and the
1587 -- formal subtype are not the same, requiring a check.
1589 -- It is necessary to exclude tagged types because of "downward
1590 -- conversion" errors.
1592 elsif Is_Access_Type (E_Formal)
1593 and then not Same_Type (E_Formal, Etype (Actual))
1594 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1596 Add_Call_By_Copy_Code;
1598 -- If the actual is not a scalar and is marked for volatile
1599 -- treatment, whereas the formal is not volatile, then pass
1600 -- by copy unless it is a by-reference type.
1602 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1603 -- because this is the enforcement of a language rule that applies
1604 -- only to "real" volatile variables, not e.g. to the address
1605 -- clause overlay case.
1607 elsif Is_Entity_Name (Actual)
1608 and then Is_Volatile (Entity (Actual))
1609 and then not Is_By_Reference_Type (Etype (Actual))
1610 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1611 and then not Is_Volatile (E_Formal)
1613 Add_Call_By_Copy_Code;
1615 elsif Nkind (Actual) = N_Indexed_Component
1616 and then Is_Entity_Name (Prefix (Actual))
1617 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1619 Add_Call_By_Copy_Code;
1621 -- Add call-by-copy code for the case of scalar out parameters
1622 -- when it is not known at compile time that the subtype of the
1623 -- formal is a subrange of the subtype of the actual (or vice
1624 -- versa for in out parameters), in order to get range checks
1625 -- on such actuals. (Maybe this case should be handled earlier
1626 -- in the if statement???)
1628 elsif Is_Scalar_Type (E_Formal)
1630 (not In_Subrange_Of (E_Formal, Etype (Actual))
1632 (Ekind (Formal) = E_In_Out_Parameter
1633 and then not In_Subrange_Of (Etype (Actual), E_Formal)))
1635 -- Perhaps the setting back to False should be done within
1636 -- Add_Call_By_Copy_Code, since it could get set on other
1637 -- cases occurring above???
1639 if Do_Range_Check (Actual) then
1640 Set_Do_Range_Check (Actual, False);
1643 Add_Call_By_Copy_Code;
1646 -- Processing for IN parameters
1649 -- For IN parameters is in the packed array case, we expand an
1650 -- indexed component (the circuit in Exp_Ch4 deliberately left
1651 -- indexed components appearing as actuals untouched, so that
1652 -- the special processing above for the OUT and IN OUT cases
1653 -- could be performed. We could make the test in Exp_Ch4 more
1654 -- complex and have it detect the parameter mode, but it is
1655 -- easier simply to handle all cases here.)
1657 if Nkind (Actual) = N_Indexed_Component
1658 and then Is_Packed (Etype (Prefix (Actual)))
1660 Reset_Packed_Prefix;
1661 Expand_Packed_Element_Reference (Actual);
1663 -- If we have a reference to a bit packed array, we copy it, since
1664 -- the actual must be byte aligned.
1666 -- Is this really necessary in all cases???
1668 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1669 Add_Simple_Call_By_Copy_Code;
1671 -- If a non-scalar actual is possibly unaligned, we need a copy
1673 elsif Is_Possibly_Unaligned_Object (Actual)
1674 and then not Represented_As_Scalar (Etype (Formal))
1676 Add_Simple_Call_By_Copy_Code;
1678 -- Similarly, we have to expand slices of packed arrays here
1679 -- because the result must be byte aligned.
1681 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1682 Add_Call_By_Copy_Code;
1684 -- Only processing remaining is to pass by copy if this is a
1685 -- reference to a possibly unaligned slice, since the caller
1686 -- expects an appropriately aligned argument.
1688 elsif Is_Possibly_Unaligned_Slice (Actual) then
1689 Add_Call_By_Copy_Code;
1691 -- An unusual case: a current instance of an enclosing task can be
1692 -- an actual, and must be replaced by a reference to self.
1694 elsif Is_Entity_Name (Actual)
1695 and then Is_Task_Type (Entity (Actual))
1697 if In_Open_Scopes (Entity (Actual)) then
1699 (Make_Function_Call (Loc,
1700 Name => New_Reference_To (RTE (RE_Self), Loc))));
1703 -- A task type cannot otherwise appear as an actual
1706 raise Program_Error;
1711 Next_Formal (Formal);
1712 Next_Actual (Actual);
1715 -- Find right place to put post call stuff if it is present
1717 if not Is_Empty_List (Post_Call) then
1719 -- If call is not a list member, it must be the triggering statement
1720 -- of a triggering alternative or an entry call alternative, and we
1721 -- can add the post call stuff to the corresponding statement list.
1723 if not Is_List_Member (N) then
1725 P : constant Node_Id := Parent (N);
1728 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1729 N_Entry_Call_Alternative));
1731 if Is_Non_Empty_List (Statements (P)) then
1732 Insert_List_Before_And_Analyze
1733 (First (Statements (P)), Post_Call);
1735 Set_Statements (P, Post_Call);
1739 -- Otherwise, normal case where N is in a statement sequence,
1740 -- just put the post-call stuff after the call statement.
1743 Insert_Actions_After (N, Post_Call);
1747 -- The call node itself is re-analyzed in Expand_Call
1755 -- This procedure handles expansion of function calls and procedure call
1756 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1757 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1759 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1760 -- Provide values of actuals for all formals in Extra_Formals list
1761 -- Replace "call" to enumeration literal function by literal itself
1762 -- Rewrite call to predefined operator as operator
1763 -- Replace actuals to in-out parameters that are numeric conversions,
1764 -- with explicit assignment to temporaries before and after the call.
1765 -- Remove optional actuals if First_Optional_Parameter specified.
1767 -- Note that the list of actuals has been filled with default expressions
1768 -- during semantic analysis of the call. Only the extra actuals required
1769 -- for the 'Constrained attribute and for accessibility checks are added
1772 procedure Expand_Call (N : Node_Id) is
1773 Loc : constant Source_Ptr := Sloc (N);
1774 Call_Node : Node_Id := N;
1775 Extra_Actuals : List_Id := No_List;
1776 Prev : Node_Id := Empty;
1778 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1779 -- Adds one entry to the end of the actual parameter list. Used for
1780 -- default parameters and for extra actuals (for Extra_Formals). The
1781 -- argument is an N_Parameter_Association node.
1783 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1784 -- Adds an extra actual to the list of extra actuals. Expr is the
1785 -- expression for the value of the actual, EF is the entity for the
1788 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1789 -- Within an instance, a type derived from a non-tagged formal derived
1790 -- type inherits from the original parent, not from the actual. The
1791 -- current derivation mechanism has the derived type inherit from the
1792 -- actual, which is only correct outside of the instance. If the
1793 -- subprogram is inherited, we test for this particular case through a
1794 -- convoluted tree traversal before setting the proper subprogram to be
1797 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
1798 -- Determine if Subp denotes a non-dispatching call to a Deep routine
1800 function New_Value (From : Node_Id) return Node_Id;
1801 -- From is the original Expression. New_Value is equivalent to a call
1802 -- to Duplicate_Subexpr with an explicit dereference when From is an
1803 -- access parameter.
1805 --------------------------
1806 -- Add_Actual_Parameter --
1807 --------------------------
1809 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1810 Actual_Expr : constant Node_Id :=
1811 Explicit_Actual_Parameter (Insert_Param);
1814 -- Case of insertion is first named actual
1816 if No (Prev) or else
1817 Nkind (Parent (Prev)) /= N_Parameter_Association
1819 Set_Next_Named_Actual
1820 (Insert_Param, First_Named_Actual (Call_Node));
1821 Set_First_Named_Actual (Call_Node, Actual_Expr);
1824 if No (Parameter_Associations (Call_Node)) then
1825 Set_Parameter_Associations (Call_Node, New_List);
1826 Append (Insert_Param, Parameter_Associations (Call_Node));
1829 Insert_After (Prev, Insert_Param);
1832 -- Case of insertion is not first named actual
1835 Set_Next_Named_Actual
1836 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1837 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1838 Append (Insert_Param, Parameter_Associations (Call_Node));
1841 Prev := Actual_Expr;
1842 end Add_Actual_Parameter;
1844 ----------------------
1845 -- Add_Extra_Actual --
1846 ----------------------
1848 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1849 Loc : constant Source_Ptr := Sloc (Expr);
1852 if Extra_Actuals = No_List then
1853 Extra_Actuals := New_List;
1854 Set_Parent (Extra_Actuals, Call_Node);
1857 Append_To (Extra_Actuals,
1858 Make_Parameter_Association (Loc,
1859 Selector_Name => Make_Identifier (Loc, Chars (EF)),
1860 Explicit_Actual_Parameter => Expr));
1862 Analyze_And_Resolve (Expr, Etype (EF));
1864 if Nkind (Call_Node) = N_Function_Call then
1865 Set_Is_Accessibility_Actual (Parent (Expr));
1867 end Add_Extra_Actual;
1869 ---------------------------
1870 -- Inherited_From_Formal --
1871 ---------------------------
1873 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1875 Gen_Par : Entity_Id;
1876 Gen_Prim : Elist_Id;
1881 -- If the operation is inherited, it is attached to the corresponding
1882 -- type derivation. If the parent in the derivation is a generic
1883 -- actual, it is a subtype of the actual, and we have to recover the
1884 -- original derived type declaration to find the proper parent.
1886 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1887 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1888 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1889 N_Derived_Type_Definition
1890 or else not In_Instance
1897 (Type_Definition (Original_Node (Parent (S))));
1899 if Nkind (Indic) = N_Subtype_Indication then
1900 Par := Entity (Subtype_Mark (Indic));
1902 Par := Entity (Indic);
1906 if not Is_Generic_Actual_Type (Par)
1907 or else Is_Tagged_Type (Par)
1908 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1909 or else not In_Open_Scopes (Scope (Par))
1913 Gen_Par := Generic_Parent_Type (Parent (Par));
1916 -- If the actual has no generic parent type, the formal is not
1917 -- a formal derived type, so nothing to inherit.
1919 if No (Gen_Par) then
1923 -- If the generic parent type is still the generic type, this is a
1924 -- private formal, not a derived formal, and there are no operations
1925 -- inherited from the formal.
1927 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1931 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1933 Elmt := First_Elmt (Gen_Prim);
1934 while Present (Elmt) loop
1935 if Chars (Node (Elmt)) = Chars (S) then
1941 F1 := First_Formal (S);
1942 F2 := First_Formal (Node (Elmt));
1944 and then Present (F2)
1946 if Etype (F1) = Etype (F2)
1947 or else Etype (F2) = Gen_Par
1953 exit; -- not the right subprogram
1965 raise Program_Error;
1966 end Inherited_From_Formal;
1968 -------------------------
1969 -- Is_Direct_Deep_Call --
1970 -------------------------
1972 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
1974 if Is_TSS (Subp, TSS_Deep_Adjust)
1975 or else Is_TSS (Subp, TSS_Deep_Finalize)
1976 or else Is_TSS (Subp, TSS_Deep_Initialize)
1983 Actual := First (Parameter_Associations (N));
1984 Formal := First_Formal (Subp);
1985 while Present (Actual)
1986 and then Present (Formal)
1988 if Nkind (Actual) = N_Identifier
1989 and then Is_Controlling_Actual (Actual)
1990 and then Etype (Actual) = Etype (Formal)
1996 Next_Formal (Formal);
2002 end Is_Direct_Deep_Call;
2008 function New_Value (From : Node_Id) return Node_Id is
2009 Res : constant Node_Id := Duplicate_Subexpr (From);
2011 if Is_Access_Type (Etype (From)) then
2013 Make_Explicit_Dereference (Sloc (From),
2022 Curr_S : constant Entity_Id := Current_Scope;
2023 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2026 Orig_Subp : Entity_Id := Empty;
2027 Param_Count : Natural := 0;
2028 Parent_Formal : Entity_Id;
2029 Parent_Subp : Entity_Id;
2033 Prev_Orig : Node_Id;
2034 -- Original node for an actual, which may have been rewritten. If the
2035 -- actual is a function call that has been transformed from a selected
2036 -- component, the original node is unanalyzed. Otherwise, it carries
2037 -- semantic information used to generate additional actuals.
2039 CW_Interface_Formals_Present : Boolean := False;
2041 -- Start of processing for Expand_Call
2044 -- Ignore if previous error
2046 if Nkind (Call_Node) in N_Has_Etype
2047 and then Etype (Call_Node) = Any_Type
2052 -- Call using access to subprogram with explicit dereference
2054 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2055 Subp := Etype (Name (Call_Node));
2056 Parent_Subp := Empty;
2058 -- Case of call to simple entry, where the Name is a selected component
2059 -- whose prefix is the task, and whose selector name is the entry name
2061 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2062 Subp := Entity (Selector_Name (Name (Call_Node)));
2063 Parent_Subp := Empty;
2065 -- Case of call to member of entry family, where Name is an indexed
2066 -- component, with the prefix being a selected component giving the
2067 -- task and entry family name, and the index being the entry index.
2069 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2070 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2071 Parent_Subp := Empty;
2076 Subp := Entity (Name (Call_Node));
2077 Parent_Subp := Alias (Subp);
2079 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2080 -- if we can tell that the first parameter cannot possibly be null.
2081 -- This improves efficiency by avoiding a run-time test.
2083 -- We do not do this if Raise_Exception_Always does not exist, which
2084 -- can happen in configurable run time profiles which provide only a
2087 if Is_RTE (Subp, RE_Raise_Exception)
2088 and then RTE_Available (RE_Raise_Exception_Always)
2091 FA : constant Node_Id :=
2092 Original_Node (First_Actual (Call_Node));
2095 -- The case we catch is where the first argument is obtained
2096 -- using the Identity attribute (which must always be
2099 if Nkind (FA) = N_Attribute_Reference
2100 and then Attribute_Name (FA) = Name_Identity
2102 Subp := RTE (RE_Raise_Exception_Always);
2103 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2108 if Ekind (Subp) = E_Entry then
2109 Parent_Subp := Empty;
2113 -- Detect the following code in System.Finalization_Masters only on
2114 -- .NET/JVM targets:
2116 -- procedure Finalize (Master : in out Finalization_Master) is
2120 -- Finalize (Curr_Ptr.all);
2122 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2123 -- cannot be named in library or user code, the compiler has to install
2124 -- a kludge and transform the call to Finalize into Deep_Finalize.
2126 if VM_Target /= No_VM
2127 and then Chars (Subp) = Name_Finalize
2128 and then Ekind (Curr_S) = E_Block
2129 and then Ekind (Scope (Curr_S)) = E_Procedure
2130 and then Chars (Scope (Curr_S)) = Name_Finalize
2131 and then Etype (First_Formal (Scope (Curr_S))) =
2132 RTE (RE_Finalization_Master)
2135 Deep_Fin : constant Entity_Id :=
2136 Find_Prim_Op (RTE (RE_Root_Controlled),
2139 -- Since Root_Controlled is a tagged type, the compiler should
2140 -- always generate Deep_Finalize for it.
2142 pragma Assert (Present (Deep_Fin));
2145 -- Deep_Finalize (Curr_Ptr.all);
2148 Make_Procedure_Call_Statement (Loc,
2150 New_Reference_To (Deep_Fin, Loc),
2151 Parameter_Associations =>
2152 New_Copy_List_Tree (Parameter_Associations (N))));
2159 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2160 -- alternative in an asynchronous select or as an entry call in
2161 -- a conditional or timed select. Check whether the procedure call
2162 -- is a renaming of an entry and rewrite it as an entry call.
2164 if Ada_Version >= Ada_2005
2165 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2167 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2168 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2170 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2171 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2175 Ren_Root : Entity_Id := Subp;
2178 -- This may be a chain of renamings, find the root
2180 if Present (Alias (Ren_Root)) then
2181 Ren_Root := Alias (Ren_Root);
2184 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2185 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2187 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2189 Make_Entry_Call_Statement (Loc,
2191 New_Copy_Tree (Name (Ren_Decl)),
2192 Parameter_Associations =>
2194 (Parameter_Associations (Call_Node))));
2202 -- First step, compute extra actuals, corresponding to any Extra_Formals
2203 -- present. Note that we do not access Extra_Formals directly, instead
2204 -- we simply note the presence of the extra formals as we process the
2205 -- regular formals collecting corresponding actuals in Extra_Actuals.
2207 -- We also generate any required range checks for actuals for in formals
2208 -- as we go through the loop, since this is a convenient place to do it.
2209 -- (Though it seems that this would be better done in Expand_Actuals???)
2211 Formal := First_Formal (Subp);
2212 Actual := First_Actual (Call_Node);
2214 while Present (Formal) loop
2216 -- Generate range check if required
2218 if Do_Range_Check (Actual)
2219 and then Ekind (Formal) = E_In_Parameter
2221 Set_Do_Range_Check (Actual, False);
2222 Generate_Range_Check
2223 (Actual, Etype (Formal), CE_Range_Check_Failed);
2226 -- Prepare to examine current entry
2229 Prev_Orig := Original_Node (Prev);
2231 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2232 -- to expand it in a further round.
2234 CW_Interface_Formals_Present :=
2235 CW_Interface_Formals_Present
2237 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2238 and then Is_Interface (Etype (Etype (Formal))))
2240 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2241 and then Is_Interface (Directly_Designated_Type
2242 (Etype (Etype (Formal)))));
2244 -- Create possible extra actual for constrained case. Usually, the
2245 -- extra actual is of the form actual'constrained, but since this
2246 -- attribute is only available for unconstrained records, TRUE is
2247 -- expanded if the type of the formal happens to be constrained (for
2248 -- instance when this procedure is inherited from an unconstrained
2249 -- record to a constrained one) or if the actual has no discriminant
2250 -- (its type is constrained). An exception to this is the case of a
2251 -- private type without discriminants. In this case we pass FALSE
2252 -- because the object has underlying discriminants with defaults.
2254 if Present (Extra_Constrained (Formal)) then
2255 if Ekind (Etype (Prev)) in Private_Kind
2256 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2259 (New_Occurrence_Of (Standard_False, Loc),
2260 Extra_Constrained (Formal));
2262 elsif Is_Constrained (Etype (Formal))
2263 or else not Has_Discriminants (Etype (Prev))
2266 (New_Occurrence_Of (Standard_True, Loc),
2267 Extra_Constrained (Formal));
2269 -- Do not produce extra actuals for Unchecked_Union parameters.
2270 -- Jump directly to the end of the loop.
2272 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2273 goto Skip_Extra_Actual_Generation;
2276 -- If the actual is a type conversion, then the constrained
2277 -- test applies to the actual, not the target type.
2283 -- Test for unchecked conversions as well, which can occur
2284 -- as out parameter actuals on calls to stream procedures.
2287 while Nkind_In (Act_Prev, N_Type_Conversion,
2288 N_Unchecked_Type_Conversion)
2290 Act_Prev := Expression (Act_Prev);
2293 -- If the expression is a conversion of a dereference, this
2294 -- is internally generated code that manipulates addresses,
2295 -- e.g. when building interface tables. No check should
2296 -- occur in this case, and the discriminated object is not
2299 if not Comes_From_Source (Actual)
2300 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2301 and then Nkind (Act_Prev) = N_Explicit_Dereference
2304 (New_Occurrence_Of (Standard_False, Loc),
2305 Extra_Constrained (Formal));
2309 (Make_Attribute_Reference (Sloc (Prev),
2311 Duplicate_Subexpr_No_Checks
2312 (Act_Prev, Name_Req => True),
2313 Attribute_Name => Name_Constrained),
2314 Extra_Constrained (Formal));
2320 -- Create possible extra actual for accessibility level
2322 if Present (Extra_Accessibility (Formal)) then
2324 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2325 -- attribute, then the original actual may be an aliased object
2326 -- occurring as the prefix in a call using "Object.Operation"
2327 -- notation. In that case we must pass the level of the object,
2328 -- so Prev_Orig is reset to Prev and the attribute will be
2329 -- processed by the code for Access attributes further below.
2331 if Prev_Orig /= Prev
2332 and then Nkind (Prev) = N_Attribute_Reference
2334 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2335 and then Is_Aliased_View (Prev_Orig)
2340 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2341 -- accessibility levels.
2343 if Ekind (Current_Scope) in Subprogram_Kind
2344 and then Is_Thunk (Current_Scope)
2347 Parm_Ent : Entity_Id;
2350 if Is_Controlling_Actual (Actual) then
2352 -- Find the corresponding actual of the thunk
2354 Parm_Ent := First_Entity (Current_Scope);
2355 for J in 2 .. Param_Count loop
2356 Next_Entity (Parm_Ent);
2359 else pragma Assert (Is_Entity_Name (Actual));
2360 Parm_Ent := Entity (Actual);
2364 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2365 Extra_Accessibility (Formal));
2368 elsif Is_Entity_Name (Prev_Orig) then
2370 -- When passing an access parameter, or a renaming of an access
2371 -- parameter, as the actual to another access parameter we need
2372 -- to pass along the actual's own access level parameter. This
2373 -- is done if we are within the scope of the formal access
2374 -- parameter (if this is an inlined body the extra formal is
2377 if (Is_Formal (Entity (Prev_Orig))
2379 (Present (Renamed_Object (Entity (Prev_Orig)))
2381 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2384 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2385 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2386 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2389 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2392 pragma Assert (Present (Parm_Ent));
2394 if Present (Extra_Accessibility (Parm_Ent)) then
2397 (Extra_Accessibility (Parm_Ent), Loc),
2398 Extra_Accessibility (Formal));
2400 -- If the actual access parameter does not have an
2401 -- associated extra formal providing its scope level,
2402 -- then treat the actual as having library-level
2407 (Make_Integer_Literal (Loc,
2408 Intval => Scope_Depth (Standard_Standard)),
2409 Extra_Accessibility (Formal));
2413 -- The actual is a normal access value, so just pass the level
2414 -- of the actual's access type.
2418 (Dynamic_Accessibility_Level (Prev_Orig),
2419 Extra_Accessibility (Formal));
2422 -- If the actual is an access discriminant, then pass the level
2423 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2425 elsif Nkind (Prev_Orig) = N_Selected_Component
2426 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2428 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2429 E_Anonymous_Access_Type
2432 (Make_Integer_Literal (Loc,
2433 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2434 Extra_Accessibility (Formal));
2439 case Nkind (Prev_Orig) is
2441 when N_Attribute_Reference =>
2442 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2444 -- For X'Access, pass on the level of the prefix X
2446 when Attribute_Access =>
2448 -- If this is an Access attribute applied to the
2449 -- the current instance object passed to a type
2450 -- initialization procedure, then use the level
2451 -- of the type itself. This is not really correct,
2452 -- as there should be an extra level parameter
2453 -- passed in with _init formals (only in the case
2454 -- where the type is immutably limited), but we
2455 -- don't have an easy way currently to create such
2456 -- an extra formal (init procs aren't ever frozen).
2457 -- For now we just use the level of the type,
2458 -- which may be too shallow, but that works better
2459 -- than passing Object_Access_Level of the type,
2460 -- which can be one level too deep in some cases.
2463 if Is_Entity_Name (Prefix (Prev_Orig))
2464 and then Is_Type (Entity (Prefix (Prev_Orig)))
2467 (Make_Integer_Literal (Loc,
2470 (Entity (Prefix (Prev_Orig)))),
2471 Extra_Accessibility (Formal));
2475 (Make_Integer_Literal (Loc,
2478 (Prefix (Prev_Orig))),
2479 Extra_Accessibility (Formal));
2482 -- Treat the unchecked attributes as library-level
2484 when Attribute_Unchecked_Access |
2485 Attribute_Unrestricted_Access =>
2487 (Make_Integer_Literal (Loc,
2488 Intval => Scope_Depth (Standard_Standard)),
2489 Extra_Accessibility (Formal));
2491 -- No other cases of attributes returning access
2492 -- values that can be passed to access parameters.
2495 raise Program_Error;
2499 -- For allocators we pass the level of the execution of the
2500 -- called subprogram, which is one greater than the current
2505 (Make_Integer_Literal (Loc,
2506 Intval => Scope_Depth (Current_Scope) + 1),
2507 Extra_Accessibility (Formal));
2509 -- For most other cases we simply pass the level of the
2510 -- actual's access type. The type is retrieved from
2511 -- Prev rather than Prev_Orig, because in some cases
2512 -- Prev_Orig denotes an original expression that has
2513 -- not been analyzed.
2517 (Dynamic_Accessibility_Level (Prev),
2518 Extra_Accessibility (Formal));
2523 -- Perform the check of 4.6(49) that prevents a null value from being
2524 -- passed as an actual to an access parameter. Note that the check
2525 -- is elided in the common cases of passing an access attribute or
2526 -- access parameter as an actual. Also, we currently don't enforce
2527 -- this check for expander-generated actuals and when -gnatdj is set.
2529 if Ada_Version >= Ada_2005 then
2531 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2532 -- the intent of 6.4.1(13) is that null-exclusion checks should
2533 -- not be done for 'out' parameters, even though it refers only
2534 -- to constraint checks, and a null_exclusion is not a constraint.
2535 -- Note that AI05-0196-1 corrects this mistake in the RM.
2537 if Is_Access_Type (Etype (Formal))
2538 and then Can_Never_Be_Null (Etype (Formal))
2539 and then Ekind (Formal) /= E_Out_Parameter
2540 and then Nkind (Prev) /= N_Raise_Constraint_Error
2541 and then (Known_Null (Prev)
2542 or else not Can_Never_Be_Null (Etype (Prev)))
2544 Install_Null_Excluding_Check (Prev);
2547 -- Ada_Version < Ada_2005
2550 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2551 or else Access_Checks_Suppressed (Subp)
2555 elsif Debug_Flag_J then
2558 elsif not Comes_From_Source (Prev) then
2561 elsif Is_Entity_Name (Prev)
2562 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2566 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2569 -- Suppress null checks when passing to access parameters of Java
2570 -- and CIL subprograms. (Should this be done for other foreign
2571 -- conventions as well ???)
2573 elsif Convention (Subp) = Convention_Java
2574 or else Convention (Subp) = Convention_CIL
2579 Install_Null_Excluding_Check (Prev);
2583 -- Perform appropriate validity checks on parameters that
2586 if Validity_Checks_On then
2587 if (Ekind (Formal) = E_In_Parameter
2588 and then Validity_Check_In_Params)
2590 (Ekind (Formal) = E_In_Out_Parameter
2591 and then Validity_Check_In_Out_Params)
2593 -- If the actual is an indexed component of a packed type (or
2594 -- is an indexed or selected component whose prefix recursively
2595 -- meets this condition), it has not been expanded yet. It will
2596 -- be copied in the validity code that follows, and has to be
2597 -- expanded appropriately, so reanalyze it.
2599 -- What we do is just to unset analyzed bits on prefixes till
2600 -- we reach something that does not have a prefix.
2607 while Nkind_In (Nod, N_Indexed_Component,
2608 N_Selected_Component)
2610 Set_Analyzed (Nod, False);
2611 Nod := Prefix (Nod);
2615 Ensure_Valid (Actual);
2619 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2620 -- since this is a left side reference. We only do this for calls
2621 -- from the source program since we assume that compiler generated
2622 -- calls explicitly generate any required checks. We also need it
2623 -- only if we are doing standard validity checks, since clearly it is
2624 -- not needed if validity checks are off, and in subscript validity
2625 -- checking mode, all indexed components are checked with a call
2626 -- directly from Expand_N_Indexed_Component.
2628 if Comes_From_Source (Call_Node)
2629 and then Ekind (Formal) /= E_In_Parameter
2630 and then Validity_Checks_On
2631 and then Validity_Check_Default
2632 and then not Validity_Check_Subscripts
2634 Check_Valid_Lvalue_Subscripts (Actual);
2637 -- Mark any scalar OUT parameter that is a simple variable as no
2638 -- longer known to be valid (unless the type is always valid). This
2639 -- reflects the fact that if an OUT parameter is never set in a
2640 -- procedure, then it can become invalid on the procedure return.
2642 if Ekind (Formal) = E_Out_Parameter
2643 and then Is_Entity_Name (Actual)
2644 and then Ekind (Entity (Actual)) = E_Variable
2645 and then not Is_Known_Valid (Etype (Actual))
2647 Set_Is_Known_Valid (Entity (Actual), False);
2650 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2651 -- clear current values, since they can be clobbered. We are probably
2652 -- doing this in more places than we need to, but better safe than
2653 -- sorry when it comes to retaining bad current values!
2655 if Ekind (Formal) /= E_In_Parameter
2656 and then Is_Entity_Name (Actual)
2657 and then Present (Entity (Actual))
2660 Ent : constant Entity_Id := Entity (Actual);
2664 -- For an OUT or IN OUT parameter that is an assignable entity,
2665 -- we do not want to clobber the Last_Assignment field, since
2666 -- if it is set, it was precisely because it is indeed an OUT
2667 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
2668 -- since the subprogram could have returned in invalid value.
2670 if (Ekind (Formal) = E_Out_Parameter
2672 Ekind (Formal) = E_In_Out_Parameter)
2673 and then Is_Assignable (Ent)
2675 Sav := Last_Assignment (Ent);
2676 Kill_Current_Values (Ent);
2677 Set_Last_Assignment (Ent, Sav);
2678 Set_Is_Known_Valid (Ent, False);
2680 -- For all other cases, just kill the current values
2683 Kill_Current_Values (Ent);
2688 -- If the formal is class wide and the actual is an aggregate, force
2689 -- evaluation so that the back end who does not know about class-wide
2690 -- type, does not generate a temporary of the wrong size.
2692 if not Is_Class_Wide_Type (Etype (Formal)) then
2695 elsif Nkind (Actual) = N_Aggregate
2696 or else (Nkind (Actual) = N_Qualified_Expression
2697 and then Nkind (Expression (Actual)) = N_Aggregate)
2699 Force_Evaluation (Actual);
2702 -- In a remote call, if the formal is of a class-wide type, check
2703 -- that the actual meets the requirements described in E.4(18).
2705 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
2706 Insert_Action (Actual,
2707 Make_Transportable_Check (Loc,
2708 Duplicate_Subexpr_Move_Checks (Actual)));
2711 -- This label is required when skipping extra actual generation for
2712 -- Unchecked_Union parameters.
2714 <<Skip_Extra_Actual_Generation>>
2716 Param_Count := Param_Count + 1;
2717 Next_Actual (Actual);
2718 Next_Formal (Formal);
2721 -- If we are expanding a rhs of an assignment we need to check if tag
2722 -- propagation is needed. You might expect this processing to be in
2723 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
2724 -- assignment might be transformed to a declaration for an unconstrained
2725 -- value if the expression is classwide.
2727 if Nkind (Call_Node) = N_Function_Call
2728 and then Is_Tag_Indeterminate (Call_Node)
2729 and then Is_Entity_Name (Name (Call_Node))
2732 Ass : Node_Id := Empty;
2735 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
2736 Ass := Parent (Call_Node);
2738 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
2739 and then Nkind (Parent (Parent (Call_Node))) =
2740 N_Assignment_Statement
2742 Ass := Parent (Parent (Call_Node));
2744 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
2745 and then Nkind (Parent (Parent (Call_Node))) =
2746 N_Assignment_Statement
2748 Ass := Parent (Parent (Call_Node));
2752 and then Is_Class_Wide_Type (Etype (Name (Ass)))
2754 if Is_Access_Type (Etype (Call_Node)) then
2755 if Designated_Type (Etype (Call_Node)) /=
2756 Root_Type (Etype (Name (Ass)))
2759 ("tag-indeterminate expression "
2760 & " must have designated type& (RM 5.2 (6))",
2761 Call_Node, Root_Type (Etype (Name (Ass))));
2763 Propagate_Tag (Name (Ass), Call_Node);
2766 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
2768 ("tag-indeterminate expression must have type&"
2770 Call_Node, Root_Type (Etype (Name (Ass))));
2773 Propagate_Tag (Name (Ass), Call_Node);
2776 -- The call will be rewritten as a dispatching call, and
2777 -- expanded as such.
2784 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
2785 -- it to point to the correct secondary virtual table
2787 if Nkind_In (Call_Node, N_Function_Call, N_Procedure_Call_Statement)
2788 and then CW_Interface_Formals_Present
2790 Expand_Interface_Actuals (Call_Node);
2793 -- Deals with Dispatch_Call if we still have a call, before expanding
2794 -- extra actuals since this will be done on the re-analysis of the
2795 -- dispatching call. Note that we do not try to shorten the actual list
2796 -- for a dispatching call, it would not make sense to do so. Expansion
2797 -- of dispatching calls is suppressed when VM_Target, because the VM
2798 -- back-ends directly handle the generation of dispatching calls and
2799 -- would have to undo any expansion to an indirect call.
2801 if Nkind_In (Call_Node, N_Function_Call, N_Procedure_Call_Statement)
2802 and then Present (Controlling_Argument (Call_Node))
2805 Call_Typ : constant Entity_Id := Etype (Call_Node);
2806 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
2807 Eq_Prim_Op : Entity_Id := Empty;
2810 Prev_Call : Node_Id;
2813 if not Is_Limited_Type (Typ) then
2814 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
2817 if Tagged_Type_Expansion then
2818 Expand_Dispatching_Call (Call_Node);
2820 -- The following return is worrisome. Is it really OK to skip
2821 -- all remaining processing in this procedure ???
2828 Apply_Tag_Checks (Call_Node);
2830 -- If this is a dispatching "=", we must first compare the
2831 -- tags so we generate: x.tag = y.tag and then x = y
2833 if Subp = Eq_Prim_Op then
2835 -- Mark the node as analyzed to avoid reanalizing this
2836 -- dispatching call (which would cause a never-ending loop)
2838 Prev_Call := Relocate_Node (Call_Node);
2839 Set_Analyzed (Prev_Call);
2841 Param := First_Actual (Call_Node);
2847 Make_Selected_Component (Loc,
2848 Prefix => New_Value (Param),
2850 New_Reference_To (First_Tag_Component (Typ),
2854 Make_Selected_Component (Loc,
2856 Unchecked_Convert_To (Typ,
2857 New_Value (Next_Actual (Param))),
2860 (First_Tag_Component (Typ), Loc))),
2861 Right_Opnd => Prev_Call);
2863 Rewrite (Call_Node, New_Call);
2866 (Call_Node, Call_Typ, Suppress => All_Checks);
2869 -- Expansion of a dispatching call results in an indirect call,
2870 -- which in turn causes current values to be killed (see
2871 -- Resolve_Call), so on VM targets we do the call here to
2872 -- ensure consistent warnings between VM and non-VM targets.
2874 Kill_Current_Values;
2877 -- If this is a dispatching "=" then we must update the reference
2878 -- to the call node because we generated:
2879 -- x.tag = y.tag and then x = y
2881 if Subp = Eq_Prim_Op then
2882 Call_Node := Right_Opnd (Call_Node);
2887 -- Similarly, expand calls to RCI subprograms on which pragma
2888 -- All_Calls_Remote applies. The rewriting will be reanalyzed
2889 -- later. Do this only when the call comes from source since we
2890 -- do not want such a rewriting to occur in expanded code.
2892 if Is_All_Remote_Call (Call_Node) then
2893 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
2895 -- Similarly, do not add extra actuals for an entry call whose entity
2896 -- is a protected procedure, or for an internal protected subprogram
2897 -- call, because it will be rewritten as a protected subprogram call
2898 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
2900 elsif Is_Protected_Type (Scope (Subp))
2901 and then (Ekind (Subp) = E_Procedure
2902 or else Ekind (Subp) = E_Function)
2906 -- During that loop we gathered the extra actuals (the ones that
2907 -- correspond to Extra_Formals), so now they can be appended.
2910 while Is_Non_Empty_List (Extra_Actuals) loop
2911 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2915 -- At this point we have all the actuals, so this is the point at which
2916 -- the various expansion activities for actuals is carried out.
2918 Expand_Actuals (Call_Node, Subp);
2920 -- If the subprogram is a renaming, or if it is inherited, replace it in
2921 -- the call with the name of the actual subprogram being called. If this
2922 -- is a dispatching call, the run-time decides what to call. The Alias
2923 -- attribute does not apply to entries.
2925 if Nkind (Call_Node) /= N_Entry_Call_Statement
2926 and then No (Controlling_Argument (Call_Node))
2927 and then Present (Parent_Subp)
2928 and then not Is_Direct_Deep_Call (Subp)
2930 if Present (Inherited_From_Formal (Subp)) then
2931 Parent_Subp := Inherited_From_Formal (Subp);
2933 Parent_Subp := Ultimate_Alias (Parent_Subp);
2936 -- The below setting of Entity is suspect, see F109-018 discussion???
2938 Set_Entity (Name (Call_Node), Parent_Subp);
2940 if Is_Abstract_Subprogram (Parent_Subp)
2941 and then not In_Instance
2944 ("cannot call abstract subprogram &!",
2945 Name (Call_Node), Parent_Subp);
2948 -- Inspect all formals of derived subprogram Subp. Compare parameter
2949 -- types with the parent subprogram and check whether an actual may
2950 -- need a type conversion to the corresponding formal of the parent
2953 -- Not clear whether intrinsic subprograms need such conversions. ???
2955 if not Is_Intrinsic_Subprogram (Parent_Subp)
2956 or else Is_Generic_Instance (Parent_Subp)
2959 procedure Convert (Act : Node_Id; Typ : Entity_Id);
2960 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
2961 -- and resolve the newly generated construct.
2967 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
2969 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
2976 Actual_Typ : Entity_Id;
2977 Formal_Typ : Entity_Id;
2978 Parent_Typ : Entity_Id;
2981 Actual := First_Actual (Call_Node);
2982 Formal := First_Formal (Subp);
2983 Parent_Formal := First_Formal (Parent_Subp);
2984 while Present (Formal) loop
2985 Actual_Typ := Etype (Actual);
2986 Formal_Typ := Etype (Formal);
2987 Parent_Typ := Etype (Parent_Formal);
2989 -- For an IN parameter of a scalar type, the parent formal
2990 -- type and derived formal type differ or the parent formal
2991 -- type and actual type do not match statically.
2993 if Is_Scalar_Type (Formal_Typ)
2994 and then Ekind (Formal) = E_In_Parameter
2995 and then Formal_Typ /= Parent_Typ
2997 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
2998 and then not Raises_Constraint_Error (Actual)
3000 Convert (Actual, Parent_Typ);
3001 Enable_Range_Check (Actual);
3003 -- If the actual has been marked as requiring a range
3004 -- check, then generate it here.
3006 if Do_Range_Check (Actual) then
3007 Set_Do_Range_Check (Actual, False);
3008 Generate_Range_Check
3009 (Actual, Etype (Formal), CE_Range_Check_Failed);
3012 -- For access types, the parent formal type and actual type
3015 elsif Is_Access_Type (Formal_Typ)
3016 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3018 if Ekind (Formal) /= E_In_Parameter then
3019 Convert (Actual, Parent_Typ);
3021 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3022 and then Designated_Type (Parent_Typ) /=
3023 Designated_Type (Actual_Typ)
3024 and then not Is_Controlling_Formal (Formal)
3026 -- This unchecked conversion is not necessary unless
3027 -- inlining is enabled, because in that case the type
3028 -- mismatch may become visible in the body about to be
3032 Unchecked_Convert_To (Parent_Typ,
3033 Relocate_Node (Actual)));
3035 Resolve (Actual, Parent_Typ);
3038 -- For array and record types, the parent formal type and
3039 -- derived formal type have different sizes or pragma Pack
3042 elsif ((Is_Array_Type (Formal_Typ)
3043 and then Is_Array_Type (Parent_Typ))
3045 (Is_Record_Type (Formal_Typ)
3046 and then Is_Record_Type (Parent_Typ)))
3048 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3049 or else Has_Pragma_Pack (Formal_Typ) /=
3050 Has_Pragma_Pack (Parent_Typ))
3052 Convert (Actual, Parent_Typ);
3055 Next_Actual (Actual);
3056 Next_Formal (Formal);
3057 Next_Formal (Parent_Formal);
3063 Subp := Parent_Subp;
3066 -- Check for violation of No_Abort_Statements
3068 if Restriction_Check_Required (No_Abort_Statements)
3069 and then Is_RTE (Subp, RE_Abort_Task)
3071 Check_Restriction (No_Abort_Statements, Call_Node);
3073 -- Check for violation of No_Dynamic_Attachment
3075 elsif Restriction_Check_Required (No_Dynamic_Attachment)
3076 and then RTU_Loaded (Ada_Interrupts)
3077 and then (Is_RTE (Subp, RE_Is_Reserved) or else
3078 Is_RTE (Subp, RE_Is_Attached) or else
3079 Is_RTE (Subp, RE_Current_Handler) or else
3080 Is_RTE (Subp, RE_Attach_Handler) or else
3081 Is_RTE (Subp, RE_Exchange_Handler) or else
3082 Is_RTE (Subp, RE_Detach_Handler) or else
3083 Is_RTE (Subp, RE_Reference))
3085 Check_Restriction (No_Dynamic_Attachment, Call_Node);
3088 -- Deal with case where call is an explicit dereference
3090 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3092 -- Handle case of access to protected subprogram type
3094 if Is_Access_Protected_Subprogram_Type
3095 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3097 -- If this is a call through an access to protected operation, the
3098 -- prefix has the form (object'address, operation'access). Rewrite
3099 -- as a for other protected calls: the object is the 1st parameter
3100 -- of the list of actuals.
3107 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3109 T : constant Entity_Id :=
3110 Equivalent_Type (Base_Type (Etype (Ptr)));
3112 D_T : constant Entity_Id :=
3113 Designated_Type (Base_Type (Etype (Ptr)));
3117 Make_Selected_Component (Loc,
3118 Prefix => Unchecked_Convert_To (T, Ptr),
3120 New_Occurrence_Of (First_Entity (T), Loc));
3123 Make_Selected_Component (Loc,
3124 Prefix => Unchecked_Convert_To (T, Ptr),
3126 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3129 Make_Explicit_Dereference (Loc,
3132 if Present (Parameter_Associations (Call_Node)) then
3133 Parm := Parameter_Associations (Call_Node);
3138 Prepend (Obj, Parm);
3140 if Etype (D_T) = Standard_Void_Type then
3142 Make_Procedure_Call_Statement (Loc,
3144 Parameter_Associations => Parm);
3147 Make_Function_Call (Loc,
3149 Parameter_Associations => Parm);
3152 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3153 Set_Etype (Call, Etype (D_T));
3155 -- We do not re-analyze the call to avoid infinite recursion.
3156 -- We analyze separately the prefix and the object, and set
3157 -- the checks on the prefix that would otherwise be emitted
3158 -- when resolving a call.
3160 Rewrite (Call_Node, Call);
3162 Apply_Access_Check (Nam);
3169 -- If this is a call to an intrinsic subprogram, then perform the
3170 -- appropriate expansion to the corresponding tree node and we
3171 -- are all done (since after that the call is gone!)
3173 -- In the case where the intrinsic is to be processed by the back end,
3174 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3175 -- since the idea in this case is to pass the call unchanged. If the
3176 -- intrinsic is an inherited unchecked conversion, and the derived type
3177 -- is the target type of the conversion, we must retain it as the return
3178 -- type of the expression. Otherwise the expansion below, which uses the
3179 -- parent operation, will yield the wrong type.
3181 if Is_Intrinsic_Subprogram (Subp) then
3182 Expand_Intrinsic_Call (Call_Node, Subp);
3184 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3185 and then Parent_Subp /= Orig_Subp
3186 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3188 Set_Etype (Call_Node, Etype (Orig_Subp));
3194 if Ekind_In (Subp, E_Function, E_Procedure) then
3196 -- We perform two simple optimization on calls:
3198 -- a) replace calls to null procedures unconditionally;
3200 -- b) for To_Address, just do an unchecked conversion. Not only is
3201 -- this efficient, but it also avoids order of elaboration problems
3202 -- when address clauses are inlined (address expression elaborated
3203 -- at the wrong point).
3205 -- We perform these optimization regardless of whether we are in the
3206 -- main unit or in a unit in the context of the main unit, to ensure
3207 -- that tree generated is the same in both cases, for Inspector use.
3209 if Is_RTE (Subp, RE_To_Address) then
3211 Unchecked_Convert_To
3212 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3215 elsif Is_Null_Procedure (Subp) then
3216 Rewrite (Call_Node, Make_Null_Statement (Loc));
3220 if Is_Inlined (Subp) then
3222 Inlined_Subprogram : declare
3224 Must_Inline : Boolean := False;
3225 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3226 Scop : constant Entity_Id := Scope (Subp);
3228 function In_Unfrozen_Instance return Boolean;
3229 -- If the subprogram comes from an instance in the same unit,
3230 -- and the instance is not yet frozen, inlining might trigger
3231 -- order-of-elaboration problems in gigi.
3233 --------------------------
3234 -- In_Unfrozen_Instance --
3235 --------------------------
3237 function In_Unfrozen_Instance return Boolean is
3243 and then S /= Standard_Standard
3245 if Is_Generic_Instance (S)
3246 and then Present (Freeze_Node (S))
3247 and then not Analyzed (Freeze_Node (S))
3256 end In_Unfrozen_Instance;
3258 -- Start of processing for Inlined_Subprogram
3261 -- Verify that the body to inline has already been seen, and
3262 -- that if the body is in the current unit the inlining does
3263 -- not occur earlier. This avoids order-of-elaboration problems
3266 -- This should be documented in sinfo/einfo ???
3269 or else Nkind (Spec) /= N_Subprogram_Declaration
3270 or else No (Body_To_Inline (Spec))
3272 Must_Inline := False;
3274 -- If this an inherited function that returns a private type,
3275 -- do not inline if the full view is an unconstrained array,
3276 -- because such calls cannot be inlined.
3278 elsif Present (Orig_Subp)
3279 and then Is_Array_Type (Etype (Orig_Subp))
3280 and then not Is_Constrained (Etype (Orig_Subp))
3282 Must_Inline := False;
3284 elsif In_Unfrozen_Instance then
3285 Must_Inline := False;
3288 Bod := Body_To_Inline (Spec);
3290 if (In_Extended_Main_Code_Unit (Call_Node)
3291 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3292 or else Has_Pragma_Inline_Always (Subp))
3293 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3295 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3297 Must_Inline := True;
3299 -- If we are compiling a package body that is not the main
3300 -- unit, it must be for inlining/instantiation purposes,
3301 -- in which case we inline the call to insure that the same
3302 -- temporaries are generated when compiling the body by
3303 -- itself. Otherwise link errors can occur.
3305 -- If the function being called is itself in the main unit,
3306 -- we cannot inline, because there is a risk of double
3307 -- elaboration and/or circularity: the inlining can make
3308 -- visible a private entity in the body of the main unit,
3309 -- that gigi will see before its sees its proper definition.
3311 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3312 and then In_Package_Body
3314 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3319 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3322 -- Let the back end handle it
3324 Add_Inlined_Body (Subp);
3326 if Front_End_Inlining
3327 and then Nkind (Spec) = N_Subprogram_Declaration
3328 and then (In_Extended_Main_Code_Unit (Call_Node))
3329 and then No (Body_To_Inline (Spec))
3330 and then not Has_Completion (Subp)
3331 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3334 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
3337 end Inlined_Subprogram;
3341 -- Check for protected subprogram. This is either an intra-object call,
3342 -- or a protected function call. Protected procedure calls are rewritten
3343 -- as entry calls and handled accordingly.
3345 -- In Ada 2005, this may be an indirect call to an access parameter that
3346 -- is an access_to_subprogram. In that case the anonymous type has a
3347 -- scope that is a protected operation, but the call is a regular one.
3348 -- In either case do not expand call if subprogram is eliminated.
3350 Scop := Scope (Subp);
3352 if Nkind (Call_Node) /= N_Entry_Call_Statement
3353 and then Is_Protected_Type (Scop)
3354 and then Ekind (Subp) /= E_Subprogram_Type
3355 and then not Is_Eliminated (Subp)
3357 -- If the call is an internal one, it is rewritten as a call to the
3358 -- corresponding unprotected subprogram.
3360 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3363 -- Functions returning controlled objects need special attention. If
3364 -- the return type is limited, then the context is initialization and
3365 -- different processing applies. If the call is to a protected function,
3366 -- the expansion above will call Expand_Call recursively. Otherwise the
3367 -- function call is transformed into a temporary which obtains the
3368 -- result from the secondary stack.
3370 if Needs_Finalization (Etype (Subp)) then
3371 if not Is_Immutably_Limited_Type (Etype (Subp))
3373 (No (First_Formal (Subp))
3375 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3377 Expand_Ctrl_Function_Call (Call_Node);
3379 -- Build-in-place function calls which appear in anonymous contexts
3380 -- need a transient scope to ensure the proper finalization of the
3381 -- intermediate result after its use.
3383 elsif Is_Build_In_Place_Function_Call (Call_Node)
3384 and then Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3386 N_Indexed_Component,
3387 N_Object_Renaming_Declaration,
3388 N_Procedure_Call_Statement,
3389 N_Selected_Component,
3392 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3396 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3397 -- if there are optional parameters at the trailing end.
3398 -- Note: we never delete procedures for call via a pointer.
3400 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3401 and then Present (First_Optional_Parameter (Subp))
3404 Last_Keep_Arg : Node_Id;
3407 -- Last_Keep_Arg will hold the last actual that should be kept.
3408 -- If it remains empty at the end, it means that all parameters
3411 Last_Keep_Arg := Empty;
3413 -- Find first optional parameter, must be present since we checked
3414 -- the validity of the parameter before setting it.
3416 Formal := First_Formal (Subp);
3417 Actual := First_Actual (Call_Node);
3418 while Formal /= First_Optional_Parameter (Subp) loop
3419 Last_Keep_Arg := Actual;
3420 Next_Formal (Formal);
3421 Next_Actual (Actual);
3424 -- We have Formal and Actual pointing to the first potentially
3425 -- droppable argument. We can drop all the trailing arguments
3426 -- whose actual matches the default. Note that we know that all
3427 -- remaining formals have defaults, because we checked that this
3428 -- requirement was met before setting First_Optional_Parameter.
3430 -- We use Fully_Conformant_Expressions to check for identity
3431 -- between formals and actuals, which may miss some cases, but
3432 -- on the other hand, this is only an optimization (if we fail
3433 -- to truncate a parameter it does not affect functionality).
3434 -- So if the default is 3 and the actual is 1+2, we consider
3435 -- them unequal, which hardly seems worrisome.
3437 while Present (Formal) loop
3438 if not Fully_Conformant_Expressions
3439 (Actual, Default_Value (Formal))
3441 Last_Keep_Arg := Actual;
3444 Next_Formal (Formal);
3445 Next_Actual (Actual);
3448 -- If no arguments, delete entire list, this is the easy case
3450 if No (Last_Keep_Arg) then
3451 Set_Parameter_Associations (Call_Node, No_List);
3452 Set_First_Named_Actual (Call_Node, Empty);
3454 -- Case where at the last retained argument is positional. This
3455 -- is also an easy case, since the retained arguments are already
3456 -- in the right form, and we don't need to worry about the order
3457 -- of arguments that get eliminated.
3459 elsif Is_List_Member (Last_Keep_Arg) then
3460 while Present (Next (Last_Keep_Arg)) loop
3461 Discard_Node (Remove_Next (Last_Keep_Arg));
3464 Set_First_Named_Actual (Call_Node, Empty);
3466 -- This is the annoying case where the last retained argument
3467 -- is a named parameter. Since the original arguments are not
3468 -- in declaration order, we may have to delete some fairly
3469 -- random collection of arguments.
3477 -- First step, remove all the named parameters from the
3478 -- list (they are still chained using First_Named_Actual
3479 -- and Next_Named_Actual, so we have not lost them!)
3481 Temp := First (Parameter_Associations (Call_Node));
3483 -- Case of all parameters named, remove them all
3485 if Nkind (Temp) = N_Parameter_Association then
3486 -- Suppress warnings to avoid warning on possible
3487 -- infinite loop (because Call_Node is not modified).
3489 pragma Warnings (Off);
3490 while Is_Non_Empty_List
3491 (Parameter_Associations (Call_Node))
3494 Remove_Head (Parameter_Associations (Call_Node));
3496 pragma Warnings (On);
3498 -- Case of mixed positional/named, remove named parameters
3501 while Nkind (Next (Temp)) /= N_Parameter_Association loop
3505 while Present (Next (Temp)) loop
3506 Remove (Next (Temp));
3510 -- Now we loop through the named parameters, till we get
3511 -- to the last one to be retained, adding them to the list.
3512 -- Note that the Next_Named_Actual list does not need to be
3513 -- touched since we are only reordering them on the actual
3514 -- parameter association list.
3516 Passoc := Parent (First_Named_Actual (Call_Node));
3518 Temp := Relocate_Node (Passoc);
3520 (Parameter_Associations (Call_Node), Temp);
3522 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
3523 Passoc := Parent (Next_Named_Actual (Passoc));
3526 Set_Next_Named_Actual (Temp, Empty);
3529 Temp := Next_Named_Actual (Passoc);
3530 exit when No (Temp);
3531 Set_Next_Named_Actual
3532 (Passoc, Next_Named_Actual (Parent (Temp)));
3541 -------------------------------
3542 -- Expand_Ctrl_Function_Call --
3543 -------------------------------
3545 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
3547 -- Optimization, if the returned value (which is on the sec-stack) is
3548 -- returned again, no need to copy/readjust/finalize, we can just pass
3549 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
3550 -- attachment is needed
3552 if Nkind (Parent (N)) = N_Simple_Return_Statement then
3556 -- Resolution is now finished, make sure we don't start analysis again
3557 -- because of the duplication.
3561 -- A function which returns a controlled object uses the secondary
3562 -- stack. Rewrite the call into a temporary which obtains the result of
3563 -- the function using 'reference.
3565 Remove_Side_Effects (N);
3566 end Expand_Ctrl_Function_Call;
3568 --------------------------
3569 -- Expand_Inlined_Call --
3570 --------------------------
3572 procedure Expand_Inlined_Call
3575 Orig_Subp : Entity_Id)
3577 Loc : constant Source_Ptr := Sloc (N);
3578 Is_Predef : constant Boolean :=
3579 Is_Predefined_File_Name
3580 (Unit_File_Name (Get_Source_Unit (Subp)));
3581 Orig_Bod : constant Node_Id :=
3582 Body_To_Inline (Unit_Declaration_Node (Subp));
3587 Decls : constant List_Id := New_List;
3588 Exit_Lab : Entity_Id := Empty;
3595 Ret_Type : Entity_Id;
3599 Temp_Typ : Entity_Id;
3601 Return_Object : Entity_Id := Empty;
3602 -- Entity in declaration in an extended_return_statement
3604 Is_Unc : constant Boolean :=
3605 Is_Array_Type (Etype (Subp))
3606 and then not Is_Constrained (Etype (Subp));
3607 -- If the type returned by the function is unconstrained and the call
3608 -- can be inlined, special processing is required.
3610 procedure Make_Exit_Label;
3611 -- Build declaration for exit label to be used in Return statements,
3612 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3613 -- declaration). Does nothing if Exit_Lab already set.
3615 function Process_Formals (N : Node_Id) return Traverse_Result;
3616 -- Replace occurrence of a formal with the corresponding actual, or the
3617 -- thunk generated for it.
3619 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
3620 -- If the call being expanded is that of an internal subprogram, set the
3621 -- sloc of the generated block to that of the call itself, so that the
3622 -- expansion is skipped by the "next" command in gdb.
3623 -- Same processing for a subprogram in a predefined file, e.g.
3624 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
3625 -- simplify our own development.
3627 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3628 -- If the function body is a single expression, replace call with
3629 -- expression, else insert block appropriately.
3631 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3632 -- If procedure body has no local variables, inline body without
3633 -- creating block, otherwise rewrite call with block.
3635 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
3636 -- Determine whether a formal parameter is used only once in Orig_Bod
3638 ---------------------
3639 -- Make_Exit_Label --
3640 ---------------------
3642 procedure Make_Exit_Label is
3643 Lab_Ent : Entity_Id;
3645 if No (Exit_Lab) then
3646 Lab_Ent := Make_Temporary (Loc, 'L');
3647 Lab_Id := New_Reference_To (Lab_Ent, Loc);
3648 Exit_Lab := Make_Label (Loc, Lab_Id);
3650 Make_Implicit_Label_Declaration (Loc,
3651 Defining_Identifier => Lab_Ent,
3652 Label_Construct => Exit_Lab);
3654 end Make_Exit_Label;
3656 ---------------------
3657 -- Process_Formals --
3658 ---------------------
3660 function Process_Formals (N : Node_Id) return Traverse_Result is
3666 if Is_Entity_Name (N)
3667 and then Present (Entity (N))
3672 and then Scope (E) = Subp
3674 A := Renamed_Object (E);
3676 -- Rewrite the occurrence of the formal into an occurrence of
3677 -- the actual. Also establish visibility on the proper view of
3678 -- the actual's subtype for the body's context (if the actual's
3679 -- subtype is private at the call point but its full view is
3680 -- visible to the body, then the inlined tree here must be
3681 -- analyzed with the full view).
3683 if Is_Entity_Name (A) then
3684 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
3685 Check_Private_View (N);
3687 elsif Nkind (A) = N_Defining_Identifier then
3688 Rewrite (N, New_Occurrence_Of (A, Loc));
3689 Check_Private_View (N);
3694 Rewrite (N, New_Copy (A));
3699 elsif Is_Entity_Name (N)
3700 and then Present (Return_Object)
3701 and then Chars (N) = Chars (Return_Object)
3703 -- Occurrence within an extended return statement. The return
3704 -- object is local to the body been inlined, and thus the generic
3705 -- copy is not analyzed yet, so we match by name, and replace it
3706 -- with target of call.
3708 if Nkind (Targ) = N_Defining_Identifier then
3709 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3711 Rewrite (N, New_Copy_Tree (Targ));
3716 elsif Nkind (N) = N_Simple_Return_Statement then
3717 if No (Expression (N)) then
3720 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3723 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3724 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3726 -- Function body is a single expression. No need for
3732 Num_Ret := Num_Ret + 1;
3736 -- Because of the presence of private types, the views of the
3737 -- expression and the context may be different, so place an
3738 -- unchecked conversion to the context type to avoid spurious
3739 -- errors, e.g. when the expression is a numeric literal and
3740 -- the context is private. If the expression is an aggregate,
3741 -- use a qualified expression, because an aggregate is not a
3742 -- legal argument of a conversion.
3744 if Nkind_In (Expression (N), N_Aggregate, N_Null) then
3746 Make_Qualified_Expression (Sloc (N),
3747 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3748 Expression => Relocate_Node (Expression (N)));
3751 Unchecked_Convert_To
3752 (Ret_Type, Relocate_Node (Expression (N)));
3755 if Nkind (Targ) = N_Defining_Identifier then
3757 Make_Assignment_Statement (Loc,
3758 Name => New_Occurrence_Of (Targ, Loc),
3759 Expression => Ret));
3762 Make_Assignment_Statement (Loc,
3763 Name => New_Copy (Targ),
3764 Expression => Ret));
3767 Set_Assignment_OK (Name (N));
3769 if Present (Exit_Lab) then
3771 Make_Goto_Statement (Loc,
3772 Name => New_Copy (Lab_Id)));
3778 elsif Nkind (N) = N_Extended_Return_Statement then
3780 -- An extended return becomes a block whose first statement is
3781 -- the assignment of the initial expression of the return object
3782 -- to the target of the call itself.
3785 Return_Decl : constant Entity_Id :=
3786 First (Return_Object_Declarations (N));
3790 Return_Object := Defining_Identifier (Return_Decl);
3792 if Present (Expression (Return_Decl)) then
3793 if Nkind (Targ) = N_Defining_Identifier then
3795 Make_Assignment_Statement (Loc,
3796 Name => New_Occurrence_Of (Targ, Loc),
3797 Expression => Expression (Return_Decl));
3800 Make_Assignment_Statement (Loc,
3801 Name => New_Copy (Targ),
3802 Expression => Expression (Return_Decl));
3805 Set_Assignment_OK (Name (Assign));
3807 Statements (Handled_Statement_Sequence (N)));
3811 Make_Block_Statement (Loc,
3812 Handled_Statement_Sequence =>
3813 Handled_Statement_Sequence (N)));
3818 -- Remove pragma Unreferenced since it may refer to formals that
3819 -- are not visible in the inlined body, and in any case we will
3820 -- not be posting warnings on the inlined body so it is unneeded.
3822 elsif Nkind (N) = N_Pragma
3823 and then Pragma_Name (N) = Name_Unreferenced
3825 Rewrite (N, Make_Null_Statement (Sloc (N)));
3831 end Process_Formals;
3833 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3839 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3841 if not Debug_Generated_Code then
3842 Set_Sloc (Nod, Sloc (N));
3843 Set_Comes_From_Source (Nod, False);
3849 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3851 ---------------------------
3852 -- Rewrite_Function_Call --
3853 ---------------------------
3855 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
3856 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3857 Fst : constant Node_Id := First (Statements (HSS));
3860 -- Optimize simple case: function body is a single return statement,
3861 -- which has been expanded into an assignment.
3863 if Is_Empty_List (Declarations (Blk))
3864 and then Nkind (Fst) = N_Assignment_Statement
3865 and then No (Next (Fst))
3868 -- The function call may have been rewritten as the temporary
3869 -- that holds the result of the call, in which case remove the
3870 -- now useless declaration.
3872 if Nkind (N) = N_Identifier
3873 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3875 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3878 Rewrite (N, Expression (Fst));
3880 elsif Nkind (N) = N_Identifier
3881 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3883 -- The block assigns the result of the call to the temporary
3885 Insert_After (Parent (Entity (N)), Blk);
3887 elsif Nkind (Parent (N)) = N_Assignment_Statement
3889 (Is_Entity_Name (Name (Parent (N)))
3891 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3892 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
3894 -- Replace assignment with the block
3897 Original_Assignment : constant Node_Id := Parent (N);
3900 -- Preserve the original assignment node to keep the complete
3901 -- assignment subtree consistent enough for Analyze_Assignment
3902 -- to proceed (specifically, the original Lhs node must still
3903 -- have an assignment statement as its parent).
3905 -- We cannot rely on Original_Node to go back from the block
3906 -- node to the assignment node, because the assignment might
3907 -- already be a rewrite substitution.
3909 Discard_Node (Relocate_Node (Original_Assignment));
3910 Rewrite (Original_Assignment, Blk);
3913 elsif Nkind (Parent (N)) = N_Object_Declaration then
3914 Set_Expression (Parent (N), Empty);
3915 Insert_After (Parent (N), Blk);
3918 Insert_Before (Parent (N), Blk);
3920 end Rewrite_Function_Call;
3922 ----------------------------
3923 -- Rewrite_Procedure_Call --
3924 ----------------------------
3926 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
3927 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3929 -- If there is a transient scope for N, this will be the scope of the
3930 -- actions for N, and the statements in Blk need to be within this
3931 -- scope. For example, they need to have visibility on the constant
3932 -- declarations created for the formals.
3934 -- If N needs no transient scope, and if there are no declarations in
3935 -- the inlined body, we can do a little optimization and insert the
3936 -- statements for the body directly after N, and rewrite N to a
3937 -- null statement, instead of rewriting N into a full-blown block
3940 if not Scope_Is_Transient
3941 and then Is_Empty_List (Declarations (Blk))
3943 Insert_List_After (N, Statements (HSS));
3944 Rewrite (N, Make_Null_Statement (Loc));
3948 end Rewrite_Procedure_Call;
3950 -------------------------
3951 -- Formal_Is_Used_Once --
3952 -------------------------
3954 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
3955 Use_Counter : Int := 0;
3957 function Count_Uses (N : Node_Id) return Traverse_Result;
3958 -- Traverse the tree and count the uses of the formal parameter.
3959 -- In this case, for optimization purposes, we do not need to
3960 -- continue the traversal once more than one use is encountered.
3966 function Count_Uses (N : Node_Id) return Traverse_Result is
3968 -- The original node is an identifier
3970 if Nkind (N) = N_Identifier
3971 and then Present (Entity (N))
3973 -- Original node's entity points to the one in the copied body
3975 and then Nkind (Entity (N)) = N_Identifier
3976 and then Present (Entity (Entity (N)))
3978 -- The entity of the copied node is the formal parameter
3980 and then Entity (Entity (N)) = Formal
3982 Use_Counter := Use_Counter + 1;
3984 if Use_Counter > 1 then
3986 -- Denote more than one use and abandon the traversal
3997 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
3999 -- Start of processing for Formal_Is_Used_Once
4002 Count_Formal_Uses (Orig_Bod);
4003 return Use_Counter = 1;
4004 end Formal_Is_Used_Once;
4006 -- Start of processing for Expand_Inlined_Call
4010 -- Check for an illegal attempt to inline a recursive procedure. If the
4011 -- subprogram has parameters this is detected when trying to supply a
4012 -- binding for parameters that already have one. For parameterless
4013 -- subprograms this must be done explicitly.
4015 if In_Open_Scopes (Subp) then
4016 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
4017 Set_Is_Inlined (Subp, False);
4021 if Nkind (Orig_Bod) = N_Defining_Identifier
4022 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
4024 -- Subprogram is renaming_as_body. Calls occurring after the renaming
4025 -- can be replaced with calls to the renamed entity directly, because
4026 -- the subprograms are subtype conformant. If the renamed subprogram
4027 -- is an inherited operation, we must redo the expansion because
4028 -- implicit conversions may be needed. Similarly, if the renamed
4029 -- entity is inlined, expand the call for further optimizations.
4031 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
4033 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
4040 -- Use generic machinery to copy body of inlined subprogram, as if it
4041 -- were an instantiation, resetting source locations appropriately, so
4042 -- that nested inlined calls appear in the main unit.
4044 Save_Env (Subp, Empty);
4045 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
4047 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
4049 Make_Block_Statement (Loc,
4050 Declarations => Declarations (Bod),
4051 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
4053 if No (Declarations (Bod)) then
4054 Set_Declarations (Blk, New_List);
4057 -- For the unconstrained case, capture the name of the local
4058 -- variable that holds the result. This must be the first declaration
4059 -- in the block, because its bounds cannot depend on local variables.
4060 -- Otherwise there is no way to declare the result outside of the
4061 -- block. Needless to say, in general the bounds will depend on the
4062 -- actuals in the call.
4065 Targ1 := Defining_Identifier (First (Declarations (Blk)));
4068 -- If this is a derived function, establish the proper return type
4070 if Present (Orig_Subp)
4071 and then Orig_Subp /= Subp
4073 Ret_Type := Etype (Orig_Subp);
4075 Ret_Type := Etype (Subp);
4078 -- Create temporaries for the actuals that are expressions, or that
4079 -- are scalars and require copying to preserve semantics.
4081 F := First_Formal (Subp);
4082 A := First_Actual (N);
4083 while Present (F) loop
4084 if Present (Renamed_Object (F)) then
4085 Error_Msg_N ("cannot inline call to recursive subprogram", N);
4089 -- If the argument may be a controlling argument in a call within
4090 -- the inlined body, we must preserve its classwide nature to insure
4091 -- that dynamic dispatching take place subsequently. If the formal
4092 -- has a constraint it must be preserved to retain the semantics of
4095 if Is_Class_Wide_Type (Etype (F))
4096 or else (Is_Access_Type (Etype (F))
4098 Is_Class_Wide_Type (Designated_Type (Etype (F))))
4100 Temp_Typ := Etype (F);
4102 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
4103 and then Etype (F) /= Base_Type (Etype (F))
4105 Temp_Typ := Etype (F);
4108 Temp_Typ := Etype (A);
4111 -- If the actual is a simple name or a literal, no need to
4112 -- create a temporary, object can be used directly.
4114 -- If the actual is a literal and the formal has its address taken,
4115 -- we cannot pass the literal itself as an argument, so its value
4116 -- must be captured in a temporary.
4118 if (Is_Entity_Name (A)
4120 (not Is_Scalar_Type (Etype (A))
4121 or else Ekind (Entity (A)) = E_Enumeration_Literal))
4123 -- When the actual is an identifier and the corresponding formal
4124 -- is used only once in the original body, the formal can be
4125 -- substituted directly with the actual parameter.
4127 or else (Nkind (A) = N_Identifier
4128 and then Formal_Is_Used_Once (F))
4131 (Nkind_In (A, N_Real_Literal,
4133 N_Character_Literal)
4134 and then not Address_Taken (F))
4136 if Etype (F) /= Etype (A) then
4138 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
4140 Set_Renamed_Object (F, A);
4144 Temp := Make_Temporary (Loc, 'C');
4146 -- If the actual for an in/in-out parameter is a view conversion,
4147 -- make it into an unchecked conversion, given that an untagged
4148 -- type conversion is not a proper object for a renaming.
4150 -- In-out conversions that involve real conversions have already
4151 -- been transformed in Expand_Actuals.
4153 if Nkind (A) = N_Type_Conversion
4154 and then Ekind (F) /= E_In_Parameter
4157 Make_Unchecked_Type_Conversion (Loc,
4158 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
4159 Expression => Relocate_Node (Expression (A)));
4161 elsif Etype (F) /= Etype (A) then
4162 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
4163 Temp_Typ := Etype (F);
4166 New_A := Relocate_Node (A);
4169 Set_Sloc (New_A, Sloc (N));
4171 -- If the actual has a by-reference type, it cannot be copied, so
4172 -- its value is captured in a renaming declaration. Otherwise
4173 -- declare a local constant initialized with the actual.
4175 -- We also use a renaming declaration for expressions of an array
4176 -- type that is not bit-packed, both for efficiency reasons and to
4177 -- respect the semantics of the call: in most cases the original
4178 -- call will pass the parameter by reference, and thus the inlined
4179 -- code will have the same semantics.
4181 if Ekind (F) = E_In_Parameter
4182 and then not Is_Limited_Type (Etype (A))
4183 and then not Is_Tagged_Type (Etype (A))
4185 (not Is_Array_Type (Etype (A))
4186 or else not Is_Object_Reference (A)
4187 or else Is_Bit_Packed_Array (Etype (A)))
4190 Make_Object_Declaration (Loc,
4191 Defining_Identifier => Temp,
4192 Constant_Present => True,
4193 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
4194 Expression => New_A);
4197 Make_Object_Renaming_Declaration (Loc,
4198 Defining_Identifier => Temp,
4199 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
4203 Append (Decl, Decls);
4204 Set_Renamed_Object (F, Temp);
4211 -- Establish target of function call. If context is not assignment or
4212 -- declaration, create a temporary as a target. The declaration for
4213 -- the temporary may be subsequently optimized away if the body is a
4214 -- single expression, or if the left-hand side of the assignment is
4215 -- simple enough, i.e. an entity or an explicit dereference of one.
4217 if Ekind (Subp) = E_Function then
4218 if Nkind (Parent (N)) = N_Assignment_Statement
4219 and then Is_Entity_Name (Name (Parent (N)))
4221 Targ := Name (Parent (N));
4223 elsif Nkind (Parent (N)) = N_Assignment_Statement
4224 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4225 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4227 Targ := Name (Parent (N));
4229 elsif Nkind (Parent (N)) = N_Object_Declaration
4230 and then Is_Limited_Type (Etype (Subp))
4232 Targ := Defining_Identifier (Parent (N));
4235 -- Replace call with temporary and create its declaration
4237 Temp := Make_Temporary (Loc, 'C');
4238 Set_Is_Internal (Temp);
4240 -- For the unconstrained case, the generated temporary has the
4241 -- same constrained declaration as the result variable. It may
4242 -- eventually be possible to remove that temporary and use the
4243 -- result variable directly.
4247 Make_Object_Declaration (Loc,
4248 Defining_Identifier => Temp,
4249 Object_Definition =>
4250 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4252 Replace_Formals (Decl);
4256 Make_Object_Declaration (Loc,
4257 Defining_Identifier => Temp,
4258 Object_Definition =>
4259 New_Occurrence_Of (Ret_Type, Loc));
4261 Set_Etype (Temp, Ret_Type);
4264 Set_No_Initialization (Decl);
4265 Append (Decl, Decls);
4266 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4271 Insert_Actions (N, Decls);
4273 -- Traverse the tree and replace formals with actuals or their thunks.
4274 -- Attach block to tree before analysis and rewriting.
4276 Replace_Formals (Blk);
4277 Set_Parent (Blk, N);
4279 if not Comes_From_Source (Subp)
4285 if Present (Exit_Lab) then
4287 -- If the body was a single expression, the single return statement
4288 -- and the corresponding label are useless.
4292 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4295 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4297 Append (Lab_Decl, (Declarations (Blk)));
4298 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
4302 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
4303 -- conflicting private views that Gigi would ignore. If this is a
4304 -- predefined unit, analyze with checks off, as is done in the non-
4305 -- inlined run-time units.
4308 I_Flag : constant Boolean := In_Inlined_Body;
4311 In_Inlined_Body := True;
4315 Style : constant Boolean := Style_Check;
4317 Style_Check := False;
4318 Analyze (Blk, Suppress => All_Checks);
4319 Style_Check := Style;
4326 In_Inlined_Body := I_Flag;
4329 if Ekind (Subp) = E_Procedure then
4330 Rewrite_Procedure_Call (N, Blk);
4332 Rewrite_Function_Call (N, Blk);
4334 -- For the unconstrained case, the replacement of the call has been
4335 -- made prior to the complete analysis of the generated declarations.
4336 -- Propagate the proper type now.
4339 if Nkind (N) = N_Identifier then
4340 Set_Etype (N, Etype (Entity (N)));
4342 Set_Etype (N, Etype (Targ1));
4349 -- Cleanup mapping between formals and actuals for other expansions
4351 F := First_Formal (Subp);
4352 while Present (F) loop
4353 Set_Renamed_Object (F, Empty);
4356 end Expand_Inlined_Call;
4358 ----------------------------------------
4359 -- Expand_N_Extended_Return_Statement --
4360 ----------------------------------------
4362 -- If there is a Handled_Statement_Sequence, we rewrite this:
4364 -- return Result : T := <expression> do
4365 -- <handled_seq_of_stms>
4371 -- Result : T := <expression>;
4373 -- <handled_seq_of_stms>
4377 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4379 -- return Result : T := <expression>;
4383 -- return <expression>;
4385 -- unless it's build-in-place or there's no <expression>, in which case
4389 -- Result : T := <expression>;
4394 -- Note that this case could have been written by the user as an extended
4395 -- return statement, or could have been transformed to this from a simple
4396 -- return statement.
4398 -- That is, we need to have a reified return object if there are statements
4399 -- (which might refer to it) or if we're doing build-in-place (so we can
4400 -- set its address to the final resting place or if there is no expression
4401 -- (in which case default initial values might need to be set).
4403 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4404 Loc : constant Source_Ptr := Sloc (N);
4406 Par_Func : constant Entity_Id :=
4407 Return_Applies_To (Return_Statement_Entity (N));
4408 Ret_Obj_Id : constant Entity_Id :=
4409 First_Entity (Return_Statement_Entity (N));
4410 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4412 Is_Build_In_Place : constant Boolean :=
4413 Is_Build_In_Place_Function (Par_Func);
4418 Return_Stmt : Node_Id;
4421 function Build_Heap_Allocator
4422 (Temp_Id : Entity_Id;
4423 Temp_Typ : Entity_Id;
4424 Func_Id : Entity_Id;
4425 Ret_Typ : Entity_Id;
4426 Alloc_Expr : Node_Id) return Node_Id;
4427 -- Create the statements necessary to allocate a return object on the
4428 -- caller's master. The master is available through implicit parameter
4429 -- BIPfinalizationmaster.
4431 -- if BIPfinalizationmaster /= null then
4433 -- type Ptr_Typ is access Ret_Typ;
4434 -- for Ptr_Typ'Storage_Pool use
4435 -- Base_Pool (BIPfinalizationmaster.all).all;
4439 -- procedure Allocate (...) is
4441 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4444 -- Local := <Alloc_Expr>;
4445 -- Temp_Id := Temp_Typ (Local);
4449 -- Temp_Id is the temporary which is used to reference the internally
4450 -- created object in all allocation forms. Temp_Typ is the type of the
4451 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4452 -- type of Func_Id. Alloc_Expr is the actual allocator.
4454 function Move_Activation_Chain return Node_Id;
4455 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4457 -- From current activation chain
4458 -- To activation chain passed in by the caller
4459 -- New_Master master passed in by the caller
4461 --------------------------
4462 -- Build_Heap_Allocator --
4463 --------------------------
4465 function Build_Heap_Allocator
4466 (Temp_Id : Entity_Id;
4467 Temp_Typ : Entity_Id;
4468 Func_Id : Entity_Id;
4469 Ret_Typ : Entity_Id;
4470 Alloc_Expr : Node_Id) return Node_Id
4473 -- Processing for build-in-place object allocation. This is disabled
4474 -- on .NET/JVM because the targets do not support pools.
4476 if VM_Target = No_VM
4477 and then Is_Build_In_Place_Function (Func_Id)
4478 and then Needs_Finalization (Ret_Typ)
4481 Decls : constant List_Id := New_List;
4482 Fin_Mas_Id : constant Entity_Id :=
4483 Build_In_Place_Formal
4484 (Func_Id, BIP_Finalization_Master);
4485 Stmts : constant List_Id := New_List;
4487 Local_Id : Entity_Id;
4488 Pool_Id : Entity_Id;
4489 Ptr_Typ : Entity_Id;
4493 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4495 Pool_Id := Make_Temporary (Loc, 'P');
4498 Make_Object_Renaming_Declaration (Loc,
4499 Defining_Identifier => Pool_Id,
4501 New_Reference_To (RTE (RE_Root_Storage_Pool), Loc),
4503 Make_Explicit_Dereference (Loc,
4505 Make_Function_Call (Loc,
4507 New_Reference_To (RTE (RE_Base_Pool), Loc),
4508 Parameter_Associations => New_List (
4509 Make_Explicit_Dereference (Loc,
4511 New_Reference_To (Fin_Mas_Id, Loc)))))));
4513 -- Create an access type which uses the storage pool of the
4514 -- caller's master. This additional type is necessary because
4515 -- the finalization master cannot be associated with the type
4516 -- of the temporary. Otherwise the secondary stack allocation
4520 -- type Ptr_Typ is access Ret_Typ;
4522 Ptr_Typ := Make_Temporary (Loc, 'P');
4525 Make_Full_Type_Declaration (Loc,
4526 Defining_Identifier => Ptr_Typ,
4528 Make_Access_To_Object_Definition (Loc,
4529 Subtype_Indication =>
4530 New_Reference_To (Ret_Typ, Loc))));
4532 -- Perform minor decoration in order to set the master and the
4533 -- storage pool attributes.
4535 Set_Ekind (Ptr_Typ, E_Access_Type);
4536 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4537 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4539 -- Create the temporary, generate:
4541 -- Local_Id : Ptr_Typ;
4543 Local_Id := Make_Temporary (Loc, 'T');
4546 Make_Object_Declaration (Loc,
4547 Defining_Identifier => Local_Id,
4548 Object_Definition =>
4549 New_Reference_To (Ptr_Typ, Loc)));
4551 -- Allocate the object, generate:
4553 -- Local_Id := <Alloc_Expr>;
4556 Make_Assignment_Statement (Loc,
4557 Name => New_Reference_To (Local_Id, Loc),
4558 Expression => Alloc_Expr));
4561 -- Temp_Id := Temp_Typ (Local_Id);
4564 Make_Assignment_Statement (Loc,
4565 Name => New_Reference_To (Temp_Id, Loc),
4567 Unchecked_Convert_To (Temp_Typ,
4568 New_Reference_To (Local_Id, Loc))));
4570 -- Wrap the allocation in a block. This is further conditioned
4571 -- by checking the caller finalization master at runtime. A
4572 -- null value indicates a non-existent master, most likely due
4573 -- to a Finalize_Storage_Only allocation.
4576 -- if BIPfinalizationmaster /= null then
4585 Make_If_Statement (Loc,
4588 Left_Opnd => New_Reference_To (Fin_Mas_Id, Loc),
4589 Right_Opnd => Make_Null (Loc)),
4591 Then_Statements => New_List (
4592 Make_Block_Statement (Loc,
4593 Declarations => Decls,
4594 Handled_Statement_Sequence =>
4595 Make_Handled_Sequence_Of_Statements (Loc,
4596 Statements => Stmts))));
4599 -- For all other cases, generate:
4601 -- Temp_Id := <Alloc_Expr>;
4605 Make_Assignment_Statement (Loc,
4606 Name => New_Reference_To (Temp_Id, Loc),
4607 Expression => Alloc_Expr);
4609 end Build_Heap_Allocator;
4611 ---------------------------
4612 -- Move_Activation_Chain --
4613 ---------------------------
4615 function Move_Activation_Chain return Node_Id is
4616 Chain_Formal : constant Entity_Id :=
4617 Build_In_Place_Formal
4618 (Par_Func, BIP_Activation_Chain);
4619 To : constant Node_Id :=
4620 New_Reference_To (Chain_Formal, Loc);
4621 Master_Formal : constant Entity_Id :=
4622 Build_In_Place_Formal (Par_Func, BIP_Master);
4623 New_Master : constant Node_Id :=
4624 New_Reference_To (Master_Formal, Loc);
4626 Chain_Id : Entity_Id;
4630 Chain_Id := First_Entity (Return_Statement_Entity (N));
4631 while Chars (Chain_Id) /= Name_uChain loop
4632 Chain_Id := Next_Entity (Chain_Id);
4636 Make_Attribute_Reference (Loc,
4638 New_Reference_To (Chain_Id, Loc),
4639 Attribute_Name => Name_Unrestricted_Access);
4640 -- ??? Not clear why "Make_Identifier (Loc, Name_uChain)" doesn't
4641 -- work, instead of "New_Reference_To (Chain_Id, Loc)" above.
4644 Make_Procedure_Call_Statement (Loc,
4646 New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
4647 Parameter_Associations => New_List (From, To, New_Master));
4648 end Move_Activation_Chain;
4650 -- Start of processing for Expand_N_Extended_Return_Statement
4653 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4654 Exp := Expression (Ret_Obj_Decl);
4659 HSS := Handled_Statement_Sequence (N);
4661 -- If the returned object needs finalization actions, the function must
4662 -- perform the appropriate cleanup should it fail to return. The state
4663 -- of the function itself is tracked through a flag which is coupled
4664 -- with the scope finalizer. There is one flag per each return object
4665 -- in case of multiple returns.
4667 if Is_Build_In_Place
4668 and then Needs_Finalization (Etype (Ret_Obj_Id))
4671 Flag_Decl : Node_Id;
4672 Flag_Id : Entity_Id;
4676 -- Recover the function body
4678 Func_Bod := Unit_Declaration_Node (Par_Func);
4679 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4680 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4683 -- Create a flag to track the function state
4685 Flag_Id := Make_Temporary (Loc, 'F');
4686 Set_Return_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4688 -- Insert the flag at the beginning of the function declarations,
4690 -- Fnn : Boolean := False;
4693 Make_Object_Declaration (Loc,
4694 Defining_Identifier => Flag_Id,
4695 Object_Definition =>
4696 New_Reference_To (Standard_Boolean, Loc),
4697 Expression => New_Reference_To (Standard_False, Loc));
4699 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4700 Analyze (Flag_Decl);
4704 -- Build a simple_return_statement that returns the return object when
4705 -- there is a statement sequence, or no expression, or the result will
4706 -- be built in place. Note however that we currently do this for all
4707 -- composite cases, even though nonlimited composite results are not yet
4708 -- built in place (though we plan to do so eventually).
4711 or else Is_Composite_Type (Etype (Par_Func))
4717 -- If the extended return has a handled statement sequence, then wrap
4718 -- it in a block and use the block as the first statement.
4722 Make_Block_Statement (Loc,
4723 Declarations => New_List,
4724 Handled_Statement_Sequence => HSS));
4727 -- If the result type contains tasks, we call Move_Activation_Chain.
4728 -- Later, the cleanup code will call Complete_Master, which will
4729 -- terminate any unactivated tasks belonging to the return statement
4730 -- master. But Move_Activation_Chain updates their master to be that
4731 -- of the caller, so they will not be terminated unless the return
4732 -- statement completes unsuccessfully due to exception, abort, goto,
4733 -- or exit. As a formality, we test whether the function requires the
4734 -- result to be built in place, though that's necessarily true for
4735 -- the case of result types with task parts.
4737 if Is_Build_In_Place
4738 and then Has_Task (Etype (Par_Func))
4740 Append_To (Stmts, Move_Activation_Chain);
4743 -- Update the state of the function right before the object is
4746 if Is_Build_In_Place
4747 and then Needs_Finalization (Etype (Ret_Obj_Id))
4750 Flag_Id : constant Entity_Id :=
4751 Return_Flag_Or_Transient_Decl (Ret_Obj_Id);
4758 Make_Assignment_Statement (Loc,
4759 Name => New_Reference_To (Flag_Id, Loc),
4760 Expression => New_Reference_To (Standard_True, Loc)));
4764 -- Build a simple_return_statement that returns the return object
4767 Make_Simple_Return_Statement (Loc,
4768 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4769 Append_To (Stmts, Return_Stmt);
4771 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4774 -- Case where we build a return statement block
4776 if Present (HSS) then
4778 Make_Block_Statement (Loc,
4779 Declarations => Return_Object_Declarations (N),
4780 Handled_Statement_Sequence => HSS);
4782 -- We set the entity of the new block statement to be that of the
4783 -- return statement. This is necessary so that various fields, such
4784 -- as Finalization_Chain_Entity carry over from the return statement
4785 -- to the block. Note that this block is unusual, in that its entity
4786 -- is an E_Return_Statement rather than an E_Block.
4789 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4791 -- If the object decl was already rewritten as a renaming, then
4792 -- we don't want to do the object allocation and transformation of
4793 -- of the return object declaration to a renaming. This case occurs
4794 -- when the return object is initialized by a call to another
4795 -- build-in-place function, and that function is responsible for the
4796 -- allocation of the return object.
4798 if Is_Build_In_Place
4799 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4802 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4803 and then Is_Build_In_Place_Function_Call
4804 (Expression (Original_Node (Ret_Obj_Decl))));
4806 -- Return the build-in-place result by reference
4808 Set_By_Ref (Return_Stmt);
4810 elsif Is_Build_In_Place then
4812 -- Locate the implicit access parameter associated with the
4813 -- caller-supplied return object and convert the return
4814 -- statement's return object declaration to a renaming of a
4815 -- dereference of the access parameter. If the return object's
4816 -- declaration includes an expression that has not already been
4817 -- expanded as separate assignments, then add an assignment
4818 -- statement to ensure the return object gets initialized.
4821 -- Result : T [:= <expression>];
4828 -- Result : T renames FuncRA.all;
4829 -- [Result := <expression;]
4834 Return_Obj_Id : constant Entity_Id :=
4835 Defining_Identifier (Ret_Obj_Decl);
4836 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
4837 Return_Obj_Expr : constant Node_Id :=
4838 Expression (Ret_Obj_Decl);
4839 Result_Subt : constant Entity_Id := Etype (Par_Func);
4840 Constr_Result : constant Boolean :=
4841 Is_Constrained (Result_Subt);
4842 Obj_Alloc_Formal : Entity_Id;
4843 Object_Access : Entity_Id;
4844 Obj_Acc_Deref : Node_Id;
4845 Init_Assignment : Node_Id := Empty;
4848 -- Build-in-place results must be returned by reference
4850 Set_By_Ref (Return_Stmt);
4852 -- Retrieve the implicit access parameter passed by the caller
4855 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
4857 -- If the return object's declaration includes an expression
4858 -- and the declaration isn't marked as No_Initialization, then
4859 -- we need to generate an assignment to the object and insert
4860 -- it after the declaration before rewriting it as a renaming
4861 -- (otherwise we'll lose the initialization). The case where
4862 -- the result type is an interface (or class-wide interface)
4863 -- is also excluded because the context of the function call
4864 -- must be unconstrained, so the initialization will always
4865 -- be done as part of an allocator evaluation (storage pool
4866 -- or secondary stack), never to a constrained target object
4867 -- passed in by the caller. Besides the assignment being
4868 -- unneeded in this case, it avoids problems with trying to
4869 -- generate a dispatching assignment when the return expression
4870 -- is a nonlimited descendant of a limited interface (the
4871 -- interface has no assignment operation).
4873 if Present (Return_Obj_Expr)
4874 and then not No_Initialization (Ret_Obj_Decl)
4875 and then not Is_Interface (Return_Obj_Typ)
4878 Make_Assignment_Statement (Loc,
4879 Name => New_Reference_To (Return_Obj_Id, Loc),
4880 Expression => Relocate_Node (Return_Obj_Expr));
4882 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
4883 Set_Assignment_OK (Name (Init_Assignment));
4884 Set_No_Ctrl_Actions (Init_Assignment);
4886 Set_Parent (Name (Init_Assignment), Init_Assignment);
4887 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4889 Set_Expression (Ret_Obj_Decl, Empty);
4891 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
4892 and then not Is_Class_Wide_Type
4893 (Etype (Expression (Init_Assignment)))
4895 Rewrite (Expression (Init_Assignment),
4896 Make_Type_Conversion (Loc,
4898 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
4900 Relocate_Node (Expression (Init_Assignment))));
4903 -- In the case of functions where the calling context can
4904 -- determine the form of allocation needed, initialization
4905 -- is done with each part of the if statement that handles
4906 -- the different forms of allocation (this is true for
4907 -- unconstrained and tagged result subtypes).
4910 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
4912 Insert_After (Ret_Obj_Decl, Init_Assignment);
4916 -- When the function's subtype is unconstrained, a run-time
4917 -- test is needed to determine the form of allocation to use
4918 -- for the return object. The function has an implicit formal
4919 -- parameter indicating this. If the BIP_Alloc_Form formal has
4920 -- the value one, then the caller has passed access to an
4921 -- existing object for use as the return object. If the value
4922 -- is two, then the return object must be allocated on the
4923 -- secondary stack. Otherwise, the object must be allocated in
4924 -- a storage pool (currently only supported for the global
4925 -- heap, user-defined storage pools TBD ???). We generate an
4926 -- if statement to test the implicit allocation formal and
4927 -- initialize a local access value appropriately, creating
4928 -- allocators in the secondary stack and global heap cases.
4929 -- The special formal also exists and must be tested when the
4930 -- function has a tagged result, even when the result subtype
4931 -- is constrained, because in general such functions can be
4932 -- called in dispatching contexts and must be handled similarly
4933 -- to functions with a class-wide result.
4935 if not Constr_Result
4936 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
4939 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
4942 Ref_Type : Entity_Id;
4943 Ptr_Type_Decl : Node_Id;
4944 Alloc_Obj_Id : Entity_Id;
4945 Alloc_Obj_Decl : Node_Id;
4946 Alloc_If_Stmt : Node_Id;
4947 Heap_Allocator : Node_Id;
4948 SS_Allocator : Node_Id;
4951 -- Reuse the itype created for the function's implicit
4952 -- access formal. This avoids the need to create a new
4953 -- access type here, plus it allows assigning the access
4954 -- formal directly without applying a conversion.
4956 -- Ref_Type := Etype (Object_Access);
4958 -- Create an access type designating the function's
4961 Ref_Type := Make_Temporary (Loc, 'A');
4964 Make_Full_Type_Declaration (Loc,
4965 Defining_Identifier => Ref_Type,
4967 Make_Access_To_Object_Definition (Loc,
4968 All_Present => True,
4969 Subtype_Indication =>
4970 New_Reference_To (Return_Obj_Typ, Loc)));
4972 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
4974 -- Create an access object that will be initialized to an
4975 -- access value denoting the return object, either coming
4976 -- from an implicit access value passed in by the caller
4977 -- or from the result of an allocator.
4979 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4980 Set_Etype (Alloc_Obj_Id, Ref_Type);
4983 Make_Object_Declaration (Loc,
4984 Defining_Identifier => Alloc_Obj_Id,
4985 Object_Definition =>
4986 New_Reference_To (Ref_Type, Loc));
4988 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
4990 -- Create allocators for both the secondary stack and
4991 -- global heap. If there's an initialization expression,
4992 -- then create these as initialized allocators.
4994 if Present (Return_Obj_Expr)
4995 and then not No_Initialization (Ret_Obj_Decl)
4997 -- Always use the type of the expression for the
4998 -- qualified expression, rather than the result type.
4999 -- In general we cannot always use the result type
5000 -- for the allocator, because the expression might be
5001 -- of a specific type, such as in the case of an
5002 -- aggregate or even a nonlimited object when the
5003 -- result type is a limited class-wide interface type.
5006 Make_Allocator (Loc,
5008 Make_Qualified_Expression (Loc,
5011 (Etype (Return_Obj_Expr), Loc),
5013 New_Copy_Tree (Return_Obj_Expr)));
5016 -- If the function returns a class-wide type we cannot
5017 -- use the return type for the allocator. Instead we
5018 -- use the type of the expression, which must be an
5019 -- aggregate of a definite type.
5021 if Is_Class_Wide_Type (Return_Obj_Typ) then
5023 Make_Allocator (Loc,
5026 (Etype (Return_Obj_Expr), Loc));
5029 Make_Allocator (Loc,
5031 New_Reference_To (Return_Obj_Typ, Loc));
5034 -- If the object requires default initialization then
5035 -- that will happen later following the elaboration of
5036 -- the object renaming. If we don't turn it off here
5037 -- then the object will be default initialized twice.
5039 Set_No_Initialization (Heap_Allocator);
5042 -- If the No_Allocators restriction is active, then only
5043 -- an allocator for secondary stack allocation is needed.
5044 -- It's OK for such allocators to have Comes_From_Source
5045 -- set to False, because gigi knows not to flag them as
5046 -- being a violation of No_Implicit_Heap_Allocations.
5048 if Restriction_Active (No_Allocators) then
5049 SS_Allocator := Heap_Allocator;
5050 Heap_Allocator := Make_Null (Loc);
5052 -- Otherwise the heap allocator may be needed, so we make
5053 -- another allocator for secondary stack allocation.
5056 SS_Allocator := New_Copy_Tree (Heap_Allocator);
5058 -- The heap allocator is marked Comes_From_Source
5059 -- since it corresponds to an explicit user-written
5060 -- allocator (that is, it will only be executed on
5061 -- behalf of callers that call the function as
5062 -- initialization for such an allocator). This
5063 -- prevents errors when No_Implicit_Heap_Allocations
5066 Set_Comes_From_Source (Heap_Allocator, True);
5069 -- The allocator is returned on the secondary stack. We
5070 -- don't do this on VM targets, since the SS is not used.
5072 if VM_Target = No_VM then
5073 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5074 Set_Procedure_To_Call
5075 (SS_Allocator, RTE (RE_SS_Allocate));
5077 -- The allocator is returned on the secondary stack,
5078 -- so indicate that the function return, as well as
5079 -- the block that encloses the allocator, must not
5080 -- release it. The flags must be set now because the
5081 -- decision to use the secondary stack is done very
5082 -- late in the course of expanding the return
5083 -- statement, past the point where these flags are
5086 Set_Sec_Stack_Needed_For_Return (Par_Func);
5087 Set_Sec_Stack_Needed_For_Return
5088 (Return_Statement_Entity (N));
5089 Set_Uses_Sec_Stack (Par_Func);
5090 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5093 -- Create an if statement to test the BIP_Alloc_Form
5094 -- formal and initialize the access object to either the
5095 -- BIP_Object_Access formal (BIP_Alloc_Form = 0), the
5096 -- result of allocating the object in the secondary stack
5097 -- (BIP_Alloc_Form = 1), or else an allocator to create
5098 -- the return object in the heap (BIP_Alloc_Form = 2).
5100 -- ??? An unchecked type conversion must be made in the
5101 -- case of assigning the access object formal to the
5102 -- local access object, because a normal conversion would
5103 -- be illegal in some cases (such as converting access-
5104 -- to-unconstrained to access-to-constrained), but the
5105 -- the unchecked conversion will presumably fail to work
5106 -- right in just such cases. It's not clear at all how to
5110 Make_If_Statement (Loc,
5114 New_Reference_To (Obj_Alloc_Formal, Loc),
5116 Make_Integer_Literal (Loc,
5117 UI_From_Int (BIP_Allocation_Form'Pos
5118 (Caller_Allocation)))),
5120 Then_Statements => New_List (
5121 Make_Assignment_Statement (Loc,
5123 New_Reference_To (Alloc_Obj_Id, Loc),
5125 Make_Unchecked_Type_Conversion (Loc,
5127 New_Reference_To (Ref_Type, Loc),
5129 New_Reference_To (Object_Access, Loc)))),
5131 Elsif_Parts => New_List (
5132 Make_Elsif_Part (Loc,
5136 New_Reference_To (Obj_Alloc_Formal, Loc),
5138 Make_Integer_Literal (Loc,
5139 UI_From_Int (BIP_Allocation_Form'Pos
5140 (Secondary_Stack)))),
5142 Then_Statements => New_List (
5143 Make_Assignment_Statement (Loc,
5145 New_Reference_To (Alloc_Obj_Id, Loc),
5146 Expression => SS_Allocator)))),
5148 Else_Statements => New_List (
5149 Build_Heap_Allocator
5150 (Temp_Id => Alloc_Obj_Id,
5151 Temp_Typ => Ref_Type,
5152 Func_Id => Par_Func,
5153 Ret_Typ => Return_Obj_Typ,
5154 Alloc_Expr => Heap_Allocator)));
5156 -- If a separate initialization assignment was created
5157 -- earlier, append that following the assignment of the
5158 -- implicit access formal to the access object, to ensure
5159 -- that the return object is initialized in that case.
5160 -- In this situation, the target of the assignment must
5161 -- be rewritten to denote a dereference of the access to
5162 -- the return object passed in by the caller.
5164 if Present (Init_Assignment) then
5165 Rewrite (Name (Init_Assignment),
5166 Make_Explicit_Dereference (Loc,
5167 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
5170 (Name (Init_Assignment), Etype (Return_Obj_Id));
5173 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5176 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5178 -- Remember the local access object for use in the
5179 -- dereference of the renaming created below.
5181 Object_Access := Alloc_Obj_Id;
5185 -- Replace the return object declaration with a renaming of a
5186 -- dereference of the access value designating the return
5190 Make_Explicit_Dereference (Loc,
5191 Prefix => New_Reference_To (Object_Access, Loc));
5193 Rewrite (Ret_Obj_Decl,
5194 Make_Object_Renaming_Declaration (Loc,
5195 Defining_Identifier => Return_Obj_Id,
5196 Access_Definition => Empty,
5198 New_Occurrence_Of (Return_Obj_Typ, Loc),
5199 Name => Obj_Acc_Deref));
5201 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
5205 -- Case where we do not build a block
5208 -- We're about to drop Return_Object_Declarations on the floor, so
5209 -- we need to insert it, in case it got expanded into useful code.
5210 -- Remove side effects from expression, which may be duplicated in
5211 -- subsequent checks (see Expand_Simple_Function_Return).
5213 Insert_List_Before (N, Return_Object_Declarations (N));
5214 Remove_Side_Effects (Exp);
5216 -- Build simple_return_statement that returns the expression directly
5218 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5219 Result := Return_Stmt;
5222 -- Set the flag to prevent infinite recursion
5224 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5226 Rewrite (N, Result);
5228 end Expand_N_Extended_Return_Statement;
5230 ----------------------------
5231 -- Expand_N_Function_Call --
5232 ----------------------------
5234 procedure Expand_N_Function_Call (N : Node_Id) is
5238 -- If the return value of a foreign compiled function is VAX Float, then
5239 -- expand the return (adjusts the location of the return value on
5240 -- Alpha/VMS, no-op everywhere else).
5241 -- Comes_From_Source intercepts recursive expansion.
5243 if Vax_Float (Etype (N))
5244 and then Nkind (N) = N_Function_Call
5245 and then Present (Name (N))
5246 and then Present (Entity (Name (N)))
5247 and then Has_Foreign_Convention (Entity (Name (N)))
5248 and then Comes_From_Source (Parent (N))
5250 Expand_Vax_Foreign_Return (N);
5252 end Expand_N_Function_Call;
5254 ---------------------------------------
5255 -- Expand_N_Procedure_Call_Statement --
5256 ---------------------------------------
5258 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5261 end Expand_N_Procedure_Call_Statement;
5263 --------------------------------------
5264 -- Expand_N_Simple_Return_Statement --
5265 --------------------------------------
5267 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5269 -- Defend against previous errors (i.e. the return statement calls a
5270 -- function that is not available in configurable runtime).
5272 if Present (Expression (N))
5273 and then Nkind (Expression (N)) = N_Empty
5278 -- Distinguish the function and non-function cases:
5280 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5283 E_Generic_Function =>
5284 Expand_Simple_Function_Return (N);
5287 E_Generic_Procedure |
5290 E_Return_Statement =>
5291 Expand_Non_Function_Return (N);
5294 raise Program_Error;
5298 when RE_Not_Available =>
5300 end Expand_N_Simple_Return_Statement;
5302 ------------------------------
5303 -- Expand_N_Subprogram_Body --
5304 ------------------------------
5306 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5309 -- Add dummy push/pop label nodes at start and end to clear any local
5310 -- exception indications if local-exception-to-goto optimization is active.
5312 -- Add return statement if last statement in body is not a return statement
5313 -- (this makes things easier on Gigi which does not want to have to handle
5314 -- a missing return).
5316 -- Add call to Activate_Tasks if body is a task activator
5318 -- Deal with possible detection of infinite recursion
5320 -- Eliminate body completely if convention stubbed
5322 -- Encode entity names within body, since we will not need to reference
5323 -- these entities any longer in the front end.
5325 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5327 -- Reset Pure indication if any parameter has root type System.Address
5328 -- or has any parameters of limited types, where limited means that the
5329 -- run-time view is limited (i.e. the full type is limited).
5333 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5334 Loc : constant Source_Ptr := Sloc (N);
5335 H : constant Node_Id := Handled_Statement_Sequence (N);
5336 Body_Id : Entity_Id;
5339 Spec_Id : Entity_Id;
5341 procedure Add_Return (S : List_Id);
5342 -- Append a return statement to the statement sequence S if the last
5343 -- statement is not already a return or a goto statement. Note that
5344 -- the latter test is not critical, it does not matter if we add a few
5345 -- extra returns, since they get eliminated anyway later on.
5351 procedure Add_Return (S : List_Id) is
5356 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5357 -- not relevant in this context since they are not executable.
5359 Last_Stm := Last (S);
5360 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
5364 -- Now insert return unless last statement is a transfer
5366 if not Is_Transfer (Last_Stm) then
5368 -- The source location for the return is the end label of the
5369 -- procedure if present. Otherwise use the sloc of the last
5370 -- statement in the list. If the list comes from a generated
5371 -- exception handler and we are not debugging generated code,
5372 -- all the statements within the handler are made invisible
5375 if Nkind (Parent (S)) = N_Exception_Handler
5376 and then not Comes_From_Source (Parent (S))
5378 Loc := Sloc (Last_Stm);
5379 elsif Present (End_Label (H)) then
5380 Loc := Sloc (End_Label (H));
5382 Loc := Sloc (Last_Stm);
5386 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
5389 -- Append return statement, and set analyzed manually. We can't
5390 -- call Analyze on this return since the scope is wrong.
5392 -- Note: it almost works to push the scope and then do the
5393 -- Analyze call, but something goes wrong in some weird cases
5394 -- and it is not worth worrying about ???
5399 -- Call _Postconditions procedure if appropriate. We need to
5400 -- do this explicitly because we did not analyze the generated
5401 -- return statement above, so the call did not get inserted.
5403 if Ekind (Spec_Id) = E_Procedure
5404 and then Has_Postconditions (Spec_Id)
5406 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
5408 Make_Procedure_Call_Statement (Loc,
5410 New_Reference_To (Postcondition_Proc (Spec_Id), Loc)));
5416 -- Start of processing for Expand_N_Subprogram_Body
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 (H);
5429 -- If local-exception-to-goto optimization active, insert dummy push
5430 -- statements at start, and dummy pop statements at end.
5432 if (Debug_Flag_Dot_G
5433 or else Restriction_Active (No_Exception_Propagation))
5434 and then Is_Non_Empty_List (L)
5437 FS : constant Node_Id := First (L);
5438 FL : constant Source_Ptr := Sloc (FS);
5443 -- LS points to either last statement, if statements are present
5444 -- or to the last declaration if there are no statements present.
5445 -- It is the node after which the pop's are generated.
5447 if Is_Non_Empty_List (Statements (H)) then
5448 LS := Last (Statements (H));
5455 Insert_List_Before_And_Analyze (FS, New_List (
5456 Make_Push_Constraint_Error_Label (FL),
5457 Make_Push_Program_Error_Label (FL),
5458 Make_Push_Storage_Error_Label (FL)));
5460 Insert_List_After_And_Analyze (LS, New_List (
5461 Make_Pop_Constraint_Error_Label (LL),
5462 Make_Pop_Program_Error_Label (LL),
5463 Make_Pop_Storage_Error_Label (LL)));
5467 -- Find entity for subprogram
5469 Body_Id := Defining_Entity (N);
5471 if Present (Corresponding_Spec (N)) then
5472 Spec_Id := Corresponding_Spec (N);
5477 -- Need poll on entry to subprogram if polling enabled. We only do this
5478 -- for non-empty subprograms, since it does not seem necessary to poll
5479 -- for a dummy null subprogram.
5481 if Is_Non_Empty_List (L) then
5483 -- Do not add a polling call if the subprogram is to be inlined by
5484 -- the back-end, to avoid repeated calls with multiple inlinings.
5486 if Is_Inlined (Spec_Id)
5487 and then Front_End_Inlining
5488 and then Optimization_Level > 1
5492 Generate_Poll_Call (First (L));
5496 -- If this is a Pure function which has any parameters whose root type
5497 -- is System.Address, reset the Pure indication, since it will likely
5498 -- cause incorrect code to be generated as the parameter is probably
5499 -- a pointer, and the fact that the same pointer is passed does not mean
5500 -- that the same value is being referenced.
5502 -- Note that if the programmer gave an explicit Pure_Function pragma,
5503 -- then we believe the programmer, and leave the subprogram Pure.
5505 -- This code should probably be at the freeze point, so that it happens
5506 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5507 -- semantic tree has Is_Pure set properly ???
5509 if Is_Pure (Spec_Id)
5510 and then Is_Subprogram (Spec_Id)
5511 and then not Has_Pragma_Pure_Function (Spec_Id)
5517 F := First_Formal (Spec_Id);
5518 while Present (F) loop
5519 if Is_Descendent_Of_Address (Etype (F))
5521 -- Note that this test is being made in the body of the
5522 -- subprogram, not the spec, so we are testing the full
5523 -- type for being limited here, as required.
5525 or else Is_Limited_Type (Etype (F))
5527 Set_Is_Pure (Spec_Id, False);
5529 if Spec_Id /= Body_Id then
5530 Set_Is_Pure (Body_Id, False);
5541 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5543 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5548 -- Loop through formals
5550 F := First_Formal (Spec_Id);
5551 while Present (F) loop
5552 if Is_Scalar_Type (Etype (F))
5553 and then Ekind (F) = E_Out_Parameter
5555 Check_Restriction (No_Default_Initialization, F);
5557 -- Insert the initialization. We turn off validity checks
5558 -- for this assignment, since we do not want any check on
5559 -- the initial value itself (which may well be invalid).
5561 Insert_Before_And_Analyze (First (L),
5562 Make_Assignment_Statement (Loc,
5563 Name => New_Occurrence_Of (F, Loc),
5564 Expression => Get_Simple_Init_Val (Etype (F), N)),
5565 Suppress => Validity_Check);
5573 -- Clear out statement list for stubbed procedure
5575 if Present (Corresponding_Spec (N)) then
5576 Set_Elaboration_Flag (N, Spec_Id);
5578 if Convention (Spec_Id) = Convention_Stubbed
5579 or else Is_Eliminated (Spec_Id)
5581 Set_Declarations (N, Empty_List);
5582 Set_Handled_Statement_Sequence (N,
5583 Make_Handled_Sequence_Of_Statements (Loc,
5584 Statements => New_List (Make_Null_Statement (Loc))));
5589 -- Create a set of discriminals for the next protected subprogram body
5591 if Is_List_Member (N)
5592 and then Present (Parent (List_Containing (N)))
5593 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5594 and then Present (Next_Protected_Operation (N))
5596 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5599 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5600 -- subprograms with no specs are not frozen.
5603 Typ : constant Entity_Id := Etype (Spec_Id);
5604 Utyp : constant Entity_Id := Underlying_Type (Typ);
5607 if not Acts_As_Spec (N)
5608 and then Nkind (Parent (Parent (Spec_Id))) /=
5609 N_Subprogram_Body_Stub
5613 elsif Is_Immutably_Limited_Type (Typ) then
5614 Set_Returns_By_Ref (Spec_Id);
5616 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5617 Set_Returns_By_Ref (Spec_Id);
5621 -- For a procedure, we add a return for all possible syntactic ends of
5624 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5625 Add_Return (Statements (H));
5627 if Present (Exception_Handlers (H)) then
5628 Except_H := First_Non_Pragma (Exception_Handlers (H));
5629 while Present (Except_H) loop
5630 Add_Return (Statements (Except_H));
5631 Next_Non_Pragma (Except_H);
5635 -- For a function, we must deal with the case where there is at least
5636 -- one missing return. What we do is to wrap the entire body of the
5637 -- function in a block:
5650 -- raise Program_Error;
5653 -- This approach is necessary because the raise must be signalled to the
5654 -- caller, not handled by any local handler (RM 6.4(11)).
5656 -- Note: we do not need to analyze the constructed sequence here, since
5657 -- it has no handler, and an attempt to analyze the handled statement
5658 -- sequence twice is risky in various ways (e.g. the issue of expanding
5659 -- cleanup actions twice).
5661 elsif Has_Missing_Return (Spec_Id) then
5663 Hloc : constant Source_Ptr := Sloc (H);
5664 Blok : constant Node_Id :=
5665 Make_Block_Statement (Hloc,
5666 Handled_Statement_Sequence => H);
5667 Rais : constant Node_Id :=
5668 Make_Raise_Program_Error (Hloc,
5669 Reason => PE_Missing_Return);
5672 Set_Handled_Statement_Sequence (N,
5673 Make_Handled_Sequence_Of_Statements (Hloc,
5674 Statements => New_List (Blok, Rais)));
5676 Push_Scope (Spec_Id);
5683 -- If subprogram contains a parameterless recursive call, then we may
5684 -- have an infinite recursion, so see if we can generate code to check
5685 -- for this possibility if storage checks are not suppressed.
5687 if Ekind (Spec_Id) = E_Procedure
5688 and then Has_Recursive_Call (Spec_Id)
5689 and then not Storage_Checks_Suppressed (Spec_Id)
5691 Detect_Infinite_Recursion (N, Spec_Id);
5694 -- Set to encode entity names in package body before gigi is called
5696 Qualify_Entity_Names (N);
5697 end Expand_N_Subprogram_Body;
5699 -----------------------------------
5700 -- Expand_N_Subprogram_Body_Stub --
5701 -----------------------------------
5703 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5705 if Present (Corresponding_Body (N)) then
5706 Expand_N_Subprogram_Body (
5707 Unit_Declaration_Node (Corresponding_Body (N)));
5709 end Expand_N_Subprogram_Body_Stub;
5711 -------------------------------------
5712 -- Expand_N_Subprogram_Declaration --
5713 -------------------------------------
5715 -- If the declaration appears within a protected body, it is a private
5716 -- operation of the protected type. We must create the corresponding
5717 -- protected subprogram an associated formals. For a normal protected
5718 -- operation, this is done when expanding the protected type declaration.
5720 -- If the declaration is for a null procedure, emit null body
5722 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5723 Loc : constant Source_Ptr := Sloc (N);
5724 Subp : constant Entity_Id := Defining_Entity (N);
5725 Scop : constant Entity_Id := Scope (Subp);
5726 Prot_Decl : Node_Id;
5728 Prot_Id : Entity_Id;
5731 -- In SPARK, subprogram declarations are only allowed in package
5734 if Nkind (Parent (N)) /= N_Package_Specification then
5735 if Nkind (Parent (N)) = N_Compilation_Unit then
5736 Check_SPARK_Restriction
5737 ("subprogram declaration is not a library item", N);
5739 elsif Present (Next (N))
5740 and then Nkind (Next (N)) = N_Pragma
5741 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
5743 -- In SPARK, subprogram declarations are also permitted in
5744 -- declarative parts when immediately followed by a corresponding
5745 -- pragma Import. We only check here that there is some pragma
5750 Check_SPARK_Restriction
5751 ("subprogram declaration is not allowed here", N);
5755 -- Deal with case of protected subprogram. Do not generate protected
5756 -- operation if operation is flagged as eliminated.
5758 if Is_List_Member (N)
5759 and then Present (Parent (List_Containing (N)))
5760 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5761 and then Is_Protected_Type (Scop)
5763 if No (Protected_Body_Subprogram (Subp))
5764 and then not Is_Eliminated (Subp)
5767 Make_Subprogram_Declaration (Loc,
5769 Build_Protected_Sub_Specification
5770 (N, Scop, Unprotected_Mode));
5772 -- The protected subprogram is declared outside of the protected
5773 -- body. Given that the body has frozen all entities so far, we
5774 -- analyze the subprogram and perform freezing actions explicitly.
5775 -- including the generation of an explicit freeze node, to ensure
5776 -- that gigi has the proper order of elaboration.
5777 -- If the body is a subunit, the insertion point is before the
5778 -- stub in the parent.
5780 Prot_Bod := Parent (List_Containing (N));
5782 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5783 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5786 Insert_Before (Prot_Bod, Prot_Decl);
5787 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5788 Set_Has_Delayed_Freeze (Prot_Id);
5790 Push_Scope (Scope (Scop));
5791 Analyze (Prot_Decl);
5792 Freeze_Before (N, Prot_Id);
5793 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5795 -- Create protected operation as well. Even though the operation
5796 -- is only accessible within the body, it is possible to make it
5797 -- available outside of the protected object by using 'Access to
5798 -- provide a callback, so build protected version in all cases.
5801 Make_Subprogram_Declaration (Loc,
5803 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5804 Insert_Before (Prot_Bod, Prot_Decl);
5805 Analyze (Prot_Decl);
5810 -- Ada 2005 (AI-348): Generate body for a null procedure.
5811 -- In most cases this is superfluous because calls to it
5812 -- will be automatically inlined, but we definitely need
5813 -- the body if preconditions for the procedure are present.
5815 elsif Nkind (Specification (N)) = N_Procedure_Specification
5816 and then Null_Present (Specification (N))
5819 Bod : constant Node_Id := Body_To_Inline (N);
5822 Set_Has_Completion (Subp, False);
5823 Append_Freeze_Action (Subp, Bod);
5825 -- The body now contains raise statements, so calls to it will
5828 Set_Is_Inlined (Subp, False);
5831 end Expand_N_Subprogram_Declaration;
5833 --------------------------------
5834 -- Expand_Non_Function_Return --
5835 --------------------------------
5837 procedure Expand_Non_Function_Return (N : Node_Id) is
5838 pragma Assert (No (Expression (N)));
5840 Loc : constant Source_Ptr := Sloc (N);
5841 Scope_Id : Entity_Id :=
5842 Return_Applies_To (Return_Statement_Entity (N));
5843 Kind : constant Entity_Kind := Ekind (Scope_Id);
5846 Goto_Stat : Node_Id;
5850 -- Call _Postconditions procedure if procedure with active
5851 -- postconditions. Here, we use the Postcondition_Proc attribute, which
5852 -- is needed for implicitly-generated returns. Functions never
5853 -- have implicitly-generated returns, and there's no room for
5854 -- Postcondition_Proc in E_Function, so we look up the identifier
5855 -- Name_uPostconditions for function returns (see
5856 -- Expand_Simple_Function_Return).
5858 if Ekind (Scope_Id) = E_Procedure
5859 and then Has_Postconditions (Scope_Id)
5861 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
5863 Make_Procedure_Call_Statement (Loc,
5864 Name => New_Reference_To (Postcondition_Proc (Scope_Id), Loc)));
5867 -- If it is a return from a procedure do no extra steps
5869 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5872 -- If it is a nested return within an extended one, replace it with a
5873 -- return of the previously declared return object.
5875 elsif Kind = E_Return_Statement then
5877 Make_Simple_Return_Statement (Loc,
5879 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5880 Set_Comes_From_Extended_Return_Statement (N);
5881 Set_Return_Statement_Entity (N, Scope_Id);
5882 Expand_Simple_Function_Return (N);
5886 pragma Assert (Is_Entry (Scope_Id));
5888 -- Look at the enclosing block to see whether the return is from an
5889 -- accept statement or an entry body.
5891 for J in reverse 0 .. Scope_Stack.Last loop
5892 Scope_Id := Scope_Stack.Table (J).Entity;
5893 exit when Is_Concurrent_Type (Scope_Id);
5896 -- If it is a return from accept statement it is expanded as call to
5897 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5899 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5900 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5902 if Is_Task_Type (Scope_Id) then
5905 Make_Procedure_Call_Statement (Loc,
5906 Name => New_Reference_To (RTE (RE_Complete_Rendezvous), Loc));
5907 Insert_Before (N, Call);
5908 -- why not insert actions here???
5911 Acc_Stat := Parent (N);
5912 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5913 Acc_Stat := Parent (Acc_Stat);
5916 Lab_Node := Last (Statements
5917 (Handled_Statement_Sequence (Acc_Stat)));
5919 Goto_Stat := Make_Goto_Statement (Loc,
5920 Name => New_Occurrence_Of
5921 (Entity (Identifier (Lab_Node)), Loc));
5923 Set_Analyzed (Goto_Stat);
5925 Rewrite (N, Goto_Stat);
5928 -- If it is a return from an entry body, put a Complete_Entry_Body call
5929 -- in front of the return.
5931 elsif Is_Protected_Type (Scope_Id) then
5933 Make_Procedure_Call_Statement (Loc,
5935 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
5936 Parameter_Associations => New_List (
5937 Make_Attribute_Reference (Loc,
5940 (Find_Protection_Object (Current_Scope), Loc),
5941 Attribute_Name => Name_Unchecked_Access)));
5943 Insert_Before (N, Call);
5946 end Expand_Non_Function_Return;
5948 ---------------------------------------
5949 -- Expand_Protected_Object_Reference --
5950 ---------------------------------------
5952 function Expand_Protected_Object_Reference
5954 Scop : Entity_Id) return Node_Id
5956 Loc : constant Source_Ptr := Sloc (N);
5963 Rec := Make_Identifier (Loc, Name_uObject);
5964 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5966 -- Find enclosing protected operation, and retrieve its first parameter,
5967 -- which denotes the enclosing protected object. If the enclosing
5968 -- operation is an entry, we are immediately within the protected body,
5969 -- and we can retrieve the object from the service entries procedure. A
5970 -- barrier function has the same signature as an entry. A barrier
5971 -- function is compiled within the protected object, but unlike
5972 -- protected operations its never needs locks, so that its protected
5973 -- body subprogram points to itself.
5975 Proc := Current_Scope;
5976 while Present (Proc)
5977 and then Scope (Proc) /= Scop
5979 Proc := Scope (Proc);
5982 Corr := Protected_Body_Subprogram (Proc);
5986 -- Previous error left expansion incomplete.
5987 -- Nothing to do on this call.
5994 (First (Parameter_Specifications (Parent (Corr))));
5996 if Is_Subprogram (Proc)
5997 and then Proc /= Corr
5999 -- Protected function or procedure
6001 Set_Entity (Rec, Param);
6003 -- Rec is a reference to an entity which will not be in scope when
6004 -- the call is reanalyzed, and needs no further analysis.
6009 -- Entry or barrier function for entry body. The first parameter of
6010 -- the entry body procedure is pointer to the object. We create a
6011 -- local variable of the proper type, duplicating what is done to
6012 -- define _object later on.
6016 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6020 Make_Full_Type_Declaration (Loc,
6021 Defining_Identifier => Obj_Ptr,
6023 Make_Access_To_Object_Definition (Loc,
6024 Subtype_Indication =>
6026 (Corresponding_Record_Type (Scop), Loc))));
6028 Insert_Actions (N, Decls);
6029 Freeze_Before (N, Obj_Ptr);
6032 Make_Explicit_Dereference (Loc,
6034 Unchecked_Convert_To (Obj_Ptr,
6035 New_Occurrence_Of (Param, Loc)));
6037 -- Analyze new actual. Other actuals in calls are already analyzed
6038 -- and the list of actuals is not reanalyzed after rewriting.
6040 Set_Parent (Rec, N);
6046 end Expand_Protected_Object_Reference;
6048 --------------------------------------
6049 -- Expand_Protected_Subprogram_Call --
6050 --------------------------------------
6052 procedure Expand_Protected_Subprogram_Call
6060 -- If the protected object is not an enclosing scope, this is an
6061 -- inter-object function call. Inter-object procedure calls are expanded
6062 -- by Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if
6063 -- the subprogram being called is in the protected body being compiled,
6064 -- and if the protected object in the call is statically the enclosing
6065 -- type. The object may be an component of some other data structure, in
6066 -- which case this must be handled as an inter-object call.
6068 if not In_Open_Scopes (Scop)
6069 or else not Is_Entity_Name (Name (N))
6071 if Nkind (Name (N)) = N_Selected_Component then
6072 Rec := Prefix (Name (N));
6075 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
6076 Rec := Prefix (Prefix (Name (N)));
6079 Build_Protected_Subprogram_Call (N,
6080 Name => New_Occurrence_Of (Subp, Sloc (N)),
6081 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6085 Rec := Expand_Protected_Object_Reference (N, Scop);
6091 Build_Protected_Subprogram_Call (N,
6098 -- If it is a function call it can appear in elaboration code and
6099 -- the called entity must be frozen here.
6101 if Ekind (Subp) = E_Function then
6102 Freeze_Expression (Name (N));
6105 -- Analyze and resolve the new call. The actuals have already been
6106 -- resolved, but expansion of a function call will add extra actuals
6107 -- if needed. Analysis of a procedure call already includes resolution.
6111 if Ekind (Subp) = E_Function then
6112 Resolve (N, Etype (Subp));
6114 end Expand_Protected_Subprogram_Call;
6116 -----------------------------------
6117 -- Expand_Simple_Function_Return --
6118 -----------------------------------
6120 -- The "simple" comes from the syntax rule simple_return_statement.
6121 -- The semantics are not at all simple!
6123 procedure Expand_Simple_Function_Return (N : Node_Id) is
6124 Loc : constant Source_Ptr := Sloc (N);
6126 Scope_Id : constant Entity_Id :=
6127 Return_Applies_To (Return_Statement_Entity (N));
6128 -- The function we are returning from
6130 R_Type : constant Entity_Id := Etype (Scope_Id);
6131 -- The result type of the function
6133 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6135 Exp : constant Node_Id := Expression (N);
6136 pragma Assert (Present (Exp));
6138 Exptyp : constant Entity_Id := Etype (Exp);
6139 -- The type of the expression (not necessarily the same as R_Type)
6141 Subtype_Ind : Node_Id;
6142 -- If the result type of the function is class-wide and the
6143 -- expression has a specific type, then we use the expression's
6144 -- type as the type of the return object. In cases where the
6145 -- expression is an aggregate that is built in place, this avoids
6146 -- the need for an expensive conversion of the return object to
6147 -- the specific type on assignments to the individual components.
6150 if Is_Class_Wide_Type (R_Type)
6151 and then not Is_Class_Wide_Type (Etype (Exp))
6153 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
6155 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6158 -- For the case of a simple return that does not come from an extended
6159 -- return, in the case of Ada 2005 where we are returning a limited
6160 -- type, we rewrite "return <expression>;" to be:
6162 -- return _anon_ : <return_subtype> := <expression>
6164 -- The expansion produced by Expand_N_Extended_Return_Statement will
6165 -- contain simple return statements (for example, a block containing
6166 -- simple return of the return object), which brings us back here with
6167 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6168 -- checking for a simple return that does not come from an extended
6169 -- return is to avoid this infinite recursion.
6171 -- The reason for this design is that for Ada 2005 limited returns, we
6172 -- need to reify the return object, so we can build it "in place", and
6173 -- we need a block statement to hang finalization and tasking stuff.
6175 -- ??? In order to avoid disruption, we avoid translating to extended
6176 -- return except in the cases where we really need to (Ada 2005 for
6177 -- inherently limited). We might prefer to do this translation in all
6178 -- cases (except perhaps for the case of Ada 95 inherently limited),
6179 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6180 -- code. This would also allow us to do the build-in-place optimization
6181 -- for efficiency even in cases where it is semantically not required.
6183 -- As before, we check the type of the return expression rather than the
6184 -- return type of the function, because the latter may be a limited
6185 -- class-wide interface type, which is not a limited type, even though
6186 -- the type of the expression may be.
6188 if not Comes_From_Extended_Return_Statement (N)
6189 and then Is_Immutably_Limited_Type (Etype (Expression (N)))
6190 and then Ada_Version >= Ada_2005
6191 and then not Debug_Flag_Dot_L
6194 Return_Object_Entity : constant Entity_Id :=
6195 Make_Temporary (Loc, 'R', Exp);
6196 Obj_Decl : constant Node_Id :=
6197 Make_Object_Declaration (Loc,
6198 Defining_Identifier => Return_Object_Entity,
6199 Object_Definition => Subtype_Ind,
6202 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
6203 Return_Object_Declarations => New_List (Obj_Decl));
6204 -- Do not perform this high-level optimization if the result type
6205 -- is an interface because the "this" pointer must be displaced.
6214 -- Here we have a simple return statement that is part of the expansion
6215 -- of an extended return statement (either written by the user, or
6216 -- generated by the above code).
6218 -- Always normalize C/Fortran boolean result. This is not always needed,
6219 -- but it seems a good idea to minimize the passing around of non-
6220 -- normalized values, and in any case this handles the processing of
6221 -- barrier functions for protected types, which turn the condition into
6222 -- a return statement.
6224 if Is_Boolean_Type (Exptyp)
6225 and then Nonzero_Is_True (Exptyp)
6227 Adjust_Condition (Exp);
6228 Adjust_Result_Type (Exp, Exptyp);
6231 -- Do validity check if enabled for returns
6233 if Validity_Checks_On
6234 and then Validity_Check_Returns
6239 -- Check the result expression of a scalar function against the subtype
6240 -- of the function by inserting a conversion. This conversion must
6241 -- eventually be performed for other classes of types, but for now it's
6242 -- only done for scalars.
6245 if Is_Scalar_Type (Exptyp) then
6246 Rewrite (Exp, Convert_To (R_Type, Exp));
6248 -- The expression is resolved to ensure that the conversion gets
6249 -- expanded to generate a possible constraint check.
6251 Analyze_And_Resolve (Exp, R_Type);
6254 -- Deal with returning variable length objects and controlled types
6256 -- Nothing to do if we are returning by reference, or this is not a
6257 -- type that requires special processing (indicated by the fact that
6258 -- it requires a cleanup scope for the secondary stack case).
6260 if Is_Immutably_Limited_Type (Exptyp)
6261 or else Is_Limited_Interface (Exptyp)
6265 elsif not Requires_Transient_Scope (R_Type) then
6267 -- Mutable records with no variable length components are not
6268 -- returned on the sec-stack, so we need to make sure that the
6269 -- backend will only copy back the size of the actual value, and not
6270 -- the maximum size. We create an actual subtype for this purpose.
6273 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6277 if Has_Discriminants (Ubt)
6278 and then not Is_Constrained (Ubt)
6279 and then not Has_Unchecked_Union (Ubt)
6281 Decl := Build_Actual_Subtype (Ubt, Exp);
6282 Ent := Defining_Identifier (Decl);
6283 Insert_Action (Exp, Decl);
6284 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6285 Analyze_And_Resolve (Exp);
6289 -- Here if secondary stack is used
6292 -- Make sure that no surrounding block will reclaim the secondary
6293 -- stack on which we are going to put the result. Not only may this
6294 -- introduce secondary stack leaks but worse, if the reclamation is
6295 -- done too early, then the result we are returning may get
6302 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
6303 Set_Sec_Stack_Needed_For_Return (S, True);
6304 S := Enclosing_Dynamic_Scope (S);
6308 -- Optimize the case where the result is a function call. In this
6309 -- case either the result is already on the secondary stack, or is
6310 -- already being returned with the stack pointer depressed and no
6311 -- further processing is required except to set the By_Ref flag to
6312 -- ensure that gigi does not attempt an extra unnecessary copy.
6313 -- (actually not just unnecessary but harmfully wrong in the case
6314 -- of a controlled type, where gigi does not know how to do a copy).
6315 -- To make up for a gcc 2.8.1 deficiency (???), we perform
6316 -- the copy for array types if the constrained status of the
6317 -- target type is different from that of the expression.
6319 if Requires_Transient_Scope (Exptyp)
6321 (not Is_Array_Type (Exptyp)
6322 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6323 or else CW_Or_Has_Controlled_Part (Utyp))
6324 and then Nkind (Exp) = N_Function_Call
6328 -- Remove side effects from the expression now so that other parts
6329 -- of the expander do not have to reanalyze this node without this
6332 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6334 -- For controlled types, do the allocation on the secondary stack
6335 -- manually in order to call adjust at the right time:
6337 -- type Anon1 is access R_Type;
6338 -- for Anon1'Storage_pool use ss_pool;
6339 -- Anon2 : anon1 := new R_Type'(expr);
6340 -- return Anon2.all;
6342 -- We do the same for classwide types that are not potentially
6343 -- controlled (by the virtue of restriction No_Finalization) because
6344 -- gigi is not able to properly allocate class-wide types.
6346 elsif CW_Or_Has_Controlled_Part (Utyp) then
6348 Loc : constant Source_Ptr := Sloc (N);
6349 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6350 Alloc_Node : Node_Id;
6354 Set_Ekind (Acc_Typ, E_Access_Type);
6356 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6358 -- This is an allocator for the secondary stack, and it's fine
6359 -- to have Comes_From_Source set False on it, as gigi knows not
6360 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6363 Make_Allocator (Loc,
6365 Make_Qualified_Expression (Loc,
6366 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
6367 Expression => Relocate_Node (Exp)));
6369 -- We do not want discriminant checks on the declaration,
6370 -- given that it gets its value from the allocator.
6372 Set_No_Initialization (Alloc_Node);
6374 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6376 Insert_List_Before_And_Analyze (N, New_List (
6377 Make_Full_Type_Declaration (Loc,
6378 Defining_Identifier => Acc_Typ,
6380 Make_Access_To_Object_Definition (Loc,
6381 Subtype_Indication => Subtype_Ind)),
6383 Make_Object_Declaration (Loc,
6384 Defining_Identifier => Temp,
6385 Object_Definition => New_Reference_To (Acc_Typ, Loc),
6386 Expression => Alloc_Node)));
6389 Make_Explicit_Dereference (Loc,
6390 Prefix => New_Reference_To (Temp, Loc)));
6392 Analyze_And_Resolve (Exp, R_Type);
6395 -- Otherwise use the gigi mechanism to allocate result on the
6399 Check_Restriction (No_Secondary_Stack, N);
6400 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6402 -- If we are generating code for the VM do not use
6403 -- SS_Allocate since everything is heap-allocated anyway.
6405 if VM_Target = No_VM then
6406 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6411 -- Implement the rules of 6.5(8-10), which require a tag check in the
6412 -- case of a limited tagged return type, and tag reassignment for
6413 -- nonlimited tagged results. These actions are needed when the return
6414 -- type is a specific tagged type and the result expression is a
6415 -- conversion or a formal parameter, because in that case the tag of the
6416 -- expression might differ from the tag of the specific result type.
6418 if Is_Tagged_Type (Utyp)
6419 and then not Is_Class_Wide_Type (Utyp)
6420 and then (Nkind_In (Exp, N_Type_Conversion,
6421 N_Unchecked_Type_Conversion)
6422 or else (Is_Entity_Name (Exp)
6423 and then Ekind (Entity (Exp)) in Formal_Kind))
6425 -- When the return type is limited, perform a check that the
6426 -- tag of the result is the same as the tag of the return type.
6428 if Is_Limited_Type (R_Type) then
6430 Make_Raise_Constraint_Error (Loc,
6434 Make_Selected_Component (Loc,
6435 Prefix => Duplicate_Subexpr (Exp),
6436 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6438 Make_Attribute_Reference (Loc,
6440 New_Occurrence_Of (Base_Type (Utyp), Loc),
6441 Attribute_Name => Name_Tag)),
6442 Reason => CE_Tag_Check_Failed));
6444 -- If the result type is a specific nonlimited tagged type, then we
6445 -- have to ensure that the tag of the result is that of the result
6446 -- type. This is handled by making a copy of the expression in the
6447 -- case where it might have a different tag, namely when the
6448 -- expression is a conversion or a formal parameter. We create a new
6449 -- object of the result type and initialize it from the expression,
6450 -- which will implicitly force the tag to be set appropriately.
6454 ExpR : constant Node_Id := Relocate_Node (Exp);
6455 Result_Id : constant Entity_Id :=
6456 Make_Temporary (Loc, 'R', ExpR);
6457 Result_Exp : constant Node_Id :=
6458 New_Reference_To (Result_Id, Loc);
6459 Result_Obj : constant Node_Id :=
6460 Make_Object_Declaration (Loc,
6461 Defining_Identifier => Result_Id,
6462 Object_Definition =>
6463 New_Reference_To (R_Type, Loc),
6464 Constant_Present => True,
6465 Expression => ExpR);
6468 Set_Assignment_OK (Result_Obj);
6469 Insert_Action (Exp, Result_Obj);
6471 Rewrite (Exp, Result_Exp);
6472 Analyze_And_Resolve (Exp, R_Type);
6476 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6477 -- a check that the level of the return expression's underlying type
6478 -- is not deeper than the level of the master enclosing the function.
6479 -- Always generate the check when the type of the return expression
6480 -- is class-wide, when it's a type conversion, or when it's a formal
6481 -- parameter. Otherwise, suppress the check in the case where the
6482 -- return expression has a specific type whose level is known not to
6483 -- be statically deeper than the function's result type.
6485 -- Note: accessibility check is skipped in the VM case, since there
6486 -- does not seem to be any practical way to implement this check.
6488 elsif Ada_Version >= Ada_2005
6489 and then Tagged_Type_Expansion
6490 and then Is_Class_Wide_Type (R_Type)
6491 and then not Scope_Suppress (Accessibility_Check)
6493 (Is_Class_Wide_Type (Etype (Exp))
6494 or else Nkind_In (Exp, N_Type_Conversion,
6495 N_Unchecked_Type_Conversion)
6496 or else (Is_Entity_Name (Exp)
6497 and then Ekind (Entity (Exp)) in Formal_Kind)
6498 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6499 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6505 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6506 -- "this" to reference the base of the object --- required to get
6507 -- access to the TSD of the object.
6509 if Is_Class_Wide_Type (Etype (Exp))
6510 and then Is_Interface (Etype (Exp))
6511 and then Nkind (Exp) = N_Explicit_Dereference
6514 Make_Explicit_Dereference (Loc,
6516 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6517 Make_Function_Call (Loc,
6519 New_Reference_To (RTE (RE_Base_Address), Loc),
6520 Parameter_Associations => New_List (
6521 Unchecked_Convert_To (RTE (RE_Address),
6522 Duplicate_Subexpr (Prefix (Exp)))))));
6525 Make_Attribute_Reference (Loc,
6526 Prefix => Duplicate_Subexpr (Exp),
6527 Attribute_Name => Name_Tag);
6531 Make_Raise_Program_Error (Loc,
6534 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6536 Make_Integer_Literal (Loc,
6537 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6538 Reason => PE_Accessibility_Check_Failed));
6541 -- AI05-0073: If function has a controlling access result, check that
6542 -- the tag of the return value, if it is not null, matches designated
6543 -- type of return type.
6544 -- The return expression is referenced twice in the code below, so
6545 -- it must be made free of side effects. Given that different compilers
6546 -- may evaluate these parameters in different order, both occurrences
6549 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6550 and then Has_Controlling_Result (Scope_Id)
6553 Make_Raise_Constraint_Error (Loc,
6558 Left_Opnd => Duplicate_Subexpr (Exp),
6559 Right_Opnd => Make_Null (Loc)),
6560 Right_Opnd => Make_Op_Ne (Loc,
6562 Make_Selected_Component (Loc,
6563 Prefix => Duplicate_Subexpr (Exp),
6564 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6566 Make_Attribute_Reference (Loc,
6568 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6569 Attribute_Name => Name_Tag))),
6570 Reason => CE_Tag_Check_Failed),
6571 Suppress => All_Checks);
6574 -- If we are returning an object that may not be bit-aligned, then copy
6575 -- the value into a temporary first. This copy may need to expand to a
6576 -- loop of component operations.
6578 if Is_Possibly_Unaligned_Slice (Exp)
6579 or else Is_Possibly_Unaligned_Object (Exp)
6582 ExpR : constant Node_Id := Relocate_Node (Exp);
6583 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6586 Make_Object_Declaration (Loc,
6587 Defining_Identifier => Tnn,
6588 Constant_Present => True,
6589 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6590 Expression => ExpR),
6591 Suppress => All_Checks);
6592 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6596 -- Generate call to postcondition checks if they are present
6598 if Ekind (Scope_Id) = E_Function
6599 and then Has_Postconditions (Scope_Id)
6601 -- We are going to reference the returned value twice in this case,
6602 -- once in the call to _Postconditions, and once in the actual return
6603 -- statement, but we can't have side effects happening twice, and in
6604 -- any case for efficiency we don't want to do the computation twice.
6606 -- If the returned expression is an entity name, we don't need to
6607 -- worry since it is efficient and safe to reference it twice, that's
6608 -- also true for literals other than string literals, and for the
6609 -- case of X.all where X is an entity name.
6611 if Is_Entity_Name (Exp)
6612 or else Nkind_In (Exp, N_Character_Literal,
6615 or else (Nkind (Exp) = N_Explicit_Dereference
6616 and then Is_Entity_Name (Prefix (Exp)))
6620 -- Otherwise we are going to need a temporary to capture the value
6624 ExpR : constant Node_Id := Relocate_Node (Exp);
6625 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6628 -- For a complex expression of an elementary type, capture
6629 -- value in the temporary and use it as the reference.
6631 if Is_Elementary_Type (R_Type) then
6633 Make_Object_Declaration (Loc,
6634 Defining_Identifier => Tnn,
6635 Constant_Present => True,
6636 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6637 Expression => ExpR),
6638 Suppress => All_Checks);
6640 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6642 -- If we have something we can rename, generate a renaming of
6643 -- the object and replace the expression with a reference
6645 elsif Is_Object_Reference (Exp) then
6647 Make_Object_Renaming_Declaration (Loc,
6648 Defining_Identifier => Tnn,
6649 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
6651 Suppress => All_Checks);
6653 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6655 -- Otherwise we have something like a string literal or an
6656 -- aggregate. We could copy the value, but that would be
6657 -- inefficient. Instead we make a reference to the value and
6658 -- capture this reference with a renaming, the expression is
6659 -- then replaced by a dereference of this renaming.
6662 -- For now, copy the value, since the code below does not
6663 -- seem to work correctly ???
6666 Make_Object_Declaration (Loc,
6667 Defining_Identifier => Tnn,
6668 Constant_Present => True,
6669 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6670 Expression => Relocate_Node (Exp)),
6671 Suppress => All_Checks);
6673 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6675 -- Insert_Action (Exp,
6676 -- Make_Object_Renaming_Declaration (Loc,
6677 -- Defining_Identifier => Tnn,
6678 -- Access_Definition =>
6679 -- Make_Access_Definition (Loc,
6680 -- All_Present => True,
6681 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
6683 -- Make_Reference (Loc,
6684 -- Prefix => Relocate_Node (Exp))),
6685 -- Suppress => All_Checks);
6688 -- Make_Explicit_Dereference (Loc,
6689 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
6694 -- Generate call to _postconditions
6697 Make_Procedure_Call_Statement (Loc,
6698 Name => Make_Identifier (Loc, Name_uPostconditions),
6699 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
6702 -- Ada 2005 (AI-251): If this return statement corresponds with an
6703 -- simple return statement associated with an extended return statement
6704 -- and the type of the returned object is an interface then generate an
6705 -- implicit conversion to force displacement of the "this" pointer.
6707 if Ada_Version >= Ada_2005
6708 and then Comes_From_Extended_Return_Statement (N)
6709 and then Nkind (Expression (N)) = N_Identifier
6710 and then Is_Interface (Utyp)
6711 and then Utyp /= Underlying_Type (Exptyp)
6713 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6714 Analyze_And_Resolve (Exp);
6716 end Expand_Simple_Function_Return;
6718 --------------------------------
6719 -- Is_Build_In_Place_Function --
6720 --------------------------------
6722 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
6724 -- This function is called from Expand_Subtype_From_Expr during
6725 -- semantic analysis, even when expansion is off. In those cases
6726 -- the build_in_place expansion will not take place.
6728 if not Expander_Active then
6732 -- For now we test whether E denotes a function or access-to-function
6733 -- type whose result subtype is inherently limited. Later this test may
6734 -- be revised to allow composite nonlimited types. Functions with a
6735 -- foreign convention or whose result type has a foreign convention
6738 if Ekind_In (E, E_Function, E_Generic_Function)
6739 or else (Ekind (E) = E_Subprogram_Type
6740 and then Etype (E) /= Standard_Void_Type)
6742 -- Note: If you have Convention (C) on an inherently limited type,
6743 -- you're on your own. That is, the C code will have to be carefully
6744 -- written to know about the Ada conventions.
6746 if Has_Foreign_Convention (E)
6747 or else Has_Foreign_Convention (Etype (E))
6751 -- In Ada 2005 all functions with an inherently limited return type
6752 -- must be handled using a build-in-place profile, including the case
6753 -- of a function with a limited interface result, where the function
6754 -- may return objects of nonlimited descendants.
6757 return Is_Immutably_Limited_Type (Etype (E))
6758 and then Ada_Version >= Ada_2005
6759 and then not Debug_Flag_Dot_L;
6765 end Is_Build_In_Place_Function;
6767 -------------------------------------
6768 -- Is_Build_In_Place_Function_Call --
6769 -------------------------------------
6771 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
6772 Exp_Node : Node_Id := N;
6773 Function_Id : Entity_Id;
6776 -- Step past qualification or unchecked conversion (the latter can occur
6777 -- in cases of calls to 'Input).
6780 (Exp_Node, N_Qualified_Expression, N_Unchecked_Type_Conversion)
6782 Exp_Node := Expression (N);
6785 if Nkind (Exp_Node) /= N_Function_Call then
6789 if Is_Entity_Name (Name (Exp_Node)) then
6790 Function_Id := Entity (Name (Exp_Node));
6792 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
6793 Function_Id := Etype (Name (Exp_Node));
6796 return Is_Build_In_Place_Function (Function_Id);
6798 end Is_Build_In_Place_Function_Call;
6800 -----------------------
6801 -- Freeze_Subprogram --
6802 -----------------------
6804 procedure Freeze_Subprogram (N : Node_Id) is
6805 Loc : constant Source_Ptr := Sloc (N);
6807 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
6808 -- (Ada 2005): Register a predefined primitive in all the secondary
6809 -- dispatch tables of its primitive type.
6811 ----------------------------------
6812 -- Register_Predefined_DT_Entry --
6813 ----------------------------------
6815 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
6816 Iface_DT_Ptr : Elmt_Id;
6817 Tagged_Typ : Entity_Id;
6818 Thunk_Id : Entity_Id;
6819 Thunk_Code : Node_Id;
6822 Tagged_Typ := Find_Dispatching_Type (Prim);
6824 if No (Access_Disp_Table (Tagged_Typ))
6825 or else not Has_Interfaces (Tagged_Typ)
6826 or else not RTE_Available (RE_Interface_Tag)
6827 or else Restriction_Active (No_Dispatching_Calls)
6832 -- Skip the first two access-to-dispatch-table pointers since they
6833 -- leads to the primary dispatch table (predefined DT and user
6834 -- defined DT). We are only concerned with the secondary dispatch
6835 -- table pointers. Note that the access-to- dispatch-table pointer
6836 -- corresponds to the first implemented interface retrieved below.
6839 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
6841 while Present (Iface_DT_Ptr)
6842 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
6844 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
6845 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
6847 if Present (Thunk_Code) then
6848 Insert_Actions_After (N, New_List (
6851 Build_Set_Predefined_Prim_Op_Address (Loc,
6853 New_Reference_To (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
6854 Position => DT_Position (Prim),
6856 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
6857 Make_Attribute_Reference (Loc,
6858 Prefix => New_Reference_To (Thunk_Id, Loc),
6859 Attribute_Name => Name_Unrestricted_Access))),
6861 Build_Set_Predefined_Prim_Op_Address (Loc,
6864 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
6866 Position => DT_Position (Prim),
6868 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
6869 Make_Attribute_Reference (Loc,
6870 Prefix => New_Reference_To (Prim, Loc),
6871 Attribute_Name => Name_Unrestricted_Access)))));
6874 -- Skip the tag of the predefined primitives dispatch table
6876 Next_Elmt (Iface_DT_Ptr);
6877 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
6879 -- Skip the tag of the no-thunks dispatch table
6881 Next_Elmt (Iface_DT_Ptr);
6882 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
6884 -- Skip the tag of the predefined primitives no-thunks dispatch
6887 Next_Elmt (Iface_DT_Ptr);
6888 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
6890 Next_Elmt (Iface_DT_Ptr);
6892 end Register_Predefined_DT_Entry;
6896 Subp : constant Entity_Id := Entity (N);
6898 -- Start of processing for Freeze_Subprogram
6901 -- We suppress the initialization of the dispatch table entry when
6902 -- VM_Target because the dispatching mechanism is handled internally
6905 if Is_Dispatching_Operation (Subp)
6906 and then not Is_Abstract_Subprogram (Subp)
6907 and then Present (DTC_Entity (Subp))
6908 and then Present (Scope (DTC_Entity (Subp)))
6909 and then Tagged_Type_Expansion
6910 and then not Restriction_Active (No_Dispatching_Calls)
6911 and then RTE_Available (RE_Tag)
6914 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
6917 -- Handle private overridden primitives
6919 if not Is_CPP_Class (Typ) then
6920 Check_Overriding_Operation (Subp);
6923 -- We assume that imported CPP primitives correspond with objects
6924 -- whose constructor is in the CPP side; therefore we don't need
6925 -- to generate code to register them in the dispatch table.
6927 if Is_CPP_Class (Typ) then
6930 -- Handle CPP primitives found in derivations of CPP_Class types.
6931 -- These primitives must have been inherited from some parent, and
6932 -- there is no need to register them in the dispatch table because
6933 -- Build_Inherit_Prims takes care of the initialization of these
6936 elsif Is_Imported (Subp)
6937 and then (Convention (Subp) = Convention_CPP
6938 or else Convention (Subp) = Convention_C)
6942 -- Generate code to register the primitive in non statically
6943 -- allocated dispatch tables
6945 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
6947 -- When a primitive is frozen, enter its name in its dispatch
6950 if not Is_Interface (Typ)
6951 or else Present (Interface_Alias (Subp))
6953 if Is_Predefined_Dispatching_Operation (Subp) then
6954 Register_Predefined_DT_Entry (Subp);
6957 Insert_Actions_After (N,
6958 Register_Primitive (Loc, Prim => Subp));
6964 -- Mark functions that return by reference. Note that it cannot be part
6965 -- of the normal semantic analysis of the spec since the underlying
6966 -- returned type may not be known yet (for private types).
6969 Typ : constant Entity_Id := Etype (Subp);
6970 Utyp : constant Entity_Id := Underlying_Type (Typ);
6972 if Is_Immutably_Limited_Type (Typ) then
6973 Set_Returns_By_Ref (Subp);
6974 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6975 Set_Returns_By_Ref (Subp);
6978 end Freeze_Subprogram;
6980 -----------------------
6981 -- Is_Null_Procedure --
6982 -----------------------
6984 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
6985 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
6988 if Ekind (Subp) /= E_Procedure then
6991 -- Check if this is a declared null procedure
6993 elsif Nkind (Decl) = N_Subprogram_Declaration then
6994 if not Null_Present (Specification (Decl)) then
6997 elsif No (Body_To_Inline (Decl)) then
7000 -- Check if the body contains only a null statement, followed by
7001 -- the return statement added during expansion.
7005 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
7011 if Nkind (Orig_Bod) /= N_Subprogram_Body then
7014 -- We must skip SCIL nodes because they are currently
7015 -- implemented as special N_Null_Statement nodes.
7019 (Statements (Handled_Statement_Sequence (Orig_Bod)));
7020 Stat2 := Next_Non_SCIL_Node (Stat);
7023 Is_Empty_List (Declarations (Orig_Bod))
7024 and then Nkind (Stat) = N_Null_Statement
7028 (Nkind (Stat2) = N_Simple_Return_Statement
7029 and then No (Next (Stat2))));
7037 end Is_Null_Procedure;
7039 -------------------------------------------
7040 -- Make_Build_In_Place_Call_In_Allocator --
7041 -------------------------------------------
7043 procedure Make_Build_In_Place_Call_In_Allocator
7044 (Allocator : Node_Id;
7045 Function_Call : Node_Id)
7048 Func_Call : Node_Id := Function_Call;
7049 Function_Id : Entity_Id;
7050 Result_Subt : Entity_Id;
7051 Acc_Type : constant Entity_Id := Etype (Allocator);
7052 New_Allocator : Node_Id;
7053 Return_Obj_Access : Entity_Id;
7056 -- Step past qualification or unchecked conversion (the latter can occur
7057 -- in cases of calls to 'Input).
7059 if Nkind_In (Func_Call,
7060 N_Qualified_Expression,
7061 N_Unchecked_Type_Conversion)
7063 Func_Call := Expression (Func_Call);
7066 -- If the call has already been processed to add build-in-place actuals
7067 -- then return. This should not normally occur in an allocator context,
7068 -- but we add the protection as a defensive measure.
7070 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7074 -- Mark the call as processed as a build-in-place call
7076 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7078 Loc := Sloc (Function_Call);
7080 if Is_Entity_Name (Name (Func_Call)) then
7081 Function_Id := Entity (Name (Func_Call));
7083 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7084 Function_Id := Etype (Name (Func_Call));
7087 raise Program_Error;
7090 Result_Subt := Etype (Function_Id);
7092 -- When the result subtype is constrained, the return object must be
7093 -- allocated on the caller side, and access to it is passed to the
7096 -- Here and in related routines, we must examine the full view of the
7097 -- type, because the view at the point of call may differ from that
7098 -- that in the function body, and the expansion mechanism depends on
7099 -- the characteristics of the full view.
7101 if Is_Constrained (Underlying_Type (Result_Subt)) then
7103 -- Replace the initialized allocator of form "new T'(Func (...))"
7104 -- with an uninitialized allocator of form "new T", where T is the
7105 -- result subtype of the called function. The call to the function
7106 -- is handled separately further below.
7109 Make_Allocator (Loc,
7110 Expression => New_Reference_To (Result_Subt, Loc));
7111 Set_No_Initialization (New_Allocator);
7113 -- Copy attributes to new allocator. Note that the new allocator
7114 -- logically comes from source if the original one did, so copy the
7115 -- relevant flag. This ensures proper treatment of the restriction
7116 -- No_Implicit_Heap_Allocations in this case.
7118 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
7119 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
7120 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
7122 Rewrite (Allocator, New_Allocator);
7124 -- Create a new access object and initialize it to the result of the
7125 -- new uninitialized allocator. Note: we do not use Allocator as the
7126 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
7127 -- as this would create a sort of infinite "recursion".
7129 Return_Obj_Access := Make_Temporary (Loc, 'R');
7130 Set_Etype (Return_Obj_Access, Acc_Type);
7132 Insert_Action (Allocator,
7133 Make_Object_Declaration (Loc,
7134 Defining_Identifier => Return_Obj_Access,
7135 Object_Definition => New_Reference_To (Acc_Type, Loc),
7136 Expression => Relocate_Node (Allocator)));
7138 -- When the function has a controlling result, an allocation-form
7139 -- parameter must be passed indicating that the caller is allocating
7140 -- the result object. This is needed because such a function can be
7141 -- called as a dispatching operation and must be treated similarly
7142 -- to functions with unconstrained result subtypes.
7144 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7145 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7147 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7148 (Func_Call, Function_Id, Acc_Type);
7150 Add_Task_Actuals_To_Build_In_Place_Call
7151 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
7153 -- Add an implicit actual to the function call that provides access
7154 -- to the allocated object. An unchecked conversion to the (specific)
7155 -- result subtype of the function is inserted to handle cases where
7156 -- the access type of the allocator has a class-wide designated type.
7158 Add_Access_Actual_To_Build_In_Place_Call
7161 Make_Unchecked_Type_Conversion (Loc,
7162 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
7164 Make_Explicit_Dereference (Loc,
7165 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
7167 -- When the result subtype is unconstrained, the function itself must
7168 -- perform the allocation of the return object, so we pass parameters
7169 -- indicating that. We don't yet handle the case where the allocation
7170 -- must be done in a user-defined storage pool, which will require
7171 -- passing another actual or two to provide allocation/deallocation
7175 -- Pass an allocation parameter indicating that the function should
7176 -- allocate its result on the heap.
7178 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7179 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
7181 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7182 (Func_Call, Function_Id, Acc_Type);
7184 Add_Task_Actuals_To_Build_In_Place_Call
7185 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
7187 -- The caller does not provide the return object in this case, so we
7188 -- have to pass null for the object access actual.
7190 Add_Access_Actual_To_Build_In_Place_Call
7191 (Func_Call, Function_Id, Return_Object => Empty);
7194 -- If the build-in-place function call returns a controlled object,
7195 -- the finalization master will require a reference to routine
7196 -- Finalize_Address of the designated type. Setting this attribute
7197 -- is done in the same manner to expansion of allocators.
7199 if Needs_Finalization (Result_Subt) then
7201 -- Controlled types with supressed finalization do not need to
7202 -- associate the address of their Finalize_Address primitives with
7203 -- a master since they do not need a master to begin with.
7205 if Is_Library_Level_Entity (Acc_Type)
7206 and then Finalize_Storage_Only (Result_Subt)
7210 -- Do not generate the call to Make_Set_Finalize_Address for
7211 -- CodePeer compilations because Finalize_Address is never built.
7213 elsif not CodePeer_Mode then
7214 Insert_Action (Allocator,
7215 Make_Set_Finalize_Address_Call (Loc,
7216 Typ => Etype (Function_Id),
7217 Ptr_Typ => Acc_Type));
7221 -- Finally, replace the allocator node with a reference to the result
7222 -- of the function call itself (which will effectively be an access
7223 -- to the object created by the allocator).
7225 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
7226 Analyze_And_Resolve (Allocator, Acc_Type);
7227 end Make_Build_In_Place_Call_In_Allocator;
7229 ---------------------------------------------------
7230 -- Make_Build_In_Place_Call_In_Anonymous_Context --
7231 ---------------------------------------------------
7233 procedure Make_Build_In_Place_Call_In_Anonymous_Context
7234 (Function_Call : Node_Id)
7237 Func_Call : Node_Id := Function_Call;
7238 Function_Id : Entity_Id;
7239 Result_Subt : Entity_Id;
7240 Return_Obj_Id : Entity_Id;
7241 Return_Obj_Decl : Entity_Id;
7244 -- Step past qualification or unchecked conversion (the latter can occur
7245 -- in cases of calls to 'Input).
7247 if Nkind_In (Func_Call, N_Qualified_Expression,
7248 N_Unchecked_Type_Conversion)
7250 Func_Call := Expression (Func_Call);
7253 -- If the call has already been processed to add build-in-place actuals
7254 -- then return. One place this can occur is for calls to build-in-place
7255 -- functions that occur within a call to a protected operation, where
7256 -- due to rewriting and expansion of the protected call there can be
7257 -- more than one call to Expand_Actuals for the same set of actuals.
7259 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7263 -- Mark the call as processed as a build-in-place call
7265 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7267 Loc := Sloc (Function_Call);
7269 if Is_Entity_Name (Name (Func_Call)) then
7270 Function_Id := Entity (Name (Func_Call));
7272 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7273 Function_Id := Etype (Name (Func_Call));
7276 raise Program_Error;
7279 Result_Subt := Etype (Function_Id);
7281 -- If the build-in-place function returns a controlled object, then the
7282 -- object needs to be finalized immediately after the context. Since
7283 -- this case produces a transient scope, the servicing finalizer needs
7284 -- to name the returned object. Create a temporary which is initialized
7285 -- with the function call:
7287 -- Temp_Id : Func_Type := BIP_Func_Call;
7289 -- The initialization expression of the temporary will be rewritten by
7290 -- the expander using the appropriate mechanism in Make_Build_In_Place_
7291 -- Call_In_Object_Declaration.
7293 if Needs_Finalization (Result_Subt) then
7295 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
7296 Temp_Decl : Node_Id;
7299 -- Reset the guard on the function call since the following does
7300 -- not perform actual call expansion.
7302 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
7305 Make_Object_Declaration (Loc,
7306 Defining_Identifier => Temp_Id,
7307 Object_Definition =>
7308 New_Reference_To (Result_Subt, Loc),
7310 New_Copy_Tree (Function_Call));
7312 Insert_Action (Function_Call, Temp_Decl);
7314 Rewrite (Function_Call, New_Reference_To (Temp_Id, Loc));
7315 Analyze (Function_Call);
7318 -- When the result subtype is constrained, an object of the subtype is
7319 -- declared and an access value designating it is passed as an actual.
7321 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
7323 -- Create a temporary object to hold the function result
7325 Return_Obj_Id := Make_Temporary (Loc, 'R');
7326 Set_Etype (Return_Obj_Id, Result_Subt);
7329 Make_Object_Declaration (Loc,
7330 Defining_Identifier => Return_Obj_Id,
7331 Aliased_Present => True,
7332 Object_Definition => New_Reference_To (Result_Subt, Loc));
7334 Set_No_Initialization (Return_Obj_Decl);
7336 Insert_Action (Func_Call, Return_Obj_Decl);
7338 -- When the function has a controlling result, an allocation-form
7339 -- parameter must be passed indicating that the caller is allocating
7340 -- the result object. This is needed because such a function can be
7341 -- called as a dispatching operation and must be treated similarly
7342 -- to functions with unconstrained result subtypes.
7344 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7345 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7347 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7348 (Func_Call, Function_Id);
7350 Add_Task_Actuals_To_Build_In_Place_Call
7351 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7353 -- Add an implicit actual to the function call that provides access
7354 -- to the caller's return object.
7356 Add_Access_Actual_To_Build_In_Place_Call
7357 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
7359 -- When the result subtype is unconstrained, the function must allocate
7360 -- the return object in the secondary stack, so appropriate implicit
7361 -- parameters are added to the call to indicate that. A transient
7362 -- scope is established to ensure eventual cleanup of the result.
7365 -- Pass an allocation parameter indicating that the function should
7366 -- allocate its result on the secondary stack.
7368 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7369 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
7371 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7372 (Func_Call, Function_Id);
7374 Add_Task_Actuals_To_Build_In_Place_Call
7375 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7377 -- Pass a null value to the function since no return object is
7378 -- available on the caller side.
7380 Add_Access_Actual_To_Build_In_Place_Call
7381 (Func_Call, Function_Id, Empty);
7383 end Make_Build_In_Place_Call_In_Anonymous_Context;
7385 --------------------------------------------
7386 -- Make_Build_In_Place_Call_In_Assignment --
7387 --------------------------------------------
7389 procedure Make_Build_In_Place_Call_In_Assignment
7391 Function_Call : Node_Id)
7393 Lhs : constant Node_Id := Name (Assign);
7394 Func_Call : Node_Id := Function_Call;
7395 Func_Id : Entity_Id;
7399 Ptr_Typ : Entity_Id;
7400 Ptr_Typ_Decl : Node_Id;
7401 Result_Subt : Entity_Id;
7405 -- Step past qualification or unchecked conversion (the latter can occur
7406 -- in cases of calls to 'Input).
7408 if Nkind_In (Func_Call, N_Qualified_Expression,
7409 N_Unchecked_Type_Conversion)
7411 Func_Call := Expression (Func_Call);
7414 -- If the call has already been processed to add build-in-place actuals
7415 -- then return. This should not normally occur in an assignment context,
7416 -- but we add the protection as a defensive measure.
7418 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7422 -- Mark the call as processed as a build-in-place call
7424 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7426 Loc := Sloc (Function_Call);
7428 if Is_Entity_Name (Name (Func_Call)) then
7429 Func_Id := Entity (Name (Func_Call));
7431 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7432 Func_Id := Etype (Name (Func_Call));
7435 raise Program_Error;
7438 Result_Subt := Etype (Func_Id);
7440 -- When the result subtype is unconstrained, an additional actual must
7441 -- be passed to indicate that the caller is providing the return object.
7442 -- This parameter must also be passed when the called function has a
7443 -- controlling result, because dispatching calls to the function needs
7444 -- to be treated effectively the same as calls to class-wide functions.
7446 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7447 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
7449 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7450 (Func_Call, Func_Id);
7452 Add_Task_Actuals_To_Build_In_Place_Call
7453 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
7455 -- Add an implicit actual to the function call that provides access to
7456 -- the caller's return object.
7458 Add_Access_Actual_To_Build_In_Place_Call
7461 Make_Unchecked_Type_Conversion (Loc,
7462 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
7463 Expression => Relocate_Node (Lhs)));
7465 -- Create an access type designating the function's result subtype
7467 Ptr_Typ := Make_Temporary (Loc, 'A');
7470 Make_Full_Type_Declaration (Loc,
7471 Defining_Identifier => Ptr_Typ,
7473 Make_Access_To_Object_Definition (Loc,
7474 All_Present => True,
7475 Subtype_Indication =>
7476 New_Reference_To (Result_Subt, Loc)));
7477 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
7479 -- Finally, create an access object initialized to a reference to the
7482 Obj_Id := Make_Temporary (Loc, 'R');
7483 Set_Etype (Obj_Id, Ptr_Typ);
7486 Make_Object_Declaration (Loc,
7487 Defining_Identifier => Obj_Id,
7488 Object_Definition => New_Reference_To (Ptr_Typ, Loc),
7489 Expression => Make_Reference (Loc, Relocate_Node (Func_Call)));
7490 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
7492 Rewrite (Assign, Make_Null_Statement (Loc));
7494 -- Retrieve the target of the assignment
7496 if Nkind (Lhs) = N_Selected_Component then
7497 Target := Selector_Name (Lhs);
7498 elsif Nkind (Lhs) = N_Type_Conversion then
7499 Target := Expression (Lhs);
7504 -- If we are assigning to a return object or this is an expression of
7505 -- an extension aggregate, the target should either be an identifier
7506 -- or a simple expression. All other cases imply a different scenario.
7508 if Nkind (Target) in N_Has_Entity then
7509 Target := Entity (Target);
7513 end Make_Build_In_Place_Call_In_Assignment;
7515 ----------------------------------------------------
7516 -- Make_Build_In_Place_Call_In_Object_Declaration --
7517 ----------------------------------------------------
7519 procedure Make_Build_In_Place_Call_In_Object_Declaration
7520 (Object_Decl : Node_Id;
7521 Function_Call : Node_Id)
7524 Obj_Def_Id : constant Entity_Id :=
7525 Defining_Identifier (Object_Decl);
7527 Func_Call : Node_Id := Function_Call;
7528 Function_Id : Entity_Id;
7529 Result_Subt : Entity_Id;
7530 Caller_Object : Node_Id;
7531 Call_Deref : Node_Id;
7532 Ref_Type : Entity_Id;
7533 Ptr_Typ_Decl : Node_Id;
7536 Enclosing_Func : Entity_Id;
7537 Pass_Caller_Acc : Boolean := False;
7540 -- Step past qualification or unchecked conversion (the latter can occur
7541 -- in cases of calls to 'Input).
7543 if Nkind_In (Func_Call, N_Qualified_Expression,
7544 N_Unchecked_Type_Conversion)
7546 Func_Call := Expression (Func_Call);
7549 -- If the call has already been processed to add build-in-place actuals
7550 -- then return. This should not normally occur in an object declaration,
7551 -- but we add the protection as a defensive measure.
7553 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7557 -- Mark the call as processed as a build-in-place call
7559 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7561 Loc := Sloc (Function_Call);
7563 if Is_Entity_Name (Name (Func_Call)) then
7564 Function_Id := Entity (Name (Func_Call));
7566 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7567 Function_Id := Etype (Name (Func_Call));
7570 raise Program_Error;
7573 Result_Subt := Etype (Function_Id);
7575 -- In the constrained case, add an implicit actual to the function call
7576 -- that provides access to the declared object. An unchecked conversion
7577 -- to the (specific) result type of the function is inserted to handle
7578 -- the case where the object is declared with a class-wide type.
7580 if Is_Constrained (Underlying_Type (Result_Subt)) then
7582 Make_Unchecked_Type_Conversion (Loc,
7583 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
7584 Expression => New_Reference_To (Obj_Def_Id, Loc));
7586 -- When the function has a controlling result, an allocation-form
7587 -- parameter must be passed indicating that the caller is allocating
7588 -- the result object. This is needed because such a function can be
7589 -- called as a dispatching operation and must be treated similarly
7590 -- to functions with unconstrained result subtypes.
7592 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7593 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7595 -- If the function's result subtype is unconstrained and the object is
7596 -- a return object of an enclosing build-in-place function, then the
7597 -- implicit build-in-place parameters of the enclosing function must be
7598 -- passed along to the called function. (Unfortunately, this won't cover
7599 -- the case of extension aggregates where the ancestor part is a build-
7600 -- in-place unconstrained function call that should be passed along the
7601 -- caller's parameters. Currently those get mishandled by reassigning
7602 -- the result of the call to the aggregate return object, when the call
7603 -- result should really be directly built in place in the aggregate and
7604 -- not built in a temporary. ???)
7606 elsif Is_Return_Object (Defining_Identifier (Object_Decl)) then
7607 Pass_Caller_Acc := True;
7609 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
7611 -- If the enclosing function has a constrained result type, then
7612 -- caller allocation will be used.
7614 if Is_Constrained (Etype (Enclosing_Func)) then
7615 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7616 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7618 -- Otherwise, when the enclosing function has an unconstrained result
7619 -- type, the BIP_Alloc_Form formal of the enclosing function must be
7620 -- passed along to the callee.
7623 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7628 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
7632 -- Retrieve the BIPacc formal from the enclosing function and convert
7633 -- it to the access type of the callee's BIP_Object_Access formal.
7636 Make_Unchecked_Type_Conversion (Loc,
7640 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
7644 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
7647 -- In other unconstrained cases, pass an indication to do the allocation
7648 -- on the secondary stack and set Caller_Object to Empty so that a null
7649 -- value will be passed for the caller's object address. A transient
7650 -- scope is established to ensure eventual cleanup of the result.
7653 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7656 Alloc_Form => Secondary_Stack);
7657 Caller_Object := Empty;
7659 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
7662 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7663 (Func_Call, Function_Id);
7665 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
7666 and then Has_Task (Result_Subt)
7668 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
7670 -- Here we're passing along the master that was passed in to this
7673 Add_Task_Actuals_To_Build_In_Place_Call
7674 (Func_Call, Function_Id,
7677 (Build_In_Place_Formal (Enclosing_Func, BIP_Master), Loc));
7680 Add_Task_Actuals_To_Build_In_Place_Call
7681 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7684 Add_Access_Actual_To_Build_In_Place_Call
7685 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
7687 -- Create an access type designating the function's result subtype. We
7688 -- use the type of the original expression because it may be a call to
7689 -- an inherited operation, which the expansion has replaced with the
7690 -- parent operation that yields the parent type.
7692 Ref_Type := Make_Temporary (Loc, 'A');
7695 Make_Full_Type_Declaration (Loc,
7696 Defining_Identifier => Ref_Type,
7698 Make_Access_To_Object_Definition (Loc,
7699 All_Present => True,
7700 Subtype_Indication =>
7701 New_Reference_To (Etype (Function_Call), Loc)));
7703 -- The access type and its accompanying object must be inserted after
7704 -- the object declaration in the constrained case, so that the function
7705 -- call can be passed access to the object. In the unconstrained case,
7706 -- the access type and object must be inserted before the object, since
7707 -- the object declaration is rewritten to be a renaming of a dereference
7708 -- of the access object.
7710 if Is_Constrained (Underlying_Type (Result_Subt)) then
7711 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
7713 Insert_Action (Object_Decl, Ptr_Typ_Decl);
7716 -- Finally, create an access object initialized to a reference to the
7719 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
7721 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
7722 Set_Etype (Def_Id, Ref_Type);
7724 Insert_After_And_Analyze (Ptr_Typ_Decl,
7725 Make_Object_Declaration (Loc,
7726 Defining_Identifier => Def_Id,
7727 Object_Definition => New_Reference_To (Ref_Type, Loc),
7728 Expression => New_Expr));
7730 if Is_Constrained (Underlying_Type (Result_Subt)) then
7731 Set_Expression (Object_Decl, Empty);
7732 Set_No_Initialization (Object_Decl);
7734 -- In case of an unconstrained result subtype, rewrite the object
7735 -- declaration as an object renaming where the renamed object is a
7736 -- dereference of <function_Call>'reference:
7738 -- Obj : Subt renames <function_call>'Ref.all;
7742 Make_Explicit_Dereference (Loc,
7743 Prefix => New_Reference_To (Def_Id, Loc));
7745 Loc := Sloc (Object_Decl);
7746 Rewrite (Object_Decl,
7747 Make_Object_Renaming_Declaration (Loc,
7748 Defining_Identifier => Make_Temporary (Loc, 'D'),
7749 Access_Definition => Empty,
7750 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7751 Name => Call_Deref));
7753 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
7755 Analyze (Object_Decl);
7757 -- Replace the internal identifier of the renaming declaration's
7758 -- entity with identifier of the original object entity. We also have
7759 -- to exchange the entities containing their defining identifiers to
7760 -- ensure the correct replacement of the object declaration by the
7761 -- object renaming declaration to avoid homograph conflicts (since
7762 -- the object declaration's defining identifier was already entered
7763 -- in current scope). The Next_Entity links of the two entities also
7764 -- have to be swapped since the entities are part of the return
7765 -- scope's entity list and the list structure would otherwise be
7766 -- corrupted. Finally, the homonym chain must be preserved as well.
7769 Renaming_Def_Id : constant Entity_Id :=
7770 Defining_Identifier (Object_Decl);
7771 Next_Entity_Temp : constant Entity_Id :=
7772 Next_Entity (Renaming_Def_Id);
7774 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
7776 -- Swap next entity links in preparation for exchanging entities
7778 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
7779 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
7780 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
7782 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
7784 -- Preserve source indication of original declaration, so that
7785 -- xref information is properly generated for the right entity.
7787 Preserve_Comes_From_Source
7788 (Object_Decl, Original_Node (Object_Decl));
7790 Preserve_Comes_From_Source
7791 (Obj_Def_Id, Original_Node (Object_Decl));
7793 Set_Comes_From_Source (Renaming_Def_Id, False);
7797 -- If the object entity has a class-wide Etype, then we need to change
7798 -- it to the result subtype of the function call, because otherwise the
7799 -- object will be class-wide without an explicit initialization and
7800 -- won't be allocated properly by the back end. It seems unclean to make
7801 -- such a revision to the type at this point, and we should try to
7802 -- improve this treatment when build-in-place functions with class-wide
7803 -- results are implemented. ???
7805 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
7806 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
7808 end Make_Build_In_Place_Call_In_Object_Declaration;
7810 -----------------------------------
7811 -- Needs_BIP_Finalization_Master --
7812 -----------------------------------
7814 function Needs_BIP_Finalization_Master
7815 (Func_Id : Entity_Id) return Boolean
7817 pragma Assert (Is_Build_In_Place_Function (Func_Id));
7818 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
7822 not Restriction_Active (No_Finalization)
7823 and then Needs_Finalization (Func_Typ);
7824 end Needs_BIP_Finalization_Master;