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