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