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