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