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