2ffa9f7906c4b63da4ec5a7ef3f5bde40a26d5a5
[gcc.git] / gcc / ada / exp_ch6.adb
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-2010, 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 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Elists; use Elists;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch2; use Exp_Ch2;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Intr; use Exp_Intr;
41 with Exp_Pakd; use Exp_Pakd;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Exp_VFpt; use Exp_VFpt;
45 with Fname; use Fname;
46 with Freeze; use Freeze;
47 with Inline; use Inline;
48 with Lib; use Lib;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sem; use Sem;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch12; use Sem_Ch12;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Mech; use Sem_Mech;
66 with Sem_Res; use Sem_Res;
67 with Sem_SCIL; use Sem_SCIL;
68 with Sem_Util; use Sem_Util;
69 with Sinfo; use Sinfo;
70 with Snames; use Snames;
71 with Stand; use Stand;
72 with Targparm; use Targparm;
73 with Tbuild; use Tbuild;
74 with Uintp; use Uintp;
75 with Validsw; use Validsw;
76
77 package body Exp_Ch6 is
78
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
82
83 procedure Add_Access_Actual_To_Build_In_Place_Call
84 (Function_Call : Node_Id;
85 Function_Id : Entity_Id;
86 Return_Object : Node_Id;
87 Is_Access : Boolean := False);
88 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
89 -- object name given by Return_Object and add the attribute to the end of
90 -- the actual parameter list associated with the build-in-place function
91 -- call denoted by Function_Call. However, if Is_Access is True, then
92 -- Return_Object is already an access expression, in which case it's passed
93 -- along directly to the build-in-place function. Finally, if Return_Object
94 -- is empty, then pass a null literal as the actual.
95
96 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
97 (Function_Call : Node_Id;
98 Function_Id : Entity_Id;
99 Alloc_Form : BIP_Allocation_Form := Unspecified;
100 Alloc_Form_Exp : Node_Id := Empty);
101 -- Ada 2005 (AI-318-02): Add an actual indicating the form of allocation,
102 -- if any, to be done by a build-in-place function. If Alloc_Form_Exp is
103 -- present, then use it, otherwise pass a literal corresponding to the
104 -- Alloc_Form parameter (which must not be Unspecified in that case).
105
106 procedure Add_Extra_Actual_To_Call
107 (Subprogram_Call : Node_Id;
108 Extra_Formal : Entity_Id;
109 Extra_Actual : Node_Id);
110 -- Adds Extra_Actual as a named parameter association for the formal
111 -- Extra_Formal in Subprogram_Call.
112
113 procedure Add_Final_List_Actual_To_Build_In_Place_Call
114 (Function_Call : Node_Id;
115 Function_Id : Entity_Id;
116 Acc_Type : Entity_Id;
117 Sel_Comp : Node_Id := Empty);
118 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type has
119 -- controlled parts, add an actual parameter that is a pointer to
120 -- appropriate finalization list. The finalization list is that of the
121 -- current scope, except for "new Acc'(F(...))" in which case it's the
122 -- finalization list of the access type returned by the allocator. Acc_Type
123 -- is that type in the allocator case; Empty otherwise. If Sel_Comp is
124 -- not Empty, then it denotes a selected component and the finalization
125 -- list is obtained from the _controller list of the prefix object.
126
127 procedure Add_Task_Actuals_To_Build_In_Place_Call
128 (Function_Call : Node_Id;
129 Function_Id : Entity_Id;
130 Master_Actual : Node_Id);
131 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
132 -- contains tasks, add two actual parameters: the master, and a pointer to
133 -- the caller's activation chain. Master_Actual is the actual parameter
134 -- expression to pass for the master. In most cases, this is the current
135 -- master (_master). The two exceptions are: If the function call is the
136 -- initialization expression for an allocator, we pass the master of the
137 -- access type. If the function call is the initialization expression for a
138 -- return object, we pass along the master passed in by the caller. The
139 -- activation chain to pass is always the local one. Note: Master_Actual
140 -- can be Empty, but only if there are no tasks
141
142 procedure Check_Overriding_Operation (Subp : Entity_Id);
143 -- Subp is a dispatching operation. Check whether it may override an
144 -- inherited private operation, in which case its DT entry is that of
145 -- the hidden operation, not the one it may have received earlier.
146 -- This must be done before emitting the code to set the corresponding
147 -- DT to the address of the subprogram. The actual placement of Subp in
148 -- the proper place in the list of primitive operations is done in
149 -- Declare_Inherited_Private_Subprograms, which also has to deal with
150 -- implicit operations. This duplication is unavoidable for now???
151
152 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
153 -- This procedure is called only if the subprogram body N, whose spec
154 -- has the given entity Spec, contains a parameterless recursive call.
155 -- It attempts to generate runtime code to detect if this a case of
156 -- infinite recursion.
157 --
158 -- The body is scanned to determine dependencies. If the only external
159 -- dependencies are on a small set of scalar variables, then the values
160 -- of these variables are captured on entry to the subprogram, and if
161 -- the values are not changed for the call, we know immediately that
162 -- we have an infinite recursion.
163
164 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id);
165 -- For each actual of an in-out or out parameter which is a numeric
166 -- (view) conversion of the form T (A), where A denotes a variable,
167 -- we insert the declaration:
168 --
169 -- Temp : T[ := T (A)];
170 --
171 -- prior to the call. Then we replace the actual with a reference to Temp,
172 -- and append the assignment:
173 --
174 -- A := TypeA (Temp);
175 --
176 -- after the call. Here TypeA is the actual type of variable A. For out
177 -- parameters, the initial declaration has no expression. If A is not an
178 -- entity name, we generate instead:
179 --
180 -- Var : TypeA renames A;
181 -- Temp : T := Var; -- omitting expression for out parameter.
182 -- ...
183 -- Var := TypeA (Temp);
184 --
185 -- For other in-out parameters, we emit the required constraint checks
186 -- before and/or after the call.
187 --
188 -- For all parameter modes, actuals that denote components and slices of
189 -- packed arrays are expanded into suitable temporaries.
190 --
191 -- For non-scalar objects that are possibly unaligned, add call by copy
192 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
193
194 procedure Expand_Inlined_Call
195 (N : Node_Id;
196 Subp : Entity_Id;
197 Orig_Subp : Entity_Id);
198 -- If called subprogram can be inlined by the front-end, retrieve the
199 -- analyzed body, replace formals with actuals and expand call in place.
200 -- Generate thunks for actuals that are expressions, and insert the
201 -- corresponding constant declarations before the call. If the original
202 -- call is to a derived operation, the return type is the one of the
203 -- derived operation, but the body is that of the original, so return
204 -- expressions in the body must be converted to the desired type (which
205 -- is simply not noted in the tree without inline expansion).
206
207 procedure Expand_Non_Function_Return (N : Node_Id);
208 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
209 -- a procedure body, entry body, accept statement, or extended return
210 -- statement. Note that all non-function returns are simple return
211 -- statements.
212
213 function Expand_Protected_Object_Reference
214 (N : Node_Id;
215 Scop : Entity_Id) return Node_Id;
216
217 procedure Expand_Protected_Subprogram_Call
218 (N : Node_Id;
219 Subp : Entity_Id;
220 Scop : Entity_Id);
221 -- A call to a protected subprogram within the protected object may appear
222 -- as a regular call. The list of actuals must be expanded to contain a
223 -- reference to the object itself, and the call becomes a call to the
224 -- corresponding protected subprogram.
225
226 function Is_Null_Procedure (Subp : Entity_Id) return Boolean;
227 -- Predicate to recognize stubbed procedures and null procedures, which
228 -- can be inlined unconditionally in all cases.
229
230 procedure Expand_Simple_Function_Return (N : Node_Id);
231 -- Expand simple return from function. In the case where we are returning
232 -- from a function body this is called by Expand_N_Simple_Return_Statement.
233
234 ----------------------------------------------
235 -- Add_Access_Actual_To_Build_In_Place_Call --
236 ----------------------------------------------
237
238 procedure Add_Access_Actual_To_Build_In_Place_Call
239 (Function_Call : Node_Id;
240 Function_Id : Entity_Id;
241 Return_Object : Node_Id;
242 Is_Access : Boolean := False)
243 is
244 Loc : constant Source_Ptr := Sloc (Function_Call);
245 Obj_Address : Node_Id;
246 Obj_Acc_Formal : Entity_Id;
247
248 begin
249 -- Locate the implicit access parameter in the called function
250
251 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
252
253 -- If no return object is provided, then pass null
254
255 if not Present (Return_Object) then
256 Obj_Address := Make_Null (Loc);
257 Set_Parent (Obj_Address, Function_Call);
258
259 -- If Return_Object is already an expression of an access type, then use
260 -- it directly, since it must be an access value denoting the return
261 -- object, and couldn't possibly be the return object itself.
262
263 elsif Is_Access then
264 Obj_Address := Return_Object;
265 Set_Parent (Obj_Address, Function_Call);
266
267 -- Apply Unrestricted_Access to caller's return object
268
269 else
270 Obj_Address :=
271 Make_Attribute_Reference (Loc,
272 Prefix => Return_Object,
273 Attribute_Name => Name_Unrestricted_Access);
274
275 Set_Parent (Return_Object, Obj_Address);
276 Set_Parent (Obj_Address, Function_Call);
277 end if;
278
279 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
280
281 -- Build the parameter association for the new actual and add it to the
282 -- end of the function's actuals.
283
284 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
285 end Add_Access_Actual_To_Build_In_Place_Call;
286
287 --------------------------------------------------
288 -- Add_Alloc_Form_Actual_To_Build_In_Place_Call --
289 --------------------------------------------------
290
291 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
292 (Function_Call : Node_Id;
293 Function_Id : Entity_Id;
294 Alloc_Form : BIP_Allocation_Form := Unspecified;
295 Alloc_Form_Exp : Node_Id := Empty)
296 is
297 Loc : constant Source_Ptr := Sloc (Function_Call);
298 Alloc_Form_Actual : Node_Id;
299 Alloc_Form_Formal : Node_Id;
300
301 begin
302 -- The allocation form generally doesn't need to be passed in the case
303 -- of a constrained result subtype, since normally the caller performs
304 -- the allocation in that case. However this formal is still needed in
305 -- the case where the function has a tagged result, because generally
306 -- such functions can be called in a dispatching context and such calls
307 -- must be handled like calls to class-wide functions.
308
309 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
310 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
311 then
312 return;
313 end if;
314
315 -- Locate the implicit allocation form parameter in the called function.
316 -- Maybe it would be better for each implicit formal of a build-in-place
317 -- function to have a flag or a Uint attribute to identify it. ???
318
319 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
320
321 if Present (Alloc_Form_Exp) then
322 pragma Assert (Alloc_Form = Unspecified);
323
324 Alloc_Form_Actual := Alloc_Form_Exp;
325
326 else
327 pragma Assert (Alloc_Form /= Unspecified);
328
329 Alloc_Form_Actual :=
330 Make_Integer_Literal (Loc,
331 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
332 end if;
333
334 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
335
336 -- Build the parameter association for the new actual and add it to the
337 -- end of the function's actuals.
338
339 Add_Extra_Actual_To_Call
340 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
341 end Add_Alloc_Form_Actual_To_Build_In_Place_Call;
342
343 ------------------------------
344 -- Add_Extra_Actual_To_Call --
345 ------------------------------
346
347 procedure Add_Extra_Actual_To_Call
348 (Subprogram_Call : Node_Id;
349 Extra_Formal : Entity_Id;
350 Extra_Actual : Node_Id)
351 is
352 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
353 Param_Assoc : Node_Id;
354
355 begin
356 Param_Assoc :=
357 Make_Parameter_Association (Loc,
358 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
359 Explicit_Actual_Parameter => Extra_Actual);
360
361 Set_Parent (Param_Assoc, Subprogram_Call);
362 Set_Parent (Extra_Actual, Param_Assoc);
363
364 if Present (Parameter_Associations (Subprogram_Call)) then
365 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
366 N_Parameter_Association
367 then
368
369 -- Find last named actual, and append
370
371 declare
372 L : Node_Id;
373 begin
374 L := First_Actual (Subprogram_Call);
375 while Present (L) loop
376 if No (Next_Actual (L)) then
377 Set_Next_Named_Actual (Parent (L), Extra_Actual);
378 exit;
379 end if;
380 Next_Actual (L);
381 end loop;
382 end;
383
384 else
385 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
386 end if;
387
388 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
389
390 else
391 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
392 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
393 end if;
394 end Add_Extra_Actual_To_Call;
395
396 --------------------------------------------------
397 -- Add_Final_List_Actual_To_Build_In_Place_Call --
398 --------------------------------------------------
399
400 procedure Add_Final_List_Actual_To_Build_In_Place_Call
401 (Function_Call : Node_Id;
402 Function_Id : Entity_Id;
403 Acc_Type : Entity_Id;
404 Sel_Comp : Node_Id := Empty)
405 is
406 Loc : constant Source_Ptr := Sloc (Function_Call);
407 Final_List : Node_Id;
408 Final_List_Actual : Node_Id;
409 Final_List_Formal : Node_Id;
410 Is_Ctrl_Result : constant Boolean :=
411 Needs_Finalization
412 (Underlying_Type (Etype (Function_Id)));
413
414 begin
415 -- No such extra parameter is needed if there are no controlled parts.
416 -- The test for Needs_Finalization accounts for class-wide results
417 -- (which potentially have controlled parts, even if the root type
418 -- doesn't), and the test for a tagged result type is needed because
419 -- calls to such a function can in general occur in dispatching
420 -- contexts, which must be treated the same as a call to class-wide
421 -- functions. Both of these situations require that a finalization list
422 -- be passed.
423
424 if not Needs_BIP_Final_List (Function_Id) then
425 return;
426 end if;
427
428 -- Locate implicit finalization list parameter in the called function
429
430 Final_List_Formal := Build_In_Place_Formal (Function_Id, BIP_Final_List);
431
432 -- Create the actual which is a pointer to the appropriate finalization
433 -- list. Acc_Type is present if and only if this call is the
434 -- initialization of an allocator. Use the Current_Scope or the
435 -- Acc_Type as appropriate.
436
437 if Present (Acc_Type)
438 and then (Ekind (Acc_Type) = E_Anonymous_Access_Type
439 or else
440 Present (Associated_Final_Chain (Base_Type (Acc_Type))))
441 then
442 Final_List := Find_Final_List (Acc_Type);
443
444 -- If Sel_Comp is present and the function result is controlled, then
445 -- the finalization list will be obtained from the _controller list of
446 -- the selected component's prefix object.
447
448 elsif Present (Sel_Comp) and then Is_Ctrl_Result then
449 Final_List := Find_Final_List (Current_Scope, Sel_Comp);
450
451 else
452 Final_List := Find_Final_List (Current_Scope);
453 end if;
454
455 Final_List_Actual :=
456 Make_Attribute_Reference (Loc,
457 Prefix => Final_List,
458 Attribute_Name => Name_Unrestricted_Access);
459
460 Analyze_And_Resolve (Final_List_Actual, Etype (Final_List_Formal));
461
462 -- Build the parameter association for the new actual and add it to the
463 -- end of the function's actuals.
464
465 Add_Extra_Actual_To_Call
466 (Function_Call, Final_List_Formal, Final_List_Actual);
467 end Add_Final_List_Actual_To_Build_In_Place_Call;
468
469 ---------------------------------------------
470 -- Add_Task_Actuals_To_Build_In_Place_Call --
471 ---------------------------------------------
472
473 procedure Add_Task_Actuals_To_Build_In_Place_Call
474 (Function_Call : Node_Id;
475 Function_Id : Entity_Id;
476 Master_Actual : Node_Id)
477 is
478 Loc : constant Source_Ptr := Sloc (Function_Call);
479 Actual : Node_Id := Master_Actual;
480
481 begin
482 -- No such extra parameters are needed if there are no tasks
483
484 if not Has_Task (Etype (Function_Id)) then
485 return;
486 end if;
487
488 -- Use a dummy _master actual in case of No_Task_Hierarchy
489
490 if Restriction_Active (No_Task_Hierarchy) then
491 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
492 end if;
493
494 -- The master
495
496 declare
497 Master_Formal : Node_Id;
498 begin
499 -- Locate implicit master parameter in the called function
500
501 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Master);
502
503 Analyze_And_Resolve (Actual, Etype (Master_Formal));
504
505 -- Build the parameter association for the new actual and add it to
506 -- the end of the function's actuals.
507
508 Add_Extra_Actual_To_Call
509 (Function_Call, Master_Formal, Actual);
510 end;
511
512 -- The activation chain
513
514 declare
515 Activation_Chain_Actual : Node_Id;
516 Activation_Chain_Formal : Node_Id;
517
518 begin
519 -- Locate implicit activation chain parameter in the called function
520
521 Activation_Chain_Formal := Build_In_Place_Formal
522 (Function_Id, BIP_Activation_Chain);
523
524 -- Create the actual which is a pointer to the current activation
525 -- chain
526
527 Activation_Chain_Actual :=
528 Make_Attribute_Reference (Loc,
529 Prefix => Make_Identifier (Loc, Name_uChain),
530 Attribute_Name => Name_Unrestricted_Access);
531
532 Analyze_And_Resolve
533 (Activation_Chain_Actual, Etype (Activation_Chain_Formal));
534
535 -- Build the parameter association for the new actual and add it to
536 -- the end of the function's actuals.
537
538 Add_Extra_Actual_To_Call
539 (Function_Call, Activation_Chain_Formal, Activation_Chain_Actual);
540 end;
541 end Add_Task_Actuals_To_Build_In_Place_Call;
542
543 -----------------------
544 -- BIP_Formal_Suffix --
545 -----------------------
546
547 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
548 begin
549 case Kind is
550 when BIP_Alloc_Form =>
551 return "BIPalloc";
552 when BIP_Final_List =>
553 return "BIPfinallist";
554 when BIP_Master =>
555 return "BIPmaster";
556 when BIP_Activation_Chain =>
557 return "BIPactivationchain";
558 when BIP_Object_Access =>
559 return "BIPaccess";
560 end case;
561 end BIP_Formal_Suffix;
562
563 ---------------------------
564 -- Build_In_Place_Formal --
565 ---------------------------
566
567 function Build_In_Place_Formal
568 (Func : Entity_Id;
569 Kind : BIP_Formal_Kind) return Entity_Id
570 is
571 Extra_Formal : Entity_Id := Extra_Formals (Func);
572
573 begin
574 -- Maybe it would be better for each implicit formal of a build-in-place
575 -- function to have a flag or a Uint attribute to identify it. ???
576
577 loop
578 pragma Assert (Present (Extra_Formal));
579 exit when
580 Chars (Extra_Formal) =
581 New_External_Name (Chars (Func), BIP_Formal_Suffix (Kind));
582 Next_Formal_With_Extras (Extra_Formal);
583 end loop;
584
585 return Extra_Formal;
586 end Build_In_Place_Formal;
587
588 --------------------------------
589 -- Check_Overriding_Operation --
590 --------------------------------
591
592 procedure Check_Overriding_Operation (Subp : Entity_Id) is
593 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
594 Op_List : constant Elist_Id := Primitive_Operations (Typ);
595 Op_Elmt : Elmt_Id;
596 Prim_Op : Entity_Id;
597 Par_Op : Entity_Id;
598
599 begin
600 if Is_Derived_Type (Typ)
601 and then not Is_Private_Type (Typ)
602 and then In_Open_Scopes (Scope (Etype (Typ)))
603 and then Typ = Base_Type (Typ)
604 then
605 -- Subp overrides an inherited private operation if there is an
606 -- inherited operation with a different name than Subp (see
607 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
608 -- same name as Subp.
609
610 Op_Elmt := First_Elmt (Op_List);
611 while Present (Op_Elmt) loop
612 Prim_Op := Node (Op_Elmt);
613 Par_Op := Alias (Prim_Op);
614
615 if Present (Par_Op)
616 and then not Comes_From_Source (Prim_Op)
617 and then Chars (Prim_Op) /= Chars (Par_Op)
618 and then Chars (Par_Op) = Chars (Subp)
619 and then Is_Hidden (Par_Op)
620 and then Type_Conformant (Prim_Op, Subp)
621 then
622 Set_DT_Position (Subp, DT_Position (Prim_Op));
623 end if;
624
625 Next_Elmt (Op_Elmt);
626 end loop;
627 end if;
628 end Check_Overriding_Operation;
629
630 -------------------------------
631 -- Detect_Infinite_Recursion --
632 -------------------------------
633
634 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
635 Loc : constant Source_Ptr := Sloc (N);
636
637 Var_List : constant Elist_Id := New_Elmt_List;
638 -- List of globals referenced by body of procedure
639
640 Call_List : constant Elist_Id := New_Elmt_List;
641 -- List of recursive calls in body of procedure
642
643 Shad_List : constant Elist_Id := New_Elmt_List;
644 -- List of entity id's for entities created to capture the value of
645 -- referenced globals on entry to the procedure.
646
647 Scop : constant Uint := Scope_Depth (Spec);
648 -- This is used to record the scope depth of the current procedure, so
649 -- that we can identify global references.
650
651 Max_Vars : constant := 4;
652 -- Do not test more than four global variables
653
654 Count_Vars : Natural := 0;
655 -- Count variables found so far
656
657 Var : Entity_Id;
658 Elm : Elmt_Id;
659 Ent : Entity_Id;
660 Call : Elmt_Id;
661 Decl : Node_Id;
662 Test : Node_Id;
663 Elm1 : Elmt_Id;
664 Elm2 : Elmt_Id;
665 Last : Node_Id;
666
667 function Process (Nod : Node_Id) return Traverse_Result;
668 -- Function to traverse the subprogram body (using Traverse_Func)
669
670 -------------
671 -- Process --
672 -------------
673
674 function Process (Nod : Node_Id) return Traverse_Result is
675 begin
676 -- Procedure call
677
678 if Nkind (Nod) = N_Procedure_Call_Statement then
679
680 -- Case of one of the detected recursive calls
681
682 if Is_Entity_Name (Name (Nod))
683 and then Has_Recursive_Call (Entity (Name (Nod)))
684 and then Entity (Name (Nod)) = Spec
685 then
686 Append_Elmt (Nod, Call_List);
687 return Skip;
688
689 -- Any other procedure call may have side effects
690
691 else
692 return Abandon;
693 end if;
694
695 -- A call to a pure function can always be ignored
696
697 elsif Nkind (Nod) = N_Function_Call
698 and then Is_Entity_Name (Name (Nod))
699 and then Is_Pure (Entity (Name (Nod)))
700 then
701 return Skip;
702
703 -- Case of an identifier reference
704
705 elsif Nkind (Nod) = N_Identifier then
706 Ent := Entity (Nod);
707
708 -- If no entity, then ignore the reference
709
710 -- Not clear why this can happen. To investigate, remove this
711 -- test and look at the crash that occurs here in 3401-004 ???
712
713 if No (Ent) then
714 return Skip;
715
716 -- Ignore entities with no Scope, again not clear how this
717 -- can happen, to investigate, look at 4108-008 ???
718
719 elsif No (Scope (Ent)) then
720 return Skip;
721
722 -- Ignore the reference if not to a more global object
723
724 elsif Scope_Depth (Scope (Ent)) >= Scop then
725 return Skip;
726
727 -- References to types, exceptions and constants are always OK
728
729 elsif Is_Type (Ent)
730 or else Ekind (Ent) = E_Exception
731 or else Ekind (Ent) = E_Constant
732 then
733 return Skip;
734
735 -- If other than a non-volatile scalar variable, we have some
736 -- kind of global reference (e.g. to a function) that we cannot
737 -- deal with so we forget the attempt.
738
739 elsif Ekind (Ent) /= E_Variable
740 or else not Is_Scalar_Type (Etype (Ent))
741 or else Treat_As_Volatile (Ent)
742 then
743 return Abandon;
744
745 -- Otherwise we have a reference to a global scalar
746
747 else
748 -- Loop through global entities already detected
749
750 Elm := First_Elmt (Var_List);
751 loop
752 -- If not detected before, record this new global reference
753
754 if No (Elm) then
755 Count_Vars := Count_Vars + 1;
756
757 if Count_Vars <= Max_Vars then
758 Append_Elmt (Entity (Nod), Var_List);
759 else
760 return Abandon;
761 end if;
762
763 exit;
764
765 -- If recorded before, ignore
766
767 elsif Node (Elm) = Entity (Nod) then
768 return Skip;
769
770 -- Otherwise keep looking
771
772 else
773 Next_Elmt (Elm);
774 end if;
775 end loop;
776
777 return Skip;
778 end if;
779
780 -- For all other node kinds, recursively visit syntactic children
781
782 else
783 return OK;
784 end if;
785 end Process;
786
787 function Traverse_Body is new Traverse_Func (Process);
788
789 -- Start of processing for Detect_Infinite_Recursion
790
791 begin
792 -- Do not attempt detection in No_Implicit_Conditional mode, since we
793 -- won't be able to generate the code to handle the recursion in any
794 -- case.
795
796 if Restriction_Active (No_Implicit_Conditionals) then
797 return;
798 end if;
799
800 -- Otherwise do traversal and quit if we get abandon signal
801
802 if Traverse_Body (N) = Abandon then
803 return;
804
805 -- We must have a call, since Has_Recursive_Call was set. If not just
806 -- ignore (this is only an error check, so if we have a funny situation,
807 -- due to bugs or errors, we do not want to bomb!)
808
809 elsif Is_Empty_Elmt_List (Call_List) then
810 return;
811 end if;
812
813 -- Here is the case where we detect recursion at compile time
814
815 -- Push our current scope for analyzing the declarations and code that
816 -- we will insert for the checking.
817
818 Push_Scope (Spec);
819
820 -- This loop builds temporary variables for each of the referenced
821 -- globals, so that at the end of the loop the list Shad_List contains
822 -- these temporaries in one-to-one correspondence with the elements in
823 -- Var_List.
824
825 Last := Empty;
826 Elm := First_Elmt (Var_List);
827 while Present (Elm) loop
828 Var := Node (Elm);
829 Ent := Make_Temporary (Loc, 'S');
830 Append_Elmt (Ent, Shad_List);
831
832 -- Insert a declaration for this temporary at the start of the
833 -- declarations for the procedure. The temporaries are declared as
834 -- constant objects initialized to the current values of the
835 -- corresponding temporaries.
836
837 Decl :=
838 Make_Object_Declaration (Loc,
839 Defining_Identifier => Ent,
840 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
841 Constant_Present => True,
842 Expression => New_Occurrence_Of (Var, Loc));
843
844 if No (Last) then
845 Prepend (Decl, Declarations (N));
846 else
847 Insert_After (Last, Decl);
848 end if;
849
850 Last := Decl;
851 Analyze (Decl);
852 Next_Elmt (Elm);
853 end loop;
854
855 -- Loop through calls
856
857 Call := First_Elmt (Call_List);
858 while Present (Call) loop
859
860 -- Build a predicate expression of the form
861
862 -- True
863 -- and then global1 = temp1
864 -- and then global2 = temp2
865 -- ...
866
867 -- This predicate determines if any of the global values
868 -- referenced by the procedure have changed since the
869 -- current call, if not an infinite recursion is assured.
870
871 Test := New_Occurrence_Of (Standard_True, Loc);
872
873 Elm1 := First_Elmt (Var_List);
874 Elm2 := First_Elmt (Shad_List);
875 while Present (Elm1) loop
876 Test :=
877 Make_And_Then (Loc,
878 Left_Opnd => Test,
879 Right_Opnd =>
880 Make_Op_Eq (Loc,
881 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
882 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
883
884 Next_Elmt (Elm1);
885 Next_Elmt (Elm2);
886 end loop;
887
888 -- Now we replace the call with the sequence
889
890 -- if no-changes (see above) then
891 -- raise Storage_Error;
892 -- else
893 -- original-call
894 -- end if;
895
896 Rewrite (Node (Call),
897 Make_If_Statement (Loc,
898 Condition => Test,
899 Then_Statements => New_List (
900 Make_Raise_Storage_Error (Loc,
901 Reason => SE_Infinite_Recursion)),
902
903 Else_Statements => New_List (
904 Relocate_Node (Node (Call)))));
905
906 Analyze (Node (Call));
907
908 Next_Elmt (Call);
909 end loop;
910
911 -- Remove temporary scope stack entry used for analysis
912
913 Pop_Scope;
914 end Detect_Infinite_Recursion;
915
916 --------------------
917 -- Expand_Actuals --
918 --------------------
919
920 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
921 Loc : constant Source_Ptr := Sloc (N);
922 Actual : Node_Id;
923 Formal : Entity_Id;
924 N_Node : Node_Id;
925 Post_Call : List_Id;
926 E_Formal : Entity_Id;
927
928 procedure Add_Call_By_Copy_Code;
929 -- For cases where the parameter must be passed by copy, this routine
930 -- generates a temporary variable into which the actual is copied and
931 -- then passes this as the parameter. For an OUT or IN OUT parameter,
932 -- an assignment is also generated to copy the result back. The call
933 -- also takes care of any constraint checks required for the type
934 -- conversion case (on both the way in and the way out).
935
936 procedure Add_Simple_Call_By_Copy_Code;
937 -- This is similar to the above, but is used in cases where we know
938 -- that all that is needed is to simply create a temporary and copy
939 -- the value in and out of the temporary.
940
941 procedure Check_Fortran_Logical;
942 -- A value of type Logical that is passed through a formal parameter
943 -- must be normalized because .TRUE. usually does not have the same
944 -- representation as True. We assume that .FALSE. = False = 0.
945 -- What about functions that return a logical type ???
946
947 function Is_Legal_Copy return Boolean;
948 -- Check that an actual can be copied before generating the temporary
949 -- to be used in the call. If the actual is of a by_reference type then
950 -- the program is illegal (this can only happen in the presence of
951 -- rep. clauses that force an incorrect alignment). If the formal is
952 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
953 -- the effect that this might lead to unaligned arguments.
954
955 function Make_Var (Actual : Node_Id) return Entity_Id;
956 -- Returns an entity that refers to the given actual parameter,
957 -- Actual (not including any type conversion). If Actual is an
958 -- entity name, then this entity is returned unchanged, otherwise
959 -- a renaming is created to provide an entity for the actual.
960
961 procedure Reset_Packed_Prefix;
962 -- The expansion of a packed array component reference is delayed in
963 -- the context of a call. Now we need to complete the expansion, so we
964 -- unmark the analyzed bits in all prefixes.
965
966 ---------------------------
967 -- Add_Call_By_Copy_Code --
968 ---------------------------
969
970 procedure Add_Call_By_Copy_Code is
971 Expr : Node_Id;
972 Init : Node_Id;
973 Temp : Entity_Id;
974 Indic : Node_Id;
975 Var : Entity_Id;
976 F_Typ : constant Entity_Id := Etype (Formal);
977 V_Typ : Entity_Id;
978 Crep : Boolean;
979
980 begin
981 if not Is_Legal_Copy then
982 return;
983 end if;
984
985 Temp := Make_Temporary (Loc, 'T', Actual);
986
987 -- Use formal type for temp, unless formal type is an unconstrained
988 -- array, in which case we don't have to worry about bounds checks,
989 -- and we use the actual type, since that has appropriate bounds.
990
991 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
992 Indic := New_Occurrence_Of (Etype (Actual), Loc);
993 else
994 Indic := New_Occurrence_Of (Etype (Formal), Loc);
995 end if;
996
997 if Nkind (Actual) = N_Type_Conversion then
998 V_Typ := Etype (Expression (Actual));
999
1000 -- If the formal is an (in-)out parameter, capture the name
1001 -- of the variable in order to build the post-call assignment.
1002
1003 Var := Make_Var (Expression (Actual));
1004
1005 Crep := not Same_Representation
1006 (F_Typ, Etype (Expression (Actual)));
1007
1008 else
1009 V_Typ := Etype (Actual);
1010 Var := Make_Var (Actual);
1011 Crep := False;
1012 end if;
1013
1014 -- Setup initialization for case of in out parameter, or an out
1015 -- parameter where the formal is an unconstrained array (in the
1016 -- latter case, we have to pass in an object with bounds).
1017
1018 -- If this is an out parameter, the initial copy is wasteful, so as
1019 -- an optimization for the one-dimensional case we extract the
1020 -- bounds of the actual and build an uninitialized temporary of the
1021 -- right size.
1022
1023 if Ekind (Formal) = E_In_Out_Parameter
1024 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1025 then
1026 if Nkind (Actual) = N_Type_Conversion then
1027 if Conversion_OK (Actual) then
1028 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1029 else
1030 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1031 end if;
1032
1033 elsif Ekind (Formal) = E_Out_Parameter
1034 and then Is_Array_Type (F_Typ)
1035 and then Number_Dimensions (F_Typ) = 1
1036 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1037 then
1038 -- Actual is a one-dimensional array or slice, and the type
1039 -- requires no initialization. Create a temporary of the
1040 -- right size, but do not copy actual into it (optimization).
1041
1042 Init := Empty;
1043 Indic :=
1044 Make_Subtype_Indication (Loc,
1045 Subtype_Mark =>
1046 New_Occurrence_Of (F_Typ, Loc),
1047 Constraint =>
1048 Make_Index_Or_Discriminant_Constraint (Loc,
1049 Constraints => New_List (
1050 Make_Range (Loc,
1051 Low_Bound =>
1052 Make_Attribute_Reference (Loc,
1053 Prefix => New_Occurrence_Of (Var, Loc),
1054 Attribute_Name => Name_First),
1055 High_Bound =>
1056 Make_Attribute_Reference (Loc,
1057 Prefix => New_Occurrence_Of (Var, Loc),
1058 Attribute_Name => Name_Last)))));
1059
1060 else
1061 Init := New_Occurrence_Of (Var, Loc);
1062 end if;
1063
1064 -- An initialization is created for packed conversions as
1065 -- actuals for out parameters to enable Make_Object_Declaration
1066 -- to determine the proper subtype for N_Node. Note that this
1067 -- is wasteful because the extra copying on the call side is
1068 -- not required for such out parameters. ???
1069
1070 elsif Ekind (Formal) = E_Out_Parameter
1071 and then Nkind (Actual) = N_Type_Conversion
1072 and then (Is_Bit_Packed_Array (F_Typ)
1073 or else
1074 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1075 then
1076 if Conversion_OK (Actual) then
1077 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1078 else
1079 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1080 end if;
1081
1082 elsif Ekind (Formal) = E_In_Parameter then
1083
1084 -- Handle the case in which the actual is a type conversion
1085
1086 if Nkind (Actual) = N_Type_Conversion then
1087 if Conversion_OK (Actual) then
1088 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1089 else
1090 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1091 end if;
1092 else
1093 Init := New_Occurrence_Of (Var, Loc);
1094 end if;
1095
1096 else
1097 Init := Empty;
1098 end if;
1099
1100 N_Node :=
1101 Make_Object_Declaration (Loc,
1102 Defining_Identifier => Temp,
1103 Object_Definition => Indic,
1104 Expression => Init);
1105 Set_Assignment_OK (N_Node);
1106 Insert_Action (N, N_Node);
1107
1108 -- Now, normally the deal here is that we use the defining
1109 -- identifier created by that object declaration. There is
1110 -- one exception to this. In the change of representation case
1111 -- the above declaration will end up looking like:
1112
1113 -- temp : type := identifier;
1114
1115 -- And in this case we might as well use the identifier directly
1116 -- and eliminate the temporary. Note that the analysis of the
1117 -- declaration was not a waste of time in that case, since it is
1118 -- what generated the necessary change of representation code. If
1119 -- the change of representation introduced additional code, as in
1120 -- a fixed-integer conversion, the expression is not an identifier
1121 -- and must be kept.
1122
1123 if Crep
1124 and then Present (Expression (N_Node))
1125 and then Is_Entity_Name (Expression (N_Node))
1126 then
1127 Temp := Entity (Expression (N_Node));
1128 Rewrite (N_Node, Make_Null_Statement (Loc));
1129 end if;
1130
1131 -- For IN parameter, all we do is to replace the actual
1132
1133 if Ekind (Formal) = E_In_Parameter then
1134 Rewrite (Actual, New_Reference_To (Temp, Loc));
1135 Analyze (Actual);
1136
1137 -- Processing for OUT or IN OUT parameter
1138
1139 else
1140 -- Kill current value indications for the temporary variable we
1141 -- created, since we just passed it as an OUT parameter.
1142
1143 Kill_Current_Values (Temp);
1144 Set_Is_Known_Valid (Temp, False);
1145
1146 -- If type conversion, use reverse conversion on exit
1147
1148 if Nkind (Actual) = N_Type_Conversion then
1149 if Conversion_OK (Actual) then
1150 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1151 else
1152 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1153 end if;
1154 else
1155 Expr := New_Occurrence_Of (Temp, Loc);
1156 end if;
1157
1158 Rewrite (Actual, New_Reference_To (Temp, Loc));
1159 Analyze (Actual);
1160
1161 -- If the actual is a conversion of a packed reference, it may
1162 -- already have been expanded by Remove_Side_Effects, and the
1163 -- resulting variable is a temporary which does not designate
1164 -- the proper out-parameter, which may not be addressable. In
1165 -- that case, generate an assignment to the original expression
1166 -- (before expansion of the packed reference) so that the proper
1167 -- expansion of assignment to a packed component can take place.
1168
1169 declare
1170 Obj : Node_Id;
1171 Lhs : Node_Id;
1172
1173 begin
1174 if Is_Renaming_Of_Object (Var)
1175 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1176 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1177 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1178 = N_Indexed_Component
1179 and then
1180 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1181 then
1182 Obj := Renamed_Object (Var);
1183 Lhs :=
1184 Make_Selected_Component (Loc,
1185 Prefix =>
1186 New_Copy_Tree (Original_Node (Prefix (Obj))),
1187 Selector_Name => New_Copy (Selector_Name (Obj)));
1188 Reset_Analyzed_Flags (Lhs);
1189
1190 else
1191 Lhs := New_Occurrence_Of (Var, Loc);
1192 end if;
1193
1194 Set_Assignment_OK (Lhs);
1195
1196 Append_To (Post_Call,
1197 Make_Assignment_Statement (Loc,
1198 Name => Lhs,
1199 Expression => Expr));
1200 end;
1201 end if;
1202 end Add_Call_By_Copy_Code;
1203
1204 ----------------------------------
1205 -- Add_Simple_Call_By_Copy_Code --
1206 ----------------------------------
1207
1208 procedure Add_Simple_Call_By_Copy_Code is
1209 Temp : Entity_Id;
1210 Decl : Node_Id;
1211 Incod : Node_Id;
1212 Outcod : Node_Id;
1213 Lhs : Node_Id;
1214 Rhs : Node_Id;
1215 Indic : Node_Id;
1216 F_Typ : constant Entity_Id := Etype (Formal);
1217
1218 begin
1219 if not Is_Legal_Copy then
1220 return;
1221 end if;
1222
1223 -- Use formal type for temp, unless formal type is an unconstrained
1224 -- array, in which case we don't have to worry about bounds checks,
1225 -- and we use the actual type, since that has appropriate bounds.
1226
1227 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1228 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1229 else
1230 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1231 end if;
1232
1233 -- Prepare to generate code
1234
1235 Reset_Packed_Prefix;
1236
1237 Temp := Make_Temporary (Loc, 'T', Actual);
1238 Incod := Relocate_Node (Actual);
1239 Outcod := New_Copy_Tree (Incod);
1240
1241 -- Generate declaration of temporary variable, initializing it
1242 -- with the input parameter unless we have an OUT formal or
1243 -- this is an initialization call.
1244
1245 -- If the formal is an out parameter with discriminants, the
1246 -- discriminants must be captured even if the rest of the object
1247 -- is in principle uninitialized, because the discriminants may
1248 -- be read by the called subprogram.
1249
1250 if Ekind (Formal) = E_Out_Parameter then
1251 Incod := Empty;
1252
1253 if Has_Discriminants (Etype (Formal)) then
1254 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1255 end if;
1256
1257 elsif Inside_Init_Proc then
1258
1259 -- Could use a comment here to match comment below ???
1260
1261 if Nkind (Actual) /= N_Selected_Component
1262 or else
1263 not Has_Discriminant_Dependent_Constraint
1264 (Entity (Selector_Name (Actual)))
1265 then
1266 Incod := Empty;
1267
1268 -- Otherwise, keep the component in order to generate the proper
1269 -- actual subtype, that depends on enclosing discriminants.
1270
1271 else
1272 null;
1273 end if;
1274 end if;
1275
1276 Decl :=
1277 Make_Object_Declaration (Loc,
1278 Defining_Identifier => Temp,
1279 Object_Definition => Indic,
1280 Expression => Incod);
1281
1282 if Inside_Init_Proc
1283 and then No (Incod)
1284 then
1285 -- If the call is to initialize a component of a composite type,
1286 -- and the component does not depend on discriminants, use the
1287 -- actual type of the component. This is required in case the
1288 -- component is constrained, because in general the formal of the
1289 -- initialization procedure will be unconstrained. Note that if
1290 -- the component being initialized is constrained by an enclosing
1291 -- discriminant, the presence of the initialization in the
1292 -- declaration will generate an expression for the actual subtype.
1293
1294 Set_No_Initialization (Decl);
1295 Set_Object_Definition (Decl,
1296 New_Occurrence_Of (Etype (Actual), Loc));
1297 end if;
1298
1299 Insert_Action (N, Decl);
1300
1301 -- The actual is simply a reference to the temporary
1302
1303 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1304
1305 -- Generate copy out if OUT or IN OUT parameter
1306
1307 if Ekind (Formal) /= E_In_Parameter then
1308 Lhs := Outcod;
1309 Rhs := New_Occurrence_Of (Temp, Loc);
1310
1311 -- Deal with conversion
1312
1313 if Nkind (Lhs) = N_Type_Conversion then
1314 Lhs := Expression (Lhs);
1315 Rhs := Convert_To (Etype (Actual), Rhs);
1316 end if;
1317
1318 Append_To (Post_Call,
1319 Make_Assignment_Statement (Loc,
1320 Name => Lhs,
1321 Expression => Rhs));
1322 Set_Assignment_OK (Name (Last (Post_Call)));
1323 end if;
1324 end Add_Simple_Call_By_Copy_Code;
1325
1326 ---------------------------
1327 -- Check_Fortran_Logical --
1328 ---------------------------
1329
1330 procedure Check_Fortran_Logical is
1331 Logical : constant Entity_Id := Etype (Formal);
1332 Var : Entity_Id;
1333
1334 -- Note: this is very incomplete, e.g. it does not handle arrays
1335 -- of logical values. This is really not the right approach at all???)
1336
1337 begin
1338 if Convention (Subp) = Convention_Fortran
1339 and then Root_Type (Etype (Formal)) = Standard_Boolean
1340 and then Ekind (Formal) /= E_In_Parameter
1341 then
1342 Var := Make_Var (Actual);
1343 Append_To (Post_Call,
1344 Make_Assignment_Statement (Loc,
1345 Name => New_Occurrence_Of (Var, Loc),
1346 Expression =>
1347 Unchecked_Convert_To (
1348 Logical,
1349 Make_Op_Ne (Loc,
1350 Left_Opnd => New_Occurrence_Of (Var, Loc),
1351 Right_Opnd =>
1352 Unchecked_Convert_To (
1353 Logical,
1354 New_Occurrence_Of (Standard_False, Loc))))));
1355 end if;
1356 end Check_Fortran_Logical;
1357
1358 -------------------
1359 -- Is_Legal_Copy --
1360 -------------------
1361
1362 function Is_Legal_Copy return Boolean is
1363 begin
1364 -- An attempt to copy a value of such a type can only occur if
1365 -- representation clauses give the actual a misaligned address.
1366
1367 if Is_By_Reference_Type (Etype (Formal)) then
1368 Error_Msg_N
1369 ("misaligned actual cannot be passed by reference", Actual);
1370 return False;
1371
1372 -- For users of Starlet, we assume that the specification of by-
1373 -- reference mechanism is mandatory. This may lead to unaligned
1374 -- objects but at least for DEC legacy code it is known to work.
1375 -- The warning will alert users of this code that a problem may
1376 -- be lurking.
1377
1378 elsif Mechanism (Formal) = By_Reference
1379 and then Is_Valued_Procedure (Scope (Formal))
1380 then
1381 Error_Msg_N
1382 ("by_reference actual may be misaligned?", Actual);
1383 return False;
1384
1385 else
1386 return True;
1387 end if;
1388 end Is_Legal_Copy;
1389
1390 --------------
1391 -- Make_Var --
1392 --------------
1393
1394 function Make_Var (Actual : Node_Id) return Entity_Id is
1395 Var : Entity_Id;
1396
1397 begin
1398 if Is_Entity_Name (Actual) then
1399 return Entity (Actual);
1400
1401 else
1402 Var := Make_Temporary (Loc, 'T', Actual);
1403
1404 N_Node :=
1405 Make_Object_Renaming_Declaration (Loc,
1406 Defining_Identifier => Var,
1407 Subtype_Mark =>
1408 New_Occurrence_Of (Etype (Actual), Loc),
1409 Name => Relocate_Node (Actual));
1410
1411 Insert_Action (N, N_Node);
1412 return Var;
1413 end if;
1414 end Make_Var;
1415
1416 -------------------------
1417 -- Reset_Packed_Prefix --
1418 -------------------------
1419
1420 procedure Reset_Packed_Prefix is
1421 Pfx : Node_Id := Actual;
1422 begin
1423 loop
1424 Set_Analyzed (Pfx, False);
1425 exit when
1426 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1427 Pfx := Prefix (Pfx);
1428 end loop;
1429 end Reset_Packed_Prefix;
1430
1431 -- Start of processing for Expand_Actuals
1432
1433 begin
1434 Post_Call := New_List;
1435
1436 Formal := First_Formal (Subp);
1437 Actual := First_Actual (N);
1438 while Present (Formal) loop
1439 E_Formal := Etype (Formal);
1440
1441 if Is_Scalar_Type (E_Formal)
1442 or else Nkind (Actual) = N_Slice
1443 then
1444 Check_Fortran_Logical;
1445
1446 -- RM 6.4.1 (11)
1447
1448 elsif Ekind (Formal) /= E_Out_Parameter then
1449
1450 -- The unusual case of the current instance of a protected type
1451 -- requires special handling. This can only occur in the context
1452 -- of a call within the body of a protected operation.
1453
1454 if Is_Entity_Name (Actual)
1455 and then Ekind (Entity (Actual)) = E_Protected_Type
1456 and then In_Open_Scopes (Entity (Actual))
1457 then
1458 if Scope (Subp) /= Entity (Actual) then
1459 Error_Msg_N ("operation outside protected type may not "
1460 & "call back its protected operations?", Actual);
1461 end if;
1462
1463 Rewrite (Actual,
1464 Expand_Protected_Object_Reference (N, Entity (Actual)));
1465 end if;
1466
1467 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1468 -- build-in-place function, then a temporary return object needs
1469 -- to be created and access to it must be passed to the function.
1470 -- Currently we limit such functions to those with inherently
1471 -- limited result subtypes, but eventually we plan to expand the
1472 -- functions that are treated as build-in-place to include other
1473 -- composite result types.
1474
1475 if Ada_Version >= Ada_2005
1476 and then Is_Build_In_Place_Function_Call (Actual)
1477 then
1478 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1479 end if;
1480
1481 Apply_Constraint_Check (Actual, E_Formal);
1482
1483 -- Out parameter case. No constraint checks on access type
1484 -- RM 6.4.1 (13)
1485
1486 elsif Is_Access_Type (E_Formal) then
1487 null;
1488
1489 -- RM 6.4.1 (14)
1490
1491 elsif Has_Discriminants (Base_Type (E_Formal))
1492 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1493 then
1494 Apply_Constraint_Check (Actual, E_Formal);
1495
1496 -- RM 6.4.1 (15)
1497
1498 else
1499 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1500 end if;
1501
1502 -- Processing for IN-OUT and OUT parameters
1503
1504 if Ekind (Formal) /= E_In_Parameter then
1505
1506 -- For type conversions of arrays, apply length/range checks
1507
1508 if Is_Array_Type (E_Formal)
1509 and then Nkind (Actual) = N_Type_Conversion
1510 then
1511 if Is_Constrained (E_Formal) then
1512 Apply_Length_Check (Expression (Actual), E_Formal);
1513 else
1514 Apply_Range_Check (Expression (Actual), E_Formal);
1515 end if;
1516 end if;
1517
1518 -- If argument is a type conversion for a type that is passed
1519 -- by copy, then we must pass the parameter by copy.
1520
1521 if Nkind (Actual) = N_Type_Conversion
1522 and then
1523 (Is_Numeric_Type (E_Formal)
1524 or else Is_Access_Type (E_Formal)
1525 or else Is_Enumeration_Type (E_Formal)
1526 or else Is_Bit_Packed_Array (Etype (Formal))
1527 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1528
1529 -- Also pass by copy if change of representation
1530
1531 or else not Same_Representation
1532 (Etype (Formal),
1533 Etype (Expression (Actual))))
1534 then
1535 Add_Call_By_Copy_Code;
1536
1537 -- References to components of bit packed arrays are expanded
1538 -- at this point, rather than at the point of analysis of the
1539 -- actuals, to handle the expansion of the assignment to
1540 -- [in] out parameters.
1541
1542 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1543 Add_Simple_Call_By_Copy_Code;
1544
1545 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1546 -- because the back-end cannot cope with such objects. In other
1547 -- cases where alignment forces a copy, the back-end generates
1548 -- it properly. It should not be generated unconditionally in the
1549 -- front-end because it does not know precisely the alignment
1550 -- requirements of the target, and makes too conservative an
1551 -- estimate, leading to superfluous copies or spurious errors
1552 -- on by-reference parameters.
1553
1554 elsif Nkind (Actual) = N_Selected_Component
1555 and then
1556 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1557 and then not Represented_As_Scalar (Etype (Formal))
1558 then
1559 Add_Simple_Call_By_Copy_Code;
1560
1561 -- References to slices of bit packed arrays are expanded
1562
1563 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1564 Add_Call_By_Copy_Code;
1565
1566 -- References to possibly unaligned slices of arrays are expanded
1567
1568 elsif Is_Possibly_Unaligned_Slice (Actual) then
1569 Add_Call_By_Copy_Code;
1570
1571 -- Deal with access types where the actual subtype and the
1572 -- formal subtype are not the same, requiring a check.
1573
1574 -- It is necessary to exclude tagged types because of "downward
1575 -- conversion" errors.
1576
1577 elsif Is_Access_Type (E_Formal)
1578 and then not Same_Type (E_Formal, Etype (Actual))
1579 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1580 then
1581 Add_Call_By_Copy_Code;
1582
1583 -- If the actual is not a scalar and is marked for volatile
1584 -- treatment, whereas the formal is not volatile, then pass
1585 -- by copy unless it is a by-reference type.
1586
1587 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1588 -- because this is the enforcement of a language rule that applies
1589 -- only to "real" volatile variables, not e.g. to the address
1590 -- clause overlay case.
1591
1592 elsif Is_Entity_Name (Actual)
1593 and then Is_Volatile (Entity (Actual))
1594 and then not Is_By_Reference_Type (Etype (Actual))
1595 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1596 and then not Is_Volatile (E_Formal)
1597 then
1598 Add_Call_By_Copy_Code;
1599
1600 elsif Nkind (Actual) = N_Indexed_Component
1601 and then Is_Entity_Name (Prefix (Actual))
1602 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1603 then
1604 Add_Call_By_Copy_Code;
1605
1606 -- Add call-by-copy code for the case of scalar out parameters
1607 -- when it is not known at compile time that the subtype of the
1608 -- formal is a subrange of the subtype of the actual (or vice
1609 -- versa for in out parameters), in order to get range checks
1610 -- on such actuals. (Maybe this case should be handled earlier
1611 -- in the if statement???)
1612
1613 elsif Is_Scalar_Type (E_Formal)
1614 and then
1615 (not In_Subrange_Of (E_Formal, Etype (Actual))
1616 or else
1617 (Ekind (Formal) = E_In_Out_Parameter
1618 and then not In_Subrange_Of (Etype (Actual), E_Formal)))
1619 then
1620 -- Perhaps the setting back to False should be done within
1621 -- Add_Call_By_Copy_Code, since it could get set on other
1622 -- cases occurring above???
1623
1624 if Do_Range_Check (Actual) then
1625 Set_Do_Range_Check (Actual, False);
1626 end if;
1627
1628 Add_Call_By_Copy_Code;
1629 end if;
1630
1631 -- Processing for IN parameters
1632
1633 else
1634 -- For IN parameters is in the packed array case, we expand an
1635 -- indexed component (the circuit in Exp_Ch4 deliberately left
1636 -- indexed components appearing as actuals untouched, so that
1637 -- the special processing above for the OUT and IN OUT cases
1638 -- could be performed. We could make the test in Exp_Ch4 more
1639 -- complex and have it detect the parameter mode, but it is
1640 -- easier simply to handle all cases here.)
1641
1642 if Nkind (Actual) = N_Indexed_Component
1643 and then Is_Packed (Etype (Prefix (Actual)))
1644 then
1645 Reset_Packed_Prefix;
1646 Expand_Packed_Element_Reference (Actual);
1647
1648 -- If we have a reference to a bit packed array, we copy it, since
1649 -- the actual must be byte aligned.
1650
1651 -- Is this really necessary in all cases???
1652
1653 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1654 Add_Simple_Call_By_Copy_Code;
1655
1656 -- If a non-scalar actual is possibly unaligned, we need a copy
1657
1658 elsif Is_Possibly_Unaligned_Object (Actual)
1659 and then not Represented_As_Scalar (Etype (Formal))
1660 then
1661 Add_Simple_Call_By_Copy_Code;
1662
1663 -- Similarly, we have to expand slices of packed arrays here
1664 -- because the result must be byte aligned.
1665
1666 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1667 Add_Call_By_Copy_Code;
1668
1669 -- Only processing remaining is to pass by copy if this is a
1670 -- reference to a possibly unaligned slice, since the caller
1671 -- expects an appropriately aligned argument.
1672
1673 elsif Is_Possibly_Unaligned_Slice (Actual) then
1674 Add_Call_By_Copy_Code;
1675
1676 -- An unusual case: a current instance of an enclosing task can be
1677 -- an actual, and must be replaced by a reference to self.
1678
1679 elsif Is_Entity_Name (Actual)
1680 and then Is_Task_Type (Entity (Actual))
1681 then
1682 if In_Open_Scopes (Entity (Actual)) then
1683 Rewrite (Actual,
1684 (Make_Function_Call (Loc,
1685 Name => New_Reference_To (RTE (RE_Self), Loc))));
1686 Analyze (Actual);
1687
1688 -- A task type cannot otherwise appear as an actual
1689
1690 else
1691 raise Program_Error;
1692 end if;
1693 end if;
1694 end if;
1695
1696 Next_Formal (Formal);
1697 Next_Actual (Actual);
1698 end loop;
1699
1700 -- Find right place to put post call stuff if it is present
1701
1702 if not Is_Empty_List (Post_Call) then
1703
1704 -- If call is not a list member, it must be the triggering statement
1705 -- of a triggering alternative or an entry call alternative, and we
1706 -- can add the post call stuff to the corresponding statement list.
1707
1708 if not Is_List_Member (N) then
1709 declare
1710 P : constant Node_Id := Parent (N);
1711
1712 begin
1713 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1714 N_Entry_Call_Alternative));
1715
1716 if Is_Non_Empty_List (Statements (P)) then
1717 Insert_List_Before_And_Analyze
1718 (First (Statements (P)), Post_Call);
1719 else
1720 Set_Statements (P, Post_Call);
1721 end if;
1722 end;
1723
1724 -- Otherwise, normal case where N is in a statement sequence,
1725 -- just put the post-call stuff after the call statement.
1726
1727 else
1728 Insert_Actions_After (N, Post_Call);
1729 end if;
1730 end if;
1731
1732 -- The call node itself is re-analyzed in Expand_Call
1733
1734 end Expand_Actuals;
1735
1736 -----------------
1737 -- Expand_Call --
1738 -----------------
1739
1740 -- This procedure handles expansion of function calls and procedure call
1741 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1742 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1743
1744 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1745 -- Provide values of actuals for all formals in Extra_Formals list
1746 -- Replace "call" to enumeration literal function by literal itself
1747 -- Rewrite call to predefined operator as operator
1748 -- Replace actuals to in-out parameters that are numeric conversions,
1749 -- with explicit assignment to temporaries before and after the call.
1750 -- Remove optional actuals if First_Optional_Parameter specified.
1751
1752 -- Note that the list of actuals has been filled with default expressions
1753 -- during semantic analysis of the call. Only the extra actuals required
1754 -- for the 'Constrained attribute and for accessibility checks are added
1755 -- at this point.
1756
1757 procedure Expand_Call (N : Node_Id) is
1758 Loc : constant Source_Ptr := Sloc (N);
1759 Call_Node : Node_Id := N;
1760 Extra_Actuals : List_Id := No_List;
1761 Prev : Node_Id := Empty;
1762
1763 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1764 -- Adds one entry to the end of the actual parameter list. Used for
1765 -- default parameters and for extra actuals (for Extra_Formals). The
1766 -- argument is an N_Parameter_Association node.
1767
1768 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1769 -- Adds an extra actual to the list of extra actuals. Expr is the
1770 -- expression for the value of the actual, EF is the entity for the
1771 -- extra formal.
1772
1773 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1774 -- Within an instance, a type derived from a non-tagged formal derived
1775 -- type inherits from the original parent, not from the actual. The
1776 -- current derivation mechanism has the derived type inherit from the
1777 -- actual, which is only correct outside of the instance. If the
1778 -- subprogram is inherited, we test for this particular case through a
1779 -- convoluted tree traversal before setting the proper subprogram to be
1780 -- called.
1781
1782 --------------------------
1783 -- Add_Actual_Parameter --
1784 --------------------------
1785
1786 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1787 Actual_Expr : constant Node_Id :=
1788 Explicit_Actual_Parameter (Insert_Param);
1789
1790 begin
1791 -- Case of insertion is first named actual
1792
1793 if No (Prev) or else
1794 Nkind (Parent (Prev)) /= N_Parameter_Association
1795 then
1796 Set_Next_Named_Actual
1797 (Insert_Param, First_Named_Actual (Call_Node));
1798 Set_First_Named_Actual (Call_Node, Actual_Expr);
1799
1800 if No (Prev) then
1801 if No (Parameter_Associations (Call_Node)) then
1802 Set_Parameter_Associations (Call_Node, New_List);
1803 Append (Insert_Param, Parameter_Associations (Call_Node));
1804 end if;
1805 else
1806 Insert_After (Prev, Insert_Param);
1807 end if;
1808
1809 -- Case of insertion is not first named actual
1810
1811 else
1812 Set_Next_Named_Actual
1813 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1814 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1815 Append (Insert_Param, Parameter_Associations (Call_Node));
1816 end if;
1817
1818 Prev := Actual_Expr;
1819 end Add_Actual_Parameter;
1820
1821 ----------------------
1822 -- Add_Extra_Actual --
1823 ----------------------
1824
1825 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1826 Loc : constant Source_Ptr := Sloc (Expr);
1827
1828 begin
1829 if Extra_Actuals = No_List then
1830 Extra_Actuals := New_List;
1831 Set_Parent (Extra_Actuals, Call_Node);
1832 end if;
1833
1834 Append_To (Extra_Actuals,
1835 Make_Parameter_Association (Loc,
1836 Selector_Name => Make_Identifier (Loc, Chars (EF)),
1837 Explicit_Actual_Parameter => Expr));
1838
1839 Analyze_And_Resolve (Expr, Etype (EF));
1840
1841 if Nkind (Call_Node) = N_Function_Call then
1842 Set_Is_Accessibility_Actual (Parent (Expr));
1843 end if;
1844 end Add_Extra_Actual;
1845
1846 ---------------------------
1847 -- Inherited_From_Formal --
1848 ---------------------------
1849
1850 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1851 Par : Entity_Id;
1852 Gen_Par : Entity_Id;
1853 Gen_Prim : Elist_Id;
1854 Elmt : Elmt_Id;
1855 Indic : Node_Id;
1856
1857 begin
1858 -- If the operation is inherited, it is attached to the corresponding
1859 -- type derivation. If the parent in the derivation is a generic
1860 -- actual, it is a subtype of the actual, and we have to recover the
1861 -- original derived type declaration to find the proper parent.
1862
1863 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1864 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1865 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1866 N_Derived_Type_Definition
1867 or else not In_Instance
1868 then
1869 return Empty;
1870
1871 else
1872 Indic :=
1873 Subtype_Indication
1874 (Type_Definition (Original_Node (Parent (S))));
1875
1876 if Nkind (Indic) = N_Subtype_Indication then
1877 Par := Entity (Subtype_Mark (Indic));
1878 else
1879 Par := Entity (Indic);
1880 end if;
1881 end if;
1882
1883 if not Is_Generic_Actual_Type (Par)
1884 or else Is_Tagged_Type (Par)
1885 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1886 or else not In_Open_Scopes (Scope (Par))
1887 then
1888 return Empty;
1889 else
1890 Gen_Par := Generic_Parent_Type (Parent (Par));
1891 end if;
1892
1893 -- If the actual has no generic parent type, the formal is not
1894 -- a formal derived type, so nothing to inherit.
1895
1896 if No (Gen_Par) then
1897 return Empty;
1898 end if;
1899
1900 -- If the generic parent type is still the generic type, this is a
1901 -- private formal, not a derived formal, and there are no operations
1902 -- inherited from the formal.
1903
1904 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1905 return Empty;
1906 end if;
1907
1908 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1909
1910 Elmt := First_Elmt (Gen_Prim);
1911 while Present (Elmt) loop
1912 if Chars (Node (Elmt)) = Chars (S) then
1913 declare
1914 F1 : Entity_Id;
1915 F2 : Entity_Id;
1916
1917 begin
1918 F1 := First_Formal (S);
1919 F2 := First_Formal (Node (Elmt));
1920 while Present (F1)
1921 and then Present (F2)
1922 loop
1923 if Etype (F1) = Etype (F2)
1924 or else Etype (F2) = Gen_Par
1925 then
1926 Next_Formal (F1);
1927 Next_Formal (F2);
1928 else
1929 Next_Elmt (Elmt);
1930 exit; -- not the right subprogram
1931 end if;
1932
1933 return Node (Elmt);
1934 end loop;
1935 end;
1936
1937 else
1938 Next_Elmt (Elmt);
1939 end if;
1940 end loop;
1941
1942 raise Program_Error;
1943 end Inherited_From_Formal;
1944
1945 -- Local variables
1946
1947 Remote : constant Boolean := Is_Remote_Call (Call_Node);
1948 Actual : Node_Id;
1949 Formal : Entity_Id;
1950 Orig_Subp : Entity_Id := Empty;
1951 Param_Count : Natural := 0;
1952 Parent_Formal : Entity_Id;
1953 Parent_Subp : Entity_Id;
1954 Scop : Entity_Id;
1955 Subp : Entity_Id;
1956
1957 Prev_Orig : Node_Id;
1958 -- Original node for an actual, which may have been rewritten. If the
1959 -- actual is a function call that has been transformed from a selected
1960 -- component, the original node is unanalyzed. Otherwise, it carries
1961 -- semantic information used to generate additional actuals.
1962
1963 CW_Interface_Formals_Present : Boolean := False;
1964
1965 -- Start of processing for Expand_Call
1966
1967 begin
1968 -- Ignore if previous error
1969
1970 if Nkind (Call_Node) in N_Has_Etype
1971 and then Etype (Call_Node) = Any_Type
1972 then
1973 return;
1974 end if;
1975
1976 -- Call using access to subprogram with explicit dereference
1977
1978 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
1979 Subp := Etype (Name (Call_Node));
1980 Parent_Subp := Empty;
1981
1982 -- Case of call to simple entry, where the Name is a selected component
1983 -- whose prefix is the task, and whose selector name is the entry name
1984
1985 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
1986 Subp := Entity (Selector_Name (Name (Call_Node)));
1987 Parent_Subp := Empty;
1988
1989 -- Case of call to member of entry family, where Name is an indexed
1990 -- component, with the prefix being a selected component giving the
1991 -- task and entry family name, and the index being the entry index.
1992
1993 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
1994 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
1995 Parent_Subp := Empty;
1996
1997 -- Normal case
1998
1999 else
2000 Subp := Entity (Name (Call_Node));
2001 Parent_Subp := Alias (Subp);
2002
2003 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2004 -- if we can tell that the first parameter cannot possibly be null.
2005 -- This improves efficiency by avoiding a run-time test.
2006
2007 -- We do not do this if Raise_Exception_Always does not exist, which
2008 -- can happen in configurable run time profiles which provide only a
2009 -- Raise_Exception.
2010
2011 if Is_RTE (Subp, RE_Raise_Exception)
2012 and then RTE_Available (RE_Raise_Exception_Always)
2013 then
2014 declare
2015 FA : constant Node_Id := Original_Node
2016 (First_Actual (Call_Node));
2017 begin
2018 -- The case we catch is where the first argument is obtained
2019 -- using the Identity attribute (which must always be
2020 -- non-null).
2021
2022 if Nkind (FA) = N_Attribute_Reference
2023 and then Attribute_Name (FA) = Name_Identity
2024 then
2025 Subp := RTE (RE_Raise_Exception_Always);
2026 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2027 end if;
2028 end;
2029 end if;
2030
2031 if Ekind (Subp) = E_Entry then
2032 Parent_Subp := Empty;
2033 end if;
2034 end if;
2035
2036 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2037 -- alternative in an asynchronous select or as an entry call in
2038 -- a conditional or timed select. Check whether the procedure call
2039 -- is a renaming of an entry and rewrite it as an entry call.
2040
2041 if Ada_Version >= Ada_2005
2042 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2043 and then
2044 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2045 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2046 or else
2047 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2048 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2049 then
2050 declare
2051 Ren_Decl : Node_Id;
2052 Ren_Root : Entity_Id := Subp;
2053
2054 begin
2055 -- This may be a chain of renamings, find the root
2056
2057 if Present (Alias (Ren_Root)) then
2058 Ren_Root := Alias (Ren_Root);
2059 end if;
2060
2061 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2062 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2063
2064 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2065 Rewrite (Call_Node,
2066 Make_Entry_Call_Statement (Loc,
2067 Name =>
2068 New_Copy_Tree (Name (Ren_Decl)),
2069 Parameter_Associations =>
2070 New_Copy_List_Tree
2071 (Parameter_Associations (Call_Node))));
2072
2073 return;
2074 end if;
2075 end if;
2076 end;
2077 end if;
2078
2079 -- First step, compute extra actuals, corresponding to any Extra_Formals
2080 -- present. Note that we do not access Extra_Formals directly, instead
2081 -- we simply note the presence of the extra formals as we process the
2082 -- regular formals collecting corresponding actuals in Extra_Actuals.
2083
2084 -- We also generate any required range checks for actuals for in formals
2085 -- as we go through the loop, since this is a convenient place to do it.
2086 -- (Though it seems that this would be better done in Expand_Actuals???)
2087
2088 Formal := First_Formal (Subp);
2089 Actual := First_Actual (Call_Node);
2090 Param_Count := 1;
2091 while Present (Formal) loop
2092
2093 -- Generate range check if required
2094
2095 if Do_Range_Check (Actual)
2096 and then Ekind (Formal) = E_In_Parameter
2097 then
2098 Set_Do_Range_Check (Actual, False);
2099 Generate_Range_Check
2100 (Actual, Etype (Formal), CE_Range_Check_Failed);
2101 end if;
2102
2103 -- Prepare to examine current entry
2104
2105 Prev := Actual;
2106 Prev_Orig := Original_Node (Prev);
2107
2108 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2109 -- to expand it in a further round.
2110
2111 CW_Interface_Formals_Present :=
2112 CW_Interface_Formals_Present
2113 or else
2114 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2115 and then Is_Interface (Etype (Etype (Formal))))
2116 or else
2117 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2118 and then Is_Interface (Directly_Designated_Type
2119 (Etype (Etype (Formal)))));
2120
2121 -- Create possible extra actual for constrained case. Usually, the
2122 -- extra actual is of the form actual'constrained, but since this
2123 -- attribute is only available for unconstrained records, TRUE is
2124 -- expanded if the type of the formal happens to be constrained (for
2125 -- instance when this procedure is inherited from an unconstrained
2126 -- record to a constrained one) or if the actual has no discriminant
2127 -- (its type is constrained). An exception to this is the case of a
2128 -- private type without discriminants. In this case we pass FALSE
2129 -- because the object has underlying discriminants with defaults.
2130
2131 if Present (Extra_Constrained (Formal)) then
2132 if Ekind (Etype (Prev)) in Private_Kind
2133 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2134 then
2135 Add_Extra_Actual
2136 (New_Occurrence_Of (Standard_False, Loc),
2137 Extra_Constrained (Formal));
2138
2139 elsif Is_Constrained (Etype (Formal))
2140 or else not Has_Discriminants (Etype (Prev))
2141 then
2142 Add_Extra_Actual
2143 (New_Occurrence_Of (Standard_True, Loc),
2144 Extra_Constrained (Formal));
2145
2146 -- Do not produce extra actuals for Unchecked_Union parameters.
2147 -- Jump directly to the end of the loop.
2148
2149 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2150 goto Skip_Extra_Actual_Generation;
2151
2152 else
2153 -- If the actual is a type conversion, then the constrained
2154 -- test applies to the actual, not the target type.
2155
2156 declare
2157 Act_Prev : Node_Id;
2158
2159 begin
2160 -- Test for unchecked conversions as well, which can occur
2161 -- as out parameter actuals on calls to stream procedures.
2162
2163 Act_Prev := Prev;
2164 while Nkind_In (Act_Prev, N_Type_Conversion,
2165 N_Unchecked_Type_Conversion)
2166 loop
2167 Act_Prev := Expression (Act_Prev);
2168 end loop;
2169
2170 -- If the expression is a conversion of a dereference, this
2171 -- is internally generated code that manipulates addresses,
2172 -- e.g. when building interface tables. No check should
2173 -- occur in this case, and the discriminated object is not
2174 -- directly a hand.
2175
2176 if not Comes_From_Source (Actual)
2177 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2178 and then Nkind (Act_Prev) = N_Explicit_Dereference
2179 then
2180 Add_Extra_Actual
2181 (New_Occurrence_Of (Standard_False, Loc),
2182 Extra_Constrained (Formal));
2183
2184 else
2185 Add_Extra_Actual
2186 (Make_Attribute_Reference (Sloc (Prev),
2187 Prefix =>
2188 Duplicate_Subexpr_No_Checks
2189 (Act_Prev, Name_Req => True),
2190 Attribute_Name => Name_Constrained),
2191 Extra_Constrained (Formal));
2192 end if;
2193 end;
2194 end if;
2195 end if;
2196
2197 -- Create possible extra actual for accessibility level
2198
2199 if Present (Extra_Accessibility (Formal)) then
2200
2201 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2202 -- attribute, then the original actual may be an aliased object
2203 -- occurring as the prefix in a call using "Object.Operation"
2204 -- notation. In that case we must pass the level of the object,
2205 -- so Prev_Orig is reset to Prev and the attribute will be
2206 -- processed by the code for Access attributes further below.
2207
2208 if Prev_Orig /= Prev
2209 and then Nkind (Prev) = N_Attribute_Reference
2210 and then
2211 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2212 and then Is_Aliased_View (Prev_Orig)
2213 then
2214 Prev_Orig := Prev;
2215 end if;
2216
2217 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2218 -- accessibility levels.
2219
2220 if Ekind (Current_Scope) in Subprogram_Kind
2221 and then Is_Thunk (Current_Scope)
2222 then
2223 declare
2224 Parm_Ent : Entity_Id;
2225
2226 begin
2227 if Is_Controlling_Actual (Actual) then
2228
2229 -- Find the corresponding actual of the thunk
2230
2231 Parm_Ent := First_Entity (Current_Scope);
2232 for J in 2 .. Param_Count loop
2233 Next_Entity (Parm_Ent);
2234 end loop;
2235
2236 else pragma Assert (Is_Entity_Name (Actual));
2237 Parm_Ent := Entity (Actual);
2238 end if;
2239
2240 Add_Extra_Actual
2241 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2242 Extra_Accessibility (Formal));
2243 end;
2244
2245 elsif Is_Entity_Name (Prev_Orig) then
2246
2247 -- When passing an access parameter, or a renaming of an access
2248 -- parameter, as the actual to another access parameter we need
2249 -- to pass along the actual's own access level parameter. This
2250 -- is done if we are within the scope of the formal access
2251 -- parameter (if this is an inlined body the extra formal is
2252 -- irrelevant).
2253
2254 if (Is_Formal (Entity (Prev_Orig))
2255 or else
2256 (Present (Renamed_Object (Entity (Prev_Orig)))
2257 and then
2258 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2259 and then
2260 Is_Formal
2261 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2262 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2263 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2264 then
2265 declare
2266 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2267
2268 begin
2269 pragma Assert (Present (Parm_Ent));
2270
2271 if Present (Extra_Accessibility (Parm_Ent)) then
2272 Add_Extra_Actual
2273 (New_Occurrence_Of
2274 (Extra_Accessibility (Parm_Ent), Loc),
2275 Extra_Accessibility (Formal));
2276
2277 -- If the actual access parameter does not have an
2278 -- associated extra formal providing its scope level,
2279 -- then treat the actual as having library-level
2280 -- accessibility.
2281
2282 else
2283 Add_Extra_Actual
2284 (Make_Integer_Literal (Loc,
2285 Intval => Scope_Depth (Standard_Standard)),
2286 Extra_Accessibility (Formal));
2287 end if;
2288 end;
2289
2290 -- The actual is a normal access value, so just pass the level
2291 -- of the actual's access type.
2292
2293 else
2294 Add_Extra_Actual
2295 (Make_Integer_Literal (Loc,
2296 Intval => Type_Access_Level (Etype (Prev_Orig))),
2297 Extra_Accessibility (Formal));
2298 end if;
2299
2300 -- If the actual is an access discriminant, then pass the level
2301 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2302
2303 elsif Nkind (Prev_Orig) = N_Selected_Component
2304 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2305 E_Discriminant
2306 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2307 E_Anonymous_Access_Type
2308 then
2309 Add_Extra_Actual
2310 (Make_Integer_Literal (Loc,
2311 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2312 Extra_Accessibility (Formal));
2313
2314 -- All other cases
2315
2316 else
2317 case Nkind (Prev_Orig) is
2318
2319 when N_Attribute_Reference =>
2320 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2321
2322 -- For X'Access, pass on the level of the prefix X
2323
2324 when Attribute_Access =>
2325 Add_Extra_Actual
2326 (Make_Integer_Literal (Loc,
2327 Intval =>
2328 Object_Access_Level
2329 (Prefix (Prev_Orig))),
2330 Extra_Accessibility (Formal));
2331
2332 -- Treat the unchecked attributes as library-level
2333
2334 when Attribute_Unchecked_Access |
2335 Attribute_Unrestricted_Access =>
2336 Add_Extra_Actual
2337 (Make_Integer_Literal (Loc,
2338 Intval => Scope_Depth (Standard_Standard)),
2339 Extra_Accessibility (Formal));
2340
2341 -- No other cases of attributes returning access
2342 -- values that can be passed to access parameters.
2343
2344 when others =>
2345 raise Program_Error;
2346
2347 end case;
2348
2349 -- For allocators we pass the level of the execution of the
2350 -- called subprogram, which is one greater than the current
2351 -- scope level.
2352
2353 when N_Allocator =>
2354 Add_Extra_Actual
2355 (Make_Integer_Literal (Loc,
2356 Intval => Scope_Depth (Current_Scope) + 1),
2357 Extra_Accessibility (Formal));
2358
2359 -- For other cases we simply pass the level of the actual's
2360 -- access type. The type is retrieved from Prev rather than
2361 -- Prev_Orig, because in some cases Prev_Orig denotes an
2362 -- original expression that has not been analyzed.
2363
2364 when others =>
2365 Add_Extra_Actual
2366 (Make_Integer_Literal (Loc,
2367 Intval => Type_Access_Level (Etype (Prev))),
2368 Extra_Accessibility (Formal));
2369 end case;
2370 end if;
2371 end if;
2372
2373 -- Perform the check of 4.6(49) that prevents a null value from being
2374 -- passed as an actual to an access parameter. Note that the check
2375 -- is elided in the common cases of passing an access attribute or
2376 -- access parameter as an actual. Also, we currently don't enforce
2377 -- this check for expander-generated actuals and when -gnatdj is set.
2378
2379 if Ada_Version >= Ada_2005 then
2380
2381 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2382 -- the intent of 6.4.1(13) is that null-exclusion checks should
2383 -- not be done for 'out' parameters, even though it refers only
2384 -- to constraint checks, and a null_exlusion is not a constraint.
2385 -- Note that AI05-0196-1 corrects this mistake in the RM.
2386
2387 if Is_Access_Type (Etype (Formal))
2388 and then Can_Never_Be_Null (Etype (Formal))
2389 and then Ekind (Formal) /= E_Out_Parameter
2390 and then Nkind (Prev) /= N_Raise_Constraint_Error
2391 and then (Known_Null (Prev)
2392 or else not Can_Never_Be_Null (Etype (Prev)))
2393 then
2394 Install_Null_Excluding_Check (Prev);
2395 end if;
2396
2397 -- Ada_Version < Ada_2005
2398
2399 else
2400 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2401 or else Access_Checks_Suppressed (Subp)
2402 then
2403 null;
2404
2405 elsif Debug_Flag_J then
2406 null;
2407
2408 elsif not Comes_From_Source (Prev) then
2409 null;
2410
2411 elsif Is_Entity_Name (Prev)
2412 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2413 then
2414 null;
2415
2416 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2417 null;
2418
2419 -- Suppress null checks when passing to access parameters of Java
2420 -- and CIL subprograms. (Should this be done for other foreign
2421 -- conventions as well ???)
2422
2423 elsif Convention (Subp) = Convention_Java
2424 or else Convention (Subp) = Convention_CIL
2425 then
2426 null;
2427
2428 else
2429 Install_Null_Excluding_Check (Prev);
2430 end if;
2431 end if;
2432
2433 -- Perform appropriate validity checks on parameters that
2434 -- are entities.
2435
2436 if Validity_Checks_On then
2437 if (Ekind (Formal) = E_In_Parameter
2438 and then Validity_Check_In_Params)
2439 or else
2440 (Ekind (Formal) = E_In_Out_Parameter
2441 and then Validity_Check_In_Out_Params)
2442 then
2443 -- If the actual is an indexed component of a packed type (or
2444 -- is an indexed or selected component whose prefix recursively
2445 -- meets this condition), it has not been expanded yet. It will
2446 -- be copied in the validity code that follows, and has to be
2447 -- expanded appropriately, so reanalyze it.
2448
2449 -- What we do is just to unset analyzed bits on prefixes till
2450 -- we reach something that does not have a prefix.
2451
2452 declare
2453 Nod : Node_Id;
2454
2455 begin
2456 Nod := Actual;
2457 while Nkind_In (Nod, N_Indexed_Component,
2458 N_Selected_Component)
2459 loop
2460 Set_Analyzed (Nod, False);
2461 Nod := Prefix (Nod);
2462 end loop;
2463 end;
2464
2465 Ensure_Valid (Actual);
2466 end if;
2467 end if;
2468
2469 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2470 -- since this is a left side reference. We only do this for calls
2471 -- from the source program since we assume that compiler generated
2472 -- calls explicitly generate any required checks. We also need it
2473 -- only if we are doing standard validity checks, since clearly it is
2474 -- not needed if validity checks are off, and in subscript validity
2475 -- checking mode, all indexed components are checked with a call
2476 -- directly from Expand_N_Indexed_Component.
2477
2478 if Comes_From_Source (Call_Node)
2479 and then Ekind (Formal) /= E_In_Parameter
2480 and then Validity_Checks_On
2481 and then Validity_Check_Default
2482 and then not Validity_Check_Subscripts
2483 then
2484 Check_Valid_Lvalue_Subscripts (Actual);
2485 end if;
2486
2487 -- Mark any scalar OUT parameter that is a simple variable as no
2488 -- longer known to be valid (unless the type is always valid). This
2489 -- reflects the fact that if an OUT parameter is never set in a
2490 -- procedure, then it can become invalid on the procedure return.
2491
2492 if Ekind (Formal) = E_Out_Parameter
2493 and then Is_Entity_Name (Actual)
2494 and then Ekind (Entity (Actual)) = E_Variable
2495 and then not Is_Known_Valid (Etype (Actual))
2496 then
2497 Set_Is_Known_Valid (Entity (Actual), False);
2498 end if;
2499
2500 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2501 -- clear current values, since they can be clobbered. We are probably
2502 -- doing this in more places than we need to, but better safe than
2503 -- sorry when it comes to retaining bad current values!
2504
2505 if Ekind (Formal) /= E_In_Parameter
2506 and then Is_Entity_Name (Actual)
2507 and then Present (Entity (Actual))
2508 then
2509 declare
2510 Ent : constant Entity_Id := Entity (Actual);
2511 Sav : Node_Id;
2512
2513 begin
2514 -- For an OUT or IN OUT parameter that is an assignable entity,
2515 -- we do not want to clobber the Last_Assignment field, since
2516 -- if it is set, it was precisely because it is indeed an OUT
2517 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
2518 -- since the subprogram could have returned in invalid value.
2519
2520 if (Ekind (Formal) = E_Out_Parameter
2521 or else
2522 Ekind (Formal) = E_In_Out_Parameter)
2523 and then Is_Assignable (Ent)
2524 then
2525 Sav := Last_Assignment (Ent);
2526 Kill_Current_Values (Ent);
2527 Set_Last_Assignment (Ent, Sav);
2528 Set_Is_Known_Valid (Ent, False);
2529
2530 -- For all other cases, just kill the current values
2531
2532 else
2533 Kill_Current_Values (Ent);
2534 end if;
2535 end;
2536 end if;
2537
2538 -- If the formal is class wide and the actual is an aggregate, force
2539 -- evaluation so that the back end who does not know about class-wide
2540 -- type, does not generate a temporary of the wrong size.
2541
2542 if not Is_Class_Wide_Type (Etype (Formal)) then
2543 null;
2544
2545 elsif Nkind (Actual) = N_Aggregate
2546 or else (Nkind (Actual) = N_Qualified_Expression
2547 and then Nkind (Expression (Actual)) = N_Aggregate)
2548 then
2549 Force_Evaluation (Actual);
2550 end if;
2551
2552 -- In a remote call, if the formal is of a class-wide type, check
2553 -- that the actual meets the requirements described in E.4(18).
2554
2555 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
2556 Insert_Action (Actual,
2557 Make_Transportable_Check (Loc,
2558 Duplicate_Subexpr_Move_Checks (Actual)));
2559 end if;
2560
2561 -- This label is required when skipping extra actual generation for
2562 -- Unchecked_Union parameters.
2563
2564 <<Skip_Extra_Actual_Generation>>
2565
2566 Param_Count := Param_Count + 1;
2567 Next_Actual (Actual);
2568 Next_Formal (Formal);
2569 end loop;
2570
2571 -- If we are expanding a rhs of an assignment we need to check if tag
2572 -- propagation is needed. You might expect this processing to be in
2573 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
2574 -- assignment might be transformed to a declaration for an unconstrained
2575 -- value if the expression is classwide.
2576
2577 if Nkind (Call_Node) = N_Function_Call
2578 and then Is_Tag_Indeterminate (Call_Node)
2579 and then Is_Entity_Name (Name (Call_Node))
2580 then
2581 declare
2582 Ass : Node_Id := Empty;
2583
2584 begin
2585 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
2586 Ass := Parent (Call_Node);
2587
2588 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
2589 and then Nkind (Parent (Parent (Call_Node)))
2590 = N_Assignment_Statement
2591 then
2592 Ass := Parent (Parent (Call_Node));
2593
2594 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
2595 and then Nkind (Parent (Parent (Call_Node)))
2596 = N_Assignment_Statement
2597 then
2598 Ass := Parent (Parent (Call_Node));
2599 end if;
2600
2601 if Present (Ass)
2602 and then Is_Class_Wide_Type (Etype (Name (Ass)))
2603 then
2604 if Is_Access_Type (Etype (Call_Node)) then
2605 if Designated_Type (Etype (Call_Node)) /=
2606 Root_Type (Etype (Name (Ass)))
2607 then
2608 Error_Msg_NE
2609 ("tag-indeterminate expression "
2610 & " must have designated type& (RM 5.2 (6))",
2611 Call_Node, Root_Type (Etype (Name (Ass))));
2612 else
2613 Propagate_Tag (Name (Ass), Call_Node);
2614 end if;
2615
2616 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
2617 Error_Msg_NE
2618 ("tag-indeterminate expression must have type&"
2619 & "(RM 5.2 (6))",
2620 Call_Node, Root_Type (Etype (Name (Ass))));
2621
2622 else
2623 Propagate_Tag (Name (Ass), Call_Node);
2624 end if;
2625
2626 -- The call will be rewritten as a dispatching call, and
2627 -- expanded as such.
2628
2629 return;
2630 end if;
2631 end;
2632 end if;
2633
2634 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
2635 -- it to point to the correct secondary virtual table
2636
2637 if Nkind_In (Call_Node, N_Function_Call, N_Procedure_Call_Statement)
2638 and then CW_Interface_Formals_Present
2639 then
2640 Expand_Interface_Actuals (Call_Node);
2641 end if;
2642
2643 -- Deals with Dispatch_Call if we still have a call, before expanding
2644 -- extra actuals since this will be done on the re-analysis of the
2645 -- dispatching call. Note that we do not try to shorten the actual list
2646 -- for a dispatching call, it would not make sense to do so. Expansion
2647 -- of dispatching calls is suppressed when VM_Target, because the VM
2648 -- back-ends directly handle the generation of dispatching calls and
2649 -- would have to undo any expansion to an indirect call.
2650
2651 if Nkind_In (Call_Node, N_Function_Call, N_Procedure_Call_Statement)
2652 and then Present (Controlling_Argument (Call_Node))
2653 then
2654 declare
2655 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
2656 Eq_Prim_Op : Entity_Id := Empty;
2657
2658 begin
2659 if not Is_Limited_Type (Typ) then
2660 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
2661 end if;
2662
2663 if Tagged_Type_Expansion then
2664 Expand_Dispatching_Call (Call_Node);
2665
2666 -- The following return is worrisome. Is it really OK to skip
2667 -- all remaining processing in this procedure ???
2668
2669 return;
2670
2671 -- VM targets
2672
2673 else
2674 Apply_Tag_Checks (Call_Node);
2675
2676 -- Expansion of a dispatching call results in an indirect call,
2677 -- which in turn causes current values to be killed (see
2678 -- Resolve_Call), so on VM targets we do the call here to
2679 -- ensure consistent warnings between VM and non-VM targets.
2680
2681 Kill_Current_Values;
2682 end if;
2683
2684 -- If this is a dispatching "=" then we must update the reference
2685 -- to the call node because we generated:
2686 -- x.tag = y.tag and then x = y
2687
2688 if Subp = Eq_Prim_Op
2689 and then Nkind (Call_Node) = N_Op_And
2690 then
2691 Call_Node := Right_Opnd (Call_Node);
2692 end if;
2693 end;
2694 end if;
2695
2696 -- Similarly, expand calls to RCI subprograms on which pragma
2697 -- All_Calls_Remote applies. The rewriting will be reanalyzed
2698 -- later. Do this only when the call comes from source since we
2699 -- do not want such a rewriting to occur in expanded code.
2700
2701 if Is_All_Remote_Call (Call_Node) then
2702 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
2703
2704 -- Similarly, do not add extra actuals for an entry call whose entity
2705 -- is a protected procedure, or for an internal protected subprogram
2706 -- call, because it will be rewritten as a protected subprogram call
2707 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
2708
2709 elsif Is_Protected_Type (Scope (Subp))
2710 and then (Ekind (Subp) = E_Procedure
2711 or else Ekind (Subp) = E_Function)
2712 then
2713 null;
2714
2715 -- During that loop we gathered the extra actuals (the ones that
2716 -- correspond to Extra_Formals), so now they can be appended.
2717
2718 else
2719 while Is_Non_Empty_List (Extra_Actuals) loop
2720 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2721 end loop;
2722 end if;
2723
2724 -- At this point we have all the actuals, so this is the point at which
2725 -- the various expansion activities for actuals is carried out.
2726
2727 Expand_Actuals (Call_Node, Subp);
2728
2729 -- If the subprogram is a renaming, or if it is inherited, replace it in
2730 -- the call with the name of the actual subprogram being called. If this
2731 -- is a dispatching call, the run-time decides what to call. The Alias
2732 -- attribute does not apply to entries.
2733
2734 if Nkind (Call_Node) /= N_Entry_Call_Statement
2735 and then No (Controlling_Argument (Call_Node))
2736 and then Present (Parent_Subp)
2737 then
2738 if Present (Inherited_From_Formal (Subp)) then
2739 Parent_Subp := Inherited_From_Formal (Subp);
2740 else
2741 Parent_Subp := Ultimate_Alias (Parent_Subp);
2742 end if;
2743
2744 -- The below setting of Entity is suspect, see F109-018 discussion???
2745
2746 Set_Entity (Name (Call_Node), Parent_Subp);
2747
2748 if Is_Abstract_Subprogram (Parent_Subp)
2749 and then not In_Instance
2750 then
2751 Error_Msg_NE
2752 ("cannot call abstract subprogram &!",
2753 Name (Call_Node), Parent_Subp);
2754 end if;
2755
2756 -- Inspect all formals of derived subprogram Subp. Compare parameter
2757 -- types with the parent subprogram and check whether an actual may
2758 -- need a type conversion to the corresponding formal of the parent
2759 -- subprogram.
2760
2761 -- Not clear whether intrinsic subprograms need such conversions. ???
2762
2763 if not Is_Intrinsic_Subprogram (Parent_Subp)
2764 or else Is_Generic_Instance (Parent_Subp)
2765 then
2766 declare
2767 procedure Convert (Act : Node_Id; Typ : Entity_Id);
2768 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
2769 -- and resolve the newly generated construct.
2770
2771 -------------
2772 -- Convert --
2773 -------------
2774
2775 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
2776 begin
2777 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
2778 Analyze (Act);
2779 Resolve (Act, Typ);
2780 end Convert;
2781
2782 -- Local variables
2783
2784 Actual_Typ : Entity_Id;
2785 Formal_Typ : Entity_Id;
2786 Parent_Typ : Entity_Id;
2787
2788 begin
2789 Actual := First_Actual (Call_Node);
2790 Formal := First_Formal (Subp);
2791 Parent_Formal := First_Formal (Parent_Subp);
2792 while Present (Formal) loop
2793 Actual_Typ := Etype (Actual);
2794 Formal_Typ := Etype (Formal);
2795 Parent_Typ := Etype (Parent_Formal);
2796
2797 -- For an IN parameter of a scalar type, the parent formal
2798 -- type and derived formal type differ or the parent formal
2799 -- type and actual type do not match statically.
2800
2801 if Is_Scalar_Type (Formal_Typ)
2802 and then Ekind (Formal) = E_In_Parameter
2803 and then Formal_Typ /= Parent_Typ
2804 and then
2805 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
2806 and then not Raises_Constraint_Error (Actual)
2807 then
2808 Convert (Actual, Parent_Typ);
2809 Enable_Range_Check (Actual);
2810
2811 -- If the actual has been marked as requiring a range
2812 -- check, then generate it here.
2813
2814 if Do_Range_Check (Actual) then
2815 Set_Do_Range_Check (Actual, False);
2816 Generate_Range_Check
2817 (Actual, Etype (Formal), CE_Range_Check_Failed);
2818 end if;
2819
2820 -- For access types, the parent formal type and actual type
2821 -- differ.
2822
2823 elsif Is_Access_Type (Formal_Typ)
2824 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
2825 then
2826 if Ekind (Formal) /= E_In_Parameter then
2827 Convert (Actual, Parent_Typ);
2828
2829 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
2830 and then Designated_Type (Parent_Typ) /=
2831 Designated_Type (Actual_Typ)
2832 and then not Is_Controlling_Formal (Formal)
2833 then
2834 -- This unchecked conversion is not necessary unless
2835 -- inlining is enabled, because in that case the type
2836 -- mismatch may become visible in the body about to be
2837 -- inlined.
2838
2839 Rewrite (Actual,
2840 Unchecked_Convert_To (Parent_Typ,
2841 Relocate_Node (Actual)));
2842 Analyze (Actual);
2843 Resolve (Actual, Parent_Typ);
2844 end if;
2845
2846 -- For array and record types, the parent formal type and
2847 -- derived formal type have different sizes or pragma Pack
2848 -- status.
2849
2850 elsif ((Is_Array_Type (Formal_Typ)
2851 and then Is_Array_Type (Parent_Typ))
2852 or else
2853 (Is_Record_Type (Formal_Typ)
2854 and then Is_Record_Type (Parent_Typ)))
2855 and then
2856 (Esize (Formal_Typ) /= Esize (Parent_Typ)
2857 or else Has_Pragma_Pack (Formal_Typ) /=
2858 Has_Pragma_Pack (Parent_Typ))
2859 then
2860 Convert (Actual, Parent_Typ);
2861 end if;
2862
2863 Next_Actual (Actual);
2864 Next_Formal (Formal);
2865 Next_Formal (Parent_Formal);
2866 end loop;
2867 end;
2868 end if;
2869
2870 Orig_Subp := Subp;
2871 Subp := Parent_Subp;
2872 end if;
2873
2874 -- Check for violation of No_Abort_Statements
2875
2876 if Is_RTE (Subp, RE_Abort_Task) then
2877 Check_Restriction (No_Abort_Statements, Call_Node);
2878
2879 -- Check for violation of No_Dynamic_Attachment
2880
2881 elsif RTU_Loaded (Ada_Interrupts)
2882 and then (Is_RTE (Subp, RE_Is_Reserved) or else
2883 Is_RTE (Subp, RE_Is_Attached) or else
2884 Is_RTE (Subp, RE_Current_Handler) or else
2885 Is_RTE (Subp, RE_Attach_Handler) or else
2886 Is_RTE (Subp, RE_Exchange_Handler) or else
2887 Is_RTE (Subp, RE_Detach_Handler) or else
2888 Is_RTE (Subp, RE_Reference))
2889 then
2890 Check_Restriction (No_Dynamic_Attachment, Call_Node);
2891 end if;
2892
2893 -- Deal with case where call is an explicit dereference
2894
2895 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2896
2897 -- Handle case of access to protected subprogram type
2898
2899 if Is_Access_Protected_Subprogram_Type
2900 (Base_Type (Etype (Prefix (Name (Call_Node)))))
2901 then
2902 -- If this is a call through an access to protected operation, the
2903 -- prefix has the form (object'address, operation'access). Rewrite
2904 -- as a for other protected calls: the object is the 1st parameter
2905 -- of the list of actuals.
2906
2907 declare
2908 Call : Node_Id;
2909 Parm : List_Id;
2910 Nam : Node_Id;
2911 Obj : Node_Id;
2912 Ptr : constant Node_Id := Prefix (Name (Call_Node));
2913
2914 T : constant Entity_Id :=
2915 Equivalent_Type (Base_Type (Etype (Ptr)));
2916
2917 D_T : constant Entity_Id :=
2918 Designated_Type (Base_Type (Etype (Ptr)));
2919
2920 begin
2921 Obj :=
2922 Make_Selected_Component (Loc,
2923 Prefix => Unchecked_Convert_To (T, Ptr),
2924 Selector_Name =>
2925 New_Occurrence_Of (First_Entity (T), Loc));
2926
2927 Nam :=
2928 Make_Selected_Component (Loc,
2929 Prefix => Unchecked_Convert_To (T, Ptr),
2930 Selector_Name =>
2931 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
2932
2933 Nam :=
2934 Make_Explicit_Dereference (Loc,
2935 Prefix => Nam);
2936
2937 if Present (Parameter_Associations (Call_Node)) then
2938 Parm := Parameter_Associations (Call_Node);
2939 else
2940 Parm := New_List;
2941 end if;
2942
2943 Prepend (Obj, Parm);
2944
2945 if Etype (D_T) = Standard_Void_Type then
2946 Call :=
2947 Make_Procedure_Call_Statement (Loc,
2948 Name => Nam,
2949 Parameter_Associations => Parm);
2950 else
2951 Call :=
2952 Make_Function_Call (Loc,
2953 Name => Nam,
2954 Parameter_Associations => Parm);
2955 end if;
2956
2957 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
2958 Set_Etype (Call, Etype (D_T));
2959
2960 -- We do not re-analyze the call to avoid infinite recursion.
2961 -- We analyze separately the prefix and the object, and set
2962 -- the checks on the prefix that would otherwise be emitted
2963 -- when resolving a call.
2964
2965 Rewrite (Call_Node, Call);
2966 Analyze (Nam);
2967 Apply_Access_Check (Nam);
2968 Analyze (Obj);
2969 return;
2970 end;
2971 end if;
2972 end if;
2973
2974 -- If this is a call to an intrinsic subprogram, then perform the
2975 -- appropriate expansion to the corresponding tree node and we
2976 -- are all done (since after that the call is gone!)
2977
2978 -- In the case where the intrinsic is to be processed by the back end,
2979 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
2980 -- since the idea in this case is to pass the call unchanged. If the
2981 -- intrinsic is an inherited unchecked conversion, and the derived type
2982 -- is the target type of the conversion, we must retain it as the return
2983 -- type of the expression. Otherwise the expansion below, which uses the
2984 -- parent operation, will yield the wrong type.
2985
2986 if Is_Intrinsic_Subprogram (Subp) then
2987 Expand_Intrinsic_Call (Call_Node, Subp);
2988
2989 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
2990 and then Parent_Subp /= Orig_Subp
2991 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
2992 then
2993 Set_Etype (Call_Node, Etype (Orig_Subp));
2994 end if;
2995
2996 return;
2997 end if;
2998
2999 if Ekind_In (Subp, E_Function, E_Procedure) then
3000
3001 -- We perform two simple optimization on calls:
3002
3003 -- a) replace calls to null procedures unconditionally;
3004
3005 -- b) for To_Address, just do an unchecked conversion. Not only is
3006 -- this efficient, but it also avoids order of elaboration problems
3007 -- when address clauses are inlined (address expression elaborated
3008 -- at the wrong point).
3009
3010 -- We perform these optimization regardless of whether we are in the
3011 -- main unit or in a unit in the context of the main unit, to ensure
3012 -- that tree generated is the same in both cases, for Inspector use.
3013
3014 if Is_RTE (Subp, RE_To_Address) then
3015 Rewrite (Call_Node,
3016 Unchecked_Convert_To
3017 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3018 return;
3019
3020 elsif Is_Null_Procedure (Subp) then
3021 Rewrite (Call_Node, Make_Null_Statement (Loc));
3022 return;
3023 end if;
3024
3025 if Is_Inlined (Subp) then
3026
3027 Inlined_Subprogram : declare
3028 Bod : Node_Id;
3029 Must_Inline : Boolean := False;
3030 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3031 Scop : constant Entity_Id := Scope (Subp);
3032
3033 function In_Unfrozen_Instance return Boolean;
3034 -- If the subprogram comes from an instance in the same unit,
3035 -- and the instance is not yet frozen, inlining might trigger
3036 -- order-of-elaboration problems in gigi.
3037
3038 --------------------------
3039 -- In_Unfrozen_Instance --
3040 --------------------------
3041
3042 function In_Unfrozen_Instance return Boolean is
3043 S : Entity_Id;
3044
3045 begin
3046 S := Scop;
3047 while Present (S)
3048 and then S /= Standard_Standard
3049 loop
3050 if Is_Generic_Instance (S)
3051 and then Present (Freeze_Node (S))
3052 and then not Analyzed (Freeze_Node (S))
3053 then
3054 return True;
3055 end if;
3056
3057 S := Scope (S);
3058 end loop;
3059
3060 return False;
3061 end In_Unfrozen_Instance;
3062
3063 -- Start of processing for Inlined_Subprogram
3064
3065 begin
3066 -- Verify that the body to inline has already been seen, and
3067 -- that if the body is in the current unit the inlining does
3068 -- not occur earlier. This avoids order-of-elaboration problems
3069 -- in the back end.
3070
3071 -- This should be documented in sinfo/einfo ???
3072
3073 if No (Spec)
3074 or else Nkind (Spec) /= N_Subprogram_Declaration
3075 or else No (Body_To_Inline (Spec))
3076 then
3077 Must_Inline := False;
3078
3079 -- If this an inherited function that returns a private type,
3080 -- do not inline if the full view is an unconstrained array,
3081 -- because such calls cannot be inlined.
3082
3083 elsif Present (Orig_Subp)
3084 and then Is_Array_Type (Etype (Orig_Subp))
3085 and then not Is_Constrained (Etype (Orig_Subp))
3086 then
3087 Must_Inline := False;
3088
3089 elsif In_Unfrozen_Instance then
3090 Must_Inline := False;
3091
3092 else
3093 Bod := Body_To_Inline (Spec);
3094
3095 if (In_Extended_Main_Code_Unit (Call_Node)
3096 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3097 or else Has_Pragma_Inline_Always (Subp))
3098 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3099 or else
3100 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3101 then
3102 Must_Inline := True;
3103
3104 -- If we are compiling a package body that is not the main
3105 -- unit, it must be for inlining/instantiation purposes,
3106 -- in which case we inline the call to insure that the same
3107 -- temporaries are generated when compiling the body by
3108 -- itself. Otherwise link errors can occur.
3109
3110 -- If the function being called is itself in the main unit,
3111 -- we cannot inline, because there is a risk of double
3112 -- elaboration and/or circularity: the inlining can make
3113 -- visible a private entity in the body of the main unit,
3114 -- that gigi will see before its sees its proper definition.
3115
3116 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3117 and then In_Package_Body
3118 then
3119 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3120 end if;
3121 end if;
3122
3123 if Must_Inline then
3124 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3125
3126 else
3127 -- Let the back end handle it
3128
3129 Add_Inlined_Body (Subp);
3130
3131 if Front_End_Inlining
3132 and then Nkind (Spec) = N_Subprogram_Declaration
3133 and then (In_Extended_Main_Code_Unit (Call_Node))
3134 and then No (Body_To_Inline (Spec))
3135 and then not Has_Completion (Subp)
3136 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3137 then
3138 Cannot_Inline
3139 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
3140 end if;
3141 end if;
3142 end Inlined_Subprogram;
3143 end if;
3144 end if;
3145
3146 -- Check for protected subprogram. This is either an intra-object call,
3147 -- or a protected function call. Protected procedure calls are rewritten
3148 -- as entry calls and handled accordingly.
3149
3150 -- In Ada 2005, this may be an indirect call to an access parameter that
3151 -- is an access_to_subprogram. In that case the anonymous type has a
3152 -- scope that is a protected operation, but the call is a regular one.
3153 -- In either case do not expand call if subprogram is eliminated.
3154
3155 Scop := Scope (Subp);
3156
3157 if Nkind (Call_Node) /= N_Entry_Call_Statement
3158 and then Is_Protected_Type (Scop)
3159 and then Ekind (Subp) /= E_Subprogram_Type
3160 and then not Is_Eliminated (Subp)
3161 then
3162 -- If the call is an internal one, it is rewritten as a call to the
3163 -- corresponding unprotected subprogram.
3164
3165 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3166 end if;
3167
3168 -- Functions returning controlled objects need special attention:
3169 -- if the return type is limited, the context is an initialization
3170 -- and different processing applies. If the call is to a protected
3171 -- function, the expansion above will call Expand_Call recusively.
3172 -- To prevent a double attachment, check that the current call is
3173 -- not a rewriting of a protected function call.
3174
3175 if Needs_Finalization (Etype (Subp)) then
3176 if not Is_Immutably_Limited_Type (Etype (Subp))
3177 and then
3178 (No (First_Formal (Subp))
3179 or else
3180 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3181 then
3182 Expand_Ctrl_Function_Call (Call_Node);
3183
3184 -- Build-in-place function calls which appear in anonymous contexts
3185 -- need a transient scope to ensure the proper finalization of the
3186 -- intermediate result after its use.
3187
3188 elsif Is_Build_In_Place_Function_Call (Call_Node)
3189 and then Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3190 N_Function_Call,
3191 N_Indexed_Component,
3192 N_Object_Renaming_Declaration,
3193 N_Procedure_Call_Statement,
3194 N_Selected_Component,
3195 N_Slice)
3196 then
3197 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3198 end if;
3199 end if;
3200
3201 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3202 -- if there are optional parameters at the trailing end.
3203 -- Note: we never delete procedures for call via a pointer.
3204
3205 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3206 and then Present (First_Optional_Parameter (Subp))
3207 then
3208 declare
3209 Last_Keep_Arg : Node_Id;
3210
3211 begin
3212 -- Last_Keep_Arg will hold the last actual that should be kept.
3213 -- If it remains empty at the end, it means that all parameters
3214 -- are optional.
3215
3216 Last_Keep_Arg := Empty;
3217
3218 -- Find first optional parameter, must be present since we checked
3219 -- the validity of the parameter before setting it.
3220
3221 Formal := First_Formal (Subp);
3222 Actual := First_Actual (Call_Node);
3223 while Formal /= First_Optional_Parameter (Subp) loop
3224 Last_Keep_Arg := Actual;
3225 Next_Formal (Formal);
3226 Next_Actual (Actual);
3227 end loop;
3228
3229 -- We have Formal and Actual pointing to the first potentially
3230 -- droppable argument. We can drop all the trailing arguments
3231 -- whose actual matches the default. Note that we know that all
3232 -- remaining formals have defaults, because we checked that this
3233 -- requirement was met before setting First_Optional_Parameter.
3234
3235 -- We use Fully_Conformant_Expressions to check for identity
3236 -- between formals and actuals, which may miss some cases, but
3237 -- on the other hand, this is only an optimization (if we fail
3238 -- to truncate a parameter it does not affect functionality).
3239 -- So if the default is 3 and the actual is 1+2, we consider
3240 -- them unequal, which hardly seems worrisome.
3241
3242 while Present (Formal) loop
3243 if not Fully_Conformant_Expressions
3244 (Actual, Default_Value (Formal))
3245 then
3246 Last_Keep_Arg := Actual;
3247 end if;
3248
3249 Next_Formal (Formal);
3250 Next_Actual (Actual);
3251 end loop;
3252
3253 -- If no arguments, delete entire list, this is the easy case
3254
3255 if No (Last_Keep_Arg) then
3256 Set_Parameter_Associations (Call_Node, No_List);
3257 Set_First_Named_Actual (Call_Node, Empty);
3258
3259 -- Case where at the last retained argument is positional. This
3260 -- is also an easy case, since the retained arguments are already
3261 -- in the right form, and we don't need to worry about the order
3262 -- of arguments that get eliminated.
3263
3264 elsif Is_List_Member (Last_Keep_Arg) then
3265 while Present (Next (Last_Keep_Arg)) loop
3266 Discard_Node (Remove_Next (Last_Keep_Arg));
3267 end loop;
3268
3269 Set_First_Named_Actual (Call_Node, Empty);
3270
3271 -- This is the annoying case where the last retained argument
3272 -- is a named parameter. Since the original arguments are not
3273 -- in declaration order, we may have to delete some fairly
3274 -- random collection of arguments.
3275
3276 else
3277 declare
3278 Temp : Node_Id;
3279 Passoc : Node_Id;
3280
3281 begin
3282 -- First step, remove all the named parameters from the
3283 -- list (they are still chained using First_Named_Actual
3284 -- and Next_Named_Actual, so we have not lost them!)
3285
3286 Temp := First (Parameter_Associations (Call_Node));
3287
3288 -- Case of all parameters named, remove them all
3289
3290 if Nkind (Temp) = N_Parameter_Association then
3291 -- Suppress warnings to avoid warning on possible
3292 -- infinite loop (because Call_Node is not modified).
3293
3294 pragma Warnings (Off);
3295 while Is_Non_Empty_List
3296 (Parameter_Associations (Call_Node))
3297 loop
3298 Temp :=
3299 Remove_Head (Parameter_Associations (Call_Node));
3300 end loop;
3301 pragma Warnings (On);
3302
3303 -- Case of mixed positional/named, remove named parameters
3304
3305 else
3306 while Nkind (Next (Temp)) /= N_Parameter_Association loop
3307 Next (Temp);
3308 end loop;
3309
3310 while Present (Next (Temp)) loop
3311 Remove (Next (Temp));
3312 end loop;
3313 end if;
3314
3315 -- Now we loop through the named parameters, till we get
3316 -- to the last one to be retained, adding them to the list.
3317 -- Note that the Next_Named_Actual list does not need to be
3318 -- touched since we are only reordering them on the actual
3319 -- parameter association list.
3320
3321 Passoc := Parent (First_Named_Actual (Call_Node));
3322 loop
3323 Temp := Relocate_Node (Passoc);
3324 Append_To
3325 (Parameter_Associations (Call_Node), Temp);
3326 exit when
3327 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
3328 Passoc := Parent (Next_Named_Actual (Passoc));
3329 end loop;
3330
3331 Set_Next_Named_Actual (Temp, Empty);
3332
3333 loop
3334 Temp := Next_Named_Actual (Passoc);
3335 exit when No (Temp);
3336 Set_Next_Named_Actual
3337 (Passoc, Next_Named_Actual (Parent (Temp)));
3338 end loop;
3339 end;
3340
3341 end if;
3342 end;
3343 end if;
3344 end Expand_Call;
3345
3346 --------------------------
3347 -- Expand_Inlined_Call --
3348 --------------------------
3349
3350 procedure Expand_Inlined_Call
3351 (N : Node_Id;
3352 Subp : Entity_Id;
3353 Orig_Subp : Entity_Id)
3354 is
3355 Loc : constant Source_Ptr := Sloc (N);
3356 Is_Predef : constant Boolean :=
3357 Is_Predefined_File_Name
3358 (Unit_File_Name (Get_Source_Unit (Subp)));
3359 Orig_Bod : constant Node_Id :=
3360 Body_To_Inline (Unit_Declaration_Node (Subp));
3361
3362 Blk : Node_Id;
3363 Bod : Node_Id;
3364 Decl : Node_Id;
3365 Decls : constant List_Id := New_List;
3366 Exit_Lab : Entity_Id := Empty;
3367 F : Entity_Id;
3368 A : Node_Id;
3369 Lab_Decl : Node_Id;
3370 Lab_Id : Node_Id;
3371 New_A : Node_Id;
3372 Num_Ret : Int := 0;
3373 Ret_Type : Entity_Id;
3374 Targ : Node_Id;
3375 Targ1 : Node_Id;
3376 Temp : Entity_Id;
3377 Temp_Typ : Entity_Id;
3378
3379 Return_Object : Entity_Id := Empty;
3380 -- Entity in declaration in an extended_return_statement
3381
3382 Is_Unc : constant Boolean :=
3383 Is_Array_Type (Etype (Subp))
3384 and then not Is_Constrained (Etype (Subp));
3385 -- If the type returned by the function is unconstrained and the call
3386 -- can be inlined, special processing is required.
3387
3388 procedure Make_Exit_Label;
3389 -- Build declaration for exit label to be used in Return statements,
3390 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3391 -- declaration). Does nothing if Exit_Lab already set.
3392
3393 function Process_Formals (N : Node_Id) return Traverse_Result;
3394 -- Replace occurrence of a formal with the corresponding actual, or the
3395 -- thunk generated for it.
3396
3397 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
3398 -- If the call being expanded is that of an internal subprogram, set the
3399 -- sloc of the generated block to that of the call itself, so that the
3400 -- expansion is skipped by the "next" command in gdb.
3401 -- Same processing for a subprogram in a predefined file, e.g.
3402 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
3403 -- simplify our own development.
3404
3405 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3406 -- If the function body is a single expression, replace call with
3407 -- expression, else insert block appropriately.
3408
3409 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3410 -- If procedure body has no local variables, inline body without
3411 -- creating block, otherwise rewrite call with block.
3412
3413 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
3414 -- Determine whether a formal parameter is used only once in Orig_Bod
3415
3416 ---------------------
3417 -- Make_Exit_Label --
3418 ---------------------
3419
3420 procedure Make_Exit_Label is
3421 Lab_Ent : Entity_Id;
3422 begin
3423 if No (Exit_Lab) then
3424 Lab_Ent := Make_Temporary (Loc, 'L');
3425 Lab_Id := New_Reference_To (Lab_Ent, Loc);
3426 Exit_Lab := Make_Label (Loc, Lab_Id);
3427 Lab_Decl :=
3428 Make_Implicit_Label_Declaration (Loc,
3429 Defining_Identifier => Lab_Ent,
3430 Label_Construct => Exit_Lab);
3431 end if;
3432 end Make_Exit_Label;
3433
3434 ---------------------
3435 -- Process_Formals --
3436 ---------------------
3437
3438 function Process_Formals (N : Node_Id) return Traverse_Result is
3439 A : Entity_Id;
3440 E : Entity_Id;
3441 Ret : Node_Id;
3442
3443 begin
3444 if Is_Entity_Name (N)
3445 and then Present (Entity (N))
3446 then
3447 E := Entity (N);
3448
3449 if Is_Formal (E)
3450 and then Scope (E) = Subp
3451 then
3452 A := Renamed_Object (E);
3453
3454 -- Rewrite the occurrence of the formal into an occurrence of
3455 -- the actual. Also establish visibility on the proper view of
3456 -- the actual's subtype for the body's context (if the actual's
3457 -- subtype is private at the call point but its full view is
3458 -- visible to the body, then the inlined tree here must be
3459 -- analyzed with the full view).
3460
3461 if Is_Entity_Name (A) then
3462 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
3463 Check_Private_View (N);
3464
3465 elsif Nkind (A) = N_Defining_Identifier then
3466 Rewrite (N, New_Occurrence_Of (A, Loc));
3467 Check_Private_View (N);
3468
3469 -- Numeric literal
3470
3471 else
3472 Rewrite (N, New_Copy (A));
3473 end if;
3474 end if;
3475 return Skip;
3476
3477 elsif Is_Entity_Name (N)
3478 and then Present (Return_Object)
3479 and then Chars (N) = Chars (Return_Object)
3480 then
3481 -- Occurrence within an extended return statement. The return
3482 -- object is local to the body been inlined, and thus the generic
3483 -- copy is not analyzed yet, so we match by name, and replace it
3484 -- with target of call.
3485
3486 if Nkind (Targ) = N_Defining_Identifier then
3487 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3488 else
3489 Rewrite (N, New_Copy_Tree (Targ));
3490 end if;
3491
3492 return Skip;
3493
3494 elsif Nkind (N) = N_Simple_Return_Statement then
3495 if No (Expression (N)) then
3496 Make_Exit_Label;
3497 Rewrite (N,
3498 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3499
3500 else
3501 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3502 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3503 then
3504 -- Function body is a single expression. No need for
3505 -- exit label.
3506
3507 null;
3508
3509 else
3510 Num_Ret := Num_Ret + 1;
3511 Make_Exit_Label;
3512 end if;
3513
3514 -- Because of the presence of private types, the views of the
3515 -- expression and the context may be different, so place an
3516 -- unchecked conversion to the context type to avoid spurious
3517 -- errors, e.g. when the expression is a numeric literal and
3518 -- the context is private. If the expression is an aggregate,
3519 -- use a qualified expression, because an aggregate is not a
3520 -- legal argument of a conversion.
3521
3522 if Nkind_In (Expression (N), N_Aggregate, N_Null) then
3523 Ret :=
3524 Make_Qualified_Expression (Sloc (N),
3525 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3526 Expression => Relocate_Node (Expression (N)));
3527 else
3528 Ret :=
3529 Unchecked_Convert_To
3530 (Ret_Type, Relocate_Node (Expression (N)));
3531 end if;
3532
3533 if Nkind (Targ) = N_Defining_Identifier then
3534 Rewrite (N,
3535 Make_Assignment_Statement (Loc,
3536 Name => New_Occurrence_Of (Targ, Loc),
3537 Expression => Ret));
3538 else
3539 Rewrite (N,
3540 Make_Assignment_Statement (Loc,
3541 Name => New_Copy (Targ),
3542 Expression => Ret));
3543 end if;
3544
3545 Set_Assignment_OK (Name (N));
3546
3547 if Present (Exit_Lab) then
3548 Insert_After (N,
3549 Make_Goto_Statement (Loc,
3550 Name => New_Copy (Lab_Id)));
3551 end if;
3552 end if;
3553
3554 return OK;
3555
3556 elsif Nkind (N) = N_Extended_Return_Statement then
3557
3558 -- An extended return becomes a block whose first statement is
3559 -- the assignment of the initial expression of the return object
3560 -- to the target of the call itself.
3561
3562 declare
3563 Return_Decl : constant Entity_Id :=
3564 First (Return_Object_Declarations (N));
3565 Assign : Node_Id;
3566
3567 begin
3568 Return_Object := Defining_Identifier (Return_Decl);
3569
3570 if Present (Expression (Return_Decl)) then
3571 if Nkind (Targ) = N_Defining_Identifier then
3572 Assign :=
3573 Make_Assignment_Statement (Loc,
3574 Name => New_Occurrence_Of (Targ, Loc),
3575 Expression => Expression (Return_Decl));
3576 else
3577 Assign :=
3578 Make_Assignment_Statement (Loc,
3579 Name => New_Copy (Targ),
3580 Expression => Expression (Return_Decl));
3581 end if;
3582
3583 Set_Assignment_OK (Name (Assign));
3584 Prepend (Assign,
3585 Statements (Handled_Statement_Sequence (N)));
3586 end if;
3587
3588 Rewrite (N,
3589 Make_Block_Statement (Loc,
3590 Handled_Statement_Sequence =>
3591 Handled_Statement_Sequence (N)));
3592
3593 return OK;
3594 end;
3595
3596 -- Remove pragma Unreferenced since it may refer to formals that
3597 -- are not visible in the inlined body, and in any case we will
3598 -- not be posting warnings on the inlined body so it is unneeded.
3599
3600 elsif Nkind (N) = N_Pragma
3601 and then Pragma_Name (N) = Name_Unreferenced
3602 then
3603 Rewrite (N, Make_Null_Statement (Sloc (N)));
3604 return OK;
3605
3606 else
3607 return OK;
3608 end if;
3609 end Process_Formals;
3610
3611 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3612
3613 ------------------
3614 -- Process_Sloc --
3615 ------------------
3616
3617 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3618 begin
3619 if not Debug_Generated_Code then
3620 Set_Sloc (Nod, Sloc (N));
3621 Set_Comes_From_Source (Nod, False);
3622 end if;
3623
3624 return OK;
3625 end Process_Sloc;
3626
3627 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3628
3629 ---------------------------
3630 -- Rewrite_Function_Call --
3631 ---------------------------
3632
3633 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
3634 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3635 Fst : constant Node_Id := First (Statements (HSS));
3636
3637 begin
3638 -- Optimize simple case: function body is a single return statement,
3639 -- which has been expanded into an assignment.
3640
3641 if Is_Empty_List (Declarations (Blk))
3642 and then Nkind (Fst) = N_Assignment_Statement
3643 and then No (Next (Fst))
3644 then
3645
3646 -- The function call may have been rewritten as the temporary
3647 -- that holds the result of the call, in which case remove the
3648 -- now useless declaration.
3649
3650 if Nkind (N) = N_Identifier
3651 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3652 then
3653 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3654 end if;
3655
3656 Rewrite (N, Expression (Fst));
3657
3658 elsif Nkind (N) = N_Identifier
3659 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3660 then
3661 -- The block assigns the result of the call to the temporary
3662
3663 Insert_After (Parent (Entity (N)), Blk);
3664
3665 elsif Nkind (Parent (N)) = N_Assignment_Statement
3666 and then
3667 (Is_Entity_Name (Name (Parent (N)))
3668 or else
3669 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3670 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
3671 then
3672 -- Replace assignment with the block
3673
3674 declare
3675 Original_Assignment : constant Node_Id := Parent (N);
3676
3677 begin
3678 -- Preserve the original assignment node to keep the complete
3679 -- assignment subtree consistent enough for Analyze_Assignment
3680 -- to proceed (specifically, the original Lhs node must still
3681 -- have an assignment statement as its parent).
3682
3683 -- We cannot rely on Original_Node to go back from the block
3684 -- node to the assignment node, because the assignment might
3685 -- already be a rewrite substitution.
3686
3687 Discard_Node (Relocate_Node (Original_Assignment));
3688 Rewrite (Original_Assignment, Blk);
3689 end;
3690
3691 elsif Nkind (Parent (N)) = N_Object_Declaration then
3692 Set_Expression (Parent (N), Empty);
3693 Insert_After (Parent (N), Blk);
3694
3695 elsif Is_Unc then
3696 Insert_Before (Parent (N), Blk);
3697 end if;
3698 end Rewrite_Function_Call;
3699
3700 ----------------------------
3701 -- Rewrite_Procedure_Call --
3702 ----------------------------
3703
3704 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
3705 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3706 begin
3707 -- If there is a transient scope for N, this will be the scope of the
3708 -- actions for N, and the statements in Blk need to be within this
3709 -- scope. For example, they need to have visibility on the constant
3710 -- declarations created for the formals.
3711
3712 -- If N needs no transient scope, and if there are no declarations in
3713 -- the inlined body, we can do a little optimization and insert the
3714 -- statements for the body directly after N, and rewrite N to a
3715 -- null statement, instead of rewriting N into a full-blown block
3716 -- statement.
3717
3718 if not Scope_Is_Transient
3719 and then Is_Empty_List (Declarations (Blk))
3720 then
3721 Insert_List_After (N, Statements (HSS));
3722 Rewrite (N, Make_Null_Statement (Loc));
3723 else
3724 Rewrite (N, Blk);
3725 end if;
3726 end Rewrite_Procedure_Call;
3727
3728 -------------------------
3729 -- Formal_Is_Used_Once --
3730 -------------------------
3731
3732 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
3733 Use_Counter : Int := 0;
3734
3735 function Count_Uses (N : Node_Id) return Traverse_Result;
3736 -- Traverse the tree and count the uses of the formal parameter.
3737 -- In this case, for optimization purposes, we do not need to
3738 -- continue the traversal once more than one use is encountered.
3739
3740 ----------------
3741 -- Count_Uses --
3742 ----------------
3743
3744 function Count_Uses (N : Node_Id) return Traverse_Result is
3745 begin
3746 -- The original node is an identifier
3747
3748 if Nkind (N) = N_Identifier
3749 and then Present (Entity (N))
3750
3751 -- Original node's entity points to the one in the copied body
3752
3753 and then Nkind (Entity (N)) = N_Identifier
3754 and then Present (Entity (Entity (N)))
3755
3756 -- The entity of the copied node is the formal parameter
3757
3758 and then Entity (Entity (N)) = Formal
3759 then
3760 Use_Counter := Use_Counter + 1;
3761
3762 if Use_Counter > 1 then
3763
3764 -- Denote more than one use and abandon the traversal
3765
3766 Use_Counter := 2;
3767 return Abandon;
3768
3769 end if;
3770 end if;
3771
3772 return OK;
3773 end Count_Uses;
3774
3775 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
3776
3777 -- Start of processing for Formal_Is_Used_Once
3778
3779 begin
3780 Count_Formal_Uses (Orig_Bod);
3781 return Use_Counter = 1;
3782 end Formal_Is_Used_Once;
3783
3784 -- Start of processing for Expand_Inlined_Call
3785
3786 begin
3787
3788 -- Check for an illegal attempt to inline a recursive procedure. If the
3789 -- subprogram has parameters this is detected when trying to supply a
3790 -- binding for parameters that already have one. For parameterless
3791 -- subprograms this must be done explicitly.
3792
3793 if In_Open_Scopes (Subp) then
3794 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
3795 Set_Is_Inlined (Subp, False);
3796 return;
3797 end if;
3798
3799 if Nkind (Orig_Bod) = N_Defining_Identifier
3800 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
3801 then
3802 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3803 -- can be replaced with calls to the renamed entity directly, because
3804 -- the subprograms are subtype conformant. If the renamed subprogram
3805 -- is an inherited operation, we must redo the expansion because
3806 -- implicit conversions may be needed. Similarly, if the renamed
3807 -- entity is inlined, expand the call for further optimizations.
3808
3809 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
3810
3811 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
3812 Expand_Call (N);
3813 end if;
3814
3815 return;
3816 end if;
3817
3818 -- Use generic machinery to copy body of inlined subprogram, as if it
3819 -- were an instantiation, resetting source locations appropriately, so
3820 -- that nested inlined calls appear in the main unit.
3821
3822 Save_Env (Subp, Empty);
3823 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
3824
3825 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3826 Blk :=
3827 Make_Block_Statement (Loc,
3828 Declarations => Declarations (Bod),
3829 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
3830
3831 if No (Declarations (Bod)) then
3832 Set_Declarations (Blk, New_List);
3833 end if;
3834
3835 -- For the unconstrained case, capture the name of the local
3836 -- variable that holds the result. This must be the first declaration
3837 -- in the block, because its bounds cannot depend on local variables.
3838 -- Otherwise there is no way to declare the result outside of the
3839 -- block. Needless to say, in general the bounds will depend on the
3840 -- actuals in the call.
3841
3842 if Is_Unc then
3843 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3844 end if;
3845
3846 -- If this is a derived function, establish the proper return type
3847
3848 if Present (Orig_Subp)
3849 and then Orig_Subp /= Subp
3850 then
3851 Ret_Type := Etype (Orig_Subp);
3852 else
3853 Ret_Type := Etype (Subp);
3854 end if;
3855
3856 -- Create temporaries for the actuals that are expressions, or that
3857 -- are scalars and require copying to preserve semantics.
3858
3859 F := First_Formal (Subp);
3860 A := First_Actual (N);
3861 while Present (F) loop
3862 if Present (Renamed_Object (F)) then
3863 Error_Msg_N ("cannot inline call to recursive subprogram", N);
3864 return;
3865 end if;
3866
3867 -- If the argument may be a controlling argument in a call within
3868 -- the inlined body, we must preserve its classwide nature to insure
3869 -- that dynamic dispatching take place subsequently. If the formal
3870 -- has a constraint it must be preserved to retain the semantics of
3871 -- the body.
3872
3873 if Is_Class_Wide_Type (Etype (F))
3874 or else (Is_Access_Type (Etype (F))
3875 and then
3876 Is_Class_Wide_Type (Designated_Type (Etype (F))))
3877 then
3878 Temp_Typ := Etype (F);
3879
3880 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3881 and then Etype (F) /= Base_Type (Etype (F))
3882 then
3883 Temp_Typ := Etype (F);
3884
3885 else
3886 Temp_Typ := Etype (A);
3887 end if;
3888
3889 -- If the actual is a simple name or a literal, no need to
3890 -- create a temporary, object can be used directly.
3891
3892 -- If the actual is a literal and the formal has its address taken,
3893 -- we cannot pass the literal itself as an argument, so its value
3894 -- must be captured in a temporary.
3895
3896 if (Is_Entity_Name (A)
3897 and then
3898 (not Is_Scalar_Type (Etype (A))
3899 or else Ekind (Entity (A)) = E_Enumeration_Literal))
3900
3901 -- When the actual is an identifier and the corresponding formal
3902 -- is used only once in the original body, the formal can be
3903 -- substituted directly with the actual parameter.
3904
3905 or else (Nkind (A) = N_Identifier
3906 and then Formal_Is_Used_Once (F))
3907
3908 or else
3909 (Nkind_In (A, N_Real_Literal,
3910 N_Integer_Literal,
3911 N_Character_Literal)
3912 and then not Address_Taken (F))
3913 then
3914 if Etype (F) /= Etype (A) then
3915 Set_Renamed_Object
3916 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3917 else
3918 Set_Renamed_Object (F, A);
3919 end if;
3920
3921 else
3922 Temp := Make_Temporary (Loc, 'C');
3923
3924 -- If the actual for an in/in-out parameter is a view conversion,
3925 -- make it into an unchecked conversion, given that an untagged
3926 -- type conversion is not a proper object for a renaming.
3927
3928 -- In-out conversions that involve real conversions have already
3929 -- been transformed in Expand_Actuals.
3930
3931 if Nkind (A) = N_Type_Conversion
3932 and then Ekind (F) /= E_In_Parameter
3933 then
3934 New_A :=
3935 Make_Unchecked_Type_Conversion (Loc,
3936 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3937 Expression => Relocate_Node (Expression (A)));
3938
3939 elsif Etype (F) /= Etype (A) then
3940 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3941 Temp_Typ := Etype (F);
3942
3943 else
3944 New_A := Relocate_Node (A);
3945 end if;
3946
3947 Set_Sloc (New_A, Sloc (N));
3948
3949 -- If the actual has a by-reference type, it cannot be copied, so
3950 -- its value is captured in a renaming declaration. Otherwise
3951 -- declare a local constant initialized with the actual.
3952
3953 -- We also use a renaming declaration for expressions of an array
3954 -- type that is not bit-packed, both for efficiency reasons and to
3955 -- respect the semantics of the call: in most cases the original
3956 -- call will pass the parameter by reference, and thus the inlined
3957 -- code will have the same semantics.
3958
3959 if Ekind (F) = E_In_Parameter
3960 and then not Is_Limited_Type (Etype (A))
3961 and then not Is_Tagged_Type (Etype (A))
3962 and then
3963 (not Is_Array_Type (Etype (A))
3964 or else not Is_Object_Reference (A)
3965 or else Is_Bit_Packed_Array (Etype (A)))
3966 then
3967 Decl :=
3968 Make_Object_Declaration (Loc,
3969 Defining_Identifier => Temp,
3970 Constant_Present => True,
3971 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3972 Expression => New_A);
3973 else
3974 Decl :=
3975 Make_Object_Renaming_Declaration (Loc,
3976 Defining_Identifier => Temp,
3977 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3978 Name => New_A);
3979 end if;
3980
3981 Append (Decl, Decls);
3982 Set_Renamed_Object (F, Temp);
3983 end if;
3984
3985 Next_Formal (F);
3986 Next_Actual (A);
3987 end loop;
3988
3989 -- Establish target of function call. If context is not assignment or
3990 -- declaration, create a temporary as a target. The declaration for
3991 -- the temporary may be subsequently optimized away if the body is a
3992 -- single expression, or if the left-hand side of the assignment is
3993 -- simple enough, i.e. an entity or an explicit dereference of one.
3994
3995 if Ekind (Subp) = E_Function then
3996 if Nkind (Parent (N)) = N_Assignment_Statement
3997 and then Is_Entity_Name (Name (Parent (N)))
3998 then
3999 Targ := Name (Parent (N));
4000
4001 elsif Nkind (Parent (N)) = N_Assignment_Statement
4002 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4003 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4004 then
4005 Targ := Name (Parent (N));
4006
4007 elsif Nkind (Parent (N)) = N_Object_Declaration
4008 and then Is_Limited_Type (Etype (Subp))
4009 then
4010 Targ := Defining_Identifier (Parent (N));
4011
4012 else
4013 -- Replace call with temporary and create its declaration
4014
4015 Temp := Make_Temporary (Loc, 'C');
4016 Set_Is_Internal (Temp);
4017
4018 -- For the unconstrained case, the generated temporary has the
4019 -- same constrained declaration as the result variable. It may
4020 -- eventually be possible to remove that temporary and use the
4021 -- result variable directly.
4022
4023 if Is_Unc then
4024 Decl :=
4025 Make_Object_Declaration (Loc,
4026 Defining_Identifier => Temp,
4027 Object_Definition =>
4028 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4029
4030 Replace_Formals (Decl);
4031
4032 else
4033 Decl :=
4034 Make_Object_Declaration (Loc,
4035 Defining_Identifier => Temp,
4036 Object_Definition =>
4037 New_Occurrence_Of (Ret_Type, Loc));
4038
4039 Set_Etype (Temp, Ret_Type);
4040 end if;
4041
4042 Set_No_Initialization (Decl);
4043 Append (Decl, Decls);
4044 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4045 Targ := Temp;
4046 end if;
4047 end if;
4048
4049 Insert_Actions (N, Decls);
4050
4051 -- Traverse the tree and replace formals with actuals or their thunks.
4052 -- Attach block to tree before analysis and rewriting.
4053
4054 Replace_Formals (Blk);
4055 Set_Parent (Blk, N);
4056
4057 if not Comes_From_Source (Subp)
4058 or else Is_Predef
4059 then
4060 Reset_Slocs (Blk);
4061 end if;
4062
4063 if Present (Exit_Lab) then
4064
4065 -- If the body was a single expression, the single return statement
4066 -- and the corresponding label are useless.
4067
4068 if Num_Ret = 1
4069 and then
4070 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4071 N_Goto_Statement
4072 then
4073 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4074 else
4075 Append (Lab_Decl, (Declarations (Blk)));
4076 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
4077 end if;
4078 end if;
4079
4080 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
4081 -- conflicting private views that Gigi would ignore. If this is a
4082 -- predefined unit, analyze with checks off, as is done in the non-
4083 -- inlined run-time units.
4084
4085 declare
4086 I_Flag : constant Boolean := In_Inlined_Body;
4087
4088 begin
4089 In_Inlined_Body := True;
4090
4091 if Is_Predef then
4092 declare
4093 Style : constant Boolean := Style_Check;
4094 begin
4095 Style_Check := False;
4096 Analyze (Blk, Suppress => All_Checks);
4097 Style_Check := Style;
4098 end;
4099
4100 else
4101 Analyze (Blk);
4102 end if;
4103
4104 In_Inlined_Body := I_Flag;
4105 end;
4106
4107 if Ekind (Subp) = E_Procedure then
4108 Rewrite_Procedure_Call (N, Blk);
4109 else
4110 Rewrite_Function_Call (N, Blk);
4111
4112 -- For the unconstrained case, the replacement of the call has been
4113 -- made prior to the complete analysis of the generated declarations.
4114 -- Propagate the proper type now.
4115
4116 if Is_Unc then
4117 if Nkind (N) = N_Identifier then
4118 Set_Etype (N, Etype (Entity (N)));
4119 else
4120 Set_Etype (N, Etype (Targ1));
4121 end if;
4122 end if;
4123 end if;
4124
4125 Restore_Env;
4126
4127 -- Cleanup mapping between formals and actuals for other expansions
4128
4129 F := First_Formal (Subp);
4130 while Present (F) loop
4131 Set_Renamed_Object (F, Empty);
4132 Next_Formal (F);
4133 end loop;
4134 end Expand_Inlined_Call;
4135
4136 ----------------------------------------
4137 -- Expand_N_Extended_Return_Statement --
4138 ----------------------------------------
4139
4140 -- If there is a Handled_Statement_Sequence, we rewrite this:
4141
4142 -- return Result : T := <expression> do
4143 -- <handled_seq_of_stms>
4144 -- end return;
4145
4146 -- to be:
4147
4148 -- declare
4149 -- Result : T := <expression>;
4150 -- begin
4151 -- <handled_seq_of_stms>
4152 -- return Result;
4153 -- end;
4154
4155 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4156
4157 -- return Result : T := <expression>;
4158
4159 -- to be:
4160
4161 -- return <expression>;
4162
4163 -- unless it's build-in-place or there's no <expression>, in which case
4164 -- we generate:
4165
4166 -- declare
4167 -- Result : T := <expression>;
4168 -- begin
4169 -- return Result;
4170 -- end;
4171
4172 -- Note that this case could have been written by the user as an extended
4173 -- return statement, or could have been transformed to this from a simple
4174 -- return statement.
4175
4176 -- That is, we need to have a reified return object if there are statements
4177 -- (which might refer to it) or if we're doing build-in-place (so we can
4178 -- set its address to the final resting place or if there is no expression
4179 -- (in which case default initial values might need to be set).
4180
4181 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4182 Loc : constant Source_Ptr := Sloc (N);
4183
4184 Return_Object_Entity : constant Entity_Id :=
4185 First_Entity (Return_Statement_Entity (N));
4186 Return_Object_Decl : constant Node_Id :=
4187 Parent (Return_Object_Entity);
4188 Parent_Function : constant Entity_Id :=
4189 Return_Applies_To (Return_Statement_Entity (N));
4190 Parent_Function_Typ : constant Entity_Id := Etype (Parent_Function);
4191 Is_Build_In_Place : constant Boolean :=
4192 Is_Build_In_Place_Function (Parent_Function);
4193
4194 Return_Stm : Node_Id;
4195 Statements : List_Id;
4196 Handled_Stm_Seq : Node_Id;
4197 Result : Node_Id;
4198 Exp : Node_Id;
4199
4200 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean;
4201 -- Determine whether type Typ is controlled or contains a controlled
4202 -- subcomponent.
4203
4204 function Move_Activation_Chain return Node_Id;
4205 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4206 -- with parameters:
4207 -- From current activation chain
4208 -- To activation chain passed in by the caller
4209 -- New_Master master passed in by the caller
4210
4211 function Move_Final_List return Node_Id;
4212 -- Construct call to System.Finalization_Implementation.Move_Final_List
4213 -- with parameters:
4214 --
4215 -- From finalization list of the return statement
4216 -- To finalization list passed in by the caller
4217
4218 --------------------------
4219 -- Has_Controlled_Parts --
4220 --------------------------
4221
4222 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean is
4223 begin
4224 return
4225 Is_Controlled (Typ)
4226 or else Has_Controlled_Component (Typ);
4227 end Has_Controlled_Parts;
4228
4229 ---------------------------
4230 -- Move_Activation_Chain --
4231 ---------------------------
4232
4233 function Move_Activation_Chain return Node_Id is
4234 Activation_Chain_Formal : constant Entity_Id :=
4235 Build_In_Place_Formal
4236 (Parent_Function, BIP_Activation_Chain);
4237 To : constant Node_Id :=
4238 New_Reference_To
4239 (Activation_Chain_Formal, Loc);
4240 Master_Formal : constant Entity_Id :=
4241 Build_In_Place_Formal
4242 (Parent_Function, BIP_Master);
4243 New_Master : constant Node_Id :=
4244 New_Reference_To (Master_Formal, Loc);
4245
4246 Chain_Entity : Entity_Id;
4247 From : Node_Id;
4248
4249 begin
4250 Chain_Entity := First_Entity (Return_Statement_Entity (N));
4251 while Chars (Chain_Entity) /= Name_uChain loop
4252 Chain_Entity := Next_Entity (Chain_Entity);
4253 end loop;
4254
4255 From :=
4256 Make_Attribute_Reference (Loc,
4257 Prefix => New_Reference_To (Chain_Entity, Loc),
4258 Attribute_Name => Name_Unrestricted_Access);
4259 -- ??? Not clear why "Make_Identifier (Loc, Name_uChain)" doesn't
4260 -- work, instead of "New_Reference_To (Chain_Entity, Loc)" above.
4261
4262 return
4263 Make_Procedure_Call_Statement (Loc,
4264 Name => New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
4265 Parameter_Associations => New_List (From, To, New_Master));
4266 end Move_Activation_Chain;
4267
4268 ---------------------
4269 -- Move_Final_List --
4270 ---------------------
4271
4272 function Move_Final_List return Node_Id is
4273 Flist : constant Entity_Id :=
4274 Finalization_Chain_Entity (Return_Statement_Entity (N));
4275
4276 From : constant Node_Id := New_Reference_To (Flist, Loc);
4277
4278 Caller_Final_List : constant Entity_Id :=
4279 Build_In_Place_Formal
4280 (Parent_Function, BIP_Final_List);
4281
4282 To : constant Node_Id := New_Reference_To (Caller_Final_List, Loc);
4283
4284 begin
4285 -- Catch cases where a finalization chain entity has not been
4286 -- associated with the return statement entity.
4287
4288 pragma Assert (Present (Flist));
4289
4290 -- Build required call
4291
4292 return
4293 Make_If_Statement (Loc,
4294 Condition =>
4295 Make_Op_Ne (Loc,
4296 Left_Opnd => New_Copy (From),
4297 Right_Opnd => New_Node (N_Null, Loc)),
4298 Then_Statements =>
4299 New_List (
4300 Make_Procedure_Call_Statement (Loc,
4301 Name => New_Reference_To (RTE (RE_Move_Final_List), Loc),
4302 Parameter_Associations => New_List (From, To))));
4303 end Move_Final_List;
4304
4305 -- Start of processing for Expand_N_Extended_Return_Statement
4306
4307 begin
4308 if Nkind (Return_Object_Decl) = N_Object_Declaration then
4309 Exp := Expression (Return_Object_Decl);
4310 else
4311 Exp := Empty;
4312 end if;
4313
4314 Handled_Stm_Seq := Handled_Statement_Sequence (N);
4315
4316 -- Build a simple_return_statement that returns the return object when
4317 -- there is a statement sequence, or no expression, or the result will
4318 -- be built in place. Note however that we currently do this for all
4319 -- composite cases, even though nonlimited composite results are not yet
4320 -- built in place (though we plan to do so eventually).
4321
4322 if Present (Handled_Stm_Seq)
4323 or else Is_Composite_Type (Etype (Parent_Function))
4324 or else No (Exp)
4325 then
4326 if No (Handled_Stm_Seq) then
4327 Statements := New_List;
4328
4329 -- If the extended return has a handled statement sequence, then wrap
4330 -- it in a block and use the block as the first statement.
4331
4332 else
4333 Statements :=
4334 New_List (Make_Block_Statement (Loc,
4335 Declarations => New_List,
4336 Handled_Statement_Sequence => Handled_Stm_Seq));
4337 end if;
4338
4339 -- If control gets past the above Statements, we have successfully
4340 -- completed the return statement. If the result type has controlled
4341 -- parts and the return is for a build-in-place function, then we
4342 -- call Move_Final_List to transfer responsibility for finalization
4343 -- of the return object to the caller. An alternative would be to
4344 -- declare a Success flag in the function, initialize it to False,
4345 -- and set it to True here. Then move the Move_Final_List call into
4346 -- the cleanup code, and check Success. If Success then make a call
4347 -- to Move_Final_List else do finalization. Then we can remove the
4348 -- abort-deferral and the nulling-out of the From parameter from
4349 -- Move_Final_List. Note that the current method is not quite correct
4350 -- in the rather obscure case of a select-then-abort statement whose
4351 -- abortable part contains the return statement.
4352
4353 -- Check the type of the function to determine whether to move the
4354 -- finalization list. A special case arises when processing a simple
4355 -- return statement which has been rewritten as an extended return.
4356 -- In that case check the type of the returned object or the original
4357 -- expression.
4358
4359 if Is_Build_In_Place
4360 and then
4361 (Has_Controlled_Parts (Parent_Function_Typ)
4362 or else (Is_Class_Wide_Type (Parent_Function_Typ)
4363 and then
4364 Has_Controlled_Parts (Root_Type (Parent_Function_Typ)))
4365 or else Has_Controlled_Parts (Etype (Return_Object_Entity))
4366 or else (Present (Exp)
4367 and then Has_Controlled_Parts (Etype (Exp))))
4368 then
4369 Append_To (Statements, Move_Final_List);
4370 end if;
4371
4372 -- Similarly to the above Move_Final_List, if the result type
4373 -- contains tasks, we call Move_Activation_Chain. Later, the cleanup
4374 -- code will call Complete_Master, which will terminate any
4375 -- unactivated tasks belonging to the return statement master. But
4376 -- Move_Activation_Chain updates their master to be that of the
4377 -- caller, so they will not be terminated unless the return statement
4378 -- completes unsuccessfully due to exception, abort, goto, or exit.
4379 -- As a formality, we test whether the function requires the result
4380 -- to be built in place, though that's necessarily true for the case
4381 -- of result types with task parts.
4382
4383 if Is_Build_In_Place and Has_Task (Etype (Parent_Function)) then
4384 Append_To (Statements, Move_Activation_Chain);
4385 end if;
4386
4387 -- Build a simple_return_statement that returns the return object
4388
4389 Return_Stm :=
4390 Make_Simple_Return_Statement (Loc,
4391 Expression => New_Occurrence_Of (Return_Object_Entity, Loc));
4392 Append_To (Statements, Return_Stm);
4393
4394 Handled_Stm_Seq :=
4395 Make_Handled_Sequence_Of_Statements (Loc, Statements);
4396 end if;
4397
4398 -- Case where we build a block
4399
4400 if Present (Handled_Stm_Seq) then
4401 Result :=
4402 Make_Block_Statement (Loc,
4403 Declarations => Return_Object_Declarations (N),
4404 Handled_Statement_Sequence => Handled_Stm_Seq);
4405
4406 -- We set the entity of the new block statement to be that of the
4407 -- return statement. This is necessary so that various fields, such
4408 -- as Finalization_Chain_Entity carry over from the return statement
4409 -- to the block. Note that this block is unusual, in that its entity
4410 -- is an E_Return_Statement rather than an E_Block.
4411
4412 Set_Identifier
4413 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4414
4415 -- If the object decl was already rewritten as a renaming, then
4416 -- we don't want to do the object allocation and transformation of
4417 -- of the return object declaration to a renaming. This case occurs
4418 -- when the return object is initialized by a call to another
4419 -- build-in-place function, and that function is responsible for the
4420 -- allocation of the return object.
4421
4422 if Is_Build_In_Place
4423 and then
4424 Nkind (Return_Object_Decl) = N_Object_Renaming_Declaration
4425 then
4426 pragma Assert (Nkind (Original_Node (Return_Object_Decl)) =
4427 N_Object_Declaration
4428 and then Is_Build_In_Place_Function_Call
4429 (Expression (Original_Node (Return_Object_Decl))));
4430
4431 Set_By_Ref (Return_Stm); -- Return build-in-place results by ref
4432
4433 elsif Is_Build_In_Place then
4434
4435 -- Locate the implicit access parameter associated with the
4436 -- caller-supplied return object and convert the return
4437 -- statement's return object declaration to a renaming of a
4438 -- dereference of the access parameter. If the return object's
4439 -- declaration includes an expression that has not already been
4440 -- expanded as separate assignments, then add an assignment
4441 -- statement to ensure the return object gets initialized.
4442
4443 -- declare
4444 -- Result : T [:= <expression>];
4445 -- begin
4446 -- ...
4447
4448 -- is converted to
4449
4450 -- declare
4451 -- Result : T renames FuncRA.all;
4452 -- [Result := <expression;]
4453 -- begin
4454 -- ...
4455
4456 declare
4457 Return_Obj_Id : constant Entity_Id :=
4458 Defining_Identifier (Return_Object_Decl);
4459 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
4460 Return_Obj_Expr : constant Node_Id :=
4461 Expression (Return_Object_Decl);
4462 Result_Subt : constant Entity_Id :=
4463 Etype (Parent_Function);
4464 Constr_Result : constant Boolean :=
4465 Is_Constrained (Result_Subt);
4466 Obj_Alloc_Formal : Entity_Id;
4467 Object_Access : Entity_Id;
4468 Obj_Acc_Deref : Node_Id;
4469 Init_Assignment : Node_Id := Empty;
4470
4471 begin
4472 -- Build-in-place results must be returned by reference
4473
4474 Set_By_Ref (Return_Stm);
4475
4476 -- Retrieve the implicit access parameter passed by the caller
4477
4478 Object_Access :=
4479 Build_In_Place_Formal (Parent_Function, BIP_Object_Access);
4480
4481 -- If the return object's declaration includes an expression
4482 -- and the declaration isn't marked as No_Initialization, then
4483 -- we need to generate an assignment to the object and insert
4484 -- it after the declaration before rewriting it as a renaming
4485 -- (otherwise we'll lose the initialization). The case where
4486 -- the result type is an interface (or class-wide interface)
4487 -- is also excluded because the context of the function call
4488 -- must be unconstrained, so the initialization will always
4489 -- be done as part of an allocator evaluation (storage pool
4490 -- or secondary stack), never to a constrained target object
4491 -- passed in by the caller. Besides the assignment being
4492 -- unneeded in this case, it avoids problems with trying to
4493 -- generate a dispatching assignment when the return expression
4494 -- is a nonlimited descendant of a limited interface (the
4495 -- interface has no assignment operation).
4496
4497 if Present (Return_Obj_Expr)
4498 and then not No_Initialization (Return_Object_Decl)
4499 and then not Is_Interface (Return_Obj_Typ)
4500 then
4501 Init_Assignment :=
4502 Make_Assignment_Statement (Loc,
4503 Name => New_Reference_To (Return_Obj_Id, Loc),
4504 Expression => Relocate_Node (Return_Obj_Expr));
4505 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
4506 Set_Assignment_OK (Name (Init_Assignment));
4507 Set_No_Ctrl_Actions (Init_Assignment);
4508
4509 Set_Parent (Name (Init_Assignment), Init_Assignment);
4510 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4511
4512 Set_Expression (Return_Object_Decl, Empty);
4513
4514 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
4515 and then not Is_Class_Wide_Type
4516 (Etype (Expression (Init_Assignment)))
4517 then
4518 Rewrite (Expression (Init_Assignment),
4519 Make_Type_Conversion (Loc,
4520 Subtype_Mark =>
4521 New_Occurrence_Of
4522 (Etype (Return_Obj_Id), Loc),
4523 Expression =>
4524 Relocate_Node (Expression (Init_Assignment))));
4525 end if;
4526
4527 -- In the case of functions where the calling context can
4528 -- determine the form of allocation needed, initialization
4529 -- is done with each part of the if statement that handles
4530 -- the different forms of allocation (this is true for
4531 -- unconstrained and tagged result subtypes).
4532
4533 if Constr_Result
4534 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
4535 then
4536 Insert_After (Return_Object_Decl, Init_Assignment);
4537 end if;
4538 end if;
4539
4540 -- When the function's subtype is unconstrained, a run-time
4541 -- test is needed to determine the form of allocation to use
4542 -- for the return object. The function has an implicit formal
4543 -- parameter indicating this. If the BIP_Alloc_Form formal has
4544 -- the value one, then the caller has passed access to an
4545 -- existing object for use as the return object. If the value
4546 -- is two, then the return object must be allocated on the
4547 -- secondary stack. Otherwise, the object must be allocated in
4548 -- a storage pool (currently only supported for the global
4549 -- heap, user-defined storage pools TBD ???). We generate an
4550 -- if statement to test the implicit allocation formal and
4551 -- initialize a local access value appropriately, creating
4552 -- allocators in the secondary stack and global heap cases.
4553 -- The special formal also exists and must be tested when the
4554 -- function has a tagged result, even when the result subtype
4555 -- is constrained, because in general such functions can be
4556 -- called in dispatching contexts and must be handled similarly
4557 -- to functions with a class-wide result.
4558
4559 if not Constr_Result
4560 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
4561 then
4562 Obj_Alloc_Formal :=
4563 Build_In_Place_Formal (Parent_Function, BIP_Alloc_Form);
4564
4565 declare
4566 Ref_Type : Entity_Id;
4567 Ptr_Type_Decl : Node_Id;
4568 Alloc_Obj_Id : Entity_Id;
4569 Alloc_Obj_Decl : Node_Id;
4570 Alloc_If_Stmt : Node_Id;
4571 SS_Allocator : Node_Id;
4572 Heap_Allocator : Node_Id;
4573
4574 begin
4575 -- Reuse the itype created for the function's implicit
4576 -- access formal. This avoids the need to create a new
4577 -- access type here, plus it allows assigning the access
4578 -- formal directly without applying a conversion.
4579
4580 -- Ref_Type := Etype (Object_Access);
4581
4582 -- Create an access type designating the function's
4583 -- result subtype.
4584
4585 Ref_Type := Make_Temporary (Loc, 'A');
4586
4587 Ptr_Type_Decl :=
4588 Make_Full_Type_Declaration (Loc,
4589 Defining_Identifier => Ref_Type,
4590 Type_Definition =>
4591 Make_Access_To_Object_Definition (Loc,
4592 All_Present => True,
4593 Subtype_Indication =>
4594 New_Reference_To (Return_Obj_Typ, Loc)));
4595
4596 Insert_Before (Return_Object_Decl, Ptr_Type_Decl);
4597
4598 -- Create an access object that will be initialized to an
4599 -- access value denoting the return object, either coming
4600 -- from an implicit access value passed in by the caller
4601 -- or from the result of an allocator.
4602
4603 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4604 Set_Etype (Alloc_Obj_Id, Ref_Type);
4605
4606 Alloc_Obj_Decl :=
4607 Make_Object_Declaration (Loc,
4608 Defining_Identifier => Alloc_Obj_Id,
4609 Object_Definition => New_Reference_To
4610 (Ref_Type, Loc));
4611
4612 Insert_Before (Return_Object_Decl, Alloc_Obj_Decl);
4613
4614 -- Create allocators for both the secondary stack and
4615 -- global heap. If there's an initialization expression,
4616 -- then create these as initialized allocators.
4617
4618 if Present (Return_Obj_Expr)
4619 and then not No_Initialization (Return_Object_Decl)
4620 then
4621 -- Always use the type of the expression for the
4622 -- qualified expression, rather than the result type.
4623 -- In general we cannot always use the result type
4624 -- for the allocator, because the expression might be
4625 -- of a specific type, such as in the case of an
4626 -- aggregate or even a nonlimited object when the
4627 -- result type is a limited class-wide interface type.
4628
4629 Heap_Allocator :=
4630 Make_Allocator (Loc,
4631 Expression =>
4632 Make_Qualified_Expression (Loc,
4633 Subtype_Mark =>
4634 New_Reference_To
4635 (Etype (Return_Obj_Expr), Loc),
4636 Expression =>
4637 New_Copy_Tree (Return_Obj_Expr)));
4638
4639 else
4640 -- If the function returns a class-wide type we cannot
4641 -- use the return type for the allocator. Instead we
4642 -- use the type of the expression, which must be an
4643 -- aggregate of a definite type.
4644
4645 if Is_Class_Wide_Type (Return_Obj_Typ) then
4646 Heap_Allocator :=
4647 Make_Allocator (Loc,
4648 Expression =>
4649 New_Reference_To
4650 (Etype (Return_Obj_Expr), Loc));
4651 else
4652 Heap_Allocator :=
4653 Make_Allocator (Loc,
4654 Expression =>
4655 New_Reference_To (Return_Obj_Typ, Loc));
4656 end if;
4657
4658 -- If the object requires default initialization then
4659 -- that will happen later following the elaboration of
4660 -- the object renaming. If we don't turn it off here
4661 -- then the object will be default initialized twice.
4662
4663 Set_No_Initialization (Heap_Allocator);
4664 end if;
4665
4666 -- If the No_Allocators restriction is active, then only
4667 -- an allocator for secondary stack allocation is needed.
4668 -- It's OK for such allocators to have Comes_From_Source
4669 -- set to False, because gigi knows not to flag them as
4670 -- being a violation of No_Implicit_Heap_Allocations.
4671
4672 if Restriction_Active (No_Allocators) then
4673 SS_Allocator := Heap_Allocator;
4674 Heap_Allocator := Make_Null (Loc);
4675
4676 -- Otherwise the heap allocator may be needed, so we make
4677 -- another allocator for secondary stack allocation.
4678
4679 else
4680 SS_Allocator := New_Copy_Tree (Heap_Allocator);
4681
4682 -- The heap allocator is marked Comes_From_Source
4683 -- since it corresponds to an explicit user-written
4684 -- allocator (that is, it will only be executed on
4685 -- behalf of callers that call the function as
4686 -- initialization for such an allocator). This
4687 -- prevents errors when No_Implicit_Heap_Allocations
4688 -- is in force.
4689
4690 Set_Comes_From_Source (Heap_Allocator, True);
4691 end if;
4692
4693 -- The allocator is returned on the secondary stack. We
4694 -- don't do this on VM targets, since the SS is not used.
4695
4696 if VM_Target = No_VM then
4697 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
4698 Set_Procedure_To_Call
4699 (SS_Allocator, RTE (RE_SS_Allocate));
4700
4701 -- The allocator is returned on the secondary stack,
4702 -- so indicate that the function return, as well as
4703 -- the block that encloses the allocator, must not
4704 -- release it. The flags must be set now because the
4705 -- decision to use the secondary stack is done very
4706 -- late in the course of expanding the return
4707 -- statement, past the point where these flags are
4708 -- normally set.
4709
4710 Set_Sec_Stack_Needed_For_Return (Parent_Function);
4711 Set_Sec_Stack_Needed_For_Return
4712 (Return_Statement_Entity (N));
4713 Set_Uses_Sec_Stack (Parent_Function);
4714 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
4715 end if;
4716
4717 -- Create an if statement to test the BIP_Alloc_Form
4718 -- formal and initialize the access object to either the
4719 -- BIP_Object_Access formal (BIP_Alloc_Form = 0), the
4720 -- result of allocating the object in the secondary stack
4721 -- (BIP_Alloc_Form = 1), or else an allocator to create
4722 -- the return object in the heap (BIP_Alloc_Form = 2).
4723
4724 -- ??? An unchecked type conversion must be made in the
4725 -- case of assigning the access object formal to the
4726 -- local access object, because a normal conversion would
4727 -- be illegal in some cases (such as converting access-
4728 -- to-unconstrained to access-to-constrained), but the
4729 -- the unchecked conversion will presumably fail to work
4730 -- right in just such cases. It's not clear at all how to
4731 -- handle this. ???
4732
4733 Alloc_If_Stmt :=
4734 Make_If_Statement (Loc,
4735 Condition =>
4736 Make_Op_Eq (Loc,
4737 Left_Opnd =>
4738 New_Reference_To (Obj_Alloc_Formal, Loc),
4739 Right_Opnd =>
4740 Make_Integer_Literal (Loc,
4741 UI_From_Int (BIP_Allocation_Form'Pos
4742 (Caller_Allocation)))),
4743 Then_Statements =>
4744 New_List (Make_Assignment_Statement (Loc,
4745 Name =>
4746 New_Reference_To
4747 (Alloc_Obj_Id, Loc),
4748 Expression =>
4749 Make_Unchecked_Type_Conversion (Loc,
4750 Subtype_Mark =>
4751 New_Reference_To (Ref_Type, Loc),
4752 Expression =>
4753 New_Reference_To
4754 (Object_Access, Loc)))),
4755 Elsif_Parts =>
4756 New_List (Make_Elsif_Part (Loc,
4757 Condition =>
4758 Make_Op_Eq (Loc,
4759 Left_Opnd =>
4760 New_Reference_To
4761 (Obj_Alloc_Formal, Loc),
4762 Right_Opnd =>
4763 Make_Integer_Literal (Loc,
4764 UI_From_Int (
4765 BIP_Allocation_Form'Pos
4766 (Secondary_Stack)))),
4767 Then_Statements =>
4768 New_List
4769 (Make_Assignment_Statement (Loc,
4770 Name =>
4771 New_Reference_To
4772 (Alloc_Obj_Id, Loc),
4773 Expression =>
4774 SS_Allocator)))),
4775 Else_Statements =>
4776 New_List (Make_Assignment_Statement (Loc,
4777 Name =>
4778 New_Reference_To
4779 (Alloc_Obj_Id, Loc),
4780 Expression =>
4781 Heap_Allocator)));
4782
4783 -- If a separate initialization assignment was created
4784 -- earlier, append that following the assignment of the
4785 -- implicit access formal to the access object, to ensure
4786 -- that the return object is initialized in that case.
4787 -- In this situation, the target of the assignment must
4788 -- be rewritten to denote a dereference of the access to
4789 -- the return object passed in by the caller.
4790
4791 if Present (Init_Assignment) then
4792 Rewrite (Name (Init_Assignment),
4793 Make_Explicit_Dereference (Loc,
4794 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
4795 Set_Etype
4796 (Name (Init_Assignment), Etype (Return_Obj_Id));
4797
4798 Append_To
4799 (Then_Statements (Alloc_If_Stmt),
4800 Init_Assignment);
4801 end if;
4802
4803 Insert_Before (Return_Object_Decl, Alloc_If_Stmt);
4804
4805 -- Remember the local access object for use in the
4806 -- dereference of the renaming created below.
4807
4808 Object_Access := Alloc_Obj_Id;
4809 end;
4810 end if;
4811
4812 -- Replace the return object declaration with a renaming of a
4813 -- dereference of the access value designating the return
4814 -- object.
4815
4816 Obj_Acc_Deref :=
4817 Make_Explicit_Dereference (Loc,
4818 Prefix => New_Reference_To (Object_Access, Loc));
4819
4820 Rewrite (Return_Object_Decl,
4821 Make_Object_Renaming_Declaration (Loc,
4822 Defining_Identifier => Return_Obj_Id,
4823 Access_Definition => Empty,
4824 Subtype_Mark => New_Occurrence_Of
4825 (Return_Obj_Typ, Loc),
4826 Name => Obj_Acc_Deref));
4827
4828 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
4829 end;
4830 end if;
4831
4832 -- Case where we do not build a block
4833
4834 else
4835 -- We're about to drop Return_Object_Declarations on the floor, so
4836 -- we need to insert it, in case it got expanded into useful code.
4837 -- Remove side effects from expression, which may be duplicated in
4838 -- subsequent checks (see Expand_Simple_Function_Return).
4839
4840 Insert_List_Before (N, Return_Object_Declarations (N));
4841 Remove_Side_Effects (Exp);
4842
4843 -- Build simple_return_statement that returns the expression directly
4844
4845 Return_Stm := Make_Simple_Return_Statement (Loc, Expression => Exp);
4846
4847 Result := Return_Stm;
4848 end if;
4849
4850 -- Set the flag to prevent infinite recursion
4851
4852 Set_Comes_From_Extended_Return_Statement (Return_Stm);
4853
4854 Rewrite (N, Result);
4855 Analyze (N);
4856 end Expand_N_Extended_Return_Statement;
4857
4858 ----------------------------
4859 -- Expand_N_Function_Call --
4860 ----------------------------
4861
4862 procedure Expand_N_Function_Call (N : Node_Id) is
4863 begin
4864 Expand_Call (N);
4865
4866 -- If the return value of a foreign compiled function is VAX Float, then
4867 -- expand the return (adjusts the location of the return value on
4868 -- Alpha/VMS, no-op everywhere else).
4869 -- Comes_From_Source intercepts recursive expansion.
4870
4871 if Vax_Float (Etype (N))
4872 and then Nkind (N) = N_Function_Call
4873 and then Present (Name (N))
4874 and then Present (Entity (Name (N)))
4875 and then Has_Foreign_Convention (Entity (Name (N)))
4876 and then Comes_From_Source (Parent (N))
4877 then
4878 Expand_Vax_Foreign_Return (N);
4879 end if;
4880 end Expand_N_Function_Call;
4881
4882 ---------------------------------------
4883 -- Expand_N_Procedure_Call_Statement --
4884 ---------------------------------------
4885
4886 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4887 begin
4888 Expand_Call (N);
4889 end Expand_N_Procedure_Call_Statement;
4890
4891 --------------------------------------
4892 -- Expand_N_Simple_Return_Statement --
4893 --------------------------------------
4894
4895 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
4896 begin
4897 -- Defend against previous errors (i.e. the return statement calls a
4898 -- function that is not available in configurable runtime).
4899
4900 if Present (Expression (N))
4901 and then Nkind (Expression (N)) = N_Empty
4902 then
4903 return;
4904 end if;
4905
4906 -- Distinguish the function and non-function cases:
4907
4908 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
4909
4910 when E_Function |
4911 E_Generic_Function =>
4912 Expand_Simple_Function_Return (N);
4913
4914 when E_Procedure |
4915 E_Generic_Procedure |
4916 E_Entry |
4917 E_Entry_Family |
4918 E_Return_Statement =>
4919 Expand_Non_Function_Return (N);
4920
4921 when others =>
4922 raise Program_Error;
4923 end case;
4924
4925 exception
4926 when RE_Not_Available =>
4927 return;
4928 end Expand_N_Simple_Return_Statement;
4929
4930 ------------------------------
4931 -- Expand_N_Subprogram_Body --
4932 ------------------------------
4933
4934 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4935 -- by the back-end.
4936
4937 -- Add dummy push/pop label nodes at start and end to clear any local
4938 -- exception indications if local-exception-to-goto optimization is active.
4939
4940 -- Add return statement if last statement in body is not a return statement
4941 -- (this makes things easier on Gigi which does not want to have to handle
4942 -- a missing return).
4943
4944 -- Add call to Activate_Tasks if body is a task activator
4945
4946 -- Deal with possible detection of infinite recursion
4947
4948 -- Eliminate body completely if convention stubbed
4949
4950 -- Encode entity names within body, since we will not need to reference
4951 -- these entities any longer in the front end.
4952
4953 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4954
4955 -- Reset Pure indication if any parameter has root type System.Address
4956 -- or has any parameters of limited types, where limited means that the
4957 -- run-time view is limited (i.e. the full type is limited).
4958
4959 -- Wrap thread body
4960
4961 procedure Expand_N_Subprogram_Body (N : Node_Id) is
4962 Loc : constant Source_Ptr := Sloc (N);
4963 H : constant Node_Id := Handled_Statement_Sequence (N);
4964 Body_Id : Entity_Id;
4965 Except_H : Node_Id;
4966 L : List_Id;
4967 Spec_Id : Entity_Id;
4968
4969 procedure Add_Return (S : List_Id);
4970 -- Append a return statement to the statement sequence S if the last
4971 -- statement is not already a return or a goto statement. Note that
4972 -- the latter test is not critical, it does not matter if we add a few
4973 -- extra returns, since they get eliminated anyway later on.
4974
4975 ----------------
4976 -- Add_Return --
4977 ----------------
4978
4979 procedure Add_Return (S : List_Id) is
4980 Last_Stm : Node_Id;
4981 Loc : Source_Ptr;
4982
4983 begin
4984 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4985 -- not relevant in this context since they are not executable.
4986
4987 Last_Stm := Last (S);
4988 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
4989 Prev (Last_Stm);
4990 end loop;
4991
4992 -- Now insert return unless last statement is a transfer
4993
4994 if not Is_Transfer (Last_Stm) then
4995
4996 -- The source location for the return is the end label of the
4997 -- procedure if present. Otherwise use the sloc of the last
4998 -- statement in the list. If the list comes from a generated
4999 -- exception handler and we are not debugging generated code,
5000 -- all the statements within the handler are made invisible
5001 -- to the debugger.
5002
5003 if Nkind (Parent (S)) = N_Exception_Handler
5004 and then not Comes_From_Source (Parent (S))
5005 then
5006 Loc := Sloc (Last_Stm);
5007
5008 elsif Present (End_Label (H)) then
5009 Loc := Sloc (End_Label (H));
5010
5011 else
5012 Loc := Sloc (Last_Stm);
5013 end if;
5014
5015 declare
5016 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
5017
5018 begin
5019 -- Append return statement, and set analyzed manually. We can't
5020 -- call Analyze on this return since the scope is wrong.
5021
5022 -- Note: it almost works to push the scope and then do the
5023 -- Analyze call, but something goes wrong in some weird cases
5024 -- and it is not worth worrying about ???
5025
5026 Append_To (S, Rtn);
5027 Set_Analyzed (Rtn);
5028
5029 -- Call _Postconditions procedure if appropriate. We need to
5030 -- do this explicitly because we did not analyze the generated
5031 -- return statement above, so the call did not get inserted.
5032
5033 if Ekind (Spec_Id) = E_Procedure
5034 and then Has_Postconditions (Spec_Id)
5035 then
5036 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
5037 Insert_Action (Rtn,
5038 Make_Procedure_Call_Statement (Loc,
5039 Name =>
5040 New_Reference_To (Postcondition_Proc (Spec_Id), Loc)));
5041 end if;
5042 end;
5043 end if;
5044 end Add_Return;
5045
5046 -- Start of processing for Expand_N_Subprogram_Body
5047
5048 begin
5049 -- Set L to either the list of declarations if present, or to the list
5050 -- of statements if no declarations are present. This is used to insert
5051 -- new stuff at the start.
5052
5053 if Is_Non_Empty_List (Declarations (N)) then
5054 L := Declarations (N);
5055 else
5056 L := Statements (H);
5057 end if;
5058
5059 -- If local-exception-to-goto optimization active, insert dummy push
5060 -- statements at start, and dummy pop statements at end.
5061
5062 if (Debug_Flag_Dot_G
5063 or else Restriction_Active (No_Exception_Propagation))
5064 and then Is_Non_Empty_List (L)
5065 then
5066 declare
5067 FS : constant Node_Id := First (L);
5068 FL : constant Source_Ptr := Sloc (FS);
5069 LS : Node_Id;
5070 LL : Source_Ptr;
5071
5072 begin
5073 -- LS points to either last statement, if statements are present
5074 -- or to the last declaration if there are no statements present.
5075 -- It is the node after which the pop's are generated.
5076
5077 if Is_Non_Empty_List (Statements (H)) then
5078 LS := Last (Statements (H));
5079 else
5080 LS := Last (L);
5081 end if;
5082
5083 LL := Sloc (LS);
5084
5085 Insert_List_Before_And_Analyze (FS, New_List (
5086 Make_Push_Constraint_Error_Label (FL),
5087 Make_Push_Program_Error_Label (FL),
5088 Make_Push_Storage_Error_Label (FL)));
5089
5090 Insert_List_After_And_Analyze (LS, New_List (
5091 Make_Pop_Constraint_Error_Label (LL),
5092 Make_Pop_Program_Error_Label (LL),
5093 Make_Pop_Storage_Error_Label (LL)));
5094 end;
5095 end if;
5096
5097 -- Find entity for subprogram
5098
5099 Body_Id := Defining_Entity (N);
5100
5101 if Present (Corresponding_Spec (N)) then
5102 Spec_Id := Corresponding_Spec (N);
5103 else
5104 Spec_Id := Body_Id;
5105 end if;
5106
5107 -- Need poll on entry to subprogram if polling enabled. We only do this
5108 -- for non-empty subprograms, since it does not seem necessary to poll
5109 -- for a dummy null subprogram.
5110
5111 if Is_Non_Empty_List (L) then
5112
5113 -- Do not add a polling call if the subprogram is to be inlined by
5114 -- the back-end, to avoid repeated calls with multiple inlinings.
5115
5116 if Is_Inlined (Spec_Id)
5117 and then Front_End_Inlining
5118 and then Optimization_Level > 1
5119 then
5120 null;
5121 else
5122 Generate_Poll_Call (First (L));
5123 end if;
5124 end if;
5125
5126 -- If this is a Pure function which has any parameters whose root type
5127 -- is System.Address, reset the Pure indication, since it will likely
5128 -- cause incorrect code to be generated as the parameter is probably
5129 -- a pointer, and the fact that the same pointer is passed does not mean
5130 -- that the same value is being referenced.
5131
5132 -- Note that if the programmer gave an explicit Pure_Function pragma,
5133 -- then we believe the programmer, and leave the subprogram Pure.
5134
5135 -- This code should probably be at the freeze point, so that it happens
5136 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5137 -- semantic tree has Is_Pure set properly ???
5138
5139 if Is_Pure (Spec_Id)
5140 and then Is_Subprogram (Spec_Id)
5141 and then not Has_Pragma_Pure_Function (Spec_Id)
5142 then
5143 declare
5144 F : Entity_Id;
5145
5146 begin
5147 F := First_Formal (Spec_Id);
5148 while Present (F) loop
5149 if Is_Descendent_Of_Address (Etype (F))
5150
5151 -- Note that this test is being made in the body of the
5152 -- subprogram, not the spec, so we are testing the full
5153 -- type for being limited here, as required.
5154
5155 or else Is_Limited_Type (Etype (F))
5156 then
5157 Set_Is_Pure (Spec_Id, False);
5158
5159 if Spec_Id /= Body_Id then
5160 Set_Is_Pure (Body_Id, False);
5161 end if;
5162
5163 exit;
5164 end if;
5165
5166 Next_Formal (F);
5167 end loop;
5168 end;
5169 end if;
5170
5171 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5172
5173 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5174 declare
5175 F : Entity_Id;
5176
5177 begin
5178 -- Loop through formals
5179
5180 F := First_Formal (Spec_Id);
5181 while Present (F) loop
5182 if Is_Scalar_Type (Etype (F))
5183 and then Ekind (F) = E_Out_Parameter
5184 then
5185 Check_Restriction (No_Default_Initialization, F);
5186
5187 -- Insert the initialization. We turn off validity checks
5188 -- for this assignment, since we do not want any check on
5189 -- the initial value itself (which may well be invalid).
5190
5191 Insert_Before_And_Analyze (First (L),
5192 Make_Assignment_Statement (Loc,
5193 Name => New_Occurrence_Of (F, Loc),
5194 Expression => Get_Simple_Init_Val (Etype (F), N)),
5195 Suppress => Validity_Check);
5196 end if;
5197
5198 Next_Formal (F);
5199 end loop;
5200 end;
5201 end if;
5202
5203 -- Clear out statement list for stubbed procedure
5204
5205 if Present (Corresponding_Spec (N)) then
5206 Set_Elaboration_Flag (N, Spec_Id);
5207
5208 if Convention (Spec_Id) = Convention_Stubbed
5209 or else Is_Eliminated (Spec_Id)
5210 then
5211 Set_Declarations (N, Empty_List);
5212 Set_Handled_Statement_Sequence (N,
5213 Make_Handled_Sequence_Of_Statements (Loc,
5214 Statements => New_List (
5215 Make_Null_Statement (Loc))));
5216 return;
5217 end if;
5218 end if;
5219
5220 -- Create a set of discriminals for the next protected subprogram body
5221
5222 if Is_List_Member (N)
5223 and then Present (Parent (List_Containing (N)))
5224 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5225 and then Present (Next_Protected_Operation (N))
5226 then
5227 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5228 end if;
5229
5230 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5231 -- subprograms with no specs are not frozen.
5232
5233 declare
5234 Typ : constant Entity_Id := Etype (Spec_Id);
5235 Utyp : constant Entity_Id := Underlying_Type (Typ);
5236
5237 begin
5238 if not Acts_As_Spec (N)
5239 and then Nkind (Parent (Parent (Spec_Id))) /=
5240 N_Subprogram_Body_Stub
5241 then
5242 null;
5243
5244 elsif Is_Immutably_Limited_Type (Typ) then
5245 Set_Returns_By_Ref (Spec_Id);
5246
5247 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5248 Set_Returns_By_Ref (Spec_Id);
5249 end if;
5250 end;
5251
5252 -- For a procedure, we add a return for all possible syntactic ends of
5253 -- the subprogram.
5254
5255 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5256 Add_Return (Statements (H));
5257
5258 if Present (Exception_Handlers (H)) then
5259 Except_H := First_Non_Pragma (Exception_Handlers (H));
5260 while Present (Except_H) loop
5261 Add_Return (Statements (Except_H));
5262 Next_Non_Pragma (Except_H);
5263 end loop;
5264 end if;
5265
5266 -- For a function, we must deal with the case where there is at least
5267 -- one missing return. What we do is to wrap the entire body of the
5268 -- function in a block:
5269
5270 -- begin
5271 -- ...
5272 -- end;
5273
5274 -- becomes
5275
5276 -- begin
5277 -- begin
5278 -- ...
5279 -- end;
5280
5281 -- raise Program_Error;
5282 -- end;
5283
5284 -- This approach is necessary because the raise must be signalled to the
5285 -- caller, not handled by any local handler (RM 6.4(11)).
5286
5287 -- Note: we do not need to analyze the constructed sequence here, since
5288 -- it has no handler, and an attempt to analyze the handled statement
5289 -- sequence twice is risky in various ways (e.g. the issue of expanding
5290 -- cleanup actions twice).
5291
5292 elsif Has_Missing_Return (Spec_Id) then
5293 declare
5294 Hloc : constant Source_Ptr := Sloc (H);
5295 Blok : constant Node_Id :=
5296 Make_Block_Statement (Hloc,
5297 Handled_Statement_Sequence => H);
5298 Rais : constant Node_Id :=
5299 Make_Raise_Program_Error (Hloc,
5300 Reason => PE_Missing_Return);
5301
5302 begin
5303 Set_Handled_Statement_Sequence (N,
5304 Make_Handled_Sequence_Of_Statements (Hloc,
5305 Statements => New_List (Blok, Rais)));
5306
5307 Push_Scope (Spec_Id);
5308 Analyze (Blok);
5309 Analyze (Rais);
5310 Pop_Scope;
5311 end;
5312 end if;
5313
5314 -- If subprogram contains a parameterless recursive call, then we may
5315 -- have an infinite recursion, so see if we can generate code to check
5316 -- for this possibility if storage checks are not suppressed.
5317
5318 if Ekind (Spec_Id) = E_Procedure
5319 and then Has_Recursive_Call (Spec_Id)
5320 and then not Storage_Checks_Suppressed (Spec_Id)
5321 then
5322 Detect_Infinite_Recursion (N, Spec_Id);
5323 end if;
5324
5325 -- Set to encode entity names in package body before gigi is called
5326
5327 Qualify_Entity_Names (N);
5328 end Expand_N_Subprogram_Body;
5329
5330 -----------------------------------
5331 -- Expand_N_Subprogram_Body_Stub --
5332 -----------------------------------
5333
5334 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5335 begin
5336 if Present (Corresponding_Body (N)) then
5337 Expand_N_Subprogram_Body (
5338 Unit_Declaration_Node (Corresponding_Body (N)));
5339 end if;
5340 end Expand_N_Subprogram_Body_Stub;
5341
5342 -------------------------------------
5343 -- Expand_N_Subprogram_Declaration --
5344 -------------------------------------
5345
5346 -- If the declaration appears within a protected body, it is a private
5347 -- operation of the protected type. We must create the corresponding
5348 -- protected subprogram an associated formals. For a normal protected
5349 -- operation, this is done when expanding the protected type declaration.
5350
5351 -- If the declaration is for a null procedure, emit null body
5352
5353 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5354 Loc : constant Source_Ptr := Sloc (N);
5355 Subp : constant Entity_Id := Defining_Entity (N);
5356 Scop : constant Entity_Id := Scope (Subp);
5357 Prot_Decl : Node_Id;
5358 Prot_Bod : Node_Id;
5359 Prot_Id : Entity_Id;
5360
5361 begin
5362 -- Deal with case of protected subprogram. Do not generate protected
5363 -- operation if operation is flagged as eliminated.
5364
5365 if Is_List_Member (N)
5366 and then Present (Parent (List_Containing (N)))
5367 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5368 and then Is_Protected_Type (Scop)
5369 then
5370 if No (Protected_Body_Subprogram (Subp))
5371 and then not Is_Eliminated (Subp)
5372 then
5373 Prot_Decl :=
5374 Make_Subprogram_Declaration (Loc,
5375 Specification =>
5376 Build_Protected_Sub_Specification
5377 (N, Scop, Unprotected_Mode));
5378
5379 -- The protected subprogram is declared outside of the protected
5380 -- body. Given that the body has frozen all entities so far, we
5381 -- analyze the subprogram and perform freezing actions explicitly.
5382 -- including the generation of an explicit freeze node, to ensure
5383 -- that gigi has the proper order of elaboration.
5384 -- If the body is a subunit, the insertion point is before the
5385 -- stub in the parent.
5386
5387 Prot_Bod := Parent (List_Containing (N));
5388
5389 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5390 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5391 end if;
5392
5393 Insert_Before (Prot_Bod, Prot_Decl);
5394 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5395 Set_Has_Delayed_Freeze (Prot_Id);
5396
5397 Push_Scope (Scope (Scop));
5398 Analyze (Prot_Decl);
5399 Insert_Actions (N, Freeze_Entity (Prot_Id, Loc));
5400 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5401
5402 -- Create protected operation as well. Even though the operation
5403 -- is only accessible within the body, it is possible to make it
5404 -- available outside of the protected object by using 'Access to
5405 -- provide a callback, so build protected version in all cases.
5406
5407 Prot_Decl :=
5408 Make_Subprogram_Declaration (Loc,
5409 Specification =>
5410 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5411 Insert_Before (Prot_Bod, Prot_Decl);
5412 Analyze (Prot_Decl);
5413
5414 Pop_Scope;
5415 end if;
5416
5417 -- Ada 2005 (AI-348): Generate body for a null procedure.
5418 -- In most cases this is superfluous because calls to it
5419 -- will be automatically inlined, but we definitely need
5420 -- the body if preconditions for the procedure are present.
5421
5422 elsif Nkind (Specification (N)) = N_Procedure_Specification
5423 and then Null_Present (Specification (N))
5424 then
5425 declare
5426 Bod : constant Node_Id := Body_To_Inline (N);
5427
5428 begin
5429 Set_Has_Completion (Subp, False);
5430 Append_Freeze_Action (Subp, Bod);
5431
5432 -- The body now contains raise statements, so calls to it will
5433 -- not be inlined.
5434
5435 Set_Is_Inlined (Subp, False);
5436 end;
5437 end if;
5438 end Expand_N_Subprogram_Declaration;
5439
5440 --------------------------------
5441 -- Expand_Non_Function_Return --
5442 --------------------------------
5443
5444 procedure Expand_Non_Function_Return (N : Node_Id) is
5445 pragma Assert (No (Expression (N)));
5446
5447 Loc : constant Source_Ptr := Sloc (N);
5448 Scope_Id : Entity_Id :=
5449 Return_Applies_To (Return_Statement_Entity (N));
5450 Kind : constant Entity_Kind := Ekind (Scope_Id);
5451 Call : Node_Id;
5452 Acc_Stat : Node_Id;
5453 Goto_Stat : Node_Id;
5454 Lab_Node : Node_Id;
5455
5456 begin
5457 -- Call _Postconditions procedure if procedure with active
5458 -- postconditions. Here, we use the Postcondition_Proc attribute, which
5459 -- is needed for implicitly-generated returns. Functions never
5460 -- have implicitly-generated returns, and there's no room for
5461 -- Postcondition_Proc in E_Function, so we look up the identifier
5462 -- Name_uPostconditions for function returns (see
5463 -- Expand_Simple_Function_Return).
5464
5465 if Ekind (Scope_Id) = E_Procedure
5466 and then Has_Postconditions (Scope_Id)
5467 then
5468 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
5469 Insert_Action (N,
5470 Make_Procedure_Call_Statement (Loc,
5471 Name => New_Reference_To (Postcondition_Proc (Scope_Id), Loc)));
5472 end if;
5473
5474 -- If it is a return from a procedure do no extra steps
5475
5476 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5477 return;
5478
5479 -- If it is a nested return within an extended one, replace it with a
5480 -- return of the previously declared return object.
5481
5482 elsif Kind = E_Return_Statement then
5483 Rewrite (N,
5484 Make_Simple_Return_Statement (Loc,
5485 Expression =>
5486 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5487 Set_Comes_From_Extended_Return_Statement (N);
5488 Set_Return_Statement_Entity (N, Scope_Id);
5489 Expand_Simple_Function_Return (N);
5490 return;
5491 end if;
5492
5493 pragma Assert (Is_Entry (Scope_Id));
5494
5495 -- Look at the enclosing block to see whether the return is from an
5496 -- accept statement or an entry body.
5497
5498 for J in reverse 0 .. Scope_Stack.Last loop
5499 Scope_Id := Scope_Stack.Table (J).Entity;
5500 exit when Is_Concurrent_Type (Scope_Id);
5501 end loop;
5502
5503 -- If it is a return from accept statement it is expanded as call to
5504 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5505
5506 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5507 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5508
5509 if Is_Task_Type (Scope_Id) then
5510
5511 Call :=
5512 Make_Procedure_Call_Statement (Loc,
5513 Name => New_Reference_To (RTE (RE_Complete_Rendezvous), Loc));
5514 Insert_Before (N, Call);
5515 -- why not insert actions here???
5516 Analyze (Call);
5517
5518 Acc_Stat := Parent (N);
5519 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5520 Acc_Stat := Parent (Acc_Stat);
5521 end loop;
5522
5523 Lab_Node := Last (Statements
5524 (Handled_Statement_Sequence (Acc_Stat)));
5525
5526 Goto_Stat := Make_Goto_Statement (Loc,
5527 Name => New_Occurrence_Of
5528 (Entity (Identifier (Lab_Node)), Loc));
5529
5530 Set_Analyzed (Goto_Stat);
5531
5532 Rewrite (N, Goto_Stat);
5533 Analyze (N);
5534
5535 -- If it is a return from an entry body, put a Complete_Entry_Body call
5536 -- in front of the return.
5537
5538 elsif Is_Protected_Type (Scope_Id) then
5539 Call :=
5540 Make_Procedure_Call_Statement (Loc,
5541 Name =>
5542 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
5543 Parameter_Associations => New_List (
5544 Make_Attribute_Reference (Loc,
5545 Prefix =>
5546 New_Reference_To
5547 (Find_Protection_Object (Current_Scope), Loc),
5548 Attribute_Name =>
5549 Name_Unchecked_Access)));
5550
5551 Insert_Before (N, Call);
5552 Analyze (Call);
5553 end if;
5554 end Expand_Non_Function_Return;
5555
5556 ---------------------------------------
5557 -- Expand_Protected_Object_Reference --
5558 ---------------------------------------
5559
5560 function Expand_Protected_Object_Reference
5561 (N : Node_Id;
5562 Scop : Entity_Id) return Node_Id
5563 is
5564 Loc : constant Source_Ptr := Sloc (N);
5565 Corr : Entity_Id;
5566 Rec : Node_Id;
5567 Param : Entity_Id;
5568 Proc : Entity_Id;
5569
5570 begin
5571 Rec :=
5572 Make_Identifier (Loc,
5573 Chars => Name_uObject);
5574 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5575
5576 -- Find enclosing protected operation, and retrieve its first parameter,
5577 -- which denotes the enclosing protected object. If the enclosing
5578 -- operation is an entry, we are immediately within the protected body,
5579 -- and we can retrieve the object from the service entries procedure. A
5580 -- barrier function has the same signature as an entry. A barrier
5581 -- function is compiled within the protected object, but unlike
5582 -- protected operations its never needs locks, so that its protected
5583 -- body subprogram points to itself.
5584
5585 Proc := Current_Scope;
5586 while Present (Proc)
5587 and then Scope (Proc) /= Scop
5588 loop
5589 Proc := Scope (Proc);
5590 end loop;
5591
5592 Corr := Protected_Body_Subprogram (Proc);
5593
5594 if No (Corr) then
5595
5596 -- Previous error left expansion incomplete.
5597 -- Nothing to do on this call.
5598
5599 return Empty;
5600 end if;
5601
5602 Param :=
5603 Defining_Identifier
5604 (First (Parameter_Specifications (Parent (Corr))));
5605
5606 if Is_Subprogram (Proc)
5607 and then Proc /= Corr
5608 then
5609 -- Protected function or procedure
5610
5611 Set_Entity (Rec, Param);
5612
5613 -- Rec is a reference to an entity which will not be in scope when
5614 -- the call is reanalyzed, and needs no further analysis.
5615
5616 Set_Analyzed (Rec);
5617
5618 else
5619 -- Entry or barrier function for entry body. The first parameter of
5620 -- the entry body procedure is pointer to the object. We create a
5621 -- local variable of the proper type, duplicating what is done to
5622 -- define _object later on.
5623
5624 declare
5625 Decls : List_Id;
5626 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
5627
5628 begin
5629 Decls := New_List (
5630 Make_Full_Type_Declaration (Loc,
5631 Defining_Identifier => Obj_Ptr,
5632 Type_Definition =>
5633 Make_Access_To_Object_Definition (Loc,
5634 Subtype_Indication =>
5635 New_Reference_To
5636 (Corresponding_Record_Type (Scop), Loc))));
5637
5638 Insert_Actions (N, Decls);
5639 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
5640
5641 Rec :=
5642 Make_Explicit_Dereference (Loc,
5643 Unchecked_Convert_To (Obj_Ptr,
5644 New_Occurrence_Of (Param, Loc)));
5645
5646 -- Analyze new actual. Other actuals in calls are already analyzed
5647 -- and the list of actuals is not reanalyzed after rewriting.
5648
5649 Set_Parent (Rec, N);
5650 Analyze (Rec);
5651 end;
5652 end if;
5653
5654 return Rec;
5655 end Expand_Protected_Object_Reference;
5656
5657 --------------------------------------
5658 -- Expand_Protected_Subprogram_Call --
5659 --------------------------------------
5660
5661 procedure Expand_Protected_Subprogram_Call
5662 (N : Node_Id;
5663 Subp : Entity_Id;
5664 Scop : Entity_Id)
5665 is
5666 Rec : Node_Id;
5667
5668 begin
5669 -- If the protected object is not an enclosing scope, this is
5670 -- an inter-object function call. Inter-object procedure
5671 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
5672 -- The call is intra-object only if the subprogram being
5673 -- called is in the protected body being compiled, and if the
5674 -- protected object in the call is statically the enclosing type.
5675 -- The object may be an component of some other data structure,
5676 -- in which case this must be handled as an inter-object call.
5677
5678 if not In_Open_Scopes (Scop)
5679 or else not Is_Entity_Name (Name (N))
5680 then
5681 if Nkind (Name (N)) = N_Selected_Component then
5682 Rec := Prefix (Name (N));
5683
5684 else
5685 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
5686 Rec := Prefix (Prefix (Name (N)));
5687 end if;
5688
5689 Build_Protected_Subprogram_Call (N,
5690 Name => New_Occurrence_Of (Subp, Sloc (N)),
5691 Rec => Convert_Concurrent (Rec, Etype (Rec)),
5692 External => True);
5693
5694 else
5695 Rec := Expand_Protected_Object_Reference (N, Scop);
5696
5697 if No (Rec) then
5698 return;
5699 end if;
5700
5701 Build_Protected_Subprogram_Call (N,
5702 Name => Name (N),
5703 Rec => Rec,
5704 External => False);
5705
5706 end if;
5707
5708 -- If it is a function call it can appear in elaboration code and
5709 -- the called entity must be frozen here.
5710
5711 if Ekind (Subp) = E_Function then
5712 Freeze_Expression (Name (N));
5713 end if;
5714
5715 -- Analyze and resolve the new call. The actuals have already been
5716 -- resolved, but expansion of a function call will add extra actuals
5717 -- if needed. Analysis of a procedure call already includes resolution.
5718
5719 Analyze (N);
5720
5721 if Ekind (Subp) = E_Function then
5722 Resolve (N, Etype (Subp));
5723 end if;
5724 end Expand_Protected_Subprogram_Call;
5725
5726 -----------------------------------
5727 -- Expand_Simple_Function_Return --
5728 -----------------------------------
5729
5730 -- The "simple" comes from the syntax rule simple_return_statement.
5731 -- The semantics are not at all simple!
5732
5733 procedure Expand_Simple_Function_Return (N : Node_Id) is
5734 Loc : constant Source_Ptr := Sloc (N);
5735
5736 Scope_Id : constant Entity_Id :=
5737 Return_Applies_To (Return_Statement_Entity (N));
5738 -- The function we are returning from
5739
5740 R_Type : constant Entity_Id := Etype (Scope_Id);
5741 -- The result type of the function
5742
5743 Utyp : constant Entity_Id := Underlying_Type (R_Type);
5744
5745 Exp : constant Node_Id := Expression (N);
5746 pragma Assert (Present (Exp));
5747
5748 Exptyp : constant Entity_Id := Etype (Exp);
5749 -- The type of the expression (not necessarily the same as R_Type)
5750
5751 Subtype_Ind : Node_Id;
5752 -- If the result type of the function is class-wide and the
5753 -- expression has a specific type, then we use the expression's
5754 -- type as the type of the return object. In cases where the
5755 -- expression is an aggregate that is built in place, this avoids
5756 -- the need for an expensive conversion of the return object to
5757 -- the specific type on assignments to the individual components.
5758
5759 begin
5760 if Is_Class_Wide_Type (R_Type)
5761 and then not Is_Class_Wide_Type (Etype (Exp))
5762 then
5763 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
5764 else
5765 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
5766 end if;
5767
5768 -- For the case of a simple return that does not come from an extended
5769 -- return, in the case of Ada 2005 where we are returning a limited
5770 -- type, we rewrite "return <expression>;" to be:
5771
5772 -- return _anon_ : <return_subtype> := <expression>
5773
5774 -- The expansion produced by Expand_N_Extended_Return_Statement will
5775 -- contain simple return statements (for example, a block containing
5776 -- simple return of the return object), which brings us back here with
5777 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
5778 -- checking for a simple return that does not come from an extended
5779 -- return is to avoid this infinite recursion.
5780
5781 -- The reason for this design is that for Ada 2005 limited returns, we
5782 -- need to reify the return object, so we can build it "in place", and
5783 -- we need a block statement to hang finalization and tasking stuff.
5784
5785 -- ??? In order to avoid disruption, we avoid translating to extended
5786 -- return except in the cases where we really need to (Ada 2005 for
5787 -- inherently limited). We might prefer to do this translation in all
5788 -- cases (except perhaps for the case of Ada 95 inherently limited),
5789 -- in order to fully exercise the Expand_N_Extended_Return_Statement
5790 -- code. This would also allow us to do the build-in-place optimization
5791 -- for efficiency even in cases where it is semantically not required.
5792
5793 -- As before, we check the type of the return expression rather than the
5794 -- return type of the function, because the latter may be a limited
5795 -- class-wide interface type, which is not a limited type, even though
5796 -- the type of the expression may be.
5797
5798 if not Comes_From_Extended_Return_Statement (N)
5799 and then Is_Immutably_Limited_Type (Etype (Expression (N)))
5800 and then Ada_Version >= Ada_2005
5801 and then not Debug_Flag_Dot_L
5802 then
5803 declare
5804 Return_Object_Entity : constant Entity_Id :=
5805 Make_Temporary (Loc, 'R', Exp);
5806 Obj_Decl : constant Node_Id :=
5807 Make_Object_Declaration (Loc,
5808 Defining_Identifier => Return_Object_Entity,
5809 Object_Definition => Subtype_Ind,
5810 Expression => Exp);
5811
5812 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
5813 Return_Object_Declarations => New_List (Obj_Decl));
5814 -- Do not perform this high-level optimization if the result type
5815 -- is an interface because the "this" pointer must be displaced.
5816
5817 begin
5818 Rewrite (N, Ext);
5819 Analyze (N);
5820 return;
5821 end;
5822 end if;
5823
5824 -- Here we have a simple return statement that is part of the expansion
5825 -- of an extended return statement (either written by the user, or
5826 -- generated by the above code).
5827
5828 -- Always normalize C/Fortran boolean result. This is not always needed,
5829 -- but it seems a good idea to minimize the passing around of non-
5830 -- normalized values, and in any case this handles the processing of
5831 -- barrier functions for protected types, which turn the condition into
5832 -- a return statement.
5833
5834 if Is_Boolean_Type (Exptyp)
5835 and then Nonzero_Is_True (Exptyp)
5836 then
5837 Adjust_Condition (Exp);
5838 Adjust_Result_Type (Exp, Exptyp);
5839 end if;
5840
5841 -- Do validity check if enabled for returns
5842
5843 if Validity_Checks_On
5844 and then Validity_Check_Returns
5845 then
5846 Ensure_Valid (Exp);
5847 end if;
5848
5849 -- Check the result expression of a scalar function against the subtype
5850 -- of the function by inserting a conversion. This conversion must
5851 -- eventually be performed for other classes of types, but for now it's
5852 -- only done for scalars.
5853 -- ???
5854
5855 if Is_Scalar_Type (Exptyp) then
5856 Rewrite (Exp, Convert_To (R_Type, Exp));
5857
5858 -- The expression is resolved to ensure that the conversion gets
5859 -- expanded to generate a possible constraint check.
5860
5861 Analyze_And_Resolve (Exp, R_Type);
5862 end if;
5863
5864 -- Deal with returning variable length objects and controlled types
5865
5866 -- Nothing to do if we are returning by reference, or this is not a
5867 -- type that requires special processing (indicated by the fact that
5868 -- it requires a cleanup scope for the secondary stack case).
5869
5870 if Is_Immutably_Limited_Type (Exptyp)
5871 or else Is_Limited_Interface (Exptyp)
5872 then
5873 null;
5874
5875 elsif not Requires_Transient_Scope (R_Type) then
5876
5877 -- Mutable records with no variable length components are not
5878 -- returned on the sec-stack, so we need to make sure that the
5879 -- backend will only copy back the size of the actual value, and not
5880 -- the maximum size. We create an actual subtype for this purpose.
5881
5882 declare
5883 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
5884 Decl : Node_Id;
5885 Ent : Entity_Id;
5886 begin
5887 if Has_Discriminants (Ubt)
5888 and then not Is_Constrained (Ubt)
5889 and then not Has_Unchecked_Union (Ubt)
5890 then
5891 Decl := Build_Actual_Subtype (Ubt, Exp);
5892 Ent := Defining_Identifier (Decl);
5893 Insert_Action (Exp, Decl);
5894 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
5895 Analyze_And_Resolve (Exp);
5896 end if;
5897 end;
5898
5899 -- Here if secondary stack is used
5900
5901 else
5902 -- Make sure that no surrounding block will reclaim the secondary
5903 -- stack on which we are going to put the result. Not only may this
5904 -- introduce secondary stack leaks but worse, if the reclamation is
5905 -- done too early, then the result we are returning may get
5906 -- clobbered.
5907
5908 declare
5909 S : Entity_Id;
5910 begin
5911 S := Current_Scope;
5912 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
5913 Set_Sec_Stack_Needed_For_Return (S, True);
5914 S := Enclosing_Dynamic_Scope (S);
5915 end loop;
5916 end;
5917
5918 -- Optimize the case where the result is a function call. In this
5919 -- case either the result is already on the secondary stack, or is
5920 -- already being returned with the stack pointer depressed and no
5921 -- further processing is required except to set the By_Ref flag to
5922 -- ensure that gigi does not attempt an extra unnecessary copy.
5923 -- (actually not just unnecessary but harmfully wrong in the case
5924 -- of a controlled type, where gigi does not know how to do a copy).
5925 -- To make up for a gcc 2.8.1 deficiency (???), we perform
5926 -- the copy for array types if the constrained status of the
5927 -- target type is different from that of the expression.
5928
5929 if Requires_Transient_Scope (Exptyp)
5930 and then
5931 (not Is_Array_Type (Exptyp)
5932 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
5933 or else CW_Or_Has_Controlled_Part (Utyp))
5934 and then Nkind (Exp) = N_Function_Call
5935 then
5936 Set_By_Ref (N);
5937
5938 -- Remove side effects from the expression now so that other parts
5939 -- of the expander do not have to reanalyze this node without this
5940 -- optimization
5941
5942 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
5943
5944 -- For controlled types, do the allocation on the secondary stack
5945 -- manually in order to call adjust at the right time:
5946
5947 -- type Anon1 is access R_Type;
5948 -- for Anon1'Storage_pool use ss_pool;
5949 -- Anon2 : anon1 := new R_Type'(expr);
5950 -- return Anon2.all;
5951
5952 -- We do the same for classwide types that are not potentially
5953 -- controlled (by the virtue of restriction No_Finalization) because
5954 -- gigi is not able to properly allocate class-wide types.
5955
5956 elsif CW_Or_Has_Controlled_Part (Utyp) then
5957 declare
5958 Loc : constant Source_Ptr := Sloc (N);
5959 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
5960 Alloc_Node : Node_Id;
5961 Temp : Entity_Id;
5962
5963 begin
5964 Set_Ekind (Acc_Typ, E_Access_Type);
5965
5966 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
5967
5968 -- This is an allocator for the secondary stack, and it's fine
5969 -- to have Comes_From_Source set False on it, as gigi knows not
5970 -- to flag it as a violation of No_Implicit_Heap_Allocations.
5971
5972 Alloc_Node :=
5973 Make_Allocator (Loc,
5974 Expression =>
5975 Make_Qualified_Expression (Loc,
5976 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
5977 Expression => Relocate_Node (Exp)));
5978
5979 -- We do not want discriminant checks on the declaration,
5980 -- given that it gets its value from the allocator.
5981
5982 Set_No_Initialization (Alloc_Node);
5983
5984 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
5985
5986 Insert_List_Before_And_Analyze (N, New_List (
5987 Make_Full_Type_Declaration (Loc,
5988 Defining_Identifier => Acc_Typ,
5989 Type_Definition =>
5990 Make_Access_To_Object_Definition (Loc,
5991 Subtype_Indication => Subtype_Ind)),
5992
5993 Make_Object_Declaration (Loc,
5994 Defining_Identifier => Temp,
5995 Object_Definition => New_Reference_To (Acc_Typ, Loc),
5996 Expression => Alloc_Node)));
5997
5998 Rewrite (Exp,
5999 Make_Explicit_Dereference (Loc,
6000 Prefix => New_Reference_To (Temp, Loc)));
6001
6002 Analyze_And_Resolve (Exp, R_Type);
6003 end;
6004
6005 -- Otherwise use the gigi mechanism to allocate result on the
6006 -- secondary stack.
6007
6008 else
6009 Check_Restriction (No_Secondary_Stack, N);
6010 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6011
6012 -- If we are generating code for the VM do not use
6013 -- SS_Allocate since everything is heap-allocated anyway.
6014
6015 if VM_Target = No_VM then
6016 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6017 end if;
6018 end if;
6019 end if;
6020
6021 -- Implement the rules of 6.5(8-10), which require a tag check in the
6022 -- case of a limited tagged return type, and tag reassignment for
6023 -- nonlimited tagged results. These actions are needed when the return
6024 -- type is a specific tagged type and the result expression is a
6025 -- conversion or a formal parameter, because in that case the tag of the
6026 -- expression might differ from the tag of the specific result type.
6027
6028 if Is_Tagged_Type (Utyp)
6029 and then not Is_Class_Wide_Type (Utyp)
6030 and then (Nkind_In (Exp, N_Type_Conversion,
6031 N_Unchecked_Type_Conversion)
6032 or else (Is_Entity_Name (Exp)
6033 and then Ekind (Entity (Exp)) in Formal_Kind))
6034 then
6035 -- When the return type is limited, perform a check that the
6036 -- tag of the result is the same as the tag of the return type.
6037
6038 if Is_Limited_Type (R_Type) then
6039 Insert_Action (Exp,
6040 Make_Raise_Constraint_Error (Loc,
6041 Condition =>
6042 Make_Op_Ne (Loc,
6043 Left_Opnd =>
6044 Make_Selected_Component (Loc,
6045 Prefix => Duplicate_Subexpr (Exp),
6046 Selector_Name =>
6047 Make_Identifier (Loc, Chars => Name_uTag)),
6048 Right_Opnd =>
6049 Make_Attribute_Reference (Loc,
6050 Prefix => New_Occurrence_Of (Base_Type (Utyp), Loc),
6051 Attribute_Name => Name_Tag)),
6052 Reason => CE_Tag_Check_Failed));
6053
6054 -- If the result type is a specific nonlimited tagged type, then we
6055 -- have to ensure that the tag of the result is that of the result
6056 -- type. This is handled by making a copy of the expression in the
6057 -- case where it might have a different tag, namely when the
6058 -- expression is a conversion or a formal parameter. We create a new
6059 -- object of the result type and initialize it from the expression,
6060 -- which will implicitly force the tag to be set appropriately.
6061
6062 else
6063 declare
6064 ExpR : constant Node_Id := Relocate_Node (Exp);
6065 Result_Id : constant Entity_Id :=
6066 Make_Temporary (Loc, 'R', ExpR);
6067 Result_Exp : constant Node_Id :=
6068 New_Reference_To (Result_Id, Loc);
6069 Result_Obj : constant Node_Id :=
6070 Make_Object_Declaration (Loc,
6071 Defining_Identifier => Result_Id,
6072 Object_Definition =>
6073 New_Reference_To (R_Type, Loc),
6074 Constant_Present => True,
6075 Expression => ExpR);
6076
6077 begin
6078 Set_Assignment_OK (Result_Obj);
6079 Insert_Action (Exp, Result_Obj);
6080
6081 Rewrite (Exp, Result_Exp);
6082 Analyze_And_Resolve (Exp, R_Type);
6083 end;
6084 end if;
6085
6086 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6087 -- a check that the level of the return expression's underlying type
6088 -- is not deeper than the level of the master enclosing the function.
6089 -- Always generate the check when the type of the return expression
6090 -- is class-wide, when it's a type conversion, or when it's a formal
6091 -- parameter. Otherwise, suppress the check in the case where the
6092 -- return expression has a specific type whose level is known not to
6093 -- be statically deeper than the function's result type.
6094
6095 -- Note: accessibility check is skipped in the VM case, since there
6096 -- does not seem to be any practical way to implement this check.
6097
6098 elsif Ada_Version >= Ada_2005
6099 and then Tagged_Type_Expansion
6100 and then Is_Class_Wide_Type (R_Type)
6101 and then not Scope_Suppress (Accessibility_Check)
6102 and then
6103 (Is_Class_Wide_Type (Etype (Exp))
6104 or else Nkind_In (Exp, N_Type_Conversion,
6105 N_Unchecked_Type_Conversion)
6106 or else (Is_Entity_Name (Exp)
6107 and then Ekind (Entity (Exp)) in Formal_Kind)
6108 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6109 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6110 then
6111 declare
6112 Tag_Node : Node_Id;
6113
6114 begin
6115 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6116 -- "this" to reference the base of the object --- required to get
6117 -- access to the TSD of the object.
6118
6119 if Is_Class_Wide_Type (Etype (Exp))
6120 and then Is_Interface (Etype (Exp))
6121 and then Nkind (Exp) = N_Explicit_Dereference
6122 then
6123 Tag_Node :=
6124 Make_Explicit_Dereference (Loc,
6125 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6126 Make_Function_Call (Loc,
6127 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
6128 Parameter_Associations => New_List (
6129 Unchecked_Convert_To (RTE (RE_Address),
6130 Duplicate_Subexpr (Prefix (Exp)))))));
6131 else
6132 Tag_Node :=
6133 Make_Attribute_Reference (Loc,
6134 Prefix => Duplicate_Subexpr (Exp),
6135 Attribute_Name => Name_Tag);
6136 end if;
6137
6138 Insert_Action (Exp,
6139 Make_Raise_Program_Error (Loc,
6140 Condition =>
6141 Make_Op_Gt (Loc,
6142 Left_Opnd =>
6143 Build_Get_Access_Level (Loc, Tag_Node),
6144 Right_Opnd =>
6145 Make_Integer_Literal (Loc,
6146 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6147 Reason => PE_Accessibility_Check_Failed));
6148 end;
6149
6150 -- AI05-0073: If function has a controlling access result, check that
6151 -- the tag of the return value, if it is not null, matches designated
6152 -- type of return type.
6153 -- The return expression is referenced twice in the code below, so
6154 -- it must be made free of side effects. Given that different compilers
6155 -- may evaluate these parameters in different order, both occurrences
6156 -- perform a copy.
6157
6158 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6159 and then Has_Controlling_Result (Scope_Id)
6160 then
6161 Insert_Action (N,
6162 Make_Raise_Constraint_Error (Loc,
6163 Condition =>
6164 Make_And_Then (Loc,
6165 Left_Opnd =>
6166 Make_Op_Ne (Loc,
6167 Left_Opnd => Duplicate_Subexpr (Exp),
6168 Right_Opnd => Make_Null (Loc)),
6169 Right_Opnd => Make_Op_Ne (Loc,
6170 Left_Opnd =>
6171 Make_Selected_Component (Loc,
6172 Prefix => Duplicate_Subexpr (Exp),
6173 Selector_Name =>
6174 Make_Identifier (Loc, Chars => Name_uTag)),
6175 Right_Opnd =>
6176 Make_Attribute_Reference (Loc,
6177 Prefix =>
6178 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6179 Attribute_Name => Name_Tag))),
6180 Reason => CE_Tag_Check_Failed),
6181 Suppress => All_Checks);
6182 end if;
6183
6184 -- If we are returning an object that may not be bit-aligned, then copy
6185 -- the value into a temporary first. This copy may need to expand to a
6186 -- loop of component operations.
6187
6188 if Is_Possibly_Unaligned_Slice (Exp)
6189 or else Is_Possibly_Unaligned_Object (Exp)
6190 then
6191 declare
6192 ExpR : constant Node_Id := Relocate_Node (Exp);
6193 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6194 begin
6195 Insert_Action (Exp,
6196 Make_Object_Declaration (Loc,
6197 Defining_Identifier => Tnn,
6198 Constant_Present => True,
6199 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6200 Expression => ExpR),
6201 Suppress => All_Checks);
6202 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6203 end;
6204 end if;
6205
6206 -- Generate call to postcondition checks if they are present
6207
6208 if Ekind (Scope_Id) = E_Function
6209 and then Has_Postconditions (Scope_Id)
6210 then
6211 -- We are going to reference the returned value twice in this case,
6212 -- once in the call to _Postconditions, and once in the actual return
6213 -- statement, but we can't have side effects happening twice, and in
6214 -- any case for efficiency we don't want to do the computation twice.
6215
6216 -- If the returned expression is an entity name, we don't need to
6217 -- worry since it is efficient and safe to reference it twice, that's
6218 -- also true for literals other than string literals, and for the
6219 -- case of X.all where X is an entity name.
6220
6221 if Is_Entity_Name (Exp)
6222 or else Nkind_In (Exp, N_Character_Literal,
6223 N_Integer_Literal,
6224 N_Real_Literal)
6225 or else (Nkind (Exp) = N_Explicit_Dereference
6226 and then Is_Entity_Name (Prefix (Exp)))
6227 then
6228 null;
6229
6230 -- Otherwise we are going to need a temporary to capture the value
6231
6232 else
6233 declare
6234 ExpR : constant Node_Id := Relocate_Node (Exp);
6235 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6236
6237 begin
6238 -- For a complex expression of an elementary type, capture
6239 -- value in the temporary and use it as the reference.
6240
6241 if Is_Elementary_Type (R_Type) then
6242 Insert_Action (Exp,
6243 Make_Object_Declaration (Loc,
6244 Defining_Identifier => Tnn,
6245 Constant_Present => True,
6246 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6247 Expression => ExpR),
6248 Suppress => All_Checks);
6249
6250 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6251
6252 -- If we have something we can rename, generate a renaming of
6253 -- the object and replace the expression with a reference
6254
6255 elsif Is_Object_Reference (Exp) then
6256 Insert_Action (Exp,
6257 Make_Object_Renaming_Declaration (Loc,
6258 Defining_Identifier => Tnn,
6259 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
6260 Name => ExpR),
6261 Suppress => All_Checks);
6262
6263 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6264
6265 -- Otherwise we have something like a string literal or an
6266 -- aggregate. We could copy the value, but that would be
6267 -- inefficient. Instead we make a reference to the value and
6268 -- capture this reference with a renaming, the expression is
6269 -- then replaced by a dereference of this renaming.
6270
6271 else
6272 -- For now, copy the value, since the code below does not
6273 -- seem to work correctly ???
6274
6275 Insert_Action (Exp,
6276 Make_Object_Declaration (Loc,
6277 Defining_Identifier => Tnn,
6278 Constant_Present => True,
6279 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6280 Expression => Relocate_Node (Exp)),
6281 Suppress => All_Checks);
6282
6283 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6284
6285 -- Insert_Action (Exp,
6286 -- Make_Object_Renaming_Declaration (Loc,
6287 -- Defining_Identifier => Tnn,
6288 -- Access_Definition =>
6289 -- Make_Access_Definition (Loc,
6290 -- All_Present => True,
6291 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
6292 -- Name =>
6293 -- Make_Reference (Loc,
6294 -- Prefix => Relocate_Node (Exp))),
6295 -- Suppress => All_Checks);
6296
6297 -- Rewrite (Exp,
6298 -- Make_Explicit_Dereference (Loc,
6299 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
6300 end if;
6301 end;
6302 end if;
6303
6304 -- Generate call to _postconditions
6305
6306 Insert_Action (Exp,
6307 Make_Procedure_Call_Statement (Loc,
6308 Name => Make_Identifier (Loc, Name_uPostconditions),
6309 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
6310 end if;
6311
6312 -- Ada 2005 (AI-251): If this return statement corresponds with an
6313 -- simple return statement associated with an extended return statement
6314 -- and the type of the returned object is an interface then generate an
6315 -- implicit conversion to force displacement of the "this" pointer.
6316
6317 if Ada_Version >= Ada_2005
6318 and then Comes_From_Extended_Return_Statement (N)
6319 and then Nkind (Expression (N)) = N_Identifier
6320 and then Is_Interface (Utyp)
6321 and then Utyp /= Underlying_Type (Exptyp)
6322 then
6323 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6324 Analyze_And_Resolve (Exp);
6325 end if;
6326 end Expand_Simple_Function_Return;
6327
6328 --------------------------------
6329 -- Is_Build_In_Place_Function --
6330 --------------------------------
6331
6332 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
6333 begin
6334 -- This function is called from Expand_Subtype_From_Expr during
6335 -- semantic analysis, even when expansion is off. In those cases
6336 -- the build_in_place expansion will not take place.
6337
6338 if not Expander_Active then
6339 return False;
6340 end if;
6341
6342 -- For now we test whether E denotes a function or access-to-function
6343 -- type whose result subtype is inherently limited. Later this test may
6344 -- be revised to allow composite nonlimited types. Functions with a
6345 -- foreign convention or whose result type has a foreign convention
6346 -- never qualify.
6347
6348 if Ekind_In (E, E_Function, E_Generic_Function)
6349 or else (Ekind (E) = E_Subprogram_Type
6350 and then Etype (E) /= Standard_Void_Type)
6351 then
6352 -- Note: If you have Convention (C) on an inherently limited type,
6353 -- you're on your own. That is, the C code will have to be carefully
6354 -- written to know about the Ada conventions.
6355
6356 if Has_Foreign_Convention (E)
6357 or else Has_Foreign_Convention (Etype (E))
6358 then
6359 return False;
6360
6361 -- In Ada 2005 all functions with an inherently limited return type
6362 -- must be handled using a build-in-place profile, including the case
6363 -- of a function with a limited interface result, where the function
6364 -- may return objects of nonlimited descendants.
6365
6366 else
6367 return Is_Immutably_Limited_Type (Etype (E))
6368 and then Ada_Version >= Ada_2005
6369 and then not Debug_Flag_Dot_L;
6370 end if;
6371
6372 else
6373 return False;
6374 end if;
6375 end Is_Build_In_Place_Function;
6376
6377 -------------------------------------
6378 -- Is_Build_In_Place_Function_Call --
6379 -------------------------------------
6380
6381 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
6382 Exp_Node : Node_Id := N;
6383 Function_Id : Entity_Id;
6384
6385 begin
6386 -- Step past qualification or unchecked conversion (the latter can occur
6387 -- in cases of calls to 'Input).
6388
6389 if Nkind_In
6390 (Exp_Node, N_Qualified_Expression, N_Unchecked_Type_Conversion)
6391 then
6392 Exp_Node := Expression (N);
6393 end if;
6394
6395 if Nkind (Exp_Node) /= N_Function_Call then
6396 return False;
6397
6398 else
6399 if Is_Entity_Name (Name (Exp_Node)) then
6400 Function_Id := Entity (Name (Exp_Node));
6401
6402 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
6403 Function_Id := Etype (Name (Exp_Node));
6404 end if;
6405
6406 return Is_Build_In_Place_Function (Function_Id);
6407 end if;
6408 end Is_Build_In_Place_Function_Call;
6409
6410 -----------------------
6411 -- Freeze_Subprogram --
6412 -----------------------
6413
6414 procedure Freeze_Subprogram (N : Node_Id) is
6415 Loc : constant Source_Ptr := Sloc (N);
6416
6417 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
6418 -- (Ada 2005): Register a predefined primitive in all the secondary
6419 -- dispatch tables of its primitive type.
6420
6421 ----------------------------------
6422 -- Register_Predefined_DT_Entry --
6423 ----------------------------------
6424
6425 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
6426 Iface_DT_Ptr : Elmt_Id;
6427 Tagged_Typ : Entity_Id;
6428 Thunk_Id : Entity_Id;
6429 Thunk_Code : Node_Id;
6430
6431 begin
6432 Tagged_Typ := Find_Dispatching_Type (Prim);
6433
6434 if No (Access_Disp_Table (Tagged_Typ))
6435 or else not Has_Interfaces (Tagged_Typ)
6436 or else not RTE_Available (RE_Interface_Tag)
6437 or else Restriction_Active (No_Dispatching_Calls)
6438 then
6439 return;
6440 end if;
6441
6442 -- Skip the first two access-to-dispatch-table pointers since they
6443 -- leads to the primary dispatch table (predefined DT and user
6444 -- defined DT). We are only concerned with the secondary dispatch
6445 -- table pointers. Note that the access-to- dispatch-table pointer
6446 -- corresponds to the first implemented interface retrieved below.
6447
6448 Iface_DT_Ptr :=
6449 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
6450
6451 while Present (Iface_DT_Ptr)
6452 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
6453 loop
6454 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
6455 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
6456
6457 if Present (Thunk_Code) then
6458 Insert_Actions_After (N, New_List (
6459 Thunk_Code,
6460
6461 Build_Set_Predefined_Prim_Op_Address (Loc,
6462 Tag_Node =>
6463 New_Reference_To (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
6464 Position => DT_Position (Prim),
6465 Address_Node =>
6466 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
6467 Make_Attribute_Reference (Loc,
6468 Prefix => New_Reference_To (Thunk_Id, Loc),
6469 Attribute_Name => Name_Unrestricted_Access))),
6470
6471 Build_Set_Predefined_Prim_Op_Address (Loc,
6472 Tag_Node =>
6473 New_Reference_To
6474 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
6475 Loc),
6476 Position => DT_Position (Prim),
6477 Address_Node =>
6478 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
6479 Make_Attribute_Reference (Loc,
6480 Prefix => New_Reference_To (Prim, Loc),
6481 Attribute_Name => Name_Unrestricted_Access)))));
6482 end if;
6483
6484 -- Skip the tag of the predefined primitives dispatch table
6485
6486 Next_Elmt (Iface_DT_Ptr);
6487 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
6488
6489 -- Skip the tag of the no-thunks dispatch table
6490
6491 Next_Elmt (Iface_DT_Ptr);
6492 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
6493
6494 -- Skip the tag of the predefined primitives no-thunks dispatch
6495 -- table
6496
6497 Next_Elmt (Iface_DT_Ptr);
6498 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
6499
6500 Next_Elmt (Iface_DT_Ptr);
6501 end loop;
6502 end Register_Predefined_DT_Entry;
6503
6504 -- Local variables
6505
6506 Subp : constant Entity_Id := Entity (N);
6507
6508 -- Start of processing for Freeze_Subprogram
6509
6510 begin
6511 -- We suppress the initialization of the dispatch table entry when
6512 -- VM_Target because the dispatching mechanism is handled internally
6513 -- by the VM.
6514
6515 if Is_Dispatching_Operation (Subp)
6516 and then not Is_Abstract_Subprogram (Subp)
6517 and then Present (DTC_Entity (Subp))
6518 and then Present (Scope (DTC_Entity (Subp)))
6519 and then Tagged_Type_Expansion
6520 and then not Restriction_Active (No_Dispatching_Calls)
6521 and then RTE_Available (RE_Tag)
6522 then
6523 declare
6524 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
6525
6526 begin
6527 -- Handle private overridden primitives
6528
6529 if not Is_CPP_Class (Typ) then
6530 Check_Overriding_Operation (Subp);
6531 end if;
6532
6533 -- We assume that imported CPP primitives correspond with objects
6534 -- whose constructor is in the CPP side; therefore we don't need
6535 -- to generate code to register them in the dispatch table.
6536
6537 if Is_CPP_Class (Typ) then
6538 null;
6539
6540 -- Handle CPP primitives found in derivations of CPP_Class types.
6541 -- These primitives must have been inherited from some parent, and
6542 -- there is no need to register them in the dispatch table because
6543 -- Build_Inherit_Prims takes care of the initialization of these
6544 -- slots.
6545
6546 elsif Is_Imported (Subp)
6547 and then (Convention (Subp) = Convention_CPP
6548 or else Convention (Subp) = Convention_C)
6549 then
6550 null;
6551
6552 -- Generate code to register the primitive in non statically
6553 -- allocated dispatch tables
6554
6555 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
6556
6557 -- When a primitive is frozen, enter its name in its dispatch
6558 -- table slot.
6559
6560 if not Is_Interface (Typ)
6561 or else Present (Interface_Alias (Subp))
6562 then
6563 if Is_Predefined_Dispatching_Operation (Subp) then
6564 Register_Predefined_DT_Entry (Subp);
6565 end if;
6566
6567 Insert_Actions_After (N,
6568 Register_Primitive (Loc, Prim => Subp));
6569 end if;
6570 end if;
6571 end;
6572 end if;
6573
6574 -- Mark functions that return by reference. Note that it cannot be part
6575 -- of the normal semantic analysis of the spec since the underlying
6576 -- returned type may not be known yet (for private types).
6577
6578 declare
6579 Typ : constant Entity_Id := Etype (Subp);
6580 Utyp : constant Entity_Id := Underlying_Type (Typ);
6581 begin
6582 if Is_Immutably_Limited_Type (Typ) then
6583 Set_Returns_By_Ref (Subp);
6584 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6585 Set_Returns_By_Ref (Subp);
6586 end if;
6587 end;
6588 end Freeze_Subprogram;
6589
6590 -----------------------
6591 -- Is_Null_Procedure --
6592 -----------------------
6593
6594 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
6595 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
6596
6597 begin
6598 if Ekind (Subp) /= E_Procedure then
6599 return False;
6600
6601 -- Check if this is a declared null procedure
6602
6603 elsif Nkind (Decl) = N_Subprogram_Declaration then
6604 if not Null_Present (Specification (Decl)) then
6605 return False;
6606
6607 elsif No (Body_To_Inline (Decl)) then
6608 return False;
6609
6610 -- Check if the body contains only a null statement, followed by
6611 -- the return statement added during expansion.
6612
6613 else
6614 declare
6615 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
6616
6617 Stat : Node_Id;
6618 Stat2 : Node_Id;
6619
6620 begin
6621 if Nkind (Orig_Bod) /= N_Subprogram_Body then
6622 return False;
6623 else
6624 -- We must skip SCIL nodes because they are currently
6625 -- implemented as special N_Null_Statement nodes.
6626
6627 Stat :=
6628 First_Non_SCIL_Node
6629 (Statements (Handled_Statement_Sequence (Orig_Bod)));
6630 Stat2 := Next_Non_SCIL_Node (Stat);
6631
6632 return
6633 Is_Empty_List (Declarations (Orig_Bod))
6634 and then Nkind (Stat) = N_Null_Statement
6635 and then
6636 (No (Stat2)
6637 or else
6638 (Nkind (Stat2) = N_Simple_Return_Statement
6639 and then No (Next (Stat2))));
6640 end if;
6641 end;
6642 end if;
6643
6644 else
6645 return False;
6646 end if;
6647 end Is_Null_Procedure;
6648
6649 -------------------------------------------
6650 -- Make_Build_In_Place_Call_In_Allocator --
6651 -------------------------------------------
6652
6653 procedure Make_Build_In_Place_Call_In_Allocator
6654 (Allocator : Node_Id;
6655 Function_Call : Node_Id)
6656 is
6657 Loc : Source_Ptr;
6658 Func_Call : Node_Id := Function_Call;
6659 Function_Id : Entity_Id;
6660 Result_Subt : Entity_Id;
6661 Acc_Type : constant Entity_Id := Etype (Allocator);
6662 New_Allocator : Node_Id;
6663 Return_Obj_Access : Entity_Id;
6664
6665 begin
6666 -- Step past qualification or unchecked conversion (the latter can occur
6667 -- in cases of calls to 'Input).
6668
6669 if Nkind_In (Func_Call,
6670 N_Qualified_Expression,
6671 N_Unchecked_Type_Conversion)
6672 then
6673 Func_Call := Expression (Func_Call);
6674 end if;
6675
6676 -- If the call has already been processed to add build-in-place actuals
6677 -- then return. This should not normally occur in an allocator context,
6678 -- but we add the protection as a defensive measure.
6679
6680 if Is_Expanded_Build_In_Place_Call (Func_Call) then
6681 return;
6682 end if;
6683
6684 -- Mark the call as processed as a build-in-place call
6685
6686 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
6687
6688 Loc := Sloc (Function_Call);
6689
6690 if Is_Entity_Name (Name (Func_Call)) then
6691 Function_Id := Entity (Name (Func_Call));
6692
6693 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
6694 Function_Id := Etype (Name (Func_Call));
6695
6696 else
6697 raise Program_Error;
6698 end if;
6699
6700 Result_Subt := Etype (Function_Id);
6701
6702 -- When the result subtype is constrained, the return object must be
6703 -- allocated on the caller side, and access to it is passed to the
6704 -- function.
6705
6706 -- Here and in related routines, we must examine the full view of the
6707 -- type, because the view at the point of call may differ from that
6708 -- that in the function body, and the expansion mechanism depends on
6709 -- the characteristics of the full view.
6710
6711 if Is_Constrained (Underlying_Type (Result_Subt)) then
6712
6713 -- Replace the initialized allocator of form "new T'(Func (...))"
6714 -- with an uninitialized allocator of form "new T", where T is the
6715 -- result subtype of the called function. The call to the function
6716 -- is handled separately further below.
6717
6718 New_Allocator :=
6719 Make_Allocator (Loc,
6720 Expression => New_Reference_To (Result_Subt, Loc));
6721 Set_No_Initialization (New_Allocator);
6722
6723 -- Copy attributes to new allocator. Note that the new allocator
6724 -- logically comes from source if the original one did, so copy the
6725 -- relevant flag. This ensures proper treatment of the restriction
6726 -- No_Implicit_Heap_Allocations in this case.
6727
6728 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
6729 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
6730 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
6731
6732 Rewrite (Allocator, New_Allocator);
6733
6734 -- Create a new access object and initialize it to the result of the
6735 -- new uninitialized allocator. Note: we do not use Allocator as the
6736 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
6737 -- as this would create a sort of infinite "recursion".
6738
6739 Return_Obj_Access := Make_Temporary (Loc, 'R');
6740 Set_Etype (Return_Obj_Access, Acc_Type);
6741
6742 Insert_Action (Allocator,
6743 Make_Object_Declaration (Loc,
6744 Defining_Identifier => Return_Obj_Access,
6745 Object_Definition => New_Reference_To (Acc_Type, Loc),
6746 Expression => Relocate_Node (Allocator)));
6747
6748 -- When the function has a controlling result, an allocation-form
6749 -- parameter must be passed indicating that the caller is allocating
6750 -- the result object. This is needed because such a function can be
6751 -- called as a dispatching operation and must be treated similarly
6752 -- to functions with unconstrained result subtypes.
6753
6754 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6755 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
6756
6757 Add_Final_List_Actual_To_Build_In_Place_Call
6758 (Func_Call, Function_Id, Acc_Type);
6759
6760 Add_Task_Actuals_To_Build_In_Place_Call
6761 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
6762
6763 -- Add an implicit actual to the function call that provides access
6764 -- to the allocated object. An unchecked conversion to the (specific)
6765 -- result subtype of the function is inserted to handle cases where
6766 -- the access type of the allocator has a class-wide designated type.
6767
6768 Add_Access_Actual_To_Build_In_Place_Call
6769 (Func_Call,
6770 Function_Id,
6771 Make_Unchecked_Type_Conversion (Loc,
6772 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
6773 Expression =>
6774 Make_Explicit_Dereference (Loc,
6775 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
6776
6777 -- When the result subtype is unconstrained, the function itself must
6778 -- perform the allocation of the return object, so we pass parameters
6779 -- indicating that. We don't yet handle the case where the allocation
6780 -- must be done in a user-defined storage pool, which will require
6781 -- passing another actual or two to provide allocation/deallocation
6782 -- operations. ???
6783
6784 else
6785
6786 -- Pass an allocation parameter indicating that the function should
6787 -- allocate its result on the heap.
6788
6789 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6790 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
6791
6792 Add_Final_List_Actual_To_Build_In_Place_Call
6793 (Func_Call, Function_Id, Acc_Type);
6794
6795 Add_Task_Actuals_To_Build_In_Place_Call
6796 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
6797
6798 -- The caller does not provide the return object in this case, so we
6799 -- have to pass null for the object access actual.
6800
6801 Add_Access_Actual_To_Build_In_Place_Call
6802 (Func_Call, Function_Id, Return_Object => Empty);
6803 end if;
6804
6805 -- Finally, replace the allocator node with a reference to the result
6806 -- of the function call itself (which will effectively be an access
6807 -- to the object created by the allocator).
6808
6809 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
6810 Analyze_And_Resolve (Allocator, Acc_Type);
6811 end Make_Build_In_Place_Call_In_Allocator;
6812
6813 ---------------------------------------------------
6814 -- Make_Build_In_Place_Call_In_Anonymous_Context --
6815 ---------------------------------------------------
6816
6817 procedure Make_Build_In_Place_Call_In_Anonymous_Context
6818 (Function_Call : Node_Id)
6819 is
6820 Loc : Source_Ptr;
6821 Func_Call : Node_Id := Function_Call;
6822 Function_Id : Entity_Id;
6823 Result_Subt : Entity_Id;
6824 Return_Obj_Id : Entity_Id;
6825 Return_Obj_Decl : Entity_Id;
6826
6827 begin
6828 -- Step past qualification or unchecked conversion (the latter can occur
6829 -- in cases of calls to 'Input).
6830
6831 if Nkind_In (Func_Call, N_Qualified_Expression,
6832 N_Unchecked_Type_Conversion)
6833 then
6834 Func_Call := Expression (Func_Call);
6835 end if;
6836
6837 -- If the call has already been processed to add build-in-place actuals
6838 -- then return. One place this can occur is for calls to build-in-place
6839 -- functions that occur within a call to a protected operation, where
6840 -- due to rewriting and expansion of the protected call there can be
6841 -- more than one call to Expand_Actuals for the same set of actuals.
6842
6843 if Is_Expanded_Build_In_Place_Call (Func_Call) then
6844 return;
6845 end if;
6846
6847 -- Mark the call as processed as a build-in-place call
6848
6849 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
6850
6851 Loc := Sloc (Function_Call);
6852
6853 if Is_Entity_Name (Name (Func_Call)) then
6854 Function_Id := Entity (Name (Func_Call));
6855
6856 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
6857 Function_Id := Etype (Name (Func_Call));
6858
6859 else
6860 raise Program_Error;
6861 end if;
6862
6863 Result_Subt := Etype (Function_Id);
6864
6865 -- When the result subtype is constrained, an object of the subtype is
6866 -- declared and an access value designating it is passed as an actual.
6867
6868 if Is_Constrained (Underlying_Type (Result_Subt)) then
6869
6870 -- Create a temporary object to hold the function result
6871
6872 Return_Obj_Id := Make_Temporary (Loc, 'R');
6873 Set_Etype (Return_Obj_Id, Result_Subt);
6874
6875 Return_Obj_Decl :=
6876 Make_Object_Declaration (Loc,
6877 Defining_Identifier => Return_Obj_Id,
6878 Aliased_Present => True,
6879 Object_Definition => New_Reference_To (Result_Subt, Loc));
6880
6881 Set_No_Initialization (Return_Obj_Decl);
6882
6883 Insert_Action (Func_Call, Return_Obj_Decl);
6884
6885 -- When the function has a controlling result, an allocation-form
6886 -- parameter must be passed indicating that the caller is allocating
6887 -- the result object. This is needed because such a function can be
6888 -- called as a dispatching operation and must be treated similarly
6889 -- to functions with unconstrained result subtypes.
6890
6891 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6892 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
6893
6894 Add_Final_List_Actual_To_Build_In_Place_Call
6895 (Func_Call, Function_Id, Acc_Type => Empty);
6896
6897 Add_Task_Actuals_To_Build_In_Place_Call
6898 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
6899
6900 -- Add an implicit actual to the function call that provides access
6901 -- to the caller's return object.
6902
6903 Add_Access_Actual_To_Build_In_Place_Call
6904 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
6905
6906 -- When the result subtype is unconstrained, the function must allocate
6907 -- the return object in the secondary stack, so appropriate implicit
6908 -- parameters are added to the call to indicate that. A transient
6909 -- scope is established to ensure eventual cleanup of the result.
6910
6911 else
6912 -- Pass an allocation parameter indicating that the function should
6913 -- allocate its result on the secondary stack.
6914
6915 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6916 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
6917
6918 Add_Final_List_Actual_To_Build_In_Place_Call
6919 (Func_Call, Function_Id, Acc_Type => Empty);
6920
6921 Add_Task_Actuals_To_Build_In_Place_Call
6922 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
6923
6924 -- Pass a null value to the function since no return object is
6925 -- available on the caller side.
6926
6927 Add_Access_Actual_To_Build_In_Place_Call
6928 (Func_Call, Function_Id, Empty);
6929 end if;
6930 end Make_Build_In_Place_Call_In_Anonymous_Context;
6931
6932 --------------------------------------------
6933 -- Make_Build_In_Place_Call_In_Assignment --
6934 --------------------------------------------
6935
6936 procedure Make_Build_In_Place_Call_In_Assignment
6937 (Assign : Node_Id;
6938 Function_Call : Node_Id)
6939 is
6940 Lhs : constant Node_Id := Name (Assign);
6941 Func_Call : Node_Id := Function_Call;
6942 Func_Id : Entity_Id;
6943 Loc : Source_Ptr;
6944 Obj_Decl : Node_Id;
6945 Obj_Id : Entity_Id;
6946 Ptr_Typ : Entity_Id;
6947 Ptr_Typ_Decl : Node_Id;
6948 Result_Subt : Entity_Id;
6949 Target : Node_Id;
6950
6951 begin
6952 -- Step past qualification or unchecked conversion (the latter can occur
6953 -- in cases of calls to 'Input).
6954
6955 if Nkind_In (Func_Call, N_Qualified_Expression,
6956 N_Unchecked_Type_Conversion)
6957 then
6958 Func_Call := Expression (Func_Call);
6959 end if;
6960
6961 -- If the call has already been processed to add build-in-place actuals
6962 -- then return. This should not normally occur in an assignment context,
6963 -- but we add the protection as a defensive measure.
6964
6965 if Is_Expanded_Build_In_Place_Call (Func_Call) then
6966 return;
6967 end if;
6968
6969 -- Mark the call as processed as a build-in-place call
6970
6971 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
6972
6973 Loc := Sloc (Function_Call);
6974
6975 if Is_Entity_Name (Name (Func_Call)) then
6976 Func_Id := Entity (Name (Func_Call));
6977
6978 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
6979 Func_Id := Etype (Name (Func_Call));
6980
6981 else
6982 raise Program_Error;
6983 end if;
6984
6985 Result_Subt := Etype (Func_Id);
6986
6987 -- When the result subtype is unconstrained, an additional actual must
6988 -- be passed to indicate that the caller is providing the return object.
6989 -- This parameter must also be passed when the called function has a
6990 -- controlling result, because dispatching calls to the function needs
6991 -- to be treated effectively the same as calls to class-wide functions.
6992
6993 Add_Alloc_Form_Actual_To_Build_In_Place_Call
6994 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
6995
6996 -- If Lhs is a selected component, then pass it along so that its prefix
6997 -- object will be used as the source of the finalization list.
6998
6999 if Nkind (Lhs) = N_Selected_Component then
7000 Add_Final_List_Actual_To_Build_In_Place_Call
7001 (Func_Call, Func_Id, Acc_Type => Empty, Sel_Comp => Lhs);
7002 else
7003 Add_Final_List_Actual_To_Build_In_Place_Call
7004 (Func_Call, Func_Id, Acc_Type => Empty);
7005 end if;
7006
7007 Add_Task_Actuals_To_Build_In_Place_Call
7008 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
7009
7010 -- Add an implicit actual to the function call that provides access to
7011 -- the caller's return object.
7012
7013 Add_Access_Actual_To_Build_In_Place_Call
7014 (Func_Call,
7015 Func_Id,
7016 Make_Unchecked_Type_Conversion (Loc,
7017 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
7018 Expression => Relocate_Node (Lhs)));
7019
7020 -- Create an access type designating the function's result subtype
7021
7022 Ptr_Typ := Make_Temporary (Loc, 'A');
7023
7024 Ptr_Typ_Decl :=
7025 Make_Full_Type_Declaration (Loc,
7026 Defining_Identifier => Ptr_Typ,
7027 Type_Definition =>
7028 Make_Access_To_Object_Definition (Loc,
7029 All_Present => True,
7030 Subtype_Indication =>
7031 New_Reference_To (Result_Subt, Loc)));
7032 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
7033
7034 -- Finally, create an access object initialized to a reference to the
7035 -- function call.
7036
7037 Obj_Id := Make_Temporary (Loc, 'R');
7038 Set_Etype (Obj_Id, Ptr_Typ);
7039
7040 Obj_Decl :=
7041 Make_Object_Declaration (Loc,
7042 Defining_Identifier => Obj_Id,
7043 Object_Definition =>
7044 New_Reference_To (Ptr_Typ, Loc),
7045 Expression =>
7046 Make_Reference (Loc,
7047 Prefix => Relocate_Node (Func_Call)));
7048 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
7049
7050 Rewrite (Assign, Make_Null_Statement (Loc));
7051
7052 -- Retrieve the target of the assignment
7053
7054 if Nkind (Lhs) = N_Selected_Component then
7055 Target := Selector_Name (Lhs);
7056 elsif Nkind (Lhs) = N_Type_Conversion then
7057 Target := Expression (Lhs);
7058 else
7059 Target := Lhs;
7060 end if;
7061
7062 -- If we are assigning to a return object or this is an expression of
7063 -- an extension aggregate, the target should either be an identifier
7064 -- or a simple expression. All other cases imply a different scenario.
7065
7066 if Nkind (Target) in N_Has_Entity then
7067 Target := Entity (Target);
7068 else
7069 return;
7070 end if;
7071
7072 -- When the target of the assignment is a return object of an enclosing
7073 -- build-in-place function and also requires finalization, the list
7074 -- generated for the assignment must be moved to that of the enclosing
7075 -- function.
7076
7077 -- function Enclosing_BIP_Function return Ctrl_Typ is
7078 -- begin
7079 -- return (Ctrl_Parent_Part => BIP_Function with ...);
7080 -- end Enclosing_BIP_Function;
7081
7082 if Is_Return_Object (Target)
7083 and then Needs_Finalization (Etype (Target))
7084 and then Needs_Finalization (Result_Subt)
7085 then
7086 declare
7087 Obj_List : constant Node_Id := Find_Final_List (Obj_Id);
7088 Encl_List : Node_Id;
7089 Encl_Scop : Entity_Id;
7090
7091 begin
7092 Encl_Scop := Scope (Target);
7093
7094 -- Locate the scope of the extended return statement
7095
7096 while Present (Encl_Scop)
7097 and then Ekind (Encl_Scop) /= E_Return_Statement
7098 loop
7099 Encl_Scop := Scope (Encl_Scop);
7100 end loop;
7101
7102 -- A return object should always be enclosed by a return statement
7103 -- scope at some level.
7104
7105 pragma Assert (Present (Encl_Scop));
7106
7107 Encl_List :=
7108 Make_Attribute_Reference (Loc,
7109 Prefix =>
7110 New_Reference_To (
7111 Finalization_Chain_Entity (Encl_Scop), Loc),
7112 Attribute_Name => Name_Unrestricted_Access);
7113
7114 -- Generate a call to move final list
7115
7116 Insert_After_And_Analyze (Obj_Decl,
7117 Make_Procedure_Call_Statement (Loc,
7118 Name =>
7119 New_Reference_To (RTE (RE_Move_Final_List), Loc),
7120 Parameter_Associations => New_List (Obj_List, Encl_List)));
7121 end;
7122 end if;
7123 end Make_Build_In_Place_Call_In_Assignment;
7124
7125 ----------------------------------------------------
7126 -- Make_Build_In_Place_Call_In_Object_Declaration --
7127 ----------------------------------------------------
7128
7129 procedure Make_Build_In_Place_Call_In_Object_Declaration
7130 (Object_Decl : Node_Id;
7131 Function_Call : Node_Id)
7132 is
7133 Loc : Source_Ptr;
7134 Obj_Def_Id : constant Entity_Id :=
7135 Defining_Identifier (Object_Decl);
7136
7137 Func_Call : Node_Id := Function_Call;
7138 Function_Id : Entity_Id;
7139 Result_Subt : Entity_Id;
7140 Caller_Object : Node_Id;
7141 Call_Deref : Node_Id;
7142 Ref_Type : Entity_Id;
7143 Ptr_Typ_Decl : Node_Id;
7144 Def_Id : Entity_Id;
7145 New_Expr : Node_Id;
7146 Enclosing_Func : Entity_Id;
7147 Pass_Caller_Acc : Boolean := False;
7148
7149 begin
7150 -- Step past qualification or unchecked conversion (the latter can occur
7151 -- in cases of calls to 'Input).
7152
7153 if Nkind_In (Func_Call, N_Qualified_Expression,
7154 N_Unchecked_Type_Conversion)
7155 then
7156 Func_Call := Expression (Func_Call);
7157 end if;
7158
7159 -- If the call has already been processed to add build-in-place actuals
7160 -- then return. This should not normally occur in an object declaration,
7161 -- but we add the protection as a defensive measure.
7162
7163 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7164 return;
7165 end if;
7166
7167 -- Mark the call as processed as a build-in-place call
7168
7169 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7170
7171 Loc := Sloc (Function_Call);
7172
7173 if Is_Entity_Name (Name (Func_Call)) then
7174 Function_Id := Entity (Name (Func_Call));
7175
7176 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7177 Function_Id := Etype (Name (Func_Call));
7178
7179 else
7180 raise Program_Error;
7181 end if;
7182
7183 Result_Subt := Etype (Function_Id);
7184
7185 -- In the constrained case, add an implicit actual to the function call
7186 -- that provides access to the declared object. An unchecked conversion
7187 -- to the (specific) result type of the function is inserted to handle
7188 -- the case where the object is declared with a class-wide type.
7189
7190 if Is_Constrained (Underlying_Type (Result_Subt)) then
7191 Caller_Object :=
7192 Make_Unchecked_Type_Conversion (Loc,
7193 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
7194 Expression => New_Reference_To (Obj_Def_Id, Loc));
7195
7196 -- When the function has a controlling result, an allocation-form
7197 -- parameter must be passed indicating that the caller is allocating
7198 -- the result object. This is needed because such a function can be
7199 -- called as a dispatching operation and must be treated similarly
7200 -- to functions with unconstrained result subtypes.
7201
7202 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7203 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7204
7205 -- If the function's result subtype is unconstrained and the object is
7206 -- a return object of an enclosing build-in-place function, then the
7207 -- implicit build-in-place parameters of the enclosing function must be
7208 -- passed along to the called function. (Unfortunately, this won't cover
7209 -- the case of extension aggregates where the ancestor part is a build-
7210 -- in-place unconstrained function call that should be passed along the
7211 -- caller's parameters. Currently those get mishandled by reassigning
7212 -- the result of the call to the aggregate return object, when the call
7213 -- result should really be directly built in place in the aggregate and
7214 -- not built in a temporary. ???)
7215
7216 elsif Is_Return_Object (Defining_Identifier (Object_Decl)) then
7217 Pass_Caller_Acc := True;
7218
7219 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
7220
7221 -- If the enclosing function has a constrained result type, then
7222 -- caller allocation will be used.
7223
7224 if Is_Constrained (Etype (Enclosing_Func)) then
7225 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7226 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7227
7228 -- Otherwise, when the enclosing function has an unconstrained result
7229 -- type, the BIP_Alloc_Form formal of the enclosing function must be
7230 -- passed along to the callee.
7231
7232 else
7233 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7234 (Func_Call,
7235 Function_Id,
7236 Alloc_Form_Exp =>
7237 New_Reference_To
7238 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
7239 Loc));
7240 end if;
7241
7242 -- Retrieve the BIPacc formal from the enclosing function and convert
7243 -- it to the access type of the callee's BIP_Object_Access formal.
7244
7245 Caller_Object :=
7246 Make_Unchecked_Type_Conversion (Loc,
7247 Subtype_Mark =>
7248 New_Reference_To
7249 (Etype
7250 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
7251 Loc),
7252 Expression =>
7253 New_Reference_To
7254 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
7255 Loc));
7256
7257 -- In other unconstrained cases, pass an indication to do the allocation
7258 -- on the secondary stack and set Caller_Object to Empty so that a null
7259 -- value will be passed for the caller's object address. A transient
7260 -- scope is established to ensure eventual cleanup of the result.
7261
7262 else
7263 Add_Alloc_Form_Actual_To_Build_In_Place_Call
7264 (Func_Call,
7265 Function_Id,
7266 Alloc_Form => Secondary_Stack);
7267 Caller_Object := Empty;
7268
7269 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
7270 end if;
7271
7272 Add_Final_List_Actual_To_Build_In_Place_Call
7273 (Func_Call, Function_Id, Acc_Type => Empty);
7274
7275 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
7276 and then Has_Task (Result_Subt)
7277 then
7278 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
7279
7280 -- Here we're passing along the master that was passed in to this
7281 -- function.
7282
7283 Add_Task_Actuals_To_Build_In_Place_Call
7284 (Func_Call, Function_Id,
7285 Master_Actual =>
7286 New_Reference_To
7287 (Build_In_Place_Formal (Enclosing_Func, BIP_Master), Loc));
7288
7289 else
7290 Add_Task_Actuals_To_Build_In_Place_Call
7291 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7292 end if;
7293
7294 Add_Access_Actual_To_Build_In_Place_Call
7295 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
7296
7297 -- Create an access type designating the function's result subtype. We
7298 -- use the type of the original expression because it may be a call to
7299 -- an inherited operation, which the expansion has replaced with the
7300 -- parent operation that yields the parent type.
7301
7302 Ref_Type := Make_Temporary (Loc, 'A');
7303
7304 Ptr_Typ_Decl :=
7305 Make_Full_Type_Declaration (Loc,
7306 Defining_Identifier => Ref_Type,
7307 Type_Definition =>
7308 Make_Access_To_Object_Definition (Loc,
7309 All_Present => True,
7310 Subtype_Indication =>
7311 New_Reference_To (Etype (Function_Call), Loc)));
7312
7313 -- The access type and its accompanying object must be inserted after
7314 -- the object declaration in the constrained case, so that the function
7315 -- call can be passed access to the object. In the unconstrained case,
7316 -- the access type and object must be inserted before the object, since
7317 -- the object declaration is rewritten to be a renaming of a dereference
7318 -- of the access object.
7319
7320 if Is_Constrained (Underlying_Type (Result_Subt)) then
7321 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
7322 else
7323 Insert_Action (Object_Decl, Ptr_Typ_Decl);
7324 end if;
7325
7326 -- Finally, create an access object initialized to a reference to the
7327 -- function call.
7328
7329 New_Expr :=
7330 Make_Reference (Loc,
7331 Prefix => Relocate_Node (Func_Call));
7332
7333 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
7334 Set_Etype (Def_Id, Ref_Type);
7335
7336 Insert_After_And_Analyze (Ptr_Typ_Decl,
7337 Make_Object_Declaration (Loc,
7338 Defining_Identifier => Def_Id,
7339 Object_Definition => New_Reference_To (Ref_Type, Loc),
7340 Expression => New_Expr));
7341
7342 if Is_Constrained (Underlying_Type (Result_Subt)) then
7343 Set_Expression (Object_Decl, Empty);
7344 Set_No_Initialization (Object_Decl);
7345
7346 -- In case of an unconstrained result subtype, rewrite the object
7347 -- declaration as an object renaming where the renamed object is a
7348 -- dereference of <function_Call>'reference:
7349 --
7350 -- Obj : Subt renames <function_call>'Ref.all;
7351
7352 else
7353 Call_Deref :=
7354 Make_Explicit_Dereference (Loc,
7355 Prefix => New_Reference_To (Def_Id, Loc));
7356
7357 Loc := Sloc (Object_Decl);
7358 Rewrite (Object_Decl,
7359 Make_Object_Renaming_Declaration (Loc,
7360 Defining_Identifier => Make_Temporary (Loc, 'D'),
7361 Access_Definition => Empty,
7362 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7363 Name => Call_Deref));
7364
7365 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
7366
7367 Analyze (Object_Decl);
7368
7369 -- Replace the internal identifier of the renaming declaration's
7370 -- entity with identifier of the original object entity. We also have
7371 -- to exchange the entities containing their defining identifiers to
7372 -- ensure the correct replacement of the object declaration by the
7373 -- object renaming declaration to avoid homograph conflicts (since
7374 -- the object declaration's defining identifier was already entered
7375 -- in current scope). The Next_Entity links of the two entities also
7376 -- have to be swapped since the entities are part of the return
7377 -- scope's entity list and the list structure would otherwise be
7378 -- corrupted. Finally, the homonym chain must be preserved as well.
7379
7380 declare
7381 Renaming_Def_Id : constant Entity_Id :=
7382 Defining_Identifier (Object_Decl);
7383 Next_Entity_Temp : constant Entity_Id :=
7384 Next_Entity (Renaming_Def_Id);
7385 begin
7386 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
7387
7388 -- Swap next entity links in preparation for exchanging entities
7389
7390 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
7391 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
7392 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
7393
7394 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
7395
7396 -- Preserve source indication of original declaration, so that
7397 -- xref information is properly generated for the right entity.
7398
7399 Preserve_Comes_From_Source
7400 (Object_Decl, Original_Node (Object_Decl));
7401 Set_Comes_From_Source (Obj_Def_Id, True);
7402 Set_Comes_From_Source (Renaming_Def_Id, False);
7403 end;
7404 end if;
7405
7406 -- If the object entity has a class-wide Etype, then we need to change
7407 -- it to the result subtype of the function call, because otherwise the
7408 -- object will be class-wide without an explicit initialization and
7409 -- won't be allocated properly by the back end. It seems unclean to make
7410 -- such a revision to the type at this point, and we should try to
7411 -- improve this treatment when build-in-place functions with class-wide
7412 -- results are implemented. ???
7413
7414 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
7415 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
7416 end if;
7417 end Make_Build_In_Place_Call_In_Object_Declaration;
7418
7419 --------------------------
7420 -- Needs_BIP_Final_List --
7421 --------------------------
7422
7423 function Needs_BIP_Final_List (E : Entity_Id) return Boolean is
7424 pragma Assert (Is_Build_In_Place_Function (E));
7425 Result_Subt : constant Entity_Id := Underlying_Type (Etype (E));
7426
7427 begin
7428 -- We need the BIP_Final_List if the result type needs finalization. We
7429 -- also need it for tagged types, even if not class-wide, because some
7430 -- type extension might need finalization, and all overriding functions
7431 -- must have the same calling conventions. However, if there is a
7432 -- pragma Restrictions (No_Finalization), we never need this parameter.
7433
7434 return (Needs_Finalization (Result_Subt)
7435 or else Is_Tagged_Type (Underlying_Type (Result_Subt)))
7436 and then not Restriction_Active (No_Finalization);
7437 end Needs_BIP_Final_List;
7438
7439 end Exp_Ch6;