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