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