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