* g++.dg/cpp0x/constexpr-53094-2.C: Ignore non-standard ABI
[official-gcc.git] / gcc / ada / exp_ch6.adb
blobcd83d45bddcfa9992a626de491cd4507348d3bcb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Elists; use Elists;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch2; use Exp_Ch2;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Intr; use Exp_Intr;
42 with Exp_Pakd; use Exp_Pakd;
43 with Exp_Tss; use Exp_Tss;
44 with Exp_Util; use Exp_Util;
45 with Exp_VFpt; use Exp_VFpt;
46 with Fname; use Fname;
47 with Freeze; use Freeze;
48 with Inline; use Inline;
49 with Lib; use Lib;
50 with Namet; use Namet;
51 with Nlists; use Nlists;
52 with Nmake; use Nmake;
53 with Opt; use Opt;
54 with Output; use Output;
55 with Restrict; use Restrict;
56 with Rident; use Rident;
57 with Rtsfind; use Rtsfind;
58 with Sem; use Sem;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Ch6; use Sem_Ch6;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch12; use Sem_Ch12;
63 with Sem_Ch13; use Sem_Ch13;
64 with Sem_Dim; use Sem_Dim;
65 with Sem_Disp; use Sem_Disp;
66 with Sem_Dist; use Sem_Dist;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Res; use Sem_Res;
70 with Sem_SCIL; use Sem_SCIL;
71 with Sem_Util; use Sem_Util;
72 with Sinfo; use Sinfo;
73 with Sinput; use Sinput;
74 with Snames; use Snames;
75 with Stand; use Stand;
76 with Targparm; use Targparm;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Validsw; use Validsw;
81 package body Exp_Ch6 is
83 Inlined_Calls : Elist_Id := No_Elist;
84 Backend_Calls : Elist_Id := No_Elist;
85 -- List of frontend inlined calls and inline calls passed to the backend
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
91 procedure Add_Access_Actual_To_Build_In_Place_Call
92 (Function_Call : Node_Id;
93 Function_Id : Entity_Id;
94 Return_Object : Node_Id;
95 Is_Access : Boolean := False);
96 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
97 -- object name given by Return_Object and add the attribute to the end of
98 -- the actual parameter list associated with the build-in-place function
99 -- call denoted by Function_Call. However, if Is_Access is True, then
100 -- Return_Object is already an access expression, in which case it's passed
101 -- along directly to the build-in-place function. Finally, if Return_Object
102 -- is empty, then pass a null literal as the actual.
104 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
105 (Function_Call : Node_Id;
106 Function_Id : Entity_Id;
107 Alloc_Form : BIP_Allocation_Form := Unspecified;
108 Alloc_Form_Exp : Node_Id := Empty;
109 Pool_Actual : Node_Id := Make_Null (No_Location));
110 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
111 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
112 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
113 -- otherwise pass a literal corresponding to the Alloc_Form parameter
114 -- (which must not be Unspecified in that case). Pool_Actual is the
115 -- parameter to pass to BIP_Storage_Pool.
117 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
118 (Func_Call : Node_Id;
119 Func_Id : Entity_Id;
120 Ptr_Typ : Entity_Id := Empty;
121 Master_Exp : Node_Id := Empty);
122 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
123 -- finalization actions, add an actual parameter which is a pointer to the
124 -- finalization master of the caller. If Master_Exp is not Empty, then that
125 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
126 -- will result in an automatic "null" value for the actual.
128 procedure Add_Task_Actuals_To_Build_In_Place_Call
129 (Function_Call : Node_Id;
130 Function_Id : Entity_Id;
131 Master_Actual : Node_Id);
132 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
133 -- contains tasks, add two actual parameters: the master, and a pointer to
134 -- the caller's activation chain. Master_Actual is the actual parameter
135 -- expression to pass for the master. In most cases, this is the current
136 -- master (_master). The two exceptions are: If the function call is the
137 -- initialization expression for an allocator, we pass the master of the
138 -- access type. If the function call is the initialization expression for a
139 -- return object, we pass along the master passed in by the caller. The
140 -- activation chain to pass is always the local one. Note: Master_Actual
141 -- can be Empty, but only if there are no tasks.
143 procedure Check_Overriding_Operation (Subp : Entity_Id);
144 -- Subp is a dispatching operation. Check whether it may override an
145 -- inherited private operation, in which case its DT entry is that of
146 -- the hidden operation, not the one it may have received earlier.
147 -- This must be done before emitting the code to set the corresponding
148 -- DT to the address of the subprogram. The actual placement of Subp in
149 -- the proper place in the list of primitive operations is done in
150 -- Declare_Inherited_Private_Subprograms, which also has to deal with
151 -- implicit operations. This duplication is unavoidable for now???
153 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
154 -- This procedure is called only if the subprogram body N, whose spec
155 -- has the given entity Spec, contains a parameterless recursive call.
156 -- It attempts to generate runtime code to detect if this a case of
157 -- infinite recursion.
159 -- The body is scanned to determine dependencies. If the only external
160 -- dependencies are on a small set of scalar variables, then the values
161 -- of these variables are captured on entry to the subprogram, and if
162 -- the values are not changed for the call, we know immediately that
163 -- we have an infinite recursion.
165 procedure Expand_Ctrl_Function_Call (N : Node_Id);
166 -- N is a function call which returns a controlled object. Transform the
167 -- call into a temporary which retrieves the returned object from the
168 -- secondary stack using 'reference.
170 procedure Expand_Inlined_Call
171 (N : Node_Id;
172 Subp : Entity_Id;
173 Orig_Subp : Entity_Id);
174 -- If called subprogram can be inlined by the front-end, retrieve the
175 -- analyzed body, replace formals with actuals and expand call in place.
176 -- Generate thunks for actuals that are expressions, and insert the
177 -- corresponding constant declarations before the call. If the original
178 -- call is to a derived operation, the return type is the one of the
179 -- derived operation, but the body is that of the original, so return
180 -- expressions in the body must be converted to the desired type (which
181 -- is simply not noted in the tree without inline expansion).
183 procedure Expand_Non_Function_Return (N : Node_Id);
184 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
185 -- a procedure body, entry body, accept statement, or extended return
186 -- statement. Note that all non-function returns are simple return
187 -- statements.
189 function Expand_Protected_Object_Reference
190 (N : Node_Id;
191 Scop : Entity_Id) return Node_Id;
193 procedure Expand_Protected_Subprogram_Call
194 (N : Node_Id;
195 Subp : Entity_Id;
196 Scop : Entity_Id);
197 -- A call to a protected subprogram within the protected object may appear
198 -- as a regular call. The list of actuals must be expanded to contain a
199 -- reference to the object itself, and the call becomes a call to the
200 -- corresponding protected subprogram.
202 function Has_Unconstrained_Access_Discriminants
203 (Subtyp : Entity_Id) return Boolean;
204 -- Returns True if the given subtype is unconstrained and has one
205 -- or more access discriminants.
207 procedure Expand_Simple_Function_Return (N : Node_Id);
208 -- Expand simple return from function. In the case where we are returning
209 -- from a function body this is called by Expand_N_Simple_Return_Statement.
211 ----------------------------------------------
212 -- Add_Access_Actual_To_Build_In_Place_Call --
213 ----------------------------------------------
215 procedure Add_Access_Actual_To_Build_In_Place_Call
216 (Function_Call : Node_Id;
217 Function_Id : Entity_Id;
218 Return_Object : Node_Id;
219 Is_Access : Boolean := False)
221 Loc : constant Source_Ptr := Sloc (Function_Call);
222 Obj_Address : Node_Id;
223 Obj_Acc_Formal : Entity_Id;
225 begin
226 -- Locate the implicit access parameter in the called function
228 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
230 -- If no return object is provided, then pass null
232 if not Present (Return_Object) then
233 Obj_Address := Make_Null (Loc);
234 Set_Parent (Obj_Address, Function_Call);
236 -- If Return_Object is already an expression of an access type, then use
237 -- it directly, since it must be an access value denoting the return
238 -- object, and couldn't possibly be the return object itself.
240 elsif Is_Access then
241 Obj_Address := Return_Object;
242 Set_Parent (Obj_Address, Function_Call);
244 -- Apply Unrestricted_Access to caller's return object
246 else
247 Obj_Address :=
248 Make_Attribute_Reference (Loc,
249 Prefix => Return_Object,
250 Attribute_Name => Name_Unrestricted_Access);
252 Set_Parent (Return_Object, Obj_Address);
253 Set_Parent (Obj_Address, Function_Call);
254 end if;
256 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
258 -- Build the parameter association for the new actual and add it to the
259 -- end of the function's actuals.
261 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
262 end Add_Access_Actual_To_Build_In_Place_Call;
264 ------------------------------------------------------
265 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
266 ------------------------------------------------------
268 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
269 (Function_Call : Node_Id;
270 Function_Id : Entity_Id;
271 Alloc_Form : BIP_Allocation_Form := Unspecified;
272 Alloc_Form_Exp : Node_Id := Empty;
273 Pool_Actual : Node_Id := Make_Null (No_Location))
275 Loc : constant Source_Ptr := Sloc (Function_Call);
276 Alloc_Form_Actual : Node_Id;
277 Alloc_Form_Formal : Node_Id;
278 Pool_Formal : Node_Id;
280 begin
281 -- The allocation form generally doesn't need to be passed in the case
282 -- of a constrained result subtype, since normally the caller performs
283 -- the allocation in that case. However this formal is still needed in
284 -- the case where the function has a tagged result, because generally
285 -- such functions can be called in a dispatching context and such calls
286 -- must be handled like calls to class-wide functions.
288 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
289 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
290 then
291 return;
292 end if;
294 -- Locate the implicit allocation form parameter in the called function.
295 -- Maybe it would be better for each implicit formal of a build-in-place
296 -- function to have a flag or a Uint attribute to identify it. ???
298 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
300 if Present (Alloc_Form_Exp) then
301 pragma Assert (Alloc_Form = Unspecified);
303 Alloc_Form_Actual := Alloc_Form_Exp;
305 else
306 pragma Assert (Alloc_Form /= Unspecified);
308 Alloc_Form_Actual :=
309 Make_Integer_Literal (Loc,
310 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
311 end if;
313 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
315 -- Build the parameter association for the new actual and add it to the
316 -- end of the function's actuals.
318 Add_Extra_Actual_To_Call
319 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
321 -- Pass the Storage_Pool parameter. This parameter is omitted on
322 -- .NET/JVM/ZFP as those targets do not support pools.
324 if VM_Target = No_VM
325 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
326 then
327 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
328 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
329 Add_Extra_Actual_To_Call
330 (Function_Call, Pool_Formal, Pool_Actual);
331 end if;
332 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
334 -----------------------------------------------------------
335 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
336 -----------------------------------------------------------
338 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
339 (Func_Call : Node_Id;
340 Func_Id : Entity_Id;
341 Ptr_Typ : Entity_Id := Empty;
342 Master_Exp : Node_Id := Empty)
344 begin
345 if not Needs_BIP_Finalization_Master (Func_Id) then
346 return;
347 end if;
349 declare
350 Formal : constant Entity_Id :=
351 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
352 Loc : constant Source_Ptr := Sloc (Func_Call);
354 Actual : Node_Id;
355 Desig_Typ : Entity_Id;
357 begin
358 -- If there is a finalization master actual, such as the implicit
359 -- finalization master of an enclosing build-in-place function,
360 -- then this must be added as an extra actual of the call.
362 if Present (Master_Exp) then
363 Actual := Master_Exp;
365 -- Case where the context does not require an actual master
367 elsif No (Ptr_Typ) then
368 Actual := Make_Null (Loc);
370 else
371 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
373 -- Check for a library-level access type whose designated type has
374 -- supressed finalization. Such an access types lack a master.
375 -- Pass a null actual to the callee in order to signal a missing
376 -- master.
378 if Is_Library_Level_Entity (Ptr_Typ)
379 and then Finalize_Storage_Only (Desig_Typ)
380 then
381 Actual := Make_Null (Loc);
383 -- Types in need of finalization actions
385 elsif Needs_Finalization (Desig_Typ) then
387 -- The general mechanism of creating finalization masters for
388 -- anonymous access types is disabled by default, otherwise
389 -- finalization masters will pop all over the place. Such types
390 -- use context-specific masters.
392 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
393 and then No (Finalization_Master (Ptr_Typ))
394 then
395 Build_Finalization_Master
396 (Typ => Ptr_Typ,
397 Ins_Node => Associated_Node_For_Itype (Ptr_Typ),
398 Encl_Scope => Scope (Ptr_Typ));
399 end if;
401 -- Access-to-controlled types should always have a master
403 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
405 Actual :=
406 Make_Attribute_Reference (Loc,
407 Prefix =>
408 New_Reference_To (Finalization_Master (Ptr_Typ), Loc),
409 Attribute_Name => Name_Unrestricted_Access);
411 -- Tagged types
413 else
414 Actual := Make_Null (Loc);
415 end if;
416 end if;
418 Analyze_And_Resolve (Actual, Etype (Formal));
420 -- Build the parameter association for the new actual and add it to
421 -- the end of the function's actuals.
423 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
424 end;
425 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
427 ------------------------------
428 -- Add_Extra_Actual_To_Call --
429 ------------------------------
431 procedure Add_Extra_Actual_To_Call
432 (Subprogram_Call : Node_Id;
433 Extra_Formal : Entity_Id;
434 Extra_Actual : Node_Id)
436 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
437 Param_Assoc : Node_Id;
439 begin
440 Param_Assoc :=
441 Make_Parameter_Association (Loc,
442 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
443 Explicit_Actual_Parameter => Extra_Actual);
445 Set_Parent (Param_Assoc, Subprogram_Call);
446 Set_Parent (Extra_Actual, Param_Assoc);
448 if Present (Parameter_Associations (Subprogram_Call)) then
449 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
450 N_Parameter_Association
451 then
453 -- Find last named actual, and append
455 declare
456 L : Node_Id;
457 begin
458 L := First_Actual (Subprogram_Call);
459 while Present (L) loop
460 if No (Next_Actual (L)) then
461 Set_Next_Named_Actual (Parent (L), Extra_Actual);
462 exit;
463 end if;
464 Next_Actual (L);
465 end loop;
466 end;
468 else
469 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
470 end if;
472 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
474 else
475 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
476 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
477 end if;
478 end Add_Extra_Actual_To_Call;
480 ---------------------------------------------
481 -- Add_Task_Actuals_To_Build_In_Place_Call --
482 ---------------------------------------------
484 procedure Add_Task_Actuals_To_Build_In_Place_Call
485 (Function_Call : Node_Id;
486 Function_Id : Entity_Id;
487 Master_Actual : Node_Id)
489 Loc : constant Source_Ptr := Sloc (Function_Call);
490 Result_Subt : constant Entity_Id :=
491 Available_View (Etype (Function_Id));
492 Actual : Node_Id;
493 Chain_Actual : Node_Id;
494 Chain_Formal : Node_Id;
495 Master_Formal : Node_Id;
497 begin
498 -- No such extra parameters are needed if there are no tasks
500 if not Has_Task (Result_Subt) then
501 return;
502 end if;
504 Actual := Master_Actual;
506 -- Use a dummy _master actual in case of No_Task_Hierarchy
508 if Restriction_Active (No_Task_Hierarchy) then
509 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
511 -- In the case where we use the master associated with an access type,
512 -- the actual is an entity and requires an explicit reference.
514 elsif Nkind (Actual) = N_Defining_Identifier then
515 Actual := New_Reference_To (Actual, Loc);
516 end if;
518 -- Locate the implicit master parameter in the called function
520 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
521 Analyze_And_Resolve (Actual, Etype (Master_Formal));
523 -- Build the parameter association for the new actual and add it to the
524 -- end of the function's actuals.
526 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
528 -- Locate the implicit activation chain parameter in the called function
530 Chain_Formal :=
531 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
533 -- Create the actual which is a pointer to the current activation chain
535 Chain_Actual :=
536 Make_Attribute_Reference (Loc,
537 Prefix => Make_Identifier (Loc, Name_uChain),
538 Attribute_Name => Name_Unrestricted_Access);
540 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
542 -- Build the parameter association for the new actual and add it to the
543 -- end of the function's actuals.
545 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
546 end Add_Task_Actuals_To_Build_In_Place_Call;
548 -----------------------
549 -- BIP_Formal_Suffix --
550 -----------------------
552 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
553 begin
554 case Kind is
555 when BIP_Alloc_Form =>
556 return "BIPalloc";
557 when BIP_Storage_Pool =>
558 return "BIPstoragepool";
559 when BIP_Finalization_Master =>
560 return "BIPfinalizationmaster";
561 when BIP_Task_Master =>
562 return "BIPtaskmaster";
563 when BIP_Activation_Chain =>
564 return "BIPactivationchain";
565 when BIP_Object_Access =>
566 return "BIPaccess";
567 end case;
568 end BIP_Formal_Suffix;
570 ---------------------------
571 -- Build_In_Place_Formal --
572 ---------------------------
574 function Build_In_Place_Formal
575 (Func : Entity_Id;
576 Kind : BIP_Formal_Kind) return Entity_Id
578 Formal_Name : constant Name_Id :=
579 New_External_Name
580 (Chars (Func), BIP_Formal_Suffix (Kind));
581 Extra_Formal : Entity_Id := Extra_Formals (Func);
583 begin
584 -- Maybe it would be better for each implicit formal of a build-in-place
585 -- function to have a flag or a Uint attribute to identify it. ???
587 -- The return type in the function declaration may have been a limited
588 -- view, and the extra formals for the function were not generated at
589 -- that point. At the point of call the full view must be available and
590 -- the extra formals can be created.
592 if No (Extra_Formal) then
593 Create_Extra_Formals (Func);
594 Extra_Formal := Extra_Formals (Func);
595 end if;
597 loop
598 pragma Assert (Present (Extra_Formal));
599 exit when Chars (Extra_Formal) = Formal_Name;
601 Next_Formal_With_Extras (Extra_Formal);
602 end loop;
604 return Extra_Formal;
605 end Build_In_Place_Formal;
607 --------------------------------
608 -- Check_Overriding_Operation --
609 --------------------------------
611 procedure Check_Overriding_Operation (Subp : Entity_Id) is
612 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
613 Op_List : constant Elist_Id := Primitive_Operations (Typ);
614 Op_Elmt : Elmt_Id;
615 Prim_Op : Entity_Id;
616 Par_Op : Entity_Id;
618 begin
619 if Is_Derived_Type (Typ)
620 and then not Is_Private_Type (Typ)
621 and then In_Open_Scopes (Scope (Etype (Typ)))
622 and then Is_Base_Type (Typ)
623 then
624 -- Subp overrides an inherited private operation if there is an
625 -- inherited operation with a different name than Subp (see
626 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
627 -- same name as Subp.
629 Op_Elmt := First_Elmt (Op_List);
630 while Present (Op_Elmt) loop
631 Prim_Op := Node (Op_Elmt);
632 Par_Op := Alias (Prim_Op);
634 if Present (Par_Op)
635 and then not Comes_From_Source (Prim_Op)
636 and then Chars (Prim_Op) /= Chars (Par_Op)
637 and then Chars (Par_Op) = Chars (Subp)
638 and then Is_Hidden (Par_Op)
639 and then Type_Conformant (Prim_Op, Subp)
640 then
641 Set_DT_Position (Subp, DT_Position (Prim_Op));
642 end if;
644 Next_Elmt (Op_Elmt);
645 end loop;
646 end if;
647 end Check_Overriding_Operation;
649 -------------------------------
650 -- Detect_Infinite_Recursion --
651 -------------------------------
653 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
654 Loc : constant Source_Ptr := Sloc (N);
656 Var_List : constant Elist_Id := New_Elmt_List;
657 -- List of globals referenced by body of procedure
659 Call_List : constant Elist_Id := New_Elmt_List;
660 -- List of recursive calls in body of procedure
662 Shad_List : constant Elist_Id := New_Elmt_List;
663 -- List of entity id's for entities created to capture the value of
664 -- referenced globals on entry to the procedure.
666 Scop : constant Uint := Scope_Depth (Spec);
667 -- This is used to record the scope depth of the current procedure, so
668 -- that we can identify global references.
670 Max_Vars : constant := 4;
671 -- Do not test more than four global variables
673 Count_Vars : Natural := 0;
674 -- Count variables found so far
676 Var : Entity_Id;
677 Elm : Elmt_Id;
678 Ent : Entity_Id;
679 Call : Elmt_Id;
680 Decl : Node_Id;
681 Test : Node_Id;
682 Elm1 : Elmt_Id;
683 Elm2 : Elmt_Id;
684 Last : Node_Id;
686 function Process (Nod : Node_Id) return Traverse_Result;
687 -- Function to traverse the subprogram body (using Traverse_Func)
689 -------------
690 -- Process --
691 -------------
693 function Process (Nod : Node_Id) return Traverse_Result is
694 begin
695 -- Procedure call
697 if Nkind (Nod) = N_Procedure_Call_Statement then
699 -- Case of one of the detected recursive calls
701 if Is_Entity_Name (Name (Nod))
702 and then Has_Recursive_Call (Entity (Name (Nod)))
703 and then Entity (Name (Nod)) = Spec
704 then
705 Append_Elmt (Nod, Call_List);
706 return Skip;
708 -- Any other procedure call may have side effects
710 else
711 return Abandon;
712 end if;
714 -- A call to a pure function can always be ignored
716 elsif Nkind (Nod) = N_Function_Call
717 and then Is_Entity_Name (Name (Nod))
718 and then Is_Pure (Entity (Name (Nod)))
719 then
720 return Skip;
722 -- Case of an identifier reference
724 elsif Nkind (Nod) = N_Identifier then
725 Ent := Entity (Nod);
727 -- If no entity, then ignore the reference
729 -- Not clear why this can happen. To investigate, remove this
730 -- test and look at the crash that occurs here in 3401-004 ???
732 if No (Ent) then
733 return Skip;
735 -- Ignore entities with no Scope, again not clear how this
736 -- can happen, to investigate, look at 4108-008 ???
738 elsif No (Scope (Ent)) then
739 return Skip;
741 -- Ignore the reference if not to a more global object
743 elsif Scope_Depth (Scope (Ent)) >= Scop then
744 return Skip;
746 -- References to types, exceptions and constants are always OK
748 elsif Is_Type (Ent)
749 or else Ekind (Ent) = E_Exception
750 or else Ekind (Ent) = E_Constant
751 then
752 return Skip;
754 -- If other than a non-volatile scalar variable, we have some
755 -- kind of global reference (e.g. to a function) that we cannot
756 -- deal with so we forget the attempt.
758 elsif Ekind (Ent) /= E_Variable
759 or else not Is_Scalar_Type (Etype (Ent))
760 or else Treat_As_Volatile (Ent)
761 then
762 return Abandon;
764 -- Otherwise we have a reference to a global scalar
766 else
767 -- Loop through global entities already detected
769 Elm := First_Elmt (Var_List);
770 loop
771 -- If not detected before, record this new global reference
773 if No (Elm) then
774 Count_Vars := Count_Vars + 1;
776 if Count_Vars <= Max_Vars then
777 Append_Elmt (Entity (Nod), Var_List);
778 else
779 return Abandon;
780 end if;
782 exit;
784 -- If recorded before, ignore
786 elsif Node (Elm) = Entity (Nod) then
787 return Skip;
789 -- Otherwise keep looking
791 else
792 Next_Elmt (Elm);
793 end if;
794 end loop;
796 return Skip;
797 end if;
799 -- For all other node kinds, recursively visit syntactic children
801 else
802 return OK;
803 end if;
804 end Process;
806 function Traverse_Body is new Traverse_Func (Process);
808 -- Start of processing for Detect_Infinite_Recursion
810 begin
811 -- Do not attempt detection in No_Implicit_Conditional mode, since we
812 -- won't be able to generate the code to handle the recursion in any
813 -- case.
815 if Restriction_Active (No_Implicit_Conditionals) then
816 return;
817 end if;
819 -- Otherwise do traversal and quit if we get abandon signal
821 if Traverse_Body (N) = Abandon then
822 return;
824 -- We must have a call, since Has_Recursive_Call was set. If not just
825 -- ignore (this is only an error check, so if we have a funny situation,
826 -- due to bugs or errors, we do not want to bomb!)
828 elsif Is_Empty_Elmt_List (Call_List) then
829 return;
830 end if;
832 -- Here is the case where we detect recursion at compile time
834 -- Push our current scope for analyzing the declarations and code that
835 -- we will insert for the checking.
837 Push_Scope (Spec);
839 -- This loop builds temporary variables for each of the referenced
840 -- globals, so that at the end of the loop the list Shad_List contains
841 -- these temporaries in one-to-one correspondence with the elements in
842 -- Var_List.
844 Last := Empty;
845 Elm := First_Elmt (Var_List);
846 while Present (Elm) loop
847 Var := Node (Elm);
848 Ent := Make_Temporary (Loc, 'S');
849 Append_Elmt (Ent, Shad_List);
851 -- Insert a declaration for this temporary at the start of the
852 -- declarations for the procedure. The temporaries are declared as
853 -- constant objects initialized to the current values of the
854 -- corresponding temporaries.
856 Decl :=
857 Make_Object_Declaration (Loc,
858 Defining_Identifier => Ent,
859 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
860 Constant_Present => True,
861 Expression => New_Occurrence_Of (Var, Loc));
863 if No (Last) then
864 Prepend (Decl, Declarations (N));
865 else
866 Insert_After (Last, Decl);
867 end if;
869 Last := Decl;
870 Analyze (Decl);
871 Next_Elmt (Elm);
872 end loop;
874 -- Loop through calls
876 Call := First_Elmt (Call_List);
877 while Present (Call) loop
879 -- Build a predicate expression of the form
881 -- True
882 -- and then global1 = temp1
883 -- and then global2 = temp2
884 -- ...
886 -- This predicate determines if any of the global values
887 -- referenced by the procedure have changed since the
888 -- current call, if not an infinite recursion is assured.
890 Test := New_Occurrence_Of (Standard_True, Loc);
892 Elm1 := First_Elmt (Var_List);
893 Elm2 := First_Elmt (Shad_List);
894 while Present (Elm1) loop
895 Test :=
896 Make_And_Then (Loc,
897 Left_Opnd => Test,
898 Right_Opnd =>
899 Make_Op_Eq (Loc,
900 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
901 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
903 Next_Elmt (Elm1);
904 Next_Elmt (Elm2);
905 end loop;
907 -- Now we replace the call with the sequence
909 -- if no-changes (see above) then
910 -- raise Storage_Error;
911 -- else
912 -- original-call
913 -- end if;
915 Rewrite (Node (Call),
916 Make_If_Statement (Loc,
917 Condition => Test,
918 Then_Statements => New_List (
919 Make_Raise_Storage_Error (Loc,
920 Reason => SE_Infinite_Recursion)),
922 Else_Statements => New_List (
923 Relocate_Node (Node (Call)))));
925 Analyze (Node (Call));
927 Next_Elmt (Call);
928 end loop;
930 -- Remove temporary scope stack entry used for analysis
932 Pop_Scope;
933 end Detect_Infinite_Recursion;
935 --------------------
936 -- Expand_Actuals --
937 --------------------
939 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
940 Loc : constant Source_Ptr := Sloc (N);
941 Actual : Node_Id;
942 Formal : Entity_Id;
943 N_Node : Node_Id;
944 Post_Call : List_Id;
945 E_Formal : Entity_Id;
947 procedure Add_Call_By_Copy_Code;
948 -- For cases where the parameter must be passed by copy, this routine
949 -- generates a temporary variable into which the actual is copied and
950 -- then passes this as the parameter. For an OUT or IN OUT parameter,
951 -- an assignment is also generated to copy the result back. The call
952 -- also takes care of any constraint checks required for the type
953 -- conversion case (on both the way in and the way out).
955 procedure Add_Simple_Call_By_Copy_Code;
956 -- This is similar to the above, but is used in cases where we know
957 -- that all that is needed is to simply create a temporary and copy
958 -- the value in and out of the temporary.
960 procedure Check_Fortran_Logical;
961 -- A value of type Logical that is passed through a formal parameter
962 -- must be normalized because .TRUE. usually does not have the same
963 -- representation as True. We assume that .FALSE. = False = 0.
964 -- What about functions that return a logical type ???
966 function Is_Legal_Copy return Boolean;
967 -- Check that an actual can be copied before generating the temporary
968 -- to be used in the call. If the actual is of a by_reference type then
969 -- the program is illegal (this can only happen in the presence of
970 -- rep. clauses that force an incorrect alignment). If the formal is
971 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
972 -- the effect that this might lead to unaligned arguments.
974 function Make_Var (Actual : Node_Id) return Entity_Id;
975 -- Returns an entity that refers to the given actual parameter,
976 -- Actual (not including any type conversion). If Actual is an
977 -- entity name, then this entity is returned unchanged, otherwise
978 -- a renaming is created to provide an entity for the actual.
980 procedure Reset_Packed_Prefix;
981 -- The expansion of a packed array component reference is delayed in
982 -- the context of a call. Now we need to complete the expansion, so we
983 -- unmark the analyzed bits in all prefixes.
985 ---------------------------
986 -- Add_Call_By_Copy_Code --
987 ---------------------------
989 procedure Add_Call_By_Copy_Code is
990 Expr : Node_Id;
991 Init : Node_Id;
992 Temp : Entity_Id;
993 Indic : Node_Id;
994 Var : Entity_Id;
995 F_Typ : constant Entity_Id := Etype (Formal);
996 V_Typ : Entity_Id;
997 Crep : Boolean;
999 begin
1000 if not Is_Legal_Copy then
1001 return;
1002 end if;
1004 Temp := Make_Temporary (Loc, 'T', Actual);
1006 -- Use formal type for temp, unless formal type is an unconstrained
1007 -- array, in which case we don't have to worry about bounds checks,
1008 -- and we use the actual type, since that has appropriate bounds.
1010 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1011 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1012 else
1013 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1014 end if;
1016 if Nkind (Actual) = N_Type_Conversion then
1017 V_Typ := Etype (Expression (Actual));
1019 -- If the formal is an (in-)out parameter, capture the name
1020 -- of the variable in order to build the post-call assignment.
1022 Var := Make_Var (Expression (Actual));
1024 Crep := not Same_Representation
1025 (F_Typ, Etype (Expression (Actual)));
1027 else
1028 V_Typ := Etype (Actual);
1029 Var := Make_Var (Actual);
1030 Crep := False;
1031 end if;
1033 -- Setup initialization for case of in out parameter, or an out
1034 -- parameter where the formal is an unconstrained array (in the
1035 -- latter case, we have to pass in an object with bounds).
1037 -- If this is an out parameter, the initial copy is wasteful, so as
1038 -- an optimization for the one-dimensional case we extract the
1039 -- bounds of the actual and build an uninitialized temporary of the
1040 -- right size.
1042 if Ekind (Formal) = E_In_Out_Parameter
1043 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1044 then
1045 if Nkind (Actual) = N_Type_Conversion then
1046 if Conversion_OK (Actual) then
1047 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1048 else
1049 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1050 end if;
1052 elsif Ekind (Formal) = E_Out_Parameter
1053 and then Is_Array_Type (F_Typ)
1054 and then Number_Dimensions (F_Typ) = 1
1055 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1056 then
1057 -- Actual is a one-dimensional array or slice, and the type
1058 -- requires no initialization. Create a temporary of the
1059 -- right size, but do not copy actual into it (optimization).
1061 Init := Empty;
1062 Indic :=
1063 Make_Subtype_Indication (Loc,
1064 Subtype_Mark =>
1065 New_Occurrence_Of (F_Typ, Loc),
1066 Constraint =>
1067 Make_Index_Or_Discriminant_Constraint (Loc,
1068 Constraints => New_List (
1069 Make_Range (Loc,
1070 Low_Bound =>
1071 Make_Attribute_Reference (Loc,
1072 Prefix => New_Occurrence_Of (Var, Loc),
1073 Attribute_Name => Name_First),
1074 High_Bound =>
1075 Make_Attribute_Reference (Loc,
1076 Prefix => New_Occurrence_Of (Var, Loc),
1077 Attribute_Name => Name_Last)))));
1079 else
1080 Init := New_Occurrence_Of (Var, Loc);
1081 end if;
1083 -- An initialization is created for packed conversions as
1084 -- actuals for out parameters to enable Make_Object_Declaration
1085 -- to determine the proper subtype for N_Node. Note that this
1086 -- is wasteful because the extra copying on the call side is
1087 -- not required for such out parameters. ???
1089 elsif Ekind (Formal) = E_Out_Parameter
1090 and then Nkind (Actual) = N_Type_Conversion
1091 and then (Is_Bit_Packed_Array (F_Typ)
1092 or else
1093 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1094 then
1095 if Conversion_OK (Actual) then
1096 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1097 else
1098 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1099 end if;
1101 elsif Ekind (Formal) = E_In_Parameter then
1103 -- Handle the case in which the actual is a type conversion
1105 if Nkind (Actual) = N_Type_Conversion then
1106 if Conversion_OK (Actual) then
1107 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1108 else
1109 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1110 end if;
1111 else
1112 Init := New_Occurrence_Of (Var, Loc);
1113 end if;
1115 else
1116 Init := Empty;
1117 end if;
1119 N_Node :=
1120 Make_Object_Declaration (Loc,
1121 Defining_Identifier => Temp,
1122 Object_Definition => Indic,
1123 Expression => Init);
1124 Set_Assignment_OK (N_Node);
1125 Insert_Action (N, N_Node);
1127 -- Now, normally the deal here is that we use the defining
1128 -- identifier created by that object declaration. There is
1129 -- one exception to this. In the change of representation case
1130 -- the above declaration will end up looking like:
1132 -- temp : type := identifier;
1134 -- And in this case we might as well use the identifier directly
1135 -- and eliminate the temporary. Note that the analysis of the
1136 -- declaration was not a waste of time in that case, since it is
1137 -- what generated the necessary change of representation code. If
1138 -- the change of representation introduced additional code, as in
1139 -- a fixed-integer conversion, the expression is not an identifier
1140 -- and must be kept.
1142 if Crep
1143 and then Present (Expression (N_Node))
1144 and then Is_Entity_Name (Expression (N_Node))
1145 then
1146 Temp := Entity (Expression (N_Node));
1147 Rewrite (N_Node, Make_Null_Statement (Loc));
1148 end if;
1150 -- For IN parameter, all we do is to replace the actual
1152 if Ekind (Formal) = E_In_Parameter then
1153 Rewrite (Actual, New_Reference_To (Temp, Loc));
1154 Analyze (Actual);
1156 -- Processing for OUT or IN OUT parameter
1158 else
1159 -- Kill current value indications for the temporary variable we
1160 -- created, since we just passed it as an OUT parameter.
1162 Kill_Current_Values (Temp);
1163 Set_Is_Known_Valid (Temp, False);
1165 -- If type conversion, use reverse conversion on exit
1167 if Nkind (Actual) = N_Type_Conversion then
1168 if Conversion_OK (Actual) then
1169 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1170 else
1171 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1172 end if;
1173 else
1174 Expr := New_Occurrence_Of (Temp, Loc);
1175 end if;
1177 Rewrite (Actual, New_Reference_To (Temp, Loc));
1178 Analyze (Actual);
1180 -- If the actual is a conversion of a packed reference, it may
1181 -- already have been expanded by Remove_Side_Effects, and the
1182 -- resulting variable is a temporary which does not designate
1183 -- the proper out-parameter, which may not be addressable. In
1184 -- that case, generate an assignment to the original expression
1185 -- (before expansion of the packed reference) so that the proper
1186 -- expansion of assignment to a packed component can take place.
1188 declare
1189 Obj : Node_Id;
1190 Lhs : Node_Id;
1192 begin
1193 if Is_Renaming_Of_Object (Var)
1194 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1195 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1196 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1197 = N_Indexed_Component
1198 and then
1199 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1200 then
1201 Obj := Renamed_Object (Var);
1202 Lhs :=
1203 Make_Selected_Component (Loc,
1204 Prefix =>
1205 New_Copy_Tree (Original_Node (Prefix (Obj))),
1206 Selector_Name => New_Copy (Selector_Name (Obj)));
1207 Reset_Analyzed_Flags (Lhs);
1209 else
1210 Lhs := New_Occurrence_Of (Var, Loc);
1211 end if;
1213 Set_Assignment_OK (Lhs);
1215 if Is_Access_Type (E_Formal)
1216 and then Is_Entity_Name (Lhs)
1217 and then
1218 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1219 then
1220 -- Copyback target is an Ada 2012 stand-alone object of an
1221 -- anonymous access type.
1223 pragma Assert (Ada_Version >= Ada_2012);
1225 if Type_Access_Level (E_Formal) >
1226 Object_Access_Level (Lhs)
1227 then
1228 Append_To (Post_Call,
1229 Make_Raise_Program_Error (Loc,
1230 Reason => PE_Accessibility_Check_Failed));
1231 end if;
1233 Append_To (Post_Call,
1234 Make_Assignment_Statement (Loc,
1235 Name => Lhs,
1236 Expression => Expr));
1238 -- We would like to somehow suppress generation of the
1239 -- extra_accessibility assignment generated by the expansion
1240 -- of the above assignment statement. It's not a correctness
1241 -- issue because the following assignment renders it dead,
1242 -- but generating back-to-back assignments to the same
1243 -- target is undesirable. ???
1245 Append_To (Post_Call,
1246 Make_Assignment_Statement (Loc,
1247 Name => New_Occurrence_Of (
1248 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1249 Expression => Make_Integer_Literal (Loc,
1250 Type_Access_Level (E_Formal))));
1252 else
1253 Append_To (Post_Call,
1254 Make_Assignment_Statement (Loc,
1255 Name => Lhs,
1256 Expression => Expr));
1257 end if;
1258 end;
1259 end if;
1260 end Add_Call_By_Copy_Code;
1262 ----------------------------------
1263 -- Add_Simple_Call_By_Copy_Code --
1264 ----------------------------------
1266 procedure Add_Simple_Call_By_Copy_Code is
1267 Temp : Entity_Id;
1268 Decl : Node_Id;
1269 Incod : Node_Id;
1270 Outcod : Node_Id;
1271 Lhs : Node_Id;
1272 Rhs : Node_Id;
1273 Indic : Node_Id;
1274 F_Typ : constant Entity_Id := Etype (Formal);
1276 begin
1277 if not Is_Legal_Copy then
1278 return;
1279 end if;
1281 -- Use formal type for temp, unless formal type is an unconstrained
1282 -- array, in which case we don't have to worry about bounds checks,
1283 -- and we use the actual type, since that has appropriate bounds.
1285 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1286 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1287 else
1288 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1289 end if;
1291 -- Prepare to generate code
1293 Reset_Packed_Prefix;
1295 Temp := Make_Temporary (Loc, 'T', Actual);
1296 Incod := Relocate_Node (Actual);
1297 Outcod := New_Copy_Tree (Incod);
1299 -- Generate declaration of temporary variable, initializing it
1300 -- with the input parameter unless we have an OUT formal or
1301 -- this is an initialization call.
1303 -- If the formal is an out parameter with discriminants, the
1304 -- discriminants must be captured even if the rest of the object
1305 -- is in principle uninitialized, because the discriminants may
1306 -- be read by the called subprogram.
1308 if Ekind (Formal) = E_Out_Parameter then
1309 Incod := Empty;
1311 if Has_Discriminants (Etype (Formal)) then
1312 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1313 end if;
1315 elsif Inside_Init_Proc then
1317 -- Could use a comment here to match comment below ???
1319 if Nkind (Actual) /= N_Selected_Component
1320 or else
1321 not Has_Discriminant_Dependent_Constraint
1322 (Entity (Selector_Name (Actual)))
1323 then
1324 Incod := Empty;
1326 -- Otherwise, keep the component in order to generate the proper
1327 -- actual subtype, that depends on enclosing discriminants.
1329 else
1330 null;
1331 end if;
1332 end if;
1334 Decl :=
1335 Make_Object_Declaration (Loc,
1336 Defining_Identifier => Temp,
1337 Object_Definition => Indic,
1338 Expression => Incod);
1340 if Inside_Init_Proc
1341 and then No (Incod)
1342 then
1343 -- If the call is to initialize a component of a composite type,
1344 -- and the component does not depend on discriminants, use the
1345 -- actual type of the component. This is required in case the
1346 -- component is constrained, because in general the formal of the
1347 -- initialization procedure will be unconstrained. Note that if
1348 -- the component being initialized is constrained by an enclosing
1349 -- discriminant, the presence of the initialization in the
1350 -- declaration will generate an expression for the actual subtype.
1352 Set_No_Initialization (Decl);
1353 Set_Object_Definition (Decl,
1354 New_Occurrence_Of (Etype (Actual), Loc));
1355 end if;
1357 Insert_Action (N, Decl);
1359 -- The actual is simply a reference to the temporary
1361 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1363 -- Generate copy out if OUT or IN OUT parameter
1365 if Ekind (Formal) /= E_In_Parameter then
1366 Lhs := Outcod;
1367 Rhs := New_Occurrence_Of (Temp, Loc);
1369 -- Deal with conversion
1371 if Nkind (Lhs) = N_Type_Conversion then
1372 Lhs := Expression (Lhs);
1373 Rhs := Convert_To (Etype (Actual), Rhs);
1374 end if;
1376 Append_To (Post_Call,
1377 Make_Assignment_Statement (Loc,
1378 Name => Lhs,
1379 Expression => Rhs));
1380 Set_Assignment_OK (Name (Last (Post_Call)));
1381 end if;
1382 end Add_Simple_Call_By_Copy_Code;
1384 ---------------------------
1385 -- Check_Fortran_Logical --
1386 ---------------------------
1388 procedure Check_Fortran_Logical is
1389 Logical : constant Entity_Id := Etype (Formal);
1390 Var : Entity_Id;
1392 -- Note: this is very incomplete, e.g. it does not handle arrays
1393 -- of logical values. This is really not the right approach at all???)
1395 begin
1396 if Convention (Subp) = Convention_Fortran
1397 and then Root_Type (Etype (Formal)) = Standard_Boolean
1398 and then Ekind (Formal) /= E_In_Parameter
1399 then
1400 Var := Make_Var (Actual);
1401 Append_To (Post_Call,
1402 Make_Assignment_Statement (Loc,
1403 Name => New_Occurrence_Of (Var, Loc),
1404 Expression =>
1405 Unchecked_Convert_To (
1406 Logical,
1407 Make_Op_Ne (Loc,
1408 Left_Opnd => New_Occurrence_Of (Var, Loc),
1409 Right_Opnd =>
1410 Unchecked_Convert_To (
1411 Logical,
1412 New_Occurrence_Of (Standard_False, Loc))))));
1413 end if;
1414 end Check_Fortran_Logical;
1416 -------------------
1417 -- Is_Legal_Copy --
1418 -------------------
1420 function Is_Legal_Copy return Boolean is
1421 begin
1422 -- An attempt to copy a value of such a type can only occur if
1423 -- representation clauses give the actual a misaligned address.
1425 if Is_By_Reference_Type (Etype (Formal)) then
1427 -- If the front-end does not perform full type layout, the actual
1428 -- may in fact be properly aligned but there is not enough front-
1429 -- end information to determine this. In that case gigi will emit
1430 -- an error if a copy is not legal, or generate the proper code.
1431 -- For other backends we report the error now.
1433 -- Seems wrong to be issuing an error in the expander, since it
1434 -- will be missed in -gnatc mode ???
1436 if Frontend_Layout_On_Target then
1437 Error_Msg_N
1438 ("misaligned actual cannot be passed by reference", Actual);
1439 end if;
1441 return False;
1443 -- For users of Starlet, we assume that the specification of by-
1444 -- reference mechanism is mandatory. This may lead to unaligned
1445 -- objects but at least for DEC legacy code it is known to work.
1446 -- The warning will alert users of this code that a problem may
1447 -- be lurking.
1449 elsif Mechanism (Formal) = By_Reference
1450 and then Is_Valued_Procedure (Scope (Formal))
1451 then
1452 Error_Msg_N
1453 ("by_reference actual may be misaligned??", Actual);
1454 return False;
1456 else
1457 return True;
1458 end if;
1459 end Is_Legal_Copy;
1461 --------------
1462 -- Make_Var --
1463 --------------
1465 function Make_Var (Actual : Node_Id) return Entity_Id is
1466 Var : Entity_Id;
1468 begin
1469 if Is_Entity_Name (Actual) then
1470 return Entity (Actual);
1472 else
1473 Var := Make_Temporary (Loc, 'T', Actual);
1475 N_Node :=
1476 Make_Object_Renaming_Declaration (Loc,
1477 Defining_Identifier => Var,
1478 Subtype_Mark =>
1479 New_Occurrence_Of (Etype (Actual), Loc),
1480 Name => Relocate_Node (Actual));
1482 Insert_Action (N, N_Node);
1483 return Var;
1484 end if;
1485 end Make_Var;
1487 -------------------------
1488 -- Reset_Packed_Prefix --
1489 -------------------------
1491 procedure Reset_Packed_Prefix is
1492 Pfx : Node_Id := Actual;
1493 begin
1494 loop
1495 Set_Analyzed (Pfx, False);
1496 exit when
1497 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1498 Pfx := Prefix (Pfx);
1499 end loop;
1500 end Reset_Packed_Prefix;
1502 -- Start of processing for Expand_Actuals
1504 begin
1505 Post_Call := New_List;
1507 Formal := First_Formal (Subp);
1508 Actual := First_Actual (N);
1509 while Present (Formal) loop
1510 E_Formal := Etype (Formal);
1512 if Is_Scalar_Type (E_Formal)
1513 or else Nkind (Actual) = N_Slice
1514 then
1515 Check_Fortran_Logical;
1517 -- RM 6.4.1 (11)
1519 elsif Ekind (Formal) /= E_Out_Parameter then
1521 -- The unusual case of the current instance of a protected type
1522 -- requires special handling. This can only occur in the context
1523 -- of a call within the body of a protected operation.
1525 if Is_Entity_Name (Actual)
1526 and then Ekind (Entity (Actual)) = E_Protected_Type
1527 and then In_Open_Scopes (Entity (Actual))
1528 then
1529 if Scope (Subp) /= Entity (Actual) then
1530 Error_Msg_N
1531 ("operation outside protected type may not "
1532 & "call back its protected operations??", Actual);
1533 end if;
1535 Rewrite (Actual,
1536 Expand_Protected_Object_Reference (N, Entity (Actual)));
1537 end if;
1539 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1540 -- build-in-place function, then a temporary return object needs
1541 -- to be created and access to it must be passed to the function.
1542 -- Currently we limit such functions to those with inherently
1543 -- limited result subtypes, but eventually we plan to expand the
1544 -- functions that are treated as build-in-place to include other
1545 -- composite result types.
1547 if Is_Build_In_Place_Function_Call (Actual) then
1548 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1549 end if;
1551 Apply_Constraint_Check (Actual, E_Formal);
1553 -- Out parameter case. No constraint checks on access type
1554 -- RM 6.4.1 (13)
1556 elsif Is_Access_Type (E_Formal) then
1557 null;
1559 -- RM 6.4.1 (14)
1561 elsif Has_Discriminants (Base_Type (E_Formal))
1562 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1563 then
1564 Apply_Constraint_Check (Actual, E_Formal);
1566 -- RM 6.4.1 (15)
1568 else
1569 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1570 end if;
1572 -- Processing for IN-OUT and OUT parameters
1574 if Ekind (Formal) /= E_In_Parameter then
1576 -- For type conversions of arrays, apply length/range checks
1578 if Is_Array_Type (E_Formal)
1579 and then Nkind (Actual) = N_Type_Conversion
1580 then
1581 if Is_Constrained (E_Formal) then
1582 Apply_Length_Check (Expression (Actual), E_Formal);
1583 else
1584 Apply_Range_Check (Expression (Actual), E_Formal);
1585 end if;
1586 end if;
1588 -- If argument is a type conversion for a type that is passed
1589 -- by copy, then we must pass the parameter by copy.
1591 if Nkind (Actual) = N_Type_Conversion
1592 and then
1593 (Is_Numeric_Type (E_Formal)
1594 or else Is_Access_Type (E_Formal)
1595 or else Is_Enumeration_Type (E_Formal)
1596 or else Is_Bit_Packed_Array (Etype (Formal))
1597 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1599 -- Also pass by copy if change of representation
1601 or else not Same_Representation
1602 (Etype (Formal),
1603 Etype (Expression (Actual))))
1604 then
1605 Add_Call_By_Copy_Code;
1607 -- References to components of bit packed arrays are expanded
1608 -- at this point, rather than at the point of analysis of the
1609 -- actuals, to handle the expansion of the assignment to
1610 -- [in] out parameters.
1612 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1613 Add_Simple_Call_By_Copy_Code;
1615 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1616 -- because the back-end cannot cope with such objects. In other
1617 -- cases where alignment forces a copy, the back-end generates
1618 -- it properly. It should not be generated unconditionally in the
1619 -- front-end because it does not know precisely the alignment
1620 -- requirements of the target, and makes too conservative an
1621 -- estimate, leading to superfluous copies or spurious errors
1622 -- on by-reference parameters.
1624 elsif Nkind (Actual) = N_Selected_Component
1625 and then
1626 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1627 and then not Represented_As_Scalar (Etype (Formal))
1628 then
1629 Add_Simple_Call_By_Copy_Code;
1631 -- References to slices of bit packed arrays are expanded
1633 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1634 Add_Call_By_Copy_Code;
1636 -- References to possibly unaligned slices of arrays are expanded
1638 elsif Is_Possibly_Unaligned_Slice (Actual) then
1639 Add_Call_By_Copy_Code;
1641 -- Deal with access types where the actual subtype and the
1642 -- formal subtype are not the same, requiring a check.
1644 -- It is necessary to exclude tagged types because of "downward
1645 -- conversion" errors.
1647 elsif Is_Access_Type (E_Formal)
1648 and then not Same_Type (E_Formal, Etype (Actual))
1649 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1650 then
1651 Add_Call_By_Copy_Code;
1653 -- If the actual is not a scalar and is marked for volatile
1654 -- treatment, whereas the formal is not volatile, then pass
1655 -- by copy unless it is a by-reference type.
1657 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1658 -- because this is the enforcement of a language rule that applies
1659 -- only to "real" volatile variables, not e.g. to the address
1660 -- clause overlay case.
1662 elsif Is_Entity_Name (Actual)
1663 and then Is_Volatile (Entity (Actual))
1664 and then not Is_By_Reference_Type (Etype (Actual))
1665 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1666 and then not Is_Volatile (E_Formal)
1667 then
1668 Add_Call_By_Copy_Code;
1670 elsif Nkind (Actual) = N_Indexed_Component
1671 and then Is_Entity_Name (Prefix (Actual))
1672 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1673 then
1674 Add_Call_By_Copy_Code;
1676 -- Add call-by-copy code for the case of scalar out parameters
1677 -- when it is not known at compile time that the subtype of the
1678 -- formal is a subrange of the subtype of the actual (or vice
1679 -- versa for in out parameters), in order to get range checks
1680 -- on such actuals. (Maybe this case should be handled earlier
1681 -- in the if statement???)
1683 elsif Is_Scalar_Type (E_Formal)
1684 and then
1685 (not In_Subrange_Of (E_Formal, Etype (Actual))
1686 or else
1687 (Ekind (Formal) = E_In_Out_Parameter
1688 and then not In_Subrange_Of (Etype (Actual), E_Formal)))
1689 then
1690 -- Perhaps the setting back to False should be done within
1691 -- Add_Call_By_Copy_Code, since it could get set on other
1692 -- cases occurring above???
1694 if Do_Range_Check (Actual) then
1695 Set_Do_Range_Check (Actual, False);
1696 end if;
1698 Add_Call_By_Copy_Code;
1699 end if;
1701 -- Processing for IN parameters
1703 else
1704 -- For IN parameters is in the packed array case, we expand an
1705 -- indexed component (the circuit in Exp_Ch4 deliberately left
1706 -- indexed components appearing as actuals untouched, so that
1707 -- the special processing above for the OUT and IN OUT cases
1708 -- could be performed. We could make the test in Exp_Ch4 more
1709 -- complex and have it detect the parameter mode, but it is
1710 -- easier simply to handle all cases here.)
1712 if Nkind (Actual) = N_Indexed_Component
1713 and then Is_Packed (Etype (Prefix (Actual)))
1714 then
1715 Reset_Packed_Prefix;
1716 Expand_Packed_Element_Reference (Actual);
1718 -- If we have a reference to a bit packed array, we copy it, since
1719 -- the actual must be byte aligned.
1721 -- Is this really necessary in all cases???
1723 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1724 Add_Simple_Call_By_Copy_Code;
1726 -- If a non-scalar actual is possibly unaligned, we need a copy
1728 elsif Is_Possibly_Unaligned_Object (Actual)
1729 and then not Represented_As_Scalar (Etype (Formal))
1730 then
1731 Add_Simple_Call_By_Copy_Code;
1733 -- Similarly, we have to expand slices of packed arrays here
1734 -- because the result must be byte aligned.
1736 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1737 Add_Call_By_Copy_Code;
1739 -- Only processing remaining is to pass by copy if this is a
1740 -- reference to a possibly unaligned slice, since the caller
1741 -- expects an appropriately aligned argument.
1743 elsif Is_Possibly_Unaligned_Slice (Actual) then
1744 Add_Call_By_Copy_Code;
1746 -- An unusual case: a current instance of an enclosing task can be
1747 -- an actual, and must be replaced by a reference to self.
1749 elsif Is_Entity_Name (Actual)
1750 and then Is_Task_Type (Entity (Actual))
1751 then
1752 if In_Open_Scopes (Entity (Actual)) then
1753 Rewrite (Actual,
1754 (Make_Function_Call (Loc,
1755 Name => New_Reference_To (RTE (RE_Self), Loc))));
1756 Analyze (Actual);
1758 -- A task type cannot otherwise appear as an actual
1760 else
1761 raise Program_Error;
1762 end if;
1763 end if;
1764 end if;
1766 Next_Formal (Formal);
1767 Next_Actual (Actual);
1768 end loop;
1770 -- Find right place to put post call stuff if it is present
1772 if not Is_Empty_List (Post_Call) then
1774 -- Cases where the call is not a member of a statement list
1776 if not Is_List_Member (N) then
1777 declare
1778 P : Node_Id := Parent (N);
1780 begin
1781 -- In Ada 2012 the call may be a function call in an expression
1782 -- (since OUT and IN OUT parameters are now allowed for such
1783 -- calls. The write-back of (in)-out parameters is handled
1784 -- by the back-end, but the constraint checks generated when
1785 -- subtypes of formal and actual don't match must be inserted
1786 -- in the form of assignments, at the nearest point after the
1787 -- declaration or statement that contains the call.
1789 if Ada_Version >= Ada_2012
1790 and then Nkind (N) = N_Function_Call
1791 then
1792 while Nkind (P) not in N_Declaration
1793 and then
1794 Nkind (P) not in N_Statement_Other_Than_Procedure_Call
1795 loop
1796 P := Parent (P);
1797 end loop;
1799 Insert_Actions_After (P, Post_Call);
1801 -- If not the special Ada 2012 case of a function call, then
1802 -- we must have the triggering statement of a triggering
1803 -- alternative or an entry call alternative, and we can add
1804 -- the post call stuff to the corresponding statement list.
1806 else
1807 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1808 N_Entry_Call_Alternative));
1810 if Is_Non_Empty_List (Statements (P)) then
1811 Insert_List_Before_And_Analyze
1812 (First (Statements (P)), Post_Call);
1813 else
1814 Set_Statements (P, Post_Call);
1815 end if;
1816 end if;
1818 end;
1820 -- Otherwise, normal case where N is in a statement sequence,
1821 -- just put the post-call stuff after the call statement.
1823 else
1824 Insert_Actions_After (N, Post_Call);
1825 end if;
1826 end if;
1828 -- The call node itself is re-analyzed in Expand_Call
1830 end Expand_Actuals;
1832 -----------------
1833 -- Expand_Call --
1834 -----------------
1836 -- This procedure handles expansion of function calls and procedure call
1837 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1838 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1840 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1841 -- Provide values of actuals for all formals in Extra_Formals list
1842 -- Replace "call" to enumeration literal function by literal itself
1843 -- Rewrite call to predefined operator as operator
1844 -- Replace actuals to in-out parameters that are numeric conversions,
1845 -- with explicit assignment to temporaries before and after the call.
1846 -- Remove optional actuals if First_Optional_Parameter specified.
1848 -- Note that the list of actuals has been filled with default expressions
1849 -- during semantic analysis of the call. Only the extra actuals required
1850 -- for the 'Constrained attribute and for accessibility checks are added
1851 -- at this point.
1853 procedure Expand_Call (N : Node_Id) is
1854 Loc : constant Source_Ptr := Sloc (N);
1855 Call_Node : Node_Id := N;
1856 Extra_Actuals : List_Id := No_List;
1857 Prev : Node_Id := Empty;
1859 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1860 -- Adds one entry to the end of the actual parameter list. Used for
1861 -- default parameters and for extra actuals (for Extra_Formals). The
1862 -- argument is an N_Parameter_Association node.
1864 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1865 -- Adds an extra actual to the list of extra actuals. Expr is the
1866 -- expression for the value of the actual, EF is the entity for the
1867 -- extra formal.
1869 procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id);
1870 -- Check and inline the body of Subp. Invoked when compiling with
1871 -- optimizations enabled and Subp has pragma inline or inline always.
1872 -- If the subprogram is a renaming, or if it is inherited, then Subp
1873 -- references the renamed entity and Orig_Subp is the entity of the
1874 -- call node N.
1876 procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id);
1877 -- Check and inline the body of Subp. Invoked when compiling without
1878 -- optimizations and Subp has pragma inline always. If the subprogram is
1879 -- a renaming, or if it is inherited, then Subp references the renamed
1880 -- entity and Orig_Subp is the entity of the call node N.
1882 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1883 -- Within an instance, a type derived from a non-tagged formal derived
1884 -- type inherits from the original parent, not from the actual. The
1885 -- current derivation mechanism has the derived type inherit from the
1886 -- actual, which is only correct outside of the instance. If the
1887 -- subprogram is inherited, we test for this particular case through a
1888 -- convoluted tree traversal before setting the proper subprogram to be
1889 -- called.
1891 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
1892 -- Return true if E comes from an instance that is not yet frozen
1894 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
1895 -- Determine if Subp denotes a non-dispatching call to a Deep routine
1897 function New_Value (From : Node_Id) return Node_Id;
1898 -- From is the original Expression. New_Value is equivalent to a call
1899 -- to Duplicate_Subexpr with an explicit dereference when From is an
1900 -- access parameter.
1902 --------------------------
1903 -- Add_Actual_Parameter --
1904 --------------------------
1906 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1907 Actual_Expr : constant Node_Id :=
1908 Explicit_Actual_Parameter (Insert_Param);
1910 begin
1911 -- Case of insertion is first named actual
1913 if No (Prev) or else
1914 Nkind (Parent (Prev)) /= N_Parameter_Association
1915 then
1916 Set_Next_Named_Actual
1917 (Insert_Param, First_Named_Actual (Call_Node));
1918 Set_First_Named_Actual (Call_Node, Actual_Expr);
1920 if No (Prev) then
1921 if No (Parameter_Associations (Call_Node)) then
1922 Set_Parameter_Associations (Call_Node, New_List);
1923 end if;
1925 Append (Insert_Param, Parameter_Associations (Call_Node));
1927 else
1928 Insert_After (Prev, Insert_Param);
1929 end if;
1931 -- Case of insertion is not first named actual
1933 else
1934 Set_Next_Named_Actual
1935 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1936 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1937 Append (Insert_Param, Parameter_Associations (Call_Node));
1938 end if;
1940 Prev := Actual_Expr;
1941 end Add_Actual_Parameter;
1943 ----------------------
1944 -- Add_Extra_Actual --
1945 ----------------------
1947 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1948 Loc : constant Source_Ptr := Sloc (Expr);
1950 begin
1951 if Extra_Actuals = No_List then
1952 Extra_Actuals := New_List;
1953 Set_Parent (Extra_Actuals, Call_Node);
1954 end if;
1956 Append_To (Extra_Actuals,
1957 Make_Parameter_Association (Loc,
1958 Selector_Name => Make_Identifier (Loc, Chars (EF)),
1959 Explicit_Actual_Parameter => Expr));
1961 Analyze_And_Resolve (Expr, Etype (EF));
1963 if Nkind (Call_Node) = N_Function_Call then
1964 Set_Is_Accessibility_Actual (Parent (Expr));
1965 end if;
1966 end Add_Extra_Actual;
1968 ----------------
1969 -- Do_Inline --
1970 ----------------
1972 procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id) is
1973 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
1975 procedure Do_Backend_Inline;
1976 -- Check that the call can be safely passed to the backend. If true
1977 -- then register the enclosing unit of Subp to Inlined_Bodies so that
1978 -- the body of Subp can be retrieved and analyzed by the backend.
1980 procedure Register_Backend_Call (N : Node_Id);
1981 -- Append N to the list Backend_Calls
1983 -----------------------
1984 -- Do_Backend_Inline --
1985 -----------------------
1987 procedure Do_Backend_Inline is
1988 begin
1989 -- No extra test needed for init subprograms since we know they
1990 -- are available to the backend!
1992 if Is_Init_Proc (Subp) then
1993 Add_Inlined_Body (Subp);
1994 Register_Backend_Call (Call_Node);
1996 -- Verify that if the body to inline is located in the current
1997 -- unit the inlining does not occur earlier. This avoids
1998 -- order-of-elaboration problems in the back end.
2000 elsif In_Same_Extended_Unit (Call_Node, Subp)
2001 and then Nkind (Spec) = N_Subprogram_Declaration
2002 and then Earlier_In_Extended_Unit
2003 (Loc, Sloc (Body_To_Inline (Spec)))
2004 then
2005 Error_Msg_NE
2006 ("cannot inline& (body not seen yet)??", Call_Node, Subp);
2008 else
2009 declare
2010 Backend_Inline : Boolean := True;
2012 begin
2013 -- If we are compiling a package body that is not the
2014 -- main unit, it must be for inlining/instantiation
2015 -- purposes, in which case we inline the call to insure
2016 -- that the same temporaries are generated when compiling
2017 -- the body by itself. Otherwise link errors can occur.
2019 -- If the function being called is itself in the main
2020 -- unit, we cannot inline, because there is a risk of
2021 -- double elaboration and/or circularity: the inlining
2022 -- can make visible a private entity in the body of the
2023 -- main unit, that gigi will see before its sees its
2024 -- proper definition.
2026 if not (In_Extended_Main_Code_Unit (Call_Node))
2027 and then In_Package_Body
2028 then
2029 Backend_Inline :=
2030 not In_Extended_Main_Source_Unit (Subp);
2031 end if;
2033 if Backend_Inline then
2034 Add_Inlined_Body (Subp);
2035 Register_Backend_Call (Call_Node);
2036 end if;
2037 end;
2038 end if;
2039 end Do_Backend_Inline;
2041 ---------------------------
2042 -- Register_Backend_Call --
2043 ---------------------------
2045 procedure Register_Backend_Call (N : Node_Id) is
2046 begin
2047 if Backend_Calls = No_Elist then
2048 Backend_Calls := New_Elmt_List;
2049 end if;
2051 Append_Elmt (N, To => Backend_Calls);
2052 end Register_Backend_Call;
2054 -- Start of processing for Do_Inline
2056 begin
2057 -- Verify that the body to inline has already been seen
2059 if No (Spec)
2060 or else Nkind (Spec) /= N_Subprogram_Declaration
2061 or else No (Body_To_Inline (Spec))
2062 then
2063 if Comes_From_Source (Subp)
2064 and then Must_Inline (Subp)
2065 then
2066 Cannot_Inline
2067 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
2069 -- Let the back end handle it
2071 else
2072 Do_Backend_Inline;
2073 return;
2074 end if;
2076 -- If this an inherited function that returns a private type, do not
2077 -- inline if the full view is an unconstrained array, because such
2078 -- calls cannot be inlined.
2080 elsif Present (Orig_Subp)
2081 and then Is_Array_Type (Etype (Orig_Subp))
2082 and then not Is_Constrained (Etype (Orig_Subp))
2083 then
2084 Cannot_Inline
2085 ("cannot inline& (unconstrained array)?", Call_Node, Subp);
2087 else
2088 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
2089 end if;
2090 end Do_Inline;
2092 ----------------------
2093 -- Do_Inline_Always --
2094 ----------------------
2096 procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id) is
2097 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2098 Body_Id : Entity_Id;
2100 begin
2101 if No (Spec)
2102 or else Nkind (Spec) /= N_Subprogram_Declaration
2103 or else No (Body_To_Inline (Spec))
2104 or else Serious_Errors_Detected /= 0
2105 then
2106 return;
2107 end if;
2109 Body_Id := Corresponding_Body (Spec);
2111 -- Verify that the body to inline has already been seen
2113 if No (Body_Id)
2114 or else not Analyzed (Body_Id)
2115 then
2116 Set_Is_Inlined (Subp, False);
2118 if Comes_From_Source (Subp) then
2120 -- Report a warning only if the call is located in the unit of
2121 -- the called subprogram; otherwise it is an error.
2123 if not In_Same_Extended_Unit (Call_Node, Subp) then
2124 Cannot_Inline
2125 ("cannot inline& (body not seen yet)?", Call_Node, Subp,
2126 Is_Serious => True);
2128 elsif In_Open_Scopes (Subp) then
2130 -- For backward compatibility we generate the same error
2131 -- or warning of the previous implementation. This will
2132 -- be changed when we definitely incorporate the new
2133 -- support ???
2135 if Front_End_Inlining
2136 and then Optimization_Level = 0
2137 then
2138 Error_Msg_N
2139 ("call to recursive subprogram cannot be inlined?p?",
2142 -- Do not emit error compiling runtime packages
2144 elsif Is_Predefined_File_Name
2145 (Unit_File_Name (Get_Source_Unit (Subp)))
2146 then
2147 Error_Msg_N
2148 ("call to recursive subprogram cannot be inlined??",
2151 else
2152 Error_Msg_N
2153 ("call to recursive subprogram cannot be inlined",
2155 end if;
2157 else
2158 Cannot_Inline
2159 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
2160 end if;
2161 end if;
2163 return;
2165 -- If this an inherited function that returns a private type, do not
2166 -- inline if the full view is an unconstrained array, because such
2167 -- calls cannot be inlined.
2169 elsif Present (Orig_Subp)
2170 and then Is_Array_Type (Etype (Orig_Subp))
2171 and then not Is_Constrained (Etype (Orig_Subp))
2172 then
2173 Cannot_Inline
2174 ("cannot inline& (unconstrained array)?", Call_Node, Subp);
2176 -- If the called subprogram comes from an instance in the same
2177 -- unit, and the instance is not yet frozen, inlining might
2178 -- trigger order-of-elaboration problems.
2180 elsif In_Unfrozen_Instance (Scope (Subp)) then
2181 Cannot_Inline
2182 ("cannot inline& (unfrozen instance)?", Call_Node, Subp);
2184 else
2185 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
2186 end if;
2187 end Do_Inline_Always;
2189 ---------------------------
2190 -- Inherited_From_Formal --
2191 ---------------------------
2193 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2194 Par : Entity_Id;
2195 Gen_Par : Entity_Id;
2196 Gen_Prim : Elist_Id;
2197 Elmt : Elmt_Id;
2198 Indic : Node_Id;
2200 begin
2201 -- If the operation is inherited, it is attached to the corresponding
2202 -- type derivation. If the parent in the derivation is a generic
2203 -- actual, it is a subtype of the actual, and we have to recover the
2204 -- original derived type declaration to find the proper parent.
2206 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2207 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2208 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2209 N_Derived_Type_Definition
2210 or else not In_Instance
2211 then
2212 return Empty;
2214 else
2215 Indic :=
2216 Subtype_Indication
2217 (Type_Definition (Original_Node (Parent (S))));
2219 if Nkind (Indic) = N_Subtype_Indication then
2220 Par := Entity (Subtype_Mark (Indic));
2221 else
2222 Par := Entity (Indic);
2223 end if;
2224 end if;
2226 if not Is_Generic_Actual_Type (Par)
2227 or else Is_Tagged_Type (Par)
2228 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2229 or else not In_Open_Scopes (Scope (Par))
2230 then
2231 return Empty;
2232 else
2233 Gen_Par := Generic_Parent_Type (Parent (Par));
2234 end if;
2236 -- If the actual has no generic parent type, the formal is not
2237 -- a formal derived type, so nothing to inherit.
2239 if No (Gen_Par) then
2240 return Empty;
2241 end if;
2243 -- If the generic parent type is still the generic type, this is a
2244 -- private formal, not a derived formal, and there are no operations
2245 -- inherited from the formal.
2247 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2248 return Empty;
2249 end if;
2251 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2253 Elmt := First_Elmt (Gen_Prim);
2254 while Present (Elmt) loop
2255 if Chars (Node (Elmt)) = Chars (S) then
2256 declare
2257 F1 : Entity_Id;
2258 F2 : Entity_Id;
2260 begin
2261 F1 := First_Formal (S);
2262 F2 := First_Formal (Node (Elmt));
2263 while Present (F1)
2264 and then Present (F2)
2265 loop
2266 if Etype (F1) = Etype (F2)
2267 or else Etype (F2) = Gen_Par
2268 then
2269 Next_Formal (F1);
2270 Next_Formal (F2);
2271 else
2272 Next_Elmt (Elmt);
2273 exit; -- not the right subprogram
2274 end if;
2276 return Node (Elmt);
2277 end loop;
2278 end;
2280 else
2281 Next_Elmt (Elmt);
2282 end if;
2283 end loop;
2285 raise Program_Error;
2286 end Inherited_From_Formal;
2288 --------------------------
2289 -- In_Unfrozen_Instance --
2290 --------------------------
2292 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2293 S : Entity_Id;
2295 begin
2296 S := E;
2297 while Present (S) and then S /= Standard_Standard loop
2298 if Is_Generic_Instance (S)
2299 and then Present (Freeze_Node (S))
2300 and then not Analyzed (Freeze_Node (S))
2301 then
2302 return True;
2303 end if;
2305 S := Scope (S);
2306 end loop;
2308 return False;
2309 end In_Unfrozen_Instance;
2311 -------------------------
2312 -- Is_Direct_Deep_Call --
2313 -------------------------
2315 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2316 begin
2317 if Is_TSS (Subp, TSS_Deep_Adjust)
2318 or else Is_TSS (Subp, TSS_Deep_Finalize)
2319 or else Is_TSS (Subp, TSS_Deep_Initialize)
2320 then
2321 declare
2322 Actual : Node_Id;
2323 Formal : Node_Id;
2325 begin
2326 Actual := First (Parameter_Associations (N));
2327 Formal := First_Formal (Subp);
2328 while Present (Actual)
2329 and then Present (Formal)
2330 loop
2331 if Nkind (Actual) = N_Identifier
2332 and then Is_Controlling_Actual (Actual)
2333 and then Etype (Actual) = Etype (Formal)
2334 then
2335 return True;
2336 end if;
2338 Next (Actual);
2339 Next_Formal (Formal);
2340 end loop;
2341 end;
2342 end if;
2344 return False;
2345 end Is_Direct_Deep_Call;
2347 ---------------
2348 -- New_Value --
2349 ---------------
2351 function New_Value (From : Node_Id) return Node_Id is
2352 Res : constant Node_Id := Duplicate_Subexpr (From);
2353 begin
2354 if Is_Access_Type (Etype (From)) then
2355 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2356 else
2357 return Res;
2358 end if;
2359 end New_Value;
2361 -- Local variables
2363 Curr_S : constant Entity_Id := Current_Scope;
2364 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2365 Actual : Node_Id;
2366 Formal : Entity_Id;
2367 Orig_Subp : Entity_Id := Empty;
2368 Param_Count : Natural := 0;
2369 Parent_Formal : Entity_Id;
2370 Parent_Subp : Entity_Id;
2371 Scop : Entity_Id;
2372 Subp : Entity_Id;
2374 Prev_Orig : Node_Id;
2375 -- Original node for an actual, which may have been rewritten. If the
2376 -- actual is a function call that has been transformed from a selected
2377 -- component, the original node is unanalyzed. Otherwise, it carries
2378 -- semantic information used to generate additional actuals.
2380 CW_Interface_Formals_Present : Boolean := False;
2382 -- Start of processing for Expand_Call
2384 begin
2385 -- Expand the procedure call if the first actual has a dimension and if
2386 -- the procedure is Put (Ada 2012).
2388 if Ada_Version >= Ada_2012
2389 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2390 and then Present (Parameter_Associations (Call_Node))
2391 then
2392 Expand_Put_Call_With_Symbol (Call_Node);
2393 end if;
2395 -- Ignore if previous error
2397 if Nkind (Call_Node) in N_Has_Etype
2398 and then Etype (Call_Node) = Any_Type
2399 then
2400 return;
2401 end if;
2403 -- Call using access to subprogram with explicit dereference
2405 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2406 Subp := Etype (Name (Call_Node));
2407 Parent_Subp := Empty;
2409 -- Case of call to simple entry, where the Name is a selected component
2410 -- whose prefix is the task, and whose selector name is the entry name
2412 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2413 Subp := Entity (Selector_Name (Name (Call_Node)));
2414 Parent_Subp := Empty;
2416 -- Case of call to member of entry family, where Name is an indexed
2417 -- component, with the prefix being a selected component giving the
2418 -- task and entry family name, and the index being the entry index.
2420 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2421 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2422 Parent_Subp := Empty;
2424 -- Normal case
2426 else
2427 Subp := Entity (Name (Call_Node));
2428 Parent_Subp := Alias (Subp);
2430 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2431 -- if we can tell that the first parameter cannot possibly be null.
2432 -- This improves efficiency by avoiding a run-time test.
2434 -- We do not do this if Raise_Exception_Always does not exist, which
2435 -- can happen in configurable run time profiles which provide only a
2436 -- Raise_Exception.
2438 if Is_RTE (Subp, RE_Raise_Exception)
2439 and then RTE_Available (RE_Raise_Exception_Always)
2440 then
2441 declare
2442 FA : constant Node_Id :=
2443 Original_Node (First_Actual (Call_Node));
2445 begin
2446 -- The case we catch is where the first argument is obtained
2447 -- using the Identity attribute (which must always be
2448 -- non-null).
2450 if Nkind (FA) = N_Attribute_Reference
2451 and then Attribute_Name (FA) = Name_Identity
2452 then
2453 Subp := RTE (RE_Raise_Exception_Always);
2454 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2455 end if;
2456 end;
2457 end if;
2459 if Ekind (Subp) = E_Entry then
2460 Parent_Subp := Empty;
2461 end if;
2462 end if;
2464 -- Detect the following code in System.Finalization_Masters only on
2465 -- .NET/JVM targets:
2467 -- procedure Finalize (Master : in out Finalization_Master) is
2468 -- begin
2469 -- . . .
2470 -- begin
2471 -- Finalize (Curr_Ptr.all);
2473 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2474 -- cannot be named in library or user code, the compiler has to install
2475 -- a kludge and transform the call to Finalize into Deep_Finalize.
2477 if VM_Target /= No_VM
2478 and then Chars (Subp) = Name_Finalize
2479 and then Ekind (Curr_S) = E_Block
2480 and then Ekind (Scope (Curr_S)) = E_Procedure
2481 and then Chars (Scope (Curr_S)) = Name_Finalize
2482 and then Etype (First_Formal (Scope (Curr_S))) =
2483 RTE (RE_Finalization_Master)
2484 then
2485 declare
2486 Deep_Fin : constant Entity_Id :=
2487 Find_Prim_Op (RTE (RE_Root_Controlled),
2488 TSS_Deep_Finalize);
2489 begin
2490 -- Since Root_Controlled is a tagged type, the compiler should
2491 -- always generate Deep_Finalize for it.
2493 pragma Assert (Present (Deep_Fin));
2495 -- Generate:
2496 -- Deep_Finalize (Curr_Ptr.all);
2498 Rewrite (N,
2499 Make_Procedure_Call_Statement (Loc,
2500 Name =>
2501 New_Reference_To (Deep_Fin, Loc),
2502 Parameter_Associations =>
2503 New_Copy_List_Tree (Parameter_Associations (N))));
2505 Analyze (N);
2506 return;
2507 end;
2508 end if;
2510 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2511 -- alternative in an asynchronous select or as an entry call in
2512 -- a conditional or timed select. Check whether the procedure call
2513 -- is a renaming of an entry and rewrite it as an entry call.
2515 if Ada_Version >= Ada_2005
2516 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2517 and then
2518 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2519 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2520 or else
2521 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2522 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2523 then
2524 declare
2525 Ren_Decl : Node_Id;
2526 Ren_Root : Entity_Id := Subp;
2528 begin
2529 -- This may be a chain of renamings, find the root
2531 if Present (Alias (Ren_Root)) then
2532 Ren_Root := Alias (Ren_Root);
2533 end if;
2535 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2536 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2538 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2539 Rewrite (Call_Node,
2540 Make_Entry_Call_Statement (Loc,
2541 Name =>
2542 New_Copy_Tree (Name (Ren_Decl)),
2543 Parameter_Associations =>
2544 New_Copy_List_Tree
2545 (Parameter_Associations (Call_Node))));
2547 return;
2548 end if;
2549 end if;
2550 end;
2551 end if;
2553 -- First step, compute extra actuals, corresponding to any Extra_Formals
2554 -- present. Note that we do not access Extra_Formals directly, instead
2555 -- we simply note the presence of the extra formals as we process the
2556 -- regular formals collecting corresponding actuals in Extra_Actuals.
2558 -- We also generate any required range checks for actuals for in formals
2559 -- as we go through the loop, since this is a convenient place to do it.
2560 -- (Though it seems that this would be better done in Expand_Actuals???)
2562 Formal := First_Formal (Subp);
2563 Actual := First_Actual (Call_Node);
2564 Param_Count := 1;
2565 while Present (Formal) loop
2567 -- Generate range check if required
2569 if Do_Range_Check (Actual)
2570 and then Ekind (Formal) = E_In_Parameter
2571 then
2572 Set_Do_Range_Check (Actual, False);
2573 Generate_Range_Check
2574 (Actual, Etype (Formal), CE_Range_Check_Failed);
2575 end if;
2577 -- Prepare to examine current entry
2579 Prev := Actual;
2580 Prev_Orig := Original_Node (Prev);
2582 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2583 -- to expand it in a further round.
2585 CW_Interface_Formals_Present :=
2586 CW_Interface_Formals_Present
2587 or else
2588 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2589 and then Is_Interface (Etype (Etype (Formal))))
2590 or else
2591 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2592 and then Is_Interface (Directly_Designated_Type
2593 (Etype (Etype (Formal)))));
2595 -- Create possible extra actual for constrained case. Usually, the
2596 -- extra actual is of the form actual'constrained, but since this
2597 -- attribute is only available for unconstrained records, TRUE is
2598 -- expanded if the type of the formal happens to be constrained (for
2599 -- instance when this procedure is inherited from an unconstrained
2600 -- record to a constrained one) or if the actual has no discriminant
2601 -- (its type is constrained). An exception to this is the case of a
2602 -- private type without discriminants. In this case we pass FALSE
2603 -- because the object has underlying discriminants with defaults.
2605 if Present (Extra_Constrained (Formal)) then
2606 if Ekind (Etype (Prev)) in Private_Kind
2607 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2608 then
2609 Add_Extra_Actual
2610 (New_Occurrence_Of (Standard_False, Loc),
2611 Extra_Constrained (Formal));
2613 elsif Is_Constrained (Etype (Formal))
2614 or else not Has_Discriminants (Etype (Prev))
2615 then
2616 Add_Extra_Actual
2617 (New_Occurrence_Of (Standard_True, Loc),
2618 Extra_Constrained (Formal));
2620 -- Do not produce extra actuals for Unchecked_Union parameters.
2621 -- Jump directly to the end of the loop.
2623 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2624 goto Skip_Extra_Actual_Generation;
2626 else
2627 -- If the actual is a type conversion, then the constrained
2628 -- test applies to the actual, not the target type.
2630 declare
2631 Act_Prev : Node_Id;
2633 begin
2634 -- Test for unchecked conversions as well, which can occur
2635 -- as out parameter actuals on calls to stream procedures.
2637 Act_Prev := Prev;
2638 while Nkind_In (Act_Prev, N_Type_Conversion,
2639 N_Unchecked_Type_Conversion)
2640 loop
2641 Act_Prev := Expression (Act_Prev);
2642 end loop;
2644 -- If the expression is a conversion of a dereference, this
2645 -- is internally generated code that manipulates addresses,
2646 -- e.g. when building interface tables. No check should
2647 -- occur in this case, and the discriminated object is not
2648 -- directly a hand.
2650 if not Comes_From_Source (Actual)
2651 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2652 and then Nkind (Act_Prev) = N_Explicit_Dereference
2653 then
2654 Add_Extra_Actual
2655 (New_Occurrence_Of (Standard_False, Loc),
2656 Extra_Constrained (Formal));
2658 else
2659 Add_Extra_Actual
2660 (Make_Attribute_Reference (Sloc (Prev),
2661 Prefix =>
2662 Duplicate_Subexpr_No_Checks
2663 (Act_Prev, Name_Req => True),
2664 Attribute_Name => Name_Constrained),
2665 Extra_Constrained (Formal));
2666 end if;
2667 end;
2668 end if;
2669 end if;
2671 -- Create possible extra actual for accessibility level
2673 if Present (Extra_Accessibility (Formal)) then
2675 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2676 -- attribute, then the original actual may be an aliased object
2677 -- occurring as the prefix in a call using "Object.Operation"
2678 -- notation. In that case we must pass the level of the object,
2679 -- so Prev_Orig is reset to Prev and the attribute will be
2680 -- processed by the code for Access attributes further below.
2682 if Prev_Orig /= Prev
2683 and then Nkind (Prev) = N_Attribute_Reference
2684 and then
2685 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2686 and then Is_Aliased_View (Prev_Orig)
2687 then
2688 Prev_Orig := Prev;
2689 end if;
2691 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2692 -- accessibility levels.
2694 if Ekind (Current_Scope) in Subprogram_Kind
2695 and then Is_Thunk (Current_Scope)
2696 then
2697 declare
2698 Parm_Ent : Entity_Id;
2700 begin
2701 if Is_Controlling_Actual (Actual) then
2703 -- Find the corresponding actual of the thunk
2705 Parm_Ent := First_Entity (Current_Scope);
2706 for J in 2 .. Param_Count loop
2707 Next_Entity (Parm_Ent);
2708 end loop;
2710 -- Handle unchecked conversion of access types generated
2711 -- in thunks (cf. Expand_Interface_Thunk).
2713 elsif Is_Access_Type (Etype (Actual))
2714 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2715 then
2716 Parm_Ent := Entity (Expression (Actual));
2718 else pragma Assert (Is_Entity_Name (Actual));
2719 Parm_Ent := Entity (Actual);
2720 end if;
2722 Add_Extra_Actual
2723 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2724 Extra_Accessibility (Formal));
2725 end;
2727 elsif Is_Entity_Name (Prev_Orig) then
2729 -- When passing an access parameter, or a renaming of an access
2730 -- parameter, as the actual to another access parameter we need
2731 -- to pass along the actual's own access level parameter. This
2732 -- is done if we are within the scope of the formal access
2733 -- parameter (if this is an inlined body the extra formal is
2734 -- irrelevant).
2736 if (Is_Formal (Entity (Prev_Orig))
2737 or else
2738 (Present (Renamed_Object (Entity (Prev_Orig)))
2739 and then
2740 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2741 and then
2742 Is_Formal
2743 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2744 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2745 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2746 then
2747 declare
2748 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2750 begin
2751 pragma Assert (Present (Parm_Ent));
2753 if Present (Extra_Accessibility (Parm_Ent)) then
2754 Add_Extra_Actual
2755 (New_Occurrence_Of
2756 (Extra_Accessibility (Parm_Ent), Loc),
2757 Extra_Accessibility (Formal));
2759 -- If the actual access parameter does not have an
2760 -- associated extra formal providing its scope level,
2761 -- then treat the actual as having library-level
2762 -- accessibility.
2764 else
2765 Add_Extra_Actual
2766 (Make_Integer_Literal (Loc,
2767 Intval => Scope_Depth (Standard_Standard)),
2768 Extra_Accessibility (Formal));
2769 end if;
2770 end;
2772 -- The actual is a normal access value, so just pass the level
2773 -- of the actual's access type.
2775 else
2776 Add_Extra_Actual
2777 (Dynamic_Accessibility_Level (Prev_Orig),
2778 Extra_Accessibility (Formal));
2779 end if;
2781 -- If the actual is an access discriminant, then pass the level
2782 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2784 elsif Nkind (Prev_Orig) = N_Selected_Component
2785 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2786 E_Discriminant
2787 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2788 E_Anonymous_Access_Type
2789 then
2790 Add_Extra_Actual
2791 (Make_Integer_Literal (Loc,
2792 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2793 Extra_Accessibility (Formal));
2795 -- All other cases
2797 else
2798 case Nkind (Prev_Orig) is
2800 when N_Attribute_Reference =>
2801 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2803 -- For X'Access, pass on the level of the prefix X
2805 when Attribute_Access =>
2807 -- If this is an Access attribute applied to the
2808 -- the current instance object passed to a type
2809 -- initialization procedure, then use the level
2810 -- of the type itself. This is not really correct,
2811 -- as there should be an extra level parameter
2812 -- passed in with _init formals (only in the case
2813 -- where the type is immutably limited), but we
2814 -- don't have an easy way currently to create such
2815 -- an extra formal (init procs aren't ever frozen).
2816 -- For now we just use the level of the type,
2817 -- which may be too shallow, but that works better
2818 -- than passing Object_Access_Level of the type,
2819 -- which can be one level too deep in some cases.
2820 -- ???
2822 if Is_Entity_Name (Prefix (Prev_Orig))
2823 and then Is_Type (Entity (Prefix (Prev_Orig)))
2824 then
2825 Add_Extra_Actual
2826 (Make_Integer_Literal (Loc,
2827 Intval =>
2828 Type_Access_Level
2829 (Entity (Prefix (Prev_Orig)))),
2830 Extra_Accessibility (Formal));
2832 else
2833 Add_Extra_Actual
2834 (Make_Integer_Literal (Loc,
2835 Intval =>
2836 Object_Access_Level
2837 (Prefix (Prev_Orig))),
2838 Extra_Accessibility (Formal));
2839 end if;
2841 -- Treat the unchecked attributes as library-level
2843 when Attribute_Unchecked_Access |
2844 Attribute_Unrestricted_Access =>
2845 Add_Extra_Actual
2846 (Make_Integer_Literal (Loc,
2847 Intval => Scope_Depth (Standard_Standard)),
2848 Extra_Accessibility (Formal));
2850 -- No other cases of attributes returning access
2851 -- values that can be passed to access parameters.
2853 when others =>
2854 raise Program_Error;
2856 end case;
2858 -- For allocators we pass the level of the execution of the
2859 -- called subprogram, which is one greater than the current
2860 -- scope level.
2862 when N_Allocator =>
2863 Add_Extra_Actual
2864 (Make_Integer_Literal (Loc,
2865 Intval => Scope_Depth (Current_Scope) + 1),
2866 Extra_Accessibility (Formal));
2868 -- For most other cases we simply pass the level of the
2869 -- actual's access type. The type is retrieved from
2870 -- Prev rather than Prev_Orig, because in some cases
2871 -- Prev_Orig denotes an original expression that has
2872 -- not been analyzed.
2874 when others =>
2875 Add_Extra_Actual
2876 (Dynamic_Accessibility_Level (Prev),
2877 Extra_Accessibility (Formal));
2878 end case;
2879 end if;
2880 end if;
2882 -- Perform the check of 4.6(49) that prevents a null value from being
2883 -- passed as an actual to an access parameter. Note that the check
2884 -- is elided in the common cases of passing an access attribute or
2885 -- access parameter as an actual. Also, we currently don't enforce
2886 -- this check for expander-generated actuals and when -gnatdj is set.
2888 if Ada_Version >= Ada_2005 then
2890 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2891 -- the intent of 6.4.1(13) is that null-exclusion checks should
2892 -- not be done for 'out' parameters, even though it refers only
2893 -- to constraint checks, and a null_exclusion is not a constraint.
2894 -- Note that AI05-0196-1 corrects this mistake in the RM.
2896 if Is_Access_Type (Etype (Formal))
2897 and then Can_Never_Be_Null (Etype (Formal))
2898 and then Ekind (Formal) /= E_Out_Parameter
2899 and then Nkind (Prev) /= N_Raise_Constraint_Error
2900 and then (Known_Null (Prev)
2901 or else not Can_Never_Be_Null (Etype (Prev)))
2902 then
2903 Install_Null_Excluding_Check (Prev);
2904 end if;
2906 -- Ada_Version < Ada_2005
2908 else
2909 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2910 or else Access_Checks_Suppressed (Subp)
2911 then
2912 null;
2914 elsif Debug_Flag_J then
2915 null;
2917 elsif not Comes_From_Source (Prev) then
2918 null;
2920 elsif Is_Entity_Name (Prev)
2921 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2922 then
2923 null;
2925 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2926 null;
2928 -- Suppress null checks when passing to access parameters of Java
2929 -- and CIL subprograms. (Should this be done for other foreign
2930 -- conventions as well ???)
2932 elsif Convention (Subp) = Convention_Java
2933 or else Convention (Subp) = Convention_CIL
2934 then
2935 null;
2937 else
2938 Install_Null_Excluding_Check (Prev);
2939 end if;
2940 end if;
2942 -- Perform appropriate validity checks on parameters that
2943 -- are entities.
2945 if Validity_Checks_On then
2946 if (Ekind (Formal) = E_In_Parameter
2947 and then Validity_Check_In_Params)
2948 or else
2949 (Ekind (Formal) = E_In_Out_Parameter
2950 and then Validity_Check_In_Out_Params)
2951 then
2952 -- If the actual is an indexed component of a packed type (or
2953 -- is an indexed or selected component whose prefix recursively
2954 -- meets this condition), it has not been expanded yet. It will
2955 -- be copied in the validity code that follows, and has to be
2956 -- expanded appropriately, so reanalyze it.
2958 -- What we do is just to unset analyzed bits on prefixes till
2959 -- we reach something that does not have a prefix.
2961 declare
2962 Nod : Node_Id;
2964 begin
2965 Nod := Actual;
2966 while Nkind_In (Nod, N_Indexed_Component,
2967 N_Selected_Component)
2968 loop
2969 Set_Analyzed (Nod, False);
2970 Nod := Prefix (Nod);
2971 end loop;
2972 end;
2974 Ensure_Valid (Actual);
2975 end if;
2976 end if;
2978 -- For Ada 2012, if a parameter is aliased, the actual must be a
2979 -- tagged type or an aliased view of an object.
2981 if Is_Aliased (Formal)
2982 and then not Is_Aliased_View (Actual)
2983 and then not Is_Tagged_Type (Etype (Formal))
2984 then
2985 Error_Msg_NE
2986 ("actual for aliased formal& must be aliased object",
2987 Actual, Formal);
2988 end if;
2990 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2991 -- since this is a left side reference. We only do this for calls
2992 -- from the source program since we assume that compiler generated
2993 -- calls explicitly generate any required checks. We also need it
2994 -- only if we are doing standard validity checks, since clearly it is
2995 -- not needed if validity checks are off, and in subscript validity
2996 -- checking mode, all indexed components are checked with a call
2997 -- directly from Expand_N_Indexed_Component.
2999 if Comes_From_Source (Call_Node)
3000 and then Ekind (Formal) /= E_In_Parameter
3001 and then Validity_Checks_On
3002 and then Validity_Check_Default
3003 and then not Validity_Check_Subscripts
3004 then
3005 Check_Valid_Lvalue_Subscripts (Actual);
3006 end if;
3008 -- Mark any scalar OUT parameter that is a simple variable as no
3009 -- longer known to be valid (unless the type is always valid). This
3010 -- reflects the fact that if an OUT parameter is never set in a
3011 -- procedure, then it can become invalid on the procedure return.
3013 if Ekind (Formal) = E_Out_Parameter
3014 and then Is_Entity_Name (Actual)
3015 and then Ekind (Entity (Actual)) = E_Variable
3016 and then not Is_Known_Valid (Etype (Actual))
3017 then
3018 Set_Is_Known_Valid (Entity (Actual), False);
3019 end if;
3021 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3022 -- clear current values, since they can be clobbered. We are probably
3023 -- doing this in more places than we need to, but better safe than
3024 -- sorry when it comes to retaining bad current values!
3026 if Ekind (Formal) /= E_In_Parameter
3027 and then Is_Entity_Name (Actual)
3028 and then Present (Entity (Actual))
3029 then
3030 declare
3031 Ent : constant Entity_Id := Entity (Actual);
3032 Sav : Node_Id;
3034 begin
3035 -- For an OUT or IN OUT parameter that is an assignable entity,
3036 -- we do not want to clobber the Last_Assignment field, since
3037 -- if it is set, it was precisely because it is indeed an OUT
3038 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
3039 -- since the subprogram could have returned in invalid value.
3041 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3042 and then Is_Assignable (Ent)
3043 then
3044 Sav := Last_Assignment (Ent);
3045 Kill_Current_Values (Ent);
3046 Set_Last_Assignment (Ent, Sav);
3047 Set_Is_Known_Valid (Ent, False);
3049 -- For all other cases, just kill the current values
3051 else
3052 Kill_Current_Values (Ent);
3053 end if;
3054 end;
3055 end if;
3057 -- If the formal is class wide and the actual is an aggregate, force
3058 -- evaluation so that the back end who does not know about class-wide
3059 -- type, does not generate a temporary of the wrong size.
3061 if not Is_Class_Wide_Type (Etype (Formal)) then
3062 null;
3064 elsif Nkind (Actual) = N_Aggregate
3065 or else (Nkind (Actual) = N_Qualified_Expression
3066 and then Nkind (Expression (Actual)) = N_Aggregate)
3067 then
3068 Force_Evaluation (Actual);
3069 end if;
3071 -- In a remote call, if the formal is of a class-wide type, check
3072 -- that the actual meets the requirements described in E.4(18).
3074 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3075 Insert_Action (Actual,
3076 Make_Transportable_Check (Loc,
3077 Duplicate_Subexpr_Move_Checks (Actual)));
3078 end if;
3080 -- This label is required when skipping extra actual generation for
3081 -- Unchecked_Union parameters.
3083 <<Skip_Extra_Actual_Generation>>
3085 Param_Count := Param_Count + 1;
3086 Next_Actual (Actual);
3087 Next_Formal (Formal);
3088 end loop;
3090 -- If we are calling an Ada 2012 function which needs to have the
3091 -- "accessibility level determined by the point of call" (AI05-0234)
3092 -- passed in to it, then pass it in.
3094 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3095 and then
3096 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3097 then
3098 declare
3099 Ancestor : Node_Id := Parent (Call_Node);
3100 Level : Node_Id := Empty;
3101 Defer : Boolean := False;
3103 begin
3104 -- Unimplemented: if Subp returns an anonymous access type, then
3106 -- a) if the call is the operand of an explict conversion, then
3107 -- the target type of the conversion (a named access type)
3108 -- determines the accessibility level pass in;
3110 -- b) if the call defines an access discriminant of an object
3111 -- (e.g., the discriminant of an object being created by an
3112 -- allocator, or the discriminant of a function result),
3113 -- then the accessibility level to pass in is that of the
3114 -- discriminated object being initialized).
3116 -- ???
3118 while Nkind (Ancestor) = N_Qualified_Expression
3119 loop
3120 Ancestor := Parent (Ancestor);
3121 end loop;
3123 case Nkind (Ancestor) is
3124 when N_Allocator =>
3126 -- At this point, we'd like to assign
3128 -- Level := Dynamic_Accessibility_Level (Ancestor);
3130 -- but Etype of Ancestor may not have been set yet,
3131 -- so that doesn't work.
3133 -- Handle this later in Expand_Allocator_Expression.
3135 Defer := True;
3137 when N_Object_Declaration | N_Object_Renaming_Declaration =>
3138 declare
3139 Def_Id : constant Entity_Id :=
3140 Defining_Identifier (Ancestor);
3142 begin
3143 if Is_Return_Object (Def_Id) then
3144 if Present (Extra_Accessibility_Of_Result
3145 (Return_Applies_To (Scope (Def_Id))))
3146 then
3147 -- Pass along value that was passed in if the
3148 -- routine we are returning from also has an
3149 -- Accessibility_Of_Result formal.
3151 Level :=
3152 New_Occurrence_Of
3153 (Extra_Accessibility_Of_Result
3154 (Return_Applies_To (Scope (Def_Id))), Loc);
3155 end if;
3156 else
3157 Level :=
3158 Make_Integer_Literal (Loc,
3159 Intval => Object_Access_Level (Def_Id));
3160 end if;
3161 end;
3163 when N_Simple_Return_Statement =>
3164 if Present (Extra_Accessibility_Of_Result
3165 (Return_Applies_To
3166 (Return_Statement_Entity (Ancestor))))
3167 then
3168 -- Pass along value that was passed in if the routine
3169 -- we are returning from also has an
3170 -- Accessibility_Of_Result formal.
3172 Level :=
3173 New_Occurrence_Of
3174 (Extra_Accessibility_Of_Result
3175 (Return_Applies_To
3176 (Return_Statement_Entity (Ancestor))), Loc);
3177 end if;
3179 when others =>
3180 null;
3181 end case;
3183 if not Defer then
3184 if not Present (Level) then
3186 -- The "innermost master that evaluates the function call".
3188 -- ??? - Should we use Integer'Last here instead in order
3189 -- to deal with (some of) the problems associated with
3190 -- calls to subps whose enclosing scope is unknown (e.g.,
3191 -- Anon_Access_To_Subp_Param.all)?
3193 Level := Make_Integer_Literal (Loc,
3194 Scope_Depth (Current_Scope) + 1);
3195 end if;
3197 Add_Extra_Actual
3198 (Level,
3199 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3200 end if;
3201 end;
3202 end if;
3204 -- If we are expanding the RHS of an assignment we need to check if tag
3205 -- propagation is needed. You might expect this processing to be in
3206 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3207 -- assignment might be transformed to a declaration for an unconstrained
3208 -- value if the expression is classwide.
3210 if Nkind (Call_Node) = N_Function_Call
3211 and then Is_Tag_Indeterminate (Call_Node)
3212 and then Is_Entity_Name (Name (Call_Node))
3213 then
3214 declare
3215 Ass : Node_Id := Empty;
3217 begin
3218 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3219 Ass := Parent (Call_Node);
3221 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3222 and then Nkind (Parent (Parent (Call_Node))) =
3223 N_Assignment_Statement
3224 then
3225 Ass := Parent (Parent (Call_Node));
3227 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3228 and then Nkind (Parent (Parent (Call_Node))) =
3229 N_Assignment_Statement
3230 then
3231 Ass := Parent (Parent (Call_Node));
3232 end if;
3234 if Present (Ass)
3235 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3236 then
3237 if Is_Access_Type (Etype (Call_Node)) then
3238 if Designated_Type (Etype (Call_Node)) /=
3239 Root_Type (Etype (Name (Ass)))
3240 then
3241 Error_Msg_NE
3242 ("tag-indeterminate expression "
3243 & " must have designated type& (RM 5.2 (6))",
3244 Call_Node, Root_Type (Etype (Name (Ass))));
3245 else
3246 Propagate_Tag (Name (Ass), Call_Node);
3247 end if;
3249 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3250 Error_Msg_NE
3251 ("tag-indeterminate expression must have type&"
3252 & "(RM 5.2 (6))",
3253 Call_Node, Root_Type (Etype (Name (Ass))));
3255 else
3256 Propagate_Tag (Name (Ass), Call_Node);
3257 end if;
3259 -- The call will be rewritten as a dispatching call, and
3260 -- expanded as such.
3262 return;
3263 end if;
3264 end;
3265 end if;
3267 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3268 -- it to point to the correct secondary virtual table
3270 if Nkind (Call_Node) in N_Subprogram_Call
3271 and then CW_Interface_Formals_Present
3272 then
3273 Expand_Interface_Actuals (Call_Node);
3274 end if;
3276 -- Deals with Dispatch_Call if we still have a call, before expanding
3277 -- extra actuals since this will be done on the re-analysis of the
3278 -- dispatching call. Note that we do not try to shorten the actual list
3279 -- for a dispatching call, it would not make sense to do so. Expansion
3280 -- of dispatching calls is suppressed when VM_Target, because the VM
3281 -- back-ends directly handle the generation of dispatching calls and
3282 -- would have to undo any expansion to an indirect call.
3284 if Nkind (Call_Node) in N_Subprogram_Call
3285 and then Present (Controlling_Argument (Call_Node))
3286 then
3287 declare
3288 Call_Typ : constant Entity_Id := Etype (Call_Node);
3289 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3290 Eq_Prim_Op : Entity_Id := Empty;
3291 New_Call : Node_Id;
3292 Param : Node_Id;
3293 Prev_Call : Node_Id;
3295 begin
3296 if not Is_Limited_Type (Typ) then
3297 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3298 end if;
3300 if Tagged_Type_Expansion then
3301 Expand_Dispatching_Call (Call_Node);
3303 -- The following return is worrisome. Is it really OK to skip
3304 -- all remaining processing in this procedure ???
3306 return;
3308 -- VM targets
3310 else
3311 Apply_Tag_Checks (Call_Node);
3313 -- If this is a dispatching "=", we must first compare the
3314 -- tags so we generate: x.tag = y.tag and then x = y
3316 if Subp = Eq_Prim_Op then
3318 -- Mark the node as analyzed to avoid reanalizing this
3319 -- dispatching call (which would cause a never-ending loop)
3321 Prev_Call := Relocate_Node (Call_Node);
3322 Set_Analyzed (Prev_Call);
3324 Param := First_Actual (Call_Node);
3325 New_Call :=
3326 Make_And_Then (Loc,
3327 Left_Opnd =>
3328 Make_Op_Eq (Loc,
3329 Left_Opnd =>
3330 Make_Selected_Component (Loc,
3331 Prefix => New_Value (Param),
3332 Selector_Name =>
3333 New_Reference_To (First_Tag_Component (Typ),
3334 Loc)),
3336 Right_Opnd =>
3337 Make_Selected_Component (Loc,
3338 Prefix =>
3339 Unchecked_Convert_To (Typ,
3340 New_Value (Next_Actual (Param))),
3341 Selector_Name =>
3342 New_Reference_To
3343 (First_Tag_Component (Typ), Loc))),
3344 Right_Opnd => Prev_Call);
3346 Rewrite (Call_Node, New_Call);
3348 Analyze_And_Resolve
3349 (Call_Node, Call_Typ, Suppress => All_Checks);
3350 end if;
3352 -- Expansion of a dispatching call results in an indirect call,
3353 -- which in turn causes current values to be killed (see
3354 -- Resolve_Call), so on VM targets we do the call here to
3355 -- ensure consistent warnings between VM and non-VM targets.
3357 Kill_Current_Values;
3358 end if;
3360 -- If this is a dispatching "=" then we must update the reference
3361 -- to the call node because we generated:
3362 -- x.tag = y.tag and then x = y
3364 if Subp = Eq_Prim_Op then
3365 Call_Node := Right_Opnd (Call_Node);
3366 end if;
3367 end;
3368 end if;
3370 -- Similarly, expand calls to RCI subprograms on which pragma
3371 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3372 -- later. Do this only when the call comes from source since we
3373 -- do not want such a rewriting to occur in expanded code.
3375 if Is_All_Remote_Call (Call_Node) then
3376 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3378 -- Similarly, do not add extra actuals for an entry call whose entity
3379 -- is a protected procedure, or for an internal protected subprogram
3380 -- call, because it will be rewritten as a protected subprogram call
3381 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3383 elsif Is_Protected_Type (Scope (Subp))
3384 and then (Ekind (Subp) = E_Procedure
3385 or else Ekind (Subp) = E_Function)
3386 then
3387 null;
3389 -- During that loop we gathered the extra actuals (the ones that
3390 -- correspond to Extra_Formals), so now they can be appended.
3392 else
3393 while Is_Non_Empty_List (Extra_Actuals) loop
3394 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3395 end loop;
3396 end if;
3398 -- At this point we have all the actuals, so this is the point at which
3399 -- the various expansion activities for actuals is carried out.
3401 Expand_Actuals (Call_Node, Subp);
3403 -- Verify that the actuals do not share storage. This check must be done
3404 -- on the caller side rather that inside the subprogram to avoid issues
3405 -- of parameter passing.
3407 if Check_Aliasing_Of_Parameters then
3408 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3409 end if;
3411 -- If the subprogram is a renaming, or if it is inherited, replace it in
3412 -- the call with the name of the actual subprogram being called. If this
3413 -- is a dispatching call, the run-time decides what to call. The Alias
3414 -- attribute does not apply to entries.
3416 if Nkind (Call_Node) /= N_Entry_Call_Statement
3417 and then No (Controlling_Argument (Call_Node))
3418 and then Present (Parent_Subp)
3419 and then not Is_Direct_Deep_Call (Subp)
3420 then
3421 if Present (Inherited_From_Formal (Subp)) then
3422 Parent_Subp := Inherited_From_Formal (Subp);
3423 else
3424 Parent_Subp := Ultimate_Alias (Parent_Subp);
3425 end if;
3427 -- The below setting of Entity is suspect, see F109-018 discussion???
3429 Set_Entity (Name (Call_Node), Parent_Subp);
3431 if Is_Abstract_Subprogram (Parent_Subp)
3432 and then not In_Instance
3433 then
3434 Error_Msg_NE
3435 ("cannot call abstract subprogram &!",
3436 Name (Call_Node), Parent_Subp);
3437 end if;
3439 -- Inspect all formals of derived subprogram Subp. Compare parameter
3440 -- types with the parent subprogram and check whether an actual may
3441 -- need a type conversion to the corresponding formal of the parent
3442 -- subprogram.
3444 -- Not clear whether intrinsic subprograms need such conversions. ???
3446 if not Is_Intrinsic_Subprogram (Parent_Subp)
3447 or else Is_Generic_Instance (Parent_Subp)
3448 then
3449 declare
3450 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3451 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3452 -- and resolve the newly generated construct.
3454 -------------
3455 -- Convert --
3456 -------------
3458 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3459 begin
3460 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3461 Analyze (Act);
3462 Resolve (Act, Typ);
3463 end Convert;
3465 -- Local variables
3467 Actual_Typ : Entity_Id;
3468 Formal_Typ : Entity_Id;
3469 Parent_Typ : Entity_Id;
3471 begin
3472 Actual := First_Actual (Call_Node);
3473 Formal := First_Formal (Subp);
3474 Parent_Formal := First_Formal (Parent_Subp);
3475 while Present (Formal) loop
3476 Actual_Typ := Etype (Actual);
3477 Formal_Typ := Etype (Formal);
3478 Parent_Typ := Etype (Parent_Formal);
3480 -- For an IN parameter of a scalar type, the parent formal
3481 -- type and derived formal type differ or the parent formal
3482 -- type and actual type do not match statically.
3484 if Is_Scalar_Type (Formal_Typ)
3485 and then Ekind (Formal) = E_In_Parameter
3486 and then Formal_Typ /= Parent_Typ
3487 and then
3488 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3489 and then not Raises_Constraint_Error (Actual)
3490 then
3491 Convert (Actual, Parent_Typ);
3492 Enable_Range_Check (Actual);
3494 -- If the actual has been marked as requiring a range
3495 -- check, then generate it here.
3497 if Do_Range_Check (Actual) then
3498 Set_Do_Range_Check (Actual, False);
3499 Generate_Range_Check
3500 (Actual, Etype (Formal), CE_Range_Check_Failed);
3501 end if;
3503 -- For access types, the parent formal type and actual type
3504 -- differ.
3506 elsif Is_Access_Type (Formal_Typ)
3507 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3508 then
3509 if Ekind (Formal) /= E_In_Parameter then
3510 Convert (Actual, Parent_Typ);
3512 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3513 and then Designated_Type (Parent_Typ) /=
3514 Designated_Type (Actual_Typ)
3515 and then not Is_Controlling_Formal (Formal)
3516 then
3517 -- This unchecked conversion is not necessary unless
3518 -- inlining is enabled, because in that case the type
3519 -- mismatch may become visible in the body about to be
3520 -- inlined.
3522 Rewrite (Actual,
3523 Unchecked_Convert_To (Parent_Typ,
3524 Relocate_Node (Actual)));
3525 Analyze (Actual);
3526 Resolve (Actual, Parent_Typ);
3527 end if;
3529 -- For array and record types, the parent formal type and
3530 -- derived formal type have different sizes or pragma Pack
3531 -- status.
3533 elsif ((Is_Array_Type (Formal_Typ)
3534 and then Is_Array_Type (Parent_Typ))
3535 or else
3536 (Is_Record_Type (Formal_Typ)
3537 and then Is_Record_Type (Parent_Typ)))
3538 and then
3539 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3540 or else Has_Pragma_Pack (Formal_Typ) /=
3541 Has_Pragma_Pack (Parent_Typ))
3542 then
3543 Convert (Actual, Parent_Typ);
3544 end if;
3546 Next_Actual (Actual);
3547 Next_Formal (Formal);
3548 Next_Formal (Parent_Formal);
3549 end loop;
3550 end;
3551 end if;
3553 Orig_Subp := Subp;
3554 Subp := Parent_Subp;
3555 end if;
3557 -- Check for violation of No_Abort_Statements
3559 if Restriction_Check_Required (No_Abort_Statements)
3560 and then Is_RTE (Subp, RE_Abort_Task)
3561 then
3562 Check_Restriction (No_Abort_Statements, Call_Node);
3564 -- Check for violation of No_Dynamic_Attachment
3566 elsif Restriction_Check_Required (No_Dynamic_Attachment)
3567 and then RTU_Loaded (Ada_Interrupts)
3568 and then (Is_RTE (Subp, RE_Is_Reserved) or else
3569 Is_RTE (Subp, RE_Is_Attached) or else
3570 Is_RTE (Subp, RE_Current_Handler) or else
3571 Is_RTE (Subp, RE_Attach_Handler) or else
3572 Is_RTE (Subp, RE_Exchange_Handler) or else
3573 Is_RTE (Subp, RE_Detach_Handler) or else
3574 Is_RTE (Subp, RE_Reference))
3575 then
3576 Check_Restriction (No_Dynamic_Attachment, Call_Node);
3577 end if;
3579 -- Deal with case where call is an explicit dereference
3581 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3583 -- Handle case of access to protected subprogram type
3585 if Is_Access_Protected_Subprogram_Type
3586 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3587 then
3588 -- If this is a call through an access to protected operation, the
3589 -- prefix has the form (object'address, operation'access). Rewrite
3590 -- as a for other protected calls: the object is the 1st parameter
3591 -- of the list of actuals.
3593 declare
3594 Call : Node_Id;
3595 Parm : List_Id;
3596 Nam : Node_Id;
3597 Obj : Node_Id;
3598 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3600 T : constant Entity_Id :=
3601 Equivalent_Type (Base_Type (Etype (Ptr)));
3603 D_T : constant Entity_Id :=
3604 Designated_Type (Base_Type (Etype (Ptr)));
3606 begin
3607 Obj :=
3608 Make_Selected_Component (Loc,
3609 Prefix => Unchecked_Convert_To (T, Ptr),
3610 Selector_Name =>
3611 New_Occurrence_Of (First_Entity (T), Loc));
3613 Nam :=
3614 Make_Selected_Component (Loc,
3615 Prefix => Unchecked_Convert_To (T, Ptr),
3616 Selector_Name =>
3617 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3619 Nam :=
3620 Make_Explicit_Dereference (Loc,
3621 Prefix => Nam);
3623 if Present (Parameter_Associations (Call_Node)) then
3624 Parm := Parameter_Associations (Call_Node);
3625 else
3626 Parm := New_List;
3627 end if;
3629 Prepend (Obj, Parm);
3631 if Etype (D_T) = Standard_Void_Type then
3632 Call :=
3633 Make_Procedure_Call_Statement (Loc,
3634 Name => Nam,
3635 Parameter_Associations => Parm);
3636 else
3637 Call :=
3638 Make_Function_Call (Loc,
3639 Name => Nam,
3640 Parameter_Associations => Parm);
3641 end if;
3643 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3644 Set_Etype (Call, Etype (D_T));
3646 -- We do not re-analyze the call to avoid infinite recursion.
3647 -- We analyze separately the prefix and the object, and set
3648 -- the checks on the prefix that would otherwise be emitted
3649 -- when resolving a call.
3651 Rewrite (Call_Node, Call);
3652 Analyze (Nam);
3653 Apply_Access_Check (Nam);
3654 Analyze (Obj);
3655 return;
3656 end;
3657 end if;
3658 end if;
3660 -- If this is a call to an intrinsic subprogram, then perform the
3661 -- appropriate expansion to the corresponding tree node and we
3662 -- are all done (since after that the call is gone!)
3664 -- In the case where the intrinsic is to be processed by the back end,
3665 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3666 -- since the idea in this case is to pass the call unchanged. If the
3667 -- intrinsic is an inherited unchecked conversion, and the derived type
3668 -- is the target type of the conversion, we must retain it as the return
3669 -- type of the expression. Otherwise the expansion below, which uses the
3670 -- parent operation, will yield the wrong type.
3672 if Is_Intrinsic_Subprogram (Subp) then
3673 Expand_Intrinsic_Call (Call_Node, Subp);
3675 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3676 and then Parent_Subp /= Orig_Subp
3677 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3678 then
3679 Set_Etype (Call_Node, Etype (Orig_Subp));
3680 end if;
3682 return;
3683 end if;
3685 if Ekind_In (Subp, E_Function, E_Procedure) then
3687 -- We perform two simple optimization on calls:
3689 -- a) replace calls to null procedures unconditionally;
3691 -- b) for To_Address, just do an unchecked conversion. Not only is
3692 -- this efficient, but it also avoids order of elaboration problems
3693 -- when address clauses are inlined (address expression elaborated
3694 -- at the wrong point).
3696 -- We perform these optimization regardless of whether we are in the
3697 -- main unit or in a unit in the context of the main unit, to ensure
3698 -- that tree generated is the same in both cases, for Inspector use.
3700 if Is_RTE (Subp, RE_To_Address) then
3701 Rewrite (Call_Node,
3702 Unchecked_Convert_To
3703 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3704 return;
3706 elsif Is_Null_Procedure (Subp) then
3707 Rewrite (Call_Node, Make_Null_Statement (Loc));
3708 return;
3709 end if;
3711 -- Handle inlining (old semantics)
3713 if Is_Inlined (Subp) and then not Debug_Flag_Dot_K then
3714 Inlined_Subprogram : declare
3715 Bod : Node_Id;
3716 Must_Inline : Boolean := False;
3717 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3719 begin
3720 -- Verify that the body to inline has already been seen, and
3721 -- that if the body is in the current unit the inlining does
3722 -- not occur earlier. This avoids order-of-elaboration problems
3723 -- in the back end.
3725 -- This should be documented in sinfo/einfo ???
3727 if No (Spec)
3728 or else Nkind (Spec) /= N_Subprogram_Declaration
3729 or else No (Body_To_Inline (Spec))
3730 then
3731 Must_Inline := False;
3733 -- If this an inherited function that returns a private type,
3734 -- do not inline if the full view is an unconstrained array,
3735 -- because such calls cannot be inlined.
3737 elsif Present (Orig_Subp)
3738 and then Is_Array_Type (Etype (Orig_Subp))
3739 and then not Is_Constrained (Etype (Orig_Subp))
3740 then
3741 Must_Inline := False;
3743 elsif In_Unfrozen_Instance (Scope (Subp)) then
3744 Must_Inline := False;
3746 else
3747 Bod := Body_To_Inline (Spec);
3749 if (In_Extended_Main_Code_Unit (Call_Node)
3750 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3751 or else Has_Pragma_Inline_Always (Subp))
3752 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3753 or else
3754 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3755 then
3756 Must_Inline := True;
3758 -- If we are compiling a package body that is not the main
3759 -- unit, it must be for inlining/instantiation purposes,
3760 -- in which case we inline the call to insure that the same
3761 -- temporaries are generated when compiling the body by
3762 -- itself. Otherwise link errors can occur.
3764 -- If the function being called is itself in the main unit,
3765 -- we cannot inline, because there is a risk of double
3766 -- elaboration and/or circularity: the inlining can make
3767 -- visible a private entity in the body of the main unit,
3768 -- that gigi will see before its sees its proper definition.
3770 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3771 and then In_Package_Body
3772 then
3773 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3774 end if;
3775 end if;
3777 if Must_Inline then
3778 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3780 else
3781 -- Let the back end handle it
3783 Add_Inlined_Body (Subp);
3785 if Front_End_Inlining
3786 and then Nkind (Spec) = N_Subprogram_Declaration
3787 and then (In_Extended_Main_Code_Unit (Call_Node))
3788 and then No (Body_To_Inline (Spec))
3789 and then not Has_Completion (Subp)
3790 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3791 then
3792 Cannot_Inline
3793 ("cannot inline& (body not seen yet)?",
3794 Call_Node, Subp);
3795 end if;
3796 end if;
3797 end Inlined_Subprogram;
3799 -- Handle inlining (new semantics)
3801 elsif Is_Inlined (Subp) then
3802 declare
3803 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3805 begin
3806 if Must_Inline (Subp) then
3807 if In_Extended_Main_Code_Unit (Call_Node)
3808 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3809 and then not Has_Completion (Subp)
3810 then
3811 Cannot_Inline
3812 ("cannot inline& (body not seen yet)?",
3813 Call_Node, Subp);
3815 else
3816 Do_Inline_Always (Subp, Orig_Subp);
3817 end if;
3819 elsif Optimization_Level > 0 then
3820 Do_Inline (Subp, Orig_Subp);
3821 end if;
3823 -- The call may have been inlined or may have been passed to
3824 -- the backend. No further action needed if it was inlined.
3826 if Nkind (N) /= N_Function_Call then
3827 return;
3828 end if;
3829 end;
3830 end if;
3831 end if;
3833 -- Check for protected subprogram. This is either an intra-object call,
3834 -- or a protected function call. Protected procedure calls are rewritten
3835 -- as entry calls and handled accordingly.
3837 -- In Ada 2005, this may be an indirect call to an access parameter that
3838 -- is an access_to_subprogram. In that case the anonymous type has a
3839 -- scope that is a protected operation, but the call is a regular one.
3840 -- In either case do not expand call if subprogram is eliminated.
3842 Scop := Scope (Subp);
3844 if Nkind (Call_Node) /= N_Entry_Call_Statement
3845 and then Is_Protected_Type (Scop)
3846 and then Ekind (Subp) /= E_Subprogram_Type
3847 and then not Is_Eliminated (Subp)
3848 then
3849 -- If the call is an internal one, it is rewritten as a call to the
3850 -- corresponding unprotected subprogram.
3852 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3853 end if;
3855 -- Functions returning controlled objects need special attention. If
3856 -- the return type is limited, then the context is initialization and
3857 -- different processing applies. If the call is to a protected function,
3858 -- the expansion above will call Expand_Call recursively. Otherwise the
3859 -- function call is transformed into a temporary which obtains the
3860 -- result from the secondary stack.
3862 if Needs_Finalization (Etype (Subp)) then
3863 if not Is_Immutably_Limited_Type (Etype (Subp))
3864 and then
3865 (No (First_Formal (Subp))
3866 or else
3867 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3868 then
3869 Expand_Ctrl_Function_Call (Call_Node);
3871 -- Build-in-place function calls which appear in anonymous contexts
3872 -- need a transient scope to ensure the proper finalization of the
3873 -- intermediate result after its use.
3875 elsif Is_Build_In_Place_Function_Call (Call_Node)
3876 and then
3877 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3878 N_Function_Call,
3879 N_Indexed_Component,
3880 N_Object_Renaming_Declaration,
3881 N_Procedure_Call_Statement,
3882 N_Selected_Component,
3883 N_Slice)
3884 then
3885 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3886 end if;
3887 end if;
3889 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3890 -- if there are optional parameters at the trailing end.
3891 -- Note: we never delete procedures for call via a pointer.
3893 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3894 and then Present (First_Optional_Parameter (Subp))
3895 then
3896 declare
3897 Last_Keep_Arg : Node_Id;
3899 begin
3900 -- Last_Keep_Arg will hold the last actual that should be kept.
3901 -- If it remains empty at the end, it means that all parameters
3902 -- are optional.
3904 Last_Keep_Arg := Empty;
3906 -- Find first optional parameter, must be present since we checked
3907 -- the validity of the parameter before setting it.
3909 Formal := First_Formal (Subp);
3910 Actual := First_Actual (Call_Node);
3911 while Formal /= First_Optional_Parameter (Subp) loop
3912 Last_Keep_Arg := Actual;
3913 Next_Formal (Formal);
3914 Next_Actual (Actual);
3915 end loop;
3917 -- We have Formal and Actual pointing to the first potentially
3918 -- droppable argument. We can drop all the trailing arguments
3919 -- whose actual matches the default. Note that we know that all
3920 -- remaining formals have defaults, because we checked that this
3921 -- requirement was met before setting First_Optional_Parameter.
3923 -- We use Fully_Conformant_Expressions to check for identity
3924 -- between formals and actuals, which may miss some cases, but
3925 -- on the other hand, this is only an optimization (if we fail
3926 -- to truncate a parameter it does not affect functionality).
3927 -- So if the default is 3 and the actual is 1+2, we consider
3928 -- them unequal, which hardly seems worrisome.
3930 while Present (Formal) loop
3931 if not Fully_Conformant_Expressions
3932 (Actual, Default_Value (Formal))
3933 then
3934 Last_Keep_Arg := Actual;
3935 end if;
3937 Next_Formal (Formal);
3938 Next_Actual (Actual);
3939 end loop;
3941 -- If no arguments, delete entire list, this is the easy case
3943 if No (Last_Keep_Arg) then
3944 Set_Parameter_Associations (Call_Node, No_List);
3945 Set_First_Named_Actual (Call_Node, Empty);
3947 -- Case where at the last retained argument is positional. This
3948 -- is also an easy case, since the retained arguments are already
3949 -- in the right form, and we don't need to worry about the order
3950 -- of arguments that get eliminated.
3952 elsif Is_List_Member (Last_Keep_Arg) then
3953 while Present (Next (Last_Keep_Arg)) loop
3954 Discard_Node (Remove_Next (Last_Keep_Arg));
3955 end loop;
3957 Set_First_Named_Actual (Call_Node, Empty);
3959 -- This is the annoying case where the last retained argument
3960 -- is a named parameter. Since the original arguments are not
3961 -- in declaration order, we may have to delete some fairly
3962 -- random collection of arguments.
3964 else
3965 declare
3966 Temp : Node_Id;
3967 Passoc : Node_Id;
3969 begin
3970 -- First step, remove all the named parameters from the
3971 -- list (they are still chained using First_Named_Actual
3972 -- and Next_Named_Actual, so we have not lost them!)
3974 Temp := First (Parameter_Associations (Call_Node));
3976 -- Case of all parameters named, remove them all
3978 if Nkind (Temp) = N_Parameter_Association then
3979 -- Suppress warnings to avoid warning on possible
3980 -- infinite loop (because Call_Node is not modified).
3982 pragma Warnings (Off);
3983 while Is_Non_Empty_List
3984 (Parameter_Associations (Call_Node))
3985 loop
3986 Temp :=
3987 Remove_Head (Parameter_Associations (Call_Node));
3988 end loop;
3989 pragma Warnings (On);
3991 -- Case of mixed positional/named, remove named parameters
3993 else
3994 while Nkind (Next (Temp)) /= N_Parameter_Association loop
3995 Next (Temp);
3996 end loop;
3998 while Present (Next (Temp)) loop
3999 Remove (Next (Temp));
4000 end loop;
4001 end if;
4003 -- Now we loop through the named parameters, till we get
4004 -- to the last one to be retained, adding them to the list.
4005 -- Note that the Next_Named_Actual list does not need to be
4006 -- touched since we are only reordering them on the actual
4007 -- parameter association list.
4009 Passoc := Parent (First_Named_Actual (Call_Node));
4010 loop
4011 Temp := Relocate_Node (Passoc);
4012 Append_To
4013 (Parameter_Associations (Call_Node), Temp);
4014 exit when
4015 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
4016 Passoc := Parent (Next_Named_Actual (Passoc));
4017 end loop;
4019 Set_Next_Named_Actual (Temp, Empty);
4021 loop
4022 Temp := Next_Named_Actual (Passoc);
4023 exit when No (Temp);
4024 Set_Next_Named_Actual
4025 (Passoc, Next_Named_Actual (Parent (Temp)));
4026 end loop;
4027 end;
4029 end if;
4030 end;
4031 end if;
4032 end Expand_Call;
4034 -------------------------------
4035 -- Expand_Ctrl_Function_Call --
4036 -------------------------------
4038 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4039 function Enclosing_Context return Node_Id;
4040 -- Find the enclosing context where the function call appears
4042 -----------------------
4043 -- Enclosing_Context --
4044 -----------------------
4046 function Enclosing_Context return Node_Id is
4047 Context : Node_Id;
4049 begin
4050 Context := Parent (N);
4051 while Present (Context) loop
4053 -- The following could use a comment (and why is N_Case_Expression
4054 -- not treated in a similar manner ???
4056 if Nkind (Context) = N_If_Expression then
4057 exit;
4059 -- Stop the search when reaching any statement because we have
4060 -- gone too far up the tree.
4062 elsif Nkind (Context) = N_Procedure_Call_Statement
4063 or else Nkind (Context) in N_Statement_Other_Than_Procedure_Call
4064 then
4065 exit;
4066 end if;
4068 Context := Parent (Context);
4069 end loop;
4071 return Context;
4072 end Enclosing_Context;
4074 -- Local variables
4076 Context : constant Node_Id := Enclosing_Context;
4078 begin
4079 -- Optimization, if the returned value (which is on the sec-stack) is
4080 -- returned again, no need to copy/readjust/finalize, we can just pass
4081 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4082 -- attachment is needed
4084 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4085 return;
4086 end if;
4088 -- Resolution is now finished, make sure we don't start analysis again
4089 -- because of the duplication.
4091 Set_Analyzed (N);
4093 -- A function which returns a controlled object uses the secondary
4094 -- stack. Rewrite the call into a temporary which obtains the result of
4095 -- the function using 'reference.
4097 Remove_Side_Effects (N);
4099 -- The function call is part of an if expression dependent expression.
4100 -- The temporary result must live as long as the if expression itself,
4101 -- otherwise it will be finalized too early. Mark the transient as
4102 -- processed to avoid untimely finalization.
4104 -- Why no special handling for case expressions here ???
4106 if Present (Context)
4107 and then Nkind (Context) = N_If_Expression
4108 and then Nkind (N) = N_Explicit_Dereference
4109 then
4110 Set_Is_Processed_Transient (Entity (Prefix (N)));
4111 end if;
4112 end Expand_Ctrl_Function_Call;
4114 -------------------------
4115 -- Expand_Inlined_Call --
4116 -------------------------
4118 procedure Expand_Inlined_Call
4119 (N : Node_Id;
4120 Subp : Entity_Id;
4121 Orig_Subp : Entity_Id)
4123 Loc : constant Source_Ptr := Sloc (N);
4124 Is_Predef : constant Boolean :=
4125 Is_Predefined_File_Name
4126 (Unit_File_Name (Get_Source_Unit (Subp)));
4127 Orig_Bod : constant Node_Id :=
4128 Body_To_Inline (Unit_Declaration_Node (Subp));
4130 Blk : Node_Id;
4131 Decl : Node_Id;
4132 Decls : constant List_Id := New_List;
4133 Exit_Lab : Entity_Id := Empty;
4134 F : Entity_Id;
4135 A : Node_Id;
4136 Lab_Decl : Node_Id;
4137 Lab_Id : Node_Id;
4138 New_A : Node_Id;
4139 Num_Ret : Int := 0;
4140 Ret_Type : Entity_Id;
4142 Targ : Node_Id;
4143 -- The target of the call. If context is an assignment statement then
4144 -- this is the left-hand side of the assignment, else it is a temporary
4145 -- to which the return value is assigned prior to rewriting the call.
4147 Targ1 : Node_Id;
4148 -- A separate target used when the return type is unconstrained
4150 Temp : Entity_Id;
4151 Temp_Typ : Entity_Id;
4153 Return_Object : Entity_Id := Empty;
4154 -- Entity in declaration in an extended_return_statement
4156 Is_Unc : Boolean;
4157 Is_Unc_Decl : Boolean;
4158 -- If the type returned by the function is unconstrained and the call
4159 -- can be inlined, special processing is required.
4161 procedure Make_Exit_Label;
4162 -- Build declaration for exit label to be used in Return statements,
4163 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
4164 -- declaration). Does nothing if Exit_Lab already set.
4166 function Process_Formals (N : Node_Id) return Traverse_Result;
4167 -- Replace occurrence of a formal with the corresponding actual, or the
4168 -- thunk generated for it.
4170 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
4171 -- If the call being expanded is that of an internal subprogram, set the
4172 -- sloc of the generated block to that of the call itself, so that the
4173 -- expansion is skipped by the "next" command in gdb.
4174 -- Same processing for a subprogram in a predefined file, e.g.
4175 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
4176 -- simplify our own development.
4178 procedure Reset_Dispatching_Calls (N : Node_Id);
4179 -- In subtree N search for occurrences of dispatching calls that use the
4180 -- Ada 2005 Object.Operation notation and the object is a formal of the
4181 -- inlined subprogram. Reset the entity associated with Operation in all
4182 -- the found occurrences.
4184 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
4185 -- If the function body is a single expression, replace call with
4186 -- expression, else insert block appropriately.
4188 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
4189 -- If procedure body has no local variables, inline body without
4190 -- creating block, otherwise rewrite call with block.
4192 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
4193 -- Determine whether a formal parameter is used only once in Orig_Bod
4195 ---------------------
4196 -- Make_Exit_Label --
4197 ---------------------
4199 procedure Make_Exit_Label is
4200 Lab_Ent : Entity_Id;
4201 begin
4202 if No (Exit_Lab) then
4203 Lab_Ent := Make_Temporary (Loc, 'L');
4204 Lab_Id := New_Reference_To (Lab_Ent, Loc);
4205 Exit_Lab := Make_Label (Loc, Lab_Id);
4206 Lab_Decl :=
4207 Make_Implicit_Label_Declaration (Loc,
4208 Defining_Identifier => Lab_Ent,
4209 Label_Construct => Exit_Lab);
4210 end if;
4211 end Make_Exit_Label;
4213 ---------------------
4214 -- Process_Formals --
4215 ---------------------
4217 function Process_Formals (N : Node_Id) return Traverse_Result is
4218 A : Entity_Id;
4219 E : Entity_Id;
4220 Ret : Node_Id;
4222 begin
4223 if Is_Entity_Name (N) and then Present (Entity (N)) then
4224 E := Entity (N);
4226 if Is_Formal (E)
4227 and then Scope (E) = Subp
4228 then
4229 A := Renamed_Object (E);
4231 -- Rewrite the occurrence of the formal into an occurrence of
4232 -- the actual. Also establish visibility on the proper view of
4233 -- the actual's subtype for the body's context (if the actual's
4234 -- subtype is private at the call point but its full view is
4235 -- visible to the body, then the inlined tree here must be
4236 -- analyzed with the full view).
4238 if Is_Entity_Name (A) then
4239 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
4240 Check_Private_View (N);
4242 elsif Nkind (A) = N_Defining_Identifier then
4243 Rewrite (N, New_Occurrence_Of (A, Loc));
4244 Check_Private_View (N);
4246 -- Numeric literal
4248 else
4249 Rewrite (N, New_Copy (A));
4250 end if;
4251 end if;
4253 return Skip;
4255 elsif Is_Entity_Name (N)
4256 and then Present (Return_Object)
4257 and then Chars (N) = Chars (Return_Object)
4258 then
4259 -- Occurrence within an extended return statement. The return
4260 -- object is local to the body been inlined, and thus the generic
4261 -- copy is not analyzed yet, so we match by name, and replace it
4262 -- with target of call.
4264 if Nkind (Targ) = N_Defining_Identifier then
4265 Rewrite (N, New_Occurrence_Of (Targ, Loc));
4266 else
4267 Rewrite (N, New_Copy_Tree (Targ));
4268 end if;
4270 return Skip;
4272 elsif Nkind (N) = N_Simple_Return_Statement then
4273 if No (Expression (N)) then
4274 Make_Exit_Label;
4275 Rewrite (N,
4276 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
4278 else
4279 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
4280 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
4281 then
4282 -- Function body is a single expression. No need for
4283 -- exit label.
4285 null;
4287 else
4288 Num_Ret := Num_Ret + 1;
4289 Make_Exit_Label;
4290 end if;
4292 -- Because of the presence of private types, the views of the
4293 -- expression and the context may be different, so place an
4294 -- unchecked conversion to the context type to avoid spurious
4295 -- errors, e.g. when the expression is a numeric literal and
4296 -- the context is private. If the expression is an aggregate,
4297 -- use a qualified expression, because an aggregate is not a
4298 -- legal argument of a conversion.
4300 if Nkind_In (Expression (N), N_Aggregate, N_Null) then
4301 Ret :=
4302 Make_Qualified_Expression (Sloc (N),
4303 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
4304 Expression => Relocate_Node (Expression (N)));
4305 else
4306 Ret :=
4307 Unchecked_Convert_To
4308 (Ret_Type, Relocate_Node (Expression (N)));
4309 end if;
4311 if Nkind (Targ) = N_Defining_Identifier then
4312 Rewrite (N,
4313 Make_Assignment_Statement (Loc,
4314 Name => New_Occurrence_Of (Targ, Loc),
4315 Expression => Ret));
4316 else
4317 Rewrite (N,
4318 Make_Assignment_Statement (Loc,
4319 Name => New_Copy (Targ),
4320 Expression => Ret));
4321 end if;
4323 Set_Assignment_OK (Name (N));
4325 if Present (Exit_Lab) then
4326 Insert_After (N,
4327 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
4328 end if;
4329 end if;
4331 return OK;
4333 -- An extended return becomes a block whose first statement is the
4334 -- assignment of the initial expression of the return object to the
4335 -- target of the call itself.
4337 elsif Nkind (N) = N_Extended_Return_Statement then
4338 declare
4339 Return_Decl : constant Entity_Id :=
4340 First (Return_Object_Declarations (N));
4341 Assign : Node_Id;
4343 begin
4344 Return_Object := Defining_Identifier (Return_Decl);
4346 if Present (Expression (Return_Decl)) then
4347 if Nkind (Targ) = N_Defining_Identifier then
4348 Assign :=
4349 Make_Assignment_Statement (Loc,
4350 Name => New_Occurrence_Of (Targ, Loc),
4351 Expression => Expression (Return_Decl));
4352 else
4353 Assign :=
4354 Make_Assignment_Statement (Loc,
4355 Name => New_Copy (Targ),
4356 Expression => Expression (Return_Decl));
4357 end if;
4359 Set_Assignment_OK (Name (Assign));
4361 if No (Handled_Statement_Sequence (N)) then
4362 Set_Handled_Statement_Sequence (N,
4363 Make_Handled_Sequence_Of_Statements (Loc,
4364 Statements => New_List));
4365 end if;
4367 Prepend (Assign,
4368 Statements (Handled_Statement_Sequence (N)));
4369 end if;
4371 Rewrite (N,
4372 Make_Block_Statement (Loc,
4373 Handled_Statement_Sequence =>
4374 Handled_Statement_Sequence (N)));
4376 return OK;
4377 end;
4379 -- Remove pragma Unreferenced since it may refer to formals that
4380 -- are not visible in the inlined body, and in any case we will
4381 -- not be posting warnings on the inlined body so it is unneeded.
4383 elsif Nkind (N) = N_Pragma
4384 and then Pragma_Name (N) = Name_Unreferenced
4385 then
4386 Rewrite (N, Make_Null_Statement (Sloc (N)));
4387 return OK;
4389 else
4390 return OK;
4391 end if;
4392 end Process_Formals;
4394 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
4396 ------------------
4397 -- Process_Sloc --
4398 ------------------
4400 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
4401 begin
4402 if not Debug_Generated_Code then
4403 Set_Sloc (Nod, Sloc (N));
4404 Set_Comes_From_Source (Nod, False);
4405 end if;
4407 return OK;
4408 end Process_Sloc;
4410 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
4412 ------------------------------
4413 -- Reset_Dispatching_Calls --
4414 ------------------------------
4416 procedure Reset_Dispatching_Calls (N : Node_Id) is
4418 function Do_Reset (N : Node_Id) return Traverse_Result;
4419 -- Comment required ???
4421 --------------
4422 -- Do_Reset --
4423 --------------
4425 function Do_Reset (N : Node_Id) return Traverse_Result is
4426 begin
4427 if Nkind (N) = N_Procedure_Call_Statement
4428 and then Nkind (Name (N)) = N_Selected_Component
4429 and then Nkind (Prefix (Name (N))) = N_Identifier
4430 and then Is_Formal (Entity (Prefix (Name (N))))
4431 and then Is_Dispatching_Operation
4432 (Entity (Selector_Name (Name (N))))
4433 then
4434 Set_Entity (Selector_Name (Name (N)), Empty);
4435 end if;
4437 return OK;
4438 end Do_Reset;
4440 function Do_Reset_Calls is new Traverse_Func (Do_Reset);
4442 -- Local variables
4444 Dummy : constant Traverse_Result := Do_Reset_Calls (N);
4445 pragma Unreferenced (Dummy);
4447 -- Start of processing for Reset_Dispatching_Calls
4449 begin
4450 null;
4451 end Reset_Dispatching_Calls;
4453 ---------------------------
4454 -- Rewrite_Function_Call --
4455 ---------------------------
4457 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
4458 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
4459 Fst : constant Node_Id := First (Statements (HSS));
4461 begin
4462 -- Optimize simple case: function body is a single return statement,
4463 -- which has been expanded into an assignment.
4465 if Is_Empty_List (Declarations (Blk))
4466 and then Nkind (Fst) = N_Assignment_Statement
4467 and then No (Next (Fst))
4468 then
4469 -- The function call may have been rewritten as the temporary
4470 -- that holds the result of the call, in which case remove the
4471 -- now useless declaration.
4473 if Nkind (N) = N_Identifier
4474 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4475 then
4476 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
4477 end if;
4479 Rewrite (N, Expression (Fst));
4481 elsif Nkind (N) = N_Identifier
4482 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4483 then
4484 -- The block assigns the result of the call to the temporary
4486 Insert_After (Parent (Entity (N)), Blk);
4488 -- If the context is an assignment, and the left-hand side is free of
4489 -- side-effects, the replacement is also safe.
4490 -- Can this be generalized further???
4492 elsif Nkind (Parent (N)) = N_Assignment_Statement
4493 and then
4494 (Is_Entity_Name (Name (Parent (N)))
4495 or else
4496 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
4497 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
4499 or else
4500 (Nkind (Name (Parent (N))) = N_Selected_Component
4501 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
4502 then
4503 -- Replace assignment with the block
4505 declare
4506 Original_Assignment : constant Node_Id := Parent (N);
4508 begin
4509 -- Preserve the original assignment node to keep the complete
4510 -- assignment subtree consistent enough for Analyze_Assignment
4511 -- to proceed (specifically, the original Lhs node must still
4512 -- have an assignment statement as its parent).
4514 -- We cannot rely on Original_Node to go back from the block
4515 -- node to the assignment node, because the assignment might
4516 -- already be a rewrite substitution.
4518 Discard_Node (Relocate_Node (Original_Assignment));
4519 Rewrite (Original_Assignment, Blk);
4520 end;
4522 elsif Nkind (Parent (N)) = N_Object_Declaration then
4524 -- A call to a function which returns an unconstrained type
4525 -- found in the expression initializing an object-declaration is
4526 -- expanded into a procedure call which must be added after the
4527 -- object declaration.
4529 if Is_Unc_Decl and then Debug_Flag_Dot_K then
4530 Insert_Action_After (Parent (N), Blk);
4531 else
4532 Set_Expression (Parent (N), Empty);
4533 Insert_After (Parent (N), Blk);
4534 end if;
4536 elsif Is_Unc and then not Debug_Flag_Dot_K then
4537 Insert_Before (Parent (N), Blk);
4538 end if;
4539 end Rewrite_Function_Call;
4541 ----------------------------
4542 -- Rewrite_Procedure_Call --
4543 ----------------------------
4545 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
4546 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
4548 begin
4549 -- If there is a transient scope for N, this will be the scope of the
4550 -- actions for N, and the statements in Blk need to be within this
4551 -- scope. For example, they need to have visibility on the constant
4552 -- declarations created for the formals.
4554 -- If N needs no transient scope, and if there are no declarations in
4555 -- the inlined body, we can do a little optimization and insert the
4556 -- statements for the body directly after N, and rewrite N to a
4557 -- null statement, instead of rewriting N into a full-blown block
4558 -- statement.
4560 if not Scope_Is_Transient
4561 and then Is_Empty_List (Declarations (Blk))
4562 then
4563 Insert_List_After (N, Statements (HSS));
4564 Rewrite (N, Make_Null_Statement (Loc));
4565 else
4566 Rewrite (N, Blk);
4567 end if;
4568 end Rewrite_Procedure_Call;
4570 -------------------------
4571 -- Formal_Is_Used_Once --
4572 -------------------------
4574 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
4575 Use_Counter : Int := 0;
4577 function Count_Uses (N : Node_Id) return Traverse_Result;
4578 -- Traverse the tree and count the uses of the formal parameter.
4579 -- In this case, for optimization purposes, we do not need to
4580 -- continue the traversal once more than one use is encountered.
4582 ----------------
4583 -- Count_Uses --
4584 ----------------
4586 function Count_Uses (N : Node_Id) return Traverse_Result is
4587 begin
4588 -- The original node is an identifier
4590 if Nkind (N) = N_Identifier
4591 and then Present (Entity (N))
4593 -- Original node's entity points to the one in the copied body
4595 and then Nkind (Entity (N)) = N_Identifier
4596 and then Present (Entity (Entity (N)))
4598 -- The entity of the copied node is the formal parameter
4600 and then Entity (Entity (N)) = Formal
4601 then
4602 Use_Counter := Use_Counter + 1;
4604 if Use_Counter > 1 then
4606 -- Denote more than one use and abandon the traversal
4608 Use_Counter := 2;
4609 return Abandon;
4611 end if;
4612 end if;
4614 return OK;
4615 end Count_Uses;
4617 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
4619 -- Start of processing for Formal_Is_Used_Once
4621 begin
4622 Count_Formal_Uses (Orig_Bod);
4623 return Use_Counter = 1;
4624 end Formal_Is_Used_Once;
4626 -- Start of processing for Expand_Inlined_Call
4628 begin
4629 -- Initializations for old/new semantics
4631 if not Debug_Flag_Dot_K then
4632 Is_Unc := Is_Array_Type (Etype (Subp))
4633 and then not Is_Constrained (Etype (Subp));
4634 Is_Unc_Decl := False;
4635 else
4636 Is_Unc := Returns_Unconstrained_Type (Subp)
4637 and then Optimization_Level > 0;
4638 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
4639 and then Is_Unc;
4640 end if;
4642 -- Check for an illegal attempt to inline a recursive procedure. If the
4643 -- subprogram has parameters this is detected when trying to supply a
4644 -- binding for parameters that already have one. For parameterless
4645 -- subprograms this must be done explicitly.
4647 if In_Open_Scopes (Subp) then
4648 Error_Msg_N ("call to recursive subprogram cannot be inlined??", N);
4649 Set_Is_Inlined (Subp, False);
4650 return;
4652 -- Skip inlining if this is not a true inlining since the attribute
4653 -- Body_To_Inline is also set for renamings (see sinfo.ads)
4655 elsif Nkind (Orig_Bod) in N_Entity then
4656 return;
4658 -- Skip inlining if the function returns an unconstrained type using
4659 -- an extended return statement since this part of the new inlining
4660 -- model which is not yet supported by the current implementation. ???
4662 elsif Is_Unc
4663 and then
4664 Nkind (First (Statements (Handled_Statement_Sequence (Orig_Bod))))
4665 = N_Extended_Return_Statement
4666 and then not Debug_Flag_Dot_K
4667 then
4668 return;
4669 end if;
4671 if Nkind (Orig_Bod) = N_Defining_Identifier
4672 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
4673 then
4674 -- Subprogram is renaming_as_body. Calls occurring after the renaming
4675 -- can be replaced with calls to the renamed entity directly, because
4676 -- the subprograms are subtype conformant. If the renamed subprogram
4677 -- is an inherited operation, we must redo the expansion because
4678 -- implicit conversions may be needed. Similarly, if the renamed
4679 -- entity is inlined, expand the call for further optimizations.
4681 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
4683 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
4684 Expand_Call (N);
4685 end if;
4687 return;
4688 end if;
4690 -- Register the call in the list of inlined calls
4692 if Inlined_Calls = No_Elist then
4693 Inlined_Calls := New_Elmt_List;
4694 end if;
4696 Append_Elmt (N, To => Inlined_Calls);
4698 -- Use generic machinery to copy body of inlined subprogram, as if it
4699 -- were an instantiation, resetting source locations appropriately, so
4700 -- that nested inlined calls appear in the main unit.
4702 Save_Env (Subp, Empty);
4703 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
4705 -- Old semantics
4707 if not Debug_Flag_Dot_K then
4708 declare
4709 Bod : Node_Id;
4711 begin
4712 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
4713 Blk :=
4714 Make_Block_Statement (Loc,
4715 Declarations => Declarations (Bod),
4716 Handled_Statement_Sequence =>
4717 Handled_Statement_Sequence (Bod));
4719 if No (Declarations (Bod)) then
4720 Set_Declarations (Blk, New_List);
4721 end if;
4723 -- For the unconstrained case, capture the name of the local
4724 -- variable that holds the result. This must be the first
4725 -- declaration in the block, because its bounds cannot depend
4726 -- on local variables. Otherwise there is no way to declare the
4727 -- result outside of the block. Needless to say, in general the
4728 -- bounds will depend on the actuals in the call.
4730 -- If the context is an assignment statement, as is the case
4731 -- for the expansion of an extended return, the left-hand side
4732 -- provides bounds even if the return type is unconstrained.
4734 if Is_Unc then
4735 declare
4736 First_Decl : Node_Id;
4738 begin
4739 First_Decl := First (Declarations (Blk));
4741 if Nkind (First_Decl) /= N_Object_Declaration then
4742 return;
4743 end if;
4745 if Nkind (Parent (N)) /= N_Assignment_Statement then
4746 Targ1 := Defining_Identifier (First_Decl);
4747 else
4748 Targ1 := Name (Parent (N));
4749 end if;
4750 end;
4751 end if;
4752 end;
4754 -- New semantics
4756 else
4757 declare
4758 Bod : Node_Id;
4760 begin
4761 -- General case
4763 if not Is_Unc then
4764 Bod :=
4765 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
4766 Blk :=
4767 Make_Block_Statement (Loc,
4768 Declarations => Declarations (Bod),
4769 Handled_Statement_Sequence =>
4770 Handled_Statement_Sequence (Bod));
4772 -- Inline a call to a function that returns an unconstrained type.
4773 -- The semantic analyzer checked that frontend-inlined functions
4774 -- returning unconstrained types have no declarations and have
4775 -- a single extended return statement. As part of its processing
4776 -- the function was split in two subprograms: a procedure P and
4777 -- a function F that has a block with a call to procedure P (see
4778 -- Split_Unconstrained_Function).
4780 else
4781 pragma Assert
4782 (Nkind
4783 (First
4784 (Statements (Handled_Statement_Sequence (Orig_Bod))))
4785 = N_Block_Statement);
4787 declare
4788 Blk_Stmt : constant Node_Id :=
4789 First
4790 (Statements
4791 (Handled_Statement_Sequence (Orig_Bod)));
4792 First_Stmt : constant Node_Id :=
4793 First
4794 (Statements
4795 (Handled_Statement_Sequence (Blk_Stmt)));
4796 Second_Stmt : constant Node_Id := Next (First_Stmt);
4798 begin
4799 pragma Assert
4800 (Nkind (First_Stmt) = N_Procedure_Call_Statement
4801 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
4802 and then No (Next (Second_Stmt)));
4804 Bod :=
4805 Copy_Generic_Node
4806 (First
4807 (Statements (Handled_Statement_Sequence (Orig_Bod))),
4808 Empty, Instantiating => True);
4809 Blk := Bod;
4811 -- Capture the name of the local variable that holds the
4812 -- result. This must be the first declaration in the block,
4813 -- because its bounds cannot depend on local variables.
4814 -- Otherwise there is no way to declare the result outside
4815 -- of the block. Needless to say, in general the bounds will
4816 -- depend on the actuals in the call.
4818 if Nkind (Parent (N)) /= N_Assignment_Statement then
4819 Targ1 := Defining_Identifier (First (Declarations (Blk)));
4821 -- If the context is an assignment statement, as is the case
4822 -- for the expansion of an extended return, the left-hand
4823 -- side provides bounds even if the return type is
4824 -- unconstrained.
4826 else
4827 Targ1 := Name (Parent (N));
4828 end if;
4829 end;
4830 end if;
4832 if No (Declarations (Bod)) then
4833 Set_Declarations (Blk, New_List);
4834 end if;
4835 end;
4836 end if;
4838 -- If this is a derived function, establish the proper return type
4840 if Present (Orig_Subp) and then Orig_Subp /= Subp then
4841 Ret_Type := Etype (Orig_Subp);
4842 else
4843 Ret_Type := Etype (Subp);
4844 end if;
4846 -- Create temporaries for the actuals that are expressions, or that are
4847 -- scalars and require copying to preserve semantics.
4849 F := First_Formal (Subp);
4850 A := First_Actual (N);
4851 while Present (F) loop
4852 if Present (Renamed_Object (F)) then
4853 Error_Msg_N ("cannot inline call to recursive subprogram", N);
4854 return;
4855 end if;
4857 -- Reset Last_Assignment for any parameters of mode out or in out, to
4858 -- prevent spurious warnings about overwriting for assignments to the
4859 -- formal in the inlined code.
4861 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
4862 Set_Last_Assignment (Entity (A), Empty);
4863 end if;
4865 -- If the argument may be a controlling argument in a call within
4866 -- the inlined body, we must preserve its classwide nature to insure
4867 -- that dynamic dispatching take place subsequently. If the formal
4868 -- has a constraint it must be preserved to retain the semantics of
4869 -- the body.
4871 if Is_Class_Wide_Type (Etype (F))
4872 or else (Is_Access_Type (Etype (F))
4873 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
4874 then
4875 Temp_Typ := Etype (F);
4877 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
4878 and then Etype (F) /= Base_Type (Etype (F))
4879 then
4880 Temp_Typ := Etype (F);
4881 else
4882 Temp_Typ := Etype (A);
4883 end if;
4885 -- If the actual is a simple name or a literal, no need to
4886 -- create a temporary, object can be used directly.
4888 -- If the actual is a literal and the formal has its address taken,
4889 -- we cannot pass the literal itself as an argument, so its value
4890 -- must be captured in a temporary.
4892 if (Is_Entity_Name (A)
4893 and then
4894 (not Is_Scalar_Type (Etype (A))
4895 or else Ekind (Entity (A)) = E_Enumeration_Literal))
4897 -- When the actual is an identifier and the corresponding formal is
4898 -- used only once in the original body, the formal can be substituted
4899 -- directly with the actual parameter.
4901 or else (Nkind (A) = N_Identifier
4902 and then Formal_Is_Used_Once (F))
4904 or else
4905 (Nkind_In (A, N_Real_Literal,
4906 N_Integer_Literal,
4907 N_Character_Literal)
4908 and then not Address_Taken (F))
4909 then
4910 if Etype (F) /= Etype (A) then
4911 Set_Renamed_Object
4912 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
4913 else
4914 Set_Renamed_Object (F, A);
4915 end if;
4917 else
4918 Temp := Make_Temporary (Loc, 'C');
4920 -- If the actual for an in/in-out parameter is a view conversion,
4921 -- make it into an unchecked conversion, given that an untagged
4922 -- type conversion is not a proper object for a renaming.
4924 -- In-out conversions that involve real conversions have already
4925 -- been transformed in Expand_Actuals.
4927 if Nkind (A) = N_Type_Conversion
4928 and then Ekind (F) /= E_In_Parameter
4929 then
4930 New_A :=
4931 Make_Unchecked_Type_Conversion (Loc,
4932 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
4933 Expression => Relocate_Node (Expression (A)));
4935 elsif Etype (F) /= Etype (A) then
4936 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
4937 Temp_Typ := Etype (F);
4939 else
4940 New_A := Relocate_Node (A);
4941 end if;
4943 Set_Sloc (New_A, Sloc (N));
4945 -- If the actual has a by-reference type, it cannot be copied,
4946 -- so its value is captured in a renaming declaration. Otherwise
4947 -- declare a local constant initialized with the actual.
4949 -- We also use a renaming declaration for expressions of an array
4950 -- type that is not bit-packed, both for efficiency reasons and to
4951 -- respect the semantics of the call: in most cases the original
4952 -- call will pass the parameter by reference, and thus the inlined
4953 -- code will have the same semantics.
4955 if Ekind (F) = E_In_Parameter
4956 and then not Is_By_Reference_Type (Etype (A))
4957 and then
4958 (not Is_Array_Type (Etype (A))
4959 or else not Is_Object_Reference (A)
4960 or else Is_Bit_Packed_Array (Etype (A)))
4961 then
4962 Decl :=
4963 Make_Object_Declaration (Loc,
4964 Defining_Identifier => Temp,
4965 Constant_Present => True,
4966 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
4967 Expression => New_A);
4968 else
4969 Decl :=
4970 Make_Object_Renaming_Declaration (Loc,
4971 Defining_Identifier => Temp,
4972 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
4973 Name => New_A);
4974 end if;
4976 Append (Decl, Decls);
4977 Set_Renamed_Object (F, Temp);
4978 end if;
4980 Next_Formal (F);
4981 Next_Actual (A);
4982 end loop;
4984 -- Establish target of function call. If context is not assignment or
4985 -- declaration, create a temporary as a target. The declaration for the
4986 -- temporary may be subsequently optimized away if the body is a single
4987 -- expression, or if the left-hand side of the assignment is simple
4988 -- enough, i.e. an entity or an explicit dereference of one.
4990 if Ekind (Subp) = E_Function then
4991 if Nkind (Parent (N)) = N_Assignment_Statement
4992 and then Is_Entity_Name (Name (Parent (N)))
4993 then
4994 Targ := Name (Parent (N));
4996 elsif Nkind (Parent (N)) = N_Assignment_Statement
4997 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4998 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4999 then
5000 Targ := Name (Parent (N));
5002 elsif Nkind (Parent (N)) = N_Assignment_Statement
5003 and then Nkind (Name (Parent (N))) = N_Selected_Component
5004 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5005 then
5006 Targ := New_Copy_Tree (Name (Parent (N)));
5008 elsif Nkind (Parent (N)) = N_Object_Declaration
5009 and then Is_Limited_Type (Etype (Subp))
5010 then
5011 Targ := Defining_Identifier (Parent (N));
5013 -- New semantics: In an object declaration avoid an extra copy
5014 -- of the result of a call to an inlined function that returns
5015 -- an unconstrained type
5017 elsif Debug_Flag_Dot_K
5018 and then Nkind (Parent (N)) = N_Object_Declaration
5019 and then Is_Unc
5020 then
5021 Targ := Defining_Identifier (Parent (N));
5023 else
5024 -- Replace call with temporary and create its declaration
5026 Temp := Make_Temporary (Loc, 'C');
5027 Set_Is_Internal (Temp);
5029 -- For the unconstrained case, the generated temporary has the
5030 -- same constrained declaration as the result variable. It may
5031 -- eventually be possible to remove that temporary and use the
5032 -- result variable directly.
5034 if Is_Unc
5035 and then Nkind (Parent (N)) /= N_Assignment_Statement
5036 then
5037 Decl :=
5038 Make_Object_Declaration (Loc,
5039 Defining_Identifier => Temp,
5040 Object_Definition =>
5041 New_Copy_Tree (Object_Definition (Parent (Targ1))));
5043 Replace_Formals (Decl);
5045 else
5046 Decl :=
5047 Make_Object_Declaration (Loc,
5048 Defining_Identifier => Temp,
5049 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
5051 Set_Etype (Temp, Ret_Type);
5052 end if;
5054 Set_No_Initialization (Decl);
5055 Append (Decl, Decls);
5056 Rewrite (N, New_Occurrence_Of (Temp, Loc));
5057 Targ := Temp;
5058 end if;
5059 end if;
5061 Insert_Actions (N, Decls);
5063 if Is_Unc_Decl then
5065 -- Special management for inlining a call to a function that returns
5066 -- an unconstrained type and initializes an object declaration: we
5067 -- avoid generating undesired extra calls and goto statements.
5069 -- Given:
5070 -- function Func (...) return ...
5071 -- begin
5072 -- declare
5073 -- Result : String (1 .. 4);
5074 -- begin
5075 -- Proc (Result, ...);
5076 -- return Result;
5077 -- end;
5078 -- end F;
5080 -- Result : String := Func (...);
5082 -- Replace this object declaration by:
5084 -- Result : String (1 .. 4);
5085 -- Proc (Result, ...);
5087 Remove_Homonym (Targ);
5089 Decl :=
5090 Make_Object_Declaration
5091 (Loc,
5092 Defining_Identifier => Targ,
5093 Object_Definition =>
5094 New_Copy_Tree (Object_Definition (Parent (Targ1))));
5095 Replace_Formals (Decl);
5096 Rewrite (Parent (N), Decl);
5097 Analyze (Parent (N));
5099 -- Avoid spurious warnings since we know that this declaration is
5100 -- referenced by the procedure call.
5102 Set_Never_Set_In_Source (Targ, False);
5104 -- Remove the local declaration of the extended return stmt from the
5105 -- inlined code
5107 Remove (Parent (Targ1));
5109 -- Update the reference to the result (since we have rewriten the
5110 -- object declaration)
5112 declare
5113 Blk_Call_Stmt : Node_Id;
5115 begin
5116 -- Capture the call to the procedure
5118 Blk_Call_Stmt :=
5119 First (Statements (Handled_Statement_Sequence (Blk)));
5120 pragma Assert
5121 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
5123 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
5124 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
5125 New_Reference_To (Targ, Loc));
5126 end;
5128 -- Remove the return statement
5130 pragma Assert
5131 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
5132 N_Simple_Return_Statement);
5134 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
5135 end if;
5137 -- Traverse the tree and replace formals with actuals or their thunks.
5138 -- Attach block to tree before analysis and rewriting.
5140 Replace_Formals (Blk);
5141 Set_Parent (Blk, N);
5143 if not Comes_From_Source (Subp) or else Is_Predef then
5144 Reset_Slocs (Blk);
5145 end if;
5147 if Is_Unc_Decl then
5149 -- No action needed since return statement has been already removed!
5151 null;
5153 elsif Present (Exit_Lab) then
5155 -- If the body was a single expression, the single return statement
5156 -- and the corresponding label are useless.
5158 if Num_Ret = 1
5159 and then
5160 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
5161 N_Goto_Statement
5162 then
5163 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
5164 else
5165 Append (Lab_Decl, (Declarations (Blk)));
5166 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
5167 end if;
5168 end if;
5170 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
5171 -- on conflicting private views that Gigi would ignore. If this is a
5172 -- predefined unit, analyze with checks off, as is done in the non-
5173 -- inlined run-time units.
5175 declare
5176 I_Flag : constant Boolean := In_Inlined_Body;
5178 begin
5179 In_Inlined_Body := True;
5181 if Is_Predef then
5182 declare
5183 Style : constant Boolean := Style_Check;
5185 begin
5186 Style_Check := False;
5188 -- Search for dispatching calls that use the Object.Operation
5189 -- notation using an Object that is a parameter of the inlined
5190 -- function. We reset the decoration of Operation to force
5191 -- the reanalysis of the inlined dispatching call because
5192 -- the actual object has been inlined.
5194 Reset_Dispatching_Calls (Blk);
5196 Analyze (Blk, Suppress => All_Checks);
5197 Style_Check := Style;
5198 end;
5200 else
5201 Analyze (Blk);
5202 end if;
5204 In_Inlined_Body := I_Flag;
5205 end;
5207 if Ekind (Subp) = E_Procedure then
5208 Rewrite_Procedure_Call (N, Blk);
5210 else
5211 Rewrite_Function_Call (N, Blk);
5213 if Is_Unc_Decl then
5214 null;
5216 -- For the unconstrained case, the replacement of the call has been
5217 -- made prior to the complete analysis of the generated declarations.
5218 -- Propagate the proper type now.
5220 elsif Is_Unc then
5221 if Nkind (N) = N_Identifier then
5222 Set_Etype (N, Etype (Entity (N)));
5223 else
5224 Set_Etype (N, Etype (Targ1));
5225 end if;
5226 end if;
5227 end if;
5229 Restore_Env;
5231 -- Cleanup mapping between formals and actuals for other expansions
5233 F := First_Formal (Subp);
5234 while Present (F) loop
5235 Set_Renamed_Object (F, Empty);
5236 Next_Formal (F);
5237 end loop;
5238 end Expand_Inlined_Call;
5240 ----------------------------------------
5241 -- Expand_N_Extended_Return_Statement --
5242 ----------------------------------------
5244 -- If there is a Handled_Statement_Sequence, we rewrite this:
5246 -- return Result : T := <expression> do
5247 -- <handled_seq_of_stms>
5248 -- end return;
5250 -- to be:
5252 -- declare
5253 -- Result : T := <expression>;
5254 -- begin
5255 -- <handled_seq_of_stms>
5256 -- return Result;
5257 -- end;
5259 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5261 -- return Result : T := <expression>;
5263 -- to be:
5265 -- return <expression>;
5267 -- unless it's build-in-place or there's no <expression>, in which case
5268 -- we generate:
5270 -- declare
5271 -- Result : T := <expression>;
5272 -- begin
5273 -- return Result;
5274 -- end;
5276 -- Note that this case could have been written by the user as an extended
5277 -- return statement, or could have been transformed to this from a simple
5278 -- return statement.
5280 -- That is, we need to have a reified return object if there are statements
5281 -- (which might refer to it) or if we're doing build-in-place (so we can
5282 -- set its address to the final resting place or if there is no expression
5283 -- (in which case default initial values might need to be set).
5285 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
5286 Loc : constant Source_Ptr := Sloc (N);
5288 Par_Func : constant Entity_Id :=
5289 Return_Applies_To (Return_Statement_Entity (N));
5290 Result_Subt : constant Entity_Id := Etype (Par_Func);
5291 Ret_Obj_Id : constant Entity_Id :=
5292 First_Entity (Return_Statement_Entity (N));
5293 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
5295 Is_Build_In_Place : constant Boolean :=
5296 Is_Build_In_Place_Function (Par_Func);
5298 Exp : Node_Id;
5299 HSS : Node_Id;
5300 Result : Node_Id;
5301 Return_Stmt : Node_Id;
5302 Stmts : List_Id;
5304 function Build_Heap_Allocator
5305 (Temp_Id : Entity_Id;
5306 Temp_Typ : Entity_Id;
5307 Func_Id : Entity_Id;
5308 Ret_Typ : Entity_Id;
5309 Alloc_Expr : Node_Id) return Node_Id;
5310 -- Create the statements necessary to allocate a return object on the
5311 -- caller's master. The master is available through implicit parameter
5312 -- BIPfinalizationmaster.
5314 -- if BIPfinalizationmaster /= null then
5315 -- declare
5316 -- type Ptr_Typ is access Ret_Typ;
5317 -- for Ptr_Typ'Storage_Pool use
5318 -- Base_Pool (BIPfinalizationmaster.all).all;
5319 -- Local : Ptr_Typ;
5321 -- begin
5322 -- procedure Allocate (...) is
5323 -- begin
5324 -- System.Storage_Pools.Subpools.Allocate_Any (...);
5325 -- end Allocate;
5327 -- Local := <Alloc_Expr>;
5328 -- Temp_Id := Temp_Typ (Local);
5329 -- end;
5330 -- end if;
5332 -- Temp_Id is the temporary which is used to reference the internally
5333 -- created object in all allocation forms. Temp_Typ is the type of the
5334 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
5335 -- type of Func_Id. Alloc_Expr is the actual allocator.
5337 function Move_Activation_Chain return Node_Id;
5338 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5339 -- with parameters:
5340 -- From current activation chain
5341 -- To activation chain passed in by the caller
5342 -- New_Master master passed in by the caller
5344 --------------------------
5345 -- Build_Heap_Allocator --
5346 --------------------------
5348 function Build_Heap_Allocator
5349 (Temp_Id : Entity_Id;
5350 Temp_Typ : Entity_Id;
5351 Func_Id : Entity_Id;
5352 Ret_Typ : Entity_Id;
5353 Alloc_Expr : Node_Id) return Node_Id
5355 begin
5356 pragma Assert (Is_Build_In_Place_Function (Func_Id));
5358 -- Processing for build-in-place object allocation. This is disabled
5359 -- on .NET/JVM because the targets do not support pools.
5361 if VM_Target = No_VM
5362 and then Needs_Finalization (Ret_Typ)
5363 then
5364 declare
5365 Decls : constant List_Id := New_List;
5366 Fin_Mas_Id : constant Entity_Id :=
5367 Build_In_Place_Formal
5368 (Func_Id, BIP_Finalization_Master);
5369 Stmts : constant List_Id := New_List;
5370 Desig_Typ : Entity_Id;
5371 Local_Id : Entity_Id;
5372 Pool_Id : Entity_Id;
5373 Ptr_Typ : Entity_Id;
5375 begin
5376 -- Generate:
5377 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
5379 Pool_Id := Make_Temporary (Loc, 'P');
5381 Append_To (Decls,
5382 Make_Object_Renaming_Declaration (Loc,
5383 Defining_Identifier => Pool_Id,
5384 Subtype_Mark =>
5385 New_Reference_To (RTE (RE_Root_Storage_Pool), Loc),
5386 Name =>
5387 Make_Explicit_Dereference (Loc,
5388 Prefix =>
5389 Make_Function_Call (Loc,
5390 Name =>
5391 New_Reference_To (RTE (RE_Base_Pool), Loc),
5392 Parameter_Associations => New_List (
5393 Make_Explicit_Dereference (Loc,
5394 Prefix =>
5395 New_Reference_To (Fin_Mas_Id, Loc)))))));
5397 -- Create an access type which uses the storage pool of the
5398 -- caller's master. This additional type is necessary because
5399 -- the finalization master cannot be associated with the type
5400 -- of the temporary. Otherwise the secondary stack allocation
5401 -- will fail.
5403 Desig_Typ := Ret_Typ;
5405 -- Ensure that the build-in-place machinery uses a fat pointer
5406 -- when allocating an unconstrained array on the heap. In this
5407 -- case the result object type is a constrained array type even
5408 -- though the function type is unconstrained.
5410 if Ekind (Desig_Typ) = E_Array_Subtype then
5411 Desig_Typ := Base_Type (Desig_Typ);
5412 end if;
5414 -- Generate:
5415 -- type Ptr_Typ is access Desig_Typ;
5417 Ptr_Typ := Make_Temporary (Loc, 'P');
5419 Append_To (Decls,
5420 Make_Full_Type_Declaration (Loc,
5421 Defining_Identifier => Ptr_Typ,
5422 Type_Definition =>
5423 Make_Access_To_Object_Definition (Loc,
5424 Subtype_Indication =>
5425 New_Reference_To (Desig_Typ, Loc))));
5427 -- Perform minor decoration in order to set the master and the
5428 -- storage pool attributes.
5430 Set_Ekind (Ptr_Typ, E_Access_Type);
5431 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
5432 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
5434 -- Create the temporary, generate:
5435 -- Local_Id : Ptr_Typ;
5437 Local_Id := Make_Temporary (Loc, 'T');
5439 Append_To (Decls,
5440 Make_Object_Declaration (Loc,
5441 Defining_Identifier => Local_Id,
5442 Object_Definition =>
5443 New_Reference_To (Ptr_Typ, Loc)));
5445 -- Allocate the object, generate:
5446 -- Local_Id := <Alloc_Expr>;
5448 Append_To (Stmts,
5449 Make_Assignment_Statement (Loc,
5450 Name => New_Reference_To (Local_Id, Loc),
5451 Expression => Alloc_Expr));
5453 -- Generate:
5454 -- Temp_Id := Temp_Typ (Local_Id);
5456 Append_To (Stmts,
5457 Make_Assignment_Statement (Loc,
5458 Name => New_Reference_To (Temp_Id, Loc),
5459 Expression =>
5460 Unchecked_Convert_To (Temp_Typ,
5461 New_Reference_To (Local_Id, Loc))));
5463 -- Wrap the allocation in a block. This is further conditioned
5464 -- by checking the caller finalization master at runtime. A
5465 -- null value indicates a non-existent master, most likely due
5466 -- to a Finalize_Storage_Only allocation.
5468 -- Generate:
5469 -- if BIPfinalizationmaster /= null then
5470 -- declare
5471 -- <Decls>
5472 -- begin
5473 -- <Stmts>
5474 -- end;
5475 -- end if;
5477 return
5478 Make_If_Statement (Loc,
5479 Condition =>
5480 Make_Op_Ne (Loc,
5481 Left_Opnd => New_Reference_To (Fin_Mas_Id, Loc),
5482 Right_Opnd => Make_Null (Loc)),
5484 Then_Statements => New_List (
5485 Make_Block_Statement (Loc,
5486 Declarations => Decls,
5487 Handled_Statement_Sequence =>
5488 Make_Handled_Sequence_Of_Statements (Loc,
5489 Statements => Stmts))));
5490 end;
5492 -- For all other cases, generate:
5493 -- Temp_Id := <Alloc_Expr>;
5495 else
5496 return
5497 Make_Assignment_Statement (Loc,
5498 Name => New_Reference_To (Temp_Id, Loc),
5499 Expression => Alloc_Expr);
5500 end if;
5501 end Build_Heap_Allocator;
5503 ---------------------------
5504 -- Move_Activation_Chain --
5505 ---------------------------
5507 function Move_Activation_Chain return Node_Id is
5508 begin
5509 return
5510 Make_Procedure_Call_Statement (Loc,
5511 Name =>
5512 New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
5514 Parameter_Associations => New_List (
5516 -- Source chain
5518 Make_Attribute_Reference (Loc,
5519 Prefix => Make_Identifier (Loc, Name_uChain),
5520 Attribute_Name => Name_Unrestricted_Access),
5522 -- Destination chain
5524 New_Reference_To
5525 (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc),
5527 -- New master
5529 New_Reference_To
5530 (Build_In_Place_Formal (Par_Func, BIP_Task_Master), Loc)));
5531 end Move_Activation_Chain;
5533 -- Start of processing for Expand_N_Extended_Return_Statement
5535 begin
5536 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
5537 Exp := Expression (Ret_Obj_Decl);
5538 else
5539 Exp := Empty;
5540 end if;
5542 HSS := Handled_Statement_Sequence (N);
5544 -- If the returned object needs finalization actions, the function must
5545 -- perform the appropriate cleanup should it fail to return. The state
5546 -- of the function itself is tracked through a flag which is coupled
5547 -- with the scope finalizer. There is one flag per each return object
5548 -- in case of multiple returns.
5550 if Is_Build_In_Place
5551 and then Needs_Finalization (Etype (Ret_Obj_Id))
5552 then
5553 declare
5554 Flag_Decl : Node_Id;
5555 Flag_Id : Entity_Id;
5556 Func_Bod : Node_Id;
5558 begin
5559 -- Recover the function body
5561 Func_Bod := Unit_Declaration_Node (Par_Func);
5563 if Nkind (Func_Bod) = N_Subprogram_Declaration then
5564 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
5565 end if;
5567 -- Create a flag to track the function state
5569 Flag_Id := Make_Temporary (Loc, 'F');
5570 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
5572 -- Insert the flag at the beginning of the function declarations,
5573 -- generate:
5574 -- Fnn : Boolean := False;
5576 Flag_Decl :=
5577 Make_Object_Declaration (Loc,
5578 Defining_Identifier => Flag_Id,
5579 Object_Definition =>
5580 New_Reference_To (Standard_Boolean, Loc),
5581 Expression => New_Reference_To (Standard_False, Loc));
5583 Prepend_To (Declarations (Func_Bod), Flag_Decl);
5584 Analyze (Flag_Decl);
5585 end;
5586 end if;
5588 -- Build a simple_return_statement that returns the return object when
5589 -- there is a statement sequence, or no expression, or the result will
5590 -- be built in place. Note however that we currently do this for all
5591 -- composite cases, even though nonlimited composite results are not yet
5592 -- built in place (though we plan to do so eventually).
5594 if Present (HSS)
5595 or else Is_Composite_Type (Result_Subt)
5596 or else No (Exp)
5597 then
5598 if No (HSS) then
5599 Stmts := New_List;
5601 -- If the extended return has a handled statement sequence, then wrap
5602 -- it in a block and use the block as the first statement.
5604 else
5605 Stmts := New_List (
5606 Make_Block_Statement (Loc,
5607 Declarations => New_List,
5608 Handled_Statement_Sequence => HSS));
5609 end if;
5611 -- If the result type contains tasks, we call Move_Activation_Chain.
5612 -- Later, the cleanup code will call Complete_Master, which will
5613 -- terminate any unactivated tasks belonging to the return statement
5614 -- master. But Move_Activation_Chain updates their master to be that
5615 -- of the caller, so they will not be terminated unless the return
5616 -- statement completes unsuccessfully due to exception, abort, goto,
5617 -- or exit. As a formality, we test whether the function requires the
5618 -- result to be built in place, though that's necessarily true for
5619 -- the case of result types with task parts.
5621 if Is_Build_In_Place
5622 and then Has_Task (Result_Subt)
5623 then
5624 -- The return expression is an aggregate for a complex type which
5625 -- contains tasks. This particular case is left unexpanded since
5626 -- the regular expansion would insert all temporaries and
5627 -- initialization code in the wrong block.
5629 if Nkind (Exp) = N_Aggregate then
5630 Expand_N_Aggregate (Exp);
5631 end if;
5633 -- Do not move the activation chain if the return object does not
5634 -- contain tasks.
5636 if Has_Task (Etype (Ret_Obj_Id)) then
5637 Append_To (Stmts, Move_Activation_Chain);
5638 end if;
5639 end if;
5641 -- Update the state of the function right before the object is
5642 -- returned.
5644 if Is_Build_In_Place
5645 and then Needs_Finalization (Etype (Ret_Obj_Id))
5646 then
5647 declare
5648 Flag_Id : constant Entity_Id :=
5649 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
5651 begin
5652 -- Generate:
5653 -- Fnn := True;
5655 Append_To (Stmts,
5656 Make_Assignment_Statement (Loc,
5657 Name => New_Reference_To (Flag_Id, Loc),
5658 Expression => New_Reference_To (Standard_True, Loc)));
5659 end;
5660 end if;
5662 -- Build a simple_return_statement that returns the return object
5664 Return_Stmt :=
5665 Make_Simple_Return_Statement (Loc,
5666 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
5667 Append_To (Stmts, Return_Stmt);
5669 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
5670 end if;
5672 -- Case where we build a return statement block
5674 if Present (HSS) then
5675 Result :=
5676 Make_Block_Statement (Loc,
5677 Declarations => Return_Object_Declarations (N),
5678 Handled_Statement_Sequence => HSS);
5680 -- We set the entity of the new block statement to be that of the
5681 -- return statement. This is necessary so that various fields, such
5682 -- as Finalization_Chain_Entity carry over from the return statement
5683 -- to the block. Note that this block is unusual, in that its entity
5684 -- is an E_Return_Statement rather than an E_Block.
5686 Set_Identifier
5687 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
5689 -- If the object decl was already rewritten as a renaming, then we
5690 -- don't want to do the object allocation and transformation of of
5691 -- the return object declaration to a renaming. This case occurs
5692 -- when the return object is initialized by a call to another
5693 -- build-in-place function, and that function is responsible for
5694 -- the allocation of the return object.
5696 if Is_Build_In_Place
5697 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
5698 then
5699 pragma Assert
5700 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
5701 and then Is_Build_In_Place_Function_Call
5702 (Expression (Original_Node (Ret_Obj_Decl))));
5704 -- Return the build-in-place result by reference
5706 Set_By_Ref (Return_Stmt);
5708 elsif Is_Build_In_Place then
5710 -- Locate the implicit access parameter associated with the
5711 -- caller-supplied return object and convert the return
5712 -- statement's return object declaration to a renaming of a
5713 -- dereference of the access parameter. If the return object's
5714 -- declaration includes an expression that has not already been
5715 -- expanded as separate assignments, then add an assignment
5716 -- statement to ensure the return object gets initialized.
5718 -- declare
5719 -- Result : T [:= <expression>];
5720 -- begin
5721 -- ...
5723 -- is converted to
5725 -- declare
5726 -- Result : T renames FuncRA.all;
5727 -- [Result := <expression;]
5728 -- begin
5729 -- ...
5731 declare
5732 Return_Obj_Id : constant Entity_Id :=
5733 Defining_Identifier (Ret_Obj_Decl);
5734 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
5735 Return_Obj_Expr : constant Node_Id :=
5736 Expression (Ret_Obj_Decl);
5737 Constr_Result : constant Boolean :=
5738 Is_Constrained (Result_Subt);
5739 Obj_Alloc_Formal : Entity_Id;
5740 Object_Access : Entity_Id;
5741 Obj_Acc_Deref : Node_Id;
5742 Init_Assignment : Node_Id := Empty;
5744 begin
5745 -- Build-in-place results must be returned by reference
5747 Set_By_Ref (Return_Stmt);
5749 -- Retrieve the implicit access parameter passed by the caller
5751 Object_Access :=
5752 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
5754 -- If the return object's declaration includes an expression
5755 -- and the declaration isn't marked as No_Initialization, then
5756 -- we need to generate an assignment to the object and insert
5757 -- it after the declaration before rewriting it as a renaming
5758 -- (otherwise we'll lose the initialization). The case where
5759 -- the result type is an interface (or class-wide interface)
5760 -- is also excluded because the context of the function call
5761 -- must be unconstrained, so the initialization will always
5762 -- be done as part of an allocator evaluation (storage pool
5763 -- or secondary stack), never to a constrained target object
5764 -- passed in by the caller. Besides the assignment being
5765 -- unneeded in this case, it avoids problems with trying to
5766 -- generate a dispatching assignment when the return expression
5767 -- is a nonlimited descendant of a limited interface (the
5768 -- interface has no assignment operation).
5770 if Present (Return_Obj_Expr)
5771 and then not No_Initialization (Ret_Obj_Decl)
5772 and then not Is_Interface (Return_Obj_Typ)
5773 then
5774 Init_Assignment :=
5775 Make_Assignment_Statement (Loc,
5776 Name => New_Reference_To (Return_Obj_Id, Loc),
5777 Expression => Relocate_Node (Return_Obj_Expr));
5779 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
5780 Set_Assignment_OK (Name (Init_Assignment));
5781 Set_No_Ctrl_Actions (Init_Assignment);
5783 Set_Parent (Name (Init_Assignment), Init_Assignment);
5784 Set_Parent (Expression (Init_Assignment), Init_Assignment);
5786 Set_Expression (Ret_Obj_Decl, Empty);
5788 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
5789 and then not Is_Class_Wide_Type
5790 (Etype (Expression (Init_Assignment)))
5791 then
5792 Rewrite (Expression (Init_Assignment),
5793 Make_Type_Conversion (Loc,
5794 Subtype_Mark =>
5795 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
5796 Expression =>
5797 Relocate_Node (Expression (Init_Assignment))));
5798 end if;
5800 -- In the case of functions where the calling context can
5801 -- determine the form of allocation needed, initialization
5802 -- is done with each part of the if statement that handles
5803 -- the different forms of allocation (this is true for
5804 -- unconstrained and tagged result subtypes).
5806 if Constr_Result
5807 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
5808 then
5809 Insert_After (Ret_Obj_Decl, Init_Assignment);
5810 end if;
5811 end if;
5813 -- When the function's subtype is unconstrained, a run-time
5814 -- test is needed to determine the form of allocation to use
5815 -- for the return object. The function has an implicit formal
5816 -- parameter indicating this. If the BIP_Alloc_Form formal has
5817 -- the value one, then the caller has passed access to an
5818 -- existing object for use as the return object. If the value
5819 -- is two, then the return object must be allocated on the
5820 -- secondary stack. Otherwise, the object must be allocated in
5821 -- a storage pool (currently only supported for the global
5822 -- heap, user-defined storage pools TBD ???). We generate an
5823 -- if statement to test the implicit allocation formal and
5824 -- initialize a local access value appropriately, creating
5825 -- allocators in the secondary stack and global heap cases.
5826 -- The special formal also exists and must be tested when the
5827 -- function has a tagged result, even when the result subtype
5828 -- is constrained, because in general such functions can be
5829 -- called in dispatching contexts and must be handled similarly
5830 -- to functions with a class-wide result.
5832 if not Constr_Result
5833 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5834 then
5835 Obj_Alloc_Formal :=
5836 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
5838 declare
5839 Pool_Id : constant Entity_Id :=
5840 Make_Temporary (Loc, 'P');
5841 Alloc_Obj_Id : Entity_Id;
5842 Alloc_Obj_Decl : Node_Id;
5843 Alloc_If_Stmt : Node_Id;
5844 Heap_Allocator : Node_Id;
5845 Pool_Decl : Node_Id;
5846 Pool_Allocator : Node_Id;
5847 Ptr_Type_Decl : Node_Id;
5848 Ref_Type : Entity_Id;
5849 SS_Allocator : Node_Id;
5851 begin
5852 -- Reuse the itype created for the function's implicit
5853 -- access formal. This avoids the need to create a new
5854 -- access type here, plus it allows assigning the access
5855 -- formal directly without applying a conversion.
5857 -- Ref_Type := Etype (Object_Access);
5859 -- Create an access type designating the function's
5860 -- result subtype.
5862 Ref_Type := Make_Temporary (Loc, 'A');
5864 Ptr_Type_Decl :=
5865 Make_Full_Type_Declaration (Loc,
5866 Defining_Identifier => Ref_Type,
5867 Type_Definition =>
5868 Make_Access_To_Object_Definition (Loc,
5869 All_Present => True,
5870 Subtype_Indication =>
5871 New_Reference_To (Return_Obj_Typ, Loc)));
5873 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5875 -- Create an access object that will be initialized to an
5876 -- access value denoting the return object, either coming
5877 -- from an implicit access value passed in by the caller
5878 -- or from the result of an allocator.
5880 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5881 Set_Etype (Alloc_Obj_Id, Ref_Type);
5883 Alloc_Obj_Decl :=
5884 Make_Object_Declaration (Loc,
5885 Defining_Identifier => Alloc_Obj_Id,
5886 Object_Definition =>
5887 New_Reference_To (Ref_Type, Loc));
5889 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5891 -- Create allocators for both the secondary stack and
5892 -- global heap. If there's an initialization expression,
5893 -- then create these as initialized allocators.
5895 if Present (Return_Obj_Expr)
5896 and then not No_Initialization (Ret_Obj_Decl)
5897 then
5898 -- Always use the type of the expression for the
5899 -- qualified expression, rather than the result type.
5900 -- In general we cannot always use the result type
5901 -- for the allocator, because the expression might be
5902 -- of a specific type, such as in the case of an
5903 -- aggregate or even a nonlimited object when the
5904 -- result type is a limited class-wide interface type.
5906 Heap_Allocator :=
5907 Make_Allocator (Loc,
5908 Expression =>
5909 Make_Qualified_Expression (Loc,
5910 Subtype_Mark =>
5911 New_Reference_To
5912 (Etype (Return_Obj_Expr), Loc),
5913 Expression =>
5914 New_Copy_Tree (Return_Obj_Expr)));
5916 else
5917 -- If the function returns a class-wide type we cannot
5918 -- use the return type for the allocator. Instead we
5919 -- use the type of the expression, which must be an
5920 -- aggregate of a definite type.
5922 if Is_Class_Wide_Type (Return_Obj_Typ) then
5923 Heap_Allocator :=
5924 Make_Allocator (Loc,
5925 Expression =>
5926 New_Reference_To
5927 (Etype (Return_Obj_Expr), Loc));
5928 else
5929 Heap_Allocator :=
5930 Make_Allocator (Loc,
5931 Expression =>
5932 New_Reference_To (Return_Obj_Typ, Loc));
5933 end if;
5935 -- If the object requires default initialization then
5936 -- that will happen later following the elaboration of
5937 -- the object renaming. If we don't turn it off here
5938 -- then the object will be default initialized twice.
5940 Set_No_Initialization (Heap_Allocator);
5941 end if;
5943 -- The Pool_Allocator is just like the Heap_Allocator,
5944 -- except we set Storage_Pool and Procedure_To_Call so
5945 -- it will use the user-defined storage pool.
5947 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
5949 -- Do not generate the renaming of the build-in-place
5950 -- pool parameter on .NET/JVM/ZFP because the parameter
5951 -- is not created in the first place.
5953 if VM_Target = No_VM
5954 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
5955 then
5956 Pool_Decl :=
5957 Make_Object_Renaming_Declaration (Loc,
5958 Defining_Identifier => Pool_Id,
5959 Subtype_Mark =>
5960 New_Reference_To
5961 (RTE (RE_Root_Storage_Pool), Loc),
5962 Name =>
5963 Make_Explicit_Dereference (Loc,
5964 New_Reference_To
5965 (Build_In_Place_Formal
5966 (Par_Func, BIP_Storage_Pool), Loc)));
5967 Set_Storage_Pool (Pool_Allocator, Pool_Id);
5968 Set_Procedure_To_Call
5969 (Pool_Allocator, RTE (RE_Allocate_Any));
5970 else
5971 Pool_Decl := Make_Null_Statement (Loc);
5972 end if;
5974 -- If the No_Allocators restriction is active, then only
5975 -- an allocator for secondary stack allocation is needed.
5976 -- It's OK for such allocators to have Comes_From_Source
5977 -- set to False, because gigi knows not to flag them as
5978 -- being a violation of No_Implicit_Heap_Allocations.
5980 if Restriction_Active (No_Allocators) then
5981 SS_Allocator := Heap_Allocator;
5982 Heap_Allocator := Make_Null (Loc);
5983 Pool_Allocator := Make_Null (Loc);
5985 -- Otherwise the heap and pool allocators may be needed,
5986 -- so we make another allocator for secondary stack
5987 -- allocation.
5989 else
5990 SS_Allocator := New_Copy_Tree (Heap_Allocator);
5992 -- The heap and pool allocators are marked as
5993 -- Comes_From_Source since they correspond to an
5994 -- explicit user-written allocator (that is, it will
5995 -- only be executed on behalf of callers that call the
5996 -- function as initialization for such an allocator).
5997 -- Prevents errors when No_Implicit_Heap_Allocations
5998 -- is in force.
6000 Set_Comes_From_Source (Heap_Allocator, True);
6001 Set_Comes_From_Source (Pool_Allocator, True);
6002 end if;
6004 -- The allocator is returned on the secondary stack. We
6005 -- don't do this on VM targets, since the SS is not used.
6007 if VM_Target = No_VM then
6008 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
6009 Set_Procedure_To_Call
6010 (SS_Allocator, RTE (RE_SS_Allocate));
6012 -- The allocator is returned on the secondary stack,
6013 -- so indicate that the function return, as well as
6014 -- the block that encloses the allocator, must not
6015 -- release it. The flags must be set now because
6016 -- the decision to use the secondary stack is done
6017 -- very late in the course of expanding the return
6018 -- statement, past the point where these flags are
6019 -- normally set.
6021 Set_Sec_Stack_Needed_For_Return (Par_Func);
6022 Set_Sec_Stack_Needed_For_Return
6023 (Return_Statement_Entity (N));
6024 Set_Uses_Sec_Stack (Par_Func);
6025 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
6026 end if;
6028 -- Create an if statement to test the BIP_Alloc_Form
6029 -- formal and initialize the access object to either the
6030 -- BIP_Object_Access formal (BIP_Alloc_Form =
6031 -- Caller_Allocation), the result of allocating the
6032 -- object in the secondary stack (BIP_Alloc_Form =
6033 -- Secondary_Stack), or else an allocator to create the
6034 -- return object in the heap or user-defined pool
6035 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
6037 -- ??? An unchecked type conversion must be made in the
6038 -- case of assigning the access object formal to the
6039 -- local access object, because a normal conversion would
6040 -- be illegal in some cases (such as converting access-
6041 -- to-unconstrained to access-to-constrained), but the
6042 -- the unchecked conversion will presumably fail to work
6043 -- right in just such cases. It's not clear at all how to
6044 -- handle this. ???
6046 Alloc_If_Stmt :=
6047 Make_If_Statement (Loc,
6048 Condition =>
6049 Make_Op_Eq (Loc,
6050 Left_Opnd =>
6051 New_Reference_To (Obj_Alloc_Formal, Loc),
6052 Right_Opnd =>
6053 Make_Integer_Literal (Loc,
6054 UI_From_Int (BIP_Allocation_Form'Pos
6055 (Caller_Allocation)))),
6057 Then_Statements => New_List (
6058 Make_Assignment_Statement (Loc,
6059 Name =>
6060 New_Reference_To (Alloc_Obj_Id, Loc),
6061 Expression =>
6062 Make_Unchecked_Type_Conversion (Loc,
6063 Subtype_Mark =>
6064 New_Reference_To (Ref_Type, Loc),
6065 Expression =>
6066 New_Reference_To (Object_Access, Loc)))),
6068 Elsif_Parts => New_List (
6069 Make_Elsif_Part (Loc,
6070 Condition =>
6071 Make_Op_Eq (Loc,
6072 Left_Opnd =>
6073 New_Reference_To (Obj_Alloc_Formal, Loc),
6074 Right_Opnd =>
6075 Make_Integer_Literal (Loc,
6076 UI_From_Int (BIP_Allocation_Form'Pos
6077 (Secondary_Stack)))),
6079 Then_Statements => New_List (
6080 Make_Assignment_Statement (Loc,
6081 Name =>
6082 New_Reference_To (Alloc_Obj_Id, Loc),
6083 Expression => SS_Allocator))),
6085 Make_Elsif_Part (Loc,
6086 Condition =>
6087 Make_Op_Eq (Loc,
6088 Left_Opnd =>
6089 New_Reference_To (Obj_Alloc_Formal, Loc),
6090 Right_Opnd =>
6091 Make_Integer_Literal (Loc,
6092 UI_From_Int (BIP_Allocation_Form'Pos
6093 (Global_Heap)))),
6095 Then_Statements => New_List (
6096 Build_Heap_Allocator
6097 (Temp_Id => Alloc_Obj_Id,
6098 Temp_Typ => Ref_Type,
6099 Func_Id => Par_Func,
6100 Ret_Typ => Return_Obj_Typ,
6101 Alloc_Expr => Heap_Allocator)))),
6103 Else_Statements => New_List (
6104 Pool_Decl,
6105 Build_Heap_Allocator
6106 (Temp_Id => Alloc_Obj_Id,
6107 Temp_Typ => Ref_Type,
6108 Func_Id => Par_Func,
6109 Ret_Typ => Return_Obj_Typ,
6110 Alloc_Expr => Pool_Allocator)));
6112 -- If a separate initialization assignment was created
6113 -- earlier, append that following the assignment of the
6114 -- implicit access formal to the access object, to ensure
6115 -- that the return object is initialized in that case. In
6116 -- this situation, the target of the assignment must be
6117 -- rewritten to denote a dereference of the access to the
6118 -- return object passed in by the caller.
6120 if Present (Init_Assignment) then
6121 Rewrite (Name (Init_Assignment),
6122 Make_Explicit_Dereference (Loc,
6123 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
6125 Set_Etype
6126 (Name (Init_Assignment), Etype (Return_Obj_Id));
6128 Append_To
6129 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
6130 end if;
6132 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
6134 -- Remember the local access object for use in the
6135 -- dereference of the renaming created below.
6137 Object_Access := Alloc_Obj_Id;
6138 end;
6139 end if;
6141 -- Replace the return object declaration with a renaming of a
6142 -- dereference of the access value designating the return
6143 -- object.
6145 Obj_Acc_Deref :=
6146 Make_Explicit_Dereference (Loc,
6147 Prefix => New_Reference_To (Object_Access, Loc));
6149 Rewrite (Ret_Obj_Decl,
6150 Make_Object_Renaming_Declaration (Loc,
6151 Defining_Identifier => Return_Obj_Id,
6152 Access_Definition => Empty,
6153 Subtype_Mark =>
6154 New_Occurrence_Of (Return_Obj_Typ, Loc),
6155 Name => Obj_Acc_Deref));
6157 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
6158 end;
6159 end if;
6161 -- Case where we do not build a block
6163 else
6164 -- We're about to drop Return_Object_Declarations on the floor, so
6165 -- we need to insert it, in case it got expanded into useful code.
6166 -- Remove side effects from expression, which may be duplicated in
6167 -- subsequent checks (see Expand_Simple_Function_Return).
6169 Insert_List_Before (N, Return_Object_Declarations (N));
6170 Remove_Side_Effects (Exp);
6172 -- Build simple_return_statement that returns the expression directly
6174 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
6175 Result := Return_Stmt;
6176 end if;
6178 -- Set the flag to prevent infinite recursion
6180 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
6182 Rewrite (N, Result);
6183 Analyze (N);
6184 end Expand_N_Extended_Return_Statement;
6186 ----------------------------
6187 -- Expand_N_Function_Call --
6188 ----------------------------
6190 procedure Expand_N_Function_Call (N : Node_Id) is
6191 begin
6192 Expand_Call (N);
6194 -- If the return value of a foreign compiled function is VAX Float, then
6195 -- expand the return (adjusts the location of the return value on
6196 -- Alpha/VMS, no-op everywhere else).
6197 -- Comes_From_Source intercepts recursive expansion.
6199 if Nkind (N) = N_Function_Call
6200 and then Vax_Float (Etype (N))
6201 and then Present (Name (N))
6202 and then Present (Entity (Name (N)))
6203 and then Has_Foreign_Convention (Entity (Name (N)))
6204 and then Comes_From_Source (Parent (N))
6205 then
6206 Expand_Vax_Foreign_Return (N);
6207 end if;
6208 end Expand_N_Function_Call;
6210 ---------------------------------------
6211 -- Expand_N_Procedure_Call_Statement --
6212 ---------------------------------------
6214 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
6215 begin
6216 Expand_Call (N);
6217 end Expand_N_Procedure_Call_Statement;
6219 --------------------------------------
6220 -- Expand_N_Simple_Return_Statement --
6221 --------------------------------------
6223 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
6224 begin
6225 -- Defend against previous errors (i.e. the return statement calls a
6226 -- function that is not available in configurable runtime).
6228 if Present (Expression (N))
6229 and then Nkind (Expression (N)) = N_Empty
6230 then
6231 Check_Error_Detected;
6232 return;
6233 end if;
6235 -- Distinguish the function and non-function cases:
6237 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
6239 when E_Function |
6240 E_Generic_Function =>
6241 Expand_Simple_Function_Return (N);
6243 when E_Procedure |
6244 E_Generic_Procedure |
6245 E_Entry |
6246 E_Entry_Family |
6247 E_Return_Statement =>
6248 Expand_Non_Function_Return (N);
6250 when others =>
6251 raise Program_Error;
6252 end case;
6254 exception
6255 when RE_Not_Available =>
6256 return;
6257 end Expand_N_Simple_Return_Statement;
6259 ------------------------------
6260 -- Expand_N_Subprogram_Body --
6261 ------------------------------
6263 -- Add poll call if ATC polling is enabled, unless the body will be inlined
6264 -- by the back-end.
6266 -- Add dummy push/pop label nodes at start and end to clear any local
6267 -- exception indications if local-exception-to-goto optimization is active.
6269 -- Add return statement if last statement in body is not a return statement
6270 -- (this makes things easier on Gigi which does not want to have to handle
6271 -- a missing return).
6273 -- Add call to Activate_Tasks if body is a task activator
6275 -- Deal with possible detection of infinite recursion
6277 -- Eliminate body completely if convention stubbed
6279 -- Encode entity names within body, since we will not need to reference
6280 -- these entities any longer in the front end.
6282 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
6284 -- Reset Pure indication if any parameter has root type System.Address
6285 -- or has any parameters of limited types, where limited means that the
6286 -- run-time view is limited (i.e. the full type is limited).
6288 -- Wrap thread body
6290 procedure Expand_N_Subprogram_Body (N : Node_Id) is
6291 Loc : constant Source_Ptr := Sloc (N);
6292 H : constant Node_Id := Handled_Statement_Sequence (N);
6293 Body_Id : Entity_Id;
6294 Except_H : Node_Id;
6295 L : List_Id;
6296 Spec_Id : Entity_Id;
6298 procedure Add_Return (S : List_Id);
6299 -- Append a return statement to the statement sequence S if the last
6300 -- statement is not already a return or a goto statement. Note that
6301 -- the latter test is not critical, it does not matter if we add a few
6302 -- extra returns, since they get eliminated anyway later on.
6304 ----------------
6305 -- Add_Return --
6306 ----------------
6308 procedure Add_Return (S : List_Id) is
6309 Last_Stm : Node_Id;
6310 Loc : Source_Ptr;
6312 begin
6313 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
6314 -- not relevant in this context since they are not executable.
6316 Last_Stm := Last (S);
6317 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
6318 Prev (Last_Stm);
6319 end loop;
6321 -- Now insert return unless last statement is a transfer
6323 if not Is_Transfer (Last_Stm) then
6325 -- The source location for the return is the end label of the
6326 -- procedure if present. Otherwise use the sloc of the last
6327 -- statement in the list. If the list comes from a generated
6328 -- exception handler and we are not debugging generated code,
6329 -- all the statements within the handler are made invisible
6330 -- to the debugger.
6332 if Nkind (Parent (S)) = N_Exception_Handler
6333 and then not Comes_From_Source (Parent (S))
6334 then
6335 Loc := Sloc (Last_Stm);
6336 elsif Present (End_Label (H)) then
6337 Loc := Sloc (End_Label (H));
6338 else
6339 Loc := Sloc (Last_Stm);
6340 end if;
6342 declare
6343 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
6345 begin
6346 -- Append return statement, and set analyzed manually. We can't
6347 -- call Analyze on this return since the scope is wrong.
6349 -- Note: it almost works to push the scope and then do the
6350 -- Analyze call, but something goes wrong in some weird cases
6351 -- and it is not worth worrying about ???
6353 Append_To (S, Rtn);
6354 Set_Analyzed (Rtn);
6356 -- Call _Postconditions procedure if appropriate. We need to
6357 -- do this explicitly because we did not analyze the generated
6358 -- return statement above, so the call did not get inserted.
6360 if Ekind (Spec_Id) = E_Procedure
6361 and then Has_Postconditions (Spec_Id)
6362 then
6363 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
6364 Insert_Action (Rtn,
6365 Make_Procedure_Call_Statement (Loc,
6366 Name =>
6367 New_Reference_To (Postcondition_Proc (Spec_Id), Loc)));
6368 end if;
6369 end;
6370 end if;
6371 end Add_Return;
6373 -- Start of processing for Expand_N_Subprogram_Body
6375 begin
6376 -- Set L to either the list of declarations if present, or to the list
6377 -- of statements if no declarations are present. This is used to insert
6378 -- new stuff at the start.
6380 if Is_Non_Empty_List (Declarations (N)) then
6381 L := Declarations (N);
6382 else
6383 L := Statements (H);
6384 end if;
6386 -- If local-exception-to-goto optimization active, insert dummy push
6387 -- statements at start, and dummy pop statements at end, but inhibit
6388 -- this if we have No_Exception_Handlers, since they are useless and
6389 -- intefere with analysis, e.g. by codepeer.
6391 if (Debug_Flag_Dot_G
6392 or else Restriction_Active (No_Exception_Propagation))
6393 and then not Restriction_Active (No_Exception_Handlers)
6394 and then not CodePeer_Mode
6395 and then Is_Non_Empty_List (L)
6396 then
6397 declare
6398 FS : constant Node_Id := First (L);
6399 FL : constant Source_Ptr := Sloc (FS);
6400 LS : Node_Id;
6401 LL : Source_Ptr;
6403 begin
6404 -- LS points to either last statement, if statements are present
6405 -- or to the last declaration if there are no statements present.
6406 -- It is the node after which the pop's are generated.
6408 if Is_Non_Empty_List (Statements (H)) then
6409 LS := Last (Statements (H));
6410 else
6411 LS := Last (L);
6412 end if;
6414 LL := Sloc (LS);
6416 Insert_List_Before_And_Analyze (FS, New_List (
6417 Make_Push_Constraint_Error_Label (FL),
6418 Make_Push_Program_Error_Label (FL),
6419 Make_Push_Storage_Error_Label (FL)));
6421 Insert_List_After_And_Analyze (LS, New_List (
6422 Make_Pop_Constraint_Error_Label (LL),
6423 Make_Pop_Program_Error_Label (LL),
6424 Make_Pop_Storage_Error_Label (LL)));
6425 end;
6426 end if;
6428 -- Find entity for subprogram
6430 Body_Id := Defining_Entity (N);
6432 if Present (Corresponding_Spec (N)) then
6433 Spec_Id := Corresponding_Spec (N);
6434 else
6435 Spec_Id := Body_Id;
6436 end if;
6438 -- Need poll on entry to subprogram if polling enabled. We only do this
6439 -- for non-empty subprograms, since it does not seem necessary to poll
6440 -- for a dummy null subprogram.
6442 if Is_Non_Empty_List (L) then
6444 -- Do not add a polling call if the subprogram is to be inlined by
6445 -- the back-end, to avoid repeated calls with multiple inlinings.
6447 if Is_Inlined (Spec_Id)
6448 and then Front_End_Inlining
6449 and then Optimization_Level > 1
6450 then
6451 null;
6452 else
6453 Generate_Poll_Call (First (L));
6454 end if;
6455 end if;
6457 -- If this is a Pure function which has any parameters whose root type
6458 -- is System.Address, reset the Pure indication, since it will likely
6459 -- cause incorrect code to be generated as the parameter is probably
6460 -- a pointer, and the fact that the same pointer is passed does not mean
6461 -- that the same value is being referenced.
6463 -- Note that if the programmer gave an explicit Pure_Function pragma,
6464 -- then we believe the programmer, and leave the subprogram Pure.
6466 -- This code should probably be at the freeze point, so that it happens
6467 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
6468 -- semantic tree has Is_Pure set properly ???
6470 if Is_Pure (Spec_Id)
6471 and then Is_Subprogram (Spec_Id)
6472 and then not Has_Pragma_Pure_Function (Spec_Id)
6473 then
6474 declare
6475 F : Entity_Id;
6477 begin
6478 F := First_Formal (Spec_Id);
6479 while Present (F) loop
6480 if Is_Descendent_Of_Address (Etype (F))
6482 -- Note that this test is being made in the body of the
6483 -- subprogram, not the spec, so we are testing the full
6484 -- type for being limited here, as required.
6486 or else Is_Limited_Type (Etype (F))
6487 then
6488 Set_Is_Pure (Spec_Id, False);
6490 if Spec_Id /= Body_Id then
6491 Set_Is_Pure (Body_Id, False);
6492 end if;
6494 exit;
6495 end if;
6497 Next_Formal (F);
6498 end loop;
6499 end;
6500 end if;
6502 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
6504 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
6505 declare
6506 F : Entity_Id;
6508 begin
6509 -- Loop through formals
6511 F := First_Formal (Spec_Id);
6512 while Present (F) loop
6513 if Is_Scalar_Type (Etype (F))
6514 and then Ekind (F) = E_Out_Parameter
6515 then
6516 Check_Restriction (No_Default_Initialization, F);
6518 -- Insert the initialization. We turn off validity checks
6519 -- for this assignment, since we do not want any check on
6520 -- the initial value itself (which may well be invalid).
6522 Insert_Before_And_Analyze (First (L),
6523 Make_Assignment_Statement (Loc,
6524 Name => New_Occurrence_Of (F, Loc),
6525 Expression => Get_Simple_Init_Val (Etype (F), N)),
6526 Suppress => Validity_Check);
6527 end if;
6529 Next_Formal (F);
6530 end loop;
6531 end;
6532 end if;
6534 -- Clear out statement list for stubbed procedure
6536 if Present (Corresponding_Spec (N)) then
6537 Set_Elaboration_Flag (N, Spec_Id);
6539 if Convention (Spec_Id) = Convention_Stubbed
6540 or else Is_Eliminated (Spec_Id)
6541 then
6542 Set_Declarations (N, Empty_List);
6543 Set_Handled_Statement_Sequence (N,
6544 Make_Handled_Sequence_Of_Statements (Loc,
6545 Statements => New_List (Make_Null_Statement (Loc))));
6546 return;
6547 end if;
6548 end if;
6550 -- Create a set of discriminals for the next protected subprogram body
6552 if Is_List_Member (N)
6553 and then Present (Parent (List_Containing (N)))
6554 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6555 and then Present (Next_Protected_Operation (N))
6556 then
6557 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
6558 end if;
6560 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
6561 -- subprograms with no specs are not frozen.
6563 declare
6564 Typ : constant Entity_Id := Etype (Spec_Id);
6565 Utyp : constant Entity_Id := Underlying_Type (Typ);
6567 begin
6568 if not Acts_As_Spec (N)
6569 and then Nkind (Parent (Parent (Spec_Id))) /=
6570 N_Subprogram_Body_Stub
6571 then
6572 null;
6574 elsif Is_Immutably_Limited_Type (Typ) then
6575 Set_Returns_By_Ref (Spec_Id);
6577 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6578 Set_Returns_By_Ref (Spec_Id);
6579 end if;
6580 end;
6582 -- For a procedure, we add a return for all possible syntactic ends of
6583 -- the subprogram.
6585 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
6586 Add_Return (Statements (H));
6588 if Present (Exception_Handlers (H)) then
6589 Except_H := First_Non_Pragma (Exception_Handlers (H));
6590 while Present (Except_H) loop
6591 Add_Return (Statements (Except_H));
6592 Next_Non_Pragma (Except_H);
6593 end loop;
6594 end if;
6596 -- For a function, we must deal with the case where there is at least
6597 -- one missing return. What we do is to wrap the entire body of the
6598 -- function in a block:
6600 -- begin
6601 -- ...
6602 -- end;
6604 -- becomes
6606 -- begin
6607 -- begin
6608 -- ...
6609 -- end;
6611 -- raise Program_Error;
6612 -- end;
6614 -- This approach is necessary because the raise must be signalled to the
6615 -- caller, not handled by any local handler (RM 6.4(11)).
6617 -- Note: we do not need to analyze the constructed sequence here, since
6618 -- it has no handler, and an attempt to analyze the handled statement
6619 -- sequence twice is risky in various ways (e.g. the issue of expanding
6620 -- cleanup actions twice).
6622 elsif Has_Missing_Return (Spec_Id) then
6623 declare
6624 Hloc : constant Source_Ptr := Sloc (H);
6625 Blok : constant Node_Id :=
6626 Make_Block_Statement (Hloc,
6627 Handled_Statement_Sequence => H);
6628 Rais : constant Node_Id :=
6629 Make_Raise_Program_Error (Hloc,
6630 Reason => PE_Missing_Return);
6632 begin
6633 Set_Handled_Statement_Sequence (N,
6634 Make_Handled_Sequence_Of_Statements (Hloc,
6635 Statements => New_List (Blok, Rais)));
6637 Push_Scope (Spec_Id);
6638 Analyze (Blok);
6639 Analyze (Rais);
6640 Pop_Scope;
6641 end;
6642 end if;
6644 -- If subprogram contains a parameterless recursive call, then we may
6645 -- have an infinite recursion, so see if we can generate code to check
6646 -- for this possibility if storage checks are not suppressed.
6648 if Ekind (Spec_Id) = E_Procedure
6649 and then Has_Recursive_Call (Spec_Id)
6650 and then not Storage_Checks_Suppressed (Spec_Id)
6651 then
6652 Detect_Infinite_Recursion (N, Spec_Id);
6653 end if;
6655 -- Set to encode entity names in package body before gigi is called
6657 Qualify_Entity_Names (N);
6658 end Expand_N_Subprogram_Body;
6660 -----------------------------------
6661 -- Expand_N_Subprogram_Body_Stub --
6662 -----------------------------------
6664 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
6665 begin
6666 if Present (Corresponding_Body (N)) then
6667 Expand_N_Subprogram_Body (
6668 Unit_Declaration_Node (Corresponding_Body (N)));
6669 end if;
6670 end Expand_N_Subprogram_Body_Stub;
6672 -------------------------------------
6673 -- Expand_N_Subprogram_Declaration --
6674 -------------------------------------
6676 -- If the declaration appears within a protected body, it is a private
6677 -- operation of the protected type. We must create the corresponding
6678 -- protected subprogram an associated formals. For a normal protected
6679 -- operation, this is done when expanding the protected type declaration.
6681 -- If the declaration is for a null procedure, emit null body
6683 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
6684 Loc : constant Source_Ptr := Sloc (N);
6685 Subp : constant Entity_Id := Defining_Entity (N);
6686 Scop : constant Entity_Id := Scope (Subp);
6687 Prot_Decl : Node_Id;
6688 Prot_Bod : Node_Id;
6689 Prot_Id : Entity_Id;
6691 begin
6692 -- In SPARK, subprogram declarations are only allowed in package
6693 -- specifications.
6695 if Nkind (Parent (N)) /= N_Package_Specification then
6696 if Nkind (Parent (N)) = N_Compilation_Unit then
6697 Check_SPARK_Restriction
6698 ("subprogram declaration is not a library item", N);
6700 elsif Present (Next (N))
6701 and then Nkind (Next (N)) = N_Pragma
6702 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
6703 then
6704 -- In SPARK, subprogram declarations are also permitted in
6705 -- declarative parts when immediately followed by a corresponding
6706 -- pragma Import. We only check here that there is some pragma
6707 -- Import.
6709 null;
6710 else
6711 Check_SPARK_Restriction
6712 ("subprogram declaration is not allowed here", N);
6713 end if;
6714 end if;
6716 -- Deal with case of protected subprogram. Do not generate protected
6717 -- operation if operation is flagged as eliminated.
6719 if Is_List_Member (N)
6720 and then Present (Parent (List_Containing (N)))
6721 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6722 and then Is_Protected_Type (Scop)
6723 then
6724 if No (Protected_Body_Subprogram (Subp))
6725 and then not Is_Eliminated (Subp)
6726 then
6727 Prot_Decl :=
6728 Make_Subprogram_Declaration (Loc,
6729 Specification =>
6730 Build_Protected_Sub_Specification
6731 (N, Scop, Unprotected_Mode));
6733 -- The protected subprogram is declared outside of the protected
6734 -- body. Given that the body has frozen all entities so far, we
6735 -- analyze the subprogram and perform freezing actions explicitly.
6736 -- including the generation of an explicit freeze node, to ensure
6737 -- that gigi has the proper order of elaboration.
6738 -- If the body is a subunit, the insertion point is before the
6739 -- stub in the parent.
6741 Prot_Bod := Parent (List_Containing (N));
6743 if Nkind (Parent (Prot_Bod)) = N_Subunit then
6744 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
6745 end if;
6747 Insert_Before (Prot_Bod, Prot_Decl);
6748 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
6749 Set_Has_Delayed_Freeze (Prot_Id);
6751 Push_Scope (Scope (Scop));
6752 Analyze (Prot_Decl);
6753 Freeze_Before (N, Prot_Id);
6754 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6756 -- Create protected operation as well. Even though the operation
6757 -- is only accessible within the body, it is possible to make it
6758 -- available outside of the protected object by using 'Access to
6759 -- provide a callback, so build protected version in all cases.
6761 Prot_Decl :=
6762 Make_Subprogram_Declaration (Loc,
6763 Specification =>
6764 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
6765 Insert_Before (Prot_Bod, Prot_Decl);
6766 Analyze (Prot_Decl);
6768 Pop_Scope;
6769 end if;
6771 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6772 -- cases this is superfluous because calls to it will be automatically
6773 -- inlined, but we definitely need the body if preconditions for the
6774 -- procedure are present.
6776 elsif Nkind (Specification (N)) = N_Procedure_Specification
6777 and then Null_Present (Specification (N))
6778 then
6779 declare
6780 Bod : constant Node_Id := Body_To_Inline (N);
6782 begin
6783 Set_Has_Completion (Subp, False);
6784 Append_Freeze_Action (Subp, Bod);
6786 -- The body now contains raise statements, so calls to it will
6787 -- not be inlined.
6789 Set_Is_Inlined (Subp, False);
6790 end;
6791 end if;
6792 end Expand_N_Subprogram_Declaration;
6794 --------------------------------
6795 -- Expand_Non_Function_Return --
6796 --------------------------------
6798 procedure Expand_Non_Function_Return (N : Node_Id) is
6799 pragma Assert (No (Expression (N)));
6801 Loc : constant Source_Ptr := Sloc (N);
6802 Scope_Id : Entity_Id :=
6803 Return_Applies_To (Return_Statement_Entity (N));
6804 Kind : constant Entity_Kind := Ekind (Scope_Id);
6805 Call : Node_Id;
6806 Acc_Stat : Node_Id;
6807 Goto_Stat : Node_Id;
6808 Lab_Node : Node_Id;
6810 begin
6811 -- Call _Postconditions procedure if procedure with active
6812 -- postconditions. Here, we use the Postcondition_Proc attribute,
6813 -- which is needed for implicitly-generated returns. Functions
6814 -- never have implicitly-generated returns, and there's no
6815 -- room for Postcondition_Proc in E_Function, so we look up the
6816 -- identifier Name_uPostconditions for function returns (see
6817 -- Expand_Simple_Function_Return).
6819 if Ekind (Scope_Id) = E_Procedure
6820 and then Has_Postconditions (Scope_Id)
6821 then
6822 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
6823 Insert_Action (N,
6824 Make_Procedure_Call_Statement (Loc,
6825 Name => New_Reference_To (Postcondition_Proc (Scope_Id), Loc)));
6826 end if;
6828 -- If it is a return from a procedure do no extra steps
6830 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6831 return;
6833 -- If it is a nested return within an extended one, replace it with a
6834 -- return of the previously declared return object.
6836 elsif Kind = E_Return_Statement then
6837 Rewrite (N,
6838 Make_Simple_Return_Statement (Loc,
6839 Expression =>
6840 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6841 Set_Comes_From_Extended_Return_Statement (N);
6842 Set_Return_Statement_Entity (N, Scope_Id);
6843 Expand_Simple_Function_Return (N);
6844 return;
6845 end if;
6847 pragma Assert (Is_Entry (Scope_Id));
6849 -- Look at the enclosing block to see whether the return is from an
6850 -- accept statement or an entry body.
6852 for J in reverse 0 .. Scope_Stack.Last loop
6853 Scope_Id := Scope_Stack.Table (J).Entity;
6854 exit when Is_Concurrent_Type (Scope_Id);
6855 end loop;
6857 -- If it is a return from accept statement it is expanded as call to
6858 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6860 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6861 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6863 if Is_Task_Type (Scope_Id) then
6865 Call :=
6866 Make_Procedure_Call_Statement (Loc,
6867 Name => New_Reference_To (RTE (RE_Complete_Rendezvous), Loc));
6868 Insert_Before (N, Call);
6869 -- why not insert actions here???
6870 Analyze (Call);
6872 Acc_Stat := Parent (N);
6873 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6874 Acc_Stat := Parent (Acc_Stat);
6875 end loop;
6877 Lab_Node := Last (Statements
6878 (Handled_Statement_Sequence (Acc_Stat)));
6880 Goto_Stat := Make_Goto_Statement (Loc,
6881 Name => New_Occurrence_Of
6882 (Entity (Identifier (Lab_Node)), Loc));
6884 Set_Analyzed (Goto_Stat);
6886 Rewrite (N, Goto_Stat);
6887 Analyze (N);
6889 -- If it is a return from an entry body, put a Complete_Entry_Body call
6890 -- in front of the return.
6892 elsif Is_Protected_Type (Scope_Id) then
6893 Call :=
6894 Make_Procedure_Call_Statement (Loc,
6895 Name =>
6896 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
6897 Parameter_Associations => New_List (
6898 Make_Attribute_Reference (Loc,
6899 Prefix =>
6900 New_Reference_To
6901 (Find_Protection_Object (Current_Scope), Loc),
6902 Attribute_Name => Name_Unchecked_Access)));
6904 Insert_Before (N, Call);
6905 Analyze (Call);
6906 end if;
6907 end Expand_Non_Function_Return;
6909 ---------------------------------------
6910 -- Expand_Protected_Object_Reference --
6911 ---------------------------------------
6913 function Expand_Protected_Object_Reference
6914 (N : Node_Id;
6915 Scop : Entity_Id) return Node_Id
6917 Loc : constant Source_Ptr := Sloc (N);
6918 Corr : Entity_Id;
6919 Rec : Node_Id;
6920 Param : Entity_Id;
6921 Proc : Entity_Id;
6923 begin
6924 Rec := Make_Identifier (Loc, Name_uObject);
6925 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6927 -- Find enclosing protected operation, and retrieve its first parameter,
6928 -- which denotes the enclosing protected object. If the enclosing
6929 -- operation is an entry, we are immediately within the protected body,
6930 -- and we can retrieve the object from the service entries procedure. A
6931 -- barrier function has the same signature as an entry. A barrier
6932 -- function is compiled within the protected object, but unlike
6933 -- protected operations its never needs locks, so that its protected
6934 -- body subprogram points to itself.
6936 Proc := Current_Scope;
6937 while Present (Proc)
6938 and then Scope (Proc) /= Scop
6939 loop
6940 Proc := Scope (Proc);
6941 end loop;
6943 Corr := Protected_Body_Subprogram (Proc);
6945 if No (Corr) then
6947 -- Previous error left expansion incomplete.
6948 -- Nothing to do on this call.
6950 return Empty;
6951 end if;
6953 Param :=
6954 Defining_Identifier
6955 (First (Parameter_Specifications (Parent (Corr))));
6957 if Is_Subprogram (Proc)
6958 and then Proc /= Corr
6959 then
6960 -- Protected function or procedure
6962 Set_Entity (Rec, Param);
6964 -- Rec is a reference to an entity which will not be in scope when
6965 -- the call is reanalyzed, and needs no further analysis.
6967 Set_Analyzed (Rec);
6969 else
6970 -- Entry or barrier function for entry body. The first parameter of
6971 -- the entry body procedure is pointer to the object. We create a
6972 -- local variable of the proper type, duplicating what is done to
6973 -- define _object later on.
6975 declare
6976 Decls : List_Id;
6977 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6979 begin
6980 Decls := New_List (
6981 Make_Full_Type_Declaration (Loc,
6982 Defining_Identifier => Obj_Ptr,
6983 Type_Definition =>
6984 Make_Access_To_Object_Definition (Loc,
6985 Subtype_Indication =>
6986 New_Reference_To
6987 (Corresponding_Record_Type (Scop), Loc))));
6989 Insert_Actions (N, Decls);
6990 Freeze_Before (N, Obj_Ptr);
6992 Rec :=
6993 Make_Explicit_Dereference (Loc,
6994 Prefix =>
6995 Unchecked_Convert_To (Obj_Ptr,
6996 New_Occurrence_Of (Param, Loc)));
6998 -- Analyze new actual. Other actuals in calls are already analyzed
6999 -- and the list of actuals is not reanalyzed after rewriting.
7001 Set_Parent (Rec, N);
7002 Analyze (Rec);
7003 end;
7004 end if;
7006 return Rec;
7007 end Expand_Protected_Object_Reference;
7009 --------------------------------------
7010 -- Expand_Protected_Subprogram_Call --
7011 --------------------------------------
7013 procedure Expand_Protected_Subprogram_Call
7014 (N : Node_Id;
7015 Subp : Entity_Id;
7016 Scop : Entity_Id)
7018 Rec : Node_Id;
7020 begin
7021 -- If the protected object is not an enclosing scope, this is an inter-
7022 -- object function call. Inter-object procedure calls are expanded by
7023 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
7024 -- subprogram being called is in the protected body being compiled, and
7025 -- if the protected object in the call is statically the enclosing type.
7026 -- The object may be an component of some other data structure, in which
7027 -- case this must be handled as an inter-object call.
7029 if not In_Open_Scopes (Scop)
7030 or else not Is_Entity_Name (Name (N))
7031 then
7032 if Nkind (Name (N)) = N_Selected_Component then
7033 Rec := Prefix (Name (N));
7035 else
7036 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
7037 Rec := Prefix (Prefix (Name (N)));
7038 end if;
7040 Build_Protected_Subprogram_Call (N,
7041 Name => New_Occurrence_Of (Subp, Sloc (N)),
7042 Rec => Convert_Concurrent (Rec, Etype (Rec)),
7043 External => True);
7045 else
7046 Rec := Expand_Protected_Object_Reference (N, Scop);
7048 if No (Rec) then
7049 return;
7050 end if;
7052 Build_Protected_Subprogram_Call (N,
7053 Name => Name (N),
7054 Rec => Rec,
7055 External => False);
7057 end if;
7059 -- If it is a function call it can appear in elaboration code and
7060 -- the called entity must be frozen here.
7062 if Ekind (Subp) = E_Function then
7063 Freeze_Expression (Name (N));
7064 end if;
7066 -- Analyze and resolve the new call. The actuals have already been
7067 -- resolved, but expansion of a function call will add extra actuals
7068 -- if needed. Analysis of a procedure call already includes resolution.
7070 Analyze (N);
7072 if Ekind (Subp) = E_Function then
7073 Resolve (N, Etype (Subp));
7074 end if;
7075 end Expand_Protected_Subprogram_Call;
7077 --------------------------------------------
7078 -- Has_Unconstrained_Access_Discriminants --
7079 --------------------------------------------
7081 function Has_Unconstrained_Access_Discriminants
7082 (Subtyp : Entity_Id) return Boolean
7084 Discr : Entity_Id;
7086 begin
7087 if Has_Discriminants (Subtyp)
7088 and then not Is_Constrained (Subtyp)
7089 then
7090 Discr := First_Discriminant (Subtyp);
7091 while Present (Discr) loop
7092 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7093 return True;
7094 end if;
7096 Next_Discriminant (Discr);
7097 end loop;
7098 end if;
7100 return False;
7101 end Has_Unconstrained_Access_Discriminants;
7103 -----------------------------------
7104 -- Expand_Simple_Function_Return --
7105 -----------------------------------
7107 -- The "simple" comes from the syntax rule simple_return_statement. The
7108 -- semantics are not at all simple!
7110 procedure Expand_Simple_Function_Return (N : Node_Id) is
7111 Loc : constant Source_Ptr := Sloc (N);
7113 Scope_Id : constant Entity_Id :=
7114 Return_Applies_To (Return_Statement_Entity (N));
7115 -- The function we are returning from
7117 R_Type : constant Entity_Id := Etype (Scope_Id);
7118 -- The result type of the function
7120 Utyp : constant Entity_Id := Underlying_Type (R_Type);
7122 Exp : constant Node_Id := Expression (N);
7123 pragma Assert (Present (Exp));
7125 Exptyp : constant Entity_Id := Etype (Exp);
7126 -- The type of the expression (not necessarily the same as R_Type)
7128 Subtype_Ind : Node_Id;
7129 -- If the result type of the function is class-wide and the expression
7130 -- has a specific type, then we use the expression's type as the type of
7131 -- the return object. In cases where the expression is an aggregate that
7132 -- is built in place, this avoids the need for an expensive conversion
7133 -- of the return object to the specific type on assignments to the
7134 -- individual components.
7136 begin
7137 if Is_Class_Wide_Type (R_Type)
7138 and then not Is_Class_Wide_Type (Etype (Exp))
7139 then
7140 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
7141 else
7142 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
7143 end if;
7145 -- For the case of a simple return that does not come from an extended
7146 -- return, in the case of Ada 2005 where we are returning a limited
7147 -- type, we rewrite "return <expression>;" to be:
7149 -- return _anon_ : <return_subtype> := <expression>
7151 -- The expansion produced by Expand_N_Extended_Return_Statement will
7152 -- contain simple return statements (for example, a block containing
7153 -- simple return of the return object), which brings us back here with
7154 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
7155 -- checking for a simple return that does not come from an extended
7156 -- return is to avoid this infinite recursion.
7158 -- The reason for this design is that for Ada 2005 limited returns, we
7159 -- need to reify the return object, so we can build it "in place", and
7160 -- we need a block statement to hang finalization and tasking stuff.
7162 -- ??? In order to avoid disruption, we avoid translating to extended
7163 -- return except in the cases where we really need to (Ada 2005 for
7164 -- inherently limited). We might prefer to do this translation in all
7165 -- cases (except perhaps for the case of Ada 95 inherently limited),
7166 -- in order to fully exercise the Expand_N_Extended_Return_Statement
7167 -- code. This would also allow us to do the build-in-place optimization
7168 -- for efficiency even in cases where it is semantically not required.
7170 -- As before, we check the type of the return expression rather than the
7171 -- return type of the function, because the latter may be a limited
7172 -- class-wide interface type, which is not a limited type, even though
7173 -- the type of the expression may be.
7175 if not Comes_From_Extended_Return_Statement (N)
7176 and then Is_Immutably_Limited_Type (Etype (Expression (N)))
7177 and then Ada_Version >= Ada_2005
7178 and then not Debug_Flag_Dot_L
7179 then
7180 declare
7181 Return_Object_Entity : constant Entity_Id :=
7182 Make_Temporary (Loc, 'R', Exp);
7183 Obj_Decl : constant Node_Id :=
7184 Make_Object_Declaration (Loc,
7185 Defining_Identifier => Return_Object_Entity,
7186 Object_Definition => Subtype_Ind,
7187 Expression => Exp);
7189 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
7190 Return_Object_Declarations => New_List (Obj_Decl));
7191 -- Do not perform this high-level optimization if the result type
7192 -- is an interface because the "this" pointer must be displaced.
7194 begin
7195 Rewrite (N, Ext);
7196 Analyze (N);
7197 return;
7198 end;
7199 end if;
7201 -- Here we have a simple return statement that is part of the expansion
7202 -- of an extended return statement (either written by the user, or
7203 -- generated by the above code).
7205 -- Always normalize C/Fortran boolean result. This is not always needed,
7206 -- but it seems a good idea to minimize the passing around of non-
7207 -- normalized values, and in any case this handles the processing of
7208 -- barrier functions for protected types, which turn the condition into
7209 -- a return statement.
7211 if Is_Boolean_Type (Exptyp)
7212 and then Nonzero_Is_True (Exptyp)
7213 then
7214 Adjust_Condition (Exp);
7215 Adjust_Result_Type (Exp, Exptyp);
7216 end if;
7218 -- Do validity check if enabled for returns
7220 if Validity_Checks_On
7221 and then Validity_Check_Returns
7222 then
7223 Ensure_Valid (Exp);
7224 end if;
7226 -- Check the result expression of a scalar function against the subtype
7227 -- of the function by inserting a conversion. This conversion must
7228 -- eventually be performed for other classes of types, but for now it's
7229 -- only done for scalars.
7230 -- ???
7232 if Is_Scalar_Type (Exptyp) then
7233 Rewrite (Exp, Convert_To (R_Type, Exp));
7235 -- The expression is resolved to ensure that the conversion gets
7236 -- expanded to generate a possible constraint check.
7238 Analyze_And_Resolve (Exp, R_Type);
7239 end if;
7241 -- Deal with returning variable length objects and controlled types
7243 -- Nothing to do if we are returning by reference, or this is not a
7244 -- type that requires special processing (indicated by the fact that
7245 -- it requires a cleanup scope for the secondary stack case).
7247 if Is_Immutably_Limited_Type (Exptyp)
7248 or else Is_Limited_Interface (Exptyp)
7249 then
7250 null;
7252 elsif not Requires_Transient_Scope (R_Type) then
7254 -- Mutable records with no variable length components are not
7255 -- returned on the sec-stack, so we need to make sure that the
7256 -- backend will only copy back the size of the actual value, and not
7257 -- the maximum size. We create an actual subtype for this purpose.
7259 declare
7260 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
7261 Decl : Node_Id;
7262 Ent : Entity_Id;
7263 begin
7264 if Has_Discriminants (Ubt)
7265 and then not Is_Constrained (Ubt)
7266 and then not Has_Unchecked_Union (Ubt)
7267 then
7268 Decl := Build_Actual_Subtype (Ubt, Exp);
7269 Ent := Defining_Identifier (Decl);
7270 Insert_Action (Exp, Decl);
7271 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
7272 Analyze_And_Resolve (Exp);
7273 end if;
7274 end;
7276 -- Here if secondary stack is used
7278 else
7279 -- Make sure that no surrounding block will reclaim the secondary
7280 -- stack on which we are going to put the result. Not only may this
7281 -- introduce secondary stack leaks but worse, if the reclamation is
7282 -- done too early, then the result we are returning may get
7283 -- clobbered.
7285 declare
7286 S : Entity_Id;
7287 begin
7288 S := Current_Scope;
7289 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
7290 Set_Sec_Stack_Needed_For_Return (S, True);
7291 S := Enclosing_Dynamic_Scope (S);
7292 end loop;
7293 end;
7295 -- Optimize the case where the result is a function call. In this
7296 -- case either the result is already on the secondary stack, or is
7297 -- already being returned with the stack pointer depressed and no
7298 -- further processing is required except to set the By_Ref flag
7299 -- to ensure that gigi does not attempt an extra unnecessary copy.
7300 -- (actually not just unnecessary but harmfully wrong in the case
7301 -- of a controlled type, where gigi does not know how to do a copy).
7302 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
7303 -- for array types if the constrained status of the target type is
7304 -- different from that of the expression.
7306 if Requires_Transient_Scope (Exptyp)
7307 and then
7308 (not Is_Array_Type (Exptyp)
7309 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
7310 or else CW_Or_Has_Controlled_Part (Utyp))
7311 and then Nkind (Exp) = N_Function_Call
7312 then
7313 Set_By_Ref (N);
7315 -- Remove side effects from the expression now so that other parts
7316 -- of the expander do not have to reanalyze this node without this
7317 -- optimization
7319 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
7321 -- For controlled types, do the allocation on the secondary stack
7322 -- manually in order to call adjust at the right time:
7324 -- type Anon1 is access R_Type;
7325 -- for Anon1'Storage_pool use ss_pool;
7326 -- Anon2 : anon1 := new R_Type'(expr);
7327 -- return Anon2.all;
7329 -- We do the same for classwide types that are not potentially
7330 -- controlled (by the virtue of restriction No_Finalization) because
7331 -- gigi is not able to properly allocate class-wide types.
7333 elsif CW_Or_Has_Controlled_Part (Utyp) then
7334 declare
7335 Loc : constant Source_Ptr := Sloc (N);
7336 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7337 Alloc_Node : Node_Id;
7338 Temp : Entity_Id;
7340 begin
7341 Set_Ekind (Acc_Typ, E_Access_Type);
7343 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
7345 -- This is an allocator for the secondary stack, and it's fine
7346 -- to have Comes_From_Source set False on it, as gigi knows not
7347 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7349 Alloc_Node :=
7350 Make_Allocator (Loc,
7351 Expression =>
7352 Make_Qualified_Expression (Loc,
7353 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
7354 Expression => Relocate_Node (Exp)));
7356 -- We do not want discriminant checks on the declaration,
7357 -- given that it gets its value from the allocator.
7359 Set_No_Initialization (Alloc_Node);
7361 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
7363 Insert_List_Before_And_Analyze (N, New_List (
7364 Make_Full_Type_Declaration (Loc,
7365 Defining_Identifier => Acc_Typ,
7366 Type_Definition =>
7367 Make_Access_To_Object_Definition (Loc,
7368 Subtype_Indication => Subtype_Ind)),
7370 Make_Object_Declaration (Loc,
7371 Defining_Identifier => Temp,
7372 Object_Definition => New_Reference_To (Acc_Typ, Loc),
7373 Expression => Alloc_Node)));
7375 Rewrite (Exp,
7376 Make_Explicit_Dereference (Loc,
7377 Prefix => New_Reference_To (Temp, Loc)));
7379 -- Ada 2005 (AI-251): If the type of the returned object is
7380 -- an interface then add an implicit type conversion to force
7381 -- displacement of the "this" pointer.
7383 if Is_Interface (R_Type) then
7384 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
7385 end if;
7387 Analyze_And_Resolve (Exp, R_Type);
7388 end;
7390 -- Otherwise use the gigi mechanism to allocate result on the
7391 -- secondary stack.
7393 else
7394 Check_Restriction (No_Secondary_Stack, N);
7395 Set_Storage_Pool (N, RTE (RE_SS_Pool));
7397 -- If we are generating code for the VM do not use
7398 -- SS_Allocate since everything is heap-allocated anyway.
7400 if VM_Target = No_VM then
7401 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
7402 end if;
7403 end if;
7404 end if;
7406 -- Implement the rules of 6.5(8-10), which require a tag check in
7407 -- the case of a limited tagged return type, and tag reassignment for
7408 -- nonlimited tagged results. These actions are needed when the return
7409 -- type is a specific tagged type and the result expression is a
7410 -- conversion or a formal parameter, because in that case the tag of
7411 -- the expression might differ from the tag of the specific result type.
7413 if Is_Tagged_Type (Utyp)
7414 and then not Is_Class_Wide_Type (Utyp)
7415 and then (Nkind_In (Exp, N_Type_Conversion,
7416 N_Unchecked_Type_Conversion)
7417 or else (Is_Entity_Name (Exp)
7418 and then Ekind (Entity (Exp)) in Formal_Kind))
7419 then
7420 -- When the return type is limited, perform a check that the tag of
7421 -- the result is the same as the tag of the return type.
7423 if Is_Limited_Type (R_Type) then
7424 Insert_Action (Exp,
7425 Make_Raise_Constraint_Error (Loc,
7426 Condition =>
7427 Make_Op_Ne (Loc,
7428 Left_Opnd =>
7429 Make_Selected_Component (Loc,
7430 Prefix => Duplicate_Subexpr (Exp),
7431 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7432 Right_Opnd =>
7433 Make_Attribute_Reference (Loc,
7434 Prefix =>
7435 New_Occurrence_Of (Base_Type (Utyp), Loc),
7436 Attribute_Name => Name_Tag)),
7437 Reason => CE_Tag_Check_Failed));
7439 -- If the result type is a specific nonlimited tagged type, then we
7440 -- have to ensure that the tag of the result is that of the result
7441 -- type. This is handled by making a copy of the expression in
7442 -- the case where it might have a different tag, namely when the
7443 -- expression is a conversion or a formal parameter. We create a new
7444 -- object of the result type and initialize it from the expression,
7445 -- which will implicitly force the tag to be set appropriately.
7447 else
7448 declare
7449 ExpR : constant Node_Id := Relocate_Node (Exp);
7450 Result_Id : constant Entity_Id :=
7451 Make_Temporary (Loc, 'R', ExpR);
7452 Result_Exp : constant Node_Id :=
7453 New_Reference_To (Result_Id, Loc);
7454 Result_Obj : constant Node_Id :=
7455 Make_Object_Declaration (Loc,
7456 Defining_Identifier => Result_Id,
7457 Object_Definition =>
7458 New_Reference_To (R_Type, Loc),
7459 Constant_Present => True,
7460 Expression => ExpR);
7462 begin
7463 Set_Assignment_OK (Result_Obj);
7464 Insert_Action (Exp, Result_Obj);
7466 Rewrite (Exp, Result_Exp);
7467 Analyze_And_Resolve (Exp, R_Type);
7468 end;
7469 end if;
7471 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
7472 -- a check that the level of the return expression's underlying type
7473 -- is not deeper than the level of the master enclosing the function.
7474 -- Always generate the check when the type of the return expression
7475 -- is class-wide, when it's a type conversion, or when it's a formal
7476 -- parameter. Otherwise, suppress the check in the case where the
7477 -- return expression has a specific type whose level is known not to
7478 -- be statically deeper than the function's result type.
7480 -- Note: accessibility check is skipped in the VM case, since there
7481 -- does not seem to be any practical way to implement this check.
7483 elsif Ada_Version >= Ada_2005
7484 and then Tagged_Type_Expansion
7485 and then Is_Class_Wide_Type (R_Type)
7486 and then not Scope_Suppress.Suppress (Accessibility_Check)
7487 and then
7488 (Is_Class_Wide_Type (Etype (Exp))
7489 or else Nkind_In (Exp, N_Type_Conversion,
7490 N_Unchecked_Type_Conversion)
7491 or else (Is_Entity_Name (Exp)
7492 and then Ekind (Entity (Exp)) in Formal_Kind)
7493 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
7494 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
7495 then
7496 declare
7497 Tag_Node : Node_Id;
7499 begin
7500 -- Ada 2005 (AI-251): In class-wide interface objects we displace
7501 -- "this" to reference the base of the object. This is required to
7502 -- get access to the TSD of the object.
7504 if Is_Class_Wide_Type (Etype (Exp))
7505 and then Is_Interface (Etype (Exp))
7506 and then Nkind (Exp) = N_Explicit_Dereference
7507 then
7508 Tag_Node :=
7509 Make_Explicit_Dereference (Loc,
7510 Prefix =>
7511 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
7512 Make_Function_Call (Loc,
7513 Name =>
7514 New_Reference_To (RTE (RE_Base_Address), Loc),
7515 Parameter_Associations => New_List (
7516 Unchecked_Convert_To (RTE (RE_Address),
7517 Duplicate_Subexpr (Prefix (Exp)))))));
7518 else
7519 Tag_Node :=
7520 Make_Attribute_Reference (Loc,
7521 Prefix => Duplicate_Subexpr (Exp),
7522 Attribute_Name => Name_Tag);
7523 end if;
7525 Insert_Action (Exp,
7526 Make_Raise_Program_Error (Loc,
7527 Condition =>
7528 Make_Op_Gt (Loc,
7529 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
7530 Right_Opnd =>
7531 Make_Integer_Literal (Loc,
7532 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
7533 Reason => PE_Accessibility_Check_Failed));
7534 end;
7536 -- AI05-0073: If function has a controlling access result, check that
7537 -- the tag of the return value, if it is not null, matches designated
7538 -- type of return type.
7539 -- The return expression is referenced twice in the code below, so
7540 -- it must be made free of side effects. Given that different compilers
7541 -- may evaluate these parameters in different order, both occurrences
7542 -- perform a copy.
7544 elsif Ekind (R_Type) = E_Anonymous_Access_Type
7545 and then Has_Controlling_Result (Scope_Id)
7546 then
7547 Insert_Action (N,
7548 Make_Raise_Constraint_Error (Loc,
7549 Condition =>
7550 Make_And_Then (Loc,
7551 Left_Opnd =>
7552 Make_Op_Ne (Loc,
7553 Left_Opnd => Duplicate_Subexpr (Exp),
7554 Right_Opnd => Make_Null (Loc)),
7556 Right_Opnd => Make_Op_Ne (Loc,
7557 Left_Opnd =>
7558 Make_Selected_Component (Loc,
7559 Prefix => Duplicate_Subexpr (Exp),
7560 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7562 Right_Opnd =>
7563 Make_Attribute_Reference (Loc,
7564 Prefix =>
7565 New_Occurrence_Of (Designated_Type (R_Type), Loc),
7566 Attribute_Name => Name_Tag))),
7568 Reason => CE_Tag_Check_Failed),
7569 Suppress => All_Checks);
7570 end if;
7572 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
7573 -- ensure that the function result does not outlive an
7574 -- object designated by one of it discriminants.
7576 if Present (Extra_Accessibility_Of_Result (Scope_Id))
7577 and then Has_Unconstrained_Access_Discriminants (R_Type)
7578 then
7579 declare
7580 Discrim_Source : Node_Id;
7582 procedure Check_Against_Result_Level (Level : Node_Id);
7583 -- Check the given accessibility level against the level
7584 -- determined by the point of call. (AI05-0234).
7586 --------------------------------
7587 -- Check_Against_Result_Level --
7588 --------------------------------
7590 procedure Check_Against_Result_Level (Level : Node_Id) is
7591 begin
7592 Insert_Action (N,
7593 Make_Raise_Program_Error (Loc,
7594 Condition =>
7595 Make_Op_Gt (Loc,
7596 Left_Opnd => Level,
7597 Right_Opnd =>
7598 New_Occurrence_Of
7599 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
7600 Reason => PE_Accessibility_Check_Failed));
7601 end Check_Against_Result_Level;
7603 begin
7604 Discrim_Source := Exp;
7605 while Nkind (Discrim_Source) = N_Qualified_Expression loop
7606 Discrim_Source := Expression (Discrim_Source);
7607 end loop;
7609 if Nkind (Discrim_Source) = N_Identifier
7610 and then Is_Return_Object (Entity (Discrim_Source))
7611 then
7612 Discrim_Source := Entity (Discrim_Source);
7614 if Is_Constrained (Etype (Discrim_Source)) then
7615 Discrim_Source := Etype (Discrim_Source);
7616 else
7617 Discrim_Source := Expression (Parent (Discrim_Source));
7618 end if;
7620 elsif Nkind (Discrim_Source) = N_Identifier
7621 and then Nkind_In (Original_Node (Discrim_Source),
7622 N_Aggregate, N_Extension_Aggregate)
7623 then
7624 Discrim_Source := Original_Node (Discrim_Source);
7626 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
7627 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
7628 then
7629 Discrim_Source := Original_Node (Discrim_Source);
7630 end if;
7632 while Nkind_In (Discrim_Source, N_Qualified_Expression,
7633 N_Type_Conversion,
7634 N_Unchecked_Type_Conversion)
7635 loop
7636 Discrim_Source := Expression (Discrim_Source);
7637 end loop;
7639 case Nkind (Discrim_Source) is
7640 when N_Defining_Identifier =>
7642 pragma Assert (Is_Composite_Type (Discrim_Source)
7643 and then Has_Discriminants (Discrim_Source)
7644 and then Is_Constrained (Discrim_Source));
7646 declare
7647 Discrim : Entity_Id :=
7648 First_Discriminant (Base_Type (R_Type));
7649 Disc_Elmt : Elmt_Id :=
7650 First_Elmt (Discriminant_Constraint
7651 (Discrim_Source));
7652 begin
7653 loop
7654 if Ekind (Etype (Discrim)) =
7655 E_Anonymous_Access_Type
7656 then
7657 Check_Against_Result_Level
7658 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
7659 end if;
7661 Next_Elmt (Disc_Elmt);
7662 Next_Discriminant (Discrim);
7663 exit when not Present (Discrim);
7664 end loop;
7665 end;
7667 when N_Aggregate | N_Extension_Aggregate =>
7669 -- Unimplemented: extension aggregate case where discrims
7670 -- come from ancestor part, not extension part.
7672 declare
7673 Discrim : Entity_Id :=
7674 First_Discriminant (Base_Type (R_Type));
7676 Disc_Exp : Node_Id := Empty;
7678 Positionals_Exhausted
7679 : Boolean := not Present (Expressions
7680 (Discrim_Source));
7682 function Associated_Expr
7683 (Comp_Id : Entity_Id;
7684 Associations : List_Id) return Node_Id;
7686 -- Given a component and a component associations list,
7687 -- locate the expression for that component; returns
7688 -- Empty if no such expression is found.
7690 ---------------------
7691 -- Associated_Expr --
7692 ---------------------
7694 function Associated_Expr
7695 (Comp_Id : Entity_Id;
7696 Associations : List_Id) return Node_Id
7698 Assoc : Node_Id;
7699 Choice : Node_Id;
7701 begin
7702 -- Simple linear search seems ok here
7704 Assoc := First (Associations);
7705 while Present (Assoc) loop
7706 Choice := First (Choices (Assoc));
7707 while Present (Choice) loop
7708 if (Nkind (Choice) = N_Identifier
7709 and then Chars (Choice) = Chars (Comp_Id))
7710 or else (Nkind (Choice) = N_Others_Choice)
7711 then
7712 return Expression (Assoc);
7713 end if;
7715 Next (Choice);
7716 end loop;
7718 Next (Assoc);
7719 end loop;
7721 return Empty;
7722 end Associated_Expr;
7724 -- Start of processing for Expand_Simple_Function_Return
7726 begin
7727 if not Positionals_Exhausted then
7728 Disc_Exp := First (Expressions (Discrim_Source));
7729 end if;
7731 loop
7732 if Positionals_Exhausted then
7733 Disc_Exp :=
7734 Associated_Expr
7735 (Discrim,
7736 Component_Associations (Discrim_Source));
7737 end if;
7739 if Ekind (Etype (Discrim)) =
7740 E_Anonymous_Access_Type
7741 then
7742 Check_Against_Result_Level
7743 (Dynamic_Accessibility_Level (Disc_Exp));
7744 end if;
7746 Next_Discriminant (Discrim);
7747 exit when not Present (Discrim);
7749 if not Positionals_Exhausted then
7750 Next (Disc_Exp);
7751 Positionals_Exhausted := not Present (Disc_Exp);
7752 end if;
7753 end loop;
7754 end;
7756 when N_Function_Call =>
7758 -- No check needed (check performed by callee)
7760 null;
7762 when others =>
7764 declare
7765 Level : constant Node_Id :=
7766 Make_Integer_Literal (Loc,
7767 Object_Access_Level (Discrim_Source));
7769 begin
7770 -- Unimplemented: check for name prefix that includes
7771 -- a dereference of an access value with a dynamic
7772 -- accessibility level (e.g., an access param or a
7773 -- saooaaat) and use dynamic level in that case. For
7774 -- example:
7775 -- return Access_Param.all(Some_Index).Some_Component;
7776 -- ???
7778 Set_Etype (Level, Standard_Natural);
7779 Check_Against_Result_Level (Level);
7780 end;
7782 end case;
7783 end;
7784 end if;
7786 -- If we are returning an object that may not be bit-aligned, then copy
7787 -- the value into a temporary first. This copy may need to expand to a
7788 -- loop of component operations.
7790 if Is_Possibly_Unaligned_Slice (Exp)
7791 or else Is_Possibly_Unaligned_Object (Exp)
7792 then
7793 declare
7794 ExpR : constant Node_Id := Relocate_Node (Exp);
7795 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7796 begin
7797 Insert_Action (Exp,
7798 Make_Object_Declaration (Loc,
7799 Defining_Identifier => Tnn,
7800 Constant_Present => True,
7801 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7802 Expression => ExpR),
7803 Suppress => All_Checks);
7804 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7805 end;
7806 end if;
7808 -- Generate call to postcondition checks if they are present
7810 if Ekind (Scope_Id) = E_Function
7811 and then Has_Postconditions (Scope_Id)
7812 then
7813 -- We are going to reference the returned value twice in this case,
7814 -- once in the call to _Postconditions, and once in the actual return
7815 -- statement, but we can't have side effects happening twice, and in
7816 -- any case for efficiency we don't want to do the computation twice.
7818 -- If the returned expression is an entity name, we don't need to
7819 -- worry since it is efficient and safe to reference it twice, that's
7820 -- also true for literals other than string literals, and for the
7821 -- case of X.all where X is an entity name.
7823 if Is_Entity_Name (Exp)
7824 or else Nkind_In (Exp, N_Character_Literal,
7825 N_Integer_Literal,
7826 N_Real_Literal)
7827 or else (Nkind (Exp) = N_Explicit_Dereference
7828 and then Is_Entity_Name (Prefix (Exp)))
7829 then
7830 null;
7832 -- Otherwise we are going to need a temporary to capture the value
7834 else
7835 declare
7836 ExpR : constant Node_Id := Relocate_Node (Exp);
7837 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7839 begin
7840 -- For a complex expression of an elementary type, capture
7841 -- value in the temporary and use it as the reference.
7843 if Is_Elementary_Type (R_Type) then
7844 Insert_Action (Exp,
7845 Make_Object_Declaration (Loc,
7846 Defining_Identifier => Tnn,
7847 Constant_Present => True,
7848 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7849 Expression => ExpR),
7850 Suppress => All_Checks);
7852 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7854 -- If we have something we can rename, generate a renaming of
7855 -- the object and replace the expression with a reference
7857 elsif Is_Object_Reference (Exp) then
7858 Insert_Action (Exp,
7859 Make_Object_Renaming_Declaration (Loc,
7860 Defining_Identifier => Tnn,
7861 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
7862 Name => ExpR),
7863 Suppress => All_Checks);
7865 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7867 -- Otherwise we have something like a string literal or an
7868 -- aggregate. We could copy the value, but that would be
7869 -- inefficient. Instead we make a reference to the value and
7870 -- capture this reference with a renaming, the expression is
7871 -- then replaced by a dereference of this renaming.
7873 else
7874 -- For now, copy the value, since the code below does not
7875 -- seem to work correctly ???
7877 Insert_Action (Exp,
7878 Make_Object_Declaration (Loc,
7879 Defining_Identifier => Tnn,
7880 Constant_Present => True,
7881 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7882 Expression => Relocate_Node (Exp)),
7883 Suppress => All_Checks);
7885 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7887 -- Insert_Action (Exp,
7888 -- Make_Object_Renaming_Declaration (Loc,
7889 -- Defining_Identifier => Tnn,
7890 -- Access_Definition =>
7891 -- Make_Access_Definition (Loc,
7892 -- All_Present => True,
7893 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
7894 -- Name =>
7895 -- Make_Reference (Loc,
7896 -- Prefix => Relocate_Node (Exp))),
7897 -- Suppress => All_Checks);
7899 -- Rewrite (Exp,
7900 -- Make_Explicit_Dereference (Loc,
7901 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
7902 end if;
7903 end;
7904 end if;
7906 -- Generate call to _postconditions
7908 Insert_Action (Exp,
7909 Make_Procedure_Call_Statement (Loc,
7910 Name => Make_Identifier (Loc, Name_uPostconditions),
7911 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
7912 end if;
7914 -- Ada 2005 (AI-251): If this return statement corresponds with an
7915 -- simple return statement associated with an extended return statement
7916 -- and the type of the returned object is an interface then generate an
7917 -- implicit conversion to force displacement of the "this" pointer.
7919 if Ada_Version >= Ada_2005
7920 and then Comes_From_Extended_Return_Statement (N)
7921 and then Nkind (Expression (N)) = N_Identifier
7922 and then Is_Interface (Utyp)
7923 and then Utyp /= Underlying_Type (Exptyp)
7924 then
7925 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7926 Analyze_And_Resolve (Exp);
7927 end if;
7928 end Expand_Simple_Function_Return;
7930 --------------------------------
7931 -- Is_Build_In_Place_Function --
7932 --------------------------------
7934 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7935 begin
7936 -- This function is called from Expand_Subtype_From_Expr during
7937 -- semantic analysis, even when expansion is off. In those cases
7938 -- the build_in_place expansion will not take place.
7940 if not Expander_Active then
7941 return False;
7942 end if;
7944 -- For now we test whether E denotes a function or access-to-function
7945 -- type whose result subtype is inherently limited. Later this test may
7946 -- be revised to allow composite nonlimited types. Functions with a
7947 -- foreign convention or whose result type has a foreign convention
7948 -- never qualify.
7950 if Ekind_In (E, E_Function, E_Generic_Function)
7951 or else (Ekind (E) = E_Subprogram_Type
7952 and then Etype (E) /= Standard_Void_Type)
7953 then
7954 -- Note: If you have Convention (C) on an inherently limited type,
7955 -- you're on your own. That is, the C code will have to be carefully
7956 -- written to know about the Ada conventions.
7958 if Has_Foreign_Convention (E)
7959 or else Has_Foreign_Convention (Etype (E))
7960 then
7961 return False;
7963 -- In Ada 2005 all functions with an inherently limited return type
7964 -- must be handled using a build-in-place profile, including the case
7965 -- of a function with a limited interface result, where the function
7966 -- may return objects of nonlimited descendants.
7968 else
7969 return Is_Immutably_Limited_Type (Etype (E))
7970 and then Ada_Version >= Ada_2005
7971 and then not Debug_Flag_Dot_L;
7972 end if;
7974 else
7975 return False;
7976 end if;
7977 end Is_Build_In_Place_Function;
7979 -------------------------------------
7980 -- Is_Build_In_Place_Function_Call --
7981 -------------------------------------
7983 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7984 Exp_Node : Node_Id := N;
7985 Function_Id : Entity_Id;
7987 begin
7988 -- Return False when the expander is inactive, since awareness of
7989 -- build-in-place treatment is only relevant during expansion. Note that
7990 -- Is_Build_In_Place_Function, which is called as part of this function,
7991 -- is also conditioned this way, but we need to check here as well to
7992 -- avoid blowing up on processing protected calls when expansion is
7993 -- disabled (such as with -gnatc) since those would trip over the raise
7994 -- of Program_Error below.
7996 if not Expander_Active then
7997 return False;
7998 end if;
8000 -- Step past qualification or unchecked conversion (the latter can occur
8001 -- in cases of calls to 'Input).
8003 if Nkind_In (Exp_Node, N_Qualified_Expression,
8004 N_Unchecked_Type_Conversion)
8005 then
8006 Exp_Node := Expression (N);
8007 end if;
8009 if Nkind (Exp_Node) /= N_Function_Call then
8010 return False;
8012 else
8013 -- In Alfa mode, build-in-place calls are not expanded, so that we
8014 -- may end up with a call that is neither resolved to an entity, nor
8015 -- an indirect call.
8017 if Alfa_Mode then
8018 return False;
8020 elsif Is_Entity_Name (Name (Exp_Node)) then
8021 Function_Id := Entity (Name (Exp_Node));
8023 -- In the case of an explicitly dereferenced call, use the subprogram
8024 -- type generated for the dereference.
8026 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
8027 Function_Id := Etype (Name (Exp_Node));
8029 else
8030 raise Program_Error;
8031 end if;
8033 return Is_Build_In_Place_Function (Function_Id);
8034 end if;
8035 end Is_Build_In_Place_Function_Call;
8037 -----------------------
8038 -- Freeze_Subprogram --
8039 -----------------------
8041 procedure Freeze_Subprogram (N : Node_Id) is
8042 Loc : constant Source_Ptr := Sloc (N);
8044 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
8045 -- (Ada 2005): Register a predefined primitive in all the secondary
8046 -- dispatch tables of its primitive type.
8048 ----------------------------------
8049 -- Register_Predefined_DT_Entry --
8050 ----------------------------------
8052 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
8053 Iface_DT_Ptr : Elmt_Id;
8054 Tagged_Typ : Entity_Id;
8055 Thunk_Id : Entity_Id;
8056 Thunk_Code : Node_Id;
8058 begin
8059 Tagged_Typ := Find_Dispatching_Type (Prim);
8061 if No (Access_Disp_Table (Tagged_Typ))
8062 or else not Has_Interfaces (Tagged_Typ)
8063 or else not RTE_Available (RE_Interface_Tag)
8064 or else Restriction_Active (No_Dispatching_Calls)
8065 then
8066 return;
8067 end if;
8069 -- Skip the first two access-to-dispatch-table pointers since they
8070 -- leads to the primary dispatch table (predefined DT and user
8071 -- defined DT). We are only concerned with the secondary dispatch
8072 -- table pointers. Note that the access-to- dispatch-table pointer
8073 -- corresponds to the first implemented interface retrieved below.
8075 Iface_DT_Ptr :=
8076 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
8078 while Present (Iface_DT_Ptr)
8079 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
8080 loop
8081 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8082 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
8084 if Present (Thunk_Code) then
8085 Insert_Actions_After (N, New_List (
8086 Thunk_Code,
8088 Build_Set_Predefined_Prim_Op_Address (Loc,
8089 Tag_Node =>
8090 New_Reference_To (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
8091 Position => DT_Position (Prim),
8092 Address_Node =>
8093 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8094 Make_Attribute_Reference (Loc,
8095 Prefix => New_Reference_To (Thunk_Id, Loc),
8096 Attribute_Name => Name_Unrestricted_Access))),
8098 Build_Set_Predefined_Prim_Op_Address (Loc,
8099 Tag_Node =>
8100 New_Reference_To
8101 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
8102 Loc),
8103 Position => DT_Position (Prim),
8104 Address_Node =>
8105 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8106 Make_Attribute_Reference (Loc,
8107 Prefix => New_Reference_To (Prim, Loc),
8108 Attribute_Name => Name_Unrestricted_Access)))));
8109 end if;
8111 -- Skip the tag of the predefined primitives dispatch table
8113 Next_Elmt (Iface_DT_Ptr);
8114 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8116 -- Skip tag of the no-thunks dispatch table
8118 Next_Elmt (Iface_DT_Ptr);
8119 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8121 -- Skip tag of predefined primitives no-thunks dispatch table
8123 Next_Elmt (Iface_DT_Ptr);
8124 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8126 Next_Elmt (Iface_DT_Ptr);
8127 end loop;
8128 end Register_Predefined_DT_Entry;
8130 -- Local variables
8132 Subp : constant Entity_Id := Entity (N);
8134 -- Start of processing for Freeze_Subprogram
8136 begin
8137 -- We suppress the initialization of the dispatch table entry when
8138 -- VM_Target because the dispatching mechanism is handled internally
8139 -- by the VM.
8141 if Is_Dispatching_Operation (Subp)
8142 and then not Is_Abstract_Subprogram (Subp)
8143 and then Present (DTC_Entity (Subp))
8144 and then Present (Scope (DTC_Entity (Subp)))
8145 and then Tagged_Type_Expansion
8146 and then not Restriction_Active (No_Dispatching_Calls)
8147 and then RTE_Available (RE_Tag)
8148 then
8149 declare
8150 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
8152 begin
8153 -- Handle private overridden primitives
8155 if not Is_CPP_Class (Typ) then
8156 Check_Overriding_Operation (Subp);
8157 end if;
8159 -- We assume that imported CPP primitives correspond with objects
8160 -- whose constructor is in the CPP side; therefore we don't need
8161 -- to generate code to register them in the dispatch table.
8163 if Is_CPP_Class (Typ) then
8164 null;
8166 -- Handle CPP primitives found in derivations of CPP_Class types.
8167 -- These primitives must have been inherited from some parent, and
8168 -- there is no need to register them in the dispatch table because
8169 -- Build_Inherit_Prims takes care of the initialization of these
8170 -- slots.
8172 elsif Is_Imported (Subp)
8173 and then (Convention (Subp) = Convention_CPP
8174 or else Convention (Subp) = Convention_C)
8175 then
8176 null;
8178 -- Generate code to register the primitive in non statically
8179 -- allocated dispatch tables
8181 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
8183 -- When a primitive is frozen, enter its name in its dispatch
8184 -- table slot.
8186 if not Is_Interface (Typ)
8187 or else Present (Interface_Alias (Subp))
8188 then
8189 if Is_Predefined_Dispatching_Operation (Subp) then
8190 Register_Predefined_DT_Entry (Subp);
8191 end if;
8193 Insert_Actions_After (N,
8194 Register_Primitive (Loc, Prim => Subp));
8195 end if;
8196 end if;
8197 end;
8198 end if;
8200 -- Mark functions that return by reference. Note that it cannot be part
8201 -- of the normal semantic analysis of the spec since the underlying
8202 -- returned type may not be known yet (for private types).
8204 declare
8205 Typ : constant Entity_Id := Etype (Subp);
8206 Utyp : constant Entity_Id := Underlying_Type (Typ);
8207 begin
8208 if Is_Immutably_Limited_Type (Typ) then
8209 Set_Returns_By_Ref (Subp);
8210 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
8211 Set_Returns_By_Ref (Subp);
8212 end if;
8213 end;
8214 end Freeze_Subprogram;
8216 -----------------------
8217 -- Is_Null_Procedure --
8218 -----------------------
8220 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8221 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8223 begin
8224 if Ekind (Subp) /= E_Procedure then
8225 return False;
8227 -- Check if this is a declared null procedure
8229 elsif Nkind (Decl) = N_Subprogram_Declaration then
8230 if not Null_Present (Specification (Decl)) then
8231 return False;
8233 elsif No (Body_To_Inline (Decl)) then
8234 return False;
8236 -- Check if the body contains only a null statement, followed by
8237 -- the return statement added during expansion.
8239 else
8240 declare
8241 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8243 Stat : Node_Id;
8244 Stat2 : Node_Id;
8246 begin
8247 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8248 return False;
8249 else
8250 -- We must skip SCIL nodes because they are currently
8251 -- implemented as special N_Null_Statement nodes.
8253 Stat :=
8254 First_Non_SCIL_Node
8255 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8256 Stat2 := Next_Non_SCIL_Node (Stat);
8258 return
8259 Is_Empty_List (Declarations (Orig_Bod))
8260 and then Nkind (Stat) = N_Null_Statement
8261 and then
8262 (No (Stat2)
8263 or else
8264 (Nkind (Stat2) = N_Simple_Return_Statement
8265 and then No (Next (Stat2))));
8266 end if;
8267 end;
8268 end if;
8270 else
8271 return False;
8272 end if;
8273 end Is_Null_Procedure;
8275 -------------------------------------------
8276 -- Make_Build_In_Place_Call_In_Allocator --
8277 -------------------------------------------
8279 procedure Make_Build_In_Place_Call_In_Allocator
8280 (Allocator : Node_Id;
8281 Function_Call : Node_Id)
8283 Acc_Type : constant Entity_Id := Etype (Allocator);
8284 Loc : Source_Ptr;
8285 Func_Call : Node_Id := Function_Call;
8286 Function_Id : Entity_Id;
8287 Result_Subt : Entity_Id;
8288 New_Allocator : Node_Id;
8289 Return_Obj_Access : Entity_Id;
8291 begin
8292 -- Step past qualification or unchecked conversion (the latter can occur
8293 -- in cases of calls to 'Input).
8295 if Nkind_In (Func_Call,
8296 N_Qualified_Expression,
8297 N_Unchecked_Type_Conversion)
8298 then
8299 Func_Call := Expression (Func_Call);
8300 end if;
8302 -- If the call has already been processed to add build-in-place actuals
8303 -- then return. This should not normally occur in an allocator context,
8304 -- but we add the protection as a defensive measure.
8306 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8307 return;
8308 end if;
8310 -- Mark the call as processed as a build-in-place call
8312 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8314 Loc := Sloc (Function_Call);
8316 if Is_Entity_Name (Name (Func_Call)) then
8317 Function_Id := Entity (Name (Func_Call));
8319 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8320 Function_Id := Etype (Name (Func_Call));
8322 else
8323 raise Program_Error;
8324 end if;
8326 Result_Subt := Available_View (Etype (Function_Id));
8328 -- Check whether return type includes tasks. This may not have been done
8329 -- previously, if the type was a limited view.
8331 if Has_Task (Result_Subt) then
8332 Build_Activation_Chain_Entity (Allocator);
8333 end if;
8335 -- When the result subtype is constrained, the return object must be
8336 -- allocated on the caller side, and access to it is passed to the
8337 -- function.
8339 -- Here and in related routines, we must examine the full view of the
8340 -- type, because the view at the point of call may differ from that
8341 -- that in the function body, and the expansion mechanism depends on
8342 -- the characteristics of the full view.
8344 if Is_Constrained (Underlying_Type (Result_Subt)) then
8346 -- Replace the initialized allocator of form "new T'(Func (...))"
8347 -- with an uninitialized allocator of form "new T", where T is the
8348 -- result subtype of the called function. The call to the function
8349 -- is handled separately further below.
8351 New_Allocator :=
8352 Make_Allocator (Loc,
8353 Expression => New_Reference_To (Result_Subt, Loc));
8354 Set_No_Initialization (New_Allocator);
8356 -- Copy attributes to new allocator. Note that the new allocator
8357 -- logically comes from source if the original one did, so copy the
8358 -- relevant flag. This ensures proper treatment of the restriction
8359 -- No_Implicit_Heap_Allocations in this case.
8361 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8362 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8363 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8365 Rewrite (Allocator, New_Allocator);
8367 -- Create a new access object and initialize it to the result of the
8368 -- new uninitialized allocator. Note: we do not use Allocator as the
8369 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
8370 -- as this would create a sort of infinite "recursion".
8372 Return_Obj_Access := Make_Temporary (Loc, 'R');
8373 Set_Etype (Return_Obj_Access, Acc_Type);
8375 Insert_Action (Allocator,
8376 Make_Object_Declaration (Loc,
8377 Defining_Identifier => Return_Obj_Access,
8378 Object_Definition => New_Reference_To (Acc_Type, Loc),
8379 Expression => Relocate_Node (Allocator)));
8381 -- When the function has a controlling result, an allocation-form
8382 -- parameter must be passed indicating that the caller is allocating
8383 -- the result object. This is needed because such a function can be
8384 -- called as a dispatching operation and must be treated similarly
8385 -- to functions with unconstrained result subtypes.
8387 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8388 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8390 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8391 (Func_Call, Function_Id, Acc_Type);
8393 Add_Task_Actuals_To_Build_In_Place_Call
8394 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
8396 -- Add an implicit actual to the function call that provides access
8397 -- to the allocated object. An unchecked conversion to the (specific)
8398 -- result subtype of the function is inserted to handle cases where
8399 -- the access type of the allocator has a class-wide designated type.
8401 Add_Access_Actual_To_Build_In_Place_Call
8402 (Func_Call,
8403 Function_Id,
8404 Make_Unchecked_Type_Conversion (Loc,
8405 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
8406 Expression =>
8407 Make_Explicit_Dereference (Loc,
8408 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
8410 -- When the result subtype is unconstrained, the function itself must
8411 -- perform the allocation of the return object, so we pass parameters
8412 -- indicating that. We don't yet handle the case where the allocation
8413 -- must be done in a user-defined storage pool, which will require
8414 -- passing another actual or two to provide allocation/deallocation
8415 -- operations. ???
8417 else
8418 -- Case of a user-defined storage pool. Pass an allocation parameter
8419 -- indicating that the function should allocate its result in the
8420 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8421 -- pool may not be aliased.
8423 if VM_Target = No_VM
8424 and then Present (Associated_Storage_Pool (Acc_Type))
8425 then
8426 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8427 (Func_Call, Function_Id, Alloc_Form => User_Storage_Pool,
8428 Pool_Actual =>
8429 Make_Attribute_Reference (Loc,
8430 Prefix =>
8431 New_Reference_To
8432 (Associated_Storage_Pool (Acc_Type), Loc),
8433 Attribute_Name => Name_Unrestricted_Access));
8435 -- No user-defined pool; pass an allocation parameter indicating that
8436 -- the function should allocate its result on the heap.
8438 else
8439 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8440 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
8441 end if;
8443 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8444 (Func_Call, Function_Id, Acc_Type);
8446 Add_Task_Actuals_To_Build_In_Place_Call
8447 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
8449 -- The caller does not provide the return object in this case, so we
8450 -- have to pass null for the object access actual.
8452 Add_Access_Actual_To_Build_In_Place_Call
8453 (Func_Call, Function_Id, Return_Object => Empty);
8454 end if;
8456 -- If the build-in-place function call returns a controlled object,
8457 -- the finalization master will require a reference to routine
8458 -- Finalize_Address of the designated type. Setting this attribute
8459 -- is done in the same manner to expansion of allocators.
8461 if Needs_Finalization (Result_Subt) then
8463 -- Controlled types with supressed finalization do not need to
8464 -- associate the address of their Finalize_Address primitives with
8465 -- a master since they do not need a master to begin with.
8467 if Is_Library_Level_Entity (Acc_Type)
8468 and then Finalize_Storage_Only (Result_Subt)
8469 then
8470 null;
8472 -- Do not generate the call to Set_Finalize_Address in Alfa mode
8473 -- because it is not necessary and results in unwanted expansion.
8474 -- This expansion is also not carried out in CodePeer mode because
8475 -- Finalize_Address is never built.
8477 elsif not Alfa_Mode
8478 and then not CodePeer_Mode
8479 then
8480 Insert_Action (Allocator,
8481 Make_Set_Finalize_Address_Call (Loc,
8482 Typ => Etype (Function_Id),
8483 Ptr_Typ => Acc_Type));
8484 end if;
8485 end if;
8487 -- Finally, replace the allocator node with a reference to the result
8488 -- of the function call itself (which will effectively be an access
8489 -- to the object created by the allocator).
8491 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
8493 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
8494 -- generate an implicit conversion to force displacement of the "this"
8495 -- pointer.
8497 if Is_Interface (Designated_Type (Acc_Type)) then
8498 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
8499 end if;
8501 Analyze_And_Resolve (Allocator, Acc_Type);
8502 end Make_Build_In_Place_Call_In_Allocator;
8504 ---------------------------------------------------
8505 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8506 ---------------------------------------------------
8508 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8509 (Function_Call : Node_Id)
8511 Loc : Source_Ptr;
8512 Func_Call : Node_Id := Function_Call;
8513 Function_Id : Entity_Id;
8514 Result_Subt : Entity_Id;
8515 Return_Obj_Id : Entity_Id;
8516 Return_Obj_Decl : Entity_Id;
8518 begin
8519 -- Step past qualification or unchecked conversion (the latter can occur
8520 -- in cases of calls to 'Input).
8522 if Nkind_In (Func_Call, N_Qualified_Expression,
8523 N_Unchecked_Type_Conversion)
8524 then
8525 Func_Call := Expression (Func_Call);
8526 end if;
8528 -- If the call has already been processed to add build-in-place actuals
8529 -- then return. One place this can occur is for calls to build-in-place
8530 -- functions that occur within a call to a protected operation, where
8531 -- due to rewriting and expansion of the protected call there can be
8532 -- more than one call to Expand_Actuals for the same set of actuals.
8534 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8535 return;
8536 end if;
8538 -- Mark the call as processed as a build-in-place call
8540 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8542 Loc := Sloc (Function_Call);
8544 if Is_Entity_Name (Name (Func_Call)) then
8545 Function_Id := Entity (Name (Func_Call));
8547 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8548 Function_Id := Etype (Name (Func_Call));
8550 else
8551 raise Program_Error;
8552 end if;
8554 Result_Subt := Etype (Function_Id);
8556 -- If the build-in-place function returns a controlled object, then the
8557 -- object needs to be finalized immediately after the context. Since
8558 -- this case produces a transient scope, the servicing finalizer needs
8559 -- to name the returned object. Create a temporary which is initialized
8560 -- with the function call:
8562 -- Temp_Id : Func_Type := BIP_Func_Call;
8564 -- The initialization expression of the temporary will be rewritten by
8565 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8566 -- Call_In_Object_Declaration.
8568 if Needs_Finalization (Result_Subt) then
8569 declare
8570 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8571 Temp_Decl : Node_Id;
8573 begin
8574 -- Reset the guard on the function call since the following does
8575 -- not perform actual call expansion.
8577 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8579 Temp_Decl :=
8580 Make_Object_Declaration (Loc,
8581 Defining_Identifier => Temp_Id,
8582 Object_Definition =>
8583 New_Reference_To (Result_Subt, Loc),
8584 Expression =>
8585 New_Copy_Tree (Function_Call));
8587 Insert_Action (Function_Call, Temp_Decl);
8589 Rewrite (Function_Call, New_Reference_To (Temp_Id, Loc));
8590 Analyze (Function_Call);
8591 end;
8593 -- When the result subtype is constrained, an object of the subtype is
8594 -- declared and an access value designating it is passed as an actual.
8596 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
8598 -- Create a temporary object to hold the function result
8600 Return_Obj_Id := Make_Temporary (Loc, 'R');
8601 Set_Etype (Return_Obj_Id, Result_Subt);
8603 Return_Obj_Decl :=
8604 Make_Object_Declaration (Loc,
8605 Defining_Identifier => Return_Obj_Id,
8606 Aliased_Present => True,
8607 Object_Definition => New_Reference_To (Result_Subt, Loc));
8609 Set_No_Initialization (Return_Obj_Decl);
8611 Insert_Action (Func_Call, Return_Obj_Decl);
8613 -- When the function has a controlling result, an allocation-form
8614 -- parameter must be passed indicating that the caller is allocating
8615 -- the result object. This is needed because such a function can be
8616 -- called as a dispatching operation and must be treated similarly
8617 -- to functions with unconstrained result subtypes.
8619 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8620 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8622 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8623 (Func_Call, Function_Id);
8625 Add_Task_Actuals_To_Build_In_Place_Call
8626 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8628 -- Add an implicit actual to the function call that provides access
8629 -- to the caller's return object.
8631 Add_Access_Actual_To_Build_In_Place_Call
8632 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
8634 -- When the result subtype is unconstrained, the function must allocate
8635 -- the return object in the secondary stack, so appropriate implicit
8636 -- parameters are added to the call to indicate that. A transient
8637 -- scope is established to ensure eventual cleanup of the result.
8639 else
8640 -- Pass an allocation parameter indicating that the function should
8641 -- allocate its result on the secondary stack.
8643 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8644 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8646 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8647 (Func_Call, Function_Id);
8649 Add_Task_Actuals_To_Build_In_Place_Call
8650 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8652 -- Pass a null value to the function since no return object is
8653 -- available on the caller side.
8655 Add_Access_Actual_To_Build_In_Place_Call
8656 (Func_Call, Function_Id, Empty);
8657 end if;
8658 end Make_Build_In_Place_Call_In_Anonymous_Context;
8660 --------------------------------------------
8661 -- Make_Build_In_Place_Call_In_Assignment --
8662 --------------------------------------------
8664 procedure Make_Build_In_Place_Call_In_Assignment
8665 (Assign : Node_Id;
8666 Function_Call : Node_Id)
8668 Lhs : constant Node_Id := Name (Assign);
8669 Func_Call : Node_Id := Function_Call;
8670 Func_Id : Entity_Id;
8671 Loc : Source_Ptr;
8672 Obj_Decl : Node_Id;
8673 Obj_Id : Entity_Id;
8674 Ptr_Typ : Entity_Id;
8675 Ptr_Typ_Decl : Node_Id;
8676 New_Expr : Node_Id;
8677 Result_Subt : Entity_Id;
8678 Target : Node_Id;
8680 begin
8681 -- Step past qualification or unchecked conversion (the latter can occur
8682 -- in cases of calls to 'Input).
8684 if Nkind_In (Func_Call, N_Qualified_Expression,
8685 N_Unchecked_Type_Conversion)
8686 then
8687 Func_Call := Expression (Func_Call);
8688 end if;
8690 -- If the call has already been processed to add build-in-place actuals
8691 -- then return. This should not normally occur in an assignment context,
8692 -- but we add the protection as a defensive measure.
8694 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8695 return;
8696 end if;
8698 -- Mark the call as processed as a build-in-place call
8700 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8702 Loc := Sloc (Function_Call);
8704 if Is_Entity_Name (Name (Func_Call)) then
8705 Func_Id := Entity (Name (Func_Call));
8707 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8708 Func_Id := Etype (Name (Func_Call));
8710 else
8711 raise Program_Error;
8712 end if;
8714 Result_Subt := Etype (Func_Id);
8716 -- When the result subtype is unconstrained, an additional actual must
8717 -- be passed to indicate that the caller is providing the return object.
8718 -- This parameter must also be passed when the called function has a
8719 -- controlling result, because dispatching calls to the function needs
8720 -- to be treated effectively the same as calls to class-wide functions.
8722 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8723 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8725 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8726 (Func_Call, Func_Id);
8728 Add_Task_Actuals_To_Build_In_Place_Call
8729 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8731 -- Add an implicit actual to the function call that provides access to
8732 -- the caller's return object.
8734 Add_Access_Actual_To_Build_In_Place_Call
8735 (Func_Call,
8736 Func_Id,
8737 Make_Unchecked_Type_Conversion (Loc,
8738 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
8739 Expression => Relocate_Node (Lhs)));
8741 -- Create an access type designating the function's result subtype
8743 Ptr_Typ := Make_Temporary (Loc, 'A');
8745 Ptr_Typ_Decl :=
8746 Make_Full_Type_Declaration (Loc,
8747 Defining_Identifier => Ptr_Typ,
8748 Type_Definition =>
8749 Make_Access_To_Object_Definition (Loc,
8750 All_Present => True,
8751 Subtype_Indication =>
8752 New_Reference_To (Result_Subt, Loc)));
8753 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8755 -- Finally, create an access object initialized to a reference to the
8756 -- function call. We know this access value is non-null, so mark the
8757 -- entity accordingly to suppress junk access checks.
8759 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8761 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8762 Set_Etype (Obj_Id, Ptr_Typ);
8763 Set_Is_Known_Non_Null (Obj_Id);
8765 Obj_Decl :=
8766 Make_Object_Declaration (Loc,
8767 Defining_Identifier => Obj_Id,
8768 Object_Definition => New_Reference_To (Ptr_Typ, Loc),
8769 Expression => New_Expr);
8770 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8772 Rewrite (Assign, Make_Null_Statement (Loc));
8774 -- Retrieve the target of the assignment
8776 if Nkind (Lhs) = N_Selected_Component then
8777 Target := Selector_Name (Lhs);
8778 elsif Nkind (Lhs) = N_Type_Conversion then
8779 Target := Expression (Lhs);
8780 else
8781 Target := Lhs;
8782 end if;
8784 -- If we are assigning to a return object or this is an expression of
8785 -- an extension aggregate, the target should either be an identifier
8786 -- or a simple expression. All other cases imply a different scenario.
8788 if Nkind (Target) in N_Has_Entity then
8789 Target := Entity (Target);
8790 else
8791 return;
8792 end if;
8793 end Make_Build_In_Place_Call_In_Assignment;
8795 ----------------------------------------------------
8796 -- Make_Build_In_Place_Call_In_Object_Declaration --
8797 ----------------------------------------------------
8799 procedure Make_Build_In_Place_Call_In_Object_Declaration
8800 (Object_Decl : Node_Id;
8801 Function_Call : Node_Id)
8803 Loc : Source_Ptr;
8804 Obj_Def_Id : constant Entity_Id :=
8805 Defining_Identifier (Object_Decl);
8806 Enclosing_Func : constant Entity_Id :=
8807 Enclosing_Subprogram (Obj_Def_Id);
8808 Call_Deref : Node_Id;
8809 Caller_Object : Node_Id;
8810 Def_Id : Entity_Id;
8811 Fmaster_Actual : Node_Id := Empty;
8812 Func_Call : Node_Id := Function_Call;
8813 Function_Id : Entity_Id;
8814 Pool_Actual : Node_Id;
8815 Ptr_Typ_Decl : Node_Id;
8816 Pass_Caller_Acc : Boolean := False;
8817 New_Expr : Node_Id;
8818 Ref_Type : Entity_Id;
8819 Result_Subt : Entity_Id;
8821 begin
8822 -- Step past qualification or unchecked conversion (the latter can occur
8823 -- in cases of calls to 'Input).
8825 if Nkind_In (Func_Call, N_Qualified_Expression,
8826 N_Unchecked_Type_Conversion)
8827 then
8828 Func_Call := Expression (Func_Call);
8829 end if;
8831 -- If the call has already been processed to add build-in-place actuals
8832 -- then return. This should not normally occur in an object declaration,
8833 -- but we add the protection as a defensive measure.
8835 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8836 return;
8837 end if;
8839 -- Mark the call as processed as a build-in-place call
8841 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8843 Loc := Sloc (Function_Call);
8845 if Is_Entity_Name (Name (Func_Call)) then
8846 Function_Id := Entity (Name (Func_Call));
8848 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8849 Function_Id := Etype (Name (Func_Call));
8851 else
8852 raise Program_Error;
8853 end if;
8855 Result_Subt := Etype (Function_Id);
8857 -- If the the object is a return object of an enclosing build-in-place
8858 -- function, then the implicit build-in-place parameters of the
8859 -- enclosing function are simply passed along to the called function.
8860 -- (Unfortunately, this won't cover the case of extension aggregates
8861 -- where the ancestor part is a build-in-place unconstrained function
8862 -- call that should be passed along the caller's parameters. Currently
8863 -- those get mishandled by reassigning the result of the call to the
8864 -- aggregate return object, when the call result should really be
8865 -- directly built in place in the aggregate and not in a temporary. ???)
8867 if Is_Return_Object (Defining_Identifier (Object_Decl)) then
8868 Pass_Caller_Acc := True;
8870 -- When the enclosing function has a BIP_Alloc_Form formal then we
8871 -- pass it along to the callee (such as when the enclosing function
8872 -- has an unconstrained or tagged result type).
8874 if Needs_BIP_Alloc_Form (Enclosing_Func) then
8875 if VM_Target = No_VM and then
8876 RTE_Available (RE_Root_Storage_Pool_Ptr)
8877 then
8878 Pool_Actual :=
8879 New_Reference_To (Build_In_Place_Formal
8880 (Enclosing_Func, BIP_Storage_Pool), Loc);
8882 -- The build-in-place pool formal is not built on .NET/JVM
8884 else
8885 Pool_Actual := Empty;
8886 end if;
8888 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8889 (Func_Call,
8890 Function_Id,
8891 Alloc_Form_Exp =>
8892 New_Reference_To
8893 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
8894 Loc),
8895 Pool_Actual => Pool_Actual);
8897 -- Otherwise, if enclosing function has a constrained result subtype,
8898 -- then caller allocation will be used.
8900 else
8901 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8902 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8903 end if;
8905 if Needs_BIP_Finalization_Master (Enclosing_Func) then
8906 Fmaster_Actual :=
8907 New_Reference_To
8908 (Build_In_Place_Formal
8909 (Enclosing_Func, BIP_Finalization_Master), Loc);
8910 end if;
8912 -- Retrieve the BIPacc formal from the enclosing function and convert
8913 -- it to the access type of the callee's BIP_Object_Access formal.
8915 Caller_Object :=
8916 Make_Unchecked_Type_Conversion (Loc,
8917 Subtype_Mark =>
8918 New_Reference_To
8919 (Etype
8920 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
8921 Loc),
8922 Expression =>
8923 New_Reference_To
8924 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
8925 Loc));
8927 -- In the constrained case, add an implicit actual to the function call
8928 -- that provides access to the declared object. An unchecked conversion
8929 -- to the (specific) result type of the function is inserted to handle
8930 -- the case where the object is declared with a class-wide type.
8932 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
8933 Caller_Object :=
8934 Make_Unchecked_Type_Conversion (Loc,
8935 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
8936 Expression => New_Reference_To (Obj_Def_Id, Loc));
8938 -- When the function has a controlling result, an allocation-form
8939 -- parameter must be passed indicating that the caller is allocating
8940 -- the result object. This is needed because such a function can be
8941 -- called as a dispatching operation and must be treated similarly
8942 -- to functions with unconstrained result subtypes.
8944 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8945 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8947 -- In other unconstrained cases, pass an indication to do the allocation
8948 -- on the secondary stack and set Caller_Object to Empty so that a null
8949 -- value will be passed for the caller's object address. A transient
8950 -- scope is established to ensure eventual cleanup of the result.
8952 else
8953 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8954 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8955 Caller_Object := Empty;
8957 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
8958 end if;
8960 -- Pass along any finalization master actual, which is needed in the
8961 -- case where the called function initializes a return object of an
8962 -- enclosing build-in-place function.
8964 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8965 (Func_Call => Func_Call,
8966 Func_Id => Function_Id,
8967 Master_Exp => Fmaster_Actual);
8969 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
8970 and then Has_Task (Result_Subt)
8971 then
8972 -- Here we're passing along the master that was passed in to this
8973 -- function.
8975 Add_Task_Actuals_To_Build_In_Place_Call
8976 (Func_Call, Function_Id,
8977 Master_Actual =>
8978 New_Reference_To (Build_In_Place_Formal
8979 (Enclosing_Func, BIP_Task_Master), Loc));
8981 else
8982 Add_Task_Actuals_To_Build_In_Place_Call
8983 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8984 end if;
8986 Add_Access_Actual_To_Build_In_Place_Call
8987 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
8989 -- Create an access type designating the function's result subtype. We
8990 -- use the type of the original expression because it may be a call to
8991 -- an inherited operation, which the expansion has replaced with the
8992 -- parent operation that yields the parent type.
8994 Ref_Type := Make_Temporary (Loc, 'A');
8996 Ptr_Typ_Decl :=
8997 Make_Full_Type_Declaration (Loc,
8998 Defining_Identifier => Ref_Type,
8999 Type_Definition =>
9000 Make_Access_To_Object_Definition (Loc,
9001 All_Present => True,
9002 Subtype_Indication =>
9003 New_Reference_To (Etype (Function_Call), Loc)));
9005 -- The access type and its accompanying object must be inserted after
9006 -- the object declaration in the constrained case, so that the function
9007 -- call can be passed access to the object. In the unconstrained case,
9008 -- or if the object declaration is for a return object, the access type
9009 -- and object must be inserted before the object, since the object
9010 -- declaration is rewritten to be a renaming of a dereference of the
9011 -- access object.
9013 if Is_Constrained (Underlying_Type (Result_Subt))
9014 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
9015 then
9016 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
9017 else
9018 Insert_Action (Object_Decl, Ptr_Typ_Decl);
9019 end if;
9021 -- Finally, create an access object initialized to a reference to the
9022 -- function call. We know this access value cannot be null, so mark the
9023 -- entity accordingly to suppress the access check.
9025 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
9027 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
9028 Set_Etype (Def_Id, Ref_Type);
9029 Set_Is_Known_Non_Null (Def_Id);
9031 Insert_After_And_Analyze (Ptr_Typ_Decl,
9032 Make_Object_Declaration (Loc,
9033 Defining_Identifier => Def_Id,
9034 Object_Definition => New_Reference_To (Ref_Type, Loc),
9035 Expression => New_Expr));
9037 -- If the result subtype of the called function is constrained and
9038 -- is not itself the return expression of an enclosing BIP function,
9039 -- then mark the object as having no initialization.
9041 if Is_Constrained (Underlying_Type (Result_Subt))
9042 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
9043 then
9044 Set_Expression (Object_Decl, Empty);
9045 Set_No_Initialization (Object_Decl);
9047 -- In case of an unconstrained result subtype, or if the call is the
9048 -- return expression of an enclosing BIP function, rewrite the object
9049 -- declaration as an object renaming where the renamed object is a
9050 -- dereference of <function_Call>'reference:
9052 -- Obj : Subt renames <function_call>'Ref.all;
9054 else
9055 Call_Deref :=
9056 Make_Explicit_Dereference (Loc,
9057 Prefix => New_Reference_To (Def_Id, Loc));
9059 Loc := Sloc (Object_Decl);
9060 Rewrite (Object_Decl,
9061 Make_Object_Renaming_Declaration (Loc,
9062 Defining_Identifier => Make_Temporary (Loc, 'D'),
9063 Access_Definition => Empty,
9064 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9065 Name => Call_Deref));
9067 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
9069 Analyze (Object_Decl);
9071 -- Replace the internal identifier of the renaming declaration's
9072 -- entity with identifier of the original object entity. We also have
9073 -- to exchange the entities containing their defining identifiers to
9074 -- ensure the correct replacement of the object declaration by the
9075 -- object renaming declaration to avoid homograph conflicts (since
9076 -- the object declaration's defining identifier was already entered
9077 -- in current scope). The Next_Entity links of the two entities also
9078 -- have to be swapped since the entities are part of the return
9079 -- scope's entity list and the list structure would otherwise be
9080 -- corrupted. Finally, the homonym chain must be preserved as well.
9082 declare
9083 Renaming_Def_Id : constant Entity_Id :=
9084 Defining_Identifier (Object_Decl);
9085 Next_Entity_Temp : constant Entity_Id :=
9086 Next_Entity (Renaming_Def_Id);
9087 begin
9088 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
9090 -- Swap next entity links in preparation for exchanging entities
9092 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
9093 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
9094 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
9096 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
9098 -- Preserve source indication of original declaration, so that
9099 -- xref information is properly generated for the right entity.
9101 Preserve_Comes_From_Source
9102 (Object_Decl, Original_Node (Object_Decl));
9104 Preserve_Comes_From_Source
9105 (Obj_Def_Id, Original_Node (Object_Decl));
9107 Set_Comes_From_Source (Renaming_Def_Id, False);
9108 end;
9109 end if;
9111 -- If the object entity has a class-wide Etype, then we need to change
9112 -- it to the result subtype of the function call, because otherwise the
9113 -- object will be class-wide without an explicit initialization and
9114 -- won't be allocated properly by the back end. It seems unclean to make
9115 -- such a revision to the type at this point, and we should try to
9116 -- improve this treatment when build-in-place functions with class-wide
9117 -- results are implemented. ???
9119 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
9120 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
9121 end if;
9122 end Make_Build_In_Place_Call_In_Object_Declaration;
9124 --------------------------------------------
9125 -- Make_CPP_Constructor_Call_In_Allocator --
9126 --------------------------------------------
9128 procedure Make_CPP_Constructor_Call_In_Allocator
9129 (Allocator : Node_Id;
9130 Function_Call : Node_Id)
9132 Loc : constant Source_Ptr := Sloc (Function_Call);
9133 Acc_Type : constant Entity_Id := Etype (Allocator);
9134 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9135 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9137 New_Allocator : Node_Id;
9138 Return_Obj_Access : Entity_Id;
9139 Tmp_Obj : Node_Id;
9141 begin
9142 pragma Assert (Nkind (Allocator) = N_Allocator
9143 and then Nkind (Function_Call) = N_Function_Call);
9144 pragma Assert (Convention (Function_Id) = Convention_CPP
9145 and then Is_Constructor (Function_Id));
9146 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9148 -- Replace the initialized allocator of form "new T'(Func (...))" with
9149 -- an uninitialized allocator of form "new T", where T is the result
9150 -- subtype of the called function. The call to the function is handled
9151 -- separately further below.
9153 New_Allocator :=
9154 Make_Allocator (Loc,
9155 Expression => New_Reference_To (Result_Subt, Loc));
9156 Set_No_Initialization (New_Allocator);
9158 -- Copy attributes to new allocator. Note that the new allocator
9159 -- logically comes from source if the original one did, so copy the
9160 -- relevant flag. This ensures proper treatment of the restriction
9161 -- No_Implicit_Heap_Allocations in this case.
9163 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9164 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9165 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9167 Rewrite (Allocator, New_Allocator);
9169 -- Create a new access object and initialize it to the result of the
9170 -- new uninitialized allocator. Note: we do not use Allocator as the
9171 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9172 -- as this would create a sort of infinite "recursion".
9174 Return_Obj_Access := Make_Temporary (Loc, 'R');
9175 Set_Etype (Return_Obj_Access, Acc_Type);
9177 -- Generate:
9178 -- Rnnn : constant ptr_T := new (T);
9179 -- Init (Rnn.all,...);
9181 Tmp_Obj :=
9182 Make_Object_Declaration (Loc,
9183 Defining_Identifier => Return_Obj_Access,
9184 Constant_Present => True,
9185 Object_Definition => New_Reference_To (Acc_Type, Loc),
9186 Expression => Relocate_Node (Allocator));
9187 Insert_Action (Allocator, Tmp_Obj);
9189 Insert_List_After_And_Analyze (Tmp_Obj,
9190 Build_Initialization_Call (Loc,
9191 Id_Ref =>
9192 Make_Explicit_Dereference (Loc,
9193 Prefix => New_Reference_To (Return_Obj_Access, Loc)),
9194 Typ => Etype (Function_Id),
9195 Constructor_Ref => Function_Call));
9197 -- Finally, replace the allocator node with a reference to the result of
9198 -- the function call itself (which will effectively be an access to the
9199 -- object created by the allocator).
9201 Rewrite (Allocator, New_Reference_To (Return_Obj_Access, Loc));
9203 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9204 -- generate an implicit conversion to force displacement of the "this"
9205 -- pointer.
9207 if Is_Interface (Designated_Type (Acc_Type)) then
9208 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9209 end if;
9211 Analyze_And_Resolve (Allocator, Acc_Type);
9212 end Make_CPP_Constructor_Call_In_Allocator;
9214 -----------------------------------
9215 -- Needs_BIP_Finalization_Master --
9216 -----------------------------------
9218 function Needs_BIP_Finalization_Master
9219 (Func_Id : Entity_Id) return Boolean
9221 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9222 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9223 begin
9224 return
9225 not Restriction_Active (No_Finalization)
9226 and then Needs_Finalization (Func_Typ);
9227 end Needs_BIP_Finalization_Master;
9229 --------------------------
9230 -- Needs_BIP_Alloc_Form --
9231 --------------------------
9233 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9234 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9235 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9236 begin
9237 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
9238 end Needs_BIP_Alloc_Form;
9240 --------------------------------------
9241 -- Needs_Result_Accessibility_Level --
9242 --------------------------------------
9244 function Needs_Result_Accessibility_Level
9245 (Func_Id : Entity_Id) return Boolean
9247 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9249 function Has_Unconstrained_Access_Discriminant_Component
9250 (Comp_Typ : Entity_Id) return Boolean;
9251 -- Returns True if any component of the type has an unconstrained access
9252 -- discriminant.
9254 -----------------------------------------------------
9255 -- Has_Unconstrained_Access_Discriminant_Component --
9256 -----------------------------------------------------
9258 function Has_Unconstrained_Access_Discriminant_Component
9259 (Comp_Typ : Entity_Id) return Boolean
9261 begin
9262 if not Is_Limited_Type (Comp_Typ) then
9263 return False;
9265 -- Only limited types can have access discriminants with
9266 -- defaults.
9268 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
9269 return True;
9271 elsif Is_Array_Type (Comp_Typ) then
9272 return Has_Unconstrained_Access_Discriminant_Component
9273 (Underlying_Type (Component_Type (Comp_Typ)));
9275 elsif Is_Record_Type (Comp_Typ) then
9276 declare
9277 Comp : Entity_Id;
9279 begin
9280 Comp := First_Component (Comp_Typ);
9281 while Present (Comp) loop
9282 if Has_Unconstrained_Access_Discriminant_Component
9283 (Underlying_Type (Etype (Comp)))
9284 then
9285 return True;
9286 end if;
9288 Next_Component (Comp);
9289 end loop;
9290 end;
9291 end if;
9293 return False;
9294 end Has_Unconstrained_Access_Discriminant_Component;
9296 Feature_Disabled : constant Boolean := True;
9297 -- Temporary
9299 -- Start of processing for Needs_Result_Accessibility_Level
9301 begin
9302 -- False if completion unavailable (how does this happen???)
9304 if not Present (Func_Typ) then
9305 return False;
9307 elsif Feature_Disabled then
9308 return False;
9310 -- False if not a function, also handle enum-lit renames case
9312 elsif Func_Typ = Standard_Void_Type
9313 or else Is_Scalar_Type (Func_Typ)
9314 then
9315 return False;
9317 -- Handle a corner case, a cross-dialect subp renaming. For example,
9318 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9319 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9321 elsif Present (Alias (Func_Id)) then
9323 -- Unimplemented: a cross-dialect subp renaming which does not set
9324 -- the Alias attribute (e.g., a rename of a dereference of an access
9325 -- to subprogram value). ???
9327 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
9329 -- Remaining cases require Ada 2012 mode
9331 elsif Ada_Version < Ada_2012 then
9332 return False;
9334 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
9335 or else Is_Tagged_Type (Func_Typ)
9336 then
9337 -- In the case of, say, a null tagged record result type, the need
9338 -- for this extra parameter might not be obvious. This function
9339 -- returns True for all tagged types for compatibility reasons.
9340 -- A function with, say, a tagged null controlling result type might
9341 -- be overridden by a primitive of an extension having an access
9342 -- discriminant and the overrider and overridden must have compatible
9343 -- calling conventions (including implicitly declared parameters).
9344 -- Similarly, values of one access-to-subprogram type might designate
9345 -- both a primitive subprogram of a given type and a function
9346 -- which is, for example, not a primitive subprogram of any type.
9347 -- Again, this requires calling convention compatibility.
9348 -- It might be possible to solve these issues by introducing
9349 -- wrappers, but that is not the approach that was chosen.
9351 return True;
9353 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
9354 return True;
9356 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
9357 return True;
9359 -- False for all other cases
9361 else
9362 return False;
9363 end if;
9364 end Needs_Result_Accessibility_Level;
9366 ------------------------
9367 -- List_Inlining_Info --
9368 ------------------------
9370 procedure List_Inlining_Info is
9371 Elmt : Elmt_Id;
9372 Nod : Node_Id;
9373 Count : Nat;
9375 begin
9376 if not Debug_Flag_Dot_J then
9377 return;
9378 end if;
9380 -- Generate listing of calls inlined by the frontend
9382 if Present (Inlined_Calls) then
9383 Count := 0;
9384 Elmt := First_Elmt (Inlined_Calls);
9385 while Present (Elmt) loop
9386 Nod := Node (Elmt);
9388 if In_Extended_Main_Code_Unit (Nod) then
9389 Count := Count + 1;
9391 if Count = 1 then
9392 Write_Str ("Listing of frontend inlined calls");
9393 Write_Eol;
9394 end if;
9396 Write_Str (" ");
9397 Write_Int (Count);
9398 Write_Str (":");
9399 Write_Location (Sloc (Nod));
9400 Write_Str (":");
9401 Output.Write_Eol;
9402 end if;
9404 Next_Elmt (Elmt);
9405 end loop;
9406 end if;
9408 -- Generate listing of calls passed to the backend
9410 if Present (Backend_Calls) then
9411 Count := 0;
9413 Elmt := First_Elmt (Backend_Calls);
9414 while Present (Elmt) loop
9415 Nod := Node (Elmt);
9417 if In_Extended_Main_Code_Unit (Nod) then
9418 Count := Count + 1;
9420 if Count = 1 then
9421 Write_Str ("Listing of inlined calls passed to the backend");
9422 Write_Eol;
9423 end if;
9425 Write_Str (" ");
9426 Write_Int (Count);
9427 Write_Str (":");
9428 Write_Location (Sloc (Nod));
9429 Output.Write_Eol;
9430 end if;
9432 Next_Elmt (Elmt);
9433 end loop;
9434 end if;
9435 end List_Inlining_Info;
9437 end Exp_Ch6;