Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / exp_ch6.adb
blob7abf25e3859a8b6992f21fa0bf3f63284002df7b
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 -- CodePeer does not do anything useful on Ada.Tags.Type_Specific_Data
691 -- components.
693 if Ada_Version >= Ada_2005
694 and then not CodePeer_Mode
695 and then Tagged_Type_Expansion
696 and then not Scope_Suppress.Suppress (Accessibility_Check)
697 and then
698 (Is_Class_Wide_Type (Etype (Exp))
699 or else Nkind (Exp) in
700 N_Type_Conversion | N_Unchecked_Type_Conversion
701 or else (Is_Entity_Name (Exp)
702 and then Is_Formal (Entity (Exp)))
703 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
704 Scope_Depth (Enclosing_Dynamic_Scope (Func)))
705 then
706 declare
707 Tag_Node : Node_Id;
709 begin
710 -- Ada 2005 (AI-251): In class-wide interface objects we displace
711 -- "this" to reference the base of the object. This is required to
712 -- get access to the TSD of the object.
714 if Is_Class_Wide_Type (Etype (Exp))
715 and then Is_Interface (Etype (Exp))
716 then
717 -- If the expression is an explicit dereference then we can
718 -- directly displace the pointer to reference the base of
719 -- the object.
721 if Nkind (Exp) = N_Explicit_Dereference then
722 Tag_Node :=
723 Make_Explicit_Dereference (Loc,
724 Prefix =>
725 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
726 Make_Function_Call (Loc,
727 Name =>
728 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
729 Parameter_Associations => New_List (
730 Unchecked_Convert_To (RTE (RE_Address),
731 Duplicate_Subexpr (Prefix (Exp)))))));
733 -- Similar case to the previous one but the expression is a
734 -- renaming of an explicit dereference.
736 elsif Nkind (Exp) = N_Identifier
737 and then Present (Renamed_Object (Entity (Exp)))
738 and then Nkind (Renamed_Object (Entity (Exp)))
739 = N_Explicit_Dereference
740 then
741 Tag_Node :=
742 Make_Explicit_Dereference (Loc,
743 Prefix =>
744 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
745 Make_Function_Call (Loc,
746 Name =>
747 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
748 Parameter_Associations => New_List (
749 Unchecked_Convert_To (RTE (RE_Address),
750 Duplicate_Subexpr
751 (Prefix
752 (Renamed_Object (Entity (Exp)))))))));
754 -- Common case: obtain the address of the actual object and
755 -- displace the pointer to reference the base of the object.
757 else
758 Tag_Node :=
759 Make_Explicit_Dereference (Loc,
760 Prefix =>
761 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
762 Make_Function_Call (Loc,
763 Name =>
764 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
765 Parameter_Associations => New_List (
766 Make_Attribute_Reference (Loc,
767 Prefix => Duplicate_Subexpr (Exp),
768 Attribute_Name => Name_Address)))));
769 end if;
770 else
771 Tag_Node :=
772 Make_Attribute_Reference (Loc,
773 Prefix => Duplicate_Subexpr (Exp),
774 Attribute_Name => Name_Tag);
775 end if;
777 -- Suppress junk access chacks on RE_Tag_Ptr
779 Insert_Action (Exp,
780 Make_Raise_Program_Error (Loc,
781 Condition =>
782 Make_Op_Gt (Loc,
783 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
784 Right_Opnd =>
785 Make_Integer_Literal (Loc,
786 Scope_Depth (Enclosing_Dynamic_Scope (Func)))),
787 Reason => PE_Accessibility_Check_Failed),
788 Suppress => Access_Check);
789 end;
790 end if;
791 end Apply_CW_Accessibility_Check;
793 -----------------------
794 -- BIP_Formal_Suffix --
795 -----------------------
797 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
798 begin
799 case Kind is
800 when BIP_Alloc_Form =>
801 return BIP_Alloc_Suffix;
803 when BIP_Storage_Pool =>
804 return BIP_Storage_Pool_Suffix;
806 when BIP_Finalization_Master =>
807 return BIP_Finalization_Master_Suffix;
809 when BIP_Task_Master =>
810 return BIP_Task_Master_Suffix;
812 when BIP_Activation_Chain =>
813 return BIP_Activation_Chain_Suffix;
815 when BIP_Object_Access =>
816 return BIP_Object_Access_Suffix;
817 end case;
818 end BIP_Formal_Suffix;
820 ---------------------
821 -- BIP_Suffix_Kind --
822 ---------------------
824 function BIP_Suffix_Kind (E : Entity_Id) return BIP_Formal_Kind is
825 Nam : constant String := Get_Name_String (Chars (E));
827 function Has_Suffix (Suffix : String) return Boolean;
828 -- Return True if Nam has suffix Suffix
830 function Has_Suffix (Suffix : String) return Boolean is
831 Len : constant Natural := Suffix'Length;
832 begin
833 return Nam'Length > Len
834 and then Nam (Nam'Last - Len + 1 .. Nam'Last) = Suffix;
835 end Has_Suffix;
837 -- Start of processing for BIP_Suffix_Kind
839 begin
840 if Has_Suffix (BIP_Alloc_Suffix) then
841 return BIP_Alloc_Form;
843 elsif Has_Suffix (BIP_Storage_Pool_Suffix) then
844 return BIP_Storage_Pool;
846 elsif Has_Suffix (BIP_Finalization_Master_Suffix) then
847 return BIP_Finalization_Master;
849 elsif Has_Suffix (BIP_Task_Master_Suffix) then
850 return BIP_Task_Master;
852 elsif Has_Suffix (BIP_Activation_Chain_Suffix) then
853 return BIP_Activation_Chain;
855 elsif Has_Suffix (BIP_Object_Access_Suffix) then
856 return BIP_Object_Access;
858 else
859 raise Program_Error;
860 end if;
861 end BIP_Suffix_Kind;
863 ---------------------------
864 -- Build_In_Place_Formal --
865 ---------------------------
867 function Build_In_Place_Formal
868 (Func : Entity_Id;
869 Kind : BIP_Formal_Kind) return Entity_Id
871 Extra_Formal : Entity_Id := Extra_Formals (Func);
872 Formal_Suffix : constant String := BIP_Formal_Suffix (Kind);
874 begin
875 -- Maybe it would be better for each implicit formal of a build-in-place
876 -- function to have a flag or a Uint attribute to identify it. ???
878 -- The return type in the function declaration may have been a limited
879 -- view, and the extra formals for the function were not generated at
880 -- that point. At the point of call the full view must be available and
881 -- the extra formals can be created and Returns_By_Ref computed.
883 if No (Extra_Formal) then
884 Create_Extra_Formals (Func);
885 Extra_Formal := Extra_Formals (Func);
886 Compute_Returns_By_Ref (Func);
887 end if;
889 -- We search for a formal with a matching suffix. We can't search
890 -- for the full name, because of the code at the end of Sem_Ch6.-
891 -- Create_Extra_Formals, which copies the Extra_Formals over to
892 -- the Alias of an instance, which will cause the formals to have
893 -- "incorrect" names.
895 loop
896 pragma Assert (Present (Extra_Formal));
897 declare
898 Name : constant String := Get_Name_String (Chars (Extra_Formal));
899 begin
900 exit when Name'Length >= Formal_Suffix'Length
901 and then Formal_Suffix =
902 Name (Name'Last - Formal_Suffix'Length + 1 .. Name'Last);
903 end;
905 Next_Formal_With_Extras (Extra_Formal);
906 end loop;
908 return Extra_Formal;
909 end Build_In_Place_Formal;
911 -------------------------------
912 -- Build_Procedure_Body_Form --
913 -------------------------------
915 function Build_Procedure_Body_Form
916 (Func_Id : Entity_Id;
917 Func_Body : Node_Id) return Node_Id
919 Loc : constant Source_Ptr := Sloc (Func_Body);
921 Proc_Decl : constant Node_Id := Prev (Unit_Declaration_Node (Func_Id));
922 -- It is assumed that the node before the declaration of the
923 -- corresponding subprogram spec is the declaration of the procedure
924 -- form.
926 Proc_Id : constant Entity_Id := Defining_Entity (Proc_Decl);
928 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id);
929 -- Replace each return statement found in the list Stmts with an
930 -- assignment of the return expression to parameter Param_Id.
932 ---------------------
933 -- Replace_Returns --
934 ---------------------
936 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id) is
937 Stmt : Node_Id;
939 begin
940 Stmt := First (Stmts);
941 while Present (Stmt) loop
942 if Nkind (Stmt) = N_Block_Statement then
943 Replace_Returns (Param_Id,
944 Statements (Handled_Statement_Sequence (Stmt)));
946 elsif Nkind (Stmt) = N_Case_Statement then
947 declare
948 Alt : Node_Id;
949 begin
950 Alt := First (Alternatives (Stmt));
951 while Present (Alt) loop
952 Replace_Returns (Param_Id, Statements (Alt));
953 Next (Alt);
954 end loop;
955 end;
957 elsif Nkind (Stmt) = N_Extended_Return_Statement then
958 declare
959 Ret_Obj : constant Entity_Id :=
960 Defining_Entity
961 (First (Return_Object_Declarations (Stmt)));
962 Assign : constant Node_Id :=
963 Make_Assignment_Statement (Sloc (Stmt),
964 Name =>
965 New_Occurrence_Of (Param_Id, Loc),
966 Expression =>
967 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
968 Stmts : List_Id;
970 begin
971 -- The extended return may just contain the declaration
973 if Present (Handled_Statement_Sequence (Stmt)) then
974 Stmts := Statements (Handled_Statement_Sequence (Stmt));
975 else
976 Stmts := New_List;
977 end if;
979 Set_Assignment_OK (Name (Assign));
981 Rewrite (Stmt,
982 Make_Block_Statement (Sloc (Stmt),
983 Declarations =>
984 Return_Object_Declarations (Stmt),
985 Handled_Statement_Sequence =>
986 Make_Handled_Sequence_Of_Statements (Loc,
987 Statements => Stmts)));
989 Replace_Returns (Param_Id, Stmts);
991 Append_To (Stmts, Assign);
992 Append_To (Stmts, Make_Simple_Return_Statement (Loc));
993 end;
995 elsif Nkind (Stmt) = N_If_Statement then
996 Replace_Returns (Param_Id, Then_Statements (Stmt));
997 Replace_Returns (Param_Id, Else_Statements (Stmt));
999 declare
1000 Part : Node_Id;
1001 begin
1002 Part := First (Elsif_Parts (Stmt));
1003 while Present (Part) loop
1004 Replace_Returns (Param_Id, Then_Statements (Part));
1005 Next (Part);
1006 end loop;
1007 end;
1009 elsif Nkind (Stmt) = N_Loop_Statement then
1010 Replace_Returns (Param_Id, Statements (Stmt));
1012 elsif Nkind (Stmt) = N_Simple_Return_Statement then
1014 -- Generate:
1015 -- Param := Expr;
1016 -- return;
1018 Rewrite (Stmt,
1019 Make_Assignment_Statement (Sloc (Stmt),
1020 Name => New_Occurrence_Of (Param_Id, Loc),
1021 Expression => Relocate_Node (Expression (Stmt))));
1023 Insert_After (Stmt, Make_Simple_Return_Statement (Loc));
1025 -- Skip the added return
1027 Next (Stmt);
1028 end if;
1030 Next (Stmt);
1031 end loop;
1032 end Replace_Returns;
1034 -- Local variables
1036 Stmts : List_Id;
1037 New_Body : Node_Id;
1039 -- Start of processing for Build_Procedure_Body_Form
1041 begin
1042 -- This routine replaces the original function body:
1044 -- function F (...) return Array_Typ is
1045 -- begin
1046 -- ...
1047 -- return Something;
1048 -- end F;
1050 -- with the following:
1052 -- procedure P (..., Result : out Array_Typ) is
1053 -- begin
1054 -- ...
1055 -- Result := Something;
1056 -- end P;
1058 Stmts :=
1059 Statements (Handled_Statement_Sequence (Func_Body));
1060 Replace_Returns (Last_Entity (Proc_Id), Stmts);
1062 New_Body :=
1063 Make_Subprogram_Body (Loc,
1064 Specification =>
1065 Copy_Subprogram_Spec (Specification (Proc_Decl)),
1066 Declarations => Declarations (Func_Body),
1067 Handled_Statement_Sequence =>
1068 Make_Handled_Sequence_Of_Statements (Loc,
1069 Statements => Stmts));
1071 -- If the function is a generic instance, so is the new procedure.
1072 -- Set flag accordingly so that the proper renaming declarations are
1073 -- generated.
1075 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
1076 return New_Body;
1077 end Build_Procedure_Body_Form;
1079 -----------------------
1080 -- Caller_Known_Size --
1081 -----------------------
1083 function Caller_Known_Size
1084 (Func_Call : Node_Id;
1085 Result_Subt : Entity_Id) return Boolean
1087 Utyp : constant Entity_Id := Underlying_Type (Result_Subt);
1089 begin
1090 return not Needs_Secondary_Stack (Utyp)
1091 and then not (Is_Tagged_Type (Utyp)
1092 and then Present (Controlling_Argument (Func_Call)));
1093 end Caller_Known_Size;
1095 -----------------------
1096 -- Check_BIP_Actuals --
1097 -----------------------
1099 function Check_BIP_Actuals
1100 (Subp_Call : Node_Id;
1101 Subp_Id : Entity_Id) return Boolean
1103 Formal : Entity_Id;
1104 Actual : Node_Id;
1106 begin
1107 pragma Assert (Nkind (Subp_Call) in N_Entry_Call_Statement
1108 | N_Function_Call
1109 | N_Procedure_Call_Statement);
1111 -- In CodePeer_Mode, the tree for `'Elab_Spec` procedures will be
1112 -- malformed because GNAT does not perform the usual expansion that
1113 -- results in the importation of external elaboration procedure symbols.
1114 -- This is expected: the CodePeer backend has special handling for this
1115 -- malformed tree.
1116 -- Thus, we do not need to check the tree (and in fact can't, because
1117 -- it's malformed).
1119 if CodePeer_Mode
1120 and then Nkind (Name (Subp_Call)) = N_Attribute_Reference
1121 and then Attribute_Name (Name (Subp_Call)) in Name_Elab_Spec
1122 | Name_Elab_Body
1123 | Name_Elab_Subp_Body
1124 then
1125 return True;
1126 end if;
1128 Formal := First_Formal_With_Extras (Subp_Id);
1129 Actual := First_Actual (Subp_Call);
1131 while Present (Formal) and then Present (Actual) loop
1132 if Is_Build_In_Place_Entity (Formal)
1133 and then Nkind (Actual) = N_Identifier
1134 and then Is_Build_In_Place_Entity (Entity (Actual))
1135 and then BIP_Suffix_Kind (Formal)
1136 /= BIP_Suffix_Kind (Entity (Actual))
1137 then
1138 return False;
1139 end if;
1141 Next_Formal_With_Extras (Formal);
1142 Next_Actual (Actual);
1143 end loop;
1145 return No (Formal) and then No (Actual);
1146 end Check_BIP_Actuals;
1148 -----------------------------
1149 -- Check_Number_Of_Actuals --
1150 -----------------------------
1152 function Check_Number_Of_Actuals
1153 (Subp_Call : Node_Id;
1154 Subp_Id : Entity_Id) return Boolean
1156 Formal : Entity_Id;
1157 Actual : Node_Id;
1159 begin
1160 pragma Assert (Nkind (Subp_Call) in N_Entry_Call_Statement
1161 | N_Function_Call
1162 | N_Procedure_Call_Statement);
1164 Formal := First_Formal_With_Extras (Subp_Id);
1165 Actual := First_Actual (Subp_Call);
1167 while Present (Formal) and then Present (Actual) loop
1168 Next_Formal_With_Extras (Formal);
1169 Next_Actual (Actual);
1170 end loop;
1172 return No (Formal) and then No (Actual);
1173 end Check_Number_Of_Actuals;
1175 --------------------------------
1176 -- Check_Overriding_Operation --
1177 --------------------------------
1179 procedure Check_Overriding_Operation (Subp : Entity_Id) is
1180 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1181 Op_List : constant Elist_Id := Primitive_Operations (Typ);
1182 Op_Elmt : Elmt_Id;
1183 Prim_Op : Entity_Id;
1184 Par_Op : Entity_Id;
1186 begin
1187 if Is_Derived_Type (Typ)
1188 and then not Is_Private_Type (Typ)
1189 and then In_Open_Scopes (Scope (Etype (Typ)))
1190 and then Is_Base_Type (Typ)
1191 then
1192 -- Subp overrides an inherited private operation if there is an
1193 -- inherited operation with a different name than Subp (see
1194 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
1195 -- same name as Subp.
1197 Op_Elmt := First_Elmt (Op_List);
1198 while Present (Op_Elmt) loop
1199 Prim_Op := Node (Op_Elmt);
1200 Par_Op := Alias (Prim_Op);
1202 if Present (Par_Op)
1203 and then not Comes_From_Source (Prim_Op)
1204 and then Chars (Prim_Op) /= Chars (Par_Op)
1205 and then Chars (Par_Op) = Chars (Subp)
1206 and then Is_Hidden (Par_Op)
1207 and then Type_Conformant (Prim_Op, Subp)
1208 then
1209 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
1210 end if;
1212 Next_Elmt (Op_Elmt);
1213 end loop;
1214 end if;
1215 end Check_Overriding_Operation;
1217 -------------------------------
1218 -- Detect_Infinite_Recursion --
1219 -------------------------------
1221 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
1222 Loc : constant Source_Ptr := Sloc (N);
1224 Var_List : constant Elist_Id := New_Elmt_List;
1225 -- List of globals referenced by body of procedure
1227 Call_List : constant Elist_Id := New_Elmt_List;
1228 -- List of recursive calls in body of procedure
1230 Shad_List : constant Elist_Id := New_Elmt_List;
1231 -- List of entity id's for entities created to capture the value of
1232 -- referenced globals on entry to the procedure.
1234 Scop : constant Uint := Scope_Depth (Spec);
1235 -- This is used to record the scope depth of the current procedure, so
1236 -- that we can identify global references.
1238 Max_Vars : constant := 4;
1239 -- Do not test more than four global variables
1241 Count_Vars : Natural := 0;
1242 -- Count variables found so far
1244 Var : Entity_Id;
1245 Elm : Elmt_Id;
1246 Ent : Entity_Id;
1247 Call : Elmt_Id;
1248 Decl : Node_Id;
1249 Test : Node_Id;
1250 Elm1 : Elmt_Id;
1251 Elm2 : Elmt_Id;
1252 Last : Node_Id;
1254 function Process (Nod : Node_Id) return Traverse_Result;
1255 -- Function to traverse the subprogram body (using Traverse_Func)
1257 -------------
1258 -- Process --
1259 -------------
1261 function Process (Nod : Node_Id) return Traverse_Result is
1262 begin
1263 -- Procedure call
1265 if Nkind (Nod) = N_Procedure_Call_Statement then
1267 -- Case of one of the detected recursive calls
1269 if Is_Entity_Name (Name (Nod))
1270 and then Has_Recursive_Call (Entity (Name (Nod)))
1271 and then Entity (Name (Nod)) = Spec
1272 then
1273 Append_Elmt (Nod, Call_List);
1274 return Skip;
1276 -- Any other procedure call may have side effects
1278 else
1279 return Abandon;
1280 end if;
1282 -- A call to a pure function can always be ignored
1284 elsif Nkind (Nod) = N_Function_Call
1285 and then Is_Entity_Name (Name (Nod))
1286 and then Is_Pure (Entity (Name (Nod)))
1287 then
1288 return Skip;
1290 -- Case of an identifier reference
1292 elsif Nkind (Nod) = N_Identifier then
1293 Ent := Entity (Nod);
1295 -- If no entity, then ignore the reference
1297 -- Not clear why this can happen. To investigate, remove this
1298 -- test and look at the crash that occurs here in 3401-004 ???
1300 if No (Ent) then
1301 return Skip;
1303 -- Ignore entities with no Scope, again not clear how this
1304 -- can happen, to investigate, look at 4108-008 ???
1306 elsif No (Scope (Ent)) then
1307 return Skip;
1309 -- Ignore the reference if not to a more global object
1311 elsif Scope_Depth (Scope (Ent)) >= Scop then
1312 return Skip;
1314 -- References to types, exceptions and constants are always OK
1316 elsif Is_Type (Ent)
1317 or else Ekind (Ent) = E_Exception
1318 or else Ekind (Ent) = E_Constant
1319 then
1320 return Skip;
1322 -- If other than a non-volatile scalar variable, we have some
1323 -- kind of global reference (e.g. to a function) that we cannot
1324 -- deal with so we forget the attempt.
1326 elsif Ekind (Ent) /= E_Variable
1327 or else not Is_Scalar_Type (Etype (Ent))
1328 or else Treat_As_Volatile (Ent)
1329 then
1330 return Abandon;
1332 -- Otherwise we have a reference to a global scalar
1334 else
1335 -- Loop through global entities already detected
1337 Elm := First_Elmt (Var_List);
1338 loop
1339 -- If not detected before, record this new global reference
1341 if No (Elm) then
1342 Count_Vars := Count_Vars + 1;
1344 if Count_Vars <= Max_Vars then
1345 Append_Elmt (Entity (Nod), Var_List);
1346 else
1347 return Abandon;
1348 end if;
1350 exit;
1352 -- If recorded before, ignore
1354 elsif Node (Elm) = Entity (Nod) then
1355 return Skip;
1357 -- Otherwise keep looking
1359 else
1360 Next_Elmt (Elm);
1361 end if;
1362 end loop;
1364 return Skip;
1365 end if;
1367 -- For all other node kinds, recursively visit syntactic children
1369 else
1370 return OK;
1371 end if;
1372 end Process;
1374 function Traverse_Body is new Traverse_Func (Process);
1376 -- Start of processing for Detect_Infinite_Recursion
1378 begin
1379 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1380 -- won't be able to generate the code to handle the recursion in any
1381 -- case.
1383 if Restriction_Active (No_Implicit_Conditionals) then
1384 return;
1385 end if;
1387 -- Otherwise do traversal and quit if we get abandon signal
1389 if Traverse_Body (N) = Abandon then
1390 return;
1392 -- We must have a call, since Has_Recursive_Call was set. If not just
1393 -- ignore (this is only an error check, so if we have a funny situation,
1394 -- due to bugs or errors, we do not want to bomb).
1396 elsif Is_Empty_Elmt_List (Call_List) then
1397 return;
1398 end if;
1400 -- Here is the case where we detect recursion at compile time
1402 -- Push our current scope for analyzing the declarations and code that
1403 -- we will insert for the checking.
1405 Push_Scope (Spec);
1407 -- This loop builds temporary variables for each of the referenced
1408 -- globals, so that at the end of the loop the list Shad_List contains
1409 -- these temporaries in one-to-one correspondence with the elements in
1410 -- Var_List.
1412 Last := Empty;
1413 Elm := First_Elmt (Var_List);
1414 while Present (Elm) loop
1415 Var := Node (Elm);
1416 Ent := Make_Temporary (Loc, 'S');
1417 Append_Elmt (Ent, Shad_List);
1419 -- Insert a declaration for this temporary at the start of the
1420 -- declarations for the procedure. The temporaries are declared as
1421 -- constant objects initialized to the current values of the
1422 -- corresponding temporaries.
1424 Decl :=
1425 Make_Object_Declaration (Loc,
1426 Defining_Identifier => Ent,
1427 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
1428 Constant_Present => True,
1429 Expression => New_Occurrence_Of (Var, Loc));
1431 if No (Last) then
1432 Prepend (Decl, Declarations (N));
1433 else
1434 Insert_After (Last, Decl);
1435 end if;
1437 Last := Decl;
1438 Analyze (Decl);
1439 Next_Elmt (Elm);
1440 end loop;
1442 -- Loop through calls
1444 Call := First_Elmt (Call_List);
1445 while Present (Call) loop
1447 -- Build a predicate expression of the form
1449 -- True
1450 -- and then global1 = temp1
1451 -- and then global2 = temp2
1452 -- ...
1454 -- This predicate determines if any of the global values
1455 -- referenced by the procedure have changed since the
1456 -- current call, if not an infinite recursion is assured.
1458 Test := New_Occurrence_Of (Standard_True, Loc);
1460 Elm1 := First_Elmt (Var_List);
1461 Elm2 := First_Elmt (Shad_List);
1462 while Present (Elm1) loop
1463 Test :=
1464 Make_And_Then (Loc,
1465 Left_Opnd => Test,
1466 Right_Opnd =>
1467 Make_Op_Eq (Loc,
1468 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1469 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1471 Next_Elmt (Elm1);
1472 Next_Elmt (Elm2);
1473 end loop;
1475 -- Now we replace the call with the sequence
1477 -- if no-changes (see above) then
1478 -- raise Storage_Error;
1479 -- else
1480 -- original-call
1481 -- end if;
1483 Rewrite (Node (Call),
1484 Make_If_Statement (Loc,
1485 Condition => Test,
1486 Then_Statements => New_List (
1487 Make_Raise_Storage_Error (Loc,
1488 Reason => SE_Infinite_Recursion)),
1490 Else_Statements => New_List (
1491 Relocate_Node (Node (Call)))));
1493 Analyze (Node (Call));
1495 Next_Elmt (Call);
1496 end loop;
1498 -- Remove temporary scope stack entry used for analysis
1500 Pop_Scope;
1501 end Detect_Infinite_Recursion;
1503 --------------------
1504 -- Expand_Actuals --
1505 --------------------
1507 procedure Expand_Actuals
1508 (N : Node_Id;
1509 Subp : Entity_Id;
1510 Post_Call : out List_Id)
1512 Loc : constant Source_Ptr := Sloc (N);
1513 Actual : Node_Id;
1514 Formal : Entity_Id;
1515 N_Node : Node_Id;
1516 E_Actual : Entity_Id;
1517 E_Formal : Entity_Id;
1519 procedure Add_Call_By_Copy_Code;
1520 -- For cases where the parameter must be passed by copy, this routine
1521 -- generates a temporary variable into which the actual is copied and
1522 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1523 -- an assignment is also generated to copy the result back. The call
1524 -- also takes care of any constraint checks required for the type
1525 -- conversion case (on both the way in and the way out).
1527 procedure Add_Simple_Call_By_Copy_Code (Force : Boolean);
1528 -- This is similar to the above, but is used in cases where we know
1529 -- that all that is needed is to simply create a temporary and copy
1530 -- the value in and out of the temporary. If Force is True, then the
1531 -- procedure may disregard legality considerations.
1533 -- ??? We need to do the copy for a bit-packed array because this is
1534 -- where the rewriting into a mask-and-shift sequence is done. But of
1535 -- course this may break the program if it expects bits to be really
1536 -- passed by reference. That's what we have done historically though.
1538 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id);
1539 -- Perform copy-back for actual parameter Act which denotes a validation
1540 -- variable.
1542 procedure Check_Fortran_Logical;
1543 -- A value of type Logical that is passed through a formal parameter
1544 -- must be normalized because .TRUE. usually does not have the same
1545 -- representation as True. We assume that .FALSE. = False = 0.
1546 -- What about functions that return a logical type ???
1548 function Is_Legal_Copy return Boolean;
1549 -- Check that an actual can be copied before generating the temporary
1550 -- to be used in the call. If the formal is of a by_reference type or
1551 -- is aliased, then the program is illegal (this can only happen in
1552 -- the presence of representation clauses that force a misalignment)
1553 -- If the formal is a by_reference parameter imposed by a DEC pragma,
1554 -- emit a warning that this might lead to unaligned arguments.
1556 function Make_Var (Actual : Node_Id) return Entity_Id;
1557 -- Returns an entity that refers to the given actual parameter, Actual
1558 -- (not including any type conversion). If Actual is an entity name,
1559 -- then this entity is returned unchanged, otherwise a renaming is
1560 -- created to provide an entity for the actual.
1562 procedure Reset_Packed_Prefix;
1563 -- The expansion of a packed array component reference is delayed in
1564 -- the context of a call. Now we need to complete the expansion, so we
1565 -- unmark the analyzed bits in all prefixes.
1567 function Requires_Atomic_Or_Volatile_Copy return Boolean;
1568 -- Returns whether a copy is required as per RM C.6(19) and gives a
1569 -- warning in this case.
1571 ---------------------------
1572 -- Add_Call_By_Copy_Code --
1573 ---------------------------
1575 procedure Add_Call_By_Copy_Code is
1576 Crep : Boolean;
1577 Expr : Node_Id;
1578 F_Typ : Entity_Id := Etype (Formal);
1579 Indic : Node_Id;
1580 Init : Node_Id;
1581 Temp : Entity_Id;
1582 V_Typ : Entity_Id;
1583 Var : Entity_Id;
1585 begin
1586 if not Is_Legal_Copy then
1587 return;
1588 end if;
1590 Temp := Make_Temporary (Loc, 'T', Actual);
1592 -- Handle formals whose type comes from the limited view
1594 if From_Limited_With (F_Typ)
1595 and then Has_Non_Limited_View (F_Typ)
1596 then
1597 F_Typ := Non_Limited_View (F_Typ);
1598 end if;
1600 -- Use formal type for temp, unless formal type is an unconstrained
1601 -- array, in which case we don't have to worry about bounds checks,
1602 -- and we use the actual type, since that has appropriate bounds.
1604 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1605 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1606 else
1607 Indic := New_Occurrence_Of (F_Typ, Loc);
1608 end if;
1610 -- The new code will be properly analyzed below and the setting of
1611 -- the Do_Range_Check flag recomputed so remove the obsolete one.
1613 Set_Do_Range_Check (Actual, False);
1615 if Nkind (Actual) = N_Type_Conversion then
1616 Set_Do_Range_Check (Expression (Actual), False);
1618 V_Typ := Etype (Expression (Actual));
1620 -- If the formal is an (in-)out parameter, capture the name
1621 -- of the variable in order to build the post-call assignment.
1623 Var := Make_Var (Expression (Actual));
1625 Crep := not Has_Compatible_Representation
1626 (Target_Typ => F_Typ,
1627 Operand_Typ => Etype (Expression (Actual)));
1629 else
1630 V_Typ := Etype (Actual);
1631 Var := Make_Var (Actual);
1632 Crep := False;
1633 end if;
1635 -- If the actual denotes a variable which captures the value of an
1636 -- object for validation purposes, we propagate the link with this
1637 -- object to the new variable made from the actual just above.
1639 if Ekind (Formal) /= E_In_Parameter
1640 and then Is_Validation_Variable_Reference (Actual)
1641 then
1642 declare
1643 Ref : constant Node_Id := Unqual_Conv (Actual);
1645 begin
1646 if Is_Entity_Name (Ref) then
1647 Set_Validated_Object (Var, Validated_Object (Entity (Ref)));
1649 else
1650 pragma Assert (False);
1651 null;
1652 end if;
1653 end;
1654 end if;
1656 -- Setup initialization for case of in out parameter, or an out
1657 -- parameter where the formal is an unconstrained array (in the
1658 -- latter case, we have to pass in an object with bounds).
1660 -- If this is an out parameter, the initial copy is wasteful, so as
1661 -- an optimization for the one-dimensional case we extract the
1662 -- bounds of the actual and build an uninitialized temporary of the
1663 -- right size.
1665 -- If the formal is an out parameter with discriminants, the
1666 -- discriminants must be captured even if the rest of the object
1667 -- is in principle uninitialized, because the discriminants may
1668 -- be read by the called subprogram.
1670 if Ekind (Formal) = E_In_Out_Parameter
1671 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1672 or else Has_Discriminants (F_Typ)
1673 then
1674 if Nkind (Actual) = N_Type_Conversion then
1675 if Conversion_OK (Actual) then
1676 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1677 else
1678 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1679 end if;
1681 elsif Ekind (Formal) = E_Out_Parameter
1682 and then Is_Array_Type (F_Typ)
1683 and then Number_Dimensions (F_Typ) = 1
1684 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1685 then
1686 -- Actual is a one-dimensional array or slice, and the type
1687 -- requires no initialization. Create a temporary of the
1688 -- right size, but do not copy actual into it (optimization).
1690 Init := Empty;
1691 Indic :=
1692 Make_Subtype_Indication (Loc,
1693 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1694 Constraint =>
1695 Make_Index_Or_Discriminant_Constraint (Loc,
1696 Constraints => New_List (
1697 Make_Range (Loc,
1698 Low_Bound =>
1699 Make_Attribute_Reference (Loc,
1700 Prefix => New_Occurrence_Of (Var, Loc),
1701 Attribute_Name => Name_First),
1702 High_Bound =>
1703 Make_Attribute_Reference (Loc,
1704 Prefix => New_Occurrence_Of (Var, Loc),
1705 Attribute_Name => Name_Last)))));
1707 else
1708 Init := New_Occurrence_Of (Var, Loc);
1709 end if;
1711 -- An initialization is created for packed conversions as
1712 -- actuals for out parameters to enable Make_Object_Declaration
1713 -- to determine the proper subtype for N_Node. Note that this
1714 -- is wasteful because the extra copying on the call side is
1715 -- not required for such out parameters. ???
1717 elsif Ekind (Formal) = E_Out_Parameter
1718 and then Nkind (Actual) = N_Type_Conversion
1719 and then (Is_Bit_Packed_Array (F_Typ)
1720 or else
1721 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1722 then
1723 if Conversion_OK (Actual) then
1724 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1725 else
1726 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1727 end if;
1729 elsif Ekind (Formal) = E_In_Parameter then
1731 -- Handle the case in which the actual is a type conversion
1733 if Nkind (Actual) = N_Type_Conversion then
1734 if Conversion_OK (Actual) then
1735 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1736 else
1737 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1738 end if;
1739 else
1740 Init := New_Occurrence_Of (Var, Loc);
1741 end if;
1743 -- Access types are passed in without checks, but if a copy-back is
1744 -- required for a null-excluding check on an in-out or out parameter,
1745 -- then the initial value is that of the actual.
1747 elsif Is_Access_Type (E_Formal)
1748 and then Can_Never_Be_Null (Etype (Actual))
1749 and then not Can_Never_Be_Null (E_Formal)
1750 then
1751 Init := New_Occurrence_Of (Var, Loc);
1753 -- View conversions when the formal type has the Default_Value aspect
1754 -- require passing in the value of the conversion's operand. The type
1755 -- of that operand also has Default_Value, as required by AI12-0074
1756 -- (RM 6.4.1(5.3/4)). The subtype denoted by the subtype_indication
1757 -- is changed to the base type of the formal subtype, to ensure that
1758 -- the actual's value can be assigned without a constraint check
1759 -- (note that no check is done on passing to an out parameter). Also
1760 -- note that the two types necessarily share the same ancestor type,
1761 -- as required by 6.4.1(5.2/4), so underlying base types will match.
1763 elsif Ekind (Formal) = E_Out_Parameter
1764 and then Is_Scalar_Type (Etype (F_Typ))
1765 and then Nkind (Actual) = N_Type_Conversion
1766 and then Present (Default_Aspect_Value (Etype (F_Typ)))
1767 then
1768 Indic := New_Occurrence_Of (Base_Type (F_Typ), Loc);
1769 Init := Convert_To
1770 (Base_Type (F_Typ), New_Occurrence_Of (Var, Loc));
1772 else
1773 Init := Empty;
1774 end if;
1776 N_Node :=
1777 Make_Object_Declaration (Loc,
1778 Defining_Identifier => Temp,
1779 Object_Definition => Indic,
1780 Expression => Init);
1781 Set_Assignment_OK (N_Node);
1782 Insert_Action (N, N_Node);
1784 -- Now, normally the deal here is that we use the defining
1785 -- identifier created by that object declaration. There is
1786 -- one exception to this. In the change of representation case
1787 -- the above declaration will end up looking like:
1789 -- temp : type := identifier;
1791 -- And in this case we might as well use the identifier directly
1792 -- and eliminate the temporary. Note that the analysis of the
1793 -- declaration was not a waste of time in that case, since it is
1794 -- what generated the necessary change of representation code. If
1795 -- the change of representation introduced additional code, as in
1796 -- a fixed-integer conversion, the expression is not an identifier
1797 -- and must be kept.
1799 if Crep
1800 and then Present (Expression (N_Node))
1801 and then Is_Entity_Name (Expression (N_Node))
1802 then
1803 Temp := Entity (Expression (N_Node));
1804 Rewrite (N_Node, Make_Null_Statement (Loc));
1805 end if;
1807 -- For IN parameter, all we do is to replace the actual
1809 if Ekind (Formal) = E_In_Parameter then
1810 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1811 Analyze (Actual);
1813 -- Processing for OUT or IN OUT parameter
1815 else
1816 -- Kill current value indications for the temporary variable we
1817 -- created, since we just passed it as an OUT parameter.
1819 Kill_Current_Values (Temp);
1820 Set_Is_Known_Valid (Temp, False);
1821 Set_Is_True_Constant (Temp, False);
1823 -- If type conversion, use reverse conversion on exit
1825 if Nkind (Actual) = N_Type_Conversion then
1826 if Conversion_OK (Actual) then
1827 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1828 else
1829 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1830 end if;
1831 else
1832 Expr := New_Occurrence_Of (Temp, Loc);
1833 end if;
1835 Rewrite (Actual, New_Occurrence_Of (Temp, Sloc (Actual)));
1836 Analyze (Actual);
1838 -- If the actual is a conversion of a packed reference, it may
1839 -- already have been expanded by Remove_Side_Effects, and the
1840 -- resulting variable is a temporary which does not designate
1841 -- the proper out-parameter, which may not be addressable. In
1842 -- that case, generate an assignment to the original expression
1843 -- (before expansion of the packed reference) so that the proper
1844 -- expansion of assignment to a packed component can take place.
1846 declare
1847 Obj : Node_Id;
1848 Lhs : Node_Id;
1850 begin
1851 if Is_Renaming_Of_Object (Var)
1852 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1853 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1854 = N_Indexed_Component
1855 and then
1856 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1857 then
1858 Obj := Renamed_Object (Var);
1859 Lhs :=
1860 Make_Selected_Component (Loc,
1861 Prefix =>
1862 New_Copy_Tree (Original_Node (Prefix (Obj))),
1863 Selector_Name => New_Copy (Selector_Name (Obj)));
1864 Reset_Analyzed_Flags (Lhs);
1866 else
1867 Lhs := New_Occurrence_Of (Var, Loc);
1868 end if;
1870 Set_Assignment_OK (Lhs);
1872 if Is_Access_Type (E_Formal)
1873 and then Is_Entity_Name (Lhs)
1874 and then
1875 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1876 and then not No_Dynamic_Accessibility_Checks_Enabled (Lhs)
1877 then
1878 -- Copyback target is an Ada 2012 stand-alone object of an
1879 -- anonymous access type.
1881 pragma Assert (Ada_Version >= Ada_2012);
1883 Apply_Accessibility_Check (Lhs, E_Formal, N);
1885 Append_To (Post_Call,
1886 Make_Assignment_Statement (Loc,
1887 Name => Lhs,
1888 Expression => Expr));
1890 -- We would like to somehow suppress generation of the
1891 -- extra_accessibility assignment generated by the expansion
1892 -- of the above assignment statement. It's not a correctness
1893 -- issue because the following assignment renders it dead,
1894 -- but generating back-to-back assignments to the same
1895 -- target is undesirable. ???
1897 Append_To (Post_Call,
1898 Make_Assignment_Statement (Loc,
1899 Name => New_Occurrence_Of (
1900 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1901 Expression => Make_Integer_Literal (Loc,
1902 Type_Access_Level (E_Formal))));
1904 else
1905 if Is_Access_Type (E_Formal)
1906 and then Can_Never_Be_Null (Etype (Actual))
1907 and then not Can_Never_Be_Null (E_Formal)
1908 then
1909 Append_To (Post_Call,
1910 Make_Raise_Constraint_Error (Loc,
1911 Condition =>
1912 Make_Op_Eq (Loc,
1913 Left_Opnd => New_Occurrence_Of (Temp, Loc),
1914 Right_Opnd => Make_Null (Loc)),
1915 Reason => CE_Access_Check_Failed));
1916 end if;
1918 Append_To (Post_Call,
1919 Make_Assignment_Statement (Loc,
1920 Name => Lhs,
1921 Expression => Expr));
1922 end if;
1924 -- Add a copy-back to reflect any potential changes in value
1925 -- back into the original object, if any.
1927 if Is_Validation_Variable_Reference (Lhs) then
1928 Add_Validation_Call_By_Copy_Code (Lhs);
1929 end if;
1930 end;
1931 end if;
1932 end Add_Call_By_Copy_Code;
1934 ----------------------------------
1935 -- Add_Simple_Call_By_Copy_Code --
1936 ----------------------------------
1938 procedure Add_Simple_Call_By_Copy_Code (Force : Boolean) is
1939 Decl : Node_Id;
1940 F_Typ : Entity_Id := Etype (Formal);
1941 Incod : Node_Id;
1942 Indic : Node_Id;
1943 Lhs : Node_Id;
1944 Outcod : Node_Id;
1945 Rhs : Node_Id;
1946 Temp : Entity_Id;
1948 begin
1949 -- Unless forced not to, check the legality of the copy operation
1951 if not Force and then not Is_Legal_Copy then
1952 return;
1953 end if;
1955 -- Handle formals whose type comes from the limited view
1957 if From_Limited_With (F_Typ)
1958 and then Has_Non_Limited_View (F_Typ)
1959 then
1960 F_Typ := Non_Limited_View (F_Typ);
1961 end if;
1963 -- Use formal type for temp, unless formal type is an unconstrained
1964 -- array, in which case we don't have to worry about bounds checks,
1965 -- and we use the actual type, since that has appropriate bounds.
1967 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1968 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1969 else
1970 Indic := New_Occurrence_Of (F_Typ, Loc);
1971 end if;
1973 -- Prepare to generate code
1975 Reset_Packed_Prefix;
1977 Temp := Make_Temporary (Loc, 'T', Actual);
1978 Incod := Relocate_Node (Actual);
1979 Outcod := New_Copy_Tree (Incod);
1981 -- Generate declaration of temporary variable, initializing it
1982 -- with the input parameter unless we have an OUT formal or
1983 -- this is an initialization call.
1985 -- If the formal is an out parameter with discriminants, the
1986 -- discriminants must be captured even if the rest of the object
1987 -- is in principle uninitialized, because the discriminants may
1988 -- be read by the called subprogram.
1990 if Ekind (Formal) = E_Out_Parameter then
1991 Incod := Empty;
1993 if Has_Discriminants (F_Typ) then
1994 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1995 end if;
1997 elsif Inside_Init_Proc then
1999 -- Skip using the actual as the expression in Decl if we are in
2000 -- an init proc and it is not a component which depends on a
2001 -- discriminant, because, in this case, we need to use the actual
2002 -- type of the component instead.
2004 if Nkind (Actual) /= N_Selected_Component
2005 or else
2006 not Has_Discriminant_Dependent_Constraint
2007 (Entity (Selector_Name (Actual)))
2008 then
2009 Incod := Empty;
2011 -- Otherwise, keep the component so we can generate the proper
2012 -- actual subtype - since the subtype depends on enclosing
2013 -- discriminants.
2015 else
2016 null;
2017 end if;
2018 end if;
2020 Decl :=
2021 Make_Object_Declaration (Loc,
2022 Defining_Identifier => Temp,
2023 Object_Definition => Indic,
2024 Expression => Incod);
2026 if Inside_Init_Proc
2027 and then No (Incod)
2028 then
2029 -- If the call is to initialize a component of a composite type,
2030 -- and the component does not depend on discriminants, use the
2031 -- actual type of the component. This is required in case the
2032 -- component is constrained, because in general the formal of the
2033 -- initialization procedure will be unconstrained. Note that if
2034 -- the component being initialized is constrained by an enclosing
2035 -- discriminant, the presence of the initialization in the
2036 -- declaration will generate an expression for the actual subtype.
2038 Set_No_Initialization (Decl);
2039 Set_Object_Definition (Decl,
2040 New_Occurrence_Of (Etype (Actual), Loc));
2041 end if;
2043 Insert_Action (N, Decl);
2045 -- The actual is simply a reference to the temporary
2047 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
2049 -- Generate copy out if OUT or IN OUT parameter
2051 if Ekind (Formal) /= E_In_Parameter then
2052 Lhs := Outcod;
2053 Rhs := New_Occurrence_Of (Temp, Loc);
2054 Set_Is_True_Constant (Temp, False);
2056 -- Deal with conversion
2058 if Nkind (Lhs) = N_Type_Conversion then
2059 Lhs := Expression (Lhs);
2060 Rhs := Convert_To (Etype (Actual), Rhs);
2061 end if;
2063 Append_To (Post_Call,
2064 Make_Assignment_Statement (Loc,
2065 Name => Lhs,
2066 Expression => Rhs));
2067 Set_Assignment_OK (Name (Last (Post_Call)));
2068 end if;
2069 end Add_Simple_Call_By_Copy_Code;
2071 --------------------------------------
2072 -- Add_Validation_Call_By_Copy_Code --
2073 --------------------------------------
2075 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id) is
2076 Var : constant Node_Id := Unqual_Conv (Act);
2078 Expr : Node_Id;
2079 Obj : Node_Id;
2080 Obj_Typ : Entity_Id;
2081 Var_Id : Entity_Id;
2083 begin
2084 -- Generate range check if required
2086 if Do_Range_Check (Actual) then
2087 Generate_Range_Check (Actual, E_Formal, CE_Range_Check_Failed);
2088 end if;
2090 -- If there is a type conversion in the actual, it will be reinstated
2091 -- below, the new instance will be properly analyzed and the setting
2092 -- of the Do_Range_Check flag recomputed so remove the obsolete one.
2094 if Nkind (Actual) = N_Type_Conversion then
2095 Set_Do_Range_Check (Expression (Actual), False);
2096 end if;
2098 -- Copy the value of the validation variable back into the object
2099 -- being validated.
2101 if Is_Entity_Name (Var) then
2102 Var_Id := Entity (Var);
2103 Obj := Validated_Object (Var_Id);
2104 Obj_Typ := Etype (Obj);
2106 Expr := New_Occurrence_Of (Var_Id, Loc);
2108 -- A type conversion is needed when the validation variable and
2109 -- the validated object carry different types. This case occurs
2110 -- when the actual is qualified in some fashion.
2112 -- Common:
2113 -- subtype Int is Integer range ...;
2114 -- procedure Call (Val : in out Integer);
2116 -- Original:
2117 -- Object : Int;
2118 -- Call (Integer (Object));
2120 -- Expanded:
2121 -- Object : Int;
2122 -- Var : Integer := Object; -- conversion to base type
2123 -- if not Var'Valid then -- validity check
2124 -- Call (Var); -- modify Var
2125 -- Object := Int (Var); -- conversion to subtype
2127 if Etype (Var_Id) /= Obj_Typ then
2128 Expr :=
2129 Make_Type_Conversion (Loc,
2130 Subtype_Mark => New_Occurrence_Of (Obj_Typ, Loc),
2131 Expression => Expr);
2132 end if;
2134 -- Generate:
2135 -- Object := Var;
2136 -- <or>
2137 -- Object := Object_Type (Var);
2139 Append_To (Post_Call,
2140 Make_Assignment_Statement (Loc,
2141 Name => Obj,
2142 Expression => Expr));
2144 -- If the flow reaches this point, then this routine was invoked with
2145 -- an actual which does not denote a validation variable.
2147 else
2148 pragma Assert (False);
2149 null;
2150 end if;
2151 end Add_Validation_Call_By_Copy_Code;
2153 ---------------------------
2154 -- Check_Fortran_Logical --
2155 ---------------------------
2157 procedure Check_Fortran_Logical is
2158 Logical : constant Entity_Id := Etype (Formal);
2159 Var : Entity_Id;
2161 -- Note: this is very incomplete, e.g. it does not handle arrays
2162 -- of logical values. This is really not the right approach at all???)
2164 begin
2165 if Convention (Subp) = Convention_Fortran
2166 and then Root_Type (Etype (Formal)) = Standard_Boolean
2167 and then Ekind (Formal) /= E_In_Parameter
2168 then
2169 Var := Make_Var (Actual);
2170 Append_To (Post_Call,
2171 Make_Assignment_Statement (Loc,
2172 Name => New_Occurrence_Of (Var, Loc),
2173 Expression =>
2174 Unchecked_Convert_To (
2175 Logical,
2176 Make_Op_Ne (Loc,
2177 Left_Opnd => New_Occurrence_Of (Var, Loc),
2178 Right_Opnd =>
2179 Unchecked_Convert_To (
2180 Logical,
2181 New_Occurrence_Of (Standard_False, Loc))))));
2182 end if;
2183 end Check_Fortran_Logical;
2185 -------------------
2186 -- Is_Legal_Copy --
2187 -------------------
2189 function Is_Legal_Copy return Boolean is
2190 begin
2191 -- An attempt to copy a value of such a type can only occur if
2192 -- representation clauses give the actual a misaligned address.
2194 if Is_By_Reference_Type (Etype (Formal))
2195 or else Is_Aliased (Formal)
2196 or else (Mechanism (Formal) = By_Reference
2197 and then not Has_Foreign_Convention (Subp))
2198 then
2200 -- The actual may in fact be properly aligned but there is not
2201 -- enough front-end information to determine this. In that case
2202 -- gigi will emit an error or a warning if a copy is not legal,
2203 -- or generate the proper code.
2205 return False;
2207 -- For users of Starlet, we assume that the specification of by-
2208 -- reference mechanism is mandatory. This may lead to unaligned
2209 -- objects but at least for DEC legacy code it is known to work.
2210 -- The warning will alert users of this code that a problem may
2211 -- be lurking.
2213 elsif Mechanism (Formal) = By_Reference
2214 and then Ekind (Scope (Formal)) = E_Procedure
2215 and then Is_Valued_Procedure (Scope (Formal))
2216 then
2217 Error_Msg_N
2218 ("by_reference actual may be misaligned??", Actual);
2219 return False;
2221 else
2222 return True;
2223 end if;
2224 end Is_Legal_Copy;
2226 --------------
2227 -- Make_Var --
2228 --------------
2230 function Make_Var (Actual : Node_Id) return Entity_Id is
2231 Var : Entity_Id;
2233 begin
2234 if Is_Entity_Name (Actual) then
2235 return Entity (Actual);
2237 else
2238 Var := Make_Temporary (Loc, 'T', Actual);
2240 N_Node :=
2241 Make_Object_Renaming_Declaration (Loc,
2242 Defining_Identifier => Var,
2243 Subtype_Mark =>
2244 New_Occurrence_Of (Etype (Actual), Loc),
2245 Name => Relocate_Node (Actual));
2247 Insert_Action (N, N_Node);
2248 return Var;
2249 end if;
2250 end Make_Var;
2252 -------------------------
2253 -- Reset_Packed_Prefix --
2254 -------------------------
2256 procedure Reset_Packed_Prefix is
2257 Pfx : Node_Id := Actual;
2258 begin
2259 loop
2260 Set_Analyzed (Pfx, False);
2261 exit when
2262 Nkind (Pfx) not in N_Selected_Component | N_Indexed_Component;
2263 Pfx := Prefix (Pfx);
2264 end loop;
2265 end Reset_Packed_Prefix;
2267 ----------------------------------------
2268 -- Requires_Atomic_Or_Volatile_Copy --
2269 ----------------------------------------
2271 function Requires_Atomic_Or_Volatile_Copy return Boolean is
2272 begin
2273 -- If the formal is already passed by copy, no need to do anything
2275 if Is_By_Copy_Type (E_Formal) then
2276 return False;
2277 end if;
2279 -- There is no requirement inside initialization procedures and this
2280 -- would generate copies for atomic or volatile composite components.
2282 if Inside_Init_Proc then
2283 return False;
2284 end if;
2286 -- Check for atomicity mismatch
2288 if Is_Atomic_Object (Actual) and then not Is_Atomic (E_Formal)
2289 then
2290 if Comes_From_Source (N) then
2291 Error_Msg_N
2292 ("??atomic actual passed by copy (RM C.6(19))", Actual);
2293 end if;
2294 return True;
2295 end if;
2297 -- Check for volatility mismatch
2299 if Is_Volatile_Object_Ref (Actual) and then not Is_Volatile (E_Formal)
2300 then
2301 if Comes_From_Source (N) then
2302 Error_Msg_N
2303 ("??volatile actual passed by copy (RM C.6(19))", Actual);
2304 end if;
2305 return True;
2306 end if;
2308 return False;
2309 end Requires_Atomic_Or_Volatile_Copy;
2311 -- Start of processing for Expand_Actuals
2313 begin
2314 Post_Call := New_List;
2316 Formal := First_Formal (Subp);
2317 Actual := First_Actual (N);
2318 while Present (Formal) loop
2319 E_Formal := Etype (Formal);
2320 E_Actual := Etype (Actual);
2322 -- Handle formals whose type comes from the limited view
2324 if From_Limited_With (E_Formal)
2325 and then Has_Non_Limited_View (E_Formal)
2326 then
2327 E_Formal := Non_Limited_View (E_Formal);
2328 end if;
2330 if Is_Scalar_Type (E_Formal)
2331 or else Nkind (Actual) = N_Slice
2332 then
2333 Check_Fortran_Logical;
2335 -- RM 6.4.1 (11)
2337 elsif Ekind (Formal) /= E_Out_Parameter then
2339 -- The unusual case of the current instance of a protected type
2340 -- requires special handling. This can only occur in the context
2341 -- of a call within the body of a protected operation.
2343 if Is_Entity_Name (Actual)
2344 and then Ekind (Entity (Actual)) = E_Protected_Type
2345 and then In_Open_Scopes (Entity (Actual))
2346 then
2347 if Scope (Subp) /= Entity (Actual) then
2348 Error_Msg_N
2349 ("operation outside protected type may not "
2350 & "call back its protected operations??", Actual);
2351 end if;
2353 Rewrite (Actual,
2354 Expand_Protected_Object_Reference (N, Entity (Actual)));
2355 end if;
2357 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
2358 -- build-in-place function, then a temporary return object needs
2359 -- to be created and access to it must be passed to the function
2360 -- (and ensure that we have an activation chain defined for tasks
2361 -- and a Master variable).
2363 -- Currently we limit such functions to those with inherently
2364 -- limited result subtypes, but eventually we plan to expand the
2365 -- functions that are treated as build-in-place to include other
2366 -- composite result types.
2368 -- But do not do it here for intrinsic subprograms since this will
2369 -- be done properly after the subprogram is expanded.
2371 if Is_Intrinsic_Subprogram (Subp) then
2372 null;
2374 elsif Is_Build_In_Place_Function_Call (Actual) then
2375 if Might_Have_Tasks (Etype (Actual)) then
2376 Build_Activation_Chain_Entity (N);
2377 Build_Master_Entity (Etype (Actual));
2378 end if;
2380 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
2382 -- Ada 2005 (AI-318-02): Specialization of the previous case for
2383 -- actuals containing build-in-place function calls whose returned
2384 -- object covers interface types.
2386 elsif Present (Unqual_BIP_Iface_Function_Call (Actual)) then
2387 Build_Activation_Chain_Entity (N);
2388 Build_Master_Entity (Etype (Actual));
2389 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Actual);
2390 end if;
2392 Apply_Constraint_Check (Actual, E_Formal);
2394 -- Out parameter case. No constraint checks on access type
2395 -- RM 6.4.1 (13), but on return a null-excluding check may be
2396 -- required (see below).
2398 elsif Is_Access_Type (E_Formal) then
2399 null;
2401 -- RM 6.4.1 (14)
2403 elsif Has_Discriminants (Base_Type (E_Formal))
2404 or else Has_Non_Null_Base_Init_Proc (E_Formal)
2405 then
2406 Apply_Constraint_Check (Actual, E_Formal);
2408 -- RM 6.4.1 (15)
2410 else
2411 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
2412 end if;
2414 -- Processing for IN-OUT and OUT parameters
2416 if Ekind (Formal) /= E_In_Parameter then
2418 -- For type conversions of arrays, apply length/range checks
2420 if Is_Array_Type (E_Formal)
2421 and then Nkind (Actual) = N_Type_Conversion
2422 then
2423 if Is_Constrained (E_Formal) then
2424 Apply_Length_Check (Expression (Actual), E_Formal);
2425 else
2426 Apply_Range_Check (Expression (Actual), E_Formal);
2427 end if;
2428 end if;
2430 -- If argument is a type conversion for a type that is passed by
2431 -- copy, then we must pass the parameter by copy.
2433 if Nkind (Actual) = N_Type_Conversion
2434 and then
2435 (Is_Elementary_Type (E_Formal)
2436 or else Is_Bit_Packed_Array (Etype (Formal))
2437 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
2439 -- Also pass by copy if change of representation
2441 or else not Has_Compatible_Representation
2442 (Target_Typ => Etype (Formal),
2443 Operand_Typ => Etype (Expression (Actual))))
2444 then
2445 Add_Call_By_Copy_Code;
2447 -- References to components of bit-packed arrays are expanded
2448 -- at this point, rather than at the point of analysis of the
2449 -- actuals, to handle the expansion of the assignment to
2450 -- [in] out parameters.
2452 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2453 Add_Simple_Call_By_Copy_Code (Force => True);
2455 -- If a nonscalar actual is possibly bit-aligned, we need a copy
2456 -- because the back-end cannot cope with such objects. In other
2457 -- cases where alignment forces a copy, the back-end generates
2458 -- it properly. It should not be generated unconditionally in the
2459 -- front-end because it does not know precisely the alignment
2460 -- requirements of the target, and makes too conservative an
2461 -- estimate, leading to superfluous copies or spurious errors
2462 -- on by-reference parameters.
2464 elsif Nkind (Actual) = N_Selected_Component
2465 and then
2466 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
2467 and then not Represented_As_Scalar (Etype (Formal))
2468 then
2469 Add_Simple_Call_By_Copy_Code (Force => False);
2471 -- References to slices of bit-packed arrays are expanded
2473 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2474 Add_Call_By_Copy_Code;
2476 -- References to possibly unaligned slices of arrays are expanded
2478 elsif Is_Possibly_Unaligned_Slice (Actual) then
2479 Add_Call_By_Copy_Code;
2481 -- Deal with access types where the actual subtype and the
2482 -- formal subtype are not the same, requiring a check.
2484 -- It is necessary to exclude tagged types because of "downward
2485 -- conversion" errors, but null-excluding checks on return may be
2486 -- required.
2488 elsif Is_Access_Type (E_Formal)
2489 and then not Is_Tagged_Type (Designated_Type (E_Formal))
2490 and then (not Same_Type (E_Formal, E_Actual)
2491 or else (Can_Never_Be_Null (E_Actual)
2492 and then not Can_Never_Be_Null (E_Formal)))
2493 then
2494 Add_Call_By_Copy_Code;
2496 -- We may need to force a copy because of atomicity or volatility
2497 -- considerations.
2499 elsif Requires_Atomic_Or_Volatile_Copy then
2500 Add_Call_By_Copy_Code;
2502 -- Add call-by-copy code for the case of scalar out parameters
2503 -- when it is not known at compile time that the subtype of the
2504 -- formal is a subrange of the subtype of the actual (or vice
2505 -- versa for in out parameters), in order to get range checks
2506 -- on such actuals. (Maybe this case should be handled earlier
2507 -- in the if statement???)
2509 elsif Is_Scalar_Type (E_Formal)
2510 and then
2511 (not In_Subrange_Of (E_Formal, E_Actual)
2512 or else
2513 (Ekind (Formal) = E_In_Out_Parameter
2514 and then not In_Subrange_Of (E_Actual, E_Formal)))
2515 then
2516 Add_Call_By_Copy_Code;
2518 -- The actual denotes a variable which captures the value of an
2519 -- object for validation purposes. Add a copy-back to reflect any
2520 -- potential changes in value back into the original object.
2522 -- Var : ... := Object;
2523 -- if not Var'Valid then -- validity check
2524 -- Call (Var); -- modify var
2525 -- Object := Var; -- update Object
2527 elsif Is_Validation_Variable_Reference (Actual) then
2528 Add_Validation_Call_By_Copy_Code (Actual);
2529 end if;
2531 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2532 -- by-reference parameters on exit from the call. If the actual
2533 -- is a derived type and the operation is inherited, the body
2534 -- of the operation will not contain a call to the predicate
2535 -- function, so it must be done explicitly after the call. Ditto
2536 -- if the actual is an entity of a predicated subtype.
2538 -- The rule refers to by-reference types, but a check is needed
2539 -- for by-copy types as well. That check is subsumed by the rule
2540 -- for subtype conversion on assignment, but we can generate the
2541 -- required check now.
2543 -- Note also that Subp may be either a subprogram entity for
2544 -- direct calls, or a type entity for indirect calls, which must
2545 -- be handled separately because the name does not denote an
2546 -- overloadable entity.
2548 By_Ref_Predicate_Check : declare
2549 Aund : constant Entity_Id := Underlying_Type (E_Actual);
2550 Atyp : Entity_Id;
2552 begin
2553 if No (Aund) then
2554 Atyp := E_Actual;
2555 else
2556 Atyp := Aund;
2557 end if;
2559 if Predicate_Enabled (Atyp)
2561 -- Skip predicate checks for special cases
2563 and then Predicate_Tests_On_Arguments (Subp)
2564 then
2565 Append_To (Post_Call,
2566 Make_Predicate_Check (Atyp, Actual));
2567 end if;
2568 end By_Ref_Predicate_Check;
2570 -- Processing for IN parameters
2572 else
2573 -- Generate range check if required
2575 if Do_Range_Check (Actual) then
2576 Generate_Range_Check (Actual, E_Formal, CE_Range_Check_Failed);
2577 end if;
2579 -- For IN parameters in the bit-packed array case, we expand an
2580 -- indexed component (the circuit in Exp_Ch4 deliberately left
2581 -- indexed components appearing as actuals untouched, so that
2582 -- the special processing above for the OUT and IN OUT cases
2583 -- could be performed. We could make the test in Exp_Ch4 more
2584 -- complex and have it detect the parameter mode, but it is
2585 -- easier simply to handle all cases here.)
2587 if Nkind (Actual) = N_Indexed_Component
2588 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2589 then
2590 Reset_Packed_Prefix;
2591 Expand_Packed_Element_Reference (Actual);
2593 -- If we have a reference to a bit-packed array, we copy it, since
2594 -- the actual must be byte aligned.
2596 -- Is this really necessary in all cases???
2598 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2599 Add_Simple_Call_By_Copy_Code (Force => True);
2601 -- If we have a C++ constructor call, we need to create the object
2603 elsif Is_CPP_Constructor_Call (Actual) then
2604 Add_Simple_Call_By_Copy_Code (Force => True);
2606 -- If a nonscalar actual is possibly unaligned, we need a copy
2608 elsif Is_Possibly_Unaligned_Object (Actual)
2609 and then not Represented_As_Scalar (Etype (Formal))
2610 then
2611 Add_Simple_Call_By_Copy_Code (Force => False);
2613 -- Similarly, we have to expand slices of packed arrays here
2614 -- because the result must be byte aligned.
2616 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2617 Add_Call_By_Copy_Code;
2619 -- Only processing remaining is to pass by copy if this is a
2620 -- reference to a possibly unaligned slice, since the caller
2621 -- expects an appropriately aligned argument.
2623 elsif Is_Possibly_Unaligned_Slice (Actual) then
2624 Add_Call_By_Copy_Code;
2626 -- We may need to force a copy because of atomicity or volatility
2627 -- considerations.
2629 elsif Requires_Atomic_Or_Volatile_Copy then
2630 Add_Call_By_Copy_Code;
2632 -- An unusual case: a current instance of an enclosing task can be
2633 -- an actual, and must be replaced by a reference to self.
2635 elsif Is_Entity_Name (Actual)
2636 and then Is_Task_Type (Entity (Actual))
2637 then
2638 if In_Open_Scopes (Entity (Actual)) then
2639 Rewrite (Actual,
2640 (Make_Function_Call (Loc,
2641 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2642 Analyze (Actual);
2644 -- A task type cannot otherwise appear as an actual
2646 else
2647 raise Program_Error;
2648 end if;
2649 end if;
2650 end if;
2652 -- Type-invariant checks for in-out and out parameters, as well as
2653 -- for in parameters of procedures (AI05-0289 and AI12-0044).
2655 if Ekind (Formal) /= E_In_Parameter
2656 or else Ekind (Subp) = E_Procedure
2657 then
2658 Caller_Side_Invariant_Checks : declare
2660 function Is_Public_Subp return Boolean;
2661 -- Check whether the subprogram being called is a visible
2662 -- operation of the type of the actual. Used to determine
2663 -- whether an invariant check must be generated on the
2664 -- caller side.
2666 ---------------------
2667 -- Is_Public_Subp --
2668 ---------------------
2670 function Is_Public_Subp return Boolean is
2671 Pack : constant Entity_Id := Scope (Subp);
2672 Subp_Decl : Node_Id;
2674 begin
2675 if not Is_Subprogram (Subp) then
2676 return False;
2678 -- The operation may be inherited, or a primitive of the
2679 -- root type.
2681 elsif
2682 Nkind (Parent (Subp)) in N_Private_Extension_Declaration
2683 | N_Full_Type_Declaration
2684 then
2685 Subp_Decl := Parent (Subp);
2687 else
2688 Subp_Decl := Unit_Declaration_Node (Subp);
2689 end if;
2691 return Ekind (Pack) = E_Package
2692 and then
2693 List_Containing (Subp_Decl) =
2694 Visible_Declarations
2695 (Specification (Unit_Declaration_Node (Pack)));
2696 end Is_Public_Subp;
2698 -- Start of processing for Caller_Side_Invariant_Checks
2700 begin
2701 -- We generate caller-side invariant checks in two cases:
2703 -- a) when calling an inherited operation, where there is an
2704 -- implicit view conversion of the actual to the parent type.
2706 -- b) When the conversion is explicit
2708 -- We treat these cases separately because the required
2709 -- conversion for a) is added later when expanding the call.
2711 if Has_Invariants (Etype (Actual))
2712 and then
2713 Nkind (Parent (Etype (Actual)))
2714 = N_Private_Extension_Declaration
2715 then
2716 if Comes_From_Source (N) and then Is_Public_Subp then
2717 Append_To (Post_Call, Make_Invariant_Call (Actual));
2718 end if;
2720 elsif Nkind (Actual) = N_Type_Conversion
2721 and then Has_Invariants (Etype (Expression (Actual)))
2722 then
2723 if Comes_From_Source (N) and then Is_Public_Subp then
2724 Append_To
2725 (Post_Call, Make_Invariant_Call (Expression (Actual)));
2726 end if;
2727 end if;
2728 end Caller_Side_Invariant_Checks;
2729 end if;
2731 Next_Formal (Formal);
2732 Next_Actual (Actual);
2733 end loop;
2734 end Expand_Actuals;
2736 -----------------
2737 -- Expand_Call --
2738 -----------------
2740 procedure Expand_Call (N : Node_Id) is
2741 Post_Call : List_Id;
2743 -- If this is an indirect call through an Access_To_Subprogram
2744 -- with contract specifications, it is rewritten as a call to
2745 -- the corresponding Access_Subprogram_Wrapper with the same
2746 -- actuals, whose body contains a naked indirect call (which
2747 -- itself must not be rewritten, to prevent infinite recursion).
2749 Must_Rewrite_Indirect_Call : constant Boolean :=
2750 Ada_Version >= Ada_2022
2751 and then Nkind (Name (N)) = N_Explicit_Dereference
2752 and then Ekind (Etype (Name (N))) = E_Subprogram_Type
2753 and then Present
2754 (Access_Subprogram_Wrapper (Etype (Name (N))));
2756 begin
2757 pragma Assert (Nkind (N) in N_Entry_Call_Statement
2758 | N_Function_Call
2759 | N_Procedure_Call_Statement);
2761 -- Check that this is not the call in the body of the access
2762 -- subprogram wrapper or the postconditions wrapper.
2764 if Must_Rewrite_Indirect_Call
2765 and then (not Is_Overloadable (Current_Scope)
2766 or else not (Is_Access_Subprogram_Wrapper (Current_Scope)
2767 or else
2768 (Chars (Current_Scope) = Name_uWrapped_Statements
2769 and then Is_Access_Subprogram_Wrapper
2770 (Scope (Current_Scope)))))
2771 then
2772 declare
2773 Loc : constant Source_Ptr := Sloc (N);
2774 Wrapper : constant Entity_Id :=
2775 Access_Subprogram_Wrapper (Etype (Name (N)));
2776 Ptr : constant Node_Id := Prefix (Name (N));
2777 Ptr_Type : constant Entity_Id := Etype (Ptr);
2778 Typ : constant Entity_Id := Etype (N);
2780 New_N : Node_Id;
2781 Parms : List_Id := Parameter_Associations (N);
2782 Ptr_Act : Node_Id;
2784 begin
2785 -- The last actual in the call is the pointer itself.
2786 -- If the aspect is inherited, convert the pointer to the
2787 -- parent type that specifies the contract.
2788 -- If the original access_to_subprogram has defaults for
2789 -- in_parameters, the call may include named associations, so
2790 -- we create one for the pointer as well.
2792 if Is_Derived_Type (Ptr_Type)
2793 and then Ptr_Type /= Etype (Last_Formal (Wrapper))
2794 then
2795 Ptr_Act :=
2796 Make_Type_Conversion (Loc,
2797 New_Occurrence_Of
2798 (Etype (Last_Formal (Wrapper)), Loc), Ptr);
2800 else
2801 Ptr_Act := Ptr;
2802 end if;
2804 -- Handle parameterless subprogram.
2806 if No (Parms) then
2807 Parms := New_List;
2808 end if;
2810 Append
2811 (Make_Parameter_Association (Loc,
2812 Selector_Name => Make_Identifier (Loc,
2813 Chars (Last_Formal (Wrapper))),
2814 Explicit_Actual_Parameter => Ptr_Act),
2815 Parms);
2817 if Nkind (N) = N_Procedure_Call_Statement then
2818 New_N := Make_Procedure_Call_Statement (Loc,
2819 Name => New_Occurrence_Of (Wrapper, Loc),
2820 Parameter_Associations => Parms);
2821 else
2822 New_N := Make_Function_Call (Loc,
2823 Name => New_Occurrence_Of (Wrapper, Loc),
2824 Parameter_Associations => Parms);
2825 end if;
2827 Rewrite (N, New_N);
2828 Analyze_And_Resolve (N, Typ);
2829 end;
2831 else
2832 Expand_Call_Helper (N, Post_Call);
2833 Insert_Post_Call_Actions (N, Post_Call);
2834 end if;
2835 end Expand_Call;
2837 ------------------------
2838 -- Expand_Call_Helper --
2839 ------------------------
2841 -- This procedure handles expansion of function calls and procedure call
2842 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2843 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2845 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2846 -- Provide values of actuals for all formals in Extra_Formals list
2847 -- Replace "call" to enumeration literal function by literal itself
2848 -- Rewrite call to predefined operator as operator
2849 -- Replace actuals to in-out parameters that are numeric conversions,
2850 -- with explicit assignment to temporaries before and after the call.
2852 -- Note that the list of actuals has been filled with default expressions
2853 -- during semantic analysis of the call. Only the extra actuals required
2854 -- for the 'Constrained attribute and for accessibility checks are added
2855 -- at this point.
2857 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is
2858 Loc : constant Source_Ptr := Sloc (N);
2859 Call_Node : Node_Id := N;
2860 Extra_Actuals : List_Id := No_List;
2861 Prev : Node_Id := Empty;
2863 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2864 -- Adds one entry to the end of the actual parameter list. Used for
2865 -- default parameters and for extra actuals (for Extra_Formals). The
2866 -- argument is an N_Parameter_Association node.
2868 procedure Add_Cond_Expression_Extra_Actual (Formal : Entity_Id);
2869 -- Adds extra accessibility actuals in the case of a conditional
2870 -- expression corresponding to Formal.
2872 -- Note: Conditional expressions used as actuals for anonymous access
2873 -- formals complicate the process of propagating extra accessibility
2874 -- actuals and must be handled in a recursive fashion since they can
2875 -- be embedded within each other.
2877 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2878 -- Adds an extra actual to the list of extra actuals. Expr is the
2879 -- expression for the value of the actual, EF is the entity for the
2880 -- extra formal.
2882 procedure Add_View_Conversion_Invariants
2883 (Formal : Entity_Id;
2884 Actual : Node_Id);
2885 -- Adds invariant checks for every intermediate type between the range
2886 -- of a view converted argument to its ancestor (from parent to child).
2888 function Can_Fold_Predicate_Call (P : Entity_Id) return Boolean;
2889 -- Try to constant-fold a predicate check, which often enough is a
2890 -- simple arithmetic expression that can be computed statically if
2891 -- its argument is static. This cleans up the output of CCG, even
2892 -- though useless predicate checks will be generally removed by
2893 -- back-end optimizations.
2895 procedure Check_Subprogram_Variant;
2896 -- Emit a call to the internally generated procedure with checks for
2897 -- aspect Subprogram_Variant, if present and enabled.
2899 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2900 -- Within an instance, a type derived from an untagged formal derived
2901 -- type inherits from the original parent, not from the actual. The
2902 -- current derivation mechanism has the derived type inherit from the
2903 -- actual, which is only correct outside of the instance. If the
2904 -- subprogram is inherited, we test for this particular case through a
2905 -- convoluted tree traversal before setting the proper subprogram to be
2906 -- called.
2908 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2909 -- Return true if E comes from an instance that is not yet frozen
2911 function Is_Class_Wide_Interface_Type (E : Entity_Id) return Boolean;
2912 -- Return True when E is a class-wide interface type or an access to
2913 -- a class-wide interface type.
2915 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2916 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2918 function New_Value (From : Node_Id) return Node_Id;
2919 -- From is the original Expression. New_Value is equivalent to a call
2920 -- to Duplicate_Subexpr with an explicit dereference when From is an
2921 -- access parameter.
2923 --------------------------
2924 -- Add_Actual_Parameter --
2925 --------------------------
2927 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2928 Actual_Expr : constant Node_Id :=
2929 Explicit_Actual_Parameter (Insert_Param);
2931 begin
2932 -- Case of insertion is first named actual
2934 if No (Prev) or else
2935 Nkind (Parent (Prev)) /= N_Parameter_Association
2936 then
2937 Set_Next_Named_Actual
2938 (Insert_Param, First_Named_Actual (Call_Node));
2939 Set_First_Named_Actual (Call_Node, Actual_Expr);
2941 if No (Prev) then
2942 if No (Parameter_Associations (Call_Node)) then
2943 Set_Parameter_Associations (Call_Node, New_List);
2944 end if;
2946 Append (Insert_Param, Parameter_Associations (Call_Node));
2948 else
2949 Insert_After (Prev, Insert_Param);
2950 end if;
2952 -- Case of insertion is not first named actual
2954 else
2955 Set_Next_Named_Actual
2956 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2957 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2958 Append (Insert_Param, Parameter_Associations (Call_Node));
2959 end if;
2961 Prev := Actual_Expr;
2962 end Add_Actual_Parameter;
2964 --------------------------------------
2965 -- Add_Cond_Expression_Extra_Actual --
2966 --------------------------------------
2968 procedure Add_Cond_Expression_Extra_Actual
2969 (Formal : Entity_Id)
2971 Decl : Node_Id;
2972 Lvl : Entity_Id;
2974 procedure Insert_Level_Assign (Branch : Node_Id);
2975 -- Recursively add assignment of the level temporary on each branch
2976 -- while moving through nested conditional expressions.
2978 -------------------------
2979 -- Insert_Level_Assign --
2980 -------------------------
2982 procedure Insert_Level_Assign (Branch : Node_Id) is
2984 procedure Expand_Branch (Res_Assn : Node_Id);
2985 -- Perform expansion or iterate further within nested
2986 -- conditionals given the object declaration or assignment to
2987 -- result object created during expansion which represents a
2988 -- branch of the conditional expression.
2990 -------------------
2991 -- Expand_Branch --
2992 -------------------
2994 procedure Expand_Branch (Res_Assn : Node_Id) is
2995 begin
2996 pragma Assert (Nkind (Res_Assn) in
2997 N_Assignment_Statement |
2998 N_Object_Declaration);
3000 -- There are more nested conditional expressions so we must go
3001 -- deeper.
3003 if Nkind (Expression (Res_Assn)) = N_Expression_With_Actions
3004 and then
3005 Nkind (Original_Node (Expression (Res_Assn)))
3006 in N_Case_Expression | N_If_Expression
3007 then
3008 Insert_Level_Assign
3009 (Expression (Res_Assn));
3011 -- Add the level assignment
3013 else
3014 Insert_Before_And_Analyze (Res_Assn,
3015 Make_Assignment_Statement (Loc,
3016 Name => New_Occurrence_Of (Lvl, Loc),
3017 Expression =>
3018 Accessibility_Level
3019 (Expr => Expression (Res_Assn),
3020 Level => Dynamic_Level,
3021 Allow_Alt_Model => False)));
3022 end if;
3023 end Expand_Branch;
3025 Cond : Node_Id;
3026 Alt : Node_Id;
3028 -- Start of processing for Insert_Level_Assign
3030 begin
3031 -- Examine further nested condtionals
3033 pragma Assert (Nkind (Branch) =
3034 N_Expression_With_Actions);
3036 -- Find the relevant statement in the actions
3038 Cond := First (Actions (Branch));
3039 while Present (Cond) loop
3040 exit when Nkind (Cond) in N_Case_Statement | N_If_Statement;
3041 Next (Cond);
3042 end loop;
3044 -- The conditional expression may have been optimized away, so
3045 -- examine the actions in the branch.
3047 if No (Cond) then
3048 Expand_Branch (Last (Actions (Branch)));
3050 -- Iterate through if expression branches
3052 elsif Nkind (Cond) = N_If_Statement then
3053 Expand_Branch (Last (Then_Statements (Cond)));
3054 Expand_Branch (Last (Else_Statements (Cond)));
3056 -- Iterate through case alternatives
3058 elsif Nkind (Cond) = N_Case_Statement then
3060 Alt := First (Alternatives (Cond));
3061 while Present (Alt) loop
3062 Expand_Branch (Last (Statements (Alt)));
3063 Next (Alt);
3064 end loop;
3065 end if;
3066 end Insert_Level_Assign;
3068 -- Start of processing for cond expression case
3070 begin
3071 -- Create declaration of a temporary to store the accessibility
3072 -- level of each branch of the conditional expression.
3074 Lvl := Make_Temporary (Loc, 'L');
3075 Decl := Make_Object_Declaration (Loc,
3076 Defining_Identifier => Lvl,
3077 Object_Definition =>
3078 New_Occurrence_Of (Standard_Natural, Loc));
3080 -- Install the declaration and perform necessary expansion if we
3081 -- are dealing with a procedure call.
3083 if Nkind (Call_Node) = N_Procedure_Call_Statement then
3084 -- Generate:
3085 -- Lvl : Natural;
3086 -- Call (
3087 -- {do
3088 -- If_Exp_Res : Typ;
3089 -- if Cond then
3090 -- Lvl := 0; -- Access level
3091 -- If_Exp_Res := Exp;
3092 -- ...
3093 -- in If_Exp_Res end;},
3094 -- Lvl,
3095 -- ...
3096 -- )
3098 Insert_Before_And_Analyze (Call_Node, Decl);
3100 -- Ditto for a function call. Note that we do not wrap the function
3101 -- call into an expression with action to avoid bad interactions with
3102 -- Exp_Ch4.Process_Transient_In_Expression.
3104 else
3105 -- Generate:
3106 -- Lvl : Natural; -- placed above the function call
3107 -- ...
3108 -- Func_Call (
3109 -- {do
3110 -- If_Exp_Res : Typ
3111 -- if Cond then
3112 -- Lvl := 0; -- Access level
3113 -- If_Exp_Res := Exp;
3114 -- in If_Exp_Res end;},
3115 -- Lvl,
3116 -- ...
3117 -- )
3119 Insert_Action (Call_Node, Decl);
3120 Analyze (Call_Node);
3121 end if;
3123 -- Decorate the conditional expression with assignments to our level
3124 -- temporary.
3126 Insert_Level_Assign (Prev);
3128 -- Make our level temporary the passed actual
3130 Add_Extra_Actual
3131 (Expr => New_Occurrence_Of (Lvl, Loc),
3132 EF => Extra_Accessibility (Formal));
3133 end Add_Cond_Expression_Extra_Actual;
3135 ----------------------
3136 -- Add_Extra_Actual --
3137 ----------------------
3139 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
3140 Loc : constant Source_Ptr := Sloc (Expr);
3142 begin
3143 if Extra_Actuals = No_List then
3144 Extra_Actuals := New_List;
3145 Set_Parent (Extra_Actuals, Call_Node);
3146 end if;
3148 Append_To (Extra_Actuals,
3149 Make_Parameter_Association (Loc,
3150 Selector_Name => New_Occurrence_Of (EF, Loc),
3151 Explicit_Actual_Parameter => Expr));
3153 Analyze_And_Resolve (Expr, Etype (EF));
3155 if Nkind (Call_Node) = N_Function_Call then
3156 Set_Is_Accessibility_Actual (Parent (Expr));
3157 end if;
3158 end Add_Extra_Actual;
3160 ------------------------------------
3161 -- Add_View_Conversion_Invariants --
3162 ------------------------------------
3164 procedure Add_View_Conversion_Invariants
3165 (Formal : Entity_Id;
3166 Actual : Node_Id)
3168 Arg : Entity_Id;
3169 Curr_Typ : Entity_Id;
3170 Inv_Checks : List_Id;
3171 Par_Typ : Entity_Id;
3173 begin
3174 Inv_Checks := No_List;
3176 -- Extract the argument from a potentially nested set of view
3177 -- conversions.
3179 Arg := Actual;
3180 while Nkind (Arg) = N_Type_Conversion loop
3181 Arg := Expression (Arg);
3182 end loop;
3184 -- Move up the derivation chain starting with the type of the formal
3185 -- parameter down to the type of the actual object.
3187 Curr_Typ := Empty;
3188 Par_Typ := Etype (Arg);
3189 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
3190 Curr_Typ := Par_Typ;
3192 if Has_Invariants (Curr_Typ)
3193 and then Present (Invariant_Procedure (Curr_Typ))
3194 then
3195 -- Verify the invariant of the current type. Generate:
3197 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
3199 Prepend_New_To (Inv_Checks,
3200 Make_Procedure_Call_Statement (Loc,
3201 Name =>
3202 New_Occurrence_Of
3203 (Invariant_Procedure (Curr_Typ), Loc),
3204 Parameter_Associations => New_List (
3205 Make_Type_Conversion (Loc,
3206 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
3207 Expression => New_Copy_Tree (Arg)))));
3208 end if;
3210 Par_Typ := Base_Type (Etype (Curr_Typ));
3211 end loop;
3213 -- If the node is a function call the generated tests have been
3214 -- already handled in Insert_Post_Call_Actions.
3216 if not Is_Empty_List (Inv_Checks)
3217 and then Nkind (Call_Node) = N_Procedure_Call_Statement
3218 then
3219 Insert_Actions_After (Call_Node, Inv_Checks);
3220 end if;
3221 end Add_View_Conversion_Invariants;
3223 -----------------------------
3224 -- Can_Fold_Predicate_Call --
3225 -----------------------------
3227 function Can_Fold_Predicate_Call (P : Entity_Id) return Boolean is
3228 Actual : Node_Id;
3230 function Augments_Other_Dynamic_Predicate (DP_Aspect_Spec : Node_Id)
3231 return Boolean;
3232 -- Given a Dynamic_Predicate aspect aspecification for a
3233 -- discrete type, returns True iff another DP specification
3234 -- applies (indirectly, via a subtype type or a derived type)
3235 -- to the same entity that this aspect spec applies to.
3237 function May_Fold (N : Node_Id) return Traverse_Result;
3238 -- The predicate expression is foldable if it only contains operators
3239 -- and literals. During this check, we also replace occurrences of
3240 -- the formal of the constructed predicate function with the static
3241 -- value of the actual. This is done on a copy of the analyzed
3242 -- expression for the predicate.
3244 --------------------------------------
3245 -- Augments_Other_Dynamic_Predicate --
3246 --------------------------------------
3248 function Augments_Other_Dynamic_Predicate (DP_Aspect_Spec : Node_Id)
3249 return Boolean
3251 Aspect_Bearer : Entity_Id := Entity (DP_Aspect_Spec);
3252 begin
3253 loop
3254 Aspect_Bearer := Nearest_Ancestor (Aspect_Bearer);
3256 if No (Aspect_Bearer) then
3257 return False;
3258 end if;
3260 declare
3261 Aspect_Spec : constant Node_Id :=
3262 Find_Aspect (Aspect_Bearer, Aspect_Dynamic_Predicate);
3263 begin
3264 if Present (Aspect_Spec)
3265 and then Aspect_Spec /= DP_Aspect_Spec
3266 then
3267 -- Found another Dynamic_Predicate aspect spec
3268 return True;
3269 end if;
3270 end;
3271 end loop;
3272 end Augments_Other_Dynamic_Predicate;
3274 --------------
3275 -- May_Fold --
3276 --------------
3278 function May_Fold (N : Node_Id) return Traverse_Result is
3279 begin
3280 case Nkind (N) is
3281 when N_Op =>
3282 return OK;
3284 when N_Expanded_Name
3285 | N_Identifier
3287 if Ekind (Entity (N)) = E_In_Parameter
3288 and then Entity (N) = First_Entity (P)
3289 then
3290 Rewrite (N, New_Copy (Actual));
3291 Set_Is_Static_Expression (N);
3292 return OK;
3294 elsif Ekind (Entity (N)) = E_Enumeration_Literal then
3295 return OK;
3297 else
3298 return Abandon;
3299 end if;
3301 when N_Case_Expression
3302 | N_If_Expression
3304 return OK;
3306 when N_Integer_Literal =>
3307 return OK;
3309 when others =>
3310 return Abandon;
3311 end case;
3312 end May_Fold;
3314 function Try_Fold is new Traverse_Func (May_Fold);
3316 -- Other Local variables
3318 Subt : constant Entity_Id := Etype (First_Entity (P));
3319 Aspect : Node_Id;
3320 Pred : Node_Id;
3322 -- Start of processing for Can_Fold_Predicate_Call
3324 begin
3325 -- Folding is only interesting if the actual is static and its type
3326 -- has a Dynamic_Predicate aspect. For CodePeer we preserve the
3327 -- function call.
3329 Actual := First (Parameter_Associations (Call_Node));
3330 Aspect := Find_Aspect (Subt, Aspect_Dynamic_Predicate);
3332 -- If actual is a declared constant, retrieve its value
3334 if Is_Entity_Name (Actual)
3335 and then Ekind (Entity (Actual)) = E_Constant
3336 then
3337 Actual := Constant_Value (Entity (Actual));
3338 end if;
3340 if No (Actual)
3341 or else Nkind (Actual) /= N_Integer_Literal
3342 or else not Has_Dynamic_Predicate_Aspect (Subt)
3343 or else No (Aspect)
3345 -- Do not fold if multiple applicable predicate aspects
3346 or else Has_Aspect (Subt, Aspect_Static_Predicate)
3347 or else Has_Aspect (Subt, Aspect_Predicate)
3348 or else Augments_Other_Dynamic_Predicate (Aspect)
3349 or else CodePeer_Mode
3350 then
3351 return False;
3352 end if;
3354 -- Retrieve the analyzed expression for the predicate
3356 Pred := New_Copy_Tree (Expression (Aspect));
3358 if Try_Fold (Pred) = OK then
3359 Rewrite (Call_Node, Pred);
3360 Analyze_And_Resolve (Call_Node, Standard_Boolean);
3361 return True;
3363 -- Otherwise continue the expansion of the function call
3365 else
3366 return False;
3367 end if;
3368 end Can_Fold_Predicate_Call;
3370 ------------------------------
3371 -- Check_Subprogram_Variant --
3372 ------------------------------
3374 procedure Check_Subprogram_Variant is
3376 function Duplicate_Params_Without_Extra_Actuals
3377 (Call_Node : Node_Id) return List_Id;
3378 -- Duplicate actual parameters of Call_Node into New_Call without
3379 -- extra actuals.
3381 --------------------------------------------
3382 -- Duplicate_Params_Without_Extra_Actuals --
3383 --------------------------------------------
3385 function Duplicate_Params_Without_Extra_Actuals
3386 (Call_Node : Node_Id) return List_Id
3388 Proc_Id : constant Entity_Id := Entity (Name (Call_Node));
3389 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3390 NL : List_Id;
3391 Actual : Node_Or_Entity_Id;
3392 Formal : Entity_Id;
3394 begin
3395 if Actuals = No_List then
3396 return No_List;
3398 else
3399 NL := New_List;
3400 Actual := First (Actuals);
3401 Formal := First_Formal (Proc_Id);
3403 while Present (Formal)
3404 and then Formal /= Extra_Formals (Proc_Id)
3405 loop
3406 Append (New_Copy (Actual), NL);
3407 Next (Actual);
3409 Next_Formal (Formal);
3410 end loop;
3412 return NL;
3413 end if;
3414 end Duplicate_Params_Without_Extra_Actuals;
3416 -- Local variables
3418 Variant_Prag : constant Node_Id :=
3419 Get_Pragma (Current_Scope, Pragma_Subprogram_Variant);
3421 New_Call : Node_Id;
3422 Pragma_Arg1 : Node_Id;
3423 Variant_Proc : Entity_Id;
3425 begin
3426 if Present (Variant_Prag) and then Is_Checked (Variant_Prag) then
3428 Pragma_Arg1 :=
3429 Expression (First (Pragma_Argument_Associations (Variant_Prag)));
3431 -- If pragma parameter is still an aggregate, it comes from a
3432 -- structural variant, which is not expanded and ignored for
3433 -- run-time execution.
3435 if Nkind (Pragma_Arg1) = N_Aggregate then
3436 pragma Assert
3437 (Chars
3438 (First
3439 (Choices
3440 (First (Component_Associations (Pragma_Arg1))))) =
3441 Name_Structural);
3442 return;
3443 end if;
3445 -- Otherwise, analysis of the pragma rewrites its argument with a
3446 -- reference to the internally generated procedure.
3448 Variant_Proc := Entity (Pragma_Arg1);
3450 New_Call :=
3451 Make_Procedure_Call_Statement (Loc,
3452 Name =>
3453 New_Occurrence_Of (Variant_Proc, Loc),
3454 Parameter_Associations =>
3455 Duplicate_Params_Without_Extra_Actuals (Call_Node));
3457 Insert_Action (Call_Node, New_Call);
3459 pragma Assert (Etype (New_Call) /= Any_Type
3460 or else Serious_Errors_Detected > 0);
3461 end if;
3462 end Check_Subprogram_Variant;
3464 ---------------------------
3465 -- Inherited_From_Formal --
3466 ---------------------------
3468 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
3469 Par : Entity_Id;
3470 Gen_Par : Entity_Id;
3471 Gen_Prim : Elist_Id;
3472 Elmt : Elmt_Id;
3473 Indic : Node_Id;
3475 begin
3476 -- If the operation is inherited, it is attached to the corresponding
3477 -- type derivation. If the parent in the derivation is a generic
3478 -- actual, it is a subtype of the actual, and we have to recover the
3479 -- original derived type declaration to find the proper parent.
3481 if Nkind (Parent (S)) /= N_Full_Type_Declaration
3482 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
3483 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
3484 N_Derived_Type_Definition
3485 or else not In_Instance
3486 then
3487 return Empty;
3489 else
3490 Indic :=
3491 Subtype_Indication
3492 (Type_Definition (Original_Node (Parent (S))));
3494 if Nkind (Indic) = N_Subtype_Indication then
3495 Par := Entity (Subtype_Mark (Indic));
3496 else
3497 Par := Entity (Indic);
3498 end if;
3499 end if;
3501 if not Is_Generic_Actual_Type (Par)
3502 or else Is_Tagged_Type (Par)
3503 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
3504 or else not In_Open_Scopes (Scope (Par))
3505 then
3506 return Empty;
3507 else
3508 Gen_Par := Generic_Parent_Type (Parent (Par));
3509 end if;
3511 -- If the actual has no generic parent type, the formal is not
3512 -- a formal derived type, so nothing to inherit.
3514 if No (Gen_Par) then
3515 return Empty;
3516 end if;
3518 -- If the generic parent type is still the generic type, this is a
3519 -- private formal, not a derived formal, and there are no operations
3520 -- inherited from the formal.
3522 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
3523 return Empty;
3524 end if;
3526 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
3528 Elmt := First_Elmt (Gen_Prim);
3529 while Present (Elmt) loop
3530 if Chars (Node (Elmt)) = Chars (S) then
3531 declare
3532 F1 : Entity_Id;
3533 F2 : Entity_Id;
3535 begin
3536 F1 := First_Formal (S);
3537 F2 := First_Formal (Node (Elmt));
3538 while Present (F1)
3539 and then Present (F2)
3540 loop
3541 if Etype (F1) = Etype (F2)
3542 or else Etype (F2) = Gen_Par
3543 then
3544 Next_Formal (F1);
3545 Next_Formal (F2);
3546 else
3547 Next_Elmt (Elmt);
3548 exit; -- not the right subprogram
3549 end if;
3551 return Node (Elmt);
3552 end loop;
3553 end;
3555 else
3556 Next_Elmt (Elmt);
3557 end if;
3558 end loop;
3560 raise Program_Error;
3561 end Inherited_From_Formal;
3563 --------------------------
3564 -- In_Unfrozen_Instance --
3565 --------------------------
3567 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
3568 S : Entity_Id;
3570 begin
3571 S := E;
3572 while Present (S) and then S /= Standard_Standard loop
3573 if Is_Generic_Instance (S)
3574 and then Present (Freeze_Node (S))
3575 and then not Analyzed (Freeze_Node (S))
3576 then
3577 return True;
3578 end if;
3580 S := Scope (S);
3581 end loop;
3583 return False;
3584 end In_Unfrozen_Instance;
3586 ----------------------------------
3587 -- Is_Class_Wide_Interface_Type --
3588 ----------------------------------
3590 function Is_Class_Wide_Interface_Type (E : Entity_Id) return Boolean is
3591 DDT : Entity_Id;
3592 Typ : Entity_Id := E;
3594 begin
3595 if Has_Non_Limited_View (Typ) then
3596 Typ := Non_Limited_View (Typ);
3597 end if;
3599 if Ekind (Typ) = E_Anonymous_Access_Type then
3600 DDT := Directly_Designated_Type (Typ);
3602 if Has_Non_Limited_View (DDT) then
3603 DDT := Non_Limited_View (DDT);
3604 end if;
3606 return Is_Class_Wide_Type (DDT) and then Is_Interface (DDT);
3607 else
3608 return Is_Class_Wide_Type (Typ) and then Is_Interface (Typ);
3609 end if;
3610 end Is_Class_Wide_Interface_Type;
3612 -------------------------
3613 -- Is_Direct_Deep_Call --
3614 -------------------------
3616 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
3617 begin
3618 if Is_TSS (Subp, TSS_Deep_Adjust)
3619 or else Is_TSS (Subp, TSS_Deep_Finalize)
3620 or else Is_TSS (Subp, TSS_Deep_Initialize)
3621 then
3622 declare
3623 Actual : Node_Id;
3624 Formal : Entity_Id;
3626 begin
3627 Actual := First (Parameter_Associations (Call_Node));
3628 Formal := First_Formal (Subp);
3629 while Present (Actual)
3630 and then Present (Formal)
3631 loop
3632 if Nkind (Actual) = N_Identifier
3633 and then Is_Controlling_Actual (Actual)
3634 and then Etype (Actual) = Etype (Formal)
3635 then
3636 return True;
3637 end if;
3639 Next (Actual);
3640 Next_Formal (Formal);
3641 end loop;
3642 end;
3643 end if;
3645 return False;
3646 end Is_Direct_Deep_Call;
3648 ---------------
3649 -- New_Value --
3650 ---------------
3652 function New_Value (From : Node_Id) return Node_Id is
3653 Res : constant Node_Id := Duplicate_Subexpr (From);
3654 begin
3655 if Is_Access_Type (Etype (From)) then
3656 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
3657 else
3658 return Res;
3659 end if;
3660 end New_Value;
3662 -- Local variables
3664 Remote : constant Boolean := Is_Remote_Call (Call_Node);
3665 Actual : Node_Id;
3666 Formal : Entity_Id;
3667 Orig_Subp : Entity_Id := Empty;
3668 Param_Count : Positive;
3669 Parent_Formal : Entity_Id;
3670 Parent_Subp : Entity_Id;
3671 Scop : Entity_Id;
3672 Subp : Entity_Id;
3674 CW_Interface_Formals_Present : Boolean := False;
3676 -- Start of processing for Expand_Call_Helper
3678 begin
3679 Post_Call := New_List;
3681 -- Expand the function or procedure call if the first actual has a
3682 -- declared dimension aspect, and the subprogram is declared in one
3683 -- of the dimension I/O packages.
3685 if Ada_Version >= Ada_2012
3686 and then Nkind (Call_Node) in N_Subprogram_Call
3687 and then Present (Parameter_Associations (Call_Node))
3688 then
3689 Expand_Put_Call_With_Symbol (Call_Node);
3690 end if;
3692 -- Ignore if previous error
3694 if Nkind (Call_Node) in N_Has_Etype
3695 and then Etype (Call_Node) = Any_Type
3696 then
3697 return;
3698 end if;
3700 -- Call using access to subprogram with explicit dereference
3702 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3703 Subp := Etype (Name (Call_Node));
3704 Parent_Subp := Empty;
3706 -- Case of call to simple entry, where the Name is a selected component
3707 -- whose prefix is the task, and whose selector name is the entry name
3709 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
3710 Subp := Entity (Selector_Name (Name (Call_Node)));
3711 Parent_Subp := Empty;
3713 -- Case of call to member of entry family, where Name is an indexed
3714 -- component, with the prefix being a selected component giving the
3715 -- task and entry family name, and the index being the entry index.
3717 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
3718 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
3719 Parent_Subp := Empty;
3721 -- Normal case
3723 else
3724 Subp := Entity (Name (Call_Node));
3725 Parent_Subp := Alias (Subp);
3727 -- Replace call to Raise_Exception by call to Raise_Exception_Always
3728 -- if we can tell that the first parameter cannot possibly be null.
3729 -- This improves efficiency by avoiding a run-time test.
3731 -- We do not do this if Raise_Exception_Always does not exist, which
3732 -- can happen in configurable run time profiles which provide only a
3733 -- Raise_Exception.
3735 if Is_RTE (Subp, RE_Raise_Exception)
3736 and then RTE_Available (RE_Raise_Exception_Always)
3737 then
3738 declare
3739 FA : constant Node_Id :=
3740 Original_Node (First_Actual (Call_Node));
3742 begin
3743 -- The case we catch is where the first argument is obtained
3744 -- using the Identity attribute (which must always be
3745 -- non-null).
3747 if Nkind (FA) = N_Attribute_Reference
3748 and then Attribute_Name (FA) = Name_Identity
3749 then
3750 Subp := RTE (RE_Raise_Exception_Always);
3751 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
3752 end if;
3753 end;
3754 end if;
3756 if Ekind (Subp) = E_Entry then
3757 Parent_Subp := Empty;
3758 end if;
3759 end if;
3761 -- Ensure that the called subprogram has all its formals
3763 if not Is_Frozen (Subp) then
3764 Create_Extra_Formals (Subp);
3765 end if;
3767 -- Ada 2005 (AI-345): We have a procedure call as a triggering
3768 -- alternative in an asynchronous select or as an entry call in
3769 -- a conditional or timed select. Check whether the procedure call
3770 -- is a renaming of an entry and rewrite it as an entry call.
3772 if Ada_Version >= Ada_2005
3773 and then Nkind (Call_Node) = N_Procedure_Call_Statement
3774 and then
3775 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
3776 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
3777 or else
3778 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
3779 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
3780 then
3781 declare
3782 Ren_Decl : Node_Id;
3783 Ren_Root : Entity_Id := Subp;
3785 begin
3786 -- This may be a chain of renamings, find the root
3788 if Present (Alias (Ren_Root)) then
3789 Ren_Root := Alias (Ren_Root);
3790 end if;
3792 if Present (Parent (Ren_Root))
3793 and then Present (Original_Node (Parent (Parent (Ren_Root))))
3794 then
3795 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
3797 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
3798 Rewrite (Call_Node,
3799 Make_Entry_Call_Statement (Loc,
3800 Name =>
3801 New_Copy_Tree (Name (Ren_Decl)),
3802 Parameter_Associations =>
3803 New_Copy_List_Tree
3804 (Parameter_Associations (Call_Node))));
3806 return;
3807 end if;
3808 end if;
3809 end;
3810 end if;
3812 -- If this is a call to a predicate function, try to constant fold it
3814 if Nkind (Call_Node) = N_Function_Call
3815 and then Is_Entity_Name (Name (Call_Node))
3816 and then Is_Predicate_Function (Subp)
3817 and then Can_Fold_Predicate_Call (Subp)
3818 then
3819 return;
3820 end if;
3822 if Transform_Function_Array
3823 and then Nkind (Call_Node) = N_Function_Call
3824 and then Is_Entity_Name (Name (Call_Node))
3825 then
3826 declare
3827 Func_Id : constant Entity_Id :=
3828 Ultimate_Alias (Entity (Name (Call_Node)));
3829 begin
3830 -- When generating C code, transform a function call that returns
3831 -- a constrained array type into procedure form.
3833 if Rewritten_For_C (Func_Id) then
3835 -- For internally generated calls ensure that they reference
3836 -- the entity of the spec of the called function (needed since
3837 -- the expander may generate calls using the entity of their
3838 -- body).
3840 if not Comes_From_Source (Call_Node)
3841 and then Nkind (Unit_Declaration_Node (Func_Id)) =
3842 N_Subprogram_Body
3843 then
3844 Set_Entity (Name (Call_Node),
3845 Corresponding_Function
3846 (Corresponding_Procedure (Func_Id)));
3847 end if;
3849 Rewrite_Function_Call_For_C (Call_Node);
3850 return;
3852 -- Also introduce a temporary for functions that return a record
3853 -- called within another procedure or function call, since records
3854 -- are passed by pointer in the generated C code, and we cannot
3855 -- take a pointer from a subprogram call.
3857 elsif Modify_Tree_For_C
3858 and then Nkind (Parent (Call_Node)) in N_Subprogram_Call
3859 and then Is_Record_Type (Etype (Func_Id))
3860 then
3861 declare
3862 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
3863 Decl : Node_Id;
3865 begin
3866 -- Generate:
3867 -- Temp : ... := Func_Call (...);
3869 Decl :=
3870 Make_Object_Declaration (Loc,
3871 Defining_Identifier => Temp_Id,
3872 Object_Definition =>
3873 New_Occurrence_Of (Etype (Func_Id), Loc),
3874 Expression =>
3875 Make_Function_Call (Loc,
3876 Name =>
3877 New_Occurrence_Of (Func_Id, Loc),
3878 Parameter_Associations =>
3879 Parameter_Associations (Call_Node)));
3881 Insert_Action (Parent (Call_Node), Decl);
3882 Rewrite (Call_Node, New_Occurrence_Of (Temp_Id, Loc));
3883 return;
3884 end;
3885 end if;
3886 end;
3887 end if;
3889 -- First step, compute extra actuals, corresponding to any Extra_Formals
3890 -- present. Note that we do not access Extra_Formals directly, instead
3891 -- we simply note the presence of the extra formals as we process the
3892 -- regular formals collecting corresponding actuals in Extra_Actuals.
3894 -- We also generate any required range checks for actuals for in formals
3895 -- as we go through the loop, since this is a convenient place to do it.
3896 -- (Though it seems that this would be better done in Expand_Actuals???)
3898 -- Special case: Thunks must not compute the extra actuals; they must
3899 -- just propagate to the target primitive their extra actuals.
3901 if Is_Thunk (Current_Scope)
3902 and then Thunk_Entity (Current_Scope) = Subp
3903 and then Present (Extra_Formals (Subp))
3904 then
3905 pragma Assert (Extra_Formals_Match_OK (Current_Scope, Subp));
3907 declare
3908 Target_Formal : Entity_Id;
3909 Thunk_Formal : Entity_Id;
3911 begin
3912 Target_Formal := Extra_Formals (Subp);
3913 Thunk_Formal := Extra_Formals (Current_Scope);
3914 while Present (Target_Formal) loop
3915 Add_Extra_Actual
3916 (Expr => New_Occurrence_Of (Thunk_Formal, Loc),
3917 EF => Thunk_Formal);
3919 Target_Formal := Extra_Formal (Target_Formal);
3920 Thunk_Formal := Extra_Formal (Thunk_Formal);
3921 end loop;
3923 while Is_Non_Empty_List (Extra_Actuals) loop
3924 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3925 end loop;
3927 -- Mark the call as processed build-in-place call; required
3928 -- to avoid adding the extra formals twice.
3930 if Nkind (Call_Node) = N_Function_Call then
3931 Set_Is_Expanded_Build_In_Place_Call (Call_Node);
3932 end if;
3934 Expand_Actuals (Call_Node, Subp, Post_Call);
3935 pragma Assert (Is_Empty_List (Post_Call));
3936 pragma Assert (Check_Number_Of_Actuals (Call_Node, Subp));
3937 pragma Assert (Check_BIP_Actuals (Call_Node, Subp));
3938 return;
3939 end;
3940 end if;
3942 Formal := First_Formal (Subp);
3943 Actual := First_Actual (Call_Node);
3944 Param_Count := 1;
3945 while Present (Formal) loop
3946 -- Prepare to examine current entry
3948 Prev := Actual;
3950 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
3951 -- to expand it in a further round.
3953 CW_Interface_Formals_Present :=
3954 CW_Interface_Formals_Present
3955 or else Is_Class_Wide_Interface_Type (Etype (Formal));
3957 -- Create possible extra actual for constrained case. Usually, the
3958 -- extra actual is of the form actual'constrained, but since this
3959 -- attribute is only available for unconstrained records, TRUE is
3960 -- expanded if the type of the formal happens to be constrained (for
3961 -- instance when this procedure is inherited from an unconstrained
3962 -- record to a constrained one) or if the actual has no discriminant
3963 -- (its type is constrained). An exception to this is the case of a
3964 -- private type without discriminants. In this case we pass FALSE
3965 -- because the object has underlying discriminants with defaults.
3967 if Present (Extra_Constrained (Formal)) then
3968 if Is_Private_Type (Etype (Prev))
3969 and then not Has_Discriminants (Base_Type (Etype (Prev)))
3970 then
3971 Add_Extra_Actual
3972 (Expr => New_Occurrence_Of (Standard_False, Loc),
3973 EF => Extra_Constrained (Formal));
3975 elsif Is_Constrained (Etype (Formal))
3976 or else not Has_Discriminants (Etype (Prev))
3977 then
3978 Add_Extra_Actual
3979 (Expr => New_Occurrence_Of (Standard_True, Loc),
3980 EF => Extra_Constrained (Formal));
3982 -- Do not produce extra actuals for Unchecked_Union parameters.
3983 -- Jump directly to the end of the loop.
3985 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
3986 goto Skip_Extra_Actual_Generation;
3988 else
3989 -- If the actual is a type conversion, then the constrained
3990 -- test applies to the actual, not the target type.
3992 declare
3993 Act_Prev : Node_Id;
3995 begin
3996 -- Test for unchecked conversions as well, which can occur
3997 -- as out parameter actuals on calls to stream procedures.
3999 Act_Prev := Prev;
4000 while Nkind (Act_Prev) in N_Type_Conversion
4001 | N_Unchecked_Type_Conversion
4002 loop
4003 Act_Prev := Expression (Act_Prev);
4004 end loop;
4006 -- If the expression is a conversion of a dereference, this
4007 -- is internally generated code that manipulates addresses,
4008 -- e.g. when building interface tables. No check should
4009 -- occur in this case, and the discriminated object is not
4010 -- directly at hand.
4012 if not Comes_From_Source (Actual)
4013 and then Nkind (Actual) = N_Unchecked_Type_Conversion
4014 and then Nkind (Act_Prev) = N_Explicit_Dereference
4015 then
4016 Add_Extra_Actual
4017 (Expr => New_Occurrence_Of (Standard_False, Loc),
4018 EF => Extra_Constrained (Formal));
4020 else
4021 Add_Extra_Actual
4022 (Expr =>
4023 Make_Attribute_Reference (Sloc (Prev),
4024 Prefix =>
4025 Duplicate_Subexpr_No_Checks
4026 (Act_Prev, Name_Req => True),
4027 Attribute_Name => Name_Constrained),
4028 EF => Extra_Constrained (Formal));
4029 end if;
4030 end;
4031 end if;
4032 end if;
4034 -- Create possible extra actual for accessibility level
4036 if Present (Extra_Accessibility (Formal)) then
4037 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
4038 -- accessibility levels.
4040 if Is_Thunk (Current_Scope) then
4041 declare
4042 Parm_Ent : Entity_Id;
4044 begin
4045 if Is_Controlling_Actual (Actual) then
4047 -- Find the corresponding actual of the thunk
4049 Parm_Ent := First_Entity (Current_Scope);
4050 for J in 2 .. Param_Count loop
4051 Next_Entity (Parm_Ent);
4052 end loop;
4054 -- Handle unchecked conversion of access types generated
4055 -- in thunks (cf. Expand_Interface_Thunk).
4057 elsif Is_Access_Type (Etype (Actual))
4058 and then Nkind (Actual) = N_Unchecked_Type_Conversion
4059 then
4060 Parm_Ent := Entity (Expression (Actual));
4062 else pragma Assert (Is_Entity_Name (Actual));
4063 Parm_Ent := Entity (Actual);
4064 end if;
4066 Add_Extra_Actual
4067 (Expr => Accessibility_Level
4068 (Expr => Parm_Ent,
4069 Level => Dynamic_Level,
4070 Allow_Alt_Model => False),
4071 EF => Extra_Accessibility (Formal));
4072 end;
4074 -- Conditional expressions
4076 elsif Nkind (Prev) = N_Expression_With_Actions
4077 and then Nkind (Original_Node (Prev)) in
4078 N_If_Expression | N_Case_Expression
4079 then
4080 Add_Cond_Expression_Extra_Actual (Formal);
4082 -- Internal constant generated to remove side effects (normally
4083 -- from the expansion of dispatching calls).
4085 -- First verify the actual is internal
4087 elsif not Comes_From_Source (Prev)
4088 and then not Is_Rewrite_Substitution (Prev)
4090 -- Next check that the actual is a constant
4092 and then Nkind (Prev) = N_Identifier
4093 and then Ekind (Entity (Prev)) = E_Constant
4094 and then Nkind (Parent (Entity (Prev))) = N_Object_Declaration
4095 then
4096 -- Generate the accessibility level based on the expression in
4097 -- the constant's declaration.
4099 Add_Extra_Actual
4100 (Expr => Accessibility_Level
4101 (Expr => Expression
4102 (Parent (Entity (Prev))),
4103 Level => Dynamic_Level,
4104 Allow_Alt_Model => False),
4105 EF => Extra_Accessibility (Formal));
4107 -- Normal case
4109 else
4110 Add_Extra_Actual
4111 (Expr => Accessibility_Level
4112 (Expr => Prev,
4113 Level => Dynamic_Level,
4114 Allow_Alt_Model => False),
4115 EF => Extra_Accessibility (Formal));
4116 end if;
4117 end if;
4119 -- Perform the check of 4.6(49) that prevents a null value from being
4120 -- passed as an actual to an access parameter. Note that the check
4121 -- is elided in the common cases of passing an access attribute or
4122 -- access parameter as an actual. Also, we currently don't enforce
4123 -- this check for expander-generated actuals and when -gnatdj is set.
4125 if Ada_Version >= Ada_2005 then
4127 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
4128 -- the intent of 6.4.1(13) is that null-exclusion checks should
4129 -- not be done for 'out' parameters, even though it refers only
4130 -- to constraint checks, and a null_exclusion is not a constraint.
4131 -- Note that AI05-0196-1 corrects this mistake in the RM.
4133 if Is_Access_Type (Etype (Formal))
4134 and then Can_Never_Be_Null (Etype (Formal))
4135 and then Ekind (Formal) /= E_Out_Parameter
4136 and then Nkind (Prev) /= N_Raise_Constraint_Error
4137 and then (Known_Null (Prev)
4138 or else not Can_Never_Be_Null (Etype (Prev)))
4139 then
4140 Install_Null_Excluding_Check (Prev);
4141 end if;
4143 -- Ada_Version < Ada_2005
4145 else
4146 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
4147 or else Access_Checks_Suppressed (Subp)
4148 then
4149 null;
4151 elsif Debug_Flag_J then
4152 null;
4154 elsif not Comes_From_Source (Prev) then
4155 null;
4157 elsif Is_Entity_Name (Prev)
4158 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
4159 then
4160 null;
4162 elsif Nkind (Prev) in N_Allocator | N_Attribute_Reference then
4163 null;
4165 else
4166 Install_Null_Excluding_Check (Prev);
4167 end if;
4168 end if;
4170 -- Perform appropriate validity checks on parameters that
4171 -- are entities.
4173 if Validity_Checks_On then
4174 if (Ekind (Formal) = E_In_Parameter
4175 and then Validity_Check_In_Params)
4176 or else
4177 (Ekind (Formal) = E_In_Out_Parameter
4178 and then Validity_Check_In_Out_Params)
4179 then
4180 -- If the actual is an indexed component of a packed type (or
4181 -- is an indexed or selected component whose prefix recursively
4182 -- meets this condition), it has not been expanded yet. It will
4183 -- be copied in the validity code that follows, and has to be
4184 -- expanded appropriately, so reanalyze it.
4186 -- What we do is just to unset analyzed bits on prefixes till
4187 -- we reach something that does not have a prefix.
4189 declare
4190 Nod : Node_Id;
4192 begin
4193 Nod := Actual;
4194 while Nkind (Nod) in
4195 N_Indexed_Component | N_Selected_Component
4196 loop
4197 Set_Analyzed (Nod, False);
4198 Nod := Prefix (Nod);
4199 end loop;
4200 end;
4202 Ensure_Valid (Actual);
4203 end if;
4204 end if;
4206 -- For IN OUT and OUT parameters, ensure that subscripts are valid
4207 -- since this is a left side reference. We only do this for calls
4208 -- from the source program since we assume that compiler generated
4209 -- calls explicitly generate any required checks. We also need it
4210 -- only if we are doing standard validity checks, since clearly it is
4211 -- not needed if validity checks are off, and in subscript validity
4212 -- checking mode, all indexed components are checked with a call
4213 -- directly from Expand_N_Indexed_Component.
4215 if Comes_From_Source (Call_Node)
4216 and then Ekind (Formal) /= E_In_Parameter
4217 and then Validity_Checks_On
4218 and then Validity_Check_Default
4219 and then not Validity_Check_Subscripts
4220 then
4221 Check_Valid_Lvalue_Subscripts (Actual);
4222 end if;
4224 -- Mark any scalar OUT parameter that is a simple variable as no
4225 -- longer known to be valid (unless the type is always valid). This
4226 -- reflects the fact that if an OUT parameter is never set in a
4227 -- procedure, then it can become invalid on the procedure return.
4229 if Ekind (Formal) = E_Out_Parameter
4230 and then Is_Entity_Name (Actual)
4231 and then Ekind (Entity (Actual)) = E_Variable
4232 and then not Is_Known_Valid (Etype (Actual))
4233 then
4234 Set_Is_Known_Valid (Entity (Actual), False);
4235 end if;
4237 -- For an OUT or IN OUT parameter, if the actual is an entity, then
4238 -- clear current values, since they can be clobbered. We are probably
4239 -- doing this in more places than we need to, but better safe than
4240 -- sorry when it comes to retaining bad current values.
4242 if Ekind (Formal) /= E_In_Parameter
4243 and then Is_Entity_Name (Actual)
4244 and then Present (Entity (Actual))
4245 then
4246 declare
4247 Ent : constant Entity_Id := Entity (Actual);
4248 Sav : Node_Id;
4250 begin
4251 -- For an OUT or IN OUT parameter that is an assignable entity,
4252 -- we do not want to clobber the Last_Assignment field, since
4253 -- if it is set, it was precisely because it is indeed an OUT
4254 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
4255 -- since the subprogram could have returned in invalid value.
4257 if Is_Assignable (Ent) then
4258 Sav := Last_Assignment (Ent);
4259 Kill_Current_Values (Ent);
4260 Set_Last_Assignment (Ent, Sav);
4261 Set_Is_Known_Valid (Ent, False);
4262 Set_Is_True_Constant (Ent, False);
4264 -- For all other cases, just kill the current values
4266 else
4267 Kill_Current_Values (Ent);
4268 end if;
4269 end;
4270 end if;
4272 -- If the formal is class-wide and the actual is an aggregate, force
4273 -- evaluation so that the back end who does not know about class-wide
4274 -- type, does not generate a temporary of the wrong size.
4276 if not Is_Class_Wide_Type (Etype (Formal)) then
4277 null;
4279 elsif Nkind (Actual) = N_Aggregate
4280 or else (Nkind (Actual) = N_Qualified_Expression
4281 and then Nkind (Expression (Actual)) = N_Aggregate)
4282 then
4283 Force_Evaluation (Actual);
4284 end if;
4286 -- In a remote call, if the formal is of a class-wide type, check
4287 -- that the actual meets the requirements described in E.4(18).
4289 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
4290 Insert_Action (Actual,
4291 Make_Transportable_Check (Loc,
4292 Duplicate_Subexpr_Move_Checks (Actual)));
4293 end if;
4295 -- Perform invariant checks for all intermediate types in a view
4296 -- conversion after successful return from a call that passes the
4297 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
4298 -- 13/3, 14/3)). Consider only source conversion in order to avoid
4299 -- generating spurious checks on complex expansion such as object
4300 -- initialization through an extension aggregate.
4302 if Comes_From_Source (Call_Node)
4303 and then Ekind (Formal) /= E_In_Parameter
4304 and then Nkind (Actual) = N_Type_Conversion
4305 then
4306 Add_View_Conversion_Invariants (Formal, Actual);
4307 end if;
4309 -- Generating C the initialization of an allocator is performed by
4310 -- means of individual statements, and hence it must be done before
4311 -- the call.
4313 if Modify_Tree_For_C
4314 and then Nkind (Actual) = N_Allocator
4315 and then Nkind (Expression (Actual)) = N_Qualified_Expression
4316 then
4317 Remove_Side_Effects (Actual);
4318 end if;
4320 -- This label is required when skipping extra actual generation for
4321 -- Unchecked_Union parameters.
4323 <<Skip_Extra_Actual_Generation>>
4325 Param_Count := Param_Count + 1;
4326 Next_Actual (Actual);
4327 Next_Formal (Formal);
4328 end loop;
4330 -- If we are calling an Ada 2012 function which needs to have the
4331 -- "accessibility level determined by the point of call" (AI05-0234)
4332 -- passed in to it, then pass it in.
4334 if Ekind (Subp) in E_Function | E_Operator | E_Subprogram_Type
4335 and then
4336 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
4337 then
4338 declare
4339 Extra_Form : Node_Id := Empty;
4340 Level : Node_Id := Empty;
4342 begin
4343 -- Detect cases where the function call has been internally
4344 -- generated by examining the original node and return library
4345 -- level - taking care to avoid ignoring function calls expanded
4346 -- in prefix notation.
4348 if Nkind (Original_Node (Call_Node)) not in N_Function_Call
4349 | N_Selected_Component
4350 | N_Indexed_Component
4351 then
4352 Level := Make_Integer_Literal
4353 (Loc, Scope_Depth (Standard_Standard));
4355 -- Otherwise get the level normally based on the call node
4357 else
4358 Level := Accessibility_Level
4359 (Expr => Call_Node,
4360 Level => Dynamic_Level,
4361 Allow_Alt_Model => False);
4362 end if;
4364 -- It may be possible that we are re-expanding an already
4365 -- expanded call when are are dealing with dispatching ???
4367 if not Present (Parameter_Associations (Call_Node))
4368 or else Nkind (Last (Parameter_Associations (Call_Node)))
4369 /= N_Parameter_Association
4370 or else not Is_Accessibility_Actual
4371 (Last (Parameter_Associations (Call_Node)))
4372 then
4373 Extra_Form := Extra_Accessibility_Of_Result
4374 (Ultimate_Alias (Subp));
4376 Add_Extra_Actual
4377 (Expr => Level,
4378 EF => Extra_Form);
4379 end if;
4380 end;
4381 end if;
4383 -- If we are expanding the RHS of an assignment we need to check if tag
4384 -- propagation is needed. You might expect this processing to be in
4385 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
4386 -- assignment might be transformed to a declaration for an unconstrained
4387 -- value if the expression is classwide.
4389 if Nkind (Call_Node) = N_Function_Call
4390 and then Is_Tag_Indeterminate (Call_Node)
4391 and then Is_Entity_Name (Name (Call_Node))
4392 then
4393 declare
4394 Ass : Node_Id := Empty;
4396 begin
4397 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
4398 Ass := Parent (Call_Node);
4400 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
4401 and then Nkind (Parent (Parent (Call_Node))) =
4402 N_Assignment_Statement
4403 then
4404 Ass := Parent (Parent (Call_Node));
4406 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
4407 and then Nkind (Parent (Parent (Call_Node))) =
4408 N_Assignment_Statement
4409 then
4410 Ass := Parent (Parent (Call_Node));
4411 end if;
4413 if Present (Ass)
4414 and then Is_Class_Wide_Type (Etype (Name (Ass)))
4415 then
4416 -- Move the error messages below to sem???
4418 if Is_Access_Type (Etype (Call_Node)) then
4419 if Designated_Type (Etype (Call_Node)) /=
4420 Root_Type (Etype (Name (Ass)))
4421 then
4422 Error_Msg_NE
4423 ("tag-indeterminate expression must have designated "
4424 & "type& (RM 5.2 (6))",
4425 Call_Node, Root_Type (Etype (Name (Ass))));
4426 else
4427 Propagate_Tag (Name (Ass), Call_Node);
4428 end if;
4430 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
4431 Error_Msg_NE
4432 ("tag-indeterminate expression must have type & "
4433 & "(RM 5.2 (6))",
4434 Call_Node, Root_Type (Etype (Name (Ass))));
4436 else
4437 Propagate_Tag (Name (Ass), Call_Node);
4438 end if;
4440 -- The call will be rewritten as a dispatching call, and
4441 -- expanded as such.
4443 return;
4444 end if;
4445 end;
4446 end if;
4448 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
4449 -- it to point to the correct secondary virtual table.
4451 if Nkind (Call_Node) in N_Subprogram_Call
4452 and then CW_Interface_Formals_Present
4453 then
4454 Expand_Interface_Actuals (Call_Node);
4455 end if;
4457 -- Install class-wide preconditions runtime check when this is a
4458 -- dispatching primitive that has or inherits class-wide preconditions;
4459 -- otherwise no runtime check is installed.
4461 if Nkind (Call_Node) in N_Subprogram_Call
4462 and then Is_Dispatching_Operation (Subp)
4463 then
4464 Install_Class_Preconditions_Check (Call_Node);
4465 end if;
4467 -- Deals with Dispatch_Call if we still have a call, before expanding
4468 -- extra actuals since this will be done on the re-analysis of the
4469 -- dispatching call. Note that we do not try to shorten the actual list
4470 -- for a dispatching call, it would not make sense to do so. Expansion
4471 -- of dispatching calls is suppressed for VM targets, because the VM
4472 -- back-ends directly handle the generation of dispatching calls and
4473 -- would have to undo any expansion to an indirect call.
4475 if Nkind (Call_Node) in N_Subprogram_Call
4476 and then Present (Controlling_Argument (Call_Node))
4477 then
4478 if Tagged_Type_Expansion then
4479 Expand_Dispatching_Call (Call_Node);
4481 -- Expand_Dispatching_Call takes care of all the needed processing
4483 return;
4484 end if;
4486 -- VM targets
4488 declare
4489 Call_Typ : constant Entity_Id := Etype (Call_Node);
4490 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
4491 Eq_Prim_Op : Entity_Id := Empty;
4492 New_Call : Node_Id;
4493 Param : Node_Id;
4494 Prev_Call : Node_Id;
4496 begin
4497 Apply_Tag_Checks (Call_Node);
4499 if not Is_Limited_Type (Typ) then
4500 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
4501 end if;
4503 -- If this is a dispatching "=", we must first compare the
4504 -- tags so we generate: x.tag = y.tag and then x = y
4506 if Subp = Eq_Prim_Op then
4508 -- Mark the node as analyzed to avoid reanalyzing this
4509 -- dispatching call (which would cause a never-ending loop)
4511 Prev_Call := Relocate_Node (Call_Node);
4512 Set_Analyzed (Prev_Call);
4514 Param := First_Actual (Call_Node);
4515 New_Call :=
4516 Make_And_Then (Loc,
4517 Left_Opnd =>
4518 Make_Op_Eq (Loc,
4519 Left_Opnd =>
4520 Make_Selected_Component (Loc,
4521 Prefix => New_Value (Param),
4522 Selector_Name =>
4523 New_Occurrence_Of
4524 (First_Tag_Component (Typ), Loc)),
4526 Right_Opnd =>
4527 Make_Selected_Component (Loc,
4528 Prefix =>
4529 Unchecked_Convert_To (Typ,
4530 New_Value (Next_Actual (Param))),
4531 Selector_Name =>
4532 New_Occurrence_Of
4533 (First_Tag_Component (Typ), Loc))),
4534 Right_Opnd => Prev_Call);
4536 Rewrite (Call_Node, New_Call);
4537 Analyze_And_Resolve
4538 (Call_Node, Call_Typ, Suppress => All_Checks);
4539 end if;
4541 -- Expansion of a dispatching call results in an indirect call,
4542 -- which in turn causes current values to be killed (see
4543 -- Resolve_Call), so on VM targets we do the call here to
4544 -- ensure consistent warnings between VM and non-VM targets.
4546 Kill_Current_Values;
4548 -- If this is a dispatching "=" then we must update the reference
4549 -- to the call node because we generated:
4550 -- x.tag = y.tag and then x = y
4552 if Subp = Eq_Prim_Op then
4553 Call_Node := Right_Opnd (Call_Node);
4554 end if;
4555 end;
4556 end if;
4558 -- Similarly, expand calls to RCI subprograms on which pragma
4559 -- All_Calls_Remote applies. The rewriting will be reanalyzed
4560 -- later. Do this only when the call comes from source since we
4561 -- do not want such a rewriting to occur in expanded code.
4563 if Is_All_Remote_Call (Call_Node) then
4564 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
4566 -- Similarly, do not add extra actuals for an entry call whose entity
4567 -- is a protected procedure, or for an internal protected subprogram
4568 -- call, because it will be rewritten as a protected subprogram call
4569 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
4571 elsif Is_Protected_Type (Scope (Subp))
4572 and then Ekind (Subp) in E_Procedure | E_Function
4573 then
4574 null;
4576 -- During that loop we gathered the extra actuals (the ones that
4577 -- correspond to Extra_Formals), so now they can be appended.
4579 else
4580 while Is_Non_Empty_List (Extra_Actuals) loop
4581 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
4582 end loop;
4583 end if;
4585 -- At this point we have all the actuals, so this is the point at which
4586 -- the various expansion activities for actuals is carried out.
4588 Expand_Actuals (Call_Node, Subp, Post_Call);
4590 -- If it is a recursive call then call the internal procedure that
4591 -- verifies Subprogram_Variant contract (if present and enabled).
4592 -- Detecting calls to subprogram aliases is necessary for recursive
4593 -- calls in instances of generic subprograms, where the renaming of
4594 -- the current subprogram is called.
4596 if Is_Subprogram (Subp)
4597 and then not Is_Ignored_Ghost_Entity (Subp)
4598 and then Same_Or_Aliased_Subprograms (Subp, Current_Scope)
4599 then
4600 Check_Subprogram_Variant;
4601 end if;
4603 -- Verify that the actuals do not share storage. This check must be done
4604 -- on the caller side rather that inside the subprogram to avoid issues
4605 -- of parameter passing.
4607 if Check_Aliasing_Of_Parameters then
4608 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
4609 end if;
4611 -- If the subprogram is a renaming, or if it is inherited, replace it in
4612 -- the call with the name of the actual subprogram being called. If this
4613 -- is a dispatching call, the run-time decides what to call. The Alias
4614 -- attribute does not apply to entries.
4616 if Nkind (Call_Node) /= N_Entry_Call_Statement
4617 and then No (Controlling_Argument (Call_Node))
4618 and then Present (Parent_Subp)
4619 and then not Is_Direct_Deep_Call (Subp)
4620 then
4621 if Present (Inherited_From_Formal (Subp)) then
4622 Parent_Subp := Inherited_From_Formal (Subp);
4623 else
4624 Parent_Subp := Ultimate_Alias (Parent_Subp);
4625 end if;
4627 -- The below setting of Entity is suspect, see F109-018 discussion???
4629 Set_Entity (Name (Call_Node), Parent_Subp);
4631 -- Inspect all formals of derived subprogram Subp. Compare parameter
4632 -- types with the parent subprogram and check whether an actual may
4633 -- need a type conversion to the corresponding formal of the parent
4634 -- subprogram.
4636 -- Not clear whether intrinsic subprograms need such conversions. ???
4638 if not Is_Intrinsic_Subprogram (Parent_Subp)
4639 or else Is_Generic_Instance (Parent_Subp)
4640 then
4641 declare
4642 procedure Convert (Act : Node_Id; Typ : Entity_Id);
4643 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
4644 -- and resolve the newly generated construct.
4646 -------------
4647 -- Convert --
4648 -------------
4650 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
4651 begin
4652 Rewrite (Act, OK_Convert_To (Typ, Act));
4653 Analyze_And_Resolve (Act, Typ);
4654 end Convert;
4656 -- Local variables
4658 Actual_Typ : Entity_Id;
4659 Formal_Typ : Entity_Id;
4660 Parent_Typ : Entity_Id;
4662 begin
4663 Actual := First_Actual (Call_Node);
4664 Formal := First_Formal (Subp);
4665 Parent_Formal := First_Formal (Parent_Subp);
4666 while Present (Formal) loop
4667 Actual_Typ := Etype (Actual);
4668 Formal_Typ := Etype (Formal);
4669 Parent_Typ := Etype (Parent_Formal);
4671 -- For an IN parameter of a scalar type, the derived formal
4672 -- type and parent formal type differ, and the parent formal
4673 -- type and actual type do not match statically.
4675 if Is_Scalar_Type (Formal_Typ)
4676 and then Ekind (Formal) = E_In_Parameter
4677 and then Formal_Typ /= Parent_Typ
4678 and then
4679 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
4680 and then not Raises_Constraint_Error (Actual)
4681 then
4682 Convert (Actual, Parent_Typ);
4684 -- For access types, the parent formal type and actual type
4685 -- differ.
4687 elsif Is_Access_Type (Formal_Typ)
4688 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
4689 then
4690 if Ekind (Formal) /= E_In_Parameter then
4691 Convert (Actual, Parent_Typ);
4693 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
4694 and then Designated_Type (Parent_Typ) /=
4695 Designated_Type (Actual_Typ)
4696 and then not Is_Controlling_Formal (Formal)
4697 then
4698 -- This unchecked conversion is not necessary unless
4699 -- inlining is enabled, because in that case the type
4700 -- mismatch may become visible in the body about to be
4701 -- inlined.
4703 Rewrite (Actual,
4704 Unchecked_Convert_To (Parent_Typ, Actual));
4705 Analyze_And_Resolve (Actual, Parent_Typ);
4706 end if;
4708 -- If there is a change of representation, then generate a
4709 -- warning, and do the change of representation.
4711 elsif not Has_Compatible_Representation
4712 (Target_Typ => Formal_Typ,
4713 Operand_Typ => Parent_Typ)
4714 then
4715 Error_Msg_N
4716 ("??change of representation required", Actual);
4717 Convert (Actual, Parent_Typ);
4719 -- For array and record types, the parent formal type and
4720 -- derived formal type have different sizes or pragma Pack
4721 -- status.
4723 elsif ((Is_Array_Type (Formal_Typ)
4724 and then Is_Array_Type (Parent_Typ))
4725 or else
4726 (Is_Record_Type (Formal_Typ)
4727 and then Is_Record_Type (Parent_Typ)))
4728 and then Known_Esize (Formal_Typ)
4729 and then Known_Esize (Parent_Typ)
4730 and then
4731 (Esize (Formal_Typ) /= Esize (Parent_Typ)
4732 or else Has_Pragma_Pack (Formal_Typ) /=
4733 Has_Pragma_Pack (Parent_Typ))
4734 then
4735 Convert (Actual, Parent_Typ);
4736 end if;
4738 Next_Actual (Actual);
4739 Next_Formal (Formal);
4740 Next_Formal (Parent_Formal);
4741 end loop;
4742 end;
4743 end if;
4745 Orig_Subp := Subp;
4746 Subp := Parent_Subp;
4747 end if;
4749 -- Deal with case where call is an explicit dereference
4751 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
4753 -- Handle case of access to protected subprogram type
4755 if Is_Access_Protected_Subprogram_Type
4756 (Base_Type (Etype (Prefix (Name (Call_Node)))))
4757 then
4758 -- If this is a call through an access to protected operation, the
4759 -- prefix has the form (object'address, operation'access). Rewrite
4760 -- as a for other protected calls: the object is the 1st parameter
4761 -- of the list of actuals.
4763 declare
4764 Call : Node_Id;
4765 Parm : List_Id;
4766 Nam : Node_Id;
4767 Obj : Node_Id;
4768 Ptr : constant Node_Id := Prefix (Name (Call_Node));
4770 T : constant Entity_Id :=
4771 Equivalent_Type (Base_Type (Etype (Ptr)));
4773 D_T : constant Entity_Id :=
4774 Designated_Type (Base_Type (Etype (Ptr)));
4776 begin
4777 Obj :=
4778 Make_Selected_Component (Loc,
4779 Prefix => Unchecked_Convert_To (T, Ptr),
4780 Selector_Name =>
4781 New_Occurrence_Of (First_Entity (T), Loc));
4783 Nam :=
4784 Make_Selected_Component (Loc,
4785 Prefix => Unchecked_Convert_To (T, Ptr),
4786 Selector_Name =>
4787 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
4789 Nam :=
4790 Make_Explicit_Dereference (Loc,
4791 Prefix => Nam);
4793 if Present (Parameter_Associations (Call_Node)) then
4794 Parm := Parameter_Associations (Call_Node);
4795 else
4796 Parm := New_List;
4797 end if;
4799 Prepend (Obj, Parm);
4801 if Etype (D_T) = Standard_Void_Type then
4802 Call :=
4803 Make_Procedure_Call_Statement (Loc,
4804 Name => Nam,
4805 Parameter_Associations => Parm);
4806 else
4807 Call :=
4808 Make_Function_Call (Loc,
4809 Name => Nam,
4810 Parameter_Associations => Parm);
4811 end if;
4813 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
4814 Set_Etype (Call, Etype (D_T));
4816 -- We do not re-analyze the call to avoid infinite recursion.
4817 -- We analyze separately the prefix and the object, and set
4818 -- the checks on the prefix that would otherwise be emitted
4819 -- when resolving a call.
4821 Rewrite (Call_Node, Call);
4822 Analyze (Nam);
4823 Apply_Access_Check (Nam);
4824 Analyze (Obj);
4825 return;
4826 end;
4827 end if;
4828 end if;
4830 -- If this is a call to an intrinsic subprogram, then perform the
4831 -- appropriate expansion to the corresponding tree node and we
4832 -- are all done (since after that the call is gone).
4834 -- In the case where the intrinsic is to be processed by the back end,
4835 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
4836 -- since the idea in this case is to pass the call unchanged. If the
4837 -- intrinsic is an inherited unchecked conversion, and the derived type
4838 -- is the target type of the conversion, we must retain it as the return
4839 -- type of the expression. Otherwise the expansion below, which uses the
4840 -- parent operation, will yield the wrong type.
4842 if Is_Intrinsic_Subprogram (Subp) then
4843 Expand_Intrinsic_Call (Call_Node, Subp);
4845 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
4846 and then Parent_Subp /= Orig_Subp
4847 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
4848 then
4849 Set_Etype (Call_Node, Etype (Orig_Subp));
4850 end if;
4852 return;
4853 end if;
4855 if Ekind (Subp) in E_Function | E_Procedure then
4857 -- We perform a simple optimization on calls for To_Address by
4858 -- replacing them with an unchecked conversion. Not only is this
4859 -- efficient, but it also avoids order of elaboration problems when
4860 -- address clauses are inlined (address expression elaborated at the
4861 -- wrong point).
4863 -- We perform this optimization regardless of whether we are in the
4864 -- main unit or in a unit in the context of the main unit, to ensure
4865 -- that the generated tree is the same in both cases, for CodePeer
4866 -- use.
4868 if Is_RTE (Subp, RE_To_Address) then
4869 Rewrite (Call_Node,
4870 Unchecked_Convert_To
4871 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
4872 return;
4874 -- A call to a null procedure is replaced by a null statement, but we
4875 -- are not allowed to ignore possible side effects of the call, so we
4876 -- make sure that actuals are evaluated.
4877 -- We also suppress this optimization for GNATcoverage.
4879 elsif Is_Null_Procedure (Subp)
4880 and then not Opt.Suppress_Control_Flow_Optimizations
4881 then
4882 Actual := First_Actual (Call_Node);
4883 while Present (Actual) loop
4884 Remove_Side_Effects (Actual);
4885 Next_Actual (Actual);
4886 end loop;
4888 Rewrite (Call_Node, Make_Null_Statement (Loc));
4889 return;
4890 end if;
4892 -- Handle inlining. No action needed if the subprogram is not inlined
4894 if not Is_Inlined (Subp) then
4895 null;
4897 -- Front-end inlining of expression functions (performed also when
4898 -- back-end inlining is enabled).
4900 elsif Is_Inlinable_Expression_Function (Subp) then
4901 Rewrite
4902 (Call_Node, New_Copy (Expression_Of_Expression_Function (Subp)));
4903 Analyze (Call_Node);
4904 return;
4906 -- Handle front-end inlining
4908 elsif not Back_End_Inlining then
4909 Inlined_Subprogram : declare
4910 Bod : Node_Id;
4911 Must_Inline : Boolean := False;
4912 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
4914 begin
4915 -- Verify that the body to inline has already been seen, and
4916 -- that if the body is in the current unit the inlining does
4917 -- not occur earlier. This avoids order-of-elaboration problems
4918 -- in the back end.
4920 -- This should be documented in sinfo/einfo ???
4922 if No (Spec)
4923 or else Nkind (Spec) /= N_Subprogram_Declaration
4924 or else No (Body_To_Inline (Spec))
4925 then
4926 Must_Inline := False;
4928 -- If this an inherited function that returns a private type,
4929 -- do not inline if the full view is an unconstrained array,
4930 -- because such calls cannot be inlined.
4932 elsif Present (Orig_Subp)
4933 and then Is_Array_Type (Etype (Orig_Subp))
4934 and then not Is_Constrained (Etype (Orig_Subp))
4935 then
4936 Must_Inline := False;
4938 elsif In_Unfrozen_Instance (Scope (Subp)) then
4939 Must_Inline := False;
4941 else
4942 Bod := Body_To_Inline (Spec);
4944 if (In_Extended_Main_Code_Unit (Call_Node)
4945 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
4946 or else Has_Pragma_Inline_Always (Subp))
4947 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
4948 or else
4949 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
4950 then
4951 Must_Inline := True;
4953 -- If we are compiling a package body that is not the main
4954 -- unit, it must be for inlining/instantiation purposes,
4955 -- in which case we inline the call to insure that the same
4956 -- temporaries are generated when compiling the body by
4957 -- itself. Otherwise link errors can occur.
4959 -- If the function being called is itself in the main unit,
4960 -- we cannot inline, because there is a risk of double
4961 -- elaboration and/or circularity: the inlining can make
4962 -- visible a private entity in the body of the main unit,
4963 -- that gigi will see before its sees its proper definition.
4965 elsif not In_Extended_Main_Code_Unit (Call_Node)
4966 and then In_Package_Body
4967 then
4968 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4970 -- Inline calls to _Wrapped_Statements when generating C
4972 elsif Modify_Tree_For_C
4973 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4974 and then Chars (Name (Call_Node))
4975 = Name_uWrapped_Statements
4976 then
4977 Must_Inline := True;
4978 end if;
4979 end if;
4981 if Must_Inline then
4982 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4984 else
4985 -- Let the back end handle it
4987 Add_Inlined_Body (Subp, Call_Node);
4989 if Front_End_Inlining
4990 and then Nkind (Spec) = N_Subprogram_Declaration
4991 and then In_Extended_Main_Code_Unit (Call_Node)
4992 and then No (Body_To_Inline (Spec))
4993 and then not Has_Completion (Subp)
4994 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4995 then
4996 Cannot_Inline
4997 ("cannot inline& (body not seen yet)?",
4998 Call_Node, Subp);
4999 end if;
5000 end if;
5001 end Inlined_Subprogram;
5003 -- Front-end expansion of simple functions returning unconstrained
5004 -- types (see Check_And_Split_Unconstrained_Function). Note that the
5005 -- case of a simple renaming (Body_To_Inline in N_Entity below, see
5006 -- also Build_Renamed_Body) cannot be expanded here because this may
5007 -- give rise to order-of-elaboration issues for the types of the
5008 -- parameters of the subprogram, if any.
5010 elsif Present (Unit_Declaration_Node (Subp))
5011 and then Nkind (Unit_Declaration_Node (Subp)) =
5012 N_Subprogram_Declaration
5013 and then Present (Body_To_Inline (Unit_Declaration_Node (Subp)))
5014 and then
5015 Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) not in
5016 N_Entity
5017 then
5018 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
5020 -- Back-end inlining either if optimization is enabled or the call is
5021 -- required to be inlined.
5023 elsif Optimization_Level > 0
5024 or else Has_Pragma_Inline_Always (Subp)
5025 then
5026 Add_Inlined_Body (Subp, Call_Node);
5027 end if;
5028 end if;
5030 -- Check for protected subprogram. This is either an intra-object call,
5031 -- or a protected function call. Protected procedure calls are rewritten
5032 -- as entry calls and handled accordingly.
5034 -- In Ada 2005, this may be an indirect call to an access parameter that
5035 -- is an access_to_subprogram. In that case the anonymous type has a
5036 -- scope that is a protected operation, but the call is a regular one.
5037 -- In either case do not expand call if subprogram is eliminated.
5039 Scop := Scope (Subp);
5041 if Nkind (Call_Node) /= N_Entry_Call_Statement
5042 and then Is_Protected_Type (Scop)
5043 and then Ekind (Subp) /= E_Subprogram_Type
5044 and then not Is_Eliminated (Subp)
5045 then
5046 -- If the call is an internal one, it is rewritten as a call to the
5047 -- corresponding unprotected subprogram.
5049 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
5050 end if;
5052 -- Functions returning controlled objects need special attention. If
5053 -- the return type is limited, then the context is initialization and
5054 -- different processing applies. If the call is to a protected function,
5055 -- the expansion above will call Expand_Call recursively. Otherwise the
5056 -- function call is transformed into a reference to the result that has
5057 -- been built either on the primary or the secondary stack.
5059 if Needs_Finalization (Etype (Subp)) then
5060 if not Is_Build_In_Place_Function_Call (Call_Node)
5061 and then
5062 (No (First_Formal (Subp))
5063 or else
5064 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
5065 then
5066 Expand_Ctrl_Function_Call
5067 (Call_Node, Needs_Secondary_Stack (Etype (Subp)));
5069 -- Build-in-place function calls which appear in anonymous contexts
5070 -- need a transient scope to ensure the proper finalization of the
5071 -- intermediate result after its use.
5073 elsif Is_Build_In_Place_Function_Call (Call_Node)
5074 and then Nkind (Parent (Unqual_Conv (Call_Node))) in
5075 N_Attribute_Reference
5076 | N_Function_Call
5077 | N_Indexed_Component
5078 | N_Object_Renaming_Declaration
5079 | N_Procedure_Call_Statement
5080 | N_Selected_Component
5081 | N_Slice
5082 and then
5083 (Ekind (Current_Scope) /= E_Loop
5084 or else Nkind (Parent (Call_Node)) /= N_Function_Call
5085 or else not
5086 Is_Build_In_Place_Function_Call (Parent (Call_Node)))
5087 then
5088 Establish_Transient_Scope
5089 (Call_Node, Needs_Secondary_Stack (Etype (Subp)));
5090 end if;
5091 end if;
5092 end Expand_Call_Helper;
5094 -------------------------------
5095 -- Expand_Ctrl_Function_Call --
5096 -------------------------------
5098 procedure Expand_Ctrl_Function_Call (N : Node_Id; Use_Sec_Stack : Boolean)
5100 Par : constant Node_Id := Parent (N);
5102 function Is_Element_Reference (N : Node_Id) return Boolean;
5103 -- Determine whether node N denotes a reference to an Ada 2012 container
5104 -- element.
5106 --------------------------
5107 -- Is_Element_Reference --
5108 --------------------------
5110 function Is_Element_Reference (N : Node_Id) return Boolean is
5111 Ref : constant Node_Id := Original_Node (N);
5113 begin
5114 -- Analysis marks an element reference by setting the generalized
5115 -- indexing attribute of an indexed component before the component
5116 -- is rewritten into a function call.
5118 return
5119 Nkind (Ref) = N_Indexed_Component
5120 and then Present (Generalized_Indexing (Ref));
5121 end Is_Element_Reference;
5123 -- Start of processing for Expand_Ctrl_Function_Call
5125 begin
5126 -- Optimization: if the returned value is returned again, then no need
5127 -- to copy/readjust/finalize, we can just pass the value through (see
5128 -- Expand_N_Simple_Return_Statement), and thus no attachment is needed.
5130 if Nkind (Par) = N_Simple_Return_Statement then
5131 return;
5132 end if;
5134 -- Another optimization: if the returned value is used to initialize an
5135 -- object, then no need to copy/readjust/finalize, we can initialize it
5136 -- in place. However, if the call returns on the secondary stack, then
5137 -- we need the expansion because we'll be renaming the temporary as the
5138 -- (permanent) object.
5140 if Nkind (Par) = N_Object_Declaration and then not Use_Sec_Stack then
5141 return;
5142 end if;
5144 -- Resolution is now finished, make sure we don't start analysis again
5145 -- because of the duplication.
5147 Set_Analyzed (N);
5149 -- Apply the transformation, unless it was already applied manually
5151 if Nkind (Par) /= N_Reference then
5152 Remove_Side_Effects (N);
5153 end if;
5155 -- The side effect removal of the function call produced a temporary.
5156 -- When the context is a case expression, if expression, or expression
5157 -- with actions, the lifetime of the temporary must be extended to match
5158 -- that of the context. Otherwise the function result will be finalized
5159 -- too early and affect the result of the expression. To prevent this
5160 -- unwanted effect, the temporary should not be considered for clean up
5161 -- actions by the general finalization machinery.
5163 -- Exception to this rule are references to Ada 2012 container elements.
5164 -- Such references must be finalized at the end of each iteration of the
5165 -- related quantified expression, otherwise the container will remain
5166 -- busy.
5168 if Nkind (N) = N_Explicit_Dereference
5169 and then Within_Case_Or_If_Expression (N)
5170 and then not Is_Element_Reference (N)
5171 then
5172 Set_Is_Ignored_Transient (Entity (Prefix (N)));
5173 end if;
5174 end Expand_Ctrl_Function_Call;
5176 ----------------------------------------
5177 -- Expand_N_Extended_Return_Statement --
5178 ----------------------------------------
5180 -- If there is a Handled_Statement_Sequence, we rewrite this:
5182 -- return Result : T := <expression> do
5183 -- <handled_seq_of_stms>
5184 -- end return;
5186 -- to be:
5188 -- declare
5189 -- Result : T := <expression>;
5190 -- begin
5191 -- <handled_seq_of_stms>
5192 -- return Result;
5193 -- end;
5195 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5197 -- return Result : T := <expression>;
5199 -- to be:
5201 -- return <expression>;
5203 -- unless it's build-in-place or there's no <expression>, in which case
5204 -- we generate:
5206 -- declare
5207 -- Result : T := <expression>;
5208 -- begin
5209 -- return Result;
5210 -- end;
5212 -- Note that this case could have been written by the user as an extended
5213 -- return statement, or could have been transformed to this from a simple
5214 -- return statement.
5216 -- That is, we need to have a reified return object if there are statements
5217 -- (which might refer to it) or if we're doing build-in-place (so we can
5218 -- set its address to the final resting place or if there is no expression
5219 -- (in which case default initial values might need to be set)).
5221 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
5222 Loc : constant Source_Ptr := Sloc (N);
5223 Func_Id : constant Entity_Id :=
5224 Return_Applies_To (Return_Statement_Entity (N));
5225 Is_BIP_Func : constant Boolean :=
5226 Is_Build_In_Place_Function (Func_Id);
5227 Ret_Obj_Id : constant Entity_Id :=
5228 First_Entity (Return_Statement_Entity (N));
5229 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
5230 Ret_Typ : constant Entity_Id := Etype (Func_Id);
5232 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
5233 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5234 -- with parameters:
5235 -- From current activation chain
5236 -- To activation chain passed in by the caller
5237 -- New_Master master passed in by the caller
5239 -- Func_Id is the entity of the function where the extended return
5240 -- statement appears.
5242 ---------------------------
5243 -- Move_Activation_Chain --
5244 ---------------------------
5246 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
5247 begin
5248 return
5249 Make_Procedure_Call_Statement (Loc,
5250 Name =>
5251 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
5253 Parameter_Associations => New_List (
5255 -- Source chain
5257 Make_Attribute_Reference (Loc,
5258 Prefix => Make_Identifier (Loc, Name_uChain),
5259 Attribute_Name => Name_Unrestricted_Access),
5261 -- Destination chain
5263 New_Occurrence_Of
5264 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
5266 -- New master
5268 New_Occurrence_Of
5269 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
5270 end Move_Activation_Chain;
5272 -- Local variables
5274 Exp : Node_Id;
5275 HSS : Node_Id;
5276 Result : Node_Id;
5277 Stmts : List_Id := No_List;
5279 Return_Stmt : Node_Id := Empty;
5280 -- Force initialization to facilitate static analysis
5282 -- Start of processing for Expand_N_Extended_Return_Statement
5284 begin
5285 -- Given that functionality of interface thunks is simple (just displace
5286 -- the pointer to the object) they are always handled by means of
5287 -- simple return statements.
5289 pragma Assert (not Is_Thunk (Current_Subprogram));
5291 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
5292 Exp := Expression (Ret_Obj_Decl);
5294 -- Assert that if F says "return R : T := G(...) do..."
5295 -- then F and G are both b-i-p, or neither b-i-p.
5297 if Present (Exp) and then Nkind (Exp) = N_Function_Call then
5298 pragma Assert (Ekind (Current_Subprogram) = E_Function);
5299 pragma Assert
5300 (Is_Build_In_Place_Function (Current_Subprogram) =
5301 Is_Build_In_Place_Function_Call (Exp));
5302 null;
5303 end if;
5305 else
5306 Exp := Empty;
5307 end if;
5309 HSS := Handled_Statement_Sequence (N);
5311 -- If the returned object needs finalization actions, the function must
5312 -- perform the appropriate cleanup should it fail to return. The state
5313 -- of the function itself is tracked through a flag which is coupled
5314 -- with the scope finalizer. There is one flag per each return object
5315 -- in case of multiple returns.
5317 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
5318 declare
5319 Flag_Decl : Node_Id;
5320 Flag_Id : Entity_Id;
5321 Func_Bod : Node_Id;
5323 begin
5324 -- Recover the function body
5326 Func_Bod := Unit_Declaration_Node (Func_Id);
5328 if Nkind (Func_Bod) = N_Subprogram_Declaration then
5329 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
5330 end if;
5332 if Nkind (Func_Bod) = N_Function_Specification then
5333 Func_Bod := Parent (Func_Bod); -- one more level for child units
5334 end if;
5336 pragma Assert (Nkind (Func_Bod) = N_Subprogram_Body);
5338 -- Create a flag to track the function state
5340 Flag_Id := Make_Temporary (Loc, 'F');
5341 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
5343 -- Insert the flag at the beginning of the function declarations,
5344 -- generate:
5345 -- Fnn : Boolean := False;
5347 Flag_Decl :=
5348 Make_Object_Declaration (Loc,
5349 Defining_Identifier => Flag_Id,
5350 Object_Definition =>
5351 New_Occurrence_Of (Standard_Boolean, Loc),
5352 Expression =>
5353 New_Occurrence_Of (Standard_False, Loc));
5355 Prepend_To (Declarations (Func_Bod), Flag_Decl);
5356 Analyze (Flag_Decl);
5357 end;
5358 end if;
5360 -- Build a simple_return_statement that returns the return object when
5361 -- there is a statement sequence, or no expression, or the analysis of
5362 -- the return object declaration generated extra actions, or the result
5363 -- will be built in place. Note however that we currently do this for
5364 -- all composite cases, even though they are not built in place.
5366 if Present (HSS)
5367 or else No (Exp)
5368 or else List_Length (Return_Object_Declarations (N)) > 1
5369 or else Is_Composite_Type (Ret_Typ)
5370 then
5371 if No (HSS) then
5372 Stmts := New_List;
5374 -- If the extended return has a handled statement sequence, then wrap
5375 -- it in a block and use the block as the first statement.
5377 else
5378 Stmts := New_List (
5379 Make_Block_Statement (Loc,
5380 Declarations => New_List,
5381 Handled_Statement_Sequence => HSS));
5382 end if;
5384 -- If the result type contains tasks, we call Move_Activation_Chain.
5385 -- Later, the cleanup code will call Complete_Master, which will
5386 -- terminate any unactivated tasks belonging to the return statement
5387 -- master. But Move_Activation_Chain updates their master to be that
5388 -- of the caller, so they will not be terminated unless the return
5389 -- statement completes unsuccessfully due to exception, abort, goto,
5390 -- or exit. As a formality, we test whether the function requires the
5391 -- result to be built in place, though that's necessarily true for
5392 -- the case of result types with task parts.
5394 if Is_BIP_Func and then Has_Task (Ret_Typ) then
5396 -- The return expression is an aggregate for a complex type which
5397 -- contains tasks. This particular case is left unexpanded since
5398 -- the regular expansion would insert all temporaries and
5399 -- initialization code in the wrong block.
5401 if Nkind (Exp) = N_Aggregate then
5402 Expand_N_Aggregate (Exp);
5403 end if;
5405 -- Do not move the activation chain if the return object does not
5406 -- contain tasks.
5408 if Has_Task (Etype (Ret_Obj_Id)) then
5409 Append_To (Stmts, Move_Activation_Chain (Func_Id));
5410 end if;
5411 end if;
5413 -- Update the state of the function right before the object is
5414 -- returned.
5416 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
5417 declare
5418 Flag_Id : constant Entity_Id :=
5419 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
5421 begin
5422 -- Generate:
5423 -- Fnn := True;
5425 Append_To (Stmts,
5426 Make_Assignment_Statement (Loc,
5427 Name => New_Occurrence_Of (Flag_Id, Loc),
5428 Expression => New_Occurrence_Of (Standard_True, Loc)));
5429 end;
5430 end if;
5432 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
5433 end if;
5435 -- Case where we build a return statement block
5437 if Present (HSS) then
5438 Result :=
5439 Make_Block_Statement (Loc,
5440 Declarations => Return_Object_Declarations (N),
5441 Handled_Statement_Sequence => HSS);
5443 -- We set the entity of the new block statement to be that of the
5444 -- return statement. This is necessary so that various fields, such
5445 -- as Finalization_Chain_Entity carry over from the return statement
5446 -- to the block. Note that this block is unusual, in that its entity
5447 -- is an E_Return_Statement rather than an E_Block.
5449 Set_Identifier
5450 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
5452 -- Build a simple_return_statement that returns the return object
5454 Return_Stmt :=
5455 Make_Simple_Return_Statement (Loc,
5456 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
5457 Append_To (Stmts, Return_Stmt);
5459 -- Case where we do not need to build a block. But we're about to drop
5460 -- Return_Object_Declarations on the floor, so assert that it contains
5461 -- only the return object declaration.
5463 else pragma Assert (List_Length (Return_Object_Declarations (N)) = 1);
5465 -- Build simple_return_statement that returns the expression directly
5467 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5468 Result := Return_Stmt;
5469 end if;
5471 -- Set the flag to prevent infinite recursion
5473 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5474 Set_Return_Statement (Ret_Obj_Id, Return_Stmt);
5476 Rewrite (N, Result);
5478 -- AI12-043: The checks of 6.5(8.1/3) and 6.5(21/3) are made immediately
5479 -- before an object is returned. A predicate that applies to the return
5480 -- subtype is checked immediately before an object is returned.
5482 Analyze (N);
5483 end Expand_N_Extended_Return_Statement;
5485 ----------------------------
5486 -- Expand_N_Function_Call --
5487 ----------------------------
5489 procedure Expand_N_Function_Call (N : Node_Id) is
5490 begin
5491 Expand_Call (N);
5492 end Expand_N_Function_Call;
5494 ---------------------------------------
5495 -- Expand_N_Procedure_Call_Statement --
5496 ---------------------------------------
5498 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5499 begin
5500 Expand_Call (N);
5501 end Expand_N_Procedure_Call_Statement;
5503 ------------------------------------
5504 -- Expand_N_Return_When_Statement --
5505 ------------------------------------
5507 procedure Expand_N_Return_When_Statement (N : Node_Id) is
5508 Loc : constant Source_Ptr := Sloc (N);
5509 begin
5510 Rewrite (N,
5511 Make_If_Statement (Loc,
5512 Condition => Condition (N),
5513 Then_Statements => New_List (
5514 Make_Simple_Return_Statement (Loc,
5515 Expression => Expression (N)))));
5517 Analyze (N);
5518 end Expand_N_Return_When_Statement;
5520 --------------------------------------
5521 -- Expand_N_Simple_Return_Statement --
5522 --------------------------------------
5524 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5525 begin
5526 -- Defend against previous errors (i.e. the return statement calls a
5527 -- function that is not available in configurable runtime).
5529 if Present (Expression (N))
5530 and then Nkind (Expression (N)) = N_Empty
5531 then
5532 Check_Error_Detected;
5533 return;
5534 end if;
5536 -- Distinguish the function and non-function cases:
5538 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5539 when E_Function
5540 | E_Generic_Function
5542 Expand_Simple_Function_Return (N);
5544 when E_Entry
5545 | E_Entry_Family
5546 | E_Generic_Procedure
5547 | E_Procedure
5548 | E_Return_Statement
5550 Expand_Non_Function_Return (N);
5552 when others =>
5553 raise Program_Error;
5554 end case;
5556 exception
5557 when RE_Not_Available =>
5558 return;
5559 end Expand_N_Simple_Return_Statement;
5561 ------------------------------
5562 -- Expand_N_Subprogram_Body --
5563 ------------------------------
5565 -- Add dummy push/pop label nodes at start and end to clear any local
5566 -- exception indications if local-exception-to-goto optimization is active.
5568 -- Add return statement if last statement in body is not a return statement
5569 -- (this makes things easier on Gigi which does not want to have to handle
5570 -- a missing return).
5572 -- Add call to Activate_Tasks if body is a task activator
5574 -- Deal with possible detection of infinite recursion
5576 -- Eliminate body completely if convention stubbed
5578 -- Encode entity names within body, since we will not need to reference
5579 -- these entities any longer in the front end.
5581 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5583 -- Reset Pure indication if any parameter has root type System.Address
5584 -- or has any parameters of limited types, where limited means that the
5585 -- run-time view is limited (i.e. the full type is limited).
5587 -- Wrap thread body
5589 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5590 Body_Id : constant Entity_Id := Defining_Entity (N);
5591 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5592 Loc : constant Source_Ptr := Sloc (N);
5594 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5595 -- Append a return statement to the statement sequence Stmts if the last
5596 -- statement is not already a return or a goto statement. Note that the
5597 -- latter test is not critical, it does not matter if we add a few extra
5598 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5599 -- the corresponding spec of the subprogram body.
5601 ----------------
5602 -- Add_Return --
5603 ----------------
5605 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5606 Last_Stmt : Node_Id;
5607 Loc : Source_Ptr;
5608 Stmt : Node_Id;
5610 begin
5611 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5612 -- not relevant in this context since they are not executable.
5614 Last_Stmt := Last (Stmts);
5615 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5616 Prev (Last_Stmt);
5617 end loop;
5619 -- Now insert return unless last statement is a transfer
5621 if not Is_Transfer (Last_Stmt) then
5623 -- The source location for the return is the end label of the
5624 -- procedure if present. Otherwise use the sloc of the last
5625 -- statement in the list. If the list comes from a generated
5626 -- exception handler and we are not debugging generated code,
5627 -- all the statements within the handler are made invisible
5628 -- to the debugger.
5630 if Nkind (Parent (Stmts)) = N_Exception_Handler
5631 and then not Comes_From_Source (Parent (Stmts))
5632 then
5633 Loc := Sloc (Last_Stmt);
5634 elsif Present (End_Label (HSS)) then
5635 Loc := Sloc (End_Label (HSS));
5636 else
5637 Loc := Sloc (Last_Stmt);
5638 end if;
5640 -- Append return statement, and set analyzed manually. We can't
5641 -- call Analyze on this return since the scope is wrong.
5643 -- Note: it almost works to push the scope and then do the Analyze
5644 -- call, but something goes wrong in some weird cases and it is
5645 -- not worth worrying about ???
5647 Stmt := Make_Simple_Return_Statement (Loc);
5649 -- The return statement is handled properly, and the call to the
5650 -- postcondition, inserted below, does not require information
5651 -- from the body either. However, that call is analyzed in the
5652 -- enclosing scope, and an elaboration check might improperly be
5653 -- added to it. A guard in Sem_Elab is needed to prevent that
5654 -- spurious check, see Check_Elab_Call.
5656 Append_To (Stmts, Stmt);
5657 Set_Analyzed (Stmt);
5659 -- Ada 2022 (AI12-0279): append the call to 'Yield unless this is
5660 -- a generic subprogram (since in such case it will be added to
5661 -- the instantiations).
5663 if Has_Yield_Aspect (Spec_Id)
5664 and then Ekind (Spec_Id) /= E_Generic_Procedure
5665 and then RTE_Available (RE_Yield)
5666 then
5667 Insert_Action (Stmt,
5668 Make_Procedure_Call_Statement (Loc,
5669 New_Occurrence_Of (RTE (RE_Yield), Loc)));
5670 end if;
5671 end if;
5672 end Add_Return;
5674 -- Local variables
5676 Except_H : Node_Id;
5677 L : List_Id;
5678 Spec_Id : Entity_Id;
5680 -- Start of processing for Expand_N_Subprogram_Body
5682 begin
5683 if Present (Corresponding_Spec (N)) then
5684 Spec_Id := Corresponding_Spec (N);
5685 else
5686 Spec_Id := Body_Id;
5687 end if;
5689 -- If this is a Pure function which has any parameters whose root type
5690 -- is System.Address, reset the Pure indication.
5691 -- This check is also performed when the subprogram is frozen, but we
5692 -- repeat it on the body so that the indication is consistent, and so
5693 -- it applies as well to bodies without separate specifications.
5695 if Is_Pure (Spec_Id)
5696 and then Is_Subprogram (Spec_Id)
5697 and then not Has_Pragma_Pure_Function (Spec_Id)
5698 then
5699 Check_Function_With_Address_Parameter (Spec_Id);
5701 if Spec_Id /= Body_Id then
5702 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5703 end if;
5704 end if;
5706 -- Set L to either the list of declarations if present, or to the list
5707 -- of statements if no declarations are present. This is used to insert
5708 -- new stuff at the start.
5710 if Is_Non_Empty_List (Declarations (N)) then
5711 L := Declarations (N);
5712 else
5713 L := Statements (HSS);
5714 end if;
5716 -- If local-exception-to-goto optimization active, insert dummy push
5717 -- statements at start, and dummy pop statements at end, but inhibit
5718 -- this if we have No_Exception_Handlers, since they are useless and
5719 -- interfere with analysis, e.g. by CodePeer. We also don't need these
5720 -- if we're unnesting subprograms because the only purpose of these
5721 -- nodes is to ensure we don't set a label in one subprogram and branch
5722 -- to it in another.
5724 if (Debug_Flag_Dot_G
5725 or else Restriction_Active (No_Exception_Propagation))
5726 and then not Restriction_Active (No_Exception_Handlers)
5727 and then not CodePeer_Mode
5728 and then not Unnest_Subprogram_Mode
5729 and then Is_Non_Empty_List (L)
5730 then
5731 declare
5732 FS : constant Node_Id := First (L);
5733 FL : constant Source_Ptr := Sloc (FS);
5734 LS : Node_Id;
5735 LL : Source_Ptr;
5737 begin
5738 -- LS points to either last statement, if statements are present
5739 -- or to the last declaration if there are no statements present.
5740 -- It is the node after which the pop's are generated.
5742 if Is_Non_Empty_List (Statements (HSS)) then
5743 LS := Last (Statements (HSS));
5744 else
5745 LS := Last (L);
5746 end if;
5748 LL := Sloc (LS);
5750 Insert_List_Before_And_Analyze (FS, New_List (
5751 Make_Push_Constraint_Error_Label (FL),
5752 Make_Push_Program_Error_Label (FL),
5753 Make_Push_Storage_Error_Label (FL)));
5755 Insert_List_After_And_Analyze (LS, New_List (
5756 Make_Pop_Constraint_Error_Label (LL),
5757 Make_Pop_Program_Error_Label (LL),
5758 Make_Pop_Storage_Error_Label (LL)));
5759 end;
5760 end if;
5762 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5764 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5765 declare
5766 F : Entity_Id;
5767 A : Node_Id;
5769 begin
5770 -- Loop through formals
5772 F := First_Formal (Spec_Id);
5773 while Present (F) loop
5774 if Is_Scalar_Type (Etype (F))
5775 and then Ekind (F) = E_Out_Parameter
5776 then
5777 Check_Restriction (No_Default_Initialization, F);
5779 -- Insert the initialization. We turn off validity checks
5780 -- for this assignment, since we do not want any check on
5781 -- the initial value itself (which may well be invalid).
5782 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5784 A :=
5785 Make_Assignment_Statement (Loc,
5786 Name => New_Occurrence_Of (F, Loc),
5787 Expression => Get_Simple_Init_Val (Etype (F), N));
5788 Set_Suppress_Assignment_Checks (A);
5790 Insert_Before_And_Analyze (First (L),
5791 A, Suppress => Validity_Check);
5792 end if;
5794 Next_Formal (F);
5795 end loop;
5796 end;
5797 end if;
5799 -- Clear out statement list for stubbed procedure
5801 if Present (Corresponding_Spec (N)) then
5802 Set_Elaboration_Flag (N, Spec_Id);
5804 if Convention (Spec_Id) = Convention_Stubbed
5805 or else Is_Eliminated (Spec_Id)
5806 then
5807 Set_Declarations (N, Empty_List);
5808 Set_Handled_Statement_Sequence (N,
5809 Make_Handled_Sequence_Of_Statements (Loc,
5810 Statements => New_List (Make_Null_Statement (Loc))));
5812 return;
5813 end if;
5814 end if;
5816 -- Create a set of discriminals for the next protected subprogram body
5818 if Is_List_Member (N)
5819 and then Present (Parent (List_Containing (N)))
5820 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5821 and then Present (Next_Protected_Operation (N))
5822 then
5823 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5824 end if;
5826 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5827 -- subprograms with no specs are not frozen.
5829 Compute_Returns_By_Ref (Spec_Id);
5831 -- For a procedure, we add a return for all possible syntactic ends of
5832 -- the subprogram.
5834 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure then
5835 Add_Return (Spec_Id, Statements (HSS));
5837 if Present (Exception_Handlers (HSS)) then
5838 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5839 while Present (Except_H) loop
5840 Add_Return (Spec_Id, Statements (Except_H));
5841 Next_Non_Pragma (Except_H);
5842 end loop;
5843 end if;
5845 -- For a function, we must deal with the case where there is at least
5846 -- one missing return. What we do is to wrap the entire body of the
5847 -- function in a block:
5849 -- begin
5850 -- ...
5851 -- end;
5853 -- becomes
5855 -- begin
5856 -- begin
5857 -- ...
5858 -- end;
5860 -- raise Program_Error;
5861 -- end;
5863 -- This approach is necessary because the raise must be signalled to the
5864 -- caller, not handled by any local handler (RM 6.4(11)).
5866 -- Note: we do not need to analyze the constructed sequence here, since
5867 -- it has no handler, and an attempt to analyze the handled statement
5868 -- sequence twice is risky in various ways (e.g. the issue of expanding
5869 -- cleanup actions twice).
5871 elsif Has_Missing_Return (Spec_Id) then
5872 declare
5873 Hloc : constant Source_Ptr := Sloc (HSS);
5874 Blok : constant Node_Id :=
5875 Make_Block_Statement (Hloc,
5876 Handled_Statement_Sequence => HSS);
5877 Rais : constant Node_Id :=
5878 Make_Raise_Program_Error (Hloc,
5879 Reason => PE_Missing_Return);
5881 begin
5882 Set_Handled_Statement_Sequence (N,
5883 Make_Handled_Sequence_Of_Statements (Hloc,
5884 Statements => New_List (Blok, Rais)));
5886 Push_Scope (Spec_Id);
5887 Analyze (Blok);
5888 Analyze (Rais);
5889 Pop_Scope;
5890 end;
5891 end if;
5893 -- If subprogram contains a parameterless recursive call, then we may
5894 -- have an infinite recursion, so see if we can generate code to check
5895 -- for this possibility if storage checks are not suppressed.
5897 if Ekind (Spec_Id) = E_Procedure
5898 and then Has_Recursive_Call (Spec_Id)
5899 and then not Storage_Checks_Suppressed (Spec_Id)
5900 then
5901 Detect_Infinite_Recursion (N, Spec_Id);
5902 end if;
5904 -- Set to encode entity names in package body before gigi is called
5906 Qualify_Entity_Names (N);
5908 -- If the body belongs to a nonabstract library-level source primitive
5909 -- of a tagged type, install an elaboration check which ensures that a
5910 -- dispatching call targeting the primitive will not execute the body
5911 -- without it being previously elaborated.
5913 Install_Primitive_Elaboration_Check (N);
5914 end Expand_N_Subprogram_Body;
5916 -----------------------------------
5917 -- Expand_N_Subprogram_Body_Stub --
5918 -----------------------------------
5920 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5921 Bod : Node_Id;
5923 begin
5924 if Present (Corresponding_Body (N)) then
5925 Bod := Unit_Declaration_Node (Corresponding_Body (N));
5927 -- The body may have been expanded already when it is analyzed
5928 -- through the subunit node. Do no expand again: it interferes
5929 -- with the construction of unnesting tables when generating C.
5931 if not Analyzed (Bod) then
5932 Expand_N_Subprogram_Body (Bod);
5933 end if;
5935 -- Add full qualification to entities that may be created late
5936 -- during unnesting.
5938 Qualify_Entity_Names (N);
5939 end if;
5940 end Expand_N_Subprogram_Body_Stub;
5942 -------------------------------------
5943 -- Expand_N_Subprogram_Declaration --
5944 -------------------------------------
5946 -- If the declaration appears within a protected body, it is a private
5947 -- operation of the protected type. We must create the corresponding
5948 -- protected subprogram an associated formals. For a normal protected
5949 -- operation, this is done when expanding the protected type declaration.
5951 -- If the declaration is for a null procedure, emit null body
5953 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5954 Loc : constant Source_Ptr := Sloc (N);
5955 Subp : constant Entity_Id := Defining_Entity (N);
5957 -- Local variables
5959 Scop : constant Entity_Id := Scope (Subp);
5960 Prot_Bod : Node_Id;
5961 Prot_Decl : Node_Id;
5962 Prot_Id : Entity_Id;
5963 Typ : Entity_Id;
5965 begin
5966 -- Deal with case of protected subprogram. Do not generate protected
5967 -- operation if operation is flagged as eliminated.
5969 if Is_List_Member (N)
5970 and then Present (Parent (List_Containing (N)))
5971 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5972 and then Is_Protected_Type (Scop)
5973 then
5974 if No (Protected_Body_Subprogram (Subp))
5975 and then not Is_Eliminated (Subp)
5976 then
5977 Prot_Decl :=
5978 Make_Subprogram_Declaration (Loc,
5979 Specification =>
5980 Build_Protected_Sub_Specification
5981 (N, Scop, Unprotected_Mode));
5983 -- The protected subprogram is declared outside of the protected
5984 -- body. Given that the body has frozen all entities so far, we
5985 -- analyze the subprogram and perform freezing actions explicitly.
5986 -- including the generation of an explicit freeze node, to ensure
5987 -- that gigi has the proper order of elaboration.
5988 -- If the body is a subunit, the insertion point is before the
5989 -- stub in the parent.
5991 Prot_Bod := Parent (List_Containing (N));
5993 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5994 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5995 end if;
5997 Insert_Before (Prot_Bod, Prot_Decl);
5998 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5999 Set_Has_Delayed_Freeze (Prot_Id);
6001 Push_Scope (Scope (Scop));
6002 Analyze (Prot_Decl);
6003 Freeze_Before (N, Prot_Id);
6004 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6005 Pop_Scope;
6006 end if;
6008 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6009 -- cases this is superfluous because calls to it will be automatically
6010 -- inlined, but we definitely need the body if preconditions for the
6011 -- procedure are present, or if performing coverage analysis.
6013 elsif Nkind (Specification (N)) = N_Procedure_Specification
6014 and then Null_Present (Specification (N))
6015 then
6016 declare
6017 Bod : constant Node_Id := Body_To_Inline (N);
6019 begin
6020 Set_Has_Completion (Subp, False);
6021 Append_Freeze_Action (Subp, Bod);
6023 -- The body now contains raise statements, so calls to it will
6024 -- not be inlined.
6026 Set_Is_Inlined (Subp, False);
6027 end;
6028 end if;
6030 -- When generating C code, transform a function that returns a
6031 -- constrained array type into a procedure with an out parameter
6032 -- that carries the return value.
6034 -- We skip this transformation for unchecked conversions, since they
6035 -- are not needed by the C generator (and this also produces cleaner
6036 -- output).
6038 Typ := Get_Fullest_View (Etype (Subp));
6040 if Transform_Function_Array
6041 and then Nkind (Specification (N)) = N_Function_Specification
6042 and then Is_Array_Type (Typ)
6043 and then Is_Constrained (Typ)
6044 and then not Is_Unchecked_Conversion_Instance (Subp)
6045 then
6046 Build_Procedure_Form (N);
6047 end if;
6048 end Expand_N_Subprogram_Declaration;
6050 --------------------------------
6051 -- Expand_Non_Function_Return --
6052 --------------------------------
6054 procedure Expand_Non_Function_Return (N : Node_Id) is
6055 pragma Assert (No (Expression (N)));
6057 Loc : constant Source_Ptr := Sloc (N);
6058 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
6059 Kind : constant Entity_Kind := Ekind (Scope_Id);
6060 Call : Node_Id;
6061 Acc_Stat : Node_Id;
6062 Goto_Stat : Node_Id;
6063 Lab_Node : Node_Id;
6065 begin
6066 -- Ada 2022 (AI12-0279)
6068 if Has_Yield_Aspect (Scope_Id)
6069 and then RTE_Available (RE_Yield)
6070 then
6071 Insert_Action (N,
6072 Make_Procedure_Call_Statement (Loc,
6073 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6074 end if;
6076 -- If it is a return from a procedure do no extra steps
6078 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6079 return;
6081 -- If it is a nested return within an extended one, replace it with a
6082 -- return of the previously declared return object.
6084 elsif Kind = E_Return_Statement then
6085 Rewrite (N,
6086 Make_Simple_Return_Statement (Loc,
6087 Expression =>
6088 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6089 Set_Comes_From_Extended_Return_Statement (N);
6090 Set_Return_Statement_Entity (N, Scope_Id);
6091 Expand_Simple_Function_Return (N);
6092 return;
6093 end if;
6095 pragma Assert (Is_Entry (Scope_Id));
6097 -- Look at the enclosing block to see whether the return is from an
6098 -- accept statement or an entry body.
6100 for J in reverse 0 .. Scope_Stack.Last loop
6101 Scope_Id := Scope_Stack.Table (J).Entity;
6102 exit when Is_Concurrent_Type (Scope_Id);
6103 end loop;
6105 -- If it is a return from accept statement it is expanded as call to
6106 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6108 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6109 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6111 if Is_Task_Type (Scope_Id) then
6113 Call :=
6114 Make_Procedure_Call_Statement (Loc,
6115 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6116 Insert_Before (N, Call);
6117 -- why not insert actions here???
6118 Analyze (Call);
6120 Acc_Stat := Parent (N);
6121 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6122 Acc_Stat := Parent (Acc_Stat);
6123 end loop;
6125 Lab_Node := Last (Statements
6126 (Handled_Statement_Sequence (Acc_Stat)));
6128 Goto_Stat := Make_Goto_Statement (Loc,
6129 Name => New_Occurrence_Of
6130 (Entity (Identifier (Lab_Node)), Loc));
6132 Set_Analyzed (Goto_Stat);
6134 Rewrite (N, Goto_Stat);
6135 Analyze (N);
6137 -- If it is a return from an entry body, put a Complete_Entry_Body call
6138 -- in front of the return.
6140 elsif Is_Protected_Type (Scope_Id) then
6141 Call :=
6142 Make_Procedure_Call_Statement (Loc,
6143 Name =>
6144 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6145 Parameter_Associations => New_List (
6146 Make_Attribute_Reference (Loc,
6147 Prefix =>
6148 New_Occurrence_Of
6149 (Find_Protection_Object (Current_Scope), Loc),
6150 Attribute_Name => Name_Unchecked_Access)));
6152 Insert_Before (N, Call);
6153 Analyze (Call);
6154 end if;
6155 end Expand_Non_Function_Return;
6157 ---------------------------------------
6158 -- Expand_Protected_Object_Reference --
6159 ---------------------------------------
6161 function Expand_Protected_Object_Reference
6162 (N : Node_Id;
6163 Scop : Entity_Id) return Node_Id
6165 Loc : constant Source_Ptr := Sloc (N);
6166 Corr : Entity_Id;
6167 Rec : Node_Id;
6168 Param : Entity_Id;
6169 Proc : Entity_Id;
6171 begin
6172 Rec := Make_Identifier (Loc, Name_uObject);
6173 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6175 -- Find enclosing protected operation, and retrieve its first parameter,
6176 -- which denotes the enclosing protected object. If the enclosing
6177 -- operation is an entry, we are immediately within the protected body,
6178 -- and we can retrieve the object from the service entries procedure. A
6179 -- barrier function has the same signature as an entry. A barrier
6180 -- function is compiled within the protected object, but unlike
6181 -- protected operations its never needs locks, so that its protected
6182 -- body subprogram points to itself.
6184 Proc := Current_Scope;
6185 while Present (Proc)
6186 and then Scope (Proc) /= Scop
6187 loop
6188 Proc := Scope (Proc);
6189 end loop;
6191 Corr := Protected_Body_Subprogram (Proc);
6193 if No (Corr) then
6195 -- Previous error left expansion incomplete.
6196 -- Nothing to do on this call.
6198 return Empty;
6199 end if;
6201 Param :=
6202 Defining_Identifier
6203 (First (Parameter_Specifications (Parent (Corr))));
6205 if Is_Subprogram (Proc) and then Proc /= Corr then
6207 -- Protected function or procedure
6209 Set_Entity (Rec, Param);
6211 -- Rec is a reference to an entity which will not be in scope when
6212 -- the call is reanalyzed, and needs no further analysis.
6214 Set_Analyzed (Rec);
6216 else
6217 -- Entry or barrier function for entry body. The first parameter of
6218 -- the entry body procedure is pointer to the object. We create a
6219 -- local variable of the proper type, duplicating what is done to
6220 -- define _object later on.
6222 declare
6223 Decls : List_Id;
6224 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6226 begin
6227 Decls := New_List (
6228 Make_Full_Type_Declaration (Loc,
6229 Defining_Identifier => Obj_Ptr,
6230 Type_Definition =>
6231 Make_Access_To_Object_Definition (Loc,
6232 Subtype_Indication =>
6233 New_Occurrence_Of
6234 (Corresponding_Record_Type (Scop), Loc))));
6236 Insert_Actions (N, Decls);
6237 Freeze_Before (N, Obj_Ptr);
6239 Rec :=
6240 Make_Explicit_Dereference (Loc,
6241 Prefix =>
6242 Unchecked_Convert_To (Obj_Ptr,
6243 New_Occurrence_Of (Param, Loc)));
6245 -- Analyze new actual. Other actuals in calls are already analyzed
6246 -- and the list of actuals is not reanalyzed after rewriting.
6248 Set_Parent (Rec, N);
6249 Analyze (Rec);
6250 end;
6251 end if;
6253 return Rec;
6254 end Expand_Protected_Object_Reference;
6256 --------------------------------------
6257 -- Expand_Protected_Subprogram_Call --
6258 --------------------------------------
6260 procedure Expand_Protected_Subprogram_Call
6261 (N : Node_Id;
6262 Subp : Entity_Id;
6263 Scop : Entity_Id)
6265 Rec : Node_Id;
6267 procedure Expand_Internal_Init_Call;
6268 -- A call to an operation of the type may occur in the initialization
6269 -- of a private component. In that case the prefix of the call is an
6270 -- entity name and the call is treated as internal even though it
6271 -- appears in code outside of the protected type.
6273 procedure Freeze_Called_Function;
6274 -- If it is a function call it can appear in elaboration code and
6275 -- the called entity must be frozen before the call. This must be
6276 -- done before the call is expanded, as the expansion may rewrite it
6277 -- to something other than a call (e.g. a temporary initialized in a
6278 -- transient block).
6280 -------------------------------
6281 -- Expand_Internal_Init_Call --
6282 -------------------------------
6284 procedure Expand_Internal_Init_Call is
6285 begin
6286 -- If the context is a protected object (rather than a protected
6287 -- type) the call itself is bound to raise program_error because
6288 -- the protected body will not have been elaborated yet. This is
6289 -- diagnosed subsequently in Sem_Elab.
6291 Freeze_Called_Function;
6293 -- The target of the internal call is the first formal of the
6294 -- enclosing initialization procedure.
6296 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6297 Build_Protected_Subprogram_Call (N,
6298 Name => Name (N),
6299 Rec => Rec,
6300 External => False);
6301 Analyze (N);
6302 Resolve (N, Etype (Subp));
6303 end Expand_Internal_Init_Call;
6305 ----------------------------
6306 -- Freeze_Called_Function --
6307 ----------------------------
6309 procedure Freeze_Called_Function is
6310 begin
6311 if Ekind (Subp) = E_Function then
6312 Freeze_Expression (Name (N));
6313 end if;
6314 end Freeze_Called_Function;
6316 -- Start of processing for Expand_Protected_Subprogram_Call
6318 begin
6319 -- If the protected object is not an enclosing scope, this is an inter-
6320 -- object function call. Inter-object procedure calls are expanded by
6321 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6322 -- subprogram being called is in the protected body being compiled, and
6323 -- if the protected object in the call is statically the enclosing type.
6324 -- The object may be a component of some other data structure, in which
6325 -- case this must be handled as an inter-object call.
6327 if not Scope_Within_Or_Same (Inner => Current_Scope, Outer => Scop)
6328 or else Is_Entry_Wrapper (Current_Scope)
6329 or else not Is_Entity_Name (Name (N))
6330 then
6331 if Nkind (Name (N)) = N_Selected_Component then
6332 Rec := Prefix (Name (N));
6334 elsif Nkind (Name (N)) = N_Indexed_Component then
6335 Rec := Prefix (Prefix (Name (N)));
6337 -- If this is a call within an entry wrapper, it appears within a
6338 -- precondition that calls another primitive of the synchronized
6339 -- type. The target object of the call is the first actual on the
6340 -- wrapper. Note that this is an external call, because the wrapper
6341 -- is called outside of the synchronized object. This means that
6342 -- an entry call to an entry with preconditions involves two
6343 -- synchronized operations.
6345 elsif Ekind (Current_Scope) = E_Procedure
6346 and then Is_Entry_Wrapper (Current_Scope)
6347 then
6348 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6350 -- A default parameter of a protected operation may be a call to
6351 -- a protected function of the type. This appears as an internal
6352 -- call in the profile of the operation, but if the context is an
6353 -- external call we must convert the call into an external one,
6354 -- using the protected object that is the target, so that:
6356 -- Prot.P (F)
6357 -- is transformed into
6358 -- Prot.P (Prot.F)
6360 elsif Nkind (Parent (N)) = N_Procedure_Call_Statement
6361 and then Nkind (Name (Parent (N))) = N_Selected_Component
6362 and then Is_Protected_Type (Etype (Prefix (Name (Parent (N)))))
6363 and then Is_Entity_Name (Name (N))
6364 and then Scope (Entity (Name (N))) =
6365 Etype (Prefix (Name (Parent (N))))
6366 then
6367 Rewrite (Name (N),
6368 Make_Selected_Component (Sloc (N),
6369 Prefix => New_Copy_Tree (Prefix (Name (Parent (N)))),
6370 Selector_Name => Relocate_Node (Name (N))));
6372 Analyze_And_Resolve (N);
6373 return;
6375 else
6376 -- If the context is the initialization procedure for a protected
6377 -- type, the call is legal because the called entity must be a
6378 -- function of that enclosing type, and this is treated as an
6379 -- internal call.
6381 pragma Assert
6382 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6384 Expand_Internal_Init_Call;
6385 return;
6386 end if;
6388 Freeze_Called_Function;
6389 Build_Protected_Subprogram_Call (N,
6390 Name => New_Occurrence_Of (Subp, Sloc (N)),
6391 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6392 External => True);
6394 else
6395 Rec := Expand_Protected_Object_Reference (N, Scop);
6397 if No (Rec) then
6398 return;
6399 end if;
6401 Freeze_Called_Function;
6402 Build_Protected_Subprogram_Call (N,
6403 Name => Name (N),
6404 Rec => Rec,
6405 External => False);
6406 end if;
6408 -- Analyze and resolve the new call. The actuals have already been
6409 -- resolved, but expansion of a function call will add extra actuals
6410 -- if needed. Analysis of a procedure call already includes resolution.
6412 Analyze (N);
6414 if Ekind (Subp) = E_Function then
6415 Resolve (N, Etype (Subp));
6416 end if;
6417 end Expand_Protected_Subprogram_Call;
6419 -----------------------------------
6420 -- Expand_Simple_Function_Return --
6421 -----------------------------------
6423 -- The "simple" comes from the syntax rule simple_return_statement. The
6424 -- semantics are not at all simple.
6426 procedure Expand_Simple_Function_Return (N : Node_Id) is
6427 Loc : constant Source_Ptr := Sloc (N);
6429 Scope_Id : constant Entity_Id :=
6430 Return_Applies_To (Return_Statement_Entity (N));
6431 -- The function we are returning from
6433 R_Type : constant Entity_Id := Etype (Scope_Id);
6434 -- The result type of the function
6436 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6437 -- The underlying result type of the function
6439 Exp : Node_Id := Expression (N);
6440 pragma Assert (Present (Exp));
6442 Exp_Is_Function_Call : constant Boolean :=
6443 Nkind (Exp) = N_Function_Call
6444 or else
6445 (Is_Captured_Function_Call (Exp)
6446 and then Is_Related_To_Func_Return (Entity (Prefix (Exp))));
6447 -- If the expression is a captured function call, then we need to make
6448 -- sure that the object doing the capture is properly recognized by the
6449 -- Is_Related_To_Func_Return predicate; otherwise, if it is of a type
6450 -- that needs finalization, Requires_Cleanup_Actions would return true
6451 -- because of this and Build_Finalizer would finalize it prematurely.
6453 Exp_Typ : constant Entity_Id := Etype (Exp);
6454 -- The type of the expression (not necessarily the same as R_Type)
6456 Subtype_Ind : Node_Id;
6457 -- If the result type of the function is class-wide and the expression
6458 -- has a specific type, then we use the expression's type as the type of
6459 -- the return object. In cases where the expression is an aggregate that
6460 -- is built in place, this avoids the need for an expensive conversion
6461 -- of the return object to the specific type on assignments to the
6462 -- individual components.
6464 -- Start of processing for Expand_Simple_Function_Return
6466 begin
6467 if Is_Class_Wide_Type (R_Type)
6468 and then not Is_Class_Wide_Type (Exp_Typ)
6469 and then Nkind (Exp) /= N_Type_Conversion
6470 then
6471 Subtype_Ind := New_Occurrence_Of (Exp_Typ, Loc);
6472 else
6473 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6475 -- If the result type is class-wide and the expression is a view
6476 -- conversion, the conversion plays no role in the expansion because
6477 -- it does not modify the tag of the object. Remove the conversion
6478 -- altogether to prevent tag overwriting.
6480 if Is_Class_Wide_Type (R_Type)
6481 and then not Is_Class_Wide_Type (Exp_Typ)
6482 and then Nkind (Exp) = N_Type_Conversion
6483 then
6484 Exp := Expression (Exp);
6485 end if;
6486 end if;
6488 -- Assert that if F says "return G(...);"
6489 -- then F and G are both b-i-p, or neither b-i-p.
6491 if Nkind (Exp) = N_Function_Call then
6492 pragma Assert (Ekind (Scope_Id) = E_Function);
6494 -- This assertion works fine because Is_Build_In_Place_Function_Call
6495 -- returns True for BIP function calls but also for function calls
6496 -- that have BIP formals.
6498 pragma Assert
6499 (Has_BIP_Formals (Scope_Id) =
6500 Is_Build_In_Place_Function_Call (Exp));
6501 null;
6502 end if;
6504 -- For the case of a simple return that does not come from an
6505 -- extended return, in the case of build-in-place, we rewrite
6506 -- "return <expression>;" to be:
6508 -- return _anon_ : <return_subtype> := <expression>
6510 -- The expansion produced by Expand_N_Extended_Return_Statement will
6511 -- contain simple return statements (for example, a block containing
6512 -- simple return of the return object), which brings us back here with
6513 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6514 -- checking for a simple return that does not come from an extended
6515 -- return is to avoid this infinite recursion.
6517 -- The reason for this design is that for Ada 2005 limited returns, we
6518 -- need to reify the return object, so we can build it "in place", and
6519 -- we need a block statement to hang finalization and tasking stuff.
6521 pragma Assert
6522 (Comes_From_Extended_Return_Statement (N)
6523 or else not Is_Build_In_Place_Function_Call (Exp)
6524 or else Has_BIP_Formals (Scope_Id));
6526 if not Comes_From_Extended_Return_Statement (N)
6527 and then Is_Build_In_Place_Function (Scope_Id)
6529 -- The functionality of interface thunks is simple and it is always
6530 -- handled by means of simple return statements. This leaves their
6531 -- expansion simple and clean.
6533 and then not Is_Thunk (Scope_Id)
6534 then
6535 declare
6536 Return_Object_Entity : constant Entity_Id :=
6537 Make_Temporary (Loc, 'R', Exp);
6539 Obj_Decl : constant Node_Id :=
6540 Make_Object_Declaration (Loc,
6541 Defining_Identifier => Return_Object_Entity,
6542 Object_Definition => Subtype_Ind,
6543 Expression => Exp);
6545 Ext : constant Node_Id :=
6546 Make_Extended_Return_Statement (Loc,
6547 Return_Object_Declarations => New_List (Obj_Decl));
6548 -- Do not perform this high-level optimization if the result type
6549 -- is an interface because the "this" pointer must be displaced.
6551 begin
6552 Rewrite (N, Ext);
6553 Analyze (N);
6554 return;
6555 end;
6556 end if;
6558 -- Here we have a simple return statement that is part of the expansion
6559 -- of an extended return statement (either written by the user, or
6560 -- generated by the above code).
6562 -- Always normalize C/Fortran boolean result. This is not always needed,
6563 -- but it seems a good idea to minimize the passing around of non-
6564 -- normalized values, and in any case this handles the processing of
6565 -- barrier functions for protected types, which turn the condition into
6566 -- a return statement.
6568 if Is_Boolean_Type (Exp_Typ) and then Nonzero_Is_True (Exp_Typ) then
6569 Adjust_Condition (Exp);
6570 Adjust_Result_Type (Exp, Exp_Typ);
6571 end if;
6573 -- Do validity check if enabled for returns
6575 if Validity_Checks_On and then Validity_Check_Returns then
6576 Ensure_Valid (Exp);
6577 end if;
6579 -- Check the result expression of a scalar function against the subtype
6580 -- of the function by inserting a conversion. This conversion must
6581 -- eventually be performed for other classes of types, but for now it's
6582 -- only done for scalars ???
6584 if Is_Scalar_Type (Exp_Typ) and then Exp_Typ /= R_Type then
6585 Rewrite (Exp, Convert_To (R_Type, Exp));
6587 -- The expression is resolved to ensure that the conversion gets
6588 -- expanded to generate a possible constraint check.
6590 Analyze_And_Resolve (Exp, R_Type);
6591 end if;
6593 -- Deal with returning variable length objects and controlled types
6595 -- Nothing to do if we are returning by reference
6597 if Is_Build_In_Place_Function (Scope_Id) then
6598 -- Prevent the reclamation of the secondary stack by all enclosing
6599 -- blocks and loops as well as the related function; otherwise the
6600 -- result would be reclaimed too early.
6602 if Needs_BIP_Alloc_Form (Scope_Id) then
6603 Set_Enclosing_Sec_Stack_Return (N);
6604 end if;
6606 elsif Is_Limited_View (R_Type) then
6607 null;
6609 -- No copy needed for thunks returning interface type objects since
6610 -- the object is returned by reference and the maximum functionality
6611 -- required is just to displace the pointer.
6613 elsif Is_Thunk (Scope_Id) and then Is_Interface (Exp_Typ) then
6614 null;
6616 -- If the call is within a thunk and the type is a limited view, the
6617 -- back end will eventually see the non-limited view of the type.
6619 elsif Is_Thunk (Scope_Id) and then Is_Incomplete_Type (Exp_Typ) then
6620 return;
6622 -- A return statement from an ignored Ghost function does not use the
6623 -- secondary stack (or any other one).
6625 elsif (not Needs_Secondary_Stack (R_Type)
6626 and then not Is_Secondary_Stack_Thunk (Scope_Id))
6627 or else Is_Ignored_Ghost_Entity (Scope_Id)
6628 then
6629 -- Mutable records with variable-length components are not returned
6630 -- on the sec-stack, so we need to make sure that the back end will
6631 -- only copy back the size of the actual value, and not the maximum
6632 -- size. We create an actual subtype for this purpose. However we
6633 -- need not do it if the expression is a function call since this
6634 -- will be done in the called function and doing it here too would
6635 -- cause a temporary with maximum size to be created. Likewise for
6636 -- a special return object, since there is no copy in this case.
6638 declare
6639 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exp_Typ));
6640 Decl : Node_Id;
6641 Ent : Entity_Id;
6643 begin
6644 if not Exp_Is_Function_Call
6645 and then not (Is_Entity_Name (Exp)
6646 and then Is_Special_Return_Object (Entity (Exp)))
6647 and then Has_Defaulted_Discriminants (Ubt)
6648 and then not Is_Constrained (Ubt)
6649 and then not Has_Unchecked_Union (Ubt)
6650 then
6651 Decl := Build_Actual_Subtype (Ubt, Exp);
6652 Ent := Defining_Identifier (Decl);
6653 Insert_Action (Exp, Decl);
6654 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6655 Analyze_And_Resolve (Exp);
6656 end if;
6657 end;
6659 -- For types which need finalization, do the allocation on the return
6660 -- stack manually in order to call Adjust at the right time:
6662 -- type Ann is access R_Type;
6663 -- for Ann'Storage_pool use rs_pool;
6664 -- Rnn : constant Ann := new Exp_Typ'(Exp);
6665 -- return Rnn.all;
6667 -- but optimize the case where the result is a function call that
6668 -- also needs finalization. In this case the result can directly be
6669 -- allocated on the return stack of the caller and no further
6670 -- processing is required. Likewise if this is a return object.
6672 if Comes_From_Extended_Return_Statement (N) then
6673 null;
6675 elsif Present (Utyp)
6676 and then Needs_Finalization (Utyp)
6677 and then not (Exp_Is_Function_Call
6678 and then Needs_Finalization (Exp_Typ))
6679 then
6680 declare
6681 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6683 Alloc_Node : Node_Id;
6684 Temp : Entity_Id;
6686 begin
6687 Mutate_Ekind (Acc_Typ, E_Access_Type);
6689 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_RS_Pool));
6691 -- This is an allocator for the return stack, and it's fine
6692 -- to have Comes_From_Source set False on it, as gigi knows not
6693 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6695 Alloc_Node :=
6696 Make_Allocator (Loc,
6697 Expression =>
6698 Make_Qualified_Expression (Loc,
6699 Subtype_Mark => New_Occurrence_Of (Exp_Typ, Loc),
6700 Expression => Relocate_Node (Exp)));
6702 -- We do not want discriminant checks on the declaration,
6703 -- given that it gets its value from the allocator.
6705 Set_No_Initialization (Alloc_Node);
6707 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6709 Insert_Actions (Exp, New_List (
6710 Make_Full_Type_Declaration (Loc,
6711 Defining_Identifier => Acc_Typ,
6712 Type_Definition =>
6713 Make_Access_To_Object_Definition (Loc,
6714 Subtype_Indication => Subtype_Ind)),
6716 Make_Object_Declaration (Loc,
6717 Defining_Identifier => Temp,
6718 Constant_Present => True,
6719 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6720 Expression => Alloc_Node)));
6722 Rewrite (Exp,
6723 Make_Explicit_Dereference (Loc,
6724 Prefix => New_Occurrence_Of (Temp, Loc)));
6726 Analyze_And_Resolve (Exp, R_Type);
6727 end;
6728 end if;
6730 -- Here if secondary stack is used
6732 else
6733 -- Prevent the reclamation of the secondary stack by all enclosing
6734 -- blocks and loops as well as the related function; otherwise the
6735 -- result would be reclaimed too early.
6737 Set_Enclosing_Sec_Stack_Return (N);
6739 -- Nothing else to do for a return object
6741 if Comes_From_Extended_Return_Statement (N) then
6742 null;
6744 -- Optimize the case where the result is a function call that also
6745 -- returns on the secondary stack; in this case the result is already
6746 -- on the secondary stack and no further processing is required.
6748 elsif Exp_Is_Function_Call
6749 and then Needs_Secondary_Stack (Exp_Typ)
6750 then
6751 -- Remove side effects from the expression now so that other parts
6752 -- of the expander do not have to reanalyze this node without this
6753 -- optimization
6755 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6757 -- Ada 2005 (AI-251): If the type of the returned object is
6758 -- an interface then add an implicit type conversion to force
6759 -- displacement of the "this" pointer.
6761 if Is_Interface (R_Type) then
6762 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6763 end if;
6765 Analyze_And_Resolve (Exp, R_Type);
6767 -- For types which both need finalization and are returned on the
6768 -- secondary stack, do the allocation on secondary stack manually
6769 -- in order to call Adjust at the right time:
6771 -- type Ann is access R_Type;
6772 -- for Ann'Storage_pool use ss_pool;
6773 -- Rnn : constant Ann := new Exp_Typ'(Exp);
6774 -- return Rnn.all;
6776 -- And we do the same for class-wide types that are not potentially
6777 -- controlled (by the virtue of restriction No_Finalization) because
6778 -- gigi is not able to properly allocate class-wide types.
6780 -- But optimize the case where the result is a function call that
6781 -- also needs finalization; in this case the result can directly be
6782 -- allocated on the secondary stack and no further processing is
6783 -- required, unless the returned object is an interface.
6785 elsif CW_Or_Needs_Finalization (Utyp)
6786 and then (Is_Interface (R_Type)
6787 or else 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;