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