1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Elists; use Elists;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch2; use Exp_Ch2;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Intr; use Exp_Intr;
41 with Exp_Pakd; use Exp_Pakd;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Exp_VFpt; use Exp_VFpt;
45 with Fname; use Fname;
46 with Freeze; use Freeze;
47 with Inline; use Inline;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch12; use Sem_Ch12;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Mech; use Sem_Mech;
66 with Sem_Res; use Sem_Res;
67 with Sem_SCIL; use Sem_SCIL;
68 with Sem_Util; use Sem_Util;
69 with Sinfo; use Sinfo;
70 with Snames; use Snames;
71 with Stand; use Stand;
72 with Targparm; use Targparm;
73 with Tbuild; use Tbuild;
74 with Uintp; use Uintp;
75 with Validsw; use Validsw;
77 package body Exp_Ch6 is
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 procedure Add_Access_Actual_To_Build_In_Place_Call
84 (Function_Call : Node_Id;
85 Function_Id : Entity_Id;
86 Return_Object : Node_Id;
87 Is_Access : Boolean := False);
88 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
89 -- object name given by Return_Object and add the attribute to the end of
90 -- the actual parameter list associated with the build-in-place function
91 -- call denoted by Function_Call. However, if Is_Access is True, then
92 -- Return_Object is already an access expression, in which case it's passed
93 -- along directly to the build-in-place function. Finally, if Return_Object
94 -- is empty, then pass a null literal as the actual.
96 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
97 (Function_Call : Node_Id;
98 Function_Id : Entity_Id;
99 Alloc_Form : BIP_Allocation_Form := Unspecified;
100 Alloc_Form_Exp : Node_Id := Empty);
101 -- Ada 2005 (AI-318-02): Add an actual indicating the form of allocation,
102 -- if any, to be done by a build-in-place function. If Alloc_Form_Exp is
103 -- present, then use it, otherwise pass a literal corresponding to the
104 -- Alloc_Form parameter (which must not be Unspecified in that case).
106 procedure Add_Extra_Actual_To_Call
107 (Subprogram_Call : Node_Id;
108 Extra_Formal : Entity_Id;
109 Extra_Actual : Node_Id);
110 -- Adds Extra_Actual as a named parameter association for the formal
111 -- Extra_Formal in Subprogram_Call.
113 procedure Add_Final_List_Actual_To_Build_In_Place_Call
114 (Function_Call : Node_Id;
115 Function_Id : Entity_Id;
116 Acc_Type : Entity_Id;
117 Sel_Comp : Node_Id := Empty);
118 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type has
119 -- controlled parts, add an actual parameter that is a pointer to
120 -- appropriate finalization list. The finalization list is that of the
121 -- current scope, except for "new Acc'(F(...))" in which case it's the
122 -- finalization list of the access type returned by the allocator. Acc_Type
123 -- is that type in the allocator case; Empty otherwise. If Sel_Comp is
124 -- not Empty, then it denotes a selected component and the finalization
125 -- list is obtained from the _controller list of the prefix object.
127 procedure Add_Task_Actuals_To_Build_In_Place_Call
128 (Function_Call : Node_Id;
129 Function_Id : Entity_Id;
130 Master_Actual : Node_Id);
131 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
132 -- contains tasks, add two actual parameters: the master, and a pointer to
133 -- the caller's activation chain. Master_Actual is the actual parameter
134 -- expression to pass for the master. In most cases, this is the current
135 -- master (_master). The two exceptions are: If the function call is the
136 -- initialization expression for an allocator, we pass the master of the
137 -- access type. If the function call is the initialization expression for a
138 -- return object, we pass along the master passed in by the caller. The
139 -- activation chain to pass is always the local one. Note: Master_Actual
140 -- can be Empty, but only if there are no tasks
142 procedure Check_Overriding_Operation (Subp : Entity_Id);
143 -- Subp is a dispatching operation. Check whether it may override an
144 -- inherited private operation, in which case its DT entry is that of
145 -- the hidden operation, not the one it may have received earlier.
146 -- This must be done before emitting the code to set the corresponding
147 -- DT to the address of the subprogram. The actual placement of Subp in
148 -- the proper place in the list of primitive operations is done in
149 -- Declare_Inherited_Private_Subprograms, which also has to deal with
150 -- implicit operations. This duplication is unavoidable for now???
152 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
153 -- This procedure is called only if the subprogram body N, whose spec
154 -- has the given entity Spec, contains a parameterless recursive call.
155 -- It attempts to generate runtime code to detect if this a case of
156 -- infinite recursion.
158 -- The body is scanned to determine dependencies. If the only external
159 -- dependencies are on a small set of scalar variables, then the values
160 -- of these variables are captured on entry to the subprogram, and if
161 -- the values are not changed for the call, we know immediately that
162 -- we have an infinite recursion.
164 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id);
165 -- For each actual of an in-out or out parameter which is a numeric
166 -- (view) conversion of the form T (A), where A denotes a variable,
167 -- we insert the declaration:
169 -- Temp : T[ := T (A)];
171 -- prior to the call. Then we replace the actual with a reference to Temp,
172 -- and append the assignment:
174 -- A := TypeA (Temp);
176 -- after the call. Here TypeA is the actual type of variable A. For out
177 -- parameters, the initial declaration has no expression. If A is not an
178 -- entity name, we generate instead:
180 -- Var : TypeA renames A;
181 -- Temp : T := Var; -- omitting expression for out parameter.
183 -- Var := TypeA (Temp);
185 -- For other in-out parameters, we emit the required constraint checks
186 -- before and/or after the call.
188 -- For all parameter modes, actuals that denote components and slices of
189 -- packed arrays are expanded into suitable temporaries.
191 -- For non-scalar objects that are possibly unaligned, add call by copy
192 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
194 procedure Expand_Inlined_Call
197 Orig_Subp : Entity_Id);
198 -- If called subprogram can be inlined by the front-end, retrieve the
199 -- analyzed body, replace formals with actuals and expand call in place.
200 -- Generate thunks for actuals that are expressions, and insert the
201 -- corresponding constant declarations before the call. If the original
202 -- call is to a derived operation, the return type is the one of the
203 -- derived operation, but the body is that of the original, so return
204 -- expressions in the body must be converted to the desired type (which
205 -- is simply not noted in the tree without inline expansion).
207 procedure Expand_Non_Function_Return (N : Node_Id);
208 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
209 -- a procedure body, entry body, accept statement, or extended return
210 -- statement. Note that all non-function returns are simple return
213 function Expand_Protected_Object_Reference
215 Scop : Entity_Id) return Node_Id;
217 procedure Expand_Protected_Subprogram_Call
221 -- A call to a protected subprogram within the protected object may appear
222 -- as a regular call. The list of actuals must be expanded to contain a
223 -- reference to the object itself, and the call becomes a call to the
224 -- corresponding protected subprogram.
226 function Is_Null_Procedure (Subp : Entity_Id) return Boolean;
227 -- Predicate to recognize stubbed procedures and null procedures, which
228 -- can be inlined unconditionally in all cases.
230 procedure Expand_Simple_Function_Return (N : Node_Id);
231 -- Expand simple return from function. In the case where we are returning
232 -- from a function body this is called by Expand_N_Simple_Return_Statement.
234 ----------------------------------------------
235 -- Add_Access_Actual_To_Build_In_Place_Call --
236 ----------------------------------------------
238 procedure Add_Access_Actual_To_Build_In_Place_Call
239 (Function_Call : Node_Id;
240 Function_Id : Entity_Id;
241 Return_Object : Node_Id;
242 Is_Access : Boolean := False)
244 Loc : constant Source_Ptr := Sloc (Function_Call);
245 Obj_Address : Node_Id;
246 Obj_Acc_Formal : Entity_Id;
249 -- Locate the implicit access parameter in the called function
251 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
253 -- If no return object is provided, then pass null
255 if not Present (Return_Object) then
256 Obj_Address := Make_Null (Loc);
257 Set_Parent (Obj_Address, Function_Call);
259 -- If Return_Object is already an expression of an access type, then use
260 -- it directly, since it must be an access value denoting the return
261 -- object, and couldn't possibly be the return object itself.
264 Obj_Address := Return_Object;
265 Set_Parent (Obj_Address, Function_Call);
267 -- Apply Unrestricted_Access to caller's return object
271 Make_Attribute_Reference (Loc,
272 Prefix => Return_Object,
273 Attribute_Name => Name_Unrestricted_Access);
275 Set_Parent (Return_Object, Obj_Address);
276 Set_Parent (Obj_Address, Function_Call);
279 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
281 -- Build the parameter association for the new actual and add it to the
282 -- end of the function's actuals.
284 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
285 end Add_Access_Actual_To_Build_In_Place_Call;
287 --------------------------------------------------
288 -- Add_Alloc_Form_Actual_To_Build_In_Place_Call --
289 --------------------------------------------------
291 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
292 (Function_Call : Node_Id;
293 Function_Id : Entity_Id;
294 Alloc_Form : BIP_Allocation_Form := Unspecified;
295 Alloc_Form_Exp : Node_Id := Empty)
297 Loc : constant Source_Ptr := Sloc (Function_Call);
298 Alloc_Form_Actual : Node_Id;
299 Alloc_Form_Formal : Node_Id;
302 -- The allocation form generally doesn't need to be passed in the case
303 -- of a constrained result subtype, since normally the caller performs
304 -- the allocation in that case. However this formal is still needed in
305 -- the case where the function has a tagged result, because generally
306 -- such functions can be called in a dispatching context and such calls
307 -- must be handled like calls to class-wide functions.
309 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
310 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
315 -- Locate the implicit allocation form parameter in the called function.
316 -- Maybe it would be better for each implicit formal of a build-in-place
317 -- function to have a flag or a Uint attribute to identify it. ???
319 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
321 if Present (Alloc_Form_Exp) then
322 pragma Assert (Alloc_Form = Unspecified);
324 Alloc_Form_Actual := Alloc_Form_Exp;
327 pragma Assert (Alloc_Form /= Unspecified);
330 Make_Integer_Literal (Loc,
331 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
334 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
336 -- Build the parameter association for the new actual and add it to the
337 -- end of the function's actuals.
339 Add_Extra_Actual_To_Call
340 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
341 end Add_Alloc_Form_Actual_To_Build_In_Place_Call;
343 ------------------------------
344 -- Add_Extra_Actual_To_Call --
345 ------------------------------
347 procedure Add_Extra_Actual_To_Call
348 (Subprogram_Call : Node_Id;
349 Extra_Formal : Entity_Id;
350 Extra_Actual : Node_Id)
352 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
353 Param_Assoc : Node_Id;
357 Make_Parameter_Association (Loc,
358 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
359 Explicit_Actual_Parameter => Extra_Actual);
361 Set_Parent (Param_Assoc, Subprogram_Call);
362 Set_Parent (Extra_Actual, Param_Assoc);
364 if Present (Parameter_Associations (Subprogram_Call)) then
365 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
366 N_Parameter_Association
369 -- Find last named actual, and append
374 L := First_Actual (Subprogram_Call);
375 while Present (L) loop
376 if No (Next_Actual (L)) then
377 Set_Next_Named_Actual (Parent (L), Extra_Actual);
385 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
388 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
391 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
392 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
394 end Add_Extra_Actual_To_Call;
396 --------------------------------------------------
397 -- Add_Final_List_Actual_To_Build_In_Place_Call --
398 --------------------------------------------------
400 procedure Add_Final_List_Actual_To_Build_In_Place_Call
401 (Function_Call : Node_Id;
402 Function_Id : Entity_Id;
403 Acc_Type : Entity_Id;
404 Sel_Comp : Node_Id := Empty)
406 Loc : constant Source_Ptr := Sloc (Function_Call);
407 Final_List : Node_Id;
408 Final_List_Actual : Node_Id;
409 Final_List_Formal : Node_Id;
410 Is_Ctrl_Result : constant Boolean :=
412 (Underlying_Type (Etype (Function_Id)));
415 -- No such extra parameter is needed if there are no controlled parts.
416 -- The test for Needs_Finalization accounts for class-wide results
417 -- (which potentially have controlled parts, even if the root type
418 -- doesn't), and the test for a tagged result type is needed because
419 -- calls to such a function can in general occur in dispatching
420 -- contexts, which must be treated the same as a call to class-wide
421 -- functions. Both of these situations require that a finalization list
424 if not Needs_BIP_Final_List (Function_Id) then
428 -- Locate implicit finalization list parameter in the called function
430 Final_List_Formal := Build_In_Place_Formal (Function_Id, BIP_Final_List);
432 -- Create the actual which is a pointer to the appropriate finalization
433 -- list. Acc_Type is present if and only if this call is the
434 -- initialization of an allocator. Use the Current_Scope or the
435 -- Acc_Type as appropriate.
437 if Present (Acc_Type)
438 and then (Ekind (Acc_Type) = E_Anonymous_Access_Type
440 Present (Associated_Final_Chain (Base_Type (Acc_Type))))
442 Final_List := Find_Final_List (Acc_Type);
444 -- If Sel_Comp is present and the function result is controlled, then
445 -- the finalization list will be obtained from the _controller list of
446 -- the selected component's prefix object.
448 elsif Present (Sel_Comp) and then Is_Ctrl_Result then
449 Final_List := Find_Final_List (Current_Scope, Sel_Comp);
452 Final_List := Find_Final_List (Current_Scope);
456 Make_Attribute_Reference (Loc,
457 Prefix => Final_List,
458 Attribute_Name => Name_Unrestricted_Access);
460 Analyze_And_Resolve (Final_List_Actual, Etype (Final_List_Formal));
462 -- Build the parameter association for the new actual and add it to the
463 -- end of the function's actuals.
465 Add_Extra_Actual_To_Call
466 (Function_Call, Final_List_Formal, Final_List_Actual);
467 end Add_Final_List_Actual_To_Build_In_Place_Call;
469 ---------------------------------------------
470 -- Add_Task_Actuals_To_Build_In_Place_Call --
471 ---------------------------------------------
473 procedure Add_Task_Actuals_To_Build_In_Place_Call
474 (Function_Call : Node_Id;
475 Function_Id : Entity_Id;
476 Master_Actual : Node_Id)
478 Loc : constant Source_Ptr := Sloc (Function_Call);
479 Actual : Node_Id := Master_Actual;
482 -- No such extra parameters are needed if there are no tasks
484 if not Has_Task (Etype (Function_Id)) then
488 -- Use a dummy _master actual in case of No_Task_Hierarchy
490 if Restriction_Active (No_Task_Hierarchy) then
491 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
497 Master_Formal : Node_Id;
499 -- Locate implicit master parameter in the called function
501 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Master);
503 Analyze_And_Resolve (Actual, Etype (Master_Formal));
505 -- Build the parameter association for the new actual and add it to
506 -- the end of the function's actuals.
508 Add_Extra_Actual_To_Call
509 (Function_Call, Master_Formal, Actual);
512 -- The activation chain
515 Activation_Chain_Actual : Node_Id;
516 Activation_Chain_Formal : Node_Id;
519 -- Locate implicit activation chain parameter in the called function
521 Activation_Chain_Formal := Build_In_Place_Formal
522 (Function_Id, BIP_Activation_Chain);
524 -- Create the actual which is a pointer to the current activation
527 Activation_Chain_Actual :=
528 Make_Attribute_Reference (Loc,
529 Prefix => Make_Identifier (Loc, Name_uChain),
530 Attribute_Name => Name_Unrestricted_Access);
533 (Activation_Chain_Actual, Etype (Activation_Chain_Formal));
535 -- Build the parameter association for the new actual and add it to
536 -- the end of the function's actuals.
538 Add_Extra_Actual_To_Call
539 (Function_Call, Activation_Chain_Formal, Activation_Chain_Actual);
541 end Add_Task_Actuals_To_Build_In_Place_Call;
543 -----------------------
544 -- BIP_Formal_Suffix --
545 -----------------------
547 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
550 when BIP_Alloc_Form =>
552 when BIP_Final_List =>
553 return "BIPfinallist";
556 when BIP_Activation_Chain =>
557 return "BIPactivationchain";
558 when BIP_Object_Access =>
561 end BIP_Formal_Suffix;
563 ---------------------------
564 -- Build_In_Place_Formal --
565 ---------------------------
567 function Build_In_Place_Formal
569 Kind : BIP_Formal_Kind) return Entity_Id
571 Extra_Formal : Entity_Id := Extra_Formals (Func);
574 -- Maybe it would be better for each implicit formal of a build-in-place
575 -- function to have a flag or a Uint attribute to identify it. ???
578 pragma Assert (Present (Extra_Formal));
580 Chars (Extra_Formal) =
581 New_External_Name (Chars (Func), BIP_Formal_Suffix (Kind));
582 Next_Formal_With_Extras (Extra_Formal);
586 end Build_In_Place_Formal;
588 --------------------------------
589 -- Check_Overriding_Operation --
590 --------------------------------
592 procedure Check_Overriding_Operation (Subp : Entity_Id) is
593 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
594 Op_List : constant Elist_Id := Primitive_Operations (Typ);
600 if Is_Derived_Type (Typ)
601 and then not Is_Private_Type (Typ)
602 and then In_Open_Scopes (Scope (Etype (Typ)))
603 and then Typ = Base_Type (Typ)
605 -- Subp overrides an inherited private operation if there is an
606 -- inherited operation with a different name than Subp (see
607 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
608 -- same name as Subp.
610 Op_Elmt := First_Elmt (Op_List);
611 while Present (Op_Elmt) loop
612 Prim_Op := Node (Op_Elmt);
613 Par_Op := Alias (Prim_Op);
616 and then not Comes_From_Source (Prim_Op)
617 and then Chars (Prim_Op) /= Chars (Par_Op)
618 and then Chars (Par_Op) = Chars (Subp)
619 and then Is_Hidden (Par_Op)
620 and then Type_Conformant (Prim_Op, Subp)
622 Set_DT_Position (Subp, DT_Position (Prim_Op));
628 end Check_Overriding_Operation;
630 -------------------------------
631 -- Detect_Infinite_Recursion --
632 -------------------------------
634 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
635 Loc : constant Source_Ptr := Sloc (N);
637 Var_List : constant Elist_Id := New_Elmt_List;
638 -- List of globals referenced by body of procedure
640 Call_List : constant Elist_Id := New_Elmt_List;
641 -- List of recursive calls in body of procedure
643 Shad_List : constant Elist_Id := New_Elmt_List;
644 -- List of entity id's for entities created to capture the value of
645 -- referenced globals on entry to the procedure.
647 Scop : constant Uint := Scope_Depth (Spec);
648 -- This is used to record the scope depth of the current procedure, so
649 -- that we can identify global references.
651 Max_Vars : constant := 4;
652 -- Do not test more than four global variables
654 Count_Vars : Natural := 0;
655 -- Count variables found so far
667 function Process (Nod : Node_Id) return Traverse_Result;
668 -- Function to traverse the subprogram body (using Traverse_Func)
674 function Process (Nod : Node_Id) return Traverse_Result is
678 if Nkind (Nod) = N_Procedure_Call_Statement then
680 -- Case of one of the detected recursive calls
682 if Is_Entity_Name (Name (Nod))
683 and then Has_Recursive_Call (Entity (Name (Nod)))
684 and then Entity (Name (Nod)) = Spec
686 Append_Elmt (Nod, Call_List);
689 -- Any other procedure call may have side effects
695 -- A call to a pure function can always be ignored
697 elsif Nkind (Nod) = N_Function_Call
698 and then Is_Entity_Name (Name (Nod))
699 and then Is_Pure (Entity (Name (Nod)))
703 -- Case of an identifier reference
705 elsif Nkind (Nod) = N_Identifier then
708 -- If no entity, then ignore the reference
710 -- Not clear why this can happen. To investigate, remove this
711 -- test and look at the crash that occurs here in 3401-004 ???
716 -- Ignore entities with no Scope, again not clear how this
717 -- can happen, to investigate, look at 4108-008 ???
719 elsif No (Scope (Ent)) then
722 -- Ignore the reference if not to a more global object
724 elsif Scope_Depth (Scope (Ent)) >= Scop then
727 -- References to types, exceptions and constants are always OK
730 or else Ekind (Ent) = E_Exception
731 or else Ekind (Ent) = E_Constant
735 -- If other than a non-volatile scalar variable, we have some
736 -- kind of global reference (e.g. to a function) that we cannot
737 -- deal with so we forget the attempt.
739 elsif Ekind (Ent) /= E_Variable
740 or else not Is_Scalar_Type (Etype (Ent))
741 or else Treat_As_Volatile (Ent)
745 -- Otherwise we have a reference to a global scalar
748 -- Loop through global entities already detected
750 Elm := First_Elmt (Var_List);
752 -- If not detected before, record this new global reference
755 Count_Vars := Count_Vars + 1;
757 if Count_Vars <= Max_Vars then
758 Append_Elmt (Entity (Nod), Var_List);
765 -- If recorded before, ignore
767 elsif Node (Elm) = Entity (Nod) then
770 -- Otherwise keep looking
780 -- For all other node kinds, recursively visit syntactic children
787 function Traverse_Body is new Traverse_Func (Process);
789 -- Start of processing for Detect_Infinite_Recursion
792 -- Do not attempt detection in No_Implicit_Conditional mode, since we
793 -- won't be able to generate the code to handle the recursion in any
796 if Restriction_Active (No_Implicit_Conditionals) then
800 -- Otherwise do traversal and quit if we get abandon signal
802 if Traverse_Body (N) = Abandon then
805 -- We must have a call, since Has_Recursive_Call was set. If not just
806 -- ignore (this is only an error check, so if we have a funny situation,
807 -- due to bugs or errors, we do not want to bomb!)
809 elsif Is_Empty_Elmt_List (Call_List) then
813 -- Here is the case where we detect recursion at compile time
815 -- Push our current scope for analyzing the declarations and code that
816 -- we will insert for the checking.
820 -- This loop builds temporary variables for each of the referenced
821 -- globals, so that at the end of the loop the list Shad_List contains
822 -- these temporaries in one-to-one correspondence with the elements in
826 Elm := First_Elmt (Var_List);
827 while Present (Elm) loop
829 Ent := Make_Temporary (Loc, 'S');
830 Append_Elmt (Ent, Shad_List);
832 -- Insert a declaration for this temporary at the start of the
833 -- declarations for the procedure. The temporaries are declared as
834 -- constant objects initialized to the current values of the
835 -- corresponding temporaries.
838 Make_Object_Declaration (Loc,
839 Defining_Identifier => Ent,
840 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
841 Constant_Present => True,
842 Expression => New_Occurrence_Of (Var, Loc));
845 Prepend (Decl, Declarations (N));
847 Insert_After (Last, Decl);
855 -- Loop through calls
857 Call := First_Elmt (Call_List);
858 while Present (Call) loop
860 -- Build a predicate expression of the form
863 -- and then global1 = temp1
864 -- and then global2 = temp2
867 -- This predicate determines if any of the global values
868 -- referenced by the procedure have changed since the
869 -- current call, if not an infinite recursion is assured.
871 Test := New_Occurrence_Of (Standard_True, Loc);
873 Elm1 := First_Elmt (Var_List);
874 Elm2 := First_Elmt (Shad_List);
875 while Present (Elm1) loop
881 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
882 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
888 -- Now we replace the call with the sequence
890 -- if no-changes (see above) then
891 -- raise Storage_Error;
896 Rewrite (Node (Call),
897 Make_If_Statement (Loc,
899 Then_Statements => New_List (
900 Make_Raise_Storage_Error (Loc,
901 Reason => SE_Infinite_Recursion)),
903 Else_Statements => New_List (
904 Relocate_Node (Node (Call)))));
906 Analyze (Node (Call));
911 -- Remove temporary scope stack entry used for analysis
914 end Detect_Infinite_Recursion;
920 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
921 Loc : constant Source_Ptr := Sloc (N);
926 E_Formal : Entity_Id;
928 procedure Add_Call_By_Copy_Code;
929 -- For cases where the parameter must be passed by copy, this routine
930 -- generates a temporary variable into which the actual is copied and
931 -- then passes this as the parameter. For an OUT or IN OUT parameter,
932 -- an assignment is also generated to copy the result back. The call
933 -- also takes care of any constraint checks required for the type
934 -- conversion case (on both the way in and the way out).
936 procedure Add_Simple_Call_By_Copy_Code;
937 -- This is similar to the above, but is used in cases where we know
938 -- that all that is needed is to simply create a temporary and copy
939 -- the value in and out of the temporary.
941 procedure Check_Fortran_Logical;
942 -- A value of type Logical that is passed through a formal parameter
943 -- must be normalized because .TRUE. usually does not have the same
944 -- representation as True. We assume that .FALSE. = False = 0.
945 -- What about functions that return a logical type ???
947 function Is_Legal_Copy return Boolean;
948 -- Check that an actual can be copied before generating the temporary
949 -- to be used in the call. If the actual is of a by_reference type then
950 -- the program is illegal (this can only happen in the presence of
951 -- rep. clauses that force an incorrect alignment). If the formal is
952 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
953 -- the effect that this might lead to unaligned arguments.
955 function Make_Var (Actual : Node_Id) return Entity_Id;
956 -- Returns an entity that refers to the given actual parameter,
957 -- Actual (not including any type conversion). If Actual is an
958 -- entity name, then this entity is returned unchanged, otherwise
959 -- a renaming is created to provide an entity for the actual.
961 procedure Reset_Packed_Prefix;
962 -- The expansion of a packed array component reference is delayed in
963 -- the context of a call. Now we need to complete the expansion, so we
964 -- unmark the analyzed bits in all prefixes.
966 ---------------------------
967 -- Add_Call_By_Copy_Code --
968 ---------------------------
970 procedure Add_Call_By_Copy_Code is
976 F_Typ : constant Entity_Id := Etype (Formal);
981 if not Is_Legal_Copy then
985 Temp := Make_Temporary (Loc, 'T', Actual);
987 -- Use formal type for temp, unless formal type is an unconstrained
988 -- array, in which case we don't have to worry about bounds checks,
989 -- and we use the actual type, since that has appropriate bounds.
991 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
992 Indic := New_Occurrence_Of (Etype (Actual), Loc);
994 Indic := New_Occurrence_Of (Etype (Formal), Loc);
997 if Nkind (Actual) = N_Type_Conversion then
998 V_Typ := Etype (Expression (Actual));
1000 -- If the formal is an (in-)out parameter, capture the name
1001 -- of the variable in order to build the post-call assignment.
1003 Var := Make_Var (Expression (Actual));
1005 Crep := not Same_Representation
1006 (F_Typ, Etype (Expression (Actual)));
1009 V_Typ := Etype (Actual);
1010 Var := Make_Var (Actual);
1014 -- Setup initialization for case of in out parameter, or an out
1015 -- parameter where the formal is an unconstrained array (in the
1016 -- latter case, we have to pass in an object with bounds).
1018 -- If this is an out parameter, the initial copy is wasteful, so as
1019 -- an optimization for the one-dimensional case we extract the
1020 -- bounds of the actual and build an uninitialized temporary of the
1023 if Ekind (Formal) = E_In_Out_Parameter
1024 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1026 if Nkind (Actual) = N_Type_Conversion then
1027 if Conversion_OK (Actual) then
1028 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1030 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1033 elsif Ekind (Formal) = E_Out_Parameter
1034 and then Is_Array_Type (F_Typ)
1035 and then Number_Dimensions (F_Typ) = 1
1036 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1038 -- Actual is a one-dimensional array or slice, and the type
1039 -- requires no initialization. Create a temporary of the
1040 -- right size, but do not copy actual into it (optimization).
1044 Make_Subtype_Indication (Loc,
1046 New_Occurrence_Of (F_Typ, Loc),
1048 Make_Index_Or_Discriminant_Constraint (Loc,
1049 Constraints => New_List (
1052 Make_Attribute_Reference (Loc,
1053 Prefix => New_Occurrence_Of (Var, Loc),
1054 Attribute_Name => Name_First),
1056 Make_Attribute_Reference (Loc,
1057 Prefix => New_Occurrence_Of (Var, Loc),
1058 Attribute_Name => Name_Last)))));
1061 Init := New_Occurrence_Of (Var, Loc);
1064 -- An initialization is created for packed conversions as
1065 -- actuals for out parameters to enable Make_Object_Declaration
1066 -- to determine the proper subtype for N_Node. Note that this
1067 -- is wasteful because the extra copying on the call side is
1068 -- not required for such out parameters. ???
1070 elsif Ekind (Formal) = E_Out_Parameter
1071 and then Nkind (Actual) = N_Type_Conversion
1072 and then (Is_Bit_Packed_Array (F_Typ)
1074 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1076 if Conversion_OK (Actual) then
1077 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1079 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1082 elsif Ekind (Formal) = E_In_Parameter then
1084 -- Handle the case in which the actual is a type conversion
1086 if Nkind (Actual) = N_Type_Conversion then
1087 if Conversion_OK (Actual) then
1088 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1090 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1093 Init := New_Occurrence_Of (Var, Loc);
1101 Make_Object_Declaration (Loc,
1102 Defining_Identifier => Temp,
1103 Object_Definition => Indic,
1104 Expression => Init);
1105 Set_Assignment_OK (N_Node);
1106 Insert_Action (N, N_Node);
1108 -- Now, normally the deal here is that we use the defining
1109 -- identifier created by that object declaration. There is
1110 -- one exception to this. In the change of representation case
1111 -- the above declaration will end up looking like:
1113 -- temp : type := identifier;
1115 -- And in this case we might as well use the identifier directly
1116 -- and eliminate the temporary. Note that the analysis of the
1117 -- declaration was not a waste of time in that case, since it is
1118 -- what generated the necessary change of representation code. If
1119 -- the change of representation introduced additional code, as in
1120 -- a fixed-integer conversion, the expression is not an identifier
1121 -- and must be kept.
1124 and then Present (Expression (N_Node))
1125 and then Is_Entity_Name (Expression (N_Node))
1127 Temp := Entity (Expression (N_Node));
1128 Rewrite (N_Node, Make_Null_Statement (Loc));
1131 -- For IN parameter, all we do is to replace the actual
1133 if Ekind (Formal) = E_In_Parameter then
1134 Rewrite (Actual, New_Reference_To (Temp, Loc));
1137 -- Processing for OUT or IN OUT parameter
1140 -- Kill current value indications for the temporary variable we
1141 -- created, since we just passed it as an OUT parameter.
1143 Kill_Current_Values (Temp);
1144 Set_Is_Known_Valid (Temp, False);
1146 -- If type conversion, use reverse conversion on exit
1148 if Nkind (Actual) = N_Type_Conversion then
1149 if Conversion_OK (Actual) then
1150 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1152 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1155 Expr := New_Occurrence_Of (Temp, Loc);
1158 Rewrite (Actual, New_Reference_To (Temp, Loc));
1161 -- If the actual is a conversion of a packed reference, it may
1162 -- already have been expanded by Remove_Side_Effects, and the
1163 -- resulting variable is a temporary which does not designate
1164 -- the proper out-parameter, which may not be addressable. In
1165 -- that case, generate an assignment to the original expression
1166 -- (before expansion of the packed reference) so that the proper
1167 -- expansion of assignment to a packed component can take place.
1174 if Is_Renaming_Of_Object (Var)
1175 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1176 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1177 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1178 = N_Indexed_Component
1180 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1182 Obj := Renamed_Object (Var);
1184 Make_Selected_Component (Loc,
1186 New_Copy_Tree (Original_Node (Prefix (Obj))),
1187 Selector_Name => New_Copy (Selector_Name (Obj)));
1188 Reset_Analyzed_Flags (Lhs);
1191 Lhs := New_Occurrence_Of (Var, Loc);
1194 Set_Assignment_OK (Lhs);
1196 Append_To (Post_Call,
1197 Make_Assignment_Statement (Loc,
1199 Expression => Expr));
1202 end Add_Call_By_Copy_Code;
1204 ----------------------------------
1205 -- Add_Simple_Call_By_Copy_Code --
1206 ----------------------------------
1208 procedure Add_Simple_Call_By_Copy_Code is
1216 F_Typ : constant Entity_Id := Etype (Formal);
1219 if not Is_Legal_Copy then
1223 -- Use formal type for temp, unless formal type is an unconstrained
1224 -- array, in which case we don't have to worry about bounds checks,
1225 -- and we use the actual type, since that has appropriate bounds.
1227 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1228 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1230 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1233 -- Prepare to generate code
1235 Reset_Packed_Prefix;
1237 Temp := Make_Temporary (Loc, 'T', Actual);
1238 Incod := Relocate_Node (Actual);
1239 Outcod := New_Copy_Tree (Incod);
1241 -- Generate declaration of temporary variable, initializing it
1242 -- with the input parameter unless we have an OUT formal or
1243 -- this is an initialization call.
1245 -- If the formal is an out parameter with discriminants, the
1246 -- discriminants must be captured even if the rest of the object
1247 -- is in principle uninitialized, because the discriminants may
1248 -- be read by the called subprogram.
1250 if Ekind (Formal) = E_Out_Parameter then
1253 if Has_Discriminants (Etype (Formal)) then
1254 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1257 elsif Inside_Init_Proc then
1259 -- Could use a comment here to match comment below ???
1261 if Nkind (Actual) /= N_Selected_Component
1263 not Has_Discriminant_Dependent_Constraint
1264 (Entity (Selector_Name (Actual)))
1268 -- Otherwise, keep the component in order to generate the proper
1269 -- actual subtype, that depends on enclosing discriminants.
1277 Make_Object_Declaration (Loc,
1278 Defining_Identifier => Temp,
1279 Object_Definition => Indic,
1280 Expression => Incod);
1285 -- If the call is to initialize a component of a composite type,
1286 -- and the component does not depend on discriminants, use the
1287 -- actual type of the component. This is required in case the
1288 -- component is constrained, because in general the formal of the
1289 -- initialization procedure will be unconstrained. Note that if
1290 -- the component being initialized is constrained by an enclosing
1291 -- discriminant, the presence of the initialization in the
1292 -- declaration will generate an expression for the actual subtype.
1294 Set_No_Initialization (Decl);
1295 Set_Object_Definition (Decl,
1296 New_Occurrence_Of (Etype (Actual), Loc));
1299 Insert_Action (N, Decl);
1301 -- The actual is simply a reference to the temporary
1303 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1305 -- Generate copy out if OUT or IN OUT parameter
1307 if Ekind (Formal) /= E_In_Parameter then
1309 Rhs := New_Occurrence_Of (Temp, Loc);
1311 -- Deal with conversion
1313 if Nkind (Lhs) = N_Type_Conversion then
1314 Lhs := Expression (Lhs);
1315 Rhs := Convert_To (Etype (Actual), Rhs);
1318 Append_To (Post_Call,
1319 Make_Assignment_Statement (Loc,
1321 Expression => Rhs));
1322 Set_Assignment_OK (Name (Last (Post_Call)));
1324 end Add_Simple_Call_By_Copy_Code;
1326 ---------------------------
1327 -- Check_Fortran_Logical --
1328 ---------------------------
1330 procedure Check_Fortran_Logical is
1331 Logical : constant Entity_Id := Etype (Formal);
1334 -- Note: this is very incomplete, e.g. it does not handle arrays
1335 -- of logical values. This is really not the right approach at all???)
1338 if Convention (Subp) = Convention_Fortran
1339 and then Root_Type (Etype (Formal)) = Standard_Boolean
1340 and then Ekind (Formal) /= E_In_Parameter
1342 Var := Make_Var (Actual);
1343 Append_To (Post_Call,
1344 Make_Assignment_Statement (Loc,
1345 Name => New_Occurrence_Of (Var, Loc),
1347 Unchecked_Convert_To (
1350 Left_Opnd => New_Occurrence_Of (Var, Loc),
1352 Unchecked_Convert_To (
1354 New_Occurrence_Of (Standard_False, Loc))))));
1356 end Check_Fortran_Logical;
1362 function Is_Legal_Copy return Boolean is
1364 -- An attempt to copy a value of such a type can only occur if
1365 -- representation clauses give the actual a misaligned address.
1367 if Is_By_Reference_Type (Etype (Formal)) then
1369 ("misaligned actual cannot be passed by reference", Actual);
1372 -- For users of Starlet, we assume that the specification of by-
1373 -- reference mechanism is mandatory. This may lead to unaligned
1374 -- objects but at least for DEC legacy code it is known to work.
1375 -- The warning will alert users of this code that a problem may
1378 elsif Mechanism (Formal) = By_Reference
1379 and then Is_Valued_Procedure (Scope (Formal))
1382 ("by_reference actual may be misaligned?", Actual);
1394 function Make_Var (Actual : Node_Id) return Entity_Id is
1398 if Is_Entity_Name (Actual) then
1399 return Entity (Actual);
1402 Var := Make_Temporary (Loc, 'T', Actual);
1405 Make_Object_Renaming_Declaration (Loc,
1406 Defining_Identifier => Var,
1408 New_Occurrence_Of (Etype (Actual), Loc),
1409 Name => Relocate_Node (Actual));
1411 Insert_Action (N, N_Node);
1416 -------------------------
1417 -- Reset_Packed_Prefix --
1418 -------------------------
1420 procedure Reset_Packed_Prefix is
1421 Pfx : Node_Id := Actual;
1424 Set_Analyzed (Pfx, False);
1426 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1427 Pfx := Prefix (Pfx);
1429 end Reset_Packed_Prefix;
1431 -- Start of processing for Expand_Actuals
1434 Post_Call := New_List;
1436 Formal := First_Formal (Subp);
1437 Actual := First_Actual (N);
1438 while Present (Formal) loop
1439 E_Formal := Etype (Formal);
1441 if Is_Scalar_Type (E_Formal)
1442 or else Nkind (Actual) = N_Slice
1444 Check_Fortran_Logical;
1448 elsif Ekind (Formal) /= E_Out_Parameter then
1450 -- The unusual case of the current instance of a protected type
1451 -- requires special handling. This can only occur in the context
1452 -- of a call within the body of a protected operation.
1454 if Is_Entity_Name (Actual)
1455 and then Ekind (Entity (Actual)) = E_Protected_Type
1456 and then In_Open_Scopes (Entity (Actual))
1458 if Scope (Subp) /= Entity (Actual) then
1459 Error_Msg_N ("operation outside protected type may not "
1460 & "call back its protected operations?", Actual);
1464 Expand_Protected_Object_Reference (N, Entity (Actual)));
1467 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1468 -- build-in-place function, then a temporary return object needs
1469 -- to be created and access to it must be passed to the function.
1470 -- Currently we limit such functions to those with inherently
1471 -- limited result subtypes, but eventually we plan to expand the
1472 -- functions that are treated as build-in-place to include other
1473 -- composite result types.
1475 if Ada_Version >= Ada_2005
1476 and then Is_Build_In_Place_Function_Call (Actual)
1478 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1481 Apply_Constraint_Check (Actual, E_Formal);
1483 -- Out parameter case. No constraint checks on access type
1486 elsif Is_Access_Type (E_Formal) then
1491 elsif Has_Discriminants (Base_Type (E_Formal))
1492 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1494 Apply_Constraint_Check (Actual, E_Formal);
1499 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1502 -- Processing for IN-OUT and OUT parameters
1504 if Ekind (Formal) /= E_In_Parameter then
1506 -- For type conversions of arrays, apply length/range checks
1508 if Is_Array_Type (E_Formal)
1509 and then Nkind (Actual) = N_Type_Conversion
1511 if Is_Constrained (E_Formal) then
1512 Apply_Length_Check (Expression (Actual), E_Formal);
1514 Apply_Range_Check (Expression (Actual), E_Formal);
1518 -- If argument is a type conversion for a type that is passed
1519 -- by copy, then we must pass the parameter by copy.
1521 if Nkind (Actual) = N_Type_Conversion
1523 (Is_Numeric_Type (E_Formal)
1524 or else Is_Access_Type (E_Formal)
1525 or else Is_Enumeration_Type (E_Formal)
1526 or else Is_Bit_Packed_Array (Etype (Formal))
1527 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1529 -- Also pass by copy if change of representation
1531 or else not Same_Representation
1533 Etype (Expression (Actual))))
1535 Add_Call_By_Copy_Code;
1537 -- References to components of bit packed arrays are expanded
1538 -- at this point, rather than at the point of analysis of the
1539 -- actuals, to handle the expansion of the assignment to
1540 -- [in] out parameters.
1542 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1543 Add_Simple_Call_By_Copy_Code;
1545 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1546 -- because the back-end cannot cope with such objects. In other
1547 -- cases where alignment forces a copy, the back-end generates
1548 -- it properly. It should not be generated unconditionally in the
1549 -- front-end because it does not know precisely the alignment
1550 -- requirements of the target, and makes too conservative an
1551 -- estimate, leading to superfluous copies or spurious errors
1552 -- on by-reference parameters.
1554 elsif Nkind (Actual) = N_Selected_Component
1556 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1557 and then not Represented_As_Scalar (Etype (Formal))
1559 Add_Simple_Call_By_Copy_Code;
1561 -- References to slices of bit packed arrays are expanded
1563 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1564 Add_Call_By_Copy_Code;
1566 -- References to possibly unaligned slices of arrays are expanded
1568 elsif Is_Possibly_Unaligned_Slice (Actual) then
1569 Add_Call_By_Copy_Code;
1571 -- Deal with access types where the actual subtype and the
1572 -- formal subtype are not the same, requiring a check.
1574 -- It is necessary to exclude tagged types because of "downward
1575 -- conversion" errors.
1577 elsif Is_Access_Type (E_Formal)
1578 and then not Same_Type (E_Formal, Etype (Actual))
1579 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1581 Add_Call_By_Copy_Code;
1583 -- If the actual is not a scalar and is marked for volatile
1584 -- treatment, whereas the formal is not volatile, then pass
1585 -- by copy unless it is a by-reference type.
1587 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1588 -- because this is the enforcement of a language rule that applies
1589 -- only to "real" volatile variables, not e.g. to the address
1590 -- clause overlay case.
1592 elsif Is_Entity_Name (Actual)
1593 and then Is_Volatile (Entity (Actual))
1594 and then not Is_By_Reference_Type (Etype (Actual))
1595 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1596 and then not Is_Volatile (E_Formal)
1598 Add_Call_By_Copy_Code;
1600 elsif Nkind (Actual) = N_Indexed_Component
1601 and then Is_Entity_Name (Prefix (Actual))
1602 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1604 Add_Call_By_Copy_Code;
1606 -- Add call-by-copy code for the case of scalar out parameters
1607 -- when it is not known at compile time that the subtype of the
1608 -- formal is a subrange of the subtype of the actual (or vice
1609 -- versa for in out parameters), in order to get range checks
1610 -- on such actuals. (Maybe this case should be handled earlier
1611 -- in the if statement???)
1613 elsif Is_Scalar_Type (E_Formal)
1615 (not In_Subrange_Of (E_Formal, Etype (Actual))
1617 (Ekind (Formal) = E_In_Out_Parameter
1618 and then not In_Subrange_Of (Etype (Actual), E_Formal)))
1620 -- Perhaps the setting back to False should be done within
1621 -- Add_Call_By_Copy_Code, since it could get set on other
1622 -- cases occurring above???
1624 if Do_Range_Check (Actual) then
1625 Set_Do_Range_Check (Actual, False);
1628 Add_Call_By_Copy_Code;
1631 -- Processing for IN parameters
1634 -- For IN parameters is in the packed array case, we expand an
1635 -- indexed component (the circuit in Exp_Ch4 deliberately left
1636 -- indexed components appearing as actuals untouched, so that
1637 -- the special processing above for the OUT and IN OUT cases
1638 -- could be performed. We could make the test in Exp_Ch4 more
1639 -- complex and have it detect the parameter mode, but it is
1640 -- easier simply to handle all cases here.)
1642 if Nkind (Actual) = N_Indexed_Component
1643 and then Is_Packed (Etype (Prefix (Actual)))
1645 Reset_Packed_Prefix;
1646 Expand_Packed_Element_Reference (Actual);
1648 -- If we have a reference to a bit packed array, we copy it, since
1649 -- the actual must be byte aligned.
1651 -- Is this really necessary in all cases???
1653 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1654 Add_Simple_Call_By_Copy_Code;
1656 -- If a non-scalar actual is possibly unaligned, we need a copy
1658 elsif Is_Possibly_Unaligned_Object (Actual)
1659 and then not Represented_As_Scalar (Etype (Formal))
1661 Add_Simple_Call_By_Copy_Code;
1663 -- Similarly, we have to expand slices of packed arrays here
1664 -- because the result must be byte aligned.
1666 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1667 Add_Call_By_Copy_Code;
1669 -- Only processing remaining is to pass by copy if this is a
1670 -- reference to a possibly unaligned slice, since the caller
1671 -- expects an appropriately aligned argument.
1673 elsif Is_Possibly_Unaligned_Slice (Actual) then
1674 Add_Call_By_Copy_Code;
1676 -- An unusual case: a current instance of an enclosing task can be
1677 -- an actual, and must be replaced by a reference to self.
1679 elsif Is_Entity_Name (Actual)
1680 and then Is_Task_Type (Entity (Actual))
1682 if In_Open_Scopes (Entity (Actual)) then
1684 (Make_Function_Call (Loc,
1685 Name => New_Reference_To (RTE (RE_Self), Loc))));
1688 -- A task type cannot otherwise appear as an actual
1691 raise Program_Error;
1696 Next_Formal (Formal);
1697 Next_Actual (Actual);
1700 -- Find right place to put post call stuff if it is present
1702 if not Is_Empty_List (Post_Call) then
1704 -- If call is not a list member, it must be the triggering statement
1705 -- of a triggering alternative or an entry call alternative, and we
1706 -- can add the post call stuff to the corresponding statement list.
1708 if not Is_List_Member (N) then
1710 P : constant Node_Id := Parent (N);
1713 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1714 N_Entry_Call_Alternative));
1716 if Is_Non_Empty_List (Statements (P)) then
1717 Insert_List_Before_And_Analyze
1718 (First (Statements (P)), Post_Call);
1720 Set_Statements (P, Post_Call);
1724 -- Otherwise, normal case where N is in a statement sequence,
1725 -- just put the post-call stuff after the call statement.
1728 Insert_Actions_After (N, Post_Call);
1732 -- The call node itself is re-analyzed in Expand_Call
1740 -- This procedure handles expansion of function calls and procedure call
1741 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1742 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1744 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1745 -- Provide values of actuals for all formals in Extra_Formals list
1746 -- Replace "call" to enumeration literal function by literal itself
1747 -- Rewrite call to predefined operator as operator
1748 -- Replace actuals to in-out parameters that are numeric conversions,
1749 -- with explicit assignment to temporaries before and after the call.
1750 -- Remove optional actuals if First_Optional_Parameter specified.
1752 -- Note that the list of actuals has been filled with default expressions
1753 -- during semantic analysis of the call. Only the extra actuals required
1754 -- for the 'Constrained attribute and for accessibility checks are added
1757 procedure Expand_Call (N : Node_Id) is
1758 Loc : constant Source_Ptr := Sloc (N);
1759 Call_Node : Node_Id := N;
1760 Extra_Actuals : List_Id := No_List;
1761 Prev : Node_Id := Empty;
1763 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1764 -- Adds one entry to the end of the actual parameter list. Used for
1765 -- default parameters and for extra actuals (for Extra_Formals). The
1766 -- argument is an N_Parameter_Association node.
1768 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1769 -- Adds an extra actual to the list of extra actuals. Expr is the
1770 -- expression for the value of the actual, EF is the entity for the
1773 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1774 -- Within an instance, a type derived from a non-tagged formal derived
1775 -- type inherits from the original parent, not from the actual. The
1776 -- current derivation mechanism has the derived type inherit from the
1777 -- actual, which is only correct outside of the instance. If the
1778 -- subprogram is inherited, we test for this particular case through a
1779 -- convoluted tree traversal before setting the proper subprogram to be
1782 --------------------------
1783 -- Add_Actual_Parameter --
1784 --------------------------
1786 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1787 Actual_Expr : constant Node_Id :=
1788 Explicit_Actual_Parameter (Insert_Param);
1791 -- Case of insertion is first named actual
1793 if No (Prev) or else
1794 Nkind (Parent (Prev)) /= N_Parameter_Association
1796 Set_Next_Named_Actual
1797 (Insert_Param, First_Named_Actual (Call_Node));
1798 Set_First_Named_Actual (Call_Node, Actual_Expr);
1801 if No (Parameter_Associations (Call_Node)) then
1802 Set_Parameter_Associations (Call_Node, New_List);
1803 Append (Insert_Param, Parameter_Associations (Call_Node));
1806 Insert_After (Prev, Insert_Param);
1809 -- Case of insertion is not first named actual
1812 Set_Next_Named_Actual
1813 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1814 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1815 Append (Insert_Param, Parameter_Associations (Call_Node));
1818 Prev := Actual_Expr;
1819 end Add_Actual_Parameter;
1821 ----------------------
1822 -- Add_Extra_Actual --
1823 ----------------------
1825 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1826 Loc : constant Source_Ptr := Sloc (Expr);
1829 if Extra_Actuals = No_List then
1830 Extra_Actuals := New_List;
1831 Set_Parent (Extra_Actuals, Call_Node);
1834 Append_To (Extra_Actuals,
1835 Make_Parameter_Association (Loc,
1836 Selector_Name => Make_Identifier (Loc, Chars (EF)),
1837 Explicit_Actual_Parameter => Expr));
1839 Analyze_And_Resolve (Expr, Etype (EF));
1841 if Nkind (Call_Node) = N_Function_Call then
1842 Set_Is_Accessibility_Actual (Parent (Expr));
1844 end Add_Extra_Actual;
1846 ---------------------------
1847 -- Inherited_From_Formal --
1848 ---------------------------
1850 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1852 Gen_Par : Entity_Id;
1853 Gen_Prim : Elist_Id;
1858 -- If the operation is inherited, it is attached to the corresponding
1859 -- type derivation. If the parent in the derivation is a generic
1860 -- actual, it is a subtype of the actual, and we have to recover the
1861 -- original derived type declaration to find the proper parent.
1863 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1864 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1865 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1866 N_Derived_Type_Definition
1867 or else not In_Instance
1874 (Type_Definition (Original_Node (Parent (S))));
1876 if Nkind (Indic) = N_Subtype_Indication then
1877 Par := Entity (Subtype_Mark (Indic));
1879 Par := Entity (Indic);
1883 if not Is_Generic_Actual_Type (Par)
1884 or else Is_Tagged_Type (Par)
1885 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1886 or else not In_Open_Scopes (Scope (Par))
1890 Gen_Par := Generic_Parent_Type (Parent (Par));
1893 -- If the actual has no generic parent type, the formal is not
1894 -- a formal derived type, so nothing to inherit.
1896 if No (Gen_Par) then
1900 -- If the generic parent type is still the generic type, this is a
1901 -- private formal, not a derived formal, and there are no operations
1902 -- inherited from the formal.
1904 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1908 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1910 Elmt := First_Elmt (Gen_Prim);
1911 while Present (Elmt) loop
1912 if Chars (Node (Elmt)) = Chars (S) then
1918 F1 := First_Formal (S);
1919 F2 := First_Formal (Node (Elmt));
1921 and then Present (F2)
1923 if Etype (F1) = Etype (F2)
1924 or else Etype (F2) = Gen_Par
1930 exit; -- not the right subprogram
1942 raise Program_Error;
1943 end Inherited_From_Formal;
1947 Remote : constant Boolean := Is_Remote_Call (Call_Node);
1950 Orig_Subp : Entity_Id := Empty;
1951 Param_Count : Natural := 0;
1952 Parent_Formal : Entity_Id;
1953 Parent_Subp : Entity_Id;
1957 Prev_Orig : Node_Id;
1958 -- Original node for an actual, which may have been rewritten. If the
1959 -- actual is a function call that has been transformed from a selected
1960 -- component, the original node is unanalyzed. Otherwise, it carries
1961 -- semantic information used to generate additional actuals.
1963 CW_Interface_Formals_Present : Boolean := False;
1965 -- Start of processing for Expand_Call
1968 -- Ignore if previous error
1970 if Nkind (Call_Node) in N_Has_Etype
1971 and then Etype (Call_Node) = Any_Type
1976 -- Call using access to subprogram with explicit dereference
1978 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
1979 Subp := Etype (Name (Call_Node));
1980 Parent_Subp := Empty;
1982 -- Case of call to simple entry, where the Name is a selected component
1983 -- whose prefix is the task, and whose selector name is the entry name
1985 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
1986 Subp := Entity (Selector_Name (Name (Call_Node)));
1987 Parent_Subp := Empty;
1989 -- Case of call to member of entry family, where Name is an indexed
1990 -- component, with the prefix being a selected component giving the
1991 -- task and entry family name, and the index being the entry index.
1993 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
1994 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
1995 Parent_Subp := Empty;
2000 Subp := Entity (Name (Call_Node));
2001 Parent_Subp := Alias (Subp);
2003 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2004 -- if we can tell that the first parameter cannot possibly be null.
2005 -- This improves efficiency by avoiding a run-time test.
2007 -- We do not do this if Raise_Exception_Always does not exist, which
2008 -- can happen in configurable run time profiles which provide only a
2011 if Is_RTE (Subp, RE_Raise_Exception)
2012 and then RTE_Available (RE_Raise_Exception_Always)
2015 FA : constant Node_Id := Original_Node
2016 (First_Actual (Call_Node));
2018 -- The case we catch is where the first argument is obtained
2019 -- using the Identity attribute (which must always be
2022 if Nkind (FA) = N_Attribute_Reference
2023 and then Attribute_Name (FA) = Name_Identity
2025 Subp := RTE (RE_Raise_Exception_Always);
2026 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2031 if Ekind (Subp) = E_Entry then
2032 Parent_Subp := Empty;
2036 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2037 -- alternative in an asynchronous select or as an entry call in
2038 -- a conditional or timed select. Check whether the procedure call
2039 -- is a renaming of an entry and rewrite it as an entry call.
2041 if Ada_Version >= Ada_2005
2042 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2044 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2045 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2047 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2048 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2052 Ren_Root : Entity_Id := Subp;
2055 -- This may be a chain of renamings, find the root
2057 if Present (Alias (Ren_Root)) then
2058 Ren_Root := Alias (Ren_Root);
2061 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2062 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2064 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2066 Make_Entry_Call_Statement (Loc,
2068 New_Copy_Tree (Name (Ren_Decl)),
2069 Parameter_Associations =>
2071 (Parameter_Associations (Call_Node))));
2079 -- First step, compute extra actuals, corresponding to any Extra_Formals
2080 -- present. Note that we do not access Extra_Formals directly, instead
2081 -- we simply note the presence of the extra formals as we process the
2082 -- regular formals collecting corresponding actuals in Extra_Actuals.
2084 -- We also generate any required range checks for actuals for in formals
2085 -- as we go through the loop, since this is a convenient place to do it.
2086 -- (Though it seems that this would be better done in Expand_Actuals???)
2088 Formal := First_Formal (Subp);
2089 Actual := First_Actual (Call_Node);
2091 while Present (Formal) loop
2093 -- Generate range check if required
2095 if Do_Range_Check (Actual)
2096 and then Ekind (Formal) = E_In_Parameter
2098 Set_Do_Range_Check (Actual, False);
2099 Generate_Range_Check
2100 (Actual, Etype (Formal), CE_Range_Check_Failed);
2103 -- Prepare to examine current entry
2106 Prev_Orig := Original_Node (Prev);
2108 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2109 -- to expand it in a further round.
2111 CW_Interface_Formals_Present :=
2112 CW_Interface_Formals_Present
2114 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2115 and then Is_Interface (Etype (Etype (Formal))))
2117 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2118 and then Is_Interface (Directly_Designated_Type
2119 (Etype (Etype (Formal)))));
2121 -- Create possible extra actual for constrained case. Usually, the
2122 -- extra actual is of the form actual'constrained, but since this
2123 -- attribute is only available for unconstrained records, TRUE is
2124 -- expanded if the type of the formal happens to be constrained (for
2125 -- instance when this procedure is inherited from an unconstrained
2126 -- record to a constrained one) or if the actual has no discriminant
2127 -- (its type is constrained). An exception to this is the case of a
2128 -- private type without discriminants. In this case we pass FALSE
2129 -- because the object has underlying discriminants with defaults.
2131 if Present (Extra_Constrained (Formal)) then
2132 if Ekind (Etype (Prev)) in Private_Kind
2133 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2136 (New_Occurrence_Of (Standard_False, Loc),
2137 Extra_Constrained (Formal));
2139 elsif Is_Constrained (Etype (Formal))
2140 or else not Has_Discriminants (Etype (Prev))
2143 (New_Occurrence_Of (Standard_True, Loc),
2144 Extra_Constrained (Formal));
2146 -- Do not produce extra actuals for Unchecked_Union parameters.
2147 -- Jump directly to the end of the loop.
2149 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2150 goto Skip_Extra_Actual_Generation;
2153 -- If the actual is a type conversion, then the constrained
2154 -- test applies to the actual, not the target type.
2160 -- Test for unchecked conversions as well, which can occur
2161 -- as out parameter actuals on calls to stream procedures.
2164 while Nkind_In (Act_Prev, N_Type_Conversion,
2165 N_Unchecked_Type_Conversion)
2167 Act_Prev := Expression (Act_Prev);
2170 -- If the expression is a conversion of a dereference, this
2171 -- is internally generated code that manipulates addresses,
2172 -- e.g. when building interface tables. No check should
2173 -- occur in this case, and the discriminated object is not
2176 if not Comes_From_Source (Actual)
2177 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2178 and then Nkind (Act_Prev) = N_Explicit_Dereference
2181 (New_Occurrence_Of (Standard_False, Loc),
2182 Extra_Constrained (Formal));
2186 (Make_Attribute_Reference (Sloc (Prev),
2188 Duplicate_Subexpr_No_Checks
2189 (Act_Prev, Name_Req => True),
2190 Attribute_Name => Name_Constrained),
2191 Extra_Constrained (Formal));
2197 -- Create possible extra actual for accessibility level
2199 if Present (Extra_Accessibility (Formal)) then
2201 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2202 -- attribute, then the original actual may be an aliased object
2203 -- occurring as the prefix in a call using "Object.Operation"
2204 -- notation. In that case we must pass the level of the object,
2205 -- so Prev_Orig is reset to Prev and the attribute will be
2206 -- processed by the code for Access attributes further below.
2208 if Prev_Orig /= Prev
2209 and then Nkind (Prev) = N_Attribute_Reference
2211 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2212 and then Is_Aliased_View (Prev_Orig)
2217 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2218 -- accessibility levels.
2220 if Ekind (Current_Scope) in Subprogram_Kind
2221 and then Is_Thunk (Current_Scope)
2224 Parm_Ent : Entity_Id;
2227 if Is_Controlling_Actual (Actual) then
2229 -- Find the corresponding actual of the thunk
2231 Parm_Ent := First_Entity (Current_Scope);
2232 for J in 2 .. Param_Count loop
2233 Next_Entity (Parm_Ent);
2236 else pragma Assert (Is_Entity_Name (Actual));
2237 Parm_Ent := Entity (Actual);
2241 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2242 Extra_Accessibility (Formal));
2245 elsif Is_Entity_Name (Prev_Orig) then
2247 -- When passing an access parameter, or a renaming of an access
2248 -- parameter, as the actual to another access parameter we need
2249 -- to pass along the actual's own access level parameter. This
2250 -- is done if we are within the scope of the formal access
2251 -- parameter (if this is an inlined body the extra formal is
2254 if (Is_Formal (Entity (Prev_Orig))
2256 (Present (Renamed_Object (Entity (Prev_Orig)))
2258 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2261 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2262 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2263 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2266 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2269 pragma Assert (Present (Parm_Ent));
2271 if Present (Extra_Accessibility (Parm_Ent)) then
2274 (Extra_Accessibility (Parm_Ent), Loc),
2275 Extra_Accessibility (Formal));
2277 -- If the actual access parameter does not have an
2278 -- associated extra formal providing its scope level,
2279 -- then treat the actual as having library-level
2284 (Make_Integer_Literal (Loc,
2285 Intval => Scope_Depth (Standard_Standard)),
2286 Extra_Accessibility (Formal));
2290 -- The actual is a normal access value, so just pass the level
2291 -- of the actual's access type.
2295 (Make_Integer_Literal (Loc,
2296 Intval => Type_Access_Level (Etype (Prev_Orig))),
2297 Extra_Accessibility (Formal));
2300 -- If the actual is an access discriminant, then pass the level
2301 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2303 elsif Nkind (Prev_Orig) = N_Selected_Component
2304 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2306 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2307 E_Anonymous_Access_Type
2310 (Make_Integer_Literal (Loc,
2311 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2312 Extra_Accessibility (Formal));
2317 case Nkind (Prev_Orig) is
2319 when N_Attribute_Reference =>
2320 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2322 -- For X'Access, pass on the level of the prefix X
2324 when Attribute_Access =>
2326 (Make_Integer_Literal (Loc,
2329 (Prefix (Prev_Orig))),
2330 Extra_Accessibility (Formal));
2332 -- Treat the unchecked attributes as library-level
2334 when Attribute_Unchecked_Access |
2335 Attribute_Unrestricted_Access =>
2337 (Make_Integer_Literal (Loc,
2338 Intval => Scope_Depth (Standard_Standard)),
2339 Extra_Accessibility (Formal));
2341 -- No other cases of attributes returning access
2342 -- values that can be passed to access parameters.
2345 raise Program_Error;
2349 -- For allocators we pass the level of the execution of the
2350 -- called subprogram, which is one greater than the current
2355 (Make_Integer_Literal (Loc,
2356 Intval => Scope_Depth (Current_Scope) + 1),
2357 Extra_Accessibility (Formal));
2359 -- For other cases we simply pass the level of the actual's
2360 -- access type. The type is retrieved from Prev rather than
2361 -- Prev_Orig, because in some cases Prev_Orig denotes an
2362 -- original expression that has not been analyzed.
2366 (Make_Integer_Literal (Loc,
2367 Intval => Type_Access_Level (Etype (Prev))),
2368 Extra_Accessibility (Formal));
2373 -- Perform the check of 4.6(49) that prevents a null value from being
2374 -- passed as an actual to an access parameter. Note that the check
2375 -- is elided in the common cases of passing an access attribute or
2376 -- access parameter as an actual. Also, we currently don't enforce
2377 -- this check for expander-generated actuals and when -gnatdj is set.
2379 if Ada_Version >= Ada_2005 then
2381 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2382 -- the intent of 6.4.1(13) is that null-exclusion checks should
2383 -- not be done for 'out' parameters, even though it refers only
2384 -- to constraint checks, and a null_exlusion is not a constraint.
2385 -- Note that AI05-0196-1 corrects this mistake in the RM.
2387 if Is_Access_Type (Etype (Formal))
2388 and then Can_Never_Be_Null (Etype (Formal))
2389 and then Ekind (Formal) /= E_Out_Parameter
2390 and then Nkind (Prev) /= N_Raise_Constraint_Error
2391 and then (Known_Null (Prev)
2392 or else not Can_Never_Be_Null (Etype (Prev)))
2394 Install_Null_Excluding_Check (Prev);
2397 -- Ada_Version < Ada_2005
2400 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2401 or else Access_Checks_Suppressed (Subp)
2405 elsif Debug_Flag_J then
2408 elsif not Comes_From_Source (Prev) then
2411 elsif Is_Entity_Name (Prev)
2412 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2416 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2419 -- Suppress null checks when passing to access parameters of Java
2420 -- and CIL subprograms. (Should this be done for other foreign
2421 -- conventions as well ???)
2423 elsif Convention (Subp) = Convention_Java
2424 or else Convention (Subp) = Convention_CIL
2429 Install_Null_Excluding_Check (Prev);
2433 -- Perform appropriate validity checks on parameters that
2436 if Validity_Checks_On then
2437 if (Ekind (Formal) = E_In_Parameter
2438 and then Validity_Check_In_Params)
2440 (Ekind (Formal) = E_In_Out_Parameter
2441 and then Validity_Check_In_Out_Params)
2443 -- If the actual is an indexed component of a packed type (or
2444 -- is an indexed or selected component whose prefix recursively
2445 -- meets this condition), it has not been expanded yet. It will
2446 -- be copied in the validity code that follows, and has to be
2447 -- expanded appropriately, so reanalyze it.
2449 -- What we do is just to unset analyzed bits on prefixes till
2450 -- we reach something that does not have a prefix.
2457 while Nkind_In (Nod, N_Indexed_Component,
2458 N_Selected_Component)
2460 Set_Analyzed (Nod, False);
2461 Nod := Prefix (Nod);
2465 Ensure_Valid (Actual);
2469 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2470 -- since this is a left side reference. We only do this for calls
2471 -- from the source program since we assume that compiler generated
2472 -- calls explicitly generate any required checks. We also need it
2473 -- only if we are doing standard validity checks, since clearly it is
2474 -- not needed if validity checks are off, and in subscript validity
2475 -- checking mode, all indexed components are checked with a call
2476 -- directly from Expand_N_Indexed_Component.
2478 if Comes_From_Source (Call_Node)
2479 and then Ekind (Formal) /= E_In_Parameter
2480 and then Validity_Checks_On
2481 and then Validity_Check_Default
2482 and then not Validity_Check_Subscripts
2484 Check_Valid_Lvalue_Subscripts (Actual);
2487 -- Mark any scalar OUT parameter that is a simple variable as no
2488 -- longer known to be valid (unless the type is always valid). This
2489 -- reflects the fact that if an OUT parameter is never set in a
2490 -- procedure, then it can become invalid on the procedure return.
2492 if Ekind (Formal) = E_Out_Parameter
2493 and then Is_Entity_Name (Actual)
2494 and then Ekind (Entity (Actual)) = E_Variable
2495 and then not Is_Known_Valid (Etype (Actual))
2497 Set_Is_Known_Valid (Entity (Actual), False);
2500 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2501 -- clear current values, since they can be clobbered. We are probably
2502 -- doing this in more places than we need to, but better safe than
2503 -- sorry when it comes to retaining bad current values!
2505 if Ekind (Formal) /= E_In_Parameter
2506 and then Is_Entity_Name (Actual)
2507 and then Present (Entity (Actual))
2510 Ent : constant Entity_Id := Entity (Actual);
2514 -- For an OUT or IN OUT parameter that is an assignable entity,
2515 -- we do not want to clobber the Last_Assignment field, since
2516 -- if it is set, it was precisely because it is indeed an OUT
2517 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
2518 -- since the subprogram could have returned in invalid value.
2520 if (Ekind (Formal) = E_Out_Parameter
2522 Ekind (Formal) = E_In_Out_Parameter)
2523 and then Is_Assignable (Ent)
2525 Sav := Last_Assignment (Ent);
2526 Kill_Current_Values (Ent);
2527 Set_Last_Assignment (Ent, Sav);
2528 Set_Is_Known_Valid (Ent, False);
2530 -- For all other cases, just kill the current values
2533 Kill_Current_Values (Ent);
2538 -- If the formal is class wide and the actual is an aggregate, force
2539 -- evaluation so that the back end who does not know about class-wide
2540 -- type, does not generate a temporary of the wrong size.
2542 if not Is_Class_Wide_Type (Etype (Formal)) then
2545 elsif Nkind (Actual) = N_Aggregate
2546 or else (Nkind (Actual) = N_Qualified_Expression
2547 and then Nkind (Expression (Actual)) = N_Aggregate)
2549 Force_Evaluation (Actual);
2552 -- In a remote call, if the formal is of a class-wide type, check
2553 -- that the actual meets the requirements described in E.4(18).
2555 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
2556 Insert_Action (Actual,
2557 Make_Transportable_Check (Loc,
2558 Duplicate_Subexpr_Move_Checks (Actual)));
2561 -- This label is required when skipping extra actual generation for
2562 -- Unchecked_Union parameters.
2564 <<Skip_Extra_Actual_Generation>>
2566 Param_Count := Param_Count + 1;
2567 Next_Actual (Actual);
2568 Next_Formal (Formal);
2571 -- If we are expanding a rhs of an assignment we need to check if tag
2572 -- propagation is needed. You might expect this processing to be in
2573 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
2574 -- assignment might be transformed to a declaration for an unconstrained
2575 -- value if the expression is classwide.
2577 if Nkind (Call_Node) = N_Function_Call
2578 and then Is_Tag_Indeterminate (Call_Node)
2579 and then Is_Entity_Name (Name (Call_Node))
2582 Ass : Node_Id := Empty;
2585 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
2586 Ass := Parent (Call_Node);
2588 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
2589 and then Nkind (Parent (Parent (Call_Node)))
2590 = N_Assignment_Statement
2592 Ass := Parent (Parent (Call_Node));
2594 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
2595 and then Nkind (Parent (Parent (Call_Node)))
2596 = N_Assignment_Statement
2598 Ass := Parent (Parent (Call_Node));
2602 and then Is_Class_Wide_Type (Etype (Name (Ass)))
2604 if Is_Access_Type (Etype (Call_Node)) then
2605 if Designated_Type (Etype (Call_Node)) /=
2606 Root_Type (Etype (Name (Ass)))
2609 ("tag-indeterminate expression "
2610 & " must have designated type& (RM 5.2 (6))",
2611 Call_Node, Root_Type (Etype (Name (Ass))));
2613 Propagate_Tag (Name (Ass), Call_Node);
2616 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
2618 ("tag-indeterminate expression must have type&"
2620 Call_Node, Root_Type (Etype (Name (Ass))));
2623 Propagate_Tag (Name (Ass), Call_Node);
2626 -- The call will be rewritten as a dispatching call, and
2627 -- expanded as such.
2634 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
2635 -- it to point to the correct secondary virtual table
2637 if Nkind_In (Call_Node, N_Function_Call, N_Procedure_Call_Statement)
2638 and then CW_Interface_Formals_Present
2640 Expand_Interface_Actuals (Call_Node);
2643 -- Deals with Dispatch_Call if we still have a call, before expanding
2644 -- extra actuals since this will be done on the re-analysis of the
2645 -- dispatching call. Note that we do not try to shorten the actual list
2646 -- for a dispatching call, it would not make sense to do so. Expansion
2647 -- of dispatching calls is suppressed when VM_Target, because the VM
2648 -- back-ends directly handle the generation of dispatching calls and
2649 -- would have to undo any expansion to an indirect call.
2651 if Nkind_In (Call_Node, N_Function_Call, N_Procedure_Call_Statement)
2652 and then Present (Controlling_Argument (Call_Node))
2655 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
2656 Eq_Prim_Op : Entity_Id := Empty;
2659 if not Is_Limited_Type (Typ) then
2660 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
2663 if Tagged_Type_Expansion then
2664 Expand_Dispatching_Call (Call_Node);
2666 -- The following return is worrisome. Is it really OK to skip
2667 -- all remaining processing in this procedure ???
2674 Apply_Tag_Checks (Call_Node);
2676 -- Expansion of a dispatching call results in an indirect call,
2677 -- which in turn causes current values to be killed (see
2678 -- Resolve_Call), so on VM targets we do the call here to
2679 -- ensure consistent warnings between VM and non-VM targets.
2681 Kill_Current_Values;
2684 -- If this is a dispatching "=" then we must update the reference
2685 -- to the call node because we generated:
2686 -- x.tag = y.tag and then x = y
2688 if Subp = Eq_Prim_Op
2689 and then Nkind (Call_Node) = N_Op_And
2691 Call_Node := Right_Opnd (Call_Node);
2696 -- Similarly, expand calls to RCI subprograms on which pragma
2697 -- All_Calls_Remote applies. The rewriting will be reanalyzed
2698 -- later. Do this only when the call comes from source since we
2699 -- do not want such a rewriting to occur in expanded code.
2701 if Is_All_Remote_Call (Call_Node) then
2702 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
2704 -- Similarly, do not add extra actuals for an entry call whose entity
2705 -- is a protected procedure, or for an internal protected subprogram
2706 -- call, because it will be rewritten as a protected subprogram call
2707 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
2709 elsif Is_Protected_Type (Scope (Subp))
2710 and then (Ekind (Subp) = E_Procedure
2711 or else Ekind (Subp) = E_Function)
2715 -- During that loop we gathered the extra actuals (the ones that
2716 -- correspond to Extra_Formals), so now they can be appended.
2719 while Is_Non_Empty_List (Extra_Actuals) loop
2720 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2724 -- At this point we have all the actuals, so this is the point at which
2725 -- the various expansion activities for actuals is carried out.
2727 Expand_Actuals (Call_Node, Subp);
2729 -- If the subprogram is a renaming, or if it is inherited, replace it in
2730 -- the call with the name of the actual subprogram being called. If this
2731 -- is a dispatching call, the run-time decides what to call. The Alias
2732 -- attribute does not apply to entries.
2734 if Nkind (Call_Node) /= N_Entry_Call_Statement
2735 and then No (Controlling_Argument (Call_Node))
2736 and then Present (Parent_Subp)
2738 if Present (Inherited_From_Formal (Subp)) then
2739 Parent_Subp := Inherited_From_Formal (Subp);
2741 Parent_Subp := Ultimate_Alias (Parent_Subp);
2744 -- The below setting of Entity is suspect, see F109-018 discussion???
2746 Set_Entity (Name (Call_Node), Parent_Subp);
2748 if Is_Abstract_Subprogram (Parent_Subp)
2749 and then not In_Instance
2752 ("cannot call abstract subprogram &!",
2753 Name (Call_Node), Parent_Subp);
2756 -- Inspect all formals of derived subprogram Subp. Compare parameter
2757 -- types with the parent subprogram and check whether an actual may
2758 -- need a type conversion to the corresponding formal of the parent
2761 -- Not clear whether intrinsic subprograms need such conversions. ???
2763 if not Is_Intrinsic_Subprogram (Parent_Subp)
2764 or else Is_Generic_Instance (Parent_Subp)
2767 procedure Convert (Act : Node_Id; Typ : Entity_Id);
2768 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
2769 -- and resolve the newly generated construct.
2775 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
2777 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
2784 Actual_Typ : Entity_Id;
2785 Formal_Typ : Entity_Id;
2786 Parent_Typ : Entity_Id;
2789 Actual := First_Actual (Call_Node);
2790 Formal := First_Formal (Subp);
2791 Parent_Formal := First_Formal (Parent_Subp);
2792 while Present (Formal) loop
2793 Actual_Typ := Etype (Actual);
2794 Formal_Typ := Etype (Formal);
2795 Parent_Typ := Etype (Parent_Formal);
2797 -- For an IN parameter of a scalar type, the parent formal
2798 -- type and derived formal type differ or the parent formal
2799 -- type and actual type do not match statically.
2801 if Is_Scalar_Type (Formal_Typ)
2802 and then Ekind (Formal) = E_In_Parameter
2803 and then Formal_Typ /= Parent_Typ
2805 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
2806 and then not Raises_Constraint_Error (Actual)
2808 Convert (Actual, Parent_Typ);
2809 Enable_Range_Check (Actual);
2811 -- If the actual has been marked as requiring a range
2812 -- check, then generate it here.
2814 if Do_Range_Check (Actual) then
2815 Set_Do_Range_Check (Actual, False);
2816 Generate_Range_Check
2817 (Actual, Etype (Formal), CE_Range_Check_Failed);
2820 -- For access types, the parent formal type and actual type
2823 elsif Is_Access_Type (Formal_Typ)
2824 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
2826 if Ekind (Formal) /= E_In_Parameter then
2827 Convert (Actual, Parent_Typ);
2829 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
2830 and then Designated_Type (Parent_Typ) /=
2831 Designated_Type (Actual_Typ)
2832 and then not Is_Controlling_Formal (Formal)
2834 -- This unchecked conversion is not necessary unless
2835 -- inlining is enabled, because in that case the type
2836 -- mismatch may become visible in the body about to be
2840 Unchecked_Convert_To (Parent_Typ,
2841 Relocate_Node (Actual)));
2843 Resolve (Actual, Parent_Typ);
2846 -- For array and record types, the parent formal type and
2847 -- derived formal type have different sizes or pragma Pack
2850 elsif ((Is_Array_Type (Formal_Typ)
2851 and then Is_Array_Type (Parent_Typ))
2853 (Is_Record_Type (Formal_Typ)
2854 and then Is_Record_Type (Parent_Typ)))
2856 (Esize (Formal_Typ) /= Esize (Parent_Typ)
2857 or else Has_Pragma_Pack (Formal_Typ) /=
2858 Has_Pragma_Pack (Parent_Typ))
2860 Convert (Actual, Parent_Typ);
2863 Next_Actual (Actual);
2864 Next_Formal (Formal);
2865 Next_Formal (Parent_Formal);
2871 Subp := Parent_Subp;
2874 -- Check for violation of No_Abort_Statements
2876 if Is_RTE (Subp, RE_Abort_Task) then
2877 Check_Restriction (No_Abort_Statements, Call_Node);
2879 -- Check for violation of No_Dynamic_Attachment
2881 elsif RTU_Loaded (Ada_Interrupts)
2882 and then (Is_RTE (Subp, RE_Is_Reserved) or else
2883 Is_RTE (Subp, RE_Is_Attached) or else
2884 Is_RTE (Subp, RE_Current_Handler) or else
2885 Is_RTE (Subp, RE_Attach_Handler) or else
2886 Is_RTE (Subp, RE_Exchange_Handler) or else
2887 Is_RTE (Subp, RE_Detach_Handler) or else
2888 Is_RTE (Subp, RE_Reference))
2890 Check_Restriction (No_Dynamic_Attachment, Call_Node);
2893 -- Deal with case where call is an explicit dereference
2895 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2897 -- Handle case of access to protected subprogram type
2899 if Is_Access_Protected_Subprogram_Type
2900 (Base_Type (Etype (Prefix (Name (Call_Node)))))
2902 -- If this is a call through an access to protected operation, the
2903 -- prefix has the form (object'address, operation'access). Rewrite
2904 -- as a for other protected calls: the object is the 1st parameter
2905 -- of the list of actuals.
2912 Ptr : constant Node_Id := Prefix (Name (Call_Node));
2914 T : constant Entity_Id :=
2915 Equivalent_Type (Base_Type (Etype (Ptr)));
2917 D_T : constant Entity_Id :=
2918 Designated_Type (Base_Type (Etype (Ptr)));
2922 Make_Selected_Component (Loc,
2923 Prefix => Unchecked_Convert_To (T, Ptr),
2925 New_Occurrence_Of (First_Entity (T), Loc));
2928 Make_Selected_Component (Loc,
2929 Prefix => Unchecked_Convert_To (T, Ptr),
2931 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
2934 Make_Explicit_Dereference (Loc,
2937 if Present (Parameter_Associations (Call_Node)) then
2938 Parm := Parameter_Associations (Call_Node);
2943 Prepend (Obj, Parm);
2945 if Etype (D_T) = Standard_Void_Type then
2947 Make_Procedure_Call_Statement (Loc,
2949 Parameter_Associations => Parm);
2952 Make_Function_Call (Loc,
2954 Parameter_Associations => Parm);
2957 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
2958 Set_Etype (Call, Etype (D_T));
2960 -- We do not re-analyze the call to avoid infinite recursion.
2961 -- We analyze separately the prefix and the object, and set
2962 -- the checks on the prefix that would otherwise be emitted
2963 -- when resolving a call.
2965 Rewrite (Call_Node, Call);
2967 Apply_Access_Check (Nam);
2974 -- If this is a call to an intrinsic subprogram, then perform the
2975 -- appropriate expansion to the corresponding tree node and we
2976 -- are all done (since after that the call is gone!)
2978 -- In the case where the intrinsic is to be processed by the back end,
2979 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
2980 -- since the idea in this case is to pass the call unchanged. If the
2981 -- intrinsic is an inherited unchecked conversion, and the derived type
2982 -- is the target type of the conversion, we must retain it as the return
2983 -- type of the expression. Otherwise the expansion below, which uses the
2984 -- parent operation, will yield the wrong type.
2986 if Is_Intrinsic_Subprogram (Subp) then
2987 Expand_Intrinsic_Call (Call_Node, Subp);
2989 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
2990 and then Parent_Subp /= Orig_Subp
2991 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
2993 Set_Etype (Call_Node, Etype (Orig_Subp));
2999 if Ekind_In (Subp, E_Function, E_Procedure) then
3001 -- We perform two simple optimization on calls:
3003 -- a) replace calls to null procedures unconditionally;
3005 -- b) for To_Address, just do an unchecked conversion. Not only is
3006 -- this efficient, but it also avoids order of elaboration problems
3007 -- when address clauses are inlined (address expression elaborated
3008 -- at the wrong point).
3010 -- We perform these optimization regardless of whether we are in the
3011 -- main unit or in a unit in the context of the main unit, to ensure
3012 -- that tree generated is the same in both cases, for Inspector use.
3014 if Is_RTE (Subp, RE_To_Address) then
3016 Unchecked_Convert_To
3017 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3020 elsif Is_Null_Procedure (Subp) then
3021 Rewrite (Call_Node, Make_Null_Statement (Loc));
3025 if Is_Inlined (Subp) then
3027 Inlined_Subprogram : declare
3029 Must_Inline : Boolean := False;
3030 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3031 Scop : constant Entity_Id := Scope (Subp);
3033 function In_Unfrozen_Instance return Boolean;
3034 -- If the subprogram comes from an instance in the same unit,
3035 -- and the instance is not yet frozen, inlining might trigger
3036 -- order-of-elaboration problems in gigi.
3038 --------------------------
3039 -- In_Unfrozen_Instance --
3040 --------------------------
3042 function In_Unfrozen_Instance return Boolean is
3048 and then S /= Standard_Standard
3050 if Is_Generic_Instance (S)
3051 and then Present (Freeze_Node (S))
3052 and then not Analyzed (Freeze_Node (S))
3061 end In_Unfrozen_Instance;
3063 -- Start of processing for Inlined_Subprogram
3066 -- Verify that the body to inline has already been seen, and
3067 -- that if the body is in the current unit the inlining does
3068 -- not occur earlier. This avoids order-of-elaboration problems
3071 -- This should be documented in sinfo/einfo ???
3074 or else Nkind (Spec) /= N_Subprogram_Declaration
3075 or else No (Body_To_Inline (Spec))
3077 Must_Inline := False;
3079 -- If this an inherited function that returns a private type,
3080 -- do not inline if the full view is an unconstrained array,
3081 -- because such calls cannot be inlined.
3083 elsif Present (Orig_Subp)
3084 and then Is_Array_Type (Etype (Orig_Subp))
3085 and then not Is_Constrained (Etype (Orig_Subp))
3087 Must_Inline := False;
3089 elsif In_Unfrozen_Instance then
3090 Must_Inline := False;
3093 Bod := Body_To_Inline (Spec);
3095 if (In_Extended_Main_Code_Unit (Call_Node)
3096 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3097 or else Has_Pragma_Inline_Always (Subp))
3098 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3100 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3102 Must_Inline := True;
3104 -- If we are compiling a package body that is not the main
3105 -- unit, it must be for inlining/instantiation purposes,
3106 -- in which case we inline the call to insure that the same
3107 -- temporaries are generated when compiling the body by
3108 -- itself. Otherwise link errors can occur.
3110 -- If the function being called is itself in the main unit,
3111 -- we cannot inline, because there is a risk of double
3112 -- elaboration and/or circularity: the inlining can make
3113 -- visible a private entity in the body of the main unit,
3114 -- that gigi will see before its sees its proper definition.
3116 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3117 and then In_Package_Body
3119 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3124 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3127 -- Let the back end handle it
3129 Add_Inlined_Body (Subp);
3131 if Front_End_Inlining
3132 and then Nkind (Spec) = N_Subprogram_Declaration
3133 and then (In_Extended_Main_Code_Unit (Call_Node))
3134 and then No (Body_To_Inline (Spec))
3135 and then not Has_Completion (Subp)
3136 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3139 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
3142 end Inlined_Subprogram;
3146 -- Check for protected subprogram. This is either an intra-object call,
3147 -- or a protected function call. Protected procedure calls are rewritten
3148 -- as entry calls and handled accordingly.
3150 -- In Ada 2005, this may be an indirect call to an access parameter that
3151 -- is an access_to_subprogram. In that case the anonymous type has a
3152 -- scope that is a protected operation, but the call is a regular one.
3153 -- In either case do not expand call if subprogram is eliminated.
3155 Scop := Scope (Subp);
3157 if Nkind (Call_Node) /= N_Entry_Call_Statement
3158 and then Is_Protected_Type (Scop)
3159 and then Ekind (Subp) /= E_Subprogram_Type
3160 and then not Is_Eliminated (Subp)
3162 -- If the call is an internal one, it is rewritten as a call to the
3163 -- corresponding unprotected subprogram.
3165 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3168 -- Functions returning controlled objects need special attention:
3169 -- if the return type is limited, the context is an initialization
3170 -- and different processing applies. If the call is to a protected
3171 -- function, the expansion above will call Expand_Call recusively.
3172 -- To prevent a double attachment, check that the current call is
3173 -- not a rewriting of a protected function call.
3175 if Needs_Finalization (Etype (Subp)) then
3176 if not Is_Immutably_Limited_Type (Etype (Subp))
3178 (No (First_Formal (Subp))
3180 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3182 Expand_Ctrl_Function_Call (Call_Node);
3184 -- Build-in-place function calls which appear in anonymous contexts
3185 -- need a transient scope to ensure the proper finalization of the
3186 -- intermediate result after its use.
3188 elsif Is_Build_In_Place_Function_Call (Call_Node)
3189 and then Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3191 N_Indexed_Component,
3192 N_Object_Renaming_Declaration,
3193 N_Procedure_Call_Statement,
3194 N_Selected_Component,
3197 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3201 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3202 -- if there are optional parameters at the trailing end.
3203 -- Note: we never delete procedures for call via a pointer.
3205 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3206 and then Present (First_Optional_Parameter (Subp))
3209 Last_Keep_Arg : Node_Id;
3212 -- Last_Keep_Arg will hold the last actual that should be kept.
3213 -- If it remains empty at the end, it means that all parameters
3216 Last_Keep_Arg := Empty;
3218 -- Find first optional parameter, must be present since we checked
3219 -- the validity of the parameter before setting it.
3221 Formal := First_Formal (Subp);
3222 Actual := First_Actual (Call_Node);
3223 while Formal /= First_Optional_Parameter (Subp) loop
3224 Last_Keep_Arg := Actual;
3225 Next_Formal (Formal);
3226 Next_Actual (Actual);
3229 -- We have Formal and Actual pointing to the first potentially
3230 -- droppable argument. We can drop all the trailing arguments
3231 -- whose actual matches the default. Note that we know that all
3232 -- remaining formals have defaults, because we checked that this
3233 -- requirement was met before setting First_Optional_Parameter.
3235 -- We use Fully_Conformant_Expressions to check for identity
3236 -- between formals and actuals, which may miss some cases, but
3237 -- on the other hand, this is only an optimization (if we fail
3238 -- to truncate a parameter it does not affect functionality).
3239 -- So if the default is 3 and the actual is 1+2, we consider
3240 -- them unequal, which hardly seems worrisome.
3242 while Present (Formal) loop
3243 if not Fully_Conformant_Expressions
3244 (Actual, Default_Value (Formal))
3246 Last_Keep_Arg := Actual;
3249 Next_Formal (Formal);
3250 Next_Actual (Actual);
3253 -- If no arguments, delete entire list, this is the easy case
3255 if No (Last_Keep_Arg) then
3256 Set_Parameter_Associations (Call_Node, No_List);
3257 Set_First_Named_Actual (Call_Node, Empty);
3259 -- Case where at the last retained argument is positional. This
3260 -- is also an easy case, since the retained arguments are already
3261 -- in the right form, and we don't need to worry about the order
3262 -- of arguments that get eliminated.
3264 elsif Is_List_Member (Last_Keep_Arg) then
3265 while Present (Next (Last_Keep_Arg)) loop
3266 Discard_Node (Remove_Next (Last_Keep_Arg));
3269 Set_First_Named_Actual (Call_Node, Empty);
3271 -- This is the annoying case where the last retained argument
3272 -- is a named parameter. Since the original arguments are not
3273 -- in declaration order, we may have to delete some fairly
3274 -- random collection of arguments.
3282 -- First step, remove all the named parameters from the
3283 -- list (they are still chained using First_Named_Actual
3284 -- and Next_Named_Actual, so we have not lost them!)
3286 Temp := First (Parameter_Associations (Call_Node));
3288 -- Case of all parameters named, remove them all
3290 if Nkind (Temp) = N_Parameter_Association then
3291 -- Suppress warnings to avoid warning on possible
3292 -- infinite loop (because Call_Node is not modified).
3294 pragma Warnings (Off);
3295 while Is_Non_Empty_List
3296 (Parameter_Associations (Call_Node))
3299 Remove_Head (Parameter_Associations (Call_Node));
3301 pragma Warnings (On);
3303 -- Case of mixed positional/named, remove named parameters
3306 while Nkind (Next (Temp)) /= N_Parameter_Association loop
3310 while Present (Next (Temp)) loop
3311 Remove (Next (Temp));
3315 -- Now we loop through the named parameters, till we get
3316 -- to the last one to be retained, adding them to the list.
3317 -- Note that the Next_Named_Actual list does not need to be
3318 -- touched since we are only reordering them on the actual
3319 -- parameter association list.
3321 Passoc := Parent (First_Named_Actual (Call_Node));
3323 Temp := Relocate_Node (Passoc);
3325 (Parameter_Associations (Call_Node), Temp);
3327 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
3328 Passoc := Parent (Next_Named_Actual (Passoc));
3331 Set_Next_Named_Actual (Temp, Empty);
3334 Temp := Next_Named_Actual (Passoc);
3335 exit when No (Temp);
3336 Set_Next_Named_Actual
3337 (Passoc, Next_Named_Actual (Parent (Temp)));
3346 --------------------------
3347 -- Expand_Inlined_Call --
3348 --------------------------
3350 procedure Expand_Inlined_Call
3353 Orig_Subp : Entity_Id)
3355 Loc : constant Source_Ptr := Sloc (N);
3356 Is_Predef : constant Boolean :=
3357 Is_Predefined_File_Name
3358 (Unit_File_Name (Get_Source_Unit (Subp)));
3359 Orig_Bod : constant Node_Id :=
3360 Body_To_Inline (Unit_Declaration_Node (Subp));
3365 Decls : constant List_Id := New_List;
3366 Exit_Lab : Entity_Id := Empty;
3373 Ret_Type : Entity_Id;
3377 Temp_Typ : Entity_Id;
3379 Return_Object : Entity_Id := Empty;
3380 -- Entity in declaration in an extended_return_statement
3382 Is_Unc : constant Boolean :=
3383 Is_Array_Type (Etype (Subp))
3384 and then not Is_Constrained (Etype (Subp));
3385 -- If the type returned by the function is unconstrained and the call
3386 -- can be inlined, special processing is required.
3388 procedure Make_Exit_Label;
3389 -- Build declaration for exit label to be used in Return statements,
3390 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3391 -- declaration). Does nothing if Exit_Lab already set.
3393 function Process_Formals (N : Node_Id) return Traverse_Result;
3394 -- Replace occurrence of a formal with the corresponding actual, or the
3395 -- thunk generated for it.
3397 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
3398 -- If the call being expanded is that of an internal subprogram, set the
3399 -- sloc of the generated block to that of the call itself, so that the
3400 -- expansion is skipped by the "next" command in gdb.
3401 -- Same processing for a subprogram in a predefined file, e.g.
3402 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
3403 -- simplify our own development.
3405 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3406 -- If the function body is a single expression, replace call with
3407 -- expression, else insert block appropriately.
3409 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3410 -- If procedure body has no local variables, inline body without
3411 -- creating block, otherwise rewrite call with block.
3413 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
3414 -- Determine whether a formal parameter is used only once in Orig_Bod
3416 ---------------------
3417 -- Make_Exit_Label --
3418 ---------------------
3420 procedure Make_Exit_Label is
3421 Lab_Ent : Entity_Id;
3423 if No (Exit_Lab) then
3424 Lab_Ent := Make_Temporary (Loc, 'L');
3425 Lab_Id := New_Reference_To (Lab_Ent, Loc);
3426 Exit_Lab := Make_Label (Loc, Lab_Id);
3428 Make_Implicit_Label_Declaration (Loc,
3429 Defining_Identifier => Lab_Ent,
3430 Label_Construct => Exit_Lab);
3432 end Make_Exit_Label;
3434 ---------------------
3435 -- Process_Formals --
3436 ---------------------
3438 function Process_Formals (N : Node_Id) return Traverse_Result is
3444 if Is_Entity_Name (N)
3445 and then Present (Entity (N))
3450 and then Scope (E) = Subp
3452 A := Renamed_Object (E);
3454 -- Rewrite the occurrence of the formal into an occurrence of
3455 -- the actual. Also establish visibility on the proper view of
3456 -- the actual's subtype for the body's context (if the actual's
3457 -- subtype is private at the call point but its full view is
3458 -- visible to the body, then the inlined tree here must be
3459 -- analyzed with the full view).
3461 if Is_Entity_Name (A) then
3462 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
3463 Check_Private_View (N);
3465 elsif Nkind (A) = N_Defining_Identifier then
3466 Rewrite (N, New_Occurrence_Of (A, Loc));
3467 Check_Private_View (N);
3472 Rewrite (N, New_Copy (A));
3477 elsif Is_Entity_Name (N)
3478 and then Present (Return_Object)
3479 and then Chars (N) = Chars (Return_Object)
3481 -- Occurrence within an extended return statement. The return
3482 -- object is local to the body been inlined, and thus the generic
3483 -- copy is not analyzed yet, so we match by name, and replace it
3484 -- with target of call.
3486 if Nkind (Targ) = N_Defining_Identifier then
3487 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3489 Rewrite (N, New_Copy_Tree (Targ));
3494 elsif Nkind (N) = N_Simple_Return_Statement then
3495 if No (Expression (N)) then
3498 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3501 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3502 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3504 -- Function body is a single expression. No need for
3510 Num_Ret := Num_Ret + 1;
3514 -- Because of the presence of private types, the views of the
3515 -- expression and the context may be different, so place an
3516 -- unchecked conversion to the context type to avoid spurious
3517 -- errors, e.g. when the expression is a numeric literal and
3518 -- the context is private. If the expression is an aggregate,
3519 -- use a qualified expression, because an aggregate is not a
3520 -- legal argument of a conversion.
3522 if Nkind_In (Expression (N), N_Aggregate, N_Null) then
3524 Make_Qualified_Expression (Sloc (N),
3525 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3526 Expression => Relocate_Node (Expression (N)));
3529 Unchecked_Convert_To
3530 (Ret_Type, Relocate_Node (Expression (N)));
3533 if Nkind (Targ) = N_Defining_Identifier then
3535 Make_Assignment_Statement (Loc,
3536 Name => New_Occurrence_Of (Targ, Loc),
3537 Expression => Ret));
3540 Make_Assignment_Statement (Loc,
3541 Name => New_Copy (Targ),
3542 Expression => Ret));
3545 Set_Assignment_OK (Name (N));
3547 if Present (Exit_Lab) then
3549 Make_Goto_Statement (Loc,
3550 Name => New_Copy (Lab_Id)));
3556 elsif Nkind (N) = N_Extended_Return_Statement then
3558 -- An extended return becomes a block whose first statement is
3559 -- the assignment of the initial expression of the return object
3560 -- to the target of the call itself.
3563 Return_Decl : constant Entity_Id :=
3564 First (Return_Object_Declarations (N));
3568 Return_Object := Defining_Identifier (Return_Decl);
3570 if Present (Expression (Return_Decl)) then
3571 if Nkind (Targ) = N_Defining_Identifier then
3573 Make_Assignment_Statement (Loc,
3574 Name => New_Occurrence_Of (Targ, Loc),
3575 Expression => Expression (Return_Decl));
3578 Make_Assignment_Statement (Loc,
3579 Name => New_Copy (Targ),
3580 Expression => Expression (Return_Decl));
3583 Set_Assignment_OK (Name (Assign));
3585 Statements (Handled_Statement_Sequence (N)));
3589 Make_Block_Statement (Loc,
3590 Handled_Statement_Sequence =>
3591 Handled_Statement_Sequence (N)));
3596 -- Remove pragma Unreferenced since it may refer to formals that
3597 -- are not visible in the inlined body, and in any case we will
3598 -- not be posting warnings on the inlined body so it is unneeded.
3600 elsif Nkind (N) = N_Pragma
3601 and then Pragma_Name (N) = Name_Unreferenced
3603 Rewrite (N, Make_Null_Statement (Sloc (N)));
3609 end Process_Formals;
3611 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3617 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3619 if not Debug_Generated_Code then
3620 Set_Sloc (Nod, Sloc (N));
3621 Set_Comes_From_Source (Nod, False);
3627 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3629 ---------------------------
3630 -- Rewrite_Function_Call --
3631 ---------------------------
3633 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
3634 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3635 Fst : constant Node_Id := First (Statements (HSS));
3638 -- Optimize simple case: function body is a single return statement,
3639 -- which has been expanded into an assignment.
3641 if Is_Empty_List (Declarations (Blk))
3642 and then Nkind (Fst) = N_Assignment_Statement
3643 and then No (Next (Fst))
3646 -- The function call may have been rewritten as the temporary
3647 -- that holds the result of the call, in which case remove the
3648 -- now useless declaration.
3650 if Nkind (N) = N_Identifier
3651 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3653 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3656 Rewrite (N, Expression (Fst));
3658 elsif Nkind (N) = N_Identifier
3659 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3661 -- The block assigns the result of the call to the temporary
3663 Insert_After (Parent (Entity (N)), Blk);
3665 elsif Nkind (Parent (N)) = N_Assignment_Statement
3667 (Is_Entity_Name (Name (Parent (N)))
3669 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3670 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
3672 -- Replace assignment with the block
3675 Original_Assignment : constant Node_Id := Parent (N);
3678 -- Preserve the original assignment node to keep the complete
3679 -- assignment subtree consistent enough for Analyze_Assignment
3680 -- to proceed (specifically, the original Lhs node must still
3681 -- have an assignment statement as its parent).
3683 -- We cannot rely on Original_Node to go back from the block
3684 -- node to the assignment node, because the assignment might
3685 -- already be a rewrite substitution.
3687 Discard_Node (Relocate_Node (Original_Assignment));
3688 Rewrite (Original_Assignment, Blk);
3691 elsif Nkind (Parent (N)) = N_Object_Declaration then
3692 Set_Expression (Parent (N), Empty);
3693 Insert_After (Parent (N), Blk);
3696 Insert_Before (Parent (N), Blk);
3698 end Rewrite_Function_Call;
3700 ----------------------------
3701 -- Rewrite_Procedure_Call --
3702 ----------------------------
3704 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
3705 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3707 -- If there is a transient scope for N, this will be the scope of the
3708 -- actions for N, and the statements in Blk need to be within this
3709 -- scope. For example, they need to have visibility on the constant
3710 -- declarations created for the formals.
3712 -- If N needs no transient scope, and if there are no declarations in
3713 -- the inlined body, we can do a little optimization and insert the
3714 -- statements for the body directly after N, and rewrite N to a
3715 -- null statement, instead of rewriting N into a full-blown block
3718 if not Scope_Is_Transient
3719 and then Is_Empty_List (Declarations (Blk))
3721 Insert_List_After (N, Statements (HSS));
3722 Rewrite (N, Make_Null_Statement (Loc));
3726 end Rewrite_Procedure_Call;
3728 -------------------------
3729 -- Formal_Is_Used_Once --
3730 -------------------------
3732 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
3733 Use_Counter : Int := 0;
3735 function Count_Uses (N : Node_Id) return Traverse_Result;
3736 -- Traverse the tree and count the uses of the formal parameter.
3737 -- In this case, for optimization purposes, we do not need to
3738 -- continue the traversal once more than one use is encountered.
3744 function Count_Uses (N : Node_Id) return Traverse_Result is
3746 -- The original node is an identifier
3748 if Nkind (N) = N_Identifier
3749 and then Present (Entity (N))
3751 -- Original node's entity points to the one in the copied body
3753 and then Nkind (Entity (N)) = N_Identifier
3754 and then Present (Entity (Entity (N)))
3756 -- The entity of the copied node is the formal parameter
3758 and then Entity (Entity (N)) = Formal
3760 Use_Counter := Use_Counter + 1;
3762 if Use_Counter > 1 then
3764 -- Denote more than one use and abandon the traversal
3775 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
3777 -- Start of processing for Formal_Is_Used_Once
3780 Count_Formal_Uses (Orig_Bod);
3781 return Use_Counter = 1;
3782 end Formal_Is_Used_Once;
3784 -- Start of processing for Expand_Inlined_Call
3788 -- Check for an illegal attempt to inline a recursive procedure. If the
3789 -- subprogram has parameters this is detected when trying to supply a
3790 -- binding for parameters that already have one. For parameterless
3791 -- subprograms this must be done explicitly.
3793 if In_Open_Scopes (Subp) then
3794 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
3795 Set_Is_Inlined (Subp, False);
3799 if Nkind (Orig_Bod) = N_Defining_Identifier
3800 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
3802 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3803 -- can be replaced with calls to the renamed entity directly, because
3804 -- the subprograms are subtype conformant. If the renamed subprogram
3805 -- is an inherited operation, we must redo the expansion because
3806 -- implicit conversions may be needed. Similarly, if the renamed
3807 -- entity is inlined, expand the call for further optimizations.
3809 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
3811 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
3818 -- Use generic machinery to copy body of inlined subprogram, as if it
3819 -- were an instantiation, resetting source locations appropriately, so
3820 -- that nested inlined calls appear in the main unit.
3822 Save_Env (Subp, Empty);
3823 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
3825 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3827 Make_Block_Statement (Loc,
3828 Declarations => Declarations (Bod),
3829 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
3831 if No (Declarations (Bod)) then
3832 Set_Declarations (Blk, New_List);
3835 -- For the unconstrained case, capture the name of the local
3836 -- variable that holds the result. This must be the first declaration
3837 -- in the block, because its bounds cannot depend on local variables.
3838 -- Otherwise there is no way to declare the result outside of the
3839 -- block. Needless to say, in general the bounds will depend on the
3840 -- actuals in the call.
3843 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3846 -- If this is a derived function, establish the proper return type
3848 if Present (Orig_Subp)
3849 and then Orig_Subp /= Subp
3851 Ret_Type := Etype (Orig_Subp);
3853 Ret_Type := Etype (Subp);
3856 -- Create temporaries for the actuals that are expressions, or that
3857 -- are scalars and require copying to preserve semantics.
3859 F := First_Formal (Subp);
3860 A := First_Actual (N);
3861 while Present (F) loop
3862 if Present (Renamed_Object (F)) then
3863 Error_Msg_N ("cannot inline call to recursive subprogram", N);
3867 -- If the argument may be a controlling argument in a call within
3868 -- the inlined body, we must preserve its classwide nature to insure
3869 -- that dynamic dispatching take place subsequently. If the formal
3870 -- has a constraint it must be preserved to retain the semantics of
3873 if Is_Class_Wide_Type (Etype (F))
3874 or else (Is_Access_Type (Etype (F))
3876 Is_Class_Wide_Type (Designated_Type (Etype (F))))
3878 Temp_Typ := Etype (F);
3880 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3881 and then Etype (F) /= Base_Type (Etype (F))
3883 Temp_Typ := Etype (F);
3886 Temp_Typ := Etype (A);
3889 -- If the actual is a simple name or a literal, no need to
3890 -- create a temporary, object can be used directly.
3892 -- If the actual is a literal and the formal has its address taken,
3893 -- we cannot pass the literal itself as an argument, so its value
3894 -- must be captured in a temporary.
3896 if (Is_Entity_Name (A)
3898 (not Is_Scalar_Type (Etype (A))
3899 or else Ekind (Entity (A)) = E_Enumeration_Literal))
3901 -- When the actual is an identifier and the corresponding formal
3902 -- is used only once in the original body, the formal can be
3903 -- substituted directly with the actual parameter.
3905 or else (Nkind (A) = N_Identifier
3906 and then Formal_Is_Used_Once (F))
3909 (Nkind_In (A, N_Real_Literal,
3911 N_Character_Literal)
3912 and then not Address_Taken (F))
3914 if Etype (F) /= Etype (A) then
3916 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3918 Set_Renamed_Object (F, A);
3922 Temp := Make_Temporary (Loc, 'C');
3924 -- If the actual for an in/in-out parameter is a view conversion,
3925 -- make it into an unchecked conversion, given that an untagged
3926 -- type conversion is not a proper object for a renaming.
3928 -- In-out conversions that involve real conversions have already
3929 -- been transformed in Expand_Actuals.
3931 if Nkind (A) = N_Type_Conversion
3932 and then Ekind (F) /= E_In_Parameter
3935 Make_Unchecked_Type_Conversion (Loc,
3936 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3937 Expression => Relocate_Node (Expression (A)));
3939 elsif Etype (F) /= Etype (A) then
3940 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3941 Temp_Typ := Etype (F);
3944 New_A := Relocate_Node (A);
3947 Set_Sloc (New_A, Sloc (N));
3949 -- If the actual has a by-reference type, it cannot be copied, so
3950 -- its value is captured in a renaming declaration. Otherwise
3951 -- declare a local constant initialized with the actual.
3953 -- We also use a renaming declaration for expressions of an array
3954 -- type that is not bit-packed, both for efficiency reasons and to
3955 -- respect the semantics of the call: in most cases the original
3956 -- call will pass the parameter by reference, and thus the inlined
3957 -- code will have the same semantics.
3959 if Ekind (F) = E_In_Parameter
3960 and then not Is_Limited_Type (Etype (A))
3961 and then not Is_Tagged_Type (Etype (A))
3963 (not Is_Array_Type (Etype (A))
3964 or else not Is_Object_Reference (A)
3965 or else Is_Bit_Packed_Array (Etype (A)))
3968 Make_Object_Declaration (Loc,
3969 Defining_Identifier => Temp,
3970 Constant_Present => True,
3971 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3972 Expression => New_A);
3975 Make_Object_Renaming_Declaration (Loc,
3976 Defining_Identifier => Temp,
3977 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3981 Append (Decl, Decls);
3982 Set_Renamed_Object (F, Temp);
3989 -- Establish target of function call. If context is not assignment or
3990 -- declaration, create a temporary as a target. The declaration for
3991 -- the temporary may be subsequently optimized away if the body is a
3992 -- single expression, or if the left-hand side of the assignment is
3993 -- simple enough, i.e. an entity or an explicit dereference of one.
3995 if Ekind (Subp) = E_Function then
3996 if Nkind (Parent (N)) = N_Assignment_Statement
3997 and then Is_Entity_Name (Name (Parent (N)))
3999 Targ := Name (Parent (N));
4001 elsif Nkind (Parent (N)) = N_Assignment_Statement
4002 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4003 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4005 Targ := Name (Parent (N));
4007 elsif Nkind (Parent (N)) = N_Object_Declaration
4008 and then Is_Limited_Type (Etype (Subp))
4010 Targ := Defining_Identifier (Parent (N));
4013 -- Replace call with temporary and create its declaration
4015 Temp := Make_Temporary (Loc, 'C');
4016 Set_Is_Internal (Temp);
4018 -- For the unconstrained case, the generated temporary has the
4019 -- same constrained declaration as the result variable. It may
4020 -- eventually be possible to remove that temporary and use the
4021 -- result variable directly.
4025 Make_Object_Declaration (Loc,
4026 Defining_Identifier => Temp,
4027 Object_Definition =>
4028 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4030 Replace_Formals (Decl);
4034 Make_Object_Declaration (Loc,
4035 Defining_Identifier => Temp,
4036 Object_Definition =>
4037 New_Occurrence_Of (Ret_Type, Loc));
4039 Set_Etype (Temp, Ret_Type);
4042 Set_No_Initialization (Decl);
4043 Append (Decl, Decls);
4044 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4049 Insert_Actions (N, Decls);
4051 -- Traverse the tree and replace formals with actuals or their thunks.
4052 -- Attach block to tree before analysis and rewriting.
4054 Replace_Formals (Blk);
4055 Set_Parent (Blk, N);
4057 if not Comes_From_Source (Subp)
4063 if Present (Exit_Lab) then
4065 -- If the body was a single expression, the single return statement
4066 -- and the corresponding label are useless.
4070 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4073 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4075 Append (Lab_Decl, (Declarations (Blk)));
4076 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
4080 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
4081 -- conflicting private views that Gigi would ignore. If this is a
4082 -- predefined unit, analyze with checks off, as is done in the non-
4083 -- inlined run-time units.
4086 I_Flag : constant Boolean := In_Inlined_Body;
4089 In_Inlined_Body := True;
4093 Style : constant Boolean := Style_Check;
4095 Style_Check := False;
4096 Analyze (Blk, Suppress => All_Checks);
4097 Style_Check := Style;
4104 In_Inlined_Body := I_Flag;
4107 if Ekind (Subp) = E_Procedure then
4108 Rewrite_Procedure_Call (N, Blk);
4110 Rewrite_Function_Call (N, Blk);
4112 -- For the unconstrained case, the replacement of the call has been
4113 -- made prior to the complete analysis of the generated declarations.
4114 -- Propagate the proper type now.
4117 if Nkind (N) = N_Identifier then
4118 Set_Etype (N, Etype (Entity (N)));
4120 Set_Etype (N, Etype (Targ1));
4127 -- Cleanup mapping between formals and actuals for other expansions
4129 F := First_Formal (Subp);
4130 while Present (F) loop
4131 Set_Renamed_Object (F, Empty);
4134 end Expand_Inlined_Call;
4136 ----------------------------------------
4137 -- Expand_N_Extended_Return_Statement --
4138 ----------------------------------------
4140 -- If there is a Handled_Statement_Sequence, we rewrite this:
4142 -- return Result : T := <expression> do
4143 -- <handled_seq_of_stms>
4149 -- Result : T := <expression>;
4151 -- <handled_seq_of_stms>
4155 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4157 -- return Result : T := <expression>;
4161 -- return <expression>;
4163 -- unless it's build-in-place or there's no <expression>, in which case
4167 -- Result : T := <expression>;
4172 -- Note that this case could have been written by the user as an extended
4173 -- return statement, or could have been transformed to this from a simple
4174 -- return statement.
4176 -- That is, we need to have a reified return object if there are statements
4177 -- (which might refer to it) or if we're doing build-in-place (so we can
4178 -- set its address to the final resting place or if there is no expression
4179 -- (in which case default initial values might need to be set).
4181 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4182 Loc : constant Source_Ptr := Sloc (N);
4184 Return_Object_Entity : constant Entity_Id :=
4185 First_Entity (Return_Statement_Entity (N));
4186 Return_Object_Decl : constant Node_Id :=
4187 Parent (Return_Object_Entity);
4188 Parent_Function : constant Entity_Id :=
4189 Return_Applies_To (Return_Statement_Entity (N));
4190 Parent_Function_Typ : constant Entity_Id := Etype (Parent_Function);
4191 Is_Build_In_Place : constant Boolean :=
4192 Is_Build_In_Place_Function (Parent_Function);
4194 Return_Stm : Node_Id;
4195 Statements : List_Id;
4196 Handled_Stm_Seq : Node_Id;
4200 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean;
4201 -- Determine whether type Typ is controlled or contains a controlled
4204 function Move_Activation_Chain return Node_Id;
4205 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4207 -- From current activation chain
4208 -- To activation chain passed in by the caller
4209 -- New_Master master passed in by the caller
4211 function Move_Final_List return Node_Id;
4212 -- Construct call to System.Finalization_Implementation.Move_Final_List
4215 -- From finalization list of the return statement
4216 -- To finalization list passed in by the caller
4218 --------------------------
4219 -- Has_Controlled_Parts --
4220 --------------------------
4222 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean is
4226 or else Has_Controlled_Component (Typ);
4227 end Has_Controlled_Parts;
4229 ---------------------------
4230 -- Move_Activation_Chain --
4231 ---------------------------
4233 function Move_Activation_Chain return Node_Id is
4234 Activation_Chain_Formal : constant Entity_Id :=
4235 Build_In_Place_Formal
4236 (Parent_Function, BIP_Activation_Chain);
4237 To : constant Node_Id :=
4239 (Activation_Chain_Formal, Loc);
4240 Master_Formal : constant Entity_Id :=
4241 Build_In_Place_Formal
4242 (Parent_Function, BIP_Master);
4243 New_Master : constant Node_Id :=
4244 New_Reference_To (Master_Formal, Loc);
4246 Chain_Entity : Entity_Id;
4250 Chain_Entity := First_Entity (Return_Statement_Entity (N));
4251 while Chars (Chain_Entity) /= Name_uChain loop
4252 Chain_Entity := Next_Entity (Chain_Entity);
4256 Make_Attribute_Reference (Loc,
4257 Prefix => New_Reference_To (Chain_Entity, Loc),
4258 Attribute_Name => Name_Unrestricted_Access);
4259 -- ??? Not clear why "Make_Identifier (Loc, Name_uChain)" doesn't
4260 -- work, instead of "New_Reference_To (Chain_Entity, Loc)" above.
4263 Make_Procedure_Call_Statement (Loc,
4264 Name => New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
4265 Parameter_Associations => New_List (From, To, New_Master));
4266 end Move_Activation_Chain;
4268 ---------------------
4269 -- Move_Final_List --
4270 ---------------------
4272 function Move_Final_List return Node_Id is
4273 Flist : constant Entity_Id :=
4274 Finalization_Chain_Entity (Return_Statement_Entity (N));
4276 From : constant Node_Id := New_Reference_To (Flist, Loc);
4278 Caller_Final_List : constant Entity_Id :=
4279 Build_In_Place_Formal
4280 (Parent_Function, BIP_Final_List);
4282 To : constant Node_Id := New_Reference_To (Caller_Final_List, Loc);
4285 -- Catch cases where a finalization chain entity has not been
4286 -- associated with the return statement entity.
4288 pragma Assert (Present (Flist));
4290 -- Build required call
4293 Make_If_Statement (Loc,
4296 Left_Opnd => New_Copy (From),
4297 Right_Opnd => New_Node (N_Null, Loc)),
4300 Make_Procedure_Call_Statement (Loc,
4301 Name => New_Reference_To (RTE (RE_Move_Final_List), Loc),
4302 Parameter_Associations => New_List (From, To))));
4303 end Move_Final_List;
4305 -- Start of processing for Expand_N_Extended_Return_Statement
4308 if Nkind (Return_Object_Decl) = N_Object_Declaration then
4309 Exp := Expression (Return_Object_Decl);
4314 Handled_Stm_Seq := Handled_Statement_Sequence (N);
4316 -- Build a simple_return_statement that returns the return object when
4317 -- there is a statement sequence, or no expression, or the result will
4318 -- be built in place. Note however that we currently do this for all
4319 -- composite cases, even though nonlimited composite results are not yet
4320 -- built in place (though we plan to do so eventually).
4322 if Present (Handled_Stm_Seq)
4323 or else Is_Composite_Type (Etype (Parent_Function))
4326 if No (Handled_Stm_Seq) then
4327 Statements := New_List;
4329 -- If the extended return has a handled statement sequence, then wrap
4330 -- it in a block and use the block as the first statement.
4334 New_List (Make_Block_Statement (Loc,
4335 Declarations => New_List,
4336 Handled_Statement_Sequence => Handled_Stm_Seq));
4339 -- If control gets past the above Statements, we have successfully
4340 -- completed the return statement. If the result type has controlled
4341 -- parts and the return is for a build-in-place function, then we
4342 -- call Move_Final_List to transfer responsibility for finalization
4343 -- of the return object to the caller. An alternative would be to
4344 -- declare a Success flag in the function, initialize it to False,
4345 -- and set it to True here. Then move the Move_Final_List call into
4346 -- the cleanup code, and check Success. If Success then make a call
4347 -- to Move_Final_List else do finalization. Then we can remove the
4348 -- abort-deferral and the nulling-out of the From parameter from
4349 -- Move_Final_List. Note that the current method is not quite correct
4350 -- in the rather obscure case of a select-then-abort statement whose
4351 -- abortable part contains the return statement.
4353 -- Check the type of the function to determine whether to move the
4354 -- finalization list. A special case arises when processing a simple
4355 -- return statement which has been rewritten as an extended return.
4356 -- In that case check the type of the returned object or the original
4359 if Is_Build_In_Place
4361 (Has_Controlled_Parts (Parent_Function_Typ)
4362 or else (Is_Class_Wide_Type (Parent_Function_Typ)
4364 Has_Controlled_Parts (Root_Type (Parent_Function_Typ)))
4365 or else Has_Controlled_Parts (Etype (Return_Object_Entity))
4366 or else (Present (Exp)
4367 and then Has_Controlled_Parts (Etype (Exp))))
4369 Append_To (Statements, Move_Final_List);
4372 -- Similarly to the above Move_Final_List, if the result type
4373 -- contains tasks, we call Move_Activation_Chain. Later, the cleanup
4374 -- code will call Complete_Master, which will terminate any
4375 -- unactivated tasks belonging to the return statement master. But
4376 -- Move_Activation_Chain updates their master to be that of the
4377 -- caller, so they will not be terminated unless the return statement
4378 -- completes unsuccessfully due to exception, abort, goto, or exit.
4379 -- As a formality, we test whether the function requires the result
4380 -- to be built in place, though that's necessarily true for the case
4381 -- of result types with task parts.
4383 if Is_Build_In_Place and Has_Task (Etype (Parent_Function)) then
4384 Append_To (Statements, Move_Activation_Chain);
4387 -- Build a simple_return_statement that returns the return object
4390 Make_Simple_Return_Statement (Loc,
4391 Expression => New_Occurrence_Of (Return_Object_Entity, Loc));
4392 Append_To (Statements, Return_Stm);
4395 Make_Handled_Sequence_Of_Statements (Loc, Statements);
4398 -- Case where we build a block
4400 if Present (Handled_Stm_Seq) then
4402 Make_Block_Statement (Loc,
4403 Declarations => Return_Object_Declarations (N),
4404 Handled_Statement_Sequence => Handled_Stm_Seq);
4406 -- We set the entity of the new block statement to be that of the
4407 -- return statement. This is necessary so that various fields, such
4408 -- as Finalization_Chain_Entity carry over from the return statement
4409 -- to the block. Note that this block is unusual, in that its entity
4410 -- is an E_Return_Statement rather than an E_Block.
4413 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4415 -- If the object decl was already rewritten as a renaming, then
4416 -- we don't want to do the object allocation and transformation of
4417 -- of the return object declaration to a renaming. This case occurs
4418 -- when the return object is initialized by a call to another
4419 -- build-in-place function, and that function is responsible for the
4420 -- allocation of the return object.
4422 if Is_Build_In_Place
4424 Nkind (Return_Object_Decl) = N_Object_Renaming_Declaration
4426 pragma Assert (Nkind (Original_Node (Return_Object_Decl)) =
4427 N_Object_Declaration
4428 and then Is_Build_In_Place_Function_Call
4429 (Expression (Original_Node (Return_Object_Decl))));
4431 Set_By_Ref (Return_Stm); -- Return build-in-place results by ref
4433 elsif Is_Build_In_Place then
4435 -- Locate the implicit access parameter associated with the
4436 -- caller-supplied return object and convert the return
4437 -- statement's return object declaration to a renaming of a
4438 -- dereference of the access parameter. If the return object's
4439 -- declaration includes an expression that has not already been
4440 -- expanded as separate assignments, then add an assignment
4441 -- statement to ensure the return object gets initialized.
4444 -- Result : T [:= <expression>];
4451 -- Result : T renames FuncRA.all;
4452 -- [Result := <expression;]
4457 Return_Obj_Id : constant Entity_Id :=
4458 Defining_Identifier (Return_Object_Decl);
4459 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
4460 Return_Obj_Expr : constant Node_Id :=
4461 Expression (Return_Object_Decl);
4462 Result_Subt : constant Entity_Id :=
4463 Etype (Parent_Function);
4464 Constr_Result : constant Boolean :=
4465 Is_Constrained (Result_Subt);
4466 Obj_Alloc_Formal : Entity_Id;
4467 Object_Access : Entity_Id;
4468 Obj_Acc_Deref : Node_Id;
4469 Init_Assignment : Node_Id := Empty;
4472 -- Build-in-place results must be returned by reference
4474 Set_By_Ref (Return_Stm);
4476 -- Retrieve the implicit access parameter passed by the caller
4479 Build_In_Place_Formal (Parent_Function, BIP_Object_Access);
4481 -- If the return object's declaration includes an expression
4482 -- and the declaration isn't marked as No_Initialization, then
4483 -- we need to generate an assignment to the object and insert
4484 -- it after the declaration before rewriting it as a renaming
4485 -- (otherwise we'll lose the initialization). The case where
4486 -- the result type is an interface (or class-wide interface)
4487 -- is also excluded because the context of the function call
4488 -- must be unconstrained, so the initialization will always
4489 -- be done as part of an allocator evaluation (storage pool
4490 -- or secondary stack), never to a constrained target object
4491 -- passed in by the caller. Besides the assignment being
4492 -- unneeded in this case, it avoids problems with trying to
4493 -- generate a dispatching assignment when the return expression
4494 -- is a nonlimited descendant of a limited interface (the
4495 -- interface has no assignment operation).
4497 if Present (Return_Obj_Expr)
4498 and then not No_Initialization (Return_Object_Decl)
4499 and then not Is_Interface (Return_Obj_Typ)
4502 Make_Assignment_Statement (Loc,
4503 Name => New_Reference_To (Return_Obj_Id, Loc),
4504 Expression => Relocate_Node (Return_Obj_Expr));
4505 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
4506 Set_Assignment_OK (Name (Init_Assignment));
4507 Set_No_Ctrl_Actions (Init_Assignment);
4509 Set_Parent (Name (Init_Assignment), Init_Assignment);
4510 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4512 Set_Expression (Return_Object_Decl, Empty);
4514 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
4515 and then not Is_Class_Wide_Type
4516 (Etype (Expression (Init_Assignment)))
4518 Rewrite (Expression (Init_Assignment),
4519 Make_Type_Conversion (Loc,
4522 (Etype (Return_Obj_Id), Loc),
4524 Relocate_Node (Expression (Init_Assignment))));
4527 -- In the case of functions where the calling context can
4528 -- determine the form of allocation needed, initialization
4529 -- is done with each part of the if statement that handles
4530 -- the different forms of allocation (this is true for
4531 -- unconstrained and tagged result subtypes).
4534 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
4536 Insert_After (Return_Object_Decl, Init_Assignment);
4540 -- When the function's subtype is unconstrained, a run-time
4541 -- test is needed to determine the form of allocation to use
4542 -- for the return object. The function has an implicit formal
4543 -- parameter indicating this. If the BIP_Alloc_Form formal has
4544 -- the value one, then the caller has passed access to an
4545 -- existing object for use as the return object. If the value
4546 -- is two, then the return object must be allocated on the
4547 -- secondary stack. Otherwise, the object must be allocated in
4548 -- a storage pool (currently only supported for the global
4549 -- heap, user-defined storage pools TBD ???). We generate an
4550 -- if statement to test the implicit allocation formal and
4551 -- initialize a local access value appropriately, creating
4552 -- allocators in the secondary stack and global heap cases.
4553 -- The special formal also exists and must be tested when the
4554 -- function has a tagged result, even when the result subtype
4555 -- is constrained, because in general such functions can be
4556 -- called in dispatching contexts and must be handled similarly
4557 -- to functions with a class-wide result.
4559 if not Constr_Result
4560 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
4563 Build_In_Place_Formal (Parent_Function, BIP_Alloc_Form);
4566 Ref_Type : Entity_Id;
4567 Ptr_Type_Decl : Node_Id;
4568 Alloc_Obj_Id : Entity_Id;
4569 Alloc_Obj_Decl : Node_Id;
4570 Alloc_If_Stmt : Node_Id;
4571 SS_Allocator : Node_Id;
4572 Heap_Allocator : Node_Id;
4575 -- Reuse the itype created for the function's implicit
4576 -- access formal. This avoids the need to create a new
4577 -- access type here, plus it allows assigning the access
4578 -- formal directly without applying a conversion.
4580 -- Ref_Type := Etype (Object_Access);
4582 -- Create an access type designating the function's
4585 Ref_Type := Make_Temporary (Loc, 'A');
4588 Make_Full_Type_Declaration (Loc,
4589 Defining_Identifier => Ref_Type,
4591 Make_Access_To_Object_Definition (Loc,
4592 All_Present => True,
4593 Subtype_Indication =>
4594 New_Reference_To (Return_Obj_Typ, Loc)));
4596 Insert_Before (Return_Object_Decl, Ptr_Type_Decl);
4598 -- Create an access object that will be initialized to an
4599 -- access value denoting the return object, either coming
4600 -- from an implicit access value passed in by the caller
4601 -- or from the result of an allocator.
4603 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4604 Set_Etype (Alloc_Obj_Id, Ref_Type);
4607 Make_Object_Declaration (Loc,
4608 Defining_Identifier => Alloc_Obj_Id,
4609 Object_Definition => New_Reference_To
4612 Insert_Before (Return_Object_Decl, Alloc_Obj_Decl);
4614 -- Create allocators for both the secondary stack and
4615 -- global heap. If there's an initialization expression,
4616 -- then create these as initialized allocators.
4618 if Present (Return_Obj_Expr)
4619 and then not No_Initialization (Return_Object_Decl)
4621 -- Always use the type of the expression for the
4622 -- qualified expression, rather than the result type.
4623 -- In general we cannot always use the result type
4624 -- for the allocator, because the expression might be
4625 -- of a specific type, such as in the case of an
4626 -- aggregate or even a nonlimited object when the
4627 -- result type is a limited class-wide interface type.
4630 Make_Allocator (Loc,
4632 Make_Qualified_Expression (Loc,
4635 (Etype (Return_Obj_Expr), Loc),
4637 New_Copy_Tree (Return_Obj_Expr)));
4640 -- If the function returns a class-wide type we cannot
4641 -- use the return type for the allocator. Instead we
4642 -- use the type of the expression, which must be an
4643 -- aggregate of a definite type.
4645 if Is_Class_Wide_Type (Return_Obj_Typ) then
4647 Make_Allocator (Loc,
4650 (Etype (Return_Obj_Expr), Loc));
4653 Make_Allocator (Loc,
4655 New_Reference_To (Return_Obj_Typ, Loc));
4658 -- If the object requires default initialization then
4659 -- that will happen later following the elaboration of
4660 -- the object renaming. If we don't turn it off here
4661 -- then the object will be default initialized twice.
4663 Set_No_Initialization (Heap_Allocator);
4666 -- If the No_Allocators restriction is active, then only
4667 -- an allocator for secondary stack allocation is needed.
4668 -- It's OK for such allocators to have Comes_From_Source
4669 -- set to False, because gigi knows not to flag them as
4670 -- being a violation of No_Implicit_Heap_Allocations.
4672 if Restriction_Active (No_Allocators) then
4673 SS_Allocator := Heap_Allocator;
4674 Heap_Allocator := Make_Null (Loc);
4676 -- Otherwise the heap allocator may be needed, so we make
4677 -- another allocator for secondary stack allocation.
4680 SS_Allocator := New_Copy_Tree (Heap_Allocator);
4682 -- The heap allocator is marked Comes_From_Source
4683 -- since it corresponds to an explicit user-written
4684 -- allocator (that is, it will only be executed on
4685 -- behalf of callers that call the function as
4686 -- initialization for such an allocator). This
4687 -- prevents errors when No_Implicit_Heap_Allocations
4690 Set_Comes_From_Source (Heap_Allocator, True);
4693 -- The allocator is returned on the secondary stack. We
4694 -- don't do this on VM targets, since the SS is not used.
4696 if VM_Target = No_VM then
4697 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
4698 Set_Procedure_To_Call
4699 (SS_Allocator, RTE (RE_SS_Allocate));
4701 -- The allocator is returned on the secondary stack,
4702 -- so indicate that the function return, as well as
4703 -- the block that encloses the allocator, must not
4704 -- release it. The flags must be set now because the
4705 -- decision to use the secondary stack is done very
4706 -- late in the course of expanding the return
4707 -- statement, past the point where these flags are
4710 Set_Sec_Stack_Needed_For_Return (Parent_Function);
4711 Set_Sec_Stack_Needed_For_Return
4712 (Return_Statement_Entity (N));
4713 Set_Uses_Sec_Stack (Parent_Function);
4714 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
4717 -- Create an if statement to test the BIP_Alloc_Form
4718 -- formal and initialize the access object to either the
4719 -- BIP_Object_Access formal (BIP_Alloc_Form = 0), the
4720 -- result of allocating the object in the secondary stack
4721 -- (BIP_Alloc_Form = 1), or else an allocator to create
4722 -- the return object in the heap (BIP_Alloc_Form = 2).
4724 -- ??? An unchecked type conversion must be made in the
4725 -- case of assigning the access object formal to the
4726 -- local access object, because a normal conversion would
4727 -- be illegal in some cases (such as converting access-
4728 -- to-unconstrained to access-to-constrained), but the
4729 -- the unchecked conversion will presumably fail to work
4730 -- right in just such cases. It's not clear at all how to
4734 Make_If_Statement (Loc,
4738 New_Reference_To (Obj_Alloc_Formal, Loc),
4740 Make_Integer_Literal (Loc,
4741 UI_From_Int (BIP_Allocation_Form'Pos
4742 (Caller_Allocation)))),
4744 New_List (Make_Assignment_Statement (Loc,
4747 (Alloc_Obj_Id, Loc),
4749 Make_Unchecked_Type_Conversion (Loc,
4751 New_Reference_To (Ref_Type, Loc),
4754 (Object_Access, Loc)))),
4756 New_List (Make_Elsif_Part (Loc,
4761 (Obj_Alloc_Formal, Loc),
4763 Make_Integer_Literal (Loc,
4765 BIP_Allocation_Form'Pos
4766 (Secondary_Stack)))),
4769 (Make_Assignment_Statement (Loc,
4772 (Alloc_Obj_Id, Loc),
4776 New_List (Make_Assignment_Statement (Loc,
4779 (Alloc_Obj_Id, Loc),
4783 -- If a separate initialization assignment was created
4784 -- earlier, append that following the assignment of the
4785 -- implicit access formal to the access object, to ensure
4786 -- that the return object is initialized in that case.
4787 -- In this situation, the target of the assignment must
4788 -- be rewritten to denote a dereference of the access to
4789 -- the return object passed in by the caller.
4791 if Present (Init_Assignment) then
4792 Rewrite (Name (Init_Assignment),
4793 Make_Explicit_Dereference (Loc,
4794 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
4796 (Name (Init_Assignment), Etype (Return_Obj_Id));
4799 (Then_Statements (Alloc_If_Stmt),
4803 Insert_Before (Return_Object_Decl, Alloc_If_Stmt);
4805 -- Remember the local access object for use in the
4806 -- dereference of the renaming created below.
4808 Object_Access := Alloc_Obj_Id;
4812 -- Replace the return object declaration with a renaming of a
4813 -- dereference of the access value designating the return
4817 Make_Explicit_Dereference (Loc,
4818 Prefix => New_Reference_To (Object_Access, Loc));
4820 Rewrite (Return_Object_Decl,
4821 Make_Object_Renaming_Declaration (Loc,
4822 Defining_Identifier => Return_Obj_Id,
4823 Access_Definition => Empty,
4824 Subtype_Mark => New_Occurrence_Of
4825 (Return_Obj_Typ, Loc),
4826 Name => Obj_Acc_Deref));
4828 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
4832 -- Case where we do not build a block
4835 -- We're about to drop Return_Object_Declarations on the floor, so
4836 -- we need to insert it, in case it got expanded into useful code.
4837 -- Remove side effects from expression, which may be duplicated in
4838 -- subsequent checks (see Expand_Simple_Function_Return).
4840 Insert_List_Before (N, Return_Object_Declarations (N));
4841 Remove_Side_Effects (Exp);
4843 -- Build simple_return_statement that returns the expression directly
4845 Return_Stm := Make_Simple_Return_Statement (Loc, Expression => Exp);
4847 Result := Return_Stm;
4850 -- Set the flag to prevent infinite recursion
4852 Set_Comes_From_Extended_Return_Statement (Return_Stm);
4854 Rewrite (N, Result);
4856 end Expand_N_Extended_Return_Statement;
4858 ----------------------------
4859 -- Expand_N_Function_Call --
4860 ----------------------------
4862 procedure Expand_N_Function_Call (N : Node_Id) is
4866 -- If the return value of a foreign compiled function is VAX Float, then
4867 -- expand the return (adjusts the location of the return value on
4868 -- Alpha/VMS, no-op everywhere else).
4869 -- Comes_From_Source intercepts recursive expansion.
4871 if Vax_Float (Etype (N))
4872 and then Nkind (N) = N_Function_Call
4873 and then Present (Name (N))
4874 and then Present (Entity (Name (N)))
4875 and then Has_Foreign_Convention (Entity (Name (N)))
4876 and then Comes_From_Source (Parent (N))
4878 Expand_Vax_Foreign_Return (N);
4880 end Expand_N_Function_Call;
4882 ---------------------------------------
4883 -- Expand_N_Procedure_Call_Statement --
4884 ---------------------------------------
4886 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4889 end Expand_N_Procedure_Call_Statement;
4891 --------------------------------------
4892 -- Expand_N_Simple_Return_Statement --
4893 --------------------------------------
4895 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
4897 -- Defend against previous errors (i.e. the return statement calls a
4898 -- function that is not available in configurable runtime).
4900 if Present (Expression (N))
4901 and then Nkind (Expression (N)) = N_Empty
4906 -- Distinguish the function and non-function cases:
4908 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
4911 E_Generic_Function =>
4912 Expand_Simple_Function_Return (N);
4915 E_Generic_Procedure |
4918 E_Return_Statement =>
4919 Expand_Non_Function_Return (N);
4922 raise Program_Error;
4926 when RE_Not_Available =>
4928 end Expand_N_Simple_Return_Statement;
4930 ------------------------------
4931 -- Expand_N_Subprogram_Body --
4932 ------------------------------
4934 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4937 -- Add dummy push/pop label nodes at start and end to clear any local
4938 -- exception indications if local-exception-to-goto optimization is active.
4940 -- Add return statement if last statement in body is not a return statement
4941 -- (this makes things easier on Gigi which does not want to have to handle
4942 -- a missing return).
4944 -- Add call to Activate_Tasks if body is a task activator
4946 -- Deal with possible detection of infinite recursion
4948 -- Eliminate body completely if convention stubbed
4950 -- Encode entity names within body, since we will not need to reference
4951 -- these entities any longer in the front end.
4953 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4955 -- Reset Pure indication if any parameter has root type System.Address
4956 -- or has any parameters of limited types, where limited means that the
4957 -- run-time view is limited (i.e. the full type is limited).
4961 procedure Expand_N_Subprogram_Body (N : Node_Id) is
4962 Loc : constant Source_Ptr := Sloc (N);
4963 H : constant Node_Id := Handled_Statement_Sequence (N);
4964 Body_Id : Entity_Id;
4967 Spec_Id : Entity_Id;
4969 procedure Add_Return (S : List_Id);
4970 -- Append a return statement to the statement sequence S if the last
4971 -- statement is not already a return or a goto statement. Note that
4972 -- the latter test is not critical, it does not matter if we add a few
4973 -- extra returns, since they get eliminated anyway later on.
4979 procedure Add_Return (S : List_Id) is
4984 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4985 -- not relevant in this context since they are not executable.
4987 Last_Stm := Last (S);
4988 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
4992 -- Now insert return unless last statement is a transfer
4994 if not Is_Transfer (Last_Stm) then
4996 -- The source location for the return is the end label of the
4997 -- procedure if present. Otherwise use the sloc of the last
4998 -- statement in the list. If the list comes from a generated
4999 -- exception handler and we are not debugging generated code,
5000 -- all the statements within the handler are made invisible
5003 if Nkind (Parent (S)) = N_Exception_Handler
5004 and then not Comes_From_Source (Parent (S))
5006 Loc := Sloc (Last_Stm);
5008 elsif Present (End_Label (H)) then
5009 Loc := Sloc (End_Label (H));
5012 Loc := Sloc (Last_Stm);
5016 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
5019 -- Append return statement, and set analyzed manually. We can't
5020 -- call Analyze on this return since the scope is wrong.
5022 -- Note: it almost works to push the scope and then do the
5023 -- Analyze call, but something goes wrong in some weird cases
5024 -- and it is not worth worrying about ???
5029 -- Call _Postconditions procedure if appropriate. We need to
5030 -- do this explicitly because we did not analyze the generated
5031 -- return statement above, so the call did not get inserted.
5033 if Ekind (Spec_Id) = E_Procedure
5034 and then Has_Postconditions (Spec_Id)
5036 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
5038 Make_Procedure_Call_Statement (Loc,
5040 New_Reference_To (Postcondition_Proc (Spec_Id), Loc)));
5046 -- Start of processing for Expand_N_Subprogram_Body
5049 -- Set L to either the list of declarations if present, or to the list
5050 -- of statements if no declarations are present. This is used to insert
5051 -- new stuff at the start.
5053 if Is_Non_Empty_List (Declarations (N)) then
5054 L := Declarations (N);
5056 L := Statements (H);
5059 -- If local-exception-to-goto optimization active, insert dummy push
5060 -- statements at start, and dummy pop statements at end.
5062 if (Debug_Flag_Dot_G
5063 or else Restriction_Active (No_Exception_Propagation))
5064 and then Is_Non_Empty_List (L)
5067 FS : constant Node_Id := First (L);
5068 FL : constant Source_Ptr := Sloc (FS);
5073 -- LS points to either last statement, if statements are present
5074 -- or to the last declaration if there are no statements present.
5075 -- It is the node after which the pop's are generated.
5077 if Is_Non_Empty_List (Statements (H)) then
5078 LS := Last (Statements (H));
5085 Insert_List_Before_And_Analyze (FS, New_List (
5086 Make_Push_Constraint_Error_Label (FL),
5087 Make_Push_Program_Error_Label (FL),
5088 Make_Push_Storage_Error_Label (FL)));
5090 Insert_List_After_And_Analyze (LS, New_List (
5091 Make_Pop_Constraint_Error_Label (LL),
5092 Make_Pop_Program_Error_Label (LL),
5093 Make_Pop_Storage_Error_Label (LL)));
5097 -- Find entity for subprogram
5099 Body_Id := Defining_Entity (N);
5101 if Present (Corresponding_Spec (N)) then
5102 Spec_Id := Corresponding_Spec (N);
5107 -- Need poll on entry to subprogram if polling enabled. We only do this
5108 -- for non-empty subprograms, since it does not seem necessary to poll
5109 -- for a dummy null subprogram.
5111 if Is_Non_Empty_List (L) then
5113 -- Do not add a polling call if the subprogram is to be inlined by
5114 -- the back-end, to avoid repeated calls with multiple inlinings.
5116 if Is_Inlined (Spec_Id)
5117 and then Front_End_Inlining
5118 and then Optimization_Level > 1
5122 Generate_Poll_Call (First (L));
5126 -- If this is a Pure function which has any parameters whose root type
5127 -- is System.Address, reset the Pure indication, since it will likely
5128 -- cause incorrect code to be generated as the parameter is probably
5129 -- a pointer, and the fact that the same pointer is passed does not mean
5130 -- that the same value is being referenced.
5132 -- Note that if the programmer gave an explicit Pure_Function pragma,
5133 -- then we believe the programmer, and leave the subprogram Pure.
5135 -- This code should probably be at the freeze point, so that it happens
5136 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5137 -- semantic tree has Is_Pure set properly ???
5139 if Is_Pure (Spec_Id)
5140 and then Is_Subprogram (Spec_Id)
5141 and then not Has_Pragma_Pure_Function (Spec_Id)
5147 F := First_Formal (Spec_Id);
5148 while Present (F) loop
5149 if Is_Descendent_Of_Address (Etype (F))
5151 -- Note that this test is being made in the body of the
5152 -- subprogram, not the spec, so we are testing the full
5153 -- type for being limited here, as required.
5155 or else Is_Limited_Type (Etype (F))
5157 Set_Is_Pure (Spec_Id, False);
5159 if Spec_Id /= Body_Id then
5160 Set_Is_Pure (Body_Id, False);
5171 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5173 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5178 -- Loop through formals
5180 F := First_Formal (Spec_Id);
5181 while Present (F) loop
5182 if Is_Scalar_Type (Etype (F))
5183 and then Ekind (F) = E_Out_Parameter
5185 Check_Restriction (No_Default_Initialization, F);
5187 -- Insert the initialization. We turn off validity checks
5188 -- for this assignment, since we do not want any check on
5189 -- the initial value itself (which may well be invalid).
5191 Insert_Before_And_Analyze (First (L),
5192 Make_Assignment_Statement (Loc,
5193 Name => New_Occurrence_Of (F, Loc),
5194 Expression => Get_Simple_Init_Val (Etype (F), N)),
5195 Suppress => Validity_Check);
5203 -- Clear out statement list for stubbed procedure
5205 if Present (Corresponding_Spec (N)) then
5206 Set_Elaboration_Flag (N, Spec_Id);
5208 if Convention (Spec_Id) = Convention_Stubbed
5209 or else Is_Eliminated (Spec_Id)
5211 Set_Declarations (N, Empty_List);
5212 Set_Handled_Statement_Sequence (N,
5213 Make_Handled_Sequence_Of_Statements (Loc,
5214 Statements => New_List (
5215 Make_Null_Statement (Loc))));
5220 -- Create a set of discriminals for the next protected subprogram body
5222 if Is_List_Member (N)
5223 and then Present (Parent (List_Containing (N)))
5224 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5225 and then Present (Next_Protected_Operation (N))
5227 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5230 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5231 -- subprograms with no specs are not frozen.
5234 Typ : constant Entity_Id := Etype (Spec_Id);
5235 Utyp : constant Entity_Id := Underlying_Type (Typ);
5238 if not Acts_As_Spec (N)
5239 and then Nkind (Parent (Parent (Spec_Id))) /=
5240 N_Subprogram_Body_Stub
5244 elsif Is_Immutably_Limited_Type (Typ) then
5245 Set_Returns_By_Ref (Spec_Id);
5247 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5248 Set_Returns_By_Ref (Spec_Id);
5252 -- For a procedure, we add a return for all possible syntactic ends of
5255 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5256 Add_Return (Statements (H));
5258 if Present (Exception_Handlers (H)) then
5259 Except_H := First_Non_Pragma (Exception_Handlers (H));
5260 while Present (Except_H) loop
5261 Add_Return (Statements (Except_H));
5262 Next_Non_Pragma (Except_H);
5266 -- For a function, we must deal with the case where there is at least
5267 -- one missing return. What we do is to wrap the entire body of the
5268 -- function in a block:
5281 -- raise Program_Error;
5284 -- This approach is necessary because the raise must be signalled to the
5285 -- caller, not handled by any local handler (RM 6.4(11)).
5287 -- Note: we do not need to analyze the constructed sequence here, since
5288 -- it has no handler, and an attempt to analyze the handled statement
5289 -- sequence twice is risky in various ways (e.g. the issue of expanding
5290 -- cleanup actions twice).
5292 elsif Has_Missing_Return (Spec_Id) then
5294 Hloc : constant Source_Ptr := Sloc (H);
5295 Blok : constant Node_Id :=
5296 Make_Block_Statement (Hloc,
5297 Handled_Statement_Sequence => H);
5298 Rais : constant Node_Id :=
5299 Make_Raise_Program_Error (Hloc,
5300 Reason => PE_Missing_Return);
5303 Set_Handled_Statement_Sequence (N,
5304 Make_Handled_Sequence_Of_Statements (Hloc,
5305 Statements => New_List (Blok, Rais)));
5307 Push_Scope (Spec_Id);
5314 -- If subprogram contains a parameterless recursive call, then we may
5315 -- have an infinite recursion, so see if we can generate code to check
5316 -- for this possibility if storage checks are not suppressed.
5318 if Ekind (Spec_Id) = E_Procedure
5319 and then Has_Recursive_Call (Spec_Id)
5320 and then not Storage_Checks_Suppressed (Spec_Id)
5322 Detect_Infinite_Recursion (N, Spec_Id);
5325 -- Set to encode entity names in package body before gigi is called
5327 Qualify_Entity_Names (N);
5328 end Expand_N_Subprogram_Body;
5330 -----------------------------------
5331 -- Expand_N_Subprogram_Body_Stub --
5332 -----------------------------------
5334 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5336 if Present (Corresponding_Body (N)) then
5337 Expand_N_Subprogram_Body (
5338 Unit_Declaration_Node (Corresponding_Body (N)));
5340 end Expand_N_Subprogram_Body_Stub;
5342 -------------------------------------
5343 -- Expand_N_Subprogram_Declaration --
5344 -------------------------------------
5346 -- If the declaration appears within a protected body, it is a private
5347 -- operation of the protected type. We must create the corresponding
5348 -- protected subprogram an associated formals. For a normal protected
5349 -- operation, this is done when expanding the protected type declaration.
5351 -- If the declaration is for a null procedure, emit null body
5353 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5354 Loc : constant Source_Ptr := Sloc (N);
5355 Subp : constant Entity_Id := Defining_Entity (N);
5356 Scop : constant Entity_Id := Scope (Subp);
5357 Prot_Decl : Node_Id;
5359 Prot_Id : Entity_Id;
5362 -- Deal with case of protected subprogram. Do not generate protected
5363 -- operation if operation is flagged as eliminated.
5365 if Is_List_Member (N)
5366 and then Present (Parent (List_Containing (N)))
5367 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5368 and then Is_Protected_Type (Scop)
5370 if No (Protected_Body_Subprogram (Subp))
5371 and then not Is_Eliminated (Subp)
5374 Make_Subprogram_Declaration (Loc,
5376 Build_Protected_Sub_Specification
5377 (N, Scop, Unprotected_Mode));
5379 -- The protected subprogram is declared outside of the protected
5380 -- body. Given that the body has frozen all entities so far, we
5381 -- analyze the subprogram and perform freezing actions explicitly.
5382 -- including the generation of an explicit freeze node, to ensure
5383 -- that gigi has the proper order of elaboration.
5384 -- If the body is a subunit, the insertion point is before the
5385 -- stub in the parent.
5387 Prot_Bod := Parent (List_Containing (N));
5389 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5390 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5393 Insert_Before (Prot_Bod, Prot_Decl);
5394 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5395 Set_Has_Delayed_Freeze (Prot_Id);
5397 Push_Scope (Scope (Scop));
5398 Analyze (Prot_Decl);
5399 Insert_Actions (N, Freeze_Entity (Prot_Id, Loc));
5400 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5402 -- Create protected operation as well. Even though the operation
5403 -- is only accessible within the body, it is possible to make it
5404 -- available outside of the protected object by using 'Access to
5405 -- provide a callback, so build protected version in all cases.
5408 Make_Subprogram_Declaration (Loc,
5410 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5411 Insert_Before (Prot_Bod, Prot_Decl);
5412 Analyze (Prot_Decl);
5417 -- Ada 2005 (AI-348): Generate body for a null procedure.
5418 -- In most cases this is superfluous because calls to it
5419 -- will be automatically inlined, but we definitely need
5420 -- the body if preconditions for the procedure are present.
5422 elsif Nkind (Specification (N)) = N_Procedure_Specification
5423 and then Null_Present (Specification (N))
5426 Bod : constant Node_Id := Body_To_Inline (N);
5429 Set_Has_Completion (Subp, False);
5430 Append_Freeze_Action (Subp, Bod);
5432 -- The body now contains raise statements, so calls to it will
5435 Set_Is_Inlined (Subp, False);
5438 end Expand_N_Subprogram_Declaration;
5440 --------------------------------
5441 -- Expand_Non_Function_Return --
5442 --------------------------------
5444 procedure Expand_Non_Function_Return (N : Node_Id) is
5445 pragma Assert (No (Expression (N)));
5447 Loc : constant Source_Ptr := Sloc (N);
5448 Scope_Id : Entity_Id :=
5449 Return_Applies_To (Return_Statement_Entity (N));
5450 Kind : constant Entity_Kind := Ekind (Scope_Id);
5453 Goto_Stat : Node_Id;
5457 -- Call _Postconditions procedure if procedure with active
5458 -- postconditions. Here, we use the Postcondition_Proc attribute, which
5459 -- is needed for implicitly-generated returns. Functions never
5460 -- have implicitly-generated returns, and there's no room for
5461 -- Postcondition_Proc in E_Function, so we look up the identifier
5462 -- Name_uPostconditions for function returns (see
5463 -- Expand_Simple_Function_Return).
5465 if Ekind (Scope_Id) = E_Procedure
5466 and then Has_Postconditions (Scope_Id)
5468 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
5470 Make_Procedure_Call_Statement (Loc,
5471 Name => New_Reference_To (Postcondition_Proc (Scope_Id), Loc)));
5474 -- If it is a return from a procedure do no extra steps
5476 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5479 -- If it is a nested return within an extended one, replace it with a
5480 -- return of the previously declared return object.
5482 elsif Kind = E_Return_Statement then
5484 Make_Simple_Return_Statement (Loc,
5486 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5487 Set_Comes_From_Extended_Return_Statement (N);
5488 Set_Return_Statement_Entity (N, Scope_Id);
5489 Expand_Simple_Function_Return (N);
5493 pragma Assert (Is_Entry (Scope_Id));
5495 -- Look at the enclosing block to see whether the return is from an
5496 -- accept statement or an entry body.
5498 for J in reverse 0 .. Scope_Stack.Last loop
5499 Scope_Id := Scope_Stack.Table (J).Entity;
5500 exit when Is_Concurrent_Type (Scope_Id);
5503 -- If it is a return from accept statement it is expanded as call to
5504 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5506 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5507 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5509 if Is_Task_Type (Scope_Id) then
5512 Make_Procedure_Call_Statement (Loc,
5513 Name => New_Reference_To (RTE (RE_Complete_Rendezvous), Loc));
5514 Insert_Before (N, Call);
5515 -- why not insert actions here???
5518 Acc_Stat := Parent (N);
5519 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5520 Acc_Stat := Parent (Acc_Stat);
5523 Lab_Node := Last (Statements
5524 (Handled_Statement_Sequence (Acc_Stat)));
5526 Goto_Stat := Make_Goto_Statement (Loc,
5527 Name => New_Occurrence_Of
5528 (Entity (Identifier (Lab_Node)), Loc));
5530 Set_Analyzed (Goto_Stat);
5532 Rewrite (N, Goto_Stat);
5535 -- If it is a return from an entry body, put a Complete_Entry_Body call
5536 -- in front of the return.
5538 elsif Is_Protected_Type (Scope_Id) then
5540 Make_Procedure_Call_Statement (Loc,
5542 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
5543 Parameter_Associations => New_List (
5544 Make_Attribute_Reference (Loc,
5547 (Find_Protection_Object (Current_Scope), Loc),
5549 Name_Unchecked_Access)));
5551 Insert_Before (N, Call);
5554 end Expand_Non_Function_Return;
5556 ---------------------------------------
5557 -- Expand_Protected_Object_Reference --
5558 ---------------------------------------
5560 function Expand_Protected_Object_Reference
5562 Scop : Entity_Id) return Node_Id
5564 Loc : constant Source_Ptr := Sloc (N);
5572 Make_Identifier (Loc,
5573 Chars => Name_uObject);
5574 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5576 -- Find enclosing protected operation, and retrieve its first parameter,
5577 -- which denotes the enclosing protected object. If the enclosing
5578 -- operation is an entry, we are immediately within the protected body,
5579 -- and we can retrieve the object from the service entries procedure. A
5580 -- barrier function has the same signature as an entry. A barrier
5581 -- function is compiled within the protected object, but unlike
5582 -- protected operations its never needs locks, so that its protected
5583 -- body subprogram points to itself.
5585 Proc := Current_Scope;
5586 while Present (Proc)
5587 and then Scope (Proc) /= Scop
5589 Proc := Scope (Proc);
5592 Corr := Protected_Body_Subprogram (Proc);
5596 -- Previous error left expansion incomplete.
5597 -- Nothing to do on this call.
5604 (First (Parameter_Specifications (Parent (Corr))));
5606 if Is_Subprogram (Proc)
5607 and then Proc /= Corr
5609 -- Protected function or procedure
5611 Set_Entity (Rec, Param);
5613 -- Rec is a reference to an entity which will not be in scope when
5614 -- the call is reanalyzed, and needs no further analysis.
5619 -- Entry or barrier function for entry body. The first parameter of
5620 -- the entry body procedure is pointer to the object. We create a
5621 -- local variable of the proper type, duplicating what is done to
5622 -- define _object later on.
5626 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
5630 Make_Full_Type_Declaration (Loc,
5631 Defining_Identifier => Obj_Ptr,
5633 Make_Access_To_Object_Definition (Loc,
5634 Subtype_Indication =>
5636 (Corresponding_Record_Type (Scop), Loc))));
5638 Insert_Actions (N, Decls);
5639 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
5642 Make_Explicit_Dereference (Loc,
5643 Unchecked_Convert_To (Obj_Ptr,
5644 New_Occurrence_Of (Param, Loc)));
5646 -- Analyze new actual. Other actuals in calls are already analyzed
5647 -- and the list of actuals is not reanalyzed after rewriting.
5649 Set_Parent (Rec, N);
5655 end Expand_Protected_Object_Reference;
5657 --------------------------------------
5658 -- Expand_Protected_Subprogram_Call --
5659 --------------------------------------
5661 procedure Expand_Protected_Subprogram_Call
5669 -- If the protected object is not an enclosing scope, this is
5670 -- an inter-object function call. Inter-object procedure
5671 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
5672 -- The call is intra-object only if the subprogram being
5673 -- called is in the protected body being compiled, and if the
5674 -- protected object in the call is statically the enclosing type.
5675 -- The object may be an component of some other data structure,
5676 -- in which case this must be handled as an inter-object call.
5678 if not In_Open_Scopes (Scop)
5679 or else not Is_Entity_Name (Name (N))
5681 if Nkind (Name (N)) = N_Selected_Component then
5682 Rec := Prefix (Name (N));
5685 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
5686 Rec := Prefix (Prefix (Name (N)));
5689 Build_Protected_Subprogram_Call (N,
5690 Name => New_Occurrence_Of (Subp, Sloc (N)),
5691 Rec => Convert_Concurrent (Rec, Etype (Rec)),
5695 Rec := Expand_Protected_Object_Reference (N, Scop);
5701 Build_Protected_Subprogram_Call (N,
5708 -- If it is a function call it can appear in elaboration code and
5709 -- the called entity must be frozen here.
5711 if Ekind (Subp) = E_Function then
5712 Freeze_Expression (Name (N));
5715 -- Analyze and resolve the new call. The actuals have already been
5716 -- resolved, but expansion of a function call will add extra actuals
5717 -- if needed. Analysis of a procedure call already includes resolution.
5721 if Ekind (Subp) = E_Function then
5722 Resolve (N, Etype (Subp));
5724 end Expand_Protected_Subprogram_Call;
5726 -----------------------------------
5727 -- Expand_Simple_Function_Return --
5728 -----------------------------------
5730 -- The "simple" comes from the syntax rule simple_return_statement.
5731 -- The semantics are not at all simple!
5733 procedure Expand_Simple_Function_Return (N : Node_Id) is
5734 Loc : constant Source_Ptr := Sloc (N);
5736 Scope_Id : constant Entity_Id :=
5737 Return_Applies_To (Return_Statement_Entity (N));
5738 -- The function we are returning from
5740 R_Type : constant Entity_Id := Etype (Scope_Id);
5741 -- The result type of the function
5743 Utyp : constant Entity_Id := Underlying_Type (R_Type);
5745 Exp : constant Node_Id := Expression (N);
5746 pragma Assert (Present (Exp));
5748 Exptyp : constant Entity_Id := Etype (Exp);
5749 -- The type of the expression (not necessarily the same as R_Type)
5751 Subtype_Ind : Node_Id;
5752 -- If the result type of the function is class-wide and the
5753 -- expression has a specific type, then we use the expression's
5754 -- type as the type of the return object. In cases where the
5755 -- expression is an aggregate that is built in place, this avoids
5756 -- the need for an expensive conversion of the return object to
5757 -- the specific type on assignments to the individual components.
5760 if Is_Class_Wide_Type (R_Type)
5761 and then not Is_Class_Wide_Type (Etype (Exp))
5763 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
5765 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
5768 -- For the case of a simple return that does not come from an extended
5769 -- return, in the case of Ada 2005 where we are returning a limited
5770 -- type, we rewrite "return <expression>;" to be:
5772 -- return _anon_ : <return_subtype> := <expression>
5774 -- The expansion produced by Expand_N_Extended_Return_Statement will
5775 -- contain simple return statements (for example, a block containing
5776 -- simple return of the return object), which brings us back here with
5777 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
5778 -- checking for a simple return that does not come from an extended
5779 -- return is to avoid this infinite recursion.
5781 -- The reason for this design is that for Ada 2005 limited returns, we
5782 -- need to reify the return object, so we can build it "in place", and
5783 -- we need a block statement to hang finalization and tasking stuff.
5785 -- ??? In order to avoid disruption, we avoid translating to extended
5786 -- return except in the cases where we really need to (Ada 2005 for
5787 -- inherently limited). We might prefer to do this translation in all
5788 -- cases (except perhaps for the case of Ada 95 inherently limited),
5789 -- in order to fully exercise the Expand_N_Extended_Return_Statement
5790 -- code. This would also allow us to do the build-in-place optimization
5791 -- for efficiency even in cases where it is semantically not required.
5793 -- As before, we check the type of the return expression rather than the
5794 -- return type of the function, because the latter may be a limited
5795 -- class-wide interface type, which is not a limited type, even though
5796 -- the type of the expression may be.
5798 if not Comes_From_Extended_Return_Statement (N)
5799 and then Is_Immutably_Limited_Type (Etype (Expression (N)))
5800 and then Ada_Version >= Ada_2005
5801 and then not Debug_Flag_Dot_L
5804 Return_Object_Entity : constant Entity_Id :=
5805 Make_Temporary (Loc, 'R', Exp);
5806 Obj_Decl : constant Node_Id :=
5807 Make_Object_Declaration (Loc,
5808 Defining_Identifier => Return_Object_Entity,
5809 Object_Definition => Subtype_Ind,
5812 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
5813 Return_Object_Declarations => New_List (Obj_Decl));
5814 -- Do not perform this high-level optimization if the result type
5815 -- is an interface because the "this" pointer must be displaced.
5824 -- Here we have a simple return statement that is part of the expansion
5825 -- of an extended return statement (either written by the user, or
5826 -- generated by the above code).
5828 -- Always normalize C/Fortran boolean result. This is not always needed,
5829 -- but it seems a good idea to minimize the passing around of non-
5830 -- normalized values, and in any case this handles the processing of
5831 -- barrier functions for protected types, which turn the condition into
5832 -- a return statement.
5834 if Is_Boolean_Type (Exptyp)
5835 and then Nonzero_Is_True (Exptyp)
5837 Adjust_Condition (Exp);
5838 Adjust_Result_Type (Exp, Exptyp);
5841 -- Do validity check if enabled for returns
5843 if Validity_Checks_On
5844 and then Validity_Check_Returns
5849 -- Check the result expression of a scalar function against the subtype
5850 -- of the function by inserting a conversion. This conversion must
5851 -- eventually be performed for other classes of types, but for now it's
5852 -- only done for scalars.
5855 if Is_Scalar_Type (Exptyp) then
5856 Rewrite (Exp, Convert_To (R_Type, Exp));
5858 -- The expression is resolved to ensure that the conversion gets
5859 -- expanded to generate a possible constraint check.
5861 Analyze_And_Resolve (Exp, R_Type);
5864 -- Deal with returning variable length objects and controlled types
5866 -- Nothing to do if we are returning by reference, or this is not a
5867 -- type that requires special processing (indicated by the fact that
5868 -- it requires a cleanup scope for the secondary stack case).
5870 if Is_Immutably_Limited_Type (Exptyp)
5871 or else Is_Limited_Interface (Exptyp)
5875 elsif not Requires_Transient_Scope (R_Type) then
5877 -- Mutable records with no variable length components are not
5878 -- returned on the sec-stack, so we need to make sure that the
5879 -- backend will only copy back the size of the actual value, and not
5880 -- the maximum size. We create an actual subtype for this purpose.
5883 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
5887 if Has_Discriminants (Ubt)
5888 and then not Is_Constrained (Ubt)
5889 and then not Has_Unchecked_Union (Ubt)
5891 Decl := Build_Actual_Subtype (Ubt, Exp);
5892 Ent := Defining_Identifier (Decl);
5893 Insert_Action (Exp, Decl);
5894 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
5895 Analyze_And_Resolve (Exp);
5899 -- Here if secondary stack is used
5902 -- Make sure that no surrounding block will reclaim the secondary
5903 -- stack on which we are going to put the result. Not only may this
5904 -- introduce secondary stack leaks but worse, if the reclamation is
5905 -- done too early, then the result we are returning may get
5912 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
5913 Set_Sec_Stack_Needed_For_Return (S, True);
5914 S := Enclosing_Dynamic_Scope (S);
5918 -- Optimize the case where the result is a function call. In this
5919 -- case either the result is already on the secondary stack, or is
5920 -- already being returned with the stack pointer depressed and no
5921 -- further processing is required except to set the By_Ref flag to
5922 -- ensure that gigi does not attempt an extra unnecessary copy.
5923 -- (actually not just unnecessary but harmfully wrong in the case
5924 -- of a controlled type, where gigi does not know how to do a copy).
5925 -- To make up for a gcc 2.8.1 deficiency (???), we perform
5926 -- the copy for array types if the constrained status of the
5927 -- target type is different from that of the expression.
5929 if Requires_Transient_Scope (Exptyp)
5931 (not Is_Array_Type (Exptyp)
5932 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
5933 or else CW_Or_Has_Controlled_Part (Utyp))
5934 and then Nkind (Exp) = N_Function_Call
5938 -- Remove side effects from the expression now so that other parts
5939 -- of the expander do not have to reanalyze this node without this
5942 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
5944 -- For controlled types, do the allocation on the secondary stack
5945 -- manually in order to call adjust at the right time:
5947 -- type Anon1 is access R_Type;
5948 -- for Anon1'Storage_pool use ss_pool;
5949 -- Anon2 : anon1 := new R_Type'(expr);
5950 -- return Anon2.all;
5952 -- We do the same for classwide types that are not potentially
5953 -- controlled (by the virtue of restriction No_Finalization) because
5954 -- gigi is not able to properly allocate class-wide types.
5956 elsif CW_Or_Has_Controlled_Part (Utyp) then
5958 Loc : constant Source_Ptr := Sloc (N);
5959 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
5960 Alloc_Node : Node_Id;
5964 Set_Ekind (Acc_Typ, E_Access_Type);
5966 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
5968 -- This is an allocator for the secondary stack, and it's fine
5969 -- to have Comes_From_Source set False on it, as gigi knows not
5970 -- to flag it as a violation of No_Implicit_Heap_Allocations.
5973 Make_Allocator (Loc,
5975 Make_Qualified_Expression (Loc,
5976 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
5977 Expression => Relocate_Node (Exp)));
5979 -- We do not want discriminant checks on the declaration,
5980 -- given that it gets its value from the allocator.
5982 Set_No_Initialization (Alloc_Node);
5984 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
5986 Insert_List_Before_And_Analyze (N, New_List (
5987 Make_Full_Type_Declaration (Loc,
5988 Defining_Identifier => Acc_Typ,
5990 Make_Access_To_Object_Definition (Loc,
5991 Subtype_Indication => Subtype_Ind)),
5993 Make_Object_Declaration (Loc,
5994 Defining_Identifier => Temp,
5995 Object_Definition => New_Reference_To (Acc_Typ, Loc),
5996 Expression => Alloc_Node)));
5999 Make_Explicit_Dereference (Loc,
6000 Prefix => New_Reference_To (Temp, Loc)));
6002 Analyze_And_Resolve (Exp, R_Type);
6005 -- Otherwise use the gigi mechanism to allocate result on the
6009 Check_Restriction (No_Secondary_Stack, N);
6010 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6012 -- If we are generating code for the VM do not use
6013 -- SS_Allocate since everything is heap-allocated anyway.
6015 if VM_Target = No_VM then
6016 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6021 -- Implement the rules of 6.5(8-10), which require a tag check in the
6022 -- case of a limited tagged return type, and tag reassignment for
6023 -- nonlimited tagged results. These actions are needed when the return
6024 -- type is a specific tagged type and the result expression is a
6025 -- conversion or a formal parameter, because in that case the tag of the
6026 -- expression might differ from the tag of the specific result type.
6028 if Is_Tagged_Type (Utyp)
6029 and then not Is_Class_Wide_Type (Utyp)
6030 and then (Nkind_In (Exp, N_Type_Conversion,
6031 N_Unchecked_Type_Conversion)
6032 or else (Is_Entity_Name (Exp)
6033 and then Ekind (Entity (Exp)) in Formal_Kind))
6035 -- When the return type is limited, perform a check that the
6036 -- tag of the result is the same as the tag of the return type.
6038 if Is_Limited_Type (R_Type) then
6040 Make_Raise_Constraint_Error (Loc,
6044 Make_Selected_Component (Loc,
6045 Prefix => Duplicate_Subexpr (Exp),
6047 Make_Identifier (Loc, Chars => Name_uTag)),
6049 Make_Attribute_Reference (Loc,
6050 Prefix => New_Occurrence_Of (Base_Type (Utyp), Loc),
6051 Attribute_Name => Name_Tag)),
6052 Reason => CE_Tag_Check_Failed));
6054 -- If the result type is a specific nonlimited tagged type, then we
6055 -- have to ensure that the tag of the result is that of the result
6056 -- type. This is handled by making a copy of the expression in the
6057 -- case where it might have a different tag, namely when the
6058 -- expression is a conversion or a formal parameter. We create a new
6059 -- object of the result type and initialize it from the expression,
6060 -- which will implicitly force the tag to be set appropriately.
6064 ExpR : constant Node_Id := Relocate_Node (Exp);
6065 Result_Id : constant Entity_Id :=
6066 Make_Temporary (Loc, 'R', ExpR);
6067 Result_Exp : constant Node_Id :=
6068 New_Reference_To (Result_Id, Loc);
6069 Result_Obj : constant Node_Id :=
6070 Make_Object_Declaration (Loc,
6071 Defining_Identifier => Result_Id,
6072 Object_Definition =>
6073 New_Reference_To (R_Type, Loc),
6074 Constant_Present => True,
6075 Expression => ExpR);
6078 Set_Assignment_OK (Result_Obj);
6079 Insert_Action (Exp, Result_Obj);
6081 Rewrite (Exp, Result_Exp);
6082 Analyze_And_Resolve (Exp, R_Type);
6086 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6087 -- a check that the level of the return expression's underlying type
6088 -- is not deeper than the level of the master enclosing the function.
6089 -- Always generate the check when the type of the return expression
6090 -- is class-wide, when it's a type conversion, or when it's a formal
6091 -- parameter. Otherwise, suppress the check in the case where the
6092 -- return expression has a specific type whose level is known not to
6093 -- be statically deeper than the function's result type.
6095 -- Note: accessibility check is skipped in the VM case, since there
6096 -- does not seem to be any practical way to implement this check.
6098 elsif Ada_Version >= Ada_2005
6099 and then Tagged_Type_Expansion
6100 and then Is_Class_Wide_Type (R_Type)
6101 and then not Scope_Suppress (Accessibility_Check)
6103 (Is_Class_Wide_Type (Etype (Exp))
6104 or else Nkind_In (Exp, N_Type_Conversion,
6105 N_Unchecked_Type_Conversion)
6106 or else (Is_Entity_Name (Exp)
6107 and then Ekind (Entity (Exp)) in Formal_Kind)
6108 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6109 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6115 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6116 -- "this" to reference the base of the object --- required to get
6117 -- access to the TSD of the object.
6119 if Is_Class_Wide_Type (Etype (Exp))
6120 and then Is_Interface (Etype (Exp))
6121 and then Nkind (Exp) = N_Explicit_Dereference
6124 Make_Explicit_Dereference (Loc,
6125 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6126 Make_Function_Call (Loc,
6127 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
6128 Parameter_Associations => New_List (
6129 Unchecked_Convert_To (RTE (RE_Address),
6130 Duplicate_Subexpr (Prefix (Exp)))))));
6133 Make_Attribute_Reference (Loc,
6134 Prefix => Duplicate_Subexpr (Exp),
6135 Attribute_Name => Name_Tag);
6139 Make_Raise_Program_Error (Loc,
6143 Build_Get_Access_Level (Loc, Tag_Node),
6145 Make_Integer_Literal (Loc,
6146 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6147 Reason => PE_Accessibility_Check_Failed));
6150 -- AI05-0073: If function has a controlling access result, check that
6151 -- the tag of the return value, if it is not null, matches designated
6152 -- type of return type.
6153 -- The return expression is referenced twice in the code below, so
6154 -- it must be made free of side effects. Given that different compilers
6155 -- may evaluate these parameters in different order, both occurrences
6158 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6159 and then Has_Controlling_Result (Scope_Id)
6162 Make_Raise_Constraint_Error (Loc,
6167 Left_Opnd => Duplicate_Subexpr (Exp),
6168 Right_Opnd => Make_Null (Loc)),
6169 Right_Opnd => Make_Op_Ne (Loc,
6171 Make_Selected_Component (Loc,
6172 Prefix => Duplicate_Subexpr (Exp),
6174 Make_Identifier (Loc, Chars => Name_uTag)),
6176 Make_Attribute_Reference (Loc,
6178 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6179 Attribute_Name => Name_Tag))),
6180 Reason => CE_Tag_Check_Failed),
6181 Suppress => All_Checks);
6184 -- If we are returning an object that may not be bit-aligned, then copy
6185 -- the value into a temporary first. This copy may need to expand to a
6186 -- loop of component operations.
6188 if Is_Possibly_Unaligned_Slice (Exp)
6189 or else Is_Possibly_Unaligned_Object (Exp)
6192 ExpR : constant Node_Id := Relocate_Node (Exp);
6193 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6196 Make_Object_Declaration (Loc,
6197 Defining_Identifier => Tnn,
6198 Constant_Present => True,
6199 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6200 Expression => ExpR),
6201 Suppress => All_Checks);
6202 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6206 -- Generate call to postcondition checks if they are present
6208 if Ekind (Scope_Id) = E_Function
6209 and then Has_Postconditions (Scope_Id)
6211 -- We are going to reference the returned value twice in this case,
6212 -- once in the call to _Postconditions, and once in the actual return
6213 -- statement, but we can't have side effects happening twice, and in
6214 -- any case for efficiency we don't want to do the computation twice.
6216 -- If the returned expression is an entity name, we don't need to
6217 -- worry since it is efficient and safe to reference it twice, that's
6218 -- also true for literals other than string literals, and for the
6219 -- case of X.all where X is an entity name.
6221 if Is_Entity_Name (Exp)
6222 or else Nkind_In (Exp, N_Character_Literal,
6225 or else (Nkind (Exp) = N_Explicit_Dereference
6226 and then Is_Entity_Name (Prefix (Exp)))
6230 -- Otherwise we are going to need a temporary to capture the value
6234 ExpR : constant Node_Id := Relocate_Node (Exp);
6235 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6238 -- For a complex expression of an elementary type, capture
6239 -- value in the temporary and use it as the reference.
6241 if Is_Elementary_Type (R_Type) then
6243 Make_Object_Declaration (Loc,
6244 Defining_Identifier => Tnn,
6245 Constant_Present => True,
6246 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6247 Expression => ExpR),
6248 Suppress => All_Checks);
6250 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6252 -- If we have something we can rename, generate a renaming of
6253 -- the object and replace the expression with a reference
6255 elsif Is_Object_Reference (Exp) then
6257 Make_Object_Renaming_Declaration (Loc,
6258 Defining_Identifier => Tnn,
6259 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
6261 Suppress => All_Checks);
6263 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6265 -- Otherwise we have something like a string literal or an
6266 -- aggregate. We could copy the value, but that would be
6267 -- inefficient. Instead we make a reference to the value and
6268 -- capture this reference with a renaming, the expression is
6269 -- then replaced by a dereference of this renaming.
6272 -- For now, copy the value, since the code below does not
6273 -- seem to work correctly ???
6276 Make_Object_Declaration (Loc,
6277 Defining_Identifier => Tnn,
6278 Constant_Present => True,
6279 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6280 Expression => Relocate_Node (Exp)),
6281 Suppress => All_Checks);
6283 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6285 -- Insert_Action (Exp,
6286 -- Make_Object_Renaming_Declaration (Loc,
6287 -- Defining_Identifier => Tnn,
6288 -- Access_Definition =>
6289 -- Make_Access_Definition (Loc,
6290 -- All_Present => True,
6291 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
6293 -- Make_Reference (Loc,
6294 -- Prefix => Relocate_Node (Exp))),
6295 -- Suppress => All_Checks);
6298 -- Make_Explicit_Dereference (Loc,
6299 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
6304 -- Generate call to _postconditions
6307 Make_Procedure_Call_Statement (Loc,
6308 Name => Make_Identifier (Loc, Name_uPostconditions),
6309 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
6312 -- Ada 2005 (AI-251): If this return statement corresponds with an
6313 -- simple return statement associated with an extended return statement
6314 -- and the type of the returned object is an interface then generate an
6315 -- implicit conversion to force displacement of the "this" pointer.
6317 if Ada_Version >= Ada_2005
6318 and then Comes_From_Extended_Return_Statement (N)
6319 and then Nkind (Expression (N)) = N_Identifier
6320 and then Is_Interface (Utyp)
6321 and then Utyp /= Underlying_Type (Exptyp)
6323 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6324 Analyze_And_Resolve (Exp);
6326 end Expand_Simple_Function_Return;
6328 --------------------------------
6329 -- Is_Build_In_Place_Function --
6330 --------------------------------
6332 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
6334 -- This function is called from Expand_Subtype_From_Expr during
6335 -- semantic analysis, even when expansion is off. In those cases
6336 -- the build_in_place expansion will not take place.
6338 if not Expander_Active then
6342 -- For now we test whether E denotes a function or access-to-function
6343 -- type whose result subtype is inherently limited. Later this test may
6344 -- be revised to allow composite nonlimited types. Functions with a
6345 -- foreign convention or whose result type has a foreign convention
6348 if Ekind_In (E, E_Function, E_Generic_Function)
6349 or else (Ekind (E) = E_Subprogram_Type
6350 and then Etype (E) /= Standard_Void_Type)
6352 -- Note: If you have Convention (C) on an inherently limited type,
6353 -- you're on your own. That is, the C code will have to be carefully
6354 -- written to know about the Ada conventions.
6356 if Has_Foreign_Convention (E)
6357 or else Has_Foreign_Convention (Etype (E))
6361 -- In Ada 2005 all functions with an inherently limited return type
6362 -- must be handled using a build-in-place profile, including the case
6363 -- of a function with a limited interface result, where the function
6364 -- may return objects of nonlimited descendants.
6367 return Is_Immutably_Limited_Type (Etype (E))
6368 and then Ada_Version >= Ada_2005
6369 and then not Debug_Flag_Dot_L;
6375 end Is_Build_In_Place_Function;
6377 -------------------------------------
6378 -- Is_Build_In_Place_Function_Call --
6379 -------------------------------------
6381 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
6382 Exp_Node : Node_Id := N;
6383 Function_Id : Entity_Id;
6386 -- Step past qualification or unchecked conversion (the latter can occur
6387 -- in cases of calls to 'Input).
6390 (Exp_Node, N_Qualified_Expression, N_Unchecked_Type_Conversion)
6392 Exp_Node := Expression (N);
6395 if Nkind (Exp_Node) /= N_Function_Call then
6399 if Is_Entity_Name (Name (Exp_Node)) then
6400 Function_Id := Entity (Name (Exp_Node));
6402 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
6403 Function_Id := Etype (Name (Exp_Node));
6406 return Is_Build_In_Place_Function (Function_Id);
6408 end Is_Build_In_Place_Function_Call;
6410 -----------------------
6411 -- Freeze_Subprogram --
6412 -----------------------
6414 procedure Freeze_Subprogram (N : Node_Id) is
6415 Loc : constant Source_Ptr := Sloc (N);
6417 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
6418 -- (Ada 2005): Register a predefined primitive in all the secondary
6419 -- dispatch tables of its primitive type.
6421 ----------------------------------
6422 -- Register_Predefined_DT_Entry --
6423 ----------------------------------
6425 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
6426 Iface_DT_Ptr : Elmt_Id;
6427 Tagged_Typ : Entity_Id;
6428 Thunk_Id : Entity_Id;
6429 Thunk_Code : Node_Id;
6432 Tagged_Typ := Find_Dispatching_Type (Prim);
6434 if No (Access_Disp_Table (Tagged_Typ))
6435 or else not Has_Interfaces (Tagged_Typ)
6436 or else not RTE_Available (RE_Interface_Tag)
6437 or else Restriction_Active (No_Dispatching_Calls)
6442 -- Skip the first two access-to-dispatch-table pointers since they
6443 -- leads to the primary dispatch table (predefined DT and user
6444 -- defined DT). We are only concerned with the secondary dispatch
6445 -- table pointers. Note that the access-to- dispatch-table pointer
6446 -- corresponds to the first implemented interface retrieved below.
6449 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
6451 while Present (Iface_DT_Ptr)
6452 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
6454 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
6455 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
6457 if Present (Thunk_Code) then
6458 Insert_Actions_After (N, New_List (
6461 Build_Set_Predefined_Prim_Op_Address (Loc,
6463 New_Reference_To (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
6464 Position => DT_Position (Prim),
6466 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
6467 Make_Attribute_Reference (Loc,
6468 Prefix => New_Reference_To (Thunk_Id, Loc),
6469 Attribute_Name => Name_Unrestricted_Access))),
6471 Build_Set_Predefined_Prim_Op_Address (Loc,
6474 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
6476 Position => DT_Position (Prim),
6478 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
6479 Make_Attribute_Reference (Loc,
6480 Prefix => New_Reference_To (Prim, Loc),
6481 Attribute_Name => Name_Unrestricted_Access)))));
6484 -- Skip the tag of the predefined primitives dispatch table
6486 Next_Elmt (Iface_DT_Ptr);
6487 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
6489 -- Skip the tag of the no-thunks dispatch table
6491 Next_Elmt (Iface_DT_Ptr);
6492 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
6494 -- Skip the tag of the predefined primitives no-thunks dispatch
6497 Next_Elmt (Iface_DT_Ptr);
6498 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
6500 Next_Elmt (Iface_DT_Ptr);
6502 end Register_Predefined_DT_Entry;
6506 Subp : constant Entity_Id := Entity (N);
6508 -- Start of processing for Freeze_Subprogram
6511 -- We suppress the initialization of the dispatch table entry when
6512 -- VM_Target because the dispatching mechanism is handled internally
6515 if Is_Dispatching_Operation (Subp)
6516 and then not Is_Abstract_Subprogram (Subp)
6517 and then Present (DTC_Entity (Subp))
6518 and then Present (Scope (DTC_Entity (Subp)))
6519 and then Tagged_Type_Expansion
6520 and then not Restriction_Active (No_Dispatching_Calls)
6521 and then RTE_Available (RE_Tag)
6524 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
6527 -- Handle private overridden primitives
6529 if not Is_CPP_Class (Typ) then
6530 Check_Overriding_Operation (Subp);
6533 -- We assume that imported CPP primitives correspond with objects
6534 -- whose constructor is in the CPP side; therefore we don't need
6535 -- to generate code to register them in the dispatch table.
6537 if Is_CPP_Class (Typ) then
6540 -- Handle CPP primitives found in derivations of CPP_Class types.
6541 -- These primitives must have been inherited from some parent, and
6542 -- there is no need to register them in the dispatch table because
6543 -- Build_Inherit_Prims takes care of the initialization of these
6546 elsif Is_Imported (Subp)
6547 and then (Convention (Subp) = Convention_CPP
6548 or else Convention (Subp) = Convention_C)
6552 -- Generate code to register the primitive in non statically
6553 -- allocated dispatch tables
6555 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
6557 -- When a primitive is frozen, enter its name in its dispatch
6560 if not Is_Interface (Typ)
6561 or else Present (Interface_Alias (Subp))
6563 if Is_Predefined_Dispatching_Operation (Subp) then
6564 Register_Predefined_DT_Entry (Subp);
6567 Insert_Actions_After (N,
6568 Register_Primitive (Loc, Prim => Subp));
6574 -- Mark functions that return by reference. Note that it cannot be part
6575 -- of the normal semantic analysis of the spec since the underlying
6576 -- returned type may not be known yet (for private types).
6579 Typ : constant Entity_Id := Etype (Subp);
6580 Utyp : constant Entity_Id := Underlying_Type (Typ);
6582 if Is_Immutably_Limited_Type (Typ) then
6583 Set_Returns_By_Ref (Subp);
6584 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6585 Set_Returns_By_Ref (Subp);
6588 end Freeze_Subprogram;
6590 -----------------------
6591 -- Is_Null_Procedure --
6592 -----------------------
6594 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
6595 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
6598 if Ekind (Subp) /= E_Procedure then
6601 -- Check if this is a declared null procedure
6603 elsif Nkind (Decl) = N_Subprogram_Declaration then
6604 if not Null_Present (Specification (Decl)) then
6607 elsif No (Body_To_Inline (Decl)) then
6610 -- Check if the body contains only a null statement, followed by
6611 -- the return statement added during expansion.
6615 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
6621 if Nkind (Orig_Bod) /= N_Subprogram_Body then
6624 -- We must skip SCIL nodes because they are currently
6625 -- implemented as special N_Null_Statement nodes.
6629 (Statements (Handled_Statement_Sequence (Orig_Bod)));
6630 Stat2 := Next_Non_SCIL_Node (Stat);
6633 Is_Empty_List (Declarations (Orig_Bod))
6634 and then Nkind (Stat) = N_Null_Statement
6638 (Nkind (Stat2) = N_Simple_Return_Statement
6639 and then No (Next (Stat2))));
6647 end Is_Null_Procedure;
6649 -------------------------------------------
6650 -- Make_Build_In_Place_Call_In_Allocator --
6651 -------------------------------------------
6653 procedure Make_Build_In_Place_Call_In_Allocator
6654 (Allocator : Node_Id;
6655 Function_Call : Node_Id)
6658 Func_Call : Node_Id := Function_Call;
6659 Function_Id : Entity_Id;
6660 Result_Subt : Entity_Id;
6661 Acc_Type : constant Entity_Id := Etype (Allocator);
6662 New_Allocator : Node_Id;
6663 Return_Obj_Access : Entity_Id;
6666 -- Step past qualification or unchecked conversion (the latter can occur
6667 -- in cases of calls to 'Input).
6669 if Nkind_In (Func_Call,
6670 N_Qualified_Expression,
6671 N_Unchecked_Type_Conversion)
6673 Func_Call := Expression (Func_Call);
6676 -- If the call has already been processed to add build-in-place actuals
6677 -- then return. This should not normally occur in an allocator context,
6678 -- but we add the protection as a defensive measure.
6680 if Is_Expanded_Build_In_Place_Call (Func_Call) then
6684 -- Mark the call as processed as a build-in-place call
6686 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
6688 Loc := Sloc (Function_Call);
6690 if Is_Entity_Name (Name (Func_Call)) then
6691 Function_Id := Entity (Name (Func_Call));
6693 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
6694 Function_Id := Etype (Name (Func_Call));
6697 raise Program_Error;
6700 Result_Subt := Etype (Function_Id);
6702 -- When the result subtype is constrained, the return object must be
6703 -- allocated on the caller side, and access to it is passed to the
6706 -- Here and in related routines, we must examine the full view of the
6707 -- type, because the view at the point of call may differ from that
6708 -- that in the function body, and the expansion mechanism depends on
6709 -- the characteristics of the full view.
6711 if Is_Constrained (Underlying_Type (Result_Subt)) then
6713 -- Replace the initialized allocator of form "new T'(Func (...))"
6714 -- with an uninitialized allocator of form "new T", where T is the
6715 -- result subtype of the called function. The call to the function
6716 -- is handled separately further below.
6719 Make_Allocator (Loc,
6720 Expression => New_Reference_To (Result_Subt, Loc));
6721 Set_No_Initialization (New_Allocator);
6723 -- Copy attributes to new allocator. Note that the new allocator
6724 -- logically comes from source if the original one did, so copy the
6725 -- relevant flag. This ensures proper treatment of the restriction
6726 -- No_Implicit_Heap_Allocations in this case.
6728 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
6729 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
6730 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
6732 Rewrite (Allocator, New_Allocator);
6734 -- Create a new access object and initialize it to the result of the
6735 -- new uninitialized allocator. Note: we do not use Allocator as the
6736 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
6737 -- as this would create a sort of infinite "recursion".
6739 Return_Obj_Access := Make_Temporary (Loc, 'R');
6740 Set_Etype (Return_Obj_Access, Acc_Type);
6742 Insert_Action (Allocator,
6743 Make_Object_Declaration (Loc,
6744 Defining_Identifier => Return_Obj_Access,
6745 Object_Definition => New_Reference_To (Acc_Type, Loc),
6746 Expression => Relocate_Node (Allocator)));
6748 -- When the function has a controlling result, an allocation-form
6749 -- parameter must be passed indicating that the caller is allocating
6750 -- the result object. This is needed because such a function can be
6751 -- called as a dispatching operation and must be treated similarly
6752 -- to functions with unconstrained result subtypes.
6754 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6755 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
6757 Add_Final_List_Actual_To_Build_In_Place_Call
6758 (Func_Call, Function_Id, Acc_Type);
6760 Add_Task_Actuals_To_Build_In_Place_Call
6761 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
6763 -- Add an implicit actual to the function call that provides access
6764 -- to the allocated object. An unchecked conversion to the (specific)
6765 -- result subtype of the function is inserted to handle cases where
6766 -- the access type of the allocator has a class-wide designated type.
6768 Add_Access_Actual_To_Build_In_Place_Call
6771 Make_Unchecked_Type_Conversion (Loc,
6772 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
6774 Make_Explicit_Dereference (Loc,
6775 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
6777 -- When the result subtype is unconstrained, the function itself must
6778 -- perform the allocation of the return object, so we pass parameters
6779 -- indicating that. We don't yet handle the case where the allocation
6780 -- must be done in a user-defined storage pool, which will require
6781 -- passing another actual or two to provide allocation/deallocation
6786 -- Pass an allocation parameter indicating that the function should
6787 -- allocate its result on the heap.
6789 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6790 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
6792 Add_Final_List_Actual_To_Build_In_Place_Call
6793 (Func_Call, Function_Id, Acc_Type);
6795 Add_Task_Actuals_To_Build_In_Place_Call
6796 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
6798 -- The caller does not provide the return object in this case, so we
6799 -- have to pass null for the object access actual.
6801 Add_Access_Actual_To_Build_In_Place_Call
6802 (Func_Call, Function_Id, Return_Object => Empty);
6805 -- Finally, replace the allocator node with a reference to the result
6806 -- of the function call itself (which will effectively be an access
6807 -- to the object created by the allocator).
6809 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
6810 Analyze_And_Resolve (Allocator, Acc_Type);
6811 end Make_Build_In_Place_Call_In_Allocator;
6813 ---------------------------------------------------
6814 -- Make_Build_In_Place_Call_In_Anonymous_Context --
6815 ---------------------------------------------------
6817 procedure Make_Build_In_Place_Call_In_Anonymous_Context
6818 (Function_Call : Node_Id)
6821 Func_Call : Node_Id := Function_Call;
6822 Function_Id : Entity_Id;
6823 Result_Subt : Entity_Id;
6824 Return_Obj_Id : Entity_Id;
6825 Return_Obj_Decl : Entity_Id;
6828 -- Step past qualification or unchecked conversion (the latter can occur
6829 -- in cases of calls to 'Input).
6831 if Nkind_In (Func_Call, N_Qualified_Expression,
6832 N_Unchecked_Type_Conversion)
6834 Func_Call := Expression (Func_Call);
6837 -- If the call has already been processed to add build-in-place actuals
6838 -- then return. One place this can occur is for calls to build-in-place
6839 -- functions that occur within a call to a protected operation, where
6840 -- due to rewriting and expansion of the protected call there can be
6841 -- more than one call to Expand_Actuals for the same set of actuals.
6843 if Is_Expanded_Build_In_Place_Call (Func_Call) then
6847 -- Mark the call as processed as a build-in-place call
6849 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
6851 Loc := Sloc (Function_Call);
6853 if Is_Entity_Name (Name (Func_Call)) then
6854 Function_Id := Entity (Name (Func_Call));
6856 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
6857 Function_Id := Etype (Name (Func_Call));
6860 raise Program_Error;
6863 Result_Subt := Etype (Function_Id);
6865 -- When the result subtype is constrained, an object of the subtype is
6866 -- declared and an access value designating it is passed as an actual.
6868 if Is_Constrained (Underlying_Type (Result_Subt)) then
6870 -- Create a temporary object to hold the function result
6872 Return_Obj_Id := Make_Temporary (Loc, 'R');
6873 Set_Etype (Return_Obj_Id, Result_Subt);
6876 Make_Object_Declaration (Loc,
6877 Defining_Identifier => Return_Obj_Id,
6878 Aliased_Present => True,
6879 Object_Definition => New_Reference_To (Result_Subt, Loc));
6881 Set_No_Initialization (Return_Obj_Decl);
6883 Insert_Action (Func_Call, Return_Obj_Decl);
6885 -- When the function has a controlling result, an allocation-form
6886 -- parameter must be passed indicating that the caller is allocating
6887 -- the result object. This is needed because such a function can be
6888 -- called as a dispatching operation and must be treated similarly
6889 -- to functions with unconstrained result subtypes.
6891 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6892 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
6894 Add_Final_List_Actual_To_Build_In_Place_Call
6895 (Func_Call, Function_Id, Acc_Type => Empty);
6897 Add_Task_Actuals_To_Build_In_Place_Call
6898 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
6900 -- Add an implicit actual to the function call that provides access
6901 -- to the caller's return object.
6903 Add_Access_Actual_To_Build_In_Place_Call
6904 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
6906 -- When the result subtype is unconstrained, the function must allocate
6907 -- the return object in the secondary stack, so appropriate implicit
6908 -- parameters are added to the call to indicate that. A transient
6909 -- scope is established to ensure eventual cleanup of the result.
6912 -- Pass an allocation parameter indicating that the function should
6913 -- allocate its result on the secondary stack.
6915 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6916 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
6918 Add_Final_List_Actual_To_Build_In_Place_Call
6919 (Func_Call, Function_Id, Acc_Type => Empty);
6921 Add_Task_Actuals_To_Build_In_Place_Call
6922 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
6924 -- Pass a null value to the function since no return object is
6925 -- available on the caller side.
6927 Add_Access_Actual_To_Build_In_Place_Call
6928 (Func_Call, Function_Id, Empty);
6930 end Make_Build_In_Place_Call_In_Anonymous_Context;
6932 --------------------------------------------
6933 -- Make_Build_In_Place_Call_In_Assignment --
6934 --------------------------------------------
6936 procedure Make_Build_In_Place_Call_In_Assignment
6938 Function_Call : Node_Id)
6940 Lhs : constant Node_Id := Name (Assign);
6941 Func_Call : Node_Id := Function_Call;
6942 Func_Id : Entity_Id;
6946 Ptr_Typ : Entity_Id;
6947 Ptr_Typ_Decl : Node_Id;
6948 Result_Subt : Entity_Id;
6952 -- Step past qualification or unchecked conversion (the latter can occur
6953 -- in cases of calls to 'Input).
6955 if Nkind_In (Func_Call, N_Qualified_Expression,
6956 N_Unchecked_Type_Conversion)
6958 Func_Call := Expression (Func_Call);
6961 -- If the call has already been processed to add build-in-place actuals
6962 -- then return. This should not normally occur in an assignment context,
6963 -- but we add the protection as a defensive measure.
6965 if Is_Expanded_Build_In_Place_Call (Func_Call) then
6969 -- Mark the call as processed as a build-in-place call
6971 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
6973 Loc := Sloc (Function_Call);
6975 if Is_Entity_Name (Name (Func_Call)) then
6976 Func_Id := Entity (Name (Func_Call));
6978 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
6979 Func_Id := Etype (Name (Func_Call));
6982 raise Program_Error;
6985 Result_Subt := Etype (Func_Id);
6987 -- When the result subtype is unconstrained, an additional actual must
6988 -- be passed to indicate that the caller is providing the return object.
6989 -- This parameter must also be passed when the called function has a
6990 -- controlling result, because dispatching calls to the function needs
6991 -- to be treated effectively the same as calls to class-wide functions.
6993 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6994 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
6996 -- If Lhs is a selected component, then pass it along so that its prefix
6997 -- object will be used as the source of the finalization list.
6999 if Nkind (Lhs) = N_Selected_Component then
7000 Add_Final_List_Actual_To_Build_In_Place_Call
7001 (Func_Call, Func_Id, Acc_Type => Empty, Sel_Comp => Lhs);
7003 Add_Final_List_Actual_To_Build_In_Place_Call
7004 (Func_Call, Func_Id, Acc_Type => Empty);
7007 Add_Task_Actuals_To_Build_In_Place_Call
7008 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
7010 -- Add an implicit actual to the function call that provides access to
7011 -- the caller's return object.
7013 Add_Access_Actual_To_Build_In_Place_Call
7016 Make_Unchecked_Type_Conversion (Loc,
7017 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
7018 Expression => Relocate_Node (Lhs)));
7020 -- Create an access type designating the function's result subtype
7022 Ptr_Typ := Make_Temporary (Loc, 'A');
7025 Make_Full_Type_Declaration (Loc,
7026 Defining_Identifier => Ptr_Typ,
7028 Make_Access_To_Object_Definition (Loc,
7029 All_Present => True,
7030 Subtype_Indication =>
7031 New_Reference_To (Result_Subt, Loc)));
7032 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
7034 -- Finally, create an access object initialized to a reference to the
7037 Obj_Id := Make_Temporary (Loc, 'R');
7038 Set_Etype (Obj_Id, Ptr_Typ);
7041 Make_Object_Declaration (Loc,
7042 Defining_Identifier => Obj_Id,
7043 Object_Definition =>
7044 New_Reference_To (Ptr_Typ, Loc),
7046 Make_Reference (Loc,
7047 Prefix => Relocate_Node (Func_Call)));
7048 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
7050 Rewrite (Assign, Make_Null_Statement (Loc));
7052 -- Retrieve the target of the assignment
7054 if Nkind (Lhs) = N_Selected_Component then
7055 Target := Selector_Name (Lhs);
7056 elsif Nkind (Lhs) = N_Type_Conversion then
7057 Target := Expression (Lhs);
7062 -- If we are assigning to a return object or this is an expression of
7063 -- an extension aggregate, the target should either be an identifier
7064 -- or a simple expression. All other cases imply a different scenario.
7066 if Nkind (Target) in N_Has_Entity then
7067 Target := Entity (Target);
7072 -- When the target of the assignment is a return object of an enclosing
7073 -- build-in-place function and also requires finalization, the list
7074 -- generated for the assignment must be moved to that of the enclosing
7077 -- function Enclosing_BIP_Function return Ctrl_Typ is
7079 -- return (Ctrl_Parent_Part => BIP_Function with ...);
7080 -- end Enclosing_BIP_Function;
7082 if Is_Return_Object (Target)
7083 and then Needs_Finalization (Etype (Target))
7084 and then Needs_Finalization (Result_Subt)
7087 Obj_List : constant Node_Id := Find_Final_List (Obj_Id);
7088 Encl_List : Node_Id;
7089 Encl_Scop : Entity_Id;
7092 Encl_Scop := Scope (Target);
7094 -- Locate the scope of the extended return statement
7096 while Present (Encl_Scop)
7097 and then Ekind (Encl_Scop) /= E_Return_Statement
7099 Encl_Scop := Scope (Encl_Scop);
7102 -- A return object should always be enclosed by a return statement
7103 -- scope at some level.
7105 pragma Assert (Present (Encl_Scop));
7108 Make_Attribute_Reference (Loc,
7111 Finalization_Chain_Entity (Encl_Scop), Loc),
7112 Attribute_Name => Name_Unrestricted_Access);
7114 -- Generate a call to move final list
7116 Insert_After_And_Analyze (Obj_Decl,
7117 Make_Procedure_Call_Statement (Loc,
7119 New_Reference_To (RTE (RE_Move_Final_List), Loc),
7120 Parameter_Associations => New_List (Obj_List, Encl_List)));
7123 end Make_Build_In_Place_Call_In_Assignment;
7125 ----------------------------------------------------
7126 -- Make_Build_In_Place_Call_In_Object_Declaration --
7127 ----------------------------------------------------
7129 procedure Make_Build_In_Place_Call_In_Object_Declaration
7130 (Object_Decl : Node_Id;
7131 Function_Call : Node_Id)
7134 Obj_Def_Id : constant Entity_Id :=
7135 Defining_Identifier (Object_Decl);
7137 Func_Call : Node_Id := Function_Call;
7138 Function_Id : Entity_Id;
7139 Result_Subt : Entity_Id;
7140 Caller_Object : Node_Id;
7141 Call_Deref : Node_Id;
7142 Ref_Type : Entity_Id;
7143 Ptr_Typ_Decl : Node_Id;
7146 Enclosing_Func : Entity_Id;
7147 Pass_Caller_Acc : Boolean := False;
7150 -- Step past qualification or unchecked conversion (the latter can occur
7151 -- in cases of calls to 'Input).
7153 if Nkind_In (Func_Call, N_Qualified_Expression,
7154 N_Unchecked_Type_Conversion)
7156 Func_Call := Expression (Func_Call);
7159 -- If the call has already been processed to add build-in-place actuals
7160 -- then return. This should not normally occur in an object declaration,
7161 -- but we add the protection as a defensive measure.
7163 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7167 -- Mark the call as processed as a build-in-place call
7169 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7171 Loc := Sloc (Function_Call);
7173 if Is_Entity_Name (Name (Func_Call)) then
7174 Function_Id := Entity (Name (Func_Call));
7176 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7177 Function_Id := Etype (Name (Func_Call));
7180 raise Program_Error;
7183 Result_Subt := Etype (Function_Id);
7185 -- In the constrained case, add an implicit actual to the function call
7186 -- that provides access to the declared object. An unchecked conversion
7187 -- to the (specific) result type of the function is inserted to handle
7188 -- the case where the object is declared with a class-wide type.
7190 if Is_Constrained (Underlying_Type (Result_Subt)) then
7192 Make_Unchecked_Type_Conversion (Loc,
7193 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
7194 Expression => New_Reference_To (Obj_Def_Id, Loc));
7196 -- When the function has a controlling result, an allocation-form
7197 -- parameter must be passed indicating that the caller is allocating
7198 -- the result object. This is needed because such a function can be
7199 -- called as a dispatching operation and must be treated similarly
7200 -- to functions with unconstrained result subtypes.
7202 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7203 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7205 -- If the function's result subtype is unconstrained and the object is
7206 -- a return object of an enclosing build-in-place function, then the
7207 -- implicit build-in-place parameters of the enclosing function must be
7208 -- passed along to the called function. (Unfortunately, this won't cover
7209 -- the case of extension aggregates where the ancestor part is a build-
7210 -- in-place unconstrained function call that should be passed along the
7211 -- caller's parameters. Currently those get mishandled by reassigning
7212 -- the result of the call to the aggregate return object, when the call
7213 -- result should really be directly built in place in the aggregate and
7214 -- not built in a temporary. ???)
7216 elsif Is_Return_Object (Defining_Identifier (Object_Decl)) then
7217 Pass_Caller_Acc := True;
7219 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
7221 -- If the enclosing function has a constrained result type, then
7222 -- caller allocation will be used.
7224 if Is_Constrained (Etype (Enclosing_Func)) then
7225 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7226 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7228 -- Otherwise, when the enclosing function has an unconstrained result
7229 -- type, the BIP_Alloc_Form formal of the enclosing function must be
7230 -- passed along to the callee.
7233 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7238 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
7242 -- Retrieve the BIPacc formal from the enclosing function and convert
7243 -- it to the access type of the callee's BIP_Object_Access formal.
7246 Make_Unchecked_Type_Conversion (Loc,
7250 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
7254 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
7257 -- In other unconstrained cases, pass an indication to do the allocation
7258 -- on the secondary stack and set Caller_Object to Empty so that a null
7259 -- value will be passed for the caller's object address. A transient
7260 -- scope is established to ensure eventual cleanup of the result.
7263 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7266 Alloc_Form => Secondary_Stack);
7267 Caller_Object := Empty;
7269 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
7272 Add_Final_List_Actual_To_Build_In_Place_Call
7273 (Func_Call, Function_Id, Acc_Type => Empty);
7275 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
7276 and then Has_Task (Result_Subt)
7278 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
7280 -- Here we're passing along the master that was passed in to this
7283 Add_Task_Actuals_To_Build_In_Place_Call
7284 (Func_Call, Function_Id,
7287 (Build_In_Place_Formal (Enclosing_Func, BIP_Master), Loc));
7290 Add_Task_Actuals_To_Build_In_Place_Call
7291 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7294 Add_Access_Actual_To_Build_In_Place_Call
7295 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
7297 -- Create an access type designating the function's result subtype. We
7298 -- use the type of the original expression because it may be a call to
7299 -- an inherited operation, which the expansion has replaced with the
7300 -- parent operation that yields the parent type.
7302 Ref_Type := Make_Temporary (Loc, 'A');
7305 Make_Full_Type_Declaration (Loc,
7306 Defining_Identifier => Ref_Type,
7308 Make_Access_To_Object_Definition (Loc,
7309 All_Present => True,
7310 Subtype_Indication =>
7311 New_Reference_To (Etype (Function_Call), Loc)));
7313 -- The access type and its accompanying object must be inserted after
7314 -- the object declaration in the constrained case, so that the function
7315 -- call can be passed access to the object. In the unconstrained case,
7316 -- the access type and object must be inserted before the object, since
7317 -- the object declaration is rewritten to be a renaming of a dereference
7318 -- of the access object.
7320 if Is_Constrained (Underlying_Type (Result_Subt)) then
7321 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
7323 Insert_Action (Object_Decl, Ptr_Typ_Decl);
7326 -- Finally, create an access object initialized to a reference to the
7330 Make_Reference (Loc,
7331 Prefix => Relocate_Node (Func_Call));
7333 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
7334 Set_Etype (Def_Id, Ref_Type);
7336 Insert_After_And_Analyze (Ptr_Typ_Decl,
7337 Make_Object_Declaration (Loc,
7338 Defining_Identifier => Def_Id,
7339 Object_Definition => New_Reference_To (Ref_Type, Loc),
7340 Expression => New_Expr));
7342 if Is_Constrained (Underlying_Type (Result_Subt)) then
7343 Set_Expression (Object_Decl, Empty);
7344 Set_No_Initialization (Object_Decl);
7346 -- In case of an unconstrained result subtype, rewrite the object
7347 -- declaration as an object renaming where the renamed object is a
7348 -- dereference of <function_Call>'reference:
7350 -- Obj : Subt renames <function_call>'Ref.all;
7354 Make_Explicit_Dereference (Loc,
7355 Prefix => New_Reference_To (Def_Id, Loc));
7357 Loc := Sloc (Object_Decl);
7358 Rewrite (Object_Decl,
7359 Make_Object_Renaming_Declaration (Loc,
7360 Defining_Identifier => Make_Temporary (Loc, 'D'),
7361 Access_Definition => Empty,
7362 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7363 Name => Call_Deref));
7365 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
7367 Analyze (Object_Decl);
7369 -- Replace the internal identifier of the renaming declaration's
7370 -- entity with identifier of the original object entity. We also have
7371 -- to exchange the entities containing their defining identifiers to
7372 -- ensure the correct replacement of the object declaration by the
7373 -- object renaming declaration to avoid homograph conflicts (since
7374 -- the object declaration's defining identifier was already entered
7375 -- in current scope). The Next_Entity links of the two entities also
7376 -- have to be swapped since the entities are part of the return
7377 -- scope's entity list and the list structure would otherwise be
7378 -- corrupted. Finally, the homonym chain must be preserved as well.
7381 Renaming_Def_Id : constant Entity_Id :=
7382 Defining_Identifier (Object_Decl);
7383 Next_Entity_Temp : constant Entity_Id :=
7384 Next_Entity (Renaming_Def_Id);
7386 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
7388 -- Swap next entity links in preparation for exchanging entities
7390 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
7391 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
7392 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
7394 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
7396 -- Preserve source indication of original declaration, so that
7397 -- xref information is properly generated for the right entity.
7399 Preserve_Comes_From_Source
7400 (Object_Decl, Original_Node (Object_Decl));
7401 Set_Comes_From_Source (Obj_Def_Id, True);
7402 Set_Comes_From_Source (Renaming_Def_Id, False);
7406 -- If the object entity has a class-wide Etype, then we need to change
7407 -- it to the result subtype of the function call, because otherwise the
7408 -- object will be class-wide without an explicit initialization and
7409 -- won't be allocated properly by the back end. It seems unclean to make
7410 -- such a revision to the type at this point, and we should try to
7411 -- improve this treatment when build-in-place functions with class-wide
7412 -- results are implemented. ???
7414 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
7415 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
7417 end Make_Build_In_Place_Call_In_Object_Declaration;
7419 --------------------------
7420 -- Needs_BIP_Final_List --
7421 --------------------------
7423 function Needs_BIP_Final_List (E : Entity_Id) return Boolean is
7424 pragma Assert (Is_Build_In_Place_Function (E));
7425 Result_Subt : constant Entity_Id := Underlying_Type (Etype (E));
7428 -- We need the BIP_Final_List if the result type needs finalization. We
7429 -- also need it for tagged types, even if not class-wide, because some
7430 -- type extension might need finalization, and all overriding functions
7431 -- must have the same calling conventions. However, if there is a
7432 -- pragma Restrictions (No_Finalization), we never need this parameter.
7434 return (Needs_Finalization (Result_Subt)
7435 or else Is_Tagged_Type (Underlying_Type (Result_Subt)))
7436 and then not Restriction_Active (No_Finalization);
7437 end Needs_BIP_Final_List;