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