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_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch2; use Exp_Ch2;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Intr; use Exp_Intr;
42 with Exp_Pakd; use Exp_Pakd;
43 with Exp_Tss; use Exp_Tss;
44 with Exp_Util; use Exp_Util;
45 with Exp_VFpt; use Exp_VFpt;
46 with Fname; use Fname;
47 with Freeze; use Freeze;
48 with Inline; use Inline;
50 with Namet; use Namet;
51 with Nlists; use Nlists;
52 with Nmake; use Nmake;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Ch6; use Sem_Ch6;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch12; use Sem_Ch12;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Res; use Sem_Res;
68 with Sem_SCIL; use Sem_SCIL;
69 with Sem_Util; use Sem_Util;
70 with Sinfo; use Sinfo;
71 with Snames; use Snames;
72 with Stand; use Stand;
73 with Targparm; use Targparm;
74 with Tbuild; use Tbuild;
75 with Uintp; use Uintp;
76 with Validsw; use Validsw;
78 package body Exp_Ch6 is
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Add_Access_Actual_To_Build_In_Place_Call
85 (Function_Call : Node_Id;
86 Function_Id : Entity_Id;
87 Return_Object : Node_Id;
88 Is_Access : Boolean := False);
89 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
90 -- object name given by Return_Object and add the attribute to the end of
91 -- the actual parameter list associated with the build-in-place function
92 -- call denoted by Function_Call. However, if Is_Access is True, then
93 -- Return_Object is already an access expression, in which case it's passed
94 -- along directly to the build-in-place function. Finally, if Return_Object
95 -- is empty, then pass a null literal as the actual.
97 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
98 (Function_Call : Node_Id;
99 Function_Id : Entity_Id;
100 Alloc_Form : BIP_Allocation_Form := Unspecified;
101 Alloc_Form_Exp : Node_Id := Empty;
102 Pool_Actual : Node_Id := Make_Null (No_Location));
103 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
104 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
105 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
106 -- otherwise pass a literal corresponding to the Alloc_Form parameter
107 -- (which must not be Unspecified in that case). Pool_Actual is the
108 -- parameter to pass to BIP_Storage_Pool.
110 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
111 (Func_Call : Node_Id;
113 Ptr_Typ : Entity_Id := Empty;
114 Master_Exp : Node_Id := Empty);
115 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
116 -- finalization actions, add an actual parameter which is a pointer to the
117 -- finalization master of the caller. If Master_Exp is not Empty, then that
118 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
119 -- will result in an automatic "null" value for the actual.
121 procedure Add_Task_Actuals_To_Build_In_Place_Call
122 (Function_Call : Node_Id;
123 Function_Id : Entity_Id;
124 Master_Actual : Node_Id);
125 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
126 -- contains tasks, add two actual parameters: the master, and a pointer to
127 -- the caller's activation chain. Master_Actual is the actual parameter
128 -- expression to pass for the master. In most cases, this is the current
129 -- master (_master). The two exceptions are: If the function call is the
130 -- initialization expression for an allocator, we pass the master of the
131 -- access type. If the function call is the initialization expression for a
132 -- return object, we pass along the master passed in by the caller. The
133 -- activation chain to pass is always the local one. Note: Master_Actual
134 -- can be Empty, but only if there are no tasks.
136 procedure Check_Overriding_Operation (Subp : Entity_Id);
137 -- Subp is a dispatching operation. Check whether it may override an
138 -- inherited private operation, in which case its DT entry is that of
139 -- the hidden operation, not the one it may have received earlier.
140 -- This must be done before emitting the code to set the corresponding
141 -- DT to the address of the subprogram. The actual placement of Subp in
142 -- the proper place in the list of primitive operations is done in
143 -- Declare_Inherited_Private_Subprograms, which also has to deal with
144 -- implicit operations. This duplication is unavoidable for now???
146 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
147 -- This procedure is called only if the subprogram body N, whose spec
148 -- has the given entity Spec, contains a parameterless recursive call.
149 -- It attempts to generate runtime code to detect if this a case of
150 -- infinite recursion.
152 -- The body is scanned to determine dependencies. If the only external
153 -- dependencies are on a small set of scalar variables, then the values
154 -- of these variables are captured on entry to the subprogram, and if
155 -- the values are not changed for the call, we know immediately that
156 -- we have an infinite recursion.
158 procedure Expand_Ctrl_Function_Call (N : Node_Id);
159 -- N is a function call which returns a controlled object. Transform the
160 -- call into a temporary which retrieves the returned object from the
161 -- secondary stack using 'reference.
163 procedure Expand_Inlined_Call
166 Orig_Subp : Entity_Id);
167 -- If called subprogram can be inlined by the front-end, retrieve the
168 -- analyzed body, replace formals with actuals and expand call in place.
169 -- Generate thunks for actuals that are expressions, and insert the
170 -- corresponding constant declarations before the call. If the original
171 -- call is to a derived operation, the return type is the one of the
172 -- derived operation, but the body is that of the original, so return
173 -- expressions in the body must be converted to the desired type (which
174 -- is simply not noted in the tree without inline expansion).
176 procedure Expand_Non_Function_Return (N : Node_Id);
177 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
178 -- a procedure body, entry body, accept statement, or extended return
179 -- statement. Note that all non-function returns are simple return
182 function Expand_Protected_Object_Reference
184 Scop : Entity_Id) return Node_Id;
186 procedure Expand_Protected_Subprogram_Call
190 -- A call to a protected subprogram within the protected object may appear
191 -- as a regular call. The list of actuals must be expanded to contain a
192 -- reference to the object itself, and the call becomes a call to the
193 -- corresponding protected subprogram.
195 function Has_Unconstrained_Access_Discriminants
196 (Subtyp : Entity_Id) return Boolean;
197 -- Returns True if the given subtype is unconstrained and has one
198 -- or more access discriminants.
200 procedure Expand_Simple_Function_Return (N : Node_Id);
201 -- Expand simple return from function. In the case where we are returning
202 -- from a function body this is called by Expand_N_Simple_Return_Statement.
204 ----------------------------------------------
205 -- Add_Access_Actual_To_Build_In_Place_Call --
206 ----------------------------------------------
208 procedure Add_Access_Actual_To_Build_In_Place_Call
209 (Function_Call : Node_Id;
210 Function_Id : Entity_Id;
211 Return_Object : Node_Id;
212 Is_Access : Boolean := False)
214 Loc : constant Source_Ptr := Sloc (Function_Call);
215 Obj_Address : Node_Id;
216 Obj_Acc_Formal : Entity_Id;
219 -- Locate the implicit access parameter in the called function
221 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
223 -- If no return object is provided, then pass null
225 if not Present (Return_Object) then
226 Obj_Address := Make_Null (Loc);
227 Set_Parent (Obj_Address, Function_Call);
229 -- If Return_Object is already an expression of an access type, then use
230 -- it directly, since it must be an access value denoting the return
231 -- object, and couldn't possibly be the return object itself.
234 Obj_Address := Return_Object;
235 Set_Parent (Obj_Address, Function_Call);
237 -- Apply Unrestricted_Access to caller's return object
241 Make_Attribute_Reference (Loc,
242 Prefix => Return_Object,
243 Attribute_Name => Name_Unrestricted_Access);
245 Set_Parent (Return_Object, Obj_Address);
246 Set_Parent (Obj_Address, Function_Call);
249 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
251 -- Build the parameter association for the new actual and add it to the
252 -- end of the function's actuals.
254 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
255 end Add_Access_Actual_To_Build_In_Place_Call;
257 ------------------------------------------------------
258 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
259 ------------------------------------------------------
261 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
262 (Function_Call : Node_Id;
263 Function_Id : Entity_Id;
264 Alloc_Form : BIP_Allocation_Form := Unspecified;
265 Alloc_Form_Exp : Node_Id := Empty;
266 Pool_Actual : Node_Id := Make_Null (No_Location))
268 Loc : constant Source_Ptr := Sloc (Function_Call);
269 Alloc_Form_Actual : Node_Id;
270 Alloc_Form_Formal : Node_Id;
271 Pool_Formal : Node_Id;
274 -- The allocation form generally doesn't need to be passed in the case
275 -- of a constrained result subtype, since normally the caller performs
276 -- the allocation in that case. However this formal is still needed in
277 -- the case where the function has a tagged result, because generally
278 -- such functions can be called in a dispatching context and such calls
279 -- must be handled like calls to class-wide functions.
281 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
282 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
287 -- Locate the implicit allocation form parameter in the called function.
288 -- Maybe it would be better for each implicit formal of a build-in-place
289 -- function to have a flag or a Uint attribute to identify it. ???
291 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
293 if Present (Alloc_Form_Exp) then
294 pragma Assert (Alloc_Form = Unspecified);
296 Alloc_Form_Actual := Alloc_Form_Exp;
299 pragma Assert (Alloc_Form /= Unspecified);
302 Make_Integer_Literal (Loc,
303 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
306 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
308 -- Build the parameter association for the new actual and add it to the
309 -- end of the function's actuals.
311 Add_Extra_Actual_To_Call
312 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
314 -- Pass the Storage_Pool parameter. This parameter is omitted on .NET
315 -- and JVM as those targets do not support pools.
317 if VM_Target = No_VM then
318 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
319 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
320 Add_Extra_Actual_To_Call
321 (Function_Call, Pool_Formal, Pool_Actual);
323 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
325 -----------------------------------------------------------
326 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
327 -----------------------------------------------------------
329 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
330 (Func_Call : Node_Id;
332 Ptr_Typ : Entity_Id := Empty;
333 Master_Exp : Node_Id := Empty)
336 if not Needs_BIP_Finalization_Master (Func_Id) then
341 Formal : constant Entity_Id :=
342 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
343 Loc : constant Source_Ptr := Sloc (Func_Call);
346 Desig_Typ : Entity_Id;
349 -- If there is a finalization master actual, such as the implicit
350 -- finalization master of an enclosing build-in-place function,
351 -- then this must be added as an extra actual of the call.
353 if Present (Master_Exp) then
354 Actual := Master_Exp;
356 -- Case where the context does not require an actual master
358 elsif No (Ptr_Typ) then
359 Actual := Make_Null (Loc);
362 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
364 -- Check for a library-level access type whose designated type has
365 -- supressed finalization. Such an access types lack a master.
366 -- Pass a null actual to the callee in order to signal a missing
369 if Is_Library_Level_Entity (Ptr_Typ)
370 and then Finalize_Storage_Only (Desig_Typ)
372 Actual := Make_Null (Loc);
374 -- Types in need of finalization actions
376 elsif Needs_Finalization (Desig_Typ) then
378 -- The general mechanism of creating finalization masters for
379 -- anonymous access types is disabled by default, otherwise
380 -- finalization masters will pop all over the place. Such types
381 -- use context-specific masters.
383 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
384 and then No (Finalization_Master (Ptr_Typ))
386 Build_Finalization_Master
388 Ins_Node => Associated_Node_For_Itype (Ptr_Typ),
389 Encl_Scope => Scope (Ptr_Typ));
392 -- Access-to-controlled types should always have a master
394 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
397 Make_Attribute_Reference (Loc,
399 New_Reference_To (Finalization_Master (Ptr_Typ), Loc),
400 Attribute_Name => Name_Unrestricted_Access);
405 Actual := Make_Null (Loc);
409 Analyze_And_Resolve (Actual, Etype (Formal));
411 -- Build the parameter association for the new actual and add it to
412 -- the end of the function's actuals.
414 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
416 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
418 ------------------------------
419 -- Add_Extra_Actual_To_Call --
420 ------------------------------
422 procedure Add_Extra_Actual_To_Call
423 (Subprogram_Call : Node_Id;
424 Extra_Formal : Entity_Id;
425 Extra_Actual : Node_Id)
427 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
428 Param_Assoc : Node_Id;
432 Make_Parameter_Association (Loc,
433 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
434 Explicit_Actual_Parameter => Extra_Actual);
436 Set_Parent (Param_Assoc, Subprogram_Call);
437 Set_Parent (Extra_Actual, Param_Assoc);
439 if Present (Parameter_Associations (Subprogram_Call)) then
440 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
441 N_Parameter_Association
444 -- Find last named actual, and append
449 L := First_Actual (Subprogram_Call);
450 while Present (L) loop
451 if No (Next_Actual (L)) then
452 Set_Next_Named_Actual (Parent (L), Extra_Actual);
460 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
463 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
466 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
467 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
469 end Add_Extra_Actual_To_Call;
471 ---------------------------------------------
472 -- Add_Task_Actuals_To_Build_In_Place_Call --
473 ---------------------------------------------
475 procedure Add_Task_Actuals_To_Build_In_Place_Call
476 (Function_Call : Node_Id;
477 Function_Id : Entity_Id;
478 Master_Actual : Node_Id)
480 Loc : constant Source_Ptr := Sloc (Function_Call);
481 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
482 Actual : Node_Id := Master_Actual;
485 -- No such extra parameters are needed if there are no tasks
487 if not Has_Task (Result_Subt) then
491 -- Use a dummy _master actual in case of No_Task_Hierarchy
493 if Restriction_Active (No_Task_Hierarchy) then
494 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
496 -- In the case where we use the master associated with an access type,
497 -- the actual is an entity and requires an explicit reference.
499 elsif Nkind (Actual) = N_Defining_Identifier then
500 Actual := New_Reference_To (Actual, Loc);
506 Master_Formal : Node_Id;
508 -- Locate implicit master parameter in the called function
510 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Master);
512 Analyze_And_Resolve (Actual, Etype (Master_Formal));
514 -- Build the parameter association for the new actual and add it to
515 -- the end of the function's actuals.
517 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
520 -- The activation chain
523 Activation_Chain_Actual : Node_Id;
524 Activation_Chain_Formal : Node_Id;
527 -- Locate implicit activation chain parameter in the called function
529 Activation_Chain_Formal :=
530 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
532 -- Create the actual which is a pointer to the current activation
535 Activation_Chain_Actual :=
536 Make_Attribute_Reference (Loc,
537 Prefix => Make_Identifier (Loc, Name_uChain),
538 Attribute_Name => Name_Unrestricted_Access);
541 (Activation_Chain_Actual, Etype (Activation_Chain_Formal));
543 -- Build the parameter association for the new actual and add it to
544 -- the end of the function's actuals.
546 Add_Extra_Actual_To_Call
547 (Function_Call, Activation_Chain_Formal, Activation_Chain_Actual);
549 end Add_Task_Actuals_To_Build_In_Place_Call;
551 -----------------------
552 -- BIP_Formal_Suffix --
553 -----------------------
555 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
558 when BIP_Alloc_Form =>
560 when BIP_Storage_Pool =>
561 return "BIPstoragepool";
562 when BIP_Finalization_Master =>
563 return "BIPfinalizationmaster";
566 when BIP_Activation_Chain =>
567 return "BIPactivationchain";
568 when BIP_Object_Access =>
571 end BIP_Formal_Suffix;
573 ---------------------------
574 -- Build_In_Place_Formal --
575 ---------------------------
577 function Build_In_Place_Formal
579 Kind : BIP_Formal_Kind) return Entity_Id
581 Extra_Formal : Entity_Id := Extra_Formals (Func);
584 -- Maybe it would be better for each implicit formal of a build-in-place
585 -- function to have a flag or a Uint attribute to identify it. ???
587 -- The return type in the function declaration may have been a limited
588 -- view, and the extra formals for the function were not generated at
589 -- that point. At the point of call the full view must be available and
590 -- the extra formals can be created.
592 if No (Extra_Formal) then
593 Create_Extra_Formals (Func);
594 Extra_Formal := Extra_Formals (Func);
598 pragma Assert (Present (Extra_Formal));
600 Chars (Extra_Formal) =
601 New_External_Name (Chars (Func), BIP_Formal_Suffix (Kind));
602 Next_Formal_With_Extras (Extra_Formal);
606 end Build_In_Place_Formal;
608 --------------------------------
609 -- Check_Overriding_Operation --
610 --------------------------------
612 procedure Check_Overriding_Operation (Subp : Entity_Id) is
613 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
614 Op_List : constant Elist_Id := Primitive_Operations (Typ);
620 if Is_Derived_Type (Typ)
621 and then not Is_Private_Type (Typ)
622 and then In_Open_Scopes (Scope (Etype (Typ)))
623 and then Is_Base_Type (Typ)
625 -- Subp overrides an inherited private operation if there is an
626 -- inherited operation with a different name than Subp (see
627 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
628 -- same name as Subp.
630 Op_Elmt := First_Elmt (Op_List);
631 while Present (Op_Elmt) loop
632 Prim_Op := Node (Op_Elmt);
633 Par_Op := Alias (Prim_Op);
636 and then not Comes_From_Source (Prim_Op)
637 and then Chars (Prim_Op) /= Chars (Par_Op)
638 and then Chars (Par_Op) = Chars (Subp)
639 and then Is_Hidden (Par_Op)
640 and then Type_Conformant (Prim_Op, Subp)
642 Set_DT_Position (Subp, DT_Position (Prim_Op));
648 end Check_Overriding_Operation;
650 -------------------------------
651 -- Detect_Infinite_Recursion --
652 -------------------------------
654 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
655 Loc : constant Source_Ptr := Sloc (N);
657 Var_List : constant Elist_Id := New_Elmt_List;
658 -- List of globals referenced by body of procedure
660 Call_List : constant Elist_Id := New_Elmt_List;
661 -- List of recursive calls in body of procedure
663 Shad_List : constant Elist_Id := New_Elmt_List;
664 -- List of entity id's for entities created to capture the value of
665 -- referenced globals on entry to the procedure.
667 Scop : constant Uint := Scope_Depth (Spec);
668 -- This is used to record the scope depth of the current procedure, so
669 -- that we can identify global references.
671 Max_Vars : constant := 4;
672 -- Do not test more than four global variables
674 Count_Vars : Natural := 0;
675 -- Count variables found so far
687 function Process (Nod : Node_Id) return Traverse_Result;
688 -- Function to traverse the subprogram body (using Traverse_Func)
694 function Process (Nod : Node_Id) return Traverse_Result is
698 if Nkind (Nod) = N_Procedure_Call_Statement then
700 -- Case of one of the detected recursive calls
702 if Is_Entity_Name (Name (Nod))
703 and then Has_Recursive_Call (Entity (Name (Nod)))
704 and then Entity (Name (Nod)) = Spec
706 Append_Elmt (Nod, Call_List);
709 -- Any other procedure call may have side effects
715 -- A call to a pure function can always be ignored
717 elsif Nkind (Nod) = N_Function_Call
718 and then Is_Entity_Name (Name (Nod))
719 and then Is_Pure (Entity (Name (Nod)))
723 -- Case of an identifier reference
725 elsif Nkind (Nod) = N_Identifier then
728 -- If no entity, then ignore the reference
730 -- Not clear why this can happen. To investigate, remove this
731 -- test and look at the crash that occurs here in 3401-004 ???
736 -- Ignore entities with no Scope, again not clear how this
737 -- can happen, to investigate, look at 4108-008 ???
739 elsif No (Scope (Ent)) then
742 -- Ignore the reference if not to a more global object
744 elsif Scope_Depth (Scope (Ent)) >= Scop then
747 -- References to types, exceptions and constants are always OK
750 or else Ekind (Ent) = E_Exception
751 or else Ekind (Ent) = E_Constant
755 -- If other than a non-volatile scalar variable, we have some
756 -- kind of global reference (e.g. to a function) that we cannot
757 -- deal with so we forget the attempt.
759 elsif Ekind (Ent) /= E_Variable
760 or else not Is_Scalar_Type (Etype (Ent))
761 or else Treat_As_Volatile (Ent)
765 -- Otherwise we have a reference to a global scalar
768 -- Loop through global entities already detected
770 Elm := First_Elmt (Var_List);
772 -- If not detected before, record this new global reference
775 Count_Vars := Count_Vars + 1;
777 if Count_Vars <= Max_Vars then
778 Append_Elmt (Entity (Nod), Var_List);
785 -- If recorded before, ignore
787 elsif Node (Elm) = Entity (Nod) then
790 -- Otherwise keep looking
800 -- For all other node kinds, recursively visit syntactic children
807 function Traverse_Body is new Traverse_Func (Process);
809 -- Start of processing for Detect_Infinite_Recursion
812 -- Do not attempt detection in No_Implicit_Conditional mode, since we
813 -- won't be able to generate the code to handle the recursion in any
816 if Restriction_Active (No_Implicit_Conditionals) then
820 -- Otherwise do traversal and quit if we get abandon signal
822 if Traverse_Body (N) = Abandon then
825 -- We must have a call, since Has_Recursive_Call was set. If not just
826 -- ignore (this is only an error check, so if we have a funny situation,
827 -- due to bugs or errors, we do not want to bomb!)
829 elsif Is_Empty_Elmt_List (Call_List) then
833 -- Here is the case where we detect recursion at compile time
835 -- Push our current scope for analyzing the declarations and code that
836 -- we will insert for the checking.
840 -- This loop builds temporary variables for each of the referenced
841 -- globals, so that at the end of the loop the list Shad_List contains
842 -- these temporaries in one-to-one correspondence with the elements in
846 Elm := First_Elmt (Var_List);
847 while Present (Elm) loop
849 Ent := Make_Temporary (Loc, 'S');
850 Append_Elmt (Ent, Shad_List);
852 -- Insert a declaration for this temporary at the start of the
853 -- declarations for the procedure. The temporaries are declared as
854 -- constant objects initialized to the current values of the
855 -- corresponding temporaries.
858 Make_Object_Declaration (Loc,
859 Defining_Identifier => Ent,
860 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
861 Constant_Present => True,
862 Expression => New_Occurrence_Of (Var, Loc));
865 Prepend (Decl, Declarations (N));
867 Insert_After (Last, Decl);
875 -- Loop through calls
877 Call := First_Elmt (Call_List);
878 while Present (Call) loop
880 -- Build a predicate expression of the form
883 -- and then global1 = temp1
884 -- and then global2 = temp2
887 -- This predicate determines if any of the global values
888 -- referenced by the procedure have changed since the
889 -- current call, if not an infinite recursion is assured.
891 Test := New_Occurrence_Of (Standard_True, Loc);
893 Elm1 := First_Elmt (Var_List);
894 Elm2 := First_Elmt (Shad_List);
895 while Present (Elm1) loop
901 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
902 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
908 -- Now we replace the call with the sequence
910 -- if no-changes (see above) then
911 -- raise Storage_Error;
916 Rewrite (Node (Call),
917 Make_If_Statement (Loc,
919 Then_Statements => New_List (
920 Make_Raise_Storage_Error (Loc,
921 Reason => SE_Infinite_Recursion)),
923 Else_Statements => New_List (
924 Relocate_Node (Node (Call)))));
926 Analyze (Node (Call));
931 -- Remove temporary scope stack entry used for analysis
934 end Detect_Infinite_Recursion;
940 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
941 Loc : constant Source_Ptr := Sloc (N);
946 E_Formal : Entity_Id;
948 procedure Add_Call_By_Copy_Code;
949 -- For cases where the parameter must be passed by copy, this routine
950 -- generates a temporary variable into which the actual is copied and
951 -- then passes this as the parameter. For an OUT or IN OUT parameter,
952 -- an assignment is also generated to copy the result back. The call
953 -- also takes care of any constraint checks required for the type
954 -- conversion case (on both the way in and the way out).
956 procedure Add_Simple_Call_By_Copy_Code;
957 -- This is similar to the above, but is used in cases where we know
958 -- that all that is needed is to simply create a temporary and copy
959 -- the value in and out of the temporary.
961 procedure Check_Fortran_Logical;
962 -- A value of type Logical that is passed through a formal parameter
963 -- must be normalized because .TRUE. usually does not have the same
964 -- representation as True. We assume that .FALSE. = False = 0.
965 -- What about functions that return a logical type ???
967 function Is_Legal_Copy return Boolean;
968 -- Check that an actual can be copied before generating the temporary
969 -- to be used in the call. If the actual is of a by_reference type then
970 -- the program is illegal (this can only happen in the presence of
971 -- rep. clauses that force an incorrect alignment). If the formal is
972 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
973 -- the effect that this might lead to unaligned arguments.
975 function Make_Var (Actual : Node_Id) return Entity_Id;
976 -- Returns an entity that refers to the given actual parameter,
977 -- Actual (not including any type conversion). If Actual is an
978 -- entity name, then this entity is returned unchanged, otherwise
979 -- a renaming is created to provide an entity for the actual.
981 procedure Reset_Packed_Prefix;
982 -- The expansion of a packed array component reference is delayed in
983 -- the context of a call. Now we need to complete the expansion, so we
984 -- unmark the analyzed bits in all prefixes.
986 ---------------------------
987 -- Add_Call_By_Copy_Code --
988 ---------------------------
990 procedure Add_Call_By_Copy_Code is
996 F_Typ : constant Entity_Id := Etype (Formal);
1001 if not Is_Legal_Copy then
1005 Temp := Make_Temporary (Loc, 'T', Actual);
1007 -- Use formal type for temp, unless formal type is an unconstrained
1008 -- array, in which case we don't have to worry about bounds checks,
1009 -- and we use the actual type, since that has appropriate bounds.
1011 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1012 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1014 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1017 if Nkind (Actual) = N_Type_Conversion then
1018 V_Typ := Etype (Expression (Actual));
1020 -- If the formal is an (in-)out parameter, capture the name
1021 -- of the variable in order to build the post-call assignment.
1023 Var := Make_Var (Expression (Actual));
1025 Crep := not Same_Representation
1026 (F_Typ, Etype (Expression (Actual)));
1029 V_Typ := Etype (Actual);
1030 Var := Make_Var (Actual);
1034 -- Setup initialization for case of in out parameter, or an out
1035 -- parameter where the formal is an unconstrained array (in the
1036 -- latter case, we have to pass in an object with bounds).
1038 -- If this is an out parameter, the initial copy is wasteful, so as
1039 -- an optimization for the one-dimensional case we extract the
1040 -- bounds of the actual and build an uninitialized temporary of the
1043 if Ekind (Formal) = E_In_Out_Parameter
1044 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1046 if Nkind (Actual) = N_Type_Conversion then
1047 if Conversion_OK (Actual) then
1048 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1050 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1053 elsif Ekind (Formal) = E_Out_Parameter
1054 and then Is_Array_Type (F_Typ)
1055 and then Number_Dimensions (F_Typ) = 1
1056 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1058 -- Actual is a one-dimensional array or slice, and the type
1059 -- requires no initialization. Create a temporary of the
1060 -- right size, but do not copy actual into it (optimization).
1064 Make_Subtype_Indication (Loc,
1066 New_Occurrence_Of (F_Typ, Loc),
1068 Make_Index_Or_Discriminant_Constraint (Loc,
1069 Constraints => New_List (
1072 Make_Attribute_Reference (Loc,
1073 Prefix => New_Occurrence_Of (Var, Loc),
1074 Attribute_Name => Name_First),
1076 Make_Attribute_Reference (Loc,
1077 Prefix => New_Occurrence_Of (Var, Loc),
1078 Attribute_Name => Name_Last)))));
1081 Init := New_Occurrence_Of (Var, Loc);
1084 -- An initialization is created for packed conversions as
1085 -- actuals for out parameters to enable Make_Object_Declaration
1086 -- to determine the proper subtype for N_Node. Note that this
1087 -- is wasteful because the extra copying on the call side is
1088 -- not required for such out parameters. ???
1090 elsif Ekind (Formal) = E_Out_Parameter
1091 and then Nkind (Actual) = N_Type_Conversion
1092 and then (Is_Bit_Packed_Array (F_Typ)
1094 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1096 if Conversion_OK (Actual) then
1097 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1099 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1102 elsif Ekind (Formal) = E_In_Parameter then
1104 -- Handle the case in which the actual is a type conversion
1106 if Nkind (Actual) = N_Type_Conversion then
1107 if Conversion_OK (Actual) then
1108 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1110 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1113 Init := New_Occurrence_Of (Var, Loc);
1121 Make_Object_Declaration (Loc,
1122 Defining_Identifier => Temp,
1123 Object_Definition => Indic,
1124 Expression => Init);
1125 Set_Assignment_OK (N_Node);
1126 Insert_Action (N, N_Node);
1128 -- Now, normally the deal here is that we use the defining
1129 -- identifier created by that object declaration. There is
1130 -- one exception to this. In the change of representation case
1131 -- the above declaration will end up looking like:
1133 -- temp : type := identifier;
1135 -- And in this case we might as well use the identifier directly
1136 -- and eliminate the temporary. Note that the analysis of the
1137 -- declaration was not a waste of time in that case, since it is
1138 -- what generated the necessary change of representation code. If
1139 -- the change of representation introduced additional code, as in
1140 -- a fixed-integer conversion, the expression is not an identifier
1141 -- and must be kept.
1144 and then Present (Expression (N_Node))
1145 and then Is_Entity_Name (Expression (N_Node))
1147 Temp := Entity (Expression (N_Node));
1148 Rewrite (N_Node, Make_Null_Statement (Loc));
1151 -- For IN parameter, all we do is to replace the actual
1153 if Ekind (Formal) = E_In_Parameter then
1154 Rewrite (Actual, New_Reference_To (Temp, Loc));
1157 -- Processing for OUT or IN OUT parameter
1160 -- Kill current value indications for the temporary variable we
1161 -- created, since we just passed it as an OUT parameter.
1163 Kill_Current_Values (Temp);
1164 Set_Is_Known_Valid (Temp, False);
1166 -- If type conversion, use reverse conversion on exit
1168 if Nkind (Actual) = N_Type_Conversion then
1169 if Conversion_OK (Actual) then
1170 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1172 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1175 Expr := New_Occurrence_Of (Temp, Loc);
1178 Rewrite (Actual, New_Reference_To (Temp, Loc));
1181 -- If the actual is a conversion of a packed reference, it may
1182 -- already have been expanded by Remove_Side_Effects, and the
1183 -- resulting variable is a temporary which does not designate
1184 -- the proper out-parameter, which may not be addressable. In
1185 -- that case, generate an assignment to the original expression
1186 -- (before expansion of the packed reference) so that the proper
1187 -- expansion of assignment to a packed component can take place.
1194 if Is_Renaming_Of_Object (Var)
1195 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1196 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1197 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1198 = N_Indexed_Component
1200 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1202 Obj := Renamed_Object (Var);
1204 Make_Selected_Component (Loc,
1206 New_Copy_Tree (Original_Node (Prefix (Obj))),
1207 Selector_Name => New_Copy (Selector_Name (Obj)));
1208 Reset_Analyzed_Flags (Lhs);
1211 Lhs := New_Occurrence_Of (Var, Loc);
1214 Set_Assignment_OK (Lhs);
1216 if Is_Access_Type (E_Formal)
1217 and then Is_Entity_Name (Lhs)
1219 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1221 -- Copyback target is an Ada 2012 stand-alone object
1222 -- of an anonymous access type
1224 pragma Assert (Ada_Version >= Ada_2012);
1226 if Type_Access_Level (E_Formal) >
1227 Object_Access_Level (Lhs)
1229 Append_To (Post_Call,
1230 Make_Raise_Program_Error (Loc,
1231 Reason => PE_Accessibility_Check_Failed));
1234 Append_To (Post_Call,
1235 Make_Assignment_Statement (Loc,
1237 Expression => Expr));
1239 -- We would like to somehow suppress generation of the
1240 -- extra_accessibility assignment generated by the expansion
1241 -- of the above assignment statement. It's not a correctness
1242 -- issue because the following assignment renders it dead,
1243 -- but generating back-to-back assignments to the same
1244 -- target is undesirable. ???
1246 Append_To (Post_Call,
1247 Make_Assignment_Statement (Loc,
1248 Name => New_Occurrence_Of (
1249 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1250 Expression => Make_Integer_Literal (Loc,
1251 Type_Access_Level (E_Formal))));
1254 Append_To (Post_Call,
1255 Make_Assignment_Statement (Loc,
1257 Expression => Expr));
1261 end Add_Call_By_Copy_Code;
1263 ----------------------------------
1264 -- Add_Simple_Call_By_Copy_Code --
1265 ----------------------------------
1267 procedure Add_Simple_Call_By_Copy_Code is
1275 F_Typ : constant Entity_Id := Etype (Formal);
1278 if not Is_Legal_Copy then
1282 -- Use formal type for temp, unless formal type is an unconstrained
1283 -- array, in which case we don't have to worry about bounds checks,
1284 -- and we use the actual type, since that has appropriate bounds.
1286 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1287 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1289 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1292 -- Prepare to generate code
1294 Reset_Packed_Prefix;
1296 Temp := Make_Temporary (Loc, 'T', Actual);
1297 Incod := Relocate_Node (Actual);
1298 Outcod := New_Copy_Tree (Incod);
1300 -- Generate declaration of temporary variable, initializing it
1301 -- with the input parameter unless we have an OUT formal or
1302 -- this is an initialization call.
1304 -- If the formal is an out parameter with discriminants, the
1305 -- discriminants must be captured even if the rest of the object
1306 -- is in principle uninitialized, because the discriminants may
1307 -- be read by the called subprogram.
1309 if Ekind (Formal) = E_Out_Parameter then
1312 if Has_Discriminants (Etype (Formal)) then
1313 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1316 elsif Inside_Init_Proc then
1318 -- Could use a comment here to match comment below ???
1320 if Nkind (Actual) /= N_Selected_Component
1322 not Has_Discriminant_Dependent_Constraint
1323 (Entity (Selector_Name (Actual)))
1327 -- Otherwise, keep the component in order to generate the proper
1328 -- actual subtype, that depends on enclosing discriminants.
1336 Make_Object_Declaration (Loc,
1337 Defining_Identifier => Temp,
1338 Object_Definition => Indic,
1339 Expression => Incod);
1344 -- If the call is to initialize a component of a composite type,
1345 -- and the component does not depend on discriminants, use the
1346 -- actual type of the component. This is required in case the
1347 -- component is constrained, because in general the formal of the
1348 -- initialization procedure will be unconstrained. Note that if
1349 -- the component being initialized is constrained by an enclosing
1350 -- discriminant, the presence of the initialization in the
1351 -- declaration will generate an expression for the actual subtype.
1353 Set_No_Initialization (Decl);
1354 Set_Object_Definition (Decl,
1355 New_Occurrence_Of (Etype (Actual), Loc));
1358 Insert_Action (N, Decl);
1360 -- The actual is simply a reference to the temporary
1362 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1364 -- Generate copy out if OUT or IN OUT parameter
1366 if Ekind (Formal) /= E_In_Parameter then
1368 Rhs := New_Occurrence_Of (Temp, Loc);
1370 -- Deal with conversion
1372 if Nkind (Lhs) = N_Type_Conversion then
1373 Lhs := Expression (Lhs);
1374 Rhs := Convert_To (Etype (Actual), Rhs);
1377 Append_To (Post_Call,
1378 Make_Assignment_Statement (Loc,
1380 Expression => Rhs));
1381 Set_Assignment_OK (Name (Last (Post_Call)));
1383 end Add_Simple_Call_By_Copy_Code;
1385 ---------------------------
1386 -- Check_Fortran_Logical --
1387 ---------------------------
1389 procedure Check_Fortran_Logical is
1390 Logical : constant Entity_Id := Etype (Formal);
1393 -- Note: this is very incomplete, e.g. it does not handle arrays
1394 -- of logical values. This is really not the right approach at all???)
1397 if Convention (Subp) = Convention_Fortran
1398 and then Root_Type (Etype (Formal)) = Standard_Boolean
1399 and then Ekind (Formal) /= E_In_Parameter
1401 Var := Make_Var (Actual);
1402 Append_To (Post_Call,
1403 Make_Assignment_Statement (Loc,
1404 Name => New_Occurrence_Of (Var, Loc),
1406 Unchecked_Convert_To (
1409 Left_Opnd => New_Occurrence_Of (Var, Loc),
1411 Unchecked_Convert_To (
1413 New_Occurrence_Of (Standard_False, Loc))))));
1415 end Check_Fortran_Logical;
1421 function Is_Legal_Copy return Boolean is
1423 -- An attempt to copy a value of such a type can only occur if
1424 -- representation clauses give the actual a misaligned address.
1426 if Is_By_Reference_Type (Etype (Formal)) then
1428 ("misaligned actual cannot be passed by reference", Actual);
1431 -- For users of Starlet, we assume that the specification of by-
1432 -- reference mechanism is mandatory. This may lead to unaligned
1433 -- objects but at least for DEC legacy code it is known to work.
1434 -- The warning will alert users of this code that a problem may
1437 elsif Mechanism (Formal) = By_Reference
1438 and then Is_Valued_Procedure (Scope (Formal))
1441 ("by_reference actual may be misaligned?", Actual);
1453 function Make_Var (Actual : Node_Id) return Entity_Id is
1457 if Is_Entity_Name (Actual) then
1458 return Entity (Actual);
1461 Var := Make_Temporary (Loc, 'T', Actual);
1464 Make_Object_Renaming_Declaration (Loc,
1465 Defining_Identifier => Var,
1467 New_Occurrence_Of (Etype (Actual), Loc),
1468 Name => Relocate_Node (Actual));
1470 Insert_Action (N, N_Node);
1475 -------------------------
1476 -- Reset_Packed_Prefix --
1477 -------------------------
1479 procedure Reset_Packed_Prefix is
1480 Pfx : Node_Id := Actual;
1483 Set_Analyzed (Pfx, False);
1485 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1486 Pfx := Prefix (Pfx);
1488 end Reset_Packed_Prefix;
1490 -- Start of processing for Expand_Actuals
1493 Post_Call := New_List;
1495 Formal := First_Formal (Subp);
1496 Actual := First_Actual (N);
1497 while Present (Formal) loop
1498 E_Formal := Etype (Formal);
1500 if Is_Scalar_Type (E_Formal)
1501 or else Nkind (Actual) = N_Slice
1503 Check_Fortran_Logical;
1507 elsif Ekind (Formal) /= E_Out_Parameter then
1509 -- The unusual case of the current instance of a protected type
1510 -- requires special handling. This can only occur in the context
1511 -- of a call within the body of a protected operation.
1513 if Is_Entity_Name (Actual)
1514 and then Ekind (Entity (Actual)) = E_Protected_Type
1515 and then In_Open_Scopes (Entity (Actual))
1517 if Scope (Subp) /= Entity (Actual) then
1518 Error_Msg_N ("operation outside protected type may not "
1519 & "call back its protected operations?", Actual);
1523 Expand_Protected_Object_Reference (N, Entity (Actual)));
1526 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1527 -- build-in-place function, then a temporary return object needs
1528 -- to be created and access to it must be passed to the function.
1529 -- Currently we limit such functions to those with inherently
1530 -- limited result subtypes, but eventually we plan to expand the
1531 -- functions that are treated as build-in-place to include other
1532 -- composite result types.
1534 if Is_Build_In_Place_Function_Call (Actual) then
1535 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1538 Apply_Constraint_Check (Actual, E_Formal);
1540 -- Out parameter case. No constraint checks on access type
1543 elsif Is_Access_Type (E_Formal) then
1548 elsif Has_Discriminants (Base_Type (E_Formal))
1549 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1551 Apply_Constraint_Check (Actual, E_Formal);
1556 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1559 -- Processing for IN-OUT and OUT parameters
1561 if Ekind (Formal) /= E_In_Parameter then
1563 -- For type conversions of arrays, apply length/range checks
1565 if Is_Array_Type (E_Formal)
1566 and then Nkind (Actual) = N_Type_Conversion
1568 if Is_Constrained (E_Formal) then
1569 Apply_Length_Check (Expression (Actual), E_Formal);
1571 Apply_Range_Check (Expression (Actual), E_Formal);
1575 -- If argument is a type conversion for a type that is passed
1576 -- by copy, then we must pass the parameter by copy.
1578 if Nkind (Actual) = N_Type_Conversion
1580 (Is_Numeric_Type (E_Formal)
1581 or else Is_Access_Type (E_Formal)
1582 or else Is_Enumeration_Type (E_Formal)
1583 or else Is_Bit_Packed_Array (Etype (Formal))
1584 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1586 -- Also pass by copy if change of representation
1588 or else not Same_Representation
1590 Etype (Expression (Actual))))
1592 Add_Call_By_Copy_Code;
1594 -- References to components of bit packed arrays are expanded
1595 -- at this point, rather than at the point of analysis of the
1596 -- actuals, to handle the expansion of the assignment to
1597 -- [in] out parameters.
1599 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1600 Add_Simple_Call_By_Copy_Code;
1602 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1603 -- because the back-end cannot cope with such objects. In other
1604 -- cases where alignment forces a copy, the back-end generates
1605 -- it properly. It should not be generated unconditionally in the
1606 -- front-end because it does not know precisely the alignment
1607 -- requirements of the target, and makes too conservative an
1608 -- estimate, leading to superfluous copies or spurious errors
1609 -- on by-reference parameters.
1611 elsif Nkind (Actual) = N_Selected_Component
1613 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1614 and then not Represented_As_Scalar (Etype (Formal))
1616 Add_Simple_Call_By_Copy_Code;
1618 -- References to slices of bit packed arrays are expanded
1620 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1621 Add_Call_By_Copy_Code;
1623 -- References to possibly unaligned slices of arrays are expanded
1625 elsif Is_Possibly_Unaligned_Slice (Actual) then
1626 Add_Call_By_Copy_Code;
1628 -- Deal with access types where the actual subtype and the
1629 -- formal subtype are not the same, requiring a check.
1631 -- It is necessary to exclude tagged types because of "downward
1632 -- conversion" errors.
1634 elsif Is_Access_Type (E_Formal)
1635 and then not Same_Type (E_Formal, Etype (Actual))
1636 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1638 Add_Call_By_Copy_Code;
1640 -- If the actual is not a scalar and is marked for volatile
1641 -- treatment, whereas the formal is not volatile, then pass
1642 -- by copy unless it is a by-reference type.
1644 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1645 -- because this is the enforcement of a language rule that applies
1646 -- only to "real" volatile variables, not e.g. to the address
1647 -- clause overlay case.
1649 elsif Is_Entity_Name (Actual)
1650 and then Is_Volatile (Entity (Actual))
1651 and then not Is_By_Reference_Type (Etype (Actual))
1652 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1653 and then not Is_Volatile (E_Formal)
1655 Add_Call_By_Copy_Code;
1657 elsif Nkind (Actual) = N_Indexed_Component
1658 and then Is_Entity_Name (Prefix (Actual))
1659 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1661 Add_Call_By_Copy_Code;
1663 -- Add call-by-copy code for the case of scalar out parameters
1664 -- when it is not known at compile time that the subtype of the
1665 -- formal is a subrange of the subtype of the actual (or vice
1666 -- versa for in out parameters), in order to get range checks
1667 -- on such actuals. (Maybe this case should be handled earlier
1668 -- in the if statement???)
1670 elsif Is_Scalar_Type (E_Formal)
1672 (not In_Subrange_Of (E_Formal, Etype (Actual))
1674 (Ekind (Formal) = E_In_Out_Parameter
1675 and then not In_Subrange_Of (Etype (Actual), E_Formal)))
1677 -- Perhaps the setting back to False should be done within
1678 -- Add_Call_By_Copy_Code, since it could get set on other
1679 -- cases occurring above???
1681 if Do_Range_Check (Actual) then
1682 Set_Do_Range_Check (Actual, False);
1685 Add_Call_By_Copy_Code;
1688 -- Processing for IN parameters
1691 -- For IN parameters is in the packed array case, we expand an
1692 -- indexed component (the circuit in Exp_Ch4 deliberately left
1693 -- indexed components appearing as actuals untouched, so that
1694 -- the special processing above for the OUT and IN OUT cases
1695 -- could be performed. We could make the test in Exp_Ch4 more
1696 -- complex and have it detect the parameter mode, but it is
1697 -- easier simply to handle all cases here.)
1699 if Nkind (Actual) = N_Indexed_Component
1700 and then Is_Packed (Etype (Prefix (Actual)))
1702 Reset_Packed_Prefix;
1703 Expand_Packed_Element_Reference (Actual);
1705 -- If we have a reference to a bit packed array, we copy it, since
1706 -- the actual must be byte aligned.
1708 -- Is this really necessary in all cases???
1710 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1711 Add_Simple_Call_By_Copy_Code;
1713 -- If a non-scalar actual is possibly unaligned, we need a copy
1715 elsif Is_Possibly_Unaligned_Object (Actual)
1716 and then not Represented_As_Scalar (Etype (Formal))
1718 Add_Simple_Call_By_Copy_Code;
1720 -- Similarly, we have to expand slices of packed arrays here
1721 -- because the result must be byte aligned.
1723 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1724 Add_Call_By_Copy_Code;
1726 -- Only processing remaining is to pass by copy if this is a
1727 -- reference to a possibly unaligned slice, since the caller
1728 -- expects an appropriately aligned argument.
1730 elsif Is_Possibly_Unaligned_Slice (Actual) then
1731 Add_Call_By_Copy_Code;
1733 -- An unusual case: a current instance of an enclosing task can be
1734 -- an actual, and must be replaced by a reference to self.
1736 elsif Is_Entity_Name (Actual)
1737 and then Is_Task_Type (Entity (Actual))
1739 if In_Open_Scopes (Entity (Actual)) then
1741 (Make_Function_Call (Loc,
1742 Name => New_Reference_To (RTE (RE_Self), Loc))));
1745 -- A task type cannot otherwise appear as an actual
1748 raise Program_Error;
1753 Next_Formal (Formal);
1754 Next_Actual (Actual);
1757 -- Find right place to put post call stuff if it is present
1759 if not Is_Empty_List (Post_Call) then
1761 -- If call is not a list member, it must be the triggering statement
1762 -- of a triggering alternative or an entry call alternative, and we
1763 -- can add the post call stuff to the corresponding statement list.
1765 if not Is_List_Member (N) then
1767 P : constant Node_Id := Parent (N);
1770 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1771 N_Entry_Call_Alternative));
1773 if Is_Non_Empty_List (Statements (P)) then
1774 Insert_List_Before_And_Analyze
1775 (First (Statements (P)), Post_Call);
1777 Set_Statements (P, Post_Call);
1781 -- Otherwise, normal case where N is in a statement sequence,
1782 -- just put the post-call stuff after the call statement.
1785 Insert_Actions_After (N, Post_Call);
1789 -- The call node itself is re-analyzed in Expand_Call
1797 -- This procedure handles expansion of function calls and procedure call
1798 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1799 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1801 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1802 -- Provide values of actuals for all formals in Extra_Formals list
1803 -- Replace "call" to enumeration literal function by literal itself
1804 -- Rewrite call to predefined operator as operator
1805 -- Replace actuals to in-out parameters that are numeric conversions,
1806 -- with explicit assignment to temporaries before and after the call.
1807 -- Remove optional actuals if First_Optional_Parameter specified.
1809 -- Note that the list of actuals has been filled with default expressions
1810 -- during semantic analysis of the call. Only the extra actuals required
1811 -- for the 'Constrained attribute and for accessibility checks are added
1814 procedure Expand_Call (N : Node_Id) is
1815 Loc : constant Source_Ptr := Sloc (N);
1816 Call_Node : Node_Id := N;
1817 Extra_Actuals : List_Id := No_List;
1818 Prev : Node_Id := Empty;
1820 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1821 -- Adds one entry to the end of the actual parameter list. Used for
1822 -- default parameters and for extra actuals (for Extra_Formals). The
1823 -- argument is an N_Parameter_Association node.
1825 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1826 -- Adds an extra actual to the list of extra actuals. Expr is the
1827 -- expression for the value of the actual, EF is the entity for the
1830 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1831 -- Within an instance, a type derived from a non-tagged formal derived
1832 -- type inherits from the original parent, not from the actual. The
1833 -- current derivation mechanism has the derived type inherit from the
1834 -- actual, which is only correct outside of the instance. If the
1835 -- subprogram is inherited, we test for this particular case through a
1836 -- convoluted tree traversal before setting the proper subprogram to be
1839 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
1840 -- Determine if Subp denotes a non-dispatching call to a Deep routine
1842 function New_Value (From : Node_Id) return Node_Id;
1843 -- From is the original Expression. New_Value is equivalent to a call
1844 -- to Duplicate_Subexpr with an explicit dereference when From is an
1845 -- access parameter.
1847 --------------------------
1848 -- Add_Actual_Parameter --
1849 --------------------------
1851 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1852 Actual_Expr : constant Node_Id :=
1853 Explicit_Actual_Parameter (Insert_Param);
1856 -- Case of insertion is first named actual
1858 if No (Prev) or else
1859 Nkind (Parent (Prev)) /= N_Parameter_Association
1861 Set_Next_Named_Actual
1862 (Insert_Param, First_Named_Actual (Call_Node));
1863 Set_First_Named_Actual (Call_Node, Actual_Expr);
1866 if No (Parameter_Associations (Call_Node)) then
1867 Set_Parameter_Associations (Call_Node, New_List);
1870 Append (Insert_Param, Parameter_Associations (Call_Node));
1873 Insert_After (Prev, Insert_Param);
1876 -- Case of insertion is not first named actual
1879 Set_Next_Named_Actual
1880 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1881 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1882 Append (Insert_Param, Parameter_Associations (Call_Node));
1885 Prev := Actual_Expr;
1886 end Add_Actual_Parameter;
1888 ----------------------
1889 -- Add_Extra_Actual --
1890 ----------------------
1892 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1893 Loc : constant Source_Ptr := Sloc (Expr);
1896 if Extra_Actuals = No_List then
1897 Extra_Actuals := New_List;
1898 Set_Parent (Extra_Actuals, Call_Node);
1901 Append_To (Extra_Actuals,
1902 Make_Parameter_Association (Loc,
1903 Selector_Name => Make_Identifier (Loc, Chars (EF)),
1904 Explicit_Actual_Parameter => Expr));
1906 Analyze_And_Resolve (Expr, Etype (EF));
1908 if Nkind (Call_Node) = N_Function_Call then
1909 Set_Is_Accessibility_Actual (Parent (Expr));
1911 end Add_Extra_Actual;
1913 ---------------------------
1914 -- Inherited_From_Formal --
1915 ---------------------------
1917 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1919 Gen_Par : Entity_Id;
1920 Gen_Prim : Elist_Id;
1925 -- If the operation is inherited, it is attached to the corresponding
1926 -- type derivation. If the parent in the derivation is a generic
1927 -- actual, it is a subtype of the actual, and we have to recover the
1928 -- original derived type declaration to find the proper parent.
1930 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1931 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1932 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1933 N_Derived_Type_Definition
1934 or else not In_Instance
1941 (Type_Definition (Original_Node (Parent (S))));
1943 if Nkind (Indic) = N_Subtype_Indication then
1944 Par := Entity (Subtype_Mark (Indic));
1946 Par := Entity (Indic);
1950 if not Is_Generic_Actual_Type (Par)
1951 or else Is_Tagged_Type (Par)
1952 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1953 or else not In_Open_Scopes (Scope (Par))
1957 Gen_Par := Generic_Parent_Type (Parent (Par));
1960 -- If the actual has no generic parent type, the formal is not
1961 -- a formal derived type, so nothing to inherit.
1963 if No (Gen_Par) then
1967 -- If the generic parent type is still the generic type, this is a
1968 -- private formal, not a derived formal, and there are no operations
1969 -- inherited from the formal.
1971 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1975 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1977 Elmt := First_Elmt (Gen_Prim);
1978 while Present (Elmt) loop
1979 if Chars (Node (Elmt)) = Chars (S) then
1985 F1 := First_Formal (S);
1986 F2 := First_Formal (Node (Elmt));
1988 and then Present (F2)
1990 if Etype (F1) = Etype (F2)
1991 or else Etype (F2) = Gen_Par
1997 exit; -- not the right subprogram
2009 raise Program_Error;
2010 end Inherited_From_Formal;
2012 -------------------------
2013 -- Is_Direct_Deep_Call --
2014 -------------------------
2016 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2018 if Is_TSS (Subp, TSS_Deep_Adjust)
2019 or else Is_TSS (Subp, TSS_Deep_Finalize)
2020 or else Is_TSS (Subp, TSS_Deep_Initialize)
2027 Actual := First (Parameter_Associations (N));
2028 Formal := First_Formal (Subp);
2029 while Present (Actual)
2030 and then Present (Formal)
2032 if Nkind (Actual) = N_Identifier
2033 and then Is_Controlling_Actual (Actual)
2034 and then Etype (Actual) = Etype (Formal)
2040 Next_Formal (Formal);
2046 end Is_Direct_Deep_Call;
2052 function New_Value (From : Node_Id) return Node_Id is
2053 Res : constant Node_Id := Duplicate_Subexpr (From);
2055 if Is_Access_Type (Etype (From)) then
2057 Make_Explicit_Dereference (Sloc (From),
2066 Curr_S : constant Entity_Id := Current_Scope;
2067 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2070 Orig_Subp : Entity_Id := Empty;
2071 Param_Count : Natural := 0;
2072 Parent_Formal : Entity_Id;
2073 Parent_Subp : Entity_Id;
2077 Prev_Orig : Node_Id;
2078 -- Original node for an actual, which may have been rewritten. If the
2079 -- actual is a function call that has been transformed from a selected
2080 -- component, the original node is unanalyzed. Otherwise, it carries
2081 -- semantic information used to generate additional actuals.
2083 CW_Interface_Formals_Present : Boolean := False;
2085 -- Start of processing for Expand_Call
2088 -- Ignore if previous error
2090 if Nkind (Call_Node) in N_Has_Etype
2091 and then Etype (Call_Node) = Any_Type
2096 -- Call using access to subprogram with explicit dereference
2098 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2099 Subp := Etype (Name (Call_Node));
2100 Parent_Subp := Empty;
2102 -- Case of call to simple entry, where the Name is a selected component
2103 -- whose prefix is the task, and whose selector name is the entry name
2105 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2106 Subp := Entity (Selector_Name (Name (Call_Node)));
2107 Parent_Subp := Empty;
2109 -- Case of call to member of entry family, where Name is an indexed
2110 -- component, with the prefix being a selected component giving the
2111 -- task and entry family name, and the index being the entry index.
2113 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2114 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2115 Parent_Subp := Empty;
2120 Subp := Entity (Name (Call_Node));
2121 Parent_Subp := Alias (Subp);
2123 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2124 -- if we can tell that the first parameter cannot possibly be null.
2125 -- This improves efficiency by avoiding a run-time test.
2127 -- We do not do this if Raise_Exception_Always does not exist, which
2128 -- can happen in configurable run time profiles which provide only a
2131 if Is_RTE (Subp, RE_Raise_Exception)
2132 and then RTE_Available (RE_Raise_Exception_Always)
2135 FA : constant Node_Id :=
2136 Original_Node (First_Actual (Call_Node));
2139 -- The case we catch is where the first argument is obtained
2140 -- using the Identity attribute (which must always be
2143 if Nkind (FA) = N_Attribute_Reference
2144 and then Attribute_Name (FA) = Name_Identity
2146 Subp := RTE (RE_Raise_Exception_Always);
2147 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2152 if Ekind (Subp) = E_Entry then
2153 Parent_Subp := Empty;
2157 -- Detect the following code in System.Finalization_Masters only on
2158 -- .NET/JVM targets:
2160 -- procedure Finalize (Master : in out Finalization_Master) is
2164 -- Finalize (Curr_Ptr.all);
2166 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2167 -- cannot be named in library or user code, the compiler has to install
2168 -- a kludge and transform the call to Finalize into Deep_Finalize.
2170 if VM_Target /= No_VM
2171 and then Chars (Subp) = Name_Finalize
2172 and then Ekind (Curr_S) = E_Block
2173 and then Ekind (Scope (Curr_S)) = E_Procedure
2174 and then Chars (Scope (Curr_S)) = Name_Finalize
2175 and then Etype (First_Formal (Scope (Curr_S))) =
2176 RTE (RE_Finalization_Master)
2179 Deep_Fin : constant Entity_Id :=
2180 Find_Prim_Op (RTE (RE_Root_Controlled),
2183 -- Since Root_Controlled is a tagged type, the compiler should
2184 -- always generate Deep_Finalize for it.
2186 pragma Assert (Present (Deep_Fin));
2189 -- Deep_Finalize (Curr_Ptr.all);
2192 Make_Procedure_Call_Statement (Loc,
2194 New_Reference_To (Deep_Fin, Loc),
2195 Parameter_Associations =>
2196 New_Copy_List_Tree (Parameter_Associations (N))));
2203 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2204 -- alternative in an asynchronous select or as an entry call in
2205 -- a conditional or timed select. Check whether the procedure call
2206 -- is a renaming of an entry and rewrite it as an entry call.
2208 if Ada_Version >= Ada_2005
2209 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2211 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2212 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2214 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2215 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2219 Ren_Root : Entity_Id := Subp;
2222 -- This may be a chain of renamings, find the root
2224 if Present (Alias (Ren_Root)) then
2225 Ren_Root := Alias (Ren_Root);
2228 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2229 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2231 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2233 Make_Entry_Call_Statement (Loc,
2235 New_Copy_Tree (Name (Ren_Decl)),
2236 Parameter_Associations =>
2238 (Parameter_Associations (Call_Node))));
2246 -- First step, compute extra actuals, corresponding to any Extra_Formals
2247 -- present. Note that we do not access Extra_Formals directly, instead
2248 -- we simply note the presence of the extra formals as we process the
2249 -- regular formals collecting corresponding actuals in Extra_Actuals.
2251 -- We also generate any required range checks for actuals for in formals
2252 -- as we go through the loop, since this is a convenient place to do it.
2253 -- (Though it seems that this would be better done in Expand_Actuals???)
2255 Formal := First_Formal (Subp);
2256 Actual := First_Actual (Call_Node);
2258 while Present (Formal) loop
2260 -- Generate range check if required
2262 if Do_Range_Check (Actual)
2263 and then Ekind (Formal) = E_In_Parameter
2265 Set_Do_Range_Check (Actual, False);
2266 Generate_Range_Check
2267 (Actual, Etype (Formal), CE_Range_Check_Failed);
2270 -- Prepare to examine current entry
2273 Prev_Orig := Original_Node (Prev);
2275 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2276 -- to expand it in a further round.
2278 CW_Interface_Formals_Present :=
2279 CW_Interface_Formals_Present
2281 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2282 and then Is_Interface (Etype (Etype (Formal))))
2284 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2285 and then Is_Interface (Directly_Designated_Type
2286 (Etype (Etype (Formal)))));
2288 -- Create possible extra actual for constrained case. Usually, the
2289 -- extra actual is of the form actual'constrained, but since this
2290 -- attribute is only available for unconstrained records, TRUE is
2291 -- expanded if the type of the formal happens to be constrained (for
2292 -- instance when this procedure is inherited from an unconstrained
2293 -- record to a constrained one) or if the actual has no discriminant
2294 -- (its type is constrained). An exception to this is the case of a
2295 -- private type without discriminants. In this case we pass FALSE
2296 -- because the object has underlying discriminants with defaults.
2298 if Present (Extra_Constrained (Formal)) then
2299 if Ekind (Etype (Prev)) in Private_Kind
2300 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2303 (New_Occurrence_Of (Standard_False, Loc),
2304 Extra_Constrained (Formal));
2306 elsif Is_Constrained (Etype (Formal))
2307 or else not Has_Discriminants (Etype (Prev))
2310 (New_Occurrence_Of (Standard_True, Loc),
2311 Extra_Constrained (Formal));
2313 -- Do not produce extra actuals for Unchecked_Union parameters.
2314 -- Jump directly to the end of the loop.
2316 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2317 goto Skip_Extra_Actual_Generation;
2320 -- If the actual is a type conversion, then the constrained
2321 -- test applies to the actual, not the target type.
2327 -- Test for unchecked conversions as well, which can occur
2328 -- as out parameter actuals on calls to stream procedures.
2331 while Nkind_In (Act_Prev, N_Type_Conversion,
2332 N_Unchecked_Type_Conversion)
2334 Act_Prev := Expression (Act_Prev);
2337 -- If the expression is a conversion of a dereference, this
2338 -- is internally generated code that manipulates addresses,
2339 -- e.g. when building interface tables. No check should
2340 -- occur in this case, and the discriminated object is not
2343 if not Comes_From_Source (Actual)
2344 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2345 and then Nkind (Act_Prev) = N_Explicit_Dereference
2348 (New_Occurrence_Of (Standard_False, Loc),
2349 Extra_Constrained (Formal));
2353 (Make_Attribute_Reference (Sloc (Prev),
2355 Duplicate_Subexpr_No_Checks
2356 (Act_Prev, Name_Req => True),
2357 Attribute_Name => Name_Constrained),
2358 Extra_Constrained (Formal));
2364 -- Create possible extra actual for accessibility level
2366 if Present (Extra_Accessibility (Formal)) then
2368 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2369 -- attribute, then the original actual may be an aliased object
2370 -- occurring as the prefix in a call using "Object.Operation"
2371 -- notation. In that case we must pass the level of the object,
2372 -- so Prev_Orig is reset to Prev and the attribute will be
2373 -- processed by the code for Access attributes further below.
2375 if Prev_Orig /= Prev
2376 and then Nkind (Prev) = N_Attribute_Reference
2378 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2379 and then Is_Aliased_View (Prev_Orig)
2384 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2385 -- accessibility levels.
2387 if Ekind (Current_Scope) in Subprogram_Kind
2388 and then Is_Thunk (Current_Scope)
2391 Parm_Ent : Entity_Id;
2394 if Is_Controlling_Actual (Actual) then
2396 -- Find the corresponding actual of the thunk
2398 Parm_Ent := First_Entity (Current_Scope);
2399 for J in 2 .. Param_Count loop
2400 Next_Entity (Parm_Ent);
2403 else pragma Assert (Is_Entity_Name (Actual));
2404 Parm_Ent := Entity (Actual);
2408 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2409 Extra_Accessibility (Formal));
2412 elsif Is_Entity_Name (Prev_Orig) then
2414 -- When passing an access parameter, or a renaming of an access
2415 -- parameter, as the actual to another access parameter we need
2416 -- to pass along the actual's own access level parameter. This
2417 -- is done if we are within the scope of the formal access
2418 -- parameter (if this is an inlined body the extra formal is
2421 if (Is_Formal (Entity (Prev_Orig))
2423 (Present (Renamed_Object (Entity (Prev_Orig)))
2425 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2428 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2429 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2430 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2433 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2436 pragma Assert (Present (Parm_Ent));
2438 if Present (Extra_Accessibility (Parm_Ent)) then
2441 (Extra_Accessibility (Parm_Ent), Loc),
2442 Extra_Accessibility (Formal));
2444 -- If the actual access parameter does not have an
2445 -- associated extra formal providing its scope level,
2446 -- then treat the actual as having library-level
2451 (Make_Integer_Literal (Loc,
2452 Intval => Scope_Depth (Standard_Standard)),
2453 Extra_Accessibility (Formal));
2457 -- The actual is a normal access value, so just pass the level
2458 -- of the actual's access type.
2462 (Dynamic_Accessibility_Level (Prev_Orig),
2463 Extra_Accessibility (Formal));
2466 -- If the actual is an access discriminant, then pass the level
2467 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2469 elsif Nkind (Prev_Orig) = N_Selected_Component
2470 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2472 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2473 E_Anonymous_Access_Type
2476 (Make_Integer_Literal (Loc,
2477 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2478 Extra_Accessibility (Formal));
2483 case Nkind (Prev_Orig) is
2485 when N_Attribute_Reference =>
2486 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2488 -- For X'Access, pass on the level of the prefix X
2490 when Attribute_Access =>
2492 -- If this is an Access attribute applied to the
2493 -- the current instance object passed to a type
2494 -- initialization procedure, then use the level
2495 -- of the type itself. This is not really correct,
2496 -- as there should be an extra level parameter
2497 -- passed in with _init formals (only in the case
2498 -- where the type is immutably limited), but we
2499 -- don't have an easy way currently to create such
2500 -- an extra formal (init procs aren't ever frozen).
2501 -- For now we just use the level of the type,
2502 -- which may be too shallow, but that works better
2503 -- than passing Object_Access_Level of the type,
2504 -- which can be one level too deep in some cases.
2507 if Is_Entity_Name (Prefix (Prev_Orig))
2508 and then Is_Type (Entity (Prefix (Prev_Orig)))
2511 (Make_Integer_Literal (Loc,
2514 (Entity (Prefix (Prev_Orig)))),
2515 Extra_Accessibility (Formal));
2519 (Make_Integer_Literal (Loc,
2522 (Prefix (Prev_Orig))),
2523 Extra_Accessibility (Formal));
2526 -- Treat the unchecked attributes as library-level
2528 when Attribute_Unchecked_Access |
2529 Attribute_Unrestricted_Access =>
2531 (Make_Integer_Literal (Loc,
2532 Intval => Scope_Depth (Standard_Standard)),
2533 Extra_Accessibility (Formal));
2535 -- No other cases of attributes returning access
2536 -- values that can be passed to access parameters.
2539 raise Program_Error;
2543 -- For allocators we pass the level of the execution of the
2544 -- called subprogram, which is one greater than the current
2549 (Make_Integer_Literal (Loc,
2550 Intval => Scope_Depth (Current_Scope) + 1),
2551 Extra_Accessibility (Formal));
2553 -- For most other cases we simply pass the level of the
2554 -- actual's access type. The type is retrieved from
2555 -- Prev rather than Prev_Orig, because in some cases
2556 -- Prev_Orig denotes an original expression that has
2557 -- not been analyzed.
2561 (Dynamic_Accessibility_Level (Prev),
2562 Extra_Accessibility (Formal));
2567 -- Perform the check of 4.6(49) that prevents a null value from being
2568 -- passed as an actual to an access parameter. Note that the check
2569 -- is elided in the common cases of passing an access attribute or
2570 -- access parameter as an actual. Also, we currently don't enforce
2571 -- this check for expander-generated actuals and when -gnatdj is set.
2573 if Ada_Version >= Ada_2005 then
2575 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2576 -- the intent of 6.4.1(13) is that null-exclusion checks should
2577 -- not be done for 'out' parameters, even though it refers only
2578 -- to constraint checks, and a null_exclusion is not a constraint.
2579 -- Note that AI05-0196-1 corrects this mistake in the RM.
2581 if Is_Access_Type (Etype (Formal))
2582 and then Can_Never_Be_Null (Etype (Formal))
2583 and then Ekind (Formal) /= E_Out_Parameter
2584 and then Nkind (Prev) /= N_Raise_Constraint_Error
2585 and then (Known_Null (Prev)
2586 or else not Can_Never_Be_Null (Etype (Prev)))
2588 Install_Null_Excluding_Check (Prev);
2591 -- Ada_Version < Ada_2005
2594 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2595 or else Access_Checks_Suppressed (Subp)
2599 elsif Debug_Flag_J then
2602 elsif not Comes_From_Source (Prev) then
2605 elsif Is_Entity_Name (Prev)
2606 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2610 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2613 -- Suppress null checks when passing to access parameters of Java
2614 -- and CIL subprograms. (Should this be done for other foreign
2615 -- conventions as well ???)
2617 elsif Convention (Subp) = Convention_Java
2618 or else Convention (Subp) = Convention_CIL
2623 Install_Null_Excluding_Check (Prev);
2627 -- Perform appropriate validity checks on parameters that
2630 if Validity_Checks_On then
2631 if (Ekind (Formal) = E_In_Parameter
2632 and then Validity_Check_In_Params)
2634 (Ekind (Formal) = E_In_Out_Parameter
2635 and then Validity_Check_In_Out_Params)
2637 -- If the actual is an indexed component of a packed type (or
2638 -- is an indexed or selected component whose prefix recursively
2639 -- meets this condition), it has not been expanded yet. It will
2640 -- be copied in the validity code that follows, and has to be
2641 -- expanded appropriately, so reanalyze it.
2643 -- What we do is just to unset analyzed bits on prefixes till
2644 -- we reach something that does not have a prefix.
2651 while Nkind_In (Nod, N_Indexed_Component,
2652 N_Selected_Component)
2654 Set_Analyzed (Nod, False);
2655 Nod := Prefix (Nod);
2659 Ensure_Valid (Actual);
2663 -- For Ada 2012, if a parameter is aliased, the actual must be an
2666 if Is_Aliased (Formal) and then not Is_Aliased_View (Actual) then
2668 ("actual for aliased formal& must be aliased object",
2672 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2673 -- since this is a left side reference. We only do this for calls
2674 -- from the source program since we assume that compiler generated
2675 -- calls explicitly generate any required checks. We also need it
2676 -- only if we are doing standard validity checks, since clearly it is
2677 -- not needed if validity checks are off, and in subscript validity
2678 -- checking mode, all indexed components are checked with a call
2679 -- directly from Expand_N_Indexed_Component.
2681 if Comes_From_Source (Call_Node)
2682 and then Ekind (Formal) /= E_In_Parameter
2683 and then Validity_Checks_On
2684 and then Validity_Check_Default
2685 and then not Validity_Check_Subscripts
2687 Check_Valid_Lvalue_Subscripts (Actual);
2690 -- Mark any scalar OUT parameter that is a simple variable as no
2691 -- longer known to be valid (unless the type is always valid). This
2692 -- reflects the fact that if an OUT parameter is never set in a
2693 -- procedure, then it can become invalid on the procedure return.
2695 if Ekind (Formal) = E_Out_Parameter
2696 and then Is_Entity_Name (Actual)
2697 and then Ekind (Entity (Actual)) = E_Variable
2698 and then not Is_Known_Valid (Etype (Actual))
2700 Set_Is_Known_Valid (Entity (Actual), False);
2703 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2704 -- clear current values, since they can be clobbered. We are probably
2705 -- doing this in more places than we need to, but better safe than
2706 -- sorry when it comes to retaining bad current values!
2708 if Ekind (Formal) /= E_In_Parameter
2709 and then Is_Entity_Name (Actual)
2710 and then Present (Entity (Actual))
2713 Ent : constant Entity_Id := Entity (Actual);
2717 -- For an OUT or IN OUT parameter that is an assignable entity,
2718 -- we do not want to clobber the Last_Assignment field, since
2719 -- if it is set, it was precisely because it is indeed an OUT
2720 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
2721 -- since the subprogram could have returned in invalid value.
2723 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
2724 and then Is_Assignable (Ent)
2726 Sav := Last_Assignment (Ent);
2727 Kill_Current_Values (Ent);
2728 Set_Last_Assignment (Ent, Sav);
2729 Set_Is_Known_Valid (Ent, False);
2731 -- For all other cases, just kill the current values
2734 Kill_Current_Values (Ent);
2739 -- If the formal is class wide and the actual is an aggregate, force
2740 -- evaluation so that the back end who does not know about class-wide
2741 -- type, does not generate a temporary of the wrong size.
2743 if not Is_Class_Wide_Type (Etype (Formal)) then
2746 elsif Nkind (Actual) = N_Aggregate
2747 or else (Nkind (Actual) = N_Qualified_Expression
2748 and then Nkind (Expression (Actual)) = N_Aggregate)
2750 Force_Evaluation (Actual);
2753 -- In a remote call, if the formal is of a class-wide type, check
2754 -- that the actual meets the requirements described in E.4(18).
2756 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
2757 Insert_Action (Actual,
2758 Make_Transportable_Check (Loc,
2759 Duplicate_Subexpr_Move_Checks (Actual)));
2762 -- This label is required when skipping extra actual generation for
2763 -- Unchecked_Union parameters.
2765 <<Skip_Extra_Actual_Generation>>
2767 Param_Count := Param_Count + 1;
2768 Next_Actual (Actual);
2769 Next_Formal (Formal);
2772 -- If we are calling an Ada2012 function which needs to have the
2773 -- "accessibility level determined by the point of call" (AI05-0234)
2774 -- passed in to it, then pass it in.
2776 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
2778 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
2781 Ancestor : Node_Id := Parent (Call_Node);
2782 Level : Node_Id := Empty;
2783 Defer : Boolean := False;
2786 -- Unimplemented: if Subp returns an anonymous access type, then
2788 -- a) if the call is the operand of an explict conversion, then
2789 -- the target type of the conversion (a named access type)
2790 -- determines the accessibility level pass in;
2792 -- b) if the call defines an access discriminant of an object
2793 -- (e.g., the discriminant of an object being created by an
2794 -- allocator, or the discriminant of a function result),
2795 -- then the accessibility level to pass in is that of the
2796 -- discriminated object being initialized).
2800 while Nkind (Ancestor) = N_Qualified_Expression
2802 Ancestor := Parent (Ancestor);
2805 case Nkind (Ancestor) is
2808 -- At this point, we'd like to assign
2810 -- Level := Dynamic_Accessibility_Level (Ancestor);
2812 -- but Etype of Ancestor may not have been set yet,
2813 -- so that doesn't work.
2815 -- Handle this later in Expand_Allocator_Expression.
2819 when N_Object_Declaration | N_Object_Renaming_Declaration =>
2821 Def_Id : constant Entity_Id :=
2822 Defining_Identifier (Ancestor);
2825 if Is_Return_Object (Def_Id) then
2826 if Present (Extra_Accessibility_Of_Result
2827 (Return_Applies_To (Scope (Def_Id))))
2829 -- Pass along value that was passed in if the
2830 -- routine we are returning from also has an
2831 -- Accessibility_Of_Result formal.
2835 (Extra_Accessibility_Of_Result
2836 (Return_Applies_To (Scope (Def_Id))), Loc);
2840 Make_Integer_Literal (Loc,
2841 Intval => Object_Access_Level (Def_Id));
2845 when N_Simple_Return_Statement =>
2846 if Present (Extra_Accessibility_Of_Result
2848 (Return_Statement_Entity (Ancestor))))
2850 -- Pass along value that was passed in if the routine
2851 -- we are returning from also has an
2852 -- Accessibility_Of_Result formal.
2856 (Extra_Accessibility_Of_Result
2858 (Return_Statement_Entity (Ancestor))), Loc);
2866 if not Present (Level) then
2868 -- The "innermost master that evaluates the function call".
2870 -- ??? - Should we use Integer'Last here instead in order
2871 -- to deal with (some of) the problems associated with
2872 -- calls to subps whose enclosing scope is unknown (e.g.,
2873 -- Anon_Access_To_Subp_Param.all)?
2875 Level := Make_Integer_Literal (Loc,
2876 Scope_Depth (Current_Scope) + 1);
2881 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
2886 -- If we are expanding a rhs of an assignment we need to check if tag
2887 -- propagation is needed. You might expect this processing to be in
2888 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
2889 -- assignment might be transformed to a declaration for an unconstrained
2890 -- value if the expression is classwide.
2892 if Nkind (Call_Node) = N_Function_Call
2893 and then Is_Tag_Indeterminate (Call_Node)
2894 and then Is_Entity_Name (Name (Call_Node))
2897 Ass : Node_Id := Empty;
2900 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
2901 Ass := Parent (Call_Node);
2903 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
2904 and then Nkind (Parent (Parent (Call_Node))) =
2905 N_Assignment_Statement
2907 Ass := Parent (Parent (Call_Node));
2909 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
2910 and then Nkind (Parent (Parent (Call_Node))) =
2911 N_Assignment_Statement
2913 Ass := Parent (Parent (Call_Node));
2917 and then Is_Class_Wide_Type (Etype (Name (Ass)))
2919 if Is_Access_Type (Etype (Call_Node)) then
2920 if Designated_Type (Etype (Call_Node)) /=
2921 Root_Type (Etype (Name (Ass)))
2924 ("tag-indeterminate expression "
2925 & " must have designated type& (RM 5.2 (6))",
2926 Call_Node, Root_Type (Etype (Name (Ass))));
2928 Propagate_Tag (Name (Ass), Call_Node);
2931 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
2933 ("tag-indeterminate expression must have type&"
2935 Call_Node, Root_Type (Etype (Name (Ass))));
2938 Propagate_Tag (Name (Ass), Call_Node);
2941 -- The call will be rewritten as a dispatching call, and
2942 -- expanded as such.
2949 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
2950 -- it to point to the correct secondary virtual table
2952 if Nkind_In (Call_Node, N_Function_Call, N_Procedure_Call_Statement)
2953 and then CW_Interface_Formals_Present
2955 Expand_Interface_Actuals (Call_Node);
2958 -- Deals with Dispatch_Call if we still have a call, before expanding
2959 -- extra actuals since this will be done on the re-analysis of the
2960 -- dispatching call. Note that we do not try to shorten the actual list
2961 -- for a dispatching call, it would not make sense to do so. Expansion
2962 -- of dispatching calls is suppressed when VM_Target, because the VM
2963 -- back-ends directly handle the generation of dispatching calls and
2964 -- would have to undo any expansion to an indirect call.
2966 if Nkind_In (Call_Node, N_Function_Call, N_Procedure_Call_Statement)
2967 and then Present (Controlling_Argument (Call_Node))
2970 Call_Typ : constant Entity_Id := Etype (Call_Node);
2971 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
2972 Eq_Prim_Op : Entity_Id := Empty;
2975 Prev_Call : Node_Id;
2978 if not Is_Limited_Type (Typ) then
2979 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
2982 if Tagged_Type_Expansion then
2983 Expand_Dispatching_Call (Call_Node);
2985 -- The following return is worrisome. Is it really OK to skip
2986 -- all remaining processing in this procedure ???
2993 Apply_Tag_Checks (Call_Node);
2995 -- If this is a dispatching "=", we must first compare the
2996 -- tags so we generate: x.tag = y.tag and then x = y
2998 if Subp = Eq_Prim_Op then
3000 -- Mark the node as analyzed to avoid reanalizing this
3001 -- dispatching call (which would cause a never-ending loop)
3003 Prev_Call := Relocate_Node (Call_Node);
3004 Set_Analyzed (Prev_Call);
3006 Param := First_Actual (Call_Node);
3012 Make_Selected_Component (Loc,
3013 Prefix => New_Value (Param),
3015 New_Reference_To (First_Tag_Component (Typ),
3019 Make_Selected_Component (Loc,
3021 Unchecked_Convert_To (Typ,
3022 New_Value (Next_Actual (Param))),
3025 (First_Tag_Component (Typ), Loc))),
3026 Right_Opnd => Prev_Call);
3028 Rewrite (Call_Node, New_Call);
3031 (Call_Node, Call_Typ, Suppress => All_Checks);
3034 -- Expansion of a dispatching call results in an indirect call,
3035 -- which in turn causes current values to be killed (see
3036 -- Resolve_Call), so on VM targets we do the call here to
3037 -- ensure consistent warnings between VM and non-VM targets.
3039 Kill_Current_Values;
3042 -- If this is a dispatching "=" then we must update the reference
3043 -- to the call node because we generated:
3044 -- x.tag = y.tag and then x = y
3046 if Subp = Eq_Prim_Op then
3047 Call_Node := Right_Opnd (Call_Node);
3052 -- Similarly, expand calls to RCI subprograms on which pragma
3053 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3054 -- later. Do this only when the call comes from source since we
3055 -- do not want such a rewriting to occur in expanded code.
3057 if Is_All_Remote_Call (Call_Node) then
3058 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3060 -- Similarly, do not add extra actuals for an entry call whose entity
3061 -- is a protected procedure, or for an internal protected subprogram
3062 -- call, because it will be rewritten as a protected subprogram call
3063 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3065 elsif Is_Protected_Type (Scope (Subp))
3066 and then (Ekind (Subp) = E_Procedure
3067 or else Ekind (Subp) = E_Function)
3071 -- During that loop we gathered the extra actuals (the ones that
3072 -- correspond to Extra_Formals), so now they can be appended.
3075 while Is_Non_Empty_List (Extra_Actuals) loop
3076 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3080 -- At this point we have all the actuals, so this is the point at which
3081 -- the various expansion activities for actuals is carried out.
3083 Expand_Actuals (Call_Node, Subp);
3085 -- If the subprogram is a renaming, or if it is inherited, replace it in
3086 -- the call with the name of the actual subprogram being called. If this
3087 -- is a dispatching call, the run-time decides what to call. The Alias
3088 -- attribute does not apply to entries.
3090 if Nkind (Call_Node) /= N_Entry_Call_Statement
3091 and then No (Controlling_Argument (Call_Node))
3092 and then Present (Parent_Subp)
3093 and then not Is_Direct_Deep_Call (Subp)
3095 if Present (Inherited_From_Formal (Subp)) then
3096 Parent_Subp := Inherited_From_Formal (Subp);
3098 Parent_Subp := Ultimate_Alias (Parent_Subp);
3101 -- The below setting of Entity is suspect, see F109-018 discussion???
3103 Set_Entity (Name (Call_Node), Parent_Subp);
3105 if Is_Abstract_Subprogram (Parent_Subp)
3106 and then not In_Instance
3109 ("cannot call abstract subprogram &!",
3110 Name (Call_Node), Parent_Subp);
3113 -- Inspect all formals of derived subprogram Subp. Compare parameter
3114 -- types with the parent subprogram and check whether an actual may
3115 -- need a type conversion to the corresponding formal of the parent
3118 -- Not clear whether intrinsic subprograms need such conversions. ???
3120 if not Is_Intrinsic_Subprogram (Parent_Subp)
3121 or else Is_Generic_Instance (Parent_Subp)
3124 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3125 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3126 -- and resolve the newly generated construct.
3132 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3134 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3141 Actual_Typ : Entity_Id;
3142 Formal_Typ : Entity_Id;
3143 Parent_Typ : Entity_Id;
3146 Actual := First_Actual (Call_Node);
3147 Formal := First_Formal (Subp);
3148 Parent_Formal := First_Formal (Parent_Subp);
3149 while Present (Formal) loop
3150 Actual_Typ := Etype (Actual);
3151 Formal_Typ := Etype (Formal);
3152 Parent_Typ := Etype (Parent_Formal);
3154 -- For an IN parameter of a scalar type, the parent formal
3155 -- type and derived formal type differ or the parent formal
3156 -- type and actual type do not match statically.
3158 if Is_Scalar_Type (Formal_Typ)
3159 and then Ekind (Formal) = E_In_Parameter
3160 and then Formal_Typ /= Parent_Typ
3162 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3163 and then not Raises_Constraint_Error (Actual)
3165 Convert (Actual, Parent_Typ);
3166 Enable_Range_Check (Actual);
3168 -- If the actual has been marked as requiring a range
3169 -- check, then generate it here.
3171 if Do_Range_Check (Actual) then
3172 Set_Do_Range_Check (Actual, False);
3173 Generate_Range_Check
3174 (Actual, Etype (Formal), CE_Range_Check_Failed);
3177 -- For access types, the parent formal type and actual type
3180 elsif Is_Access_Type (Formal_Typ)
3181 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3183 if Ekind (Formal) /= E_In_Parameter then
3184 Convert (Actual, Parent_Typ);
3186 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3187 and then Designated_Type (Parent_Typ) /=
3188 Designated_Type (Actual_Typ)
3189 and then not Is_Controlling_Formal (Formal)
3191 -- This unchecked conversion is not necessary unless
3192 -- inlining is enabled, because in that case the type
3193 -- mismatch may become visible in the body about to be
3197 Unchecked_Convert_To (Parent_Typ,
3198 Relocate_Node (Actual)));
3200 Resolve (Actual, Parent_Typ);
3203 -- For array and record types, the parent formal type and
3204 -- derived formal type have different sizes or pragma Pack
3207 elsif ((Is_Array_Type (Formal_Typ)
3208 and then Is_Array_Type (Parent_Typ))
3210 (Is_Record_Type (Formal_Typ)
3211 and then Is_Record_Type (Parent_Typ)))
3213 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3214 or else Has_Pragma_Pack (Formal_Typ) /=
3215 Has_Pragma_Pack (Parent_Typ))
3217 Convert (Actual, Parent_Typ);
3220 Next_Actual (Actual);
3221 Next_Formal (Formal);
3222 Next_Formal (Parent_Formal);
3228 Subp := Parent_Subp;
3231 -- Check for violation of No_Abort_Statements
3233 if Restriction_Check_Required (No_Abort_Statements)
3234 and then Is_RTE (Subp, RE_Abort_Task)
3236 Check_Restriction (No_Abort_Statements, Call_Node);
3238 -- Check for violation of No_Dynamic_Attachment
3240 elsif Restriction_Check_Required (No_Dynamic_Attachment)
3241 and then RTU_Loaded (Ada_Interrupts)
3242 and then (Is_RTE (Subp, RE_Is_Reserved) or else
3243 Is_RTE (Subp, RE_Is_Attached) or else
3244 Is_RTE (Subp, RE_Current_Handler) or else
3245 Is_RTE (Subp, RE_Attach_Handler) or else
3246 Is_RTE (Subp, RE_Exchange_Handler) or else
3247 Is_RTE (Subp, RE_Detach_Handler) or else
3248 Is_RTE (Subp, RE_Reference))
3250 Check_Restriction (No_Dynamic_Attachment, Call_Node);
3253 -- Deal with case where call is an explicit dereference
3255 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3257 -- Handle case of access to protected subprogram type
3259 if Is_Access_Protected_Subprogram_Type
3260 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3262 -- If this is a call through an access to protected operation, the
3263 -- prefix has the form (object'address, operation'access). Rewrite
3264 -- as a for other protected calls: the object is the 1st parameter
3265 -- of the list of actuals.
3272 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3274 T : constant Entity_Id :=
3275 Equivalent_Type (Base_Type (Etype (Ptr)));
3277 D_T : constant Entity_Id :=
3278 Designated_Type (Base_Type (Etype (Ptr)));
3282 Make_Selected_Component (Loc,
3283 Prefix => Unchecked_Convert_To (T, Ptr),
3285 New_Occurrence_Of (First_Entity (T), Loc));
3288 Make_Selected_Component (Loc,
3289 Prefix => Unchecked_Convert_To (T, Ptr),
3291 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3294 Make_Explicit_Dereference (Loc,
3297 if Present (Parameter_Associations (Call_Node)) then
3298 Parm := Parameter_Associations (Call_Node);
3303 Prepend (Obj, Parm);
3305 if Etype (D_T) = Standard_Void_Type then
3307 Make_Procedure_Call_Statement (Loc,
3309 Parameter_Associations => Parm);
3312 Make_Function_Call (Loc,
3314 Parameter_Associations => Parm);
3317 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3318 Set_Etype (Call, Etype (D_T));
3320 -- We do not re-analyze the call to avoid infinite recursion.
3321 -- We analyze separately the prefix and the object, and set
3322 -- the checks on the prefix that would otherwise be emitted
3323 -- when resolving a call.
3325 Rewrite (Call_Node, Call);
3327 Apply_Access_Check (Nam);
3334 -- If this is a call to an intrinsic subprogram, then perform the
3335 -- appropriate expansion to the corresponding tree node and we
3336 -- are all done (since after that the call is gone!)
3338 -- In the case where the intrinsic is to be processed by the back end,
3339 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3340 -- since the idea in this case is to pass the call unchanged. If the
3341 -- intrinsic is an inherited unchecked conversion, and the derived type
3342 -- is the target type of the conversion, we must retain it as the return
3343 -- type of the expression. Otherwise the expansion below, which uses the
3344 -- parent operation, will yield the wrong type.
3346 if Is_Intrinsic_Subprogram (Subp) then
3347 Expand_Intrinsic_Call (Call_Node, Subp);
3349 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3350 and then Parent_Subp /= Orig_Subp
3351 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3353 Set_Etype (Call_Node, Etype (Orig_Subp));
3359 if Ekind_In (Subp, E_Function, E_Procedure) then
3361 -- We perform two simple optimization on calls:
3363 -- a) replace calls to null procedures unconditionally;
3365 -- b) for To_Address, just do an unchecked conversion. Not only is
3366 -- this efficient, but it also avoids order of elaboration problems
3367 -- when address clauses are inlined (address expression elaborated
3368 -- at the wrong point).
3370 -- We perform these optimization regardless of whether we are in the
3371 -- main unit or in a unit in the context of the main unit, to ensure
3372 -- that tree generated is the same in both cases, for Inspector use.
3374 if Is_RTE (Subp, RE_To_Address) then
3376 Unchecked_Convert_To
3377 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3380 elsif Is_Null_Procedure (Subp) then
3381 Rewrite (Call_Node, Make_Null_Statement (Loc));
3385 if Is_Inlined (Subp) then
3387 Inlined_Subprogram : declare
3389 Must_Inline : Boolean := False;
3390 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3391 Scop : constant Entity_Id := Scope (Subp);
3393 function In_Unfrozen_Instance return Boolean;
3394 -- If the subprogram comes from an instance in the same unit,
3395 -- and the instance is not yet frozen, inlining might trigger
3396 -- order-of-elaboration problems in gigi.
3398 --------------------------
3399 -- In_Unfrozen_Instance --
3400 --------------------------
3402 function In_Unfrozen_Instance return Boolean is
3408 and then S /= Standard_Standard
3410 if Is_Generic_Instance (S)
3411 and then Present (Freeze_Node (S))
3412 and then not Analyzed (Freeze_Node (S))
3421 end In_Unfrozen_Instance;
3423 -- Start of processing for Inlined_Subprogram
3426 -- Verify that the body to inline has already been seen, and
3427 -- that if the body is in the current unit the inlining does
3428 -- not occur earlier. This avoids order-of-elaboration problems
3431 -- This should be documented in sinfo/einfo ???
3434 or else Nkind (Spec) /= N_Subprogram_Declaration
3435 or else No (Body_To_Inline (Spec))
3437 Must_Inline := False;
3439 -- If this an inherited function that returns a private type,
3440 -- do not inline if the full view is an unconstrained array,
3441 -- because such calls cannot be inlined.
3443 elsif Present (Orig_Subp)
3444 and then Is_Array_Type (Etype (Orig_Subp))
3445 and then not Is_Constrained (Etype (Orig_Subp))
3447 Must_Inline := False;
3449 elsif In_Unfrozen_Instance then
3450 Must_Inline := False;
3453 Bod := Body_To_Inline (Spec);
3455 if (In_Extended_Main_Code_Unit (Call_Node)
3456 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3457 or else Has_Pragma_Inline_Always (Subp))
3458 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3460 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3462 Must_Inline := True;
3464 -- If we are compiling a package body that is not the main
3465 -- unit, it must be for inlining/instantiation purposes,
3466 -- in which case we inline the call to insure that the same
3467 -- temporaries are generated when compiling the body by
3468 -- itself. Otherwise link errors can occur.
3470 -- If the function being called is itself in the main unit,
3471 -- we cannot inline, because there is a risk of double
3472 -- elaboration and/or circularity: the inlining can make
3473 -- visible a private entity in the body of the main unit,
3474 -- that gigi will see before its sees its proper definition.
3476 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3477 and then In_Package_Body
3479 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3484 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3487 -- Let the back end handle it
3489 Add_Inlined_Body (Subp);
3491 if Front_End_Inlining
3492 and then Nkind (Spec) = N_Subprogram_Declaration
3493 and then (In_Extended_Main_Code_Unit (Call_Node))
3494 and then No (Body_To_Inline (Spec))
3495 and then not Has_Completion (Subp)
3496 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3499 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
3502 end Inlined_Subprogram;
3506 -- Check for protected subprogram. This is either an intra-object call,
3507 -- or a protected function call. Protected procedure calls are rewritten
3508 -- as entry calls and handled accordingly.
3510 -- In Ada 2005, this may be an indirect call to an access parameter that
3511 -- is an access_to_subprogram. In that case the anonymous type has a
3512 -- scope that is a protected operation, but the call is a regular one.
3513 -- In either case do not expand call if subprogram is eliminated.
3515 Scop := Scope (Subp);
3517 if Nkind (Call_Node) /= N_Entry_Call_Statement
3518 and then Is_Protected_Type (Scop)
3519 and then Ekind (Subp) /= E_Subprogram_Type
3520 and then not Is_Eliminated (Subp)
3522 -- If the call is an internal one, it is rewritten as a call to the
3523 -- corresponding unprotected subprogram.
3525 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3528 -- Functions returning controlled objects need special attention. If
3529 -- the return type is limited, then the context is initialization and
3530 -- different processing applies. If the call is to a protected function,
3531 -- the expansion above will call Expand_Call recursively. Otherwise the
3532 -- function call is transformed into a temporary which obtains the
3533 -- result from the secondary stack.
3535 if Needs_Finalization (Etype (Subp)) then
3536 if not Is_Immutably_Limited_Type (Etype (Subp))
3538 (No (First_Formal (Subp))
3540 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3542 Expand_Ctrl_Function_Call (Call_Node);
3544 -- Build-in-place function calls which appear in anonymous contexts
3545 -- need a transient scope to ensure the proper finalization of the
3546 -- intermediate result after its use.
3548 elsif Is_Build_In_Place_Function_Call (Call_Node)
3549 and then Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3551 N_Indexed_Component,
3552 N_Object_Renaming_Declaration,
3553 N_Procedure_Call_Statement,
3554 N_Selected_Component,
3557 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3561 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3562 -- if there are optional parameters at the trailing end.
3563 -- Note: we never delete procedures for call via a pointer.
3565 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3566 and then Present (First_Optional_Parameter (Subp))
3569 Last_Keep_Arg : Node_Id;
3572 -- Last_Keep_Arg will hold the last actual that should be kept.
3573 -- If it remains empty at the end, it means that all parameters
3576 Last_Keep_Arg := Empty;
3578 -- Find first optional parameter, must be present since we checked
3579 -- the validity of the parameter before setting it.
3581 Formal := First_Formal (Subp);
3582 Actual := First_Actual (Call_Node);
3583 while Formal /= First_Optional_Parameter (Subp) loop
3584 Last_Keep_Arg := Actual;
3585 Next_Formal (Formal);
3586 Next_Actual (Actual);
3589 -- We have Formal and Actual pointing to the first potentially
3590 -- droppable argument. We can drop all the trailing arguments
3591 -- whose actual matches the default. Note that we know that all
3592 -- remaining formals have defaults, because we checked that this
3593 -- requirement was met before setting First_Optional_Parameter.
3595 -- We use Fully_Conformant_Expressions to check for identity
3596 -- between formals and actuals, which may miss some cases, but
3597 -- on the other hand, this is only an optimization (if we fail
3598 -- to truncate a parameter it does not affect functionality).
3599 -- So if the default is 3 and the actual is 1+2, we consider
3600 -- them unequal, which hardly seems worrisome.
3602 while Present (Formal) loop
3603 if not Fully_Conformant_Expressions
3604 (Actual, Default_Value (Formal))
3606 Last_Keep_Arg := Actual;
3609 Next_Formal (Formal);
3610 Next_Actual (Actual);
3613 -- If no arguments, delete entire list, this is the easy case
3615 if No (Last_Keep_Arg) then
3616 Set_Parameter_Associations (Call_Node, No_List);
3617 Set_First_Named_Actual (Call_Node, Empty);
3619 -- Case where at the last retained argument is positional. This
3620 -- is also an easy case, since the retained arguments are already
3621 -- in the right form, and we don't need to worry about the order
3622 -- of arguments that get eliminated.
3624 elsif Is_List_Member (Last_Keep_Arg) then
3625 while Present (Next (Last_Keep_Arg)) loop
3626 Discard_Node (Remove_Next (Last_Keep_Arg));
3629 Set_First_Named_Actual (Call_Node, Empty);
3631 -- This is the annoying case where the last retained argument
3632 -- is a named parameter. Since the original arguments are not
3633 -- in declaration order, we may have to delete some fairly
3634 -- random collection of arguments.
3642 -- First step, remove all the named parameters from the
3643 -- list (they are still chained using First_Named_Actual
3644 -- and Next_Named_Actual, so we have not lost them!)
3646 Temp := First (Parameter_Associations (Call_Node));
3648 -- Case of all parameters named, remove them all
3650 if Nkind (Temp) = N_Parameter_Association then
3651 -- Suppress warnings to avoid warning on possible
3652 -- infinite loop (because Call_Node is not modified).
3654 pragma Warnings (Off);
3655 while Is_Non_Empty_List
3656 (Parameter_Associations (Call_Node))
3659 Remove_Head (Parameter_Associations (Call_Node));
3661 pragma Warnings (On);
3663 -- Case of mixed positional/named, remove named parameters
3666 while Nkind (Next (Temp)) /= N_Parameter_Association loop
3670 while Present (Next (Temp)) loop
3671 Remove (Next (Temp));
3675 -- Now we loop through the named parameters, till we get
3676 -- to the last one to be retained, adding them to the list.
3677 -- Note that the Next_Named_Actual list does not need to be
3678 -- touched since we are only reordering them on the actual
3679 -- parameter association list.
3681 Passoc := Parent (First_Named_Actual (Call_Node));
3683 Temp := Relocate_Node (Passoc);
3685 (Parameter_Associations (Call_Node), Temp);
3687 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
3688 Passoc := Parent (Next_Named_Actual (Passoc));
3691 Set_Next_Named_Actual (Temp, Empty);
3694 Temp := Next_Named_Actual (Passoc);
3695 exit when No (Temp);
3696 Set_Next_Named_Actual
3697 (Passoc, Next_Named_Actual (Parent (Temp)));
3706 -------------------------------
3707 -- Expand_Ctrl_Function_Call --
3708 -------------------------------
3710 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
3712 -- Optimization, if the returned value (which is on the sec-stack) is
3713 -- returned again, no need to copy/readjust/finalize, we can just pass
3714 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
3715 -- attachment is needed
3717 if Nkind (Parent (N)) = N_Simple_Return_Statement then
3721 -- Resolution is now finished, make sure we don't start analysis again
3722 -- because of the duplication.
3726 -- A function which returns a controlled object uses the secondary
3727 -- stack. Rewrite the call into a temporary which obtains the result of
3728 -- the function using 'reference.
3730 Remove_Side_Effects (N);
3731 end Expand_Ctrl_Function_Call;
3733 --------------------------
3734 -- Expand_Inlined_Call --
3735 --------------------------
3737 procedure Expand_Inlined_Call
3740 Orig_Subp : Entity_Id)
3742 Loc : constant Source_Ptr := Sloc (N);
3743 Is_Predef : constant Boolean :=
3744 Is_Predefined_File_Name
3745 (Unit_File_Name (Get_Source_Unit (Subp)));
3746 Orig_Bod : constant Node_Id :=
3747 Body_To_Inline (Unit_Declaration_Node (Subp));
3752 Decls : constant List_Id := New_List;
3753 Exit_Lab : Entity_Id := Empty;
3760 Ret_Type : Entity_Id;
3763 -- The target of the call. If context is an assignment statement then
3764 -- this is the left-hand side of the assignment. else it is a temporary
3765 -- to which the return value is assigned prior to rewriting the call.
3768 -- A separate target used when the return type is unconstrained
3771 Temp_Typ : Entity_Id;
3773 Return_Object : Entity_Id := Empty;
3774 -- Entity in declaration in an extended_return_statement
3776 Is_Unc : constant Boolean :=
3777 Is_Array_Type (Etype (Subp))
3778 and then not Is_Constrained (Etype (Subp));
3779 -- If the type returned by the function is unconstrained and the call
3780 -- can be inlined, special processing is required.
3782 procedure Make_Exit_Label;
3783 -- Build declaration for exit label to be used in Return statements,
3784 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3785 -- declaration). Does nothing if Exit_Lab already set.
3787 function Process_Formals (N : Node_Id) return Traverse_Result;
3788 -- Replace occurrence of a formal with the corresponding actual, or the
3789 -- thunk generated for it.
3791 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
3792 -- If the call being expanded is that of an internal subprogram, set the
3793 -- sloc of the generated block to that of the call itself, so that the
3794 -- expansion is skipped by the "next" command in gdb.
3795 -- Same processing for a subprogram in a predefined file, e.g.
3796 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
3797 -- simplify our own development.
3799 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3800 -- If the function body is a single expression, replace call with
3801 -- expression, else insert block appropriately.
3803 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3804 -- If procedure body has no local variables, inline body without
3805 -- creating block, otherwise rewrite call with block.
3807 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
3808 -- Determine whether a formal parameter is used only once in Orig_Bod
3810 ---------------------
3811 -- Make_Exit_Label --
3812 ---------------------
3814 procedure Make_Exit_Label is
3815 Lab_Ent : Entity_Id;
3817 if No (Exit_Lab) then
3818 Lab_Ent := Make_Temporary (Loc, 'L');
3819 Lab_Id := New_Reference_To (Lab_Ent, Loc);
3820 Exit_Lab := Make_Label (Loc, Lab_Id);
3822 Make_Implicit_Label_Declaration (Loc,
3823 Defining_Identifier => Lab_Ent,
3824 Label_Construct => Exit_Lab);
3826 end Make_Exit_Label;
3828 ---------------------
3829 -- Process_Formals --
3830 ---------------------
3832 function Process_Formals (N : Node_Id) return Traverse_Result is
3838 if Is_Entity_Name (N)
3839 and then Present (Entity (N))
3844 and then Scope (E) = Subp
3846 A := Renamed_Object (E);
3848 -- Rewrite the occurrence of the formal into an occurrence of
3849 -- the actual. Also establish visibility on the proper view of
3850 -- the actual's subtype for the body's context (if the actual's
3851 -- subtype is private at the call point but its full view is
3852 -- visible to the body, then the inlined tree here must be
3853 -- analyzed with the full view).
3855 if Is_Entity_Name (A) then
3856 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
3857 Check_Private_View (N);
3859 elsif Nkind (A) = N_Defining_Identifier then
3860 Rewrite (N, New_Occurrence_Of (A, Loc));
3861 Check_Private_View (N);
3866 Rewrite (N, New_Copy (A));
3872 elsif Is_Entity_Name (N)
3873 and then Present (Return_Object)
3874 and then Chars (N) = Chars (Return_Object)
3876 -- Occurrence within an extended return statement. The return
3877 -- object is local to the body been inlined, and thus the generic
3878 -- copy is not analyzed yet, so we match by name, and replace it
3879 -- with target of call.
3881 if Nkind (Targ) = N_Defining_Identifier then
3882 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3884 Rewrite (N, New_Copy_Tree (Targ));
3889 elsif Nkind (N) = N_Simple_Return_Statement then
3890 if No (Expression (N)) then
3893 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3896 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3897 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3899 -- Function body is a single expression. No need for
3905 Num_Ret := Num_Ret + 1;
3909 -- Because of the presence of private types, the views of the
3910 -- expression and the context may be different, so place an
3911 -- unchecked conversion to the context type to avoid spurious
3912 -- errors, e.g. when the expression is a numeric literal and
3913 -- the context is private. If the expression is an aggregate,
3914 -- use a qualified expression, because an aggregate is not a
3915 -- legal argument of a conversion.
3917 if Nkind_In (Expression (N), N_Aggregate, N_Null) then
3919 Make_Qualified_Expression (Sloc (N),
3920 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3921 Expression => Relocate_Node (Expression (N)));
3924 Unchecked_Convert_To
3925 (Ret_Type, Relocate_Node (Expression (N)));
3928 if Nkind (Targ) = N_Defining_Identifier then
3930 Make_Assignment_Statement (Loc,
3931 Name => New_Occurrence_Of (Targ, Loc),
3932 Expression => Ret));
3935 Make_Assignment_Statement (Loc,
3936 Name => New_Copy (Targ),
3937 Expression => Ret));
3940 Set_Assignment_OK (Name (N));
3942 if Present (Exit_Lab) then
3944 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3950 -- An extended return becomes a block whose first statement is the
3951 -- assignment of the initial expression of the return object to the
3952 -- target of the call itself.
3954 elsif Nkind (N) = N_Extended_Return_Statement then
3956 Return_Decl : constant Entity_Id :=
3957 First (Return_Object_Declarations (N));
3961 Return_Object := Defining_Identifier (Return_Decl);
3963 if Present (Expression (Return_Decl)) then
3964 if Nkind (Targ) = N_Defining_Identifier then
3966 Make_Assignment_Statement (Loc,
3967 Name => New_Occurrence_Of (Targ, Loc),
3968 Expression => Expression (Return_Decl));
3971 Make_Assignment_Statement (Loc,
3972 Name => New_Copy (Targ),
3973 Expression => Expression (Return_Decl));
3976 Set_Assignment_OK (Name (Assign));
3978 Statements (Handled_Statement_Sequence (N)));
3982 Make_Block_Statement (Loc,
3983 Handled_Statement_Sequence =>
3984 Handled_Statement_Sequence (N)));
3989 -- Remove pragma Unreferenced since it may refer to formals that
3990 -- are not visible in the inlined body, and in any case we will
3991 -- not be posting warnings on the inlined body so it is unneeded.
3993 elsif Nkind (N) = N_Pragma
3994 and then Pragma_Name (N) = Name_Unreferenced
3996 Rewrite (N, Make_Null_Statement (Sloc (N)));
4002 end Process_Formals;
4004 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
4010 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
4012 if not Debug_Generated_Code then
4013 Set_Sloc (Nod, Sloc (N));
4014 Set_Comes_From_Source (Nod, False);
4020 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
4022 ---------------------------
4023 -- Rewrite_Function_Call --
4024 ---------------------------
4026 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
4027 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
4028 Fst : constant Node_Id := First (Statements (HSS));
4031 -- Optimize simple case: function body is a single return statement,
4032 -- which has been expanded into an assignment.
4034 if Is_Empty_List (Declarations (Blk))
4035 and then Nkind (Fst) = N_Assignment_Statement
4036 and then No (Next (Fst))
4038 -- The function call may have been rewritten as the temporary
4039 -- that holds the result of the call, in which case remove the
4040 -- now useless declaration.
4042 if Nkind (N) = N_Identifier
4043 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4045 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
4048 Rewrite (N, Expression (Fst));
4050 elsif Nkind (N) = N_Identifier
4051 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4053 -- The block assigns the result of the call to the temporary
4055 Insert_After (Parent (Entity (N)), Blk);
4057 -- If the context is an assignment, and the left-hand side is free of
4058 -- side-effects, the replacement is also safe.
4059 -- Can this be generalized further???
4061 elsif Nkind (Parent (N)) = N_Assignment_Statement
4063 (Is_Entity_Name (Name (Parent (N)))
4065 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
4066 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
4069 (Nkind (Name (Parent (N))) = N_Selected_Component
4070 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
4072 -- Replace assignment with the block
4075 Original_Assignment : constant Node_Id := Parent (N);
4078 -- Preserve the original assignment node to keep the complete
4079 -- assignment subtree consistent enough for Analyze_Assignment
4080 -- to proceed (specifically, the original Lhs node must still
4081 -- have an assignment statement as its parent).
4083 -- We cannot rely on Original_Node to go back from the block
4084 -- node to the assignment node, because the assignment might
4085 -- already be a rewrite substitution.
4087 Discard_Node (Relocate_Node (Original_Assignment));
4088 Rewrite (Original_Assignment, Blk);
4091 elsif Nkind (Parent (N)) = N_Object_Declaration then
4092 Set_Expression (Parent (N), Empty);
4093 Insert_After (Parent (N), Blk);
4096 Insert_Before (Parent (N), Blk);
4098 end Rewrite_Function_Call;
4100 ----------------------------
4101 -- Rewrite_Procedure_Call --
4102 ----------------------------
4104 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
4105 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
4108 -- If there is a transient scope for N, this will be the scope of the
4109 -- actions for N, and the statements in Blk need to be within this
4110 -- scope. For example, they need to have visibility on the constant
4111 -- declarations created for the formals.
4113 -- If N needs no transient scope, and if there are no declarations in
4114 -- the inlined body, we can do a little optimization and insert the
4115 -- statements for the body directly after N, and rewrite N to a
4116 -- null statement, instead of rewriting N into a full-blown block
4119 if not Scope_Is_Transient
4120 and then Is_Empty_List (Declarations (Blk))
4122 Insert_List_After (N, Statements (HSS));
4123 Rewrite (N, Make_Null_Statement (Loc));
4127 end Rewrite_Procedure_Call;
4129 -------------------------
4130 -- Formal_Is_Used_Once --
4131 -------------------------
4133 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
4134 Use_Counter : Int := 0;
4136 function Count_Uses (N : Node_Id) return Traverse_Result;
4137 -- Traverse the tree and count the uses of the formal parameter.
4138 -- In this case, for optimization purposes, we do not need to
4139 -- continue the traversal once more than one use is encountered.
4145 function Count_Uses (N : Node_Id) return Traverse_Result is
4147 -- The original node is an identifier
4149 if Nkind (N) = N_Identifier
4150 and then Present (Entity (N))
4152 -- Original node's entity points to the one in the copied body
4154 and then Nkind (Entity (N)) = N_Identifier
4155 and then Present (Entity (Entity (N)))
4157 -- The entity of the copied node is the formal parameter
4159 and then Entity (Entity (N)) = Formal
4161 Use_Counter := Use_Counter + 1;
4163 if Use_Counter > 1 then
4165 -- Denote more than one use and abandon the traversal
4176 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
4178 -- Start of processing for Formal_Is_Used_Once
4181 Count_Formal_Uses (Orig_Bod);
4182 return Use_Counter = 1;
4183 end Formal_Is_Used_Once;
4185 -- Start of processing for Expand_Inlined_Call
4188 -- Check for an illegal attempt to inline a recursive procedure. If the
4189 -- subprogram has parameters this is detected when trying to supply a
4190 -- binding for parameters that already have one. For parameterless
4191 -- subprograms this must be done explicitly.
4193 if In_Open_Scopes (Subp) then
4194 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
4195 Set_Is_Inlined (Subp, False);
4199 if Nkind (Orig_Bod) = N_Defining_Identifier
4200 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
4202 -- Subprogram is renaming_as_body. Calls occurring after the renaming
4203 -- can be replaced with calls to the renamed entity directly, because
4204 -- the subprograms are subtype conformant. If the renamed subprogram
4205 -- is an inherited operation, we must redo the expansion because
4206 -- implicit conversions may be needed. Similarly, if the renamed
4207 -- entity is inlined, expand the call for further optimizations.
4209 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
4211 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
4218 -- Use generic machinery to copy body of inlined subprogram, as if it
4219 -- were an instantiation, resetting source locations appropriately, so
4220 -- that nested inlined calls appear in the main unit.
4222 Save_Env (Subp, Empty);
4223 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
4225 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
4227 Make_Block_Statement (Loc,
4228 Declarations => Declarations (Bod),
4229 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
4231 if No (Declarations (Bod)) then
4232 Set_Declarations (Blk, New_List);
4235 -- For the unconstrained case, capture the name of the local variable
4236 -- that holds the result. This must be the first declaration in the
4237 -- block, because its bounds cannot depend on local variables. Otherwise
4238 -- there is no way to declare the result outside of the block. Needless
4239 -- to say, in general the bounds will depend on the actuals in the call.
4241 -- If the context is an assignment statement, as is the case for the
4242 -- expansion of an extended return, the left-hand side provides bounds
4243 -- even if the return type is unconstrained.
4246 if Nkind (Parent (N)) /= N_Assignment_Statement then
4247 Targ1 := Defining_Identifier (First (Declarations (Blk)));
4249 Targ1 := Name (Parent (N));
4253 -- If this is a derived function, establish the proper return type
4255 if Present (Orig_Subp) and then Orig_Subp /= Subp then
4256 Ret_Type := Etype (Orig_Subp);
4258 Ret_Type := Etype (Subp);
4261 -- Create temporaries for the actuals that are expressions, or that
4262 -- are scalars and require copying to preserve semantics.
4264 F := First_Formal (Subp);
4265 A := First_Actual (N);
4266 while Present (F) loop
4267 if Present (Renamed_Object (F)) then
4268 Error_Msg_N ("cannot inline call to recursive subprogram", N);
4272 -- If the argument may be a controlling argument in a call within
4273 -- the inlined body, we must preserve its classwide nature to insure
4274 -- that dynamic dispatching take place subsequently. If the formal
4275 -- has a constraint it must be preserved to retain the semantics of
4278 if Is_Class_Wide_Type (Etype (F))
4279 or else (Is_Access_Type (Etype (F))
4280 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
4282 Temp_Typ := Etype (F);
4284 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
4285 and then Etype (F) /= Base_Type (Etype (F))
4287 Temp_Typ := Etype (F);
4289 Temp_Typ := Etype (A);
4292 -- If the actual is a simple name or a literal, no need to
4293 -- create a temporary, object can be used directly.
4295 -- If the actual is a literal and the formal has its address taken,
4296 -- we cannot pass the literal itself as an argument, so its value
4297 -- must be captured in a temporary.
4299 if (Is_Entity_Name (A)
4301 (not Is_Scalar_Type (Etype (A))
4302 or else Ekind (Entity (A)) = E_Enumeration_Literal))
4304 -- When the actual is an identifier and the corresponding formal
4305 -- is used only once in the original body, the formal can be
4306 -- substituted directly with the actual parameter.
4308 or else (Nkind (A) = N_Identifier
4309 and then Formal_Is_Used_Once (F))
4312 (Nkind_In (A, N_Real_Literal,
4314 N_Character_Literal)
4315 and then not Address_Taken (F))
4317 if Etype (F) /= Etype (A) then
4319 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
4321 Set_Renamed_Object (F, A);
4325 Temp := Make_Temporary (Loc, 'C');
4327 -- If the actual for an in/in-out parameter is a view conversion,
4328 -- make it into an unchecked conversion, given that an untagged
4329 -- type conversion is not a proper object for a renaming.
4331 -- In-out conversions that involve real conversions have already
4332 -- been transformed in Expand_Actuals.
4334 if Nkind (A) = N_Type_Conversion
4335 and then Ekind (F) /= E_In_Parameter
4338 Make_Unchecked_Type_Conversion (Loc,
4339 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
4340 Expression => Relocate_Node (Expression (A)));
4342 elsif Etype (F) /= Etype (A) then
4343 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
4344 Temp_Typ := Etype (F);
4347 New_A := Relocate_Node (A);
4350 Set_Sloc (New_A, Sloc (N));
4352 -- If the actual has a by-reference type, it cannot be copied, so
4353 -- its value is captured in a renaming declaration. Otherwise
4354 -- declare a local constant initialized with the actual.
4356 -- We also use a renaming declaration for expressions of an array
4357 -- type that is not bit-packed, both for efficiency reasons and to
4358 -- respect the semantics of the call: in most cases the original
4359 -- call will pass the parameter by reference, and thus the inlined
4360 -- code will have the same semantics.
4362 if Ekind (F) = E_In_Parameter
4363 and then not Is_By_Reference_Type (Etype (A))
4365 (not Is_Array_Type (Etype (A))
4366 or else not Is_Object_Reference (A)
4367 or else Is_Bit_Packed_Array (Etype (A)))
4370 Make_Object_Declaration (Loc,
4371 Defining_Identifier => Temp,
4372 Constant_Present => True,
4373 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
4374 Expression => New_A);
4377 Make_Object_Renaming_Declaration (Loc,
4378 Defining_Identifier => Temp,
4379 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
4383 Append (Decl, Decls);
4384 Set_Renamed_Object (F, Temp);
4391 -- Establish target of function call. If context is not assignment or
4392 -- declaration, create a temporary as a target. The declaration for the
4393 -- temporary may be subsequently optimized away if the body is a single
4394 -- expression, or if the left-hand side of the assignment is simple
4395 -- enough, i.e. an entity or an explicit dereference of one.
4397 if Ekind (Subp) = E_Function then
4398 if Nkind (Parent (N)) = N_Assignment_Statement
4399 and then Is_Entity_Name (Name (Parent (N)))
4401 Targ := Name (Parent (N));
4403 elsif Nkind (Parent (N)) = N_Assignment_Statement
4404 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4405 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4407 Targ := Name (Parent (N));
4409 elsif Nkind (Parent (N)) = N_Assignment_Statement
4410 and then Nkind (Name (Parent (N))) = N_Selected_Component
4411 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4413 Targ := New_Copy_Tree (Name (Parent (N)));
4415 elsif Nkind (Parent (N)) = N_Object_Declaration
4416 and then Is_Limited_Type (Etype (Subp))
4418 Targ := Defining_Identifier (Parent (N));
4421 -- Replace call with temporary and create its declaration
4423 Temp := Make_Temporary (Loc, 'C');
4424 Set_Is_Internal (Temp);
4426 -- For the unconstrained case, the generated temporary has the
4427 -- same constrained declaration as the result variable. It may
4428 -- eventually be possible to remove that temporary and use the
4429 -- result variable directly.
4432 and then Nkind (Parent (N)) /= N_Assignment_Statement
4435 Make_Object_Declaration (Loc,
4436 Defining_Identifier => Temp,
4437 Object_Definition =>
4438 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4440 Replace_Formals (Decl);
4444 Make_Object_Declaration (Loc,
4445 Defining_Identifier => Temp,
4446 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
4448 Set_Etype (Temp, Ret_Type);
4451 Set_No_Initialization (Decl);
4452 Append (Decl, Decls);
4453 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4458 Insert_Actions (N, Decls);
4460 -- Traverse the tree and replace formals with actuals or their thunks.
4461 -- Attach block to tree before analysis and rewriting.
4463 Replace_Formals (Blk);
4464 Set_Parent (Blk, N);
4466 if not Comes_From_Source (Subp) or else Is_Predef then
4470 if Present (Exit_Lab) then
4472 -- If the body was a single expression, the single return statement
4473 -- and the corresponding label are useless.
4477 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4480 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4482 Append (Lab_Decl, (Declarations (Blk)));
4483 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
4487 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
4488 -- conflicting private views that Gigi would ignore. If this is a
4489 -- predefined unit, analyze with checks off, as is done in the non-
4490 -- inlined run-time units.
4493 I_Flag : constant Boolean := In_Inlined_Body;
4496 In_Inlined_Body := True;
4500 Style : constant Boolean := Style_Check;
4502 Style_Check := False;
4503 Analyze (Blk, Suppress => All_Checks);
4504 Style_Check := Style;
4511 In_Inlined_Body := I_Flag;
4514 if Ekind (Subp) = E_Procedure then
4515 Rewrite_Procedure_Call (N, Blk);
4518 Rewrite_Function_Call (N, Blk);
4520 -- For the unconstrained case, the replacement of the call has been
4521 -- made prior to the complete analysis of the generated declarations.
4522 -- Propagate the proper type now.
4525 if Nkind (N) = N_Identifier then
4526 Set_Etype (N, Etype (Entity (N)));
4528 Set_Etype (N, Etype (Targ1));
4535 -- Cleanup mapping between formals and actuals for other expansions
4537 F := First_Formal (Subp);
4538 while Present (F) loop
4539 Set_Renamed_Object (F, Empty);
4542 end Expand_Inlined_Call;
4544 ----------------------------------------
4545 -- Expand_N_Extended_Return_Statement --
4546 ----------------------------------------
4548 -- If there is a Handled_Statement_Sequence, we rewrite this:
4550 -- return Result : T := <expression> do
4551 -- <handled_seq_of_stms>
4557 -- Result : T := <expression>;
4559 -- <handled_seq_of_stms>
4563 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4565 -- return Result : T := <expression>;
4569 -- return <expression>;
4571 -- unless it's build-in-place or there's no <expression>, in which case
4575 -- Result : T := <expression>;
4580 -- Note that this case could have been written by the user as an extended
4581 -- return statement, or could have been transformed to this from a simple
4582 -- return statement.
4584 -- That is, we need to have a reified return object if there are statements
4585 -- (which might refer to it) or if we're doing build-in-place (so we can
4586 -- set its address to the final resting place or if there is no expression
4587 -- (in which case default initial values might need to be set).
4589 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4590 Loc : constant Source_Ptr := Sloc (N);
4592 Par_Func : constant Entity_Id :=
4593 Return_Applies_To (Return_Statement_Entity (N));
4594 Result_Subt : constant Entity_Id := Etype (Par_Func);
4595 Ret_Obj_Id : constant Entity_Id :=
4596 First_Entity (Return_Statement_Entity (N));
4597 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4599 Is_Build_In_Place : constant Boolean :=
4600 Is_Build_In_Place_Function (Par_Func);
4605 Return_Stmt : Node_Id;
4608 function Build_Heap_Allocator
4609 (Temp_Id : Entity_Id;
4610 Temp_Typ : Entity_Id;
4611 Func_Id : Entity_Id;
4612 Ret_Typ : Entity_Id;
4613 Alloc_Expr : Node_Id) return Node_Id;
4614 -- Create the statements necessary to allocate a return object on the
4615 -- caller's master. The master is available through implicit parameter
4616 -- BIPfinalizationmaster.
4618 -- if BIPfinalizationmaster /= null then
4620 -- type Ptr_Typ is access Ret_Typ;
4621 -- for Ptr_Typ'Storage_Pool use
4622 -- Base_Pool (BIPfinalizationmaster.all).all;
4626 -- procedure Allocate (...) is
4628 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4631 -- Local := <Alloc_Expr>;
4632 -- Temp_Id := Temp_Typ (Local);
4636 -- Temp_Id is the temporary which is used to reference the internally
4637 -- created object in all allocation forms. Temp_Typ is the type of the
4638 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4639 -- type of Func_Id. Alloc_Expr is the actual allocator.
4641 function Move_Activation_Chain return Node_Id;
4642 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4644 -- From current activation chain
4645 -- To activation chain passed in by the caller
4646 -- New_Master master passed in by the caller
4648 --------------------------
4649 -- Build_Heap_Allocator --
4650 --------------------------
4652 function Build_Heap_Allocator
4653 (Temp_Id : Entity_Id;
4654 Temp_Typ : Entity_Id;
4655 Func_Id : Entity_Id;
4656 Ret_Typ : Entity_Id;
4657 Alloc_Expr : Node_Id) return Node_Id
4660 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4662 -- Processing for build-in-place object allocation. This is disabled
4663 -- on .NET/JVM because the targets do not support pools.
4665 if VM_Target = No_VM
4666 and then Needs_Finalization (Ret_Typ)
4669 Decls : constant List_Id := New_List;
4670 Fin_Mas_Id : constant Entity_Id :=
4671 Build_In_Place_Formal
4672 (Func_Id, BIP_Finalization_Master);
4673 Stmts : constant List_Id := New_List;
4674 Desig_Typ : Entity_Id;
4675 Local_Id : Entity_Id;
4676 Pool_Id : Entity_Id;
4677 Ptr_Typ : Entity_Id;
4681 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4683 Pool_Id := Make_Temporary (Loc, 'P');
4686 Make_Object_Renaming_Declaration (Loc,
4687 Defining_Identifier => Pool_Id,
4689 New_Reference_To (RTE (RE_Root_Storage_Pool), Loc),
4691 Make_Explicit_Dereference (Loc,
4693 Make_Function_Call (Loc,
4695 New_Reference_To (RTE (RE_Base_Pool), Loc),
4696 Parameter_Associations => New_List (
4697 Make_Explicit_Dereference (Loc,
4699 New_Reference_To (Fin_Mas_Id, Loc)))))));
4701 -- Create an access type which uses the storage pool of the
4702 -- caller's master. This additional type is necessary because
4703 -- the finalization master cannot be associated with the type
4704 -- of the temporary. Otherwise the secondary stack allocation
4707 Desig_Typ := Ret_Typ;
4709 -- Ensure that the build-in-place machinery uses a fat pointer
4710 -- when allocating an unconstrained array on the heap. In this
4711 -- case the result object type is a constrained array type even
4712 -- though the function type is unconstrained.
4714 if Ekind (Desig_Typ) = E_Array_Subtype then
4715 Desig_Typ := Base_Type (Desig_Typ);
4719 -- type Ptr_Typ is access Desig_Typ;
4721 Ptr_Typ := Make_Temporary (Loc, 'P');
4724 Make_Full_Type_Declaration (Loc,
4725 Defining_Identifier => Ptr_Typ,
4727 Make_Access_To_Object_Definition (Loc,
4728 Subtype_Indication =>
4729 New_Reference_To (Desig_Typ, Loc))));
4731 -- Perform minor decoration in order to set the master and the
4732 -- storage pool attributes.
4734 Set_Ekind (Ptr_Typ, E_Access_Type);
4735 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4736 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4738 -- Create the temporary, generate:
4739 -- Local_Id : Ptr_Typ;
4741 Local_Id := Make_Temporary (Loc, 'T');
4744 Make_Object_Declaration (Loc,
4745 Defining_Identifier => Local_Id,
4746 Object_Definition =>
4747 New_Reference_To (Ptr_Typ, Loc)));
4749 -- Allocate the object, generate:
4750 -- Local_Id := <Alloc_Expr>;
4753 Make_Assignment_Statement (Loc,
4754 Name => New_Reference_To (Local_Id, Loc),
4755 Expression => Alloc_Expr));
4758 -- Temp_Id := Temp_Typ (Local_Id);
4761 Make_Assignment_Statement (Loc,
4762 Name => New_Reference_To (Temp_Id, Loc),
4764 Unchecked_Convert_To (Temp_Typ,
4765 New_Reference_To (Local_Id, Loc))));
4767 -- Wrap the allocation in a block. This is further conditioned
4768 -- by checking the caller finalization master at runtime. A
4769 -- null value indicates a non-existent master, most likely due
4770 -- to a Finalize_Storage_Only allocation.
4773 -- if BIPfinalizationmaster /= null then
4782 Make_If_Statement (Loc,
4785 Left_Opnd => New_Reference_To (Fin_Mas_Id, Loc),
4786 Right_Opnd => Make_Null (Loc)),
4788 Then_Statements => New_List (
4789 Make_Block_Statement (Loc,
4790 Declarations => Decls,
4791 Handled_Statement_Sequence =>
4792 Make_Handled_Sequence_Of_Statements (Loc,
4793 Statements => Stmts))));
4796 -- For all other cases, generate:
4797 -- Temp_Id := <Alloc_Expr>;
4801 Make_Assignment_Statement (Loc,
4802 Name => New_Reference_To (Temp_Id, Loc),
4803 Expression => Alloc_Expr);
4805 end Build_Heap_Allocator;
4807 ---------------------------
4808 -- Move_Activation_Chain --
4809 ---------------------------
4811 function Move_Activation_Chain return Node_Id is
4814 Make_Procedure_Call_Statement (Loc,
4816 New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
4818 Parameter_Associations => New_List (
4822 Make_Attribute_Reference (Loc,
4823 Prefix => Make_Identifier (Loc, Name_uChain),
4824 Attribute_Name => Name_Unrestricted_Access),
4826 -- Destination chain
4829 (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc),
4834 (Build_In_Place_Formal (Par_Func, BIP_Master), Loc)));
4835 end Move_Activation_Chain;
4837 -- Start of processing for Expand_N_Extended_Return_Statement
4840 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4841 Exp := Expression (Ret_Obj_Decl);
4846 HSS := Handled_Statement_Sequence (N);
4848 -- If the returned object needs finalization actions, the function must
4849 -- perform the appropriate cleanup should it fail to return. The state
4850 -- of the function itself is tracked through a flag which is coupled
4851 -- with the scope finalizer. There is one flag per each return object
4852 -- in case of multiple returns.
4854 if Is_Build_In_Place
4855 and then Needs_Finalization (Etype (Ret_Obj_Id))
4858 Flag_Decl : Node_Id;
4859 Flag_Id : Entity_Id;
4863 -- Recover the function body
4865 Func_Bod := Unit_Declaration_Node (Par_Func);
4867 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4868 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4871 -- Create a flag to track the function state
4873 Flag_Id := Make_Temporary (Loc, 'F');
4874 Set_Return_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4876 -- Insert the flag at the beginning of the function declarations,
4878 -- Fnn : Boolean := False;
4881 Make_Object_Declaration (Loc,
4882 Defining_Identifier => Flag_Id,
4883 Object_Definition =>
4884 New_Reference_To (Standard_Boolean, Loc),
4885 Expression => New_Reference_To (Standard_False, Loc));
4887 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4888 Analyze (Flag_Decl);
4892 -- Build a simple_return_statement that returns the return object when
4893 -- there is a statement sequence, or no expression, or the result will
4894 -- be built in place. Note however that we currently do this for all
4895 -- composite cases, even though nonlimited composite results are not yet
4896 -- built in place (though we plan to do so eventually).
4899 or else Is_Composite_Type (Result_Subt)
4905 -- If the extended return has a handled statement sequence, then wrap
4906 -- it in a block and use the block as the first statement.
4910 Make_Block_Statement (Loc,
4911 Declarations => New_List,
4912 Handled_Statement_Sequence => HSS));
4915 -- If the result type contains tasks, we call Move_Activation_Chain.
4916 -- Later, the cleanup code will call Complete_Master, which will
4917 -- terminate any unactivated tasks belonging to the return statement
4918 -- master. But Move_Activation_Chain updates their master to be that
4919 -- of the caller, so they will not be terminated unless the return
4920 -- statement completes unsuccessfully due to exception, abort, goto,
4921 -- or exit. As a formality, we test whether the function requires the
4922 -- result to be built in place, though that's necessarily true for
4923 -- the case of result types with task parts.
4925 if Is_Build_In_Place
4926 and then Has_Task (Result_Subt)
4928 -- The return expression is an aggregate for a complex type which
4929 -- contains tasks. This particular case is left unexpanded since
4930 -- the regular expansion would insert all temporaries and
4931 -- initialization code in the wrong block.
4933 if Nkind (Exp) = N_Aggregate then
4934 Expand_N_Aggregate (Exp);
4937 -- Do not move the activation chain if the return object does not
4940 if Has_Task (Etype (Ret_Obj_Id)) then
4941 Append_To (Stmts, Move_Activation_Chain);
4945 -- Update the state of the function right before the object is
4948 if Is_Build_In_Place
4949 and then Needs_Finalization (Etype (Ret_Obj_Id))
4952 Flag_Id : constant Entity_Id :=
4953 Return_Flag_Or_Transient_Decl (Ret_Obj_Id);
4960 Make_Assignment_Statement (Loc,
4961 Name => New_Reference_To (Flag_Id, Loc),
4962 Expression => New_Reference_To (Standard_True, Loc)));
4966 -- Build a simple_return_statement that returns the return object
4969 Make_Simple_Return_Statement (Loc,
4970 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4971 Append_To (Stmts, Return_Stmt);
4973 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4976 -- Case where we build a return statement block
4978 if Present (HSS) then
4980 Make_Block_Statement (Loc,
4981 Declarations => Return_Object_Declarations (N),
4982 Handled_Statement_Sequence => HSS);
4984 -- We set the entity of the new block statement to be that of the
4985 -- return statement. This is necessary so that various fields, such
4986 -- as Finalization_Chain_Entity carry over from the return statement
4987 -- to the block. Note that this block is unusual, in that its entity
4988 -- is an E_Return_Statement rather than an E_Block.
4991 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4993 -- If the object decl was already rewritten as a renaming, then we
4994 -- don't want to do the object allocation and transformation of of
4995 -- the return object declaration to a renaming. This case occurs
4996 -- when the return object is initialized by a call to another
4997 -- build-in-place function, and that function is responsible for
4998 -- the allocation of the return object.
5000 if Is_Build_In_Place
5001 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
5004 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
5005 and then Is_Build_In_Place_Function_Call
5006 (Expression (Original_Node (Ret_Obj_Decl))));
5008 -- Return the build-in-place result by reference
5010 Set_By_Ref (Return_Stmt);
5012 elsif Is_Build_In_Place then
5014 -- Locate the implicit access parameter associated with the
5015 -- caller-supplied return object and convert the return
5016 -- statement's return object declaration to a renaming of a
5017 -- dereference of the access parameter. If the return object's
5018 -- declaration includes an expression that has not already been
5019 -- expanded as separate assignments, then add an assignment
5020 -- statement to ensure the return object gets initialized.
5023 -- Result : T [:= <expression>];
5030 -- Result : T renames FuncRA.all;
5031 -- [Result := <expression;]
5036 Return_Obj_Id : constant Entity_Id :=
5037 Defining_Identifier (Ret_Obj_Decl);
5038 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
5039 Return_Obj_Expr : constant Node_Id :=
5040 Expression (Ret_Obj_Decl);
5041 Constr_Result : constant Boolean :=
5042 Is_Constrained (Result_Subt);
5043 Obj_Alloc_Formal : Entity_Id;
5044 Object_Access : Entity_Id;
5045 Obj_Acc_Deref : Node_Id;
5046 Init_Assignment : Node_Id := Empty;
5049 -- Build-in-place results must be returned by reference
5051 Set_By_Ref (Return_Stmt);
5053 -- Retrieve the implicit access parameter passed by the caller
5056 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
5058 -- If the return object's declaration includes an expression
5059 -- and the declaration isn't marked as No_Initialization, then
5060 -- we need to generate an assignment to the object and insert
5061 -- it after the declaration before rewriting it as a renaming
5062 -- (otherwise we'll lose the initialization). The case where
5063 -- the result type is an interface (or class-wide interface)
5064 -- is also excluded because the context of the function call
5065 -- must be unconstrained, so the initialization will always
5066 -- be done as part of an allocator evaluation (storage pool
5067 -- or secondary stack), never to a constrained target object
5068 -- passed in by the caller. Besides the assignment being
5069 -- unneeded in this case, it avoids problems with trying to
5070 -- generate a dispatching assignment when the return expression
5071 -- is a nonlimited descendant of a limited interface (the
5072 -- interface has no assignment operation).
5074 if Present (Return_Obj_Expr)
5075 and then not No_Initialization (Ret_Obj_Decl)
5076 and then not Is_Interface (Return_Obj_Typ)
5079 Make_Assignment_Statement (Loc,
5080 Name => New_Reference_To (Return_Obj_Id, Loc),
5081 Expression => Relocate_Node (Return_Obj_Expr));
5083 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
5084 Set_Assignment_OK (Name (Init_Assignment));
5085 Set_No_Ctrl_Actions (Init_Assignment);
5087 Set_Parent (Name (Init_Assignment), Init_Assignment);
5088 Set_Parent (Expression (Init_Assignment), Init_Assignment);
5090 Set_Expression (Ret_Obj_Decl, Empty);
5092 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
5093 and then not Is_Class_Wide_Type
5094 (Etype (Expression (Init_Assignment)))
5096 Rewrite (Expression (Init_Assignment),
5097 Make_Type_Conversion (Loc,
5099 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
5101 Relocate_Node (Expression (Init_Assignment))));
5104 -- In the case of functions where the calling context can
5105 -- determine the form of allocation needed, initialization
5106 -- is done with each part of the if statement that handles
5107 -- the different forms of allocation (this is true for
5108 -- unconstrained and tagged result subtypes).
5111 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
5113 Insert_After (Ret_Obj_Decl, Init_Assignment);
5117 -- When the function's subtype is unconstrained, a run-time
5118 -- test is needed to determine the form of allocation to use
5119 -- for the return object. The function has an implicit formal
5120 -- parameter indicating this. If the BIP_Alloc_Form formal has
5121 -- the value one, then the caller has passed access to an
5122 -- existing object for use as the return object. If the value
5123 -- is two, then the return object must be allocated on the
5124 -- secondary stack. Otherwise, the object must be allocated in
5125 -- a storage pool (currently only supported for the global
5126 -- heap, user-defined storage pools TBD ???). We generate an
5127 -- if statement to test the implicit allocation formal and
5128 -- initialize a local access value appropriately, creating
5129 -- allocators in the secondary stack and global heap cases.
5130 -- The special formal also exists and must be tested when the
5131 -- function has a tagged result, even when the result subtype
5132 -- is constrained, because in general such functions can be
5133 -- called in dispatching contexts and must be handled similarly
5134 -- to functions with a class-wide result.
5136 if not Constr_Result
5137 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5140 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
5143 Pool_Id : constant Entity_Id :=
5144 Make_Temporary (Loc, 'P');
5145 Alloc_Obj_Id : Entity_Id;
5146 Alloc_Obj_Decl : Node_Id;
5147 Alloc_If_Stmt : Node_Id;
5148 Heap_Allocator : Node_Id;
5149 Pool_Decl : Node_Id;
5150 Pool_Allocator : Node_Id;
5151 Ptr_Type_Decl : Node_Id;
5152 Ref_Type : Entity_Id;
5153 SS_Allocator : Node_Id;
5156 -- Reuse the itype created for the function's implicit
5157 -- access formal. This avoids the need to create a new
5158 -- access type here, plus it allows assigning the access
5159 -- formal directly without applying a conversion.
5161 -- Ref_Type := Etype (Object_Access);
5163 -- Create an access type designating the function's
5166 Ref_Type := Make_Temporary (Loc, 'A');
5169 Make_Full_Type_Declaration (Loc,
5170 Defining_Identifier => Ref_Type,
5172 Make_Access_To_Object_Definition (Loc,
5173 All_Present => True,
5174 Subtype_Indication =>
5175 New_Reference_To (Return_Obj_Typ, Loc)));
5177 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5179 -- Create an access object that will be initialized to an
5180 -- access value denoting the return object, either coming
5181 -- from an implicit access value passed in by the caller
5182 -- or from the result of an allocator.
5184 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5185 Set_Etype (Alloc_Obj_Id, Ref_Type);
5188 Make_Object_Declaration (Loc,
5189 Defining_Identifier => Alloc_Obj_Id,
5190 Object_Definition =>
5191 New_Reference_To (Ref_Type, Loc));
5193 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5195 -- Create allocators for both the secondary stack and
5196 -- global heap. If there's an initialization expression,
5197 -- then create these as initialized allocators.
5199 if Present (Return_Obj_Expr)
5200 and then not No_Initialization (Ret_Obj_Decl)
5202 -- Always use the type of the expression for the
5203 -- qualified expression, rather than the result type.
5204 -- In general we cannot always use the result type
5205 -- for the allocator, because the expression might be
5206 -- of a specific type, such as in the case of an
5207 -- aggregate or even a nonlimited object when the
5208 -- result type is a limited class-wide interface type.
5211 Make_Allocator (Loc,
5213 Make_Qualified_Expression (Loc,
5216 (Etype (Return_Obj_Expr), Loc),
5218 New_Copy_Tree (Return_Obj_Expr)));
5221 -- If the function returns a class-wide type we cannot
5222 -- use the return type for the allocator. Instead we
5223 -- use the type of the expression, which must be an
5224 -- aggregate of a definite type.
5226 if Is_Class_Wide_Type (Return_Obj_Typ) then
5228 Make_Allocator (Loc,
5231 (Etype (Return_Obj_Expr), Loc));
5234 Make_Allocator (Loc,
5236 New_Reference_To (Return_Obj_Typ, Loc));
5239 -- If the object requires default initialization then
5240 -- that will happen later following the elaboration of
5241 -- the object renaming. If we don't turn it off here
5242 -- then the object will be default initialized twice.
5244 Set_No_Initialization (Heap_Allocator);
5247 -- The Pool_Allocator is just like the Heap_Allocator,
5248 -- except we set Storage_Pool and Procedure_To_Call so
5249 -- it will use the user-defined storage pool.
5251 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
5253 -- Do not generate the renaming of the build-in-place
5254 -- pool parameter on .NET/JVM because the parameter is
5255 -- not created in the first place.
5257 if VM_Target = No_VM then
5259 Make_Object_Renaming_Declaration (Loc,
5260 Defining_Identifier => Pool_Id,
5263 (RTE (RE_Root_Storage_Pool), Loc),
5265 Make_Explicit_Dereference (Loc,
5267 (Build_In_Place_Formal
5268 (Par_Func, BIP_Storage_Pool), Loc)));
5269 Set_Storage_Pool (Pool_Allocator, Pool_Id);
5270 Set_Procedure_To_Call
5271 (Pool_Allocator, RTE (RE_Allocate_Any));
5273 Pool_Decl := Make_Null_Statement (Loc);
5276 -- If the No_Allocators restriction is active, then only
5277 -- an allocator for secondary stack allocation is needed.
5278 -- It's OK for such allocators to have Comes_From_Source
5279 -- set to False, because gigi knows not to flag them as
5280 -- being a violation of No_Implicit_Heap_Allocations.
5282 if Restriction_Active (No_Allocators) then
5283 SS_Allocator := Heap_Allocator;
5284 Heap_Allocator := Make_Null (Loc);
5285 Pool_Allocator := Make_Null (Loc);
5287 -- Otherwise the heap and pool allocators may be needed,
5288 -- so we make another allocator for secondary stack
5292 SS_Allocator := New_Copy_Tree (Heap_Allocator);
5294 -- The heap and pool allocators are marked as
5295 -- Comes_From_Source since they correspond to an
5296 -- explicit user-written allocator (that is, it will
5297 -- only be executed on behalf of callers that call the
5298 -- function as initialization for such an allocator).
5299 -- Prevents errors when No_Implicit_Heap_Allocations
5302 Set_Comes_From_Source (Heap_Allocator, True);
5303 Set_Comes_From_Source (Pool_Allocator, True);
5306 -- The allocator is returned on the secondary stack. We
5307 -- don't do this on VM targets, since the SS is not used.
5309 if VM_Target = No_VM then
5310 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5311 Set_Procedure_To_Call
5312 (SS_Allocator, RTE (RE_SS_Allocate));
5314 -- The allocator is returned on the secondary stack,
5315 -- so indicate that the function return, as well as
5316 -- the block that encloses the allocator, must not
5317 -- release it. The flags must be set now because
5318 -- the decision to use the secondary stack is done
5319 -- very late in the course of expanding the return
5320 -- statement, past the point where these flags are
5323 Set_Sec_Stack_Needed_For_Return (Par_Func);
5324 Set_Sec_Stack_Needed_For_Return
5325 (Return_Statement_Entity (N));
5326 Set_Uses_Sec_Stack (Par_Func);
5327 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5330 -- Create an if statement to test the BIP_Alloc_Form
5331 -- formal and initialize the access object to either the
5332 -- BIP_Object_Access formal (BIP_Alloc_Form =
5333 -- Caller_Allocation), the result of allocating the
5334 -- object in the secondary stack (BIP_Alloc_Form =
5335 -- Secondary_Stack), or else an allocator to create the
5336 -- return object in the heap or user-defined pool
5337 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5339 -- ??? An unchecked type conversion must be made in the
5340 -- case of assigning the access object formal to the
5341 -- local access object, because a normal conversion would
5342 -- be illegal in some cases (such as converting access-
5343 -- to-unconstrained to access-to-constrained), but the
5344 -- the unchecked conversion will presumably fail to work
5345 -- right in just such cases. It's not clear at all how to
5349 Make_If_Statement (Loc,
5353 New_Reference_To (Obj_Alloc_Formal, Loc),
5355 Make_Integer_Literal (Loc,
5356 UI_From_Int (BIP_Allocation_Form'Pos
5357 (Caller_Allocation)))),
5359 Then_Statements => New_List (
5360 Make_Assignment_Statement (Loc,
5362 New_Reference_To (Alloc_Obj_Id, Loc),
5364 Make_Unchecked_Type_Conversion (Loc,
5366 New_Reference_To (Ref_Type, Loc),
5368 New_Reference_To (Object_Access, Loc)))),
5370 Elsif_Parts => New_List (
5371 Make_Elsif_Part (Loc,
5375 New_Reference_To (Obj_Alloc_Formal, Loc),
5377 Make_Integer_Literal (Loc,
5378 UI_From_Int (BIP_Allocation_Form'Pos
5379 (Secondary_Stack)))),
5381 Then_Statements => New_List (
5382 Make_Assignment_Statement (Loc,
5384 New_Reference_To (Alloc_Obj_Id, Loc),
5385 Expression => SS_Allocator))),
5387 Make_Elsif_Part (Loc,
5391 New_Reference_To (Obj_Alloc_Formal, Loc),
5393 Make_Integer_Literal (Loc,
5394 UI_From_Int (BIP_Allocation_Form'Pos
5397 Then_Statements => New_List (
5398 Build_Heap_Allocator
5399 (Temp_Id => Alloc_Obj_Id,
5400 Temp_Typ => Ref_Type,
5401 Func_Id => Par_Func,
5402 Ret_Typ => Return_Obj_Typ,
5403 Alloc_Expr => Heap_Allocator)))),
5405 Else_Statements => New_List (
5407 Build_Heap_Allocator
5408 (Temp_Id => Alloc_Obj_Id,
5409 Temp_Typ => Ref_Type,
5410 Func_Id => Par_Func,
5411 Ret_Typ => Return_Obj_Typ,
5412 Alloc_Expr => Pool_Allocator)));
5414 -- If a separate initialization assignment was created
5415 -- earlier, append that following the assignment of the
5416 -- implicit access formal to the access object, to ensure
5417 -- that the return object is initialized in that case. In
5418 -- this situation, the target of the assignment must be
5419 -- rewritten to denote a dereference of the access to the
5420 -- return object passed in by the caller.
5422 if Present (Init_Assignment) then
5423 Rewrite (Name (Init_Assignment),
5424 Make_Explicit_Dereference (Loc,
5425 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
5428 (Name (Init_Assignment), Etype (Return_Obj_Id));
5431 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5434 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5436 -- Remember the local access object for use in the
5437 -- dereference of the renaming created below.
5439 Object_Access := Alloc_Obj_Id;
5443 -- Replace the return object declaration with a renaming of a
5444 -- dereference of the access value designating the return
5448 Make_Explicit_Dereference (Loc,
5449 Prefix => New_Reference_To (Object_Access, Loc));
5451 Rewrite (Ret_Obj_Decl,
5452 Make_Object_Renaming_Declaration (Loc,
5453 Defining_Identifier => Return_Obj_Id,
5454 Access_Definition => Empty,
5456 New_Occurrence_Of (Return_Obj_Typ, Loc),
5457 Name => Obj_Acc_Deref));
5459 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
5463 -- Case where we do not build a block
5466 -- We're about to drop Return_Object_Declarations on the floor, so
5467 -- we need to insert it, in case it got expanded into useful code.
5468 -- Remove side effects from expression, which may be duplicated in
5469 -- subsequent checks (see Expand_Simple_Function_Return).
5471 Insert_List_Before (N, Return_Object_Declarations (N));
5472 Remove_Side_Effects (Exp);
5474 -- Build simple_return_statement that returns the expression directly
5476 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5477 Result := Return_Stmt;
5480 -- Set the flag to prevent infinite recursion
5482 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5484 Rewrite (N, Result);
5486 end Expand_N_Extended_Return_Statement;
5488 ----------------------------
5489 -- Expand_N_Function_Call --
5490 ----------------------------
5492 procedure Expand_N_Function_Call (N : Node_Id) is
5496 -- If the return value of a foreign compiled function is VAX Float, then
5497 -- expand the return (adjusts the location of the return value on
5498 -- Alpha/VMS, no-op everywhere else).
5499 -- Comes_From_Source intercepts recursive expansion.
5501 if Vax_Float (Etype (N))
5502 and then Nkind (N) = N_Function_Call
5503 and then Present (Name (N))
5504 and then Present (Entity (Name (N)))
5505 and then Has_Foreign_Convention (Entity (Name (N)))
5506 and then Comes_From_Source (Parent (N))
5508 Expand_Vax_Foreign_Return (N);
5510 end Expand_N_Function_Call;
5512 ---------------------------------------
5513 -- Expand_N_Procedure_Call_Statement --
5514 ---------------------------------------
5516 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5519 end Expand_N_Procedure_Call_Statement;
5521 --------------------------------------
5522 -- Expand_N_Simple_Return_Statement --
5523 --------------------------------------
5525 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5527 -- Defend against previous errors (i.e. the return statement calls a
5528 -- function that is not available in configurable runtime).
5530 if Present (Expression (N))
5531 and then Nkind (Expression (N)) = N_Empty
5536 -- Distinguish the function and non-function cases:
5538 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5541 E_Generic_Function =>
5542 Expand_Simple_Function_Return (N);
5545 E_Generic_Procedure |
5548 E_Return_Statement =>
5549 Expand_Non_Function_Return (N);
5552 raise Program_Error;
5556 when RE_Not_Available =>
5558 end Expand_N_Simple_Return_Statement;
5560 ------------------------------
5561 -- Expand_N_Subprogram_Body --
5562 ------------------------------
5564 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5567 -- Add dummy push/pop label nodes at start and end to clear any local
5568 -- exception indications if local-exception-to-goto optimization is active.
5570 -- Add return statement if last statement in body is not a return statement
5571 -- (this makes things easier on Gigi which does not want to have to handle
5572 -- a missing return).
5574 -- Add call to Activate_Tasks if body is a task activator
5576 -- Deal with possible detection of infinite recursion
5578 -- Eliminate body completely if convention stubbed
5580 -- Encode entity names within body, since we will not need to reference
5581 -- these entities any longer in the front end.
5583 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5585 -- Reset Pure indication if any parameter has root type System.Address
5586 -- or has any parameters of limited types, where limited means that the
5587 -- run-time view is limited (i.e. the full type is limited).
5591 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5592 Loc : constant Source_Ptr := Sloc (N);
5593 H : constant Node_Id := Handled_Statement_Sequence (N);
5594 Body_Id : Entity_Id;
5597 Spec_Id : Entity_Id;
5599 procedure Add_Return (S : List_Id);
5600 -- Append a return statement to the statement sequence S if the last
5601 -- statement is not already a return or a goto statement. Note that
5602 -- the latter test is not critical, it does not matter if we add a few
5603 -- extra returns, since they get eliminated anyway later on.
5609 procedure Add_Return (S : List_Id) is
5614 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5615 -- not relevant in this context since they are not executable.
5617 Last_Stm := Last (S);
5618 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
5622 -- Now insert return unless last statement is a transfer
5624 if not Is_Transfer (Last_Stm) then
5626 -- The source location for the return is the end label of the
5627 -- procedure if present. Otherwise use the sloc of the last
5628 -- statement in the list. If the list comes from a generated
5629 -- exception handler and we are not debugging generated code,
5630 -- all the statements within the handler are made invisible
5633 if Nkind (Parent (S)) = N_Exception_Handler
5634 and then not Comes_From_Source (Parent (S))
5636 Loc := Sloc (Last_Stm);
5637 elsif Present (End_Label (H)) then
5638 Loc := Sloc (End_Label (H));
5640 Loc := Sloc (Last_Stm);
5644 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
5647 -- Append return statement, and set analyzed manually. We can't
5648 -- call Analyze on this return since the scope is wrong.
5650 -- Note: it almost works to push the scope and then do the
5651 -- Analyze call, but something goes wrong in some weird cases
5652 -- and it is not worth worrying about ???
5657 -- Call _Postconditions procedure if appropriate. We need to
5658 -- do this explicitly because we did not analyze the generated
5659 -- return statement above, so the call did not get inserted.
5661 if Ekind (Spec_Id) = E_Procedure
5662 and then Has_Postconditions (Spec_Id)
5664 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
5666 Make_Procedure_Call_Statement (Loc,
5668 New_Reference_To (Postcondition_Proc (Spec_Id), Loc)));
5674 -- Start of processing for Expand_N_Subprogram_Body
5677 -- Set L to either the list of declarations if present, or to the list
5678 -- of statements if no declarations are present. This is used to insert
5679 -- new stuff at the start.
5681 if Is_Non_Empty_List (Declarations (N)) then
5682 L := Declarations (N);
5684 L := Statements (H);
5687 -- If local-exception-to-goto optimization active, insert dummy push
5688 -- statements at start, and dummy pop statements at end.
5690 if (Debug_Flag_Dot_G
5691 or else Restriction_Active (No_Exception_Propagation))
5692 and then Is_Non_Empty_List (L)
5695 FS : constant Node_Id := First (L);
5696 FL : constant Source_Ptr := Sloc (FS);
5701 -- LS points to either last statement, if statements are present
5702 -- or to the last declaration if there are no statements present.
5703 -- It is the node after which the pop's are generated.
5705 if Is_Non_Empty_List (Statements (H)) then
5706 LS := Last (Statements (H));
5713 Insert_List_Before_And_Analyze (FS, New_List (
5714 Make_Push_Constraint_Error_Label (FL),
5715 Make_Push_Program_Error_Label (FL),
5716 Make_Push_Storage_Error_Label (FL)));
5718 Insert_List_After_And_Analyze (LS, New_List (
5719 Make_Pop_Constraint_Error_Label (LL),
5720 Make_Pop_Program_Error_Label (LL),
5721 Make_Pop_Storage_Error_Label (LL)));
5725 -- Find entity for subprogram
5727 Body_Id := Defining_Entity (N);
5729 if Present (Corresponding_Spec (N)) then
5730 Spec_Id := Corresponding_Spec (N);
5735 -- Need poll on entry to subprogram if polling enabled. We only do this
5736 -- for non-empty subprograms, since it does not seem necessary to poll
5737 -- for a dummy null subprogram.
5739 if Is_Non_Empty_List (L) then
5741 -- Do not add a polling call if the subprogram is to be inlined by
5742 -- the back-end, to avoid repeated calls with multiple inlinings.
5744 if Is_Inlined (Spec_Id)
5745 and then Front_End_Inlining
5746 and then Optimization_Level > 1
5750 Generate_Poll_Call (First (L));
5754 -- If this is a Pure function which has any parameters whose root type
5755 -- is System.Address, reset the Pure indication, since it will likely
5756 -- cause incorrect code to be generated as the parameter is probably
5757 -- a pointer, and the fact that the same pointer is passed does not mean
5758 -- that the same value is being referenced.
5760 -- Note that if the programmer gave an explicit Pure_Function pragma,
5761 -- then we believe the programmer, and leave the subprogram Pure.
5763 -- This code should probably be at the freeze point, so that it happens
5764 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5765 -- semantic tree has Is_Pure set properly ???
5767 if Is_Pure (Spec_Id)
5768 and then Is_Subprogram (Spec_Id)
5769 and then not Has_Pragma_Pure_Function (Spec_Id)
5775 F := First_Formal (Spec_Id);
5776 while Present (F) loop
5777 if Is_Descendent_Of_Address (Etype (F))
5779 -- Note that this test is being made in the body of the
5780 -- subprogram, not the spec, so we are testing the full
5781 -- type for being limited here, as required.
5783 or else Is_Limited_Type (Etype (F))
5785 Set_Is_Pure (Spec_Id, False);
5787 if Spec_Id /= Body_Id then
5788 Set_Is_Pure (Body_Id, False);
5799 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5801 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5806 -- Loop through formals
5808 F := First_Formal (Spec_Id);
5809 while Present (F) loop
5810 if Is_Scalar_Type (Etype (F))
5811 and then Ekind (F) = E_Out_Parameter
5813 Check_Restriction (No_Default_Initialization, F);
5815 -- Insert the initialization. We turn off validity checks
5816 -- for this assignment, since we do not want any check on
5817 -- the initial value itself (which may well be invalid).
5819 Insert_Before_And_Analyze (First (L),
5820 Make_Assignment_Statement (Loc,
5821 Name => New_Occurrence_Of (F, Loc),
5822 Expression => Get_Simple_Init_Val (Etype (F), N)),
5823 Suppress => Validity_Check);
5831 -- Clear out statement list for stubbed procedure
5833 if Present (Corresponding_Spec (N)) then
5834 Set_Elaboration_Flag (N, Spec_Id);
5836 if Convention (Spec_Id) = Convention_Stubbed
5837 or else Is_Eliminated (Spec_Id)
5839 Set_Declarations (N, Empty_List);
5840 Set_Handled_Statement_Sequence (N,
5841 Make_Handled_Sequence_Of_Statements (Loc,
5842 Statements => New_List (Make_Null_Statement (Loc))));
5847 -- Create a set of discriminals for the next protected subprogram body
5849 if Is_List_Member (N)
5850 and then Present (Parent (List_Containing (N)))
5851 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5852 and then Present (Next_Protected_Operation (N))
5854 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5857 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5858 -- subprograms with no specs are not frozen.
5861 Typ : constant Entity_Id := Etype (Spec_Id);
5862 Utyp : constant Entity_Id := Underlying_Type (Typ);
5865 if not Acts_As_Spec (N)
5866 and then Nkind (Parent (Parent (Spec_Id))) /=
5867 N_Subprogram_Body_Stub
5871 elsif Is_Immutably_Limited_Type (Typ) then
5872 Set_Returns_By_Ref (Spec_Id);
5874 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5875 Set_Returns_By_Ref (Spec_Id);
5879 -- For a procedure, we add a return for all possible syntactic ends of
5882 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5883 Add_Return (Statements (H));
5885 if Present (Exception_Handlers (H)) then
5886 Except_H := First_Non_Pragma (Exception_Handlers (H));
5887 while Present (Except_H) loop
5888 Add_Return (Statements (Except_H));
5889 Next_Non_Pragma (Except_H);
5893 -- For a function, we must deal with the case where there is at least
5894 -- one missing return. What we do is to wrap the entire body of the
5895 -- function in a block:
5908 -- raise Program_Error;
5911 -- This approach is necessary because the raise must be signalled to the
5912 -- caller, not handled by any local handler (RM 6.4(11)).
5914 -- Note: we do not need to analyze the constructed sequence here, since
5915 -- it has no handler, and an attempt to analyze the handled statement
5916 -- sequence twice is risky in various ways (e.g. the issue of expanding
5917 -- cleanup actions twice).
5919 elsif Has_Missing_Return (Spec_Id) then
5921 Hloc : constant Source_Ptr := Sloc (H);
5922 Blok : constant Node_Id :=
5923 Make_Block_Statement (Hloc,
5924 Handled_Statement_Sequence => H);
5925 Rais : constant Node_Id :=
5926 Make_Raise_Program_Error (Hloc,
5927 Reason => PE_Missing_Return);
5930 Set_Handled_Statement_Sequence (N,
5931 Make_Handled_Sequence_Of_Statements (Hloc,
5932 Statements => New_List (Blok, Rais)));
5934 Push_Scope (Spec_Id);
5941 -- If subprogram contains a parameterless recursive call, then we may
5942 -- have an infinite recursion, so see if we can generate code to check
5943 -- for this possibility if storage checks are not suppressed.
5945 if Ekind (Spec_Id) = E_Procedure
5946 and then Has_Recursive_Call (Spec_Id)
5947 and then not Storage_Checks_Suppressed (Spec_Id)
5949 Detect_Infinite_Recursion (N, Spec_Id);
5952 -- Set to encode entity names in package body before gigi is called
5954 Qualify_Entity_Names (N);
5955 end Expand_N_Subprogram_Body;
5957 -----------------------------------
5958 -- Expand_N_Subprogram_Body_Stub --
5959 -----------------------------------
5961 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5963 if Present (Corresponding_Body (N)) then
5964 Expand_N_Subprogram_Body (
5965 Unit_Declaration_Node (Corresponding_Body (N)));
5967 end Expand_N_Subprogram_Body_Stub;
5969 -------------------------------------
5970 -- Expand_N_Subprogram_Declaration --
5971 -------------------------------------
5973 -- If the declaration appears within a protected body, it is a private
5974 -- operation of the protected type. We must create the corresponding
5975 -- protected subprogram an associated formals. For a normal protected
5976 -- operation, this is done when expanding the protected type declaration.
5978 -- If the declaration is for a null procedure, emit null body
5980 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5981 Loc : constant Source_Ptr := Sloc (N);
5982 Subp : constant Entity_Id := Defining_Entity (N);
5983 Scop : constant Entity_Id := Scope (Subp);
5984 Prot_Decl : Node_Id;
5986 Prot_Id : Entity_Id;
5989 -- In SPARK, subprogram declarations are only allowed in package
5992 if Nkind (Parent (N)) /= N_Package_Specification then
5993 if Nkind (Parent (N)) = N_Compilation_Unit then
5994 Check_SPARK_Restriction
5995 ("subprogram declaration is not a library item", N);
5997 elsif Present (Next (N))
5998 and then Nkind (Next (N)) = N_Pragma
5999 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
6001 -- In SPARK, subprogram declarations are also permitted in
6002 -- declarative parts when immediately followed by a corresponding
6003 -- pragma Import. We only check here that there is some pragma
6008 Check_SPARK_Restriction
6009 ("subprogram declaration is not allowed here", N);
6013 -- Deal with case of protected subprogram. Do not generate protected
6014 -- operation if operation is flagged as eliminated.
6016 if Is_List_Member (N)
6017 and then Present (Parent (List_Containing (N)))
6018 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6019 and then Is_Protected_Type (Scop)
6021 if No (Protected_Body_Subprogram (Subp))
6022 and then not Is_Eliminated (Subp)
6025 Make_Subprogram_Declaration (Loc,
6027 Build_Protected_Sub_Specification
6028 (N, Scop, Unprotected_Mode));
6030 -- The protected subprogram is declared outside of the protected
6031 -- body. Given that the body has frozen all entities so far, we
6032 -- analyze the subprogram and perform freezing actions explicitly.
6033 -- including the generation of an explicit freeze node, to ensure
6034 -- that gigi has the proper order of elaboration.
6035 -- If the body is a subunit, the insertion point is before the
6036 -- stub in the parent.
6038 Prot_Bod := Parent (List_Containing (N));
6040 if Nkind (Parent (Prot_Bod)) = N_Subunit then
6041 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
6044 Insert_Before (Prot_Bod, Prot_Decl);
6045 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
6046 Set_Has_Delayed_Freeze (Prot_Id);
6048 Push_Scope (Scope (Scop));
6049 Analyze (Prot_Decl);
6050 Freeze_Before (N, Prot_Id);
6051 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6053 -- Create protected operation as well. Even though the operation
6054 -- is only accessible within the body, it is possible to make it
6055 -- available outside of the protected object by using 'Access to
6056 -- provide a callback, so build protected version in all cases.
6059 Make_Subprogram_Declaration (Loc,
6061 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
6062 Insert_Before (Prot_Bod, Prot_Decl);
6063 Analyze (Prot_Decl);
6068 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6069 -- cases this is superfluous because calls to it will be automatically
6070 -- inlined, but we definitely need the body if preconditions for the
6071 -- procedure are present.
6073 elsif Nkind (Specification (N)) = N_Procedure_Specification
6074 and then Null_Present (Specification (N))
6077 Bod : constant Node_Id := Body_To_Inline (N);
6080 Set_Has_Completion (Subp, False);
6081 Append_Freeze_Action (Subp, Bod);
6083 -- The body now contains raise statements, so calls to it will
6086 Set_Is_Inlined (Subp, False);
6089 end Expand_N_Subprogram_Declaration;
6091 --------------------------------
6092 -- Expand_Non_Function_Return --
6093 --------------------------------
6095 procedure Expand_Non_Function_Return (N : Node_Id) is
6096 pragma Assert (No (Expression (N)));
6098 Loc : constant Source_Ptr := Sloc (N);
6099 Scope_Id : Entity_Id :=
6100 Return_Applies_To (Return_Statement_Entity (N));
6101 Kind : constant Entity_Kind := Ekind (Scope_Id);
6104 Goto_Stat : Node_Id;
6108 -- Call _Postconditions procedure if procedure with active
6109 -- postconditions. Here, we use the Postcondition_Proc attribute,
6110 -- which is needed for implicitly-generated returns. Functions
6111 -- never have implicitly-generated returns, and there's no
6112 -- room for Postcondition_Proc in E_Function, so we look up the
6113 -- identifier Name_uPostconditions for function returns (see
6114 -- Expand_Simple_Function_Return).
6116 if Ekind (Scope_Id) = E_Procedure
6117 and then Has_Postconditions (Scope_Id)
6119 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
6121 Make_Procedure_Call_Statement (Loc,
6122 Name => New_Reference_To (Postcondition_Proc (Scope_Id), Loc)));
6125 -- If it is a return from a procedure do no extra steps
6127 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6130 -- If it is a nested return within an extended one, replace it with a
6131 -- return of the previously declared return object.
6133 elsif Kind = E_Return_Statement then
6135 Make_Simple_Return_Statement (Loc,
6137 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6138 Set_Comes_From_Extended_Return_Statement (N);
6139 Set_Return_Statement_Entity (N, Scope_Id);
6140 Expand_Simple_Function_Return (N);
6144 pragma Assert (Is_Entry (Scope_Id));
6146 -- Look at the enclosing block to see whether the return is from an
6147 -- accept statement or an entry body.
6149 for J in reverse 0 .. Scope_Stack.Last loop
6150 Scope_Id := Scope_Stack.Table (J).Entity;
6151 exit when Is_Concurrent_Type (Scope_Id);
6154 -- If it is a return from accept statement it is expanded as call to
6155 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6157 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6158 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6160 if Is_Task_Type (Scope_Id) then
6163 Make_Procedure_Call_Statement (Loc,
6164 Name => New_Reference_To (RTE (RE_Complete_Rendezvous), Loc));
6165 Insert_Before (N, Call);
6166 -- why not insert actions here???
6169 Acc_Stat := Parent (N);
6170 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6171 Acc_Stat := Parent (Acc_Stat);
6174 Lab_Node := Last (Statements
6175 (Handled_Statement_Sequence (Acc_Stat)));
6177 Goto_Stat := Make_Goto_Statement (Loc,
6178 Name => New_Occurrence_Of
6179 (Entity (Identifier (Lab_Node)), Loc));
6181 Set_Analyzed (Goto_Stat);
6183 Rewrite (N, Goto_Stat);
6186 -- If it is a return from an entry body, put a Complete_Entry_Body call
6187 -- in front of the return.
6189 elsif Is_Protected_Type (Scope_Id) then
6191 Make_Procedure_Call_Statement (Loc,
6193 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
6194 Parameter_Associations => New_List (
6195 Make_Attribute_Reference (Loc,
6198 (Find_Protection_Object (Current_Scope), Loc),
6199 Attribute_Name => Name_Unchecked_Access)));
6201 Insert_Before (N, Call);
6204 end Expand_Non_Function_Return;
6206 ---------------------------------------
6207 -- Expand_Protected_Object_Reference --
6208 ---------------------------------------
6210 function Expand_Protected_Object_Reference
6212 Scop : Entity_Id) return Node_Id
6214 Loc : constant Source_Ptr := Sloc (N);
6221 Rec := Make_Identifier (Loc, Name_uObject);
6222 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6224 -- Find enclosing protected operation, and retrieve its first parameter,
6225 -- which denotes the enclosing protected object. If the enclosing
6226 -- operation is an entry, we are immediately within the protected body,
6227 -- and we can retrieve the object from the service entries procedure. A
6228 -- barrier function has the same signature as an entry. A barrier
6229 -- function is compiled within the protected object, but unlike
6230 -- protected operations its never needs locks, so that its protected
6231 -- body subprogram points to itself.
6233 Proc := Current_Scope;
6234 while Present (Proc)
6235 and then Scope (Proc) /= Scop
6237 Proc := Scope (Proc);
6240 Corr := Protected_Body_Subprogram (Proc);
6244 -- Previous error left expansion incomplete.
6245 -- Nothing to do on this call.
6252 (First (Parameter_Specifications (Parent (Corr))));
6254 if Is_Subprogram (Proc)
6255 and then Proc /= Corr
6257 -- Protected function or procedure
6259 Set_Entity (Rec, Param);
6261 -- Rec is a reference to an entity which will not be in scope when
6262 -- the call is reanalyzed, and needs no further analysis.
6267 -- Entry or barrier function for entry body. The first parameter of
6268 -- the entry body procedure is pointer to the object. We create a
6269 -- local variable of the proper type, duplicating what is done to
6270 -- define _object later on.
6274 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6278 Make_Full_Type_Declaration (Loc,
6279 Defining_Identifier => Obj_Ptr,
6281 Make_Access_To_Object_Definition (Loc,
6282 Subtype_Indication =>
6284 (Corresponding_Record_Type (Scop), Loc))));
6286 Insert_Actions (N, Decls);
6287 Freeze_Before (N, Obj_Ptr);
6290 Make_Explicit_Dereference (Loc,
6292 Unchecked_Convert_To (Obj_Ptr,
6293 New_Occurrence_Of (Param, Loc)));
6295 -- Analyze new actual. Other actuals in calls are already analyzed
6296 -- and the list of actuals is not reanalyzed after rewriting.
6298 Set_Parent (Rec, N);
6304 end Expand_Protected_Object_Reference;
6306 --------------------------------------
6307 -- Expand_Protected_Subprogram_Call --
6308 --------------------------------------
6310 procedure Expand_Protected_Subprogram_Call
6318 -- If the protected object is not an enclosing scope, this is an inter-
6319 -- object function call. Inter-object procedure calls are expanded by
6320 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6321 -- subprogram being called is in the protected body being compiled, and
6322 -- if the protected object in the call is statically the enclosing type.
6323 -- The object may be an component of some other data structure, in which
6324 -- case this must be handled as an inter-object call.
6326 if not In_Open_Scopes (Scop)
6327 or else not Is_Entity_Name (Name (N))
6329 if Nkind (Name (N)) = N_Selected_Component then
6330 Rec := Prefix (Name (N));
6333 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
6334 Rec := Prefix (Prefix (Name (N)));
6337 Build_Protected_Subprogram_Call (N,
6338 Name => New_Occurrence_Of (Subp, Sloc (N)),
6339 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6343 Rec := Expand_Protected_Object_Reference (N, Scop);
6349 Build_Protected_Subprogram_Call (N,
6356 -- If it is a function call it can appear in elaboration code and
6357 -- the called entity must be frozen here.
6359 if Ekind (Subp) = E_Function then
6360 Freeze_Expression (Name (N));
6363 -- Analyze and resolve the new call. The actuals have already been
6364 -- resolved, but expansion of a function call will add extra actuals
6365 -- if needed. Analysis of a procedure call already includes resolution.
6369 if Ekind (Subp) = E_Function then
6370 Resolve (N, Etype (Subp));
6372 end Expand_Protected_Subprogram_Call;
6374 --------------------------------------------
6375 -- Has_Unconstrained_Access_Discriminants --
6376 --------------------------------------------
6378 function Has_Unconstrained_Access_Discriminants
6379 (Subtyp : Entity_Id) return Boolean
6384 if Has_Discriminants (Subtyp)
6385 and then not Is_Constrained (Subtyp)
6387 Discr := First_Discriminant (Subtyp);
6388 while Present (Discr) loop
6389 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
6393 Next_Discriminant (Discr);
6398 end Has_Unconstrained_Access_Discriminants;
6400 -----------------------------------
6401 -- Expand_Simple_Function_Return --
6402 -----------------------------------
6404 -- The "simple" comes from the syntax rule simple_return_statement. The
6405 -- semantics are not at all simple!
6407 procedure Expand_Simple_Function_Return (N : Node_Id) is
6408 Loc : constant Source_Ptr := Sloc (N);
6410 Scope_Id : constant Entity_Id :=
6411 Return_Applies_To (Return_Statement_Entity (N));
6412 -- The function we are returning from
6414 R_Type : constant Entity_Id := Etype (Scope_Id);
6415 -- The result type of the function
6417 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6419 Exp : constant Node_Id := Expression (N);
6420 pragma Assert (Present (Exp));
6422 Exptyp : constant Entity_Id := Etype (Exp);
6423 -- The type of the expression (not necessarily the same as R_Type)
6425 Subtype_Ind : Node_Id;
6426 -- If the result type of the function is class-wide and the expression
6427 -- has a specific type, then we use the expression's type as the type of
6428 -- the return object. In cases where the expression is an aggregate that
6429 -- is built in place, this avoids the need for an expensive conversion
6430 -- of the return object to the specific type on assignments to the
6431 -- individual components.
6434 if Is_Class_Wide_Type (R_Type)
6435 and then not Is_Class_Wide_Type (Etype (Exp))
6437 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
6439 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6442 -- For the case of a simple return that does not come from an extended
6443 -- return, in the case of Ada 2005 where we are returning a limited
6444 -- type, we rewrite "return <expression>;" to be:
6446 -- return _anon_ : <return_subtype> := <expression>
6448 -- The expansion produced by Expand_N_Extended_Return_Statement will
6449 -- contain simple return statements (for example, a block containing
6450 -- simple return of the return object), which brings us back here with
6451 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6452 -- checking for a simple return that does not come from an extended
6453 -- return is to avoid this infinite recursion.
6455 -- The reason for this design is that for Ada 2005 limited returns, we
6456 -- need to reify the return object, so we can build it "in place", and
6457 -- we need a block statement to hang finalization and tasking stuff.
6459 -- ??? In order to avoid disruption, we avoid translating to extended
6460 -- return except in the cases where we really need to (Ada 2005 for
6461 -- inherently limited). We might prefer to do this translation in all
6462 -- cases (except perhaps for the case of Ada 95 inherently limited),
6463 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6464 -- code. This would also allow us to do the build-in-place optimization
6465 -- for efficiency even in cases where it is semantically not required.
6467 -- As before, we check the type of the return expression rather than the
6468 -- return type of the function, because the latter may be a limited
6469 -- class-wide interface type, which is not a limited type, even though
6470 -- the type of the expression may be.
6472 if not Comes_From_Extended_Return_Statement (N)
6473 and then Is_Immutably_Limited_Type (Etype (Expression (N)))
6474 and then Ada_Version >= Ada_2005
6475 and then not Debug_Flag_Dot_L
6478 Return_Object_Entity : constant Entity_Id :=
6479 Make_Temporary (Loc, 'R', Exp);
6480 Obj_Decl : constant Node_Id :=
6481 Make_Object_Declaration (Loc,
6482 Defining_Identifier => Return_Object_Entity,
6483 Object_Definition => Subtype_Ind,
6486 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
6487 Return_Object_Declarations => New_List (Obj_Decl));
6488 -- Do not perform this high-level optimization if the result type
6489 -- is an interface because the "this" pointer must be displaced.
6498 -- Here we have a simple return statement that is part of the expansion
6499 -- of an extended return statement (either written by the user, or
6500 -- generated by the above code).
6502 -- Always normalize C/Fortran boolean result. This is not always needed,
6503 -- but it seems a good idea to minimize the passing around of non-
6504 -- normalized values, and in any case this handles the processing of
6505 -- barrier functions for protected types, which turn the condition into
6506 -- a return statement.
6508 if Is_Boolean_Type (Exptyp)
6509 and then Nonzero_Is_True (Exptyp)
6511 Adjust_Condition (Exp);
6512 Adjust_Result_Type (Exp, Exptyp);
6515 -- Do validity check if enabled for returns
6517 if Validity_Checks_On
6518 and then Validity_Check_Returns
6523 -- Check the result expression of a scalar function against the subtype
6524 -- of the function by inserting a conversion. This conversion must
6525 -- eventually be performed for other classes of types, but for now it's
6526 -- only done for scalars.
6529 if Is_Scalar_Type (Exptyp) then
6530 Rewrite (Exp, Convert_To (R_Type, Exp));
6532 -- The expression is resolved to ensure that the conversion gets
6533 -- expanded to generate a possible constraint check.
6535 Analyze_And_Resolve (Exp, R_Type);
6538 -- Deal with returning variable length objects and controlled types
6540 -- Nothing to do if we are returning by reference, or this is not a
6541 -- type that requires special processing (indicated by the fact that
6542 -- it requires a cleanup scope for the secondary stack case).
6544 if Is_Immutably_Limited_Type (Exptyp)
6545 or else Is_Limited_Interface (Exptyp)
6549 elsif not Requires_Transient_Scope (R_Type) then
6551 -- Mutable records with no variable length components are not
6552 -- returned on the sec-stack, so we need to make sure that the
6553 -- backend will only copy back the size of the actual value, and not
6554 -- the maximum size. We create an actual subtype for this purpose.
6557 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6561 if Has_Discriminants (Ubt)
6562 and then not Is_Constrained (Ubt)
6563 and then not Has_Unchecked_Union (Ubt)
6565 Decl := Build_Actual_Subtype (Ubt, Exp);
6566 Ent := Defining_Identifier (Decl);
6567 Insert_Action (Exp, Decl);
6568 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6569 Analyze_And_Resolve (Exp);
6573 -- Here if secondary stack is used
6576 -- Make sure that no surrounding block will reclaim the secondary
6577 -- stack on which we are going to put the result. Not only may this
6578 -- introduce secondary stack leaks but worse, if the reclamation is
6579 -- done too early, then the result we are returning may get
6586 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
6587 Set_Sec_Stack_Needed_For_Return (S, True);
6588 S := Enclosing_Dynamic_Scope (S);
6592 -- Optimize the case where the result is a function call. In this
6593 -- case either the result is already on the secondary stack, or is
6594 -- already being returned with the stack pointer depressed and no
6595 -- further processing is required except to set the By_Ref flag
6596 -- to ensure that gigi does not attempt an extra unnecessary copy.
6597 -- (actually not just unnecessary but harmfully wrong in the case
6598 -- of a controlled type, where gigi does not know how to do a copy).
6599 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6600 -- for array types if the constrained status of the target type is
6601 -- different from that of the expression.
6603 if Requires_Transient_Scope (Exptyp)
6605 (not Is_Array_Type (Exptyp)
6606 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6607 or else CW_Or_Has_Controlled_Part (Utyp))
6608 and then Nkind (Exp) = N_Function_Call
6612 -- Remove side effects from the expression now so that other parts
6613 -- of the expander do not have to reanalyze this node without this
6616 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6618 -- For controlled types, do the allocation on the secondary stack
6619 -- manually in order to call adjust at the right time:
6621 -- type Anon1 is access R_Type;
6622 -- for Anon1'Storage_pool use ss_pool;
6623 -- Anon2 : anon1 := new R_Type'(expr);
6624 -- return Anon2.all;
6626 -- We do the same for classwide types that are not potentially
6627 -- controlled (by the virtue of restriction No_Finalization) because
6628 -- gigi is not able to properly allocate class-wide types.
6630 elsif CW_Or_Has_Controlled_Part (Utyp) then
6632 Loc : constant Source_Ptr := Sloc (N);
6633 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6634 Alloc_Node : Node_Id;
6638 Set_Ekind (Acc_Typ, E_Access_Type);
6640 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6642 -- This is an allocator for the secondary stack, and it's fine
6643 -- to have Comes_From_Source set False on it, as gigi knows not
6644 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6647 Make_Allocator (Loc,
6649 Make_Qualified_Expression (Loc,
6650 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
6651 Expression => Relocate_Node (Exp)));
6653 -- We do not want discriminant checks on the declaration,
6654 -- given that it gets its value from the allocator.
6656 Set_No_Initialization (Alloc_Node);
6658 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6660 Insert_List_Before_And_Analyze (N, New_List (
6661 Make_Full_Type_Declaration (Loc,
6662 Defining_Identifier => Acc_Typ,
6664 Make_Access_To_Object_Definition (Loc,
6665 Subtype_Indication => Subtype_Ind)),
6667 Make_Object_Declaration (Loc,
6668 Defining_Identifier => Temp,
6669 Object_Definition => New_Reference_To (Acc_Typ, Loc),
6670 Expression => Alloc_Node)));
6673 Make_Explicit_Dereference (Loc,
6674 Prefix => New_Reference_To (Temp, Loc)));
6676 Analyze_And_Resolve (Exp, R_Type);
6679 -- Otherwise use the gigi mechanism to allocate result on the
6683 Check_Restriction (No_Secondary_Stack, N);
6684 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6686 -- If we are generating code for the VM do not use
6687 -- SS_Allocate since everything is heap-allocated anyway.
6689 if VM_Target = No_VM then
6690 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6695 -- Implement the rules of 6.5(8-10), which require a tag check in
6696 -- the case of a limited tagged return type, and tag reassignment for
6697 -- nonlimited tagged results. These actions are needed when the return
6698 -- type is a specific tagged type and the result expression is a
6699 -- conversion or a formal parameter, because in that case the tag of
6700 -- the expression might differ from the tag of the specific result type.
6702 if Is_Tagged_Type (Utyp)
6703 and then not Is_Class_Wide_Type (Utyp)
6704 and then (Nkind_In (Exp, N_Type_Conversion,
6705 N_Unchecked_Type_Conversion)
6706 or else (Is_Entity_Name (Exp)
6707 and then Ekind (Entity (Exp)) in Formal_Kind))
6709 -- When the return type is limited, perform a check that the tag of
6710 -- the result is the same as the tag of the return type.
6712 if Is_Limited_Type (R_Type) then
6714 Make_Raise_Constraint_Error (Loc,
6718 Make_Selected_Component (Loc,
6719 Prefix => Duplicate_Subexpr (Exp),
6720 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6722 Make_Attribute_Reference (Loc,
6724 New_Occurrence_Of (Base_Type (Utyp), Loc),
6725 Attribute_Name => Name_Tag)),
6726 Reason => CE_Tag_Check_Failed));
6728 -- If the result type is a specific nonlimited tagged type, then we
6729 -- have to ensure that the tag of the result is that of the result
6730 -- type. This is handled by making a copy of the expression in
6731 -- the case where it might have a different tag, namely when the
6732 -- expression is a conversion or a formal parameter. We create a new
6733 -- object of the result type and initialize it from the expression,
6734 -- which will implicitly force the tag to be set appropriately.
6738 ExpR : constant Node_Id := Relocate_Node (Exp);
6739 Result_Id : constant Entity_Id :=
6740 Make_Temporary (Loc, 'R', ExpR);
6741 Result_Exp : constant Node_Id :=
6742 New_Reference_To (Result_Id, Loc);
6743 Result_Obj : constant Node_Id :=
6744 Make_Object_Declaration (Loc,
6745 Defining_Identifier => Result_Id,
6746 Object_Definition =>
6747 New_Reference_To (R_Type, Loc),
6748 Constant_Present => True,
6749 Expression => ExpR);
6752 Set_Assignment_OK (Result_Obj);
6753 Insert_Action (Exp, Result_Obj);
6755 Rewrite (Exp, Result_Exp);
6756 Analyze_And_Resolve (Exp, R_Type);
6760 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6761 -- a check that the level of the return expression's underlying type
6762 -- is not deeper than the level of the master enclosing the function.
6763 -- Always generate the check when the type of the return expression
6764 -- is class-wide, when it's a type conversion, or when it's a formal
6765 -- parameter. Otherwise, suppress the check in the case where the
6766 -- return expression has a specific type whose level is known not to
6767 -- be statically deeper than the function's result type.
6769 -- Note: accessibility check is skipped in the VM case, since there
6770 -- does not seem to be any practical way to implement this check.
6772 elsif Ada_Version >= Ada_2005
6773 and then Tagged_Type_Expansion
6774 and then Is_Class_Wide_Type (R_Type)
6775 and then not Scope_Suppress (Accessibility_Check)
6777 (Is_Class_Wide_Type (Etype (Exp))
6778 or else Nkind_In (Exp, N_Type_Conversion,
6779 N_Unchecked_Type_Conversion)
6780 or else (Is_Entity_Name (Exp)
6781 and then Ekind (Entity (Exp)) in Formal_Kind)
6782 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6783 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6789 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6790 -- "this" to reference the base of the object. This is required to
6791 -- get access to the TSD of the object.
6793 if Is_Class_Wide_Type (Etype (Exp))
6794 and then Is_Interface (Etype (Exp))
6795 and then Nkind (Exp) = N_Explicit_Dereference
6798 Make_Explicit_Dereference (Loc,
6800 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6801 Make_Function_Call (Loc,
6803 New_Reference_To (RTE (RE_Base_Address), Loc),
6804 Parameter_Associations => New_List (
6805 Unchecked_Convert_To (RTE (RE_Address),
6806 Duplicate_Subexpr (Prefix (Exp)))))));
6809 Make_Attribute_Reference (Loc,
6810 Prefix => Duplicate_Subexpr (Exp),
6811 Attribute_Name => Name_Tag);
6815 Make_Raise_Program_Error (Loc,
6818 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6820 Make_Integer_Literal (Loc,
6821 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6822 Reason => PE_Accessibility_Check_Failed));
6825 -- AI05-0073: If function has a controlling access result, check that
6826 -- the tag of the return value, if it is not null, matches designated
6827 -- type of return type.
6828 -- The return expression is referenced twice in the code below, so
6829 -- it must be made free of side effects. Given that different compilers
6830 -- may evaluate these parameters in different order, both occurrences
6833 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6834 and then Has_Controlling_Result (Scope_Id)
6837 Make_Raise_Constraint_Error (Loc,
6842 Left_Opnd => Duplicate_Subexpr (Exp),
6843 Right_Opnd => Make_Null (Loc)),
6845 Right_Opnd => Make_Op_Ne (Loc,
6847 Make_Selected_Component (Loc,
6848 Prefix => Duplicate_Subexpr (Exp),
6849 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6852 Make_Attribute_Reference (Loc,
6854 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6855 Attribute_Name => Name_Tag))),
6857 Reason => CE_Tag_Check_Failed),
6858 Suppress => All_Checks);
6861 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6862 -- ensure that the function result does not outlive an
6863 -- object designated by one of it discriminants.
6865 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6866 and then Has_Unconstrained_Access_Discriminants (R_Type)
6869 Discrim_Source : Node_Id;
6871 procedure Check_Against_Result_Level (Level : Node_Id);
6872 -- Check the given accessibility level against the level
6873 -- determined by the point of call. (AI05-0234).
6875 --------------------------------
6876 -- Check_Against_Result_Level --
6877 --------------------------------
6879 procedure Check_Against_Result_Level (Level : Node_Id) is
6882 Make_Raise_Program_Error (Loc,
6888 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6889 Reason => PE_Accessibility_Check_Failed));
6890 end Check_Against_Result_Level;
6893 Discrim_Source := Exp;
6894 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6895 Discrim_Source := Expression (Discrim_Source);
6898 if Nkind (Discrim_Source) = N_Identifier
6899 and then Is_Return_Object (Entity (Discrim_Source))
6901 Discrim_Source := Entity (Discrim_Source);
6903 if Is_Constrained (Etype (Discrim_Source)) then
6904 Discrim_Source := Etype (Discrim_Source);
6906 Discrim_Source := Expression (Parent (Discrim_Source));
6909 elsif Nkind (Discrim_Source) = N_Identifier
6910 and then Nkind_In (Original_Node (Discrim_Source),
6911 N_Aggregate, N_Extension_Aggregate)
6913 Discrim_Source := Original_Node (Discrim_Source);
6915 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6916 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6918 Discrim_Source := Original_Node (Discrim_Source);
6921 while Nkind_In (Discrim_Source, N_Qualified_Expression,
6923 N_Unchecked_Type_Conversion)
6925 Discrim_Source := Expression (Discrim_Source);
6928 case Nkind (Discrim_Source) is
6929 when N_Defining_Identifier =>
6931 pragma Assert (Is_Composite_Type (Discrim_Source)
6932 and then Has_Discriminants (Discrim_Source)
6933 and then Is_Constrained (Discrim_Source));
6936 Discrim : Entity_Id :=
6937 First_Discriminant (Base_Type (R_Type));
6938 Disc_Elmt : Elmt_Id :=
6939 First_Elmt (Discriminant_Constraint
6943 if Ekind (Etype (Discrim)) =
6944 E_Anonymous_Access_Type
6946 Check_Against_Result_Level
6947 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
6950 Next_Elmt (Disc_Elmt);
6951 Next_Discriminant (Discrim);
6952 exit when not Present (Discrim);
6956 when N_Aggregate | N_Extension_Aggregate =>
6958 -- Unimplemented: extension aggregate case where discrims
6959 -- come from ancestor part, not extension part.
6962 Discrim : Entity_Id :=
6963 First_Discriminant (Base_Type (R_Type));
6965 Disc_Exp : Node_Id := Empty;
6967 Positionals_Exhausted
6968 : Boolean := not Present (Expressions
6971 function Associated_Expr
6972 (Comp_Id : Entity_Id;
6973 Associations : List_Id) return Node_Id;
6975 -- Given a component and a component associations list,
6976 -- locate the expression for that component; returns
6977 -- Empty if no such expression is found.
6979 ---------------------
6980 -- Associated_Expr --
6981 ---------------------
6983 function Associated_Expr
6984 (Comp_Id : Entity_Id;
6985 Associations : List_Id) return Node_Id
6991 -- Simple linear search seems ok here
6993 Assoc := First (Associations);
6994 while Present (Assoc) loop
6995 Choice := First (Choices (Assoc));
6996 while Present (Choice) loop
6997 if (Nkind (Choice) = N_Identifier
6998 and then Chars (Choice) = Chars (Comp_Id))
6999 or else (Nkind (Choice) = N_Others_Choice)
7001 return Expression (Assoc);
7011 end Associated_Expr;
7013 -- Start of processing for Expand_Simple_Function_Return
7016 if not Positionals_Exhausted then
7017 Disc_Exp := First (Expressions (Discrim_Source));
7021 if Positionals_Exhausted then
7025 Component_Associations (Discrim_Source));
7028 if Ekind (Etype (Discrim)) =
7029 E_Anonymous_Access_Type
7031 Check_Against_Result_Level
7032 (Dynamic_Accessibility_Level (Disc_Exp));
7035 Next_Discriminant (Discrim);
7036 exit when not Present (Discrim);
7038 if not Positionals_Exhausted then
7040 Positionals_Exhausted := not Present (Disc_Exp);
7045 when N_Function_Call =>
7047 -- No check needed (check performed by callee)
7054 Level : constant Node_Id :=
7055 Make_Integer_Literal (Loc,
7056 Object_Access_Level (Discrim_Source));
7059 -- Unimplemented: check for name prefix that includes
7060 -- a dereference of an access value with a dynamic
7061 -- accessibility level (e.g., an access param or a
7062 -- saooaaat) and use dynamic level in that case. For
7064 -- return Access_Param.all(Some_Index).Some_Component;
7067 Set_Etype (Level, Standard_Natural);
7068 Check_Against_Result_Level (Level);
7075 -- If we are returning an object that may not be bit-aligned, then copy
7076 -- the value into a temporary first. This copy may need to expand to a
7077 -- loop of component operations.
7079 if Is_Possibly_Unaligned_Slice (Exp)
7080 or else Is_Possibly_Unaligned_Object (Exp)
7083 ExpR : constant Node_Id := Relocate_Node (Exp);
7084 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7087 Make_Object_Declaration (Loc,
7088 Defining_Identifier => Tnn,
7089 Constant_Present => True,
7090 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7091 Expression => ExpR),
7092 Suppress => All_Checks);
7093 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7097 -- Generate call to postcondition checks if they are present
7099 if Ekind (Scope_Id) = E_Function
7100 and then Has_Postconditions (Scope_Id)
7102 -- We are going to reference the returned value twice in this case,
7103 -- once in the call to _Postconditions, and once in the actual return
7104 -- statement, but we can't have side effects happening twice, and in
7105 -- any case for efficiency we don't want to do the computation twice.
7107 -- If the returned expression is an entity name, we don't need to
7108 -- worry since it is efficient and safe to reference it twice, that's
7109 -- also true for literals other than string literals, and for the
7110 -- case of X.all where X is an entity name.
7112 if Is_Entity_Name (Exp)
7113 or else Nkind_In (Exp, N_Character_Literal,
7116 or else (Nkind (Exp) = N_Explicit_Dereference
7117 and then Is_Entity_Name (Prefix (Exp)))
7121 -- Otherwise we are going to need a temporary to capture the value
7125 ExpR : constant Node_Id := Relocate_Node (Exp);
7126 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7129 -- For a complex expression of an elementary type, capture
7130 -- value in the temporary and use it as the reference.
7132 if Is_Elementary_Type (R_Type) then
7134 Make_Object_Declaration (Loc,
7135 Defining_Identifier => Tnn,
7136 Constant_Present => True,
7137 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7138 Expression => ExpR),
7139 Suppress => All_Checks);
7141 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7143 -- If we have something we can rename, generate a renaming of
7144 -- the object and replace the expression with a reference
7146 elsif Is_Object_Reference (Exp) then
7148 Make_Object_Renaming_Declaration (Loc,
7149 Defining_Identifier => Tnn,
7150 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
7152 Suppress => All_Checks);
7154 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7156 -- Otherwise we have something like a string literal or an
7157 -- aggregate. We could copy the value, but that would be
7158 -- inefficient. Instead we make a reference to the value and
7159 -- capture this reference with a renaming, the expression is
7160 -- then replaced by a dereference of this renaming.
7163 -- For now, copy the value, since the code below does not
7164 -- seem to work correctly ???
7167 Make_Object_Declaration (Loc,
7168 Defining_Identifier => Tnn,
7169 Constant_Present => True,
7170 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7171 Expression => Relocate_Node (Exp)),
7172 Suppress => All_Checks);
7174 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7176 -- Insert_Action (Exp,
7177 -- Make_Object_Renaming_Declaration (Loc,
7178 -- Defining_Identifier => Tnn,
7179 -- Access_Definition =>
7180 -- Make_Access_Definition (Loc,
7181 -- All_Present => True,
7182 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
7184 -- Make_Reference (Loc,
7185 -- Prefix => Relocate_Node (Exp))),
7186 -- Suppress => All_Checks);
7189 -- Make_Explicit_Dereference (Loc,
7190 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
7195 -- Generate call to _postconditions
7198 Make_Procedure_Call_Statement (Loc,
7199 Name => Make_Identifier (Loc, Name_uPostconditions),
7200 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
7203 -- Ada 2005 (AI-251): If this return statement corresponds with an
7204 -- simple return statement associated with an extended return statement
7205 -- and the type of the returned object is an interface then generate an
7206 -- implicit conversion to force displacement of the "this" pointer.
7208 if Ada_Version >= Ada_2005
7209 and then Comes_From_Extended_Return_Statement (N)
7210 and then Nkind (Expression (N)) = N_Identifier
7211 and then Is_Interface (Utyp)
7212 and then Utyp /= Underlying_Type (Exptyp)
7214 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7215 Analyze_And_Resolve (Exp);
7217 end Expand_Simple_Function_Return;
7219 --------------------------------
7220 -- Is_Build_In_Place_Function --
7221 --------------------------------
7223 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7225 -- This function is called from Expand_Subtype_From_Expr during
7226 -- semantic analysis, even when expansion is off. In those cases
7227 -- the build_in_place expansion will not take place.
7229 if not Expander_Active then
7233 -- For now we test whether E denotes a function or access-to-function
7234 -- type whose result subtype is inherently limited. Later this test may
7235 -- be revised to allow composite nonlimited types. Functions with a
7236 -- foreign convention or whose result type has a foreign convention
7239 if Ekind_In (E, E_Function, E_Generic_Function)
7240 or else (Ekind (E) = E_Subprogram_Type
7241 and then Etype (E) /= Standard_Void_Type)
7243 -- Note: If you have Convention (C) on an inherently limited type,
7244 -- you're on your own. That is, the C code will have to be carefully
7245 -- written to know about the Ada conventions.
7247 if Has_Foreign_Convention (E)
7248 or else Has_Foreign_Convention (Etype (E))
7252 -- In Ada 2005 all functions with an inherently limited return type
7253 -- must be handled using a build-in-place profile, including the case
7254 -- of a function with a limited interface result, where the function
7255 -- may return objects of nonlimited descendants.
7258 return Is_Immutably_Limited_Type (Etype (E))
7259 and then Ada_Version >= Ada_2005
7260 and then not Debug_Flag_Dot_L;
7266 end Is_Build_In_Place_Function;
7268 -------------------------------------
7269 -- Is_Build_In_Place_Function_Call --
7270 -------------------------------------
7272 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7273 Exp_Node : Node_Id := N;
7274 Function_Id : Entity_Id;
7277 -- Return False when the expander is inactive, since awareness of
7278 -- build-in-place treatment is only relevant during expansion. Note that
7279 -- Is_Build_In_Place_Function, which is called as part of this function,
7280 -- is also conditioned this way, but we need to check here as well to
7281 -- avoid blowing up on processing protected calls when expansion is
7282 -- disabled (such as with -gnatc) since those would trip over the raise
7283 -- of Program_Error below.
7285 if not Expander_Active then
7289 -- Step past qualification or unchecked conversion (the latter can occur
7290 -- in cases of calls to 'Input).
7292 if Nkind_In (Exp_Node, N_Qualified_Expression,
7293 N_Unchecked_Type_Conversion)
7295 Exp_Node := Expression (N);
7298 if Nkind (Exp_Node) /= N_Function_Call then
7302 -- In Alfa mode, build-in-place calls are not expanded, so that we
7303 -- may end up with a call that is neither resolved to an entity, nor
7304 -- an indirect call.
7309 elsif Is_Entity_Name (Name (Exp_Node)) then
7310 Function_Id := Entity (Name (Exp_Node));
7312 -- In the case of an explicitly dereferenced call, use the subprogram
7313 -- type generated for the dereference.
7315 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7316 Function_Id := Etype (Name (Exp_Node));
7319 raise Program_Error;
7322 return Is_Build_In_Place_Function (Function_Id);
7324 end Is_Build_In_Place_Function_Call;
7326 -----------------------
7327 -- Freeze_Subprogram --
7328 -----------------------
7330 procedure Freeze_Subprogram (N : Node_Id) is
7331 Loc : constant Source_Ptr := Sloc (N);
7333 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7334 -- (Ada 2005): Register a predefined primitive in all the secondary
7335 -- dispatch tables of its primitive type.
7337 ----------------------------------
7338 -- Register_Predefined_DT_Entry --
7339 ----------------------------------
7341 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7342 Iface_DT_Ptr : Elmt_Id;
7343 Tagged_Typ : Entity_Id;
7344 Thunk_Id : Entity_Id;
7345 Thunk_Code : Node_Id;
7348 Tagged_Typ := Find_Dispatching_Type (Prim);
7350 if No (Access_Disp_Table (Tagged_Typ))
7351 or else not Has_Interfaces (Tagged_Typ)
7352 or else not RTE_Available (RE_Interface_Tag)
7353 or else Restriction_Active (No_Dispatching_Calls)
7358 -- Skip the first two access-to-dispatch-table pointers since they
7359 -- leads to the primary dispatch table (predefined DT and user
7360 -- defined DT). We are only concerned with the secondary dispatch
7361 -- table pointers. Note that the access-to- dispatch-table pointer
7362 -- corresponds to the first implemented interface retrieved below.
7365 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7367 while Present (Iface_DT_Ptr)
7368 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7370 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7371 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7373 if Present (Thunk_Code) then
7374 Insert_Actions_After (N, New_List (
7377 Build_Set_Predefined_Prim_Op_Address (Loc,
7379 New_Reference_To (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7380 Position => DT_Position (Prim),
7382 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7383 Make_Attribute_Reference (Loc,
7384 Prefix => New_Reference_To (Thunk_Id, Loc),
7385 Attribute_Name => Name_Unrestricted_Access))),
7387 Build_Set_Predefined_Prim_Op_Address (Loc,
7390 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7392 Position => DT_Position (Prim),
7394 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7395 Make_Attribute_Reference (Loc,
7396 Prefix => New_Reference_To (Prim, Loc),
7397 Attribute_Name => Name_Unrestricted_Access)))));
7400 -- Skip the tag of the predefined primitives dispatch table
7402 Next_Elmt (Iface_DT_Ptr);
7403 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7405 -- Skip tag of the no-thunks dispatch table
7407 Next_Elmt (Iface_DT_Ptr);
7408 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7410 -- Skip tag of predefined primitives no-thunks dispatch table
7412 Next_Elmt (Iface_DT_Ptr);
7413 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7415 Next_Elmt (Iface_DT_Ptr);
7417 end Register_Predefined_DT_Entry;
7421 Subp : constant Entity_Id := Entity (N);
7423 -- Start of processing for Freeze_Subprogram
7426 -- We suppress the initialization of the dispatch table entry when
7427 -- VM_Target because the dispatching mechanism is handled internally
7430 if Is_Dispatching_Operation (Subp)
7431 and then not Is_Abstract_Subprogram (Subp)
7432 and then Present (DTC_Entity (Subp))
7433 and then Present (Scope (DTC_Entity (Subp)))
7434 and then Tagged_Type_Expansion
7435 and then not Restriction_Active (No_Dispatching_Calls)
7436 and then RTE_Available (RE_Tag)
7439 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7442 -- Handle private overridden primitives
7444 if not Is_CPP_Class (Typ) then
7445 Check_Overriding_Operation (Subp);
7448 -- We assume that imported CPP primitives correspond with objects
7449 -- whose constructor is in the CPP side; therefore we don't need
7450 -- to generate code to register them in the dispatch table.
7452 if Is_CPP_Class (Typ) then
7455 -- Handle CPP primitives found in derivations of CPP_Class types.
7456 -- These primitives must have been inherited from some parent, and
7457 -- there is no need to register them in the dispatch table because
7458 -- Build_Inherit_Prims takes care of the initialization of these
7461 elsif Is_Imported (Subp)
7462 and then (Convention (Subp) = Convention_CPP
7463 or else Convention (Subp) = Convention_C)
7467 -- Generate code to register the primitive in non statically
7468 -- allocated dispatch tables
7470 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7472 -- When a primitive is frozen, enter its name in its dispatch
7475 if not Is_Interface (Typ)
7476 or else Present (Interface_Alias (Subp))
7478 if Is_Predefined_Dispatching_Operation (Subp) then
7479 Register_Predefined_DT_Entry (Subp);
7482 Insert_Actions_After (N,
7483 Register_Primitive (Loc, Prim => Subp));
7489 -- Mark functions that return by reference. Note that it cannot be part
7490 -- of the normal semantic analysis of the spec since the underlying
7491 -- returned type may not be known yet (for private types).
7494 Typ : constant Entity_Id := Etype (Subp);
7495 Utyp : constant Entity_Id := Underlying_Type (Typ);
7497 if Is_Immutably_Limited_Type (Typ) then
7498 Set_Returns_By_Ref (Subp);
7499 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7500 Set_Returns_By_Ref (Subp);
7503 end Freeze_Subprogram;
7505 -----------------------
7506 -- Is_Null_Procedure --
7507 -----------------------
7509 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
7510 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
7513 if Ekind (Subp) /= E_Procedure then
7516 -- Check if this is a declared null procedure
7518 elsif Nkind (Decl) = N_Subprogram_Declaration then
7519 if not Null_Present (Specification (Decl)) then
7522 elsif No (Body_To_Inline (Decl)) then
7525 -- Check if the body contains only a null statement, followed by
7526 -- the return statement added during expansion.
7530 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
7536 if Nkind (Orig_Bod) /= N_Subprogram_Body then
7539 -- We must skip SCIL nodes because they are currently
7540 -- implemented as special N_Null_Statement nodes.
7544 (Statements (Handled_Statement_Sequence (Orig_Bod)));
7545 Stat2 := Next_Non_SCIL_Node (Stat);
7548 Is_Empty_List (Declarations (Orig_Bod))
7549 and then Nkind (Stat) = N_Null_Statement
7553 (Nkind (Stat2) = N_Simple_Return_Statement
7554 and then No (Next (Stat2))));
7562 end Is_Null_Procedure;
7564 -------------------------------------------
7565 -- Make_Build_In_Place_Call_In_Allocator --
7566 -------------------------------------------
7568 procedure Make_Build_In_Place_Call_In_Allocator
7569 (Allocator : Node_Id;
7570 Function_Call : Node_Id)
7572 Acc_Type : constant Entity_Id := Etype (Allocator);
7574 Func_Call : Node_Id := Function_Call;
7575 Function_Id : Entity_Id;
7576 Result_Subt : Entity_Id;
7577 New_Allocator : Node_Id;
7578 Return_Obj_Access : Entity_Id;
7581 -- Step past qualification or unchecked conversion (the latter can occur
7582 -- in cases of calls to 'Input).
7584 if Nkind_In (Func_Call,
7585 N_Qualified_Expression,
7586 N_Unchecked_Type_Conversion)
7588 Func_Call := Expression (Func_Call);
7591 -- If the call has already been processed to add build-in-place actuals
7592 -- then return. This should not normally occur in an allocator context,
7593 -- but we add the protection as a defensive measure.
7595 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7599 -- Mark the call as processed as a build-in-place call
7601 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7603 Loc := Sloc (Function_Call);
7605 if Is_Entity_Name (Name (Func_Call)) then
7606 Function_Id := Entity (Name (Func_Call));
7608 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7609 Function_Id := Etype (Name (Func_Call));
7612 raise Program_Error;
7615 Result_Subt := Available_View (Etype (Function_Id));
7617 -- Check whether return type includes tasks. This may not have been done
7618 -- previously, if the type was a limited view.
7620 if Has_Task (Result_Subt) then
7621 Build_Activation_Chain_Entity (Allocator);
7624 -- When the result subtype is constrained, the return object must be
7625 -- allocated on the caller side, and access to it is passed to the
7628 -- Here and in related routines, we must examine the full view of the
7629 -- type, because the view at the point of call may differ from that
7630 -- that in the function body, and the expansion mechanism depends on
7631 -- the characteristics of the full view.
7633 if Is_Constrained (Underlying_Type (Result_Subt)) then
7635 -- Replace the initialized allocator of form "new T'(Func (...))"
7636 -- with an uninitialized allocator of form "new T", where T is the
7637 -- result subtype of the called function. The call to the function
7638 -- is handled separately further below.
7641 Make_Allocator (Loc,
7642 Expression => New_Reference_To (Result_Subt, Loc));
7643 Set_No_Initialization (New_Allocator);
7645 -- Copy attributes to new allocator. Note that the new allocator
7646 -- logically comes from source if the original one did, so copy the
7647 -- relevant flag. This ensures proper treatment of the restriction
7648 -- No_Implicit_Heap_Allocations in this case.
7650 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
7651 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
7652 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
7654 Rewrite (Allocator, New_Allocator);
7656 -- Create a new access object and initialize it to the result of the
7657 -- new uninitialized allocator. Note: we do not use Allocator as the
7658 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
7659 -- as this would create a sort of infinite "recursion".
7661 Return_Obj_Access := Make_Temporary (Loc, 'R');
7662 Set_Etype (Return_Obj_Access, Acc_Type);
7664 Insert_Action (Allocator,
7665 Make_Object_Declaration (Loc,
7666 Defining_Identifier => Return_Obj_Access,
7667 Object_Definition => New_Reference_To (Acc_Type, Loc),
7668 Expression => Relocate_Node (Allocator)));
7670 -- When the function has a controlling result, an allocation-form
7671 -- parameter must be passed indicating that the caller is allocating
7672 -- the result object. This is needed because such a function can be
7673 -- called as a dispatching operation and must be treated similarly
7674 -- to functions with unconstrained result subtypes.
7676 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7677 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7679 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7680 (Func_Call, Function_Id, Acc_Type);
7682 Add_Task_Actuals_To_Build_In_Place_Call
7683 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
7685 -- Add an implicit actual to the function call that provides access
7686 -- to the allocated object. An unchecked conversion to the (specific)
7687 -- result subtype of the function is inserted to handle cases where
7688 -- the access type of the allocator has a class-wide designated type.
7690 Add_Access_Actual_To_Build_In_Place_Call
7693 Make_Unchecked_Type_Conversion (Loc,
7694 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
7696 Make_Explicit_Dereference (Loc,
7697 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
7699 -- When the result subtype is unconstrained, the function itself must
7700 -- perform the allocation of the return object, so we pass parameters
7701 -- indicating that. We don't yet handle the case where the allocation
7702 -- must be done in a user-defined storage pool, which will require
7703 -- passing another actual or two to provide allocation/deallocation
7707 -- Case of a user-defined storage pool. Pass an allocation parameter
7708 -- indicating that the function should allocate its result in the
7709 -- pool, and pass the pool. Use 'Unrestricted_Access because the
7710 -- pool may not be aliased.
7712 if VM_Target = No_VM
7713 and then Present (Associated_Storage_Pool (Acc_Type))
7715 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7716 (Func_Call, Function_Id, Alloc_Form => User_Storage_Pool,
7718 Make_Attribute_Reference (Loc,
7721 (Associated_Storage_Pool (Acc_Type), Loc),
7722 Attribute_Name => Name_Unrestricted_Access));
7724 -- No user-defined pool; pass an allocation parameter indicating that
7725 -- the function should allocate its result on the heap.
7728 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7729 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
7732 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7733 (Func_Call, Function_Id, Acc_Type);
7735 Add_Task_Actuals_To_Build_In_Place_Call
7736 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
7738 -- The caller does not provide the return object in this case, so we
7739 -- have to pass null for the object access actual.
7741 Add_Access_Actual_To_Build_In_Place_Call
7742 (Func_Call, Function_Id, Return_Object => Empty);
7745 -- If the build-in-place function call returns a controlled object,
7746 -- the finalization master will require a reference to routine
7747 -- Finalize_Address of the designated type. Setting this attribute
7748 -- is done in the same manner to expansion of allocators.
7750 if Needs_Finalization (Result_Subt) then
7752 -- Controlled types with supressed finalization do not need to
7753 -- associate the address of their Finalize_Address primitives with
7754 -- a master since they do not need a master to begin with.
7756 if Is_Library_Level_Entity (Acc_Type)
7757 and then Finalize_Storage_Only (Result_Subt)
7761 -- Do not generate the call to Set_Finalize_Address in Alfa mode
7762 -- because it is not necessary and results in unwanted expansion.
7763 -- This expansion is also not carried out in CodePeer mode because
7764 -- Finalize_Address is never built.
7767 and then not CodePeer_Mode
7769 Insert_Action (Allocator,
7770 Make_Set_Finalize_Address_Call (Loc,
7771 Typ => Etype (Function_Id),
7772 Ptr_Typ => Acc_Type));
7776 -- Finally, replace the allocator node with a reference to the result
7777 -- of the function call itself (which will effectively be an access
7778 -- to the object created by the allocator).
7780 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
7781 Analyze_And_Resolve (Allocator, Acc_Type);
7782 end Make_Build_In_Place_Call_In_Allocator;
7784 ---------------------------------------------------
7785 -- Make_Build_In_Place_Call_In_Anonymous_Context --
7786 ---------------------------------------------------
7788 procedure Make_Build_In_Place_Call_In_Anonymous_Context
7789 (Function_Call : Node_Id)
7792 Func_Call : Node_Id := Function_Call;
7793 Function_Id : Entity_Id;
7794 Result_Subt : Entity_Id;
7795 Return_Obj_Id : Entity_Id;
7796 Return_Obj_Decl : Entity_Id;
7799 -- Step past qualification or unchecked conversion (the latter can occur
7800 -- in cases of calls to 'Input).
7802 if Nkind_In (Func_Call, N_Qualified_Expression,
7803 N_Unchecked_Type_Conversion)
7805 Func_Call := Expression (Func_Call);
7808 -- If the call has already been processed to add build-in-place actuals
7809 -- then return. One place this can occur is for calls to build-in-place
7810 -- functions that occur within a call to a protected operation, where
7811 -- due to rewriting and expansion of the protected call there can be
7812 -- more than one call to Expand_Actuals for the same set of actuals.
7814 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7818 -- Mark the call as processed as a build-in-place call
7820 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7822 Loc := Sloc (Function_Call);
7824 if Is_Entity_Name (Name (Func_Call)) then
7825 Function_Id := Entity (Name (Func_Call));
7827 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7828 Function_Id := Etype (Name (Func_Call));
7831 raise Program_Error;
7834 Result_Subt := Etype (Function_Id);
7836 -- If the build-in-place function returns a controlled object, then the
7837 -- object needs to be finalized immediately after the context. Since
7838 -- this case produces a transient scope, the servicing finalizer needs
7839 -- to name the returned object. Create a temporary which is initialized
7840 -- with the function call:
7842 -- Temp_Id : Func_Type := BIP_Func_Call;
7844 -- The initialization expression of the temporary will be rewritten by
7845 -- the expander using the appropriate mechanism in Make_Build_In_Place_
7846 -- Call_In_Object_Declaration.
7848 if Needs_Finalization (Result_Subt) then
7850 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
7851 Temp_Decl : Node_Id;
7854 -- Reset the guard on the function call since the following does
7855 -- not perform actual call expansion.
7857 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
7860 Make_Object_Declaration (Loc,
7861 Defining_Identifier => Temp_Id,
7862 Object_Definition =>
7863 New_Reference_To (Result_Subt, Loc),
7865 New_Copy_Tree (Function_Call));
7867 Insert_Action (Function_Call, Temp_Decl);
7869 Rewrite (Function_Call, New_Reference_To (Temp_Id, Loc));
7870 Analyze (Function_Call);
7873 -- When the result subtype is constrained, an object of the subtype is
7874 -- declared and an access value designating it is passed as an actual.
7876 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
7878 -- Create a temporary object to hold the function result
7880 Return_Obj_Id := Make_Temporary (Loc, 'R');
7881 Set_Etype (Return_Obj_Id, Result_Subt);
7884 Make_Object_Declaration (Loc,
7885 Defining_Identifier => Return_Obj_Id,
7886 Aliased_Present => True,
7887 Object_Definition => New_Reference_To (Result_Subt, Loc));
7889 Set_No_Initialization (Return_Obj_Decl);
7891 Insert_Action (Func_Call, Return_Obj_Decl);
7893 -- When the function has a controlling result, an allocation-form
7894 -- parameter must be passed indicating that the caller is allocating
7895 -- the result object. This is needed because such a function can be
7896 -- called as a dispatching operation and must be treated similarly
7897 -- to functions with unconstrained result subtypes.
7899 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7900 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7902 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7903 (Func_Call, Function_Id);
7905 Add_Task_Actuals_To_Build_In_Place_Call
7906 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7908 -- Add an implicit actual to the function call that provides access
7909 -- to the caller's return object.
7911 Add_Access_Actual_To_Build_In_Place_Call
7912 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
7914 -- When the result subtype is unconstrained, the function must allocate
7915 -- the return object in the secondary stack, so appropriate implicit
7916 -- parameters are added to the call to indicate that. A transient
7917 -- scope is established to ensure eventual cleanup of the result.
7920 -- Pass an allocation parameter indicating that the function should
7921 -- allocate its result on the secondary stack.
7923 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7924 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
7926 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7927 (Func_Call, Function_Id);
7929 Add_Task_Actuals_To_Build_In_Place_Call
7930 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7932 -- Pass a null value to the function since no return object is
7933 -- available on the caller side.
7935 Add_Access_Actual_To_Build_In_Place_Call
7936 (Func_Call, Function_Id, Empty);
7938 end Make_Build_In_Place_Call_In_Anonymous_Context;
7940 --------------------------------------------
7941 -- Make_Build_In_Place_Call_In_Assignment --
7942 --------------------------------------------
7944 procedure Make_Build_In_Place_Call_In_Assignment
7946 Function_Call : Node_Id)
7948 Lhs : constant Node_Id := Name (Assign);
7949 Func_Call : Node_Id := Function_Call;
7950 Func_Id : Entity_Id;
7954 Ptr_Typ : Entity_Id;
7955 Ptr_Typ_Decl : Node_Id;
7956 Result_Subt : Entity_Id;
7960 -- Step past qualification or unchecked conversion (the latter can occur
7961 -- in cases of calls to 'Input).
7963 if Nkind_In (Func_Call, N_Qualified_Expression,
7964 N_Unchecked_Type_Conversion)
7966 Func_Call := Expression (Func_Call);
7969 -- If the call has already been processed to add build-in-place actuals
7970 -- then return. This should not normally occur in an assignment context,
7971 -- but we add the protection as a defensive measure.
7973 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7977 -- Mark the call as processed as a build-in-place call
7979 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7981 Loc := Sloc (Function_Call);
7983 if Is_Entity_Name (Name (Func_Call)) then
7984 Func_Id := Entity (Name (Func_Call));
7986 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7987 Func_Id := Etype (Name (Func_Call));
7990 raise Program_Error;
7993 Result_Subt := Etype (Func_Id);
7995 -- When the result subtype is unconstrained, an additional actual must
7996 -- be passed to indicate that the caller is providing the return object.
7997 -- This parameter must also be passed when the called function has a
7998 -- controlling result, because dispatching calls to the function needs
7999 -- to be treated effectively the same as calls to class-wide functions.
8001 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8002 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8004 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8005 (Func_Call, Func_Id);
8007 Add_Task_Actuals_To_Build_In_Place_Call
8008 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8010 -- Add an implicit actual to the function call that provides access to
8011 -- the caller's return object.
8013 Add_Access_Actual_To_Build_In_Place_Call
8016 Make_Unchecked_Type_Conversion (Loc,
8017 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
8018 Expression => Relocate_Node (Lhs)));
8020 -- Create an access type designating the function's result subtype
8022 Ptr_Typ := Make_Temporary (Loc, 'A');
8025 Make_Full_Type_Declaration (Loc,
8026 Defining_Identifier => Ptr_Typ,
8028 Make_Access_To_Object_Definition (Loc,
8029 All_Present => True,
8030 Subtype_Indication =>
8031 New_Reference_To (Result_Subt, Loc)));
8032 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8034 -- Finally, create an access object initialized to a reference to the
8037 Obj_Id := Make_Temporary (Loc, 'R');
8038 Set_Etype (Obj_Id, Ptr_Typ);
8041 Make_Object_Declaration (Loc,
8042 Defining_Identifier => Obj_Id,
8043 Object_Definition => New_Reference_To (Ptr_Typ, Loc),
8044 Expression => Make_Reference (Loc, Relocate_Node (Func_Call)));
8045 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8047 Rewrite (Assign, Make_Null_Statement (Loc));
8049 -- Retrieve the target of the assignment
8051 if Nkind (Lhs) = N_Selected_Component then
8052 Target := Selector_Name (Lhs);
8053 elsif Nkind (Lhs) = N_Type_Conversion then
8054 Target := Expression (Lhs);
8059 -- If we are assigning to a return object or this is an expression of
8060 -- an extension aggregate, the target should either be an identifier
8061 -- or a simple expression. All other cases imply a different scenario.
8063 if Nkind (Target) in N_Has_Entity then
8064 Target := Entity (Target);
8068 end Make_Build_In_Place_Call_In_Assignment;
8070 ----------------------------------------------------
8071 -- Make_Build_In_Place_Call_In_Object_Declaration --
8072 ----------------------------------------------------
8074 procedure Make_Build_In_Place_Call_In_Object_Declaration
8075 (Object_Decl : Node_Id;
8076 Function_Call : Node_Id)
8079 Obj_Def_Id : constant Entity_Id :=
8080 Defining_Identifier (Object_Decl);
8081 Enclosing_Func : constant Entity_Id :=
8082 Enclosing_Subprogram (Obj_Def_Id);
8083 Call_Deref : Node_Id;
8084 Caller_Object : Node_Id;
8086 Fmaster_Actual : Node_Id := Empty;
8087 Func_Call : Node_Id := Function_Call;
8088 Function_Id : Entity_Id;
8089 Pool_Actual : Node_Id;
8090 Ptr_Typ_Decl : Node_Id;
8091 Pass_Caller_Acc : Boolean := False;
8093 Ref_Type : Entity_Id;
8094 Result_Subt : Entity_Id;
8097 -- Step past qualification or unchecked conversion (the latter can occur
8098 -- in cases of calls to 'Input).
8100 if Nkind_In (Func_Call, N_Qualified_Expression,
8101 N_Unchecked_Type_Conversion)
8103 Func_Call := Expression (Func_Call);
8106 -- If the call has already been processed to add build-in-place actuals
8107 -- then return. This should not normally occur in an object declaration,
8108 -- but we add the protection as a defensive measure.
8110 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8114 -- Mark the call as processed as a build-in-place call
8116 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8118 Loc := Sloc (Function_Call);
8120 if Is_Entity_Name (Name (Func_Call)) then
8121 Function_Id := Entity (Name (Func_Call));
8123 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8124 Function_Id := Etype (Name (Func_Call));
8127 raise Program_Error;
8130 Result_Subt := Etype (Function_Id);
8132 -- If the the object is a return object of an enclosing build-in-place
8133 -- function, then the implicit build-in-place parameters of the
8134 -- enclosing function are simply passed along to the called function.
8135 -- (Unfortunately, this won't cover the case of extension aggregates
8136 -- where the ancestor part is a build-in-place unconstrained function
8137 -- call that should be passed along the caller's parameters. Currently
8138 -- those get mishandled by reassigning the result of the call to the
8139 -- aggregate return object, when the call result should really be
8140 -- directly built in place in the aggregate and not in a temporary. ???)
8142 if Is_Return_Object (Defining_Identifier (Object_Decl)) then
8143 Pass_Caller_Acc := True;
8145 -- When the enclosing function has a BIP_Alloc_Form formal then we
8146 -- pass it along to the callee (such as when the enclosing function
8147 -- has an unconstrained or tagged result type).
8149 if Needs_BIP_Alloc_Form (Enclosing_Func) then
8150 if VM_Target = No_VM then
8152 New_Reference_To (Build_In_Place_Formal
8153 (Enclosing_Func, BIP_Storage_Pool), Loc);
8155 -- The build-in-place pool formal is not built on .NET/JVM
8158 Pool_Actual := Empty;
8161 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8166 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
8168 Pool_Actual => Pool_Actual);
8170 -- Otherwise, if enclosing function has a constrained result subtype,
8171 -- then caller allocation will be used.
8174 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8175 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8178 if Needs_BIP_Finalization_Master (Enclosing_Func) then
8181 (Build_In_Place_Formal
8182 (Enclosing_Func, BIP_Finalization_Master), Loc);
8185 -- Retrieve the BIPacc formal from the enclosing function and convert
8186 -- it to the access type of the callee's BIP_Object_Access formal.
8189 Make_Unchecked_Type_Conversion (Loc,
8193 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
8197 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
8200 -- In the constrained case, add an implicit actual to the function call
8201 -- that provides access to the declared object. An unchecked conversion
8202 -- to the (specific) result type of the function is inserted to handle
8203 -- the case where the object is declared with a class-wide type.
8205 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
8207 Make_Unchecked_Type_Conversion (Loc,
8208 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
8209 Expression => New_Reference_To (Obj_Def_Id, Loc));
8211 -- When the function has a controlling result, an allocation-form
8212 -- parameter must be passed indicating that the caller is allocating
8213 -- the result object. This is needed because such a function can be
8214 -- called as a dispatching operation and must be treated similarly
8215 -- to functions with unconstrained result subtypes.
8217 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8218 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8220 -- In other unconstrained cases, pass an indication to do the allocation
8221 -- on the secondary stack and set Caller_Object to Empty so that a null
8222 -- value will be passed for the caller's object address. A transient
8223 -- scope is established to ensure eventual cleanup of the result.
8226 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8227 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8228 Caller_Object := Empty;
8230 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
8233 -- Pass along any finalization master actual, which is needed in the
8234 -- case where the called function initializes a return object of an
8235 -- enclosing build-in-place function.
8237 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8238 (Func_Call => Func_Call,
8239 Func_Id => Function_Id,
8240 Master_Exp => Fmaster_Actual);
8242 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
8243 and then Has_Task (Result_Subt)
8245 -- Here we're passing along the master that was passed in to this
8248 Add_Task_Actuals_To_Build_In_Place_Call
8249 (Func_Call, Function_Id,
8252 (Build_In_Place_Formal (Enclosing_Func, BIP_Master), Loc));
8255 Add_Task_Actuals_To_Build_In_Place_Call
8256 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8259 Add_Access_Actual_To_Build_In_Place_Call
8260 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
8262 -- Create an access type designating the function's result subtype. We
8263 -- use the type of the original expression because it may be a call to
8264 -- an inherited operation, which the expansion has replaced with the
8265 -- parent operation that yields the parent type.
8267 Ref_Type := Make_Temporary (Loc, 'A');
8270 Make_Full_Type_Declaration (Loc,
8271 Defining_Identifier => Ref_Type,
8273 Make_Access_To_Object_Definition (Loc,
8274 All_Present => True,
8275 Subtype_Indication =>
8276 New_Reference_To (Etype (Function_Call), Loc)));
8278 -- The access type and its accompanying object must be inserted after
8279 -- the object declaration in the constrained case, so that the function
8280 -- call can be passed access to the object. In the unconstrained case,
8281 -- or if the object declaration is for a return object, the access type
8282 -- and object must be inserted before the object, since the object
8283 -- declaration is rewritten to be a renaming of a dereference of the
8286 if Is_Constrained (Underlying_Type (Result_Subt))
8287 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
8289 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
8291 Insert_Action (Object_Decl, Ptr_Typ_Decl);
8294 -- Finally, create an access object initialized to a reference to the
8297 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8299 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
8300 Set_Etype (Def_Id, Ref_Type);
8302 Insert_After_And_Analyze (Ptr_Typ_Decl,
8303 Make_Object_Declaration (Loc,
8304 Defining_Identifier => Def_Id,
8305 Object_Definition => New_Reference_To (Ref_Type, Loc),
8306 Expression => New_Expr));
8308 -- If the result subtype of the called function is constrained and
8309 -- is not itself the return expression of an enclosing BIP function,
8310 -- then mark the object as having no initialization.
8312 if Is_Constrained (Underlying_Type (Result_Subt))
8313 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
8315 Set_Expression (Object_Decl, Empty);
8316 Set_No_Initialization (Object_Decl);
8318 -- In case of an unconstrained result subtype, or if the call is the
8319 -- return expression of an enclosing BIP function, rewrite the object
8320 -- declaration as an object renaming where the renamed object is a
8321 -- dereference of <function_Call>'reference:
8323 -- Obj : Subt renames <function_call>'Ref.all;
8327 Make_Explicit_Dereference (Loc,
8328 Prefix => New_Reference_To (Def_Id, Loc));
8330 Loc := Sloc (Object_Decl);
8331 Rewrite (Object_Decl,
8332 Make_Object_Renaming_Declaration (Loc,
8333 Defining_Identifier => Make_Temporary (Loc, 'D'),
8334 Access_Definition => Empty,
8335 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8336 Name => Call_Deref));
8338 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
8340 Analyze (Object_Decl);
8342 -- Replace the internal identifier of the renaming declaration's
8343 -- entity with identifier of the original object entity. We also have
8344 -- to exchange the entities containing their defining identifiers to
8345 -- ensure the correct replacement of the object declaration by the
8346 -- object renaming declaration to avoid homograph conflicts (since
8347 -- the object declaration's defining identifier was already entered
8348 -- in current scope). The Next_Entity links of the two entities also
8349 -- have to be swapped since the entities are part of the return
8350 -- scope's entity list and the list structure would otherwise be
8351 -- corrupted. Finally, the homonym chain must be preserved as well.
8354 Renaming_Def_Id : constant Entity_Id :=
8355 Defining_Identifier (Object_Decl);
8356 Next_Entity_Temp : constant Entity_Id :=
8357 Next_Entity (Renaming_Def_Id);
8359 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
8361 -- Swap next entity links in preparation for exchanging entities
8363 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
8364 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
8365 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
8367 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
8369 -- Preserve source indication of original declaration, so that
8370 -- xref information is properly generated for the right entity.
8372 Preserve_Comes_From_Source
8373 (Object_Decl, Original_Node (Object_Decl));
8375 Preserve_Comes_From_Source
8376 (Obj_Def_Id, Original_Node (Object_Decl));
8378 Set_Comes_From_Source (Renaming_Def_Id, False);
8382 -- If the object entity has a class-wide Etype, then we need to change
8383 -- it to the result subtype of the function call, because otherwise the
8384 -- object will be class-wide without an explicit initialization and
8385 -- won't be allocated properly by the back end. It seems unclean to make
8386 -- such a revision to the type at this point, and we should try to
8387 -- improve this treatment when build-in-place functions with class-wide
8388 -- results are implemented. ???
8390 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
8391 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
8393 end Make_Build_In_Place_Call_In_Object_Declaration;
8395 -----------------------------------
8396 -- Needs_BIP_Finalization_Master --
8397 -----------------------------------
8399 function Needs_BIP_Finalization_Master
8400 (Func_Id : Entity_Id) return Boolean
8402 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8403 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8406 not Restriction_Active (No_Finalization)
8407 and then Needs_Finalization (Func_Typ);
8408 end Needs_BIP_Finalization_Master;
8410 --------------------------
8411 -- Needs_BIP_Alloc_Form --
8412 --------------------------
8414 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
8415 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8416 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8418 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
8419 end Needs_BIP_Alloc_Form;
8421 --------------------------------------
8422 -- Needs_Result_Accessibility_Level --
8423 --------------------------------------
8425 function Needs_Result_Accessibility_Level
8426 (Func_Id : Entity_Id) return Boolean
8428 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8430 function Has_Unconstrained_Access_Discriminant_Component
8431 (Comp_Typ : Entity_Id) return Boolean;
8432 -- Returns True if any component of the type has an unconstrained access
8435 -----------------------------------------------------
8436 -- Has_Unconstrained_Access_Discriminant_Component --
8437 -----------------------------------------------------
8439 function Has_Unconstrained_Access_Discriminant_Component
8440 (Comp_Typ : Entity_Id) return Boolean
8443 if not Is_Limited_Type (Comp_Typ) then
8446 -- Only limited types can have access discriminants with
8449 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
8452 elsif Is_Array_Type (Comp_Typ) then
8453 return Has_Unconstrained_Access_Discriminant_Component
8454 (Underlying_Type (Component_Type (Comp_Typ)));
8456 elsif Is_Record_Type (Comp_Typ) then
8461 Comp := First_Component (Comp_Typ);
8462 while Present (Comp) loop
8463 if Has_Unconstrained_Access_Discriminant_Component
8464 (Underlying_Type (Etype (Comp)))
8469 Next_Component (Comp);
8475 end Has_Unconstrained_Access_Discriminant_Component;
8477 Feature_Disabled : constant Boolean := True;
8480 -- Start of processing for Needs_Result_Accessibility_Level
8483 -- False if completion unavailable (how does this happen???)
8485 if not Present (Func_Typ) then
8488 elsif Feature_Disabled then
8491 -- False if not a function, also handle enum-lit renames case
8493 elsif Func_Typ = Standard_Void_Type
8494 or else Is_Scalar_Type (Func_Typ)
8498 -- Handle a corner case, a cross-dialect subp renaming. For example,
8499 -- an Ada2012 renaming of an Ada05 subprogram. This can occur when a
8500 -- non-Ada2012 unit references predefined runtime units.
8502 elsif Present (Alias (Func_Id)) then
8504 -- Unimplemented: a cross-dialect subp renaming which does not set
8505 -- the Alias attribute (e.g., a rename of a dereference of an access
8506 -- to subprogram value). ???
8508 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
8510 -- Remaining cases require Ada 2012 mode
8512 elsif Ada_Version < Ada_2012 then
8515 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
8516 or else Is_Tagged_Type (Func_Typ)
8518 -- In the case of, say, a null tagged record result type, the need
8519 -- for this extra parameter might not be obvious. This function
8520 -- returns True for all tagged types for compatibility reasons.
8521 -- A function with, say, a tagged null controlling result type might
8522 -- be overridden by a primitive of an extension having an access
8523 -- discriminant and the overrider and overridden must have compatible
8524 -- calling conventions (including implicitly declared parameters).
8525 -- Similarly, values of one access-to-subprogram type might designate
8526 -- both a primitive subprogram of a given type and a function
8527 -- which is, for example, not a primitive subprogram of any type.
8528 -- Again, this requires calling convention compatibility.
8529 -- It might be possible to solve these issues by introducing
8530 -- wrappers, but that is not the approach that was chosen.
8534 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
8537 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
8540 -- False for all other cases
8545 end Needs_Result_Accessibility_Level;