]> 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,
1798 Explicit_Actual_Parameter => Expr,
1799 Selector_Name =>
1800 Make_Identifier (Loc, Chars (EF))));
1801
1802 Analyze_And_Resolve (Expr, Etype (EF));
75a64833
AC
1803
1804 if Nkind (N) = N_Function_Call then
1805 Set_Is_Accessibility_Actual (Parent (Expr));
1806 end if;
70482933
RK
1807 end Add_Extra_Actual;
1808
1809 ---------------------------
1810 -- Inherited_From_Formal --
1811 ---------------------------
1812
1813 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1814 Par : Entity_Id;
1815 Gen_Par : Entity_Id;
1816 Gen_Prim : Elist_Id;
1817 Elmt : Elmt_Id;
1818 Indic : Node_Id;
1819
1820 begin
1821 -- If the operation is inherited, it is attached to the corresponding
1822 -- type derivation. If the parent in the derivation is a generic
1823 -- actual, it is a subtype of the actual, and we have to recover the
1824 -- original derived type declaration to find the proper parent.
1825
1826 if Nkind (Parent (S)) /= N_Full_Type_Declaration
fbf5a39b 1827 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2f1b20a9
ES
1828 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1829 N_Derived_Type_Definition
fbf5a39b 1830 or else not In_Instance
70482933
RK
1831 then
1832 return Empty;
1833
1834 else
1835 Indic :=
e27b834b
AC
1836 Subtype_Indication
1837 (Type_Definition (Original_Node (Parent (S))));
70482933
RK
1838
1839 if Nkind (Indic) = N_Subtype_Indication then
1840 Par := Entity (Subtype_Mark (Indic));
1841 else
1842 Par := Entity (Indic);
1843 end if;
1844 end if;
1845
1846 if not Is_Generic_Actual_Type (Par)
1847 or else Is_Tagged_Type (Par)
1848 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1849 or else not In_Open_Scopes (Scope (Par))
70482933
RK
1850 then
1851 return Empty;
70482933
RK
1852 else
1853 Gen_Par := Generic_Parent_Type (Parent (Par));
1854 end if;
1855
7888a6ae
GD
1856 -- If the actual has no generic parent type, the formal is not
1857 -- a formal derived type, so nothing to inherit.
1858
1859 if No (Gen_Par) then
1860 return Empty;
1861 end if;
1862
2f1b20a9
ES
1863 -- If the generic parent type is still the generic type, this is a
1864 -- private formal, not a derived formal, and there are no operations
1865 -- inherited from the formal.
fbf5a39b
AC
1866
1867 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1868 return Empty;
1869 end if;
1870
70482933 1871 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
70482933 1872
2f1b20a9 1873 Elmt := First_Elmt (Gen_Prim);
70482933
RK
1874 while Present (Elmt) loop
1875 if Chars (Node (Elmt)) = Chars (S) then
1876 declare
1877 F1 : Entity_Id;
1878 F2 : Entity_Id;
70482933 1879
2f1b20a9 1880 begin
70482933
RK
1881 F1 := First_Formal (S);
1882 F2 := First_Formal (Node (Elmt));
70482933
RK
1883 while Present (F1)
1884 and then Present (F2)
1885 loop
70482933
RK
1886 if Etype (F1) = Etype (F2)
1887 or else Etype (F2) = Gen_Par
1888 then
1889 Next_Formal (F1);
1890 Next_Formal (F2);
1891 else
1892 Next_Elmt (Elmt);
1893 exit; -- not the right subprogram
1894 end if;
1895
1896 return Node (Elmt);
1897 end loop;
1898 end;
1899
1900 else
1901 Next_Elmt (Elmt);
1902 end if;
1903 end loop;
1904
1905 raise Program_Error;
1906 end Inherited_From_Formal;
1907
fdce4bb7
JM
1908 -- Local variables
1909
1910 Remote : constant Boolean := Is_Remote_Call (N);
1911 Actual : Node_Id;
1912 Formal : Entity_Id;
1913 Orig_Subp : Entity_Id := Empty;
1914 Param_Count : Natural := 0;
1915 Parent_Formal : Entity_Id;
1916 Parent_Subp : Entity_Id;
1917 Scop : Entity_Id;
1918 Subp : Entity_Id;
1919
e27b834b 1920 Prev_Orig : Node_Id;
fdce4bb7
JM
1921 -- Original node for an actual, which may have been rewritten. If the
1922 -- actual is a function call that has been transformed from a selected
1923 -- component, the original node is unanalyzed. Otherwise, it carries
1924 -- semantic information used to generate additional actuals.
1925
1926 CW_Interface_Formals_Present : Boolean := False;
1927
70482933
RK
1928 -- Start of processing for Expand_Call
1929
1930 begin
07fc65c4
GB
1931 -- Ignore if previous error
1932
1933 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1934 return;
1935 end if;
1936
70482933
RK
1937 -- Call using access to subprogram with explicit dereference
1938
1939 if Nkind (Name (N)) = N_Explicit_Dereference then
1940 Subp := Etype (Name (N));
1941 Parent_Subp := Empty;
1942
1943 -- Case of call to simple entry, where the Name is a selected component
1944 -- whose prefix is the task, and whose selector name is the entry name
1945
1946 elsif Nkind (Name (N)) = N_Selected_Component then
1947 Subp := Entity (Selector_Name (Name (N)));
1948 Parent_Subp := Empty;
1949
1950 -- Case of call to member of entry family, where Name is an indexed
1951 -- component, with the prefix being a selected component giving the
1952 -- task and entry family name, and the index being the entry index.
1953
1954 elsif Nkind (Name (N)) = N_Indexed_Component then
1955 Subp := Entity (Selector_Name (Prefix (Name (N))));
1956 Parent_Subp := Empty;
1957
1958 -- Normal case
1959
1960 else
1961 Subp := Entity (Name (N));
1962 Parent_Subp := Alias (Subp);
1963
1964 -- Replace call to Raise_Exception by call to Raise_Exception_Always
1965 -- if we can tell that the first parameter cannot possibly be null.
70f91180 1966 -- This improves efficiency by avoiding a run-time test.
70482933 1967
7888a6ae
GD
1968 -- We do not do this if Raise_Exception_Always does not exist, which
1969 -- can happen in configurable run time profiles which provide only a
70f91180 1970 -- Raise_Exception.
7888a6ae
GD
1971
1972 if Is_RTE (Subp, RE_Raise_Exception)
1973 and then RTE_Available (RE_Raise_Exception_Always)
70482933
RK
1974 then
1975 declare
1976 FA : constant Node_Id := Original_Node (First_Actual (N));
1977
1978 begin
1979 -- The case we catch is where the first argument is obtained
2f1b20a9
ES
1980 -- using the Identity attribute (which must always be
1981 -- non-null).
70482933
RK
1982
1983 if Nkind (FA) = N_Attribute_Reference
1984 and then Attribute_Name (FA) = Name_Identity
1985 then
1986 Subp := RTE (RE_Raise_Exception_Always);
7888a6ae 1987 Set_Name (N, New_Occurrence_Of (Subp, Loc));
70482933
RK
1988 end if;
1989 end;
1990 end if;
1991
1992 if Ekind (Subp) = E_Entry then
1993 Parent_Subp := Empty;
1994 end if;
1995 end if;
1996
f4d379b8
HK
1997 -- Ada 2005 (AI-345): We have a procedure call as a triggering
1998 -- alternative in an asynchronous select or as an entry call in
1999 -- a conditional or timed select. Check whether the procedure call
2000 -- is a renaming of an entry and rewrite it as an entry call.
2001
2002 if Ada_Version >= Ada_05
2003 and then Nkind (N) = N_Procedure_Call_Statement
2004 and then
2005 ((Nkind (Parent (N)) = N_Triggering_Alternative
2006 and then Triggering_Statement (Parent (N)) = N)
2007 or else
2008 (Nkind (Parent (N)) = N_Entry_Call_Alternative
2009 and then Entry_Call_Statement (Parent (N)) = N))
2010 then
2011 declare
2012 Ren_Decl : Node_Id;
2013 Ren_Root : Entity_Id := Subp;
2014
2015 begin
2016 -- This may be a chain of renamings, find the root
2017
2018 if Present (Alias (Ren_Root)) then
2019 Ren_Root := Alias (Ren_Root);
2020 end if;
2021
2022 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2023 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2024
2025 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2026 Rewrite (N,
2027 Make_Entry_Call_Statement (Loc,
2028 Name =>
2029 New_Copy_Tree (Name (Ren_Decl)),
2030 Parameter_Associations =>
2031 New_Copy_List_Tree (Parameter_Associations (N))));
2032
2033 return;
2034 end if;
2035 end if;
2036 end;
2037 end if;
2038
e27b834b
AC
2039 -- First step, compute extra actuals, corresponding to any Extra_Formals
2040 -- present. Note that we do not access Extra_Formals directly, instead
2041 -- we simply note the presence of the extra formals as we process the
2042 -- regular formals collecting corresponding actuals in Extra_Actuals.
70482933 2043
c2369146
AC
2044 -- We also generate any required range checks for actuals for in formals
2045 -- as we go through the loop, since this is a convenient place to do it.
2046 -- (Though it seems that this would be better done in Expand_Actuals???)
fbf5a39b 2047
fdce4bb7
JM
2048 Formal := First_Formal (Subp);
2049 Actual := First_Actual (N);
2050 Param_Count := 1;
70482933 2051 while Present (Formal) loop
fbf5a39b 2052
d79e621a 2053 -- Generate range check if required
fbf5a39b 2054
d79e621a 2055 if Do_Range_Check (Actual)
c2369146 2056 and then Ekind (Formal) = E_In_Parameter
d79e621a
GD
2057 then
2058 Set_Do_Range_Check (Actual, False);
2059 Generate_Range_Check
2060 (Actual, Etype (Formal), CE_Range_Check_Failed);
2061 end if;
fbf5a39b
AC
2062
2063 -- Prepare to examine current entry
2064
70482933
RK
2065 Prev := Actual;
2066 Prev_Orig := Original_Node (Prev);
2067
758c442c 2068 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2f1b20a9 2069 -- to expand it in a further round.
758c442c
GD
2070
2071 CW_Interface_Formals_Present :=
2072 CW_Interface_Formals_Present
2073 or else
2074 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2075 and then Is_Interface (Etype (Etype (Formal))))
2076 or else
2077 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2078 and then Is_Interface (Directly_Designated_Type
2079 (Etype (Etype (Formal)))));
2080
2081 -- Create possible extra actual for constrained case. Usually, the
2082 -- extra actual is of the form actual'constrained, but since this
2083 -- attribute is only available for unconstrained records, TRUE is
2084 -- expanded if the type of the formal happens to be constrained (for
2085 -- instance when this procedure is inherited from an unconstrained
2086 -- record to a constrained one) or if the actual has no discriminant
2087 -- (its type is constrained). An exception to this is the case of a
2088 -- private type without discriminants. In this case we pass FALSE
2089 -- because the object has underlying discriminants with defaults.
70482933
RK
2090
2091 if Present (Extra_Constrained (Formal)) then
2092 if Ekind (Etype (Prev)) in Private_Kind
2093 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2094 then
01aef5ad
GD
2095 Add_Extra_Actual
2096 (New_Occurrence_Of (Standard_False, Loc),
2097 Extra_Constrained (Formal));
70482933
RK
2098
2099 elsif Is_Constrained (Etype (Formal))
2100 or else not Has_Discriminants (Etype (Prev))
2101 then
01aef5ad
GD
2102 Add_Extra_Actual
2103 (New_Occurrence_Of (Standard_True, Loc),
2104 Extra_Constrained (Formal));
70482933 2105
5d09245e
AC
2106 -- Do not produce extra actuals for Unchecked_Union parameters.
2107 -- Jump directly to the end of the loop.
2108
2109 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2110 goto Skip_Extra_Actual_Generation;
2111
70482933
RK
2112 else
2113 -- If the actual is a type conversion, then the constrained
2114 -- test applies to the actual, not the target type.
2115
2116 declare
2f1b20a9 2117 Act_Prev : Node_Id;
70482933
RK
2118
2119 begin
2f1b20a9
ES
2120 -- Test for unchecked conversions as well, which can occur
2121 -- as out parameter actuals on calls to stream procedures.
70482933 2122
2f1b20a9 2123 Act_Prev := Prev;
ac4d6407
RD
2124 while Nkind_In (Act_Prev, N_Type_Conversion,
2125 N_Unchecked_Type_Conversion)
fbf5a39b 2126 loop
70482933 2127 Act_Prev := Expression (Act_Prev);
fbf5a39b 2128 end loop;
70482933 2129
3563739b
AC
2130 -- If the expression is a conversion of a dereference, this
2131 -- is internally generated code that manipulates addresses,
2132 -- e.g. when building interface tables. No check should
2133 -- occur in this case, and the discriminated object is not
2134 -- directly a hand.
f4d379b8
HK
2135
2136 if not Comes_From_Source (Actual)
2137 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2138 and then Nkind (Act_Prev) = N_Explicit_Dereference
2139 then
2140 Add_Extra_Actual
2141 (New_Occurrence_Of (Standard_False, Loc),
2142 Extra_Constrained (Formal));
2143
2144 else
2145 Add_Extra_Actual
2146 (Make_Attribute_Reference (Sloc (Prev),
2147 Prefix =>
2148 Duplicate_Subexpr_No_Checks
2149 (Act_Prev, Name_Req => True),
2150 Attribute_Name => Name_Constrained),
2151 Extra_Constrained (Formal));
2152 end if;
70482933
RK
2153 end;
2154 end if;
2155 end if;
2156
2157 -- Create possible extra actual for accessibility level
2158
2159 if Present (Extra_Accessibility (Formal)) then
7888a6ae
GD
2160
2161 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2162 -- attribute, then the original actual may be an aliased object
2163 -- occurring as the prefix in a call using "Object.Operation"
2164 -- notation. In that case we must pass the level of the object,
2165 -- so Prev_Orig is reset to Prev and the attribute will be
2166 -- processed by the code for Access attributes further below.
2167
2168 if Prev_Orig /= Prev
2169 and then Nkind (Prev) = N_Attribute_Reference
2170 and then
2171 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2172 and then Is_Aliased_View (Prev_Orig)
2173 then
2174 Prev_Orig := Prev;
2175 end if;
2176
fdce4bb7
JM
2177 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals
2178 -- of accessibility levels.
2179
2180 if Ekind (Current_Scope) in Subprogram_Kind
2181 and then Is_Thunk (Current_Scope)
2182 then
2183 declare
2184 Parm_Ent : Entity_Id;
2185
2186 begin
2187 if Is_Controlling_Actual (Actual) then
2188
2189 -- Find the corresponding actual of the thunk
2190
2191 Parm_Ent := First_Entity (Current_Scope);
2192 for J in 2 .. Param_Count loop
2193 Next_Entity (Parm_Ent);
2194 end loop;
2195
2196 else pragma Assert (Is_Entity_Name (Actual));
2197 Parm_Ent := Entity (Actual);
2198 end if;
2199
2200 Add_Extra_Actual
2201 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2202 Extra_Accessibility (Formal));
2203 end;
2204
2205 elsif Is_Entity_Name (Prev_Orig) then
70482933 2206
d766cee3
RD
2207 -- When passing an access parameter, or a renaming of an access
2208 -- parameter, as the actual to another access parameter we need
2209 -- to pass along the actual's own access level parameter. This
2210 -- is done if we are within the scope of the formal access
2211 -- parameter (if this is an inlined body the extra formal is
2212 -- irrelevant).
2213
2214 if (Is_Formal (Entity (Prev_Orig))
2215 or else
2216 (Present (Renamed_Object (Entity (Prev_Orig)))
2217 and then
2218 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2219 and then
2220 Is_Formal
2221 (Entity (Renamed_Object (Entity (Prev_Orig))))))
70482933
RK
2222 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2223 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2224 then
2225 declare
2226 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2227
2228 begin
2229 pragma Assert (Present (Parm_Ent));
2230
2231 if Present (Extra_Accessibility (Parm_Ent)) then
f4d379b8
HK
2232 Add_Extra_Actual
2233 (New_Occurrence_Of
2234 (Extra_Accessibility (Parm_Ent), Loc),
2235 Extra_Accessibility (Formal));
70482933
RK
2236
2237 -- If the actual access parameter does not have an
2238 -- associated extra formal providing its scope level,
2239 -- then treat the actual as having library-level
2240 -- accessibility.
2241
2242 else
f4d379b8
HK
2243 Add_Extra_Actual
2244 (Make_Integer_Literal (Loc,
01aef5ad 2245 Intval => Scope_Depth (Standard_Standard)),
f4d379b8 2246 Extra_Accessibility (Formal));
70482933
RK
2247 end if;
2248 end;
2249
7888a6ae
GD
2250 -- The actual is a normal access value, so just pass the level
2251 -- of the actual's access type.
70482933
RK
2252
2253 else
f4d379b8
HK
2254 Add_Extra_Actual
2255 (Make_Integer_Literal (Loc,
01aef5ad 2256 Intval => Type_Access_Level (Etype (Prev_Orig))),
f4d379b8 2257 Extra_Accessibility (Formal));
70482933
RK
2258 end if;
2259
01aef5ad
GD
2260 -- If the actual is an access discriminant, then pass the level
2261 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2262
2263 elsif Nkind (Prev_Orig) = N_Selected_Component
2264 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2265 E_Discriminant
2266 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2267 E_Anonymous_Access_Type
2268 then
2269 Add_Extra_Actual
2270 (Make_Integer_Literal (Loc,
2271 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2272 Extra_Accessibility (Formal));
2273
2274 -- All other cases
fdce4bb7 2275
70482933
RK
2276 else
2277 case Nkind (Prev_Orig) is
2278
2279 when N_Attribute_Reference =>
70482933
RK
2280 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2281
75a64833 2282 -- For X'Access, pass on the level of the prefix X
70482933
RK
2283
2284 when Attribute_Access =>
75a64833
AC
2285 Add_Extra_Actual
2286 (Make_Integer_Literal (Loc,
2287 Intval =>
2288 Object_Access_Level
2289 (Prefix (Prev_Orig))),
bac7206d 2290 Extra_Accessibility (Formal));
70482933
RK
2291
2292 -- Treat the unchecked attributes as library-level
2293
2294 when Attribute_Unchecked_Access |
2295 Attribute_Unrestricted_Access =>
01aef5ad
GD
2296 Add_Extra_Actual
2297 (Make_Integer_Literal (Loc,
2298 Intval => Scope_Depth (Standard_Standard)),
2299 Extra_Accessibility (Formal));
70482933
RK
2300
2301 -- No other cases of attributes returning access
2302 -- values that can be passed to access parameters
2303
2304 when others =>
2305 raise Program_Error;
2306
2307 end case;
2308
92a745f3
TQ
2309 -- For allocators we pass the level of the execution of the
2310 -- called subprogram, which is one greater than the current
2311 -- scope level.
70482933
RK
2312
2313 when N_Allocator =>
01aef5ad
GD
2314 Add_Extra_Actual
2315 (Make_Integer_Literal (Loc,
2316 Intval => Scope_Depth (Current_Scope) + 1),
2317 Extra_Accessibility (Formal));
70482933 2318
8ca3bf91
GD
2319 -- For other cases we simply pass the level of the actual's
2320 -- access type. The type is retrieved from Prev rather than
61549759 2321 -- Prev_Orig, because in some cases Prev_Orig denotes an
8ca3bf91 2322 -- original expression that has not been analyzed.
70482933
RK
2323
2324 when others =>
01aef5ad
GD
2325 Add_Extra_Actual
2326 (Make_Integer_Literal (Loc,
8ca3bf91 2327 Intval => Type_Access_Level (Etype (Prev))),
01aef5ad 2328 Extra_Accessibility (Formal));
70482933
RK
2329 end case;
2330 end if;
2331 end if;
2332
2f1b20a9
ES
2333 -- Perform the check of 4.6(49) that prevents a null value from being
2334 -- passed as an actual to an access parameter. Note that the check is
2335 -- elided in the common cases of passing an access attribute or
2336 -- access parameter as an actual. Also, we currently don't enforce
2337 -- this check for expander-generated actuals and when -gnatdj is set.
70482933 2338
2f1b20a9 2339 if Ada_Version >= Ada_05 then
70482933 2340
2f1b20a9 2341 -- Ada 2005 (AI-231): Check null-excluding access types
70482933 2342
2f1b20a9
ES
2343 if Is_Access_Type (Etype (Formal))
2344 and then Can_Never_Be_Null (Etype (Formal))
2345 and then Nkind (Prev) /= N_Raise_Constraint_Error
d766cee3 2346 and then (Known_Null (Prev)
2f1b20a9
ES
2347 or else not Can_Never_Be_Null (Etype (Prev)))
2348 then
2349 Install_Null_Excluding_Check (Prev);
2350 end if;
70482933 2351
2f1b20a9 2352 -- Ada_Version < Ada_05
70482933 2353
2f1b20a9
ES
2354 else
2355 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2356 or else Access_Checks_Suppressed (Subp)
2357 then
2358 null;
70482933 2359
2f1b20a9
ES
2360 elsif Debug_Flag_J then
2361 null;
70482933 2362
2f1b20a9
ES
2363 elsif not Comes_From_Source (Prev) then
2364 null;
70482933 2365
2f1b20a9
ES
2366 elsif Is_Entity_Name (Prev)
2367 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2368 then
2369 null;
2820d220 2370
ac4d6407 2371 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2f1b20a9
ES
2372 null;
2373
2374 -- Suppress null checks when passing to access parameters of Java
7888a6ae
GD
2375 -- and CIL subprograms. (Should this be done for other foreign
2376 -- conventions as well ???)
2f1b20a9 2377
7888a6ae
GD
2378 elsif Convention (Subp) = Convention_Java
2379 or else Convention (Subp) = Convention_CIL
2380 then
2f1b20a9
ES
2381 null;
2382
2383 else
2384 Install_Null_Excluding_Check (Prev);
2385 end if;
70482933
RK
2386 end if;
2387
fbf5a39b
AC
2388 -- Perform appropriate validity checks on parameters that
2389 -- are entities.
70482933
RK
2390
2391 if Validity_Checks_On then
6cdb2c6e
AC
2392 if (Ekind (Formal) = E_In_Parameter
2393 and then Validity_Check_In_Params)
2394 or else
2395 (Ekind (Formal) = E_In_Out_Parameter
2396 and then Validity_Check_In_Out_Params)
70482933 2397 then
7888a6ae
GD
2398 -- If the actual is an indexed component of a packed type (or
2399 -- is an indexed or selected component whose prefix recursively
2400 -- meets this condition), it has not been expanded yet. It will
2401 -- be copied in the validity code that follows, and has to be
2402 -- expanded appropriately, so reanalyze it.
08aa9a4a 2403
7888a6ae
GD
2404 -- What we do is just to unset analyzed bits on prefixes till
2405 -- we reach something that does not have a prefix.
2406
2407 declare
2408 Nod : Node_Id;
2409
2410 begin
2411 Nod := Actual;
ac4d6407
RD
2412 while Nkind_In (Nod, N_Indexed_Component,
2413 N_Selected_Component)
7888a6ae
GD
2414 loop
2415 Set_Analyzed (Nod, False);
2416 Nod := Prefix (Nod);
2417 end loop;
2418 end;
08aa9a4a 2419
70482933 2420 Ensure_Valid (Actual);
70482933
RK
2421 end if;
2422 end if;
2423
2424 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2425 -- since this is a left side reference. We only do this for calls
2426 -- from the source program since we assume that compiler generated
2427 -- calls explicitly generate any required checks. We also need it
2428 -- only if we are doing standard validity checks, since clearly it
2429 -- is not needed if validity checks are off, and in subscript
2430 -- validity checking mode, all indexed components are checked with
2431 -- a call directly from Expand_N_Indexed_Component.
2432
2433 if Comes_From_Source (N)
2434 and then Ekind (Formal) /= E_In_Parameter
2435 and then Validity_Checks_On
2436 and then Validity_Check_Default
2437 and then not Validity_Check_Subscripts
2438 then
2439 Check_Valid_Lvalue_Subscripts (Actual);
2440 end if;
2441
c8ef728f
ES
2442 -- Mark any scalar OUT parameter that is a simple variable as no
2443 -- longer known to be valid (unless the type is always valid). This
2444 -- reflects the fact that if an OUT parameter is never set in a
2445 -- procedure, then it can become invalid on the procedure return.
fbf5a39b
AC
2446
2447 if Ekind (Formal) = E_Out_Parameter
2448 and then Is_Entity_Name (Actual)
2449 and then Ekind (Entity (Actual)) = E_Variable
2450 and then not Is_Known_Valid (Etype (Actual))
2451 then
2452 Set_Is_Known_Valid (Entity (Actual), False);
2453 end if;
2454
c8ef728f
ES
2455 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2456 -- clear current values, since they can be clobbered. We are probably
2457 -- doing this in more places than we need to, but better safe than
2458 -- sorry when it comes to retaining bad current values!
fbf5a39b
AC
2459
2460 if Ekind (Formal) /= E_In_Parameter
2461 and then Is_Entity_Name (Actual)
67ce0d7e 2462 and then Present (Entity (Actual))
fbf5a39b 2463 then
67ce0d7e
RD
2464 declare
2465 Ent : constant Entity_Id := Entity (Actual);
2466 Sav : Node_Id;
2467
2468 begin
ac4d6407
RD
2469 -- For an OUT or IN OUT parameter that is an assignable entity,
2470 -- we do not want to clobber the Last_Assignment field, since
2471 -- if it is set, it was precisely because it is indeed an OUT
75ba322d
AC
2472 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
2473 -- since the subprogram could have returned in invalid value.
ac4d6407
RD
2474
2475 if (Ekind (Formal) = E_Out_Parameter
2476 or else
2477 Ekind (Formal) = E_In_Out_Parameter)
67ce0d7e
RD
2478 and then Is_Assignable (Ent)
2479 then
2480 Sav := Last_Assignment (Ent);
2481 Kill_Current_Values (Ent);
2482 Set_Last_Assignment (Ent, Sav);
75ba322d 2483 Set_Is_Known_Valid (Ent, False);
67ce0d7e
RD
2484
2485 -- For all other cases, just kill the current values
2486
2487 else
2488 Kill_Current_Values (Ent);
2489 end if;
2490 end;
fbf5a39b
AC
2491 end if;
2492
70482933
RK
2493 -- If the formal is class wide and the actual is an aggregate, force
2494 -- evaluation so that the back end who does not know about class-wide
2495 -- type, does not generate a temporary of the wrong size.
2496
2497 if not Is_Class_Wide_Type (Etype (Formal)) then
2498 null;
2499
2500 elsif Nkind (Actual) = N_Aggregate
2501 or else (Nkind (Actual) = N_Qualified_Expression
2502 and then Nkind (Expression (Actual)) = N_Aggregate)
2503 then
2504 Force_Evaluation (Actual);
2505 end if;
2506
2507 -- In a remote call, if the formal is of a class-wide type, check
2508 -- that the actual meets the requirements described in E.4(18).
2509
7888a6ae 2510 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
70482933 2511 Insert_Action (Actual,
7888a6ae
GD
2512 Make_Transportable_Check (Loc,
2513 Duplicate_Subexpr_Move_Checks (Actual)));
70482933
RK
2514 end if;
2515
5d09245e
AC
2516 -- This label is required when skipping extra actual generation for
2517 -- Unchecked_Union parameters.
2518
2519 <<Skip_Extra_Actual_Generation>>
2520
fdce4bb7 2521 Param_Count := Param_Count + 1;
70482933
RK
2522 Next_Actual (Actual);
2523 Next_Formal (Formal);
2524 end loop;
2525
c8ef728f
ES
2526 -- If we are expanding a rhs of an assignment we need to check if tag
2527 -- propagation is needed. You might expect this processing to be in
2528 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
2529 -- assignment might be transformed to a declaration for an unconstrained
2530 -- value if the expression is classwide.
70482933
RK
2531
2532 if Nkind (N) = N_Function_Call
2533 and then Is_Tag_Indeterminate (N)
2534 and then Is_Entity_Name (Name (N))
2535 then
2536 declare
2537 Ass : Node_Id := Empty;
2538
2539 begin
2540 if Nkind (Parent (N)) = N_Assignment_Statement then
2541 Ass := Parent (N);
2542
2543 elsif Nkind (Parent (N)) = N_Qualified_Expression
2544 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2545 then
2546 Ass := Parent (Parent (N));
02822a92
RD
2547
2548 elsif Nkind (Parent (N)) = N_Explicit_Dereference
2549 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2550 then
2551 Ass := Parent (Parent (N));
70482933
RK
2552 end if;
2553
2554 if Present (Ass)
2555 and then Is_Class_Wide_Type (Etype (Name (Ass)))
2556 then
02822a92
RD
2557 if Is_Access_Type (Etype (N)) then
2558 if Designated_Type (Etype (N)) /=
2559 Root_Type (Etype (Name (Ass)))
2560 then
2561 Error_Msg_NE
2562 ("tag-indeterminate expression "
d766cee3 2563 & " must have designated type& (RM 5.2 (6))",
02822a92
RD
2564 N, Root_Type (Etype (Name (Ass))));
2565 else
2566 Propagate_Tag (Name (Ass), N);
2567 end if;
2568
2569 elsif Etype (N) /= Root_Type (Etype (Name (Ass))) then
fbf5a39b
AC
2570 Error_Msg_NE
2571 ("tag-indeterminate expression must have type&"
d766cee3 2572 & "(RM 5.2 (6))", N, Root_Type (Etype (Name (Ass))));
02822a92 2573
fbf5a39b
AC
2574 else
2575 Propagate_Tag (Name (Ass), N);
2576 end if;
2577
2578 -- The call will be rewritten as a dispatching call, and
2579 -- expanded as such.
2580
70482933
RK
2581 return;
2582 end if;
2583 end;
2584 end if;
2585
758c442c
GD
2586 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
2587 -- it to point to the correct secondary virtual table
2588
ac4d6407 2589 if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
758c442c
GD
2590 and then CW_Interface_Formals_Present
2591 then
2592 Expand_Interface_Actuals (N);
2593 end if;
2594
70482933
RK
2595 -- Deals with Dispatch_Call if we still have a call, before expanding
2596 -- extra actuals since this will be done on the re-analysis of the
2597 -- dispatching call. Note that we do not try to shorten the actual
2598 -- list for a dispatching call, it would not make sense to do so.
7888a6ae
GD
2599 -- Expansion of dispatching calls is suppressed when VM_Target, because
2600 -- the VM back-ends directly handle the generation of dispatching
70482933
RK
2601 -- calls and would have to undo any expansion to an indirect call.
2602
ac4d6407 2603 if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
70482933 2604 and then Present (Controlling_Argument (N))
70482933 2605 then
1f110335 2606 if Tagged_Type_Expansion then
70f91180 2607 Expand_Dispatching_Call (N);
fbf5a39b 2608
70f91180
RD
2609 -- The following return is worrisome. Is it really OK to
2610 -- skip all remaining processing in this procedure ???
fbf5a39b 2611
70f91180
RD
2612 return;
2613
70f91180 2614 else
5a1ccfb1
AC
2615 Apply_Tag_Checks (N);
2616
2617 -- Expansion of a dispatching call results in an indirect call,
2618 -- which in turn causes current values to be killed (see
2619 -- Resolve_Call), so on VM targets we do the call here to ensure
2620 -- consistent warnings between VM and non-VM targets.
2621
70f91180
RD
2622 Kill_Current_Values;
2623 end if;
2624 end if;
70482933
RK
2625
2626 -- Similarly, expand calls to RCI subprograms on which pragma
2627 -- All_Calls_Remote applies. The rewriting will be reanalyzed
2628 -- later. Do this only when the call comes from source since we do
8fc789c8 2629 -- not want such a rewriting to occur in expanded code.
70482933 2630
70f91180 2631 if Is_All_Remote_Call (N) then
70482933
RK
2632 Expand_All_Calls_Remote_Subprogram_Call (N);
2633
2634 -- Similarly, do not add extra actuals for an entry call whose entity
2635 -- is a protected procedure, or for an internal protected subprogram
2636 -- call, because it will be rewritten as a protected subprogram call
2637 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
2638
2639 elsif Is_Protected_Type (Scope (Subp))
2640 and then (Ekind (Subp) = E_Procedure
2641 or else Ekind (Subp) = E_Function)
2642 then
2643 null;
2644
2645 -- During that loop we gathered the extra actuals (the ones that
2646 -- correspond to Extra_Formals), so now they can be appended.
2647
2648 else
2649 while Is_Non_Empty_List (Extra_Actuals) loop
2650 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2651 end loop;
2652 end if;
2653
f44fe430
RD
2654 -- At this point we have all the actuals, so this is the point at
2655 -- which the various expansion activities for actuals is carried out.
2656
2657 Expand_Actuals (N, Subp);
70482933
RK
2658
2659 -- If the subprogram is a renaming, or if it is inherited, replace it
2660 -- in the call with the name of the actual subprogram being called.
2661 -- If this is a dispatching call, the run-time decides what to call.
2662 -- The Alias attribute does not apply to entries.
2663
2664 if Nkind (N) /= N_Entry_Call_Statement
2665 and then No (Controlling_Argument (N))
2666 and then Present (Parent_Subp)
2667 then
2668 if Present (Inherited_From_Formal (Subp)) then
2669 Parent_Subp := Inherited_From_Formal (Subp);
2670 else
2671 while Present (Alias (Parent_Subp)) loop
2672 Parent_Subp := Alias (Parent_Subp);
2673 end loop;
2674 end if;
2675
c8ef728f
ES
2676 -- The below setting of Entity is suspect, see F109-018 discussion???
2677
70482933
RK
2678 Set_Entity (Name (N), Parent_Subp);
2679
f937473f 2680 if Is_Abstract_Subprogram (Parent_Subp)
70482933
RK
2681 and then not In_Instance
2682 then
2683 Error_Msg_NE
2684 ("cannot call abstract subprogram &!", Name (N), Parent_Subp);
2685 end if;
2686
d4817e3f
HK
2687 -- Inspect all formals of derived subprogram Subp. Compare parameter
2688 -- types with the parent subprogram and check whether an actual may
2689 -- need a type conversion to the corresponding formal of the parent
2690 -- subprogram.
70482933 2691
d4817e3f 2692 -- Not clear whether intrinsic subprograms need such conversions. ???
70482933
RK
2693
2694 if not Is_Intrinsic_Subprogram (Parent_Subp)
2695 or else Is_Generic_Instance (Parent_Subp)
2696 then
d4817e3f
HK
2697 declare
2698 procedure Convert (Act : Node_Id; Typ : Entity_Id);
2699 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
2700 -- and resolve the newly generated construct.
70482933 2701
d4817e3f
HK
2702 -------------
2703 -- Convert --
2704 -------------
70482933 2705
d4817e3f
HK
2706 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
2707 begin
2708 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
2709 Analyze (Act);
2710 Resolve (Act, Typ);
2711 end Convert;
2712
2713 -- Local variables
2714
2715 Actual_Typ : Entity_Id;
2716 Formal_Typ : Entity_Id;
2717 Parent_Typ : Entity_Id;
2718
2719 begin
2720 Actual := First_Actual (N);
2721 Formal := First_Formal (Subp);
2722 Parent_Formal := First_Formal (Parent_Subp);
2723 while Present (Formal) loop
2724 Actual_Typ := Etype (Actual);
2725 Formal_Typ := Etype (Formal);
2726 Parent_Typ := Etype (Parent_Formal);
2727
2728 -- For an IN parameter of a scalar type, the parent formal
2729 -- type and derived formal type differ or the parent formal
2730 -- type and actual type do not match statically.
2731
2732 if Is_Scalar_Type (Formal_Typ)
2733 and then Ekind (Formal) = E_In_Parameter
2734 and then Formal_Typ /= Parent_Typ
2735 and then
2736 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
2737 and then not Raises_Constraint_Error (Actual)
2738 then
2739 Convert (Actual, Parent_Typ);
2740 Enable_Range_Check (Actual);
2741
d79e621a
GD
2742 -- If the actual has been marked as requiring a range
2743 -- check, then generate it here.
2744
2745 if Do_Range_Check (Actual) then
2746 Set_Do_Range_Check (Actual, False);
2747 Generate_Range_Check
2748 (Actual, Etype (Formal), CE_Range_Check_Failed);
2749 end if;
2750
d4817e3f
HK
2751 -- For access types, the parent formal type and actual type
2752 -- differ.
2753
2754 elsif Is_Access_Type (Formal_Typ)
2755 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
70482933 2756 then
d4817e3f
HK
2757 if Ekind (Formal) /= E_In_Parameter then
2758 Convert (Actual, Parent_Typ);
2759
2760 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
2761 and then Designated_Type (Parent_Typ) /=
2762 Designated_Type (Actual_Typ)
2763 and then not Is_Controlling_Formal (Formal)
2764 then
2765 -- This unchecked conversion is not necessary unless
2766 -- inlining is enabled, because in that case the type
2767 -- mismatch may become visible in the body about to be
2768 -- inlined.
2769
2770 Rewrite (Actual,
2771 Unchecked_Convert_To (Parent_Typ,
2772 Relocate_Node (Actual)));
2773
90e9a6be
AC
2774 -- If the relocated node is a function call then it
2775 -- can be part of the expansion of the predefined
2776 -- equality operator of a tagged type and we may
2777 -- need to adjust its SCIL dispatching node.
2778
2779 if Generate_SCIL
2780 and then Nkind (Actual) /= N_Null
2781 and then Nkind (Expression (Actual))
2782 = N_Function_Call
2783 then
2784 Adjust_SCIL_Node (Actual, Expression (Actual));
2785 end if;
2786
d4817e3f
HK
2787 Analyze (Actual);
2788 Resolve (Actual, Parent_Typ);
2789 end if;
70482933 2790
d4817e3f
HK
2791 -- For array and record types, the parent formal type and
2792 -- derived formal type have different sizes or pragma Pack
2793 -- status.
70482933 2794
d4817e3f
HK
2795 elsif ((Is_Array_Type (Formal_Typ)
2796 and then Is_Array_Type (Parent_Typ))
2797 or else
2798 (Is_Record_Type (Formal_Typ)
2799 and then Is_Record_Type (Parent_Typ)))
2800 and then
2801 (Esize (Formal_Typ) /= Esize (Parent_Typ)
2802 or else Has_Pragma_Pack (Formal_Typ) /=
2803 Has_Pragma_Pack (Parent_Typ))
2804 then
2805 Convert (Actual, Parent_Typ);
70482933 2806 end if;
70482933 2807
d4817e3f
HK
2808 Next_Actual (Actual);
2809 Next_Formal (Formal);
2810 Next_Formal (Parent_Formal);
2811 end loop;
2812 end;
70482933
RK
2813 end if;
2814
2815 Orig_Subp := Subp;
2816 Subp := Parent_Subp;
2817 end if;
2818
8a36a0cc
AC
2819 -- Check for violation of No_Abort_Statements
2820
fbf5a39b
AC
2821 if Is_RTE (Subp, RE_Abort_Task) then
2822 Check_Restriction (No_Abort_Statements, N);
8a36a0cc
AC
2823
2824 -- Check for violation of No_Dynamic_Attachment
2825
2826 elsif RTU_Loaded (Ada_Interrupts)
2827 and then (Is_RTE (Subp, RE_Is_Reserved) or else
2828 Is_RTE (Subp, RE_Is_Attached) or else
2829 Is_RTE (Subp, RE_Current_Handler) or else
2830 Is_RTE (Subp, RE_Attach_Handler) or else
2831 Is_RTE (Subp, RE_Exchange_Handler) or else
2832 Is_RTE (Subp, RE_Detach_Handler) or else
2833 Is_RTE (Subp, RE_Reference))
2834 then
2835 Check_Restriction (No_Dynamic_Attachment, N);
fbf5a39b
AC
2836 end if;
2837
8a36a0cc
AC
2838 -- Deal with case where call is an explicit dereference
2839
c01a9391 2840 if Nkind (Name (N)) = N_Explicit_Dereference then
70482933
RK
2841
2842 -- Handle case of access to protected subprogram type
2843
f937473f 2844 if Is_Access_Protected_Subprogram_Type
d4817e3f 2845 (Base_Type (Etype (Prefix (Name (N)))))
70482933
RK
2846 then
2847 -- If this is a call through an access to protected operation,
2848 -- the prefix has the form (object'address, operation'access).
2849 -- Rewrite as a for other protected calls: the object is the
2850 -- first parameter of the list of actuals.
2851
2852 declare
2853 Call : Node_Id;
2854 Parm : List_Id;
2855 Nam : Node_Id;
2856 Obj : Node_Id;
fbf5a39b
AC
2857 Ptr : constant Node_Id := Prefix (Name (N));
2858
2859 T : constant Entity_Id :=
2860 Equivalent_Type (Base_Type (Etype (Ptr)));
2861
2862 D_T : constant Entity_Id :=
2863 Designated_Type (Base_Type (Etype (Ptr)));
70482933
RK
2864
2865 begin
f44fe430
RD
2866 Obj :=
2867 Make_Selected_Component (Loc,
2868 Prefix => Unchecked_Convert_To (T, Ptr),
2869 Selector_Name =>
2870 New_Occurrence_Of (First_Entity (T), Loc));
2871
2872 Nam :=
2873 Make_Selected_Component (Loc,
2874 Prefix => Unchecked_Convert_To (T, Ptr),
2875 Selector_Name =>
2876 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
70482933 2877
02822a92
RD
2878 Nam :=
2879 Make_Explicit_Dereference (Loc,
2880 Prefix => Nam);
70482933
RK
2881
2882 if Present (Parameter_Associations (N)) then
2883 Parm := Parameter_Associations (N);
2884 else
2885 Parm := New_List;
2886 end if;
2887
2888 Prepend (Obj, Parm);
2889
2890 if Etype (D_T) = Standard_Void_Type then
02822a92
RD
2891 Call :=
2892 Make_Procedure_Call_Statement (Loc,
2893 Name => Nam,
2894 Parameter_Associations => Parm);
70482933 2895 else
02822a92
RD
2896 Call :=
2897 Make_Function_Call (Loc,
2898 Name => Nam,
2899 Parameter_Associations => Parm);
70482933
RK
2900 end if;
2901
2902 Set_First_Named_Actual (Call, First_Named_Actual (N));
70482933
RK
2903 Set_Etype (Call, Etype (D_T));
2904
2905 -- We do not re-analyze the call to avoid infinite recursion.
2906 -- We analyze separately the prefix and the object, and set
2907 -- the checks on the prefix that would otherwise be emitted
2908 -- when resolving a call.
2909
2910 Rewrite (N, Call);
2911 Analyze (Nam);
2912 Apply_Access_Check (Nam);
2913 Analyze (Obj);
2914 return;
2915 end;
2916 end if;
2917 end if;
2918
2919 -- If this is a call to an intrinsic subprogram, then perform the
2920 -- appropriate expansion to the corresponding tree node and we
2921 -- are all done (since after that the call is gone!)
2922
98f01d53
AC
2923 -- In the case where the intrinsic is to be processed by the back end,
2924 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
2925 -- since the idea in this case is to pass the call unchanged.
d766cee3
RD
2926 -- If the intrinsic is an inherited unchecked conversion, and the
2927 -- derived type is the target type of the conversion, we must retain
2928 -- it as the return type of the expression. Otherwise the expansion
2929 -- below, which uses the parent operation, will yield the wrong type.
98f01d53 2930
70482933
RK
2931 if Is_Intrinsic_Subprogram (Subp) then
2932 Expand_Intrinsic_Call (N, Subp);
d766cee3
RD
2933
2934 if Nkind (N) = N_Unchecked_Type_Conversion
2935 and then Parent_Subp /= Orig_Subp
2936 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
2937 then
2938 Set_Etype (N, Etype (Orig_Subp));
2939 end if;
2940
70482933
RK
2941 return;
2942 end if;
2943
b29def53
AC
2944 if Ekind_In (Subp, E_Function, E_Procedure) then
2945
26a43556 2946 -- We perform two simple optimization on calls:
8dbf3473 2947
3563739b 2948 -- a) replace calls to null procedures unconditionally;
26a43556 2949
3563739b 2950 -- b) for To_Address, just do an unchecked conversion. Not only is
26a43556
AC
2951 -- this efficient, but it also avoids order of elaboration problems
2952 -- when address clauses are inlined (address expression elaborated
2953 -- at the wrong point).
2954
2955 -- We perform these optimization regardless of whether we are in the
2956 -- main unit or in a unit in the context of the main unit, to ensure
2957 -- that tree generated is the same in both cases, for Inspector use.
2958
2959 if Is_RTE (Subp, RE_To_Address) then
2960 Rewrite (N,
2961 Unchecked_Convert_To
2962 (RTE (RE_Address), Relocate_Node (First_Actual (N))));
2963 return;
2964
2965 elsif Is_Null_Procedure (Subp) then
8dbf3473
AC
2966 Rewrite (N, Make_Null_Statement (Loc));
2967 return;
2968 end if;
2969
70482933
RK
2970 if Is_Inlined (Subp) then
2971
a41ea816 2972 Inlined_Subprogram : declare
fbf5a39b
AC
2973 Bod : Node_Id;
2974 Must_Inline : Boolean := False;
2975 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
5b4994bc 2976 Scop : constant Entity_Id := Scope (Subp);
70482933 2977
a41ea816 2978 function In_Unfrozen_Instance return Boolean;
26a43556
AC
2979 -- If the subprogram comes from an instance in the same unit,
2980 -- and the instance is not yet frozen, inlining might trigger
2981 -- order-of-elaboration problems in gigi.
a41ea816
AC
2982
2983 --------------------------
2984 -- In_Unfrozen_Instance --
2985 --------------------------
2986
2987 function In_Unfrozen_Instance return Boolean is
2f1b20a9 2988 S : Entity_Id;
a41ea816
AC
2989
2990 begin
2f1b20a9 2991 S := Scop;
a41ea816
AC
2992 while Present (S)
2993 and then S /= Standard_Standard
2994 loop
2995 if Is_Generic_Instance (S)
2996 and then Present (Freeze_Node (S))
2997 and then not Analyzed (Freeze_Node (S))
2998 then
2999 return True;
3000 end if;
3001
3002 S := Scope (S);
3003 end loop;
3004
3005 return False;
3006 end In_Unfrozen_Instance;
3007
3008 -- Start of processing for Inlined_Subprogram
3009
70482933 3010 begin
2f1b20a9
ES
3011 -- Verify that the body to inline has already been seen, and
3012 -- that if the body is in the current unit the inlining does
3013 -- not occur earlier. This avoids order-of-elaboration problems
3014 -- in the back end.
3015
3016 -- This should be documented in sinfo/einfo ???
70482933 3017
fbf5a39b
AC
3018 if No (Spec)
3019 or else Nkind (Spec) /= N_Subprogram_Declaration
3020 or else No (Body_To_Inline (Spec))
70482933 3021 then
fbf5a39b
AC
3022 Must_Inline := False;
3023
26a43556
AC
3024 -- If this an inherited function that returns a private type,
3025 -- do not inline if the full view is an unconstrained array,
3026 -- because such calls cannot be inlined.
5b4994bc
AC
3027
3028 elsif Present (Orig_Subp)
3029 and then Is_Array_Type (Etype (Orig_Subp))
3030 and then not Is_Constrained (Etype (Orig_Subp))
3031 then
3032 Must_Inline := False;
3033
a41ea816 3034 elsif In_Unfrozen_Instance then
5b4994bc
AC
3035 Must_Inline := False;
3036
fbf5a39b
AC
3037 else
3038 Bod := Body_To_Inline (Spec);
3039
3040 if (In_Extended_Main_Code_Unit (N)
3041 or else In_Extended_Main_Code_Unit (Parent (N))
ac4d6407 3042 or else Has_Pragma_Inline_Always (Subp))
fbf5a39b
AC
3043 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3044 or else
3045 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3046 then
3047 Must_Inline := True;
3048
3049 -- If we are compiling a package body that is not the main
3050 -- unit, it must be for inlining/instantiation purposes,
3051 -- in which case we inline the call to insure that the same
3052 -- temporaries are generated when compiling the body by
3053 -- itself. Otherwise link errors can occur.
3054
2820d220
AC
3055 -- If the function being called is itself in the main unit,
3056 -- we cannot inline, because there is a risk of double
3057 -- elaboration and/or circularity: the inlining can make
3058 -- visible a private entity in the body of the main unit,
3059 -- that gigi will see before its sees its proper definition.
3060
fbf5a39b
AC
3061 elsif not (In_Extended_Main_Code_Unit (N))
3062 and then In_Package_Body
3063 then
2820d220 3064 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
fbf5a39b
AC
3065 end if;
3066 end if;
3067
3068 if Must_Inline then
70482933
RK
3069 Expand_Inlined_Call (N, Subp, Orig_Subp);
3070
3071 else
fbf5a39b 3072 -- Let the back end handle it
70482933
RK
3073
3074 Add_Inlined_Body (Subp);
3075
3076 if Front_End_Inlining
3077 and then Nkind (Spec) = N_Subprogram_Declaration
3078 and then (In_Extended_Main_Code_Unit (N))
3079 and then No (Body_To_Inline (Spec))
3080 and then not Has_Completion (Subp)
3081 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
70482933 3082 then
fbf5a39b 3083 Cannot_Inline
26a43556 3084 ("cannot inline& (body not seen yet)?", N, Subp);
70482933
RK
3085 end if;
3086 end if;
a41ea816 3087 end Inlined_Subprogram;
70482933
RK
3088 end if;
3089 end if;
3090
26a43556
AC
3091 -- Check for protected subprogram. This is either an intra-object call,
3092 -- or a protected function call. Protected procedure calls are rewritten
3093 -- as entry calls and handled accordingly.
70482933 3094
26a43556
AC
3095 -- In Ada 2005, this may be an indirect call to an access parameter that
3096 -- is an access_to_subprogram. In that case the anonymous type has a
3097 -- scope that is a protected operation, but the call is a regular one.
6f76a257 3098 -- In either case do not expand call if subprogram is eliminated.
c8ef728f 3099
70482933
RK
3100 Scop := Scope (Subp);
3101
3102 if Nkind (N) /= N_Entry_Call_Statement
3103 and then Is_Protected_Type (Scop)
c8ef728f 3104 and then Ekind (Subp) /= E_Subprogram_Type
6f76a257 3105 and then not Is_Eliminated (Subp)
70482933 3106 then
26a43556
AC
3107 -- If the call is an internal one, it is rewritten as a call to the
3108 -- corresponding unprotected subprogram.
70482933
RK
3109
3110 Expand_Protected_Subprogram_Call (N, Subp, Scop);
3111 end if;
3112
26a43556
AC
3113 -- Functions returning controlled objects need special attention:
3114 -- if the return type is limited, the context is an initialization
6eab5a95
AC
3115 -- and different processing applies. If the call is to a protected
3116 -- function, the expansion above will call Expand_Call recusively.
3117 -- To prevent a double attachment, check that the current call is
3118 -- not a rewriting of a protected function call.
70482933 3119
048e5cef 3120 if Needs_Finalization (Etype (Subp))
02822a92 3121 and then not Is_Inherently_Limited_Type (Etype (Subp))
6eab5a95
AC
3122 and then
3123 (No (First_Formal (Subp))
3124 or else
3125 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
70482933
RK
3126 then
3127 Expand_Ctrl_Function_Call (N);
3128 end if;
3129
26a43556
AC
3130 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3131 -- if there are optional parameters at the trailing end.
3132 -- Note: we never delete procedures for call via a pointer.
70482933
RK
3133
3134 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3135 and then Present (First_Optional_Parameter (Subp))
3136 then
3137 declare
3138 Last_Keep_Arg : Node_Id;
3139
3140 begin
26a43556
AC
3141 -- Last_Keep_Arg will hold the last actual that should be kept.
3142 -- If it remains empty at the end, it means that all parameters
3143 -- are optional.
70482933
RK
3144
3145 Last_Keep_Arg := Empty;
3146
26a43556
AC
3147 -- Find first optional parameter, must be present since we checked
3148 -- the validity of the parameter before setting it.
70482933
RK
3149
3150 Formal := First_Formal (Subp);
3151 Actual := First_Actual (N);
3152 while Formal /= First_Optional_Parameter (Subp) loop
3153 Last_Keep_Arg := Actual;
3154 Next_Formal (Formal);
3155 Next_Actual (Actual);
3156 end loop;
3157
fbf5a39b
AC
3158 -- We have Formal and Actual pointing to the first potentially
3159 -- droppable argument. We can drop all the trailing arguments
3160 -- whose actual matches the default. Note that we know that all
3161 -- remaining formals have defaults, because we checked that this
3162 -- requirement was met before setting First_Optional_Parameter.
70482933
RK
3163
3164 -- We use Fully_Conformant_Expressions to check for identity
3165 -- between formals and actuals, which may miss some cases, but
3166 -- on the other hand, this is only an optimization (if we fail
3167 -- to truncate a parameter it does not affect functionality).
3168 -- So if the default is 3 and the actual is 1+2, we consider
3169 -- them unequal, which hardly seems worrisome.
3170
3171 while Present (Formal) loop
3172 if not Fully_Conformant_Expressions
3173 (Actual, Default_Value (Formal))
3174 then
3175 Last_Keep_Arg := Actual;
3176 end if;
3177
3178 Next_Formal (Formal);
3179 Next_Actual (Actual);
3180 end loop;
3181
3182 -- If no arguments, delete entire list, this is the easy case
3183
3184 if No (Last_Keep_Arg) then
70482933
RK
3185 Set_Parameter_Associations (N, No_List);
3186 Set_First_Named_Actual (N, Empty);
3187
3188 -- Case where at the last retained argument is positional. This
3189 -- is also an easy case, since the retained arguments are already
3190 -- in the right form, and we don't need to worry about the order
3191 -- of arguments that get eliminated.
3192
3193 elsif Is_List_Member (Last_Keep_Arg) then
3194 while Present (Next (Last_Keep_Arg)) loop
ac4d6407 3195 Discard_Node (Remove_Next (Last_Keep_Arg));
70482933
RK
3196 end loop;
3197
3198 Set_First_Named_Actual (N, Empty);
3199
3200 -- This is the annoying case where the last retained argument
3201 -- is a named parameter. Since the original arguments are not
3202 -- in declaration order, we may have to delete some fairly
3203 -- random collection of arguments.
3204
3205 else
3206 declare
3207 Temp : Node_Id;
3208 Passoc : Node_Id;
fbf5a39b 3209
70482933
RK
3210 begin
3211 -- First step, remove all the named parameters from the
3212 -- list (they are still chained using First_Named_Actual
3213 -- and Next_Named_Actual, so we have not lost them!)
3214
3215 Temp := First (Parameter_Associations (N));
3216
3217 -- Case of all parameters named, remove them all
3218
3219 if Nkind (Temp) = N_Parameter_Association then
3220 while Is_Non_Empty_List (Parameter_Associations (N)) loop
3221 Temp := Remove_Head (Parameter_Associations (N));
3222 end loop;
3223
3224 -- Case of mixed positional/named, remove named parameters
3225
3226 else
3227 while Nkind (Next (Temp)) /= N_Parameter_Association loop
3228 Next (Temp);
3229 end loop;
3230
3231 while Present (Next (Temp)) loop
7888a6ae 3232 Remove (Next (Temp));
70482933
RK
3233 end loop;
3234 end if;
3235
3236 -- Now we loop through the named parameters, till we get
3237 -- to the last one to be retained, adding them to the list.
3238 -- Note that the Next_Named_Actual list does not need to be
3239 -- touched since we are only reordering them on the actual
3240 -- parameter association list.
3241
3242 Passoc := Parent (First_Named_Actual (N));
3243 loop
3244 Temp := Relocate_Node (Passoc);
3245 Append_To
3246 (Parameter_Associations (N), Temp);
3247 exit when
3248 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
3249 Passoc := Parent (Next_Named_Actual (Passoc));
3250 end loop;
3251
3252 Set_Next_Named_Actual (Temp, Empty);
3253
3254 loop
3255 Temp := Next_Named_Actual (Passoc);
3256 exit when No (Temp);
3257 Set_Next_Named_Actual
3258 (Passoc, Next_Named_Actual (Parent (Temp)));
70482933
RK
3259 end loop;
3260 end;
811c6a85 3261
70482933
RK
3262 end if;
3263 end;
3264 end if;
70482933
RK
3265 end Expand_Call;
3266
3267 --------------------------
3268 -- Expand_Inlined_Call --
3269 --------------------------
3270
3271 procedure Expand_Inlined_Call
3272 (N : Node_Id;
3273 Subp : Entity_Id;
3274 Orig_Subp : Entity_Id)
3275 is
fbf5a39b
AC
3276 Loc : constant Source_Ptr := Sloc (N);
3277 Is_Predef : constant Boolean :=
3278 Is_Predefined_File_Name
3279 (Unit_File_Name (Get_Source_Unit (Subp)));
3280 Orig_Bod : constant Node_Id :=
3281 Body_To_Inline (Unit_Declaration_Node (Subp));
3282
70482933
RK
3283 Blk : Node_Id;
3284 Bod : Node_Id;
3285 Decl : Node_Id;
c8ef728f 3286 Decls : constant List_Id := New_List;
70482933
RK
3287 Exit_Lab : Entity_Id := Empty;
3288 F : Entity_Id;
3289 A : Node_Id;
3290 Lab_Decl : Node_Id;
3291 Lab_Id : Node_Id;
3292 New_A : Node_Id;
3293 Num_Ret : Int := 0;
70482933
RK
3294 Ret_Type : Entity_Id;
3295 Targ : Node_Id;
c8ef728f 3296 Targ1 : Node_Id;
70482933
RK
3297 Temp : Entity_Id;
3298 Temp_Typ : Entity_Id;
3299
3e2399ba
AC
3300 Return_Object : Entity_Id := Empty;
3301 -- Entity in declaration in an extended_return_statement
3302
c8ef728f
ES
3303 Is_Unc : constant Boolean :=
3304 Is_Array_Type (Etype (Subp))
3305 and then not Is_Constrained (Etype (Subp));
26a43556
AC
3306 -- If the type returned by the function is unconstrained and the call
3307 -- can be inlined, special processing is required.
c8ef728f 3308
70482933 3309 procedure Make_Exit_Label;
26a43556 3310 -- Build declaration for exit label to be used in Return statements,
c12beea0
RD
3311 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3312 -- declaration). Does nothing if Exit_Lab already set.
70482933
RK
3313
3314 function Process_Formals (N : Node_Id) return Traverse_Result;
26a43556
AC
3315 -- Replace occurrence of a formal with the corresponding actual, or the
3316 -- thunk generated for it.
70482933 3317
fbf5a39b 3318 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
26a43556
AC
3319 -- If the call being expanded is that of an internal subprogram, set the
3320 -- sloc of the generated block to that of the call itself, so that the
3321 -- expansion is skipped by the "next" command in gdb.
fbf5a39b 3322 -- Same processing for a subprogram in a predefined file, e.g.
26a43556
AC
3323 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
3324 -- simplify our own development.
fbf5a39b 3325
70482933
RK
3326 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3327 -- If the function body is a single expression, replace call with
3328 -- expression, else insert block appropriately.
3329
3330 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3331 -- If procedure body has no local variables, inline body without
02822a92 3332 -- creating block, otherwise rewrite call with block.
70482933 3333
5453d5bd
AC
3334 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
3335 -- Determine whether a formal parameter is used only once in Orig_Bod
3336
70482933
RK
3337 ---------------------
3338 -- Make_Exit_Label --
3339 ---------------------
3340
3341 procedure Make_Exit_Label is
c12beea0 3342 Lab_Ent : Entity_Id;
70482933 3343 begin
70482933 3344 if No (Exit_Lab) then
c12beea0
RD
3345 Lab_Ent := Make_Temporary (Loc, 'L');
3346 Lab_Id := New_Reference_To (Lab_Ent, Loc);
70482933 3347 Exit_Lab := Make_Label (Loc, Lab_Id);
70482933
RK
3348 Lab_Decl :=
3349 Make_Implicit_Label_Declaration (Loc,
c12beea0 3350 Defining_Identifier => Lab_Ent,
70482933
RK
3351 Label_Construct => Exit_Lab);
3352 end if;
3353 end Make_Exit_Label;
3354
3355 ---------------------
3356 -- Process_Formals --
3357 ---------------------
3358
3359 function Process_Formals (N : Node_Id) return Traverse_Result is
3360 A : Entity_Id;
3361 E : Entity_Id;
3362 Ret : Node_Id;
3363
3364 begin
3365 if Is_Entity_Name (N)
3366 and then Present (Entity (N))
3367 then
3368 E := Entity (N);
3369
3370 if Is_Formal (E)
3371 and then Scope (E) = Subp
3372 then
3373 A := Renamed_Object (E);
3374
02822a92
RD
3375 -- Rewrite the occurrence of the formal into an occurrence of
3376 -- the actual. Also establish visibility on the proper view of
3377 -- the actual's subtype for the body's context (if the actual's
3378 -- subtype is private at the call point but its full view is
3379 -- visible to the body, then the inlined tree here must be
3380 -- analyzed with the full view).
3381
70482933
RK
3382 if Is_Entity_Name (A) then
3383 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
02822a92 3384 Check_Private_View (N);
70482933
RK
3385
3386 elsif Nkind (A) = N_Defining_Identifier then
3387 Rewrite (N, New_Occurrence_Of (A, Loc));
02822a92 3388 Check_Private_View (N);
70482933 3389
d766cee3
RD
3390 -- Numeric literal
3391
3392 else
70482933
RK
3393 Rewrite (N, New_Copy (A));
3394 end if;
3395 end if;
3e2399ba
AC
3396 return Skip;
3397
3398 elsif Is_Entity_Name (N)
9f5b6c7f 3399 and then Present (Return_Object)
3e2399ba
AC
3400 and then Chars (N) = Chars (Return_Object)
3401 then
3402 -- Occurrence within an extended return statement. The return
3403 -- object is local to the body been inlined, and thus the generic
3404 -- copy is not analyzed yet, so we match by name, and replace it
3405 -- with target of call.
3406
3407 if Nkind (Targ) = N_Defining_Identifier then
3408 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3409 else
3410 Rewrite (N, New_Copy_Tree (Targ));
3411 end if;
70482933
RK
3412
3413 return Skip;
3414
d766cee3 3415 elsif Nkind (N) = N_Simple_Return_Statement then
70482933
RK
3416 if No (Expression (N)) then
3417 Make_Exit_Label;
d766cee3 3418 Rewrite (N,
3e2399ba 3419 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
70482933
RK
3420
3421 else
3422 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3423 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3424 then
fbf5a39b 3425 -- Function body is a single expression. No need for
70482933 3426 -- exit label.
fbf5a39b 3427
70482933
RK
3428 null;
3429
3430 else
3431 Num_Ret := Num_Ret + 1;
3432 Make_Exit_Label;
3433 end if;
3434
3435 -- Because of the presence of private types, the views of the
3436 -- expression and the context may be different, so place an
3437 -- unchecked conversion to the context type to avoid spurious
8fc789c8 3438 -- errors, e.g. when the expression is a numeric literal and
70482933
RK
3439 -- the context is private. If the expression is an aggregate,
3440 -- use a qualified expression, because an aggregate is not a
3441 -- legal argument of a conversion.
3442
ac4d6407 3443 if Nkind_In (Expression (N), N_Aggregate, N_Null) then
70482933
RK
3444 Ret :=
3445 Make_Qualified_Expression (Sloc (N),
3446 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3447 Expression => Relocate_Node (Expression (N)));
3448 else
3449 Ret :=
3450 Unchecked_Convert_To
3451 (Ret_Type, Relocate_Node (Expression (N)));
3452 end if;
3453
3454 if Nkind (Targ) = N_Defining_Identifier then
3455 Rewrite (N,
3456 Make_Assignment_Statement (Loc,
3457 Name => New_Occurrence_Of (Targ, Loc),
3458 Expression => Ret));
3459 else
3460 Rewrite (N,
3461 Make_Assignment_Statement (Loc,
3462 Name => New_Copy (Targ),
3463 Expression => Ret));
3464 end if;
3465
3466 Set_Assignment_OK (Name (N));
3467
3468 if Present (Exit_Lab) then
3469 Insert_After (N,
3470 Make_Goto_Statement (Loc,
3471 Name => New_Copy (Lab_Id)));
3472 end if;
3473 end if;
3474
3475 return OK;
3476
3e2399ba
AC
3477 elsif Nkind (N) = N_Extended_Return_Statement then
3478
3479 -- An extended return becomes a block whose first statement is
3480 -- the assignment of the initial expression of the return object
3481 -- to the target of the call itself.
3482
3483 declare
3484 Return_Decl : constant Entity_Id :=
3485 First (Return_Object_Declarations (N));
3486 Assign : Node_Id;
3487
3488 begin
3489 Return_Object := Defining_Identifier (Return_Decl);
3490
3491 if Present (Expression (Return_Decl)) then
3492 if Nkind (Targ) = N_Defining_Identifier then
3493 Assign :=
3494 Make_Assignment_Statement (Loc,
3495 Name => New_Occurrence_Of (Targ, Loc),
3496 Expression => Expression (Return_Decl));
3497 else
3498 Assign :=
3499 Make_Assignment_Statement (Loc,
3500 Name => New_Copy (Targ),
3501 Expression => Expression (Return_Decl));
3502 end if;
3503
3504 Set_Assignment_OK (Name (Assign));
3505 Prepend (Assign,
3506 Statements (Handled_Statement_Sequence (N)));
3507 end if;
3508
3509 Rewrite (N,
3510 Make_Block_Statement (Loc,
3511 Handled_Statement_Sequence =>
3512 Handled_Statement_Sequence (N)));
3513
3514 return OK;
3515 end;
3516
fbf5a39b
AC
3517 -- Remove pragma Unreferenced since it may refer to formals that
3518 -- are not visible in the inlined body, and in any case we will
3519 -- not be posting warnings on the inlined body so it is unneeded.
3520
3521 elsif Nkind (N) = N_Pragma
1923d2d6 3522 and then Pragma_Name (N) = Name_Unreferenced
fbf5a39b
AC
3523 then
3524 Rewrite (N, Make_Null_Statement (Sloc (N)));
3525 return OK;
3526
70482933
RK
3527 else
3528 return OK;
3529 end if;
3530 end Process_Formals;
3531
3532 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3533
fbf5a39b
AC
3534 ------------------
3535 -- Process_Sloc --
3536 ------------------
3537
3538 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3539 begin
3540 if not Debug_Generated_Code then
3541 Set_Sloc (Nod, Sloc (N));
3542 Set_Comes_From_Source (Nod, False);
3543 end if;
3544
3545 return OK;
3546 end Process_Sloc;
3547
3548 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3549
70482933
RK
3550 ---------------------------
3551 -- Rewrite_Function_Call --
3552 ---------------------------
3553
3554 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
fbf5a39b
AC
3555 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3556 Fst : constant Node_Id := First (Statements (HSS));
70482933
RK
3557
3558 begin
70482933
RK
3559 -- Optimize simple case: function body is a single return statement,
3560 -- which has been expanded into an assignment.
3561
3562 if Is_Empty_List (Declarations (Blk))
3563 and then Nkind (Fst) = N_Assignment_Statement
3564 and then No (Next (Fst))
3565 then
3566
3567 -- The function call may have been rewritten as the temporary
3568 -- that holds the result of the call, in which case remove the
3569 -- now useless declaration.
3570
3571 if Nkind (N) = N_Identifier
3572 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3573 then
3574 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3575 end if;
3576
3577 Rewrite (N, Expression (Fst));
3578
3579 elsif Nkind (N) = N_Identifier
3580 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3581 then
98f01d53 3582 -- The block assigns the result of the call to the temporary
70482933
RK
3583
3584 Insert_After (Parent (Entity (N)), Blk);
3585
3586 elsif Nkind (Parent (N)) = N_Assignment_Statement
c8ef728f
ES
3587 and then
3588 (Is_Entity_Name (Name (Parent (N)))
3589 or else
3590 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3591 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
70482933 3592 then
fbf5a39b 3593 -- Replace assignment with the block
70482933 3594
30c20106
AC
3595 declare
3596 Original_Assignment : constant Node_Id := Parent (N);
7324bf49
AC
3597
3598 begin
2f1b20a9
ES
3599 -- Preserve the original assignment node to keep the complete
3600 -- assignment subtree consistent enough for Analyze_Assignment
3601 -- to proceed (specifically, the original Lhs node must still
3602 -- have an assignment statement as its parent).
7324bf49 3603
2f1b20a9
ES
3604 -- We cannot rely on Original_Node to go back from the block
3605 -- node to the assignment node, because the assignment might
3606 -- already be a rewrite substitution.
30c20106 3607
7324bf49 3608 Discard_Node (Relocate_Node (Original_Assignment));
30c20106
AC
3609 Rewrite (Original_Assignment, Blk);
3610 end;
70482933
RK
3611
3612 elsif Nkind (Parent (N)) = N_Object_Declaration then
3613 Set_Expression (Parent (N), Empty);
3614 Insert_After (Parent (N), Blk);
c8ef728f
ES
3615
3616 elsif Is_Unc then
3617 Insert_Before (Parent (N), Blk);
70482933
RK
3618 end if;
3619 end Rewrite_Function_Call;
3620
3621 ----------------------------
3622 -- Rewrite_Procedure_Call --
3623 ----------------------------
3624
3625 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
fbf5a39b 3626 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
70482933 3627 begin
02822a92
RD
3628 -- If there is a transient scope for N, this will be the scope of the
3629 -- actions for N, and the statements in Blk need to be within this
3630 -- scope. For example, they need to have visibility on the constant
3631 -- declarations created for the formals.
3632
3633 -- If N needs no transient scope, and if there are no declarations in
3634 -- the inlined body, we can do a little optimization and insert the
3635 -- statements for the body directly after N, and rewrite N to a
3636 -- null statement, instead of rewriting N into a full-blown block
3637 -- statement.
3638
3639 if not Scope_Is_Transient
3640 and then Is_Empty_List (Declarations (Blk))
3641 then
70482933
RK
3642 Insert_List_After (N, Statements (HSS));
3643 Rewrite (N, Make_Null_Statement (Loc));
3644 else
3645 Rewrite (N, Blk);
3646 end if;
3647 end Rewrite_Procedure_Call;
3648
5453d5bd
AC
3649 -------------------------
3650 -- Formal_Is_Used_Once --
02822a92 3651 -------------------------
5453d5bd
AC
3652
3653 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
3654 Use_Counter : Int := 0;
3655
3656 function Count_Uses (N : Node_Id) return Traverse_Result;
3657 -- Traverse the tree and count the uses of the formal parameter.
3658 -- In this case, for optimization purposes, we do not need to
3659 -- continue the traversal once more than one use is encountered.
3660
cc335f43
AC
3661 ----------------
3662 -- Count_Uses --
3663 ----------------
3664
5453d5bd
AC
3665 function Count_Uses (N : Node_Id) return Traverse_Result is
3666 begin
5453d5bd
AC
3667 -- The original node is an identifier
3668
3669 if Nkind (N) = N_Identifier
3670 and then Present (Entity (N))
3671
2f1b20a9 3672 -- Original node's entity points to the one in the copied body
5453d5bd
AC
3673
3674 and then Nkind (Entity (N)) = N_Identifier
3675 and then Present (Entity (Entity (N)))
3676
3677 -- The entity of the copied node is the formal parameter
3678
3679 and then Entity (Entity (N)) = Formal
3680 then
3681 Use_Counter := Use_Counter + 1;
3682
3683 if Use_Counter > 1 then
3684
3685 -- Denote more than one use and abandon the traversal
3686
3687 Use_Counter := 2;
3688 return Abandon;
3689
3690 end if;
3691 end if;
3692
3693 return OK;
3694 end Count_Uses;
3695
3696 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
3697
3698 -- Start of processing for Formal_Is_Used_Once
3699
3700 begin
5453d5bd
AC
3701 Count_Formal_Uses (Orig_Bod);
3702 return Use_Counter = 1;
5453d5bd
AC
3703 end Formal_Is_Used_Once;
3704
70482933
RK
3705 -- Start of processing for Expand_Inlined_Call
3706
3707 begin
8dbf3473 3708
f44fe430
RD
3709 -- Check for an illegal attempt to inline a recursive procedure. If the
3710 -- subprogram has parameters this is detected when trying to supply a
3711 -- binding for parameters that already have one. For parameterless
3712 -- subprograms this must be done explicitly.
3713
3714 if In_Open_Scopes (Subp) then
3715 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
3716 Set_Is_Inlined (Subp, False);
3717 return;
3718 end if;
3719
2ccf2fb3
ES
3720 if Nkind (Orig_Bod) = N_Defining_Identifier
3721 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
3722 then
8a45b58c
RD
3723 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3724 -- can be replaced with calls to the renamed entity directly, because
3725 -- the subprograms are subtype conformant. If the renamed subprogram
3726 -- is an inherited operation, we must redo the expansion because
3727 -- implicit conversions may be needed. Similarly, if the renamed
3728 -- entity is inlined, expand the call for further optimizations.
70482933
RK
3729
3730 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
f44fe430 3731
9c8ff9b9
AC
3732 if Present (Alias (Orig_Bod))
3733 or else Is_Inlined (Orig_Bod)
3734 then
f44fe430
RD
3735 Expand_Call (N);
3736 end if;
3737
70482933
RK
3738 return;
3739 end if;
3740
3741 -- Use generic machinery to copy body of inlined subprogram, as if it
3742 -- were an instantiation, resetting source locations appropriately, so
3743 -- that nested inlined calls appear in the main unit.
3744
3745 Save_Env (Subp, Empty);
fbf5a39b 3746 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
70482933 3747
fbf5a39b 3748 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
70482933
RK
3749 Blk :=
3750 Make_Block_Statement (Loc,
3751 Declarations => Declarations (Bod),
3752 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
3753
3754 if No (Declarations (Bod)) then
3755 Set_Declarations (Blk, New_List);
3756 end if;
3757
c8ef728f 3758 -- For the unconstrained case, capture the name of the local
02822a92
RD
3759 -- variable that holds the result. This must be the first declaration
3760 -- in the block, because its bounds cannot depend on local variables.
3761 -- Otherwise there is no way to declare the result outside of the
3762 -- block. Needless to say, in general the bounds will depend on the
3763 -- actuals in the call.
c8ef728f
ES
3764
3765 if Is_Unc then
02822a92 3766 Targ1 := Defining_Identifier (First (Declarations (Blk)));
c8ef728f
ES
3767 end if;
3768
98f01d53 3769 -- If this is a derived function, establish the proper return type
70482933
RK
3770
3771 if Present (Orig_Subp)
3772 and then Orig_Subp /= Subp
3773 then
3774 Ret_Type := Etype (Orig_Subp);
3775 else
3776 Ret_Type := Etype (Subp);
3777 end if;
3778
70482933
RK
3779 -- Create temporaries for the actuals that are expressions, or that
3780 -- are scalars and require copying to preserve semantics.
3781
2f1b20a9
ES
3782 F := First_Formal (Subp);
3783 A := First_Actual (N);
70482933 3784 while Present (F) loop
70482933 3785 if Present (Renamed_Object (F)) then
2f1b20a9 3786 Error_Msg_N ("cannot inline call to recursive subprogram", N);
70482933
RK
3787 return;
3788 end if;
3789
3790 -- If the argument may be a controlling argument in a call within
f44fe430
RD
3791 -- the inlined body, we must preserve its classwide nature to insure
3792 -- that dynamic dispatching take place subsequently. If the formal
3793 -- has a constraint it must be preserved to retain the semantics of
3794 -- the body.
70482933
RK
3795
3796 if Is_Class_Wide_Type (Etype (F))
3797 or else (Is_Access_Type (Etype (F))
3798 and then
3799 Is_Class_Wide_Type (Designated_Type (Etype (F))))
3800 then
3801 Temp_Typ := Etype (F);
3802
3803 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3804 and then Etype (F) /= Base_Type (Etype (F))
3805 then
3806 Temp_Typ := Etype (F);
3807
3808 else
3809 Temp_Typ := Etype (A);
3810 end if;
3811
5b4994bc
AC
3812 -- If the actual is a simple name or a literal, no need to
3813 -- create a temporary, object can be used directly.
70482933 3814
7888a6ae
GD
3815 -- If the actual is a literal and the formal has its address taken,
3816 -- we cannot pass the literal itself as an argument, so its value
3817 -- must be captured in a temporary.
3818
fbf5a39b
AC
3819 if (Is_Entity_Name (A)
3820 and then
3821 (not Is_Scalar_Type (Etype (A))
3822 or else Ekind (Entity (A)) = E_Enumeration_Literal))
3823
5453d5bd
AC
3824 -- When the actual is an identifier and the corresponding formal
3825 -- is used only once in the original body, the formal can be
3826 -- substituted directly with the actual parameter.
3827
3828 or else (Nkind (A) = N_Identifier
3829 and then Formal_Is_Used_Once (F))
3830
7888a6ae 3831 or else
ac4d6407
RD
3832 (Nkind_In (A, N_Real_Literal,
3833 N_Integer_Literal,
3834 N_Character_Literal)
3835 and then not Address_Taken (F))
70482933 3836 then
fbf5a39b
AC
3837 if Etype (F) /= Etype (A) then
3838 Set_Renamed_Object
3839 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3840 else
3841 Set_Renamed_Object (F, A);
3842 end if;
3843
3844 else
c12beea0 3845 Temp := Make_Temporary (Loc, 'C');
70482933
RK
3846
3847 -- If the actual for an in/in-out parameter is a view conversion,
3848 -- make it into an unchecked conversion, given that an untagged
3849 -- type conversion is not a proper object for a renaming.
fbf5a39b 3850
70482933
RK
3851 -- In-out conversions that involve real conversions have already
3852 -- been transformed in Expand_Actuals.
3853
3854 if Nkind (A) = N_Type_Conversion
fbf5a39b 3855 and then Ekind (F) /= E_In_Parameter
70482933 3856 then
02822a92
RD
3857 New_A :=
3858 Make_Unchecked_Type_Conversion (Loc,
3859 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3860 Expression => Relocate_Node (Expression (A)));
70482933
RK
3861
3862 elsif Etype (F) /= Etype (A) then
3863 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3864 Temp_Typ := Etype (F);
3865
3866 else
3867 New_A := Relocate_Node (A);
3868 end if;
3869
3870 Set_Sloc (New_A, Sloc (N));
3871
02822a92
RD
3872 -- If the actual has a by-reference type, it cannot be copied, so
3873 -- its value is captured in a renaming declaration. Otherwise
7888a6ae 3874 -- declare a local constant initialized with the actual.
02822a92 3875
4a3b249c
RD
3876 -- We also use a renaming declaration for expressions of an array
3877 -- type that is not bit-packed, both for efficiency reasons and to
3878 -- respect the semantics of the call: in most cases the original
3879 -- call will pass the parameter by reference, and thus the inlined
3880 -- code will have the same semantics.
bafc9e1d 3881
70482933
RK
3882 if Ekind (F) = E_In_Parameter
3883 and then not Is_Limited_Type (Etype (A))
02822a92 3884 and then not Is_Tagged_Type (Etype (A))
bafc9e1d
AC
3885 and then
3886 (not Is_Array_Type (Etype (A))
f66d46ec 3887 or else not Is_Object_Reference (A)
bafc9e1d 3888 or else Is_Bit_Packed_Array (Etype (A)))
70482933
RK
3889 then
3890 Decl :=
3891 Make_Object_Declaration (Loc,
3892 Defining_Identifier => Temp,
3893 Constant_Present => True,
3894 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3895 Expression => New_A);
3896 else
3897 Decl :=
3898 Make_Object_Renaming_Declaration (Loc,
3899 Defining_Identifier => Temp,
3900 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3901 Name => New_A);
3902 end if;
3903
c8ef728f 3904 Append (Decl, Decls);
70482933 3905 Set_Renamed_Object (F, Temp);
70482933
RK
3906 end if;
3907
3908 Next_Formal (F);
3909 Next_Actual (A);
3910 end loop;
3911
3912 -- Establish target of function call. If context is not assignment or
3913 -- declaration, create a temporary as a target. The declaration for
3914 -- the temporary may be subsequently optimized away if the body is a
3915 -- single expression, or if the left-hand side of the assignment is
c8ef728f 3916 -- simple enough, i.e. an entity or an explicit dereference of one.
70482933
RK
3917
3918 if Ekind (Subp) = E_Function then
3919 if Nkind (Parent (N)) = N_Assignment_Statement
3920 and then Is_Entity_Name (Name (Parent (N)))
3921 then
3922 Targ := Name (Parent (N));
3923
c8ef728f
ES
3924 elsif Nkind (Parent (N)) = N_Assignment_Statement
3925 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3926 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3927 then
3928 Targ := Name (Parent (N));
3929
3e2399ba
AC
3930 elsif Nkind (Parent (N)) = N_Object_Declaration
3931 and then Is_Limited_Type (Etype (Subp))
3932 then
3933 Targ := Defining_Identifier (Parent (N));
3934
70482933 3935 else
98f01d53 3936 -- Replace call with temporary and create its declaration
70482933 3937
c12beea0 3938 Temp := Make_Temporary (Loc, 'C');
758c442c 3939 Set_Is_Internal (Temp);
70482933 3940
30783513 3941 -- For the unconstrained case, the generated temporary has the
4a3b249c
RD
3942 -- same constrained declaration as the result variable. It may
3943 -- eventually be possible to remove that temporary and use the
3944 -- result variable directly.
c8ef728f
ES
3945
3946 if Is_Unc then
3947 Decl :=
3948 Make_Object_Declaration (Loc,
3949 Defining_Identifier => Temp,
3950 Object_Definition =>
3951 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3952
3953 Replace_Formals (Decl);
3954
3955 else
3956 Decl :=
3957 Make_Object_Declaration (Loc,
3958 Defining_Identifier => Temp,
3959 Object_Definition =>
3960 New_Occurrence_Of (Ret_Type, Loc));
3961
3962 Set_Etype (Temp, Ret_Type);
3963 end if;
70482933
RK
3964
3965 Set_No_Initialization (Decl);
c8ef728f 3966 Append (Decl, Decls);
70482933
RK
3967 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3968 Targ := Temp;
3969 end if;
3970 end if;
3971
c8ef728f
ES
3972 Insert_Actions (N, Decls);
3973
98f01d53 3974 -- Traverse the tree and replace formals with actuals or their thunks.
70482933
RK
3975 -- Attach block to tree before analysis and rewriting.
3976
3977 Replace_Formals (Blk);
3978 Set_Parent (Blk, N);
3979
fbf5a39b
AC
3980 if not Comes_From_Source (Subp)
3981 or else Is_Predef
3982 then
3983 Reset_Slocs (Blk);
3984 end if;
3985
70482933
RK
3986 if Present (Exit_Lab) then
3987
3988 -- If the body was a single expression, the single return statement
3989 -- and the corresponding label are useless.
3990
3991 if Num_Ret = 1
3992 and then
3993 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3994 N_Goto_Statement
3995 then
3996 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3997 else
3998 Append (Lab_Decl, (Declarations (Blk)));
3999 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
4000 end if;
4001 end if;
4002
4003 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
4a3b249c 4004 -- conflicting private views that Gigi would ignore. If this is a
fbf5a39b
AC
4005 -- predefined unit, analyze with checks off, as is done in the non-
4006 -- inlined run-time units.
70482933
RK
4007
4008 declare
4009 I_Flag : constant Boolean := In_Inlined_Body;
4010
4011 begin
4012 In_Inlined_Body := True;
fbf5a39b
AC
4013
4014 if Is_Predef then
4015 declare
4016 Style : constant Boolean := Style_Check;
4017 begin
4018 Style_Check := False;
4019 Analyze (Blk, Suppress => All_Checks);
4020 Style_Check := Style;
4021 end;
4022
4023 else
4024 Analyze (Blk);
4025 end if;
4026
70482933
RK
4027 In_Inlined_Body := I_Flag;
4028 end;
4029
4030 if Ekind (Subp) = E_Procedure then
4031 Rewrite_Procedure_Call (N, Blk);
4032 else
4033 Rewrite_Function_Call (N, Blk);
c8ef728f
ES
4034
4035 -- For the unconstrained case, the replacement of the call has been
4036 -- made prior to the complete analysis of the generated declarations.
4037 -- Propagate the proper type now.
4038
4039 if Is_Unc then
4040 if Nkind (N) = N_Identifier then
4041 Set_Etype (N, Etype (Entity (N)));
4042 else
4043 Set_Etype (N, Etype (Targ1));
4044 end if;
4045 end if;
70482933
RK
4046 end if;
4047
4048 Restore_Env;
4049
98f01d53 4050 -- Cleanup mapping between formals and actuals for other expansions
70482933
RK
4051
4052 F := First_Formal (Subp);
70482933
RK
4053 while Present (F) loop
4054 Set_Renamed_Object (F, Empty);
4055 Next_Formal (F);
4056 end loop;
4057 end Expand_Inlined_Call;
4058
4059 ----------------------------
4060 -- Expand_N_Function_Call --
4061 ----------------------------
4062
4063 procedure Expand_N_Function_Call (N : Node_Id) is
70482933 4064 begin
ac4d6407 4065 Expand_Call (N);
c986420e 4066
4a3b249c
RD
4067 -- If the return value of a foreign compiled function is VAX Float, then
4068 -- expand the return (adjusts the location of the return value on
4069 -- Alpha/VMS, no-op everywhere else).
612c5336 4070 -- Comes_From_Source intercepts recursive expansion.
2acde248 4071
c986420e
DR
4072 if Vax_Float (Etype (N))
4073 and then Nkind (N) = N_Function_Call
c986420e
DR
4074 and then Present (Name (N))
4075 and then Present (Entity (Name (N)))
4076 and then Has_Foreign_Convention (Entity (Name (N)))
612c5336 4077 and then Comes_From_Source (Parent (N))
c986420e
DR
4078 then
4079 Expand_Vax_Foreign_Return (N);
4080 end if;
70482933
RK
4081 end Expand_N_Function_Call;
4082
4083 ---------------------------------------
4084 -- Expand_N_Procedure_Call_Statement --
4085 ---------------------------------------
4086
4087 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4088 begin
4089 Expand_Call (N);
4090 end Expand_N_Procedure_Call_Statement;
4091
4092 ------------------------------
4093 -- Expand_N_Subprogram_Body --
4094 ------------------------------
4095
4a3b249c
RD
4096 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4097 -- by the back-end.
70482933 4098
7888a6ae 4099 -- Add dummy push/pop label nodes at start and end to clear any local
4a3b249c 4100 -- exception indications if local-exception-to-goto optimization is active.
7888a6ae 4101
f44fe430
RD
4102 -- Add return statement if last statement in body is not a return statement
4103 -- (this makes things easier on Gigi which does not want to have to handle
4104 -- a missing return).
70482933
RK
4105
4106 -- Add call to Activate_Tasks if body is a task activator
4107
4108 -- Deal with possible detection of infinite recursion
4109
4110 -- Eliminate body completely if convention stubbed
4111
4112 -- Encode entity names within body, since we will not need to reference
4113 -- these entities any longer in the front end.
4114
4115 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4116
c9a4817d
RD
4117 -- Reset Pure indication if any parameter has root type System.Address
4118
12e0c41c
AC
4119 -- Wrap thread body
4120
70482933
RK
4121 procedure Expand_N_Subprogram_Body (N : Node_Id) is
4122 Loc : constant Source_Ptr := Sloc (N);
4123 H : constant Node_Id := Handled_Statement_Sequence (N);
c9a4817d 4124 Body_Id : Entity_Id;
70482933 4125 Except_H : Node_Id;
70482933 4126 L : List_Id;
70f91180 4127 Spec_Id : Entity_Id;
70482933
RK
4128
4129 procedure Add_Return (S : List_Id);
4130 -- Append a return statement to the statement sequence S if the last
4131 -- statement is not already a return or a goto statement. Note that
4a3b249c
RD
4132 -- the latter test is not critical, it does not matter if we add a few
4133 -- extra returns, since they get eliminated anyway later on.
70482933
RK
4134
4135 ----------------
4136 -- Add_Return --
4137 ----------------
4138
4139 procedure Add_Return (S : List_Id) is
7888a6ae
GD
4140 Last_Stm : Node_Id;
4141 Loc : Source_Ptr;
12e0c41c
AC
4142
4143 begin
7888a6ae
GD
4144 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4145 -- not relevant in this context since they are not executable.
12e0c41c 4146
7888a6ae
GD
4147 Last_Stm := Last (S);
4148 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
4149 Prev (Last_Stm);
4150 end loop;
12e0c41c 4151
7888a6ae 4152 -- Now insert return unless last statement is a transfer
12e0c41c 4153
7888a6ae 4154 if not Is_Transfer (Last_Stm) then
12e0c41c 4155
7888a6ae
GD
4156 -- The source location for the return is the end label of the
4157 -- procedure if present. Otherwise use the sloc of the last
4158 -- statement in the list. If the list comes from a generated
4159 -- exception handler and we are not debugging generated code,
4160 -- all the statements within the handler are made invisible
4161 -- to the debugger.
12e0c41c 4162
7888a6ae
GD
4163 if Nkind (Parent (S)) = N_Exception_Handler
4164 and then not Comes_From_Source (Parent (S))
4165 then
4166 Loc := Sloc (Last_Stm);
12e0c41c 4167
7888a6ae
GD
4168 elsif Present (End_Label (H)) then
4169 Loc := Sloc (End_Label (H));
12e0c41c 4170
7888a6ae
GD
4171 else
4172 Loc := Sloc (Last_Stm);
4173 end if;
12e0c41c 4174
5334d18f
BD
4175 declare
4176 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
4177
4178 begin
4a3b249c
RD
4179 -- Append return statement, and set analyzed manually. We can't
4180 -- call Analyze on this return since the scope is wrong.
5334d18f
BD
4181
4182 -- Note: it almost works to push the scope and then do the
4a3b249c 4183 -- Analyze call, but something goes wrong in some weird cases
5334d18f
BD
4184 -- and it is not worth worrying about ???
4185
4186 Append_To (S, Rtn);
4187 Set_Analyzed (Rtn);
4188
4189 -- Call _Postconditions procedure if appropriate. We need to
4190 -- do this explicitly because we did not analyze the generated
4191 -- return statement above, so the call did not get inserted.
4192
4193 if Ekind (Spec_Id) = E_Procedure
4194 and then Has_Postconditions (Spec_Id)
4195 then
4196 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
4197 Insert_Action (Rtn,
4198 Make_Procedure_Call_Statement (Loc,
4199 Name =>
4200 New_Reference_To (Postcondition_Proc (Spec_Id), Loc)));
4201 end if;
4202 end;
12e0c41c 4203 end if;
7888a6ae 4204 end Add_Return;
12e0c41c 4205
70482933
RK
4206 -- Start of processing for Expand_N_Subprogram_Body
4207
4208 begin
4a3b249c
RD
4209 -- Set L to either the list of declarations if present, or to the list
4210 -- of statements if no declarations are present. This is used to insert
4211 -- new stuff at the start.
70482933
RK
4212
4213 if Is_Non_Empty_List (Declarations (N)) then
4214 L := Declarations (N);
4215 else
7888a6ae
GD
4216 L := Statements (H);
4217 end if;
4218
4219 -- If local-exception-to-goto optimization active, insert dummy push
4220 -- statements at start, and dummy pop statements at end.
4221
4222 if (Debug_Flag_Dot_G
4223 or else Restriction_Active (No_Exception_Propagation))
4224 and then Is_Non_Empty_List (L)
4225 then
4226 declare
4227 FS : constant Node_Id := First (L);
4228 FL : constant Source_Ptr := Sloc (FS);
4229 LS : Node_Id;
4230 LL : Source_Ptr;
4231
4232 begin
4233 -- LS points to either last statement, if statements are present
4234 -- or to the last declaration if there are no statements present.
4235 -- It is the node after which the pop's are generated.
4236
4237 if Is_Non_Empty_List (Statements (H)) then
4238 LS := Last (Statements (H));
4239 else
4240 LS := Last (L);
4241 end if;
4242
4243 LL := Sloc (LS);
4244
4245 Insert_List_Before_And_Analyze (FS, New_List (
4246 Make_Push_Constraint_Error_Label (FL),
4247 Make_Push_Program_Error_Label (FL),
4248 Make_Push_Storage_Error_Label (FL)));
4249
4250 Insert_List_After_And_Analyze (LS, New_List (
4251 Make_Pop_Constraint_Error_Label (LL),
4252 Make_Pop_Program_Error_Label (LL),
4253 Make_Pop_Storage_Error_Label (LL)));
4254 end;
70482933
RK
4255 end if;
4256
70482933
RK
4257 -- Find entity for subprogram
4258
c9a4817d
RD
4259 Body_Id := Defining_Entity (N);
4260
70482933
RK
4261 if Present (Corresponding_Spec (N)) then
4262 Spec_Id := Corresponding_Spec (N);
4263 else
c9a4817d
RD
4264 Spec_Id := Body_Id;
4265 end if;
4266
7888a6ae
GD
4267 -- Need poll on entry to subprogram if polling enabled. We only do this
4268 -- for non-empty subprograms, since it does not seem necessary to poll
4a3b249c 4269 -- for a dummy null subprogram.
c885d7a1
AC
4270
4271 if Is_Non_Empty_List (L) then
4a3b249c
RD
4272
4273 -- Do not add a polling call if the subprogram is to be inlined by
4274 -- the back-end, to avoid repeated calls with multiple inlinings.
4275
c885d7a1
AC
4276 if Is_Inlined (Spec_Id)
4277 and then Front_End_Inlining
4278 and then Optimization_Level > 1
4279 then
4280 null;
4281 else
4282 Generate_Poll_Call (First (L));
4283 end if;
4284 end if;
4285
4a3b249c
RD
4286 -- If this is a Pure function which has any parameters whose root type
4287 -- is System.Address, reset the Pure indication, since it will likely
4288 -- cause incorrect code to be generated as the parameter is probably
4289 -- a pointer, and the fact that the same pointer is passed does not mean
4290 -- that the same value is being referenced.
91b1417d
AC
4291
4292 -- Note that if the programmer gave an explicit Pure_Function pragma,
4293 -- then we believe the programmer, and leave the subprogram Pure.
4294
4a3b249c
RD
4295 -- This code should probably be at the freeze point, so that it happens
4296 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
4297 -- semantic tree has Is_Pure set properly ???
c9a4817d
RD
4298
4299 if Is_Pure (Spec_Id)
4300 and then Is_Subprogram (Spec_Id)
4301 and then not Has_Pragma_Pure_Function (Spec_Id)
4302 then
4303 declare
2f1b20a9 4304 F : Entity_Id;
c9a4817d
RD
4305
4306 begin
2f1b20a9 4307 F := First_Formal (Spec_Id);
c9a4817d 4308 while Present (F) loop
8a36a0cc 4309 if Is_Descendent_Of_Address (Etype (F)) then
c9a4817d
RD
4310 Set_Is_Pure (Spec_Id, False);
4311
4312 if Spec_Id /= Body_Id then
4313 Set_Is_Pure (Body_Id, False);
4314 end if;
4315
4316 exit;
4317 end if;
4318
4319 Next_Formal (F);
4320 end loop;
4321 end;
70482933
RK
4322 end if;
4323
4324 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
4325
4326 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
4327 declare
2f1b20a9 4328 F : Entity_Id;
70482933
RK
4329
4330 begin
70482933
RK
4331 -- Loop through formals
4332
2f1b20a9 4333 F := First_Formal (Spec_Id);
70482933
RK
4334 while Present (F) loop
4335 if Is_Scalar_Type (Etype (F))
4336 and then Ekind (F) = E_Out_Parameter
4337 then
70f91180
RD
4338 Check_Restriction (No_Default_Initialization, F);
4339
02822a92
RD
4340 -- Insert the initialization. We turn off validity checks
4341 -- for this assignment, since we do not want any check on
4342 -- the initial value itself (which may well be invalid).
4343
70482933
RK
4344 Insert_Before_And_Analyze (First (L),
4345 Make_Assignment_Statement (Loc,
02822a92 4346 Name => New_Occurrence_Of (F, Loc),
70f91180 4347 Expression => Get_Simple_Init_Val (Etype (F), N)),
02822a92 4348 Suppress => Validity_Check);
70482933
RK
4349 end if;
4350
4351 Next_Formal (F);
4352 end loop;
70482933
RK
4353 end;
4354 end if;
4355
4356 -- Clear out statement list for stubbed procedure
4357
4358 if Present (Corresponding_Spec (N)) then
4359 Set_Elaboration_Flag (N, Spec_Id);
4360
4361 if Convention (Spec_Id) = Convention_Stubbed
4362 or else Is_Eliminated (Spec_Id)
4363 then
4364 Set_Declarations (N, Empty_List);
4365 Set_Handled_Statement_Sequence (N,
4366 Make_Handled_Sequence_Of_Statements (Loc,
4367 Statements => New_List (
4368 Make_Null_Statement (Loc))));
4369 return;
4370 end if;
4371 end if;
4372
70f91180
RD
4373 -- Create a set of discriminals for the next protected subprogram body
4374
4375 if Is_List_Member (N)
4376 and then Present (Parent (List_Containing (N)))
4377 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
4378 and then Present (Next_Protected_Operation (N))
4379 then
4380 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
4381 end if;
4382
4a3b249c
RD
4383 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
4384 -- subprograms with no specs are not frozen.
70482933
RK
4385
4386 declare
4387 Typ : constant Entity_Id := Etype (Spec_Id);
4388 Utyp : constant Entity_Id := Underlying_Type (Typ);
4389
4390 begin
4391 if not Acts_As_Spec (N)
4392 and then Nkind (Parent (Parent (Spec_Id))) /=
4393 N_Subprogram_Body_Stub
4394 then
4395 null;
4396
02822a92 4397 elsif Is_Inherently_Limited_Type (Typ) then
70482933
RK
4398 Set_Returns_By_Ref (Spec_Id);
4399
048e5cef 4400 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
70482933
RK
4401 Set_Returns_By_Ref (Spec_Id);
4402 end if;
4403 end;
4404
4a3b249c
RD
4405 -- For a procedure, we add a return for all possible syntactic ends of
4406 -- the subprogram.
70482933 4407
b29def53 4408 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
70482933
RK
4409 Add_Return (Statements (H));
4410
4411 if Present (Exception_Handlers (H)) then
4412 Except_H := First_Non_Pragma (Exception_Handlers (H));
70482933
RK
4413 while Present (Except_H) loop
4414 Add_Return (Statements (Except_H));
4415 Next_Non_Pragma (Except_H);
4416 end loop;
4417 end if;
4418
98f01d53
AC
4419 -- For a function, we must deal with the case where there is at least
4420 -- one missing return. What we do is to wrap the entire body of the
4421 -- function in a block:
70482933
RK
4422
4423 -- begin
4424 -- ...
4425 -- end;
4426
4427 -- becomes
4428
4429 -- begin
4430 -- begin
4431 -- ...
4432 -- end;
4433
4434 -- raise Program_Error;
4435 -- end;
4436
4a3b249c
RD
4437 -- This approach is necessary because the raise must be signalled to the
4438 -- caller, not handled by any local handler (RM 6.4(11)).
70482933 4439
4a3b249c
RD
4440 -- Note: we do not need to analyze the constructed sequence here, since
4441 -- it has no handler, and an attempt to analyze the handled statement
4442 -- sequence twice is risky in various ways (e.g. the issue of expanding
4443 -- cleanup actions twice).
70482933
RK
4444
4445 elsif Has_Missing_Return (Spec_Id) then
4446 declare
4447 Hloc : constant Source_Ptr := Sloc (H);
4448 Blok : constant Node_Id :=
4449 Make_Block_Statement (Hloc,
4450 Handled_Statement_Sequence => H);
4451 Rais : constant Node_Id :=
07fc65c4
GB
4452 Make_Raise_Program_Error (Hloc,
4453 Reason => PE_Missing_Return);
70482933
RK
4454
4455 begin
4456 Set_Handled_Statement_Sequence (N,
4457 Make_Handled_Sequence_Of_Statements (Hloc,
4458 Statements => New_List (Blok, Rais)));
4459
7888a6ae 4460 Push_Scope (Spec_Id);
70482933
RK
4461 Analyze (Blok);
4462 Analyze (Rais);
4463 Pop_Scope;
4464 end;
4465 end if;
4466
70482933
RK
4467 -- If subprogram contains a parameterless recursive call, then we may
4468 -- have an infinite recursion, so see if we can generate code to check
4469 -- for this possibility if storage checks are not suppressed.
4470
4471 if Ekind (Spec_Id) = E_Procedure
4472 and then Has_Recursive_Call (Spec_Id)
4473 and then not Storage_Checks_Suppressed (Spec_Id)
4474 then
4475 Detect_Infinite_Recursion (N, Spec_Id);
4476 end if;
4477
70482933
RK
4478 -- Set to encode entity names in package body before gigi is called
4479
4480 Qualify_Entity_Names (N);
4481 end Expand_N_Subprogram_Body;
4482
4483 -----------------------------------
4484 -- Expand_N_Subprogram_Body_Stub --
4485 -----------------------------------
4486
4487 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
4488 begin
4489 if Present (Corresponding_Body (N)) then
4490 Expand_N_Subprogram_Body (
4491 Unit_Declaration_Node (Corresponding_Body (N)));
4492 end if;
70482933
RK
4493 end Expand_N_Subprogram_Body_Stub;
4494
4495 -------------------------------------
4496 -- Expand_N_Subprogram_Declaration --
4497 -------------------------------------
4498
70482933
RK
4499 -- If the declaration appears within a protected body, it is a private
4500 -- operation of the protected type. We must create the corresponding
4501 -- protected subprogram an associated formals. For a normal protected
4502 -- operation, this is done when expanding the protected type declaration.
4503
758c442c
GD
4504 -- If the declaration is for a null procedure, emit null body
4505
70482933 4506 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
fbf5a39b
AC
4507 Loc : constant Source_Ptr := Sloc (N);
4508 Subp : constant Entity_Id := Defining_Entity (N);
4509 Scop : constant Entity_Id := Scope (Subp);
4510 Prot_Decl : Node_Id;
4511 Prot_Bod : Node_Id;
4512 Prot_Id : Entity_Id;
70482933
RK
4513
4514 begin
2f1b20a9
ES
4515 -- Deal with case of protected subprogram. Do not generate protected
4516 -- operation if operation is flagged as eliminated.
70482933
RK
4517
4518 if Is_List_Member (N)
4519 and then Present (Parent (List_Containing (N)))
4520 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
4521 and then Is_Protected_Type (Scop)
4522 then
6871ba5f
AC
4523 if No (Protected_Body_Subprogram (Subp))
4524 and then not Is_Eliminated (Subp)
4525 then
fbf5a39b 4526 Prot_Decl :=
70482933
RK
4527 Make_Subprogram_Declaration (Loc,
4528 Specification =>
4529 Build_Protected_Sub_Specification
2f1b20a9 4530 (N, Scop, Unprotected_Mode));
70482933
RK
4531
4532 -- The protected subprogram is declared outside of the protected
4533 -- body. Given that the body has frozen all entities so far, we
fbf5a39b 4534 -- analyze the subprogram and perform freezing actions explicitly.
19590d70
GD
4535 -- including the generation of an explicit freeze node, to ensure
4536 -- that gigi has the proper order of elaboration.
fbf5a39b
AC
4537 -- If the body is a subunit, the insertion point is before the
4538 -- stub in the parent.
70482933
RK
4539
4540 Prot_Bod := Parent (List_Containing (N));
4541
4542 if Nkind (Parent (Prot_Bod)) = N_Subunit then
4543 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
4544 end if;
4545
fbf5a39b
AC
4546 Insert_Before (Prot_Bod, Prot_Decl);
4547 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
19590d70 4548 Set_Has_Delayed_Freeze (Prot_Id);
70482933 4549
7888a6ae 4550 Push_Scope (Scope (Scop));
fbf5a39b 4551 Analyze (Prot_Decl);
19590d70 4552 Insert_Actions (N, Freeze_Entity (Prot_Id, Loc));
fbf5a39b 4553 Set_Protected_Body_Subprogram (Subp, Prot_Id);
47bfea3a
AC
4554
4555 -- Create protected operation as well. Even though the operation
4556 -- is only accessible within the body, it is possible to make it
4557 -- available outside of the protected object by using 'Access to
3d923671 4558 -- provide a callback, so build protected version in all cases.
47bfea3a
AC
4559
4560 Prot_Decl :=
3d923671
AC
4561 Make_Subprogram_Declaration (Loc,
4562 Specification =>
4563 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
47bfea3a
AC
4564 Insert_Before (Prot_Bod, Prot_Decl);
4565 Analyze (Prot_Decl);
4566
70482933
RK
4567 Pop_Scope;
4568 end if;
758c442c 4569
e1f3cb58
AC
4570 -- Ada 2005 (AI-348): Generate body for a null procedure.
4571 -- In most cases this is superfluous because calls to it
4572 -- will be automatically inlined, but we definitely need
4573 -- the body if preconditions for the procedure are present.
02822a92 4574
758c442c
GD
4575 elsif Nkind (Specification (N)) = N_Procedure_Specification
4576 and then Null_Present (Specification (N))
4577 then
4578 declare
e1f3cb58 4579 Bod : constant Node_Id := Body_To_Inline (N);
d6533e74 4580
758c442c 4581 begin
e1f3cb58
AC
4582 Set_Has_Completion (Subp, False);
4583 Append_Freeze_Action (Subp, Bod);
c73ae90f 4584
e1f3cb58
AC
4585 -- The body now contains raise statements, so calls to it will
4586 -- not be inlined.
c73ae90f 4587
e1f3cb58 4588 Set_Is_Inlined (Subp, False);
758c442c 4589 end;
70482933
RK
4590 end if;
4591 end Expand_N_Subprogram_Declaration;
4592
4593 ---------------------------------------
4594 -- Expand_Protected_Object_Reference --
4595 ---------------------------------------
4596
4597 function Expand_Protected_Object_Reference
4598 (N : Node_Id;
02822a92 4599 Scop : Entity_Id) return Node_Id
70482933
RK
4600 is
4601 Loc : constant Source_Ptr := Sloc (N);
4602 Corr : Entity_Id;
4603 Rec : Node_Id;
4604 Param : Entity_Id;
4605 Proc : Entity_Id;
4606
4607 begin
02822a92
RD
4608 Rec :=
4609 Make_Identifier (Loc,
4610 Chars => Name_uObject);
70482933
RK
4611 Set_Etype (Rec, Corresponding_Record_Type (Scop));
4612
2f1b20a9
ES
4613 -- Find enclosing protected operation, and retrieve its first parameter,
4614 -- which denotes the enclosing protected object. If the enclosing
4615 -- operation is an entry, we are immediately within the protected body,
4616 -- and we can retrieve the object from the service entries procedure. A
16b05213 4617 -- barrier function has the same signature as an entry. A barrier
2f1b20a9
ES
4618 -- function is compiled within the protected object, but unlike
4619 -- protected operations its never needs locks, so that its protected
4620 -- body subprogram points to itself.
70482933
RK
4621
4622 Proc := Current_Scope;
70482933
RK
4623 while Present (Proc)
4624 and then Scope (Proc) /= Scop
4625 loop
4626 Proc := Scope (Proc);
4627 end loop;
4628
4629 Corr := Protected_Body_Subprogram (Proc);
4630
4631 if No (Corr) then
4632
4633 -- Previous error left expansion incomplete.
4634 -- Nothing to do on this call.
4635
4636 return Empty;
4637 end if;
4638
4639 Param :=
4640 Defining_Identifier
4641 (First (Parameter_Specifications (Parent (Corr))));
4642
4643 if Is_Subprogram (Proc)
4644 and then Proc /= Corr
4645 then
98f01d53 4646 -- Protected function or procedure
70482933
RK
4647
4648 Set_Entity (Rec, Param);
4649
2f1b20a9
ES
4650 -- Rec is a reference to an entity which will not be in scope when
4651 -- the call is reanalyzed, and needs no further analysis.
70482933
RK
4652
4653 Set_Analyzed (Rec);
4654
4655 else
2f1b20a9
ES
4656 -- Entry or barrier function for entry body. The first parameter of
4657 -- the entry body procedure is pointer to the object. We create a
4658 -- local variable of the proper type, duplicating what is done to
4659 -- define _object later on.
70482933
RK
4660
4661 declare
c12beea0
RD
4662 Decls : List_Id;
4663 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
fbf5a39b 4664
70482933
RK
4665 begin
4666 Decls := New_List (
4667 Make_Full_Type_Declaration (Loc,
4668 Defining_Identifier => Obj_Ptr,
4669 Type_Definition =>
4670 Make_Access_To_Object_Definition (Loc,
4671 Subtype_Indication =>
4672 New_Reference_To
c12beea0 4673 (Corresponding_Record_Type (Scop), Loc))));
70482933
RK
4674
4675 Insert_Actions (N, Decls);
4676 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
4677
4678 Rec :=
4679 Make_Explicit_Dereference (Loc,
4680 Unchecked_Convert_To (Obj_Ptr,
4681 New_Occurrence_Of (Param, Loc)));
4682
2f1b20a9 4683 -- Analyze new actual. Other actuals in calls are already analyzed
7888a6ae 4684 -- and the list of actuals is not reanalyzed after rewriting.
70482933
RK
4685
4686 Set_Parent (Rec, N);
4687 Analyze (Rec);
4688 end;
4689 end if;
4690
4691 return Rec;
4692 end Expand_Protected_Object_Reference;
4693
4694 --------------------------------------
4695 -- Expand_Protected_Subprogram_Call --
4696 --------------------------------------
4697
4698 procedure Expand_Protected_Subprogram_Call
4699 (N : Node_Id;
4700 Subp : Entity_Id;
4701 Scop : Entity_Id)
4702 is
4703 Rec : Node_Id;
4704
4705 begin
4706 -- If the protected object is not an enclosing scope, this is
4707 -- an inter-object function call. Inter-object procedure
4708 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
4709 -- The call is intra-object only if the subprogram being
4710 -- called is in the protected body being compiled, and if the
4711 -- protected object in the call is statically the enclosing type.
4712 -- The object may be an component of some other data structure,
4713 -- in which case this must be handled as an inter-object call.
4714
4715 if not In_Open_Scopes (Scop)
4716 or else not Is_Entity_Name (Name (N))
4717 then
4718 if Nkind (Name (N)) = N_Selected_Component then
4719 Rec := Prefix (Name (N));
4720
4721 else
4722 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
4723 Rec := Prefix (Prefix (Name (N)));
4724 end if;
4725
4726 Build_Protected_Subprogram_Call (N,
4727 Name => New_Occurrence_Of (Subp, Sloc (N)),
4728 Rec => Convert_Concurrent (Rec, Etype (Rec)),
4729 External => True);
4730
4731 else
4732 Rec := Expand_Protected_Object_Reference (N, Scop);
4733
4734 if No (Rec) then
4735 return;
4736 end if;
4737
4738 Build_Protected_Subprogram_Call (N,
4739 Name => Name (N),
4740 Rec => Rec,
4741 External => False);
4742
4743 end if;
4744
70482933
RK
4745 -- If it is a function call it can appear in elaboration code and
4746 -- the called entity must be frozen here.
4747
4748 if Ekind (Subp) = E_Function then
4749 Freeze_Expression (Name (N));
4750 end if;
811c6a85
AC
4751
4752 -- Analyze and resolve the new call. The actuals have already been
b0159fbe 4753 -- resolved, but expansion of a function call will add extra actuals
811c6a85
AC
4754 -- if needed. Analysis of a procedure call already includes resolution.
4755
4756 Analyze (N);
4757
4758 if Ekind (Subp) = E_Function then
4759 Resolve (N, Etype (Subp));
4760 end if;
70482933
RK
4761 end Expand_Protected_Subprogram_Call;
4762
02822a92
RD
4763 --------------------------------
4764 -- Is_Build_In_Place_Function --
4765 --------------------------------
70482933 4766
02822a92
RD
4767 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
4768 begin
4769 -- For now we test whether E denotes a function or access-to-function
f937473f
RD
4770 -- type whose result subtype is inherently limited. Later this test may
4771 -- be revised to allow composite nonlimited types. Functions with a
4772 -- foreign convention or whose result type has a foreign convention
02822a92
RD
4773 -- never qualify.
4774
b29def53 4775 if Ekind_In (E, E_Function, E_Generic_Function)
02822a92
RD
4776 or else (Ekind (E) = E_Subprogram_Type
4777 and then Etype (E) /= Standard_Void_Type)
4778 then
f937473f
RD
4779 -- Note: If you have Convention (C) on an inherently limited type,
4780 -- you're on your own. That is, the C code will have to be carefully
4781 -- written to know about the Ada conventions.
4782
02822a92
RD
4783 if Has_Foreign_Convention (E)
4784 or else Has_Foreign_Convention (Etype (E))
3ca505dc 4785 then
02822a92 4786 return False;
c8ef728f 4787
2a31c32b
AC
4788 -- In Ada 2005 all functions with an inherently limited return type
4789 -- must be handled using a build-in-place profile, including the case
4790 -- of a function with a limited interface result, where the function
4791 -- may return objects of nonlimited descendants.
7888a6ae 4792
02822a92
RD
4793 else
4794 return Is_Inherently_Limited_Type (Etype (E))
f937473f
RD
4795 and then Ada_Version >= Ada_05
4796 and then not Debug_Flag_Dot_L;
c8ef728f
ES
4797 end if;
4798
02822a92
RD
4799 else
4800 return False;
4801 end if;
4802 end Is_Build_In_Place_Function;
f4d379b8 4803
02822a92
RD
4804 -------------------------------------
4805 -- Is_Build_In_Place_Function_Call --
4806 -------------------------------------
f4d379b8 4807
02822a92
RD
4808 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
4809 Exp_Node : Node_Id := N;
4810 Function_Id : Entity_Id;
f4d379b8 4811
02822a92 4812 begin
19590d70
GD
4813 -- Step past qualification or unchecked conversion (the latter can occur
4814 -- in cases of calls to 'Input).
4815
ac4d6407
RD
4816 if Nkind_In
4817 (Exp_Node, N_Qualified_Expression, N_Unchecked_Type_Conversion)
19590d70 4818 then
02822a92
RD
4819 Exp_Node := Expression (N);
4820 end if;
758c442c 4821
02822a92
RD
4822 if Nkind (Exp_Node) /= N_Function_Call then
4823 return False;
3ca505dc 4824
02822a92
RD
4825 else
4826 if Is_Entity_Name (Name (Exp_Node)) then
4827 Function_Id := Entity (Name (Exp_Node));
758c442c 4828
02822a92
RD
4829 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
4830 Function_Id := Etype (Name (Exp_Node));
4831 end if;
758c442c 4832
02822a92
RD
4833 return Is_Build_In_Place_Function (Function_Id);
4834 end if;
4835 end Is_Build_In_Place_Function_Call;
758c442c 4836
02822a92
RD
4837 -----------------------
4838 -- Freeze_Subprogram --
4839 -----------------------
758c442c 4840
02822a92
RD
4841 procedure Freeze_Subprogram (N : Node_Id) is
4842 Loc : constant Source_Ptr := Sloc (N);
3ca505dc 4843
02822a92
RD
4844 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
4845 -- (Ada 2005): Register a predefined primitive in all the secondary
4846 -- dispatch tables of its primitive type.
3ca505dc 4847
f4d379b8
HK
4848 ----------------------------------
4849 -- Register_Predefined_DT_Entry --
4850 ----------------------------------
4851
4852 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
4853 Iface_DT_Ptr : Elmt_Id;
02822a92 4854 Tagged_Typ : Entity_Id;
f4d379b8 4855 Thunk_Id : Entity_Id;
7888a6ae 4856 Thunk_Code : Node_Id;
f4d379b8
HK
4857
4858 begin
02822a92 4859 Tagged_Typ := Find_Dispatching_Type (Prim);
f4d379b8 4860
02822a92 4861 if No (Access_Disp_Table (Tagged_Typ))
ce2b6ba5 4862 or else not Has_Interfaces (Tagged_Typ)
c8ef728f 4863 or else not RTE_Available (RE_Interface_Tag)
f937473f 4864 or else Restriction_Active (No_Dispatching_Calls)
f4d379b8
HK
4865 then
4866 return;
4867 end if;
4868
1923d2d6
JM
4869 -- Skip the first two access-to-dispatch-table pointers since they
4870 -- leads to the primary dispatch table (predefined DT and user
4871 -- defined DT). We are only concerned with the secondary dispatch
4872 -- table pointers. Note that the access-to- dispatch-table pointer
4873 -- corresponds to the first implemented interface retrieved below.
f4d379b8 4874
02822a92 4875 Iface_DT_Ptr :=
1923d2d6 4876 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
f937473f 4877
7888a6ae
GD
4878 while Present (Iface_DT_Ptr)
4879 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
4880 loop
ac4d6407 4881 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
d766cee3 4882 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7888a6ae
GD
4883
4884 if Present (Thunk_Code) then
ac4d6407 4885 Insert_Actions_After (N, New_List (
7888a6ae
GD
4886 Thunk_Code,
4887
4888 Build_Set_Predefined_Prim_Op_Address (Loc,
1923d2d6
JM
4889 Tag_Node =>
4890 New_Reference_To (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7888a6ae
GD
4891 Position => DT_Position (Prim),
4892 Address_Node =>
70f91180 4893 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
1923d2d6
JM
4894 Make_Attribute_Reference (Loc,
4895 Prefix => New_Reference_To (Thunk_Id, Loc),
4896 Attribute_Name => Name_Unrestricted_Access))),
ac4d6407
RD
4897
4898 Build_Set_Predefined_Prim_Op_Address (Loc,
1923d2d6
JM
4899 Tag_Node =>
4900 New_Reference_To
4901 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
4902 Loc),
ac4d6407
RD
4903 Position => DT_Position (Prim),
4904 Address_Node =>
70f91180 4905 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
1923d2d6
JM
4906 Make_Attribute_Reference (Loc,
4907 Prefix => New_Reference_To (Prim, Loc),
4908 Attribute_Name => Name_Unrestricted_Access)))));
7888a6ae 4909 end if;
f4d379b8 4910
1923d2d6
JM
4911 -- Skip the tag of the predefined primitives dispatch table
4912
4913 Next_Elmt (Iface_DT_Ptr);
4914 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
4915
4916 -- Skip the tag of the no-thunks dispatch table
4917
4918 Next_Elmt (Iface_DT_Ptr);
4919 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
4920
4921 -- Skip the tag of the predefined primitives no-thunks dispatch
4922 -- table
4923
ac4d6407
RD
4924 Next_Elmt (Iface_DT_Ptr);
4925 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
4926
f4d379b8 4927 Next_Elmt (Iface_DT_Ptr);
f4d379b8
HK
4928 end loop;
4929 end Register_Predefined_DT_Entry;
4930
7888a6ae 4931 -- Local variables
3ca505dc 4932
7888a6ae 4933 Subp : constant Entity_Id := Entity (N);
3ca505dc 4934
ac4d6407
RD
4935 -- Start of processing for Freeze_Subprogram
4936
7888a6ae 4937 begin
d766cee3
RD
4938 -- We suppress the initialization of the dispatch table entry when
4939 -- VM_Target because the dispatching mechanism is handled internally
4940 -- by the VM.
4941
4942 if Is_Dispatching_Operation (Subp)
4943 and then not Is_Abstract_Subprogram (Subp)
4944 and then Present (DTC_Entity (Subp))
4945 and then Present (Scope (DTC_Entity (Subp)))
1f110335 4946 and then Tagged_Type_Expansion
d766cee3
RD
4947 and then not Restriction_Active (No_Dispatching_Calls)
4948 and then RTE_Available (RE_Tag)
4949 then
7888a6ae 4950 declare
d766cee3 4951 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
c8ef728f 4952
7888a6ae 4953 begin
8fc789c8 4954 -- Handle private overridden primitives
c8ef728f 4955
d766cee3
RD
4956 if not Is_CPP_Class (Typ) then
4957 Check_Overriding_Operation (Subp);
7888a6ae 4958 end if;
c8ef728f 4959
d766cee3
RD
4960 -- We assume that imported CPP primitives correspond with objects
4961 -- whose constructor is in the CPP side; therefore we don't need
4962 -- to generate code to register them in the dispatch table.
c8ef728f 4963
d766cee3
RD
4964 if Is_CPP_Class (Typ) then
4965 null;
3ca505dc 4966
d766cee3
RD
4967 -- Handle CPP primitives found in derivations of CPP_Class types.
4968 -- These primitives must have been inherited from some parent, and
4969 -- there is no need to register them in the dispatch table because
4970 -- Build_Inherit_Prims takes care of the initialization of these
4971 -- slots.
3ca505dc 4972
d766cee3
RD
4973 elsif Is_Imported (Subp)
4974 and then (Convention (Subp) = Convention_CPP
4975 or else Convention (Subp) = Convention_C)
4976 then
4977 null;
4978
4979 -- Generate code to register the primitive in non statically
4980 -- allocated dispatch tables
4981
4982 elsif not Static_Dispatch_Tables
4983 or else not
4984 Is_Library_Level_Tagged_Type (Scope (DTC_Entity (Subp)))
4985 then
4986 -- When a primitive is frozen, enter its name in its dispatch
4987 -- table slot.
f4d379b8 4988
d766cee3 4989 if not Is_Interface (Typ)
ce2b6ba5 4990 or else Present (Interface_Alias (Subp))
d766cee3
RD
4991 then
4992 if Is_Predefined_Dispatching_Operation (Subp) then
4993 Register_Predefined_DT_Entry (Subp);
7888a6ae 4994 end if;
d766cee3 4995
991395ab
AC
4996 Insert_Actions_After (N,
4997 Register_Primitive (Loc, Prim => Subp));
7888a6ae
GD
4998 end if;
4999 end if;
5000 end;
70482933
RK
5001 end if;
5002
7888a6ae
GD
5003 -- Mark functions that return by reference. Note that it cannot be part
5004 -- of the normal semantic analysis of the spec since the underlying
5005 -- returned type may not be known yet (for private types).
70482933 5006
d766cee3
RD
5007 declare
5008 Typ : constant Entity_Id := Etype (Subp);
5009 Utyp : constant Entity_Id := Underlying_Type (Typ);
5010 begin
5011 if Is_Inherently_Limited_Type (Typ) then
5012 Set_Returns_By_Ref (Subp);
048e5cef 5013 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
d766cee3
RD
5014 Set_Returns_By_Ref (Subp);
5015 end if;
5016 end;
70482933
RK
5017 end Freeze_Subprogram;
5018
8dbf3473
AC
5019 -----------------------
5020 -- Is_Null_Procedure --
5021 -----------------------
5022
5023 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
5024 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
5025
5026 begin
5027 if Ekind (Subp) /= E_Procedure then
5028 return False;
5029
5030 -- Check if this is a declared null procedure
5031
5032 elsif Nkind (Decl) = N_Subprogram_Declaration then
e1f3cb58
AC
5033 if not Null_Present (Specification (Decl)) then
5034 return False;
8dbf3473
AC
5035
5036 elsif No (Body_To_Inline (Decl)) then
5037 return False;
5038
5039 -- Check if the body contains only a null statement, followed by
5040 -- the return statement added during expansion.
5041
5042 else
5043 declare
5044 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
5045
5046 Stat : Node_Id;
5047 Stat2 : Node_Id;
5048
5049 begin
5050 if Nkind (Orig_Bod) /= N_Subprogram_Body then
5051 return False;
5052 else
327503f1
JM
5053 -- We must skip SCIL nodes because they are currently
5054 -- implemented as special N_Null_Statement nodes.
5055
8dbf3473 5056 Stat :=
327503f1 5057 First_Non_SCIL_Node
8dbf3473 5058 (Statements (Handled_Statement_Sequence (Orig_Bod)));
327503f1 5059 Stat2 := Next_Non_SCIL_Node (Stat);
8dbf3473
AC
5060
5061 return
e1f3cb58
AC
5062 Is_Empty_List (Declarations (Orig_Bod))
5063 and then Nkind (Stat) = N_Null_Statement
5064 and then
8dbf3473
AC
5065 (No (Stat2)
5066 or else
5067 (Nkind (Stat2) = N_Simple_Return_Statement
5068 and then No (Next (Stat2))));
5069 end if;
5070 end;
5071 end if;
5072
5073 else
5074 return False;
5075 end if;
5076 end Is_Null_Procedure;
5077
02822a92
RD
5078 -------------------------------------------
5079 -- Make_Build_In_Place_Call_In_Allocator --
5080 -------------------------------------------
5081
5082 procedure Make_Build_In_Place_Call_In_Allocator
5083 (Allocator : Node_Id;
5084 Function_Call : Node_Id)
5085 is
5086 Loc : Source_Ptr;
5087 Func_Call : Node_Id := Function_Call;
5088 Function_Id : Entity_Id;
5089 Result_Subt : Entity_Id;
5090 Acc_Type : constant Entity_Id := Etype (Allocator);
5091 New_Allocator : Node_Id;
5092 Return_Obj_Access : Entity_Id;
5093
5094 begin
19590d70
GD
5095 -- Step past qualification or unchecked conversion (the latter can occur
5096 -- in cases of calls to 'Input).
5097
ac4d6407
RD
5098 if Nkind_In (Func_Call,
5099 N_Qualified_Expression,
5100 N_Unchecked_Type_Conversion)
19590d70 5101 then
02822a92
RD
5102 Func_Call := Expression (Func_Call);
5103 end if;
5104
fdce4bb7
JM
5105 -- If the call has already been processed to add build-in-place actuals
5106 -- then return. This should not normally occur in an allocator context,
5107 -- but we add the protection as a defensive measure.
5108
5109 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5110 return;
5111 end if;
5112
5113 -- Mark the call as processed as a build-in-place call
5114
5115 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5116
02822a92
RD
5117 Loc := Sloc (Function_Call);
5118
5119 if Is_Entity_Name (Name (Func_Call)) then
5120 Function_Id := Entity (Name (Func_Call));
5121
5122 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5123 Function_Id := Etype (Name (Func_Call));
5124
5125 else
5126 raise Program_Error;
5127 end if;
5128
5129 Result_Subt := Etype (Function_Id);
5130
f937473f
RD
5131 -- When the result subtype is constrained, the return object must be
5132 -- allocated on the caller side, and access to it is passed to the
5133 -- function.
02822a92 5134
7888a6ae
GD
5135 -- Here and in related routines, we must examine the full view of the
5136 -- type, because the view at the point of call may differ from that
5137 -- that in the function body, and the expansion mechanism depends on
5138 -- the characteristics of the full view.
5139
5140 if Is_Constrained (Underlying_Type (Result_Subt)) then
02822a92 5141
f937473f
RD
5142 -- Replace the initialized allocator of form "new T'(Func (...))"
5143 -- with an uninitialized allocator of form "new T", where T is the
5144 -- result subtype of the called function. The call to the function
5145 -- is handled separately further below.
02822a92 5146
f937473f 5147 New_Allocator :=
fad0600d
AC
5148 Make_Allocator (Loc,
5149 Expression => New_Reference_To (Result_Subt, Loc));
5150 Set_No_Initialization (New_Allocator);
5151
5152 -- Copy attributes to new allocator. Note that the new allocator
5153 -- logically comes from source if the original one did, so copy the
5154 -- relevant flag. This ensures proper treatment of the restriction
5155 -- No_Implicit_Heap_Allocations in this case.
02822a92 5156
fad0600d 5157 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
f937473f 5158 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
fad0600d 5159 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
02822a92 5160
f937473f 5161 Rewrite (Allocator, New_Allocator);
02822a92 5162
f937473f 5163 -- Create a new access object and initialize it to the result of the
b0b7b57d 5164 -- new uninitialized allocator. Note: we do not use Allocator as the
f104fca1
AC
5165 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
5166 -- as this would create a sort of infinite "recursion".
02822a92 5167
f104fca1 5168 Return_Obj_Access := Make_Temporary (Loc, 'R');
f937473f
RD
5169 Set_Etype (Return_Obj_Access, Acc_Type);
5170
5171 Insert_Action (Allocator,
5172 Make_Object_Declaration (Loc,
5173 Defining_Identifier => Return_Obj_Access,
5174 Object_Definition => New_Reference_To (Acc_Type, Loc),
5175 Expression => Relocate_Node (Allocator)));
5176
7888a6ae
GD
5177 -- When the function has a controlling result, an allocation-form
5178 -- parameter must be passed indicating that the caller is allocating
5179 -- the result object. This is needed because such a function can be
5180 -- called as a dispatching operation and must be treated similarly
5181 -- to functions with unconstrained result subtypes.
5182
5183 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5184 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5185
5186 Add_Final_List_Actual_To_Build_In_Place_Call
5187 (Func_Call, Function_Id, Acc_Type);
5188
5189 Add_Task_Actuals_To_Build_In_Place_Call
5190 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
5191
f937473f
RD
5192 -- Add an implicit actual to the function call that provides access
5193 -- to the allocated object. An unchecked conversion to the (specific)
5194 -- result subtype of the function is inserted to handle cases where
5195 -- the access type of the allocator has a class-wide designated type.
5196
f937473f
RD
5197 Add_Access_Actual_To_Build_In_Place_Call
5198 (Func_Call,
5199 Function_Id,
5200 Make_Unchecked_Type_Conversion (Loc,
5201 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5202 Expression =>
5203 Make_Explicit_Dereference (Loc,
5204 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
5205
5206 -- When the result subtype is unconstrained, the function itself must
5207 -- perform the allocation of the return object, so we pass parameters
5208 -- indicating that. We don't yet handle the case where the allocation
5209 -- must be done in a user-defined storage pool, which will require
5210 -- passing another actual or two to provide allocation/deallocation
5211 -- operations. ???
5212
5213 else
7888a6ae 5214
f937473f
RD
5215 -- Pass an allocation parameter indicating that the function should
5216 -- allocate its result on the heap.
5217
5218 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5219 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
5220
7888a6ae
GD
5221 Add_Final_List_Actual_To_Build_In_Place_Call
5222 (Func_Call, Function_Id, Acc_Type);
f937473f 5223
f937473f
RD
5224 Add_Task_Actuals_To_Build_In_Place_Call
5225 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
7888a6ae
GD
5226
5227 -- The caller does not provide the return object in this case, so we
5228 -- have to pass null for the object access actual.
5229
f937473f
RD
5230 Add_Access_Actual_To_Build_In_Place_Call
5231 (Func_Call, Function_Id, Return_Object => Empty);
5232 end if;
02822a92
RD
5233
5234 -- Finally, replace the allocator node with a reference to the result
5235 -- of the function call itself (which will effectively be an access
5236 -- to the object created by the allocator).
5237
5238 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
5239 Analyze_And_Resolve (Allocator, Acc_Type);
5240 end Make_Build_In_Place_Call_In_Allocator;
5241
5242 ---------------------------------------------------
5243 -- Make_Build_In_Place_Call_In_Anonymous_Context --
5244 ---------------------------------------------------
5245
5246 procedure Make_Build_In_Place_Call_In_Anonymous_Context
5247 (Function_Call : Node_Id)
5248 is
5249 Loc : Source_Ptr;
5250 Func_Call : Node_Id := Function_Call;
5251 Function_Id : Entity_Id;
5252 Result_Subt : Entity_Id;
5253 Return_Obj_Id : Entity_Id;
5254 Return_Obj_Decl : Entity_Id;
5255
5256 begin
19590d70
GD
5257 -- Step past qualification or unchecked conversion (the latter can occur
5258 -- in cases of calls to 'Input).
5259
ac4d6407
RD
5260 if Nkind_In (Func_Call, N_Qualified_Expression,
5261 N_Unchecked_Type_Conversion)
19590d70 5262 then
02822a92
RD
5263 Func_Call := Expression (Func_Call);
5264 end if;
5265
fdce4bb7
JM
5266 -- If the call has already been processed to add build-in-place actuals
5267 -- then return. One place this can occur is for calls to build-in-place
5268 -- functions that occur within a call to a protected operation, where
5269 -- due to rewriting and expansion of the protected call there can be
5270 -- more than one call to Expand_Actuals for the same set of actuals.
5271
5272 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5273 return;
5274 end if;
5275
5276 -- Mark the call as processed as a build-in-place call
5277
5278 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5279
02822a92
RD
5280 Loc := Sloc (Function_Call);
5281
5282 if Is_Entity_Name (Name (Func_Call)) then
5283 Function_Id := Entity (Name (Func_Call));
5284
5285 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5286 Function_Id := Etype (Name (Func_Call));
5287
5288 else
5289 raise Program_Error;
5290 end if;
5291
5292 Result_Subt := Etype (Function_Id);
5293
f937473f
RD
5294 -- When the result subtype is constrained, an object of the subtype is
5295 -- declared and an access value designating it is passed as an actual.
02822a92 5296
7888a6ae 5297 if Is_Constrained (Underlying_Type (Result_Subt)) then
02822a92 5298
f937473f
RD
5299 -- Create a temporary object to hold the function result
5300
c12beea0 5301 Return_Obj_Id := Make_Temporary (Loc, 'R');
f937473f 5302 Set_Etype (Return_Obj_Id, Result_Subt);
02822a92 5303
f937473f
RD
5304 Return_Obj_Decl :=
5305 Make_Object_Declaration (Loc,
5306 Defining_Identifier => Return_Obj_Id,
5307 Aliased_Present => True,
5308 Object_Definition => New_Reference_To (Result_Subt, Loc));
02822a92 5309
f937473f 5310 Set_No_Initialization (Return_Obj_Decl);
02822a92 5311
f937473f 5312 Insert_Action (Func_Call, Return_Obj_Decl);
02822a92 5313
7888a6ae
GD
5314 -- When the function has a controlling result, an allocation-form
5315 -- parameter must be passed indicating that the caller is allocating
5316 -- the result object. This is needed because such a function can be
5317 -- called as a dispatching operation and must be treated similarly
5318 -- to functions with unconstrained result subtypes.
5319
5320 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5321 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5322
5323 Add_Final_List_Actual_To_Build_In_Place_Call
5324 (Func_Call, Function_Id, Acc_Type => Empty);
f937473f 5325
f937473f
RD
5326 Add_Task_Actuals_To_Build_In_Place_Call
5327 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7888a6ae
GD
5328
5329 -- Add an implicit actual to the function call that provides access
5330 -- to the caller's return object.
5331
f937473f
RD
5332 Add_Access_Actual_To_Build_In_Place_Call
5333 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
5334
5335 -- When the result subtype is unconstrained, the function must allocate
5336 -- the return object in the secondary stack, so appropriate implicit
5337 -- parameters are added to the call to indicate that. A transient
5338 -- scope is established to ensure eventual cleanup of the result.
5339
5340 else
7888a6ae 5341
f937473f
RD
5342 -- Pass an allocation parameter indicating that the function should
5343 -- allocate its result on the secondary stack.
5344
5345 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5346 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
5347
7888a6ae
GD
5348 Add_Final_List_Actual_To_Build_In_Place_Call
5349 (Func_Call, Function_Id, Acc_Type => Empty);
f937473f 5350
f937473f
RD
5351 Add_Task_Actuals_To_Build_In_Place_Call
5352 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7888a6ae
GD
5353
5354 -- Pass a null value to the function since no return object is
5355 -- available on the caller side.
5356
f937473f
RD
5357 Add_Access_Actual_To_Build_In_Place_Call
5358 (Func_Call, Function_Id, Empty);
5359
5360 Establish_Transient_Scope (Func_Call, Sec_Stack => True);
5361 end if;
02822a92
RD
5362 end Make_Build_In_Place_Call_In_Anonymous_Context;
5363
ce2798e8 5364 --------------------------------------------
02822a92 5365 -- Make_Build_In_Place_Call_In_Assignment --
ce2798e8 5366 --------------------------------------------
02822a92
RD
5367
5368 procedure Make_Build_In_Place_Call_In_Assignment
5369 (Assign : Node_Id;
5370 Function_Call : Node_Id)
5371 is
3a69b5ff
AC
5372 Lhs : constant Node_Id := Name (Assign);
5373 Func_Call : Node_Id := Function_Call;
5374 Func_Id : Entity_Id;
5375 Loc : Source_Ptr;
5376 Obj_Decl : Node_Id;
5377 Obj_Id : Entity_Id;
5378 Ptr_Typ : Entity_Id;
5379 Ptr_Typ_Decl : Node_Id;
5380 Result_Subt : Entity_Id;
5381 Target : Node_Id;
02822a92
RD
5382
5383 begin
19590d70
GD
5384 -- Step past qualification or unchecked conversion (the latter can occur
5385 -- in cases of calls to 'Input).
5386
ac4d6407
RD
5387 if Nkind_In (Func_Call, N_Qualified_Expression,
5388 N_Unchecked_Type_Conversion)
19590d70 5389 then
02822a92
RD
5390 Func_Call := Expression (Func_Call);
5391 end if;
5392
fdce4bb7
JM
5393 -- If the call has already been processed to add build-in-place actuals
5394 -- then return. This should not normally occur in an assignment context,
5395 -- but we add the protection as a defensive measure.
5396
5397 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5398 return;
5399 end if;
5400
5401 -- Mark the call as processed as a build-in-place call
5402
5403 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5404
02822a92
RD
5405 Loc := Sloc (Function_Call);
5406
5407 if Is_Entity_Name (Name (Func_Call)) then
3a69b5ff 5408 Func_Id := Entity (Name (Func_Call));
02822a92
RD
5409
5410 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
3a69b5ff 5411 Func_Id := Etype (Name (Func_Call));
02822a92
RD
5412
5413 else
5414 raise Program_Error;
5415 end if;
5416
3a69b5ff 5417 Result_Subt := Etype (Func_Id);
02822a92 5418
f937473f
RD
5419 -- When the result subtype is unconstrained, an additional actual must
5420 -- be passed to indicate that the caller is providing the return object.
7888a6ae
GD
5421 -- This parameter must also be passed when the called function has a
5422 -- controlling result, because dispatching calls to the function needs
5423 -- to be treated effectively the same as calls to class-wide functions.
f937473f 5424
7888a6ae 5425 Add_Alloc_Form_Actual_To_Build_In_Place_Call
3a69b5ff 5426 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
f937473f 5427
70f91180
RD
5428 -- If Lhs is a selected component, then pass it along so that its prefix
5429 -- object will be used as the source of the finalization list.
5430
5431 if Nkind (Lhs) = N_Selected_Component then
5432 Add_Final_List_Actual_To_Build_In_Place_Call
3a69b5ff 5433 (Func_Call, Func_Id, Acc_Type => Empty, Sel_Comp => Lhs);
70f91180
RD
5434 else
5435 Add_Final_List_Actual_To_Build_In_Place_Call
3a69b5ff 5436 (Func_Call, Func_Id, Acc_Type => Empty);
70f91180 5437 end if;
02822a92 5438
f937473f 5439 Add_Task_Actuals_To_Build_In_Place_Call
3a69b5ff 5440 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
7888a6ae
GD
5441
5442 -- Add an implicit actual to the function call that provides access to
5443 -- the caller's return object.
5444
02822a92
RD
5445 Add_Access_Actual_To_Build_In_Place_Call
5446 (Func_Call,
3a69b5ff 5447 Func_Id,
02822a92
RD
5448 Make_Unchecked_Type_Conversion (Loc,
5449 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5450 Expression => Relocate_Node (Lhs)));
5451
5452 -- Create an access type designating the function's result subtype
5453
c12beea0 5454 Ptr_Typ := Make_Temporary (Loc, 'A');
02822a92
RD
5455
5456 Ptr_Typ_Decl :=
5457 Make_Full_Type_Declaration (Loc,
3a69b5ff 5458 Defining_Identifier => Ptr_Typ,
02822a92
RD
5459 Type_Definition =>
5460 Make_Access_To_Object_Definition (Loc,
5461 All_Present => True,
5462 Subtype_Indication =>
5463 New_Reference_To (Result_Subt, Loc)));
02822a92
RD
5464 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
5465
5466 -- Finally, create an access object initialized to a reference to the
5467 -- function call.
5468
c12beea0 5469 Obj_Id := Make_Temporary (Loc, 'R');
3a69b5ff 5470 Set_Etype (Obj_Id, Ptr_Typ);
02822a92 5471
3a69b5ff 5472 Obj_Decl :=
02822a92 5473 Make_Object_Declaration (Loc,
3a69b5ff
AC
5474 Defining_Identifier => Obj_Id,
5475 Object_Definition =>
5476 New_Reference_To (Ptr_Typ, Loc),
5477 Expression =>
5478 Make_Reference (Loc,
5479 Prefix => Relocate_Node (Func_Call)));
5480 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
02822a92
RD
5481
5482 Rewrite (Assign, Make_Null_Statement (Loc));
3a69b5ff
AC
5483
5484 -- Retrieve the target of the assignment
5485
5486 if Nkind (Lhs) = N_Selected_Component then
5487 Target := Selector_Name (Lhs);
5488 elsif Nkind (Lhs) = N_Type_Conversion then
5489 Target := Expression (Lhs);
5490 else
5491 Target := Lhs;
5492 end if;
5493
5494 -- If we are assigning to a return object or this is an expression of
5495 -- an extension aggregate, the target should either be an identifier
5496 -- or a simple expression. All other cases imply a different scenario.
5497
5498 if Nkind (Target) in N_Has_Entity then
5499 Target := Entity (Target);
5500 else
5501 return;
5502 end if;
5503
5504 -- When the target of the assignment is a return object of an enclosing
5505 -- build-in-place function and also requires finalization, the list
5506 -- generated for the assignment must be moved to that of the enclosing
5507 -- function.
5508
5509 -- function Enclosing_BIP_Function return Ctrl_Typ is
5510 -- begin
5511 -- return (Ctrl_Parent_Part => BIP_Function with ...);
5512 -- end Enclosing_BIP_Function;
5513
5514 if Is_Return_Object (Target)
5515 and then Needs_Finalization (Etype (Target))
5516 and then Needs_Finalization (Result_Subt)
5517 then
5518 declare
5519 Obj_List : constant Node_Id := Find_Final_List (Obj_Id);
5520 Encl_List : Node_Id;
5521 Encl_Scop : Entity_Id;
5522
5523 begin
5524 Encl_Scop := Scope (Target);
5525
5526 -- Locate the scope of the extended return statement
5527
5528 while Present (Encl_Scop)
5529 and then Ekind (Encl_Scop) /= E_Return_Statement
5530 loop
5531 Encl_Scop := Scope (Encl_Scop);
5532 end loop;
5533
5534 -- A return object should always be enclosed by a return statement
5535 -- scope at some level.
5536
5537 pragma Assert (Present (Encl_Scop));
5538
5539 Encl_List :=
5540 Make_Attribute_Reference (Loc,
5541 Prefix =>
5542 New_Reference_To (
5543 Finalization_Chain_Entity (Encl_Scop), Loc),
5544 Attribute_Name => Name_Unrestricted_Access);
5545
5546 -- Generate a call to move final list
5547
5548 Insert_After_And_Analyze (Obj_Decl,
5549 Make_Procedure_Call_Statement (Loc,
5550 Name =>
5551 New_Reference_To (RTE (RE_Move_Final_List), Loc),
5552 Parameter_Associations => New_List (Obj_List, Encl_List)));
5553 end;
5554 end if;
02822a92
RD
5555 end Make_Build_In_Place_Call_In_Assignment;
5556
5557 ----------------------------------------------------
5558 -- Make_Build_In_Place_Call_In_Object_Declaration --
5559 ----------------------------------------------------
5560
5561 procedure Make_Build_In_Place_Call_In_Object_Declaration
5562 (Object_Decl : Node_Id;
5563 Function_Call : Node_Id)
5564 is
f937473f
RD
5565 Loc : Source_Ptr;
5566 Obj_Def_Id : constant Entity_Id :=
5567 Defining_Identifier (Object_Decl);
7888a6ae 5568
f937473f
RD
5569 Func_Call : Node_Id := Function_Call;
5570 Function_Id : Entity_Id;
5571 Result_Subt : Entity_Id;
5572 Caller_Object : Node_Id;
5573 Call_Deref : Node_Id;
5574 Ref_Type : Entity_Id;
5575 Ptr_Typ_Decl : Node_Id;
5576 Def_Id : Entity_Id;
5577 New_Expr : Node_Id;
5578 Enclosing_Func : Entity_Id;
5579 Pass_Caller_Acc : Boolean := False;
02822a92
RD
5580
5581 begin
19590d70
GD
5582 -- Step past qualification or unchecked conversion (the latter can occur
5583 -- in cases of calls to 'Input).
5584
ac4d6407
RD
5585 if Nkind_In (Func_Call, N_Qualified_Expression,
5586 N_Unchecked_Type_Conversion)
19590d70 5587 then
02822a92
RD
5588 Func_Call := Expression (Func_Call);
5589 end if;
5590
fdce4bb7
JM
5591 -- If the call has already been processed to add build-in-place actuals
5592 -- then return. This should not normally occur in an object declaration,
5593 -- but we add the protection as a defensive measure.
5594
5595 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5596 return;
5597 end if;
5598
5599 -- Mark the call as processed as a build-in-place call
5600
5601 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5602
02822a92
RD
5603 Loc := Sloc (Function_Call);
5604
5605 if Is_Entity_Name (Name (Func_Call)) then
5606 Function_Id := Entity (Name (Func_Call));
5607
5608 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5609 Function_Id := Etype (Name (Func_Call));
5610
5611 else
5612 raise Program_Error;
5613 end if;
5614
5615 Result_Subt := Etype (Function_Id);
5616
f937473f
RD
5617 -- In the constrained case, add an implicit actual to the function call
5618 -- that provides access to the declared object. An unchecked conversion
5619 -- to the (specific) result type of the function is inserted to handle
5620 -- the case where the object is declared with a class-wide type.
5621
7888a6ae 5622 if Is_Constrained (Underlying_Type (Result_Subt)) then
f937473f
RD
5623 Caller_Object :=
5624 Make_Unchecked_Type_Conversion (Loc,
5625 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5626 Expression => New_Reference_To (Obj_Def_Id, Loc));
02822a92 5627
7888a6ae
GD
5628 -- When the function has a controlling result, an allocation-form
5629 -- parameter must be passed indicating that the caller is allocating
5630 -- the result object. This is needed because such a function can be
5631 -- called as a dispatching operation and must be treated similarly
5632 -- to functions with unconstrained result subtypes.
5633
5634 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5635 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5636
f937473f
RD
5637 -- If the function's result subtype is unconstrained and the object is
5638 -- a return object of an enclosing build-in-place function, then the
5639 -- implicit build-in-place parameters of the enclosing function must be
ce14c577
AC
5640 -- passed along to the called function. (Unfortunately, this won't cover
5641 -- the case of extension aggregates where the ancestor part is a build-
5642 -- in-place unconstrained function call that should be passed along the
5643 -- caller's parameters. Currently those get mishandled by reassigning
5644 -- the result of the call to the aggregate return object, when the call
5645 -- result should really be directly built in place in the aggregate and
5646 -- not built in a temporary. ???)
5647
5648 elsif Is_Return_Object (Defining_Identifier (Object_Decl)) then
f937473f
RD
5649 Pass_Caller_Acc := True;
5650
5651 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
5652
5653 -- If the enclosing function has a constrained result type, then
5654 -- caller allocation will be used.
5655
5656 if Is_Constrained (Etype (Enclosing_Func)) then
5657 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5658 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5659
5660 -- Otherwise, when the enclosing function has an unconstrained result
5661 -- type, the BIP_Alloc_Form formal of the enclosing function must be
7888a6ae 5662 -- passed along to the callee.
f937473f
RD
5663
5664 else
5665 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5666 (Func_Call,
5667 Function_Id,
5668 Alloc_Form_Exp =>
5669 New_Reference_To
5670 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
5671 Loc));
5672 end if;
5673
5674 -- Retrieve the BIPacc formal from the enclosing function and convert
5675 -- it to the access type of the callee's BIP_Object_Access formal.
5676
5677 Caller_Object :=
5678 Make_Unchecked_Type_Conversion (Loc,
5679 Subtype_Mark =>
5680 New_Reference_To
5681 (Etype
5682 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
5683 Loc),
5684 Expression =>
5685 New_Reference_To
5686 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
5687 Loc));
5688
5689 -- In other unconstrained cases, pass an indication to do the allocation
5690 -- on the secondary stack and set Caller_Object to Empty so that a null
5691 -- value will be passed for the caller's object address. A transient
5692 -- scope is established to ensure eventual cleanup of the result.
5693
5694 else
5695 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5696 (Func_Call,
5697 Function_Id,
5698 Alloc_Form => Secondary_Stack);
5699 Caller_Object := Empty;
5700
5701 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
5702 end if;
5703
7888a6ae
GD
5704 Add_Final_List_Actual_To_Build_In_Place_Call
5705 (Func_Call, Function_Id, Acc_Type => Empty);
5706
f937473f
RD
5707 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
5708 and then Has_Task (Result_Subt)
5709 then
5710 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
7888a6ae
GD
5711
5712 -- Here we're passing along the master that was passed in to this
5713 -- function.
5714
f937473f
RD
5715 Add_Task_Actuals_To_Build_In_Place_Call
5716 (Func_Call, Function_Id,
5717 Master_Actual =>
5718 New_Reference_To
5719 (Build_In_Place_Formal (Enclosing_Func, BIP_Master), Loc));
7888a6ae 5720
f937473f
RD
5721 else
5722 Add_Task_Actuals_To_Build_In_Place_Call
5723 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
5724 end if;
7888a6ae 5725
02822a92 5726 Add_Access_Actual_To_Build_In_Place_Call
f937473f 5727 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
02822a92 5728
b0b7b57d
AC
5729 -- Create an access type designating the function's result subtype. We
5730 -- use the type of the original expression because it may be a call to
5731 -- an inherited operation, which the expansion has replaced with the
5732 -- parent operation that yields the parent type.
02822a92 5733
c12beea0 5734 Ref_Type := Make_Temporary (Loc, 'A');
02822a92
RD
5735
5736 Ptr_Typ_Decl :=
5737 Make_Full_Type_Declaration (Loc,
5738 Defining_Identifier => Ref_Type,
5739 Type_Definition =>
5740 Make_Access_To_Object_Definition (Loc,
5741 All_Present => True,
5742 Subtype_Indication =>
b0b7b57d 5743 New_Reference_To (Etype (Function_Call), Loc)));
02822a92 5744
f937473f
RD
5745 -- The access type and its accompanying object must be inserted after
5746 -- the object declaration in the constrained case, so that the function
5747 -- call can be passed access to the object. In the unconstrained case,
5748 -- the access type and object must be inserted before the object, since
5749 -- the object declaration is rewritten to be a renaming of a dereference
5750 -- of the access object.
5751
7888a6ae 5752 if Is_Constrained (Underlying_Type (Result_Subt)) then
f937473f
RD
5753 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
5754 else
4f6e2c24 5755 Insert_Action (Object_Decl, Ptr_Typ_Decl);
f937473f 5756 end if;
02822a92
RD
5757
5758 -- Finally, create an access object initialized to a reference to the
5759 -- function call.
5760
02822a92
RD
5761 New_Expr :=
5762 Make_Reference (Loc,
5763 Prefix => Relocate_Node (Func_Call));
5764
c12beea0
RD
5765 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
5766 Set_Etype (Def_Id, Ref_Type);
5767
02822a92
RD
5768 Insert_After_And_Analyze (Ptr_Typ_Decl,
5769 Make_Object_Declaration (Loc,
5770 Defining_Identifier => Def_Id,
5771 Object_Definition => New_Reference_To (Ref_Type, Loc),
5772 Expression => New_Expr));
5773
7888a6ae 5774 if Is_Constrained (Underlying_Type (Result_Subt)) then
f937473f
RD
5775 Set_Expression (Object_Decl, Empty);
5776 Set_No_Initialization (Object_Decl);
5777
5778 -- In case of an unconstrained result subtype, rewrite the object
5779 -- declaration as an object renaming where the renamed object is a
5780 -- dereference of <function_Call>'reference:
5781 --
5782 -- Obj : Subt renames <function_call>'Ref.all;
5783
5784 else
5785 Call_Deref :=
5786 Make_Explicit_Dereference (Loc,
5787 Prefix => New_Reference_To (Def_Id, Loc));
5788
5789 Rewrite (Object_Decl,
5790 Make_Object_Renaming_Declaration (Loc,
c12beea0 5791 Defining_Identifier => Make_Temporary (Loc, 'D'),
f937473f
RD
5792 Access_Definition => Empty,
5793 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
5794 Name => Call_Deref));
5795
5796 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
5797
5798 Analyze (Object_Decl);
5799
5800 -- Replace the internal identifier of the renaming declaration's
5801 -- entity with identifier of the original object entity. We also have
5802 -- to exchange the entities containing their defining identifiers to
5803 -- ensure the correct replacement of the object declaration by the
5804 -- object renaming declaration to avoid homograph conflicts (since
5805 -- the object declaration's defining identifier was already entered
67ce0d7e
RD
5806 -- in current scope). The Next_Entity links of the two entities also
5807 -- have to be swapped since the entities are part of the return
5808 -- scope's entity list and the list structure would otherwise be
7e8ed0a6 5809 -- corrupted. Finally, the homonym chain must be preserved as well.
67ce0d7e
RD
5810
5811 declare
5812 Renaming_Def_Id : constant Entity_Id :=
5813 Defining_Identifier (Object_Decl);
5814 Next_Entity_Temp : constant Entity_Id :=
5815 Next_Entity (Renaming_Def_Id);
5816 begin
5817 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
5818
5819 -- Swap next entity links in preparation for exchanging entities
f937473f 5820
67ce0d7e
RD
5821 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
5822 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
7e8ed0a6 5823 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
67ce0d7e
RD
5824
5825 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
5826 end;
f937473f 5827 end if;
02822a92
RD
5828
5829 -- If the object entity has a class-wide Etype, then we need to change
5830 -- it to the result subtype of the function call, because otherwise the
53b308f6
AC
5831 -- object will be class-wide without an explicit initialization and
5832 -- won't be allocated properly by the back end. It seems unclean to make
5833 -- such a revision to the type at this point, and we should try to
5834 -- improve this treatment when build-in-place functions with class-wide
5835 -- results are implemented. ???
02822a92
RD
5836
5837 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
5838 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
5839 end if;
5840 end Make_Build_In_Place_Call_In_Object_Declaration;
5841
8fb68c56
RD
5842 --------------------------
5843 -- Needs_BIP_Final_List --
5844 --------------------------
5845
048e5cef
BD
5846 function Needs_BIP_Final_List (E : Entity_Id) return Boolean is
5847 pragma Assert (Is_Build_In_Place_Function (E));
5848 Result_Subt : constant Entity_Id := Underlying_Type (Etype (E));
8fb68c56 5849
048e5cef
BD
5850 begin
5851 -- We need the BIP_Final_List if the result type needs finalization. We
5852 -- also need it for tagged types, even if not class-wide, because some
5853 -- type extension might need finalization, and all overriding functions
5854 -- must have the same calling conventions. However, if there is a
5855 -- pragma Restrictions (No_Finalization), we never need this parameter.
5856
5857 return (Needs_Finalization (Result_Subt)
8fb68c56 5858 or else Is_Tagged_Type (Underlying_Type (Result_Subt)))
048e5cef
BD
5859 and then not Restriction_Active (No_Finalization);
5860 end Needs_BIP_Final_List;
5861
70482933 5862end Exp_Ch6;
This page took 2.832528 seconds and 5 git commands to generate.