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