]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/exp_ch6.adb
[multiple changes]
[gcc.git] / gcc / ada / exp_ch6.adb
CommitLineData
01aef5ad 1------------------------------------------------------------------------------
70482933
RK
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- E X P _ C H 6 --
6-- --
7-- B o d y --
8-- --
3e2399ba 9-- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
70482933
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
70482933
RK
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 --
b5c84c3c
RD
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. --
70482933
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
70482933
RK
23-- --
24------------------------------------------------------------------------------
25
26with Atree; use Atree;
27with Checks; use Checks;
28with Debug; use Debug;
29with Einfo; use Einfo;
30with Errout; use Errout;
31with Elists; use Elists;
f937473f 32with Exp_Atag; use Exp_Atag;
70482933
RK
33with Exp_Ch2; use Exp_Ch2;
34with Exp_Ch3; use Exp_Ch3;
35with Exp_Ch7; use Exp_Ch7;
36with Exp_Ch9; use Exp_Ch9;
70482933
RK
37with Exp_Dbug; use Exp_Dbug;
38with Exp_Disp; use Exp_Disp;
39with Exp_Dist; use Exp_Dist;
40with Exp_Intr; use Exp_Intr;
41with Exp_Pakd; use Exp_Pakd;
42with Exp_Tss; use Exp_Tss;
43with Exp_Util; use Exp_Util;
c986420e 44with Exp_VFpt; use Exp_VFpt;
fbf5a39b 45with Fname; use Fname;
70482933 46with Freeze; use Freeze;
70482933
RK
47with Inline; use Inline;
48with Lib; use Lib;
7888a6ae 49with Namet; use Namet;
70482933
RK
50with Nlists; use Nlists;
51with Nmake; use Nmake;
52with Opt; use Opt;
53with Restrict; use Restrict;
6e937c1c 54with Rident; use Rident;
70482933
RK
55with Rtsfind; use Rtsfind;
56with Sem; use Sem;
a4100e55 57with Sem_Aux; use Sem_Aux;
70482933
RK
58with Sem_Ch6; use Sem_Ch6;
59with Sem_Ch8; use Sem_Ch8;
60with Sem_Ch12; use Sem_Ch12;
61with Sem_Ch13; use Sem_Ch13;
02822a92 62with Sem_Eval; use Sem_Eval;
70482933
RK
63with Sem_Disp; use Sem_Disp;
64with Sem_Dist; use Sem_Dist;
758c442c 65with Sem_Mech; use Sem_Mech;
70482933 66with Sem_Res; use Sem_Res;
d06b3b1d 67with Sem_SCIL; use Sem_SCIL;
70482933
RK
68with Sem_Util; use Sem_Util;
69with Sinfo; use Sinfo;
70with Snames; use Snames;
71with Stand; use Stand;
72with Tbuild; use Tbuild;
73with Uintp; use Uintp;
74with Validsw; use Validsw;
75
76package body Exp_Ch6 is
77
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
81
02822a92
RD
82 procedure Add_Access_Actual_To_Build_In_Place_Call
83 (Function_Call : Node_Id;
84 Function_Id : Entity_Id;
f937473f
RD
85 Return_Object : Node_Id;
86 Is_Access : Boolean := False);
02822a92
RD
87 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
88 -- object name given by Return_Object and add the attribute to the end of
89 -- the actual parameter list associated with the build-in-place function
f937473f
RD
90 -- call denoted by Function_Call. However, if Is_Access is True, then
91 -- Return_Object is already an access expression, in which case it's passed
92 -- along directly to the build-in-place function. Finally, if Return_Object
93 -- is empty, then pass a null literal as the actual.
94
95 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
96 (Function_Call : Node_Id;
97 Function_Id : Entity_Id;
98 Alloc_Form : BIP_Allocation_Form := Unspecified;
99 Alloc_Form_Exp : Node_Id := Empty);
100 -- Ada 2005 (AI-318-02): Add an actual indicating the form of allocation,
101 -- if any, to be done by a build-in-place function. If Alloc_Form_Exp is
102 -- present, then use it, otherwise pass a literal corresponding to the
103 -- Alloc_Form parameter (which must not be Unspecified in that case).
104
105 procedure Add_Extra_Actual_To_Call
106 (Subprogram_Call : Node_Id;
107 Extra_Formal : Entity_Id;
108 Extra_Actual : Node_Id);
109 -- Adds Extra_Actual as a named parameter association for the formal
110 -- Extra_Formal in Subprogram_Call.
111
112 procedure Add_Final_List_Actual_To_Build_In_Place_Call
113 (Function_Call : Node_Id;
7888a6ae 114 Function_Id : Entity_Id;
70f91180
RD
115 Acc_Type : Entity_Id;
116 Sel_Comp : Node_Id := Empty);
f937473f 117 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type has
7888a6ae
GD
118 -- controlled parts, add an actual parameter that is a pointer to
119 -- appropriate finalization list. The finalization list is that of the
120 -- current scope, except for "new Acc'(F(...))" in which case it's the
121 -- finalization list of the access type returned by the allocator. Acc_Type
70f91180
RD
122 -- is that type in the allocator case; Empty otherwise. If Sel_Comp is
123 -- not Empty, then it denotes a selected component and the finalization
124 -- list is obtained from the _controller list of the prefix object.
f937473f
RD
125
126 procedure Add_Task_Actuals_To_Build_In_Place_Call
127 (Function_Call : Node_Id;
128 Function_Id : Entity_Id;
129 Master_Actual : Node_Id);
130 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
131 -- contains tasks, add two actual parameters: the master, and a pointer to
132 -- the caller's activation chain. Master_Actual is the actual parameter
133 -- expression to pass for the master. In most cases, this is the current
134 -- master (_master). The two exceptions are: If the function call is the
135 -- initialization expression for an allocator, we pass the master of the
136 -- access type. If the function call is the initialization expression for
137 -- a return object, we pass along the master passed in by the caller. The
138 -- activation chain to pass is always the local one.
02822a92 139
70482933
RK
140 procedure Check_Overriding_Operation (Subp : Entity_Id);
141 -- Subp is a dispatching operation. Check whether it may override an
142 -- inherited private operation, in which case its DT entry is that of
143 -- the hidden operation, not the one it may have received earlier.
144 -- This must be done before emitting the code to set the corresponding
145 -- DT to the address of the subprogram. The actual placement of Subp in
146 -- the proper place in the list of primitive operations is done in
147 -- Declare_Inherited_Private_Subprograms, which also has to deal with
148 -- implicit operations. This duplication is unavoidable for now???
149
150 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
151 -- This procedure is called only if the subprogram body N, whose spec
152 -- has the given entity Spec, contains a parameterless recursive call.
153 -- It attempts to generate runtime code to detect if this a case of
154 -- infinite recursion.
155 --
156 -- The body is scanned to determine dependencies. If the only external
157 -- dependencies are on a small set of scalar variables, then the values
158 -- of these variables are captured on entry to the subprogram, and if
159 -- the values are not changed for the call, we know immediately that
160 -- we have an infinite recursion.
161
162 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id);
f4d379b8
HK
163 -- For each actual of an in-out or out parameter which is a numeric
164 -- (view) conversion of the form T (A), where A denotes a variable,
165 -- we insert the declaration:
70482933 166 --
f4d379b8 167 -- Temp : T[ := T (A)];
70482933
RK
168 --
169 -- prior to the call. Then we replace the actual with a reference to Temp,
170 -- and append the assignment:
171 --
fbf5a39b 172 -- A := TypeA (Temp);
70482933 173 --
1c6b973a
AC
174 -- after the call. Here TypeA is the actual type of variable A. For out
175 -- parameters, the initial declaration has no expression. If A is not an
176 -- entity name, we generate instead:
70482933 177 --
fbf5a39b 178 -- Var : TypeA renames A;
70482933
RK
179 -- Temp : T := Var; -- omitting expression for out parameter.
180 -- ...
fbf5a39b 181 -- Var := TypeA (Temp);
70482933
RK
182 --
183 -- For other in-out parameters, we emit the required constraint checks
184 -- before and/or after the call.
fbf5a39b 185 --
1c6b973a
AC
186 -- For all parameter modes, actuals that denote components and slices of
187 -- packed arrays are expanded into suitable temporaries.
f44fe430
RD
188 --
189 -- For non-scalar objects that are possibly unaligned, add call by copy
190 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
70482933
RK
191
192 procedure Expand_Inlined_Call
193 (N : Node_Id;
194 Subp : Entity_Id;
195 Orig_Subp : Entity_Id);
196 -- If called subprogram can be inlined by the front-end, retrieve the
197 -- analyzed body, replace formals with actuals and expand call in place.
198 -- Generate thunks for actuals that are expressions, and insert the
199 -- corresponding constant declarations before the call. If the original
200 -- call is to a derived operation, the return type is the one of the
201 -- derived operation, but the body is that of the original, so return
202 -- expressions in the body must be converted to the desired type (which
203 -- is simply not noted in the tree without inline expansion).
204
205 function Expand_Protected_Object_Reference
206 (N : Node_Id;
02822a92 207 Scop : Entity_Id) return Node_Id;
70482933
RK
208
209 procedure Expand_Protected_Subprogram_Call
210 (N : Node_Id;
211 Subp : Entity_Id;
212 Scop : Entity_Id);
213 -- A call to a protected subprogram within the protected object may appear
214 -- as a regular call. The list of actuals must be expanded to contain a
215 -- reference to the object itself, and the call becomes a call to the
216 -- corresponding protected subprogram.
217
8dbf3473
AC
218 function Is_Null_Procedure (Subp : Entity_Id) return Boolean;
219 -- Predicate to recognize stubbed procedures and null procedures, which
220 -- can be inlined unconditionally in all cases.
221
02822a92
RD
222 ----------------------------------------------
223 -- Add_Access_Actual_To_Build_In_Place_Call --
224 ----------------------------------------------
225
226 procedure Add_Access_Actual_To_Build_In_Place_Call
227 (Function_Call : Node_Id;
228 Function_Id : Entity_Id;
f937473f
RD
229 Return_Object : Node_Id;
230 Is_Access : Boolean := False)
02822a92
RD
231 is
232 Loc : constant Source_Ptr := Sloc (Function_Call);
233 Obj_Address : Node_Id;
f937473f 234 Obj_Acc_Formal : Entity_Id;
02822a92
RD
235
236 begin
f937473f 237 -- Locate the implicit access parameter in the called function
02822a92 238
f937473f 239 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
02822a92 240
f937473f
RD
241 -- If no return object is provided, then pass null
242
243 if not Present (Return_Object) then
244 Obj_Address := Make_Null (Loc);
7888a6ae 245 Set_Parent (Obj_Address, Function_Call);
02822a92 246
f937473f
RD
247 -- If Return_Object is already an expression of an access type, then use
248 -- it directly, since it must be an access value denoting the return
249 -- object, and couldn't possibly be the return object itself.
250
251 elsif Is_Access then
252 Obj_Address := Return_Object;
7888a6ae 253 Set_Parent (Obj_Address, Function_Call);
02822a92
RD
254
255 -- Apply Unrestricted_Access to caller's return object
256
f937473f
RD
257 else
258 Obj_Address :=
259 Make_Attribute_Reference (Loc,
260 Prefix => Return_Object,
261 Attribute_Name => Name_Unrestricted_Access);
7888a6ae
GD
262
263 Set_Parent (Return_Object, Obj_Address);
264 Set_Parent (Obj_Address, Function_Call);
f937473f 265 end if;
02822a92
RD
266
267 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
268
269 -- Build the parameter association for the new actual and add it to the
270 -- end of the function's actuals.
271
f937473f
RD
272 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
273 end Add_Access_Actual_To_Build_In_Place_Call;
274
275 --------------------------------------------------
276 -- Add_Alloc_Form_Actual_To_Build_In_Place_Call --
277 --------------------------------------------------
278
279 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
280 (Function_Call : Node_Id;
281 Function_Id : Entity_Id;
282 Alloc_Form : BIP_Allocation_Form := Unspecified;
283 Alloc_Form_Exp : Node_Id := Empty)
284 is
285 Loc : constant Source_Ptr := Sloc (Function_Call);
286 Alloc_Form_Actual : Node_Id;
287 Alloc_Form_Formal : Node_Id;
288
289 begin
7888a6ae
GD
290 -- The allocation form generally doesn't need to be passed in the case
291 -- of a constrained result subtype, since normally the caller performs
292 -- the allocation in that case. However this formal is still needed in
293 -- the case where the function has a tagged result, because generally
294 -- such functions can be called in a dispatching context and such calls
295 -- must be handled like calls to class-wide functions.
296
297 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
298 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
299 then
300 return;
301 end if;
302
f937473f
RD
303 -- Locate the implicit allocation form parameter in the called function.
304 -- Maybe it would be better for each implicit formal of a build-in-place
305 -- function to have a flag or a Uint attribute to identify it. ???
306
307 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
308
309 if Present (Alloc_Form_Exp) then
310 pragma Assert (Alloc_Form = Unspecified);
311
312 Alloc_Form_Actual := Alloc_Form_Exp;
313
314 else
315 pragma Assert (Alloc_Form /= Unspecified);
316
317 Alloc_Form_Actual :=
318 Make_Integer_Literal (Loc,
319 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
320 end if;
321
322 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
323
324 -- Build the parameter association for the new actual and add it to the
325 -- end of the function's actuals.
326
327 Add_Extra_Actual_To_Call
328 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
329 end Add_Alloc_Form_Actual_To_Build_In_Place_Call;
330
331 ------------------------------
332 -- Add_Extra_Actual_To_Call --
333 ------------------------------
334
335 procedure Add_Extra_Actual_To_Call
336 (Subprogram_Call : Node_Id;
337 Extra_Formal : Entity_Id;
338 Extra_Actual : Node_Id)
339 is
340 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
341 Param_Assoc : Node_Id;
342
343 begin
02822a92
RD
344 Param_Assoc :=
345 Make_Parameter_Association (Loc,
f937473f
RD
346 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
347 Explicit_Actual_Parameter => Extra_Actual);
02822a92 348
f937473f
RD
349 Set_Parent (Param_Assoc, Subprogram_Call);
350 Set_Parent (Extra_Actual, Param_Assoc);
02822a92 351
f937473f
RD
352 if Present (Parameter_Associations (Subprogram_Call)) then
353 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
02822a92
RD
354 N_Parameter_Association
355 then
f937473f
RD
356
357 -- Find last named actual, and append
358
359 declare
360 L : Node_Id;
361 begin
362 L := First_Actual (Subprogram_Call);
363 while Present (L) loop
364 if No (Next_Actual (L)) then
365 Set_Next_Named_Actual (Parent (L), Extra_Actual);
366 exit;
367 end if;
368 Next_Actual (L);
369 end loop;
370 end;
371
02822a92 372 else
f937473f 373 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
02822a92
RD
374 end if;
375
f937473f 376 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
02822a92
RD
377
378 else
f937473f
RD
379 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
380 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
02822a92 381 end if;
f937473f
RD
382 end Add_Extra_Actual_To_Call;
383
384 --------------------------------------------------
385 -- Add_Final_List_Actual_To_Build_In_Place_Call --
386 --------------------------------------------------
387
388 procedure Add_Final_List_Actual_To_Build_In_Place_Call
389 (Function_Call : Node_Id;
7888a6ae 390 Function_Id : Entity_Id;
70f91180
RD
391 Acc_Type : Entity_Id;
392 Sel_Comp : Node_Id := Empty)
f937473f
RD
393 is
394 Loc : constant Source_Ptr := Sloc (Function_Call);
395 Final_List : Node_Id;
396 Final_List_Actual : Node_Id;
397 Final_List_Formal : Node_Id;
70f91180 398 Is_Ctrl_Result : constant Boolean :=
048e5cef 399 Needs_Finalization
70f91180 400 (Underlying_Type (Etype (Function_Id)));
f937473f
RD
401
402 begin
7888a6ae 403 -- No such extra parameter is needed if there are no controlled parts.
048e5cef
BD
404 -- The test for Needs_Finalization accounts for class-wide results
405 -- (which potentially have controlled parts, even if the root type
406 -- doesn't), and the test for a tagged result type is needed because
407 -- calls to such a function can in general occur in dispatching
408 -- contexts, which must be treated the same as a call to class-wide
409 -- functions. Both of these situations require that a finalization list
410 -- be passed.
411
412 if not Needs_BIP_Final_List (Function_Id) then
f937473f
RD
413 return;
414 end if;
415
416 -- Locate implicit finalization list parameter in the called function
417
418 Final_List_Formal := Build_In_Place_Formal (Function_Id, BIP_Final_List);
419
7888a6ae
GD
420 -- Create the actual which is a pointer to the appropriate finalization
421 -- list. Acc_Type is present if and only if this call is the
1c6b973a
AC
422 -- initialization of an allocator. Use the Current_Scope or the
423 -- Acc_Type as appropriate.
7888a6ae
GD
424
425 if Present (Acc_Type)
426 and then (Ekind (Acc_Type) = E_Anonymous_Access_Type
427 or else
428 Present (Associated_Final_Chain (Base_Type (Acc_Type))))
429 then
430 Final_List := Find_Final_List (Acc_Type);
70f91180
RD
431
432 -- If Sel_Comp is present and the function result is controlled, then
433 -- the finalization list will be obtained from the _controller list of
434 -- the selected component's prefix object.
435
436 elsif Present (Sel_Comp) and then Is_Ctrl_Result then
437 Final_List := Find_Final_List (Current_Scope, Sel_Comp);
438
7888a6ae
GD
439 else
440 Final_List := Find_Final_List (Current_Scope);
441 end if;
f937473f 442
f937473f
RD
443 Final_List_Actual :=
444 Make_Attribute_Reference (Loc,
445 Prefix => Final_List,
446 Attribute_Name => Name_Unrestricted_Access);
447
448 Analyze_And_Resolve (Final_List_Actual, Etype (Final_List_Formal));
449
450 -- Build the parameter association for the new actual and add it to the
451 -- end of the function's actuals.
452
453 Add_Extra_Actual_To_Call
454 (Function_Call, Final_List_Formal, Final_List_Actual);
455 end Add_Final_List_Actual_To_Build_In_Place_Call;
456
457 ---------------------------------------------
458 -- Add_Task_Actuals_To_Build_In_Place_Call --
459 ---------------------------------------------
460
461 procedure Add_Task_Actuals_To_Build_In_Place_Call
462 (Function_Call : Node_Id;
463 Function_Id : Entity_Id;
464 Master_Actual : Node_Id)
465 -- Note: Master_Actual can be Empty, but only if there are no tasks
466 is
467 Loc : constant Source_Ptr := Sloc (Function_Call);
468
469 begin
470 -- No such extra parameters are needed if there are no tasks
471
472 if not Has_Task (Etype (Function_Id)) then
473 return;
474 end if;
475
476 -- The master
477
478 declare
479 Master_Formal : Node_Id;
480 begin
481 -- Locate implicit master parameter in the called function
482
483 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Master);
484
485 Analyze_And_Resolve (Master_Actual, Etype (Master_Formal));
486
487 -- Build the parameter association for the new actual and add it to
488 -- the end of the function's actuals.
489
490 Add_Extra_Actual_To_Call
491 (Function_Call, Master_Formal, Master_Actual);
492 end;
493
494 -- The activation chain
495
496 declare
497 Activation_Chain_Actual : Node_Id;
498 Activation_Chain_Formal : Node_Id;
75a64833 499
f937473f
RD
500 begin
501 -- Locate implicit activation chain parameter in the called function
502
503 Activation_Chain_Formal := Build_In_Place_Formal
504 (Function_Id, BIP_Activation_Chain);
505
506 -- Create the actual which is a pointer to the current activation
507 -- chain
508
509 Activation_Chain_Actual :=
510 Make_Attribute_Reference (Loc,
511 Prefix => Make_Identifier (Loc, Name_uChain),
512 Attribute_Name => Name_Unrestricted_Access);
513
514 Analyze_And_Resolve
515 (Activation_Chain_Actual, Etype (Activation_Chain_Formal));
516
517 -- Build the parameter association for the new actual and add it to
518 -- the end of the function's actuals.
519
520 Add_Extra_Actual_To_Call
521 (Function_Call, Activation_Chain_Formal, Activation_Chain_Actual);
522 end;
523 end Add_Task_Actuals_To_Build_In_Place_Call;
524
525 -----------------------
526 -- BIP_Formal_Suffix --
527 -----------------------
528
529 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
530 begin
531 case Kind is
532 when BIP_Alloc_Form =>
533 return "BIPalloc";
534 when BIP_Final_List =>
535 return "BIPfinallist";
536 when BIP_Master =>
537 return "BIPmaster";
538 when BIP_Activation_Chain =>
539 return "BIPactivationchain";
540 when BIP_Object_Access =>
541 return "BIPaccess";
542 end case;
543 end BIP_Formal_Suffix;
544
545 ---------------------------
546 -- Build_In_Place_Formal --
547 ---------------------------
548
549 function Build_In_Place_Formal
550 (Func : Entity_Id;
551 Kind : BIP_Formal_Kind) return Entity_Id
552 is
553 Extra_Formal : Entity_Id := Extra_Formals (Func);
554
555 begin
556 -- Maybe it would be better for each implicit formal of a build-in-place
557 -- function to have a flag or a Uint attribute to identify it. ???
558
559 loop
19590d70 560 pragma Assert (Present (Extra_Formal));
f937473f
RD
561 exit when
562 Chars (Extra_Formal) =
563 New_External_Name (Chars (Func), BIP_Formal_Suffix (Kind));
564 Next_Formal_With_Extras (Extra_Formal);
565 end loop;
566
f937473f
RD
567 return Extra_Formal;
568 end Build_In_Place_Formal;
02822a92 569
c9a4817d
RD
570 --------------------------------
571 -- Check_Overriding_Operation --
572 --------------------------------
70482933
RK
573
574 procedure Check_Overriding_Operation (Subp : Entity_Id) is
575 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
576 Op_List : constant Elist_Id := Primitive_Operations (Typ);
577 Op_Elmt : Elmt_Id;
578 Prim_Op : Entity_Id;
579 Par_Op : Entity_Id;
580
581 begin
582 if Is_Derived_Type (Typ)
583 and then not Is_Private_Type (Typ)
584 and then In_Open_Scopes (Scope (Etype (Typ)))
585 and then Typ = Base_Type (Typ)
586 then
2f1b20a9
ES
587 -- Subp overrides an inherited private operation if there is an
588 -- inherited operation with a different name than Subp (see
589 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
590 -- same name as Subp.
70482933
RK
591
592 Op_Elmt := First_Elmt (Op_List);
593 while Present (Op_Elmt) loop
594 Prim_Op := Node (Op_Elmt);
595 Par_Op := Alias (Prim_Op);
596
597 if Present (Par_Op)
598 and then not Comes_From_Source (Prim_Op)
599 and then Chars (Prim_Op) /= Chars (Par_Op)
600 and then Chars (Par_Op) = Chars (Subp)
601 and then Is_Hidden (Par_Op)
602 and then Type_Conformant (Prim_Op, Subp)
603 then
604 Set_DT_Position (Subp, DT_Position (Prim_Op));
605 end if;
606
607 Next_Elmt (Op_Elmt);
608 end loop;
609 end if;
610 end Check_Overriding_Operation;
611
612 -------------------------------
613 -- Detect_Infinite_Recursion --
614 -------------------------------
615
616 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
617 Loc : constant Source_Ptr := Sloc (N);
618
fbf5a39b 619 Var_List : constant Elist_Id := New_Elmt_List;
70482933
RK
620 -- List of globals referenced by body of procedure
621
fbf5a39b 622 Call_List : constant Elist_Id := New_Elmt_List;
70482933
RK
623 -- List of recursive calls in body of procedure
624
fbf5a39b 625 Shad_List : constant Elist_Id := New_Elmt_List;
2f1b20a9
ES
626 -- List of entity id's for entities created to capture the value of
627 -- referenced globals on entry to the procedure.
70482933
RK
628
629 Scop : constant Uint := Scope_Depth (Spec);
2f1b20a9
ES
630 -- This is used to record the scope depth of the current procedure, so
631 -- that we can identify global references.
70482933
RK
632
633 Max_Vars : constant := 4;
634 -- Do not test more than four global variables
635
636 Count_Vars : Natural := 0;
637 -- Count variables found so far
638
639 Var : Entity_Id;
640 Elm : Elmt_Id;
641 Ent : Entity_Id;
642 Call : Elmt_Id;
643 Decl : Node_Id;
644 Test : Node_Id;
645 Elm1 : Elmt_Id;
646 Elm2 : Elmt_Id;
647 Last : Node_Id;
648
649 function Process (Nod : Node_Id) return Traverse_Result;
650 -- Function to traverse the subprogram body (using Traverse_Func)
651
652 -------------
653 -- Process --
654 -------------
655
656 function Process (Nod : Node_Id) return Traverse_Result is
657 begin
658 -- Procedure call
659
660 if Nkind (Nod) = N_Procedure_Call_Statement then
661
662 -- Case of one of the detected recursive calls
663
664 if Is_Entity_Name (Name (Nod))
665 and then Has_Recursive_Call (Entity (Name (Nod)))
666 and then Entity (Name (Nod)) = Spec
667 then
668 Append_Elmt (Nod, Call_List);
669 return Skip;
670
671 -- Any other procedure call may have side effects
672
673 else
674 return Abandon;
675 end if;
676
677 -- A call to a pure function can always be ignored
678
679 elsif Nkind (Nod) = N_Function_Call
680 and then Is_Entity_Name (Name (Nod))
681 and then Is_Pure (Entity (Name (Nod)))
682 then
683 return Skip;
684
685 -- Case of an identifier reference
686
687 elsif Nkind (Nod) = N_Identifier then
688 Ent := Entity (Nod);
689
690 -- If no entity, then ignore the reference
691
692 -- Not clear why this can happen. To investigate, remove this
693 -- test and look at the crash that occurs here in 3401-004 ???
694
695 if No (Ent) then
696 return Skip;
697
698 -- Ignore entities with no Scope, again not clear how this
699 -- can happen, to investigate, look at 4108-008 ???
700
701 elsif No (Scope (Ent)) then
702 return Skip;
703
704 -- Ignore the reference if not to a more global object
705
706 elsif Scope_Depth (Scope (Ent)) >= Scop then
707 return Skip;
708
709 -- References to types, exceptions and constants are always OK
710
711 elsif Is_Type (Ent)
712 or else Ekind (Ent) = E_Exception
713 or else Ekind (Ent) = E_Constant
714 then
715 return Skip;
716
717 -- If other than a non-volatile scalar variable, we have some
718 -- kind of global reference (e.g. to a function) that we cannot
719 -- deal with so we forget the attempt.
720
721 elsif Ekind (Ent) /= E_Variable
722 or else not Is_Scalar_Type (Etype (Ent))
fbf5a39b 723 or else Treat_As_Volatile (Ent)
70482933
RK
724 then
725 return Abandon;
726
727 -- Otherwise we have a reference to a global scalar
728
729 else
730 -- Loop through global entities already detected
731
732 Elm := First_Elmt (Var_List);
733 loop
734 -- If not detected before, record this new global reference
735
736 if No (Elm) then
737 Count_Vars := Count_Vars + 1;
738
739 if Count_Vars <= Max_Vars then
740 Append_Elmt (Entity (Nod), Var_List);
741 else
742 return Abandon;
743 end if;
744
745 exit;
746
747 -- If recorded before, ignore
748
749 elsif Node (Elm) = Entity (Nod) then
750 return Skip;
751
752 -- Otherwise keep looking
753
754 else
755 Next_Elmt (Elm);
756 end if;
757 end loop;
758
759 return Skip;
760 end if;
761
762 -- For all other node kinds, recursively visit syntactic children
763
764 else
765 return OK;
766 end if;
767 end Process;
768
02822a92 769 function Traverse_Body is new Traverse_Func (Process);
70482933
RK
770
771 -- Start of processing for Detect_Infinite_Recursion
772
773 begin
2f1b20a9
ES
774 -- Do not attempt detection in No_Implicit_Conditional mode, since we
775 -- won't be able to generate the code to handle the recursion in any
776 -- case.
70482933 777
6e937c1c 778 if Restriction_Active (No_Implicit_Conditionals) then
70482933
RK
779 return;
780 end if;
781
782 -- Otherwise do traversal and quit if we get abandon signal
783
784 if Traverse_Body (N) = Abandon then
785 return;
786
2f1b20a9
ES
787 -- We must have a call, since Has_Recursive_Call was set. If not just
788 -- ignore (this is only an error check, so if we have a funny situation,
789 -- due to bugs or errors, we do not want to bomb!)
70482933
RK
790
791 elsif Is_Empty_Elmt_List (Call_List) then
792 return;
793 end if;
794
795 -- Here is the case where we detect recursion at compile time
796
2f1b20a9
ES
797 -- Push our current scope for analyzing the declarations and code that
798 -- we will insert for the checking.
70482933 799
7888a6ae 800 Push_Scope (Spec);
70482933 801
2f1b20a9
ES
802 -- This loop builds temporary variables for each of the referenced
803 -- globals, so that at the end of the loop the list Shad_List contains
804 -- these temporaries in one-to-one correspondence with the elements in
805 -- Var_List.
70482933
RK
806
807 Last := Empty;
808 Elm := First_Elmt (Var_List);
809 while Present (Elm) loop
810 Var := Node (Elm);
c12beea0 811 Ent := Make_Temporary (Loc, 'S');
70482933
RK
812 Append_Elmt (Ent, Shad_List);
813
2f1b20a9
ES
814 -- Insert a declaration for this temporary at the start of the
815 -- declarations for the procedure. The temporaries are declared as
816 -- constant objects initialized to the current values of the
817 -- corresponding temporaries.
70482933
RK
818
819 Decl :=
820 Make_Object_Declaration (Loc,
821 Defining_Identifier => Ent,
822 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
823 Constant_Present => True,
824 Expression => New_Occurrence_Of (Var, Loc));
825
826 if No (Last) then
827 Prepend (Decl, Declarations (N));
828 else
829 Insert_After (Last, Decl);
830 end if;
831
832 Last := Decl;
833 Analyze (Decl);
834 Next_Elmt (Elm);
835 end loop;
836
837 -- Loop through calls
838
839 Call := First_Elmt (Call_List);
840 while Present (Call) loop
841
842 -- Build a predicate expression of the form
843
844 -- True
845 -- and then global1 = temp1
846 -- and then global2 = temp2
847 -- ...
848
849 -- This predicate determines if any of the global values
850 -- referenced by the procedure have changed since the
851 -- current call, if not an infinite recursion is assured.
852
853 Test := New_Occurrence_Of (Standard_True, Loc);
854
855 Elm1 := First_Elmt (Var_List);
856 Elm2 := First_Elmt (Shad_List);
857 while Present (Elm1) loop
858 Test :=
859 Make_And_Then (Loc,
860 Left_Opnd => Test,
861 Right_Opnd =>
862 Make_Op_Eq (Loc,
863 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
864 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
865
866 Next_Elmt (Elm1);
867 Next_Elmt (Elm2);
868 end loop;
869
870 -- Now we replace the call with the sequence
871
872 -- if no-changes (see above) then
873 -- raise Storage_Error;
874 -- else
875 -- original-call
876 -- end if;
877
878 Rewrite (Node (Call),
879 Make_If_Statement (Loc,
880 Condition => Test,
881 Then_Statements => New_List (
07fc65c4
GB
882 Make_Raise_Storage_Error (Loc,
883 Reason => SE_Infinite_Recursion)),
70482933
RK
884
885 Else_Statements => New_List (
886 Relocate_Node (Node (Call)))));
887
888 Analyze (Node (Call));
889
890 Next_Elmt (Call);
891 end loop;
892
893 -- Remove temporary scope stack entry used for analysis
894
895 Pop_Scope;
896 end Detect_Infinite_Recursion;
897
898 --------------------
899 -- Expand_Actuals --
900 --------------------
901
902 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
903 Loc : constant Source_Ptr := Sloc (N);
904 Actual : Node_Id;
905 Formal : Entity_Id;
906 N_Node : Node_Id;
907 Post_Call : List_Id;
908 E_Formal : Entity_Id;
909
910 procedure Add_Call_By_Copy_Code;
fbf5a39b
AC
911 -- For cases where the parameter must be passed by copy, this routine
912 -- generates a temporary variable into which the actual is copied and
913 -- then passes this as the parameter. For an OUT or IN OUT parameter,
914 -- an assignment is also generated to copy the result back. The call
915 -- also takes care of any constraint checks required for the type
916 -- conversion case (on both the way in and the way out).
70482933 917
f44fe430
RD
918 procedure Add_Simple_Call_By_Copy_Code;
919 -- This is similar to the above, but is used in cases where we know
920 -- that all that is needed is to simply create a temporary and copy
921 -- the value in and out of the temporary.
70482933
RK
922
923 procedure Check_Fortran_Logical;
924 -- A value of type Logical that is passed through a formal parameter
925 -- must be normalized because .TRUE. usually does not have the same
926 -- representation as True. We assume that .FALSE. = False = 0.
927 -- What about functions that return a logical type ???
928
758c442c
GD
929 function Is_Legal_Copy return Boolean;
930 -- Check that an actual can be copied before generating the temporary
931 -- to be used in the call. If the actual is of a by_reference type then
932 -- the program is illegal (this can only happen in the presence of
933 -- rep. clauses that force an incorrect alignment). If the formal is
934 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
935 -- the effect that this might lead to unaligned arguments.
936
70482933
RK
937 function Make_Var (Actual : Node_Id) return Entity_Id;
938 -- Returns an entity that refers to the given actual parameter,
939 -- Actual (not including any type conversion). If Actual is an
940 -- entity name, then this entity is returned unchanged, otherwise
941 -- a renaming is created to provide an entity for the actual.
942
943 procedure Reset_Packed_Prefix;
944 -- The expansion of a packed array component reference is delayed in
945 -- the context of a call. Now we need to complete the expansion, so we
946 -- unmark the analyzed bits in all prefixes.
947
948 ---------------------------
949 -- Add_Call_By_Copy_Code --
950 ---------------------------
951
952 procedure Add_Call_By_Copy_Code is
cc335f43
AC
953 Expr : Node_Id;
954 Init : Node_Id;
955 Temp : Entity_Id;
f44fe430 956 Indic : Node_Id;
cc335f43 957 Var : Entity_Id;
0da2c8ac 958 F_Typ : constant Entity_Id := Etype (Formal);
cc335f43
AC
959 V_Typ : Entity_Id;
960 Crep : Boolean;
70482933
RK
961
962 begin
758c442c
GD
963 if not Is_Legal_Copy then
964 return;
965 end if;
966
b086849e 967 Temp := Make_Temporary (Loc, 'T', Actual);
70482933 968
f44fe430
RD
969 -- Use formal type for temp, unless formal type is an unconstrained
970 -- array, in which case we don't have to worry about bounds checks,
758c442c 971 -- and we use the actual type, since that has appropriate bounds.
f44fe430
RD
972
973 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
974 Indic := New_Occurrence_Of (Etype (Actual), Loc);
975 else
976 Indic := New_Occurrence_Of (Etype (Formal), Loc);
977 end if;
978
70482933
RK
979 if Nkind (Actual) = N_Type_Conversion then
980 V_Typ := Etype (Expression (Actual));
19f0526a
AC
981
982 -- If the formal is an (in-)out parameter, capture the name
983 -- of the variable in order to build the post-call assignment.
81a5b587
AC
984
985 Var := Make_Var (Expression (Actual));
19f0526a 986
08aa9a4a 987 Crep := not Same_Representation
0da2c8ac 988 (F_Typ, Etype (Expression (Actual)));
08aa9a4a 989
70482933
RK
990 else
991 V_Typ := Etype (Actual);
992 Var := Make_Var (Actual);
993 Crep := False;
994 end if;
995
996 -- Setup initialization for case of in out parameter, or an out
997 -- parameter where the formal is an unconstrained array (in the
998 -- latter case, we have to pass in an object with bounds).
999
cc335f43
AC
1000 -- If this is an out parameter, the initial copy is wasteful, so as
1001 -- an optimization for the one-dimensional case we extract the
1002 -- bounds of the actual and build an uninitialized temporary of the
1003 -- right size.
1004
70482933 1005 if Ekind (Formal) = E_In_Out_Parameter
0da2c8ac 1006 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
70482933
RK
1007 then
1008 if Nkind (Actual) = N_Type_Conversion then
1009 if Conversion_OK (Actual) then
0da2c8ac 1010 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
70482933 1011 else
0da2c8ac 1012 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
70482933 1013 end if;
cc335f43
AC
1014
1015 elsif Ekind (Formal) = E_Out_Parameter
0da2c8ac
AC
1016 and then Is_Array_Type (F_Typ)
1017 and then Number_Dimensions (F_Typ) = 1
1018 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
cc335f43
AC
1019 then
1020 -- Actual is a one-dimensional array or slice, and the type
1021 -- requires no initialization. Create a temporary of the
f44fe430 1022 -- right size, but do not copy actual into it (optimization).
cc335f43
AC
1023
1024 Init := Empty;
1025 Indic :=
1026 Make_Subtype_Indication (Loc,
1027 Subtype_Mark =>
0da2c8ac 1028 New_Occurrence_Of (F_Typ, Loc),
cc335f43
AC
1029 Constraint =>
1030 Make_Index_Or_Discriminant_Constraint (Loc,
1031 Constraints => New_List (
1032 Make_Range (Loc,
1033 Low_Bound =>
1034 Make_Attribute_Reference (Loc,
1035 Prefix => New_Occurrence_Of (Var, Loc),
70f91180 1036 Attribute_Name => Name_First),
cc335f43
AC
1037 High_Bound =>
1038 Make_Attribute_Reference (Loc,
1039 Prefix => New_Occurrence_Of (Var, Loc),
1040 Attribute_Name => Name_Last)))));
1041
70482933
RK
1042 else
1043 Init := New_Occurrence_Of (Var, Loc);
1044 end if;
1045
1046 -- An initialization is created for packed conversions as
1047 -- actuals for out parameters to enable Make_Object_Declaration
1048 -- to determine the proper subtype for N_Node. Note that this
1049 -- is wasteful because the extra copying on the call side is
1050 -- not required for such out parameters. ???
1051
1052 elsif Ekind (Formal) = E_Out_Parameter
1053 and then Nkind (Actual) = N_Type_Conversion
0da2c8ac 1054 and then (Is_Bit_Packed_Array (F_Typ)
70482933
RK
1055 or else
1056 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1057 then
1058 if Conversion_OK (Actual) then
f44fe430 1059 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
70482933 1060 else
f44fe430 1061 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
70482933 1062 end if;
2e071734
AC
1063
1064 elsif Ekind (Formal) = E_In_Parameter then
02822a92
RD
1065
1066 -- Handle the case in which the actual is a type conversion
1067
1068 if Nkind (Actual) = N_Type_Conversion then
1069 if Conversion_OK (Actual) then
1070 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1071 else
1072 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1073 end if;
1074 else
1075 Init := New_Occurrence_Of (Var, Loc);
1076 end if;
2e071734 1077
70482933
RK
1078 else
1079 Init := Empty;
1080 end if;
1081
1082 N_Node :=
1083 Make_Object_Declaration (Loc,
1084 Defining_Identifier => Temp,
cc335f43 1085 Object_Definition => Indic,
f44fe430 1086 Expression => Init);
70482933
RK
1087 Set_Assignment_OK (N_Node);
1088 Insert_Action (N, N_Node);
1089
1090 -- Now, normally the deal here is that we use the defining
1091 -- identifier created by that object declaration. There is
1092 -- one exception to this. In the change of representation case
1093 -- the above declaration will end up looking like:
1094
1095 -- temp : type := identifier;
1096
1097 -- And in this case we might as well use the identifier directly
1098 -- and eliminate the temporary. Note that the analysis of the
1099 -- declaration was not a waste of time in that case, since it is
1100 -- what generated the necessary change of representation code. If
1101 -- the change of representation introduced additional code, as in
1102 -- a fixed-integer conversion, the expression is not an identifier
1103 -- and must be kept.
1104
1105 if Crep
1106 and then Present (Expression (N_Node))
1107 and then Is_Entity_Name (Expression (N_Node))
1108 then
1109 Temp := Entity (Expression (N_Node));
1110 Rewrite (N_Node, Make_Null_Statement (Loc));
1111 end if;
1112
fbf5a39b 1113 -- For IN parameter, all we do is to replace the actual
70482933 1114
fbf5a39b
AC
1115 if Ekind (Formal) = E_In_Parameter then
1116 Rewrite (Actual, New_Reference_To (Temp, Loc));
1117 Analyze (Actual);
1118
1119 -- Processing for OUT or IN OUT parameter
1120
1121 else
c8ef728f
ES
1122 -- Kill current value indications for the temporary variable we
1123 -- created, since we just passed it as an OUT parameter.
1124
1125 Kill_Current_Values (Temp);
75ba322d 1126 Set_Is_Known_Valid (Temp, False);
c8ef728f 1127
fbf5a39b
AC
1128 -- If type conversion, use reverse conversion on exit
1129
1130 if Nkind (Actual) = N_Type_Conversion then
1131 if Conversion_OK (Actual) then
1132 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1133 else
1134 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1135 end if;
70482933 1136 else
fbf5a39b 1137 Expr := New_Occurrence_Of (Temp, Loc);
70482933 1138 end if;
70482933 1139
fbf5a39b
AC
1140 Rewrite (Actual, New_Reference_To (Temp, Loc));
1141 Analyze (Actual);
70482933 1142
d766cee3
RD
1143 -- If the actual is a conversion of a packed reference, it may
1144 -- already have been expanded by Remove_Side_Effects, and the
1145 -- resulting variable is a temporary which does not designate
1146 -- the proper out-parameter, which may not be addressable. In
1147 -- that case, generate an assignment to the original expression
b0159fbe 1148 -- (before expansion of the packed reference) so that the proper
d766cee3 1149 -- expansion of assignment to a packed component can take place.
70482933 1150
d766cee3
RD
1151 declare
1152 Obj : Node_Id;
1153 Lhs : Node_Id;
1154
1155 begin
1156 if Is_Renaming_Of_Object (Var)
1157 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1158 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1159 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1160 = N_Indexed_Component
1161 and then
1162 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1163 then
1164 Obj := Renamed_Object (Var);
1165 Lhs :=
1166 Make_Selected_Component (Loc,
1167 Prefix =>
1168 New_Copy_Tree (Original_Node (Prefix (Obj))),
1169 Selector_Name => New_Copy (Selector_Name (Obj)));
1170 Reset_Analyzed_Flags (Lhs);
1171
1172 else
1173 Lhs := New_Occurrence_Of (Var, Loc);
1174 end if;
1175
1176 Set_Assignment_OK (Lhs);
1177
1178 Append_To (Post_Call,
1179 Make_Assignment_Statement (Loc,
1180 Name => Lhs,
1181 Expression => Expr));
1182 end;
fbf5a39b 1183 end if;
70482933
RK
1184 end Add_Call_By_Copy_Code;
1185
1186 ----------------------------------
f44fe430 1187 -- Add_Simple_Call_By_Copy_Code --
70482933
RK
1188 ----------------------------------
1189
f44fe430 1190 procedure Add_Simple_Call_By_Copy_Code is
70482933 1191 Temp : Entity_Id;
758c442c 1192 Decl : Node_Id;
70482933
RK
1193 Incod : Node_Id;
1194 Outcod : Node_Id;
1195 Lhs : Node_Id;
1196 Rhs : Node_Id;
f44fe430
RD
1197 Indic : Node_Id;
1198 F_Typ : constant Entity_Id := Etype (Formal);
70482933
RK
1199
1200 begin
758c442c
GD
1201 if not Is_Legal_Copy then
1202 return;
1203 end if;
1204
f44fe430
RD
1205 -- Use formal type for temp, unless formal type is an unconstrained
1206 -- array, in which case we don't have to worry about bounds checks,
758c442c 1207 -- and we use the actual type, since that has appropriate bounds.
f44fe430
RD
1208
1209 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1210 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1211 else
1212 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1213 end if;
70482933
RK
1214
1215 -- Prepare to generate code
1216
f44fe430
RD
1217 Reset_Packed_Prefix;
1218
b086849e 1219 Temp := Make_Temporary (Loc, 'T', Actual);
70482933
RK
1220 Incod := Relocate_Node (Actual);
1221 Outcod := New_Copy_Tree (Incod);
1222
1223 -- Generate declaration of temporary variable, initializing it
c73ae90f 1224 -- with the input parameter unless we have an OUT formal or
758c442c 1225 -- this is an initialization call.
70482933 1226
c73ae90f
GD
1227 -- If the formal is an out parameter with discriminants, the
1228 -- discriminants must be captured even if the rest of the object
1229 -- is in principle uninitialized, because the discriminants may
1230 -- be read by the called subprogram.
1231
70482933
RK
1232 if Ekind (Formal) = E_Out_Parameter then
1233 Incod := Empty;
758c442c 1234
c73ae90f
GD
1235 if Has_Discriminants (Etype (Formal)) then
1236 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1237 end if;
1238
758c442c 1239 elsif Inside_Init_Proc then
c73ae90f
GD
1240
1241 -- Could use a comment here to match comment below ???
1242
758c442c
GD
1243 if Nkind (Actual) /= N_Selected_Component
1244 or else
1245 not Has_Discriminant_Dependent_Constraint
1246 (Entity (Selector_Name (Actual)))
1247 then
1248 Incod := Empty;
1249
c73ae90f
GD
1250 -- Otherwise, keep the component in order to generate the proper
1251 -- actual subtype, that depends on enclosing discriminants.
758c442c 1252
c73ae90f 1253 else
758c442c
GD
1254 null;
1255 end if;
70482933
RK
1256 end if;
1257
758c442c 1258 Decl :=
70482933
RK
1259 Make_Object_Declaration (Loc,
1260 Defining_Identifier => Temp,
f44fe430 1261 Object_Definition => Indic,
758c442c
GD
1262 Expression => Incod);
1263
1264 if Inside_Init_Proc
1265 and then No (Incod)
1266 then
1267 -- If the call is to initialize a component of a composite type,
1268 -- and the component does not depend on discriminants, use the
1269 -- actual type of the component. This is required in case the
1270 -- component is constrained, because in general the formal of the
1271 -- initialization procedure will be unconstrained. Note that if
1272 -- the component being initialized is constrained by an enclosing
1273 -- discriminant, the presence of the initialization in the
1274 -- declaration will generate an expression for the actual subtype.
1275
1276 Set_No_Initialization (Decl);
1277 Set_Object_Definition (Decl,
1278 New_Occurrence_Of (Etype (Actual), Loc));
1279 end if;
1280
1281 Insert_Action (N, Decl);
70482933
RK
1282
1283 -- The actual is simply a reference to the temporary
1284
1285 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1286
1287 -- Generate copy out if OUT or IN OUT parameter
1288
1289 if Ekind (Formal) /= E_In_Parameter then
1290 Lhs := Outcod;
1291 Rhs := New_Occurrence_Of (Temp, Loc);
1292
1293 -- Deal with conversion
1294
1295 if Nkind (Lhs) = N_Type_Conversion then
1296 Lhs := Expression (Lhs);
1297 Rhs := Convert_To (Etype (Actual), Rhs);
1298 end if;
1299
1300 Append_To (Post_Call,
1301 Make_Assignment_Statement (Loc,
1302 Name => Lhs,
1303 Expression => Rhs));
f44fe430 1304 Set_Assignment_OK (Name (Last (Post_Call)));
70482933 1305 end if;
f44fe430 1306 end Add_Simple_Call_By_Copy_Code;
70482933
RK
1307
1308 ---------------------------
1309 -- Check_Fortran_Logical --
1310 ---------------------------
1311
1312 procedure Check_Fortran_Logical is
fbf5a39b 1313 Logical : constant Entity_Id := Etype (Formal);
70482933
RK
1314 Var : Entity_Id;
1315
1316 -- Note: this is very incomplete, e.g. it does not handle arrays
1317 -- of logical values. This is really not the right approach at all???)
1318
1319 begin
1320 if Convention (Subp) = Convention_Fortran
1321 and then Root_Type (Etype (Formal)) = Standard_Boolean
1322 and then Ekind (Formal) /= E_In_Parameter
1323 then
1324 Var := Make_Var (Actual);
1325 Append_To (Post_Call,
1326 Make_Assignment_Statement (Loc,
1327 Name => New_Occurrence_Of (Var, Loc),
1328 Expression =>
1329 Unchecked_Convert_To (
1330 Logical,
1331 Make_Op_Ne (Loc,
1332 Left_Opnd => New_Occurrence_Of (Var, Loc),
1333 Right_Opnd =>
1334 Unchecked_Convert_To (
1335 Logical,
1336 New_Occurrence_Of (Standard_False, Loc))))));
1337 end if;
1338 end Check_Fortran_Logical;
1339
758c442c
GD
1340 -------------------
1341 -- Is_Legal_Copy --
1342 -------------------
1343
1344 function Is_Legal_Copy return Boolean is
1345 begin
1346 -- An attempt to copy a value of such a type can only occur if
1347 -- representation clauses give the actual a misaligned address.
1348
1349 if Is_By_Reference_Type (Etype (Formal)) then
1350 Error_Msg_N
1351 ("misaligned actual cannot be passed by reference", Actual);
1352 return False;
1353
1354 -- For users of Starlet, we assume that the specification of by-
7888a6ae 1355 -- reference mechanism is mandatory. This may lead to unaligned
758c442c
GD
1356 -- objects but at least for DEC legacy code it is known to work.
1357 -- The warning will alert users of this code that a problem may
1358 -- be lurking.
1359
1360 elsif Mechanism (Formal) = By_Reference
1361 and then Is_Valued_Procedure (Scope (Formal))
1362 then
1363 Error_Msg_N
1364 ("by_reference actual may be misaligned?", Actual);
1365 return False;
1366
1367 else
1368 return True;
1369 end if;
1370 end Is_Legal_Copy;
1371
70482933
RK
1372 --------------
1373 -- Make_Var --
1374 --------------
1375
1376 function Make_Var (Actual : Node_Id) return Entity_Id is
1377 Var : Entity_Id;
1378
1379 begin
1380 if Is_Entity_Name (Actual) then
1381 return Entity (Actual);
1382
1383 else
b086849e 1384 Var := Make_Temporary (Loc, 'T', Actual);
70482933
RK
1385
1386 N_Node :=
1387 Make_Object_Renaming_Declaration (Loc,
1388 Defining_Identifier => Var,
1389 Subtype_Mark =>
1390 New_Occurrence_Of (Etype (Actual), Loc),
1391 Name => Relocate_Node (Actual));
1392
1393 Insert_Action (N, N_Node);
1394 return Var;
1395 end if;
1396 end Make_Var;
1397
1398 -------------------------
1399 -- Reset_Packed_Prefix --
1400 -------------------------
1401
1402 procedure Reset_Packed_Prefix is
1403 Pfx : Node_Id := Actual;
70482933
RK
1404 begin
1405 loop
1406 Set_Analyzed (Pfx, False);
ac4d6407
RD
1407 exit when
1408 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
70482933
RK
1409 Pfx := Prefix (Pfx);
1410 end loop;
1411 end Reset_Packed_Prefix;
1412
1413 -- Start of processing for Expand_Actuals
1414
1415 begin
70482933
RK
1416 Post_Call := New_List;
1417
2f1b20a9
ES
1418 Formal := First_Formal (Subp);
1419 Actual := First_Actual (N);
70482933
RK
1420 while Present (Formal) loop
1421 E_Formal := Etype (Formal);
1422
1423 if Is_Scalar_Type (E_Formal)
1424 or else Nkind (Actual) = N_Slice
1425 then
1426 Check_Fortran_Logical;
1427
1428 -- RM 6.4.1 (11)
1429
1430 elsif Ekind (Formal) /= E_Out_Parameter then
1431
1432 -- The unusual case of the current instance of a protected type
1433 -- requires special handling. This can only occur in the context
1434 -- of a call within the body of a protected operation.
1435
1436 if Is_Entity_Name (Actual)
1437 and then Ekind (Entity (Actual)) = E_Protected_Type
1438 and then In_Open_Scopes (Entity (Actual))
1439 then
1440 if Scope (Subp) /= Entity (Actual) then
1441 Error_Msg_N ("operation outside protected type may not "
1442 & "call back its protected operations?", Actual);
1443 end if;
1444
1445 Rewrite (Actual,
1446 Expand_Protected_Object_Reference (N, Entity (Actual)));
1447 end if;
1448
02822a92
RD
1449 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1450 -- build-in-place function, then a temporary return object needs
1451 -- to be created and access to it must be passed to the function.
f937473f
RD
1452 -- Currently we limit such functions to those with inherently
1453 -- limited result subtypes, but eventually we plan to expand the
1454 -- functions that are treated as build-in-place to include other
1455 -- composite result types.
02822a92
RD
1456
1457 if Ada_Version >= Ada_05
1458 and then Is_Build_In_Place_Function_Call (Actual)
1459 then
1460 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1461 end if;
1462
70482933
RK
1463 Apply_Constraint_Check (Actual, E_Formal);
1464
1465 -- Out parameter case. No constraint checks on access type
1466 -- RM 6.4.1 (13)
1467
1468 elsif Is_Access_Type (E_Formal) then
1469 null;
1470
1471 -- RM 6.4.1 (14)
1472
1473 elsif Has_Discriminants (Base_Type (E_Formal))
1474 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1475 then
1476 Apply_Constraint_Check (Actual, E_Formal);
1477
1478 -- RM 6.4.1 (15)
1479
1480 else
1481 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1482 end if;
1483
1484 -- Processing for IN-OUT and OUT parameters
1485
1486 if Ekind (Formal) /= E_In_Parameter then
1487
1488 -- For type conversions of arrays, apply length/range checks
1489
1490 if Is_Array_Type (E_Formal)
1491 and then Nkind (Actual) = N_Type_Conversion
1492 then
1493 if Is_Constrained (E_Formal) then
1494 Apply_Length_Check (Expression (Actual), E_Formal);
1495 else
1496 Apply_Range_Check (Expression (Actual), E_Formal);
1497 end if;
1498 end if;
1499
1500 -- If argument is a type conversion for a type that is passed
1501 -- by copy, then we must pass the parameter by copy.
1502
1503 if Nkind (Actual) = N_Type_Conversion
1504 and then
1505 (Is_Numeric_Type (E_Formal)
1506 or else Is_Access_Type (E_Formal)
1507 or else Is_Enumeration_Type (E_Formal)
1508 or else Is_Bit_Packed_Array (Etype (Formal))
1509 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1510
1511 -- Also pass by copy if change of representation
1512
1513 or else not Same_Representation
1514 (Etype (Formal),
1515 Etype (Expression (Actual))))
1516 then
1517 Add_Call_By_Copy_Code;
1518
1519 -- References to components of bit packed arrays are expanded
1520 -- at this point, rather than at the point of analysis of the
1521 -- actuals, to handle the expansion of the assignment to
1522 -- [in] out parameters.
1523
1524 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
f44fe430
RD
1525 Add_Simple_Call_By_Copy_Code;
1526
02822a92
RD
1527 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1528 -- because the back-end cannot cope with such objects. In other
1529 -- cases where alignment forces a copy, the back-end generates
1530 -- it properly. It should not be generated unconditionally in the
1531 -- front-end because it does not know precisely the alignment
1532 -- requirements of the target, and makes too conservative an
1533 -- estimate, leading to superfluous copies or spurious errors
1534 -- on by-reference parameters.
f44fe430 1535
02822a92
RD
1536 elsif Nkind (Actual) = N_Selected_Component
1537 and then
1538 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
f44fe430
RD
1539 and then not Represented_As_Scalar (Etype (Formal))
1540 then
1541 Add_Simple_Call_By_Copy_Code;
70482933
RK
1542
1543 -- References to slices of bit packed arrays are expanded
1544
1545 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1546 Add_Call_By_Copy_Code;
1547
fbf5a39b
AC
1548 -- References to possibly unaligned slices of arrays are expanded
1549
1550 elsif Is_Possibly_Unaligned_Slice (Actual) then
1551 Add_Call_By_Copy_Code;
1552
7888a6ae 1553 -- Deal with access types where the actual subtype and the
70482933
RK
1554 -- formal subtype are not the same, requiring a check.
1555
638e383e 1556 -- It is necessary to exclude tagged types because of "downward
70f91180 1557 -- conversion" errors.
70482933
RK
1558
1559 elsif Is_Access_Type (E_Formal)
1560 and then not Same_Type (E_Formal, Etype (Actual))
1561 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1562 then
1563 Add_Call_By_Copy_Code;
1564
faf3cf91
ES
1565 -- If the actual is not a scalar and is marked for volatile
1566 -- treatment, whereas the formal is not volatile, then pass
1567 -- by copy unless it is a by-reference type.
1568
0386aad1
AC
1569 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1570 -- because this is the enforcement of a language rule that applies
1571 -- only to "real" volatile variables, not e.g. to the address
1572 -- clause overlay case.
1573
70482933 1574 elsif Is_Entity_Name (Actual)
0386aad1 1575 and then Is_Volatile (Entity (Actual))
faf3cf91 1576 and then not Is_By_Reference_Type (Etype (Actual))
70482933 1577 and then not Is_Scalar_Type (Etype (Entity (Actual)))
0386aad1 1578 and then not Is_Volatile (E_Formal)
70482933
RK
1579 then
1580 Add_Call_By_Copy_Code;
1581
1582 elsif Nkind (Actual) = N_Indexed_Component
1583 and then Is_Entity_Name (Prefix (Actual))
1584 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1585 then
1586 Add_Call_By_Copy_Code;
d79e621a
GD
1587
1588 -- Add call-by-copy code for the case of scalar out parameters
1589 -- when it is not known at compile time that the subtype of the
c2369146
AC
1590 -- formal is a subrange of the subtype of the actual (or vice
1591 -- versa for in out parameters), in order to get range checks
1592 -- on such actuals. (Maybe this case should be handled earlier
1593 -- in the if statement???)
d79e621a
GD
1594
1595 elsif Is_Scalar_Type (E_Formal)
c2369146
AC
1596 and then
1597 (not In_Subrange_Of (E_Formal, Etype (Actual))
1598 or else
1599 (Ekind (Formal) = E_In_Out_Parameter
1600 and then not In_Subrange_Of (Etype (Actual), E_Formal)))
d79e621a
GD
1601 then
1602 -- Perhaps the setting back to False should be done within
1603 -- Add_Call_By_Copy_Code, since it could get set on other
1604 -- cases occurring above???
1605
1606 if Do_Range_Check (Actual) then
1607 Set_Do_Range_Check (Actual, False);
1608 end if;
1609
1610 Add_Call_By_Copy_Code;
70482933
RK
1611 end if;
1612
fbf5a39b 1613 -- Processing for IN parameters
70482933
RK
1614
1615 else
fbf5a39b
AC
1616 -- For IN parameters is in the packed array case, we expand an
1617 -- indexed component (the circuit in Exp_Ch4 deliberately left
1618 -- indexed components appearing as actuals untouched, so that
1619 -- the special processing above for the OUT and IN OUT cases
1620 -- could be performed. We could make the test in Exp_Ch4 more
1621 -- complex and have it detect the parameter mode, but it is
f44fe430 1622 -- easier simply to handle all cases here.)
fbf5a39b 1623
70482933
RK
1624 if Nkind (Actual) = N_Indexed_Component
1625 and then Is_Packed (Etype (Prefix (Actual)))
1626 then
1627 Reset_Packed_Prefix;
1628 Expand_Packed_Element_Reference (Actual);
1629
0386aad1
AC
1630 -- If we have a reference to a bit packed array, we copy it, since
1631 -- the actual must be byte aligned.
70482933 1632
fbf5a39b 1633 -- Is this really necessary in all cases???
70482933 1634
fbf5a39b 1635 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
f44fe430
RD
1636 Add_Simple_Call_By_Copy_Code;
1637
1638 -- If a non-scalar actual is possibly unaligned, we need a copy
1639
1640 elsif Is_Possibly_Unaligned_Object (Actual)
1641 and then not Represented_As_Scalar (Etype (Formal))
1642 then
1643 Add_Simple_Call_By_Copy_Code;
70482933 1644
fbf5a39b
AC
1645 -- Similarly, we have to expand slices of packed arrays here
1646 -- because the result must be byte aligned.
70482933 1647
fbf5a39b
AC
1648 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1649 Add_Call_By_Copy_Code;
70482933 1650
fbf5a39b
AC
1651 -- Only processing remaining is to pass by copy if this is a
1652 -- reference to a possibly unaligned slice, since the caller
1653 -- expects an appropriately aligned argument.
70482933 1654
fbf5a39b
AC
1655 elsif Is_Possibly_Unaligned_Slice (Actual) then
1656 Add_Call_By_Copy_Code;
70482933
RK
1657 end if;
1658 end if;
1659
1660 Next_Formal (Formal);
1661 Next_Actual (Actual);
1662 end loop;
1663
1664 -- Find right place to put post call stuff if it is present
1665
1666 if not Is_Empty_List (Post_Call) then
1667
2f1b20a9
ES
1668 -- If call is not a list member, it must be the triggering statement
1669 -- of a triggering alternative or an entry call alternative, and we
1670 -- can add the post call stuff to the corresponding statement list.
70482933
RK
1671
1672 if not Is_List_Member (N) then
1673 declare
1674 P : constant Node_Id := Parent (N);
1675
1676 begin
ac4d6407
RD
1677 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1678 N_Entry_Call_Alternative));
70482933
RK
1679
1680 if Is_Non_Empty_List (Statements (P)) then
1681 Insert_List_Before_And_Analyze
1682 (First (Statements (P)), Post_Call);
1683 else
1684 Set_Statements (P, Post_Call);
1685 end if;
1686 end;
1687
1688 -- Otherwise, normal case where N is in a statement sequence,
1689 -- just put the post-call stuff after the call statement.
1690
1691 else
1692 Insert_Actions_After (N, Post_Call);
1693 end if;
1694 end if;
1695
98f01d53 1696 -- The call node itself is re-analyzed in Expand_Call
70482933
RK
1697
1698 end Expand_Actuals;
1699
1700 -----------------
1701 -- Expand_Call --
1702 -----------------
1703
1704 -- This procedure handles expansion of function calls and procedure call
1705 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
70f91180 1706 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
70482933 1707
70f91180 1708 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
70482933
RK
1709 -- Provide values of actuals for all formals in Extra_Formals list
1710 -- Replace "call" to enumeration literal function by literal itself
1711 -- Rewrite call to predefined operator as operator
1712 -- Replace actuals to in-out parameters that are numeric conversions,
1713 -- with explicit assignment to temporaries before and after the call.
1714 -- Remove optional actuals if First_Optional_Parameter specified.
1715
1716 -- Note that the list of actuals has been filled with default expressions
1717 -- during semantic analysis of the call. Only the extra actuals required
1718 -- for the 'Constrained attribute and for accessibility checks are added
1719 -- at this point.
1720
1721 procedure Expand_Call (N : Node_Id) is
1722 Loc : constant Source_Ptr := Sloc (N);
70482933 1723 Extra_Actuals : List_Id := No_List;
fdce4bb7 1724 Prev : Node_Id := Empty;
758c442c 1725
70482933
RK
1726 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1727 -- Adds one entry to the end of the actual parameter list. Used for
2f1b20a9
ES
1728 -- default parameters and for extra actuals (for Extra_Formals). The
1729 -- argument is an N_Parameter_Association node.
70482933
RK
1730
1731 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2f1b20a9
ES
1732 -- Adds an extra actual to the list of extra actuals. Expr is the
1733 -- expression for the value of the actual, EF is the entity for the
1734 -- extra formal.
70482933
RK
1735
1736 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1737 -- Within an instance, a type derived from a non-tagged formal derived
70f91180
RD
1738 -- type inherits from the original parent, not from the actual. The
1739 -- current derivation mechanism has the derived type inherit from the
1740 -- actual, which is only correct outside of the instance. If the
1741 -- subprogram is inherited, we test for this particular case through a
1742 -- convoluted tree traversal before setting the proper subprogram to be
1743 -- called.
70482933
RK
1744
1745 --------------------------
1746 -- Add_Actual_Parameter --
1747 --------------------------
1748
1749 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1750 Actual_Expr : constant Node_Id :=
1751 Explicit_Actual_Parameter (Insert_Param);
1752
1753 begin
1754 -- Case of insertion is first named actual
1755
1756 if No (Prev) or else
1757 Nkind (Parent (Prev)) /= N_Parameter_Association
1758 then
1759 Set_Next_Named_Actual (Insert_Param, First_Named_Actual (N));
1760 Set_First_Named_Actual (N, Actual_Expr);
1761
1762 if No (Prev) then
c8ef728f 1763 if No (Parameter_Associations (N)) then
70482933
RK
1764 Set_Parameter_Associations (N, New_List);
1765 Append (Insert_Param, Parameter_Associations (N));
1766 end if;
1767 else
1768 Insert_After (Prev, Insert_Param);
1769 end if;
1770
1771 -- Case of insertion is not first named actual
1772
1773 else
1774 Set_Next_Named_Actual
1775 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1776 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1777 Append (Insert_Param, Parameter_Associations (N));
1778 end if;
1779
1780 Prev := Actual_Expr;
1781 end Add_Actual_Parameter;
1782
1783 ----------------------
1784 -- Add_Extra_Actual --
1785 ----------------------
1786
1787 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1788 Loc : constant Source_Ptr := Sloc (Expr);
1789
1790 begin
1791 if Extra_Actuals = No_List then
1792 Extra_Actuals := New_List;
1793 Set_Parent (Extra_Actuals, N);
1794 end if;
1795
1796 Append_To (Extra_Actuals,
1797 Make_Parameter_Association (Loc,
9d983bbf
AC
1798 Selector_Name => Make_Identifier (Loc, Chars (EF)),
1799 Explicit_Actual_Parameter => Expr));
70482933
RK
1800
1801 Analyze_And_Resolve (Expr, Etype (EF));
75a64833
AC
1802
1803 if Nkind (N) = N_Function_Call then
1804 Set_Is_Accessibility_Actual (Parent (Expr));
1805 end if;
70482933
RK
1806 end Add_Extra_Actual;
1807
1808 ---------------------------
1809 -- Inherited_From_Formal --
1810 ---------------------------
1811
1812 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1813 Par : Entity_Id;
1814 Gen_Par : Entity_Id;
1815 Gen_Prim : Elist_Id;
1816 Elmt : Elmt_Id;
1817 Indic : Node_Id;
1818
1819 begin
1820 -- If the operation is inherited, it is attached to the corresponding
1821 -- type derivation. If the parent in the derivation is a generic
1822 -- actual, it is a subtype of the actual, and we have to recover the
1823 -- original derived type declaration to find the proper parent.
1824
1825 if Nkind (Parent (S)) /= N_Full_Type_Declaration
fbf5a39b 1826 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2f1b20a9
ES
1827 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1828 N_Derived_Type_Definition
fbf5a39b 1829 or else not In_Instance
70482933
RK
1830 then
1831 return Empty;
1832
1833 else
1834 Indic :=
e27b834b
AC
1835 Subtype_Indication
1836 (Type_Definition (Original_Node (Parent (S))));
70482933
RK
1837
1838 if Nkind (Indic) = N_Subtype_Indication then
1839 Par := Entity (Subtype_Mark (Indic));
1840 else
1841 Par := Entity (Indic);
1842 end if;
1843 end if;
1844
1845 if not Is_Generic_Actual_Type (Par)
1846 or else Is_Tagged_Type (Par)
1847 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1848 or else not In_Open_Scopes (Scope (Par))
70482933
RK
1849 then
1850 return Empty;
70482933
RK
1851 else
1852 Gen_Par := Generic_Parent_Type (Parent (Par));
1853 end if;
1854
7888a6ae
GD
1855 -- If the actual has no generic parent type, the formal is not
1856 -- a formal derived type, so nothing to inherit.
1857
1858 if No (Gen_Par) then
1859 return Empty;
1860 end if;
1861
2f1b20a9
ES
1862 -- If the generic parent type is still the generic type, this is a
1863 -- private formal, not a derived formal, and there are no operations
1864 -- inherited from the formal.
fbf5a39b
AC
1865
1866 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1867 return Empty;
1868 end if;
1869
70482933 1870 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
70482933 1871
2f1b20a9 1872 Elmt := First_Elmt (Gen_Prim);
70482933
RK
1873 while Present (Elmt) loop
1874 if Chars (Node (Elmt)) = Chars (S) then
1875 declare
1876 F1 : Entity_Id;
1877 F2 : Entity_Id;
70482933 1878
2f1b20a9 1879 begin
70482933
RK
1880 F1 := First_Formal (S);
1881 F2 := First_Formal (Node (Elmt));
70482933
RK
1882 while Present (F1)
1883 and then Present (F2)
1884 loop
70482933
RK
1885 if Etype (F1) = Etype (F2)
1886 or else Etype (F2) = Gen_Par
1887 then
1888 Next_Formal (F1);
1889 Next_Formal (F2);
1890 else
1891 Next_Elmt (Elmt);
1892 exit; -- not the right subprogram
1893 end if;
1894
1895 return Node (Elmt);
1896 end loop;
1897 end;
1898
1899 else
1900 Next_Elmt (Elmt);
1901 end if;
1902 end loop;
1903
1904 raise Program_Error;
1905 end Inherited_From_Formal;
1906
fdce4bb7
JM
1907 -- Local variables
1908
1909 Remote : constant Boolean := Is_Remote_Call (N);
1910 Actual : Node_Id;
1911 Formal : Entity_Id;
1912 Orig_Subp : Entity_Id := Empty;
1913 Param_Count : Natural := 0;
1914 Parent_Formal : Entity_Id;
1915 Parent_Subp : Entity_Id;
1916 Scop : Entity_Id;
1917 Subp : Entity_Id;
1918
e27b834b 1919 Prev_Orig : Node_Id;
fdce4bb7
JM
1920 -- Original node for an actual, which may have been rewritten. If the
1921 -- actual is a function call that has been transformed from a selected
1922 -- component, the original node is unanalyzed. Otherwise, it carries
1923 -- semantic information used to generate additional actuals.
1924
1925 CW_Interface_Formals_Present : Boolean := False;
1926
70482933
RK
1927 -- Start of processing for Expand_Call
1928
1929 begin
07fc65c4
GB
1930 -- Ignore if previous error
1931
1932 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1933 return;
1934 end if;
1935
70482933
RK
1936 -- Call using access to subprogram with explicit dereference
1937
1938 if Nkind (Name (N)) = N_Explicit_Dereference then
1939 Subp := Etype (Name (N));
1940 Parent_Subp := Empty;
1941
1942 -- Case of call to simple entry, where the Name is a selected component
1943 -- whose prefix is the task, and whose selector name is the entry name
1944
1945 elsif Nkind (Name (N)) = N_Selected_Component then
1946 Subp := Entity (Selector_Name (Name (N)));
1947 Parent_Subp := Empty;
1948
1949 -- Case of call to member of entry family, where Name is an indexed
1950 -- component, with the prefix being a selected component giving the
1951 -- task and entry family name, and the index being the entry index.
1952
1953 elsif Nkind (Name (N)) = N_Indexed_Component then
1954 Subp := Entity (Selector_Name (Prefix (Name (N))));
1955 Parent_Subp := Empty;
1956
1957 -- Normal case
1958
1959 else
1960 Subp := Entity (Name (N));
1961 Parent_Subp := Alias (Subp);
1962
1963 -- Replace call to Raise_Exception by call to Raise_Exception_Always
1964 -- if we can tell that the first parameter cannot possibly be null.
70f91180 1965 -- This improves efficiency by avoiding a run-time test.
70482933 1966
7888a6ae
GD
1967 -- We do not do this if Raise_Exception_Always does not exist, which
1968 -- can happen in configurable run time profiles which provide only a
70f91180 1969 -- Raise_Exception.
7888a6ae
GD
1970
1971 if Is_RTE (Subp, RE_Raise_Exception)
1972 and then RTE_Available (RE_Raise_Exception_Always)
70482933
RK
1973 then
1974 declare
1975 FA : constant Node_Id := Original_Node (First_Actual (N));
1976
1977 begin
1978 -- The case we catch is where the first argument is obtained
2f1b20a9
ES
1979 -- using the Identity attribute (which must always be
1980 -- non-null).
70482933
RK
1981
1982 if Nkind (FA) = N_Attribute_Reference
1983 and then Attribute_Name (FA) = Name_Identity
1984 then
1985 Subp := RTE (RE_Raise_Exception_Always);
7888a6ae 1986 Set_Name (N, New_Occurrence_Of (Subp, Loc));
70482933
RK
1987 end if;
1988 end;
1989 end if;
1990
1991 if Ekind (Subp) = E_Entry then
1992 Parent_Subp := Empty;
1993 end if;
1994 end if;
1995
f4d379b8
HK
1996 -- Ada 2005 (AI-345): We have a procedure call as a triggering
1997 -- alternative in an asynchronous select or as an entry call in
1998 -- a conditional or timed select. Check whether the procedure call
1999 -- is a renaming of an entry and rewrite it as an entry call.
2000
2001 if Ada_Version >= Ada_05
2002 and then Nkind (N) = N_Procedure_Call_Statement
2003 and then
2004 ((Nkind (Parent (N)) = N_Triggering_Alternative
2005 and then Triggering_Statement (Parent (N)) = N)
2006 or else
2007 (Nkind (Parent (N)) = N_Entry_Call_Alternative
2008 and then Entry_Call_Statement (Parent (N)) = N))
2009 then
2010 declare
2011 Ren_Decl : Node_Id;
2012 Ren_Root : Entity_Id := Subp;
2013
2014 begin
2015 -- This may be a chain of renamings, find the root
2016
2017 if Present (Alias (Ren_Root)) then
2018 Ren_Root := Alias (Ren_Root);
2019 end if;
2020
2021 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2022 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2023
2024 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2025 Rewrite (N,
2026 Make_Entry_Call_Statement (Loc,
2027 Name =>
2028 New_Copy_Tree (Name (Ren_Decl)),
2029 Parameter_Associations =>
2030 New_Copy_List_Tree (Parameter_Associations (N))));
2031
2032 return;
2033 end if;
2034 end if;
2035 end;
2036 end if;
2037
e27b834b
AC
2038 -- First step, compute extra actuals, corresponding to any Extra_Formals
2039 -- present. Note that we do not access Extra_Formals directly, instead
2040 -- we simply note the presence of the extra formals as we process the
2041 -- regular formals collecting corresponding actuals in Extra_Actuals.
70482933 2042
c2369146
AC
2043 -- We also generate any required range checks for actuals for in formals
2044 -- as we go through the loop, since this is a convenient place to do it.
2045 -- (Though it seems that this would be better done in Expand_Actuals???)
fbf5a39b 2046
fdce4bb7
JM
2047 Formal := First_Formal (Subp);
2048 Actual := First_Actual (N);
2049 Param_Count := 1;
70482933 2050 while Present (Formal) loop
fbf5a39b 2051
d79e621a 2052 -- Generate range check if required
fbf5a39b 2053
d79e621a 2054 if Do_Range_Check (Actual)
c2369146 2055 and then Ekind (Formal) = E_In_Parameter
d79e621a
GD
2056 then
2057 Set_Do_Range_Check (Actual, False);
2058 Generate_Range_Check
2059 (Actual, Etype (Formal), CE_Range_Check_Failed);
2060 end if;
fbf5a39b
AC
2061
2062 -- Prepare to examine current entry
2063
70482933
RK
2064 Prev := Actual;
2065 Prev_Orig := Original_Node (Prev);
2066
758c442c 2067 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2f1b20a9 2068 -- to expand it in a further round.
758c442c
GD
2069
2070 CW_Interface_Formals_Present :=
2071 CW_Interface_Formals_Present
2072 or else
2073 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2074 and then Is_Interface (Etype (Etype (Formal))))
2075 or else
2076 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2077 and then Is_Interface (Directly_Designated_Type
2078 (Etype (Etype (Formal)))));
2079
2080 -- Create possible extra actual for constrained case. Usually, the
2081 -- extra actual is of the form actual'constrained, but since this
2082 -- attribute is only available for unconstrained records, TRUE is
2083 -- expanded if the type of the formal happens to be constrained (for
2084 -- instance when this procedure is inherited from an unconstrained
2085 -- record to a constrained one) or if the actual has no discriminant
2086 -- (its type is constrained). An exception to this is the case of a
2087 -- private type without discriminants. In this case we pass FALSE
2088 -- because the object has underlying discriminants with defaults.
70482933
RK
2089
2090 if Present (Extra_Constrained (Formal)) then
2091 if Ekind (Etype (Prev)) in Private_Kind
2092 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2093 then
01aef5ad
GD
2094 Add_Extra_Actual
2095 (New_Occurrence_Of (Standard_False, Loc),
2096 Extra_Constrained (Formal));
70482933
RK
2097
2098 elsif Is_Constrained (Etype (Formal))
2099 or else not Has_Discriminants (Etype (Prev))
2100 then
01aef5ad
GD
2101 Add_Extra_Actual
2102 (New_Occurrence_Of (Standard_True, Loc),
2103 Extra_Constrained (Formal));
70482933 2104
5d09245e
AC
2105 -- Do not produce extra actuals for Unchecked_Union parameters.
2106 -- Jump directly to the end of the loop.
2107
2108 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2109 goto Skip_Extra_Actual_Generation;
2110
70482933
RK
2111 else
2112 -- If the actual is a type conversion, then the constrained
2113 -- test applies to the actual, not the target type.
2114
2115 declare
2f1b20a9 2116 Act_Prev : Node_Id;
70482933
RK
2117
2118 begin
2f1b20a9
ES
2119 -- Test for unchecked conversions as well, which can occur
2120 -- as out parameter actuals on calls to stream procedures.
70482933 2121
2f1b20a9 2122 Act_Prev := Prev;
ac4d6407
RD
2123 while Nkind_In (Act_Prev, N_Type_Conversion,
2124 N_Unchecked_Type_Conversion)
fbf5a39b 2125 loop
70482933 2126 Act_Prev := Expression (Act_Prev);
fbf5a39b 2127 end loop;
70482933 2128
3563739b
AC
2129 -- If the expression is a conversion of a dereference, this
2130 -- is internally generated code that manipulates addresses,
2131 -- e.g. when building interface tables. No check should
2132 -- occur in this case, and the discriminated object is not
2133 -- directly a hand.
f4d379b8
HK
2134
2135 if not Comes_From_Source (Actual)
2136 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2137 and then Nkind (Act_Prev) = N_Explicit_Dereference
2138 then
2139 Add_Extra_Actual
2140 (New_Occurrence_Of (Standard_False, Loc),
2141 Extra_Constrained (Formal));
2142
2143 else
2144 Add_Extra_Actual
2145 (Make_Attribute_Reference (Sloc (Prev),
2146 Prefix =>
2147 Duplicate_Subexpr_No_Checks
2148 (Act_Prev, Name_Req => True),
2149 Attribute_Name => Name_Constrained),
2150 Extra_Constrained (Formal));
2151 end if;
70482933
RK
2152 end;
2153 end if;
2154 end if;
2155
2156 -- Create possible extra actual for accessibility level
2157
2158 if Present (Extra_Accessibility (Formal)) then
7888a6ae
GD
2159
2160 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2161 -- attribute, then the original actual may be an aliased object
2162 -- occurring as the prefix in a call using "Object.Operation"
2163 -- notation. In that case we must pass the level of the object,
2164 -- so Prev_Orig is reset to Prev and the attribute will be
2165 -- processed by the code for Access attributes further below.
2166
2167 if Prev_Orig /= Prev
2168 and then Nkind (Prev) = N_Attribute_Reference
2169 and then
2170 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2171 and then Is_Aliased_View (Prev_Orig)
2172 then
2173 Prev_Orig := Prev;
2174 end if;
2175
9d983bbf
AC
2176 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2177 -- accessibility levels.
fdce4bb7
JM
2178
2179 if Ekind (Current_Scope) in Subprogram_Kind
2180 and then Is_Thunk (Current_Scope)
2181 then
2182 declare
2183 Parm_Ent : Entity_Id;
2184
2185 begin
2186 if Is_Controlling_Actual (Actual) then
2187
2188 -- Find the corresponding actual of the thunk
2189
2190 Parm_Ent := First_Entity (Current_Scope);
2191 for J in 2 .. Param_Count loop
2192 Next_Entity (Parm_Ent);
2193 end loop;
2194
2195 else pragma Assert (Is_Entity_Name (Actual));
2196 Parm_Ent := Entity (Actual);
2197 end if;
2198
2199 Add_Extra_Actual
2200 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2201 Extra_Accessibility (Formal));
2202 end;
2203
2204 elsif Is_Entity_Name (Prev_Orig) then
70482933 2205
d766cee3
RD
2206 -- When passing an access parameter, or a renaming of an access
2207 -- parameter, as the actual to another access parameter we need
2208 -- to pass along the actual's own access level parameter. This
2209 -- is done if we are within the scope of the formal access
2210 -- parameter (if this is an inlined body the extra formal is
2211 -- irrelevant).
2212
2213 if (Is_Formal (Entity (Prev_Orig))
2214 or else
2215 (Present (Renamed_Object (Entity (Prev_Orig)))
2216 and then
2217 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2218 and then
2219 Is_Formal
2220 (Entity (Renamed_Object (Entity (Prev_Orig))))))
70482933
RK
2221 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2222 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2223 then
2224 declare
2225 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2226
2227 begin
2228 pragma Assert (Present (Parm_Ent));
2229
2230 if Present (Extra_Accessibility (Parm_Ent)) then
f4d379b8
HK
2231 Add_Extra_Actual
2232 (New_Occurrence_Of
2233 (Extra_Accessibility (Parm_Ent), Loc),
2234 Extra_Accessibility (Formal));
70482933
RK
2235
2236 -- If the actual access parameter does not have an
2237 -- associated extra formal providing its scope level,
2238 -- then treat the actual as having library-level
2239 -- accessibility.
2240
2241 else
f4d379b8
HK
2242 Add_Extra_Actual
2243 (Make_Integer_Literal (Loc,
01aef5ad 2244 Intval => Scope_Depth (Standard_Standard)),
f4d379b8 2245 Extra_Accessibility (Formal));
70482933
RK
2246 end if;
2247 end;
2248
7888a6ae
GD
2249 -- The actual is a normal access value, so just pass the level
2250 -- of the actual's access type.
70482933
RK
2251
2252 else
f4d379b8
HK
2253 Add_Extra_Actual
2254 (Make_Integer_Literal (Loc,
01aef5ad 2255 Intval => Type_Access_Level (Etype (Prev_Orig))),
f4d379b8 2256 Extra_Accessibility (Formal));
70482933
RK
2257 end if;
2258
01aef5ad
GD
2259 -- If the actual is an access discriminant, then pass the level
2260 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2261
2262 elsif Nkind (Prev_Orig) = N_Selected_Component
2263 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2264 E_Discriminant
2265 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2266 E_Anonymous_Access_Type
2267 then
2268 Add_Extra_Actual
2269 (Make_Integer_Literal (Loc,
2270 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2271 Extra_Accessibility (Formal));
2272
2273 -- All other cases
fdce4bb7 2274
70482933
RK
2275 else
2276 case Nkind (Prev_Orig) is
2277
2278 when N_Attribute_Reference =>
70482933
RK
2279 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2280
75a64833 2281 -- For X'Access, pass on the level of the prefix X
70482933
RK
2282
2283 when Attribute_Access =>
75a64833
AC
2284 Add_Extra_Actual
2285 (Make_Integer_Literal (Loc,
2286 Intval =>
2287 Object_Access_Level
2288 (Prefix (Prev_Orig))),
bac7206d 2289 Extra_Accessibility (Formal));
70482933
RK
2290
2291 -- Treat the unchecked attributes as library-level
2292
2293 when Attribute_Unchecked_Access |
2294 Attribute_Unrestricted_Access =>
01aef5ad
GD
2295 Add_Extra_Actual
2296 (Make_Integer_Literal (Loc,
2297 Intval => Scope_Depth (Standard_Standard)),
2298 Extra_Accessibility (Formal));
70482933
RK
2299
2300 -- No other cases of attributes returning access
9d983bbf 2301 -- values that can be passed to access parameters.
70482933
RK
2302
2303 when others =>
2304 raise Program_Error;
2305
2306 end case;
2307
92a745f3
TQ
2308 -- For allocators we pass the level of the execution of the
2309 -- called subprogram, which is one greater than the current
2310 -- scope level.
70482933
RK
2311
2312 when N_Allocator =>
01aef5ad
GD
2313 Add_Extra_Actual
2314 (Make_Integer_Literal (Loc,
2315 Intval => Scope_Depth (Current_Scope) + 1),
2316 Extra_Accessibility (Formal));
70482933 2317
8ca3bf91
GD
2318 -- For other cases we simply pass the level of the actual's
2319 -- access type. The type is retrieved from Prev rather than
61549759 2320 -- Prev_Orig, because in some cases Prev_Orig denotes an
8ca3bf91 2321 -- original expression that has not been analyzed.
70482933
RK
2322
2323 when others =>
01aef5ad
GD
2324 Add_Extra_Actual
2325 (Make_Integer_Literal (Loc,
8ca3bf91 2326 Intval => Type_Access_Level (Etype (Prev))),
01aef5ad 2327 Extra_Accessibility (Formal));
70482933
RK
2328 end case;
2329 end if;
2330 end if;
2331
2f1b20a9
ES
2332 -- Perform the check of 4.6(49) that prevents a null value from being
2333 -- passed as an actual to an access parameter. Note that the check is
2334 -- elided in the common cases of passing an access attribute or
2335 -- access parameter as an actual. Also, we currently don't enforce
2336 -- this check for expander-generated actuals and when -gnatdj is set.
70482933 2337
2f1b20a9 2338 if Ada_Version >= Ada_05 then
70482933 2339
2f1b20a9 2340 -- Ada 2005 (AI-231): Check null-excluding access types
70482933 2341
2f1b20a9
ES
2342 if Is_Access_Type (Etype (Formal))
2343 and then Can_Never_Be_Null (Etype (Formal))
2344 and then Nkind (Prev) /= N_Raise_Constraint_Error
d766cee3 2345 and then (Known_Null (Prev)
2f1b20a9
ES
2346 or else not Can_Never_Be_Null (Etype (Prev)))
2347 then
2348 Install_Null_Excluding_Check (Prev);
2349 end if;
70482933 2350
2f1b20a9 2351 -- Ada_Version < Ada_05
70482933 2352
2f1b20a9
ES
2353 else
2354 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2355 or else Access_Checks_Suppressed (Subp)
2356 then
2357 null;
70482933 2358
2f1b20a9
ES
2359 elsif Debug_Flag_J then
2360 null;
70482933 2361
2f1b20a9
ES
2362 elsif not Comes_From_Source (Prev) then
2363 null;
70482933 2364
2f1b20a9
ES
2365 elsif Is_Entity_Name (Prev)
2366 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2367 then
2368 null;
2820d220 2369
ac4d6407 2370 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2f1b20a9
ES
2371 null;
2372
2373 -- Suppress null checks when passing to access parameters of Java
7888a6ae
GD
2374 -- and CIL subprograms. (Should this be done for other foreign
2375 -- conventions as well ???)
2f1b20a9 2376
7888a6ae
GD
2377 elsif Convention (Subp) = Convention_Java
2378 or else Convention (Subp) = Convention_CIL
2379 then
2f1b20a9
ES
2380 null;
2381
2382 else
2383 Install_Null_Excluding_Check (Prev);
2384 end if;
70482933
RK
2385 end if;
2386
fbf5a39b
AC
2387 -- Perform appropriate validity checks on parameters that
2388 -- are entities.
70482933
RK
2389
2390 if Validity_Checks_On then
6cdb2c6e
AC
2391 if (Ekind (Formal) = E_In_Parameter
2392 and then Validity_Check_In_Params)
2393 or else
2394 (Ekind (Formal) = E_In_Out_Parameter
2395 and then Validity_Check_In_Out_Params)
70482933 2396 then
7888a6ae
GD
2397 -- If the actual is an indexed component of a packed type (or
2398 -- is an indexed or selected component whose prefix recursively
2399 -- meets this condition), it has not been expanded yet. It will
2400 -- be copied in the validity code that follows, and has to be
2401 -- expanded appropriately, so reanalyze it.
08aa9a4a 2402
7888a6ae
GD
2403 -- What we do is just to unset analyzed bits on prefixes till
2404 -- we reach something that does not have a prefix.
2405
2406 declare
2407 Nod : Node_Id;
2408
2409 begin
2410 Nod := Actual;
ac4d6407
RD
2411 while Nkind_In (Nod, N_Indexed_Component,
2412 N_Selected_Component)
7888a6ae
GD
2413 loop
2414 Set_Analyzed (Nod, False);
2415 Nod := Prefix (Nod);
2416 end loop;
2417 end;
08aa9a4a 2418
70482933 2419 Ensure_Valid (Actual);
70482933
RK
2420 end if;
2421 end if;
2422
2423 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2424 -- since this is a left side reference. We only do this for calls
2425 -- from the source program since we assume that compiler generated
2426 -- calls explicitly generate any required checks. We also need it
2427 -- only if we are doing standard validity checks, since clearly it
2428 -- is not needed if validity checks are off, and in subscript
2429 -- validity checking mode, all indexed components are checked with
2430 -- a call directly from Expand_N_Indexed_Component.
2431
2432 if Comes_From_Source (N)
2433 and then Ekind (Formal) /= E_In_Parameter
2434 and then Validity_Checks_On
2435 and then Validity_Check_Default
2436 and then not Validity_Check_Subscripts
2437 then
2438 Check_Valid_Lvalue_Subscripts (Actual);
2439 end if;
2440
c8ef728f
ES
2441 -- Mark any scalar OUT parameter that is a simple variable as no
2442 -- longer known to be valid (unless the type is always valid). This
2443 -- reflects the fact that if an OUT parameter is never set in a
2444 -- procedure, then it can become invalid on the procedure return.
fbf5a39b
AC
2445
2446 if Ekind (Formal) = E_Out_Parameter
2447 and then Is_Entity_Name (Actual)
2448 and then Ekind (Entity (Actual)) = E_Variable
2449 and then not Is_Known_Valid (Etype (Actual))
2450 then
2451 Set_Is_Known_Valid (Entity (Actual), False);
2452 end if;
2453
c8ef728f
ES
2454 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2455 -- clear current values, since they can be clobbered. We are probably
2456 -- doing this in more places than we need to, but better safe than
2457 -- sorry when it comes to retaining bad current values!
fbf5a39b
AC
2458
2459 if Ekind (Formal) /= E_In_Parameter
2460 and then Is_Entity_Name (Actual)
67ce0d7e 2461 and then Present (Entity (Actual))
fbf5a39b 2462 then
67ce0d7e
RD
2463 declare
2464 Ent : constant Entity_Id := Entity (Actual);
2465 Sav : Node_Id;
2466
2467 begin
ac4d6407
RD
2468 -- For an OUT or IN OUT parameter that is an assignable entity,
2469 -- we do not want to clobber the Last_Assignment field, since
2470 -- if it is set, it was precisely because it is indeed an OUT
75ba322d
AC
2471 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
2472 -- since the subprogram could have returned in invalid value.
ac4d6407
RD
2473
2474 if (Ekind (Formal) = E_Out_Parameter
2475 or else
2476 Ekind (Formal) = E_In_Out_Parameter)
67ce0d7e
RD
2477 and then Is_Assignable (Ent)
2478 then
2479 Sav := Last_Assignment (Ent);
2480 Kill_Current_Values (Ent);
2481 Set_Last_Assignment (Ent, Sav);
75ba322d 2482 Set_Is_Known_Valid (Ent, False);
67ce0d7e
RD
2483
2484 -- For all other cases, just kill the current values
2485
2486 else
2487 Kill_Current_Values (Ent);
2488 end if;
2489 end;
fbf5a39b
AC
2490 end if;
2491
70482933
RK
2492 -- If the formal is class wide and the actual is an aggregate, force
2493 -- evaluation so that the back end who does not know about class-wide
2494 -- type, does not generate a temporary of the wrong size.
2495
2496 if not Is_Class_Wide_Type (Etype (Formal)) then
2497 null;
2498
2499 elsif Nkind (Actual) = N_Aggregate
2500 or else (Nkind (Actual) = N_Qualified_Expression
2501 and then Nkind (Expression (Actual)) = N_Aggregate)
2502 then
2503 Force_Evaluation (Actual);
2504 end if;
2505
2506 -- In a remote call, if the formal is of a class-wide type, check
2507 -- that the actual meets the requirements described in E.4(18).
2508
7888a6ae 2509 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
70482933 2510 Insert_Action (Actual,
7888a6ae
GD
2511 Make_Transportable_Check (Loc,
2512 Duplicate_Subexpr_Move_Checks (Actual)));
70482933
RK
2513 end if;
2514
5d09245e
AC
2515 -- This label is required when skipping extra actual generation for
2516 -- Unchecked_Union parameters.
2517
2518 <<Skip_Extra_Actual_Generation>>
2519
fdce4bb7 2520 Param_Count := Param_Count + 1;
70482933
RK
2521 Next_Actual (Actual);
2522 Next_Formal (Formal);
2523 end loop;
2524
c8ef728f
ES
2525 -- If we are expanding a rhs of an assignment we need to check if tag
2526 -- propagation is needed. You might expect this processing to be in
2527 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
2528 -- assignment might be transformed to a declaration for an unconstrained
2529 -- value if the expression is classwide.
70482933
RK
2530
2531 if Nkind (N) = N_Function_Call
2532 and then Is_Tag_Indeterminate (N)
2533 and then Is_Entity_Name (Name (N))
2534 then
2535 declare
2536 Ass : Node_Id := Empty;
2537
2538 begin
2539 if Nkind (Parent (N)) = N_Assignment_Statement then
2540 Ass := Parent (N);
2541
2542 elsif Nkind (Parent (N)) = N_Qualified_Expression
2543 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2544 then
2545 Ass := Parent (Parent (N));
02822a92
RD
2546
2547 elsif Nkind (Parent (N)) = N_Explicit_Dereference
2548 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2549 then
2550 Ass := Parent (Parent (N));
70482933
RK
2551 end if;
2552
2553 if Present (Ass)
2554 and then Is_Class_Wide_Type (Etype (Name (Ass)))
2555 then
02822a92
RD
2556 if Is_Access_Type (Etype (N)) then
2557 if Designated_Type (Etype (N)) /=
2558 Root_Type (Etype (Name (Ass)))
2559 then
2560 Error_Msg_NE
2561 ("tag-indeterminate expression "
d766cee3 2562 & " must have designated type& (RM 5.2 (6))",
02822a92
RD
2563 N, Root_Type (Etype (Name (Ass))));
2564 else
2565 Propagate_Tag (Name (Ass), N);
2566 end if;
2567
2568 elsif Etype (N) /= Root_Type (Etype (Name (Ass))) then
fbf5a39b
AC
2569 Error_Msg_NE
2570 ("tag-indeterminate expression must have type&"
d766cee3 2571 & "(RM 5.2 (6))", N, Root_Type (Etype (Name (Ass))));
02822a92 2572
fbf5a39b
AC
2573 else
2574 Propagate_Tag (Name (Ass), N);
2575 end if;
2576
2577 -- The call will be rewritten as a dispatching call, and
2578 -- expanded as such.
2579
70482933
RK
2580 return;
2581 end if;
2582 end;
2583 end if;
2584
758c442c
GD
2585 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
2586 -- it to point to the correct secondary virtual table
2587
ac4d6407 2588 if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
758c442c
GD
2589 and then CW_Interface_Formals_Present
2590 then
2591 Expand_Interface_Actuals (N);
2592 end if;
2593
70482933
RK
2594 -- Deals with Dispatch_Call if we still have a call, before expanding
2595 -- extra actuals since this will be done on the re-analysis of the
2596 -- dispatching call. Note that we do not try to shorten the actual
2597 -- list for a dispatching call, it would not make sense to do so.
7888a6ae
GD
2598 -- Expansion of dispatching calls is suppressed when VM_Target, because
2599 -- the VM back-ends directly handle the generation of dispatching
70482933
RK
2600 -- calls and would have to undo any expansion to an indirect call.
2601
ac4d6407 2602 if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
70482933 2603 and then Present (Controlling_Argument (N))
70482933 2604 then
1f110335 2605 if Tagged_Type_Expansion then
70f91180 2606 Expand_Dispatching_Call (N);
fbf5a39b 2607
70f91180
RD
2608 -- The following return is worrisome. Is it really OK to
2609 -- skip all remaining processing in this procedure ???
fbf5a39b 2610
70f91180
RD
2611 return;
2612
70f91180 2613 else
5a1ccfb1
AC
2614 Apply_Tag_Checks (N);
2615
2616 -- Expansion of a dispatching call results in an indirect call,
2617 -- which in turn causes current values to be killed (see
2618 -- Resolve_Call), so on VM targets we do the call here to ensure
2619 -- consistent warnings between VM and non-VM targets.
2620
70f91180
RD
2621 Kill_Current_Values;
2622 end if;
2623 end if;
70482933
RK
2624
2625 -- Similarly, expand calls to RCI subprograms on which pragma
2626 -- All_Calls_Remote applies. The rewriting will be reanalyzed
2627 -- later. Do this only when the call comes from source since we do
8fc789c8 2628 -- not want such a rewriting to occur in expanded code.
70482933 2629
70f91180 2630 if Is_All_Remote_Call (N) then
70482933
RK
2631 Expand_All_Calls_Remote_Subprogram_Call (N);
2632
2633 -- Similarly, do not add extra actuals for an entry call whose entity
2634 -- is a protected procedure, or for an internal protected subprogram
2635 -- call, because it will be rewritten as a protected subprogram call
2636 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
2637
2638 elsif Is_Protected_Type (Scope (Subp))
2639 and then (Ekind (Subp) = E_Procedure
2640 or else Ekind (Subp) = E_Function)
2641 then
2642 null;
2643
2644 -- During that loop we gathered the extra actuals (the ones that
2645 -- correspond to Extra_Formals), so now they can be appended.
2646
2647 else
2648 while Is_Non_Empty_List (Extra_Actuals) loop
2649 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2650 end loop;
2651 end if;
2652
f44fe430
RD
2653 -- At this point we have all the actuals, so this is the point at
2654 -- which the various expansion activities for actuals is carried out.
2655
2656 Expand_Actuals (N, Subp);
70482933
RK
2657
2658 -- If the subprogram is a renaming, or if it is inherited, replace it
2659 -- in the call with the name of the actual subprogram being called.
2660 -- If this is a dispatching call, the run-time decides what to call.
2661 -- The Alias attribute does not apply to entries.
2662
2663 if Nkind (N) /= N_Entry_Call_Statement
2664 and then No (Controlling_Argument (N))
2665 and then Present (Parent_Subp)
2666 then
2667 if Present (Inherited_From_Formal (Subp)) then
2668 Parent_Subp := Inherited_From_Formal (Subp);
2669 else
b81a5940 2670 Parent_Subp := Ultimate_Alias (Parent_Subp);
70482933
RK
2671 end if;
2672
c8ef728f
ES
2673 -- The below setting of Entity is suspect, see F109-018 discussion???
2674
70482933
RK
2675 Set_Entity (Name (N), Parent_Subp);
2676
f937473f 2677 if Is_Abstract_Subprogram (Parent_Subp)
70482933
RK
2678 and then not In_Instance
2679 then
2680 Error_Msg_NE
2681 ("cannot call abstract subprogram &!", Name (N), Parent_Subp);
2682 end if;
2683
d4817e3f
HK
2684 -- Inspect all formals of derived subprogram Subp. Compare parameter
2685 -- types with the parent subprogram and check whether an actual may
2686 -- need a type conversion to the corresponding formal of the parent
2687 -- subprogram.
70482933 2688
d4817e3f 2689 -- Not clear whether intrinsic subprograms need such conversions. ???
70482933
RK
2690
2691 if not Is_Intrinsic_Subprogram (Parent_Subp)
2692 or else Is_Generic_Instance (Parent_Subp)
2693 then
d4817e3f
HK
2694 declare
2695 procedure Convert (Act : Node_Id; Typ : Entity_Id);
2696 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
2697 -- and resolve the newly generated construct.
70482933 2698
d4817e3f
HK
2699 -------------
2700 -- Convert --
2701 -------------
70482933 2702
d4817e3f
HK
2703 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
2704 begin
2705 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
2706 Analyze (Act);
2707 Resolve (Act, Typ);
2708 end Convert;
2709
2710 -- Local variables
2711
2712 Actual_Typ : Entity_Id;
2713 Formal_Typ : Entity_Id;
2714 Parent_Typ : Entity_Id;
2715
2716 begin
2717 Actual := First_Actual (N);
2718 Formal := First_Formal (Subp);
2719 Parent_Formal := First_Formal (Parent_Subp);
2720 while Present (Formal) loop
2721 Actual_Typ := Etype (Actual);
2722 Formal_Typ := Etype (Formal);
2723 Parent_Typ := Etype (Parent_Formal);
2724
2725 -- For an IN parameter of a scalar type, the parent formal
2726 -- type and derived formal type differ or the parent formal
2727 -- type and actual type do not match statically.
2728
2729 if Is_Scalar_Type (Formal_Typ)
2730 and then Ekind (Formal) = E_In_Parameter
2731 and then Formal_Typ /= Parent_Typ
2732 and then
2733 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
2734 and then not Raises_Constraint_Error (Actual)
2735 then
2736 Convert (Actual, Parent_Typ);
2737 Enable_Range_Check (Actual);
2738
d79e621a
GD
2739 -- If the actual has been marked as requiring a range
2740 -- check, then generate it here.
2741
2742 if Do_Range_Check (Actual) then
2743 Set_Do_Range_Check (Actual, False);
2744 Generate_Range_Check
2745 (Actual, Etype (Formal), CE_Range_Check_Failed);
2746 end if;
2747
d4817e3f
HK
2748 -- For access types, the parent formal type and actual type
2749 -- differ.
2750
2751 elsif Is_Access_Type (Formal_Typ)
2752 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
70482933 2753 then
d4817e3f
HK
2754 if Ekind (Formal) /= E_In_Parameter then
2755 Convert (Actual, Parent_Typ);
2756
2757 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
2758 and then Designated_Type (Parent_Typ) /=
2759 Designated_Type (Actual_Typ)
2760 and then not Is_Controlling_Formal (Formal)
2761 then
2762 -- This unchecked conversion is not necessary unless
2763 -- inlining is enabled, because in that case the type
2764 -- mismatch may become visible in the body about to be
2765 -- inlined.
2766
2767 Rewrite (Actual,
2768 Unchecked_Convert_To (Parent_Typ,
2769 Relocate_Node (Actual)));
d4817e3f
HK
2770 Analyze (Actual);
2771 Resolve (Actual, Parent_Typ);
2772 end if;
70482933 2773
d4817e3f
HK
2774 -- For array and record types, the parent formal type and
2775 -- derived formal type have different sizes or pragma Pack
2776 -- status.
70482933 2777
d4817e3f
HK
2778 elsif ((Is_Array_Type (Formal_Typ)
2779 and then Is_Array_Type (Parent_Typ))
2780 or else
2781 (Is_Record_Type (Formal_Typ)
2782 and then Is_Record_Type (Parent_Typ)))
2783 and then
2784 (Esize (Formal_Typ) /= Esize (Parent_Typ)
2785 or else Has_Pragma_Pack (Formal_Typ) /=
2786 Has_Pragma_Pack (Parent_Typ))
2787 then
2788 Convert (Actual, Parent_Typ);
70482933 2789 end if;
70482933 2790
d4817e3f
HK
2791 Next_Actual (Actual);
2792 Next_Formal (Formal);
2793 Next_Formal (Parent_Formal);
2794 end loop;
2795 end;
70482933
RK
2796 end if;
2797
2798 Orig_Subp := Subp;
2799 Subp := Parent_Subp;
2800 end if;
2801
8a36a0cc
AC
2802 -- Check for violation of No_Abort_Statements
2803
fbf5a39b
AC
2804 if Is_RTE (Subp, RE_Abort_Task) then
2805 Check_Restriction (No_Abort_Statements, N);
8a36a0cc
AC
2806
2807 -- Check for violation of No_Dynamic_Attachment
2808
2809 elsif RTU_Loaded (Ada_Interrupts)
2810 and then (Is_RTE (Subp, RE_Is_Reserved) or else
2811 Is_RTE (Subp, RE_Is_Attached) or else
2812 Is_RTE (Subp, RE_Current_Handler) or else
2813 Is_RTE (Subp, RE_Attach_Handler) or else
2814 Is_RTE (Subp, RE_Exchange_Handler) or else
2815 Is_RTE (Subp, RE_Detach_Handler) or else
2816 Is_RTE (Subp, RE_Reference))
2817 then
2818 Check_Restriction (No_Dynamic_Attachment, N);
fbf5a39b
AC
2819 end if;
2820
8a36a0cc
AC
2821 -- Deal with case where call is an explicit dereference
2822
c01a9391 2823 if Nkind (Name (N)) = N_Explicit_Dereference then
70482933
RK
2824
2825 -- Handle case of access to protected subprogram type
2826
f937473f 2827 if Is_Access_Protected_Subprogram_Type
d4817e3f 2828 (Base_Type (Etype (Prefix (Name (N)))))
70482933
RK
2829 then
2830 -- If this is a call through an access to protected operation,
2831 -- the prefix has the form (object'address, operation'access).
2832 -- Rewrite as a for other protected calls: the object is the
2833 -- first parameter of the list of actuals.
2834
2835 declare
2836 Call : Node_Id;
2837 Parm : List_Id;
2838 Nam : Node_Id;
2839 Obj : Node_Id;
fbf5a39b
AC
2840 Ptr : constant Node_Id := Prefix (Name (N));
2841
2842 T : constant Entity_Id :=
2843 Equivalent_Type (Base_Type (Etype (Ptr)));
2844
2845 D_T : constant Entity_Id :=
2846 Designated_Type (Base_Type (Etype (Ptr)));
70482933
RK
2847
2848 begin
f44fe430
RD
2849 Obj :=
2850 Make_Selected_Component (Loc,
2851 Prefix => Unchecked_Convert_To (T, Ptr),
2852 Selector_Name =>
2853 New_Occurrence_Of (First_Entity (T), Loc));
2854
2855 Nam :=
2856 Make_Selected_Component (Loc,
2857 Prefix => Unchecked_Convert_To (T, Ptr),
2858 Selector_Name =>
2859 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
70482933 2860
02822a92
RD
2861 Nam :=
2862 Make_Explicit_Dereference (Loc,
2863 Prefix => Nam);
70482933
RK
2864
2865 if Present (Parameter_Associations (N)) then
2866 Parm := Parameter_Associations (N);
2867 else
2868 Parm := New_List;
2869 end if;
2870
2871 Prepend (Obj, Parm);
2872
2873 if Etype (D_T) = Standard_Void_Type then
02822a92
RD
2874 Call :=
2875 Make_Procedure_Call_Statement (Loc,
2876 Name => Nam,
2877 Parameter_Associations => Parm);
70482933 2878 else
02822a92
RD
2879 Call :=
2880 Make_Function_Call (Loc,
2881 Name => Nam,
2882 Parameter_Associations => Parm);
70482933
RK
2883 end if;
2884
2885 Set_First_Named_Actual (Call, First_Named_Actual (N));
70482933
RK
2886 Set_Etype (Call, Etype (D_T));
2887
2888 -- We do not re-analyze the call to avoid infinite recursion.
2889 -- We analyze separately the prefix and the object, and set
2890 -- the checks on the prefix that would otherwise be emitted
2891 -- when resolving a call.
2892
2893 Rewrite (N, Call);
2894 Analyze (Nam);
2895 Apply_Access_Check (Nam);
2896 Analyze (Obj);
2897 return;
2898 end;
2899 end if;
2900 end if;
2901
2902 -- If this is a call to an intrinsic subprogram, then perform the
2903 -- appropriate expansion to the corresponding tree node and we
2904 -- are all done (since after that the call is gone!)
2905
98f01d53
AC
2906 -- In the case where the intrinsic is to be processed by the back end,
2907 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
2908 -- since the idea in this case is to pass the call unchanged.
d766cee3
RD
2909 -- If the intrinsic is an inherited unchecked conversion, and the
2910 -- derived type is the target type of the conversion, we must retain
2911 -- it as the return type of the expression. Otherwise the expansion
2912 -- below, which uses the parent operation, will yield the wrong type.
98f01d53 2913
70482933
RK
2914 if Is_Intrinsic_Subprogram (Subp) then
2915 Expand_Intrinsic_Call (N, Subp);
d766cee3
RD
2916
2917 if Nkind (N) = N_Unchecked_Type_Conversion
2918 and then Parent_Subp /= Orig_Subp
2919 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
2920 then
2921 Set_Etype (N, Etype (Orig_Subp));
2922 end if;
2923
70482933
RK
2924 return;
2925 end if;
2926
b29def53
AC
2927 if Ekind_In (Subp, E_Function, E_Procedure) then
2928
26a43556 2929 -- We perform two simple optimization on calls:
8dbf3473 2930
3563739b 2931 -- a) replace calls to null procedures unconditionally;
26a43556 2932
3563739b 2933 -- b) for To_Address, just do an unchecked conversion. Not only is
26a43556
AC
2934 -- this efficient, but it also avoids order of elaboration problems
2935 -- when address clauses are inlined (address expression elaborated
2936 -- at the wrong point).
2937
2938 -- We perform these optimization regardless of whether we are in the
2939 -- main unit or in a unit in the context of the main unit, to ensure
2940 -- that tree generated is the same in both cases, for Inspector use.
2941
2942 if Is_RTE (Subp, RE_To_Address) then
2943 Rewrite (N,
2944 Unchecked_Convert_To
2945 (RTE (RE_Address), Relocate_Node (First_Actual (N))));
2946 return;
2947
2948 elsif Is_Null_Procedure (Subp) then
8dbf3473
AC
2949 Rewrite (N, Make_Null_Statement (Loc));
2950 return;
2951 end if;
2952
70482933
RK
2953 if Is_Inlined (Subp) then
2954
a41ea816 2955 Inlined_Subprogram : declare
fbf5a39b
AC
2956 Bod : Node_Id;
2957 Must_Inline : Boolean := False;
2958 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
5b4994bc 2959 Scop : constant Entity_Id := Scope (Subp);
70482933 2960
a41ea816 2961 function In_Unfrozen_Instance return Boolean;
26a43556
AC
2962 -- If the subprogram comes from an instance in the same unit,
2963 -- and the instance is not yet frozen, inlining might trigger
2964 -- order-of-elaboration problems in gigi.
a41ea816
AC
2965
2966 --------------------------
2967 -- In_Unfrozen_Instance --
2968 --------------------------
2969
2970 function In_Unfrozen_Instance return Boolean is
2f1b20a9 2971 S : Entity_Id;
a41ea816
AC
2972
2973 begin
2f1b20a9 2974 S := Scop;
a41ea816
AC
2975 while Present (S)
2976 and then S /= Standard_Standard
2977 loop
2978 if Is_Generic_Instance (S)
2979 and then Present (Freeze_Node (S))
2980 and then not Analyzed (Freeze_Node (S))
2981 then
2982 return True;
2983 end if;
2984
2985 S := Scope (S);
2986 end loop;
2987
2988 return False;
2989 end In_Unfrozen_Instance;
2990
2991 -- Start of processing for Inlined_Subprogram
2992
70482933 2993 begin
2f1b20a9
ES
2994 -- Verify that the body to inline has already been seen, and
2995 -- that if the body is in the current unit the inlining does
2996 -- not occur earlier. This avoids order-of-elaboration problems
2997 -- in the back end.
2998
2999 -- This should be documented in sinfo/einfo ???
70482933 3000
fbf5a39b
AC
3001 if No (Spec)
3002 or else Nkind (Spec) /= N_Subprogram_Declaration
3003 or else No (Body_To_Inline (Spec))
70482933 3004 then
fbf5a39b
AC
3005 Must_Inline := False;
3006
26a43556
AC
3007 -- If this an inherited function that returns a private type,
3008 -- do not inline if the full view is an unconstrained array,
3009 -- because such calls cannot be inlined.
5b4994bc
AC
3010
3011 elsif Present (Orig_Subp)
3012 and then Is_Array_Type (Etype (Orig_Subp))
3013 and then not Is_Constrained (Etype (Orig_Subp))
3014 then
3015 Must_Inline := False;
3016
a41ea816 3017 elsif In_Unfrozen_Instance then
5b4994bc
AC
3018 Must_Inline := False;
3019
fbf5a39b
AC
3020 else
3021 Bod := Body_To_Inline (Spec);
3022
3023 if (In_Extended_Main_Code_Unit (N)
3024 or else In_Extended_Main_Code_Unit (Parent (N))
ac4d6407 3025 or else Has_Pragma_Inline_Always (Subp))
fbf5a39b
AC
3026 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3027 or else
3028 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3029 then
3030 Must_Inline := True;
3031
3032 -- If we are compiling a package body that is not the main
3033 -- unit, it must be for inlining/instantiation purposes,
3034 -- in which case we inline the call to insure that the same
3035 -- temporaries are generated when compiling the body by
3036 -- itself. Otherwise link errors can occur.
3037
2820d220
AC
3038 -- If the function being called is itself in the main unit,
3039 -- we cannot inline, because there is a risk of double
3040 -- elaboration and/or circularity: the inlining can make
3041 -- visible a private entity in the body of the main unit,
3042 -- that gigi will see before its sees its proper definition.
3043
fbf5a39b
AC
3044 elsif not (In_Extended_Main_Code_Unit (N))
3045 and then In_Package_Body
3046 then
2820d220 3047 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
fbf5a39b
AC
3048 end if;
3049 end if;
3050
3051 if Must_Inline then
70482933
RK
3052 Expand_Inlined_Call (N, Subp, Orig_Subp);
3053
3054 else
fbf5a39b 3055 -- Let the back end handle it
70482933
RK
3056
3057 Add_Inlined_Body (Subp);
3058
3059 if Front_End_Inlining
3060 and then Nkind (Spec) = N_Subprogram_Declaration
3061 and then (In_Extended_Main_Code_Unit (N))
3062 and then No (Body_To_Inline (Spec))
3063 and then not Has_Completion (Subp)
3064 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
70482933 3065 then
fbf5a39b 3066 Cannot_Inline
26a43556 3067 ("cannot inline& (body not seen yet)?", N, Subp);
70482933
RK
3068 end if;
3069 end if;
a41ea816 3070 end Inlined_Subprogram;
70482933
RK
3071 end if;
3072 end if;
3073
26a43556
AC
3074 -- Check for protected subprogram. This is either an intra-object call,
3075 -- or a protected function call. Protected procedure calls are rewritten
3076 -- as entry calls and handled accordingly.
70482933 3077
26a43556
AC
3078 -- In Ada 2005, this may be an indirect call to an access parameter that
3079 -- is an access_to_subprogram. In that case the anonymous type has a
3080 -- scope that is a protected operation, but the call is a regular one.
6f76a257 3081 -- In either case do not expand call if subprogram is eliminated.
c8ef728f 3082
70482933
RK
3083 Scop := Scope (Subp);
3084
3085 if Nkind (N) /= N_Entry_Call_Statement
3086 and then Is_Protected_Type (Scop)
c8ef728f 3087 and then Ekind (Subp) /= E_Subprogram_Type
6f76a257 3088 and then not Is_Eliminated (Subp)
70482933 3089 then
26a43556
AC
3090 -- If the call is an internal one, it is rewritten as a call to the
3091 -- corresponding unprotected subprogram.
70482933
RK
3092
3093 Expand_Protected_Subprogram_Call (N, Subp, Scop);
3094 end if;
3095
26a43556
AC
3096 -- Functions returning controlled objects need special attention:
3097 -- if the return type is limited, the context is an initialization
6eab5a95
AC
3098 -- and different processing applies. If the call is to a protected
3099 -- function, the expansion above will call Expand_Call recusively.
3100 -- To prevent a double attachment, check that the current call is
3101 -- not a rewriting of a protected function call.
70482933 3102
048e5cef 3103 if Needs_Finalization (Etype (Subp))
02822a92 3104 and then not Is_Inherently_Limited_Type (Etype (Subp))
6eab5a95
AC
3105 and then
3106 (No (First_Formal (Subp))
3107 or else
3108 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
70482933
RK
3109 then
3110 Expand_Ctrl_Function_Call (N);
3111 end if;
3112
26a43556
AC
3113 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3114 -- if there are optional parameters at the trailing end.
3115 -- Note: we never delete procedures for call via a pointer.
70482933
RK
3116
3117 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3118 and then Present (First_Optional_Parameter (Subp))
3119 then
3120 declare
3121 Last_Keep_Arg : Node_Id;
3122
3123 begin
26a43556
AC
3124 -- Last_Keep_Arg will hold the last actual that should be kept.
3125 -- If it remains empty at the end, it means that all parameters
3126 -- are optional.
70482933
RK
3127
3128 Last_Keep_Arg := Empty;
3129
26a43556
AC
3130 -- Find first optional parameter, must be present since we checked
3131 -- the validity of the parameter before setting it.
70482933
RK
3132
3133 Formal := First_Formal (Subp);
3134 Actual := First_Actual (N);
3135 while Formal /= First_Optional_Parameter (Subp) loop
3136 Last_Keep_Arg := Actual;
3137 Next_Formal (Formal);
3138 Next_Actual (Actual);
3139 end loop;
3140
fbf5a39b
AC
3141 -- We have Formal and Actual pointing to the first potentially
3142 -- droppable argument. We can drop all the trailing arguments
3143 -- whose actual matches the default. Note that we know that all
3144 -- remaining formals have defaults, because we checked that this
3145 -- requirement was met before setting First_Optional_Parameter.
70482933
RK
3146
3147 -- We use Fully_Conformant_Expressions to check for identity
3148 -- between formals and actuals, which may miss some cases, but
3149 -- on the other hand, this is only an optimization (if we fail
3150 -- to truncate a parameter it does not affect functionality).
3151 -- So if the default is 3 and the actual is 1+2, we consider
3152 -- them unequal, which hardly seems worrisome.
3153
3154 while Present (Formal) loop
3155 if not Fully_Conformant_Expressions
3156 (Actual, Default_Value (Formal))
3157 then
3158 Last_Keep_Arg := Actual;
3159 end if;
3160
3161 Next_Formal (Formal);
3162 Next_Actual (Actual);
3163 end loop;
3164
3165 -- If no arguments, delete entire list, this is the easy case
3166
3167 if No (Last_Keep_Arg) then
70482933
RK
3168 Set_Parameter_Associations (N, No_List);
3169 Set_First_Named_Actual (N, Empty);
3170
3171 -- Case where at the last retained argument is positional. This
3172 -- is also an easy case, since the retained arguments are already
3173 -- in the right form, and we don't need to worry about the order
3174 -- of arguments that get eliminated.
3175
3176 elsif Is_List_Member (Last_Keep_Arg) then
3177 while Present (Next (Last_Keep_Arg)) loop
ac4d6407 3178 Discard_Node (Remove_Next (Last_Keep_Arg));
70482933
RK
3179 end loop;
3180
3181 Set_First_Named_Actual (N, Empty);
3182
3183 -- This is the annoying case where the last retained argument
3184 -- is a named parameter. Since the original arguments are not
3185 -- in declaration order, we may have to delete some fairly
3186 -- random collection of arguments.
3187
3188 else
3189 declare
3190 Temp : Node_Id;
3191 Passoc : Node_Id;
fbf5a39b 3192
70482933
RK
3193 begin
3194 -- First step, remove all the named parameters from the
3195 -- list (they are still chained using First_Named_Actual
3196 -- and Next_Named_Actual, so we have not lost them!)
3197
3198 Temp := First (Parameter_Associations (N));
3199
3200 -- Case of all parameters named, remove them all
3201
3202 if Nkind (Temp) = N_Parameter_Association then
3203 while Is_Non_Empty_List (Parameter_Associations (N)) loop
3204 Temp := Remove_Head (Parameter_Associations (N));
3205 end loop;
3206
3207 -- Case of mixed positional/named, remove named parameters
3208
3209 else
3210 while Nkind (Next (Temp)) /= N_Parameter_Association loop
3211 Next (Temp);
3212 end loop;
3213
3214 while Present (Next (Temp)) loop
7888a6ae 3215 Remove (Next (Temp));
70482933
RK
3216 end loop;
3217 end if;
3218
3219 -- Now we loop through the named parameters, till we get
3220 -- to the last one to be retained, adding them to the list.
3221 -- Note that the Next_Named_Actual list does not need to be
3222 -- touched since we are only reordering them on the actual
3223 -- parameter association list.
3224
3225 Passoc := Parent (First_Named_Actual (N));
3226 loop
3227 Temp := Relocate_Node (Passoc);
3228 Append_To
3229 (Parameter_Associations (N), Temp);
3230 exit when
3231 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
3232 Passoc := Parent (Next_Named_Actual (Passoc));
3233 end loop;
3234
3235 Set_Next_Named_Actual (Temp, Empty);
3236
3237 loop
3238 Temp := Next_Named_Actual (Passoc);
3239 exit when No (Temp);
3240 Set_Next_Named_Actual
3241 (Passoc, Next_Named_Actual (Parent (Temp)));
70482933
RK
3242 end loop;
3243 end;
811c6a85 3244
70482933
RK
3245 end if;
3246 end;
3247 end if;
70482933
RK
3248 end Expand_Call;
3249
3250 --------------------------
3251 -- Expand_Inlined_Call --
3252 --------------------------
3253
3254 procedure Expand_Inlined_Call
3255 (N : Node_Id;
3256 Subp : Entity_Id;
3257 Orig_Subp : Entity_Id)
3258 is
fbf5a39b
AC
3259 Loc : constant Source_Ptr := Sloc (N);
3260 Is_Predef : constant Boolean :=
3261 Is_Predefined_File_Name
3262 (Unit_File_Name (Get_Source_Unit (Subp)));
3263 Orig_Bod : constant Node_Id :=
3264 Body_To_Inline (Unit_Declaration_Node (Subp));
3265
70482933
RK
3266 Blk : Node_Id;
3267 Bod : Node_Id;
3268 Decl : Node_Id;
c8ef728f 3269 Decls : constant List_Id := New_List;
70482933
RK
3270 Exit_Lab : Entity_Id := Empty;
3271 F : Entity_Id;
3272 A : Node_Id;
3273 Lab_Decl : Node_Id;
3274 Lab_Id : Node_Id;
3275 New_A : Node_Id;
3276 Num_Ret : Int := 0;
70482933
RK
3277 Ret_Type : Entity_Id;
3278 Targ : Node_Id;
c8ef728f 3279 Targ1 : Node_Id;
70482933
RK
3280 Temp : Entity_Id;
3281 Temp_Typ : Entity_Id;
3282
3e2399ba
AC
3283 Return_Object : Entity_Id := Empty;
3284 -- Entity in declaration in an extended_return_statement
3285
c8ef728f
ES
3286 Is_Unc : constant Boolean :=
3287 Is_Array_Type (Etype (Subp))
3288 and then not Is_Constrained (Etype (Subp));
26a43556
AC
3289 -- If the type returned by the function is unconstrained and the call
3290 -- can be inlined, special processing is required.
c8ef728f 3291
70482933 3292 procedure Make_Exit_Label;
26a43556 3293 -- Build declaration for exit label to be used in Return statements,
c12beea0
RD
3294 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3295 -- declaration). Does nothing if Exit_Lab already set.
70482933
RK
3296
3297 function Process_Formals (N : Node_Id) return Traverse_Result;
26a43556
AC
3298 -- Replace occurrence of a formal with the corresponding actual, or the
3299 -- thunk generated for it.
70482933 3300
fbf5a39b 3301 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
26a43556
AC
3302 -- If the call being expanded is that of an internal subprogram, set the
3303 -- sloc of the generated block to that of the call itself, so that the
3304 -- expansion is skipped by the "next" command in gdb.
fbf5a39b 3305 -- Same processing for a subprogram in a predefined file, e.g.
26a43556
AC
3306 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
3307 -- simplify our own development.
fbf5a39b 3308
70482933
RK
3309 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3310 -- If the function body is a single expression, replace call with
3311 -- expression, else insert block appropriately.
3312
3313 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3314 -- If procedure body has no local variables, inline body without
02822a92 3315 -- creating block, otherwise rewrite call with block.
70482933 3316
5453d5bd
AC
3317 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
3318 -- Determine whether a formal parameter is used only once in Orig_Bod
3319
70482933
RK
3320 ---------------------
3321 -- Make_Exit_Label --
3322 ---------------------
3323
3324 procedure Make_Exit_Label is
c12beea0 3325 Lab_Ent : Entity_Id;
70482933 3326 begin
70482933 3327 if No (Exit_Lab) then
c12beea0
RD
3328 Lab_Ent := Make_Temporary (Loc, 'L');
3329 Lab_Id := New_Reference_To (Lab_Ent, Loc);
70482933 3330 Exit_Lab := Make_Label (Loc, Lab_Id);
70482933
RK
3331 Lab_Decl :=
3332 Make_Implicit_Label_Declaration (Loc,
c12beea0 3333 Defining_Identifier => Lab_Ent,
70482933
RK
3334 Label_Construct => Exit_Lab);
3335 end if;
3336 end Make_Exit_Label;
3337
3338 ---------------------
3339 -- Process_Formals --
3340 ---------------------
3341
3342 function Process_Formals (N : Node_Id) return Traverse_Result is
3343 A : Entity_Id;
3344 E : Entity_Id;
3345 Ret : Node_Id;
3346
3347 begin
3348 if Is_Entity_Name (N)
3349 and then Present (Entity (N))
3350 then
3351 E := Entity (N);
3352
3353 if Is_Formal (E)
3354 and then Scope (E) = Subp
3355 then
3356 A := Renamed_Object (E);
3357
02822a92
RD
3358 -- Rewrite the occurrence of the formal into an occurrence of
3359 -- the actual. Also establish visibility on the proper view of
3360 -- the actual's subtype for the body's context (if the actual's
3361 -- subtype is private at the call point but its full view is
3362 -- visible to the body, then the inlined tree here must be
3363 -- analyzed with the full view).
3364
70482933
RK
3365 if Is_Entity_Name (A) then
3366 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
02822a92 3367 Check_Private_View (N);
70482933
RK
3368
3369 elsif Nkind (A) = N_Defining_Identifier then
3370 Rewrite (N, New_Occurrence_Of (A, Loc));
02822a92 3371 Check_Private_View (N);
70482933 3372
d766cee3
RD
3373 -- Numeric literal
3374
3375 else
70482933
RK
3376 Rewrite (N, New_Copy (A));
3377 end if;
3378 end if;
3e2399ba
AC
3379 return Skip;
3380
3381 elsif Is_Entity_Name (N)
9f5b6c7f 3382 and then Present (Return_Object)
3e2399ba
AC
3383 and then Chars (N) = Chars (Return_Object)
3384 then
3385 -- Occurrence within an extended return statement. The return
3386 -- object is local to the body been inlined, and thus the generic
3387 -- copy is not analyzed yet, so we match by name, and replace it
3388 -- with target of call.
3389
3390 if Nkind (Targ) = N_Defining_Identifier then
3391 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3392 else
3393 Rewrite (N, New_Copy_Tree (Targ));
3394 end if;
70482933
RK
3395
3396 return Skip;
3397
d766cee3 3398 elsif Nkind (N) = N_Simple_Return_Statement then
70482933
RK
3399 if No (Expression (N)) then
3400 Make_Exit_Label;
d766cee3 3401 Rewrite (N,
3e2399ba 3402 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
70482933
RK
3403
3404 else
3405 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3406 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3407 then
fbf5a39b 3408 -- Function body is a single expression. No need for
70482933 3409 -- exit label.
fbf5a39b 3410
70482933
RK
3411 null;
3412
3413 else
3414 Num_Ret := Num_Ret + 1;
3415 Make_Exit_Label;
3416 end if;
3417
3418 -- Because of the presence of private types, the views of the
3419 -- expression and the context may be different, so place an
3420 -- unchecked conversion to the context type to avoid spurious
8fc789c8 3421 -- errors, e.g. when the expression is a numeric literal and
70482933
RK
3422 -- the context is private. If the expression is an aggregate,
3423 -- use a qualified expression, because an aggregate is not a
3424 -- legal argument of a conversion.
3425
ac4d6407 3426 if Nkind_In (Expression (N), N_Aggregate, N_Null) then
70482933
RK
3427 Ret :=
3428 Make_Qualified_Expression (Sloc (N),
3429 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3430 Expression => Relocate_Node (Expression (N)));
3431 else
3432 Ret :=
3433 Unchecked_Convert_To
3434 (Ret_Type, Relocate_Node (Expression (N)));
3435 end if;
3436
3437 if Nkind (Targ) = N_Defining_Identifier then
3438 Rewrite (N,
3439 Make_Assignment_Statement (Loc,
3440 Name => New_Occurrence_Of (Targ, Loc),
3441 Expression => Ret));
3442 else
3443 Rewrite (N,
3444 Make_Assignment_Statement (Loc,
3445 Name => New_Copy (Targ),
3446 Expression => Ret));
3447 end if;
3448
3449 Set_Assignment_OK (Name (N));
3450
3451 if Present (Exit_Lab) then
3452 Insert_After (N,
3453 Make_Goto_Statement (Loc,
3454 Name => New_Copy (Lab_Id)));
3455 end if;
3456 end if;
3457
3458 return OK;
3459
3e2399ba
AC
3460 elsif Nkind (N) = N_Extended_Return_Statement then
3461
3462 -- An extended return becomes a block whose first statement is
3463 -- the assignment of the initial expression of the return object
3464 -- to the target of the call itself.
3465
3466 declare
3467 Return_Decl : constant Entity_Id :=
3468 First (Return_Object_Declarations (N));
3469 Assign : Node_Id;
3470
3471 begin
3472 Return_Object := Defining_Identifier (Return_Decl);
3473
3474 if Present (Expression (Return_Decl)) then
3475 if Nkind (Targ) = N_Defining_Identifier then
3476 Assign :=
3477 Make_Assignment_Statement (Loc,
3478 Name => New_Occurrence_Of (Targ, Loc),
3479 Expression => Expression (Return_Decl));
3480 else
3481 Assign :=
3482 Make_Assignment_Statement (Loc,
3483 Name => New_Copy (Targ),
3484 Expression => Expression (Return_Decl));
3485 end if;
3486
3487 Set_Assignment_OK (Name (Assign));
3488 Prepend (Assign,
3489 Statements (Handled_Statement_Sequence (N)));
3490 end if;
3491
3492 Rewrite (N,
3493 Make_Block_Statement (Loc,
3494 Handled_Statement_Sequence =>
3495 Handled_Statement_Sequence (N)));
3496
3497 return OK;
3498 end;
3499
fbf5a39b
AC
3500 -- Remove pragma Unreferenced since it may refer to formals that
3501 -- are not visible in the inlined body, and in any case we will
3502 -- not be posting warnings on the inlined body so it is unneeded.
3503
3504 elsif Nkind (N) = N_Pragma
1923d2d6 3505 and then Pragma_Name (N) = Name_Unreferenced
fbf5a39b
AC
3506 then
3507 Rewrite (N, Make_Null_Statement (Sloc (N)));
3508 return OK;
3509
70482933
RK
3510 else
3511 return OK;
3512 end if;
3513 end Process_Formals;
3514
3515 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3516
fbf5a39b
AC
3517 ------------------
3518 -- Process_Sloc --
3519 ------------------
3520
3521 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3522 begin
3523 if not Debug_Generated_Code then
3524 Set_Sloc (Nod, Sloc (N));
3525 Set_Comes_From_Source (Nod, False);
3526 end if;
3527
3528 return OK;
3529 end Process_Sloc;
3530
3531 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3532
70482933
RK
3533 ---------------------------
3534 -- Rewrite_Function_Call --
3535 ---------------------------
3536
3537 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
fbf5a39b
AC
3538 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3539 Fst : constant Node_Id := First (Statements (HSS));
70482933
RK
3540
3541 begin
70482933
RK
3542 -- Optimize simple case: function body is a single return statement,
3543 -- which has been expanded into an assignment.
3544
3545 if Is_Empty_List (Declarations (Blk))
3546 and then Nkind (Fst) = N_Assignment_Statement
3547 and then No (Next (Fst))
3548 then
3549
3550 -- The function call may have been rewritten as the temporary
3551 -- that holds the result of the call, in which case remove the
3552 -- now useless declaration.
3553
3554 if Nkind (N) = N_Identifier
3555 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3556 then
3557 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3558 end if;
3559
3560 Rewrite (N, Expression (Fst));
3561
3562 elsif Nkind (N) = N_Identifier
3563 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3564 then
98f01d53 3565 -- The block assigns the result of the call to the temporary
70482933
RK
3566
3567 Insert_After (Parent (Entity (N)), Blk);
3568
3569 elsif Nkind (Parent (N)) = N_Assignment_Statement
c8ef728f
ES
3570 and then
3571 (Is_Entity_Name (Name (Parent (N)))
3572 or else
3573 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3574 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
70482933 3575 then
fbf5a39b 3576 -- Replace assignment with the block
70482933 3577
30c20106
AC
3578 declare
3579 Original_Assignment : constant Node_Id := Parent (N);
7324bf49
AC
3580
3581 begin
2f1b20a9
ES
3582 -- Preserve the original assignment node to keep the complete
3583 -- assignment subtree consistent enough for Analyze_Assignment
3584 -- to proceed (specifically, the original Lhs node must still
3585 -- have an assignment statement as its parent).
7324bf49 3586
2f1b20a9
ES
3587 -- We cannot rely on Original_Node to go back from the block
3588 -- node to the assignment node, because the assignment might
3589 -- already be a rewrite substitution.
30c20106 3590
7324bf49 3591 Discard_Node (Relocate_Node (Original_Assignment));
30c20106
AC
3592 Rewrite (Original_Assignment, Blk);
3593 end;
70482933
RK
3594
3595 elsif Nkind (Parent (N)) = N_Object_Declaration then
3596 Set_Expression (Parent (N), Empty);
3597 Insert_After (Parent (N), Blk);
c8ef728f
ES
3598
3599 elsif Is_Unc then
3600 Insert_Before (Parent (N), Blk);
70482933
RK
3601 end if;
3602 end Rewrite_Function_Call;
3603
3604 ----------------------------
3605 -- Rewrite_Procedure_Call --
3606 ----------------------------
3607
3608 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
fbf5a39b 3609 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
70482933 3610 begin
02822a92
RD
3611 -- If there is a transient scope for N, this will be the scope of the
3612 -- actions for N, and the statements in Blk need to be within this
3613 -- scope. For example, they need to have visibility on the constant
3614 -- declarations created for the formals.
3615
3616 -- If N needs no transient scope, and if there are no declarations in
3617 -- the inlined body, we can do a little optimization and insert the
3618 -- statements for the body directly after N, and rewrite N to a
3619 -- null statement, instead of rewriting N into a full-blown block
3620 -- statement.
3621
3622 if not Scope_Is_Transient
3623 and then Is_Empty_List (Declarations (Blk))
3624 then
70482933
RK
3625 Insert_List_After (N, Statements (HSS));
3626 Rewrite (N, Make_Null_Statement (Loc));
3627 else
3628 Rewrite (N, Blk);
3629 end if;
3630 end Rewrite_Procedure_Call;
3631
5453d5bd
AC
3632 -------------------------
3633 -- Formal_Is_Used_Once --
02822a92 3634 -------------------------
5453d5bd
AC
3635
3636 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
3637 Use_Counter : Int := 0;
3638
3639 function Count_Uses (N : Node_Id) return Traverse_Result;
3640 -- Traverse the tree and count the uses of the formal parameter.
3641 -- In this case, for optimization purposes, we do not need to
3642 -- continue the traversal once more than one use is encountered.
3643
cc335f43
AC
3644 ----------------
3645 -- Count_Uses --
3646 ----------------
3647
5453d5bd
AC
3648 function Count_Uses (N : Node_Id) return Traverse_Result is
3649 begin
5453d5bd
AC
3650 -- The original node is an identifier
3651
3652 if Nkind (N) = N_Identifier
3653 and then Present (Entity (N))
3654
2f1b20a9 3655 -- Original node's entity points to the one in the copied body
5453d5bd
AC
3656
3657 and then Nkind (Entity (N)) = N_Identifier
3658 and then Present (Entity (Entity (N)))
3659
3660 -- The entity of the copied node is the formal parameter
3661
3662 and then Entity (Entity (N)) = Formal
3663 then
3664 Use_Counter := Use_Counter + 1;
3665
3666 if Use_Counter > 1 then
3667
3668 -- Denote more than one use and abandon the traversal
3669
3670 Use_Counter := 2;
3671 return Abandon;
3672
3673 end if;
3674 end if;
3675
3676 return OK;
3677 end Count_Uses;
3678
3679 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
3680
3681 -- Start of processing for Formal_Is_Used_Once
3682
3683 begin
5453d5bd
AC
3684 Count_Formal_Uses (Orig_Bod);
3685 return Use_Counter = 1;
5453d5bd
AC
3686 end Formal_Is_Used_Once;
3687
70482933
RK
3688 -- Start of processing for Expand_Inlined_Call
3689
3690 begin
8dbf3473 3691
f44fe430
RD
3692 -- Check for an illegal attempt to inline a recursive procedure. If the
3693 -- subprogram has parameters this is detected when trying to supply a
3694 -- binding for parameters that already have one. For parameterless
3695 -- subprograms this must be done explicitly.
3696
3697 if In_Open_Scopes (Subp) then
3698 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
3699 Set_Is_Inlined (Subp, False);
3700 return;
3701 end if;
3702
2ccf2fb3
ES
3703 if Nkind (Orig_Bod) = N_Defining_Identifier
3704 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
3705 then
8a45b58c
RD
3706 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3707 -- can be replaced with calls to the renamed entity directly, because
3708 -- the subprograms are subtype conformant. If the renamed subprogram
3709 -- is an inherited operation, we must redo the expansion because
3710 -- implicit conversions may be needed. Similarly, if the renamed
3711 -- entity is inlined, expand the call for further optimizations.
70482933
RK
3712
3713 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
f44fe430 3714
676e8420 3715 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
f44fe430
RD
3716 Expand_Call (N);
3717 end if;
3718
70482933
RK
3719 return;
3720 end if;
3721
3722 -- Use generic machinery to copy body of inlined subprogram, as if it
3723 -- were an instantiation, resetting source locations appropriately, so
3724 -- that nested inlined calls appear in the main unit.
3725
3726 Save_Env (Subp, Empty);
fbf5a39b 3727 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
70482933 3728
fbf5a39b 3729 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
70482933
RK
3730 Blk :=
3731 Make_Block_Statement (Loc,
3732 Declarations => Declarations (Bod),
3733 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
3734
3735 if No (Declarations (Bod)) then
3736 Set_Declarations (Blk, New_List);
3737 end if;
3738
c8ef728f 3739 -- For the unconstrained case, capture the name of the local
02822a92
RD
3740 -- variable that holds the result. This must be the first declaration
3741 -- in the block, because its bounds cannot depend on local variables.
3742 -- Otherwise there is no way to declare the result outside of the
3743 -- block. Needless to say, in general the bounds will depend on the
3744 -- actuals in the call.
c8ef728f
ES
3745
3746 if Is_Unc then
02822a92 3747 Targ1 := Defining_Identifier (First (Declarations (Blk)));
c8ef728f
ES
3748 end if;
3749
98f01d53 3750 -- If this is a derived function, establish the proper return type
70482933
RK
3751
3752 if Present (Orig_Subp)
3753 and then Orig_Subp /= Subp
3754 then
3755 Ret_Type := Etype (Orig_Subp);
3756 else
3757 Ret_Type := Etype (Subp);
3758 end if;
3759
70482933
RK
3760 -- Create temporaries for the actuals that are expressions, or that
3761 -- are scalars and require copying to preserve semantics.
3762
2f1b20a9
ES
3763 F := First_Formal (Subp);
3764 A := First_Actual (N);
70482933 3765 while Present (F) loop
70482933 3766 if Present (Renamed_Object (F)) then
2f1b20a9 3767 Error_Msg_N ("cannot inline call to recursive subprogram", N);
70482933
RK
3768 return;
3769 end if;
3770
3771 -- If the argument may be a controlling argument in a call within
f44fe430
RD
3772 -- the inlined body, we must preserve its classwide nature to insure
3773 -- that dynamic dispatching take place subsequently. If the formal
3774 -- has a constraint it must be preserved to retain the semantics of
3775 -- the body.
70482933
RK
3776
3777 if Is_Class_Wide_Type (Etype (F))
3778 or else (Is_Access_Type (Etype (F))
3779 and then
3780 Is_Class_Wide_Type (Designated_Type (Etype (F))))
3781 then
3782 Temp_Typ := Etype (F);
3783
3784 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3785 and then Etype (F) /= Base_Type (Etype (F))
3786 then
3787 Temp_Typ := Etype (F);
3788
3789 else
3790 Temp_Typ := Etype (A);
3791 end if;
3792
5b4994bc
AC
3793 -- If the actual is a simple name or a literal, no need to
3794 -- create a temporary, object can be used directly.
70482933 3795
7888a6ae
GD
3796 -- If the actual is a literal and the formal has its address taken,
3797 -- we cannot pass the literal itself as an argument, so its value
3798 -- must be captured in a temporary.
3799
fbf5a39b
AC
3800 if (Is_Entity_Name (A)
3801 and then
3802 (not Is_Scalar_Type (Etype (A))
3803 or else Ekind (Entity (A)) = E_Enumeration_Literal))
3804
5453d5bd
AC
3805 -- When the actual is an identifier and the corresponding formal
3806 -- is used only once in the original body, the formal can be
3807 -- substituted directly with the actual parameter.
3808
3809 or else (Nkind (A) = N_Identifier
3810 and then Formal_Is_Used_Once (F))
3811
7888a6ae 3812 or else
ac4d6407
RD
3813 (Nkind_In (A, N_Real_Literal,
3814 N_Integer_Literal,
3815 N_Character_Literal)
3816 and then not Address_Taken (F))
70482933 3817 then
fbf5a39b
AC
3818 if Etype (F) /= Etype (A) then
3819 Set_Renamed_Object
3820 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3821 else
3822 Set_Renamed_Object (F, A);
3823 end if;
3824
3825 else
c12beea0 3826 Temp := Make_Temporary (Loc, 'C');
70482933
RK
3827
3828 -- If the actual for an in/in-out parameter is a view conversion,
3829 -- make it into an unchecked conversion, given that an untagged
3830 -- type conversion is not a proper object for a renaming.
fbf5a39b 3831
70482933
RK
3832 -- In-out conversions that involve real conversions have already
3833 -- been transformed in Expand_Actuals.
3834
3835 if Nkind (A) = N_Type_Conversion
fbf5a39b 3836 and then Ekind (F) /= E_In_Parameter
70482933 3837 then
02822a92
RD
3838 New_A :=
3839 Make_Unchecked_Type_Conversion (Loc,
3840 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3841 Expression => Relocate_Node (Expression (A)));
70482933
RK
3842
3843 elsif Etype (F) /= Etype (A) then
3844 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3845 Temp_Typ := Etype (F);
3846
3847 else
3848 New_A := Relocate_Node (A);
3849 end if;
3850
3851 Set_Sloc (New_A, Sloc (N));
3852
02822a92
RD
3853 -- If the actual has a by-reference type, it cannot be copied, so
3854 -- its value is captured in a renaming declaration. Otherwise
7888a6ae 3855 -- declare a local constant initialized with the actual.
02822a92 3856
4a3b249c
RD
3857 -- We also use a renaming declaration for expressions of an array
3858 -- type that is not bit-packed, both for efficiency reasons and to
3859 -- respect the semantics of the call: in most cases the original
3860 -- call will pass the parameter by reference, and thus the inlined
3861 -- code will have the same semantics.
bafc9e1d 3862
70482933
RK
3863 if Ekind (F) = E_In_Parameter
3864 and then not Is_Limited_Type (Etype (A))
02822a92 3865 and then not Is_Tagged_Type (Etype (A))
bafc9e1d
AC
3866 and then
3867 (not Is_Array_Type (Etype (A))
f66d46ec 3868 or else not Is_Object_Reference (A)
bafc9e1d 3869 or else Is_Bit_Packed_Array (Etype (A)))
70482933
RK
3870 then
3871 Decl :=
3872 Make_Object_Declaration (Loc,
3873 Defining_Identifier => Temp,
3874 Constant_Present => True,
3875 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3876 Expression => New_A);
3877 else
3878 Decl :=
3879 Make_Object_Renaming_Declaration (Loc,
3880 Defining_Identifier => Temp,
3881 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3882 Name => New_A);
3883 end if;
3884
c8ef728f 3885 Append (Decl, Decls);
70482933 3886 Set_Renamed_Object (F, Temp);
70482933
RK
3887 end if;
3888
3889 Next_Formal (F);
3890 Next_Actual (A);
3891 end loop;
3892
3893 -- Establish target of function call. If context is not assignment or
3894 -- declaration, create a temporary as a target. The declaration for
3895 -- the temporary may be subsequently optimized away if the body is a
3896 -- single expression, or if the left-hand side of the assignment is
c8ef728f 3897 -- simple enough, i.e. an entity or an explicit dereference of one.
70482933
RK
3898
3899 if Ekind (Subp) = E_Function then
3900 if Nkind (Parent (N)) = N_Assignment_Statement
3901 and then Is_Entity_Name (Name (Parent (N)))
3902 then
3903 Targ := Name (Parent (N));
3904
c8ef728f
ES
3905 elsif Nkind (Parent (N)) = N_Assignment_Statement
3906 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3907 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3908 then
3909 Targ := Name (Parent (N));
3910
3e2399ba
AC
3911 elsif Nkind (Parent (N)) = N_Object_Declaration
3912 and then Is_Limited_Type (Etype (Subp))
3913 then
3914 Targ := Defining_Identifier (Parent (N));
3915
70482933 3916 else
98f01d53 3917 -- Replace call with temporary and create its declaration
70482933 3918
c12beea0 3919 Temp := Make_Temporary (Loc, 'C');
758c442c 3920 Set_Is_Internal (Temp);
70482933 3921
30783513 3922 -- For the unconstrained case, the generated temporary has the
4a3b249c
RD
3923 -- same constrained declaration as the result variable. It may
3924 -- eventually be possible to remove that temporary and use the
3925 -- result variable directly.
c8ef728f
ES
3926
3927 if Is_Unc then
3928 Decl :=
3929 Make_Object_Declaration (Loc,
3930 Defining_Identifier => Temp,
3931 Object_Definition =>
3932 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3933
3934 Replace_Formals (Decl);
3935
3936 else
3937 Decl :=
3938 Make_Object_Declaration (Loc,
3939 Defining_Identifier => Temp,
3940 Object_Definition =>
3941 New_Occurrence_Of (Ret_Type, Loc));
3942
3943 Set_Etype (Temp, Ret_Type);
3944 end if;
70482933
RK
3945
3946 Set_No_Initialization (Decl);
c8ef728f 3947 Append (Decl, Decls);
70482933
RK
3948 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3949 Targ := Temp;
3950 end if;
3951 end if;
3952
c8ef728f
ES
3953 Insert_Actions (N, Decls);
3954
98f01d53 3955 -- Traverse the tree and replace formals with actuals or their thunks.
70482933
RK
3956 -- Attach block to tree before analysis and rewriting.
3957
3958 Replace_Formals (Blk);
3959 Set_Parent (Blk, N);
3960
fbf5a39b
AC
3961 if not Comes_From_Source (Subp)
3962 or else Is_Predef
3963 then
3964 Reset_Slocs (Blk);
3965 end if;
3966
70482933
RK
3967 if Present (Exit_Lab) then
3968
3969 -- If the body was a single expression, the single return statement
3970 -- and the corresponding label are useless.
3971
3972 if Num_Ret = 1
3973 and then
3974 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3975 N_Goto_Statement
3976 then
3977 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3978 else
3979 Append (Lab_Decl, (Declarations (Blk)));
3980 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3981 end if;
3982 end if;
3983
3984 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
4a3b249c 3985 -- conflicting private views that Gigi would ignore. If this is a
fbf5a39b
AC
3986 -- predefined unit, analyze with checks off, as is done in the non-
3987 -- inlined run-time units.
70482933
RK
3988
3989 declare
3990 I_Flag : constant Boolean := In_Inlined_Body;
3991
3992 begin
3993 In_Inlined_Body := True;
fbf5a39b
AC
3994
3995 if Is_Predef then
3996 declare
3997 Style : constant Boolean := Style_Check;
3998 begin
3999 Style_Check := False;
4000 Analyze (Blk, Suppress => All_Checks);
4001 Style_Check := Style;
4002 end;
4003
4004 else
4005 Analyze (Blk);
4006 end if;
4007
70482933
RK
4008 In_Inlined_Body := I_Flag;
4009 end;
4010
4011 if Ekind (Subp) = E_Procedure then
4012 Rewrite_Procedure_Call (N, Blk);
4013 else
4014 Rewrite_Function_Call (N, Blk);
c8ef728f
ES
4015
4016 -- For the unconstrained case, the replacement of the call has been
4017 -- made prior to the complete analysis of the generated declarations.
4018 -- Propagate the proper type now.
4019
4020 if Is_Unc then
4021 if Nkind (N) = N_Identifier then
4022 Set_Etype (N, Etype (Entity (N)));
4023 else
4024 Set_Etype (N, Etype (Targ1));
4025 end if;
4026 end if;
70482933
RK
4027 end if;
4028
4029 Restore_Env;
4030
98f01d53 4031 -- Cleanup mapping between formals and actuals for other expansions
70482933
RK
4032
4033 F := First_Formal (Subp);
70482933
RK
4034 while Present (F) loop
4035 Set_Renamed_Object (F, Empty);
4036 Next_Formal (F);
4037 end loop;
4038 end Expand_Inlined_Call;
4039
4040 ----------------------------
4041 -- Expand_N_Function_Call --
4042 ----------------------------
4043
4044 procedure Expand_N_Function_Call (N : Node_Id) is
70482933 4045 begin
ac4d6407 4046 Expand_Call (N);
c986420e 4047
4a3b249c
RD
4048 -- If the return value of a foreign compiled function is VAX Float, then
4049 -- expand the return (adjusts the location of the return value on
4050 -- Alpha/VMS, no-op everywhere else).
612c5336 4051 -- Comes_From_Source intercepts recursive expansion.
2acde248 4052
c986420e
DR
4053 if Vax_Float (Etype (N))
4054 and then Nkind (N) = N_Function_Call
c986420e
DR
4055 and then Present (Name (N))
4056 and then Present (Entity (Name (N)))
4057 and then Has_Foreign_Convention (Entity (Name (N)))
612c5336 4058 and then Comes_From_Source (Parent (N))
c986420e
DR
4059 then
4060 Expand_Vax_Foreign_Return (N);
4061 end if;
70482933
RK
4062 end Expand_N_Function_Call;
4063
4064 ---------------------------------------
4065 -- Expand_N_Procedure_Call_Statement --
4066 ---------------------------------------
4067
4068 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4069 begin
4070 Expand_Call (N);
4071 end Expand_N_Procedure_Call_Statement;
4072
4073 ------------------------------
4074 -- Expand_N_Subprogram_Body --
4075 ------------------------------
4076
4a3b249c
RD
4077 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4078 -- by the back-end.
70482933 4079
7888a6ae 4080 -- Add dummy push/pop label nodes at start and end to clear any local
4a3b249c 4081 -- exception indications if local-exception-to-goto optimization is active.
7888a6ae 4082
f44fe430
RD
4083 -- Add return statement if last statement in body is not a return statement
4084 -- (this makes things easier on Gigi which does not want to have to handle
4085 -- a missing return).
70482933
RK
4086
4087 -- Add call to Activate_Tasks if body is a task activator
4088
4089 -- Deal with possible detection of infinite recursion
4090
4091 -- Eliminate body completely if convention stubbed
4092
4093 -- Encode entity names within body, since we will not need to reference
4094 -- these entities any longer in the front end.
4095
4096 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4097
c9a4817d 4098 -- Reset Pure indication if any parameter has root type System.Address
199c6a10
AC
4099 -- or has any parameters of limited types, where limited means that the
4100 -- run-time view is limited (i.e. the full type is limited).
c9a4817d 4101
12e0c41c
AC
4102 -- Wrap thread body
4103
70482933
RK
4104 procedure Expand_N_Subprogram_Body (N : Node_Id) is
4105 Loc : constant Source_Ptr := Sloc (N);
4106 H : constant Node_Id := Handled_Statement_Sequence (N);
c9a4817d 4107 Body_Id : Entity_Id;
70482933 4108 Except_H : Node_Id;
70482933 4109 L : List_Id;
70f91180 4110 Spec_Id : Entity_Id;
70482933
RK
4111
4112 procedure Add_Return (S : List_Id);
4113 -- Append a return statement to the statement sequence S if the last
4114 -- statement is not already a return or a goto statement. Note that
4a3b249c
RD
4115 -- the latter test is not critical, it does not matter if we add a few
4116 -- extra returns, since they get eliminated anyway later on.
70482933
RK
4117
4118 ----------------
4119 -- Add_Return --
4120 ----------------
4121
4122 procedure Add_Return (S : List_Id) is
7888a6ae
GD
4123 Last_Stm : Node_Id;
4124 Loc : Source_Ptr;
12e0c41c
AC
4125
4126 begin
7888a6ae
GD
4127 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4128 -- not relevant in this context since they are not executable.
12e0c41c 4129
7888a6ae
GD
4130 Last_Stm := Last (S);
4131 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
4132 Prev (Last_Stm);
4133 end loop;
12e0c41c 4134
7888a6ae 4135 -- Now insert return unless last statement is a transfer
12e0c41c 4136
7888a6ae 4137 if not Is_Transfer (Last_Stm) then
12e0c41c 4138
7888a6ae
GD
4139 -- The source location for the return is the end label of the
4140 -- procedure if present. Otherwise use the sloc of the last
4141 -- statement in the list. If the list comes from a generated
4142 -- exception handler and we are not debugging generated code,
4143 -- all the statements within the handler are made invisible
4144 -- to the debugger.
12e0c41c 4145
7888a6ae
GD
4146 if Nkind (Parent (S)) = N_Exception_Handler
4147 and then not Comes_From_Source (Parent (S))
4148 then
4149 Loc := Sloc (Last_Stm);
12e0c41c 4150
7888a6ae
GD
4151 elsif Present (End_Label (H)) then
4152 Loc := Sloc (End_Label (H));
12e0c41c 4153
7888a6ae
GD
4154 else
4155 Loc := Sloc (Last_Stm);
4156 end if;
12e0c41c 4157
5334d18f
BD
4158 declare
4159 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
4160
4161 begin
4a3b249c
RD
4162 -- Append return statement, and set analyzed manually. We can't
4163 -- call Analyze on this return since the scope is wrong.
5334d18f
BD
4164
4165 -- Note: it almost works to push the scope and then do the
4a3b249c 4166 -- Analyze call, but something goes wrong in some weird cases
5334d18f
BD
4167 -- and it is not worth worrying about ???
4168
4169 Append_To (S, Rtn);
4170 Set_Analyzed (Rtn);
4171
4172 -- Call _Postconditions procedure if appropriate. We need to
4173 -- do this explicitly because we did not analyze the generated
4174 -- return statement above, so the call did not get inserted.
4175
4176 if Ekind (Spec_Id) = E_Procedure
4177 and then Has_Postconditions (Spec_Id)
4178 then
4179 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
4180 Insert_Action (Rtn,
4181 Make_Procedure_Call_Statement (Loc,
4182 Name =>
4183 New_Reference_To (Postcondition_Proc (Spec_Id), Loc)));
4184 end if;
4185 end;
12e0c41c 4186 end if;
7888a6ae 4187 end Add_Return;
12e0c41c 4188
70482933
RK
4189 -- Start of processing for Expand_N_Subprogram_Body
4190
4191 begin
4a3b249c
RD
4192 -- Set L to either the list of declarations if present, or to the list
4193 -- of statements if no declarations are present. This is used to insert
4194 -- new stuff at the start.
70482933
RK
4195
4196 if Is_Non_Empty_List (Declarations (N)) then
4197 L := Declarations (N);
4198 else
7888a6ae
GD
4199 L := Statements (H);
4200 end if;
4201
4202 -- If local-exception-to-goto optimization active, insert dummy push
4203 -- statements at start, and dummy pop statements at end.
4204
4205 if (Debug_Flag_Dot_G
4206 or else Restriction_Active (No_Exception_Propagation))
4207 and then Is_Non_Empty_List (L)
4208 then
4209 declare
4210 FS : constant Node_Id := First (L);
4211 FL : constant Source_Ptr := Sloc (FS);
4212 LS : Node_Id;
4213 LL : Source_Ptr;
4214
4215 begin
4216 -- LS points to either last statement, if statements are present
4217 -- or to the last declaration if there are no statements present.
4218 -- It is the node after which the pop's are generated.
4219
4220 if Is_Non_Empty_List (Statements (H)) then
4221 LS := Last (Statements (H));
4222 else
4223 LS := Last (L);
4224 end if;
4225
4226 LL := Sloc (LS);
4227
4228 Insert_List_Before_And_Analyze (FS, New_List (
4229 Make_Push_Constraint_Error_Label (FL),
4230 Make_Push_Program_Error_Label (FL),
4231 Make_Push_Storage_Error_Label (FL)));
4232
4233 Insert_List_After_And_Analyze (LS, New_List (
4234 Make_Pop_Constraint_Error_Label (LL),
4235 Make_Pop_Program_Error_Label (LL),
4236 Make_Pop_Storage_Error_Label (LL)));
4237 end;
70482933
RK
4238 end if;
4239
70482933
RK
4240 -- Find entity for subprogram
4241
c9a4817d
RD
4242 Body_Id := Defining_Entity (N);
4243
70482933
RK
4244 if Present (Corresponding_Spec (N)) then
4245 Spec_Id := Corresponding_Spec (N);
4246 else
c9a4817d
RD
4247 Spec_Id := Body_Id;
4248 end if;
4249
7888a6ae
GD
4250 -- Need poll on entry to subprogram if polling enabled. We only do this
4251 -- for non-empty subprograms, since it does not seem necessary to poll
4a3b249c 4252 -- for a dummy null subprogram.
c885d7a1
AC
4253
4254 if Is_Non_Empty_List (L) then
4a3b249c
RD
4255
4256 -- Do not add a polling call if the subprogram is to be inlined by
4257 -- the back-end, to avoid repeated calls with multiple inlinings.
4258
c885d7a1
AC
4259 if Is_Inlined (Spec_Id)
4260 and then Front_End_Inlining
4261 and then Optimization_Level > 1
4262 then
4263 null;
4264 else
4265 Generate_Poll_Call (First (L));
4266 end if;
4267 end if;
4268
4a3b249c
RD
4269 -- If this is a Pure function which has any parameters whose root type
4270 -- is System.Address, reset the Pure indication, since it will likely
4271 -- cause incorrect code to be generated as the parameter is probably
4272 -- a pointer, and the fact that the same pointer is passed does not mean
4273 -- that the same value is being referenced.
91b1417d
AC
4274
4275 -- Note that if the programmer gave an explicit Pure_Function pragma,
4276 -- then we believe the programmer, and leave the subprogram Pure.
4277
4a3b249c
RD
4278 -- This code should probably be at the freeze point, so that it happens
4279 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
4280 -- semantic tree has Is_Pure set properly ???
c9a4817d
RD
4281
4282 if Is_Pure (Spec_Id)
4283 and then Is_Subprogram (Spec_Id)
4284 and then not Has_Pragma_Pure_Function (Spec_Id)
4285 then
4286 declare
2f1b20a9 4287 F : Entity_Id;
c9a4817d
RD
4288
4289 begin
2f1b20a9 4290 F := First_Formal (Spec_Id);
c9a4817d 4291 while Present (F) loop
e5dc610e 4292 if Is_Descendent_Of_Address (Etype (F))
199c6a10
AC
4293
4294 -- Note that this test is being made in the body of the
4295 -- subprogram, not the spec, so we are testing the full
4296 -- type for being limited here, as required.
4297
e5dc610e
AC
4298 or else Is_Limited_Type (Etype (F))
4299 then
c9a4817d
RD
4300 Set_Is_Pure (Spec_Id, False);
4301
4302 if Spec_Id /= Body_Id then
4303 Set_Is_Pure (Body_Id, False);
4304 end if;
4305
4306 exit;
4307 end if;
4308
4309 Next_Formal (F);
4310 end loop;
4311 end;
70482933
RK
4312 end if;
4313
4314 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
4315
4316 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
4317 declare
2f1b20a9 4318 F : Entity_Id;
70482933
RK
4319
4320 begin
70482933
RK
4321 -- Loop through formals
4322
2f1b20a9 4323 F := First_Formal (Spec_Id);
70482933
RK
4324 while Present (F) loop
4325 if Is_Scalar_Type (Etype (F))
4326 and then Ekind (F) = E_Out_Parameter
4327 then
70f91180
RD
4328 Check_Restriction (No_Default_Initialization, F);
4329
02822a92
RD
4330 -- Insert the initialization. We turn off validity checks
4331 -- for this assignment, since we do not want any check on
4332 -- the initial value itself (which may well be invalid).
4333
70482933
RK
4334 Insert_Before_And_Analyze (First (L),
4335 Make_Assignment_Statement (Loc,
02822a92 4336 Name => New_Occurrence_Of (F, Loc),
70f91180 4337 Expression => Get_Simple_Init_Val (Etype (F), N)),
02822a92 4338 Suppress => Validity_Check);
70482933
RK
4339 end if;
4340
4341 Next_Formal (F);
4342 end loop;
70482933
RK
4343 end;
4344 end if;
4345
4346 -- Clear out statement list for stubbed procedure
4347
4348 if Present (Corresponding_Spec (N)) then
4349 Set_Elaboration_Flag (N, Spec_Id);
4350
4351 if Convention (Spec_Id) = Convention_Stubbed
4352 or else Is_Eliminated (Spec_Id)
4353 then
4354 Set_Declarations (N, Empty_List);
4355 Set_Handled_Statement_Sequence (N,
4356 Make_Handled_Sequence_Of_Statements (Loc,
4357 Statements => New_List (
4358 Make_Null_Statement (Loc))));
4359 return;
4360 end if;
4361 end if;
4362
70f91180
RD
4363 -- Create a set of discriminals for the next protected subprogram body
4364
4365 if Is_List_Member (N)
4366 and then Present (Parent (List_Containing (N)))
4367 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
4368 and then Present (Next_Protected_Operation (N))
4369 then
4370 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
4371 end if;
4372
4a3b249c
RD
4373 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
4374 -- subprograms with no specs are not frozen.
70482933
RK
4375
4376 declare
4377 Typ : constant Entity_Id := Etype (Spec_Id);
4378 Utyp : constant Entity_Id := Underlying_Type (Typ);
4379
4380 begin
4381 if not Acts_As_Spec (N)
4382 and then Nkind (Parent (Parent (Spec_Id))) /=
4383 N_Subprogram_Body_Stub
4384 then
4385 null;
4386
02822a92 4387 elsif Is_Inherently_Limited_Type (Typ) then
70482933
RK
4388 Set_Returns_By_Ref (Spec_Id);
4389
048e5cef 4390 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
70482933
RK
4391 Set_Returns_By_Ref (Spec_Id);
4392 end if;
4393 end;
4394
4a3b249c
RD
4395 -- For a procedure, we add a return for all possible syntactic ends of
4396 -- the subprogram.
70482933 4397
b29def53 4398 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
70482933
RK
4399 Add_Return (Statements (H));
4400
4401 if Present (Exception_Handlers (H)) then
4402 Except_H := First_Non_Pragma (Exception_Handlers (H));
70482933
RK
4403 while Present (Except_H) loop
4404 Add_Return (Statements (Except_H));
4405 Next_Non_Pragma (Except_H);
4406 end loop;
4407 end if;
4408
98f01d53
AC
4409 -- For a function, we must deal with the case where there is at least
4410 -- one missing return. What we do is to wrap the entire body of the
4411 -- function in a block:
70482933
RK
4412
4413 -- begin
4414 -- ...
4415 -- end;
4416
4417 -- becomes
4418
4419 -- begin
4420 -- begin
4421 -- ...
4422 -- end;
4423
4424 -- raise Program_Error;
4425 -- end;
4426
4a3b249c
RD
4427 -- This approach is necessary because the raise must be signalled to the
4428 -- caller, not handled by any local handler (RM 6.4(11)).
70482933 4429
4a3b249c
RD
4430 -- Note: we do not need to analyze the constructed sequence here, since
4431 -- it has no handler, and an attempt to analyze the handled statement
4432 -- sequence twice is risky in various ways (e.g. the issue of expanding
4433 -- cleanup actions twice).
70482933
RK
4434
4435 elsif Has_Missing_Return (Spec_Id) then
4436 declare
4437 Hloc : constant Source_Ptr := Sloc (H);
4438 Blok : constant Node_Id :=
4439 Make_Block_Statement (Hloc,
4440 Handled_Statement_Sequence => H);
4441 Rais : constant Node_Id :=
07fc65c4
GB
4442 Make_Raise_Program_Error (Hloc,
4443 Reason => PE_Missing_Return);
70482933
RK
4444
4445 begin
4446 Set_Handled_Statement_Sequence (N,
4447 Make_Handled_Sequence_Of_Statements (Hloc,
4448 Statements => New_List (Blok, Rais)));
4449
7888a6ae 4450 Push_Scope (Spec_Id);
70482933
RK
4451 Analyze (Blok);
4452 Analyze (Rais);
4453 Pop_Scope;
4454 end;
4455 end if;
4456
70482933
RK
4457 -- If subprogram contains a parameterless recursive call, then we may
4458 -- have an infinite recursion, so see if we can generate code to check
4459 -- for this possibility if storage checks are not suppressed.
4460
4461 if Ekind (Spec_Id) = E_Procedure
4462 and then Has_Recursive_Call (Spec_Id)
4463 and then not Storage_Checks_Suppressed (Spec_Id)
4464 then
4465 Detect_Infinite_Recursion (N, Spec_Id);
4466 end if;
4467
70482933
RK
4468 -- Set to encode entity names in package body before gigi is called
4469
4470 Qualify_Entity_Names (N);
4471 end Expand_N_Subprogram_Body;
4472
4473 -----------------------------------
4474 -- Expand_N_Subprogram_Body_Stub --
4475 -----------------------------------
4476
4477 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
4478 begin
4479 if Present (Corresponding_Body (N)) then
4480 Expand_N_Subprogram_Body (
4481 Unit_Declaration_Node (Corresponding_Body (N)));
4482 end if;
70482933
RK
4483 end Expand_N_Subprogram_Body_Stub;
4484
4485 -------------------------------------
4486 -- Expand_N_Subprogram_Declaration --
4487 -------------------------------------
4488
70482933
RK
4489 -- If the declaration appears within a protected body, it is a private
4490 -- operation of the protected type. We must create the corresponding
4491 -- protected subprogram an associated formals. For a normal protected
4492 -- operation, this is done when expanding the protected type declaration.
4493
758c442c
GD
4494 -- If the declaration is for a null procedure, emit null body
4495
70482933 4496 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
fbf5a39b
AC
4497 Loc : constant Source_Ptr := Sloc (N);
4498 Subp : constant Entity_Id := Defining_Entity (N);
4499 Scop : constant Entity_Id := Scope (Subp);
4500 Prot_Decl : Node_Id;
4501 Prot_Bod : Node_Id;
4502 Prot_Id : Entity_Id;
70482933
RK
4503
4504 begin
2f1b20a9
ES
4505 -- Deal with case of protected subprogram. Do not generate protected
4506 -- operation if operation is flagged as eliminated.
70482933
RK
4507
4508 if Is_List_Member (N)
4509 and then Present (Parent (List_Containing (N)))
4510 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
4511 and then Is_Protected_Type (Scop)
4512 then
6871ba5f
AC
4513 if No (Protected_Body_Subprogram (Subp))
4514 and then not Is_Eliminated (Subp)
4515 then
fbf5a39b 4516 Prot_Decl :=
70482933
RK
4517 Make_Subprogram_Declaration (Loc,
4518 Specification =>
4519 Build_Protected_Sub_Specification
2f1b20a9 4520 (N, Scop, Unprotected_Mode));
70482933
RK
4521
4522 -- The protected subprogram is declared outside of the protected
4523 -- body. Given that the body has frozen all entities so far, we
fbf5a39b 4524 -- analyze the subprogram and perform freezing actions explicitly.
19590d70
GD
4525 -- including the generation of an explicit freeze node, to ensure
4526 -- that gigi has the proper order of elaboration.
fbf5a39b
AC
4527 -- If the body is a subunit, the insertion point is before the
4528 -- stub in the parent.
70482933
RK
4529
4530 Prot_Bod := Parent (List_Containing (N));
4531
4532 if Nkind (Parent (Prot_Bod)) = N_Subunit then
4533 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
4534 end if;
4535
fbf5a39b
AC
4536 Insert_Before (Prot_Bod, Prot_Decl);
4537 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
19590d70 4538 Set_Has_Delayed_Freeze (Prot_Id);
70482933 4539
7888a6ae 4540 Push_Scope (Scope (Scop));
fbf5a39b 4541 Analyze (Prot_Decl);
19590d70 4542 Insert_Actions (N, Freeze_Entity (Prot_Id, Loc));
fbf5a39b 4543 Set_Protected_Body_Subprogram (Subp, Prot_Id);
47bfea3a
AC
4544
4545 -- Create protected operation as well. Even though the operation
4546 -- is only accessible within the body, it is possible to make it
4547 -- available outside of the protected object by using 'Access to
3d923671 4548 -- provide a callback, so build protected version in all cases.
47bfea3a
AC
4549
4550 Prot_Decl :=
3d923671
AC
4551 Make_Subprogram_Declaration (Loc,
4552 Specification =>
4553 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
47bfea3a
AC
4554 Insert_Before (Prot_Bod, Prot_Decl);
4555 Analyze (Prot_Decl);
4556
70482933
RK
4557 Pop_Scope;
4558 end if;
758c442c 4559
e1f3cb58
AC
4560 -- Ada 2005 (AI-348): Generate body for a null procedure.
4561 -- In most cases this is superfluous because calls to it
4562 -- will be automatically inlined, but we definitely need
4563 -- the body if preconditions for the procedure are present.
02822a92 4564
758c442c
GD
4565 elsif Nkind (Specification (N)) = N_Procedure_Specification
4566 and then Null_Present (Specification (N))
4567 then
4568 declare
e1f3cb58 4569 Bod : constant Node_Id := Body_To_Inline (N);
d6533e74 4570
758c442c 4571 begin
e1f3cb58
AC
4572 Set_Has_Completion (Subp, False);
4573 Append_Freeze_Action (Subp, Bod);
c73ae90f 4574
e1f3cb58
AC
4575 -- The body now contains raise statements, so calls to it will
4576 -- not be inlined.
c73ae90f 4577
e1f3cb58 4578 Set_Is_Inlined (Subp, False);
758c442c 4579 end;
70482933
RK
4580 end if;
4581 end Expand_N_Subprogram_Declaration;
4582
4583 ---------------------------------------
4584 -- Expand_Protected_Object_Reference --
4585 ---------------------------------------
4586
4587 function Expand_Protected_Object_Reference
4588 (N : Node_Id;
02822a92 4589 Scop : Entity_Id) return Node_Id
70482933
RK
4590 is
4591 Loc : constant Source_Ptr := Sloc (N);
4592 Corr : Entity_Id;
4593 Rec : Node_Id;
4594 Param : Entity_Id;
4595 Proc : Entity_Id;
4596
4597 begin
02822a92
RD
4598 Rec :=
4599 Make_Identifier (Loc,
4600 Chars => Name_uObject);
70482933
RK
4601 Set_Etype (Rec, Corresponding_Record_Type (Scop));
4602
2f1b20a9
ES
4603 -- Find enclosing protected operation, and retrieve its first parameter,
4604 -- which denotes the enclosing protected object. If the enclosing
4605 -- operation is an entry, we are immediately within the protected body,
4606 -- and we can retrieve the object from the service entries procedure. A
16b05213 4607 -- barrier function has the same signature as an entry. A barrier
2f1b20a9
ES
4608 -- function is compiled within the protected object, but unlike
4609 -- protected operations its never needs locks, so that its protected
4610 -- body subprogram points to itself.
70482933
RK
4611
4612 Proc := Current_Scope;
70482933
RK
4613 while Present (Proc)
4614 and then Scope (Proc) /= Scop
4615 loop
4616 Proc := Scope (Proc);
4617 end loop;
4618
4619 Corr := Protected_Body_Subprogram (Proc);
4620
4621 if No (Corr) then
4622
4623 -- Previous error left expansion incomplete.
4624 -- Nothing to do on this call.
4625
4626 return Empty;
4627 end if;
4628
4629 Param :=
4630 Defining_Identifier
4631 (First (Parameter_Specifications (Parent (Corr))));
4632
4633 if Is_Subprogram (Proc)
4634 and then Proc /= Corr
4635 then
98f01d53 4636 -- Protected function or procedure
70482933
RK
4637
4638 Set_Entity (Rec, Param);
4639
2f1b20a9
ES
4640 -- Rec is a reference to an entity which will not be in scope when
4641 -- the call is reanalyzed, and needs no further analysis.
70482933
RK
4642
4643 Set_Analyzed (Rec);
4644
4645 else
2f1b20a9
ES
4646 -- Entry or barrier function for entry body. The first parameter of
4647 -- the entry body procedure is pointer to the object. We create a
4648 -- local variable of the proper type, duplicating what is done to
4649 -- define _object later on.
70482933
RK
4650
4651 declare
c12beea0
RD
4652 Decls : List_Id;
4653 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
fbf5a39b 4654
70482933
RK
4655 begin
4656 Decls := New_List (
4657 Make_Full_Type_Declaration (Loc,
4658 Defining_Identifier => Obj_Ptr,
4659 Type_Definition =>
4660 Make_Access_To_Object_Definition (Loc,
4661 Subtype_Indication =>
4662 New_Reference_To
c12beea0 4663 (Corresponding_Record_Type (Scop), Loc))));
70482933
RK
4664
4665 Insert_Actions (N, Decls);
4666 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
4667
4668 Rec :=
4669 Make_Explicit_Dereference (Loc,
4670 Unchecked_Convert_To (Obj_Ptr,
4671 New_Occurrence_Of (Param, Loc)));
4672
2f1b20a9 4673 -- Analyze new actual. Other actuals in calls are already analyzed
7888a6ae 4674 -- and the list of actuals is not reanalyzed after rewriting.
70482933
RK
4675
4676 Set_Parent (Rec, N);
4677 Analyze (Rec);
4678 end;
4679 end if;
4680
4681 return Rec;
4682 end Expand_Protected_Object_Reference;
4683
4684 --------------------------------------
4685 -- Expand_Protected_Subprogram_Call --
4686 --------------------------------------
4687
4688 procedure Expand_Protected_Subprogram_Call
4689 (N : Node_Id;
4690 Subp : Entity_Id;
4691 Scop : Entity_Id)
4692 is
4693 Rec : Node_Id;
4694
4695 begin
4696 -- If the protected object is not an enclosing scope, this is
4697 -- an inter-object function call. Inter-object procedure
4698 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
4699 -- The call is intra-object only if the subprogram being
4700 -- called is in the protected body being compiled, and if the
4701 -- protected object in the call is statically the enclosing type.
4702 -- The object may be an component of some other data structure,
4703 -- in which case this must be handled as an inter-object call.
4704
4705 if not In_Open_Scopes (Scop)
4706 or else not Is_Entity_Name (Name (N))
4707 then
4708 if Nkind (Name (N)) = N_Selected_Component then
4709 Rec := Prefix (Name (N));
4710
4711 else
4712 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
4713 Rec := Prefix (Prefix (Name (N)));
4714 end if;
4715
4716 Build_Protected_Subprogram_Call (N,
4717 Name => New_Occurrence_Of (Subp, Sloc (N)),
4718 Rec => Convert_Concurrent (Rec, Etype (Rec)),
4719 External => True);
4720
4721 else
4722 Rec := Expand_Protected_Object_Reference (N, Scop);
4723
4724 if No (Rec) then
4725 return;
4726 end if;
4727
4728 Build_Protected_Subprogram_Call (N,
4729 Name => Name (N),
4730 Rec => Rec,
4731 External => False);
4732
4733 end if;
4734
70482933
RK
4735 -- If it is a function call it can appear in elaboration code and
4736 -- the called entity must be frozen here.
4737
4738 if Ekind (Subp) = E_Function then
4739 Freeze_Expression (Name (N));
4740 end if;
811c6a85
AC
4741
4742 -- Analyze and resolve the new call. The actuals have already been
b0159fbe 4743 -- resolved, but expansion of a function call will add extra actuals
811c6a85
AC
4744 -- if needed. Analysis of a procedure call already includes resolution.
4745
4746 Analyze (N);
4747
4748 if Ekind (Subp) = E_Function then
4749 Resolve (N, Etype (Subp));
4750 end if;
70482933
RK
4751 end Expand_Protected_Subprogram_Call;
4752
02822a92
RD
4753 --------------------------------
4754 -- Is_Build_In_Place_Function --
4755 --------------------------------
70482933 4756
02822a92
RD
4757 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
4758 begin
5087048c
AC
4759 -- This function is called from Expand_Subtype_From_Expr during
4760 -- semantic analysis, even when expansion is off. In those cases
4761 -- the build_in_place expansion will not take place.
b0256cb6
AC
4762
4763 if not Expander_Active then
4764 return False;
4765 end if;
4766
02822a92 4767 -- For now we test whether E denotes a function or access-to-function
f937473f
RD
4768 -- type whose result subtype is inherently limited. Later this test may
4769 -- be revised to allow composite nonlimited types. Functions with a
4770 -- foreign convention or whose result type has a foreign convention
02822a92
RD
4771 -- never qualify.
4772
b29def53 4773 if Ekind_In (E, E_Function, E_Generic_Function)
02822a92
RD
4774 or else (Ekind (E) = E_Subprogram_Type
4775 and then Etype (E) /= Standard_Void_Type)
4776 then
f937473f
RD
4777 -- Note: If you have Convention (C) on an inherently limited type,
4778 -- you're on your own. That is, the C code will have to be carefully
4779 -- written to know about the Ada conventions.
4780
02822a92
RD
4781 if Has_Foreign_Convention (E)
4782 or else Has_Foreign_Convention (Etype (E))
3ca505dc 4783 then
02822a92 4784 return False;
c8ef728f 4785
2a31c32b
AC
4786 -- In Ada 2005 all functions with an inherently limited return type
4787 -- must be handled using a build-in-place profile, including the case
4788 -- of a function with a limited interface result, where the function
4789 -- may return objects of nonlimited descendants.
7888a6ae 4790
02822a92
RD
4791 else
4792 return Is_Inherently_Limited_Type (Etype (E))
f937473f
RD
4793 and then Ada_Version >= Ada_05
4794 and then not Debug_Flag_Dot_L;
c8ef728f
ES
4795 end if;
4796
02822a92
RD
4797 else
4798 return False;
4799 end if;
4800 end Is_Build_In_Place_Function;
f4d379b8 4801
02822a92
RD
4802 -------------------------------------
4803 -- Is_Build_In_Place_Function_Call --
4804 -------------------------------------
f4d379b8 4805
02822a92
RD
4806 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
4807 Exp_Node : Node_Id := N;
4808 Function_Id : Entity_Id;
f4d379b8 4809
02822a92 4810 begin
19590d70
GD
4811 -- Step past qualification or unchecked conversion (the latter can occur
4812 -- in cases of calls to 'Input).
4813
ac4d6407
RD
4814 if Nkind_In
4815 (Exp_Node, N_Qualified_Expression, N_Unchecked_Type_Conversion)
19590d70 4816 then
02822a92
RD
4817 Exp_Node := Expression (N);
4818 end if;
758c442c 4819
02822a92
RD
4820 if Nkind (Exp_Node) /= N_Function_Call then
4821 return False;
3ca505dc 4822
02822a92
RD
4823 else
4824 if Is_Entity_Name (Name (Exp_Node)) then
4825 Function_Id := Entity (Name (Exp_Node));
758c442c 4826
02822a92
RD
4827 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
4828 Function_Id := Etype (Name (Exp_Node));
4829 end if;
758c442c 4830
02822a92
RD
4831 return Is_Build_In_Place_Function (Function_Id);
4832 end if;
4833 end Is_Build_In_Place_Function_Call;
758c442c 4834
02822a92
RD
4835 -----------------------
4836 -- Freeze_Subprogram --
4837 -----------------------
758c442c 4838
02822a92
RD
4839 procedure Freeze_Subprogram (N : Node_Id) is
4840 Loc : constant Source_Ptr := Sloc (N);
3ca505dc 4841
02822a92
RD
4842 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
4843 -- (Ada 2005): Register a predefined primitive in all the secondary
4844 -- dispatch tables of its primitive type.
3ca505dc 4845
f4d379b8
HK
4846 ----------------------------------
4847 -- Register_Predefined_DT_Entry --
4848 ----------------------------------
4849
4850 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
4851 Iface_DT_Ptr : Elmt_Id;
02822a92 4852 Tagged_Typ : Entity_Id;
f4d379b8 4853 Thunk_Id : Entity_Id;
7888a6ae 4854 Thunk_Code : Node_Id;
f4d379b8
HK
4855
4856 begin
02822a92 4857 Tagged_Typ := Find_Dispatching_Type (Prim);
f4d379b8 4858
02822a92 4859 if No (Access_Disp_Table (Tagged_Typ))
ce2b6ba5 4860 or else not Has_Interfaces (Tagged_Typ)
c8ef728f 4861 or else not RTE_Available (RE_Interface_Tag)
f937473f 4862 or else Restriction_Active (No_Dispatching_Calls)
f4d379b8
HK
4863 then
4864 return;
4865 end if;
4866
1923d2d6
JM
4867 -- Skip the first two access-to-dispatch-table pointers since they
4868 -- leads to the primary dispatch table (predefined DT and user
4869 -- defined DT). We are only concerned with the secondary dispatch
4870 -- table pointers. Note that the access-to- dispatch-table pointer
4871 -- corresponds to the first implemented interface retrieved below.
f4d379b8 4872
02822a92 4873 Iface_DT_Ptr :=
1923d2d6 4874 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
f937473f 4875
7888a6ae
GD
4876 while Present (Iface_DT_Ptr)
4877 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
4878 loop
ac4d6407 4879 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
d766cee3 4880 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7888a6ae
GD
4881
4882 if Present (Thunk_Code) then
ac4d6407 4883 Insert_Actions_After (N, New_List (
7888a6ae
GD
4884 Thunk_Code,
4885
4886 Build_Set_Predefined_Prim_Op_Address (Loc,
1923d2d6
JM
4887 Tag_Node =>
4888 New_Reference_To (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7888a6ae
GD
4889 Position => DT_Position (Prim),
4890 Address_Node =>
70f91180 4891 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
1923d2d6
JM
4892 Make_Attribute_Reference (Loc,
4893 Prefix => New_Reference_To (Thunk_Id, Loc),
4894 Attribute_Name => Name_Unrestricted_Access))),
ac4d6407
RD
4895
4896 Build_Set_Predefined_Prim_Op_Address (Loc,
1923d2d6
JM
4897 Tag_Node =>
4898 New_Reference_To
4899 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
4900 Loc),
ac4d6407
RD
4901 Position => DT_Position (Prim),
4902 Address_Node =>
70f91180 4903 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
1923d2d6
JM
4904 Make_Attribute_Reference (Loc,
4905 Prefix => New_Reference_To (Prim, Loc),
4906 Attribute_Name => Name_Unrestricted_Access)))));
7888a6ae 4907 end if;
f4d379b8 4908
1923d2d6
JM
4909 -- Skip the tag of the predefined primitives dispatch table
4910
4911 Next_Elmt (Iface_DT_Ptr);
4912 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
4913
4914 -- Skip the tag of the no-thunks dispatch table
4915
4916 Next_Elmt (Iface_DT_Ptr);
4917 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
4918
4919 -- Skip the tag of the predefined primitives no-thunks dispatch
4920 -- table
4921
ac4d6407
RD
4922 Next_Elmt (Iface_DT_Ptr);
4923 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
4924
f4d379b8 4925 Next_Elmt (Iface_DT_Ptr);
f4d379b8
HK
4926 end loop;
4927 end Register_Predefined_DT_Entry;
4928
7888a6ae 4929 -- Local variables
3ca505dc 4930
7888a6ae 4931 Subp : constant Entity_Id := Entity (N);
3ca505dc 4932
ac4d6407
RD
4933 -- Start of processing for Freeze_Subprogram
4934
7888a6ae 4935 begin
d766cee3
RD
4936 -- We suppress the initialization of the dispatch table entry when
4937 -- VM_Target because the dispatching mechanism is handled internally
4938 -- by the VM.
4939
4940 if Is_Dispatching_Operation (Subp)
4941 and then not Is_Abstract_Subprogram (Subp)
4942 and then Present (DTC_Entity (Subp))
4943 and then Present (Scope (DTC_Entity (Subp)))
1f110335 4944 and then Tagged_Type_Expansion
d766cee3
RD
4945 and then not Restriction_Active (No_Dispatching_Calls)
4946 and then RTE_Available (RE_Tag)
4947 then
7888a6ae 4948 declare
d766cee3 4949 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
c8ef728f 4950
7888a6ae 4951 begin
8fc789c8 4952 -- Handle private overridden primitives
c8ef728f 4953
d766cee3
RD
4954 if not Is_CPP_Class (Typ) then
4955 Check_Overriding_Operation (Subp);
7888a6ae 4956 end if;
c8ef728f 4957
d766cee3
RD
4958 -- We assume that imported CPP primitives correspond with objects
4959 -- whose constructor is in the CPP side; therefore we don't need
4960 -- to generate code to register them in the dispatch table.
c8ef728f 4961
d766cee3
RD
4962 if Is_CPP_Class (Typ) then
4963 null;
3ca505dc 4964
d766cee3
RD
4965 -- Handle CPP primitives found in derivations of CPP_Class types.
4966 -- These primitives must have been inherited from some parent, and
4967 -- there is no need to register them in the dispatch table because
4968 -- Build_Inherit_Prims takes care of the initialization of these
4969 -- slots.
3ca505dc 4970
d766cee3
RD
4971 elsif Is_Imported (Subp)
4972 and then (Convention (Subp) = Convention_CPP
4973 or else Convention (Subp) = Convention_C)
4974 then
4975 null;
4976
4977 -- Generate code to register the primitive in non statically
4978 -- allocated dispatch tables
4979
4980 elsif not Static_Dispatch_Tables
4981 or else not
4982 Is_Library_Level_Tagged_Type (Scope (DTC_Entity (Subp)))
4983 then
4984 -- When a primitive is frozen, enter its name in its dispatch
4985 -- table slot.
f4d379b8 4986
d766cee3 4987 if not Is_Interface (Typ)
ce2b6ba5 4988 or else Present (Interface_Alias (Subp))
d766cee3
RD
4989 then
4990 if Is_Predefined_Dispatching_Operation (Subp) then
4991 Register_Predefined_DT_Entry (Subp);
7888a6ae 4992 end if;
d766cee3 4993
991395ab
AC
4994 Insert_Actions_After (N,
4995 Register_Primitive (Loc, Prim => Subp));
7888a6ae
GD
4996 end if;
4997 end if;
4998 end;
70482933
RK
4999 end if;
5000
7888a6ae
GD
5001 -- Mark functions that return by reference. Note that it cannot be part
5002 -- of the normal semantic analysis of the spec since the underlying
5003 -- returned type may not be known yet (for private types).
70482933 5004
d766cee3
RD
5005 declare
5006 Typ : constant Entity_Id := Etype (Subp);
5007 Utyp : constant Entity_Id := Underlying_Type (Typ);
5008 begin
5009 if Is_Inherently_Limited_Type (Typ) then
5010 Set_Returns_By_Ref (Subp);
048e5cef 5011 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
d766cee3
RD
5012 Set_Returns_By_Ref (Subp);
5013 end if;
5014 end;
70482933
RK
5015 end Freeze_Subprogram;
5016
8dbf3473
AC
5017 -----------------------
5018 -- Is_Null_Procedure --
5019 -----------------------
5020
5021 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
5022 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
5023
5024 begin
5025 if Ekind (Subp) /= E_Procedure then
5026 return False;
5027
5028 -- Check if this is a declared null procedure
5029
5030 elsif Nkind (Decl) = N_Subprogram_Declaration then
e1f3cb58
AC
5031 if not Null_Present (Specification (Decl)) then
5032 return False;
8dbf3473
AC
5033
5034 elsif No (Body_To_Inline (Decl)) then
5035 return False;
5036
5037 -- Check if the body contains only a null statement, followed by
5038 -- the return statement added during expansion.
5039
5040 else
5041 declare
5042 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
5043
5044 Stat : Node_Id;
5045 Stat2 : Node_Id;
5046
5047 begin
5048 if Nkind (Orig_Bod) /= N_Subprogram_Body then
5049 return False;
5050 else
327503f1
JM
5051 -- We must skip SCIL nodes because they are currently
5052 -- implemented as special N_Null_Statement nodes.
5053
8dbf3473 5054 Stat :=
327503f1 5055 First_Non_SCIL_Node
8dbf3473 5056 (Statements (Handled_Statement_Sequence (Orig_Bod)));
327503f1 5057 Stat2 := Next_Non_SCIL_Node (Stat);
8dbf3473
AC
5058
5059 return
e1f3cb58
AC
5060 Is_Empty_List (Declarations (Orig_Bod))
5061 and then Nkind (Stat) = N_Null_Statement
5062 and then
8dbf3473
AC
5063 (No (Stat2)
5064 or else
5065 (Nkind (Stat2) = N_Simple_Return_Statement
5066 and then No (Next (Stat2))));
5067 end if;
5068 end;
5069 end if;
5070
5071 else
5072 return False;
5073 end if;
5074 end Is_Null_Procedure;
5075
02822a92
RD
5076 -------------------------------------------
5077 -- Make_Build_In_Place_Call_In_Allocator --
5078 -------------------------------------------
5079
5080 procedure Make_Build_In_Place_Call_In_Allocator
5081 (Allocator : Node_Id;
5082 Function_Call : Node_Id)
5083 is
5084 Loc : Source_Ptr;
5085 Func_Call : Node_Id := Function_Call;
5086 Function_Id : Entity_Id;
5087 Result_Subt : Entity_Id;
5088 Acc_Type : constant Entity_Id := Etype (Allocator);
5089 New_Allocator : Node_Id;
5090 Return_Obj_Access : Entity_Id;
5091
5092 begin
19590d70
GD
5093 -- Step past qualification or unchecked conversion (the latter can occur
5094 -- in cases of calls to 'Input).
5095
ac4d6407
RD
5096 if Nkind_In (Func_Call,
5097 N_Qualified_Expression,
5098 N_Unchecked_Type_Conversion)
19590d70 5099 then
02822a92
RD
5100 Func_Call := Expression (Func_Call);
5101 end if;
5102
fdce4bb7
JM
5103 -- If the call has already been processed to add build-in-place actuals
5104 -- then return. This should not normally occur in an allocator context,
5105 -- but we add the protection as a defensive measure.
5106
5107 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5108 return;
5109 end if;
5110
5111 -- Mark the call as processed as a build-in-place call
5112
5113 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5114
02822a92
RD
5115 Loc := Sloc (Function_Call);
5116
5117 if Is_Entity_Name (Name (Func_Call)) then
5118 Function_Id := Entity (Name (Func_Call));
5119
5120 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5121 Function_Id := Etype (Name (Func_Call));
5122
5123 else
5124 raise Program_Error;
5125 end if;
5126
5127 Result_Subt := Etype (Function_Id);
5128
f937473f
RD
5129 -- When the result subtype is constrained, the return object must be
5130 -- allocated on the caller side, and access to it is passed to the
5131 -- function.
02822a92 5132
7888a6ae
GD
5133 -- Here and in related routines, we must examine the full view of the
5134 -- type, because the view at the point of call may differ from that
5135 -- that in the function body, and the expansion mechanism depends on
5136 -- the characteristics of the full view.
5137
5138 if Is_Constrained (Underlying_Type (Result_Subt)) then
02822a92 5139
f937473f
RD
5140 -- Replace the initialized allocator of form "new T'(Func (...))"
5141 -- with an uninitialized allocator of form "new T", where T is the
5142 -- result subtype of the called function. The call to the function
5143 -- is handled separately further below.
02822a92 5144
f937473f 5145 New_Allocator :=
fad0600d
AC
5146 Make_Allocator (Loc,
5147 Expression => New_Reference_To (Result_Subt, Loc));
5148 Set_No_Initialization (New_Allocator);
5149
5150 -- Copy attributes to new allocator. Note that the new allocator
5151 -- logically comes from source if the original one did, so copy the
5152 -- relevant flag. This ensures proper treatment of the restriction
5153 -- No_Implicit_Heap_Allocations in this case.
02822a92 5154
fad0600d 5155 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
f937473f 5156 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
fad0600d 5157 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
02822a92 5158
f937473f 5159 Rewrite (Allocator, New_Allocator);
02822a92 5160
f937473f 5161 -- Create a new access object and initialize it to the result of the
b0b7b57d 5162 -- new uninitialized allocator. Note: we do not use Allocator as the
f104fca1
AC
5163 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
5164 -- as this would create a sort of infinite "recursion".
02822a92 5165
f104fca1 5166 Return_Obj_Access := Make_Temporary (Loc, 'R');
f937473f
RD
5167 Set_Etype (Return_Obj_Access, Acc_Type);
5168
5169 Insert_Action (Allocator,
5170 Make_Object_Declaration (Loc,
5171 Defining_Identifier => Return_Obj_Access,
5172 Object_Definition => New_Reference_To (Acc_Type, Loc),
5173 Expression => Relocate_Node (Allocator)));
5174
7888a6ae
GD
5175 -- When the function has a controlling result, an allocation-form
5176 -- parameter must be passed indicating that the caller is allocating
5177 -- the result object. This is needed because such a function can be
5178 -- called as a dispatching operation and must be treated similarly
5179 -- to functions with unconstrained result subtypes.
5180
5181 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5182 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5183
5184 Add_Final_List_Actual_To_Build_In_Place_Call
5185 (Func_Call, Function_Id, Acc_Type);
5186
5187 Add_Task_Actuals_To_Build_In_Place_Call
5188 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
5189
f937473f
RD
5190 -- Add an implicit actual to the function call that provides access
5191 -- to the allocated object. An unchecked conversion to the (specific)
5192 -- result subtype of the function is inserted to handle cases where
5193 -- the access type of the allocator has a class-wide designated type.
5194
f937473f
RD
5195 Add_Access_Actual_To_Build_In_Place_Call
5196 (Func_Call,
5197 Function_Id,
5198 Make_Unchecked_Type_Conversion (Loc,
5199 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5200 Expression =>
5201 Make_Explicit_Dereference (Loc,
5202 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
5203
5204 -- When the result subtype is unconstrained, the function itself must
5205 -- perform the allocation of the return object, so we pass parameters
5206 -- indicating that. We don't yet handle the case where the allocation
5207 -- must be done in a user-defined storage pool, which will require
5208 -- passing another actual or two to provide allocation/deallocation
5209 -- operations. ???
5210
5211 else
7888a6ae 5212
f937473f
RD
5213 -- Pass an allocation parameter indicating that the function should
5214 -- allocate its result on the heap.
5215
5216 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5217 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
5218
7888a6ae
GD
5219 Add_Final_List_Actual_To_Build_In_Place_Call
5220 (Func_Call, Function_Id, Acc_Type);
f937473f 5221
f937473f
RD
5222 Add_Task_Actuals_To_Build_In_Place_Call
5223 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
7888a6ae
GD
5224
5225 -- The caller does not provide the return object in this case, so we
5226 -- have to pass null for the object access actual.
5227
f937473f
RD
5228 Add_Access_Actual_To_Build_In_Place_Call
5229 (Func_Call, Function_Id, Return_Object => Empty);
5230 end if;
02822a92
RD
5231
5232 -- Finally, replace the allocator node with a reference to the result
5233 -- of the function call itself (which will effectively be an access
5234 -- to the object created by the allocator).
5235
5236 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
5237 Analyze_And_Resolve (Allocator, Acc_Type);
5238 end Make_Build_In_Place_Call_In_Allocator;
5239
5240 ---------------------------------------------------
5241 -- Make_Build_In_Place_Call_In_Anonymous_Context --
5242 ---------------------------------------------------
5243
5244 procedure Make_Build_In_Place_Call_In_Anonymous_Context
5245 (Function_Call : Node_Id)
5246 is
5247 Loc : Source_Ptr;
5248 Func_Call : Node_Id := Function_Call;
5249 Function_Id : Entity_Id;
5250 Result_Subt : Entity_Id;
5251 Return_Obj_Id : Entity_Id;
5252 Return_Obj_Decl : Entity_Id;
5253
5254 begin
19590d70
GD
5255 -- Step past qualification or unchecked conversion (the latter can occur
5256 -- in cases of calls to 'Input).
5257
ac4d6407
RD
5258 if Nkind_In (Func_Call, N_Qualified_Expression,
5259 N_Unchecked_Type_Conversion)
19590d70 5260 then
02822a92
RD
5261 Func_Call := Expression (Func_Call);
5262 end if;
5263
fdce4bb7
JM
5264 -- If the call has already been processed to add build-in-place actuals
5265 -- then return. One place this can occur is for calls to build-in-place
5266 -- functions that occur within a call to a protected operation, where
5267 -- due to rewriting and expansion of the protected call there can be
5268 -- more than one call to Expand_Actuals for the same set of actuals.
5269
5270 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5271 return;
5272 end if;
5273
5274 -- Mark the call as processed as a build-in-place call
5275
5276 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5277
02822a92
RD
5278 Loc := Sloc (Function_Call);
5279
5280 if Is_Entity_Name (Name (Func_Call)) then
5281 Function_Id := Entity (Name (Func_Call));
5282
5283 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5284 Function_Id := Etype (Name (Func_Call));
5285
5286 else
5287 raise Program_Error;
5288 end if;
5289
5290 Result_Subt := Etype (Function_Id);
5291
f937473f
RD
5292 -- When the result subtype is constrained, an object of the subtype is
5293 -- declared and an access value designating it is passed as an actual.
02822a92 5294
7888a6ae 5295 if Is_Constrained (Underlying_Type (Result_Subt)) then
02822a92 5296
f937473f
RD
5297 -- Create a temporary object to hold the function result
5298
c12beea0 5299 Return_Obj_Id := Make_Temporary (Loc, 'R');
f937473f 5300 Set_Etype (Return_Obj_Id, Result_Subt);
02822a92 5301
f937473f
RD
5302 Return_Obj_Decl :=
5303 Make_Object_Declaration (Loc,
5304 Defining_Identifier => Return_Obj_Id,
5305 Aliased_Present => True,
5306 Object_Definition => New_Reference_To (Result_Subt, Loc));
02822a92 5307
f937473f 5308 Set_No_Initialization (Return_Obj_Decl);
02822a92 5309
f937473f 5310 Insert_Action (Func_Call, Return_Obj_Decl);
02822a92 5311
7888a6ae
GD
5312 -- When the function has a controlling result, an allocation-form
5313 -- parameter must be passed indicating that the caller is allocating
5314 -- the result object. This is needed because such a function can be
5315 -- called as a dispatching operation and must be treated similarly
5316 -- to functions with unconstrained result subtypes.
5317
5318 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5319 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5320
5321 Add_Final_List_Actual_To_Build_In_Place_Call
5322 (Func_Call, Function_Id, Acc_Type => Empty);
f937473f 5323
f937473f
RD
5324 Add_Task_Actuals_To_Build_In_Place_Call
5325 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7888a6ae
GD
5326
5327 -- Add an implicit actual to the function call that provides access
5328 -- to the caller's return object.
5329
f937473f
RD
5330 Add_Access_Actual_To_Build_In_Place_Call
5331 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
5332
5333 -- When the result subtype is unconstrained, the function must allocate
5334 -- the return object in the secondary stack, so appropriate implicit
5335 -- parameters are added to the call to indicate that. A transient
5336 -- scope is established to ensure eventual cleanup of the result.
5337
5338 else
7888a6ae 5339
f937473f
RD
5340 -- Pass an allocation parameter indicating that the function should
5341 -- allocate its result on the secondary stack.
5342
5343 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5344 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
5345
7888a6ae
GD
5346 Add_Final_List_Actual_To_Build_In_Place_Call
5347 (Func_Call, Function_Id, Acc_Type => Empty);
f937473f 5348
f937473f
RD
5349 Add_Task_Actuals_To_Build_In_Place_Call
5350 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7888a6ae
GD
5351
5352 -- Pass a null value to the function since no return object is
5353 -- available on the caller side.
5354
f937473f
RD
5355 Add_Access_Actual_To_Build_In_Place_Call
5356 (Func_Call, Function_Id, Empty);
5357
5358 Establish_Transient_Scope (Func_Call, Sec_Stack => True);
5359 end if;
02822a92
RD
5360 end Make_Build_In_Place_Call_In_Anonymous_Context;
5361
ce2798e8 5362 --------------------------------------------
02822a92 5363 -- Make_Build_In_Place_Call_In_Assignment --
ce2798e8 5364 --------------------------------------------
02822a92
RD
5365
5366 procedure Make_Build_In_Place_Call_In_Assignment
5367 (Assign : Node_Id;
5368 Function_Call : Node_Id)
5369 is
3a69b5ff
AC
5370 Lhs : constant Node_Id := Name (Assign);
5371 Func_Call : Node_Id := Function_Call;
5372 Func_Id : Entity_Id;
5373 Loc : Source_Ptr;
5374 Obj_Decl : Node_Id;
5375 Obj_Id : Entity_Id;
5376 Ptr_Typ : Entity_Id;
5377 Ptr_Typ_Decl : Node_Id;
5378 Result_Subt : Entity_Id;
5379 Target : Node_Id;
02822a92
RD
5380
5381 begin
19590d70
GD
5382 -- Step past qualification or unchecked conversion (the latter can occur
5383 -- in cases of calls to 'Input).
5384
ac4d6407
RD
5385 if Nkind_In (Func_Call, N_Qualified_Expression,
5386 N_Unchecked_Type_Conversion)
19590d70 5387 then
02822a92
RD
5388 Func_Call := Expression (Func_Call);
5389 end if;
5390
fdce4bb7
JM
5391 -- If the call has already been processed to add build-in-place actuals
5392 -- then return. This should not normally occur in an assignment context,
5393 -- but we add the protection as a defensive measure.
5394
5395 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5396 return;
5397 end if;
5398
5399 -- Mark the call as processed as a build-in-place call
5400
5401 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5402
02822a92
RD
5403 Loc := Sloc (Function_Call);
5404
5405 if Is_Entity_Name (Name (Func_Call)) then
3a69b5ff 5406 Func_Id := Entity (Name (Func_Call));
02822a92
RD
5407
5408 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
3a69b5ff 5409 Func_Id := Etype (Name (Func_Call));
02822a92
RD
5410
5411 else
5412 raise Program_Error;
5413 end if;
5414
3a69b5ff 5415 Result_Subt := Etype (Func_Id);
02822a92 5416
f937473f
RD
5417 -- When the result subtype is unconstrained, an additional actual must
5418 -- be passed to indicate that the caller is providing the return object.
7888a6ae
GD
5419 -- This parameter must also be passed when the called function has a
5420 -- controlling result, because dispatching calls to the function needs
5421 -- to be treated effectively the same as calls to class-wide functions.
f937473f 5422
7888a6ae 5423 Add_Alloc_Form_Actual_To_Build_In_Place_Call
3a69b5ff 5424 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
f937473f 5425
70f91180
RD
5426 -- If Lhs is a selected component, then pass it along so that its prefix
5427 -- object will be used as the source of the finalization list.
5428
5429 if Nkind (Lhs) = N_Selected_Component then
5430 Add_Final_List_Actual_To_Build_In_Place_Call
3a69b5ff 5431 (Func_Call, Func_Id, Acc_Type => Empty, Sel_Comp => Lhs);
70f91180
RD
5432 else
5433 Add_Final_List_Actual_To_Build_In_Place_Call
3a69b5ff 5434 (Func_Call, Func_Id, Acc_Type => Empty);
70f91180 5435 end if;
02822a92 5436
f937473f 5437 Add_Task_Actuals_To_Build_In_Place_Call
3a69b5ff 5438 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
7888a6ae
GD
5439
5440 -- Add an implicit actual to the function call that provides access to
5441 -- the caller's return object.
5442
02822a92
RD
5443 Add_Access_Actual_To_Build_In_Place_Call
5444 (Func_Call,
3a69b5ff 5445 Func_Id,
02822a92
RD
5446 Make_Unchecked_Type_Conversion (Loc,
5447 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5448 Expression => Relocate_Node (Lhs)));
5449
5450 -- Create an access type designating the function's result subtype
5451
c12beea0 5452 Ptr_Typ := Make_Temporary (Loc, 'A');
02822a92
RD
5453
5454 Ptr_Typ_Decl :=
5455 Make_Full_Type_Declaration (Loc,
3a69b5ff 5456 Defining_Identifier => Ptr_Typ,
02822a92
RD
5457 Type_Definition =>
5458 Make_Access_To_Object_Definition (Loc,
5459 All_Present => True,
5460 Subtype_Indication =>
5461 New_Reference_To (Result_Subt, Loc)));
02822a92
RD
5462 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
5463
5464 -- Finally, create an access object initialized to a reference to the
5465 -- function call.
5466
c12beea0 5467 Obj_Id := Make_Temporary (Loc, 'R');
3a69b5ff 5468 Set_Etype (Obj_Id, Ptr_Typ);
02822a92 5469
3a69b5ff 5470 Obj_Decl :=
02822a92 5471 Make_Object_Declaration (Loc,
3a69b5ff
AC
5472 Defining_Identifier => Obj_Id,
5473 Object_Definition =>
5474 New_Reference_To (Ptr_Typ, Loc),
5475 Expression =>
5476 Make_Reference (Loc,
5477 Prefix => Relocate_Node (Func_Call)));
5478 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
02822a92
RD
5479
5480 Rewrite (Assign, Make_Null_Statement (Loc));
3a69b5ff
AC
5481
5482 -- Retrieve the target of the assignment
5483
5484 if Nkind (Lhs) = N_Selected_Component then
5485 Target := Selector_Name (Lhs);
5486 elsif Nkind (Lhs) = N_Type_Conversion then
5487 Target := Expression (Lhs);
5488 else
5489 Target := Lhs;
5490 end if;
5491
5492 -- If we are assigning to a return object or this is an expression of
5493 -- an extension aggregate, the target should either be an identifier
5494 -- or a simple expression. All other cases imply a different scenario.
5495
5496 if Nkind (Target) in N_Has_Entity then
5497 Target := Entity (Target);
5498 else
5499 return;
5500 end if;
5501
5502 -- When the target of the assignment is a return object of an enclosing
5503 -- build-in-place function and also requires finalization, the list
5504 -- generated for the assignment must be moved to that of the enclosing
5505 -- function.
5506
5507 -- function Enclosing_BIP_Function return Ctrl_Typ is
5508 -- begin
5509 -- return (Ctrl_Parent_Part => BIP_Function with ...);
5510 -- end Enclosing_BIP_Function;
5511
5512 if Is_Return_Object (Target)
5513 and then Needs_Finalization (Etype (Target))
5514 and then Needs_Finalization (Result_Subt)
5515 then
5516 declare
5517 Obj_List : constant Node_Id := Find_Final_List (Obj_Id);
5518 Encl_List : Node_Id;
5519 Encl_Scop : Entity_Id;
5520
5521 begin
5522 Encl_Scop := Scope (Target);
5523
5524 -- Locate the scope of the extended return statement
5525
5526 while Present (Encl_Scop)
5527 and then Ekind (Encl_Scop) /= E_Return_Statement
5528 loop
5529 Encl_Scop := Scope (Encl_Scop);
5530 end loop;
5531
5532 -- A return object should always be enclosed by a return statement
5533 -- scope at some level.
5534
5535 pragma Assert (Present (Encl_Scop));
5536
5537 Encl_List :=
5538 Make_Attribute_Reference (Loc,
5539 Prefix =>
5540 New_Reference_To (
5541 Finalization_Chain_Entity (Encl_Scop), Loc),
5542 Attribute_Name => Name_Unrestricted_Access);
5543
5544 -- Generate a call to move final list
5545
5546 Insert_After_And_Analyze (Obj_Decl,
5547 Make_Procedure_Call_Statement (Loc,
5548 Name =>
5549 New_Reference_To (RTE (RE_Move_Final_List), Loc),
5550 Parameter_Associations => New_List (Obj_List, Encl_List)));
5551 end;
5552 end if;
02822a92
RD
5553 end Make_Build_In_Place_Call_In_Assignment;
5554
5555 ----------------------------------------------------
5556 -- Make_Build_In_Place_Call_In_Object_Declaration --
5557 ----------------------------------------------------
5558
5559 procedure Make_Build_In_Place_Call_In_Object_Declaration
5560 (Object_Decl : Node_Id;
5561 Function_Call : Node_Id)
5562 is
f937473f
RD
5563 Loc : Source_Ptr;
5564 Obj_Def_Id : constant Entity_Id :=
5565 Defining_Identifier (Object_Decl);
7888a6ae 5566
f937473f
RD
5567 Func_Call : Node_Id := Function_Call;
5568 Function_Id : Entity_Id;
5569 Result_Subt : Entity_Id;
5570 Caller_Object : Node_Id;
5571 Call_Deref : Node_Id;
5572 Ref_Type : Entity_Id;
5573 Ptr_Typ_Decl : Node_Id;
5574 Def_Id : Entity_Id;
5575 New_Expr : Node_Id;
5576 Enclosing_Func : Entity_Id;
5577 Pass_Caller_Acc : Boolean := False;
02822a92
RD
5578
5579 begin
19590d70
GD
5580 -- Step past qualification or unchecked conversion (the latter can occur
5581 -- in cases of calls to 'Input).
5582
ac4d6407
RD
5583 if Nkind_In (Func_Call, N_Qualified_Expression,
5584 N_Unchecked_Type_Conversion)
19590d70 5585 then
02822a92
RD
5586 Func_Call := Expression (Func_Call);
5587 end if;
5588
fdce4bb7
JM
5589 -- If the call has already been processed to add build-in-place actuals
5590 -- then return. This should not normally occur in an object declaration,
5591 -- but we add the protection as a defensive measure.
5592
5593 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5594 return;
5595 end if;
5596
5597 -- Mark the call as processed as a build-in-place call
5598
5599 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5600
02822a92
RD
5601 Loc := Sloc (Function_Call);
5602
5603 if Is_Entity_Name (Name (Func_Call)) then
5604 Function_Id := Entity (Name (Func_Call));
5605
5606 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5607 Function_Id := Etype (Name (Func_Call));
5608
5609 else
5610 raise Program_Error;
5611 end if;
5612
5613 Result_Subt := Etype (Function_Id);
5614
f937473f
RD
5615 -- In the constrained case, add an implicit actual to the function call
5616 -- that provides access to the declared object. An unchecked conversion
5617 -- to the (specific) result type of the function is inserted to handle
5618 -- the case where the object is declared with a class-wide type.
5619
7888a6ae 5620 if Is_Constrained (Underlying_Type (Result_Subt)) then
f937473f
RD
5621 Caller_Object :=
5622 Make_Unchecked_Type_Conversion (Loc,
5623 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5624 Expression => New_Reference_To (Obj_Def_Id, Loc));
02822a92 5625
7888a6ae
GD
5626 -- When the function has a controlling result, an allocation-form
5627 -- parameter must be passed indicating that the caller is allocating
5628 -- the result object. This is needed because such a function can be
5629 -- called as a dispatching operation and must be treated similarly
5630 -- to functions with unconstrained result subtypes.
5631
5632 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5633 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5634
f937473f
RD
5635 -- If the function's result subtype is unconstrained and the object is
5636 -- a return object of an enclosing build-in-place function, then the
5637 -- implicit build-in-place parameters of the enclosing function must be
ce14c577
AC
5638 -- passed along to the called function. (Unfortunately, this won't cover
5639 -- the case of extension aggregates where the ancestor part is a build-
5640 -- in-place unconstrained function call that should be passed along the
5641 -- caller's parameters. Currently those get mishandled by reassigning
5642 -- the result of the call to the aggregate return object, when the call
5643 -- result should really be directly built in place in the aggregate and
5644 -- not built in a temporary. ???)
5645
5646 elsif Is_Return_Object (Defining_Identifier (Object_Decl)) then
f937473f
RD
5647 Pass_Caller_Acc := True;
5648
5649 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
5650
5651 -- If the enclosing function has a constrained result type, then
5652 -- caller allocation will be used.
5653
5654 if Is_Constrained (Etype (Enclosing_Func)) then
5655 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5656 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5657
5658 -- Otherwise, when the enclosing function has an unconstrained result
5659 -- type, the BIP_Alloc_Form formal of the enclosing function must be
7888a6ae 5660 -- passed along to the callee.
f937473f
RD
5661
5662 else
5663 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5664 (Func_Call,
5665 Function_Id,
5666 Alloc_Form_Exp =>
5667 New_Reference_To
5668 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
5669 Loc));
5670 end if;
5671
5672 -- Retrieve the BIPacc formal from the enclosing function and convert
5673 -- it to the access type of the callee's BIP_Object_Access formal.
5674
5675 Caller_Object :=
5676 Make_Unchecked_Type_Conversion (Loc,
5677 Subtype_Mark =>
5678 New_Reference_To
5679 (Etype
5680 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
5681 Loc),
5682 Expression =>
5683 New_Reference_To
5684 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
5685 Loc));
5686
5687 -- In other unconstrained cases, pass an indication to do the allocation
5688 -- on the secondary stack and set Caller_Object to Empty so that a null
5689 -- value will be passed for the caller's object address. A transient
5690 -- scope is established to ensure eventual cleanup of the result.
5691
5692 else
5693 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5694 (Func_Call,
5695 Function_Id,
5696 Alloc_Form => Secondary_Stack);
5697 Caller_Object := Empty;
5698
5699 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
5700 end if;
5701
7888a6ae
GD
5702 Add_Final_List_Actual_To_Build_In_Place_Call
5703 (Func_Call, Function_Id, Acc_Type => Empty);
5704
f937473f
RD
5705 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
5706 and then Has_Task (Result_Subt)
5707 then
5708 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
7888a6ae
GD
5709
5710 -- Here we're passing along the master that was passed in to this
5711 -- function.
5712
f937473f
RD
5713 Add_Task_Actuals_To_Build_In_Place_Call
5714 (Func_Call, Function_Id,
5715 Master_Actual =>
5716 New_Reference_To
5717 (Build_In_Place_Formal (Enclosing_Func, BIP_Master), Loc));
7888a6ae 5718
f937473f
RD
5719 else
5720 Add_Task_Actuals_To_Build_In_Place_Call
5721 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
5722 end if;
7888a6ae 5723
02822a92 5724 Add_Access_Actual_To_Build_In_Place_Call
f937473f 5725 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
02822a92 5726
b0b7b57d
AC
5727 -- Create an access type designating the function's result subtype. We
5728 -- use the type of the original expression because it may be a call to
5729 -- an inherited operation, which the expansion has replaced with the
5730 -- parent operation that yields the parent type.
02822a92 5731
c12beea0 5732 Ref_Type := Make_Temporary (Loc, 'A');
02822a92
RD
5733
5734 Ptr_Typ_Decl :=
5735 Make_Full_Type_Declaration (Loc,
5736 Defining_Identifier => Ref_Type,
5737 Type_Definition =>
5738 Make_Access_To_Object_Definition (Loc,
5739 All_Present => True,
5740 Subtype_Indication =>
b0b7b57d 5741 New_Reference_To (Etype (Function_Call), Loc)));
02822a92 5742
f937473f
RD
5743 -- The access type and its accompanying object must be inserted after
5744 -- the object declaration in the constrained case, so that the function
5745 -- call can be passed access to the object. In the unconstrained case,
5746 -- the access type and object must be inserted before the object, since
5747 -- the object declaration is rewritten to be a renaming of a dereference
5748 -- of the access object.
5749
7888a6ae 5750 if Is_Constrained (Underlying_Type (Result_Subt)) then
f937473f
RD
5751 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
5752 else
4f6e2c24 5753 Insert_Action (Object_Decl, Ptr_Typ_Decl);
f937473f 5754 end if;
02822a92
RD
5755
5756 -- Finally, create an access object initialized to a reference to the
5757 -- function call.
5758
02822a92
RD
5759 New_Expr :=
5760 Make_Reference (Loc,
5761 Prefix => Relocate_Node (Func_Call));
5762
c12beea0
RD
5763 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
5764 Set_Etype (Def_Id, Ref_Type);
5765
02822a92
RD
5766 Insert_After_And_Analyze (Ptr_Typ_Decl,
5767 Make_Object_Declaration (Loc,
5768 Defining_Identifier => Def_Id,
5769 Object_Definition => New_Reference_To (Ref_Type, Loc),
5770 Expression => New_Expr));
5771
7888a6ae 5772 if Is_Constrained (Underlying_Type (Result_Subt)) then
f937473f
RD
5773 Set_Expression (Object_Decl, Empty);
5774 Set_No_Initialization (Object_Decl);
5775
5776 -- In case of an unconstrained result subtype, rewrite the object
5777 -- declaration as an object renaming where the renamed object is a
5778 -- dereference of <function_Call>'reference:
5779 --
5780 -- Obj : Subt renames <function_call>'Ref.all;
5781
5782 else
5783 Call_Deref :=
5784 Make_Explicit_Dereference (Loc,
5785 Prefix => New_Reference_To (Def_Id, Loc));
5786
5787 Rewrite (Object_Decl,
5788 Make_Object_Renaming_Declaration (Loc,
c12beea0 5789 Defining_Identifier => Make_Temporary (Loc, 'D'),
f937473f
RD
5790 Access_Definition => Empty,
5791 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
5792 Name => Call_Deref));
5793
5794 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
5795
5796 Analyze (Object_Decl);
5797
5798 -- Replace the internal identifier of the renaming declaration's
5799 -- entity with identifier of the original object entity. We also have
5800 -- to exchange the entities containing their defining identifiers to
5801 -- ensure the correct replacement of the object declaration by the
5802 -- object renaming declaration to avoid homograph conflicts (since
5803 -- the object declaration's defining identifier was already entered
67ce0d7e
RD
5804 -- in current scope). The Next_Entity links of the two entities also
5805 -- have to be swapped since the entities are part of the return
5806 -- scope's entity list and the list structure would otherwise be
7e8ed0a6 5807 -- corrupted. Finally, the homonym chain must be preserved as well.
67ce0d7e
RD
5808
5809 declare
5810 Renaming_Def_Id : constant Entity_Id :=
5811 Defining_Identifier (Object_Decl);
5812 Next_Entity_Temp : constant Entity_Id :=
5813 Next_Entity (Renaming_Def_Id);
5814 begin
5815 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
5816
5817 -- Swap next entity links in preparation for exchanging entities
f937473f 5818
67ce0d7e
RD
5819 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
5820 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
7e8ed0a6 5821 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
67ce0d7e
RD
5822
5823 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
5824 end;
f937473f 5825 end if;
02822a92
RD
5826
5827 -- If the object entity has a class-wide Etype, then we need to change
5828 -- it to the result subtype of the function call, because otherwise the
53b308f6
AC
5829 -- object will be class-wide without an explicit initialization and
5830 -- won't be allocated properly by the back end. It seems unclean to make
5831 -- such a revision to the type at this point, and we should try to
5832 -- improve this treatment when build-in-place functions with class-wide
5833 -- results are implemented. ???
02822a92
RD
5834
5835 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
5836 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
5837 end if;
5838 end Make_Build_In_Place_Call_In_Object_Declaration;
5839
8fb68c56
RD
5840 --------------------------
5841 -- Needs_BIP_Final_List --
5842 --------------------------
5843
048e5cef
BD
5844 function Needs_BIP_Final_List (E : Entity_Id) return Boolean is
5845 pragma Assert (Is_Build_In_Place_Function (E));
5846 Result_Subt : constant Entity_Id := Underlying_Type (Etype (E));
8fb68c56 5847
048e5cef
BD
5848 begin
5849 -- We need the BIP_Final_List if the result type needs finalization. We
5850 -- also need it for tagged types, even if not class-wide, because some
5851 -- type extension might need finalization, and all overriding functions
5852 -- must have the same calling conventions. However, if there is a
5853 -- pragma Restrictions (No_Finalization), we never need this parameter.
5854
5855 return (Needs_Finalization (Result_Subt)
8fb68c56 5856 or else Is_Tagged_Type (Underlying_Type (Result_Subt)))
048e5cef
BD
5857 and then not Restriction_Active (No_Finalization);
5858 end Needs_BIP_Final_List;
5859
70482933 5860end Exp_Ch6;
This page took 2.77275 seconds and 5 git commands to generate.