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